xref: /titanic_50/usr/src/uts/sun4v/io/dr_util.c (revision 174bc6499d233e329ecd3d98a880a7b07df16bfa)
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 2010 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 #include <sys/memlist.h>
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 struct memlist *
146 dr_memlist_dup(struct memlist *mlist)
147 {
148 	struct memlist *hl = NULL, *tl, **mlp;
149 
150 	if (mlist == NULL)
151 		return (NULL);
152 
153 	mlp = &hl;
154 	tl = *mlp;
155 	for (; mlist; mlist = mlist->ml_next) {
156 		*mlp = 	(struct memlist *)kmem_zalloc(sizeof (struct memlist),\
157 		    KM_SLEEP);
158 		(*mlp)->ml_address = mlist->ml_address;
159 		(*mlp)->ml_size = mlist->ml_size;
160 		(*mlp)->ml_prev = tl;
161 		tl = *mlp;
162 		mlp = &((*mlp)->ml_next);
163 	}
164 	*mlp = NULL;
165 
166 	return (hl);
167 }
168 
169 /*
170  * Free a memlist and its elements
171  */
172 void
173 dr_memlist_delete(struct memlist *mlist)
174 {
175 	register struct memlist *ml;
176 
177 	for (ml = mlist; ml; ml = mlist) {
178 		mlist = ml->ml_next;
179 		kmem_free((void *)ml, sizeof (struct memlist));
180 	}
181 }
182 
183 /*
184  * Debugging Features
185  */
186 #ifdef DEBUG
187 
188 uint_t dr_debug = 0x0;
189 
190 #define	BYTESPERLINE    8
191 #define	LINEWIDTH	((BYTESPERLINE * 3) + (BYTESPERLINE + 2) + 1)
192 #define	ASCIIOFFSET	((BYTESPERLINE * 3) + 2)
193 #define	ISPRINT(c)	((c >= ' ') && (c <= '~'))
194 
195 /*
196  * Output a buffer formatted with a set number of bytes on
197  * each line. Append each line with the ASCII equivalent of
198  * each byte if it falls within the printable ASCII range,
199  * and '.' otherwise.
200  */
201 void
202 dr_dbg_dump_msg(void *buf, size_t len)
203 {
204 	int	i, j;
205 	char	*msg = buf;
206 	char	*curr;
207 	char	*aoff;
208 	char	line[LINEWIDTH];
209 
210 	/* abort if not debugging transport */
211 	if (!(dr_debug & DR_DBG_FLAG_TRANS)) {
212 		return;
213 	}
214 
215 	/* walk the buffer one line at a time */
216 	for (i = 0; i < len; i += BYTESPERLINE) {
217 
218 		bzero(line, LINEWIDTH);
219 
220 		curr = line;
221 		aoff = line + ASCIIOFFSET;
222 
223 		/*
224 		 * Walk the bytes in the current line, storing
225 		 * the hex value for the byte as well as the
226 		 * ASCII representation in a temporary buffer.
227 		 * All ASCII values are placed at the end of
228 		 * the line.
229 		 */
230 		for (j = 0; (j < BYTESPERLINE) && ((i + j) < len); j++) {
231 			(void) sprintf(curr, " %02x", msg[i + j]);
232 			*aoff = (ISPRINT(msg[i + j])) ? msg[i + j] : '.';
233 			curr += 3;
234 			aoff++;
235 		}
236 
237 		/*
238 		 * Fill in to the start of the ASCII translation
239 		 * with spaces. This will only be necessary if
240 		 * this is the last line and there are not enough
241 		 * bytes to fill the whole line.
242 		 */
243 		while (curr != (line + ASCIIOFFSET))
244 			*curr++ = ' ';
245 
246 		DR_DBG_TRANS("%s\n", line);
247 	}
248 }
249 #endif /* DEBUG */
250