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
52732Sscarter * Common Development and Distribution License (the "License").
62732Sscarter * 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 /*
22*9739SJames.Anderson@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * Implementation of ri_init routine for obtaining mapping
280Sstevel@tonic-gate * of system board attachment points to physical devices and to
290Sstevel@tonic-gate * the Reconfiguration Coordination Manager (RCM) client usage
300Sstevel@tonic-gate * of these devices.
310Sstevel@tonic-gate */
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <unistd.h>
350Sstevel@tonic-gate #include <kstat.h>
360Sstevel@tonic-gate #include <sys/param.h>
370Sstevel@tonic-gate #include <sys/sbd_ioctl.h>
380Sstevel@tonic-gate #include "rsrc_info_impl.h"
390Sstevel@tonic-gate
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate * Occupant types exported by cfgadm sbd plugin via
420Sstevel@tonic-gate * config_admin(3CFGADM).
430Sstevel@tonic-gate */
440Sstevel@tonic-gate #define SBD_CM_CPU "cpu"
450Sstevel@tonic-gate #define SBD_CM_MEM "memory"
460Sstevel@tonic-gate #define SBD_CM_IO "io"
470Sstevel@tonic-gate
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate * RCM abstract resource names.
500Sstevel@tonic-gate */
510Sstevel@tonic-gate #define RCM_MEM_ALL "SUNW_memory"
520Sstevel@tonic-gate #define RCM_CPU_ALL "SUNW_cpu"
530Sstevel@tonic-gate #define RCM_CPU RCM_CPU_ALL"/cpu"
540Sstevel@tonic-gate
550Sstevel@tonic-gate #define KBYTE 1024
560Sstevel@tonic-gate #define MBYTE 1048576
570Sstevel@tonic-gate #define USAGE_ALLOC_SIZE 128
580Sstevel@tonic-gate
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate * define to allow io_cm_info to return NODE is NULL to ri_init,
610Sstevel@tonic-gate * in order to skip over nodes w/unattached drivers
620Sstevel@tonic-gate */
630Sstevel@tonic-gate #define RI_NODE_NIL 1
640Sstevel@tonic-gate
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate * This code is CMP aware as it parses the
670Sstevel@tonic-gate * cfgadm info field for individual cpuids.
680Sstevel@tonic-gate */
690Sstevel@tonic-gate #define CPUID_SEP ","
700Sstevel@tonic-gate #define CPU_INFO_FMT "cpuid=%s speed=%d ecache=%d"
710Sstevel@tonic-gate
720Sstevel@tonic-gate typedef struct {
730Sstevel@tonic-gate cfga_list_data_t *cfga_list_data;
740Sstevel@tonic-gate int nlist;
750Sstevel@tonic-gate } apd_t;
760Sstevel@tonic-gate
770Sstevel@tonic-gate typedef struct {
780Sstevel@tonic-gate long pagesize;
790Sstevel@tonic-gate long syspages;
800Sstevel@tonic-gate long sysmb;
810Sstevel@tonic-gate } mem_stat_t;
820Sstevel@tonic-gate
830Sstevel@tonic-gate #define ms_syspages m_stat.syspages
840Sstevel@tonic-gate #define ms_pagesize m_stat.pagesize
850Sstevel@tonic-gate #define ms_sysmb m_stat.sysmb
860Sstevel@tonic-gate
870Sstevel@tonic-gate typedef int32_t cpuid_t;
880Sstevel@tonic-gate
890Sstevel@tonic-gate typedef struct {
900Sstevel@tonic-gate int cpuid_max; /* maximum cpuid value */
91*9739SJames.Anderson@Sun.COM int ecache_curr; /* cached during tree walk */
920Sstevel@tonic-gate int *ecache_sizes; /* indexed by cpuid */
930Sstevel@tonic-gate } ecache_info_t;
940Sstevel@tonic-gate
950Sstevel@tonic-gate typedef struct {
960Sstevel@tonic-gate rcm_handle_t *hdl;
970Sstevel@tonic-gate rcm_info_t *offline_query_info;
980Sstevel@tonic-gate char **rlist;
990Sstevel@tonic-gate int nrlist;
1000Sstevel@tonic-gate cpuid_t *cpus;
1010Sstevel@tonic-gate int ncpus;
1020Sstevel@tonic-gate int ndevs;
1030Sstevel@tonic-gate uint_t query_pages;
1040Sstevel@tonic-gate mem_stat_t m_stat;
1050Sstevel@tonic-gate ecache_info_t ecache_info;
1060Sstevel@tonic-gate } rcmd_t;
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate typedef struct {
1090Sstevel@tonic-gate const char *rsrc;
1100Sstevel@tonic-gate const char *info;
1110Sstevel@tonic-gate } usage_t;
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate /* Lookup table entry for matching IO devices to RCM resource usage */
1140Sstevel@tonic-gate typedef struct {
1150Sstevel@tonic-gate int index; /* index into the table array */
1160Sstevel@tonic-gate di_node_t node; /* associated devinfo node */
1170Sstevel@tonic-gate char *name; /* device full path name */
1180Sstevel@tonic-gate int n_usage;
1190Sstevel@tonic-gate usage_t *usage;
1200Sstevel@tonic-gate } lookup_entry_t;
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate typedef struct {
1230Sstevel@tonic-gate int n_entries;
1240Sstevel@tonic-gate int n_slots;
1250Sstevel@tonic-gate lookup_entry_t *table;
1260Sstevel@tonic-gate } lookup_table_t;
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate typedef struct {
1290Sstevel@tonic-gate int err;
1300Sstevel@tonic-gate di_node_t node;
1310Sstevel@tonic-gate char *pathbuf;
1320Sstevel@tonic-gate lookup_table_t *table;
1330Sstevel@tonic-gate di_devlink_handle_t linkhd;
1340Sstevel@tonic-gate } devinfo_arg_t;
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate static int dyn_ap_ids(char *, cfga_list_data_t **, int *);
1370Sstevel@tonic-gate static int rcm_init(rcmd_t *, apd_t [], int, int);
1380Sstevel@tonic-gate static void rcm_fini(rcmd_t *);
1390Sstevel@tonic-gate static int rcm_query_init(rcmd_t *, apd_t [], int);
1400Sstevel@tonic-gate static int cap_request(ri_hdl_t *, rcmd_t *);
1410Sstevel@tonic-gate static int syscpus(cpuid_t **, int *);
1420Sstevel@tonic-gate static int cpu_cap_request(ri_hdl_t *, rcmd_t *);
1430Sstevel@tonic-gate static int mem_cap_request(ri_hdl_t *, rcmd_t *);
1440Sstevel@tonic-gate static int (*cm_rcm_qpass_func(cfga_type_t))(cfga_list_data_t *, rcmd_t *);
1450Sstevel@tonic-gate static int cpu_rcm_qpass(cfga_list_data_t *, rcmd_t *);
1460Sstevel@tonic-gate static int mem_rcm_qpass(cfga_list_data_t *, rcmd_t *);
1470Sstevel@tonic-gate static int io_rcm_qpass(cfga_list_data_t *, rcmd_t *);
1480Sstevel@tonic-gate static int (*cm_info_func(cfga_type_t))(ri_ap_t *, cfga_list_data_t *, int,
1490Sstevel@tonic-gate rcmd_t *);
1500Sstevel@tonic-gate static int cpu_cm_info(ri_ap_t *, cfga_list_data_t *, int, rcmd_t *);
1513254Sscarter static int i_cpu_cm_info(processorid_t, int, int, ri_ap_t *, rcmd_t *);
1520Sstevel@tonic-gate static int mem_cm_info(ri_ap_t *, cfga_list_data_t *, int, rcmd_t *);
1530Sstevel@tonic-gate static int io_cm_info(ri_ap_t *, cfga_list_data_t *, int, rcmd_t *);
1540Sstevel@tonic-gate static int ident_leaf(di_node_t);
1550Sstevel@tonic-gate static int mk_drv_inst(di_node_t, char [], char *);
1560Sstevel@tonic-gate static int devinfo_node_walk(di_node_t, void *);
1570Sstevel@tonic-gate static int devinfo_minor_walk(di_node_t, di_minor_t, void *);
1580Sstevel@tonic-gate static int devinfo_devlink_walk(di_devlink_t, void *);
1590Sstevel@tonic-gate static int add_rcm_clients(ri_client_t **, rcmd_t *, rcm_info_t *, int, int *);
1600Sstevel@tonic-gate static int rcm_ignore(char *, char *);
1610Sstevel@tonic-gate static int add_query_state(rcmd_t *, ri_client_t *, const char *, const char *);
1620Sstevel@tonic-gate static int state2query(int);
1630Sstevel@tonic-gate static void dev_list_append(ri_dev_t **, ri_dev_t *);
1640Sstevel@tonic-gate static void dev_list_cpu_insert(ri_dev_t **, ri_dev_t *, processorid_t);
1650Sstevel@tonic-gate static rcm_info_tuple_t *tuple_lookup(rcmd_t *, const char *, const char *);
1660Sstevel@tonic-gate static ri_ap_t *ri_ap_alloc(char *, ri_hdl_t *);
1670Sstevel@tonic-gate static ri_dev_t *ri_dev_alloc(void);
1680Sstevel@tonic-gate static ri_dev_t *io_dev_alloc(char *);
1690Sstevel@tonic-gate static ri_client_t *ri_client_alloc(char *, char *);
1700Sstevel@tonic-gate static void apd_tbl_free(apd_t [], int);
1710Sstevel@tonic-gate static char *pstate2str(int);
1720Sstevel@tonic-gate static int ecache_info_init(ecache_info_t *);
1730Sstevel@tonic-gate static int find_cpu_nodes(di_node_t, void *);
1740Sstevel@tonic-gate static int prop_lookup_int(di_node_t, di_prom_handle_t, char *, int **);
1750Sstevel@tonic-gate static int add_lookup_entry(lookup_table_t *, const char *, di_node_t);
1760Sstevel@tonic-gate static int table_compare_names(const void *, const void *);
1770Sstevel@tonic-gate static int table_compare_indices(const void *, const void *);
1780Sstevel@tonic-gate static lookup_entry_t *lookup(lookup_table_t *table, const char *);
1790Sstevel@tonic-gate static int add_usage(lookup_entry_t *, const char *, rcm_info_tuple_t *);
1800Sstevel@tonic-gate static void empty_table(lookup_table_t *);
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate #ifdef DEBUG
1830Sstevel@tonic-gate static void dump_apd_tbl(FILE *, apd_t *, int);
1840Sstevel@tonic-gate #endif /* DEBUG */
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate static struct {
1870Sstevel@tonic-gate char *type;
1880Sstevel@tonic-gate int (*cm_info)(ri_ap_t *, cfga_list_data_t *, int, rcmd_t *);
1890Sstevel@tonic-gate int (*cm_rcm_qpass)(cfga_list_data_t *, rcmd_t *);
1900Sstevel@tonic-gate } cm_ctl[] = {
1910Sstevel@tonic-gate {SBD_CM_CPU, cpu_cm_info, cpu_rcm_qpass},
1920Sstevel@tonic-gate {SBD_CM_MEM, mem_cm_info, mem_rcm_qpass},
1930Sstevel@tonic-gate {SBD_CM_IO, io_cm_info, io_rcm_qpass}
1940Sstevel@tonic-gate };
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate * Table of known info string prefixes for RCM modules that do not
1980Sstevel@tonic-gate * represent actual resource usage, but instead provide name translations
1990Sstevel@tonic-gate * or sequencing within the RCM namespace. Since RCM provides no way to
2000Sstevel@tonic-gate * filter these out, we must maintain this hack.
2010Sstevel@tonic-gate */
2020Sstevel@tonic-gate static char *rcm_info_filter[] = {
2030Sstevel@tonic-gate "Network interface", /* Network naming module */
2040Sstevel@tonic-gate NULL
2050Sstevel@tonic-gate };
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate /*
2090Sstevel@tonic-gate * Allocate snapshot handle.
2100Sstevel@tonic-gate */
2110Sstevel@tonic-gate int
ri_init(int n_apids,char ** ap_ids,int flags,ri_hdl_t ** hdlp)2120Sstevel@tonic-gate ri_init(int n_apids, char **ap_ids, int flags, ri_hdl_t **hdlp)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate int i, j;
2150Sstevel@tonic-gate ri_hdl_t *ri_hdl;
2160Sstevel@tonic-gate ri_ap_t *ap_hdl;
2170Sstevel@tonic-gate rcmd_t *rcm = NULL;
2180Sstevel@tonic-gate cfga_list_data_t *cfga_ldata;
2190Sstevel@tonic-gate apd_t *apd, *apd_tbl = NULL;
2200Sstevel@tonic-gate int (*cm_info)(ri_ap_t *, cfga_list_data_t *,
2210Sstevel@tonic-gate int, rcmd_t *);
2220Sstevel@tonic-gate int rv = RI_SUCCESS;
2230Sstevel@tonic-gate int cm_info_rv;
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate if (n_apids <= 0 || ap_ids == NULL || hdlp == NULL)
2260Sstevel@tonic-gate return (RI_INVAL);
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate if (flags & ~RI_REQ_MASK)
2290Sstevel@tonic-gate return (RI_NOTSUP);
2300Sstevel@tonic-gate
2310Sstevel@tonic-gate *hdlp = NULL;
2320Sstevel@tonic-gate if ((ri_hdl = calloc(1, sizeof (*ri_hdl))) == NULL ||
2330Sstevel@tonic-gate (rcm = calloc(1, sizeof (*rcm))) == NULL ||
2340Sstevel@tonic-gate (apd_tbl = calloc(n_apids, sizeof (*apd_tbl))) == NULL) {
2350Sstevel@tonic-gate dprintf((stderr, "calloc: %s\n", strerror(errno)));
2360Sstevel@tonic-gate rv = RI_FAILURE;
2370Sstevel@tonic-gate goto out;
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate /*
2410Sstevel@tonic-gate * Create mapping of boards to components.
2420Sstevel@tonic-gate */
2430Sstevel@tonic-gate for (i = 0, apd = apd_tbl; i < n_apids; i++, apd++) {
2440Sstevel@tonic-gate if (dyn_ap_ids(ap_ids[i], &apd->cfga_list_data,
2450Sstevel@tonic-gate &apd->nlist) == -1) {
2460Sstevel@tonic-gate rv = RI_INVAL;
2470Sstevel@tonic-gate goto out;
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate #ifdef DEBUG
2510Sstevel@tonic-gate dump_apd_tbl(stderr, apd_tbl, n_apids);
2520Sstevel@tonic-gate #endif /* DEBUG */
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate if (rcm_init(rcm, apd_tbl, n_apids, flags) != 0) {
2550Sstevel@tonic-gate rv = RI_FAILURE;
2560Sstevel@tonic-gate goto out;
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate
2590Sstevel@tonic-gate /*
2600Sstevel@tonic-gate * Best effort attempt to read cpu ecache sizes from
2610Sstevel@tonic-gate * OBP/Solaris device trees. These are later looked up
2620Sstevel@tonic-gate * in i_cpu_cm_info().
2630Sstevel@tonic-gate */
2640Sstevel@tonic-gate (void) ecache_info_init(&rcm->ecache_info);
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate for (i = 0, apd = apd_tbl; i < n_apids; i++, apd++) {
2670Sstevel@tonic-gate if ((ap_hdl = ri_ap_alloc(ap_ids[i], ri_hdl)) == NULL) {
2680Sstevel@tonic-gate rv = RI_FAILURE;
2690Sstevel@tonic-gate goto out;
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate /*
2730Sstevel@tonic-gate * Add component info based on occupant type. Note all
2740Sstevel@tonic-gate * passes through the apd table skip over the first
2750Sstevel@tonic-gate * cfgadm_list_data entry, which is the static system board
2760Sstevel@tonic-gate * attachment point.
2770Sstevel@tonic-gate */
2780Sstevel@tonic-gate for (j = 1, cfga_ldata = &apd->cfga_list_data[1];
2790Sstevel@tonic-gate j < apd->nlist; j++, cfga_ldata++) {
2800Sstevel@tonic-gate if (cfga_ldata->ap_o_state != CFGA_STAT_CONFIGURED) {
2810Sstevel@tonic-gate continue;
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate if ((cm_info =
2850Sstevel@tonic-gate cm_info_func(cfga_ldata->ap_type)) != NULL) {
2860Sstevel@tonic-gate cm_info_rv =
2870Sstevel@tonic-gate (*cm_info)(ap_hdl, cfga_ldata, flags, rcm);
2880Sstevel@tonic-gate if (cm_info_rv != 0) {
2890Sstevel@tonic-gate /*
2900Sstevel@tonic-gate * If we cannot obtain info for the ap,
2910Sstevel@tonic-gate * skip it and do not fail the entire
2920Sstevel@tonic-gate * operation. This case occurs when the
2930Sstevel@tonic-gate * driver for a device is not attached:
2940Sstevel@tonic-gate * di_init() returns failed back to
2950Sstevel@tonic-gate * io_cm_info().
2960Sstevel@tonic-gate */
2970Sstevel@tonic-gate if (cm_info_rv == RI_NODE_NIL)
2980Sstevel@tonic-gate continue;
2990Sstevel@tonic-gate else {
3000Sstevel@tonic-gate rv = RI_FAILURE;
3010Sstevel@tonic-gate goto out;
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate if ((flags & RI_INCLUDE_QUERY) && cap_request(ri_hdl, rcm) != 0)
3090Sstevel@tonic-gate rv = RI_FAILURE;
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate out:
3120Sstevel@tonic-gate if (apd_tbl != NULL)
3130Sstevel@tonic-gate apd_tbl_free(apd_tbl, n_apids);
3140Sstevel@tonic-gate if (rcm != NULL)
3150Sstevel@tonic-gate rcm_fini(rcm);
3160Sstevel@tonic-gate
3170Sstevel@tonic-gate if (rv == RI_SUCCESS)
3180Sstevel@tonic-gate *hdlp = ri_hdl;
3190Sstevel@tonic-gate else
3200Sstevel@tonic-gate ri_fini(ri_hdl);
3210Sstevel@tonic-gate
3220Sstevel@tonic-gate return (rv);
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate
3250Sstevel@tonic-gate /*
3260Sstevel@tonic-gate * Map static board attachment point to dynamic attachment points (components).
3270Sstevel@tonic-gate */
3280Sstevel@tonic-gate static int
dyn_ap_ids(char * ap_id,cfga_list_data_t ** ap_id_list,int * nlist)3290Sstevel@tonic-gate dyn_ap_ids(char *ap_id, cfga_list_data_t **ap_id_list, int *nlist)
3300Sstevel@tonic-gate {
3310Sstevel@tonic-gate cfga_err_t cfga_err;
3320Sstevel@tonic-gate char *errstr;
3330Sstevel@tonic-gate char *opts = "parsable";
3340Sstevel@tonic-gate char *listops = "class=sbd";
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate cfga_err = config_list_ext(1, &ap_id, ap_id_list, nlist,
3370Sstevel@tonic-gate opts, listops, &errstr, CFGA_FLAG_LIST_ALL);
3380Sstevel@tonic-gate if (cfga_err != CFGA_OK) {
3390Sstevel@tonic-gate dprintf((stderr, "config_list_ext: %s\n",
3400Sstevel@tonic-gate config_strerror(cfga_err)));
3410Sstevel@tonic-gate return (-1);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate return (0);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate /*
3480Sstevel@tonic-gate * Initialize rcm handle, memory stats. Cache query result if necessary.
3490Sstevel@tonic-gate */
3500Sstevel@tonic-gate static int
rcm_init(rcmd_t * rcm,apd_t apd_tbl[],int napds,int flags)3510Sstevel@tonic-gate rcm_init(rcmd_t *rcm, apd_t apd_tbl[], int napds, int flags)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate longlong_t ii;
3540Sstevel@tonic-gate int rv = 0;
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate rcm->offline_query_info = NULL;
3570Sstevel@tonic-gate rcm->rlist = NULL;
3580Sstevel@tonic-gate rcm->cpus = NULL;
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate if (rcm_alloc_handle(NULL, RCM_NOPID, NULL, &rcm->hdl) != RCM_SUCCESS) {
3610Sstevel@tonic-gate dprintf((stderr, "rcm_alloc_handle (errno=%d)\n", errno));
3620Sstevel@tonic-gate return (-1);
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate if ((rcm->ms_pagesize = sysconf(_SC_PAGE_SIZE)) == -1 ||
3660Sstevel@tonic-gate (rcm->ms_syspages = sysconf(_SC_PHYS_PAGES)) == -1) {
3670Sstevel@tonic-gate dprintf((stderr, "sysconf: %s\n", strerror(errno)));
3680Sstevel@tonic-gate return (-1);
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate ii = (longlong_t)rcm->ms_pagesize * rcm->ms_syspages;
3710Sstevel@tonic-gate rcm->ms_sysmb = (int)((ii+MBYTE-1) / MBYTE);
3720Sstevel@tonic-gate
3730Sstevel@tonic-gate if (flags & RI_INCLUDE_QUERY)
374*9739SJames.Anderson@Sun.COM rv = rcm_query_init(rcm, apd_tbl, napds);
3750Sstevel@tonic-gate
3760Sstevel@tonic-gate return (rv);
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate
3790Sstevel@tonic-gate static void
rcm_fini(rcmd_t * rcm)3800Sstevel@tonic-gate rcm_fini(rcmd_t *rcm)
3810Sstevel@tonic-gate {
3820Sstevel@tonic-gate char **cpp;
3830Sstevel@tonic-gate
3840Sstevel@tonic-gate assert(rcm != NULL);
3850Sstevel@tonic-gate
3860Sstevel@tonic-gate if (rcm->offline_query_info != NULL)
3870Sstevel@tonic-gate rcm_free_info(rcm->offline_query_info);
3880Sstevel@tonic-gate if (rcm->hdl != NULL)
3890Sstevel@tonic-gate rcm_free_handle(rcm->hdl);
3900Sstevel@tonic-gate
3910Sstevel@tonic-gate if (rcm->rlist != NULL) {
3920Sstevel@tonic-gate for (cpp = rcm->rlist; *cpp != NULL; cpp++)
3930Sstevel@tonic-gate s_free(*cpp);
3940Sstevel@tonic-gate free(rcm->rlist);
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate
3970Sstevel@tonic-gate s_free(rcm->cpus);
3980Sstevel@tonic-gate free(rcm);
3990Sstevel@tonic-gate }
4000Sstevel@tonic-gate
4010Sstevel@tonic-gate #define NODENAME_CMP "cmp"
4020Sstevel@tonic-gate #define NODENAME_SSM "ssm"
4030Sstevel@tonic-gate #define PROP_CPUID "cpuid"
4040Sstevel@tonic-gate #define PROP_DEVICE_TYPE "device-type"
4050Sstevel@tonic-gate #define PROP_ECACHE_SIZE "ecache-size"
4060Sstevel@tonic-gate #define PROP_L2_CACHE_SIZE "l2-cache-size"
4070Sstevel@tonic-gate #define PROP_L3_CACHE_SIZE "l3-cache-size"
4080Sstevel@tonic-gate
4090Sstevel@tonic-gate typedef struct {
4100Sstevel@tonic-gate di_node_t root;
4110Sstevel@tonic-gate di_prom_handle_t ph;
4120Sstevel@tonic-gate ecache_info_t *ecache_info;
4130Sstevel@tonic-gate } di_arg_t;
4140Sstevel@tonic-gate
4150Sstevel@tonic-gate /*
4160Sstevel@tonic-gate * The ecache sizes for individual cpus are read from the
4170Sstevel@tonic-gate * OBP/Solaris device trees. This info cannot be derived
4180Sstevel@tonic-gate * from the cfgadm_sbd cpu attachment point ecache info,
4190Sstevel@tonic-gate * which may be a sum of multiple cores for CMP.
4200Sstevel@tonic-gate */
4210Sstevel@tonic-gate static int
ecache_info_init(ecache_info_t * ec)4220Sstevel@tonic-gate ecache_info_init(ecache_info_t *ec)
4230Sstevel@tonic-gate {
4240Sstevel@tonic-gate di_arg_t di_arg;
4250Sstevel@tonic-gate di_prom_handle_t ph = DI_PROM_HANDLE_NIL;
4260Sstevel@tonic-gate di_node_t root = DI_NODE_NIL;
4270Sstevel@tonic-gate int cpuid_max, rv = 0;
4280Sstevel@tonic-gate
4290Sstevel@tonic-gate assert(ec != NULL && ec->cpuid_max == 0 && ec->ecache_sizes == NULL);
4300Sstevel@tonic-gate
4310Sstevel@tonic-gate if ((cpuid_max = sysconf(_SC_CPUID_MAX)) == -1) {
4320Sstevel@tonic-gate dprintf((stderr, "sysconf fail: %s\n", strerror(errno)));
4330Sstevel@tonic-gate rv = -1;
4340Sstevel@tonic-gate goto done;
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate
4370Sstevel@tonic-gate if ((root = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
4380Sstevel@tonic-gate dprintf((stderr, "di_init fail: %s\n", strerror(errno)));
4390Sstevel@tonic-gate rv = -1;
4400Sstevel@tonic-gate goto done;
4410Sstevel@tonic-gate }
4420Sstevel@tonic-gate
4430Sstevel@tonic-gate if ((ph = di_prom_init()) == DI_PROM_HANDLE_NIL) {
4440Sstevel@tonic-gate dprintf((stderr, "di_prom_init fail: %s\n", strerror(errno)));
4450Sstevel@tonic-gate rv = -1;
4460Sstevel@tonic-gate goto done;
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate
4490Sstevel@tonic-gate if ((ec->ecache_sizes = calloc(cpuid_max + 1, sizeof (int))) == NULL) {
4500Sstevel@tonic-gate dprintf((stderr, "calloc fail: %s\n", strerror(errno)));
4510Sstevel@tonic-gate rv = -1;
4520Sstevel@tonic-gate goto done;
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate ec->cpuid_max = cpuid_max;
4550Sstevel@tonic-gate
4560Sstevel@tonic-gate dprintf((stderr, "cpuid_max is set to %d\n", ec->cpuid_max));
4570Sstevel@tonic-gate
4580Sstevel@tonic-gate di_arg.ph = ph;
4590Sstevel@tonic-gate di_arg.root = root;
4600Sstevel@tonic-gate di_arg.ecache_info = ec;
4610Sstevel@tonic-gate
4620Sstevel@tonic-gate if (di_walk_node(root, DI_WALK_CLDFIRST, (void *)&di_arg,
4630Sstevel@tonic-gate find_cpu_nodes) != 0) {
4640Sstevel@tonic-gate dprintf((stderr, "di_walk_node fail: %s\n", strerror(errno)));
4650Sstevel@tonic-gate rv = -1;
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate
4680Sstevel@tonic-gate done:
4690Sstevel@tonic-gate if (root != DI_NODE_NIL)
4700Sstevel@tonic-gate di_fini(root);
4710Sstevel@tonic-gate if (ph != DI_PROM_HANDLE_NIL)
4720Sstevel@tonic-gate di_prom_fini(ph);
4730Sstevel@tonic-gate
4740Sstevel@tonic-gate return (rv);
4750Sstevel@tonic-gate }
4760Sstevel@tonic-gate
4770Sstevel@tonic-gate /*
4780Sstevel@tonic-gate * Libdevinfo node walk callback for reading ecache size
4790Sstevel@tonic-gate * properties for cpu device nodes. Subtrees not containing
4800Sstevel@tonic-gate * cpu nodes are filtered out.
4810Sstevel@tonic-gate */
4820Sstevel@tonic-gate static int
find_cpu_nodes(di_node_t node,void * arg)4830Sstevel@tonic-gate find_cpu_nodes(di_node_t node, void *arg)
4840Sstevel@tonic-gate {
4850Sstevel@tonic-gate char *name;
4860Sstevel@tonic-gate int *cpuid, *ecache;
4870Sstevel@tonic-gate di_arg_t *di_arg = (di_arg_t *)arg;
4880Sstevel@tonic-gate ecache_info_t *ec = di_arg->ecache_info;
4890Sstevel@tonic-gate di_prom_handle_t ph = di_arg->ph;
490*9739SJames.Anderson@Sun.COM int walk_child = 0;
4910Sstevel@tonic-gate
4920Sstevel@tonic-gate if (node == DI_NODE_NIL) {
4930Sstevel@tonic-gate return (DI_WALK_TERMINATE);
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate
4960Sstevel@tonic-gate if (node == di_arg->root) {
4970Sstevel@tonic-gate return (DI_WALK_CONTINUE);
4980Sstevel@tonic-gate }
4990Sstevel@tonic-gate
5000Sstevel@tonic-gate if (di_nodeid(node) == DI_PSEUDO_NODEID) {
5010Sstevel@tonic-gate return (DI_WALK_PRUNECHILD);
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate
5040Sstevel@tonic-gate name = di_node_name(node);
5050Sstevel@tonic-gate if (name != NULL) {
5060Sstevel@tonic-gate /*
5070Sstevel@tonic-gate * CMP nodes will be the parent of cpu nodes. On some platforms,
5080Sstevel@tonic-gate * cpu nodes will be under the ssm node. In either case,
5090Sstevel@tonic-gate * continue searching this subtree.
5100Sstevel@tonic-gate */
5110Sstevel@tonic-gate if (strncmp(name, NODENAME_SSM, strlen(NODENAME_SSM)) == 0 ||
5120Sstevel@tonic-gate strncmp(name, NODENAME_CMP, strlen(NODENAME_CMP)) == 0) {
5130Sstevel@tonic-gate return (DI_WALK_CONTINUE);
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate
5170Sstevel@tonic-gate dprintf((stderr, "find_cpu_nodes: node=%p, name=%s, binding_name=%s\n",
5180Sstevel@tonic-gate node, di_node_name(node), di_binding_name(node)));
5190Sstevel@tonic-gate
5200Sstevel@tonic-gate /*
5210Sstevel@tonic-gate * Ecache size property name differs with processor implementation.
5220Sstevel@tonic-gate * Panther has both L2 and L3, so check for L3 first to differentiate
5230Sstevel@tonic-gate * from Jaguar, which has only L2.
5240Sstevel@tonic-gate */
525*9739SJames.Anderson@Sun.COM if (prop_lookup_int(node, ph, PROP_ECACHE_SIZE, &ecache) == 0 ||
5260Sstevel@tonic-gate prop_lookup_int(node, ph, PROP_L3_CACHE_SIZE, &ecache) == 0 ||
527*9739SJames.Anderson@Sun.COM prop_lookup_int(node, ph, PROP_L2_CACHE_SIZE, &ecache) == 0) {
528*9739SJames.Anderson@Sun.COM /*
529*9739SJames.Anderson@Sun.COM * On some platforms the cache property is in the core
530*9739SJames.Anderson@Sun.COM * node while the cpuid is in the child cpu node. It may
531*9739SJames.Anderson@Sun.COM * be needed while processing this node or a child node.
532*9739SJames.Anderson@Sun.COM */
533*9739SJames.Anderson@Sun.COM ec->ecache_curr = *ecache;
534*9739SJames.Anderson@Sun.COM walk_child = 1;
535*9739SJames.Anderson@Sun.COM }
536*9739SJames.Anderson@Sun.COM
537*9739SJames.Anderson@Sun.COM if (prop_lookup_int(node, ph, PROP_CPUID, &cpuid) == 0) {
538*9739SJames.Anderson@Sun.COM
5390Sstevel@tonic-gate assert(ec != NULL && ec->ecache_sizes != NULL &&
5400Sstevel@tonic-gate *cpuid <= ec->cpuid_max);
541*9739SJames.Anderson@Sun.COM
542*9739SJames.Anderson@Sun.COM if (ec->ecache_curr != 0) {
543*9739SJames.Anderson@Sun.COM ec->ecache_sizes[*cpuid] = ec->ecache_curr;
544*9739SJames.Anderson@Sun.COM
545*9739SJames.Anderson@Sun.COM }
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate
548*9739SJames.Anderson@Sun.COM return (walk_child ? DI_WALK_CONTINUE : DI_WALK_PRUNECHILD);
5490Sstevel@tonic-gate }
5500Sstevel@tonic-gate
5510Sstevel@tonic-gate /*
5520Sstevel@tonic-gate * Given a di_node_t, call the appropriate int property lookup routine.
5530Sstevel@tonic-gate * Note: This lookup fails if the int property has multiple value entries.
5540Sstevel@tonic-gate */
5550Sstevel@tonic-gate static int
prop_lookup_int(di_node_t node,di_prom_handle_t ph,char * propname,int ** ival)5560Sstevel@tonic-gate prop_lookup_int(di_node_t node, di_prom_handle_t ph, char *propname, int **ival)
5570Sstevel@tonic-gate {
5580Sstevel@tonic-gate int rv;
5590Sstevel@tonic-gate
5600Sstevel@tonic-gate rv = (di_nodeid(node) == DI_PROM_NODEID) ?
5610Sstevel@tonic-gate di_prom_prop_lookup_ints(ph, node, propname, ival) :
5620Sstevel@tonic-gate di_prop_lookup_ints(DDI_DEV_T_ANY, node, propname, ival);
5630Sstevel@tonic-gate
5640Sstevel@tonic-gate return (rv == 1 ? 0 : -1);
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate
5670Sstevel@tonic-gate /*
5680Sstevel@tonic-gate * For offline queries, RCM must be given a list of all resources
5690Sstevel@tonic-gate * so modules can have access to the full scope of the operation.
5700Sstevel@tonic-gate * The rcm_get_info calls are made individually in order to map the
5710Sstevel@tonic-gate * returned rcm_info_t's to physical devices. The rcm_request_offline
5720Sstevel@tonic-gate * result is cached so the query state can be looked up as we process
5730Sstevel@tonic-gate * the rcm_get_info calls. This routine also tallies up the amount of
5740Sstevel@tonic-gate * memory going away and creates a list of cpu ids to be used
5750Sstevel@tonic-gate * later for rcm_request_capacity_change.
5760Sstevel@tonic-gate */
5770Sstevel@tonic-gate static int
rcm_query_init(rcmd_t * rcm,apd_t apd_tbl[],int napds)5780Sstevel@tonic-gate rcm_query_init(rcmd_t *rcm, apd_t apd_tbl[], int napds)
5790Sstevel@tonic-gate {
5800Sstevel@tonic-gate apd_t *apd;
5810Sstevel@tonic-gate int i, j;
5820Sstevel@tonic-gate cfga_list_data_t *cfga_ldata;
5830Sstevel@tonic-gate int (*cm_rcm_qpass)(cfga_list_data_t *, rcmd_t *);
5840Sstevel@tonic-gate #ifdef DEBUG
5850Sstevel@tonic-gate char **cpp;
5860Sstevel@tonic-gate #endif /* DEBUG */
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate /*
5890Sstevel@tonic-gate * Initial pass to size cpu and resource name arrays needed to
5900Sstevel@tonic-gate * interface with RCM. Attachment point ids for CMP can represent
5910Sstevel@tonic-gate * multiple cpus (and resource names). Instead of parsing the
5920Sstevel@tonic-gate * cfgadm info field here, use the worse case that all component
5930Sstevel@tonic-gate * attachment points are CMP.
5940Sstevel@tonic-gate */
5950Sstevel@tonic-gate rcm->ndevs = 0;
5960Sstevel@tonic-gate for (i = 0, apd = apd_tbl; i < napds; i++, apd++) {
5970Sstevel@tonic-gate for (j = 1, cfga_ldata = &apd->cfga_list_data[1];
5980Sstevel@tonic-gate j < apd->nlist; j++, cfga_ldata++) {
5990Sstevel@tonic-gate if (cfga_ldata->ap_o_state != CFGA_STAT_CONFIGURED) {
6000Sstevel@tonic-gate continue;
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate rcm->ndevs += SBD_MAX_CORES_PER_CMP;
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate }
6050Sstevel@tonic-gate
6060Sstevel@tonic-gate /* account for trailing NULL in rlist */
6070Sstevel@tonic-gate if (rcm->ndevs > 0 &&
6080Sstevel@tonic-gate ((rcm->cpus = calloc(rcm->ndevs, sizeof (cpuid_t))) == NULL ||
6090Sstevel@tonic-gate (rcm->rlist = calloc(rcm->ndevs + 1, sizeof (char *))) == NULL)) {
6100Sstevel@tonic-gate dprintf((stderr, "calloc: %s\n", strerror(errno)));
6110Sstevel@tonic-gate return (-1);
6120Sstevel@tonic-gate }
6130Sstevel@tonic-gate
6140Sstevel@tonic-gate /*
6150Sstevel@tonic-gate * Second pass to fill in the RCM resource and cpu lists.
6160Sstevel@tonic-gate */
6170Sstevel@tonic-gate for (i = 0, apd = apd_tbl; i < napds; i++, apd++) {
6180Sstevel@tonic-gate for (j = 1, cfga_ldata = &apd->cfga_list_data[1];
6190Sstevel@tonic-gate j < apd->nlist; j++, cfga_ldata++) {
6200Sstevel@tonic-gate if (cfga_ldata->ap_o_state != CFGA_STAT_CONFIGURED) {
6210Sstevel@tonic-gate continue;
6220Sstevel@tonic-gate }
6230Sstevel@tonic-gate if ((cm_rcm_qpass =
6240Sstevel@tonic-gate cm_rcm_qpass_func(cfga_ldata->ap_type)) != NULL &&
6250Sstevel@tonic-gate (*cm_rcm_qpass)(cfga_ldata, rcm) != 0) {
6260Sstevel@tonic-gate return (-1);
6270Sstevel@tonic-gate }
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate }
6300Sstevel@tonic-gate
6310Sstevel@tonic-gate if (rcm->nrlist == 0)
6320Sstevel@tonic-gate return (0);
6330Sstevel@tonic-gate
6340Sstevel@tonic-gate /*
6350Sstevel@tonic-gate * Cache query result. Since we are only interested in the
6360Sstevel@tonic-gate * set of RCM clients processed and not their request status,
6370Sstevel@tonic-gate * the return value is irrelevant.
6380Sstevel@tonic-gate */
6390Sstevel@tonic-gate (void) rcm_request_offline_list(rcm->hdl, rcm->rlist,
6400Sstevel@tonic-gate RCM_QUERY|RCM_SCOPE, &rcm->offline_query_info);
6410Sstevel@tonic-gate
6420Sstevel@tonic-gate #ifdef DEBUG
6430Sstevel@tonic-gate dprintf((stderr, "RCM rlist: nrlist=%d\n", rcm->nrlist));
6440Sstevel@tonic-gate for (cpp = rcm->rlist, i = 0; *cpp != NULL; cpp++, i++) {
6450Sstevel@tonic-gate dprintf((stderr, "rlist[%d]=%s\n", i, *cpp));
6460Sstevel@tonic-gate }
6470Sstevel@tonic-gate #endif /* DEBUG */
6480Sstevel@tonic-gate
6490Sstevel@tonic-gate return (0);
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate
6520Sstevel@tonic-gate static int
cap_request(ri_hdl_t * ri_hdl,rcmd_t * rcm)6530Sstevel@tonic-gate cap_request(ri_hdl_t *ri_hdl, rcmd_t *rcm)
6540Sstevel@tonic-gate {
6550Sstevel@tonic-gate return (((rcm->ncpus > 0 && cpu_cap_request(ri_hdl, rcm) != 0) ||
6560Sstevel@tonic-gate (rcm->query_pages > 0 && mem_cap_request(ri_hdl, rcm) != 0)) ?
6570Sstevel@tonic-gate -1 : 0);
6580Sstevel@tonic-gate }
6590Sstevel@tonic-gate
6600Sstevel@tonic-gate /*
6610Sstevel@tonic-gate * RCM capacity change request for cpus.
6620Sstevel@tonic-gate */
6630Sstevel@tonic-gate static int
cpu_cap_request(ri_hdl_t * ri_hdl,rcmd_t * rcm)6640Sstevel@tonic-gate cpu_cap_request(ri_hdl_t *ri_hdl, rcmd_t *rcm)
6650Sstevel@tonic-gate {
6660Sstevel@tonic-gate cpuid_t *syscpuids, *newcpuids;
6670Sstevel@tonic-gate int sysncpus, newncpus;
6680Sstevel@tonic-gate rcm_info_t *rcm_info = NULL;
6690Sstevel@tonic-gate int i, j, k;
6700Sstevel@tonic-gate nvlist_t *nvl;
6710Sstevel@tonic-gate int rv = 0;
6720Sstevel@tonic-gate
6730Sstevel@tonic-gate /* get all cpus in the system */
6740Sstevel@tonic-gate if (syscpus(&syscpuids, &sysncpus) == -1)
6750Sstevel@tonic-gate return (-1);
6760Sstevel@tonic-gate
6770Sstevel@tonic-gate newncpus = sysncpus - rcm->ncpus;
6780Sstevel@tonic-gate if ((newcpuids = calloc(newncpus, sizeof (cpuid_t))) == NULL) {
6790Sstevel@tonic-gate dprintf((stderr, "calloc: %s", strerror(errno)));
6800Sstevel@tonic-gate rv = -1;
6810Sstevel@tonic-gate goto out;
6820Sstevel@tonic-gate }
6830Sstevel@tonic-gate
6840Sstevel@tonic-gate if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
6850Sstevel@tonic-gate dprintf((stderr, "nvlist_alloc fail\n"));
6860Sstevel@tonic-gate rv = -1;
6870Sstevel@tonic-gate goto out;
6880Sstevel@tonic-gate }
6890Sstevel@tonic-gate
6900Sstevel@tonic-gate /*
6910Sstevel@tonic-gate * Construct the new cpu list.
6920Sstevel@tonic-gate */
6930Sstevel@tonic-gate for (i = 0, j = 0; i < sysncpus; i++) {
6940Sstevel@tonic-gate for (k = 0; k < rcm->ncpus; k++) {
6950Sstevel@tonic-gate if (rcm->cpus[k] == syscpuids[i]) {
6960Sstevel@tonic-gate break;
6970Sstevel@tonic-gate }
6980Sstevel@tonic-gate }
6990Sstevel@tonic-gate if (k == rcm->ncpus) {
7000Sstevel@tonic-gate newcpuids[j++] = syscpuids[i];
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate }
7030Sstevel@tonic-gate
7040Sstevel@tonic-gate if (nvlist_add_int32(nvl, "old_total", sysncpus) != 0 ||
7050Sstevel@tonic-gate nvlist_add_int32(nvl, "new_total", newncpus) != 0 ||
7060Sstevel@tonic-gate nvlist_add_int32_array(nvl, "old_cpu_list", syscpuids,
7070Sstevel@tonic-gate sysncpus) != 0 ||
7080Sstevel@tonic-gate nvlist_add_int32_array(nvl, "new_cpu_list", newcpuids,
7090Sstevel@tonic-gate newncpus) != 0) {
7100Sstevel@tonic-gate dprintf((stderr, "nvlist_add fail\n"));
7110Sstevel@tonic-gate rv = -1;
7120Sstevel@tonic-gate goto out;
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate
7150Sstevel@tonic-gate #ifdef DEBUG
7160Sstevel@tonic-gate dprintf((stderr, "old_total=%d\n", sysncpus));
7170Sstevel@tonic-gate for (i = 0; i < sysncpus; i++) {
7180Sstevel@tonic-gate dprintf((stderr, "old_cpu_list[%d]=%d\n", i, syscpuids[i]));
7190Sstevel@tonic-gate }
7200Sstevel@tonic-gate dprintf((stderr, "new_total=%d\n", newncpus));
7210Sstevel@tonic-gate for (i = 0; i < newncpus; i++) {
7220Sstevel@tonic-gate dprintf((stderr, "new_cpu_list[%d]=%d\n", i, newcpuids[i]));
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate #endif /* DEBUG */
7250Sstevel@tonic-gate
7260Sstevel@tonic-gate (void) rcm_request_capacity_change(rcm->hdl, RCM_CPU_ALL,
7270Sstevel@tonic-gate RCM_QUERY|RCM_SCOPE, nvl, &rcm_info);
7280Sstevel@tonic-gate
7290Sstevel@tonic-gate rv = add_rcm_clients(&ri_hdl->cpu_cap_clients, rcm, rcm_info, 0, NULL);
7300Sstevel@tonic-gate
7310Sstevel@tonic-gate out:
7320Sstevel@tonic-gate s_free(syscpuids);
7330Sstevel@tonic-gate s_free(newcpuids);
7340Sstevel@tonic-gate if (nvl != NULL)
7350Sstevel@tonic-gate nvlist_free(nvl);
7360Sstevel@tonic-gate if (rcm_info != NULL)
7370Sstevel@tonic-gate rcm_free_info(rcm_info);
7380Sstevel@tonic-gate
7390Sstevel@tonic-gate return (rv);
7400Sstevel@tonic-gate }
7410Sstevel@tonic-gate
7420Sstevel@tonic-gate static int
syscpus(cpuid_t ** cpuids,int * ncpus)7430Sstevel@tonic-gate syscpus(cpuid_t **cpuids, int *ncpus)
7440Sstevel@tonic-gate {
7450Sstevel@tonic-gate kstat_t *ksp;
7460Sstevel@tonic-gate kstat_ctl_t *kc;
7470Sstevel@tonic-gate cpuid_t *cp;
7480Sstevel@tonic-gate int i;
7490Sstevel@tonic-gate
7500Sstevel@tonic-gate if ((*ncpus = sysconf(_SC_NPROCESSORS_CONF)) == -1) {
7510Sstevel@tonic-gate dprintf((stderr, "sysconf: %s\n", errno));
7520Sstevel@tonic-gate return (-1);
7530Sstevel@tonic-gate }
7540Sstevel@tonic-gate
7550Sstevel@tonic-gate if ((kc = kstat_open()) == NULL) {
7560Sstevel@tonic-gate dprintf((stderr, "kstat_open fail\n"));
7570Sstevel@tonic-gate return (-1);
7580Sstevel@tonic-gate }
7590Sstevel@tonic-gate
7600Sstevel@tonic-gate if ((cp = calloc(*ncpus, sizeof (cpuid_t))) == NULL) {
7610Sstevel@tonic-gate dprintf((stderr, "calloc: %s\n", errno));
7620Sstevel@tonic-gate (void) kstat_close(kc);
7630Sstevel@tonic-gate return (-1);
7640Sstevel@tonic-gate }
7650Sstevel@tonic-gate
7660Sstevel@tonic-gate for (i = 0, ksp = kc->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
7670Sstevel@tonic-gate if (strcmp(ksp->ks_module, "cpu_info") == 0) {
7680Sstevel@tonic-gate cp[i++] = ksp->ks_instance;
7690Sstevel@tonic-gate }
7700Sstevel@tonic-gate }
7710Sstevel@tonic-gate
7720Sstevel@tonic-gate (void) kstat_close(kc);
7730Sstevel@tonic-gate *cpuids = cp;
7740Sstevel@tonic-gate
7750Sstevel@tonic-gate return (0);
7760Sstevel@tonic-gate }
7770Sstevel@tonic-gate
7780Sstevel@tonic-gate /*
7790Sstevel@tonic-gate * RCM capacity change request for memory.
7800Sstevel@tonic-gate */
7810Sstevel@tonic-gate static int
mem_cap_request(ri_hdl_t * ri_hdl,rcmd_t * rcm)7820Sstevel@tonic-gate mem_cap_request(ri_hdl_t *ri_hdl, rcmd_t *rcm)
7830Sstevel@tonic-gate {
7840Sstevel@tonic-gate nvlist_t *nvl;
7850Sstevel@tonic-gate rcm_info_t *rcm_info = NULL;
7860Sstevel@tonic-gate long newpages;
7870Sstevel@tonic-gate int rv = 0;
7880Sstevel@tonic-gate
7890Sstevel@tonic-gate if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
7900Sstevel@tonic-gate dprintf((stderr, "nvlist_alloc fail\n"));
7910Sstevel@tonic-gate return (-1);
7920Sstevel@tonic-gate }
7930Sstevel@tonic-gate
7940Sstevel@tonic-gate newpages = rcm->ms_syspages - rcm->query_pages;
7950Sstevel@tonic-gate if (nvlist_add_int32(nvl, "page_size", rcm->ms_pagesize) != 0 ||
7960Sstevel@tonic-gate nvlist_add_int32(nvl, "old_pages", rcm->ms_syspages) != 0 ||
7970Sstevel@tonic-gate nvlist_add_int32(nvl, "new_pages", newpages) != 0) {
7980Sstevel@tonic-gate dprintf((stderr, "nvlist_add fail\n"));
7990Sstevel@tonic-gate nvlist_free(nvl);
8000Sstevel@tonic-gate return (-1);
8010Sstevel@tonic-gate }
8020Sstevel@tonic-gate
8030Sstevel@tonic-gate dprintf((stderr, "memory capacity change req: "
8040Sstevel@tonic-gate "page_size=%d, old_pages=%d, new_pages=%d\n",
8050Sstevel@tonic-gate rcm->ms_pagesize, rcm->ms_syspages, newpages));
8060Sstevel@tonic-gate
8070Sstevel@tonic-gate (void) rcm_request_capacity_change(rcm->hdl, RCM_MEM_ALL,
8080Sstevel@tonic-gate RCM_QUERY|RCM_SCOPE, nvl, &rcm_info);
8090Sstevel@tonic-gate
8100Sstevel@tonic-gate rv = add_rcm_clients(&ri_hdl->mem_cap_clients, rcm, rcm_info, 0, NULL);
8110Sstevel@tonic-gate
8120Sstevel@tonic-gate nvlist_free(nvl);
8130Sstevel@tonic-gate if (rcm_info != NULL)
8140Sstevel@tonic-gate rcm_free_info(rcm_info);
8150Sstevel@tonic-gate
8160Sstevel@tonic-gate return (rv);
8170Sstevel@tonic-gate }
8180Sstevel@tonic-gate
8190Sstevel@tonic-gate static int
cm_rcm_qpass_func(cfga_type_t ap_type)8200Sstevel@tonic-gate (*cm_rcm_qpass_func(cfga_type_t ap_type))(cfga_list_data_t *, rcmd_t *)
8210Sstevel@tonic-gate {
8220Sstevel@tonic-gate int i;
8230Sstevel@tonic-gate
8240Sstevel@tonic-gate for (i = 0; i < sizeof (cm_ctl) / sizeof (cm_ctl[0]); i++) {
8250Sstevel@tonic-gate if (strcmp(cm_ctl[i].type, ap_type) == 0) {
8260Sstevel@tonic-gate return (cm_ctl[i].cm_rcm_qpass);
8270Sstevel@tonic-gate }
8280Sstevel@tonic-gate }
8290Sstevel@tonic-gate return (NULL);
8300Sstevel@tonic-gate }
8310Sstevel@tonic-gate
8320Sstevel@tonic-gate /*
8330Sstevel@tonic-gate * Save cpu ids and RCM abstract resource names.
8340Sstevel@tonic-gate * Cpu ids will be used for the capacity change request.
8350Sstevel@tonic-gate * Resource names will be used for the offline query.
8360Sstevel@tonic-gate */
8370Sstevel@tonic-gate static int
cpu_rcm_qpass(cfga_list_data_t * cfga_ldata,rcmd_t * rcm)8380Sstevel@tonic-gate cpu_rcm_qpass(cfga_list_data_t *cfga_ldata, rcmd_t *rcm)
8390Sstevel@tonic-gate {
8400Sstevel@tonic-gate processorid_t cpuid;
8410Sstevel@tonic-gate char *cpustr, *lasts, *rsrcname, rbuf[32];
8420Sstevel@tonic-gate char cbuf[CFGA_INFO_LEN];
8430Sstevel@tonic-gate int speed, ecache;
8440Sstevel@tonic-gate
8450Sstevel@tonic-gate assert(sscanf(cfga_ldata->ap_info, CPU_INFO_FMT, &cbuf, &speed,
8460Sstevel@tonic-gate &ecache) == 3);
8470Sstevel@tonic-gate
8480Sstevel@tonic-gate for (cpustr = (char *)strtok_r(cbuf, CPUID_SEP, &lasts);
8490Sstevel@tonic-gate cpustr != NULL;
8500Sstevel@tonic-gate cpustr = (char *)strtok_r(NULL, CPUID_SEP, &lasts)) {
8510Sstevel@tonic-gate cpuid = atoi(cpustr);
8520Sstevel@tonic-gate
8530Sstevel@tonic-gate (void) snprintf(rbuf, sizeof (rbuf), "%s%d", RCM_CPU, cpuid);
8540Sstevel@tonic-gate if ((rsrcname = strdup(rbuf)) == NULL) {
8550Sstevel@tonic-gate dprintf((stderr, "strdup fail\n"));
8560Sstevel@tonic-gate return (-1);
8570Sstevel@tonic-gate }
8580Sstevel@tonic-gate assert(rcm->nrlist < rcm->ndevs && rcm->ncpus < rcm->ndevs);
8590Sstevel@tonic-gate rcm->rlist[rcm->nrlist++] = rsrcname;
8600Sstevel@tonic-gate rcm->cpus[rcm->ncpus++] = (cpuid_t)cpuid;
8610Sstevel@tonic-gate
8620Sstevel@tonic-gate dprintf((stderr, "cpu_cm_info: cpuid=%d, rsrcname=%s",
8630Sstevel@tonic-gate cpuid, rsrcname));
8640Sstevel@tonic-gate }
8650Sstevel@tonic-gate
8660Sstevel@tonic-gate return (0);
8670Sstevel@tonic-gate }
8680Sstevel@tonic-gate
8690Sstevel@tonic-gate /*
8700Sstevel@tonic-gate * No RCM resource names for individual memory units, so
8710Sstevel@tonic-gate * just add to offline query page count.
8720Sstevel@tonic-gate */
8730Sstevel@tonic-gate static int
mem_rcm_qpass(cfga_list_data_t * cfga,rcmd_t * rcm)8740Sstevel@tonic-gate mem_rcm_qpass(cfga_list_data_t *cfga, rcmd_t *rcm)
8750Sstevel@tonic-gate {
8760Sstevel@tonic-gate char *cp;
8770Sstevel@tonic-gate uint_t kbytes;
8780Sstevel@tonic-gate longlong_t ii;
8790Sstevel@tonic-gate
8800Sstevel@tonic-gate if ((cp = strstr(cfga->ap_info, "size")) == NULL ||
8810Sstevel@tonic-gate sscanf(cp, "size=%u", &kbytes) != 1) {
8820Sstevel@tonic-gate dprintf((stderr, "unknown sbd info format: %s\n", cp));
8830Sstevel@tonic-gate return (-1);
8840Sstevel@tonic-gate }
8850Sstevel@tonic-gate
8860Sstevel@tonic-gate ii = (longlong_t)kbytes * KBYTE;
8870Sstevel@tonic-gate rcm->query_pages += (uint_t)(ii / rcm->ms_pagesize);
8880Sstevel@tonic-gate
8890Sstevel@tonic-gate dprintf((stderr, "%s: npages=%u\n", cfga->ap_log_id,
8900Sstevel@tonic-gate (uint_t)(ii / rcm->ms_pagesize)));
8910Sstevel@tonic-gate
8920Sstevel@tonic-gate return (0);
8930Sstevel@tonic-gate }
8940Sstevel@tonic-gate
8950Sstevel@tonic-gate /*
8960Sstevel@tonic-gate * Add physical I/O bus name to RCM resource list.
8970Sstevel@tonic-gate */
8980Sstevel@tonic-gate static int
io_rcm_qpass(cfga_list_data_t * cfga,rcmd_t * rcm)8990Sstevel@tonic-gate io_rcm_qpass(cfga_list_data_t *cfga, rcmd_t *rcm)
9000Sstevel@tonic-gate {
9010Sstevel@tonic-gate char path[MAXPATHLEN];
9020Sstevel@tonic-gate char buf[MAXPATHLEN];
9030Sstevel@tonic-gate char *rsrcname;
9040Sstevel@tonic-gate
9050Sstevel@tonic-gate if (sscanf(cfga->ap_info, "device=%s", path) != 1) {
9060Sstevel@tonic-gate dprintf((stderr, "unknown sbd info format: %s\n",
9070Sstevel@tonic-gate cfga->ap_info));
9080Sstevel@tonic-gate return (-1);
9090Sstevel@tonic-gate }
9100Sstevel@tonic-gate
9110Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "/devices%s", path);
9120Sstevel@tonic-gate if ((rsrcname = strdup(buf)) == NULL) {
9130Sstevel@tonic-gate dprintf((stderr, "strdup fail\n"));
9140Sstevel@tonic-gate return (-1);
9150Sstevel@tonic-gate }
9160Sstevel@tonic-gate
9170Sstevel@tonic-gate assert(rcm->nrlist < rcm->ndevs);
9180Sstevel@tonic-gate rcm->rlist[rcm->nrlist++] = rsrcname;
9190Sstevel@tonic-gate
9200Sstevel@tonic-gate return (0);
9210Sstevel@tonic-gate }
9220Sstevel@tonic-gate
9230Sstevel@tonic-gate static int
cm_info_func(cfga_type_t ap_type)9240Sstevel@tonic-gate (*cm_info_func(cfga_type_t ap_type))(ri_ap_t *, cfga_list_data_t *,
9250Sstevel@tonic-gate int, rcmd_t *)
9260Sstevel@tonic-gate {
9270Sstevel@tonic-gate int i;
9280Sstevel@tonic-gate
9290Sstevel@tonic-gate for (i = 0; i < sizeof (cm_ctl) / sizeof (cm_ctl[0]); i++) {
9300Sstevel@tonic-gate if (strcmp(cm_ctl[i].type, ap_type) == 0) {
9310Sstevel@tonic-gate return (cm_ctl[i].cm_info);
9320Sstevel@tonic-gate }
9330Sstevel@tonic-gate }
9340Sstevel@tonic-gate return (NULL);
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate
9370Sstevel@tonic-gate /*
9380Sstevel@tonic-gate * Create cpu handle, adding properties exported by sbd plugin and
9390Sstevel@tonic-gate * RCM client usage.
9400Sstevel@tonic-gate */
9410Sstevel@tonic-gate /* ARGSUSED */
9420Sstevel@tonic-gate static int
cpu_cm_info(ri_ap_t * ap,cfga_list_data_t * cfga,int flags,rcmd_t * rcm)9430Sstevel@tonic-gate cpu_cm_info(ri_ap_t *ap, cfga_list_data_t *cfga, int flags, rcmd_t *rcm)
9440Sstevel@tonic-gate {
9450Sstevel@tonic-gate processorid_t cpuid;
9460Sstevel@tonic-gate int speed, ecache, rv = 0;
9470Sstevel@tonic-gate char buf[CFGA_INFO_LEN], *cpustr, *lasts;
9480Sstevel@tonic-gate
9490Sstevel@tonic-gate if (sscanf(cfga->ap_info, CPU_INFO_FMT, &buf, &speed, &ecache) != 3) {
9500Sstevel@tonic-gate dprintf((stderr, "unknown sbd info format: %s\n",
9510Sstevel@tonic-gate cfga->ap_info));
9520Sstevel@tonic-gate return (-1);
9530Sstevel@tonic-gate }
9540Sstevel@tonic-gate
9550Sstevel@tonic-gate /* parse cpuids */
9560Sstevel@tonic-gate for (cpustr = (char *)strtok_r(buf, CPUID_SEP, &lasts);
9570Sstevel@tonic-gate cpustr != NULL;
9580Sstevel@tonic-gate cpustr = (char *)strtok_r(NULL, CPUID_SEP, &lasts)) {
9590Sstevel@tonic-gate cpuid = atoi(cpustr);
9603254Sscarter if ((rv = i_cpu_cm_info(cpuid, speed, ecache, ap, rcm)) != 0) {
9610Sstevel@tonic-gate break;
9620Sstevel@tonic-gate }
9630Sstevel@tonic-gate }
9640Sstevel@tonic-gate
9650Sstevel@tonic-gate return (rv);
9660Sstevel@tonic-gate }
9670Sstevel@tonic-gate
9680Sstevel@tonic-gate static int
i_cpu_cm_info(processorid_t cpuid,int speed,int ecache_cfga,ri_ap_t * ap,rcmd_t * rcm)9693254Sscarter i_cpu_cm_info(processorid_t cpuid, int speed, int ecache_cfga, ri_ap_t *ap,
9703254Sscarter rcmd_t *rcm)
9710Sstevel@tonic-gate {
972*9739SJames.Anderson@Sun.COM int ecache_mb = 0;
973*9739SJames.Anderson@Sun.COM int ecache_kb = 0;
9740Sstevel@tonic-gate char *state, buf[32];
9750Sstevel@tonic-gate processor_info_t cpu_info;
9760Sstevel@tonic-gate ri_dev_t *cpu = NULL;
9770Sstevel@tonic-gate rcm_info_t *rcm_info = NULL;
9780Sstevel@tonic-gate
9790Sstevel@tonic-gate /*
9800Sstevel@tonic-gate * Could have been unconfigured in the interim, so cannot
9810Sstevel@tonic-gate * count on processor_info recognizing it.
9820Sstevel@tonic-gate */
9830Sstevel@tonic-gate state = (processor_info(cpuid, &cpu_info) == 0) ?
9840Sstevel@tonic-gate pstate2str(cpu_info.pi_state) : "unknown";
9850Sstevel@tonic-gate
9860Sstevel@tonic-gate if ((cpu = ri_dev_alloc()) == NULL) {
9870Sstevel@tonic-gate dprintf((stderr, "ri_dev_alloc failed\n"));
9880Sstevel@tonic-gate return (-1);
9890Sstevel@tonic-gate }
9900Sstevel@tonic-gate
9913254Sscarter /*
9923254Sscarter * Assume the ecache_info table has the right e-cache size for
9933254Sscarter * this CPU. Use the value found in cfgadm (ecache_cfga) if not.
9943254Sscarter */
9950Sstevel@tonic-gate if (rcm->ecache_info.ecache_sizes != NULL) {
9960Sstevel@tonic-gate assert(rcm->ecache_info.cpuid_max != 0 &&
9970Sstevel@tonic-gate cpuid <= rcm->ecache_info.cpuid_max);
998*9739SJames.Anderson@Sun.COM ecache_mb = rcm->ecache_info.ecache_sizes[cpuid] / MBYTE;
999*9739SJames.Anderson@Sun.COM ecache_kb = rcm->ecache_info.ecache_sizes[cpuid] / KBYTE;
10000Sstevel@tonic-gate }
1001*9739SJames.Anderson@Sun.COM
1002*9739SJames.Anderson@Sun.COM if (ecache_mb == 0) {
1003*9739SJames.Anderson@Sun.COM ecache_mb = ecache_cfga;
10043254Sscarter }
10050Sstevel@tonic-gate
10060Sstevel@tonic-gate dprintf((stderr, "i_cpu_cm_info: cpu(%d) ecache=%d MB\n",
10070Sstevel@tonic-gate cpuid, ecache));
10080Sstevel@tonic-gate
10090Sstevel@tonic-gate if (nvlist_add_int32(cpu->conf_props, RI_CPU_ID, cpuid) != 0 ||
10100Sstevel@tonic-gate nvlist_add_int32(cpu->conf_props, RI_CPU_SPEED, speed) != 0 ||
1011*9739SJames.Anderson@Sun.COM nvlist_add_int32(cpu->conf_props, RI_CPU_ECACHE, ecache_mb) != 0 ||
10120Sstevel@tonic-gate nvlist_add_string(cpu->conf_props, RI_CPU_STATE, state) != 0) {
10130Sstevel@tonic-gate dprintf((stderr, "nvlist_add fail\n"));
10140Sstevel@tonic-gate ri_dev_free(cpu);
10150Sstevel@tonic-gate return (-1);
10160Sstevel@tonic-gate }
10170Sstevel@tonic-gate
1018*9739SJames.Anderson@Sun.COM /*
1019*9739SJames.Anderson@Sun.COM * Report cache size in kilobyte units if available. This info is
1020*9739SJames.Anderson@Sun.COM * added to support processors with cache sizes that are non-integer
1021*9739SJames.Anderson@Sun.COM * megabyte multiples.
1022*9739SJames.Anderson@Sun.COM */
1023*9739SJames.Anderson@Sun.COM if (ecache_kb != 0) {
1024*9739SJames.Anderson@Sun.COM if (nvlist_add_int32(cpu->conf_props, RI_CPU_ECACHE_KBYTE,
1025*9739SJames.Anderson@Sun.COM ecache_kb) != 0) {
1026*9739SJames.Anderson@Sun.COM dprintf((stderr, "nvlist_add fail: %s\n",
1027*9739SJames.Anderson@Sun.COM RI_CPU_ECACHE_KBYTE));
1028*9739SJames.Anderson@Sun.COM ri_dev_free(cpu);
1029*9739SJames.Anderson@Sun.COM return (-1);
1030*9739SJames.Anderson@Sun.COM }
1031*9739SJames.Anderson@Sun.COM }
1032*9739SJames.Anderson@Sun.COM
10330Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%s%d", RCM_CPU, cpuid);
10340Sstevel@tonic-gate dprintf((stderr, "rcm_get_info(%s)\n", buf));
10350Sstevel@tonic-gate if (rcm_get_info(rcm->hdl, buf, RCM_INCLUDE_DEPENDENT,
10360Sstevel@tonic-gate &rcm_info) != RCM_SUCCESS) {
10370Sstevel@tonic-gate dprintf((stderr, "rcm_get_info (errno=%d)\n", errno));
10380Sstevel@tonic-gate ri_dev_free(cpu);
10390Sstevel@tonic-gate if (rcm_info != NULL)
10400Sstevel@tonic-gate rcm_free_info(rcm_info);
10410Sstevel@tonic-gate return (-1);
10420Sstevel@tonic-gate }
10430Sstevel@tonic-gate
10440Sstevel@tonic-gate dev_list_cpu_insert(&ap->cpus, cpu, cpuid);
10450Sstevel@tonic-gate
10460Sstevel@tonic-gate return (0);
10470Sstevel@tonic-gate }
10480Sstevel@tonic-gate
10490Sstevel@tonic-gate /*
10500Sstevel@tonic-gate * Create memory handle, adding properties exported by sbd plugin.
10510Sstevel@tonic-gate * No RCM tuples to be saved unless RCM is modified to export names
10520Sstevel@tonic-gate * for individual memory units.
10530Sstevel@tonic-gate */
10540Sstevel@tonic-gate /* ARGSUSED */
10550Sstevel@tonic-gate static int
mem_cm_info(ri_ap_t * ap,cfga_list_data_t * cfga,int flags,rcmd_t * rcm)10560Sstevel@tonic-gate mem_cm_info(ri_ap_t *ap, cfga_list_data_t *cfga, int flags, rcmd_t *rcm)
10570Sstevel@tonic-gate {
10580Sstevel@tonic-gate ri_dev_t *mem;
10590Sstevel@tonic-gate char *cp;
10602732Sscarter char *cpval;
10612732Sscarter int len;
10622732Sscarter uint64_t base_addr; /* required */
10632732Sscarter int32_t size_kb; /* required */
10642732Sscarter int32_t perm_kb = 0; /* optional */
10652732Sscarter char target[CFGA_AP_LOG_ID_LEN] = ""; /* optional */
10662732Sscarter int32_t del_kb = 0; /* optional */
10672732Sscarter int32_t rem_kb = 0; /* optional */
10682732Sscarter char source[CFGA_AP_LOG_ID_LEN] = ""; /* optional */
10690Sstevel@tonic-gate
10700Sstevel@tonic-gate if (sscanf(cfga->ap_info, "address=0x%llx size=%u", &base_addr,
10710Sstevel@tonic-gate &size_kb) != 2) {
10720Sstevel@tonic-gate goto err_fmt;
10730Sstevel@tonic-gate }
10740Sstevel@tonic-gate
10750Sstevel@tonic-gate if ((cp = strstr(cfga->ap_info, "permanent")) != NULL &&
10760Sstevel@tonic-gate sscanf(cp, "permanent=%u", &perm_kb) != 1) {
10770Sstevel@tonic-gate goto err_fmt;
10780Sstevel@tonic-gate }
10790Sstevel@tonic-gate
10802732Sscarter if ((cp = strstr(cfga->ap_info, "target")) != NULL) {
10812732Sscarter if ((cpval = strstr(cp, "=")) == NULL) {
10822732Sscarter goto err_fmt;
10832732Sscarter }
10842732Sscarter for (len = 0; cpval[len] != '\0' && cpval[len] != ' '; len++) {
10852732Sscarter if (len >= CFGA_AP_LOG_ID_LEN) {
10862732Sscarter goto err_fmt;
10872732Sscarter }
10882732Sscarter }
10892732Sscarter if (sscanf(cp, "target=%s deleted=%u remaining=%u", &target,
10902732Sscarter &del_kb, &rem_kb) != 3) {
10912732Sscarter goto err_fmt;
10922732Sscarter }
10930Sstevel@tonic-gate }
10940Sstevel@tonic-gate
10952732Sscarter if ((cp = strstr(cfga->ap_info, "source")) != NULL) {
10962732Sscarter if ((cpval = strstr(cp, "=")) == NULL) {
10972732Sscarter goto err_fmt;
10982732Sscarter }
10992732Sscarter for (len = 0; cpval[len] != '\0' && cpval[len] != ' '; len++) {
11002732Sscarter if (len >= CFGA_AP_LOG_ID_LEN) {
11012732Sscarter goto err_fmt;
11022732Sscarter }
11032732Sscarter }
11042732Sscarter if (sscanf(cp, "source=%s", &source) != 1) {
11052732Sscarter goto err_fmt;
11062732Sscarter }
11070Sstevel@tonic-gate }
11080Sstevel@tonic-gate
11090Sstevel@tonic-gate dprintf((stderr, "%s: base=0x%llx, size=%u, permanent=%u\n",
11100Sstevel@tonic-gate cfga->ap_log_id, base_addr, size_kb, perm_kb));
11110Sstevel@tonic-gate
11120Sstevel@tonic-gate if ((mem = ri_dev_alloc()) == NULL)
11130Sstevel@tonic-gate return (-1);
11140Sstevel@tonic-gate
11150Sstevel@tonic-gate /*
11160Sstevel@tonic-gate * Convert memory sizes to MB (truncate).
11170Sstevel@tonic-gate */
11180Sstevel@tonic-gate if (nvlist_add_uint64(mem->conf_props, RI_MEM_ADDR, base_addr) != 0 ||
11190Sstevel@tonic-gate nvlist_add_int32(mem->conf_props, RI_MEM_BRD, size_kb/KBYTE) != 0 ||
11200Sstevel@tonic-gate nvlist_add_int32(mem->conf_props, RI_MEM_PERM,
11210Sstevel@tonic-gate perm_kb/KBYTE) != 0) {
11220Sstevel@tonic-gate dprintf((stderr, "nvlist_add failure\n"));
11230Sstevel@tonic-gate ri_dev_free(mem);
11240Sstevel@tonic-gate return (-1);
11250Sstevel@tonic-gate }
11260Sstevel@tonic-gate
11272732Sscarter if (target[0] != '\0' &&
11280Sstevel@tonic-gate (nvlist_add_string(mem->conf_props, RI_MEM_TARG, target) != 0 ||
11290Sstevel@tonic-gate nvlist_add_int32(mem->conf_props, RI_MEM_DEL, del_kb/KBYTE) != 0 ||
11300Sstevel@tonic-gate nvlist_add_int32(mem->conf_props, RI_MEM_REMAIN,
11310Sstevel@tonic-gate rem_kb/KBYTE) != 0)) {
11320Sstevel@tonic-gate dprintf((stderr, "nvlist_add failure\n"));
11330Sstevel@tonic-gate ri_dev_free(mem);
11340Sstevel@tonic-gate return (-1);
11350Sstevel@tonic-gate }
11360Sstevel@tonic-gate
11372732Sscarter if (source[0] != '\0' &&
11382732Sscarter nvlist_add_string(mem->conf_props, RI_MEM_SRC, source) != 0) {
11390Sstevel@tonic-gate dprintf((stderr, "nvlist_add failure\n"));
11400Sstevel@tonic-gate ri_dev_free(mem);
11410Sstevel@tonic-gate return (-1);
11420Sstevel@tonic-gate }
11430Sstevel@tonic-gate
11440Sstevel@tonic-gate /*
11450Sstevel@tonic-gate * XXX - move this property to attachment point hdl?
11460Sstevel@tonic-gate */
11470Sstevel@tonic-gate if (nvlist_add_int32(mem->conf_props, RI_MEM_DOMAIN,
11480Sstevel@tonic-gate rcm->ms_sysmb) != 0) {
11490Sstevel@tonic-gate dprintf((stderr, "nvlist_add failure\n"));
11500Sstevel@tonic-gate ri_dev_free(mem);
11510Sstevel@tonic-gate return (-1);
11520Sstevel@tonic-gate }
11530Sstevel@tonic-gate
11540Sstevel@tonic-gate dev_list_append(&ap->mems, mem);
11550Sstevel@tonic-gate return (0);
11560Sstevel@tonic-gate
11570Sstevel@tonic-gate err_fmt:
11580Sstevel@tonic-gate dprintf((stderr, "unknown sbd info format: %s\n", cfga->ap_info));
11590Sstevel@tonic-gate return (-1);
11600Sstevel@tonic-gate }
11610Sstevel@tonic-gate
11620Sstevel@tonic-gate /*
11630Sstevel@tonic-gate * Initiate a libdevinfo walk on the IO bus path.
11640Sstevel@tonic-gate * XXX - investigate performance using two threads here: one thread to do the
11650Sstevel@tonic-gate * libdevinfo snapshot and treewalk; and one thread to get RCM usage info
11660Sstevel@tonic-gate */
11670Sstevel@tonic-gate static int
io_cm_info(ri_ap_t * ap,cfga_list_data_t * cfga,int flags,rcmd_t * rcm)11680Sstevel@tonic-gate io_cm_info(ri_ap_t *ap, cfga_list_data_t *cfga, int flags, rcmd_t *rcm)
11690Sstevel@tonic-gate {
11700Sstevel@tonic-gate int i;
11710Sstevel@tonic-gate int j;
11720Sstevel@tonic-gate int k;
11730Sstevel@tonic-gate int set_size;
11740Sstevel@tonic-gate int retval = 0;
11750Sstevel@tonic-gate int n_usage;
11760Sstevel@tonic-gate devinfo_arg_t di_arg;
11770Sstevel@tonic-gate lookup_table_t devicetable;
11780Sstevel@tonic-gate lookup_entry_t *deventry;
11790Sstevel@tonic-gate lookup_entry_t *lastdeventry;
11800Sstevel@tonic-gate ri_dev_t *io = NULL;
11810Sstevel@tonic-gate ri_client_t *client;
11820Sstevel@tonic-gate ri_client_t *tmp;
11830Sstevel@tonic-gate di_devlink_handle_t linkhd = NULL;
11840Sstevel@tonic-gate di_node_t root = DI_NODE_NIL;
11850Sstevel@tonic-gate di_node_t node = DI_NODE_NIL;
11860Sstevel@tonic-gate rcm_info_tuple_t *rcm_tuple;
11870Sstevel@tonic-gate rcm_info_t *rcm_info = NULL;
11880Sstevel@tonic-gate const char *rcm_rsrc = NULL;
11890Sstevel@tonic-gate char drv_inst[MAXPATHLEN];
11900Sstevel@tonic-gate char path[MAXPATHLEN];
11910Sstevel@tonic-gate char pathbuf[MAXPATHLEN];
11920Sstevel@tonic-gate
11930Sstevel@tonic-gate dprintf((stderr, "io_cm_info(%s)\n", cfga->ap_log_id));
11940Sstevel@tonic-gate
11950Sstevel@tonic-gate /* Extract devfs path from cfgadm information */
11960Sstevel@tonic-gate if (sscanf(cfga->ap_info, "device=%s\n", path) != 1) {
11970Sstevel@tonic-gate dprintf((stderr, "unknown sbd info format: %s\n",
11980Sstevel@tonic-gate cfga->ap_info));
11990Sstevel@tonic-gate return (-1);
12000Sstevel@tonic-gate }
12010Sstevel@tonic-gate
12020Sstevel@tonic-gate /* Initialize empty device lookup table */
12030Sstevel@tonic-gate devicetable.n_entries = 0;
12040Sstevel@tonic-gate devicetable.n_slots = 0;
12050Sstevel@tonic-gate devicetable.table = NULL;
12060Sstevel@tonic-gate
12070Sstevel@tonic-gate /* Get libdevinfo snapshot */
12080Sstevel@tonic-gate dprintf((stderr, "di_init(%s)\n", path));
12090Sstevel@tonic-gate if ((root = di_init(path, DINFOCPYALL)) == DI_NODE_NIL) {
12100Sstevel@tonic-gate dprintf((stderr, "di_init: %s\n", strerror(errno)));
12110Sstevel@tonic-gate retval = RI_NODE_NIL; /* tell ri_init to skip this node */
12120Sstevel@tonic-gate goto end;
12130Sstevel@tonic-gate }
12140Sstevel@tonic-gate
12150Sstevel@tonic-gate /*
12160Sstevel@tonic-gate * Map in devlinks database.
12170Sstevel@tonic-gate * XXX - This could be moved to ri_init() for better performance.
12180Sstevel@tonic-gate */
12190Sstevel@tonic-gate dprintf((stderr, "di_devlink_init()\n"));
12200Sstevel@tonic-gate if ((linkhd = di_devlink_init(NULL, 0)) == NULL) {
12210Sstevel@tonic-gate dprintf((stderr, "di_devlink_init: %s\n", strerror(errno)));
12220Sstevel@tonic-gate retval = -1;
12230Sstevel@tonic-gate goto end;
12240Sstevel@tonic-gate }
12250Sstevel@tonic-gate
12260Sstevel@tonic-gate /* Initialize argument for devinfo treewalk */
12270Sstevel@tonic-gate di_arg.err = 0;
12280Sstevel@tonic-gate di_arg.node = DI_NODE_NIL;
12290Sstevel@tonic-gate di_arg.pathbuf = pathbuf;
12300Sstevel@tonic-gate di_arg.table = &devicetable;
12310Sstevel@tonic-gate di_arg.linkhd = linkhd;
12320Sstevel@tonic-gate
12330Sstevel@tonic-gate /* Use libdevinfo treewalk to build device lookup table */
12340Sstevel@tonic-gate if (di_walk_node(root, DI_WALK_CLDFIRST, (void *)&di_arg,
12350Sstevel@tonic-gate devinfo_node_walk) != 0) {
12360Sstevel@tonic-gate dprintf((stderr, "di_walk_node: %s\n", strerror(errno)));
12370Sstevel@tonic-gate retval = -1;
12380Sstevel@tonic-gate goto end;
12390Sstevel@tonic-gate }
12400Sstevel@tonic-gate if (di_arg.err != 0) {
12410Sstevel@tonic-gate dprintf((stderr, "di_walk_node: device tree walk failed\n"));
12420Sstevel@tonic-gate retval = -1;
12430Sstevel@tonic-gate goto end;
12440Sstevel@tonic-gate }
12450Sstevel@tonic-gate
12460Sstevel@tonic-gate /* Call RCM to gather usage information */
12470Sstevel@tonic-gate (void) snprintf(pathbuf, MAXPATHLEN, "/devices%s", path);
12480Sstevel@tonic-gate dprintf((stderr, "rcm_get_info(%s)\n", pathbuf));
12490Sstevel@tonic-gate if (rcm_get_info(rcm->hdl, pathbuf,
12500Sstevel@tonic-gate RCM_INCLUDE_SUBTREE|RCM_INCLUDE_DEPENDENT, &rcm_info) !=
12510Sstevel@tonic-gate RCM_SUCCESS) {
12520Sstevel@tonic-gate dprintf((stderr, "rcm_get_info (errno=%d)\n", errno));
12530Sstevel@tonic-gate retval = -1;
12540Sstevel@tonic-gate goto end;
12550Sstevel@tonic-gate }
12560Sstevel@tonic-gate
12570Sstevel@tonic-gate /* Sort the device table by name (proper order for lookups) */
12580Sstevel@tonic-gate qsort(devicetable.table, devicetable.n_entries, sizeof (lookup_entry_t),
12590Sstevel@tonic-gate table_compare_names);
12600Sstevel@tonic-gate
12610Sstevel@tonic-gate /* Perform mappings of RCM usage segments to device table entries */
12620Sstevel@tonic-gate lastdeventry = NULL;
12630Sstevel@tonic-gate rcm_tuple = NULL;
12640Sstevel@tonic-gate while ((rcm_tuple = rcm_info_next(rcm_info, rcm_tuple)) != NULL) {
12650Sstevel@tonic-gate if ((rcm_rsrc = rcm_info_rsrc(rcm_tuple)) == NULL)
12660Sstevel@tonic-gate continue;
12670Sstevel@tonic-gate if (deventry = lookup(&devicetable, rcm_rsrc)) {
12680Sstevel@tonic-gate if (add_usage(deventry, rcm_rsrc, rcm_tuple)) {
12690Sstevel@tonic-gate retval = -1;
12700Sstevel@tonic-gate goto end;
12710Sstevel@tonic-gate }
12720Sstevel@tonic-gate lastdeventry = deventry;
12730Sstevel@tonic-gate } else {
12740Sstevel@tonic-gate if (add_usage(lastdeventry, rcm_rsrc, rcm_tuple)) {
12750Sstevel@tonic-gate retval = -1;
12760Sstevel@tonic-gate goto end;
12770Sstevel@tonic-gate }
12780Sstevel@tonic-gate }
12790Sstevel@tonic-gate }
12800Sstevel@tonic-gate
12810Sstevel@tonic-gate /* Re-sort the device table by index number (original treewalk order) */
12820Sstevel@tonic-gate qsort(devicetable.table, devicetable.n_entries, sizeof (lookup_entry_t),
12830Sstevel@tonic-gate table_compare_indices);
12840Sstevel@tonic-gate
12850Sstevel@tonic-gate /*
12860Sstevel@tonic-gate * Use the mapped usage and the device table to construct ri_dev_t's.
12870Sstevel@tonic-gate * Construct one for each set of entries in the device table with
12880Sstevel@tonic-gate * matching di_node_t's, if: 1) it has mapped RCM usage, or 2) it is
12890Sstevel@tonic-gate * a leaf node and the caller has requested that unmanaged nodes be
12900Sstevel@tonic-gate * included in the output.
12910Sstevel@tonic-gate */
12920Sstevel@tonic-gate i = 0;
12930Sstevel@tonic-gate while (i < devicetable.n_entries) {
12940Sstevel@tonic-gate
12950Sstevel@tonic-gate node = devicetable.table[i].node;
12960Sstevel@tonic-gate
12970Sstevel@tonic-gate /* Count how many usage records are mapped to this node's set */
12980Sstevel@tonic-gate n_usage = 0;
12990Sstevel@tonic-gate set_size = 0;
13000Sstevel@tonic-gate while (((i + set_size) < devicetable.n_entries) &&
13010Sstevel@tonic-gate (devicetable.table[i + set_size].node == node)) {
13020Sstevel@tonic-gate n_usage += devicetable.table[i + set_size].n_usage;
13030Sstevel@tonic-gate set_size += 1;
13040Sstevel@tonic-gate }
13050Sstevel@tonic-gate
13060Sstevel@tonic-gate /*
13070Sstevel@tonic-gate * If there's no usage, then the node is unmanaged. Skip this
13080Sstevel@tonic-gate * set of devicetable entries unless the node is a leaf node
13090Sstevel@tonic-gate * and the caller has requested information on unmanaged leaves.
13100Sstevel@tonic-gate */
13110Sstevel@tonic-gate if ((n_usage == 0) &&
13120Sstevel@tonic-gate !((flags & RI_INCLUDE_UNMANAGED) && (ident_leaf(node)))) {
13130Sstevel@tonic-gate i += set_size;
13140Sstevel@tonic-gate continue;
13150Sstevel@tonic-gate }
13160Sstevel@tonic-gate
13170Sstevel@tonic-gate /*
13180Sstevel@tonic-gate * The checks above determined that this node is going in.
13190Sstevel@tonic-gate * So determine its driver/instance name and allocate an
13200Sstevel@tonic-gate * ri_dev_t for this node.
13210Sstevel@tonic-gate */
13220Sstevel@tonic-gate if (mk_drv_inst(node, drv_inst, devicetable.table[i].name)) {
13230Sstevel@tonic-gate dprintf((stderr, "mk_drv_inst failed\n"));
13240Sstevel@tonic-gate retval = -1;
13250Sstevel@tonic-gate break;
13260Sstevel@tonic-gate }
13270Sstevel@tonic-gate if ((io = io_dev_alloc(drv_inst)) == NULL) {
13280Sstevel@tonic-gate dprintf((stderr, "io_dev_alloc failed\n"));
13290Sstevel@tonic-gate retval = -1;
13300Sstevel@tonic-gate break;
13310Sstevel@tonic-gate }
13320Sstevel@tonic-gate
13330Sstevel@tonic-gate /* Now add all the RCM usage records (if any) to the ri_dev_t */
13340Sstevel@tonic-gate for (j = i; j < (i + set_size); j++) {
13350Sstevel@tonic-gate for (k = 0; k < devicetable.table[j].n_usage; k++) {
13360Sstevel@tonic-gate /* Create new ri_client_t for basic usage */
13370Sstevel@tonic-gate client = ri_client_alloc(
13380Sstevel@tonic-gate (char *)devicetable.table[j].usage[k].rsrc,
13390Sstevel@tonic-gate (char *)devicetable.table[j].usage[k].info);
13400Sstevel@tonic-gate if (client == NULL) {
13410Sstevel@tonic-gate dprintf((stderr,
13420Sstevel@tonic-gate "ri_client_alloc failed\n"));
13430Sstevel@tonic-gate ri_dev_free(io);
13440Sstevel@tonic-gate retval = -1;
13450Sstevel@tonic-gate goto end;
13460Sstevel@tonic-gate }
13470Sstevel@tonic-gate
13480Sstevel@tonic-gate /* Add extra query usage to the ri_client_t */
13490Sstevel@tonic-gate if ((flags & RI_INCLUDE_QUERY) &&
13500Sstevel@tonic-gate (add_query_state(rcm, client,
13510Sstevel@tonic-gate devicetable.table[j].usage[k].rsrc,
13520Sstevel@tonic-gate devicetable.table[j].usage[k].info) != 0)) {
13530Sstevel@tonic-gate dprintf((stderr,
13540Sstevel@tonic-gate "add_query_state failed\n"));
13550Sstevel@tonic-gate ri_dev_free(io);
13560Sstevel@tonic-gate ri_client_free(client);
13570Sstevel@tonic-gate retval = -1;
13580Sstevel@tonic-gate goto end;
13590Sstevel@tonic-gate }
13600Sstevel@tonic-gate
13610Sstevel@tonic-gate /* Link new ri_client_t to ri_dev_t */
13620Sstevel@tonic-gate if (io->rcm_clients) {
13630Sstevel@tonic-gate tmp = io->rcm_clients;
13640Sstevel@tonic-gate while (tmp->next)
13650Sstevel@tonic-gate tmp = tmp->next;
13660Sstevel@tonic-gate tmp->next = client;
13670Sstevel@tonic-gate } else {
13680Sstevel@tonic-gate io->rcm_clients = client;
13690Sstevel@tonic-gate }
13700Sstevel@tonic-gate }
13710Sstevel@tonic-gate }
13720Sstevel@tonic-gate
13730Sstevel@tonic-gate /* Link the ri_dev_t into the return value */
13740Sstevel@tonic-gate dev_list_append(&ap->ios, io);
13750Sstevel@tonic-gate
13760Sstevel@tonic-gate /* Advance to the next node set */
13770Sstevel@tonic-gate i += set_size;
13780Sstevel@tonic-gate }
13790Sstevel@tonic-gate
13800Sstevel@tonic-gate end:
13810Sstevel@tonic-gate if (rcm_info != NULL)
13820Sstevel@tonic-gate rcm_free_info(rcm_info);
13830Sstevel@tonic-gate if (linkhd != NULL)
13840Sstevel@tonic-gate di_devlink_fini(&linkhd);
13850Sstevel@tonic-gate if (root != DI_NODE_NIL)
13860Sstevel@tonic-gate di_fini(root);
13870Sstevel@tonic-gate empty_table(&devicetable);
13880Sstevel@tonic-gate
13890Sstevel@tonic-gate dprintf((stderr, "io_cm_info: returning %d\n", retval));
13900Sstevel@tonic-gate return (retval);
13910Sstevel@tonic-gate }
13920Sstevel@tonic-gate
13930Sstevel@tonic-gate static int
ident_leaf(di_node_t node)13940Sstevel@tonic-gate ident_leaf(di_node_t node)
13950Sstevel@tonic-gate {
13960Sstevel@tonic-gate di_minor_t minor = DI_MINOR_NIL;
13970Sstevel@tonic-gate
13980Sstevel@tonic-gate return ((minor = di_minor_next(node, minor)) != DI_MINOR_NIL &&
13990Sstevel@tonic-gate di_child_node(node) == DI_NODE_NIL);
14000Sstevel@tonic-gate }
14010Sstevel@tonic-gate
14020Sstevel@tonic-gate /* ARGSUSED */
14030Sstevel@tonic-gate static int
mk_drv_inst(di_node_t node,char drv_inst[],char * devfs_path)14040Sstevel@tonic-gate mk_drv_inst(di_node_t node, char drv_inst[], char *devfs_path)
14050Sstevel@tonic-gate {
14060Sstevel@tonic-gate char *drv;
14070Sstevel@tonic-gate int inst;
14080Sstevel@tonic-gate
14090Sstevel@tonic-gate if ((drv = di_driver_name(node)) == NULL) {
14100Sstevel@tonic-gate dprintf((stderr, "no driver bound to %s\n",
14110Sstevel@tonic-gate devfs_path));
14120Sstevel@tonic-gate return (-1);
14130Sstevel@tonic-gate }
14140Sstevel@tonic-gate
14150Sstevel@tonic-gate if ((inst = di_instance(node)) == -1) {
14160Sstevel@tonic-gate dprintf((stderr, "no instance assigned to %s\n",
14170Sstevel@tonic-gate devfs_path));
14180Sstevel@tonic-gate return (-1);
14190Sstevel@tonic-gate }
14200Sstevel@tonic-gate (void) snprintf(drv_inst, MAXPATHLEN, "%s%d", drv, inst);
14210Sstevel@tonic-gate
14220Sstevel@tonic-gate return (0);
14230Sstevel@tonic-gate }
14240Sstevel@tonic-gate
14250Sstevel@tonic-gate /*
14260Sstevel@tonic-gate * Libdevinfo walker.
14270Sstevel@tonic-gate *
14280Sstevel@tonic-gate * During the tree walk of the attached IO devices, for each node
14290Sstevel@tonic-gate * and all of its associated minors, the following actions are performed:
14300Sstevel@tonic-gate * - The /devices path of the physical device node or minor
14310Sstevel@tonic-gate * is stored in a lookup table along with a reference to the
14320Sstevel@tonic-gate * libdevinfo node it represents via add_lookup_entry().
14330Sstevel@tonic-gate * - The device links associated with each device are also
14340Sstevel@tonic-gate * stored in the same lookup table along with a reference to
14350Sstevel@tonic-gate * the libdevinfo node it represents via the minor walk callback.
14360Sstevel@tonic-gate *
14370Sstevel@tonic-gate */
14380Sstevel@tonic-gate static int
devinfo_node_walk(di_node_t node,void * arg)14390Sstevel@tonic-gate devinfo_node_walk(di_node_t node, void *arg)
14400Sstevel@tonic-gate {
14410Sstevel@tonic-gate char *devfs_path;
14420Sstevel@tonic-gate #ifdef DEBUG
14430Sstevel@tonic-gate char *drv;
14440Sstevel@tonic-gate #endif /* DEBUG */
14450Sstevel@tonic-gate devinfo_arg_t *di_arg = (devinfo_arg_t *)arg;
14460Sstevel@tonic-gate
14470Sstevel@tonic-gate if (node == DI_NODE_NIL) {
14480Sstevel@tonic-gate return (DI_WALK_TERMINATE);
14490Sstevel@tonic-gate }
14500Sstevel@tonic-gate
14510Sstevel@tonic-gate if (((di_state(node) & DI_DRIVER_DETACHED) == 0) &&
14520Sstevel@tonic-gate ((devfs_path = di_devfs_path(node)) != NULL)) {
14530Sstevel@tonic-gate
14540Sstevel@tonic-gate /* Use the provided path buffer to create full /devices path */
14550Sstevel@tonic-gate (void) snprintf(di_arg->pathbuf, MAXPATHLEN, "/devices%s",
14560Sstevel@tonic-gate devfs_path);
14570Sstevel@tonic-gate
14580Sstevel@tonic-gate #ifdef DEBUG
14590Sstevel@tonic-gate dprintf((stderr, "devinfo_node_walk(%s)\n", di_arg->pathbuf));
14600Sstevel@tonic-gate if ((drv = di_driver_name(node)) != NULL)
14610Sstevel@tonic-gate dprintf((stderr, " driver name %s instance %d\n", drv,
14620Sstevel@tonic-gate di_instance(node)));
14630Sstevel@tonic-gate #endif
14640Sstevel@tonic-gate
14650Sstevel@tonic-gate /* Free the devfs_path */
14660Sstevel@tonic-gate di_devfs_path_free(devfs_path);
14670Sstevel@tonic-gate
14680Sstevel@tonic-gate /* Add an entry to the lookup table for this physical device */
14690Sstevel@tonic-gate if (add_lookup_entry(di_arg->table, di_arg->pathbuf, node)) {
14700Sstevel@tonic-gate dprintf((stderr, "add_lookup_entry: %s\n",
14710Sstevel@tonic-gate strerror(errno)));
14720Sstevel@tonic-gate di_arg->err = 1;
14730Sstevel@tonic-gate return (DI_WALK_TERMINATE);
14740Sstevel@tonic-gate }
14750Sstevel@tonic-gate
14760Sstevel@tonic-gate /* Check if this node has minors */
14770Sstevel@tonic-gate if ((di_minor_next(node, DI_MINOR_NIL)) != DI_MINOR_NIL) {
14780Sstevel@tonic-gate /* Walk this node's minors */
14790Sstevel@tonic-gate di_arg->node = node;
14800Sstevel@tonic-gate if (di_walk_minor(node, NULL, DI_CHECK_ALIAS, arg,
14810Sstevel@tonic-gate devinfo_minor_walk) != 0) {
14820Sstevel@tonic-gate dprintf((stderr, "di_walk_minor: %s\n",
14830Sstevel@tonic-gate strerror(errno)));
14840Sstevel@tonic-gate di_arg->err = 1;
14850Sstevel@tonic-gate return (DI_WALK_TERMINATE);
14860Sstevel@tonic-gate }
14870Sstevel@tonic-gate }
14880Sstevel@tonic-gate }
14890Sstevel@tonic-gate
14900Sstevel@tonic-gate return (DI_WALK_CONTINUE);
14910Sstevel@tonic-gate }
14920Sstevel@tonic-gate
14930Sstevel@tonic-gate /*
14940Sstevel@tonic-gate * Use di_devlink_walk to find the /dev link from /devices path for this minor
14950Sstevel@tonic-gate */
14960Sstevel@tonic-gate static int
devinfo_minor_walk(di_node_t node,di_minor_t minor,void * arg)14970Sstevel@tonic-gate devinfo_minor_walk(di_node_t node, di_minor_t minor, void *arg)
14980Sstevel@tonic-gate {
14990Sstevel@tonic-gate char *name;
15000Sstevel@tonic-gate char *devfs_path;
15010Sstevel@tonic-gate devinfo_arg_t *di_arg = (devinfo_arg_t *)arg;
15020Sstevel@tonic-gate char pathbuf[MAXPATHLEN];
15030Sstevel@tonic-gate
15040Sstevel@tonic-gate #ifdef DEBUG
15050Sstevel@tonic-gate dprintf((stderr, "devinfo_minor_walk(%d) %s\n", minor,
15060Sstevel@tonic-gate di_arg->pathbuf));
15070Sstevel@tonic-gate
15080Sstevel@tonic-gate if ((name = di_minor_name(minor)) != NULL) {
15090Sstevel@tonic-gate dprintf((stderr, " minor name %s\n", name));
15100Sstevel@tonic-gate }
15110Sstevel@tonic-gate #endif /* DEBUG */
15120Sstevel@tonic-gate
15130Sstevel@tonic-gate /* Terminate the walk when the device node changes */
15140Sstevel@tonic-gate if (node != di_arg->node) {
15150Sstevel@tonic-gate return (DI_WALK_TERMINATE);
15160Sstevel@tonic-gate }
15170Sstevel@tonic-gate
15180Sstevel@tonic-gate /* Construct full /devices path for this minor */
15190Sstevel@tonic-gate if ((name = di_minor_name(minor)) == NULL) {
15200Sstevel@tonic-gate return (DI_WALK_CONTINUE);
15210Sstevel@tonic-gate }
15220Sstevel@tonic-gate (void) snprintf(pathbuf, MAXPATHLEN, "%s:%s", di_arg->pathbuf, name);
15230Sstevel@tonic-gate
15240Sstevel@tonic-gate /* Add lookup entry for this minor node */
15250Sstevel@tonic-gate if (add_lookup_entry(di_arg->table, pathbuf, node)) {
15260Sstevel@tonic-gate dprintf((stderr, "add_lookup_entry: %s\n", strerror(errno)));
15270Sstevel@tonic-gate di_arg->err = 1;
15280Sstevel@tonic-gate return (DI_WALK_TERMINATE);
15290Sstevel@tonic-gate }
15300Sstevel@tonic-gate
15310Sstevel@tonic-gate /*
15320Sstevel@tonic-gate * Walk the associated device links.
15330Sstevel@tonic-gate * Note that di_devlink_walk() doesn't want "/devices" in its paths.
15340Sstevel@tonic-gate * Also note that di_devlink_walk() will fail if there are no device
15350Sstevel@tonic-gate * links, which is fine; so ignore if it fails. Only check for
15360Sstevel@tonic-gate * internal failures during such a walk.
15370Sstevel@tonic-gate */
15380Sstevel@tonic-gate devfs_path = &pathbuf[strlen("/devices")];
15390Sstevel@tonic-gate (void) di_devlink_walk(di_arg->linkhd, NULL, devfs_path, 0, arg,
15400Sstevel@tonic-gate devinfo_devlink_walk);
15410Sstevel@tonic-gate if (di_arg->err != 0) {
15420Sstevel@tonic-gate return (DI_WALK_TERMINATE);
15430Sstevel@tonic-gate }
15440Sstevel@tonic-gate
15450Sstevel@tonic-gate return (DI_WALK_CONTINUE);
15460Sstevel@tonic-gate }
15470Sstevel@tonic-gate
15480Sstevel@tonic-gate static int
devinfo_devlink_walk(di_devlink_t devlink,void * arg)15490Sstevel@tonic-gate devinfo_devlink_walk(di_devlink_t devlink, void *arg)
15500Sstevel@tonic-gate {
15510Sstevel@tonic-gate const char *linkpath;
15520Sstevel@tonic-gate devinfo_arg_t *di_arg = (devinfo_arg_t *)arg;
15530Sstevel@tonic-gate
15540Sstevel@tonic-gate /* Get the devlink's path */
15550Sstevel@tonic-gate if ((linkpath = di_devlink_path(devlink)) == NULL) {
15560Sstevel@tonic-gate dprintf((stderr, "di_devlink_path: %s\n", strerror(errno)));
15570Sstevel@tonic-gate di_arg->err = 1;
15580Sstevel@tonic-gate return (DI_WALK_TERMINATE);
15590Sstevel@tonic-gate }
15600Sstevel@tonic-gate dprintf((stderr, "devinfo_devlink_walk: %s\n", linkpath));
15610Sstevel@tonic-gate
15620Sstevel@tonic-gate /* Add lookup entry for this devlink */
15630Sstevel@tonic-gate if (add_lookup_entry(di_arg->table, linkpath, di_arg->node)) {
15640Sstevel@tonic-gate dprintf((stderr, "add_lookup_entry: %s\n", strerror(errno)));
15650Sstevel@tonic-gate di_arg->err = 1;
15660Sstevel@tonic-gate return (DI_WALK_TERMINATE);
15670Sstevel@tonic-gate }
15680Sstevel@tonic-gate
15690Sstevel@tonic-gate return (DI_WALK_CONTINUE);
15700Sstevel@tonic-gate }
15710Sstevel@tonic-gate
15720Sstevel@tonic-gate /*
15730Sstevel@tonic-gate * Map rcm_info_t's to ri_client_t's, filtering out "uninteresting" (hack)
15740Sstevel@tonic-gate * RCM clients. The number of "interesting" ri_client_t's is returned
15750Sstevel@tonic-gate * in cnt if passed non-NULL.
15760Sstevel@tonic-gate */
15770Sstevel@tonic-gate static int
add_rcm_clients(ri_client_t ** client_list,rcmd_t * rcm,rcm_info_t * info,int flags,int * cnt)15780Sstevel@tonic-gate add_rcm_clients(ri_client_t **client_list, rcmd_t *rcm, rcm_info_t *info,
15790Sstevel@tonic-gate int flags, int *cnt)
15800Sstevel@tonic-gate {
15810Sstevel@tonic-gate rcm_info_tuple_t *tuple;
15820Sstevel@tonic-gate char *rsrc, *usage;
15830Sstevel@tonic-gate ri_client_t *client, *tmp;
15840Sstevel@tonic-gate
15850Sstevel@tonic-gate assert(client_list != NULL && rcm != NULL);
15860Sstevel@tonic-gate
15870Sstevel@tonic-gate if (info == NULL)
15880Sstevel@tonic-gate return (0);
15890Sstevel@tonic-gate
15900Sstevel@tonic-gate if (cnt != NULL)
15910Sstevel@tonic-gate *cnt = 0;
15920Sstevel@tonic-gate
15930Sstevel@tonic-gate tuple = NULL;
15940Sstevel@tonic-gate while ((tuple = rcm_info_next(info, tuple)) != NULL) {
15950Sstevel@tonic-gate if ((rsrc = (char *)rcm_info_rsrc(tuple)) == NULL ||
15960Sstevel@tonic-gate (usage = (char *)rcm_info_info(tuple)) == NULL) {
15970Sstevel@tonic-gate continue;
15980Sstevel@tonic-gate }
15990Sstevel@tonic-gate
16000Sstevel@tonic-gate if (rcm_ignore(rsrc, usage) == 0)
16010Sstevel@tonic-gate continue;
16020Sstevel@tonic-gate
16030Sstevel@tonic-gate if ((client = ri_client_alloc(rsrc, usage)) == NULL)
16040Sstevel@tonic-gate return (-1);
16050Sstevel@tonic-gate
16060Sstevel@tonic-gate if ((flags & RI_INCLUDE_QUERY) && add_query_state(rcm, client,
16070Sstevel@tonic-gate rsrc, usage) != 0) {
16080Sstevel@tonic-gate ri_client_free(client);
16090Sstevel@tonic-gate return (-1);
16100Sstevel@tonic-gate }
16110Sstevel@tonic-gate
16120Sstevel@tonic-gate if (cnt != NULL)
16130Sstevel@tonic-gate ++*cnt;
16140Sstevel@tonic-gate
16150Sstevel@tonic-gate /*
16160Sstevel@tonic-gate * Link in
16170Sstevel@tonic-gate */
16180Sstevel@tonic-gate if ((tmp = *client_list) == NULL) {
16190Sstevel@tonic-gate *client_list = client;
16200Sstevel@tonic-gate continue;
16210Sstevel@tonic-gate }
16220Sstevel@tonic-gate while (tmp->next != NULL) {
16230Sstevel@tonic-gate tmp = tmp->next;
16240Sstevel@tonic-gate }
16250Sstevel@tonic-gate tmp->next = client;
16260Sstevel@tonic-gate }
16270Sstevel@tonic-gate
16280Sstevel@tonic-gate return (0);
16290Sstevel@tonic-gate }
16300Sstevel@tonic-gate
16310Sstevel@tonic-gate /*
16320Sstevel@tonic-gate * Currently only filtering out based on known info string prefixes.
16330Sstevel@tonic-gate */
16340Sstevel@tonic-gate /* ARGSUSED */
16350Sstevel@tonic-gate static int
rcm_ignore(char * rsrc,char * infostr)16360Sstevel@tonic-gate rcm_ignore(char *rsrc, char *infostr)
16370Sstevel@tonic-gate {
16380Sstevel@tonic-gate char **cpp;
16390Sstevel@tonic-gate
16400Sstevel@tonic-gate for (cpp = rcm_info_filter; *cpp != NULL; cpp++) {
16410Sstevel@tonic-gate if (strncmp(infostr, *cpp, strlen(*cpp)) == 0) {
16420Sstevel@tonic-gate return (0);
16430Sstevel@tonic-gate }
16440Sstevel@tonic-gate }
16450Sstevel@tonic-gate return (-1);
16460Sstevel@tonic-gate }
16470Sstevel@tonic-gate
16480Sstevel@tonic-gate /*
16490Sstevel@tonic-gate * If this tuple was cached in the offline query pass, add the
16500Sstevel@tonic-gate * query state and error string to the ri_client_t.
16510Sstevel@tonic-gate */
16520Sstevel@tonic-gate static int
add_query_state(rcmd_t * rcm,ri_client_t * client,const char * rsrc,const char * info)16530Sstevel@tonic-gate add_query_state(rcmd_t *rcm, ri_client_t *client, const char *rsrc,
16540Sstevel@tonic-gate const char *info)
16550Sstevel@tonic-gate {
16560Sstevel@tonic-gate int qstate = RI_QUERY_UNKNOWN;
16570Sstevel@tonic-gate char *errstr = NULL;
16580Sstevel@tonic-gate rcm_info_tuple_t *cached_tuple;
16590Sstevel@tonic-gate
16600Sstevel@tonic-gate if ((cached_tuple = tuple_lookup(rcm, rsrc, info)) != NULL) {
16610Sstevel@tonic-gate qstate = state2query(rcm_info_state(cached_tuple));
16620Sstevel@tonic-gate errstr = (char *)rcm_info_error(cached_tuple);
16630Sstevel@tonic-gate }
16640Sstevel@tonic-gate
16650Sstevel@tonic-gate if (nvlist_add_int32(client->usg_props, RI_QUERY_STATE, qstate) != 0 ||
16660Sstevel@tonic-gate (errstr != NULL && nvlist_add_string(client->usg_props,
16670Sstevel@tonic-gate RI_QUERY_ERR, errstr) != 0)) {
16680Sstevel@tonic-gate dprintf((stderr, "nvlist_add fail\n"));
16690Sstevel@tonic-gate return (-1);
16700Sstevel@tonic-gate }
16710Sstevel@tonic-gate
16720Sstevel@tonic-gate return (0);
16730Sstevel@tonic-gate }
16740Sstevel@tonic-gate
16750Sstevel@tonic-gate static int
state2query(int rcm_state)16760Sstevel@tonic-gate state2query(int rcm_state)
16770Sstevel@tonic-gate {
16780Sstevel@tonic-gate int query;
16790Sstevel@tonic-gate
16800Sstevel@tonic-gate switch (rcm_state) {
16810Sstevel@tonic-gate case RCM_STATE_OFFLINE_QUERY:
16820Sstevel@tonic-gate case RCM_STATE_SUSPEND_QUERY:
16830Sstevel@tonic-gate query = RI_QUERY_OK;
16840Sstevel@tonic-gate break;
16850Sstevel@tonic-gate case RCM_STATE_OFFLINE_QUERY_FAIL:
16860Sstevel@tonic-gate case RCM_STATE_SUSPEND_QUERY_FAIL:
16870Sstevel@tonic-gate query = RI_QUERY_FAIL;
16880Sstevel@tonic-gate break;
16890Sstevel@tonic-gate default:
16900Sstevel@tonic-gate query = RI_QUERY_UNKNOWN;
16910Sstevel@tonic-gate break;
16920Sstevel@tonic-gate }
16930Sstevel@tonic-gate
16940Sstevel@tonic-gate return (query);
16950Sstevel@tonic-gate }
16960Sstevel@tonic-gate
16970Sstevel@tonic-gate static void
dev_list_append(ri_dev_t ** head,ri_dev_t * dev)16980Sstevel@tonic-gate dev_list_append(ri_dev_t **head, ri_dev_t *dev)
16990Sstevel@tonic-gate {
17000Sstevel@tonic-gate ri_dev_t *tmp;
17010Sstevel@tonic-gate
17020Sstevel@tonic-gate if ((tmp = *head) == NULL) {
17030Sstevel@tonic-gate *head = dev;
17040Sstevel@tonic-gate return;
17050Sstevel@tonic-gate }
17060Sstevel@tonic-gate while (tmp->next != NULL) {
17070Sstevel@tonic-gate tmp = tmp->next;
17080Sstevel@tonic-gate }
17090Sstevel@tonic-gate tmp->next = dev;
17100Sstevel@tonic-gate }
17110Sstevel@tonic-gate
17120Sstevel@tonic-gate /*
17130Sstevel@tonic-gate * The cpu list is ordered on cpuid since CMP cpuids will not necessarily
17140Sstevel@tonic-gate * be discovered in sequence.
17150Sstevel@tonic-gate */
17160Sstevel@tonic-gate static void
dev_list_cpu_insert(ri_dev_t ** listp,ri_dev_t * dev,processorid_t newid)17170Sstevel@tonic-gate dev_list_cpu_insert(ri_dev_t **listp, ri_dev_t *dev, processorid_t newid)
17180Sstevel@tonic-gate {
17190Sstevel@tonic-gate ri_dev_t *tmp;
17200Sstevel@tonic-gate int32_t cpuid;
17210Sstevel@tonic-gate
17220Sstevel@tonic-gate while ((tmp = *listp) != NULL &&
17230Sstevel@tonic-gate nvlist_lookup_int32(tmp->conf_props, RI_CPU_ID, &cpuid) == 0 &&
17240Sstevel@tonic-gate cpuid < newid) {
17250Sstevel@tonic-gate listp = &tmp->next;
17260Sstevel@tonic-gate }
17270Sstevel@tonic-gate
17280Sstevel@tonic-gate dev->next = tmp;
17290Sstevel@tonic-gate *listp = dev;
17300Sstevel@tonic-gate }
17310Sstevel@tonic-gate
17320Sstevel@tonic-gate /*
17330Sstevel@tonic-gate * Linear lookup. Should convert to hash tab.
17340Sstevel@tonic-gate */
17350Sstevel@tonic-gate static rcm_info_tuple_t *
tuple_lookup(rcmd_t * rcm,const char * krsrc,const char * kinfo)17360Sstevel@tonic-gate tuple_lookup(rcmd_t *rcm, const char *krsrc, const char *kinfo)
17370Sstevel@tonic-gate {
17380Sstevel@tonic-gate rcm_info_tuple_t *tuple = NULL;
17390Sstevel@tonic-gate const char *rsrc, *info;
17400Sstevel@tonic-gate
17410Sstevel@tonic-gate if ((rcm == NULL) || (krsrc == NULL) || (kinfo == NULL)) {
17420Sstevel@tonic-gate return (NULL);
17430Sstevel@tonic-gate }
17440Sstevel@tonic-gate
17450Sstevel@tonic-gate while ((tuple = rcm_info_next(rcm->offline_query_info,
17460Sstevel@tonic-gate tuple)) != NULL) {
17470Sstevel@tonic-gate if ((rsrc = rcm_info_rsrc(tuple)) == NULL ||
17480Sstevel@tonic-gate (info = rcm_info_info(tuple)) == NULL) {
17490Sstevel@tonic-gate continue;
17500Sstevel@tonic-gate }
17510Sstevel@tonic-gate
17520Sstevel@tonic-gate if (strcmp(rsrc, krsrc) == 0 && strcmp(info, kinfo) == 0) {
17530Sstevel@tonic-gate return (tuple);
17540Sstevel@tonic-gate }
17550Sstevel@tonic-gate }
17560Sstevel@tonic-gate return (NULL);
17570Sstevel@tonic-gate }
17580Sstevel@tonic-gate
17590Sstevel@tonic-gate /*
17600Sstevel@tonic-gate * Create and link attachment point handle.
17610Sstevel@tonic-gate */
17620Sstevel@tonic-gate static ri_ap_t *
ri_ap_alloc(char * ap_id,ri_hdl_t * hdl)17630Sstevel@tonic-gate ri_ap_alloc(char *ap_id, ri_hdl_t *hdl)
17640Sstevel@tonic-gate {
17650Sstevel@tonic-gate ri_ap_t *ap, *tmp;
17660Sstevel@tonic-gate
17670Sstevel@tonic-gate if ((ap = calloc(1, sizeof (*ap))) == NULL) {
17680Sstevel@tonic-gate dprintf((stderr, "calloc: %s\n", strerror(errno)));
17690Sstevel@tonic-gate return (NULL);
17700Sstevel@tonic-gate }
17710Sstevel@tonic-gate
17720Sstevel@tonic-gate if (nvlist_alloc(&ap->conf_props, NV_UNIQUE_NAME, 0) != 0 ||
17730Sstevel@tonic-gate nvlist_add_string(ap->conf_props, RI_AP_REQ_ID, ap_id) != 0) {
17740Sstevel@tonic-gate if (ap->conf_props != NULL)
17750Sstevel@tonic-gate nvlist_free(ap->conf_props);
17760Sstevel@tonic-gate free(ap);
17770Sstevel@tonic-gate return (NULL);
17780Sstevel@tonic-gate }
17790Sstevel@tonic-gate
17800Sstevel@tonic-gate if ((tmp = hdl->aps) == NULL) {
17810Sstevel@tonic-gate hdl->aps = ap;
17820Sstevel@tonic-gate } else {
17830Sstevel@tonic-gate while (tmp->next != NULL) {
17840Sstevel@tonic-gate tmp = tmp->next;
17850Sstevel@tonic-gate }
17860Sstevel@tonic-gate tmp->next = ap;
17870Sstevel@tonic-gate }
17880Sstevel@tonic-gate
17890Sstevel@tonic-gate return (ap);
17900Sstevel@tonic-gate }
17910Sstevel@tonic-gate
17920Sstevel@tonic-gate static ri_dev_t *
ri_dev_alloc(void)17930Sstevel@tonic-gate ri_dev_alloc(void)
17940Sstevel@tonic-gate {
17950Sstevel@tonic-gate ri_dev_t *dev;
17960Sstevel@tonic-gate
17970Sstevel@tonic-gate if ((dev = calloc(1, sizeof (*dev))) == NULL ||
17980Sstevel@tonic-gate nvlist_alloc(&dev->conf_props, NV_UNIQUE_NAME, 0) != 0) {
17990Sstevel@tonic-gate s_free(dev);
18000Sstevel@tonic-gate }
18010Sstevel@tonic-gate return (dev);
18020Sstevel@tonic-gate }
18030Sstevel@tonic-gate
18040Sstevel@tonic-gate static ri_dev_t *
io_dev_alloc(char * drv_inst)18050Sstevel@tonic-gate io_dev_alloc(char *drv_inst)
18060Sstevel@tonic-gate {
18070Sstevel@tonic-gate ri_dev_t *io;
18080Sstevel@tonic-gate
18090Sstevel@tonic-gate assert(drv_inst != NULL);
18100Sstevel@tonic-gate
18110Sstevel@tonic-gate if ((io = ri_dev_alloc()) == NULL)
18120Sstevel@tonic-gate return (NULL);
18130Sstevel@tonic-gate
18140Sstevel@tonic-gate if (nvlist_add_string(io->conf_props, RI_IO_DRV_INST,
18150Sstevel@tonic-gate drv_inst) != 0) {
18160Sstevel@tonic-gate dprintf((stderr, "nvlist_add_string fail\n"));
18170Sstevel@tonic-gate ri_dev_free(io);
18180Sstevel@tonic-gate return (NULL);
18190Sstevel@tonic-gate }
18200Sstevel@tonic-gate
18210Sstevel@tonic-gate return (io);
18220Sstevel@tonic-gate }
18230Sstevel@tonic-gate
18240Sstevel@tonic-gate static ri_client_t *
ri_client_alloc(char * rsrc,char * usage)18250Sstevel@tonic-gate ri_client_alloc(char *rsrc, char *usage)
18260Sstevel@tonic-gate {
18270Sstevel@tonic-gate ri_client_t *client;
18280Sstevel@tonic-gate
18290Sstevel@tonic-gate assert(rsrc != NULL && usage != NULL);
18300Sstevel@tonic-gate
18310Sstevel@tonic-gate if ((client = calloc(1, sizeof (*client))) == NULL) {
18320Sstevel@tonic-gate dprintf((stderr, "calloc: %s\n", strerror(errno)));
18330Sstevel@tonic-gate return (NULL);
18340Sstevel@tonic-gate }
18350Sstevel@tonic-gate
18360Sstevel@tonic-gate if (nvlist_alloc(&client->usg_props, NV_UNIQUE_NAME, 0) != 0) {
18370Sstevel@tonic-gate dprintf((stderr, "nvlist_alloc fail\n"));
18380Sstevel@tonic-gate free(client);
18390Sstevel@tonic-gate return (NULL);
18400Sstevel@tonic-gate }
18410Sstevel@tonic-gate
18420Sstevel@tonic-gate if (nvlist_add_string(client->usg_props, RI_CLIENT_RSRC, rsrc) != 0 ||
18430Sstevel@tonic-gate nvlist_add_string(client->usg_props, RI_CLIENT_USAGE, usage) != 0) {
18440Sstevel@tonic-gate dprintf((stderr, "nvlist_add_string fail\n"));
18450Sstevel@tonic-gate ri_client_free(client);
18460Sstevel@tonic-gate return (NULL);
18470Sstevel@tonic-gate }
18480Sstevel@tonic-gate
18490Sstevel@tonic-gate return (client);
18500Sstevel@tonic-gate }
18510Sstevel@tonic-gate
18520Sstevel@tonic-gate static void
apd_tbl_free(apd_t apd_tbl[],int napds)18530Sstevel@tonic-gate apd_tbl_free(apd_t apd_tbl[], int napds)
18540Sstevel@tonic-gate {
18550Sstevel@tonic-gate int i;
18560Sstevel@tonic-gate apd_t *apd;
18570Sstevel@tonic-gate
18580Sstevel@tonic-gate for (i = 0, apd = apd_tbl; i < napds; i++, apd++)
18590Sstevel@tonic-gate s_free(apd->cfga_list_data);
18600Sstevel@tonic-gate
18610Sstevel@tonic-gate free(apd_tbl);
18620Sstevel@tonic-gate }
18630Sstevel@tonic-gate
18640Sstevel@tonic-gate static char *
pstate2str(int pi_state)18650Sstevel@tonic-gate pstate2str(int pi_state)
18660Sstevel@tonic-gate {
18670Sstevel@tonic-gate char *state;
18680Sstevel@tonic-gate
18690Sstevel@tonic-gate switch (pi_state) {
18700Sstevel@tonic-gate case P_OFFLINE:
18710Sstevel@tonic-gate state = PS_OFFLINE;
18720Sstevel@tonic-gate break;
18730Sstevel@tonic-gate case P_ONLINE:
18740Sstevel@tonic-gate state = PS_ONLINE;
18750Sstevel@tonic-gate break;
18760Sstevel@tonic-gate case P_FAULTED:
18770Sstevel@tonic-gate state = PS_FAULTED;
18780Sstevel@tonic-gate break;
18790Sstevel@tonic-gate case P_POWEROFF:
18800Sstevel@tonic-gate state = PS_POWEROFF;
18810Sstevel@tonic-gate break;
18820Sstevel@tonic-gate case P_NOINTR:
18830Sstevel@tonic-gate state = PS_NOINTR;
18840Sstevel@tonic-gate break;
18850Sstevel@tonic-gate case P_SPARE:
18860Sstevel@tonic-gate state = PS_SPARE;
18870Sstevel@tonic-gate break;
18880Sstevel@tonic-gate default:
18890Sstevel@tonic-gate state = "unknown";
18900Sstevel@tonic-gate break;
18910Sstevel@tonic-gate }
18920Sstevel@tonic-gate
18930Sstevel@tonic-gate return (state);
18940Sstevel@tonic-gate }
18950Sstevel@tonic-gate
18960Sstevel@tonic-gate #ifdef DEBUG
18970Sstevel@tonic-gate static void
dump_apd_tbl(FILE * fp,apd_t * apds,int n_apds)18980Sstevel@tonic-gate dump_apd_tbl(FILE *fp, apd_t *apds, int n_apds)
18990Sstevel@tonic-gate {
19000Sstevel@tonic-gate int i, j;
19010Sstevel@tonic-gate cfga_list_data_t *cfga_ldata;
19020Sstevel@tonic-gate
19030Sstevel@tonic-gate for (i = 0; i < n_apds; i++, apds++) {
19040Sstevel@tonic-gate dprintf((stderr, "apd_tbl[%d].nlist=%d\n", i, apds->nlist));
19050Sstevel@tonic-gate for (j = 0, cfga_ldata = apds->cfga_list_data; j < apds->nlist;
19060Sstevel@tonic-gate j++, cfga_ldata++) {
19070Sstevel@tonic-gate dprintf((fp,
19080Sstevel@tonic-gate "apd_tbl[%d].cfga_list_data[%d].ap_log_id=%s\n",
19090Sstevel@tonic-gate i, j, cfga_ldata->ap_log_id));
19100Sstevel@tonic-gate }
19110Sstevel@tonic-gate }
19120Sstevel@tonic-gate }
19130Sstevel@tonic-gate #endif /* DEBUG */
19140Sstevel@tonic-gate
19150Sstevel@tonic-gate /*
19160Sstevel@tonic-gate * The lookup table is a simple array that is grown in chunks
19170Sstevel@tonic-gate * to optimize memory allocation.
19180Sstevel@tonic-gate * Indices are assigned to each array entry in-order so that
19190Sstevel@tonic-gate * the original device tree ordering can be discerned at a later time.
19200Sstevel@tonic-gate *
19210Sstevel@tonic-gate * add_lookup_entry is called from the libdevinfo tree traversal callbacks:
19220Sstevel@tonic-gate * 1) devinfo_node_walk - physical device path for each node in
19230Sstevel@tonic-gate * the devinfo tree via di_walk_node(), lookup entry name is
19240Sstevel@tonic-gate * /devices/[di_devfs_path]
19250Sstevel@tonic-gate * 2) devinfo_minor_walk - physical device path plus minor name for
19260Sstevel@tonic-gate * each minor associated with a node via di_walk_minor(), lookup entry
19270Sstevel@tonic-gate * name is /devices/[di_devfs_path:di_minor_name]
19280Sstevel@tonic-gate * 3) devinfo_devlink_walk - for each minor's /dev link from its /devices
19290Sstevel@tonic-gate * path via di_devlink_walk(), lookup entry name is di_devlink_path()
19300Sstevel@tonic-gate */
19310Sstevel@tonic-gate static int
add_lookup_entry(lookup_table_t * table,const char * name,di_node_t node)19320Sstevel@tonic-gate add_lookup_entry(lookup_table_t *table, const char *name, di_node_t node)
19330Sstevel@tonic-gate {
19340Sstevel@tonic-gate size_t size;
19350Sstevel@tonic-gate lookup_entry_t *new_table;
19360Sstevel@tonic-gate
19370Sstevel@tonic-gate
19380Sstevel@tonic-gate /* Grow the lookup table by USAGE_ALLOC_SIZE slots if necessary */
19390Sstevel@tonic-gate if (table->n_entries == table->n_slots) {
19400Sstevel@tonic-gate size = (table->n_slots + USAGE_ALLOC_SIZE) *
19410Sstevel@tonic-gate sizeof (lookup_entry_t);
19420Sstevel@tonic-gate new_table = (lookup_entry_t *)realloc(table->table, size);
19430Sstevel@tonic-gate if (new_table == NULL) {
19440Sstevel@tonic-gate dprintf((stderr, "add_lookup_entry: alloc failed: %s\n",
19450Sstevel@tonic-gate strerror(errno)));
19460Sstevel@tonic-gate errno = ENOMEM;
19470Sstevel@tonic-gate return (-1);
19480Sstevel@tonic-gate }
19490Sstevel@tonic-gate table->table = new_table;
19500Sstevel@tonic-gate table->n_slots += USAGE_ALLOC_SIZE;
19510Sstevel@tonic-gate }
19520Sstevel@tonic-gate
19530Sstevel@tonic-gate dprintf((stderr, "add_lookup_entry[%d]:%s\n", table->n_entries, name));
19540Sstevel@tonic-gate
19550Sstevel@tonic-gate /* Add this name to the next slot */
19560Sstevel@tonic-gate if ((table->table[table->n_entries].name = strdup(name)) == NULL) {
19570Sstevel@tonic-gate dprintf((stderr, "add_lookup_entry: strdup failed: %s\n",
19580Sstevel@tonic-gate strerror(errno)));
19590Sstevel@tonic-gate errno = ENOMEM;
19600Sstevel@tonic-gate return (-1);
19610Sstevel@tonic-gate }
19620Sstevel@tonic-gate table->table[table->n_entries].index = table->n_entries;
19630Sstevel@tonic-gate table->table[table->n_entries].node = node;
19640Sstevel@tonic-gate table->table[table->n_entries].n_usage = 0;
19650Sstevel@tonic-gate table->table[table->n_entries].usage = NULL;
19660Sstevel@tonic-gate table->n_entries += 1;
19670Sstevel@tonic-gate
19680Sstevel@tonic-gate return (0);
19690Sstevel@tonic-gate }
19700Sstevel@tonic-gate
19710Sstevel@tonic-gate /*
19720Sstevel@tonic-gate * lookup table entry names are full pathname strings, all start with /
19730Sstevel@tonic-gate */
19740Sstevel@tonic-gate static int
table_compare_names(const void * a,const void * b)19750Sstevel@tonic-gate table_compare_names(const void *a, const void *b)
19760Sstevel@tonic-gate {
19770Sstevel@tonic-gate lookup_entry_t *entry1 = (lookup_entry_t *)a;
19780Sstevel@tonic-gate lookup_entry_t *entry2 = (lookup_entry_t *)b;
19790Sstevel@tonic-gate
19800Sstevel@tonic-gate return (strcmp(entry1->name, entry2->name));
19810Sstevel@tonic-gate }
19820Sstevel@tonic-gate
19830Sstevel@tonic-gate
19840Sstevel@tonic-gate /*
19850Sstevel@tonic-gate * Compare two indices and return -1 for less, 1 for greater, 0 for equal
19860Sstevel@tonic-gate */
19870Sstevel@tonic-gate static int
table_compare_indices(const void * a,const void * b)19880Sstevel@tonic-gate table_compare_indices(const void *a, const void *b)
19890Sstevel@tonic-gate {
19900Sstevel@tonic-gate lookup_entry_t *entry1 = (lookup_entry_t *)a;
19910Sstevel@tonic-gate lookup_entry_t *entry2 = (lookup_entry_t *)b;
19920Sstevel@tonic-gate
19930Sstevel@tonic-gate if (entry1->index < entry2->index)
19940Sstevel@tonic-gate return (-1);
19950Sstevel@tonic-gate if (entry1->index > entry2->index)
19960Sstevel@tonic-gate return (1);
19970Sstevel@tonic-gate return (0);
19980Sstevel@tonic-gate }
19990Sstevel@tonic-gate
20000Sstevel@tonic-gate /*
20010Sstevel@tonic-gate * Given a RCM resource name, find the matching entry in the IO device table
20020Sstevel@tonic-gate */
20030Sstevel@tonic-gate static lookup_entry_t *
lookup(lookup_table_t * table,const char * rcm_rsrc)20040Sstevel@tonic-gate lookup(lookup_table_t *table, const char *rcm_rsrc)
20050Sstevel@tonic-gate {
20060Sstevel@tonic-gate lookup_entry_t *entry;
20070Sstevel@tonic-gate lookup_entry_t lookup_arg;
20080Sstevel@tonic-gate
20090Sstevel@tonic-gate dprintf((stderr, "lookup:%s\n", rcm_rsrc));
20100Sstevel@tonic-gate lookup_arg.name = (char *)rcm_rsrc;
20110Sstevel@tonic-gate entry = bsearch(&lookup_arg, table->table, table->n_entries,
20120Sstevel@tonic-gate sizeof (lookup_entry_t), table_compare_names);
20130Sstevel@tonic-gate
20140Sstevel@tonic-gate #ifdef DEBUG
20150Sstevel@tonic-gate if (entry != NULL) {
20160Sstevel@tonic-gate dprintf((stderr, " found entry:%d\n", entry->index));
20170Sstevel@tonic-gate }
20180Sstevel@tonic-gate #endif /* DEBUG */
20190Sstevel@tonic-gate return (entry);
20200Sstevel@tonic-gate }
20210Sstevel@tonic-gate
20220Sstevel@tonic-gate /*
20230Sstevel@tonic-gate * Add RCM usage to the given device table entry.
20240Sstevel@tonic-gate * Returns -1 on realloc failure.
20250Sstevel@tonic-gate */
20260Sstevel@tonic-gate static int
add_usage(lookup_entry_t * entry,const char * rcm_rsrc,rcm_info_tuple_t * tuple)20270Sstevel@tonic-gate add_usage(lookup_entry_t *entry, const char *rcm_rsrc, rcm_info_tuple_t *tuple)
20280Sstevel@tonic-gate {
20290Sstevel@tonic-gate size_t size;
20300Sstevel@tonic-gate const char *info;
20310Sstevel@tonic-gate usage_t *new_usage;
20320Sstevel@tonic-gate
20330Sstevel@tonic-gate if ((entry == NULL) ||
20340Sstevel@tonic-gate ((info = rcm_info_info(tuple)) == NULL))
20350Sstevel@tonic-gate return (0);
20360Sstevel@tonic-gate
20370Sstevel@tonic-gate if (rcm_ignore((char *)rcm_rsrc, (char *)info) == 0)
20380Sstevel@tonic-gate return (0);
20390Sstevel@tonic-gate
20400Sstevel@tonic-gate size = (entry->n_usage + 1) * sizeof (usage_t);
20410Sstevel@tonic-gate new_usage = (usage_t *)realloc(entry->usage, size);
20420Sstevel@tonic-gate if (new_usage == NULL) {
20430Sstevel@tonic-gate dprintf((stderr, "add_usage: alloc failed: %s\n",
20440Sstevel@tonic-gate strerror(errno)));
20450Sstevel@tonic-gate return (-1);
20460Sstevel@tonic-gate }
20470Sstevel@tonic-gate dprintf((stderr, "add_usage: entry %d rsrc: %s info: %s\n",
20480Sstevel@tonic-gate entry->index, rcm_rsrc, info));
20490Sstevel@tonic-gate
20500Sstevel@tonic-gate entry->usage = new_usage;
20510Sstevel@tonic-gate entry->usage[entry->n_usage].rsrc = rcm_rsrc;
20520Sstevel@tonic-gate entry->usage[entry->n_usage].info = info;
20530Sstevel@tonic-gate entry->n_usage += 1;
20540Sstevel@tonic-gate return (0);
20550Sstevel@tonic-gate }
20560Sstevel@tonic-gate
20570Sstevel@tonic-gate static void
empty_table(lookup_table_t * table)20580Sstevel@tonic-gate empty_table(lookup_table_t *table)
20590Sstevel@tonic-gate {
20600Sstevel@tonic-gate int i;
20610Sstevel@tonic-gate
20620Sstevel@tonic-gate if (table) {
20630Sstevel@tonic-gate for (i = 0; i < table->n_entries; i++) {
20640Sstevel@tonic-gate if (table->table[i].name)
20650Sstevel@tonic-gate free(table->table[i].name);
20660Sstevel@tonic-gate /*
20670Sstevel@tonic-gate * Note: the strings pointed to from within
20680Sstevel@tonic-gate * usage were freed already by rcm_free_info
20690Sstevel@tonic-gate */
20700Sstevel@tonic-gate if (table->table[i].usage)
20710Sstevel@tonic-gate free(table->table[i].usage);
20720Sstevel@tonic-gate }
20730Sstevel@tonic-gate if (table->table)
20740Sstevel@tonic-gate free(table->table);
20750Sstevel@tonic-gate table->table = NULL;
20760Sstevel@tonic-gate table->n_entries = 0;
20770Sstevel@tonic-gate table->n_slots = 0;
20780Sstevel@tonic-gate }
20790Sstevel@tonic-gate }
2080