xref: /illumos-gate/usr/src/cmd/dcs/sparc/sun4u/ri_init.c (revision aab83bb8)
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
5c2623971Sscarter  * Common Development and Distribution License (the "License").
6c2623971Sscarter  * 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 /*
22fc0105deSJames Anderson  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Implementation of ri_init routine for obtaining mapping
287c478bd9Sstevel@tonic-gate  * of system board attachment points to physical devices and to
297c478bd9Sstevel@tonic-gate  * the Reconfiguration Coordination Manager (RCM) client usage
307c478bd9Sstevel@tonic-gate  * of these devices.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <stdlib.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <kstat.h>
367c478bd9Sstevel@tonic-gate #include <sys/param.h>
377c478bd9Sstevel@tonic-gate #include <sys/sbd_ioctl.h>
387c478bd9Sstevel@tonic-gate #include "rsrc_info_impl.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * Occupant types exported by cfgadm sbd plugin via
427c478bd9Sstevel@tonic-gate  * config_admin(3CFGADM).
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate #define	SBD_CM_CPU	"cpu"
457c478bd9Sstevel@tonic-gate #define	SBD_CM_MEM	"memory"
467c478bd9Sstevel@tonic-gate #define	SBD_CM_IO	"io"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * RCM abstract resource names.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate #define	RCM_MEM_ALL	"SUNW_memory"
527c478bd9Sstevel@tonic-gate #define	RCM_CPU_ALL	"SUNW_cpu"
537c478bd9Sstevel@tonic-gate #define	RCM_CPU		RCM_CPU_ALL"/cpu"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	KBYTE		1024
567c478bd9Sstevel@tonic-gate #define	MBYTE		1048576
577c478bd9Sstevel@tonic-gate #define	USAGE_ALLOC_SIZE	128
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * define to allow io_cm_info to return NODE is NULL to ri_init,
617c478bd9Sstevel@tonic-gate  * in order to skip over nodes w/unattached drivers
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate #define	RI_NODE_NIL	1
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * This code is CMP aware as it parses the
677c478bd9Sstevel@tonic-gate  * cfgadm info field for individual cpuids.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate #define	CPUID_SEP	","
707c478bd9Sstevel@tonic-gate #define	CPU_INFO_FMT	"cpuid=%s speed=%d ecache=%d"
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate typedef struct {
737c478bd9Sstevel@tonic-gate 	cfga_list_data_t *cfga_list_data;
747c478bd9Sstevel@tonic-gate 	int		nlist;
757c478bd9Sstevel@tonic-gate } apd_t;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate typedef struct {
787c478bd9Sstevel@tonic-gate 	long		pagesize;
797c478bd9Sstevel@tonic-gate 	long		syspages;
807c478bd9Sstevel@tonic-gate 	long		sysmb;
817c478bd9Sstevel@tonic-gate } mem_stat_t;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #define	ms_syspages	m_stat.syspages
847c478bd9Sstevel@tonic-gate #define	ms_pagesize	m_stat.pagesize
857c478bd9Sstevel@tonic-gate #define	ms_sysmb	m_stat.sysmb
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate typedef int32_t		cpuid_t;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate typedef struct {
907c478bd9Sstevel@tonic-gate 	int	cpuid_max;	/* maximum cpuid value */
91fc0105deSJames Anderson 	int	ecache_curr;	/* cached during tree walk */
927c478bd9Sstevel@tonic-gate 	int	*ecache_sizes;	/* indexed by cpuid */
937c478bd9Sstevel@tonic-gate } ecache_info_t;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate typedef struct {
967c478bd9Sstevel@tonic-gate 	rcm_handle_t	*hdl;
977c478bd9Sstevel@tonic-gate 	rcm_info_t	*offline_query_info;
987c478bd9Sstevel@tonic-gate 	char		**rlist;
997c478bd9Sstevel@tonic-gate 	int		nrlist;
1007c478bd9Sstevel@tonic-gate 	cpuid_t		*cpus;
1017c478bd9Sstevel@tonic-gate 	int		ncpus;
1027c478bd9Sstevel@tonic-gate 	int		ndevs;
1037c478bd9Sstevel@tonic-gate 	uint_t		query_pages;
1047c478bd9Sstevel@tonic-gate 	mem_stat_t	m_stat;
1057c478bd9Sstevel@tonic-gate 	ecache_info_t	ecache_info;
1067c478bd9Sstevel@tonic-gate } rcmd_t;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate typedef struct {
1097c478bd9Sstevel@tonic-gate 	const char	*rsrc;
1107c478bd9Sstevel@tonic-gate 	const char	*info;
1117c478bd9Sstevel@tonic-gate } usage_t;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /* Lookup table entry for matching IO devices to RCM resource usage */
1147c478bd9Sstevel@tonic-gate typedef struct {
1157c478bd9Sstevel@tonic-gate 	int		index;		/* index into the table array */
1167c478bd9Sstevel@tonic-gate 	di_node_t	node;		/* associated devinfo node */
1177c478bd9Sstevel@tonic-gate 	char		*name;		/* device full path name */
1187c478bd9Sstevel@tonic-gate 	int		n_usage;
1197c478bd9Sstevel@tonic-gate 	usage_t		*usage;
1207c478bd9Sstevel@tonic-gate } lookup_entry_t;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate typedef struct {
1237c478bd9Sstevel@tonic-gate 	int		n_entries;
1247c478bd9Sstevel@tonic-gate 	int		n_slots;
1257c478bd9Sstevel@tonic-gate 	lookup_entry_t	*table;
1267c478bd9Sstevel@tonic-gate } lookup_table_t;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate typedef struct {
1297c478bd9Sstevel@tonic-gate 	int			err;
1307c478bd9Sstevel@tonic-gate 	di_node_t		node;
1317c478bd9Sstevel@tonic-gate 	char			*pathbuf;
1327c478bd9Sstevel@tonic-gate 	lookup_table_t		*table;
1337c478bd9Sstevel@tonic-gate 	di_devlink_handle_t	linkhd;
1347c478bd9Sstevel@tonic-gate } devinfo_arg_t;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate static int dyn_ap_ids(char *, cfga_list_data_t **, int *);
1377c478bd9Sstevel@tonic-gate static int rcm_init(rcmd_t *, apd_t [], int, int);
1387c478bd9Sstevel@tonic-gate static void rcm_fini(rcmd_t *);
1397c478bd9Sstevel@tonic-gate static int rcm_query_init(rcmd_t *, apd_t [], int);
1407c478bd9Sstevel@tonic-gate static int cap_request(ri_hdl_t *, rcmd_t *);
1417c478bd9Sstevel@tonic-gate static int syscpus(cpuid_t **, int *);
1427c478bd9Sstevel@tonic-gate static int cpu_cap_request(ri_hdl_t *, rcmd_t *);
1437c478bd9Sstevel@tonic-gate static int mem_cap_request(ri_hdl_t *, rcmd_t *);
1447c478bd9Sstevel@tonic-gate static int (*cm_rcm_qpass_func(cfga_type_t))(cfga_list_data_t *, rcmd_t *);
1457c478bd9Sstevel@tonic-gate static int cpu_rcm_qpass(cfga_list_data_t *, rcmd_t *);
1467c478bd9Sstevel@tonic-gate static int mem_rcm_qpass(cfga_list_data_t *, rcmd_t *);
1477c478bd9Sstevel@tonic-gate static int io_rcm_qpass(cfga_list_data_t *, rcmd_t *);
1487c478bd9Sstevel@tonic-gate static int (*cm_info_func(cfga_type_t))(ri_ap_t *, cfga_list_data_t *, int,
1497c478bd9Sstevel@tonic-gate     rcmd_t *);
1507c478bd9Sstevel@tonic-gate static int cpu_cm_info(ri_ap_t *, cfga_list_data_t *, int, rcmd_t *);
15161ddd3aaSscarter static int i_cpu_cm_info(processorid_t, int, int, ri_ap_t *, rcmd_t *);
1527c478bd9Sstevel@tonic-gate static int mem_cm_info(ri_ap_t *, cfga_list_data_t *, int, rcmd_t *);
1537c478bd9Sstevel@tonic-gate static int io_cm_info(ri_ap_t *, cfga_list_data_t *, int, rcmd_t *);
1547c478bd9Sstevel@tonic-gate static int ident_leaf(di_node_t);
1557c478bd9Sstevel@tonic-gate static int mk_drv_inst(di_node_t, char [], char *);
1567c478bd9Sstevel@tonic-gate static int devinfo_node_walk(di_node_t, void *);
1577c478bd9Sstevel@tonic-gate static int devinfo_minor_walk(di_node_t, di_minor_t, void *);
1587c478bd9Sstevel@tonic-gate static int devinfo_devlink_walk(di_devlink_t, void *);
1597c478bd9Sstevel@tonic-gate static int add_rcm_clients(ri_client_t **, rcmd_t *, rcm_info_t *, int, int *);
1607c478bd9Sstevel@tonic-gate static int rcm_ignore(char *, char *);
1617c478bd9Sstevel@tonic-gate static int add_query_state(rcmd_t *, ri_client_t *, const char *, const char *);
1627c478bd9Sstevel@tonic-gate static int state2query(int);
1637c478bd9Sstevel@tonic-gate static void dev_list_append(ri_dev_t **, ri_dev_t *);
1647c478bd9Sstevel@tonic-gate static void dev_list_cpu_insert(ri_dev_t **, ri_dev_t *, processorid_t);
1657c478bd9Sstevel@tonic-gate static rcm_info_tuple_t *tuple_lookup(rcmd_t *, const char *, const char *);
1667c478bd9Sstevel@tonic-gate static ri_ap_t *ri_ap_alloc(char *, ri_hdl_t *);
1677c478bd9Sstevel@tonic-gate static ri_dev_t *ri_dev_alloc(void);
1687c478bd9Sstevel@tonic-gate static ri_dev_t *io_dev_alloc(char *);
1697c478bd9Sstevel@tonic-gate static ri_client_t *ri_client_alloc(char *, char *);
1707c478bd9Sstevel@tonic-gate static void apd_tbl_free(apd_t [], int);
1717c478bd9Sstevel@tonic-gate static char *pstate2str(int);
1727c478bd9Sstevel@tonic-gate static int ecache_info_init(ecache_info_t *);
1737c478bd9Sstevel@tonic-gate static int find_cpu_nodes(di_node_t, void *);
1747c478bd9Sstevel@tonic-gate static int prop_lookup_int(di_node_t, di_prom_handle_t, char *, int **);
1757c478bd9Sstevel@tonic-gate static int add_lookup_entry(lookup_table_t *, const char *, di_node_t);
1767c478bd9Sstevel@tonic-gate static int table_compare_names(const void *, const void *);
1777c478bd9Sstevel@tonic-gate static int table_compare_indices(const void *, const void *);
1787c478bd9Sstevel@tonic-gate static lookup_entry_t *lookup(lookup_table_t *table, const char *);
1797c478bd9Sstevel@tonic-gate static int add_usage(lookup_entry_t *, const char *, rcm_info_tuple_t *);
1807c478bd9Sstevel@tonic-gate static void empty_table(lookup_table_t *);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate #ifdef DEBUG
1837c478bd9Sstevel@tonic-gate static void		dump_apd_tbl(FILE *, apd_t *, int);
1847c478bd9Sstevel@tonic-gate #endif /* DEBUG */
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate static struct {
1877c478bd9Sstevel@tonic-gate 	char	*type;
1887c478bd9Sstevel@tonic-gate 	int	(*cm_info)(ri_ap_t *, cfga_list_data_t *, int, rcmd_t *);
1897c478bd9Sstevel@tonic-gate 	int	(*cm_rcm_qpass)(cfga_list_data_t *, rcmd_t *);
1907c478bd9Sstevel@tonic-gate } cm_ctl[] = {
1917c478bd9Sstevel@tonic-gate 	{SBD_CM_CPU,	cpu_cm_info,	cpu_rcm_qpass},
1927c478bd9Sstevel@tonic-gate 	{SBD_CM_MEM,	mem_cm_info,	mem_rcm_qpass},
1937c478bd9Sstevel@tonic-gate 	{SBD_CM_IO,	io_cm_info,	io_rcm_qpass}
1947c478bd9Sstevel@tonic-gate };
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate  * Table of known info string prefixes for RCM modules that do not
1987c478bd9Sstevel@tonic-gate  * represent actual resource usage, but instead provide name translations
1997c478bd9Sstevel@tonic-gate  * or sequencing within the RCM namespace. Since RCM provides no way to
2007c478bd9Sstevel@tonic-gate  * filter these out, we must maintain this hack.
2017c478bd9Sstevel@tonic-gate  */
2027c478bd9Sstevel@tonic-gate static char *rcm_info_filter[] = {
2037c478bd9Sstevel@tonic-gate 	"Network interface",		/* Network naming module */
2047c478bd9Sstevel@tonic-gate 	NULL
2057c478bd9Sstevel@tonic-gate };
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate  * Allocate snapshot handle.
2107c478bd9Sstevel@tonic-gate  */
2117c478bd9Sstevel@tonic-gate int
ri_init(int n_apids,char ** ap_ids,int flags,ri_hdl_t ** hdlp)2127c478bd9Sstevel@tonic-gate ri_init(int n_apids, char **ap_ids, int flags, ri_hdl_t **hdlp)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	int			i, j;
2157c478bd9Sstevel@tonic-gate 	ri_hdl_t		*ri_hdl;
2167c478bd9Sstevel@tonic-gate 	ri_ap_t			*ap_hdl;
2177c478bd9Sstevel@tonic-gate 	rcmd_t			*rcm = NULL;
2187c478bd9Sstevel@tonic-gate 	cfga_list_data_t	*cfga_ldata;
2197c478bd9Sstevel@tonic-gate 	apd_t			*apd, *apd_tbl = NULL;
2207c478bd9Sstevel@tonic-gate 	int			(*cm_info)(ri_ap_t *, cfga_list_data_t *,
2217c478bd9Sstevel@tonic-gate 				    int, rcmd_t *);
2227c478bd9Sstevel@tonic-gate 	int			rv = RI_SUCCESS;
2237c478bd9Sstevel@tonic-gate 	int			cm_info_rv;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	if (n_apids <= 0 || ap_ids == NULL || hdlp == NULL)
2267c478bd9Sstevel@tonic-gate 		return (RI_INVAL);
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	if (flags & ~RI_REQ_MASK)
2297c478bd9Sstevel@tonic-gate 		return (RI_NOTSUP);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	*hdlp = NULL;
2327c478bd9Sstevel@tonic-gate 	if ((ri_hdl = calloc(1, sizeof (*ri_hdl))) == NULL ||
2337c478bd9Sstevel@tonic-gate 	    (rcm = calloc(1, sizeof (*rcm))) == NULL ||
2347c478bd9Sstevel@tonic-gate 	    (apd_tbl = calloc(n_apids, sizeof (*apd_tbl))) == NULL) {
2357c478bd9Sstevel@tonic-gate 		dprintf((stderr, "calloc: %s\n", strerror(errno)));
2367c478bd9Sstevel@tonic-gate 		rv = RI_FAILURE;
2377c478bd9Sstevel@tonic-gate 		goto out;
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	/*
2417c478bd9Sstevel@tonic-gate 	 * Create mapping of boards to components.
2427c478bd9Sstevel@tonic-gate 	 */
2437c478bd9Sstevel@tonic-gate 	for (i = 0, apd = apd_tbl; i < n_apids; i++, apd++) {
2447c478bd9Sstevel@tonic-gate 		if (dyn_ap_ids(ap_ids[i], &apd->cfga_list_data,
2457c478bd9Sstevel@tonic-gate 		    &apd->nlist) == -1) {
2467c478bd9Sstevel@tonic-gate 			rv = RI_INVAL;
2477c478bd9Sstevel@tonic-gate 			goto out;
2487c478bd9Sstevel@tonic-gate 		}
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate #ifdef DEBUG
2517c478bd9Sstevel@tonic-gate 	dump_apd_tbl(stderr, apd_tbl, n_apids);
2527c478bd9Sstevel@tonic-gate #endif /* DEBUG */
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	if (rcm_init(rcm, apd_tbl, n_apids, flags) != 0) {
2557c478bd9Sstevel@tonic-gate 		rv = RI_FAILURE;
2567c478bd9Sstevel@tonic-gate 		goto out;
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	/*
2607c478bd9Sstevel@tonic-gate 	 * Best effort attempt to read cpu ecache sizes from
2617c478bd9Sstevel@tonic-gate 	 * OBP/Solaris device trees. These are later looked up
2627c478bd9Sstevel@tonic-gate 	 * in i_cpu_cm_info().
2637c478bd9Sstevel@tonic-gate 	 */
2647c478bd9Sstevel@tonic-gate 	(void) ecache_info_init(&rcm->ecache_info);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	for (i = 0, apd = apd_tbl; i < n_apids; i++, apd++) {
2677c478bd9Sstevel@tonic-gate 		if ((ap_hdl = ri_ap_alloc(ap_ids[i], ri_hdl)) == NULL) {
2687c478bd9Sstevel@tonic-gate 			rv = RI_FAILURE;
2697c478bd9Sstevel@tonic-gate 			goto out;
2707c478bd9Sstevel@tonic-gate 		}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 		/*
2737c478bd9Sstevel@tonic-gate 		 * Add component info based on occupant type. Note all
2747c478bd9Sstevel@tonic-gate 		 * passes through the apd table skip over the first
2757c478bd9Sstevel@tonic-gate 		 * cfgadm_list_data entry, which is the static system board
2767c478bd9Sstevel@tonic-gate 		 * attachment point.
2777c478bd9Sstevel@tonic-gate 		 */
2787c478bd9Sstevel@tonic-gate 		for (j = 1, cfga_ldata = &apd->cfga_list_data[1];
2797c478bd9Sstevel@tonic-gate 		    j < apd->nlist; j++, cfga_ldata++) {
2807c478bd9Sstevel@tonic-gate 			if (cfga_ldata->ap_o_state != CFGA_STAT_CONFIGURED) {
2817c478bd9Sstevel@tonic-gate 				continue;
2827c478bd9Sstevel@tonic-gate 			}
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 			if ((cm_info =
2857c478bd9Sstevel@tonic-gate 			    cm_info_func(cfga_ldata->ap_type)) != NULL) {
2867c478bd9Sstevel@tonic-gate 				cm_info_rv =
2877c478bd9Sstevel@tonic-gate 				    (*cm_info)(ap_hdl, cfga_ldata, flags, rcm);
2887c478bd9Sstevel@tonic-gate 				if (cm_info_rv != 0) {
2897c478bd9Sstevel@tonic-gate 					/*
2907c478bd9Sstevel@tonic-gate 					 * If we cannot obtain info for the ap,
2917c478bd9Sstevel@tonic-gate 					 * skip it and do not fail the entire
2927c478bd9Sstevel@tonic-gate 					 * operation.  This case occurs when the
2937c478bd9Sstevel@tonic-gate 					 * driver for a device is not attached:
2947c478bd9Sstevel@tonic-gate 					 * di_init() returns failed back to
2957c478bd9Sstevel@tonic-gate 					 * io_cm_info().
2967c478bd9Sstevel@tonic-gate 					 */
2977c478bd9Sstevel@tonic-gate 					if (cm_info_rv == RI_NODE_NIL)
2987c478bd9Sstevel@tonic-gate 						continue;
2997c478bd9Sstevel@tonic-gate 					else {
3007c478bd9Sstevel@tonic-gate 						rv = RI_FAILURE;
3017c478bd9Sstevel@tonic-gate 						goto out;
3027c478bd9Sstevel@tonic-gate 					}
3037c478bd9Sstevel@tonic-gate 				}
3047c478bd9Sstevel@tonic-gate 			}
3057c478bd9Sstevel@tonic-gate 		}
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if ((flags & RI_INCLUDE_QUERY) && cap_request(ri_hdl, rcm) != 0)
3097c478bd9Sstevel@tonic-gate 		rv = RI_FAILURE;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate out:
3127c478bd9Sstevel@tonic-gate 	if (apd_tbl != NULL)
3137c478bd9Sstevel@tonic-gate 		apd_tbl_free(apd_tbl, n_apids);
3147c478bd9Sstevel@tonic-gate 	if (rcm != NULL)
3157c478bd9Sstevel@tonic-gate 		rcm_fini(rcm);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	if (rv == RI_SUCCESS)
3187c478bd9Sstevel@tonic-gate 		*hdlp = ri_hdl;
3197c478bd9Sstevel@tonic-gate 	else
3207c478bd9Sstevel@tonic-gate 		ri_fini(ri_hdl);
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	return (rv);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate /*
3267c478bd9Sstevel@tonic-gate  * Map static board attachment point to dynamic attachment points (components).
3277c478bd9Sstevel@tonic-gate  */
3287c478bd9Sstevel@tonic-gate static int
dyn_ap_ids(char * ap_id,cfga_list_data_t ** ap_id_list,int * nlist)3297c478bd9Sstevel@tonic-gate dyn_ap_ids(char *ap_id, cfga_list_data_t **ap_id_list, int *nlist)
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate 	cfga_err_t	cfga_err;
3327c478bd9Sstevel@tonic-gate 	char		*errstr;
3337c478bd9Sstevel@tonic-gate 	char		*opts = "parsable";
3347c478bd9Sstevel@tonic-gate 	char		*listops = "class=sbd";
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	cfga_err = config_list_ext(1, &ap_id, ap_id_list, nlist,
3377c478bd9Sstevel@tonic-gate 	    opts, listops, &errstr, CFGA_FLAG_LIST_ALL);
3387c478bd9Sstevel@tonic-gate 	if (cfga_err != CFGA_OK) {
3397c478bd9Sstevel@tonic-gate 		dprintf((stderr, "config_list_ext: %s\n",
3407c478bd9Sstevel@tonic-gate 		    config_strerror(cfga_err)));
3417c478bd9Sstevel@tonic-gate 		return (-1);
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	return (0);
3457c478bd9Sstevel@tonic-gate }
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate /*
3487c478bd9Sstevel@tonic-gate  * Initialize rcm handle, memory stats. Cache query result if necessary.
3497c478bd9Sstevel@tonic-gate  */
3507c478bd9Sstevel@tonic-gate static int
rcm_init(rcmd_t * rcm,apd_t apd_tbl[],int napds,int flags)3517c478bd9Sstevel@tonic-gate rcm_init(rcmd_t *rcm, apd_t apd_tbl[], int napds, int flags)
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate 	longlong_t	ii;
3547c478bd9Sstevel@tonic-gate 	int		rv = 0;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	rcm->offline_query_info = NULL;
3577c478bd9Sstevel@tonic-gate 	rcm->rlist = NULL;
3587c478bd9Sstevel@tonic-gate 	rcm->cpus = NULL;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	if (rcm_alloc_handle(NULL, RCM_NOPID, NULL, &rcm->hdl) != RCM_SUCCESS) {
3617c478bd9Sstevel@tonic-gate 		dprintf((stderr, "rcm_alloc_handle (errno=%d)\n", errno));
3627c478bd9Sstevel@tonic-gate 		return (-1);
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	if ((rcm->ms_pagesize = sysconf(_SC_PAGE_SIZE)) == -1 ||
3667c478bd9Sstevel@tonic-gate 	    (rcm->ms_syspages = sysconf(_SC_PHYS_PAGES)) == -1) {
3677c478bd9Sstevel@tonic-gate 		dprintf((stderr, "sysconf: %s\n", strerror(errno)));
3687c478bd9Sstevel@tonic-gate 		return (-1);
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 	ii = (longlong_t)rcm->ms_pagesize * rcm->ms_syspages;
3717c478bd9Sstevel@tonic-gate 	rcm->ms_sysmb = (int)((ii+MBYTE-1) / MBYTE);
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	if (flags & RI_INCLUDE_QUERY)
374fc0105deSJames Anderson 		rv = rcm_query_init(rcm, apd_tbl, napds);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	return (rv);
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate static void
rcm_fini(rcmd_t * rcm)3807c478bd9Sstevel@tonic-gate rcm_fini(rcmd_t *rcm)
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	char	**cpp;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	assert(rcm != NULL);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	if (rcm->offline_query_info != NULL)
3877c478bd9Sstevel@tonic-gate 		rcm_free_info(rcm->offline_query_info);
3887c478bd9Sstevel@tonic-gate 	if (rcm->hdl != NULL)
3897c478bd9Sstevel@tonic-gate 		rcm_free_handle(rcm->hdl);
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	if (rcm->rlist != NULL) {
3927c478bd9Sstevel@tonic-gate 		for (cpp = rcm->rlist; *cpp != NULL; cpp++)
3937c478bd9Sstevel@tonic-gate 			s_free(*cpp);
3947c478bd9Sstevel@tonic-gate 		free(rcm->rlist);
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	s_free(rcm->cpus);
3987c478bd9Sstevel@tonic-gate 	free(rcm);
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate #define	NODENAME_CMP		"cmp"
4027c478bd9Sstevel@tonic-gate #define	NODENAME_SSM		"ssm"
4037c478bd9Sstevel@tonic-gate #define	PROP_CPUID		"cpuid"
4047c478bd9Sstevel@tonic-gate #define	PROP_DEVICE_TYPE	"device-type"
4057c478bd9Sstevel@tonic-gate #define	PROP_ECACHE_SIZE	"ecache-size"
4067c478bd9Sstevel@tonic-gate #define	PROP_L2_CACHE_SIZE	"l2-cache-size"
4077c478bd9Sstevel@tonic-gate #define	PROP_L3_CACHE_SIZE	"l3-cache-size"
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate typedef struct {
4107c478bd9Sstevel@tonic-gate 	di_node_t		root;
4117c478bd9Sstevel@tonic-gate 	di_prom_handle_t	ph;
4127c478bd9Sstevel@tonic-gate 	ecache_info_t		*ecache_info;
4137c478bd9Sstevel@tonic-gate } di_arg_t;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate /*
4167c478bd9Sstevel@tonic-gate  * The ecache sizes for individual cpus are read from the
4177c478bd9Sstevel@tonic-gate  * OBP/Solaris device trees. This info cannot be derived
4187c478bd9Sstevel@tonic-gate  * from the cfgadm_sbd cpu attachment point ecache info,
4197c478bd9Sstevel@tonic-gate  * which may be a sum of multiple cores for CMP.
4207c478bd9Sstevel@tonic-gate  */
4217c478bd9Sstevel@tonic-gate static int
ecache_info_init(ecache_info_t * ec)4227c478bd9Sstevel@tonic-gate ecache_info_init(ecache_info_t *ec)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate 	di_arg_t	di_arg;
4257c478bd9Sstevel@tonic-gate 	di_prom_handle_t ph = DI_PROM_HANDLE_NIL;
4267c478bd9Sstevel@tonic-gate 	di_node_t	root = DI_NODE_NIL;
4277c478bd9Sstevel@tonic-gate 	int		cpuid_max, rv = 0;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	assert(ec != NULL && ec->cpuid_max == 0 && ec->ecache_sizes == NULL);
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	if ((cpuid_max = sysconf(_SC_CPUID_MAX)) == -1) {
4327c478bd9Sstevel@tonic-gate 		dprintf((stderr, "sysconf fail: %s\n", strerror(errno)));
4337c478bd9Sstevel@tonic-gate 		rv = -1;
4347c478bd9Sstevel@tonic-gate 		goto done;
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	if ((root = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
4387c478bd9Sstevel@tonic-gate 		dprintf((stderr, "di_init fail: %s\n", strerror(errno)));
4397c478bd9Sstevel@tonic-gate 		rv = -1;
4407c478bd9Sstevel@tonic-gate 		goto done;
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	if ((ph = di_prom_init()) == DI_PROM_HANDLE_NIL) {
4447c478bd9Sstevel@tonic-gate 		dprintf((stderr, "di_prom_init fail: %s\n", strerror(errno)));
4457c478bd9Sstevel@tonic-gate 		rv = -1;
4467c478bd9Sstevel@tonic-gate 		goto done;
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	if ((ec->ecache_sizes = calloc(cpuid_max + 1, sizeof (int))) == NULL) {
4507c478bd9Sstevel@tonic-gate 		dprintf((stderr, "calloc fail: %s\n", strerror(errno)));
4517c478bd9Sstevel@tonic-gate 		rv = -1;
4527c478bd9Sstevel@tonic-gate 		goto done;
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 	ec->cpuid_max = cpuid_max;
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	dprintf((stderr, "cpuid_max is set to %d\n", ec->cpuid_max));
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	di_arg.ph = ph;
4597c478bd9Sstevel@tonic-gate 	di_arg.root = root;
4607c478bd9Sstevel@tonic-gate 	di_arg.ecache_info = ec;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	if (di_walk_node(root, DI_WALK_CLDFIRST, (void *)&di_arg,
4637c478bd9Sstevel@tonic-gate 	    find_cpu_nodes) != 0) {
4647c478bd9Sstevel@tonic-gate 		dprintf((stderr, "di_walk_node fail: %s\n", strerror(errno)));
4657c478bd9Sstevel@tonic-gate 		rv = -1;
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate done:
4697c478bd9Sstevel@tonic-gate 	if (root != DI_NODE_NIL)
4707c478bd9Sstevel@tonic-gate 		di_fini(root);
4717c478bd9Sstevel@tonic-gate 	if (ph != DI_PROM_HANDLE_NIL)
4727c478bd9Sstevel@tonic-gate 		di_prom_fini(ph);
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	return (rv);
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate /*
4787c478bd9Sstevel@tonic-gate  * Libdevinfo node walk callback for reading ecache size
4797c478bd9Sstevel@tonic-gate  * properties for cpu device nodes. Subtrees not containing
4807c478bd9Sstevel@tonic-gate  * cpu nodes are filtered out.
4817c478bd9Sstevel@tonic-gate  */
4827c478bd9Sstevel@tonic-gate static int
find_cpu_nodes(di_node_t node,void * arg)4837c478bd9Sstevel@tonic-gate find_cpu_nodes(di_node_t node, void *arg)
4847c478bd9Sstevel@tonic-gate {
4857c478bd9Sstevel@tonic-gate 	char			*name;
4867c478bd9Sstevel@tonic-gate 	int			*cpuid, *ecache;
4877c478bd9Sstevel@tonic-gate 	di_arg_t		*di_arg = (di_arg_t *)arg;
4887c478bd9Sstevel@tonic-gate 	ecache_info_t		*ec = di_arg->ecache_info;
4897c478bd9Sstevel@tonic-gate 	di_prom_handle_t	ph = di_arg->ph;
490fc0105deSJames Anderson 	int			walk_child = 0;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
4937c478bd9Sstevel@tonic-gate 		return (DI_WALK_TERMINATE);
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	if (node == di_arg->root) {
4977c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
4987c478bd9Sstevel@tonic-gate 	}
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	if (di_nodeid(node) == DI_PSEUDO_NODEID) {
5017c478bd9Sstevel@tonic-gate 		return (DI_WALK_PRUNECHILD);
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	name = di_node_name(node);
5057c478bd9Sstevel@tonic-gate 	if (name != NULL) {
5067c478bd9Sstevel@tonic-gate 		/*
5077c478bd9Sstevel@tonic-gate 		 * CMP nodes will be the parent of cpu nodes. On some platforms,
5087c478bd9Sstevel@tonic-gate 		 * cpu nodes will be under the ssm node. In either case,
5097c478bd9Sstevel@tonic-gate 		 * continue searching this subtree.
5107c478bd9Sstevel@tonic-gate 		 */
5117c478bd9Sstevel@tonic-gate 		if (strncmp(name, NODENAME_SSM, strlen(NODENAME_SSM)) == 0 ||
5127c478bd9Sstevel@tonic-gate 		    strncmp(name, NODENAME_CMP, strlen(NODENAME_CMP)) == 0) {
5137c478bd9Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
5147c478bd9Sstevel@tonic-gate 		}
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	dprintf((stderr, "find_cpu_nodes: node=%p, name=%s, binding_name=%s\n",
5187c478bd9Sstevel@tonic-gate 	    node, di_node_name(node), di_binding_name(node)));
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	/*
5217c478bd9Sstevel@tonic-gate 	 * Ecache size property name differs with processor implementation.
5227c478bd9Sstevel@tonic-gate 	 * Panther has both L2 and L3, so check for L3 first to differentiate
5237c478bd9Sstevel@tonic-gate 	 * from Jaguar, which has only L2.
5247c478bd9Sstevel@tonic-gate 	 */
525fc0105deSJames Anderson 	if (prop_lookup_int(node, ph, PROP_ECACHE_SIZE, &ecache) == 0 ||
5267c478bd9Sstevel@tonic-gate 	    prop_lookup_int(node, ph, PROP_L3_CACHE_SIZE, &ecache) == 0 ||
527fc0105deSJames Anderson 	    prop_lookup_int(node, ph, PROP_L2_CACHE_SIZE, &ecache) == 0) {
528fc0105deSJames Anderson 		/*
529fc0105deSJames Anderson 		 * On some platforms the cache property is in the core
530fc0105deSJames Anderson 		 * node while the cpuid is in the child cpu node.  It may
531fc0105deSJames Anderson 		 * be needed while processing this node or a child node.
532fc0105deSJames Anderson 		 */
533fc0105deSJames Anderson 		ec->ecache_curr = *ecache;
534fc0105deSJames Anderson 		walk_child = 1;
535fc0105deSJames Anderson 	}
536fc0105deSJames Anderson 
537fc0105deSJames Anderson 	if (prop_lookup_int(node, ph, PROP_CPUID, &cpuid) == 0) {
538fc0105deSJames Anderson 
5397c478bd9Sstevel@tonic-gate 		assert(ec != NULL && ec->ecache_sizes != NULL &&
5407c478bd9Sstevel@tonic-gate 		    *cpuid <= ec->cpuid_max);
541fc0105deSJames Anderson 
542fc0105deSJames Anderson 		if (ec->ecache_curr != 0) {
543fc0105deSJames Anderson 			ec->ecache_sizes[*cpuid] = ec->ecache_curr;
544fc0105deSJames Anderson 
545fc0105deSJames Anderson 		}
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 
548fc0105deSJames Anderson 	return (walk_child ? DI_WALK_CONTINUE : DI_WALK_PRUNECHILD);
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate /*
5527c478bd9Sstevel@tonic-gate  * Given a di_node_t, call the appropriate int property lookup routine.
5537c478bd9Sstevel@tonic-gate  * Note: This lookup fails if the int property has multiple value entries.
5547c478bd9Sstevel@tonic-gate  */
5557c478bd9Sstevel@tonic-gate static int
prop_lookup_int(di_node_t node,di_prom_handle_t ph,char * propname,int ** ival)5567c478bd9Sstevel@tonic-gate prop_lookup_int(di_node_t node, di_prom_handle_t ph, char *propname, int **ival)
5577c478bd9Sstevel@tonic-gate {
5587c478bd9Sstevel@tonic-gate 	int rv;
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	rv = (di_nodeid(node) == DI_PROM_NODEID) ?
5617c478bd9Sstevel@tonic-gate 	    di_prom_prop_lookup_ints(ph, node, propname, ival) :
5627c478bd9Sstevel@tonic-gate 	    di_prop_lookup_ints(DDI_DEV_T_ANY, node, propname, ival);
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	return (rv == 1 ? 0 : -1);
5657c478bd9Sstevel@tonic-gate }
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate /*
5687c478bd9Sstevel@tonic-gate  * For offline queries, RCM must be given a list of all resources
5697c478bd9Sstevel@tonic-gate  * so modules can have access to the full scope of the operation.
5707c478bd9Sstevel@tonic-gate  * The rcm_get_info calls are made individually in order to map the
5717c478bd9Sstevel@tonic-gate  * returned rcm_info_t's to physical devices. The rcm_request_offline
5727c478bd9Sstevel@tonic-gate  * result is cached so the query state can be looked up as we process
5737c478bd9Sstevel@tonic-gate  * the rcm_get_info calls. This routine also tallies up the amount of
5747c478bd9Sstevel@tonic-gate  * memory going away and creates a list of cpu ids to be used
5757c478bd9Sstevel@tonic-gate  * later for rcm_request_capacity_change.
5767c478bd9Sstevel@tonic-gate  */
5777c478bd9Sstevel@tonic-gate static int
rcm_query_init(rcmd_t * rcm,apd_t apd_tbl[],int napds)5787c478bd9Sstevel@tonic-gate rcm_query_init(rcmd_t *rcm, apd_t apd_tbl[], int napds)
5797c478bd9Sstevel@tonic-gate {
5807c478bd9Sstevel@tonic-gate 	apd_t			*apd;
5817c478bd9Sstevel@tonic-gate 	int 			i, j;
5827c478bd9Sstevel@tonic-gate 	cfga_list_data_t	*cfga_ldata;
5837c478bd9Sstevel@tonic-gate 	int			(*cm_rcm_qpass)(cfga_list_data_t *, rcmd_t *);
5847c478bd9Sstevel@tonic-gate #ifdef DEBUG
5857c478bd9Sstevel@tonic-gate 	char			**cpp;
5867c478bd9Sstevel@tonic-gate #endif /* DEBUG */
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	/*
5897c478bd9Sstevel@tonic-gate 	 * Initial pass to size cpu and resource name arrays needed to
5907c478bd9Sstevel@tonic-gate 	 * interface with RCM. Attachment point ids for CMP can represent
5917c478bd9Sstevel@tonic-gate 	 * multiple cpus (and resource names). Instead of parsing the
5927c478bd9Sstevel@tonic-gate 	 * cfgadm info field here, use the worse case that all component
5937c478bd9Sstevel@tonic-gate 	 * attachment points are CMP.
5947c478bd9Sstevel@tonic-gate 	 */
5957c478bd9Sstevel@tonic-gate 	rcm->ndevs = 0;
5967c478bd9Sstevel@tonic-gate 	for (i = 0, apd = apd_tbl; i < napds; i++, apd++) {
5977c478bd9Sstevel@tonic-gate 		for (j = 1, cfga_ldata = &apd->cfga_list_data[1];
5987c478bd9Sstevel@tonic-gate 		    j < apd->nlist; j++, cfga_ldata++) {
5997c478bd9Sstevel@tonic-gate 			if (cfga_ldata->ap_o_state != CFGA_STAT_CONFIGURED) {
6007c478bd9Sstevel@tonic-gate 				continue;
6017c478bd9Sstevel@tonic-gate 			}
6027c478bd9Sstevel@tonic-gate 			rcm->ndevs += SBD_MAX_CORES_PER_CMP;
6037c478bd9Sstevel@tonic-gate 		}
6047c478bd9Sstevel@tonic-gate 	}
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	/* account for trailing NULL in rlist */
6077c478bd9Sstevel@tonic-gate 	if (rcm->ndevs > 0 &&
6087c478bd9Sstevel@tonic-gate 	    ((rcm->cpus = calloc(rcm->ndevs, sizeof (cpuid_t))) == NULL ||
6097c478bd9Sstevel@tonic-gate 	    (rcm->rlist = calloc(rcm->ndevs + 1, sizeof (char *))) == NULL)) {
6107c478bd9Sstevel@tonic-gate 		dprintf((stderr, "calloc: %s\n", strerror(errno)));
6117c478bd9Sstevel@tonic-gate 		return (-1);
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	/*
6157c478bd9Sstevel@tonic-gate 	 * Second pass to fill in the RCM resource and cpu lists.
6167c478bd9Sstevel@tonic-gate 	 */
6177c478bd9Sstevel@tonic-gate 	for (i = 0, apd = apd_tbl; i < napds; i++, apd++) {
6187c478bd9Sstevel@tonic-gate 		for (j = 1, cfga_ldata = &apd->cfga_list_data[1];
6197c478bd9Sstevel@tonic-gate 		    j < apd->nlist; j++, cfga_ldata++) {
6207c478bd9Sstevel@tonic-gate 			if (cfga_ldata->ap_o_state != CFGA_STAT_CONFIGURED) {
6217c478bd9Sstevel@tonic-gate 				continue;
6227c478bd9Sstevel@tonic-gate 			}
6237c478bd9Sstevel@tonic-gate 			if ((cm_rcm_qpass =
6247c478bd9Sstevel@tonic-gate 			    cm_rcm_qpass_func(cfga_ldata->ap_type)) != NULL &&
6257c478bd9Sstevel@tonic-gate 			    (*cm_rcm_qpass)(cfga_ldata, rcm) != 0) {
6267c478bd9Sstevel@tonic-gate 				return (-1);
6277c478bd9Sstevel@tonic-gate 			}
6287c478bd9Sstevel@tonic-gate 		}
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	if (rcm->nrlist == 0)
6327c478bd9Sstevel@tonic-gate 		return (0);
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	/*
6357c478bd9Sstevel@tonic-gate 	 * Cache query result. Since we are only interested in the
6367c478bd9Sstevel@tonic-gate 	 * set of RCM clients processed and not their request status,
6377c478bd9Sstevel@tonic-gate 	 * the return value is irrelevant.
6387c478bd9Sstevel@tonic-gate 	 */
6397c478bd9Sstevel@tonic-gate 	(void) rcm_request_offline_list(rcm->hdl, rcm->rlist,
6407c478bd9Sstevel@tonic-gate 	    RCM_QUERY|RCM_SCOPE, &rcm->offline_query_info);
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate #ifdef DEBUG
6437c478bd9Sstevel@tonic-gate 	dprintf((stderr, "RCM rlist: nrlist=%d\n", rcm->nrlist));
6447c478bd9Sstevel@tonic-gate 	for (cpp = rcm->rlist, i = 0; *cpp != NULL; cpp++, i++) {
6457c478bd9Sstevel@tonic-gate 		dprintf((stderr, "rlist[%d]=%s\n", i, *cpp));
6467c478bd9Sstevel@tonic-gate 	}
6477c478bd9Sstevel@tonic-gate #endif /* DEBUG */
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	return (0);
6507c478bd9Sstevel@tonic-gate }
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate static int
cap_request(ri_hdl_t * ri_hdl,rcmd_t * rcm)6537c478bd9Sstevel@tonic-gate cap_request(ri_hdl_t *ri_hdl, rcmd_t *rcm)
6547c478bd9Sstevel@tonic-gate {
6557c478bd9Sstevel@tonic-gate 	return (((rcm->ncpus > 0 && cpu_cap_request(ri_hdl, rcm) != 0) ||
6567c478bd9Sstevel@tonic-gate 	    (rcm->query_pages > 0 && mem_cap_request(ri_hdl, rcm) != 0)) ?
6577c478bd9Sstevel@tonic-gate 	    -1 : 0);
6587c478bd9Sstevel@tonic-gate }
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate /*
6617c478bd9Sstevel@tonic-gate  * RCM capacity change request for cpus.
6627c478bd9Sstevel@tonic-gate  */
6637c478bd9Sstevel@tonic-gate static int
cpu_cap_request(ri_hdl_t * ri_hdl,rcmd_t * rcm)6647c478bd9Sstevel@tonic-gate cpu_cap_request(ri_hdl_t *ri_hdl, rcmd_t *rcm)
6657c478bd9Sstevel@tonic-gate {
6667c478bd9Sstevel@tonic-gate 	cpuid_t		*syscpuids, *newcpuids;
6677c478bd9Sstevel@tonic-gate 	int		sysncpus, newncpus;
6687c478bd9Sstevel@tonic-gate 	rcm_info_t	*rcm_info = NULL;
6697c478bd9Sstevel@tonic-gate 	int		i, j, k;
6707c478bd9Sstevel@tonic-gate 	nvlist_t	*nvl;
6717c478bd9Sstevel@tonic-gate 	int		rv = 0;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	/* get all cpus in the system */
6747c478bd9Sstevel@tonic-gate 	if (syscpus(&syscpuids, &sysncpus) == -1)
6757c478bd9Sstevel@tonic-gate 		return (-1);
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	newncpus = sysncpus - rcm->ncpus;
6787c478bd9Sstevel@tonic-gate 	if ((newcpuids = calloc(newncpus, sizeof (cpuid_t))) == NULL) {
6797c478bd9Sstevel@tonic-gate 		dprintf((stderr, "calloc: %s", strerror(errno)));
6807c478bd9Sstevel@tonic-gate 		rv = -1;
6817c478bd9Sstevel@tonic-gate 		goto out;
6827c478bd9Sstevel@tonic-gate 	}
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
6857c478bd9Sstevel@tonic-gate 		dprintf((stderr, "nvlist_alloc fail\n"));
6867c478bd9Sstevel@tonic-gate 		rv = -1;
6877c478bd9Sstevel@tonic-gate 		goto out;
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	/*
6917c478bd9Sstevel@tonic-gate 	 * Construct the new cpu list.
6927c478bd9Sstevel@tonic-gate 	 */
6937c478bd9Sstevel@tonic-gate 	for (i = 0, j = 0; i < sysncpus; i++) {
6947c478bd9Sstevel@tonic-gate 		for (k = 0; k < rcm->ncpus; k++) {
6957c478bd9Sstevel@tonic-gate 			if (rcm->cpus[k] == syscpuids[i]) {
6967c478bd9Sstevel@tonic-gate 				break;
6977c478bd9Sstevel@tonic-gate 			}
6987c478bd9Sstevel@tonic-gate 		}
6997c478bd9Sstevel@tonic-gate 		if (k == rcm->ncpus) {
7007c478bd9Sstevel@tonic-gate 			newcpuids[j++] = syscpuids[i];
7017c478bd9Sstevel@tonic-gate 		}
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	if (nvlist_add_int32(nvl, "old_total", sysncpus) != 0 ||
7057c478bd9Sstevel@tonic-gate 	    nvlist_add_int32(nvl, "new_total", newncpus) != 0 ||
7067c478bd9Sstevel@tonic-gate 	    nvlist_add_int32_array(nvl, "old_cpu_list", syscpuids,
7077c478bd9Sstevel@tonic-gate 	    sysncpus) != 0 ||
7087c478bd9Sstevel@tonic-gate 	    nvlist_add_int32_array(nvl, "new_cpu_list", newcpuids,
7097c478bd9Sstevel@tonic-gate 	    newncpus) != 0) {
7107c478bd9Sstevel@tonic-gate 		dprintf((stderr, "nvlist_add fail\n"));
7117c478bd9Sstevel@tonic-gate 		rv = -1;
7127c478bd9Sstevel@tonic-gate 		goto out;
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate #ifdef DEBUG
7167c478bd9Sstevel@tonic-gate 	dprintf((stderr, "old_total=%d\n", sysncpus));
7177c478bd9Sstevel@tonic-gate 	for (i = 0; i < sysncpus; i++) {
7187c478bd9Sstevel@tonic-gate 		dprintf((stderr, "old_cpu_list[%d]=%d\n", i, syscpuids[i]));
7197c478bd9Sstevel@tonic-gate 	}
7207c478bd9Sstevel@tonic-gate 	dprintf((stderr, "new_total=%d\n", newncpus));
7217c478bd9Sstevel@tonic-gate 	for (i = 0; i < newncpus; i++) {
7227c478bd9Sstevel@tonic-gate 		dprintf((stderr, "new_cpu_list[%d]=%d\n", i, newcpuids[i]));
7237c478bd9Sstevel@tonic-gate 	}
7247c478bd9Sstevel@tonic-gate #endif /* DEBUG */
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	(void) rcm_request_capacity_change(rcm->hdl, RCM_CPU_ALL,
7277c478bd9Sstevel@tonic-gate 	    RCM_QUERY|RCM_SCOPE, nvl, &rcm_info);
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	rv = add_rcm_clients(&ri_hdl->cpu_cap_clients, rcm, rcm_info, 0, NULL);
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate out:
7327c478bd9Sstevel@tonic-gate 	s_free(syscpuids);
7337c478bd9Sstevel@tonic-gate 	s_free(newcpuids);
734*aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(nvl);
7357c478bd9Sstevel@tonic-gate 	if (rcm_info != NULL)
7367c478bd9Sstevel@tonic-gate 		rcm_free_info(rcm_info);
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	return (rv);
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate static int
syscpus(cpuid_t ** cpuids,int * ncpus)7427c478bd9Sstevel@tonic-gate syscpus(cpuid_t **cpuids, int *ncpus)
7437c478bd9Sstevel@tonic-gate {
7447c478bd9Sstevel@tonic-gate 	kstat_t		*ksp;
7457c478bd9Sstevel@tonic-gate 	kstat_ctl_t	*kc;
7467c478bd9Sstevel@tonic-gate 	cpuid_t		*cp;
7477c478bd9Sstevel@tonic-gate 	int		i;
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	if ((*ncpus = sysconf(_SC_NPROCESSORS_CONF)) == -1) {
7507c478bd9Sstevel@tonic-gate 		dprintf((stderr, "sysconf: %s\n", errno));
7517c478bd9Sstevel@tonic-gate 		return (-1);
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	if ((kc = kstat_open()) == NULL) {
7557c478bd9Sstevel@tonic-gate 		dprintf((stderr, "kstat_open fail\n"));
7567c478bd9Sstevel@tonic-gate 		return (-1);
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	if ((cp = calloc(*ncpus, sizeof (cpuid_t))) == NULL) {
7607c478bd9Sstevel@tonic-gate 		dprintf((stderr, "calloc: %s\n", errno));
7617c478bd9Sstevel@tonic-gate 		(void) kstat_close(kc);
7627c478bd9Sstevel@tonic-gate 		return (-1);
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	for (i = 0, ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
7667c478bd9Sstevel@tonic-gate 		if (strcmp(ksp->ks_module, "cpu_info") == 0) {
7677c478bd9Sstevel@tonic-gate 			cp[i++] = ksp->ks_instance;
7687c478bd9Sstevel@tonic-gate 		}
7697c478bd9Sstevel@tonic-gate 	}
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	(void) kstat_close(kc);
7727c478bd9Sstevel@tonic-gate 	*cpuids = cp;
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	return (0);
7757c478bd9Sstevel@tonic-gate }
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate /*
7787c478bd9Sstevel@tonic-gate  * RCM capacity change request for memory.
7797c478bd9Sstevel@tonic-gate  */
7807c478bd9Sstevel@tonic-gate static int
mem_cap_request(ri_hdl_t * ri_hdl,rcmd_t * rcm)7817c478bd9Sstevel@tonic-gate mem_cap_request(ri_hdl_t *ri_hdl, rcmd_t *rcm)
7827c478bd9Sstevel@tonic-gate {
7837c478bd9Sstevel@tonic-gate 	nvlist_t	*nvl;
7847c478bd9Sstevel@tonic-gate 	rcm_info_t	*rcm_info = NULL;
7857c478bd9Sstevel@tonic-gate 	long 		newpages;
7867c478bd9Sstevel@tonic-gate 	int		rv = 0;
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
7897c478bd9Sstevel@tonic-gate 		dprintf((stderr, "nvlist_alloc fail\n"));
7907c478bd9Sstevel@tonic-gate 		return (-1);
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	newpages = rcm->ms_syspages - rcm->query_pages;
7947c478bd9Sstevel@tonic-gate 	if (nvlist_add_int32(nvl, "page_size", rcm->ms_pagesize) != 0 ||
7957c478bd9Sstevel@tonic-gate 	    nvlist_add_int32(nvl, "old_pages", rcm->ms_syspages) != 0 ||
7967c478bd9Sstevel@tonic-gate 	    nvlist_add_int32(nvl, "new_pages", newpages) != 0) {
7977c478bd9Sstevel@tonic-gate 		dprintf((stderr, "nvlist_add fail\n"));
7987c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
7997c478bd9Sstevel@tonic-gate 		return (-1);
8007c478bd9Sstevel@tonic-gate 	}
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	dprintf((stderr, "memory capacity change req: "
8037c478bd9Sstevel@tonic-gate 	    "page_size=%d, old_pages=%d, new_pages=%d\n",
8047c478bd9Sstevel@tonic-gate 	    rcm->ms_pagesize, rcm->ms_syspages, newpages));
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	(void) rcm_request_capacity_change(rcm->hdl, RCM_MEM_ALL,
8077c478bd9Sstevel@tonic-gate 	    RCM_QUERY|RCM_SCOPE, nvl, &rcm_info);
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	rv = add_rcm_clients(&ri_hdl->mem_cap_clients, rcm, rcm_info, 0, NULL);
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	nvlist_free(nvl);
8127c478bd9Sstevel@tonic-gate 	if (rcm_info != NULL)
8137c478bd9Sstevel@tonic-gate 		rcm_free_info(rcm_info);
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	return (rv);
8167c478bd9Sstevel@tonic-gate }
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate static int
cm_rcm_qpass_func(cfga_type_t ap_type)8197c478bd9Sstevel@tonic-gate (*cm_rcm_qpass_func(cfga_type_t ap_type))(cfga_list_data_t *, rcmd_t *)
8207c478bd9Sstevel@tonic-gate {
8217c478bd9Sstevel@tonic-gate 	int i;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (cm_ctl) / sizeof (cm_ctl[0]); i++) {
8247c478bd9Sstevel@tonic-gate 		if (strcmp(cm_ctl[i].type, ap_type) == 0) {
8257c478bd9Sstevel@tonic-gate 			return (cm_ctl[i].cm_rcm_qpass);
8267c478bd9Sstevel@tonic-gate 		}
8277c478bd9Sstevel@tonic-gate 	}
8287c478bd9Sstevel@tonic-gate 	return (NULL);
8297c478bd9Sstevel@tonic-gate }
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate /*
8327c478bd9Sstevel@tonic-gate  * Save cpu ids and RCM abstract resource names.
8337c478bd9Sstevel@tonic-gate  * Cpu ids will be used for the capacity change request.
8347c478bd9Sstevel@tonic-gate  * Resource names will be used for the offline query.
8357c478bd9Sstevel@tonic-gate  */
8367c478bd9Sstevel@tonic-gate static int
cpu_rcm_qpass(cfga_list_data_t * cfga_ldata,rcmd_t * rcm)8377c478bd9Sstevel@tonic-gate cpu_rcm_qpass(cfga_list_data_t *cfga_ldata, rcmd_t *rcm)
8387c478bd9Sstevel@tonic-gate {
8397c478bd9Sstevel@tonic-gate 	processorid_t	cpuid;
8407c478bd9Sstevel@tonic-gate 	char		*cpustr, *lasts, *rsrcname, rbuf[32];
8417c478bd9Sstevel@tonic-gate 	char		cbuf[CFGA_INFO_LEN];
8427c478bd9Sstevel@tonic-gate 	int		speed, ecache;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	assert(sscanf(cfga_ldata->ap_info, CPU_INFO_FMT, &cbuf, &speed,
8457c478bd9Sstevel@tonic-gate 	    &ecache) == 3);
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	for (cpustr = (char *)strtok_r(cbuf, CPUID_SEP, &lasts);
8487c478bd9Sstevel@tonic-gate 	    cpustr != NULL;
8497c478bd9Sstevel@tonic-gate 	    cpustr = (char *)strtok_r(NULL, CPUID_SEP, &lasts)) {
8507c478bd9Sstevel@tonic-gate 		cpuid = atoi(cpustr);
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 		(void) snprintf(rbuf, sizeof (rbuf), "%s%d", RCM_CPU, cpuid);
8537c478bd9Sstevel@tonic-gate 		if ((rsrcname = strdup(rbuf)) == NULL) {
8547c478bd9Sstevel@tonic-gate 			dprintf((stderr, "strdup fail\n"));
8557c478bd9Sstevel@tonic-gate 			return (-1);
8567c478bd9Sstevel@tonic-gate 		}
8577c478bd9Sstevel@tonic-gate 		assert(rcm->nrlist < rcm->ndevs && rcm->ncpus < rcm->ndevs);
8587c478bd9Sstevel@tonic-gate 		rcm->rlist[rcm->nrlist++] = rsrcname;
8597c478bd9Sstevel@tonic-gate 		rcm->cpus[rcm->ncpus++] = (cpuid_t)cpuid;
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 		dprintf((stderr, "cpu_cm_info: cpuid=%d, rsrcname=%s",
8627c478bd9Sstevel@tonic-gate 		    cpuid, rsrcname));
8637c478bd9Sstevel@tonic-gate 	}
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	return (0);
8667c478bd9Sstevel@tonic-gate }
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate /*
8697c478bd9Sstevel@tonic-gate  * No RCM resource names for individual memory units, so
8707c478bd9Sstevel@tonic-gate  * just add to offline query page count.
8717c478bd9Sstevel@tonic-gate  */
8727c478bd9Sstevel@tonic-gate static int
mem_rcm_qpass(cfga_list_data_t * cfga,rcmd_t * rcm)8737c478bd9Sstevel@tonic-gate mem_rcm_qpass(cfga_list_data_t *cfga, rcmd_t *rcm)
8747c478bd9Sstevel@tonic-gate {
8757c478bd9Sstevel@tonic-gate 	char		*cp;
8767c478bd9Sstevel@tonic-gate 	uint_t		kbytes;
8777c478bd9Sstevel@tonic-gate 	longlong_t	ii;
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	if ((cp = strstr(cfga->ap_info, "size")) == NULL ||
8807c478bd9Sstevel@tonic-gate 	    sscanf(cp, "size=%u", &kbytes) != 1) {
8817c478bd9Sstevel@tonic-gate 		dprintf((stderr, "unknown sbd info format: %s\n", cp));
8827c478bd9Sstevel@tonic-gate 		return (-1);
8837c478bd9Sstevel@tonic-gate 	}
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	ii = (longlong_t)kbytes * KBYTE;
8867c478bd9Sstevel@tonic-gate 	rcm->query_pages += (uint_t)(ii / rcm->ms_pagesize);
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	dprintf((stderr, "%s: npages=%u\n", cfga->ap_log_id,
8897c478bd9Sstevel@tonic-gate 	    (uint_t)(ii / rcm->ms_pagesize)));
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	return (0);
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate /*
8957c478bd9Sstevel@tonic-gate  * Add physical I/O bus name to RCM resource list.
8967c478bd9Sstevel@tonic-gate  */
8977c478bd9Sstevel@tonic-gate static int
io_rcm_qpass(cfga_list_data_t * cfga,rcmd_t * rcm)8987c478bd9Sstevel@tonic-gate io_rcm_qpass(cfga_list_data_t *cfga, rcmd_t *rcm)
8997c478bd9Sstevel@tonic-gate {
9007c478bd9Sstevel@tonic-gate 	char		path[MAXPATHLEN];
9017c478bd9Sstevel@tonic-gate 	char		buf[MAXPATHLEN];
9027c478bd9Sstevel@tonic-gate 	char		*rsrcname;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	if (sscanf(cfga->ap_info, "device=%s", path) != 1) {
9057c478bd9Sstevel@tonic-gate 		dprintf((stderr, "unknown sbd info format: %s\n",
9067c478bd9Sstevel@tonic-gate 		    cfga->ap_info));
9077c478bd9Sstevel@tonic-gate 		return (-1);
9087c478bd9Sstevel@tonic-gate 	}
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "/devices%s", path);
9117c478bd9Sstevel@tonic-gate 	if ((rsrcname = strdup(buf)) == NULL) {
9127c478bd9Sstevel@tonic-gate 		dprintf((stderr, "strdup fail\n"));
9137c478bd9Sstevel@tonic-gate 		return (-1);
9147c478bd9Sstevel@tonic-gate 	}
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	assert(rcm->nrlist < rcm->ndevs);
9177c478bd9Sstevel@tonic-gate 	rcm->rlist[rcm->nrlist++] = rsrcname;
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	return (0);
9207c478bd9Sstevel@tonic-gate }
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate static int
cm_info_func(cfga_type_t ap_type)9237c478bd9Sstevel@tonic-gate (*cm_info_func(cfga_type_t ap_type))(ri_ap_t *, cfga_list_data_t *,
9247c478bd9Sstevel@tonic-gate     int, rcmd_t *)
9257c478bd9Sstevel@tonic-gate {
9267c478bd9Sstevel@tonic-gate 	int i;
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (cm_ctl) / sizeof (cm_ctl[0]); i++) {
9297c478bd9Sstevel@tonic-gate 		if (strcmp(cm_ctl[i].type, ap_type) == 0) {
9307c478bd9Sstevel@tonic-gate 			return (cm_ctl[i].cm_info);
9317c478bd9Sstevel@tonic-gate 		}
9327c478bd9Sstevel@tonic-gate 	}
9337c478bd9Sstevel@tonic-gate 	return (NULL);
9347c478bd9Sstevel@tonic-gate }
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate /*
9377c478bd9Sstevel@tonic-gate  * Create cpu handle, adding properties exported by sbd plugin and
9387c478bd9Sstevel@tonic-gate  * RCM client usage.
9397c478bd9Sstevel@tonic-gate  */
9407c478bd9Sstevel@tonic-gate /* ARGSUSED */
9417c478bd9Sstevel@tonic-gate static int
cpu_cm_info(ri_ap_t * ap,cfga_list_data_t * cfga,int flags,rcmd_t * rcm)9427c478bd9Sstevel@tonic-gate cpu_cm_info(ri_ap_t *ap, cfga_list_data_t *cfga, int flags, rcmd_t *rcm)
9437c478bd9Sstevel@tonic-gate {
9447c478bd9Sstevel@tonic-gate 	processorid_t	cpuid;
9457c478bd9Sstevel@tonic-gate 	int		speed, ecache, rv = 0;
9467c478bd9Sstevel@tonic-gate 	char		buf[CFGA_INFO_LEN], *cpustr, *lasts;
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	if (sscanf(cfga->ap_info, CPU_INFO_FMT, &buf, &speed, &ecache) != 3) {
9497c478bd9Sstevel@tonic-gate 		dprintf((stderr, "unknown sbd info format: %s\n",
9507c478bd9Sstevel@tonic-gate 		    cfga->ap_info));
9517c478bd9Sstevel@tonic-gate 		return (-1);
9527c478bd9Sstevel@tonic-gate 	}
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	/* parse cpuids */
9557c478bd9Sstevel@tonic-gate 	for (cpustr = (char *)strtok_r(buf, CPUID_SEP, &lasts);
9567c478bd9Sstevel@tonic-gate 	    cpustr != NULL;
9577c478bd9Sstevel@tonic-gate 	    cpustr = (char *)strtok_r(NULL, CPUID_SEP, &lasts)) {
9587c478bd9Sstevel@tonic-gate 		cpuid = atoi(cpustr);
95961ddd3aaSscarter 		if ((rv = i_cpu_cm_info(cpuid, speed, ecache, ap, rcm)) != 0) {
9607c478bd9Sstevel@tonic-gate 			break;
9617c478bd9Sstevel@tonic-gate 		}
9627c478bd9Sstevel@tonic-gate 	}
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	return (rv);
9657c478bd9Sstevel@tonic-gate }
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate static int
i_cpu_cm_info(processorid_t cpuid,int speed,int ecache_cfga,ri_ap_t * ap,rcmd_t * rcm)96861ddd3aaSscarter i_cpu_cm_info(processorid_t cpuid, int speed, int ecache_cfga, ri_ap_t *ap,
96961ddd3aaSscarter     rcmd_t *rcm)
9707c478bd9Sstevel@tonic-gate {
971fc0105deSJames Anderson 	int		ecache_mb = 0;
972fc0105deSJames Anderson 	int		ecache_kb = 0;
9737c478bd9Sstevel@tonic-gate 	char		*state, buf[32];
9747c478bd9Sstevel@tonic-gate 	processor_info_t cpu_info;
9757c478bd9Sstevel@tonic-gate 	ri_dev_t	*cpu = NULL;
9767c478bd9Sstevel@tonic-gate 	rcm_info_t	*rcm_info = NULL;
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	/*
9797c478bd9Sstevel@tonic-gate 	 * Could have been unconfigured in the interim, so cannot
9807c478bd9Sstevel@tonic-gate 	 * count on processor_info recognizing it.
9817c478bd9Sstevel@tonic-gate 	 */
9827c478bd9Sstevel@tonic-gate 	state = (processor_info(cpuid, &cpu_info) == 0) ?
9837c478bd9Sstevel@tonic-gate 	    pstate2str(cpu_info.pi_state) : "unknown";
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	if ((cpu = ri_dev_alloc()) == NULL) {
9867c478bd9Sstevel@tonic-gate 		dprintf((stderr, "ri_dev_alloc failed\n"));
9877c478bd9Sstevel@tonic-gate 		return (-1);
9887c478bd9Sstevel@tonic-gate 	}
9897c478bd9Sstevel@tonic-gate 
99061ddd3aaSscarter 	/*
99161ddd3aaSscarter 	 * Assume the ecache_info table has the right e-cache size for
99261ddd3aaSscarter 	 * this CPU.  Use the value found in cfgadm (ecache_cfga) if not.
99361ddd3aaSscarter 	 */
9947c478bd9Sstevel@tonic-gate 	if (rcm->ecache_info.ecache_sizes != NULL) {
9957c478bd9Sstevel@tonic-gate 		assert(rcm->ecache_info.cpuid_max != 0 &&
9967c478bd9Sstevel@tonic-gate 		    cpuid <= rcm->ecache_info.cpuid_max);
997fc0105deSJames Anderson 		ecache_mb = rcm->ecache_info.ecache_sizes[cpuid] / MBYTE;
998fc0105deSJames Anderson 		ecache_kb = rcm->ecache_info.ecache_sizes[cpuid] / KBYTE;
9997c478bd9Sstevel@tonic-gate 	}
1000fc0105deSJames Anderson 
1001fc0105deSJames Anderson 	if (ecache_mb == 0) {
1002fc0105deSJames Anderson 		ecache_mb = ecache_cfga;
100361ddd3aaSscarter 	}
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	dprintf((stderr, "i_cpu_cm_info: cpu(%d) ecache=%d MB\n",
10067c478bd9Sstevel@tonic-gate 	    cpuid, ecache));
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	if (nvlist_add_int32(cpu->conf_props, RI_CPU_ID, cpuid) != 0 ||
10097c478bd9Sstevel@tonic-gate 	    nvlist_add_int32(cpu->conf_props, RI_CPU_SPEED, speed) != 0 ||
1010fc0105deSJames Anderson 	    nvlist_add_int32(cpu->conf_props, RI_CPU_ECACHE, ecache_mb) != 0 ||
10117c478bd9Sstevel@tonic-gate 	    nvlist_add_string(cpu->conf_props, RI_CPU_STATE, state) != 0) {
10127c478bd9Sstevel@tonic-gate 		dprintf((stderr, "nvlist_add fail\n"));
10137c478bd9Sstevel@tonic-gate 		ri_dev_free(cpu);
10147c478bd9Sstevel@tonic-gate 		return (-1);
10157c478bd9Sstevel@tonic-gate 	}
10167c478bd9Sstevel@tonic-gate 
1017fc0105deSJames Anderson 	/*
1018fc0105deSJames Anderson 	 * Report cache size in kilobyte units if available.  This info is
1019fc0105deSJames Anderson 	 * added to support processors with cache sizes that are non-integer
1020fc0105deSJames Anderson 	 * megabyte multiples.
1021fc0105deSJames Anderson 	 */
1022fc0105deSJames Anderson 	if (ecache_kb != 0) {
1023fc0105deSJames Anderson 		if (nvlist_add_int32(cpu->conf_props, RI_CPU_ECACHE_KBYTE,
1024fc0105deSJames Anderson 		    ecache_kb) != 0)  {
1025fc0105deSJames Anderson 			dprintf((stderr, "nvlist_add fail: %s\n",
1026fc0105deSJames Anderson 			    RI_CPU_ECACHE_KBYTE));
1027fc0105deSJames Anderson 			ri_dev_free(cpu);
1028fc0105deSJames Anderson 			return (-1);
1029fc0105deSJames Anderson 		}
1030fc0105deSJames Anderson 	}
1031fc0105deSJames Anderson 
10327c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%s%d", RCM_CPU, cpuid);
10337c478bd9Sstevel@tonic-gate 	dprintf((stderr, "rcm_get_info(%s)\n", buf));
10347c478bd9Sstevel@tonic-gate 	if (rcm_get_info(rcm->hdl, buf, RCM_INCLUDE_DEPENDENT,
10357c478bd9Sstevel@tonic-gate 	    &rcm_info) != RCM_SUCCESS) {
10367c478bd9Sstevel@tonic-gate 		dprintf((stderr, "rcm_get_info (errno=%d)\n", errno));
10377c478bd9Sstevel@tonic-gate 		ri_dev_free(cpu);
10387c478bd9Sstevel@tonic-gate 		if (rcm_info != NULL)
10397c478bd9Sstevel@tonic-gate 			rcm_free_info(rcm_info);
10407c478bd9Sstevel@tonic-gate 		return (-1);
10417c478bd9Sstevel@tonic-gate 	}
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	dev_list_cpu_insert(&ap->cpus, cpu, cpuid);
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	return (0);
10467c478bd9Sstevel@tonic-gate }
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate /*
10497c478bd9Sstevel@tonic-gate  * Create memory handle, adding properties exported by sbd plugin.
10507c478bd9Sstevel@tonic-gate  * No RCM tuples to be saved unless RCM is modified to export names
10517c478bd9Sstevel@tonic-gate  * for individual memory units.
10527c478bd9Sstevel@tonic-gate  */
10537c478bd9Sstevel@tonic-gate /* ARGSUSED */
10547c478bd9Sstevel@tonic-gate static int
mem_cm_info(ri_ap_t * ap,cfga_list_data_t * cfga,int flags,rcmd_t * rcm)10557c478bd9Sstevel@tonic-gate mem_cm_info(ri_ap_t *ap, cfga_list_data_t *cfga, int flags, rcmd_t *rcm)
10567c478bd9Sstevel@tonic-gate {
10577c478bd9Sstevel@tonic-gate 	ri_dev_t	*mem;
10587c478bd9Sstevel@tonic-gate 	char		*cp;
1059c2623971Sscarter 	char		*cpval;
1060c2623971Sscarter 	int		len;
1061c2623971Sscarter 	uint64_t	base_addr;				/* required */
1062c2623971Sscarter 	int32_t		size_kb;				/* required */
1063c2623971Sscarter 	int32_t		perm_kb = 0;				/* optional */
1064c2623971Sscarter 	char		target[CFGA_AP_LOG_ID_LEN] = "";	/* optional */
1065c2623971Sscarter 	int32_t		del_kb = 0;				/* optional */
1066c2623971Sscarter 	int32_t		rem_kb = 0;				/* optional */
1067c2623971Sscarter 	char		source[CFGA_AP_LOG_ID_LEN] = "";	/* optional */
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	if (sscanf(cfga->ap_info, "address=0x%llx size=%u", &base_addr,
10707c478bd9Sstevel@tonic-gate 	    &size_kb) != 2) {
10717c478bd9Sstevel@tonic-gate 		goto err_fmt;
10727c478bd9Sstevel@tonic-gate 	}
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	if ((cp = strstr(cfga->ap_info, "permanent")) != NULL &&
10757c478bd9Sstevel@tonic-gate 	    sscanf(cp, "permanent=%u", &perm_kb) != 1) {
10767c478bd9Sstevel@tonic-gate 		goto err_fmt;
10777c478bd9Sstevel@tonic-gate 	}
10787c478bd9Sstevel@tonic-gate 
1079c2623971Sscarter 	if ((cp = strstr(cfga->ap_info, "target")) != NULL) {
1080c2623971Sscarter 		if ((cpval = strstr(cp, "=")) == NULL) {
1081c2623971Sscarter 			goto err_fmt;
1082c2623971Sscarter 		}
1083c2623971Sscarter 		for (len = 0; cpval[len] != '\0' && cpval[len] != ' '; len++) {
1084c2623971Sscarter 			if (len >= CFGA_AP_LOG_ID_LEN) {
1085c2623971Sscarter 				goto err_fmt;
1086c2623971Sscarter 			}
1087c2623971Sscarter 		}
1088c2623971Sscarter 		if (sscanf(cp, "target=%s deleted=%u remaining=%u", &target,
1089c2623971Sscarter 		    &del_kb, &rem_kb) != 3) {
1090c2623971Sscarter 			goto err_fmt;
1091c2623971Sscarter 		}
10927c478bd9Sstevel@tonic-gate 	}
10937c478bd9Sstevel@tonic-gate 
1094c2623971Sscarter 	if ((cp = strstr(cfga->ap_info, "source")) != NULL) {
1095c2623971Sscarter 		if ((cpval = strstr(cp, "=")) == NULL) {
1096c2623971Sscarter 			goto err_fmt;
1097c2623971Sscarter 		}
1098c2623971Sscarter 		for (len = 0; cpval[len] != '\0' && cpval[len] != ' '; len++) {
1099c2623971Sscarter 			if (len >= CFGA_AP_LOG_ID_LEN) {
1100c2623971Sscarter 				goto err_fmt;
1101c2623971Sscarter 			}
1102c2623971Sscarter 		}
1103c2623971Sscarter 		if (sscanf(cp, "source=%s", &source) != 1) {
1104c2623971Sscarter 			goto err_fmt;
1105c2623971Sscarter 		}
11067c478bd9Sstevel@tonic-gate 	}
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	dprintf((stderr, "%s: base=0x%llx, size=%u, permanent=%u\n",
11097c478bd9Sstevel@tonic-gate 	    cfga->ap_log_id, base_addr, size_kb, perm_kb));
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	if ((mem = ri_dev_alloc()) == NULL)
11127c478bd9Sstevel@tonic-gate 		return (-1);
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	/*
11157c478bd9Sstevel@tonic-gate 	 * Convert memory sizes to MB (truncate).
11167c478bd9Sstevel@tonic-gate 	 */
11177c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint64(mem->conf_props, RI_MEM_ADDR, base_addr) != 0 ||
11187c478bd9Sstevel@tonic-gate 	    nvlist_add_int32(mem->conf_props, RI_MEM_BRD, size_kb/KBYTE) != 0 ||
11197c478bd9Sstevel@tonic-gate 	    nvlist_add_int32(mem->conf_props, RI_MEM_PERM,
11207c478bd9Sstevel@tonic-gate 	    perm_kb/KBYTE) != 0) {
11217c478bd9Sstevel@tonic-gate 		dprintf((stderr, "nvlist_add failure\n"));
11227c478bd9Sstevel@tonic-gate 		ri_dev_free(mem);
11237c478bd9Sstevel@tonic-gate 		return (-1);
11247c478bd9Sstevel@tonic-gate 	}
11257c478bd9Sstevel@tonic-gate 
1126c2623971Sscarter 	if (target[0] != '\0' &&
11277c478bd9Sstevel@tonic-gate 	    (nvlist_add_string(mem->conf_props, RI_MEM_TARG, target) != 0 ||
11287c478bd9Sstevel@tonic-gate 	    nvlist_add_int32(mem->conf_props, RI_MEM_DEL, del_kb/KBYTE) != 0 ||
11297c478bd9Sstevel@tonic-gate 	    nvlist_add_int32(mem->conf_props, RI_MEM_REMAIN,
11307c478bd9Sstevel@tonic-gate 	    rem_kb/KBYTE) != 0)) {
11317c478bd9Sstevel@tonic-gate 		dprintf((stderr, "nvlist_add failure\n"));
11327c478bd9Sstevel@tonic-gate 		ri_dev_free(mem);
11337c478bd9Sstevel@tonic-gate 		return (-1);
11347c478bd9Sstevel@tonic-gate 	}
11357c478bd9Sstevel@tonic-gate 
1136c2623971Sscarter 	if (source[0] != '\0' &&
1137c2623971Sscarter 	    nvlist_add_string(mem->conf_props, RI_MEM_SRC, source) != 0) {
11387c478bd9Sstevel@tonic-gate 		dprintf((stderr, "nvlist_add failure\n"));
11397c478bd9Sstevel@tonic-gate 		ri_dev_free(mem);
11407c478bd9Sstevel@tonic-gate 		return (-1);
11417c478bd9Sstevel@tonic-gate 	}
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	/*
11447c478bd9Sstevel@tonic-gate 	 * XXX - move this property to attachment point hdl?
11457c478bd9Sstevel@tonic-gate 	 */
11467c478bd9Sstevel@tonic-gate 	if (nvlist_add_int32(mem->conf_props, RI_MEM_DOMAIN,
11477c478bd9Sstevel@tonic-gate 	    rcm->ms_sysmb) != 0) {
11487c478bd9Sstevel@tonic-gate 		dprintf((stderr, "nvlist_add failure\n"));
11497c478bd9Sstevel@tonic-gate 		ri_dev_free(mem);
11507c478bd9Sstevel@tonic-gate 		return (-1);
11517c478bd9