xref: /illumos-gate/usr/src/uts/common/io/atge/atge_main.c (revision 4f60987d)
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 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/stream.h>
29 #include <sys/strsun.h>
30 #include <sys/stat.h>
31 #include <sys/modctl.h>
32 #include <sys/kstat.h>
33 #include <sys/ethernet.h>
34 #include <sys/devops.h>
35 #include <sys/debug.h>
36 #include <sys/conf.h>
37 #include <sys/mii.h>
38 #include <sys/miiregs.h>
39 #include <sys/mac.h>
40 #include <sys/mac_provider.h>
41 #include <sys/mac_ether.h>
42 #include <sys/sysmacros.h>
43 #include <sys/dditypes.h>
44 #include <sys/ddi.h>
45 #include <sys/sunddi.h>
46 #include <sys/byteorder.h>
47 #include <sys/note.h>
48 #include <sys/vlan.h>
49 #include <sys/strsubr.h>
50 #include <sys/crc32.h>
51 #include <sys/sdt.h>
52 #include <sys/pci.h>
53 #include <sys/pci_cap.h>
54 
55 #include "atge.h"
56 #include "atge_cmn_reg.h"
57 #include "atge_l1e_reg.h"
58 #include "atge_l1_reg.h"
59 
60 
61 /*
62  * Atheros/Attansic Ethernet chips are of three types - L1, L2 and L1E.
63  * This driver is for L1E/L1 but can be extended to support other chips.
64  * L1E comes in 1Gigabit and Fast Ethernet flavors. L1 comes in 1Gigabit
65  * flavors only.
66  *
67  * Atheros/Attansic Ethernet controllers have descriptor based TX and RX
68  * with an exception of L1E. L1E's RX side is not descriptor based ring.
69  * The L1E's RX uses pages (not to be confused with MMU pages) for
70  * receiving pkts. The header has four fields :
71  *
72  *        uint32_t seqno;    Sequence number of the frame.
73  *        uint32_t length;   Length of the frame.
74  *        uint32_t flags;    Flags
75  *        uint32_t vtag;     We don't use hardware VTAG.
76  *
77  * We use only one queue for RX (each queue can have two pages) and each
78  * page is L1E_RX_PAGE_SZ large in bytes. That's the reason we don't
79  * use zero-copy RX because we are limited to two pages and each page
80  * accomodates large number of pkts.
81  *
82  * The TX side on all three chips is descriptor based ring; and all the
83  * more reason to have one driver for these chips.
84  *
85  * We use two locks - atge_intr_lock and atge_tx_lock. Both the locks
86  * should be held if the operation has impact on the driver instance.
87  *
88  * All the three chips have hash-based multicast filter.
89  *
90  * We use CMB (Coalescing Message Block) for RX but not for TX as there
91  * are some issues with TX. RX CMB is used to get the last descriptor
92  * posted by the chip. Each CMB is for a RX page (one queue can have two
93  * pages) and are uint32_t (4 bytes) long.
94  *
95  * The descriptor table should have 32-bit physical address limit due to
96  * the limitation of having same high address for TX/RX/SMB/CMB. The
97  * TX/RX buffers can be 64-bit.
98  *
99  * Every DMA memory in atge is represented by atge_dma_t be it TX/RX Buffers
100  * or TX/RX descriptor table or SMB/CMB. To keep the code simple, we have
101  * kept sgl as 1 so that we get contingous pages from root complex.
102  *
103  * L1 chip (0x1048) uses descriptor based TX and RX ring. Most of registers are
104  * common with L1E chip (0x1026).
105  */
106 
107 /*
108  * Function Prototypes for debugging.
109  */
110 void	atge_error(dev_info_t *, char *, ...);
111 void	atge_debug_func(char *, ...);
112 
113 /*
114  * Function Prototypes for driver operations.
115  */
116 static int	atge_resume(dev_info_t *);
117 static int	atge_add_intr(atge_t *);
118 static int	atge_alloc_dma(atge_t *);
119 static void	atge_remove_intr(atge_t *);
120 static void	atge_free_dma(atge_t *);
121 static void	atge_device_reset(atge_t *);
122 static void	atge_device_init(atge_t *);
123 static void	atge_device_start(atge_t *);
124 static void	atge_disable_intrs(atge_t *);
125 atge_dma_t *atge_alloc_a_dma_blk(atge_t *, ddi_dma_attr_t *, int, int);
126 void	atge_free_a_dma_blk(atge_dma_t *);
127 static void	atge_rxfilter(atge_t *);
128 static void	atge_device_reset_l1_l1e(atge_t *);
129 void	atge_program_ether(atge_t *atgep);
130 void	atge_device_restart(atge_t *);
131 void	atge_device_stop(atge_t *);
132 static int	atge_send_a_packet(atge_t *, mblk_t *);
133 static uint32_t	atge_ether_crc(const uint8_t *, int);
134 
135 
136 /*
137  * L1E/L2E specific functions.
138  */
139 void	atge_l1e_device_reset(atge_t *);
140 void	atge_l1e_stop_mac(atge_t *);
141 int	atge_l1e_alloc_dma(atge_t *);
142 void	atge_l1e_free_dma(atge_t *);
143 void	atge_l1e_init_tx_ring(atge_t *);
144 void	atge_l1e_init_rx_pages(atge_t *);
145 void	atge_l1e_program_dma(atge_t *);
146 void	atge_l1e_send_packet(atge_ring_t *);
147 mblk_t	*atge_l1e_receive(atge_t *);
148 uint_t	atge_l1e_interrupt(caddr_t, caddr_t);
149 void	atge_l1e_gather_stats(atge_t *);
150 void	atge_l1e_clear_stats(atge_t *);
151 
152 /*
153  * L1 specific functions.
154  */
155 int	atge_l1_alloc_dma(atge_t *);
156 void	atge_l1_init_tx_ring(atge_t *);
157 void	atge_l1_init_rx_ring(atge_t *);
158 void	atge_l1_init_rr_ring(atge_t *);
159 void	atge_l1_init_cmb(atge_t *);
160 void	atge_l1_init_smb(atge_t *);
161 void	atge_l1_program_dma(atge_t *);
162 void	atge_l1_stop_tx_mac(atge_t *);
163 void	atge_l1_stop_rx_mac(atge_t *);
164 uint_t	atge_l1_interrupt(caddr_t, caddr_t);
165 void	atge_l1_send_packet(atge_ring_t *);
166 
167 
168 /*
169  * Function prototyps for MII operations.
170  */
171 uint16_t	atge_mii_read(void *, uint8_t, uint8_t);
172 void	atge_mii_write(void *, uint8_t, uint8_t, uint16_t);
173 void	atge_l1e_mii_reset(void *);
174 void	atge_l1_mii_reset(void *);
175 static void	atge_mii_notify(void *, link_state_t);
176 void	atge_tx_reclaim(atge_t *atgep, int cons);
177 
178 /*
179  * L1E/L2E chip.
180  */
181 static	mii_ops_t atge_l1e_mii_ops = {
182 	MII_OPS_VERSION,
183 	atge_mii_read,
184 	atge_mii_write,
185 	atge_mii_notify,
186 	atge_l1e_mii_reset
187 };
188 
189 /*
190  * L1 chip.
191  */
192 static	mii_ops_t atge_l1_mii_ops = {
193 	MII_OPS_VERSION,
194 	atge_mii_read,
195 	atge_mii_write,
196 	atge_mii_notify,
197 	atge_l1_mii_reset
198 };
199 
200 /*
201  * Function Prototypes for MAC callbacks.
202  */
203 static int	atge_m_stat(void *, uint_t, uint64_t *);
204 static int	atge_m_start(void *);
205 static void	atge_m_stop(void *);
206 static int	atge_m_getprop(void *, const char *, mac_prop_id_t, uint_t,
207     void *);
208 static int	atge_m_setprop(void *, const char *, mac_prop_id_t, uint_t,
209     const void *);
210 static void	atge_m_propinfo(void *, const char *, mac_prop_id_t,
211     mac_prop_info_handle_t);
212 static int	atge_m_unicst(void *, const uint8_t *);
213 static int	atge_m_multicst(void *, boolean_t, const uint8_t *);
214 static int	atge_m_promisc(void *, boolean_t);
215 static mblk_t	*atge_m_tx(void *, mblk_t *);
216 
217 static	mac_callbacks_t	atge_m_callbacks = {
218 	MC_SETPROP | MC_GETPROP | MC_PROPINFO,
219 	atge_m_stat,
220 	atge_m_start,
221 	atge_m_stop,
222 	atge_m_promisc,
223 	atge_m_multicst,
224 	atge_m_unicst,
225 	atge_m_tx,
226 	NULL,		/* mc_reserved */
227 	NULL,		/* mc_ioctl */
228 	NULL,		/* mc_getcapab */
229 	NULL,		/* mc_open */
230 	NULL,		/* mc_close */
231 	atge_m_setprop,
232 	atge_m_getprop,
233 	atge_m_propinfo
234 };
235 
236 /*
237  * DMA Data access requirements.
238  */
239 static struct ddi_device_acc_attr atge_dev_attr = {
240 	DDI_DEVICE_ATTR_V0,
241 	DDI_STRUCTURE_LE_ACC,
242 	DDI_STRICTORDER_ACC
243 };
244 
245 /*
246  * Buffers should be native endianness.
247  */
248 static struct ddi_device_acc_attr atge_buf_attr = {
249 	DDI_DEVICE_ATTR_V0,
250 	DDI_NEVERSWAP_ACC,	/* native endianness */
251 	DDI_STRICTORDER_ACC
252 };
253 
254 /*
255  * DMA device attributes. Buffer can be 64-bit.
256  */
257 static ddi_dma_attr_t atge_dma_attr_buf = {
258 	DMA_ATTR_V0,		/* dma_attr_version */
259 	0,			/* dma_attr_addr_lo */
260 	0x00ffffffffffull,	/* dma_attr_addr_hi */
261 	0x000000003fffull,	/* dma_attr_count_max */
262 	8,			/* dma_attr_align */
263 	0x00003ffc,		/* dma_attr_burstsizes */
264 	1,			/* dma_attr_minxfer */
265 	0x0000000027ffull,	/* dma_attr_maxxfer */
266 	0x0000ffffffffull,	/* dma_attr_seg */
267 	1,			/* dma_attr_sgllen */
268 	1,			/* dma_attr_granular */
269 	0			/* dma_attr_flags */
270 };
271 
272 /*
273  * Table of supported devices.
274  */
275 #define	ATGE_VENDOR_ID	0x1969
276 #define	ATGE_L1E_STR	"Atheros AR8121/8113/8114"
277 
278 static atge_cards_t atge_cards[] = {
279 	{ATGE_VENDOR_ID, ATGE_CHIP_L1E_DEV_ID, ATGE_L1E_STR, ATGE_CHIP_L1E},
280 	{ATGE_VENDOR_ID, ATGE_CHIP_L1_DEV_ID, "Attansic L1", ATGE_CHIP_L1},
281 };
282 
283 /*
284  * Global Debugging flag. Developer level debugging is done only in DEBUG mode.
285  */
286 int	atge_debug = 1;
287 
288 /*
289  * Debugging and error reporting.
290  */
291 void
292 atge_debug_func(char *fmt, ...)
293 {
294 	va_list	ap;
295 	char	buf[256];
296 
297 	va_start(ap, fmt);
298 	(void) vsnprintf(buf, sizeof (buf), fmt, ap);
299 	va_end(ap);
300 
301 	DTRACE_PROBE1(atge__debug, char *, buf);
302 }
303 
304 void
305 atge_error(dev_info_t *dip, char *fmt, ...)
306 {
307 	va_list	ap;
308 	char	buf[256];
309 
310 	va_start(ap, fmt);
311 	(void) vsnprintf(buf, sizeof (buf), fmt, ap);
312 	va_end(ap);
313 
314 	if (dip) {
315 		cmn_err(CE_WARN, "%s%d: %s",
316 		    ddi_driver_name(dip), ddi_get_instance(dip), buf);
317 	} else {
318 		cmn_err(CE_WARN, "atge: %s", buf);
319 	}
320 }
321 
322 void
323 atge_mac_config(atge_t *atgep)
324 {
325 	uint32_t reg;
326 	int speed;
327 	link_duplex_t ld;
328 
329 	reg = INL(atgep, ATGE_MAC_CFG);
330 	reg &= ~(ATGE_CFG_FULL_DUPLEX | ATGE_CFG_TX_FC | ATGE_CFG_RX_FC |
331 	    ATGE_CFG_SPEED_MASK);
332 
333 	speed = mii_get_speed(atgep->atge_mii);
334 	switch (speed) {
335 	case 10:
336 	case 100:
337 		reg |= ATGE_CFG_SPEED_10_100;
338 		break;
339 	case 1000:
340 		reg |= ATGE_CFG_SPEED_1000;
341 		break;
342 	}
343 
344 	ld = mii_get_duplex(atgep->atge_mii);
345 	if (ld == LINK_DUPLEX_FULL)
346 		reg |= ATGE_CFG_FULL_DUPLEX;
347 
348 	/* Re-enable TX/RX MACs */
349 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E) {
350 		reg |= ATGE_CFG_TX_ENB | ATGE_CFG_RX_ENB | ATGE_CFG_RX_FC;
351 	} else if (ATGE_MODEL(atgep) == ATGE_CHIP_L1) {
352 		reg |= ATGE_CFG_TX_ENB | ATGE_CFG_RX_ENB;
353 	}
354 
355 	OUTL(atgep, ATGE_MAC_CFG, reg);
356 
357 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E) {
358 		reg = ATGE_USECS(ATGE_IM_RX_TIMER_DEFAULT) << IM_TIMER_RX_SHIFT;
359 		reg |= ATGE_USECS(ATGE_IM_TX_TIMER_DEFAULT) <<
360 		    IM_TIMER_TX_SHIFT;
361 		OUTL(atgep, ATGE_IM_TIMER, reg);
362 	}
363 
364 	ATGE_DB(("%s: %s() mac_cfg is : %x",
365 	    atgep->atge_name, __func__, INL(atgep, ATGE_MAC_CFG)));
366 }
367 
368 static void
369 atge_mii_notify(void *arg, link_state_t link)
370 {
371 	atge_t *atgep = arg;
372 
373 	ATGE_DB(("%s: %s() LINK STATUS CHANGED from %x -> %x",
374 	    atgep->atge_name, __func__, atgep->atge_link_state, link));
375 
376 	mac_link_update(atgep->atge_mh, link);
377 
378 	/*
379 	 * Reconfigure MAC if link status is UP now.
380 	 */
381 	mutex_enter(&atgep->atge_tx_lock);
382 	if (link == LINK_STATE_UP) {
383 		atgep->atge_link_state = LINK_STATE_UP;
384 		atge_mac_config(atgep);
385 		atgep->atge_tx_resched = 0;
386 	} else {
387 		atgep->atge_link_state = LINK_STATE_DOWN;
388 		atgep->atge_flags |= ATGE_MII_CHECK;
389 	}
390 
391 	mutex_exit(&atgep->atge_tx_lock);
392 
393 	if (link == LINK_STATE_UP)
394 		mac_tx_update(atgep->atge_mh);
395 }
396 
397 void
398 atge_tx_reclaim(atge_t *atgep, int end)
399 {
400 	atge_tx_desc_t  *txd;
401 	atge_ring_t *r = atgep->atge_tx_ring;
402 	uchar_t *c;
403 	int start;
404 
405 	ASSERT(MUTEX_HELD(&atgep->atge_tx_lock));
406 	ASSERT(r != NULL);
407 
408 	start = r->r_consumer;
409 
410 	if (start == end)
411 		return;
412 
413 	while (start != end) {
414 		r->r_avail_desc++;
415 		if (r->r_avail_desc > ATGE_TX_RING_CNT) {
416 
417 			atge_error(atgep->atge_dip,
418 			    "Reclaim : TX descriptor error");
419 
420 			if (r->r_avail_desc > (ATGE_TX_RING_CNT + 5)) {
421 				atge_device_stop(atgep);
422 				break;
423 			}
424 		}
425 
426 		c = (uchar_t *)r->r_desc_ring->addr;
427 		c += (sizeof (atge_tx_desc_t) * start);
428 		txd = (atge_tx_desc_t *)c;
429 
430 		/*
431 		 * Clearing TX descriptor helps in debugging some strange
432 		 * problems.
433 		 */
434 		txd->addr = 0;
435 		txd->len = 0;
436 		txd->flags = 0;
437 
438 		ATGE_INC_SLOT(start, ATGE_TX_RING_CNT);
439 	}
440 
441 	atgep->atge_tx_ring->r_consumer = start;
442 
443 	DMA_SYNC(r->r_desc_ring, 0, ATGE_TX_RING_SZ, DDI_DMA_SYNC_FORDEV);
444 }
445 
446 /*
447  * Adds interrupt handler depending upon the type of interrupt supported by
448  * the chip.
449  */
450 static int
451 atge_add_intr_handler(atge_t *atgep, int intr_type)
452 {
453 	int err;
454 	int count = 0;
455 	int avail = 0;
456 	int i;
457 	int flag;
458 
459 	if (intr_type != DDI_INTR_TYPE_FIXED) {
460 		err = ddi_intr_get_nintrs(atgep->atge_dip, intr_type, &count);
461 		if (err != DDI_SUCCESS) {
462 			atge_error(atgep->atge_dip,
463 			    "ddi_intr_get_nintrs failed : %d", err);
464 			return (DDI_FAILURE);
465 		}
466 
467 		ATGE_DB(("%s: %s() count : %d",
468 		    atgep->atge_name, __func__, count));
469 
470 		err = ddi_intr_get_navail(atgep->atge_dip, intr_type, &avail);
471 		if (err != DDI_SUCCESS) {
472 			atge_error(atgep->atge_dip,
473 			    "ddi_intr_get_navail failed : %d", err);
474 			return (DDI_FAILURE);
475 		}
476 
477 		if (avail < count) {
478 			atge_error(atgep->atge_dip, "count :%d,"
479 			    " avail : %d", count, avail);
480 		}
481 
482 		flag = DDI_INTR_ALLOC_STRICT;
483 	} else {
484 		/*
485 		 * DDI_INTR_TYPE_FIXED case.
486 		 */
487 		count = 1;
488 		avail = 1;
489 		flag = DDI_INTR_ALLOC_NORMAL;
490 	}
491 
492 	atgep->atge_intr_size = avail * sizeof (ddi_intr_handle_t);
493 	atgep->atge_intr_handle = kmem_zalloc(atgep->atge_intr_size, KM_SLEEP);
494 
495 	ATGE_DB(("%s: %s() avail:%d, count : %d, type : %d",
496 	    atgep->atge_name, __func__, avail, count,
497 	    intr_type));
498 
499 	err = ddi_intr_alloc(atgep->atge_dip, atgep->atge_intr_handle,
500 	    intr_type, 0, avail, &atgep->atge_intr_cnt, flag);
501 
502 	if (err != DDI_SUCCESS) {
503 		atge_error(atgep->atge_dip, "ddi_intr_alloc failed : %d", err);
504 		kmem_free(atgep->atge_intr_handle, atgep->atge_intr_size);
505 		return (DDI_FAILURE);
506 	}
507 
508 	ATGE_DB(("%s: atge_add_intr_handler() after alloc count"
509 	    " :%d, avail : %d", atgep->atge_name, count, avail));
510 
511 	err = ddi_intr_get_pri(atgep->atge_intr_handle[0],
512 	    &atgep->atge_intr_pri);
513 	if (err != DDI_SUCCESS) {
514 		atge_error(atgep->atge_dip, "ddi_intr_get_pri failed:%d", err);
515 		for (i = 0; i < atgep->atge_intr_cnt; i++) {
516 			(void) ddi_intr_free(atgep->atge_intr_handle[i]);
517 		}
518 		kmem_free(atgep->atge_intr_handle, atgep->atge_intr_size);
519 
520 		return (DDI_FAILURE);
521 	}
522 
523 	/*
524 	 * Add interrupt handler now.
525 	 */
526 	for (i = 0; i < atgep->atge_intr_cnt; i++) {
527 		if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E) {
528 			err = ddi_intr_add_handler(atgep->atge_intr_handle[i],
529 			    atge_l1e_interrupt, atgep, (caddr_t)(uintptr_t)i);
530 		} else if (ATGE_MODEL(atgep) == ATGE_CHIP_L1) {
531 			err = ddi_intr_add_handler(atgep->atge_intr_handle[i],
532 			    atge_l1_interrupt, atgep, (caddr_t)(uintptr_t)i);
533 		}
534 
535 		if (err != DDI_SUCCESS) {
536 			atge_error(atgep->atge_dip,
537 			    "ddi_intr_add_handler failed : %d", err);
538 
539 			(void) ddi_intr_free(atgep->atge_intr_handle[i]);
540 			while (--i >= 0) {
541 				(void) ddi_intr_remove_handler(
542 				    atgep->atge_intr_handle[i]);
543 				(void) ddi_intr_free(
544 				    atgep->atge_intr_handle[i]);
545 			}
546 
547 			kmem_free(atgep->atge_intr_handle,
548 			    atgep->atge_intr_size);
549 
550 			return (DDI_FAILURE);
551 		}
552 	}
553 
554 	err = ddi_intr_get_cap(atgep->atge_intr_handle[0],
555 	    &atgep->atge_intr_cap);
556 
557 	if (err != DDI_SUCCESS) {
558 		atge_error(atgep->atge_dip,
559 		    "ddi_intr_get_cap failed : %d", err);
560 		atge_remove_intr(atgep);
561 		return (DDI_FAILURE);
562 	}
563 
564 	if (intr_type == DDI_INTR_TYPE_FIXED)
565 		atgep->atge_flags |= ATGE_FIXED_TYPE;
566 	else if (intr_type == DDI_INTR_TYPE_MSI)
567 		atgep->atge_flags |= ATGE_MSI_TYPE;
568 	else if (intr_type == DDI_INTR_TYPE_MSIX)
569 		atgep->atge_flags |= ATGE_MSIX_TYPE;
570 
571 	return (DDI_SUCCESS);
572 }
573 
574 void
575 atge_remove_intr(atge_t *atgep)
576 {
577 	int i;
578 	int cap = 0;
579 
580 	if (atgep->atge_intr_handle == NULL)
581 		return;
582 
583 	if (atgep->atge_intr_cap & DDI_INTR_FLAG_BLOCK) {
584 		(void) ddi_intr_block_disable(atgep->atge_intr_handle,
585 		    atgep->atge_intr_cnt);
586 
587 		cap = 1;
588 	}
589 
590 	for (i = 0; i < atgep->atge_intr_cnt; i++) {
591 		if (cap == 0)
592 			(void) ddi_intr_disable(atgep->atge_intr_handle[i]);
593 
594 		(void) ddi_intr_remove_handler(atgep->atge_intr_handle[i]);
595 		(void) ddi_intr_free(atgep->atge_intr_handle[i]);
596 	}
597 
598 	kmem_free(atgep->atge_intr_handle, atgep->atge_intr_size);
599 }
600 
601 int
602 atge_enable_intrs(atge_t *atgep)
603 {
604 	int err;
605 	int i;
606 
607 	if (atgep->atge_intr_cap & DDI_INTR_FLAG_BLOCK) {
608 		/*
609 		 * Do block enable.
610 		 */
611 		err = ddi_intr_block_enable(atgep->atge_intr_handle,
612 		    atgep->atge_intr_cnt);
613 
614 		if (err != DDI_SUCCESS) {
615 			atge_error(atgep->atge_dip,
616 			    "Failed to block enable intrs %d", err);
617 			err = DDI_FAILURE;
618 		} else {
619 			err = DDI_SUCCESS;
620 		}
621 	} else {
622 		/*
623 		 * Call ddi_intr_enable() for MSI non-block enable.
624 		 */
625 		for (i = 0; i < atgep->atge_intr_cnt; i++) {
626 			err = ddi_intr_enable(atgep->atge_intr_handle[i]);
627 			if (err != DDI_SUCCESS) {
628 				atge_error(atgep->atge_dip,
629 				    "Failed to enable intrs on %d with : %d",
630 				    i, err);
631 				break;
632 			}
633 		}
634 
635 		if (err == DDI_SUCCESS)
636 			err = DDI_SUCCESS;
637 		else
638 			err = DDI_FAILURE;
639 	}
640 
641 	return (err);
642 }
643 
644 /*
645  * Adds interrupt handler depending on the supported interrupt type by the
646  * chip.
647  */
648 static int
649 atge_add_intr(atge_t *atgep)
650 {
651 	int	err;
652 
653 	/*
654 	 * Get the supported interrupt types.
655 	 */
656 	err = ddi_intr_get_supported_types(atgep->atge_dip,
657 	    &atgep->atge_intr_types);
658 	if (err != DDI_SUCCESS) {
659 		atge_error(atgep->atge_dip,
660 		    "ddi_intr_get_supported_types failed : %d", err);
661 		return (DDI_FAILURE);
662 	}
663 
664 	ATGE_DB(("%s: ddi_intr_get_supported_types() returned : %d",
665 	    atgep->atge_name, atgep->atge_intr_types));
666 
667 
668 	if (atgep->atge_intr_types & DDI_INTR_TYPE_MSIX) {
669 		err = atge_add_intr_handler(atgep, DDI_INTR_TYPE_MSIX);
670 		if (err == DDI_SUCCESS) {
671 			ATGE_DB(("%s: Using MSIx for interrupt",
672 			    atgep->atge_name));
673 			return (err);
674 		}
675 	}
676 
677 	if (atgep->atge_intr_types & DDI_INTR_TYPE_MSI) {
678 		err = atge_add_intr_handler(atgep, DDI_INTR_TYPE_MSI);
679 		if (err == DDI_SUCCESS) {
680 			ATGE_DB(("%s: Using MSI for interrupt",
681 			    atgep->atge_name));
682 			return (err);
683 		}
684 	}
685 
686 	err = DDI_FAILURE;
687 	if (atgep->atge_intr_types & DDI_INTR_TYPE_FIXED) {
688 		err = atge_add_intr_handler(atgep, DDI_INTR_TYPE_FIXED);
689 		if (err == DDI_SUCCESS) {
690 			ATGE_DB(("%s: Using FIXED type for interrupt",
691 			    atgep->atge_name));
692 			return (err);
693 		}
694 	}
695 
696 	return (err);
697 }
698 
699 int
700 atge_identify_hardware(atge_t *atgep)
701 {
702 	uint16_t vid, did;
703 	int i;
704 
705 	vid = pci_config_get16(atgep->atge_conf_handle, PCI_CONF_VENID);
706 	did = pci_config_get16(atgep->atge_conf_handle, PCI_CONF_DEVID);
707 
708 	atgep->atge_model = 0;
709 	for (i = 0; i < (sizeof (atge_cards) / sizeof (atge_cards_t)); i++) {
710 		if (atge_cards[i].vendor_id == vid &&
711 		    atge_cards[i].device_id == did) {
712 			atgep->atge_model = atge_cards[i].model;
713 			atgep->atge_revid =
714 			    pci_config_get8(atgep->atge_conf_handle,
715 			    PCI_CONF_REVID);
716 			ATGE_DB(("%s: %s : PCI-ID pci%x,%x and model : %d",
717 			    atgep->atge_name, __func__, vid, did,
718 			    atgep->atge_model));
719 
720 			return (DDI_SUCCESS);
721 		}
722 	}
723 
724 	atge_error(atgep->atge_dip, "atge driver is attaching to unknown"
725 	    " pci%d,%d vendor/device-id card", vid, did);
726 
727 	/*
728 	 * Assume it's L1 chip.
729 	 */
730 	atgep->atge_model = ATGE_CHIP_L1;
731 	atgep->atge_revid = pci_config_get8(atgep->atge_conf_handle,
732 	    PCI_CONF_REVID);
733 
734 	/*
735 	 * We will leave the decision to caller.
736 	 */
737 	return (DDI_FAILURE);
738 }
739 
740 int
741 atge_get_macaddr(atge_t *atgep)
742 {
743 	uint32_t reg;
744 
745 	reg = INL(atgep, ATGE_SPI_CTRL);
746 	if ((reg & SPI_VPD_ENB) != 0) {
747 		/*
748 		 * Get VPD stored in TWSI EEPROM.
749 		 */
750 		reg &= ~SPI_VPD_ENB;
751 		OUTL(atgep, ATGE_SPI_CTRL, reg);
752 
753 		ATGE_DB(("%s: %s called Get VPD", atgep->atge_name, __func__));
754 	}
755 
756 	atgep->atge_ether_addr[5] = INB(atgep, ATGE_PAR0 + 0);
757 	atgep->atge_ether_addr[4] = INB(atgep, ATGE_PAR0 + 1);
758 	atgep->atge_ether_addr[3] = INB(atgep, ATGE_PAR0 + 2);
759 	atgep->atge_ether_addr[2] = INB(atgep, ATGE_PAR0 + 3);
760 	atgep->atge_ether_addr[1] = INB(atgep, ATGE_PAR1 + 0);
761 	atgep->atge_ether_addr[0] = INB(atgep, ATGE_PAR1 + 1);
762 
763 	ATGE_DB(("%s: %s() Station Address - %x:%x:%x:%x:%x:%x",
764 	    atgep->atge_name, __func__,
765 	    atgep->atge_ether_addr[0],
766 	    atgep->atge_ether_addr[1],
767 	    atgep->atge_ether_addr[2],
768 	    atgep->atge_ether_addr[3],
769 	    atgep->atge_ether_addr[4],
770 	    atgep->atge_ether_addr[5]));
771 
772 	bcopy(atgep->atge_ether_addr, atgep->atge_dev_addr, ETHERADDRL);
773 
774 	return (DDI_SUCCESS);
775 }
776 
777 /*
778  * Reset functionality for L1 and L1E. It's same.
779  */
780 static void
781 atge_device_reset(atge_t *atgep)
782 {
783 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E ||
784 	    ATGE_MODEL(atgep) == ATGE_CHIP_L1)
785 		atge_device_reset_l1_l1e(atgep);
786 }
787 
788 void
789 atge_device_reset_l1_l1e(atge_t *atgep)
790 {
791 	uint32_t reg;
792 	int t;
793 
794 	OUTL(atgep, ATGE_MASTER_CFG, MASTER_RESET);
795 	reg = INL(atgep, ATGE_MASTER_CFG);
796 	for (t = ATGE_RESET_TIMEOUT; t > 0; t--) {
797 		drv_usecwait(10);
798 		reg = INL(atgep, ATGE_MASTER_CFG);
799 		if ((reg & MASTER_RESET) == 0)
800 			break;
801 	}
802 
803 	if (t == 0) {
804 		atge_error(atgep->atge_dip, " master reset timeout reg : %x",
805 		    reg);
806 	}
807 
808 	for (t = ATGE_RESET_TIMEOUT; t > 0; t--) {
809 		if ((reg = INL(atgep, ATGE_IDLE_STATUS)) == 0)
810 			break;
811 
812 		drv_usecwait(10);
813 	}
814 
815 	if (t == 0) {
816 		atge_error(atgep->atge_dip, "device reset timeout reg : %x",
817 		    reg);
818 	}
819 
820 	/*
821 	 * Initialize PCIe module. These values came from FreeBSD and
822 	 * we don't know the meaning of it.
823 	 */
824 	OUTL(atgep, 0x12FC, 0x6500);
825 	reg = INL(atgep, 0x1008) | 0x8000;
826 	OUTL(atgep, 0x1008, reg);
827 
828 	/*
829 	 * Get chip revision.
830 	 */
831 	atgep->atge_chip_rev = INL(atgep, ATGE_MASTER_CFG) >>
832 	    MASTER_CHIP_REV_SHIFT;
833 
834 	ATGE_DB(("%s: %s reset successfully rev : %x", atgep->atge_name,
835 	    __func__, atgep->atge_chip_rev));
836 }
837 
838 /*
839  * DMA allocation for L1 and L1E is bit different since L1E uses RX pages
840  * instead of descriptor based RX model.
841  */
842 static int
843 atge_alloc_dma(atge_t *atgep)
844 {
845 	int err = DDI_FAILURE;
846 
847 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E) {
848 		err = atge_l1e_alloc_dma(atgep);
849 	} else if (ATGE_MODEL(atgep) == ATGE_CHIP_L1) {
850 		err = atge_l1_alloc_dma(atgep);
851 	}
852 
853 	return (err);
854 }
855 
856 static void
857 atge_free_dma(atge_t *atgep)
858 {
859 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E) {
860 		atge_l1e_free_dma(atgep);
861 	}
862 }
863 
864 /*
865  * Attach entry point in the driver.
866  */
867 static int
868 atge_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
869 {
870 	atge_t	*atgep;
871 	mac_register_t	*macreg;
872 	int	instance;
873 	uint16_t cap_ptr;
874 	uint16_t burst;
875 	int err;
876 	mii_ops_t *mii_ops;
877 
878 	instance =  ddi_get_instance(devinfo);
879 
880 	switch (cmd) {
881 	default:
882 		return (DDI_FAILURE);
883 
884 	case DDI_RESUME:
885 		return (atge_resume(devinfo));
886 
887 	case DDI_ATTACH:
888 		ddi_set_driver_private(devinfo, NULL);
889 		break;
890 	}
891 
892 	atgep = kmem_zalloc(sizeof (atge_t), KM_SLEEP);
893 	ddi_set_driver_private(devinfo, atgep);
894 	atgep->atge_dip = devinfo;
895 
896 	/*
897 	 * Setup name and instance number to be used for debugging and
898 	 * error reporting.
899 	 */
900 	(void) snprintf(atgep->atge_name, sizeof (atgep->atge_name), "%s%d",
901 	    "atge", instance);
902 
903 
904 	/*
905 	 * Map PCI config space.
906 	 */
907 	err = pci_config_setup(devinfo, &atgep->atge_conf_handle);
908 	if (err != DDI_SUCCESS) {
909 		atge_error(devinfo, "pci_config_setup() failed");
910 		goto fail1;
911 	}
912 
913 	(void) atge_identify_hardware(atgep);
914 
915 	/*
916 	 * Map Device registers.
917 	 */
918 	err = ddi_regs_map_setup(devinfo, ATGE_PCI_REG_NUMBER,
919 	    &atgep->atge_io_regs, 0, 0, &atge_dev_attr, &atgep->atge_io_handle);
920 	if (err != DDI_SUCCESS) {
921 		atge_error(devinfo, "ddi_regs_map_setup() failed");
922 		goto fail2;
923 	}
924 
925 	/*
926 	 * Add interrupt and its associated handler.
927 	 */
928 	err = atge_add_intr(atgep);
929 	if (err != DDI_SUCCESS) {
930 		atge_error(devinfo, "Failed to add interrupt handler");
931 		goto fail3;
932 	}
933 
934 	mutex_init(&atgep->atge_intr_lock, NULL, MUTEX_DRIVER,
935 	    DDI_INTR_PRI(atgep->atge_intr_pri));
936 
937 	mutex_init(&atgep->atge_tx_lock, NULL, MUTEX_DRIVER,
938 	    DDI_INTR_PRI(atgep->atge_intr_pri));
939 
940 	mutex_init(&atgep->atge_rx_lock, NULL, MUTEX_DRIVER,
941 	    DDI_INTR_PRI(atgep->atge_intr_pri));
942 
943 	mutex_init(&atgep->atge_mii_lock, NULL, MUTEX_DRIVER, NULL);
944 
945 	/*
946 	 * Used to lock down MBOX register on L1 chip since RX consumer,
947 	 * TX producer and RX return ring consumer are shared.
948 	 */
949 	mutex_init(&atgep->atge_mbox_lock, NULL, MUTEX_DRIVER,
950 	    DDI_INTR_PRI(atgep->atge_intr_pri));
951 
952 	atgep->atge_link_state = LINK_STATE_DOWN;
953 	atgep->atge_mtu = ETHERMTU;
954 
955 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E) {
956 		if (atgep->atge_revid > 0xF0) {
957 			/* L2E Rev. B. AR8114 */
958 			atgep->atge_flags |= ATGE_FLAG_FASTETHER;
959 		} else {
960 			if ((INL(atgep, L1E_PHY_STATUS) &
961 			    PHY_STATUS_100M) != 0) {
962 				/* L1E AR8121 */
963 				atgep->atge_flags |= ATGE_FLAG_JUMBO;
964 			} else {
965 				/* L2E Rev. A. AR8113 */
966 				atgep->atge_flags |= ATGE_FLAG_FASTETHER;
967 			}
968 		}
969 	}
970 
971 	/*
972 	 * Get DMA parameters from PCIe device control register.
973 	 */
974 	err = PCI_CAP_LOCATE(atgep->atge_conf_handle, PCI_CAP_ID_PCI_E,
975 	    &cap_ptr);
976 
977 	if (err == DDI_FAILURE) {
978 		atgep->atge_dma_rd_burst = DMA_CFG_RD_BURST_128;
979 		atgep->atge_dma_wr_burst = DMA_CFG_WR_BURST_128;
980 	} else {
981 		atgep->atge_flags |= ATGE_FLAG_PCIE;
982 		burst = pci_config_get16(atgep->atge_conf_handle,
983 		    cap_ptr + 0x08);
984 
985 		/*
986 		 * Max read request size.
987 		 */
988 		atgep->atge_dma_rd_burst = ((burst >> 12) & 0x07) <<
989 		    DMA_CFG_RD_BURST_SHIFT;
990 
991 		/*
992 		 * Max Payload Size.
993 		 */
994 		atgep->atge_dma_wr_burst = ((burst >> 5) & 0x07) <<
995 		    DMA_CFG_WR_BURST_SHIFT;
996 
997 		ATGE_DB(("%s: %s() MRR : %d, MPS : %d",
998 		    atgep->atge_name, __func__,
999 		    (128 << ((burst >> 12) & 0x07)),
1000 		    (128 << ((burst >> 5) & 0x07))));
1001 	}
1002 
1003 	/*
1004 	 * Allocate DMA resources.
1005 	 */
1006 	err = atge_alloc_dma(atgep);
1007 	if (err != DDI_SUCCESS) {
1008 		atge_error(devinfo, "Failed to allocate DMA resources");
1009 		goto fail4;
1010 	}
1011 
1012 	/*
1013 	 * Get station address.
1014 	 */
1015 	(void) atge_get_macaddr(atgep);
1016 
1017 	/*
1018 	 * Setup MII.
1019 	 */
1020 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E) {
1021 		mii_ops = &atge_l1e_mii_ops;
1022 	} else if (ATGE_MODEL(atgep) == ATGE_CHIP_L1) {
1023 		mii_ops = &atge_l1_mii_ops;
1024 	}
1025 
1026 	if ((atgep->atge_mii = mii_alloc(atgep, devinfo,
1027 	    mii_ops)) == NULL) {
1028 		atge_error(devinfo, "mii_alloc() failed");
1029 		goto fail4;
1030 	}
1031 
1032 	/*
1033 	 * Register with MAC layer.
1034 	 */
1035 	if ((macreg = mac_alloc(MAC_VERSION)) == NULL) {
1036 		atge_error(devinfo, "mac_alloc() failed due to version");
1037 		goto fail4;
1038 	}
1039 
1040 	macreg->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
1041 	macreg->m_driver = atgep;
1042 	macreg->m_dip = devinfo;
1043 	macreg->m_instance = instance;
1044 	macreg->m_src_addr = atgep->atge_ether_addr;
1045 	macreg->m_callbacks = &atge_m_callbacks;
1046 	macreg->m_min_sdu = 0;
1047 	macreg->m_max_sdu = atgep->atge_mtu;
1048 	macreg->m_margin = VLAN_TAGSZ;
1049 
1050 	if ((err = mac_register(macreg, &atgep->atge_mh)) != 0) {
1051 		atge_error(devinfo, "mac_register() failed with :%d", err);
1052 		mac_free(macreg);
1053 		goto fail4;
1054 	}
1055 
1056 	mac_free(macreg);
1057 
1058 	ATGE_DB(("%s: %s() driver attached successfully",
1059 	    atgep->atge_name, __func__));
1060 
1061 	atge_device_reset(atgep);
1062 
1063 	atgep->atge_chip_state = ATGE_CHIP_INITIALIZED;
1064 
1065 	/*
1066 	 * At last - enable interrupts.
1067 	 */
1068 	err = atge_enable_intrs(atgep);
1069 	if (err == DDI_FAILURE) {
1070 		goto fail5;
1071 	}
1072 
1073 	/*
1074 	 * Reset the PHY before starting.
1075 	 */
1076 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E) {
1077 		atge_l1e_mii_reset(atgep);
1078 	} else if (ATGE_MODEL(atgep) == ATGE_CHIP_L1) {
1079 		atge_l1_mii_reset(atgep);
1080 	}
1081 
1082 	/*
1083 	 * Let the PHY run.
1084 	 */
1085 	mii_start(atgep->atge_mii);
1086 
1087 	return (DDI_SUCCESS);
1088 
1089 fail5:
1090 	(void) mac_unregister(atgep->atge_mh);
1091 	atge_device_stop(atgep);
1092 	mii_stop(atgep->atge_mii);
1093 	mii_free(atgep->atge_mii);
1094 fail4:
1095 	atge_free_dma(atgep);
1096 	mutex_destroy(&atgep->atge_intr_lock);
1097 	mutex_destroy(&atgep->atge_tx_lock);
1098 	mutex_destroy(&atgep->atge_rx_lock);
1099 	atge_remove_intr(atgep);
1100 fail3:
1101 	ddi_regs_map_free(&atgep->atge_io_handle);
1102 fail2:
1103 	pci_config_teardown(&atgep->atge_conf_handle);
1104 fail1:
1105 	if (atgep)
1106 		kmem_free(atgep, sizeof (atge_t));
1107 
1108 	return (DDI_FAILURE);
1109 }
1110 
1111 static int
1112 atge_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
1113 {
1114 	atge_t	*atgep;
1115 
1116 	atgep = ddi_get_driver_private(dip);
1117 	if (atgep == NULL) {
1118 		atge_error(dip, "No soft state in detach");
1119 		return (DDI_FAILURE);
1120 	}
1121 
1122 	switch (cmd) {
1123 	case DDI_DETACH:
1124 		mii_stop(atgep->atge_mii);
1125 
1126 		/*
1127 		 * First unregister with MAC layer before stopping DMA
1128 		 */
1129 		if (mac_unregister(atgep->atge_mh) != DDI_SUCCESS)
1130 			return (DDI_FAILURE);
1131 
1132 		atgep->atge_mh = NULL;
1133 
1134 		mutex_enter(&atgep->atge_intr_lock);
1135 		mutex_enter(&atgep->atge_tx_lock);
1136 		atge_device_stop(atgep);
1137 		mutex_exit(&atgep->atge_tx_lock);
1138 		mutex_exit(&atgep->atge_intr_lock);
1139 
1140 		mii_free(atgep->atge_mii);
1141 		atge_free_dma(atgep);
1142 
1143 		ddi_regs_map_free(&atgep->atge_io_handle);
1144 		atge_remove_intr(atgep);
1145 		pci_config_teardown(&atgep->atge_conf_handle);
1146 
1147 		mutex_destroy(&atgep->atge_intr_lock);
1148 		mutex_destroy(&atgep->atge_tx_lock);
1149 		mutex_destroy(&atgep->atge_rx_lock);
1150 		kmem_free(atgep, sizeof (atge_t));
1151 		ddi_set_driver_private(dip, NULL);
1152 
1153 		return (DDI_SUCCESS);
1154 
1155 	case DDI_SUSPEND:
1156 		ATGE_DB(("%s: %s() is being suspended",
1157 		    atgep->atge_name, __func__));
1158 
1159 		/*
1160 		 * Suspend monitoring MII.
1161 		 */
1162 		mii_suspend(atgep->atge_mii);
1163 
1164 		mutex_enter(&atgep->atge_intr_lock);
1165 		mutex_enter(&atgep->atge_tx_lock);
1166 		atgep->atge_chip_state |= ATGE_CHIP_SUSPENDED;
1167 		atge_device_stop(atgep);
1168 		mutex_exit(&atgep->atge_tx_lock);
1169 		mutex_exit(&atgep->atge_intr_lock);
1170 
1171 		return (DDI_SUCCESS);
1172 
1173 	default:
1174 		return (DDI_FAILURE);
1175 	}
1176 }
1177 
1178 int
1179 atge_alloc_buffers(atge_ring_t *r, size_t rcnt, size_t buflen, int f)
1180 {
1181 	atge_dma_t *dma;
1182 	atge_dma_t **tbl;
1183 	int err = DDI_SUCCESS;
1184 	int i;
1185 
1186 	tbl = kmem_zalloc(rcnt * sizeof (atge_dma_t *), KM_SLEEP);
1187 	r->r_buf_tbl = tbl;
1188 
1189 	for (i = 0; i < rcnt; i++) {
1190 		dma = atge_buf_alloc(r->r_atge, buflen, f);
1191 		if (dma == NULL) {
1192 			err = DDI_FAILURE;
1193 			break;
1194 		}
1195 
1196 		tbl[i] = dma;
1197 	}
1198 
1199 	return (err);
1200 }
1201 
1202 void
1203 atge_free_buffers(atge_ring_t *r, size_t rcnt)
1204 {
1205 	atge_dma_t **tbl;
1206 	int i;
1207 
1208 	if (r == NULL || r->r_buf_tbl == NULL)
1209 		return;
1210 
1211 	tbl = r->r_buf_tbl;
1212 	for (i = 0; i < rcnt; i++)  {
1213 		if (tbl[i] != NULL) {
1214 			atge_buf_free(tbl[i]);
1215 		}
1216 	}
1217 
1218 	kmem_free(tbl, rcnt * sizeof (atge_dma_t *));
1219 }
1220 
1221 atge_dma_t *
1222 atge_alloc_a_dma_blk(atge_t *atgep, ddi_dma_attr_t *attr, int size, int d)
1223 {
1224 	int err;
1225 	atge_dma_t *dma;
1226 
1227 	dma = kmem_zalloc(sizeof (atge_dma_t), KM_SLEEP);
1228 
1229 	err = ddi_dma_alloc_handle(atgep->atge_dip, attr,
1230 	    DDI_DMA_SLEEP, NULL, &dma->hdl);
1231 
1232 	if (err != DDI_SUCCESS) {
1233 		atge_error(atgep->atge_dip, "%s() : failed"
1234 		    " in ddi_dma_alloc_handle() : %d", __func__, err);
1235 		goto fail;
1236 	}
1237 
1238 	err = ddi_dma_mem_alloc(dma->hdl,
1239 	    size, &atge_buf_attr, DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, NULL,
1240 	    &dma->addr, &dma->len, &dma->acchdl);
1241 
1242 	if (err != DDI_SUCCESS) {
1243 		atge_error(atgep->atge_dip, "%s() : failed"
1244 		    " in ddi_dma_mem_alloc() : %d", __func__, err);
1245 		ddi_dma_free_handle(&dma->hdl);
1246 		goto fail;
1247 	}
1248 
1249 	err = ddi_dma_addr_bind_handle(dma->hdl, NULL, dma->addr,
1250 	    dma->len, d | DDI_DMA_CONSISTENT, DDI_DMA_SLEEP,
1251 	    NULL, &dma->cookie, &dma->count);
1252 
1253 	if (err != DDI_SUCCESS) {
1254 		atge_error(atgep->atge_dip, "%s() : failed"
1255 		    " in ddi_dma_addr_bind_handle() : %d", __func__, err);
1256 		ddi_dma_mem_free(&dma->acchdl);
1257 		ddi_dma_free_handle(&dma->hdl);
1258 		goto fail;
1259 	}
1260 
1261 	return (dma);
1262 fail:
1263 	kmem_free(dma, sizeof (atge_dma_t));
1264 	return (NULL);
1265 }
1266 
1267 void
1268 atge_free_a_dma_blk(atge_dma_t *dma)
1269 {
1270 	if (dma != NULL) {
1271 		(void) ddi_dma_unbind_handle(dma->hdl);
1272 		ddi_dma_mem_free(&dma->acchdl);
1273 		ddi_dma_free_handle(&dma->hdl);
1274 		kmem_free(dma, sizeof (atge_dma_t));
1275 	}
1276 }
1277 
1278 atge_dma_t *
1279 atge_buf_alloc(atge_t *atgep, size_t len, int f)
1280 {
1281 	atge_dma_t *dma = NULL;
1282 	int err;
1283 
1284 	dma = kmem_zalloc(sizeof (atge_dma_t), KM_SLEEP);
1285 
1286 	err = ddi_dma_alloc_handle(atgep->atge_dip, &atge_dma_attr_buf,
1287 	    DDI_DMA_SLEEP, NULL, &dma->hdl);
1288 
1289 	if (err != DDI_SUCCESS) {
1290 		atge_error(atgep->atge_dip, "%s() : failed"
1291 		    " in %s() : %d", __func__, err);
1292 		goto fail;
1293 	}
1294 
1295 	err = ddi_dma_mem_alloc(dma->hdl, len, &atge_buf_attr,
1296 	    DDI_DMA_STREAMING, DDI_DMA_SLEEP, NULL, &dma->addr,
1297 	    &dma->len, &dma->acchdl);
1298 
1299 	if (err != DDI_SUCCESS) {
1300 		atge_error(atgep->atge_dip, "%s() : failed"
1301 		    " in %s() : %d", __func__, err);
1302 		ddi_dma_free_handle(&dma->hdl);
1303 		goto fail;
1304 	}
1305 
1306 	err = ddi_dma_addr_bind_handle(dma->hdl, NULL, dma->addr, dma->len,
1307 	    (f | DDI_DMA_CONSISTENT), DDI_DMA_SLEEP, NULL, &dma->cookie,
1308 	    &dma->count);
1309 
1310 	if (err != DDI_SUCCESS) {
1311 		atge_error(atgep->atge_dip, "%s() : failed"
1312 		    " in %s() : %d", __func__, err);
1313 		ddi_dma_mem_free(&dma->acchdl);
1314 		ddi_dma_free_handle(&dma->hdl);
1315 		goto fail;
1316 	}
1317 
1318 	/*
1319 	 * Number of return'ed cookie should be one.
1320 	 */
1321 	ASSERT(dma->count == 1);
1322 
1323 	return (dma);
1324 fail:
1325 	kmem_free(dma, sizeof (atge_dma_t));
1326 	return (NULL);
1327 }
1328 
1329 void
1330 atge_buf_free(atge_dma_t *dma)
1331 {
1332 	ASSERT(dma != NULL);
1333 
1334 	(void) ddi_dma_unbind_handle(dma->hdl);
1335 	ddi_dma_mem_free(&dma->acchdl);
1336 	ddi_dma_free_handle(&dma->hdl);
1337 	kmem_free(dma, sizeof (atge_dma_t));
1338 }
1339 
1340 static int
1341 atge_resume(dev_info_t *dip)
1342 {
1343 	atge_t	*atgep;
1344 
1345 	if ((atgep = ddi_get_driver_private(dip)) == NULL) {
1346 		return (DDI_FAILURE);
1347 	}
1348 
1349 	mutex_enter(&atgep->atge_intr_lock);
1350 	mutex_enter(&atgep->atge_tx_lock);
1351 
1352 	atgep->atge_chip_state &= ~ATGE_CHIP_SUSPENDED;
1353 
1354 	if (atgep->atge_chip_state & ATGE_CHIP_RUNNING) {
1355 		atge_device_restart(atgep);
1356 	} else {
1357 		atge_device_reset(atgep);
1358 	}
1359 
1360 	mutex_exit(&atgep->atge_tx_lock);
1361 	mutex_exit(&atgep->atge_intr_lock);
1362 
1363 	/*
1364 	 * Reset the PHY before resuming MII.
1365 	 */
1366 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E) {
1367 		atge_l1e_mii_reset(atgep);
1368 	}
1369 
1370 	mii_resume(atgep->atge_mii);
1371 
1372 	/* kick-off downstream */
1373 	mac_tx_update(atgep->atge_mh);
1374 
1375 	return (DDI_SUCCESS);
1376 }
1377 
1378 static int
1379 atge_quiesce(dev_info_t *dip)
1380 {
1381 	atge_t	*atgep;
1382 
1383 	if ((atgep = ddi_get_driver_private(dip)) == NULL) {
1384 		return (DDI_FAILURE);
1385 	}
1386 
1387 	atge_device_stop(atgep);
1388 
1389 	return (DDI_SUCCESS);
1390 }
1391 
1392 void
1393 atge_add_multicst(atge_t *atgep, uint8_t *macaddr)
1394 {
1395 	uint32_t crc;
1396 	int bit;
1397 
1398 	ASSERT(MUTEX_HELD(&atgep->atge_intr_lock));
1399 	ASSERT(MUTEX_HELD(&atgep->atge_tx_lock));
1400 
1401 	ATGE_DB(("%s: %s() %x:%x:%x:%x:%x:%x",
1402 	    atgep->atge_name, __func__, macaddr[0], macaddr[1], macaddr[2],
1403 	    macaddr[3], macaddr[4], macaddr[5]));
1404 
1405 	crc = atge_ether_crc(macaddr, ETHERADDRL);
1406 	bit = (crc >> 26);
1407 	atgep->atge_mchash_ref_cnt[bit]++;
1408 	atgep->atge_mchash |= (1ULL << (crc >> 26));
1409 
1410 	ATGE_DB(("%s: %s() mchash :%llx, bit : %d,"
1411 	    " atge_mchash_ref_cnt[bit] :%d",
1412 	    atgep->atge_name, __func__, atgep->atge_mchash, bit,
1413 	    atgep->atge_mchash_ref_cnt[bit]));
1414 }
1415 
1416 void
1417 atge_remove_multicst(atge_t *atgep, uint8_t *macaddr)
1418 {
1419 	uint32_t crc;
1420 	int bit;
1421 
1422 	ASSERT(MUTEX_HELD(&atgep->atge_intr_lock));
1423 	ASSERT(MUTEX_HELD(&atgep->atge_tx_lock));
1424 
1425 	ATGE_DB(("%s: %s() %x:%x:%x:%x:%x:%x",
1426 	    atgep->atge_name, __func__, macaddr[0], macaddr[1], macaddr[2],
1427 	    macaddr[3], macaddr[4], macaddr[5]));
1428 
1429 	crc = atge_ether_crc(macaddr, ETHERADDRL);
1430 	bit = (crc >> 26);
1431 	atgep->atge_mchash_ref_cnt[bit]--;
1432 	if (atgep->atge_mchash_ref_cnt[bit] == 0)
1433 		atgep->atge_mchash &= ~(1ULL << (crc >> 26));
1434 
1435 	ATGE_DB(("%s: %s() mchash :%llx, bit : %d,"
1436 	    " atge_mchash_ref_cnt[bit] :%d",
1437 	    atgep->atge_name, __func__, atgep->atge_mchash, bit,
1438 	    atgep->atge_mchash_ref_cnt[bit]));
1439 }
1440 
1441 int
1442 atge_m_multicst(void *arg, boolean_t add, const uint8_t *macaddr)
1443 {
1444 	atge_t *atgep = arg;
1445 
1446 	mutex_enter(&atgep->atge_intr_lock);
1447 	mutex_enter(&atgep->atge_tx_lock);
1448 
1449 	if (add) {
1450 		atge_add_multicst(atgep, (uint8_t *)macaddr);
1451 	} else {
1452 		atge_remove_multicst(atgep, (uint8_t *)macaddr);
1453 	}
1454 
1455 	atge_rxfilter(atgep);
1456 
1457 	mutex_exit(&atgep->atge_tx_lock);
1458 	mutex_exit(&atgep->atge_intr_lock);
1459 
1460 	return (0);
1461 }
1462 
1463 int
1464 atge_m_promisc(void *arg, boolean_t on)
1465 {
1466 	atge_t *atgep = arg;
1467 
1468 	mutex_enter(&atgep->atge_intr_lock);
1469 	mutex_enter(&atgep->atge_tx_lock);
1470 
1471 	if (on) {
1472 		atgep->atge_filter_flags |= ATGE_PROMISC;
1473 	} else {
1474 		atgep->atge_filter_flags &= ~ATGE_PROMISC;
1475 	}
1476 
1477 	if (atgep->atge_chip_state & ATGE_CHIP_RUNNING) {
1478 		atge_rxfilter(atgep);
1479 	}
1480 
1481 	mutex_exit(&atgep->atge_tx_lock);
1482 	mutex_exit(&atgep->atge_intr_lock);
1483 
1484 	return (0);
1485 }
1486 
1487 int
1488 atge_m_unicst(void *arg, const uint8_t *macaddr)
1489 {
1490 	atge_t *atgep = arg;
1491 
1492 	mutex_enter(&atgep->atge_intr_lock);
1493 	mutex_enter(&atgep->atge_tx_lock);
1494 	bcopy(macaddr, atgep->atge_ether_addr, ETHERADDRL);
1495 	atge_program_ether(atgep);
1496 	atge_rxfilter(atgep);
1497 	mutex_exit(&atgep->atge_tx_lock);
1498 	mutex_exit(&atgep->atge_intr_lock);
1499 
1500 	return (0);
1501 }
1502 
1503 mblk_t *
1504 atge_m_tx(void *arg, mblk_t *mp)
1505 {
1506 	atge_t *atgep = arg;
1507 	mblk_t	*nmp;
1508 
1509 	mutex_enter(&atgep->atge_tx_lock);
1510 
1511 	/*
1512 	 * This NIC does not like us to send pkt when link is down.
1513 	 */
1514 	if (!(atgep->atge_link_state & LINK_STATE_UP)) {
1515 		atgep->atge_tx_resched = 1;
1516 
1517 		mutex_exit(&atgep->atge_tx_lock);
1518 		return (mp);
1519 	}
1520 
1521 	/*
1522 	 * Don't send a pkt if chip isn't running or in suspended state.
1523 	 */
1524 	if ((atgep->atge_chip_state & ATGE_CHIP_RUNNING) == 0 ||
1525 	    atgep->atge_chip_state & ATGE_CHIP_SUSPENDED) {
1526 		atgep->atge_carrier_errors++;
1527 		atgep->atge_tx_resched = 1;
1528 
1529 		mutex_exit(&atgep->atge_tx_lock);
1530 		return (mp);
1531 	}
1532 
1533 	while (mp != NULL) {
1534 		nmp = mp->b_next;
1535 		mp->b_next = NULL;
1536 
1537 		if (atge_send_a_packet(atgep, mp) == DDI_FAILURE) {
1538 			mp->b_next = nmp;
1539 			break;
1540 		}
1541 
1542 		mp = nmp;
1543 	}
1544 
1545 	mutex_exit(&atgep->atge_tx_lock);
1546 	return (mp);
1547 }
1548 
1549 int
1550 atge_m_start(void *arg)
1551 {
1552 	atge_t *atgep = arg;
1553 	int started = 0;
1554 
1555 	ASSERT(atgep != NULL);
1556 
1557 
1558 	mii_stop(atgep->atge_mii);
1559 
1560 	mutex_enter(&atgep->atge_intr_lock);
1561 	mutex_enter(&atgep->atge_tx_lock);
1562 
1563 	if (!(atgep->atge_chip_state & ATGE_CHIP_SUSPENDED)) {
1564 		atge_device_restart(atgep);
1565 		started = 1;
1566 	}
1567 
1568 	mutex_exit(&atgep->atge_tx_lock);
1569 	mutex_exit(&atgep->atge_intr_lock);
1570 
1571 	mii_start(atgep->atge_mii);
1572 
1573 	/* kick-off downstream */
1574 	if (started)
1575 		mac_tx_update(atgep->atge_mh);
1576 
1577 	return (0);
1578 }
1579 
1580 void
1581 atge_m_stop(void *arg)
1582 {
1583 	atge_t *atgep = arg;
1584 
1585 	mii_stop(atgep->atge_mii);
1586 
1587 	/*
1588 	 * Cancel any pending I/O.
1589 	 */
1590 	mutex_enter(&atgep->atge_intr_lock);
1591 	atgep->atge_chip_state &= ~ATGE_CHIP_RUNNING;
1592 	if (!(atgep->atge_chip_state & ATGE_CHIP_SUSPENDED))
1593 		atge_device_stop(atgep);
1594 	mutex_exit(&atgep->atge_intr_lock);
1595 }
1596 
1597 int
1598 atge_m_stat(void *arg, uint_t stat, uint64_t *val)
1599 {
1600 	atge_t *atgep = arg;
1601 
1602 	if (mii_m_getstat(atgep->atge_mii, stat, val) == 0) {
1603 		return (0);
1604 	}
1605 
1606 	switch (stat) {
1607 	case MAC_STAT_MULTIRCV:
1608 		*val = atgep->atge_multircv;
1609 		break;
1610 
1611 	case MAC_STAT_BRDCSTRCV:
1612 		*val = atgep->atge_brdcstrcv;
1613 		break;
1614 
1615 	case MAC_STAT_MULTIXMT:
1616 		*val = atgep->atge_multixmt;
1617 		break;
1618 
1619 	case MAC_STAT_BRDCSTXMT:
1620 		*val = atgep->atge_brdcstxmt;
1621 		break;
1622 
1623 	case MAC_STAT_IPACKETS:
1624 		*val = atgep->atge_ipackets;
1625 		break;
1626 
1627 	case MAC_STAT_RBYTES:
1628 		*val = atgep->atge_rbytes;
1629 		break;
1630 
1631 	case MAC_STAT_OPACKETS:
1632 		*val = atgep->atge_opackets;
1633 		break;
1634 
1635 	case MAC_STAT_OBYTES:
1636 		*val = atgep->atge_obytes;
1637 		break;
1638 
1639 	case MAC_STAT_NORCVBUF:
1640 		*val = atgep->atge_norcvbuf;
1641 		break;
1642 
1643 	case MAC_STAT_NOXMTBUF:
1644 		*val = 0;
1645 		break;
1646 
1647 	case MAC_STAT_COLLISIONS:
1648 		*val = atgep->atge_collisions;
1649 		break;
1650 
1651 	case MAC_STAT_IERRORS:
1652 		*val = atgep->atge_errrcv;
1653 		break;
1654 
1655 	case MAC_STAT_OERRORS:
1656 		*val = atgep->atge_errxmt;
1657 		break;
1658 
1659 	case ETHER_STAT_ALIGN_ERRORS:
1660 		*val = atgep->atge_align_errors;
1661 		break;
1662 
1663 	case ETHER_STAT_FCS_ERRORS:
1664 		*val = atgep->atge_fcs_errors;
1665 		break;
1666 
1667 	case ETHER_STAT_SQE_ERRORS:
1668 		*val = atgep->atge_sqe_errors;
1669 		break;
1670 
1671 	case ETHER_STAT_DEFER_XMTS:
1672 		*val = atgep->atge_defer_xmts;
1673 		break;
1674 
1675 	case ETHER_STAT_FIRST_COLLISIONS:
1676 		*val = atgep->atge_first_collisions;
1677 		break;
1678 
1679 	case ETHER_STAT_MULTI_COLLISIONS:
1680 		*val = atgep->atge_multi_collisions;
1681 		break;
1682 
1683 	case ETHER_STAT_TX_LATE_COLLISIONS:
1684 		*val = atgep->atge_tx_late_collisions;
1685 		break;
1686 
1687 	case ETHER_STAT_EX_COLLISIONS:
1688 		*val = atgep->atge_ex_collisions;
1689 		break;
1690 
1691 	case ETHER_STAT_MACXMT_ERRORS:
1692 		*val = atgep->atge_macxmt_errors;
1693 		break;
1694 
1695 	case ETHER_STAT_CARRIER_ERRORS:
1696 		*val = atgep->atge_carrier_errors;
1697 		break;
1698 
1699 	case ETHER_STAT_TOOLONG_ERRORS:
1700 		*val = atgep->atge_toolong_errors;
1701 		break;
1702 
1703 	case ETHER_STAT_MACRCV_ERRORS:
1704 		*val = atgep->atge_macrcv_errors;
1705 		break;
1706 
1707 	case MAC_STAT_OVERFLOWS:
1708 		*val = atgep->atge_overflow;
1709 		break;
1710 
1711 	case MAC_STAT_UNDERFLOWS:
1712 		*val = atgep->atge_underflow;
1713 		break;
1714 
1715 	case ETHER_STAT_TOOSHORT_ERRORS:
1716 		*val = atgep->atge_runt;
1717 		break;
1718 
1719 	case ETHER_STAT_JABBER_ERRORS:
1720 		*val = atgep->atge_jabber;
1721 		break;
1722 
1723 	default:
1724 		return (ENOTSUP);
1725 	}
1726 
1727 	return (0);
1728 }
1729 
1730 int
1731 atge_m_getprop(void *arg, const char *name, mac_prop_id_t num, uint_t sz,
1732     void *val)
1733 {
1734 	atge_t *atgep = arg;
1735 
1736 	return (mii_m_getprop(atgep->atge_mii, name, num, sz, val));
1737 }
1738 
1739 int
1740 atge_m_setprop(void *arg, const char *name, mac_prop_id_t num, uint_t sz,
1741     const void *val)
1742 {
1743 	atge_t *atgep = arg;
1744 	int r;
1745 
1746 	r = mii_m_setprop(atgep->atge_mii, name, num, sz, val);
1747 
1748 	if (r == 0) {
1749 		mutex_enter(&atgep->atge_intr_lock);
1750 		mutex_enter(&atgep->atge_tx_lock);
1751 
1752 		if (atgep->atge_chip_state & ATGE_CHIP_RUNNING) {
1753 			atge_device_restart(atgep);
1754 		}
1755 
1756 		mutex_exit(&atgep->atge_tx_lock);
1757 		mutex_exit(&atgep->atge_intr_lock);
1758 	}
1759 
1760 	return (r);
1761 }
1762 
1763 static void
1764 atge_m_propinfo(void *arg, const char *name, mac_prop_id_t num,
1765     mac_prop_info_handle_t prh)
1766 {
1767 	atge_t *atgep = arg;
1768 
1769 	mii_m_propinfo(atgep->atge_mii, name, num, prh);
1770 }
1771 
1772 void
1773 atge_program_ether(atge_t *atgep)
1774 {
1775 	ether_addr_t e;
1776 
1777 	/*
1778 	 * Reprogram the Station address.
1779 	 */
1780 	bcopy(atgep->atge_ether_addr, e, ETHERADDRL);
1781 	OUTL(atgep, ATGE_PAR0,
1782 	    ((e[2] << 24) | (e[3] << 16) | (e[4] << 8) | e[5]));
1783 	OUTL(atgep, ATGE_PAR1, (e[0] << 8) | e[1]);
1784 }
1785 
1786 /*
1787  * Device specific operations.
1788  */
1789 void
1790 atge_device_start(atge_t *atgep)
1791 {
1792 	uint32_t rxf_hi, rxf_lo, rrd_hi, rrd_lo;
1793 	uint32_t reg;
1794 	uint32_t fsize;
1795 
1796 	/*
1797 	 * Reprogram the Station address.
1798 	 */
1799 	atge_program_ether(atgep);
1800 
1801 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E) {
1802 		atge_l1e_program_dma(atgep);
1803 	} else if (ATGE_MODEL(atgep) == ATGE_CHIP_L1) {
1804 		atge_l1_program_dma(atgep);
1805 	}
1806 
1807 	ATGE_DB(("%s: %s() dma, counters programmed ", atgep->atge_name,
1808 	    __func__));
1809 
1810 	OUTW(atgep, ATGE_INTR_CLR_TIMER, 1*1000/2);
1811 
1812 	/*
1813 	 * Set Maximum frame size but don't let MTU be less than ETHER_MTU.
1814 	 */
1815 	if (atgep->atge_mtu < ETHERMTU)
1816 		atgep->atge_max_frame_size = ETHERMTU;
1817 	else
1818 		atgep->atge_max_frame_size = atgep->atge_mtu;
1819 
1820 	atgep->atge_max_frame_size += sizeof (struct ether_header) +
1821 	    VLAN_TAGSZ + ETHERFCSL;
1822 	OUTL(atgep, ATGE_FRAME_SIZE, atgep->atge_max_frame_size);
1823 
1824 
1825 	/*
1826 	 * Configure IPG/IFG parameters.
1827 	 */
1828 	OUTL(atgep, ATGE_IPG_IFG_CFG,
1829 	    ((IPG_IFG_IPG2_DEFAULT << IPG_IFG_IPG2_SHIFT) & IPG_IFG_IPG2_MASK) |
1830 	    ((IPG_IFG_IPG1_DEFAULT << IPG_IFG_IPG1_SHIFT) & IPG_IFG_IPG1_MASK) |
1831 	    ((IPG_IFG_MIFG_DEFAULT << IPG_IFG_MIFG_SHIFT) & IPG_IFG_MIFG_MASK) |
1832 	    ((IPG_IFG_IPGT_DEFAULT << IPG_IFG_IPGT_SHIFT) & IPG_IFG_IPGT_MASK));
1833 
1834 	/*
1835 	 * Set parameters for half-duplex media.
1836 	 */
1837 	OUTL(atgep, ATGE_HDPX_CFG,
1838 	    ((HDPX_CFG_LCOL_DEFAULT << HDPX_CFG_LCOL_SHIFT) &
1839 	    HDPX_CFG_LCOL_MASK) |
1840 	    ((HDPX_CFG_RETRY_DEFAULT << HDPX_CFG_RETRY_SHIFT) &
1841 	    HDPX_CFG_RETRY_MASK) | HDPX_CFG_EXC_DEF_EN |
1842 	    ((HDPX_CFG_ABEBT_DEFAULT << HDPX_CFG_ABEBT_SHIFT) &
1843 	    HDPX_CFG_ABEBT_MASK) |
1844 	    ((HDPX_CFG_JAMIPG_DEFAULT << HDPX_CFG_JAMIPG_SHIFT) &
1845 	    HDPX_CFG_JAMIPG_MASK));
1846 
1847 	/*
1848 	 * Configure jumbo frame.
1849 	 */
1850 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1) {
1851 		fsize = ROUNDUP(atgep->atge_max_frame_size, sizeof (uint64_t));
1852 		OUTL(atgep, ATGE_RXQ_JUMBO_CFG,
1853 		    (((fsize / sizeof (uint64_t)) <<
1854 		    RXQ_JUMBO_CFG_SZ_THRESH_SHIFT) &
1855 		    RXQ_JUMBO_CFG_SZ_THRESH_MASK) |
1856 		    ((RXQ_JUMBO_CFG_LKAH_DEFAULT <<
1857 		    RXQ_JUMBO_CFG_LKAH_SHIFT) & RXQ_JUMBO_CFG_LKAH_MASK) |
1858 		    ((ATGE_USECS(8) << RXQ_JUMBO_CFG_RRD_TIMER_SHIFT) &
1859 		    RXQ_JUMBO_CFG_RRD_TIMER_MASK));
1860 	} else if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E &&
1861 	    atgep->atge_flags & ATGE_FLAG_JUMBO) {
1862 
1863 		if (atgep->atge_mtu < ETHERMTU)
1864 			reg = atgep->atge_max_frame_size;
1865 		else if (atgep->atge_mtu < 6 * 1024)
1866 			reg = (atgep->atge_max_frame_size * 2) / 3;
1867 		else
1868 			reg = atgep->atge_max_frame_size / 2;
1869 
1870 		OUTL(atgep, L1E_TX_JUMBO_THRESH,
1871 		    ROUNDUP(reg, TX_JUMBO_THRESH_UNIT) >>
1872 		    TX_JUMBO_THRESH_UNIT_SHIFT);
1873 	}
1874 
1875 	/*
1876 	 * Configure flow-control parameters.
1877 	 */
1878 	if ((atgep->atge_flags & ATGE_FLAG_PCIE) != 0) {
1879 		/*
1880 		 * Some hardware version require this magic.
1881 		 */
1882 		OUTL(atgep, 0x12FC, 0x6500);
1883 		reg = INL(atgep, 0x1008);
1884 		OUTL(atgep, 0x1008, reg | 0x8000);
1885 	}
1886 
1887 	/*
1888 	 * These are all magic parameters which came from FreeBSD.
1889 	 */
1890 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1) {
1891 		switch (atgep->atge_chip_rev) {
1892 		case 0x8001:
1893 		case 0x9001:
1894 		case 0x9002:
1895 		case 0x9003:
1896 			rxf_hi = L1_RX_RING_CNT / 16;
1897 			rxf_lo = (L1_RX_RING_CNT * 7) / 8;
1898 			rrd_hi = (L1_RR_RING_CNT * 7) / 8;
1899 			rrd_lo = L1_RR_RING_CNT / 16;
1900 			break;
1901 		default:
1902 			reg = INL(atgep, L1_SRAM_RX_FIFO_LEN);
1903 			rxf_lo = reg / 16;
1904 			if (rxf_lo > 192)
1905 				rxf_lo = 192;
1906 			rxf_hi = (reg * 7) / 8;
1907 			if (rxf_hi < rxf_lo)
1908 				rxf_hi = rxf_lo + 16;
1909 			reg = INL(atgep, L1_SRAM_RRD_LEN);
1910 			rrd_lo = reg / 8;
1911 			rrd_hi = (reg * 7) / 8;
1912 			if (rrd_lo > 2)
1913 				rrd_lo = 2;
1914 			if (rrd_hi < rrd_lo)
1915 				rrd_hi = rrd_lo + 3;
1916 			break;
1917 		}
1918 
1919 		OUTL(atgep, ATGE_RXQ_FIFO_PAUSE_THRESH,
1920 		    ((rxf_lo << RXQ_FIFO_PAUSE_THRESH_LO_SHIFT) &
1921 		    RXQ_FIFO_PAUSE_THRESH_LO_MASK) |
1922 		    ((rxf_hi << RXQ_FIFO_PAUSE_THRESH_HI_SHIFT) &
1923 		    RXQ_FIFO_PAUSE_THRESH_HI_MASK));
1924 
1925 		OUTL(atgep, L1_RXQ_RRD_PAUSE_THRESH,
1926 		    ((rrd_lo << RXQ_RRD_PAUSE_THRESH_LO_SHIFT) &
1927 		    RXQ_RRD_PAUSE_THRESH_LO_MASK) |
1928 		    ((rrd_hi << RXQ_RRD_PAUSE_THRESH_HI_SHIFT) &
1929 		    RXQ_RRD_PAUSE_THRESH_HI_MASK));
1930 	} else if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E) {
1931 		reg = INL(atgep, L1E_SRAM_RX_FIFO_LEN);
1932 		rxf_hi = (reg * 4) / 5;
1933 		rxf_lo = reg/ 5;
1934 
1935 		OUTL(atgep, ATGE_RXQ_FIFO_PAUSE_THRESH,
1936 		    ((rxf_lo << RXQ_FIFO_PAUSE_THRESH_LO_SHIFT) &
1937 		    RXQ_FIFO_PAUSE_THRESH_LO_MASK) |
1938 		    ((rxf_hi << RXQ_FIFO_PAUSE_THRESH_HI_SHIFT) &
1939 		    RXQ_FIFO_PAUSE_THRESH_HI_MASK));
1940 	}
1941 
1942 	/* Configure RxQ. */
1943 	reg = 0;
1944 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1) {
1945 		reg =
1946 		    ((RXQ_CFG_RD_BURST_DEFAULT << RXQ_CFG_RD_BURST_SHIFT) &
1947 		    RXQ_CFG_RD_BURST_MASK) |
1948 		    ((RXQ_CFG_RRD_BURST_THRESH_DEFAULT <<
1949 		    RXQ_CFG_RRD_BURST_THRESH_SHIFT) &
1950 		    RXQ_CFG_RRD_BURST_THRESH_MASK) |
1951 		    ((RXQ_CFG_RD_PREF_MIN_IPG_DEFAULT <<
1952 		    RXQ_CFG_RD_PREF_MIN_IPG_SHIFT) &
1953 		    RXQ_CFG_RD_PREF_MIN_IPG_MASK) |
1954 		    RXQ_CFG_CUT_THROUGH_ENB | RXQ_CFG_ENB;
1955 		OUTL(atgep, ATGE_RXQ_CFG, reg);
1956 	} else if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E) {
1957 		reg = RXQ_CFG_ALIGN_32 | RXQ_CFG_CUT_THROUGH_ENB |
1958 		    RXQ_CFG_IPV6_CSUM_VERIFY | RXQ_CFG_ENB;
1959 		OUTL(atgep, ATGE_RXQ_CFG, reg);
1960 	}
1961 
1962 	/*
1963 	 * Configure TxQ.
1964 	 */
1965 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1) {
1966 		reg =
1967 		    (((TXQ_CFG_TPD_BURST_DEFAULT << TXQ_CFG_TPD_BURST_SHIFT) &
1968 		    TXQ_CFG_TPD_BURST_MASK) |
1969 		    ((TXQ_CFG_TX_FIFO_BURST_DEFAULT <<
1970 		    TXQ_CFG_TX_FIFO_BURST_SHIFT) &
1971 		    TXQ_CFG_TX_FIFO_BURST_MASK) |
1972 		    ((TXQ_CFG_TPD_FETCH_DEFAULT <<
1973 		    TXQ_CFG_TPD_FETCH_THRESH_SHIFT) &
1974 		    TXQ_CFG_TPD_FETCH_THRESH_MASK) |
1975 		    TXQ_CFG_ENB);
1976 		OUTL(atgep, ATGE_TXQ_CFG, reg);
1977 	} else if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E) {
1978 		reg = (128 <<
1979 		    (atgep->atge_dma_rd_burst >> DMA_CFG_RD_BURST_SHIFT)) <<
1980 		    TXQ_CFG_TX_FIFO_BURST_SHIFT;
1981 
1982 		reg |= (TXQ_CFG_TPD_BURST_DEFAULT << TXQ_CFG_TPD_BURST_SHIFT) &
1983 		    TXQ_CFG_TPD_BURST_MASK;
1984 
1985 		reg |= TXQ_CFG_ENHANCED_MODE | TXQ_CFG_ENB;
1986 
1987 		OUTL(atgep, ATGE_TXQ_CFG, reg);
1988 	}
1989 
1990 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1) {
1991 		OUTL(atgep, L1_TX_JUMBO_TPD_TH_IPG,
1992 		    (((fsize / sizeof (uint64_t) << TX_JUMBO_TPD_TH_SHIFT)) &
1993 		    TX_JUMBO_TPD_TH_MASK) |
1994 		    ((TX_JUMBO_TPD_IPG_DEFAULT << TX_JUMBO_TPD_IPG_SHIFT) &
1995 		    TX_JUMBO_TPD_IPG_MASK));
1996 	}
1997 
1998 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E) {
1999 		/* Disable RSS. */
2000 		OUTL(atgep, L1E_RSS_IDT_TABLE0, 0);
2001 		OUTL(atgep, L1E_RSS_CPU, 0);
2002 	}
2003 
2004 	/*
2005 	 * Configure DMA parameters.
2006 	 */
2007 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1) {
2008 		OUTL(atgep, ATGE_DMA_CFG,
2009 		    DMA_CFG_ENH_ORDER | DMA_CFG_RCB_64 |
2010 		    atgep->atge_dma_rd_burst | DMA_CFG_RD_ENB |
2011 		    atgep->atge_dma_wr_burst | DMA_CFG_WR_ENB);
2012 
2013 		/* Configure CMB DMA write threshold. */
2014 		OUTL(atgep, L1_CMB_WR_THRESH,
2015 		    ((CMB_WR_THRESH_RRD_DEFAULT << CMB_WR_THRESH_RRD_SHIFT) &
2016 		    CMB_WR_THRESH_RRD_MASK) |
2017 		    ((CMB_WR_THRESH_TPD_DEFAULT << CMB_WR_THRESH_TPD_SHIFT) &
2018 		    CMB_WR_THRESH_TPD_MASK));
2019 	} else if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E) {
2020 		/*
2021 		 * Don't use Tx CMB. It is known to cause RRS update failure
2022 		 * under certain circumstances. Typical phenomenon of the
2023 		 * issue would be unexpected sequence number encountered in
2024 		 * Rx handler. Hence we don't set DMA_CFG_TXCMB_ENB.
2025 		 */
2026 		OUTL(atgep, ATGE_DMA_CFG,
2027 		    DMA_CFG_OUT_ORDER | DMA_CFG_RD_REQ_PRI | DMA_CFG_RCB_64 |
2028 		    atgep->atge_dma_rd_burst | atgep->atge_dma_wr_burst |
2029 		    DMA_CFG_RXCMB_ENB |
2030 		    ((DMA_CFG_RD_DELAY_CNT_DEFAULT <<
2031 		    DMA_CFG_RD_DELAY_CNT_SHIFT) & DMA_CFG_RD_DELAY_CNT_MASK) |
2032 		    ((DMA_CFG_WR_DELAY_CNT_DEFAULT <<
2033 		    DMA_CFG_WR_DELAY_CNT_SHIFT) & DMA_CFG_WR_DELAY_CNT_MASK));
2034 	}
2035 
2036 	/*
2037 	 * Enable CMB/SMB timer.
2038 	 */
2039 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1) {
2040 		/* Set CMB/SMB timer and enable them. */
2041 		OUTL(atgep, L1_CMB_WR_TIMER,
2042 		    ((ATGE_USECS(2) << CMB_WR_TIMER_TX_SHIFT) &
2043 		    CMB_WR_TIMER_TX_MASK) |
2044 		    ((ATGE_USECS(2) << CMB_WR_TIMER_RX_SHIFT) &
2045 		    CMB_WR_TIMER_RX_MASK));
2046 
2047 		/* Request SMB updates for every seconds. */
2048 		OUTL(atgep, L1_SMB_TIMER, ATGE_USECS(1000 * 1000));
2049 		OUTL(atgep, L1_CSMB_CTRL,
2050 		    CSMB_CTRL_SMB_ENB | CSMB_CTRL_CMB_ENB);
2051 	} else if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E) {
2052 		OUTL(atgep, L1E_SMB_STAT_TIMER, 100000);
2053 		atge_l1e_clear_stats(atgep);
2054 	}
2055 
2056 
2057 	/*
2058 	 * Disable all WOL bits as WOL can interfere normal Rx
2059 	 * operation.
2060 	 */
2061 	OUTL(atgep, ATGE_WOL_CFG, 0);
2062 
2063 	/*
2064 	 * Configure Tx/Rx MACs.
2065 	 *  - Auto-padding for short frames.
2066 	 *  - Enable CRC generation.
2067 	 *
2068 	 *  Start with full-duplex/1000Mbps media. Actual reconfiguration
2069 	 *  of MAC is followed after link establishment.
2070 	 */
2071 	reg = (ATGE_CFG_TX_CRC_ENB | ATGE_CFG_TX_AUTO_PAD |
2072 	    ATGE_CFG_FULL_DUPLEX |
2073 	    ((ATGE_CFG_PREAMBLE_DEFAULT << ATGE_CFG_PREAMBLE_SHIFT) &
2074 	    ATGE_CFG_PREAMBLE_MASK));
2075 
2076 	if ((atgep->atge_flags & ATGE_FLAG_FASTETHER) != 0) {
2077 		reg |= ATGE_CFG_SPEED_10_100;
2078 		ATGE_DB(("%s: %s() Fast Ethernet", atgep->atge_name, __func__));
2079 	} else {
2080 		reg |= ATGE_CFG_SPEED_1000;
2081 		ATGE_DB(("%s: %s() 1G speed", atgep->atge_name, __func__));
2082 	}
2083 
2084 	OUTL(atgep, ATGE_MAC_CFG, reg);
2085 
2086 	atgep->atge_chip_state |= ATGE_CHIP_RUNNING;
2087 
2088 	/*
2089 	 * Set up the receive filter.
2090 	 */
2091 	atge_rxfilter(atgep);
2092 
2093 	/*
2094 	 * Acknowledge all pending interrupts and clear it.
2095 	 */
2096 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1) {
2097 		OUTL(atgep, ATGE_INTR_STATUS, 0);
2098 		OUTL(atgep, ATGE_INTR_MASK, atgep->atge_intrs);
2099 	} else if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E) {
2100 		OUTL(atgep, ATGE_INTR_MASK, L1E_INTRS);
2101 		OUTL(atgep, ATGE_INTR_STATUS, 0xFFFFFFFF);
2102 		OUTL(atgep, ATGE_INTR_STATUS, 0);
2103 	}
2104 
2105 	atge_mac_config(atgep);
2106 
2107 	ATGE_DB(("%s: %s() device started", atgep->atge_name, __func__));
2108 }
2109 
2110 /*
2111  * Generic functions.
2112  */
2113 
2114 #define	CRC32_POLY_BE   0x04c11db7
2115 uint32_t
2116 atge_ether_crc(const uint8_t *addr, int len)
2117 {
2118 	int idx;
2119 	int bit;
2120 	uint_t data;
2121 	uint32_t crc;
2122 
2123 	crc = 0xffffffff;
2124 	for (idx = 0; idx < len; idx++) {
2125 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1) {
2126 			crc = (crc << 1)
2127 			    ^ ((((crc >> 31) ^ data) & 1) ? CRC32_POLY_BE : 0);
2128 		}
2129 	}
2130 
2131 	return (crc);
2132 }
2133 
2134 
2135 /*
2136  * Programs RX filter. We use a link-list to keep track of all multicast
2137  * addressess.
2138  */
2139 void
2140 atge_rxfilter(atge_t *atgep)
2141 {
2142 	uint32_t rxcfg;
2143 	uint64_t mchash;
2144 
2145 	rxcfg = INL(atgep, ATGE_MAC_CFG);
2146 	rxcfg &= ~(ATGE_CFG_ALLMULTI | ATGE_CFG_PROMISC);
2147 
2148 	/*
2149 	 * Accept broadcast frames.
2150 	 */
2151 	rxcfg |= ATGE_CFG_BCAST;
2152 
2153 	/*
2154 	 * We don't use Hardware VLAN tagging.
2155 	 */
2156 	rxcfg &= ~ATGE_CFG_VLAN_TAG_STRIP;
2157 
2158 	if (atgep->atge_filter_flags & (ATGE_PROMISC | ATGE_ALL_MULTICST)) {
2159 		mchash = ~0ULL;
2160 
2161 		if (atgep->atge_filter_flags & ATGE_PROMISC)
2162 			rxcfg |= ATGE_CFG_PROMISC;
2163 
2164 		if (atgep->atge_filter_flags & ATGE_ALL_MULTICST)
2165 			rxcfg |= ATGE_CFG_ALLMULTI;
2166 	} else {
2167 		mchash = atgep->atge_mchash;
2168 	}
2169 
2170 	atge_program_ether(atgep);
2171 
2172 	OUTL(atgep, ATGE_MAR0, (uint32_t)mchash);
2173 	OUTL(atgep, ATGE_MAR1, (uint32_t)(mchash >> 32));
2174 	OUTL(atgep, ATGE_MAC_CFG, rxcfg);
2175 
2176 	ATGE_DB(("%s: %s() mac_cfg is : %x, mchash : %llx",
2177 	    atgep->atge_name, __func__, rxcfg, mchash));
2178 }
2179 
2180 void
2181 atge_device_stop(atge_t *atgep)
2182 {
2183 	uint32_t reg;
2184 	int t;
2185 
2186 	/*
2187 	 * If the chip is being suspended, then don't touch the state. Caller
2188 	 * will take care of setting the correct state.
2189 	 */
2190 	if (!(atgep->atge_chip_state & ATGE_CHIP_SUSPENDED)) {
2191 		atgep->atge_chip_state |= ATGE_CHIP_STOPPED;
2192 		atgep->atge_chip_state &= ~ATGE_CHIP_RUNNING;
2193 	}
2194 
2195 	/*
2196 	 * Collect stats for L1E. L1 chip's stats are collected by interrupt.
2197 	 */
2198 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E) {
2199 		atge_l1e_gather_stats(atgep);
2200 	}
2201 
2202 	/*
2203 	 * Disable interrupts.
2204 	 */
2205 	atge_disable_intrs(atgep);
2206 
2207 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1)
2208 		OUTL(atgep, L1_CSMB_CTRL, 0);
2209 
2210 	/* Stop DMA Engine */
2211 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1) {
2212 		atge_l1_stop_tx_mac(atgep);
2213 		atge_l1_stop_rx_mac(atgep);
2214 
2215 		reg = INL(atgep, ATGE_DMA_CFG);
2216 		reg &= ~(DMA_CFG_RD_ENB | DMA_CFG_WR_ENB);
2217 		OUTL(atgep, ATGE_DMA_CFG, reg);
2218 
2219 	}
2220 
2221 	/*
2222 	 * Disable queue processing.
2223 	 */
2224 	/* Stop TxQ */
2225 	reg = INL(atgep, ATGE_TXQ_CFG);
2226 	reg = reg & ~TXQ_CFG_ENB;
2227 	OUTL(atgep, ATGE_TXQ_CFG, reg);
2228 
2229 	/* Stop RxQ */
2230 	reg = INL(atgep, ATGE_RXQ_CFG);
2231 	reg = reg & ~RXQ_CFG_ENB;
2232 	OUTL(atgep, ATGE_RXQ_CFG, reg);
2233 
2234 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E) {
2235 		reg = INL(atgep, ATGE_DMA_CFG);
2236 		reg = reg & ~(DMA_CFG_TXCMB_ENB | DMA_CFG_RXCMB_ENB);
2237 		OUTL(atgep, ATGE_DMA_CFG, reg);
2238 		drv_usecwait(1000);
2239 		atge_l1e_stop_mac(atgep);
2240 		OUTL(atgep, ATGE_INTR_STATUS, 0xFFFFFFFF);
2241 	}
2242 
2243 	for (t = ATGE_RESET_TIMEOUT; t > 0; t--) {
2244 		if ((reg = INL(atgep, ATGE_IDLE_STATUS)) == 0)
2245 			break;
2246 		drv_usecwait(10);
2247 	}
2248 
2249 	if (t == 0) {
2250 		atge_error(atgep->atge_dip, "%s() stopping TX/RX MAC timeout",
2251 		    __func__);
2252 	}
2253 }
2254 
2255 void
2256 atge_disable_intrs(atge_t *atgep)
2257 {
2258 	OUTL(atgep, ATGE_INTR_MASK, 0);
2259 	OUTL(atgep, ATGE_INTR_STATUS, 0xFFFFFFFF);
2260 }
2261 
2262 void
2263 atge_device_init(atge_t *atgep)
2264 {
2265 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E) {
2266 		atgep->atge_intrs = L1E_INTRS;
2267 		atgep->atge_int_mod = ATGE_IM_TIMER_DEFAULT;
2268 
2269 		atge_l1e_init_tx_ring(atgep);
2270 		atge_l1e_init_rx_pages(atgep);
2271 	} else if (ATGE_MODEL(atgep) == ATGE_CHIP_L1) {
2272 		atgep->atge_intrs = L1_INTRS | INTR_GPHY | INTR_PHY_LINK_DOWN |
2273 		    INTR_LINK_CHG;
2274 		atgep->atge_int_mod = ATGE_IM_TIMER_DEFAULT;
2275 
2276 		atge_l1_init_tx_ring(atgep);
2277 		atge_l1_init_rx_ring(atgep);
2278 		atge_l1_init_rr_ring(atgep);
2279 		atge_l1_init_cmb(atgep);
2280 		atge_l1_init_smb(atgep);
2281 	}
2282 }
2283 
2284 void
2285 atge_device_restart(atge_t *atgep)
2286 {
2287 	ASSERT(MUTEX_HELD(&atgep->atge_intr_lock));
2288 	ASSERT(MUTEX_HELD(&atgep->atge_tx_lock));
2289 
2290 	/*
2291 	 * Cancel any pending I/O.
2292 	 */
2293 	atge_device_stop(atgep);
2294 
2295 	/*
2296 	 * Reset the chip to a known state.
2297 	 */
2298 	atge_device_reset(atgep);
2299 
2300 	/*
2301 	 * Initialize the ring and other descriptor like CMB/SMB/Rx return.
2302 	 */
2303 	atge_device_init(atgep);
2304 
2305 	/*
2306 	 * Start the chip.
2307 	 */
2308 	atge_device_start(atgep);
2309 
2310 }
2311 
2312 static int
2313 atge_send_a_packet(atge_t *atgep, mblk_t *mp)
2314 {
2315 	atge_tx_desc_t	*txd;
2316 	uchar_t *c;
2317 	uint32_t cflags = 0;
2318 	atge_ring_t *r;
2319 	size_t pktlen;
2320 	uchar_t *buf;
2321 	int	start;
2322 
2323 	ASSERT(MUTEX_HELD(&atgep->atge_tx_lock));
2324 	ASSERT(mp != NULL);
2325 
2326 	pktlen = msgsize(mp);
2327 	if (pktlen > atgep->atge_tx_buf_len) {
2328 		atgep->atge_macxmt_errors++;
2329 
2330 		ATGE_DB(("%s: %s() pktlen (%d) > rx_buf_len (%d)",
2331 		    atgep->atge_name, __func__,
2332 		    pktlen, atgep->atge_rx_buf_len));
2333 
2334 		freemsg(mp);
2335 		return (DDI_SUCCESS);
2336 	}
2337 
2338 	r = atgep->atge_tx_ring;
2339 
2340 	if (r->r_avail_desc <= 1) {
2341 		atgep->atge_noxmtbuf++;
2342 		atgep->atge_tx_resched = 1;
2343 
2344 		ATGE_DB(("%s: %s() No transmit buf",
2345 		    atgep->atge_name, __func__));
2346 
2347 		return (DDI_FAILURE);
2348 	}
2349 
2350 	start = r->r_producer;
2351 
2352 	/*
2353 	 * Get the DMA buffer to hold a packet.
2354 	 */
2355 	buf = (uchar_t *)r->r_buf_tbl[start]->addr;
2356 
2357 	/*
2358 	 * Copy the msg and free mp
2359 	 */
2360 	mcopymsg(mp, buf);
2361 
2362 	r->r_avail_desc--;
2363 
2364 	c = (uchar_t *)r->r_desc_ring->addr;
2365 	c += (sizeof (atge_tx_desc_t) * start);
2366 	txd = (atge_tx_desc_t *)c;
2367 
2368 	ATGE_PUT64(r->r_desc_ring, &txd->addr,
2369 	    r->r_buf_tbl[start]->cookie.dmac_laddress);
2370 
2371 	ATGE_PUT32(r->r_desc_ring, &txd->len, ATGE_TX_BYTES(pktlen));
2372 
2373 	cflags |= ATGE_TD_EOP;
2374 	ATGE_PUT32(r->r_desc_ring, &txd->flags, cflags);
2375 
2376 	/*
2377 	 * Sync buffer first.
2378 	 */
2379 	DMA_SYNC(r->r_buf_tbl[start], 0, pktlen, DDI_DMA_SYNC_FORDEV);
2380 
2381 	/*
2382 	 * Increment TX producer count by one.
2383 	 */
2384 	ATGE_INC_SLOT(r->r_producer, ATGE_TX_RING_CNT);
2385 
2386 	/*
2387 	 * Sync descriptor table.
2388 	 */
2389 	DMA_SYNC(r->r_desc_ring, 0, ATGE_TX_RING_SZ, DDI_DMA_SYNC_FORDEV);
2390 
2391 	/*
2392 	 * Program TX descriptor to send a packet.
2393 	 */
2394 	if (ATGE_MODEL(atgep) == ATGE_CHIP_L1E) {
2395 		atge_l1e_send_packet(r);
2396 	} else if (ATGE_MODEL(atgep) == ATGE_CHIP_L1) {
2397 		atge_l1_send_packet(r);
2398 	}
2399 
2400 	r->r_atge->atge_opackets++;
2401 	r->r_atge->atge_obytes += pktlen;
2402 
2403 	ATGE_DB(("%s: %s() pktlen : %d, avail_desc : %d, producer  :%d, "
2404 	    "consumer : %d", atgep->atge_name, __func__, pktlen,
2405 	    r->r_avail_desc, r->r_producer, r->r_consumer));
2406 
2407 	return (DDI_SUCCESS);
2408 }
2409 
2410 /*
2411  * Stream Information.
2412  */
2413 DDI_DEFINE_STREAM_OPS(atge_devops, nulldev, nulldev, atge_attach, atge_detach,
2414     nodev, NULL, D_MP, NULL, atge_quiesce);
2415 
2416 /*
2417  * Module linkage information.
2418  */
2419 static	struct	modldrv	atge_modldrv = {
2420 	&mod_driverops,				/* Type of Module */
2421 	"Atheros/Attansic Gb Ethernet",		/* Description */
2422 	&atge_devops				/* drv_dev_ops */
2423 };
2424 
2425 static	struct	modlinkage atge_modlinkage = {
2426 	MODREV_1,			/* ml_rev */
2427 	(void *)&atge_modldrv,
2428 	NULL
2429 };
2430 
2431 /*
2432  * DDI Entry points.
2433  */
2434 int
2435 _init(void)
2436 {
2437 	int	r;
2438 	mac_init_ops(&atge_devops, "atge");
2439 	if ((r = mod_install(&atge_modlinkage)) != DDI_SUCCESS) {
2440 		mac_fini_ops(&atge_devops);
2441 	}
2442 
2443 	return (r);
2444 }
2445 
2446 int
2447 _fini(void)
2448 {
2449 	int	r;
2450 
2451 	if ((r = mod_remove(&atge_modlinkage)) == DDI_SUCCESS) {
2452 		mac_fini_ops(&atge_devops);
2453 	}
2454 
2455 	return (r);
2456 }
2457 
2458 int
2459 _info(struct modinfo *modinfop)
2460 {
2461 	return (mod_info(&atge_modlinkage, modinfop));
2462 }
2463