xref: /illumos-gate/usr/src/cmd/picl/plugins/common/memcfg/piclmemcfg_comm.c (revision e9610e3e86cbfb5fd6797a438c65b493250b4219)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
522f288dbSMichael Bergknoff  * Common Development and Distribution License (the "License").
622f288dbSMichael Bergknoff  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2222f288dbSMichael Bergknoff  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24720c4860SJohn Levon  *
25720c4860SJohn Levon  * Copyright (c) 2018, Joyent, Inc.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * This plugin creates memory configuration nodes and properties in the
307c478bd9Sstevel@tonic-gate  * PICL tree for Cheetah platforms.
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  * Subtree of memory-controller in the physical aspect.
337c478bd9Sstevel@tonic-gate  * memory-controller --- memory-module-group --- memory-module
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * Subtree of memory in the logical aspect.
367c478bd9Sstevel@tonic-gate  * memory --- memory-segment --- memory-bank
377c478bd9Sstevel@tonic-gate  * Add property _memory-module-group_ at memory-segment referring to the
387c478bd9Sstevel@tonic-gate  * memory-module-group if InterleaveFactor is one, or at memory-bank
397c478bd9Sstevel@tonic-gate  * if InterleaveFactor is greater than one.
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  * Undo strategy:
427c478bd9Sstevel@tonic-gate  * Create all nodes and properties, or none if it fails in physical and
437c478bd9Sstevel@tonic-gate  * logical memory tree respectively. It keeps on creating logic
447c478bd9Sstevel@tonic-gate  * memory tree although it falis on physical logic tree, but no link to
457c478bd9Sstevel@tonic-gate  * memory module group.
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * NOTE:
487c478bd9Sstevel@tonic-gate  * It depends on PICL devtree plugin and currently
497c478bd9Sstevel@tonic-gate  * there is no refresh routine for DR.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate #include <stdio.h>
527c478bd9Sstevel@tonic-gate #include <stdlib.h>
537c478bd9Sstevel@tonic-gate #include <string.h>
547c478bd9Sstevel@tonic-gate #include <unistd.h>
557c478bd9Sstevel@tonic-gate #include <alloca.h>
567c478bd9Sstevel@tonic-gate #include <syslog.h>
577c478bd9Sstevel@tonic-gate #include <string.h>
587c478bd9Sstevel@tonic-gate #include <libintl.h>
597c478bd9Sstevel@tonic-gate #include <picl.h>
607c478bd9Sstevel@tonic-gate #include <picltree.h>
617c478bd9Sstevel@tonic-gate #include <fcntl.h>
627c478bd9Sstevel@tonic-gate #include <errno.h>
637c478bd9Sstevel@tonic-gate #include <sys/types.h>
647c478bd9Sstevel@tonic-gate #include <dirent.h>
657c478bd9Sstevel@tonic-gate #include <sys/stat.h>
667c478bd9Sstevel@tonic-gate #include <mc.h>
677c478bd9Sstevel@tonic-gate #include <libnvpair.h>
687c478bd9Sstevel@tonic-gate #include <limits.h>
697c478bd9Sstevel@tonic-gate #include "piclmemcfg.h"
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * Plugin registration entry points
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate static	void	piclmemcfg_register(void);
757c478bd9Sstevel@tonic-gate static	void	piclmemcfg_init(void);
767c478bd9Sstevel@tonic-gate static	void	piclmemcfg_fini(void);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * PICL event handler
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate static void  piclmemcfg_evhandler(const char *ename, const void *earg,
827c478bd9Sstevel@tonic-gate 		size_t size, void *cookie);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #pragma	init(piclmemcfg_register)
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static picld_plugin_reg_t  my_reg_info = {
877c478bd9Sstevel@tonic-gate 	PICLD_PLUGIN_VERSION_1,
887c478bd9Sstevel@tonic-gate 	PICLD_PLUGIN_NON_CRITICAL,
897c478bd9Sstevel@tonic-gate 	"SUNW_piclmemcfg",
907c478bd9Sstevel@tonic-gate 	piclmemcfg_init,
917c478bd9Sstevel@tonic-gate 	piclmemcfg_fini
927c478bd9Sstevel@tonic-gate };
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * Log message texts
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate #define	EM_INIT_FAILED		gettext("SUNW_piclmemcfg init failed!\n")
987c478bd9Sstevel@tonic-gate #define	EM_PHYSIC_MEM_TREE_FAILED	\
997c478bd9Sstevel@tonic-gate 	gettext("SUNW_piclmemcfg physical memory tree failed!\n")
1007c478bd9Sstevel@tonic-gate #define	EM_LOGIC_MEM_TREE_FAILED		\
1017c478bd9Sstevel@tonic-gate 	gettext("SUNW_piclmemcfg logical memory tree failed!\n")
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate #define	EM_INIT_MC_FAILED	\
1047c478bd9Sstevel@tonic-gate 	gettext("SUNW_piclmemcfg init mc failed!\n")
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * Global variables for Memory Controllers
1087c478bd9Sstevel@tonic-gate  */
1097c478bd9Sstevel@tonic-gate #define	MC_DIR	"/dev/mc/"
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static int	nsegments;	/* The number of memory segments */
1127c478bd9Sstevel@tonic-gate static int	nbanks;		/* The max. number of banks per segment */
1137c478bd9Sstevel@tonic-gate static int	ndevgrps;	/* The max. number of device groups per mc */
1147c478bd9Sstevel@tonic-gate static int	ndevs;		/* The max. number of devices per dev group */
1157c478bd9Sstevel@tonic-gate static int	transfersize;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate static picl_nodehdl_t	*msegh_info;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * Memory-module-group node handle list, a singal linking list, where
1217c478bd9Sstevel@tonic-gate  * memory module group id is the key to match.
1227c478bd9Sstevel@tonic-gate  *
1237c478bd9Sstevel@tonic-gate  * It is allocated and added to the head of list, and freed as well.
1247c478bd9Sstevel@tonic-gate  * The mmgh field is cleared if failure is encountered in the physical
1257c478bd9Sstevel@tonic-gate  * memory tree.
1267c478bd9Sstevel@tonic-gate  *
1277c478bd9Sstevel@tonic-gate  * This list is accessed in the logical memory tree, and allocated memory
1287c478bd9Sstevel@tonic-gate  * is released at the end of plugin.
1297c478bd9Sstevel@tonic-gate  */
1307c478bd9Sstevel@tonic-gate typedef struct memmodgrp_info {
1317c478bd9Sstevel@tonic-gate 	int			mmgid;
1327c478bd9Sstevel@tonic-gate 	struct memmodgrp_info	*next;
1337c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		mmgh;
1347c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		mch;
1357c478bd9Sstevel@tonic-gate } mmodgrp_info_t;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate static	mmodgrp_info_t		*head2mmodgrp;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate  * Release the allocated memory of mmodgrp_info
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate static void
free_allocated_mem(void)1437c478bd9Sstevel@tonic-gate free_allocated_mem(void)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	mmodgrp_info_t		*mmghdl, *currmmghdl;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	mmghdl = head2mmodgrp;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	while (mmghdl) {
1507c478bd9Sstevel@tonic-gate 		currmmghdl = mmghdl;
1517c478bd9Sstevel@tonic-gate 		mmghdl = mmghdl->next;
1527c478bd9Sstevel@tonic-gate 		free(currmmghdl);
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	head2mmodgrp = NULL;
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate  * Delete nodes whose MC is gone at mmodgrp_info
1607c478bd9Sstevel@tonic-gate  */
1617c478bd9Sstevel@tonic-gate static void
del_plugout_mmodgrp(picl_nodehdl_t mch)1627c478bd9Sstevel@tonic-gate del_plugout_mmodgrp(picl_nodehdl_t mch)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	mmodgrp_info_t		*mmghdl, *prevmmghdl, *nextmmghdl;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	for (mmghdl = head2mmodgrp, prevmmghdl = NULL; mmghdl != NULL;
1677c478bd9Sstevel@tonic-gate 	    mmghdl = nextmmghdl) {
1687c478bd9Sstevel@tonic-gate 		nextmmghdl = mmghdl->next;
1697c478bd9Sstevel@tonic-gate 		if (mmghdl->mch == mch) {
1707c478bd9Sstevel@tonic-gate 			if (prevmmghdl == NULL)
1717c478bd9Sstevel@tonic-gate 				/* we are at the head */
1727c478bd9Sstevel@tonic-gate 				head2mmodgrp = nextmmghdl;
1737c478bd9Sstevel@tonic-gate 			else
1747c478bd9Sstevel@tonic-gate 				prevmmghdl->next = nextmmghdl;
1757c478bd9Sstevel@tonic-gate 			free(mmghdl);
1767c478bd9Sstevel@tonic-gate 		} else
1777c478bd9Sstevel@tonic-gate 			prevmmghdl = mmghdl;
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * Search the memory module group node in the mmodgrp_info by global id.
1837c478bd9Sstevel@tonic-gate  * The matched memory-module-group node handle will be assigned to
1847c478bd9Sstevel@tonic-gate  * the second parameter.
1857c478bd9Sstevel@tonic-gate  */
1867c478bd9Sstevel@tonic-gate static int
find_mem_mod_grp_hdl(int id,picl_nodehdl_t * mmodgrph)1877c478bd9Sstevel@tonic-gate find_mem_mod_grp_hdl(int id, picl_nodehdl_t *mmodgrph)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	mmodgrp_info_t		*mmghdl;
1907c478bd9Sstevel@tonic-gate 	int			err = PICL_FAILURE;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	mmghdl = head2mmodgrp;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	while (mmghdl) {
1957c478bd9Sstevel@tonic-gate 		if ((mmghdl->mmgh) && (mmghdl->mmgid == id)) {
1967c478bd9Sstevel@tonic-gate 			*mmodgrph = mmghdl->mmgh;
1977c478bd9Sstevel@tonic-gate 			err = PICL_SUCCESS;
1987c478bd9Sstevel@tonic-gate 			break;
1997c478bd9Sstevel@tonic-gate 		}
2007c478bd9Sstevel@tonic-gate 		mmghdl = mmghdl->next;
2017c478bd9Sstevel@tonic-gate 	}
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	return (err);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * Delete nodes and properties created in the physical memory tree.
2087c478bd9Sstevel@tonic-gate  */
2097c478bd9Sstevel@tonic-gate static void
undo_phymem_tree(void)2107c478bd9Sstevel@tonic-gate undo_phymem_tree(void)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate 	mmodgrp_info_t		*mmghdl;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	mmghdl = head2mmodgrp;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	while (mmghdl) {
2177c478bd9Sstevel@tonic-gate 		/*
2187c478bd9Sstevel@tonic-gate 		 * Delete nodes and properties of memory-module-group(s)
2197c478bd9Sstevel@tonic-gate 		 */
220*e9610e3eSToomas Soome 		if (mmghdl->mmgh == 0)
2217c478bd9Sstevel@tonic-gate 			continue;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		(void) ptree_delete_node(mmghdl->mmgh);
2247c478bd9Sstevel@tonic-gate 		(void) ptree_destroy_node(mmghdl->mmgh);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 		/*
2277c478bd9Sstevel@tonic-gate 		 * Clear out the saved node handle of memory module group
2287c478bd9Sstevel@tonic-gate 		 * so that logic memory tree won't link to it.
2297c478bd9Sstevel@tonic-gate 		 */
230*e9610e3eSToomas Soome 		mmghdl->mch = mmghdl->mmgh = 0;
2317c478bd9Sstevel@tonic-gate 		mmghdl = mmghdl->next;
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate /*
2367c478bd9Sstevel@tonic-gate  * Create all memory-banks under the given memory-segment.
2377c478bd9Sstevel@tonic-gate  */
2387c478bd9Sstevel@tonic-gate static int
add_mem_banks(picl_nodehdl_t msegh,int fd,struct mc_segment * mcseg)2397c478bd9Sstevel@tonic-gate add_mem_banks(picl_nodehdl_t msegh, int fd, struct mc_segment *mcseg)
2407c478bd9Sstevel@tonic-gate {
2417c478bd9Sstevel@tonic-gate 	int			i;
2427c478bd9Sstevel@tonic-gate 	int			err = PICL_SUCCESS;
2437c478bd9Sstevel@tonic-gate 	static picl_nodehdl_t	mmodgrph;
2447c478bd9Sstevel@tonic-gate 	picl_prophdl_t		bankh;
2457c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
2467c478bd9Sstevel@tonic-gate 	struct mc_bank		mcbank;
2477c478bd9Sstevel@tonic-gate 	char			propname[PICL_CLASSNAMELEN_MAX];
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/*
2507c478bd9Sstevel@tonic-gate 	 * Get all bank information via ioctl
2517c478bd9Sstevel@tonic-gate 	 */
2527c478bd9Sstevel@tonic-gate 	for (i = 0; i < mcseg->nbanks; i++) {
2537c478bd9Sstevel@tonic-gate 		mcbank.id = mcseg->bankids[i].globalid;
2547c478bd9Sstevel@tonic-gate 		if (ioctl(fd, MCIOC_BANK, &mcbank) == -1)
2557c478bd9Sstevel@tonic-gate 			return (PICL_FAILURE);
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 		/*
2587c478bd9Sstevel@tonic-gate 		 * Create memory-bank node under memory-segment node
2597c478bd9Sstevel@tonic-gate 		 */
2607c478bd9Sstevel@tonic-gate 		err = ptree_create_and_add_node(msegh, PICL_NAME_MEMORY_BANK,
2617c478bd9Sstevel@tonic-gate 		    PICL_CLASS_MEMORY_BANK, &bankh);
2627c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
2637c478bd9Sstevel@tonic-gate 			break;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 		/*
2667c478bd9Sstevel@tonic-gate 		 * Add property, Size to memory-bank node
2677c478bd9Sstevel@tonic-gate 		 */
2687c478bd9Sstevel@tonic-gate 		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
2697c478bd9Sstevel@tonic-gate 		    PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (mcbank.size),
2707c478bd9Sstevel@tonic-gate 		    PICL_PROP_SIZE, NULL, NULL);
2717c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
2727c478bd9Sstevel@tonic-gate 			break;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		err = ptree_create_and_add_prop(bankh, &propinfo, &mcbank.size,
2757c478bd9Sstevel@tonic-gate 		    NULL);
2767c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
2777c478bd9Sstevel@tonic-gate 			break;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 		/*
2807c478bd9Sstevel@tonic-gate 		 * Add property, AddressMask to memory-bank node
2817c478bd9Sstevel@tonic-gate 		 */
2827c478bd9Sstevel@tonic-gate 		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
2837c478bd9Sstevel@tonic-gate 		    PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (mcbank.mask),
2847c478bd9Sstevel@tonic-gate 		    PICL_PROP_ADDRESSMASK, NULL, NULL);
2857c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
2867c478bd9Sstevel@tonic-gate 			break;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 		err = ptree_create_and_add_prop(bankh, &propinfo, &mcbank.mask,
2897c478bd9Sstevel@tonic-gate 		    NULL);
2907c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
2917c478bd9Sstevel@tonic-gate 			break;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 		/*
2947c478bd9Sstevel@tonic-gate 		 * Add property, AddressMatch to memory-bank node
2957c478bd9Sstevel@tonic-gate 		 */
2967c478bd9Sstevel@tonic-gate 		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
2977c478bd9Sstevel@tonic-gate 		    PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (mcbank.match),
2987c478bd9Sstevel@tonic-gate 		    PICL_PROP_ADDRESSMATCH, NULL, NULL);
2997c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
3007c478bd9Sstevel@tonic-gate 			break;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 		err = ptree_create_and_add_prop(bankh, &propinfo,
3037c478bd9Sstevel@tonic-gate 		    &mcbank.match, NULL);
3047c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
3057c478bd9Sstevel@tonic-gate 			break;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 		/*
3087c478bd9Sstevel@tonic-gate 		 * Add global id of bank to property, ID memory-bank node
3097c478bd9Sstevel@tonic-gate 		 */
3107c478bd9Sstevel@tonic-gate 		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
3117c478bd9Sstevel@tonic-gate 		    PICL_PTYPE_INT, PICL_READ, sizeof (mcbank.id), PICL_PROP_ID,
3127c478bd9Sstevel@tonic-gate 		    NULL, NULL);
3137c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
3147c478bd9Sstevel@tonic-gate 			break;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 		err = ptree_create_and_add_prop(bankh, &propinfo, &mcbank.id,
3177c478bd9Sstevel@tonic-gate 		    NULL);
3187c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
3197c478bd9Sstevel@tonic-gate 			break;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 		/*
3227c478bd9Sstevel@tonic-gate 		 * Add property, _memory-module-group_ to memory-bank node
3237c478bd9Sstevel@tonic-gate 		 */
3247c478bd9Sstevel@tonic-gate 		if ((find_mem_mod_grp_hdl(mcbank.devgrpid.globalid,
3257c478bd9Sstevel@tonic-gate 		    &mmodgrph)) != PICL_SUCCESS)
3267c478bd9Sstevel@tonic-gate 			continue;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 		/*
3297c478bd9Sstevel@tonic-gate 		 * The number of memory modules > 1 means there needs
3307c478bd9Sstevel@tonic-gate 		 * memory module group, and then refers to it. Otherwise,
3317c478bd9Sstevel@tonic-gate 		 * it refers to memory module node handle instead.
3327c478bd9Sstevel@tonic-gate 		 */
3337c478bd9Sstevel@tonic-gate 		(void) strlcpy(propname, (ndevs > 1 ?
3347c478bd9Sstevel@tonic-gate 		    PICL_REFPROP_MEMORY_MODULE_GROUP :
3357c478bd9Sstevel@tonic-gate 		    PICL_REFPROP_MEMORY_MODULE), PICL_CLASSNAMELEN_MAX);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
3387c478bd9Sstevel@tonic-gate 		    PICL_PTYPE_REFERENCE, PICL_READ, sizeof (picl_nodehdl_t),
3397c478bd9Sstevel@tonic-gate 		    propname, NULL, NULL);
3407c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
3417c478bd9Sstevel@tonic-gate 			break;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 		err = ptree_create_and_add_prop(bankh, &propinfo, &mmodgrph,
3447c478bd9Sstevel@tonic-gate 		    NULL);
3457c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
3467c478bd9Sstevel@tonic-gate 			break;
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate static void
undo_logical_tree(int nsegments)3527c478bd9Sstevel@tonic-gate undo_logical_tree(int nsegments)
3537c478bd9Sstevel@tonic-gate {
3547c478bd9Sstevel@tonic-gate 	int	i;
3557c478bd9Sstevel@tonic-gate 	/*
3567c478bd9Sstevel@tonic-gate 	 * Undo in the logical memory tree
3577c478bd9Sstevel@tonic-gate 	 */
3587c478bd9Sstevel@tonic-gate 	for (i = 0; i < nsegments; i++) {
3597c478bd9Sstevel@tonic-gate 		(void) ptree_delete_node(msegh_info[i]);
3607c478bd9Sstevel@tonic-gate 		(void) ptree_destroy_node(msegh_info[i]);
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate  * Create logical memory tree
3667c478bd9Sstevel@tonic-gate  * memory --- memory-segment --- memory-bank
3677c478bd9Sstevel@tonic-gate  * Get information via ioctl of memory control driver
3687c478bd9Sstevel@tonic-gate  */
3697c478bd9Sstevel@tonic-gate static int
create_logical_tree(picl_nodehdl_t memh,int fd)3707c478bd9Sstevel@tonic-gate create_logical_tree(picl_nodehdl_t memh, int fd)
3717c478bd9Sstevel@tonic-gate {
3727c478bd9Sstevel@tonic-gate 	int			i;
3737c478bd9Sstevel@tonic-gate 	int			err = PICL_SUCCESS;
3747c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		msegh;
3757c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
3767c478bd9Sstevel@tonic-gate 	struct mc_memory	*mcmem;
3777c478bd9Sstevel@tonic-gate 	struct mc_segment	*mcseg;
3787c478bd9Sstevel@tonic-gate 	picl_prophdl_t		proph;
3797c478bd9Sstevel@tonic-gate 	uint64_t		memsize = 0;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	/*
3827c478bd9Sstevel@tonic-gate 	 * allocate memory for mc_memory where nsegmentids are various
3837c478bd9Sstevel@tonic-gate 	 */
3847c478bd9Sstevel@tonic-gate 	if ((mcmem = alloca((nsegments - 1) * sizeof (mcmem->segmentids[0]) +
3857c478bd9Sstevel@tonic-gate 	    sizeof (*mcmem))) == NULL)
3867c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	mcmem->nsegments = nsegments;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	/*
3917c478bd9Sstevel@tonic-gate 	 * Get logical memory information
3927c478bd9Sstevel@tonic-gate 	 */
3937c478bd9Sstevel@tonic-gate 	if (ioctl(fd, MCIOC_MEM, mcmem) == -1)
3947c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	/*
3977c478bd9Sstevel@tonic-gate 	 * allocate memory for mc_segment where nbanks are various
3987c478bd9Sstevel@tonic-gate 	 */
3997c478bd9Sstevel@tonic-gate 	if ((mcseg = alloca((nbanks - 1) * sizeof (mcseg->bankids[0]) +
4007c478bd9Sstevel@tonic-gate 	    sizeof (*mcseg))) == NULL)
4017c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	/*
4047c478bd9Sstevel@tonic-gate 	 * Get all segments to create memory-segment nodes and
4057c478bd9Sstevel@tonic-gate 	 * add properties.
4067c478bd9Sstevel@tonic-gate 	 */
4077c478bd9Sstevel@tonic-gate 	for (i = 0; i < nsegments; i++) {
4087c478bd9Sstevel@tonic-gate 		mcseg->id = mcmem->segmentids[i].globalid;
4097c478bd9Sstevel@tonic-gate 		mcseg->nbanks = nbanks;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 		if (ioctl(fd, MCIOC_SEG, mcseg) == -1)
4127c478bd9Sstevel@tonic-gate 			break;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 		/*
4157c478bd9Sstevel@tonic-gate 		 * Create memory-segment node under memory node
4167c478bd9Sstevel@tonic-gate 		 */
4177c478bd9Sstevel@tonic-gate 		err = ptree_create_and_add_node(memh, PICL_NAME_MEMORY_SEGMENT,
4187c478bd9Sstevel@tonic-gate 		    PICL_CLASS_MEMORY_SEGMENT, &msegh);
4197c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
4207c478bd9Sstevel@tonic-gate 			break;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 		msegh_info[i] = msegh;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 		/*
4257c478bd9Sstevel@tonic-gate 		 * Add property, Size to memory-segment node
4267c478bd9Sstevel@tonic-gate 		 */
427720c4860SJohn Levon 		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
4287c478bd9Sstevel@tonic-gate 		    PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (mcseg->size),
429720c4860SJohn Levon 		    PICL_PROP_SIZE, NULL, NULL);
4307c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
4317c478bd9Sstevel@tonic-gate 			break;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 		memsize += mcseg->size;
4347c478bd9Sstevel@tonic-gate 		err = ptree_create_and_add_prop(msegh, &propinfo, &mcseg->size,
4357c478bd9Sstevel@tonic-gate 		    NULL);
4367c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
4377c478bd9Sstevel@tonic-gate 			break;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 		/*
4407c478bd9Sstevel@tonic-gate 		 * Add property, BaseAddress to memory-segment node
4417c478bd9Sstevel@tonic-gate 		 */
4427c478bd9Sstevel@tonic-gate 		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
4437c478bd9Sstevel@tonic-gate 		    PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (mcseg->base),
4447c478bd9Sstevel@tonic-gate 		    PICL_PROP_BASEADDRESS, NULL, NULL);
4457c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
4467c478bd9Sstevel@tonic-gate 			break;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 		err = ptree_create_and_add_prop(msegh, &propinfo, &mcseg->base,
4497c478bd9Sstevel@tonic-gate 		    NULL);
4507c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
4517c478bd9Sstevel@tonic-gate 			break;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
4547c478bd9Sstevel@tonic-gate 		    PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (mcseg->ifactor),
4557c478bd9Sstevel@tonic-gate 		    PICL_PROP_INTERLEAVE_FACTOR, NULL, NULL);
4567c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
4577c478bd9Sstevel@tonic-gate 			break;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 		err = ptree_create_and_add_prop(msegh, &propinfo,
4607c478bd9Sstevel@tonic-gate 		    &mcseg->ifactor, NULL);
4617c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
4627c478bd9Sstevel@tonic-gate 			break;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 		err = add_mem_banks(msegh, fd, mcseg);
4657c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
4667c478bd9Sstevel@tonic-gate 			break;
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
4707c478bd9Sstevel@tonic-gate 		undo_logical_tree(nsegments);
4717c478bd9Sstevel@tonic-gate 		return (err);
4727c478bd9Sstevel@tonic-gate 	}
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	err = ptree_get_prop_by_name(memh, PICL_PROP_SIZE, &proph);
4757c478bd9Sstevel@tonic-gate 	if (err == PICL_SUCCESS) {	/* update the value */
4767c478bd9Sstevel@tonic-gate 		err = ptree_update_propval(proph, &memsize, sizeof (memsize));
4777c478bd9Sstevel@tonic-gate 		return (err);
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	/*
4817c478bd9Sstevel@tonic-gate 	 * Add the size property
4827c478bd9Sstevel@tonic-gate 	 */
4837c478bd9Sstevel@tonic-gate 	(void) ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
4847c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (memsize),
4857c478bd9Sstevel@tonic-gate 	    PICL_PROP_SIZE, NULL, NULL);
4867c478bd9Sstevel@tonic-gate 	err = ptree_create_and_add_prop(memh, &propinfo, &memsize, NULL);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	return (err);
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate /*
4927c478bd9Sstevel@tonic-gate  * Add memory-module nodes and properties at each enabled memory-module-group.
4937c478bd9Sstevel@tonic-gate  * The formula of unique id is (id of the given memory module group *
4947c478bd9Sstevel@tonic-gate  * max number of memory modules per memory module group) + index
4957c478bd9Sstevel@tonic-gate  * of memory modules in this memory module group
4967c478bd9Sstevel@tonic-gate  */
4977c478bd9Sstevel@tonic-gate static int
add_mem_modules(picl_nodehdl_t mmodgrph,struct mc_devgrp * mcdevgrp)4987c478bd9Sstevel@tonic-gate add_mem_modules(picl_nodehdl_t mmodgrph, struct mc_devgrp *mcdevgrp)
4997c478bd9Sstevel@tonic-gate {
5007c478bd9Sstevel@tonic-gate 	uint64_t		size;
5017c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		dimmh;
5027c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
5037c478bd9Sstevel@tonic-gate 	int			i;
5047c478bd9Sstevel@tonic-gate 	int			err = PICL_SUCCESS;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	size = mcdevgrp->size / mcdevgrp->ndevices;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	/*
5097c478bd9Sstevel@tonic-gate 	 * Get all memory-modules of the given memory-module-group
5107c478bd9Sstevel@tonic-gate 	 */
5117c478bd9Sstevel@tonic-gate 	for (i = 0; i < mcdevgrp->ndevices; i++) {
5127c478bd9Sstevel@tonic-gate 		/*
5137c478bd9Sstevel@tonic-gate 		 * Create memory-module node under memory-module-group
5147c478bd9Sstevel@tonic-gate 		 */
5157c478bd9Sstevel@tonic-gate 		err = ptree_create_and_add_node(mmodgrph,
5167c478bd9Sstevel@tonic-gate 		    PICL_NAME_MEMORY_MODULE, PICL_CLASS_MEMORY_MODULE, &dimmh);
5177c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
5187c478bd9Sstevel@tonic-gate 			break;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 		/*
5217c478bd9Sstevel@tonic-gate 		 * Add property, Size to memory-module-group node
5227c478bd9Sstevel@tonic-gate 		 */
5237c478bd9Sstevel@tonic-gate 		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
5247c478bd9Sstevel@tonic-gate 		    PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (size),
5257c478bd9Sstevel@tonic-gate 		    PICL_PROP_SIZE, NULL, NULL);
5267c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
5277c478bd9Sstevel@tonic-gate 			break;
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 		err = ptree_create_and_add_prop(dimmh, &propinfo, &size, NULL);
5307c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
5317c478bd9Sstevel@tonic-gate 			break;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 		/*
5347c478bd9Sstevel@tonic-gate 		 * Add property, ID to memory-module-group node
5357c478bd9Sstevel@tonic-gate 		 */
5367c478bd9Sstevel@tonic-gate 		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
5377c478bd9Sstevel@tonic-gate 		    PICL_PTYPE_INT, PICL_READ, sizeof (i), PICL_PROP_ID,
5387c478bd9Sstevel@tonic-gate 		    NULL, NULL);
5397c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
5407c478bd9Sstevel@tonic-gate 			break;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 		err = ptree_create_and_add_prop(dimmh, &propinfo, &i,
5437c478bd9Sstevel@tonic-gate 		    NULL);
5447c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
5457c478bd9Sstevel@tonic-gate 			break;
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 	return (err);
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate /*
5517c478bd9Sstevel@tonic-gate  * Create the subtree at every enabled Memory Controller where size of
5527c478bd9Sstevel@tonic-gate  * memory module group is greater than zero.
5537c478bd9Sstevel@tonic-gate  * Get information via ioctl of memory control driver
5547c478bd9Sstevel@tonic-gate  */
5557c478bd9Sstevel@tonic-gate static int
create_physical_tree(picl_nodehdl_t mch,void * args)5567c478bd9Sstevel@tonic-gate create_physical_tree(picl_nodehdl_t mch, void *args)
5577c478bd9Sstevel@tonic-gate {
5587c478bd9Sstevel@tonic-gate 	int			i, portid;
5597c478bd9Sstevel@tonic-gate 	int			err = PICL_SUCCESS;
5607c478bd9Sstevel@tonic-gate 	mmodgrp_info_t		*mmghdl;
5617c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		mmodgrph;
5627c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
5637c478bd9Sstevel@tonic-gate 	struct mc_control	*mccontrol;
5647c478bd9Sstevel@tonic-gate 	struct mc_devgrp	mcdevgrp;
5657c478bd9Sstevel@tonic-gate 	int			fd;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	fd = (int)args;
5687c478bd9Sstevel@tonic-gate 	/*
5697c478bd9Sstevel@tonic-gate 	 * Get portid of memory-controller as the key to get its
5707c478bd9Sstevel@tonic-gate 	 * configuration via ioctl.
5717c478bd9Sstevel@tonic-gate 	 */
5727c478bd9Sstevel@tonic-gate 	err = ptree_get_propval_by_name(mch, OBP_PROP_PORTID, &portid,
5737c478bd9Sstevel@tonic-gate 	    sizeof (portid));
5747c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
5757c478bd9Sstevel@tonic-gate 		return (err);
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	if ((mccontrol = alloca((ndevgrps - 1) *
5787c478bd9Sstevel@tonic-gate 	    sizeof (mccontrol->devgrpids[0]) + sizeof (*mccontrol))) == NULL)
5797c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	mccontrol->id = portid;
5827c478bd9Sstevel@tonic-gate 	mccontrol->ndevgrps = ndevgrps;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	if (ioctl(fd, MCIOC_CONTROL, mccontrol) == -1) {
5857c478bd9Sstevel@tonic-gate 		if (errno == EINVAL)
5867c478bd9Sstevel@tonic-gate 			return (PICL_WALK_CONTINUE);
5877c478bd9Sstevel@tonic-gate 		else
5887c478bd9Sstevel@tonic-gate 			return (PICL_FAILURE);
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	/*
5927c478bd9Sstevel@tonic-gate 	 * If returned ndevgrps is zero, Memory Controller is disable, and
5937c478bd9Sstevel@tonic-gate 	 * skip it.
5947c478bd9Sstevel@tonic-gate 	 */
5957c478bd9Sstevel@tonic-gate 	if (mccontrol->ndevgrps == 0)
5967c478bd9Sstevel@tonic-gate 		return (PICL_WALK_CONTINUE);
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	/*
5997c478bd9Sstevel@tonic-gate 	 * Get all memory module groups of the given memory controller.
6007c478bd9Sstevel@tonic-gate 	 */
6017c478bd9Sstevel@tonic-gate 	for (i = 0; i < mccontrol->ndevgrps; i++) {
6027c478bd9Sstevel@tonic-gate 		int	mmglocalid = mccontrol->devgrpids[i].localid;
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 		mcdevgrp.id = mccontrol->devgrpids[i].globalid;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 		if (ioctl(fd, MCIOC_DEVGRP, &mcdevgrp) == -1)
6077c478bd9Sstevel@tonic-gate 			return (PICL_FAILURE);
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 		/*
6107c478bd9Sstevel@tonic-gate 		 * Node doesn't need to be created if size is 0, i.e.
6117c478bd9Sstevel@tonic-gate 		 * there is no memory dimm at slot.
6127c478bd9Sstevel@tonic-gate 		 */
6137c478bd9Sstevel@tonic-gate 		if (mcdevgrp.size == 0)
6147c478bd9Sstevel@tonic-gate 			continue;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 		/*
6177c478bd9Sstevel@tonic-gate 		 * Create memory-module-group node under memory-controller
6187c478bd9Sstevel@tonic-gate 		 */
6197c478bd9Sstevel@tonic-gate 		err = ptree_create_and_add_node(mch, PICL_NAME_MEM_MOD_GROUP,
6207c478bd9Sstevel@tonic-gate 		    PICL_CLASS_MEMORY_MODULE_GROUP, &mmodgrph);
6217c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
6227c478bd9Sstevel@tonic-gate 			break;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 		/*
6257c478bd9Sstevel@tonic-gate 		 * Allocate space for mmodgrp_info to save the information
6267c478bd9Sstevel@tonic-gate 		 * so that it is easier to do the undo and setup of the
6277c478bd9Sstevel@tonic-gate 		 * reference property in logical memory tree.
6287c478bd9Sstevel@tonic-gate 		 */
6297c478bd9Sstevel@tonic-gate 		if ((mmghdl = malloc(sizeof (*mmghdl))) == NULL)
6307c478bd9Sstevel@tonic-gate 			return (PICL_FAILURE);
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 		/*
6337c478bd9Sstevel@tonic-gate 		 * Save the information and add it to the beginnong of list.
6347c478bd9Sstevel@tonic-gate 		 */
6357c478bd9Sstevel@tonic-gate 		mmghdl->mmgid = mcdevgrp.id;
6367c478bd9Sstevel@tonic-gate 		mmghdl->mmgh = mmodgrph;
6377c478bd9Sstevel@tonic-gate 		mmghdl->mch = mch;
6387c478bd9Sstevel@tonic-gate 		mmghdl->next = head2mmodgrp;
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 		head2mmodgrp = mmghdl;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 		/*
6437c478bd9Sstevel@tonic-gate 		 * Add property, Size to memory-module-group node
6447c478bd9Sstevel@tonic-gate 		 */
6457c478bd9Sstevel@tonic-gate 		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
6467c478bd9Sstevel@tonic-gate 		    PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (mcdevgrp.size),
6477c478bd9Sstevel@tonic-gate 		    PICL_PROP_SIZE, NULL, NULL);
6487c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
6497c478bd9Sstevel@tonic-gate 			break;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 		err = ptree_create_and_add_prop(mmodgrph, &propinfo,
6527c478bd9Sstevel@tonic-gate 		    &mcdevgrp.size, NULL);
6537c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
6547c478bd9Sstevel@tonic-gate 			break;
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 		/*
6577c478bd9Sstevel@tonic-gate 		 * Add property, ID to memory-module-group node
6587c478bd9Sstevel@tonic-gate 		 */
6597c478bd9Sstevel@tonic-gate 		err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
6607c478bd9Sstevel@tonic-gate 		    PICL_PTYPE_INT, PICL_READ, sizeof (mmglocalid),
6617c478bd9Sstevel@tonic-gate 		    PICL_PROP_ID, NULL, NULL);
6627c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
6637c478bd9Sstevel@tonic-gate 			break;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 		err = ptree_create_and_add_prop(mmodgrph, &propinfo,
6667c478bd9Sstevel@tonic-gate 		    &mmglocalid, NULL);
6677c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
6687c478bd9Sstevel@tonic-gate 			break;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 		/*
6717c478bd9Sstevel@tonic-gate 		 * Create all memory-module nodes and properties.
6727c478bd9Sstevel@tonic-gate 		 */
6737c478bd9Sstevel@tonic-gate 		err = add_mem_modules(mmodgrph, &mcdevgrp);
6747c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
6757c478bd9Sstevel@tonic-gate 			break;
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	if (err == PICL_SUCCESS)
6797c478bd9Sstevel@tonic-gate 		return (PICL_WALK_CONTINUE);
6807c478bd9Sstevel@tonic-gate 	return (err);
6817c478bd9Sstevel@tonic-gate }
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate /*
6847c478bd9Sstevel@tonic-gate  * Create physical memory tree
6857c478bd9Sstevel@tonic-gate  * memory-controller --- memory-module-group --- memory-module
6867c478bd9Sstevel@tonic-gate  *
6877c478bd9Sstevel@tonic-gate  * It searches all memory-controller nodes in the whole devtree.
6887c478bd9Sstevel@tonic-gate  * It returns failure if encountering error in physical tree.
6897c478bd9Sstevel@tonic-gate  */
6907c478bd9Sstevel@tonic-gate static int
find_mc_create_tree(picl_nodehdl_t rooth,int fd)6917c478bd9Sstevel@tonic-gate find_mc_create_tree(picl_nodehdl_t rooth, int fd)
6927c478bd9Sstevel@tonic-gate {
6937c478bd9Sstevel@tonic-gate 	int		err;
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	err = ptree_walk_tree_by_class(rooth, PICL_CLASS_MEMORY_CONTROLLER,
6967c478bd9Sstevel@tonic-gate 	    (void *)fd, create_physical_tree);
6977c478bd9Sstevel@tonic-gate 	return (err);
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate static int
init_mc(void)7017c478bd9Sstevel@tonic-gate init_mc(void)
7027c478bd9Sstevel@tonic-gate {
7037c478bd9Sstevel@tonic-gate 	struct mc_memconf	mcmemconf;
7047c478bd9Sstevel@tonic-gate 	int			fd;
7057c478bd9Sstevel@tonic-gate 	DIR			*dirp;
7064bc0a2efScasper 	struct dirent		*retp;
7077c478bd9Sstevel@tonic-gate 	char			path[PATH_MAX];
7087c478bd9Sstevel@tonic-gate 	int			found = 0;
70922f288dbSMichael Bergknoff 	int			valid_entry = 0;
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	/* open the directory */
7127c478bd9Sstevel@tonic-gate 	if ((dirp = opendir(MC_DIR)) == NULL) {
7137c478bd9Sstevel@tonic-gate 		/*
7147c478bd9Sstevel@tonic-gate 		 * As not all platforms have mc drivers that create the
7157c478bd9Sstevel@tonic-gate 		 * /dev/mc directory, print a message only if there is
7167c478bd9Sstevel@tonic-gate 		 * an entry found on which the open failed.
7177c478bd9Sstevel@tonic-gate 		 */
7187c478bd9Sstevel@tonic-gate 		if (errno != ENOENT)
7197c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, EM_INIT_MC_FAILED);
7207c478bd9Sstevel@tonic-gate 		return (-1);
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	/* start searching this directory */
7244bc0a2efScasper 	while ((retp = readdir(dirp)) != NULL) {
7257c478bd9Sstevel@tonic-gate 		/* skip . .. etc... */
7267c478bd9Sstevel@tonic-gate 		if (strcmp(retp->d_name, ".") == 0 ||
7277c478bd9Sstevel@tonic-gate 		    strcmp(retp->d_name, "..") == 0)
7287c478bd9Sstevel@tonic-gate 			continue;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 		(void) strcpy(path, MC_DIR);
7317c478bd9Sstevel@tonic-gate 		(void) strcat(path, retp->d_name);
7327c478bd9Sstevel@tonic-gate 		/* open the memory controller driver */
7337c478bd9Sstevel@tonic-gate 		if ((fd = open(path, O_RDONLY, 0)) != -1) {
7347c478bd9Sstevel@tonic-gate 			found = 1;
7357c478bd9Sstevel@tonic-gate 			break;
7367c478bd9Sstevel@tonic-gate 		}
73722f288dbSMichael Bergknoff 		if (errno != ENOENT)
73822f288dbSMichael Bergknoff 			valid_entry = 1;
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	if (!found) {
74322f288dbSMichael Bergknoff 		if (valid_entry)
7447c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, EM_INIT_MC_FAILED);
7457c478bd9Sstevel@tonic-gate 		return (-1);
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	/*
7497c478bd9Sstevel@tonic-gate 	 * Initialize some global variables via ioctl
7507c478bd9Sstevel@tonic-gate 	 */
7517c478bd9Sstevel@tonic-gate 	if (ioctl(fd, MCIOC_MEMCONF, &mcmemconf) == -1) {
7527c478bd9Sstevel@tonic-gate 		(void) close(fd);
7537c478bd9Sstevel@tonic-gate 		return (-1);
7547c478bd9Sstevel@tonic-gate 	}
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	nsegments = mcmemconf.nsegments;
7577c478bd9Sstevel@tonic-gate 	nbanks = mcmemconf.nbanks;
7587c478bd9Sstevel@tonic-gate 	ndevgrps = mcmemconf.ndevgrps;
7597c478bd9Sstevel@tonic-gate 	ndevs = mcmemconf.ndevs;
7607c478bd9Sstevel@tonic-gate 	transfersize = mcmemconf.xfer_size;
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	return (fd);
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate /*
7667c478bd9Sstevel@tonic-gate  * executed as part of .init when the plugin is dlopen()ed
7677c478bd9Sstevel@tonic-gate  */
7687c478bd9Sstevel@tonic-gate void
piclmemcfg_register(void)7697c478bd9Sstevel@tonic-gate piclmemcfg_register(void)
7707c478bd9Sstevel@tonic-gate {
7717c478bd9Sstevel@tonic-gate 	(void) picld_plugin_register(&my_reg_info);
7727c478bd9Sstevel@tonic-gate }
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate /*
7757c478bd9Sstevel@tonic-gate  * init entry point of the plugin
7767c478bd9Sstevel@tonic-gate  * Creates the PICL nodes and properties in the physical and logical aspects.
7777c478bd9Sstevel@tonic-gate  */
7787c478bd9Sstevel@tonic-gate void
piclmemcfg_init(void)7797c478bd9Sstevel@tonic-gate piclmemcfg_init(void)
7807c478bd9Sstevel@tonic-gate {
7817c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		plfh;
7827c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		memh;
7837c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
7847c478bd9Sstevel@tonic-gate 	int			fd, err;
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	/*
7877c478bd9Sstevel@tonic-gate 	 * Initialize the header pointer of mmodgrp_info list
7887c478bd9Sstevel@tonic-gate 	 */
7897c478bd9Sstevel@tonic-gate 	head2mmodgrp = NULL;
7907c478bd9Sstevel@tonic-gate 	msegh_info = NULL;
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	if ((fd = init_mc()) < 0)
7937c478bd9Sstevel@tonic-gate 		return;
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	/*
7967c478bd9Sstevel@tonic-gate 	 * allocate memory to save memory-segment node handles. Thus,
7977c478bd9Sstevel@tonic-gate 	 * it is easier to delete them if it fails.
7987c478bd9Sstevel@tonic-gate 	 */
7997c478bd9Sstevel@tonic-gate 	if ((msegh_info = malloc(nsegments * sizeof (picl_nodehdl_t))) ==
8007c478bd9Sstevel@tonic-gate 	    NULL) {
8017c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, EM_INIT_FAILED);
8027c478bd9Sstevel@tonic-gate 		(void) close(fd);
8037c478bd9Sstevel@tonic-gate 		return;
8047c478bd9Sstevel@tonic-gate 	}
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	/*
8077c478bd9Sstevel@tonic-gate 	 * find platform node
8087c478bd9Sstevel@tonic-gate 	 */
8097c478bd9Sstevel@tonic-gate 	if ((ptree_get_node_by_path(PLATFORM_PATH, &plfh)) != PICL_SUCCESS) {
8107c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, EM_INIT_FAILED);
8117c478bd9Sstevel@tonic-gate 		(void) close(fd);
8127c478bd9Sstevel@tonic-gate 		return;
8137c478bd9Sstevel@tonic-gate 	}
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	/*
8167c478bd9Sstevel@tonic-gate 	 * Find the memory node
8177c478bd9Sstevel@tonic-gate 	 */
8187c478bd9Sstevel@tonic-gate 	if ((ptree_get_node_by_path(MEMORY_PATH, &memh)) != PICL_SUCCESS) {
8197c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, EM_INIT_FAILED);
8207c478bd9Sstevel@tonic-gate 		(void) close(fd);
8217c478bd9Sstevel@tonic-gate 		return;
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	/*
8257c478bd9Sstevel@tonic-gate 	 * Create subtree of memory-controller in the physical aspect.
8267c478bd9Sstevel@tonic-gate 	 * memory-controller --- memory-module-group --- memory-module
8277c478bd9Sstevel@tonic-gate 	 */
8287c478bd9Sstevel@tonic-gate 	err = find_mc_create_tree(plfh, fd);
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
8317c478bd9Sstevel@tonic-gate 		undo_phymem_tree();
8327c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, EM_PHYSIC_MEM_TREE_FAILED);
8337c478bd9Sstevel@tonic-gate 	}
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	/*
8367c478bd9Sstevel@tonic-gate 	 * Add property, TransferSize to memory node
8377c478bd9Sstevel@tonic-gate 	 */
8387c478bd9Sstevel@tonic-gate 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
8397c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_UNSIGNED_INT, PICL_READ, sizeof (transfersize),
8407c478bd9Sstevel@tonic-gate 	    PICL_PROP_TRANSFER_SIZE, NULL, NULL);
8417c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
8427c478bd9Sstevel@tonic-gate 		(void) close(fd);
8437c478bd9Sstevel@tonic-gate 		return;
8447c478bd9Sstevel@tonic-gate 	}
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	err = ptree_create_and_add_prop(memh, &propinfo,
8477c478bd9Sstevel@tonic-gate 	    &transfersize, NULL);
8487c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
8497c478bd9Sstevel@tonic-gate 		(void) close(fd);
8507c478bd9Sstevel@tonic-gate 		return;
8517c478bd9Sstevel@tonic-gate 	}
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	/*
8547c478bd9Sstevel@tonic-gate 	 * Create subtree of memory in the logical aspect.
8557c478bd9Sstevel@tonic-gate 	 * memory --- memory-segment --- memory-bank
8567c478bd9Sstevel@tonic-gate 	 */
8577c478bd9Sstevel@tonic-gate 	if ((create_logical_tree(memh, fd)) != PICL_SUCCESS) {
8587c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, EM_LOGIC_MEM_TREE_FAILED);
8597c478bd9Sstevel@tonic-gate 		undo_logical_tree(nsegments);
8607c478bd9Sstevel@tonic-gate 	}
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	(void) close(fd);
8637c478bd9Sstevel@tonic-gate 	(void) ptree_register_handler(PICLEVENT_MC_ADDED,
8647c478bd9Sstevel@tonic-gate 	    piclmemcfg_evhandler, NULL);
8657c478bd9Sstevel@tonic-gate 	(void) ptree_register_handler(PICLEVENT_MC_REMOVED,
8667c478bd9Sstevel@tonic-gate 	    piclmemcfg_evhandler, NULL);
8677c478bd9Sstevel@tonic-gate }
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate /*
8707c478bd9Sstevel@tonic-gate  * fini entry point of the plugin
8717c478bd9Sstevel@tonic-gate  */
8727c478bd9Sstevel@tonic-gate void
piclmemcfg_fini(void)8737c478bd9Sstevel@tonic-gate piclmemcfg_fini(void)
8747c478bd9Sstevel@tonic-gate {
8757c478bd9Sstevel@tonic-gate 	(void) ptree_unregister_handler(PICLEVENT_MC_ADDED,
8767c478bd9Sstevel@tonic-gate 	    piclmemcfg_evhandler, NULL);
8777c478bd9Sstevel@tonic-gate 	(void) ptree_unregister_handler(PICLEVENT_MC_REMOVED,
8787c478bd9Sstevel@tonic-gate 	    piclmemcfg_evhandler, NULL);
8797c478bd9Sstevel@tonic-gate 	/*
8807c478bd9Sstevel@tonic-gate 	 * Release all the allocated memory for global structures
8817c478bd9Sstevel@tonic-gate 	 */
8827c478bd9Sstevel@tonic-gate 	free_allocated_mem();
8837c478bd9Sstevel@tonic-gate 	if (msegh_info)
8847c478bd9Sstevel@tonic-gate 		free(msegh_info);
8857c478bd9Sstevel@tonic-gate }
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate /*
8887c478bd9Sstevel@tonic-gate  * Event handler of this plug-in
8897c478bd9Sstevel@tonic-gate  */
8907c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8917c478bd9Sstevel@tonic-gate static void
piclmemcfg_evhandler(const char * ename,const void * earg,size_t size,void * cookie)8927c478bd9Sstevel@tonic-gate piclmemcfg_evhandler(const char *ename, const void *earg, size_t size,
8937c478bd9Sstevel@tonic-gate     void *cookie)
8947c478bd9Sstevel@tonic-gate {
8957c478bd9Sstevel@tonic-gate 	int		err;
8967c478bd9Sstevel@tonic-gate 	int		fd;
8977c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	memh;
8987c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	nodeh;
8997c478bd9Sstevel@tonic-gate 	int		old_nsegs;
9007c478bd9Sstevel@tonic-gate 	nvlist_t	*nvlp;
9017c478bd9Sstevel@tonic-gate 
902*e9610e3eSToomas Soome 	memh = 0;
903*e9610e3eSToomas Soome 	if (nvlist_unpack((char *)earg, size, &nvlp, 0))
9047c478bd9Sstevel@tonic-gate 		return;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	if (nvlist_lookup_uint64(nvlp, PICLEVENTARG_NODEHANDLE, &nodeh)) {
9077c478bd9Sstevel@tonic-gate 		nvlist_free(nvlp);
9087c478bd9Sstevel@tonic-gate 		return;
9097c478bd9Sstevel@tonic-gate 	}
9107c478bd9Sstevel@tonic-gate 	nvlist_free(nvlp);
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	/*
9137c478bd9Sstevel@tonic-gate 	 * get the memory node
9147c478bd9Sstevel@tonic-gate 	 */
9157c478bd9Sstevel@tonic-gate 	err = ptree_get_node_by_path(MEMORY_PATH, &memh);
9167c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
9177c478bd9Sstevel@tonic-gate 		return;
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	/*
9207c478bd9Sstevel@tonic-gate 	 * nsegments won't be overwritten until init_mc succeeds
9217c478bd9Sstevel@tonic-gate 	 */
9227c478bd9Sstevel@tonic-gate 	old_nsegs = nsegments;
9237c478bd9Sstevel@tonic-gate 	if ((fd = init_mc()) < 0)
9247c478bd9Sstevel@tonic-gate 		return;
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	if (strcmp(ename, PICLEVENT_MC_ADDED) == 0)
9277c478bd9Sstevel@tonic-gate 		(void) create_physical_tree(nodeh, (void *)fd);
9287c478bd9Sstevel@tonic-gate 	else if (strcmp(ename, PICLEVENT_MC_REMOVED) == 0)
9297c478bd9Sstevel@tonic-gate 		/*
9307c478bd9Sstevel@tonic-gate 		 * Delete the entry at the list only since class at PICL is
9317c478bd9Sstevel@tonic-gate 		 * deleted in devtree plugin.
9327c478bd9Sstevel@tonic-gate 		 */
9337c478bd9Sstevel@tonic-gate 		(void) del_plugout_mmodgrp(nodeh);
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	(void) undo_logical_tree(old_nsegs);
9367c478bd9Sstevel@tonic-gate 	free(msegh_info);
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	/*
9397c478bd9Sstevel@tonic-gate 	 * allocate memory to save memory-segment node handles. Thus,
9407c478bd9Sstevel@tonic-gate 	 * it is easier to delete them if it fails.
9417c478bd9Sstevel@tonic-gate 	 */
9427c478bd9Sstevel@tonic-gate 	if ((msegh_info = malloc(nsegments * sizeof (picl_nodehdl_t))) ==
9437c478bd9Sstevel@tonic-gate 	    NULL) {
9447c478bd9Sstevel@tonic-gate 		(void) close(fd);
9457c478bd9Sstevel@tonic-gate 		return;
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	(void) create_logical_tree(memh, fd);
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	(void) close(fd);
9517c478bd9Sstevel@tonic-gate }
952