xref: /illumos-gate/usr/src/uts/intel/os/fmsmb.c (revision 4df55fde49134f9735f84011f23a767c75e393c7)
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  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/types.h>
27 #include <sys/time.h>
28 #include <sys/nvpair.h>
29 #include <sys/cmn_err.h>
30 #include <sys/fm/util.h>
31 #include <sys/fm/protocol.h>
32 #include <sys/smbios.h>
33 #include <sys/smbios_impl.h>
34 
35 /*
36  * Variable used to determine if the x86 generic topology enumerator will
37  * revert to legacy enumeration. I.E. Big Kill Switch... tunable via
38  * /etc/system
39  */
40 int x86gentopo_legacy = 0;
41 
42 #define	MC		0
43 #define	PROC		1
44 #define	MAX_PAIRS	20
45 #define	MAX_CONT	40
46 
47 typedef struct bbindex  {
48 	int count;
49 	uint16_t index[MAX_PAIRS];
50 } bbindex_t;
51 
52 /*
53  * the enum values come from DMTF
54  */
55 typedef enum baseb {
56 	BB_BAD = 0,		/* There is no bb value 0 */
57 	BB_UNKNOWN,		/* Unknown */
58 	BB_OTHER,		/* Other */
59 	BB_BLADE,		/* Server Blade */
60 	BB_CONNSW,		/* Connectivity Switch */
61 	BB_SMM,			/* System Management Module */
62 	BB_PROCMOD,		/* Processor Module */
63 	BB_IOMOD,		/* I/O Module */
64 	BB_MEMMOD,		/* Memory Module */
65 	BB_DBOARD,		/* Daughter Board */
66 	BB_MBOARD,		/* Motherboard */
67 	BB_PROCMMOD,		/* Processor/Memory Module */
68 	BB_PROCIOMOD,		/* Processor/IO Module */
69 	BB_ICONNBD		/* Interconnect Board */
70 } bbd_t;
71 
72 static struct bboard_type {
73 	bbd_t		baseb;
74 	const char	*name;
75 } bbd_type[] = {
76 	{BB_BAD,		NULL},
77 	{BB_UNKNOWN,		"unknown"},
78 	{BB_OTHER,		"other"},
79 	{BB_BLADE,		"systemboard"},
80 	{BB_CONNSW,		"connswitch"},
81 	{BB_SMM,		"smmodule"},
82 	{BB_PROCMOD,		"cpuboard"},
83 	{BB_IOMOD,		"ioboard"},
84 	{BB_MEMMOD,		"memboard"},
85 	{BB_DBOARD,		"systemboard"},
86 	{BB_MBOARD,		"motherboard"},
87 	{BB_PROCMMOD,		"systemboard"},
88 	{BB_PROCIOMOD,		"systemboard"},
89 	{BB_ICONNBD,		"systemboard"}
90 };
91 
92 typedef struct smbs_con_ids {
93 	int id;
94 	int inst;
95 	int cont_count;
96 	uint16_t **cont_ids;
97 	int cont_by_id;
98 	int visited;
99 } smbs_con_ids_t;
100 
101 typedef struct smbs_cnt {
102 	int type;			/* SMBIOS stucture type */
103 	int count;			/* number of table entries */
104 	smbs_con_ids_t **ids;		/* SMBIOS table entry id(s) */
105 } smbs_cnt_t;
106 
107 /*
108  * dynamically allocate the storage for the smbs_cnt_t
109  */
110 static smbs_cnt_t *
111 smb_create_strcnt(int count)
112 {
113 	smbs_cnt_t *types = NULL;
114 	int i, j;
115 
116 	types = kmem_zalloc(sizeof (smbs_cnt_t), KM_SLEEP);
117 
118 	types->ids = (smbs_con_ids_t **)kmem_zalloc(
119 	    count * sizeof (smbs_con_ids_t *), KM_SLEEP);
120 
121 	for (i = 0; i < count; i++) {
122 		types->ids[i] = (smbs_con_ids_t *)kmem_zalloc(
123 		    sizeof (smbs_con_ids_t), KM_SLEEP);
124 	}
125 
126 	for (i = 0; i < count; i++) {
127 		types->ids[i]->cont_ids = (uint16_t **)kmem_zalloc(
128 		    MAX_CONT * sizeof (uint16_t *), KM_SLEEP);
129 	}
130 
131 	for (i = 0; i < count; i++) {
132 		for (j = 0; j < MAX_CONT; j++) {
133 			types->ids[i]->cont_ids[j] = (uint16_t *)kmem_zalloc(
134 			    sizeof (uint16_t), KM_SLEEP);
135 		}
136 	}
137 	return (types);
138 }
139 
140 /*
141  * free the smbs_cnt_t memory
142  */
143 static void
144 smb_free_strcnt(smbs_cnt_t *types, int count)
145 {
146 	int i, j;
147 
148 	if (types == NULL)
149 		return;
150 
151 	for (i = 0; i < count; i++) {
152 		for (j = 0; j < MAX_CONT; j++) {
153 			if (types->ids[i]->cont_ids[j] != NULL)
154 				kmem_free(types->ids[i]->cont_ids[j],
155 				    sizeof (uint16_t));
156 		}
157 	}
158 
159 	for (i = 0; i < count; i++) {
160 		if (types->ids[i]->cont_ids != NULL)
161 			kmem_free(types->ids[i]->cont_ids,
162 			    MAX_CONT * sizeof (uint16_t *));
163 	}
164 
165 	for (i = 0; i < count; i++) {
166 		if (types->ids[i] != NULL)
167 			kmem_free(types->ids[i], sizeof (smbs_con_ids_t));
168 	}
169 
170 	if (types->ids != NULL)
171 		kmem_free(types->ids, count * sizeof (smbs_con_ids_t *));
172 
173 	if (types != NULL)
174 		kmem_free(types, sizeof (smbs_cnt_t));
175 
176 }
177 
178 /*
179  * count number of the structure type in the ksmbios
180  */
181 static int
182 smb_cnttypes(smbios_hdl_t *shp, int type)
183 {
184 	const smb_struct_t *sp = shp->sh_structs;
185 	int nstructs = shp->sh_nstructs;
186 	int i;
187 	int cnt = 0;
188 
189 	for (i = 0, cnt = 0; i < nstructs; i++, sp++) {
190 		if (sp->smbst_hdr->smbh_type == type)
191 			cnt++;
192 	}
193 	return (cnt);
194 }
195 
196 static void
197 smb_strcnt(smbios_hdl_t *shp, smbs_cnt_t *stype)
198 {
199 	const smb_struct_t *sp = shp->sh_structs;
200 	int nstructs = shp->sh_nstructs;
201 	smbios_bboard_t bb;
202 	int i, cnt;
203 	int mb_cnt = 0;
204 	int cpub_cnt = 0;
205 	int sysb_cnt = 0;
206 	int memb_cnt = 0;
207 	int iob_cnt = 0;
208 	int inst = 0;
209 	int rc = 0;
210 
211 	for (i = 0, cnt = 0; i < nstructs; i++, sp++) {
212 		if (sp->smbst_hdr->smbh_type == stype->type) {
213 			stype->ids[cnt]->id = sp->smbst_hdr->smbh_hdl;
214 			stype->ids[cnt]->inst = cnt;
215 			stype->ids[cnt]->visited = 0;
216 			stype->ids[cnt]->cont_by_id = -1;
217 			if (stype->type == SMB_TYPE_BASEBOARD) {
218 				rc = smbios_info_bboard(shp,
219 				    stype->ids[cnt]->id, &bb);
220 				if (rc == 0) {
221 					switch (bb.smbb_type) {
222 						case SMB_BBT_PROC :
223 							inst = cpub_cnt++;
224 							break;
225 						case SMB_BBT_IO :
226 							inst = iob_cnt++;
227 							break;
228 						case SMB_BBT_MEM :
229 							inst = memb_cnt++;
230 							break;
231 						case SMB_BBT_MOTHER :
232 							inst = mb_cnt++;
233 							break;
234 						default:
235 							/*
236 							 * SMB_BBT_UNKNOWN
237 							 * SMB_BBT_OTHER
238 							 * SMB_BBT_SBLADE
239 							 * SMB_BBT_CSWITCH
240 							 * SMB_BBT_SMM
241 							 * SMB_BBT_DAUGHTER
242 							 * SMB_BBT_PROCMEM
243 							 * SMB_BBT_PROCIO
244 							 * SMB_BBT_INTER
245 							 */
246 							inst = sysb_cnt++;
247 							break;
248 					}
249 					stype->ids[cnt]->inst = inst;
250 				}
251 			}
252 			cnt++;
253 		}
254 	}
255 	stype->count = cnt;
256 }
257 
258 /*
259  * Go through the smbios structures looking for type 2. Fill in
260  * the cont_id and cont_by_id for each type 2
261  *
262  */
263 static void
264 smb_bb_contains(smbios_hdl_t *shp, smbs_cnt_t *stype)
265 {
266 	int i, j, cnt, c;
267 	uint_t cont_count;
268 	const smb_struct_t *spt;
269 	smbios_bboard_t smb_bb;
270 	uint16_t bb_id, cont_id;
271 	uint_t cont_len;
272 	id_t *cont_hdl = NULL;
273 	int rc;
274 
275 	for (cnt = 0; cnt < stype->count; cnt++) {
276 		bb_id = stype->ids[cnt]->id;
277 		(void) smbios_info_bboard(shp, stype->ids[cnt]->id, &smb_bb);
278 		cont_count = (uint_t)smb_bb.smbb_contn;
279 		if (cont_count == 0) {
280 			continue;
281 		}
282 
283 		cont_len = sizeof (id_t);
284 		cont_hdl = kmem_zalloc(cont_count * cont_len, KM_SLEEP);
285 		if (cont_hdl == NULL)
286 			continue;
287 
288 		rc = smbios_info_contains(shp, stype->ids[cnt]->id,
289 		    cont_count, cont_hdl);
290 		if (rc > SMB_CONT_MAX) {
291 			kmem_free(cont_hdl, cont_count * cont_len);
292 			continue;
293 		}
294 		cont_count = MIN(rc, cont_count);
295 
296 		/*
297 		 * fill in the type 2 and type 4 ids which are
298 		 * contained in this type 2
299 		 */
300 		c = 0;
301 		for (j = 0; j < cont_count; j++) {
302 			cont_id = (uint16_t)cont_hdl[j];
303 			spt = smb_lookup_id(shp, cont_id);
304 			if (spt->smbst_hdr->smbh_type == SMB_TYPE_BASEBOARD ||
305 			    spt->smbst_hdr->smbh_type == SMB_TYPE_PROCESSOR) {
306 				*stype->ids[cnt]->cont_ids[c] = cont_id;
307 				c++;
308 			}
309 
310 			if (spt->smbst_hdr->smbh_type == SMB_TYPE_BASEBOARD) {
311 				for (i = 0; i < stype->count; i++) {
312 					if (stype->ids[i]->id == cont_id) {
313 						stype->ids[i]->cont_by_id =
314 						    bb_id;
315 					}
316 				}
317 			}
318 
319 		}
320 		stype->ids[cnt]->cont_count = c;
321 		if (cont_hdl != NULL)
322 			kmem_free(cont_hdl, cont_count * cont_len);
323 	}
324 }
325 
326 /*
327  * Verify SMBIOS structures for x86 generic topology.
328  *
329  * Return (0) on success.
330  */
331 static int
332 fm_smb_check(smbios_hdl_t *shp)
333 {
334 	int i;
335 	int bb_cnt = 0;
336 	int pr_cnt = 0;
337 	int expr_cnt = 0;
338 	int ma_cnt = 0;
339 	int exma_cnt = 0;
340 	int mdev_cnt = 0;
341 	int exmdev_cnt = 0;
342 	uint16_t bb_id;
343 	uint16_t pr_id, expr_id;
344 	uint16_t ma_id, exma_id;
345 	uint16_t mdev_id, exmdev_id;
346 	smbios_bboard_t bb;
347 	smbios_processor_ext_t exproc;
348 	smbios_memarray_ext_t exma;
349 	smbios_memdevice_ext_t exmdev;
350 	smbs_cnt_t *bb_stype;
351 	smbs_cnt_t *pr_stype, *expr_stype;
352 	smbs_cnt_t *ma_stype, *exma_stype;
353 	smbs_cnt_t *mdev_stype, *exmdev_stype;
354 
355 	/*
356 	 * Verify the existance of the requuired extended OEM-Specific
357 	 * structures and they coincide with the structures they extend
358 	 * (e.g. the number of extended processor structures equal the
359 	 * number of processor structures).
360 	 */
361 	pr_cnt = smb_cnttypes(shp, SMB_TYPE_PROCESSOR);
362 	expr_cnt = smb_cnttypes(shp, SUN_OEM_EXT_PROCESSOR);
363 	ma_cnt = smb_cnttypes(shp, SMB_TYPE_MEMARRAY);
364 	exma_cnt = smb_cnttypes(shp, SUN_OEM_EXT_MEMARRAY);
365 	mdev_cnt = smb_cnttypes(shp, SMB_TYPE_MEMDEVICE);
366 	exmdev_cnt = smb_cnttypes(shp, SUN_OEM_EXT_MEMDEVICE);
367 	if (expr_cnt == 0 || exma_cnt == 0 || exmdev_cnt == 0 ||
368 	    expr_cnt != pr_cnt || exma_cnt != ma_cnt ||
369 	    exmdev_cnt != mdev_cnt) {
370 #ifdef	DEBUG
371 		cmn_err(CE_NOTE, "!Structure mismatch: ext_proc (%d) "
372 		    "proc (%d) ext_ma (%d) ma (%d) ext_mdev (%d) mdev (%d)\n",
373 		    expr_cnt, pr_cnt, exma_cnt, ma_cnt, exmdev_cnt,
374 		    mdev_cnt);
375 #endif	/* DEBUG */
376 		return (-1);
377 	}
378 
379 	/*
380 	 * Verify the OEM-Specific structrures are correctly
381 	 * linked to the SMBIOS structure types they extend.
382 	 */
383 
384 	/* allocate processor stypes */
385 	pr_stype = smb_create_strcnt(pr_cnt);
386 	expr_stype = smb_create_strcnt(expr_cnt);
387 
388 	/* fill in stypes */
389 	pr_stype->type = SMB_TYPE_PROCESSOR;
390 	smb_strcnt(shp, pr_stype);
391 	expr_stype->type = SUN_OEM_EXT_PROCESSOR;
392 	smb_strcnt(shp, expr_stype);
393 
394 	/* verify the ext proc struct belong to the proc struct */
395 	for (i = 0; i < pr_cnt; i++) {
396 		pr_id = pr_stype->ids[i]->id;
397 		expr_id = expr_stype->ids[i]->id;
398 		(void) smbios_info_extprocessor(shp, expr_id, &exproc);
399 		if (exproc.smbpe_processor != pr_id) {
400 #ifdef	DEBUG
401 			cmn_err(CE_NOTE, "!Processor struct linkage (%d)", i);
402 #endif	/* DEBUG */
403 			smb_free_strcnt(pr_stype, pr_cnt);
404 			smb_free_strcnt(expr_stype, expr_cnt);
405 			return (-1);
406 		}
407 	}
408 
409 	/* free stypes */
410 	smb_free_strcnt(pr_stype, pr_cnt);
411 	smb_free_strcnt(expr_stype, expr_cnt);
412 
413 	/* allocate memory array stypes */
414 	ma_stype = smb_create_strcnt(ma_cnt);
415 	exma_stype = smb_create_strcnt(exma_cnt);
416 
417 	/* fill in stypes */
418 	ma_stype->type = SMB_TYPE_MEMARRAY;
419 	smb_strcnt(shp, ma_stype);
420 	exma_stype->type = SUN_OEM_EXT_MEMARRAY;
421 	smb_strcnt(shp, exma_stype);
422 
423 	/* verify linkage from ext memarray struct to memarray struct */
424 	for (i = 0; i < ma_cnt; i++) {
425 		ma_id = ma_stype->ids[i]->id;
426 		exma_id = exma_stype->ids[i]->id;
427 		(void) smbios_info_extmemarray(shp, exma_id, &exma);
428 		if (exma.smbmae_ma != ma_id) {
429 #ifdef	DEBUG
430 			cmn_err(CE_NOTE,
431 			    "!Memory Array struct linkage (%d)", i);
432 #endif	/* DEBUG */
433 			smb_free_strcnt(ma_stype, ma_cnt);
434 			smb_free_strcnt(exma_stype, exma_cnt);
435 			return (-1);
436 		}
437 	}
438 
439 	/* free stypes */
440 	smb_free_strcnt(ma_stype, ma_cnt);
441 	smb_free_strcnt(exma_stype, exma_cnt);
442 
443 	/* allocate memory device stypes */
444 	mdev_stype = smb_create_strcnt(mdev_cnt);
445 	exmdev_stype = smb_create_strcnt(exmdev_cnt);
446 
447 	/* fill in stypes */
448 	mdev_stype->type = SMB_TYPE_MEMDEVICE;
449 	smb_strcnt(shp, mdev_stype);
450 	exmdev_stype->type = SUN_OEM_EXT_MEMDEVICE;
451 	smb_strcnt(shp, exmdev_stype);
452 
453 	/* verify linkage */
454 	for (i = 0; i < mdev_cnt; i++) {
455 		mdev_id = mdev_stype->ids[i]->id;
456 		exmdev_id = exmdev_stype->ids[i]->id;
457 		(void) smbios_info_extmemdevice(shp, exmdev_id, &exmdev);
458 		if (exmdev.smbmdeve_md != mdev_id) {
459 #ifdef	DEBUG
460 			cmn_err(CE_NOTE, "!Memory Device struct linkage (%d)",
461 			    i);
462 #endif	/* DEBUG */
463 			smb_free_strcnt(mdev_stype, mdev_cnt);
464 			smb_free_strcnt(exmdev_stype, exmdev_cnt);
465 			return (-1);
466 		}
467 	}
468 
469 	/* free stypes */
470 	smb_free_strcnt(mdev_stype, mdev_cnt);
471 	smb_free_strcnt(exmdev_stype, exmdev_cnt);
472 
473 	/*
474 	 * Verify the presece of contained handles if there are more
475 	 * than one Type-2 (Base Board) structures.
476 	 */
477 	bb_cnt = smb_cnttypes(shp, SMB_TYPE_BASEBOARD);
478 	if (bb_cnt > 1) {
479 		/* allocate base board stypes */
480 		bb_stype = smb_create_strcnt(bb_cnt);
481 
482 		/* fill in stypes */
483 		bb_stype->type = SMB_TYPE_BASEBOARD;
484 		smb_strcnt(shp, bb_stype);
485 
486 		/* verify contained handles */
487 		for (i = 0; i < bb_cnt; i++) {
488 			bb_id = bb_stype->ids[i]->id;
489 			(void) smbios_info_bboard(shp, bb_id, &bb);
490 			if (bb.smbb_contn == 0) {
491 #ifdef	DEBUG
492 				cmn_err(CE_NOTE, "!No contained hanldes (%d)",
493 				    i);
494 #endif	/* DEBUG */
495 				smb_free_strcnt(bb_stype, bb_cnt);
496 				return (-1);
497 			}
498 		}
499 
500 		/* free stypes */
501 		smb_free_strcnt(bb_stype, bb_cnt);
502 	}
503 
504 	return (0);
505 }
506 
507 void
508 fm_smb_fmacompat()
509 {
510 	int i, j;
511 	int id;
512 	int cnt;
513 	const char **oem_strings = NULL;
514 	smbs_cnt_t *oemstypes;
515 	smbios_hdl_t *shp;
516 	int strcnt;
517 	int compat = 0;
518 
519 	/* check for BKS */
520 	if (x86gentopo_legacy == 1) {
521 		return;
522 	}
523 
524 	shp = ksmbios;
525 	if (shp == NULL) {
526 		goto bad;
527 	}
528 
529 	/* OEM strings (Type 11) */
530 	strcnt = smb_cnttypes(shp, SMB_TYPE_OEMSTR);
531 	if (strcnt == 0)
532 		goto bad;
533 
534 	oemstypes = smb_create_strcnt(strcnt);
535 	if (oemstypes == NULL)
536 		goto bad;
537 
538 	oemstypes->type = SMB_TYPE_OEMSTR;
539 	smb_strcnt(shp, oemstypes);
540 
541 	for (i = 0; i < oemstypes->count; i++) {
542 		id = oemstypes->ids[i]->id;
543 		cnt = smbios_info_strtab(shp, id, 0, NULL);
544 		if (cnt > 0) {
545 			oem_strings = kmem_zalloc(sizeof (char *) * cnt,
546 			    KM_SLEEP);
547 			(void) smbios_info_strtab(shp, id, cnt, oem_strings);
548 
549 			for (j = 0; j < cnt; j++) {
550 				if (strncmp(oem_strings[j], SMB_PRMS1,
551 				    strlen(SMB_PRMS1) + 1) == 0) {
552 					kmem_free(oem_strings,
553 					    sizeof (char *) * cnt);
554 					smb_free_strcnt(oemstypes, strcnt);
555 					compat = 1;
556 					break;
557 				}
558 			}
559 		}
560 	}
561 
562 	if (compat == 0) {
563 		/* didn't find x86pi magic cookie */
564 		if (oem_strings != NULL)
565 			kmem_free(oem_strings, sizeof (char *) * cnt);
566 		smb_free_strcnt(oemstypes, strcnt);
567 		goto bad;
568 	}
569 
570 	/* sanity check SMBIOS structures */
571 	if (fm_smb_check(shp) == 0)
572 		return;
573 
574 bad:
575 	/* not compatible with x86gentopo; revert to legacy enumeration */
576 #ifdef	DEBUG
577 	cmn_err(CE_NOTE,
578 	    "!SMBIOS is not compatible with x86 generic topology.");
579 	cmn_err(CE_NOTE, "!Invoking legacy x86 topology enumeration.");
580 #endif	/* DEBUG */
581 	x86gentopo_legacy = 1;
582 }
583 
584 static int
585 find_matching_apic(smbios_hdl_t *shp, uint16_t proc_id, uint_t strand_apicid)
586 {
587 	uint16_t ext_id;
588 	int i, j;
589 	smbios_processor_ext_t ep;
590 	smbs_cnt_t *pstypes;
591 	int strcnt;
592 
593 	strcnt = smb_cnttypes(shp, SUN_OEM_EXT_PROCESSOR);
594 	if (strcnt == 0)
595 		return (0);
596 
597 	pstypes = smb_create_strcnt(strcnt);
598 	if (pstypes == NULL)
599 		return (0);
600 
601 	pstypes->type = SUN_OEM_EXT_PROCESSOR;
602 	smb_strcnt(shp, pstypes);
603 	for (i = 0; i < pstypes->count; i++) {
604 		ext_id = pstypes->ids[i]->id;
605 		(void) smbios_info_extprocessor(shp, ext_id, &ep);
606 		if (ep.smbpe_processor == proc_id) {
607 			for (j = 0; j < ep.smbpe_n; j++) {
608 				if (ep.smbpe_apicid[j] == strand_apicid) {
609 					smb_free_strcnt(pstypes, strcnt);
610 					return (1);
611 				}
612 			}
613 		}
614 	}
615 	smb_free_strcnt(pstypes, strcnt);
616 	return (0);
617 }
618 
619 /*
620  * go throught the type 2 structure contained_ids looking for
621  * the type 4 which  has strand_apicid == this strand_apicid
622  */
623 static int
624 find_matching_proc(smbios_hdl_t *shp, uint_t strand_apicid,
625     uint16_t bb_id, uint16_t proc_hdl, int is_proc)
626 {
627 	int n;
628 	const smb_struct_t *sp;
629 	smbios_bboard_t bb;
630 	uint_t cont_count, cont_len;
631 	uint16_t cont_id;
632 	id_t *cont_hdl = NULL;
633 	int rc;
634 
635 
636 	(void) smbios_info_bboard(shp, bb_id, &bb);
637 	cont_count = (uint_t)bb.smbb_contn;
638 	if (cont_count == 0)
639 		return (0);
640 
641 	cont_len = sizeof (id_t);
642 	cont_hdl = kmem_zalloc(cont_count * cont_len, KM_SLEEP);
643 	if (cont_hdl == NULL)
644 		return (0);
645 
646 	rc = smbios_info_contains(shp, bb_id, cont_count, cont_hdl);
647 	if (rc > SMB_CONT_MAX) {
648 		kmem_free(cont_hdl, cont_count * cont_len);
649 		return (0);
650 	}
651 	cont_count = MIN(rc, cont_count);
652 
653 	for (n = 0; n < cont_count; n++) {
654 		cont_id = (uint16_t)cont_hdl[n];
655 		sp = smb_lookup_id(shp, cont_id);
656 		if (sp->smbst_hdr->smbh_type == SMB_TYPE_PROCESSOR) {
657 			if (is_proc) {
658 				if (find_matching_apic(shp, cont_id,
659 				    strand_apicid)) {
660 					kmem_free(cont_hdl,
661 					    cont_count * cont_len);
662 					return (1);
663 				}
664 			} else {
665 				if (cont_id == proc_hdl) {
666 					kmem_free(cont_hdl,
667 					    cont_count * cont_len);
668 					return (1);
669 				}
670 			}
671 		}
672 	}
673 	if (cont_hdl != NULL)
674 		kmem_free(cont_hdl, cont_count * cont_len);
675 
676 	return (0);
677 }
678 
679 void
680 get_bboard_index(smbs_cnt_t *bbstypes, uint_t bb_id, bbindex_t *bb_idx)
681 {
682 	int curr_id, tmp_id;
683 	int i, j, nb;
684 	bbindex_t tmp_idx;
685 
686 	for (i = 0; i < MAX_PAIRS; i++)
687 		tmp_idx.index[i] = 0;
688 
689 	tmp_idx.count = 0;
690 
691 	curr_id = bb_id;
692 	for (nb = bbstypes->count-1, i = 0; nb >= 0; nb--) {
693 		tmp_id = bbstypes->ids[nb]->id;
694 		if (tmp_id == curr_id) {
695 			tmp_idx.index[i] = nb;
696 			tmp_idx.count++;
697 			curr_id = bbstypes->ids[nb]->cont_by_id;
698 			if (curr_id == -1)
699 				break;
700 			i++;
701 		}
702 	}
703 
704 	for (i = tmp_idx.count - 1, j = 0; i >= 0; i--) {
705 		bb_idx->index[j] = tmp_idx.index[i];
706 		j++;
707 	}
708 
709 	bb_idx->count = tmp_idx.count;
710 }
711 
712 int
713 get_chassis_inst(smbios_hdl_t *shp, uint16_t *chassis_inst,
714     uint16_t bb_id, int *chcnt)
715 {
716 	int ch_strcnt;
717 	smbs_cnt_t *chstypes;
718 	uint16_t chassis_id, tmp_id;
719 	smbios_bboard_t bb;
720 	int rc = 0;
721 	int i;
722 
723 	rc = smbios_info_bboard(shp, bb_id, &bb);
724 	if (rc != 0) {
725 		return (-1);
726 	}
727 
728 	chassis_id = bb.smbb_chassis;
729 
730 	ch_strcnt = smb_cnttypes(shp, SMB_TYPE_CHASSIS);
731 
732 	if (ch_strcnt == 0)
733 		return (-1);
734 
735 	chstypes = smb_create_strcnt(ch_strcnt);
736 	if (chstypes == NULL)
737 		return (-1);
738 
739 	chstypes->type = SMB_TYPE_CHASSIS;
740 	smb_strcnt(shp, chstypes);
741 
742 	for (i = 0; i < chstypes->count; i++) {
743 		tmp_id = chstypes->ids[i]->id;
744 		if (tmp_id == chassis_id) {
745 			*chassis_inst = chstypes->ids[i]->inst;
746 			if (chstypes->ids[i]->inst != 0)
747 				*chcnt = 2;
748 			else
749 				*chcnt = 1;
750 			smb_free_strcnt(chstypes, ch_strcnt);
751 			return (0);
752 		}
753 	}
754 
755 	smb_free_strcnt(chstypes, ch_strcnt);
756 	return (-1);
757 }
758 
759 int
760 smb_get_bb_fmri(smbios_hdl_t *shp, nvlist_t *fmri,  uint_t parent,
761     smbs_cnt_t *bbstypes)
762 {
763 	int rc = 0;
764 	int i, j, n, cnt;
765 	int id, index;
766 	nvlist_t *pairs[MAX_PAIRS];
767 	smbios_bboard_t bb;
768 	uint16_t chassis_inst, mch_inst;
769 	char name[40];
770 	char idstr[11];
771 	bbindex_t bb_idx;
772 	uint16_t bbid;
773 	int chcnt = 0;
774 
775 	for (n = 0; n < MAX_PAIRS; n++) {
776 		bb_idx.index[n] = 0;
777 		pairs[n] = NULL;
778 	}
779 	bb_idx.count = 0;
780 
781 	get_bboard_index(bbstypes, parent, &bb_idx);
782 
783 	index = bb_idx.index[0];
784 	bbid = bbstypes->ids[index]->id;
785 
786 	rc = get_chassis_inst(shp, &chassis_inst, bbid, &chcnt);
787 
788 	if (rc != 0) {
789 		return (rc);
790 	}
791 
792 	if ((bb_idx.count + chcnt) > MAX_PAIRS) {
793 		return (-1);
794 	}
795 
796 	i = 0;
797 	if (chcnt > 1) {
798 		/*
799 		 * create main chassis pair
800 		 */
801 		pairs[i] = fm_nvlist_create(NULL);
802 		if (pairs[i] == NULL) {
803 			return (-1);
804 		}
805 		mch_inst = 0;
806 		(void) snprintf(idstr, sizeof (idstr), "%u", mch_inst);
807 		if ((nvlist_add_string(pairs[i], FM_FMRI_HC_NAME,
808 		    "chassis") != 0) ||
809 		    (nvlist_add_string(pairs[i], FM_FMRI_HC_ID, idstr)) != 0) {
810 			fm_nvlist_destroy(pairs[i], FM_NVA_FREE);
811 			return (-1);
812 		}
813 		i++;
814 	}
815 
816 	/*
817 	 * create chassis pair
818 	 */
819 	pairs[i] = fm_nvlist_create(NULL);
820 	if (pairs[i] == NULL) {
821 		for (n = 0; n < MAX_PAIRS; n++) {
822 			if (pairs[n] != NULL)
823 				fm_nvlist_destroy(pairs[n], FM_NVA_FREE);
824 		}
825 		return (-1);
826 	}
827 	(void) snprintf(idstr, sizeof (idstr), "%u", chassis_inst);
828 	if ((nvlist_add_string(pairs[i], FM_FMRI_HC_NAME, "chassis") != 0) ||
829 	    (nvlist_add_string(pairs[i], FM_FMRI_HC_ID, idstr) != 0)) {
830 		for (n = 0; n < MAX_PAIRS; n++) {
831 			if (pairs[n] != NULL)
832 				fm_nvlist_destroy(pairs[n], FM_NVA_FREE);
833 		}
834 		return (-1);
835 	}
836 
837 	for (j = 0, i = chcnt, cnt = chcnt; j < bb_idx.count; j++) {
838 		index = bb_idx.index[j];
839 		bbid = bbstypes->ids[index]->id;
840 		rc =  smbios_info_bboard(shp, bbid, &bb);
841 		if (rc != 0) {
842 			rc = -1;
843 			break;
844 		}
845 
846 		pairs[i] = fm_nvlist_create(NULL);
847 		if (pairs[i] == NULL) {
848 			rc = -1;
849 			break;
850 		}
851 
852 		id = bbstypes->ids[index]->inst;
853 		(void) snprintf(idstr, sizeof (idstr), "%u", id);
854 		(void) strncpy(name, bbd_type[bb.smbb_type].name,
855 		    sizeof (name));
856 		cnt++;
857 
858 		if (nvlist_add_string(pairs[i], FM_FMRI_HC_NAME, name) != 0 ||
859 		    nvlist_add_string(pairs[i], FM_FMRI_HC_ID, idstr)
860 		    != 0) {
861 			rc = -1;
862 			break;
863 		}
864 		i++;
865 	}
866 
867 	if (rc != -1) {
868 		if (nvlist_add_nvlist_array(fmri, FM_FMRI_HC_LIST,
869 		    pairs, cnt) != 0) {
870 			rc = -1;
871 		}
872 	}
873 
874 	for (n = 0; n < cnt; n++) {
875 		if (pairs[n] != NULL)
876 			fm_nvlist_destroy(pairs[n], FM_NVA_FREE);
877 	}
878 
879 	return (rc);
880 }
881 
882 /*
883  * pass in strand_apic id
884  * return chip's bboards list which has strand_apicid == passed
885  * in strand_apic id
886  */
887 static nvlist_t *
888 smb_bboard(uint_t strand_apicid, uint16_t proc_hdl, int is_proc)
889 {
890 	smbios_hdl_t *shp;
891 	smbs_cnt_t *bbstypes;
892 	int nb;
893 	int bb_smbid;
894 	nvlist_t *fmri = NULL;
895 	int rc = 0;
896 	int bb_strcnt;
897 
898 	if (x86gentopo_legacy)
899 		return (NULL);
900 
901 	shp = ksmbios;
902 	if (shp == NULL) {
903 		goto bad;
904 	}
905 
906 	/*
907 	 * Type 2 structs : "base board"
908 	 */
909 	bb_strcnt = smb_cnttypes(shp, SMB_TYPE_BASEBOARD);
910 	if (bb_strcnt == 0) {
911 		goto bad;
912 	}
913 
914 	bbstypes = smb_create_strcnt(bb_strcnt);
915 	if (bbstypes == NULL)  {
916 		goto bad;
917 	}
918 
919 	bbstypes->type = SMB_TYPE_BASEBOARD;
920 	smb_strcnt(shp, bbstypes);
921 	smb_bb_contains(shp, bbstypes);
922 
923 	for (nb = 0; nb < bbstypes->count; nb++) {
924 		if (bbstypes->ids[nb]->visited) {
925 			continue;
926 		}
927 
928 		bbstypes->ids[nb]->visited = 1;
929 		bb_smbid = bbstypes->ids[nb]->id;
930 
931 		/*
932 		 * check if there is a matching  processor under
933 		 * this board. If found, find base board(s) of this proc
934 		 * If proc is not in contained handle of a base board and
935 		 * there is only one base board in the system, treat that base
936 		 * board as the parent of the proc
937 		 */
938 		if (find_matching_proc(shp, strand_apicid,
939 		    bb_smbid, proc_hdl, is_proc) || (bbstypes->count == 1)) {
940 			fmri = fm_nvlist_create(NULL);
941 			if (fmri == NULL) {
942 				smb_free_strcnt(bbstypes, bb_strcnt);
943 				goto bad;
944 			}
945 			/*
946 			 * find parent by walking the cont_by_id
947 			 */
948 			rc = smb_get_bb_fmri(shp, fmri, bb_smbid, bbstypes);
949 			smb_free_strcnt(bbstypes, bb_strcnt);
950 			if (rc == 0) {
951 				return (fmri);
952 			} else
953 				goto bad;
954 		}
955 
956 	}
957 
958 	smb_free_strcnt(bbstypes, bb_strcnt);
959 bad:
960 	/* revert to legacy enumeration */
961 	x86gentopo_legacy = 1;
962 
963 	return (NULL);
964 }
965 
966 nvlist_t *
967 fm_smb_bboard(uint_t strand_apicid)
968 {
969 	return (smb_bboard(strand_apicid, 0, PROC));
970 }
971 
972 int
973 fm_smb_chipinst(uint_t strand_apicid, uint_t *chip_inst, uint16_t *smbiosid)
974 {
975 	int n;
976 	smbios_hdl_t *shp;
977 	uint16_t proc_id;
978 	smbs_cnt_t *pstypes;
979 	int strcnt;
980 
981 	if (x86gentopo_legacy)
982 		return (-1);
983 
984 	shp = ksmbios;
985 	if (shp == NULL) {
986 		goto bad;
987 	}
988 
989 	strcnt = smb_cnttypes(shp, SMB_TYPE_PROCESSOR);
990 	if (strcnt == 0)
991 		goto bad;
992 
993 	pstypes = smb_create_strcnt(strcnt);
994 	if (pstypes == NULL)
995 		goto bad;
996 
997 	pstypes->type = SMB_TYPE_PROCESSOR;
998 	smb_strcnt(shp, pstypes);
999 	for (n = 0; n < pstypes->count; n++) {
1000 		proc_id = pstypes->ids[n]->id;
1001 		if (find_matching_apic(shp, proc_id, strand_apicid)) {
1002 			*chip_inst = pstypes->ids[n]->inst;
1003 			*smbiosid = pstypes->ids[n]->id;
1004 			smb_free_strcnt(pstypes, strcnt);
1005 			return (0);
1006 		}
1007 	}
1008 	smb_free_strcnt(pstypes, strcnt);
1009 bad:
1010 	/* revert to legacy enumerarion */
1011 	x86gentopo_legacy = 1;
1012 
1013 	return (-1);
1014 }
1015 
1016 nvlist_t *
1017 fm_smb_mc_bboards(uint_t bdf)
1018 {
1019 
1020 	int i;
1021 	smbios_hdl_t *shp;
1022 	uint16_t ext_id;
1023 	smbios_memarray_ext_t em;
1024 	nvlist_t *fmri = NULL;
1025 	smbs_cnt_t *mastypes;
1026 	int strcnt;
1027 
1028 	if (x86gentopo_legacy)
1029 		return (NULL);
1030 
1031 	shp = ksmbios;
1032 	if (shp == NULL) {
1033 		goto bad;
1034 	}
1035 
1036 	strcnt = smb_cnttypes(shp, SUN_OEM_EXT_MEMARRAY);
1037 	if (strcnt == 0)
1038 		goto bad;
1039 
1040 	mastypes = smb_create_strcnt(strcnt);
1041 	if (mastypes == NULL)
1042 		goto bad;
1043 
1044 	mastypes->type = SUN_OEM_EXT_MEMARRAY;
1045 	smb_strcnt(shp, mastypes);
1046 	for (i = 0; i < mastypes->count; i++) {
1047 		ext_id = mastypes->ids[i]->id;
1048 		(void) smbios_info_extmemarray(shp, ext_id, &em);
1049 		if (em.smbmae_bdf == bdf) {
1050 			fmri = smb_bboard(0, em.smbmae_comp, MC);
1051 			smb_free_strcnt(mastypes, strcnt);
1052 			return (fmri);
1053 		}
1054 	}
1055 	smb_free_strcnt(mastypes, strcnt);
1056 bad:
1057 	/* revert to legacy enumerarion */
1058 	x86gentopo_legacy = 1;
1059 
1060 	return (NULL);
1061 }
1062 
1063 int
1064 fm_smb_mc_chipinst(uint_t bdf, uint_t *chip_inst) {
1065 
1066 	int i, j;
1067 	smbios_hdl_t *shp;
1068 	smbios_memarray_ext_t em;
1069 	uint16_t ext_id, proc_id;
1070 	smbs_cnt_t *mastypes;
1071 	smbs_cnt_t *pstypes;
1072 	int ma_strcnt, p_strcnt;
1073 
1074 	if (x86gentopo_legacy)
1075 		return (-1);
1076 
1077 	shp = ksmbios;
1078 	if (shp == NULL) {
1079 		goto bad;
1080 	}
1081 
1082 	ma_strcnt = smb_cnttypes(shp, SUN_OEM_EXT_MEMARRAY);
1083 	if (ma_strcnt == 0)
1084 		goto bad;
1085 
1086 	mastypes = smb_create_strcnt(ma_strcnt);
1087 	if (mastypes == NULL)
1088 		goto bad;
1089 
1090 	mastypes->type = SUN_OEM_EXT_MEMARRAY;
1091 	smb_strcnt(shp, mastypes);
1092 	for (i = 0; i < mastypes->count; i++) {
1093 		ext_id = mastypes->ids[i]->id;
1094 		(void) smbios_info_extmemarray(shp, ext_id, &em);
1095 		    if (em.smbmae_bdf == bdf) {
1096 			p_strcnt = smb_cnttypes(shp, SMB_TYPE_PROCESSOR);
1097 			if (p_strcnt == 0) {
1098 				smb_free_strcnt(mastypes, ma_strcnt);
1099 				goto bad;
1100 			}
1101 
1102 			pstypes = smb_create_strcnt(p_strcnt);
1103 			if (pstypes == NULL) {
1104 				smb_free_strcnt(mastypes, ma_strcnt);
1105 				goto bad;
1106 			}
1107 
1108 			pstypes->type = SMB_TYPE_PROCESSOR;
1109 			smb_strcnt(shp, pstypes);
1110 			for (j = 0; j < pstypes->count; j++) {
1111 				proc_id = pstypes->ids[j]->id;
1112 				if (proc_id == em.smbmae_comp) {
1113 					*chip_inst = pstypes->ids[j]->inst;
1114 					smb_free_strcnt(mastypes, ma_strcnt);
1115 					smb_free_strcnt(pstypes, p_strcnt);
1116 					return (0);
1117 				}
1118 			}
1119 		}
1120 	}
1121 	smb_free_strcnt(mastypes, ma_strcnt);
1122 	smb_free_strcnt(pstypes, p_strcnt);
1123 bad:
1124 	/* revert to legacy enumeration */
1125 	x86gentopo_legacy = 1;
1126 
1127 	return (-1);
1128 }
1129