#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>
uint_t enx_log_size = ENX_LOGSZ_DEFAULT;
int enx_log_level = ENX_MSGS_DEFAULT | ENX_MSGS_DEBUG;
int enx_log_timestamps = 0;
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;
enx_debug_buf_ndx += msglen;
enx_debug_buf[enx_debug_buf_ndx] = 0;
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);
}
#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);
}
}