xref: /titanic_50/usr/src/uts/common/io/bge/bge_main2.c (revision 64cfc8ed0c5da83f827c424588bfe709e3fb5a7a)
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 2007 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 "bge_impl.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[] = "Broadcom Gb Ethernet v0.60";
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 fm_cap[] = "fm-capable";
52 static char default_mtu[] = "default_mtu";
53 
54 static int bge_add_intrs(bge_t *, int);
55 static void bge_rem_intrs(bge_t *);
56 
57 /*
58  * Describes the chip's DMA engine
59  */
60 static ddi_dma_attr_t dma_attr = {
61 	DMA_ATTR_V0,			/* dma_attr version	*/
62 	0x0000000000000000ull,		/* dma_attr_addr_lo	*/
63 	0xFFFFFFFFFFFFFFFFull,		/* dma_attr_addr_hi	*/
64 	0x00000000FFFFFFFFull,		/* dma_attr_count_max	*/
65 	0x0000000000000001ull,		/* dma_attr_align	*/
66 	0x00000FFF,			/* dma_attr_burstsizes	*/
67 	0x00000001,			/* dma_attr_minxfer	*/
68 	0x000000000000FFFFull,		/* dma_attr_maxxfer	*/
69 	0xFFFFFFFFFFFFFFFFull,		/* dma_attr_seg		*/
70 	1,				/* dma_attr_sgllen 	*/
71 	0x00000001,			/* dma_attr_granular 	*/
72 	DDI_DMA_FLAGERR			/* dma_attr_flags */
73 };
74 
75 /*
76  * PIO access attributes for registers
77  */
78 static ddi_device_acc_attr_t bge_reg_accattr = {
79 	DDI_DEVICE_ATTR_V0,
80 	DDI_NEVERSWAP_ACC,
81 	DDI_STRICTORDER_ACC,
82 	DDI_FLAGERR_ACC
83 };
84 
85 /*
86  * DMA access attributes for descriptors: NOT to be byte swapped.
87  */
88 static ddi_device_acc_attr_t bge_desc_accattr = {
89 	DDI_DEVICE_ATTR_V0,
90 	DDI_NEVERSWAP_ACC,
91 	DDI_STRICTORDER_ACC,
92 	DDI_FLAGERR_ACC
93 };
94 
95 /*
96  * DMA access attributes for data: NOT to be byte swapped.
97  */
98 static ddi_device_acc_attr_t bge_data_accattr = {
99 	DDI_DEVICE_ATTR_V0,
100 	DDI_NEVERSWAP_ACC,
101 	DDI_STRICTORDER_ACC
102 };
103 
104 /*
105  * Versions of the O/S up to Solaris 8 didn't support network booting
106  * from any network interface except the first (NET0).  Patching this
107  * flag to a non-zero value will tell the driver to work around this
108  * limitation by creating an extra (internal) pathname node.  To do
109  * this, just add a line like the following to the CLIENT'S etc/system
110  * file ON THE ROOT FILESYSTEM SERVER before booting the client:
111  *
112  *	set bge:bge_net1_boot_support = 1;
113  */
114 static uint32_t bge_net1_boot_support = 1;
115 
116 static int		bge_m_start(void *);
117 static void		bge_m_stop(void *);
118 static int		bge_m_promisc(void *, boolean_t);
119 static int		bge_m_multicst(void *, boolean_t, const uint8_t *);
120 static int		bge_m_unicst(void *, const uint8_t *);
121 static void		bge_m_resources(void *);
122 static void		bge_m_ioctl(void *, queue_t *, mblk_t *);
123 static boolean_t	bge_m_getcapab(void *, mac_capab_t, void *);
124 static int		bge_unicst_set(void *, const uint8_t *,
125     mac_addr_slot_t);
126 static int		bge_m_unicst_add(void *, mac_multi_addr_t *);
127 static int		bge_m_unicst_remove(void *, mac_addr_slot_t);
128 static int		bge_m_unicst_modify(void *, mac_multi_addr_t *);
129 static int		bge_m_unicst_get(void *, mac_multi_addr_t *);
130 
131 #define	BGE_M_CALLBACK_FLAGS	(MC_RESOURCES | MC_IOCTL | MC_GETCAPAB)
132 
133 static mac_callbacks_t bge_m_callbacks = {
134 	BGE_M_CALLBACK_FLAGS,
135 	bge_m_stat,
136 	bge_m_start,
137 	bge_m_stop,
138 	bge_m_promisc,
139 	bge_m_multicst,
140 	bge_m_unicst,
141 	bge_m_tx,
142 	bge_m_resources,
143 	bge_m_ioctl,
144 	bge_m_getcapab
145 };
146 
147 /*
148  * ========== Transmit and receive ring reinitialisation ==========
149  */
150 
151 /*
152  * These <reinit> routines each reset the specified ring to an initial
153  * state, assuming that the corresponding <init> routine has already
154  * been called exactly once.
155  */
156 
157 static void
158 bge_reinit_send_ring(send_ring_t *srp)
159 {
160 	bge_queue_t *txbuf_queue;
161 	bge_queue_item_t *txbuf_head;
162 	sw_txbuf_t *txbuf;
163 	sw_sbd_t *ssbdp;
164 	uint32_t slot;
165 
166 	/*
167 	 * Reinitialise control variables ...
168 	 */
169 	srp->tx_flow = 0;
170 	srp->tx_next = 0;
171 	srp->txfill_next = 0;
172 	srp->tx_free = srp->desc.nslots;
173 	ASSERT(mutex_owned(srp->tc_lock));
174 	srp->tc_next = 0;
175 	srp->txpkt_next = 0;
176 	srp->tx_block = 0;
177 	srp->tx_nobd = 0;
178 	srp->tx_nobuf = 0;
179 
180 	/*
181 	 * Initialize the tx buffer push queue
182 	 */
183 	mutex_enter(srp->freetxbuf_lock);
184 	mutex_enter(srp->txbuf_lock);
185 	txbuf_queue = &srp->freetxbuf_queue;
186 	txbuf_queue->head = NULL;
187 	txbuf_queue->count = 0;
188 	txbuf_queue->lock = srp->freetxbuf_lock;
189 	srp->txbuf_push_queue = txbuf_queue;
190 
191 	/*
192 	 * Initialize the tx buffer pop queue
193 	 */
194 	txbuf_queue = &srp->txbuf_queue;
195 	txbuf_queue->head = NULL;
196 	txbuf_queue->count = 0;
197 	txbuf_queue->lock = srp->txbuf_lock;
198 	srp->txbuf_pop_queue = txbuf_queue;
199 	txbuf_head = srp->txbuf_head;
200 	txbuf = srp->txbuf;
201 	for (slot = 0; slot < srp->tx_buffers; ++slot) {
202 		txbuf_head->item = txbuf;
203 		txbuf_head->next = txbuf_queue->head;
204 		txbuf_queue->head = txbuf_head;
205 		txbuf_queue->count++;
206 		txbuf++;
207 		txbuf_head++;
208 	}
209 	mutex_exit(srp->txbuf_lock);
210 	mutex_exit(srp->freetxbuf_lock);
211 
212 	/*
213 	 * Zero and sync all the h/w Send Buffer Descriptors
214 	 */
215 	DMA_ZERO(srp->desc);
216 	DMA_SYNC(srp->desc, DDI_DMA_SYNC_FORDEV);
217 	bzero(srp->pktp, BGE_SEND_BUF_MAX * sizeof (*srp->pktp));
218 	ssbdp = srp->sw_sbds;
219 	for (slot = 0; slot < srp->desc.nslots; ++ssbdp, ++slot)
220 		ssbdp->pbuf = NULL;
221 }
222 
223 static void
224 bge_reinit_recv_ring(recv_ring_t *rrp)
225 {
226 	/*
227 	 * Reinitialise control variables ...
228 	 */
229 	rrp->rx_next = 0;
230 }
231 
232 static void
233 bge_reinit_buff_ring(buff_ring_t *brp, uint32_t ring)
234 {
235 	bge_rbd_t *hw_rbd_p;
236 	sw_rbd_t *srbdp;
237 	uint32_t bufsize;
238 	uint32_t nslots;
239 	uint32_t slot;
240 
241 	static uint16_t ring_type_flag[BGE_BUFF_RINGS_MAX] = {
242 		RBD_FLAG_STD_RING,
243 		RBD_FLAG_JUMBO_RING,
244 		RBD_FLAG_MINI_RING
245 	};
246 
247 	/*
248 	 * Zero, initialise and sync all the h/w Receive Buffer Descriptors
249 	 * Note: all the remaining fields (<type>, <flags>, <ip_cksum>,
250 	 * <tcp_udp_cksum>, <error_flag>, <vlan_tag>, and <reserved>)
251 	 * should be zeroed, and so don't need to be set up specifically
252 	 * once the whole area has been cleared.
253 	 */
254 	DMA_ZERO(brp->desc);
255 
256 	hw_rbd_p = DMA_VPTR(brp->desc);
257 	nslots = brp->desc.nslots;
258 	ASSERT(brp->buf[0].nslots == nslots/BGE_SPLIT);
259 	bufsize = brp->buf[0].size;
260 	srbdp = brp->sw_rbds;
261 	for (slot = 0; slot < nslots; ++hw_rbd_p, ++srbdp, ++slot) {
262 		hw_rbd_p->host_buf_addr = srbdp->pbuf.cookie.dmac_laddress;
263 		hw_rbd_p->index = slot;
264 		hw_rbd_p->len = bufsize;
265 		hw_rbd_p->opaque = srbdp->pbuf.token;
266 		hw_rbd_p->flags |= ring_type_flag[ring];
267 	}
268 
269 	DMA_SYNC(brp->desc, DDI_DMA_SYNC_FORDEV);
270 
271 	/*
272 	 * Finally, reinitialise the ring control variables ...
273 	 */
274 	brp->rf_next = (nslots != 0) ? (nslots-1) : 0;
275 }
276 
277 /*
278  * Reinitialize all rings
279  */
280 static void
281 bge_reinit_rings(bge_t *bgep)
282 {
283 	uint32_t ring;
284 
285 	ASSERT(mutex_owned(bgep->genlock));
286 
287 	/*
288 	 * Send Rings ...
289 	 */
290 	for (ring = 0; ring < bgep->chipid.tx_rings; ++ring)
291 		bge_reinit_send_ring(&bgep->send[ring]);
292 
293 	/*
294 	 * Receive Return Rings ...
295 	 */
296 	for (ring = 0; ring < bgep->chipid.rx_rings; ++ring)
297 		bge_reinit_recv_ring(&bgep->recv[ring]);
298 
299 	/*
300 	 * Receive Producer Rings ...
301 	 */
302 	for (ring = 0; ring < BGE_BUFF_RINGS_USED; ++ring)
303 		bge_reinit_buff_ring(&bgep->buff[ring], ring);
304 }
305 
306 /*
307  * ========== Internal state management entry points ==========
308  */
309 
310 #undef	BGE_DBG
311 #define	BGE_DBG		BGE_DBG_NEMO	/* debug flag for this code	*/
312 
313 /*
314  * These routines provide all the functionality required by the
315  * corresponding GLD entry points, but don't update the GLD state
316  * so they can be called internally without disturbing our record
317  * of what GLD thinks we should be doing ...
318  */
319 
320 /*
321  *	bge_reset() -- reset h/w & rings to initial state
322  */
323 static int
324 #ifdef BGE_IPMI_ASF
325 bge_reset(bge_t *bgep, uint_t asf_mode)
326 #else
327 bge_reset(bge_t *bgep)
328 #endif
329 {
330 	uint32_t	ring;
331 	int retval;
332 
333 	BGE_TRACE(("bge_reset($%p)", (void *)bgep));
334 
335 	ASSERT(mutex_owned(bgep->genlock));
336 
337 	/*
338 	 * Grab all the other mutexes in the world (this should
339 	 * ensure no other threads are manipulating driver state)
340 	 */
341 	for (ring = 0; ring < BGE_RECV_RINGS_MAX; ++ring)
342 		mutex_enter(bgep->recv[ring].rx_lock);
343 	for (ring = 0; ring < BGE_BUFF_RINGS_MAX; ++ring)
344 		mutex_enter(bgep->buff[ring].rf_lock);
345 	rw_enter(bgep->errlock, RW_WRITER);
346 	for (ring = 0; ring < BGE_SEND_RINGS_MAX; ++ring)
347 		mutex_enter(bgep->send[ring].tx_lock);
348 	for (ring = 0; ring < BGE_SEND_RINGS_MAX; ++ring)
349 		mutex_enter(bgep->send[ring].tc_lock);
350 
351 #ifdef BGE_IPMI_ASF
352 	retval = bge_chip_reset(bgep, B_TRUE, asf_mode);
353 #else
354 	retval = bge_chip_reset(bgep, B_TRUE);
355 #endif
356 	bge_reinit_rings(bgep);
357 
358 	/*
359 	 * Free the world ...
360 	 */
361 	for (ring = BGE_SEND_RINGS_MAX; ring-- > 0; )
362 		mutex_exit(bgep->send[ring].tc_lock);
363 	for (ring = 0; ring < BGE_SEND_RINGS_MAX; ++ring)
364 		mutex_exit(bgep->send[ring].tx_lock);
365 	rw_exit(bgep->errlock);
366 	for (ring = BGE_BUFF_RINGS_MAX; ring-- > 0; )
367 		mutex_exit(bgep->buff[ring].rf_lock);
368 	for (ring = BGE_RECV_RINGS_MAX; ring-- > 0; )
369 		mutex_exit(bgep->recv[ring].rx_lock);
370 
371 	BGE_DEBUG(("bge_reset($%p) done", (void *)bgep));
372 	return (retval);
373 }
374 
375 /*
376  *	bge_stop() -- stop processing, don't reset h/w or rings
377  */
378 static void
379 bge_stop(bge_t *bgep)
380 {
381 	BGE_TRACE(("bge_stop($%p)", (void *)bgep));
382 
383 	ASSERT(mutex_owned(bgep->genlock));
384 
385 #ifdef BGE_IPMI_ASF
386 	if (bgep->asf_enabled) {
387 		bgep->asf_pseudostop = B_TRUE;
388 	} else {
389 #endif
390 		bge_chip_stop(bgep, B_FALSE);
391 #ifdef BGE_IPMI_ASF
392 	}
393 #endif
394 
395 	BGE_DEBUG(("bge_stop($%p) done", (void *)bgep));
396 }
397 
398 /*
399  *	bge_start() -- start transmitting/receiving
400  */
401 static int
402 bge_start(bge_t *bgep, boolean_t reset_phys)
403 {
404 	int retval;
405 
406 	BGE_TRACE(("bge_start($%p, %d)", (void *)bgep, reset_phys));
407 
408 	ASSERT(mutex_owned(bgep->genlock));
409 
410 	/*
411 	 * Start chip processing, including enabling interrupts
412 	 */
413 	retval = bge_chip_start(bgep, reset_phys);
414 
415 	BGE_DEBUG(("bge_start($%p, %d) done", (void *)bgep, reset_phys));
416 	return (retval);
417 }
418 
419 /*
420  * bge_restart - restart transmitting/receiving after error or suspend
421  */
422 int
423 bge_restart(bge_t *bgep, boolean_t reset_phys)
424 {
425 	int retval = DDI_SUCCESS;
426 	ASSERT(mutex_owned(bgep->genlock));
427 
428 #ifdef BGE_IPMI_ASF
429 	if (bgep->asf_enabled) {
430 		if (bge_reset(bgep, ASF_MODE_POST_INIT) != DDI_SUCCESS)
431 			retval = DDI_FAILURE;
432 	} else
433 		if (bge_reset(bgep, ASF_MODE_NONE) != DDI_SUCCESS)
434 			retval = DDI_FAILURE;
435 #else
436 	if (bge_reset(bgep) != DDI_SUCCESS)
437 		retval = DDI_FAILURE;
438 #endif
439 	if (bgep->bge_mac_state == BGE_MAC_STARTED) {
440 		if (bge_start(bgep, reset_phys) != DDI_SUCCESS)
441 			retval = DDI_FAILURE;
442 		bgep->watchdog = 0;
443 		ddi_trigger_softintr(bgep->drain_id);
444 	}
445 
446 	BGE_DEBUG(("bge_restart($%p, %d) done", (void *)bgep, reset_phys));
447 	return (retval);
448 }
449 
450 
451 /*
452  * ========== Nemo-required management entry points ==========
453  */
454 
455 #undef	BGE_DBG
456 #define	BGE_DBG		BGE_DBG_NEMO	/* debug flag for this code	*/
457 
458 /*
459  *	bge_m_stop() -- stop transmitting/receiving
460  */
461 static void
462 bge_m_stop(void *arg)
463 {
464 	bge_t *bgep = arg;		/* private device info	*/
465 	send_ring_t *srp;
466 	uint32_t ring;
467 
468 	BGE_TRACE(("bge_m_stop($%p)", arg));
469 
470 	/*
471 	 * Just stop processing, then record new GLD state
472 	 */
473 	mutex_enter(bgep->genlock);
474 	if (!(bgep->progress & PROGRESS_INTR)) {
475 		/* can happen during autorecovery */
476 		mutex_exit(bgep->genlock);
477 		return;
478 	}
479 	bge_stop(bgep);
480 	/*
481 	 * Free the possible tx buffers allocated in tx process.
482 	 */
483 #ifdef BGE_IPMI_ASF
484 	if (!bgep->asf_pseudostop)
485 #endif
486 	{
487 		rw_enter(bgep->errlock, RW_WRITER);
488 		for (ring = 0; ring < bgep->chipid.tx_rings; ++ring) {
489 			srp = &bgep->send[ring];
490 			mutex_enter(srp->tx_lock);
491 			if (srp->tx_array > 1)
492 				bge_free_txbuf_arrays(srp);
493 			mutex_exit(srp->tx_lock);
494 		}
495 		rw_exit(bgep->errlock);
496 	}
497 	bgep->bge_mac_state = BGE_MAC_STOPPED;
498 	BGE_DEBUG(("bge_m_stop($%p) done", arg));
499 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
500 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_UNAFFECTED);
501 	mutex_exit(bgep->genlock);
502 }
503 
504 /*
505  *	bge_m_start() -- start transmitting/receiving
506  */
507 static int
508 bge_m_start(void *arg)
509 {
510 	bge_t *bgep = arg;		/* private device info	*/
511 
512 	BGE_TRACE(("bge_m_start($%p)", arg));
513 
514 	/*
515 	 * Start processing and record new GLD state
516 	 */
517 	mutex_enter(bgep->genlock);
518 	if (!(bgep->progress & PROGRESS_INTR)) {
519 		/* can happen during autorecovery */
520 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
521 		mutex_exit(bgep->genlock);
522 		return (EIO);
523 	}
524 #ifdef BGE_IPMI_ASF
525 	if (bgep->asf_enabled) {
526 		if ((bgep->asf_status == ASF_STAT_RUN) &&
527 		    (bgep->asf_pseudostop)) {
528 			bgep->bge_mac_state = BGE_MAC_STARTED;
529 			mutex_exit(bgep->genlock);
530 			return (0);
531 		}
532 	}
533 	if (bge_reset(bgep, ASF_MODE_INIT) != DDI_SUCCESS) {
534 #else
535 	if (bge_reset(bgep) != DDI_SUCCESS) {
536 #endif
537 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
538 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
539 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
540 		mutex_exit(bgep->genlock);
541 		return (EIO);
542 	}
543 	if (bge_start(bgep, B_TRUE) != DDI_SUCCESS) {
544 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
545 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
546 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
547 		mutex_exit(bgep->genlock);
548 		return (EIO);
549 	}
550 	bgep->bge_mac_state = BGE_MAC_STARTED;
551 	BGE_DEBUG(("bge_m_start($%p) done", arg));
552 
553 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
554 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
555 		mutex_exit(bgep->genlock);
556 		return (EIO);
557 	}
558 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
559 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
560 		mutex_exit(bgep->genlock);
561 		return (EIO);
562 	}
563 #ifdef BGE_IPMI_ASF
564 	if (bgep->asf_enabled) {
565 		if (bgep->asf_status != ASF_STAT_RUN) {
566 			/* start ASF heart beat */
567 			bgep->asf_timeout_id = timeout(bge_asf_heartbeat,
568 			    (void *)bgep,
569 			    drv_usectohz(BGE_ASF_HEARTBEAT_INTERVAL));
570 			bgep->asf_status = ASF_STAT_RUN;
571 		}
572 	}
573 #endif
574 	mutex_exit(bgep->genlock);
575 
576 	return (0);
577 }
578 
579 /*
580  *	bge_m_unicst() -- set the physical network address
581  */
582 static int
583 bge_m_unicst(void *arg, const uint8_t *macaddr)
584 {
585 	/*
586 	 * Request to set address in
587 	 * address slot 0, i.e., default address
588 	 */
589 	return (bge_unicst_set(arg, macaddr, 0));
590 }
591 
592 /*
593  *	bge_unicst_set() -- set the physical network address
594  */
595 static int
596 bge_unicst_set(void *arg, const uint8_t *macaddr, mac_addr_slot_t slot)
597 {
598 	bge_t *bgep = arg;		/* private device info	*/
599 
600 	BGE_TRACE(("bge_m_unicst_set($%p, %s)", arg,
601 	    ether_sprintf((void *)macaddr)));
602 	/*
603 	 * Remember the new current address in the driver state
604 	 * Sync the chip's idea of the address too ...
605 	 */
606 	mutex_enter(bgep->genlock);
607 	if (!(bgep->progress & PROGRESS_INTR)) {
608 		/* can happen during autorecovery */
609 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
610 		mutex_exit(bgep->genlock);
611 		return (EIO);
612 	}
613 	ethaddr_copy(macaddr, bgep->curr_addr[slot].addr);
614 #ifdef BGE_IPMI_ASF
615 	if (bge_chip_sync(bgep, B_FALSE) == DDI_FAILURE) {
616 #else
617 	if (bge_chip_sync(bgep) == DDI_FAILURE) {
618 #endif
619 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
620 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
621 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
622 		mutex_exit(bgep->genlock);
623 		return (EIO);
624 	}
625 #ifdef BGE_IPMI_ASF
626 	if (bgep->asf_enabled) {
627 		/*
628 		 * The above bge_chip_sync() function wrote the ethernet MAC
629 		 * addresses registers which destroyed the IPMI/ASF sideband.
630 		 * Here, we have to reset chip to make IPMI/ASF sideband work.
631 		 */
632 		if (bgep->asf_status == ASF_STAT_RUN) {
633 			/*
634 			 * We must stop ASF heart beat before bge_chip_stop(),
635 			 * otherwise some computers (ex. IBM HS20 blade server)
636 			 * may crash.
637 			 */
638 			bge_asf_update_status(bgep);
639 			bge_asf_stop_timer(bgep);
640 			bgep->asf_status = ASF_STAT_STOP;
641 
642 			bge_asf_pre_reset_operations(bgep, BGE_INIT_RESET);
643 		}
644 		bge_chip_stop(bgep, B_FALSE);
645 
646 		if (bge_restart(bgep, B_FALSE) == DDI_FAILURE) {
647 			(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
648 			(void) bge_check_acc_handle(bgep, bgep->io_handle);
649 			ddi_fm_service_impact(bgep->devinfo,
650 			    DDI_SERVICE_DEGRADED);
651 			mutex_exit(bgep->genlock);
652 			return (EIO);
653 		}
654 
655 		/*
656 		 * Start our ASF heartbeat counter as soon as possible.
657 		 */
658 		if (bgep->asf_status != ASF_STAT_RUN) {
659 			/* start ASF heart beat */
660 			bgep->asf_timeout_id = timeout(bge_asf_heartbeat,
661 			    (void *)bgep,
662 			    drv_usectohz(BGE_ASF_HEARTBEAT_INTERVAL));
663 			bgep->asf_status = ASF_STAT_RUN;
664 		}
665 	}
666 #endif
667 	BGE_DEBUG(("bge_m_unicst_set($%p) done", arg));
668 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
669 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
670 		mutex_exit(bgep->genlock);
671 		return (EIO);
672 	}
673 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
674 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
675 		mutex_exit(bgep->genlock);
676 		return (EIO);
677 	}
678 	mutex_exit(bgep->genlock);
679 
680 	return (0);
681 }
682 
683 /*
684  * The following four routines are used as callbacks for multiple MAC
685  * address support:
686  *    -  bge_m_unicst_add(void *, mac_multi_addr_t *);
687  *    -  bge_m_unicst_remove(void *, mac_addr_slot_t);
688  *    -  bge_m_unicst_modify(void *, mac_multi_addr_t *);
689  *    -  bge_m_unicst_get(void *, mac_multi_addr_t *);
690  */
691 
692 /*
693  * bge_m_unicst_add() - will find an unused address slot, set the
694  * address value to the one specified, reserve that slot and enable
695  * the NIC to start filtering on the new MAC address.
696  * address slot. Returns 0 on success.
697  */
698 static int
699 bge_m_unicst_add(void *arg, mac_multi_addr_t *maddr)
700 {
701 	bge_t *bgep = arg;		/* private device info	*/
702 	mac_addr_slot_t slot;
703 	int err;
704 
705 	if (mac_unicst_verify(bgep->mh,
706 	    maddr->mma_addr, maddr->mma_addrlen) == B_FALSE)
707 		return (EINVAL);
708 
709 	mutex_enter(bgep->genlock);
710 	if (bgep->unicst_addr_avail == 0) {
711 		/* no slots available */
712 		mutex_exit(bgep->genlock);
713 		return (ENOSPC);
714 	}
715 
716 	/*
717 	 * Primary/default address is in slot 0. The next three
718 	 * addresses are the multiple MAC addresses. So multiple
719 	 * MAC address 0 is in slot 1, 1 in slot 2, and so on.
720 	 * So the first multiple MAC address resides in slot 1.
721 	 */
722 	for (slot = 1; slot < bgep->unicst_addr_total; slot++) {
723 		if (bgep->curr_addr[slot].set == B_FALSE) {
724 			bgep->curr_addr[slot].set = B_TRUE;
725 			break;
726 		}
727 	}
728 
729 	ASSERT(slot < bgep->unicst_addr_total);
730 	bgep->unicst_addr_avail--;
731 	mutex_exit(bgep->genlock);
732 	maddr->mma_slot = slot;
733 
734 	if ((err = bge_unicst_set(bgep, maddr->mma_addr, slot)) != 0) {
735 		mutex_enter(bgep->genlock);
736 		bgep->curr_addr[slot].set = B_FALSE;
737 		bgep->unicst_addr_avail++;
738 		mutex_exit(bgep->genlock);
739 	}
740 	return (err);
741 }
742 
743 /*
744  * bge_m_unicst_remove() - removes a MAC address that was added by a
745  * call to bge_m_unicst_add(). The slot number that was returned in
746  * add() is passed in the call to remove the address.
747  * Returns 0 on success.
748  */
749 static int
750 bge_m_unicst_remove(void *arg, mac_addr_slot_t slot)
751 {
752 	bge_t *bgep = arg;		/* private device info	*/
753 
754 	if (slot <= 0 || slot >= bgep->unicst_addr_total)
755 		return (EINVAL);
756 
757 	mutex_enter(bgep->genlock);
758 	if (bgep->curr_addr[slot].set == B_TRUE) {
759 		bgep->curr_addr[slot].set = B_FALSE;
760 		bgep->unicst_addr_avail++;
761 		mutex_exit(bgep->genlock);
762 		/*
763 		 * Copy the default address to the passed slot
764 		 */
765 		return (bge_unicst_set(bgep, bgep->curr_addr[0].addr, slot));
766 	}
767 	mutex_exit(bgep->genlock);
768 	return (EINVAL);
769 }
770 
771 /*
772  * bge_m_unicst_modify() - modifies the value of an address that
773  * has been added by bge_m_unicst_add(). The new address, address
774  * length and the slot number that was returned in the call to add
775  * should be passed to bge_m_unicst_modify(). mma_flags should be
776  * set to 0. Returns 0 on success.
777  */
778 static int
779 bge_m_unicst_modify(void *arg, mac_multi_addr_t *maddr)
780 {
781 	bge_t *bgep = arg;		/* private device info	*/
782 	mac_addr_slot_t slot;
783 
784 	if (mac_unicst_verify(bgep->mh,
785 	    maddr->mma_addr, maddr->mma_addrlen) == B_FALSE)
786 		return (EINVAL);
787 
788 	slot = maddr->mma_slot;
789 
790 	if (slot <= 0 || slot >= bgep->unicst_addr_total)
791 		return (EINVAL);
792 
793 	mutex_enter(bgep->genlock);
794 	if (bgep->curr_addr[slot].set == B_TRUE) {
795 		mutex_exit(bgep->genlock);
796 		return (bge_unicst_set(bgep, maddr->mma_addr, slot));
797 	}
798 	mutex_exit(bgep->genlock);
799 
800 	return (EINVAL);
801 }
802 
803 /*
804  * bge_m_unicst_get() - will get the MAC address and all other
805  * information related to the address slot passed in mac_multi_addr_t.
806  * mma_flags should be set to 0 in the call.
807  * On return, mma_flags can take the following values:
808  * 1) MMAC_SLOT_UNUSED
809  * 2) MMAC_SLOT_USED | MMAC_VENDOR_ADDR
810  * 3) MMAC_SLOT_UNUSED | MMAC_VENDOR_ADDR
811  * 4) MMAC_SLOT_USED
812  */
813 static int
814 bge_m_unicst_get(void *arg, mac_multi_addr_t *maddr)
815 {
816 	bge_t *bgep = arg;		/* private device info	*/
817 	mac_addr_slot_t slot;
818 
819 	slot = maddr->mma_slot;
820 
821 	if (slot <= 0 || slot >= bgep->unicst_addr_total)
822 		return (EINVAL);
823 
824 	mutex_enter(bgep->genlock);
825 	if (bgep->curr_addr[slot].set == B_TRUE) {
826 		ethaddr_copy(bgep->curr_addr[slot].addr,
827 		    maddr->mma_addr);
828 		maddr->mma_flags = MMAC_SLOT_USED;
829 	} else {
830 		maddr->mma_flags = MMAC_SLOT_UNUSED;
831 	}
832 	mutex_exit(bgep->genlock);
833 
834 	return (0);
835 }
836 
837 /*
838  * Compute the index of the required bit in the multicast hash map.
839  * This must mirror the way the hardware actually does it!
840  * See Broadcom document 570X-PG102-R page 125.
841  */
842 static uint32_t
843 bge_hash_index(const uint8_t *mca)
844 {
845 	uint32_t hash;
846 
847 	CRC32(hash, mca, ETHERADDRL, -1U, crc32_table);
848 
849 	return (hash);
850 }
851 
852 /*
853  *	bge_m_multicst_add() -- enable/disable a multicast address
854  */
855 static int
856 bge_m_multicst(void *arg, boolean_t add, const uint8_t *mca)
857 {
858 	bge_t *bgep = arg;		/* private device info	*/
859 	uint32_t hash;
860 	uint32_t index;
861 	uint32_t word;
862 	uint32_t bit;
863 	uint8_t *refp;
864 
865 	BGE_TRACE(("bge_m_multicst($%p, %s, %s)", arg,
866 	    (add) ? "add" : "remove", ether_sprintf((void *)mca)));
867 
868 	/*
869 	 * Precalculate all required masks, pointers etc ...
870 	 */
871 	hash = bge_hash_index(mca);
872 	index = hash % BGE_HASH_TABLE_SIZE;
873 	word = index/32u;
874 	bit = 1 << (index % 32u);
875 	refp = &bgep->mcast_refs[index];
876 
877 	BGE_DEBUG(("bge_m_multicst: hash 0x%x index %d (%d:0x%x) = %d",
878 	    hash, index, word, bit, *refp));
879 
880 	/*
881 	 * We must set the appropriate bit in the hash map (and the
882 	 * corresponding h/w register) when the refcount goes from 0
883 	 * to >0, and clear it when the last ref goes away (refcount
884 	 * goes from >0 back to 0).  If we change the hash map, we
885 	 * must also update the chip's hardware map registers.
886 	 */
887 	mutex_enter(bgep->genlock);
888 	if (!(bgep->progress & PROGRESS_INTR)) {
889 		/* can happen during autorecovery */
890 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
891 		mutex_exit(bgep->genlock);
892 		return (EIO);
893 	}
894 	if (add) {
895 		if ((*refp)++ == 0) {
896 			bgep->mcast_hash[word] |= bit;
897 #ifdef BGE_IPMI_ASF
898 			if (bge_chip_sync(bgep, B_TRUE) == DDI_FAILURE) {
899 #else
900 			if (bge_chip_sync(bgep) == DDI_FAILURE) {
901 #endif
902 				(void) bge_check_acc_handle(bgep,
903 				    bgep->cfg_handle);
904 				(void) bge_check_acc_handle(bgep,
905 				    bgep->io_handle);
906 				ddi_fm_service_impact(bgep->devinfo,
907 				    DDI_SERVICE_DEGRADED);
908 				mutex_exit(bgep->genlock);
909 				return (EIO);
910 			}
911 		}
912 	} else {
913 		if (--(*refp) == 0) {
914 			bgep->mcast_hash[word] &= ~bit;
915 #ifdef BGE_IPMI_ASF
916 			if (bge_chip_sync(bgep, B_TRUE) == DDI_FAILURE) {
917 #else
918 			if (bge_chip_sync(bgep) == DDI_FAILURE) {
919 #endif
920 				(void) bge_check_acc_handle(bgep,
921 				    bgep->cfg_handle);
922 				(void) bge_check_acc_handle(bgep,
923 				    bgep->io_handle);
924 				ddi_fm_service_impact(bgep->devinfo,
925 				    DDI_SERVICE_DEGRADED);
926 				mutex_exit(bgep->genlock);
927 				return (EIO);
928 			}
929 		}
930 	}
931 	BGE_DEBUG(("bge_m_multicst($%p) done", arg));
932 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
933 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
934 		mutex_exit(bgep->genlock);
935 		return (EIO);
936 	}
937 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
938 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
939 		mutex_exit(bgep->genlock);
940 		return (EIO);
941 	}
942 	mutex_exit(bgep->genlock);
943 
944 	return (0);
945 }
946 
947 /*
948  * bge_m_promisc() -- set or reset promiscuous mode on the board
949  *
950  *	Program the hardware to enable/disable promiscuous and/or
951  *	receive-all-multicast modes.
952  */
953 static int
954 bge_m_promisc(void *arg, boolean_t on)
955 {
956 	bge_t *bgep = arg;
957 
958 	BGE_TRACE(("bge_m_promisc_set($%p, %d)", arg, on));
959 
960 	/*
961 	 * Store MAC layer specified mode and pass to chip layer to update h/w
962 	 */
963 	mutex_enter(bgep->genlock);
964 	if (!(bgep->progress & PROGRESS_INTR)) {
965 		/* can happen during autorecovery */
966 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
967 		mutex_exit(bgep->genlock);
968 		return (EIO);
969 	}
970 	bgep->promisc = on;
971 #ifdef BGE_IPMI_ASF
972 	if (bge_chip_sync(bgep, B_TRUE) == DDI_FAILURE) {
973 #else
974 	if (bge_chip_sync(bgep) == DDI_FAILURE) {
975 #endif
976 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
977 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
978 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
979 		mutex_exit(bgep->genlock);
980 		return (EIO);
981 	}
982 	BGE_DEBUG(("bge_m_promisc_set($%p) done", arg));
983 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
984 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
985 		mutex_exit(bgep->genlock);
986 		return (EIO);
987 	}
988 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
989 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
990 		mutex_exit(bgep->genlock);
991 		return (EIO);
992 	}
993 	mutex_exit(bgep->genlock);
994 	return (0);
995 }
996 
997 /*ARGSUSED*/
998 static boolean_t
999 bge_m_getcapab(void *arg, mac_capab_t cap, void *cap_data)
1000 {
1001 	bge_t *bgep = arg;
1002 
1003 	switch (cap) {
1004 	case MAC_CAPAB_HCKSUM: {
1005 		uint32_t *txflags = cap_data;
1006 
1007 		*txflags = HCKSUM_INET_FULL_V4 | HCKSUM_IPHDRCKSUM;
1008 		break;
1009 	}
1010 
1011 	case MAC_CAPAB_POLL:
1012 		/*
1013 		 * There's nothing for us to fill in, simply returning
1014 		 * B_TRUE stating that we support polling is sufficient.
1015 		 */
1016 		break;
1017 
1018 	case MAC_CAPAB_MULTIADDRESS: {
1019 		multiaddress_capab_t	*mmacp = cap_data;
1020 
1021 		mutex_enter(bgep->genlock);
1022 		/*
1023 		 * The number of MAC addresses made available by
1024 		 * this capability is one less than the total as
1025 		 * the primary address in slot 0 is counted in
1026 		 * the total.
1027 		 */
1028 		mmacp->maddr_naddr = bgep->unicst_addr_total - 1;
1029 		mmacp->maddr_naddrfree = bgep->unicst_addr_avail;
1030 		/* No multiple factory addresses, set mma_flag to 0 */
1031 		mmacp->maddr_flag = 0;
1032 		mmacp->maddr_handle = bgep;
1033 		mmacp->maddr_add = bge_m_unicst_add;
1034 		mmacp->maddr_remove = bge_m_unicst_remove;
1035 		mmacp->maddr_modify = bge_m_unicst_modify;
1036 		mmacp->maddr_get = bge_m_unicst_get;
1037 		mmacp->maddr_reserve = NULL;
1038 		mutex_exit(bgep->genlock);
1039 		break;
1040 	}
1041 
1042 	default:
1043 		return (B_FALSE);
1044 	}
1045 	return (B_TRUE);
1046 }
1047 
1048 /*
1049  * Loopback ioctl code
1050  */
1051 
1052 static lb_property_t loopmodes[] = {
1053 	{ normal,	"normal",	BGE_LOOP_NONE		},
1054 	{ external,	"1000Mbps",	BGE_LOOP_EXTERNAL_1000	},
1055 	{ external,	"100Mbps",	BGE_LOOP_EXTERNAL_100	},
1056 	{ external,	"10Mbps",	BGE_LOOP_EXTERNAL_10	},
1057 	{ internal,	"PHY",		BGE_LOOP_INTERNAL_PHY	},
1058 	{ internal,	"MAC",		BGE_LOOP_INTERNAL_MAC	}
1059 };
1060 
1061 static enum ioc_reply
1062 bge_set_loop_mode(bge_t *bgep, uint32_t mode)
1063 {
1064 	/*
1065 	 * If the mode isn't being changed, there's nothing to do ...
1066 	 */
1067 	if (mode == bgep->param_loop_mode)
1068 		return (IOC_ACK);
1069 
1070 	/*
1071 	 * Validate the requested mode and prepare a suitable message
1072 	 * to explain the link down/up cycle that the change will
1073 	 * probably induce ...
1074 	 */
1075 	switch (mode) {
1076 	default:
1077 		return (IOC_INVAL);
1078 
1079 	case BGE_LOOP_NONE:
1080 	case BGE_LOOP_EXTERNAL_1000:
1081 	case BGE_LOOP_EXTERNAL_100:
1082 	case BGE_LOOP_EXTERNAL_10:
1083 	case BGE_LOOP_INTERNAL_PHY:
1084 	case BGE_LOOP_INTERNAL_MAC:
1085 		break;
1086 	}
1087 
1088 	/*
1089 	 * All OK; tell the caller to reprogram
1090 	 * the PHY and/or MAC for the new mode ...
1091 	 */
1092 	bgep->param_loop_mode = mode;
1093 	return (IOC_RESTART_ACK);
1094 }
1095 
1096 static enum ioc_reply
1097 bge_loop_ioctl(bge_t *bgep, queue_t *wq, mblk_t *mp, struct iocblk *iocp)
1098 {
1099 	lb_info_sz_t *lbsp;
1100 	lb_property_t *lbpp;
1101 	uint32_t *lbmp;
1102 	int cmd;
1103 
1104 	_NOTE(ARGUNUSED(wq))
1105 
1106 	/*
1107 	 * Validate format of ioctl
1108 	 */
1109 	if (mp->b_cont == NULL)
1110 		return (IOC_INVAL);
1111 
1112 	cmd = iocp->ioc_cmd;
1113 	switch (cmd) {
1114 	default:
1115 		/* NOTREACHED */
1116 		bge_error(bgep, "bge_loop_ioctl: invalid cmd 0x%x", cmd);
1117 		return (IOC_INVAL);
1118 
1119 	case LB_GET_INFO_SIZE:
1120 		if (iocp->ioc_count != sizeof (lb_info_sz_t))
1121 			return (IOC_INVAL);
1122 		lbsp = (lb_info_sz_t *)mp->b_cont->b_rptr;
1123 		*lbsp = sizeof (loopmodes);
1124 		return (IOC_REPLY);
1125 
1126 	case LB_GET_INFO:
1127 		if (iocp->ioc_count != sizeof (loopmodes))
1128 			return (IOC_INVAL);
1129 		lbpp = (lb_property_t *)mp->b_cont->b_rptr;
1130 		bcopy(loopmodes, lbpp, sizeof (loopmodes));
1131 		return (IOC_REPLY);
1132 
1133 	case LB_GET_MODE:
1134 		if (iocp->ioc_count != sizeof (uint32_t))
1135 			return (IOC_INVAL);
1136 		lbmp = (uint32_t *)mp->b_cont->b_rptr;
1137 		*lbmp = bgep->param_loop_mode;
1138 		return (IOC_REPLY);
1139 
1140 	case LB_SET_MODE:
1141 		if (iocp->ioc_count != sizeof (uint32_t))
1142 			return (IOC_INVAL);
1143 		lbmp = (uint32_t *)mp->b_cont->b_rptr;
1144 		return (bge_set_loop_mode(bgep, *lbmp));
1145 	}
1146 }
1147 
1148 /*
1149  * Specific bge IOCTLs, the gld module handles the generic ones.
1150  */
1151 static void
1152 bge_m_ioctl(void *arg, queue_t *wq, mblk_t *mp)
1153 {
1154 	bge_t *bgep = arg;
1155 	struct iocblk *iocp;
1156 	enum ioc_reply status;
1157 	boolean_t need_privilege;
1158 	int err;
1159 	int cmd;
1160 
1161 	/*
1162 	 * Validate the command before bothering with the mutex ...
1163 	 */
1164 	iocp = (struct iocblk *)mp->b_rptr;
1165 	iocp->ioc_error = 0;
1166 	need_privilege = B_TRUE;
1167 	cmd = iocp->ioc_cmd;
1168 	switch (cmd) {
1169 	default:
1170 		miocnak(wq, mp, 0, EINVAL);
1171 		return;
1172 
1173 	case BGE_MII_READ:
1174 	case BGE_MII_WRITE:
1175 	case BGE_SEE_READ:
1176 	case BGE_SEE_WRITE:
1177 	case BGE_FLASH_READ:
1178 	case BGE_FLASH_WRITE:
1179 	case BGE_DIAG:
1180 	case BGE_PEEK:
1181 	case BGE_POKE:
1182 	case BGE_PHY_RESET:
1183 	case BGE_SOFT_RESET:
1184 	case BGE_HARD_RESET:
1185 		break;
1186 
1187 	case LB_GET_INFO_SIZE:
1188 	case LB_GET_INFO:
1189 	case LB_GET_MODE:
1190 		need_privilege = B_FALSE;
1191 		/* FALLTHRU */
1192 	case LB_SET_MODE:
1193 		break;
1194 
1195 	case ND_GET:
1196 		need_privilege = B_FALSE;
1197 		/* FALLTHRU */
1198 	case ND_SET:
1199 		break;
1200 	}
1201 
1202 	if (need_privilege) {
1203 		/*
1204 		 * Check for specific net_config privilege on Solaris 10+.
1205 		 */
1206 		err = secpolicy_net_config(iocp->ioc_cr, B_FALSE);
1207 		if (err != 0) {
1208 			miocnak(wq, mp, 0, err);
1209 			return;
1210 		}
1211 	}
1212 
1213 	mutex_enter(bgep->genlock);
1214 	if (!(bgep->progress & PROGRESS_INTR)) {
1215 		/* can happen during autorecovery */
1216 		mutex_exit(bgep->genlock);
1217 		miocnak(wq, mp, 0, EIO);
1218 		return;
1219 	}
1220 
1221 	switch (cmd) {
1222 	default:
1223 		_NOTE(NOTREACHED)
1224 		status = IOC_INVAL;
1225 		break;
1226 
1227 	case BGE_MII_READ:
1228 	case BGE_MII_WRITE:
1229 	case BGE_SEE_READ:
1230 	case BGE_SEE_WRITE:
1231 	case BGE_FLASH_READ:
1232 	case BGE_FLASH_WRITE:
1233 	case BGE_DIAG:
1234 	case BGE_PEEK:
1235 	case BGE_POKE:
1236 	case BGE_PHY_RESET:
1237 	case BGE_SOFT_RESET:
1238 	case BGE_HARD_RESET:
1239 		status = bge_chip_ioctl(bgep, wq, mp, iocp);
1240 		break;
1241 
1242 	case LB_GET_INFO_SIZE:
1243 	case LB_GET_INFO:
1244 	case LB_GET_MODE:
1245 	case LB_SET_MODE:
1246 		status = bge_loop_ioctl(bgep, wq, mp, iocp);
1247 		break;
1248 
1249 	case ND_GET:
1250 	case ND_SET:
1251 		status = bge_nd_ioctl(bgep, wq, mp, iocp);
1252 		break;
1253 	}
1254 
1255 	/*
1256 	 * Do we need to reprogram the PHY and/or the MAC?
1257 	 * Do it now, while we still have the mutex.
1258 	 *
1259 	 * Note: update the PHY first, 'cos it controls the
1260 	 * speed/duplex parameters that the MAC code uses.
1261 	 */
1262 	switch (status) {
1263 	case IOC_RESTART_REPLY:
1264 	case IOC_RESTART_ACK:
1265 		if (bge_phys_update(bgep) != DDI_SUCCESS) {
1266 			ddi_fm_service_impact(bgep->devinfo,
1267 			    DDI_SERVICE_DEGRADED);
1268 			status = IOC_INVAL;
1269 		}
1270 #ifdef BGE_IPMI_ASF
1271 		if (bge_chip_sync(bgep, B_TRUE) == DDI_FAILURE) {
1272 #else
1273 		if (bge_chip_sync(bgep) == DDI_FAILURE) {
1274 #endif
1275 			ddi_fm_service_impact(bgep->devinfo,
1276 			    DDI_SERVICE_DEGRADED);
1277 			status = IOC_INVAL;
1278 		}
1279 		if (bgep->intr_type == DDI_INTR_TYPE_MSI)
1280 			bge_chip_msi_trig(bgep);
1281 		break;
1282 	}
1283 
1284 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
1285 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
1286 		status = IOC_INVAL;
1287 	}
1288 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
1289 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
1290 		status = IOC_INVAL;
1291 	}
1292 	mutex_exit(bgep->genlock);
1293 
1294 	/*
1295 	 * Finally, decide how to reply
1296 	 */
1297 	switch (status) {
1298 	default:
1299 	case IOC_INVAL:
1300 		/*
1301 		 * Error, reply with a NAK and EINVAL or the specified error
1302 		 */
1303 		miocnak(wq, mp, 0, iocp->ioc_error == 0 ?
1304 		    EINVAL : iocp->ioc_error);
1305 		break;
1306 
1307 	case IOC_DONE:
1308 		/*
1309 		 * OK, reply already sent
1310 		 */
1311 		break;
1312 
1313 	case IOC_RESTART_ACK:
1314 	case IOC_ACK:
1315 		/*
1316 		 * OK, reply with an ACK
1317 		 */
1318 		miocack(wq, mp, 0, 0);
1319 		break;
1320 
1321 	case IOC_RESTART_REPLY:
1322 	case IOC_REPLY:
1323 		/*
1324 		 * OK, send prepared reply as ACK or NAK
1325 		 */
1326 		mp->b_datap->db_type = iocp->ioc_error == 0 ?
1327 		    M_IOCACK : M_IOCNAK;
1328 		qreply(wq, mp);
1329 		break;
1330 	}
1331 }
1332 
1333 static void
1334 bge_m_resources(void *arg)
1335 {
1336 	bge_t *bgep = arg;
1337 	recv_ring_t *rrp;
1338 	mac_rx_fifo_t mrf;
1339 	int ring;
1340 
1341 	mutex_enter(bgep->genlock);
1342 
1343 	/*
1344 	 * Register Rx rings as resources and save mac
1345 	 * resource id for future reference
1346 	 */
1347 	mrf.mrf_type = MAC_RX_FIFO;
1348 	mrf.mrf_blank = bge_chip_blank;
1349 	mrf.mrf_arg = (void *)bgep;
1350 	mrf.mrf_normal_blank_time = bge_rx_ticks_norm;
1351 	mrf.mrf_normal_pkt_count = bge_rx_count_norm;
1352 
1353 	for (ring = 0; ring < bgep->chipid.rx_rings; ring++) {
1354 		rrp = &bgep->recv[ring];
1355 		rrp->handle = mac_resource_add(bgep->mh,
1356 		    (mac_resource_t *)&mrf);
1357 	}
1358 
1359 	mutex_exit(bgep->genlock);
1360 }
1361 
1362 /*
1363  * ========== Per-instance setup/teardown code ==========
1364  */
1365 
1366 #undef	BGE_DBG
1367 #define	BGE_DBG		BGE_DBG_INIT	/* debug flag for this code	*/
1368 /*
1369  * Allocate an area of memory and a DMA handle for accessing it
1370  */
1371 static int
1372 bge_alloc_dma_mem(bge_t *bgep, size_t memsize, ddi_device_acc_attr_t *attr_p,
1373 	uint_t dma_flags, dma_area_t *dma_p)
1374 {
1375 	caddr_t va;
1376 	int err;
1377 
1378 	BGE_TRACE(("bge_alloc_dma_mem($%p, %ld, $%p, 0x%x, $%p)",
1379 	    (void *)bgep, memsize, attr_p, dma_flags, dma_p));
1380 
1381 	/*
1382 	 * Allocate handle
1383 	 */
1384 	err = ddi_dma_alloc_handle(bgep->devinfo, &dma_attr,
1385 	    DDI_DMA_DONTWAIT, NULL, &dma_p->dma_hdl);
1386 	if (err != DDI_SUCCESS)
1387 		return (DDI_FAILURE);
1388 
1389 	/*
1390 	 * Allocate memory
1391 	 */
1392 	err = ddi_dma_mem_alloc(dma_p->dma_hdl, memsize, attr_p,
1393 	    dma_flags, DDI_DMA_DONTWAIT, NULL, &va, &dma_p->alength,
1394 	    &dma_p->acc_hdl);
1395 	if (err != DDI_SUCCESS)
1396 		return (DDI_FAILURE);
1397 
1398 	/*
1399 	 * Bind the two together
1400 	 */
1401 	dma_p->mem_va = va;
1402 	err = ddi_dma_addr_bind_handle(dma_p->dma_hdl, NULL,
1403 	    va, dma_p->alength, dma_flags, DDI_DMA_DONTWAIT, NULL,
1404 	    &dma_p->cookie, &dma_p->ncookies);
1405 
1406 	BGE_DEBUG(("bge_alloc_dma_mem(): bind %d bytes; err %d, %d cookies",
1407 	    dma_p->alength, err, dma_p->ncookies));
1408 
1409 	if (err != DDI_DMA_MAPPED || dma_p->ncookies != 1)
1410 		return (DDI_FAILURE);
1411 
1412 	dma_p->nslots = ~0U;
1413 	dma_p->size = ~0U;
1414 	dma_p->token = ~0U;
1415 	dma_p->offset = 0;
1416 	return (DDI_SUCCESS);
1417 }
1418 
1419 /*
1420  * Free one allocated area of DMAable memory
1421  */
1422 static void
1423 bge_free_dma_mem(dma_area_t *dma_p)
1424 {
1425 	if (dma_p->dma_hdl != NULL) {
1426 		if (dma_p->ncookies) {
1427 			(void) ddi_dma_unbind_handle(dma_p->dma_hdl);
1428 			dma_p->ncookies = 0;
1429 		}
1430 		ddi_dma_free_handle(&dma_p->dma_hdl);
1431 		dma_p->dma_hdl = NULL;
1432 	}
1433 
1434 	if (dma_p->acc_hdl != NULL) {
1435 		ddi_dma_mem_free(&dma_p->acc_hdl);
1436 		dma_p->acc_hdl = NULL;
1437 	}
1438 }
1439 /*
1440  * Utility routine to carve a slice off a chunk of allocated memory,
1441  * updating the chunk descriptor accordingly.  The size of the slice
1442  * is given by the product of the <qty> and <size> parameters.
1443  */
1444 static void
1445 bge_slice_chunk(dma_area_t *slice, dma_area_t *chunk,
1446 	uint32_t qty, uint32_t size)
1447 {
1448 	static uint32_t sequence = 0xbcd5704a;
1449 	size_t totsize;
1450 
1451 	totsize = qty*size;
1452 	ASSERT(size >= 0);
1453 	ASSERT(totsize <= chunk->alength);
1454 
1455 	*slice = *chunk;
1456 	slice->nslots = qty;
1457 	slice->size = size;
1458 	slice->alength = totsize;
1459 	slice->token = ++sequence;
1460 
1461 	chunk->mem_va = (caddr_t)chunk->mem_va + totsize;
1462 	chunk->alength -= totsize;
1463 	chunk->offset += totsize;
1464 	chunk->cookie.dmac_laddress += totsize;
1465 	chunk->cookie.dmac_size -= totsize;
1466 }
1467 
1468 /*
1469  * Initialise the specified Receive Producer (Buffer) Ring, using
1470  * the information in the <dma_area> descriptors that it contains
1471  * to set up all the other fields. This routine should be called
1472  * only once for each ring.
1473  */
1474 static void
1475 bge_init_buff_ring(bge_t *bgep, uint64_t ring)
1476 {
1477 	buff_ring_t *brp;
1478 	bge_status_t *bsp;
1479 	sw_rbd_t *srbdp;
1480 	dma_area_t pbuf;
1481 	uint32_t bufsize;
1482 	uint32_t nslots;
1483 	uint32_t slot;
1484 	uint32_t split;
1485 
1486 	static bge_regno_t nic_ring_addrs[BGE_BUFF_RINGS_MAX] = {
1487 		NIC_MEM_SHADOW_BUFF_STD,
1488 		NIC_MEM_SHADOW_BUFF_JUMBO,
1489 		NIC_MEM_SHADOW_BUFF_MINI
1490 	};
1491 	static bge_regno_t mailbox_regs[BGE_BUFF_RINGS_MAX] = {
1492 		RECV_STD_PROD_INDEX_REG,
1493 		RECV_JUMBO_PROD_INDEX_REG,
1494 		RECV_MINI_PROD_INDEX_REG
1495 	};
1496 	static bge_regno_t buff_cons_xref[BGE_BUFF_RINGS_MAX] = {
1497 		STATUS_STD_BUFF_CONS_INDEX,
1498 		STATUS_JUMBO_BUFF_CONS_INDEX,
1499 		STATUS_MINI_BUFF_CONS_INDEX
1500 	};
1501 
1502 	BGE_TRACE(("bge_init_buff_ring($%p, %d)",
1503 	    (void *)bgep, ring));
1504 
1505 	brp = &bgep->buff[ring];
1506 	nslots = brp->desc.nslots;
1507 	ASSERT(brp->buf[0].nslots == nslots/BGE_SPLIT);
1508 	bufsize = brp->buf[0].size;
1509 
1510 	/*
1511 	 * Set up the copy of the h/w RCB
1512 	 *
1513 	 * Note: unlike Send & Receive Return Rings, (where the max_len
1514 	 * field holds the number of slots), in a Receive Buffer Ring
1515 	 * this field indicates the size of each buffer in the ring.
1516 	 */
1517 	brp->hw_rcb.host_ring_addr = brp->desc.cookie.dmac_laddress;
1518 	brp->hw_rcb.max_len = bufsize;
1519 	brp->hw_rcb.flags = nslots > 0 ? 0 : RCB_FLAG_RING_DISABLED;
1520 	brp->hw_rcb.nic_ring_addr = nic_ring_addrs[ring];
1521 
1522 	/*
1523 	 * Other one-off initialisation of per-ring data
1524 	 */
1525 	brp->bgep = bgep;
1526 	bsp = DMA_VPTR(bgep->status_block);
1527 	brp->cons_index_p = &bsp->buff_cons_index[buff_cons_xref[ring]];
1528 	brp->chip_mbx_reg = mailbox_regs[ring];
1529 	mutex_init(brp->rf_lock, NULL, MUTEX_DRIVER,
1530 	    DDI_INTR_PRI(bgep->intr_pri));
1531 
1532 	/*
1533 	 * Allocate the array of s/w Receive Buffer Descriptors
1534 	 */
1535 	srbdp = kmem_zalloc(nslots*sizeof (*srbdp), KM_SLEEP);
1536 	brp->sw_rbds = srbdp;
1537 
1538 	/*
1539 	 * Now initialise each array element once and for all
1540 	 */
1541 	for (split = 0; split < BGE_SPLIT; ++split) {
1542 		pbuf = brp->buf[split];
1543 		for (slot = 0; slot < nslots/BGE_SPLIT; ++srbdp, ++slot)
1544 			bge_slice_chunk(&srbdp->pbuf, &pbuf, 1, bufsize);
1545 		ASSERT(pbuf.alength == 0);
1546 	}
1547 }
1548 
1549 /*
1550  * Clean up initialisation done above before the memory is freed
1551  */
1552 static void
1553 bge_fini_buff_ring(bge_t *bgep, uint64_t ring)
1554 {
1555 	buff_ring_t *brp;
1556 	sw_rbd_t *srbdp;
1557 
1558 	BGE_TRACE(("bge_fini_buff_ring($%p, %d)",
1559 	    (void *)bgep, ring));
1560 
1561 	brp = &bgep->buff[ring];
1562 	srbdp = brp->sw_rbds;
1563 	kmem_free(srbdp, brp->desc.nslots*sizeof (*srbdp));
1564 
1565 	mutex_destroy(brp->rf_lock);
1566 }
1567 
1568 /*
1569  * Initialise the specified Receive (Return) Ring, using the
1570  * information in the <dma_area> descriptors that it contains
1571  * to set up all the other fields. This routine should be called
1572  * only once for each ring.
1573  */
1574 static void
1575 bge_init_recv_ring(bge_t *bgep, uint64_t ring)
1576 {
1577 	recv_ring_t *rrp;
1578 	bge_status_t *bsp;
1579 	uint32_t nslots;
1580 
1581 	BGE_TRACE(("bge_init_recv_ring($%p, %d)",
1582 	    (void *)bgep, ring));
1583 
1584 	/*
1585 	 * The chip architecture requires that receive return rings have
1586 	 * 512 or 1024 or 2048 elements per ring.  See 570X-PG108-R page 103.
1587 	 */
1588 	rrp = &bgep->recv[ring];
1589 	nslots = rrp->desc.nslots;
1590 	ASSERT(nslots == 0 || nslots == 512 ||
1591 	    nslots == 1024 || nslots == 2048);
1592 
1593 	/*
1594 	 * Set up the copy of the h/w RCB
1595 	 */
1596 	rrp->hw_rcb.host_ring_addr = rrp->desc.cookie.dmac_laddress;
1597 	rrp->hw_rcb.max_len = nslots;
1598 	rrp->hw_rcb.flags = nslots > 0 ? 0 : RCB_FLAG_RING_DISABLED;
1599 	rrp->hw_rcb.nic_ring_addr = 0;
1600 
1601 	/*
1602 	 * Other one-off initialisation of per-ring data
1603 	 */
1604 	rrp->bgep = bgep;
1605 	bsp = DMA_VPTR(bgep->status_block);
1606 	rrp->prod_index_p = RECV_INDEX_P(bsp, ring);
1607 	rrp->chip_mbx_reg = RECV_RING_CONS_INDEX_REG(ring);
1608 	mutex_init(rrp->rx_lock, NULL, MUTEX_DRIVER,
1609 	    DDI_INTR_PRI(bgep->intr_pri));
1610 }
1611 
1612 
1613 /*
1614  * Clean up initialisation done above before the memory is freed
1615  */
1616 static void
1617 bge_fini_recv_ring(bge_t *bgep, uint64_t ring)
1618 {
1619 	recv_ring_t *rrp;
1620 
1621 	BGE_TRACE(("bge_fini_recv_ring($%p, %d)",
1622 	    (void *)bgep, ring));
1623 
1624 	rrp = &bgep->recv[ring];
1625 	if (rrp->rx_softint)
1626 		ddi_remove_softintr(rrp->rx_softint);
1627 	mutex_destroy(rrp->rx_lock);
1628 }
1629 
1630 /*
1631  * Initialise the specified Send Ring, using the information in the
1632  * <dma_area> descriptors that it contains to set up all the other
1633  * fields. This routine should be called only once for each ring.
1634  */
1635 static void
1636 bge_init_send_ring(bge_t *bgep, uint64_t ring)
1637 {
1638 	send_ring_t *srp;
1639 	bge_status_t *bsp;
1640 	sw_sbd_t *ssbdp;
1641 	dma_area_t desc;
1642 	dma_area_t pbuf;
1643 	uint32_t nslots;
1644 	uint32_t slot;
1645 	uint32_t split;
1646 	sw_txbuf_t *txbuf;
1647 
1648 	BGE_TRACE(("bge_init_send_ring($%p, %d)",
1649 	    (void *)bgep, ring));
1650 
1651 	/*
1652 	 * The chip architecture requires that host-based send rings
1653 	 * have 512 elements per ring.  See 570X-PG102-R page 56.
1654 	 */
1655 	srp = &bgep->send[ring];
1656 	nslots = srp->desc.nslots;
1657 	ASSERT(nslots == 0 || nslots == 512);
1658 
1659 	/*
1660 	 * Set up the copy of the h/w RCB
1661 	 */
1662 	srp->hw_rcb.host_ring_addr = srp->desc.cookie.dmac_laddress;
1663 	srp->hw_rcb.max_len = nslots;
1664 	srp->hw_rcb.flags = nslots > 0 ? 0 : RCB_FLAG_RING_DISABLED;
1665 	srp->hw_rcb.nic_ring_addr = NIC_MEM_SHADOW_SEND_RING(ring, nslots);
1666 
1667 	/*
1668 	 * Other one-off initialisation of per-ring data
1669 	 */
1670 	srp->bgep = bgep;
1671 	bsp = DMA_VPTR(bgep->status_block);
1672 	srp->cons_index_p = SEND_INDEX_P(bsp, ring);
1673 	srp->chip_mbx_reg = SEND_RING_HOST_INDEX_REG(ring);
1674 	mutex_init(srp->tx_lock, NULL, MUTEX_DRIVER,
1675 	    DDI_INTR_PRI(bgep->intr_pri));
1676 	mutex_init(srp->txbuf_lock, NULL, MUTEX_DRIVER,
1677 	    DDI_INTR_PRI(bgep->intr_pri));
1678 	mutex_init(srp->freetxbuf_lock, NULL, MUTEX_DRIVER,
1679 	    DDI_INTR_PRI(bgep->intr_pri));
1680 	mutex_init(srp->tc_lock, NULL, MUTEX_DRIVER,
1681 	    DDI_INTR_PRI(bgep->intr_pri));
1682 	if (nslots == 0)
1683 		return;
1684 
1685 	/*
1686 	 * Allocate the array of s/w Send Buffer Descriptors
1687 	 */
1688 	ssbdp = kmem_zalloc(nslots*sizeof (*ssbdp), KM_SLEEP);
1689 	txbuf = kmem_zalloc(BGE_SEND_BUF_MAX*sizeof (*txbuf), KM_SLEEP);
1690 	srp->txbuf_head =
1691 	    kmem_zalloc(BGE_SEND_BUF_MAX*sizeof (bge_queue_item_t), KM_SLEEP);
1692 	srp->pktp = kmem_zalloc(BGE_SEND_BUF_MAX*sizeof (send_pkt_t), KM_SLEEP);
1693 	srp->sw_sbds = ssbdp;
1694 	srp->txbuf = txbuf;
1695 	srp->tx_buffers = BGE_SEND_BUF_NUM;
1696 	srp->tx_buffers_low = srp->tx_buffers / 4;
1697 	if (bgep->chipid.snd_buff_size > BGE_SEND_BUFF_SIZE_DEFAULT)
1698 		srp->tx_array_max = BGE_SEND_BUF_ARRAY_JUMBO;
1699 	else
1700 		srp->tx_array_max = BGE_SEND_BUF_ARRAY;
1701 	srp->tx_array = 1;
1702 
1703 	/*
1704 	 * Chunk tx desc area
1705 	 */
1706 	desc = srp->desc;
1707 	for (slot = 0; slot < nslots; ++ssbdp, ++slot) {
1708 		bge_slice_chunk(&ssbdp->desc, &desc, 1,
1709 		    sizeof (bge_sbd_t));
1710 	}
1711 	ASSERT(desc.alength == 0);
1712 
1713 	/*
1714 	 * Chunk tx buffer area
1715 	 */
1716 	for (split = 0; split < BGE_SPLIT; ++split) {
1717 		pbuf = srp->buf[0][split];
1718 		for (slot = 0; slot < BGE_SEND_BUF_NUM/BGE_SPLIT; ++slot) {
1719 			bge_slice_chunk(&txbuf->buf, &pbuf, 1,
1720 			    bgep->chipid.snd_buff_size);
1721 			txbuf++;
1722 		}
1723 		ASSERT(pbuf.alength == 0);
1724 	}
1725 }
1726 
1727 /*
1728  * Clean up initialisation done above before the memory is freed
1729  */
1730 static void
1731 bge_fini_send_ring(bge_t *bgep, uint64_t ring)
1732 {
1733 	send_ring_t *srp;
1734 	uint32_t array;
1735 	uint32_t split;
1736 	uint32_t nslots;
1737 
1738 	BGE_TRACE(("bge_fini_send_ring($%p, %d)",
1739 	    (void *)bgep, ring));
1740 
1741 	srp = &bgep->send[ring];
1742 	mutex_destroy(srp->tc_lock);
1743 	mutex_destroy(srp->freetxbuf_lock);
1744 	mutex_destroy(srp->txbuf_lock);
1745 	mutex_destroy(srp->tx_lock);
1746 	nslots = srp->desc.nslots;
1747 	if (nslots == 0)
1748 		return;
1749 
1750 	for (array = 1; array < srp->tx_array; ++array)
1751 		for (split = 0; split < BGE_SPLIT; ++split)
1752 			bge_free_dma_mem(&srp->buf[array][split]);
1753 	kmem_free(srp->sw_sbds, nslots*sizeof (*srp->sw_sbds));
1754 	kmem_free(srp->txbuf_head, BGE_SEND_BUF_MAX*sizeof (*srp->txbuf_head));
1755 	kmem_free(srp->txbuf, BGE_SEND_BUF_MAX*sizeof (*srp->txbuf));
1756 	kmem_free(srp->pktp, BGE_SEND_BUF_MAX*sizeof (*srp->pktp));
1757 	srp->sw_sbds = NULL;
1758 	srp->txbuf_head = NULL;
1759 	srp->txbuf = NULL;
1760 	srp->pktp = NULL;
1761 }
1762 
1763 /*
1764  * Initialise all transmit, receive, and buffer rings.
1765  */
1766 void
1767 bge_init_rings(bge_t *bgep)
1768 {
1769 	uint32_t ring;
1770 
1771 	BGE_TRACE(("bge_init_rings($%p)", (void *)bgep));
1772 
1773 	/*
1774 	 * Perform one-off initialisation of each ring ...
1775 	 */
1776 	for (ring = 0; ring < BGE_SEND_RINGS_MAX; ++ring)
1777 		bge_init_send_ring(bgep, ring);
1778 	for (ring = 0; ring < BGE_RECV_RINGS_MAX; ++ring)
1779 		bge_init_recv_ring(bgep, ring);
1780 	for (ring = 0; ring < BGE_BUFF_RINGS_MAX; ++ring)
1781 		bge_init_buff_ring(bgep, ring);
1782 }
1783 
1784 /*
1785  * Undo the work of bge_init_rings() above before the memory is freed
1786  */
1787 void
1788 bge_fini_rings(bge_t *bgep)
1789 {
1790 	uint32_t ring;
1791 
1792 	BGE_TRACE(("bge_fini_rings($%p)", (void *)bgep));
1793 
1794 	for (ring = 0; ring < BGE_BUFF_RINGS_MAX; ++ring)
1795 		bge_fini_buff_ring(bgep, ring);
1796 	for (ring = 0; ring < BGE_RECV_RINGS_MAX; ++ring)
1797 		bge_fini_recv_ring(bgep, ring);
1798 	for (ring = 0; ring < BGE_SEND_RINGS_MAX; ++ring)
1799 		bge_fini_send_ring(bgep, ring);
1800 }
1801 
1802 /*
1803  * Called from the bge_m_stop() to free the tx buffers which are
1804  * allocated from the tx process.
1805  */
1806 void
1807 bge_free_txbuf_arrays(send_ring_t *srp)
1808 {
1809 	uint32_t array;
1810 	uint32_t split;
1811 
1812 	ASSERT(mutex_owned(srp->tx_lock));
1813 
1814 	/*
1815 	 * Free the extra tx buffer DMA area
1816 	 */
1817 	for (array = 1; array < srp->tx_array; ++array)
1818 		for (split = 0; split < BGE_SPLIT; ++split)
1819 			bge_free_dma_mem(&srp->buf[array][split]);
1820 
1821 	/*
1822 	 * Restore initial tx buffer numbers
1823 	 */
1824 	srp->tx_array = 1;
1825 	srp->tx_buffers = BGE_SEND_BUF_NUM;
1826 	srp->tx_buffers_low = srp->tx_buffers / 4;
1827 	srp->tx_flow = 0;
1828 	bzero(srp->pktp, BGE_SEND_BUF_MAX * sizeof (*srp->pktp));
1829 }
1830 
1831 /*
1832  * Called from tx process to allocate more tx buffers
1833  */
1834 bge_queue_item_t *
1835 bge_alloc_txbuf_array(bge_t *bgep, send_ring_t *srp)
1836 {
1837 	bge_queue_t *txbuf_queue;
1838 	bge_queue_item_t *txbuf_item_last;
1839 	bge_queue_item_t *txbuf_item;
1840 	bge_queue_item_t *txbuf_item_rtn;
1841 	sw_txbuf_t *txbuf;
1842 	dma_area_t area;
1843 	size_t txbuffsize;
1844 	uint32_t slot;
1845 	uint32_t array;
1846 	uint32_t split;
1847 	uint32_t err;
1848 
1849 	ASSERT(mutex_owned(srp->tx_lock));
1850 
1851 	array = srp->tx_array;
1852 	if (array >= srp->tx_array_max)
1853 		return (NULL);
1854 
1855 	/*
1856 	 * Allocate memory & handles for TX buffers
1857 	 */
1858 	txbuffsize = BGE_SEND_BUF_NUM*bgep->chipid.snd_buff_size;
1859 	ASSERT((txbuffsize % BGE_SPLIT) == 0);
1860 	for (split = 0; split < BGE_SPLIT; ++split) {
1861 		err = bge_alloc_dma_mem(bgep, txbuffsize/BGE_SPLIT,
1862 		    &bge_data_accattr, DDI_DMA_WRITE | BGE_DMA_MODE,
1863 		    &srp->buf[array][split]);
1864 		if (err != DDI_SUCCESS) {
1865 			/* Free the last already allocated OK chunks */
1866 			for (slot = 0; slot <= split; ++slot)
1867 				bge_free_dma_mem(&srp->buf[array][slot]);
1868 			srp->tx_alloc_fail++;
1869 			return (NULL);
1870 		}
1871 	}
1872 
1873 	/*
1874 	 * Chunk tx buffer area
1875 	 */
1876 	txbuf = srp->txbuf + array*BGE_SEND_BUF_NUM;
1877 	for (split = 0; split < BGE_SPLIT; ++split) {
1878 		area = srp->buf[array][split];
1879 		for (slot = 0; slot < BGE_SEND_BUF_NUM/BGE_SPLIT; ++slot) {
1880 			bge_slice_chunk(&txbuf->buf, &area, 1,
1881 			    bgep->chipid.snd_buff_size);
1882 			txbuf++;
1883 		}
1884 	}
1885 
1886 	/*
1887 	 * Add above buffers to the tx buffer pop queue
1888 	 */
1889 	txbuf_item = srp->txbuf_head + array*BGE_SEND_BUF_NUM;
1890 	txbuf = srp->txbuf + array*BGE_SEND_BUF_NUM;
1891 	txbuf_item_last = NULL;
1892 	for (slot = 0; slot < BGE_SEND_BUF_NUM; ++slot) {
1893 		txbuf_item->item = txbuf;
1894 		txbuf_item->next = txbuf_item_last;
1895 		txbuf_item_last = txbuf_item;
1896 		txbuf++;
1897 		txbuf_item++;
1898 	}
1899 	txbuf_item = srp->txbuf_head + array*BGE_SEND_BUF_NUM;
1900 	txbuf_item_rtn = txbuf_item;
1901 	txbuf_item++;
1902 	txbuf_queue = srp->txbuf_pop_queue;
1903 	mutex_enter(txbuf_queue->lock);
1904 	txbuf_item->next = txbuf_queue->head;
1905 	txbuf_queue->head = txbuf_item_last;
1906 	txbuf_queue->count += BGE_SEND_BUF_NUM - 1;
1907 	mutex_exit(txbuf_queue->lock);
1908 
1909 	srp->tx_array++;
1910 	srp->tx_buffers += BGE_SEND_BUF_NUM;
1911 	srp->tx_buffers_low = srp->tx_buffers / 4;
1912 
1913 	return (txbuf_item_rtn);
1914 }
1915 
1916 /*
1917  * This function allocates all the transmit and receive buffers
1918  * and descriptors, in four chunks.
1919  */
1920 int
1921 bge_alloc_bufs(bge_t *bgep)
1922 {
1923 	dma_area_t area;
1924 	size_t rxbuffsize;
1925 	size_t txbuffsize;
1926 	size_t rxbuffdescsize;
1927 	size_t rxdescsize;
1928 	size_t txdescsize;
1929 	uint32_t ring;
1930 	uint32_t rx_rings = bgep->chipid.rx_rings;
1931 	uint32_t tx_rings = bgep->chipid.tx_rings;
1932 	int split;
1933 	int err;
1934 
1935 	BGE_TRACE(("bge_alloc_bufs($%p)",
1936 	    (void *)bgep));
1937 
1938 	rxbuffsize = BGE_STD_SLOTS_USED*bgep->chipid.std_buf_size;
1939 	rxbuffsize += bgep->chipid.jumbo_slots*bgep->chipid.recv_jumbo_size;
1940 	rxbuffsize += BGE_MINI_SLOTS_USED*BGE_MINI_BUFF_SIZE;
1941 
1942 	txbuffsize = BGE_SEND_BUF_NUM*bgep->chipid.snd_buff_size;
1943 	txbuffsize *= tx_rings;
1944 
1945 	rxdescsize = rx_rings*bgep->chipid.recv_slots;
1946 	rxdescsize *= sizeof (bge_rbd_t);
1947 
1948 	rxbuffdescsize = BGE_STD_SLOTS_USED;
1949 	rxbuffdescsize += bgep->chipid.jumbo_slots;
1950 	rxbuffdescsize += BGE_MINI_SLOTS_USED;
1951 	rxbuffdescsize *= sizeof (bge_rbd_t);
1952 
1953 	txdescsize = tx_rings*BGE_SEND_SLOTS_USED;
1954 	txdescsize *= sizeof (bge_sbd_t);
1955 	txdescsize += sizeof (bge_statistics_t);
1956 	txdescsize += sizeof (bge_status_t);
1957 	txdescsize += BGE_STATUS_PADDING;
1958 
1959 	/*
1960 	 * Enable PCI relaxed ordering only for RX/TX data buffers
1961 	 */
1962 	if (bge_relaxed_ordering)
1963 		dma_attr.dma_attr_flags |= DDI_DMA_RELAXED_ORDERING;
1964 
1965 	/*
1966 	 * Allocate memory & handles for RX buffers
1967 	 */
1968 	ASSERT((rxbuffsize % BGE_SPLIT) == 0);
1969 	for (split = 0; split < BGE_SPLIT; ++split) {
1970 		err = bge_alloc_dma_mem(bgep, rxbuffsize/BGE_SPLIT,
1971 		    &bge_data_accattr, DDI_DMA_READ | BGE_DMA_MODE,
1972 		    &bgep->rx_buff[split]);
1973 		if (err != DDI_SUCCESS)
1974 			return (DDI_FAILURE);
1975 	}
1976 
1977 	/*
1978 	 * Allocate memory & handles for TX buffers
1979 	 */
1980 	ASSERT((txbuffsize % BGE_SPLIT) == 0);
1981 	for (split = 0; split < BGE_SPLIT; ++split) {
1982 		err = bge_alloc_dma_mem(bgep, txbuffsize/BGE_SPLIT,
1983 		    &bge_data_accattr, DDI_DMA_WRITE | BGE_DMA_MODE,
1984 		    &bgep->tx_buff[split]);
1985 		if (err != DDI_SUCCESS)
1986 			return (DDI_FAILURE);
1987 	}
1988 
1989 	dma_attr.dma_attr_flags &= ~DDI_DMA_RELAXED_ORDERING;
1990 
1991 	/*
1992 	 * Allocate memory & handles for receive return rings
1993 	 */
1994 	ASSERT((rxdescsize % rx_rings) == 0);
1995 	for (split = 0; split < rx_rings; ++split) {
1996 		err = bge_alloc_dma_mem(bgep, rxdescsize/rx_rings,
1997 		    &bge_desc_accattr, DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
1998 		    &bgep->rx_desc[split]);
1999 		if (err != DDI_SUCCESS)
2000 			return (DDI_FAILURE);
2001 	}
2002 
2003 	/*
2004 	 * Allocate memory & handles for buffer (producer) descriptor rings
2005 	 */
2006 	err = bge_alloc_dma_mem(bgep, rxbuffdescsize, &bge_desc_accattr,
2007 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT, &bgep->rx_desc[split]);
2008 	if (err != DDI_SUCCESS)
2009 		return (DDI_FAILURE);
2010 
2011 	/*
2012 	 * Allocate memory & handles for TX descriptor rings,
2013 	 * status block, and statistics area
2014 	 */
2015 	err = bge_alloc_dma_mem(bgep, txdescsize, &bge_desc_accattr,
2016 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT, &bgep->tx_desc);
2017 	if (err != DDI_SUCCESS)
2018 		return (DDI_FAILURE);
2019 
2020 	/*
2021 	 * Now carve up each of the allocated areas ...
2022 	 */
2023 	for (split = 0; split < BGE_SPLIT; ++split) {
2024 		area = bgep->rx_buff[split];
2025 		bge_slice_chunk(&bgep->buff[BGE_STD_BUFF_RING].buf[split],
2026 		    &area, BGE_STD_SLOTS_USED/BGE_SPLIT,
2027 		    bgep->chipid.std_buf_size);
2028 		bge_slice_chunk(&bgep->buff[BGE_JUMBO_BUFF_RING].buf[split],
2029 		    &area, bgep->chipid.jumbo_slots/BGE_SPLIT,
2030 		    bgep->chipid.recv_jumbo_size);
2031 		bge_slice_chunk(&bgep->buff[BGE_MINI_BUFF_RING].buf[split],
2032 		    &area, BGE_MINI_SLOTS_USED/BGE_SPLIT,
2033 		    BGE_MINI_BUFF_SIZE);
2034 		ASSERT(area.alength >= 0);
2035 	}
2036 
2037 	for (split = 0; split < BGE_SPLIT; ++split) {
2038 		area = bgep->tx_buff[split];
2039 		for (ring = 0; ring < tx_rings; ++ring)
2040 			bge_slice_chunk(&bgep->send[ring].buf[0][split],
2041 			    &area, BGE_SEND_BUF_NUM/BGE_SPLIT,
2042 			    bgep->chipid.snd_buff_size);
2043 		for (; ring < BGE_SEND_RINGS_MAX; ++ring)
2044 			bge_slice_chunk(&bgep->send[ring].buf[0][split],
2045 			    &area, 0, bgep->chipid.snd_buff_size);
2046 		ASSERT(area.alength >= 0);
2047 	}
2048 
2049 	for (ring = 0; ring < rx_rings; ++ring)
2050 		bge_slice_chunk(&bgep->recv[ring].desc, &bgep->rx_desc[ring],
2051 		    bgep->chipid.recv_slots, sizeof (bge_rbd_t));
2052 
2053 	area = bgep->rx_desc[rx_rings];
2054 	for (; ring < BGE_RECV_RINGS_MAX; ++ring)
2055 		bge_slice_chunk(&bgep->recv[ring].desc, &area,
2056 		    0, sizeof (bge_rbd_t));
2057 	bge_slice_chunk(&bgep->buff[BGE_STD_BUFF_RING].desc, &area,
2058 	    BGE_STD_SLOTS_USED, sizeof (bge_rbd_t));
2059 	bge_slice_chunk(&bgep->buff[BGE_JUMBO_BUFF_RING].desc, &area,
2060 	    bgep->chipid.jumbo_slots, sizeof (bge_rbd_t));
2061 	bge_slice_chunk(&bgep->buff[BGE_MINI_BUFF_RING].desc, &area,
2062 	    BGE_MINI_SLOTS_USED, sizeof (bge_rbd_t));
2063 	ASSERT(area.alength == 0);
2064 
2065 	area = bgep->tx_desc;
2066 	for (ring = 0; ring < tx_rings; ++ring)
2067 		bge_slice_chunk(&bgep->send[ring].desc, &area,
2068 		    BGE_SEND_SLOTS_USED, sizeof (bge_sbd_t));
2069 	for (; ring < BGE_SEND_RINGS_MAX; ++ring)
2070 		bge_slice_chunk(&bgep->send[ring].desc, &area,
2071 		    0, sizeof (bge_sbd_t));
2072 	bge_slice_chunk(&bgep->statistics, &area, 1, sizeof (bge_statistics_t));
2073 	bge_slice_chunk(&bgep->status_block, &area, 1, sizeof (bge_status_t));
2074 	ASSERT(area.alength == BGE_STATUS_PADDING);
2075 	DMA_ZERO(bgep->status_block);
2076 
2077 	return (DDI_SUCCESS);
2078 }
2079 
2080 /*
2081  * This routine frees the transmit and receive buffers and descriptors.
2082  * Make sure the chip is stopped before calling it!
2083  */
2084 void
2085 bge_free_bufs(bge_t *bgep)
2086 {
2087 	int split;
2088 
2089 	BGE_TRACE(("bge_free_bufs($%p)",
2090 	    (void *)bgep));
2091 
2092 	bge_free_dma_mem(&bgep->tx_desc);
2093 	for (split = 0; split < BGE_RECV_RINGS_SPLIT; ++split)
2094 		bge_free_dma_mem(&bgep->rx_desc[split]);
2095 	for (split = 0; split < BGE_SPLIT; ++split)
2096 		bge_free_dma_mem(&bgep->tx_buff[split]);
2097 	for (split = 0; split < BGE_SPLIT; ++split)
2098 		bge_free_dma_mem(&bgep->rx_buff[split]);
2099 }
2100 
2101 /*
2102  * Determine (initial) MAC address ("BIA") to use for this interface
2103  */
2104 
2105 static void
2106 bge_find_mac_address(bge_t *bgep, chip_id_t *cidp)
2107 {
2108 	struct ether_addr sysaddr;
2109 	char propbuf[8];		/* "true" or "false", plus NUL	*/
2110 	uchar_t *bytes;
2111 	int *ints;
2112 	uint_t nelts;
2113 	int err;
2114 
2115 	BGE_TRACE(("bge_find_mac_address($%p)",
2116 	    (void *)bgep));
2117 
2118 	BGE_DEBUG(("bge_find_mac_address: hw_mac_addr %012llx, => %s (%sset)",
2119 	    cidp->hw_mac_addr,
2120 	    ether_sprintf((void *)cidp->vendor_addr.addr),
2121 	    cidp->vendor_addr.set ? "" : "not "));
2122 
2123 	/*
2124 	 * The "vendor's factory-set address" may already have
2125 	 * been extracted from the chip, but if the property
2126 	 * "local-mac-address" is set we use that instead.  It
2127 	 * will normally be set by OBP, but it could also be
2128 	 * specified in a .conf file(!)
2129 	 *
2130 	 * There doesn't seem to be a way to define byte-array
2131 	 * properties in a .conf, so we check whether it looks
2132 	 * like an array of 6 ints instead.
2133 	 *
2134 	 * Then, we check whether it looks like an array of 6
2135 	 * bytes (which it should, if OBP set it).  If we can't
2136 	 * make sense of it either way, we'll ignore it.
2137 	 */
2138 	err = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, bgep->devinfo,
2139 	    DDI_PROP_DONTPASS, localmac_propname, &ints, &nelts);
2140 	if (err == DDI_PROP_SUCCESS) {
2141 		if (nelts == ETHERADDRL) {
2142 			while (nelts--)
2143 				cidp->vendor_addr.addr[nelts] = ints[nelts];
2144 			cidp->vendor_addr.set = B_TRUE;
2145 		}
2146 		ddi_prop_free(ints);
2147 	}
2148 
2149 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, bgep->devinfo,
2150 	    DDI_PROP_DONTPASS, localmac_propname, &bytes, &nelts);
2151 	if (err == DDI_PROP_SUCCESS) {
2152 		if (nelts == ETHERADDRL) {
2153 			while (nelts--)
2154 				cidp->vendor_addr.addr[nelts] = bytes[nelts];
2155 			cidp->vendor_addr.set = B_TRUE;
2156 		}
2157 		ddi_prop_free(bytes);
2158 	}
2159 
2160 	BGE_DEBUG(("bge_find_mac_address: +local %s (%sset)",
2161 	    ether_sprintf((void *)cidp->vendor_addr.addr),
2162 	    cidp->vendor_addr.set ? "" : "not "));
2163 
2164 	/*
2165 	 * Look up the OBP property "local-mac-address?".  Note that even
2166 	 * though its value is a string (which should be "true" or "false"),
2167 	 * it can't be decoded by ddi_prop_lookup_string(9F).  So, we zero
2168 	 * the buffer first and then fetch the property as an untyped array;
2169 	 * this may or may not include a final NUL, but since there will
2170 	 * always be one left at the end of the buffer we can now treat it
2171 	 * as a string anyway.
2172 	 */
2173 	nelts = sizeof (propbuf);
2174 	bzero(propbuf, nelts--);
2175 	err = ddi_getlongprop_buf(DDI_DEV_T_ANY, bgep->devinfo,
2176 	    DDI_PROP_CANSLEEP, localmac_boolname, propbuf, (int *)&nelts);
2177 
2178 	/*
2179 	 * Now, if the address still isn't set from the hardware (SEEPROM)
2180 	 * or the OBP or .conf property, OR if the user has foolishly set
2181 	 * 'local-mac-address? = false', use "the system address" instead
2182 	 * (but only if it's non-null i.e. has been set from the IDPROM).
2183 	 */
2184 	if (cidp->vendor_addr.set == B_FALSE || strcmp(propbuf, "false") == 0)
2185 		if (localetheraddr(NULL, &sysaddr) != 0) {
2186 			ethaddr_copy(&sysaddr, cidp->vendor_addr.addr);
2187 			cidp->vendor_addr.set = B_TRUE;
2188 		}
2189 
2190 	BGE_DEBUG(("bge_find_mac_address: +system %s (%sset)",
2191 	    ether_sprintf((void *)cidp->vendor_addr.addr),
2192 	    cidp->vendor_addr.set ? "" : "not "));
2193 
2194 	/*
2195 	 * Finally(!), if there's a valid "mac-address" property (created
2196 	 * if we netbooted from this interface), we must use this instead
2197 	 * of any of the above to ensure that the NFS/install server doesn't
2198 	 * get confused by the address changing as Solaris takes over!
2199 	 */
2200 	err = ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, bgep->devinfo,
2201 	    DDI_PROP_DONTPASS, macaddr_propname, &bytes, &nelts);
2202 	if (err == DDI_PROP_SUCCESS) {
2203 		if (nelts == ETHERADDRL) {
2204 			while (nelts--)
2205 				cidp->vendor_addr.addr[nelts] = bytes[nelts];
2206 			cidp->vendor_addr.set = B_TRUE;
2207 		}
2208 		ddi_prop_free(bytes);
2209 	}
2210 
2211 	BGE_DEBUG(("bge_find_mac_address: =final %s (%sset)",
2212 	    ether_sprintf((void *)cidp->vendor_addr.addr),
2213 	    cidp->vendor_addr.set ? "" : "not "));
2214 }
2215 
2216 
2217 /*ARGSUSED*/
2218 int
2219 bge_check_acc_handle(bge_t *bgep, ddi_acc_handle_t handle)
2220 {
2221 	ddi_fm_error_t de;
2222 
2223 	ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
2224 	ddi_fm_acc_err_clear(handle, DDI_FME_VERSION);
2225 	return (de.fme_status);
2226 }
2227 
2228 /*ARGSUSED*/
2229 int
2230 bge_check_dma_handle(bge_t *bgep, ddi_dma_handle_t handle)
2231 {
2232 	ddi_fm_error_t de;
2233 
2234 	ASSERT(bgep->progress & PROGRESS_BUFS);
2235 	ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
2236 	return (de.fme_status);
2237 }
2238 
2239 /*
2240  * The IO fault service error handling callback function
2241  */
2242 /*ARGSUSED*/
2243 static int
2244 bge_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
2245 {
2246 	/*
2247 	 * as the driver can always deal with an error in any dma or
2248 	 * access handle, we can just return the fme_status value.
2249 	 */
2250 	pci_ereport_post(dip, err, NULL);
2251 	return (err->fme_status);
2252 }
2253 
2254 static void
2255 bge_fm_init(bge_t *bgep)
2256 {
2257 	ddi_iblock_cookie_t iblk;
2258 
2259 	/* Only register with IO Fault Services if we have some capability */
2260 	if (bgep->fm_capabilities) {
2261 		bge_reg_accattr.devacc_attr_access = DDI_FLAGERR_ACC;
2262 		bge_desc_accattr.devacc_attr_access = DDI_FLAGERR_ACC;
2263 		dma_attr.dma_attr_flags = DDI_DMA_FLAGERR;
2264 
2265 		/* Register capabilities with IO Fault Services */
2266 		ddi_fm_init(bgep->devinfo, &bgep->fm_capabilities, &iblk);
2267 
2268 		/*
2269 		 * Initialize pci ereport capabilities if ereport capable
2270 		 */
2271 		if (DDI_FM_EREPORT_CAP(bgep->fm_capabilities) ||
2272 		    DDI_FM_ERRCB_CAP(bgep->fm_capabilities))
2273 			pci_ereport_setup(bgep->devinfo);
2274 
2275 		/*
2276 		 * Register error callback if error callback capable
2277 		 */
2278 		if (DDI_FM_ERRCB_CAP(bgep->fm_capabilities))
2279 			ddi_fm_handler_register(bgep->devinfo,
2280 			    bge_fm_error_cb, (void*) bgep);
2281 	} else {
2282 		/*
2283 		 * These fields have to be cleared of FMA if there are no
2284 		 * FMA capabilities at runtime.
2285 		 */
2286 		bge_reg_accattr.devacc_attr_access = DDI_DEFAULT_ACC;
2287 		bge_desc_accattr.devacc_attr_access = DDI_DEFAULT_ACC;
2288 		dma_attr.dma_attr_flags = 0;
2289 	}
2290 }
2291 
2292 static void
2293 bge_fm_fini(bge_t *bgep)
2294 {
2295 	/* Only unregister FMA capabilities if we registered some */
2296 	if (bgep->fm_capabilities) {
2297 
2298 		/*
2299 		 * Release any resources allocated by pci_ereport_setup()
2300 		 */
2301 		if (DDI_FM_EREPORT_CAP(bgep->fm_capabilities) ||
2302 		    DDI_FM_ERRCB_CAP(bgep->fm_capabilities))
2303 			pci_ereport_teardown(bgep->devinfo);
2304 
2305 		/*
2306 		 * Un-register error callback if error callback capable
2307 		 */
2308 		if (DDI_FM_ERRCB_CAP(bgep->fm_capabilities))
2309 			ddi_fm_handler_unregister(bgep->devinfo);
2310 
2311 		/* Unregister from IO Fault Services */
2312 		ddi_fm_fini(bgep->devinfo);
2313 	}
2314 }
2315 
2316 static void
2317 #ifdef BGE_IPMI_ASF
2318 bge_unattach(bge_t *bgep, uint_t asf_mode)
2319 #else
2320 bge_unattach(bge_t *bgep)
2321 #endif
2322 {
2323 	BGE_TRACE(("bge_unattach($%p)",
2324 		(void *)bgep));
2325 
2326 	/*
2327 	 * Flag that no more activity may be initiated
2328 	 */
2329 	bgep->progress &= ~PROGRESS_READY;
2330 
2331 	/*
2332 	 * Quiesce the PHY and MAC (leave it reset but still powered).
2333 	 * Clean up and free all BGE data structures
2334 	 */
2335 	if (bgep->periodic_id != NULL) {
2336 		ddi_periodic_delete(bgep->periodic_id);
2337 		bgep->periodic_id = NULL;
2338 	}
2339 	if (bgep->progress & PROGRESS_KSTATS)
2340 		bge_fini_kstats(bgep);
2341 	if (bgep->progress & PROGRESS_NDD)
2342 		bge_nd_cleanup(bgep);
2343 	if (bgep->progress & PROGRESS_PHY)
2344 		bge_phys_reset(bgep);
2345 	if (bgep->progress & PROGRESS_HWINT) {
2346 		mutex_enter(bgep->genlock);
2347 #ifdef BGE_IPMI_ASF
2348 		if (bge_chip_reset(bgep, B_FALSE, asf_mode) != DDI_SUCCESS)
2349 #else
2350 		if (bge_chip_reset(bgep, B_FALSE) != DDI_SUCCESS)
2351 #endif
2352 			ddi_fm_service_impact(bgep->devinfo,
2353 			    DDI_SERVICE_UNAFFECTED);
2354 #ifdef BGE_IPMI_ASF
2355 		if (bgep->asf_enabled) {
2356 			/*
2357 			 * This register has been overlaid. We restore its
2358 			 * initial value here.
2359 			 */
2360 			bge_nic_put32(bgep, BGE_NIC_DATA_SIG_ADDR,
2361 			    BGE_NIC_DATA_SIG);
2362 		}
2363 #endif
2364 		if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK)
2365 			ddi_fm_service_impact(bgep->devinfo,
2366 			    DDI_SERVICE_UNAFFECTED);
2367 		if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
2368 			ddi_fm_service_impact(bgep->devinfo,
2369 			    DDI_SERVICE_UNAFFECTED);
2370 		mutex_exit(bgep->genlock);
2371 	}
2372 	if (bgep->progress & PROGRESS_INTR) {
2373 		bge_intr_disable(bgep);
2374 		bge_fini_rings(bgep);
2375 	}
2376 	if (bgep->progress & PROGRESS_HWINT) {
2377 		bge_rem_intrs(bgep);
2378 		rw_destroy(bgep->errlock);
2379 		mutex_destroy(bgep->softintrlock);
2380 		mutex_destroy(bgep->genlock);
2381 	}
2382 	if (bgep->progress & PROGRESS_FACTOTUM)
2383 		ddi_remove_softintr(bgep->factotum_id);
2384 	if (bgep->progress & PROGRESS_RESCHED)
2385 		ddi_remove_softintr(bgep->drain_id);
2386 	if (bgep->progress & PROGRESS_BUFS)
2387 		bge_free_bufs(bgep);
2388 	if (bgep->progress & PROGRESS_REGS)
2389 		ddi_regs_map_free(&bgep->io_handle);
2390 	if (bgep->progress & PROGRESS_CFG)
2391 		pci_config_teardown(&bgep->cfg_handle);
2392 
2393 	bge_fm_fini(bgep);
2394 
2395 	ddi_remove_minor_node(bgep->devinfo, NULL);
2396 	kmem_free(bgep->pstats, sizeof (bge_statistics_reg_t));
2397 	kmem_free(bgep->nd_params, PARAM_COUNT * sizeof (nd_param_t));
2398 	kmem_free(bgep, sizeof (*bgep));
2399 }
2400 
2401 static int
2402 bge_resume(dev_info_t *devinfo)
2403 {
2404 	bge_t *bgep;				/* Our private data	*/
2405 	chip_id_t *cidp;
2406 	chip_id_t chipid;
2407 
2408 	bgep = ddi_get_driver_private(devinfo);
2409 	if (bgep == NULL)
2410 		return (DDI_FAILURE);
2411 
2412 	/*
2413 	 * Refuse to resume if the data structures aren't consistent
2414 	 */
2415 	if (bgep->devinfo != devinfo)
2416 		return (DDI_FAILURE);
2417 
2418 #ifdef BGE_IPMI_ASF
2419 	/*
2420 	 * Power management hasn't been supported in BGE now. If you
2421 	 * want to implement it, please add the ASF/IPMI related
2422 	 * code here.
2423 	 */
2424 
2425 #endif
2426 
2427 	/*
2428 	 * Read chip ID & set up config space command register(s)
2429 	 * Refuse to resume if the chip has changed its identity!
2430 	 */
2431 	cidp = &bgep->chipid;
2432 	mutex_enter(bgep->genlock);
2433 	bge_chip_cfg_init(bgep, &chipid, B_FALSE);
2434 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
2435 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
2436 		mutex_exit(bgep->genlock);
2437 		return (DDI_FAILURE);
2438 	}
2439 	mutex_exit(bgep->genlock);
2440 	if (chipid.vendor != cidp->vendor)
2441 		return (DDI_FAILURE);
2442 	if (chipid.device != cidp->device)
2443 		return (DDI_FAILURE);
2444 	if (chipid.revision != cidp->revision)
2445 		return (DDI_FAILURE);
2446 	if (chipid.asic_rev != cidp->asic_rev)
2447 		return (DDI_FAILURE);
2448 
2449 	/*
2450 	 * All OK, reinitialise h/w & kick off GLD scheduling
2451 	 */
2452 	mutex_enter(bgep->genlock);
2453 	if (bge_restart(bgep, B_TRUE) != DDI_SUCCESS) {
2454 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
2455 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
2456 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
2457 		mutex_exit(bgep->genlock);
2458 		return (DDI_FAILURE);
2459 	}
2460 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
2461 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
2462 		mutex_exit(bgep->genlock);
2463 		return (DDI_FAILURE);
2464 	}
2465 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
2466 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
2467 		mutex_exit(bgep->genlock);
2468 		return (DDI_FAILURE);
2469 	}
2470 	mutex_exit(bgep->genlock);
2471 	return (DDI_SUCCESS);
2472 }
2473 
2474 /*
2475  * attach(9E) -- Attach a device to the system
2476  *
2477  * Called once for each board successfully probed.
2478  */
2479 static int
2480 bge_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
2481 {
2482 	bge_t *bgep;				/* Our private data	*/
2483 	mac_register_t *macp;
2484 	chip_id_t *cidp;
2485 	caddr_t regs;
2486 	int instance;
2487 	int err;
2488 	int intr_types;
2489 #ifdef BGE_IPMI_ASF
2490 	uint32_t mhcrValue;
2491 #ifdef __sparc
2492 	uint16_t value16;
2493 #endif
2494 #ifdef BGE_NETCONSOLE
2495 	int retval;
2496 #endif
2497 #endif
2498 
2499 	instance = ddi_get_instance(devinfo);
2500 
2501 	BGE_GTRACE(("bge_attach($%p, %d) instance %d",
2502 	    (void *)devinfo, cmd, instance));
2503 	BGE_BRKPT(NULL, "bge_attach");
2504 
2505 	switch (cmd) {
2506 	default:
2507 		return (DDI_FAILURE);
2508 
2509 	case DDI_RESUME:
2510 		return (bge_resume(devinfo));
2511 
2512 	case DDI_ATTACH:
2513 		break;
2514 	}
2515 
2516 	bgep = kmem_zalloc(sizeof (*bgep), KM_SLEEP);
2517 	bgep->pstats = kmem_zalloc(sizeof (bge_statistics_reg_t), KM_SLEEP);
2518 	bgep->nd_params =
2519 	    kmem_zalloc(PARAM_COUNT * sizeof (nd_param_t), KM_SLEEP);
2520 	ddi_set_driver_private(devinfo, bgep);
2521 	bgep->bge_guard = BGE_GUARD;
2522 	bgep->devinfo = devinfo;
2523 
2524 	/*
2525 	 * Initialize more fields in BGE private data
2526 	 */
2527 	bgep->debug = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
2528 	    DDI_PROP_DONTPASS, debug_propname, bge_debug);
2529 	(void) snprintf(bgep->ifname, sizeof (bgep->ifname), "%s%d",
2530 	    BGE_DRIVER_NAME, instance);
2531 
2532 	/*
2533 	 * Initialize for fma support
2534 	 */
2535 	bgep->fm_capabilities = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
2536 	    DDI_PROP_DONTPASS, fm_cap,
2537 	    DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
2538 	    DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
2539 	BGE_DEBUG(("bgep->fm_capabilities = %d", bgep->fm_capabilities));
2540 	bge_fm_init(bgep);
2541 
2542 	/*
2543 	 * Look up the IOMMU's page size for DVMA mappings (must be
2544 	 * a power of 2) and convert to a mask.  This can be used to
2545 	 * determine whether a message buffer crosses a page boundary.
2546 	 * Note: in 2s complement binary notation, if X is a power of
2547 	 * 2, then -X has the representation "11...1100...00".
2548 	 */
2549 	bgep->pagemask = dvma_pagesize(devinfo);
2550 	ASSERT(ddi_ffs(bgep->pagemask) == ddi_fls(bgep->pagemask));
2551 	bgep->pagemask = -bgep->pagemask;
2552 
2553 	/*
2554 	 * Map config space registers
2555 	 * Read chip ID & set up config space command register(s)
2556 	 *
2557 	 * Note: this leaves the chip accessible by Memory Space
2558 	 * accesses, but with interrupts and Bus Mastering off.
2559 	 * This should ensure that nothing untoward will happen
2560 	 * if it has been left active by the (net-)bootloader.
2561 	 * We'll re-enable Bus Mastering once we've reset the chip,
2562 	 * and allow interrupts only when everything else is set up.
2563 	 */
2564 	err = pci_config_setup(devinfo, &bgep->cfg_handle);
2565 #ifdef BGE_IPMI_ASF
2566 #ifdef __sparc
2567 	value16 = pci_config_get16(bgep->cfg_handle, PCI_CONF_COMM);
2568 	value16 = value16 | (PCI_COMM_MAE | PCI_COMM_ME);
2569 	pci_config_put16(bgep->cfg_handle, PCI_CONF_COMM, value16);
2570 	mhcrValue = MHCR_ENABLE_INDIRECT_ACCESS |
2571 	    MHCR_ENABLE_TAGGED_STATUS_MODE |
2572 	    MHCR_MASK_INTERRUPT_MODE |
2573 	    MHCR_MASK_PCI_INT_OUTPUT |
2574 	    MHCR_CLEAR_INTERRUPT_INTA |
2575 	    MHCR_ENABLE_ENDIAN_WORD_SWAP |
2576 	    MHCR_ENABLE_ENDIAN_BYTE_SWAP;
2577 	pci_config_put32(bgep->cfg_handle, PCI_CONF_BGE_MHCR, mhcrValue);
2578 	bge_ind_put32(bgep, MEMORY_ARBITER_MODE_REG,
2579 	    bge_ind_get32(bgep, MEMORY_ARBITER_MODE_REG) |
2580 	    MEMORY_ARBITER_ENABLE);
2581 #else
2582 	mhcrValue = pci_config_get32(bgep->cfg_handle, PCI_CONF_BGE_MHCR);
2583 #endif
2584 	if (mhcrValue & MHCR_ENABLE_ENDIAN_WORD_SWAP) {
2585 		bgep->asf_wordswapped = B_TRUE;
2586 	} else {
2587 		bgep->asf_wordswapped = B_FALSE;
2588 	}
2589 	bge_asf_get_config(bgep);
2590 #endif
2591 	if (err != DDI_SUCCESS) {
2592 		bge_problem(bgep, "pci_config_setup() failed");
2593 		goto attach_fail;
2594 	}
2595 	bgep->progress |= PROGRESS_CFG;
2596 	cidp = &bgep->chipid;
2597 	bzero(cidp, sizeof (*cidp));
2598 	bge_chip_cfg_init(bgep, cidp, B_FALSE);
2599 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
2600 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
2601 		goto attach_fail;
2602 	}
2603 
2604 #ifdef BGE_IPMI_ASF
2605 	if (DEVICE_5721_SERIES_CHIPSETS(bgep) ||
2606 	    DEVICE_5714_SERIES_CHIPSETS(bgep)) {
2607 		bgep->asf_newhandshake = B_TRUE;
2608 	} else {
2609 		bgep->asf_newhandshake = B_FALSE;
2610 	}
2611 #endif
2612 
2613 	/*
2614 	 * Update those parts of the chip ID derived from volatile
2615 	 * registers with the values seen by OBP (in case the chip
2616 	 * has been reset externally and therefore lost them).
2617 	 */
2618 	cidp->subven = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
2619 	    DDI_PROP_DONTPASS, subven_propname, cidp->subven);
2620 	cidp->subdev = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
2621 	    DDI_PROP_DONTPASS, subdev_propname, cidp->subdev);
2622 	cidp->clsize = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
2623 	    DDI_PROP_DONTPASS, clsize_propname, cidp->clsize);
2624 	cidp->latency = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
2625 	    DDI_PROP_DONTPASS, latency_propname, cidp->latency);
2626 	cidp->rx_rings = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
2627 	    DDI_PROP_DONTPASS, rxrings_propname, cidp->rx_rings);
2628 	cidp->tx_rings = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
2629 	    DDI_PROP_DONTPASS, txrings_propname, cidp->tx_rings);
2630 
2631 	if (bge_jumbo_enable == B_TRUE) {
2632 		cidp->default_mtu = ddi_prop_get_int(DDI_DEV_T_ANY, devinfo,
2633 		    DDI_PROP_DONTPASS, default_mtu, BGE_DEFAULT_MTU);
2634 		if ((cidp->default_mtu < BGE_DEFAULT_MTU)||
2635 		    (cidp->default_mtu > BGE_MAXIMUM_MTU)) {
2636 			cidp->default_mtu = BGE_DEFAULT_MTU;
2637 		}
2638 	}
2639 	/*
2640 	 * Map operating registers
2641 	 */
2642 	err = ddi_regs_map_setup(devinfo, BGE_PCI_OPREGS_RNUMBER,
2643 	    &regs, 0, 0, &bge_reg_accattr, &bgep->io_handle);
2644 	if (err != DDI_SUCCESS) {
2645 		bge_problem(bgep, "ddi_regs_map_setup() failed");
2646 		goto attach_fail;
2647 	}
2648 	bgep->io_regs = regs;
2649 	bgep->progress |= PROGRESS_REGS;
2650 
2651 	/*
2652 	 * Characterise the device, so we know its requirements.
2653 	 * Then allocate the appropriate TX and RX descriptors & buffers.
2654 	 */
2655 	if (bge_chip_id_init(bgep) == EIO) {
2656 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
2657 		goto attach_fail;
2658 	}
2659 	err = bge_alloc_bufs(bgep);
2660 	if (err != DDI_SUCCESS) {
2661 		bge_problem(bgep, "DMA buffer allocation failed");
2662 		goto attach_fail;
2663 	}
2664 	bgep->progress |= PROGRESS_BUFS;
2665 
2666 	/*
2667 	 * Add the softint handlers:
2668 	 *
2669 	 * Both of these handlers are used to avoid restrictions on the
2670 	 * context and/or mutexes required for some operations.  In
2671 	 * particular, the hardware interrupt handler and its subfunctions
2672 	 * can detect a number of conditions that we don't want to handle
2673 	 * in that context or with that set of mutexes held.  So, these
2674 	 * softints are triggered instead:
2675 	 *
2676 	 * the <resched> softint is triggered if we have previously
2677 	 * had to refuse to send a packet because of resource shortage
2678 	 * (we've run out of transmit buffers), but the send completion
2679 	 * interrupt handler has now detected that more buffers have
2680 	 * become available.
2681 	 *
2682 	 * the <factotum> is triggered if the h/w interrupt handler
2683 	 * sees the <link state changed> or <error> bits in the status
2684 	 * block.  It's also triggered periodically to poll the link
2685 	 * state, just in case we aren't getting link status change
2686 	 * interrupts ...
2687 	 */
2688 	err = ddi_add_softintr(devinfo, DDI_SOFTINT_LOW, &bgep->drain_id,
2689 	    NULL, NULL, bge_send_drain, (caddr_t)bgep);
2690 	if (err != DDI_SUCCESS) {
2691 		bge_problem(bgep, "ddi_add_softintr() failed");
2692 		goto attach_fail;
2693 	}
2694 	bgep->progress |= PROGRESS_RESCHED;
2695 	err = ddi_add_softintr(devinfo, DDI_SOFTINT_LOW, &bgep->factotum_id,
2696 	    NULL, NULL, bge_chip_factotum, (caddr_t)bgep);
2697 	if (err != DDI_SUCCESS) {
2698 		bge_problem(bgep, "ddi_add_softintr() failed");
2699 		goto attach_fail;
2700 	}
2701 	bgep->progress |= PROGRESS_FACTOTUM;
2702 
2703 	/* Get supported interrupt types */
2704 	if (ddi_intr_get_supported_types(devinfo, &intr_types) != DDI_SUCCESS) {
2705 		bge_error(bgep, "ddi_intr_get_supported_types failed\n");
2706 
2707 		goto attach_fail;
2708 	}
2709 
2710 	BGE_DEBUG(("%s: ddi_intr_get_supported_types() returned: %x",
2711 	    bgep->ifname, intr_types));
2712 
2713 	if ((intr_types & DDI_INTR_TYPE_MSI) && bgep->chipid.msi_enabled) {
2714 		if (bge_add_intrs(bgep, DDI_INTR_TYPE_MSI) != DDI_SUCCESS) {
2715 			bge_error(bgep, "MSI registration failed, "
2716 			    "trying FIXED interrupt type\n");
2717 		} else {
2718 			BGE_DEBUG(("%s: Using MSI interrupt type",
2719 			    bgep->ifname));
2720 			bgep->intr_type = DDI_INTR_TYPE_MSI;
2721 			bgep->progress |= PROGRESS_HWINT;
2722 		}
2723 	}
2724 
2725 	if (!(bgep->progress & PROGRESS_HWINT) &&
2726 	    (intr_types & DDI_INTR_TYPE_FIXED)) {
2727 		if (bge_add_intrs(bgep, DDI_INTR_TYPE_FIXED) != DDI_SUCCESS) {
2728 			bge_error(bgep, "FIXED interrupt "
2729 			    "registration failed\n");
2730 			goto attach_fail;
2731 		}
2732 
2733 		BGE_DEBUG(("%s: Using FIXED interrupt type", bgep->ifname));
2734 
2735 		bgep->intr_type = DDI_INTR_TYPE_FIXED;
2736 		bgep->progress |= PROGRESS_HWINT;
2737 	}
2738 
2739 	if (!(bgep->progress & PROGRESS_HWINT)) {
2740 		bge_error(bgep, "No interrupts registered\n");
2741 		goto attach_fail;
2742 	}
2743 
2744 	/*
2745 	 * Note that interrupts are not enabled yet as
2746 	 * mutex locks are not initialized. Initialize mutex locks.
2747 	 */
2748 	mutex_init(bgep->genlock, NULL, MUTEX_DRIVER,
2749 	    DDI_INTR_PRI(bgep->intr_pri));
2750 	mutex_init(bgep->softintrlock, NULL, MUTEX_DRIVER,
2751 	    DDI_INTR_PRI(bgep->intr_pri));
2752 	rw_init(bgep->errlock, NULL, RW_DRIVER,
2753 	    DDI_INTR_PRI(bgep->intr_pri));
2754 
2755 	/*
2756 	 * Initialize rings.
2757 	 */
2758 	bge_init_rings(bgep);
2759 
2760 	/*
2761 	 * Now that mutex locks are initialized, enable interrupts.
2762 	 */
2763 	bge_intr_enable(bgep);
2764 	bgep->progress |= PROGRESS_INTR;
2765 
2766 	/*
2767 	 * Initialise link state variables
2768 	 * Stop, reset & reinitialise the chip.
2769 	 * Initialise the (internal) PHY.
2770 	 */
2771 	bgep->link_state = LINK_STATE_UNKNOWN;
2772 
2773 	mutex_enter(bgep->genlock);
2774 
2775 	/*
2776 	 * Reset chip & rings to initial state; also reset address
2777 	 * filtering, promiscuity, loopback mode.
2778 	 */
2779 #ifdef BGE_IPMI_ASF
2780 #ifdef BGE_NETCONSOLE
2781 	if (bge_reset(bgep, ASF_MODE_INIT) != DDI_SUCCESS) {
2782 #else
2783 	if (bge_reset(bgep, ASF_MODE_SHUTDOWN) != DDI_SUCCESS) {
2784 #endif
2785 #else
2786 	if (bge_reset(bgep) != DDI_SUCCESS) {
2787 #endif
2788 		(void) bge_check_acc_handle(bgep, bgep->cfg_handle);
2789 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
2790 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
2791 		mutex_exit(bgep->genlock);
2792 		goto attach_fail;
2793 	}
2794 
2795 #ifdef BGE_IPMI_ASF
2796 	if (bgep->asf_enabled) {
2797 		bgep->asf_status = ASF_STAT_RUN_INIT;
2798 	}
2799 #endif
2800 
2801 	bzero(bgep->mcast_hash, sizeof (bgep->mcast_hash));
2802 	bzero(bgep->mcast_refs, sizeof (bgep->mcast_refs));
2803 	bgep->promisc = B_FALSE;
2804 	bgep->param_loop_mode = BGE_LOOP_NONE;
2805 	if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK) {
2806 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
2807 		mutex_exit(bgep->genlock);
2808 		goto attach_fail;
2809 	}
2810 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
2811 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
2812 		mutex_exit(bgep->genlock);
2813 		goto attach_fail;
2814 	}
2815 
2816 	mutex_exit(bgep->genlock);
2817 
2818 	if (bge_phys_init(bgep) == EIO) {
2819 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_LOST);
2820 		goto attach_fail;
2821 	}
2822 	bgep->progress |= PROGRESS_PHY;
2823 
2824 	/*
2825 	 * Register NDD-tweakable parameters
2826 	 */
2827 	if (bge_nd_init(bgep)) {
2828 		bge_problem(bgep, "bge_nd_init() failed");
2829 		goto attach_fail;
2830 	}
2831 	bgep->progress |= PROGRESS_NDD;
2832 
2833 	/*
2834 	 * Create & initialise named kstats
2835 	 */
2836 	bge_init_kstats(bgep, instance);
2837 	bgep->progress |= PROGRESS_KSTATS;
2838 
2839 	/*
2840 	 * Determine whether to override the chip's own MAC address
2841 	 */
2842 	bge_find_mac_address(bgep, cidp);
2843 	ethaddr_copy(cidp->vendor_addr.addr, bgep->curr_addr[0].addr);
2844 	bgep->curr_addr[0].set = B_TRUE;
2845 
2846 	bgep->unicst_addr_total = MAC_ADDRESS_REGS_MAX;
2847 	/*
2848 	 * Address available is one less than MAX
2849 	 * as primary address is not advertised
2850 	 * as a multiple MAC address.
2851 	 */
2852 	bgep->unicst_addr_avail = MAC_ADDRESS_REGS_MAX - 1;
2853 
2854 	if ((macp = mac_alloc(MAC_VERSION)) == NULL)
2855 		goto attach_fail;
2856 	macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
2857 	macp->m_driver = bgep;
2858 	macp->m_dip = devinfo;
2859 	macp->m_src_addr = bgep->curr_addr[0].addr;
2860 	macp->m_callbacks = &bge_m_callbacks;
2861 	macp->m_min_sdu = 0;
2862 	macp->m_max_sdu = cidp->ethmax_size - sizeof (struct ether_header);
2863 	/*
2864 	 * Finally, we're ready to register ourselves with the MAC layer
2865 	 * interface; if this succeeds, we're all ready to start()
2866 	 */
2867 	err = mac_register(macp, &bgep->mh);
2868 	mac_free(macp);
2869 	if (err != 0)
2870 		goto attach_fail;
2871 
2872 	/*
2873 	 * Register a periodical handler.
2874 	 * bge_chip_cyclic() is invoked in kernel context.
2875 	 */
2876 	bgep->periodic_id = ddi_periodic_add(bge_chip_cyclic, bgep,
2877 	    BGE_CYCLIC_PERIOD, DDI_IPL_0);
2878 
2879 	bgep->progress |= PROGRESS_READY;
2880 	ASSERT(bgep->bge_guard == BGE_GUARD);
2881 #ifdef BGE_IPMI_ASF
2882 #ifdef BGE_NETCONSOLE
2883 	if (bgep->asf_enabled) {
2884 		mutex_enter(bgep->genlock);
2885 		retval = bge_chip_start(bgep, B_TRUE);
2886 		mutex_exit(bgep->genlock);
2887 		if (retval != DDI_SUCCESS)
2888 			goto attach_fail;
2889 	}
2890 #endif
2891 #endif
2892 	return (DDI_SUCCESS);
2893 
2894 attach_fail:
2895 #ifdef BGE_IPMI_ASF
2896 	bge_unattach(bgep, ASF_MODE_SHUTDOWN);
2897 #else
2898 	bge_unattach(bgep);
2899 #endif
2900 	return (DDI_FAILURE);
2901 }
2902 
2903 /*
2904  *	bge_suspend() -- suspend transmit/receive for powerdown
2905  */
2906 static int
2907 bge_suspend(bge_t *bgep)
2908 {
2909 	/*
2910 	 * Stop processing and idle (powerdown) the PHY ...
2911 	 */
2912 	mutex_enter(bgep->genlock);
2913 #ifdef BGE_IPMI_ASF
2914 	/*
2915 	 * Power management hasn't been supported in BGE now. If you
2916 	 * want to implement it, please add the ASF/IPMI related
2917 	 * code here.
2918 	 */
2919 #endif
2920 	bge_stop(bgep);
2921 	if (bge_phys_idle(bgep) != DDI_SUCCESS) {
2922 		(void) bge_check_acc_handle(bgep, bgep->io_handle);
2923 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
2924 		mutex_exit(bgep->genlock);
2925 		return (DDI_FAILURE);
2926 	}
2927 	if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK) {
2928 		ddi_fm_service_impact(bgep->devinfo, DDI_SERVICE_DEGRADED);
2929 		mutex_exit(bgep->genlock);
2930 		return (DDI_FAILURE);
2931 	}
2932 	mutex_exit(bgep->genlock);
2933 
2934 	return (DDI_SUCCESS);
2935 }
2936 
2937 /*
2938  * detach(9E) -- Detach a device from the system
2939  */
2940 static int
2941 bge_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
2942 {
2943 	bge_t *bgep;
2944 #ifdef BGE_IPMI_ASF
2945 	uint_t asf_mode;
2946 	asf_mode = ASF_MODE_NONE;
2947 #endif
2948 
2949 	BGE_GTRACE(("bge_detach($%p, %d)", (void *)devinfo, cmd));
2950 
2951 	bgep = ddi_get_driver_private(devinfo);
2952 
2953 	switch (cmd) {
2954 	default:
2955 		return (DDI_FAILURE);
2956 
2957 	case DDI_SUSPEND:
2958 		return (bge_suspend(bgep));
2959 
2960 	case DDI_DETACH:
2961 		break;
2962 	}
2963 
2964 #ifdef BGE_IPMI_ASF
2965 	mutex_enter(bgep->genlock);
2966 	if (bgep->asf_enabled && ((bgep->asf_status == ASF_STAT_RUN) ||
2967 	    (bgep->asf_status == ASF_STAT_RUN_INIT))) {
2968 
2969 		bge_asf_update_status(bgep);
2970 		if (bgep->asf_status == ASF_STAT_RUN) {
2971 			bge_asf_stop_timer(bgep);
2972 		}
2973 		bgep->asf_status = ASF_STAT_STOP;
2974 
2975 		bge_asf_pre_reset_operations(bgep, BGE_SHUTDOWN_RESET);
2976 
2977 		if (bgep->asf_pseudostop) {
2978 			bge_chip_stop(bgep, B_FALSE);
2979 			bgep->bge_mac_state = BGE_MAC_STOPPED;
2980 			bgep->asf_pseudostop = B_FALSE;
2981 		}
2982 
2983 		asf_mode = ASF_MODE_POST_SHUTDOWN;
2984 
2985 		if (bge_check_acc_handle(bgep, bgep->cfg_handle) != DDI_FM_OK)
2986 			ddi_fm_service_impact(bgep->devinfo,
2987 			    DDI_SERVICE_UNAFFECTED);
2988 		if (bge_check_acc_handle(bgep, bgep->io_handle) != DDI_FM_OK)
2989 			ddi_fm_service_impact(bgep->devinfo,
2990 			    DDI_SERVICE_UNAFFECTED);
2991 	}
2992 	mutex_exit(bgep->genlock);
2993 #endif
2994 
2995 	/*
2996 	 * Unregister from the GLD subsystem.  This can fail, in
2997 	 * particular if there are DLPI style-2 streams still open -
2998 	 * in which case we just return failure without shutting
2999 	 * down chip operations.
3000 	 */
3001 	if (mac_unregister(bgep->mh) != 0)
3002 		return (DDI_FAILURE);
3003 
3004 	/*
3005 	 * All activity stopped, so we can clean up & exit
3006 	 */
3007 #ifdef BGE_IPMI_ASF
3008 	bge_unattach(bgep, asf_mode);
3009 #else
3010 	bge_unattach(bgep);
3011 #endif
3012 	return (DDI_SUCCESS);
3013 }
3014 
3015 
3016 /*
3017  * ========== Module Loading Data & Entry Points ==========
3018  */
3019 
3020 #undef	BGE_DBG
3021 #define	BGE_DBG		BGE_DBG_INIT	/* debug flag for this code	*/
3022 
3023 DDI_DEFINE_STREAM_OPS(bge_dev_ops, nulldev, nulldev, bge_attach, bge_detach,
3024     nodev, NULL, D_MP, NULL);
3025 
3026 static struct modldrv bge_modldrv = {
3027 	&mod_driverops,		/* Type of module.  This one is a driver */
3028 	bge_ident,		/* short description */
3029 	&bge_dev_ops		/* driver specific ops */
3030 };
3031 
3032 static struct modlinkage modlinkage = {
3033 	MODREV_1, (void *)&bge_modldrv, NULL
3034 };
3035 
3036 
3037 int
3038 _info(struct modinfo *modinfop)
3039 {
3040 	return (mod_info(&modlinkage, modinfop));
3041 }
3042 
3043 int
3044 _init(void)
3045 {
3046 	int status;
3047 
3048 	mac_init_ops(&bge_dev_ops, "bge");
3049 	status = mod_install(&modlinkage);
3050 	if (status == DDI_SUCCESS)
3051 		mutex_init(bge_log_mutex, NULL, MUTEX_DRIVER, NULL);
3052 	else
3053 		mac_fini_ops(&bge_dev_ops);
3054 	return (status);
3055 }
3056 
3057 int
3058 _fini(void)
3059 {
3060 	int status;
3061 
3062 	status = mod_remove(&modlinkage);
3063 	if (status == DDI_SUCCESS) {
3064 		mac_fini_ops(&bge_dev_ops);
3065 		mutex_destroy(bge_log_mutex);
3066 	}
3067 	return (status);
3068 }
3069 
3070 
3071 /*
3072  * bge_add_intrs:
3073  *
3074  * Register FIXED or MSI interrupts.
3075  */
3076 static int
3077 bge_add_intrs(bge_t *bgep, int	intr_type)
3078 {
3079 	dev_info_t	*dip = bgep->devinfo;
3080 	int		avail, actual, intr_size, count = 0;
3081 	int		i, flag, ret;
3082 
3083 	BGE_DEBUG(("bge_add_intrs($%p, 0x%x)", (void *)bgep, intr_type));
3084 
3085 	/* Get number of interrupts */
3086 	ret = ddi_intr_get_nintrs(dip, intr_type, &count);
3087 	if ((ret != DDI_SUCCESS) || (count == 0)) {
3088 		bge_error(bgep, "ddi_intr_get_nintrs() failure, ret: %d, "
3089 		    "count: %d", ret, count);
3090 
3091 		return (DDI_FAILURE);
3092 	}
3093 
3094 	/* Get number of available interrupts */
3095 	ret = ddi_intr_get_navail(dip, intr_type, &avail);
3096 	if ((ret != DDI_SUCCESS) || (avail == 0)) {
3097 		bge_error(bgep, "ddi_intr_get_navail() failure, "
3098 		    "ret: %d, avail: %d\n", ret, avail);
3099 
3100 		return (DDI_FAILURE);
3101 	}
3102 
3103 	if (avail < count) {
3104 		BGE_DEBUG(("%s: nintrs() returned %d, navail returned %d",
3105 		    bgep->ifname, count, avail));
3106 	}
3107 
3108 	/*
3109 	 * BGE hardware generates only single MSI even though it claims
3110 	 * to support multiple MSIs. So, hard code MSI count value to 1.
3111 	 */
3112 	if (intr_type == DDI_INTR_TYPE_MSI) {
3113 		count = 1;
3114 		flag = DDI_INTR_ALLOC_STRICT;
3115 	} else {
3116 		flag = DDI_INTR_ALLOC_NORMAL;
3117 	}
3118 
3119 	/* Allocate an array of interrupt handles */
3120 	intr_size = count * sizeof (ddi_intr_handle_t);
3121 	bgep->htable = kmem_alloc(intr_size, KM_SLEEP);
3122 
3123 	/* Call ddi_intr_alloc() */
3124 	ret = ddi_intr_alloc(dip, bgep->htable, intr_type, 0,
3125 	    count, &actual, flag);
3126 
3127 	if ((ret != DDI_SUCCESS) || (actual == 0)) {
3128 		bge_error(bgep, "ddi_intr_alloc() failed %d\n", ret);
3129 
3130 		kmem_free(bgep->htable, intr_size);
3131 		return (DDI_FAILURE);
3132 	}
3133 
3134 	if (actual < count) {
3135 		BGE_DEBUG(("%s: Requested: %d, Received: %d",
3136 		    bgep->ifname, count, actual));
3137 	}
3138 
3139 	bgep->intr_cnt = actual;
3140 
3141 	/*
3142 	 * Get priority for first msi, assume remaining are all the same
3143 	 */
3144 	if ((ret = ddi_intr_get_pri(bgep->htable[0], &bgep->intr_pri)) !=
3145 	    DDI_SUCCESS) {
3146 		bge_error(bgep, "ddi_intr_get_pri() failed %d\n", ret);
3147 
3148 		/* Free already allocated intr */
3149 		for (i = 0; i < actual; i++) {
3150 			(void) ddi_intr_free(bgep->htable[i]);
3151 		}
3152 
3153 		kmem_free(bgep->htable, intr_size);
3154 		return (DDI_FAILURE);
3155 	}
3156 
3157 	/* Call ddi_intr_add_handler() */
3158 	for (i = 0; i < actual; i++) {
3159 		if ((ret = ddi_intr_add_handler(bgep->htable[i], bge_intr,
3160 		    (caddr_t)bgep, (caddr_t)(uintptr_t)i)) != DDI_SUCCESS) {
3161 			bge_error(bgep, "ddi_intr_add_handler() "
3162 			    "failed %d\n", ret);
3163 
3164 			/* Free already allocated intr */
3165 			for (i = 0; i < actual; i++) {
3166 				(void) ddi_intr_free(bgep->htable[i]);
3167 			}
3168 
3169 			kmem_free(bgep->htable, intr_size);
3170 			return (DDI_FAILURE);
3171 		}
3172 	}
3173 
3174 	if ((ret = ddi_intr_get_cap(bgep->htable[0], &bgep->intr_cap))
3175 	    != DDI_SUCCESS) {
3176 		bge_error(bgep, "ddi_intr_get_cap() failed %d\n", ret);
3177 
3178 		for (i = 0; i < actual; i++) {
3179 			(void) ddi_intr_remove_handler(bgep->htable[i]);
3180 			(void) ddi_intr_free(bgep->htable[i]);
3181 		}
3182 
3183 		kmem_free(bgep->htable, intr_size);
3184 		return (DDI_FAILURE);
3185 	}
3186 
3187 	return (DDI_SUCCESS);
3188 }
3189 
3190 /*
3191  * bge_rem_intrs:
3192  *
3193  * Unregister FIXED or MSI interrupts
3194  */
3195 static void
3196 bge_rem_intrs(bge_t *bgep)
3197 {
3198 	int	i;
3199 
3200 	BGE_DEBUG(("bge_rem_intrs($%p)", (void *)bgep));
3201 
3202 	/* Call ddi_intr_remove_handler() */
3203 	for (i = 0; i < bgep->intr_cnt; i++) {
3204 		(void) ddi_intr_remove_handler(bgep->htable[i]);
3205 		(void) ddi_intr_free(bgep->htable[i]);
3206 	}
3207 
3208 	kmem_free(bgep->htable, bgep->intr_cnt * sizeof (ddi_intr_handle_t));
3209 }
3210 
3211 
3212 void
3213 bge_intr_enable(bge_t *bgep)
3214 {
3215 	int i;
3216 
3217 	if (bgep->intr_cap & DDI_INTR_FLAG_BLOCK) {
3218 		/* Call ddi_intr_block_enable() for MSI interrupts */
3219 		(void) ddi_intr_block_enable(bgep->htable, bgep->intr_cnt);
3220 	} else {
3221 		/* Call ddi_intr_enable for MSI or FIXED interrupts */
3222 		for (i = 0; i < bgep->intr_cnt; i++) {
3223 			(void) ddi_intr_enable(bgep->htable[i]);
3224 		}
3225 	}
3226 }
3227 
3228 
3229 void
3230 bge_intr_disable(bge_t *bgep)
3231 {
3232 	int i;
3233 
3234 	if (bgep->intr_cap & DDI_INTR_FLAG_BLOCK) {
3235 		/* Call ddi_intr_block_disable() */
3236 		(void) ddi_intr_block_disable(bgep->htable, bgep->intr_cnt);
3237 	} else {
3238 		for (i = 0; i < bgep->intr_cnt; i++) {
3239 			(void) ddi_intr_disable(bgep->htable[i]);
3240 		}
3241 	}
3242 }
3243