summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fm/schemes/mem/mem_unum.c
blob: 51c9708878f7687ffe713b77f28388d4c4dc0862 (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
443
444
445
446
447
448
449
450
/*
 * 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.
 */

/*
 * Copyright 2019 Peter Tribble.
 */

#include <mem.h>
#include <fm/fmd_fmri.h>
#include <fm/libtopo.h>

#include <string.h>
#include <strings.h>
#include <ctype.h>

#define	ISHCUNUM(unum) (strncmp(unum, "hc:/", 4) == 0)

/*
 * Given a DIMM or bank unum, mem_unum_burst will break it apart into individual
 * DIMM names.  If it's a DIMM, one name will be returned.  If it's a bank, the
 * unums for the individual DIMMs will be returned.
 *
 * Plain J-number DIMM and bank unums are simple.  J DIMMs have one J number.  J
 * banks have multiple whitespace-separated J numbers.
 *
 * The others are more complex, and consist of a common portion c, a colon, and
 * a DIMM-specific portion d.  DIMMs are of the form "c: d", while banks are of
 * the form "c: d d ...".  The patterns are designed to handle the complex case,
 * but also handle the simple ones as an afterthought.  bd_pat is used to
 * match specific styles of unum.  In bd_pat, the first %n indicates the end of
 * the common portion ("c" above).  The second %n marks the beginning of the
 * repetitive portion ("d" above).  The third %n is used to determine whether or
 * not the entire pattern matched.  bd_reppat is used to match instances of the
 * repetitive part.
 *
 * sscanf is your disturbingly powerful friend.
 *
 * The "bd_subst" element of the bank_dimm structure was added for Ontario
 * in order to accommodate its bank string names.  Previously, to convert
 * from a bank representation <common piece> <dimm1> <dimm2> ...
 * we concatenated the common piece with each dimm-specific piece in turn,
 * possibly deleting some characters in between.  Ontario is the first
 * platform which requires that characters be substituted (like a vi s/1/2/)
 * in place of characters deleted.  "bd_subst" represents the character(s)
 * to be substituted between the common piece and each dimm-specific piece
 * as part of the bursting.  For prior platforms, this value is skipped.
 *
 * Example:
 * input: "MB/CMP0/CH3: R1/D0/J1901 R1/D1/J2001"
 * outputs: "MB/CMP0/CH3/R1/D0/J1901", "MB/CMP0/CH3/R1/D1/J2001"
 */

typedef struct bank_dimm {
	const char *bd_pat;
	const char *bd_reppat;
	const char *bd_subst;
} bank_dimm_t;

static const bank_dimm_t bank_dimm[] = {
	{ "%n%nJ%*4d%n",			" J%*4d%n" },
	{ "MB/P%*d/%nB%*d:%n%n",		" B%*d/D%*d%n" },
	{ "MB/P%*d/%nB%*d/D%*d:%n%n",		" B%*d/D%*d%n" },
	{ "C%*d/P%*d/%nB%*d:%n%n",		" B%*d/D%*d%n" },
	{ "C%*d/P%*d/%nB%*d/D%*d:%n%n",		" B%*d/D%*d%n" },
	{ "Slot %*c: %n%nJ%*4d%n",		" J%*4d%n" },
	{ "%n%nDIMM%*d%n",			" DIMM%*d%n" },
	{ "MB/%nDIMM%*d MB/DIMM%*d: %n%n",	" DIMM%*d%n" },
	{ "MB/%nDIMM%*d:%n%n",			" DIMM%*d%n" },
	{ "MB/CMP%*d/CH%*d%n:%n%n",		" R%*d/D%*d/J%*4d%n",	"/" },
	{ "MB/CMP%*d/CH%*d%n%n%n",		"/R%*d/D%*d/J%*4d%n" },
	{ "MB/C%*d/P%*d/%nB%*d:%n%n",		" B%*d/D%*d%n" },
	{ "MB/C%*d/P%*d/%nB%*d/D%*d:%n%n",	" B%*d/D%*d%n" },
	{ "/MBU_A/MEMB%*d/%n%nMEM%*d%*1c%n",	" MEM%*d%*1c%n" },
	{ "/MBU_B/MEMB%*d/%n%nMEM%*d%*1c%n",	" MEM%*d%*1c%n" },
	{ "/MBU_A/%n%nMEM%*d%*1c%n",		" MEM%*d%*1c%n" },
	{ "/CMU%*2d/%n%nMEM%*2d%*1c%n",		" MEM%*2d%*1c%n" },
	{ "MB/CMP%*d/BR%*d%n:%n%n",		" CH%*d/D%*d/J%*4d%n", "/" },
	{ "%n%nMB/CMP%*d/BR%*d/CH%*d/D%*d/J%*4d%n",
	    "MB/CMP%*d/BR%*d/CH%*d/D%*d/J%*4d%n" },
	{ "%n%nMB/CMP%*d/BR%*d/CH%*d/D%*d%n", "MB/CMP%*d/BR%*d/CH%*d/D%*d%n" },
	{ "MB/CPU%*d/CMP%*d/BR%*d%n:%n%n",	" CH%*d/D%*d/J%*4d%n", "/"},
	{ "MB/MEM%*d/CMP%*d/BR%*d%n:%n%n",	" CH%*d/D%*d/J%*4d%n", "/"},
	{ "%n%nMB/MEM%*d/CMP%*d/BR%*d/CH%*d/D%*d/J%*4d%n",
	    "MB/MEM%*d/CMP%*d/BR%*d/CH%*d/D%*d/J%*4d%n" },
	{ "%n%nMB/CPU%*d/CMP%*d/BR%*d/CH%*d/D%*d/J%*4d%n",
	    "MB/CPU%*d/CMP%*d/BR%*d/CH%*d/D%*d/J%*4d%n" },
	{ "%n%nMB/MEM%*d/CMP%*d/BR%*d/CH%*d/D%*d%n",
	    "MB/MEM%*d/CMP%*d/BR%*d/CH%*d/D%*d%n"  },
	{ "%n%nMB/CPU%*d/CMP%*d/BR%*d/CH%*d/D%*d%n",
	    "MB/CPU%*d/CMP%*d/BR%*d/CH%*d/D%*d%n"  },
	{ NULL }
};

/*
 * Burst Serengeti-style unums.
 * A DIMM unum string is expected to be in this form:
 * "[/N0/]SB12/P0/B0/D2 [J13500]"
 * A bank unum string is expected to be in this form:
 * "[/N0/]SB12/P0/B0 [J13500, ...]"
 */
static int
mem_unum_burst_sgsc(const char *pat, char ***dimmsp, size_t *ndimmsp)
{
	char buf[64];
	char **dimms;
	char *base;
	const char *c;
	char *copy;
	size_t copysz;
	int i;

	/*
	 * No expansion is required for a DIMM unum
	 */
	if (strchr(pat, 'D') != NULL) {
		dimms = fmd_fmri_alloc(sizeof (char *));
		dimms[0] = fmd_fmri_strdup(pat);
		*dimmsp = dimms;
		*ndimmsp = 1;
		return (0);
	}

	/*
	 * strtok is destructive so we need to work with
	 * a copy and keep track of the size allocated.
	 */
	copysz = strlen(pat) + 1;
	copy = fmd_fmri_alloc(copysz);
	(void) strcpy(copy, pat);

	base = strtok(copy, " ");

	/* There are four DIMMs in a bank */
	dimms = fmd_fmri_alloc(sizeof (char *) * 4);

	for (i = 0; i < 4; i++) {
		(void) snprintf(buf, sizeof (buf), "%s/D%d", base, i);

		if ((c = strtok(NULL, " ")) != NULL)
			(void) snprintf(buf, sizeof (buf), "%s %s", buf, c);

		dimms[i] = fmd_fmri_strdup(buf);
	}

	fmd_fmri_free(copy, copysz);

	*dimmsp = dimms;
	*ndimmsp = 4;
	return (0);
}


/*
 * Returns 0 (with dimmsp and ndimmsp set) if the unum could be bursted, -1
 * otherwise.
 */
static int
mem_unum_burst_pattern(const char *pat, char ***dimmsp, size_t *ndimmsp)
{
	const bank_dimm_t *bd;
	char **dimms = NULL, **newdimms;
	size_t ndimms = 0;
	const char *c;


	for (bd = bank_dimm; bd->bd_pat != NULL; bd++) {
		int replace, start, matched;
		char dimmname[64];

		replace = start = matched = -1;
		(void) sscanf(pat, bd->bd_pat, &replace, &start, &matched);
		if (matched == -1)
			continue;
		(void) strlcpy(dimmname, pat, sizeof (dimmname));
		if (bd->bd_subst != NULL) {
			(void) strlcpy(dimmname+replace, bd->bd_subst,
			    sizeof (dimmname) - strlen(bd->bd_subst));
			replace += strlen(bd->bd_subst);
		}

		c = pat + start;
		while (*c != '\0') {
			int dimmlen = -1;

			(void) sscanf(c, bd->bd_reppat, &dimmlen);
			if (dimmlen == -1)
				break;

			while (*c == ' ') {
				c++;
				dimmlen--;
			}

			if (dimmlen > sizeof (dimmname) - replace)
				break;

			(void) strlcpy(dimmname + replace, c, dimmlen + 1);

			newdimms = fmd_fmri_alloc(sizeof (char *) *
			    (ndimms + 1));
			if (ndimms != 0) {
				bcopy(dimms, newdimms, sizeof (char *) *
				    ndimms);
				fmd_fmri_free(dimms, sizeof (char *) * ndimms);
			}
			newdimms[ndimms++] = fmd_fmri_strdup(dimmname);
			dimms = newdimms;

			c += dimmlen;

			if (*c != ' ' && *c != '\0')
				break;
		}

		if (*c != '\0')
			break;

		*dimmsp = dimms;
		*ndimmsp = ndimms;

		return (0);
	}

	mem_strarray_free(dimms, ndimms);

	/*
	 * Set errno to ENOTSUP and return -1. This allows support for DIMMs
	 * with unknown unum strings and/or serial numbers. The only consumer
	 * of mem_unum_burst_pattern() that cares/checks for the returned
	 * errno is fmd_fmri_expand().
	 */
	return (fmd_fmri_set_errno(ENOTSUP));
}

int
mem_unum_burst(const char *pat, char ***dimmsp, size_t *ndimmsp)
{
	const char *platform = fmd_fmri_get_platform();

	/*
	 * Call mem_unum_burst_sgsc() for Serengeti and
	 * Lightweight 8 platforms.  Call mem_unum_burst_pattern()
	 * for all other platforms.
	 */
	if (strcmp(platform, "SUNW,Sun-Fire") == 0 ||
	    strcmp(platform, "SUNW,Netra-T12") == 0)
		return (mem_unum_burst_sgsc(pat, dimmsp, ndimmsp));
	else
		return (mem_unum_burst_pattern(pat, dimmsp, ndimmsp));
}

/*
 * The unum containership operation is designed to tell the caller whether a
 * given FMRI contains another.  In the case of this plugin, we tell the caller
 * whether a given memory FMRI (usually a bank) contains another (usually a
 * DIMM).  We do this in one of two ways, depending on the platform.  For most
 * platforms, we can use the bursting routine to generate the list of member
 * unums from the container unum.  Membership can then be determined by
 * searching the bursted list for the containee's unum.
 *
 * Some platforms, however, cannot be bursted, as their bank unums do not
 * contain all of the information needed to generate the complete list of
 * member DIMM unums.  For these unums, we must make do with a substring
 * comparison.
 */

static int
unum_contains_bypat(const char *erunum, const char *eeunum)
{
	char **ernms, **eenms;
	size_t nernms, neenms;
	int i, j, rv = 1;

	if (mem_unum_burst(erunum, &ernms, &nernms) < 0)
		return (fmd_fmri_set_errno(EINVAL));
	if (mem_unum_burst(eeunum, &eenms, &neenms) < 0) {
		mem_strarray_free(ernms, nernms);
		return (fmd_fmri_set_errno(EINVAL));
	}

	for (i = 0; i < neenms; i++) {
		for (j = 0; j < nernms; j++) {
			if (strcmp(eenms[i], ernms[j]) == 0)
				break;
		}

		if (j == nernms) {
			/*
			 * This DIMM was not found in the container.
			 */
			rv = 0;
			break;
		}
	}

	mem_strarray_free(ernms, nernms);
	mem_strarray_free(eenms, neenms);

	return (rv);
}

static int
unum_strip_one_jnum(const char *unum, uint_t *endp)
{
	char *c;
	int i;

	if ((c = strrchr(unum, 'J')) == NULL)
		return (0);

	while (c > unum && isspace(c[-1]))
		c--;

	(void) sscanf(c, " J%*[0-9] %n", &i);
	if (i == 0 || (uintptr_t)(c - unum) + i != strlen(unum))
		return (0);

	*endp = (uint_t)(c - unum);
	return (1);
}


static int
unum_contains_bysubstr(const char *erunum, const char *eeunum)
{
	uint_t erlen, eelen;
	int nojnumstrip = 0;

	/*
	 * This comparison method is only known to work on specific types of
	 * unums.  Check for those types here.
	 */
	if ((strncmp(erunum, "/N", 2) != 0 && strncmp(erunum, "/IO", 3) != 0 &&
	    strncmp(erunum, "/SB", 3) != 0) ||
	    (strncmp(eeunum, "/N", 2) != 0 && strncmp(eeunum, "/IO", 3) != 0 &&
	    strncmp(eeunum, "/SB", 3) != 0)) {
		if (ISHCUNUM(erunum) && ISHCUNUM(eeunum)) {
			nojnumstrip = 1;
			erlen = strlen(erunum);
			eelen = strlen(eeunum);
		} else {
			return (fmd_fmri_set_errno(EINVAL));
		}
	}

	if (!nojnumstrip) {
		erlen = unum_strip_one_jnum(erunum, &erlen) ?
		    erlen : strlen(erunum);
		eelen = unum_strip_one_jnum(eeunum, &eelen) ?
		    eelen : strlen(eeunum);
	}

	return (strncmp(erunum, eeunum, MIN(erlen, eelen)) == 0);
}

typedef int unum_cmptor_f(const char *, const char *);

static unum_cmptor_f *const unum_cmptors[] = {
	unum_contains_bypat,
	unum_contains_bysubstr
};

int
mem_unum_contains(const char *erunum, const char *eeunum)
{
	static int cmptor = 0;
	int rc;

	while (isspace(*erunum))
		erunum++;
	while (isspace(*eeunum))
		eeunum++;

	if ((rc = unum_cmptors[cmptor](erunum, eeunum)) >= 0)
		return (rc);

	if ((rc = unum_cmptors[cmptor == 0](erunum, eeunum)) >= 0) {
		/*
		 * We succeeded with the non-default comparator.  Change the
		 * default so we use the correct one next time.
		 */
		cmptor = (cmptor == 0);
	}

	return (rc);
}

/*
 * If an asru has a unum string that is an hc path string then return
 * a new nvl (to be freed by the caller) that is a duplicate of the
 * original but with an additional member of a reconstituted hc fmri.
 */
int
mem_unum_rewrite(nvlist_t *nvl, nvlist_t **rnvl)
{
	int err;
	char *unumstr;
	nvlist_t *unum;
	struct topo_hdl *thp;

	if (nvlist_lookup_string(nvl, FM_FMRI_MEM_UNUM, &unumstr) != 0 ||
	    !ISHCUNUM(unumstr))
		return (0);

	if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
		return (EINVAL);

	if (topo_fmri_str2nvl(thp, unumstr, &unum, &err) != 0) {
		fmd_fmri_topo_rele(thp);
		return (EINVAL);
	}

	fmd_fmri_topo_rele(thp);

	if ((err = nvlist_dup(nvl, rnvl, 0)) != 0) {
		nvlist_free(unum);
		return (err);
	}

	err = nvlist_add_nvlist(*rnvl, FM_FMRI_MEM_UNUM "-fmri", unum);
	nvlist_free(unum);

	if (err != 0)
		nvlist_free(*rnvl);

	return (err);
}