xref: /titanic_52/usr/src/cmd/fm/schemes/mem/mem_unum.c (revision 27e6fb2101ae37ba3de6dbb1567bf7558ffaccad)
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 	{ "MB/CMP%*d/BR%*d%n%n%n",		"/CH%*d/D%*d/J%*4d%n" },
99 	{ NULL }
100 };
101 
102 /*
103  * Burst Serengeti and Starcat-style unums.
104  * A DIMM unum string is expected to be in this form:
105  * "[/N0/]SB12/P0/B0/D2 [J13500]"
106  * A bank unum string is expected to be in this form:
107  * "[/N0/]SB12/P0/B0 [J13500, ...]"
108  */
109 static int
110 mem_unum_burst_sgsc(const char *pat, char ***dimmsp, size_t *ndimmsp)
111 {
112 	char buf[64];
113 	char **dimms;
114 	char *base;
115 	const char *c;
116 	char *copy;
117 	size_t copysz;
118 	int i;
119 
120 	/*
121 	 * No expansion is required for a DIMM unum
122 	 */
123 	if (strchr(pat, 'D') != NULL) {
124 		dimms = fmd_fmri_alloc(sizeof (char *));
125 		dimms[0] = fmd_fmri_strdup(pat);
126 		*dimmsp = dimms;
127 		*ndimmsp = 1;
128 		return (0);
129 	}
130 
131 	/*
132 	 * strtok is destructive so we need to work with
133 	 * a copy and keep track of the size allocated.
134 	 */
135 	copysz = strlen(pat) + 1;
136 	copy = fmd_fmri_alloc(copysz);
137 	(void) strcpy(copy, pat);
138 
139 	base = strtok(copy, " ");
140 
141 	/* There are four DIMMs in a bank */
142 	dimms = fmd_fmri_alloc(sizeof (char *) * 4);
143 
144 	for (i = 0; i < 4; i++) {
145 		(void) snprintf(buf, sizeof (buf), "%s/D%d", base, i);
146 
147 		if ((c = strtok(NULL, " ")) != NULL)
148 			(void) snprintf(buf, sizeof (buf), "%s %s", buf, c);
149 
150 		dimms[i] = fmd_fmri_strdup(buf);
151 	}
152 
153 	fmd_fmri_free(copy, copysz);
154 
155 	*dimmsp = dimms;
156 	*ndimmsp = 4;
157 	return (0);
158 }
159 
160 
161 /*
162  * Returns 0 (with dimmsp and ndimmsp set) if the unum could be bursted, -1
163  * otherwise.
164  */
165 static int
166 mem_unum_burst_pattern(const char *pat, char ***dimmsp, size_t *ndimmsp)
167 {
168 	const bank_dimm_t *bd;
169 	char **dimms = NULL, **newdimms;
170 	size_t ndimms = 0;
171 	const char *c;
172 
173 
174 	for (bd = bank_dimm; bd->bd_pat != NULL; bd++) {
175 		int replace, start, matched;
176 		char dimmname[64];
177 
178 		replace = start = matched = -1;
179 		(void) sscanf(pat, bd->bd_pat, &replace, &start, &matched);
180 		if (matched == -1)
181 			continue;
182 
183 		(void) strlcpy(dimmname, pat, sizeof (dimmname));
184 		if (bd->bd_subst != NULL) {
185 			(void) strlcpy(dimmname+replace, bd->bd_subst,
186 			    sizeof (dimmname) - strlen(bd->bd_subst));
187 			replace += strlen(bd->bd_subst);
188 		}
189 
190 		c = pat + start;
191 		while (*c != '\0') {
192 			int dimmlen = -1;
193 
194 			(void) sscanf(c, bd->bd_reppat, &dimmlen);
195 			if (dimmlen == -1)
196 				break;
197 
198 			while (*c == ' ') {
199 				c++;
200 				dimmlen--;
201 			}
202 
203 			if (dimmlen > sizeof (dimmname) - replace)
204 				break;
205 
206 			(void) strlcpy(dimmname + replace, c, dimmlen + 1);
207 
208 			newdimms = fmd_fmri_alloc(sizeof (char *) *
209 			    (ndimms + 1));
210 			if (ndimms != 0) {
211 				bcopy(dimms, newdimms, sizeof (char *) *
212 				    ndimms);
213 				fmd_fmri_free(dimms, sizeof (char *) * ndimms);
214 			}
215 			newdimms[ndimms++] = fmd_fmri_strdup(dimmname);
216 			dimms = newdimms;
217 
218 			c += dimmlen;
219 
220 			if (*c != ' ' && *c != '\0')
221 				break;
222 		}
223 
224 		if (*c != '\0')
225 			break;
226 
227 		*dimmsp = dimms;
228 		*ndimmsp = ndimms;
229 
230 		return (0);
231 	}
232 
233 	mem_strarray_free(dimms, ndimms);
234 
235 	return (fmd_fmri_set_errno(EINVAL));
236 }
237 
238 int
239 mem_unum_burst(const char *pat, char ***dimmsp, size_t *ndimmsp)
240 {
241 	const char *platform = fmd_fmri_get_platform();
242 
243 	/*
244 	 * Call mem_unum_burst_sgsc() for Starcat, Serengeti, and
245 	 * Lightweight 8 platforms.  Call mem_unum_burst_pattern()
246 	 * for all other platforms.
247 	 */
248 	if (strcmp(platform, "SUNW,Sun-Fire-15000") == 0 ||
249 	    strcmp(platform, "SUNW,Sun-Fire") == 0 ||
250 	    strcmp(platform, "SUNW,Netra-T12") == 0)
251 		return (mem_unum_burst_sgsc(pat, dimmsp, ndimmsp));
252 	else
253 		return (mem_unum_burst_pattern(pat, dimmsp, ndimmsp));
254 }
255 
256 /*
257  * The unum containership operation is designed to tell the caller whether a
258  * given FMRI contains another.  In the case of this plugin, we tell the caller
259  * whether a given memory FMRI (usually a bank) contains another (usually a
260  * DIMM).  We do this in one of two ways, depending on the platform.  For most
261  * platforms, we can use the bursting routine to generate the list of member
262  * unums from the container unum.  Membership can then be determined by
263  * searching the bursted list for the containee's unum.
264  *
265  * Some platforms, however, cannot be bursted, as their bank unums do not
266  * contain all of the information needed to generate the complete list of
267  * member DIMM unums.  For these unums, we must make do with a substring
268  * comparison.
269  */
270 
271 static int
272 unum_contains_bypat(const char *erunum, const char *eeunum)
273 {
274 	char **ernms, **eenms;
275 	size_t nernms, neenms;
276 	int i, j, rv = 1;
277 
278 	if (mem_unum_burst(erunum, &ernms, &nernms) < 0)
279 		return (fmd_fmri_set_errno(EINVAL));
280 	if (mem_unum_burst(eeunum, &eenms, &neenms) < 0) {
281 		mem_strarray_free(ernms, nernms);
282 		return (fmd_fmri_set_errno(EINVAL));
283 	}
284 
285 	for (i = 0; i < neenms; i++) {
286 		for (j = 0; j < nernms; j++) {
287 			if (strcmp(eenms[i], ernms[j]) == 0)
288 				break;
289 		}
290 
291 		if (j == nernms) {
292 			/*
293 			 * This DIMM was not found in the container.
294 			 */
295 			rv = 0;
296 			break;
297 		}
298 	}
299 
300 	mem_strarray_free(ernms, nernms);
301 	mem_strarray_free(eenms, neenms);
302 
303 	return (rv);
304 }
305 
306 static int
307 unum_strip_one_jnum(const char *unum, uint_t *endp)
308 {
309 	char *c;
310 	int i;
311 
312 	if ((c = strrchr(unum, 'J')) == NULL)
313 		return (0);
314 
315 	while (c > unum && isspace(c[-1]))
316 		c--;
317 
318 	(void) sscanf(c, " J%*[0-9] %n", &i);
319 	if (i == 0 || (uintptr_t)(c - unum) + i != strlen(unum))
320 		return (0);
321 
322 	*endp = (uint_t)(c - unum);
323 	return (1);
324 }
325 
326 
327 static int
328 unum_contains_bysubstr(const char *erunum, const char *eeunum)
329 {
330 	uint_t erlen, eelen;
331 	int nojnumstrip = 0;
332 
333 	/*
334 	 * This comparison method is only known to work on specific types of
335 	 * unums.  Check for those types here.
336 	 */
337 	if ((strncmp(erunum, "/N", 2) != 0 && strncmp(erunum, "/IO", 3) != 0 &&
338 	    strncmp(erunum, "/SB", 3) != 0) ||
339 	    (strncmp(eeunum, "/N", 2) != 0 && strncmp(eeunum, "/IO", 3) != 0 &&
340 	    strncmp(eeunum, "/SB", 3) != 0)) {
341 		if (ISHCUNUM(erunum) && ISHCUNUM(eeunum)) {
342 			nojnumstrip = 1;
343 			erlen = strlen(erunum);
344 			eelen = strlen(eeunum);
345 		} else {
346 			return (fmd_fmri_set_errno(EINVAL));
347 		}
348 	}
349 
350 	if (!nojnumstrip) {
351 		erlen = unum_strip_one_jnum(erunum, &erlen) ?
352 		    erlen : strlen(erunum);
353 		eelen = unum_strip_one_jnum(eeunum, &eelen) ?
354 		    eelen : strlen(eeunum);
355 	}
356 
357 	return (strncmp(erunum, eeunum, MIN(erlen, eelen)) == 0);
358 }
359 
360 typedef int unum_cmptor_f(const char *, const char *);
361 
362 static unum_cmptor_f *const unum_cmptors[] = {
363 	unum_contains_bypat,
364 	unum_contains_bysubstr
365 };
366 
367 int
368 mem_unum_contains(const char *erunum, const char *eeunum)
369 {
370 	static int cmptor = 0;
371 	int rc;
372 
373 	while (isspace(*erunum))
374 		erunum++;
375 	while (isspace(*eeunum))
376 		eeunum++;
377 
378 	if ((rc = unum_cmptors[cmptor](erunum, eeunum)) >= 0)
379 		return (rc);
380 
381 	if ((rc = unum_cmptors[cmptor == 0](erunum, eeunum)) >= 0) {
382 		/*
383 		 * We succeeded with the non-default comparator.  Change the
384 		 * default so we use the correct one next time.
385 		 */
386 		cmptor = (cmptor == 0);
387 	}
388 
389 	return (rc);
390 }
391 
392 /*
393  * If an asru has a unum string that is an hc path string then return
394  * a new nvl (to be freed by the caller) that is a duplicate of the
395  * original but with an additional member of a reconstituted hc fmri.
396  */
397 int
398 mem_unum_rewrite(nvlist_t *nvl, nvlist_t **rnvl)
399 {
400 	int err;
401 	char *unumstr;
402 	nvlist_t *unum;
403 	struct topo_hdl *thp;
404 
405 	if (nvlist_lookup_string(nvl, FM_FMRI_MEM_UNUM, &unumstr) != 0 ||
406 	    !ISHCUNUM(unumstr))
407 		return (0);
408 
409 	thp = fmd_fmri_topology(TOPO_VERSION);
410 
411 	if (topo_fmri_str2nvl(thp, unumstr, &unum, &err) != 0)
412 		return (EINVAL);
413 
414 	if ((err = nvlist_dup(nvl, rnvl, 0)) != 0) {
415 		nvlist_free(unum);
416 		return (err);
417 	}
418 
419 	err = nvlist_add_nvlist(*rnvl, FM_FMRI_MEM_UNUM "-fmri", unum);
420 	nvlist_free(unum);
421 
422 	if (err != 0)
423 		nvlist_free(*rnvl);
424 
425 	return (err);
426 }
427