summaryrefslogtreecommitdiff
path: root/usr/src/uts/sun4v/io/dr_util.c
blob: 534bd82e981672ffc36f39be4e68e6845a39b58e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * sun4v DR Utility functions
 */

#include <sys/types.h>
#include <sys/cmn_err.h>
#include <sys/sunddi.h>
#include <sys/note.h>
#include <sys/sysevent.h>
#include <sys/sysevent/dr.h>
#include <sys/sysevent/eventdefs.h>
#include <sys/ldoms.h>
#include <sys/memlist.h>
#include <sys/dr_util.h>

extern int ppvm_enable;

boolean_t
dr_is_disabled(dr_type_t type)
{
	/*
	 * The type argument is currently unused. However, it
	 * keeps the interface flexible enough to allows for
	 * only disabling certain types of DR.
	 */
	_NOTE(ARGUNUSED(type))

	/*
	 * DR requires that the kernel is using its own CIF
	 * handler. If that is not the case, either because
	 * domaining has been explicitly disabled, or because
	 * the firmware does not support it, the system must
	 * remain static and DR must be disabled.
	 */
	if (!domaining_enabled()) {
		cmn_err(CE_NOTE, "!Kernel CIF handler is not enabled, DR "
		    "is not available\n");
		return (B_TRUE);
	}

	if (type == DR_TYPE_MEM && ppvm_enable == 0) {
		cmn_err(CE_NOTE, "!Memory DR is disabled\n");
		return (B_TRUE);
	}

	return (B_FALSE);
}

/*
 * Generate a DR sysevent based on the type of resource and
 * sysevent hint specified. The hint indicates whether the
 * resource was added or removed.
 */
void
dr_generate_event(dr_type_t type, int se_hint)
{
	int			rv;
	sysevent_id_t		eid;
	sysevent_t		*ev = NULL;
	sysevent_attr_list_t	*evnt_attr_list = NULL;
	sysevent_value_t	evnt_val;
	static char		pubname[] = SUNW_KERN_PUB"dr";

	DR_DBG_ALL("generate_event: type=%s, hint=%s\n", DR_TYPE2STR(type),
	    SE_HINT2STR(se_hint));

	/*
	 * Add the attachment point attribute
	 */
	ev = sysevent_alloc(EC_DR, ESC_DR_AP_STATE_CHANGE, pubname, KM_SLEEP);
	evnt_val.value_type = SE_DATA_TYPE_STRING;
	evnt_val.value.sv_string = DR_TYPE2STR(type);

	rv = sysevent_add_attr(&evnt_attr_list, DR_AP_ID, &evnt_val, KM_SLEEP);
	if (rv != 0) {
		DR_DBG_ALL("generate_event: failed to add attr '%s' for "
		    "'%s' event\n", DR_AP_ID, EC_DR);
		goto done;
	}

	/*
	 * Add the DR hint attribute
	 */
	evnt_val.value_type = SE_DATA_TYPE_STRING;
	evnt_val.value.sv_string = SE_HINT2STR(se_hint);

	rv = sysevent_add_attr(&evnt_attr_list, DR_HINT, &evnt_val, KM_SLEEP);
	if (rv != 0) {
		DR_DBG_ALL("generate_event: failed to add attr '%s' for "
		    "'%s' event\n", DR_HINT, EC_DR);
		sysevent_free_attr(evnt_attr_list);
		goto done;
	}

	/*
	 * Attach the attribute list to the event
	 */
	rv = sysevent_attach_attributes(ev, evnt_attr_list);
	if (rv != 0) {
		DR_DBG_ALL("generate_event: failed to add attr list for "
		    "'%s' event\n", EC_DR);
		sysevent_free_attr(evnt_attr_list);
		goto done;
	}

	/*
	 * Log the event
	 */
	rv = log_sysevent(ev, KM_NOSLEEP, &eid);
	if (rv != 0) {
		DR_DBG_ALL("generate_event: failed to log event (%d)\n", rv);
	}

done:
	if (ev != NULL)
		sysevent_free(ev);
}

struct memlist *
dr_memlist_dup(struct memlist *mlist)
{
	struct memlist *hl = NULL, *tl, **mlp;

	if (mlist == NULL)
		return (NULL);

	mlp = &hl;
	tl = *mlp;
	for (; mlist; mlist = mlist->ml_next) {
		*mlp = 	(struct memlist *)kmem_zalloc(sizeof (struct memlist),\
		    KM_SLEEP);
		(*mlp)->ml_address = mlist->ml_address;
		(*mlp)->ml_size = mlist->ml_size;
		(*mlp)->ml_prev = tl;
		tl = *mlp;
		mlp = &((*mlp)->ml_next);
	}
	*mlp = NULL;

	return (hl);
}

/*
 * Free a memlist and its elements
 */
void
dr_memlist_delete(struct memlist *mlist)
{
	register struct memlist *ml;

	for (ml = mlist; ml; ml = mlist) {
		mlist = ml->ml_next;
		kmem_free((void *)ml, sizeof (struct memlist));
	}
}

/*
 * Debugging Features
 */
#ifdef DEBUG

uint_t dr_debug = 0x0;

#define	BYTESPERLINE    8
#define	LINEWIDTH	((BYTESPERLINE * 3) + (BYTESPERLINE + 2) + 1)
#define	ASCIIOFFSET	((BYTESPERLINE * 3) + 2)
#define	ISPRINT(c)	((c >= ' ') && (c <= '~'))

/*
 * Output a buffer formatted with a set number of bytes on
 * each line. Append each line with the ASCII equivalent of
 * each byte if it falls within the printable ASCII range,
 * and '.' otherwise.
 */
void
dr_dbg_dump_msg(void *buf, size_t len)
{
	int	i, j;
	char	*msg = buf;
	char	*curr;
	char	*aoff;
	char	line[LINEWIDTH];

	/* abort if not debugging transport */
	if (!(dr_debug & DR_DBG_FLAG_TRANS)) {
		return;
	}

	/* walk the buffer one line at a time */
	for (i = 0; i < len; i += BYTESPERLINE) {

		bzero(line, LINEWIDTH);

		curr = line;
		aoff = line + ASCIIOFFSET;

		/*
		 * Walk the bytes in the current line, storing
		 * the hex value for the byte as well as the
		 * ASCII representation in a temporary buffer.
		 * All ASCII values are placed at the end of
		 * the line.
		 */
		for (j = 0; (j < BYTESPERLINE) && ((i + j) < len); j++) {
			(void) sprintf(curr, " %02x", msg[i + j]);
			*aoff = (ISPRINT(msg[i + j])) ? msg[i + j] : '.';
			curr += 3;
			aoff++;
		}

		/*
		 * Fill in to the start of the ASCII translation
		 * with spaces. This will only be necessary if
		 * this is the last line and there are not enough
		 * bytes to fill the whole line.
		 */
		while (curr != (line + ASCIIOFFSET))
			*curr++ = ' ';

		DR_DBG_TRANS("%s\n", line);
	}
}
#endif /* DEBUG */