xref: /illumos-gate/usr/src/cmd/fm/schemes/mem/mem_unum.c (revision 5328fc53d11d7151861fa272e4fb0248b8f0e145)
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 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Copyright 2019 Peter Tribble.
29  */
30 
31 #include <mem.h>
32 #include <fm/fmd_fmri.h>
33 #include <fm/libtopo.h>
34 
35 #include <string.h>
36 #include <strings.h>
37 #include <ctype.h>
38 
39 #define	ISHCUNUM(unum) (strncmp(unum, "hc:/", 4) == 0)
40 
41 /*
42  * Given a DIMM or bank unum, mem_unum_burst will break it apart into individual
43  * DIMM names.  If it's a DIMM, one name will be returned.  If it's a bank, the
44  * unums for the individual DIMMs will be returned.
45  *
46  * Plain J-number DIMM and bank unums are simple.  J DIMMs have one J number.  J
47  * banks have multiple whitespace-separated J numbers.
48  *
49  * The others are more complex, and consist of a common portion c, a colon, and
50  * a DIMM-specific portion d.  DIMMs are of the form "c: d", while banks are of
51  * the form "c: d d ...".  The patterns are designed to handle the complex case,
52  * but also handle the simple ones as an afterthought.  bd_pat is used to
53  * match specific styles of unum.  In bd_pat, the first %n indicates the end of
54  * the common portion ("c" above).  The second %n marks the beginning of the
55  * repetitive portion ("d" above).  The third %n is used to determine whether or
56  * not the entire pattern matched.  bd_reppat is used to match instances of the
57  * repetitive part.
58  *
59  * sscanf is your disturbingly powerful friend.
60  *
61  * The "bd_subst" element of the bank_dimm structure was added for Ontario
62  * in order to accommodate its bank string names.  Previously, to convert
63  * from a bank representation <common piece> <dimm1> <dimm2> ...
64  * we concatenated the common piece with each dimm-specific piece in turn,
65  * possibly deleting some characters in between.  Ontario is the first
66  * platform which requires that characters be substituted (like a vi s/1/2/)
67  * in place of characters deleted.  "bd_subst" represents the character(s)
68  * to be substituted between the common piece and each dimm-specific piece
69  * as part of the bursting.  For prior platforms, this value is skipped.
70  *
71  * Example:
72  * input: "MB/CMP0/CH3: R1/D0/J1901 R1/D1/J2001"
73  * outputs: "MB/CMP0/CH3/R1/D0/J1901", "MB/CMP0/CH3/R1/D1/J2001"
74  */
75 
76 typedef struct bank_dimm {
77 	const char *bd_pat;
78 	const char *bd_reppat;
79 	const char *bd_subst;
80 } bank_dimm_t;
81 
82 static const bank_dimm_t bank_dimm[] = {
83 	{ "%n%nJ%*4d%n",			" J%*4d%n" },
84 	{ "MB/P%*d/%nB%*d:%n%n",		" B%*d/D%*d%n" },
85 	{ "MB/P%*d/%nB%*d/D%*d:%n%n",		" B%*d/D%*d%n" },
86 	{ "C%*d/P%*d/%nB%*d:%n%n",		" B%*d/D%*d%n" },
87 	{ "C%*d/P%*d/%nB%*d/D%*d:%n%n",		" B%*d/D%*d%n" },
88 	{ "Slot %*c: %n%nJ%*4d%n",		" J%*4d%n" },
89 	{ "%n%nDIMM%*d%n",			" DIMM%*d%n" },
90 	{ "MB/%nDIMM%*d MB/DIMM%*d: %n%n",	" DIMM%*d%n" },
91 	{ "MB/%nDIMM%*d:%n%n",			" DIMM%*d%n" },
92 	{ "MB/CMP%*d/CH%*d%n:%n%n",		" R%*d/D%*d/J%*4d%n",	"/" },
93 	{ "MB/CMP%*d/CH%*d%n%n%n",		"/R%*d/D%*d/J%*4d%n" },
94 	{ "MB/C%*d/P%*d/%nB%*d:%n%n",		" B%*d/D%*d%n" },
95 	{ "MB/C%*d/P%*d/%nB%*d/D%*d:%n%n",	" B%*d/D%*d%n" },
96 	{ "/MBU_A/MEMB%*d/%n%nMEM%*d%*1c%n",	" MEM%*d%*1c%n" },
97 	{ "/MBU_B/MEMB%*d/%n%nMEM%*d%*1c%n",	" MEM%*d%*1c%n" },
98 	{ "/MBU_A/%n%nMEM%*d%*1c%n",		" MEM%*d%*1c%n" },
99 	{ "/CMU%*2d/%n%nMEM%*2d%*1c%n",		" MEM%*2d%*1c%n" },
100 	{ "MB/CMP%*d/BR%*d%n:%n%n",		" CH%*d/D%*d/J%*4d%n", "/" },
101 	{ "%n%nMB/CMP%*d/BR%*d/CH%*d/D%*d/J%*4d%n",
102 	    "MB/CMP%*d/BR%*d/CH%*d/D%*d/J%*4d%n" },
103 	{ "%n%nMB/CMP%*d/BR%*d/CH%*d/D%*d%n", "MB/CMP%*d/BR%*d/CH%*d/D%*d%n" },
104 	{ "MB/CPU%*d/CMP%*d/BR%*d%n:%n%n",	" CH%*d/D%*d/J%*4d%n", "/"},
105 	{ "MB/MEM%*d/CMP%*d/BR%*d%n:%n%n",	" CH%*d/D%*d/J%*4d%n", "/"},
106 	{ "%n%nMB/MEM%*d/CMP%*d/BR%*d/CH%*d/D%*d/J%*4d%n",
107 	    "MB/MEM%*d/CMP%*d/BR%*d/CH%*d/D%*d/J%*4d%n" },
108 	{ "%n%nMB/CPU%*d/CMP%*d/BR%*d/CH%*d/D%*d/J%*4d%n",
109 	    "MB/CPU%*d/CMP%*d/BR%*d/CH%*d/D%*d/J%*4d%n" },
110 	{ "%n%nMB/MEM%*d/CMP%*d/BR%*d/CH%*d/D%*d%n",
111 	    "MB/MEM%*d/CMP%*d/BR%*d/CH%*d/D%*d%n"  },
112 	{ "%n%nMB/CPU%*d/CMP%*d/BR%*d/CH%*d/D%*d%n",
113 	    "MB/CPU%*d/CMP%*d/BR%*d/CH%*d/D%*d%n"  },
114 	{ NULL }
115 };
116 
117 /*
118  * Burst Serengeti-style unums.
119  * A DIMM unum string is expected to be in this form:
120  * "[/N0/]SB12/P0/B0/D2 [J13500]"
121  * A bank unum string is expected to be in this form:
122  * "[/N0/]SB12/P0/B0 [J13500, ...]"
123  */
124 static int
125 mem_unum_burst_sgsc(const char *pat, char ***dimmsp, size_t *ndimmsp)
126 {
127 	char buf[64];
128 	char **dimms;
129 	char *base;
130 	const char *c;
131 	char *copy;
132 	size_t copysz;
133 	int i;
134 
135 	/*
136 	 * No expansion is required for a DIMM unum
137 	 */
138 	if (strchr(pat, 'D') != NULL) {
139 		dimms = fmd_fmri_alloc(sizeof (char *));
140 		dimms[0] = fmd_fmri_strdup(pat);
141 		*dimmsp = dimms;
142 		*ndimmsp = 1;
143 		return (0);
144 	}
145 
146 	/*
147 	 * strtok is destructive so we need to work with
148 	 * a copy and keep track of the size allocated.
149 	 */
150 	copysz = strlen(pat) + 1;
151 	copy = fmd_fmri_alloc(copysz);
152 	(void) strcpy(copy, pat);
153 
154 	base = strtok(copy, " ");
155 
156 	/* There are four DIMMs in a bank */
157 	dimms = fmd_fmri_alloc(sizeof (char *) * 4);
158 
159 	for (i = 0; i < 4; i++) {
160 		(void) snprintf(buf, sizeof (buf), "%s/D%d", base, i);
161 
162 		if ((c = strtok(NULL, " ")) != NULL)
163 			(void) snprintf(buf, sizeof (buf), "%s %s", buf, c);
164 
165 		dimms[i] = fmd_fmri_strdup(buf);
166 	}
167 
168 	fmd_fmri_free(copy, copysz);
169 
170 	*dimmsp = dimms;
171 	*ndimmsp = 4;
172 	return (0);
173 }
174 
175 
176 /*
177  * Returns 0 (with dimmsp and ndimmsp set) if the unum could be bursted, -1
178  * otherwise.
179  */
180 static int
181 mem_unum_burst_pattern(const char *pat, char ***dimmsp, size_t *ndimmsp)
182 {
183 	const bank_dimm_t *bd;
184 	char **dimms = NULL, **newdimms;
185 	size_t ndimms = 0;
186 	const char *c;
187 
188 
189 	for (bd = bank_dimm; bd->bd_pat != NULL; bd++) {
190 		int replace, start, matched;
191 		char dimmname[64];
192 
193 		replace = start = matched = -1;
194 		(void) sscanf(pat, bd->bd_pat, &replace, &start, &matched);
195 		if (matched == -1)
196 			continue;
197 		(void) strlcpy(dimmname, pat, sizeof (dimmname));
198 		if (bd->bd_subst != NULL) {
199 			(void) strlcpy(dimmname+replace, bd->bd_subst,
200 			    sizeof (dimmname) - strlen(bd->bd_subst));
201 			replace += strlen(bd->bd_subst);
202 		}
203 
204 		c = pat + start;
205 		while (*c != '\0') {
206 			int dimmlen = -1;
207 
208 			(void) sscanf(c, bd->bd_reppat, &dimmlen);
209 			if (dimmlen == -1)
210 				break;
211 
212 			while (*c == ' ') {
213 				c++;
214 				dimmlen--;
215 			}
216 
217 			if (dimmlen > sizeof (dimmname) - replace)
218 				break;
219 
220 			(void) strlcpy(dimmname + replace, c, dimmlen + 1);
221 
222 			newdimms = fmd_fmri_alloc(sizeof (char *) *
223 			    (ndimms + 1));
224 			if (ndimms != 0) {
225 				bcopy(dimms, newdimms, sizeof (char *) *
226 				    ndimms);
227 				fmd_fmri_free(dimms, sizeof (char *) * ndimms);
228 			}
229 			newdimms[ndimms++] = fmd_fmri_strdup(dimmname);
230 			dimms = newdimms;
231 
232 			c += dimmlen;
233 
234 			if (*c != ' ' && *c != '\0')
235 				break;
236 		}
237 
238 		if (*c != '\0')
239 			break;
240 
241 		*dimmsp = dimms;
242 		*ndimmsp = ndimms;
243 
244 		return (0);
245 	}
246 
247 	mem_strarray_free(dimms, ndimms);
248 
249 	/*
250 	 * Set errno to ENOTSUP and return -1. This allows support for DIMMs
251 	 * with unknown unum strings and/or serial numbers. The only consumer
252 	 * of mem_unum_burst_pattern() that cares/checks for the returned
253 	 * errno is fmd_fmri_expand().
254 	 */
255 	return (fmd_fmri_set_errno(ENOTSUP));
256 }
257 
258 int
259 mem_unum_burst(const char *pat, char ***dimmsp, size_t *ndimmsp)
260 {
261 	const char *platform = fmd_fmri_get_platform();
262 
263 	/*
264 	 * Call mem_unum_burst_sgsc() for Serengeti and
265 	 * Lightweight 8 platforms.  Call mem_unum_burst_pattern()
266 	 * for all other platforms.
267 	 */
268 	if (strcmp(platform, "SUNW,Sun-Fire") == 0 ||
269 	    strcmp(platform, "SUNW,Netra-T12") == 0)
270 		return (mem_unum_burst_sgsc(pat, dimmsp, ndimmsp));
271 	else
272 		return (mem_unum_burst_pattern(pat, dimmsp, ndimmsp));
273 }
274 
275 /*
276  * The unum containership operation is designed to tell the caller whether a
277  * given FMRI contains another.  In the case of this plugin, we tell the caller
278  * whether a given memory FMRI (usually a bank) contains another (usually a
279  * DIMM).  We do this in one of two ways, depending on the platform.  For most
280  * platforms, we can use the bursting routine to generate the list of member
281  * unums from the container unum.  Membership can then be determined by
282  * searching the bursted list for the containee's unum.
283  *
284  * Some platforms, however, cannot be bursted, as their bank unums do not
285  * contain all of the information needed to generate the complete list of
286  * member DIMM unums.  For these unums, we must make do with a substring
287  * comparison.
288  */
289 
290 static int
291 unum_contains_bypat(const char *erunum, const char *eeunum)
292 {
293 	char **ernms, **eenms;
294 	size_t nernms, neenms;
295 	int i, j, rv = 1;
296 
297 	if (mem_unum_burst(erunum, &ernms, &nernms) < 0)
298 		return (fmd_fmri_set_errno(EINVAL));
299 	if (mem_unum_burst(eeunum, &eenms, &neenms) < 0) {
300 		mem_strarray_free(ernms, nernms);
301 		return (fmd_fmri_set_errno(EINVAL));
302 	}
303 
304 	for (i = 0; i < neenms; i++) {
305 		for (j = 0; j < nernms; j++) {
306 			if (strcmp(eenms[i], ernms[j]) == 0)
307 				break;
308 		}
309 
310 		if (j == nernms) {
311 			/*
312 			 * This DIMM was not found in the container.
313 			 */
314 			rv = 0;
315 			break;
316 		}
317 	}
318 
319 	mem_strarray_free(ernms, nernms);
320 	mem_strarray_free(eenms, neenms);
321 
322 	return (rv);
323 }
324 
325 static int
326 unum_strip_one_jnum(const char *unum, uint_t *endp)
327 {
328 	char *c;
329 	int i;
330 
331 	if ((c = strrchr(unum, 'J')) == NULL)
332 		return (0);
333 
334 	while (c > unum && isspace(c[-1]))
335 		c--;
336 
337 	(void) sscanf(c, " J%*[0-9] %n", &i);
338 	if (i == 0 || (uintptr_t)(c - unum) + i != strlen(unum))
339 		return (0);
340 
341 	*endp = (uint_t)(c - unum);
342 	return (1);
343 }
344 
345 
346 static int
347 unum_contains_bysubstr(const char *erunum, const char *eeunum)
348 {
349 	uint_t erlen, eelen;
350 	int nojnumstrip = 0;
351 
352 	/*
353 	 * This comparison method is only known to work on specific types of
354 	 * unums.  Check for those types here.
355 	 */
356 	if ((strncmp(erunum, "/N", 2) != 0 && strncmp(erunum, "/IO", 3) != 0 &&
357 	    strncmp(erunum, "/SB", 3) != 0) ||
358 	    (strncmp(eeunum, "/N", 2) != 0 && strncmp(eeunum, "/IO", 3) != 0 &&
359 	    strncmp(eeunum, "/SB", 3) != 0)) {
360 		if (ISHCUNUM(erunum) && ISHCUNUM(eeunum)) {
361 			nojnumstrip = 1;
362 			erlen = strlen(erunum);
363 			eelen = strlen(eeunum);
364 		} else {
365 			return (fmd_fmri_set_errno(EINVAL));
366 		}
367 	}
368 
369 	if (!nojnumstrip) {
370 		erlen = unum_strip_one_jnum(erunum, &erlen) ?
371 		    erlen : strlen(erunum);
372 		eelen = unum_strip_one_jnum(eeunum, &eelen) ?
373 		    eelen : strlen(eeunum);
374 	}
375 
376 	return (strncmp(erunum, eeunum, MIN(erlen, eelen)) == 0);
377 }
378 
379 typedef int unum_cmptor_f(const char *, const char *);
380 
381 static unum_cmptor_f *const unum_cmptors[] = {
382 	unum_contains_bypat,
383 	unum_contains_bysubstr
384 };
385 
386 int
387 mem_unum_contains(const char *erunum, const char *eeunum)
388 {
389 	static int cmptor = 0;
390 	int rc;
391 
392 	while (isspace(*erunum))
393 		erunum++;
394 	while (isspace(*eeunum))
395 		eeunum++;
396 
397 	if ((rc = unum_cmptors[cmptor](erunum, eeunum)) >= 0)
398 		return (rc);
399 
400 	if ((rc = unum_cmptors[cmptor == 0](erunum, eeunum)) >= 0) {
401 		/*
402 		 * We succeeded with the non-default comparator.  Change the
403 		 * default so we use the correct one next time.
404 		 */
405 		cmptor = (cmptor == 0);
406 	}
407 
408 	return (rc);
409 }
410 
411 /*
412  * If an asru has a unum string that is an hc path string then return
413  * a new nvl (to be freed by the caller) that is a duplicate of the
414  * original but with an additional member of a reconstituted hc fmri.
415  */
416 int
417 mem_unum_rewrite(nvlist_t *nvl, nvlist_t **rnvl)
418 {
419 	int err;
420 	char *unumstr;
421 	nvlist_t *unum;
422 	struct topo_hdl *thp;
423 
424 	if (nvlist_lookup_string(nvl, FM_FMRI_MEM_UNUM, &unumstr) != 0 ||
425 	    !ISHCUNUM(unumstr))
426 		return (0);
427 
428 	if ((thp = fmd_fmri_topo_hold(TOPO_VERSION)) == NULL)
429 		return (EINVAL);
430 
431 	if (topo_fmri_str2nvl(thp, unumstr, &unum, &err) != 0) {
432 		fmd_fmri_topo_rele(thp);
433 		return (EINVAL);
434 	}
435 
436 	fmd_fmri_topo_rele(thp);
437 
438 	if ((err = nvlist_dup(nvl, rnvl, 0)) != 0) {
439 		nvlist_free(unum);
440 		return (err);
441 	}
442 
443 	err = nvlist_add_nvlist(*rnvl, FM_FMRI_MEM_UNUM "-fmri", unum);
444 	nvlist_free(unum);
445 
446 	if (err != 0)
447 		nvlist_free(*rnvl);
448 
449 	return (err);
450 }
451