13859Sml29623 /*
23859Sml29623 * CDDL HEADER START
33859Sml29623 *
43859Sml29623 * The contents of this file are subject to the terms of the
53859Sml29623 * Common Development and Distribution License (the "License").
63859Sml29623 * You may not use this file except in compliance with the License.
73859Sml29623 *
83859Sml29623 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93859Sml29623 * or http://www.opensolaris.org/os/licensing.
103859Sml29623 * See the License for the specific language governing permissions
113859Sml29623 * and limitations under the License.
123859Sml29623 *
133859Sml29623 * When distributing Covered Code, include this CDDL HEADER in each
143859Sml29623 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153859Sml29623 * If applicable, add the following below this CDDL HEADER, with the
163859Sml29623 * fields enclosed by brackets "[]" replaced with your own identifying
173859Sml29623 * information: Portions Copyright [yyyy] [name of copyright owner]
183859Sml29623 *
193859Sml29623 * CDDL HEADER END
203859Sml29623 */
213859Sml29623 /*
226495Sspeer * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
233859Sml29623 * Use is subject to license terms.
243859Sml29623 */
253859Sml29623
263859Sml29623 #pragma ident "%Z%%M% %I% %E% SMI"
273859Sml29623
283859Sml29623 #include <npi.h>
293859Sml29623 #include <sys/nxge/nxge_impl.h>
303859Sml29623
313859Sml29623 nxge_os_mutex_t npidebuglock;
323859Sml29623 int npi_debug_init = 0;
333859Sml29623 uint64_t npi_debug_level = 0;
343859Sml29623
356495Sspeer extern void npi_trace_dump(rtrace_t *, int);
366495Sspeer
373859Sml29623 void
npi_debug_msg(npi_handle_function_t function,uint64_t level,char * fmt,...)383859Sml29623 npi_debug_msg(npi_handle_function_t function, uint64_t level, char *fmt, ...)
393859Sml29623 {
403859Sml29623 char msg_buffer[1024];
413859Sml29623 char prefix_buffer[32];
423859Sml29623 int cmn_level = CE_CONT;
433859Sml29623 va_list ap;
443859Sml29623
453859Sml29623 if ((level & npi_debug_level) ||
46*6929Smisaki (level & NPI_REG_CTL) ||
47*6929Smisaki (level & NPI_ERR_CTL)) {
483859Sml29623
493859Sml29623 if (npi_debug_init == 0) {
503859Sml29623 MUTEX_INIT(&npidebuglock, NULL, MUTEX_DRIVER, NULL);
513859Sml29623 npi_debug_init = 1;
523859Sml29623 }
533859Sml29623
543859Sml29623 MUTEX_ENTER(&npidebuglock);
553859Sml29623
563859Sml29623 if (level & NPI_ERR_CTL) {
573859Sml29623 cmn_level = CE_WARN;
583859Sml29623 }
593859Sml29623
603859Sml29623 va_start(ap, fmt);
613859Sml29623 (void) vsprintf(msg_buffer, fmt, ap);
623859Sml29623 va_end(ap);
633859Sml29623
643859Sml29623 (void) sprintf(prefix_buffer, "%s%d(%d):", "npi",
65*6929Smisaki function.instance, function.function);
663859Sml29623
673859Sml29623 MUTEX_EXIT(&npidebuglock);
683859Sml29623 cmn_err(cmn_level, "!%s %s\n", prefix_buffer, msg_buffer);
693859Sml29623 }
703859Sml29623 }
713859Sml29623
723859Sml29623 void
npi_rtrace_buf_init(rtrace_t * rt)733859Sml29623 npi_rtrace_buf_init(rtrace_t *rt)
743859Sml29623 {
753859Sml29623 int i;
763859Sml29623
773859Sml29623 rt->next_idx = 0;
783859Sml29623 rt->last_idx = MAX_RTRACE_ENTRIES - 1;
793859Sml29623 rt->wrapped = B_FALSE;
803859Sml29623 for (i = 0; i < MAX_RTRACE_ENTRIES; i++) {
813859Sml29623 rt->buf[i].ctl_addr = TRACE_CTL_INVALID;
823859Sml29623 rt->buf[i].val_l32 = 0;
833859Sml29623 rt->buf[i].val_h32 = 0;
843859Sml29623 }
853859Sml29623 }
863859Sml29623
873859Sml29623 void
npi_rtrace_update(npi_handle_t handle,boolean_t wr,rtrace_t * rt,uint32_t addr,uint64_t val)883859Sml29623 npi_rtrace_update(npi_handle_t handle, boolean_t wr, rtrace_t *rt,
896495Sspeer uint32_t addr, uint64_t val)
903859Sml29623 {
913859Sml29623 int idx;
923859Sml29623 idx = rt->next_idx;
933859Sml29623 if (wr == B_TRUE)
943859Sml29623 rt->buf[idx].ctl_addr = (addr & TRACE_ADDR_MASK)
95*6929Smisaki | TRACE_CTL_WR;
963859Sml29623 else
973859Sml29623 rt->buf[idx].ctl_addr = (addr & TRACE_ADDR_MASK);
983859Sml29623 rt->buf[idx].ctl_addr |= (((handle.function.function
99*6929Smisaki << TRACE_FUNC_SHIFT) & TRACE_FUNC_MASK) |
100*6929Smisaki ((handle.function.instance
101*6929Smisaki << TRACE_INST_SHIFT) & TRACE_INST_MASK));
1023859Sml29623 rt->buf[idx].val_l32 = val & 0xFFFFFFFF;
1033859Sml29623 rt->buf[idx].val_h32 = (val >> 32) & 0xFFFFFFFF;
1043859Sml29623 rt->next_idx++;
1053859Sml29623 if (rt->next_idx > rt->last_idx) {
1063859Sml29623 rt->next_idx = 0;
1073859Sml29623 rt->wrapped = B_TRUE;
1083859Sml29623 }
1093859Sml29623 }
1106495Sspeer
1116495Sspeer void
npi_trace_update(npi_handle_t handle,boolean_t wr,rtrace_t * rt,const char * name,uint32_t addr,uint64_t val)1126495Sspeer npi_trace_update(npi_handle_t handle, boolean_t wr, rtrace_t *rt,
1136495Sspeer const char *name, uint32_t addr, uint64_t val)
1146495Sspeer {
1156495Sspeer int idx;
1166495Sspeer idx = rt->next_idx;
1176495Sspeer if (wr == B_TRUE)
1186495Sspeer rt->buf[idx].ctl_addr = (addr & TRACE_ADDR_MASK)
119*6929Smisaki | TRACE_CTL_WR;
1206495Sspeer else
1216495Sspeer rt->buf[idx].ctl_addr = (addr & TRACE_ADDR_MASK);
1226495Sspeer /*
1236495Sspeer * Control Address field format
1246495Sspeer *
1256495Sspeer * Bit 0 - 23: Address
1266495Sspeer * Bit 24 - 25: Function Number
1276495Sspeer * Bit 26 - 29: Instance Number
1286495Sspeer * Bit 30: Read/Write Direction bit
1296495Sspeer * Bit 31: Invalid bit
1306495Sspeer */
1316495Sspeer rt->buf[idx].ctl_addr |= (((handle.function.function
132*6929Smisaki << TRACE_FUNC_SHIFT) & TRACE_FUNC_MASK) |
133*6929Smisaki ((handle.function.instance
134*6929Smisaki << TRACE_INST_SHIFT) & TRACE_INST_MASK));
1356495Sspeer rt->buf[idx].val_l32 = val & 0xFFFFFFFF;
1366495Sspeer rt->buf[idx].val_h32 = (val >> 32) & 0xFFFFFFFF;
1376495Sspeer (void) strncpy(rt->buf[idx].name, name, 15);
1386495Sspeer rt->next_idx++;
1396495Sspeer if (rt->next_idx > rt->last_idx) {
1406495Sspeer rt->next_idx = 0;
1416495Sspeer rt->wrapped = B_TRUE;
1426495Sspeer }
1436495Sspeer }
1446495Sspeer
1456495Sspeer #if defined(NPI_DEBUG)
1466495Sspeer void
npi_trace_dump(rtrace_t * rt,int count)1476495Sspeer npi_trace_dump(
1486495Sspeer rtrace_t *rt,
1496495Sspeer int count)
1506495Sspeer {
1516495Sspeer rt_buf_t *trace;
1526495Sspeer int cursor, i;
1536495Sspeer
1546495Sspeer if (count == 0 || count > MAX_RTRACE_ENTRIES)
1556495Sspeer count = 32;
1566495Sspeer
1576495Sspeer /*
1586495Sspeer * Starting with the last recorded entry,
1596495Sspeer * dump the <count> most recent records.
1606495Sspeer */
1616495Sspeer cursor = rt->next_idx;
1626495Sspeer cursor = (cursor == 0) ? rt->last_idx : cursor - 1;
1636495Sspeer
1646495Sspeer for (i = 0; i < count; i++) {
1656495Sspeer trace = &rt->buf[cursor];
1666495Sspeer if (trace->ctl_addr == 0)
1676495Sspeer break;
1686495Sspeer cmn_err(CE_NOTE, "%16s @ 0x%08x: 0x%08x.%08x: %c",
1696495Sspeer trace->name, trace->ctl_addr,
1706495Sspeer trace->val_h32, trace->val_l32,
1716495Sspeer trace->ctl_addr & TRACE_CTL_WR ? 'W' : 'R');
1726495Sspeer cursor = (cursor == 0) ? rt->last_idx : cursor - 1;
1736495Sspeer }
1746495Sspeer }
1756495Sspeer #endif
176