xref: /illumos-gate/usr/src/uts/common/io/nxge/npi/npi.c (revision 2d6eb4a5)
144961713Sgirish /*
244961713Sgirish  * CDDL HEADER START
344961713Sgirish  *
444961713Sgirish  * The contents of this file are subject to the terms of the
544961713Sgirish  * Common Development and Distribution License (the "License").
644961713Sgirish  * You may not use this file except in compliance with the License.
744961713Sgirish  *
844961713Sgirish  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
944961713Sgirish  * or http://www.opensolaris.org/os/licensing.
1044961713Sgirish  * See the License for the specific language governing permissions
1144961713Sgirish  * and limitations under the License.
1244961713Sgirish  *
1344961713Sgirish  * When distributing Covered Code, include this CDDL HEADER in each
1444961713Sgirish  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1544961713Sgirish  * If applicable, add the following below this CDDL HEADER, with the
1644961713Sgirish  * fields enclosed by brackets "[]" replaced with your own identifying
1744961713Sgirish  * information: Portions Copyright [yyyy] [name of copyright owner]
1844961713Sgirish  *
1944961713Sgirish  * CDDL HEADER END
2044961713Sgirish  */
2144961713Sgirish /*
22678453a8Sspeer  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
2344961713Sgirish  * Use is subject to license terms.
2444961713Sgirish  */
2544961713Sgirish 
2644961713Sgirish #include <npi.h>
2744961713Sgirish #include <sys/nxge/nxge_impl.h>
2844961713Sgirish 
2944961713Sgirish nxge_os_mutex_t npidebuglock;
3044961713Sgirish int npi_debug_init = 0;
3144961713Sgirish uint64_t npi_debug_level = 0;
3244961713Sgirish 
33678453a8Sspeer extern void npi_trace_dump(rtrace_t *, int);
34678453a8Sspeer 
3544961713Sgirish void
npi_debug_msg(npi_handle_function_t function,uint64_t level,char * fmt,...)3644961713Sgirish npi_debug_msg(npi_handle_function_t function, uint64_t level, char *fmt, ...)
3744961713Sgirish {
3844961713Sgirish 	char msg_buffer[1024];
3944961713Sgirish 	char prefix_buffer[32];
4044961713Sgirish 	int cmn_level = CE_CONT;
4144961713Sgirish 	va_list ap;
4244961713Sgirish 
4344961713Sgirish 	if ((level & npi_debug_level) ||
44*52ccf843Smisaki 	    (level & NPI_REG_CTL) ||
45*52ccf843Smisaki 	    (level & NPI_ERR_CTL)) {
4644961713Sgirish 
4744961713Sgirish 		if (npi_debug_init == 0) {
4844961713Sgirish 			MUTEX_INIT(&npidebuglock, NULL, MUTEX_DRIVER, NULL);
4944961713Sgirish 			npi_debug_init = 1;
5044961713Sgirish 		}
5144961713Sgirish 
5244961713Sgirish 		MUTEX_ENTER(&npidebuglock);
5344961713Sgirish 
5444961713Sgirish 		if (level & NPI_ERR_CTL) {
5544961713Sgirish 			cmn_level = CE_WARN;
5644961713Sgirish 		}
5744961713Sgirish 
5844961713Sgirish 		va_start(ap, fmt);
5944961713Sgirish 		(void) vsprintf(msg_buffer, fmt, ap);
6044961713Sgirish 		va_end(ap);
6144961713Sgirish 
6244961713Sgirish 		(void) sprintf(prefix_buffer, "%s%d(%d):", "npi",
63*52ccf843Smisaki 		    function.instance, function.function);
6444961713Sgirish 
6544961713Sgirish 		MUTEX_EXIT(&npidebuglock);
6644961713Sgirish 		cmn_err(cmn_level, "!%s %s\n", prefix_buffer, msg_buffer);
6744961713Sgirish 	}
6844961713Sgirish }
6944961713Sgirish 
7044961713Sgirish void
npi_rtrace_buf_init(rtrace_t * rt)7144961713Sgirish npi_rtrace_buf_init(rtrace_t *rt)
7244961713Sgirish {
7344961713Sgirish 	int i;
7444961713Sgirish 
7544961713Sgirish 	rt->next_idx = 0;
7644961713Sgirish 	rt->last_idx = MAX_RTRACE_ENTRIES - 1;
7744961713Sgirish 	rt->wrapped = B_FALSE;
7844961713Sgirish 	for (i = 0; i < MAX_RTRACE_ENTRIES; i++) {
7944961713Sgirish 		rt->buf[i].ctl_addr = TRACE_CTL_INVALID;
8044961713Sgirish 		rt->buf[i].val_l32 = 0;
8144961713Sgirish 		rt->buf[i].val_h32 = 0;
8244961713Sgirish 	}
8344961713Sgirish }
8444961713Sgirish 
8544961713Sgirish void
npi_rtrace_update(npi_handle_t handle,boolean_t wr,rtrace_t * rt,uint32_t addr,uint64_t val)8644961713Sgirish npi_rtrace_update(npi_handle_t handle, boolean_t wr, rtrace_t *rt,
87678453a8Sspeer     uint32_t addr, uint64_t val)
8844961713Sgirish {
8944961713Sgirish 	int idx;
9044961713Sgirish 	idx = rt->next_idx;
9144961713Sgirish 	if (wr == B_TRUE)
9244961713Sgirish 		rt->buf[idx].ctl_addr = (addr & TRACE_ADDR_MASK)
93*52ccf843Smisaki 		    | TRACE_CTL_WR;
9444961713Sgirish 	else
9544961713Sgirish 		rt->buf[idx].ctl_addr = (addr & TRACE_ADDR_MASK);
9644961713Sgirish 	rt->buf[idx].ctl_addr |= (((handle.function.function
97*52ccf843Smisaki 	    << TRACE_FUNC_SHIFT) & TRACE_FUNC_MASK) |
98*52ccf843Smisaki 	    ((handle.function.instance
99*52ccf843Smisaki 	    << TRACE_INST_SHIFT) & TRACE_INST_MASK));
10044961713Sgirish 	rt->buf[idx].val_l32 = val & 0xFFFFFFFF;
10144961713Sgirish 	rt->buf[idx].val_h32 = (val >> 32) & 0xFFFFFFFF;
10244961713Sgirish 	rt->next_idx++;
10344961713Sgirish 	if (rt->next_idx > rt->last_idx) {
10444961713Sgirish 		rt->next_idx = 0;
10544961713Sgirish 		rt->wrapped = B_TRUE;
10644961713Sgirish 	}
10744961713Sgirish }
108678453a8Sspeer 
109678453a8Sspeer void
npi_trace_update(npi_handle_t handle,boolean_t wr,rtrace_t * rt,const char * name,uint32_t addr,uint64_t val)110678453a8Sspeer npi_trace_update(npi_handle_t handle, boolean_t wr, rtrace_t *rt,
111678453a8Sspeer     const char *name, uint32_t addr, uint64_t val)
112678453a8Sspeer {
113678453a8Sspeer 	int idx;
114678453a8Sspeer 	idx = rt->next_idx;
115678453a8Sspeer 	if (wr == B_TRUE)
116678453a8Sspeer 		rt->buf[idx].ctl_addr = (addr & TRACE_ADDR_MASK)
117*52ccf843Smisaki 		    | TRACE_CTL_WR;
118678453a8Sspeer 	else
119678453a8Sspeer 		rt->buf[idx].ctl_addr = (addr & TRACE_ADDR_MASK);
120678453a8Sspeer 	/*
121678453a8Sspeer 	 * Control Address field format
122678453a8Sspeer 	 *
123678453a8Sspeer 	 * Bit  0 - 23: Address
124678453a8Sspeer 	 * Bit 24 - 25: Function Number
125678453a8Sspeer 	 * Bit 26 - 29: Instance Number
126678453a8Sspeer 	 * Bit 30: Read/Write Direction bit
127678453a8Sspeer 	 * Bit 31: Invalid bit
128678453a8Sspeer 	 */
129678453a8Sspeer 	rt->buf[idx].ctl_addr |= (((handle.function.function
130*52ccf843Smisaki 	    << TRACE_FUNC_SHIFT) & TRACE_FUNC_MASK) |
131*52ccf843Smisaki 	    ((handle.function.instance
132*52ccf843Smisaki 	    << TRACE_INST_SHIFT) & TRACE_INST_MASK));
133678453a8Sspeer 	rt->buf[idx].val_l32 = val & 0xFFFFFFFF;
134678453a8Sspeer 	rt->buf[idx].val_h32 = (val >> 32) & 0xFFFFFFFF;
135678453a8Sspeer 	(void) strncpy(rt->buf[idx].name, name, 15);
136678453a8Sspeer 	rt->next_idx++;
137678453a8Sspeer 	if (rt->next_idx > rt->last_idx) {
138678453a8Sspeer 		rt->next_idx = 0;
139678453a8Sspeer 		rt->wrapped = B_TRUE;
140678453a8Sspeer 	}
141678453a8Sspeer }
142678453a8Sspeer 
143678453a8Sspeer #if defined(NPI_DEBUG)
144678453a8Sspeer void
npi_trace_dump(rtrace_t * rt,int count)145678453a8Sspeer npi_trace_dump(
146678453a8Sspeer 	rtrace_t *rt,
147678453a8Sspeer 	int count)
148678453a8Sspeer {
149678453a8Sspeer 	rt_buf_t *trace;
150678453a8Sspeer 	int cursor, i;
151678453a8Sspeer 
152678453a8Sspeer 	if (count == 0 || count > MAX_RTRACE_ENTRIES)
153678453a8Sspeer 		count = 32;
154678453a8Sspeer 
155678453a8Sspeer 	/*
156678453a8Sspeer 	 * Starting with the last recorded entry,
157678453a8Sspeer 	 * dump the <count> most recent records.
158678453a8Sspeer 	 */
159678453a8Sspeer 	cursor = rt->next_idx;
160678453a8Sspeer 	cursor = (cursor == 0) ? rt->last_idx : cursor - 1;
161678453a8Sspeer 
162678453a8Sspeer 	for (i = 0; i < count; i++) {
163678453a8Sspeer 		trace = &rt->buf[cursor];
164678453a8Sspeer 		if (trace->ctl_addr == 0)
165678453a8Sspeer 			break;
166678453a8Sspeer 		cmn_err(CE_NOTE, "%16s @ 0x%08x: 0x%08x.%08x: %c",
167678453a8Sspeer 		    trace->name, trace->ctl_addr,
168678453a8Sspeer 		    trace->val_h32, trace->val_l32,
169678453a8Sspeer 		    trace->ctl_addr & TRACE_CTL_WR ? 'W' : 'R');
170678453a8Sspeer 		cursor = (cursor == 0) ? rt->last_idx : cursor - 1;
171678453a8Sspeer 	}
172678453a8Sspeer }
173678453a8Sspeer #endif
174