xref: /illumos-gate/usr/src/uts/common/io/nxge/npi/npi.c (revision afab0816ecb604f0099a09ad8ee398f0d7b77b1c)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <npi.h>
29 #include <sys/nxge/nxge_impl.h>
30 
31 nxge_os_mutex_t npidebuglock;
32 int npi_debug_init = 0;
33 uint64_t npi_debug_level = 0;
34 
35 extern void npi_trace_dump(rtrace_t *, int);
36 
37 void
38 npi_debug_msg(npi_handle_function_t function, uint64_t level, char *fmt, ...)
39 {
40 	char msg_buffer[1024];
41 	char prefix_buffer[32];
42 	int cmn_level = CE_CONT;
43 	va_list ap;
44 
45 	if ((level & npi_debug_level) ||
46 	    (level & NPI_REG_CTL) ||
47 	    (level & NPI_ERR_CTL)) {
48 
49 		if (npi_debug_init == 0) {
50 			MUTEX_INIT(&npidebuglock, NULL, MUTEX_DRIVER, NULL);
51 			npi_debug_init = 1;
52 		}
53 
54 		MUTEX_ENTER(&npidebuglock);
55 
56 		if (level & NPI_ERR_CTL) {
57 			cmn_level = CE_WARN;
58 		}
59 
60 		va_start(ap, fmt);
61 		(void) vsprintf(msg_buffer, fmt, ap);
62 		va_end(ap);
63 
64 		(void) sprintf(prefix_buffer, "%s%d(%d):", "npi",
65 		    function.instance, function.function);
66 
67 		MUTEX_EXIT(&npidebuglock);
68 		cmn_err(cmn_level, "!%s %s\n", prefix_buffer, msg_buffer);
69 	}
70 }
71 
72 void
73 npi_rtrace_buf_init(rtrace_t *rt)
74 {
75 	int i;
76 
77 	rt->next_idx = 0;
78 	rt->last_idx = MAX_RTRACE_ENTRIES - 1;
79 	rt->wrapped = B_FALSE;
80 	for (i = 0; i < MAX_RTRACE_ENTRIES; i++) {
81 		rt->buf[i].ctl_addr = TRACE_CTL_INVALID;
82 		rt->buf[i].val_l32 = 0;
83 		rt->buf[i].val_h32 = 0;
84 	}
85 }
86 
87 void
88 npi_rtrace_update(npi_handle_t handle, boolean_t wr, rtrace_t *rt,
89     uint32_t addr, uint64_t val)
90 {
91 	int idx;
92 	idx = rt->next_idx;
93 	if (wr == B_TRUE)
94 		rt->buf[idx].ctl_addr = (addr & TRACE_ADDR_MASK)
95 		    | TRACE_CTL_WR;
96 	else
97 		rt->buf[idx].ctl_addr = (addr & TRACE_ADDR_MASK);
98 	rt->buf[idx].ctl_addr |= (((handle.function.function
99 	    << TRACE_FUNC_SHIFT) & TRACE_FUNC_MASK) |
100 	    ((handle.function.instance
101 	    << TRACE_INST_SHIFT) & TRACE_INST_MASK));
102 	rt->buf[idx].val_l32 = val & 0xFFFFFFFF;
103 	rt->buf[idx].val_h32 = (val >> 32) & 0xFFFFFFFF;
104 	rt->next_idx++;
105 	if (rt->next_idx > rt->last_idx) {
106 		rt->next_idx = 0;
107 		rt->wrapped = B_TRUE;
108 	}
109 }
110 
111 void
112 npi_trace_update(npi_handle_t handle, boolean_t wr, rtrace_t *rt,
113     const char *name, uint32_t addr, uint64_t val)
114 {
115 	int idx;
116 	idx = rt->next_idx;
117 	if (wr == B_TRUE)
118 		rt->buf[idx].ctl_addr = (addr & TRACE_ADDR_MASK)
119 		    | TRACE_CTL_WR;
120 	else
121 		rt->buf[idx].ctl_addr = (addr & TRACE_ADDR_MASK);
122 	/*
123 	 * Control Address field format
124 	 *
125 	 * Bit  0 - 23: Address
126 	 * Bit 24 - 25: Function Number
127 	 * Bit 26 - 29: Instance Number
128 	 * Bit 30: Read/Write Direction bit
129 	 * Bit 31: Invalid bit
130 	 */
131 	rt->buf[idx].ctl_addr |= (((handle.function.function
132 	    << TRACE_FUNC_SHIFT) & TRACE_FUNC_MASK) |
133 	    ((handle.function.instance
134 	    << TRACE_INST_SHIFT) & TRACE_INST_MASK));
135 	rt->buf[idx].val_l32 = val & 0xFFFFFFFF;
136 	rt->buf[idx].val_h32 = (val >> 32) & 0xFFFFFFFF;
137 	(void) strncpy(rt->buf[idx].name, name, 15);
138 	rt->next_idx++;
139 	if (rt->next_idx > rt->last_idx) {
140 		rt->next_idx = 0;
141 		rt->wrapped = B_TRUE;
142 	}
143 }
144 
145 #if defined(NPI_DEBUG)
146 void
147 npi_trace_dump(
148 	rtrace_t *rt,
149 	int count)
150 {
151 	rt_buf_t *trace;
152 	int cursor, i;
153 
154 	if (count == 0 || count > MAX_RTRACE_ENTRIES)
155 		count = 32;
156 
157 	/*
158 	 * Starting with the last recorded entry,
159 	 * dump the <count> most recent records.
160 	 */
161 	cursor = rt->next_idx;
162 	cursor = (cursor == 0) ? rt->last_idx : cursor - 1;
163 
164 	for (i = 0; i < count; i++) {
165 		trace = &rt->buf[cursor];
166 		if (trace->ctl_addr == 0)
167 			break;
168 		cmn_err(CE_NOTE, "%16s @ 0x%08x: 0x%08x.%08x: %c",
169 		    trace->name, trace->ctl_addr,
170 		    trace->val_h32, trace->val_l32,
171 		    trace->ctl_addr & TRACE_CTL_WR ? 'W' : 'R');
172 		cursor = (cursor == 0) ? rt->last_idx : cursor - 1;
173 	}
174 }
175 #endif
176