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