summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fm/modules/sun4u/cpumem-diagnosis/cmd_dp.c
blob: cf91caa1f594e892dfad8bcdaf2e710e1c9c25cf (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
 * 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 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <strings.h>
#include <string.h>
#include <errno.h>
#include <fm/fmd_api.h>
#include <sys/fm/protocol.h>
#include <sys/async.h>
#include <sys/time.h>
#include <cmd.h>
#include <cmd_state.h>
#include <cmd_mem.h>
#include <cmd_dp.h>
#include <cmd_dp_page.h>
#include <libnvpair.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mem.h>
#include <sys/plat_datapath.h>

/*ARGSUSED*/
static nvlist_t *
dp_cpu_fmri(fmd_hdl_t *hdl, uint32_t cpuid, uint64_t serial_id)
{
	nvlist_t	*nvl = NULL;
	int		err;
	char sbuf[21]; /* sizeof (UINT64_MAX) + '\0' */

	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
		return (NULL);

	err = nvlist_add_string(nvl, FM_FMRI_SCHEME, FM_FMRI_SCHEME_CPU);
	err |= nvlist_add_uint8(nvl, FM_VERSION, FM_CPU_SCHEME_VERSION);
	err |= nvlist_add_uint32(nvl, FM_FMRI_CPU_ID, cpuid);

	/*
	 * Version 1 calls for a string-based serial number
	 */
	(void) snprintf(sbuf, sizeof (sbuf), "%llX", (u_longlong_t)serial_id);
	err |= nvlist_add_string(nvl, FM_FMRI_CPU_SERIAL_ID, sbuf);
	if (err != 0) {
		nvlist_free(nvl);
		return (NULL);
	}
	return (nvl);
}

cmd_dp_t *
cmd_dp_lookup_fault(fmd_hdl_t *hdl, uint32_t cpuid)
{
	cmd_dp_t	*ptr;
	int		i, found = 0;

	/*
	 * Scan the cmd.cmd_datapaths list to see if there is
	 * a fault event present that impacts 'cpuid'
	 */
	for (ptr = cmd_list_next(&cmd.cmd_datapaths); ptr != NULL;
	    ptr = cmd_list_next(ptr)) {
		if (ptr->dp_erpt_type == DP_FAULT) {
			for (i = 0; i < ptr->dp_ncpus; i++) {
				if (ptr->dp_cpuid_list[i] == cpuid) {
					found = 1;
					break;
				}
			}
		}
		if (found)
			break;
	}

	/*
	 * Check if the FMRI for the found cpuid exists in the domain.
	 * If it does not, it implies a DR has been done and this DP_FAULT
	 * is no longer needed.
	 */
	if (ptr != NULL) {
		nvlist_t	*nvl;

		nvl = dp_cpu_fmri(hdl, ptr->dp_cpuid_list[i],
		    ptr->dp_serid_list[i]);

		if (nvl != NULL) {
			if (!fmd_nvl_fmri_present(hdl, nvl)) {
				cmd_dp_destroy(hdl, ptr);
				ptr = NULL;
			}
			nvlist_free(nvl);
		}
	}
	return (ptr);
}

cmd_dp_t *
cmd_dp_lookup_error(cmd_dp_t *dp)
{
	cmd_dp_t	*ptr;

	/*
	 * Scan the cmd.cmd_datapaths list to see if there is
	 * an existing error that matches 'dp'. A match is if
	 * both dp_err and the base cpuid are identical
	 */
	for (ptr = cmd_list_next(&cmd.cmd_datapaths); ptr != NULL;
	    ptr = cmd_list_next(ptr)) {
		if (ptr->dp_erpt_type == DP_ERROR) {
			if ((ptr->dp_err == dp->dp_err) &&
			    (ptr->dp_cpuid_list[0] == dp->dp_cpuid_list[0]))
				return (ptr);
		}
	}
	return (NULL);
}

/*
 * Allocates an nvlist_t, and sets ASRU information according to
 * the cmd_dp_t provided.
 */
/*ARGSUSED*/
nvlist_t *
cmd_dp_setasru(fmd_hdl_t *hdl, cmd_dp_t *dpt)
{
	nvlist_t	*asru, *hcelem[DP_MAX_ASRUS];
	int		i, j, sz, err;
	char		buf[DP_MAX_BUF];

	sz = dpt->dp_ncpus;

	/* put ASRUs in an nvlist */
	for (i = 0; i < sz; i++) {
		(void) snprintf(buf, DP_MAX_BUF, "%d", dpt->dp_cpuid_list[i]);
		if (nvlist_alloc(&hcelem[i], NV_UNIQUE_NAME, 0) != 0)
			return (NULL);

		err = nvlist_add_string(hcelem[i], FM_FMRI_HC_NAME,
		    FM_FMRI_CPU_ID);
		err |= nvlist_add_string(hcelem[i], FM_FMRI_HC_ID, buf);
		if (err != 0) {
			for (j = 0; j < i + 1; j++)
				nvlist_free(hcelem[j]);
			return (NULL);
		}
	}

	/* put it in an HC scheme */
	if (nvlist_alloc(&asru, NV_UNIQUE_NAME, 0) != 0) {
		for (j = 0; j < sz; j++)
			nvlist_free(hcelem[j]);
		return (NULL);
	}
	err = nvlist_add_uint8(asru, FM_VERSION, FM_HC_SCHEME_VERSION);
	err |= nvlist_add_string(asru, FM_FMRI_SCHEME, FM_FMRI_SCHEME_HC);
	err |= nvlist_add_string(asru, FM_FMRI_HC_ROOT, "");
	err |= nvlist_add_uint32(asru, FM_FMRI_HC_LIST_SZ, sz);
	err |= nvlist_add_nvlist_array(asru, FM_FMRI_HC_LIST, &hcelem[0],
	    dpt->dp_ncpus);
	if (err != 0) {
		for (j = 0; j < sz; j++)
			nvlist_free(hcelem[j]);
		nvlist_free(asru);
		return (NULL);
	}

	/* free up memory */
	for (j = 0; j < sz; j++)
		nvlist_free(hcelem[j]);

	/* return the ASRU */
	return (asru);
}

void
dp_buf_write(fmd_hdl_t *hdl, cmd_dp_t *dp)
{
	size_t sz;

	if ((sz = fmd_buf_size(hdl, NULL, dp->dp_bufname)) != 0 &&
	    sz != sizeof (cmd_dp_pers_t))
		fmd_buf_destroy(hdl, NULL, dp->dp_bufname);

	fmd_buf_write(hdl, NULL, dp->dp_bufname, &dp->dp_pers,
	    sizeof (cmd_dp_pers_t));
}

static cmd_dp_t *
dp_wrapv0(fmd_hdl_t *hdl, cmd_dp_pers_t *pers, size_t psz)
{
	cmd_dp_t *dp;

	if (psz != sizeof (cmd_dp_pers_t)) {
		fmd_hdl_abort(hdl, "size of state doesn't match size of "
		    "version 1 state (%u bytes).\n", sizeof (cmd_dp_pers_t));
	}

	dp = fmd_hdl_zalloc(hdl, sizeof (cmd_dp_t), FMD_SLEEP);
	bcopy(pers, dp, sizeof (cmd_dp_pers_t));
	fmd_hdl_free(hdl, pers, psz);
	return (dp);
}

void *
cmd_dp_restore(fmd_hdl_t *hdl, fmd_case_t *cp, cmd_case_ptr_t *ptr)
{
	cmd_dp_t *dp;

	for (dp = cmd_list_next(&cmd.cmd_datapaths); dp != NULL;
	    dp = cmd_list_next(dp)) {
		if (dp->dp_case == cp)
			break;
	}

	if (dp == NULL) {
		size_t dpsz;

		fmd_hdl_debug(hdl, "restoring dp from %s\n", ptr->ptr_name);

		if ((dpsz = fmd_buf_size(hdl, NULL, ptr->ptr_name)) == 0) {
			if (fmd_case_solved(hdl, cp) ||
			    fmd_case_closed(hdl, cp)) {
				fmd_hdl_debug(hdl, "dp %s from case %s not "
				    "found. Case is already solved or closed\n",
				    ptr->ptr_name, fmd_case_uuid(hdl, cp));
				return (NULL);
			} else {
				fmd_hdl_abort(hdl, "dp referenced by case %s "
				    "does not exist in saved state\n",
				    fmd_case_uuid(hdl, cp));
			}
		} else if (dpsz > CMD_DP_MAXSIZE ||
		    dpsz < CMD_DP_MINSIZE) {
			fmd_hdl_abort(hdl, "dp buffer referenced by "
			    "case %s is out of bounds (is %u bytes, "
			    "max %u, min %u)\n", fmd_case_uuid(hdl, cp),
			    dpsz, CMD_DP_MAXSIZE, CMD_DP_MINSIZE);
		}

		if ((dp = cmd_buf_read(hdl, NULL, ptr->ptr_name, dpsz)) == NULL)
			fmd_hdl_abort(hdl, "failed to read dp buf %s",
			    ptr->ptr_name);

		switch (dp->dp_version) {
		case CMD_DP_VERSION_0:
			dp = dp_wrapv0(hdl, (cmd_dp_pers_t *)dp, dpsz);
			break;
		default:
			fmd_hdl_abort(hdl, "unknown version (found %d) "
			    "for dp state referenced by case %s.\n",
			    dp->dp_version, fmd_case_uuid(hdl, cp));
			break;
		}

		dp->dp_case = cp;

		if (dp->dp_erpt_type == DP_ERROR) {
			fmd_event_t *ep = fmd_case_getprincipal(hdl, cp);

			++cmd.cmd_dp_flag;

			dp->dp_id = fmd_timer_install(hdl,
			    (void *)CMD_TIMERTYPE_DP, ep,
			    (hrtime_t)NANOSEC * (dp->dp_t_value + 120));
		}

		cmd_list_append(&cmd.cmd_datapaths, dp);
	}

	return (dp);
}

void
cmd_dp_close(fmd_hdl_t *hdl, void *arg)
{
	cmd_dp_destroy(hdl, arg);
}

void
cmd_dp_timeout(fmd_hdl_t *hdl, id_t id)
{
	cmd_dp_t		*dp;

	/* close case associated with the timer */
	for (dp = cmd_list_next(&cmd.cmd_datapaths); dp != NULL;
	    dp = cmd_list_next(dp)) {
		if (dp->dp_id == id) {
			cmd_dp_destroy(hdl, dp);
			break;
		}
	}

	fmd_hdl_debug(hdl, "cmd_dp_timeout() complete\n");
}

/*
 * Validate by matching each cmd_dp_t cpu and serial id to what is
 * installed and active on this machine or domain. Delete the cmd_dp_t
 * if no match is made.
 */
void
cmd_dp_validate(fmd_hdl_t *hdl)
{
	cmd_dp_t *dp, *next;
	nvlist_t *nvl;
	int i, no_match;

	for (dp = cmd_list_next(&cmd.cmd_datapaths); dp != NULL; dp = next) {
		next = cmd_list_next(dp);

		for (i = 0, no_match = 0; i < dp->dp_ncpus; i++) {
			nvl = dp_cpu_fmri(hdl, dp->dp_cpuid_list[i],
			    dp->dp_serid_list[i]);

			if (nvl == NULL)
				fmd_hdl_abort(hdl, "could not make CPU fmri");

			if (!fmd_nvl_fmri_present(hdl, nvl))
				no_match = 1;

			nvlist_free(nvl);

			if (no_match) {
				cmd_dp_destroy(hdl, dp);
				break;
			}
		}
	}
}

static void
cmd_dp_free(fmd_hdl_t *hdl, cmd_dp_t *dp, int destroy)
{
	if (dp->dp_case != NULL)
		cmd_case_fini(hdl, dp->dp_case, destroy);

	if (destroy && dp->dp_erpt_type == DP_ERROR) {
		--cmd.cmd_dp_flag;
		/*
		 * If there are no active datapath events, replay any
		 * pages that were deferred.
		 */
		if (cmd.cmd_dp_flag == 0)
			cmd_dp_page_replay(hdl);
	}

	if (destroy)
		fmd_buf_destroy(hdl, NULL, dp->dp_bufname);

	cmd_list_delete(&cmd.cmd_datapaths, dp);
	fmd_hdl_free(hdl, dp, sizeof (cmd_dp_t));
}

void
cmd_dp_destroy(fmd_hdl_t *hdl, cmd_dp_t *dp)
{
	cmd_dp_free(hdl, dp, FMD_B_TRUE);
}

/*ARGSUSED*/
int
cmd_dp_error(fmd_hdl_t *hdl)
{
	if (cmd.cmd_dp_flag)
		return (1);
	else
		return (0);
}

int
cmd_dp_get_mcid(uint64_t addr, int *mcid)
{
	int fd, rc;
	mem_info_t data;

	if ((fd = open("/dev/mem", O_RDONLY)) < 0)
		return (-1);

	data.m_addr = addr;
	data.m_synd = 0;
	if ((rc = ioctl(fd, MEM_INFO, &data)) < 0) {
		(void) close(fd);
		return (rc);
	}

	(void) close(fd);
	*mcid = data.m_mcid;

	return (0);
}

/*ARGSUSED*/
int
cmd_dp_fault(fmd_hdl_t *hdl, uint64_t addr)
{
	int mcid;

	if (cmd_dp_get_mcid(addr, &mcid) < 0)
		fmd_hdl_abort(hdl, "cmd_dp_get_mcid failed");

	if (cmd_dp_lookup_fault(hdl, mcid) != NULL)
		return (1);
	else
		return (0);
}

void
cmd_dp_fini(fmd_hdl_t *hdl)
{
	cmd_dp_t *dp;
	cmd_dp_defer_t *dpage;

	while ((dp = cmd_list_next(&cmd.cmd_datapaths)) != NULL)
		cmd_dp_free(hdl, dp, FMD_B_FALSE);

	while ((dpage = cmd_list_next(&cmd.cmd_deferred_pages)) != NULL) {
		cmd_list_delete(&cmd.cmd_deferred_pages, dpage);
		fmd_hdl_free(hdl, dpage, sizeof (cmd_dp_defer_t));
	}
}