xref: /titanic_50/usr/src/uts/common/io/bge/bge_main2.c (revision 45916cd2fec6e79bca5dee0421bd39e3c2910d1e)
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 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "sys/bge_impl2.h"
30 #include <sys/sdt.h>
31 
32 /*
33  * This is the string displayed by modinfo, etc.
34  * Make sure you keep the version ID up to date!
35  */
36 static char bge_ident[] = "BCM579x driver v0.51";
37 
38 /*
39  * Property names
40  */
41 static char debug_propname[] = "bge-debug-flags";
42 static char clsize_propname[] = "cache-line-size";
43 static char latency_propname[] = "latency-timer";
44 static char localmac_boolname[] = "local-mac-address?";
45 static char localmac_propname[] = "local-mac-address";
46 static char macaddr_propname[] = "mac-address";
47 static char subdev_propname[] = "subsystem-id";
48 static char subven_propname[] = "subsystem-vendor-id";
49 static char rxrings_propname[] = "bge-rx-rings";
50 static char txrings_propname[] = "bge-tx-rings";
51 static char default_mtu[] = "default-mtu";
52 
53 static int bge_add_intrs(bge_t *, int);
54 static void bge_rem_intrs(bge_t *);
55 
56 /*
57  * Describes the chip's DMA engine
58  */
59 static ddi_dma_attr_t dma_attr = {
60 	DMA_ATTR_V0,			/* dma_attr version	*/
61 	0x0000000000000000ull,		/* dma_attr_addr_lo	*/
62 	0xFFFFFFFFFFFFFFFFull,		/* dma_attr_addr_hi	*/
63 	0x00000000FFFFFFFFull,		/* dma_attr_count_max	*/
64 	0x0000000000000001ull,		/* dma_attr_align	*/
65 	0x00000FFF,			/* dma_attr_burstsizes	*/
66 	0x00000001,			/* dma_attr_minxfer	*/
67 	0x000000000000FFFFull,		/* dma_attr_maxxfer	*/
68 	0xFFFFFFFFFFFFFFFFull,		/* dma_attr_seg		*/
69 	1,				/* dma_attr_sgllen 	*/
70 	0x00000001,			/* dma_attr_granular 	*/
71 	0				/* dma_attr_flags 	*/
72 };
73 
74 /*
75  * PIO access attributes for registers
76  */
77 static ddi_device_acc_attr_t bge_reg_accattr = {
78 	DDI_DEVICE_ATTR_V0,
79 	DDI_NEVERSWAP_ACC,
80 	DDI_STRICTORDER_ACC
81 };
82 
83 /*
84  * DMA access attributes for descriptors: NOT to be byte swapped.
85  */
86 static ddi_device_acc_attr_t bge_desc_accattr = {
87 	DDI_DEVICE_ATTR_V0,
88 	DDI_NEVERSWAP_ACC,
89 	DDI_STRICTORDER_ACC
90 };
91 
92 /*
93  * DMA access attributes for data: NOT to be byte swapped.
94  */
95 static ddi_device_acc_attr_t bge_data_accattr = {
96 	DDI_DEVICE_ATTR_V0,
97 	DDI_NEVERSWAP_ACC,
98 	DDI_STRICTORDER_ACC
99 };
100 
101 static ether_addr_t bge_broadcast_addr = {
102 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff
103 };
104 
105 /*
106  * Versions of the O/S up to Solaris 8 didn't support network booting
107  * from any network interface except the first (NET0).  Patching this
108  * flag to a non-zero value will tell the driver to work around this
109  * limitation by creating an extra (internal) pathname node.  To do
110  * this, just add a line like the following to the CLIENT'S etc/system
111  * file ON THE ROOT FILESYSTEM SERVER before booting the client:
112  *
113  *	set bge:bge_net1_boot_support = 1;
114  */
115 static uint32_t bge_net1_boot_support = 1;
116 
117 /*
118  * ========== Transmit and receive ring reinitialisation ==========
119  */
120 
121 /*
122  * These <reinit> routines each reset the specified ring to an initial
123  * state, assuming that the corresponding <init> routine has already
124  * been called exactly once.
125  */
126 
127 static void
128 bge_reinit_send_ring(send_ring_t *srp)
129 {
130 	/*
131 	 * Reinitialise control variables ...
132 	 */
133 	ASSERT(srp->tx_flow == 0);
134 	srp->tx_next = 0;
135 	srp->tx_free = srp->desc.nslots;
136 
137 	ASSERT(mutex_owned(srp->tc_lock));
138 	srp->tc_next = 0;
139 
140 	/*
141 	 * Zero and sync all the h/w Send Buffer Descriptors
142 	 */
143 	DMA_ZERO(srp->desc);
144 	DMA_SYNC(srp->desc, DDI_DMA_SYNC_FORDEV);
145 }
146 
147 static void
148 bge_reinit_recv_ring(recv_ring_t *rrp)
149 {
150 	/*
151 	 * Reinitialise control variables ...
152 	 */
153 	rrp->rx_next = 0;
154 }
155 
156 static void
157 bge_reinit_buff_ring(buff_ring_t *brp, uint64_t ring)
158 {
159 	bge_rbd_t *hw_rbd_p;
160 	sw_rbd_t *srbdp;
161 	uint32_t bufsize;
162 	uint32_t nslots;
163 	uint32_t slot;
164 
165 	static uint16_t ring_type_flag[BGE_BUFF_RINGS_MAX] = {
166 		RBD_FLAG_STD_RING,
167 		RBD_FLAG_JUMBO_RING,
168 		RBD_FLAG_MINI_RING
169 	};
170 
171 	/*
172 	 * Zero, initialise and sync all the h/w Receive Buffer Descriptors
173 	 * Note: all the remaining fields (<type>, <flags>, <ip_cksum>,
174 	 * <tcp_udp_cksum>, <error_flag>, <vlan_tag>, and <reserved>)
175 	 * should be zeroed, and so don't need to be set up specifically
176 	 * once the whole area has been cleared.
177 	 */
178 	DMA_ZERO(brp->desc);
179 
180 	hw_rbd_p = DMA_VPTR(brp->desc);
181 	nslots = brp->desc.nslots;
182 	ASSERT(brp->buf[0].nslots == nslots/BGE_SPLIT);
183 	bufsize = brp->buf[0].size;
184 	srbdp = brp->sw_rbds;
185 	for (slot = 0; slot < nslots; ++hw_rbd_p, ++srbdp, ++slot) {
186 		hw_rbd_p->host_buf_addr = srbdp->pbuf.cookie.dmac_laddress;
187 		hw_rbd_p->index = slot;
188 		hw_rbd_p->len = bufsize;
189 		hw_rbd_p->opaque = srbdp->pbuf.token;
190 		hw_rbd_p->flags |= ring_type_flag[ring];
191 	}
192 
193 	DMA_SYNC(brp->desc, DDI_DMA_SYNC_FORDEV);
194 
195 	/*
196 	 * Finally, reinitialise the ring control variables ...
197 	 */
198 	brp->rf_next = (nslots != 0) ? (nslots-1) : 0;
199 }
200 
201 /*
202  * Reinitialize all rings
203  */
204 static void
205 bge_reinit_rings(bge_t *bgep)
206 {
207 	uint64_t ring;
208 
209 	ASSERT(mutex_owned(bgep->genlock));
210 
211 	/*
212 	 * Send Rings ...
213 	 */
214 	for (ring = 0; ring < bgep->chipid.tx_rings; ++ring)
215 		bge_reinit_send_ring(&bgep->send[ring]);
216 
217 	/*
218 	 * Receive Return Rings ...
219 	 */
220 	for (ring = 0; ring < bgep->chipid.rx_rings; ++ring)
221 		bge_reinit_recv_ring(&bgep->recv[ring]);
222 
223 	/*
224 	 * Receive Producer Rings ...
225 	 */
226 	for (ring = 0; ring < BGE_BUFF_RINGS_USED; ++ring)
227 		bge_reinit_buff_ring(&bgep->buff[ring], ring);
228 }
229 
230 /*
231  * ========== Internal state management entry points ==========
232  */
233 
234 #undef	BGE_DBG
235 #define	BGE_DBG		BGE_DBG_NEMO	/* debug flag for this code	*/
236 
237 /*
238  * These routines provide all the functionality required by the
239  * corresponding GLD entry points, but don't update the GLD state
240  * so they can be called internally without disturbing our record
241  * of what GLD thinks we should be doing ...
242  */
243 
244 /*
245  *	bge_reset() -- reset h/w & rings to initial state
246  */
247 static void
248 #ifdef BGE_IPMI_ASF
249 bge_reset(bge_t *bgep, uint_t asf_mode)
250 #else
251 bge_reset(bge_t *bgep)
252 #endif
253 {
254 	uint64_t	ring;
255 
256 	BGE_TRACE(("bge_reset($%p)", (void *)bgep));
257 
258 	ASSERT(mutex_owned(bgep->genlock));
259 
260 	/*
261 	 * Grab all the other mutexes in the world (this should
262 	 * ensure no other threads are manipulating driver state)
263 	 */
264 	for (ring = 0; ring < BGE_RECV_RINGS_MAX; ++ring)
265 		mutex_enter(bgep->recv[ring].rx_lock);
266 	for (ring = 0; ring < BGE_BUFF_RINGS_MAX; ++ring)
267 		mutex_enter(bgep->buff[ring].rf_lock);
268 	rw_enter(bgep->errlock, RW_WRITER);
269 	for (ring = 0; ring < BGE_SEND_RINGS_MAX; ++ring)
270 		mutex_enter(bgep->send[ring].tc_lock);
271 
272 #ifdef BGE_IPMI_ASF
273 	bge_chip_reset(bgep, B_TRUE, asf_mode);
274 #else
275 	bge_chip_reset(bgep, B_TRUE);
276 #endif
277 	bge_reinit_rings(bgep);
278 
279 	/*
280 	 * Free the world ...
281 	 */
282 	for (ring = BGE_SEND_RINGS_MAX; ring-- > 0; )
283 		mutex_exit(bgep->send[ring].tc_lock);
284 	rw_exit(bgep->errlock);
285 	for (ring = BGE_BUFF_RINGS_MAX; ring-- > 0; )
286 		mutex_exit(bgep->buff[ring].rf_lock);
287 	for (ring = BGE_RECV_RINGS_MAX; ring-- > 0; )
288 		mutex_exit(bgep->recv[ring].rx_lock);
289 
290 	BGE_DEBUG(("bge_reset($%p) done", (void *)bgep));
291 }
292 
293 /*
294  *	bge_stop() -- stop processing, don't reset h/w or rings
295  */
296 static void
297 bge_stop(bge_t *bgep)
298 {
299 	BGE_TRACE(("bge_stop($%p)", (void *)bgep));
300 
301 	ASSERT(mutex_owned(bgep->genlock));
302 
303 #ifdef BGE_IPMI_ASF
304 	if (bgep->asf_enabled) {
305 		bgep->asf_pseudostop = B_TRUE;
306 	} else {
307 #endif
308 		bge_chip_stop(bgep, B_FALSE);
309 #ifdef BGE_IPMI_ASF
310 	}
311 #endif
312 
313 	BGE_DEBUG(("bge_stop($%p) done", (void *)bgep));
314 }
315 
316 /*
317  *	bge_start() -- start transmitting/receiving
318  */
319 static void
320 bge_start(bge_t *bgep, boolean_t reset_phys)
321 {
322 	BGE_TRACE(("bge_start($%p, %d)", (void *)bgep, reset_phys));
323 
324 	ASSERT(mutex_owned(bgep->genlock));
325 
326 	/*
327 	 * Start chip processing, including enabling interrupts
328 	 */
329 	bge_chip_start(bgep, reset_phys);
330 
331 	BGE_DEBUG(("bge_start($%p, %d) done", (void *)bgep, reset_phys));
332 }
333 
334 /*
335  * bge_restart - restart transmitting/receiving after error or suspend
336  */
337 void
338 bge_restart(bge_t *bgep, boolean_t reset_phys)
339 {
340 	ASSERT(mutex_owned(bgep->genlock));
341 
342 #ifdef BGE_IPMI_ASF
343 	if (bgep->asf_enabled) {
344 		bge_reset(bgep, ASF_MODE_POST_INIT);
345 	} else
346 		bge_reset(bgep, ASF_MODE_NONE);
347 #else
348 	bge_reset(bgep);
349 #endif
350 	if (bgep->bge_mac_state == BGE_MAC_STARTED) {
351 		bge_start(bgep, reset_phys);
352 		bgep->watchdog = 0;
353 		ddi_trigger_softintr(bgep->resched_id);
354 	}
355 
356 	BGE_DEBUG(("bge_restart($%p, %d) done", (void *)bgep, reset_phys));
357 }
358 
359 
360 /*
361  * ========== Nemo-required management entry points ==========
362  */
363 
364 #undef	BGE_DBG
365 #define	BGE_DBG		BGE_DBG_NEMO	/* debug flag for this code	*/
366 
367 /*
368  *	bge_m_stop() -- stop transmitting/receiving
369  */
370 static void
371 bge_m_stop(void *arg)
372 {
373 	bge_t *bgep = arg;		/* private device info	*/
374 
375 	BGE_TRACE(("bge_m_stop($%p)", arg));
376 
377 	/*
378 	 * Just stop processing, then record new GLD state
379 	 */
380 	mutex_enter(bgep->genlock);
381 	bgep->link_up_msg = bgep->link_down_msg = " (stopped)";
382 	bge_stop(bgep);
383 	bgep->bge_mac_state = BGE_MAC_STOPPED;
384 	BGE_DEBUG(("bge_m_stop($%p) done", arg));
385 	mutex_exit(bgep->genlock);
386 }
387 
388 /*
389  *	bge_m_start() -- start transmitting/receiving
390  */
391 static int
392 bge_m_start(void *arg)
393 {
394 	bge_t *bgep = arg;		/* private device info	*/
395 
396 	BGE_TRACE(("bge_m_start($%p)", arg));
397 
398 	/*
399 	 * Start processing and record new GLD state
400 	 */
401 	mutex_enter(bgep->genlock);
402 #ifdef BGE_IPMI_ASF
403 	if (bgep->asf_enabled) {
404 		if ((bgep->asf_status == ASF_STAT_RUN) &&
405 			(bgep->asf_pseudostop)) {
406 
407 			bgep->link_up_msg = bgep->link_down_msg
408 				= " (initialized)";
409 			bgep->bge_mac_state = BGE_MAC_STARTED;
410 			mutex_exit(bgep->genlock);
411 			return (0);
412 		}
413 	}
414 	bge_reset(bgep, ASF_MODE_INIT);
415 #else
416 	bge_reset(bgep);
417 #endif
418 	bgep->link_up_msg = bgep->link_down_msg = " (initialized)";
419 	bge_start(bgep, B_TRUE);
420 	bgep->bge_mac_state = BGE_MAC_STARTED;
421 	BGE_DEBUG(("bge_m_start($%p) done", arg));
422 
423 #ifdef BGE_IPMI_ASF
424 	if (bgep->asf_enabled) {
425 		if (bgep->asf_status != ASF_STAT_RUN) {
426 			/* start ASF heart beat */
427 			bgep->asf_timeout_id = timeout(bge_asf_heartbeat,
428 				(void *)bgep,
429 				drv_usectohz(BGE_ASF_HEARTBEAT_INTERVAL));
430 			bgep->asf_status = ASF_STAT_RUN;
431 		}
432 	}
433 #endif
434 	mutex_exit(bgep->genlock);
435 
436 	return (0);
437 }
438 
439 /*
440  *	bge_m_unicst_set() -- set the physical network address
441  */
442 static int
443 bge_m_unicst(void *arg, const uint8_t *macaddr)
444 {
445 	bge_t *bgep = arg;		/* private device info	*/
446 
447 	BGE_TRACE(("bge_m_unicst_set($%p, %s)", arg,
448 		ether_sprintf((void *)macaddr)));
449 
450 	/*
451 	 * Remember the new current address in the driver state
452 	 * Sync the chip's idea of the address too ...
453 	 */
454 	mutex_enter(bgep->genlock);
455 	ethaddr_copy(macaddr, bgep->curr_addr.addr);
456 #ifdef BGE_IPMI_ASF
457 	bge_chip_sync(bgep, B_FALSE);
458 	if (bgep->asf_enabled) {
459 		/*
460 		 * The above bge_chip_sync() function wrote the ethernet MAC
461 		 * addresses registers which destroyed the IPMI/ASF sideband.
462 		 * Here, we have to reset chip to make IPMI/ASF sideband work.
463 		 */
464 		if (bgep->asf_status == ASF_STAT_RUN) {
465 			/*
466 			 * We must stop ASF heart beat before bge_chip_stop(),
467 			 * otherwise some computers (ex. IBM HS20 blade server)
468 			 * may crash.
469 			 */
470 			bge_asf_update_status(bgep);
471 			bge_asf_stop_timer(bgep);
472 			bgep->asf_status = ASF_STAT_STOP;
473 
474 			bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
475 		}
476 		bge_chip_stop(bgep, B_TRUE);
477 
478 		bge_restart(bgep, B_FALSE);
479 		/*
480 		 * Start our ASF heartbeat counter as soon as possible.
481 		 */
482 		if (bgep->asf_status != ASF_STAT_RUN) {
483 			/* start ASF heart beat */
484 			bgep->asf_timeout_id = timeout(bge_asf_heartbeat,
485 				(void *)bgep,
486 				drv_usectohz(BGE_ASF_HEARTBEAT_INTERVAL));
487 			bgep->asf_status = ASF_STAT_RUN;
488 		}
489 	}
490 #else
491 	bge_chip_sync(bgep);
492 #endif
493 	BGE_DEBUG(("bge_m_unicst_set($%p) done", arg));
494 	mutex_exit(bgep->genlock);
495 
496 	return (0);
497 }
498 
499 /*
500  * Compute the index of the required bit in the multicast hash map.
501  * This must mirror the way the hardware actually does it!
502  * See Broadcom document 570X-PG102-R page 125.
503  */
504 static uint32_t
505 bge_hash_index(const uint8_t *mca)
506 {
507 	uint32_t hash;
508 
509 	CRC32(hash, mca, ETHERADDRL, -1U, crc32_table);
510 
511 	return (hash);
512 }
513 
514 /*
515  *	bge_m_multicst_add() -- enable/disable a multicast address
516  */
517 static int
518 bge_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
519 {
520 	bge_t *bgep = arg;		/* private device info	*/
521 	uint32_t hash;
522 	uint32_t index;
523 	uint32_t word;
524 	uint32_t bit;
525 	uint8_t *refp;
526 
527 	BGE_TRACE(("bge_m_multicst($%p, %s, %s)", arg,
528 		(add) ? "add" : "remove", ether_sprintf((void *)mca)));
529 
530 	/*
531 	 * Precalculate all required masks, pointers etc ...
532 	 */
533 	hash = bge_hash_index(mca);
534 	index = hash % BGE_HASH_TABLE_SIZE;
535 	word = index/32u;
536 	bit = 1 << (index % 32u);
537 	refp = &bgep->mcast_refs[index];
538 
539 	BGE_DEBUG(("bge_m_multicst: hash 0x%x index %d (%d:0x%x) = %d",
540 		hash, index, word, bit, *refp));
541 
542 	/*
543 	 * We must set the appropriate bit in the hash map (and the
544 	 * corresponding h/w register) when the refcount goes from 0
545 	 * to >0, and clear it when the last ref goes away (refcount
546 	 * goes from >0 back to 0).  If we change the hash map, we
547 	 * must also update the chip's hardware map registers.
548 	 */
549 	mutex_enter(bgep->genlock);
550 	if (add) {
551 		if ((*refp)++ == 0) {
552 			bgep->mcast_hash[word] |= bit;
553 #ifdef BGE_IPMI_ASF
554 			bge_chip_sync(bgep, B_TRUE);
555 #else
556 			bge_chip_sync(bgep);
557 #endif
558 		}
559 	} else {
560 		if (--(*refp) == 0) {
561 			bgep->mcast_hash[word] &= ~bit;
562 #ifdef BGE_IPMI_ASF
563 			bge_chip_sync(bgep, B_TRUE);
564 #else
565 			bge_chip_sync(bgep);
566 #endif
567 		}
568 	}
569 	BGE_DEBUG(("bge_m_multicst($%p) done", arg));
570 	mutex_exit(bgep->genlock);
571 
572 	return (0);
573 }
574 
575 /*
576  * bge_m_promisc() -- set or reset promiscuous mode on the board
577  *
578  *	Program the hardware to enable/disable promiscuous and/or
579  *	receive-all-multicast modes.
580  */
581 static int
582 bge_m_promisc(void *arg, boolean_t on)
583 {
584 	bge_t *bgep = arg;
585 
586 	BGE_TRACE(("bge_m_promisc_set($%p, %d)", arg, on));
587 
588 	/*
589 	 * Store MAC layer specified mode and pass to chip layer to update h/w
590 	 */
591 	mutex_enter(bgep->genlock);
592 	bgep->promisc = on;
593 #ifdef BGE_IPMI_ASF
594 	bge_chip_sync(bgep, B_TRUE);
595 #else
596 	bge_chip_sync(bgep);
597 #endif
598 	BGE_DEBUG(("bge_m_promisc_set($%p) done", arg));
599 	mutex_exit(bgep->genlock);
600 	return (0);
601 }
602 
603 /*
604  * Loopback ioctl code
605  */
606 
607 static lb_property_t loopmodes[] = {
608 	{ normal,	"normal",	BGE_LOOP_NONE		},
609 	{ external,	"1000Mbps",	BGE_LOOP_EXTERNAL_1000	},
610 	{ external,	"100Mbps",	BGE_LOOP_EXTERNAL_100	},
611 	{ external,	"10Mbps",	BGE_LOOP_EXTERNAL_10	},
612 	{ internal,	"PHY",		BGE_LOOP_INTERNAL_PHY	},
613 	{ internal,	"MAC",		BGE_LOOP_INTERNAL_MAC	}
614 };
615 
616 static enum ioc_reply
617 bge_set_loop_mode(bge_t *bgep, uint32_t mode)
618 {
619 	const char *msg;
620 
621 	/*
622 	 * If the mode isn't being changed, there's nothing to do ...
623 	 */
624 	if (mode == bgep->param_loop_mode)
625 		return (IOC_ACK);
626 
627 	/*
628 	 * Validate the requested mode and prepare a suitable message
629 	 * to explain the link down/up cycle that the change will
630 	 * probably induce ...
631 	 */
632 	switch (mode) {
633 	default:
634 		return (IOC_INVAL);
635 
636 	case BGE_LOOP_NONE:
637 		msg = " (loopback disabled)";
638 		break;
639 
640 	case BGE_LOOP_EXTERNAL_1000:
641 	case BGE_LOOP_EXTERNAL_100:
642 	case BGE_LOOP_EXTERNAL_10:
643 		msg = " (external loopback selected)";
644 		break;
645 
646 	case BGE_LOOP_INTERNAL_PHY:
647 		msg = " (PHY internal loopback selected)";
648 		break;
649 
650 	case BGE_LOOP_INTERNAL_MAC:
651 		msg = " (MAC internal loopback selected)";
652 		break;
653 	}
654 
655 	/*
656 	 * All OK; tell the caller to reprogram
657 	 * the PHY and/or MAC for the new mode ...
658 	 */
659 	bgep->link_down_msg = bgep->link_up_msg = msg;
660 	bgep->param_loop_mode = mode;
661 	return (IOC_RESTART_ACK);
662 }
663 
664 static enum ioc_reply
665 bge_loop_ioctl(bge_t *bgep, queue_t *wq, mblk_t *mp, struct iocblk *iocp)
666 {
667 	lb_info_sz_t *lbsp;
668 	lb_property_t *lbpp;
669 	uint32_t *lbmp;
670 	int cmd;
671 
672 	_NOTE(ARGUNUSED(wq))
673 
674 	/*
675 	 * Validate format of ioctl
676 	 */
677 	if (mp->b_cont == NULL)
678 		return (IOC_INVAL);
679 
680 	cmd = iocp->ioc_cmd;
681 	switch (cmd) {
682 	default:
683 		/* NOTREACHED */
684 		bge_error(bgep, "bge_loop_ioctl: invalid cmd 0x%x", cmd);
685 		return (IOC_INVAL);
686 
687 	case LB_GET_INFO_SIZE:
688 		if (iocp->ioc_count != sizeof (lb_info_sz_t))
689 			return (IOC_INVAL);
690 		lbsp = (lb_info_sz_t *)mp->b_cont->b_rptr;
691 		*lbsp = sizeof (loopmodes);
692 		return (IOC_REPLY);
693 
694 	case LB_GET_INFO:
695 		if (iocp->ioc_count != sizeof (loopmodes))
696 			return (IOC_INVAL);
697 		lbpp = (lb_property_t *)mp->b_cont->b_rptr;
698 		bcopy(loopmodes, lbpp, sizeof (loopmodes));
699 		return (IOC_REPLY);
700 
701 	case LB_GET_MODE:
702 		if (iocp->ioc_count != sizeof (uint32_t))
703 			return (IOC_INVAL);
704 		lbmp = (uint32_t *)mp->b_cont->b_rptr;
705 		*lbmp = bgep->param_loop_mode;
706 		return (IOC_REPLY);
707 
708 	case LB_SET_MODE:
709 		if (iocp->ioc_count != sizeof (uint32_t))
710 			return (IOC_INVAL);
711 		lbmp = (uint32_t *)mp->b_cont->b_rptr;
712 		return (bge_set_loop_mode(bgep, *lbmp));
713 	}
714 }
715 
716 /*
717  * Specific bge IOCTLs, the gld module handles the generic ones.
718  */
719 static void
720 bge_m_ioctl(void *arg, queue_t *wq, mblk_t *mp)
721 {
722 	bge_t *bgep = arg;
723 	struct iocblk *iocp;
724 	enum ioc_reply status;
725 	boolean_t need_privilege;
726 	int err;
727 	int cmd;
728 
729 	/*
730 	 * Validate the command before bothering with the mutex ...
731 	 */
732 	iocp = (struct iocblk *)mp->b_rptr;
733 	iocp->ioc_error = 0;
734 	need_privilege = B_TRUE;
735 	cmd = iocp->ioc_cmd;
736 	switch (cmd) {
737 	default:
738 		miocnak(wq, mp, 0, EINVAL);
739 		return;
740 
741 	case BGE_MII_READ:
742 	case BGE_MII_WRITE:
743 	case BGE_SEE_READ:
744 	case BGE_SEE_WRITE:
745 	case BGE_DIAG:
746 	case BGE_PEEK:
747 	case BGE_POKE:
748 	case BGE_PHY_RESET:
749 	case BGE_SOFT_RESET:
750 	case BGE_HARD_RESET:
751 		break;
752 
753 	case LB_GET_INFO_SIZE:
754 	case LB_GET_INFO:
755 	case LB_GET_MODE:
756 		need_privilege = B_FALSE;
757 		/* FALLTHRU */
758 	case LB_SET_MODE:
759 		break;
760 
761 	case ND_GET:
762 		need_privilege = B_FALSE;
763 		/* FALLTHRU */
764 	case ND_SET:
765 		break;
766 	}
767 
768 	if (need_privilege) {
769 		/*
770 		 * Check for specific net_config privilege on Solaris 10+.
771 		 * Otherwise just check for root access ...
772 		 */
773 		if (secpolicy_net_config != NULL)
774 			err = secpolicy_net_config(iocp->ioc_cr, B_FALSE);
775 		else
776 			err = drv_priv(iocp->ioc_cr);
777 		if (err != 0) {
778 			miocnak(wq, mp, 0, err);
779 			return;
780 		}
781 	}
782 
783 	mutex_enter(bgep->genlock);
784 
785 	switch (cmd) {
786 	default:
787 		_NOTE(NOTREACHED)
788 		status = IOC_INVAL;
789 		break;
790 
791 	case BGE_MII_READ:
792 	case BGE_MII_WRITE:
793 	case BGE_SEE_READ:
794 	case BGE_SEE_WRITE:
795 	case BGE_DIAG:
796 	case BGE_PEEK:
797 	case BGE_POKE:
798 	case BGE_PHY_RESET:
799 	case BGE_SOFT_RESET:
800 	case BGE_HARD_RESET:
801 		status = bge_chip_ioctl(bgep, wq, mp, iocp);
802 		break;
803 
804 	case LB_GET_INFO_SIZE:
805 	case LB_GET_INFO:
806 	case LB_GET_MODE:
807 	case LB_SET_MODE:
808 		status = bge_loop_ioctl(bgep, wq, mp, iocp);
809 		break;
810 
811 	case ND_GET:
812 	case ND_SET:
813 		status = bge_nd_ioctl(bgep, wq, mp, iocp);
814 		break;
815 	}
816 
817 	/*
818 	 * Do we need to reprogram the PHY and/or the MAC?
819 	 * Do it now, while we still have the mutex.
820 	 *
821 	 * Note: update the PHY first, 'cos it controls the
822 	 * speed/duplex parameters that the MAC code uses.
823 	 */
824 	switch (status) {
825 	case IOC_RESTART_REPLY:
826 	case IOC_RESTART_ACK:
827 		bge_phys_update(bgep);
828 #ifdef BGE_IPMI_ASF
829 		bge_chip_sync(bgep, B_FALSE);
830 #else
831 		bge_chip_sync(bgep);
832 #endif
833 		if (bgep->intr_type == DDI_INTR_TYPE_MSI)
834 			bge_chip_msi_trig(bgep);
835 		break;
836 	}
837 
838 	mutex_exit(bgep->genlock);
839 
840 	/*
841 	 * Finally, decide how to reply
842 	 */
843 	switch (status) {
844 	default:
845 	case IOC_INVAL:
846 		/*
847 		 * Error, reply with a NAK and EINVAL or the specified error
848 		 */
849 		miocnak(wq, mp, 0, iocp->ioc_error == 0 ?
850 			EINVAL : iocp->ioc_error);
851 		break;
852 
853 	case IOC_DONE:
854 		/*
855 		 * OK, reply already sent
856 		 */
857 		break;
858 
859 	case IOC_RESTART_ACK:
860 	case IOC_ACK:
861 		/*
862 		 * OK, reply with an ACK
863 		 */
864 		miocack(wq, mp, 0, 0);
865 		break;
866 
867 	case IOC_RESTART_REPLY:
868 	case IOC_REPLY:
869 		/*
870 		 * OK, send prepared reply as ACK or NAK
871 		 */
872 		mp->b_datap->db_type = iocp->ioc_error == 0 ?
873 			M_IOCACK : M_IOCNAK;
874 		qreply(wq, mp);
875 		break;
876 	}
877 }
878 
879 static void
880 bge_m_resources(void *arg)
881 {
882 	bge_t *bgep = arg;
883 	recv_ring_t *rrp;
884 	mac_rx_fifo_t mrf;
885 	int ring;
886 
887 	mutex_enter(bgep->genlock);
888 
889 	/*
890 	 * Register Rx rings as resources and save mac
891 	 * resource id for future reference
892 	 */
893 	mrf.mrf_type = MAC_RX_FIFO;
894 	mrf.mrf_blank = bge_chip_blank;
895 	mrf.mrf_arg = (void *)bgep;
896 	mrf.mrf_normal_blank_time = bge_rx_ticks_norm;
897 	mrf.mrf_normal_pkt_count = bge_rx_count_norm;
898 
899 	for (ring = 0; ring < bgep->chipid.rx_rings; ring++) {
900 		rrp = &bgep->recv[ring];
901 		rrp->handle = mac_resource_add(bgep->macp,
902 		    (mac_resource_t *)&mrf);
903 	}
904 
905 	mutex_exit(bgep->genlock);
906 }
907 
908 /*
909  * ========== Per-instance setup/teardown code ==========
910  */
911 
912 #undef	BGE_DBG
913 #define	BGE_DBG		BGE_DBG_INIT	/* debug flag for this code	*/
914 
915 /*
916  * Utility routine to carve a slice off a chunk of allocated memory,
917  * updating the chunk descriptor accordingly.  The size of the slice
918  * is given by the product of the <qty> and <size> parameters.
919  */
920 static void
921 bge_slice_chunk(dma_area_t *slice, dma_area_t *chunk,
922 	uint32_t qty, uint32_t size)
923 {
924 	static uint32_t sequence = 0xbcd5704a;
925 	size_t totsize;
926 
927 	totsize = qty*size;
928 	ASSERT(size >= 0);
929 	ASSERT(totsize <= chunk->alength);
930 
931 	*slice = *chunk;
932 	slice->nslots = qty;
933 	slice->size = size;
934 	slice->alength = totsize;
935 	slice->token = ++sequence;
936 
937 	chunk->mem_va = (caddr_t)chunk->mem_va + totsize;
938 	chunk->alength -= totsize;
939 	chunk->offset += totsize;
940 	chunk->cookie.dmac_laddress += totsize;
941 	chunk->cookie.dmac_size -= totsize;
942 }
943 
944 /*
945  * Initialise the specified Receive Producer (Buffer) Ring, using
946  * the information in the <dma_area> descriptors that it contains
947  * to set up all the other fields. This routine should be called
948  * only once for each ring.
949  */
950 static void
951 bge_init_buff_ring(bge_t *bgep, uint64_t ring)
952 {
953 	buff_ring_t *brp;
954 	bge_status_t *bsp;
955 	sw_rbd_t *srbdp;
956 	dma_area_t pbuf;
957 	uint32_t bufsize;
958 	uint32_t nslots;
959 	uint32_t slot;
960 	uint32_t split;
961 
962 	static bge_regno_t nic_ring_addrs[BGE_BUFF_RINGS_MAX] = {
963 		NIC_MEM_SHADOW_BUFF_STD,
964 		NIC_MEM_SHADOW_BUFF_JUMBO,
965 		NIC_MEM_SHADOW_BUFF_MINI
966 	};
967 	static bge_regno_t mailbox_regs[BGE_BUFF_RINGS_MAX] = {
968 		RECV_STD_PROD_INDEX_REG,
969 		RECV_JUMBO_PROD_INDEX_REG,
970 		RECV_MINI_PROD_INDEX_REG
971 	};
972 	static bge_regno_t buff_cons_xref[BGE_BUFF_RINGS_MAX] = {
973 		STATUS_STD_BUFF_CONS_INDEX,
974 		STATUS_JUMBO_BUFF_CONS_INDEX,
975 		STATUS_MINI_BUFF_CONS_INDEX
976 	};
977 
978 	BGE_TRACE(("bge_init_buff_ring($%p, %d)",
979 		(void *)bgep, ring));
980 
981 	brp = &bgep->buff[ring];
982 	nslots = brp->desc.nslots;
983 	ASSERT(brp->buf[0].nslots == nslots/BGE_SPLIT);
984 	bufsize = brp->buf[0].size;
985 
986 	/*
987 	 * Set up the copy of the h/w RCB
988 	 *
989 	 * Note: unlike Send & Receive Return Rings, (where the max_len
990 	 * field holds the number of slots), in a Receive Buffer Ring
991 	 * this field indicates the size of each buffer in the ring.
992 	 */
993 	brp->hw_rcb.host_ring_addr = brp->desc.cookie.dmac_laddress;
994 	brp->hw_rcb.max_len = bufsize;
995 	brp->hw_rcb.flags = nslots > 0 ? 0 : RCB_FLAG_RING_DISABLED;
996 	brp->hw_rcb.nic_ring_addr = nic_ring_addrs[ring];
997 
998 	/*
999 	 * Other one-off initialisation of per-ring data
1000 	 */
1001 	brp->bgep = bgep;
1002 	bsp = DMA_VPTR(bgep->status_block);
1003 	brp->cons_index_p = &bsp->buff_cons_index[buff_cons_xref[ring]];
1004 	brp->chip_mbx_reg = mailbox_regs[ring];
1005 	mutex_init(brp->rf_lock, NULL, MUTEX_DRIVER,
1006 	    DDI_INTR_PRI(bgep->intr_pri));
1007 
1008 	/*
1009 	 * Allocate the array of s/w Receive Buffer Descriptors
1010 	 */
1011 	srbdp = kmem_zalloc(nslots*sizeof (*srbdp), KM_SLEEP);
1012 	brp->sw_rbds = srbdp;
1013 
1014 	/*
1015 	 * Now initialise each array element once and for all
1016 	 */
1017 	for (split = 0; split < BGE_SPLIT; ++split) {
1018 		pbuf = brp->buf[split];
1019 		for (slot = 0; slot < nslots/BGE_SPLIT; ++srbdp, ++slot)
1020 			bge_slice_chunk(&srbdp->pbuf, &pbuf, 1, bufsize);
1021 		ASSERT(pbuf.alength == 0);
1022 	}
1023 }
1024 
1025 /*
1026  * Clean up initialisation done above before the memory is freed
1027  */
1028 static void
1029 bge_fini_buff_ring(bge_t *bgep, uint64_t ring)
1030 {
1031 	buff_ring_t *brp;
1032 	sw_rbd_t *srbdp;
1033 
1034 	BGE_TRACE(("bge_fini_buff_ring($%p, %d)",
1035 		(void *)bgep, ring));
1036 
1037 	brp = &bgep->buff[ring];
1038 	srbdp = brp->sw_rbds;
1039 	kmem_free(srbdp, brp->desc.nslots*sizeof (*srbdp));
1040 
1041 	mutex_destroy(brp->rf_lock);
1042 }
1043 
1044 /*
1045  * Initialise the specified Receive (Return) Ring, using the
1046  * information in the <dma_area> descriptors that it contains
1047  * to set up all the other fields. This routine should be called
1048  * only once for each ring.
1049  */
1050 static void
1051 bge_init_recv_ring(bge_t *bgep, uint64_t ring)
1052 {
1053 	recv_ring_t *rrp;
1054 	bge_status_t *bsp;
1055 	uint32_t nslots;
1056 
1057 	BGE_TRACE(("bge_init_recv_ring($%p, %d)",
1058 		(void *)bgep, ring));
1059 
1060 	/*
1061 	 * The chip architecture requires that receive return rings have
1062 	 * 512 or 1024 or 2048 elements per ring.  See 570X-PG108-R page 103.
1063 	 */
1064 	rrp = &bgep->recv[ring];
1065 	nslots = rrp->desc.nslots;
1066 	ASSERT(nslots == 0 || nslots == 512 ||
1067 		nslots == 1024 || nslots == 2048);
1068 
1069 	/*
1070 	 * Set up the copy of the h/w RCB
1071 	 */
1072 	rrp->hw_rcb.host_ring_addr = rrp->desc.cookie.dmac_laddress;
1073 	rrp->hw_rcb.max_len = nslots;
1074 	rrp->hw_rcb.flags = nslots > 0 ? 0 : RCB_FLAG_RING_DISABLED;
1075 	rrp->hw_rcb.nic_ring_addr = 0;
1076 
1077 	/*
1078 	 * Other one-off initialisation of per-ring data
1079 	 */
1080 	rrp->bgep = bgep;
1081 	bsp = DMA_VPTR(bgep->status_block);
1082 	rrp->prod_index_p = RECV_INDEX_P(bsp, ring);
1083 	rrp->chip_mbx_reg = RECV_RING_CONS_INDEX_REG(ring);
1084 	mutex_init(rrp->rx_lock, NULL, MUTEX_DRIVER,
1085 	    DDI_INTR_PRI(bgep->intr_pri));
1086 }
1087 
1088 
1089 /*
1090  * Clean up initialisation done above before the memory is freed
1091  */
1092 static void
1093 bge_fini_recv_ring(bge_t *bgep, uint64_t ring)
1094 {
1095 	recv_ring_t *rrp;
1096 
1097 	BGE_TRACE(("bge_fini_recv_ring($%p, %d)",
1098 		(void *)bgep, ring));
1099 
1100 	rrp = &bgep->recv[ring];
1101 	if (rrp->rx_softint)
1102 		ddi_remove_softintr(rrp->rx_softint);
1103 	mutex_destroy(rrp->rx_lock);
1104 }
1105 
1106 /*
1107  * Initialise the specified Send Ring, using the information in the
1108  * <dma_area> descriptors that it contains to set up all the other
1109  * fields. This routine should be called only once for each ring.
1110  */
1111 static void
1112 bge_init_send_ring(bge_t *bgep, uint64_t ring)
1113 {
1114 	send_ring_t *srp;
1115 	bge_status_t *bsp;
1116 	sw_sbd_t *ssbdp;
1117 	dma_area_t desc;
1118 	dma_area_t pbuf;
1119 	uint32_t nslots;
1120 	uint32_t slot;
1121 	uint32_t split;
1122 
1123 	BGE_TRACE(("bge_init_send_ring($%p, %d)",
1124 		(void *)bgep, ring));
1125 
1126 	/*
1127 	 * The chip architecture requires that host-based send rings
1128 	 * have 512 elements per ring.  See 570X-PG102-R page 56.
1129 	 */
1130 	srp = &bgep->send[ring];
1131 	nslots = srp->desc.nslots;
1132 	ASSERT(nslots == 0 || nslots == 512);
1133 
1134 	/*
1135 	 * Set up the copy of the h/w RCB
1136 	 */
1137 	srp->hw_rcb.host_ring_addr = srp->desc.cookie.dmac_laddress;
1138 	srp->hw_rcb.max_len = nslots;
1139 	srp->hw_rcb.flags = nslots > 0 ? 0 : RCB_FLAG_RING_DISABLED;
1140 	srp->hw_rcb.nic_ring_addr = NIC_MEM_SHADOW_SEND_RING(ring, nslots);
1141 
1142 	/*
1143 	 * Other one-off initialisation of per-ring data
1144 	 */
1145 	srp->bgep = bgep;
1146 	bsp = DMA_VPTR(bgep->status_block);
1147 	srp->cons_index_p = SEND_INDEX_P(bsp, ring);
1148 	srp->chip_mbx_reg = SEND_RING_HOST_INDEX_REG(ring);
1149 	mutex_init(srp->tx_lock, NULL, MUTEX_DRIVER,
1150 	    DDI_INTR_PRI(bgep->intr_pri));
1151 	mutex_init(srp->tc_lock, NULL, MUTEX_DRIVER,
1152 	    DDI_INTR_PRI(bgep->intr_pri));
1153 
1154 	/*
1155 	 * Allocate the array of s/w Send Buffer Descriptors
1156 	 */
1157 	ssbdp = kmem_zalloc(nslots*sizeof (*ssbdp), KM_SLEEP);
1158 	srp->sw_sbds = ssbdp;
1159 
1160 	/*
1161 	 * Now initialise each array element once and for all
1162 	 */
1163 	desc = srp->desc;
1164 	for (split = 0; split < BGE_SPLIT; ++split) {
1165 		pbuf = srp->buf[split];
1166 		for (slot = 0; slot < nslots/BGE_SPLIT; ++ssbdp, ++slot) {
1167 			bge_slice_chunk(&ssbdp->desc, &desc, 1,
1168 				sizeof (bge_sbd_t));
1169 			bge_slice_chunk(&ssbdp->pbuf, &pbuf, 1,
1170 				bgep->chipid.snd_buff_size);
1171 		}
1172 		ASSERT(pbuf.alength == 0);
1173 	}
1174 	ASSERT(desc.alength == 0);
1175 }
1176 
1177 /*
1178  * Clean up initialisation done above before the memory is freed
1179  */
1180 static void
1181 bge_fini_send_ring(bge_t *bgep, uint64_t ring)
1182 {
1183 	send_ring_t *srp;
1184 	sw_sbd_t *ssbdp;
1185 
1186 	BGE_TRACE(("bge_fini_send_ring($%p, %d)",
1187 		(void *)bgep, ring));
1188 
1189 	srp = &bgep->send[ring];
1190 	ssbdp = srp->sw_sbds;
1191 	kmem_free(ssbdp, srp->desc.nslots*sizeof (*ssbdp));
1192 
1193 	mutex_destroy(srp->tx_lock);
1194 	mutex_destroy(srp->tc_lock);
1195 }
1196 
1197 /*
1198  * Initialise all transmit, receive, and buffer rings.
1199  * (also a few top-level mutexen that can't be done until
1200  * the h/w interrupt handler has been registered 'cos we
1201  * need the cookie).
1202  */
1203 static void
1204 bge_init_rings(bge_t *bgep)
1205 {
1206 	uint64_t ring;
1207 
1208 	BGE_TRACE(("bge_init_rings($%p)", (void *)bgep));
1209 
1210 	mutex_init(bgep->genlock, NULL, MUTEX_DRIVER,
1211 	    DDI_INTR_PRI(bgep->intr_pri));
1212 	mutex_init(bgep->softintrlock, NULL, MUTEX_DRIVER,
1213 		DDI_INTR_PRI(bgep->intr_pri));
1214 	rw_init(bgep->errlock, NULL, RW_DRIVER,
1215 	    DDI_INTR_PRI(bgep->intr_pri));
1216 
1217 	/*
1218 	 * Perform one-off initialisation of each ring ...
1219 	 */
1220 	for (ring = 0; ring < BGE_SEND_RINGS_MAX; ++ring)
1221 		bge_init_send_ring(bgep, ring);
1222 	for (ring = 0; ring < BGE_RECV_RINGS_MAX; ++ring)
1223 		bge_init_recv_ring(bgep, ring);
1224 	for (ring = 0; ring < BGE_BUFF_RINGS_MAX; ++ring)
1225 		bge_init_buff_ring(bgep, ring);
1226 }
1227 
1228 /*
1229  * Undo the work of bge_init_rings() above before the memory is freed
1230  */
1231 static void
1232 bge_fini_rings(bge_t *bgep)
1233 {
1234 	uint64_t ring;
1235 
1236 	BGE_TRACE(("bge_fini_rings($%p)", (void *)bgep));
1237 
1238 	for (ring = 0; ring < BGE_BUFF_RINGS_MAX; ++ring)
1239 		bge_fini_buff_ring(bgep, ring);
1240 	for (ring = 0; ring < BGE_RECV_RINGS_MAX; ++ring)
1241 		bge_fini_recv_ring(bgep, ring);
1242 	for (ring = 0; ring < BGE_SEND_RINGS_MAX; ++ring)
1243 		bge_fini_send_ring(bgep, ring);
1244 
1245 	rw_destroy(bgep->errlock);
1246 	mutex_destroy(bgep->softintrlock);
1247 	mutex_destroy(bgep->genlock);
1248 }
1249 
1250 /*
1251  * Allocate an area of memory and a DMA handle for accessing it
1252  */
1253 static int
1254 bge_alloc_dma_mem(bge_t *bgep, size_t memsize, ddi_device_acc_attr_t *attr_p,
1255 	uint_t dma_flags, dma_area_t *dma_p)
1256 {
1257 	caddr_t va;
1258 	int err;
1259 
1260 	BGE_TRACE(("bge_alloc_dma_mem($%p, %ld, $%p, 0x%x, $%p)",
1261 		(void *)bgep, memsize, attr_p, dma_flags, dma_p));
1262 
1263 	/*
1264 	 * Allocate handle
1265 	 */
1266 	err = ddi_dma_alloc_handle(bgep->devinfo, &dma_attr,
1267 		DDI_DMA_SLEEP, NULL, &dma_p->dma_hdl);
1268 	if (err != DDI_SUCCESS)
1269 		return (DDI_FAILURE);
1270 
1271 	/*
1272 	 * Allocate memory
1273 	 */
1274 	err = ddi_dma_mem_alloc(dma_p->dma_hdl, memsize, attr_p,
1275 		dma_flags & (DDI_DMA_CONSISTENT | DDI_DMA_STREAMING),
1276 		DDI_DMA_SLEEP, NULL, &va, &dma_p->alength, &dma_p->acc_hdl);
1277 	if (err != DDI_SUCCESS)
1278 		return (DDI_FAILURE);
1279 
1280 	/*
1281 	 * Bind the two together
1282 	 */
1283 	dma_p->mem_va = va;
1284 	err = ddi_dma_addr_bind_handle(dma_p->dma_hdl, NULL,
1285 		va, dma_p->alength, dma_flags, DDI_DMA_SLEEP, NULL,
1286 		&dma_p->cookie, &dma_p->ncookies);
1287 
1288 	BGE_DEBUG(("bge_alloc_dma_mem(): bind %d bytes; err %d, %d cookies",
1289 		dma_p->alength, err, dma_p->ncookies));
1290 
1291 	if (err != DDI_DMA_MAPPED || dma_p->ncookies != 1)
1292 		return (DDI_FAILURE);
1293 
1294 	dma_p->nslots = ~0U;
1295 	dma_p->size = ~0U;
1296 	dma_p->token = ~0U;
1297 	dma_p->offset = 0;
1298 	return (DDI_SUCCESS);
1299 }
1300 
1301 /*
1302  * Free one allocated area of DMAable memory
1303  */
1304 static void
1305 bge_free_dma_mem(dma_area_t *dma_p)
1306 {
1307 	if (dma_p->dma_hdl != NULL) {
1308 		if (dma_p->ncookies) {
1309 			(void) ddi_dma_unbind_handle(dma_p->dma_hdl);
1310 			dma_p->ncookies = 0;
1311 		}
1312 		ddi_dma_free_handle(&dma_p->dma_hdl);
1313 		dma_p->dma_hdl = NULL;
1314 	}
1315 
1316 	if (dma_p->acc_hdl != NULL) {
1317 		ddi_dma_mem_free(&dma_p->acc_hdl);
1318 		dma_p->acc_hdl = NULL;
1319 	}
1320 }
1321 
1322 /*
1323  * This function allocates all the transmit and receive buffers
1324  * and descriptors, in four chunks (or one, if MONOLITHIC).
1325  */
1326 static int
1327 bge_alloc_bufs(bge_t *bgep)
1328 {
1329 	dma_area_t area;
1330 	size_t rxbuffsize;
1331 	size_t txbuffsize;
1332 	size_t rxbuffdescsize;
1333 	size_t rxdescsize;
1334 	size_t txdescsize;
1335 	uint64_t ring;
1336 	uint64_t rx_rings = bgep->chipid.rx_rings;
1337 	uint64_t tx_rings = bgep->chipid.tx_rings;
1338 	int split;
1339 	int err;
1340 
1341 	BGE_TRACE(("bge_alloc_bufs($%p)",
1342 		(void *)bgep));
1343 
1344 	rxbuffsize = BGE_STD_SLOTS_USED*BGE_STD_BUFF_SIZE;
1345 	rxbuffsize += bgep->chipid.jumbo_slots*bgep->chipid.recv_jumbo_size;
1346 	rxbuffsize += BGE_MINI_SLOTS_USED*BGE_MINI_BUFF_SIZE;
1347 
1348 	txbuffsize = BGE_SEND_SLOTS_USED*bgep->chipid.snd_buff_size;
1349 	txbuffsize *= tx_rings;
1350 
1351 	rxdescsize = rx_rings*bgep->chipid.recv_slots;
1352 	rxdescsize *= sizeof (bge_rbd_t);
1353 
1354 	rxbuffdescsize = BGE_STD_SLOTS_USED;
1355 	rxbuffdescsize += bgep->chipid.jumbo_slots;
1356 	rxbuffdescsize += BGE_MINI_SLOTS_USED;
1357 	rxbuffdescsize *= sizeof (bge_rbd_t);
1358 
1359 	txdescsize = tx_rings*BGE_SEND_SLOTS_USED;
1360 	txdescsize *= sizeof (bge_sbd_t);
1361 	txdescsize += sizeof (bge_statistics_t);
1362 	txdescsize += sizeof (bge_status_t);
1363 	txdescsize += BGE_STATUS_PADDING;
1364 
1365 #if	BGE_MONOLITHIC
1366 
1367 	err = bge_alloc_dma_mem(bgep,
1368 		rxbuffsize+txbuffsize+rxbuffdescsize+rxdescsize+txdescsize,
1369 		&bge_data_accattr, DDI_DMA_RDWR | DDI_DMA_CONSISTENT, &area);
1370 	if (err != DDI_SUCCESS)
1371 		return (DDI_FAILURE);
1372 
1373 	BGE_DEBUG(("allocated range $%p-$%p (0x%lx-0x%lx)",
1374 		DMA_VPTR(area),
1375 		(caddr_t)DMA_VPTR(area)+area.alength,
1376 		area.cookie.dmac_laddress,
1377 		area.cookie.dmac_laddress+area.alength));
1378 
1379 	bge_slice_chunk(&bgep->rx_buff[0], &area, 1, rxbuffsize);
1380 	bge_slice_chunk(&bgep->tx_buff[0], &area, 1, txbuffsize);
1381 	bge_slice_chunk(&bgep->rx_desc[0], &area, 1, rxdescsize);
1382 	bge_slice_chunk(&bgep->tx_desc, &area, 1, txdescsize);
1383 
1384 #else
1385 	/*
1386 	 * Allocate memory & handles for RX buffers
1387 	 */
1388 	ASSERT((rxbuffsize % BGE_SPLIT) == 0);
1389 	for (split = 0; split < BGE_SPLIT; ++split) {
1390 		err = bge_alloc_dma_mem(bgep, rxbuffsize/BGE_SPLIT,
1391 			&bge_data_accattr, DDI_DMA_READ | BGE_DMA_MODE,
1392 			&bgep->rx_buff[split]);
1393 		if (err != DDI_SUCCESS)
1394 			return (DDI_FAILURE);
1395 	}
1396 
1397 	/*
1398 	 * Allocate memory & handles for TX buffers
1399 	 */
1400 	ASSERT((txbuffsize % BGE_SPLIT) == 0);
1401 	for (split = 0; split < BGE_SPLIT; ++split) {
1402 		err = bge_alloc_dma_mem(bgep, txbuffsize/BGE_SPLIT,
1403 			&bge_data_accattr, DDI_DMA_WRITE | BGE_DMA_MODE,
1404 			&bgep->tx_buff[split]);
1405 		if (err != DDI_SUCCESS)
1406 			return (DDI_FAILURE);
1407 	}
1408 
1409 	/*
1410 	 * Allocate memory & handles for receive return rings
1411 	 */
1412 	ASSERT((rxdescsize % rx_rings) == 0);
1413 	for (split = 0; split < rx_rings; ++split) {
1414 		err = bge_alloc_dma_mem(bgep, rxdescsize/rx_rings,
1415 			&bge_desc_accattr, DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
1416 			&bgep->rx_desc[split]);
1417 		if (err != DDI_SUCCESS)
1418 			return (DDI_FAILURE);
1419 	}
1420 
1421 	/*
1422 	 * Allocate memory & handles for buffer (producer) descriptor rings
1423 	 */
1424 	err = bge_alloc_dma_mem(bgep, rxbuffdescsize, &bge_desc_accattr,
1425 		DDI_DMA_RDWR | DDI_DMA_CONSISTENT, &bgep->rx_desc[split]);
1426 	if (err != DDI_SUCCESS)
1427 		return (DDI_FAILURE);
1428 
1429 	/*
1430 	 * Allocate memory & handles for TX descriptor rings,
1431 	 * status block, and statistics area
1432 	 */
1433 	err = bge_alloc_dma_mem(bgep, txdescsize, &bge_desc_accattr,
1434 		DDI_DMA_RDWR | DDI_DMA_CONSISTENT, &bgep->tx_desc);
1435 	if (err != DDI_SUCCESS)
1436 		return (DDI_FAILURE);
1437 
1438 #endif	/* BGE_MONOLITHIC */
1439 
1440 	/*
1441 	 * Now carve up each of the allocated areas ...
1442 	 */
1443 	for (split = 0; split < BGE_SPLIT; ++split) {
1444 		area = bgep->rx_buff[split];
1445 		bge_slice_chunk(&bgep->buff[BGE_STD_BUFF_RING].buf[split],
1446 			&area, BGE_STD_SLOTS_USED/BGE_SPLIT,
1447 			BGE_STD_BUFF_SIZE);
1448 		bge_slice_chunk(&bgep->buff[BGE_JUMBO_BUFF_RING].buf[split],
1449 			&area, bgep->chipid.jumbo_slots/BGE_SPLIT,
1450 			bgep->chipid.recv_jumbo_size);
1451 		bge_slice_chunk(&bgep->buff[BGE_MINI_BUFF_RING].buf[split],
1452 			&area, BGE_MINI_SLOTS_USED/BGE_SPLIT,
1453 			BGE_MINI_BUFF_SIZE);
1454 		ASSERT(area.alength >= 0);
1455 	}
1456 
1457 	for (split = 0; split < BGE_SPLIT; ++split) {
1458 		area = bgep->tx_buff[split];
1459 		for (ring = 0; ring < tx_rings; ++ring)
1460 			bge_slice_chunk(&bgep->send[ring].buf[split],
1461 				&area, BGE_SEND_SLOTS_USED/BGE_SPLIT,
1462 				bgep->chipid.snd_buff_size);
1463 		for (; ring < BGE_SEND_RINGS_MAX; ++ring)
1464 			bge_slice_chunk(&bgep->send[ring].buf[split],
1465 				&area, 0/BGE_SPLIT,
1466 				bgep->chipid.snd_buff_size);
1467 		ASSERT(area.alength >= 0);
1468 	}
1469 
1470 	for (ring = 0; ring < rx_rings; ++ring)
1471 		bge_slice_chunk(&bgep->recv[ring].desc, &bgep->rx_desc[ring],
1472 			bgep->chipid.recv_slots, sizeof (bge_rbd_t));
1473 
1474 	area = bgep->rx_desc[rx_rings];
1475 	for (; ring < BGE_RECV_RINGS_MAX; ++ring)
1476 		bge_slice_chunk(&bgep->recv[ring].desc, &area,
1477 			0, sizeof (bge_rbd_t));
1478 	bge_slice_chunk(&bgep->buff[BGE_STD_BUFF_RING].desc, &area,
1479 		BGE_STD_SLOTS_USED, sizeof (bge_rbd_t));
1480 	bge_slice_chunk(&bgep->buff[BGE_JUMBO_BUFF_RING].desc, &area,
1481 		bgep->chipid.jumbo_slots, sizeof (bge_rbd_t));
1482 	bge_slice_chunk(&bgep->buff[BGE_MINI_BUFF_RING].desc, &area,
1483 		BGE_MINI_SLOTS_USED, sizeof (bge_rbd_t));
1484 	ASSERT(area.alength == 0);
1485 
1486 	area = bgep->tx_desc;
1487 	for (ring = 0; ring < tx_rings; ++ring)
1488 		bge_slice_chunk(&bgep->send[ring].desc, &area,
1489 			BGE_SEND_SLOTS_USED, sizeof (bge_sbd_t));
1490 	for (; ring < BGE_SEND_RINGS_MAX; ++ring)
1491 		bge_slice_chunk(&bgep->send[ring].desc, &area,
1492 			0, sizeof (bge_sbd_t));
1493 	bge_slice_chunk(&bgep->statistics, &area, 1, sizeof (bge_statistics_t));
1494 	bge_slice_chunk(&bgep->status_block, &area, 1, sizeof (bge_status_t));
1495 	ASSERT(area.alength == BGE_STATUS_PADDING);
1496 	DMA_ZERO(bgep->status_block);
1497 
1498 	return (DDI_SUCCESS);
1499 }
1500 
1501 /*
1502  * This routine frees the transmit and receive buffers and descriptors.
1503  * Make sure the chip is stopped before calling it!
1504  */
1505 static void
1506 bge_free_bufs(bge_t *bgep)
1507 {
1508 	int split;
1509 
1510 	BGE_TRACE(("bge_free_bufs($%p)",
1511 		(void *)bgep));
1512 
1513 #if	BGE_MONOLITHIC
1514 	bge_free_dma_mem(&bgep->rx_buff[0]);
1515 #else
1516 	bge_free_dma_mem(&bgep->tx_desc);
1517 	for (split = 0; split < BGE_RECV_RINGS_SPLIT; ++split)
1518 		bge_free_dma_mem(&bgep->rx_desc[split]);
1519 	for (split = 0; split < BGE_SPLIT; ++split)
1520 		bge_free_dma_mem(&bgep->tx_buff[split]);
1521 	for (split = 0; split < BGE_SPLIT; ++split)
1522 		bge_free_dma_mem(&bgep->rx_buff[split]);
1523 #endif	/* BGE_MONOLITHIC */
1524 }
1525 
1526 /*
1527  * Determine (initial) MAC address ("BIA") to use for this interface
1528  */
1529 
1530 static void
1531 bge_find_mac_address(bge_t *bgep, chip_id_t *cidp)
1532 {
1533 	struct ether_addr sysaddr;
1534 	char propbuf[8];		/* "true" or "false", plus NUL	*/
1535 	uchar_t *bytes;
1536 	int *ints;
1537 	uint_t nelts;
1538 	int err;
1539 
1540 	BGE_TRACE(("bge_find_mac_address($%p)",
1541 		(void *)bgep));
1542 
1543 	BGE_DEBUG(("bge_find_mac_address: hw_mac_addr %012llx, => %s (%sset)",
1544 		cidp->hw_mac_addr,
1545 		ether_sprintf((void *)cidp->vendor_addr.addr),
1546 		cidp->vendor_addr.set ? "" : "not "));
1547 
1548 	/*
1549 	 * The "vendor's factory-set address" may already have
1550 	 * been extracted from the chip, but if the property
1551 	 * "local-mac-address" is set we use that instead.  It
1552 	 * will normally be set by OBP, but it could also be
1553 	 * specified in a .conf file(!)
1554 	 *
1555 	 * There doesn't seem to be a way to define byte-array
1556 	 * properties in a .conf, so we check whether it looks
1557 	 * like an array of 6 ints instead.
1558 	 *
1559 	 * Then, we check whether it looks like an array of 6
1560 	 * bytes (which it should, if OBP set it).  If we can't
1561 	 * make sense of it either way, we'll ignore it.
1562 	 */
1563 	err = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, bgep->devinfo,
1564 		DDI_PROP_DONTPASS, localmac_propname, &ints, &nelts);
1565 	if (err == DDI_PROP_SUCCESS) {
1566 		if (nelts == ETHERADDRL) {
1567 			while (nelts--)
1568 				cidp->vendor_addr.addr[nelts] = ints[nelts];
1569 			cidp->vendor_addr.set = 1;
1570 		}
1571 		ddi_prop_free(ints);
1572 	}
1573 
1574 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, bgep->devinfo,
1575 		DDI_PROP_DONTPASS, localmac_propname, &bytes, &nelts);
1576 	if (err == DDI_PROP_SUCCESS) {
1577 		if (nelts == ETHERADDRL) {
1578 			while (nelts--)
1579 				cidp->vendor_addr.addr[nelts] = bytes[nelts];
1580 			cidp->vendor_addr.set = 1;
1581 		}
1582 		ddi_prop_free(bytes);
1583 	}
1584 
1585 	BGE_DEBUG(("bge_find_mac_address: +local %s (%sset)",
1586 		ether_sprintf((void *)cidp->vendor_addr.addr),
1587 		cidp->vendor_addr.set ? "" : "not "));
1588 
1589 	/*
1590 	 * Look up the OBP property "local-mac-address?".  Note that even
1591 	 * though its value is a string (which should be "true" or "false"),
1592 	 * it can't be decoded by ddi_prop_lookup_string(9F).  So, we zero
1593 	 * the buffer first and then fetch the property as an untyped array;
1594 	 * this may or may not include a final NUL, but since there will
1595 	 * always be one left at the end of the buffer we can now treat it
1596 	 * as a string anyway.
1597 	 */
1598 	nelts = sizeof (propbuf);
1599 	bzero(propbuf, nelts--);
1600 	err = ddi_getlongprop_buf(DDI_DEV_T_ANY, bgep->devinfo,
1601 		DDI_PROP_CANSLEEP, localmac_boolname, propbuf, (int *)&nelts);
1602 
1603 	/*
1604 	 * Now, if the address still isn't set from the hardware (SEEPROM)
1605 	 * or the OBP or .conf property, OR if the user has foolishly set
1606 	 * 'local-mac-address? = false', use "the system address" instead
1607 	 * (but only if it's non-null i.e. has been set from the IDPROM).
1608 	 */
1609 	if (cidp->vendor_addr.set == 0 || strcmp(propbuf, "false") == 0)
1610 		if (localetheraddr(NULL, &sysaddr) != 0) {
1611 			ethaddr_copy(&sysaddr, cidp->vendor_addr.addr);
1612 			cidp->vendor_addr.set = 1;
1613 		}
1614 
1615 	BGE_DEBUG(("bge_find_mac_address: +system %s (%sset)",
1616 		ether_sprintf((void *)cidp->vendor_addr.addr),
1617 		cidp->vendor_addr.set ? "" : "not "));
1618 
1619 	/*
1620 	 * Finally(!), if there's a valid "mac-address" property (created
1621 	 * if we netbooted from this interface), we must use this instead
1622 	 * of any of the above to ensure that the NFS/install server doesn't
1623 	 * get confused by the address changing as Solaris takes over!
1624 	 */
1625 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, bgep->devinfo,
1626 		DDI_PROP_DONTPASS, macaddr_propname, &bytes, &nelts);
1627 	if (err == DDI_PROP_SUCCESS) {
1628 		if (nelts == ETHERADDRL) {
1629 			while (nelts--)
1630 				cidp->vendor_addr.addr[nelts] = bytes[nelts];
1631 			cidp->vendor_addr.set = 1;
1632 		}
1633 		ddi_prop_free(bytes);
1634 	}
1635 
1636 	BGE_DEBUG(("bge_find_mac_address: =final %s (%sset)",
1637 		ether_sprintf((void *)cidp->vendor_addr.addr),
1638 		cidp->vendor_addr.set ? "" : "not "));
1639 }
1640 
1641 static void
1642 #ifdef BGE_IPMI_ASF
1643 bge_unattach(bge_t *bgep, uint_t asf_mode)
1644 #else
1645 bge_unattach(bge_t *bgep)
1646 #endif
1647 {
1648 	mac_t	*macp;
1649 
1650 	BGE_TRACE(("bge_unattach($%p)",
1651 		(void *)bgep));
1652 
1653 	/*
1654 	 * Flag that no more activity may be initiated
1655 	 */
1656 	bgep->progress &= ~PROGRESS_READY;
1657 
1658 	/*
1659 	 * Quiesce the PHY and MAC (leave it reset but still powered).
1660 	 * Clean up and free all BGE data structures
1661 	 */
1662 	if (bgep->cyclic_id) {
1663 		mutex_enter(&cpu_lock);
1664 		cyclic_remove(bgep->cyclic_id);
1665 		mutex_exit(&cpu_lock);
1666 	}
1667 	if (bgep->progress & PROGRESS_KSTATS)
1668 		bge_fini_kstats(bgep);
1669 	if (bgep->progress & PROGRESS_NDD)
1670 		bge_nd_cleanup(bgep);
1671 	if (bgep->progress & PROGRESS_PHY)
1672 		bge_phys_reset(bgep);
1673 	if (bgep->progress & PROGRESS_HWINT) {
1674 		mutex_enter(bgep->genlock);
1675 #ifdef BGE_IPMI_ASF
1676 		bge_chip_reset(bgep, B_FALSE, asf_mode);
1677 		if (bgep->asf_enabled) {
1678 			/*
1679 			 * This register has been overlaid. We restore its
1680 			 * initial value here.
1681 			 */
1682 			bge_nic_put32(bgep, BGE_NIC_DATA_SIG_ADDR,
1683 			    BGE_NIC_DATA_SIG);
1684 		}
1685 #else
1686 		bge_chip_reset(bgep, B_FALSE);
1687 #endif
1688 		mutex_exit(bgep->genlock);
1689 	}
1690 
1691 	if (bgep->progress & PROGRESS_INTR) {
1692 		bge_rem_intrs(bgep);
1693 		bge_fini_rings(bgep);
1694 	}
1695 
1696 	if (bgep->progress & PROGRESS_FACTOTUM)
1697 		ddi_remove_softintr(bgep->factotum_id);
1698 	if (bgep->progress & PROGRESS_RESCHED)
1699 		ddi_remove_softintr(bgep->resched_id);
1700 	bge_free_bufs(bgep);
1701 	if (bgep->progress & PROGRESS_REGS)
1702 		ddi_regs_map_free(&bgep->io_handle);
1703 	if (bgep->progress & PROGRESS_CFG)
1704 		pci_config_teardown(&bgep->cfg_handle);
1705 
1706 	ddi_remove_minor_node(bgep->devinfo, NULL);
1707 	macp = bgep->macp;
1708 	kmem_free(macp, sizeof (*macp));
1709 	kmem_free(bgep, sizeof (*bgep));
1710 }
1711 
1712 static int
1713 bge_resume(dev_info_t *devinfo)
1714 {
1715 	bge_t *bgep;				/* Our private data	*/
1716 	chip_id_t *cidp;
1717 	chip_id_t chipid;
1718 
1719 	bgep = ddi_get_driver_private(devinfo);
1720 	if (bgep == NULL)
1721 		return (DDI_FAILURE);
1722 
1723 	/*
1724 	 * Refuse to resume if the data structures aren't consistent
1725 	 */
1726 	if (bgep->devinfo != devinfo)
1727 		return (DDI_FAILURE);
1728 
1729 #ifdef BGE_IPMI_ASF
1730 	/*
1731 	 * Power management hasn't been supported in BGE now. If you
1732 	 * want to implement it, please add the ASF/IPMI related
1733 	 * code here.
1734 	 */
1735 
1736 #endif
1737 
1738 	/*
1739 	 * Read chip ID & set up config space command register(s)
1740 	 * Refuse to resume if the chip has changed its identity!
1741 	 */
1742 	cidp = &bgep->chipid;
1743 	bge_chip_cfg_init(bgep, &chipid, B_FALSE);
1744 	if (chipid.vendor != cidp->vendor)
1745 		return (DDI_FAILURE);
1746 	if (chipid.device != cidp->device)
1747 		return (DDI_FAILURE);
1748 	if (chipid.revision != cidp->revision)
1749 		return (DDI_FAILURE);
1750 	if (chipid.asic_rev != cidp->asic_rev)
1751 		return (DDI_FAILURE);
1752 
1753 	/*
1754 	 * All OK, reinitialise h/w & kick off GLD scheduling
1755 	 */
1756 	mutex_enter(bgep->genlock);
1757 	bge_restart(bgep, B_TRUE);
1758 	mutex_exit(bgep->genlock);
1759 	return (DDI_SUCCESS);
1760 }
1761 
1762 static uint8_t ether_brdcst[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1763 
1764 /*
1765  * attach(9E) -- Attach a device to the system
1766  *
1767  * Called once for each board successfully probed.
1768  */
1769 static int
1770 bge_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
1771 {
1772 	bge_t *bgep;				/* Our private data	*/
1773 	mac_t *macp;
1774 	chip_id_t *cidp;
1775 	cyc_handler_t cychand;
1776 	cyc_time_t cyctime;
1777 	caddr_t regs;
1778 	int instance;
1779 	int err;
1780 	mac_info_t *mip;
1781 	int intr_types;
1782 	int i;
1783 #ifdef BGE_IPMI_ASF
1784 	uint32_t mhcrValue;
1785 #endif
1786 
1787 	instance = ddi_get_instance(devinfo);
1788 
1789 	BGE_GTRACE(("bge_attach($%p, %d) instance %d",
1790 		(void *)devinfo, cmd, instance));
1791 	BGE_BRKPT(NULL, "bge_attach");
1792 
1793 	switch (cmd) {
1794 	default:
1795 		return (DDI_FAILURE);
1796 
1797 	case DDI_RESUME:
1798 		return (bge_resume(devinfo));
1799 
1800 	case DDI_ATTACH:
1801 		break;
1802 	}
1803 
1804 	/*
1805 	 * Allocate mac_t and BGE private structures, and
1806 	 * cross-link them so that given either one of these or
1807 	 * the devinfo the others can be derived.
1808 	 */
1809 	macp = kmem_zalloc(sizeof (*macp), KM_SLEEP);
1810 	bgep = kmem_zalloc(sizeof (*bgep), KM_SLEEP);
1811 	ddi_set_driver_private(devinfo, bgep);
1812 	bgep->bge_guard = BGE_GUARD;
1813 	bgep->devinfo = devinfo;
1814 	bgep->macp = macp;
1815 	macp->m_driver = bgep;
1816 
1817 	/*
1818 	 * Initialize more fields in BGE private data
1819 	 */
1820 	bgep->debug = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
1821 		DDI_PROP_DONTPASS, debug_propname, bge_debug);
1822 	(void) snprintf(bgep->ifname, sizeof (bgep->ifname), "%s%d",
1823 		BGE_DRIVER_NAME, instance);
1824 
1825 	/*
1826 	 * Look up the IOMMU's page size for DVMA mappings (must be
1827 	 * a power of 2) and convert to a mask.  This can be used to
1828 	 * determine whether a message buffer crosses a page boundary.
1829 	 * Note: in 2s complement binary notation, if X is a power of
1830 	 * 2, then -X has the representation "11...1100...00".
1831 	 */
1832 	bgep->pagemask = dvma_pagesize(devinfo);
1833 	ASSERT(ddi_ffs(bgep->pagemask) == ddi_fls(bgep->pagemask));
1834 	bgep->pagemask = -bgep->pagemask;
1835 
1836 	/*
1837 	 * Map config space registers
1838 	 * Read chip ID & set up config space command register(s)
1839 	 *
1840 	 * Note: this leaves the chip accessible by Memory Space
1841 	 * accesses, but with interrupts and Bus Mastering off.
1842 	 * This should ensure that nothing untoward will happen
1843 	 * if it has been left active by the (net-)bootloader.
1844 	 * We'll re-enable Bus Mastering once we've reset the chip,
1845 	 * and allow interrupts only when everything else is set up.
1846 	 */
1847 	err = pci_config_setup(devinfo, &bgep->cfg_handle);
1848 #ifdef BGE_IPMI_ASF
1849 	mhcrValue = pci_config_get32(bgep->cfg_handle, PCI_CONF_BGE_MHCR);
1850 	if (mhcrValue & MHCR_ENABLE_ENDIAN_WORD_SWAP) {
1851 		bgep->asf_wordswapped = B_TRUE;
1852 	} else {
1853 		bgep->asf_wordswapped = B_FALSE;
1854 	}
1855 	bge_asf_get_config(bgep);
1856 #endif
1857 	if (err != DDI_SUCCESS) {
1858 		bge_problem(bgep, "pci_config_setup() failed");
1859 		goto attach_fail;
1860 	}
1861 	bgep->progress |= PROGRESS_CFG;
1862 	cidp = &bgep->chipid;
1863 	bzero(cidp, sizeof (*cidp));
1864 	bge_chip_cfg_init(bgep, cidp, B_FALSE);
1865 
1866 #ifdef BGE_IPMI_ASF
1867 	if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
1868 	    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
1869 		bgep->asf_newhandshake = B_TRUE;
1870 	} else {
1871 		bgep->asf_newhandshake = B_FALSE;
1872 	}
1873 #endif
1874 
1875 	/*
1876 	 * Update those parts of the chip ID derived from volatile
1877 	 * registers with the values seen by OBP (in case the chip
1878 	 * has been reset externally and therefore lost them).
1879 	 */
1880 	cidp->subven = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
1881 		DDI_PROP_DONTPASS, subven_propname, cidp->subven);
1882 	cidp->subdev = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
1883 		DDI_PROP_DONTPASS, subdev_propname, cidp->subdev);
1884 	cidp->clsize = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
1885 		DDI_PROP_DONTPASS, clsize_propname, cidp->clsize);
1886 	cidp->latency = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
1887 		DDI_PROP_DONTPASS, latency_propname, cidp->latency);
1888 	cidp->rx_rings = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
1889 		DDI_PROP_DONTPASS, rxrings_propname, cidp->rx_rings);
1890 	cidp->tx_rings = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
1891 		DDI_PROP_DONTPASS, txrings_propname, cidp->tx_rings);
1892 
1893 	if (bge_jumbo_enable == B_TRUE) {
1894 		cidp->default_mtu = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
1895 			DDI_PROP_DONTPASS, default_mtu, BGE_DEFAULT_MTU);
1896 		if ((cidp->default_mtu < BGE_DEFAULT_MTU)||
1897 			(cidp->default_mtu > BGE_MAXIMUM_MTU)) {
1898 			cidp->default_mtu = BGE_DEFAULT_MTU;
1899 		}
1900 	}
1901 	/*
1902 	 * Map operating registers
1903 	 */
1904 	err = ddi_regs_map_setup(devinfo, BGE_PCI_OPREGS_RNUMBER,
1905 		&regs, 0, 0, &bge_reg_accattr, &bgep->io_handle);
1906 	if (err != DDI_SUCCESS) {
1907 		bge_problem(bgep, "ddi_regs_map_setup() failed");
1908 		goto attach_fail;
1909 	}
1910 	bgep->io_regs = regs;
1911 	bgep->progress |= PROGRESS_REGS;
1912 
1913 	/*
1914 	 * Characterise the device, so we know its requirements.
1915 	 * Then allocate the appropriate TX and RX descriptors & buffers.
1916 	 */
1917 	bge_chip_id_init(bgep);
1918 	err = bge_alloc_bufs(bgep);
1919 	if (err != DDI_SUCCESS) {
1920 		bge_problem(bgep, "DMA buffer allocation failed");
1921 		goto attach_fail;
1922 	}
1923 
1924 	/*
1925 	 * Add the softint handlers:
1926 	 *
1927 	 * Both of these handlers are used to avoid restrictions on the
1928 	 * context and/or mutexes required for some operations.  In
1929 	 * particular, the hardware interrupt handler and its subfunctions
1930 	 * can detect a number of conditions that we don't want to handle
1931 	 * in that context or with that set of mutexes held.  So, these
1932 	 * softints are triggered instead:
1933 	 *
1934 	 * the <resched> softint is triggered if if we have previously
1935 	 * had to refuse to send a packet because of resource shortage
1936 	 * (we've run out of transmit buffers), but the send completion
1937 	 * interrupt handler has now detected that more buffers have
1938 	 * become available.
1939 	 *
1940 	 * the <factotum> is triggered if the h/w interrupt handler
1941 	 * sees the <link state changed> or <error> bits in the status
1942 	 * block.  It's also triggered periodically to poll the link
1943 	 * state, just in case we aren't getting link status change
1944 	 * interrupts ...
1945 	 */
1946 	err = ddi_add_softintr(devinfo, DDI_SOFTINT_LOW, &bgep->resched_id,
1947 		NULL, NULL, bge_reschedule, (caddr_t)bgep);
1948 	if (err != DDI_SUCCESS) {
1949 		bge_problem(bgep, "ddi_add_softintr() failed");
1950 		goto attach_fail;
1951 	}
1952 	bgep->progress |= PROGRESS_RESCHED;
1953 	err = ddi_add_softintr(devinfo, DDI_SOFTINT_LOW, &bgep->factotum_id,
1954 		NULL, NULL, bge_chip_factotum, (caddr_t)bgep);
1955 	if (err != DDI_SUCCESS) {
1956 		bge_problem(bgep, "ddi_add_softintr() failed");
1957 		goto attach_fail;
1958 	}
1959 	bgep->progress |= PROGRESS_FACTOTUM;
1960 
1961 	/* Get supported interrupt types */
1962 	if (ddi_intr_get_supported_types(devinfo, &intr_types) != DDI_SUCCESS) {
1963 		bge_error(bgep, "ddi_intr_get_supported_types failed\n");
1964 
1965 		goto attach_fail;
1966 	}
1967 
1968 	bge_log(bgep, "ddi_intr_get_supported_types() returned: %x",
1969 	    intr_types);
1970 
1971 	if ((intr_types & DDI_INTR_TYPE_MSI) && bgep->chipid.msi_enabled) {
1972 		if (bge_add_intrs(bgep, DDI_INTR_TYPE_MSI) != DDI_SUCCESS) {
1973 			bge_error(bgep, "MSI registration failed, "
1974 			    "trying FIXED interrupt type\n");
1975 		} else {
1976 			bge_log(bgep, "Using MSI interrupt type\n");
1977 
1978 			bgep->intr_type = DDI_INTR_TYPE_MSI;
1979 			bgep->progress |= PROGRESS_INTR;
1980 		}
1981 	}
1982 
1983 	if (!(bgep->progress & PROGRESS_INTR) &&
1984 	    (intr_types & DDI_INTR_TYPE_FIXED)) {
1985 		if (bge_add_intrs(bgep, DDI_INTR_TYPE_FIXED) != DDI_SUCCESS) {
1986 			bge_error(bgep, "FIXED interrupt "
1987 			    "registration failed\n");
1988 			goto attach_fail;
1989 		}
1990 
1991 		bge_log(bgep, "Using FIXED interrupt type\n");
1992 
1993 		bgep->intr_type = DDI_INTR_TYPE_FIXED;
1994 		bgep->progress |= PROGRESS_INTR;
1995 	}
1996 
1997 	if (!(bgep->progress & PROGRESS_INTR)) {
1998 		bge_error(bgep, "No interrupts registered\n");
1999 		goto attach_fail;
2000 	}
2001 
2002 	/*
2003 	 * Note that interrupts are not enabled yet as
2004 	 * mutex locks are not initialized.
2005 	 * Initialize rings and mutex locks.
2006 	 */
2007 	bge_init_rings(bgep);
2008 	bgep->progress |= PROGRESS_HWINT;
2009 
2010 	/*
2011 	 * Now that mutex locks are initialized, enable interrupts.
2012 	 */
2013 	if (bgep->intr_cap & DDI_INTR_FLAG_BLOCK) {
2014 		/* Call ddi_intr_block_enable() for MSI interrupts */
2015 		(void) ddi_intr_block_enable(bgep->htable, bgep->intr_cnt);
2016 	} else {
2017 		/* Call ddi_intr_enable for MSI or FIXED interrupts */
2018 		for (i = 0; i < bgep->intr_cnt; i++) {
2019 			(void) ddi_intr_enable(bgep->htable[i]);
2020 		}
2021 	}
2022 
2023 	/*
2024 	 * Initialise link state variables
2025 	 * Stop, reset & reinitialise the chip.
2026 	 * Initialise the (internal) PHY.
2027 	 */
2028 	bgep->link_state = LINK_STATE_UNKNOWN;
2029 	bgep->link_up_msg = bgep->link_down_msg = " (initialized)";
2030 
2031 	mutex_enter(bgep->genlock);
2032 
2033 	/*
2034 	 * Reset chip & rings to initial state; also reset address
2035 	 * filtering, promiscuity, loopback mode.
2036 	 */
2037 #ifdef BGE_IPMI_ASF
2038 	bge_reset(bgep, ASF_MODE_SHUTDOWN);
2039 #else
2040 	bge_reset(bgep);
2041 #endif
2042 
2043 	bzero(bgep->mcast_hash, sizeof (bgep->mcast_hash));
2044 	bzero(bgep->mcast_refs, sizeof (bgep->mcast_refs));
2045 	bgep->promisc = B_FALSE;
2046 	bgep->param_loop_mode = BGE_LOOP_NONE;
2047 
2048 	mutex_exit(bgep->genlock);
2049 
2050 	bge_phys_init(bgep);
2051 	bgep->progress |= PROGRESS_PHY;
2052 
2053 	/*
2054 	 * Register NDD-tweakable parameters
2055 	 */
2056 	if (bge_nd_init(bgep)) {
2057 		bge_problem(bgep, "bge_nd_init() failed");
2058 		goto attach_fail;
2059 	}
2060 	bgep->progress |= PROGRESS_NDD;
2061 
2062 	/*
2063 	 * Create & initialise named kstats
2064 	 */
2065 	bge_init_kstats(bgep, instance);
2066 	bgep->progress |= PROGRESS_KSTATS;
2067 
2068 	/*
2069 	 * Determine whether to override the chip's own MAC address
2070 	 */
2071 	bge_find_mac_address(bgep, cidp);
2072 	ethaddr_copy(cidp->vendor_addr.addr, bgep->curr_addr.addr);
2073 	bgep->curr_addr.set = 1;
2074 
2075 	/*
2076 	 * Initialize pointers to device specific functions which
2077 	 * will be used by the generic layer.
2078 	 */
2079 	mip = &(macp->m_info);
2080 	mip->mi_media = DL_ETHER;
2081 	mip->mi_sdu_min = 0;
2082 	mip->mi_sdu_max = cidp->ethmax_size - sizeof (struct ether_header);
2083 	mip->mi_poll = DL_CAPAB_POLL;
2084 
2085 	/*
2086 	 * Workaround for Incorrect pseudo-header checksum calculation.
2087 	 * 	Use partial checksum offload for all affected chips.
2088 	 */
2089 	if (DEVICE_5704_SERIES_CHIPSETS(bgep))
2090 		mip->mi_cksum = HCKSUM_INET_PARTIAL | HCKSUM_IPHDRCKSUM;
2091 	else
2092 		mip->mi_cksum = HCKSUM_INET_FULL_V4 | HCKSUM_IPHDRCKSUM;
2093 
2094 	mip->mi_addr_length = ETHERADDRL;
2095 	bcopy(ether_brdcst, mip->mi_brdcst_addr, ETHERADDRL);
2096 	bcopy(bgep->curr_addr.addr, mip->mi_unicst_addr, ETHERADDRL);
2097 
2098 	MAC_STAT_MIB(mip->mi_stat);
2099 	mip->mi_stat[MAC_STAT_UNKNOWNS] = B_FALSE;
2100 	MAC_STAT_ETHER(mip->mi_stat);
2101 	mip->mi_stat[MAC_STAT_SQE_ERRORS] = B_FALSE;
2102 	mip->mi_stat[MAC_STAT_MACRCV_ERRORS] = B_FALSE;
2103 	if (!(bgep->chipid.flags & CHIP_FLAG_SERDES))
2104 		MAC_STAT_MII(mip->mi_stat);
2105 
2106 	macp->m_stat = bge_m_stat;
2107 	macp->m_stop = bge_m_stop;
2108 	macp->m_start = bge_m_start;
2109 	macp->m_unicst = bge_m_unicst;
2110 	macp->m_multicst = bge_m_multicst;
2111 	macp->m_promisc = bge_m_promisc;
2112 	macp->m_tx = bge_m_tx;
2113 	macp->m_resources = bge_m_resources;
2114 	macp->m_ioctl = bge_m_ioctl;
2115 
2116 	macp->m_dip = devinfo;
2117 	macp->m_ident = MAC_IDENT;
2118 
2119 	/*
2120 	 * Finally, we're ready to register ourselves with the MAC layer
2121 	 * interface; if this succeeds, we're all ready to start()
2122 	 */
2123 	if (mac_register(macp) != 0)
2124 		goto attach_fail;
2125 
2126 	cychand.cyh_func = bge_chip_cyclic;
2127 	cychand.cyh_arg = bgep;
2128 	cychand.cyh_level = CY_LOCK_LEVEL;
2129 	cyctime.cyt_when = 0;
2130 	cyctime.cyt_interval = BGE_CYCLIC_PERIOD;
2131 	mutex_enter(&cpu_lock);
2132 	bgep->cyclic_id = cyclic_add(&cychand, &cyctime);
2133 	mutex_exit(&cpu_lock);
2134 
2135 	bgep->progress |= PROGRESS_READY;
2136 	ASSERT(bgep->bge_guard == BGE_GUARD);
2137 	return (DDI_SUCCESS);
2138 
2139 attach_fail:
2140 #ifdef BGE_IPMI_ASF
2141 	bge_unattach(bgep, ASF_MODE_NONE);
2142 #else
2143 	bge_unattach(bgep);
2144 #endif
2145 	return (DDI_FAILURE);
2146 }
2147 
2148 /*
2149  *	bge_suspend() -- suspend transmit/receive for powerdown
2150  */
2151 static int
2152 bge_suspend(bge_t *bgep)
2153 {
2154 	/*
2155 	 * Stop processing and idle (powerdown) the PHY ...
2156 	 */
2157 	mutex_enter(bgep->genlock);
2158 #ifdef BGE_IPMI_ASF
2159 	/*
2160 	 * Power management hasn't been supported in BGE now. If you
2161 	 * want to implement it, please add the ASF/IPMI related
2162 	 * code here.
2163 	 */
2164 #endif
2165 	bge_stop(bgep);
2166 	bge_phys_idle(bgep);
2167 	mutex_exit(bgep->genlock);
2168 
2169 	return (DDI_SUCCESS);
2170 }
2171 
2172 /*
2173  * detach(9E) -- Detach a device from the system
2174  */
2175 static int
2176 bge_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
2177 {
2178 	bge_t *bgep;
2179 #ifdef BGE_IPMI_ASF
2180 	uint_t asf_mode;
2181 	asf_mode = ASF_MODE_NONE;
2182 #endif
2183 
2184 	BGE_GTRACE(("bge_detach($%p, %d)", (void *)devinfo, cmd));
2185 
2186 	bgep = ddi_get_driver_private(devinfo);
2187 
2188 	switch (cmd) {
2189 	default:
2190 		return (DDI_FAILURE);
2191 
2192 	case DDI_SUSPEND:
2193 		return (bge_suspend(bgep));
2194 
2195 	case DDI_DETACH:
2196 		break;
2197 	}
2198 
2199 #ifdef BGE_IPMI_ASF
2200 	mutex_enter(bgep->genlock);
2201 	if (bgep->asf_enabled && (bgep->asf_status == ASF_STAT_RUN)) {
2202 
2203 		bge_asf_update_status(bgep);
2204 		bge_asf_stop_timer(bgep);
2205 		bgep->asf_status = ASF_STAT_STOP;
2206 
2207 		bge_asf_pre_reset_operations(bgep, BGE_SHUTDOWN_RESET);
2208 
2209 		if (bgep->asf_pseudostop) {
2210 			bgep->link_up_msg = bgep->link_down_msg = " (stopped)";
2211 			bge_chip_stop(bgep, B_FALSE);
2212 			bgep->bge_mac_state = BGE_MAC_STOPPED;
2213 			bgep->asf_pseudostop = B_FALSE;
2214 		}
2215 
2216 		asf_mode = ASF_MODE_POST_SHUTDOWN;
2217 	}
2218 	mutex_exit(bgep->genlock);
2219 #endif
2220 
2221 	/*
2222 	 * Unregister from the GLD subsystem.  This can fail, in
2223 	 * particular if there are DLPI style-2 streams still open -
2224 	 * in which case we just return failure without shutting
2225 	 * down chip operations.
2226 	 */
2227 	if (mac_unregister(bgep->macp) != 0)
2228 		return (DDI_FAILURE);
2229 
2230 	/*
2231 	 * All activity stopped, so we can clean up & exit
2232 	 */
2233 #ifdef BGE_IPMI_ASF
2234 	bge_unattach(bgep, asf_mode);
2235 #else
2236 	bge_unattach(bgep);
2237 #endif
2238 	return (DDI_SUCCESS);
2239 }
2240 
2241 
2242 /*
2243  * ========== Module Loading Data & Entry Points ==========
2244  */
2245 
2246 #undef	BGE_DBG
2247 #define	BGE_DBG		BGE_DBG_INIT	/* debug flag for this code	*/
2248 
2249 DDI_DEFINE_STREAM_OPS(bge_dev_ops, nulldev, nulldev, bge_attach, bge_detach,
2250     nodev, NULL, D_MP, NULL);
2251 
2252 static struct modldrv bge_modldrv = {
2253 	&mod_driverops,		/* Type of module.  This one is a driver */
2254 	bge_ident,		/* short description */
2255 	&bge_dev_ops		/* driver specific ops */
2256 };
2257 
2258 static struct modlinkage modlinkage = {
2259 	MODREV_1, (void *)&bge_modldrv, NULL
2260 };
2261 
2262 
2263 int
2264 _info(struct modinfo *modinfop)
2265 {
2266 	return (mod_info(&modlinkage, modinfop));
2267 }
2268 
2269 int
2270 _init(void)
2271 {
2272 	int status;
2273 
2274 	mac_init_ops(&bge_dev_ops, "bge");
2275 	status = mod_install(&modlinkage);
2276 	if (status == DDI_SUCCESS)
2277 		mutex_init(bge_log_mutex, NULL, MUTEX_DRIVER, NULL);
2278 	else
2279 		mac_fini_ops(&bge_dev_ops);
2280 	return (status);
2281 }
2282 
2283 int
2284 _fini(void)
2285 {
2286 	int status;
2287 
2288 	status = mod_remove(&modlinkage);
2289 	if (status == DDI_SUCCESS) {
2290 		mac_fini_ops(&bge_dev_ops);
2291 		mutex_destroy(bge_log_mutex);
2292 	}
2293 	return (status);
2294 }
2295 
2296 
2297 /*
2298  * bge_add_intrs:
2299  *
2300  * Register FIXED or MSI interrupts.
2301  */
2302 static int
2303 bge_add_intrs(bge_t *bgep, int	intr_type)
2304 {
2305 	dev_info_t	*dip = bgep->devinfo;
2306 	int		avail, actual, intr_size, count = 0;
2307 	int		i, flag, ret;
2308 
2309 	bge_log(bgep, "bge_add_intrs: interrupt type 0x%x\n", intr_type);
2310 
2311 	/* Get number of interrupts */
2312 	ret = ddi_intr_get_nintrs(dip, intr_type, &count);
2313 	if ((ret != DDI_SUCCESS) || (count == 0)) {
2314 		bge_error(bgep, "ddi_intr_get_nintrs() failure, ret: %d, "
2315 		    "count: %d", ret, count);
2316 
2317 		return (DDI_FAILURE);
2318 	}
2319 
2320 	/* Get number of available interrupts */
2321 	ret = ddi_intr_get_navail(dip, intr_type, &avail);
2322 	if ((ret != DDI_SUCCESS) || (avail == 0)) {
2323 		bge_error(bgep, "ddi_intr_get_navail() failure, "
2324 		    "ret: %d, avail: %d\n", ret, avail);
2325 
2326 		return (DDI_FAILURE);
2327 	}
2328 
2329 	if (avail < count) {
2330 		bge_log(bgep, "nitrs() returned %d, navail returned %d\n",
2331 		    count, avail);
2332 	}
2333 
2334 	/*
2335 	 * BGE hardware generates only single MSI even though it claims
2336 	 * to support multiple MSIs. So, hard code MSI count value to 1.
2337 	 */
2338 	if (intr_type == DDI_INTR_TYPE_MSI) {
2339 		count = 1;
2340 		flag = DDI_INTR_ALLOC_STRICT;
2341 	} else {
2342 		flag = DDI_INTR_ALLOC_NORMAL;
2343 	}
2344 
2345 	/* Allocate an array of interrupt handles */
2346 	intr_size = count * sizeof (ddi_intr_handle_t);
2347 	bgep->htable = kmem_alloc(intr_size, KM_SLEEP);
2348 
2349 	/* Call ddi_intr_alloc() */
2350 	ret = ddi_intr_alloc(dip, bgep->htable, intr_type, 0,
2351 	    count, &actual, flag);
2352 
2353 	if ((ret != DDI_SUCCESS) || (actual == 0)) {
2354 		bge_error(bgep, "ddi_intr_alloc() failed %d\n", ret);
2355 
2356 		kmem_free(bgep->htable, intr_size);
2357 		return (DDI_FAILURE);
2358 	}
2359 
2360 	if (actual < count) {
2361 		bge_log(bgep, "Requested: %d, Received: %d\n", count, actual);
2362 	}
2363 
2364 	bgep->intr_cnt = actual;
2365 
2366 	/*
2367 	 * Get priority for first msi, assume remaining are all the same
2368 	 */
2369 	if ((ret = ddi_intr_get_pri(bgep->htable[0], &bgep->intr_pri)) !=
2370 	    DDI_SUCCESS) {
2371 		bge_error(bgep, "ddi_intr_get_pri() failed %d\n", ret);
2372 
2373 		/* Free already allocated intr */
2374 		for (i = 0; i < actual; i++) {
2375 			(void) ddi_intr_free(bgep->htable[i]);
2376 		}
2377 
2378 		kmem_free(bgep->htable, intr_size);
2379 		return (DDI_FAILURE);
2380 	}
2381 
2382 	/* Call ddi_intr_add_handler() */
2383 	for (i = 0; i < actual; i++) {
2384 		if ((ret = ddi_intr_add_handler(bgep->htable[i], bge_intr,
2385 		    (caddr_t)bgep, (caddr_t)(uintptr_t)i)) != DDI_SUCCESS) {
2386 			bge_error(bgep, "ddi_intr_add_handler() "
2387 			    "failed %d\n", ret);
2388 
2389 			/* Free already allocated intr */
2390 			for (i = 0; i < actual; i++) {
2391 				(void) ddi_intr_free(bgep->htable[i]);
2392 			}
2393 
2394 			kmem_free(bgep->htable, intr_size);
2395 			return (DDI_FAILURE);
2396 		}
2397 	}
2398 
2399 	if ((ret = ddi_intr_get_cap(bgep->htable[0], &bgep->intr_cap))
2400 		!= DDI_SUCCESS) {
2401 		bge_error(bgep, "ddi_intr_get_cap() failed %d\n", ret);
2402 
2403 		for (i = 0; i < actual; i++) {
2404 			(void) ddi_intr_remove_handler(bgep->htable[i]);
2405 			(void) ddi_intr_free(bgep->htable[i]);
2406 		}
2407 
2408 		kmem_free(bgep->htable, intr_size);
2409 		return (DDI_FAILURE);
2410 	}
2411 
2412 	return (DDI_SUCCESS);
2413 }
2414 
2415 /*
2416  * bge_rem_intrs:
2417  *
2418  * Unregister FIXED or MSI interrupts
2419  */
2420 static void
2421 bge_rem_intrs(bge_t *bgep)
2422 {
2423 	int	i;
2424 
2425 	bge_log(bgep, "bge_rem_intrs\n");
2426 
2427 	/* Disable all interrupts */
2428 	if (bgep->intr_cap & DDI_INTR_FLAG_BLOCK) {
2429 		/* Call ddi_intr_block_disable() */
2430 		(void) ddi_intr_block_disable(bgep->htable, bgep->intr_cnt);
2431 	} else {
2432 		for (i = 0; i < bgep->intr_cnt; i++) {
2433 			(void) ddi_intr_disable(bgep->htable[i]);
2434 		}
2435 	}
2436 
2437 	/* Call ddi_intr_remove_handler() */
2438 	for (i = 0; i < bgep->intr_cnt; i++) {
2439 		(void) ddi_intr_remove_handler(bgep->htable[i]);
2440 		(void) ddi_intr_free(bgep->htable[i]);
2441 	}
2442 
2443 	kmem_free(bgep->htable, bgep->intr_cnt * sizeof (ddi_intr_handle_t));
2444 }
2445