xref: /titanic_50/usr/src/uts/common/io/e1000g/e1000g_main.c (revision 1100f00d5652de2808b73c61bcfdb3fc87ef1fc8)
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 		Adapter->smartspeed = 0;
2596 	} else {
2597 		if (Adapter->link_state != LINK_STATE_DOWN) {
2598 			Adapter->link_speed = 0;
2599 			Adapter->link_duplex = 0;
2600 			Adapter->link_state = LINK_STATE_DOWN;
2601 			link_changed = B_TRUE;
2602 
2603 			/*
2604 			 * SmartSpeed workaround for Tabor/TanaX, When the
2605 			 * driver loses link disable auto master/slave
2606 			 * resolution.
2607 			 */
2608 			if (hw->phy_type == e1000_phy_igp) {
2609 				e1000_read_phy_reg(hw,
2610 				    PHY_1000T_CTRL, &phydata);
2611 				phydata |= CR_1000T_MS_ENABLE;
2612 				e1000_write_phy_reg(hw,
2613 				    PHY_1000T_CTRL, phydata);
2614 			}
2615 		} else {
2616 			e1000g_smartspeed(Adapter);
2617 		}
2618 
2619 		if (Adapter->started) {
2620 			if (Adapter->tx_link_down_timeout <
2621 			    MAX_TX_LINK_DOWN_TIMEOUT) {
2622 				Adapter->tx_link_down_timeout++;
2623 			} else if (Adapter->tx_link_down_timeout ==
2624 			    MAX_TX_LINK_DOWN_TIMEOUT) {
2625 				rw_enter(&Adapter->chip_lock, RW_WRITER);
2626 				e1000g_tx_drop(Adapter);
2627 				rw_exit(&Adapter->chip_lock);
2628 				Adapter->tx_link_down_timeout++;
2629 			}
2630 		}
2631 	}
2632 
2633 	return (link_changed);
2634 }
2635 
2636 static void
2637 e1000g_LocalTimer(void *ws)
2638 {
2639 	struct e1000g *Adapter = (struct e1000g *)ws;
2640 	struct e1000_hw *hw;
2641 	e1000g_ether_addr_t ether_addr;
2642 	boolean_t link_changed;
2643 
2644 	hw = &Adapter->Shared;
2645 
2646 	(void) e1000g_tx_freemsg((caddr_t)Adapter, NULL);
2647 
2648 	if (e1000g_stall_check(Adapter)) {
2649 		e1000g_DEBUGLOG_0(Adapter, e1000g_INFO_LEVEL,
2650 		    "Tx stall detected. Activate automatic recovery.\n");
2651 		Adapter->StallWatchdog = 0;
2652 		Adapter->tx_recycle_fail = 0;
2653 		Adapter->reset_count++;
2654 		(void) e1000g_reset(Adapter);
2655 	}
2656 
2657 	link_changed = B_FALSE;
2658 	mutex_enter(&Adapter->e1000g_linklock);
2659 	if (Adapter->link_complete)
2660 		link_changed = e1000g_link_check(Adapter);
2661 	mutex_exit(&Adapter->e1000g_linklock);
2662 
2663 	if (link_changed) {
2664 		/*
2665 		 * Workaround for esb2. Data stuck in fifo on a link
2666 		 * down event. Reset the adapter to recover it.
2667 		 */
2668 		if ((Adapter->link_state == LINK_STATE_DOWN) &&
2669 		    (hw->mac_type == e1000_80003es2lan))
2670 			(void) e1000g_reset(Adapter);
2671 
2672 		mac_link_update(Adapter->mh, Adapter->link_state);
2673 	}
2674 
2675 	/*
2676 	 * With 82571 controllers, any locally administered address will
2677 	 * be overwritten when there is a reset on the other port.
2678 	 * Detect this circumstance and correct it.
2679 	 */
2680 	if ((hw->mac_type == e1000_82571) && hw->laa_is_present) {
2681 		ether_addr.reg.low = E1000_READ_REG_ARRAY(hw, RA, 0);
2682 		ether_addr.reg.high = E1000_READ_REG_ARRAY(hw, RA, 1);
2683 
2684 		ether_addr.reg.low = ntohl(ether_addr.reg.low);
2685 		ether_addr.reg.high = ntohl(ether_addr.reg.high);
2686 
2687 		if ((ether_addr.mac.addr[5] != hw->mac_addr[0]) ||
2688 		    (ether_addr.mac.addr[4] != hw->mac_addr[1]) ||
2689 		    (ether_addr.mac.addr[3] != hw->mac_addr[2]) ||
2690 		    (ether_addr.mac.addr[2] != hw->mac_addr[3]) ||
2691 		    (ether_addr.mac.addr[1] != hw->mac_addr[4]) ||
2692 		    (ether_addr.mac.addr[0] != hw->mac_addr[5])) {
2693 			e1000_rar_set(hw, hw->mac_addr, 0);
2694 		}
2695 	}
2696 
2697 	/*
2698 	 * RP: ttl_workaround : DCR 49
2699 	 */
2700 	e1000_igp_ttl_workaround(hw);
2701 
2702 	/*
2703 	 * Check for Adaptive IFS settings If there are lots of collisions
2704 	 * change the value in steps...
2705 	 * These properties should only be set for 10/100
2706 	 */
2707 	if ((hw->media_type == e1000_media_type_copper) &&
2708 	    ((Adapter->link_speed == SPEED_100) ||
2709 	    (Adapter->link_speed == SPEED_10))) {
2710 		e1000_update_adaptive(hw);
2711 	}
2712 	/*
2713 	 * Set Timer Interrupts
2714 	 */
2715 	E1000_WRITE_REG(hw, ICS, E1000_IMS_RXT0);
2716 
2717 	restart_timeout(Adapter);
2718 }
2719 
2720 /*
2721  * The function e1000g_link_timer() is called when the timer for link setup
2722  * is expired, which indicates the completion of the link setup. The link
2723  * state will not be updated until the link setup is completed. And the
2724  * link state will not be sent to the upper layer through mac_link_update()
2725  * in this function. It will be updated in the local timer routine or the
2726  * interrupt service routine after the interface is started (plumbed).
2727  */
2728 static void
2729 e1000g_link_timer(void *arg)
2730 {
2731 	struct e1000g *Adapter = (struct e1000g *)arg;
2732 
2733 	mutex_enter(&Adapter->e1000g_linklock);
2734 	Adapter->link_complete = B_TRUE;
2735 	Adapter->link_tid = 0;
2736 	mutex_exit(&Adapter->e1000g_linklock);
2737 }
2738 
2739 /*
2740  * **********************************************************************
2741  * Name:      e1000g_force_speed_duplex					*
2742  *									*
2743  * Description:								*
2744  *   This function forces speed and duplex for 10/100 Mbps speeds	*
2745  *   and also for 1000 Mbps speeds, it advertises half or full duplex	*
2746  *									*
2747  * Parameter Passed:							*
2748  *   struct e1000g* (information of adpater)				*
2749  *									*
2750  * Return Value:							*
2751  *									*
2752  * Functions called:							*
2753  * **********************************************************************
2754  */
2755 static void
2756 e1000g_force_speed_duplex(struct e1000g *Adapter)
2757 {
2758 	int forced;
2759 
2760 	/*
2761 	 * get value out of config file
2762 	 */
2763 	forced = e1000g_getprop(Adapter, "ForceSpeedDuplex",
2764 	    GDIAG_10_HALF, GDIAG_ANY, GDIAG_ANY);
2765 
2766 	switch (forced) {
2767 	case GDIAG_10_HALF:
2768 		/*
2769 		 * Disable Auto Negotiation
2770 		 */
2771 		Adapter->Shared.autoneg = B_FALSE;
2772 		Adapter->Shared.forced_speed_duplex = e1000_10_half;
2773 		break;
2774 	case GDIAG_10_FULL:
2775 		/*
2776 		 * Disable Auto Negotiation
2777 		 */
2778 		Adapter->Shared.autoneg = B_FALSE;
2779 		Adapter->Shared.forced_speed_duplex = e1000_10_full;
2780 		break;
2781 	case GDIAG_100_HALF:
2782 		/*
2783 		 * Disable Auto Negotiation
2784 		 */
2785 		Adapter->Shared.autoneg = B_FALSE;
2786 		Adapter->Shared.forced_speed_duplex = e1000_100_half;
2787 		break;
2788 	case GDIAG_100_FULL:
2789 		/*
2790 		 * Disable Auto Negotiation
2791 		 */
2792 		Adapter->Shared.autoneg = B_FALSE;
2793 		Adapter->Shared.forced_speed_duplex = e1000_100_full;
2794 		break;
2795 	case GDIAG_1000_FULL:
2796 		/*
2797 		 * The gigabit spec requires autonegotiation.  Therefore,
2798 		 * when the user wants to force the speed to 1000Mbps, we
2799 		 * enable AutoNeg, but only allow the harware to advertise
2800 		 * 1000Mbps.  This is different from 10/100 operation, where
2801 		 * we are allowed to link without any negotiation.
2802 		 */
2803 		Adapter->Shared.autoneg = B_TRUE;
2804 		Adapter->Shared.autoneg_advertised = ADVERTISE_1000_FULL;
2805 		break;
2806 	default:	/* obey the setting of AutoNegAdvertised */
2807 		Adapter->Shared.autoneg = B_TRUE;
2808 		Adapter->Shared.autoneg_advertised =
2809 		    (uint16_t)e1000g_getprop(Adapter, "AutoNegAdvertised",
2810 		    0, AUTONEG_ADVERTISE_SPEED_DEFAULT,
2811 		    AUTONEG_ADVERTISE_SPEED_DEFAULT);
2812 		break;
2813 	}	/* switch */
2814 }
2815 
2816 /*
2817  * **********************************************************************
2818  * Name:      e1000g_get_max_frame_size					*
2819  *									*
2820  * Description:								*
2821  *   This function reads MaxFrameSize from e1000g.conf and sets it for	*
2822  *   adapter.								*
2823  *									*
2824  * Parameter Passed:							*
2825  *   struct e1000g* (information of adpater)				*
2826  *									*
2827  * Return Value:							*
2828  *									*
2829  * Functions called:							*
2830  * **********************************************************************
2831  */
2832 static void
2833 e1000g_get_max_frame_size(struct e1000g *Adapter)
2834 {
2835 	int max_frame;
2836 
2837 	/*
2838 	 * get value out of config file
2839 	 */
2840 	max_frame = e1000g_getprop(Adapter, "MaxFrameSize", 0, 3, 0);
2841 
2842 	switch (max_frame) {
2843 	case 0:
2844 		Adapter->Shared.max_frame_size = ETHERMAX;
2845 		break;
2846 	case 1:
2847 		Adapter->Shared.max_frame_size = FRAME_SIZE_UPTO_4K;
2848 		break;
2849 	case 2:
2850 		Adapter->Shared.max_frame_size = FRAME_SIZE_UPTO_8K;
2851 		break;
2852 	case 3:
2853 		if (Adapter->Shared.mac_type < e1000_82571)
2854 			Adapter->Shared.max_frame_size = FRAME_SIZE_UPTO_16K;
2855 		else
2856 			Adapter->Shared.max_frame_size = FRAME_SIZE_UPTO_10K;
2857 		break;
2858 	default:
2859 		Adapter->Shared.max_frame_size = ETHERMAX;
2860 		break;
2861 	}	/* switch */
2862 
2863 	/* ich8 does not do jumbo frames */
2864 	if (Adapter->Shared.mac_type == e1000_ich8lan) {
2865 		Adapter->Shared.max_frame_size = ETHERMAX;
2866 	}
2867 }
2868 
2869 static void
2870 arm_timer(struct e1000g *Adapter)
2871 {
2872 	Adapter->WatchDogTimer_id =
2873 	    timeout(e1000g_LocalTimer,
2874 	    (void *)Adapter, 1 * drv_usectohz(1000000));
2875 }
2876 
2877 static void
2878 enable_timeout(struct e1000g *Adapter)
2879 {
2880 	mutex_enter(&Adapter->e1000g_timeout_lock);
2881 
2882 	if (!Adapter->timeout_enabled) {
2883 		Adapter->timeout_enabled = B_TRUE;
2884 		Adapter->timeout_started = B_TRUE;
2885 
2886 		arm_timer(Adapter);
2887 	}
2888 
2889 	mutex_exit(&Adapter->e1000g_timeout_lock);
2890 }
2891 
2892 static void
2893 disable_timeout(struct e1000g *Adapter)
2894 {
2895 	timeout_id_t tid;
2896 
2897 	mutex_enter(&Adapter->e1000g_timeout_lock);
2898 
2899 	Adapter->timeout_enabled = B_FALSE;
2900 	Adapter->timeout_started = B_FALSE;
2901 
2902 	tid = Adapter->WatchDogTimer_id;
2903 	Adapter->WatchDogTimer_id = 0;
2904 
2905 	mutex_exit(&Adapter->e1000g_timeout_lock);
2906 
2907 	if (tid != 0)
2908 		(void) untimeout(tid);
2909 }
2910 
2911 static void
2912 start_timeout(struct e1000g *Adapter)
2913 {
2914 	mutex_enter(&Adapter->e1000g_timeout_lock);
2915 
2916 	if (Adapter->timeout_enabled) {
2917 		if (!Adapter->timeout_started) {
2918 			Adapter->timeout_started = B_TRUE;
2919 			arm_timer(Adapter);
2920 		}
2921 	}
2922 
2923 	mutex_exit(&Adapter->e1000g_timeout_lock);
2924 }
2925 
2926 static void
2927 restart_timeout(struct e1000g *Adapter)
2928 {
2929 	mutex_enter(&Adapter->e1000g_timeout_lock);
2930 
2931 	if (Adapter->timeout_started)
2932 		arm_timer(Adapter);
2933 
2934 	mutex_exit(&Adapter->e1000g_timeout_lock);
2935 }
2936 
2937 static void
2938 stop_timeout(struct e1000g *Adapter)
2939 {
2940 	timeout_id_t tid;
2941 
2942 	mutex_enter(&Adapter->e1000g_timeout_lock);
2943 
2944 	Adapter->timeout_started = B_FALSE;
2945 
2946 	tid = Adapter->WatchDogTimer_id;
2947 	Adapter->WatchDogTimer_id = 0;
2948 
2949 	mutex_exit(&Adapter->e1000g_timeout_lock);
2950 
2951 	if (tid != 0)
2952 		(void) untimeout(tid);
2953 }
2954 
2955 void
2956 e1000g_DisableInterrupt(struct e1000g *Adapter)
2957 {
2958 	E1000_WRITE_REG(&Adapter->Shared, IMC,
2959 	    0xffffffff & ~E1000_IMC_RXSEQ);
2960 }
2961 
2962 void
2963 e1000g_EnableInterrupt(struct e1000g *Adapter)
2964 {
2965 	E1000_WRITE_REG(&Adapter->Shared, IMS,
2966 	    IMS_ENABLE_MASK & ~E1000_IMS_TXDW & ~E1000_IMS_TXQE);
2967 }
2968 
2969 void
2970 e1000g_DisableAllInterrupts(struct e1000g *Adapter)
2971 {
2972 	E1000_WRITE_REG(&Adapter->Shared, IMC, 0xffffffff)
2973 }
2974 
2975 void
2976 e1000g_EnableTxInterrupt(struct e1000g *Adapter)
2977 {
2978 	E1000_WRITE_REG(&Adapter->Shared, IMS, E1000G_IMS_TX_INTR);
2979 }
2980 
2981 void
2982 e1000g_DisableTxInterrupt(struct e1000g *Adapter)
2983 {
2984 	E1000_WRITE_REG(&Adapter->Shared, IMC, E1000G_IMC_TX_INTR);
2985 }
2986 
2987 void
2988 e1000_pci_set_mwi(struct e1000_hw *hw)
2989 {
2990 	uint16_t val = hw->pci_cmd_word | CMD_MEM_WRT_INVALIDATE;
2991 	e1000_write_pci_cfg(hw, PCI_COMMAND_REGISTER, &val);
2992 }
2993 
2994 void
2995 e1000_pci_clear_mwi(struct e1000_hw *hw)
2996 {
2997 	uint16_t val = hw->pci_cmd_word & ~CMD_MEM_WRT_INVALIDATE;
2998 	e1000_write_pci_cfg(hw, PCI_COMMAND_REGISTER, &val);
2999 }
3000 
3001 void
3002 e1000_write_pci_cfg(struct e1000_hw *adapter,
3003     uint32_t reg, uint16_t *value)
3004 {
3005 	pci_config_put16(((struct e1000g_osdep *)(adapter->back))->handle,
3006 	    reg, *value);
3007 }
3008 
3009 void
3010 e1000_read_pci_cfg(struct e1000_hw *adapter,
3011     uint32_t reg, uint16_t *value)
3012 {
3013 	*value =
3014 	    pci_config_get16(((struct e1000g_osdep *)(adapter->back))->
3015 	    handle, reg);
3016 }
3017 
3018 #ifndef __sparc
3019 void
3020 e1000_io_write(struct e1000_hw *hw, unsigned long port, uint32_t value)
3021 {
3022 	outl(port, value);
3023 }
3024 
3025 uint32_t
3026 e1000_io_read(struct e1000_hw *hw, unsigned long port)
3027 {
3028 	return (inl(port));
3029 }
3030 #endif
3031 
3032 static void
3033 e1000g_smartspeed(struct e1000g *adapter)
3034 {
3035 	uint16_t phy_status;
3036 	uint16_t phy_ctrl;
3037 
3038 	/*
3039 	 * If we're not T-or-T, or we're not autoneg'ing, or we're not
3040 	 * advertising 1000Full, we don't even use the workaround
3041 	 */
3042 	if ((adapter->Shared.phy_type != e1000_phy_igp) ||
3043 	    !adapter->Shared.autoneg ||
3044 	    !(adapter->Shared.autoneg_advertised & ADVERTISE_1000_FULL))
3045 		return;
3046 
3047 	/*
3048 	 * True if this is the first call of this function or after every
3049 	 * 30 seconds of not having link
3050 	 */
3051 	if (adapter->smartspeed == 0) {
3052 		/*
3053 		 * If Master/Slave config fault is asserted twice, we
3054 		 * assume back-to-back
3055 		 */
3056 		e1000_read_phy_reg(&adapter->Shared, PHY_1000T_STATUS,
3057 		    &phy_status);
3058 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
3059 			return;
3060 
3061 		e1000_read_phy_reg(&adapter->Shared, PHY_1000T_STATUS,
3062 		    &phy_status);
3063 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT))
3064 			return;
3065 		/*
3066 		 * We're assuming back-2-back because our status register
3067 		 * insists! there's a fault in the master/slave
3068 		 * relationship that was "negotiated"
3069 		 */
3070 		e1000_read_phy_reg(&adapter->Shared, PHY_1000T_CTRL,
3071 		    &phy_ctrl);
3072 		/*
3073 		 * Is the phy configured for manual configuration of
3074 		 * master/slave?
3075 		 */
3076 		if (phy_ctrl & CR_1000T_MS_ENABLE) {
3077 			/*
3078 			 * Yes.  Then disable manual configuration (enable
3079 			 * auto configuration) of master/slave
3080 			 */
3081 			phy_ctrl &= ~CR_1000T_MS_ENABLE;
3082 			e1000_write_phy_reg(&adapter->Shared,
3083 			    PHY_1000T_CTRL, phy_ctrl);
3084 			/*
3085 			 * Effectively starting the clock
3086 			 */
3087 			adapter->smartspeed++;
3088 			/*
3089 			 * Restart autonegotiation
3090 			 */
3091 			if (!e1000_phy_setup_autoneg(&adapter->Shared) &&
3092 			    !e1000_read_phy_reg(&adapter->Shared, PHY_CTRL,
3093 			    &phy_ctrl)) {
3094 				phy_ctrl |= (MII_CR_AUTO_NEG_EN |
3095 				    MII_CR_RESTART_AUTO_NEG);
3096 				e1000_write_phy_reg(&adapter->Shared,
3097 				    PHY_CTRL, phy_ctrl);
3098 			}
3099 		}
3100 		return;
3101 		/*
3102 		 * Has 6 seconds transpired still without link? Remember,
3103 		 * you should reset the smartspeed counter once you obtain
3104 		 * link
3105 		 */
3106 	} else if (adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
3107 		/*
3108 		 * Yes.  Remember, we did at the start determine that
3109 		 * there's a master/slave configuration fault, so we're
3110 		 * still assuming there's someone on the other end, but we
3111 		 * just haven't yet been able to talk to it. We then
3112 		 * re-enable auto configuration of master/slave to see if
3113 		 * we're running 2/3 pair cables.
3114 		 */
3115 		/*
3116 		 * If still no link, perhaps using 2/3 pair cable
3117 		 */
3118 		e1000_read_phy_reg(&adapter->Shared, PHY_1000T_CTRL,
3119 		    &phy_ctrl);
3120 		phy_ctrl |= CR_1000T_MS_ENABLE;
3121 		e1000_write_phy_reg(&adapter->Shared, PHY_1000T_CTRL,
3122 		    phy_ctrl);
3123 		/*
3124 		 * Restart autoneg with phy enabled for manual
3125 		 * configuration of master/slave
3126 		 */
3127 		if (!e1000_phy_setup_autoneg(&adapter->Shared) &&
3128 		    !e1000_read_phy_reg(&adapter->Shared, PHY_CTRL,
3129 		    &phy_ctrl)) {
3130 			phy_ctrl |=
3131 			    (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
3132 			e1000_write_phy_reg(&adapter->Shared, PHY_CTRL,
3133 			    phy_ctrl);
3134 		}
3135 		/*
3136 		 * Hopefully, there are no more faults and we've obtained
3137 		 * link as a result.
3138 		 */
3139 	}
3140 	/*
3141 	 * Restart process after E1000_SMARTSPEED_MAX iterations (30
3142 	 * seconds)
3143 	 */
3144 	if (adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
3145 		adapter->smartspeed = 0;
3146 }
3147 
3148 static boolean_t
3149 is_valid_mac_addr(uint8_t *mac_addr)
3150 {
3151 	const uint8_t addr_test1[6] = { 0, 0, 0, 0, 0, 0 };
3152 	const uint8_t addr_test2[6] =
3153 	    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3154 
3155 	if (!(bcmp(addr_test1, mac_addr, ETHERADDRL)) ||
3156 	    !(bcmp(addr_test2, mac_addr, ETHERADDRL)))
3157 		return (B_FALSE);
3158 
3159 	return (B_TRUE);
3160 }
3161 
3162 /*
3163  * **********************************************************************
3164  * Name:								*
3165  *	e1000g_stall_check						*
3166  *									*
3167  * Description:								*
3168  *	This function checks if the adapter is stalled. (In transmit)	*
3169  *									*
3170  *	It is called each time the timeout is invoked.			*
3171  *	If the transmit descriptor reclaim continuously fails,		*
3172  *	the watchdog value will increment by 1. If the watchdog		*
3173  *	value exceeds the threshold, the adapter is assumed to		*
3174  *	have stalled and need to be reset.				*
3175  *									*
3176  * Arguments:								*
3177  *	Adapter - A pointer to our context sensitive "Adapter"		*
3178  *	structure.							*
3179  *									*
3180  * Returns:								*
3181  *	B_TRUE - The dapter is assumed to have stalled.			*
3182  *	B_FALSE								*
3183  *									*
3184  * **********************************************************************
3185  */
3186 static boolean_t
3187 e1000g_stall_check(struct e1000g *Adapter)
3188 {
3189 	if (Adapter->link_state != LINK_STATE_UP)
3190 		return (B_FALSE);
3191 
3192 	if (Adapter->tx_recycle_fail > 0)
3193 		Adapter->StallWatchdog++;
3194 	else
3195 		Adapter->StallWatchdog = 0;
3196 
3197 	if (Adapter->StallWatchdog < E1000G_STALL_WATCHDOG_COUNT)
3198 		return (B_FALSE);
3199 
3200 	return (B_TRUE);
3201 }
3202 
3203 
3204 static enum ioc_reply
3205 e1000g_pp_ioctl(struct e1000g *e1000gp, struct iocblk *iocp, mblk_t *mp)
3206 {
3207 	void (*ppfn)(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd);
3208 	e1000g_peekpoke_t *ppd;
3209 	uint64_t mem_va;
3210 	uint64_t maxoff;
3211 	boolean_t peek;
3212 
3213 	switch (iocp->ioc_cmd) {
3214 
3215 	case E1000G_IOC_REG_PEEK:
3216 		peek = B_TRUE;
3217 		break;
3218 
3219 	case E1000G_IOC_REG_POKE:
3220 		peek = B_FALSE;
3221 		break;
3222 
3223 	deault:
3224 		e1000g_DEBUGLOG_1(e1000gp, e1000g_INFO_LEVEL,
3225 		    "e1000g_diag_ioctl: invalid ioctl command 0x%X\n",
3226 		    iocp->ioc_cmd);
3227 		return (IOC_INVAL);
3228 	}
3229 
3230 	/*
3231 	 * Validate format of ioctl
3232 	 */
3233 	if (iocp->ioc_count != sizeof (e1000g_peekpoke_t))
3234 		return (IOC_INVAL);
3235 	if (mp->b_cont == NULL)
3236 		return (IOC_INVAL);
3237 
3238 	ppd = (e1000g_peekpoke_t *)mp->b_cont->b_rptr;
3239 
3240 	/*
3241 	 * Validate request parameters
3242 	 */
3243 	switch (ppd->pp_acc_space) {
3244 
3245 	default:
3246 		e1000g_DEBUGLOG_1(e1000gp, e1000g_INFO_LEVEL,
3247 		    "e1000g_diag_ioctl: invalid access space 0x%X\n",
3248 		    ppd->pp_acc_space);
3249 		return (IOC_INVAL);
3250 
3251 	case E1000G_PP_SPACE_REG:
3252 		/*
3253 		 * Memory-mapped I/O space
3254 		 */
3255 		ASSERT(ppd->pp_acc_size == 4);
3256 		if (ppd->pp_acc_size != 4)
3257 			return (IOC_INVAL);
3258 
3259 		if ((ppd->pp_acc_offset % ppd->pp_acc_size) != 0)
3260 			return (IOC_INVAL);
3261 
3262 		mem_va = 0;
3263 		maxoff = 0x10000;
3264 		ppfn = peek ? e1000g_ioc_peek_reg : e1000g_ioc_poke_reg;
3265 		break;
3266 
3267 	case E1000G_PP_SPACE_E1000G:
3268 		/*
3269 		 * E1000g data structure!
3270 		 */
3271 		mem_va = (uintptr_t)e1000gp;
3272 		maxoff = sizeof (struct e1000g);
3273 		ppfn = peek ? e1000g_ioc_peek_mem : e1000g_ioc_poke_mem;
3274 		break;
3275 
3276 	}
3277 
3278 	if (ppd->pp_acc_offset >= maxoff)
3279 		return (IOC_INVAL);
3280 
3281 	if (ppd->pp_acc_offset + ppd->pp_acc_size > maxoff)
3282 		return (IOC_INVAL);
3283 
3284 	/*
3285 	 * All OK - go!
3286 	 */
3287 	ppd->pp_acc_offset += mem_va;
3288 	(*ppfn)(e1000gp, ppd);
3289 	return (peek ? IOC_REPLY : IOC_ACK);
3290 }
3291 
3292 static void
3293 e1000g_ioc_peek_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
3294 {
3295 	ddi_acc_handle_t handle;
3296 	uint32_t *regaddr;
3297 
3298 	handle =
3299 	    ((struct e1000g_osdep *)(&e1000gp->Shared)->back)->E1000_handle;
3300 	regaddr =
3301 	    (uint32_t *)((&e1000gp->Shared)->hw_addr + ppd->pp_acc_offset);
3302 
3303 	ppd->pp_acc_data = ddi_get32(handle, regaddr);
3304 }
3305 
3306 static void
3307 e1000g_ioc_poke_reg(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
3308 {
3309 	ddi_acc_handle_t handle;
3310 	uint32_t *regaddr;
3311 	uint32_t value;
3312 
3313 	handle =
3314 	    ((struct e1000g_osdep *)(&e1000gp->Shared)->back)->E1000_handle;
3315 	regaddr =
3316 	    (uint32_t *)((&e1000gp->Shared)->hw_addr + ppd->pp_acc_offset);
3317 	value = (uint32_t)ppd->pp_acc_data;
3318 
3319 	ddi_put32(handle, regaddr, value);
3320 }
3321 
3322 static void
3323 e1000g_ioc_peek_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
3324 {
3325 	uint64_t value;
3326 	void *vaddr;
3327 
3328 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
3329 
3330 	switch (ppd->pp_acc_size) {
3331 	case 1:
3332 		value = *(uint8_t *)vaddr;
3333 		break;
3334 
3335 	case 2:
3336 		value = *(uint16_t *)vaddr;
3337 		break;
3338 
3339 	case 4:
3340 		value = *(uint32_t *)vaddr;
3341 		break;
3342 
3343 	case 8:
3344 		value = *(uint64_t *)vaddr;
3345 		break;
3346 	}
3347 
3348 	e1000g_DEBUGLOG_4(e1000gp, e1000g_INFO_LEVEL,
3349 	    "e1000g_ioc_peek_mem($%p, $%p) peeked 0x%llx from $%p\n",
3350 	    (void *)e1000gp, (void *)ppd, value, vaddr);
3351 
3352 	ppd->pp_acc_data = value;
3353 }
3354 
3355 static void
3356 e1000g_ioc_poke_mem(struct e1000g *e1000gp, e1000g_peekpoke_t *ppd)
3357 {
3358 	uint64_t value;
3359 	void *vaddr;
3360 
3361 	vaddr = (void *)(uintptr_t)ppd->pp_acc_offset;
3362 	value = ppd->pp_acc_data;
3363 
3364 	e1000g_DEBUGLOG_4(e1000gp, e1000g_INFO_LEVEL,
3365 	    "e1000g_ioc_poke_mem($%p, $%p) poking 0x%llx at $%p\n",
3366 	    (void *)e1000gp, (void *)ppd, value, vaddr);
3367 
3368 	switch (ppd->pp_acc_size) {
3369 	case 1:
3370 		*(uint8_t *)vaddr = (uint8_t)value;
3371 		break;
3372 
3373 	case 2:
3374 		*(uint16_t *)vaddr = (uint16_t)value;
3375 		break;
3376 
3377 	case 4:
3378 		*(uint32_t *)vaddr = (uint32_t)value;
3379 		break;
3380 
3381 	case 8:
3382 		*(uint64_t *)vaddr = (uint64_t)value;
3383 		break;
3384 	}
3385 }
3386 
3387 /*
3388  * Loopback Support
3389  */
3390 static lb_property_t lb_normal =
3391 	{ normal,	"normal",	E1000G_LB_NONE		};
3392 static lb_property_t lb_external1000 =
3393 	{ external,	"1000Mbps",	E1000G_LB_EXTERNAL_1000	};
3394 static lb_property_t lb_external100 =
3395 	{ external,	"100Mbps",	E1000G_LB_EXTERNAL_100	};
3396 static lb_property_t lb_external10 =
3397 	{ external,	"10Mbps",	E1000G_LB_EXTERNAL_10	};
3398 static lb_property_t lb_phy =
3399 	{ internal,	"PHY",		E1000G_LB_INTERNAL_PHY	};
3400 
3401 static enum ioc_reply
3402 e1000g_loopback_ioctl(struct e1000g *Adapter, struct iocblk *iocp, mblk_t *mp)
3403 {
3404 	lb_info_sz_t *lbsp;
3405 	lb_property_t *lbpp;
3406 	struct e1000_hw *hw;
3407 	uint32_t *lbmp;
3408 	uint32_t size;
3409 	uint32_t value;
3410 	uint16_t phy_status;
3411 	uint16_t phy_ext_status;
3412 
3413 	hw = &Adapter->Shared;
3414 
3415 	if (mp->b_cont == NULL)
3416 		return (IOC_INVAL);
3417 
3418 	switch (iocp->ioc_cmd) {
3419 	default:
3420 		return (IOC_INVAL);
3421 
3422 	case LB_GET_INFO_SIZE:
3423 		size = sizeof (lb_info_sz_t);
3424 		if (iocp->ioc_count != size)
3425 			return (IOC_INVAL);
3426 
3427 		e1000_read_phy_reg(hw, PHY_EXT_STATUS, &phy_ext_status);
3428 		e1000_read_phy_reg(hw, PHY_STATUS, &phy_status);
3429 
3430 		value = sizeof (lb_normal);
3431 		if ((phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
3432 		    (phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
3433 		    (hw->media_type == e1000_media_type_fiber) ||
3434 		    (hw->media_type == e1000_media_type_internal_serdes)) {
3435 			value += sizeof (lb_phy);
3436 			switch (hw->mac_type) {
3437 			case e1000_82571:
3438 			case e1000_82572:
3439 				value += sizeof (lb_external1000);
3440 				break;
3441 			}
3442 		}
3443 		if ((phy_status & MII_SR_100X_FD_CAPS) ||
3444 		    (phy_status & MII_SR_100T2_FD_CAPS))
3445 			value += sizeof (lb_external100);
3446 		if (phy_status & MII_SR_10T_FD_CAPS)
3447 			value += sizeof (lb_external10);
3448 
3449 		lbsp = (lb_info_sz_t *)mp->b_cont->b_rptr;
3450 		*lbsp = value;
3451 		break;
3452 
3453 	case LB_GET_INFO:
3454 		e1000_read_phy_reg(hw, PHY_EXT_STATUS, &phy_ext_status);
3455 		e1000_read_phy_reg(hw, PHY_STATUS, &phy_status);
3456 
3457 		value = sizeof (lb_normal);
3458 		if ((phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
3459 		    (phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
3460 		    (hw->media_type == e1000_media_type_fiber) ||
3461 		    (hw->media_type == e1000_media_type_internal_serdes)) {
3462 			value += sizeof (lb_phy);
3463 			switch (hw->mac_type) {
3464 			case e1000_82571:
3465 			case e1000_82572:
3466 				value += sizeof (lb_external1000);
3467 				break;
3468 			}
3469 		}
3470 		if ((phy_status & MII_SR_100X_FD_CAPS) ||
3471 		    (phy_status & MII_SR_100T2_FD_CAPS))
3472 			value += sizeof (lb_external100);
3473 		if (phy_status & MII_SR_10T_FD_CAPS)
3474 			value += sizeof (lb_external10);
3475 
3476 		size = value;
3477 		if (iocp->ioc_count != size)
3478 			return (IOC_INVAL);
3479 
3480 		value = 0;
3481 		lbpp = (lb_property_t *)mp->b_cont->b_rptr;
3482 		lbpp[value++] = lb_normal;
3483 		if ((phy_ext_status & IEEE_ESR_1000T_FD_CAPS) ||
3484 		    (phy_ext_status & IEEE_ESR_1000X_FD_CAPS) ||
3485 		    (hw->media_type == e1000_media_type_fiber) ||
3486 		    (hw->media_type == e1000_media_type_internal_serdes)) {
3487 			lbpp[value++] = lb_phy;
3488 			switch (hw->mac_type) {
3489 			case e1000_82571:
3490 			case e1000_82572:
3491 				lbpp[value++] = lb_external1000;
3492 				break;
3493 			}
3494 		}
3495 		if ((phy_status & MII_SR_100X_FD_CAPS) ||
3496 		    (phy_status & MII_SR_100T2_FD_CAPS))
3497 			lbpp[value++] = lb_external100;
3498 		if (phy_status & MII_SR_10T_FD_CAPS)
3499 			lbpp[value++] = lb_external10;
3500 		break;
3501 
3502 	case LB_GET_MODE:
3503 		size = sizeof (uint32_t);
3504 		if (iocp->ioc_count != size)
3505 			return (IOC_INVAL);
3506 
3507 		lbmp = (uint32_t *)mp->b_cont->b_rptr;
3508 		*lbmp = Adapter->loopback_mode;
3509 		break;
3510 
3511 	case LB_SET_MODE:
3512 		size = 0;
3513 		if (iocp->ioc_count != sizeof (uint32_t))
3514 			return (IOC_INVAL);
3515 
3516 		lbmp = (uint32_t *)mp->b_cont->b_rptr;
3517 		if (!e1000g_set_loopback_mode(Adapter, *lbmp))
3518 			return (IOC_INVAL);
3519 		break;
3520 	}
3521 
3522 	iocp->ioc_count = size;
3523 	iocp->ioc_error = 0;
3524 
3525 	return (IOC_REPLY);
3526 }
3527 
3528 static boolean_t
3529 e1000g_set_loopback_mode(struct e1000g *Adapter, uint32_t mode)
3530 {
3531 	struct e1000_hw *hw;
3532 #ifndef __sparc
3533 	uint32_t reg_rctl;
3534 #endif
3535 	int i, times;
3536 
3537 	if (mode == Adapter->loopback_mode)
3538 		return (B_TRUE);
3539 
3540 	hw = &Adapter->Shared;
3541 	times = 0;
3542 
3543 again:
3544 	switch (mode) {
3545 	default:
3546 		return (B_FALSE);
3547 
3548 	case E1000G_LB_NONE:
3549 		/* Get original speed and duplex settings */
3550 		e1000g_force_speed_duplex(Adapter);
3551 		/* Reset the chip */
3552 		hw->wait_autoneg_complete = B_TRUE;
3553 		(void) e1000g_reset(Adapter);
3554 		hw->wait_autoneg_complete = B_FALSE;
3555 		break;
3556 
3557 	case E1000G_LB_EXTERNAL_1000:
3558 		e1000g_set_external_loopback_1000(Adapter);
3559 		break;
3560 
3561 	case E1000G_LB_EXTERNAL_100:
3562 		e1000g_set_external_loopback_100(Adapter);
3563 		break;
3564 
3565 	case E1000G_LB_EXTERNAL_10:
3566 		e1000g_set_external_loopback_10(Adapter);
3567 		break;
3568 
3569 	case E1000G_LB_INTERNAL_PHY:
3570 		e1000g_set_internal_loopback(Adapter);
3571 		break;
3572 	}
3573 
3574 	times++;
3575 
3576 	switch (mode) {
3577 	case E1000G_LB_EXTERNAL_1000:
3578 	case E1000G_LB_EXTERNAL_100:
3579 	case E1000G_LB_EXTERNAL_10:
3580 	case E1000G_LB_INTERNAL_PHY:
3581 #ifndef __sparc
3582 		/* Enable the CRC stripping for loopback */
3583 		reg_rctl = E1000_READ_REG(hw, RCTL);
3584 		reg_rctl |= E1000_RCTL_SECRC;
3585 		E1000_WRITE_REG(hw, RCTL, reg_rctl);
3586 #endif
3587 		/* Wait for link up */
3588 		for (i = (PHY_FORCE_TIME * 2); i > 0; i--)
3589 			msec_delay(100);
3590 
3591 		if (!e1000g_link_up(Adapter)) {
3592 			e1000g_DEBUGLOG_0(Adapter, e1000g_INFO_LEVEL,
3593 			    "Failed to get the link up");
3594 			if (times < 2) {
3595 				/* Reset the link */
3596 				e1000g_DEBUGLOG_0(Adapter, e1000g_INFO_LEVEL,
3597 				    "Reset the link ...");
3598 				(void) e1000g_reset(Adapter);
3599 				goto again;
3600 			}
3601 		}
3602 		break;
3603 	}
3604 
3605 	Adapter->loopback_mode = mode;
3606 
3607 	return (B_TRUE);
3608 }
3609 
3610 /*
3611  * The following loopback settings are from Intel's technical
3612  * document - "How To Loopback". All the register settings and
3613  * time delay values are directly inherited from the document
3614  * without more explanations available.
3615  */
3616 static void
3617 e1000g_set_internal_loopback(struct e1000g *Adapter)
3618 {
3619 	struct e1000_hw *hw;
3620 	uint32_t ctrl;
3621 	uint32_t status;
3622 	uint16_t phy_ctrl;
3623 
3624 	hw = &Adapter->Shared;
3625 
3626 	/* Disable Smart Power Down */
3627 	phy_spd_state(hw, B_FALSE);
3628 
3629 	e1000_read_phy_reg(hw, PHY_CTRL, &phy_ctrl);
3630 	phy_ctrl &= ~(MII_CR_AUTO_NEG_EN | MII_CR_SPEED_100 | MII_CR_SPEED_10);
3631 	phy_ctrl |= MII_CR_FULL_DUPLEX | MII_CR_SPEED_1000;
3632 
3633 	switch (hw->mac_type) {
3634 	case e1000_82540:
3635 	case e1000_82545:
3636 	case e1000_82545_rev_3:
3637 	case e1000_82546:
3638 	case e1000_82546_rev_3:
3639 	case e1000_82573:
3640 		/* Auto-MDI/MDIX off */
3641 		e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, 0x0808);
3642 		/* Reset PHY to update Auto-MDI/MDIX */
3643 		e1000_write_phy_reg(hw, PHY_CTRL,
3644 		    phy_ctrl | MII_CR_RESET | MII_CR_AUTO_NEG_EN);
3645 		/* Reset PHY to auto-neg off and force 1000 */
3646 		e1000_write_phy_reg(hw, PHY_CTRL,
3647 		    phy_ctrl | MII_CR_RESET);
3648 		break;
3649 	}
3650 
3651 	/* Set loopback */
3652 	e1000_write_phy_reg(hw, PHY_CTRL, phy_ctrl | MII_CR_LOOPBACK);
3653 
3654 	msec_delay(250);
3655 
3656 	/* Now set up the MAC to the same speed/duplex as the PHY. */
3657 	ctrl = E1000_READ_REG(hw, CTRL);
3658 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
3659 	ctrl |= (E1000_CTRL_FRCSPD |	/* Set the Force Speed Bit */
3660 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
3661 	    E1000_CTRL_SPD_1000 |	/* Force Speed to 1000 */
3662 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
3663 
3664 	switch (hw->mac_type) {
3665 	case e1000_82540:
3666 	case e1000_82545:
3667 	case e1000_82545_rev_3:
3668 	case e1000_82546:
3669 	case e1000_82546_rev_3:
3670 		/*
3671 		 * For some serdes we'll need to commit the writes now
3672 		 * so that the status is updated on link
3673 		 */
3674 		if (hw->media_type == e1000_media_type_internal_serdes) {
3675 			E1000_WRITE_REG(hw, CTRL, ctrl);
3676 			msec_delay(100);
3677 			ctrl = E1000_READ_REG(hw, CTRL);
3678 		}
3679 
3680 		if (hw->media_type == e1000_media_type_copper) {
3681 			/* Invert Loss of Signal */
3682 			ctrl |= E1000_CTRL_ILOS;
3683 		} else {
3684 			/* Set ILOS on fiber nic if half duplex is detected */
3685 			status = E1000_READ_REG(hw, STATUS);
3686 			if ((status & E1000_STATUS_FD) == 0)
3687 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
3688 		}
3689 		break;
3690 
3691 	case e1000_82571:
3692 	case e1000_82572:
3693 		if (hw->media_type != e1000_media_type_copper) {
3694 			/* Set ILOS on fiber nic if half duplex is detected */
3695 			status = E1000_READ_REG(hw, STATUS);
3696 			if ((status & E1000_STATUS_FD) == 0)
3697 				ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
3698 		}
3699 		break;
3700 
3701 	case e1000_82573:
3702 		ctrl |= E1000_CTRL_ILOS;
3703 		break;
3704 	}
3705 
3706 	E1000_WRITE_REG(hw, CTRL, ctrl);
3707 
3708 	/*
3709 	 * Disable PHY receiver for 82540/545/546 and 82573 Family.
3710 	 * For background, see comments above e1000g_set_internal_loopback().
3711 	 */
3712 	switch (hw->mac_type) {
3713 	case e1000_82540:
3714 	case e1000_82545:
3715 	case e1000_82545_rev_3:
3716 	case e1000_82546:
3717 	case e1000_82546_rev_3:
3718 	case e1000_82573:
3719 		e1000_write_phy_reg(hw, 29, 0x001F);
3720 		e1000_write_phy_reg(hw, 30, 0x8FFC);
3721 		e1000_write_phy_reg(hw, 29, 0x001A);
3722 		e1000_write_phy_reg(hw, 30, 0x8FF0);
3723 		break;
3724 	}
3725 }
3726 
3727 static void
3728 e1000g_set_external_loopback_1000(struct e1000g *Adapter)
3729 {
3730 	struct e1000_hw *hw;
3731 	uint32_t rctl;
3732 	uint32_t ctrl_ext;
3733 	uint32_t ctrl;
3734 	uint32_t status;
3735 	uint32_t txcw;
3736 
3737 	hw = &Adapter->Shared;
3738 
3739 	/* Disable Smart Power Down */
3740 	phy_spd_state(hw, B_FALSE);
3741 
3742 	switch (hw->media_type) {
3743 	case e1000_media_type_copper:
3744 		/* Force link up (Must be done before the PHY writes) */
3745 		ctrl = E1000_READ_REG(hw, CTRL);
3746 		ctrl |= E1000_CTRL_SLU;	/* Force Link Up */
3747 		E1000_WRITE_REG(hw, CTRL, ctrl);
3748 
3749 		rctl = E1000_READ_REG(hw, RCTL);
3750 		rctl |= (E1000_RCTL_EN |
3751 		    E1000_RCTL_SBP |
3752 		    E1000_RCTL_UPE |
3753 		    E1000_RCTL_MPE |
3754 		    E1000_RCTL_LPE |
3755 		    E1000_RCTL_BAM);		/* 0x803E */
3756 		E1000_WRITE_REG(hw, RCTL, rctl);
3757 
3758 		ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
3759 		ctrl_ext |= (E1000_CTRL_EXT_SDP4_DATA |
3760 		    E1000_CTRL_EXT_SDP6_DATA |
3761 		    E1000_CTRL_EXT_SDP7_DATA |
3762 		    E1000_CTRL_EXT_SDP4_DIR |
3763 		    E1000_CTRL_EXT_SDP6_DIR |
3764 		    E1000_CTRL_EXT_SDP7_DIR);	/* 0x0DD0 */
3765 		E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
3766 
3767 		/*
3768 		 * This sequence tunes the PHY's SDP and no customer
3769 		 * settable values. For background, see comments above
3770 		 * e1000g_set_internal_loopback().
3771 		 */
3772 		e1000_write_phy_reg(hw, 0x0, 0x140);
3773 		msec_delay(10);
3774 		e1000_write_phy_reg(hw, 0x9, 0x1A00);
3775 		e1000_write_phy_reg(hw, 0x12, 0xC10);
3776 		e1000_write_phy_reg(hw, 0x12, 0x1C10);
3777 		e1000_write_phy_reg(hw, 0x1F37, 0x76);
3778 		e1000_write_phy_reg(hw, 0x1F33, 0x1);
3779 		e1000_write_phy_reg(hw, 0x1F33, 0x0);
3780 
3781 		e1000_write_phy_reg(hw, 0x1F35, 0x65);
3782 		e1000_write_phy_reg(hw, 0x1837, 0x3F7C);
3783 		e1000_write_phy_reg(hw, 0x1437, 0x3FDC);
3784 		e1000_write_phy_reg(hw, 0x1237, 0x3F7C);
3785 		e1000_write_phy_reg(hw, 0x1137, 0x3FDC);
3786 
3787 		msec_delay(50);
3788 		break;
3789 	case e1000_media_type_fiber:
3790 	case e1000_media_type_internal_serdes:
3791 		status = E1000_READ_REG(hw, STATUS);
3792 		if (((status & E1000_STATUS_LU) == 0) ||
3793 		    (hw->media_type == e1000_media_type_internal_serdes)) {
3794 			ctrl = E1000_READ_REG(hw, CTRL);
3795 			ctrl |= E1000_CTRL_ILOS | E1000_CTRL_SLU;
3796 			E1000_WRITE_REG(hw, CTRL, ctrl);
3797 		}
3798 
3799 		/* Disable autoneg by setting bit 31 of TXCW to zero */
3800 		txcw = E1000_READ_REG(hw, TXCW);
3801 		txcw &= ~((uint32_t)1 << 31);
3802 		E1000_WRITE_REG(hw, TXCW, txcw);
3803 
3804 		/*
3805 		 * Write 0x410 to Serdes Control register
3806 		 * to enable Serdes analog loopback
3807 		 */
3808 		E1000_WRITE_REG(hw, SCTL, 0x0410);
3809 		msec_delay(10);
3810 		break;
3811 	default:
3812 		break;
3813 	}
3814 }
3815 
3816 static void
3817 e1000g_set_external_loopback_100(struct e1000g *Adapter)
3818 {
3819 	struct e1000_hw *hw;
3820 	uint32_t ctrl;
3821 	uint16_t phy_ctrl;
3822 
3823 	hw = &Adapter->Shared;
3824 
3825 	/* Disable Smart Power Down */
3826 	phy_spd_state(hw, B_FALSE);
3827 
3828 	phy_ctrl = (MII_CR_FULL_DUPLEX |
3829 	    MII_CR_SPEED_100);
3830 
3831 	/* Force 100/FD, reset PHY */
3832 	e1000_write_phy_reg(hw, PHY_CTRL,
3833 	    phy_ctrl | MII_CR_RESET);	/* 0xA100 */
3834 	msec_delay(10);
3835 
3836 	/* Force 100/FD */
3837 	e1000_write_phy_reg(hw, PHY_CTRL,
3838 	    phy_ctrl);			/* 0x2100 */
3839 	msec_delay(10);
3840 
3841 	/* Now setup the MAC to the same speed/duplex as the PHY. */
3842 	ctrl = E1000_READ_REG(hw, CTRL);
3843 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
3844 	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
3845 	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
3846 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
3847 	    E1000_CTRL_SPD_100 |	/* Force Speed to 100 */
3848 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
3849 
3850 	E1000_WRITE_REG(hw, CTRL, ctrl);
3851 }
3852 
3853 static void
3854 e1000g_set_external_loopback_10(struct e1000g *Adapter)
3855 {
3856 	struct e1000_hw *hw;
3857 	uint32_t ctrl;
3858 	uint16_t phy_ctrl;
3859 
3860 	hw = &Adapter->Shared;
3861 
3862 	/* Disable Smart Power Down */
3863 	phy_spd_state(hw, B_FALSE);
3864 
3865 	phy_ctrl = (MII_CR_FULL_DUPLEX |
3866 	    MII_CR_SPEED_10);
3867 
3868 	/* Force 10/FD, reset PHY */
3869 	e1000_write_phy_reg(hw, PHY_CTRL,
3870 	    phy_ctrl | MII_CR_RESET);	/* 0x8100 */
3871 	msec_delay(10);
3872 
3873 	/* Force 10/FD */
3874 	e1000_write_phy_reg(hw, PHY_CTRL,
3875 	    phy_ctrl);			/* 0x0100 */
3876 	msec_delay(10);
3877 
3878 	/* Now setup the MAC to the same speed/duplex as the PHY. */
3879 	ctrl = E1000_READ_REG(hw, CTRL);
3880 	ctrl &= ~E1000_CTRL_SPD_SEL;	/* Clear the speed sel bits */
3881 	ctrl |= (E1000_CTRL_SLU |	/* Force Link Up */
3882 	    E1000_CTRL_FRCSPD |		/* Set the Force Speed Bit */
3883 	    E1000_CTRL_FRCDPX |		/* Set the Force Duplex Bit */
3884 	    E1000_CTRL_SPD_10 |		/* Force Speed to 10 */
3885 	    E1000_CTRL_FD);		/* Force Duplex to FULL */
3886 
3887 	E1000_WRITE_REG(hw, CTRL, ctrl);
3888 }
3889 
3890 #ifdef __sparc
3891 static boolean_t
3892 e1000g_find_mac_address(struct e1000g *Adapter)
3893 {
3894 	uchar_t *bytes;
3895 	struct ether_addr sysaddr;
3896 	uint_t nelts;
3897 	int err;
3898 	boolean_t found = B_FALSE;
3899 
3900 	/*
3901 	 * The "vendor's factory-set address" may already have
3902 	 * been extracted from the chip, but if the property
3903 	 * "local-mac-address" is set we use that instead.
3904 	 *
3905 	 * We check whether it looks like an array of 6
3906 	 * bytes (which it should, if OBP set it).  If we can't
3907 	 * make sense of it this way, we'll ignore it.
3908 	 */
3909 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
3910 	    DDI_PROP_DONTPASS, "local-mac-address", &bytes, &nelts);
3911 	if (err == DDI_PROP_SUCCESS) {
3912 		if (nelts == ETHERADDRL) {
3913 			while (nelts--)
3914 				Adapter->Shared.mac_addr[nelts] = bytes[nelts];
3915 			found = B_TRUE;
3916 		}
3917 		ddi_prop_free(bytes);
3918 	}
3919 
3920 	/*
3921 	 * Look up the OBP property "local-mac-address?". If the user has set
3922 	 * 'local-mac-address? = false', use "the system address" instead.
3923 	 */
3924 	if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip, 0,
3925 	    "local-mac-address?", &bytes, &nelts) == DDI_PROP_SUCCESS) {
3926 		if (strncmp("false", (caddr_t)bytes, (size_t)nelts) == 0) {
3927 			if (localetheraddr(NULL, &sysaddr) != 0) {
3928 				bcopy(&sysaddr, Adapter->Shared.mac_addr,
3929 				    ETHERADDRL);
3930 				found = B_TRUE;
3931 			}
3932 		}
3933 		ddi_prop_free(bytes);
3934 	}
3935 
3936 	/*
3937 	 * Finally(!), if there's a valid "mac-address" property (created
3938 	 * if we netbooted from this interface), we must use this instead
3939 	 * of any of the above to ensure that the NFS/install server doesn't
3940 	 * get confused by the address changing as Solaris takes over!
3941 	 */
3942 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, Adapter->dip,
3943 	    DDI_PROP_DONTPASS, "mac-address", &bytes, &nelts);
3944 	if (err == DDI_PROP_SUCCESS) {
3945 		if (nelts == ETHERADDRL) {
3946 			while (nelts--)
3947 				Adapter->Shared.mac_addr[nelts] = bytes[nelts];
3948 			found = B_TRUE;
3949 		}
3950 		ddi_prop_free(bytes);
3951 	}
3952 
3953 	if (found) {
3954 		bcopy(Adapter->Shared.mac_addr, Adapter->Shared.perm_mac_addr,
3955 		    ETHERADDRL);
3956 	}
3957 
3958 	return (found);
3959 }
3960 #endif
3961 
3962 static int
3963 e1000g_add_intrs(struct e1000g *Adapter)
3964 {
3965 	dev_info_t *devinfo;
3966 	int intr_types;
3967 	int rc;
3968 
3969 	devinfo = Adapter->dip;
3970 
3971 	/* Get supported interrupt types */
3972 	rc = ddi_intr_get_supported_types(devinfo, &intr_types);
3973 
3974 	if (rc != DDI_SUCCESS) {
3975 		e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
3976 		    "Get supported interrupt types failed: %d\n", rc);
3977 		return (DDI_FAILURE);
3978 	}
3979 
3980 	/*
3981 	 * Based on Intel Technical Advisory document (TA-160), there are some
3982 	 * cases where some older Intel PCI-X NICs may "advertise" to the OS
3983 	 * that it supports MSI, but in fact has problems.
3984 	 * So we should only enable MSI for PCI-E NICs and disable MSI for old
3985 	 * PCI/PCI-X NICs.
3986 	 */
3987 	if (Adapter->Shared.mac_type < e1000_82571)
3988 		Adapter->msi_enabled = B_FALSE;
3989 
3990 	if ((intr_types & DDI_INTR_TYPE_MSI) && Adapter->msi_enabled) {
3991 		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_MSI);
3992 
3993 		if (rc != DDI_SUCCESS) {
3994 			e1000g_DEBUGLOG_0(Adapter, e1000g_INFO_LEVEL,
3995 			    "Add MSI failed, trying Legacy interrupts\n");
3996 		} else {
3997 			Adapter->intr_type = DDI_INTR_TYPE_MSI;
3998 		}
3999 	}
4000 
4001 	if ((Adapter->intr_type == 0) &&
4002 	    (intr_types & DDI_INTR_TYPE_FIXED)) {
4003 		rc = e1000g_intr_add(Adapter, DDI_INTR_TYPE_FIXED);
4004 
4005 		if (rc != DDI_SUCCESS) {
4006 			e1000g_DEBUGLOG_0(Adapter, e1000g_INFO_LEVEL,
4007 			    "Add Legacy interrupts failed\n");
4008 			return (DDI_FAILURE);
4009 		}
4010 
4011 		Adapter->intr_type = DDI_INTR_TYPE_FIXED;
4012 	}
4013 
4014 	if (Adapter->intr_type == 0) {
4015 		e1000g_DEBUGLOG_0(Adapter, e1000g_INFO_LEVEL,
4016 		    "No interrupts registered\n");
4017 		return (DDI_FAILURE);
4018 	}
4019 
4020 	return (DDI_SUCCESS);
4021 }
4022 
4023 /*
4024  * e1000g_intr_add() handles MSI/Legacy interrupts
4025  */
4026 static int
4027 e1000g_intr_add(struct e1000g *Adapter, int intr_type)
4028 {
4029 	dev_info_t *devinfo;
4030 	int count, avail, actual;
4031 	int x, y, rc, inum = 0;
4032 	int flag;
4033 	ddi_intr_handler_t *intr_handler;
4034 
4035 	devinfo = Adapter->dip;
4036 
4037 	/* get number of interrupts */
4038 	rc = ddi_intr_get_nintrs(devinfo, intr_type, &count);
4039 	if ((rc != DDI_SUCCESS) || (count == 0)) {
4040 		e1000g_DEBUGLOG_2(Adapter, e1000g_INFO_LEVEL,
4041 		    "Get interrupt number failed. Return: %d, count: %d\n",
4042 		    rc, count);
4043 		return (DDI_FAILURE);
4044 	}
4045 
4046 	/* get number of available interrupts */
4047 	rc = ddi_intr_get_navail(devinfo, intr_type, &avail);
4048 	if ((rc != DDI_SUCCESS) || (avail == 0)) {
4049 		e1000g_DEBUGLOG_2(Adapter, e1000g_INFO_LEVEL,
4050 		    "Get interrupt available number failed. "
4051 		    "Return: %d, available: %d\n", rc, avail);
4052 		return (DDI_FAILURE);
4053 	}
4054 
4055 	if (avail < count) {
4056 		e1000g_DEBUGLOG_2(Adapter, e1000g_INFO_LEVEL,
4057 		    "Interrupts count: %d, available: %d\n",
4058 		    count, avail);
4059 	}
4060 
4061 	/* Allocate an array of interrupt handles */
4062 	Adapter->intr_size = count * sizeof (ddi_intr_handle_t);
4063 	Adapter->htable = kmem_alloc(Adapter->intr_size, KM_SLEEP);
4064 
4065 	/* Set NORMAL behavior for both MSI and FIXED interrupt */
4066 	flag = DDI_INTR_ALLOC_NORMAL;
4067 
4068 	/* call ddi_intr_alloc() */
4069 	rc = ddi_intr_alloc(devinfo, Adapter->htable, intr_type, inum,
4070 	    count, &actual, flag);
4071 
4072 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
4073 		e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
4074 		    "Allocate interrupts failed: %d\n", rc);
4075 
4076 		kmem_free(Adapter->htable, Adapter->intr_size);
4077 		return (DDI_FAILURE);
4078 	}
4079 
4080 	if (actual < count) {
4081 		e1000g_DEBUGLOG_2(Adapter, e1000g_INFO_LEVEL,
4082 		    "Interrupts requested: %d, received: %d\n",
4083 		    count, actual);
4084 	}
4085 
4086 	Adapter->intr_cnt = actual;
4087 
4088 	/* Get priority for first msi, assume remaining are all the same */
4089 	rc = ddi_intr_get_pri(Adapter->htable[0], &Adapter->intr_pri);
4090 
4091 	if (rc != DDI_SUCCESS) {
4092 		e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
4093 		    "Get interrupt priority failed: %d\n", rc);
4094 
4095 		/* Free already allocated intr */
4096 		for (y = 0; y < actual; y++)
4097 			(void) ddi_intr_free(Adapter->htable[y]);
4098 
4099 		kmem_free(Adapter->htable, Adapter->intr_size);
4100 		return (DDI_FAILURE);
4101 	}
4102 
4103 	/*
4104 	 * In Legacy Interrupt mode, for PCI-Express adapters, we should
4105 	 * use the interrupt service routine e1000g_intr_pciexpress()
4106 	 * to avoid interrupt stealing when sharing interrupt with other
4107 	 * devices.
4108 	 */
4109 	if (Adapter->Shared.mac_type < e1000_82571)
4110 		intr_handler = (ddi_intr_handler_t *)e1000g_intr;
4111 	else
4112 		intr_handler = (ddi_intr_handler_t *)e1000g_intr_pciexpress;
4113 
4114 	/* Call ddi_intr_add_handler() */
4115 	for (x = 0; x < actual; x++) {
4116 		rc = ddi_intr_add_handler(Adapter->htable[x],
4117 		    intr_handler, (caddr_t)Adapter, NULL);
4118 
4119 		if (rc != DDI_SUCCESS) {
4120 			e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
4121 			    "Add interrupt handler failed: %d\n", rc);
4122 
4123 			/* Remove already added handler */
4124 			for (y = 0; y < x; y++)
4125 				(void) ddi_intr_remove_handler(
4126 				    Adapter->htable[y]);
4127 
4128 			/* Free already allocated intr */
4129 			for (y = 0; y < actual; y++)
4130 				(void) ddi_intr_free(Adapter->htable[y]);
4131 
4132 			kmem_free(Adapter->htable, Adapter->intr_size);
4133 			return (DDI_FAILURE);
4134 		}
4135 	}
4136 
4137 	rc = ddi_intr_get_cap(Adapter->htable[0], &Adapter->intr_cap);
4138 
4139 	if (rc != DDI_SUCCESS) {
4140 		e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
4141 		    "Get interrupt cap failed: %d\n", rc);
4142 
4143 		/* Free already allocated intr */
4144 		for (y = 0; y < actual; y++) {
4145 			(void) ddi_intr_remove_handler(Adapter->htable[y]);
4146 			(void) ddi_intr_free(Adapter->htable[y]);
4147 		}
4148 
4149 		kmem_free(Adapter->htable, Adapter->intr_size);
4150 		return (DDI_FAILURE);
4151 	}
4152 
4153 	return (DDI_SUCCESS);
4154 }
4155 
4156 static int
4157 e1000g_rem_intrs(struct e1000g *Adapter)
4158 {
4159 	int x;
4160 	int rc;
4161 
4162 	for (x = 0; x < Adapter->intr_cnt; x++) {
4163 		rc = ddi_intr_remove_handler(Adapter->htable[x]);
4164 		if (rc != DDI_SUCCESS) {
4165 			e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
4166 			    "Remove intr handler failed: %d\n", rc);
4167 			return (DDI_FAILURE);
4168 		}
4169 
4170 		rc = ddi_intr_free(Adapter->htable[x]);
4171 		if (rc != DDI_SUCCESS) {
4172 			e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
4173 			    "Free intr failed: %d\n", rc);
4174 			return (DDI_FAILURE);
4175 		}
4176 	}
4177 
4178 	kmem_free(Adapter->htable, Adapter->intr_size);
4179 
4180 	return (DDI_SUCCESS);
4181 }
4182 
4183 static int
4184 e1000g_enable_intrs(struct e1000g *Adapter)
4185 {
4186 	int x;
4187 	int rc;
4188 
4189 	/* Enable interrupts */
4190 	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
4191 		/* Call ddi_intr_block_enable() for MSI */
4192 		rc = ddi_intr_block_enable(Adapter->htable,
4193 		    Adapter->intr_cnt);
4194 		if (rc != DDI_SUCCESS) {
4195 			e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
4196 			    "Enable block intr failed: %d\n", rc);
4197 			return (DDI_FAILURE);
4198 		}
4199 	} else {
4200 		/* Call ddi_intr_enable() for Legacy/MSI non block enable */
4201 		for (x = 0; x < Adapter->intr_cnt; x++) {
4202 			rc = ddi_intr_enable(Adapter->htable[x]);
4203 			if (rc != DDI_SUCCESS) {
4204 				e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
4205 				    "Enable intr failed: %d\n", rc);
4206 				return (DDI_FAILURE);
4207 			}
4208 		}
4209 	}
4210 
4211 	return (DDI_SUCCESS);
4212 }
4213 
4214 static int
4215 e1000g_disable_intrs(struct e1000g *Adapter)
4216 {
4217 	int x;
4218 	int rc;
4219 
4220 	/* Disable all interrupts */
4221 	if (Adapter->intr_cap & DDI_INTR_FLAG_BLOCK) {
4222 		rc = ddi_intr_block_disable(Adapter->htable,
4223 		    Adapter->intr_cnt);
4224 		if (rc != DDI_SUCCESS) {
4225 			e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
4226 			    "Disable block intr failed: %d\n", rc);
4227 			return (DDI_FAILURE);
4228 		}
4229 	} else {
4230 		for (x = 0; x < Adapter->intr_cnt; x++) {
4231 			rc = ddi_intr_disable(Adapter->htable[x]);
4232 			if (rc != DDI_SUCCESS) {
4233 				e1000g_DEBUGLOG_1(Adapter, e1000g_INFO_LEVEL,
4234 				    "Disable intr failed: %d\n", rc);
4235 				return (DDI_FAILURE);
4236 			}
4237 		}
4238 	}
4239 
4240 	return (DDI_SUCCESS);
4241 }
4242 
4243 /*
4244  * phy_spd_state - set smart-power-down (SPD) state
4245  *
4246  * This only acts on the 82541/47 family and the 82571/72 family.
4247  * For any others, return without doing anything.
4248  */
4249 void
4250 phy_spd_state(struct e1000_hw *hw, boolean_t enable)
4251 {
4252 	int32_t offset;		/* offset to register */
4253 	uint16_t spd_bit;	/* bit to be set */
4254 	uint16_t reg;		/* register contents */
4255 
4256 	switch (hw->mac_type) {
4257 	case e1000_82541:
4258 	case e1000_82547:
4259 	case e1000_82541_rev_2:
4260 	case e1000_82547_rev_2:
4261 		offset = IGP01E1000_GMII_FIFO;
4262 		spd_bit = IGP01E1000_GMII_SPD;
4263 		break;
4264 	case e1000_82571:
4265 	case e1000_82572:
4266 		offset = IGP02E1000_PHY_POWER_MGMT;
4267 		spd_bit = IGP02E1000_PM_SPD;
4268 		break;
4269 	default:
4270 		return;		/* no action */
4271 	}
4272 
4273 	e1000_read_phy_reg(hw, offset, &reg);
4274 
4275 	if (enable)
4276 		reg |= spd_bit;		/* enable: set the spd bit */
4277 	else
4278 		reg &= ~spd_bit;	/* disable: clear the spd bit */
4279 
4280 	e1000_write_phy_reg(hw, offset, reg);
4281 }
4282 
4283 /*
4284  * The real intent of this routine is to return the value from pci-e
4285  * config space at offset reg into the capability space.
4286  * ICH devices are "PCI Express"-ish.  They have a configuration space,
4287  * but do not contain PCI Express Capability registers, so this returns
4288  * the equivalent of "not supported"
4289  */
4290 int32_t
4291 e1000_read_pcie_cap_reg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
4292 {
4293 	*value = pci_config_get16(((struct e1000g_osdep *)hw->back)->handle,
4294 	    PCI_EX_CONF_CAP + reg);
4295 
4296 	return (0);
4297 }
4298 
4299 /*
4300  * Enables PCI-Express master access.
4301  *
4302  * hw: Struct containing variables accessed by shared code
4303  *
4304  * returns: - none.
4305  */
4306 void
4307 e1000_enable_pciex_master(struct e1000_hw *hw)
4308 {
4309 	uint32_t ctrl;
4310 
4311 	if (hw->bus_type != e1000_bus_type_pci_express)
4312 		return;
4313 
4314 	ctrl = E1000_READ_REG(hw, CTRL);
4315 	ctrl &= ~E1000_CTRL_GIO_MASTER_DISABLE;
4316 	E1000_WRITE_REG(hw, CTRL, ctrl);
4317 }
4318