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