xref: /illumos-gate/usr/src/lib/cfgadm_plugins/sbd/common/ap_rcm.c (revision aab83bb83be7342f6cfccaed8d5fe0b2f404855d)
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
5d62bc4baSyz147064  * Common Development and Distribution License (the "License").
6d62bc4baSyz147064  * 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 /*
22d62bc4baSyz147064  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <ctype.h>
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <stdarg.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <macros.h>
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate #include <kstat.h>
357c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
367c478bd9Sstevel@tonic-gate #include <dlfcn.h>
377c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
387c478bd9Sstevel@tonic-gate #include <librcm.h>
397c478bd9Sstevel@tonic-gate #include <libintl.h>
407c478bd9Sstevel@tonic-gate #define	CFGA_PLUGIN_LIB
417c478bd9Sstevel@tonic-gate #include <config_admin.h>
427c478bd9Sstevel@tonic-gate #include <sys/sbd_ioctl.h>
437c478bd9Sstevel@tonic-gate #include "ap.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate typedef int32_t	cpuid_t;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate typedef struct {
487c478bd9Sstevel@tonic-gate 	int valid;
497c478bd9Sstevel@tonic-gate 	cfga_stat_t ostate;
507c478bd9Sstevel@tonic-gate 	int ncap;
517c478bd9Sstevel@tonic-gate 	union {
527c478bd9Sstevel@tonic-gate 		long npages;
537c478bd9Sstevel@tonic-gate 		cpuid_t cpuid[SBD_MAX_CORES_PER_CMP];
547c478bd9Sstevel@tonic-gate 	} type;
557c478bd9Sstevel@tonic-gate } cap_info_t;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate typedef struct {
587c478bd9Sstevel@tonic-gate 	int firstcm;		/* first component to operate on */
597c478bd9Sstevel@tonic-gate 	int lastcm;		/* last component to operate on */
607c478bd9Sstevel@tonic-gate 	void *lib;
617c478bd9Sstevel@tonic-gate 	char **rlist;
627c478bd9Sstevel@tonic-gate 	cap_info_t *capinfo;
637c478bd9Sstevel@tonic-gate 	int ncpus;		/* # of CPUs in cpuids list */
647c478bd9Sstevel@tonic-gate 	cpuid_t *cpuids;	/* List of cpuids */
657c478bd9Sstevel@tonic-gate 	int capcpus;		/* # of CPUs - tracking capacity */
667c478bd9Sstevel@tonic-gate 	int cappages;		/* # of memory pages - tracking capacity */
677c478bd9Sstevel@tonic-gate 	rcm_handle_t *hd;
687c478bd9Sstevel@tonic-gate 	rcm_info_t *rinfo;
697c478bd9Sstevel@tonic-gate 	rcm_info_tuple_t *infot;
707c478bd9Sstevel@tonic-gate 	int (*alloc_handle)(char *, uint_t, void *, rcm_handle_t **);
717c478bd9Sstevel@tonic-gate 	void (*free_handle)(rcm_handle_t *);
727c478bd9Sstevel@tonic-gate 	int (*get_info)(rcm_handle_t *, char *, uint_t, rcm_info_t **);
737c478bd9Sstevel@tonic-gate 	void (*free_info)(rcm_info_t *);
747c478bd9Sstevel@tonic-gate 	rcm_info_tuple_t *(*info_next)(rcm_info_t *, rcm_info_tuple_t *);
757c478bd9Sstevel@tonic-gate 	int (*info_state)(rcm_info_tuple_t *);
767c478bd9Sstevel@tonic-gate 	pid_t (*info_pid)(rcm_info_tuple_t *);
777c478bd9Sstevel@tonic-gate 	const char *(*info_error)(rcm_info_tuple_t *);
787c478bd9Sstevel@tonic-gate 	const char *(*info_info)(rcm_info_tuple_t *);
797c478bd9Sstevel@tonic-gate 	const char *(*info_rsrc)(rcm_info_tuple_t *);
807c478bd9Sstevel@tonic-gate 	int (*request_offline_list)(rcm_handle_t *, char **, uint_t,
817c478bd9Sstevel@tonic-gate 	    rcm_info_t **);
827c478bd9Sstevel@tonic-gate 	int (*notify_online_list)(rcm_handle_t *, char **, uint_t,
837c478bd9Sstevel@tonic-gate 	    rcm_info_t **);
847c478bd9Sstevel@tonic-gate 	int (*request_suspend)(rcm_handle_t *, char *, uint_t, timespec_t *,
857c478bd9Sstevel@tonic-gate 		rcm_info_t **);
867c478bd9Sstevel@tonic-gate 	int (*notify_resume)(rcm_handle_t *, char *, uint_t, rcm_info_t **);
877c478bd9Sstevel@tonic-gate 	int (*notify_remove_list)(rcm_handle_t *, char **, uint_t,
887c478bd9Sstevel@tonic-gate 	    rcm_info_t **);
897c478bd9Sstevel@tonic-gate 	int (*request_capacity_change)(rcm_handle_t *, char *, uint_t,
907c478bd9Sstevel@tonic-gate 		nvlist_t *, rcm_info_t **);
917c478bd9Sstevel@tonic-gate 	int (*notify_capacity_change)(rcm_handle_t *, char *, uint_t,
927c478bd9Sstevel@tonic-gate 		nvlist_t *, rcm_info_t **);
937c478bd9Sstevel@tonic-gate } rcmd_t;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static char *
967c478bd9Sstevel@tonic-gate ap_rcm_ops[] = {
977c478bd9Sstevel@tonic-gate 	"rcm_alloc_handle",
987c478bd9Sstevel@tonic-gate 	"rcm_free_handle",
997c478bd9Sstevel@tonic-gate 	"rcm_get_info",
1007c478bd9Sstevel@tonic-gate 	"rcm_free_info",
1017c478bd9Sstevel@tonic-gate 	"rcm_info_next",
1027c478bd9Sstevel@tonic-gate 	"rcm_info_state",
1037c478bd9Sstevel@tonic-gate 	"rcm_info_pid",
1047c478bd9Sstevel@tonic-gate 	"rcm_info_error",
1057c478bd9Sstevel@tonic-gate 	"rcm_info_info",
1067c478bd9Sstevel@tonic-gate 	"rcm_info_rsrc",
1077c478bd9Sstevel@tonic-gate 	"rcm_request_offline_list",
1087c478bd9Sstevel@tonic-gate 	"rcm_notify_online_list",
1097c478bd9Sstevel@tonic-gate 	"rcm_request_suspend",
1107c478bd9Sstevel@tonic-gate 	"rcm_notify_resume",
1117c478bd9Sstevel@tonic-gate 	"rcm_notify_remove_list",
1127c478bd9Sstevel@tonic-gate 	"rcm_request_capacity_change",
1137c478bd9Sstevel@tonic-gate 	"rcm_notify_capacity_change",
1147c478bd9Sstevel@tonic-gate 	NULL
1157c478bd9Sstevel@tonic-gate };
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate #define	ALLOC_HANDLE		0
1187c478bd9Sstevel@tonic-gate #define	FREE_HANDLE		1
1197c478bd9Sstevel@tonic-gate #define	GET_INFO		2
1207c478bd9Sstevel@tonic-gate #define	FREE_INFO		3
1217c478bd9Sstevel@tonic-gate #define	INFO_TUPLE_NEXT		4
1227c478bd9Sstevel@tonic-gate #define	INFO_TUPLE_STATE	5
1237c478bd9Sstevel@tonic-gate #define	INFO_TUPLE_ID		6
1247c478bd9Sstevel@tonic-gate #define	INFO_TUPLE_ERROR	7
1257c478bd9Sstevel@tonic-gate #define	INFO_TUPLE_INFO		8
1267c478bd9Sstevel@tonic-gate #define	INFO_TUPLE_RSRC		9
1277c478bd9Sstevel@tonic-gate #define	REQUEST_OFFLINE		10
1287c478bd9Sstevel@tonic-gate #define	NOTIFY_ONLINE		11
1297c478bd9Sstevel@tonic-gate #define	REQUEST_SUSPEND		12
1307c478bd9Sstevel@tonic-gate #define	NOTIFY_RESUME		13
1317c478bd9Sstevel@tonic-gate #define	NOTIFY_REMOVE		14
1327c478bd9Sstevel@tonic-gate #define	REQUEST_CAP_CHANGE	15
1337c478bd9Sstevel@tonic-gate #define	NOTIFY_CAP_CHANGE	16
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate /*
1367c478bd9Sstevel@tonic-gate  * There is no consumer for SUNW_OS. This is defined here
1377c478bd9Sstevel@tonic-gate  * for generic OS quiescence.
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate #define	OS	"SUNW_OS"	/* XXX */
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /* Max width of an RCM formatted message line */
1427c478bd9Sstevel@tonic-gate #define	RCM_MAX_FORMAT	80
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate #ifdef	__sparcv9
145d62bc4baSyz147064 #define	RCMLIB	"/lib/sparcv9/librcm.so";
146*a3114836SGerry Liu #elif defined(__amd64)
147*a3114836SGerry Liu #define	RCMLIB	"/lib/amd64/librcm.so";
1487c478bd9Sstevel@tonic-gate #else
149d62bc4baSyz147064 #define	RCMLIB	"/lib/librcm.so";
1507c478bd9Sstevel@tonic-gate #endif
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate static cfga_err_t
ap_capinfo(apd_t * a,int firstcm,int lastcm,cap_info_t ** capinfo)1537c478bd9Sstevel@tonic-gate ap_capinfo(apd_t *a, int firstcm, int lastcm, cap_info_t **capinfo)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	int cm;
1567c478bd9Sstevel@tonic-gate 	int ncm;
1577c478bd9Sstevel@tonic-gate 	void *cap;
1587c478bd9Sstevel@tonic-gate 	int *ncap;
1597c478bd9Sstevel@tonic-gate 	cfga_stat_t *os;
1607c478bd9Sstevel@tonic-gate 	cap_info_t *cinfo, *cp;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	DBG("ap_capinfo(%p)\n", (void *)a);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (capinfo == NULL) {
1657c478bd9Sstevel@tonic-gate 		ap_err(a, ERR_PLUGIN, "null capinfo");
1667c478bd9Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/*
1707c478bd9Sstevel@tonic-gate 	 * Assume there are components with valid capacity
1717c478bd9Sstevel@tonic-gate 	 * information and allocate space for them.  If there
1727c478bd9Sstevel@tonic-gate 	 * are none at the end, free the allocated space.
1737c478bd9Sstevel@tonic-gate 	 */
1747c478bd9Sstevel@tonic-gate 	ncm = lastcm - firstcm + 1;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	cinfo = (cap_info_t *)calloc(ncm, sizeof (cap_info_t));
1777c478bd9Sstevel@tonic-gate 	if (cinfo == NULL) {
1787c478bd9Sstevel@tonic-gate 		ap_err(a, ERR_NOMEM);
1797c478bd9Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	*capinfo = NULL;
1837c478bd9Sstevel@tonic-gate 	ncm = 0;
1847c478bd9Sstevel@tonic-gate 	for (cp = cinfo, cm = firstcm; cm <= lastcm; cm++, cp++) {
1857c478bd9Sstevel@tonic-gate 		os = &cp->ostate;
1867c478bd9Sstevel@tonic-gate 		ncap = &cp->ncap;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 		switch (ap_cm_type(a, cm)) {
1897c478bd9Sstevel@tonic-gate 		case AP_CPU:
1907c478bd9Sstevel@tonic-gate 		case AP_CMP:
1917c478bd9Sstevel@tonic-gate 			cap = (void *)(cp->type.cpuid);
1927c478bd9Sstevel@tonic-gate 			break;
1937c478bd9Sstevel@tonic-gate 		case AP_MEM:
1947c478bd9Sstevel@tonic-gate 			cap = (void *)&(cp->type.npages);
1957c478bd9Sstevel@tonic-gate 			break;
1967c478bd9Sstevel@tonic-gate 		default:
1977c478bd9Sstevel@tonic-gate 			continue;
1987c478bd9Sstevel@tonic-gate 		}
1997c478bd9Sstevel@tonic-gate 		/*
2007c478bd9Sstevel@tonic-gate 		 * Remember which components have valid
2017c478bd9Sstevel@tonic-gate 		 * capacity information.
2027c478bd9Sstevel@tonic-gate 		 */
2037c478bd9Sstevel@tonic-gate 		if (ap_cm_capacity(a, cm, cap, ncap, os)) {
2047c478bd9Sstevel@tonic-gate 			cp->valid = 1;
2057c478bd9Sstevel@tonic-gate 			ncm++;
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	if (ncm == 0)
2107c478bd9Sstevel@tonic-gate 		free(cinfo);
2117c478bd9Sstevel@tonic-gate 	else
2127c478bd9Sstevel@tonic-gate 		*capinfo = cinfo;
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	return (CFGA_OK);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate static int
getsyscpuids(int * ncpuids,cpuid_t ** cpuids)2187c478bd9Sstevel@tonic-gate getsyscpuids(int *ncpuids, cpuid_t **cpuids)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	int		ncpu;
2217c478bd9Sstevel@tonic-gate 	int		maxncpu;
2227c478bd9Sstevel@tonic-gate 	kstat_t		*ksp;
2237c478bd9Sstevel@tonic-gate 	kstat_ctl_t	*kc = NULL;
2247c478bd9Sstevel@tonic-gate 	cpuid_t		*cp;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	DBG("getsyscpuids\n");
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	if ((maxncpu = sysconf(_SC_NPROCESSORS_MAX)) == -1 ||
2297c478bd9Sstevel@tonic-gate 	    (kc = kstat_open()) == NULL ||
2307c478bd9Sstevel@tonic-gate 	    (cp = (cpuid_t *)calloc(maxncpu, sizeof (cpuid_t))) == NULL) {
2317c478bd9Sstevel@tonic-gate 		/* if calloc failed, clean up kstats */
2327c478bd9Sstevel@tonic-gate 		if (kc != NULL) {
2337c478bd9Sstevel@tonic-gate 			(void) kstat_close(kc);
2347c478bd9Sstevel@tonic-gate 		}
2357c478bd9Sstevel@tonic-gate 		return (-1);
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	DBG("syscpuids: ");
2397c478bd9Sstevel@tonic-gate 	for (ncpu = 0, ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
2407c478bd9Sstevel@tonic-gate 		if (strcmp(ksp->ks_module, "cpu_info") == 0) {
2417c478bd9Sstevel@tonic-gate 			cp[ncpu++] = ksp->ks_instance;
2427c478bd9Sstevel@tonic-gate 			DBG("%d ", ksp->ks_instance);
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 	DBG("\n");
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	(void) kstat_close(kc);
2487c478bd9Sstevel@tonic-gate 	*cpuids = cp;
2497c478bd9Sstevel@tonic-gate 	*ncpuids = ncpu;
2507c478bd9Sstevel@tonic-gate 	return (0);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate cfga_err_t
ap_rcm_init(apd_t * a)2547c478bd9Sstevel@tonic-gate ap_rcm_init(apd_t *a)
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate 	int i;
2577c478bd9Sstevel@tonic-gate 	char *err;
2587c478bd9Sstevel@tonic-gate 	char *rcmlib;
2597c478bd9Sstevel@tonic-gate 	void *sym;
2607c478bd9Sstevel@tonic-gate 	void *lib;
2617c478bd9Sstevel@tonic-gate 	char **op;
2627c478bd9Sstevel@tonic-gate 	rcmd_t *rcm;
2637c478bd9Sstevel@tonic-gate 	cfga_err_t rc;
2647c478bd9Sstevel@tonic-gate 	struct stat buf;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	DBG("ap_rcm_init(%p)\n", (void *)a);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	/*
2697c478bd9Sstevel@tonic-gate 	 * If the initial command is status, or the RCM feature is not
2707c478bd9Sstevel@tonic-gate 	 * available, or the RCM interface has already been initialized,
2717c478bd9Sstevel@tonic-gate 	 * just return.
2727c478bd9Sstevel@tonic-gate 	 */
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	if ((a->statonly != 0) || (a->norcm != 0) ||
2757c478bd9Sstevel@tonic-gate 	    ((rcm = (rcmd_t *)a->rcm) != NULL)) {
2767c478bd9Sstevel@tonic-gate 		return (CFGA_OK);
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	rcmlib = RCMLIB;
2807c478bd9Sstevel@tonic-gate 	rc = CFGA_LIB_ERROR;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	DBG("Looking for %s\n", rcmlib);
2837c478bd9Sstevel@tonic-gate 	/*
2847c478bd9Sstevel@tonic-gate 	 * If the library is not present, there is nothing more
2857c478bd9Sstevel@tonic-gate 	 * to do.  The RCM offline/suspend steps become no-ops
2867c478bd9Sstevel@tonic-gate 	 * in that case.
2877c478bd9Sstevel@tonic-gate 	 */
2887c478bd9Sstevel@tonic-gate 	if (stat(rcmlib, &buf) == -1) {
2897c478bd9Sstevel@tonic-gate 		if (errno == ENOENT) {
2907c478bd9Sstevel@tonic-gate 			a->norcm++;
2917c478bd9Sstevel@tonic-gate 			ap_msg(a, MSG_NORCM);
2927c478bd9Sstevel@tonic-gate 			return (CFGA_OK);
2937c478bd9Sstevel@tonic-gate 		} else {
2947c478bd9Sstevel@tonic-gate 			ap_err(a, ERR_STAT, rcmlib);
2957c478bd9Sstevel@tonic-gate 			return (rc);
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 	DBG("%s found\n", rcmlib);
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	if ((a->rcm = calloc(1, sizeof (rcmd_t))) == NULL) {
3017c478bd9Sstevel@tonic-gate 		ap_err(a, ERR_NOMEM);
3027c478bd9Sstevel@tonic-gate 		return (rc);
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	rcm = (rcmd_t *)a->rcm;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	if ((lib = dlopen(rcmlib, RTLD_NOW)) == NULL) {
3087c478bd9Sstevel@tonic-gate 		if ((err = dlerror()) != NULL)
3097c478bd9Sstevel@tonic-gate 			err = strdup(err);
3107c478bd9Sstevel@tonic-gate 		ap_err(a, ERR_LIB_OPEN, rcmlib, err);
3117c478bd9Sstevel@tonic-gate 		if (err != NULL)
3127c478bd9Sstevel@tonic-gate 			free(err);
3137c478bd9Sstevel@tonic-gate 		return (rc);
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	rcm->lib = lib;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	for (i = 0, op = ap_rcm_ops; *op != NULL; op++, i++) {
3197c478bd9Sstevel@tonic-gate 		if ((sym = dlsym(lib, *op)) == NULL) {
3207c478bd9Sstevel@tonic-gate 			ap_err(a, ERR_LIB_SYM, rcmlib, *op);
3217c478bd9Sstevel@tonic-gate 			return (rc);
3227c478bd9Sstevel@tonic-gate 		}
3237c478bd9Sstevel@tonic-gate 		switch (i) {
3247c478bd9Sstevel@tonic-gate 		case ALLOC_HANDLE:
3257c478bd9Sstevel@tonic-gate 			rcm->alloc_handle = (int(*)
3267c478bd9Sstevel@tonic-gate 			    (char *, uint_t, void *, rcm_handle_t **))sym;
3277c478bd9Sstevel@tonic-gate 			break;
3287c478bd9Sstevel@tonic-gate 		case FREE_HANDLE:
3297c478bd9Sstevel@tonic-gate 			rcm->free_handle = (void (*)(rcm_handle_t *))sym;
3307c478bd9Sstevel@tonic-gate 			break;
3317c478bd9Sstevel@tonic-gate 		case GET_INFO:
3327c478bd9Sstevel@tonic-gate 			rcm->get_info = (int (*)
333*a3114836SGerry Liu 			    (rcm_handle_t *, char *, uint_t, rcm_info_t **))sym;
3347c478bd9Sstevel@tonic-gate 			break;
3357c478bd9Sstevel@tonic-gate 		case FREE_INFO:
3367c478bd9Sstevel@tonic-gate 			rcm->free_info = (void (*)(rcm_info_t *))sym;
3377c478bd9Sstevel@tonic-gate 			break;
3387c478bd9Sstevel@tonic-gate 		case INFO_TUPLE_NEXT:
3397c478bd9Sstevel@tonic-gate 			rcm->info_next = (rcm_info_tuple_t *(*)
3407c478bd9Sstevel@tonic-gate 			    (rcm_info_t *, rcm_info_tuple_t *))sym;
3417c478bd9Sstevel@tonic-gate 			break;
3427c478bd9Sstevel@tonic-gate 		case INFO_TUPLE_STATE:
3437c478bd9Sstevel@tonic-gate 			rcm->info_state = (int (*)(rcm_info_tuple_t *))sym;
3447c478bd9Sstevel@tonic-gate 			break;
3457c478bd9Sstevel@tonic-gate 		case INFO_TUPLE_ID:
3467c478bd9Sstevel@tonic-gate 			rcm->info_pid = (pid_t (*)(rcm_info_tuple_t *))sym;
3477c478bd9Sstevel@tonic-gate 			break;
3487c478bd9Sstevel@tonic-gate 		case INFO_TUPLE_ERROR:
3497c478bd9Sstevel@tonic-gate 			rcm->info_error = (const char *(*)
3507c478bd9Sstevel@tonic-gate 			    (rcm_info_tuple_t *))sym;
3517c478bd9Sstevel@tonic-gate 			break;
3527c478bd9Sstevel@tonic-gate 		case INFO_TUPLE_INFO:
3537c478bd9Sstevel@tonic-gate 			rcm->info_info = (const char *(*)
3547c478bd9Sstevel@tonic-gate 			    (rcm_info_tuple_t *))sym;
3557c478bd9Sstevel@tonic-gate 			break;
3567c478bd9Sstevel@tonic-gate 		case INFO_TUPLE_RSRC:
3577c478bd9Sstevel@tonic-gate 			rcm->info_rsrc = (const char *(*)
3587c478bd9Sstevel@tonic-gate 			    (rcm_info_tuple_t *))sym;
3597c478bd9Sstevel@tonic-gate 			break;
3607c478bd9Sstevel@tonic-gate 		case REQUEST_OFFLINE:
3617c478bd9Sstevel@tonic-gate 			rcm->request_offline_list = (int (*)
3627c478bd9Sstevel@tonic-gate 			    (rcm_handle_t *, char **, uint_t,
3637c478bd9Sstevel@tonic-gate 			    rcm_info_t **))sym;
3647c478bd9Sstevel@tonic-gate 			break;
3657c478bd9Sstevel@tonic-gate 		case NOTIFY_ONLINE:
3667c478bd9Sstevel@tonic-gate 			rcm->notify_online_list = (int (*)
3677c478bd9Sstevel@tonic-gate 			    (rcm_handle_t *, char **, uint_t,
3687c478bd9Sstevel@tonic-gate 			    rcm_info_t **))sym;
3697c478bd9Sstevel@tonic-gate 			break;
3707c478bd9Sstevel@tonic-gate 		case REQUEST_SUSPEND:
3717c478bd9Sstevel@tonic-gate 			rcm->request_suspend = (int (*)
3727c478bd9Sstevel@tonic-gate 			    (rcm_handle_t *, char *, uint_t,
3737c478bd9Sstevel@tonic-gate 			    timespec_t *, rcm_info_t **))sym;
3747c478bd9Sstevel@tonic-gate 			break;
3757c478bd9Sstevel@tonic-gate 		case NOTIFY_RESUME:
3767c478bd9Sstevel@tonic-gate 			rcm->notify_resume = (int (*)
3777c478bd9Sstevel@tonic-gate 			    (rcm_handle_t *, char *, uint_t,
3787c478bd9Sstevel@tonic-gate 			    rcm_info_t **))sym;
3797c478bd9Sstevel@tonic-gate 			break;
3807c478bd9Sstevel@tonic-gate 		case NOTIFY_REMOVE:
3817c478bd9Sstevel@tonic-gate 			rcm->notify_remove_list = (int (*)
3827c478bd9Sstevel@tonic-gate 			    (rcm_handle_t *, char **, uint_t,
3837c478bd9Sstevel@tonic-gate 			    rcm_info_t **))sym;
3847c478bd9Sstevel@tonic-gate 			break;
3857c478bd9Sstevel@tonic-gate 		case REQUEST_CAP_CHANGE:
3867c478bd9Sstevel@tonic-gate 			rcm->request_capacity_change = (int (*)
3877c478bd9Sstevel@tonic-gate 			    (rcm_handle_t *, char *, uint_t,
3887c478bd9Sstevel@tonic-gate 			    nvlist_t *, rcm_info_t **))sym;
3897c478bd9Sstevel@tonic-gate 			break;
3907c478bd9Sstevel@tonic-gate 		case NOTIFY_CAP_CHANGE:
3917c478bd9Sstevel@tonic-gate 			rcm->notify_capacity_change = (int (*)
3927c478bd9Sstevel@tonic-gate 			    (rcm_handle_t *, char *, uint_t,
3937c478bd9Sstevel@tonic-gate 			    nvlist_t *, rcm_info_t **))sym;
3947c478bd9Sstevel@tonic-gate 			break;
3957c478bd9Sstevel@tonic-gate 		default:
3967c478bd9Sstevel@tonic-gate 			break;
3977c478bd9Sstevel@tonic-gate 		}
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	if (rcm->alloc_handle == NULL ||
4017c478bd9Sstevel@tonic-gate 	    (*rcm->alloc_handle)(NULL, RCM_NOPID, NULL, &rcm->hd)
4027c478bd9Sstevel@tonic-gate 	    != RCM_SUCCESS) {
4037c478bd9Sstevel@tonic-gate 		ap_err(a, ERR_RCM_HANDLE);
4047c478bd9Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
4057c478bd9Sstevel@tonic-gate 	}
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	/*
4087c478bd9Sstevel@tonic-gate 	 * Offlining/onlining a board means offlining/onlining
4097c478bd9Sstevel@tonic-gate 	 * all components on the board.  When operating on a
4107c478bd9Sstevel@tonic-gate 	 * single component no component sequence number is
4117c478bd9Sstevel@tonic-gate 	 * needed since the default is the current (target)
4127c478bd9Sstevel@tonic-gate 	 * component.
4137c478bd9Sstevel@tonic-gate 	 */
4147c478bd9Sstevel@tonic-gate 	if (a->tgt == AP_BOARD) {
4157c478bd9Sstevel@tonic-gate 		rcm->firstcm = 0;
4167c478bd9Sstevel@tonic-gate 		rcm->lastcm = a->ncm - 1;
4177c478bd9Sstevel@tonic-gate 	} else {
4187c478bd9Sstevel@tonic-gate 		rcm->firstcm = CM_DFLT;
4197c478bd9Sstevel@tonic-gate 		rcm->lastcm = CM_DFLT;
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	if (rcm->cpuids == NULL) {
4237c478bd9Sstevel@tonic-gate 		int cm;
4247c478bd9Sstevel@tonic-gate 		int ncpu;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 		/*
4277c478bd9Sstevel@tonic-gate 		 * Allocate space for the cpu capacity change info.
4287c478bd9Sstevel@tonic-gate 		 * Not every cpu may be relevant to the capacity
4297c478bd9Sstevel@tonic-gate 		 * request, but allocating for the maximum makes
4307c478bd9Sstevel@tonic-gate 		 * it easier, and the space is insignifcant.
4317c478bd9Sstevel@tonic-gate 		 */
4327c478bd9Sstevel@tonic-gate 		for (ncpu = 0, cm = rcm->firstcm; cm <= rcm->lastcm; cm++) {
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 			ap_target_t type = ap_cm_type(a, cm);
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 			if ((type == AP_CPU) || (type == AP_CMP)) {
4377c478bd9Sstevel@tonic-gate 				ncpu += ap_cm_ncap(a, cm);
4387c478bd9Sstevel@tonic-gate 			}
4397c478bd9Sstevel@tonic-gate 		}
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 		rcm->ncpus = ncpu;
4427c478bd9Sstevel@tonic-gate 		if ((rcm->cpuids = (cpuid_t *)calloc(ncpu, sizeof (cpuid_t)))
4437c478bd9Sstevel@tonic-gate 		    == NULL) {
4447c478bd9Sstevel@tonic-gate 			ap_err(a, ERR_NOMEM);
4457c478bd9Sstevel@tonic-gate 			return (CFGA_LIB_ERROR);
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	/*
4507c478bd9Sstevel@tonic-gate 	 * Remember initial capacity information.
4517c478bd9Sstevel@tonic-gate 	 * This information is based on the initial
4527c478bd9Sstevel@tonic-gate 	 * state of the ap_id, i.e. before any
4537c478bd9Sstevel@tonic-gate 	 * state change change operations were
4547c478bd9Sstevel@tonic-gate 	 * executed.  We will later get the
4557c478bd9Sstevel@tonic-gate 	 * current capacity information in order
4567c478bd9Sstevel@tonic-gate 	 * to figure out exactly what has changed
4577c478bd9Sstevel@tonic-gate 	 * as the result of the executed command
4587c478bd9Sstevel@tonic-gate 	 * sequence.
4597c478bd9Sstevel@tonic-gate 	 */
4607c478bd9Sstevel@tonic-gate 	rc = ap_capinfo(a, rcm->firstcm, rcm->lastcm, &rcm->capinfo);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	rcm->capcpus = sysconf(_SC_NPROCESSORS_CONF);
4637c478bd9Sstevel@tonic-gate 	rcm->cappages = sysconf(_SC_PHYS_PAGES);
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	return (rc);
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate void
ap_rcm_fini(apd_t * a)4697c478bd9Sstevel@tonic-gate ap_rcm_fini(apd_t *a)
4707c478bd9Sstevel@tonic-gate {
4717c478bd9Sstevel@tonic-gate 	rcmd_t *rcm;
4727c478bd9Sstevel@tonic-gate 	char **rp;
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 	DBG("ap_rcm_fini(%p)\n", (void *)a);
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	if ((rcm = (rcmd_t *)a->rcm) == NULL)
4777c478bd9Sstevel@tonic-gate 		return;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	if (rcm->hd)
4807c478bd9Sstevel@tonic-gate 		(*rcm->free_handle)(rcm->hd);
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	(void) dlclose(rcm->lib);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	/*
4857c478bd9Sstevel@tonic-gate 	 * Free all the names in the resource list, followed
4867c478bd9Sstevel@tonic-gate 	 * by the resource list itself.
4877c478bd9Sstevel@tonic-gate 	 */
4887c478bd9Sstevel@tonic-gate 	if (rcm->rlist)
4897c478bd9Sstevel@tonic-gate 		for (rp = rcm->rlist; *rp; rp++)
4907c478bd9Sstevel@tonic-gate 			s_free(*rp);
4917c478bd9Sstevel@tonic-gate 	s_free(rcm->rlist);
4927c478bd9Sstevel@tonic-gate 	s_free(rcm->cpuids);
4937c478bd9Sstevel@tonic-gate 	s_free(rcm->capinfo);
4947c478bd9Sstevel@tonic-gate 	s_free(a->rcm);
4957c478bd9Sstevel@tonic-gate }
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate static cfga_err_t
ap_rcm_rlist(apd_t * a,int firstcm,int lastcm,char *** rlist,int cmd)4987c478bd9Sstevel@tonic-gate ap_rcm_rlist(apd_t *a, int firstcm, int lastcm, char ***rlist, int cmd)
4997c478bd9Sstevel@tonic-gate {
5007c478bd9Sstevel@tonic-gate 	int n;
5017c478bd9Sstevel@tonic-gate 	int cm;
5027c478bd9Sstevel@tonic-gate 	int ncap;
5037c478bd9Sstevel@tonic-gate 	char *path;
5047c478bd9Sstevel@tonic-gate 	char *cpuname;
5057c478bd9Sstevel@tonic-gate 	char **rp;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	DBG("ap_rcm_rlist(%p)\n", (void *)a);
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	/*
5107c478bd9Sstevel@tonic-gate 	 * Allocate space for the maximum number of components
5117c478bd9Sstevel@tonic-gate 	 * that can be affected by this operation.
5127c478bd9Sstevel@tonic-gate 	 */
5137c478bd9Sstevel@tonic-gate 	for (ncap = 0, cm = firstcm; cm <= lastcm; cm++) {
5147c478bd9Sstevel@tonic-gate 		ncap += ap_cm_ncap(a, cm);
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	DBG("ncap=%d\n", ncap);
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	if ((rp = (char **)calloc(ncap + 1, sizeof (char *))) == NULL) {
5207c478bd9Sstevel@tonic-gate 		ap_err(a, ERR_NOMEM);
5217c478bd9Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	n = 12;	/* SUNW_cpu/cpuCCC */
5257c478bd9Sstevel@tonic-gate 		/* <--- 12 --->    */
5267c478bd9Sstevel@tonic-gate 	cpuname = "SUNW_cpu/cpuCCC";
5277c478bd9Sstevel@tonic-gate 	/*
5287c478bd9Sstevel@tonic-gate 	 * Set the RCM resource name for each component:
5297c478bd9Sstevel@tonic-gate 	 *
5307c478bd9Sstevel@tonic-gate 	 * io:		<device-path>
5317c478bd9Sstevel@tonic-gate 	 * cpu:		SUNW_cpu/cpu<cpuid>
5327c478bd9Sstevel@tonic-gate 	 *
5337c478bd9Sstevel@tonic-gate 	 */
5347c478bd9Sstevel@tonic-gate 	for (ncap = 0, cm = firstcm; cm <= lastcm; cm++) {
5357c478bd9Sstevel@tonic-gate 		switch (ap_cm_type(a, cm)) {
5367c478bd9Sstevel@tonic-gate 		case AP_CPU:
5377c478bd9Sstevel@tonic-gate 		case AP_CMP: {
5387c478bd9Sstevel@tonic-gate 			int		i;
5397c478bd9Sstevel@tonic-gate 			int		len;
5407c478bd9Sstevel@tonic-gate 			cap_info_t	cap;
5417c478bd9Sstevel@tonic-gate 			cfga_stat_t	os;
5427c478bd9Sstevel@tonic-gate 			cpuid_t		*cpuid;
5437c478bd9Sstevel@tonic-gate 			int		*nc;
5447c478bd9Sstevel@tonic-gate 			cap_info_t	*prevcap;
5457c478bd9Sstevel@tonic-gate 			rcmd_t		*rcm;
5467c478bd9Sstevel@tonic-gate 			int		allow_op;
5477c478bd9Sstevel@tonic-gate 			int		capindex;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 			cpuid = cap.type.cpuid;
5507c478bd9Sstevel@tonic-gate 			nc = &cap.ncap;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 			/*
5537c478bd9Sstevel@tonic-gate 			 * See if the request target is a single
5547c478bd9Sstevel@tonic-gate 			 * (default) component
5557c478bd9Sstevel@tonic-gate 			 */
5567c478bd9Sstevel@tonic-gate 			capindex = (cm == CM_DFLT) ? 0 : cm;
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 			/* Get the previous capacity info */
5597c478bd9Sstevel@tonic-gate 			rcm = (rcmd_t *)a->rcm;
5607c478bd9Sstevel@tonic-gate 			prevcap = rcm->capinfo;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 			if (!ap_cm_capacity(a, cm, cpuid, nc, &os)) {
5637c478bd9Sstevel@tonic-gate 				break;
5647c478bd9Sstevel@tonic-gate 			}
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 			len = (strlen(cpuname) - n) + 1;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 			/*
5697c478bd9Sstevel@tonic-gate 			 * For CMD_RCM_OFFLINE and REMOVE, add the CPU to the
5707c478bd9Sstevel@tonic-gate 			 * list if it is currently configured. For
5717c478bd9Sstevel@tonic-gate 			 * CMD_RCM_ONLINE, do so only if the state has changed
5727c478bd9Sstevel@tonic-gate 			 * to CFGA_STAT_CONFIGURED.
5737c478bd9Sstevel@tonic-gate 			 */
5747c478bd9Sstevel@tonic-gate 			allow_op = 0;
5757c478bd9Sstevel@tonic-gate 			if ((cmd == CMD_RCM_OFFLINE) ||
5767c478bd9Sstevel@tonic-gate 			    (cmd == CMD_RCM_REMOVE)) {
5777c478bd9Sstevel@tonic-gate 				if (os == CFGA_STAT_CONFIGURED)
5787c478bd9Sstevel@tonic-gate 					allow_op = 1;
5797c478bd9Sstevel@tonic-gate 			} else {
5807c478bd9Sstevel@tonic-gate 				if ((os == CFGA_STAT_CONFIGURED) &&
5817c478bd9Sstevel@tonic-gate 				    ((prevcap == NULL) ||
5827c478bd9Sstevel@tonic-gate 				    (prevcap[capindex].ostate != os)))
5837c478bd9Sstevel@tonic-gate 					allow_op = 1;
5847c478bd9Sstevel@tonic-gate 			}
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 			if (allow_op) {
5877c478bd9Sstevel@tonic-gate 				for (i = 0; i < *nc; i++) {
5887c478bd9Sstevel@tonic-gate 					if ((path = strdup(cpuname)) == NULL) {
5897c478bd9Sstevel@tonic-gate 						ap_err(a, ERR_NOMEM);
5907c478bd9Sstevel@tonic-gate 						return (CFGA_LIB_ERROR);
5917c478bd9Sstevel@tonic-gate 					}
5927c478bd9Sstevel@tonic-gate 					(void) snprintf(&path[n], len, "%d",
5937c478bd9Sstevel@tonic-gate 					    cpuid[i]);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 					DBG("rp[%d]=%s\n", ncap, path);
5967c478bd9Sstevel@tonic-gate 					rp[ncap++] = path;
5977c478bd9Sstevel@tonic-gate 				}
5987c478bd9Sstevel@tonic-gate 			}
5997c478bd9Sstevel@tonic-gate 			break;
6007c478bd9Sstevel@tonic-gate 		}
6017c478bd9Sstevel@tonic-gate 		case AP_IO:
6027c478bd9Sstevel@tonic-gate 			if ((path = ap_cm_devpath(a, cm)) != NULL) {
6037c478bd9Sstevel@tonic-gate 				DBG("rp[%d]=%s\n", ncap, path);
6047c478bd9Sstevel@tonic-gate 				rp[ncap++] = path;
6057c478bd9Sstevel@tonic-gate 			}
6067c478bd9Sstevel@tonic-gate 			break;
6077c478bd9Sstevel@tonic-gate 		case AP_MEM:
6087c478bd9Sstevel@tonic-gate 			/*
6097c478bd9Sstevel@tonic-gate 			 * Nothing to do for AP_MEM since only capacity
6107c478bd9Sstevel@tonic-gate 			 * change notifications apply to SUNW_memory
6117c478bd9Sstevel@tonic-gate 			 */
6127c478bd9Sstevel@tonic-gate 		default:
6137c478bd9Sstevel@tonic-gate 			break;
6147c478bd9Sstevel@tonic-gate 		}
6157c478bd9Sstevel@tonic-gate 	}
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	rp[ncap] = NULL;
6187c478bd9Sstevel@tonic-gate 	if (rlist)
6197c478bd9Sstevel@tonic-gate 		*rlist = rp;
6207c478bd9Sstevel@tonic-gate 	return (CFGA_OK);
6217c478bd9Sstevel@tonic-gate }
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate /*
6247c478bd9Sstevel@tonic-gate  * Returns 1 if the cpu ID 'cpuid' is in the list of CPU IDs
6257c478bd9Sstevel@tonic-gate  * 'list' of length 'length'. Returns 0 otherwise.
6267c478bd9Sstevel@tonic-gate  */
6277c478bd9Sstevel@tonic-gate static int
is_cpu_in_list(cpuid_t cpuid,cpuid_t * list,int length)6287c478bd9Sstevel@tonic-gate is_cpu_in_list(cpuid_t cpuid, cpuid_t *list, int length)
6297c478bd9Sstevel@tonic-gate {
6307c478bd9Sstevel@tonic-gate 	int i;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	DBG("is_cpu_in_list\n");
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	if (list == NULL)
6357c478bd9Sstevel@tonic-gate 		return (0);
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	for (i = 0; i < length; i++) {
6387c478bd9Sstevel@tonic-gate 		if (list[i] == cpuid)
6397c478bd9Sstevel@tonic-gate 			return (1);
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate 	return (0);
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate static int
ap_rcm_cap_cpu(apd_t * a,rcmd_t * rcm,rcm_handle_t * hd,uint_t flags,rcm_info_t ** rinfo,int cmd,int change)6457c478bd9Sstevel@tonic-gate ap_rcm_cap_cpu(apd_t *a, rcmd_t *rcm, rcm_handle_t *hd, uint_t flags,
6467c478bd9Sstevel@tonic-gate 	rcm_info_t **rinfo, int cmd, int change)
6477c478bd9Sstevel@tonic-gate {
6487c478bd9Sstevel@tonic-gate 	int i;
6497c478bd9Sstevel@tonic-gate 	int rv = RCM_FAILURE;
6507c478bd9Sstevel@tonic-gate 	int ncpuids;
6517c478bd9Sstevel@tonic-gate 	int oldncpuids;
6527c478bd9Sstevel@tonic-gate 	int newncpuids;
6537c478bd9Sstevel@tonic-gate 	char buf[32];
6547c478bd9Sstevel@tonic-gate 	const char *fmt;
6557c478bd9Sstevel@tonic-gate 	size_t size;
6567c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
6577c478bd9Sstevel@tonic-gate 	cpuid_t *cpuids = NULL;
6587c478bd9Sstevel@tonic-gate 	cpuid_t *oldcpuids = NULL;
6597c478bd9Sstevel@tonic-gate 	cpuid_t *newcpuids = NULL;
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	DBG("ap_rcm_cap_cpu(%p)\n", (void *)a);
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	/*
6647c478bd9Sstevel@tonic-gate 	 * Get the current number of configured cpus.
6657c478bd9Sstevel@tonic-gate 	 */
6667c478bd9Sstevel@tonic-gate 	if (getsyscpuids(&ncpuids, &cpuids) == -1)
6677c478bd9Sstevel@tonic-gate 		return (rv);
6687c478bd9Sstevel@tonic-gate 	else if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
6697c478bd9Sstevel@tonic-gate 		free(cpuids);
6707c478bd9Sstevel@tonic-gate 		goto done;
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	if (change == 1)
6747c478bd9Sstevel@tonic-gate 		fmt = "(%d cpu)";
6757c478bd9Sstevel@tonic-gate 	else
6767c478bd9Sstevel@tonic-gate 		fmt = "(%d cpus)";
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	size = sizeof (cpuid_t);
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 	if (cmd == CMD_RCM_CAP_DEL) {
6817c478bd9Sstevel@tonic-gate 		/*
6827c478bd9Sstevel@tonic-gate 		 * A delete request. rcm->cpuids represents the
6837c478bd9Sstevel@tonic-gate 		 * cpus that will be unconfigured. The current
6847c478bd9Sstevel@tonic-gate 		 * set of cpus, before the unconfigure operation,
6857c478bd9Sstevel@tonic-gate 		 * are the old CPUs. The new CPUs are those
6867c478bd9Sstevel@tonic-gate 		 * that would remain.
6877c478bd9Sstevel@tonic-gate 		 */
6887c478bd9Sstevel@tonic-gate 		oldncpuids = ncpuids;
6897c478bd9Sstevel@tonic-gate 		oldcpuids = cpuids;
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 		/*
6927c478bd9Sstevel@tonic-gate 		 * Fill newcpuids with the CPU IDs in the cpuids array,
6937c478bd9Sstevel@tonic-gate 		 * but not in rcm->cpuids.
6947c478bd9Sstevel@tonic-gate 		 */
6957c478bd9Sstevel@tonic-gate 		newcpuids = (cpuid_t *)calloc(ncpuids, size);
6967c478bd9Sstevel@tonic-gate 		if (newcpuids == NULL)
6977c478bd9Sstevel@tonic-gate 			goto done;
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 		newncpuids = 0;
7007c478bd9Sstevel@tonic-gate 		for (i = 0; i < ncpuids; i++) {
7017c478bd9Sstevel@tonic-gate 			if (!is_cpu_in_list(cpuids[i], rcm->cpuids, change))
7027c478bd9Sstevel@tonic-gate 				newcpuids[newncpuids++] = cpuids[i];
7037c478bd9Sstevel@tonic-gate 		}
7047c478bd9Sstevel@tonic-gate 	} else if (cmd == CMD_RCM_CAP_NOTIFY) {
7057c478bd9Sstevel@tonic-gate 		/*
7067c478bd9Sstevel@tonic-gate 		 * An unconfigure capacity change notification. This
7077c478bd9Sstevel@tonic-gate 		 * notification is sent after a DR unconfigure, whether
7087c478bd9Sstevel@tonic-gate 		 * or not the DR was successful. rcm->cpuids represents
7097c478bd9Sstevel@tonic-gate 		 * the CPUs that have been unconfigured.
7107c478bd9Sstevel@tonic-gate 		 */
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 		/* New CPU IDs are the CPUs configured right now. */
7137c478bd9Sstevel@tonic-gate 		newncpuids = ncpuids;
7147c478bd9Sstevel@tonic-gate 		newcpuids = cpuids;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 		/*
7177c478bd9Sstevel@tonic-gate 		 * Old CPU IDs are the CPUs configured right now
7187c478bd9Sstevel@tonic-gate 		 * in addition to those that have been unconfigured.
7197c478bd9Sstevel@tonic-gate 		 * We build the old CPU ID list by concatenating
7207c478bd9Sstevel@tonic-gate 		 * cpuids and rcm->cpuids.
7217c478bd9Sstevel@tonic-gate 		 */
7227c478bd9Sstevel@tonic-gate 		oldcpuids = (cpuid_t *)calloc(ncpuids + change, size);
7237c478bd9Sstevel@tonic-gate 		if (oldcpuids == NULL)
7247c478bd9Sstevel@tonic-gate 			goto done;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 		oldncpuids = 0;
7277c478bd9Sstevel@tonic-gate 		for (i = 0; i < ncpuids; i++) {
7287c478bd9Sstevel@tonic-gate 			if (!is_cpu_in_list(cpuids[i], rcm->cpuids, change))
7297c478bd9Sstevel@tonic-gate 				oldcpuids[oldncpuids++] = cpuids[i];
7307c478bd9Sstevel@tonic-gate 		}
7317c478bd9Sstevel@tonic-gate 		for (i = 0; i < change; i++)
7327c478bd9Sstevel@tonic-gate 			oldcpuids[oldncpuids++] = rcm->cpuids[i];
7337c478bd9Sstevel@tonic-gate 	} else {
7347c478bd9Sstevel@tonic-gate 		DBG("ap_rcm_cap_cpu: CPU capacity, old = %d, new = %d \n",
7357c478bd9Sstevel@tonic-gate 		    rcm->capcpus, ncpuids);
7367c478bd9Sstevel@tonic-gate 		if (rcm->capcpus == ncpuids) {
7377c478bd9Sstevel@tonic-gate 			/* No real change in CPU capacity */
7387c478bd9Sstevel@tonic-gate 			rv = RCM_SUCCESS;
7397c478bd9Sstevel@tonic-gate 			goto done;
7407c478bd9Sstevel@tonic-gate 		}
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 		/*
7437c478bd9Sstevel@tonic-gate 		 * An add notification.  rcm->cpuids represents the
7447c478bd9Sstevel@tonic-gate 		 * cpus that have been configured.  The current
7457c478bd9Sstevel@tonic-gate 		 * set of cpus, after the configure operation,
7467c478bd9Sstevel@tonic-gate 		 * are the new CPU IDs.
7477c478bd9Sstevel@tonic-gate 		 */
7487c478bd9Sstevel@tonic-gate 		newncpuids = ncpuids;
7497c478bd9Sstevel@tonic-gate 		newcpuids = cpuids;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 		/*
7527c478bd9Sstevel@tonic-gate 		 * Fill oldcpuids with the CPU IDs in the cpuids array,
7537c478bd9Sstevel@tonic-gate 		 * but not in rcm->cpuids.
7547c478bd9Sstevel@tonic-gate 		 */
7557c478bd9Sstevel@tonic-gate 		oldcpuids = (cpuid_t *)calloc(ncpuids, size);
7567c478bd9Sstevel@tonic-gate 		if (oldcpuids == NULL)
7577c478bd9Sstevel@tonic-gate 			goto done;
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 		oldncpuids = 0;
7607c478bd9Sstevel@tonic-gate 		for (i = 0; i < ncpuids; i++) {
7617c478bd9Sstevel@tonic-gate 			if (!is_cpu_in_list(cpuids[i], rcm->cpuids, change))
7627c478bd9Sstevel@tonic-gate 				oldcpuids[oldncpuids++] = cpuids[i];
7637c478bd9Sstevel@tonic-gate 		}
7647c478bd9Sstevel@tonic-gate 	}
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	DBG("oldcpuids: ");
7677c478bd9Sstevel@tonic-gate 	for (i = 0; i < oldncpuids; i++)
7687c478bd9Sstevel@tonic-gate 		DBG("%d ", oldcpuids[i]);
7697c478bd9Sstevel@tonic-gate 	DBG("\n");
7707c478bd9Sstevel@tonic-gate 	DBG("change   : ");
7717c478bd9Sstevel@tonic-gate 	for (i = 0; i < change; i++)
7727c478bd9Sstevel@tonic-gate 		DBG("%d ", rcm->cpuids[i]);
7737c478bd9Sstevel@tonic-gate 	DBG("\n");
7747c478bd9Sstevel@tonic-gate 	DBG("newcpuids: ");
7757c478bd9Sstevel@tonic-gate 	for (i = 0; i < newncpuids; i++)
7767c478bd9Sstevel@tonic-gate 		DBG("%d ", newcpuids[i]);
7777c478bd9Sstevel@tonic-gate 	DBG("\n");
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	if (nvlist_add_string(nvl, "state", "capacity") != 0 ||
7807c478bd9Sstevel@tonic-gate 	    nvlist_add_int32(nvl, "old_total", oldncpuids) != 0 ||
7817c478bd9Sstevel@tonic-gate 	    nvlist_add_int32(nvl, "new_total", newncpuids) != 0 ||
7827c478bd9Sstevel@tonic-gate 	    nvlist_add_int32_array(nvl, "old_cpu_list", oldcpuids,
7837c478bd9Sstevel@tonic-gate 	    oldncpuids) != 0 ||
7847c478bd9Sstevel@tonic-gate 	    nvlist_add_int32_array(nvl, "new_cpu_list", newcpuids,
7857c478bd9Sstevel@tonic-gate 	    newncpuids) != 0)
7867c478bd9Sstevel@tonic-gate 		goto done;
7877c478bd9Sstevel@tonic-gate 
788*a3114836SGerry Liu 	(void) snprintf(buf, sizeof (buf), fmt, change);
7897c478bd9Sstevel@tonic-gate 	ap_msg(a, MSG_ISSUE, cmd, buf);
7907c478bd9Sstevel@tonic-gate 
791*a3114836SGerry Liu 	if (cmd == CMD_RCM_CAP_DEL) {
7927c478bd9Sstevel@tonic-gate 		rv = (*rcm->request_capacity_change)(hd, "SUNW_cpu",
7937c478bd9Sstevel@tonic-gate 		    flags, nvl, rinfo);
794*a3114836SGerry Liu 	} else {
7957c478bd9Sstevel@tonic-gate 		rv = (*rcm->notify_capacity_change)(hd, "SUNW_cpu",
7967c478bd9Sstevel@tonic-gate 		    flags & ~RCM_FORCE, nvl, rinfo);
797*a3114836SGerry Liu 	}
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate done:
8007c478bd9Sstevel@tonic-gate 	nvlist_free(nvl);
8017c478bd9Sstevel@tonic-gate 	s_free(oldcpuids);
8027c478bd9Sstevel@tonic-gate 	s_free(newcpuids);
8037c478bd9Sstevel@tonic-gate 	return (rv);
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate static int
ap_rcm_cap_mem(apd_t * a,rcmd_t * rcm,rcm_handle_t * hd,uint_t flags,rcm_info_t ** rinfo,int cmd,long change)8077c478bd9Sstevel@tonic-gate ap_rcm_cap_mem(apd_t *a, rcmd_t *rcm, rcm_handle_t *hd, uint_t flags,
8087c478bd9Sstevel@tonic-gate 	rcm_info_t **rinfo, int cmd, long change)
8097c478bd9Sstevel@tonic-gate {
8107c478bd9Sstevel@tonic-gate 	int rv;
8117c478bd9Sstevel@tonic-gate 	int pgsize;
8127c478bd9Sstevel@tonic-gate 	long oldpages;
8137c478bd9Sstevel@tonic-gate 	long newpages;
8147c478bd9Sstevel@tonic-gate 	long currpages;
8157c478bd9Sstevel@tonic-gate 	char buf[32];
8167c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 	DBG("ap_rcm_cap_mem(%p)\n", (void *)a);
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	/*
8217c478bd9Sstevel@tonic-gate 	 * Get the current amount of configured memory.
8227c478bd9Sstevel@tonic-gate 	 */
8237c478bd9Sstevel@tonic-gate 	if ((pgsize = sysconf(_SC_PAGE_SIZE)) == -1 ||
8247c478bd9Sstevel@tonic-gate 	    (currpages = sysconf(_SC_PHYS_PAGES)) == -1 ||
8257c478bd9Sstevel@tonic-gate 	    nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) > 0)
8267c478bd9Sstevel@tonic-gate 		return (RCM_FAILURE);
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	/*
8297c478bd9Sstevel@tonic-gate 	 * If this is a (delete) request, change represents
8307c478bd9Sstevel@tonic-gate 	 * the amount of capacity that will be deleted from the
8317c478bd9Sstevel@tonic-gate 	 * system.  If this is an (add) notification, change
8327c478bd9Sstevel@tonic-gate 	 * represents the amount of capacity that has already
8337c478bd9Sstevel@tonic-gate 	 * been added to the system.
8347c478bd9Sstevel@tonic-gate 	 */
8357c478bd9Sstevel@tonic-gate 	if (cmd == CMD_RCM_CAP_DEL) {
8367c478bd9Sstevel@tonic-gate 		oldpages = currpages;
8377c478bd9Sstevel@tonic-gate 		newpages = currpages - change;
8387c478bd9Sstevel@tonic-gate 	} else if (cmd == CMD_RCM_CAP_NOTIFY) {
8397c478bd9Sstevel@tonic-gate 		newpages = currpages;
8407c478bd9Sstevel@tonic-gate 		oldpages = rcm->cappages;
8417c478bd9Sstevel@tonic-gate 	} else {
8427c478bd9Sstevel@tonic-gate 		if (rcm->cappages == currpages) {
8437c478bd9Sstevel@tonic-gate 			/* No real change in memory capacity */
8447c478bd9Sstevel@tonic-gate 			DBG("ap_rcm_cap_mem: no change in capacity.\n");
8457c478bd9Sstevel@tonic-gate 			nvlist_free(nvl);
8467c478bd9Sstevel@tonic-gate 			return (RCM_SUCCESS);
8477c478bd9Sstevel@tonic-gate 		}
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 		oldpages = currpages - change;
8507c478bd9Sstevel@tonic-gate 		newpages = currpages;
8517c478bd9Sstevel@tonic-gate 	}
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	DBG("ap_rcm_cap_mem: Memory capacity, old = %ld, new = %ld\n",
8547c478bd9Sstevel@tonic-gate 	    oldpages, newpages);
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	if (nvlist_add_string(nvl, "state", "capacity") != 0 ||
8577c478bd9Sstevel@tonic-gate 	    nvlist_add_int32(nvl, "page_size", pgsize) != 0 ||
8587c478bd9Sstevel@tonic-gate 	    nvlist_add_int32(nvl, "old_pages", oldpages) != 0 ||
8597c478bd9Sstevel@tonic-gate 	    nvlist_add_int32(nvl, "new_pages", newpages) != 0) {
8607c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
8617c478bd9Sstevel@tonic-gate 		return (RCM_FAILURE);
8627c478bd9Sstevel@tonic-gate 	}
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "(%ld pages)", change);
8657c478bd9Sstevel@tonic-gate 	ap_msg(a, MSG_ISSUE, cmd, buf);
8667c478bd9Sstevel@tonic-gate 
867*a3114836SGerry Liu 	if (cmd == CMD_RCM_CAP_DEL) {
8687c478bd9Sstevel@tonic-gate 		rv = (*rcm->request_capacity_change)(hd, "SUNW_memory",
8697c478bd9Sstevel@tonic-gate 		    flags, nvl, rinfo);
870*a3114836SGerry Liu 	} else {
8717c478bd9Sstevel@tonic-gate 		rv = (*rcm->notify_capacity_change)(hd, "SUNW_memory",
8727c478bd9Sstevel@tonic-gate 		    flags & ~RCM_FORCE, nvl, rinfo);
873*a3114836SGerry Liu 	}
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	nvlist_free(nvl);
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	return (rv);
8787c478bd9Sstevel@tonic-gate }
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate static cfga_err_t
ap_rcm_request_cap(apd_t * a,rcmd_t * rcm,rcm_handle_t * hd,int * rv,uint_t flags,rcm_info_t ** rinfo)8817c478bd9Sstevel@tonic-gate ap_rcm_request_cap(apd_t *a, rcmd_t *rcm, rcm_handle_t *hd,
8827c478bd9Sstevel@tonic-gate 	int *rv, uint_t flags, rcm_info_t **rinfo)
8837c478bd9Sstevel@tonic-gate {
8847c478bd9Sstevel@tonic-gate 	int cm;
8857c478bd9Sstevel@tonic-gate 	int ncpus;
8867c478bd9Sstevel@tonic-gate 	long npages;
8877c478bd9Sstevel@tonic-gate 	cap_info_t *capinfo;
8887c478bd9Sstevel@tonic-gate 	ap_target_t type;
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	DBG("ap_rcm_request_cap(%p)\n", (void *)a);
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	if ((capinfo = rcm->capinfo) == NULL) {
8937c478bd9Sstevel@tonic-gate 		ap_err(a, ERR_PLUGIN, "null capinfo");
8947c478bd9Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
8957c478bd9Sstevel@tonic-gate 	}
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	ncpus = npages = 0;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	for (cm = rcm->firstcm; cm <= rcm->lastcm; cm++) {
9007c478bd9Sstevel@tonic-gate 		int i, j;
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 		/*
9037c478bd9Sstevel@tonic-gate 		 * See if the request target is a single
9047c478bd9Sstevel@tonic-gate 		 * (default) component
9057c478bd9Sstevel@tonic-gate 		 */
9067c478bd9Sstevel@tonic-gate 		i = (cm == CM_DFLT) ? 0 : cm;
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 		/*
9097c478bd9Sstevel@tonic-gate 		 * We are interested only in those components
9107c478bd9Sstevel@tonic-gate 		 * in the configured state since they represent
9117c478bd9Sstevel@tonic-gate 		 * available capacity.
9127c478bd9Sstevel@tonic-gate 		 */
9137c478bd9Sstevel@tonic-gate 		type = ap_cm_type(a, cm);
9147c478bd9Sstevel@tonic-gate 		if (capinfo[i].valid == 0 ||
9157c478bd9Sstevel@tonic-gate 		    capinfo[i].ostate != CFGA_STAT_CONFIGURED)
9167c478bd9Sstevel@tonic-gate 			continue;
9177c478bd9Sstevel@tonic-gate 		else if ((type == AP_CPU) || (type == AP_CMP)) {
9187c478bd9Sstevel@tonic-gate 			for (j = 0; j < capinfo[i].ncap; j++) {
9197c478bd9Sstevel@tonic-gate 				rcm->cpuids[ncpus++] = capinfo[i].type.cpuid[j];
9207c478bd9Sstevel@tonic-gate 			}
9217c478bd9Sstevel@tonic-gate 		} else if (type == AP_MEM)
9227c478bd9Sstevel@tonic-gate 			npages += capinfo[i].type.npages;
9237c478bd9Sstevel@tonic-gate 	}
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	if (ncpus && ((*rv = ap_rcm_cap_cpu(a, rcm, hd, flags, rinfo,
926*a3114836SGerry Liu 	    CMD_RCM_CAP_DEL, ncpus)) != RCM_SUCCESS)) {
9277c478bd9Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
928*a3114836SGerry Liu 	}
9297c478bd9Sstevel@tonic-gate 	if (npages && ((*rv = ap_rcm_cap_mem(a, rcm, hd, flags, rinfo,
930*a3114836SGerry Liu 	    CMD_RCM_CAP_DEL, npages)) != RCM_SUCCESS)) {
9317c478bd9Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
932*a3114836SGerry Liu 	}
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	return (CFGA_OK);
9357c478bd9Sstevel@tonic-gate }
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate static cfga_err_t
ap_rcm_add_cap(apd_t * a,rcmd_t * rcm,rcm_handle_t * hd,int * rv,uint_t flags,rcm_info_t ** rinfo)9387c478bd9Sstevel@tonic-gate ap_rcm_add_cap(apd_t *a, rcmd_t *rcm, rcm_handle_t *hd,
9397c478bd9Sstevel@tonic-gate 	int *rv, uint_t flags, rcm_info_t **rinfo)
9407c478bd9Sstevel@tonic-gate {
9417c478bd9Sstevel@tonic-gate 	int cm;
9427c478bd9Sstevel@tonic-gate 	int ncpus;
9437c478bd9Sstevel@tonic-gate 	long npages;
9447c478bd9Sstevel@tonic-gate 	cap_info_t *capinfo, *prevcapinfo;
9457c478bd9Sstevel@tonic-gate 	cfga_err_t rc;
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	DBG("ap_rcm_add_cap(%p)\n", (void *)a);
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	/* Get the new capacity info to figure out what has changed */
950*a3114836SGerry Liu 	if ((rc = ap_capinfo(a, rcm->firstcm, rcm->lastcm, &capinfo)) !=
951*a3114836SGerry Liu 	    CFGA_OK)
9527c478bd9Sstevel@tonic-gate 		return (rc);
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	if (capinfo == NULL) {
9557c478bd9Sstevel@tonic-gate 		DBG("no pertinent capacity info\n");
9567c478bd9Sstevel@tonic-gate 		return (CFGA_OK);
9577c478bd9Sstevel@tonic-gate 	}
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	ncpus = npages = 0;
9607c478bd9Sstevel@tonic-gate 	prevcapinfo = rcm->capinfo;
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	for (cm = rcm->firstcm; cm <= rcm->lastcm; cm++) {
9637c478bd9Sstevel@tonic-gate 		int i, j;
9647c478bd9Sstevel@tonic-gate 		cfga_stat_t os, prevos;
9657c478bd9Sstevel@tonic-gate 		int prevvalidity;
9667c478bd9Sstevel@tonic-gate 		ap_target_t type;
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 		/*
9697c478bd9Sstevel@tonic-gate 		 * See if the request target is a single
9707c478bd9Sstevel@tonic-gate 		 * (default) component
9717c478bd9Sstevel@tonic-gate 		 */
9727c478bd9Sstevel@tonic-gate 		i = cm == CM_DFLT ? 0 : cm;
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 		os = capinfo[i].ostate;
9757c478bd9Sstevel@tonic-gate 		if (prevcapinfo == NULL) {
9767c478bd9Sstevel@tonic-gate 			prevos = CFGA_STAT_EMPTY;
9777c478bd9Sstevel@tonic-gate 			prevvalidity = 1;
9787c478bd9Sstevel@tonic-gate 		} else {
9797c478bd9Sstevel@tonic-gate 			prevos = prevcapinfo[i].ostate;
9807c478bd9Sstevel@tonic-gate 			prevvalidity = prevcapinfo[i].valid;
9817c478bd9Sstevel@tonic-gate 		}
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 		type = ap_cm_type(a, cm);
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 		DBG("cm=%d valid=%d type=%d, prevos=%d os=%d\n",
9867c478bd9Sstevel@tonic-gate 		    cm, prevvalidity, type, prevos, os);
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 		/*
9897c478bd9Sstevel@tonic-gate 		 * We are interested only in those components
9907c478bd9Sstevel@tonic-gate 		 * whose states have changed to configured as
9917c478bd9Sstevel@tonic-gate 		 * the result of the current cfgadm request.
9927c478bd9Sstevel@tonic-gate 		 */
9937c478bd9Sstevel@tonic-gate 		if (prevvalidity == 0 || os != CFGA_STAT_CONFIGURED) {
9947c478bd9Sstevel@tonic-gate 			capinfo[i].valid = 0;
9957c478bd9Sstevel@tonic-gate 			continue;
9967c478bd9Sstevel@tonic-gate 		} else if (prevos != CFGA_STAT_CONFIGURED) {
9977c478bd9Sstevel@tonic-gate 			/*
9987c478bd9Sstevel@tonic-gate 			 * The occupant state is configured, and
9997c478bd9Sstevel@tonic-gate 			 * the previous occupant state was not.
10007c478bd9Sstevel@tonic-gate 			 */
10017c478bd9Sstevel@tonic-gate 			if ((type == AP_CPU) || (type == AP_CMP)) {
10027c478bd9Sstevel@tonic-gate 				for (j = 0; j < capinfo[i].ncap; j++) {
10037c478bd9Sstevel@tonic-gate 					rcm->cpuids[ncpus++] =
10047c478bd9Sstevel@tonic-gate 					    capinfo[i].type.cpuid[j];
10057c478bd9Sstevel@tonic-gate 				}
10067c478bd9Sstevel@tonic-gate 			} else if (type == AP_MEM)
10077c478bd9Sstevel@tonic-gate 				npages += capinfo[i].type.npages;
10087c478bd9Sstevel@tonic-gate 		}
10097c478bd9Sstevel@tonic-gate 	}
10107c478bd9Sstevel@tonic-gate 	free(capinfo);
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	if (ncpus && ((*rv = ap_rcm_cap_cpu(a, rcm, hd, flags, rinfo,
1013*a3114836SGerry Liu 	    CMD_RCM_CAP_ADD, ncpus)) != RCM_SUCCESS)) {
10147c478bd9Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
1015*a3114836SGerry Liu 	}
10167c478bd9Sstevel@tonic-gate 	if (npages && ((*rv = ap_rcm_cap_mem(a, rcm, hd, flags, rinfo,
1017*a3114836SGerry Liu 	    CMD_RCM_CAP_ADD, npages)) != RCM_SUCCESS)) {
10187c478bd9Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
1019*a3114836SGerry Liu 	}
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	return (CFGA_OK);
10227c478bd9Sstevel@tonic-gate }
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate /*
10257c478bd9Sstevel@tonic-gate  * ap_rcm_notify_cap:
10267c478bd9Sstevel@tonic-gate  *
10277c478bd9Sstevel@tonic-gate  * This routine handles the CMD_RCM_CAP_NOTIFY command. It
10287c478bd9Sstevel@tonic-gate  * is called after a successful/failed DR unconfigure
10297c478bd9Sstevel@tonic-gate  * operation. It filters out components that have changed
10307c478bd9Sstevel@tonic-gate  * and passes this information on to ap_rcm_cap_{cpu,mem}.
10317c478bd9Sstevel@tonic-gate  *
10327c478bd9Sstevel@tonic-gate  * ap_rcm_cap_{cpu,mem} will still be called if all the
10337c478bd9Sstevel@tonic-gate  * components have not changed and at least one {cpu,mem}
10347c478bd9Sstevel@tonic-gate  * component was originally configured.
10357c478bd9Sstevel@tonic-gate  */
10367c478bd9Sstevel@tonic-gate static cfga_err_t
ap_rcm_notify_cap(apd_t * a,rcmd_t * rcm,rcm_handle_t * hd,int * rv,uint_t flags,rcm_info_t ** rinfo)10377c478bd9Sstevel@tonic-gate ap_rcm_notify_cap(apd_t *a, rcmd_t *rcm, rcm_handle_t *hd,
10387c478bd9Sstevel@tonic-gate 	int *rv, uint_t flags, rcm_info_t **rinfo)
10397c478bd9Sstevel@tonic-gate {
10407c478bd9Sstevel@tonic-gate 	cfga_err_t  rc;
10417c478bd9Sstevel@tonic-gate 	cap_info_t  *capinfo;
10427c478bd9Sstevel@tonic-gate 	cap_info_t  *prevcapinfo;
10437c478bd9Sstevel@tonic-gate 	int	    cm;
10447c478bd9Sstevel@tonic-gate 	long	    npages	= 0;
10457c478bd9Sstevel@tonic-gate 	int	    ncpus	= 0;
10467c478bd9Sstevel@tonic-gate 	int	    prev_mem	= 0; /* # of prev. configured mem components */
10477c478bd9Sstevel@tonic-gate 	int	    prev_cpus	= 0; /* # of prev. configured CPUs */
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	DBG("ap_rcm_notify_cap(%p)\n", (void *)a);
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 	/* Get the new capacity info to figure out what has changed */
1052*a3114836SGerry Liu 	if ((rc = ap_capinfo(a, rcm->firstcm, rcm->lastcm, &capinfo)) !=
1053*a3114836SGerry Liu 	    CFGA_OK)
10547c478bd9Sstevel@tonic-gate 		return (rc);
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	if (capinfo == NULL) {
10577c478bd9Sstevel@tonic-gate 		DBG("no pertinent capacity info\n");
10587c478bd9Sstevel@tonic-gate 		return (CFGA_OK);
10597c478bd9Sstevel@tonic-gate 	}
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	/* The original capacity info */
10627c478bd9Sstevel@tonic-gate 	prevcapinfo = rcm->capinfo;
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	/*
10657c478bd9Sstevel@tonic-gate 	 * Cycle through all components that we are operating
10667c478bd9Sstevel@tonic-gate 	 * on. Record which components' occupant states have
10677c478bd9Sstevel@tonic-gate 	 * changed.
10687c478bd9Sstevel@tonic-gate 	 */
10697c478bd9Sstevel@tonic-gate 	for (cm = rcm->firstcm; cm <= rcm->lastcm; cm++) {
10707c478bd9Sstevel@tonic-gate 		int i;
10717c478bd9Sstevel@tonic-gate 		cfga_stat_t prevos, os;
10727c478bd9Sstevel@tonic-gate 		ap_target_t type;
10737c478bd9Sstevel@tonic-gate 		int prev_conf = 0;
10747c478bd9Sstevel@tonic-gate 		int now_conf  = 0;
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 		/*
10777c478bd9Sstevel@tonic-gate 		 * See if the request target is a single
10787c478bd9Sstevel@tonic-gate 		 * (default) component
10797c478bd9Sstevel@tonic-gate 		 */
10807c478bd9Sstevel@tonic-gate 		i = cm == CM_DFLT ? 0 : cm;
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 		os = capinfo[i].ostate;
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 		if (prevcapinfo == NULL) {
10857c478bd9Sstevel@tonic-gate 			prevos = CFGA_STAT_EMPTY;
10867c478bd9Sstevel@tonic-gate 		} else {
10877c478bd9Sstevel@tonic-gate 			prevos = prevcapinfo[i].ostate;
10887c478bd9Sstevel@tonic-gate 			if (prevcapinfo[i].valid == 0) {
10897c478bd9Sstevel@tonic-gate 				DBG("ap_rcm_notify_cap: skipping component "
10907c478bd9Sstevel@tonic-gate 				    "due to prevvalidity == 0\n");
10917c478bd9Sstevel@tonic-gate 				continue;
10927c478bd9Sstevel@tonic-gate 			}
10937c478bd9Sstevel@tonic-gate 		}
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 		type = ap_cm_type(a, cm);
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 		prev_conf = (prevos == CFGA_STAT_CONFIGURED);
10987c478bd9Sstevel@tonic-gate 		now_conf  = (os == CFGA_STAT_CONFIGURED);
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 		/*
11017c478bd9Sstevel@tonic-gate 		 * Build up rcm->cpuids with the IDs of CPUs that
11027c478bd9Sstevel@tonic-gate 		 * have been removed. Record the number of removed
11037c478bd9Sstevel@tonic-gate 		 * CPUs and pages.
11047c478bd9Sstevel@tonic-gate 		 */
11057c478bd9Sstevel@tonic-gate 		if (type == AP_CPU || type == AP_CMP) {
11067c478bd9Sstevel@tonic-gate 			if (prev_conf)
11077c478bd9Sstevel@tonic-gate 				prev_cpus++;
11087c478bd9Sstevel@tonic-gate 			if (prev_conf && !now_conf) {
11097c478bd9Sstevel@tonic-gate 				int j;
11107c478bd9Sstevel@tonic-gate 				for (j = 0; j < capinfo[i].ncap; j++) {
11117c478bd9Sstevel@tonic-gate 					rcm->cpuids[ncpus++] =
11127c478bd9Sstevel@tonic-gate 					    capinfo[i].type.cpuid[j];
11137c478bd9Sstevel@tonic-gate 				}
11147c478bd9Sstevel@tonic-gate 			}
11157c478bd9Sstevel@tonic-gate 		} else if (type == AP_MEM) {
11167c478bd9Sstevel@tonic-gate 			if (prev_conf)
11177c478bd9Sstevel@tonic-gate 				prev_mem++;
11187c478bd9Sstevel@tonic-gate 			if (prev_conf && !now_conf)
11197c478bd9Sstevel@tonic-gate 				npages += capinfo[i].type.npages;
11207c478bd9Sstevel@tonic-gate 		}
11217c478bd9Sstevel@tonic-gate 	}
11227c478bd9Sstevel@tonic-gate 	free(capinfo);
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	/*
11257c478bd9Sstevel@tonic-gate 	 * If any CPU or memory components were operated on,
11267c478bd9Sstevel@tonic-gate 	 * successfully or not, the rcm_notify_capacity_change()
11277c478bd9Sstevel@tonic-gate 	 * routine must be called.
11287c478bd9Sstevel@tonic-gate 	 */
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	if (prev_cpus) {
11317c478bd9Sstevel@tonic-gate 		*rv = ap_rcm_cap_cpu(a, rcm, hd, flags, rinfo,
11327c478bd9Sstevel@tonic-gate 		    CMD_RCM_CAP_NOTIFY, ncpus);
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 		if (*rv != RCM_SUCCESS)
11357c478bd9Sstevel@tonic-gate 			return (CFGA_LIB_ERROR);
11367c478bd9Sstevel@tonic-gate 	}
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	if (prev_mem) {
11397c478bd9Sstevel@tonic-gate 		*rv = ap_rcm_cap_mem(a, rcm, hd, flags, rinfo,
11407c478bd9Sstevel@tonic-gate 		    CMD_RCM_CAP_NOTIFY, npages);
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 		if (*rv != RCM_SUCCESS)
11437c478bd9Sstevel@tonic-gate 			return (CFGA_LIB_ERROR);
11447c478bd9Sstevel@tonic-gate 	}
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	return (CFGA_OK);
11477c478bd9Sstevel@tonic-gate }
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate cfga_err_t
ap_rcm_ctl(apd_t * a,int cmd)11507c478bd9Sstevel@tonic-gate ap_rcm_ctl(apd_t *a, int cmd)
11517c478bd9Sstevel@tonic-gate {
11527c478bd9Sstevel@tonic-gate 	int i;
11537c478bd9Sstevel@tonic-gate 	int rv;
11547c478bd9Sstevel@tonic-gate 	int noop;
11557c478bd9Sstevel@tonic-gate 	int ncpus;
11567c478bd9Sstevel@tonic-gate 	int cm;
11577c478bd9Sstevel@tonic-gate 	uint_t flags;
11587c478bd9Sstevel@tonic-gate 	char *rsrc;
11597c478bd9Sstevel@tonic-gate 	char **rlist;
11607c478bd9Sstevel@tonic-gate 	rcmd_t *rcm;
11617c478bd9Sstevel@tonic-gate 	rcm_info_t *rinfo;
11627c478bd9Sstevel@tonic-gate 	rcm_handle_t *hd;
11637c478bd9Sstevel@tonic-gate 	cfga_err_t rc;
11647c478bd9Sstevel@tonic-gate 	cpuid_t *growcpuids;
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 	DBG("ap_rcm_ctl(%p)\n", (void *)a);
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	if ((rcm = (rcmd_t *)a->rcm) == NULL) {
11697c478bd9Sstevel@tonic-gate 		ap_msg(a, MSG_SKIP, cmd, a->target);
11707c478bd9Sstevel@tonic-gate 		return (CFGA_OK);
11717c478bd9Sstevel@tonic-gate 	}
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	hd = rcm->hd;
11747c478bd9Sstevel@tonic-gate 	rv = RCM_SUCCESS;
11757c478bd9Sstevel@tonic-gate 	rc = CFGA_OK;
11767c478bd9Sstevel@tonic-gate 	if (ap_getopt(a, OPT_FORCE))
11777c478bd9Sstevel@tonic-gate 		flags = RCM_FORCE;
11787c478bd9Sstevel@tonic-gate 	else
11797c478bd9Sstevel@tonic-gate 		flags = 0;
11807c478bd9Sstevel@tonic-gate 	rinfo = NULL;
11817c478bd9Sstevel@tonic-gate 	rlist = NULL;
11827c478bd9Sstevel@tonic-gate 	rsrc = NULL;
11837c478bd9Sstevel@tonic-gate 	noop = 0;
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 	switch (cmd) {
11867c478bd9Sstevel@tonic-gate 	case CMD_RCM_CAP_DEL:
11877c478bd9Sstevel@tonic-gate 		if (rcm->capinfo == NULL)
11887c478bd9Sstevel@tonic-gate 			noop++;
11897c478bd9Sstevel@tonic-gate 		else
1190*a3114836SGerry Liu 			rc = ap_rcm_request_cap(a, rcm, hd, &rv, flags, &rinfo);
11917c478bd9Sstevel@tonic-gate 		break;
11927c478bd9Sstevel@tonic-gate 	case CMD_RCM_CAP_ADD:
11937c478bd9Sstevel@tonic-gate 		rc = ap_rcm_add_cap(a, rcm, hd, &rv, flags, &rinfo);
11947c478bd9Sstevel@tonic-gate 		break;
11957c478bd9Sstevel@tonic-gate 	case CMD_RCM_CAP_NOTIFY:
11967c478bd9Sstevel@tonic-gate 		rc = ap_rcm_notify_cap(a, rcm, hd, &rv, flags, &rinfo);
11977c478bd9Sstevel@tonic-gate 		break;
11987c478bd9Sstevel@tonic-gate 	case CMD_RCM_ONLINE:
11997c478bd9Sstevel@tonic-gate 		/* Refresh changed component states */
12007c478bd9Sstevel@tonic-gate 		if ((rc = ap_stat(a, 1)) != CFGA_OK) {
12017c478bd9Sstevel@tonic-gate 			noop++;
12027c478bd9Sstevel@tonic-gate 			break;
12037c478bd9Sstevel@tonic-gate 		}
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 		if (a->tgt == AP_BOARD) {
12067c478bd9Sstevel@tonic-gate 			rcm->firstcm = 0;
12077c478bd9Sstevel@tonic-gate 			rcm->lastcm = a->ncm - 1;
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 			/* Check if we need to grow our cpuids list */
12107c478bd9Sstevel@tonic-gate 			for (ncpus = 0, cm = rcm->firstcm; cm <= rcm->lastcm;
12117c478bd9Sstevel@tonic-gate 			    cm++) {
12127c478bd9Sstevel@tonic-gate 				ap_target_t type = ap_cm_type(a, cm);
12137c478bd9Sstevel@tonic-gate 				if ((type == AP_CPU) || (type == AP_CMP))
12147c478bd9Sstevel@tonic-gate 					ncpus += ap_cm_ncap(a, cm);
12157c478bd9Sstevel@tonic-gate 			}
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 			if (rcm->ncpus < ncpus) {
12187c478bd9Sstevel@tonic-gate 				if ((growcpuids =
12197c478bd9Sstevel@tonic-gate 				    (cpuid_t *)realloc(rcm->cpuids,
12207c478bd9Sstevel@tonic-gate 				    (ncpus * sizeof (cpuid_t)))) == NULL) {
12217c478bd9Sstevel@tonic-gate 					ap_err(a, ERR_NOMEM);
12227c478bd9Sstevel@tonic-gate 					return (CFGA_LIB_ERROR);
12237c478bd9Sstevel@tonic-gate 				}
12247c478bd9Sstevel@tonic-gate 				rcm->ncpus = ncpus;
12257c478bd9Sstevel@tonic-gate 				rcm->cpuids = growcpuids;
12267c478bd9Sstevel@tonic-gate 			}
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 		} else {
12297c478bd9Sstevel@tonic-gate 			rcm->firstcm = CM_DFLT;
12307c478bd9Sstevel@tonic-gate 			rcm->lastcm = CM_DFLT;
12317c478bd9Sstevel@tonic-gate 		}
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	case CMD_RCM_OFFLINE:
12367c478bd9Sstevel@tonic-gate 	case CMD_RCM_REMOVE: {
12377c478bd9Sstevel@tonic-gate 		uint_t nrsrc;
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 		if (cmd == CMD_RCM_REMOVE) {
12407c478bd9Sstevel@tonic-gate 			/*
12417c478bd9Sstevel@tonic-gate 			 * An unconfigure has just taken place, so
12427c478bd9Sstevel@tonic-gate 			 * refresh the changed component states.
12437c478bd9Sstevel@tonic-gate 			 */
12447c478bd9Sstevel@tonic-gate 			if ((rc = ap_stat(a, 1)) != CFGA_OK) {
12457c478bd9Sstevel@tonic-gate 				noop++;
12467c478bd9Sstevel@tonic-gate 				break;
12477c478bd9Sstevel@tonic-gate 			}
12487c478bd9Sstevel@tonic-gate 		}
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 		/* Check if this is an empty board, i.e. no components */
12517c478bd9Sstevel@tonic-gate 		if (a->ncm == 0) {
12527c478bd9Sstevel@tonic-gate 			noop++;
12537c478bd9Sstevel@tonic-gate 			break;
12547c478bd9Sstevel@tonic-gate 		}
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 		if ((rlist = rcm->rlist) == NULL) {
12577c478bd9Sstevel@tonic-gate 			rc = ap_rcm_rlist(a, rcm->firstcm, rcm->lastcm, &rlist,
12587c478bd9Sstevel@tonic-gate 			    cmd);
12597c478bd9Sstevel@tonic-gate 			if ((rc == CFGA_OK) && (rlist != NULL) &&
12607c478bd9Sstevel@tonic-gate 			    (rlist[0] != NULL)) {
12617c478bd9Sstevel@tonic-gate 				rcm->rlist = rlist;
12627c478bd9Sstevel@tonic-gate 			} else {
12637c478bd9Sstevel@tonic-gate 				/* Do not pass up empty resource list to RCM */
12647c478bd9Sstevel@tonic-gate 				noop++;
12657c478bd9Sstevel@tonic-gate 				break;
12667c478bd9Sstevel@tonic-gate 			}
12677c478bd9Sstevel@tonic-gate 		}
12687c478bd9Sstevel@tonic-gate 		for (nrsrc = 0; rlist[nrsrc] != NULL; nrsrc++)
12697c478bd9Sstevel@tonic-gate 			ap_msg(a, MSG_ISSUE, cmd, rlist[nrsrc]);
12707c478bd9Sstevel@tonic-gate 		if (cmd == CMD_RCM_OFFLINE)
12717c478bd9Sstevel@tonic-gate 			rv = (*rcm->request_offline_list)(hd, rlist, flags,
12727c478bd9Sstevel@tonic-gate 			    &rinfo);
12737c478bd9Sstevel@tonic-gate 		else if (cmd == CMD_RCM_ONLINE)
12747c478bd9Sstevel@tonic-gate 			rv = (*rcm->notify_online_list)(hd, rlist,
12757c478bd9Sstevel@tonic-gate 			    flags & ~RCM_FORCE, &rinfo);
12767c478bd9Sstevel@tonic-gate 		else
12777c478bd9Sstevel@tonic-gate 			rv = (*rcm->notify_remove_list)(hd, rlist,
12787c478bd9Sstevel@tonic-gate 			    flags & ~RCM_FORCE, &rinfo);
12797c478bd9Sstevel@tonic-gate 		break;
12807c478bd9Sstevel@tonic-gate 	}
12817c478bd9Sstevel@tonic-gate 	case CMD_RCM_SUSPEND: {
12827c478bd9Sstevel@tonic-gate 		timespec_t t;
12837c478bd9Sstevel@tonic-gate 		t.tv_sec = (time_t)0;
12847c478bd9Sstevel@tonic-gate 		t.tv_nsec = (long)0;
12857c478bd9Sstevel@tonic-gate 		rsrc = OS;
12867c478bd9Sstevel@tonic-gate 		ap_msg(a, MSG_ISSUE, cmd, rsrc);
12877c478bd9Sstevel@tonic-gate 		rv = (*rcm->request_suspend)(hd, rsrc, flags, &t, &rinfo);
12887c478bd9Sstevel@tonic-gate 		break;
12897c478bd9Sstevel@tonic-gate 	}
12907c478bd9Sstevel@tonic-gate 	case CMD_RCM_RESUME:
12917c478bd9Sstevel@tonic-gate 		rsrc = OS;
12927c478bd9Sstevel@tonic-gate 		ap_msg(a, MSG_ISSUE, cmd, rsrc);
12937c478bd9Sstevel@tonic-gate 		rv = (*rcm->notify_resume)(hd, rsrc, 0, &rinfo);
12947c478bd9Sstevel@tonic-gate 		break;
12957c478bd9Sstevel@tonic-gate 	default:
12967c478bd9Sstevel@tonic-gate 		ap_err(a, ERR_CMD_INVAL, cmd);
12977c478bd9Sstevel@tonic-gate 		return (CFGA_INVAL);
12987c478bd9Sstevel@tonic-gate 	}
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	if (rv != RCM_SUCCESS) {
13017c478bd9Sstevel@tonic-gate 		rcm->rinfo = rinfo;
13027c478bd9Sstevel@tonic-gate 		rcm->infot = NULL;
13037c478bd9Sstevel@tonic-gate 		ap_err(a, ERR_RCM_CMD, cmd);
13047c478bd9Sstevel@tonic-gate 		(*rcm->free_info)(rinfo);
13057c478bd9Sstevel@tonic-gate 		if (rc == CFGA_OK)
13067c478bd9Sstevel@tonic-gate 			rc = CFGA_LIB_ERROR;	/* make sure error is set */
13077c478bd9Sstevel@tonic-gate 	}
13087c478bd9Sstevel@tonic-gate 	if ((rc == CFGA_OK) && (noop == 0)) {
13097c478bd9Sstevel@tonic-gate 		if (rlist)
13107c478bd9Sstevel@tonic-gate 			for (i = 0; rlist[i]; i++)
13117c478bd9Sstevel@tonic-gate 				ap_msg(a, MSG_DONE, cmd, rlist[i]);
13127c478bd9Sstevel@tonic-gate 		else if (rsrc)
13137c478bd9Sstevel@tonic-gate 			ap_msg(a, MSG_DONE, cmd, rsrc);
13147c478bd9Sstevel@tonic-gate 		else
13157c478bd9Sstevel@tonic-gate 			ap_msg(a, MSG_DONE, cmd, a->target);
13167c478bd9Sstevel@tonic-gate 	}
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	return (rc);
13197c478bd9Sstevel@tonic-gate }
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate /*
13227c478bd9Sstevel@tonic-gate  * ap_rcm_info
13237c478bd9Sstevel@tonic-gate  *
13247c478bd9Sstevel@tonic-gate  * Takes an ap_id and a character pointer, and formats
13257c478bd9Sstevel@tonic-gate  * the rcm_info_t data in the form of a table to the given character pointer.
13267c478bd9Sstevel@tonic-gate  * Code duplicated from the scsi plugin.
13277c478bd9Sstevel@tonic-gate  * Note: This function will go away when a generic librcm callback is
13287c478bd9Sstevel@tonic-gate  *	implemented to format RCM messages for plugins.
13297c478bd9Sstevel@tonic-gate  */
13307c478bd9Sstevel@tonic-gate int
ap_rcm_info(apd_t * a,char ** msg)13317c478bd9Sstevel@tonic-gate ap_rcm_info(apd_t *a, char **msg)
13327c478bd9Sstevel@tonic-gate {
13337c478bd9Sstevel@tonic-gate 	rcmd_t *rcm;
13347c478bd9Sstevel@tonic-gate 	rcm_info_t *rinfo;
13357c478bd9Sstevel@tonic-gate 	int i;
13367c478bd9Sstevel@tonic-gate 	size_t w;
13377c478bd9Sstevel@tonic-gate 	size_t width = 0;
13387c478bd9Sstevel@tonic-gate 	size_t w_rsrc = 0;
13397c478bd9Sstevel@tonic-gate 	size_t w_info = 0;
13407c478bd9Sstevel@tonic-gate 	size_t msg_size = 0;
13417c478bd9Sstevel@tonic-gate 	uint_t tuples = 0;
13427c478bd9Sstevel@tonic-gate 	rcm_info_tuple_t *tuple = NULL;
13437c478bd9Sstevel@tonic-gate 	char *rsrc;
13447c478bd9Sstevel@tonic-gate 	char *info;
13457c478bd9Sstevel@tonic-gate 	char *newmsg;
13467c478bd9Sstevel@tonic-gate 	static char format[RCM_MAX_FORMAT];
13477c478bd9Sstevel@tonic-gate 	const char *infostr;
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	DBG("ap_rcm_info(%p)\n", (void *)a);
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	/* Protect against invalid arguments */
13537c478bd9Sstevel@tonic-gate 	if ((a == NULL) || ((rcm = (rcmd_t *)a->rcm) == NULL) ||
13547c478bd9Sstevel@tonic-gate 	    ((rinfo = rcm->rinfo) == NULL) || (msg == NULL)) {
13557c478bd9Sstevel@tonic-gate 		return (-1);
13567c478bd9Sstevel@tonic-gate 	}
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	/* Set localized table header strings */
13597c478bd9Sstevel@tonic-gate 	rsrc = dgettext(TEXT_DOMAIN, "Resource");
13607c478bd9Sstevel@tonic-gate 	info = dgettext(TEXT_DOMAIN, "Information");
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 	/* A first pass, to size up the RCM information */
13637c478bd9Sstevel@tonic-gate 	while (tuple = (*rcm->info_next)(rinfo, tuple)) {
13647c478bd9Sstevel@tonic-gate 		if ((infostr = (*rcm->info_info)(tuple)) != NULL) {
13657c478bd9Sstevel@tonic-gate 			tuples++;
13667c478bd9Sstevel@tonic-gate 			if ((w = strlen((*rcm->info_rsrc)(tuple))) > w_rsrc)
13677c478bd9Sstevel@tonic-gate 				w_rsrc = w;
13687c478bd9Sstevel@tonic-gate 			if ((w = strlen(infostr)) > w_info)
13697c478bd9Sstevel@tonic-gate 				w_info = w;
13707c478bd9Sstevel@tonic-gate 		}
13717c478bd9Sstevel@tonic-gate 	}
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	/* If nothing was sized up above, stop early */
13747c478bd9Sstevel@tonic-gate 	if (tuples == 0)
13757c478bd9Sstevel@tonic-gate 		return (0);
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 	/* Adjust column widths for column headings */
13787c478bd9Sstevel@tonic-gate 	if ((w = strlen(rsrc)) > w_rsrc)
13797c478bd9Sstevel@tonic-gate 		w_rsrc = w;
13807c478bd9Sstevel@tonic-gate 	else if ((w_rsrc - w) % 2)
13817c478bd9Sstevel@tonic-gate 		w_rsrc++;
13827c478bd9Sstevel@tonic-gate 	if ((w = strlen(info)) > w_info)
13837c478bd9Sstevel@tonic-gate 		w_info = w;
13847c478bd9Sstevel@tonic-gate 	else if ((w_info - w) % 2)
13857c478bd9Sstevel@tonic-gate 		w_info++;
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 	/*
13887c478bd9Sstevel@tonic-gate 	 * Compute the total line width of each line,
13897c478bd9Sstevel@tonic-gate 	 * accounting for intercolumn spacing.
13907c478bd9Sstevel@tonic-gate 	 */
13917c478bd9Sstevel@tonic-gate 	width = w_info + w_rsrc + 4;
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 	/* Allocate space for the table */
13947c478bd9Sstevel@tonic-gate 	msg_size = (2 + tuples) * (width + 1) + 2;
13957c478bd9Sstevel@tonic-gate 	if (*msg == NULL) {
13967c478bd9Sstevel@tonic-gate 		/* zero fill for the strcat() call below */
13977c478bd9Sstevel@tonic-gate 		*msg = calloc(msg_size, sizeof (char));
13987c478bd9Sstevel@tonic-gate 		if (*msg == NULL)
13997c478bd9Sstevel@tonic-gate 			return (-1);
14007c478bd9Sstevel@tonic-gate 	} else {
14017c478bd9Sstevel@tonic-gate 		newmsg = realloc(*msg, strlen(*msg) + msg_size);
14027c478bd9Sstevel@tonic-gate 		if (newmsg == NULL)
14037c478bd9Sstevel@tonic-gate 			return (-1);
14047c478bd9Sstevel@tonic-gate 		else
14057c478bd9Sstevel@tonic-gate 			*msg = newmsg;
14067c478bd9Sstevel@tonic-gate 	}
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	/* Place a table header into the string */
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	/* The resource header */
14117c478bd9Sstevel@tonic-gate 	(void) strcat(*msg, "\n");
14127c478bd9Sstevel@tonic-gate 	w = strlen(rsrc);
14137c478bd9Sstevel@tonic-gate 	for (i = 0; i < ((w_rsrc - w) / 2); i++)
14147c478bd9Sstevel@tonic-gate 		(void) strcat(*msg, " ");
14157c478bd9Sstevel@tonic-gate 	(void) strcat(*msg, rsrc);
14167c478bd9Sstevel@tonic-gate 	for (i = 0; i < ((w_rsrc - w) / 2); i++)
14177c478bd9Sstevel@tonic-gate 		(void) strcat(*msg, " ");
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	/* The information header */
14207c478bd9Sstevel@tonic-gate 	(void) strcat(*msg, "  ");
14217c478bd9Sstevel@tonic-gate 	w = strlen(info);
14227c478bd9Sstevel@tonic-gate 	for (i = 0; i < ((w_info - w) / 2); i++)
14237c478bd9Sstevel@tonic-gate 		(void) strcat(*msg, " ");
14247c478bd9Sstevel@tonic-gate 	(void) strcat(*msg, info);
14257c478bd9Sstevel@tonic-gate 	for (i = 0; i < ((w_info - w) / 2); i++)
14267c478bd9Sstevel@tonic-gate 		(void) strcat(*msg, " ");
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	/* Underline the headers */
14297c478bd9Sstevel@tonic-gate 	(void) strcat(*msg, "\n");
14307c478bd9Sstevel@tonic-gate 	for (i = 0; i < w_rsrc; i++)
14317c478bd9Sstevel@tonic-gate 		(void) strcat(*msg, "-");
14327c478bd9Sstevel@tonic-gate 	(void) strcat(*msg, "  ");
14337c478bd9Sstevel@tonic-gate 	for (i = 0; i < w_info; i++)
14347c478bd9Sstevel@tonic-gate 		(void) strcat(*msg, "-");
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	/* Construct the format string */
14377c478bd9Sstevel@tonic-gate 	(void) snprintf(format, RCM_MAX_FORMAT, "%%-%ds  %%-%ds",
14387c478bd9Sstevel@tonic-gate 	    (int)w_rsrc, (int)w_info);
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 	/* Add the tuples to the table string */
14417c478bd9Sstevel@tonic-gate 	tuple = NULL;
14427c478bd9Sstevel@tonic-gate 	while ((tuple = (*rcm->info_next)(rinfo, tuple)) != NULL) {
14437c478bd9Sstevel@tonic-gate 		if ((infostr = (*rcm->info_info)(tuple)) != NULL) {
14447c478bd9Sstevel@tonic-gate 			(void) strcat(*msg, "\n");
14457c478bd9Sstevel@tonic-gate 			(void) sprintf(&((*msg)[strlen(*msg)]), format,
14467c478bd9Sstevel@tonic-gate 			    (*rcm->info_rsrc)(tuple), infostr);
14477c478bd9Sstevel@tonic-gate 		}
14487c478bd9Sstevel@tonic-gate 	}
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 	DBG("ap_rcm_info(%p) success\n", (void *)a);
14517c478bd9Sstevel@tonic-gate 	return (0);
14527c478bd9Sstevel@tonic-gate }
1453