xref: /titanic_51/usr/src/uts/common/io/nxge/npi/npi.c (revision 52ccf843e173e2a4a657360b0a22853fd413905f)
16f45ec7bSml29623 /*
26f45ec7bSml29623  * CDDL HEADER START
36f45ec7bSml29623  *
46f45ec7bSml29623  * The contents of this file are subject to the terms of the
56f45ec7bSml29623  * Common Development and Distribution License (the "License").
66f45ec7bSml29623  * You may not use this file except in compliance with the License.
76f45ec7bSml29623  *
86f45ec7bSml29623  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96f45ec7bSml29623  * or http://www.opensolaris.org/os/licensing.
106f45ec7bSml29623  * See the License for the specific language governing permissions
116f45ec7bSml29623  * and limitations under the License.
126f45ec7bSml29623  *
136f45ec7bSml29623  * When distributing Covered Code, include this CDDL HEADER in each
146f45ec7bSml29623  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156f45ec7bSml29623  * If applicable, add the following below this CDDL HEADER, with the
166f45ec7bSml29623  * fields enclosed by brackets "[]" replaced with your own identifying
176f45ec7bSml29623  * information: Portions Copyright [yyyy] [name of copyright owner]
186f45ec7bSml29623  *
196f45ec7bSml29623  * CDDL HEADER END
206f45ec7bSml29623  */
216f45ec7bSml29623 /*
22*678453a8Sspeer  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
236f45ec7bSml29623  * Use is subject to license terms.
246f45ec7bSml29623  */
256f45ec7bSml29623 
266f45ec7bSml29623 #pragma ident	"%Z%%M%	%I%	%E% SMI"
276f45ec7bSml29623 
286f45ec7bSml29623 #include <npi.h>
296f45ec7bSml29623 #include <sys/nxge/nxge_impl.h>
306f45ec7bSml29623 
316f45ec7bSml29623 nxge_os_mutex_t npidebuglock;
326f45ec7bSml29623 int npi_debug_init = 0;
336f45ec7bSml29623 uint64_t npi_debug_level = 0;
346f45ec7bSml29623 
35*678453a8Sspeer extern void npi_trace_dump(rtrace_t *, int);
36*678453a8Sspeer 
376f45ec7bSml29623 void
386f45ec7bSml29623 npi_debug_msg(npi_handle_function_t function, uint64_t level, char *fmt, ...)
396f45ec7bSml29623 {
406f45ec7bSml29623 	char msg_buffer[1024];
416f45ec7bSml29623 	char prefix_buffer[32];
426f45ec7bSml29623 	int cmn_level = CE_CONT;
436f45ec7bSml29623 	va_list ap;
446f45ec7bSml29623 
456f45ec7bSml29623 	if ((level & npi_debug_level) ||
466f45ec7bSml29623 	    (level & NPI_REG_CTL) ||
476f45ec7bSml29623 	    (level & NPI_ERR_CTL)) {
486f45ec7bSml29623 
496f45ec7bSml29623 		if (npi_debug_init == 0) {
506f45ec7bSml29623 			MUTEX_INIT(&npidebuglock, NULL, MUTEX_DRIVER, NULL);
516f45ec7bSml29623 			npi_debug_init = 1;
526f45ec7bSml29623 		}
536f45ec7bSml29623 
546f45ec7bSml29623 		MUTEX_ENTER(&npidebuglock);
556f45ec7bSml29623 
566f45ec7bSml29623 		if (level & NPI_ERR_CTL) {
576f45ec7bSml29623 			cmn_level = CE_WARN;
586f45ec7bSml29623 		}
596f45ec7bSml29623 
606f45ec7bSml29623 		va_start(ap, fmt);
616f45ec7bSml29623 		(void) vsprintf(msg_buffer, fmt, ap);
626f45ec7bSml29623 		va_end(ap);
636f45ec7bSml29623 
646f45ec7bSml29623 		(void) sprintf(prefix_buffer, "%s%d(%d):", "npi",
656f45ec7bSml29623 		    function.instance, function.function);
666f45ec7bSml29623 
676f45ec7bSml29623 		MUTEX_EXIT(&npidebuglock);
686f45ec7bSml29623 		cmn_err(cmn_level, "!%s %s\n", prefix_buffer, msg_buffer);
696f45ec7bSml29623 	}
706f45ec7bSml29623 }
716f45ec7bSml29623 
726f45ec7bSml29623 void
736f45ec7bSml29623 npi_rtrace_buf_init(rtrace_t *rt)
746f45ec7bSml29623 {
756f45ec7bSml29623 	int i;
766f45ec7bSml29623 
776f45ec7bSml29623 	rt->next_idx = 0;
786f45ec7bSml29623 	rt->last_idx = MAX_RTRACE_ENTRIES - 1;
796f45ec7bSml29623 	rt->wrapped = B_FALSE;
806f45ec7bSml29623 	for (i = 0; i < MAX_RTRACE_ENTRIES; i++) {
816f45ec7bSml29623 		rt->buf[i].ctl_addr = TRACE_CTL_INVALID;
826f45ec7bSml29623 		rt->buf[i].val_l32 = 0;
836f45ec7bSml29623 		rt->buf[i].val_h32 = 0;
846f45ec7bSml29623 	}
856f45ec7bSml29623 }
866f45ec7bSml29623 
876f45ec7bSml29623 void
886f45ec7bSml29623 npi_rtrace_update(npi_handle_t handle, boolean_t wr, rtrace_t *rt,
896f45ec7bSml29623     uint32_t addr, uint64_t val)
906f45ec7bSml29623 {
916f45ec7bSml29623 	int idx;
926f45ec7bSml29623 	idx = rt->next_idx;
936f45ec7bSml29623 	if (wr == B_TRUE)
946f45ec7bSml29623 		rt->buf[idx].ctl_addr = (addr & TRACE_ADDR_MASK)
956f45ec7bSml29623 		    | TRACE_CTL_WR;
966f45ec7bSml29623 	else
976f45ec7bSml29623 		rt->buf[idx].ctl_addr = (addr & TRACE_ADDR_MASK);
986f45ec7bSml29623 	rt->buf[idx].ctl_addr |= (((handle.function.function
996f45ec7bSml29623 	    << TRACE_FUNC_SHIFT) & TRACE_FUNC_MASK) |
1006f45ec7bSml29623 	    ((handle.function.instance
1016f45ec7bSml29623 	    << TRACE_INST_SHIFT) & TRACE_INST_MASK));
1026f45ec7bSml29623 	rt->buf[idx].val_l32 = val & 0xFFFFFFFF;
1036f45ec7bSml29623 	rt->buf[idx].val_h32 = (val >> 32) & 0xFFFFFFFF;
1046f45ec7bSml29623 	rt->next_idx++;
1056f45ec7bSml29623 	if (rt->next_idx > rt->last_idx) {
1066f45ec7bSml29623 		rt->next_idx = 0;
1076f45ec7bSml29623 		rt->wrapped = B_TRUE;
1086f45ec7bSml29623 	}
1096f45ec7bSml29623 }
110*678453a8Sspeer 
111*678453a8Sspeer void
112*678453a8Sspeer npi_trace_update(npi_handle_t handle, boolean_t wr, rtrace_t *rt,
113*678453a8Sspeer     const char *name, uint32_t addr, uint64_t val)
114*678453a8Sspeer {
115*678453a8Sspeer 	int idx;
116*678453a8Sspeer 	idx = rt->next_idx;
117*678453a8Sspeer 	if (wr == B_TRUE)
118*678453a8Sspeer 		rt->buf[idx].ctl_addr = (addr & TRACE_ADDR_MASK)
119*678453a8Sspeer 		    | TRACE_CTL_WR;
120*678453a8Sspeer 	else
121*678453a8Sspeer 		rt->buf[idx].ctl_addr = (addr & TRACE_ADDR_MASK);
122*678453a8Sspeer 	/*
123*678453a8Sspeer 	 * Control Address field format
124*678453a8Sspeer 	 *
125*678453a8Sspeer 	 * Bit  0 - 23: Address
126*678453a8Sspeer 	 * Bit 24 - 25: Function Number
127*678453a8Sspeer 	 * Bit 26 - 29: Instance Number
128*678453a8Sspeer 	 * Bit 30: Read/Write Direction bit
129*678453a8Sspeer 	 * Bit 31: Invalid bit
130*678453a8Sspeer 	 */
131*678453a8Sspeer 	rt->buf[idx].ctl_addr |= (((handle.function.function
132*678453a8Sspeer 	    << TRACE_FUNC_SHIFT) & TRACE_FUNC_MASK) |
133*678453a8Sspeer 	    ((handle.function.instance
134*678453a8Sspeer 	    << TRACE_INST_SHIFT) & TRACE_INST_MASK));
135*678453a8Sspeer 	rt->buf[idx].val_l32 = val & 0xFFFFFFFF;
136*678453a8Sspeer 	rt->buf[idx].val_h32 = (val >> 32) & 0xFFFFFFFF;
137*678453a8Sspeer 	(void) strncpy(rt->buf[idx].name, name, 15);
138*678453a8Sspeer 	rt->next_idx++;
139*678453a8Sspeer 	if (rt->next_idx > rt->last_idx) {
140*678453a8Sspeer 		rt->next_idx = 0;
141*678453a8Sspeer 		rt->wrapped = B_TRUE;
142*678453a8Sspeer 	}
143*678453a8Sspeer }
144*678453a8Sspeer 
145*678453a8Sspeer #if defined(NPI_DEBUG)
146*678453a8Sspeer void
147*678453a8Sspeer npi_trace_dump(
148*678453a8Sspeer 	rtrace_t *rt,
149*678453a8Sspeer 	int count)
150*678453a8Sspeer {
151*678453a8Sspeer 	rt_buf_t *trace;
152*678453a8Sspeer 	int cursor, i;
153*678453a8Sspeer 
154*678453a8Sspeer 	if (count == 0 || count > MAX_RTRACE_ENTRIES)
155*678453a8Sspeer 		count = 32;
156*678453a8Sspeer 
157*678453a8Sspeer 	/*
158*678453a8Sspeer 	 * Starting with the last recorded entry,
159*678453a8Sspeer 	 * dump the <count> most recent records.
160*678453a8Sspeer 	 */
161*678453a8Sspeer 	cursor = rt->next_idx;
162*678453a8Sspeer 	cursor = (cursor == 0) ? rt->last_idx : cursor - 1;
163*678453a8Sspeer 
164*678453a8Sspeer 	for (i = 0; i < count; i++) {
165*678453a8Sspeer 		trace = &rt->buf[cursor];
166*678453a8Sspeer 		if (trace->ctl_addr == 0)
167*678453a8Sspeer 			break;
168*678453a8Sspeer 		cmn_err(CE_NOTE, "%16s @ 0x%08x: 0x%08x.%08x: %c",
169*678453a8Sspeer 		    trace->name, trace->ctl_addr,
170*678453a8Sspeer 		    trace->val_h32, trace->val_l32,
171*678453a8Sspeer 		    trace->ctl_addr & TRACE_CTL_WR ? 'W' : 'R');
172*678453a8Sspeer 		cursor = (cursor == 0) ? rt->last_idx : cursor - 1;
173*678453a8Sspeer 	}
174*678453a8Sspeer }
175*678453a8Sspeer #endif
176