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