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