xref: /titanic_52/usr/src/uts/common/io/hxge/hxge_hw.c (revision d656abb5804319b33c85955a73ee450ef7ff9739)
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  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <hxge_impl.h>
27 
28 lb_property_t lb_normal = {normal, "normal", hxge_lb_normal};
29 lb_property_t lb_mac10g = {internal, "mac10g", hxge_lb_mac10g};
30 
31 uint32_t hxge_lb_dbg = 1;
32 
33 extern uint32_t hxge_jumbo_frame_size;
34 
35 static void hxge_rtrace_ioctl(p_hxge_t, queue_t *, mblk_t *, struct iocblk *);
36 
37 void
38 hxge_global_reset(p_hxge_t hxgep)
39 {
40 	HXGE_DEBUG_MSG((hxgep, DDI_CTL, "==> hxge_global_reset"));
41 
42 	(void) hxge_intr_hw_disable(hxgep);
43 
44 	if (hxgep->suspended)
45 		(void) hxge_link_init(hxgep);
46 
47 	(void) hxge_vmac_init(hxgep);
48 
49 	(void) hxge_intr_hw_enable(hxgep);
50 
51 	HXGE_DEBUG_MSG((hxgep, DDI_CTL, "<== hxge_global_reset"));
52 }
53 
54 
55 void
56 hxge_hw_id_init(p_hxge_t hxgep)
57 {
58 	HXGE_DEBUG_MSG((hxgep, DDI_CTL, "==> hxge_hw_id_init"));
59 
60 	/*
61 	 * Initialize the frame size to either standard "1500 + 38" or
62 	 * jumbo. The user may tune the frame size through the "mtu" parameter
63 	 * using "dladm set-linkprop"
64 	 */
65 	hxgep->vmac.minframesize = MIN_FRAME_SIZE;
66 	hxgep->vmac.maxframesize = HXGE_DEFAULT_MTU + MTU_TO_FRAME_SIZE;
67 	if (hxgep->param_arr[param_accept_jumbo].value)
68 		hxgep->vmac.maxframesize = (uint16_t)hxge_jumbo_frame_size;
69 
70 	HXGE_DEBUG_MSG((hxgep, DDI_CTL, "==> hxge_hw_id_init: maxframesize %d",
71 	    hxgep->vmac.maxframesize));
72 	HXGE_DEBUG_MSG((hxgep, DDI_CTL, "<== hxge_hw_id_init"));
73 }
74 
75 void
76 hxge_hw_init_niu_common(p_hxge_t hxgep)
77 {
78 	p_hxge_hw_list_t hw_p;
79 
80 	HXGE_DEBUG_MSG((hxgep, DDI_CTL, "==> hxge_hw_init_niu_common"));
81 
82 	if ((hw_p = hxgep->hxge_hw_p) == NULL) {
83 		return;
84 	}
85 
86 	MUTEX_ENTER(&hw_p->hxge_cfg_lock);
87 	if (hw_p->flags & COMMON_INIT_DONE) {
88 		HXGE_DEBUG_MSG((hxgep, MOD_CTL, "hxge_hw_init_niu_common"
89 		    " already done for dip $%p exiting", hw_p->parent_devp));
90 		MUTEX_EXIT(&hw_p->hxge_cfg_lock);
91 		return;
92 	}
93 
94 	hw_p->flags = COMMON_INIT_START;
95 	HXGE_DEBUG_MSG((hxgep, MOD_CTL,
96 	    "hxge_hw_init_niu_common Started for device id %x",
97 	    hw_p->parent_devp));
98 
99 	(void) hxge_pfc_hw_reset(hxgep);
100 	hw_p->flags = COMMON_INIT_DONE;
101 	MUTEX_EXIT(&hw_p->hxge_cfg_lock);
102 
103 	HXGE_DEBUG_MSG((hxgep, MOD_CTL,
104 	    "hxge_hw_init_niu_common Done for device id %x",
105 	    hw_p->parent_devp));
106 	HXGE_DEBUG_MSG((hxgep, DDI_CTL, "<== hxge_hw_init_niu_common"));
107 }
108 
109 uint_t
110 hxge_intr(caddr_t arg1, caddr_t arg2)
111 {
112 	p_hxge_ldv_t		ldvp = (p_hxge_ldv_t)arg1;
113 	p_hxge_t		hxgep = (p_hxge_t)arg2;
114 	uint_t			serviced = DDI_INTR_UNCLAIMED;
115 	uint8_t			ldv;
116 	hpi_handle_t		handle;
117 	p_hxge_ldgv_t		ldgvp;
118 	p_hxge_ldg_t		ldgp, t_ldgp;
119 	p_hxge_ldv_t		t_ldvp;
120 	uint32_t		vector0 = 0, vector1 = 0;
121 	int			i, j, nldvs, nintrs = 1;
122 	hpi_status_t		rs = HPI_SUCCESS;
123 
124 	/*
125 	 * DDI interface returns second arg as NULL
126 	 */
127 	if ((arg2 == NULL) || ((void *) ldvp->hxgep != arg2)) {
128 		hxgep = ldvp->hxgep;
129 	}
130 
131 	HXGE_DEBUG_MSG((hxgep, INT_CTL, "==> hxge_intr"));
132 
133 	if (!(hxgep->drv_state & STATE_HW_INITIALIZED)) {
134 		HXGE_ERROR_MSG((hxgep, INT_CTL,
135 		    "<== hxge_intr: not initialized 0x%x", serviced));
136 		return (serviced);
137 	}
138 
139 	ldgvp = hxgep->ldgvp;
140 
141 	HXGE_DEBUG_MSG((hxgep, INT_CTL, "==> hxge_intr: ldgvp $%p", ldgvp));
142 
143 	if (ldvp == NULL && ldgvp) {
144 		t_ldvp = ldvp = ldgvp->ldvp;
145 	}
146 
147 	if (ldvp) {
148 		ldgp = t_ldgp = ldvp->ldgp;
149 	}
150 
151 	HXGE_DEBUG_MSG((hxgep, INT_CTL, "==> hxge_intr: "
152 	    "ldgvp $%p ldvp $%p ldgp $%p", ldgvp, ldvp, ldgp));
153 
154 	if (ldgvp == NULL || ldvp == NULL || ldgp == NULL) {
155 		HXGE_ERROR_MSG((hxgep, INT_CTL, "==> hxge_intr: "
156 		    "ldgvp $%p ldvp $%p ldgp $%p", ldgvp, ldvp, ldgp));
157 		HXGE_ERROR_MSG((hxgep, INT_CTL, "<== hxge_intr: not ready"));
158 		return (DDI_INTR_UNCLAIMED);
159 	}
160 
161 	/*
162 	 * This interrupt handler will have to go through
163 	 * all the logical devices to find out which
164 	 * logical device interrupts us and then call
165 	 * its handler to process the events.
166 	 */
167 	handle = HXGE_DEV_HPI_HANDLE(hxgep);
168 	t_ldgp = ldgp;
169 	t_ldvp = ldgp->ldvp;
170 
171 	nldvs = ldgp->nldvs;
172 
173 	HXGE_DEBUG_MSG((hxgep, INT_CTL, "==> hxge_intr: #ldvs %d #intrs %d",
174 	    nldvs, ldgvp->ldg_intrs));
175 
176 	serviced = DDI_INTR_CLAIMED;
177 	for (i = 0; i < nintrs; i++, t_ldgp++) {
178 		HXGE_DEBUG_MSG((hxgep, INT_CTL, "==> hxge_intr(%d): #ldvs %d "
179 		    " #intrs %d", i, nldvs, nintrs));
180 
181 		/* Get this group's flag bits. */
182 		t_ldgp->interrupted = B_FALSE;
183 		rs = hpi_ldsv_ldfs_get(handle, t_ldgp->ldg, &vector0, &vector1);
184 		if (rs) {
185 			continue;
186 		}
187 
188 		if (!vector0 && !vector1) {
189 			HXGE_DEBUG_MSG((hxgep, INT_CTL, "==> hxge_intr: "
190 			    "no interrupts on group %d", t_ldgp->ldg));
191 			continue;
192 		}
193 
194 		HXGE_DEBUG_MSG((hxgep, INT_CTL, "==> hxge_intr: "
195 		    "vector0 0x%llx vector1 0x%llx", vector0, vector1));
196 
197 		t_ldgp->interrupted = B_TRUE;
198 		nldvs = t_ldgp->nldvs;
199 
200 		for (j = 0; j < nldvs; j++, t_ldvp++) {
201 			/*
202 			 * Call device's handler if flag bits are on.
203 			 */
204 			ldv = t_ldvp->ldv;
205 			if ((LDV_ON(ldv, vector0) | (LDV_ON(ldv, vector1)))) {
206 				HXGE_DEBUG_MSG((hxgep, INT_CTL,
207 				    "==> hxge_intr: calling device %d"
208 				    " #ldvs %d #intrs %d", j, nldvs, nintrs));
209 				(void) (t_ldvp->ldv_intr_handler)(
210 				    (caddr_t)t_ldvp, arg2);
211 			}
212 		}
213 	}
214 
215 	t_ldgp = ldgp;
216 	for (i = 0; i < nintrs; i++, t_ldgp++) {
217 		/* rearm group interrupts */
218 		if (t_ldgp->interrupted) {
219 			HXGE_DEBUG_MSG((hxgep, INT_CTL,
220 			    "==> hxge_intr: arm group %d", t_ldgp->ldg));
221 			(void) hpi_intr_ldg_mgmt_set(handle, t_ldgp->ldg,
222 			    t_ldgp->arm, t_ldgp->ldg_timer);
223 		}
224 	}
225 
226 	HXGE_DEBUG_MSG((hxgep, INT_CTL, "<== hxge_intr: serviced 0x%x",
227 	    serviced));
228 	return (serviced);
229 }
230 
231 hxge_status_t
232 hxge_peu_handle_sys_errors(p_hxge_t hxgep)
233 {
234 	hpi_handle_t		handle;
235 	p_hxge_peu_sys_stats_t	statsp;
236 	peu_intr_stat_t		stat;
237 
238 	handle = hxgep->hpi_handle;
239 	statsp = (p_hxge_peu_sys_stats_t)&hxgep->statsp->peu_sys_stats;
240 
241 	HXGE_REG_RD32(handle, PEU_INTR_STAT, &stat.value);
242 
243 	/*
244 	 * The PCIE errors are unrecoverrable and cannot be cleared.
245 	 * The only thing we can do here is to mask them off to prevent
246 	 * continued interrupts.
247 	 */
248 	HXGE_REG_WR32(handle, PEU_INTR_MASK, 0xffffffff);
249 
250 	if (stat.bits.spc_acc_err) {
251 		statsp->spc_acc_err++;
252 		HXGE_ERROR_MSG((hxgep, HXGE_ERR_CTL,
253 		    "==> hxge_peu_handle_sys_errors: spc_acc_err"));
254 	}
255 
256 	if (stat.bits.tdc_pioacc_err) {
257 		statsp->tdc_pioacc_err++;
258 		HXGE_ERROR_MSG((hxgep, HXGE_ERR_CTL,
259 		    "==> hxge_peu_handle_sys_errors: tdc_pioacc_err"));
260 	}
261 
262 	if (stat.bits.rdc_pioacc_err) {
263 		statsp->rdc_pioacc_err++;
264 		HXGE_ERROR_MSG((hxgep, HXGE_ERR_CTL,
265 		    "==> hxge_peu_handle_sys_errors: rdc_pioacc_err"));
266 	}
267 
268 	if (stat.bits.pfc_pioacc_err) {
269 		statsp->pfc_pioacc_err++;
270 		HXGE_ERROR_MSG((hxgep, HXGE_ERR_CTL,
271 		    "==> hxge_peu_handle_sys_errors: pfc_pioacc_err"));
272 	}
273 
274 	if (stat.bits.vmac_pioacc_err) {
275 		statsp->vmac_pioacc_err++;
276 		HXGE_ERROR_MSG((hxgep, HXGE_ERR_CTL,
277 		    "==> hxge_peu_handle_sys_errors: vmac_pioacc_err"));
278 	}
279 
280 	if (stat.bits.cpl_hdrq_parerr) {
281 		statsp->cpl_hdrq_parerr++;
282 		HXGE_ERROR_MSG((hxgep, HXGE_ERR_CTL,
283 		    "==> hxge_peu_handle_sys_errors: cpl_hdrq_parerr"));
284 	}
285 
286 	if (stat.bits.cpl_dataq_parerr) {
287 		statsp->cpl_dataq_parerr++;
288 		HXGE_ERROR_MSG((hxgep, HXGE_ERR_CTL,
289 		    "==> hxge_peu_handle_sys_errors: cpl_dataq_parerr"));
290 	}
291 
292 	if (stat.bits.retryram_xdlh_parerr) {
293 		statsp->retryram_xdlh_parerr++;
294 		HXGE_ERROR_MSG((hxgep, HXGE_ERR_CTL,
295 		    "==> hxge_peu_handle_sys_errors: retryram_xdlh_parerr"));
296 	}
297 
298 	if (stat.bits.retrysotram_xdlh_parerr) {
299 		statsp->retrysotram_xdlh_parerr++;
300 		HXGE_ERROR_MSG((hxgep, HXGE_ERR_CTL,
301 		    "==> hxge_peu_handle_sys_errors: retrysotram_xdlh_parerr"));
302 	}
303 
304 	if (stat.bits.p_hdrq_parerr) {
305 		statsp->p_hdrq_parerr++;
306 		HXGE_ERROR_MSG((hxgep, HXGE_ERR_CTL,
307 		    "==> hxge_peu_handle_sys_errors: p_hdrq_parerr"));
308 	}
309 
310 	if (stat.bits.p_dataq_parerr) {
311 		statsp->p_dataq_parerr++;
312 		HXGE_ERROR_MSG((hxgep, HXGE_ERR_CTL,
313 		    "==> hxge_peu_handle_sys_errors: p_dataq_parerr"));
314 	}
315 
316 	if (stat.bits.np_hdrq_parerr) {
317 		statsp->np_hdrq_parerr++;
318 		HXGE_ERROR_MSG((hxgep, HXGE_ERR_CTL,
319 		    "==> hxge_peu_handle_sys_errors: np_hdrq_parerr"));
320 	}
321 
322 	if (stat.bits.np_dataq_parerr) {
323 		statsp->np_dataq_parerr++;
324 		HXGE_ERROR_MSG((hxgep, HXGE_ERR_CTL,
325 		    "==> hxge_peu_handle_sys_errors: np_dataq_parerr"));
326 	}
327 
328 	if (stat.bits.eic_msix_parerr) {
329 		statsp->eic_msix_parerr++;
330 		HXGE_ERROR_MSG((hxgep, HXGE_ERR_CTL,
331 		    "==> hxge_peu_handle_sys_errors: eic_msix_parerr"));
332 	}
333 
334 	if (stat.bits.hcr_parerr) {
335 		statsp->hcr_parerr++;
336 		HXGE_ERROR_MSG((hxgep, HXGE_ERR_CTL,
337 		    "==> hxge_peu_handle_sys_errors: hcr_parerr"));
338 	}
339 
340 	HXGE_FM_REPORT_ERROR(hxgep, NULL, HXGE_FM_EREPORT_PEU_ERR);
341 
342 	return (HXGE_OK);
343 }
344 
345 /*ARGSUSED*/
346 uint_t
347 hxge_syserr_intr(caddr_t arg1, caddr_t arg2)
348 {
349 	p_hxge_ldv_t	ldvp = (p_hxge_ldv_t)arg1;
350 	p_hxge_t	hxgep = (p_hxge_t)arg2;
351 	p_hxge_ldg_t	ldgp = NULL;
352 	hpi_handle_t	handle;
353 	dev_err_stat_t	estat;
354 	uint_t		serviced = DDI_INTR_UNCLAIMED;
355 
356 	if ((arg1 == NULL) && (arg2 == NULL)) {
357 		return (serviced);
358 	}
359 
360 	if ((arg2 == NULL) ||
361 	    ((ldvp != NULL) && ((void *)ldvp->hxgep != arg2))) {
362 		if (ldvp != NULL) {
363 			hxgep = ldvp->hxgep;
364 		}
365 	}
366 
367 	HXGE_DEBUG_MSG((hxgep, SYSERR_CTL,
368 	    "==> hxge_syserr_intr: arg2 $%p arg1 $%p", hxgep, ldvp));
369 
370 	if (ldvp != NULL && ldvp->use_timer == B_FALSE) {
371 		ldgp = ldvp->ldgp;
372 		if (ldgp == NULL) {
373 			HXGE_ERROR_MSG((hxgep, SYSERR_CTL,
374 			    "<== hxge_syserrintr(no logical group): "
375 			    "arg2 $%p arg1 $%p", hxgep, ldvp));
376 			return (DDI_INTR_UNCLAIMED);
377 		}
378 		/*
379 		 * Get the logical device state if the function uses interrupt.
380 		 */
381 	}
382 
383 	/* This interrupt handler is for system error interrupts.  */
384 	handle = HXGE_DEV_HPI_HANDLE(hxgep);
385 	estat.value = 0;
386 	(void) hpi_fzc_sys_err_stat_get(handle, &estat);
387 	HXGE_DEBUG_MSG((hxgep, SYSERR_CTL,
388 	    "==> hxge_syserr_intr: device error 0x%016llx", estat.value));
389 
390 	if (estat.bits.tdc_err0 || estat.bits.tdc_err1) {
391 		/* TDMC */
392 		HXGE_ERROR_MSG((hxgep, HXGE_ERR_CTL,
393 		    "==> hxge_syserr_intr: device error - TDMC"));
394 		(void) hxge_txdma_handle_sys_errors(hxgep);
395 	} else if (estat.bits.rdc_err0 || estat.bits.rdc_err1) {
396 		/* RDMC */
397 		HXGE_ERROR_MSG((hxgep, HXGE_ERR_CTL,
398 		    "==> hxge_syserr_intr: device error - RDMC"));
399 		(void) hxge_rxdma_handle_sys_errors(hxgep);
400 	} else if (estat.bits.vnm_pio_err1 || estat.bits.peu_err1) {
401 		/* PCI-E */
402 		HXGE_ERROR_MSG((hxgep, HXGE_ERR_CTL,
403 		    "==> hxge_syserr_intr: device error - PCI-E"));
404 
405 		/* kstats are updated here */
406 		(void) hxge_peu_handle_sys_errors(hxgep);
407 
408 		if (estat.bits.peu_err1)
409 			HXGE_FM_REPORT_ERROR(hxgep, NULL,
410 			    HXGE_FM_EREPORT_PEU_ERR);
411 
412 		if (estat.bits.vnm_pio_err1)
413 			HXGE_FM_REPORT_ERROR(hxgep, NULL,
414 			    HXGE_FM_EREPORT_PEU_VNM_PIO_ERR);
415 	} else if (estat.value != 0) {
416 		HXGE_ERROR_MSG((hxgep, HXGE_ERR_CTL,
417 		    "==> hxge_syserr_intr: device error - unknown"));
418 	}
419 
420 	serviced = DDI_INTR_CLAIMED;
421 
422 	if ((ldgp != NULL) && (ldvp != NULL) &&
423 	    (ldgp->nldvs == 1) && !ldvp->use_timer) {
424 		(void) hpi_intr_ldg_mgmt_set(handle, ldgp->ldg,
425 		    B_TRUE, ldgp->ldg_timer);
426 	}
427 
428 	HXGE_DEBUG_MSG((hxgep, SYSERR_CTL, "<== hxge_syserr_intr"));
429 	return (serviced);
430 }
431 
432 void
433 hxge_intr_hw_enable(p_hxge_t hxgep)
434 {
435 	HXGE_DEBUG_MSG((hxgep, INT_CTL, "==> hxge_intr_hw_enable"));
436 
437 	(void) hxge_intr_mask_mgmt_set(hxgep, B_TRUE);
438 
439 	HXGE_DEBUG_MSG((hxgep, INT_CTL, "<== hxge_intr_hw_enable"));
440 }
441 
442 void
443 hxge_intr_hw_disable(p_hxge_t hxgep)
444 {
445 	HXGE_DEBUG_MSG((hxgep, INT_CTL, "==> hxge_intr_hw_disable"));
446 
447 	(void) hxge_intr_mask_mgmt_set(hxgep, B_FALSE);
448 
449 	HXGE_DEBUG_MSG((hxgep, INT_CTL, "<== hxge_intr_hw_disable"));
450 }
451 
452 /*ARGSUSED*/
453 void
454 hxge_rx_hw_blank(void *arg, time_t ticks, uint_t count)
455 {
456 	p_hxge_t	hxgep = (p_hxge_t)arg;
457 
458 	HXGE_DEBUG_MSG((hxgep, INT_CTL, "==> hxge_rx_hw_blank"));
459 
460 	/*
461 	 * Replace current ticks and counts for later
462 	 * processing by the receive packet interrupt routines.
463 	 */
464 	hxgep->intr_timeout = (uint16_t)ticks;
465 
466 	HXGE_DEBUG_MSG((hxgep, INT_CTL, "<== hxge_rx_hw_blank"));
467 }
468 
469 void
470 hxge_hw_stop(p_hxge_t hxgep)
471 {
472 	HXGE_DEBUG_MSG((hxgep, DDI_CTL, "==> hxge_hw_stop"));
473 
474 	(void) hxge_tx_vmac_disable(hxgep);
475 	(void) hxge_rx_vmac_disable(hxgep);
476 	(void) hxge_txdma_hw_mode(hxgep, HXGE_DMA_STOP);
477 	(void) hxge_rxdma_hw_mode(hxgep, HXGE_DMA_STOP);
478 
479 	HXGE_DEBUG_MSG((hxgep, DDI_CTL, "<== hxge_hw_stop"));
480 }
481 
482 void
483 hxge_hw_ioctl(p_hxge_t hxgep, queue_t *wq, mblk_t *mp, struct iocblk *iocp)
484 {
485 	int cmd;
486 
487 	HXGE_DEBUG_MSG((hxgep, IOC_CTL, "==> hxge_hw_ioctl"));
488 
489 	if (hxgep == NULL) {
490 		miocnak(wq, mp, 0, EINVAL);
491 		return;
492 	}
493 
494 	iocp->ioc_error = 0;
495 	cmd = iocp->ioc_cmd;
496 
497 	switch (cmd) {
498 	default:
499 		miocnak(wq, mp, 0, EINVAL);
500 		return;
501 
502 	case HXGE_PUT_TCAM:
503 		hxge_put_tcam(hxgep, mp->b_cont);
504 		miocack(wq, mp, 0, 0);
505 		break;
506 
507 	case HXGE_GET_TCAM:
508 		hxge_get_tcam(hxgep, mp->b_cont);
509 		miocack(wq, mp, 0, 0);
510 		break;
511 
512 	case HXGE_RTRACE:
513 		hxge_rtrace_ioctl(hxgep, wq, mp, iocp);
514 		break;
515 	}
516 }
517 
518 /*
519  * 10G is the only loopback mode for Hydra.
520  */
521 void
522 hxge_loopback_ioctl(p_hxge_t hxgep, queue_t *wq, mblk_t *mp,
523     struct iocblk *iocp)
524 {
525 	p_lb_property_t lb_props;
526 	size_t		size;
527 	int		i;
528 
529 	if (mp->b_cont == NULL) {
530 		miocnak(wq, mp, 0, EINVAL);
531 	}
532 
533 	switch (iocp->ioc_cmd) {
534 	case LB_GET_MODE:
535 		HXGE_DEBUG_MSG((hxgep, IOC_CTL, "HXGE_GET_LB_MODE command"));
536 		if (hxgep != NULL) {
537 			*(lb_info_sz_t *)mp->b_cont->b_rptr =
538 			    hxgep->statsp->port_stats.lb_mode;
539 			miocack(wq, mp, sizeof (hxge_lb_t), 0);
540 		} else
541 			miocnak(wq, mp, 0, EINVAL);
542 		break;
543 
544 	case LB_SET_MODE:
545 		HXGE_DEBUG_MSG((hxgep, IOC_CTL, "HXGE_SET_LB_MODE command"));
546 		if (iocp->ioc_count != sizeof (uint32_t)) {
547 			miocack(wq, mp, 0, 0);
548 			break;
549 		}
550 		if ((hxgep != NULL) && hxge_set_lb(hxgep, wq, mp->b_cont)) {
551 			miocack(wq, mp, 0, 0);
552 		} else {
553 			miocnak(wq, mp, 0, EPROTO);
554 		}
555 		break;
556 
557 	case LB_GET_INFO_SIZE:
558 		HXGE_DEBUG_MSG((hxgep, IOC_CTL, "LB_GET_INFO_SIZE command"));
559 		if (hxgep != NULL) {
560 			size = sizeof (lb_normal) + sizeof (lb_mac10g);
561 
562 			*(lb_info_sz_t *)mp->b_cont->b_rptr = size;
563 
564 			HXGE_DEBUG_MSG((hxgep, IOC_CTL,
565 			    "HXGE_GET_LB_INFO command: size %d", size));
566 			miocack(wq, mp, sizeof (lb_info_sz_t), 0);
567 		} else
568 			miocnak(wq, mp, 0, EINVAL);
569 		break;
570 
571 	case LB_GET_INFO:
572 		HXGE_DEBUG_MSG((hxgep, IOC_CTL, "HXGE_GET_LB_INFO command"));
573 		if (hxgep != NULL) {
574 			size = sizeof (lb_normal) + sizeof (lb_mac10g);
575 			HXGE_DEBUG_MSG((hxgep, IOC_CTL,
576 			    "HXGE_GET_LB_INFO command: size %d", size));
577 			if (size == iocp->ioc_count) {
578 				i = 0;
579 				lb_props = (p_lb_property_t)mp->b_cont->b_rptr;
580 				lb_props[i++] = lb_normal;
581 				lb_props[i++] = lb_mac10g;
582 
583 				miocack(wq, mp, size, 0);
584 			} else
585 				miocnak(wq, mp, 0, EINVAL);
586 		} else {
587 			miocnak(wq, mp, 0, EINVAL);
588 			cmn_err(CE_NOTE, "hxge_hw_ioctl: invalid command 0x%x",
589 			    iocp->ioc_cmd);
590 		}
591 
592 		break;
593 	}
594 }
595 
596 /*ARGSUSED*/
597 boolean_t
598 hxge_set_lb(p_hxge_t hxgep, queue_t *wq, p_mblk_t mp)
599 {
600 	boolean_t	status = B_TRUE;
601 	uint32_t	lb_mode;
602 	lb_property_t	*lb_info;
603 
604 	HXGE_DEBUG_MSG((hxgep, IOC_CTL, "<== hxge_set_lb"));
605 	lb_mode = hxgep->statsp->port_stats.lb_mode;
606 	if (lb_mode == *(uint32_t *)mp->b_rptr) {
607 		cmn_err(CE_NOTE,
608 		    "hxge%d: Loopback mode already set (lb_mode %d).\n",
609 		    hxgep->instance, lb_mode);
610 		status = B_FALSE;
611 		goto hxge_set_lb_exit;
612 	}
613 
614 	lb_mode = *(uint32_t *)mp->b_rptr;
615 	lb_info = NULL;
616 
617 	/* 10G is the only loopback mode for Hydra */
618 	if (lb_mode == lb_normal.value)
619 		lb_info = &lb_normal;
620 	else if (lb_mode == lb_mac10g.value)
621 		lb_info = &lb_mac10g;
622 	else {
623 		cmn_err(CE_NOTE,
624 		    "hxge%d: Loopback mode not supported(mode %d).\n",
625 		    hxgep->instance, lb_mode);
626 		status = B_FALSE;
627 		goto hxge_set_lb_exit;
628 	}
629 
630 	if (lb_mode == hxge_lb_normal) {
631 		if (hxge_lb_dbg) {
632 			cmn_err(CE_NOTE,
633 			    "!hxge%d: Returning to normal operation",
634 			    hxgep->instance);
635 		}
636 
637 		hxgep->statsp->port_stats.lb_mode = hxge_lb_normal;
638 		hxge_global_reset(hxgep);
639 
640 		goto hxge_set_lb_exit;
641 	}
642 
643 	hxgep->statsp->port_stats.lb_mode = lb_mode;
644 
645 	if (hxge_lb_dbg)
646 		cmn_err(CE_NOTE, "!hxge%d: Adapter now in %s loopback mode",
647 		    hxgep->instance, lb_info->key);
648 
649 	if (lb_info->lb_type == internal) {
650 		if ((hxgep->statsp->port_stats.lb_mode == hxge_lb_mac10g))
651 			hxgep->statsp->mac_stats.link_speed = 10000;
652 		else {
653 			cmn_err(CE_NOTE,
654 			    "hxge%d: Loopback mode not supported(mode %d).\n",
655 			    hxgep->instance, lb_mode);
656 			status = B_FALSE;
657 			goto hxge_set_lb_exit;
658 		}
659 		hxgep->statsp->mac_stats.link_duplex = 2;
660 		hxgep->statsp->mac_stats.link_up = 1;
661 	}
662 
663 	hxge_global_reset(hxgep);
664 
665 hxge_set_lb_exit:
666 	HXGE_DEBUG_MSG((hxgep, DDI_CTL,
667 	    "<== hxge_set_lb status = 0x%08x", status));
668 
669 	return (status);
670 }
671 
672 void
673 hxge_check_hw_state(p_hxge_t hxgep)
674 {
675 	p_hxge_ldgv_t		ldgvp;
676 	p_hxge_ldv_t		t_ldvp;
677 
678 	HXGE_DEBUG_MSG((hxgep, SYSERR_CTL, "==> hxge_check_hw_state"));
679 
680 	MUTEX_ENTER(hxgep->genlock);
681 
682 	hxgep->hxge_timerid = 0;
683 	if (!(hxgep->drv_state & STATE_HW_INITIALIZED)) {
684 		goto hxge_check_hw_state_exit;
685 	}
686 
687 	hxge_check_tx_hang(hxgep);
688 
689 	ldgvp = hxgep->ldgvp;
690 	if (ldgvp == NULL || (ldgvp->ldvp_syserr == NULL)) {
691 		HXGE_ERROR_MSG((hxgep, SYSERR_CTL, "<== hxge_check_hw_state: "
692 		    "NULL ldgvp (interrupt not ready)."));
693 		goto hxge_check_hw_state_exit;
694 	}
695 
696 	t_ldvp = ldgvp->ldvp_syserr;
697 	if (!t_ldvp->use_timer) {
698 		HXGE_DEBUG_MSG((hxgep, SYSERR_CTL, "<== hxge_check_hw_state: "
699 		    "ldgvp $%p t_ldvp $%p use_timer flag %d",
700 		    ldgvp, t_ldvp, t_ldvp->use_timer));
701 		goto hxge_check_hw_state_exit;
702 	}
703 
704 	if (fm_check_acc_handle(hxgep->dev_regs->hxge_regh) != DDI_FM_OK) {
705 		HXGE_ERROR_MSG((hxgep, HXGE_ERR_CTL,
706 		    "Bad register acc handle"));
707 	}
708 
709 	(void) hxge_syserr_intr((caddr_t)t_ldvp, (caddr_t)hxgep);
710 
711 	hxgep->hxge_timerid = hxge_start_timer(hxgep, hxge_check_hw_state,
712 	    HXGE_CHECK_TIMER);
713 
714 hxge_check_hw_state_exit:
715 	MUTEX_EXIT(hxgep->genlock);
716 
717 	HXGE_DEBUG_MSG((hxgep, SYSERR_CTL, "<== hxge_check_hw_state"));
718 }
719 
720 /*ARGSUSED*/
721 static void
722 hxge_rtrace_ioctl(p_hxge_t hxgep, queue_t *wq, mblk_t *mp,
723     struct iocblk *iocp)
724 {
725 	ssize_t		size;
726 	rtrace_t	*rtp;
727 	mblk_t		*nmp;
728 	uint32_t	i, j;
729 	uint32_t	start_blk;
730 	uint32_t	base_entry;
731 	uint32_t	num_entries;
732 
733 	HXGE_DEBUG_MSG((hxgep, STR_CTL, "==> hxge_rtrace_ioctl"));
734 
735 	size = 1024;
736 	if (mp->b_cont == NULL || MBLKL(mp->b_cont) < size) {
737 		HXGE_DEBUG_MSG((hxgep, STR_CTL,
738 		    "malformed M_IOCTL MBLKL = %d size = %d",
739 		    MBLKL(mp->b_cont), size));
740 		miocnak(wq, mp, 0, EINVAL);
741 		return;
742 	}
743 
744 	nmp = mp->b_cont;
745 	rtp = (rtrace_t *)nmp->b_rptr;
746 	start_blk = rtp->next_idx;
747 	num_entries = rtp->last_idx;
748 	base_entry = start_blk * MAX_RTRACE_IOC_ENTRIES;
749 
750 	HXGE_DEBUG_MSG((hxgep, STR_CTL, "start_blk = %d\n", start_blk));
751 	HXGE_DEBUG_MSG((hxgep, STR_CTL, "num_entries = %d\n", num_entries));
752 	HXGE_DEBUG_MSG((hxgep, STR_CTL, "base_entry = %d\n", base_entry));
753 
754 	rtp->next_idx = hpi_rtracebuf.next_idx;
755 	rtp->last_idx = hpi_rtracebuf.last_idx;
756 	rtp->wrapped = hpi_rtracebuf.wrapped;
757 	for (i = 0, j = base_entry; i < num_entries; i++, j++) {
758 		rtp->buf[i].ctl_addr = hpi_rtracebuf.buf[j].ctl_addr;
759 		rtp->buf[i].val_l32 = hpi_rtracebuf.buf[j].val_l32;
760 		rtp->buf[i].val_h32 = hpi_rtracebuf.buf[j].val_h32;
761 	}
762 
763 	nmp->b_wptr = nmp->b_rptr + size;
764 	HXGE_DEBUG_MSG((hxgep, STR_CTL, "<== hxge_rtrace_ioctl"));
765 	miocack(wq, mp, (int)size, 0);
766 }
767