xref: /titanic_44/usr/src/uts/common/io/e1000g/e1000g_main.c (revision abddfefb3168362a915cd681eb5a6498ec6c9e09)
1 /*
2  * This file is provided under a CDDLv1 license.  When using or
3  * redistributing this file, you may do so under this license.
4  * In redistributing this file this license must be included
5  * and no other modification of this header file is permitted.
6  *
7  * CDDL LICENSE SUMMARY
8  *
9  * Copyright(c) 1999 - 2009 Intel Corporation. All rights reserved.
10  *
11  * The contents of this file are subject to the terms of Version
12  * 1.0 of the Common Development and Distribution License (the "License").
13  *
14  * You should have received a copy of the License with this software.
15  * You can obtain a copy of the License at
16  *	http://www.opensolaris.org/os/licensing.
17  * See the License for the specific language governing permissions
18  * and limitations under the License.
19  */
20 
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * **********************************************************************
28  *									*
29  * Module Name:								*
30  *   e1000g_main.c							*
31  *									*
32  * Abstract:								*
33  *   This file contains the interface routines for the solaris OS.	*
34  *   It has all DDI entry point routines and GLD entry point routines.	*
35  *									*
36  *   This file also contains routines that take care of initialization	*
37  *   uninit routine and interrupt routine.				*
38  *									*
39  * **********************************************************************
40  */
41 
42 #include <sys/dlpi.h>
43 #include <sys/mac.h>
44 #include "e1000g_sw.h"
45 #include "e1000g_debug.h"
46 
47 static char ident[] = "Intel PRO/1000 Ethernet";
48 static char e1000g_string[] = "Intel(R) PRO/1000 Network Connection";
49 static char e1000g_version[] = "Driver Ver. 5.3.8";
50 
51 /*
52  * Proto types for DDI entry points
53  */
54 static int e1000g_attach(dev_info_t *, ddi_attach_cmd_t);
55 static int e1000g_detach(dev_info_t *, ddi_detach_cmd_t);
56 static int e1000g_quiesce(dev_info_t *);
57 
58 /*
59  * init and intr routines prototype
60  */
61 static int e1000g_resume(dev_info_t *);
62 static int e1000g_suspend(dev_info_t *);
63 static uint_t e1000g_intr_pciexpress(caddr_t);
64 static uint_t e1000g_intr(caddr_t);
65 static void e1000g_intr_work(struct e1000g *, uint32_t);
66 #pragma inline(e1000g_intr_work)
67 static int e1000g_init(struct e1000g *);
68 static int e1000g_start(struct e1000g *, boolean_t);
69 static void e1000g_stop(struct e1000g *, boolean_t);
70 static int e1000g_m_start(void *);
71 static void e1000g_m_stop(void *);
72 static int e1000g_m_promisc(void *, boolean_t);
73 static boolean_t e1000g_m_getcapab(void *, mac_capab_t, void *);
74 static int e1000g_m_multicst(void *, boolean_t, const uint8_t *);
75 static void e1000g_m_ioctl(void *, queue_t *, mblk_t *);
76 static int e1000g_m_setprop(void *, const char *, mac_prop_id_t,
77     uint_t, const void *);
78 static int e1000g_m_getprop(void *, const char *, mac_prop_id_t,
79     uint_t, uint_t, void *, uint_t *);
80 static int e1000g_set_priv_prop(struct e1000g *, const char *, uint_t,
81     const void *);
82 static int e1000g_get_priv_prop(struct e1000g *, const char *, uint_t,
83     uint_t, void *, uint_t *);
84 static void e1000g_init_locks(struct e1000g *);
85 static void e1000g_destroy_locks(struct e1000g *);
86 static int e1000g_identify_hardware(struct e1000g *);
87 static int e1000g_regs_map(struct e1000g *);
88 static int e1000g_set_driver_params(struct e1000g *);
89 static void e1000g_set_bufsize(struct e1000g *);
90 static int e1000g_register_mac(struct e1000g *);
91 static boolean_t e1000g_rx_drain(struct e1000g *);
92 static boolean_t e1000g_tx_drain(struct e1000g *);
93 static void e1000g_init_unicst(struct e1000g *);
94 static int e1000g_unicst_set(struct e1000g *, const uint8_t *, int);
95 static int e1000g_alloc_rx_data(struct e1000g *);
96 
97 /*
98  * Local routines
99  */
100 static boolean_t e1000g_reset_adapter(struct e1000g *);
101 static void e1000g_tx_clean(struct e1000g *);
102 static void e1000g_rx_clean(struct e1000g *);
103 static void e1000g_link_timer(void *);
104 static void e1000g_local_timer(void *);
105 static boolean_t e1000g_link_check(struct e1000g *);
106 static boolean_t e1000g_stall_check(struct e1000g *);
107 static void e1000g_smartspeed(struct e1000g *);
108 static void e1000g_get_conf(struct e1000g *);
109 static int e1000g_get_prop(struct e1000g *, char *, int, int, int);
110 static void enable_watchdog_timer(struct e1000g *);
111 static void disable_watchdog_timer(struct e1000g *);
112 static void start_watchdog_timer(struct e1000g *);
113 static void restart_watchdog_timer(struct e1000g *);
114 static void stop_watchdog_timer(struct e1000g *);
115 static void stop_link_timer(struct e1000g *);
116 static void stop_82547_timer(e1000g_tx_ring_t *);
117 static void e1000g_force_speed_duplex(struct e1000g *);
118 static void e1000g_get_max_frame_size(struct e1000g *);
119 static boolean_t is_valid_mac_addr(uint8_t *);
120 static void e1000g_unattach(dev_info_t *, struct e1000g *);
121 #ifdef E1000G_DEBUG
122 static void e1000g_ioc_peek_reg(struct e1000g *, e1000g_peekpoke_t *);
123 static void e1000g_ioc_poke_reg(struct e1000g *, e1000g_peekpoke_t *);
124 static void e1000g_ioc_peek_mem(struct e1000g *, e1000g_peekpoke_t *);
125 static void e1000g_ioc_poke_mem(struct e1000g *, e1000g_peekpoke_t *);
126 static enum ioc_reply e1000g_pp_ioctl(struct e1000g *,
127     struct iocblk *, mblk_t *);
128 #endif
129 static enum ioc_reply e1000g_loopback_ioctl(struct e1000g *,
130     struct iocblk *, mblk_t *);
131 static boolean_t e1000g_check_loopback_support(struct e1000_hw *);
132 static boolean_t e1000g_set_loopback_mode(struct e1000g *, uint32_t);
133 static void e1000g_set_internal_loopback(struct e1000g *);
134 static void e1000g_set_external_loopback_1000(struct e1000g *);
135 static void e1000g_set_external_loopback_100(struct e1000g *);
136 static void e1000g_set_external_loopback_10(struct e1000g *);
137 static int e1000g_add_intrs(struct e1000g *);
138 static int e1000g_intr_add(struct e1000g *, int);
139 static int e1000g_rem_intrs(struct e1000g *);
140 static int e1000g_enable_intrs(struct e1000g *);
141 static int e1000g_disable_intrs(struct e1000g *);
142 static boolean_t e1000g_link_up(struct e1000g *);
143 #ifdef __sparc
144 static boolean_t e1000g_find_mac_address(struct e1000g *);
145 #endif
146 static void e1000g_get_phy_state(struct e1000g *);
147 static int e1000g_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err,
148     const void *impl_data);
149 static void e1000g_fm_init(struct e1000g *Adapter);
150 static void e1000g_fm_fini(struct e1000g *Adapter);
151 static int e1000g_get_def_val(struct e1000g *, mac_prop_id_t, uint_t, void *);
152 static void e1000g_param_sync(struct e1000g *);
153 static void e1000g_get_driver_control(struct e1000_hw *);
154 static void e1000g_release_driver_control(struct e1000_hw *);
155 static void e1000g_restore_promisc(struct e1000g *Adapter);
156 
157 mac_priv_prop_t e1000g_priv_props[] = {
158 	{"_tx_bcopy_threshold", MAC_PROP_PERM_RW},
159 	{"_tx_interrupt_enable", MAC_PROP_PERM_RW},
160 	{"_tx_intr_delay", MAC_PROP_PERM_RW},
161 	{"_tx_intr_abs_delay", MAC_PROP_PERM_RW},
162 	{"_rx_bcopy_threshold", MAC_PROP_PERM_RW},
163 	{"_max_num_rcv_packets", MAC_PROP_PERM_RW},
164 	{"_rx_intr_delay", MAC_PROP_PERM_RW},
165 	{"_rx_intr_abs_delay", MAC_PROP_PERM_RW},
166 	{"_intr_throttling_rate", MAC_PROP_PERM_RW},
167 	{"_intr_adaptive", MAC_PROP_PERM_RW},
168 	{"_adv_pause_cap", MAC_PROP_PERM_READ},
169 	{"_adv_asym_pause_cap", MAC_PROP_PERM_READ},
170 };
171 #define	E1000G_MAX_PRIV_PROPS	\
172 	(sizeof (e1000g_priv_props)/sizeof (mac_priv_prop_t))
173 
174 
175 static struct cb_ops cb_ws_ops = {
176 	nulldev,		/* cb_open */
177 	nulldev,		/* cb_close */
178 	nodev,			/* cb_strategy */
179 	nodev,			/* cb_print */
180 	nodev,			/* cb_dump */
181 	nodev,			/* cb_read */
182 	nodev,			/* cb_write */
183 	nodev,			/* cb_ioctl */
184 	nodev,			/* cb_devmap */
185 	nodev,			/* cb_mmap */
186 	nodev,			/* cb_segmap */
187 	nochpoll,		/* cb_chpoll */
188 	ddi_prop_op,		/* cb_prop_op */
189 	NULL,			/* cb_stream */
190 	D_MP | D_HOTPLUG,	/* cb_flag */
191 	CB_REV,			/* cb_rev */
192 	nodev,			/* cb_aread */
193 	nodev			/* cb_awrite */
194 };
195 
196 static struct dev_ops ws_ops = {
197 	DEVO_REV,		/* devo_rev */
198 	0,			/* devo_refcnt */
199 	NULL,			/* devo_getinfo */
200 	nulldev,		/* devo_identify */
201 	nulldev,		/* devo_probe */
202 	e1000g_attach,		/* devo_attach */
203 	e1000g_detach,		/* devo_detach */
204 	nodev,			/* devo_reset */
205 	&cb_ws_ops,		/* devo_cb_ops */
206 	NULL,			/* devo_bus_ops */
207 	ddi_power,		/* devo_power */
208 	e1000g_quiesce		/* devo_quiesce */
209 };
210 
211 static struct modldrv modldrv = {
212 	&mod_driverops,		/* Type of module.  This one is a driver */
213 	ident,			/* Discription string */
214 	&ws_ops,		/* driver ops */
215 };
216 
217 static struct modlinkage modlinkage = {
218 	MODREV_1, &modldrv, NULL
219 };
220 
221 /* Access attributes for register mapping */
222 static ddi_device_acc_attr_t e1000g_regs_acc_attr = {
223 	DDI_DEVICE_ATTR_V0,
224 	DDI_STRUCTURE_LE_ACC,
225 	DDI_STRICTORDER_ACC,
226 	DDI_FLAGERR_ACC
227 };
228 
229 #define	E1000G_M_CALLBACK_FLAGS \
230 	(MC_IOCTL | MC_GETCAPAB | MC_SETPROP | MC_GETPROP)
231 
232 static mac_callbacks_t e1000g_m_callbacks = {
233 	E1000G_M_CALLBACK_FLAGS,
234 	e1000g_m_stat,
235 	e1000g_m_start,
236 	e1000g_m_stop,
237 	e1000g_m_promisc,
238 	e1000g_m_multicst,
239 	NULL,
240 	e1000g_m_tx,
241 	e1000g_m_ioctl,
242 	e1000g_m_getcapab,
243 	NULL,
244 	NULL,
245 	e1000g_m_setprop,
246 	e1000g_m_getprop
247 };
248 
249 /*
250  * Global variables
251  */
252 uint32_t e1000g_mblks_pending = 0;
253 /*
254  * Workaround for Dynamic Reconfiguration support, for x86 platform only.
255  * Here we maintain a private dev_info list if e1000g_force_detach is
256  * enabled. If we force the driver to detach while there are still some
257  * rx buffers retained in the upper layer, we have to keep a copy of the
258  * dev_info. In some cases (Dynamic Reconfiguration), the dev_info data
259  * structure will be freed after the driver is detached. However when we
260  * finally free those rx buffers released by the upper layer, we need to
261  * refer to the dev_info to free the dma buffers. So we save a copy of
262  * the dev_info for this purpose. On x86 platform, we assume this copy
263  * of dev_info is always valid, but on SPARC platform, it could be invalid
264  * after the system board level DR operation. For this reason, the global
265  * variable e1000g_force_detach must be B_FALSE on SPARC platform.
266  */
267 #ifdef __sparc
268 boolean_t e1000g_force_detach = B_FALSE;
269 #else
270 boolean_t e1000g_force_detach = B_TRUE;
271 #endif
272 private_devi_list_t *e1000g_private_devi_list = NULL;
273 
274 /*
275  * The mutex e1000g_rx_detach_lock is defined to protect the processing of
276  * the private dev_info list, and to serialize the processing of rx buffer
277  * freeing and rx buffer recycling.
278  */
279 kmutex_t e1000g_rx_detach_lock;
280 /*
281  * The rwlock e1000g_dma_type_lock is defined to protect the global flag
282  * e1000g_dma_type. For SPARC, the initial value of the flag is "USE_DVMA".
283  * If there are many e1000g instances, the system may run out of DVMA
284  * resources during the initialization of the instances, then the flag will
285  * be changed to "USE_DMA". Because different e1000g instances are initialized
286  * in parallel, we need to use this lock to protect the flag.
287  */
288 krwlock_t e1000g_dma_type_lock;
289 
290 /*
291  * The 82546 chipset is a dual-port device, both the ports share one eeprom.
292  * Based on the information from Intel, the 82546 chipset has some hardware
293  * problem. When one port is being reset and the other port is trying to
294  * access the eeprom, it could cause system hang or panic. To workaround this
295  * hardware problem, we use a global mutex to prevent such operations from
296  * happening simultaneously on different instances. This workaround is applied
297  * to all the devices supported by this driver.
298  */
299 kmutex_t e1000g_nvm_lock;
300 
301 /*
302  * Loadable module configuration entry points for the driver
303  */
304 
305 /*
306  * _init - module initialization
307  */
308 int
309 _init(void)
310 {
311 	int status;
312 
313 	mac_init_ops(&ws_ops, WSNAME);
314 	status = mod_install(&modlinkage);
315 	if (status != DDI_SUCCESS)
316 		mac_fini_ops(&ws_ops);
317 	else {
318 		mutex_init(&e1000g_rx_detach_lock, NULL, MUTEX_DRIVER, NULL);
319 		rw_init(&e1000g_dma_type_lock, NULL, RW_DRIVER, NULL);
320 		mutex_init(&e1000g_nvm_lock, NULL, MUTEX_DRIVER, NULL);
321 	}
322 
323 	return (status);
324 }
325 
326 /*
327  * _fini - module finalization
328  */
329 int
330 _fini(void)
331 {
332 	int status;
333 
334 	if (e1000g_mblks_pending != 0)
335 		return (EBUSY);
336 
337 	status = mod_remove(&modlinkage);
338 	if (status == DDI_SUCCESS) {
339 		mac_fini_ops(&ws_ops);
340 
341 		if (e1000g_force_detach) {
342 			private_devi_list_t *devi_node;
343 
344 			mutex_enter(&e1000g_rx_detach_lock);
345 			while (e1000g_private_devi_list != NULL) {
346 				devi_node = e1000g_private_devi_list;
347 				e1000g_private_devi_list =
348 				    e1000g_private_devi_list->next;
349 
350 				kmem_free(devi_node->priv_dip,
351 				    sizeof (struct dev_info));
352 				kmem_free(devi_node,
353 				    sizeof (private_devi_list_t));
354 			}
355 			mutex_exit(&e1000g_rx_detach_lock);
356 		}
357 
358 		mutex_destroy(&e1000g_rx_detach_lock);
359 		rw_destroy(&e1000g_dma_type_lock);
360 		mutex_destroy(&e1000g_nvm_lock);
361 	}
362 
363 	return (status);
364 }
365 
366 /*
367  * _info - module information
368  */
369 int
370 _info(struct modinfo *modinfop)
371 {
372 	return (mod_info(&modlinkage, modinfop));
373 }
374 
375 /*
376  * e1000g_attach - driver attach
377  *
378  * This function is the device-specific initialization entry
379  * point. This entry point is required and must be written.
380  * The DDI_ATTACH command must be provided in the attach entry
381  * point. When attach() is called with cmd set to DDI_ATTACH,
382  * all normal kernel services (such as kmem_alloc(9F)) are
383  * available for use by the driver.
384  *
385  * The attach() function will be called once for each instance
386  * of  the  device  on  the  system with cmd set to DDI_ATTACH.
387  * Until attach() succeeds, the only driver entry points which
388  * may be called are open(9E) and getinfo(9E).
389  */
390 static int
391 e1000g_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
392 {
393 	struct e1000g *Adapter;
394 	struct e1000_hw *hw;
395 	struct e1000g_osdep *osdep;
396 	int instance;
397 
398 	switch (cmd) {
399 	default:
400 		e1000g_log(NULL, CE_WARN,
401 		    "Unsupported command send to e1000g_attach... ");
402 		return (DDI_FAILURE);
403 
404 	case DDI_RESUME:
405 		return (e1000g_resume(devinfo));
406 
407 	case DDI_ATTACH:
408 		break;
409 	}
410 
411 	/*
412 	 * get device instance number
413 	 */
414 	instance = ddi_get_instance(devinfo);
415 
416 	/*
417 	 * Allocate soft data structure
418 	 */
419 	Adapter =
420 	    (struct e1000g *)kmem_zalloc(sizeof (*Adapter), KM_SLEEP);
421 
422 	Adapter->dip = devinfo;
423 	Adapter->instance = instance;
424 	Adapter->tx_ring->adapter = Adapter;
425 	Adapter->rx_ring->adapter = Adapter;
426 
427 	hw = &Adapter->shared;
428 	osdep = &Adapter->osdep;
429 	hw->back = osdep;
430 	osdep->adapter = Adapter;
431 
432 	ddi_set_driver_private(devinfo, (caddr_t)Adapter);
433 
434 	/*
435 	 * Initialize for fma support
436 	 */
437 	Adapter->fm_capabilities = e1000g_get_prop(Adapter, "fm-capable",
438 	    0, 0x0f,
439 	    DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
440 	    DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
441 	e1000g_fm_init(Adapter);
442 	Adapter->attach_progress |= ATTACH_PROGRESS_FMINIT;
443 
444 	/*
445 	 * PCI Configure
446 	 */
447 	if (pci_config_setup(devinfo, &osdep->cfg_handle) != DDI_SUCCESS) {
448 		e1000g_log(Adapter, CE_WARN, "PCI configuration failed");
449 		goto attach_fail;
450 	}
451 	Adapter->attach_progress |= ATTACH_PROGRESS_PCI_CONFIG;
452 
453 	/*
454 	 * Setup hardware
455 	 */
456 	if (e1000g_identify_hardware(Adapter) != DDI_SUCCESS) {
457 		e1000g_log(Adapter, CE_WARN, "Identify hardware failed");
458 		goto attach_fail;
459 	}
460 
461 	/*
462 	 * Map in the device registers.
463 	 */
464 	if (e1000g_regs_map(Adapter) != DDI_SUCCESS) {
465 		e1000g_log(Adapter, CE_WARN, "Mapping registers failed");
466 		goto attach_fail;
467 	}
468 	Adapter->attach_progress |= ATTACH_PROGRESS_REGS_MAP;
469 
470 	/*
471 	 * Initialize driver parameters
472 	 */
473 	if (e1000g_set_driver_params(Adapter) != DDI_SUCCESS) {
474 		goto attach_fail;
475 	}
476 	Adapter->attach_progress |= ATTACH_PROGRESS_SETUP;
477 
478 	if (e1000g_check_acc_handle(Adapter->osdep.cfg_handle) != DDI_FM_OK) {
479 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
480 		goto attach_fail;
481 	}
482 
483 	/*
484 	 * Initialize interrupts
485 	 */
486 	if (e1000g_add_intrs(Adapter) != DDI_SUCCESS) {
487 		e1000g_log(Adapter, CE_WARN, "Add interrupts failed");
488 		goto attach_fail;
489 	}
490 	Adapter->attach_progress |= ATTACH_PROGRESS_ADD_INTR;
491 
492 	/*
493 	 * Initialize mutex's for this device.
494 	 * Do this before enabling the interrupt handler and
495 	 * register the softint to avoid the condition where
496 	 * interrupt handler can try using uninitialized mutex
497 	 */
498 	e1000g_init_locks(Adapter);
499 	Adapter->attach_progress |= ATTACH_PROGRESS_LOCKS;
500 
501 	/*
502 	 * Initialize Driver Counters
503 	 */
504 	if (e1000g_init_stats(Adapter) != DDI_SUCCESS) {
505 		e1000g_log(Adapter, CE_WARN, "Init stats failed");
506 		goto attach_fail;
507 	}
508 	Adapter->attach_progress |= ATTACH_PROGRESS_KSTATS;
509 
510 	/*
511 	 * Initialize chip hardware and software structures
512 	 */
513 	rw_enter(&Adapter->chip_lock, RW_WRITER);
514 	if (e1000g_init(Adapter) != DDI_SUCCESS) {
515 		rw_exit(&Adapter->chip_lock);
516 		e1000g_log(Adapter, CE_WARN, "Adapter initialization failed");
517 		goto attach_fail;
518 	}
519 	rw_exit(&Adapter->chip_lock);
520 	Adapter->attach_progress |= ATTACH_PROGRESS_INIT;
521 
522 	/*
523 	 * Register the driver to the MAC
524 	 */
525 	if (e1000g_register_mac(Adapter) != DDI_SUCCESS) {
526 		e1000g_log(Adapter, CE_WARN, "Register MAC failed");
527 		goto attach_fail;
528 	}
529 	Adapter->attach_progress |= ATTACH_PROGRESS_MAC;
530 
531 	/*
532 	 * Now that mutex locks are initialized, and the chip is also
533 	 * initialized, enable interrupts.
534 	 */
535 	if (e1000g_enable_intrs(Adapter) != DDI_SUCCESS) {
536 		e1000g_log(Adapter, CE_WARN, "Enable DDI interrupts failed");
537 		goto attach_fail;
538 	}
539 	Adapter->attach_progress |= ATTACH_PROGRESS_ENABLE_INTR;
540 
541 	/*
542 	 * If e1000g_force_detach is enabled, in global private dip list,
543 	 * we will create a new entry, which maintains the priv_dip for DR
544 	 * supports after driver detached.
545 	 */
546 	if (e1000g_force_detach) {
547 		private_devi_list_t *devi_node;
548 
549 		Adapter->priv_dip =
550 		    kmem_zalloc(sizeof (struct dev_info), KM_SLEEP);
551 		bcopy(DEVI(devinfo), DEVI(Adapter->priv_dip),
552 		    sizeof (struct dev_info));
553 
554 		devi_node =
555 		    kmem_zalloc(sizeof (private_devi_list_t), KM_SLEEP);
556 
557 		mutex_enter(&e1000g_rx_detach_lock);
558 		devi_node->priv_dip = Adapter->priv_dip;
559 		devi_node->flag = E1000G_PRIV_DEVI_ATTACH;
560 		devi_node->pending_rx_count = 0;
561 
562 		Adapter->priv_devi_node = devi_node;
563 
564 		if (e1000g_private_devi_list == NULL) {
565 			devi_node->prev = NULL;
566 			devi_node->next = NULL;
567 			e1000g_private_devi_list = devi_node;
568 		} else {
569 			devi_node->prev = NULL;
570 			devi_node->next = e1000g_private_devi_list;
571 			e1000g_private_devi_list->prev = devi_node;
572 			e1000g_private_devi_list = devi_node;
573 		}
574 		mutex_exit(&e1000g_rx_detach_lock);
575 	}
576 
577 	cmn_err(CE_CONT, "!%s, %s\n", e1000g_string, e1000g_version);
578 	Adapter->e1000g_state = E1000G_INITIALIZED;
579 
580 	return (DDI_SUCCESS);
581 
582 attach_fail:
583 	e1000g_unattach(devinfo, Adapter);
584 	return (DDI_FAILURE);
585 }
586 
587 static int
588 e1000g_register_mac(struct e1000g *Adapter)
589 {
590 	struct e1000_hw *hw = &Adapter->shared;
591 	mac_register_t *mac;
592 	int err;
593 
594 	if ((mac = mac_alloc(MAC_VERSION)) == NULL)
595 		return (DDI_FAILURE);
596 
597 	mac->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
598 	mac->m_driver = Adapter;
599 	mac->m_dip = Adapter->dip;
600 	mac->m_src_addr = hw->mac.addr;
601 	mac->m_callbacks = &e1000g_m_callbacks;
602 	mac->m_min_sdu = 0;
603 	mac->m_max_sdu = Adapter->default_mtu;
604 	mac->m_margin = VLAN_TAGSZ;
605 	mac->m_priv_props = e1000g_priv_props;
606 	mac->m_priv_prop_count = E1000G_MAX_PRIV_PROPS;
607 	mac->m_v12n = MAC_VIRT_LEVEL1;
608 
609 	err = mac_register(mac, &Adapter->mh);
610 	mac_free(mac);
611 
612 	return (err == 0 ? DDI_SUCCESS : DDI_FAILURE);
613 }
614 
615 static int
616 e1000g_identify_hardware(struct e1000g *Adapter)
617 {
618 	struct e1000_hw *hw = &Adapter->shared;
619 	struct e1000g_osdep *osdep = &Adapter->osdep;
620 
621 	/* Get the device id */
622 	hw->vendor_id =
623 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_VENID);
624 	hw->device_id =
625 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_DEVID);
626 	hw->revision_id =
627 	    pci_config_get8(osdep->cfg_handle, PCI_CONF_REVID);
628 	hw->subsystem_device_id =
629 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBSYSID);
630 	hw->subsystem_vendor_id =
631 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBVENID);
632 
633 	if (e1000_set_mac_type(hw) != E1000_SUCCESS) {
634 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
635 		    "MAC type could not be set properly.");
636 		return (DDI_FAILURE);
637 	}
638 
639 	return (DDI_SUCCESS);
640 }
641 
642 static int
643 e1000g_regs_map(struct e1000g *Adapter)
644 {
645 	dev_info_t *devinfo = Adapter->dip;
646 	struct e1000_hw *hw = &Adapter->shared;
647 	struct e1000g_osdep *osdep = &Adapter->osdep;
648 	off_t mem_size;
649 
650 	/* Get size of adapter register memory */
651 	if (ddi_dev_regsize(devinfo, ADAPTER_REG_SET, &mem_size) !=
652 	    DDI_SUCCESS) {
653 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
654 		    "ddi_dev_regsize for registers failed");
655 		return (DDI_FAILURE);
656 	}
657 
658 	/* Map adapter register memory */
659 	if ((ddi_regs_map_setup(devinfo, ADAPTER_REG_SET,
660 	    (caddr_t *)&hw->hw_addr, 0, mem_size, &e1000g_regs_acc_attr,
661 	    &osdep->reg_handle)) != DDI_SUCCESS) {
662 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
663 		    "ddi_regs_map_setup for registers failed");
664 		goto regs_map_fail;
665 	}
666 
667 	/* ICH needs to map flash memory */
668 	if (hw->mac.type == e1000_ich8lan ||
669 	    hw->mac.type == e1000_ich9lan ||
670 	    hw->mac.type == e1000_ich10lan) {
671 		/* get flash size */
672 		if (ddi_dev_regsize(devinfo, ICH_FLASH_REG_SET,
673 		    &mem_size) != DDI_SUCCESS) {
674 			E1000G_DEBUGLOG_0(Adapter, CE_WARN,
675 			    "ddi_dev_regsize for ICH flash failed");
676 			goto regs_map_fail;
677 		}
678 
679 		/* map flash in */
680 		if (ddi_regs_map_setup(devinfo, ICH_FLASH_REG_SET,
681 		    (caddr_t *)&hw->flash_address, 0,
682 		    mem_size, &e1000g_regs_acc_attr,
683 		    &osdep->ich_flash_handle) != DDI_SUCCESS) {
684 			E1000G_DEBUGLOG_0(Adapter, CE_WARN,
685 			    "ddi_regs_map_setup for ICH flash failed");
686 			goto regs_map_fail;
687 		}
688 	}
689 
690 	return (DDI_SUCCESS);
691 
692 regs_map_fail:
693 	if (osdep->reg_handle != NULL)
694 		ddi_regs_map_free(&osdep->reg_handle);
695 
696 	return (DDI_FAILURE);
697 }
698 
699 static int
700 e1000g_set_driver_params(struct e1000g *Adapter)
701 {
702 	struct e1000_hw *hw;
703 	uint32_t mem_bar, io_bar, bar64;
704 
705 	hw = &Adapter->shared;
706 
707 	/* Set MAC type and initialize hardware functions */
708 	if (e1000_setup_init_funcs(hw, B_TRUE) != E1000_SUCCESS) {
709 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
710 		    "Could not setup hardware functions");
711 		return (DDI_FAILURE);
712 	}
713 
714 	/* Get bus information */
715 	if (e1000_get_bus_info(hw) != E1000_SUCCESS) {
716 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
717 		    "Could not get bus information");
718 		return (DDI_FAILURE);
719 	}
720 
721 	/* get mem_base addr */
722 	mem_bar = pci_config_get32(Adapter->osdep.cfg_handle, PCI_CONF_BASE0);
723 	bar64 = mem_bar & PCI_BASE_TYPE_ALL;
724 
725 	/* get io_base addr */
726 	if (hw->mac.type >= e1000_82544) {
727 		if (bar64) {
728 			/* IO BAR is different for 64 bit BAR mode */
729 			io_bar = pci_config_get32(Adapter->osdep.cfg_handle,
730 			    PCI_CONF_BASE4);
731 		} else {
732 			/* normal 32-bit BAR mode */
733 			io_bar = pci_config_get32(Adapter->osdep.cfg_handle,
734 			    PCI_CONF_BASE2);
735 		}
736 		hw->io_base = io_bar & PCI_BASE_IO_ADDR_M;
737 	} else {
738 		/* no I/O access for adapters prior to 82544 */
739 		hw->io_base = 0x0;
740 	}
741 
742 	e1000_read_pci_cfg(hw, PCI_COMMAND_REGISTER, &hw->bus.pci_cmd_word);
743 
744 	hw->mac.autoneg_failed = B_TRUE;
745 
746 	/* Set the autoneg_wait_to_complete flag to B_FALSE */
747 	hw->phy.autoneg_wait_to_complete = B_FALSE;
748 
749 	/* Adaptive IFS related changes */
750 	hw->mac.adaptive_ifs = B_TRUE;
751 
752 	/* Enable phy init script for IGP phy of 82541/82547 */
753 	if ((hw->mac.type == e1000_82547) ||
754 	    (hw->mac.type == e1000_82541) ||
755 	    (hw->mac.type == e1000_82547_rev_2) ||
756 	    (hw->mac.type == e1000_82541_rev_2))
757 		e1000_init_script_state_82541(hw, B_TRUE);
758 
759 	/* Enable the TTL workaround for 82541/82547 */
760 	e1000_set_ttl_workaround_state_82541(hw, B_TRUE);
761 
762 #ifdef __sparc
763 	Adapter->strip_crc = B_TRUE;
764 #else
765 	Adapter->strip_crc = B_FALSE;
766 #endif
767 
768 	/* Get conf file properties */
769 	e1000g_get_conf(Adapter);
770 
771 	/* Get speed/duplex settings in conf file */
772 	hw->mac.forced_speed_duplex = ADVERTISE_100_FULL;
773 	hw->phy.autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
774 	e1000g_force_speed_duplex(Adapter);
775 
776 	/* Get Jumbo Frames settings in conf file */
777 	e1000g_get_max_frame_size(Adapter);
778 
779 	/* Set Rx/Tx buffer size */
780 	e1000g_set_bufsize(Adapter);
781 
782 	/* Master Latency Timer */
783 	Adapter->master_latency_timer = DEFAULT_MASTER_LATENCY_TIMER;
784 
785 	/* copper options */
786 	if (hw->phy.media_type == e1000_media_type_copper) {
787 		hw->phy.mdix = 0;	/* AUTO_ALL_MODES */
788 		hw->phy.disable_polarity_correction = B_FALSE;
789 		hw->phy.ms_type = e1000_ms_hw_default;	/* E1000_MASTER_SLAVE */
790 	}
791 
792 	/* The initial link state should be "unknown" */
793 	Adapter->link_state = LINK_STATE_UNKNOWN;
794 
795 	/* Initialize rx parameters */
796 	Adapter->rx_intr_delay = DEFAULT_RX_INTR_DELAY;
797 	Adapter->rx_intr_abs_delay = DEFAULT_RX_INTR_ABS_DELAY;
798 
799 	/* Initialize tx parameters */
800 	Adapter->tx_intr_enable = DEFAULT_TX_INTR_ENABLE;
801 	Adapter->tx_bcopy_thresh = DEFAULT_TX_BCOPY_THRESHOLD;
802 	Adapter->tx_intr_delay = DEFAULT_TX_INTR_DELAY;
803 	Adapter->tx_intr_abs_delay = DEFAULT_TX_INTR_ABS_DELAY;
804 
805 	/* Initialize rx parameters */
806 	Adapter->rx_bcopy_thresh = DEFAULT_RX_BCOPY_THRESHOLD;
807 
808 	return (DDI_SUCCESS);
809 }
810 
811 static void
812 e1000g_set_bufsize(struct e1000g *Adapter)
813 {
814 	struct e1000_mac_info *mac = &Adapter->shared.mac;
815 	uint64_t rx_size;
816 	uint64_t tx_size;
817 
818 	dev_info_t *devinfo = Adapter->dip;
819 #ifdef __sparc
820 	ulong_t iommu_pagesize;
821 #endif
822 	/* Get the system page size */
823 	Adapter->sys_page_sz = ddi_ptob(devinfo, (ulong_t)1);
824 
825 #ifdef __sparc
826 	iommu_pagesize = dvma_pagesize(devinfo);
827 	if (iommu_pagesize != 0) {
828 		if (Adapter->sys_page_sz == iommu_pagesize) {
829 			if (iommu_pagesize > 0x4000)
830 				Adapter->sys_page_sz = 0x4000;
831 		} else {
832 			if (Adapter->sys_page_sz > iommu_pagesize)
833 				Adapter->sys_page_sz = iommu_pagesize;
834 		}
835 	}
836 	if (Adapter->lso_enable) {
837 		Adapter->dvma_page_num = E1000_LSO_MAXLEN /
838 		    Adapter->sys_page_sz + E1000G_DEFAULT_DVMA_PAGE_NUM;
839 	} else {
840 		Adapter->dvma_page_num = Adapter->max_frame_size /
841 		    Adapter->sys_page_sz + E1000G_DEFAULT_DVMA_PAGE_NUM;
842 	}
843 	ASSERT(Adapter->dvma_page_num >= E1000G_DEFAULT_DVMA_PAGE_NUM);
844 #endif
845 
846 	Adapter->min_frame_size = ETHERMIN + ETHERFCSL;
847 
848 	if (Adapter->mem_workaround_82546 &&
849 	    ((mac->type == e1000_82545) ||
850 	    (mac->type == e1000_82546) ||
851 	    (mac->type == e1000_82546_rev_3))) {
852 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_2K;
853 	} else {
854 		rx_size = Adapter->max_frame_size + E1000G_IPALIGNPRESERVEROOM;
855 		if ((rx_size > FRAME_SIZE_UPTO_2K) &&
856 		    (rx_size <= FRAME_SIZE_UPTO_4K))
857 			Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_4K;
858 		else if ((rx_size > FRAME_SIZE_UPTO_4K) &&
859 		    (rx_size <= FRAME_SIZE_UPTO_8K))
860 			Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_8K;
861 		else if ((rx_size > FRAME_SIZE_UPTO_8K) &&
862 		    (rx_size <= FRAME_SIZE_UPTO_16K))
863 			Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_16K;
864 		else
865 			Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_2K;
866 	}
867 
868 	tx_size = Adapter->max_frame_size;
869 	if ((tx_size > FRAME_SIZE_UPTO_2K) && (tx_size <= FRAME_SIZE_UPTO_4K))
870 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_4K;
871 	else if ((tx_size > FRAME_SIZE_UPTO_4K) &&
872 	    (tx_size <= FRAME_SIZE_UPTO_8K))
873 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_8K;
874 	else if ((tx_size > FRAME_SIZE_UPTO_8K) &&
875 	    (tx_size <= FRAME_SIZE_UPTO_16K))
876 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_16K;
877 	else
878 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_2K;
879 
880 	/*
881 	 * For Wiseman adapters we have an requirement of having receive
882 	 * buffers aligned at 256 byte boundary. Since Livengood does not
883 	 * require this and forcing it for all hardwares will have
884 	 * performance implications, I am making it applicable only for
885 	 * Wiseman and for Jumbo frames enabled mode as rest of the time,
886 	 * it is okay to have normal frames...but it does involve a
887 	 * potential risk where we may loose data if buffer is not
888 	 * aligned...so all wiseman boards to have 256 byte aligned
889 	 * buffers
890 	 */
891 	if (mac->type < e1000_82543)
892 		Adapter->rx_buf_align = RECEIVE_BUFFER_ALIGN_SIZE;
893 	else
894 		Adapter->rx_buf_align = 1;
895 }
896 
897 /*
898  * e1000g_detach - driver detach
899  *
900  * The detach() function is the complement of the attach routine.
901  * If cmd is set to DDI_DETACH, detach() is used to remove  the
902  * state  associated  with  a  given  instance of a device node
903  * prior to the removal of that instance from the system.
904  *
905  * The detach() function will be called once for each  instance
906  * of the device for which there has been a successful attach()
907  * once there are no longer  any  opens  on  the  device.
908  *
909  * Interrupts routine are disabled, All memory allocated by this
910  * driver are freed.
911  */
912 static int
913 e1000g_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
914 {
915 	struct e1000g *Adapter;
916 	boolean_t rx_drain;
917 
918 	switch (cmd) {
919 	default:
920 		return (DDI_FAILURE);
921 
922 	case DDI_SUSPEND:
923 		return (e1000g_suspend(devinfo));
924 
925 	case DDI_DETACH:
926 		break;
927 	}
928 
929 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
930 	if (Adapter == NULL)
931 		return (DDI_FAILURE);
932 
933 	rx_drain = e1000g_rx_drain(Adapter);
934 	if (!rx_drain && !e1000g_force_detach)
935 		return (DDI_FAILURE);
936 
937 	if (mac_unregister(Adapter->mh) != 0) {
938 		e1000g_log(Adapter, CE_WARN, "Unregister MAC failed");
939 		return (DDI_FAILURE);
940 	}
941 	Adapter->attach_progress &= ~ATTACH_PROGRESS_MAC;
942 
943 	ASSERT(!(Adapter->e1000g_state & E1000G_STARTED));
944 
945 	if (!e1000g_force_detach && !rx_drain)
946 		return (DDI_FAILURE);
947 
948 	e1000g_unattach(devinfo, Adapter);
949 
950 	return (DDI_SUCCESS);
951 }
952 
953 /*
954  * e1000g_free_priv_devi_node - free a priv_dip entry for driver instance
955  */
956 void
957 e1000g_free_priv_devi_node(private_devi_list_t *devi_node)
958 {
959 	ASSERT(e1000g_private_devi_list != NULL);
960 	ASSERT(devi_node != NULL);
961 
962 	if (devi_node->prev != NULL)
963 		devi_node->prev->next = devi_node->next;
964 	if (devi_node->next != NULL)
965 		devi_node->next->prev = devi_node->prev;
966 	if (devi_node == e1000g_private_devi_list)
967 		e1000g_private_devi_list = devi_node->next;
968 
969 	kmem_free(devi_node->priv_dip,
970 	    sizeof (struct dev_info));
971 	kmem_free(devi_node,
972 	    sizeof (private_devi_list_t));
973 }
974 
975 static void
976 e1000g_unattach(dev_info_t *devinfo, struct e1000g *Adapter)
977 {
978 	private_devi_list_t *devi_node;
979 	int result;
980 
981 	if (Adapter->attach_progress & ATTACH_PROGRESS_ENABLE_INTR) {
982 		(void) e1000g_disable_intrs(Adapter);
983 	}
984 
985 	if (Adapter->attach_progress & ATTACH_PROGRESS_MAC) {
986 		(void) mac_unregister(Adapter->mh);
987 	}
988 
989 	if (Adapter->attach_progress & ATTACH_PROGRESS_ADD_INTR) {
990 		(void) e1000g_rem_intrs(Adapter);
991 	}
992 
993 	if (Adapter->attach_progress & ATTACH_PROGRESS_SETUP) {
994 		(void) ddi_prop_remove_all(devinfo);
995 	}
996 
997 	if (Adapter->attach_progress & ATTACH_PROGRESS_KSTATS) {
998 		kstat_delete((kstat_t *)Adapter->e1000g_ksp);
999 	}
1000 
1001 	if (Adapter->attach_progress & ATTACH_PROGRESS_INIT) {
1002 		stop_link_timer(Adapter);
1003 
1004 		mutex_enter(&e1000g_nvm_lock);
1005 		result = e1000_reset_hw(&Adapter->shared);
1006 		mutex_exit(&e1000g_nvm_lock);
1007 
1008 		if (result != E1000_SUCCESS) {
1009 			e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
1010 			ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
1011 		}
1012 	}
1013 
1014 	if (Adapter->attach_progress & ATTACH_PROGRESS_REGS_MAP) {
1015 		if (Adapter->osdep.reg_handle != NULL)
1016 			ddi_regs_map_free(&Adapter->osdep.reg_handle);
1017 		if (Adapter->osdep.ich_flash_handle != NULL)
1018 			ddi_regs_map_free(&Adapter->osdep.ich_flash_handle);
1019 	}
1020 
1021 	if (Adapter->attach_progress & ATTACH_PROGRESS_PCI_CONFIG) {
1022 		if (Adapter->osdep.cfg_handle != NULL)
1023 			pci_config_teardown(&Adapter->osdep.cfg_handle);
1024 	}
1025 
1026 	if (Adapter->attach_progress & ATTACH_PROGRESS_LOCKS) {
1027 		e1000g_destroy_locks(Adapter);
1028 	}
1029 
1030 	if (Adapter->attach_progress & ATTACH_PROGRESS_FMINIT) {
1031 		e1000g_fm_fini(Adapter);
1032 	}
1033 
1034 	mutex_enter(&e1000g_rx_detach_lock);
1035 	if (e1000g_force_detach && (Adapter->priv_devi_node != NULL)) {
1036 		devi_node = Adapter->priv_devi_node;
1037 		devi_node->flag |= E1000G_PRIV_DEVI_DETACH;
1038 
1039 		if (devi_node->pending_rx_count == 0) {
1040 			e1000g_free_priv_devi_node(devi_node);
1041 		}
1042 	}
1043 	mutex_exit(&e1000g_rx_detach_lock);
1044 
1045 	kmem_free((caddr_t)Adapter, sizeof (struct e1000g));
1046 
1047 	/*
1048 	 * Another hotplug spec requirement,
1049 	 * run ddi_set_driver_private(devinfo, null);
1050 	 */
1051 	ddi_set_driver_private(devinfo, NULL);
1052 }
1053 
1054 static void
1055 e1000g_init_locks(struct e1000g *Adapter)
1056 {
1057 	e1000g_tx_ring_t *tx_ring;
1058 	e1000g_rx_ring_t *rx_ring;
1059 
1060 	rw_init(&Adapter->chip_lock, NULL,
1061 	    RW_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
1062 	mutex_init(&Adapter->link_lock, NULL,
1063 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
1064 	mutex_init(&Adapter->watchdog_lock, NULL,
1065 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
1066 
1067 	tx_ring = Adapter->tx_ring;
1068 
1069 	mutex_init(&tx_ring->tx_lock, NULL,
1070 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
1071 	mutex_init(&tx_ring->usedlist_lock, NULL,
1072 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
1073 	mutex_init(&tx_ring->freelist_lock, NULL,
1074 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
1075 
1076 	rx_ring = Adapter->rx_ring;
1077 
1078 	mutex_init(&rx_ring->rx_lock, NULL,
1079 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
1080 }
1081 
1082 static void
1083 e1000g_destroy_locks(struct e1000g *Adapter)
1084 {
1085 	e1000g_tx_ring_t *tx_ring;
1086 	e1000g_rx_ring_t *rx_ring;
1087 
1088 	tx_ring = Adapter->tx_ring;
1089 	mutex_destroy(&tx_ring->tx_lock);
1090 	mutex_destroy(&tx_ring->usedlist_lock);
1091 	mutex_destroy(&tx_ring->freelist_lock);
1092 
1093 	rx_ring = Adapter->rx_ring;
1094 	mutex_destroy(&rx_ring->rx_lock);
1095 
1096 	mutex_destroy(&Adapter->link_lock);
1097 	mutex_destroy(&Adapter->watchdog_lock);
1098 	rw_destroy(&Adapter->chip_lock);
1099 }
1100 
1101 static int
1102 e1000g_resume(dev_info_t *devinfo)
1103 {
1104 	struct e1000g *Adapter;
1105 
1106 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
1107 	if (Adapter == NULL)
1108 		e1000g_log(Adapter, CE_PANIC,
1109 		    "Instance pointer is null\n");
1110 
1111 	if (Adapter->dip != devinfo)
1112 		e1000g_log(Adapter, CE_PANIC,
1113 		    "Devinfo is not the same as saved devinfo\n");
1114 
1115 	rw_enter(&Adapter->chip_lock, RW_WRITER);
1116 
1117 	if (Adapter->e1000g_state & E1000G_STARTED) {
1118 		if (e1000g_start(Adapter, B_FALSE) != DDI_SUCCESS) {
1119 			rw_exit(&Adapter->chip_lock);
1120 			/*
1121 			 * We note the failure, but return success, as the
1122 			 * system is still usable without this controller.
1123 			 */
1124 			e1000g_log(Adapter, CE_WARN,
1125 			    "e1000g_resume: failed to restart controller\n");
1126 			return (DDI_SUCCESS);
1127 		}
1128 		/* Enable and start the watchdog timer */
1129 		enable_watchdog_timer(Adapter);
1130 	}
1131 
1132 	Adapter->e1000g_state &= ~E1000G_SUSPENDED;
1133 
1134 	rw_exit(&Adapter->chip_lock);
1135 
1136 	return (DDI_SUCCESS);
1137 }
1138 
1139 static int
1140 e1000g_suspend(dev_info_t *devinfo)
1141 {
1142 	struct e1000g *Adapter;
1143 
1144 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
1145 	if (Adapter == NULL)
1146 		return (DDI_FAILURE);
1147 
1148 	rw_enter(&Adapter->chip_lock, RW_WRITER);
1149 
1150 	Adapter->e1000g_state |= E1000G_SUSPENDED;
1151 
1152 	/* if the port isn't plumbed, we can simply return */
1153 	if (!(Adapter->e1000g_state & E1000G_STARTED)) {
1154 		rw_exit(&Adapter->chip_lock);
1155 		return (DDI_SUCCESS);
1156 	}
1157 
1158 	e1000g_stop(Adapter, B_FALSE);
1159 
1160 	rw_exit(&Adapter->chip_lock);
1161 
1162 	/* Disable and stop all the timers */
1163 	disable_watchdog_timer(Adapter);
1164 	stop_link_timer(Adapter);
1165 	stop_82547_timer(Adapter->tx_ring);
1166 
1167 	return (DDI_SUCCESS);
1168 }
1169 
1170 static int
1171 e1000g_init(struct e1000g *Adapter)
1172 {
1173 	uint32_t pba;
1174 	uint32_t high_water;
1175 	struct e1000_hw *hw;
1176 	clock_t link_timeout;
1177 	int result;
1178 
1179 	hw = &Adapter->shared;
1180 
1181 	/*
1182 	 * reset to put the hardware in a known state
1183 	 * before we try to do anything with the eeprom
1184 	 */
1185 	mutex_enter(&e1000g_nvm_lock);
1186 	result = e1000_reset_hw(hw);
1187 	mutex_exit(&e1000g_nvm_lock);
1188 
1189 	if (result != E1000_SUCCESS) {
1190 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
1191 		goto init_fail;
1192 	}
1193 
1194 	mutex_enter(&e1000g_nvm_lock);
1195 	result = e1000_validate_nvm_checksum(hw);
1196 	if (result < E1000_SUCCESS) {
1197 		/*
1198 		 * Some PCI-E parts fail the first check due to
1199 		 * the link being in sleep state.  Call it again,
1200 		 * if it fails a second time its a real issue.
1201 		 */
1202 		result = e1000_validate_nvm_checksum(hw);
1203 	}
1204 	mutex_exit(&e1000g_nvm_lock);
1205 
1206 	if (result < E1000_SUCCESS) {
1207 		e1000g_log(Adapter, CE_WARN,
1208 		    "Invalid NVM checksum. Please contact "
1209 		    "the vendor to update the NVM.");
1210 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
1211 		goto init_fail;
1212 	}
1213 
1214 	result = 0;
1215 #ifdef __sparc
1216 	/*
1217 	 * First, we try to get the local ethernet address from OBP. If
1218 	 * failed, then we get it from the EEPROM of NIC card.
1219 	 */
1220 	result = e1000g_find_mac_address(Adapter);
1221 #endif
1222 	/* Get the local ethernet address. */
1223 	if (!result) {
1224 		mutex_enter(&e1000g_nvm_lock);
1225 		result = e1000_read_mac_addr(hw);
1226 		mutex_exit(&e1000g_nvm_lock);
1227 	}
1228 
1229 	if (result < E1000_SUCCESS) {
1230 		e1000g_log(Adapter, CE_WARN, "Read mac addr failed");
1231 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
1232 		goto init_fail;
1233 	}
1234 
1235 	/* check for valid mac address */
1236 	if (!is_valid_mac_addr(hw->mac.addr)) {
1237 		e1000g_log(Adapter, CE_WARN, "Invalid mac addr");
1238 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
1239 		goto init_fail;
1240 	}
1241 
1242 	/* Set LAA state for 82571 chipset */
1243 	e1000_set_laa_state_82571(hw, B_TRUE);
1244 
1245 	/* Master Latency Timer implementation */
1246 	if (Adapter->master_latency_timer) {
1247 		pci_config_put8(Adapter->osdep.cfg_handle,
1248 		    PCI_CONF_LATENCY_TIMER, Adapter->master_latency_timer);
1249 	}
1250 
1251 	if (hw->mac.type < e1000_82547) {
1252 		/*
1253 		 * Total FIFO is 64K
1254 		 */
1255 		if (Adapter->max_frame_size > FRAME_SIZE_UPTO_8K)
1256 			pba = E1000_PBA_40K;	/* 40K for Rx, 24K for Tx */
1257 		else
1258 			pba = E1000_PBA_48K;	/* 48K for Rx, 16K for Tx */
1259 	} else if ((hw->mac.type == e1000_82571) ||
1260 	    (hw->mac.type == e1000_82572) ||
1261 	    (hw->mac.type == e1000_80003es2lan)) {
1262 		/*
1263 		 * Total FIFO is 48K
1264 		 */
1265 		if (Adapter->max_frame_size > FRAME_SIZE_UPTO_8K)
1266 			pba = E1000_PBA_30K;	/* 30K for Rx, 18K for Tx */
1267 		else
1268 			pba = E1000_PBA_38K;	/* 38K for Rx, 10K for Tx */
1269 	} else if (hw->mac.type == e1000_82573) {
1270 		pba = E1000_PBA_20K;		/* 20K for Rx, 12K for Tx */
1271 	} else if (hw->mac.type == e1000_82574) {
1272 		/* Keep adapter default: 20K for Rx, 20K for Tx */
1273 		pba = E1000_READ_REG(hw, E1000_PBA);
1274 	} else if (hw->mac.type == e1000_ich8lan) {
1275 		pba = E1000_PBA_8K;		/* 8K for Rx, 12K for Tx */
1276 	} else if (hw->mac.type == e1000_ich9lan) {
1277 		pba = E1000_PBA_10K;
1278 	} else if (hw->mac.type == e1000_ich10lan) {
1279 		pba = E1000_PBA_10K;
1280 	} else {
1281 		/*
1282 		 * Total FIFO is 40K
1283 		 */
1284 		if (Adapter->max_frame_size > FRAME_SIZE_UPTO_8K)
1285 			pba = E1000_PBA_22K;	/* 22K for Rx, 18K for Tx */
1286 		else
1287 			pba = E1000_PBA_30K;	/* 30K for Rx, 10K for Tx */
1288 	}
1289 	E1000_WRITE_REG(hw, E1000_PBA, pba);
1290 
1291 	/*
1292 	 * These parameters set thresholds for the adapter's generation(Tx)
1293 	 * and response(Rx) to Ethernet PAUSE frames.  These are just threshold
1294 	 * settings.  Flow control is enabled or disabled in the configuration
1295 	 * file.
1296 	 * High-water mark is set down from the top of the rx fifo (not
1297 	 * sensitive to max_frame_size) and low-water is set just below
1298 	 * high-water mark.
1299 	 * The high water mark must be low enough to fit one full frame above
1300 	 * it in the rx FIFO.  Should be the lower of:
1301 	 * 90% of the Rx FIFO size and the full Rx FIFO size minus the early
1302 	 * receive size (assuming ERT set to E1000_ERT_2048), or the full
1303 	 * Rx FIFO size minus one full frame.
1304 	 */
1305 	high_water = min(((pba << 10) * 9 / 10),
1306 	    ((hw->mac.type == e1000_82573 || hw->mac.type == e1000_82574 ||
1307 	    hw->mac.type == e1000_ich9lan || hw->mac.type == e1000_ich10lan) ?
1308 	    ((pba << 10) - (E1000_ERT_2048 << 3)) :
1309 	    ((pba << 10) - Adapter->max_frame_size)));
1310 
1311 	hw->fc.high_water = high_water & 0xFFF8;
1312 	hw->fc.low_water = hw->fc.high_water - 8;
1313 
1314 	if (hw->mac.type == e1000_80003es2lan)
1315 		hw->fc.pause_time = 0xFFFF;
1316 	else
1317 		hw->fc.pause_time = E1000_FC_PAUSE_TIME;
1318 	hw->fc.send_xon = B_TRUE;
1319 
1320 	/*
1321 	 * Reset the adapter hardware the second time.
1322 	 */
1323 	mutex_enter(&e1000g_nvm_lock);
1324 	result = e1000_reset_hw(hw);
1325 	mutex_exit(&e1000g_nvm_lock);
1326 
1327 	if (result != E1000_SUCCESS) {
1328 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
1329 		goto init_fail;
1330 	}
1331 
1332 	/* disable wakeup control by default */
1333 	if (hw->mac.type >= e1000_82544)
1334 		E1000_WRITE_REG(hw, E1000_WUC, 0);
1335 
1336 	/*
1337 	 * MWI should be disabled on 82546.
1338 	 */
1339 	if (hw->mac.type == e1000_82546)
1340 		e1000_pci_clear_mwi(hw);
1341 	else
1342 		e1000_pci_set_mwi(hw);
1343 
1344 	/*
1345 	 * Configure/Initialize hardware
1346 	 */
1347 	mutex_enter(&e1000g_nvm_lock);
1348 	result = e1000_init_hw(hw);
1349 	mutex_exit(&e1000g_nvm_lock);
1350 
1351 	if (result < E1000_SUCCESS) {
1352 		e1000g_log(Adapter, CE_WARN, "Initialize hw failed");
1353 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
1354 		goto init_fail;
1355 	}
1356 
1357 	/*
1358 	 * Restore LED settings to the default from EEPROM
1359 	 * to meet the standard for Sun platforms.
1360 	 */
1361 	if ((hw->mac.type != e1000_82541) &&
1362 	    (hw->mac.type != e1000_82541_rev_2) &&
1363 	    (hw->mac.type != e1000_82547) &&
1364 	    (hw->mac.type != e1000_82547_rev_2))
1365 		(void) e1000_cleanup_led(hw);
1366 
1367 	/* Disable Smart Power Down */
1368 	phy_spd_state(hw, B_FALSE);
1369 
1370 	/* Make sure driver has control */
1371 	e1000g_get_driver_control(hw);
1372 
1373 	/*
1374 	 * Initialize unicast addresses.
1375 	 */
1376 	e1000g_init_unicst(Adapter);
1377 
1378 	/*
1379 	 * Setup and initialize the mctable structures.  After this routine
1380 	 * completes  Multicast table will be set
1381 	 */
1382 	e1000g_setup_multicast(Adapter);
1383 	msec_delay(5);
1384 
1385 	/*
1386 	 * Implement Adaptive IFS
1387 	 */
1388 	e1000_reset_adaptive(hw);
1389 
1390 	/* Setup Interrupt Throttling Register */
1391 	if (hw->mac.type >= e1000_82540) {
1392 		E1000_WRITE_REG(hw, E1000_ITR, Adapter->intr_throttling_rate);
1393 	} else
1394 		Adapter->intr_adaptive = B_FALSE;
1395 
1396 	/* Start the timer for link setup */
1397 	if (hw->mac.autoneg)
1398 		link_timeout = PHY_AUTO_NEG_LIMIT * drv_usectohz(100000);
1399 	else
1400 		link_timeout = PHY_FORCE_LIMIT * drv_usectohz(100000);
1401 
1402 	mutex_enter(&Adapter->link_lock);
1403 	if (hw->phy.autoneg_wait_to_complete) {
1404 		Adapter->link_complete = B_TRUE;
1405 	} else {
1406 		Adapter->link_complete = B_FALSE;
1407 		Adapter->link_tid = timeout(e1000g_link_timer,
1408 		    (void *)Adapter, link_timeout);
1409 	}
1410 	mutex_exit(&Adapter->link_lock);
1411 
1412 	/* Enable PCI-Ex master */
1413 	if (hw->bus.type == e1000_bus_type_pci_express) {
1414 		e1000_enable_pciex_master(hw);
1415 	}
1416 
1417 	/* Save the state of the phy */
1418 	e1000g_get_phy_state(Adapter);
1419 
1420 	e1000g_param_sync(Adapter);
1421 
1422 	Adapter->init_count++;
1423 
1424 	if (e1000g_check_acc_handle(Adapter->osdep.cfg_handle) != DDI_FM_OK) {
1425 		goto init_fail;
1426 	}
1427 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
1428 		goto init_fail;
1429 	}
1430 
1431 	Adapter->poll_mode = e1000g_poll_mode;
1432 
1433 	return (DDI_SUCCESS);
1434 
1435 init_fail:
1436 	ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
1437 	return (DDI_FAILURE);
1438 }
1439 
1440 static int
1441 e1000g_alloc_rx_data(struct e1000g *Adapter)
1442 {
1443 	e1000g_rx_ring_t *rx_ring;
1444 	e1000g_rx_data_t *rx_data;
1445 
1446 	rx_ring = Adapter->rx_ring;
1447 
1448 	rx_data = kmem_zalloc(sizeof (e1000g_rx_data_t), KM_NOSLEEP);
1449 
1450 	if (rx_data == NULL)
1451 		return (DDI_FAILURE);
1452 
1453 	rx_data->priv_devi_node = Adapter->priv_devi_node;
1454 	rx_data->rx_ring = rx_ring;
1455 
1456 	mutex_init(&rx_data->freelist_lock, NULL,
1457 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
1458 	mutex_init(&rx_data->recycle_lock, NULL,
1459 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
1460 
1461 	rx_ring->rx_data = rx_data;
1462 
1463 	return (DDI_SUCCESS);
1464 }
1465 
1466 void
1467 e1000g_free_rx_pending_buffers(e1000g_rx_data_t *rx_data)
1468 {
1469 	rx_sw_packet_t *packet, *next_packet;
1470 
1471 	if (rx_data == NULL)
1472 		return;
1473 
1474 	packet = rx_data->packet_area;
1475 	while (packet != NULL) {
1476 		next_packet = packet->next;
1477 		e1000g_free_rx_sw_packet(packet, B_TRUE);
1478 		packet = next_packet;
1479 	}
1480 	rx_data->packet_area = NULL;
1481 }
1482 
1483 void
1484 e1000g_free_rx_data(e1000g_rx_data_t *rx_data)
1485 {
1486 	if (rx_data == NULL)
1487 		return;
1488 
1489 	mutex_destroy(&rx_data->freelist_lock);
1490 	mutex_destroy(&rx_data->recycle_lock);
1491 
1492 	kmem_free(rx_data, sizeof (e1000g_rx_data_t));
1493 }
1494 
1495 /*
1496  * Check if the link is up
1497  */
1498 static boolean_t
1499 e1000g_link_up(struct e1000g *Adapter)
1500 {
1501 	struct e1000_hw *hw;
1502 	boolean_t link_up;
1503 
1504 	hw = &Adapter->shared;
1505 
1506 	(void) e1000_check_for_link(hw);
1507 
1508 	if ((E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU) ||
1509 	    ((!hw->mac.get_link_status) && (hw->mac.type == e1000_82543)) ||
1510 	    ((hw->phy.media_type == e1000_media_type_internal_serdes) &&
1511 	    (hw->mac.serdes_has_link))) {
1512 		link_up = B_TRUE;
1513 	} else {
1514 		link_up = B_FALSE;
1515 	}
1516 
1517 	return (link_up);
1518 }
1519 
1520 static void
1521 e1000g_m_ioctl(void *arg, queue_t *q, mblk_t *mp)
1522 {
1523 	struct iocblk *iocp;
1524 	struct e1000g *e1000gp;
1525 	enum ioc_reply status;
1526 
1527 	iocp = (struct iocblk *)(uintptr_t)mp->b_rptr;
1528 	iocp->ioc_error = 0;
1529 	e1000gp = (struct e1000g *)arg;
1530 
1531 	ASSERT(e1000gp);
1532 	if (e1000gp == NULL) {
1533 		miocnak(q, mp, 0, EINVAL);
1534 		return;
1535 	}
1536 
1537 	rw_enter(&e1000gp->chip_lock, RW_READER);
1538 	if (e1000gp->e1000g_state & E1000G_SUSPENDED) {
1539 		rw_exit(&e1000gp->chip_lock);
1540 		miocnak(q, mp, 0, EINVAL);
1541 		return;
1542 	}
1543 	rw_exit(&e1000gp->chip_lock);
1544 
1545 	switch (iocp->ioc_cmd) {
1546 
1547 	case LB_GET_INFO_SIZE:
1548 	case LB_GET_INFO:
1549 	case LB_GET_MODE:
1550 	case LB_SET_MODE:
1551 		status = e1000g_loopback_ioctl(e1000gp, iocp, mp);
1552 		break;
1553 
1554 
1555 #ifdef E1000G_DEBUG
1556 	case E1000G_IOC_REG_PEEK:
1557 	case E1000G_IOC_REG_POKE:
1558 		status = e1000g_pp_ioctl(e1000gp, iocp, mp);
1559 		break;
1560 	case E1000G_IOC_CHIP_RESET:
1561 		e1000gp->reset_count++;
1562 		if (e1000g_reset_adapter(e1000gp))
1563 			status = IOC_ACK;
1564 		else
1565 			status = IOC_INVAL;
1566 		break;
1567 #endif
1568 	default:
1569 		status = IOC_INVAL;
1570 		break;
1571 	}
1572 
1573 	/*
1574 	 * Decide how to reply
1575 	 */
1576 	switch (status) {
1577 	default:
1578 	case IOC_INVAL:
1579 		/*
1580 		 * Error, reply with a NAK and EINVAL or the specified error
1581 		 */
1582 		miocnak(q, mp, 0, iocp->ioc_error == 0 ?
1583 		    EINVAL : iocp->ioc_error);
1584 		break;
1585 
1586 	case IOC_DONE:
1587 		/*
1588 		 * OK, reply already sent
1589 		 */
1590 		break;
1591 
1592 	case IOC_ACK:
1593 		/*
1594 		 * OK, reply with an ACK
1595 		 */
1596 		miocack(q, mp, 0, 0);
1597 		break;
1598 
1599 	case IOC_REPLY:
1600 		/*
1601 		 * OK, send prepared reply as ACK or NAK
1602 		 */
1603 		mp->b_datap->db_type = iocp->ioc_error == 0 ?
1604 		    M_IOCACK : M_IOCNAK;
1605 		qreply(q, mp);
1606 		break;
1607 	}
1608 }
1609 
1610 /*
1611  * The default value of e1000g_poll_mode == 0 assumes that the NIC is
1612  * capable of supporting only one interrupt and we shouldn't disable
1613  * the physical interrupt. In this case we let the interrupt come and
1614  * we queue the packets in the rx ring itself in case we are in polling
1615  * mode (better latency but slightly lower performance and a very
1616  * high intrrupt count in mpstat which is harmless).
1617  *
1618  * e1000g_poll_mode == 1 assumes that we have per Rx ring interrupt
1619  * which can be disabled in poll mode. This gives better overall
1620  * throughput (compared to the mode above), shows very low interrupt
1621  * count but has slightly higher latency since we pick the packets when
1622  * the poll thread does polling.
1623  *
1624  * Currently, this flag should be enabled only while doing performance
1625  * measurement or when it can be guaranteed that entire NIC going
1626  * in poll mode will not harm any traffic like cluster heartbeat etc.
1627  */
1628 int e1000g_poll_mode = 0;
1629 
1630 /*
1631  * Called from the upper layers when driver is in polling mode to
1632  * pick up any queued packets. Care should be taken to not block
1633  * this thread.
1634  */
1635 static mblk_t *e1000g_poll_ring(void *arg, int bytes_to_pickup)
1636 {
1637 	e1000g_rx_ring_t	*rx_ring = (e1000g_rx_ring_t *)arg;
1638 	mblk_t			*mp = NULL;
1639 	mblk_t			*tail;
1640 	struct e1000g 		*adapter;
1641 
1642 	adapter = rx_ring->adapter;
1643 
1644 	rw_enter(&adapter->chip_lock, RW_READER);
1645 
1646 	if (adapter->e1000g_state & E1000G_SUSPENDED) {
1647 		rw_exit(&adapter->chip_lock);
1648 		return (NULL);
1649 	}
1650 
1651 	mutex_enter(&rx_ring->rx_lock);
1652 	mp = e1000g_receive(rx_ring, &tail, bytes_to_pickup);
1653 	mutex_exit(&rx_ring->rx_lock);
1654 	rw_exit(&adapter->chip_lock);
1655 	return (mp);
1656 }
1657 
1658 static int
1659 e1000g_m_start(void *arg)
1660 {
1661 	struct e1000g *Adapter = (struct e1000g *)arg;
1662 
1663 	rw_enter(&Adapter->chip_lock, RW_WRITER);
1664 
1665 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
1666 		rw_exit(&Adapter->chip_lock);
1667 		return (ECANCELED);
1668 	}
1669 
1670 	if (e1000g_start(Adapter, B_TRUE) != DDI_SUCCESS) {
1671 		rw_exit(&Adapter->chip_lock);
1672 		return (ENOTACTIVE);
1673 	}
1674 
1675 	Adapter->e1000g_state |= E1000G_STARTED;
1676 
1677 	rw_exit(&Adapter->chip_lock);
1678 
1679 	/* Enable and start the watchdog timer */
1680 	enable_watchdog_timer(Adapter);
1681 
1682 	return (0);
1683 }
1684 
1685 static int
1686 e1000g_start(struct e1000g *Adapter, boolean_t global)
1687 {
1688 	e1000g_rx_data_t *rx_data;
1689 
1690 	if (global) {
1691 		if (e1000g_alloc_rx_data(Adapter) != DDI_SUCCESS) {
1692 			e1000g_log(Adapter, CE_WARN, "Allocate rx data failed");
1693 			goto start_fail;
1694 		}
1695 
1696 		/* Allocate dma resources for descriptors and buffers */
1697 		if (e1000g_alloc_dma_resources(Adapter) != DDI_SUCCESS) {
1698 			e1000g_log(Adapter, CE_WARN,
1699 			    "Alloc DMA resources failed");
1700 			goto start_fail;
1701 		}
1702 		Adapter->rx_buffer_setup = B_FALSE;
1703 	}
1704 
1705 	if (!(Adapter->attach_progress & ATTACH_PROGRESS_INIT)) {
1706 		if (e1000g_init(Adapter) != DDI_SUCCESS) {
1707 			e1000g_log(Adapter, CE_WARN,
1708 			    "Adapter initialization failed");
1709 			goto start_fail;
1710 		}
1711 	}
1712 
1713 	/* Setup and initialize the transmit structures */
1714 	e1000g_tx_setup(Adapter);
1715 	msec_delay(5);
1716 
1717 	/* Setup and initialize the receive structures */
1718 	e1000g_rx_setup(Adapter);
1719 	msec_delay(5);
1720 
1721 	/* Restore the e1000g promiscuous mode */
1722 	e1000g_restore_promisc(Adapter);
1723 
1724 	e1000g_mask_interrupt(Adapter);
1725 
1726 	Adapter->attach_progress |= ATTACH_PROGRESS_INIT;
1727 
1728 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
1729 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
1730 		goto start_fail;
1731 	}
1732 
1733 	return (DDI_SUCCESS);
1734 
1735 start_fail:
1736 	rx_data = Adapter->rx_ring->rx_data;
1737 
1738 	if (global) {
1739 		e1000g_release_dma_resources(Adapter);
1740 		e1000g_free_rx_pending_buffers(rx_data);
1741 		e1000g_free_rx_data(rx_data);
1742 	}
1743 
1744 	mutex_enter(&e1000g_nvm_lock);
1745 	(void) e1000_reset_hw(&Adapter->shared);
1746 	mutex_exit(&e1000g_nvm_lock);
1747 
1748 	return (DDI_FAILURE);
1749 }
1750 
1751 static void
1752 e1000g_m_stop(void *arg)
1753 {
1754 	struct e1000g *Adapter = (struct e1000g *)arg;
1755 
1756 	/* Drain tx sessions */
1757 	(void) e1000g_tx_drain(Adapter);
1758 
1759 	rw_enter(&Adapter->chip_lock, RW_WRITER);
1760 
1761 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
1762 		rw_exit(&Adapter->chip_lock);
1763 		return;
1764 	}
1765 	Adapter->e1000g_state &= ~E1000G_STARTED;
1766 	e1000g_stop(Adapter, B_TRUE);
1767 
1768 	rw_exit(&Adapter->chip_lock);
1769 
1770 	/* Disable and stop all the timers */
1771 	disable_watchdog_timer(Adapter);
1772 	stop_link_timer(Adapter);
1773 	stop_82547_timer(Adapter->tx_ring);
1774 }
1775 
1776 static void
1777 e1000g_stop(struct e1000g *Adapter, boolean_t global)
1778 {
1779 	private_devi_list_t *devi_node;
1780 	e1000g_rx_data_t *rx_data;
1781 	int result;
1782 
1783 	Adapter->attach_progress &= ~ATTACH_PROGRESS_INIT;
1784 
1785 	/* Stop the chip and release pending resources */
1786 
1787 	/* Tell firmware driver is no longer in control */
1788 	e1000g_release_driver_control(&Adapter->shared);
1789 
1790 	e1000g_clear_all_interrupts(Adapter);
1791 
1792 	mutex_enter(&e1000g_nvm_lock);
1793 	result = e1000_reset_hw(&Adapter->shared);
1794 	mutex_exit(&e1000g_nvm_lock);
1795 
1796 	if (result != E1000_SUCCESS) {
1797 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
1798 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
1799 	}
1800 
1801 	/* Release resources still held by the TX descriptors */
1802 	e1000g_tx_clean(Adapter);
1803 
1804 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
1805 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
1806 
1807 	/* Clean the pending rx jumbo packet fragment */
1808 	e1000g_rx_clean(Adapter);
1809 
1810 	if (global) {
1811 		e1000g_release_dma_resources(Adapter);
1812 
1813 		mutex_enter(&e1000g_rx_detach_lock);
1814 		rx_data = Adapter->rx_ring->rx_data;
1815 		rx_data->flag |= E1000G_RX_STOPPED;
1816 
1817 		if (rx_data->pending_count == 0) {
1818 			e1000g_free_rx_pending_buffers(rx_data);
1819 			e1000g_free_rx_data(rx_data);
1820 		} else {
1821 			devi_node = rx_data->priv_devi_node;
1822 			if (devi_node != NULL)
1823 				atomic_inc_32(&devi_node->pending_rx_count);
1824 			else
1825 				atomic_inc_32(&Adapter->pending_rx_count);
1826 		}
1827 		mutex_exit(&e1000g_rx_detach_lock);
1828 	}
1829 
1830 	if (Adapter->link_state == LINK_STATE_UP) {
1831 		Adapter->link_state = LINK_STATE_UNKNOWN;
1832 		mac_link_update(Adapter->mh, Adapter->link_state);
1833 	}
1834 }
1835 
1836 static void
1837 e1000g_rx_clean(struct e1000g *Adapter)
1838 {
1839 	e1000g_rx_data_t *rx_data = Adapter->rx_ring->rx_data;
1840 
1841 	if (rx_data == NULL)
1842 		return;
1843 
1844 	if (rx_data->rx_mblk != NULL) {
1845 		freemsg(rx_data->rx_mblk);
1846 		rx_data->rx_mblk = NULL;
1847 		rx_data->rx_mblk_tail = NULL;
1848 		rx_data->rx_mblk_len = 0;
1849 	}
1850 }
1851 
1852 static void
1853 e1000g_tx_clean(struct e1000g *Adapter)
1854 {
1855 	e1000g_tx_ring_t *tx_ring;
1856 	p_tx_sw_packet_t packet;
1857 	mblk_t *mp;
1858 	mblk_t *nmp;
1859 	uint32_t packet_count;
1860 
1861 	tx_ring = Adapter->tx_ring;
1862 
1863 	/*
1864 	 * Here we don't need to protect the lists using
1865 	 * the usedlist_lock and freelist_lock, for they
1866 	 * have been protected by the chip_lock.
1867 	 */
1868 	mp = NULL;
1869 	nmp = NULL;
1870 	packet_count = 0;
1871 	packet = (p_tx_sw_packet_t)QUEUE_GET_HEAD(&tx_ring->used_list);
1872 	while (packet != NULL) {
1873 		if (packet->mp != NULL) {
1874 			/* Assemble the message chain */
1875 			if (mp == NULL) {
1876 				mp = packet->mp;
1877 				nmp = packet->mp;
1878 			} else {
1879 				nmp->b_next = packet->mp;
1880 				nmp = packet->mp;
1881 			}
1882 			/* Disconnect the message from the sw packet */
1883 			packet->mp = NULL;
1884 		}
1885 
1886 		e1000g_free_tx_swpkt(packet);
1887 		packet_count++;
1888 
1889 		packet = (p_tx_sw_packet_t)
1890 		    QUEUE_GET_NEXT(&tx_ring->used_list, &packet->Link);
1891 	}
1892 
1893 	if (mp != NULL)
1894 		freemsgchain(mp);
1895 
1896 	if (packet_count > 0) {
1897 		QUEUE_APPEND(&tx_ring->free_list, &tx_ring->used_list);
1898 		QUEUE_INIT_LIST(&tx_ring->used_list);
1899 
1900 		/* Setup TX descriptor pointers */
1901 		tx_ring->tbd_next = tx_ring->tbd_first;
1902 		tx_ring->tbd_oldest = tx_ring->tbd_first;
1903 
1904 		/* Setup our HW Tx Head & Tail descriptor pointers */
1905 		E1000_WRITE_REG(&Adapter->shared, E1000_TDH(0), 0);
1906 		E1000_WRITE_REG(&Adapter->shared, E1000_TDT(0), 0);
1907 	}
1908 }
1909 
1910 static boolean_t
1911 e1000g_tx_drain(struct e1000g *Adapter)
1912 {
1913 	int i;
1914 	boolean_t done;
1915 	e1000g_tx_ring_t *tx_ring;
1916 
1917 	tx_ring = Adapter->tx_ring;
1918 
1919 	/* Allow up to 'wsdraintime' for pending xmit's to complete. */
1920 	for (i = 0; i < TX_DRAIN_TIME; i++) {
1921 		mutex_enter(&tx_ring->usedlist_lock);
1922 		done = IS_QUEUE_EMPTY(&tx_ring->used_list);
1923 		mutex_exit(&tx_ring->usedlist_lock);
1924 
1925 		if (done)
1926 			break;
1927 
1928 		msec_delay(1);
1929 	}
1930 
1931 	return (done);
1932 }
1933 
1934 static boolean_t
1935 e1000g_rx_drain(struct e1000g *Adapter)
1936 {
1937 	int i;
1938 	boolean_t done;
1939 
1940 	/*
1941 	 * Allow up to RX_DRAIN_TIME for pending received packets to complete.
1942 	 */
1943 	for (i = 0; i < RX_DRAIN_TIME; i++) {
1944 		done = (Adapter->pending_rx_count == 0);
1945 
1946 		if (done)
1947 			break;
1948 
1949 		msec_delay(1);
1950 	}
1951 
1952 	return (done);
1953 }
1954 
1955 static boolean_t
1956 e1000g_reset_adapter(struct e1000g *Adapter)
1957 {
1958 	/* Disable and stop all the timers */
1959 	disable_watchdog_timer(Adapter);
1960 	stop_link_timer(Adapter);
1961 	stop_82547_timer(Adapter->tx_ring);
1962 
1963 	rw_enter(&Adapter->chip_lock, RW_WRITER);
1964 
1965 	e1000g_stop(Adapter, B_FALSE);
1966 
1967 	if (e1000g_start(Adapter, B_FALSE) != DDI_SUCCESS) {
1968 		rw_exit(&Adapter->chip_lock);
1969 		e1000g_log(Adapter, CE_WARN, "Reset failed");
1970 			return (B_FALSE);
1971 	}
1972 
1973 	rw_exit(&Adapter->chip_lock);
1974 
1975 	/* Enable and start the watchdog timer */
1976 	enable_watchdog_timer(Adapter);
1977 
1978 	return (B_TRUE);
1979 }
1980 
1981 boolean_t
1982 e1000g_global_reset(struct e1000g *Adapter)
1983 {
1984 	/* Disable and stop all the timers */
1985 	disable_watchdog_timer(Adapter);
1986 	stop_link_timer(Adapter);
1987 	stop_82547_timer(Adapter->tx_ring);
1988 
1989 	rw_enter(&Adapter->chip_lock, RW_WRITER);
1990 
1991 	e1000g_stop(Adapter, B_TRUE);
1992 
1993 	Adapter->init_count = 0;
1994 
1995 	if (e1000g_start(Adapter, B_TRUE) != DDI_SUCCESS) {
1996 		rw_exit(&Adapter->chip_lock);
1997 		e1000g_log(Adapter, CE_WARN, "Reset failed");
1998 		return (B_FALSE);
1999 	}
2000 
2001 	rw_exit(&Adapter->chip_lock);
2002 
2003 	/* Enable and start the watchdog timer */
2004 	enable_watchdog_timer(Adapter);
2005 
2006 	return (B_TRUE);
2007 }
2008 
2009 /*
2010  * e1000g_intr_pciexpress - ISR for PCI Express chipsets
2011  *
2012  * This interrupt service routine is for PCI-Express adapters.
2013  * The ICR contents is valid only when the E1000_ICR_INT_ASSERTED
2014  * bit is set.
2015  */
2016 static uint_t
2017 e1000g_intr_pciexpress(caddr_t arg)
2018 {
2019 	struct e1000g *Adapter;
2020 	uint32_t icr;
2021 
2022 	Adapter = (struct e1000g *)(uintptr_t)arg;
2023 	icr = E1000_READ_REG(&Adapter->shared, E1000_ICR);
2024 
2025 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
2026 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
2027 
2028 	if (icr & E1000_ICR_INT_ASSERTED) {
2029 		/*
2030 		 * E1000_ICR_INT_ASSERTED bit was set:
2031 		 * Read(Clear) the ICR, claim this interrupt,
2032 		 * look for work to do.
2033 		 */
2034 		e1000g_intr_work(Adapter, icr);
2035 		return (DDI_INTR_CLAIMED);
2036 	} else {
2037 		/*
2038 		 * E1000_ICR_INT_ASSERTED bit was not set:
2039 		 * Don't claim this interrupt, return immediately.
2040 		 */
2041 		return (DDI_INTR_UNCLAIMED);
2042 	}
2043 }
2044 
2045 /*
2046  * e1000g_intr - ISR for PCI/PCI-X chipsets
2047  *
2048  * This interrupt service routine is for PCI/PCI-X adapters.
2049  * We check the ICR contents no matter the E1000_ICR_INT_ASSERTED
2050  * bit is set or not.
2051  */
2052 static uint_t
2053 e1000g_intr(caddr_t arg)
2054 {
2055 	struct e1000g *Adapter;
2056 	uint32_t icr;
2057 
2058 	Adapter = (struct e1000g *)(uintptr_t)arg;
2059 	icr = E1000_READ_REG(&Adapter->shared, E1000_ICR);
2060 
2061 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
2062 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
2063 
2064 	if (icr) {
2065 		/*
2066 		 * Any bit was set in ICR:
2067 		 * Read(Clear) the ICR, claim this interrupt,
2068 		 * look for work to do.
2069 		 */
2070 		e1000g_intr_work(Adapter, icr);
2071 		return (DDI_INTR_CLAIMED);
2072 	} else {
2073 		/*
2074 		 * No bit was set in ICR:
2075 		 * Don't claim this interrupt, return immediately.
2076 		 */
2077 		return (DDI_INTR_UNCLAIMED);
2078 	}
2079 }
2080 
2081 /*
2082  * e1000g_intr_work - actual processing of ISR
2083  *
2084  * Read(clear) the ICR contents and call appropriate interrupt
2085  * processing routines.
2086  */
2087 static void
2088 e1000g_intr_work(struct e1000g *Adapter, uint32_t icr)
2089 {
2090 	struct e1000_hw *hw;
2091 	hw = &Adapter->shared;
2092 	e1000g_tx_ring_t *tx_ring = Adapter->tx_ring;
2093 
2094 	Adapter->rx_pkt_cnt = 0;
2095 	Adapter->tx_pkt_cnt = 0;
2096 
2097 	rw_enter(&Adapter->chip_lock, RW_READER);
2098 
2099 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
2100 		rw_exit(&Adapter->chip_lock);
2101 		return;
2102 	}
2103 	/*
2104 	 * Here we need to check the "e1000g_state" flag within the chip_lock to
2105 	 * ensure the receive routine will not execute when the adapter is
2106 	 * being reset.
2107 	 */
2108 	if (!(Adapter->e1000g_state & E1000G_STARTED)) {
2109 		rw_exit(&Adapter->chip_lock);
2110 		return;
2111 	}
2112 
2113 	if (icr & E1000_ICR_RXT0) {
2114 		mblk_t			*mp = NULL;
2115 		mblk_t			*tail = NULL;
2116 		e1000g_rx_ring_t	*rx_ring;
2117 
2118 		rx_ring = Adapter->rx_ring;
2119 		mutex_enter(&rx_ring->rx_lock);
2120 		/*
2121 		 * Sometimes with legacy interrupts, it possible that
2122 		 * there is a single interrupt for Rx/Tx. In which
2123 		 * case, if poll flag is set, we shouldn't really
2124 		 * be doing Rx processing.
2125 		 */
2126 		if (!rx_ring->poll_flag)
2127 			mp = e1000g_receive(rx_ring, &tail,
2128 			    E1000G_CHAIN_NO_LIMIT);
2129 		mutex_exit(&rx_ring->rx_lock);
2130 		rw_exit(&Adapter->chip_lock);
2131 		if (mp != NULL)
2132 			mac_rx_ring(Adapter->mh, rx_ring->mrh,
2133 			    mp, rx_ring->ring_gen_num);
2134 	} else
2135 		rw_exit(&Adapter->chip_lock);
2136 
2137 	if (icr & E1000_ICR_TXDW) {
2138 		if (!Adapter->tx_intr_enable)
2139 			e1000g_clear_tx_interrupt(Adapter);
2140 
2141 		/* Recycle the tx descriptors */
2142 		rw_enter(&Adapter->chip_lock, RW_READER);
2143 		(void) e1000g_recycle(tx_ring);
2144 		E1000G_DEBUG_STAT(tx_ring->stat_recycle_intr);
2145 		rw_exit(&Adapter->chip_lock);
2146 
2147 		if (tx_ring->resched_needed &&
2148 		    (tx_ring->tbd_avail > DEFAULT_TX_UPDATE_THRESHOLD)) {
2149 			tx_ring->resched_needed = B_FALSE;
2150 			mac_tx_update(Adapter->mh);
2151 			E1000G_STAT(tx_ring->stat_reschedule);
2152 		}
2153 	}
2154 
2155 	/*
2156 	 * The Receive Sequence errors RXSEQ and the link status change LSC
2157 	 * are checked to detect that the cable has been pulled out. For
2158 	 * the Wiseman 2.0 silicon, the receive sequence errors interrupt
2159 	 * are an indication that cable is not connected.
2160 	 */
2161 	if ((icr & E1000_ICR_RXSEQ) ||
2162 	    (icr & E1000_ICR_LSC) ||
2163 	    (icr & E1000_ICR_GPI_EN1)) {
2164 		boolean_t link_changed;
2165 		timeout_id_t tid = 0;
2166 
2167 		stop_watchdog_timer(Adapter);
2168 
2169 		rw_enter(&Adapter->chip_lock, RW_WRITER);
2170 
2171 		/*
2172 		 * Because we got a link-status-change interrupt, force
2173 		 * e1000_check_for_link() to look at phy
2174 		 */
2175 		Adapter->shared.mac.get_link_status = B_TRUE;
2176 
2177 		/* e1000g_link_check takes care of link status change */
2178 		link_changed = e1000g_link_check(Adapter);
2179 
2180 		/* Get new phy state */
2181 		e1000g_get_phy_state(Adapter);
2182 
2183 		/*
2184 		 * If the link timer has not timed out, we'll not notify
2185 		 * the upper layer with any link state until the link is up.
2186 		 */
2187 		if (link_changed && !Adapter->link_complete) {
2188 			if (Adapter->link_state == LINK_STATE_UP) {
2189 				mutex_enter(&Adapter->link_lock);
2190 				Adapter->link_complete = B_TRUE;
2191 				tid = Adapter->link_tid;
2192 				Adapter->link_tid = 0;
2193 				mutex_exit(&Adapter->link_lock);
2194 			} else {
2195 				link_changed = B_FALSE;
2196 			}
2197 		}
2198 		rw_exit(&Adapter->chip_lock);
2199 
2200 		if (link_changed) {
2201 			if (tid != 0)
2202 				(void) untimeout(tid);
2203 
2204 			/*
2205 			 * Workaround for esb2. Data stuck in fifo on a link
2206 			 * down event. Stop receiver here and reset in watchdog.
2207 			 */
2208 			if ((Adapter->link_state == LINK_STATE_DOWN) &&
2209 			    (Adapter->shared.mac.type == e1000_80003es2lan)) {
2210 				uint32_t rctl = E1000_READ_REG(hw, E1000_RCTL);
2211 				E1000_WRITE_REG(hw, E1000_RCTL,
2212 				    rctl & ~E1000_RCTL_EN);
2213 				e1000g_log(Adapter, CE_WARN,
2214 				    "ESB2 receiver disabled");
2215 				Adapter->esb2_workaround = B_TRUE;
2216 			}
2217 			if (!Adapter->reset_flag)
2218 				mac_link_update(Adapter->mh,
2219 				    Adapter->link_state);
2220 			if (Adapter->link_state == LINK_STATE_UP)
2221 				Adapter->reset_flag = B_FALSE;
2222 		}
2223 
2224 		start_watchdog_timer(Adapter);
2225 	}
2226 }
2227 
2228 static void
2229 e1000g_init_unicst(struct e1000g *Adapter)
2230 {
2231 	struct e1000_hw *hw;
2232 	int slot;
2233 
2234 	hw = &Adapter->shared;
2235 
2236 	if (Adapter->init_count == 0) {
2237 		/* Initialize the multiple unicast addresses */
2238 		Adapter->unicst_total = MAX_NUM_UNICAST_ADDRESSES;
2239 
2240 		/* Workaround for an erratum of 82571 chipst */
2241 		if ((hw->mac.type == e1000_82571) &&
2242 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
2243 			Adapter->unicst_total--;
2244 
2245 		Adapter->unicst_avail = Adapter->unicst_total;
2246 
2247 		for (slot = 0; slot < Adapter->unicst_total; slot++) {
2248 			/* Clear both the flag and MAC address */
2249 			Adapter->unicst_addr[slot].reg.high = 0;
2250 			Adapter->unicst_addr[slot].reg.low = 0;
2251 		}
2252 	} else {
2253 		/* Workaround for an erratum of 82571 chipst */
2254 		if ((hw->mac.type == e1000_82571) &&
2255 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
2256 			e1000_rar_set(hw, hw->mac.addr, LAST_RAR_ENTRY);
2257 
2258 		/* Re-configure the RAR registers */
2259 		for (slot = 0; slot < Adapter->unicst_total; slot++)
2260 			if (Adapter->unicst_addr[slot].mac.set == 1)
2261 				e1000_rar_set(hw,
2262 				    Adapter->unicst_addr[slot].mac.addr, slot);
2263 	}
2264 
2265 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
2266 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
2267 }
2268 
2269 static int
2270 e1000g_unicst_set(struct e1000g *Adapter, const uint8_t *mac_addr,
2271     int slot)
2272 {
2273 	struct e1000_hw *hw;
2274 
2275 	hw = &Adapter->shared;
2276 
2277 	/*
2278 	 * The first revision of Wiseman silicon (rev 2.0) has an errata
2279 	 * that requires the receiver to be in reset when any of the
2280 	 * receive address registers (RAR regs) are accessed.  The first
2281 	 * rev of Wiseman silicon also requires MWI to be disabled when
2282 	 * a global reset or a receive reset is issued.  So before we
2283 	 * initialize the RARs, we check the rev of the Wiseman controller
2284 	 * and work around any necessary HW errata.
2285 	 */
2286 	if ((hw->mac.type == e1000_82542) &&
2287 	    (hw->revision_id == E1000_REVISION_2)) {
2288 		e1000_pci_clear_mwi(hw);
2289 		E1000_WRITE_REG(hw, E1000_RCTL, E1000_RCTL_RST);
2290 		msec_delay(5);
2291 	}
2292 	if (mac_addr == NULL) {
2293 		E1000_WRITE_REG_ARRAY(hw, E1000_RA, slot << 1, 0);
2294 		E1000_WRITE_FLUSH(hw);
2295 		E1000_WRITE_REG_ARRAY(hw, E1000_RA, (slot << 1) + 1, 0);
2296 		E1000_WRITE_FLUSH(hw);
2297 		/* Clear both the flag and MAC address */
2298 		Adapter->unicst_addr[slot].reg.high = 0;
2299 		Adapter->unicst_addr[slot].reg.low = 0;
2300 	} else {
2301 		bcopy(mac_addr, Adapter->unicst_addr[slot].mac.addr,
2302 		    ETHERADDRL);
2303 		e1000_rar_set(hw, (uint8_t *)mac_addr, slot);
2304 		Adapter->unicst_addr[slot].mac.set = 1;
2305 	}
2306 
2307 	/* Workaround for an erratum of 82571 chipst */
2308 	if (slot == 0) {
2309 		if ((hw->mac.type == e1000_82571) &&
2310 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
2311 			if (mac_addr == NULL) {
2312 				E1000_WRITE_REG_ARRAY(hw, E1000_RA,
2313 				    slot << 1, 0);
2314 				E1000_WRITE_FLUSH(hw);
2315 				E1000_WRITE_REG_ARRAY(hw, E1000_RA,
2316 				    (slot << 1) + 1, 0);
2317 				E1000_WRITE_FLUSH(hw);
2318 			} else {
2319 				e1000_rar_set(hw, (uint8_t *)mac_addr,
2320 				    LAST_RAR_ENTRY);
2321 			}
2322 	}
2323 
2324 	/*
2325 	 * If we are using Wiseman rev 2.0 silicon, we will have previously
2326 	 * put the receive in reset, and disabled MWI, to work around some
2327 	 * HW errata.  Now we should take the receiver out of reset, and
2328 	 * re-enabled if MWI if it was previously enabled by the PCI BIOS.
2329 	 */
2330 	if ((hw->mac.type == e1000_82542) &&
2331 	    (hw->revision_id == E1000_REVISION_2)) {
2332 		E1000_WRITE_REG(hw, E1000_RCTL, 0);
2333 		msec_delay(1);
2334 		if (hw->bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
2335 			e1000_pci_set_mwi(hw);
2336 		e1000g_rx_setup(Adapter);
2337 	}
2338 
2339 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
2340 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
2341 		return (EIO);
2342 	}
2343 
2344 	return (0);
2345 }
2346 
2347 static int
2348 multicst_add(struct e1000g *Adapter, const uint8_t *multiaddr)
2349 {
2350 	struct e1000_hw *hw = &Adapter->shared;
2351 	int res = 0;
2352 
2353 	if ((multiaddr[0] & 01) == 0) {
2354 		res = EINVAL;
2355 		goto done;
2356 	}
2357 
2358 	if (Adapter->mcast_count >= MAX_NUM_MULTICAST_ADDRESSES) {
2359 		res = ENOENT;
2360 		goto done;
2361 	}
2362 
2363 	bcopy(multiaddr,
2364 	    &Adapter->mcast_table[Adapter->mcast_count], ETHERADDRL);
2365 	Adapter->mcast_count++;
2366 
2367 	/*
2368 	 * Update the MC table in the hardware
2369 	 */
2370 	e1000g_clear_interrupt(Adapter);
2371 
2372 	e1000g_setup_multicast(Adapter);
2373 
2374 	if ((hw->mac.type == e1000_82542) &&
2375 	    (hw->revision_id == E1000_REVISION_2))
2376 		e1000g_rx_setup(Adapter);
2377 
2378 	e1000g_mask_interrupt(Adapter);
2379 
2380 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
2381 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
2382 		res = EIO;
2383 	}
2384 
2385 done:
2386 	return (res);
2387 }
2388 
2389 static int
2390 multicst_remove(struct e1000g *Adapter, const uint8_t *multiaddr)
2391 {
2392 	struct e1000_hw *hw = &Adapter->shared;
2393 	unsigned i;
2394 
2395 	for (i = 0; i < Adapter->mcast_count; i++) {
2396 		if (bcmp(multiaddr, &Adapter->mcast_table[i],
2397 		    ETHERADDRL) == 0) {
2398 			for (i++; i < Adapter->mcast_count; i++) {
2399 				Adapter->mcast_table[i - 1] =
2400 				    Adapter->mcast_table[i];
2401 			}
2402 			Adapter->mcast_count--;
2403 			break;
2404 		}
2405 	}
2406 
2407 	/*
2408 	 * Update the MC table in the hardware
2409 	 */
2410 	e1000g_clear_interrupt(Adapter);
2411 
2412 	e1000g_setup_multicast(Adapter);
2413 
2414 	if ((hw->mac.type == e1000_82542) &&
2415 	    (hw->revision_id == E1000_REVISION_2))
2416 		e1000g_rx_setup(Adapter);
2417 
2418 	e1000g_mask_interrupt(Adapter);
2419 
2420 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
2421 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
2422 		return (EIO);
2423 	}
2424 
2425 	return (0);
2426 }
2427 
2428 /*
2429  * e1000g_setup_multicast - setup multicast data structures
2430  *
2431  * This routine initializes all of the multicast related structures.
2432  */
2433 void
2434 e1000g_setup_multicast(struct e1000g *Adapter)
2435 {
2436 	uint8_t *mc_addr_list;
2437 	uint32_t mc_addr_count;
2438 	uint32_t rctl;
2439 	struct e1000_hw *hw;
2440 
2441 	hw = &Adapter->shared;
2442 
2443 	/*
2444 	 * The e1000g has the ability to do perfect filtering of 16
2445 	 * addresses. The driver uses one of the e1000g's 16 receive
2446 	 * address registers for its node/network/mac/individual address.
2447 	 * So, we have room for up to 15 multicast addresses in the CAM,
2448 	 * additional MC addresses are handled by the MTA (Multicast Table
2449 	 * Array)
2450 	 */
2451 
2452 	rctl = E1000_READ_REG(hw, E1000_RCTL);
2453 
2454 	mc_addr_list = (uint8_t *)Adapter->mcast_table;
2455 
2456 	if (Adapter->mcast_count > MAX_NUM_MULTICAST_ADDRESSES) {
2457 		E1000G_DEBUGLOG_1(Adapter, CE_WARN,
2458 		    "Adapter requested more than %d MC Addresses.\n",
2459 		    MAX_NUM_MULTICAST_ADDRESSES);
2460 		mc_addr_count = MAX_NUM_MULTICAST_ADDRESSES;
2461 	} else {
2462 		/*
2463 		 * Set the number of MC addresses that we are being
2464 		 * requested to use
2465 		 */
2466 		mc_addr_count = Adapter->mcast_count;
2467 	}
2468 	/*
2469 	 * The Wiseman 2.0 silicon has an errata by which the receiver will
2470 	 * hang  while writing to the receive address registers if the receiver
2471 	 * is not in reset before writing to the registers. Updating the RAR
2472 	 * is done during the setting up of the multicast table, hence the
2473 	 * receiver has to be put in reset before updating the multicast table
2474 	 * and then taken out of reset at the end
2475 	 */
2476 	/*
2477 	 * if WMI was enabled then dis able it before issueing the global
2478 	 * reset to the hardware.
2479 	 */
2480 	/*
2481 	 * Only required for WISEMAN_2_0
2482 	 */
2483 	if ((hw->mac.type == e1000_82542) &&
2484 	    (hw->revision_id == E1000_REVISION_2)) {
2485 		e1000_pci_clear_mwi(hw);
2486 		/*
2487 		 * The e1000g must be in reset before changing any RA
2488 		 * registers. Reset receive unit.  The chip will remain in
2489 		 * the reset state until software explicitly restarts it.
2490 		 */
2491 		E1000_WRITE_REG(hw, E1000_RCTL, E1000_RCTL_RST);
2492 		/* Allow receiver time to go in to reset */
2493 		msec_delay(5);
2494 	}
2495 
2496 	e1000_update_mc_addr_list(hw, mc_addr_list, mc_addr_count,
2497 	    Adapter->unicst_total, hw->mac.rar_entry_count);
2498 
2499 	/*
2500 	 * Only for Wiseman_2_0
2501 	 * If MWI was enabled then re-enable it after issueing (as we
2502 	 * disabled it up there) the receive reset command.
2503 	 * Wainwright does not have a receive reset command and only thing
2504 	 * close to it is global reset which will require tx setup also
2505 	 */
2506 	if ((hw->mac.type == e1000_82542) &&
2507 	    (hw->revision_id == E1000_REVISION_2)) {
2508 		/*
2509 		 * if WMI was enabled then reenable it after issueing the
2510 		 * global or receive reset to the hardware.
2511 		 */
2512 
2513 		/*
2514 		 * Take receiver out of reset
2515 		 * clear E1000_RCTL_RST bit (and all others)
2516 		 */
2517 		E1000_WRITE_REG(hw, E1000_RCTL, 0);
2518 		msec_delay(5);
2519 		if (hw->bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
2520 			e1000_pci_set_mwi(hw);
2521 	}
2522 
2523 	/*
2524 	 * Restore original value
2525 	 */
2526 	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
2527 }
2528 
2529 int
2530 e1000g_m_multicst(void *arg, boolean_t add, const uint8_t *addr)
2531 {
2532 	struct e1000g *Adapter = (struct e1000g *)arg;
2533 	int result;
2534 
2535 	rw_enter(&Adapter->chip_lock, RW_WRITER);
2536 
2537 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
2538 		result = ECANCELED;
2539 		goto done;
2540 	}
2541 
2542 	result = (add) ? multicst_add(Adapter, addr)
2543 	    : multicst_remove(Adapter, addr);
2544 
2545 done:
2546 	rw_exit(&Adapter->chip_lock);
2547 	return (result);
2548 
2549 }
2550 
2551 int
2552 e1000g_m_promisc(void *arg, boolean_t on)
2553 {
2554 	struct e1000g *Adapter = (struct e1000g *)arg;
2555 	uint32_t rctl;
2556 
2557 	rw_enter(&Adapter->chip_lock, RW_WRITER);
2558 
2559 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
2560 		rw_exit(&Adapter->chip_lock);
2561 		return (ECANCELED);
2562 	}
2563 
2564 	rctl = E1000_READ_REG(&Adapter->shared, E1000_RCTL);
2565 
2566 	if (on)
2567 		rctl |=
2568 		    (E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_BAM);
2569 	else
2570 		rctl &= (~(E1000_RCTL_UPE | E1000_RCTL_MPE));
2571 
2572 	E1000_WRITE_REG(&Adapter->shared, E1000_RCTL, rctl);
2573 
2574 	Adapter->e1000g_promisc = on;
2575 
2576 	rw_exit(&Adapter->chip_lock);
2577 
2578 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
2579 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
2580 		return (EIO);
2581 	}
2582 
2583 	return (0);
2584 }
2585 
2586 /*
2587  * Entry points to enable and disable interrupts at the granularity of
2588  * a group.
2589  * Turns the poll_mode for the whole adapter on and off to enable or
2590  * override the ring level polling control over the hardware interrupts.
2591  */
2592 static int
2593 e1000g_rx_group_intr_enable(mac_intr_handle_t arg)
2594 {
2595 	struct e1000g		*adapter = (struct e1000g *)arg;
2596 	e1000g_rx_ring_t *rx_ring = adapter->rx_ring;
2597 
2598 	/*
2599 	 * Later interrupts at the granularity of the this ring will
2600 	 * invoke mac_rx() with NULL, indicating the need for another
2601 	 * software classification.
2602 	 * We have a single ring usable per adapter now, so we only need to
2603 	 * reset the rx handle for that one.
2604 	 * When more RX rings can be used, we should update each one of them.
2605 	 */
2606 	mutex_enter(&rx_ring->rx_lock);
2607 	rx_ring->mrh = NULL;
2608 	adapter->poll_mode = B_FALSE;
2609 	mutex_exit(&rx_ring->rx_lock);
2610 	return (0);
2611 }
2612 
2613 static int
2614 e1000g_rx_group_intr_disable(mac_intr_handle_t arg)
2615 {
2616 	struct e1000g *adapter = (struct e1000g *)arg;
2617 	e1000g_rx_ring_t *rx_ring = adapter->rx_ring;
2618 
2619 	mutex_enter(&rx_ring->rx_lock);
2620 
2621 	/*
2622 	 * Later interrupts at the granularity of the this ring will
2623 	 * invoke mac_rx() with the handle for this ring;
2624 	 */
2625 	adapter->poll_mode = B_TRUE;
2626 	rx_ring->mrh = rx_ring->mrh_init;
2627 	mutex_exit(&rx_ring->rx_lock);
2628 	return (0);
2629 }
2630 
2631 /*
2632  * Entry points to enable and disable interrupts at the granularity of
2633  * a ring.
2634  * adapter poll_mode controls whether we actually proceed with hardware
2635  * interrupt toggling.
2636  */
2637 static int
2638 e1000g_rx_ring_intr_enable(mac_intr_handle_t intrh)
2639 {
2640 	e1000g_rx_ring_t	*rx_ring = (e1000g_rx_ring_t *)intrh;
2641 	struct e1000g 		*adapter = rx_ring->adapter;
2642 	struct e1000_hw 	*hw = &adapter->shared;
2643 	uint32_t		intr_mask;
2644 
2645 	rw_enter(&adapter->chip_lock, RW_READER);
2646 
2647 	if (adapter->e1000g_state & E1000G_SUSPENDED) {
2648 		rw_exit(&adapter->chip_lock);
2649 		return (0);
2650 	}
2651 
2652 	mutex_enter(&rx_ring->rx_lock);
2653 	rx_ring->poll_flag = 0;
2654 	mutex_exit(&rx_ring->rx_lock);
2655 
2656 	/* Rx interrupt enabling for MSI and legacy */
2657 	intr_mask = E1000_READ_REG(hw, E1000_IMS);
2658 	intr_mask |= E1000_IMS_RXT0;
2659 	E1000_WRITE_REG(hw, E1000_IMS, intr_mask);
2660 	E1000_WRITE_FLUSH(hw);
2661 
2662 	/* Trigger a Rx interrupt to check Rx ring */
2663 	E1000_WRITE_REG(hw, E1000_ICS, E1000_IMS_RXT0);
2664 	E1000_WRITE_FLUSH(hw);
2665 
2666 	rw_exit(&adapter->chip_lock);
2667 	return (0);
2668 }
2669 
2670 static int
2671 e1000g_rx_ring_intr_disable(mac_intr_handle_t intrh)
2672 {
2673 	e1000g_rx_ring_t	*rx_ring = (e1000g_rx_ring_t *)intrh;
2674 	struct e1000g 		*adapter = rx_ring->adapter;
2675 	struct e1000_hw 	*hw = &adapter->shared;
2676 
2677 	rw_enter(&adapter->chip_lock, RW_READER);
2678 
2679 	if (adapter->e1000g_state & E1000G_SUSPENDED) {
2680 		rw_exit(&adapter->chip_lock);
2681 		return (0);
2682 	}
2683 	mutex_enter(&rx_ring->rx_lock);
2684 	rx_ring->poll_flag = 1;
2685 	mutex_exit(&rx_ring->rx_lock);
2686 
2687 	/* Rx interrupt disabling for MSI and legacy */
2688 	E1000_WRITE_REG(hw, E1000_IMC, E1000_IMS_RXT0);
2689 	E1000_WRITE_FLUSH(hw);
2690 
2691 	rw_exit(&adapter->chip_lock);
2692 	return (0);
2693 }
2694 
2695 /*
2696  * e1000g_unicst_find - Find the slot for the specified unicast address
2697  */
2698 static int
2699 e1000g_unicst_find(struct e1000g *Adapter, const uint8_t *mac_addr)
2700 {
2701 	int slot;
2702 
2703 	for (slot = 0; slot < Adapter->unicst_total; slot++) {
2704 		if ((Adapter->unicst_addr[slot].mac.set == 1) &&
2705 		    (bcmp(Adapter->unicst_addr[slot].mac.addr,
2706 		    mac_addr, ETHERADDRL) == 0))
2707 				return (slot);
2708 	}
2709 
2710 	return (-1);
2711 }
2712 
2713 /*
2714  * Entry points to add and remove a MAC address to a ring group.
2715  * The caller takes care of adding and removing the MAC addresses
2716  * to the filter via these two routines.
2717  */
2718 
2719 static int
2720 e1000g_addmac(void *arg, const uint8_t *mac_addr)
2721 {
2722 	struct e1000g *Adapter = (struct e1000g *)arg;
2723 	int slot, err;
2724 
2725 	rw_enter(&Adapter->chip_lock, RW_WRITER);
2726 
2727 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
2728 		rw_exit(&Adapter->chip_lock);
2729 		return (ECANCELED);
2730 	}
2731 
2732 	if (e1000g_unicst_find(Adapter, mac_addr) != -1) {
2733 		/* The same address is already in slot */
2734 		rw_exit(&Adapter->chip_lock);
2735 		return (0);
2736 	}
2737 
2738 	if (Adapter->unicst_avail == 0) {
2739 		/* no slots available */
2740 		rw_exit(&Adapter->chip_lock);
2741 		return (ENOSPC);
2742 	}
2743 
2744 	/* Search for a free slot */
2745 	for (slot = 0; slot < Adapter->unicst_total; slot++) {
2746 		if (Adapter->unicst_addr[slot].mac.set == 0)
2747 			break;
2748 	}
2749 	ASSERT(slot < Adapter->unicst_total);
2750 
2751 	err = e1000g_unicst_set(Adapter, mac_addr, slot);
2752 	if (err == 0)
2753 		Adapter->unicst_avail--;
2754 
2755 	rw_exit(&Adapter->chip_lock);
2756 
2757 	return (err);
2758 }
2759 
2760 static int
2761 e1000g_remmac(void *arg, const uint8_t *mac_addr)
2762 {
2763 	struct e1000g *Adapter = (struct e1000g *)arg;
2764 	int slot, err;
2765 
2766 	rw_enter(&Adapter->chip_lock, RW_WRITER);
2767 
2768 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
2769 		rw_exit(&Adapter->chip_lock);
2770 		return (ECANCELED);
2771 	}
2772 
2773 	slot = e1000g_unicst_find(Adapter, mac_addr);
2774 	if (slot == -1) {
2775 		rw_exit(&Adapter->chip_lock);
2776 		return (EINVAL);
2777 	}
2778 
2779 	ASSERT(Adapter->unicst_addr[slot].mac.set);
2780 
2781 	/* Clear this slot */
2782 	err = e1000g_unicst_set(Adapter, NULL, slot);
2783 	if (err == 0)
2784 		Adapter->unicst_avail++;
2785 
2786 	rw_exit(&Adapter->chip_lock);
2787 
2788 	return (err);
2789 }
2790 
2791 static int
2792 e1000g_ring_start(mac_ring_driver_t rh, uint64_t mr_gen_num)
2793 {
2794 	e1000g_rx_ring_t *rx_ring = (e1000g_rx_ring_t *)rh;
2795 
2796 	mutex_enter(&rx_ring->rx_lock);
2797 	rx_ring->ring_gen_num = mr_gen_num;
2798 	mutex_exit(&rx_ring->rx_lock);
2799 	return (0);
2800 }
2801 
2802 /*
2803  * Callback funtion for MAC layer to register all rings.
2804  *
2805  * The hardware supports a single group with currently only one ring
2806  * available.
2807  * Though not offering virtualization ability per se, exposing the
2808  * group/ring still enables the polling and interrupt toggling.
2809  */
2810 void
2811 e1000g_fill_ring(void *arg, mac_ring_type_t rtype, const int grp_index,
2812     const int ring_index, mac_ring_info_t *infop, mac_ring_handle_t rh)
2813 {
2814 	struct e1000g *Adapter = (struct e1000g *)arg;
2815 	e1000g_rx_ring_t *rx_ring = Adapter->rx_ring;
2816 	mac_intr_t *mintr;
2817 
2818 	/*
2819 	 * We advertised only RX group/rings, so the MAC framework shouldn't
2820 	 * ask for any thing else.
2821 	 */
2822 	ASSERT(rtype == MAC_RING_TYPE_RX && grp_index == 0 && ring_index == 0);
2823 
2824 	rx_ring->mrh = rx_ring->mrh_init = rh;
2825 	infop->mri_driver = (mac_ring_driver_t)rx_ring;
2826 	infop->mri_start = e1000g_ring_start;
2827 	infop->mri_stop = NULL;
2828 	infop->mri_poll = e1000g_poll_ring;
2829 
2830 	/* Ring level interrupts */
2831 	mintr = &infop->mri_intr;
2832 	mintr->mi_handle = (mac_intr_handle_t)rx_ring;
2833 	mintr->mi_enable = e1000g_rx_ring_intr_enable;
2834 	mintr->mi_disable = e1000g_rx_ring_intr_disable;
2835 }
2836 
2837 static void
2838 e1000g_fill_group(void *arg, mac_ring_type_t rtype, const int grp_index,
2839     mac_group_info_t *infop, mac_group_handle_t gh)
2840 {
2841 	struct e1000g *Adapter = (struct e1000g *)arg;
2842 	mac_intr_t *mintr;
2843 
2844 	/*
2845 	 * We advertised a single RX ring. Getting a request for anything else
2846 	 * signifies a bug in the MAC framework.
2847 	 */
2848 	ASSERT(rtype == MAC_RING_TYPE_RX && grp_index == 0);
2849 
2850 	Adapter->rx_group = gh;
2851 
2852 	infop->mgi_driver = (mac_group_driver_t)Adapter;
2853 	infop->mgi_start = NULL;
2854 	infop->mgi_stop = NULL;
2855 	infop->mgi_addmac = e1000g_addmac;
2856 	infop->mgi_remmac = e1000g_remmac;
2857 	infop->mgi_count = 1;
2858 
2859 	/* Group level interrupts */
2860 	mintr = &infop->mgi_intr;
2861 	mintr->mi_handle = (mac_intr_handle_t)Adapter;
2862 	mintr->mi_enable = e1000g_rx_group_intr_enable;
2863 	mintr->mi_disable = e1000g_rx_group_intr_disable;
2864 }
2865 
2866 static boolean_t
2867 e1000g_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
2868 {
2869 	struct e1000g *Adapter = (struct e1000g *)arg;
2870 
2871 	switch (cap) {
2872 	case MAC_CAPAB_HCKSUM: {
2873 		uint32_t *txflags = cap_data;
2874 
2875 		if (Adapter->tx_hcksum_enable)
2876 			*txflags = HCKSUM_IPHDRCKSUM |
2877 			    HCKSUM_INET_PARTIAL;
2878 		else
2879 			return (B_FALSE);
2880 		break;
2881 	}
2882 
2883 	case MAC_CAPAB_LSO: {
2884 		mac_capab_lso_t *cap_lso = cap_data;
2885 
2886 		if (Adapter->lso_enable) {
2887 			cap_lso->lso_flags = LSO_TX_BASIC_TCP_IPV4;
2888 			cap_lso->lso_basic_tcp_ipv4.lso_max =
2889 			    E1000_LSO_MAXLEN;
2890 		} else
2891 			return (B_FALSE);
2892 		break;
2893 	}
2894 	case MAC_CAPAB_RINGS: {
2895 		mac_capab_rings_t *cap_rings = cap_data;
2896 
2897 		/* No TX rings exposed yet */
2898 		if (cap_rings->mr_type != MAC_RING_TYPE_RX)
2899 			return (B_FALSE);
2900 
2901 		cap_rings->mr_group_type = MAC_GROUP_TYPE_STATIC;
2902 		cap_rings->mr_rnum = 1;
2903 		cap_rings->mr_gnum = 1;
2904 		cap_rings->mr_rget = e1000g_fill_ring;
2905 		cap_rings->mr_gget = e1000g_fill_group;
2906 		break;
2907 	}
2908 	default:
2909 		return (B_FALSE);
2910 	}
2911 	return (B_TRUE);
2912 }
2913 
2914 static boolean_t
2915 e1000g_param_locked(mac_prop_id_t pr_num)
2916 {
2917 	/*
2918 	 * All en_* parameters are locked (read-only) while
2919 	 * the device is in any sort of loopback mode ...
2920 	 */
2921 	switch (pr_num) {
2922 		case MAC_PROP_EN_1000FDX_CAP:
2923 		case MAC_PROP_EN_1000HDX_CAP:
2924 		case MAC_PROP_EN_100FDX_CAP:
2925 		case MAC_PROP_EN_100HDX_CAP:
2926 		case MAC_PROP_EN_10FDX_CAP:
2927 		case MAC_PROP_EN_10HDX_CAP:
2928 		case MAC_PROP_AUTONEG:
2929 		case MAC_PROP_FLOWCTRL:
2930 			return (B_TRUE);
2931 	}
2932 	return (B_FALSE);
2933 }
2934 
2935 /*
2936  * callback function for set/get of properties
2937  */
2938 static int
2939 e1000g_m_setprop(void *arg, const char *pr_name, mac_prop_id_t pr_num,
2940     uint_t pr_valsize, const void *pr_val)
2941 {
2942 	struct e1000g *Adapter = arg;
2943 	struct e1000_mac_info *mac = &Adapter->shared.mac;
2944 	struct e1000_phy_info *phy = &Adapter->shared.phy;
2945 	struct e1000_fc_info *fc = &Adapter->shared.fc;
2946 	int err = 0;
2947 	link_flowctrl_t flowctrl;
2948 	uint32_t cur_mtu, new_mtu;
2949 	uint64_t tmp = 0;
2950 
2951 	rw_enter(&Adapter->chip_lock, RW_WRITER);
2952 
2953 	if (Adapter->e1000g_state & E1000G_SUSPENDED) {
2954 		rw_exit(&Adapter->chip_lock);
2955 		return (ECANCELED);
2956 	}
2957 
2958 	if (Adapter->loopback_mode != E1000G_LB_NONE &&
2959 	    e1000g_param_locked(pr_num)) {
2960 		/*
2961 		 * All en_* parameters are locked (read-only)
2962 		 * while the device is in any sort of loopback mode.
2963 		 */
2964 		rw_exit(&Adapter->chip_lock);
2965 		return (EBUSY);
2966 	}
2967 
2968 	switch (pr_num) {
2969 		case MAC_PROP_EN_1000FDX_CAP:
2970 			Adapter->param_en_1000fdx = *(uint8_t *)pr_val;
2971 			Adapter->param_adv_1000fdx = *(uint8_t *)pr_val;
2972 			goto reset;
2973 		case MAC_PROP_EN_100FDX_CAP:
2974 			Adapter->param_en_100fdx = *(uint8_t *)pr_val;
2975 			Adapter->param_adv_100fdx = *(uint8_t *)pr_val;
2976 			goto reset;
2977 		case MAC_PROP_EN_100HDX_CAP:
2978 			Adapter->param_en_100hdx = *(uint8_t *)pr_val;
2979 			Adapter->param_adv_100hdx = *(uint8_t *)pr_val;
2980 			goto reset;
2981 		case MAC_PROP_EN_10FDX_CAP:
2982 			Adapter->param_en_10fdx = *(uint8_t *)pr_val;
2983 			Adapter->param_adv_10fdx = *(uint8_t *)pr_val;
2984 			goto reset;
2985 		case MAC_PROP_EN_10HDX_CAP:
2986 			Adapter->param_en_10hdx = *(uint8_t *)pr_val;
2987 			Adapter->param_adv_10hdx = *(uint8_t *)pr_val;
2988 			goto reset;
2989 		case MAC_PROP_AUTONEG:
2990 			Adapter->param_adv_autoneg = *(uint8_t *)pr_val;
2991 			goto reset;
2992 		case MAC_PROP_FLOWCTRL:
2993 			fc->send_xon = B_TRUE;
2994 			bcopy(pr_val, &flowctrl, sizeof (flowctrl));
2995 
2996 			switch (flowctrl) {
2997 			default:
2998 				err = EINVAL;
2999 				break;
3000 			case LINK_FLOWCTRL_NONE:
3001 				fc->requested_mode = e1000_fc_none;
3002 				break;
3003 			case LINK_FLOWCTRL_RX:
3004 				fc->requested_mode = e1000_fc_rx_pause;
3005 				break;
3006 			case LINK_FLOWCTRL_TX:
3007 				fc->requested_mode = e1000_fc_tx_pause;
3008 				break;
3009 			case LINK_FLOWCTRL_BI:
3010 				fc->requested_mode = e1000_fc_full;
3011 				break;
3012 			}
3013 reset:
3014 			if (err == 0) {
3015 				if (e1000g_reset_link(Adapter) != DDI_SUCCESS)
3016 					err = EINVAL;
3017 			}
3018 			break;
3019 		case MAC_PROP_ADV_1000FDX_CAP:
3020 		case MAC_PROP_ADV_1000HDX_CAP:
3021 		case MAC_PROP_ADV_100FDX_CAP:
3022 		case MAC_PROP_ADV_100HDX_CAP:
3023 		case MAC_PROP_ADV_10FDX_CAP:
3024 		case MAC_PROP_ADV_10HDX_CAP:
3025 		case MAC_PROP_EN_1000HDX_CAP:
3026 		case MAC_PROP_STATUS:
3027 		case MAC_PROP_SPEED:
3028 		case MAC_PROP_DUPLEX:
3029 			err = ENOTSUP; /* read-only prop. Can't set this. */
3030 			break;
3031 		case MAC_PROP_MTU:
3032 			cur_mtu = Adapter->default_mtu;
3033 			bcopy(pr_val, &new_mtu, sizeof (new_mtu));
3034 			if (new_mtu == cur_mtu) {
3035 				err = 0;
3036 				break;
3037 			}
3038 
3039 			tmp = new_mtu + sizeof (struct ether_vlan_header) +
3040 			    ETHERFCSL;
3041 			if ((tmp < DEFAULT_FRAME_SIZE) ||
3042 			    (tmp > MAXIMUM_FRAME_SIZE)) {
3043 				err = EINVAL;
3044 				break;
3045 			}
3046 
3047 			/* ich8 does not support jumbo frames */
3048 			if ((mac->type == e1000_ich8lan) &&
3049 			    (tmp > DEFAULT_FRAME_SIZE)) {
3050 				err = EINVAL;
3051 				break;
3052 			}
3053 			/* ich9 does not do jumbo frames on one phy type */
3054 			if ((mac->type == e1000_ich9lan) &&
3055 			    (phy->type == e1000_phy_ife) &&
3056 			    (tmp > DEFAULT_FRAME_SIZE)) {
3057 				err = EINVAL;
3058 				break;
3059 			}
3060 			if (Adapter->e1000g_state & E1000G_STARTED) {
3061 				err = EBUSY;
3062 				break;
3063 			}
3064 
3065 			err = mac_maxsdu_update(Adapter->mh, new_mtu);
3066 			if (err == 0) {
3067 				Adapter->max_frame_size = (uint32_t)tmp;
3068 				Adapter->default_mtu = new_mtu;
3069 				e1000g_set_bufsize(Adapter);
3070 			}
3071 			break;
3072 		case MAC_PROP_PRIVATE:
3073 			err = e1000g_set_priv_prop(Adapter, pr_name,
3074 			    pr_valsize, pr_val);
3075 			break;
3076 		default:
3077 			err = ENOTSUP;
3078 			break;
3079 	}
3080 	rw_exit(&Adapter->chip_lock);
3081 	return (err);
3082 }
3083 
3084 static int
3085 e1000g_m_getprop(void *arg, const char *pr_name, mac_prop_id_t pr_num,
3086     uint_t pr_flags, uint_t pr_valsize, void *pr_val, uint_t *perm)
3087 {
3088 	struct e1000g *Adapter = arg;
3089 	struct e1000_fc_info *fc = &Adapter->shared.fc;
3090 	int err = 0;
3091 	link_flowctrl_t flowctrl;
3092 	uint64_t tmp = 0;
3093 
3094 	if (pr_valsize == 0)
3095 		return (EINVAL);
3096 
3097 	*perm = MAC_PROP_PERM_RW;
3098 
3099 	bzero(pr_val, pr_valsize);
3100 	if ((pr_flags & MAC_PROP_DEFAULT) && (pr_num != MAC_PROP_PRIVATE)) {
3101 		return (e1000g_get_def_val(Adapter, pr_num,
3102 		    pr_valsize, pr_val));
3103 	}
3104 
3105 	switch (pr_num) {
3106 		case MAC_PROP_DUPLEX:
3107 			*perm = MAC_PROP_PERM_READ;
3108 			if (pr_valsize >= sizeof (link_duplex_t)) {
3109 				bcopy(&Adapter->link_duplex, pr_val,
3110 				    sizeof (link_duplex_t));
3111 			} else
3112 				err = EINVAL;
3113 			break;
3114 		case MAC_PROP_SPEED:
3115 			*perm = MAC_PROP_PERM_READ;
3116 			if (pr_valsize >= sizeof (uint64_t)) {
3117 				tmp = Adapter->link_speed * 1000000ull;
3118 				bcopy(&tmp, pr_val, sizeof (tmp));
3119 			} else
3120 				err = EINVAL;
3121 			break;
3122 		case MAC_PROP_AUTONEG:
3123 			*(uint8_t *)pr_val = Adapter->param_adv_autoneg;
3124 			break;
3125 		case MAC_PROP_FLOWCTRL:
3126 			if (pr_valsize >= sizeof (link_flowctrl_t)) {
3127 				switch (fc->current_mode) {
3128 					case e1000_fc_none:
3129 						flowctrl = LINK_FLOWCTRL_NONE;
3130 						break;
3131 					case e1000_fc_rx_pause:
3132 						flowctrl = LINK_FLOWCTRL_RX;
3133 						break;
3134 					case e1000_fc_tx_pause:
3135 						flowctrl = LINK_FLOWCTRL_TX;
3136 						break;
3137 					case e1000_fc_full:
3138 						flowctrl = LINK_FLOWCTRL_BI;
3139 						break;
3140 				}
3141 				bcopy(&flowctrl, pr_val, sizeof (flowctrl));
3142 			} else
3143 				err = EINVAL;
3144 			break;
3145 		case MAC_PROP_ADV_1000FDX_CAP:
3146 			*perm = MAC_PROP_PERM_READ;
3147 			*(uint8_t *)pr_val = Adapter->param_adv_1000fdx;
3148 			break;
3149 		case MAC_PROP_EN_1000FDX_CAP:
3150 			*(uint8_t *)pr_val = Adapter->param_en_1000fdx;
3151 			break;
3152 		case MAC_PROP_ADV_1000HDX_CAP:
3153 			*perm = MAC_PROP_PERM_READ;
3154 			*(uint8_t *)pr_val = Adapter->param_adv_1000hdx;
3155 			break;
3156 		case MAC_PROP_EN_1000HDX_CAP:
3157 			*perm = MAC_PROP_PERM_READ;
3158 			*(uint8_t *)pr_val = Adapter->param_en_1000hdx;
3159 			break;
3160 		case MAC_PROP_ADV_100FDX_CAP:
3161 			*perm = MAC_PROP_PERM_READ;
3162 			*(uint8_t *)pr_val = Adapter->param_adv_100fdx;
3163 			break;
3164 		case MAC_PROP_EN_100FDX_CAP:
3165 			*(uint8_t *)pr_val = Adapter->param_en_100fdx;
3166 			break;
3167 		case MAC_PROP_ADV_100HDX_CAP:
3168 			*perm = MAC_PROP_PERM_READ;
3169 			*(uint8_t *)pr_val = Adapter->param_adv_100hdx;
3170 			break;
3171 		case MAC_PROP_EN_100HDX_CAP:
3172 			*(uint8_t *)pr_val = Adapter->param_en_100hdx;
3173 			break;
3174 		case MAC_PROP_ADV_10FDX_CAP:
3175 			*perm = MAC_PROP_PERM_READ;
3176 			*(uint8_t *)pr_val = Adapter->param_adv_10fdx;
3177 			break;
3178 		case MAC_PROP_EN_10FDX_CAP:
3179 			*(uint8_t *)pr_val = Adapter->param_en_10fdx;
3180 			break;
3181 		case MAC_PROP_ADV_10HDX_CAP:
3182 			*perm = MAC_PROP_PERM_READ;
3183 			*(uint8_t *)pr_val = Adapter->param_adv_10hdx;
3184 			break;
3185 		case MAC_PROP_EN_10HDX_CAP:
3186 			*(uint8_t *)pr_val = Adapter->param_en_10hdx;
3187 			break;
3188 		case MAC_PROP_ADV_100T4_CAP:
3189 		case MAC_PROP_EN_100T4_CAP:
3190 			*perm = MAC_PROP_PERM_READ;
3191 			*(uint8_t *)pr_val = Adapter->param_adv_100t4;
3192 			break;
3193 		case MAC_PROP_PRIVATE:
3194 			err = e1000g_get_priv_prop(Adapter, pr_name,
3195 			    pr_flags, pr_valsize, pr_val, perm);
3196 			break;
3197 		default:
3198 			err = ENOTSUP;
3199 			break;
3200 	}
3201 	return (err);
3202 }
3203 
3204 /* ARGSUSED2 */
3205 static int
3206 e1000g_set_priv_prop(struct e1000g *Adapter, const char *pr_name,
3207     uint_t pr_valsize, const void *pr_val)
3208 {
3209 	int err = 0;
3210 	long result;
3211 	struct e1000_hw *hw = &Adapter->shared;
3212 
3213 	if (strcmp(pr_name, "_tx_bcopy_threshold") == 0) {
3214 		if (pr_val == NULL) {
3215 			err = EINVAL;
3216 			return (err);
3217 		}
3218 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
3219 		if (result < MIN_TX_BCOPY_THRESHOLD ||
3220 		    result > MAX_TX_BCOPY_THRESHOLD)
3221 			err = EINVAL;
3222 		else {
3223 			Adapter->tx_bcopy_thresh = (uint32_t)result;
3224 		}
3225 		return (err);
3226 	}
3227 	if (strcmp(pr_name, "_tx_interrupt_enable") == 0) {
3228 		if (pr_val == NULL) {
3229 			err = EINVAL;
3230 			return (err);
3231 		}
3232 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
3233 		if (result < 0 || result > 1)
3234 			err = EINVAL;
3235 		else {
3236 			Adapter->tx_intr_enable = (result == 1) ?
3237 			    B_TRUE: B_FALSE;
3238 			if (Adapter->tx_intr_enable)
3239 				e1000g_mask_tx_interrupt(Adapter);
3240 			else
3241 				e1000g_clear_tx_interrupt(Adapter);
3242 			if (e1000g_check_acc_handle(
3243 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
3244 				ddi_fm_service_impact(Adapter->dip,
3245 				    DDI_SERVICE_DEGRADED);
3246 		}
3247 		return (err);
3248 	}
3249 	if (strcmp(pr_name, "_tx_intr_delay") == 0) {
3250 		if (pr_val == NULL) {
3251 			err = EINVAL;
3252 			return (err);
3253 		}
3254 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
3255 		if (result < MIN_TX_INTR_DELAY ||
3256 		    result > MAX_TX_INTR_DELAY)
3257 			err = EINVAL;
3258 		else {
3259 			Adapter->tx_intr_delay = (uint32_t)result;
3260 			E1000_WRITE_REG(hw, E1000_TIDV, Adapter->tx_intr_delay);
3261 			if (e1000g_check_acc_handle(
3262 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
3263 				ddi_fm_service_impact(Adapter->dip,
3264 				    DDI_SERVICE_DEGRADED);
3265 		}
3266 		return (err);
3267 	}
3268 	if (strcmp(pr_name, "_tx_intr_abs_delay") == 0) {
3269 		if (pr_val == NULL) {
3270 			err = EINVAL;
3271 			return (err);
3272 		}
3273 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
3274 		if (result < MIN_TX_INTR_ABS_DELAY ||
3275 		    result > MAX_TX_INTR_ABS_DELAY)
3276 			err = EINVAL;
3277 		else {
3278 			Adapter->tx_intr_abs_delay = (uint32_t)result;
3279 			E1000_WRITE_REG(hw, E1000_TADV,
3280 			    Adapter->tx_intr_abs_delay);
3281 			if (e1000g_check_acc_handle(
3282 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
3283 				ddi_fm_service_impact(Adapter->dip,
3284 				    DDI_SERVICE_DEGRADED);
3285 		}
3286 		return (err);
3287 	}
3288 	if (strcmp(pr_name, "_rx_bcopy_threshold") == 0) {
3289 		if (pr_val == NULL) {
3290 			err = EINVAL;
3291 			return (err);
3292 		}
3293 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
3294 		if (result < MIN_RX_BCOPY_THRESHOLD ||
3295 		    result > MAX_RX_BCOPY_THRESHOLD)
3296 			err = EINVAL;
3297 		else
3298 			Adapter->rx_bcopy_thresh = (uint32_t)result;
3299 		return (err);
3300 	}
3301 	if (strcmp(pr_name, "_max_num_rcv_packets") == 0) {
3302 		if (pr_val == NULL) {
3303 			err = EINVAL;
3304 			return (err);
3305 		}
3306 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
3307 		if (result < MIN_RX_LIMIT_ON_INTR ||
3308 		    result > MAX_RX_LIMIT_ON_INTR)
3309 			err = EINVAL;
3310 		else
3311 			Adapter->rx_limit_onintr = (uint32_t)result;
3312 		return (err);
3313 	}
3314 	if (strcmp(pr_name, "_rx_intr_delay") == 0) {
3315 		if (pr_val == NULL) {
3316 			err = EINVAL;
3317 			return (err);
3318 		}
3319 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
3320 		if (result < MIN_RX_INTR_DELAY ||
3321 		    result > MAX_RX_INTR_DELAY)
3322 			err = EINVAL;
3323 		else {
3324 			Adapter->rx_intr_delay = (uint32_t)result;
3325 			E1000_WRITE_REG(hw, E1000_RDTR, Adapter->rx_intr_delay);
3326 			if (e1000g_check_acc_handle(
3327 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
3328 				ddi_fm_service_impact(Adapter->dip,
3329 				    DDI_SERVICE_DEGRADED);
3330 		}
3331 		return (err);
3332 	}
3333 	if (strcmp(pr_name, "_rx_intr_abs_delay") == 0) {
3334 		if (pr_val == NULL) {
3335 			err = EINVAL;
3336 			return (err);
3337 		}
3338 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
3339 		if (result < MIN_RX_INTR_ABS_DELAY ||
3340 		    result > MAX_RX_INTR_ABS_DELAY)
3341 			err = EINVAL;
3342 		else {
3343 			Adapter->rx_intr_abs_delay = (uint32_t)result;
3344 			E1000_WRITE_REG(hw, E1000_RADV,
3345 			    Adapter->rx_intr_abs_delay);
3346 			if (e1000g_check_acc_handle(
3347 			    Adapter->osdep.reg_handle) != DDI_FM_OK)
3348 				ddi_fm_service_impact(Adapter->dip,
3349 				    DDI_SERVICE_DEGRADED);
3350 		}
3351 		return (err);
3352 	}
3353 	if (strcmp(pr_name, "_intr_throttling_rate") == 0) {
3354 		if (pr_val == NULL) {
3355 			err = EINVAL;
3356 			return (err);
3357 		}
3358 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
3359 		if (result < MIN_INTR_THROTTLING ||
3360 		    result > MAX_INTR_THROTTLING)
3361 			err = EINVAL;
3362 		else {
3363 			if (hw->mac.type >= e1000_82540) {
3364 				Adapter->intr_throttling_rate =
3365 				    (uint32_t)result;
3366 				E1000_WRITE_REG(hw, E1000_ITR,
3367 				    Adapter->intr_throttling_rate);
3368 				if (e1000g_check_acc_handle(
3369 				    Adapter->osdep.reg_handle) != DDI_FM_OK)
3370 					ddi_fm_service_impact(Adapter->dip,
3371 					    DDI_SERVICE_DEGRADED);
3372 			} else
3373 				err = EINVAL;
3374 		}
3375 		return (err);
3376 	}
3377 	if (strcmp(pr_name, "_intr_adaptive") == 0) {
3378 		if (pr_val == NULL) {
3379 			err = EINVAL;
3380 			return (err);
3381 		}
3382 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
3383 		if (result < 0 || result > 1)
3384 			err = EINVAL;
3385 		else {
3386 			if (hw->mac.type >= e1000_82540) {
3387 				Adapter->intr_adaptive = (result == 1) ?
3388 				    B_TRUE : B_FALSE;
3389 			} else {
3390 				err = EINVAL;
3391 			}
3392 		}
3393 		return (err);
3394 	}
3395 	return (ENOTSUP);
3396 }
3397 
3398 static int
3399 e1000g_get_priv_prop(struct e1000g *Adapter, const char *pr_name,
3400     uint_t pr_flags, uint_t pr_valsize, void *pr_val, uint_t *perm)
3401 {
3402 	int err = ENOTSUP;
3403 	boolean_t is_default = (pr_flags & MAC_PROP_DEFAULT);
3404 	int value;
3405 
3406 	if (strcmp(pr_name, "_adv_pause_cap") == 0) {
3407 		*perm = MAC_PROP_PERM_READ;
3408 		if (is_default)
3409 			goto done;
3410 		value = Adapter->param_adv_pause;
3411 		err = 0;
3412 		goto done;
3413 	}
3414 	if (strcmp(pr_name, "_adv_asym_pause_cap") == 0) {
3415 		*perm = MAC_PROP_PERM_READ;
3416 		if (is_default)
3417 			goto done;
3418 		value = Adapter->param_adv_asym_pause;
3419 		err = 0;
3420 		goto done;
3421 	}
3422 	if (strcmp(pr_name, "_tx_bcopy_threshold") == 0) {
3423 		value = (is_default ? DEFAULT_TX_BCOPY_THRESHOLD :
3424 		    Adapter->tx_bcopy_thresh);
3425 		err = 0;
3426 		goto done;
3427 	}
3428 	if (strcmp(pr_name, "_tx_interrupt_enable") == 0) {
3429 		value = (is_default ? DEFAULT_TX_INTR_ENABLE :
3430 		    Adapter->tx_intr_enable);
3431 		err = 0;
3432 		goto done;
3433 	}
3434 	if (strcmp(pr_name, "_tx_intr_delay") == 0) {
3435 		value = (is_default ? DEFAULT_TX_INTR_DELAY :
3436 		    Adapter->tx_intr_delay);
3437 		err = 0;
3438 		goto done;
3439 	}
3440 	if (strcmp(pr_name, "_tx_intr_abs_delay") == 0) {
3441 		value = (is_default ? DEFAULT_TX_INTR_ABS_DELAY :
3442 		    Adapter->tx_intr_abs_delay);
3443 		err = 0;
3444 		goto done;
3445 	}
3446 	if (strcmp(pr_name, "_rx_bcopy_threshold") == 0) {
3447 		value = (is_default ? DEFAULT_RX_BCOPY_THRESHOLD :
3448 		    Adapter->rx_bcopy_thresh);
3449 		err = 0;
3450 		goto done;
3451 	}
3452 	if (strcmp(pr_name, "_max_num_rcv_packets") == 0) {
3453 		value = (is_default ? DEFAULT_RX_LIMIT_ON_INTR :
3454 		    Adapter->rx_limit_onintr);
3455 		err = 0;
3456 		goto done;
3457 	}
3458 	if (strcmp(pr_name, "_rx_intr_delay") == 0) {
3459 		value = (is_default ? DEFAULT_RX_INTR_DELAY :
3460 		    Adapter->rx_intr_delay);
3461 		err = 0;
3462 		goto done;
3463 	}
3464 	if (strcmp(pr_name, "_rx_intr_abs_delay") == 0) {
3465 		value = (is_default ? DEFAULT_RX_INTR_ABS_DELAY :
3466 		    Adapter->rx_intr_abs_delay);
3467 		err = 0;
3468 		goto done;
3469 	}
3470 	if (strcmp(pr_name, "_intr_throttling_rate") == 0) {
3471 		value = (is_default ? DEFAULT_INTR_THROTTLING :
3472 		    Adapter->intr_throttling_rate);
3473 		err = 0;
3474 		goto done;
3475 	}
3476 	if (strcmp(pr_name, "_intr_adaptive") == 0) {
3477 		value = (is_default ? 1 : Adapter->intr_adaptive);
3478 		err = 0;
3479 		goto done;
3480 	}
3481 done:
3482 	if (err == 0) {
3483 		(void) snprintf(pr_val, pr_valsize, "%d", value);
3484 	}
3485 	return (err);
3486 }
3487 
3488 /*
3489  * e1000g_get_conf - get configurations set in e1000g.conf
3490  * This routine gets user-configured values out of the configuration
3491  * file e1000g.conf.
3492  *
3493  * For each configurable value, there is a minimum, a maximum, and a
3494  * default.
3495  * If user does not configure a value, use the default.
3496  * If user configures below the minimum, use the minumum.
3497  * If user configures above the maximum, use the maxumum.
3498  */
3499 static void
3500 e1000g_get_conf(struct e1000g *Adapter)
3501 {
3502 	struct e1000_hw *hw = &Adapter->shared;
3503 	boolean_t tbi_compatibility = B_FALSE;
3504 
3505 	/*
3506 	 * get each configurable property from e1000g.conf
3507 	 */
3508 
3509 	/*
3510 	 * NumTxDescriptors
3511 	 */
3512 	Adapter->tx_desc_num =
3513 	    e1000g_get_prop(Adapter, "NumTxDescriptors",
3514 	    MIN_NUM_TX_DESCRIPTOR, MAX_NUM_TX_DESCRIPTOR,
3515 	    DEFAULT_NUM_TX_DESCRIPTOR);
3516 
3517 	/*
3518 	 * NumRxDescriptors
3519 	 */
3520 	Adapter->rx_desc_num =
3521 	    e1000g_get_prop(Adapter, "NumRxDescriptors",
3522 	    MIN_NUM_RX_DESCRIPTOR, MAX_NUM_RX_DESCRIPTOR,
3523 	    DEFAULT_NUM_RX_DESCRIPTOR);
3524 
3525 	/*
3526 	 * NumRxFreeList
3527 	 */
3528 	Adapter->rx_freelist_num =
3529 	    e1000g_get_prop(Adapter, "NumRxFreeList",
3530 	    MIN_NUM_RX_FREELIST, MAX_NUM_RX_FREELIST,
3531 	    DEFAULT_NUM_RX_FREELIST);
3532 
3533 	/*
3534 	 * NumTxPacketList
3535 	 */
3536 	Adapter->tx_freelist_num =
3537 	    e1000g_get_prop(Adapter, "NumTxPacketList",
3538 	    MIN_NUM_TX_FREELIST, MAX_NUM_TX_FREELIST,
3539 	    DEFAULT_NUM_TX_FREELIST);
3540 
3541 	/*
3542 	 * FlowControl
3543 	 */
3544 	hw->fc.send_xon = B_TRUE;
3545 	hw->fc.requested_mode =
3546 	    e1000g_get_prop(Adapter, "FlowControl",
3547 	    e1000_fc_none, 4, DEFAULT_FLOW_CONTROL);
3548 	/* 4 is the setting that says "let the eeprom decide" */
3549 	if (hw->fc.requested_mode == 4)
3550 		hw->fc.requested_mode = e1000_fc_default;
3551 
3552 	/*
3553 	 * Max Num Receive Packets on Interrupt
3554 	 */
3555 	Adapter->rx_limit_onintr =
3556 	    e1000g_get_prop(Adapter, "MaxNumReceivePackets",
3557 	    MIN_RX_LIMIT_ON_INTR, MAX_RX_LIMIT_ON_INTR,
3558 	    DEFAULT_RX_LIMIT_ON_INTR);
3559 
3560 	/*
3561 	 * PHY master slave setting
3562 	 */
3563 	hw->phy.ms_type =
3564 	    e1000g_get_prop(Adapter, "SetMasterSlave",
3565 	    e1000_ms_hw_default, e1000_ms_auto,
3566 	    e1000_ms_hw_default);
3567 
3568 	/*
3569 	 * Parameter which controls TBI mode workaround, which is only
3570 	 * needed on certain switches such as Cisco 6500/Foundry
3571 	 */
3572 	tbi_compatibility =
3573 	    e1000g_get_prop(Adapter, "TbiCompatibilityEnable",
3574 	    0, 1, DEFAULT_TBI_COMPAT_ENABLE);
3575 	e1000_set_tbi_compatibility_82543(hw, tbi_compatibility);
3576 
3577 	/*
3578 	 * MSI Enable
3579 	 */
3580 	Adapter->msi_enable =
3581 	    e1000g_get_prop(Adapter, "MSIEnable",
3582 	    0, 1, DEFAULT_MSI_ENABLE);
3583 
3584 	/*
3585 	 * Interrupt Throttling Rate
3586 	 */
3587 	Adapter->intr_throttling_rate =
3588 	    e1000g_get_prop(Adapter, "intr_throttling_rate",
3589 	    MIN_INTR_THROTTLING, MAX_INTR_THROTTLING,
3590 	    DEFAULT_INTR_THROTTLING);
3591 
3592 	/*
3593 	 * Adaptive Interrupt Blanking Enable/Disable
3594 	 * It is enabled by default
3595 	 */
3596 	Adapter->intr_adaptive =
3597 	    (e1000g_get_prop(Adapter, "intr_adaptive", 0, 1, 1) == 1) ?
3598 	    B_TRUE : B_FALSE;
3599 
3600 	/*
3601 	 * Hardware checksum enable/disable parameter
3602 	 */
3603 	Adapter->tx_hcksum_enable =
3604 	    e1000g_get_prop(Adapter, "tx_hcksum_enable",
3605 	    0, 1, DEFAULT_TX_HCKSUM_ENABLE);
3606 	/*
3607 	 * Checksum on/off selection via global parameters.
3608 	 *
3609 	 * If the chip is flagged as not capable of (correctly)
3610 	 * handling checksumming, we don't enable it on either
3611 	 * Rx or Tx side.  Otherwise, we take this chip's settings
3612 	 * from the patchable global defaults.
3613 	 *
3614 	 * We advertise our capabilities only if TX offload is
3615 	 * enabled.  On receive, the stack will accept checksummed
3616 	 * packets anyway, even if we haven't said we can deliver
3617 	 * them.
3618 	 */
3619 	switch (hw->mac.type) {
3620 		case e1000_82540:
3621 		case e1000_82544:
3622 		case e1000_82545:
3623 		case e1000_82545_rev_3:
3624 		case e1000_82546:
3625 		case e1000_82546_rev_3:
3626 		case e1000_82571:
3627 		case e1000_82572:
3628 		case e1000_82573:
3629 		case e1000_80003es2lan:
3630 			break;
3631 		/*
3632 		 * For the following Intel PRO/1000 chipsets, we have not
3633 		 * tested the hardware checksum offload capability, so we
3634 		 * disable the capability for them.
3635 		 *	e1000_82542,
3636 		 *	e1000_82543,
3637 		 *	e1000_82541,
3638 		 *	e1000_82541_rev_2,
3639 		 *	e1000_82547,
3640 		 *	e1000_82547_rev_2,
3641 		 */
3642 		default:
3643 			Adapter->tx_hcksum_enable = B_FALSE;
3644 	}
3645 
3646 	/*
3647 	 * Large Send Offloading(LSO) Enable/Disable
3648 	 * If the tx hardware checksum is not enabled, LSO should be
3649 	 * disabled.
3650 	 */
3651 	Adapter->lso_enable =
3652 	    e1000g_get_prop(Adapter, "lso_enable",
3653 	    0, 1, DEFAULT_LSO_ENABLE);
3654 
3655 	switch (hw->mac.type) {
3656 		case e1000_82546:
3657 		case e1000_82546_rev_3:
3658 			if (Adapter->lso_enable)
3659 				Adapter->lso_premature_issue = B_TRUE;
3660 			/* FALLTHRU */
3661 		case e1000_82571:
3662 		case e1000_82572:
3663 		case e1000_82573:
3664 		case e1000_80003es2lan:
3665 			break;
3666 		default:
3667 			Adapter->lso_enable = B_FALSE;
3668 	}
3669 
3670 	if (!Adapter->tx_hcksum_enable) {
3671 		Adapter->lso_premature_issue = B_FALSE;
3672 		Adapter->lso_enable = B_FALSE;
3673 	}
3674 
3675 	/*
3676 	 * If mem_workaround_82546 is enabled, the rx buffer allocated by
3677 	 * e1000_82545, e1000_82546 and e1000_82546_rev_3
3678 	 * will not cross 64k boundary.
3679 	 */
3680 	Adapter->mem_workaround_82546 =
3681 	    e1000g_get_prop(Adapter, "mem_workaround_82546",
3682 	    0, 1, DEFAULT_MEM_WORKAROUND_82546);
3683 }
3684 
3685 /*
3686  * e1000g_get_prop - routine to read properties
3687  *
3688  * Get a user-configure property value out of the configuration
3689  * file e1000g.conf.
3690  *
3691  * Caller provides name of the property, a default value, a minimum
3692  * value, and a maximum value.
3693  *
3694  * Return configured value of the property, with default, minimum and
3695  * maximum properly applied.
3696  */
3697 static int
3698 e1000g_get_prop(struct e1000g *Adapter,	/* point to per-adapter structure */
3699     char *propname,		/* name of the property */
3700     int minval,			/* minimum acceptable value */
3701     int maxval,			/* maximim acceptable value */
3702     int defval)			/* default value */
3703 {
3704 	int propval;		/* value returned for requested property */
3705 	int *props;		/* point to array of properties returned */
3706 	uint_t nprops;		/* number of property value returned */
3707 
3708 	/*
3709 	 * get the array of properties from the config file
3710 	 */
3711 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, Adapter->dip,
3712 	    DDI_PROP_DONTPASS, propname, &props, &nprops) == DDI_PROP_SUCCESS) {
3713 		/* got some properties, test if we got enough */
3714 		if (Adapter->instance < nprops) {
3715 			propval = props[Adapter->instance];
3716 		} else {
3717 			/* not enough properties configured */
3718 			propval = defval;
3719 			E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
3720 			    "Not Enough %s values found in e1000g.conf"
3721 			    " - set to %d\n",
3722 			    propname, propval);
3723 		}
3724 
3725 		/* free memory allocated for properties */
3726 		ddi_prop_free(props);
3727 
3728 	} else {
3729 		propval = defval;
3730 	}
3731 
3732 	/*
3733 	 * enforce limits
3734 	 */
3735 	if (propval > maxval) {
3736 		propval = maxval;
3737 		E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
3738 		    "Too High %s value in e1000g.conf - set to %d\n",
3739 		    propname, propval);
3740 	}
3741 
3742 	if (propval < minval) {
3743 		propval = minval;
3744 		E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
3745 		    "Too Low %s value in e1000g.conf - set to %d\n",
3746 		    propname, propval);
3747 	}
3748 
3749 	return (propval);
3750 }
3751 
3752 static boolean_t
3753 e1000g_link_check(struct e1000g *Adapter)
3754 {
3755 	uint16_t speed, duplex, phydata;
3756 	boolean_t link_changed = B_FALSE;
3757 	struct e1000_hw *hw;
3758 	uint32_t reg_tarc;
3759 
3760 	hw = &Adapter->shared;
3761 
3762 	if (e1000g_link_up(Adapter)) {
3763 		/*
3764 		 * The Link is up, check whether it was marked as down earlier
3765 		 */
3766 		if (Adapter->link_state != LINK_STATE_UP) {
3767 			(void) e1000_get_speed_and_duplex(hw, &speed, &duplex);
3768 			Adapter->link_speed = speed;
3769 			Adapter->link_duplex = duplex;
3770 			Adapter->link_state = LINK_STATE_UP;
3771 			link_changed = B_TRUE;
3772 
3773 			if (Adapter->link_speed == SPEED_1000)
3774 				Adapter->stall_threshold = TX_STALL_TIME_2S;
3775 			else
3776 				Adapter->stall_threshold = TX_STALL_TIME_8S;
3777 
3778 			Adapter->tx_link_down_timeout = 0;
3779 
3780 			if ((hw->mac.type == e1000_82571) ||
3781 			    (hw->mac.type == e1000_82572)) {
3782 				reg_tarc = E1000_READ_REG(hw, E1000_TARC(0));
3783 				if (speed == SPEED_1000)
3784 					reg_tarc |= (1 << 21);
3785 				else
3786 					reg_tarc &= ~(1 << 21);
3787 				E1000_WRITE_REG(hw, E1000_TARC(0), reg_tarc);
3788 			}
3789 		}
3790 		Adapter->smartspeed = 0;
3791 	} else {
3792 		if (Adapter->link_state != LINK_STATE_DOWN) {
3793 			Adapter->link_speed = 0;
3794 			Adapter->link_duplex = 0;
3795 			Adapter->link_state = LINK_STATE_DOWN;
3796 			link_changed = B_TRUE;
3797 
3798 			/*
3799 			 * SmartSpeed workaround for Tabor/TanaX, When the
3800 			 * driver loses link disable auto master/slave
3801 			 * resolution.
3802 			 */
3803 			if (hw->phy.type == e1000_phy_igp) {
3804 				(void) e1000_read_phy_reg(hw,
3805 				    PHY_1000T_CTRL, &phydata);
3806 				phydata |= CR_1000T_MS_ENABLE;
3807 				(void) e1000_write_phy_reg(hw,
3808 				    PHY_1000T_CTRL, phydata);
3809 			}
3810 		} else {
3811 			e1000g_smartspeed(Adapter);
3812 		}
3813 
3814 		if (Adapter->e1000g_state & E1000G_STARTED) {
3815 			if (Adapter->tx_link_down_timeout <
3816 			    MAX_TX_LINK_DOWN_TIMEOUT) {
3817 				Adapter->tx_link_down_timeout++;
3818 			} else if (Adapter->tx_link_down_timeout ==
3819 			    MAX_TX_LINK_DOWN_TIMEOUT) {
3820 				e1000g_tx_clean(Adapter);
3821 				Adapter->tx_link_down_timeout++;
3822 			}
3823 		}
3824 	}
3825 
3826 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
3827 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
3828 
3829 	return (link_changed);
3830 }
3831 
3832 /*
3833  * e1000g_reset_link - Using the link properties to setup the link
3834  */
3835 int
3836 e1000g_reset_link(struct e1000g *Adapter)
3837 {
3838 	struct e1000_mac_info *mac;
3839 	struct e1000_phy_info *phy;
3840 	boolean_t invalid;
3841 
3842 	mac = &Adapter->shared.mac;
3843 	phy = &Adapter->shared.phy;
3844 	invalid = B_FALSE;
3845 
3846 	if (Adapter->param_adv_autoneg == 1) {
3847 		mac->autoneg = B_TRUE;
3848 		phy->autoneg_advertised = 0;
3849 
3850 		/*
3851 		 * 1000hdx is not supported for autonegotiation
3852 		 */
3853 		if (Adapter->param_adv_1000fdx == 1)
3854 			phy->autoneg_advertised |= ADVERTISE_1000_FULL;
3855 
3856 		if (Adapter->param_adv_100fdx == 1)
3857 			phy->autoneg_advertised |= ADVERTISE_100_FULL;
3858 
3859 		if (Adapter->param_adv_100hdx == 1)
3860 			phy->autoneg_advertised |= ADVERTISE_100_HALF;
3861 
3862 		if (Adapter->param_adv_10fdx == 1)
3863 			phy->autoneg_advertised |= ADVERTISE_10_FULL;
3864 
3865 		if (Adapter->param_adv_10hdx == 1)
3866 			phy->autoneg_advertised |= ADVERTISE_10_HALF;
3867 
3868 		if (phy->autoneg_advertised == 0)
3869 			invalid = B_TRUE;
3870 	} else {
3871 		mac->autoneg = B_FALSE;
3872 
3873 		/*
3874 		 * 1000fdx and 1000hdx are not supported for forced link
3875 		 */
3876 		if (Adapter->param_adv_100fdx == 1)
3877 			mac->forced_speed_duplex = ADVERTISE_100_FULL;
3878 		else if (Adapter->param_adv_100hdx == 1)
3879 			mac->forced_speed_duplex = ADVERTISE_100_HALF;
3880 		else if (Adapter->param_adv_10fdx == 1)
3881 			mac->forced_speed_duplex = ADVERTISE_10_FULL;
3882 		else if (Adapter->param_adv_10hdx == 1)
3883 			mac->forced_speed_duplex = ADVERTISE_10_HALF;
3884 		else
3885 			invalid = B_TRUE;
3886 
3887 	}
3888 
3889 	if (invalid) {
3890 		e1000g_log(Adapter, CE_WARN,
3891 		    "Invalid link sets. Setup link to"
3892 		    "support autonegotiation with all link capabilities.");
3893 		mac->autoneg = B_TRUE;
3894 		phy->autoneg_advertised = ADVERTISE_1000_FULL |
3895 		    ADVERTISE_100_FULL | ADVERTISE_100_HALF |
3896 		    ADVERTISE_10_FULL | ADVERTISE_10_HALF;
3897 	}
3898 
3899 	return (e1000_setup_link(&Adapter->shared));
3900 }
3901 
3902 static void
3903 e1000g_timer_tx_resched(struct e1000g *Adapter)
3904 {
3905 	e1000g_tx_ring_t *tx_ring = Adapter->tx_ring;
3906 
3907 	rw_enter(&Adapter->chip_lock, RW_READER);
3908 
3909 	if (tx_ring->resched_needed &&
3910 	    ((ddi_get_lbolt() - tx_ring->resched_timestamp) >
3911 	    drv_usectohz(1000000)) &&
3912 	    (Adapter->e1000g_state & E1000G_STARTED) &&
3913 	    (tx_ring->tbd_avail >= DEFAULT_TX_NO_RESOURCE)) {
3914 		tx_ring->resched_needed = B_FALSE;
3915 		mac_tx_update(Adapter->mh);
3916 		E1000G_STAT(tx_ring->stat_reschedule);
3917 		E1000G_STAT(tx_ring->stat_timer_reschedule);
3918 	}
3919 
3920 	rw_exit(&Adapter->chip_lock);
3921 }
3922 
3923 static void
3924 e1000g_local_timer(void *ws)
3925 {
3926 	struct e1000g *Adapter = (struct e1000g *)ws;
3927 	struct e1000_hw *hw;
3928 	e1000g_ether_addr_t ether_addr;
3929 	boolean_t link_changed;
3930 
3931 	hw = &Adapter->shared;
3932 
3933 	if (Adapter->e1000g_state & E1000G_ERROR) {
3934 		rw_enter(&Adapter->chip_lock, RW_WRITER);
3935 		Adapter->e1000g_state &= ~E1000G_ERROR;
3936 		rw_exit(&Adapter->chip_lock);
3937 
3938 		Adapter->reset_count++;
3939 		if (e1000g_global_reset(Adapter)) {
3940 			ddi_fm_service_impact(Adapter->dip,
3941 			    DDI_SERVICE_RESTORED);
3942 			e1000g_timer_tx_resched(Adapter);
3943 		} else
3944 			ddi_fm_service_impact(Adapter->dip,
3945 			    DDI_SERVICE_LOST);
3946 		return;
3947 	}
3948 
3949 	if (e1000g_stall_check(Adapter)) {
3950 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
3951 		    "Tx stall detected. Activate automatic recovery.\n");
3952 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_STALL);
3953 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
3954 		Adapter->reset_count++;
3955 		if (e1000g_reset_adapter(Adapter)) {
3956 			ddi_fm_service_impact(Adapter->dip,
3957 			    DDI_SERVICE_RESTORED);
3958 			e1000g_timer_tx_resched(Adapter);
3959 		}
3960 		return;
3961 	}
3962 
3963 	link_changed = B_FALSE;
3964 	rw_enter(&Adapter->chip_lock, RW_READER);
3965 	if (Adapter->link_complete)
3966 		link_changed = e1000g_link_check(Adapter);
3967 	rw_exit(&Adapter->chip_lock);
3968 
3969 	if (link_changed) {
3970 		if (!Adapter->reset_flag)
3971 			mac_link_update(Adapter->mh, Adapter->link_state);
3972 		if (Adapter->link_state == LINK_STATE_UP)
3973 			Adapter->reset_flag = B_FALSE;
3974 	}
3975 	/*
3976 	 * Workaround for esb2. Data stuck in fifo on a link
3977 	 * down event. Reset the adapter to recover it.
3978 	 */
3979 	if (Adapter->esb2_workaround) {
3980 		Adapter->esb2_workaround = B_FALSE;
3981 		(void) e1000g_reset_adapter(Adapter);
3982 		return;
3983 	}
3984 
3985 	/*
3986 	 * With 82571 controllers, any locally administered address will
3987 	 * be overwritten when there is a reset on the other port.
3988 	 * Detect this circumstance and correct it.
3989 	 */
3990 	if ((hw->mac.type == e1000_82571) &&
3991 	    (e1000_get_laa_state_82571(hw) == B_TRUE)) {
3992 		ether_addr.reg.low = E1000_READ_REG_ARRAY(hw, E1000_RA, 0);
3993 		ether_addr.reg.high = E1000_READ_REG_ARRAY(hw, E1000_RA, 1);
3994 
3995 		ether_addr.reg.low = ntohl(ether_addr.reg.low);
3996 		ether_addr.reg.high = ntohl(ether_addr.reg.high);
3997 
3998 		if ((ether_addr.mac.addr[5] != hw->mac.addr[0]) ||
3999 		    (ether_addr.mac.addr[4] != hw->mac.addr[1]) ||
4000 		    (ether_addr.mac.addr[3] != hw->mac.addr[2]) ||
4001 		    (ether_addr.mac.addr[2] != hw->mac.addr[3]) ||
4002 		    (ether_addr.mac.addr[1] != hw->mac.addr[4]) ||
4003 		    (ether_addr.mac.addr[0] != hw->mac.addr[5])) {
4004 			e1000_rar_set(hw, hw->mac.addr, 0);
4005 		}
4006 	}
4007 
4008 	/*
4009 	 * Long TTL workaround for 82541/82547
4010 	 */
4011 	(void) e1000_igp_ttl_workaround_82547(hw);
4012 
4013 	/*
4014 	 * Check for Adaptive IFS settings If there are lots of collisions
4015 	 * change the value in steps...
4016 	 * These properties should only be set for 10/100
4017 	 */
4018 	if ((hw->phy.media_type == e1000_media_type_copper) &&
4019 	    ((Adapter->link_speed == SPEED_100) ||
4020 	    (Adapter->link_speed == SPEED_10))) {
4021 		e1000_update_adaptive(hw);
4022 	}
4023 	/*
4024 	 * Set Timer Interrupts
4025 	 */
4026 	E1000_WRITE_REG(hw, E1000_ICS, E1000_IMS_RXT0);
4027 
4028 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
4029 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
4030 	else
4031 		e1000g_timer_tx_resched(Adapter);
4032 
4033 	restart_watchdog_timer(Adapter);
4034 }
4035 
4036 /*
4037  * The function e1000g_link_timer() is called when the timer for link setup
4038  * is expired, which indicates the completion of the link setup. The link
4039  * state will not be updated until the link setup is completed. And the
4040  * link state will not be sent to the upper layer through mac_link_update()
4041  * in this function. It will be updated in the local timer routine or the
4042  * interrupt service routine after the interface is started (plumbed).
4043  */
4044 static void
4045 e1000g_link_timer(void *arg)
4046 {
4047 	struct e1000g *Adapter = (struct e1000g *)arg;
4048 
4049 	mutex_enter(&Adapter->link_lock);
4050 	Adapter->link_complete = B_TRUE;
4051 	Adapter->link_tid = 0;
4052 	mutex_exit(&Adapter->link_lock);
4053 }
4054 
4055 /*
4056  * e1000g_force_speed_duplex - read forced speed/duplex out of e1000g.conf
4057  *
4058  * This function read the forced speed and duplex for 10/100 Mbps speeds
4059  * and also for 1000 Mbps speeds from the e1000g.conf file
4060  */
4061 static void
4062 e1000g_force_speed_duplex(struct e1000g *Adapter)
4063 {
4064 	int forced;
4065 	struct e1000_mac_info *mac = &Adapter->shared.mac;
4066 	struct e1000_phy_info *phy = &Adapter->shared.phy;
4067 
4068 	/*
4069 	 * get value out of config file
4070 	 */
4071 	forced = e1000g_get_prop(Adapter, "ForceSpeedDuplex",
4072 	    GDIAG_10_HALF, GDIAG_ANY, GDIAG_ANY);
4073 
4074 	switch (forced) {
4075 	case GDIAG_10_HALF:
4076 		/*
4077 		 * Disable Auto Negotiation
4078 		 */
4079 		mac->autoneg = B_FALSE;
4080 		mac->forced_speed_duplex = ADVERTISE_10_HALF;
4081 		break;
4082 	case GDIAG_10_FULL:
4083 		/*
4084 		 * Disable Auto Negotiation
4085 		 */
4086 		mac->autoneg = B_FALSE;
4087 		mac->forced_speed_duplex = ADVERTISE_10_FULL;
4088 		break;
4089 	case GDIAG_100_HALF:
4090 		/*
4091 		 * Disable Auto Negotiation
4092 		 */
4093 		mac->autoneg = B_FALSE;
4094 		mac->forced_speed_duplex = ADVERTISE_100_HALF;
4095 		break;
4096 	case GDIAG_100_FULL:
4097 		/*
4098 		 * Disable Auto Negotiation
4099 		 */
4100 		mac->autoneg = B_FALSE;
4101 		mac->forced_speed_duplex = ADVERTISE_100_FULL;
4102 		break;
4103 	case GDIAG_1000_FULL:
4104 		/*
4105 		 * The gigabit spec requires autonegotiation.  Therefore,
4106 		 * when the user wants to force the speed to 1000Mbps, we
4107 		 * enable AutoNeg, but only allow the harware to advertise
4108 		 * 1000Mbps.  This is different from 10/100 operation, where
4109 		 * we are allowed to link without any negotiation.
4110 		 */
4111 		mac->autoneg = B_TRUE;
4112 		phy->autoneg_advertised = ADVERTISE_1000_FULL;
4113 		break;
4114 	default:	/* obey the setting of AutoNegAdvertised */
4115 		mac->autoneg = B_TRUE;
4116 		phy->autoneg_advertised =
4117 		    (uint16_t)e1000g_get_prop(Adapter, "AutoNegAdvertised",
4118 		    0, AUTONEG_ADVERTISE_SPEED_DEFAULT,
4119 		    AUTONEG_ADVERTISE_SPEED_DEFAULT);
4120 		break;
4121 	}	/* switch */
4122 }
4123 
4124 /*
4125  * e1000g_get_max_frame_size - get jumbo frame setting from e1000g.conf
4126  *
4127  * This function reads MaxFrameSize from e1000g.conf
4128  */
4129 static void
4130 e1000g_get_max_frame_size(struct e1000g *Adapter)
4131 {
4132 	int max_frame;
4133 	struct e1000_mac_info *mac = &Adapter->shared.mac;
4134 	struct e1000_phy_info *phy = &Adapter->shared.phy;
4135 
4136 	/*
4137 	 * get value out of config file
4138 	 */
4139 	max_frame = e1000g_get_prop(Adapter, "MaxFrameSize", 0, 3, 0);
4140 
4141 	switch (max_frame) {
4142 	case 0:
4143 		Adapter->default_mtu = ETHERMTU;
4144 		break;
4145 	/*
4146 	 * To avoid excessive memory allocation for rx buffers,
4147 	 * the bytes of E1000G_IPALIGNPRESERVEROOM are reserved.
4148 	 */
4149 	case 1:
4150 		Adapter->default_mtu = FRAME_SIZE_UPTO_4K -
4151 		    sizeof (struct ether_vlan_header) - ETHERFCSL -
4152 		    E1000G_IPALIGNPRESERVEROOM;
4153 		break;
4154 	case 2:
4155 		Adapter->default_mtu = FRAME_SIZE_UPTO_8K -
4156 		    sizeof (struct ether_vlan_header) - ETHERFCSL -
4157 		    E1000G_IPALIGNPRESERVEROOM;
4158 		break;
4159 	case 3:
4160 		if (mac->type >= e1000_82571)
4161 			Adapter->default_mtu = MAXIMUM_MTU;
4162 		else
4163 			Adapter->default_mtu = FRAME_SIZE_UPTO_16K -
4164 			    sizeof (struct ether_vlan_header) - ETHERFCSL -
4165 			    E1000G_IPALIGNPRESERVEROOM;
4166 		break;
4167 	default:
4168 		Adapter->default_mtu = ETHERMTU;
4169 		break;
4170 	}	/* switch */
4171 
4172 	Adapter->max_frame_size = Adapter->default_mtu +
4173 	    sizeof (struct ether_vlan_header) + ETHERFCSL;
4174 
4175 	/* ich8 does not do jumbo frames */
4176 	if (mac->type == e1000_ich8lan) {
4177 		Adapter->default_mtu = ETHERMTU;
4178 		Adapter->max_frame_size = ETHERMTU +
4179 		    sizeof (struct ether_vlan_header) + ETHERFCSL;
4180 	}
4181 
4182 	/* ich9 does not do jumbo frames on one phy type */
4183 	if ((mac->type == e1000_ich9lan) &&
4184 	    (phy->type == e1000_phy_ife)) {
4185 		Adapter->default_mtu = ETHERMTU;
4186 		Adapter->max_frame_size = ETHERMTU +
4187 		    sizeof (struct ether_vlan_header) + ETHERFCSL;
4188 	}
4189 }
4190 
4191 static void
4192 arm_watchdog_timer(struct e1000g *Adapter)
4193 {
4194 	Adapter->watchdog_tid =
4195 	    timeout(e1000g_local_timer,
4196 	    (void *)Adapter, 1 * drv_usectohz(1000000));
4197 }
4198 #pragma inline(arm_watchdog_timer)
4199 
4200 static void
4201 enable_watchdog_timer(struct e1000g *Adapter)
4202 {
4203 	mutex_enter(&Adapter->watchdog_lock);
4204 
4205 	if (!Adapter->watchdog_timer_enabled) {
4206 		Adapter->watchdog_timer_enabled = B_TRUE;
4207 		Adapter->watchdog_timer_started = B_TRUE;
4208 		arm_watchdog_timer(Adapter);
4209 	}
4210 
4211 	mutex_exit(&Adapter->watchdog_lock);
4212 }
4213 
4214 static void
4215 disable_watchdog_timer(struct e1000g *Adapter)
4216 {
4217 	timeout_id_t tid;
4218 
4219 	mutex_enter(&Adapter->watchdog_lock);
4220 
4221 	Adapter->watchdog_timer_enabled = B_FALSE;
4222 	Adapter->watchdog_timer_started = B_FALSE;
4223 	tid = Adapter->watchdog_tid;
4224 	Adapter->watchdog_tid = 0;
4225 
4226 	mutex_exit(&Adapter->watchdog_lock);
4227 
4228 	if (tid != 0)
4229 		(void) untimeout(tid);
4230 }
4231 
4232 static void
4233 start_watchdog_timer(struct e1000g *Adapter)
4234 {
4235 	mutex_enter(&Adapter->watchdog_lock);
4236 
4237 	if (Adapter->watchdog_timer_enabled) {
4238 		if (!Adapter->watchdog_timer_started) {
4239 			Adapter->watchdog_timer_started = B_TRUE;
4240 			arm_watchdog_timer(Adapter);
4241 		}
4242 	}
4243 
4244 	mutex_exit(&Adapter->watchdog_lock);
4245 }
4246 
4247 static void
4248 restart_watchdog_timer(struct e1000g *Adapter)
4249 {
4250 	mutex_enter(&Adapter->watchdog_lock);
4251 
4252 	if (Adapter->watchdog_timer_started)
4253 		arm_watchdog_timer(Adapter);
4254 
4255 	mutex_exit(&Adapter->watchdog_lock);
4256 }
4257 
4258 static void
4259 stop_watchdog_timer(struct e1000g *Adapter)
4260 {
4261 	timeout_id_t tid;
4262 
4263 	mutex_enter(&Adapter->watchdog_lock);
4264 
4265 	Adapter->watchdog_timer_started = B_FALSE;
4266 	tid = Adapter->watchdog_tid;
4267 	Adapter->watchdog_tid = 0;
4268 
4269 	mutex_exit(&Adapter->watchdog_lock);
4270 
4271 	if (tid != 0)
4272 		(void) untimeout(tid);
4273 }
4274 
4275 static void
4276 stop_link_timer(struct e1000g *Adapter)
4277 {
4278 	timeout_id_t tid;
4279 
4280 	/* Disable the link timer */
4281 	mutex_enter(&Adapter->link_lock);
4282 
4283 	tid = Adapter->link_tid;
4284 	Adapter->link_tid = 0;
4285 
4286 	mutex_exit(&Adapter->link_lock);
4287 
4288 	if (tid != 0)
4289 		(void) untimeout(tid);
4290 }
4291 
4292 static void
4293 stop_82547_timer(e1000g_tx_ring_t *tx_ring)
4294 {
4295 	timeout_id_t tid;
4296 
4297 	/* Disable the tx timer for 82547 chipset */
4298 	mutex_enter(&tx_ring->tx_lock);
4299 
4300 	tx_ring->timer_enable_82547 = B_FALSE;
4301 	tid = tx_ring->timer_id_82547;
4302 	tx_ring->timer_id_82547 = 0;
4303 
4304 	mutex_exit(&tx_ring->tx_lock);
4305 
4306 	if (tid != 0)
4307 		(void) untimeout(tid);
4308 }
4309 
4310 void
4311 e1000g_clear_interrupt(struct e1000g *Adapter)
4312 {
4313 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC,
4314 	    0xffffffff & ~E1000_IMS_RXSEQ);
4315 }
4316 
4317 void
4318 e1000g_mask_interrupt(struct e1000g *Adapter)
4319 {
4320 	E1000_WRITE_REG(&Adapter->shared, E1000_IMS,
4321 	    IMS_ENABLE_MASK & ~E1000_IMS_TXDW);
4322 
4323 	if (Adapter->tx_intr_enable)
4324 		e1000g_mask_tx_interrupt(Adapter);
4325 }
4326 
4327 /*
4328  * This routine is called by e1000g_quiesce(), therefore must not block.
4329  */
4330 void
4331 e1000g_clear_all_interrupts(struct e1000g *Adapter)
4332 {
4333 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC, 0xffffffff);
4334 }
4335 
4336 void
4337 e1000g_mask_tx_interrupt(struct e1000g *Adapter)
4338 {
4339 	E1000_WRITE_REG(&Adapter->shared, E1000_IMS, E1000_IMS_TXDW);
4340 }
4341 
4342 void
4343 e1000g_clear_tx_interrupt(struct e1000g *Adapter)
4344 {
4345 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC, E1000_IMS_TXDW);
4346 }
4347 
4348 static void
4349 e1000g_smartspeed(struct e1000g *Adapter)
4350 {
4351 	struct e1000_hw *hw = &Adapter->shared;
4352 	uint16_t phy_status;
4353 	uint16_t phy_ctrl;
4354 
4355 	/*
4356 	 * If we're not T-or-T, or we're not autoneg'ing, or we're not
4357 	 * advertising 1000Full, we don't even use the workaround
4358 	 */
4359 	if ((hw->phy.type != e1000_phy_igp) ||
4360 	    !hw->mac.autoneg ||
4361 	    !(hw->phy.autoneg_advertised & ADVERTISE_1000_FULL))
4362 		return;
4363 
4364 	/*
4365 	 * True if this is the first call of this function or after every
4366 	 * 30 seconds of not having link
4367 	 */
4368 	if (Adapter->smartspeed == 0) {
4369 		/*
4370 		 * If Master/Slave config fault is asserted twice, we
4371 		 * assume back-to-back
4372 		 */
4373 		(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
4374 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
4375 			return;
4376 
4377 		(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
4378 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
4379 			return;
4380 		/*
4381 		 * We're assuming back-2-back because our status register
4382 		 * insists! there's a fault in the master/slave
4383 		 * relationship that was "negotiated"
4384 		 */
4385 		(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
4386 		/*
4387 		 * Is the phy configured for manual configuration of
4388 		 * master/slave?
4389 		 */
4390 		if (phy_ctrl & CR_1000T_MS_ENABLE) {
4391 			/*
4392 			 * Yes.  Then disable manual configuration (enable
4393 			 * auto configuration) of master/slave
4394 			 */
4395 			phy_ctrl &= ~CR_1000T_MS_ENABLE;
4396 			(void) e1000_write_phy_reg(hw,
4397 			    PHY_1000T_CTRL, phy_ctrl);
4398 			/*
4399 			 * Effectively starting the clock
4400 			 */
4401 			Adapter->smartspeed++;
4402 			/*
4403 			 * Restart autonegotiation
4404 			 */
4405 			if (!e1000_phy_setup_autoneg(hw) &&
4406 			    !e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl)) {
4407 				phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4408 				    MII_CR_RESTART_AUTO_NEG);
4409 				(void) e1000_write_phy_reg(hw,
4410 				    PHY_CONTROL, phy_ctrl);
4411 			}
4412 		}
4413 		return;
4414 		/*
4415 		 * Has 6 seconds transpired still without link? Remember,
4416 		 * you should reset the smartspeed counter once you obtain
4417 		 * link
4418 		 */
4419 	} else if (Adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
4420 		/*
4421 		 * Yes.  Remember, we did at the start determine that
4422 		 * there's a master/slave configuration fault, so we're
4423 		 * still assuming there's someone on the other end, but we
4424 		 * just haven't yet been able to talk to it. We then
4425 		 * re-enable auto configuration of master/slave to see if
4426 		 * we're running 2/3 pair cables.
4427 		 */
4428 		/*
4429 		 * If still no link, perhaps using 2/3 pair cable
4430 		 */
4431 		(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
4432 		phy_ctrl |= CR_1000T_MS_ENABLE;
4433 		(void) e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_ctrl);
4434 		/*
4435 		 * Restart autoneg with phy enabled for manual
4436 		 * configuration of master/slave
4437 		 */
4438 		if (!e1000_phy_setup_autoneg(hw) &&
4439 		    !e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl)) {
4440 			phy_ctrl |=
4441 			    (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
4442 			(void) e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl);
4443 		}
4444 		/*
4445 		 * Hopefully, there are no more faults and we've obtained
4446 		 * link as a result.
4447 		 */
4448 	}
4449 	/*
4450 	 * Restart process after E1000_SMARTSPEED_MAX iterations (30
4451 	 * seconds)
4452 	 */
4453 	if (Adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
4454 		Adapter->smartspeed = 0;
4455 }
4456 
4457 static boolean_t
4458 is_valid_mac_addr(uint8_t *mac_addr)
4459 {
4460 	const uint8_t addr_test1[6] = { 0, 0, 0, 0, 0, 0 };
4461 	const uint8_t addr_test2[6] =
4462 	    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4463 
4464 	if (!(bcmp(addr_test1, mac_addr, ETHERADDRL)) ||
4465 	    !(bcmp(addr_test2, mac_addr, ETHERADDRL)))
4466 		return (B_FALSE);
4467 
4468 	return (B_TRUE);
4469 }
4470 
4471 /*
4472  * e1000g_stall_check - check for tx stall
4473  *
4474  * This function checks if the adapter is stalled (in transmit).
4475  *
4476  * It is called each time the watchdog timeout is invoked.
4477  * If the transmit descriptor reclaim continuously fails,
4478  * the watchdog value will increment by 1. If the watchdog
4479  * value exceeds the threshold, the adapter is assumed to
4480  * have stalled and need to be reset.
4481  */
4482 static boolean_t
4483 e1000g_stall_check(struct e1000g *Adapter)
4484 {
4485 	e1000g_tx_ring_t *tx_ring;
4486 
4487 	tx_ring = Adapter->tx_ring;
4488 
4489 	if (Adapter->link_state != LINK_STATE_UP)
4490 		return (B_FALSE);
4491 
4492 	(void) e1000g_recycle(tx_ring);
4493 
4494 	if (Adapter->stall_flag) {
4495 		Adapter->stall_flag = B_FALSE;
4496 		Adapter->reset_flag = B_TRUE;
4497 		return (B_TRUE);
4498 	}
4499 
4500 	return (B_FALSE);
4501 }
4502 
4503 #ifdef E1000G_DEBUG
4504 static enum ioc_reply
4505 e1000g_pp_ioctl(struct e1000g *e1000gp, struct iocblk *iocp, mblk_t *mp)
4506 {
4507 	void (*ppfn)(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd);
4508 	e1000g_peekpoke_t *ppd;
4509 	uint64_t mem_va;
4510 	uint64_t maxoff;
4511 	boolean_t peek;
4512 
4513 	switch (iocp->ioc_cmd) {
4514 
4515 	case E1000G_IOC_REG_PEEK:
4516 		peek = B_TRUE;
4517 		break;
4518 
4519 	case E1000G_IOC_REG_POKE:
4520 		peek = B_FALSE;
4521 		break;
4522 
4523 	deault:
4524 		E1000G_DEBUGLOG_1(e1000gp, E1000G_INFO_LEVEL,
4525 		    "e1000g_diag_ioctl: invalid ioctl command 0x%X\n",
4526 		    iocp->ioc_cmd);
4527 		return (IOC_INVAL);
4528 	}
4529 
4530 	/*
4531 	 * Validate format of ioctl
4532 	 */
4533 	if (iocp->ioc_count != sizeof (e1000g_peekpoke_t))
4534 		return (IOC_INVAL);
4535 	if (mp->b_cont == NULL)
4536 		return (IOC_INVAL);
4537 
4538 	ppd = (e1000g_peekpoke_t *)(uintptr_t)mp->b_cont->b_rptr;
4539 
4540 	/*
4541 	 * Validate request parameters
4542 	 */
4543 	switch (ppd->pp_acc_space) {
4544 
4545 	default:
4546 		E1000G_DEBUGLOG_1(e1000gp, E1000G_INFO_LEVEL,
4547 		    "e1000g_diag_ioctl: invalid access space 0x%X\n",
4548 		    ppd->pp_acc_space);
4549 		return (IOC_INVAL);
4550 
4551 	case E1000G_PP_SPACE_REG:
4552 		/*
4553 		 * Memory-mapped I/O space
4554 		 */
4555 		ASSERT(ppd->pp_acc_size == 4);
4556 		if (ppd->pp_acc_size != 4)
4557 			return (IOC_INVAL);
4558 
4559 		if ((ppd->pp_acc_offset % ppd->pp_acc_size) != 0)
4560 			return (IOC_INVAL);
4561 
4562 		mem_va = 0;
4563 		maxoff = 0x10000;
4564 		ppfn = peek ? e1000g_ioc_peek_reg : e1000g_ioc_poke_reg;
4565 		break;
4566 
4567 	case E1000G_PP_SPACE_E1000G:
4568 		/*
4569 		 * E1000g data structure!
4570 		 */
4571 		mem_va = (uintptr_t)e1000gp;
4572 		maxoff = sizeof (struct e1000g);
4573 		ppfn = peek ? e1000g_ioc_peek_mem : e1000g_ioc_poke_mem;
4574 		break;
4575 
4576 	}
4577 
4578 	if (ppd->pp_acc_offset >= maxoff)
4579 		return (IOC_INVAL);
4580 
4581 	if (ppd->pp_acc_offset + ppd->pp_acc_size > maxoff)
4582 		return (IOC_INVAL);
4583 
4584 	/*
4585 	 * All OK - go!
4586 	 */
4587 	ppd->pp_acc_offset += mem_va;
4588 	(*ppfn)(e1000gp, ppd);
4589 	return (peek ? IOC_REPLY : IOC_ACK);
4590 }
4591 
4592 static void
4593 e1000g_ioc_peek_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
4594 {
4595 	ddi_acc_handle_t handle;
4596 	uint32_t *regaddr;
4597 
4598 	handle = e1000gp->osdep.reg_handle;
4599 	regaddr = (uint32_t *)((uintptr_t)e1000gp->shared.hw_addr +
4600 	    (uintptr_t)ppd->pp_acc_offset);
4601 
4602 	ppd->pp_acc_data = ddi_get32(handle, regaddr);
4603 }
4604 
4605 static void
4606 e1000g_ioc_poke_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
4607 {
4608 	ddi_acc_handle_t handle;
4609 	uint32_t *regaddr;
4610 	uint32_t value;
4611 
4612 	handle = e1000gp->osdep.reg_handle;
4613 	regaddr = (uint32_t *)((uintptr_t)e1000gp->shared.hw_addr +
4614 	    (uintptr_t)ppd->pp_acc_offset);
4615 	value = (uint32_t)ppd->pp_acc_data;
4616 
4617 	ddi_put32(handle, regaddr, value);
4618 }
4619 
4620 static void
4621 e1000g_ioc_peek_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
4622 {
4623 	uint64_t value;
4624 	void *vaddr;
4625 
4626 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
4627 
4628 	switch (ppd->pp_acc_size) {
4629 	case 1:
4630 		value = *(uint8_t *)vaddr;
4631 		break;
4632 
4633 	case 2:
4634 		value = *(uint16_t *)vaddr;
4635 		break;
4636 
4637 	case 4:
4638 		value = *(uint32_t *)vaddr;
4639 		break;
4640 
4641 	case 8:
4642 		value = *(uint64_t *)vaddr;
4643 		break;
4644 	}
4645 
4646 	E1000G_DEBUGLOG_4(e1000gp, E1000G_INFO_LEVEL,
4647 	    "e1000g_ioc_peek_mem($%p, $%p) peeked 0x%llx from $%p\n",
4648 	    (void *)e1000gp, (void *)ppd, value, vaddr);
4649 
4650 	ppd->pp_acc_data = value;
4651 }
4652 
4653 static void
4654 e1000g_ioc_poke_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
4655 {
4656 	uint64_t value;
4657 	void *vaddr;
4658 
4659 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
4660 	value = ppd->pp_acc_data;
4661 
4662 	E1000G_DEBUGLOG_4(e1000gp, E1000G_INFO_LEVEL,
4663 	    "e1000g_ioc_poke_mem($%p, $%p) poking 0x%llx at $%p\n",
4664 	    (void *)e1000gp, (void *)ppd, value, vaddr);
4665 
4666 	switch (ppd->pp_acc_size) {
4667 	case 1:
4668 		*(uint8_t *)vaddr = (uint8_t)value;
4669 		break;
4670 
4671 	case 2:
4672 		*(uint16_t *)vaddr = (uint16_t)value;
4673 		break;
4674 
4675 	case 4:
4676 		*(uint32_t *)vaddr = (uint32_t)value;
4677 		break;
4678 
4679 	case 8:
4680 		*(uint64_t *)vaddr = (uint64_t)value;
4681 		break;
4682 	}
4683 }
4684 #endif
4685 
4686 /*
4687  * Loopback Support
4688  */
4689 static lb_property_t lb_normal =
4690 	{ normal,	"normal",	E1000G_LB_NONE		};
4691 static lb_property_t lb_external1000 =
4692 	{ external,	"1000Mbps",	E1000G_LB_EXTERNAL_1000	};
4693 static lb_property_t lb_external100 =
4694 	{ external,	"100Mbps",	E1000G_LB_EXTERNAL_100	};
4695 static lb_property_t lb_external10 =
4696 	{ external,	"10Mbps",	E1000G_LB_EXTERNAL_10	};
4697 static lb_property_t lb_phy =
4698 	{ internal,	"PHY",		E1000G_LB_INTERNAL_PHY	};
4699 
4700 static enum ioc_reply
4701 e1000g_loopback_ioctl(struct e1000g *Adapter, struct iocblk *iocp, mblk_t *mp)
4702 {
4703 	lb_info_sz_t *lbsp;
4704 	lb_property_t *lbpp;
4705 	struct e1000_hw *hw;
4706 	uint32_t *lbmp;
4707 	uint32_t size;
4708 	uint32_t value;
4709 
4710 	hw = &Adapter->shared;
4711 
4712 	if (mp->b_cont == NULL)
4713 		return (IOC_INVAL);
4714 
4715 	if (!e1000g_check_loopback_support(hw)) {
4716 		e1000g_log(NULL, CE_WARN,
4717 		    "Loopback is not supported on e1000g%d", Adapter->instance);
4718 		return (IOC_INVAL);
4719 	}
4720 
4721 	switch (iocp->ioc_cmd) {
4722 	default:
4723 		return (IOC_INVAL);
4724 
4725 	case LB_GET_INFO_SIZE:
4726 		size = sizeof (lb_info_sz_t);
4727 		if (iocp->ioc_count != size)
4728 			return (IOC_INVAL);
4729 
4730 		rw_enter(&Adapter->chip_lock, RW_WRITER);
4731 		e1000g_get_phy_state(Adapter);
4732 
4733 		/*
4734 		 * Workaround for hardware faults. In order to get a stable
4735 		 * state of phy, we will wait for a specific interval and
4736 		 * try again. The time delay is an experiential value based
4737 		 * on our testing.
4738 		 */
4739 		msec_delay(100);
4740 		e1000g_get_phy_state(Adapter);
4741 		rw_exit(&Adapter->chip_lock);
4742 
4743 		value = sizeof (lb_normal);
4744 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
4745 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
4746 		    (hw->phy.media_type == e1000_media_type_fiber) ||
4747 		    (hw->phy.media_type == e1000_media_type_internal_serdes)) {
4748 			value += sizeof (lb_phy);
4749 			switch (hw->mac.type) {
4750 			case e1000_82571:
4751 			case e1000_82572:
4752 			case e1000_80003es2lan:
4753 				value += sizeof (lb_external1000);
4754 				break;
4755 			}
4756 		}
4757 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
4758 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
4759 			value += sizeof (lb_external100);
4760 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
4761 			value += sizeof (lb_external10);
4762 
4763 		lbsp = (lb_info_sz_t *)(uintptr_t)mp->b_cont->b_rptr;
4764 		*lbsp = value;
4765 		break;
4766 
4767 	case LB_GET_INFO:
4768 		value = sizeof (lb_normal);
4769 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
4770 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
4771 		    (hw->phy.media_type == e1000_media_type_fiber) ||
4772 		    (hw->phy.media_type == e1000_media_type_internal_serdes)) {
4773 			value += sizeof (lb_phy);
4774 			switch (hw->mac.type) {
4775 			case e1000_82571:
4776 			case e1000_82572:
4777 			case e1000_80003es2lan:
4778 				value += sizeof (lb_external1000);
4779 				break;
4780 			}
4781 		}
4782 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
4783 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
4784 			value += sizeof (lb_external100);
4785 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
4786 			value += sizeof (lb_external10);
4787 
4788 		size = value;
4789 		if (iocp->ioc_count != size)
4790 			return (IOC_INVAL);
4791 
4792 		value = 0;
4793 		lbpp = (lb_property_t *)(uintptr_t)mp->b_cont->b_rptr;
4794 		lbpp[value++] = lb_normal;
4795 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
4796 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
4797 		    (hw->phy.media_type == e1000_media_type_fiber) ||
4798 		    (hw->phy.media_type == e1000_media_type_internal_serdes)) {
4799 			lbpp[value++] = lb_phy;
4800 			switch (hw->mac.type) {
4801 			case e1000_82571:
4802 			case e1000_82572:
4803 			case e1000_80003es2lan:
4804 				lbpp[value++] = lb_external1000;
4805 				break;
4806 			}
4807 		}
4808 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
4809 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
4810 			lbpp[value++] = lb_external100;
4811 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
4812 			lbpp[value++] = lb_external10;
4813 		break;
4814 
4815 	case LB_GET_MODE:
4816 		size = sizeof (uint32_t);
4817 		if (iocp->ioc_count != size)
4818 			return (IOC_INVAL);
4819 
4820 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
4821 		*lbmp = Adapter->loopback_mode;
4822 		break;
4823 
4824 	case LB_SET_MODE:
4825 		size = 0;
4826 		if (iocp->ioc_count != sizeof (uint32_t))
4827 			return (IOC_INVAL);
4828 
4829 		lbmp = (uint32_t *)(uintptr_t)mp->b_cont->b_rptr;
4830 		if (!e1000g_set_loopback_mode(Adapter, *lbmp))
4831 			return (IOC_INVAL);
4832 		break;
4833 	}
4834 
4835 	iocp->ioc_count = size;
4836 	iocp->ioc_error = 0;
4837 
4838 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
4839 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
4840 		return (IOC_INVAL);
4841 	}
4842 
4843 	return (IOC_REPLY);
4844 }
4845 
4846 static boolean_t
4847 e1000g_check_loopback_support(struct e1000_hw *hw)
4848 {
4849 	switch (hw->mac.type) {
4850 	case e1000_82540:
4851 	case e1000_82545:
4852 	case e1000_82545_rev_3:
4853 	case e1000_82546:
4854 	case e1000_82546_rev_3:
4855 	case e1000_82541:
4856 	case e1000_82541_rev_2:
4857 	case e1000_82547:
4858 	case e1000_82547_rev_2:
4859 	case e1000_82571:
4860 	case e1000_82572:
4861 	case e1000_82573:
4862 	case e1000_82574:
4863 	case e1000_80003es2lan:
4864 	case e1000_ich9lan:
4865 	case e1000_ich10lan:
4866 		return (B_TRUE);
4867 	}
4868 	return (B_FALSE);
4869 }
4870 
4871 static boolean_t
4872 e1000g_set_loopback_mode(struct e1000g *Adapter, uint32_t mode)
4873 {
4874 	struct e1000_hw *hw;
4875 	int i, times;
4876 	boolean_t link_up;
4877 
4878 	if (mode == Adapter->loopback_mode)
4879 		return (B_TRUE);
4880 
4881 	hw = &Adapter->shared;
4882 	times = 0;
4883 
4884 	Adapter->loopback_mode = mode;
4885 
4886 	if (mode == E1000G_LB_NONE) {
4887 		/* Reset the chip */
4888 		hw->phy.autoneg_wait_to_complete = B_TRUE;
4889 		(void) e1000g_reset_adapter(Adapter);
4890 		hw->phy.autoneg_wait_to_complete = B_FALSE;
4891 		return (B_TRUE);
4892 	}
4893 
4894 again:
4895 
4896 	rw_enter(&Adapter->chip_lock, RW_WRITER);
4897 
4898 	switch (mode) {
4899 	default:
4900 		rw_exit(&Adapter->chip_lock);
4901 		return (B_FALSE);
4902 
4903 	case E1000G_LB_EXTERNAL_1000:
4904 		e1000g_set_external_loopback_1000(Adapter);
4905 		break;
4906 
4907 	case E1000G_LB_EXTERNAL_100:
4908 		e1000g_set_external_loopback_100(Adapter);
4909 		break;
4910 
4911 	case E1000G_LB_EXTERNAL_10:
4912 		e1000g_set_external_loopback_10(Adapter);
4913 		break;
4914 
4915 	case E1000G_LB_INTERNAL_PHY:
4916 		e1000g_set_internal_loopback(Adapter);
4917 		break;
4918 	}
4919 
4920 	times++;
4921 
4922 	rw_exit(&Adapter->chip_lock);
4923 
4924 	/* Wait for link up */
4925 	for (i = (PHY_FORCE_LIMIT * 2); i > 0; i--)
4926 		msec_delay(100);
4927 
4928 	rw_enter(&Adapter->chip_lock, RW_WRITER);
4929 
4930 	link_up = e1000g_link_up(Adapter);
4931 
4932 	rw_exit(&Adapter->chip_lock);
4933 
4934 	if (!link_up) {
4935 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
4936 		    "Failed to get the link up");
4937 		if (times < 2) {
4938 			/* Reset the link */
4939 			E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
4940 			    "Reset the link ...");
4941 			(void) e1000g_reset_adapter(Adapter);
4942 			goto again;
4943 		}
4944 	}
4945 
4946 	return (B_TRUE);
4947 }
4948 
4949 /*
4950  * The following loopback settings are from Intel's technical
4951  * document - "How To Loopback". All the register settings and
4952  * time delay values are directly inherited from the document
4953  * without more explanations available.
4954  */
4955 static void
4956 e1000g_set_internal_loopback(struct e1000g *Adapter)
4957 {
4958 	struct e1000_hw *hw;
4959 	uint32_t ctrl;
4960 	uint32_t status;
4961 	uint16_t phy_ctrl;
4962 	uint16_t phy_reg;
4963 	uint32_t txcw;
4964 
4965 	hw = &Adapter->shared;
4966 
4967 	/* Disable Smart Power Down */
4968 	phy_spd_state(hw, B_FALSE);
4969 
4970 	(void) e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl);
4971 	phy_ctrl &= ~(MII_CR_AUTO_NEG_EN | MII_CR_SPEED_100 | MII_CR_SPEED_10);
4972 	phy_ctrl |= MII_CR_FULL_DUPLEX | MII_CR_SPEED_1000;
4973 
4974 	switch (hw->mac.type) {
4975 	case e1000_82540:
4976 	case e1000_82545:
4977 	case e1000_82545_rev_3:
4978 	case e1000_82546:
4979 	case e1000_82546_rev_3:
4980 	case e1000_82573:
4981 		/* Auto-MDI/MDIX off */
4982 		(void) e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, 0x0808);
4983 		/* Reset PHY to update Auto-MDI/MDIX */
4984 		(void) e1000_write_phy_reg(hw, PHY_CONTROL,
4985 		    phy_ctrl | MII_CR_RESET | MII_CR_AUTO_NEG_EN);
4986 		/* Reset PHY to auto-neg off and force 1000 */
4987 		(void) e1000_write_phy_reg(hw, PHY_CONTROL,
4988 		    phy_ctrl | MII_CR_RESET);
4989 		/*
4990 		 * Disable PHY receiver for 82540/545/546 and 82573 Family.
4991 		 * See comments above e1000g_set_internal_loopback() for the
4992 		 * background.
4993 		 */
4994 		(void) e1000_write_phy_reg(hw, 29, 0x001F);
4995 		(void) e1000_write_phy_reg(hw, 30, 0x8FFC);
4996 		(void) e1000_write_phy_reg(hw, 29, 0x001A);
4997 		(void) e1000_write_phy_reg(hw, 30, 0x8FF0);
4998 		break;
4999 	case e1000_80003es2lan:
5000 		/* Force Link Up */
5001 		(void) e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL,
5002 		    0x1CC);
5003 		/* Sets PCS loopback at 1Gbs */
5004 		(void) e1000_write_phy_reg(hw, GG82563_PHY_MAC_SPEC_CTRL,
5005 		    0x1046);
5006 		break;
5007 	}
5008 
5009 	/*
5010 	 * The following registers should be set for e1000_phy_bm phy type.
5011 	 * e1000_82574, e1000_ich10lan and some e1000_ich9lan use this phy.
5012 	 * For others, we do not need to set these registers.
5013 	 */
5014 	if (hw->phy.type == e1000_phy_bm) {
5015 		/* Set Default MAC Interface speed to 1GB */
5016 		(void) e1000_read_phy_reg(hw, PHY_REG(2, 21), &phy_reg);
5017 		phy_reg &= ~0x0007;
5018 		phy_reg |= 0x006;
5019 		(void) e1000_write_phy_reg(hw, PHY_REG(2, 21), phy_reg);
5020 		/* Assert SW reset for above settings to take effect */
5021 		(void) e1000_phy_commit(hw);
5022 		msec_delay(1);
5023 		/* Force Full Duplex */
5024 		(void) e1000_read_phy_reg(hw, PHY_REG(769, 16), &phy_reg);
5025 		(void) e1000_write_phy_reg(hw, PHY_REG(769, 16),
5026 		    phy_reg | 0x000C);
5027 		/* Set Link Up (in force link) */
5028 		(void) e1000_read_phy_reg(hw, PHY_REG(776, 16), &phy_reg);
5029 		(void) e1000_write_phy_reg(hw, PHY_REG(776, 16),
5030 		    phy_reg | 0x0040);
5031 		/* Force Link */
5032 		(void) e1000_read_phy_reg(hw, PHY_REG(769, 16), &phy_reg);
5033 		(void) e1000_write_phy_reg(hw, PHY_REG(769, 16),
5034 		    phy_reg | 0x0040);
5035 		/* Set Early Link Enable */
5036 		(void) e1000_read_phy_reg(hw, PHY_REG(769, 20), &phy_reg);
5037 		(void) e1000_write_phy_reg(hw, PHY_REG(769, 20),
5038 		    phy_reg | 0x0400);
5039 	}
5040 
5041 	/* Set loopback */
5042 	(void) e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl | MII_CR_LOOPBACK);
5043 
5044 	msec_delay(250);
5045 
5046 	/* Now set up the MAC to the same speed/duplex as the PHY. */
5047 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
5048 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
5049 	ctrl |= (E1000_CTRL_FRCSPD |	/* Set the Force Speed Bit */
5050 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
5051 	    E1000_CTRL_SPD_1000 |	/* Force Speed to 1000 */
5052 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
5053 
5054 	switch (hw->mac.type) {
5055 	case e1000_82540:
5056 	case e1000_82545:
5057 	case e1000_82545_rev_3:
5058 	case e1000_82546:
5059 	case e1000_82546_rev_3:
5060 		/*
5061 		 * For some serdes we'll need to commit the writes now
5062 		 * so that the status is updated on link
5063 		 */
5064 		if (hw->phy.media_type == e1000_media_type_internal_serdes) {
5065 			E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
5066 			msec_delay(100);
5067 			ctrl = E1000_READ_REG(hw, E1000_CTRL);
5068 		}
5069 
5070 		if (hw->phy.media_type == e1000_media_type_copper) {
5071 			/* Invert Loss of Signal */
5072 			ctrl |= E1000_CTRL_ILOS;
5073 		} else {
5074 			/* Set ILOS on fiber nic if half duplex is detected */
5075 			status = E1000_READ_REG(hw, E1000_STATUS);
5076 			if ((status & E1000_STATUS_FD) == 0)
5077 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
5078 		}
5079 		break;
5080 
5081 	case e1000_82571:
5082 	case e1000_82572:
5083 		/*
5084 		 * The fiber/SerDes versions of this adapter do not contain an
5085 		 * accessible PHY. Therefore, loopback beyond MAC must be done
5086 		 * using SerDes analog loopback.
5087 		 */
5088 		if (hw->phy.media_type != e1000_media_type_copper) {
5089 			/* Disable autoneg by setting bit 31 of TXCW to zero */
5090 			txcw = E1000_READ_REG(hw, E1000_TXCW);
5091 			txcw &= ~((uint32_t)1 << 31);
5092 			E1000_WRITE_REG(hw, E1000_TXCW, txcw);
5093 
5094 			/*
5095 			 * Write 0x410 to Serdes Control register
5096 			 * to enable Serdes analog loopback
5097 			 */
5098 			E1000_WRITE_REG(hw, E1000_SCTL, 0x0410);
5099 			msec_delay(10);
5100 		}
5101 
5102 		status = E1000_READ_REG(hw, E1000_STATUS);
5103 		/* Set ILOS on fiber nic if half duplex is detected */
5104 		if ((hw->phy.media_type == e1000_media_type_fiber) &&
5105 		    ((status & E1000_STATUS_FD) == 0 ||
5106 		    (status & E1000_STATUS_LU) == 0))
5107 			ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
5108 		else if (hw->phy.media_type == e1000_media_type_internal_serdes)
5109 			ctrl |= E1000_CTRL_SLU;
5110 		break;
5111 
5112 	case e1000_82573:
5113 		ctrl |= E1000_CTRL_ILOS;
5114 		break;
5115 	case e1000_ich9lan:
5116 	case e1000_ich10lan:
5117 		ctrl |= E1000_CTRL_SLU;
5118 		break;
5119 	}
5120 	if (hw->phy.type == e1000_phy_bm)
5121 		ctrl |= E1000_CTRL_SLU | E1000_CTRL_ILOS;
5122 
5123 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
5124 }
5125 
5126 static void
5127 e1000g_set_external_loopback_1000(struct e1000g *Adapter)
5128 {
5129 	struct e1000_hw *hw;
5130 	uint32_t rctl;
5131 	uint32_t ctrl_ext;
5132 	uint32_t ctrl;
5133 	uint32_t status;
5134 	uint32_t txcw;
5135 	uint16_t phydata;
5136 
5137 	hw = &Adapter->shared;
5138 
5139 	/* Disable Smart Power Down */
5140 	phy_spd_state(hw, B_FALSE);
5141 
5142 	switch (hw->mac.type) {
5143 	case e1000_82571:
5144 	case e1000_82572:
5145 		switch (hw->phy.media_type) {
5146 		case e1000_media_type_copper:
5147 			/* Force link up (Must be done before the PHY writes) */
5148 			ctrl = E1000_READ_REG(hw, E1000_CTRL);
5149 			ctrl |= E1000_CTRL_SLU;	/* Force Link Up */
5150 			E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
5151 
5152 			rctl = E1000_READ_REG(hw, E1000_RCTL);
5153 			rctl |= (E1000_RCTL_EN |
5154 			    E1000_RCTL_SBP |
5155 			    E1000_RCTL_UPE |
5156 			    E1000_RCTL_MPE |
5157 			    E1000_RCTL_LPE |
5158 			    E1000_RCTL_BAM);		/* 0x803E */
5159 			E1000_WRITE_REG(hw, E1000_RCTL, rctl);
5160 
5161 			ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
5162 			ctrl_ext |= (E1000_CTRL_EXT_SDP4_DATA |
5163 			    E1000_CTRL_EXT_SDP6_DATA |
5164 			    E1000_CTRL_EXT_SDP7_DATA |
5165 			    E1000_CTRL_EXT_SDP4_DIR |
5166 			    E1000_CTRL_EXT_SDP6_DIR |
5167 			    E1000_CTRL_EXT_SDP7_DIR);	/* 0x0DD0 */
5168 			E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
5169 
5170 			/*
5171 			 * This sequence tunes the PHY's SDP and no customer
5172 			 * settable values. For background, see comments above
5173 			 * e1000g_set_internal_loopback().
5174 			 */
5175 			(void) e1000_write_phy_reg(hw, 0x0, 0x140);
5176 			msec_delay(10);
5177 			(void) e1000_write_phy_reg(hw, 0x9, 0x1A00);
5178 			(void) e1000_write_phy_reg(hw, 0x12, 0xC10);
5179 			(void) e1000_write_phy_reg(hw, 0x12, 0x1C10);
5180 			(void) e1000_write_phy_reg(hw, 0x1F37, 0x76);
5181 			(void) e1000_write_phy_reg(hw, 0x1F33, 0x1);
5182 			(void) e1000_write_phy_reg(hw, 0x1F33, 0x0);
5183 
5184 			(void) e1000_write_phy_reg(hw, 0x1F35, 0x65);
5185 			(void) e1000_write_phy_reg(hw, 0x1837, 0x3F7C);
5186 			(void) e1000_write_phy_reg(hw, 0x1437, 0x3FDC);
5187 			(void) e1000_write_phy_reg(hw, 0x1237, 0x3F7C);
5188 			(void) e1000_write_phy_reg(hw, 0x1137, 0x3FDC);
5189 
5190 			msec_delay(50);
5191 			break;
5192 		case e1000_media_type_fiber:
5193 		case e1000_media_type_internal_serdes:
5194 			status = E1000_READ_REG(hw, E1000_STATUS);
5195 			if (((status & E1000_STATUS_LU) == 0) ||
5196 			    (hw->phy.media_type ==
5197 			    e1000_media_type_internal_serdes)) {
5198 				ctrl = E1000_READ_REG(hw, E1000_CTRL);
5199 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
5200 				E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
5201 			}
5202 
5203 			/* Disable autoneg by setting bit 31 of TXCW to zero */
5204 			txcw = E1000_READ_REG(hw, E1000_TXCW);
5205 			txcw &= ~((uint32_t)1 << 31);
5206 			E1000_WRITE_REG(hw, E1000_TXCW, txcw);
5207 
5208 			/*
5209 			 * Write 0x410 to Serdes Control register
5210 			 * to enable Serdes analog loopback
5211 			 */
5212 			E1000_WRITE_REG(hw, E1000_SCTL, 0x0410);
5213 			msec_delay(10);
5214 			break;
5215 		default:
5216 			break;
5217 		}
5218 		break;
5219 	case e1000_82574:
5220 	case e1000_80003es2lan:
5221 	case e1000_ich9lan:
5222 	case e1000_ich10lan:
5223 		(void) e1000_read_phy_reg(hw, GG82563_REG(6, 16), &phydata);
5224 		(void) e1000_write_phy_reg(hw, GG82563_REG(6, 16),
5225 		    phydata | (1 << 5));
5226 		Adapter->param_adv_autoneg = 1;
5227 		Adapter->param_adv_1000fdx = 1;
5228 		(void) e1000g_reset_link(Adapter);
5229 		break;
5230 	}
5231 }
5232 
5233 static void
5234 e1000g_set_external_loopback_100(struct e1000g *Adapter)
5235 {
5236 	struct e1000_hw *hw;
5237 	uint32_t ctrl;
5238 	uint16_t phy_ctrl;
5239 
5240 	hw = &Adapter->shared;
5241 
5242 	/* Disable Smart Power Down */
5243 	phy_spd_state(hw, B_FALSE);
5244 
5245 	phy_ctrl = (MII_CR_FULL_DUPLEX |
5246 	    MII_CR_SPEED_100);
5247 
5248 	/* Force 100/FD, reset PHY */
5249 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
5250 	    phy_ctrl | MII_CR_RESET);	/* 0xA100 */
5251 	msec_delay(10);
5252 
5253 	/* Force 100/FD */
5254 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
5255 	    phy_ctrl);			/* 0x2100 */
5256 	msec_delay(10);
5257 
5258 	/* Now setup the MAC to the same speed/duplex as the PHY. */
5259 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
5260 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
5261 	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
5262 	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
5263 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
5264 	    E1000_CTRL_SPD_100 |	/* Force Speed to 100 */
5265 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
5266 
5267 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
5268 }
5269 
5270 static void
5271 e1000g_set_external_loopback_10(struct e1000g *Adapter)
5272 {
5273 	struct e1000_hw *hw;
5274 	uint32_t ctrl;
5275 	uint16_t phy_ctrl;
5276 
5277 	hw = &Adapter->shared;
5278 
5279 	/* Disable Smart Power Down */
5280 	phy_spd_state(hw, B_FALSE);
5281 
5282 	phy_ctrl = (MII_CR_FULL_DUPLEX |
5283 	    MII_CR_SPEED_10);
5284 
5285 	/* Force 10/FD, reset PHY */
5286 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
5287 	    phy_ctrl | MII_CR_RESET);	/* 0x8100 */
5288 	msec_delay(10);
5289 
5290 	/* Force 10/FD */
5291 	(void) e1000_write_phy_reg(hw, PHY_CONTROL,
5292 	    phy_ctrl);			/* 0x0100 */
5293 	msec_delay(10);
5294 
5295 	/* Now setup the MAC to the same speed/duplex as the PHY. */
5296 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
5297 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
5298 	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
5299 	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
5300 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
5301 	    E1000_CTRL_SPD_10 |		/* Force Speed to 10 */
5302 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
5303 
5304 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
5305 }
5306 
5307 #ifdef __sparc
5308 static boolean_t
5309 e1000g_find_mac_address(struct e1000g *Adapter)
5310 {
5311 	struct e1000_hw *hw = &Adapter->shared;
5312 	uchar_t *bytes;
5313 	struct ether_addr sysaddr;
5314 	uint_t nelts;
5315 	int err;
5316 	boolean_t found = B_FALSE;
5317 
5318 	/*
5319 	 * The "vendor's factory-set address" may already have
5320 	 * been extracted from the chip, but if the property
5321 	 * "local-mac-address" is set we use that instead.
5322 	 *
5323 	 * We check whether it looks like an array of 6
5324 	 * bytes (which it should, if OBP set it).  If we can't
5325 	 * make sense of it this way, we'll ignore it.
5326 	 */
5327 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
5328 	    DDI_PROP_DONTPASS, "local-mac-address", &bytes, &nelts);
5329 	if (err == DDI_PROP_SUCCESS) {
5330 		if (nelts == ETHERADDRL) {
5331 			while (nelts--)
5332 				hw->mac.addr[nelts] = bytes[nelts];
5333 			found = B_TRUE;
5334 		}
5335 		ddi_prop_free(bytes);
5336 	}
5337 
5338 	/*
5339 	 * Look up the OBP property "local-mac-address?". If the user has set
5340 	 * 'local-mac-address? = false', use "the system address" instead.
5341 	 */
5342 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip, 0,
5343 	    "local-mac-address?", &bytes, &nelts) == DDI_PROP_SUCCESS) {
5344 		if (strncmp("false", (caddr_t)bytes, (size_t)nelts) == 0) {
5345 			if (localetheraddr(NULL, &sysaddr) != 0) {
5346 				bcopy(&sysaddr, hw->mac.addr, ETHERADDRL);
5347 				found = B_TRUE;
5348 			}
5349 		}
5350 		ddi_prop_free(bytes);
5351 	}
5352 
5353 	/*
5354 	 * Finally(!), if there's a valid "mac-address" property (created
5355 	 * if we netbooted from this interface), we must use this instead
5356 	 * of any of the above to ensure that the NFS/install server doesn't
5357 	 * get confused by the address changing as Solaris takes over!
5358 	 */
5359 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
5360 	    DDI_PROP_DONTPASS, "mac-address", &bytes, &nelts);
5361 	if (err == DDI_PROP_SUCCESS) {
5362 		if (nelts == ETHERADDRL) {
5363 			while (nelts--)
5364 				hw->mac.addr[nelts] = bytes[nelts];
5365 			found = B_TRUE;
5366 		}
5367 		ddi_prop_free(bytes);
5368 	}
5369 
5370 	if (found) {
5371 		bcopy(hw->mac.addr, hw->mac.perm_addr,
5372 		    ETHERADDRL);
5373 	}
5374 
5375 	return (found);
5376 }
5377 #endif
5378 
5379 static int
5380 e1000g_add_intrs(struct e1000g *Adapter)
5381 {
5382 	dev_info_t *devinfo;
5383 	int intr_types;
5384 	int rc;
5385 
5386 	devinfo = Adapter->dip;
5387 
5388 	/* Get supported interrupt types */
5389 	rc = ddi_intr_get_supported_types(devinfo, &intr_types);
5390 
5391 	if (rc != DDI_SUCCESS) {
5392 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
5393 		    "Get supported interrupt types failed: %d\n", rc);
5394 		return (DDI_FAILURE);
5395 	}
5396 
5397 	/*
5398 	 * Based on Intel Technical Advisory document (TA-160), there are some
5399 	 * cases where some older Intel PCI-X NICs may "advertise" to the OS
5400 	 * that it supports MSI, but in fact has problems.
5401 	 * So we should only enable MSI for PCI-E NICs and disable MSI for old
5402 	 * PCI/PCI-X NICs.
5403 	 */
5404 	if (Adapter->shared.mac.type < e1000_82571)
5405 		Adapter->msi_enable = B_FALSE;
5406 
5407 	if ((intr_types & DDI_INTR_TYPE_MSI) && Adapter->msi_enable) {
5408 		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_MSI);
5409 
5410 		if (rc != DDI_SUCCESS) {
5411 			E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
5412 			    "Add MSI failed, trying Legacy interrupts\n");
5413 		} else {
5414 			Adapter->intr_type = DDI_INTR_TYPE_MSI;
5415 		}
5416 	}
5417 
5418 	if ((Adapter->intr_type == 0) &&
5419 	    (intr_types & DDI_INTR_TYPE_FIXED)) {
5420 		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_FIXED);
5421 
5422 		if (rc != DDI_SUCCESS) {
5423 			E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
5424 			    "Add Legacy interrupts failed\n");
5425 			return (DDI_FAILURE);
5426 		}
5427 
5428 		Adapter->intr_type = DDI_INTR_TYPE_FIXED;
5429 	}
5430 
5431 	if (Adapter->intr_type == 0) {
5432 		E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
5433 		    "No interrupts registered\n");
5434 		return (DDI_FAILURE);
5435 	}
5436 
5437 	return (DDI_SUCCESS);
5438 }
5439 
5440 /*
5441  * e1000g_intr_add() handles MSI/Legacy interrupts
5442  */
5443 static int
5444 e1000g_intr_add(struct e1000g *Adapter, int intr_type)
5445 {
5446 	dev_info_t *devinfo;
5447 	int count, avail, actual;
5448 	int x, y, rc, inum = 0;
5449 	int flag;
5450 	ddi_intr_handler_t *intr_handler;
5451 
5452 	devinfo = Adapter->dip;
5453 
5454 	/* get number of interrupts */
5455 	rc = ddi_intr_get_nintrs(devinfo, intr_type, &count);
5456 	if ((rc != DDI_SUCCESS) || (count == 0)) {
5457 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
5458 		    "Get interrupt number failed. Return: %d, count: %d\n",
5459 		    rc, count);
5460 		return (DDI_FAILURE);
5461 	}
5462 
5463 	/* get number of available interrupts */
5464 	rc = ddi_intr_get_navail(devinfo, intr_type, &avail);
5465 	if ((rc != DDI_SUCCESS) || (avail == 0)) {
5466 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
5467 		    "Get interrupt available number failed. "
5468 		    "Return: %d, available: %d\n", rc, avail);
5469 		return (DDI_FAILURE);
5470 	}
5471 
5472 	if (avail < count) {
5473 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
5474 		    "Interrupts count: %d, available: %d\n",
5475 		    count, avail);
5476 	}
5477 
5478 	/* Allocate an array of interrupt handles */
5479 	Adapter->intr_size = count * sizeof (ddi_intr_handle_t);
5480 	Adapter->htable = kmem_alloc(Adapter->intr_size, KM_SLEEP);
5481 
5482 	/* Set NORMAL behavior for both MSI and FIXED interrupt */
5483 	flag = DDI_INTR_ALLOC_NORMAL;
5484 
5485 	/* call ddi_intr_alloc() */
5486 	rc = ddi_intr_alloc(devinfo, Adapter->htable, intr_type, inum,
5487 	    count, &actual, flag);
5488 
5489 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
5490 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
5491 		    "Allocate interrupts failed: %d\n", rc);
5492 
5493 		kmem_free(Adapter->htable, Adapter->intr_size);
5494 		return (DDI_FAILURE);
5495 	}
5496 
5497 	if (actual < count) {
5498 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
5499 		    "Interrupts requested: %d, received: %d\n",
5500 		    count, actual);
5501 	}
5502 
5503 	Adapter->intr_cnt = actual;
5504 
5505 	/* Get priority for first msi, assume remaining are all the same */
5506 	rc = ddi_intr_get_pri(Adapter->htable[0], &Adapter->intr_pri);
5507 
5508 	if (rc != DDI_SUCCESS) {
5509 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
5510 		    "Get interrupt priority failed: %d\n", rc);
5511 
5512 		/* Free already allocated intr */
5513 		for (y = 0; y < actual; y++)
5514 			(void) ddi_intr_free(Adapter->htable[y]);
5515 
5516 		kmem_free(Adapter->htable, Adapter->intr_size);
5517 		return (DDI_FAILURE);
5518 	}
5519 
5520 	/*
5521 	 * In Legacy Interrupt mode, for PCI-Express adapters, we should
5522 	 * use the interrupt service routine e1000g_intr_pciexpress()
5523 	 * to avoid interrupt stealing when sharing interrupt with other
5524 	 * devices.
5525 	 */
5526 	if (Adapter->shared.mac.type < e1000_82571)
5527 		intr_handler = (ddi_intr_handler_t *)e1000g_intr;
5528 	else
5529 		intr_handler = (ddi_intr_handler_t *)e1000g_intr_pciexpress;
5530 
5531 	/* Call ddi_intr_add_handler() */
5532 	for (x = 0; x < actual; x++) {
5533 		rc = ddi_intr_add_handler(Adapter->htable[x],
5534 		    intr_handler, (caddr_t)Adapter, NULL);
5535 
5536 		if (rc != DDI_SUCCESS) {
5537 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
5538 			    "Add interrupt handler failed: %d\n", rc);
5539 
5540 			/* Remove already added handler */
5541 			for (y = 0; y < x; y++)
5542 				(void) ddi_intr_remove_handler(
5543 				    Adapter->htable[y]);
5544 
5545 			/* Free already allocated intr */
5546 			for (y = 0; y < actual; y++)
5547 				(void) ddi_intr_free(Adapter->htable[y]);
5548 
5549 			kmem_free(Adapter->htable, Adapter->intr_size);
5550 			return (DDI_FAILURE);
5551 		}
5552 	}
5553 
5554 	rc = ddi_intr_get_cap(Adapter->htable[0], &Adapter->intr_cap);
5555 
5556 	if (rc != DDI_SUCCESS) {
5557 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
5558 		    "Get interrupt cap failed: %d\n", rc);
5559 
5560 		/* Free already allocated intr */
5561 		for (y = 0; y < actual; y++) {
5562 			(void) ddi_intr_remove_handler(Adapter->htable[y]);
5563 			(void) ddi_intr_free(Adapter->htable[y]);
5564 		}
5565 
5566 		kmem_free(Adapter->htable, Adapter->intr_size);
5567 		return (DDI_FAILURE);
5568 	}
5569 
5570 	return (DDI_SUCCESS);
5571 }
5572 
5573 static int
5574 e1000g_rem_intrs(struct e1000g *Adapter)
5575 {
5576 	int x;
5577 	int rc;
5578 
5579 	for (x = 0; x < Adapter->intr_cnt; x++) {
5580 		rc = ddi_intr_remove_handler(Adapter->htable[x]);
5581 		if (rc != DDI_SUCCESS) {
5582 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
5583 			    "Remove intr handler failed: %d\n", rc);
5584 			return (DDI_FAILURE);
5585 		}
5586 
5587 		rc = ddi_intr_free(Adapter->htable[x]);
5588 		if (rc != DDI_SUCCESS) {
5589 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
5590 			    "Free intr failed: %d\n", rc);
5591 			return (DDI_FAILURE);
5592 		}
5593 	}
5594 
5595 	kmem_free(Adapter->htable, Adapter->intr_size);
5596 
5597 	return (DDI_SUCCESS);
5598 }
5599 
5600 static int
5601 e1000g_enable_intrs(struct e1000g *Adapter)
5602 {
5603 	int x;
5604 	int rc;
5605 
5606 	/* Enable interrupts */
5607 	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
5608 		/* Call ddi_intr_block_enable() for MSI */
5609 		rc = ddi_intr_block_enable(Adapter->htable,
5610 		    Adapter->intr_cnt);
5611 		if (rc != DDI_SUCCESS) {
5612 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
5613 			    "Enable block intr failed: %d\n", rc);
5614 			return (DDI_FAILURE);
5615 		}
5616 	} else {
5617 		/* Call ddi_intr_enable() for Legacy/MSI non block enable */
5618 		for (x = 0; x < Adapter->intr_cnt; x++) {
5619 			rc = ddi_intr_enable(Adapter->htable[x]);
5620 			if (rc != DDI_SUCCESS) {
5621 				E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
5622 				    "Enable intr failed: %d\n", rc);
5623 				return (DDI_FAILURE);
5624 			}
5625 		}
5626 	}
5627 
5628 	return (DDI_SUCCESS);
5629 }
5630 
5631 static int
5632 e1000g_disable_intrs(struct e1000g *Adapter)
5633 {
5634 	int x;
5635 	int rc;
5636 
5637 	/* Disable all interrupts */
5638 	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
5639 		rc = ddi_intr_block_disable(Adapter->htable,
5640 		    Adapter->intr_cnt);
5641 		if (rc != DDI_SUCCESS) {
5642 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
5643 			    "Disable block intr failed: %d\n", rc);
5644 			return (DDI_FAILURE);
5645 		}
5646 	} else {
5647 		for (x = 0; x < Adapter->intr_cnt; x++) {
5648 			rc = ddi_intr_disable(Adapter->htable[x]);
5649 			if (rc != DDI_SUCCESS) {
5650 				E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
5651 				    "Disable intr failed: %d\n", rc);
5652 				return (DDI_FAILURE);
5653 			}
5654 		}
5655 	}
5656 
5657 	return (DDI_SUCCESS);
5658 }
5659 
5660 /*
5661  * e1000g_get_phy_state - get the state of PHY registers, save in the adapter
5662  */
5663 static void
5664 e1000g_get_phy_state(struct e1000g *Adapter)
5665 {
5666 	struct e1000_hw *hw = &Adapter->shared;
5667 
5668 	(void) e1000_read_phy_reg(hw, PHY_CONTROL, &Adapter->phy_ctrl);
5669 	(void) e1000_read_phy_reg(hw, PHY_STATUS, &Adapter->phy_status);
5670 	(void) e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &Adapter->phy_an_adv);
5671 	(void) e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &Adapter->phy_an_exp);
5672 	(void) e1000_read_phy_reg(hw, PHY_EXT_STATUS, &Adapter->phy_ext_status);
5673 	(void) e1000_read_phy_reg(hw, PHY_1000T_CTRL, &Adapter->phy_1000t_ctrl);
5674 	(void) e1000_read_phy_reg(hw, PHY_1000T_STATUS,
5675 	    &Adapter->phy_1000t_status);
5676 	(void) e1000_read_phy_reg(hw, PHY_LP_ABILITY, &Adapter->phy_lp_able);
5677 
5678 	Adapter->param_autoneg_cap =
5679 	    (Adapter->phy_status & MII_SR_AUTONEG_CAPS) ? 1 : 0;
5680 	Adapter->param_pause_cap =
5681 	    (Adapter->phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
5682 	Adapter->param_asym_pause_cap =
5683 	    (Adapter->phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
5684 	Adapter->param_1000fdx_cap =
5685 	    ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
5686 	    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS)) ? 1 : 0;
5687 	Adapter->param_1000hdx_cap =
5688 	    ((Adapter->phy_ext_status & IEEE_ESR_1000T_HD_CAPS) ||
5689 	    (Adapter->phy_ext_status & IEEE_ESR_1000X_HD_CAPS)) ? 1 : 0;
5690 	Adapter->param_100t4_cap =
5691 	    (Adapter->phy_status & MII_SR_100T4_CAPS) ? 1 : 0;
5692 	Adapter->param_100fdx_cap =
5693 	    ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
5694 	    (Adapter->phy_status & MII_SR_100T2_FD_CAPS)) ? 1 : 0;
5695 	Adapter->param_100hdx_cap =
5696 	    ((Adapter->phy_status & MII_SR_100X_HD_CAPS) ||
5697 	    (Adapter->phy_status & MII_SR_100T2_HD_CAPS)) ? 1 : 0;
5698 	Adapter->param_10fdx_cap =
5699 	    (Adapter->phy_status & MII_SR_10T_FD_CAPS) ? 1 : 0;
5700 	Adapter->param_10hdx_cap =
5701 	    (Adapter->phy_status & MII_SR_10T_HD_CAPS) ? 1 : 0;
5702 
5703 	Adapter->param_adv_autoneg = hw->mac.autoneg;
5704 	Adapter->param_adv_pause =
5705 	    (Adapter->phy_an_adv & NWAY_AR_PAUSE) ? 1 : 0;
5706 	Adapter->param_adv_asym_pause =
5707 	    (Adapter->phy_an_adv & NWAY_AR_ASM_DIR) ? 1 : 0;
5708 	Adapter->param_adv_1000hdx =
5709 	    (Adapter->phy_1000t_ctrl & CR_1000T_HD_CAPS) ? 1 : 0;
5710 	Adapter->param_adv_100t4 =
5711 	    (Adapter->phy_an_adv & NWAY_AR_100T4_CAPS) ? 1 : 0;
5712 	if (Adapter->param_adv_autoneg == 1) {
5713 		Adapter->param_adv_1000fdx =
5714 		    (Adapter->phy_1000t_ctrl & CR_1000T_FD_CAPS) ? 1 : 0;
5715 		Adapter->param_adv_100fdx =
5716 		    (Adapter->phy_an_adv & NWAY_AR_100TX_FD_CAPS) ? 1 : 0;
5717 		Adapter->param_adv_100hdx =
5718 		    (Adapter->phy_an_adv & NWAY_AR_100TX_HD_CAPS) ? 1 : 0;
5719 		Adapter->param_adv_10fdx =
5720 		    (Adapter->phy_an_adv & NWAY_AR_10T_FD_CAPS) ? 1 : 0;
5721 		Adapter->param_adv_10hdx =
5722 		    (Adapter->phy_an_adv & NWAY_AR_10T_HD_CAPS) ? 1 : 0;
5723 	}
5724 
5725 	Adapter->param_lp_autoneg =
5726 	    (Adapter->phy_an_exp & NWAY_ER_LP_NWAY_CAPS) ? 1 : 0;
5727 	Adapter->param_lp_pause =
5728 	    (Adapter->phy_lp_able & NWAY_LPAR_PAUSE) ? 1 : 0;
5729 	Adapter->param_lp_asym_pause =
5730 	    (Adapter->phy_lp_able & NWAY_LPAR_ASM_DIR) ? 1 : 0;
5731 	Adapter->param_lp_1000fdx =
5732 	    (Adapter->phy_1000t_status & SR_1000T_LP_FD_CAPS) ? 1 : 0;
5733 	Adapter->param_lp_1000hdx =
5734 	    (Adapter->phy_1000t_status & SR_1000T_LP_HD_CAPS) ? 1 : 0;
5735 	Adapter->param_lp_100t4 =
5736 	    (Adapter->phy_lp_able & NWAY_LPAR_100T4_CAPS) ? 1 : 0;
5737 	Adapter->param_lp_100fdx =
5738 	    (Adapter->phy_lp_able & NWAY_LPAR_100TX_FD_CAPS) ? 1 : 0;
5739 	Adapter->param_lp_100hdx =
5740 	    (Adapter->phy_lp_able & NWAY_LPAR_100TX_HD_CAPS) ? 1 : 0;
5741 	Adapter->param_lp_10fdx =
5742 	    (Adapter->phy_lp_able & NWAY_LPAR_10T_FD_CAPS) ? 1 : 0;
5743 	Adapter->param_lp_10hdx =
5744 	    (Adapter->phy_lp_able & NWAY_LPAR_10T_HD_CAPS) ? 1 : 0;
5745 }
5746 
5747 /*
5748  * FMA support
5749  */
5750 
5751 int
5752 e1000g_check_acc_handle(ddi_acc_handle_t handle)
5753 {
5754 	ddi_fm_error_t de;
5755 
5756 	ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
5757 	ddi_fm_acc_err_clear(handle, DDI_FME_VERSION);
5758 	return (de.fme_status);
5759 }
5760 
5761 int
5762 e1000g_check_dma_handle(ddi_dma_handle_t handle)
5763 {
5764 	ddi_fm_error_t de;
5765 
5766 	ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
5767 	return (de.fme_status);
5768 }
5769 
5770 /*
5771  * The IO fault service error handling callback function
5772  */
5773 /* ARGSUSED2 */
5774 static int
5775 e1000g_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
5776 {
5777 	/*
5778 	 * as the driver can always deal with an error in any dma or
5779 	 * access handle, we can just return the fme_status value.
5780 	 */
5781 	pci_ereport_post(dip, err, NULL);
5782 	return (err->fme_status);
5783 }
5784 
5785 static void
5786 e1000g_fm_init(struct e1000g *Adapter)
5787 {
5788 	ddi_iblock_cookie_t iblk;
5789 	int fma_acc_flag, fma_dma_flag;
5790 
5791 	/* Only register with IO Fault Services if we have some capability */
5792 	if (Adapter->fm_capabilities & DDI_FM_ACCCHK_CAPABLE) {
5793 		e1000g_regs_acc_attr.devacc_attr_access = DDI_FLAGERR_ACC;
5794 		fma_acc_flag = 1;
5795 	} else {
5796 		e1000g_regs_acc_attr.devacc_attr_access = DDI_DEFAULT_ACC;
5797 		fma_acc_flag = 0;
5798 	}
5799 
5800 	if (Adapter->fm_capabilities & DDI_FM_DMACHK_CAPABLE) {
5801 		fma_dma_flag = 1;
5802 	} else {
5803 		fma_dma_flag = 0;
5804 	}
5805 
5806 	(void) e1000g_set_fma_flags(Adapter, fma_acc_flag, fma_dma_flag);
5807 
5808 	if (Adapter->fm_capabilities) {
5809 
5810 		/* Register capabilities with IO Fault Services */
5811 		ddi_fm_init(Adapter->dip, &Adapter->fm_capabilities, &iblk);
5812 
5813 		/*
5814 		 * Initialize pci ereport capabilities if ereport capable
5815 		 */
5816 		if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities) ||
5817 		    DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
5818 			pci_ereport_setup(Adapter->dip);
5819 
5820 		/*
5821 		 * Register error callback if error callback capable
5822 		 */
5823 		if (DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
5824 			ddi_fm_handler_register(Adapter->dip,
5825 			    e1000g_fm_error_cb, (void*) Adapter);
5826 	}
5827 }
5828 
5829 static void
5830 e1000g_fm_fini(struct e1000g *Adapter)
5831 {
5832 	/* Only unregister FMA capabilities if we registered some */
5833 	if (Adapter->fm_capabilities) {
5834 
5835 		/*
5836 		 * Release any resources allocated by pci_ereport_setup()
5837 		 */
5838 		if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities) ||
5839 		    DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
5840 			pci_ereport_teardown(Adapter->dip);
5841 
5842 		/*
5843 		 * Un-register error callback if error callback capable
5844 		 */
5845 		if (DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
5846 			ddi_fm_handler_unregister(Adapter->dip);
5847 
5848 		/* Unregister from IO Fault Services */
5849 		mutex_enter(&e1000g_rx_detach_lock);
5850 		ddi_fm_fini(Adapter->dip);
5851 		if (Adapter->priv_dip != NULL) {
5852 			DEVI(Adapter->priv_dip)->devi_fmhdl = NULL;
5853 		}
5854 		mutex_exit(&e1000g_rx_detach_lock);
5855 	}
5856 }
5857 
5858 void
5859 e1000g_fm_ereport(struct e1000g *Adapter, char *detail)
5860 {
5861 	uint64_t ena;
5862 	char buf[FM_MAX_CLASS];
5863 
5864 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
5865 	ena = fm_ena_generate(0, FM_ENA_FMT1);
5866 	if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities)) {
5867 		ddi_fm_ereport_post(Adapter->dip, buf, ena, DDI_NOSLEEP,
5868 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, NULL);
5869 	}
5870 }
5871 
5872 /*
5873  * quiesce(9E) entry point.
5874  *
5875  * This function is called when the system is single-threaded at high
5876  * PIL with preemption disabled. Therefore, this function must not be
5877  * blocked.
5878  *
5879  * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
5880  * DDI_FAILURE indicates an error condition and should almost never happen.
5881  */
5882 static int
5883 e1000g_quiesce(dev_info_t *devinfo)
5884 {
5885 	struct e1000g *Adapter;
5886 
5887 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
5888 
5889 	if (Adapter == NULL)
5890 		return (DDI_FAILURE);
5891 
5892 	e1000g_clear_all_interrupts(Adapter);
5893 
5894 	(void) e1000_reset_hw(&Adapter->shared);
5895 
5896 	/* Setup our HW Tx Head & Tail descriptor pointers */
5897 	E1000_WRITE_REG(&Adapter->shared, E1000_TDH(0), 0);
5898 	E1000_WRITE_REG(&Adapter->shared, E1000_TDT(0), 0);
5899 
5900 	/* Setup our HW Rx Head & Tail descriptor pointers */
5901 	E1000_WRITE_REG(&Adapter->shared, E1000_RDH(0), 0);
5902 	E1000_WRITE_REG(&Adapter->shared, E1000_RDT(0), 0);
5903 
5904 	return (DDI_SUCCESS);
5905 }
5906 
5907 static int
5908 e1000g_get_def_val(struct e1000g *Adapter, mac_prop_id_t pr_num,
5909     uint_t pr_valsize, void *pr_val)
5910 {
5911 	link_flowctrl_t fl;
5912 	int err = 0;
5913 
5914 	ASSERT(pr_valsize > 0);
5915 	switch (pr_num) {
5916 	case MAC_PROP_AUTONEG:
5917 		*(uint8_t *)pr_val =
5918 		    ((Adapter->phy_status & MII_SR_AUTONEG_CAPS) ? 1 : 0);
5919 		break;
5920 	case MAC_PROP_FLOWCTRL:
5921 		if (pr_valsize < sizeof (link_flowctrl_t))
5922 			return (EINVAL);
5923 		fl = LINK_FLOWCTRL_BI;
5924 		bcopy(&fl, pr_val, sizeof (fl));
5925 		break;
5926 	case MAC_PROP_ADV_1000FDX_CAP:
5927 	case MAC_PROP_EN_1000FDX_CAP:
5928 		*(uint8_t *)pr_val =
5929 		    ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
5930 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS)) ? 1 : 0;
5931 		break;
5932 	case MAC_PROP_ADV_1000HDX_CAP:
5933 	case MAC_PROP_EN_1000HDX_CAP:
5934 		*(uint8_t *)pr_val =
5935 		    ((Adapter->phy_ext_status & IEEE_ESR_1000T_HD_CAPS) ||
5936 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_HD_CAPS)) ? 1 : 0;
5937 		break;
5938 	case MAC_PROP_ADV_100FDX_CAP:
5939 	case MAC_PROP_EN_100FDX_CAP:
5940 		*(uint8_t *)pr_val =
5941 		    ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
5942 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS)) ? 1 : 0;
5943 		break;
5944 	case MAC_PROP_ADV_100HDX_CAP:
5945 	case MAC_PROP_EN_100HDX_CAP:
5946 		*(uint8_t *)pr_val =
5947 		    ((Adapter->phy_status & MII_SR_100X_HD_CAPS) ||
5948 		    (Adapter->phy_status & MII_SR_100T2_HD_CAPS)) ? 1 : 0;
5949 		break;
5950 	case MAC_PROP_ADV_10FDX_CAP:
5951 	case MAC_PROP_EN_10FDX_CAP:
5952 		*(uint8_t *)pr_val =
5953 		    (Adapter->phy_status & MII_SR_10T_FD_CAPS) ? 1 : 0;
5954 		break;
5955 	case MAC_PROP_ADV_10HDX_CAP:
5956 	case MAC_PROP_EN_10HDX_CAP:
5957 		*(uint8_t *)pr_val =
5958 		    (Adapter->phy_status & MII_SR_10T_HD_CAPS) ? 1 : 0;
5959 		break;
5960 	default:
5961 		err = ENOTSUP;
5962 		break;
5963 	}
5964 	return (err);
5965 }
5966 
5967 /*
5968  * synchronize the adv* and en* parameters.
5969  *
5970  * See comments in <sys/dld.h> for details of the *_en_*
5971  * parameters. The usage of ndd for setting adv parameters will
5972  * synchronize all the en parameters with the e1000g parameters,
5973  * implicitly disabling any settings made via dladm.
5974  */
5975 static void
5976 e1000g_param_sync(struct e1000g *Adapter)
5977 {
5978 	Adapter->param_en_1000fdx = Adapter->param_adv_1000fdx;
5979 	Adapter->param_en_1000hdx = Adapter->param_adv_1000hdx;
5980 	Adapter->param_en_100fdx = Adapter->param_adv_100fdx;
5981 	Adapter->param_en_100hdx = Adapter->param_adv_100hdx;
5982 	Adapter->param_en_10fdx = Adapter->param_adv_10fdx;
5983 	Adapter->param_en_10hdx = Adapter->param_adv_10hdx;
5984 }
5985 
5986 /*
5987  * e1000g_get_driver_control - tell manageability firmware that the driver
5988  * has control.
5989  */
5990 static void
5991 e1000g_get_driver_control(struct e1000_hw *hw)
5992 {
5993 	uint32_t ctrl_ext;
5994 	uint32_t swsm;
5995 
5996 	/* tell manageability firmware the driver has taken over */
5997 	switch (hw->mac.type) {
5998 	case e1000_82573:
5999 		swsm = E1000_READ_REG(hw, E1000_SWSM);
6000 		E1000_WRITE_REG(hw, E1000_SWSM, swsm | E1000_SWSM_DRV_LOAD);
6001 		break;
6002 	case e1000_82571:
6003 	case e1000_82572:
6004 	case e1000_82574:
6005 	case e1000_80003es2lan:
6006 	case e1000_ich8lan:
6007 	case e1000_ich9lan:
6008 	case e1000_ich10lan:
6009 		ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
6010 		E1000_WRITE_REG(hw, E1000_CTRL_EXT,
6011 		    ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
6012 		break;
6013 	default:
6014 		/* no manageability firmware: do nothing */
6015 		break;
6016 	}
6017 }
6018 
6019 /*
6020  * e1000g_release_driver_control - tell manageability firmware that the driver
6021  * has released control.
6022  */
6023 static void
6024 e1000g_release_driver_control(struct e1000_hw *hw)
6025 {
6026 	uint32_t ctrl_ext;
6027 	uint32_t swsm;
6028 
6029 	/* tell manageability firmware the driver has released control */
6030 	switch (hw->mac.type) {
6031 	case e1000_82573:
6032 		swsm = E1000_READ_REG(hw, E1000_SWSM);
6033 		E1000_WRITE_REG(hw, E1000_SWSM, swsm & ~E1000_SWSM_DRV_LOAD);
6034 		break;
6035 	case e1000_82571:
6036 	case e1000_82572:
6037 	case e1000_82574:
6038 	case e1000_80003es2lan:
6039 	case e1000_ich8lan:
6040 	case e1000_ich9lan:
6041 	case e1000_ich10lan:
6042 		ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
6043 		E1000_WRITE_REG(hw, E1000_CTRL_EXT,
6044 		    ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
6045 		break;
6046 	default:
6047 		/* no manageability firmware: do nothing */
6048 		break;
6049 	}
6050 }
6051 
6052 /*
6053  * Restore e1000g promiscuous mode.
6054  */
6055 static void
6056 e1000g_restore_promisc(struct e1000g *Adapter)
6057 {
6058 	if (Adapter->e1000g_promisc) {
6059 		uint32_t rctl;
6060 
6061 		rctl = E1000_READ_REG(&Adapter->shared, E1000_RCTL);
6062 		rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_BAM);
6063 		E1000_WRITE_REG(&Adapter->shared, E1000_RCTL, rctl);
6064 	}
6065 }
6066