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