summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/ib/clients/eoib/enx_log.c
blob: ac264cacf67a06171c1c4d02951460448f9e1bbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
 */

#include <sys/types.h>
#include <sys/kmem.h>
#include <sys/cmn_err.h>
#include <sys/conf.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/ksynch.h>
#include <sys/varargs.h>

#include <sys/ib/clients/eoib/enx_impl.h>

/*
 * Defaults
 */
uint_t enx_log_size = ENX_LOGSZ_DEFAULT;
int enx_log_level = ENX_MSGS_DEFAULT | ENX_MSGS_DEBUG;
int enx_log_timestamps = 0;

/*
 * Debug variables, should not be tunables so allocated debug buffer
 * and its size remain consistent.
 */
static kmutex_t enx_debug_buf_lock;
static uint8_t *enx_debug_buf;
static uint32_t enx_debug_buf_ndx;
static uint_t enx_debug_buf_sz;

static void eibnx_log(char *);

void
eibnx_debug_init(void)
{
	enx_debug_buf_ndx = 0;
	enx_debug_buf_sz = enx_log_size;
	enx_debug_buf = kmem_zalloc(enx_debug_buf_sz, KM_SLEEP);

	mutex_init(&enx_debug_buf_lock, NULL, MUTEX_DRIVER, NULL);
}

void
eibnx_debug_fini(void)
{
	mutex_destroy(&enx_debug_buf_lock);

	if (enx_debug_buf && enx_debug_buf_sz) {
		kmem_free(enx_debug_buf, enx_debug_buf_sz);
		enx_debug_buf = NULL;
	}
	enx_debug_buf_sz = 0;
	enx_debug_buf_ndx = 0;
}

void
eibnx_log(char *msg)
{
	uint32_t off;
	int msglen;
	char msgbuf[ENX_MAX_LINE];

	if (enx_debug_buf == NULL)
		return;

	if (enx_log_timestamps) {
		msglen = snprintf(msgbuf, ENX_MAX_LINE, "%llx: %s",
		    (unsigned long long)ddi_get_lbolt64(), msg);
	} else {
		msglen = snprintf(msgbuf, ENX_MAX_LINE, "%s", msg);
	}

	if (msglen < 0)
		return;
	else if (msglen >= ENX_MAX_LINE)
		msglen = ENX_MAX_LINE - 1;

	mutex_enter(&enx_debug_buf_lock);

	if ((enx_debug_buf_ndx == 0) ||
	    (enx_debug_buf[enx_debug_buf_ndx-1] != '\n')) {
		enx_debug_buf[enx_debug_buf_ndx] = '\n';
		enx_debug_buf_ndx++;
	}

	off = enx_debug_buf_ndx;	/* current msg should go here */

	enx_debug_buf_ndx += msglen;	/* next msg should start here */
	enx_debug_buf[enx_debug_buf_ndx] = 0;	/* terminate current msg */

	if (enx_debug_buf_ndx >= (enx_debug_buf_sz - 2 * ENX_MAX_LINE))
		enx_debug_buf_ndx = 0;

	mutex_exit(&enx_debug_buf_lock);

	bcopy(msgbuf, enx_debug_buf+off, msglen);    /* no lock needed */
}

#ifdef ENX_DEBUG
void
eibnx_dprintf_verbose(const char *fmt, ...)
{
	va_list ap;
	int msglen;
	char msgbuf[ENX_MAX_LINE];
	char newfmt[ENX_MAX_LINE];

	if ((enx_log_level & ENX_MSGS_VERBOSE) != ENX_MSGS_VERBOSE)
		return;

	(void) snprintf(newfmt, ENX_MAX_LINE, "..........%s", fmt);

	va_start(ap, fmt);
	msglen = vsnprintf(msgbuf, ENX_MAX_LINE, newfmt, ap);
	va_end(ap);

	if (msglen > 0) {
		eibnx_log(msgbuf);
	}
}

void
eibnx_dprintf_args(const char *fmt, ...)
{
	va_list ap;
	int msglen;
	char msgbuf[ENX_MAX_LINE];
	char newfmt[ENX_MAX_LINE];

	if ((enx_log_level & ENX_MSGS_ARGS) != ENX_MSGS_ARGS)
		return;

	(void) snprintf(newfmt, ENX_MAX_LINE, "........%s", fmt);

	va_start(ap, fmt);
	msglen = vsnprintf(msgbuf, ENX_MAX_LINE, newfmt, ap);
	va_end(ap);

	if (msglen > 0) {
		eibnx_log(msgbuf);
	}
}

void
eibnx_dprintf_debug(const char *fmt, ...)
{
	va_list ap;
	int msglen;
	char msgbuf[ENX_MAX_LINE];
	char newfmt[ENX_MAX_LINE];

	if ((enx_log_level & ENX_MSGS_DEBUG) != ENX_MSGS_DEBUG)
		return;

	(void) snprintf(newfmt, ENX_MAX_LINE, "......%s", fmt);

	va_start(ap, fmt);
	msglen = vsnprintf(msgbuf, ENX_MAX_LINE, newfmt, ap);
	va_end(ap);

	if (msglen > 0) {
		eibnx_log(msgbuf);
	}
}
#endif

void
eibnx_dprintf_warn(const char *fmt, ...)
{
	va_list ap;
	int msglen;
	char msgbuf[ENX_MAX_LINE];
	char newfmt[ENX_MAX_LINE];

	if ((enx_log_level & ENX_MSGS_WARN) != ENX_MSGS_WARN)
		return;

	(void) snprintf(newfmt, ENX_MAX_LINE, "....%s", fmt);

	va_start(ap, fmt);
	msglen = vsnprintf(msgbuf, ENX_MAX_LINE, newfmt, ap);
	va_end(ap);

	if (msglen > 0) {
		eibnx_log(msgbuf);
	}
}

void
eibnx_dprintf_err(const char *fmt, ...)
{
	va_list ap;
	int msglen;
	char msgbuf[ENX_MAX_LINE];
	char newfmt[ENX_MAX_LINE];

	if ((enx_log_level & ENX_MSGS_ERR) != ENX_MSGS_ERR)
		return;

	(void) snprintf(newfmt, ENX_MAX_LINE, "..%s", fmt);

	va_start(ap, fmt);
	msglen = vsnprintf(msgbuf, ENX_MAX_LINE, newfmt, ap);
	va_end(ap);

	if (msglen > 0) {
		eibnx_log(msgbuf);
		cmn_err(CE_WARN, "!%s\n", msgbuf);
	}
}

void
eibnx_dprintf_crit(const char *fmt, ...)
{
	va_list ap;
	int msglen;
	char msgbuf[ENX_MAX_LINE];

	if ((enx_log_level & ENX_MSGS_CRIT) != ENX_MSGS_CRIT)
		return;

	va_start(ap, fmt);
	msglen = vsnprintf(msgbuf, ENX_MAX_LINE, fmt, ap);
	va_end(ap);

	if (msglen > 0) {
		eibnx_log(msgbuf);
		cmn_err(CE_PANIC, "!%s\n", msgbuf);
	}
}