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