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