10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
55895Syz147064 * Common Development and Distribution License (the "License").
65895Syz147064 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
225895Syz147064 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <ctype.h>
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <stdarg.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <unistd.h>
320Sstevel@tonic-gate #include <macros.h>
330Sstevel@tonic-gate #include <errno.h>
340Sstevel@tonic-gate #include <kstat.h>
350Sstevel@tonic-gate #include <sys/kmem.h>
360Sstevel@tonic-gate #include <dlfcn.h>
370Sstevel@tonic-gate #include <libdevinfo.h>
380Sstevel@tonic-gate #include <librcm.h>
390Sstevel@tonic-gate #include <libintl.h>
400Sstevel@tonic-gate #define CFGA_PLUGIN_LIB
410Sstevel@tonic-gate #include <config_admin.h>
420Sstevel@tonic-gate #include <sys/sbd_ioctl.h>
430Sstevel@tonic-gate #include "ap.h"
440Sstevel@tonic-gate
450Sstevel@tonic-gate typedef int32_t cpuid_t;
460Sstevel@tonic-gate
470Sstevel@tonic-gate typedef struct {
480Sstevel@tonic-gate int valid;
490Sstevel@tonic-gate cfga_stat_t ostate;
500Sstevel@tonic-gate int ncap;
510Sstevel@tonic-gate union {
520Sstevel@tonic-gate long npages;
530Sstevel@tonic-gate cpuid_t cpuid[SBD_MAX_CORES_PER_CMP];
540Sstevel@tonic-gate } type;
550Sstevel@tonic-gate } cap_info_t;
560Sstevel@tonic-gate
570Sstevel@tonic-gate typedef struct {
580Sstevel@tonic-gate int firstcm; /* first component to operate on */
590Sstevel@tonic-gate int lastcm; /* last component to operate on */
600Sstevel@tonic-gate void *lib;
610Sstevel@tonic-gate char **rlist;
620Sstevel@tonic-gate cap_info_t *capinfo;
630Sstevel@tonic-gate int ncpus; /* # of CPUs in cpuids list */
640Sstevel@tonic-gate cpuid_t *cpuids; /* List of cpuids */
650Sstevel@tonic-gate int capcpus; /* # of CPUs - tracking capacity */
660Sstevel@tonic-gate int cappages; /* # of memory pages - tracking capacity */
670Sstevel@tonic-gate rcm_handle_t *hd;
680Sstevel@tonic-gate rcm_info_t *rinfo;
690Sstevel@tonic-gate rcm_info_tuple_t *infot;
700Sstevel@tonic-gate int (*alloc_handle)(char *, uint_t, void *, rcm_handle_t **);
710Sstevel@tonic-gate void (*free_handle)(rcm_handle_t *);
720Sstevel@tonic-gate int (*get_info)(rcm_handle_t *, char *, uint_t, rcm_info_t **);
730Sstevel@tonic-gate void (*free_info)(rcm_info_t *);
740Sstevel@tonic-gate rcm_info_tuple_t *(*info_next)(rcm_info_t *, rcm_info_tuple_t *);
750Sstevel@tonic-gate int (*info_state)(rcm_info_tuple_t *);
760Sstevel@tonic-gate pid_t (*info_pid)(rcm_info_tuple_t *);
770Sstevel@tonic-gate const char *(*info_error)(rcm_info_tuple_t *);
780Sstevel@tonic-gate const char *(*info_info)(rcm_info_tuple_t *);
790Sstevel@tonic-gate const char *(*info_rsrc)(rcm_info_tuple_t *);
800Sstevel@tonic-gate int (*request_offline_list)(rcm_handle_t *, char **, uint_t,
810Sstevel@tonic-gate rcm_info_t **);
820Sstevel@tonic-gate int (*notify_online_list)(rcm_handle_t *, char **, uint_t,
830Sstevel@tonic-gate rcm_info_t **);
840Sstevel@tonic-gate int (*request_suspend)(rcm_handle_t *, char *, uint_t, timespec_t *,
850Sstevel@tonic-gate rcm_info_t **);
860Sstevel@tonic-gate int (*notify_resume)(rcm_handle_t *, char *, uint_t, rcm_info_t **);
870Sstevel@tonic-gate int (*notify_remove_list)(rcm_handle_t *, char **, uint_t,
880Sstevel@tonic-gate rcm_info_t **);
890Sstevel@tonic-gate int (*request_capacity_change)(rcm_handle_t *, char *, uint_t,
900Sstevel@tonic-gate nvlist_t *, rcm_info_t **);
910Sstevel@tonic-gate int (*notify_capacity_change)(rcm_handle_t *, char *, uint_t,
920Sstevel@tonic-gate nvlist_t *, rcm_info_t **);
930Sstevel@tonic-gate } rcmd_t;
940Sstevel@tonic-gate
950Sstevel@tonic-gate static char *
960Sstevel@tonic-gate ap_rcm_ops[] = {
970Sstevel@tonic-gate "rcm_alloc_handle",
980Sstevel@tonic-gate "rcm_free_handle",
990Sstevel@tonic-gate "rcm_get_info",
1000Sstevel@tonic-gate "rcm_free_info",
1010Sstevel@tonic-gate "rcm_info_next",
1020Sstevel@tonic-gate "rcm_info_state",
1030Sstevel@tonic-gate "rcm_info_pid",
1040Sstevel@tonic-gate "rcm_info_error",
1050Sstevel@tonic-gate "rcm_info_info",
1060Sstevel@tonic-gate "rcm_info_rsrc",
1070Sstevel@tonic-gate "rcm_request_offline_list",
1080Sstevel@tonic-gate "rcm_notify_online_list",
1090Sstevel@tonic-gate "rcm_request_suspend",
1100Sstevel@tonic-gate "rcm_notify_resume",
1110Sstevel@tonic-gate "rcm_notify_remove_list",
1120Sstevel@tonic-gate "rcm_request_capacity_change",
1130Sstevel@tonic-gate "rcm_notify_capacity_change",
1140Sstevel@tonic-gate NULL
1150Sstevel@tonic-gate };
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate #define ALLOC_HANDLE 0
1180Sstevel@tonic-gate #define FREE_HANDLE 1
1190Sstevel@tonic-gate #define GET_INFO 2
1200Sstevel@tonic-gate #define FREE_INFO 3
1210Sstevel@tonic-gate #define INFO_TUPLE_NEXT 4
1220Sstevel@tonic-gate #define INFO_TUPLE_STATE 5
1230Sstevel@tonic-gate #define INFO_TUPLE_ID 6
1240Sstevel@tonic-gate #define INFO_TUPLE_ERROR 7
1250Sstevel@tonic-gate #define INFO_TUPLE_INFO 8
1260Sstevel@tonic-gate #define INFO_TUPLE_RSRC 9
1270Sstevel@tonic-gate #define REQUEST_OFFLINE 10
1280Sstevel@tonic-gate #define NOTIFY_ONLINE 11
1290Sstevel@tonic-gate #define REQUEST_SUSPEND 12
1300Sstevel@tonic-gate #define NOTIFY_RESUME 13
1310Sstevel@tonic-gate #define NOTIFY_REMOVE 14
1320Sstevel@tonic-gate #define REQUEST_CAP_CHANGE 15
1330Sstevel@tonic-gate #define NOTIFY_CAP_CHANGE 16
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate /*
1360Sstevel@tonic-gate * There is no consumer for SUNW_OS. This is defined here
1370Sstevel@tonic-gate * for generic OS quiescence.
1380Sstevel@tonic-gate */
1390Sstevel@tonic-gate #define OS "SUNW_OS" /* XXX */
1400Sstevel@tonic-gate
1410Sstevel@tonic-gate /* Max width of an RCM formatted message line */
1420Sstevel@tonic-gate #define RCM_MAX_FORMAT 80
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate #ifdef __sparcv9
1455895Syz147064 #define RCMLIB "/lib/sparcv9/librcm.so";
146*12004Sjiang.liu@intel.com #elif defined(__amd64)
147*12004Sjiang.liu@intel.com #define RCMLIB "/lib/amd64/librcm.so";
1480Sstevel@tonic-gate #else
1495895Syz147064 #define RCMLIB "/lib/librcm.so";
1500Sstevel@tonic-gate #endif
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate static cfga_err_t
ap_capinfo(apd_t * a,int firstcm,int lastcm,cap_info_t ** capinfo)1530Sstevel@tonic-gate ap_capinfo(apd_t *a, int firstcm, int lastcm, cap_info_t **capinfo)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate int cm;
1560Sstevel@tonic-gate int ncm;
1570Sstevel@tonic-gate void *cap;
1580Sstevel@tonic-gate int *ncap;
1590Sstevel@tonic-gate cfga_stat_t *os;
1600Sstevel@tonic-gate cap_info_t *cinfo, *cp;
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate DBG("ap_capinfo(%p)\n", (void *)a);
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate if (capinfo == NULL) {
1650Sstevel@tonic-gate ap_err(a, ERR_PLUGIN, "null capinfo");
1660Sstevel@tonic-gate return (CFGA_LIB_ERROR);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate * Assume there are components with valid capacity
1710Sstevel@tonic-gate * information and allocate space for them. If there
1720Sstevel@tonic-gate * are none at the end, free the allocated space.
1730Sstevel@tonic-gate */
1740Sstevel@tonic-gate ncm = lastcm - firstcm + 1;
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate cinfo = (cap_info_t *)calloc(ncm, sizeof (cap_info_t));
1770Sstevel@tonic-gate if (cinfo == NULL) {
1780Sstevel@tonic-gate ap_err(a, ERR_NOMEM);
1790Sstevel@tonic-gate return (CFGA_LIB_ERROR);
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate *capinfo = NULL;
1830Sstevel@tonic-gate ncm = 0;
1840Sstevel@tonic-gate for (cp = cinfo, cm = firstcm; cm <= lastcm; cm++, cp++) {
1850Sstevel@tonic-gate os = &cp->ostate;
1860Sstevel@tonic-gate ncap = &cp->ncap;
1870Sstevel@tonic-gate
1880Sstevel@tonic-gate switch (ap_cm_type(a, cm)) {
1890Sstevel@tonic-gate case AP_CPU:
1900Sstevel@tonic-gate case AP_CMP:
1910Sstevel@tonic-gate cap = (void *)(cp->type.cpuid);
1920Sstevel@tonic-gate break;
1930Sstevel@tonic-gate case AP_MEM:
1940Sstevel@tonic-gate cap = (void *)&(cp->type.npages);
1950Sstevel@tonic-gate break;
1960Sstevel@tonic-gate default:
1970Sstevel@tonic-gate continue;
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate /*
2000Sstevel@tonic-gate * Remember which components have valid
2010Sstevel@tonic-gate * capacity information.
2020Sstevel@tonic-gate */
2030Sstevel@tonic-gate if (ap_cm_capacity(a, cm, cap, ncap, os)) {
2040Sstevel@tonic-gate cp->valid = 1;
2050Sstevel@tonic-gate ncm++;
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate if (ncm == 0)
2100Sstevel@tonic-gate free(cinfo);
2110Sstevel@tonic-gate else
2120Sstevel@tonic-gate *capinfo = cinfo;
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate return (CFGA_OK);
2150Sstevel@tonic-gate }
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate static int
getsyscpuids(int * ncpuids,cpuid_t ** cpuids)2180Sstevel@tonic-gate getsyscpuids(int *ncpuids, cpuid_t **cpuids)
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate int ncpu;
2210Sstevel@tonic-gate int maxncpu;
2220Sstevel@tonic-gate kstat_t *ksp;
2230Sstevel@tonic-gate kstat_ctl_t *kc = NULL;
2240Sstevel@tonic-gate cpuid_t *cp;
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate DBG("getsyscpuids\n");
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate if ((maxncpu = sysconf(_SC_NPROCESSORS_MAX)) == -1 ||
2290Sstevel@tonic-gate (kc = kstat_open()) == NULL ||
2300Sstevel@tonic-gate (cp = (cpuid_t *)calloc(maxncpu, sizeof (cpuid_t))) == NULL) {
2310Sstevel@tonic-gate /* if calloc failed, clean up kstats */
2320Sstevel@tonic-gate if (kc != NULL) {
2330Sstevel@tonic-gate (void) kstat_close(kc);
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate return (-1);
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate DBG("syscpuids: ");
2390Sstevel@tonic-gate for (ncpu = 0, ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
2400Sstevel@tonic-gate if (strcmp(ksp->ks_module, "cpu_info") == 0) {
2410Sstevel@tonic-gate cp[ncpu++] = ksp->ks_instance;
2420Sstevel@tonic-gate DBG("%d ", ksp->ks_instance);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate DBG("\n");
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate (void) kstat_close(kc);
2480Sstevel@tonic-gate *cpuids = cp;
2490Sstevel@tonic-gate *ncpuids = ncpu;
2500Sstevel@tonic-gate return (0);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate cfga_err_t
ap_rcm_init(apd_t * a)2540Sstevel@tonic-gate ap_rcm_init(apd_t *a)
2550Sstevel@tonic-gate {
2560Sstevel@tonic-gate int i;
2570Sstevel@tonic-gate char *err;
2580Sstevel@tonic-gate char *rcmlib;
2590Sstevel@tonic-gate void *sym;
2600Sstevel@tonic-gate void *lib;
2610Sstevel@tonic-gate char **op;
2620Sstevel@tonic-gate rcmd_t *rcm;
2630Sstevel@tonic-gate cfga_err_t rc;
2640Sstevel@tonic-gate struct stat buf;
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate DBG("ap_rcm_init(%p)\n", (void *)a);
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate /*
2690Sstevel@tonic-gate * If the initial command is status, or the RCM feature is not
2700Sstevel@tonic-gate * available, or the RCM interface has already been initialized,
2710Sstevel@tonic-gate * just return.
2720Sstevel@tonic-gate */
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate if ((a->statonly != 0) || (a->norcm != 0) ||
2750Sstevel@tonic-gate ((rcm = (rcmd_t *)a->rcm) != NULL)) {
2760Sstevel@tonic-gate return (CFGA_OK);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate rcmlib = RCMLIB;
2800Sstevel@tonic-gate rc = CFGA_LIB_ERROR;
2810Sstevel@tonic-gate
2820Sstevel@tonic-gate DBG("Looking for %s\n", rcmlib);
2830Sstevel@tonic-gate /*
2840Sstevel@tonic-gate * If the library is not present, there is nothing more
2850Sstevel@tonic-gate * to do. The RCM offline/suspend steps become no-ops
2860Sstevel@tonic-gate * in that case.
2870Sstevel@tonic-gate */
2880Sstevel@tonic-gate if (stat(rcmlib, &buf) == -1) {
2890Sstevel@tonic-gate if (errno == ENOENT) {
2900Sstevel@tonic-gate a->norcm++;
2910Sstevel@tonic-gate ap_msg(a, MSG_NORCM);
2920Sstevel@tonic-gate return (CFGA_OK);
2930Sstevel@tonic-gate } else {
2940Sstevel@tonic-gate ap_err(a, ERR_STAT, rcmlib);
2950Sstevel@tonic-gate return (rc);
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate DBG("%s found\n", rcmlib);
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate if ((a->rcm = calloc(1, sizeof (rcmd_t))) == NULL) {
3010Sstevel@tonic-gate ap_err(a, ERR_NOMEM);
3020Sstevel@tonic-gate return (rc);
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate
3050Sstevel@tonic-gate rcm = (rcmd_t *)a->rcm;
3060Sstevel@tonic-gate
3070Sstevel@tonic-gate if ((lib = dlopen(rcmlib, RTLD_NOW)) == NULL) {
3080Sstevel@tonic-gate if ((err = dlerror()) != NULL)
3090Sstevel@tonic-gate err = strdup(err);
3100Sstevel@tonic-gate ap_err(a, ERR_LIB_OPEN, rcmlib, err);
3110Sstevel@tonic-gate if (err != NULL)
3120Sstevel@tonic-gate free(err);
3130Sstevel@tonic-gate return (rc);
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate rcm->lib = lib;
3170Sstevel@tonic-gate
3180Sstevel@tonic-gate for (i = 0, op = ap_rcm_ops; *op != NULL; op++, i++) {
3190Sstevel@tonic-gate if ((sym = dlsym(lib, *op)) == NULL) {
3200Sstevel@tonic-gate ap_err(a, ERR_LIB_SYM, rcmlib, *op);
3210Sstevel@tonic-gate return (rc);
3220Sstevel@tonic-gate }
3230Sstevel@tonic-gate switch (i) {
3240Sstevel@tonic-gate case ALLOC_HANDLE:
3250Sstevel@tonic-gate rcm->alloc_handle = (int(*)
326*12004Sjiang.liu@intel.com (char *, uint_t, void *, rcm_handle_t **))sym;
3270Sstevel@tonic-gate break;
3280Sstevel@tonic-gate case FREE_HANDLE:
3290Sstevel@tonic-gate rcm->free_handle = (void (*)(rcm_handle_t *))sym;
3300Sstevel@tonic-gate break;
3310Sstevel@tonic-gate case GET_INFO:
3320Sstevel@tonic-gate rcm->get_info = (int (*)
333*12004Sjiang.liu@intel.com (rcm_handle_t *, char *, uint_t, rcm_info_t **))sym;
3340Sstevel@tonic-gate break;
3350Sstevel@tonic-gate case FREE_INFO:
3360Sstevel@tonic-gate rcm->free_info = (void (*)(rcm_info_t *))sym;
3370Sstevel@tonic-gate break;
3380Sstevel@tonic-gate case INFO_TUPLE_NEXT:
3390Sstevel@tonic-gate rcm->info_next = (rcm_info_tuple_t *(*)
340*12004Sjiang.liu@intel.com (rcm_info_t *, rcm_info_tuple_t *))sym;
3410Sstevel@tonic-gate break;
3420Sstevel@tonic-gate case INFO_TUPLE_STATE:
3430Sstevel@tonic-gate rcm->info_state = (int (*)(rcm_info_tuple_t *))sym;
3440Sstevel@tonic-gate break;
3450Sstevel@tonic-gate case INFO_TUPLE_ID:
3460Sstevel@tonic-gate rcm->info_pid = (pid_t (*)(rcm_info_tuple_t *))sym;
3470Sstevel@tonic-gate break;
3480Sstevel@tonic-gate case INFO_TUPLE_ERROR:
3490Sstevel@tonic-gate rcm->info_error = (const char *(*)
350*12004Sjiang.liu@intel.com (rcm_info_tuple_t *))sym;
3510Sstevel@tonic-gate break;
3520Sstevel@tonic-gate case INFO_TUPLE_INFO:
3530Sstevel@tonic-gate rcm->info_info = (const char *(*)
354*12004Sjiang.liu@intel.com (rcm_info_tuple_t *))sym;
3550Sstevel@tonic-gate break;
3560Sstevel@tonic-gate case INFO_TUPLE_RSRC:
3570Sstevel@tonic-gate rcm->info_rsrc = (const char *(*)
358*12004Sjiang.liu@intel.com (rcm_info_tuple_t *))sym;
3590Sstevel@tonic-gate break;
3600Sstevel@tonic-gate case REQUEST_OFFLINE:
3610Sstevel@tonic-gate rcm->request_offline_list = (int (*)
362*12004Sjiang.liu@intel.com (rcm_handle_t *, char **, uint_t,
363*12004Sjiang.liu@intel.com rcm_info_t **))sym;
3640Sstevel@tonic-gate break;
3650Sstevel@tonic-gate case NOTIFY_ONLINE:
3660Sstevel@tonic-gate rcm->notify_online_list = (int (*)
367*12004Sjiang.liu@intel.com (rcm_handle_t *, char **, uint_t,
368*12004Sjiang.liu@intel.com rcm_info_t **))sym;
3690Sstevel@tonic-gate break;
3700Sstevel@tonic-gate case REQUEST_SUSPEND:
3710Sstevel@tonic-gate rcm->request_suspend = (int (*)
372*12004Sjiang.liu@intel.com (rcm_handle_t *, char *, uint_t,
373*12004Sjiang.liu@intel.com timespec_t *, rcm_info_t **))sym;
3740Sstevel@tonic-gate break;
3750Sstevel@tonic-gate case NOTIFY_RESUME:
3760Sstevel@tonic-gate rcm->notify_resume = (int (*)
377*12004Sjiang.liu@intel.com (rcm_handle_t *, char *, uint_t,
378*12004Sjiang.liu@intel.com rcm_info_t **))sym;
3790Sstevel@tonic-gate break;
3800Sstevel@tonic-gate case NOTIFY_REMOVE:
3810Sstevel@tonic-gate rcm->notify_remove_list = (int (*)
382*12004Sjiang.liu@intel.com (rcm_handle_t *, char **, uint_t,
383*12004Sjiang.liu@intel.com rcm_info_t **))sym;
3840Sstevel@tonic-gate break;
3850Sstevel@tonic-gate case REQUEST_CAP_CHANGE:
3860Sstevel@tonic-gate rcm->request_capacity_change = (int (*)
387*12004Sjiang.liu@intel.com (rcm_handle_t *, char *, uint_t,
388*12004Sjiang.liu@intel.com nvlist_t *, rcm_info_t **))sym;
3890Sstevel@tonic-gate break;
3900Sstevel@tonic-gate case NOTIFY_CAP_CHANGE:
3910Sstevel@tonic-gate rcm->notify_capacity_change = (int (*)
392*12004Sjiang.liu@intel.com (rcm_handle_t *, char *, uint_t,
393*12004Sjiang.liu@intel.com nvlist_t *, rcm_info_t **))sym;
3940Sstevel@tonic-gate break;
3950Sstevel@tonic-gate default:
3960Sstevel@tonic-gate break;
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate
4000Sstevel@tonic-gate if (rcm->alloc_handle == NULL ||
4010Sstevel@tonic-gate (*rcm->alloc_handle)(NULL, RCM_NOPID, NULL, &rcm->hd)
402*12004Sjiang.liu@intel.com != RCM_SUCCESS) {
4030Sstevel@tonic-gate ap_err(a, ERR_RCM_HANDLE);
4040Sstevel@tonic-gate return (CFGA_LIB_ERROR);
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate
4070Sstevel@tonic-gate /*
4080Sstevel@tonic-gate * Offlining/onlining a board means offlining/onlining
4090Sstevel@tonic-gate * all components on the board. When operating on a
4100Sstevel@tonic-gate * single component no component sequence number is
4110Sstevel@tonic-gate * needed since the default is the current (target)
4120Sstevel@tonic-gate * component.
4130Sstevel@tonic-gate */
4140Sstevel@tonic-gate if (a->tgt == AP_BOARD) {
4150Sstevel@tonic-gate rcm->firstcm = 0;
4160Sstevel@tonic-gate rcm->lastcm = a->ncm - 1;
4170Sstevel@tonic-gate } else {
4180Sstevel@tonic-gate rcm->firstcm = CM_DFLT;
4190Sstevel@tonic-gate rcm->lastcm = CM_DFLT;
4200Sstevel@tonic-gate }
4210Sstevel@tonic-gate
4220Sstevel@tonic-gate if (rcm->cpuids == NULL) {
4230Sstevel@tonic-gate int cm;
4240Sstevel@tonic-gate int ncpu;
4250Sstevel@tonic-gate
4260Sstevel@tonic-gate /*
4270Sstevel@tonic-gate * Allocate space for the cpu capacity change info.
4280Sstevel@tonic-gate * Not every cpu may be relevant to the capacity
4290Sstevel@tonic-gate * request, but allocating for the maximum makes
4300Sstevel@tonic-gate * it easier, and the space is insignifcant.
4310Sstevel@tonic-gate */
4320Sstevel@tonic-gate for (ncpu = 0, cm = rcm->firstcm; cm <= rcm->lastcm; cm++) {
4330Sstevel@tonic-gate
4340Sstevel@tonic-gate ap_target_t type = ap_cm_type(a, cm);
4350Sstevel@tonic-gate
4360Sstevel@tonic-gate if ((type == AP_CPU) || (type == AP_CMP)) {
4370Sstevel@tonic-gate ncpu += ap_cm_ncap(a, cm);
4380Sstevel@tonic-gate }
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate
4410Sstevel@tonic-gate rcm->ncpus = ncpu;
4420Sstevel@tonic-gate if ((rcm->cpuids = (cpuid_t *)calloc(ncpu, sizeof (cpuid_t)))
443*12004Sjiang.liu@intel.com == NULL) {
4440Sstevel@tonic-gate ap_err(a, ERR_NOMEM);
4450Sstevel@tonic-gate return (CFGA_LIB_ERROR);
4460Sstevel@tonic-gate }
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate
4490Sstevel@tonic-gate /*
4500Sstevel@tonic-gate * Remember initial capacity information.
4510Sstevel@tonic-gate * This information is based on the initial
4520Sstevel@tonic-gate * state of the ap_id, i.e. before any
4530Sstevel@tonic-gate * state change change operations were
4540Sstevel@tonic-gate * executed. We will later get the
4550Sstevel@tonic-gate * current capacity information in order
4560Sstevel@tonic-gate * to figure out exactly what has changed
4570Sstevel@tonic-gate * as the result of the executed command
4580Sstevel@tonic-gate * sequence.
4590Sstevel@tonic-gate */
4600Sstevel@tonic-gate rc = ap_capinfo(a, rcm->firstcm, rcm->lastcm, &rcm->capinfo);
4610Sstevel@tonic-gate
4620Sstevel@tonic-gate rcm->capcpus = sysconf(_SC_NPROCESSORS_CONF);
4630Sstevel@tonic-gate rcm->cappages = sysconf(_SC_PHYS_PAGES);
4640Sstevel@tonic-gate
4650Sstevel@tonic-gate return (rc);
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate
4680Sstevel@tonic-gate void
ap_rcm_fini(apd_t * a)4690Sstevel@tonic-gate ap_rcm_fini(apd_t *a)
4700Sstevel@tonic-gate {
4710Sstevel@tonic-gate rcmd_t *rcm;
4720Sstevel@tonic-gate char **rp;
4730Sstevel@tonic-gate
4740Sstevel@tonic-gate DBG("ap_rcm_fini(%p)\n", (void *)a);
4750Sstevel@tonic-gate
4760Sstevel@tonic-gate if ((rcm = (rcmd_t *)a->rcm) == NULL)
4770Sstevel@tonic-gate return;
4780Sstevel@tonic-gate
4790Sstevel@tonic-gate if (rcm->hd)
4800Sstevel@tonic-gate (*rcm->free_handle)(rcm->hd);
4810Sstevel@tonic-gate
4820Sstevel@tonic-gate (void) dlclose(rcm->lib);
4830Sstevel@tonic-gate
4840Sstevel@tonic-gate /*
4850Sstevel@tonic-gate * Free all the names in the resource list, followed
4860Sstevel@tonic-gate * by the resource list itself.
4870Sstevel@tonic-gate */
4880Sstevel@tonic-gate if (rcm->rlist)
4890Sstevel@tonic-gate for (rp = rcm->rlist; *rp; rp++)
4900Sstevel@tonic-gate s_free(*rp);
4910Sstevel@tonic-gate s_free(rcm->rlist);
4920Sstevel@tonic-gate s_free(rcm->cpuids);
4930Sstevel@tonic-gate s_free(rcm->capinfo);
4940Sstevel@tonic-gate s_free(a->rcm);
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate
4970Sstevel@tonic-gate static cfga_err_t
ap_rcm_rlist(apd_t * a,int firstcm,int lastcm,char *** rlist,int cmd)4980Sstevel@tonic-gate ap_rcm_rlist(apd_t *a, int firstcm, int lastcm, char ***rlist, int cmd)
4990Sstevel@tonic-gate {
5000Sstevel@tonic-gate int n;
5010Sstevel@tonic-gate int cm;
5020Sstevel@tonic-gate int ncap;
5030Sstevel@tonic-gate char *path;
5040Sstevel@tonic-gate char *cpuname;
5050Sstevel@tonic-gate char **rp;
5060Sstevel@tonic-gate
5070Sstevel@tonic-gate DBG("ap_rcm_rlist(%p)\n", (void *)a);
5080Sstevel@tonic-gate
5090Sstevel@tonic-gate /*
5100Sstevel@tonic-gate * Allocate space for the maximum number of components
5110Sstevel@tonic-gate * that can be affected by this operation.
5120Sstevel@tonic-gate */
5130Sstevel@tonic-gate for (ncap = 0, cm = firstcm; cm <= lastcm; cm++) {
5140Sstevel@tonic-gate ncap += ap_cm_ncap(a, cm);
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate
5170Sstevel@tonic-gate DBG("ncap=%d\n", ncap);
5180Sstevel@tonic-gate
5190Sstevel@tonic-gate if ((rp = (char **)calloc(ncap + 1, sizeof (char *))) == NULL) {
5200Sstevel@tonic-gate ap_err(a, ERR_NOMEM);
5210Sstevel@tonic-gate return (CFGA_LIB_ERROR);
5220Sstevel@tonic-gate }
5230Sstevel@tonic-gate
5240Sstevel@tonic-gate n = 12; /* SUNW_cpu/cpuCCC */
5250Sstevel@tonic-gate /* <--- 12 ---> */
5260Sstevel@tonic-gate cpuname = "SUNW_cpu/cpuCCC";
5270Sstevel@tonic-gate /*
5280Sstevel@tonic-gate * Set the RCM resource name for each component:
5290Sstevel@tonic-gate *
5300Sstevel@tonic-gate * io: <device-path>
5310Sstevel@tonic-gate * cpu: SUNW_cpu/cpu<cpuid>
5320Sstevel@tonic-gate *
5330Sstevel@tonic-gate */
5340Sstevel@tonic-gate for (ncap = 0, cm = firstcm; cm <= lastcm; cm++) {
5350Sstevel@tonic-gate switch (ap_cm_type(a, cm)) {
5360Sstevel@tonic-gate case AP_CPU:
5370Sstevel@tonic-gate case AP_CMP: {
5380Sstevel@tonic-gate int i;
5390Sstevel@tonic-gate int len;
5400Sstevel@tonic-gate cap_info_t cap;
5410Sstevel@tonic-gate cfga_stat_t os;
5420Sstevel@tonic-gate cpuid_t *cpuid;
5430Sstevel@tonic-gate int *nc;
5440Sstevel@tonic-gate cap_info_t *prevcap;
5450Sstevel@tonic-gate rcmd_t *rcm;
5460Sstevel@tonic-gate int allow_op;
5470Sstevel@tonic-gate int capindex;
5480Sstevel@tonic-gate
5490Sstevel@tonic-gate cpuid = cap.type.cpuid;
5500Sstevel@tonic-gate nc = &cap.ncap;
5510Sstevel@tonic-gate
5520Sstevel@tonic-gate /*
5530Sstevel@tonic-gate * See if the request target is a single
5540Sstevel@tonic-gate * (default) component
5550Sstevel@tonic-gate */
5560Sstevel@tonic-gate capindex = (cm == CM_DFLT) ? 0 : cm;
5570Sstevel@tonic-gate
5580Sstevel@tonic-gate /* Get the previous capacity info */
5590Sstevel@tonic-gate rcm = (rcmd_t *)a->rcm;
5600Sstevel@tonic-gate prevcap = rcm->capinfo;
5610Sstevel@tonic-gate
5620Sstevel@tonic-gate if (!ap_cm_capacity(a, cm, cpuid, nc, &os)) {
5630Sstevel@tonic-gate break;
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate
5660Sstevel@tonic-gate len = (strlen(cpuname) - n) + 1;
5670Sstevel@tonic-gate
5680Sstevel@tonic-gate /*
5690Sstevel@tonic-gate * For CMD_RCM_OFFLINE and REMOVE, add the CPU to the
5700Sstevel@tonic-gate * list if it is currently configured. For
5710Sstevel@tonic-gate * CMD_RCM_ONLINE, do so only if the state has changed
5720Sstevel@tonic-gate * to CFGA_STAT_CONFIGURED.
5730Sstevel@tonic-gate */
5740Sstevel@tonic-gate allow_op = 0;
5750Sstevel@tonic-gate if ((cmd == CMD_RCM_OFFLINE) ||
5760Sstevel@tonic-gate (cmd == CMD_RCM_REMOVE)) {
5770Sstevel@tonic-gate if (os == CFGA_STAT_CONFIGURED)
5780Sstevel@tonic-gate allow_op = 1;
5790Sstevel@tonic-gate } else {
5800Sstevel@tonic-gate if ((os == CFGA_STAT_CONFIGURED) &&
5810Sstevel@tonic-gate ((prevcap == NULL) ||
5820Sstevel@tonic-gate (prevcap[capindex].ostate != os)))
5830Sstevel@tonic-gate allow_op = 1;
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate
5860Sstevel@tonic-gate if (allow_op) {
5870Sstevel@tonic-gate for (i = 0; i < *nc; i++) {
5880Sstevel@tonic-gate if ((path = strdup(cpuname)) == NULL) {
5890Sstevel@tonic-gate ap_err(a, ERR_NOMEM);
5900Sstevel@tonic-gate return (CFGA_LIB_ERROR);
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate (void) snprintf(&path[n], len, "%d",
5930Sstevel@tonic-gate cpuid[i]);
5940Sstevel@tonic-gate
5950Sstevel@tonic-gate DBG("rp[%d]=%s\n", ncap, path);
5960Sstevel@tonic-gate rp[ncap++] = path;
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate break;
6000Sstevel@tonic-gate }
6010Sstevel@tonic-gate case AP_IO:
6020Sstevel@tonic-gate if ((path = ap_cm_devpath(a, cm)) != NULL) {
6030Sstevel@tonic-gate DBG("rp[%d]=%s\n", ncap, path);
6040Sstevel@tonic-gate rp[ncap++] = path;
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate break;
6070Sstevel@tonic-gate case AP_MEM:
6080Sstevel@tonic-gate /*
6090Sstevel@tonic-gate * Nothing to do for AP_MEM since only capacity
6100Sstevel@tonic-gate * change notifications apply to SUNW_memory
6110Sstevel@tonic-gate */
6120Sstevel@tonic-gate default:
6130Sstevel@tonic-gate break;
6140Sstevel@tonic-gate }
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate
6170Sstevel@tonic-gate rp[ncap] = NULL;
6180Sstevel@tonic-gate if (rlist)
6190Sstevel@tonic-gate *rlist = rp;
6200Sstevel@tonic-gate return (CFGA_OK);
6210Sstevel@tonic-gate }
6220Sstevel@tonic-gate
6230Sstevel@tonic-gate /*
6240Sstevel@tonic-gate * Returns 1 if the cpu ID 'cpuid' is in the list of CPU IDs
6250Sstevel@tonic-gate * 'list' of length 'length'. Returns 0 otherwise.
6260Sstevel@tonic-gate */
6270Sstevel@tonic-gate static int
is_cpu_in_list(cpuid_t cpuid,cpuid_t * list,int length)6280Sstevel@tonic-gate is_cpu_in_list(cpuid_t cpuid, cpuid_t *list, int length)
6290Sstevel@tonic-gate {
6300Sstevel@tonic-gate int i;
6310Sstevel@tonic-gate
6320Sstevel@tonic-gate DBG("is_cpu_in_list\n");
6330Sstevel@tonic-gate
6340Sstevel@tonic-gate if (list == NULL)
6350Sstevel@tonic-gate return (0);
6360Sstevel@tonic-gate
6370Sstevel@tonic-gate for (i = 0; i < length; i++) {
6380Sstevel@tonic-gate if (list[i] == cpuid)
6390Sstevel@tonic-gate return (1);
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate return (0);
6420Sstevel@tonic-gate }
6430Sstevel@tonic-gate
6440Sstevel@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)6450Sstevel@tonic-gate ap_rcm_cap_cpu(apd_t *a, rcmd_t *rcm, rcm_handle_t *hd, uint_t flags,
6460Sstevel@tonic-gate rcm_info_t **rinfo, int cmd, int change)
6470Sstevel@tonic-gate {
6480Sstevel@tonic-gate int i;
6490Sstevel@tonic-gate int rv = RCM_FAILURE;
6500Sstevel@tonic-gate int ncpuids;
6510Sstevel@tonic-gate int oldncpuids;
6520Sstevel@tonic-gate int newncpuids;
6530Sstevel@tonic-gate char buf[32];
6540Sstevel@tonic-gate const char *fmt;
6550Sstevel@tonic-gate size_t size;
6560Sstevel@tonic-gate nvlist_t *nvl = NULL;
6570Sstevel@tonic-gate cpuid_t *cpuids = NULL;
6580Sstevel@tonic-gate cpuid_t *oldcpuids = NULL;
6590Sstevel@tonic-gate cpuid_t *newcpuids = NULL;
6600Sstevel@tonic-gate
6610Sstevel@tonic-gate DBG("ap_rcm_cap_cpu(%p)\n", (void *)a);
6620Sstevel@tonic-gate
6630Sstevel@tonic-gate /*
6640Sstevel@tonic-gate * Get the current number of configured cpus.
6650Sstevel@tonic-gate */
6660Sstevel@tonic-gate if (getsyscpuids(&ncpuids, &cpuids) == -1)
6670Sstevel@tonic-gate return (rv);
6680Sstevel@tonic-gate else if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
6690Sstevel@tonic-gate free(cpuids);
6700Sstevel@tonic-gate goto done;
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate
6730Sstevel@tonic-gate if (change == 1)
6740Sstevel@tonic-gate fmt = "(%d cpu)";
6750Sstevel@tonic-gate else
6760Sstevel@tonic-gate fmt = "(%d cpus)";
6770Sstevel@tonic-gate
6780Sstevel@tonic-gate size = sizeof (cpuid_t);
6790Sstevel@tonic-gate
6800Sstevel@tonic-gate if (cmd == CMD_RCM_CAP_DEL) {
6810Sstevel@tonic-gate /*
6820Sstevel@tonic-gate * A delete request. rcm->cpuids represents the
6830Sstevel@tonic-gate * cpus that will be unconfigured. The current
6840Sstevel@tonic-gate * set of cpus, before the unconfigure operation,
6850Sstevel@tonic-gate * are the old CPUs. The new CPUs are those
6860Sstevel@tonic-gate * that would remain.
6870Sstevel@tonic-gate */
6880Sstevel@tonic-gate oldncpuids = ncpuids;
6890Sstevel@tonic-gate oldcpuids = cpuids;
6900Sstevel@tonic-gate
6910Sstevel@tonic-gate /*
6920Sstevel@tonic-gate * Fill newcpuids with the CPU IDs in the cpuids array,
6930Sstevel@tonic-gate * but not in rcm->cpuids.
6940Sstevel@tonic-gate */
6950Sstevel@tonic-gate newcpuids = (cpuid_t *)calloc(ncpuids, size);
6960Sstevel@tonic-gate if (newcpuids == NULL)
6970Sstevel@tonic-gate goto done;
6980Sstevel@tonic-gate
6990Sstevel@tonic-gate newncpuids = 0;
7000Sstevel@tonic-gate for (i = 0; i < ncpuids; i++) {
7010Sstevel@tonic-gate if (!is_cpu_in_list(cpuids[i], rcm->cpuids, change))
7020Sstevel@tonic-gate newcpuids[newncpuids++] = cpuids[i];
7030Sstevel@tonic-gate }
7040Sstevel@tonic-gate } else if (cmd == CMD_RCM_CAP_NOTIFY) {
7050Sstevel@tonic-gate /*
7060Sstevel@tonic-gate * An unconfigure capacity change notification. This
7070Sstevel@tonic-gate * notification is sent after a DR unconfigure, whether
7080Sstevel@tonic-gate * or not the DR was successful. rcm->cpuids represents
7090Sstevel@tonic-gate * the CPUs that have been unconfigured.
7100Sstevel@tonic-gate */
7110Sstevel@tonic-gate
7120Sstevel@tonic-gate /* New CPU IDs are the CPUs configured right now. */
7130Sstevel@tonic-gate newncpuids = ncpuids;
7140Sstevel@tonic-gate newcpuids = cpuids;
7150Sstevel@tonic-gate
7160Sstevel@tonic-gate /*
7170Sstevel@tonic-gate * Old CPU IDs are the CPUs configured right now
7180Sstevel@tonic-gate * in addition to those that have been unconfigured.
7190Sstevel@tonic-gate * We build the old CPU ID list by concatenating
7200Sstevel@tonic-gate * cpuids and rcm->cpuids.
7210Sstevel@tonic-gate */
7220Sstevel@tonic-gate oldcpuids = (cpuid_t *)calloc(ncpuids + change, size);
7230Sstevel@tonic-gate if (oldcpuids == NULL)
7240Sstevel@tonic-gate goto done;
7250Sstevel@tonic-gate
7260Sstevel@tonic-gate oldncpuids = 0;
7270Sstevel@tonic-gate for (i = 0; i < ncpuids; i++) {
7280Sstevel@tonic-gate if (!is_cpu_in_list(cpuids[i], rcm->cpuids, change))
7290Sstevel@tonic-gate oldcpuids[oldncpuids++] = cpuids[i];
7300Sstevel@tonic-gate }
7310Sstevel@tonic-gate for (i = 0; i < change; i++)
7320Sstevel@tonic-gate oldcpuids[oldncpuids++] = rcm->cpuids[i];
7330Sstevel@tonic-gate } else {
7340Sstevel@tonic-gate DBG("ap_rcm_cap_cpu: CPU capacity, old = %d, new = %d \n",
7350Sstevel@tonic-gate rcm->capcpus, ncpuids);
7360Sstevel@tonic-gate if (rcm->capcpus == ncpuids) {
7370Sstevel@tonic-gate /* No real change in CPU capacity */
7380Sstevel@tonic-gate rv = RCM_SUCCESS;
7390Sstevel@tonic-gate goto done;
7400Sstevel@tonic-gate }
7410Sstevel@tonic-gate
7420Sstevel@tonic-gate /*
7430Sstevel@tonic-gate * An add notification. rcm->cpuids represents the
7440Sstevel@tonic-gate * cpus that have been configured. The current
7450Sstevel@tonic-gate * set of cpus, after the configure operation,
7460Sstevel@tonic-gate * are the new CPU IDs.
7470Sstevel@tonic-gate */
7480Sstevel@tonic-gate newncpuids = ncpuids;
7490Sstevel@tonic-gate newcpuids = cpuids;
7500Sstevel@tonic-gate
7510Sstevel@tonic-gate /*
7520Sstevel@tonic-gate * Fill oldcpuids with the CPU IDs in the cpuids array,
7530Sstevel@tonic-gate * but not in rcm->cpuids.
7540Sstevel@tonic-gate */
7550Sstevel@tonic-gate oldcpuids = (cpuid_t *)calloc(ncpuids, size);
7560Sstevel@tonic-gate if (oldcpuids == NULL)
7570Sstevel@tonic-gate goto done;
7580Sstevel@tonic-gate
7590Sstevel@tonic-gate oldncpuids = 0;
7600Sstevel@tonic-gate for (i = 0; i < ncpuids; i++) {
7610Sstevel@tonic-gate if (!is_cpu_in_list(cpuids[i], rcm->cpuids, change))
7620Sstevel@tonic-gate oldcpuids[oldncpuids++] = cpuids[i];
7630Sstevel@tonic-gate }
7640Sstevel@tonic-gate }
7650Sstevel@tonic-gate
7660Sstevel@tonic-gate DBG("oldcpuids: ");
7670Sstevel@tonic-gate for (i = 0; i < oldncpuids; i++)
7680Sstevel@tonic-gate DBG("%d ", oldcpuids[i]);
7690Sstevel@tonic-gate DBG("\n");
7700Sstevel@tonic-gate DBG("change : ");
7710Sstevel@tonic-gate for (i = 0; i < change; i++)
7720Sstevel@tonic-gate DBG("%d ", rcm->cpuids[i]);
7730Sstevel@tonic-gate DBG("\n");
7740Sstevel@tonic-gate DBG("newcpuids: ");
7750Sstevel@tonic-gate for (i = 0; i < newncpuids; i++)
7760Sstevel@tonic-gate DBG("%d ", newcpuids[i]);
7770Sstevel@tonic-gate DBG("\n");
7780Sstevel@tonic-gate
7790Sstevel@tonic-gate if (nvlist_add_string(nvl, "state", "capacity") != 0 ||
7800Sstevel@tonic-gate nvlist_add_int32(nvl, "old_total", oldncpuids) != 0 ||
7810Sstevel@tonic-gate nvlist_add_int32(nvl, "new_total", newncpuids) != 0 ||
7820Sstevel@tonic-gate nvlist_add_int32_array(nvl, "old_cpu_list", oldcpuids,
783*12004Sjiang.liu@intel.com oldncpuids) != 0 ||
7840Sstevel@tonic-gate nvlist_add_int32_array(nvl, "new_cpu_list", newcpuids,
785*12004Sjiang.liu@intel.com newncpuids) != 0)
7860Sstevel@tonic-gate goto done;
7870Sstevel@tonic-gate
788*12004Sjiang.liu@intel.com (void) snprintf(buf, sizeof (buf), fmt, change);
7890Sstevel@tonic-gate ap_msg(a, MSG_ISSUE, cmd, buf);
7900Sstevel@tonic-gate
791*12004Sjiang.liu@intel.com if (cmd == CMD_RCM_CAP_DEL) {
7920Sstevel@tonic-gate rv = (*rcm->request_capacity_change)(hd, "SUNW_cpu",
793*12004Sjiang.liu@intel.com flags, nvl, rinfo);
794*12004Sjiang.liu@intel.com } else {
7950Sstevel@tonic-gate rv = (*rcm->notify_capacity_change)(hd, "SUNW_cpu",
796*12004Sjiang.liu@intel.com flags & ~RCM_FORCE, nvl, rinfo);
797*12004Sjiang.liu@intel.com }
7980Sstevel@tonic-gate
7990Sstevel@tonic-gate done:
8000Sstevel@tonic-gate if (nvl)
8010Sstevel@tonic-gate nvlist_free(nvl);
8020Sstevel@tonic-gate s_free(oldcpuids);
8030Sstevel@tonic-gate s_free(newcpuids);
8040Sstevel@tonic-gate return (rv);
8050Sstevel@tonic-gate }
8060Sstevel@tonic-gate
8070Sstevel@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)8080Sstevel@tonic-gate ap_rcm_cap_mem(apd_t *a, rcmd_t *rcm, rcm_handle_t *hd, uint_t flags,
8090Sstevel@tonic-gate rcm_info_t **rinfo, int cmd, long change)
8100Sstevel@tonic-gate {
8110Sstevel@tonic-gate int rv;
8120Sstevel@tonic-gate int pgsize;
8130Sstevel@tonic-gate long oldpages;
8140Sstevel@tonic-gate long newpages;
8150Sstevel@tonic-gate long currpages;
8160Sstevel@tonic-gate char buf[32];
8170Sstevel@tonic-gate nvlist_t *nvl;
8180Sstevel@tonic-gate
8190Sstevel@tonic-gate DBG("ap_rcm_cap_mem(%p)\n", (void *)a);
8200Sstevel@tonic-gate
8210Sstevel@tonic-gate /*
8220Sstevel@tonic-gate * Get the current amount of configured memory.
8230Sstevel@tonic-gate */
8240Sstevel@tonic-gate if ((pgsize = sysconf(_SC_PAGE_SIZE)) == -1 ||
8250Sstevel@tonic-gate (currpages = sysconf(_SC_PHYS_PAGES)) == -1 ||
8260Sstevel@tonic-gate nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) > 0)
8270Sstevel@tonic-gate return (RCM_FAILURE);
8280Sstevel@tonic-gate
8290Sstevel@tonic-gate /*
8300Sstevel@tonic-gate * If this is a (delete) request, change represents
8310Sstevel@tonic-gate * the amount of capacity that will be deleted from the
8320Sstevel@tonic-gate * system. If this is an (add) notification, change
8330Sstevel@tonic-gate * represents the amount of capacity that has already
8340Sstevel@tonic-gate * been added to the system.
8350Sstevel@tonic-gate */
8360Sstevel@tonic-gate if (cmd == CMD_RCM_CAP_DEL) {
8370Sstevel@tonic-gate oldpages = currpages;
8380Sstevel@tonic-gate newpages = currpages - change;
8390Sstevel@tonic-gate } else if (cmd == CMD_RCM_CAP_NOTIFY) {
8400Sstevel@tonic-gate newpages = currpages;
8410Sstevel@tonic-gate oldpages = rcm->cappages;
8420Sstevel@tonic-gate } else {
8430Sstevel@tonic-gate if (rcm->cappages == currpages) {
8440Sstevel@tonic-gate /* No real change in memory capacity */
8450Sstevel@tonic-gate DBG("ap_rcm_cap_mem: no change in capacity.\n");
8460Sstevel@tonic-gate nvlist_free(nvl);
8470Sstevel@tonic-gate return (RCM_SUCCESS);
8480Sstevel@tonic-gate }
8490Sstevel@tonic-gate
8500Sstevel@tonic-gate oldpages = currpages - change;
8510Sstevel@tonic-gate newpages = currpages;
8520Sstevel@tonic-gate }
8530Sstevel@tonic-gate
8540Sstevel@tonic-gate DBG("ap_rcm_cap_mem: Memory capacity, old = %ld, new = %ld\n",
8550Sstevel@tonic-gate oldpages, newpages);
8560Sstevel@tonic-gate
8570Sstevel@tonic-gate if (nvlist_add_string(nvl, "state", "capacity") != 0 ||
8580Sstevel@tonic-gate nvlist_add_int32(nvl, "page_size", pgsize) != 0 ||
8590Sstevel@tonic-gate nvlist_add_int32(nvl, "old_pages", oldpages) != 0 ||
8600Sstevel@tonic-gate nvlist_add_int32(nvl, "new_pages", newpages) != 0) {
8610Sstevel@tonic-gate nvlist_free(nvl);
8620Sstevel@tonic-gate return (RCM_FAILURE);
8630Sstevel@tonic-gate }
8640Sstevel@tonic-gate
8650Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "(%ld pages)", change);
8660Sstevel@tonic-gate ap_msg(a, MSG_ISSUE, cmd, buf);
8670Sstevel@tonic-gate
868*12004Sjiang.liu@intel.com if (cmd == CMD_RCM_CAP_DEL) {
8690Sstevel@tonic-gate rv = (*rcm->request_capacity_change)(hd, "SUNW_memory",
870*12004Sjiang.liu@intel.com flags, nvl, rinfo);
871*12004Sjiang.liu@intel.com } else {
8720Sstevel@tonic-gate rv = (*rcm->notify_capacity_change)(hd, "SUNW_memory",
873*12004Sjiang.liu@intel.com flags & ~RCM_FORCE, nvl, rinfo);
874*12004Sjiang.liu@intel.com }
8750Sstevel@tonic-gate
8760Sstevel@tonic-gate nvlist_free(nvl);
8770Sstevel@tonic-gate
8780Sstevel@tonic-gate return (rv);
8790Sstevel@tonic-gate }
8800Sstevel@tonic-gate
8810Sstevel@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)8820Sstevel@tonic-gate ap_rcm_request_cap(apd_t *a, rcmd_t *rcm, rcm_handle_t *hd,
8830Sstevel@tonic-gate int *rv, uint_t flags, rcm_info_t **rinfo)
8840Sstevel@tonic-gate {
8850Sstevel@tonic-gate int cm;
8860Sstevel@tonic-gate int ncpus;
8870Sstevel@tonic-gate long npages;
8880Sstevel@tonic-gate cap_info_t *capinfo;
8890Sstevel@tonic-gate ap_target_t type;
8900Sstevel@tonic-gate
8910Sstevel@tonic-gate DBG("ap_rcm_request_cap(%p)\n", (void *)a);
8920Sstevel@tonic-gate
8930Sstevel@tonic-gate if ((capinfo = rcm->capinfo) == NULL) {
8940Sstevel@tonic-gate ap_err(a, ERR_PLUGIN, "null capinfo");
8950Sstevel@tonic-gate return (CFGA_LIB_ERROR);
8960Sstevel@tonic-gate }
8970Sstevel@tonic-gate
8980Sstevel@tonic-gate ncpus = npages = 0;
8990Sstevel@tonic-gate
9000Sstevel@tonic-gate for (cm = rcm->firstcm; cm <= rcm->lastcm; cm++) {
9010Sstevel@tonic-gate int i, j;
9020Sstevel@tonic-gate
9030Sstevel@tonic-gate /*
9040Sstevel@tonic-gate * See if the request target is a single
9050Sstevel@tonic-gate * (default) component
9060Sstevel@tonic-gate */
9070Sstevel@tonic-gate i = (cm == CM_DFLT) ? 0 : cm;
9080Sstevel@tonic-gate
9090Sstevel@tonic-gate /*
9100Sstevel@tonic-gate * We are interested only in those components
9110Sstevel@tonic-gate * in the configured state since they represent
9120Sstevel@tonic-gate * available capacity.
9130Sstevel@tonic-gate */
9140Sstevel@tonic-gate type = ap_cm_type(a, cm);
9150Sstevel@tonic-gate if (capinfo[i].valid == 0 ||
9160Sstevel@tonic-gate capinfo[i].ostate != CFGA_STAT_CONFIGURED)
9170Sstevel@tonic-gate continue;
9180Sstevel@tonic-gate else if ((type == AP_CPU) || (type == AP_CMP)) {
9190Sstevel@tonic-gate for (j = 0; j < capinfo[i].ncap; j++) {
9200Sstevel@tonic-gate rcm->cpuids[ncpus++] = capinfo[i].type.cpuid[j];
9210Sstevel@tonic-gate }
9220Sstevel@tonic-gate } else if (type == AP_MEM)
9230Sstevel@tonic-gate npages += capinfo[i].type.npages;
9240Sstevel@tonic-gate }
9250Sstevel@tonic-gate
9260Sstevel@tonic-gate if (ncpus && ((*rv = ap_rcm_cap_cpu(a, rcm, hd, flags, rinfo,
927*12004Sjiang.liu@intel.com CMD_RCM_CAP_DEL, ncpus)) != RCM_SUCCESS)) {
9280Sstevel@tonic-gate return (CFGA_LIB_ERROR);
929*12004Sjiang.liu@intel.com }
9300Sstevel@tonic-gate if (npages && ((*rv = ap_rcm_cap_mem(a, rcm, hd, flags, rinfo,
931*12004Sjiang.liu@intel.com CMD_RCM_CAP_DEL, npages)) != RCM_SUCCESS)) {
9320Sstevel@tonic-gate return (CFGA_LIB_ERROR);
933*12004Sjiang.liu@intel.com }
9340Sstevel@tonic-gate
9350Sstevel@tonic-gate return (CFGA_OK);
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate
9380Sstevel@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)9390Sstevel@tonic-gate ap_rcm_add_cap(apd_t *a, rcmd_t *rcm, rcm_handle_t *hd,
9400Sstevel@tonic-gate int *rv, uint_t flags, rcm_info_t **rinfo)
9410Sstevel@tonic-gate {
9420Sstevel@tonic-gate int cm;
9430Sstevel@tonic-gate int ncpus;
9440Sstevel@tonic-gate long npages;
9450Sstevel@tonic-gate cap_info_t *capinfo, *prevcapinfo;
9460Sstevel@tonic-gate cfga_err_t rc;
9470Sstevel@tonic-gate
9480Sstevel@tonic-gate DBG("ap_rcm_add_cap(%p)\n", (void *)a);
9490Sstevel@tonic-gate
9500Sstevel@tonic-gate /* Get the new capacity info to figure out what has changed */
951*12004Sjiang.liu@intel.com if ((rc = ap_capinfo(a, rcm->firstcm, rcm->lastcm, &capinfo)) !=
952*12004Sjiang.liu@intel.com CFGA_OK)
9530Sstevel@tonic-gate return (rc);
9540Sstevel@tonic-gate
9550Sstevel@tonic-gate if (capinfo == NULL) {
9560Sstevel@tonic-gate DBG("no pertinent capacity info\n");
9570Sstevel@tonic-gate return (CFGA_OK);
9580Sstevel@tonic-gate }
9590Sstevel@tonic-gate
9600Sstevel@tonic-gate ncpus = npages = 0;
9610Sstevel@tonic-gate prevcapinfo = rcm->capinfo;
9620Sstevel@tonic-gate
9630Sstevel@tonic-gate for (cm = rcm->firstcm; cm <= rcm->lastcm; cm++) {
9640Sstevel@tonic-gate int i, j;
9650Sstevel@tonic-gate cfga_stat_t os, prevos;
9660Sstevel@tonic-gate int prevvalidity;
9670Sstevel@tonic-gate ap_target_t type;
9680Sstevel@tonic-gate
9690Sstevel@tonic-gate /*
9700Sstevel@tonic-gate * See if the request target is a single
9710Sstevel@tonic-gate * (default) component
9720Sstevel@tonic-gate */
9730Sstevel@tonic-gate i = cm == CM_DFLT ? 0 : cm;
9740Sstevel@tonic-gate
9750Sstevel@tonic-gate os = capinfo[i].ostate;
9760Sstevel@tonic-gate if (prevcapinfo == NULL) {
9770Sstevel@tonic-gate prevos = CFGA_STAT_EMPTY;
9780Sstevel@tonic-gate prevvalidity = 1;
9790Sstevel@tonic-gate } else {
9800Sstevel@tonic-gate prevos = prevcapinfo[i].ostate;
9810Sstevel@tonic-gate prevvalidity = prevcapinfo[i].valid;
9820Sstevel@tonic-gate }
9830Sstevel@tonic-gate
9840Sstevel@tonic-gate type = ap_cm_type(a, cm);
9850Sstevel@tonic-gate
9860Sstevel@tonic-gate DBG("cm=%d valid=%d type=%d, prevos=%d os=%d\n",
987*12004Sjiang.liu@intel.com cm, prevvalidity, type, prevos, os);
9880Sstevel@tonic-gate
9890Sstevel@tonic-gate /*
9900Sstevel@tonic-gate * We are interested only in those components
9910Sstevel@tonic-gate * whose states have changed to configured as
9920Sstevel@tonic-gate * the result of the current cfgadm request.
9930Sstevel@tonic-gate */
9940Sstevel@tonic-gate if (prevvalidity == 0 || os != CFGA_STAT_CONFIGURED) {
9950Sstevel@tonic-gate capinfo[i].valid = 0;
9960Sstevel@tonic-gate continue;
9970Sstevel@tonic-gate } else if (prevos != CFGA_STAT_CONFIGURED) {
9980Sstevel@tonic-gate /*
9990Sstevel@tonic-gate * The occupant state is configured, and
10000Sstevel@tonic-gate * the previous occupant state was not.
10010Sstevel@tonic-gate */
10020Sstevel@tonic-gate if ((type == AP_CPU) || (type == AP_CMP)) {
10030Sstevel@tonic-gate for (j = 0; j < capinfo[i].ncap; j++) {
10040Sstevel@tonic-gate rcm->cpuids[ncpus++] =
10050Sstevel@tonic-gate capinfo[i].type.cpuid[j];
10060Sstevel@tonic-gate }
10070Sstevel@tonic-gate } else if (type == AP_MEM)
10080Sstevel@tonic-gate npages += capinfo[i].type.npages;
10090Sstevel@tonic-gate }
10100Sstevel@tonic-gate }
10110Sstevel@tonic-gate free(capinfo);
10120Sstevel@tonic-gate
10130Sstevel@tonic-gate if (ncpus && ((*rv = ap_rcm_cap_cpu(a, rcm, hd, flags, rinfo,
1014*12004Sjiang.liu@intel.com CMD_RCM_CAP_ADD, ncpus)) != RCM_SUCCESS)) {
10150Sstevel@tonic-gate return (CFGA_LIB_ERROR);
1016*12004Sjiang.liu@intel.com }
10170Sstevel@tonic-gate if (npages && ((*rv = ap_rcm_cap_mem(a, rcm, hd, flags, rinfo,
1018*12004Sjiang.liu@intel.com CMD_RCM_CAP_ADD, npages)) != RCM_SUCCESS)) {
10190Sstevel@tonic-gate return (CFGA_LIB_ERROR);
1020*12004Sjiang.liu@intel.com }
10210Sstevel@tonic-gate
10220Sstevel@tonic-gate return (CFGA_OK);
10230Sstevel@tonic-gate }
10240Sstevel@tonic-gate
10250Sstevel@tonic-gate /*
10260Sstevel@tonic-gate * ap_rcm_notify_cap:
10270Sstevel@tonic-gate *
10280Sstevel@tonic-gate * This routine handles the CMD_RCM_CAP_NOTIFY command. It
10290Sstevel@tonic-gate * is called after a successful/failed DR unconfigure
10300Sstevel@tonic-gate * operation. It filters out components that have changed
10310Sstevel@tonic-gate * and passes this information on to ap_rcm_cap_{cpu,mem}.
10320Sstevel@tonic-gate *
10330Sstevel@tonic-gate * ap_rcm_cap_{cpu,mem} will still be called if all the
10340Sstevel@tonic-gate * components have not changed and at least one {cpu,mem}
10350Sstevel@tonic-gate * component was originally configured.
10360Sstevel@tonic-gate */
10370Sstevel@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)10380Sstevel@tonic-gate ap_rcm_notify_cap(apd_t *a, rcmd_t *rcm, rcm_handle_t *hd,
10390Sstevel@tonic-gate int *rv, uint_t flags, rcm_info_t **rinfo)
10400Sstevel@tonic-gate {
10410Sstevel@tonic-gate cfga_err_t rc;
10420Sstevel@tonic-gate cap_info_t *capinfo;
10430Sstevel@tonic-gate cap_info_t *prevcapinfo;
10440Sstevel@tonic-gate int cm;
10450Sstevel@tonic-gate long npages = 0;
10460Sstevel@tonic-gate int ncpus = 0;
10470Sstevel@tonic-gate int prev_mem = 0; /* # of prev. configured mem components */
10480Sstevel@tonic-gate int prev_cpus = 0; /* # of prev. configured CPUs */
10490Sstevel@tonic-gate
10500Sstevel@tonic-gate DBG("ap_rcm_notify_cap(%p)\n", (void *)a);
10510Sstevel@tonic-gate
10520Sstevel@tonic-gate /* Get the new capacity info to figure out what has changed */
1053*12004Sjiang.liu@intel.com if ((rc = ap_capinfo(a, rcm->firstcm, rcm->lastcm, &capinfo)) !=
1054*12004Sjiang.liu@intel.com CFGA_OK)
10550Sstevel@tonic-gate return (rc);
10560Sstevel@tonic-gate
10570Sstevel@tonic-gate if (capinfo == NULL) {
10580Sstevel@tonic-gate DBG("no pertinent capacity info\n");
10590Sstevel@tonic-gate return (CFGA_OK);
10600Sstevel@tonic-gate }
10610Sstevel@tonic-gate
10620Sstevel@tonic-gate /* The original capacity info */
10630Sstevel@tonic-gate prevcapinfo = rcm->capinfo;
10640Sstevel@tonic-gate
10650Sstevel@tonic-gate /*
10660Sstevel@tonic-gate * Cycle through all components that we are operating
10670Sstevel@tonic-gate * on. Record which components' occupant states have
10680Sstevel@tonic-gate * changed.
10690Sstevel@tonic-gate */
10700Sstevel@tonic-gate for (cm = rcm->firstcm; cm <= rcm->lastcm; cm++) {
10710Sstevel@tonic-gate int i;
10720Sstevel@tonic-gate cfga_stat_t prevos, os;
10730Sstevel@tonic-gate ap_target_t type;
10740Sstevel@tonic-gate int prev_conf = 0;
10750Sstevel@tonic-gate int now_conf = 0;
10760Sstevel@tonic-gate
10770Sstevel@tonic-gate /*
10780Sstevel@tonic-gate * See if the request target is a single
10790Sstevel@tonic-gate * (default) component
10800Sstevel@tonic-gate */
10810Sstevel@tonic-gate i = cm == CM_DFLT ? 0 : cm;
10820Sstevel@tonic-gate
10830Sstevel@tonic-gate os = capinfo[i].ostate;
10840Sstevel@tonic-gate
10850Sstevel@tonic-gate if (prevcapinfo == NULL) {
10860Sstevel@tonic-gate prevos = CFGA_STAT_EMPTY;
10870Sstevel@tonic-gate } else {
10880Sstevel@tonic-gate prevos = prevcapinfo[i].ostate;
10890Sstevel@tonic-gate if (prevcapinfo[i].valid == 0) {
10900Sstevel@tonic-gate DBG("ap_rcm_notify_cap: skipping component "
10910Sstevel@tonic-gate "due to prevvalidity == 0\n");
10920Sstevel@tonic-gate continue;
10930Sstevel@tonic-gate }
10940Sstevel@tonic-gate }
10950Sstevel@tonic-gate
10960Sstevel@tonic-gate type = ap_cm_type(a, cm);
10970Sstevel@tonic-gate
10980Sstevel@tonic-gate prev_conf = (prevos == CFGA_STAT_CONFIGURED);
10990Sstevel@tonic-gate now_conf = (os == CFGA_STAT_CONFIGURED);
11000Sstevel@tonic-gate
11010Sstevel@tonic-gate /*
11020Sstevel@tonic-gate * Build up rcm->cpuids with the IDs of CPUs that
11030Sstevel@tonic-gate * have been removed. Record the number of removed
11040Sstevel@tonic-gate * CPUs and pages.
11050Sstevel@tonic-gate */
11060Sstevel@tonic-gate if (type == AP_CPU || type == AP_CMP) {
11070Sstevel@tonic-gate if (prev_conf)
11080Sstevel@tonic-gate prev_cpus++;
11090Sstevel@tonic-gate if (prev_conf && !now_conf) {
11100Sstevel@tonic-gate int j;
11110Sstevel@tonic-gate for (j = 0; j < capinfo[i].ncap; j++) {
11120Sstevel@tonic-gate rcm->cpuids[ncpus++] =
11130Sstevel@tonic-gate capinfo[i].type.cpuid[j];
11140Sstevel@tonic-gate }
11150Sstevel@tonic-gate }
11160Sstevel@tonic-gate } else if (type == AP_MEM) {
11170Sstevel@tonic-gate if (prev_conf)
11180Sstevel@tonic-gate prev_mem++;
11190Sstevel@tonic-gate if (prev_conf && !now_conf)
11200Sstevel@tonic-gate npages += capinfo[i].type.npages;
11210Sstevel@tonic-gate }
11220Sstevel@tonic-gate }
11230Sstevel@tonic-gate free(capinfo);
11240Sstevel@tonic-gate
11250Sstevel@tonic-gate /*
11260Sstevel@tonic-gate * If any CPU or memory components were operated on,
11270Sstevel@tonic-gate * successfully or not, the rcm_notify_capacity_change()
11280Sstevel@tonic-gate * routine must be called.
11290Sstevel@tonic-gate */
11300Sstevel@tonic-gate
11310Sstevel@tonic-gate if (prev_cpus) {
11320Sstevel@tonic-gate *rv = ap_rcm_cap_cpu(a, rcm, hd, flags, rinfo,
11330Sstevel@tonic-gate CMD_RCM_CAP_NOTIFY, ncpus);
11340Sstevel@tonic-gate
11350Sstevel@tonic-gate if (*rv != RCM_SUCCESS)
11360Sstevel@tonic-gate return (CFGA_LIB_ERROR);
11370Sstevel@tonic-gate }
11380Sstevel@tonic-gate
11390Sstevel@tonic-gate if (prev_mem) {
11400Sstevel@tonic-gate *rv = ap_rcm_cap_mem(a, rcm, hd, flags, rinfo,
11410Sstevel@tonic-gate CMD_RCM_CAP_NOTIFY, npages);
11420Sstevel@tonic-gate
11430Sstevel@tonic-gate if (*rv != RCM_SUCCESS)
11440Sstevel@tonic-gate return (CFGA_LIB_ERROR);
11450Sstevel@tonic-gate }
11460Sstevel@tonic-gate
11470Sstevel@tonic-gate return (CFGA_OK);
11480Sstevel@tonic-gate }
11490Sstevel@tonic-gate
11500Sstevel@tonic-gate cfga_err_t
ap_rcm_ctl(apd_t * a,int cmd)11510Sstevel@tonic-gate ap_rcm_ctl(apd_t *a, int cmd)
11520Sstevel@tonic-gate {
11530Sstevel@tonic-gate int i;
11540Sstevel@tonic-gate int rv;
11550Sstevel@tonic-gate int noop;
11560Sstevel@tonic-gate int ncpus;
11570Sstevel@tonic-gate int cm;
11580Sstevel@tonic-gate uint_t flags;
11590Sstevel@tonic-gate char *rsrc;
11600Sstevel@tonic-gate char **rlist;
11610Sstevel@tonic-gate rcmd_t *rcm;
11620Sstevel@tonic-gate rcm_info_t *rinfo;
11630Sstevel@tonic-gate rcm_handle_t *hd;
11640Sstevel@tonic-gate cfga_err_t rc;
11650Sstevel@tonic-gate cpuid_t *growcpuids;
11660Sstevel@tonic-gate
11670Sstevel@tonic-gate DBG("ap_rcm_ctl(%p)\n", (void *)a);
11680Sstevel@tonic-gate
11690Sstevel@tonic-gate if ((rcm = (rcmd_t *)a->rcm) == NULL) {
11700Sstevel@tonic-gate ap_msg(a, MSG_SKIP, cmd, a->target);
11710Sstevel@tonic-gate return (CFGA_OK);
11720Sstevel@tonic-gate }
11730Sstevel@tonic-gate
11740Sstevel@tonic-gate hd = rcm->hd;
11750Sstevel@tonic-gate rv = RCM_SUCCESS;
11760Sstevel@tonic-gate rc = CFGA_OK;
11770Sstevel@tonic-gate if (ap_getopt(a, OPT_FORCE))
11780Sstevel@tonic-gate flags = RCM_FORCE;
11790Sstevel@tonic-gate else
11800Sstevel@tonic-gate flags = 0;
11810Sstevel@tonic-gate rinfo = NULL;
11820Sstevel@tonic-gate rlist = NULL;
11830Sstevel@tonic-gate rsrc = NULL;
11840Sstevel@tonic-gate noop = 0;
11850Sstevel@tonic-gate
11860Sstevel@tonic-gate switch (cmd) {
11870Sstevel@tonic-gate case CMD_RCM_CAP_DEL:
11880Sstevel@tonic-gate if (rcm->capinfo == NULL)
11890Sstevel@tonic-gate noop++;
11900Sstevel@tonic-gate else
1191*12004Sjiang.liu@intel.com rc = ap_rcm_request_cap(a, rcm, hd, &rv, flags, &rinfo);
11920Sstevel@tonic-gate break;
11930Sstevel@tonic-gate case CMD_RCM_CAP_ADD:
11940Sstevel@tonic-gate rc = ap_rcm_add_cap(a, rcm, hd, &rv, flags, &rinfo);
11950Sstevel@tonic-gate break;
11960Sstevel@tonic-gate case CMD_RCM_CAP_NOTIFY:
11970Sstevel@tonic-gate rc = ap_rcm_notify_cap(a, rcm, hd, &rv, flags, &rinfo);
11980Sstevel@tonic-gate break;
11990Sstevel@tonic-gate case CMD_RCM_ONLINE:
12000Sstevel@tonic-gate /* Refresh changed component states */
12010Sstevel@tonic-gate if ((rc = ap_stat(a, 1)) != CFGA_OK) {
12020Sstevel@tonic-gate noop++;
12030Sstevel@tonic-gate break;
12040Sstevel@tonic-gate }
12050Sstevel@tonic-gate
12060Sstevel@tonic-gate if (a->tgt == AP_BOARD) {
12070Sstevel@tonic-gate rcm->firstcm = 0;
12080Sstevel@tonic-gate rcm->lastcm = a->ncm - 1;
12090Sstevel@tonic-gate
12100Sstevel@tonic-gate /* Check if we need to grow our cpuids list */
12110Sstevel@tonic-gate for (ncpus = 0, cm = rcm->firstcm; cm <= rcm->lastcm;
12120Sstevel@tonic-gate cm++) {
12130Sstevel@tonic-gate ap_target_t type = ap_cm_type(a, cm);
12140Sstevel@tonic-gate if ((type == AP_CPU) || (type == AP_CMP))
12150Sstevel@tonic-gate ncpus += ap_cm_ncap(a, cm);
12160Sstevel@tonic-gate }
12170Sstevel@tonic-gate
12180Sstevel@tonic-gate if (rcm->ncpus < ncpus) {
12190Sstevel@tonic-gate if ((growcpuids =
12200Sstevel@tonic-gate (cpuid_t *)realloc(rcm->cpuids,
12210Sstevel@tonic-gate (ncpus * sizeof (cpuid_t)))) == NULL) {
12220Sstevel@tonic-gate ap_err(a, ERR_NOMEM);
12230Sstevel@tonic-gate return (CFGA_LIB_ERROR);
12240Sstevel@tonic-gate }
12250Sstevel@tonic-gate rcm->ncpus = ncpus;
12260Sstevel@tonic-gate rcm->cpuids = growcpuids;
12270Sstevel@tonic-gate }
12280Sstevel@tonic-gate
12290Sstevel@tonic-gate } else {
12300Sstevel@tonic-gate rcm->firstcm = CM_DFLT;
12310Sstevel@tonic-gate rcm->lastcm = CM_DFLT;
12320Sstevel@tonic-gate }
12330Sstevel@tonic-gate
12340Sstevel@tonic-gate /*FALLTHROUGH*/
12350Sstevel@tonic-gate
12360Sstevel@tonic-gate case CMD_RCM_OFFLINE:
12370Sstevel@tonic-gate case CMD_RCM_REMOVE: {
12380Sstevel@tonic-gate uint_t nrsrc;
12390Sstevel@tonic-gate
12400Sstevel@tonic-gate if (cmd == CMD_RCM_REMOVE) {
12410Sstevel@tonic-gate /*
12420Sstevel@tonic-gate * An unconfigure has just taken place, so
12430Sstevel@tonic-gate * refresh the changed component states.
12440Sstevel@tonic-gate */
12450Sstevel@tonic-gate if ((rc = ap_stat(a, 1)) != CFGA_OK) {
12460Sstevel@tonic-gate noop++;
12470Sstevel@tonic-gate break;
12480Sstevel@tonic-gate }
12490Sstevel@tonic-gate }
12500Sstevel@tonic-gate
12510Sstevel@tonic-gate /* Check if this is an empty board, i.e. no components */
12520Sstevel@tonic-gate if (a->ncm == 0) {
12530Sstevel@tonic-gate noop++;
12540Sstevel@tonic-gate break;
12550Sstevel@tonic-gate }
12560Sstevel@tonic-gate
12570Sstevel@tonic-gate if ((rlist = rcm->rlist) == NULL) {
12580Sstevel@tonic-gate rc = ap_rcm_rlist(a, rcm->firstcm, rcm->lastcm, &rlist,
12590Sstevel@tonic-gate cmd);
12600Sstevel@tonic-gate if ((rc == CFGA_OK) && (rlist != NULL) &&
12610Sstevel@tonic-gate (rlist[0] != NULL)) {
12620Sstevel@tonic-gate rcm->rlist = rlist;
12630Sstevel@tonic-gate } else {
12640Sstevel@tonic-gate /* Do not pass up empty resource list to RCM */
12650Sstevel@tonic-gate noop++;
12660Sstevel@tonic-gate break;
12670Sstevel@tonic-gate }
12680Sstevel@tonic-gate }
12690Sstevel@tonic-gate for (nrsrc = 0; rlist[nrsrc] != NULL; nrsrc++)
12700Sstevel@tonic-gate ap_msg(a, MSG_ISSUE, cmd, rlist[nrsrc]);
12710Sstevel@tonic-gate if (cmd == CMD_RCM_OFFLINE)
12720Sstevel@tonic-gate rv = (*rcm->request_offline_list)(hd, rlist, flags,
1273*12004Sjiang.liu@intel.com &rinfo);
12740Sstevel@tonic-gate else if (cmd == CMD_RCM_ONLINE)
12750Sstevel@tonic-gate rv = (*rcm->notify_online_list)(hd, rlist,
1276*12004Sjiang.liu@intel.com flags & ~RCM_FORCE, &rinfo);
12770Sstevel@tonic-gate else
12780Sstevel@tonic-gate rv = (*rcm->notify_remove_list)(hd, rlist,
1279*12004Sjiang.liu@intel.com flags & ~RCM_FORCE, &rinfo);
12800Sstevel@tonic-gate break;
12810Sstevel@tonic-gate }
12820Sstevel@tonic-gate case CMD_RCM_SUSPEND: {
12830Sstevel@tonic-gate timespec_t t;
12840Sstevel@tonic-gate t.tv_sec = (time_t)0;
12850Sstevel@tonic-gate t.tv_nsec = (long)0;
12860Sstevel@tonic-gate rsrc = OS;
12870Sstevel@tonic-gate ap_msg(a, MSG_ISSUE, cmd, rsrc);
12880Sstevel@tonic-gate rv = (*rcm->request_suspend)(hd, rsrc, flags, &t, &rinfo);
12890Sstevel@tonic-gate break;
12900Sstevel@tonic-gate }
12910Sstevel@tonic-gate case CMD_RCM_RESUME:
12920Sstevel@tonic-gate rsrc = OS;
12930Sstevel@tonic-gate ap_msg(a, MSG_ISSUE, cmd, rsrc);
12940Sstevel@tonic-gate rv = (*rcm->notify_resume)(hd, rsrc, 0, &rinfo);
12950Sstevel@tonic-gate break;
12960Sstevel@tonic-gate default:
12970Sstevel@tonic-gate ap_err(a, ERR_CMD_INVAL, cmd);
12980Sstevel@tonic-gate return (CFGA_INVAL);
12990Sstevel@tonic-gate }
13000Sstevel@tonic-gate
13010Sstevel@tonic-gate if (rv != RCM_SUCCESS) {
13020Sstevel@tonic-gate rcm->rinfo = rinfo;
13030Sstevel@tonic-gate rcm->infot = NULL;
13040Sstevel@tonic-gate ap_err(a, ERR_RCM_CMD, cmd);
13050Sstevel@tonic-gate (*rcm->free_info)(rinfo);
13060Sstevel@tonic-gate if (rc == CFGA_OK)
13070Sstevel@tonic-gate rc = CFGA_LIB_ERROR; /* make sure error is set */
13080Sstevel@tonic-gate }
13090Sstevel@tonic-gate if ((rc == CFGA_OK) && (noop == 0)) {
13100Sstevel@tonic-gate if (rlist)
13110Sstevel@tonic-gate for (i = 0; rlist[i]; i++)
13120Sstevel@tonic-gate ap_msg(a, MSG_DONE, cmd, rlist[i]);
13130Sstevel@tonic-gate else if (rsrc)
13140Sstevel@tonic-gate ap_msg(a, MSG_DONE, cmd, rsrc);
13150Sstevel@tonic-gate else
13160Sstevel@tonic-gate ap_msg(a, MSG_DONE, cmd, a->target);
13170Sstevel@tonic-gate }
13180Sstevel@tonic-gate
13190Sstevel@tonic-gate return (rc);
13200Sstevel@tonic-gate }
13210Sstevel@tonic-gate
13220Sstevel@tonic-gate /*
13230Sstevel@tonic-gate * ap_rcm_info
13240Sstevel@tonic-gate *
13250Sstevel@tonic-gate * Takes an ap_id and a character pointer, and formats
13260Sstevel@tonic-gate * the rcm_info_t data in the form of a table to the given character pointer.
13270Sstevel@tonic-gate * Code duplicated from the scsi plugin.
13280Sstevel@tonic-gate * Note: This function will go away when a generic librcm callback is
13290Sstevel@tonic-gate * implemented to format RCM messages for plugins.
13300Sstevel@tonic-gate */
13310Sstevel@tonic-gate int
ap_rcm_info(apd_t * a,char ** msg)13320Sstevel@tonic-gate ap_rcm_info(apd_t *a, char **msg)
13330Sstevel@tonic-gate {
13340Sstevel@tonic-gate rcmd_t *rcm;
13350Sstevel@tonic-gate rcm_info_t *rinfo;
13360Sstevel@tonic-gate int i;
13370Sstevel@tonic-gate size_t w;
13380Sstevel@tonic-gate size_t width = 0;
13390Sstevel@tonic-gate size_t w_rsrc = 0;
13400Sstevel@tonic-gate size_t w_info = 0;
13410Sstevel@tonic-gate size_t msg_size = 0;
13420Sstevel@tonic-gate uint_t tuples = 0;
13430Sstevel@tonic-gate rcm_info_tuple_t *tuple = NULL;
13440Sstevel@tonic-gate char *rsrc;
13450Sstevel@tonic-gate char *info;
13460Sstevel@tonic-gate char *newmsg;
13470Sstevel@tonic-gate static char format[RCM_MAX_FORMAT];
13480Sstevel@tonic-gate const char *infostr;
13490Sstevel@tonic-gate
13500Sstevel@tonic-gate
13510Sstevel@tonic-gate DBG("ap_rcm_info(%p)\n", (void *)a);
13520Sstevel@tonic-gate
13530Sstevel@tonic-gate /* Protect against invalid arguments */
13540Sstevel@tonic-gate if ((a == NULL) || ((rcm = (rcmd_t *)a->rcm) == NULL) ||
13550Sstevel@tonic-gate ((rinfo = rcm->rinfo) == NULL) || (msg == NULL)) {
13560Sstevel@tonic-gate return (-1);
13570Sstevel@tonic-gate }
13580Sstevel@tonic-gate
13590Sstevel@tonic-gate /* Set localized table header strings */
13600Sstevel@tonic-gate rsrc = dgettext(TEXT_DOMAIN, "Resource");
13610Sstevel@tonic-gate info = dgettext(TEXT_DOMAIN, "Information");
13620Sstevel@tonic-gate
13630Sstevel@tonic-gate /* A first pass, to size up the RCM information */
13640Sstevel@tonic-gate while (tuple = (*rcm->info_next)(rinfo, tuple)) {
13650Sstevel@tonic-gate if ((infostr = (*rcm->info_info)(tuple)) != NULL) {
13660Sstevel@tonic-gate tuples++;
13670Sstevel@tonic-gate if ((w = strlen((*rcm->info_rsrc)(tuple))) > w_rsrc)
13680Sstevel@tonic-gate w_rsrc = w;
13690Sstevel@tonic-gate if ((w = strlen(infostr)) > w_info)
13700Sstevel@tonic-gate w_info = w;
13710Sstevel@tonic-gate }
13720Sstevel@tonic-gate }
13730Sstevel@tonic-gate
13740Sstevel@tonic-gate /* If nothing was sized up above, stop early */
13750Sstevel@tonic-gate if (tuples == 0)
13760Sstevel@tonic-gate return (0);
13770Sstevel@tonic-gate
13780Sstevel@tonic-gate /* Adjust column widths for column headings */
13790Sstevel@tonic-gate if ((w = strlen(rsrc)) > w_rsrc)
13800Sstevel@tonic-gate w_rsrc = w;
13810Sstevel@tonic-gate else if ((w_rsrc - w) % 2)
13820Sstevel@tonic-gate w_rsrc++;
13830Sstevel@tonic-gate if ((w = strlen(info)) > w_info)
13840Sstevel@tonic-gate w_info = w;
13850Sstevel@tonic-gate else if ((w_info - w) % 2)
13860Sstevel@tonic-gate w_info++;
13870Sstevel@tonic-gate
13880Sstevel@tonic-gate /*
13890Sstevel@tonic-gate * Compute the total line width of each line,
13900Sstevel@tonic-gate * accounting for intercolumn spacing.
13910Sstevel@tonic-gate */
13920Sstevel@tonic-gate width = w_info + w_rsrc + 4;
13930Sstevel@tonic-gate
13940Sstevel@tonic-gate /* Allocate space for the table */
13950Sstevel@tonic-gate msg_size = (2 + tuples) * (width + 1) + 2;
13960Sstevel@tonic-gate if (*msg == NULL) {
13970Sstevel@tonic-gate /* zero fill for the strcat() call below */
13980Sstevel@tonic-gate *msg = calloc(msg_size, sizeof (char));
13990Sstevel@tonic-gate if (*msg == NULL)
14000Sstevel@tonic-gate return (-1);
14010Sstevel@tonic-gate } else {
14020Sstevel@tonic-gate newmsg = realloc(*msg, strlen(*msg) + msg_size);
14030Sstevel@tonic-gate if (newmsg == NULL)
14040Sstevel@tonic-gate return (-1);
14050Sstevel@tonic-gate else
14060Sstevel@tonic-gate *msg = newmsg;
14070Sstevel@tonic-gate }
14080Sstevel@tonic-gate
14090Sstevel@tonic-gate /* Place a table header into the string */
14100Sstevel@tonic-gate
14110Sstevel@tonic-gate /* The resource header */
14120Sstevel@tonic-gate (void) strcat(*msg, "\n");
14130Sstevel@tonic-gate w = strlen(rsrc);
14140Sstevel@tonic-gate for (i = 0; i < ((w_rsrc - w) / 2); i++)
14150Sstevel@tonic-gate (void) strcat(*msg, " ");
14160Sstevel@tonic-gate (void) strcat(*msg, rsrc);
14170Sstevel@tonic-gate for (i = 0; i < ((w_rsrc - w) / 2); i++)
14180Sstevel@tonic-gate (void) strcat(*msg, " ");
14190Sstevel@tonic-gate
14200Sstevel@tonic-gate /* The information header */
14210Sstevel@tonic-gate (void) strcat(*msg, " ");
14220Sstevel@tonic-gate w = strlen(info);
14230Sstevel@tonic-gate for (i = 0; i < ((w_info - w) / 2); i++)
14240Sstevel@tonic-gate (void) strcat(*msg, " ");
14250Sstevel@tonic-gate (void) strcat(*msg, info);
14260Sstevel@tonic-gate for (i = 0; i < ((w_info - w) / 2); i++)
14270Sstevel@tonic-gate (void) strcat(*msg, " ");
14280Sstevel@tonic-gate
14290Sstevel@tonic-gate /* Underline the headers */
14300Sstevel@tonic-gate (void) strcat(*msg, "\n");
14310Sstevel@tonic-gate for (i = 0; i < w_rsrc; i++)
14320Sstevel@tonic-gate (void) strcat(*msg, "-");
14330Sstevel@tonic-gate (void) strcat(*msg, " ");
14340Sstevel@tonic-gate for (i = 0; i < w_info; i++)
14350Sstevel@tonic-gate (void) strcat(*msg, "-");
14360Sstevel@tonic-gate
14370Sstevel@tonic-gate /* Construct the format string */
14380Sstevel@tonic-gate (void) snprintf(format, RCM_MAX_FORMAT, "%%-%ds %%-%ds",
14390Sstevel@tonic-gate (int)w_rsrc, (int)w_info);
14400Sstevel@tonic-gate
14410Sstevel@tonic-gate /* Add the tuples to the table string */
14420Sstevel@tonic-gate tuple = NULL;
14430Sstevel@tonic-gate while ((tuple = (*rcm->info_next)(rinfo, tuple)) != NULL) {
14440Sstevel@tonic-gate if ((infostr = (*rcm->info_info)(tuple)) != NULL) {
14450Sstevel@tonic-gate (void) strcat(*msg, "\n");
14460Sstevel@tonic-gate (void) sprintf(&((*msg)[strlen(*msg)]), format,
1447*12004Sjiang.liu@intel.com (*rcm->info_rsrc)(tuple), infostr);
14480Sstevel@tonic-gate }
14490Sstevel@tonic-gate }
14500Sstevel@tonic-gate
14510Sstevel@tonic-gate DBG("ap_rcm_info(%p) success\n", (void *)a);
14520Sstevel@tonic-gate return (0);
14530Sstevel@tonic-gate }
1454