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