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