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  * Copyright 2019, Joyent, Inc.
27  */
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <libnvpair.h>
35 #include <kstat.h>
36 #include <unistd.h>
37 #include <sys/types.h>
38 #include <fm/topo_mod.h>
39 #include <sys/devfm.h>
40 #include <fm/fmd_agent.h>
41 
42 #define	BUFSZ	128
43 
44 char *
get_fmtstr(topo_mod_t * mod,nvlist_t * in)45 get_fmtstr(topo_mod_t *mod, nvlist_t *in)
46 {
47 	char *fmtstr;
48 	nvlist_t *args;
49 	int ret;
50 
51 	topo_mod_dprintf(mod, "get_fmtstr() called\n");
52 
53 	if ((ret = nvlist_lookup_nvlist(in, TOPO_PROP_ARGS, &args)) != 0) {
54 		topo_mod_dprintf(mod, "Failed to lookup 'args' list (%s)\n",
55 		    strerror(ret));
56 		(void) topo_mod_seterrno(mod, EMOD_NVL_INVAL);
57 		return (NULL);
58 	}
59 	if ((ret = nvlist_lookup_string(args, "format", &fmtstr)) != 0) {
60 		topo_mod_dprintf(mod, "Failed to lookup 'format' arg (%s)\n",
61 		    strerror(ret));
62 		(void) topo_mod_seterrno(mod, EMOD_NVL_INVAL);
63 		return (NULL);
64 	}
65 	return (fmtstr);
66 }
67 
68 int
store_prop_val(topo_mod_t * mod,char * buf,char * propname,nvlist_t ** out)69 store_prop_val(topo_mod_t *mod, char *buf, char *propname, nvlist_t **out)
70 {
71 	if (topo_mod_nvalloc(mod, out, NV_UNIQUE_NAME) != 0) {
72 		topo_mod_dprintf(mod, "Failed to allocate 'out' nvlist\n");
73 		return (topo_mod_seterrno(mod, EMOD_NOMEM));
74 	}
75 	if (nvlist_add_string(*out, TOPO_PROP_VAL_NAME, propname) != 0) {
76 		topo_mod_dprintf(mod, "Failed to set '%s'\n",
77 		    TOPO_PROP_VAL_NAME);
78 		nvlist_free(*out);
79 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
80 	}
81 	if (nvlist_add_uint32(*out, TOPO_PROP_VAL_TYPE, TOPO_TYPE_STRING)
82 	    != 0) {
83 		topo_mod_dprintf(mod, "Failed to set '%s'\n",
84 		    TOPO_PROP_VAL_TYPE);
85 		nvlist_free(*out);
86 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
87 	}
88 	if (nvlist_add_string(*out, TOPO_PROP_VAL_VAL, buf) != 0) {
89 		topo_mod_dprintf(mod, "Failed to set '%s'\n",
90 		    TOPO_PROP_VAL_VAL);
91 		nvlist_free(*out);
92 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
93 	}
94 	return (0);
95 }
96 
97 /*
98  * This is a somewhat generic property method for labelling the dimm slots on
99  * uni-socket x86/x64 platforms.  This method assumes a direct linear
100  * correlation between the dimm topo node instance number and the dimm slot
101  * label number.  It takes the following two arguments:
102  *
103  * format:	a string containing a printf-like format with a single %d token
104  *              which this method computes
105  *
106  *              i.e.: DIMM %d
107  *
108  * offset:      a numeric offset that we'll number the dimms from.  This is to
109  *              allow for the fact that some systems number the dimm slots
110  *              from zero and others start from one (like the Ultra 20)
111  */
112 /* ARGSUSED */
113 int
simple_dimm_label(topo_mod_t * mod,tnode_t * node,topo_version_t vers,nvlist_t * in,nvlist_t ** out)114 simple_dimm_label(topo_mod_t *mod, tnode_t *node, topo_version_t vers,
115     nvlist_t *in, nvlist_t **out)
116 {
117 	char *fmtstr, buf[BUFSZ];
118 	int ret;
119 	uint32_t offset;
120 	nvlist_t *args;
121 
122 	topo_mod_dprintf(mod, "simple_dimm_label() called\n");
123 	if ((ret = nvlist_lookup_nvlist(in, TOPO_PROP_ARGS, &args)) != 0) {
124 		topo_mod_dprintf(mod, "Failed to lookup 'args' list (%s)\n",
125 		    strerror(ret));
126 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
127 	}
128 	if ((ret = nvlist_lookup_uint32(args, "offset", &offset)) != 0) {
129 		topo_mod_dprintf(mod, "Failed to lookup 'offset' arg (%s)\n",
130 		    strerror(ret));
131 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
132 	}
133 
134 	if ((fmtstr = get_fmtstr(mod, in)) == NULL) {
135 		topo_mod_dprintf(mod, "Failed to retrieve 'format' arg\n");
136 		/* topo errno already set */
137 		return (-1);
138 	}
139 
140 	/* LINTED: E_SEC_PRINTF_VAR_FMT */
141 	(void) snprintf(buf, BUFSZ, fmtstr,
142 	    (topo_node_instance(node) + offset));
143 
144 	if (store_prop_val(mod, buf, "label", out) != 0) {
145 		topo_mod_dprintf(mod, "Failed to set label\n");
146 		/* topo errno already set */
147 		return (-1);
148 	}
149 
150 	return (0);
151 }
152 
153 
154 /*
155  * This is a somewhat generic property method for labelling the dimm slots on
156  * multi-socket x86/x64 platforms.  It takes the following two arguments:
157  *
158  * format:	a string containing a printf-like format with a two %d tokens
159  *              for the cpu and dimm slot label numbers, which this method
160  *              computes
161  *
162  *              i.e.: CPU %d DIMM %d
163  *
164  * offset:      a numeric offset that we'll number the dimms from.  This is to
165  *              allow for the fact that some systems number the dimm slots
166  *              from zero while others may start from one
167  *
168  * order:	"reverse" or "forward" - sets the direction of the correlation
169  *              between dimm topo node instance number and DIMM slot number
170  *
171  * dimms_per_chip:  the number of DIMM slots per chip
172  */
173 /* ARGSUSED */
174 int
simple_dimm_label_mp(topo_mod_t * mod,tnode_t * node,topo_version_t vers,nvlist_t * in,nvlist_t ** out)175 simple_dimm_label_mp(topo_mod_t *mod, tnode_t *node, topo_version_t vers,
176     nvlist_t *in, nvlist_t **out)
177 {
178 	char *fmtstr, *order, buf[BUFSZ];
179 	tnode_t *chip;
180 	int ret;
181 	uint32_t offset, dimms_per_chip;
182 	nvlist_t *args;
183 
184 	topo_mod_dprintf(mod, "simple_dimm_label_mp() called\n");
185 
186 	if ((ret = nvlist_lookup_nvlist(in, TOPO_PROP_ARGS, &args)) != 0) {
187 		topo_mod_dprintf(mod, "Failed to lookup 'args' list (%s)\n",
188 		    strerror(ret));
189 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
190 	}
191 	if ((ret = nvlist_lookup_uint32(args, "offset", &offset)) != 0) {
192 		topo_mod_dprintf(mod, "Failed to lookup 'offset' arg (%s)\n",
193 		    strerror(ret));
194 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
195 	}
196 	if ((ret = nvlist_lookup_uint32(args, "dimms_per_chip",
197 	    &dimms_per_chip)) != 0) {
198 		topo_mod_dprintf(mod, "Failed to lookup 'dimms_per_chip' arg "
199 		    "(%s)\n", strerror(ret));
200 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
201 	}
202 	if ((ret = nvlist_lookup_string(args, "order", &order)) != 0) {
203 		topo_mod_dprintf(mod, "Failed to lookup 'order' arg (%s)\n",
204 		    strerror(ret));
205 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
206 	}
207 	if ((fmtstr = get_fmtstr(mod, in)) == NULL) {
208 		topo_mod_dprintf(mod, "Failed to retrieve 'format' arg\n");
209 		topo_mod_free(mod, order, BUFSZ);
210 		/* topo errno already set */
211 		return (-1);
212 	}
213 
214 	chip = topo_node_parent(topo_node_parent(node));
215 
216 	if (strcasecmp(order, "forward") == 0)
217 		/* LINTED: E_SEC_PRINTF_VAR_FMT */
218 		(void) snprintf(buf, BUFSZ, fmtstr, topo_node_instance(chip),
219 		    (topo_node_instance(node) + offset));
220 	else if (strcasecmp(order, "reverse") == 0)
221 		/* LINTED: E_SEC_PRINTF_VAR_FMT */
222 		(void) snprintf(buf, BUFSZ, fmtstr, topo_node_instance(chip),
223 		    (((topo_node_instance(chip) + 1) * dimms_per_chip)
224 		    - (topo_node_instance(node)) - 1 + offset));
225 	else {
226 		topo_mod_dprintf(mod, "Invalid value for order arg\n");
227 		topo_mod_free(mod, order, BUFSZ);
228 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
229 	}
230 
231 	if (store_prop_val(mod, buf, "label", out) != 0) {
232 		topo_mod_dprintf(mod, "Failed to set label\n");
233 		topo_mod_free(mod, order, BUFSZ);
234 		/* topo errno already set */
235 		return (-1);
236 	}
237 
238 	return (0);
239 }
240 
241 /*
242  * This method assumes a correspondence between the dimm topo node instance
243  * number and the dimm slot label number, but unlike simple_chip_label_mp, the
244  * slot numbers aren't reused between CPU's.  This method assumes there
245  * are 4 DIMM slots per chip.  It takes the following two arguments:
246  *
247  * format:	a string containing a printf-like format with a single %d token
248  *              which this method computes
249  *
250  *              i.e.: DIMM %d
251  *
252  * offset:      a numeric offset that we'll number the dimms from.  This is to
253  *              allow for the fact that some systems number the dimm slots
254  *              from zero and others may start from one
255  *
256  * order:	"reverse" or "forward" - sets the direction of the correlation
257  *              between dimm topo node instance number and DIMM slot number
258  */
259 /* ARGSUSED */
260 int
seq_dimm_label(topo_mod_t * mod,tnode_t * node,topo_version_t vers,nvlist_t * in,nvlist_t ** out)261 seq_dimm_label(topo_mod_t *mod, tnode_t *node, topo_version_t vers,
262     nvlist_t *in, nvlist_t **out)
263 {
264 	char *fmtstr, *order, buf[BUFSZ];
265 	int ret;
266 	uint32_t offset;
267 	nvlist_t *args;
268 	tnode_t *chip;
269 
270 	topo_mod_dprintf(mod, "seq_dimm_label() called\n");
271 	if ((ret = nvlist_lookup_nvlist(in, TOPO_PROP_ARGS, &args)) != 0) {
272 		topo_mod_dprintf(mod, "Failed to lookup 'args' list (%s)\n",
273 		    strerror(ret));
274 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
275 	}
276 	if ((ret = nvlist_lookup_uint32(args, "offset", &offset)) != 0) {
277 		topo_mod_dprintf(mod, "Failed to lookup 'offset' arg (%s)\n",
278 		    strerror(ret));
279 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
280 	}
281 	if ((ret = nvlist_lookup_string(args, "order", &order)) != 0) {
282 		topo_mod_dprintf(mod, "Failed to lookup 'order' arg (%s)\n",
283 		    strerror(ret));
284 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
285 	}
286 
287 	if ((fmtstr = get_fmtstr(mod, in)) == NULL) {
288 		topo_mod_dprintf(mod, "Failed to retrieve 'format' arg\n");
289 		topo_mod_free(mod, order, BUFSZ);
290 		/* topo errno already set */
291 		return (-1);
292 	}
293 
294 	chip = topo_node_parent(topo_node_parent(node));
295 
296 	if (strcasecmp(order, "forward") == 0)
297 		/* LINTED: E_SEC_PRINTF_VAR_FMT */
298 		(void) snprintf(buf, BUFSZ, fmtstr, ((topo_node_instance(node))
299 		    + (topo_node_instance(chip) * 4) + offset));
300 	else if (strcasecmp(order, "reverse") == 0)
301 		/* LINTED: E_SEC_PRINTF_VAR_FMT */
302 		(void) snprintf(buf, BUFSZ, fmtstr,
303 		    (((topo_node_instance(chip) + 1) * 4)
304 		    - (topo_node_instance(node)) - 1 + offset));
305 	else {
306 		topo_mod_dprintf(mod, "Invalid value for order arg\n");
307 		topo_mod_free(mod, order, BUFSZ);
308 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
309 	}
310 
311 	if (store_prop_val(mod, buf, "label", out) != 0) {
312 		topo_mod_dprintf(mod, "Failed to set label\n");
313 		topo_mod_free(mod, order, BUFSZ);
314 		/* topo errno already set */
315 		return (-1);
316 	}
317 
318 	return (0);
319 }
320 
321 
322 /*
323  * This is a somewhat generic property method for labelling the CPU sockets on
324  * x86/x64 platforms.  This method assumes a correspondence between
325  * the chip topo node instance number and the CPU socket label number.  It takes
326  * the following two arguments:
327  *
328  * format:	a string containing a printf-like format with a single %d token
329  *              which this method computes
330  *
331  *              i.e.: CPU %d
332  *
333  * offset:      a numeric offset that we'll number the CPU's from.  This is to
334  *              allow for the fact that some systems number the CPU sockets
335  *              from zero and others start from one (like the X4X00-M2 systems)
336  */
337 /* ARGSUSED */
338 int
simple_chip_label(topo_mod_t * mod,tnode_t * node,topo_version_t vers,nvlist_t * in,nvlist_t ** out)339 simple_chip_label(topo_mod_t *mod, tnode_t *node, topo_version_t vers,
340     nvlist_t *in, nvlist_t **out)
341 {
342 	char *fmtstr, buf[BUFSZ];
343 	int ret;
344 	uint32_t offset;
345 	nvlist_t *args;
346 
347 	topo_mod_dprintf(mod, "simple_chip_label() called\n");
348 	if ((ret = nvlist_lookup_nvlist(in, TOPO_PROP_ARGS, &args)) != 0) {
349 		topo_mod_dprintf(mod, "Failed to lookup 'args' list (%s)\n",
350 		    strerror(ret));
351 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
352 	}
353 	if ((ret = nvlist_lookup_uint32(args, "offset", &offset)) != 0) {
354 		topo_mod_dprintf(mod, "Failed to lookup 'offset' arg (%s)\n",
355 		    strerror(ret));
356 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
357 	}
358 
359 	if ((fmtstr = get_fmtstr(mod, in)) == NULL) {
360 		topo_mod_dprintf(mod, "Failed to retrieve 'format' arg\n");
361 		/* topo errno already set */
362 		return (-1);
363 	}
364 
365 	/* LINTED: E_SEC_PRINTF_VAR_FMT */
366 	(void) snprintf(buf, BUFSZ, fmtstr,
367 	    (topo_node_instance(node) + offset));
368 
369 	if (store_prop_val(mod, buf, "label", out) != 0) {
370 		topo_mod_dprintf(mod, "Failed to set label\n");
371 		/* topo errno already set */
372 		return (-1);
373 	}
374 
375 	return (0);
376 }
377 
378 
379 /*
380  * This is a somewhat generic property method for labelling the CPU sockets on
381  * x86/x64 platforms.  This method assumes a correspondence between
382  * the chip topo node instance number and the CPU socket label number.  It takes
383  * the following argument:
384  *
385  * format:	a string containing a printf-like format with a single %d token
386  *              which this method computes
387  *
388  *              i.e.: CPU %d
389  *
390  * offset:      a numeric offset that we'll number the CPU's from.  This is to
391  *              allow for the fact that some systems number the CPU sockets
392  *              from zero and others start from one (like the X8450 systems)
393  */
394 /* ARGSUSED */
395 int
fsb2_chip_label(topo_mod_t * mod,tnode_t * node,topo_version_t vers,nvlist_t * in,nvlist_t ** out)396 fsb2_chip_label(topo_mod_t *mod, tnode_t *node, topo_version_t vers,
397     nvlist_t *in, nvlist_t **out)
398 {
399 	char *fmtstr, buf[BUFSZ];
400 	int ret;
401 	uint32_t offset;
402 	nvlist_t *args;
403 
404 	topo_mod_dprintf(mod, "fsb2_chip_label() called\n");
405 	if ((ret = nvlist_lookup_nvlist(in, TOPO_PROP_ARGS, &args)) != 0) {
406 		topo_mod_dprintf(mod, "Failed to lookup 'args' list (%s)\n",
407 		    strerror(ret));
408 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
409 	}
410 	if ((ret = nvlist_lookup_uint32(args, "offset", &offset)) != 0) {
411 		topo_mod_dprintf(mod, "Failed to lookup 'offset' arg (%s)\n",
412 		    strerror(ret));
413 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
414 	}
415 
416 	if ((fmtstr = get_fmtstr(mod, in)) == NULL) {
417 		topo_mod_dprintf(mod, "Failed to retrieve 'format' arg\n");
418 		/* topo errno already set */
419 		return (-1);
420 	}
421 
422 	/* LINTED: E_SEC_PRINTF_VAR_FMT */
423 	(void) snprintf(buf, BUFSZ, fmtstr,
424 	    ((topo_node_instance(node) / 2) + offset));
425 
426 	if (store_prop_val(mod, buf, "label", out) != 0) {
427 		topo_mod_dprintf(mod, "Failed to set label\n");
428 		/* topo errno already set */
429 		return (-1);
430 	}
431 
432 	return (0);
433 }
434 
435 
436 /*
437  * This is a custom property method for generating the CPU slot label for the
438  * Galaxy 4E/4F platforms.
439  *
440  * format:	a string containing a printf-like format with a single %c token
441  *              which this method computes
442  *
443  *              i.e.: CPU %c
444  */
445 /* ARGSUSED */
446 int
g4_chip_label(topo_mod_t * mod,tnode_t * node,topo_version_t vers,nvlist_t * in,nvlist_t ** out)447 g4_chip_label(topo_mod_t *mod, tnode_t *node, topo_version_t vers,
448     nvlist_t *in, nvlist_t **out)
449 {
450 	char *fmtstr, buf[BUFSZ], slot_id;
451 	int err, htid, mapidx;
452 	uint32_t num_nodes;
453 	/*
454 	 * G4 HT node ID to FRU label translation.  The g4map array
455 	 * is indexed by (number of coherent nodes) / 2 - 1.
456 	 * The value for a given number of nodes is a char array
457 	 * indexed by node ID.
458 	 */
459 	const char *g4map[] = {
460 	    "AB",	/* 2 nodes */
461 	    "ADEH",	/* 4 nodes */
462 	    "ABDEFH",	/* 6 nodes */
463 	    "ACBDEFGH"	/* 8 nodes */
464 	};
465 
466 	topo_mod_dprintf(mod, "g4_chip_label() called\n");
467 	if ((fmtstr = get_fmtstr(mod, in)) == NULL) {
468 		topo_mod_dprintf(mod, "Failed to retrieve 'format' arg\n");
469 		/* topo errno already set */
470 		return (-1);
471 	}
472 	/*
473 	 * The chip-properties property will not exist if this platform has
474 	 * AMD family 0x10 modules.  In that case we don't want to treat it as a
475 	 * fatal error as that will cause calls like topo_prop_getprops to fail
476 	 * to return any properties on this node.  Therefore, if the topo errno
477 	 * is set to ETOPO_PROP_NOENT, then we'll just set an empty label
478 	 * and return 0.  If the topo errno is set to anything else we'll
479 	 * return -1.
480 	 */
481 	if (topo_prop_get_uint32(node, "chip-properties", "CoherentNodes",
482 	    &num_nodes, &err) != 0) {
483 		if (err == ETOPO_PROP_NOENT) {
484 			if (store_prop_val(mod, "", "label", out) != 0) {
485 				topo_mod_dprintf(mod, "Failed to set label\n");
486 				/* topo errno already set */
487 				return (-1);
488 			}
489 			return (0);
490 		} else {
491 			topo_mod_dprintf(mod, "Failed to lookup 'CoherentNodes'"
492 			    "property\n");
493 			return (topo_mod_seterrno(mod, err));
494 		}
495 	}
496 
497 	mapidx = num_nodes / 2 - 1;
498 	htid = topo_node_instance(node);
499 
500 	/* HT nodes must number 0 .. num_nodes - 1 */
501 	if (htid >= num_nodes) {
502 		topo_mod_dprintf(mod, "chip node instance range check failed:"
503 		    "num_nodes=%d, instance=%d\n", num_nodes, htid);
504 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
505 	}
506 
507 	switch (num_nodes) {
508 		case (2):
509 		case (4):
510 		case (6):
511 		case (8):
512 			/* htid is already range-checked */
513 			mapidx = num_nodes / 2 - 1;
514 			slot_id = g4map[mapidx][htid];
515 			break;
516 		default:
517 			topo_mod_dprintf(mod, "Invalid number of CoherentNodes:"
518 			    " %d\n", num_nodes);
519 			return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
520 	}
521 
522 	/* LINTED: E_SEC_PRINTF_VAR_FMT */
523 	(void) snprintf(buf, BUFSZ, fmtstr, slot_id);
524 
525 	if (store_prop_val(mod, buf, "label", out) != 0) {
526 		topo_mod_dprintf(mod, "Failed to set label\n");
527 		/* topo errno already set */
528 		return (-1);
529 	}
530 
531 	return (0);
532 }
533 
534 /*
535  * Utility function used by a4fplus_chip_label to determine the number of chips
536  * (as opposed to processors) that are installed in the system by counting
537  * the unique chipids.
538  */
539 static int
get_num_chips(topo_mod_t * mod)540 get_num_chips(topo_mod_t *mod)
541 {
542 	fmd_agent_hdl_t *hdl;
543 	nvlist_t **cpus;
544 	uint_t ncpu;
545 	int i, nchip = 0;
546 	int32_t chipid;
547 	uint64_t bitmap = 0;
548 
549 	if ((hdl = fmd_agent_open(FMD_AGENT_VERSION)) == NULL)
550 		return (-1);
551 	if (fmd_agent_physcpu_info(hdl, &cpus, &ncpu) == -1) {
552 		topo_mod_dprintf(mod, "get physcpu info failed:%s\n",
553 		    fmd_agent_errmsg(hdl));
554 		fmd_agent_close(hdl);
555 		return (-1);
556 	}
557 	fmd_agent_close(hdl);
558 
559 	for (i = 0; i < ncpu; i++) {
560 		if (nvlist_lookup_int32(cpus[i], FM_PHYSCPU_INFO_CHIP_ID,
561 		    &chipid) != 0 || chipid >= 64) {
562 			topo_mod_dprintf(mod, "lookup chipid failed\n");
563 			nchip = -1;
564 			break;
565 		}
566 		if ((bitmap & (1ULL << chipid)) != 0) {
567 			bitmap |= (1ULL << chipid);
568 			nchip++;
569 		}
570 	}
571 
572 	for (i = 0; i < ncpu; i++)
573 		nvlist_free(cpus[i]);
574 	umem_free(cpus, sizeof (nvlist_t *) * ncpu);
575 
576 	return (nchip);
577 }
578 
579 /*
580  * This is a custom property method for generating the CPU slot label for the
581  * Andromeda Fplus platforms.
582  *
583  * format:	a string containing a printf-like format with a single %d token
584  *              which this method computes
585  *
586  *              i.e.: CPU %d
587  */
588 /* ARGSUSED */
589 int
a4fplus_chip_label(topo_mod_t * mod,tnode_t * node,topo_version_t vers,nvlist_t * in,nvlist_t ** out)590 a4fplus_chip_label(topo_mod_t *mod, tnode_t *node, topo_version_t vers,
591     nvlist_t *in, nvlist_t **out)
592 {
593 	char *fmtstr, buf[BUFSZ];
594 	int num_nodes;
595 
596 	topo_mod_dprintf(mod, "a4fplus_chip_label() called\n");
597 	if ((fmtstr = get_fmtstr(mod, in)) == NULL) {
598 		topo_mod_dprintf(mod, "Failed to retrieve 'format' arg\n");
599 		/* topo errno already set */
600 		return (-1);
601 	}
602 
603 	/*
604 	 * Normally we'd figure out the total number of chip nodes by looking
605 	 * at the CoherentNodes property.  However, due to the lack of a memory
606 	 * controller driver for family 0x10, this property wont exist on the
607 	 * chip nodes on A4Fplus.
608 	 */
609 	if ((num_nodes = get_num_chips(mod)) < 0) {
610 		topo_mod_dprintf(mod, "Failed to determine number of chip "
611 		    "nodes\n");
612 		return (topo_mod_seterrno(mod, EMOD_UNKNOWN));
613 	}
614 	switch (num_nodes) {
615 		case (2):
616 			/* LINTED: E_SEC_PRINTF_VAR_FMT */
617 			(void) snprintf(buf, BUFSZ, fmtstr,
618 			    topo_node_instance(node) + 2);
619 			break;
620 		case (4):
621 			/* LINTED: E_SEC_PRINTF_VAR_FMT */
622 			(void) snprintf(buf, BUFSZ, fmtstr,
623 			    topo_node_instance(node));
624 			break;
625 		default:
626 			topo_mod_dprintf(mod, "Invalid number of chip nodes:"
627 			    " %d\n", num_nodes);
628 			return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
629 	}
630 
631 
632 	if (store_prop_val(mod, buf, "label", out) != 0) {
633 		topo_mod_dprintf(mod, "Failed to set label\n");
634 		/* topo errno already set */
635 		return (-1);
636 	}
637 
638 	return (0);
639 }
640 
641 /*
642  * This is a somewhat generic property method for labelling the chip-select
643  * nodes on multi-socket AMD family 0x10 platforms.  This is necessary because
644  * these platforms are not supported by the current AMD memory controller driver
645  * and therefore we're not able to discover the memory topology on AMD family
646  * 0x10 systems.  As a result, instead of enumerating the installed dimms and
647  * their ranks, the chip enumerator generically enumerates all of the possible
648  * chip-selects beneath each dram channel.
649  *
650  * When we diagnose a dimm fault, the FRU fmri will be for the chip-select node,
651  * so we need to attach FRU labels to the chip-select nodes.
652  *
653  * format:	a string containing a printf-like format with a two %d tokens
654  *              for the cpu and dimm slot label numbers, which this method
655  *              computes
656  *
657  *              i.e.: CPU %d DIMM %d
658  *
659  * offset:      a numeric offset that we'll number the dimms from.  This is to
660  *              allow for the fact that some systems may number the dimm slots
661  *              from zero while others may start from one
662  *
663  * This function computes the DIMM slot number using the following formula:
664  *
665  *	slot = cs - (cs % 2) + channel + offset
666  */
667 /* ARGSUSED */
668 int
simple_cs_label_mp(topo_mod_t * mod,tnode_t * node,topo_version_t vers,nvlist_t * in,nvlist_t ** out)669 simple_cs_label_mp(topo_mod_t *mod, tnode_t *node, topo_version_t vers,
670     nvlist_t *in, nvlist_t **out)
671 {
672 	char *fmtstr, buf[BUFSZ];
673 	tnode_t *chip, *chan;
674 	int dimm_num, ret;
675 	uint32_t offset;
676 	nvlist_t *args;
677 
678 	topo_mod_dprintf(mod, "simple_cs_label_mp() called\n");
679 
680 	if ((ret = nvlist_lookup_nvlist(in, TOPO_PROP_ARGS, &args)) != 0) {
681 		topo_mod_dprintf(mod, "Failed to lookup 'args' list (%s)\n",
682 		    strerror(ret));
683 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
684 	}
685 	if ((ret = nvlist_lookup_uint32(args, "offset", &offset)) != 0) {
686 		topo_mod_dprintf(mod, "Failed to lookup 'offset' arg (%s)\n",
687 		    strerror(ret));
688 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
689 	}
690 	if ((fmtstr = get_fmtstr(mod, in)) == NULL) {
691 		topo_mod_dprintf(mod, "Failed to retrieve 'format' arg\n");
692 		/* topo errno already set */
693 		return (-1);
694 	}
695 
696 	chip = topo_node_parent(topo_node_parent(topo_node_parent(node)));
697 	chan = topo_node_parent(node);
698 
699 	dimm_num = topo_node_instance(node) - (topo_node_instance(node) % 2)
700 	    + topo_node_instance(chan) + offset;
701 	/* LINTED: E_SEC_PRINTF_VAR_FMT */
702 	(void) snprintf(buf, BUFSZ, fmtstr, topo_node_instance(chip),
703 	    dimm_num);
704 
705 	if (store_prop_val(mod, buf, "label", out) != 0) {
706 		topo_mod_dprintf(mod, "Failed to set label\n");
707 		/* topo errno already set */
708 		return (-1);
709 	}
710 
711 	return (0);
712 }
713 
714 /* ARGSUSED */
715 int
g4_dimm_label(topo_mod_t * mod,tnode_t * node,topo_version_t vers,nvlist_t * in,nvlist_t ** out)716 g4_dimm_label(topo_mod_t *mod, tnode_t *node, topo_version_t vers,
717     nvlist_t *in, nvlist_t **out)
718 {
719 	char *fmtstr, *chip_lbl, buf[BUFSZ];
720 	tnode_t *chip;
721 	int ret, err = 0;
722 	uint32_t offset;
723 	nvlist_t *args;
724 
725 	topo_mod_dprintf(mod, "g4_dimm_label() called\n");
726 
727 	if ((ret = nvlist_lookup_nvlist(in, TOPO_PROP_ARGS, &args)) != 0) {
728 		topo_mod_dprintf(mod, "Failed to lookup 'args' list (%s)\n",
729 		    strerror(ret));
730 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
731 	}
732 	if ((ret = nvlist_lookup_uint32(args, "offset", &offset)) != 0) {
733 		topo_mod_dprintf(mod, "Failed to lookup 'offset' arg (%s)\n",
734 		    strerror(ret));
735 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
736 	}
737 	if ((fmtstr = get_fmtstr(mod, in)) == NULL) {
738 		topo_mod_dprintf(mod, "Failed to retrieve 'format' arg\n");
739 		/* topo errno already set */
740 		return (-1);
741 	}
742 
743 	/*
744 	 * The 4600/4600M2 have a weird way of labeling the chip nodes, so
745 	 * instead of trying to recompute it, we'll simply look it up and
746 	 * prepend it to our dimm label.
747 	 */
748 	chip = topo_node_parent(topo_node_parent(node));
749 	if (topo_prop_get_string(chip, TOPO_PGROUP_PROTOCOL, "label", &chip_lbl,
750 	    &err) != 0) {
751 		topo_mod_dprintf(mod, "Failed to lookup label prop on %s=%d\n",
752 		    topo_node_name(chip), topo_node_instance(chip),
753 		    topo_strerror(err));
754 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
755 	}
756 
757 	/* LINTED: E_SEC_PRINTF_VAR_FMT */
758 	(void) snprintf(buf, BUFSZ, fmtstr, chip_lbl,
759 	    (topo_node_instance(node) + offset));
760 
761 	topo_mod_strfree(mod, chip_lbl);
762 
763 	if (store_prop_val(mod, buf, "label", out) != 0) {
764 		topo_mod_dprintf(mod, "Failed to set label\n");
765 		/* topo errno already set */
766 		return (-1);
767 	}
768 
769 	return (0);
770 }
771 
772 /*
773  * This method is used to compute the labels for DIMM slots on the Galaxy 1F and
774  * 2F platforms.  It results in following dimm node label assignments:
775  *
776  * chip/dimm instances      label
777  * -------------------      -----
778  * chip=0/dimm=0            CPU 1 DIMM A0
779  * chip=0/dimm=1            CPU 1 DIMM B0
780  * chip=0/dimm=2            CPU 1 DIMM A1
781  * chip=0/dimm=3            CPU 1 DIMM B1
782  *
783  * chip=1/dimm=0            CPU 2 DIMM A0
784  * chip=1/dimm=1            CPU 2 DIMM B0
785  * chip=1/dimm=2            CPU 2 DIMM A1
786  * chip=1/dimm=3            CPU 2 DIMM B1
787  */
788 /* ARGSUSED */
789 int
g12f_dimm_label(topo_mod_t * mod,tnode_t * node,topo_version_t vers,nvlist_t * in,nvlist_t ** out)790 g12f_dimm_label(topo_mod_t *mod, tnode_t *node, topo_version_t vers,
791     nvlist_t *in, nvlist_t **out)
792 {
793 	char *fmtstr, buf[BUFSZ], chan;
794 	tnode_t *chip;
795 	int ret, dimm_inst, slot_num;
796 	nvlist_t *args;
797 
798 	topo_mod_dprintf(mod, "g12f_dimm_label() called\n");
799 
800 	if ((ret = nvlist_lookup_nvlist(in, TOPO_PROP_ARGS, &args)) != 0) {
801 		topo_mod_dprintf(mod, "Failed to lookup 'args' list (%s)\n",
802 		    strerror(ret));
803 		return (topo_mod_seterrno(mod, EMOD_NVL_INVAL));
804 	}
805 	if ((fmtstr = get_fmtstr(mod, in)) == NULL) {
806 		topo_mod_dprintf(mod, "Failed to retrieve 'format' arg\n");
807 		/* topo errno already set */
808 		return (-1);
809 	}
810 
811 	chip = topo_node_parent(topo_node_parent(node));
812 	dimm_inst = topo_node_instance(node);
813 	chan = dimm_inst == 0 || dimm_inst == 2 ? 'A': 'B';
814 	slot_num = (dimm_inst <= 1 ? 0 : 1);
815 
816 	/* LINTED: E_SEC_PRINTF_VAR_FMT */
817 	(void) snprintf(buf, BUFSZ, fmtstr, topo_node_instance(chip) + 1, chan,
818 	    slot_num);
819 
820 	if (store_prop_val(mod, buf, "label", out) != 0) {
821 		topo_mod_dprintf(mod, "Failed to set label\n");
822 		/* topo errno already set */
823 		return (-1);
824 	}
825 
826 	return (0);
827 }
828