xref: /titanic_41/usr/src/uts/common/io/nge/nge_main.c (revision cc1a9a89a73172cc2db053635fab3b1b91691657)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 
28 #include "nge.h"
29 
30 /*
31  * Describes the chip's DMA engine
32  */
33 
34 static ddi_dma_attr_t hot_dma_attr = {
35 	DMA_ATTR_V0,			/* dma_attr version	*/
36 	0x0000000000000000ull,		/* dma_attr_addr_lo	*/
37 	0x000000FFFFFFFFFFull,		/* dma_attr_addr_hi	*/
38 	0x000000007FFFFFFFull,		/* dma_attr_count_max	*/
39 	0x0000000000000010ull,		/* dma_attr_align	*/
40 	0x00000FFF,			/* dma_attr_burstsizes	*/
41 	0x00000001,			/* dma_attr_minxfer	*/
42 	0x000000000000FFFFull,		/* dma_attr_maxxfer	*/
43 	0x000000FFFFFFFFFFull,		/* dma_attr_seg		*/
44 	1,				/* dma_attr_sgllen 	*/
45 	0x00000001,			/* dma_attr_granular 	*/
46 	0
47 };
48 
49 static ddi_dma_attr_t hot_tx_dma_attr = {
50 	DMA_ATTR_V0,			/* dma_attr version	*/
51 	0x0000000000000000ull,		/* dma_attr_addr_lo	*/
52 	0x000000FFFFFFFFFFull,		/* dma_attr_addr_hi	*/
53 	0x0000000000003FFFull,		/* dma_attr_count_max	*/
54 	0x0000000000000010ull,		/* dma_attr_align	*/
55 	0x00000FFF,			/* dma_attr_burstsizes	*/
56 	0x00000001,			/* dma_attr_minxfer	*/
57 	0x0000000000003FFFull,		/* dma_attr_maxxfer	*/
58 	0x000000FFFFFFFFFFull,		/* dma_attr_seg		*/
59 	NGE_MAX_COOKIES,		/* dma_attr_sgllen 	*/
60 	1,				/* dma_attr_granular 	*/
61 	0
62 };
63 
64 static ddi_dma_attr_t sum_dma_attr = {
65 	DMA_ATTR_V0,			/* dma_attr version	*/
66 	0x0000000000000000ull,		/* dma_attr_addr_lo	*/
67 	0x00000000FFFFFFFFull,		/* dma_attr_addr_hi	*/
68 	0x000000007FFFFFFFull,		/* dma_attr_count_max	*/
69 	0x0000000000000010ull,		/* dma_attr_align	*/
70 	0x00000FFF,			/* dma_attr_burstsizes	*/
71 	0x00000001,			/* dma_attr_minxfer	*/
72 	0x000000000000FFFFull,		/* dma_attr_maxxfer	*/
73 	0x00000000FFFFFFFFull,		/* dma_attr_seg		*/
74 	1,				/* dma_attr_sgllen 	*/
75 	0x00000001,			/* dma_attr_granular 	*/
76 	0
77 };
78 
79 static ddi_dma_attr_t sum_tx_dma_attr = {
80 	DMA_ATTR_V0,			/* dma_attr version	*/
81 	0x0000000000000000ull,		/* dma_attr_addr_lo	*/
82 	0x00000000FFFFFFFFull,		/* dma_attr_addr_hi	*/
83 	0x0000000000003FFFull,		/* dma_attr_count_max	*/
84 	0x0000000000000010ull,		/* dma_attr_align	*/
85 	0x00000FFF,			/* dma_attr_burstsizes	*/
86 	0x00000001,			/* dma_attr_minxfer	*/
87 	0x0000000000003FFFull,		/* dma_attr_maxxfer	*/
88 	0x00000000FFFFFFFFull,		/* dma_attr_seg		*/
89 	NGE_MAX_COOKIES,		/* dma_attr_sgllen 	*/
90 	1,				/* dma_attr_granular 	*/
91 	0
92 };
93 
94 /*
95  * DMA access attributes for data.
96  */
97 ddi_device_acc_attr_t nge_data_accattr = {
98 	DDI_DEVICE_ATTR_V0,
99 	DDI_STRUCTURE_LE_ACC,
100 	DDI_STRICTORDER_ACC,
101 	DDI_DEFAULT_ACC
102 };
103 
104 /*
105  * DMA access attributes for descriptors.
106  */
107 static ddi_device_acc_attr_t nge_desc_accattr = {
108 	DDI_DEVICE_ATTR_V0,
109 	DDI_STRUCTURE_LE_ACC,
110 	DDI_STRICTORDER_ACC,
111 	DDI_DEFAULT_ACC
112 };
113 
114 /*
115  * PIO access attributes for registers
116  */
117 static ddi_device_acc_attr_t nge_reg_accattr = {
118 	DDI_DEVICE_ATTR_V0,
119 	DDI_STRUCTURE_LE_ACC,
120 	DDI_STRICTORDER_ACC,
121 	DDI_DEFAULT_ACC
122 };
123 
124 /*
125  * NIC DESC MODE 2
126  */
127 
128 static const nge_desc_attr_t nge_sum_desc = {
129 
130 	sizeof (sum_rx_bd),
131 	sizeof (sum_tx_bd),
132 	&sum_dma_attr,
133 	&sum_tx_dma_attr,
134 	nge_sum_rxd_fill,
135 	nge_sum_rxd_check,
136 	nge_sum_txd_fill,
137 	nge_sum_txd_check,
138 };
139 
140 /*
141  * NIC DESC MODE 3
142  */
143 
144 static const nge_desc_attr_t nge_hot_desc = {
145 
146 	sizeof (hot_rx_bd),
147 	sizeof (hot_tx_bd),
148 	&hot_dma_attr,
149 	&hot_tx_dma_attr,
150 	nge_hot_rxd_fill,
151 	nge_hot_rxd_check,
152 	nge_hot_txd_fill,
153 	nge_hot_txd_check,
154 };
155 
156 static char nge_ident[] = "nVidia 1Gb Ethernet";
157 static char clsize_propname[] = "cache-line-size";
158 static char latency_propname[] = "latency-timer";
159 static char debug_propname[]	= "nge-debug-flags";
160 static char intr_moderation[] = "intr-moderation";
161 static char rx_data_hw[] = "rx-data-hw";
162 static char rx_prd_lw[] = "rx-prd-lw";
163 static char rx_prd_hw[] = "rx-prd-hw";
164 static char sw_intr_intv[] = "sw-intr-intvl";
165 static char nge_desc_mode[] = "desc-mode";
166 static char default_mtu[] = "default_mtu";
167 static char low_memory_mode[] = "minimal-memory-usage";
168 extern kmutex_t nge_log_mutex[1];
169 
170 static int		nge_m_start(void *);
171 static void		nge_m_stop(void *);
172 static int		nge_m_promisc(void *, boolean_t);
173 static int		nge_m_multicst(void *, boolean_t, const uint8_t *);
174 static int		nge_m_unicst(void *, const uint8_t *);
175 static void		nge_m_ioctl(void *, queue_t *, mblk_t *);
176 static boolean_t	nge_m_getcapab(void *, mac_capab_t, void *);
177 static int		nge_m_setprop(void *, const char *, mac_prop_id_t,
178 	uint_t, const void *);
179 static int		nge_m_getprop(void *, const char *, mac_prop_id_t,
180 	uint_t, uint_t, void *);
181 static int		nge_set_priv_prop(nge_t *, const char *, uint_t,
182 	const void *);
183 static int		nge_get_priv_prop(nge_t *, const char *, uint_t,
184 	uint_t, void *);
185 
186 #define		NGE_M_CALLBACK_FLAGS\
187 		(MC_IOCTL | MC_GETCAPAB | MC_SETPROP | MC_GETPROP)
188 
189 static mac_callbacks_t nge_m_callbacks = {
190 	NGE_M_CALLBACK_FLAGS,
191 	nge_m_stat,
192 	nge_m_start,
193 	nge_m_stop,
194 	nge_m_promisc,
195 	nge_m_multicst,
196 	nge_m_unicst,
197 	nge_m_tx,
198 	NULL,
199 	nge_m_ioctl,
200 	nge_m_getcapab,
201 	NULL,
202 	NULL,
203 	nge_m_setprop,
204 	nge_m_getprop
205 };
206 
207 mac_priv_prop_t nge_priv_props[] = {
208 	{"_tx_bcopy_threshold", MAC_PROP_PERM_RW},
209 	{"_rx_bcopy_threshold", MAC_PROP_PERM_RW},
210 	{"_recv_max_packet", MAC_PROP_PERM_RW},
211 	{"_poll_quiet_time", MAC_PROP_PERM_RW},
212 	{"_poll_busy_time", MAC_PROP_PERM_RW},
213 	{"_rx_intr_hwater", MAC_PROP_PERM_RW},
214 	{"_rx_intr_lwater", MAC_PROP_PERM_RW},
215 	{"_adv_pause_cap", MAC_PROP_PERM_RW},
216 	{"_adv_asym_pause_cap", MAC_PROP_PERM_RW},
217 	{"_tx_n_intr", MAC_PROP_PERM_RW}
218 };
219 
220 #define	NGE_MAX_PRIV_PROPS \
221 	(sizeof (nge_priv_props)/sizeof (mac_priv_prop_t))
222 
223 static int nge_add_intrs(nge_t *, int);
224 static void nge_rem_intrs(nge_t *);
225 static int nge_register_intrs_and_init_locks(nge_t *);
226 
227 /*
228  * NGE MSI tunable:
229  */
230 boolean_t nge_enable_msi = B_FALSE;
231 
232 static enum ioc_reply
233 nge_set_loop_mode(nge_t *ngep, uint32_t mode)
234 {
235 	/*
236 	 * If the mode isn't being changed, there's nothing to do ...
237 	 */
238 	if (mode == ngep->param_loop_mode)
239 		return (IOC_ACK);
240 
241 	/*
242 	 * Validate the requested mode and prepare a suitable message
243 	 * to explain the link down/up cycle that the change will
244 	 * probably induce ...
245 	 */
246 	switch (mode) {
247 	default:
248 		return (IOC_INVAL);
249 
250 	case NGE_LOOP_NONE:
251 	case NGE_LOOP_EXTERNAL_100:
252 	case NGE_LOOP_EXTERNAL_10:
253 	case NGE_LOOP_INTERNAL_PHY:
254 		break;
255 	}
256 
257 	/*
258 	 * All OK; tell the caller to reprogram
259 	 * the PHY and/or MAC for the new mode ...
260 	 */
261 	ngep->param_loop_mode = mode;
262 	return (IOC_RESTART_ACK);
263 }
264 
265 #undef	NGE_DBG
266 #define	NGE_DBG		NGE_DBG_INIT
267 
268 /*
269  * Utility routine to carve a slice off a chunk of allocated memory,
270  * updating the chunk descriptor accordingly.  The size of the slice
271  * is given by the product of the <qty> and <size> parameters.
272  */
273 void
274 nge_slice_chunk(dma_area_t *slice, dma_area_t *chunk,
275     uint32_t qty, uint32_t size)
276 {
277 	size_t totsize;
278 
279 	totsize = qty*size;
280 	ASSERT(size > 0);
281 	ASSERT(totsize <= chunk->alength);
282 
283 	*slice = *chunk;
284 	slice->nslots = qty;
285 	slice->size = size;
286 	slice->alength = totsize;
287 
288 	chunk->mem_va = (caddr_t)chunk->mem_va + totsize;
289 	chunk->alength -= totsize;
290 	chunk->offset += totsize;
291 	chunk->cookie.dmac_laddress += totsize;
292 	chunk->cookie.dmac_size -= totsize;
293 }
294 
295 /*
296  * Allocate an area of memory and a DMA handle for accessing it
297  */
298 int
299 nge_alloc_dma_mem(nge_t *ngep, size_t memsize, ddi_device_acc_attr_t *attr_p,
300     uint_t dma_flags, dma_area_t *dma_p)
301 {
302 	int err;
303 	caddr_t va;
304 
305 	NGE_TRACE(("nge_alloc_dma_mem($%p, %ld, $%p, 0x%x, $%p)",
306 	    (void *)ngep, memsize, attr_p, dma_flags, dma_p));
307 	/*
308 	 * Allocate handle
309 	 */
310 	err = ddi_dma_alloc_handle(ngep->devinfo, ngep->desc_attr.dma_attr,
311 	    DDI_DMA_DONTWAIT, NULL, &dma_p->dma_hdl);
312 	if (err != DDI_SUCCESS)
313 		goto fail;
314 
315 	/*
316 	 * Allocate memory
317 	 */
318 	err = ddi_dma_mem_alloc(dma_p->dma_hdl, memsize, attr_p,
319 	    dma_flags & (DDI_DMA_CONSISTENT | DDI_DMA_STREAMING),
320 	    DDI_DMA_DONTWAIT, NULL, &va, &dma_p->alength, &dma_p->acc_hdl);
321 	if (err != DDI_SUCCESS)
322 		goto fail;
323 
324 	/*
325 	 * Bind the two together
326 	 */
327 	dma_p->mem_va = va;
328 	err = ddi_dma_addr_bind_handle(dma_p->dma_hdl, NULL,
329 	    va, dma_p->alength, dma_flags, DDI_DMA_DONTWAIT, NULL,
330 	    &dma_p->cookie, &dma_p->ncookies);
331 
332 	if (err != DDI_DMA_MAPPED || dma_p->ncookies != 1)
333 		goto fail;
334 
335 	dma_p->nslots = ~0U;
336 	dma_p->size = ~0U;
337 	dma_p->offset = 0;
338 
339 	return (DDI_SUCCESS);
340 
341 fail:
342 	nge_free_dma_mem(dma_p);
343 	NGE_DEBUG(("nge_alloc_dma_mem: fail to alloc dma memory!"));
344 
345 	return (DDI_FAILURE);
346 }
347 
348 /*
349  * Free one allocated area of DMAable memory
350  */
351 void
352 nge_free_dma_mem(dma_area_t *dma_p)
353 {
354 	if (dma_p->dma_hdl != NULL) {
355 		if (dma_p->ncookies) {
356 			(void) ddi_dma_unbind_handle(dma_p->dma_hdl);
357 			dma_p->ncookies = 0;
358 		}
359 	}
360 	if (dma_p->acc_hdl != NULL) {
361 		ddi_dma_mem_free(&dma_p->acc_hdl);
362 		dma_p->acc_hdl = NULL;
363 	}
364 	if (dma_p->dma_hdl != NULL) {
365 		ddi_dma_free_handle(&dma_p->dma_hdl);
366 		dma_p->dma_hdl = NULL;
367 	}
368 }
369 
370 #define	ALLOC_TX_BUF	0x1
371 #define	ALLOC_TX_DESC	0x2
372 #define	ALLOC_RX_DESC	0x4
373 
374 int
375 nge_alloc_bufs(nge_t *ngep)
376 {
377 	int err;
378 	int split;
379 	int progress;
380 	size_t txbuffsize;
381 	size_t rxdescsize;
382 	size_t txdescsize;
383 
384 	txbuffsize = ngep->tx_desc * ngep->buf_size;
385 	rxdescsize = ngep->rx_desc;
386 	txdescsize = ngep->tx_desc;
387 	rxdescsize *= ngep->desc_attr.rxd_size;
388 	txdescsize *= ngep->desc_attr.txd_size;
389 	progress = 0;
390 
391 	NGE_TRACE(("nge_alloc_bufs($%p)", (void *)ngep));
392 	/*
393 	 * Allocate memory & handles for TX buffers
394 	 */
395 	ASSERT((txbuffsize % ngep->nge_split) == 0);
396 	for (split = 0; split < ngep->nge_split; ++split) {
397 		err = nge_alloc_dma_mem(ngep, txbuffsize/ngep->nge_split,
398 		    &nge_data_accattr, DDI_DMA_WRITE | NGE_DMA_MODE,
399 		    &ngep->send->buf[split]);
400 		if (err != DDI_SUCCESS)
401 			goto fail;
402 	}
403 
404 	progress |= ALLOC_TX_BUF;
405 
406 	/*
407 	 * Allocate memory & handles for receive return rings and
408 	 * buffer (producer) descriptor rings
409 	 */
410 	err = nge_alloc_dma_mem(ngep, rxdescsize, &nge_desc_accattr,
411 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT, &ngep->recv->desc);
412 	if (err != DDI_SUCCESS)
413 		goto fail;
414 	progress |= ALLOC_RX_DESC;
415 
416 	/*
417 	 * Allocate memory & handles for TX descriptor rings,
418 	 */
419 	err = nge_alloc_dma_mem(ngep, txdescsize, &nge_desc_accattr,
420 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT, &ngep->send->desc);
421 	if (err != DDI_SUCCESS)
422 		goto fail;
423 	return (DDI_SUCCESS);
424 
425 fail:
426 	if (progress & ALLOC_RX_DESC)
427 		nge_free_dma_mem(&ngep->recv->desc);
428 	if (progress & ALLOC_TX_BUF) {
429 		for (split = 0; split < ngep->nge_split; ++split)
430 			nge_free_dma_mem(&ngep->send->buf[split]);
431 	}
432 
433 	return (DDI_FAILURE);
434 }
435 
436 /*
437  * This routine frees the transmit and receive buffers and descriptors.
438  * Make sure the chip is stopped before calling it!
439  */
440 void
441 nge_free_bufs(nge_t *ngep)
442 {
443 	int split;
444 
445 	NGE_TRACE(("nge_free_bufs($%p)", (void *)ngep));
446 
447 	nge_free_dma_mem(&ngep->recv->desc);
448 	nge_free_dma_mem(&ngep->send->desc);
449 
450 	for (split = 0; split < ngep->nge_split; ++split)
451 		nge_free_dma_mem(&ngep->send->buf[split]);
452 }
453 
454 /*
455  * Clean up initialisation done above before the memory is freed
456  */
457 static void
458 nge_fini_send_ring(nge_t *ngep)
459 {
460 	uint32_t slot;
461 	size_t dmah_num;
462 	send_ring_t *srp;
463 	sw_tx_sbd_t *ssbdp;
464 
465 	srp = ngep->send;
466 	ssbdp = srp->sw_sbds;
467 
468 	NGE_TRACE(("nge_fini_send_ring($%p)", (void *)ngep));
469 
470 	dmah_num = sizeof (srp->dmahndl) / sizeof (srp->dmahndl[0]);
471 
472 	for (slot = 0; slot < dmah_num; ++slot) {
473 		if (srp->dmahndl[slot].hndl) {
474 			(void) ddi_dma_unbind_handle(srp->dmahndl[slot].hndl);
475 			ddi_dma_free_handle(&srp->dmahndl[slot].hndl);
476 			srp->dmahndl[slot].hndl = NULL;
477 			srp->dmahndl[slot].next = NULL;
478 		}
479 	}
480 
481 	srp->dmah_free.head = NULL;
482 	srp->dmah_free.tail = NULL;
483 
484 	kmem_free(ssbdp, srp->desc.nslots*sizeof (*ssbdp));
485 
486 }
487 
488 /*
489  * Initialise the specified Send Ring, using the information in the
490  * <dma_area> descriptors that it contains to set up all the other
491  * fields. This routine should be called only once for each ring.
492  */
493 static int
494 nge_init_send_ring(nge_t *ngep)
495 {
496 	size_t dmah_num;
497 	uint32_t nslots;
498 	uint32_t err;
499 	uint32_t slot;
500 	uint32_t split;
501 	send_ring_t *srp;
502 	sw_tx_sbd_t *ssbdp;
503 	dma_area_t desc;
504 	dma_area_t pbuf;
505 
506 	srp = ngep->send;
507 	srp->desc.nslots = ngep->tx_desc;
508 	nslots = srp->desc.nslots;
509 
510 	NGE_TRACE(("nge_init_send_ring($%p)", (void *)ngep));
511 	/*
512 	 * Other one-off initialisation of per-ring data
513 	 */
514 	srp->ngep = ngep;
515 
516 	/*
517 	 * Allocate the array of s/w Send Buffer Descriptors
518 	 */
519 	ssbdp = kmem_zalloc(nslots*sizeof (*ssbdp), KM_SLEEP);
520 	srp->sw_sbds = ssbdp;
521 
522 	/*
523 	 * Now initialise each array element once and for all
524 	 */
525 	desc = srp->desc;
526 	for (split = 0; split < ngep->nge_split; ++split) {
527 		pbuf = srp->buf[split];
528 		for (slot = 0; slot < nslots/ngep->nge_split; ++ssbdp, ++slot) {
529 			nge_slice_chunk(&ssbdp->desc, &desc, 1,
530 			    ngep->desc_attr.txd_size);
531 			nge_slice_chunk(&ssbdp->pbuf, &pbuf, 1,
532 			    ngep->buf_size);
533 		}
534 		ASSERT(pbuf.alength == 0);
535 	}
536 	ASSERT(desc.alength == 0);
537 
538 	dmah_num = sizeof (srp->dmahndl) / sizeof (srp->dmahndl[0]);
539 
540 	/* preallocate dma handles for tx buffer */
541 	for (slot = 0; slot < dmah_num; ++slot) {
542 
543 		err = ddi_dma_alloc_handle(ngep->devinfo,
544 		    ngep->desc_attr.tx_dma_attr, DDI_DMA_DONTWAIT,
545 		    NULL, &srp->dmahndl[slot].hndl);
546 
547 		if (err != DDI_SUCCESS) {
548 			nge_fini_send_ring(ngep);
549 			nge_error(ngep,
550 			    "nge_init_send_ring: alloc dma handle fails");
551 			return (DDI_FAILURE);
552 		}
553 		srp->dmahndl[slot].next = srp->dmahndl + slot + 1;
554 	}
555 
556 	srp->dmah_free.head = srp->dmahndl;
557 	srp->dmah_free.tail = srp->dmahndl + dmah_num - 1;
558 	srp->dmah_free.tail->next = NULL;
559 
560 	return (DDI_SUCCESS);
561 }
562 
563 /*
564  * Intialize the tx recycle pointer and tx sending pointer of tx ring
565  * and set the type of tx's data descriptor by default.
566  */
567 static void
568 nge_reinit_send_ring(nge_t *ngep)
569 {
570 	size_t dmah_num;
571 	uint32_t slot;
572 	send_ring_t *srp;
573 	sw_tx_sbd_t *ssbdp;
574 
575 	srp = ngep->send;
576 
577 	/*
578 	 * Reinitialise control variables ...
579 	 */
580 
581 	srp->tx_hwmark = NGE_DESC_MIN;
582 	srp->tx_lwmark = NGE_DESC_MIN;
583 
584 	srp->tx_next = 0;
585 	srp->tx_free = srp->desc.nslots;
586 	srp->tc_next = 0;
587 
588 	dmah_num = sizeof (srp->dmahndl) / sizeof (srp->dmahndl[0]);
589 
590 	for (slot = 0; slot - dmah_num != 0; ++slot)
591 		srp->dmahndl[slot].next = srp->dmahndl + slot + 1;
592 
593 	srp->dmah_free.head = srp->dmahndl;
594 	srp->dmah_free.tail = srp->dmahndl + dmah_num - 1;
595 	srp->dmah_free.tail->next = NULL;
596 
597 	/*
598 	 * Zero and sync all the h/w Send Buffer Descriptors
599 	 */
600 	for (slot = 0; slot < srp->desc.nslots; ++slot) {
601 		ssbdp = &srp->sw_sbds[slot];
602 		ssbdp->flags = HOST_OWN;
603 	}
604 
605 	DMA_ZERO(srp->desc);
606 	DMA_SYNC(srp->desc, DDI_DMA_SYNC_FORDEV);
607 }
608 
609 /*
610  * Initialize the slot number of rx's ring
611  */
612 static void
613 nge_init_recv_ring(nge_t *ngep)
614 {
615 	recv_ring_t *rrp;
616 
617 	rrp = ngep->recv;
618 	rrp->desc.nslots = ngep->rx_desc;
619 	rrp->ngep = ngep;
620 }
621 
622 /*
623  * Intialize the rx recycle pointer and rx sending pointer of rx ring
624  */
625 static void
626 nge_reinit_recv_ring(nge_t *ngep)
627 {
628 	recv_ring_t *rrp;
629 
630 	rrp = ngep->recv;
631 
632 	/*
633 	 * Reinitialise control variables ...
634 	 */
635 	rrp->prod_index = 0;
636 	/*
637 	 * Zero and sync all the h/w Send Buffer Descriptors
638 	 */
639 	DMA_ZERO(rrp->desc);
640 	DMA_SYNC(rrp->desc, DDI_DMA_SYNC_FORDEV);
641 }
642 
643 /*
644  * Clean up initialisation done above before the memory is freed
645  */
646 static void
647 nge_fini_buff_ring(nge_t *ngep)
648 {
649 	uint32_t i;
650 	buff_ring_t *brp;
651 	dma_area_t *bufp;
652 	sw_rx_sbd_t *bsbdp;
653 
654 	brp = ngep->buff;
655 	bsbdp = brp->sw_rbds;
656 
657 	NGE_DEBUG(("nge_fini_buff_ring($%p)", (void *)ngep));
658 
659 	mutex_enter(brp->recycle_lock);
660 	brp->buf_sign++;
661 	mutex_exit(brp->recycle_lock);
662 	for (i = 0; i < ngep->rx_desc; i++, ++bsbdp) {
663 		if (bsbdp->bufp) {
664 			if (bsbdp->bufp->mp)
665 				freemsg(bsbdp->bufp->mp);
666 			nge_free_dma_mem(bsbdp->bufp);
667 			kmem_free(bsbdp->bufp, sizeof (dma_area_t));
668 			bsbdp->bufp = NULL;
669 		}
670 	}
671 	while (brp->free_list != NULL) {
672 		bufp = brp->free_list;
673 		brp->free_list = bufp->next;
674 		bufp->next = NULL;
675 		if (bufp->mp)
676 			freemsg(bufp->mp);
677 		nge_free_dma_mem(bufp);
678 		kmem_free(bufp, sizeof (dma_area_t));
679 	}
680 	while (brp->recycle_list != NULL) {
681 		bufp = brp->recycle_list;
682 		brp->recycle_list = bufp->next;
683 		bufp->next = NULL;
684 		if (bufp->mp)
685 			freemsg(bufp->mp);
686 		nge_free_dma_mem(bufp);
687 		kmem_free(bufp, sizeof (dma_area_t));
688 	}
689 
690 
691 	kmem_free(brp->sw_rbds, (ngep->rx_desc * sizeof (*bsbdp)));
692 	brp->sw_rbds = NULL;
693 }
694 
695 /*
696  * Intialize the Rx's data ring and free ring
697  */
698 static int
699 nge_init_buff_ring(nge_t *ngep)
700 {
701 	uint32_t err;
702 	uint32_t slot;
703 	uint32_t nslots_buff;
704 	uint32_t nslots_recv;
705 	buff_ring_t *brp;
706 	recv_ring_t *rrp;
707 	dma_area_t desc;
708 	dma_area_t *bufp;
709 	sw_rx_sbd_t *bsbdp;
710 
711 	rrp = ngep->recv;
712 	brp = ngep->buff;
713 	brp->nslots = ngep->rx_buf;
714 	brp->rx_bcopy = B_FALSE;
715 	nslots_recv = rrp->desc.nslots;
716 	nslots_buff = brp->nslots;
717 	brp->ngep = ngep;
718 
719 	NGE_TRACE(("nge_init_buff_ring($%p)", (void *)ngep));
720 
721 	/*
722 	 * Allocate the array of s/w Recv Buffer Descriptors
723 	 */
724 	bsbdp = kmem_zalloc(nslots_recv *sizeof (*bsbdp), KM_SLEEP);
725 	brp->sw_rbds = bsbdp;
726 	brp->free_list = NULL;
727 	brp->recycle_list = NULL;
728 	for (slot = 0; slot < nslots_buff; ++slot) {
729 		bufp = kmem_zalloc(sizeof (dma_area_t), KM_SLEEP);
730 		err = nge_alloc_dma_mem(ngep, (ngep->buf_size
731 		    + NGE_HEADROOM),
732 		    &nge_data_accattr, DDI_DMA_READ | NGE_DMA_MODE, bufp);
733 		if (err != DDI_SUCCESS) {
734 			kmem_free(bufp, sizeof (dma_area_t));
735 			return (DDI_FAILURE);
736 		}
737 
738 		bufp->alength -= NGE_HEADROOM;
739 		bufp->offset += NGE_HEADROOM;
740 		bufp->private = (caddr_t)ngep;
741 		bufp->rx_recycle.free_func = nge_recv_recycle;
742 		bufp->rx_recycle.free_arg = (caddr_t)bufp;
743 		bufp->signature = brp->buf_sign;
744 		bufp->rx_delivered = B_FALSE;
745 		bufp->mp = desballoc(DMA_VPTR(*bufp),
746 		    ngep->buf_size + NGE_HEADROOM,
747 		    0, &bufp->rx_recycle);
748 
749 		if (bufp->mp == NULL) {
750 			return (DDI_FAILURE);
751 		}
752 		bufp->next = brp->free_list;
753 		brp->free_list = bufp;
754 	}
755 
756 	/*
757 	 * Now initialise each array element once and for all
758 	 */
759 	desc = rrp->desc;
760 	for (slot = 0; slot < nslots_recv; ++slot, ++bsbdp) {
761 		nge_slice_chunk(&bsbdp->desc, &desc, 1,
762 		    ngep->desc_attr.rxd_size);
763 		bufp = brp->free_list;
764 		brp->free_list = bufp->next;
765 		bsbdp->bufp = bufp;
766 		bsbdp->flags = CONTROLER_OWN;
767 		bufp->next = NULL;
768 	}
769 
770 	ASSERT(desc.alength == 0);
771 	return (DDI_SUCCESS);
772 }
773 
774 /*
775  * Fill the host address of data in rx' descriptor
776  * and initialize free pointers of rx free ring
777  */
778 static int
779 nge_reinit_buff_ring(nge_t *ngep)
780 {
781 	uint32_t slot;
782 	uint32_t nslots_recv;
783 	buff_ring_t *brp;
784 	recv_ring_t *rrp;
785 	sw_rx_sbd_t *bsbdp;
786 	void *hw_bd_p;
787 
788 	brp = ngep->buff;
789 	rrp = ngep->recv;
790 	bsbdp = brp->sw_rbds;
791 	nslots_recv = rrp->desc.nslots;
792 	for (slot = 0; slot < nslots_recv; ++bsbdp, ++slot) {
793 		hw_bd_p = DMA_VPTR(bsbdp->desc);
794 	/*
795 	 * There is a scenario: When the traffic of small tcp
796 	 * packet is heavy, suspending the tcp traffic will
797 	 * cause the preallocated buffers for rx not to be
798 	 * released in time by tcp taffic and cause rx's buffer
799 	 * pointers not to be refilled in time.
800 	 *
801 	 * At this point, if we reinitialize the driver, the bufp
802 	 * pointer for rx's traffic will be NULL.
803 	 * So the result of the reinitializion fails.
804 	 */
805 		if (bsbdp->bufp == NULL)
806 			return (DDI_FAILURE);
807 
808 		ngep->desc_attr.rxd_fill(hw_bd_p, &bsbdp->bufp->cookie,
809 		    bsbdp->bufp->alength);
810 	}
811 	return (DDI_SUCCESS);
812 }
813 
814 static void
815 nge_init_ring_param_lock(nge_t *ngep)
816 {
817 	buff_ring_t *brp;
818 	send_ring_t *srp;
819 
820 	srp = ngep->send;
821 	brp = ngep->buff;
822 
823 	/* Init the locks for send ring */
824 	mutex_init(srp->tx_lock, NULL, MUTEX_DRIVER,
825 	    DDI_INTR_PRI(ngep->intr_pri));
826 	mutex_init(srp->tc_lock, NULL, MUTEX_DRIVER,
827 	    DDI_INTR_PRI(ngep->intr_pri));
828 	mutex_init(&srp->dmah_lock, NULL, MUTEX_DRIVER,
829 	    DDI_INTR_PRI(ngep->intr_pri));
830 
831 	/* Init parameters of buffer ring */
832 	brp->free_list = NULL;
833 	brp->recycle_list = NULL;
834 	brp->rx_hold = 0;
835 	brp->buf_sign = 0;
836 
837 	/* Init recycle list lock */
838 	mutex_init(brp->recycle_lock, NULL, MUTEX_DRIVER,
839 	    DDI_INTR_PRI(ngep->intr_pri));
840 }
841 
842 int
843 nge_init_rings(nge_t *ngep)
844 {
845 	uint32_t err;
846 
847 	err = nge_init_send_ring(ngep);
848 	if (err != DDI_SUCCESS) {
849 		return (err);
850 	}
851 	nge_init_recv_ring(ngep);
852 
853 	err = nge_init_buff_ring(ngep);
854 	if (err != DDI_SUCCESS) {
855 		nge_fini_send_ring(ngep);
856 		return (DDI_FAILURE);
857 	}
858 
859 	return (err);
860 }
861 
862 static int
863 nge_reinit_ring(nge_t *ngep)
864 {
865 	int err;
866 
867 	nge_reinit_recv_ring(ngep);
868 	nge_reinit_send_ring(ngep);
869 	err = nge_reinit_buff_ring(ngep);
870 	return (err);
871 }
872 
873 
874 void
875 nge_fini_rings(nge_t *ngep)
876 {
877 	/*
878 	 * For receive ring, nothing need to be finished.
879 	 * So only finish buffer ring and send ring here.
880 	 */
881 	nge_fini_buff_ring(ngep);
882 	nge_fini_send_ring(ngep);
883 }
884 
885 /*
886  * Loopback ioctl code
887  */
888 
889 static lb_property_t loopmodes[] = {
890 	{ normal,	"normal",	NGE_LOOP_NONE		},
891 	{ external,	"100Mbps",	NGE_LOOP_EXTERNAL_100	},
892 	{ external,	"10Mbps",	NGE_LOOP_EXTERNAL_10	},
893 	{ internal,	"PHY",		NGE_LOOP_INTERNAL_PHY	},
894 };
895 
896 enum ioc_reply
897 nge_loop_ioctl(nge_t *ngep, mblk_t *mp, struct iocblk *iocp)
898 {
899 	int cmd;
900 	uint32_t *lbmp;
901 	lb_info_sz_t *lbsp;
902 	lb_property_t *lbpp;
903 
904 	/*
905 	 * Validate format of ioctl
906 	 */
907 	if (mp->b_cont == NULL)
908 		return (IOC_INVAL);
909 
910 	cmd = iocp->ioc_cmd;
911 
912 	switch (cmd) {
913 	default:
914 		return (IOC_INVAL);
915 
916 	case LB_GET_INFO_SIZE:
917 		if (iocp->ioc_count != sizeof (lb_info_sz_t))
918 			return (IOC_INVAL);
919 		lbsp = (lb_info_sz_t *)mp->b_cont->b_rptr;
920 		*lbsp = sizeof (loopmodes);
921 		return (IOC_REPLY);
922 
923 	case LB_GET_INFO:
924 		if (iocp->ioc_count != sizeof (loopmodes))
925 			return (IOC_INVAL);
926 		lbpp = (lb_property_t *)mp->b_cont->b_rptr;
927 		bcopy(loopmodes, lbpp, sizeof (loopmodes));
928 		return (IOC_REPLY);
929 
930 	case LB_GET_MODE:
931 		if (iocp->ioc_count != sizeof (uint32_t))
932 			return (IOC_INVAL);
933 		lbmp = (uint32_t *)mp->b_cont->b_rptr;
934 		*lbmp = ngep->param_loop_mode;
935 		return (IOC_REPLY);
936 
937 	case LB_SET_MODE:
938 		if (iocp->ioc_count != sizeof (uint32_t))
939 			return (IOC_INVAL);
940 		lbmp = (uint32_t *)mp->b_cont->b_rptr;
941 		return (nge_set_loop_mode(ngep, *lbmp));
942 	}
943 }
944 
945 #undef	NGE_DBG
946 #define	NGE_DBG	NGE_DBG_NEMO
947 
948 
949 static void
950 nge_check_desc_prop(nge_t *ngep)
951 {
952 	if (ngep->desc_mode != DESC_HOT && ngep->desc_mode != DESC_OFFLOAD)
953 		ngep->desc_mode = DESC_HOT;
954 
955 	if (ngep->desc_mode == DESC_OFFLOAD)	{
956 
957 		ngep->desc_attr = nge_sum_desc;
958 
959 	}	else if (ngep->desc_mode == DESC_HOT)	{
960 
961 		ngep->desc_attr = nge_hot_desc;
962 	}
963 }
964 
965 /*
966  * nge_get_props -- get the parameters to tune the driver
967  */
968 static void
969 nge_get_props(nge_t *ngep)
970 {
971 	chip_info_t *infop;
972 	dev_info_t *devinfo;
973 	nge_dev_spec_param_t *dev_param_p;
974 
975 	devinfo = ngep->devinfo;
976 	infop = (chip_info_t *)&ngep->chipinfo;
977 	dev_param_p = &ngep->dev_spec_param;
978 
979 	infop->clsize = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
980 	    DDI_PROP_DONTPASS, clsize_propname, 32);
981 
982 	infop->latency = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
983 	    DDI_PROP_DONTPASS, latency_propname, 64);
984 	ngep->intr_moderation = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
985 	    DDI_PROP_DONTPASS, intr_moderation, NGE_SET);
986 	ngep->rx_datahwm = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
987 	    DDI_PROP_DONTPASS, rx_data_hw, 0x20);
988 	ngep->rx_prdlwm = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
989 	    DDI_PROP_DONTPASS, rx_prd_lw, 0x4);
990 	ngep->rx_prdhwm = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
991 	    DDI_PROP_DONTPASS, rx_prd_hw, 0xc);
992 
993 	ngep->sw_intr_intv = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
994 	    DDI_PROP_DONTPASS, sw_intr_intv, SWTR_ITC);
995 	ngep->debug = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
996 	    DDI_PROP_DONTPASS, debug_propname, NGE_DBG_CHIP);
997 	ngep->desc_mode = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
998 	    DDI_PROP_DONTPASS, nge_desc_mode, dev_param_p->desc_type);
999 	ngep->lowmem_mode = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
1000 	    DDI_PROP_DONTPASS, low_memory_mode, 0);
1001 
1002 	if (dev_param_p->jumbo) {
1003 		ngep->default_mtu = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
1004 		    DDI_PROP_DONTPASS, default_mtu, ETHERMTU);
1005 	} else
1006 		ngep->default_mtu = ETHERMTU;
1007 
1008 	if (ngep->default_mtu > ETHERMTU &&
1009 	    ngep->default_mtu <= NGE_MTU_2500) {
1010 		ngep->buf_size = NGE_JB2500_BUFSZ;
1011 		ngep->tx_desc = NGE_SEND_JB2500_SLOTS_DESC;
1012 		ngep->rx_desc = NGE_RECV_JB2500_SLOTS_DESC;
1013 		ngep->rx_buf = NGE_RECV_JB2500_SLOTS_DESC * 2;
1014 		ngep->nge_split = NGE_SPLIT_256;
1015 	} else if (ngep->default_mtu > NGE_MTU_2500 &&
1016 	    ngep->default_mtu <= NGE_MTU_4500) {
1017 		ngep->buf_size = NGE_JB4500_BUFSZ;
1018 		ngep->tx_desc = NGE_SEND_JB4500_SLOTS_DESC;
1019 		ngep->rx_desc = NGE_RECV_JB4500_SLOTS_DESC;
1020 		ngep->rx_buf = NGE_RECV_JB4500_SLOTS_DESC * 2;
1021 		ngep->nge_split = NGE_SPLIT_256;
1022 	} else if (ngep->default_mtu > NGE_MTU_4500 &&
1023 	    ngep->default_mtu <= NGE_MAX_MTU) {
1024 		ngep->buf_size = NGE_JB9000_BUFSZ;
1025 		ngep->tx_desc = NGE_SEND_JB9000_SLOTS_DESC;
1026 		ngep->rx_desc = NGE_RECV_JB9000_SLOTS_DESC;
1027 		ngep->rx_buf = NGE_RECV_JB9000_SLOTS_DESC * 2;
1028 		ngep->nge_split = NGE_SPLIT_256;
1029 	} else if (ngep->default_mtu > NGE_MAX_MTU) {
1030 		ngep->default_mtu = NGE_MAX_MTU;
1031 		ngep->buf_size = NGE_JB9000_BUFSZ;
1032 		ngep->tx_desc = NGE_SEND_JB9000_SLOTS_DESC;
1033 		ngep->rx_desc = NGE_RECV_JB9000_SLOTS_DESC;
1034 		ngep->rx_buf = NGE_RECV_JB9000_SLOTS_DESC * 2;
1035 		ngep->nge_split = NGE_SPLIT_256;
1036 	} else if (ngep->lowmem_mode != 0) {
1037 		ngep->default_mtu = ETHERMTU;
1038 		ngep->buf_size = NGE_STD_BUFSZ;
1039 		ngep->tx_desc = NGE_SEND_LOWMEM_SLOTS_DESC;
1040 		ngep->rx_desc = NGE_RECV_LOWMEM_SLOTS_DESC;
1041 		ngep->rx_buf = NGE_RECV_LOWMEM_SLOTS_DESC * 2;
1042 		ngep->nge_split = NGE_SPLIT_32;
1043 	} else {
1044 		ngep->default_mtu = ETHERMTU;
1045 		ngep->buf_size = NGE_STD_BUFSZ;
1046 		ngep->tx_desc = dev_param_p->tx_desc_num;
1047 		ngep->rx_desc = dev_param_p->rx_desc_num;
1048 		ngep->rx_buf = dev_param_p->rx_desc_num * 2;
1049 		ngep->nge_split = dev_param_p->nge_split;
1050 	}
1051 
1052 	nge_check_desc_prop(ngep);
1053 }
1054 
1055 
1056 static int
1057 nge_reset_dev(nge_t *ngep)
1058 {
1059 	int err;
1060 	nge_mul_addr1 maddr1;
1061 	nge_sw_statistics_t *sw_stp;
1062 	sw_stp = &ngep->statistics.sw_statistics;
1063 	send_ring_t *srp = ngep->send;
1064 
1065 	ASSERT(mutex_owned(ngep->genlock));
1066 	mutex_enter(srp->tc_lock);
1067 	mutex_enter(srp->tx_lock);
1068 
1069 	nge_tx_recycle_all(ngep);
1070 	err = nge_reinit_ring(ngep);
1071 	if (err == DDI_FAILURE) {
1072 		mutex_exit(srp->tx_lock);
1073 		mutex_exit(srp->tc_lock);
1074 		return (err);
1075 	}
1076 	err = nge_chip_reset(ngep);
1077 	/*
1078 	 * Clear the Multicast mac address table
1079 	 */
1080 	nge_reg_put32(ngep, NGE_MUL_ADDR0, 0);
1081 	maddr1.addr_val = nge_reg_get32(ngep, NGE_MUL_ADDR1);
1082 	maddr1.addr_bits.addr = 0;
1083 	nge_reg_put32(ngep, NGE_MUL_ADDR1, maddr1.addr_val);
1084 
1085 	mutex_exit(srp->tx_lock);
1086 	mutex_exit(srp->tc_lock);
1087 	if (err == DDI_FAILURE)
1088 		return (err);
1089 	ngep->watchdog = 0;
1090 	ngep->resched_needed = B_FALSE;
1091 	ngep->promisc = B_FALSE;
1092 	ngep->param_loop_mode = NGE_LOOP_NONE;
1093 	ngep->factotum_flag = 0;
1094 	ngep->resched_needed = 0;
1095 	ngep->nge_mac_state = NGE_MAC_RESET;
1096 	ngep->max_sdu = ngep->default_mtu + ETHER_HEAD_LEN + ETHERFCSL;
1097 	ngep->max_sdu += VTAG_SIZE;
1098 	ngep->rx_def = 0x16;
1099 
1100 	/* Clear the software statistics */
1101 	sw_stp->recv_count = 0;
1102 	sw_stp->xmit_count = 0;
1103 	sw_stp->rbytes = 0;
1104 	sw_stp->obytes = 0;
1105 
1106 	return (DDI_SUCCESS);
1107 }
1108 
1109 static void
1110 nge_m_stop(void *arg)
1111 {
1112 	nge_t *ngep = arg;		/* private device info	*/
1113 
1114 	NGE_TRACE(("nge_m_stop($%p)", arg));
1115 
1116 	/*
1117 	 * Just stop processing, then record new MAC state
1118 	 */
1119 	mutex_enter(ngep->genlock);
1120 	/* If suspended, the adapter is already stopped, just return. */
1121 	if (ngep->suspended) {
1122 		ASSERT(ngep->nge_mac_state == NGE_MAC_STOPPED);
1123 		mutex_exit(ngep->genlock);
1124 		return;
1125 	}
1126 	rw_enter(ngep->rwlock, RW_WRITER);
1127 
1128 	(void) nge_chip_stop(ngep, B_FALSE);
1129 	ngep->nge_mac_state = NGE_MAC_STOPPED;
1130 
1131 	/* Recycle all the TX BD */
1132 	nge_tx_recycle_all(ngep);
1133 	nge_fini_rings(ngep);
1134 	nge_free_bufs(ngep);
1135 
1136 	NGE_DEBUG(("nge_m_stop($%p) done", arg));
1137 
1138 	rw_exit(ngep->rwlock);
1139 	mutex_exit(ngep->genlock);
1140 }
1141 
1142 static int
1143 nge_m_start(void *arg)
1144 {
1145 	int err;
1146 	nge_t *ngep = arg;
1147 
1148 	NGE_TRACE(("nge_m_start($%p)", arg));
1149 
1150 	/*
1151 	 * Start processing and record new MAC state
1152 	 */
1153 	mutex_enter(ngep->genlock);
1154 	/*
1155 	 * If suspended, don't start, as the resume processing
1156 	 * will recall this function with the suspended flag off.
1157 	 */
1158 	if (ngep->suspended) {
1159 		mutex_exit(ngep->genlock);
1160 		return (EIO);
1161 	}
1162 	rw_enter(ngep->rwlock, RW_WRITER);
1163 	err = nge_alloc_bufs(ngep);
1164 	if (err != DDI_SUCCESS) {
1165 		nge_problem(ngep, "nge_m_start: DMA buffer allocation failed");
1166 		goto finish;
1167 	}
1168 	err = nge_init_rings(ngep);
1169 	if (err != DDI_SUCCESS) {
1170 		nge_free_bufs(ngep);
1171 		nge_problem(ngep, "nge_init_rings() failed,err=%x", err);
1172 		goto finish;
1173 	}
1174 	err = nge_restart(ngep);
1175 
1176 	NGE_DEBUG(("nge_m_start($%p) done", arg));
1177 finish:
1178 	rw_exit(ngep->rwlock);
1179 	mutex_exit(ngep->genlock);
1180 
1181 	return (err == DDI_SUCCESS ? 0 : EIO);
1182 }
1183 
1184 static int
1185 nge_m_unicst(void *arg, const uint8_t *macaddr)
1186 {
1187 	nge_t *ngep = arg;
1188 
1189 	NGE_TRACE(("nge_m_unicst($%p)", arg));
1190 	/*
1191 	 * Remember the new current address in the driver state
1192 	 * Sync the chip's idea of the address too ...
1193 	 */
1194 	mutex_enter(ngep->genlock);
1195 
1196 	ethaddr_copy(macaddr, ngep->cur_uni_addr.addr);
1197 	ngep->cur_uni_addr.set = 1;
1198 
1199 	/*
1200 	 * If we are suspended, we want to quit now, and not update
1201 	 * the chip.  Doing so might put it in a bad state, but the
1202 	 * resume will get the unicast address installed.
1203 	 */
1204 	if (ngep->suspended) {
1205 		mutex_exit(ngep->genlock);
1206 		return (DDI_SUCCESS);
1207 	}
1208 	nge_chip_sync(ngep);
1209 
1210 	NGE_DEBUG(("nge_m_unicst($%p) done", arg));
1211 	mutex_exit(ngep->genlock);
1212 
1213 	return (0);
1214 }
1215 
1216 static int
1217 nge_m_promisc(void *arg, boolean_t on)
1218 {
1219 	nge_t *ngep = arg;
1220 
1221 	NGE_TRACE(("nge_m_promisc($%p)", arg));
1222 
1223 	/*
1224 	 * Store specified mode and pass to chip layer to update h/w
1225 	 */
1226 	mutex_enter(ngep->genlock);
1227 	/*
1228 	 * If suspended, there is no need to do anything, even
1229 	 * recording the promiscuious mode is not neccessary, as
1230 	 * it won't be properly set on resume.  Just return failing.
1231 	 */
1232 	if (ngep->suspended) {
1233 		mutex_exit(ngep->genlock);
1234 		return (DDI_FAILURE);
1235 	}
1236 	if (ngep->promisc == on) {
1237 		mutex_exit(ngep->genlock);
1238 		NGE_DEBUG(("nge_m_promisc($%p) done", arg));
1239 		return (0);
1240 	}
1241 	ngep->promisc = on;
1242 	ngep->record_promisc = ngep->promisc;
1243 	nge_chip_sync(ngep);
1244 	NGE_DEBUG(("nge_m_promisc($%p) done", arg));
1245 	mutex_exit(ngep->genlock);
1246 
1247 	return (0);
1248 }
1249 
1250 static void nge_mulparam(nge_t *ngep)
1251 {
1252 	uint8_t number;
1253 	ether_addr_t pand;
1254 	ether_addr_t por;
1255 	mul_item *plist;
1256 
1257 	for (number = 0; number < ETHERADDRL; number++) {
1258 		pand[number] = 0x00;
1259 		por[number] = 0x00;
1260 	}
1261 	for (plist = ngep->pcur_mulist; plist != NULL; plist = plist->next) {
1262 		for (number = 0; number < ETHERADDRL; number++) {
1263 			pand[number] &= plist->mul_addr[number];
1264 			por[number] |= plist->mul_addr[number];
1265 		}
1266 	}
1267 	for (number = 0; number < ETHERADDRL; number++) {
1268 		ngep->cur_mul_addr.addr[number]
1269 		    = pand[number] & por[number];
1270 		ngep->cur_mul_mask.addr[number]
1271 		    = pand [number] | (~por[number]);
1272 	}
1273 }
1274 static int
1275 nge_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
1276 {
1277 	boolean_t update;
1278 	boolean_t b_eq;
1279 	nge_t *ngep = arg;
1280 	mul_item *plist;
1281 	mul_item *plist_prev;
1282 	mul_item *pitem;
1283 
1284 	NGE_TRACE(("nge_m_multicst($%p, %s, %s)", arg,
1285 	    (add) ? "add" : "remove", ether_sprintf((void *)mca)));
1286 
1287 	update = B_FALSE;
1288 	plist = plist_prev = NULL;
1289 	mutex_enter(ngep->genlock);
1290 	if (add) {
1291 		if (ngep->pcur_mulist != NULL) {
1292 			for (plist = ngep->pcur_mulist; plist != NULL;
1293 			    plist = plist->next) {
1294 				b_eq = ether_eq(plist->mul_addr, mca);
1295 				if (b_eq) {
1296 					plist->ref_cnt++;
1297 					break;
1298 				}
1299 				plist_prev = plist;
1300 			}
1301 		}
1302 
1303 		if (plist == NULL) {
1304 			pitem = kmem_zalloc(sizeof (mul_item), KM_SLEEP);
1305 			ether_copy(mca, pitem->mul_addr);
1306 			pitem ->ref_cnt++;
1307 			pitem ->next = NULL;
1308 			if (plist_prev == NULL)
1309 				ngep->pcur_mulist = pitem;
1310 			else
1311 				plist_prev->next = pitem;
1312 			update = B_TRUE;
1313 		}
1314 	} else {
1315 		if (ngep->pcur_mulist != NULL) {
1316 			for (plist = ngep->pcur_mulist; plist != NULL;
1317 			    plist = plist->next) {
1318 				b_eq = ether_eq(plist->mul_addr, mca);
1319 				if (b_eq) {
1320 					update = B_TRUE;
1321 					break;
1322 				}
1323 				plist_prev = plist;
1324 			}
1325 
1326 			if (update) {
1327 				if ((plist_prev == NULL) &&
1328 				    (plist->next == NULL))
1329 					ngep->pcur_mulist = NULL;
1330 				else if ((plist_prev == NULL) &&
1331 				    (plist->next != NULL))
1332 					ngep->pcur_mulist = plist->next;
1333 				else
1334 					plist_prev->next = plist->next;
1335 				kmem_free(plist, sizeof (mul_item));
1336 			}
1337 		}
1338 	}
1339 
1340 	if (update && !ngep->suspended) {
1341 		nge_mulparam(ngep);
1342 		nge_chip_sync(ngep);
1343 	}
1344 	NGE_DEBUG(("nge_m_multicst($%p) done", arg));
1345 	mutex_exit(ngep->genlock);
1346 
1347 	return (0);
1348 }
1349 
1350 static void
1351 nge_m_ioctl(void *arg, queue_t *wq, mblk_t *mp)
1352 {
1353 	int err;
1354 	int cmd;
1355 	nge_t *ngep = arg;
1356 	struct iocblk *iocp;
1357 	enum ioc_reply status;
1358 	boolean_t need_privilege;
1359 
1360 	/*
1361 	 * If suspended, we might actually be able to do some of
1362 	 * these ioctls, but it is harder to make sure they occur
1363 	 * without actually putting the hardware in an undesireable
1364 	 * state.  So just NAK it.
1365 	 */
1366 	mutex_enter(ngep->genlock);
1367 	if (ngep->suspended) {
1368 		miocnak(wq, mp, 0, EINVAL);
1369 		mutex_exit(ngep->genlock);
1370 		return;
1371 	}
1372 	mutex_exit(ngep->genlock);
1373 
1374 	/*
1375 	 * Validate the command before bothering with the mutex ...
1376 	 */
1377 	iocp = (struct iocblk *)mp->b_rptr;
1378 	iocp->ioc_error = 0;
1379 	need_privilege = B_TRUE;
1380 	cmd = iocp->ioc_cmd;
1381 
1382 	NGE_DEBUG(("nge_m_ioctl:  cmd 0x%x", cmd));
1383 	switch (cmd) {
1384 	default:
1385 		NGE_LDB(NGE_DBG_BADIOC,
1386 		    ("nge_m_ioctl: unknown cmd 0x%x", cmd));
1387 
1388 		miocnak(wq, mp, 0, EINVAL);
1389 		return;
1390 
1391 	case NGE_MII_READ:
1392 	case NGE_MII_WRITE:
1393 	case NGE_SEE_READ:
1394 	case NGE_SEE_WRITE:
1395 	case NGE_DIAG:
1396 	case NGE_PEEK:
1397 	case NGE_POKE:
1398 	case NGE_PHY_RESET:
1399 	case NGE_SOFT_RESET:
1400 	case NGE_HARD_RESET:
1401 		break;
1402 
1403 	case LB_GET_INFO_SIZE:
1404 	case LB_GET_INFO:
1405 	case LB_GET_MODE:
1406 		need_privilege = B_FALSE;
1407 		break;
1408 	case LB_SET_MODE:
1409 		break;
1410 	}
1411 
1412 	if (need_privilege) {
1413 		/*
1414 		 * Check for specific net_config privilege.
1415 		 */
1416 		err = secpolicy_net_config(iocp->ioc_cr, B_FALSE);
1417 		if (err != 0) {
1418 			NGE_DEBUG(("nge_m_ioctl: rejected cmd 0x%x, err %d",
1419 			    cmd, err));
1420 			miocnak(wq, mp, 0, err);
1421 			return;
1422 		}
1423 	}
1424 
1425 	mutex_enter(ngep->genlock);
1426 
1427 	switch (cmd) {
1428 	default:
1429 		_NOTE(NOTREACHED)
1430 		status = IOC_INVAL;
1431 	break;
1432 
1433 	case NGE_MII_READ:
1434 	case NGE_MII_WRITE:
1435 	case NGE_SEE_READ:
1436 	case NGE_SEE_WRITE:
1437 	case NGE_DIAG:
1438 	case NGE_PEEK:
1439 	case NGE_POKE:
1440 	case NGE_PHY_RESET:
1441 	case NGE_SOFT_RESET:
1442 	case NGE_HARD_RESET:
1443 		status = nge_chip_ioctl(ngep, mp, iocp);
1444 	break;
1445 
1446 	case LB_GET_INFO_SIZE:
1447 	case LB_GET_INFO:
1448 	case LB_GET_MODE:
1449 	case LB_SET_MODE:
1450 		status = nge_loop_ioctl(ngep, mp, iocp);
1451 	break;
1452 
1453 	}
1454 
1455 	/*
1456 	 * Do we need to reprogram the PHY and/or the MAC?
1457 	 * Do it now, while we still have the mutex.
1458 	 *
1459 	 * Note: update the PHY first, 'cos it controls the
1460 	 * speed/duplex parameters that the MAC code uses.
1461 	 */
1462 
1463 	NGE_DEBUG(("nge_m_ioctl: cmd 0x%x status %d", cmd, status));
1464 
1465 	switch (status) {
1466 	case IOC_RESTART_REPLY:
1467 	case IOC_RESTART_ACK:
1468 		(*ngep->physops->phys_update)(ngep);
1469 		nge_chip_sync(ngep);
1470 		break;
1471 
1472 	default:
1473 	break;
1474 	}
1475 
1476 	mutex_exit(ngep->genlock);
1477 
1478 	/*
1479 	 * Finally, decide how to reply
1480 	 */
1481 	switch (status) {
1482 
1483 	default:
1484 	case IOC_INVAL:
1485 		miocnak(wq, mp, 0, iocp->ioc_error == 0 ?
1486 		    EINVAL : iocp->ioc_error);
1487 		break;
1488 
1489 	case IOC_DONE:
1490 		break;
1491 
1492 	case IOC_RESTART_ACK:
1493 	case IOC_ACK:
1494 		miocack(wq, mp, 0, 0);
1495 		break;
1496 
1497 	case IOC_RESTART_REPLY:
1498 	case IOC_REPLY:
1499 		mp->b_datap->db_type = iocp->ioc_error == 0 ?
1500 		    M_IOCACK : M_IOCNAK;
1501 		qreply(wq, mp);
1502 		break;
1503 	}
1504 }
1505 
1506 static boolean_t
1507 nge_param_locked(mac_prop_id_t pr_num)
1508 {
1509 	/*
1510 	 * All adv_* parameters are locked (read-only) while
1511 	 * the device is in any sort of loopback mode ...
1512 	 */
1513 	switch (pr_num) {
1514 		case MAC_PROP_ADV_1000FDX_CAP:
1515 		case MAC_PROP_EN_1000FDX_CAP:
1516 		case MAC_PROP_ADV_1000HDX_CAP:
1517 		case MAC_PROP_EN_1000HDX_CAP:
1518 		case MAC_PROP_ADV_100FDX_CAP:
1519 		case MAC_PROP_EN_100FDX_CAP:
1520 		case MAC_PROP_ADV_100HDX_CAP:
1521 		case MAC_PROP_EN_100HDX_CAP:
1522 		case MAC_PROP_ADV_10FDX_CAP:
1523 		case MAC_PROP_EN_10FDX_CAP:
1524 		case MAC_PROP_ADV_10HDX_CAP:
1525 		case MAC_PROP_EN_10HDX_CAP:
1526 		case MAC_PROP_AUTONEG:
1527 		case MAC_PROP_FLOWCTRL:
1528 			return (B_TRUE);
1529 	}
1530 	return (B_FALSE);
1531 }
1532 
1533 /*
1534  * callback functions for set/get of properties
1535  */
1536 static int
1537 nge_m_setprop(void *barg, const char *pr_name, mac_prop_id_t pr_num,
1538     uint_t pr_valsize, const void *pr_val)
1539 {
1540 	nge_t *ngep = barg;
1541 	int err = 0;
1542 	uint32_t cur_mtu, new_mtu;
1543 	link_flowctrl_t fl;
1544 
1545 	mutex_enter(ngep->genlock);
1546 	if (ngep->param_loop_mode != NGE_LOOP_NONE &&
1547 	    nge_param_locked(pr_num)) {
1548 		/*
1549 		 * All adv_* parameters are locked (read-only)
1550 		 * while the device is in any sort of loopback mode.
1551 		 */
1552 		mutex_exit(ngep->genlock);
1553 		return (EBUSY);
1554 	}
1555 	switch (pr_num) {
1556 		case MAC_PROP_EN_1000FDX_CAP:
1557 			ngep->param_en_1000fdx = *(uint8_t *)pr_val;
1558 			ngep->param_adv_1000fdx = *(uint8_t *)pr_val;
1559 			goto reprogram;
1560 		case MAC_PROP_EN_100FDX_CAP:
1561 			ngep->param_en_100fdx = *(uint8_t *)pr_val;
1562 			ngep->param_adv_100fdx = *(uint8_t *)pr_val;
1563 			goto reprogram;
1564 		case MAC_PROP_EN_100HDX_CAP:
1565 			ngep->param_en_100hdx = *(uint8_t *)pr_val;
1566 			ngep->param_adv_100hdx = *(uint8_t *)pr_val;
1567 			goto reprogram;
1568 		case MAC_PROP_EN_10FDX_CAP:
1569 			ngep->param_en_10fdx = *(uint8_t *)pr_val;
1570 			ngep->param_adv_10fdx = *(uint8_t *)pr_val;
1571 			goto reprogram;
1572 		case MAC_PROP_EN_10HDX_CAP:
1573 			ngep->param_en_10hdx = *(uint8_t *)pr_val;
1574 			ngep->param_adv_10hdx = *(uint8_t *)pr_val;
1575 reprogram:
1576 		(*ngep->physops->phys_update)(ngep);
1577 		nge_chip_sync(ngep);
1578 		break;
1579 
1580 		case MAC_PROP_ADV_1000FDX_CAP:
1581 		case MAC_PROP_ADV_1000HDX_CAP:
1582 		case MAC_PROP_ADV_100FDX_CAP:
1583 		case MAC_PROP_ADV_100HDX_CAP:
1584 		case MAC_PROP_ADV_10FDX_CAP:
1585 		case MAC_PROP_ADV_10HDX_CAP:
1586 		case MAC_PROP_STATUS:
1587 		case MAC_PROP_SPEED:
1588 		case MAC_PROP_DUPLEX:
1589 		case MAC_PROP_EN_1000HDX_CAP:
1590 			err = ENOTSUP; /* read-only prop. Can't set this */
1591 			break;
1592 		case MAC_PROP_AUTONEG:
1593 			ngep->param_adv_autoneg = *(uint8_t *)pr_val;
1594 			(*ngep->physops->phys_update)(ngep);
1595 			nge_chip_sync(ngep);
1596 			break;
1597 		case MAC_PROP_MTU:
1598 			cur_mtu = ngep->default_mtu;
1599 			bcopy(pr_val, &new_mtu, sizeof (new_mtu));
1600 			if (new_mtu == cur_mtu) {
1601 				err = 0;
1602 				break;
1603 			}
1604 			if (new_mtu < ETHERMTU ||
1605 			    new_mtu > NGE_MAX_MTU) {
1606 				err = EINVAL;
1607 				break;
1608 			}
1609 			if ((new_mtu > ETHERMTU) &&
1610 			    (!ngep->dev_spec_param.jumbo)) {
1611 				err = EINVAL;
1612 				break;
1613 			}
1614 			if (ngep->nge_mac_state == NGE_MAC_STARTED) {
1615 				err = EBUSY;
1616 				break;
1617 			}
1618 
1619 			ngep->default_mtu = new_mtu;
1620 			if (ngep->default_mtu > ETHERMTU &&
1621 			    ngep->default_mtu <= NGE_MTU_2500) {
1622 				ngep->buf_size = NGE_JB2500_BUFSZ;
1623 				ngep->tx_desc = NGE_SEND_JB2500_SLOTS_DESC;
1624 				ngep->rx_desc = NGE_RECV_JB2500_SLOTS_DESC;
1625 				ngep->rx_buf = NGE_RECV_JB2500_SLOTS_DESC * 2;
1626 				ngep->nge_split = NGE_SPLIT_256;
1627 			} else if (ngep->default_mtu > NGE_MTU_2500 &&
1628 			    ngep->default_mtu <= NGE_MTU_4500) {
1629 				ngep->buf_size = NGE_JB4500_BUFSZ;
1630 				ngep->tx_desc = NGE_SEND_JB4500_SLOTS_DESC;
1631 				ngep->rx_desc = NGE_RECV_JB4500_SLOTS_DESC;
1632 				ngep->rx_buf = NGE_RECV_JB4500_SLOTS_DESC * 2;
1633 				ngep->nge_split = NGE_SPLIT_256;
1634 			} else if (ngep->default_mtu > NGE_MTU_4500 &&
1635 			    ngep->default_mtu <= NGE_MAX_MTU) {
1636 				ngep->buf_size = NGE_JB9000_BUFSZ;
1637 				ngep->tx_desc = NGE_SEND_JB9000_SLOTS_DESC;
1638 				ngep->rx_desc = NGE_RECV_JB9000_SLOTS_DESC;
1639 				ngep->rx_buf = NGE_RECV_JB9000_SLOTS_DESC * 2;
1640 				ngep->nge_split = NGE_SPLIT_256;
1641 			} else if (ngep->default_mtu > NGE_MAX_MTU) {
1642 				ngep->default_mtu = NGE_MAX_MTU;
1643 				ngep->buf_size = NGE_JB9000_BUFSZ;
1644 				ngep->tx_desc = NGE_SEND_JB9000_SLOTS_DESC;
1645 				ngep->rx_desc = NGE_RECV_JB9000_SLOTS_DESC;
1646 				ngep->rx_buf = NGE_RECV_JB9000_SLOTS_DESC * 2;
1647 				ngep->nge_split = NGE_SPLIT_256;
1648 			} else if (ngep->lowmem_mode != 0) {
1649 				ngep->default_mtu = ETHERMTU;
1650 				ngep->buf_size = NGE_STD_BUFSZ;
1651 				ngep->tx_desc = NGE_SEND_LOWMEM_SLOTS_DESC;
1652 				ngep->rx_desc = NGE_RECV_LOWMEM_SLOTS_DESC;
1653 				ngep->rx_buf = NGE_RECV_LOWMEM_SLOTS_DESC * 2;
1654 				ngep->nge_split = NGE_SPLIT_32;
1655 			} else {
1656 				ngep->default_mtu = ETHERMTU;
1657 				ngep->buf_size = NGE_STD_BUFSZ;
1658 				ngep->tx_desc =
1659 				    ngep->dev_spec_param.tx_desc_num;
1660 				ngep->rx_desc =
1661 				    ngep->dev_spec_param.rx_desc_num;
1662 				ngep->rx_buf =
1663 				    ngep->dev_spec_param.rx_desc_num * 2;
1664 				ngep->nge_split =
1665 				    ngep->dev_spec_param.nge_split;
1666 			}
1667 
1668 			err = mac_maxsdu_update(ngep->mh, ngep->default_mtu);
1669 
1670 			break;
1671 		case MAC_PROP_FLOWCTRL:
1672 			bcopy(pr_val, &fl, sizeof (fl));
1673 			switch (fl) {
1674 			default:
1675 				err = ENOTSUP;
1676 				break;
1677 			case LINK_FLOWCTRL_NONE:
1678 				ngep->param_adv_pause = 0;
1679 				ngep->param_adv_asym_pause = 0;
1680 
1681 				ngep->param_link_rx_pause = B_FALSE;
1682 				ngep->param_link_tx_pause = B_FALSE;
1683 				break;
1684 			case LINK_FLOWCTRL_RX:
1685 				if (!((ngep->param_lp_pause == 0) &&
1686 				    (ngep->param_lp_asym_pause == 1))) {
1687 					err = EINVAL;
1688 					break;
1689 				}
1690 				ngep->param_adv_pause = 1;
1691 				ngep->param_adv_asym_pause = 1;
1692 
1693 				ngep->param_link_rx_pause = B_TRUE;
1694 				ngep->param_link_tx_pause = B_FALSE;
1695 				break;
1696 			case LINK_FLOWCTRL_TX:
1697 				if (!((ngep->param_lp_pause == 1) &&
1698 				    (ngep->param_lp_asym_pause == 1))) {
1699 					err = EINVAL;
1700 					break;
1701 				}
1702 				ngep->param_adv_pause = 0;
1703 				ngep->param_adv_asym_pause = 1;
1704 
1705 				ngep->param_link_rx_pause = B_FALSE;
1706 				ngep->param_link_tx_pause = B_TRUE;
1707 				break;
1708 			case LINK_FLOWCTRL_BI:
1709 				if (ngep->param_lp_pause != 1) {
1710 					err = EINVAL;
1711 					break;
1712 				}
1713 				ngep->param_adv_pause = 1;
1714 
1715 				ngep->param_link_rx_pause = B_TRUE;
1716 				ngep->param_link_tx_pause = B_TRUE;
1717 				break;
1718 			}
1719 
1720 			if (err == 0) {
1721 				(*ngep->physops->phys_update)(ngep);
1722 				nge_chip_sync(ngep);
1723 			}
1724 
1725 			break;
1726 		case MAC_PROP_PRIVATE:
1727 			err = nge_set_priv_prop(ngep, pr_name, pr_valsize,
1728 			    pr_val);
1729 			if (err == 0) {
1730 				(*ngep->physops->phys_update)(ngep);
1731 				nge_chip_sync(ngep);
1732 			}
1733 			break;
1734 		default:
1735 			err = ENOTSUP;
1736 	}
1737 	mutex_exit(ngep->genlock);
1738 	return (err);
1739 }
1740 
1741 static int
1742 nge_m_getprop(void *barg, const char *pr_name, mac_prop_id_t pr_num,
1743     uint_t pr_flags, uint_t pr_valsize, void *pr_val)
1744 {
1745 	nge_t *ngep = barg;
1746 	int err = 0;
1747 	link_flowctrl_t fl;
1748 	uint64_t speed;
1749 	boolean_t is_default = (pr_flags & MAC_PROP_DEFAULT);
1750 
1751 	if (pr_valsize == 0)
1752 		return (EINVAL);
1753 
1754 	bzero(pr_val, pr_valsize);
1755 	switch (pr_num) {
1756 		case MAC_PROP_DUPLEX:
1757 			if (pr_valsize >= sizeof (link_duplex_t)) {
1758 				bcopy(&ngep->param_link_duplex, pr_val,
1759 				    sizeof (link_duplex_t));
1760 			} else
1761 				err = EINVAL;
1762 			break;
1763 		case MAC_PROP_SPEED:
1764 			if (pr_valsize >= sizeof (uint64_t)) {
1765 				speed = ngep->param_link_speed * 1000000ull;
1766 				bcopy(&speed, pr_val, sizeof (speed));
1767 			} else
1768 				err = EINVAL;
1769 			break;
1770 		case MAC_PROP_AUTONEG:
1771 			if (is_default) {
1772 				*(uint8_t *)pr_val = 1;
1773 			} else {
1774 				*(uint8_t *)pr_val = ngep->param_adv_autoneg;
1775 			}
1776 			break;
1777 		case MAC_PROP_FLOWCTRL:
1778 			if (pr_valsize >= sizeof (link_flowctrl_t)) {
1779 				if (pr_flags & MAC_PROP_DEFAULT) {
1780 					fl = LINK_FLOWCTRL_BI;
1781 					bcopy(&fl, pr_val, sizeof (fl));
1782 					break;
1783 				}
1784 				if (ngep->param_link_rx_pause &&
1785 				    !ngep->param_link_tx_pause)
1786 					fl = LINK_FLOWCTRL_RX;
1787 
1788 				if (!ngep->param_link_rx_pause &&
1789 				    !ngep->param_link_tx_pause)
1790 					fl = LINK_FLOWCTRL_NONE;
1791 
1792 				if (!ngep->param_link_rx_pause &&
1793 				    ngep->param_link_tx_pause)
1794 					fl = LINK_FLOWCTRL_TX;
1795 
1796 				if (ngep->param_link_rx_pause &&
1797 				    ngep->param_link_tx_pause)
1798 					fl = LINK_FLOWCTRL_BI;
1799 				bcopy(&fl, pr_val, sizeof (fl));
1800 			} else
1801 				err = EINVAL;
1802 			break;
1803 		case MAC_PROP_ADV_1000FDX_CAP:
1804 			if (is_default) {
1805 				*(uint8_t *)pr_val = 1;
1806 			} else {
1807 				*(uint8_t *)pr_val = ngep->param_adv_1000fdx;
1808 			}
1809 			break;
1810 		case MAC_PROP_EN_1000FDX_CAP:
1811 			if (is_default) {
1812 				*(uint8_t *)pr_val = 1;
1813 			} else {
1814 				*(uint8_t *)pr_val = ngep->param_en_1000fdx;
1815 			}
1816 			break;
1817 		case MAC_PROP_ADV_1000HDX_CAP:
1818 			if (is_default) {
1819 				*(uint8_t *)pr_val = 0;
1820 			} else {
1821 				*(uint8_t *)pr_val = ngep->param_adv_1000hdx;
1822 			}
1823 			break;
1824 		case MAC_PROP_EN_1000HDX_CAP:
1825 			if (is_default) {
1826 				*(uint8_t *)pr_val = 0;
1827 			} else {
1828 				*(uint8_t *)pr_val = ngep->param_en_1000hdx;
1829 			}
1830 			break;
1831 		case MAC_PROP_ADV_100FDX_CAP:
1832 			if (is_default) {
1833 				*(uint8_t *)pr_val = 1;
1834 			} else {
1835 				*(uint8_t *)pr_val = ngep->param_adv_100fdx;
1836 			}
1837 			break;
1838 		case MAC_PROP_EN_100FDX_CAP:
1839 			if (is_default) {
1840 				*(uint8_t *)pr_val = 1;
1841 			} else {
1842 				*(uint8_t *)pr_val = ngep->param_en_100fdx;
1843 			}
1844 			break;
1845 		case MAC_PROP_ADV_100HDX_CAP:
1846 			if (is_default) {
1847 				*(uint8_t *)pr_val = 1;
1848 			} else {
1849 				*(uint8_t *)pr_val = ngep->param_adv_100hdx;
1850 			}
1851 			break;
1852 		case MAC_PROP_EN_100HDX_CAP:
1853 			if (is_default) {
1854 				*(uint8_t *)pr_val = 1;
1855 			} else {
1856 				*(uint8_t *)pr_val = ngep->param_en_100hdx;
1857 			}
1858 			break;
1859 		case MAC_PROP_ADV_10FDX_CAP:
1860 			if (is_default) {
1861 				*(uint8_t *)pr_val = 1;
1862 			} else {
1863 				*(uint8_t *)pr_val = ngep->param_adv_10fdx;
1864 			}
1865 			break;
1866 		case MAC_PROP_EN_10FDX_CAP:
1867 			if (is_default) {
1868 				*(uint8_t *)pr_val = 1;
1869 			} else {
1870 				*(uint8_t *)pr_val = ngep->param_en_10fdx;
1871 			}
1872 			break;
1873 		case MAC_PROP_ADV_10HDX_CAP:
1874 			if (is_default) {
1875 				*(uint8_t *)pr_val = 1;
1876 			} else {
1877 				*(uint8_t *)pr_val = ngep->param_adv_10hdx;
1878 			}
1879 			break;
1880 		case MAC_PROP_EN_10HDX_CAP:
1881 			if (is_default) {
1882 				*(uint8_t *)pr_val = 1;
1883 			} else {
1884 				*(uint8_t *)pr_val = ngep->param_en_10hdx;
1885 			}
1886 			break;
1887 		case MAC_PROP_ADV_100T4_CAP:
1888 		case MAC_PROP_EN_100T4_CAP:
1889 			*(uint8_t *)pr_val = 0;
1890 			break;
1891 		case MAC_PROP_PRIVATE:
1892 			err = nge_get_priv_prop(ngep, pr_name, pr_flags,
1893 			    pr_valsize, pr_val);
1894 			break;
1895 		default:
1896 			err = ENOTSUP;
1897 	}
1898 	return (err);
1899 }
1900 
1901 /* ARGSUSED */
1902 static int
1903 nge_set_priv_prop(nge_t *ngep, const char *pr_name, uint_t pr_valsize,
1904     const void *pr_val)
1905 {
1906 	int err = 0;
1907 	long result;
1908 
1909 	if (strcmp(pr_name, "_tx_bcopy_threshold") == 0) {
1910 		if (pr_val == NULL) {
1911 			err = EINVAL;
1912 			return (err);
1913 		}
1914 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
1915 		if (result < 0 || result > NGE_MAX_SDU) {
1916 			err = EINVAL;
1917 		} else {
1918 			ngep->param_txbcopy_threshold = (uint32_t)result;
1919 			goto reprogram;
1920 		}
1921 		return (err);
1922 	}
1923 	if (strcmp(pr_name, "_rx_bcopy_threshold") == 0) {
1924 		if (pr_val == NULL) {
1925 			err = EINVAL;
1926 			return (err);
1927 		}
1928 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
1929 		if (result < 0 || result > NGE_MAX_SDU) {
1930 			err = EINVAL;
1931 		} else {
1932 			ngep->param_rxbcopy_threshold = (uint32_t)result;
1933 			goto reprogram;
1934 		}
1935 		return (err);
1936 	}
1937 	if (strcmp(pr_name, "_recv_max_packet") == 0) {
1938 		if (pr_val == NULL) {
1939 			err = EINVAL;
1940 			return (err);
1941 		}
1942 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
1943 		if (result < 0 || result > NGE_RECV_SLOTS_DESC_1024) {
1944 			err = EINVAL;
1945 		} else {
1946 			ngep->param_recv_max_packet = (uint32_t)result;
1947 			goto reprogram;
1948 		}
1949 		return (err);
1950 	}
1951 	if (strcmp(pr_name, "_poll_quiet_time") == 0) {
1952 		if (pr_val == NULL) {
1953 			err = EINVAL;
1954 			return (err);
1955 		}
1956 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
1957 		if (result < 0 || result > 10000) {
1958 			err = EINVAL;
1959 		} else {
1960 			ngep->param_poll_quiet_time = (uint32_t)result;
1961 			goto reprogram;
1962 		}
1963 		return (err);
1964 	}
1965 	if (strcmp(pr_name, "_poll_busy_time") == 0) {
1966 		if (pr_val == NULL) {
1967 			err = EINVAL;
1968 			return (err);
1969 		}
1970 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
1971 		if (result < 0 || result > 10000) {
1972 			err = EINVAL;
1973 		} else {
1974 			ngep->param_poll_busy_time = (uint32_t)result;
1975 			goto reprogram;
1976 		}
1977 		return (err);
1978 	}
1979 	if (strcmp(pr_name, "_rx_intr_hwater") == 0) {
1980 		if (pr_val == NULL) {
1981 			err = EINVAL;
1982 			return (err);
1983 		}
1984 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
1985 		if (result < 0 || result > NGE_RECV_SLOTS_DESC_1024) {
1986 			err = EINVAL;
1987 		} else {
1988 			ngep->param_rx_intr_hwater = (uint32_t)result;
1989 			goto reprogram;
1990 		}
1991 		return (err);
1992 	}
1993 	if (strcmp(pr_name, "_rx_intr_lwater") == 0) {
1994 		if (pr_val == NULL) {
1995 			err = EINVAL;
1996 			return (err);
1997 		}
1998 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
1999 		if (result < 0 || result > NGE_RECV_SLOTS_DESC_1024) {
2000 			err = EINVAL;
2001 		} else {
2002 			ngep->param_rx_intr_lwater = (uint32_t)result;
2003 			goto reprogram;
2004 		}
2005 		return (err);
2006 	}
2007 	if (strcmp(pr_name, "_tx_n_intr") == 0) {
2008 		if (pr_val == NULL) {
2009 			err = EINVAL;
2010 			return (err);
2011 		}
2012 		(void) ddi_strtol(pr_val, (char **)NULL, 0, &result);
2013 		if (result < 1 || result > 10000) {
2014 			err = EINVAL;
2015 		} else {
2016 			ngep->param_tx_n_intr = (uint32_t)result;
2017 			goto reprogram;
2018 		}
2019 		return (err);
2020 	}
2021 
2022 	err = ENOTSUP;
2023 	return (err);
2024 
2025 reprogram:
2026 	if (err == 0) {
2027 		(*ngep->physops->phys_update)(ngep);
2028 		nge_chip_sync(ngep);
2029 	}
2030 
2031 	return (err);
2032 }
2033 
2034 static int
2035 nge_get_priv_prop(nge_t *ngep, const char *pr_name, uint_t pr_flags,
2036     uint_t pr_valsize, void *pr_val)
2037 {
2038 	int err = ENOTSUP;
2039 	boolean_t is_default = (pr_flags & MAC_PROP_DEFAULT);
2040 	int value;
2041 
2042 	if (strcmp(pr_name, "_adv_pause_cap") == 0) {
2043 		value = (is_default ? 1 : ngep->param_adv_pause);
2044 		err = 0;
2045 		goto done;
2046 	}
2047 	if (strcmp(pr_name, "_adv_asym_pause_cap") == 0) {
2048 		value = (is_default ? 1 : ngep->param_adv_asym_pause);
2049 		err = 0;
2050 		goto done;
2051 	}
2052 	if (strcmp(pr_name, "_tx_bcopy_threshold") == 0) {
2053 		value = (is_default ? NGE_TX_COPY_SIZE :
2054 		    ngep->param_txbcopy_threshold);
2055 		err = 0;
2056 		goto done;
2057 	}
2058 	if (strcmp(pr_name, "_rx_bcopy_threshold") == 0) {
2059 		value = (is_default ? NGE_RX_COPY_SIZE :
2060 		    ngep->param_rxbcopy_threshold);
2061 		err = 0;
2062 		goto done;
2063 	}
2064 	if (strcmp(pr_name, "_recv_max_packet") == 0) {
2065 		value = (is_default ? 128 : ngep->param_recv_max_packet);
2066 		err = 0;
2067 		goto done;
2068 	}
2069 	if (strcmp(pr_name, "_poll_quiet_time") == 0) {
2070 		value = (is_default ? NGE_POLL_QUIET_TIME :
2071 		    ngep->param_poll_quiet_time);
2072 		err = 0;
2073 		goto done;
2074 	}
2075 	if (strcmp(pr_name, "_poll_busy_time") == 0) {
2076 		value = (is_default ? NGE_POLL_BUSY_TIME :
2077 		    ngep->param_poll_busy_time);
2078 		err = 0;
2079 		goto done;
2080 	}
2081 	if (strcmp(pr_name, "_rx_intr_hwater") == 0) {
2082 		value = (is_default ? 1 : ngep->param_rx_intr_hwater);
2083 		err = 0;
2084 		goto done;
2085 	}
2086 	if (strcmp(pr_name, "_rx_intr_lwater") == 0) {
2087 		value = (is_default ? 8 : ngep->param_rx_intr_lwater);
2088 		err = 0;
2089 		goto done;
2090 	}
2091 	if (strcmp(pr_name, "_tx_n_intr") == 0) {
2092 		value = (is_default ? NGE_TX_N_INTR :
2093 		    ngep->param_tx_n_intr);
2094 		err = 0;
2095 		goto done;
2096 	}
2097 
2098 done:
2099 	if (err == 0) {
2100 		(void) snprintf(pr_val, pr_valsize, "%d", value);
2101 	}
2102 	return (err);
2103 }
2104 
2105 /* ARGSUSED */
2106 static boolean_t
2107 nge_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
2108 {
2109 	nge_t	*ngep = arg;
2110 	nge_dev_spec_param_t *dev_param_p;
2111 
2112 	dev_param_p = &ngep->dev_spec_param;
2113 
2114 	switch (cap) {
2115 	case MAC_CAPAB_HCKSUM: {
2116 		uint32_t *hcksum_txflags = cap_data;
2117 
2118 		if (dev_param_p->tx_hw_checksum) {
2119 			*hcksum_txflags = dev_param_p->tx_hw_checksum;
2120 		} else
2121 			return (B_FALSE);
2122 		break;
2123 	}
2124 	case MAC_CAPAB_POLL:
2125 		/*
2126 		 * There's nothing for us to fill in, simply returning
2127 		 * B_TRUE, stating that we support polling is sufficient.
2128 		 */
2129 		break;
2130 	default:
2131 		return (B_FALSE);
2132 	}
2133 	return (B_TRUE);
2134 }
2135 
2136 #undef	NGE_DBG
2137 #define	NGE_DBG	NGE_DBG_INIT	/* debug flag for this code	*/
2138 int
2139 nge_restart(nge_t *ngep)
2140 {
2141 	int err = 0;
2142 	err = nge_reset_dev(ngep);
2143 	/* write back the promisc setting */
2144 	ngep->promisc = ngep->record_promisc;
2145 	nge_chip_sync(ngep);
2146 	if (!err)
2147 		err = nge_chip_start(ngep);
2148 
2149 	if (err) {
2150 		ngep->nge_mac_state = NGE_MAC_STOPPED;
2151 		return (DDI_FAILURE);
2152 	} else {
2153 		ngep->nge_mac_state = NGE_MAC_STARTED;
2154 		return (DDI_SUCCESS);
2155 	}
2156 }
2157 
2158 void
2159 nge_wake_factotum(nge_t *ngep)
2160 {
2161 	mutex_enter(ngep->softlock);
2162 	if (ngep->factotum_flag == 0) {
2163 		ngep->factotum_flag = 1;
2164 		(void) ddi_intr_trigger_softint(ngep->factotum_hdl, NULL);
2165 	}
2166 	mutex_exit(ngep->softlock);
2167 }
2168 
2169 /*
2170  * High-level cyclic handler
2171  *
2172  * This routine schedules a (low-level) softint callback to the
2173  * factotum.
2174  */
2175 
2176 static void
2177 nge_chip_cyclic(void *arg)
2178 {
2179 	nge_t *ngep;
2180 
2181 	ngep = (nge_t *)arg;
2182 
2183 	switch (ngep->nge_chip_state) {
2184 	default:
2185 		return;
2186 
2187 	case NGE_CHIP_RUNNING:
2188 		break;
2189 
2190 	case NGE_CHIP_FAULT:
2191 	case NGE_CHIP_ERROR:
2192 		break;
2193 	}
2194 
2195 	nge_wake_factotum(ngep);
2196 }
2197 
2198 static void
2199 nge_unattach(nge_t *ngep)
2200 {
2201 	send_ring_t *srp;
2202 	buff_ring_t *brp;
2203 
2204 	srp = ngep->send;
2205 	brp = ngep->buff;
2206 	NGE_TRACE(("nge_unattach($%p)", (void *)ngep));
2207 
2208 	/*
2209 	 * Flag that no more activity may be initiated
2210 	 */
2211 	ngep->progress &= ~PROGRESS_READY;
2212 	ngep->nge_mac_state = NGE_MAC_UNATTACH;
2213 
2214 	/*
2215 	 * Quiesce the PHY and MAC (leave it reset but still powered).
2216 	 * Clean up and free all NGE data structures
2217 	 */
2218 	if (ngep->periodic_id != NULL) {
2219 		ddi_periodic_delete(ngep->periodic_id);
2220 		ngep->periodic_id = NULL;
2221 	}
2222 
2223 	if (ngep->progress & PROGRESS_KSTATS)
2224 		nge_fini_kstats(ngep);
2225 
2226 	if (ngep->progress & PROGRESS_HWINT) {
2227 		mutex_enter(ngep->genlock);
2228 		nge_restore_mac_addr(ngep);
2229 		(void) nge_chip_stop(ngep, B_FALSE);
2230 		mutex_exit(ngep->genlock);
2231 	}
2232 
2233 	if (ngep->progress & PROGRESS_SWINT)
2234 		nge_rem_intrs(ngep);
2235 
2236 	if (ngep->progress & PROGRESS_FACTOTUM)
2237 		(void) ddi_intr_remove_softint(ngep->factotum_hdl);
2238 
2239 	if (ngep->progress & PROGRESS_RESCHED)
2240 		(void) ddi_intr_remove_softint(ngep->resched_hdl);
2241 
2242 	if (ngep->progress & PROGRESS_INTR) {
2243 		mutex_destroy(srp->tx_lock);
2244 		mutex_destroy(srp->tc_lock);
2245 		mutex_destroy(&srp->dmah_lock);
2246 		mutex_destroy(brp->recycle_lock);
2247 
2248 		mutex_destroy(ngep->genlock);
2249 		mutex_destroy(ngep->softlock);
2250 		rw_destroy(ngep->rwlock);
2251 	}
2252 
2253 	if (ngep->progress & PROGRESS_REGS)
2254 		ddi_regs_map_free(&ngep->io_handle);
2255 
2256 	if (ngep->progress & PROGRESS_CFG)
2257 		pci_config_teardown(&ngep->cfg_handle);
2258 
2259 	ddi_remove_minor_node(ngep->devinfo, NULL);
2260 
2261 	kmem_free(ngep, sizeof (*ngep));
2262 }
2263 
2264 static int
2265 nge_resume(dev_info_t *devinfo)
2266 {
2267 	nge_t		*ngep;
2268 	chip_info_t	*infop;
2269 	int 		err;
2270 
2271 	ASSERT(devinfo != NULL);
2272 
2273 	ngep = ddi_get_driver_private(devinfo);
2274 	err = 0;
2275 
2276 	/*
2277 	 * If there are state inconsistancies, this is bad.  Returning
2278 	 * DDI_FAILURE here will eventually cause the machine to panic,
2279 	 * so it is best done here so that there is a possibility of
2280 	 * debugging the problem.
2281 	 */
2282 	if (ngep == NULL)
2283 		cmn_err(CE_PANIC,
2284 		    "nge: ngep returned from ddi_get_driver_private was NULL");
2285 	infop = (chip_info_t *)&ngep->chipinfo;
2286 
2287 	if (ngep->devinfo != devinfo)
2288 		cmn_err(CE_PANIC,
2289 		    "nge: passed devinfo not the same as saved devinfo");
2290 
2291 	mutex_enter(ngep->genlock);
2292 	rw_enter(ngep->rwlock, RW_WRITER);
2293 
2294 	/*
2295 	 * Fetch the config space.  Even though we have most of it cached,
2296 	 * some values *might* change across a suspend/resume.
2297 	 */
2298 	nge_chip_cfg_init(ngep, infop, B_FALSE);
2299 
2300 	/*
2301 	 * Only in one case, this conditional branch can be executed: the port
2302 	 * hasn't been plumbed.
2303 	 */
2304 	if (ngep->suspended == B_FALSE) {
2305 		rw_exit(ngep->rwlock);
2306 		mutex_exit(ngep->genlock);
2307 		return (DDI_SUCCESS);
2308 	}
2309 
2310 	nge_tx_recycle_all(ngep);
2311 	err = nge_reinit_ring(ngep);
2312 	if (!err) {
2313 		err = nge_chip_reset(ngep);
2314 		if (!err)
2315 			err = nge_chip_start(ngep);
2316 	}
2317 
2318 	if (err) {
2319 		/*
2320 		 * We note the failure, but return success, as the
2321 		 * system is still usable without this controller.
2322 		 */
2323 		cmn_err(CE_WARN, "nge: resume: failed to restart controller");
2324 	} else {
2325 		ngep->nge_mac_state = NGE_MAC_STARTED;
2326 	}
2327 	ngep->suspended = B_FALSE;
2328 
2329 	rw_exit(ngep->rwlock);
2330 	mutex_exit(ngep->genlock);
2331 
2332 	return (DDI_SUCCESS);
2333 }
2334 
2335 /*
2336  * attach(9E) -- Attach a device to the system
2337  *
2338  * Called once for each board successfully probed.
2339  */
2340 static int
2341 nge_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
2342 {
2343 	int		err;
2344 	int		i;
2345 	int		instance;
2346 	caddr_t		regs;
2347 	nge_t		*ngep;
2348 	chip_info_t	*infop;
2349 	mac_register_t	*macp;
2350 
2351 	switch (cmd) {
2352 	default:
2353 		return (DDI_FAILURE);
2354 
2355 	case DDI_RESUME:
2356 		return (nge_resume(devinfo));
2357 
2358 	case DDI_ATTACH:
2359 		break;
2360 	}
2361 
2362 	ngep = kmem_zalloc(sizeof (*ngep), KM_SLEEP);
2363 	instance = ddi_get_instance(devinfo);
2364 	ddi_set_driver_private(devinfo, ngep);
2365 	ngep->devinfo = devinfo;
2366 
2367 	(void) snprintf(ngep->ifname, sizeof (ngep->ifname), "%s%d",
2368 	    NGE_DRIVER_NAME, instance);
2369 	err = pci_config_setup(devinfo, &ngep->cfg_handle);
2370 	if (err != DDI_SUCCESS) {
2371 		nge_problem(ngep, "nge_attach: pci_config_setup() failed");
2372 		goto attach_fail;
2373 	}
2374 	/*
2375 	 * param_txbcopy_threshold and param_rxbcopy_threshold are tx/rx bcopy
2376 	 * thresholds. Bounds: min 0, max NGE_MAX_SDU
2377 	 */
2378 	ngep->param_txbcopy_threshold = NGE_TX_COPY_SIZE;
2379 	ngep->param_rxbcopy_threshold = NGE_RX_COPY_SIZE;
2380 
2381 	/*
2382 	 * param_recv_max_packet is max packet received per interupt.
2383 	 * Bounds: min 0, max NGE_RECV_SLOTS_DESC_1024
2384 	 */
2385 	ngep->param_recv_max_packet = 128;
2386 
2387 	/*
2388 	 * param_poll_quiet_time and param_poll_busy_time are quiet/busy time
2389 	 * switch from per packet interrupt to polling interrupt.
2390 	 * Bounds: min 0, max 10000
2391 	 */
2392 	ngep->param_poll_quiet_time = NGE_POLL_QUIET_TIME;
2393 	ngep->param_poll_busy_time = NGE_POLL_BUSY_TIME;
2394 
2395 	/*
2396 	 * param_rx_intr_hwater/param_rx_intr_lwater: ackets received
2397 	 * to trigger the poll_quiet_time/poll_busy_time counter.
2398 	 * Bounds: min 0, max  NGE_RECV_SLOTS_DESC_1024.
2399 	 */
2400 	ngep->param_rx_intr_hwater = 1;
2401 	ngep->param_rx_intr_lwater = 8;
2402 
2403 	/*
2404 	 * param_tx_n_intr: Per N tx packets to do tx recycle in poll mode.
2405 	 * Bounds: min 1, max 10000.
2406 	 */
2407 	ngep->param_tx_n_intr = NGE_TX_N_INTR;
2408 
2409 	infop = (chip_info_t *)&ngep->chipinfo;
2410 	nge_chip_cfg_init(ngep, infop, B_FALSE);
2411 	nge_init_dev_spec_param(ngep);
2412 	nge_get_props(ngep);
2413 	ngep->progress |= PROGRESS_CFG;
2414 
2415 	err = ddi_regs_map_setup(devinfo, NGE_PCI_OPREGS_RNUMBER,
2416 	    &regs, 0, 0, &nge_reg_accattr, &ngep->io_handle);
2417 	if (err != DDI_SUCCESS) {
2418 		nge_problem(ngep, "nge_attach: ddi_regs_map_setup() failed");
2419 		goto attach_fail;
2420 	}
2421 	ngep->io_regs = regs;
2422 	ngep->progress |= PROGRESS_REGS;
2423 
2424 	err = nge_register_intrs_and_init_locks(ngep);
2425 	if (err != DDI_SUCCESS) {
2426 		nge_problem(ngep, "nge_attach:"
2427 		    " register intrs and init locks failed");
2428 		goto attach_fail;
2429 	}
2430 	nge_init_ring_param_lock(ngep);
2431 	ngep->progress |= PROGRESS_INTR;
2432 
2433 	mutex_enter(ngep->genlock);
2434 
2435 	/*
2436 	 * Initialise link state variables
2437 	 * Stop, reset & reinitialise the chip.
2438 	 * Initialise the (internal) PHY.
2439 	 */
2440 	nge_phys_init(ngep);
2441 	err = nge_chip_reset(ngep);
2442 	if (err != DDI_SUCCESS) {
2443 		nge_problem(ngep, "nge_attach: nge_chip_reset() failed");
2444 		mutex_exit(ngep->genlock);
2445 		goto attach_fail;
2446 	}
2447 	nge_chip_sync(ngep);
2448 
2449 	/*
2450 	 * Now that mutex locks are initialized, enable interrupts.
2451 	 */
2452 	if (ngep->intr_cap & DDI_INTR_FLAG_BLOCK) {
2453 		/* Call ddi_intr_block_enable() for MSI interrupts */
2454 		(void) ddi_intr_block_enable(ngep->htable,
2455 		    ngep->intr_actual_cnt);
2456 	} else {
2457 		/* Call ddi_intr_enable for MSI or FIXED interrupts */
2458 		for (i = 0; i < ngep->intr_actual_cnt; i++) {
2459 			(void) ddi_intr_enable(ngep->htable[i]);
2460 		}
2461 	}
2462 
2463 	ngep->link_state = LINK_STATE_UNKNOWN;
2464 	ngep->progress |= PROGRESS_HWINT;
2465 
2466 	/*
2467 	 * Register NDD-tweakable parameters
2468 	 */
2469 	if (nge_nd_init(ngep)) {
2470 		nge_problem(ngep, "nge_attach: nge_nd_init() failed");
2471 		mutex_exit(ngep->genlock);
2472 		goto attach_fail;
2473 	}
2474 	ngep->progress |= PROGRESS_NDD;
2475 
2476 	/*
2477 	 * Create & initialise named kstats
2478 	 */
2479 	nge_init_kstats(ngep, instance);
2480 	ngep->progress |= PROGRESS_KSTATS;
2481 
2482 	mutex_exit(ngep->genlock);
2483 
2484 	if ((macp = mac_alloc(MAC_VERSION)) == NULL)
2485 		goto attach_fail;
2486 	macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
2487 	macp->m_driver = ngep;
2488 	macp->m_dip = devinfo;
2489 	macp->m_src_addr = infop->vendor_addr.addr;
2490 	macp->m_callbacks = &nge_m_callbacks;
2491 	macp->m_min_sdu = 0;
2492 	macp->m_max_sdu = ngep->default_mtu;
2493 	macp->m_margin = VTAG_SIZE;
2494 	macp->m_priv_props = nge_priv_props;
2495 	macp->m_priv_prop_count = NGE_MAX_PRIV_PROPS;
2496 	/*
2497 	 * Finally, we're ready to register ourselves with the mac
2498 	 * interface; if this succeeds, we're all ready to start()
2499 	 */
2500 	err = mac_register(macp, &ngep->mh);
2501 	mac_free(macp);
2502 	if (err != 0)
2503 		goto attach_fail;
2504 
2505 	/*
2506 	 * Register a periodical handler.
2507 	 * nge_chip_cyclic() is invoked in kernel context.
2508 	 */
2509 	ngep->periodic_id = ddi_periodic_add(nge_chip_cyclic, ngep,
2510 	    NGE_CYCLIC_PERIOD, DDI_IPL_0);
2511 
2512 	ngep->progress |= PROGRESS_READY;
2513 	return (DDI_SUCCESS);
2514 
2515 attach_fail:
2516 	nge_unattach(ngep);
2517 	return (DDI_FAILURE);
2518 }
2519 
2520 static int
2521 nge_suspend(nge_t *ngep)
2522 {
2523 	mutex_enter(ngep->genlock);
2524 	rw_enter(ngep->rwlock, RW_WRITER);
2525 
2526 	/* if the port hasn't been plumbed, just return */
2527 	if (ngep->nge_mac_state != NGE_MAC_STARTED) {
2528 		rw_exit(ngep->rwlock);
2529 		mutex_exit(ngep->genlock);
2530 		return (DDI_SUCCESS);
2531 	}
2532 	ngep->suspended = B_TRUE;
2533 	(void) nge_chip_stop(ngep, B_FALSE);
2534 	ngep->nge_mac_state = NGE_MAC_STOPPED;
2535 
2536 	rw_exit(ngep->rwlock);
2537 	mutex_exit(ngep->genlock);
2538 	return (DDI_SUCCESS);
2539 }
2540 
2541 /*
2542  * detach(9E) -- Detach a device from the system
2543  */
2544 static int
2545 nge_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
2546 {
2547 	int i;
2548 	nge_t *ngep;
2549 	mul_item *p, *nextp;
2550 	buff_ring_t *brp;
2551 
2552 	NGE_GTRACE(("nge_detach($%p, %d)", (void *)devinfo, cmd));
2553 
2554 	ngep = ddi_get_driver_private(devinfo);
2555 	brp = ngep->buff;
2556 
2557 	switch (cmd) {
2558 	default:
2559 		return (DDI_FAILURE);
2560 
2561 	case DDI_SUSPEND:
2562 		/*
2563 		 * Stop the NIC
2564 		 * Note: This driver doesn't currently support WOL, but
2565 		 *	should it in the future, it is important to
2566 		 *	make sure the PHY remains powered so that the
2567 		 *	wakeup packet can actually be recieved.
2568 		 */
2569 		return (nge_suspend(ngep));
2570 
2571 	case DDI_DETACH:
2572 		break;
2573 	}
2574 
2575 	/* Try to wait all the buffer post to upper layer be released */
2576 	for (i = 0; i < 1000; i++) {
2577 		if (brp->rx_hold == 0)
2578 			break;
2579 		drv_usecwait(1000);
2580 	}
2581 
2582 	/* If there is any posted buffer, reject to detach */
2583 	if (brp->rx_hold != 0)
2584 		return (DDI_FAILURE);
2585 
2586 	/*
2587 	 * Unregister from the GLD subsystem.  This can fail, in
2588 	 * particular if there are DLPI style-2 streams still open -
2589 	 * in which case we just return failure without shutting
2590 	 * down chip operations.
2591 	 */
2592 	if (mac_unregister(ngep->mh) != DDI_SUCCESS)
2593 		return (DDI_FAILURE);
2594 
2595 	/*
2596 	 * Recycle the multicast table. mac_unregister() should be called
2597 	 * before it to ensure the multicast table can be used even if
2598 	 * mac_unregister() fails.
2599 	 */
2600 	for (p = ngep->pcur_mulist; p != NULL; p = nextp) {
2601 		nextp = p->next;
2602 		kmem_free(p, sizeof (mul_item));
2603 	}
2604 	ngep->pcur_mulist = NULL;
2605 
2606 	/*
2607 	 * All activity stopped, so we can clean up & exit
2608 	 */
2609 	nge_unattach(ngep);
2610 	return (DDI_SUCCESS);
2611 }
2612 
2613 /*
2614  * quiesce(9E) entry point.
2615  *
2616  * This function is called when the system is single-threaded at high
2617  * PIL with preemption disabled. Therefore, this function must not be
2618  * blocked.
2619  *
2620  * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
2621  * DDI_FAILURE indicates an error condition and should almost never happen.
2622  */
2623 static int
2624 nge_quiesce(dev_info_t *devinfo)
2625 {
2626 	nge_t *ngep;
2627 
2628 	ngep = ddi_get_driver_private(devinfo);
2629 
2630 	if (ngep == NULL)
2631 		return (DDI_FAILURE);
2632 
2633 	/*
2634 	 * Turn off debug tracing
2635 	 */
2636 	nge_debug = 0;
2637 	ngep->debug = 0;
2638 
2639 	nge_restore_mac_addr(ngep);
2640 	(void) nge_chip_stop(ngep, B_FALSE);
2641 
2642 	return (DDI_SUCCESS);
2643 }
2644 
2645 
2646 
2647 /*
2648  * ========== Module Loading Data & Entry Points ==========
2649  */
2650 
2651 DDI_DEFINE_STREAM_OPS(nge_dev_ops, nulldev, nulldev, nge_attach, nge_detach,
2652     NULL, NULL, D_MP, NULL, nge_quiesce);
2653 
2654 
2655 static struct modldrv nge_modldrv = {
2656 	&mod_driverops,		/* Type of module.  This one is a driver */
2657 	nge_ident,		/* short description */
2658 	&nge_dev_ops		/* driver specific ops */
2659 };
2660 
2661 static struct modlinkage modlinkage = {
2662 	MODREV_1, (void *)&nge_modldrv, NULL
2663 };
2664 
2665 
2666 int
2667 _info(struct modinfo *modinfop)
2668 {
2669 	return (mod_info(&modlinkage, modinfop));
2670 }
2671 
2672 int
2673 _init(void)
2674 {
2675 	int status;
2676 
2677 	mac_init_ops(&nge_dev_ops, "nge");
2678 	status = mod_install(&modlinkage);
2679 	if (status != DDI_SUCCESS)
2680 		mac_fini_ops(&nge_dev_ops);
2681 	else
2682 		mutex_init(nge_log_mutex, NULL, MUTEX_DRIVER, NULL);
2683 
2684 	return (status);
2685 }
2686 
2687 int
2688 _fini(void)
2689 {
2690 	int status;
2691 
2692 	status = mod_remove(&modlinkage);
2693 	if (status == DDI_SUCCESS) {
2694 		mac_fini_ops(&nge_dev_ops);
2695 		mutex_destroy(nge_log_mutex);
2696 	}
2697 
2698 	return (status);
2699 }
2700 
2701 /*
2702  * ============ Init MSI/Fixed/SoftInterrupt routines ==============
2703  */
2704 
2705 /*
2706  * Register interrupts and initialize each mutex and condition variables
2707  */
2708 
2709 static int
2710 nge_register_intrs_and_init_locks(nge_t *ngep)
2711 {
2712 	int		err;
2713 	int		intr_types;
2714 	uint_t		soft_prip;
2715 	nge_msi_mask	msi_mask;
2716 	nge_msi_map0_vec map0_vec;
2717 	nge_msi_map1_vec map1_vec;
2718 
2719 	/*
2720 	 * Add the softint handlers:
2721 	 *
2722 	 * Both of these handlers are used to avoid restrictions on the
2723 	 * context and/or mutexes required for some operations.  In
2724 	 * particular, the hardware interrupt handler and its subfunctions
2725 	 * can detect a number of conditions that we don't want to handle
2726 	 * in that context or with that set of mutexes held.  So, these
2727 	 * softints are triggered instead:
2728 	 *
2729 	 * the <resched> softint is triggered if if we have previously
2730 	 * had to refuse to send a packet because of resource shortage
2731 	 * (we've run out of transmit buffers), but the send completion
2732 	 * interrupt handler has now detected that more buffers have
2733 	 * become available.  Its only purpose is to call gld_sched()
2734 	 * to retry the pending transmits (we're not allowed to hold
2735 	 * driver-defined mutexes across gld_sched()).
2736 	 *
2737 	 * the <factotum> is triggered if the h/w interrupt handler
2738 	 * sees the <link state changed> or <error> bits in the status
2739 	 * block.  It's also triggered periodically to poll the link
2740 	 * state, just in case we aren't getting link status change
2741 	 * interrupts ...
2742 	 */
2743 	err = ddi_intr_add_softint(ngep->devinfo, &ngep->resched_hdl,
2744 	    DDI_INTR_SOFTPRI_MIN, nge_reschedule, (caddr_t)ngep);
2745 	if (err != DDI_SUCCESS) {
2746 		nge_problem(ngep,
2747 		    "nge_attach: add nge_reschedule softintr failed");
2748 
2749 		return (DDI_FAILURE);
2750 	}
2751 	ngep->progress |= PROGRESS_RESCHED;
2752 	err = ddi_intr_add_softint(ngep->devinfo, &ngep->factotum_hdl,
2753 	    DDI_INTR_SOFTPRI_MIN, nge_chip_factotum, (caddr_t)ngep);
2754 	if (err != DDI_SUCCESS) {
2755 		nge_problem(ngep,
2756 		    "nge_attach: add nge_chip_factotum softintr failed!");
2757 
2758 		return (DDI_FAILURE);
2759 	}
2760 	if (ddi_intr_get_softint_pri(ngep->factotum_hdl, &soft_prip)
2761 	    != DDI_SUCCESS) {
2762 		nge_problem(ngep, "nge_attach: get softintr priority failed\n");
2763 
2764 		return (DDI_FAILURE);
2765 	}
2766 	ngep->soft_pri = soft_prip;
2767 
2768 	ngep->progress |= PROGRESS_FACTOTUM;
2769 	/* Get supported interrupt types */
2770 	if (ddi_intr_get_supported_types(ngep->devinfo, &intr_types)
2771 	    != DDI_SUCCESS) {
2772 		nge_error(ngep, "ddi_intr_get_supported_types failed\n");
2773 
2774 		return (DDI_FAILURE);
2775 	}
2776 
2777 	NGE_DEBUG(("ddi_intr_get_supported_types() returned: %x",
2778 	    intr_types));
2779 
2780 	if ((intr_types & DDI_INTR_TYPE_MSI) && nge_enable_msi) {
2781 
2782 		/* MSI Configurations for mcp55 chipset */
2783 		if (ngep->chipinfo.device == DEVICE_ID_MCP55_373 ||
2784 		    ngep->chipinfo.device == DEVICE_ID_MCP55_372) {
2785 
2786 
2787 			/* Enable the 8 vectors */
2788 			msi_mask.msi_mask_val =
2789 			    nge_reg_get32(ngep, NGE_MSI_MASK);
2790 			msi_mask.msi_msk_bits.vec0 = NGE_SET;
2791 			msi_mask.msi_msk_bits.vec1 = NGE_SET;
2792 			msi_mask.msi_msk_bits.vec2 = NGE_SET;
2793 			msi_mask.msi_msk_bits.vec3 = NGE_SET;
2794 			msi_mask.msi_msk_bits.vec4 = NGE_SET;
2795 			msi_mask.msi_msk_bits.vec5 = NGE_SET;
2796 			msi_mask.msi_msk_bits.vec6 = NGE_SET;
2797 			msi_mask.msi_msk_bits.vec7 = NGE_SET;
2798 			nge_reg_put32(ngep, NGE_MSI_MASK,
2799 			    msi_mask.msi_mask_val);
2800 
2801 			/*
2802 			 * Remapping the MSI MAP0 and MAP1. MCP55
2803 			 * is default mapping all the interrupt to 0 vector.
2804 			 * Software needs to remapping this.
2805 			 * This mapping is same as CK804.
2806 			 */
2807 			map0_vec.msi_map0_val =
2808 			    nge_reg_get32(ngep, NGE_MSI_MAP0);
2809 			map1_vec.msi_map1_val =
2810 			    nge_reg_get32(ngep, NGE_MSI_MAP1);
2811 			map0_vec.vecs_bits.reint_vec = 0;
2812 			map0_vec.vecs_bits.rcint_vec = 0;
2813 			map0_vec.vecs_bits.miss_vec = 3;
2814 			map0_vec.vecs_bits.teint_vec = 5;
2815 			map0_vec.vecs_bits.tcint_vec = 5;
2816 			map0_vec.vecs_bits.stint_vec = 2;
2817 			map0_vec.vecs_bits.mint_vec = 6;
2818 			map0_vec.vecs_bits.rfint_vec = 0;
2819 			map1_vec.vecs_bits.tfint_vec = 5;
2820 			map1_vec.vecs_bits.feint_vec = 6;
2821 			map1_vec.vecs_bits.resv8_11 = 3;
2822 			map1_vec.vecs_bits.resv12_15 = 1;
2823 			map1_vec.vecs_bits.resv16_19 = 0;
2824 			map1_vec.vecs_bits.resv20_23 = 7;
2825 			map1_vec.vecs_bits.resv24_31 = 0xff;
2826 			nge_reg_put32(ngep, NGE_MSI_MAP0,
2827 			    map0_vec.msi_map0_val);
2828 			nge_reg_put32(ngep, NGE_MSI_MAP1,
2829 			    map1_vec.msi_map1_val);
2830 		}
2831 		if (nge_add_intrs(ngep, DDI_INTR_TYPE_MSI) != DDI_SUCCESS) {
2832 			NGE_DEBUG(("MSI registration failed, "
2833 			    "trying FIXED interrupt type\n"));
2834 		} else {
2835 			nge_log(ngep, "Using MSI interrupt type\n");
2836 
2837 			ngep->intr_type = DDI_INTR_TYPE_MSI;
2838 			ngep->progress |= PROGRESS_SWINT;
2839 		}
2840 	}
2841 
2842 	if (!(ngep->progress & PROGRESS_SWINT) &&
2843 	    (intr_types & DDI_INTR_TYPE_FIXED)) {
2844 		if (nge_add_intrs(ngep, DDI_INTR_TYPE_FIXED) != DDI_SUCCESS) {
2845 			nge_error(ngep, "FIXED interrupt "
2846 			    "registration failed\n");
2847 
2848 			return (DDI_FAILURE);
2849 		}
2850 
2851 		nge_log(ngep, "Using FIXED interrupt type\n");
2852 
2853 		ngep->intr_type = DDI_INTR_TYPE_FIXED;
2854 		ngep->progress |= PROGRESS_SWINT;
2855 	}
2856 
2857 
2858 	if (!(ngep->progress & PROGRESS_SWINT)) {
2859 		nge_error(ngep, "No interrupts registered\n");
2860 
2861 		return (DDI_FAILURE);
2862 	}
2863 	mutex_init(ngep->genlock, NULL, MUTEX_DRIVER,
2864 	    DDI_INTR_PRI(ngep->intr_pri));
2865 	mutex_init(ngep->softlock, NULL, MUTEX_DRIVER,
2866 	    DDI_INTR_PRI(ngep->soft_pri));
2867 	rw_init(ngep->rwlock, NULL, RW_DRIVER,
2868 	    DDI_INTR_PRI(ngep->intr_pri));
2869 
2870 	return (DDI_SUCCESS);
2871 }
2872 
2873 /*
2874  * nge_add_intrs:
2875  *
2876  * Register FIXED or MSI interrupts.
2877  */
2878 static int
2879 nge_add_intrs(nge_t *ngep, int	intr_type)
2880 {
2881 	dev_info_t	*dip = ngep->devinfo;
2882 	int		avail, actual, intr_size, count = 0;
2883 	int		i, flag, ret;
2884 
2885 	NGE_DEBUG(("nge_add_intrs: interrupt type 0x%x\n", intr_type));
2886 
2887 	/* Get number of interrupts */
2888 	ret = ddi_intr_get_nintrs(dip, intr_type, &count);
2889 	if ((ret != DDI_SUCCESS) || (count == 0)) {
2890 		nge_error(ngep, "ddi_intr_get_nintrs() failure, ret: %d, "
2891 		    "count: %d", ret, count);
2892 
2893 		return (DDI_FAILURE);
2894 	}
2895 
2896 	/* Get number of available interrupts */
2897 	ret = ddi_intr_get_navail(dip, intr_type, &avail);
2898 	if ((ret != DDI_SUCCESS) || (avail == 0)) {
2899 		nge_error(ngep, "ddi_intr_get_navail() failure, "
2900 		    "ret: %d, avail: %d\n", ret, avail);
2901 
2902 		return (DDI_FAILURE);
2903 	}
2904 
2905 	if (avail < count) {
2906 		NGE_DEBUG(("nitrs() returned %d, navail returned %d\n",
2907 		    count, avail));
2908 	}
2909 	flag = DDI_INTR_ALLOC_NORMAL;
2910 
2911 	/* Allocate an array of interrupt handles */
2912 	intr_size = count * sizeof (ddi_intr_handle_t);
2913 	ngep->htable = kmem_alloc(intr_size, KM_SLEEP);
2914 
2915 	/* Call ddi_intr_alloc() */
2916 	ret = ddi_intr_alloc(dip, ngep->htable, intr_type, 0,
2917 	    count, &actual, flag);
2918 
2919 	if ((ret != DDI_SUCCESS) || (actual == 0)) {
2920 		nge_error(ngep, "ddi_intr_alloc() failed %d\n", ret);
2921 
2922 		kmem_free(ngep->htable, intr_size);
2923 		return (DDI_FAILURE);
2924 	}
2925 
2926 	if (actual < count) {
2927 		NGE_DEBUG(("Requested: %d, Received: %d\n",
2928 		    count, actual));
2929 	}
2930 
2931 	ngep->intr_actual_cnt = actual;
2932 	ngep->intr_req_cnt = count;
2933 
2934 	/*
2935 	 * Get priority for first msi, assume remaining are all the same
2936 	 */
2937 	if ((ret = ddi_intr_get_pri(ngep->htable[0], &ngep->intr_pri)) !=
2938 	    DDI_SUCCESS) {
2939 		nge_error(ngep, "ddi_intr_get_pri() failed %d\n", ret);
2940 
2941 		/* Free already allocated intr */
2942 		for (i = 0; i < actual; i++) {
2943 			(void) ddi_intr_free(ngep->htable[i]);
2944 		}
2945 
2946 		kmem_free(ngep->htable, intr_size);
2947 
2948 		return (DDI_FAILURE);
2949 	}
2950 	/* Test for high level mutex */
2951 	if (ngep->intr_pri >= ddi_intr_get_hilevel_pri()) {
2952 		nge_error(ngep, "nge_add_intrs:"
2953 		    "Hi level interrupt not supported");
2954 
2955 		for (i = 0; i < actual; i++)
2956 			(void) ddi_intr_free(ngep->htable[i]);
2957 
2958 		kmem_free(ngep->htable, intr_size);
2959 
2960 		return (DDI_FAILURE);
2961 	}
2962 
2963 
2964 	/* Call ddi_intr_add_handler() */
2965 	for (i = 0; i < actual; i++) {
2966 		if ((ret = ddi_intr_add_handler(ngep->htable[i], nge_chip_intr,
2967 		    (caddr_t)ngep, (caddr_t)(uintptr_t)i)) != DDI_SUCCESS) {
2968 			nge_error(ngep, "ddi_intr_add_handler() "
2969 			    "failed %d\n", ret);
2970 
2971 			/* Free already allocated intr */
2972 			for (i = 0; i < actual; i++) {
2973 				(void) ddi_intr_free(ngep->htable[i]);
2974 			}
2975 
2976 			kmem_free(ngep->htable, intr_size);
2977 
2978 			return (DDI_FAILURE);
2979 		}
2980 	}
2981 
2982 	if ((ret = ddi_intr_get_cap(ngep->htable[0], &ngep->intr_cap))
2983 	    != DDI_SUCCESS) {
2984 		nge_error(ngep, "ddi_intr_get_cap() failed %d\n", ret);
2985 
2986 		for (i = 0; i < actual; i++) {
2987 			(void) ddi_intr_remove_handler(ngep->htable[i]);
2988 			(void) ddi_intr_free(ngep->htable[i]);
2989 		}
2990 
2991 		kmem_free(ngep->htable, intr_size);
2992 
2993 		return (DDI_FAILURE);
2994 	}
2995 
2996 	return (DDI_SUCCESS);
2997 }
2998 
2999 /*
3000  * nge_rem_intrs:
3001  *
3002  * Unregister FIXED or MSI interrupts
3003  */
3004 static void
3005 nge_rem_intrs(nge_t *ngep)
3006 {
3007 	int	i;
3008 
3009 	NGE_DEBUG(("nge_rem_intrs\n"));
3010 
3011 	/* Disable all interrupts */
3012 	if (ngep->intr_cap & DDI_INTR_FLAG_BLOCK) {
3013 		/* Call ddi_intr_block_disable() */
3014 		(void) ddi_intr_block_disable(ngep->htable,
3015 		    ngep->intr_actual_cnt);
3016 	} else {
3017 		for (i = 0; i < ngep->intr_actual_cnt; i++) {
3018 			(void) ddi_intr_disable(ngep->htable[i]);
3019 		}
3020 	}
3021 
3022 	/* Call ddi_intr_remove_handler() */
3023 	for (i = 0; i < ngep->intr_actual_cnt; i++) {
3024 		(void) ddi_intr_remove_handler(ngep->htable[i]);
3025 		(void) ddi_intr_free(ngep->htable[i]);
3026 	}
3027 
3028 	kmem_free(ngep->htable,
3029 	    ngep->intr_req_cnt * sizeof (ddi_intr_handle_t));
3030 }
3031