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 - 2008 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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms of the CDDLv1.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * **********************************************************************
30  *									*
31  * Module Name:								*
32  *   e1000g_main.c							*
33  *									*
34  * Abstract:								*
35  *   This file contains the interface routines for the solaris OS.	*
36  *   It has all DDI entry point routines and GLD entry point routines.	*
37  *									*
38  *   This file also contains routines that take care of initialization	*
39  *   uninit routine and interrupt routine.				*
40  *									*
41  * **********************************************************************
42  */
43 
44 #include <sys/dlpi.h>
45 #include <sys/mac.h>
46 #include "e1000g_sw.h"
47 #include "e1000g_debug.h"
48 
49 static char ident[] = "Intel PRO/1000 Ethernet 5.2.6";
50 static char e1000g_string[] = "Intel(R) PRO/1000 Network Connection";
51 static char e1000g_version[] = "Driver Ver. 5.2.6";
52 
53 /*
54  * Proto types for DDI entry points
55  */
56 static int e1000g_attach(dev_info_t *, ddi_attach_cmd_t);
57 static int e1000g_detach(dev_info_t *, ddi_detach_cmd_t);
58 
59 /*
60  * init and intr routines prototype
61  */
62 static int e1000g_resume(dev_info_t *);
63 static int e1000g_suspend(dev_info_t *);
64 static uint_t e1000g_intr_pciexpress(caddr_t);
65 static uint_t e1000g_intr(caddr_t);
66 static void e1000g_intr_work(struct e1000g *, uint32_t);
67 #pragma inline(e1000g_intr_work)
68 static uint32_t e1000g_get_itr(uint32_t, uint32_t, uint32_t);
69 #pragma inline(e1000g_get_itr)
70 static int e1000g_init(struct e1000g *);
71 static int e1000g_start(struct e1000g *, boolean_t);
72 static void e1000g_stop(struct e1000g *, boolean_t);
73 static int e1000g_m_start(void *);
74 static void e1000g_m_stop(void *);
75 static int e1000g_m_promisc(void *, boolean_t);
76 static boolean_t e1000g_m_getcapab(void *, mac_capab_t, void *);
77 static int e1000g_m_unicst(void *, const uint8_t *);
78 static int e1000g_m_unicst_add(void *, mac_multi_addr_t *);
79 static int e1000g_m_unicst_remove(void *, mac_addr_slot_t);
80 static int e1000g_m_unicst_modify(void *, mac_multi_addr_t *);
81 static int e1000g_m_unicst_get(void *, mac_multi_addr_t *);
82 static int e1000g_m_multicst(void *, boolean_t, const uint8_t *);
83 static void e1000g_m_ioctl(void *, queue_t *, mblk_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 int e1000g_register_mac(struct e1000g *);
90 static boolean_t e1000g_rx_drain(struct e1000g *);
91 static boolean_t e1000g_tx_drain(struct e1000g *);
92 static void e1000g_init_unicst(struct e1000g *);
93 static int e1000g_unicst_set(struct e1000g *, const uint8_t *, mac_addr_slot_t);
94 
95 /*
96  * Local routines
97  */
98 static void e1000g_tx_clean(struct e1000g *);
99 static void e1000g_rx_clean(struct e1000g *);
100 static void e1000g_link_timer(void *);
101 static void e1000g_local_timer(void *);
102 static boolean_t e1000g_link_check(struct e1000g *);
103 static boolean_t e1000g_stall_check(struct e1000g *);
104 static void e1000g_smartspeed(struct e1000g *);
105 static void e1000g_get_conf(struct e1000g *);
106 static int e1000g_get_prop(struct e1000g *, char *, int, int, int);
107 static void enable_watchdog_timer(struct e1000g *);
108 static void disable_watchdog_timer(struct e1000g *);
109 static void start_watchdog_timer(struct e1000g *);
110 static void restart_watchdog_timer(struct e1000g *);
111 static void stop_watchdog_timer(struct e1000g *);
112 static void stop_link_timer(struct e1000g *);
113 static void stop_82547_timer(e1000g_tx_ring_t *);
114 static void e1000g_force_speed_duplex(struct e1000g *);
115 static void e1000g_get_max_frame_size(struct e1000g *);
116 static boolean_t is_valid_mac_addr(uint8_t *);
117 static void e1000g_unattach(dev_info_t *, struct e1000g *);
118 #ifdef E1000G_DEBUG
119 static void e1000g_ioc_peek_reg(struct e1000g *, e1000g_peekpoke_t *);
120 static void e1000g_ioc_poke_reg(struct e1000g *, e1000g_peekpoke_t *);
121 static void e1000g_ioc_peek_mem(struct e1000g *, e1000g_peekpoke_t *);
122 static void e1000g_ioc_poke_mem(struct e1000g *, e1000g_peekpoke_t *);
123 static enum ioc_reply e1000g_pp_ioctl(struct e1000g *,
124     struct iocblk *, mblk_t *);
125 #endif
126 static enum ioc_reply e1000g_loopback_ioctl(struct e1000g *,
127     struct iocblk *, mblk_t *);
128 static boolean_t e1000g_set_loopback_mode(struct e1000g *, uint32_t);
129 static void e1000g_set_internal_loopback(struct e1000g *);
130 static void e1000g_set_external_loopback_1000(struct e1000g *);
131 static void e1000g_set_external_loopback_100(struct e1000g *);
132 static void e1000g_set_external_loopback_10(struct e1000g *);
133 static int e1000g_add_intrs(struct e1000g *);
134 static int e1000g_intr_add(struct e1000g *, int);
135 static int e1000g_rem_intrs(struct e1000g *);
136 static int e1000g_enable_intrs(struct e1000g *);
137 static int e1000g_disable_intrs(struct e1000g *);
138 static boolean_t e1000g_link_up(struct e1000g *);
139 #ifdef __sparc
140 static boolean_t e1000g_find_mac_address(struct e1000g *);
141 #endif
142 static void e1000g_get_phy_state(struct e1000g *);
143 static void e1000g_free_priv_devi_node(struct e1000g *, boolean_t);
144 static int e1000g_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err,
145     const void *impl_data);
146 static void e1000g_fm_init(struct e1000g *Adapter);
147 static void e1000g_fm_fini(struct e1000g *Adapter);
148 
149 static struct cb_ops cb_ws_ops = {
150 	nulldev,		/* cb_open */
151 	nulldev,		/* cb_close */
152 	nodev,			/* cb_strategy */
153 	nodev,			/* cb_print */
154 	nodev,			/* cb_dump */
155 	nodev,			/* cb_read */
156 	nodev,			/* cb_write */
157 	nodev,			/* cb_ioctl */
158 	nodev,			/* cb_devmap */
159 	nodev,			/* cb_mmap */
160 	nodev,			/* cb_segmap */
161 	nochpoll,		/* cb_chpoll */
162 	ddi_prop_op,		/* cb_prop_op */
163 	NULL,			/* cb_stream */
164 	D_MP | D_HOTPLUG,	/* cb_flag */
165 	CB_REV,			/* cb_rev */
166 	nodev,			/* cb_aread */
167 	nodev			/* cb_awrite */
168 };
169 
170 static struct dev_ops ws_ops = {
171 	DEVO_REV,		/* devo_rev */
172 	0,			/* devo_refcnt */
173 	NULL,			/* devo_getinfo */
174 	nulldev,		/* devo_identify */
175 	nulldev,		/* devo_probe */
176 	e1000g_attach,		/* devo_attach */
177 	e1000g_detach,		/* devo_detach */
178 	nodev,			/* devo_reset */
179 	&cb_ws_ops,		/* devo_cb_ops */
180 	NULL,			/* devo_bus_ops */
181 	ddi_power		/* devo_power */
182 };
183 
184 static struct modldrv modldrv = {
185 	&mod_driverops,		/* Type of module.  This one is a driver */
186 	ident,			/* Discription string */
187 	&ws_ops,		/* driver ops */
188 };
189 
190 static struct modlinkage modlinkage = {
191 	MODREV_1, &modldrv, NULL
192 };
193 
194 /* Access attributes for register mapping */
195 static ddi_device_acc_attr_t e1000g_regs_acc_attr = {
196 	DDI_DEVICE_ATTR_V0,
197 	DDI_STRUCTURE_LE_ACC,
198 	DDI_STRICTORDER_ACC,
199 	DDI_FLAGERR_ACC
200 };
201 
202 #define	E1000G_M_CALLBACK_FLAGS	(MC_IOCTL | MC_GETCAPAB)
203 
204 static mac_callbacks_t e1000g_m_callbacks = {
205 	E1000G_M_CALLBACK_FLAGS,
206 	e1000g_m_stat,
207 	e1000g_m_start,
208 	e1000g_m_stop,
209 	e1000g_m_promisc,
210 	e1000g_m_multicst,
211 	e1000g_m_unicst,
212 	e1000g_m_tx,
213 	NULL,
214 	e1000g_m_ioctl,
215 	e1000g_m_getcapab
216 };
217 
218 /*
219  * Global variables
220  */
221 uint32_t e1000g_mblks_pending = 0;
222 /*
223  * Workaround for Dynamic Reconfiguration support, for x86 platform only.
224  * Here we maintain a private dev_info list if e1000g_force_detach is
225  * enabled. If we force the driver to detach while there are still some
226  * rx buffers retained in the upper layer, we have to keep a copy of the
227  * dev_info. In some cases (Dynamic Reconfiguration), the dev_info data
228  * structure will be freed after the driver is detached. However when we
229  * finally free those rx buffers released by the upper layer, we need to
230  * refer to the dev_info to free the dma buffers. So we save a copy of
231  * the dev_info for this purpose. On x86 platform, we assume this copy
232  * of dev_info is always valid, but on SPARC platform, it could be invalid
233  * after the system board level DR operation. For this reason, the global
234  * variable e1000g_force_detach must be B_FALSE on SPARC platform.
235  */
236 #ifdef __sparc
237 boolean_t e1000g_force_detach = B_FALSE;
238 #else
239 boolean_t e1000g_force_detach = B_TRUE;
240 #endif
241 private_devi_list_t *e1000g_private_devi_list = NULL;
242 
243 /*
244  * The rwlock is defined to protect the whole processing of rx recycling
245  * and the rx packets release in detach processing to make them mutually
246  * exclusive.
247  * The rx recycling processes different rx packets in different threads,
248  * so it will be protected with RW_READER and it won't block any other rx
249  * recycling threads.
250  * While the detach processing will be protected with RW_WRITER to make
251  * it mutually exclusive with the rx recycling.
252  */
253 krwlock_t e1000g_rx_detach_lock;
254 /*
255  * The rwlock e1000g_dma_type_lock is defined to protect the global flag
256  * e1000g_dma_type. For SPARC, the initial value of the flag is "USE_DVMA".
257  * If there are many e1000g instances, the system may run out of DVMA
258  * resources during the initialization of the instances, then the flag will
259  * be changed to "USE_DMA". Because different e1000g instances are initialized
260  * in parallel, we need to use this lock to protect the flag.
261  */
262 krwlock_t e1000g_dma_type_lock;
263 
264 
265 /*
266  * Loadable module configuration entry points for the driver
267  */
268 
269 /*
270  * _init - module initialization
271  */
272 int
273 _init(void)
274 {
275 	int status;
276 
277 	mac_init_ops(&ws_ops, WSNAME);
278 	status = mod_install(&modlinkage);
279 	if (status != DDI_SUCCESS)
280 		mac_fini_ops(&ws_ops);
281 	else {
282 		rw_init(&e1000g_rx_detach_lock, NULL, RW_DRIVER, NULL);
283 		rw_init(&e1000g_dma_type_lock, NULL, RW_DRIVER, NULL);
284 	}
285 
286 	return (status);
287 }
288 
289 /*
290  * _fini - module finalization
291  */
292 int
293 _fini(void)
294 {
295 	int status;
296 
297 	rw_enter(&e1000g_rx_detach_lock, RW_READER);
298 	if (e1000g_mblks_pending != 0) {
299 		rw_exit(&e1000g_rx_detach_lock);
300 		return (EBUSY);
301 	}
302 	rw_exit(&e1000g_rx_detach_lock);
303 
304 	status = mod_remove(&modlinkage);
305 	if (status == DDI_SUCCESS) {
306 		mac_fini_ops(&ws_ops);
307 
308 		if (e1000g_force_detach) {
309 			private_devi_list_t *devi_node;
310 
311 			rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
312 			while (e1000g_private_devi_list != NULL) {
313 				devi_node = e1000g_private_devi_list;
314 				e1000g_private_devi_list =
315 				    e1000g_private_devi_list->next;
316 
317 				kmem_free(devi_node->priv_dip,
318 				    sizeof (struct dev_info));
319 				kmem_free(devi_node,
320 				    sizeof (private_devi_list_t));
321 			}
322 			rw_exit(&e1000g_rx_detach_lock);
323 		}
324 
325 		rw_destroy(&e1000g_rx_detach_lock);
326 		rw_destroy(&e1000g_dma_type_lock);
327 	}
328 
329 	return (status);
330 }
331 
332 /*
333  * _info - module information
334  */
335 int
336 _info(struct modinfo *modinfop)
337 {
338 	return (mod_info(&modlinkage, modinfop));
339 }
340 
341 /*
342  * e1000g_attach - driver attach
343  *
344  * This function is the device-specific initialization entry
345  * point. This entry point is required and must be written.
346  * The DDI_ATTACH command must be provided in the attach entry
347  * point. When attach() is called with cmd set to DDI_ATTACH,
348  * all normal kernel services (such as kmem_alloc(9F)) are
349  * available for use by the driver.
350  *
351  * The attach() function will be called once for each instance
352  * of  the  device  on  the  system with cmd set to DDI_ATTACH.
353  * Until attach() succeeds, the only driver entry points which
354  * may be called are open(9E) and getinfo(9E).
355  */
356 static int
357 e1000g_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
358 {
359 	struct e1000g *Adapter;
360 	struct e1000_hw *hw;
361 	struct e1000g_osdep *osdep;
362 	int instance;
363 
364 	switch (cmd) {
365 	default:
366 		e1000g_log(NULL, CE_WARN,
367 		    "Unsupported command send to e1000g_attach... ");
368 		return (DDI_FAILURE);
369 
370 	case DDI_RESUME:
371 		return (e1000g_resume(devinfo));
372 
373 	case DDI_ATTACH:
374 		break;
375 	}
376 
377 	/*
378 	 * get device instance number
379 	 */
380 	instance = ddi_get_instance(devinfo);
381 
382 	/*
383 	 * Allocate soft data structure
384 	 */
385 	Adapter =
386 	    (struct e1000g *)kmem_zalloc(sizeof (*Adapter), KM_SLEEP);
387 
388 	Adapter->dip = devinfo;
389 	Adapter->instance = instance;
390 	Adapter->tx_ring->adapter = Adapter;
391 	Adapter->rx_ring->adapter = Adapter;
392 
393 	hw = &Adapter->shared;
394 	osdep = &Adapter->osdep;
395 	hw->back = osdep;
396 	osdep->adapter = Adapter;
397 
398 	ddi_set_driver_private(devinfo, (caddr_t)Adapter);
399 
400 	/*
401 	 * Initialize for fma support
402 	 */
403 	Adapter->fm_capabilities = e1000g_get_prop(Adapter, "fm-capable",
404 	    0, 0x0f,
405 	    DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
406 	    DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
407 	e1000g_fm_init(Adapter);
408 	Adapter->attach_progress |= ATTACH_PROGRESS_FMINIT;
409 
410 	/*
411 	 * PCI Configure
412 	 */
413 	if (pci_config_setup(devinfo, &osdep->cfg_handle) != DDI_SUCCESS) {
414 		e1000g_log(Adapter, CE_WARN, "PCI configuration failed");
415 		goto attach_fail;
416 	}
417 	Adapter->attach_progress |= ATTACH_PROGRESS_PCI_CONFIG;
418 
419 	/*
420 	 * Setup hardware
421 	 */
422 	if (e1000g_identify_hardware(Adapter) != DDI_SUCCESS) {
423 		e1000g_log(Adapter, CE_WARN, "Identify hardware failed");
424 		goto attach_fail;
425 	}
426 
427 	/*
428 	 * Map in the device registers.
429 	 */
430 	if (e1000g_regs_map(Adapter) != DDI_SUCCESS) {
431 		e1000g_log(Adapter, CE_WARN, "Mapping registers failed");
432 		goto attach_fail;
433 	}
434 	Adapter->attach_progress |= ATTACH_PROGRESS_REGS_MAP;
435 
436 	/*
437 	 * Initialize driver parameters
438 	 */
439 	if (e1000g_set_driver_params(Adapter) != DDI_SUCCESS) {
440 		goto attach_fail;
441 	}
442 	Adapter->attach_progress |= ATTACH_PROGRESS_SETUP;
443 
444 	if (e1000g_check_acc_handle(Adapter->osdep.cfg_handle) != DDI_FM_OK) {
445 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
446 		goto attach_fail;
447 	}
448 
449 	/*
450 	 * Initialize interrupts
451 	 */
452 	if (e1000g_add_intrs(Adapter) != DDI_SUCCESS) {
453 		e1000g_log(Adapter, CE_WARN, "Add interrupts failed");
454 		goto attach_fail;
455 	}
456 	Adapter->attach_progress |= ATTACH_PROGRESS_ADD_INTR;
457 
458 	/*
459 	 * Initialize mutex's for this device.
460 	 * Do this before enabling the interrupt handler and
461 	 * register the softint to avoid the condition where
462 	 * interrupt handler can try using uninitialized mutex
463 	 */
464 	e1000g_init_locks(Adapter);
465 	Adapter->attach_progress |= ATTACH_PROGRESS_LOCKS;
466 
467 	/*
468 	 * Initialize Driver Counters
469 	 */
470 	if (e1000g_init_stats(Adapter) != DDI_SUCCESS) {
471 		e1000g_log(Adapter, CE_WARN, "Init stats failed");
472 		goto attach_fail;
473 	}
474 	Adapter->attach_progress |= ATTACH_PROGRESS_KSTATS;
475 
476 	/*
477 	 * Initialize chip hardware and software structures
478 	 */
479 	if (e1000g_init(Adapter) != DDI_SUCCESS) {
480 		e1000g_log(Adapter, CE_WARN, "Adapter initialization failed");
481 		goto attach_fail;
482 	}
483 	Adapter->attach_progress |= ATTACH_PROGRESS_INIT;
484 
485 	/*
486 	 * Initialize NDD parameters
487 	 */
488 	if (e1000g_nd_init(Adapter) != DDI_SUCCESS) {
489 		e1000g_log(Adapter, CE_WARN, "Init ndd failed");
490 		goto attach_fail;
491 	}
492 	Adapter->attach_progress |= ATTACH_PROGRESS_NDD;
493 
494 	/*
495 	 * Register the driver to the MAC
496 	 */
497 	if (e1000g_register_mac(Adapter) != DDI_SUCCESS) {
498 		e1000g_log(Adapter, CE_WARN, "Register MAC failed");
499 		goto attach_fail;
500 	}
501 	Adapter->attach_progress |= ATTACH_PROGRESS_MAC;
502 
503 	/*
504 	 * Now that mutex locks are initialized, and the chip is also
505 	 * initialized, enable interrupts.
506 	 */
507 	if (e1000g_enable_intrs(Adapter) != DDI_SUCCESS) {
508 		e1000g_log(Adapter, CE_WARN, "Enable DDI interrupts failed");
509 		goto attach_fail;
510 	}
511 	Adapter->attach_progress |= ATTACH_PROGRESS_ENABLE_INTR;
512 
513 	/*
514 	 * If e1000g_force_detach is enabled, in global private dip list,
515 	 * we will create a new entry, which maintains the priv_dip for DR
516 	 * supports after driver detached.
517 	 */
518 	if (e1000g_force_detach) {
519 		private_devi_list_t *devi_node;
520 
521 		Adapter->priv_dip =
522 		    kmem_zalloc(sizeof (struct dev_info), KM_SLEEP);
523 		bcopy(DEVI(devinfo), DEVI(Adapter->priv_dip),
524 		    sizeof (struct dev_info));
525 
526 		devi_node =
527 		    kmem_zalloc(sizeof (private_devi_list_t), KM_SLEEP);
528 
529 		rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
530 		devi_node->priv_dip = Adapter->priv_dip;
531 		devi_node->flag = E1000G_PRIV_DEVI_ATTACH;
532 		devi_node->next = e1000g_private_devi_list;
533 		e1000g_private_devi_list = devi_node;
534 		rw_exit(&e1000g_rx_detach_lock);
535 	}
536 
537 	cmn_err(CE_CONT, "!%s, %s\n", e1000g_string, e1000g_version);
538 
539 	return (DDI_SUCCESS);
540 
541 attach_fail:
542 	e1000g_unattach(devinfo, Adapter);
543 	return (DDI_FAILURE);
544 }
545 
546 static int
547 e1000g_register_mac(struct e1000g *Adapter)
548 {
549 	struct e1000_hw *hw = &Adapter->shared;
550 	mac_register_t *mac;
551 	int err;
552 
553 	if ((mac = mac_alloc(MAC_VERSION)) == NULL)
554 		return (DDI_FAILURE);
555 
556 	mac->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
557 	mac->m_driver = Adapter;
558 	mac->m_dip = Adapter->dip;
559 	mac->m_src_addr = hw->mac.addr;
560 	mac->m_callbacks = &e1000g_m_callbacks;
561 	mac->m_min_sdu = 0;
562 	mac->m_max_sdu =
563 	    (hw->mac.max_frame_size > FRAME_SIZE_UPTO_8K) ?
564 	    hw->mac.max_frame_size - 256 :
565 	    (hw->mac.max_frame_size != ETHERMAX) ?
566 	    hw->mac.max_frame_size - 24 : ETHERMTU;
567 	mac->m_margin = VLAN_TAGSZ;
568 
569 	err = mac_register(mac, &Adapter->mh);
570 	mac_free(mac);
571 
572 	return (err == 0 ? DDI_SUCCESS : DDI_FAILURE);
573 }
574 
575 static int
576 e1000g_identify_hardware(struct e1000g *Adapter)
577 {
578 	struct e1000_hw *hw = &Adapter->shared;
579 	struct e1000g_osdep *osdep = &Adapter->osdep;
580 
581 	/* Get the device id */
582 	hw->vendor_id =
583 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_VENID);
584 	hw->device_id =
585 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_DEVID);
586 	hw->revision_id =
587 	    pci_config_get8(osdep->cfg_handle, PCI_CONF_REVID);
588 	hw->subsystem_device_id =
589 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBSYSID);
590 	hw->subsystem_vendor_id =
591 	    pci_config_get16(osdep->cfg_handle, PCI_CONF_SUBVENID);
592 
593 	if (e1000_set_mac_type(hw) != E1000_SUCCESS) {
594 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
595 		    "MAC type could not be set properly.");
596 		return (DDI_FAILURE);
597 	}
598 
599 	return (DDI_SUCCESS);
600 }
601 
602 static int
603 e1000g_regs_map(struct e1000g *Adapter)
604 {
605 	dev_info_t *devinfo = Adapter->dip;
606 	struct e1000_hw *hw = &Adapter->shared;
607 	struct e1000g_osdep *osdep = &Adapter->osdep;
608 	off_t mem_size;
609 
610 	/*
611 	 * first get the size of device register to be mapped. The
612 	 * second parameter is the register we are interested. I our
613 	 * wiseman 0 is for config registers and 1 is for memory mapped
614 	 * registers Mem size should have memory mapped region size
615 	 */
616 	if (ddi_dev_regsize(devinfo, 1, &mem_size) != DDI_SUCCESS) {
617 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
618 		    "ddi_dev_regsize for registers failed");
619 		return (DDI_FAILURE);
620 	}
621 
622 	if ((ddi_regs_map_setup(devinfo, 1, /* register of interest */
623 	    (caddr_t *)&hw->hw_addr, 0, mem_size, &e1000g_regs_acc_attr,
624 	    &osdep->reg_handle)) != DDI_SUCCESS) {
625 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
626 		    "ddi_regs_map_setup for registers failed");
627 		goto regs_map_fail;
628 	}
629 
630 	/* ICH needs to map flash memory */
631 	if (hw->mac.type == e1000_ich8lan || hw->mac.type == e1000_ich9lan) {
632 		/* get flash size */
633 		if (ddi_dev_regsize(devinfo, ICH_FLASH_REG_SET,
634 		    &mem_size) != DDI_SUCCESS) {
635 			E1000G_DEBUGLOG_0(Adapter, CE_WARN,
636 			    "ddi_dev_regsize for ICH flash failed");
637 			goto regs_map_fail;
638 		}
639 
640 		/* map flash in */
641 		if (ddi_regs_map_setup(devinfo, ICH_FLASH_REG_SET,
642 		    (caddr_t *)&hw->flash_address, 0,
643 		    mem_size, &e1000g_regs_acc_attr,
644 		    &osdep->ich_flash_handle) != DDI_SUCCESS) {
645 			E1000G_DEBUGLOG_0(Adapter, CE_WARN,
646 			    "ddi_regs_map_setup for ICH flash failed");
647 			goto regs_map_fail;
648 		}
649 	}
650 
651 	return (DDI_SUCCESS);
652 
653 regs_map_fail:
654 	if (osdep->reg_handle != NULL)
655 		ddi_regs_map_free(&osdep->reg_handle);
656 
657 	return (DDI_FAILURE);
658 }
659 
660 static int
661 e1000g_set_driver_params(struct e1000g *Adapter)
662 {
663 	struct e1000_hw *hw;
664 	e1000g_tx_ring_t *tx_ring;
665 	uint32_t mem_bar, io_bar, bar64;
666 #ifdef __sparc
667 	dev_info_t *devinfo = Adapter->dip;
668 	ulong_t iommu_pagesize;
669 #endif
670 
671 	hw = &Adapter->shared;
672 
673 	/* Set MAC type and initialize hardware functions */
674 	if (e1000_setup_init_funcs(hw, B_TRUE) != E1000_SUCCESS) {
675 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
676 		    "Could not setup hardware functions");
677 		return (DDI_FAILURE);
678 	}
679 
680 	/* Get bus information */
681 	if (e1000_get_bus_info(hw) != E1000_SUCCESS) {
682 		E1000G_DEBUGLOG_0(Adapter, CE_WARN,
683 		    "Could not get bus information");
684 		return (DDI_FAILURE);
685 	}
686 
687 	/* get mem_base addr */
688 	mem_bar = pci_config_get32(Adapter->osdep.cfg_handle, PCI_CONF_BASE0);
689 	bar64 = mem_bar & PCI_BASE_TYPE_ALL;
690 
691 	/* get io_base addr */
692 	if (hw->mac.type >= e1000_82544) {
693 		if (bar64) {
694 			/* IO BAR is different for 64 bit BAR mode */
695 			io_bar = pci_config_get32(Adapter->osdep.cfg_handle,
696 			    PCI_CONF_BASE4);
697 		} else {
698 			/* normal 32-bit BAR mode */
699 			io_bar = pci_config_get32(Adapter->osdep.cfg_handle,
700 			    PCI_CONF_BASE2);
701 		}
702 		hw->io_base = io_bar & PCI_BASE_IO_ADDR_M;
703 	} else {
704 		/* no I/O access for adapters prior to 82544 */
705 		hw->io_base = 0x0;
706 	}
707 
708 	e1000_read_pci_cfg(hw, PCI_COMMAND_REGISTER, &hw->bus.pci_cmd_word);
709 
710 	hw->mac.autoneg_failed = B_TRUE;
711 
712 	/* Set the wait_for_link flag to B_FALSE */
713 	hw->phy.wait_for_link = B_FALSE;
714 
715 	/* Adaptive IFS related changes */
716 	hw->mac.adaptive_ifs = B_TRUE;
717 
718 	/* Enable phy init script for IGP phy of 82541/82547 */
719 	if ((hw->mac.type == e1000_82547) ||
720 	    (hw->mac.type == e1000_82541) ||
721 	    (hw->mac.type == e1000_82547_rev_2) ||
722 	    (hw->mac.type == e1000_82541_rev_2))
723 		e1000_init_script_state_82541(hw, B_TRUE);
724 
725 	/* Enable the TTL workaround for 82541/82547 */
726 	e1000_set_ttl_workaround_state_82541(hw, B_TRUE);
727 
728 #ifdef __sparc
729 	Adapter->strip_crc = B_TRUE;
730 #else
731 	Adapter->strip_crc = B_FALSE;
732 #endif
733 
734 	/* Get conf file properties */
735 	e1000g_get_conf(Adapter);
736 
737 	/* Get speed/duplex settings in conf file */
738 	hw->mac.forced_speed_duplex = ADVERTISE_100_FULL;
739 	hw->phy.autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
740 	e1000g_force_speed_duplex(Adapter);
741 
742 	/* Get Jumbo Frames settings in conf file */
743 	e1000g_get_max_frame_size(Adapter);
744 	hw->mac.min_frame_size =
745 	    MINIMUM_ETHERNET_PACKET_SIZE + CRC_LENGTH;
746 
747 #ifdef __sparc
748 	/* Get the system page size */
749 	Adapter->sys_page_sz = ddi_ptob(devinfo, (ulong_t)1);
750 	iommu_pagesize = dvma_pagesize(devinfo);
751 	if (iommu_pagesize != 0) {
752 		if (Adapter->sys_page_sz == iommu_pagesize) {
753 			if (iommu_pagesize > 0x4000)
754 				Adapter->sys_page_sz = 0x4000;
755 		} else {
756 			if (Adapter->sys_page_sz > iommu_pagesize)
757 				Adapter->sys_page_sz = iommu_pagesize;
758 		}
759 	}
760 	Adapter->dvma_page_num = hw->mac.max_frame_size /
761 	    Adapter->sys_page_sz + E1000G_DEFAULT_DVMA_PAGE_NUM;
762 	ASSERT(Adapter->dvma_page_num >= E1000G_DEFAULT_DVMA_PAGE_NUM);
763 #endif
764 
765 	/* Set Rx/Tx buffer size */
766 	switch (hw->mac.max_frame_size) {
767 	case ETHERMAX:
768 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_2K;
769 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_2K;
770 		break;
771 	case FRAME_SIZE_UPTO_4K:
772 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_4K;
773 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_4K;
774 		break;
775 	case FRAME_SIZE_UPTO_8K:
776 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_8K;
777 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_8K;
778 		break;
779 	case FRAME_SIZE_UPTO_9K:
780 	case FRAME_SIZE_UPTO_16K:
781 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_16K;
782 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_16K;
783 		break;
784 	default:
785 		Adapter->rx_buffer_size = E1000_RX_BUFFER_SIZE_2K;
786 		Adapter->tx_buffer_size = E1000_TX_BUFFER_SIZE_2K;
787 		break;
788 	}
789 	Adapter->rx_buffer_size += E1000G_IPALIGNPRESERVEROOM;
790 
791 #ifndef NO_82542_SUPPORT
792 	/*
793 	 * For Wiseman adapters we have an requirement of having receive
794 	 * buffers aligned at 256 byte boundary. Since Livengood does not
795 	 * require this and forcing it for all hardwares will have
796 	 * performance implications, I am making it applicable only for
797 	 * Wiseman and for Jumbo frames enabled mode as rest of the time,
798 	 * it is okay to have normal frames...but it does involve a
799 	 * potential risk where we may loose data if buffer is not
800 	 * aligned...so all wiseman boards to have 256 byte aligned
801 	 * buffers
802 	 */
803 	if (hw->mac.type < e1000_82543)
804 		Adapter->rx_buf_align = RECEIVE_BUFFER_ALIGN_SIZE;
805 	else
806 		Adapter->rx_buf_align = 1;
807 #endif
808 
809 	/* Master Latency Timer */
810 	Adapter->master_latency_timer = DEFAULT_MASTER_LATENCY_TIMER;
811 
812 	/* copper options */
813 	if (hw->media_type == e1000_media_type_copper) {
814 		hw->phy.mdix = 0;	/* AUTO_ALL_MODES */
815 		hw->phy.disable_polarity_correction = B_FALSE;
816 		hw->phy.ms_type = e1000_ms_hw_default;	/* E1000_MASTER_SLAVE */
817 	}
818 
819 	/* The initial link state should be "unknown" */
820 	Adapter->link_state = LINK_STATE_UNKNOWN;
821 
822 	/* Initialize rx parameters */
823 	Adapter->rx_intr_delay = DEFAULT_RX_INTR_DELAY;
824 	Adapter->rx_intr_abs_delay = DEFAULT_RX_INTR_ABS_DELAY;
825 
826 	/* Initialize tx parameters */
827 	Adapter->tx_intr_enable = DEFAULT_TX_INTR_ENABLE;
828 	Adapter->tx_bcopy_thresh = DEFAULT_TX_BCOPY_THRESHOLD;
829 	Adapter->tx_intr_delay = DEFAULT_TX_INTR_DELAY;
830 	Adapter->tx_intr_abs_delay = DEFAULT_TX_INTR_ABS_DELAY;
831 
832 	tx_ring = Adapter->tx_ring;
833 	tx_ring->frags_limit =
834 	    (hw->mac.max_frame_size / Adapter->tx_bcopy_thresh) + 2;
835 	if (tx_ring->frags_limit > (MAX_TX_DESC_PER_PACKET >> 1))
836 		tx_ring->frags_limit = (MAX_TX_DESC_PER_PACKET >> 1);
837 
838 	/* Initialize rx parameters */
839 	Adapter->rx_bcopy_thresh = DEFAULT_RX_BCOPY_THRESHOLD;
840 
841 	return (DDI_SUCCESS);
842 }
843 
844 /*
845  * e1000g_detach - driver detach
846  *
847  * The detach() function is the complement of the attach routine.
848  * If cmd is set to DDI_DETACH, detach() is used to remove  the
849  * state  associated  with  a  given  instance of a device node
850  * prior to the removal of that instance from the system.
851  *
852  * The detach() function will be called once for each  instance
853  * of the device for which there has been a successful attach()
854  * once there are no longer  any  opens  on  the  device.
855  *
856  * Interrupts routine are disabled, All memory allocated by this
857  * driver are freed.
858  */
859 static int
860 e1000g_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
861 {
862 	struct e1000g *Adapter;
863 	boolean_t rx_drain;
864 
865 	switch (cmd) {
866 	default:
867 		return (DDI_FAILURE);
868 
869 	case DDI_SUSPEND:
870 		return (e1000g_suspend(devinfo));
871 
872 	case DDI_DETACH:
873 		break;
874 	}
875 
876 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
877 	if (Adapter == NULL)
878 		return (DDI_FAILURE);
879 
880 	if (mac_unregister(Adapter->mh) != 0) {
881 		e1000g_log(Adapter, CE_WARN, "Unregister MAC failed");
882 		return (DDI_FAILURE);
883 	}
884 	Adapter->attach_progress &= ~ATTACH_PROGRESS_MAC;
885 
886 
887 	if (Adapter->chip_state != E1000G_STOP)
888 		e1000g_stop(Adapter, B_TRUE);
889 
890 	rx_drain = e1000g_rx_drain(Adapter);
891 
892 	/*
893 	 * If e1000g_force_detach is enabled, driver detach is safe.
894 	 * We will let e1000g_free_priv_devi_node routine determine
895 	 * whether we need to free the priv_dip entry for current
896 	 * driver instance.
897 	 */
898 	if (e1000g_force_detach) {
899 		e1000g_free_priv_devi_node(Adapter, rx_drain);
900 	} else {
901 		if (!rx_drain)
902 			return (DDI_FAILURE);
903 	}
904 
905 	e1000g_unattach(devinfo, Adapter);
906 
907 	return (DDI_SUCCESS);
908 }
909 
910 /*
911  * e1000g_free_priv_devi_node - free a priv_dip entry for driver instance
912  *
913  * If free_flag is true, that indicates the upper layer is not holding
914  * the rx buffers, we could free the priv_dip entry safely.
915  *
916  * Otherwise, we have to keep this entry even after driver detached,
917  * and we also need to mark this entry with E1000G_PRIV_DEVI_DETACH flag,
918  * so that driver could free it while all of rx buffers are returned
919  * by upper layer later.
920  */
921 static void
922 e1000g_free_priv_devi_node(struct e1000g *Adapter, boolean_t free_flag)
923 {
924 	private_devi_list_t *devi_node, *devi_del;
925 
926 	rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
927 	ASSERT(e1000g_private_devi_list != NULL);
928 	ASSERT(Adapter->priv_dip != NULL);
929 
930 	devi_node = e1000g_private_devi_list;
931 	if (devi_node->priv_dip == Adapter->priv_dip) {
932 		if (free_flag) {
933 			e1000g_private_devi_list =
934 			    devi_node->next;
935 			kmem_free(devi_node->priv_dip,
936 			    sizeof (struct dev_info));
937 			kmem_free(devi_node,
938 			    sizeof (private_devi_list_t));
939 		} else {
940 			ASSERT(e1000g_mblks_pending != 0);
941 			devi_node->flag =
942 			    E1000G_PRIV_DEVI_DETACH;
943 		}
944 		rw_exit(&e1000g_rx_detach_lock);
945 		return;
946 	}
947 
948 	devi_node = e1000g_private_devi_list;
949 	while (devi_node->next != NULL) {
950 		if (devi_node->next->priv_dip == Adapter->priv_dip) {
951 			if (free_flag) {
952 				devi_del = devi_node->next;
953 				devi_node->next = devi_del->next;
954 				kmem_free(devi_del->priv_dip,
955 				    sizeof (struct dev_info));
956 				kmem_free(devi_del,
957 				    sizeof (private_devi_list_t));
958 			} else {
959 				ASSERT(e1000g_mblks_pending != 0);
960 				devi_node->next->flag =
961 				    E1000G_PRIV_DEVI_DETACH;
962 			}
963 			break;
964 		}
965 		devi_node = devi_node->next;
966 	}
967 	rw_exit(&e1000g_rx_detach_lock);
968 }
969 
970 static void
971 e1000g_unattach(dev_info_t *devinfo, struct e1000g *Adapter)
972 {
973 	if (Adapter->attach_progress & ATTACH_PROGRESS_ENABLE_INTR) {
974 		(void) e1000g_disable_intrs(Adapter);
975 	}
976 
977 	if (Adapter->attach_progress & ATTACH_PROGRESS_MAC) {
978 		(void) mac_unregister(Adapter->mh);
979 	}
980 
981 	if (Adapter->attach_progress & ATTACH_PROGRESS_NDD) {
982 		e1000g_nd_cleanup(Adapter);
983 	}
984 
985 	if (Adapter->attach_progress & ATTACH_PROGRESS_ADD_INTR) {
986 		(void) e1000g_rem_intrs(Adapter);
987 	}
988 
989 	if (Adapter->attach_progress & ATTACH_PROGRESS_SETUP) {
990 		(void) ddi_prop_remove_all(devinfo);
991 	}
992 
993 	if (Adapter->attach_progress & ATTACH_PROGRESS_KSTATS) {
994 		kstat_delete((kstat_t *)Adapter->e1000g_ksp);
995 	}
996 
997 	if (Adapter->attach_progress & ATTACH_PROGRESS_INIT) {
998 		stop_link_timer(Adapter);
999 		if (e1000_reset_hw(&Adapter->shared) != 0) {
1000 			e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
1001 			ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
1002 		}
1003 	}
1004 
1005 	if (Adapter->attach_progress & ATTACH_PROGRESS_REGS_MAP) {
1006 		if (Adapter->osdep.reg_handle != NULL)
1007 			ddi_regs_map_free(&Adapter->osdep.reg_handle);
1008 		if (Adapter->osdep.ich_flash_handle != NULL)
1009 			ddi_regs_map_free(&Adapter->osdep.ich_flash_handle);
1010 	}
1011 
1012 	if (Adapter->attach_progress & ATTACH_PROGRESS_PCI_CONFIG) {
1013 		if (Adapter->osdep.cfg_handle != NULL)
1014 			pci_config_teardown(&Adapter->osdep.cfg_handle);
1015 	}
1016 
1017 	if (Adapter->attach_progress & ATTACH_PROGRESS_LOCKS) {
1018 		e1000g_destroy_locks(Adapter);
1019 	}
1020 
1021 	if (Adapter->attach_progress & ATTACH_PROGRESS_FMINIT) {
1022 		e1000g_fm_fini(Adapter);
1023 	}
1024 
1025 	e1000_remove_device(&Adapter->shared);
1026 
1027 	kmem_free((caddr_t)Adapter, sizeof (struct e1000g));
1028 
1029 	/*
1030 	 * Another hotplug spec requirement,
1031 	 * run ddi_set_driver_private(devinfo, null);
1032 	 */
1033 	ddi_set_driver_private(devinfo, NULL);
1034 }
1035 
1036 static void
1037 e1000g_init_locks(struct e1000g *Adapter)
1038 {
1039 	e1000g_tx_ring_t *tx_ring;
1040 	e1000g_rx_ring_t *rx_ring;
1041 
1042 	rw_init(&Adapter->chip_lock, NULL,
1043 	    RW_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
1044 	mutex_init(&Adapter->link_lock, NULL,
1045 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
1046 	mutex_init(&Adapter->watchdog_lock, NULL,
1047 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
1048 
1049 	tx_ring = Adapter->tx_ring;
1050 
1051 	mutex_init(&tx_ring->tx_lock, NULL,
1052 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
1053 	mutex_init(&tx_ring->usedlist_lock, NULL,
1054 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
1055 	mutex_init(&tx_ring->freelist_lock, NULL,
1056 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
1057 
1058 	rx_ring = Adapter->rx_ring;
1059 
1060 	mutex_init(&rx_ring->freelist_lock, NULL,
1061 	    MUTEX_DRIVER, DDI_INTR_PRI(Adapter->intr_pri));
1062 }
1063 
1064 static void
1065 e1000g_destroy_locks(struct e1000g *Adapter)
1066 {
1067 	e1000g_tx_ring_t *tx_ring;
1068 	e1000g_rx_ring_t *rx_ring;
1069 
1070 	tx_ring = Adapter->tx_ring;
1071 	mutex_destroy(&tx_ring->tx_lock);
1072 	mutex_destroy(&tx_ring->usedlist_lock);
1073 	mutex_destroy(&tx_ring->freelist_lock);
1074 
1075 	rx_ring = Adapter->rx_ring;
1076 	mutex_destroy(&rx_ring->freelist_lock);
1077 
1078 	mutex_destroy(&Adapter->link_lock);
1079 	mutex_destroy(&Adapter->watchdog_lock);
1080 	rw_destroy(&Adapter->chip_lock);
1081 }
1082 
1083 static int
1084 e1000g_resume(dev_info_t *devinfo)
1085 {
1086 	struct e1000g *Adapter;
1087 
1088 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
1089 	if (Adapter == NULL)
1090 		return (DDI_FAILURE);
1091 
1092 	if (e1000g_start(Adapter, B_TRUE))
1093 		return (DDI_FAILURE);
1094 
1095 	return (DDI_SUCCESS);
1096 }
1097 
1098 static int
1099 e1000g_suspend(dev_info_t *devinfo)
1100 {
1101 	struct e1000g *Adapter;
1102 
1103 	Adapter = (struct e1000g *)ddi_get_driver_private(devinfo);
1104 	if (Adapter == NULL)
1105 		return (DDI_FAILURE);
1106 
1107 	e1000g_stop(Adapter, B_TRUE);
1108 
1109 	return (DDI_SUCCESS);
1110 }
1111 
1112 static int
1113 e1000g_init(struct e1000g *Adapter)
1114 {
1115 	uint32_t pba;
1116 	uint32_t high_water;
1117 	struct e1000_hw *hw;
1118 	clock_t link_timeout;
1119 
1120 	hw = &Adapter->shared;
1121 
1122 	rw_enter(&Adapter->chip_lock, RW_WRITER);
1123 
1124 	/*
1125 	 * reset to put the hardware in a known state
1126 	 * before we try to do anything with the eeprom
1127 	 */
1128 	if (e1000_reset_hw(hw) != 0) {
1129 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
1130 		goto init_fail;
1131 	}
1132 
1133 	if (e1000_validate_nvm_checksum(hw) < 0) {
1134 		/*
1135 		 * Some PCI-E parts fail the first check due to
1136 		 * the link being in sleep state.  Call it again,
1137 		 * if it fails a second time its a real issue.
1138 		 */
1139 		if (e1000_validate_nvm_checksum(hw) < 0) {
1140 			e1000g_log(Adapter, CE_WARN,
1141 			    "Invalid NVM checksum. Please contact "
1142 			    "the vendor to update the NVM.");
1143 			e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
1144 			goto init_fail;
1145 		}
1146 	}
1147 
1148 #ifdef __sparc
1149 	/*
1150 	 * Firstly, we try to get the local ethernet address from OBP. If
1151 	 * fail, we get from EEPROM of NIC card.
1152 	 */
1153 	if (!e1000g_find_mac_address(Adapter)) {
1154 		if (e1000_read_mac_addr(hw) < 0) {
1155 			e1000g_log(Adapter, CE_WARN, "Read mac addr failed");
1156 			e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
1157 			goto init_fail;
1158 		}
1159 	}
1160 #else
1161 	/* Get the local ethernet address. */
1162 	if (e1000_read_mac_addr(hw) < 0) {
1163 		e1000g_log(Adapter, CE_WARN, "Read mac addr failed");
1164 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
1165 		goto init_fail;
1166 	}
1167 #endif
1168 
1169 	/* check for valid mac address */
1170 	if (!is_valid_mac_addr(hw->mac.addr)) {
1171 		e1000g_log(Adapter, CE_WARN, "Invalid mac addr");
1172 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
1173 		goto init_fail;
1174 	}
1175 
1176 	/* Set LAA state for 82571 chipset */
1177 	e1000_set_laa_state_82571(hw, B_TRUE);
1178 
1179 	/* Master Latency Timer implementation */
1180 	if (Adapter->master_latency_timer) {
1181 		pci_config_put8(Adapter->osdep.cfg_handle,
1182 		    PCI_CONF_LATENCY_TIMER, Adapter->master_latency_timer);
1183 	}
1184 
1185 	if (hw->mac.type < e1000_82547) {
1186 		/*
1187 		 * Total FIFO is 64K
1188 		 */
1189 		if (hw->mac.max_frame_size > FRAME_SIZE_UPTO_8K)
1190 			pba = E1000_PBA_40K;	/* 40K for Rx, 24K for Tx */
1191 		else
1192 			pba = E1000_PBA_48K;	/* 48K for Rx, 16K for Tx */
1193 	} else if (hw->mac.type >= e1000_82571 &&
1194 	    hw->mac.type <= e1000_82572) {
1195 		/*
1196 		 * Total FIFO is 48K
1197 		 */
1198 		if (hw->mac.max_frame_size > FRAME_SIZE_UPTO_8K)
1199 			pba = E1000_PBA_30K;	/* 30K for Rx, 18K for Tx */
1200 		else
1201 			pba = E1000_PBA_38K;	/* 38K for Rx, 10K for Tx */
1202 	} else if (hw->mac.type == e1000_ich8lan) {
1203 		pba = E1000_PBA_8K;		/* 8K for Rx, 12K for Tx */
1204 	} else if (hw->mac.type == e1000_ich9lan) {
1205 		pba = E1000_PBA_12K;
1206 	} else {
1207 		/*
1208 		 * Total FIFO is 40K
1209 		 */
1210 		if (hw->mac.max_frame_size > FRAME_SIZE_UPTO_8K)
1211 			pba = E1000_PBA_22K;	/* 22K for Rx, 18K for Tx */
1212 		else
1213 			pba = E1000_PBA_30K;	/* 30K for Rx, 10K for Tx */
1214 	}
1215 	E1000_WRITE_REG(hw, E1000_PBA, pba);
1216 
1217 	/*
1218 	 * These parameters set thresholds for the adapter's generation(Tx)
1219 	 * and response(Rx) to Ethernet PAUSE frames.  These are just threshold
1220 	 * settings.  Flow control is enabled or disabled in the configuration
1221 	 * file.
1222 	 * High-water mark is set down from the top of the rx fifo (not
1223 	 * sensitive to max_frame_size) and low-water is set just below
1224 	 * high-water mark.
1225 	 * The high water mark must be low enough to fit one full frame above
1226 	 * it in the rx FIFO.  Should be the lower of:
1227 	 * 90% of the Rx FIFO size and the full Rx FIFO size minus the early
1228 	 * receive size (assuming ERT set to E1000_ERT_2048), or the full
1229 	 * Rx FIFO size minus one full frame.
1230 	 */
1231 	high_water = min(((pba << 10) * 9 / 10),
1232 	    ((hw->mac.type == e1000_82573 || hw->mac.type == e1000_ich9lan) ?
1233 	    ((pba << 10) - (E1000_ERT_2048 << 3)) :
1234 	    ((pba << 10) - hw->mac.max_frame_size)));
1235 
1236 	hw->mac.fc_high_water = high_water & 0xFFF8;
1237 	hw->mac.fc_low_water = hw->mac.fc_high_water - 8;
1238 
1239 	if (hw->mac.type == e1000_80003es2lan)
1240 		hw->mac.fc_pause_time = 0xFFFF;
1241 	else
1242 		hw->mac.fc_pause_time = E1000_FC_PAUSE_TIME;
1243 	hw->mac.fc_send_xon = B_TRUE;
1244 	hw->mac.fc = hw->mac.original_fc;
1245 
1246 	/*
1247 	 * Reset the adapter hardware the second time.
1248 	 */
1249 	if (e1000_reset_hw(hw) != 0) {
1250 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
1251 		goto init_fail;
1252 	}
1253 
1254 	/* disable wakeup control by default */
1255 	if (hw->mac.type >= e1000_82544)
1256 		E1000_WRITE_REG(hw, E1000_WUC, 0);
1257 
1258 	/* MWI setup */
1259 	e1000_pci_set_mwi(hw);
1260 
1261 	/*
1262 	 * Configure/Initialize hardware
1263 	 */
1264 	if (e1000_init_hw(hw) < 0) {
1265 		e1000g_log(Adapter, CE_WARN, "Initialize hw failed");
1266 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
1267 		goto init_fail;
1268 	}
1269 
1270 	/* Disable Smart Power Down */
1271 	phy_spd_state(hw, B_FALSE);
1272 
1273 	/* Make sure driver has control */
1274 	e1000g_get_driver_control(hw);
1275 
1276 	/*
1277 	 * Initialize unicast addresses.
1278 	 */
1279 	e1000g_init_unicst(Adapter);
1280 
1281 	/*
1282 	 * Setup and initialize the mctable structures.  After this routine
1283 	 * completes  Multicast table will be set
1284 	 */
1285 	e1000g_setup_multicast(Adapter);
1286 	msec_delay(5);
1287 
1288 	/*
1289 	 * Implement Adaptive IFS
1290 	 */
1291 	e1000_reset_adaptive(hw);
1292 
1293 	/* Setup Interrupt Throttling Register */
1294 	if (hw->mac.type >= e1000_82540) {
1295 		E1000_WRITE_REG(hw, E1000_ITR, Adapter->intr_throttling_rate);
1296 	} else
1297 		Adapter->intr_adaptive = B_FALSE;
1298 
1299 	/* Start the timer for link setup */
1300 	if (hw->mac.autoneg)
1301 		link_timeout = PHY_AUTO_NEG_LIMIT * drv_usectohz(100000);
1302 	else
1303 		link_timeout = PHY_FORCE_LIMIT * drv_usectohz(100000);
1304 
1305 	mutex_enter(&Adapter->link_lock);
1306 	if (hw->phy.wait_for_link) {
1307 		Adapter->link_complete = B_TRUE;
1308 	} else {
1309 		Adapter->link_complete = B_FALSE;
1310 		Adapter->link_tid = timeout(e1000g_link_timer,
1311 		    (void *)Adapter, link_timeout);
1312 	}
1313 	mutex_exit(&Adapter->link_lock);
1314 
1315 	/* Enable PCI-Ex master */
1316 	if (hw->bus.type == e1000_bus_type_pci_express) {
1317 		e1000_enable_pciex_master(hw);
1318 	}
1319 
1320 	/* Save the state of the phy */
1321 	e1000g_get_phy_state(Adapter);
1322 
1323 	Adapter->init_count++;
1324 
1325 	if (e1000g_check_acc_handle(Adapter->osdep.cfg_handle) != DDI_FM_OK) {
1326 		goto init_fail;
1327 	}
1328 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
1329 		goto init_fail;
1330 	}
1331 
1332 	rw_exit(&Adapter->chip_lock);
1333 
1334 	return (DDI_SUCCESS);
1335 
1336 init_fail:
1337 	rw_exit(&Adapter->chip_lock);
1338 	ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
1339 	return (DDI_FAILURE);
1340 }
1341 
1342 /*
1343  * Check if the link is up
1344  */
1345 static boolean_t
1346 e1000g_link_up(struct e1000g *Adapter)
1347 {
1348 	struct e1000_hw *hw;
1349 	boolean_t link_up;
1350 
1351 	hw = &Adapter->shared;
1352 
1353 	e1000_check_for_link(hw);
1354 
1355 	if ((E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU) ||
1356 	    ((!hw->mac.get_link_status) && (hw->mac.type == e1000_82543)) ||
1357 	    ((hw->media_type == e1000_media_type_internal_serdes) &&
1358 	    (hw->mac.serdes_has_link))) {
1359 		link_up = B_TRUE;
1360 	} else {
1361 		link_up = B_FALSE;
1362 	}
1363 
1364 	return (link_up);
1365 }
1366 
1367 static void
1368 e1000g_m_ioctl(void *arg, queue_t *q, mblk_t *mp)
1369 {
1370 	struct iocblk *iocp;
1371 	struct e1000g *e1000gp;
1372 	enum ioc_reply status;
1373 	int err;
1374 
1375 	iocp = (struct iocblk *)mp->b_rptr;
1376 	iocp->ioc_error = 0;
1377 	e1000gp = (struct e1000g *)arg;
1378 
1379 	ASSERT(e1000gp);
1380 	if (e1000gp == NULL) {
1381 		miocnak(q, mp, 0, EINVAL);
1382 		return;
1383 	}
1384 
1385 	switch (iocp->ioc_cmd) {
1386 
1387 	case LB_GET_INFO_SIZE:
1388 	case LB_GET_INFO:
1389 	case LB_GET_MODE:
1390 	case LB_SET_MODE:
1391 		status = e1000g_loopback_ioctl(e1000gp, iocp, mp);
1392 		break;
1393 
1394 	case ND_GET:
1395 	case ND_SET:
1396 		status = e1000g_nd_ioctl(e1000gp, q, mp, iocp);
1397 		break;
1398 
1399 #ifdef E1000G_DEBUG
1400 	case E1000G_IOC_REG_PEEK:
1401 	case E1000G_IOC_REG_POKE:
1402 		status = e1000g_pp_ioctl(e1000gp, iocp, mp);
1403 		break;
1404 	case E1000G_IOC_CHIP_RESET:
1405 		e1000gp->reset_count++;
1406 		if (e1000g_reset(e1000gp))
1407 			status = IOC_ACK;
1408 		else
1409 			status = IOC_INVAL;
1410 		break;
1411 #endif
1412 	default:
1413 		status = IOC_INVAL;
1414 		break;
1415 	}
1416 
1417 	/*
1418 	 * Decide how to reply
1419 	 */
1420 	switch (status) {
1421 	default:
1422 	case IOC_INVAL:
1423 		/*
1424 		 * Error, reply with a NAK and EINVAL or the specified error
1425 		 */
1426 		miocnak(q, mp, 0, iocp->ioc_error == 0 ?
1427 		    EINVAL : iocp->ioc_error);
1428 		break;
1429 
1430 	case IOC_DONE:
1431 		/*
1432 		 * OK, reply already sent
1433 		 */
1434 		break;
1435 
1436 	case IOC_ACK:
1437 		/*
1438 		 * OK, reply with an ACK
1439 		 */
1440 		miocack(q, mp, 0, 0);
1441 		break;
1442 
1443 	case IOC_REPLY:
1444 		/*
1445 		 * OK, send prepared reply as ACK or NAK
1446 		 */
1447 		mp->b_datap->db_type = iocp->ioc_error == 0 ?
1448 		    M_IOCACK : M_IOCNAK;
1449 		qreply(q, mp);
1450 		break;
1451 	}
1452 }
1453 
1454 static int
1455 e1000g_m_start(void *arg)
1456 {
1457 	struct e1000g *Adapter = (struct e1000g *)arg;
1458 
1459 	return (e1000g_start(Adapter, B_TRUE));
1460 }
1461 
1462 static int
1463 e1000g_start(struct e1000g *Adapter, boolean_t global)
1464 {
1465 	if (global) {
1466 		/* Allocate dma resources for descriptors and buffers */
1467 		if (e1000g_alloc_dma_resources(Adapter) != DDI_SUCCESS) {
1468 			e1000g_log(Adapter, CE_WARN,
1469 			    "Alloc DMA resources failed");
1470 			return (ENOTACTIVE);
1471 		}
1472 		Adapter->rx_buffer_setup = B_FALSE;
1473 	}
1474 
1475 	if (!(Adapter->attach_progress & ATTACH_PROGRESS_INIT)) {
1476 		if (e1000g_init(Adapter) != DDI_SUCCESS) {
1477 			e1000g_log(Adapter, CE_WARN,
1478 			    "Adapter initialization failed");
1479 			if (global)
1480 				e1000g_release_dma_resources(Adapter);
1481 			return (ENOTACTIVE);
1482 		}
1483 	}
1484 
1485 	rw_enter(&Adapter->chip_lock, RW_WRITER);
1486 
1487 	/* Setup and initialize the transmit structures */
1488 	e1000g_tx_setup(Adapter);
1489 	msec_delay(5);
1490 
1491 	/* Setup and initialize the receive structures */
1492 	e1000g_rx_setup(Adapter);
1493 	msec_delay(5);
1494 
1495 	e1000g_mask_interrupt(Adapter);
1496 
1497 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
1498 		rw_exit(&Adapter->chip_lock);
1499 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
1500 		return (ENOTACTIVE);
1501 	}
1502 
1503 	Adapter->chip_state = E1000G_START;
1504 	Adapter->attach_progress |= ATTACH_PROGRESS_INIT;
1505 
1506 	rw_exit(&Adapter->chip_lock);
1507 
1508 	/* Enable and start the watchdog timer */
1509 	enable_watchdog_timer(Adapter);
1510 
1511 	return (0);
1512 }
1513 
1514 static void
1515 e1000g_m_stop(void *arg)
1516 {
1517 	struct e1000g *Adapter = (struct e1000g *)arg;
1518 
1519 	e1000g_stop(Adapter, B_TRUE);
1520 }
1521 
1522 static void
1523 e1000g_stop(struct e1000g *Adapter, boolean_t global)
1524 {
1525 	/* Set stop flags */
1526 	rw_enter(&Adapter->chip_lock, RW_WRITER);
1527 
1528 	Adapter->chip_state = E1000G_STOP;
1529 	Adapter->attach_progress &= ~ATTACH_PROGRESS_INIT;
1530 
1531 	rw_exit(&Adapter->chip_lock);
1532 
1533 	/* Drain tx sessions */
1534 	(void) e1000g_tx_drain(Adapter);
1535 
1536 	/* Disable and stop all the timers */
1537 	disable_watchdog_timer(Adapter);
1538 	stop_link_timer(Adapter);
1539 	stop_82547_timer(Adapter->tx_ring);
1540 
1541 	/* Stop the chip and release pending resources */
1542 	rw_enter(&Adapter->chip_lock, RW_WRITER);
1543 
1544 	e1000g_clear_all_interrupts(Adapter);
1545 	if (e1000_reset_hw(&Adapter->shared) != 0) {
1546 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_INVAL_STATE);
1547 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
1548 	}
1549 
1550 	/* Release resources still held by the TX descriptors */
1551 	e1000g_tx_clean(Adapter);
1552 
1553 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
1554 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_LOST);
1555 
1556 	/* Clean the pending rx jumbo packet fragment */
1557 	e1000g_rx_clean(Adapter);
1558 
1559 	rw_exit(&Adapter->chip_lock);
1560 
1561 	if (global)
1562 		e1000g_release_dma_resources(Adapter);
1563 }
1564 
1565 static void
1566 e1000g_rx_clean(struct e1000g *Adapter)
1567 {
1568 	e1000g_rx_ring_t *rx_ring = Adapter->rx_ring;
1569 
1570 	if (rx_ring->rx_mblk != NULL) {
1571 		freemsg(rx_ring->rx_mblk);
1572 		rx_ring->rx_mblk = NULL;
1573 		rx_ring->rx_mblk_tail = NULL;
1574 		rx_ring->rx_mblk_len = 0;
1575 	}
1576 }
1577 
1578 static void
1579 e1000g_tx_clean(struct e1000g *Adapter)
1580 {
1581 	e1000g_tx_ring_t *tx_ring;
1582 	p_tx_sw_packet_t packet;
1583 	mblk_t *mp;
1584 	mblk_t *nmp;
1585 	uint32_t packet_count;
1586 
1587 	tx_ring = Adapter->tx_ring;
1588 
1589 	/*
1590 	 * Here we don't need to protect the lists using
1591 	 * the usedlist_lock and freelist_lock, for they
1592 	 * have been protected by the chip_lock.
1593 	 */
1594 	mp = NULL;
1595 	nmp = NULL;
1596 	packet_count = 0;
1597 	packet = (p_tx_sw_packet_t)QUEUE_GET_HEAD(&tx_ring->used_list);
1598 	while (packet != NULL) {
1599 		if (packet->mp != NULL) {
1600 			/* Assemble the message chain */
1601 			if (mp == NULL) {
1602 				mp = packet->mp;
1603 				nmp = packet->mp;
1604 			} else {
1605 				nmp->b_next = packet->mp;
1606 				nmp = packet->mp;
1607 			}
1608 			/* Disconnect the message from the sw packet */
1609 			packet->mp = NULL;
1610 		}
1611 
1612 		e1000g_free_tx_swpkt(packet);
1613 		packet_count++;
1614 
1615 		packet = (p_tx_sw_packet_t)
1616 		    QUEUE_GET_NEXT(&tx_ring->used_list, &packet->Link);
1617 	}
1618 
1619 	if (mp != NULL)
1620 		freemsgchain(mp);
1621 
1622 	if (packet_count > 0) {
1623 		QUEUE_APPEND(&tx_ring->free_list, &tx_ring->used_list);
1624 		QUEUE_INIT_LIST(&tx_ring->used_list);
1625 
1626 		/* Setup TX descriptor pointers */
1627 		tx_ring->tbd_next = tx_ring->tbd_first;
1628 		tx_ring->tbd_oldest = tx_ring->tbd_first;
1629 
1630 		/* Setup our HW Tx Head & Tail descriptor pointers */
1631 		E1000_WRITE_REG(&Adapter->shared, E1000_TDH, 0);
1632 		E1000_WRITE_REG(&Adapter->shared, E1000_TDT, 0);
1633 	}
1634 }
1635 
1636 static boolean_t
1637 e1000g_tx_drain(struct e1000g *Adapter)
1638 {
1639 	int i;
1640 	boolean_t done;
1641 	e1000g_tx_ring_t *tx_ring;
1642 
1643 	tx_ring = Adapter->tx_ring;
1644 
1645 	/* Allow up to 'wsdraintime' for pending xmit's to complete. */
1646 	for (i = 0; i < TX_DRAIN_TIME; i++) {
1647 		mutex_enter(&tx_ring->usedlist_lock);
1648 		done = IS_QUEUE_EMPTY(&tx_ring->used_list);
1649 		mutex_exit(&tx_ring->usedlist_lock);
1650 
1651 		if (done)
1652 			break;
1653 
1654 		msec_delay(1);
1655 	}
1656 
1657 	return (done);
1658 }
1659 
1660 static boolean_t
1661 e1000g_rx_drain(struct e1000g *Adapter)
1662 {
1663 	e1000g_rx_ring_t *rx_ring;
1664 	p_rx_sw_packet_t packet;
1665 	boolean_t done;
1666 
1667 	rx_ring = Adapter->rx_ring;
1668 	done = B_TRUE;
1669 
1670 	rw_enter(&e1000g_rx_detach_lock, RW_WRITER);
1671 
1672 	while (rx_ring->pending_list != NULL) {
1673 		packet = rx_ring->pending_list;
1674 		rx_ring->pending_list =
1675 		    rx_ring->pending_list->next;
1676 
1677 		if (packet->flag == E1000G_RX_SW_STOP) {
1678 			packet->flag = E1000G_RX_SW_DETACH;
1679 			done = B_FALSE;
1680 		} else {
1681 			ASSERT(packet->flag == E1000G_RX_SW_FREE);
1682 			ASSERT(packet->mp == NULL);
1683 			e1000g_free_rx_sw_packet(packet);
1684 		}
1685 	}
1686 
1687 	rw_exit(&e1000g_rx_detach_lock);
1688 
1689 	return (done);
1690 }
1691 
1692 boolean_t
1693 e1000g_reset(struct e1000g *Adapter)
1694 {
1695 	e1000g_stop(Adapter, B_FALSE);
1696 
1697 	if (e1000g_start(Adapter, B_FALSE)) {
1698 		e1000g_log(Adapter, CE_WARN, "Reset failed");
1699 		return (B_FALSE);
1700 	}
1701 
1702 	return (B_TRUE);
1703 }
1704 
1705 boolean_t
1706 e1000g_global_reset(struct e1000g *Adapter)
1707 {
1708 	e1000g_stop(Adapter, B_TRUE);
1709 
1710 	Adapter->init_count = 0;
1711 
1712 	if (e1000g_start(Adapter, B_TRUE)) {
1713 		e1000g_log(Adapter, CE_WARN, "Reset failed");
1714 		return (B_FALSE);
1715 	}
1716 
1717 	return (B_TRUE);
1718 }
1719 
1720 /*
1721  * e1000g_intr_pciexpress - ISR for PCI Express chipsets
1722  *
1723  * This interrupt service routine is for PCI-Express adapters.
1724  * The ICR contents is valid only when the E1000_ICR_INT_ASSERTED
1725  * bit is set.
1726  */
1727 static uint_t
1728 e1000g_intr_pciexpress(caddr_t arg)
1729 {
1730 	struct e1000g *Adapter;
1731 	uint32_t icr;
1732 
1733 	Adapter = (struct e1000g *)arg;
1734 	icr = E1000_READ_REG(&Adapter->shared, E1000_ICR);
1735 
1736 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
1737 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
1738 
1739 	if (icr & E1000_ICR_INT_ASSERTED) {
1740 		/*
1741 		 * E1000_ICR_INT_ASSERTED bit was set:
1742 		 * Read(Clear) the ICR, claim this interrupt,
1743 		 * look for work to do.
1744 		 */
1745 		e1000g_intr_work(Adapter, icr);
1746 		return (DDI_INTR_CLAIMED);
1747 	} else {
1748 		/*
1749 		 * E1000_ICR_INT_ASSERTED bit was not set:
1750 		 * Don't claim this interrupt, return immediately.
1751 		 */
1752 		return (DDI_INTR_UNCLAIMED);
1753 	}
1754 }
1755 
1756 /*
1757  * e1000g_intr - ISR for PCI/PCI-X chipsets
1758  *
1759  * This interrupt service routine is for PCI/PCI-X adapters.
1760  * We check the ICR contents no matter the E1000_ICR_INT_ASSERTED
1761  * bit is set or not.
1762  */
1763 static uint_t
1764 e1000g_intr(caddr_t arg)
1765 {
1766 	struct e1000g *Adapter;
1767 	uint32_t icr;
1768 
1769 	Adapter = (struct e1000g *)arg;
1770 	icr = E1000_READ_REG(&Adapter->shared, E1000_ICR);
1771 
1772 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
1773 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
1774 
1775 	if (icr) {
1776 		/*
1777 		 * Any bit was set in ICR:
1778 		 * Read(Clear) the ICR, claim this interrupt,
1779 		 * look for work to do.
1780 		 */
1781 		e1000g_intr_work(Adapter, icr);
1782 		return (DDI_INTR_CLAIMED);
1783 	} else {
1784 		/*
1785 		 * No bit was set in ICR:
1786 		 * Don't claim this interrupt, return immediately.
1787 		 */
1788 		return (DDI_INTR_UNCLAIMED);
1789 	}
1790 }
1791 
1792 /*
1793  * e1000g_intr_work - actual processing of ISR
1794  *
1795  * Read(clear) the ICR contents and call appropriate interrupt
1796  * processing routines.
1797  */
1798 static void
1799 e1000g_intr_work(struct e1000g *Adapter, uint32_t icr)
1800 {
1801 	struct e1000_hw *hw;
1802 	hw = &Adapter->shared;
1803 	e1000g_tx_ring_t *tx_ring = Adapter->tx_ring;
1804 	uint32_t itr;
1805 
1806 	Adapter->rx_pkt_cnt = 0;
1807 	Adapter->tx_pkt_cnt = 0;
1808 
1809 	rw_enter(&Adapter->chip_lock, RW_READER);
1810 	/*
1811 	 * Here we need to check the "chip_state" flag within the chip_lock to
1812 	 * ensure the receive routine will not execute when the adapter is
1813 	 * being reset.
1814 	 */
1815 	if (Adapter->chip_state != E1000G_START) {
1816 		rw_exit(&Adapter->chip_lock);
1817 		return;
1818 	}
1819 
1820 	if (icr & E1000_ICR_RXT0) {
1821 		mblk_t *mp;
1822 
1823 		mp = e1000g_receive(Adapter);
1824 
1825 		rw_exit(&Adapter->chip_lock);
1826 
1827 		if (mp != NULL)
1828 			mac_rx(Adapter->mh, Adapter->mrh, mp);
1829 	} else
1830 		rw_exit(&Adapter->chip_lock);
1831 
1832 	if (icr & E1000_ICR_TXDW) {
1833 		if (!Adapter->tx_intr_enable)
1834 			e1000g_clear_tx_interrupt(Adapter);
1835 
1836 		/* Recycle the tx descriptors */
1837 		rw_enter(&Adapter->chip_lock, RW_READER);
1838 		e1000g_recycle(tx_ring);
1839 		E1000G_DEBUG_STAT(tx_ring->stat_recycle_intr);
1840 		rw_exit(&Adapter->chip_lock);
1841 
1842 		/* Schedule the re-transmit */
1843 		if (tx_ring->resched_needed &&
1844 		    (tx_ring->tbd_avail > DEFAULT_TX_UPDATE_THRESHOLD)) {
1845 			tx_ring->resched_needed = B_FALSE;
1846 			mac_tx_update(Adapter->mh);
1847 			E1000G_STAT(tx_ring->stat_reschedule);
1848 		}
1849 	}
1850 
1851 	if (Adapter->intr_adaptive) {
1852 		itr = e1000g_get_itr(Adapter->rx_pkt_cnt, Adapter->tx_pkt_cnt,
1853 		    Adapter->intr_throttling_rate);
1854 		if (itr) {
1855 			E1000_WRITE_REG(hw, E1000_ITR, itr);
1856 			Adapter->intr_throttling_rate = itr;
1857 		}
1858 	}
1859 
1860 	/*
1861 	 * The Receive Sequence errors RXSEQ and the link status change LSC
1862 	 * are checked to detect that the cable has been pulled out. For
1863 	 * the Wiseman 2.0 silicon, the receive sequence errors interrupt
1864 	 * are an indication that cable is not connected.
1865 	 */
1866 	if ((icr & E1000_ICR_RXSEQ) ||
1867 	    (icr & E1000_ICR_LSC) ||
1868 	    (icr & E1000_ICR_GPI_EN1)) {
1869 		boolean_t link_changed;
1870 		timeout_id_t tid = 0;
1871 
1872 		stop_watchdog_timer(Adapter);
1873 
1874 		rw_enter(&Adapter->chip_lock, RW_WRITER);
1875 
1876 		/*
1877 		 * Because we got a link-status-change interrupt, force
1878 		 * e1000_check_for_link() to look at phy
1879 		 */
1880 		Adapter->shared.mac.get_link_status = B_TRUE;
1881 
1882 		/* e1000g_link_check takes care of link status change */
1883 		link_changed = e1000g_link_check(Adapter);
1884 
1885 		/* Get new phy state */
1886 		e1000g_get_phy_state(Adapter);
1887 
1888 		/*
1889 		 * If the link timer has not timed out, we'll not notify
1890 		 * the upper layer with any link state until the link is up.
1891 		 */
1892 		if (link_changed && !Adapter->link_complete) {
1893 			if (Adapter->link_state == LINK_STATE_UP) {
1894 				mutex_enter(&Adapter->link_lock);
1895 				Adapter->link_complete = B_TRUE;
1896 				tid = Adapter->link_tid;
1897 				Adapter->link_tid = 0;
1898 				mutex_exit(&Adapter->link_lock);
1899 			} else {
1900 				link_changed = B_FALSE;
1901 			}
1902 		}
1903 		rw_exit(&Adapter->chip_lock);
1904 
1905 		if (link_changed) {
1906 			if (tid != 0)
1907 				(void) untimeout(tid);
1908 
1909 			/*
1910 			 * Workaround for esb2. Data stuck in fifo on a link
1911 			 * down event. Reset the adapter to recover it.
1912 			 */
1913 			if ((Adapter->link_state == LINK_STATE_DOWN) &&
1914 			    (Adapter->shared.mac.type == e1000_80003es2lan))
1915 				(void) e1000g_reset(Adapter);
1916 
1917 			mac_link_update(Adapter->mh, Adapter->link_state);
1918 		}
1919 
1920 		start_watchdog_timer(Adapter);
1921 	}
1922 }
1923 
1924 static uint32_t
1925 e1000g_get_itr(uint32_t rx_packet, uint32_t tx_packet, uint32_t cur_itr)
1926 {
1927 	uint32_t new_itr;
1928 
1929 	/*
1930 	 * Determine a propper itr according to rx/tx packet count
1931 	 * per interrupt, the value of itr are based on document
1932 	 * and testing.
1933 	 */
1934 	if ((rx_packet < DEFAULT_INTR_PACKET_LOW) ||
1935 	    (tx_packet < DEFAULT_INTR_PACKET_LOW)) {
1936 		new_itr = DEFAULT_INTR_THROTTLING_LOW;
1937 		goto itr_done;
1938 	}
1939 	if ((rx_packet > DEFAULT_INTR_PACKET_HIGH) ||
1940 	    (tx_packet > DEFAULT_INTR_PACKET_HIGH)) {
1941 		new_itr = DEFAULT_INTR_THROTTLING_LOW;
1942 		goto itr_done;
1943 	}
1944 	if (cur_itr < DEFAULT_INTR_THROTTLING_HIGH) {
1945 		new_itr = cur_itr + (DEFAULT_INTR_THROTTLING_HIGH >> 2);
1946 		if (new_itr > DEFAULT_INTR_THROTTLING_HIGH)
1947 			new_itr = DEFAULT_INTR_THROTTLING_HIGH;
1948 	} else
1949 		new_itr = DEFAULT_INTR_THROTTLING_HIGH;
1950 
1951 itr_done:
1952 	if (cur_itr == new_itr)
1953 		return (0);
1954 	else
1955 		return (new_itr);
1956 }
1957 
1958 static void
1959 e1000g_init_unicst(struct e1000g *Adapter)
1960 {
1961 	struct e1000_hw *hw;
1962 	int slot;
1963 
1964 	hw = &Adapter->shared;
1965 
1966 	if (!Adapter->unicst_init) {
1967 		/* Initialize the multiple unicast addresses */
1968 		Adapter->unicst_total = MAX_NUM_UNICAST_ADDRESSES;
1969 
1970 		if ((hw->mac.type == e1000_82571) &&
1971 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
1972 			Adapter->unicst_total--;
1973 
1974 		Adapter->unicst_avail = Adapter->unicst_total - 1;
1975 
1976 		/* Store the default mac address */
1977 		e1000_rar_set(hw, hw->mac.addr, 0);
1978 		if ((hw->mac.type == e1000_82571) &&
1979 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
1980 			e1000_rar_set(hw, hw->mac.addr, LAST_RAR_ENTRY);
1981 
1982 		bcopy(hw->mac.addr, Adapter->unicst_addr[0].mac.addr,
1983 		    ETHERADDRL);
1984 		Adapter->unicst_addr[0].mac.set = 1;
1985 
1986 		for (slot = 1; slot < Adapter->unicst_total; slot++)
1987 			Adapter->unicst_addr[slot].mac.set = 0;
1988 
1989 		Adapter->unicst_init = B_TRUE;
1990 	} else {
1991 		/* Recover the default mac address */
1992 		bcopy(Adapter->unicst_addr[0].mac.addr, hw->mac.addr,
1993 		    ETHERADDRL);
1994 
1995 		/* Store the default mac address */
1996 		e1000_rar_set(hw, hw->mac.addr, 0);
1997 		if ((hw->mac.type == e1000_82571) &&
1998 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
1999 			e1000_rar_set(hw, hw->mac.addr, LAST_RAR_ENTRY);
2000 
2001 		/* Re-configure the RAR registers */
2002 		for (slot = 1; slot < Adapter->unicst_total; slot++)
2003 			e1000_rar_set(hw,
2004 			    Adapter->unicst_addr[slot].mac.addr, slot);
2005 	}
2006 
2007 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
2008 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
2009 }
2010 
2011 static int
2012 e1000g_m_unicst(void *arg, const uint8_t *mac_addr)
2013 {
2014 	struct e1000g *Adapter;
2015 
2016 	Adapter = (struct e1000g *)arg;
2017 
2018 	/* Store the default MAC address */
2019 	bcopy(mac_addr, Adapter->shared.mac.addr, ETHERADDRL);
2020 
2021 	/* Set MAC address in address slot 0, which is the default address */
2022 	return (e1000g_unicst_set(Adapter, mac_addr, 0));
2023 }
2024 
2025 static int
2026 e1000g_unicst_set(struct e1000g *Adapter, const uint8_t *mac_addr,
2027     mac_addr_slot_t slot)
2028 {
2029 	struct e1000_hw *hw;
2030 
2031 	hw = &Adapter->shared;
2032 
2033 	rw_enter(&Adapter->chip_lock, RW_WRITER);
2034 
2035 #ifndef NO_82542_SUPPORT
2036 	/*
2037 	 * The first revision of Wiseman silicon (rev 2.0) has an errata
2038 	 * that requires the receiver to be in reset when any of the
2039 	 * receive address registers (RAR regs) are accessed.  The first
2040 	 * rev of Wiseman silicon also requires MWI to be disabled when
2041 	 * a global reset or a receive reset is issued.  So before we
2042 	 * initialize the RARs, we check the rev of the Wiseman controller
2043 	 * and work around any necessary HW errata.
2044 	 */
2045 	if ((hw->mac.type == e1000_82542) &&
2046 	    (hw->revision_id == E1000_REVISION_2)) {
2047 		e1000_pci_clear_mwi(hw);
2048 		E1000_WRITE_REG(hw, E1000_RCTL, E1000_RCTL_RST);
2049 		msec_delay(5);
2050 	}
2051 #endif
2052 
2053 	bcopy(mac_addr, Adapter->unicst_addr[slot].mac.addr, ETHERADDRL);
2054 	e1000_rar_set(hw, (uint8_t *)mac_addr, slot);
2055 
2056 	if (slot == 0) {
2057 		if ((hw->mac.type == e1000_82571) &&
2058 		    (e1000_get_laa_state_82571(hw) == B_TRUE))
2059 			e1000_rar_set(hw, (uint8_t *)mac_addr, LAST_RAR_ENTRY);
2060 	}
2061 
2062 #ifndef NO_82542_SUPPORT
2063 	/*
2064 	 * If we are using Wiseman rev 2.0 silicon, we will have previously
2065 	 * put the receive in reset, and disabled MWI, to work around some
2066 	 * HW errata.  Now we should take the receiver out of reset, and
2067 	 * re-enabled if MWI if it was previously enabled by the PCI BIOS.
2068 	 */
2069 	if ((hw->mac.type == e1000_82542) &&
2070 	    (hw->revision_id == E1000_REVISION_2)) {
2071 		E1000_WRITE_REG(hw, E1000_RCTL, 0);
2072 		msec_delay(1);
2073 		if (hw->bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
2074 			e1000_pci_set_mwi(hw);
2075 		e1000g_rx_setup(Adapter);
2076 	}
2077 #endif
2078 
2079 	rw_exit(&Adapter->chip_lock);
2080 
2081 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
2082 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
2083 		return (EIO);
2084 	}
2085 
2086 	return (0);
2087 }
2088 
2089 /*
2090  * e1000g_m_unicst_add() - will find an unused address slot, set the
2091  * address value to the one specified, reserve that slot and enable
2092  * the NIC to start filtering on the new MAC address.
2093  * Returns 0 on success.
2094  */
2095 static int
2096 e1000g_m_unicst_add(void *arg, mac_multi_addr_t *maddr)
2097 {
2098 	struct e1000g *Adapter = (struct e1000g *)arg;
2099 	mac_addr_slot_t slot;
2100 	int err;
2101 
2102 	if (mac_unicst_verify(Adapter->mh,
2103 	    maddr->mma_addr, maddr->mma_addrlen) == B_FALSE)
2104 		return (EINVAL);
2105 
2106 	rw_enter(&Adapter->chip_lock, RW_WRITER);
2107 	if (Adapter->unicst_avail == 0) {
2108 		/* no slots available */
2109 		rw_exit(&Adapter->chip_lock);
2110 		return (ENOSPC);
2111 	}
2112 
2113 	/*
2114 	 * Primary/default address is in slot 0. The next addresses
2115 	 * are the multiple MAC addresses. So multiple MAC address 0
2116 	 * is in slot 1, 1 in slot 2, and so on. So the first multiple
2117 	 * MAC address resides in slot 1.
2118 	 */
2119 	for (slot = 1; slot < Adapter->unicst_total; slot++) {
2120 		if (Adapter->unicst_addr[slot].mac.set == 0) {
2121 			Adapter->unicst_addr[slot].mac.set = 1;
2122 			break;
2123 		}
2124 	}
2125 
2126 	ASSERT((slot > 0) && (slot < Adapter->unicst_total));
2127 
2128 	Adapter->unicst_avail--;
2129 	rw_exit(&Adapter->chip_lock);
2130 
2131 	maddr->mma_slot = slot;
2132 
2133 	if ((err = e1000g_unicst_set(Adapter, maddr->mma_addr, slot)) != 0) {
2134 		rw_enter(&Adapter->chip_lock, RW_WRITER);
2135 		Adapter->unicst_addr[slot].mac.set = 0;
2136 		Adapter->unicst_avail++;
2137 		rw_exit(&Adapter->chip_lock);
2138 	}
2139 
2140 	return (err);
2141 }
2142 
2143 /*
2144  * e1000g_m_unicst_remove() - removes a MAC address that was added by a
2145  * call to e1000g_m_unicst_add(). The slot number that was returned in
2146  * e1000g_m_unicst_add() is passed in the call to remove the address.
2147  * Returns 0 on success.
2148  */
2149 static int
2150 e1000g_m_unicst_remove(void *arg, mac_addr_slot_t slot)
2151 {
2152 	struct e1000g *Adapter = (struct e1000g *)arg;
2153 	int err;
2154 
2155 	if ((slot <= 0) || (slot >= Adapter->unicst_total))
2156 		return (EINVAL);
2157 
2158 	rw_enter(&Adapter->chip_lock, RW_WRITER);
2159 	if (Adapter->unicst_addr[slot].mac.set == 1) {
2160 		Adapter->unicst_addr[slot].mac.set = 0;
2161 		Adapter->unicst_avail++;
2162 		rw_exit(&Adapter->chip_lock);
2163 
2164 		/* Copy the default address to the passed slot */
2165 		if (err = e1000g_unicst_set(Adapter,
2166 		    Adapter->unicst_addr[0].mac.addr, slot) != 0) {
2167 			rw_enter(&Adapter->chip_lock, RW_WRITER);
2168 			Adapter->unicst_addr[slot].mac.set = 1;
2169 			Adapter->unicst_avail--;
2170 			rw_exit(&Adapter->chip_lock);
2171 		}
2172 		return (err);
2173 	}
2174 	rw_exit(&Adapter->chip_lock);
2175 
2176 	return (EINVAL);
2177 }
2178 
2179 /*
2180  * e1000g_m_unicst_modify() - modifies the value of an address that
2181  * has been added by e1000g_m_unicst_add(). The new address, address
2182  * length and the slot number that was returned in the call to add
2183  * should be passed to e1000g_m_unicst_modify(). mma_flags should be
2184  * set to 0. Returns 0 on success.
2185  */
2186 static int
2187 e1000g_m_unicst_modify(void *arg, mac_multi_addr_t *maddr)
2188 {
2189 	struct e1000g *Adapter = (struct e1000g *)arg;
2190 	mac_addr_slot_t slot;
2191 
2192 	if (mac_unicst_verify(Adapter->mh,
2193 	    maddr->mma_addr, maddr->mma_addrlen) == B_FALSE)
2194 		return (EINVAL);
2195 
2196 	slot = maddr->mma_slot;
2197 
2198 	if ((slot <= 0) || (slot >= Adapter->unicst_total))
2199 		return (EINVAL);
2200 
2201 	rw_enter(&Adapter->chip_lock, RW_WRITER);
2202 	if (Adapter->unicst_addr[slot].mac.set == 1) {
2203 		rw_exit(&Adapter->chip_lock);
2204 
2205 		return (e1000g_unicst_set(Adapter, maddr->mma_addr, slot));
2206 	}
2207 	rw_exit(&Adapter->chip_lock);
2208 
2209 	return (EINVAL);
2210 }
2211 
2212 /*
2213  * e1000g_m_unicst_get() - will get the MAC address and all other
2214  * information related to the address slot passed in mac_multi_addr_t.
2215  * mma_flags should be set to 0 in the call.
2216  * On return, mma_flags can take the following values:
2217  * 1) MMAC_SLOT_UNUSED
2218  * 2) MMAC_SLOT_USED | MMAC_VENDOR_ADDR
2219  * 3) MMAC_SLOT_UNUSED | MMAC_VENDOR_ADDR
2220  * 4) MMAC_SLOT_USED
2221  */
2222 static int
2223 e1000g_m_unicst_get(void *arg, mac_multi_addr_t *maddr)
2224 {
2225 	struct e1000g *Adapter = (struct e1000g *)arg;
2226 	mac_addr_slot_t slot;
2227 
2228 	slot = maddr->mma_slot;
2229 
2230 	if ((slot <= 0) || (slot >= Adapter->unicst_total))
2231 		return (EINVAL);
2232 
2233 	rw_enter(&Adapter->chip_lock, RW_WRITER);
2234 	if (Adapter->unicst_addr[slot].mac.set == 1) {
2235 		bcopy(Adapter->unicst_addr[slot].mac.addr,
2236 		    maddr->mma_addr, ETHERADDRL);
2237 		maddr->mma_flags = MMAC_SLOT_USED;
2238 	} else {
2239 		maddr->mma_flags = MMAC_SLOT_UNUSED;
2240 	}
2241 	rw_exit(&Adapter->chip_lock);
2242 
2243 	return (0);
2244 }
2245 
2246 static int
2247 multicst_add(struct e1000g *Adapter, const uint8_t *multiaddr)
2248 {
2249 	struct e1000_hw *hw = &Adapter->shared;
2250 	unsigned i;
2251 	int res = 0;
2252 
2253 	rw_enter(&Adapter->chip_lock, RW_WRITER);
2254 
2255 	if ((multiaddr[0] & 01) == 0) {
2256 		res = EINVAL;
2257 		goto done;
2258 	}
2259 
2260 	if (Adapter->mcast_count >= MAX_NUM_MULTICAST_ADDRESSES) {
2261 		res = ENOENT;
2262 		goto done;
2263 	}
2264 
2265 	bcopy(multiaddr,
2266 	    &Adapter->mcast_table[Adapter->mcast_count], ETHERADDRL);
2267 	Adapter->mcast_count++;
2268 
2269 	/*
2270 	 * Update the MC table in the hardware
2271 	 */
2272 	e1000g_clear_interrupt(Adapter);
2273 
2274 	e1000g_setup_multicast(Adapter);
2275 
2276 #ifndef NO_82542_SUPPORT
2277 	if ((hw->mac.type == e1000_82542) &&
2278 	    (hw->revision_id == E1000_REVISION_2))
2279 		e1000g_rx_setup(Adapter);
2280 #endif
2281 
2282 	e1000g_mask_interrupt(Adapter);
2283 
2284 done:
2285 	rw_exit(&Adapter->chip_lock);
2286 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
2287 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
2288 		res = EIO;
2289 	}
2290 
2291 	return (res);
2292 }
2293 
2294 static int
2295 multicst_remove(struct e1000g *Adapter, const uint8_t *multiaddr)
2296 {
2297 	struct e1000_hw *hw = &Adapter->shared;
2298 	unsigned i;
2299 
2300 	rw_enter(&Adapter->chip_lock, RW_WRITER);
2301 
2302 	for (i = 0; i < Adapter->mcast_count; i++) {
2303 		if (bcmp(multiaddr, &Adapter->mcast_table[i],
2304 		    ETHERADDRL) == 0) {
2305 			for (i++; i < Adapter->mcast_count; i++) {
2306 				Adapter->mcast_table[i - 1] =
2307 				    Adapter->mcast_table[i];
2308 			}
2309 			Adapter->mcast_count--;
2310 			break;
2311 		}
2312 	}
2313 
2314 	/*
2315 	 * Update the MC table in the hardware
2316 	 */
2317 	e1000g_clear_interrupt(Adapter);
2318 
2319 	e1000g_setup_multicast(Adapter);
2320 
2321 #ifndef NO_82542_SUPPORT
2322 	if ((hw->mac.type == e1000_82542) &&
2323 	    (hw->revision_id == E1000_REVISION_2))
2324 		e1000g_rx_setup(Adapter);
2325 #endif
2326 
2327 	e1000g_mask_interrupt(Adapter);
2328 
2329 done:
2330 	rw_exit(&Adapter->chip_lock);
2331 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
2332 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
2333 		return (EIO);
2334 	}
2335 
2336 	return (0);
2337 }
2338 
2339 /*
2340  * e1000g_setup_multicast - setup multicast data structures
2341  *
2342  * This routine initializes all of the multicast related structures.
2343  */
2344 void
2345 e1000g_setup_multicast(struct e1000g *Adapter)
2346 {
2347 	uint8_t *mc_addr_list;
2348 	uint32_t mc_addr_count;
2349 	uint32_t rctl;
2350 	struct e1000_hw *hw;
2351 
2352 	hw = &Adapter->shared;
2353 
2354 	/*
2355 	 * The e1000g has the ability to do perfect filtering of 16
2356 	 * addresses. The driver uses one of the e1000g's 16 receive
2357 	 * address registers for its node/network/mac/individual address.
2358 	 * So, we have room for up to 15 multicast addresses in the CAM,
2359 	 * additional MC addresses are handled by the MTA (Multicast Table
2360 	 * Array)
2361 	 */
2362 
2363 	rctl = E1000_READ_REG(hw, E1000_RCTL);
2364 
2365 	mc_addr_list = (uint8_t *)Adapter->mcast_table;
2366 
2367 	if (Adapter->mcast_count > MAX_NUM_MULTICAST_ADDRESSES) {
2368 		E1000G_DEBUGLOG_1(Adapter, CE_WARN,
2369 		    "Adapter requested more than %d MC Addresses.\n",
2370 		    MAX_NUM_MULTICAST_ADDRESSES);
2371 		mc_addr_count = MAX_NUM_MULTICAST_ADDRESSES;
2372 	} else {
2373 		/*
2374 		 * Set the number of MC addresses that we are being
2375 		 * requested to use
2376 		 */
2377 		mc_addr_count = Adapter->mcast_count;
2378 	}
2379 #ifndef NO_82542_SUPPORT
2380 	/*
2381 	 * The Wiseman 2.0 silicon has an errata by which the receiver will
2382 	 * hang  while writing to the receive address registers if the receiver
2383 	 * is not in reset before writing to the registers. Updating the RAR
2384 	 * is done during the setting up of the multicast table, hence the
2385 	 * receiver has to be put in reset before updating the multicast table
2386 	 * and then taken out of reset at the end
2387 	 */
2388 	/*
2389 	 * if WMI was enabled then dis able it before issueing the global
2390 	 * reset to the hardware.
2391 	 */
2392 	/*
2393 	 * Only required for WISEMAN_2_0
2394 	 */
2395 	if ((hw->mac.type == e1000_82542) &&
2396 	    (hw->revision_id == E1000_REVISION_2)) {
2397 		e1000_pci_clear_mwi(hw);
2398 		/*
2399 		 * The e1000g must be in reset before changing any RA
2400 		 * registers. Reset receive unit.  The chip will remain in
2401 		 * the reset state until software explicitly restarts it.
2402 		 */
2403 		E1000_WRITE_REG(hw, E1000_RCTL, E1000_RCTL_RST);
2404 		/* Allow receiver time to go in to reset */
2405 		msec_delay(5);
2406 	}
2407 #endif
2408 
2409 	e1000_mc_addr_list_update(hw, mc_addr_list, mc_addr_count,
2410 	    Adapter->unicst_total, hw->mac.rar_entry_count);
2411 
2412 #ifndef NO_82542_SUPPORT
2413 	/*
2414 	 * Only for Wiseman_2_0
2415 	 * If MWI was enabled then re-enable it after issueing (as we
2416 	 * disabled it up there) the receive reset command.
2417 	 * Wainwright does not have a receive reset command and only thing
2418 	 * close to it is global reset which will require tx setup also
2419 	 */
2420 	if ((hw->mac.type == e1000_82542) &&
2421 	    (hw->revision_id == E1000_REVISION_2)) {
2422 		/*
2423 		 * if WMI was enabled then reenable it after issueing the
2424 		 * global or receive reset to the hardware.
2425 		 */
2426 
2427 		/*
2428 		 * Take receiver out of reset
2429 		 * clear E1000_RCTL_RST bit (and all others)
2430 		 */
2431 		E1000_WRITE_REG(hw, E1000_RCTL, 0);
2432 		msec_delay(5);
2433 		if (hw->bus.pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
2434 			e1000_pci_set_mwi(hw);
2435 	}
2436 #endif
2437 
2438 	/*
2439 	 * Restore original value
2440 	 */
2441 	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
2442 }
2443 
2444 int
2445 e1000g_m_multicst(void *arg, boolean_t add, const uint8_t *addr)
2446 {
2447 	struct e1000g *Adapter = (struct e1000g *)arg;
2448 
2449 	return ((add) ? multicst_add(Adapter, addr)
2450 	    : multicst_remove(Adapter, addr));
2451 }
2452 
2453 int
2454 e1000g_m_promisc(void *arg, boolean_t on)
2455 {
2456 	struct e1000g *Adapter = (struct e1000g *)arg;
2457 	uint32_t rctl;
2458 
2459 	rw_enter(&Adapter->chip_lock, RW_WRITER);
2460 
2461 	rctl = E1000_READ_REG(&Adapter->shared, E1000_RCTL);
2462 
2463 	if (on)
2464 		rctl |=
2465 		    (E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_BAM);
2466 	else
2467 		rctl &= (~(E1000_RCTL_UPE | E1000_RCTL_MPE));
2468 
2469 	E1000_WRITE_REG(&Adapter->shared, E1000_RCTL, rctl);
2470 
2471 	Adapter->e1000g_promisc = on;
2472 
2473 	rw_exit(&Adapter->chip_lock);
2474 
2475 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
2476 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
2477 		return (EIO);
2478 	}
2479 
2480 	return (0);
2481 }
2482 
2483 static boolean_t
2484 e1000g_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
2485 {
2486 	struct e1000g *Adapter = (struct e1000g *)arg;
2487 	struct e1000_hw *hw = &Adapter->shared;
2488 
2489 	switch (cap) {
2490 	case MAC_CAPAB_HCKSUM: {
2491 		uint32_t *txflags = cap_data;
2492 		/*
2493 		 * Checksum on/off selection via global parameters.
2494 		 *
2495 		 * If the chip is flagged as not capable of (correctly)
2496 		 * handling checksumming, we don't enable it on either
2497 		 * Rx or Tx side.  Otherwise, we take this chip's settings
2498 		 * from the patchable global defaults.
2499 		 *
2500 		 * We advertise our capabilities only if TX offload is
2501 		 * enabled.  On receive, the stack will accept checksummed
2502 		 * packets anyway, even if we haven't said we can deliver
2503 		 * them.
2504 		 */
2505 		switch (hw->mac.type) {
2506 		case e1000_82540:
2507 		case e1000_82544:
2508 		case e1000_82545:
2509 		case e1000_82545_rev_3:
2510 		case e1000_82546:
2511 		case e1000_82546_rev_3:
2512 		case e1000_82571:
2513 		case e1000_82572:
2514 		case e1000_82573:
2515 		case e1000_80003es2lan:
2516 			if (Adapter->tx_hcksum_enabled)
2517 				*txflags = HCKSUM_IPHDRCKSUM |
2518 				    HCKSUM_INET_PARTIAL;
2519 			else
2520 				return (B_FALSE);
2521 			break;
2522 
2523 		/*
2524 		 * For the following Intel PRO/1000 chipsets, we have not
2525 		 * tested the hardware checksum offload capability, so we
2526 		 * disable the capability for them.
2527 		 *	e1000_82542,
2528 		 *	e1000_82543,
2529 		 *	e1000_82541,
2530 		 *	e1000_82541_rev_2,
2531 		 *	e1000_82547,
2532 		 *	e1000_82547_rev_2,
2533 		 */
2534 		default:
2535 			return (B_FALSE);
2536 		}
2537 
2538 		break;
2539 	}
2540 	case MAC_CAPAB_POLL:
2541 		/*
2542 		 * There's nothing for us to fill in, simply returning
2543 		 * B_TRUE stating that we support polling is sufficient.
2544 		 */
2545 		break;
2546 
2547 	case MAC_CAPAB_MULTIADDRESS: {
2548 		multiaddress_capab_t *mmacp = cap_data;
2549 
2550 		/*
2551 		 * The number of MAC addresses made available by
2552 		 * this capability is one less than the total as
2553 		 * the primary address in slot 0 is counted in
2554 		 * the total.
2555 		 */
2556 		mmacp->maddr_naddr = Adapter->unicst_total - 1;
2557 		mmacp->maddr_naddrfree = Adapter->unicst_avail;
2558 		/* No multiple factory addresses, set mma_flag to 0 */
2559 		mmacp->maddr_flag = 0;
2560 		mmacp->maddr_handle = Adapter;
2561 		mmacp->maddr_add = e1000g_m_unicst_add;
2562 		mmacp->maddr_remove = e1000g_m_unicst_remove;
2563 		mmacp->maddr_modify = e1000g_m_unicst_modify;
2564 		mmacp->maddr_get = e1000g_m_unicst_get;
2565 		mmacp->maddr_reserve = NULL;
2566 		break;
2567 	}
2568 	default:
2569 		return (B_FALSE);
2570 	}
2571 	return (B_TRUE);
2572 }
2573 
2574 /*
2575  * e1000g_get_conf - get configurations set in e1000g.conf
2576  *
2577  * This routine gets user-configured values out of the configuration
2578  * file e1000g.conf.
2579  *
2580  * For each configurable value, there is a minimum, a maximum, and a
2581  * default.
2582  * If user does not configure a value, use the default.
2583  * If user configures below the minimum, use the minumum.
2584  * If user configures above the maximum, use the maxumum.
2585  */
2586 static void
2587 e1000g_get_conf(struct e1000g *Adapter)
2588 {
2589 	struct e1000_hw *hw = &Adapter->shared;
2590 	boolean_t tbi_compatibility = B_FALSE;
2591 
2592 	/*
2593 	 * get each configurable property from e1000g.conf
2594 	 */
2595 
2596 	/*
2597 	 * NumTxDescriptors
2598 	 */
2599 	Adapter->tx_desc_num =
2600 	    e1000g_get_prop(Adapter, "NumTxDescriptors",
2601 	    MIN_NUM_TX_DESCRIPTOR, MAX_NUM_TX_DESCRIPTOR,
2602 	    DEFAULT_NUM_TX_DESCRIPTOR);
2603 
2604 	/*
2605 	 * NumRxDescriptors
2606 	 */
2607 	Adapter->rx_desc_num =
2608 	    e1000g_get_prop(Adapter, "NumRxDescriptors",
2609 	    MIN_NUM_RX_DESCRIPTOR, MAX_NUM_RX_DESCRIPTOR,
2610 	    DEFAULT_NUM_RX_DESCRIPTOR);
2611 
2612 	/*
2613 	 * NumRxFreeList
2614 	 */
2615 	Adapter->rx_freelist_num =
2616 	    e1000g_get_prop(Adapter, "NumRxFreeList",
2617 	    MIN_NUM_RX_FREELIST, MAX_NUM_RX_FREELIST,
2618 	    DEFAULT_NUM_RX_FREELIST);
2619 
2620 	/*
2621 	 * NumTxPacketList
2622 	 */
2623 	Adapter->tx_freelist_num =
2624 	    e1000g_get_prop(Adapter, "NumTxPacketList",
2625 	    MIN_NUM_TX_FREELIST, MAX_NUM_TX_FREELIST,
2626 	    DEFAULT_NUM_TX_FREELIST);
2627 
2628 	/*
2629 	 * FlowControl
2630 	 */
2631 	hw->mac.fc_send_xon = B_TRUE;
2632 	hw->mac.fc =
2633 	    e1000g_get_prop(Adapter, "FlowControl",
2634 	    e1000_fc_none, 4, DEFAULT_FLOW_CONTROL);
2635 	/* 4 is the setting that says "let the eeprom decide" */
2636 	if (hw->mac.fc == 4)
2637 		hw->mac.fc = e1000_fc_default;
2638 
2639 	/*
2640 	 * Max Num Receive Packets on Interrupt
2641 	 */
2642 	Adapter->rx_limit_onintr =
2643 	    e1000g_get_prop(Adapter, "MaxNumReceivePackets",
2644 	    MIN_RX_LIMIT_ON_INTR, MAX_RX_LIMIT_ON_INTR,
2645 	    DEFAULT_RX_LIMIT_ON_INTR);
2646 
2647 	/*
2648 	 * PHY master slave setting
2649 	 */
2650 	hw->phy.ms_type =
2651 	    e1000g_get_prop(Adapter, "SetMasterSlave",
2652 	    e1000_ms_hw_default, e1000_ms_auto,
2653 	    e1000_ms_hw_default);
2654 
2655 	/*
2656 	 * Parameter which controls TBI mode workaround, which is only
2657 	 * needed on certain switches such as Cisco 6500/Foundry
2658 	 */
2659 	tbi_compatibility =
2660 	    e1000g_get_prop(Adapter, "TbiCompatibilityEnable",
2661 	    0, 1, DEFAULT_TBI_COMPAT_ENABLE);
2662 	e1000_set_tbi_compatibility_82543(hw, tbi_compatibility);
2663 
2664 	/*
2665 	 * MSI Enable
2666 	 */
2667 	Adapter->msi_enabled =
2668 	    e1000g_get_prop(Adapter, "MSIEnable",
2669 	    0, 1, DEFAULT_MSI_ENABLE);
2670 
2671 	/*
2672 	 * Interrupt Throttling Rate
2673 	 */
2674 	Adapter->intr_throttling_rate =
2675 	    e1000g_get_prop(Adapter, "intr_throttling_rate",
2676 	    MIN_INTR_THROTTLING, MAX_INTR_THROTTLING,
2677 	    DEFAULT_INTR_THROTTLING);
2678 
2679 	/*
2680 	 * Adaptive Interrupt Blanking Enable/Disable
2681 	 * It is enabled by default
2682 	 */
2683 	Adapter->intr_adaptive =
2684 	    (e1000g_get_prop(Adapter, "intr_adaptive", 0, 1, 1) == 1) ?
2685 	    B_TRUE : B_FALSE;
2686 
2687 	/*
2688 	 * Tx recycle threshold
2689 	 */
2690 	Adapter->tx_recycle_thresh =
2691 	    e1000g_get_prop(Adapter, "tx_recycle_thresh",
2692 	    MIN_TX_RECYCLE_THRESHOLD, MAX_TX_RECYCLE_THRESHOLD,
2693 	    DEFAULT_TX_RECYCLE_THRESHOLD);
2694 
2695 	/*
2696 	 * Tx recycle descriptor number
2697 	 */
2698 	Adapter->tx_recycle_num =
2699 	    e1000g_get_prop(Adapter, "tx_recycle_num",
2700 	    MIN_TX_RECYCLE_NUM, MAX_TX_RECYCLE_NUM,
2701 	    DEFAULT_TX_RECYCLE_NUM);
2702 
2703 	/*
2704 	 * Hardware checksum enable/disable parameter
2705 	 */
2706 	Adapter->tx_hcksum_enabled =
2707 	    e1000g_get_prop(Adapter, "tx_hcksum_enabled",
2708 	    0, 1, DEFAULT_TX_HCKSUM_ENABLE);
2709 
2710 }
2711 
2712 /*
2713  * e1000g_get_prop - routine to read properties
2714  *
2715  * Get a user-configure property value out of the configuration
2716  * file e1000g.conf.
2717  *
2718  * Caller provides name of the property, a default value, a minimum
2719  * value, and a maximum value.
2720  *
2721  * Return configured value of the property, with default, minimum and
2722  * maximum properly applied.
2723  */
2724 static int
2725 e1000g_get_prop(struct e1000g *Adapter,	/* point to per-adapter structure */
2726     char *propname,		/* name of the property */
2727     int minval,			/* minimum acceptable value */
2728     int maxval,			/* maximim acceptable value */
2729     int defval)			/* default value */
2730 {
2731 	int propval;		/* value returned for requested property */
2732 	int *props;		/* point to array of properties returned */
2733 	uint_t nprops;		/* number of property value returned */
2734 
2735 	/*
2736 	 * get the array of properties from the config file
2737 	 */
2738 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, Adapter->dip,
2739 	    DDI_PROP_DONTPASS, propname, &props, &nprops) == DDI_PROP_SUCCESS) {
2740 		/* got some properties, test if we got enough */
2741 		if (Adapter->instance < nprops) {
2742 			propval = props[Adapter->instance];
2743 		} else {
2744 			/* not enough properties configured */
2745 			propval = defval;
2746 			E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
2747 			    "Not Enough %s values found in e1000g.conf"
2748 			    " - set to %d\n",
2749 			    propname, propval);
2750 		}
2751 
2752 		/* free memory allocated for properties */
2753 		ddi_prop_free(props);
2754 
2755 	} else {
2756 		propval = defval;
2757 	}
2758 
2759 	/*
2760 	 * enforce limits
2761 	 */
2762 	if (propval > maxval) {
2763 		propval = maxval;
2764 		E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
2765 		    "Too High %s value in e1000g.conf - set to %d\n",
2766 		    propname, propval);
2767 	}
2768 
2769 	if (propval < minval) {
2770 		propval = minval;
2771 		E1000G_DEBUGLOG_2(Adapter, E1000G_INFO_LEVEL,
2772 		    "Too Low %s value in e1000g.conf - set to %d\n",
2773 		    propname, propval);
2774 	}
2775 
2776 	return (propval);
2777 }
2778 
2779 static boolean_t
2780 e1000g_link_check(struct e1000g *Adapter)
2781 {
2782 	uint16_t speed, duplex, phydata;
2783 	boolean_t link_changed = B_FALSE;
2784 	struct e1000_hw *hw;
2785 	uint32_t reg_tarc;
2786 
2787 	hw = &Adapter->shared;
2788 
2789 	if (e1000g_link_up(Adapter)) {
2790 		/*
2791 		 * The Link is up, check whether it was marked as down earlier
2792 		 */
2793 		if (Adapter->link_state != LINK_STATE_UP) {
2794 			e1000_get_speed_and_duplex(hw, &speed, &duplex);
2795 			Adapter->link_speed = speed;
2796 			Adapter->link_duplex = duplex;
2797 			Adapter->link_state = LINK_STATE_UP;
2798 			link_changed = B_TRUE;
2799 
2800 			Adapter->tx_link_down_timeout = 0;
2801 
2802 			if ((hw->mac.type == e1000_82571) ||
2803 			    (hw->mac.type == e1000_82572)) {
2804 				reg_tarc = E1000_READ_REG(hw, E1000_TARC0);
2805 				if (speed == SPEED_1000)
2806 					reg_tarc |= (1 << 21);
2807 				else
2808 					reg_tarc &= ~(1 << 21);
2809 				E1000_WRITE_REG(hw, E1000_TARC0, reg_tarc);
2810 			}
2811 		}
2812 		Adapter->smartspeed = 0;
2813 	} else {
2814 		if (Adapter->link_state != LINK_STATE_DOWN) {
2815 			Adapter->link_speed = 0;
2816 			Adapter->link_duplex = 0;
2817 			Adapter->link_state = LINK_STATE_DOWN;
2818 			link_changed = B_TRUE;
2819 
2820 			/*
2821 			 * SmartSpeed workaround for Tabor/TanaX, When the
2822 			 * driver loses link disable auto master/slave
2823 			 * resolution.
2824 			 */
2825 			if (hw->phy.type == e1000_phy_igp) {
2826 				e1000_read_phy_reg(hw,
2827 				    PHY_1000T_CTRL, &phydata);
2828 				phydata |= CR_1000T_MS_ENABLE;
2829 				e1000_write_phy_reg(hw,
2830 				    PHY_1000T_CTRL, phydata);
2831 			}
2832 		} else {
2833 			e1000g_smartspeed(Adapter);
2834 		}
2835 
2836 		if (Adapter->chip_state == E1000G_START) {
2837 			if (Adapter->tx_link_down_timeout <
2838 			    MAX_TX_LINK_DOWN_TIMEOUT) {
2839 				Adapter->tx_link_down_timeout++;
2840 			} else if (Adapter->tx_link_down_timeout ==
2841 			    MAX_TX_LINK_DOWN_TIMEOUT) {
2842 				e1000g_tx_clean(Adapter);
2843 				Adapter->tx_link_down_timeout++;
2844 			}
2845 		}
2846 	}
2847 
2848 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
2849 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
2850 
2851 	return (link_changed);
2852 }
2853 
2854 static void
2855 e1000g_local_timer(void *ws)
2856 {
2857 	struct e1000g *Adapter = (struct e1000g *)ws;
2858 	struct e1000_hw *hw;
2859 	e1000g_ether_addr_t ether_addr;
2860 	boolean_t link_changed;
2861 
2862 	hw = &Adapter->shared;
2863 
2864 	if (Adapter->chip_state == E1000G_ERROR) {
2865 		Adapter->reset_count++;
2866 		if (e1000g_global_reset(Adapter))
2867 			ddi_fm_service_impact(Adapter->dip,
2868 			    DDI_SERVICE_RESTORED);
2869 		else
2870 			ddi_fm_service_impact(Adapter->dip,
2871 			    DDI_SERVICE_LOST);
2872 		return;
2873 	}
2874 
2875 	if (e1000g_stall_check(Adapter)) {
2876 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
2877 		    "Tx stall detected. Activate automatic recovery.\n");
2878 		e1000g_fm_ereport(Adapter, DDI_FM_DEVICE_STALL);
2879 		Adapter->reset_count++;
2880 		if (e1000g_reset(Adapter))
2881 			ddi_fm_service_impact(Adapter->dip,
2882 			    DDI_SERVICE_RESTORED);
2883 		else
2884 			ddi_fm_service_impact(Adapter->dip,
2885 			    DDI_SERVICE_LOST);
2886 		return;
2887 	}
2888 
2889 	link_changed = B_FALSE;
2890 	rw_enter(&Adapter->chip_lock, RW_READER);
2891 	if (Adapter->link_complete)
2892 		link_changed = e1000g_link_check(Adapter);
2893 	rw_exit(&Adapter->chip_lock);
2894 
2895 	if (link_changed) {
2896 		/*
2897 		 * Workaround for esb2. Data stuck in fifo on a link
2898 		 * down event. Reset the adapter to recover it.
2899 		 */
2900 		if ((Adapter->link_state == LINK_STATE_DOWN) &&
2901 		    (hw->mac.type == e1000_80003es2lan))
2902 			(void) e1000g_reset(Adapter);
2903 
2904 		mac_link_update(Adapter->mh, Adapter->link_state);
2905 	}
2906 
2907 	/*
2908 	 * With 82571 controllers, any locally administered address will
2909 	 * be overwritten when there is a reset on the other port.
2910 	 * Detect this circumstance and correct it.
2911 	 */
2912 	if ((hw->mac.type == e1000_82571) &&
2913 	    (e1000_get_laa_state_82571(hw) == B_TRUE)) {
2914 		ether_addr.reg.low = E1000_READ_REG_ARRAY(hw, E1000_RA, 0);
2915 		ether_addr.reg.high = E1000_READ_REG_ARRAY(hw, E1000_RA, 1);
2916 
2917 		ether_addr.reg.low = ntohl(ether_addr.reg.low);
2918 		ether_addr.reg.high = ntohl(ether_addr.reg.high);
2919 
2920 		if ((ether_addr.mac.addr[5] != hw->mac.addr[0]) ||
2921 		    (ether_addr.mac.addr[4] != hw->mac.addr[1]) ||
2922 		    (ether_addr.mac.addr[3] != hw->mac.addr[2]) ||
2923 		    (ether_addr.mac.addr[2] != hw->mac.addr[3]) ||
2924 		    (ether_addr.mac.addr[1] != hw->mac.addr[4]) ||
2925 		    (ether_addr.mac.addr[0] != hw->mac.addr[5])) {
2926 			e1000_rar_set(hw, hw->mac.addr, 0);
2927 		}
2928 	}
2929 
2930 	/*
2931 	 * Long TTL workaround for 82541/82547
2932 	 */
2933 	e1000_igp_ttl_workaround_82547(hw);
2934 
2935 	/*
2936 	 * Check for Adaptive IFS settings If there are lots of collisions
2937 	 * change the value in steps...
2938 	 * These properties should only be set for 10/100
2939 	 */
2940 	if ((hw->media_type == e1000_media_type_copper) &&
2941 	    ((Adapter->link_speed == SPEED_100) ||
2942 	    (Adapter->link_speed == SPEED_10))) {
2943 		e1000_update_adaptive(hw);
2944 	}
2945 	/*
2946 	 * Set Timer Interrupts
2947 	 */
2948 	E1000_WRITE_REG(hw, E1000_ICS, E1000_IMS_RXT0);
2949 
2950 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK)
2951 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
2952 
2953 	restart_watchdog_timer(Adapter);
2954 }
2955 
2956 /*
2957  * The function e1000g_link_timer() is called when the timer for link setup
2958  * is expired, which indicates the completion of the link setup. The link
2959  * state will not be updated until the link setup is completed. And the
2960  * link state will not be sent to the upper layer through mac_link_update()
2961  * in this function. It will be updated in the local timer routine or the
2962  * interrupt service routine after the interface is started (plumbed).
2963  */
2964 static void
2965 e1000g_link_timer(void *arg)
2966 {
2967 	struct e1000g *Adapter = (struct e1000g *)arg;
2968 
2969 	mutex_enter(&Adapter->link_lock);
2970 	Adapter->link_complete = B_TRUE;
2971 	Adapter->link_tid = 0;
2972 	mutex_exit(&Adapter->link_lock);
2973 }
2974 
2975 /*
2976  * e1000g_force_speed_duplex - read forced speed/duplex out of e1000g.conf
2977  *
2978  * This function read the forced speed and duplex for 10/100 Mbps speeds
2979  * and also for 1000 Mbps speeds from the e1000g.conf file
2980  */
2981 static void
2982 e1000g_force_speed_duplex(struct e1000g *Adapter)
2983 {
2984 	int forced;
2985 	struct e1000_mac_info *mac = &Adapter->shared.mac;
2986 	struct e1000_phy_info *phy = &Adapter->shared.phy;
2987 
2988 	/*
2989 	 * get value out of config file
2990 	 */
2991 	forced = e1000g_get_prop(Adapter, "ForceSpeedDuplex",
2992 	    GDIAG_10_HALF, GDIAG_ANY, GDIAG_ANY);
2993 
2994 	switch (forced) {
2995 	case GDIAG_10_HALF:
2996 		/*
2997 		 * Disable Auto Negotiation
2998 		 */
2999 		mac->autoneg = B_FALSE;
3000 		mac->forced_speed_duplex = ADVERTISE_10_HALF;
3001 		break;
3002 	case GDIAG_10_FULL:
3003 		/*
3004 		 * Disable Auto Negotiation
3005 		 */
3006 		mac->autoneg = B_FALSE;
3007 		mac->forced_speed_duplex = ADVERTISE_10_FULL;
3008 		break;
3009 	case GDIAG_100_HALF:
3010 		/*
3011 		 * Disable Auto Negotiation
3012 		 */
3013 		mac->autoneg = B_FALSE;
3014 		mac->forced_speed_duplex = ADVERTISE_100_HALF;
3015 		break;
3016 	case GDIAG_100_FULL:
3017 		/*
3018 		 * Disable Auto Negotiation
3019 		 */
3020 		mac->autoneg = B_FALSE;
3021 		mac->forced_speed_duplex = ADVERTISE_100_FULL;
3022 		break;
3023 	case GDIAG_1000_FULL:
3024 		/*
3025 		 * The gigabit spec requires autonegotiation.  Therefore,
3026 		 * when the user wants to force the speed to 1000Mbps, we
3027 		 * enable AutoNeg, but only allow the harware to advertise
3028 		 * 1000Mbps.  This is different from 10/100 operation, where
3029 		 * we are allowed to link without any negotiation.
3030 		 */
3031 		mac->autoneg = B_TRUE;
3032 		phy->autoneg_advertised = ADVERTISE_1000_FULL;
3033 		break;
3034 	default:	/* obey the setting of AutoNegAdvertised */
3035 		mac->autoneg = B_TRUE;
3036 		phy->autoneg_advertised =
3037 		    (uint16_t)e1000g_get_prop(Adapter, "AutoNegAdvertised",
3038 		    0, AUTONEG_ADVERTISE_SPEED_DEFAULT,
3039 		    AUTONEG_ADVERTISE_SPEED_DEFAULT);
3040 		break;
3041 	}	/* switch */
3042 }
3043 
3044 /*
3045  * e1000g_get_max_frame_size - get jumbo frame setting from e1000g.conf
3046  *
3047  * This function reads MaxFrameSize from e1000g.conf
3048  */
3049 static void
3050 e1000g_get_max_frame_size(struct e1000g *Adapter)
3051 {
3052 	int max_frame;
3053 	struct e1000_mac_info *mac = &Adapter->shared.mac;
3054 	struct e1000_phy_info *phy = &Adapter->shared.phy;
3055 
3056 	/*
3057 	 * get value out of config file
3058 	 */
3059 	max_frame = e1000g_get_prop(Adapter, "MaxFrameSize", 0, 3, 0);
3060 
3061 	switch (max_frame) {
3062 	case 0:
3063 		mac->max_frame_size = ETHERMAX;
3064 		break;
3065 	case 1:
3066 		mac->max_frame_size = FRAME_SIZE_UPTO_4K;
3067 		break;
3068 	case 2:
3069 		mac->max_frame_size = FRAME_SIZE_UPTO_8K;
3070 		break;
3071 	case 3:
3072 		if (mac->type < e1000_82571)
3073 			mac->max_frame_size = FRAME_SIZE_UPTO_16K;
3074 		else
3075 			mac->max_frame_size = FRAME_SIZE_UPTO_9K;
3076 		break;
3077 	default:
3078 		mac->max_frame_size = ETHERMAX;
3079 		break;
3080 	}	/* switch */
3081 
3082 	/* ich8 does not do jumbo frames */
3083 	if (mac->type == e1000_ich8lan) {
3084 		mac->max_frame_size = ETHERMAX;
3085 	}
3086 
3087 	/* ich9 does not do jumbo frames on one phy type */
3088 	if ((mac->type == e1000_ich9lan) &&
3089 	    (phy->type == e1000_phy_ife)) {
3090 		mac->max_frame_size = ETHERMAX;
3091 	}
3092 }
3093 
3094 static void
3095 arm_watchdog_timer(struct e1000g *Adapter)
3096 {
3097 	Adapter->watchdog_tid =
3098 	    timeout(e1000g_local_timer,
3099 	    (void *)Adapter, 1 * drv_usectohz(1000000));
3100 }
3101 #pragma inline(arm_watchdog_timer)
3102 
3103 static void
3104 enable_watchdog_timer(struct e1000g *Adapter)
3105 {
3106 	mutex_enter(&Adapter->watchdog_lock);
3107 
3108 	if (!Adapter->watchdog_timer_enabled) {
3109 		Adapter->watchdog_timer_enabled = B_TRUE;
3110 		Adapter->watchdog_timer_started = B_TRUE;
3111 		arm_watchdog_timer(Adapter);
3112 	}
3113 
3114 	mutex_exit(&Adapter->watchdog_lock);
3115 }
3116 
3117 static void
3118 disable_watchdog_timer(struct e1000g *Adapter)
3119 {
3120 	timeout_id_t tid;
3121 
3122 	mutex_enter(&Adapter->watchdog_lock);
3123 
3124 	Adapter->watchdog_timer_enabled = B_FALSE;
3125 	Adapter->watchdog_timer_started = B_FALSE;
3126 	tid = Adapter->watchdog_tid;
3127 	Adapter->watchdog_tid = 0;
3128 
3129 	mutex_exit(&Adapter->watchdog_lock);
3130 
3131 	if (tid != 0)
3132 		(void) untimeout(tid);
3133 }
3134 
3135 static void
3136 start_watchdog_timer(struct e1000g *Adapter)
3137 {
3138 	mutex_enter(&Adapter->watchdog_lock);
3139 
3140 	if (Adapter->watchdog_timer_enabled) {
3141 		if (!Adapter->watchdog_timer_started) {
3142 			Adapter->watchdog_timer_started = B_TRUE;
3143 			arm_watchdog_timer(Adapter);
3144 		}
3145 	}
3146 
3147 	mutex_exit(&Adapter->watchdog_lock);
3148 }
3149 
3150 static void
3151 restart_watchdog_timer(struct e1000g *Adapter)
3152 {
3153 	mutex_enter(&Adapter->watchdog_lock);
3154 
3155 	if (Adapter->watchdog_timer_started)
3156 		arm_watchdog_timer(Adapter);
3157 
3158 	mutex_exit(&Adapter->watchdog_lock);
3159 }
3160 
3161 static void
3162 stop_watchdog_timer(struct e1000g *Adapter)
3163 {
3164 	timeout_id_t tid;
3165 
3166 	mutex_enter(&Adapter->watchdog_lock);
3167 
3168 	Adapter->watchdog_timer_started = B_FALSE;
3169 	tid = Adapter->watchdog_tid;
3170 	Adapter->watchdog_tid = 0;
3171 
3172 	mutex_exit(&Adapter->watchdog_lock);
3173 
3174 	if (tid != 0)
3175 		(void) untimeout(tid);
3176 }
3177 
3178 static void
3179 stop_link_timer(struct e1000g *Adapter)
3180 {
3181 	timeout_id_t tid;
3182 
3183 	/* Disable the link timer */
3184 	mutex_enter(&Adapter->link_lock);
3185 
3186 	tid = Adapter->link_tid;
3187 	Adapter->link_tid = 0;
3188 
3189 	mutex_exit(&Adapter->link_lock);
3190 
3191 	if (tid != 0)
3192 		(void) untimeout(tid);
3193 }
3194 
3195 static void
3196 stop_82547_timer(e1000g_tx_ring_t *tx_ring)
3197 {
3198 	timeout_id_t tid;
3199 
3200 	/* Disable the tx timer for 82547 chipset */
3201 	mutex_enter(&tx_ring->tx_lock);
3202 
3203 	tx_ring->timer_enable_82547 = B_FALSE;
3204 	tid = tx_ring->timer_id_82547;
3205 	tx_ring->timer_id_82547 = 0;
3206 
3207 	mutex_exit(&tx_ring->tx_lock);
3208 
3209 	if (tid != 0)
3210 		(void) untimeout(tid);
3211 }
3212 
3213 void
3214 e1000g_clear_interrupt(struct e1000g *Adapter)
3215 {
3216 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC,
3217 	    0xffffffff & ~E1000_IMS_RXSEQ);
3218 }
3219 
3220 void
3221 e1000g_mask_interrupt(struct e1000g *Adapter)
3222 {
3223 	E1000_WRITE_REG(&Adapter->shared, E1000_IMS,
3224 	    IMS_ENABLE_MASK & ~E1000_IMS_TXDW);
3225 
3226 	if (Adapter->tx_intr_enable)
3227 		e1000g_mask_tx_interrupt(Adapter);
3228 }
3229 
3230 void
3231 e1000g_clear_all_interrupts(struct e1000g *Adapter)
3232 {
3233 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC, 0xffffffff);
3234 }
3235 
3236 void
3237 e1000g_mask_tx_interrupt(struct e1000g *Adapter)
3238 {
3239 	E1000_WRITE_REG(&Adapter->shared, E1000_IMS, E1000_IMS_TXDW);
3240 }
3241 
3242 void
3243 e1000g_clear_tx_interrupt(struct e1000g *Adapter)
3244 {
3245 	E1000_WRITE_REG(&Adapter->shared, E1000_IMC, E1000_IMS_TXDW);
3246 }
3247 
3248 static void
3249 e1000g_smartspeed(struct e1000g *Adapter)
3250 {
3251 	struct e1000_hw *hw = &Adapter->shared;
3252 	uint16_t phy_status;
3253 	uint16_t phy_ctrl;
3254 
3255 	/*
3256 	 * If we're not T-or-T, or we're not autoneg'ing, or we're not
3257 	 * advertising 1000Full, we don't even use the workaround
3258 	 */
3259 	if ((hw->phy.type != e1000_phy_igp) ||
3260 	    !hw->mac.autoneg ||
3261 	    !(hw->phy.autoneg_advertised & ADVERTISE_1000_FULL))
3262 		return;
3263 
3264 	/*
3265 	 * True if this is the first call of this function or after every
3266 	 * 30 seconds of not having link
3267 	 */
3268 	if (Adapter->smartspeed == 0) {
3269 		/*
3270 		 * If Master/Slave config fault is asserted twice, we
3271 		 * assume back-to-back
3272 		 */
3273 		e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
3274 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
3275 			return;
3276 
3277 		e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
3278 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
3279 			return;
3280 		/*
3281 		 * We're assuming back-2-back because our status register
3282 		 * insists! there's a fault in the master/slave
3283 		 * relationship that was "negotiated"
3284 		 */
3285 		e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
3286 		/*
3287 		 * Is the phy configured for manual configuration of
3288 		 * master/slave?
3289 		 */
3290 		if (phy_ctrl & CR_1000T_MS_ENABLE) {
3291 			/*
3292 			 * Yes.  Then disable manual configuration (enable
3293 			 * auto configuration) of master/slave
3294 			 */
3295 			phy_ctrl &= ~CR_1000T_MS_ENABLE;
3296 			e1000_write_phy_reg(hw,
3297 			    PHY_1000T_CTRL, phy_ctrl);
3298 			/*
3299 			 * Effectively starting the clock
3300 			 */
3301 			Adapter->smartspeed++;
3302 			/*
3303 			 * Restart autonegotiation
3304 			 */
3305 			if (!e1000_phy_setup_autoneg(hw) &&
3306 			    !e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl)) {
3307 				phy_ctrl |= (MII_CR_AUTO_NEG_EN |
3308 				    MII_CR_RESTART_AUTO_NEG);
3309 				e1000_write_phy_reg(hw,
3310 				    PHY_CONTROL, phy_ctrl);
3311 			}
3312 		}
3313 		return;
3314 		/*
3315 		 * Has 6 seconds transpired still without link? Remember,
3316 		 * you should reset the smartspeed counter once you obtain
3317 		 * link
3318 		 */
3319 	} else if (Adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
3320 		/*
3321 		 * Yes.  Remember, we did at the start determine that
3322 		 * there's a master/slave configuration fault, so we're
3323 		 * still assuming there's someone on the other end, but we
3324 		 * just haven't yet been able to talk to it. We then
3325 		 * re-enable auto configuration of master/slave to see if
3326 		 * we're running 2/3 pair cables.
3327 		 */
3328 		/*
3329 		 * If still no link, perhaps using 2/3 pair cable
3330 		 */
3331 		e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
3332 		phy_ctrl |= CR_1000T_MS_ENABLE;
3333 		e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_ctrl);
3334 		/*
3335 		 * Restart autoneg with phy enabled for manual
3336 		 * configuration of master/slave
3337 		 */
3338 		if (!e1000_phy_setup_autoneg(hw) &&
3339 		    !e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl)) {
3340 			phy_ctrl |=
3341 			    (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
3342 			e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl);
3343 		}
3344 		/*
3345 		 * Hopefully, there are no more faults and we've obtained
3346 		 * link as a result.
3347 		 */
3348 	}
3349 	/*
3350 	 * Restart process after E1000_SMARTSPEED_MAX iterations (30
3351 	 * seconds)
3352 	 */
3353 	if (Adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
3354 		Adapter->smartspeed = 0;
3355 }
3356 
3357 static boolean_t
3358 is_valid_mac_addr(uint8_t *mac_addr)
3359 {
3360 	const uint8_t addr_test1[6] = { 0, 0, 0, 0, 0, 0 };
3361 	const uint8_t addr_test2[6] =
3362 	    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3363 
3364 	if (!(bcmp(addr_test1, mac_addr, ETHERADDRL)) ||
3365 	    !(bcmp(addr_test2, mac_addr, ETHERADDRL)))
3366 		return (B_FALSE);
3367 
3368 	return (B_TRUE);
3369 }
3370 
3371 /*
3372  * e1000g_stall_check - check for tx stall
3373  *
3374  * This function checks if the adapter is stalled (in transmit).
3375  *
3376  * It is called each time the watchdog timeout is invoked.
3377  * If the transmit descriptor reclaim continuously fails,
3378  * the watchdog value will increment by 1. If the watchdog
3379  * value exceeds the threshold, the adapter is assumed to
3380  * have stalled and need to be reset.
3381  */
3382 static boolean_t
3383 e1000g_stall_check(struct e1000g *Adapter)
3384 {
3385 	e1000g_tx_ring_t *tx_ring;
3386 
3387 	tx_ring = Adapter->tx_ring;
3388 
3389 	if (Adapter->link_state != LINK_STATE_UP)
3390 		return (B_FALSE);
3391 
3392 	if (tx_ring->recycle_fail > 0)
3393 		tx_ring->stall_watchdog++;
3394 	else
3395 		tx_ring->stall_watchdog = 0;
3396 
3397 	if (tx_ring->stall_watchdog < E1000G_STALL_WATCHDOG_COUNT)
3398 		return (B_FALSE);
3399 
3400 	tx_ring->stall_watchdog = 0;
3401 	tx_ring->recycle_fail = 0;
3402 
3403 	return (B_TRUE);
3404 }
3405 
3406 #ifdef E1000G_DEBUG
3407 static enum ioc_reply
3408 e1000g_pp_ioctl(struct e1000g *e1000gp, struct iocblk *iocp, mblk_t *mp)
3409 {
3410 	void (*ppfn)(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd);
3411 	e1000g_peekpoke_t *ppd;
3412 	uint64_t mem_va;
3413 	uint64_t maxoff;
3414 	boolean_t peek;
3415 
3416 	switch (iocp->ioc_cmd) {
3417 
3418 	case E1000G_IOC_REG_PEEK:
3419 		peek = B_TRUE;
3420 		break;
3421 
3422 	case E1000G_IOC_REG_POKE:
3423 		peek = B_FALSE;
3424 		break;
3425 
3426 	deault:
3427 		E1000G_DEBUGLOG_1(e1000gp, E1000G_INFO_LEVEL,
3428 		    "e1000g_diag_ioctl: invalid ioctl command 0x%X\n",
3429 		    iocp->ioc_cmd);
3430 		return (IOC_INVAL);
3431 	}
3432 
3433 	/*
3434 	 * Validate format of ioctl
3435 	 */
3436 	if (iocp->ioc_count != sizeof (e1000g_peekpoke_t))
3437 		return (IOC_INVAL);
3438 	if (mp->b_cont == NULL)
3439 		return (IOC_INVAL);
3440 
3441 	ppd = (e1000g_peekpoke_t *)mp->b_cont->b_rptr;
3442 
3443 	/*
3444 	 * Validate request parameters
3445 	 */
3446 	switch (ppd->pp_acc_space) {
3447 
3448 	default:
3449 		E1000G_DEBUGLOG_1(e1000gp, E1000G_INFO_LEVEL,
3450 		    "e1000g_diag_ioctl: invalid access space 0x%X\n",
3451 		    ppd->pp_acc_space);
3452 		return (IOC_INVAL);
3453 
3454 	case E1000G_PP_SPACE_REG:
3455 		/*
3456 		 * Memory-mapped I/O space
3457 		 */
3458 		ASSERT(ppd->pp_acc_size == 4);
3459 		if (ppd->pp_acc_size != 4)
3460 			return (IOC_INVAL);
3461 
3462 		if ((ppd->pp_acc_offset % ppd->pp_acc_size) != 0)
3463 			return (IOC_INVAL);
3464 
3465 		mem_va = 0;
3466 		maxoff = 0x10000;
3467 		ppfn = peek ? e1000g_ioc_peek_reg : e1000g_ioc_poke_reg;
3468 		break;
3469 
3470 	case E1000G_PP_SPACE_E1000G:
3471 		/*
3472 		 * E1000g data structure!
3473 		 */
3474 		mem_va = (uintptr_t)e1000gp;
3475 		maxoff = sizeof (struct e1000g);
3476 		ppfn = peek ? e1000g_ioc_peek_mem : e1000g_ioc_poke_mem;
3477 		break;
3478 
3479 	}
3480 
3481 	if (ppd->pp_acc_offset >= maxoff)
3482 		return (IOC_INVAL);
3483 
3484 	if (ppd->pp_acc_offset + ppd->pp_acc_size > maxoff)
3485 		return (IOC_INVAL);
3486 
3487 	/*
3488 	 * All OK - go!
3489 	 */
3490 	ppd->pp_acc_offset += mem_va;
3491 	(*ppfn)(e1000gp, ppd);
3492 	return (peek ? IOC_REPLY : IOC_ACK);
3493 }
3494 
3495 static void
3496 e1000g_ioc_peek_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
3497 {
3498 	ddi_acc_handle_t handle;
3499 	uint32_t *regaddr;
3500 
3501 	handle = e1000gp->osdep.reg_handle;
3502 	regaddr =
3503 	    (uint32_t *)(e1000gp->shared.hw_addr + ppd->pp_acc_offset);
3504 
3505 	ppd->pp_acc_data = ddi_get32(handle, regaddr);
3506 }
3507 
3508 static void
3509 e1000g_ioc_poke_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
3510 {
3511 	ddi_acc_handle_t handle;
3512 	uint32_t *regaddr;
3513 	uint32_t value;
3514 
3515 	handle = e1000gp->osdep.reg_handle;
3516 	regaddr =
3517 	    (uint32_t *)(e1000gp->shared.hw_addr + ppd->pp_acc_offset);
3518 	value = (uint32_t)ppd->pp_acc_data;
3519 
3520 	ddi_put32(handle, regaddr, value);
3521 }
3522 
3523 static void
3524 e1000g_ioc_peek_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
3525 {
3526 	uint64_t value;
3527 	void *vaddr;
3528 
3529 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
3530 
3531 	switch (ppd->pp_acc_size) {
3532 	case 1:
3533 		value = *(uint8_t *)vaddr;
3534 		break;
3535 
3536 	case 2:
3537 		value = *(uint16_t *)vaddr;
3538 		break;
3539 
3540 	case 4:
3541 		value = *(uint32_t *)vaddr;
3542 		break;
3543 
3544 	case 8:
3545 		value = *(uint64_t *)vaddr;
3546 		break;
3547 	}
3548 
3549 	E1000G_DEBUGLOG_4(e1000gp, E1000G_INFO_LEVEL,
3550 	    "e1000g_ioc_peek_mem($%p, $%p) peeked 0x%llx from $%p\n",
3551 	    (void *)e1000gp, (void *)ppd, value, vaddr);
3552 
3553 	ppd->pp_acc_data = value;
3554 }
3555 
3556 static void
3557 e1000g_ioc_poke_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
3558 {
3559 	uint64_t value;
3560 	void *vaddr;
3561 
3562 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
3563 	value = ppd->pp_acc_data;
3564 
3565 	E1000G_DEBUGLOG_4(e1000gp, E1000G_INFO_LEVEL,
3566 	    "e1000g_ioc_poke_mem($%p, $%p) poking 0x%llx at $%p\n",
3567 	    (void *)e1000gp, (void *)ppd, value, vaddr);
3568 
3569 	switch (ppd->pp_acc_size) {
3570 	case 1:
3571 		*(uint8_t *)vaddr = (uint8_t)value;
3572 		break;
3573 
3574 	case 2:
3575 		*(uint16_t *)vaddr = (uint16_t)value;
3576 		break;
3577 
3578 	case 4:
3579 		*(uint32_t *)vaddr = (uint32_t)value;
3580 		break;
3581 
3582 	case 8:
3583 		*(uint64_t *)vaddr = (uint64_t)value;
3584 		break;
3585 	}
3586 }
3587 #endif
3588 
3589 /*
3590  * Loopback Support
3591  */
3592 static lb_property_t lb_normal =
3593 	{ normal,	"normal",	E1000G_LB_NONE		};
3594 static lb_property_t lb_external1000 =
3595 	{ external,	"1000Mbps",	E1000G_LB_EXTERNAL_1000	};
3596 static lb_property_t lb_external100 =
3597 	{ external,	"100Mbps",	E1000G_LB_EXTERNAL_100	};
3598 static lb_property_t lb_external10 =
3599 	{ external,	"10Mbps",	E1000G_LB_EXTERNAL_10	};
3600 static lb_property_t lb_phy =
3601 	{ internal,	"PHY",		E1000G_LB_INTERNAL_PHY	};
3602 
3603 static enum ioc_reply
3604 e1000g_loopback_ioctl(struct e1000g *Adapter, struct iocblk *iocp, mblk_t *mp)
3605 {
3606 	lb_info_sz_t *lbsp;
3607 	lb_property_t *lbpp;
3608 	struct e1000_hw *hw;
3609 	uint32_t *lbmp;
3610 	uint32_t size;
3611 	uint32_t value;
3612 
3613 	hw = &Adapter->shared;
3614 
3615 	if (mp->b_cont == NULL)
3616 		return (IOC_INVAL);
3617 
3618 	switch (iocp->ioc_cmd) {
3619 	default:
3620 		return (IOC_INVAL);
3621 
3622 	case LB_GET_INFO_SIZE:
3623 		size = sizeof (lb_info_sz_t);
3624 		if (iocp->ioc_count != size)
3625 			return (IOC_INVAL);
3626 
3627 		rw_enter(&Adapter->chip_lock, RW_WRITER);
3628 		e1000g_get_phy_state(Adapter);
3629 
3630 		/*
3631 		 * Workaround for hardware faults. In order to get a stable
3632 		 * state of phy, we will wait for a specific interval and
3633 		 * try again. The time delay is an experiential value based
3634 		 * on our testing.
3635 		 */
3636 		msec_delay(100);
3637 		e1000g_get_phy_state(Adapter);
3638 		rw_exit(&Adapter->chip_lock);
3639 
3640 		value = sizeof (lb_normal);
3641 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
3642 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
3643 		    (hw->media_type == e1000_media_type_fiber) ||
3644 		    (hw->media_type == e1000_media_type_internal_serdes)) {
3645 			value += sizeof (lb_phy);
3646 			switch (hw->mac.type) {
3647 			case e1000_82571:
3648 			case e1000_82572:
3649 				value += sizeof (lb_external1000);
3650 				break;
3651 			}
3652 		}
3653 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
3654 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
3655 			value += sizeof (lb_external100);
3656 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
3657 			value += sizeof (lb_external10);
3658 
3659 		lbsp = (lb_info_sz_t *)mp->b_cont->b_rptr;
3660 		*lbsp = value;
3661 		break;
3662 
3663 	case LB_GET_INFO:
3664 		value = sizeof (lb_normal);
3665 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
3666 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
3667 		    (hw->media_type == e1000_media_type_fiber) ||
3668 		    (hw->media_type == e1000_media_type_internal_serdes)) {
3669 			value += sizeof (lb_phy);
3670 			switch (hw->mac.type) {
3671 			case e1000_82571:
3672 			case e1000_82572:
3673 				value += sizeof (lb_external1000);
3674 				break;
3675 			}
3676 		}
3677 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
3678 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
3679 			value += sizeof (lb_external100);
3680 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
3681 			value += sizeof (lb_external10);
3682 
3683 		size = value;
3684 		if (iocp->ioc_count != size)
3685 			return (IOC_INVAL);
3686 
3687 		value = 0;
3688 		lbpp = (lb_property_t *)mp->b_cont->b_rptr;
3689 		lbpp[value++] = lb_normal;
3690 		if ((Adapter->phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
3691 		    (Adapter->phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
3692 		    (hw->media_type == e1000_media_type_fiber) ||
3693 		    (hw->media_type == e1000_media_type_internal_serdes)) {
3694 			lbpp[value++] = lb_phy;
3695 			switch (hw->mac.type) {
3696 			case e1000_82571:
3697 			case e1000_82572:
3698 				lbpp[value++] = lb_external1000;
3699 				break;
3700 			}
3701 		}
3702 		if ((Adapter->phy_status & MII_SR_100X_FD_CAPS) ||
3703 		    (Adapter->phy_status & MII_SR_100T2_FD_CAPS))
3704 			lbpp[value++] = lb_external100;
3705 		if (Adapter->phy_status & MII_SR_10T_FD_CAPS)
3706 			lbpp[value++] = lb_external10;
3707 		break;
3708 
3709 	case LB_GET_MODE:
3710 		size = sizeof (uint32_t);
3711 		if (iocp->ioc_count != size)
3712 			return (IOC_INVAL);
3713 
3714 		lbmp = (uint32_t *)mp->b_cont->b_rptr;
3715 		*lbmp = Adapter->loopback_mode;
3716 		break;
3717 
3718 	case LB_SET_MODE:
3719 		size = 0;
3720 		if (iocp->ioc_count != sizeof (uint32_t))
3721 			return (IOC_INVAL);
3722 
3723 		lbmp = (uint32_t *)mp->b_cont->b_rptr;
3724 		if (!e1000g_set_loopback_mode(Adapter, *lbmp))
3725 			return (IOC_INVAL);
3726 		break;
3727 	}
3728 
3729 	iocp->ioc_count = size;
3730 	iocp->ioc_error = 0;
3731 
3732 	if (e1000g_check_acc_handle(Adapter->osdep.reg_handle) != DDI_FM_OK) {
3733 		ddi_fm_service_impact(Adapter->dip, DDI_SERVICE_DEGRADED);
3734 		return (IOC_INVAL);
3735 	}
3736 
3737 	return (IOC_REPLY);
3738 }
3739 
3740 static boolean_t
3741 e1000g_set_loopback_mode(struct e1000g *Adapter, uint32_t mode)
3742 {
3743 	struct e1000_hw *hw;
3744 	int i, times;
3745 	boolean_t link_up;
3746 
3747 	if (mode == Adapter->loopback_mode)
3748 		return (B_TRUE);
3749 
3750 	hw = &Adapter->shared;
3751 	times = 0;
3752 
3753 	Adapter->loopback_mode = mode;
3754 
3755 	if (mode == E1000G_LB_NONE) {
3756 		/* Reset the chip */
3757 		hw->phy.wait_for_link = B_TRUE;
3758 		(void) e1000g_reset(Adapter);
3759 		hw->phy.wait_for_link = B_FALSE;
3760 		return (B_TRUE);
3761 	}
3762 
3763 again:
3764 
3765 	rw_enter(&Adapter->chip_lock, RW_WRITER);
3766 
3767 	switch (mode) {
3768 	default:
3769 		rw_exit(&Adapter->chip_lock);
3770 		return (B_FALSE);
3771 
3772 	case E1000G_LB_EXTERNAL_1000:
3773 		e1000g_set_external_loopback_1000(Adapter);
3774 		break;
3775 
3776 	case E1000G_LB_EXTERNAL_100:
3777 		e1000g_set_external_loopback_100(Adapter);
3778 		break;
3779 
3780 	case E1000G_LB_EXTERNAL_10:
3781 		e1000g_set_external_loopback_10(Adapter);
3782 		break;
3783 
3784 	case E1000G_LB_INTERNAL_PHY:
3785 		e1000g_set_internal_loopback(Adapter);
3786 		break;
3787 	}
3788 
3789 	times++;
3790 
3791 	rw_exit(&Adapter->chip_lock);
3792 
3793 	/* Wait for link up */
3794 	for (i = (PHY_FORCE_LIMIT * 2); i > 0; i--)
3795 		msec_delay(100);
3796 
3797 	rw_enter(&Adapter->chip_lock, RW_WRITER);
3798 
3799 	link_up = e1000g_link_up(Adapter);
3800 
3801 	rw_exit(&Adapter->chip_lock);
3802 
3803 	if (!link_up) {
3804 		E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
3805 		    "Failed to get the link up");
3806 		if (times < 2) {
3807 			/* Reset the link */
3808 			E1000G_DEBUGLOG_0(Adapter, E1000G_INFO_LEVEL,
3809 			    "Reset the link ...");
3810 			(void) e1000g_reset(Adapter);
3811 			goto again;
3812 		}
3813 	}
3814 
3815 	return (B_TRUE);
3816 }
3817 
3818 /*
3819  * The following loopback settings are from Intel's technical
3820  * document - "How To Loopback". All the register settings and
3821  * time delay values are directly inherited from the document
3822  * without more explanations available.
3823  */
3824 static void
3825 e1000g_set_internal_loopback(struct e1000g *Adapter)
3826 {
3827 	struct e1000_hw *hw;
3828 	uint32_t ctrl;
3829 	uint32_t status;
3830 	uint16_t phy_ctrl;
3831 	uint32_t txcw;
3832 
3833 	hw = &Adapter->shared;
3834 
3835 	/* Disable Smart Power Down */
3836 	phy_spd_state(hw, B_FALSE);
3837 
3838 	e1000_read_phy_reg(hw, PHY_CONTROL, &phy_ctrl);
3839 	phy_ctrl &= ~(MII_CR_AUTO_NEG_EN | MII_CR_SPEED_100 | MII_CR_SPEED_10);
3840 	phy_ctrl |= MII_CR_FULL_DUPLEX | MII_CR_SPEED_1000;
3841 
3842 	switch (hw->mac.type) {
3843 	case e1000_82540:
3844 	case e1000_82545:
3845 	case e1000_82545_rev_3:
3846 	case e1000_82546:
3847 	case e1000_82546_rev_3:
3848 	case e1000_82573:
3849 		/* Auto-MDI/MDIX off */
3850 		e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, 0x0808);
3851 		/* Reset PHY to update Auto-MDI/MDIX */
3852 		e1000_write_phy_reg(hw, PHY_CONTROL,
3853 		    phy_ctrl | MII_CR_RESET | MII_CR_AUTO_NEG_EN);
3854 		/* Reset PHY to auto-neg off and force 1000 */
3855 		e1000_write_phy_reg(hw, PHY_CONTROL,
3856 		    phy_ctrl | MII_CR_RESET);
3857 		/*
3858 		 * Disable PHY receiver for 82540/545/546 and 82573 Family.
3859 		 * See comments above e1000g_set_internal_loopback() for the
3860 		 * background.
3861 		 */
3862 		e1000_write_phy_reg(hw, 29, 0x001F);
3863 		e1000_write_phy_reg(hw, 30, 0x8FFC);
3864 		e1000_write_phy_reg(hw, 29, 0x001A);
3865 		e1000_write_phy_reg(hw, 30, 0x8FF0);
3866 		break;
3867 	}
3868 
3869 	/* Set loopback */
3870 	e1000_write_phy_reg(hw, PHY_CONTROL, phy_ctrl | MII_CR_LOOPBACK);
3871 
3872 	msec_delay(250);
3873 
3874 	/* Now set up the MAC to the same speed/duplex as the PHY. */
3875 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
3876 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
3877 	ctrl |= (E1000_CTRL_FRCSPD |	/* Set the Force Speed Bit */
3878 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
3879 	    E1000_CTRL_SPD_1000 |	/* Force Speed to 1000 */
3880 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
3881 
3882 	switch (hw->mac.type) {
3883 	case e1000_82540:
3884 	case e1000_82545:
3885 	case e1000_82545_rev_3:
3886 	case e1000_82546:
3887 	case e1000_82546_rev_3:
3888 		/*
3889 		 * For some serdes we'll need to commit the writes now
3890 		 * so that the status is updated on link
3891 		 */
3892 		if (hw->media_type == e1000_media_type_internal_serdes) {
3893 			E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
3894 			msec_delay(100);
3895 			ctrl = E1000_READ_REG(hw, E1000_CTRL);
3896 		}
3897 
3898 		if (hw->media_type == e1000_media_type_copper) {
3899 			/* Invert Loss of Signal */
3900 			ctrl |= E1000_CTRL_ILOS;
3901 		} else {
3902 			/* Set ILOS on fiber nic if half duplex is detected */
3903 			status = E1000_READ_REG(hw, E1000_STATUS);
3904 			if ((status & E1000_STATUS_FD) == 0)
3905 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
3906 		}
3907 		break;
3908 
3909 	case e1000_82571:
3910 	case e1000_82572:
3911 		/*
3912 		 * The fiber/SerDes versions of this adapter do not contain an
3913 		 * accessible PHY. Therefore, loopback beyond MAC must be done
3914 		 * using SerDes analog loopback.
3915 		 */
3916 		if (hw->media_type != e1000_media_type_copper) {
3917 			status = E1000_READ_REG(hw, E1000_STATUS);
3918 			/* Set ILOS on fiber nic if half duplex is detected */
3919 			if (((status & E1000_STATUS_LU) == 0) ||
3920 			    ((status & E1000_STATUS_FD) == 0) ||
3921 			    (hw->media_type ==
3922 			    e1000_media_type_internal_serdes))
3923 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
3924 
3925 			/* Disable autoneg by setting bit 31 of TXCW to zero */
3926 			txcw = E1000_READ_REG(hw, E1000_TXCW);
3927 			txcw &= ~((uint32_t)1 << 31);
3928 			E1000_WRITE_REG(hw, E1000_TXCW, txcw);
3929 
3930 			/*
3931 			 * Write 0x410 to Serdes Control register
3932 			 * to enable Serdes analog loopback
3933 			 */
3934 			E1000_WRITE_REG(hw, E1000_SCTL, 0x0410);
3935 			msec_delay(10);
3936 		}
3937 		break;
3938 
3939 	case e1000_82573:
3940 		ctrl |= E1000_CTRL_ILOS;
3941 		break;
3942 	}
3943 
3944 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
3945 
3946 }
3947 
3948 static void
3949 e1000g_set_external_loopback_1000(struct e1000g *Adapter)
3950 {
3951 	struct e1000_hw *hw;
3952 	uint32_t rctl;
3953 	uint32_t ctrl_ext;
3954 	uint32_t ctrl;
3955 	uint32_t status;
3956 	uint32_t txcw;
3957 
3958 	hw = &Adapter->shared;
3959 
3960 	/* Disable Smart Power Down */
3961 	phy_spd_state(hw, B_FALSE);
3962 
3963 	switch (hw->media_type) {
3964 	case e1000_media_type_copper:
3965 		/* Force link up (Must be done before the PHY writes) */
3966 		ctrl = E1000_READ_REG(hw, E1000_CTRL);
3967 		ctrl |= E1000_CTRL_SLU;	/* Force Link Up */
3968 		E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
3969 
3970 		rctl = E1000_READ_REG(hw, E1000_RCTL);
3971 		rctl |= (E1000_RCTL_EN |
3972 		    E1000_RCTL_SBP |
3973 		    E1000_RCTL_UPE |
3974 		    E1000_RCTL_MPE |
3975 		    E1000_RCTL_LPE |
3976 		    E1000_RCTL_BAM);		/* 0x803E */
3977 		E1000_WRITE_REG(hw, E1000_RCTL, rctl);
3978 
3979 		ctrl_ext = E1000_READ_REG(hw, E1000_CTRL_EXT);
3980 		ctrl_ext |= (E1000_CTRL_EXT_SDP4_DATA |
3981 		    E1000_CTRL_EXT_SDP6_DATA |
3982 		    E1000_CTRL_EXT_SDP7_DATA |
3983 		    E1000_CTRL_EXT_SDP4_DIR |
3984 		    E1000_CTRL_EXT_SDP6_DIR |
3985 		    E1000_CTRL_EXT_SDP7_DIR);	/* 0x0DD0 */
3986 		E1000_WRITE_REG(hw, E1000_CTRL_EXT, ctrl_ext);
3987 
3988 		/*
3989 		 * This sequence tunes the PHY's SDP and no customer
3990 		 * settable values. For background, see comments above
3991 		 * e1000g_set_internal_loopback().
3992 		 */
3993 		e1000_write_phy_reg(hw, 0x0, 0x140);
3994 		msec_delay(10);
3995 		e1000_write_phy_reg(hw, 0x9, 0x1A00);
3996 		e1000_write_phy_reg(hw, 0x12, 0xC10);
3997 		e1000_write_phy_reg(hw, 0x12, 0x1C10);
3998 		e1000_write_phy_reg(hw, 0x1F37, 0x76);
3999 		e1000_write_phy_reg(hw, 0x1F33, 0x1);
4000 		e1000_write_phy_reg(hw, 0x1F33, 0x0);
4001 
4002 		e1000_write_phy_reg(hw, 0x1F35, 0x65);
4003 		e1000_write_phy_reg(hw, 0x1837, 0x3F7C);
4004 		e1000_write_phy_reg(hw, 0x1437, 0x3FDC);
4005 		e1000_write_phy_reg(hw, 0x1237, 0x3F7C);
4006 		e1000_write_phy_reg(hw, 0x1137, 0x3FDC);
4007 
4008 		msec_delay(50);
4009 		break;
4010 	case e1000_media_type_fiber:
4011 	case e1000_media_type_internal_serdes:
4012 		status = E1000_READ_REG(hw, E1000_STATUS);
4013 		if (((status & E1000_STATUS_LU) == 0) ||
4014 		    (hw->media_type == e1000_media_type_internal_serdes)) {
4015 			ctrl = E1000_READ_REG(hw, E1000_CTRL);
4016 			ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
4017 			E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
4018 		}
4019 
4020 		/* Disable autoneg by setting bit 31 of TXCW to zero */
4021 		txcw = E1000_READ_REG(hw, E1000_TXCW);
4022 		txcw &= ~((uint32_t)1 << 31);
4023 		E1000_WRITE_REG(hw, E1000_TXCW, txcw);
4024 
4025 		/*
4026 		 * Write 0x410 to Serdes Control register
4027 		 * to enable Serdes analog loopback
4028 		 */
4029 		E1000_WRITE_REG(hw, E1000_SCTL, 0x0410);
4030 		msec_delay(10);
4031 		break;
4032 	default:
4033 		break;
4034 	}
4035 }
4036 
4037 static void
4038 e1000g_set_external_loopback_100(struct e1000g *Adapter)
4039 {
4040 	struct e1000_hw *hw;
4041 	uint32_t ctrl;
4042 	uint16_t phy_ctrl;
4043 
4044 	hw = &Adapter->shared;
4045 
4046 	/* Disable Smart Power Down */
4047 	phy_spd_state(hw, B_FALSE);
4048 
4049 	phy_ctrl = (MII_CR_FULL_DUPLEX |
4050 	    MII_CR_SPEED_100);
4051 
4052 	/* Force 100/FD, reset PHY */
4053 	e1000_write_phy_reg(hw, PHY_CONTROL,
4054 	    phy_ctrl | MII_CR_RESET);	/* 0xA100 */
4055 	msec_delay(10);
4056 
4057 	/* Force 100/FD */
4058 	e1000_write_phy_reg(hw, PHY_CONTROL,
4059 	    phy_ctrl);			/* 0x2100 */
4060 	msec_delay(10);
4061 
4062 	/* Now setup the MAC to the same speed/duplex as the PHY. */
4063 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
4064 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
4065 	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
4066 	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
4067 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
4068 	    E1000_CTRL_SPD_100 |	/* Force Speed to 100 */
4069 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
4070 
4071 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
4072 }
4073 
4074 static void
4075 e1000g_set_external_loopback_10(struct e1000g *Adapter)
4076 {
4077 	struct e1000_hw *hw;
4078 	uint32_t ctrl;
4079 	uint16_t phy_ctrl;
4080 
4081 	hw = &Adapter->shared;
4082 
4083 	/* Disable Smart Power Down */
4084 	phy_spd_state(hw, B_FALSE);
4085 
4086 	phy_ctrl = (MII_CR_FULL_DUPLEX |
4087 	    MII_CR_SPEED_10);
4088 
4089 	/* Force 10/FD, reset PHY */
4090 	e1000_write_phy_reg(hw, PHY_CONTROL,
4091 	    phy_ctrl | MII_CR_RESET);	/* 0x8100 */
4092 	msec_delay(10);
4093 
4094 	/* Force 10/FD */
4095 	e1000_write_phy_reg(hw, PHY_CONTROL,
4096 	    phy_ctrl);			/* 0x0100 */
4097 	msec_delay(10);
4098 
4099 	/* Now setup the MAC to the same speed/duplex as the PHY. */
4100 	ctrl = E1000_READ_REG(hw, E1000_CTRL);
4101 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
4102 	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
4103 	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
4104 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
4105 	    E1000_CTRL_SPD_10 |		/* Force Speed to 10 */
4106 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
4107 
4108 	E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
4109 }
4110 
4111 #ifdef __sparc
4112 static boolean_t
4113 e1000g_find_mac_address(struct e1000g *Adapter)
4114 {
4115 	struct e1000_hw *hw = &Adapter->shared;
4116 	uchar_t *bytes;
4117 	struct ether_addr sysaddr;
4118 	uint_t nelts;
4119 	int err;
4120 	boolean_t found = B_FALSE;
4121 
4122 	/*
4123 	 * The "vendor's factory-set address" may already have
4124 	 * been extracted from the chip, but if the property
4125 	 * "local-mac-address" is set we use that instead.
4126 	 *
4127 	 * We check whether it looks like an array of 6
4128 	 * bytes (which it should, if OBP set it).  If we can't
4129 	 * make sense of it this way, we'll ignore it.
4130 	 */
4131 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
4132 	    DDI_PROP_DONTPASS, "local-mac-address", &bytes, &nelts);
4133 	if (err == DDI_PROP_SUCCESS) {
4134 		if (nelts == ETHERADDRL) {
4135 			while (nelts--)
4136 				hw->mac.addr[nelts] = bytes[nelts];
4137 			found = B_TRUE;
4138 		}
4139 		ddi_prop_free(bytes);
4140 	}
4141 
4142 	/*
4143 	 * Look up the OBP property "local-mac-address?". If the user has set
4144 	 * 'local-mac-address? = false', use "the system address" instead.
4145 	 */
4146 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip, 0,
4147 	    "local-mac-address?", &bytes, &nelts) == DDI_PROP_SUCCESS) {
4148 		if (strncmp("false", (caddr_t)bytes, (size_t)nelts) == 0) {
4149 			if (localetheraddr(NULL, &sysaddr) != 0) {
4150 				bcopy(&sysaddr, hw->mac.addr, ETHERADDRL);
4151 				found = B_TRUE;
4152 			}
4153 		}
4154 		ddi_prop_free(bytes);
4155 	}
4156 
4157 	/*
4158 	 * Finally(!), if there's a valid "mac-address" property (created
4159 	 * if we netbooted from this interface), we must use this instead
4160 	 * of any of the above to ensure that the NFS/install server doesn't
4161 	 * get confused by the address changing as Solaris takes over!
4162 	 */
4163 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
4164 	    DDI_PROP_DONTPASS, "mac-address", &bytes, &nelts);
4165 	if (err == DDI_PROP_SUCCESS) {
4166 		if (nelts == ETHERADDRL) {
4167 			while (nelts--)
4168 				hw->mac.addr[nelts] = bytes[nelts];
4169 			found = B_TRUE;
4170 		}
4171 		ddi_prop_free(bytes);
4172 	}
4173 
4174 	if (found) {
4175 		bcopy(hw->mac.addr, hw->mac.perm_addr,
4176 		    ETHERADDRL);
4177 	}
4178 
4179 	return (found);
4180 }
4181 #endif
4182 
4183 static int
4184 e1000g_add_intrs(struct e1000g *Adapter)
4185 {
4186 	dev_info_t *devinfo;
4187 	int intr_types;
4188 	int rc;
4189 
4190 	devinfo = Adapter->dip;
4191 
4192 	/* Get supported interrupt types */
4193 	rc = ddi_intr_get_supported_types(devinfo, &intr_types);
4194 
4195 	if (rc != DDI_SUCCESS) {
4196 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
4197 		    "Get supported interrupt types failed: %d\n", rc);
4198 		return (DDI_FAILURE);
4199 	}
4200 
4201 	/*
4202 	 * Based on Intel Technical Advisory document (TA-160), there are some
4203 	 * cases where some older Intel PCI-X NICs may "advertise" to the OS
4204 	 * that it supports MSI, but in fact has problems.
4205 	 * So we should only enable MSI for PCI-E NICs and disable MSI for old
4206 	 * PCI/PCI-X NICs.
4207 	 */
4208 	if (Adapter->shared.mac.type < e1000_82571)
4209 		Adapter->msi_enabled = B_FALSE;
4210 
4211 	if ((intr_types & DDI_INTR_TYPE_MSI) && Adapter->msi_enabled) {
4212 		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_MSI);
4213 
4214 		if (rc != DDI_SUCCESS) {
4215 			E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
4216 			    "Add MSI failed, trying Legacy interrupts\n");
4217 		} else {
4218 			Adapter->intr_type = DDI_INTR_TYPE_MSI;
4219 		}
4220 	}
4221 
4222 	if ((Adapter->intr_type == 0) &&
4223 	    (intr_types & DDI_INTR_TYPE_FIXED)) {
4224 		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_FIXED);
4225 
4226 		if (rc != DDI_SUCCESS) {
4227 			E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
4228 			    "Add Legacy interrupts failed\n");
4229 			return (DDI_FAILURE);
4230 		}
4231 
4232 		Adapter->intr_type = DDI_INTR_TYPE_FIXED;
4233 	}
4234 
4235 	if (Adapter->intr_type == 0) {
4236 		E1000G_DEBUGLOG_0(Adapter, E1000G_WARN_LEVEL,
4237 		    "No interrupts registered\n");
4238 		return (DDI_FAILURE);
4239 	}
4240 
4241 	return (DDI_SUCCESS);
4242 }
4243 
4244 /*
4245  * e1000g_intr_add() handles MSI/Legacy interrupts
4246  */
4247 static int
4248 e1000g_intr_add(struct e1000g *Adapter, int intr_type)
4249 {
4250 	dev_info_t *devinfo;
4251 	int count, avail, actual;
4252 	int x, y, rc, inum = 0;
4253 	int flag;
4254 	ddi_intr_handler_t *intr_handler;
4255 
4256 	devinfo = Adapter->dip;
4257 
4258 	/* get number of interrupts */
4259 	rc = ddi_intr_get_nintrs(devinfo, intr_type, &count);
4260 	if ((rc != DDI_SUCCESS) || (count == 0)) {
4261 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
4262 		    "Get interrupt number failed. Return: %d, count: %d\n",
4263 		    rc, count);
4264 		return (DDI_FAILURE);
4265 	}
4266 
4267 	/* get number of available interrupts */
4268 	rc = ddi_intr_get_navail(devinfo, intr_type, &avail);
4269 	if ((rc != DDI_SUCCESS) || (avail == 0)) {
4270 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
4271 		    "Get interrupt available number failed. "
4272 		    "Return: %d, available: %d\n", rc, avail);
4273 		return (DDI_FAILURE);
4274 	}
4275 
4276 	if (avail < count) {
4277 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
4278 		    "Interrupts count: %d, available: %d\n",
4279 		    count, avail);
4280 	}
4281 
4282 	/* Allocate an array of interrupt handles */
4283 	Adapter->intr_size = count * sizeof (ddi_intr_handle_t);
4284 	Adapter->htable = kmem_alloc(Adapter->intr_size, KM_SLEEP);
4285 
4286 	/* Set NORMAL behavior for both MSI and FIXED interrupt */
4287 	flag = DDI_INTR_ALLOC_NORMAL;
4288 
4289 	/* call ddi_intr_alloc() */
4290 	rc = ddi_intr_alloc(devinfo, Adapter->htable, intr_type, inum,
4291 	    count, &actual, flag);
4292 
4293 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
4294 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
4295 		    "Allocate interrupts failed: %d\n", rc);
4296 
4297 		kmem_free(Adapter->htable, Adapter->intr_size);
4298 		return (DDI_FAILURE);
4299 	}
4300 
4301 	if (actual < count) {
4302 		E1000G_DEBUGLOG_2(Adapter, E1000G_WARN_LEVEL,
4303 		    "Interrupts requested: %d, received: %d\n",
4304 		    count, actual);
4305 	}
4306 
4307 	Adapter->intr_cnt = actual;
4308 
4309 	/* Get priority for first msi, assume remaining are all the same */
4310 	rc = ddi_intr_get_pri(Adapter->htable[0], &Adapter->intr_pri);
4311 
4312 	if (rc != DDI_SUCCESS) {
4313 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
4314 		    "Get interrupt priority failed: %d\n", rc);
4315 
4316 		/* Free already allocated intr */
4317 		for (y = 0; y < actual; y++)
4318 			(void) ddi_intr_free(Adapter->htable[y]);
4319 
4320 		kmem_free(Adapter->htable, Adapter->intr_size);
4321 		return (DDI_FAILURE);
4322 	}
4323 
4324 	/*
4325 	 * In Legacy Interrupt mode, for PCI-Express adapters, we should
4326 	 * use the interrupt service routine e1000g_intr_pciexpress()
4327 	 * to avoid interrupt stealing when sharing interrupt with other
4328 	 * devices.
4329 	 */
4330 	if (Adapter->shared.mac.type < e1000_82571)
4331 		intr_handler = (ddi_intr_handler_t *)e1000g_intr;
4332 	else
4333 		intr_handler = (ddi_intr_handler_t *)e1000g_intr_pciexpress;
4334 
4335 	/* Call ddi_intr_add_handler() */
4336 	for (x = 0; x < actual; x++) {
4337 		rc = ddi_intr_add_handler(Adapter->htable[x],
4338 		    intr_handler, (caddr_t)Adapter, NULL);
4339 
4340 		if (rc != DDI_SUCCESS) {
4341 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
4342 			    "Add interrupt handler failed: %d\n", rc);
4343 
4344 			/* Remove already added handler */
4345 			for (y = 0; y < x; y++)
4346 				(void) ddi_intr_remove_handler(
4347 				    Adapter->htable[y]);
4348 
4349 			/* Free already allocated intr */
4350 			for (y = 0; y < actual; y++)
4351 				(void) ddi_intr_free(Adapter->htable[y]);
4352 
4353 			kmem_free(Adapter->htable, Adapter->intr_size);
4354 			return (DDI_FAILURE);
4355 		}
4356 	}
4357 
4358 	rc = ddi_intr_get_cap(Adapter->htable[0], &Adapter->intr_cap);
4359 
4360 	if (rc != DDI_SUCCESS) {
4361 		E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
4362 		    "Get interrupt cap failed: %d\n", rc);
4363 
4364 		/* Free already allocated intr */
4365 		for (y = 0; y < actual; y++) {
4366 			(void) ddi_intr_remove_handler(Adapter->htable[y]);
4367 			(void) ddi_intr_free(Adapter->htable[y]);
4368 		}
4369 
4370 		kmem_free(Adapter->htable, Adapter->intr_size);
4371 		return (DDI_FAILURE);
4372 	}
4373 
4374 	return (DDI_SUCCESS);
4375 }
4376 
4377 static int
4378 e1000g_rem_intrs(struct e1000g *Adapter)
4379 {
4380 	int x;
4381 	int rc;
4382 
4383 	for (x = 0; x < Adapter->intr_cnt; x++) {
4384 		rc = ddi_intr_remove_handler(Adapter->htable[x]);
4385 		if (rc != DDI_SUCCESS) {
4386 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
4387 			    "Remove intr handler failed: %d\n", rc);
4388 			return (DDI_FAILURE);
4389 		}
4390 
4391 		rc = ddi_intr_free(Adapter->htable[x]);
4392 		if (rc != DDI_SUCCESS) {
4393 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
4394 			    "Free intr failed: %d\n", rc);
4395 			return (DDI_FAILURE);
4396 		}
4397 	}
4398 
4399 	kmem_free(Adapter->htable, Adapter->intr_size);
4400 
4401 	return (DDI_SUCCESS);
4402 }
4403 
4404 static int
4405 e1000g_enable_intrs(struct e1000g *Adapter)
4406 {
4407 	int x;
4408 	int rc;
4409 
4410 	/* Enable interrupts */
4411 	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
4412 		/* Call ddi_intr_block_enable() for MSI */
4413 		rc = ddi_intr_block_enable(Adapter->htable,
4414 		    Adapter->intr_cnt);
4415 		if (rc != DDI_SUCCESS) {
4416 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
4417 			    "Enable block intr failed: %d\n", rc);
4418 			return (DDI_FAILURE);
4419 		}
4420 	} else {
4421 		/* Call ddi_intr_enable() for Legacy/MSI non block enable */
4422 		for (x = 0; x < Adapter->intr_cnt; x++) {
4423 			rc = ddi_intr_enable(Adapter->htable[x]);
4424 			if (rc != DDI_SUCCESS) {
4425 				E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
4426 				    "Enable intr failed: %d\n", rc);
4427 				return (DDI_FAILURE);
4428 			}
4429 		}
4430 	}
4431 
4432 	return (DDI_SUCCESS);
4433 }
4434 
4435 static int
4436 e1000g_disable_intrs(struct e1000g *Adapter)
4437 {
4438 	int x;
4439 	int rc;
4440 
4441 	/* Disable all interrupts */
4442 	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
4443 		rc = ddi_intr_block_disable(Adapter->htable,
4444 		    Adapter->intr_cnt);
4445 		if (rc != DDI_SUCCESS) {
4446 			E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
4447 			    "Disable block intr failed: %d\n", rc);
4448 			return (DDI_FAILURE);
4449 		}
4450 	} else {
4451 		for (x = 0; x < Adapter->intr_cnt; x++) {
4452 			rc = ddi_intr_disable(Adapter->htable[x]);
4453 			if (rc != DDI_SUCCESS) {
4454 				E1000G_DEBUGLOG_1(Adapter, E1000G_WARN_LEVEL,
4455 				    "Disable intr failed: %d\n", rc);
4456 				return (DDI_FAILURE);
4457 			}
4458 		}
4459 	}
4460 
4461 	return (DDI_SUCCESS);
4462 }
4463 
4464 /*
4465  * e1000g_get_phy_state - get the state of PHY registers, save in the adapter
4466  */
4467 static void
4468 e1000g_get_phy_state(struct e1000g *Adapter)
4469 {
4470 	struct e1000_hw *hw = &Adapter->shared;
4471 
4472 	e1000_read_phy_reg(hw, PHY_CONTROL, &Adapter->phy_ctrl);
4473 	e1000_read_phy_reg(hw, PHY_STATUS, &Adapter->phy_status);
4474 	e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &Adapter->phy_an_adv);
4475 	e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &Adapter->phy_an_exp);
4476 	e1000_read_phy_reg(hw, PHY_EXT_STATUS, &Adapter->phy_ext_status);
4477 	e1000_read_phy_reg(hw, PHY_1000T_CTRL, &Adapter->phy_1000t_ctrl);
4478 	e1000_read_phy_reg(hw, PHY_1000T_STATUS, &Adapter->phy_1000t_status);
4479 	e1000_read_phy_reg(hw, PHY_LP_ABILITY, &Adapter->phy_lp_able);
4480 }
4481 
4482 /*
4483  * FMA support
4484  */
4485 
4486 int
4487 e1000g_check_acc_handle(ddi_acc_handle_t handle)
4488 {
4489 	ddi_fm_error_t de;
4490 
4491 	ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
4492 	ddi_fm_acc_err_clear(handle, DDI_FME_VERSION);
4493 	return (de.fme_status);
4494 }
4495 
4496 int
4497 e1000g_check_dma_handle(ddi_dma_handle_t handle)
4498 {
4499 	ddi_fm_error_t de;
4500 
4501 	ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
4502 	return (de.fme_status);
4503 }
4504 
4505 /*
4506  * The IO fault service error handling callback function
4507  */
4508 static int
4509 e1000g_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
4510 {
4511 	/*
4512 	 * as the driver can always deal with an error in any dma or
4513 	 * access handle, we can just return the fme_status value.
4514 	 */
4515 	pci_ereport_post(dip, err, NULL);
4516 	return (err->fme_status);
4517 }
4518 
4519 static void
4520 e1000g_fm_init(struct e1000g *Adapter)
4521 {
4522 	ddi_iblock_cookie_t iblk;
4523 	int fma_acc_flag, fma_dma_flag;
4524 
4525 	/* Only register with IO Fault Services if we have some capability */
4526 	if (Adapter->fm_capabilities & DDI_FM_ACCCHK_CAPABLE) {
4527 		e1000g_regs_acc_attr.devacc_attr_access = DDI_FLAGERR_ACC;
4528 		fma_acc_flag = 1;
4529 	} else {
4530 		e1000g_regs_acc_attr.devacc_attr_access = DDI_DEFAULT_ACC;
4531 		fma_acc_flag = 0;
4532 	}
4533 
4534 	if (Adapter->fm_capabilities & DDI_FM_DMACHK_CAPABLE) {
4535 		fma_dma_flag = 1;
4536 	} else {
4537 		fma_dma_flag = 0;
4538 	}
4539 
4540 	(void) e1000g_set_fma_flags(Adapter, fma_acc_flag, fma_dma_flag);
4541 
4542 	if (Adapter->fm_capabilities) {
4543 
4544 		/* Register capabilities with IO Fault Services */
4545 		ddi_fm_init(Adapter->dip, &Adapter->fm_capabilities, &iblk);
4546 
4547 		/*
4548 		 * Initialize pci ereport capabilities if ereport capable
4549 		 */
4550 		if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities) ||
4551 		    DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
4552 			pci_ereport_setup(Adapter->dip);
4553 
4554 		/*
4555 		 * Register error callback if error callback capable
4556 		 */
4557 		if (DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
4558 			ddi_fm_handler_register(Adapter->dip,
4559 			    e1000g_fm_error_cb, (void*) Adapter);
4560 	}
4561 }
4562 
4563 static void
4564 e1000g_fm_fini(struct e1000g *Adapter)
4565 {
4566 	/* Only unregister FMA capabilities if we registered some */
4567 	if (Adapter->fm_capabilities) {
4568 
4569 		/*
4570 		 * Release any resources allocated by pci_ereport_setup()
4571 		 */
4572 		if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities) ||
4573 		    DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
4574 			pci_ereport_teardown(Adapter->dip);
4575 
4576 		/*
4577 		 * Un-register error callback if error callback capable
4578 		 */
4579 		if (DDI_FM_ERRCB_CAP(Adapter->fm_capabilities))
4580 			ddi_fm_handler_unregister(Adapter->dip);
4581 
4582 		/* Unregister from IO Fault Services */
4583 		ddi_fm_fini(Adapter->dip);
4584 	}
4585 }
4586 
4587 void
4588 e1000g_fm_ereport(struct e1000g *Adapter, char *detail)
4589 {
4590 	uint64_t ena;
4591 	char buf[FM_MAX_CLASS];
4592 
4593 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
4594 	ena = fm_ena_generate(0, FM_ENA_FMT1);
4595 	if (DDI_FM_EREPORT_CAP(Adapter->fm_capabilities)) {
4596 		ddi_fm_ereport_post(Adapter->dip, buf, ena, DDI_NOSLEEP,
4597 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, NULL);
4598 	}
4599 }
4600