Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / uts / common / io / ib / clients / eoib / eib_log.c
blobf57f040eafc8055a694e34594afa191f7f4b5cfa
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <sys/types.h>
27 #include <sys/kmem.h>
28 #include <sys/cmn_err.h>
29 #include <sys/conf.h>
30 #include <sys/ddi.h>
31 #include <sys/sunddi.h>
32 #include <sys/ksynch.h>
33 #include <sys/varargs.h>
35 #include <sys/ib/clients/eoib/eib_impl.h>
38 * Defaults
40 uint_t eib_log_size = EIB_LOGSZ_DEFAULT;
41 int eib_log_level = EIB_MSGS_DEFAULT | EIB_MSGS_DEBUG;
42 int eib_log_timestamps = 0;
45 * Debug variables, should not be tunables so allocated debug buffer
46 * and its size remain consistent.
48 static kmutex_t eib_debug_buf_lock;
49 static uint8_t *eib_debug_buf;
50 static uint32_t eib_debug_buf_ndx;
51 static uint_t eib_debug_buf_sz = 0;
54 * Local declarations
56 static void eib_log(char *);
58 void
59 eib_debug_init(void)
61 eib_debug_buf_ndx = 0;
62 eib_debug_buf_sz = eib_log_size;
63 eib_debug_buf = kmem_zalloc(eib_debug_buf_sz, KM_SLEEP);
65 mutex_init(&eib_debug_buf_lock, NULL, MUTEX_DRIVER, NULL);
68 void
69 eib_debug_fini(void)
71 mutex_destroy(&eib_debug_buf_lock);
73 if (eib_debug_buf && eib_debug_buf_sz) {
74 kmem_free(eib_debug_buf, eib_debug_buf_sz);
75 eib_debug_buf = NULL;
77 eib_debug_buf_sz = 0;
78 eib_debug_buf_ndx = 0;
81 void
82 eib_log(char *msg)
84 uint32_t off;
85 int msglen;
86 char msgbuf[EIB_MAX_LINE];
88 if (eib_debug_buf == NULL)
89 return;
91 if (eib_log_timestamps) {
92 msglen = snprintf(msgbuf, EIB_MAX_LINE, "%llx: %s",
93 (unsigned long long)ddi_get_lbolt64(), msg);
94 } else {
95 msglen = snprintf(msgbuf, EIB_MAX_LINE, "%s", msg);
98 if (msglen < 0)
99 return;
100 else if (msglen >= EIB_MAX_LINE)
101 msglen = EIB_MAX_LINE - 1;
103 mutex_enter(&eib_debug_buf_lock);
104 if ((eib_debug_buf_ndx == 0) ||
105 (eib_debug_buf[eib_debug_buf_ndx-1] != '\n')) {
106 eib_debug_buf[eib_debug_buf_ndx] = '\n';
107 eib_debug_buf_ndx++;
110 off = eib_debug_buf_ndx; /* current msg should go here */
112 eib_debug_buf_ndx += msglen; /* next msg should start here */
113 eib_debug_buf[eib_debug_buf_ndx] = 0; /* terminate current msg */
115 if (eib_debug_buf_ndx >= (eib_debug_buf_sz - 2 * EIB_MAX_LINE))
116 eib_debug_buf_ndx = 0;
118 mutex_exit(&eib_debug_buf_lock);
120 bcopy(msgbuf, eib_debug_buf+off, msglen); /* no lock needed */
123 #ifdef EIB_DEBUG
124 void
125 eib_dprintf_verbose(int inst, const char *fmt, ...)
127 va_list ap;
128 int msglen;
129 char msgbuf[EIB_MAX_LINE];
130 char newfmt[EIB_MAX_LINE];
132 if ((eib_log_level & EIB_MSGS_VERBOSE) != EIB_MSGS_VERBOSE)
133 return;
135 (void) snprintf(newfmt, EIB_MAX_LINE, "eoib%d__%s", inst, fmt);
137 va_start(ap, fmt);
138 msglen = vsnprintf(msgbuf, EIB_MAX_LINE, newfmt, ap);
139 va_end(ap);
141 if (msglen > 0) {
142 eib_log(msgbuf);
146 void
147 eib_dprintf_pkt(int inst, uint8_t *pkt, uint_t sz)
149 char msgbuf[EIB_MAX_LINE];
150 char *bufp;
151 uint8_t *p = pkt;
152 uint_t len;
153 uint_t i;
155 if ((eib_log_level & EIB_MSGS_PKT) != EIB_MSGS_PKT)
156 return;
158 while (sz >= 16) {
159 (void) snprintf(msgbuf, EIB_MAX_LINE,
160 "eoib%02d__%02x %02x %02x %02x %02x %02x %02x %02x "
161 "%02x %02x %02x %02x %02x %02x %02x %02x\n", inst,
162 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
163 p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
165 eib_log(msgbuf);
167 p += 16;
168 sz -= 16;
171 len = EIB_MAX_LINE;
172 bufp = msgbuf;
173 for (i = 0; i < sz; i++) {
174 if (i == 0) {
175 (void) snprintf(bufp, len, "eoib%02d__%02x ",
176 inst, p[i]);
177 len -= 11;
178 bufp += 11;
179 } else if (i < (sz - 1)) {
180 (void) snprintf(bufp, len, "%02x ", p[i]);
181 len -= 3;
182 bufp += 3;
183 } else {
184 (void) snprintf(bufp, len, "%02x\n", p[i]);
185 len -= 3;
186 bufp += 3;
190 eib_log(msgbuf);
193 void
194 eib_dprintf_args(int inst, const char *fmt, ...)
196 va_list ap;
197 int msglen;
198 char msgbuf[EIB_MAX_LINE];
199 char newfmt[EIB_MAX_LINE];
201 if ((eib_log_level & EIB_MSGS_ARGS) != EIB_MSGS_ARGS)
202 return;
204 (void) snprintf(newfmt, EIB_MAX_LINE, "eoib%d__%s", inst, fmt);
206 va_start(ap, fmt);
207 msglen = vsnprintf(msgbuf, EIB_MAX_LINE, newfmt, ap);
208 va_end(ap);
210 if (msglen > 0) {
211 eib_log(msgbuf);
215 void
216 eib_dprintf_debug(int inst, const char *fmt, ...)
218 va_list ap;
219 int msglen;
220 char msgbuf[EIB_MAX_LINE];
221 char newfmt[EIB_MAX_LINE];
223 if ((eib_log_level & EIB_MSGS_DEBUG) != EIB_MSGS_DEBUG)
224 return;
226 (void) snprintf(newfmt, EIB_MAX_LINE, "eoib%d__%s", inst, fmt);
228 va_start(ap, fmt);
229 msglen = vsnprintf(msgbuf, EIB_MAX_LINE, newfmt, ap);
230 va_end(ap);
232 if (msglen > 0) {
233 eib_log(msgbuf);
236 #endif
238 void
239 eib_dprintf_warn(int inst, const char *fmt, ...)
241 va_list ap;
242 int msglen;
243 char msgbuf[EIB_MAX_LINE];
244 char newfmt[EIB_MAX_LINE];
246 if ((eib_log_level & EIB_MSGS_WARN) != EIB_MSGS_WARN)
247 return;
249 (void) snprintf(newfmt, EIB_MAX_LINE, "eoib%d__%s", inst, fmt);
251 va_start(ap, fmt);
252 msglen = vsnprintf(msgbuf, EIB_MAX_LINE, newfmt, ap);
253 va_end(ap);
255 if (msglen > 0) {
256 eib_log(msgbuf);
260 void
261 eib_dprintf_err(int inst, const char *fmt, ...)
263 va_list ap;
264 int msglen;
265 char msgbuf[EIB_MAX_LINE];
266 char newfmt[EIB_MAX_LINE];
268 if ((eib_log_level & EIB_MSGS_ERR) != EIB_MSGS_ERR)
269 return;
271 (void) snprintf(newfmt, EIB_MAX_LINE, "eoib%d__%s", inst, fmt);
273 va_start(ap, fmt);
274 msglen = vsnprintf(msgbuf, EIB_MAX_LINE, newfmt, ap);
275 va_end(ap);
277 if (msglen > 0) {
278 eib_log(msgbuf);
279 cmn_err(CE_WARN, "!%s\n", msgbuf);
283 void
284 eib_dprintf_crit(int inst, const char *fmt, ...)
286 va_list ap;
287 int msglen;
288 char msgbuf[EIB_MAX_LINE];
289 char newfmt[EIB_MAX_LINE];
291 if ((eib_log_level & EIB_MSGS_CRIT) != EIB_MSGS_CRIT)
292 return;
294 (void) snprintf(newfmt, EIB_MAX_LINE, "eoib%d__%s", inst, fmt);
296 va_start(ap, fmt);
297 msglen = vsnprintf(msgbuf, EIB_MAX_LINE, newfmt, ap);
298 va_end(ap);
300 if (msglen > 0) {
301 eib_log(msgbuf);
302 cmn_err(CE_PANIC, "!%s\n", msgbuf);