xref: /illumos-gate/usr/src/uts/sun4v/io/dr_util.c (revision 93a18d6d401e844455263f926578e9d2aa6b47ec)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * sun4v DR Utility functions
29  */
30 
31 #include <sys/types.h>
32 #include <sys/cmn_err.h>
33 #include <sys/sunddi.h>
34 #include <sys/note.h>
35 #include <sys/sysevent.h>
36 #include <sys/sysevent/dr.h>
37 #include <sys/sysevent/eventdefs.h>
38 #include <sys/ldoms.h>
39 
40 #include <sys/dr_util.h>
41 
42 extern int ppvm_enable;
43 
44 boolean_t
45 dr_is_disabled(dr_type_t type)
46 {
47 	/*
48 	 * The type argument is currently unused. However, it
49 	 * keeps the interface flexible enough to allows for
50 	 * only disabling certain types of DR.
51 	 */
52 	_NOTE(ARGUNUSED(type))
53 
54 	/*
55 	 * DR requires that the kernel is using its own CIF
56 	 * handler. If that is not the case, either because
57 	 * domaining has been explicitly disabled, or because
58 	 * the firmware does not support it, the system must
59 	 * remain static and DR must be disabled.
60 	 */
61 	if (!domaining_enabled()) {
62 		cmn_err(CE_NOTE, "!Kernel CIF handler is not enabled, DR "
63 		    "is not available\n");
64 		return (B_TRUE);
65 	}
66 
67 	if (type == DR_TYPE_MEM && ppvm_enable == 0) {
68 		cmn_err(CE_NOTE, "!Memory DR is disabled\n");
69 		return (B_TRUE);
70 	}
71 
72 	return (B_FALSE);
73 }
74 
75 /*
76  * Generate a DR sysevent based on the type of resource and
77  * sysevent hint specified. The hint indicates whether the
78  * resource was added or removed.
79  */
80 void
81 dr_generate_event(dr_type_t type, int se_hint)
82 {
83 	int			rv;
84 	sysevent_id_t		eid;
85 	sysevent_t		*ev = NULL;
86 	sysevent_attr_list_t	*evnt_attr_list = NULL;
87 	sysevent_value_t	evnt_val;
88 	static char		pubname[] = SUNW_KERN_PUB"dr";
89 
90 	DR_DBG_ALL("generate_event: type=%s, hint=%s\n", DR_TYPE2STR(type),
91 	    SE_HINT2STR(se_hint));
92 
93 	/*
94 	 * Add the attachment point attribute
95 	 */
96 	ev = sysevent_alloc(EC_DR, ESC_DR_AP_STATE_CHANGE, pubname, KM_SLEEP);
97 	evnt_val.value_type = SE_DATA_TYPE_STRING;
98 	evnt_val.value.sv_string = DR_TYPE2STR(type);
99 
100 	rv = sysevent_add_attr(&evnt_attr_list, DR_AP_ID, &evnt_val, KM_SLEEP);
101 	if (rv != 0) {
102 		DR_DBG_ALL("generate_event: failed to add attr '%s' for "
103 		    "'%s' event\n", DR_AP_ID, EC_DR);
104 		goto done;
105 	}
106 
107 	/*
108 	 * Add the DR hint attribute
109 	 */
110 	evnt_val.value_type = SE_DATA_TYPE_STRING;
111 	evnt_val.value.sv_string = SE_HINT2STR(se_hint);
112 
113 	rv = sysevent_add_attr(&evnt_attr_list, DR_HINT, &evnt_val, KM_SLEEP);
114 	if (rv != 0) {
115 		DR_DBG_ALL("generate_event: failed to add attr '%s' for "
116 		    "'%s' event\n", DR_HINT, EC_DR);
117 		sysevent_free_attr(evnt_attr_list);
118 		goto done;
119 	}
120 
121 	/*
122 	 * Attach the attribute list to the event
123 	 */
124 	rv = sysevent_attach_attributes(ev, evnt_attr_list);
125 	if (rv != 0) {
126 		DR_DBG_ALL("generate_event: failed to add attr list for "
127 		    "'%s' event\n", EC_DR);
128 		sysevent_free_attr(evnt_attr_list);
129 		goto done;
130 	}
131 
132 	/*
133 	 * Log the event
134 	 */
135 	rv = log_sysevent(ev, KM_NOSLEEP, &eid);
136 	if (rv != 0) {
137 		DR_DBG_ALL("generate_event: failed to log event (%d)\n", rv);
138 	}
139 
140 done:
141 	if (ev != NULL)
142 		sysevent_free(ev);
143 }
144 
145 /*
146  * Debugging Features
147  */
148 #ifdef DEBUG
149 
150 uint_t dr_debug = 0x0;
151 
152 #define	BYTESPERLINE    8
153 #define	LINEWIDTH	((BYTESPERLINE * 3) + (BYTESPERLINE + 2) + 1)
154 #define	ASCIIOFFSET	((BYTESPERLINE * 3) + 2)
155 #define	ISPRINT(c)	((c >= ' ') && (c <= '~'))
156 
157 /*
158  * Output a buffer formatted with a set number of bytes on
159  * each line. Append each line with the ASCII equivalent of
160  * each byte if it falls within the printable ASCII range,
161  * and '.' otherwise.
162  */
163 void
164 dr_dbg_dump_msg(void *buf, size_t len)
165 {
166 	int	i, j;
167 	char	*msg = buf;
168 	char	*curr;
169 	char	*aoff;
170 	char	line[LINEWIDTH];
171 
172 	/* abort if not debugging transport */
173 	if (!(dr_debug & DR_DBG_FLAG_TRANS)) {
174 		return;
175 	}
176 
177 	/* walk the buffer one line at a time */
178 	for (i = 0; i < len; i += BYTESPERLINE) {
179 
180 		bzero(line, LINEWIDTH);
181 
182 		curr = line;
183 		aoff = line + ASCIIOFFSET;
184 
185 		/*
186 		 * Walk the bytes in the current line, storing
187 		 * the hex value for the byte as well as the
188 		 * ASCII representation in a temporary buffer.
189 		 * All ASCII values are placed at the end of
190 		 * the line.
191 		 */
192 		for (j = 0; (j < BYTESPERLINE) && ((i + j) < len); j++) {
193 			(void) sprintf(curr, " %02x", msg[i + j]);
194 			*aoff = (ISPRINT(msg[i + j])) ? msg[i + j] : '.';
195 			curr += 3;
196 			aoff++;
197 		}
198 
199 		/*
200 		 * Fill in to the start of the ASCII translation
201 		 * with spaces. This will only be necessary if
202 		 * this is the last line and there are not enough
203 		 * bytes to fill the whole line.
204 		 */
205 		while (curr != (line + ASCIIOFFSET))
206 			*curr++ = ' ';
207 
208 		DR_DBG_TRANS("%s\n", line);
209 	}
210 }
211 #endif /* DEBUG */
212