xref: /illumos-gate/usr/src/uts/common/io/bge/bge_log.c (revision ddb365bfc9e868ad24ccdcb0dc91af18b10df082)
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 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include "bge_impl.h"
28 
29 
30 /*
31  * Global variable for default debug flags
32  */
33 uint32_t bge_debug;
34 
35 /*
36  * Global mutex used by logging routines below
37  */
38 kmutex_t bge_log_mutex[1];
39 
40 /*
41  * Static data used by logging routines; protected by <bge_log_mutex>
42  */
43 static struct {
44 	const char *who;
45 	const char *fmt;
46 	int level;
47 } bge_log_data;
48 
49 
50 /*
51  * Backend print routine for all the routines below
52  */
53 static void
54 bge_vprt(const char *fmt, va_list args)
55 {
56 	char buf[128];
57 
58 	ASSERT(mutex_owned(bge_log_mutex));
59 
60 	(void) vsnprintf(buf, sizeof (buf), fmt, args);
61 	cmn_err(bge_log_data.level, bge_log_data.fmt, bge_log_data.who, buf);
62 }
63 
64 /*
65  * Log a run-time event (CE_NOTE, log only)
66  */
67 void
68 bge_log(bge_t *bgep, const char *fmt, ...)
69 {
70 	va_list args;
71 
72 	mutex_enter(bge_log_mutex);
73 	bge_log_data.who = bgep->ifname;
74 	bge_log_data.fmt = "!%s: %s";
75 	bge_log_data.level = CE_NOTE;
76 
77 	va_start(args, fmt);
78 	bge_vprt(fmt, args);
79 	va_end(args);
80 
81 	mutex_exit(bge_log_mutex);
82 }
83 
84 /*
85  * Log a run-time problem (CE_WARN, log only)
86  */
87 void
88 bge_problem(bge_t *bgep, const char *fmt, ...)
89 {
90 	va_list args;
91 
92 	mutex_enter(bge_log_mutex);
93 	bge_log_data.who = bgep->ifname;
94 	bge_log_data.fmt = "!%s: %s";
95 	bge_log_data.level = CE_WARN;
96 
97 	va_start(args, fmt);
98 	bge_vprt(fmt, args);
99 	va_end(args);
100 
101 	mutex_exit(bge_log_mutex);
102 }
103 
104 /*
105  * Log a programming error (CE_WARN, log only)
106  */
107 void
108 bge_error(bge_t *bgep, const char *fmt, ...)
109 {
110 	va_list args;
111 
112 	mutex_enter(bge_log_mutex);
113 	bge_log_data.who = bgep->ifname;
114 	bge_log_data.fmt = "!%s: %s";
115 	bge_log_data.level = CE_WARN;
116 
117 	va_start(args, fmt);
118 	bge_vprt(fmt, args);
119 	va_end(args);
120 
121 	mutex_exit(bge_log_mutex);
122 }
123 
124 void
125 bge_fm_ereport(bge_t *bgep, char *detail)
126 {
127 	uint64_t ena;
128 	char buf[FM_MAX_CLASS];
129 
130 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
131 	ena = fm_ena_generate(0, FM_ENA_FMT1);
132 	if (DDI_FM_EREPORT_CAP(bgep->fm_capabilities)) {
133 		ddi_fm_ereport_post(bgep->devinfo, buf, ena, DDI_NOSLEEP,
134 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0, NULL);
135 	}
136 }
137 
138 #if	BGE_DEBUGGING
139 
140 static void
141 bge_prt(const char *fmt, ...)
142 {
143 	va_list args;
144 
145 	ASSERT(mutex_owned(bge_log_mutex));
146 
147 	va_start(args, fmt);
148 	bge_vprt(fmt, args);
149 	va_end(args);
150 
151 	mutex_exit(bge_log_mutex);
152 }
153 
154 void
155 (*bge_gdb(void))(const char *fmt, ...)
156 {
157 	mutex_enter(bge_log_mutex);
158 	bge_log_data.who = "bge";
159 	bge_log_data.fmt = "?%s: %s\n";
160 	bge_log_data.level = CE_CONT;
161 
162 	return (bge_prt);
163 }
164 
165 void
166 (*bge_db(bge_t *bgep))(const char *fmt, ...)
167 {
168 	mutex_enter(bge_log_mutex);
169 	bge_log_data.who = bgep->ifname;
170 	bge_log_data.fmt = "?%s: %s\n";
171 	bge_log_data.level = CE_CONT;
172 
173 	return (bge_prt);
174 }
175 
176 /*
177  * Dump a chunk of memory, 16 bytes at a time
178  */
179 static void
180 minidump(bge_t *bgep, const char *caption, void *dp, uint_t len)
181 {
182 	uint32_t buf[4];
183 	uint32_t nbytes;
184 
185 	bge_log(bgep, "%d bytes of %s at address %p:-", len, caption, dp);
186 
187 	for (len = MIN(len, BGE_STD_BUFF_SIZE); len != 0; len -= nbytes) {
188 		nbytes = MIN(len, sizeof (buf));
189 		bzero(buf, sizeof (buf));
190 		bcopy(dp, buf, nbytes);
191 		bge_log(bgep, "%08x %08x %08x %08x",
192 			buf[0], buf[1], buf[2], buf[3]);
193 		dp = (caddr_t)dp + nbytes;
194 	}
195 }
196 
197 void
198 bge_pkt_dump(bge_t *bgep, bge_rbd_t *hrbdp, sw_rbd_t *srbdp, const char *msg)
199 {
200 	bge_problem(bgep, "driver-detected hardware error: %s", msg);
201 
202 	minidump(bgep, "hardware descriptor", hrbdp, sizeof (*hrbdp));
203 
204 	bge_log(bgep, "PCI address %llx packet len 0x%x token 0x%x "
205 			"flags 0x%x index 0x%x",
206 			hrbdp->host_buf_addr,
207 			hrbdp->len,
208 			hrbdp->opaque,
209 			hrbdp->flags,
210 			hrbdp->index);
211 
212 	if (srbdp != NULL) {
213 		minidump(bgep, "software descriptor", srbdp, sizeof (*srbdp));
214 
215 		bge_log(bgep, "PCI address %llx buffer len 0x%x token 0x%x",
216 			srbdp->pbuf.cookie.dmac_laddress,
217 			srbdp->pbuf.alength,
218 			srbdp->pbuf.token);
219 
220 		minidump(bgep, "packet data", srbdp->pbuf.mem_va, hrbdp->len);
221 	}
222 }
223 
224 void
225 bge_dbg_enter(bge_t *bgep, const char *s)
226 {
227 	uint32_t debug;
228 
229 	debug = bgep != NULL ? bgep->debug : bge_debug;
230 	if (debug & BGE_DBG_STOP) {
231 		cmn_err(CE_CONT, "bge_dbg_enter(%p): %s\n", (void *)bgep, s);
232 		debug_enter("");
233 	}
234 }
235 
236 #endif	/* BGE_DEBUGGING */
237