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
51957Sdnielsen * Common Development and Distribution License (the "License").
61957Sdnielsen * 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 */
21133Sdmick
22133Sdmick /* Portions Copyright 2005 Cyril Plisko */
23133Sdmick
240Sstevel@tonic-gate /*
25*10923SEvan.Yan@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
260Sstevel@tonic-gate * Use is subject to license terms.
270Sstevel@tonic-gate */
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <errno.h>
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate #include <locale.h>
340Sstevel@tonic-gate #include <langinfo.h>
350Sstevel@tonic-gate #include <time.h>
360Sstevel@tonic-gate
370Sstevel@tonic-gate #if !defined(DEBUG)
380Sstevel@tonic-gate #define NDEBUG 1
390Sstevel@tonic-gate #else
400Sstevel@tonic-gate #undef NDEBUG
410Sstevel@tonic-gate #endif
420Sstevel@tonic-gate
430Sstevel@tonic-gate #include <assert.h>
440Sstevel@tonic-gate #include <sys/types.h>
450Sstevel@tonic-gate #include <sys/stat.h>
460Sstevel@tonic-gate #include <sys/param.h>
470Sstevel@tonic-gate #include <dlfcn.h>
480Sstevel@tonic-gate #include <synch.h>
490Sstevel@tonic-gate #include <sys/systeminfo.h>
500Sstevel@tonic-gate #include <sys/sunddi.h>
510Sstevel@tonic-gate #include <libdevinfo.h>
520Sstevel@tonic-gate #include <unistd.h>
530Sstevel@tonic-gate #include <stdarg.h>
540Sstevel@tonic-gate #include <limits.h>
550Sstevel@tonic-gate #include <ftw.h>
560Sstevel@tonic-gate #include <ctype.h>
570Sstevel@tonic-gate
580Sstevel@tonic-gate #define CFGA_PLUGIN_LIB
590Sstevel@tonic-gate #include <config_admin.h>
600Sstevel@tonic-gate
610Sstevel@tonic-gate /* Limit size of sysinfo return */
620Sstevel@tonic-gate #define SYSINFO_LENGTH 256
630Sstevel@tonic-gate
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate * Attachment point specifier types.
660Sstevel@tonic-gate */
670Sstevel@tonic-gate typedef enum {
680Sstevel@tonic-gate UNKNOWN_AP,
690Sstevel@tonic-gate LOGICAL_LINK_AP,
700Sstevel@tonic-gate LOGICAL_DRV_AP,
710Sstevel@tonic-gate PHYSICAL_AP,
720Sstevel@tonic-gate AP_TYPE
730Sstevel@tonic-gate } cfga_ap_types_t;
740Sstevel@tonic-gate
750Sstevel@tonic-gate static char *listopt_array[] = {
760Sstevel@tonic-gate
770Sstevel@tonic-gate #define LISTOPT_CLASS 0
780Sstevel@tonic-gate "class",
790Sstevel@tonic-gate NULL
800Sstevel@tonic-gate };
810Sstevel@tonic-gate
820Sstevel@tonic-gate typedef struct {
830Sstevel@tonic-gate int v_min; /* Min acceptable version */
840Sstevel@tonic-gate int v_max; /* Max acceptable version */
850Sstevel@tonic-gate } vers_req_t;
860Sstevel@tonic-gate
870Sstevel@tonic-gate #define INVALID_VERSION -1
880Sstevel@tonic-gate #define VALID_HSL_VERS(v) (((v) >= CFGA_HSL_V1) && \
890Sstevel@tonic-gate ((v) <= CFGA_HSL_VERS))
900Sstevel@tonic-gate
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate * Incomplete definition
930Sstevel@tonic-gate */
940Sstevel@tonic-gate struct cfga_vers_ops;
950Sstevel@tonic-gate
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate * Structure that contains plugin library information.
980Sstevel@tonic-gate */
990Sstevel@tonic-gate typedef struct plugin_lib {
1000Sstevel@tonic-gate struct plugin_lib *next; /* pointer to next */
1010Sstevel@tonic-gate mutex_t lock; /* protects refcnt */
1020Sstevel@tonic-gate int refcnt; /* reference count */
1030Sstevel@tonic-gate void *handle; /* handle from dlopen */
1040Sstevel@tonic-gate cfga_err_t (*cfga_change_state_p)();
1050Sstevel@tonic-gate cfga_err_t (*cfga_private_func_p)();
1060Sstevel@tonic-gate cfga_err_t (*cfga_test_p)();
1070Sstevel@tonic-gate cfga_err_t (*cfga_stat_p)();
1080Sstevel@tonic-gate cfga_err_t (*cfga_list_p)();
1090Sstevel@tonic-gate cfga_err_t (*cfga_help_p)();
1100Sstevel@tonic-gate int (*cfga_ap_id_cmp_p)();
1110Sstevel@tonic-gate cfga_err_t (*cfga_list_ext_p)(); /* For V2 plug-ins only */
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate int plugin_vers; /* actual plugin version */
1140Sstevel@tonic-gate struct cfga_vers_ops *vers_ops; /* version dependant routines */
1150Sstevel@tonic-gate char libpath[MAXPATHLEN]; /* full pathname to lib */
1160Sstevel@tonic-gate } plugin_lib_t;
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate static plugin_lib_t plugin_list;
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate typedef struct lib_cache {
1210Sstevel@tonic-gate struct lib_cache *lc_next;
1220Sstevel@tonic-gate plugin_lib_t *lc_libp;
1230Sstevel@tonic-gate char *lc_ap_id;
1240Sstevel@tonic-gate char *lc_ap_physical; /* physical ap_id */
1250Sstevel@tonic-gate char *lc_ap_logical; /* logical ap_id */
1260Sstevel@tonic-gate } lib_cache_t;
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate static lib_cache_t *lib_cache;
1290Sstevel@tonic-gate static mutex_t lib_cache_lock;
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate * Library locator data struct - used to pass down through the device
1330Sstevel@tonic-gate * tree walking code.
1340Sstevel@tonic-gate */
1350Sstevel@tonic-gate typedef struct lib_locator {
1360Sstevel@tonic-gate char ap_base[MAXPATHLEN];
1370Sstevel@tonic-gate char ap_logical[CFGA_LOG_EXT_LEN];
1380Sstevel@tonic-gate char ap_physical[CFGA_PHYS_EXT_LEN];
1390Sstevel@tonic-gate char ap_class[CFGA_CLASS_LEN];
1400Sstevel@tonic-gate char pathname[MAXPATHLEN];
1410Sstevel@tonic-gate plugin_lib_t *libp;
1420Sstevel@tonic-gate cfga_err_t status;
1430Sstevel@tonic-gate vers_req_t vers_req; /* plug-in version required */
1440Sstevel@tonic-gate } lib_loc_t;
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate /*
1470Sstevel@tonic-gate * linked list of cfga_stat_data structs - used for
1480Sstevel@tonic-gate * config_list
1490Sstevel@tonic-gate */
1500Sstevel@tonic-gate typedef struct stat_data_list {
1510Sstevel@tonic-gate struct stat_data_list *next;
1520Sstevel@tonic-gate cfga_stat_data_t stat_data;
1530Sstevel@tonic-gate } stat_data_list_t;
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate /*
1560Sstevel@tonic-gate * linked list of arrays. Each array represents a bunch
1570Sstevel@tonic-gate * of list_data_t structures returned by a single call
1580Sstevel@tonic-gate * to a plugin's cfga_list_ext() routine.
1590Sstevel@tonic-gate */
1600Sstevel@tonic-gate typedef struct array_list {
1610Sstevel@tonic-gate struct array_list *next;
1620Sstevel@tonic-gate cfga_list_data_t *array;
1630Sstevel@tonic-gate int nelem;
1640Sstevel@tonic-gate } array_list_t;
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate * encapsulate config_list args to get them through the tree
1680Sstevel@tonic-gate * walking code
1690Sstevel@tonic-gate */
1700Sstevel@tonic-gate typedef struct list_stat {
1710Sstevel@tonic-gate const char *opts; /* Hardware specific options */
1720Sstevel@tonic-gate char **errstr;
1730Sstevel@tonic-gate cfga_flags_t flags;
1740Sstevel@tonic-gate int *countp; /* Total number of list and stat structures */
1750Sstevel@tonic-gate stat_data_list_t *sdl; /* Linked list of stat structures */
1760Sstevel@tonic-gate array_list_t *al; /* Linked list of arrays of list structures */
1770Sstevel@tonic-gate vers_req_t use_vers; /* plugin versions to be stat'ed */
178*10923SEvan.Yan@Sun.COM char *shp_errstr; /* only for shp plugin */
1790Sstevel@tonic-gate } list_stat_t;
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate /*
1820Sstevel@tonic-gate * Internal operations for libcfgadm which are version dependant
1830Sstevel@tonic-gate */
1840Sstevel@tonic-gate struct cfga_vers_ops {
1850Sstevel@tonic-gate cfga_err_t (*resolve_lib)(plugin_lib_t *libp);
1860Sstevel@tonic-gate cfga_err_t (*stat_plugin)(list_stat_t *, lib_loc_t *, char **errstring);
1870Sstevel@tonic-gate cfga_err_t (*mklog)(di_node_t, di_minor_t, plugin_lib_t *,
1880Sstevel@tonic-gate lib_loc_t *liblocp);
1890Sstevel@tonic-gate cfga_err_t (*get_cond)(lib_loc_t *, cfga_cond_t *, char **);
1900Sstevel@tonic-gate };
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate /*
1940Sstevel@tonic-gate * Lock to protect list of libraries
1950Sstevel@tonic-gate */
1960Sstevel@tonic-gate static mutex_t plugin_list_lock;
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate /*
1990Sstevel@tonic-gate * Forward declarations
2000Sstevel@tonic-gate */
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate static const char *__config_strerror(cfga_err_t);
2030Sstevel@tonic-gate static void *config_calloc_check(size_t, size_t, char **);
2040Sstevel@tonic-gate static cfga_err_t resolve_lib_ref(plugin_lib_t *, lib_loc_t *);
2050Sstevel@tonic-gate static cfga_err_t config_get_lib(const char *, lib_loc_t *, char **);
2060Sstevel@tonic-gate static int check_ap(di_node_t, di_minor_t, void *);
207*10923SEvan.Yan@Sun.COM static int check_ap_hp(di_node_t, di_hp_t, void *);
208*10923SEvan.Yan@Sun.COM static int check_ap_impl(di_node_t, di_minor_t, di_hp_t, void *);
2090Sstevel@tonic-gate static int check_ap_phys(di_node_t, di_minor_t, void *);
210*10923SEvan.Yan@Sun.COM static int check_ap_phys_hp(di_node_t, di_hp_t, void *);
211*10923SEvan.Yan@Sun.COM static int check_ap_phys_impl(di_node_t, di_minor_t, di_hp_t, void *);
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate static cfga_err_t find_ap_common(lib_loc_t *libloc_p, const char *rootpath,
214*10923SEvan.Yan@Sun.COM int (*fcn)(di_node_t node, di_minor_t minor, void *arg),
215*10923SEvan.Yan@Sun.COM int (*fcn_hp)(di_node_t node, di_hp_t hp, void *arg),
216*10923SEvan.Yan@Sun.COM char **errstring);
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate static plugin_lib_t *lib_in_list(char *);
219*10923SEvan.Yan@Sun.COM static cfga_err_t find_lib(di_node_t, di_minor_t, lib_loc_t *);
220*10923SEvan.Yan@Sun.COM static cfga_err_t find_lib_hp(di_node_t, di_hp_t, lib_loc_t *);
221*10923SEvan.Yan@Sun.COM static cfga_err_t find_lib_impl(char *, lib_loc_t *);
2220Sstevel@tonic-gate static cfga_err_t load_lib(di_node_t, di_minor_t, lib_loc_t *);
223*10923SEvan.Yan@Sun.COM static cfga_err_t load_lib_hp(di_node_t, di_hp_t, lib_loc_t *);
224*10923SEvan.Yan@Sun.COM static cfga_err_t load_lib_impl(di_node_t, di_minor_t, di_hp_t, lib_loc_t *);
2250Sstevel@tonic-gate extern void bcopy(const void *, void *, size_t);
2260Sstevel@tonic-gate static void config_err(int, int, char **);
2270Sstevel@tonic-gate static void hold_lib(plugin_lib_t *);
2280Sstevel@tonic-gate static void rele_lib(plugin_lib_t *);
2290Sstevel@tonic-gate
2300Sstevel@tonic-gate static cfga_err_t parse_listopt(char *listopts, char **classpp,
2310Sstevel@tonic-gate char **errstring);
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate static cfga_err_t list_common(list_stat_t *lstatp, const char *class);
2340Sstevel@tonic-gate static int do_list_common(di_node_t node, di_minor_t minor, void *arg);
235*10923SEvan.Yan@Sun.COM static int do_list_common_hp(di_node_t node, di_hp_t hp, void *arg);
236*10923SEvan.Yan@Sun.COM static int do_list_common_impl(di_node_t node, di_minor_t minor,
237*10923SEvan.Yan@Sun.COM di_hp_t hp, void *arg);
2380Sstevel@tonic-gate static cfga_err_t stat_common(int num_ap_ids, char *const *ap_ids,
2390Sstevel@tonic-gate const char *class, list_stat_t *lstatp);
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate static cfga_err_t null_resolve(plugin_lib_t *libp);
2420Sstevel@tonic-gate static cfga_err_t resolve_v1(plugin_lib_t *libp);
2430Sstevel@tonic-gate static cfga_err_t resolve_v2(plugin_lib_t *libp);
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate static cfga_err_t mklog_common(di_node_t node, di_minor_t minor,
2460Sstevel@tonic-gate lib_loc_t *liblocp, size_t len);
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate static cfga_err_t null_mklog(di_node_t node, di_minor_t minor,
2490Sstevel@tonic-gate plugin_lib_t *libp, lib_loc_t *liblocp);
2500Sstevel@tonic-gate static cfga_err_t mklog_v1(di_node_t node, di_minor_t minor,
2510Sstevel@tonic-gate plugin_lib_t *libp, lib_loc_t *liblocp);
2520Sstevel@tonic-gate static cfga_err_t mklog_v2(di_node_t node, di_minor_t minor,
2530Sstevel@tonic-gate plugin_lib_t *libp, lib_loc_t *liblocp);
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate static cfga_err_t null_stat_plugin(list_stat_t *lstatp, lib_loc_t *libloc_p,
2560Sstevel@tonic-gate char **errstring);
2570Sstevel@tonic-gate static cfga_err_t stat_plugin_v2(list_stat_t *lstat, lib_loc_t *libloc_p,
2580Sstevel@tonic-gate char **errstring);
2590Sstevel@tonic-gate static cfga_err_t stat_plugin_v1(list_stat_t *lstat, lib_loc_t *libloc_p,
2600Sstevel@tonic-gate char **errstring);
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate static cfga_err_t null_get_cond(lib_loc_t *liblocp, cfga_cond_t *condp,
2630Sstevel@tonic-gate char **errstring);
2640Sstevel@tonic-gate static cfga_err_t get_cond_v1(lib_loc_t *liblocp, cfga_cond_t *condp,
2650Sstevel@tonic-gate char **errstring);
2660Sstevel@tonic-gate static cfga_err_t get_cond_v2(lib_loc_t *liblocp, cfga_cond_t *condp,
2670Sstevel@tonic-gate char **errstring);
2680Sstevel@tonic-gate
2690Sstevel@tonic-gate static cfga_err_t realloc_data(cfga_stat_data_t **ap_id_list,
2700Sstevel@tonic-gate int *nlistp, list_stat_t *lstatp);
2710Sstevel@tonic-gate static cfga_err_t realloc_data_ext(cfga_list_data_t **ap_id_list,
2720Sstevel@tonic-gate int *nlistp, list_stat_t *lstatp);
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate static void stat_to_list(cfga_list_data_t *lp, cfga_stat_data_t *statp);
2750Sstevel@tonic-gate static void lstat_free(list_stat_t *lstatp);
2760Sstevel@tonic-gate static cfga_ap_types_t find_arg_type(const char *ap_id);
2770Sstevel@tonic-gate static int compat_plugin(vers_req_t *reqp, int plugin_vers);
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate static cfga_err_t check_flags(cfga_flags_t flags, cfga_flags_t mask,
2800Sstevel@tonic-gate char **errstring);
2810Sstevel@tonic-gate static cfga_err_t check_apids(int num_ap_ids, char *const *ap_ids,
2820Sstevel@tonic-gate char **errstring);
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate static char *get_class(di_minor_t minor);
2850Sstevel@tonic-gate static cfga_err_t split_apid(char *ap_id, char **dyncompp, char **errstring);
2860Sstevel@tonic-gate static void append_dyn(char *buf, const char *dyncomp, size_t blen);
2870Sstevel@tonic-gate static int default_ap_id_cmp(const char *ap_id1, const char *ap_id2);
2880Sstevel@tonic-gate static void destroy_cache();
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate /*
2910Sstevel@tonic-gate * Plugin library search path helpers
2920Sstevel@tonic-gate */
2930Sstevel@tonic-gate #define LIB_PATH_BASE1 "/usr/platform/"
2940Sstevel@tonic-gate #define LIB_PATH_BASE2 "/usr"
295133Sdmick #if defined(__sparcv9)
2960Sstevel@tonic-gate #define LIB_PATH_MIDDLE "/lib/cfgadm/sparcv9/"
297133Sdmick #elif defined(__amd64)
298133Sdmick #define LIB_PATH_MIDDLE "/lib/cfgadm/amd64/"
2990Sstevel@tonic-gate #else
3000Sstevel@tonic-gate #define LIB_PATH_MIDDLE "/lib/cfgadm/"
3010Sstevel@tonic-gate #endif
3020Sstevel@tonic-gate #define LIB_PATH_TAIL ".so.1"
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate
3050Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
3060Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
3070Sstevel@tonic-gate #endif
3080Sstevel@tonic-gate
3090Sstevel@tonic-gate /*
3100Sstevel@tonic-gate * Defined constants
3110Sstevel@tonic-gate */
3120Sstevel@tonic-gate #define DEVICES_DIR "/devices"
3130Sstevel@tonic-gate #define DOT_DOT_DEVICES "../devices"
3140Sstevel@tonic-gate #define CFGA_DEV_DIR "/dev/cfg"
3150Sstevel@tonic-gate #define SLASH "/"
3160Sstevel@tonic-gate #define S_FREE(x) (((x) != NULL) ? (free(x), (x) = NULL) : (void *)0)
3170Sstevel@tonic-gate #define GET_DYN(a) (strstr((a), CFGA_DYN_SEP))
3180Sstevel@tonic-gate
3190Sstevel@tonic-gate #define CFGA_NO_CLASS "none"
3200Sstevel@tonic-gate
3210Sstevel@tonic-gate /*
3220Sstevel@tonic-gate * Error strings
3230Sstevel@tonic-gate */
3240Sstevel@tonic-gate #define DI_INIT_FAILED 1
3250Sstevel@tonic-gate #define ALLOC_FAILED 2
3260Sstevel@tonic-gate #define INVALID_ARGS 3
3270Sstevel@tonic-gate
3280Sstevel@tonic-gate static char *
3290Sstevel@tonic-gate err_strings[] = {
3300Sstevel@tonic-gate NULL,
3310Sstevel@tonic-gate "Device library initialize failed",
3320Sstevel@tonic-gate "Memory allocation failed",
3330Sstevel@tonic-gate "Invalid argument(s)"
3340Sstevel@tonic-gate };
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate static const char err_sep[] = ": ";
3370Sstevel@tonic-gate
3380Sstevel@tonic-gate
3390Sstevel@tonic-gate /*
3400Sstevel@tonic-gate * Table of version dependant routines
3410Sstevel@tonic-gate */
3420Sstevel@tonic-gate static struct cfga_vers_ops cfga_vers_ops[CFGA_HSL_VERS + 1] = {
3430Sstevel@tonic-gate
3440Sstevel@tonic-gate {null_resolve, null_stat_plugin, null_mklog, null_get_cond },
3450Sstevel@tonic-gate {resolve_v1, stat_plugin_v1, mklog_v1, get_cond_v1 },
3460Sstevel@tonic-gate {resolve_v2, stat_plugin_v2, mklog_v2, get_cond_v2 }
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate };
3490Sstevel@tonic-gate #define VERS_ARRAY_SZ (sizeof (cfga_vers_ops)/sizeof (cfga_vers_ops[0]))
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate /*
3530Sstevel@tonic-gate * Public interfaces for libcfgadm, as documented in config_admin.3x
3540Sstevel@tonic-gate */
3550Sstevel@tonic-gate
3560Sstevel@tonic-gate /*
3570Sstevel@tonic-gate * config_change_state
3580Sstevel@tonic-gate */
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate cfga_err_t
config_change_state(cfga_cmd_t state_change_cmd,int num_ap_ids,char * const * ap_id,const char * options,struct cfga_confirm * confp,struct cfga_msg * msgp,char ** errstring,cfga_flags_t flags)3610Sstevel@tonic-gate config_change_state(
3620Sstevel@tonic-gate cfga_cmd_t state_change_cmd,
3630Sstevel@tonic-gate int num_ap_ids,
3640Sstevel@tonic-gate char *const *ap_id,
3650Sstevel@tonic-gate const char *options,
3660Sstevel@tonic-gate struct cfga_confirm *confp,
3670Sstevel@tonic-gate struct cfga_msg *msgp,
3680Sstevel@tonic-gate char **errstring,
3690Sstevel@tonic-gate cfga_flags_t flags)
3700Sstevel@tonic-gate {
3710Sstevel@tonic-gate /*
3720Sstevel@tonic-gate * for each arg -
3730Sstevel@tonic-gate * load hs library,
3740Sstevel@tonic-gate * if force
3750Sstevel@tonic-gate * call cfga_state_change_func
3760Sstevel@tonic-gate * return status
3770Sstevel@tonic-gate * else
3780Sstevel@tonic-gate * call it's cfga_stat
3790Sstevel@tonic-gate * check condition
3800Sstevel@tonic-gate * call cfga_state_change_func
3810Sstevel@tonic-gate * return status
3820Sstevel@tonic-gate */
3830Sstevel@tonic-gate int i;
3840Sstevel@tonic-gate lib_loc_t libloc;
3850Sstevel@tonic-gate plugin_lib_t *libp;
3860Sstevel@tonic-gate cfga_cond_t cond;
3870Sstevel@tonic-gate
3880Sstevel@tonic-gate cfga_err_t retval = CFGA_OK;
3890Sstevel@tonic-gate
3900Sstevel@tonic-gate /* Sanity checks */
3910Sstevel@tonic-gate if (state_change_cmd == CFGA_CMD_NONE)
3920Sstevel@tonic-gate return (retval);
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate if ((state_change_cmd < CFGA_CMD_NONE) ||
3950Sstevel@tonic-gate (state_change_cmd > CFGA_CMD_UNCONFIGURE))
3960Sstevel@tonic-gate return (CFGA_INVAL);
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate if (errstring != NULL) {
3990Sstevel@tonic-gate *errstring = NULL;
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate
4020Sstevel@tonic-gate if (check_flags(flags, CFGA_FLAG_FORCE | CFGA_FLAG_VERBOSE, errstring)
4030Sstevel@tonic-gate != CFGA_OK) {
4040Sstevel@tonic-gate return (CFGA_ERROR);
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate
4070Sstevel@tonic-gate if (check_apids(num_ap_ids, ap_id, errstring) != CFGA_OK) {
4080Sstevel@tonic-gate return (CFGA_ERROR);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate
4110Sstevel@tonic-gate /*
4120Sstevel@tonic-gate * operate on each ap_id
4130Sstevel@tonic-gate */
4140Sstevel@tonic-gate for (i = 0; (i < num_ap_ids) && (retval == CFGA_OK); i++) {
4150Sstevel@tonic-gate libloc.libp = NULL;
4160Sstevel@tonic-gate if ((retval = config_get_lib(ap_id[i], &libloc, errstring)) !=
4170Sstevel@tonic-gate CFGA_OK) {
4180Sstevel@tonic-gate break;
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate
4210Sstevel@tonic-gate libp = libloc.libp;
4220Sstevel@tonic-gate if ((flags & CFGA_FLAG_FORCE) ||
4230Sstevel@tonic-gate (state_change_cmd == CFGA_CMD_UNLOAD) ||
4240Sstevel@tonic-gate (state_change_cmd == CFGA_CMD_DISCONNECT) ||
4250Sstevel@tonic-gate (state_change_cmd == CFGA_CMD_UNCONFIGURE)) {
4260Sstevel@tonic-gate errno = 0;
4270Sstevel@tonic-gate retval = (*libp->cfga_change_state_p)
4280Sstevel@tonic-gate (state_change_cmd, libloc.ap_physical, options,
4290Sstevel@tonic-gate confp, msgp, errstring, flags);
4300Sstevel@tonic-gate } else {
4310Sstevel@tonic-gate /*
4320Sstevel@tonic-gate * Need to check condition before proceeding in
4330Sstevel@tonic-gate * the "configure direction"
4340Sstevel@tonic-gate */
4350Sstevel@tonic-gate if ((retval = libp->vers_ops->get_cond(&libloc, &cond,
4360Sstevel@tonic-gate errstring)) != CFGA_OK) {
4370Sstevel@tonic-gate break;
4380Sstevel@tonic-gate }
4390Sstevel@tonic-gate
4400Sstevel@tonic-gate if (cond == CFGA_COND_OK || cond == CFGA_COND_UNKNOWN) {
4410Sstevel@tonic-gate errno = 0;
4420Sstevel@tonic-gate retval =
4430Sstevel@tonic-gate (*libp->cfga_change_state_p)(
4440Sstevel@tonic-gate state_change_cmd,
4450Sstevel@tonic-gate libloc.ap_physical, options,
4460Sstevel@tonic-gate confp, msgp, errstring,
4470Sstevel@tonic-gate flags);
4480Sstevel@tonic-gate } else {
4490Sstevel@tonic-gate retval = CFGA_INSUFFICENT_CONDITION;
4500Sstevel@tonic-gate }
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate rele_lib(libp);
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate
4550Sstevel@tonic-gate return (retval);
4560Sstevel@tonic-gate }
4570Sstevel@tonic-gate
4580Sstevel@tonic-gate /*
4590Sstevel@tonic-gate * config_private_func
4600Sstevel@tonic-gate */
4610Sstevel@tonic-gate
4620Sstevel@tonic-gate cfga_err_t
config_private_func(const char * function,int num_ap_ids,char * const * ap_ids,const char * options,struct cfga_confirm * confp,struct cfga_msg * msgp,char ** errstring,cfga_flags_t flags)4630Sstevel@tonic-gate config_private_func(
4640Sstevel@tonic-gate const char *function,
4650Sstevel@tonic-gate int num_ap_ids,
4660Sstevel@tonic-gate char *const *ap_ids,
4670Sstevel@tonic-gate const char *options,
4680Sstevel@tonic-gate struct cfga_confirm *confp,
4690Sstevel@tonic-gate struct cfga_msg *msgp,
4700Sstevel@tonic-gate char **errstring,
4710Sstevel@tonic-gate cfga_flags_t flags)
4720Sstevel@tonic-gate {
4730Sstevel@tonic-gate int i;
4740Sstevel@tonic-gate lib_loc_t libloc;
4750Sstevel@tonic-gate cfga_err_t retval = CFGA_OK;
4760Sstevel@tonic-gate
4770Sstevel@tonic-gate
4780Sstevel@tonic-gate if (errstring != NULL) {
4790Sstevel@tonic-gate *errstring = NULL;
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate
4820Sstevel@tonic-gate if (check_flags(flags, CFGA_FLAG_FORCE | CFGA_FLAG_VERBOSE, errstring)
4830Sstevel@tonic-gate != CFGA_OK) {
4840Sstevel@tonic-gate return (CFGA_ERROR);
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate
4870Sstevel@tonic-gate if (check_apids(num_ap_ids, ap_ids, errstring) != CFGA_OK) {
4880Sstevel@tonic-gate return (CFGA_ERROR);
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate
4910Sstevel@tonic-gate /*
4920Sstevel@tonic-gate * operate on each ap_id
4930Sstevel@tonic-gate */
4940Sstevel@tonic-gate for (i = 0; (i < num_ap_ids) && (retval == CFGA_OK); i++) {
4950Sstevel@tonic-gate libloc.libp = NULL;
4960Sstevel@tonic-gate if ((retval = config_get_lib(ap_ids[i], &libloc, errstring)) !=
4970Sstevel@tonic-gate CFGA_OK) {
4980Sstevel@tonic-gate return (retval);
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate
5010Sstevel@tonic-gate errno = 0;
5020Sstevel@tonic-gate retval = (*libloc.libp->cfga_private_func_p)(function,
5030Sstevel@tonic-gate libloc.ap_physical, options, confp, msgp, errstring,
5040Sstevel@tonic-gate flags);
5050Sstevel@tonic-gate rele_lib(libloc.libp);
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate
5080Sstevel@tonic-gate return (retval);
5090Sstevel@tonic-gate }
5100Sstevel@tonic-gate
5110Sstevel@tonic-gate
5120Sstevel@tonic-gate /*
5130Sstevel@tonic-gate * config_test
5140Sstevel@tonic-gate */
5150Sstevel@tonic-gate
5160Sstevel@tonic-gate cfga_err_t
config_test(int num_ap_ids,char * const * ap_ids,const char * options,struct cfga_msg * msgp,char ** errstring,cfga_flags_t flags)5170Sstevel@tonic-gate config_test(
5180Sstevel@tonic-gate int num_ap_ids,
5190Sstevel@tonic-gate char *const *ap_ids,
5200Sstevel@tonic-gate const char *options,
5210Sstevel@tonic-gate struct cfga_msg *msgp,
5220Sstevel@tonic-gate char **errstring,
5230Sstevel@tonic-gate cfga_flags_t flags)
5240Sstevel@tonic-gate {
5250Sstevel@tonic-gate int i;
5260Sstevel@tonic-gate lib_loc_t libloc;
5270Sstevel@tonic-gate cfga_err_t retval = CFGA_OK;
5280Sstevel@tonic-gate
5290Sstevel@tonic-gate if (errstring != NULL) {
5300Sstevel@tonic-gate *errstring = NULL;
5310Sstevel@tonic-gate }
5320Sstevel@tonic-gate
5330Sstevel@tonic-gate if (check_flags(flags, CFGA_FLAG_FORCE | CFGA_FLAG_VERBOSE, errstring)
5340Sstevel@tonic-gate != CFGA_OK) {
5350Sstevel@tonic-gate return (CFGA_ERROR);
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate
5380Sstevel@tonic-gate if (check_apids(num_ap_ids, ap_ids, errstring) != CFGA_OK) {
5390Sstevel@tonic-gate return (CFGA_ERROR);
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate
5420Sstevel@tonic-gate /*
5430Sstevel@tonic-gate * operate on each ap_id
5440Sstevel@tonic-gate */
5450Sstevel@tonic-gate for (i = 0; (i < num_ap_ids) && (retval == CFGA_OK); i++) {
5460Sstevel@tonic-gate libloc.libp = NULL;
5470Sstevel@tonic-gate if ((retval = config_get_lib(ap_ids[i], &libloc, errstring)) !=
5480Sstevel@tonic-gate CFGA_OK) {
5490Sstevel@tonic-gate return (retval);
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate
5520Sstevel@tonic-gate errno = 0;
5530Sstevel@tonic-gate retval = (*libloc.libp->cfga_test_p)(libloc.ap_physical,
5540Sstevel@tonic-gate options, msgp, errstring, flags);
5550Sstevel@tonic-gate rele_lib(libloc.libp);
5560Sstevel@tonic-gate }
5570Sstevel@tonic-gate
5580Sstevel@tonic-gate return (retval);
5590Sstevel@tonic-gate }
5600Sstevel@tonic-gate
5610Sstevel@tonic-gate cfga_err_t
config_stat(int num_ap_ids,char * const * ap_ids,struct cfga_stat_data * buf,const char * options,char ** errstring)5620Sstevel@tonic-gate config_stat(
5630Sstevel@tonic-gate int num_ap_ids,
5640Sstevel@tonic-gate char *const *ap_ids,
5650Sstevel@tonic-gate struct cfga_stat_data *buf,
5660Sstevel@tonic-gate const char *options,
5670Sstevel@tonic-gate char **errstring)
5680Sstevel@tonic-gate {
5690Sstevel@tonic-gate int nstat, n, i;
5700Sstevel@tonic-gate list_stat_t lstat = {NULL};
5710Sstevel@tonic-gate cfga_err_t rc = CFGA_OK;
5720Sstevel@tonic-gate
5730Sstevel@tonic-gate if (check_apids(num_ap_ids, ap_ids, errstring) != CFGA_OK) {
5740Sstevel@tonic-gate return (CFGA_ERROR);
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate
5770Sstevel@tonic-gate /*
5780Sstevel@tonic-gate * V1 entry points don't support dynamic attachment points
5790Sstevel@tonic-gate */
5800Sstevel@tonic-gate for (i = 0; i < num_ap_ids; i++) {
5810Sstevel@tonic-gate if (GET_DYN(ap_ids[i]) != NULL) {
5820Sstevel@tonic-gate return (CFGA_APID_NOEXIST);
5830Sstevel@tonic-gate }
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate
5860Sstevel@tonic-gate
5870Sstevel@tonic-gate nstat = n = 0;
5880Sstevel@tonic-gate lstat.countp = &nstat;
5890Sstevel@tonic-gate lstat.opts = options;
5900Sstevel@tonic-gate lstat.errstr = errstring;
591*10923SEvan.Yan@Sun.COM lstat.shp_errstr = NULL;
5920Sstevel@tonic-gate /*
5930Sstevel@tonic-gate * This is a V1 interface which can use only V1 plugins
5940Sstevel@tonic-gate */
5950Sstevel@tonic-gate lstat.use_vers.v_max = lstat.use_vers.v_min = CFGA_HSL_V1;
5960Sstevel@tonic-gate
5970Sstevel@tonic-gate rc = stat_common(num_ap_ids, ap_ids, NULL, &lstat);
5980Sstevel@tonic-gate if (rc == CFGA_OK) {
5990Sstevel@tonic-gate assert(*lstat.countp == num_ap_ids);
6000Sstevel@tonic-gate rc = realloc_data(&buf, &n, &lstat);
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate
6030Sstevel@tonic-gate return (rc);
6040Sstevel@tonic-gate }
6050Sstevel@tonic-gate
6060Sstevel@tonic-gate /*
6070Sstevel@tonic-gate * config_list
6080Sstevel@tonic-gate */
6090Sstevel@tonic-gate cfga_err_t
config_list(struct cfga_stat_data ** ap_id_list,int * nlistp,const char * options,char ** errstring)6100Sstevel@tonic-gate config_list(
6110Sstevel@tonic-gate struct cfga_stat_data **ap_id_list,
6120Sstevel@tonic-gate int *nlistp,
6130Sstevel@tonic-gate const char *options,
6140Sstevel@tonic-gate char **errstring)
6150Sstevel@tonic-gate {
6160Sstevel@tonic-gate int nstat;
6170Sstevel@tonic-gate list_stat_t lstat = {NULL};
6180Sstevel@tonic-gate cfga_err_t retval = CFGA_ERROR;
6190Sstevel@tonic-gate
6200Sstevel@tonic-gate if (errstring != NULL) {
6210Sstevel@tonic-gate *errstring = NULL;
6220Sstevel@tonic-gate }
6230Sstevel@tonic-gate
6240Sstevel@tonic-gate nstat = 0;
6250Sstevel@tonic-gate lstat.countp = &nstat;
6260Sstevel@tonic-gate lstat.opts = options;
6270Sstevel@tonic-gate lstat.errstr = errstring;
628*10923SEvan.Yan@Sun.COM lstat.shp_errstr = NULL;
6290Sstevel@tonic-gate /*
6300Sstevel@tonic-gate * This is a V1 interface which can use only V1 plugins
6310Sstevel@tonic-gate */
6320Sstevel@tonic-gate lstat.use_vers.v_max = lstat.use_vers.v_min = CFGA_HSL_V1;
6330Sstevel@tonic-gate
6340Sstevel@tonic-gate
6350Sstevel@tonic-gate *ap_id_list = NULL;
6360Sstevel@tonic-gate *nlistp = 0;
6370Sstevel@tonic-gate
6380Sstevel@tonic-gate /*
6390Sstevel@tonic-gate * V1 interfaces don't support prefiltering, no class
6400Sstevel@tonic-gate * specified.
6410Sstevel@tonic-gate */
6420Sstevel@tonic-gate retval = list_common(&lstat, NULL);
6430Sstevel@tonic-gate if (retval == CFGA_OK) {
6440Sstevel@tonic-gate retval = realloc_data(ap_id_list, nlistp, &lstat);
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate
6470Sstevel@tonic-gate assert((ap_id_list != NULL && *nlistp != 0) ||
6480Sstevel@tonic-gate (ap_id_list == NULL && *nlistp == 0));
6490Sstevel@tonic-gate
6500Sstevel@tonic-gate if (retval == CFGA_OK && *nlistp == 0) {
6510Sstevel@tonic-gate return (CFGA_NOTSUPP);
6520Sstevel@tonic-gate } else {
6530Sstevel@tonic-gate return (retval);
6540Sstevel@tonic-gate }
6550Sstevel@tonic-gate }
6560Sstevel@tonic-gate
6570Sstevel@tonic-gate
6580Sstevel@tonic-gate /*
6590Sstevel@tonic-gate * config_list_ext
6600Sstevel@tonic-gate */
6610Sstevel@tonic-gate cfga_err_t
config_list_ext(int num_ap_ids,char * const * ap_ids,struct cfga_list_data ** ap_id_list,int * nlistp,const char * options,const char * listopts,char ** errstring,cfga_flags_t flags)6620Sstevel@tonic-gate config_list_ext(
6630Sstevel@tonic-gate int num_ap_ids,
6640Sstevel@tonic-gate char *const *ap_ids,
6650Sstevel@tonic-gate struct cfga_list_data **ap_id_list,
6660Sstevel@tonic-gate int *nlistp,
6670Sstevel@tonic-gate const char *options,
6680Sstevel@tonic-gate const char *listopts,
6690Sstevel@tonic-gate char **errstring,
6700Sstevel@tonic-gate cfga_flags_t flags)
6710Sstevel@tonic-gate {
6720Sstevel@tonic-gate int nstat, list, prefilter;
6730Sstevel@tonic-gate list_stat_t lstat = {NULL};
6740Sstevel@tonic-gate char *class;
6750Sstevel@tonic-gate
6760Sstevel@tonic-gate cfga_err_t rc = CFGA_ERROR;
6770Sstevel@tonic-gate
6780Sstevel@tonic-gate *nlistp = 0;
6790Sstevel@tonic-gate *ap_id_list = NULL;
6800Sstevel@tonic-gate
6810Sstevel@tonic-gate if (errstring != NULL) {
6820Sstevel@tonic-gate *errstring = NULL;
6830Sstevel@tonic-gate }
6840Sstevel@tonic-gate
6850Sstevel@tonic-gate if (check_flags(flags, CFGA_FLAG_LIST_ALL, errstring) != CFGA_OK) {
6860Sstevel@tonic-gate return (CFGA_ERROR);
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate
6890Sstevel@tonic-gate class = NULL;
6900Sstevel@tonic-gate if ((rc = parse_listopt((char *)listopts, &class, errstring))
6910Sstevel@tonic-gate != CFGA_OK) {
6920Sstevel@tonic-gate return (rc);
6930Sstevel@tonic-gate }
6940Sstevel@tonic-gate
6950Sstevel@tonic-gate prefilter = (class == NULL) ? 0 : 1;
6960Sstevel@tonic-gate
6970Sstevel@tonic-gate nstat = 0;
6980Sstevel@tonic-gate lstat.countp = &nstat;
6990Sstevel@tonic-gate lstat.opts = options;
7000Sstevel@tonic-gate lstat.errstr = errstring;
701*10923SEvan.Yan@Sun.COM lstat.shp_errstr = NULL;
7020Sstevel@tonic-gate lstat.flags = flags;
7030Sstevel@tonic-gate /*
7040Sstevel@tonic-gate * We support both V1 and V2 plugins through this entry
7050Sstevel@tonic-gate * point.
7060Sstevel@tonic-gate */
7070Sstevel@tonic-gate lstat.use_vers.v_min = CFGA_HSL_V1;
7080Sstevel@tonic-gate lstat.use_vers.v_max = CFGA_HSL_V2;
7090Sstevel@tonic-gate
7100Sstevel@tonic-gate list = 0;
7110Sstevel@tonic-gate if (num_ap_ids == 0 && ap_ids == NULL) {
7120Sstevel@tonic-gate /*
7130Sstevel@tonic-gate * discover and stat all attachment points
7140Sstevel@tonic-gate */
7150Sstevel@tonic-gate list = 1;
7160Sstevel@tonic-gate rc = list_common(&lstat, class);
7170Sstevel@tonic-gate } else if (num_ap_ids > 0 && ap_ids != NULL) {
7180Sstevel@tonic-gate /*
7190Sstevel@tonic-gate * Stat specified attachment points. With dynamic expansion
7200Sstevel@tonic-gate * more data may be returned than was specified by user.
7210Sstevel@tonic-gate */
7220Sstevel@tonic-gate rc = stat_common(num_ap_ids, ap_ids, class, &lstat);
7230Sstevel@tonic-gate } else {
7240Sstevel@tonic-gate rc = CFGA_ERROR;
7250Sstevel@tonic-gate }
7260Sstevel@tonic-gate
7270Sstevel@tonic-gate S_FREE(class);
7280Sstevel@tonic-gate
7290Sstevel@tonic-gate if (rc != CFGA_OK) {
7300Sstevel@tonic-gate return (rc);
7310Sstevel@tonic-gate }
7320Sstevel@tonic-gate
7330Sstevel@tonic-gate rc = realloc_data_ext(ap_id_list, nlistp, &lstat);
7340Sstevel@tonic-gate
7350Sstevel@tonic-gate assert((ap_id_list != NULL && *nlistp != 0) ||
7360Sstevel@tonic-gate (ap_id_list == NULL && *nlistp == 0));
7370Sstevel@tonic-gate
7380Sstevel@tonic-gate /*
7390Sstevel@tonic-gate * For the list command notify user if no attachment
7400Sstevel@tonic-gate * point is found in the system.
7410Sstevel@tonic-gate *
7420Sstevel@tonic-gate */
7430Sstevel@tonic-gate if (list && rc == CFGA_OK && *nlistp == 0) {
7440Sstevel@tonic-gate /*
7450Sstevel@tonic-gate * If attachment points are being prefiltered, absence of data
7460Sstevel@tonic-gate * does not imply that config. admin. is not
7470Sstevel@tonic-gate * supported by the system.
7480Sstevel@tonic-gate */
7490Sstevel@tonic-gate if (prefilter) {
7500Sstevel@tonic-gate /*
7510Sstevel@tonic-gate * Prefiltering: requested class is absent
7520Sstevel@tonic-gate */
7530Sstevel@tonic-gate return (CFGA_APID_NOEXIST);
7540Sstevel@tonic-gate } else {
7550Sstevel@tonic-gate /*
7560Sstevel@tonic-gate * No attachment points in system
7570Sstevel@tonic-gate */
7580Sstevel@tonic-gate return (CFGA_NOTSUPP);
7590Sstevel@tonic-gate }
7600Sstevel@tonic-gate } else {
7610Sstevel@tonic-gate return (rc);
7620Sstevel@tonic-gate }
7630Sstevel@tonic-gate }
7640Sstevel@tonic-gate
7650Sstevel@tonic-gate
7660Sstevel@tonic-gate /*
7670Sstevel@tonic-gate * config_unload_libs
7680Sstevel@tonic-gate *
7690Sstevel@tonic-gate * Attempts to remove all libs on the plugin list.
7700Sstevel@tonic-gate */
7710Sstevel@tonic-gate void
config_unload_libs()7720Sstevel@tonic-gate config_unload_libs()
7730Sstevel@tonic-gate {
7740Sstevel@tonic-gate plugin_lib_t *libp, *prev = &plugin_list, *next = NULL;
7750Sstevel@tonic-gate
7760Sstevel@tonic-gate /* destroy cache entries to remove refcnt agains plugins */
7770Sstevel@tonic-gate destroy_cache();
7780Sstevel@tonic-gate
7790Sstevel@tonic-gate (void) mutex_lock(&plugin_list_lock);
7800Sstevel@tonic-gate for (libp = plugin_list.next; libp != NULL; libp = next) {
7810Sstevel@tonic-gate next = libp->next;
7820Sstevel@tonic-gate (void) mutex_lock(&libp->lock);
7830Sstevel@tonic-gate if (libp->refcnt) {
7840Sstevel@tonic-gate (void) mutex_unlock(&libp->lock);
7850Sstevel@tonic-gate prev = libp;
7860Sstevel@tonic-gate continue;
7870Sstevel@tonic-gate }
7880Sstevel@tonic-gate (void) mutex_unlock(&libp->lock);
7890Sstevel@tonic-gate prev->next = next;
7900Sstevel@tonic-gate (void) dlclose(libp->handle);
7910Sstevel@tonic-gate (void) mutex_destroy(&libp->lock);
7920Sstevel@tonic-gate free(libp);
7930Sstevel@tonic-gate }
7940Sstevel@tonic-gate (void) mutex_unlock(&plugin_list_lock);
7950Sstevel@tonic-gate }
7960Sstevel@tonic-gate
7970Sstevel@tonic-gate /*
7980Sstevel@tonic-gate * config_ap_id_cmp
7990Sstevel@tonic-gate */
8000Sstevel@tonic-gate int
config_ap_id_cmp(const cfga_ap_log_id_t ap1,const cfga_ap_log_id_t ap2)8010Sstevel@tonic-gate config_ap_id_cmp(
8020Sstevel@tonic-gate const cfga_ap_log_id_t ap1,
8030Sstevel@tonic-gate const cfga_ap_log_id_t ap2)
8040Sstevel@tonic-gate {
8050Sstevel@tonic-gate int ret;
8060Sstevel@tonic-gate lib_loc_t libloc;
8070Sstevel@tonic-gate char apstat1[CFGA_PHYS_EXT_LEN];
8080Sstevel@tonic-gate char apstat2[CFGA_PHYS_EXT_LEN];
8090Sstevel@tonic-gate char *sep1, *sep2;
8100Sstevel@tonic-gate
8110Sstevel@tonic-gate /*
8120Sstevel@tonic-gate * Extract static ap_ids
8130Sstevel@tonic-gate */
8140Sstevel@tonic-gate (void) strlcpy(apstat1, ap1, sizeof (apstat1));
8150Sstevel@tonic-gate (void) strlcpy(apstat2, ap2, sizeof (apstat2));
8160Sstevel@tonic-gate
8170Sstevel@tonic-gate sep1 = GET_DYN(apstat1);
8180Sstevel@tonic-gate sep2 = GET_DYN(apstat2);
8190Sstevel@tonic-gate
8200Sstevel@tonic-gate if (sep1)
8210Sstevel@tonic-gate *sep1 = '\0';
8220Sstevel@tonic-gate if (sep2)
8230Sstevel@tonic-gate *sep2 = '\0';
8240Sstevel@tonic-gate
8250Sstevel@tonic-gate /*
8260Sstevel@tonic-gate * Use the default comparator for static ap_ids
8270Sstevel@tonic-gate */
8280Sstevel@tonic-gate ret = default_ap_id_cmp(apstat1, apstat2);
8290Sstevel@tonic-gate if (ret)
8300Sstevel@tonic-gate return (ret);
8310Sstevel@tonic-gate
8320Sstevel@tonic-gate /*
8330Sstevel@tonic-gate * static components match. They belong to
8340Sstevel@tonic-gate * the same static ap_id. Check if both are dynamic
8350Sstevel@tonic-gate * If not, static < dynamic.
8360Sstevel@tonic-gate */
8370Sstevel@tonic-gate if ((sep1 == NULL) ^ (sep2 == NULL))
8380Sstevel@tonic-gate return (sep1 ? 1 : -1);
8390Sstevel@tonic-gate
8400Sstevel@tonic-gate /*
8410Sstevel@tonic-gate * If both are static, then ap1 = ap2
8420Sstevel@tonic-gate */
8430Sstevel@tonic-gate if (sep1 == NULL)
8440Sstevel@tonic-gate return (0);
8450Sstevel@tonic-gate
8460Sstevel@tonic-gate /*
8470Sstevel@tonic-gate * Both are dynamic and belong to same static ap_id.
8480Sstevel@tonic-gate * Use the plugin comparator
8490Sstevel@tonic-gate */
8500Sstevel@tonic-gate libloc.libp = NULL;
8510Sstevel@tonic-gate if (config_get_lib(ap1, &libloc, NULL) != CFGA_OK) {
8520Sstevel@tonic-gate return (strncmp(sep1, sep2, CFGA_PHYS_EXT_LEN));
8530Sstevel@tonic-gate }
8540Sstevel@tonic-gate
8550Sstevel@tonic-gate ret = (*libloc.libp->cfga_ap_id_cmp_p)(ap1, ap2);
8560Sstevel@tonic-gate
8570Sstevel@tonic-gate rele_lib(libloc.libp);
8580Sstevel@tonic-gate
8590Sstevel@tonic-gate return (ret);
8600Sstevel@tonic-gate }
8610Sstevel@tonic-gate
8620Sstevel@tonic-gate /*
8630Sstevel@tonic-gate * config_strerror
8640Sstevel@tonic-gate */
8650Sstevel@tonic-gate
8660Sstevel@tonic-gate const char *
config_strerror(cfga_err_t cfgerrnum)8670Sstevel@tonic-gate config_strerror(cfga_err_t cfgerrnum)
8680Sstevel@tonic-gate {
8690Sstevel@tonic-gate const char *ep = NULL;
8700Sstevel@tonic-gate
8710Sstevel@tonic-gate if ((cfgerrnum < CFGA_OK) || (cfgerrnum > CFGA_ATTR_INVAL))
8720Sstevel@tonic-gate return (NULL);
8730Sstevel@tonic-gate
8740Sstevel@tonic-gate ep = __config_strerror(cfgerrnum);
8750Sstevel@tonic-gate
8760Sstevel@tonic-gate return ((ep != NULL) ? dgettext(TEXT_DOMAIN, ep) : NULL);
8770Sstevel@tonic-gate }
8780Sstevel@tonic-gate
8790Sstevel@tonic-gate /*
8800Sstevel@tonic-gate * config_help
8810Sstevel@tonic-gate */
8820Sstevel@tonic-gate cfga_err_t
config_help(int num_ap_ids,char * const * ap_ids,struct cfga_msg * msgp,const char * options,cfga_flags_t flags)8830Sstevel@tonic-gate config_help(
8840Sstevel@tonic-gate int num_ap_ids,
8850Sstevel@tonic-gate char *const *ap_ids,
8860Sstevel@tonic-gate struct cfga_msg *msgp,
8870Sstevel@tonic-gate const char *options,
8880Sstevel@tonic-gate cfga_flags_t flags)
8890Sstevel@tonic-gate {
8900Sstevel@tonic-gate int i;
8910Sstevel@tonic-gate lib_loc_t libloc;
8920Sstevel@tonic-gate cfga_err_t retval = CFGA_OK;
8930Sstevel@tonic-gate
8940Sstevel@tonic-gate if (check_flags(flags, CFGA_FLAG_FORCE | CFGA_FLAG_VERBOSE, NULL)
8950Sstevel@tonic-gate != CFGA_OK) {
8960Sstevel@tonic-gate return (CFGA_ERROR);
8970Sstevel@tonic-gate }
8980Sstevel@tonic-gate
8990Sstevel@tonic-gate if (num_ap_ids < 0) {
9000Sstevel@tonic-gate return (CFGA_ERROR);
9010Sstevel@tonic-gate }
9020Sstevel@tonic-gate
9030Sstevel@tonic-gate if (num_ap_ids > 0 && ap_ids == NULL) {
9040Sstevel@tonic-gate return (CFGA_ERROR);
9050Sstevel@tonic-gate }
9060Sstevel@tonic-gate
9070Sstevel@tonic-gate /*
9080Sstevel@tonic-gate * operate on each ap_id
9090Sstevel@tonic-gate */
9100Sstevel@tonic-gate for (i = 0; (i < num_ap_ids) && (retval == CFGA_OK); i++) {
9110Sstevel@tonic-gate libloc.libp = NULL;
9120Sstevel@tonic-gate if ((retval = config_get_lib(ap_ids[i], &libloc,
9130Sstevel@tonic-gate NULL)) != CFGA_OK) {
9140Sstevel@tonic-gate return (retval);
9150Sstevel@tonic-gate }
9160Sstevel@tonic-gate
9170Sstevel@tonic-gate errno = 0;
9180Sstevel@tonic-gate retval = (*libloc.libp->cfga_help_p)(msgp, options, flags);
9190Sstevel@tonic-gate rele_lib(libloc.libp);
9200Sstevel@tonic-gate }
9210Sstevel@tonic-gate return (retval);
9220Sstevel@tonic-gate }
9230Sstevel@tonic-gate
9240Sstevel@tonic-gate /*
9250Sstevel@tonic-gate * Private support routines for the public interfaces
9260Sstevel@tonic-gate */
9270Sstevel@tonic-gate
9280Sstevel@tonic-gate static const char *
__config_strerror(cfga_err_t cfgerrnum)9290Sstevel@tonic-gate __config_strerror(cfga_err_t cfgerrnum)
9300Sstevel@tonic-gate {
9310Sstevel@tonic-gate const char *ep = NULL;
9320Sstevel@tonic-gate
9330Sstevel@tonic-gate switch (cfgerrnum) {
9340Sstevel@tonic-gate case CFGA_OK:
9350Sstevel@tonic-gate ep = "Configuration operation succeeded";
9360Sstevel@tonic-gate break;
9370Sstevel@tonic-gate case CFGA_NACK:
9380Sstevel@tonic-gate ep = "Configuration operation cancelled";
9390Sstevel@tonic-gate break;
9400Sstevel@tonic-gate case CFGA_INVAL:
9410Sstevel@tonic-gate ep = "Configuration operation invalid";
9420Sstevel@tonic-gate break;
9430Sstevel@tonic-gate case CFGA_NOTSUPP:
9440Sstevel@tonic-gate ep = "Configuration administration not supported";
9450Sstevel@tonic-gate break;
9460Sstevel@tonic-gate case CFGA_OPNOTSUPP:
9470Sstevel@tonic-gate ep = "Configuration operation not supported";
9480Sstevel@tonic-gate break;
9490Sstevel@tonic-gate case CFGA_PRIV:
9500Sstevel@tonic-gate ep = "Insufficient privileges";
9510Sstevel@tonic-gate break;
9520Sstevel@tonic-gate case CFGA_BUSY:
9530Sstevel@tonic-gate ep = "Component system is busy, try again";
9540Sstevel@tonic-gate break;
9550Sstevel@tonic-gate case CFGA_SYSTEM_BUSY:
9560Sstevel@tonic-gate ep = "System is busy, try again";
9570Sstevel@tonic-gate break;
9580Sstevel@tonic-gate case CFGA_DATA_ERROR:
9590Sstevel@tonic-gate ep = "Data error";
9600Sstevel@tonic-gate break;
9610Sstevel@tonic-gate case CFGA_LIB_ERROR:
9620Sstevel@tonic-gate ep = "Library error";
9630Sstevel@tonic-gate break;
9640Sstevel@tonic-gate case CFGA_NO_LIB:
9650Sstevel@tonic-gate ep = "No Library found";
9660Sstevel@tonic-gate break;
9670Sstevel@tonic-gate case CFGA_INSUFFICENT_CONDITION:
9680Sstevel@tonic-gate ep = "Insufficient condition";
9690Sstevel@tonic-gate break;
9700Sstevel@tonic-gate case CFGA_ERROR:
9710Sstevel@tonic-gate ep = "Hardware specific failure";
9720Sstevel@tonic-gate break;
9730Sstevel@tonic-gate case CFGA_APID_NOEXIST:
9740Sstevel@tonic-gate ep = "Attachment point not found";
9750Sstevel@tonic-gate break;
9760Sstevel@tonic-gate case CFGA_ATTR_INVAL:
9770Sstevel@tonic-gate ep = "No attachment point with specified attributes found";
9780Sstevel@tonic-gate break;
9790Sstevel@tonic-gate default:
9800Sstevel@tonic-gate ep = NULL;
9810Sstevel@tonic-gate break;
9820Sstevel@tonic-gate }
9830Sstevel@tonic-gate return (ep);
9840Sstevel@tonic-gate }
9850Sstevel@tonic-gate
9860Sstevel@tonic-gate /*
9870Sstevel@tonic-gate * listopts is a string in the getsubopt(3C) style:
9880Sstevel@tonic-gate * name1=value1,name2=value2,
9890Sstevel@tonic-gate */
9900Sstevel@tonic-gate static cfga_err_t
parse_listopt(char * listopts,char ** classpp,char ** errstring)9910Sstevel@tonic-gate parse_listopt(char *listopts, char **classpp, char **errstring)
9920Sstevel@tonic-gate {
9930Sstevel@tonic-gate char *bufp, *optp, *val = NULL;
9940Sstevel@tonic-gate cfga_err_t rc = CFGA_ERROR;
9950Sstevel@tonic-gate
9960Sstevel@tonic-gate *classpp = NULL;
9970Sstevel@tonic-gate
9980Sstevel@tonic-gate /*
9990Sstevel@tonic-gate * NULL is a legal value for listopts
10000Sstevel@tonic-gate */
10010Sstevel@tonic-gate if (listopts == NULL) {
10020Sstevel@tonic-gate return (CFGA_OK);
10030Sstevel@tonic-gate }
10040Sstevel@tonic-gate
10050Sstevel@tonic-gate if ((bufp = config_calloc_check(1, strlen(listopts) + 1, errstring))
10060Sstevel@tonic-gate == NULL) {
10070Sstevel@tonic-gate return (CFGA_LIB_ERROR);
10080Sstevel@tonic-gate }
10090Sstevel@tonic-gate (void) strcpy(bufp, listopts);
10100Sstevel@tonic-gate
10110Sstevel@tonic-gate optp = bufp; /* getsubopt() modifies its argument */
10120Sstevel@tonic-gate while (*optp != '\0') {
10130Sstevel@tonic-gate switch (getsubopt(&optp, listopt_array, &val)) {
10140Sstevel@tonic-gate case LISTOPT_CLASS:
10150Sstevel@tonic-gate if (val == NULL || *classpp != NULL) {
10160Sstevel@tonic-gate rc = CFGA_ERROR;
10170Sstevel@tonic-gate goto out;
10180Sstevel@tonic-gate }
10190Sstevel@tonic-gate if ((*classpp = config_calloc_check(1, strlen(val) + 1,
10200Sstevel@tonic-gate errstring)) == NULL) {
10210Sstevel@tonic-gate rc = CFGA_LIB_ERROR;
10220Sstevel@tonic-gate goto out;
10230Sstevel@tonic-gate }
10240Sstevel@tonic-gate (void) strcpy(*classpp, val);
10250Sstevel@tonic-gate break;
10260Sstevel@tonic-gate default:
10270Sstevel@tonic-gate rc = CFGA_ERROR;
10280Sstevel@tonic-gate goto out;
10290Sstevel@tonic-gate }
10300Sstevel@tonic-gate }
10310Sstevel@tonic-gate
10320Sstevel@tonic-gate rc = CFGA_OK;
10330Sstevel@tonic-gate /*FALLTHRU*/
10340Sstevel@tonic-gate out:
10350Sstevel@tonic-gate S_FREE(bufp);
10360Sstevel@tonic-gate if (rc != CFGA_OK) {
10370Sstevel@tonic-gate S_FREE(*classpp);
10380Sstevel@tonic-gate }
10390Sstevel@tonic-gate return (rc);
10400Sstevel@tonic-gate }
10410Sstevel@tonic-gate
10420Sstevel@tonic-gate /*ARGSUSED*/
10430Sstevel@tonic-gate static cfga_err_t
null_mklog(di_node_t node,di_minor_t minor,plugin_lib_t * libp,lib_loc_t * liblocp)10440Sstevel@tonic-gate null_mklog(
10450Sstevel@tonic-gate di_node_t node,
10460Sstevel@tonic-gate di_minor_t minor,
10470Sstevel@tonic-gate plugin_lib_t *libp,
10480Sstevel@tonic-gate lib_loc_t *liblocp)
10490Sstevel@tonic-gate {
10500Sstevel@tonic-gate return (CFGA_OK);
10510Sstevel@tonic-gate }
10520Sstevel@tonic-gate
10530Sstevel@tonic-gate static cfga_err_t
mklog_v1(di_node_t node,di_minor_t minor,plugin_lib_t * libp,lib_loc_t * liblocp)10540Sstevel@tonic-gate mklog_v1(
10550Sstevel@tonic-gate di_node_t node,
10560Sstevel@tonic-gate di_minor_t minor,
10570Sstevel@tonic-gate plugin_lib_t *libp,
10580Sstevel@tonic-gate lib_loc_t *liblocp)
10590Sstevel@tonic-gate {
10600Sstevel@tonic-gate const size_t len = CFGA_AP_LOG_ID_LEN;
10610Sstevel@tonic-gate
10620Sstevel@tonic-gate assert(len <= sizeof (liblocp->ap_logical));
10630Sstevel@tonic-gate
10640Sstevel@tonic-gate if (libp->plugin_vers != CFGA_HSL_V1) {
10650Sstevel@tonic-gate return (CFGA_LIB_ERROR);
10660Sstevel@tonic-gate }
10670Sstevel@tonic-gate
10680Sstevel@tonic-gate return (mklog_common(node, minor, liblocp, len));
10690Sstevel@tonic-gate }
10700Sstevel@tonic-gate
10710Sstevel@tonic-gate
10720Sstevel@tonic-gate /*
10730Sstevel@tonic-gate * Obtain the devlink from a /devices path
10740Sstevel@tonic-gate */
10750Sstevel@tonic-gate static int
get_link(di_devlink_t devlink,void * arg)10760Sstevel@tonic-gate get_link(di_devlink_t devlink, void *arg)
10770Sstevel@tonic-gate {
10780Sstevel@tonic-gate char *linkp = (char *)arg;
10790Sstevel@tonic-gate
10800Sstevel@tonic-gate (void) snprintf(linkp, CFGA_LOG_EXT_LEN, "%s",
10810Sstevel@tonic-gate di_devlink_path(devlink));
10820Sstevel@tonic-gate return (DI_WALK_TERMINATE);
10830Sstevel@tonic-gate }
10840Sstevel@tonic-gate
10850Sstevel@tonic-gate static cfga_err_t
mklog_v2(di_node_t node,di_minor_t minor,plugin_lib_t * libp,lib_loc_t * liblocp)10860Sstevel@tonic-gate mklog_v2(
10870Sstevel@tonic-gate di_node_t node,
10880Sstevel@tonic-gate di_minor_t minor,
10890Sstevel@tonic-gate plugin_lib_t *libp,
10900Sstevel@tonic-gate lib_loc_t *liblocp)
10910Sstevel@tonic-gate {
10920Sstevel@tonic-gate const size_t len = CFGA_LOG_EXT_LEN;
10930Sstevel@tonic-gate di_devlink_handle_t hdl;
10940Sstevel@tonic-gate
10950Sstevel@tonic-gate assert(len <= sizeof (liblocp->ap_logical));
10960Sstevel@tonic-gate
10970Sstevel@tonic-gate if (libp->plugin_vers != CFGA_HSL_V2) {
10980Sstevel@tonic-gate return (CFGA_LIB_ERROR);
10990Sstevel@tonic-gate }
11000Sstevel@tonic-gate
11010Sstevel@tonic-gate /* open devlink database */
11020Sstevel@tonic-gate if ((hdl = di_devlink_init(NULL, 0)) == NULL) {
11030Sstevel@tonic-gate return (CFGA_LIB_ERROR);
11040Sstevel@tonic-gate }
11050Sstevel@tonic-gate
11060Sstevel@tonic-gate liblocp->ap_logical[0] = '\0';
11070Sstevel@tonic-gate (void) di_devlink_walk(hdl, NULL,
11080Sstevel@tonic-gate liblocp->ap_physical + strlen(DEVICES_DIR),
11090Sstevel@tonic-gate DI_PRIMARY_LINK, (void *)liblocp->ap_logical, get_link);
11100Sstevel@tonic-gate
11110Sstevel@tonic-gate (void) di_devlink_fini(&hdl);
11120Sstevel@tonic-gate
11130Sstevel@tonic-gate if (liblocp->ap_logical[0] != '\0')
11140Sstevel@tonic-gate return (CFGA_OK);
11150Sstevel@tonic-gate return (mklog_common(node, minor, liblocp, len));
11160Sstevel@tonic-gate }
11170Sstevel@tonic-gate
11180Sstevel@tonic-gate /*
11190Sstevel@tonic-gate * mklog_common - make a logical name from the driver and instance
11200Sstevel@tonic-gate */
11210Sstevel@tonic-gate static cfga_err_t
mklog_common(di_node_t node,di_minor_t minor,lib_loc_t * libloc_p,size_t len)11220Sstevel@tonic-gate mklog_common(
11230Sstevel@tonic-gate di_node_t node,
11240Sstevel@tonic-gate di_minor_t minor,
11250Sstevel@tonic-gate lib_loc_t *libloc_p,
11260Sstevel@tonic-gate size_t len)
11270Sstevel@tonic-gate {
11280Sstevel@tonic-gate int inst;
11290Sstevel@tonic-gate char *drv, *minor_name;
11300Sstevel@tonic-gate
11310Sstevel@tonic-gate drv = di_driver_name(node);
11320Sstevel@tonic-gate inst = di_instance(node);
11330Sstevel@tonic-gate minor_name = di_minor_name(minor);
11340Sstevel@tonic-gate
11350Sstevel@tonic-gate errno = 0;
11360Sstevel@tonic-gate if (drv != NULL && inst != -1 && minor_name != NULL &&
11370Sstevel@tonic-gate snprintf(libloc_p->ap_logical, len, "%s%d:%s", drv, inst,
11380Sstevel@tonic-gate minor_name) < len) { /* snprintf returns strlen */
11390Sstevel@tonic-gate return (CFGA_OK);
11400Sstevel@tonic-gate }
11410Sstevel@tonic-gate
11420Sstevel@tonic-gate return (CFGA_LIB_ERROR);
11430Sstevel@tonic-gate }
11440Sstevel@tonic-gate
11450Sstevel@tonic-gate /*
1146*10923SEvan.Yan@Sun.COM * mklog_common - make a logical name from the driver and instance
1147*10923SEvan.Yan@Sun.COM */
1148*10923SEvan.Yan@Sun.COM /*ARGSUSED*/
1149*10923SEvan.Yan@Sun.COM static cfga_err_t
mklog_hp(di_node_t node,di_hp_t hp,plugin_lib_t * libp,lib_loc_t * liblocp)1150*10923SEvan.Yan@Sun.COM mklog_hp(
1151*10923SEvan.Yan@Sun.COM di_node_t node,
1152*10923SEvan.Yan@Sun.COM di_hp_t hp,
1153*10923SEvan.Yan@Sun.COM plugin_lib_t *libp,
1154*10923SEvan.Yan@Sun.COM lib_loc_t *liblocp)
1155*10923SEvan.Yan@Sun.COM {
1156*10923SEvan.Yan@Sun.COM const size_t len = CFGA_LOG_EXT_LEN;
1157*10923SEvan.Yan@Sun.COM int inst;
1158*10923SEvan.Yan@Sun.COM char *drv, *hp_name;
1159*10923SEvan.Yan@Sun.COM
1160*10923SEvan.Yan@Sun.COM drv = di_driver_name(node);
1161*10923SEvan.Yan@Sun.COM inst = di_instance(node);
1162*10923SEvan.Yan@Sun.COM hp_name = di_hp_name(hp);
1163*10923SEvan.Yan@Sun.COM
1164*10923SEvan.Yan@Sun.COM errno = 0;
1165*10923SEvan.Yan@Sun.COM if (drv != NULL && inst != -1 && hp_name != NULL &&
1166*10923SEvan.Yan@Sun.COM snprintf(liblocp->ap_logical, len, "%s%d:%s", drv, inst,
1167*10923SEvan.Yan@Sun.COM hp_name) < len) { /* snprintf returns strlen */
1168*10923SEvan.Yan@Sun.COM return (CFGA_OK);
1169*10923SEvan.Yan@Sun.COM }
1170*10923SEvan.Yan@Sun.COM
1171*10923SEvan.Yan@Sun.COM return (CFGA_LIB_ERROR);
1172*10923SEvan.Yan@Sun.COM }
1173*10923SEvan.Yan@Sun.COM
1174*10923SEvan.Yan@Sun.COM /*
11750Sstevel@tonic-gate * resolve_lib_ref - relocate to use plugin lib
11760Sstevel@tonic-gate */
11770Sstevel@tonic-gate static cfga_err_t
resolve_lib_ref(plugin_lib_t * libp,lib_loc_t * libloc_p)11780Sstevel@tonic-gate resolve_lib_ref(
11790Sstevel@tonic-gate plugin_lib_t *libp,
11800Sstevel@tonic-gate lib_loc_t *libloc_p)
11810Sstevel@tonic-gate {
11820Sstevel@tonic-gate void *sym;
11830Sstevel@tonic-gate void *libhdlp = libp->handle;
11840Sstevel@tonic-gate int plug_vers;
11850Sstevel@tonic-gate
11860Sstevel@tonic-gate if ((sym = dlsym(libhdlp, "cfga_version")) == NULL) {
11870Sstevel@tonic-gate /*
11880Sstevel@tonic-gate * Version symbol not defined, must be the first version
11890Sstevel@tonic-gate */
11900Sstevel@tonic-gate plug_vers = CFGA_HSL_V1;
11910Sstevel@tonic-gate } else {
11920Sstevel@tonic-gate plug_vers = *((int *)sym);
11930Sstevel@tonic-gate }
11940Sstevel@tonic-gate
11950Sstevel@tonic-gate /*
11960Sstevel@tonic-gate * Check if plugin version matches request.
11970Sstevel@tonic-gate */
11980Sstevel@tonic-gate if (!compat_plugin(&libloc_p->vers_req, plug_vers)) {
11990Sstevel@tonic-gate return (CFGA_NO_LIB);
12000Sstevel@tonic-gate }
12010Sstevel@tonic-gate
12020Sstevel@tonic-gate /*
12030Sstevel@tonic-gate * Record the plugin version and setup version dependant routines
12040Sstevel@tonic-gate */
12050Sstevel@tonic-gate assert(plug_vers < VERS_ARRAY_SZ);
12060Sstevel@tonic-gate libp->plugin_vers = plug_vers;
12070Sstevel@tonic-gate libp->vers_ops = &cfga_vers_ops[plug_vers];
12080Sstevel@tonic-gate
12090Sstevel@tonic-gate /* resolve symbols common to all versions */
12100Sstevel@tonic-gate if ((sym = dlsym(libhdlp, "cfga_change_state")) == NULL) {
12110Sstevel@tonic-gate perror("dlsym: cfga_change_state");
12120Sstevel@tonic-gate return (CFGA_LIB_ERROR);
12130Sstevel@tonic-gate } else
12140Sstevel@tonic-gate libp->cfga_change_state_p = (cfga_err_t (*)(cfga_cmd_t,
12150Sstevel@tonic-gate const char *, const char *, struct cfga_confirm *,
12160Sstevel@tonic-gate struct cfga_msg *, char **, cfga_flags_t)) sym;
12170Sstevel@tonic-gate
12180Sstevel@tonic-gate if ((sym = dlsym(libhdlp, "cfga_private_func")) == NULL) {
12190Sstevel@tonic-gate perror("dlsym: cfga_private_func");
12200Sstevel@tonic-gate return (CFGA_LIB_ERROR);
12210Sstevel@tonic-gate } else
12220Sstevel@tonic-gate libp->cfga_private_func_p = (cfga_err_t (*)(const char *,
12230Sstevel@tonic-gate const char *, const char *, struct cfga_confirm *,
12240Sstevel@tonic-gate struct cfga_msg *, char **, cfga_flags_t))sym;
12250Sstevel@tonic-gate
12260Sstevel@tonic-gate if ((sym = dlsym(libhdlp, "cfga_test")) == NULL) {
12270Sstevel@tonic-gate perror("dlsym: cfga_test");
12280Sstevel@tonic-gate return (CFGA_LIB_ERROR);
12290Sstevel@tonic-gate } else
12300Sstevel@tonic-gate libp->cfga_test_p = (cfga_err_t (*)(const char *, const char *,
12310Sstevel@tonic-gate struct cfga_msg *, char **, cfga_flags_t))sym;
12320Sstevel@tonic-gate
12330Sstevel@tonic-gate if ((sym = dlsym(libhdlp, "cfga_help")) == NULL) {
12340Sstevel@tonic-gate perror("dlsym: cfga_help");
12350Sstevel@tonic-gate return (CFGA_LIB_ERROR);
12360Sstevel@tonic-gate } else
12370Sstevel@tonic-gate libp->cfga_help_p = (cfga_err_t (*)(struct cfga_msg *,
12380Sstevel@tonic-gate const char *, cfga_flags_t))sym;
12390Sstevel@tonic-gate
12400Sstevel@tonic-gate if ((sym = dlsym(libhdlp, "cfga_ap_id_cmp")) == NULL) {
12410Sstevel@tonic-gate libp->cfga_ap_id_cmp_p = default_ap_id_cmp;
12420Sstevel@tonic-gate } else
12430Sstevel@tonic-gate libp->cfga_ap_id_cmp_p = (int (*)(const
12440Sstevel@tonic-gate cfga_ap_log_id_t, const cfga_ap_log_id_t))sym;
12450Sstevel@tonic-gate
12460Sstevel@tonic-gate /* Resolve version specific symbols */
12470Sstevel@tonic-gate return (libp->vers_ops->resolve_lib(libp));
12480Sstevel@tonic-gate }
12490Sstevel@tonic-gate
12500Sstevel@tonic-gate /*ARGSUSED*/
12510Sstevel@tonic-gate static cfga_err_t
null_resolve(plugin_lib_t * libp)12520Sstevel@tonic-gate null_resolve(plugin_lib_t *libp)
12530Sstevel@tonic-gate {
12540Sstevel@tonic-gate return (CFGA_OK);
12550Sstevel@tonic-gate }
12560Sstevel@tonic-gate
12570Sstevel@tonic-gate static cfga_err_t
resolve_v1(plugin_lib_t * libp)12580Sstevel@tonic-gate resolve_v1(plugin_lib_t *libp)
12590Sstevel@tonic-gate {
12600Sstevel@tonic-gate void *sym, *libhdlp = libp->handle;
12610Sstevel@tonic-gate
12620Sstevel@tonic-gate
12630Sstevel@tonic-gate if (libp->plugin_vers != CFGA_HSL_V1) {
12640Sstevel@tonic-gate return (CFGA_NO_LIB);
12650Sstevel@tonic-gate }
12660Sstevel@tonic-gate
12670Sstevel@tonic-gate if ((sym = dlsym(libhdlp, "cfga_stat")) == NULL) {
12680Sstevel@tonic-gate perror("dlsym: cfga_stat");
12690Sstevel@tonic-gate return (CFGA_LIB_ERROR);
12700Sstevel@tonic-gate } else
12710Sstevel@tonic-gate libp->cfga_stat_p = (cfga_err_t (*)(const char *,
12720Sstevel@tonic-gate struct cfga_stat_data *, const char *,
12730Sstevel@tonic-gate char **))sym;
12740Sstevel@tonic-gate
12750Sstevel@tonic-gate if ((sym = dlsym(libhdlp, "cfga_list")) == NULL) {
12760Sstevel@tonic-gate perror("dlsym: cfga_list");
12770Sstevel@tonic-gate return (CFGA_LIB_ERROR);
12780Sstevel@tonic-gate } else
12790Sstevel@tonic-gate libp->cfga_list_p = (cfga_err_t (*)(struct cfga_stat_data **,
12800Sstevel@tonic-gate int *, const char *, char **))sym;
12810Sstevel@tonic-gate
12820Sstevel@tonic-gate return (CFGA_OK);
12830Sstevel@tonic-gate }
12840Sstevel@tonic-gate
12850Sstevel@tonic-gate static cfga_err_t
resolve_v2(plugin_lib_t * libp)12860Sstevel@tonic-gate resolve_v2(plugin_lib_t *libp)
12870Sstevel@tonic-gate {
12880Sstevel@tonic-gate void *sym;
12890Sstevel@tonic-gate
12900Sstevel@tonic-gate
12910Sstevel@tonic-gate if (libp->plugin_vers != CFGA_HSL_V2) {
12920Sstevel@tonic-gate return (CFGA_NO_LIB);
12930Sstevel@tonic-gate }
12940Sstevel@tonic-gate
12950Sstevel@tonic-gate if ((sym = dlsym(libp->handle, "cfga_list_ext")) == NULL) {
12960Sstevel@tonic-gate perror("dlsym: cfga_list_ext");
12970Sstevel@tonic-gate return (CFGA_LIB_ERROR);
12980Sstevel@tonic-gate } else {
12990Sstevel@tonic-gate libp->cfga_list_ext_p = (cfga_err_t (*)(const char *,
13000Sstevel@tonic-gate struct cfga_list_data **, int *, const char *,
13010Sstevel@tonic-gate const char *, char **, cfga_flags_t))sym;
13020Sstevel@tonic-gate return (CFGA_OK);
13030Sstevel@tonic-gate }
13040Sstevel@tonic-gate }
13050Sstevel@tonic-gate
13060Sstevel@tonic-gate /*
13070Sstevel@tonic-gate * config_calloc_check - perform allocation, check result and
13080Sstevel@tonic-gate * set error string
13090Sstevel@tonic-gate */
13100Sstevel@tonic-gate static void *
config_calloc_check(size_t nelem,size_t elsize,char ** errstring)13110Sstevel@tonic-gate config_calloc_check(
13120Sstevel@tonic-gate size_t nelem,
13130Sstevel@tonic-gate size_t elsize,
13140Sstevel@tonic-gate char **errstring)
13150Sstevel@tonic-gate {
13160Sstevel@tonic-gate void *p;
13170Sstevel@tonic-gate
13180Sstevel@tonic-gate p = calloc(nelem, elsize);
13190Sstevel@tonic-gate if (p == NULL) {
13200Sstevel@tonic-gate config_err(0, ALLOC_FAILED, errstring);
13210Sstevel@tonic-gate }
13220Sstevel@tonic-gate
13230Sstevel@tonic-gate return (p);
13240Sstevel@tonic-gate }
13250Sstevel@tonic-gate
13260Sstevel@tonic-gate
13270Sstevel@tonic-gate /*
13280Sstevel@tonic-gate * config_get_lib - given an ap_id find the library name
13290Sstevel@tonic-gate * If successful, the plugin library is held.
13300Sstevel@tonic-gate */
13310Sstevel@tonic-gate static cfga_err_t
config_get_lib(const char * ap_id,lib_loc_t * lib_loc_p,char ** errstring)13320Sstevel@tonic-gate config_get_lib(
13330Sstevel@tonic-gate const char *ap_id,
13340Sstevel@tonic-gate lib_loc_t *lib_loc_p,
13350Sstevel@tonic-gate char **errstring)
13360Sstevel@tonic-gate {
13370Sstevel@tonic-gate char *dyncomp, path[PATH_MAX];
13380Sstevel@tonic-gate char *apdup;
13390Sstevel@tonic-gate cfga_ap_types_t type = UNKNOWN_AP;
13400Sstevel@tonic-gate cfga_err_t ret = CFGA_ERROR;
13410Sstevel@tonic-gate
13420Sstevel@tonic-gate if (ap_id == NULL) {
13430Sstevel@tonic-gate config_err(0, INVALID_ARGS, errstring);
13440Sstevel@tonic-gate return (ret);
13450Sstevel@tonic-gate }
13460Sstevel@tonic-gate
13470Sstevel@tonic-gate lib_loc_p->libp = NULL;
13480Sstevel@tonic-gate
13490Sstevel@tonic-gate if ((apdup = config_calloc_check(1, strlen(ap_id) + 1, errstring))
13500Sstevel@tonic-gate == NULL) {
13510Sstevel@tonic-gate return (CFGA_LIB_ERROR);
13520Sstevel@tonic-gate }
13530Sstevel@tonic-gate (void) strcpy(apdup, ap_id);
13540Sstevel@tonic-gate
13550Sstevel@tonic-gate /*
13560Sstevel@tonic-gate * Separate into base and dynamic components
13570Sstevel@tonic-gate */
13580Sstevel@tonic-gate if ((ret = split_apid(apdup, &dyncomp, errstring)) != CFGA_OK) {
13590Sstevel@tonic-gate goto out;
13600Sstevel@tonic-gate }
13610Sstevel@tonic-gate
13620Sstevel@tonic-gate /*
13630Sstevel@tonic-gate * No upper limit on version
13640Sstevel@tonic-gate */
13650Sstevel@tonic-gate lib_loc_p->vers_req.v_max = CFGA_HSL_VERS;
13660Sstevel@tonic-gate if (dyncomp != NULL) {
13670Sstevel@tonic-gate /*
13680Sstevel@tonic-gate * We need atleast version 2 of the plug-in library
13690Sstevel@tonic-gate * interface since the ap_id has a dynamic component.
13700Sstevel@tonic-gate */
13710Sstevel@tonic-gate
13720Sstevel@tonic-gate lib_loc_p->vers_req.v_min = CFGA_HSL_V2;
13730Sstevel@tonic-gate } else {
13740Sstevel@tonic-gate lib_loc_p->vers_req.v_min = CFGA_HSL_V1;
13750Sstevel@tonic-gate }
13760Sstevel@tonic-gate
13770Sstevel@tonic-gate /*
13780Sstevel@tonic-gate * If the ap_id is a devlink in CFGA_DEV_DIR, follow link
13790Sstevel@tonic-gate * to get the physical ap_id.
13800Sstevel@tonic-gate */
13810Sstevel@tonic-gate if ((type = find_arg_type(apdup)) == LOGICAL_LINK_AP) {
13820Sstevel@tonic-gate (void) snprintf(lib_loc_p->ap_base, sizeof (lib_loc_p->ap_base),
13830Sstevel@tonic-gate "%s%s", CFGA_DEV_DIR SLASH, apdup);
13840Sstevel@tonic-gate }
13850Sstevel@tonic-gate
13860Sstevel@tonic-gate path[sizeof (path) - 1] = '\0';
13870Sstevel@tonic-gate if (type == LOGICAL_LINK_AP && realpath(lib_loc_p->ap_base, path)
13880Sstevel@tonic-gate != NULL) {
13890Sstevel@tonic-gate (void) snprintf(lib_loc_p->ap_base, sizeof (lib_loc_p->ap_base),
13900Sstevel@tonic-gate "%s", path);
13910Sstevel@tonic-gate } else {
13920Sstevel@tonic-gate (void) snprintf(lib_loc_p->ap_base, sizeof (lib_loc_p->ap_base),
13930Sstevel@tonic-gate "%s", apdup);
13940Sstevel@tonic-gate }
13950Sstevel@tonic-gate
13960Sstevel@tonic-gate
13970Sstevel@tonic-gate /*
13980Sstevel@tonic-gate * find and load the library
13990Sstevel@tonic-gate * The base component of the ap_id is used to locate the plug-in
1400*10923SEvan.Yan@Sun.COM *
1401*10923SEvan.Yan@Sun.COM * NOTE that PCIE/PCISHPC connectors also have minor nodes &
1402*10923SEvan.Yan@Sun.COM * dev links created for now.
14030Sstevel@tonic-gate */
14040Sstevel@tonic-gate if ((type = find_arg_type(lib_loc_p->ap_base)) == PHYSICAL_AP) {
14050Sstevel@tonic-gate /*
14060Sstevel@tonic-gate * physical ap_id: Use ap_base as root for tree walk
14070Sstevel@tonic-gate * A link based apid (logical) will resolve to a physical
14080Sstevel@tonic-gate * ap_id.
14090Sstevel@tonic-gate */
14100Sstevel@tonic-gate ret = find_ap_common(lib_loc_p, lib_loc_p->ap_base,
1411*10923SEvan.Yan@Sun.COM check_ap_phys, check_ap_phys_hp, errstring);
14120Sstevel@tonic-gate } else if ((type == LOGICAL_DRV_AP) ||
14130Sstevel@tonic-gate (type == AP_TYPE && dyncomp == NULL)) {
14140Sstevel@tonic-gate /*
14150Sstevel@tonic-gate * logical ap_id or ap_type: Use "/" as root for tree walk
14160Sstevel@tonic-gate * Note: an aptype cannot have a dynamic component
14170Sstevel@tonic-gate */
1418*10923SEvan.Yan@Sun.COM ret = find_ap_common(lib_loc_p, "/", check_ap,
1419*10923SEvan.Yan@Sun.COM check_ap_hp, errstring);
14200Sstevel@tonic-gate } else {
14210Sstevel@tonic-gate ret = CFGA_APID_NOEXIST;
14220Sstevel@tonic-gate }
14230Sstevel@tonic-gate
14240Sstevel@tonic-gate if (ret == CFGA_OK) {
14250Sstevel@tonic-gate #ifndef NDEBUG
14260Sstevel@tonic-gate /*
14270Sstevel@tonic-gate * variables used by assert() only which is disabled
14280Sstevel@tonic-gate * by defining NDEBUG (see top of this file)
14290Sstevel@tonic-gate */
14300Sstevel@tonic-gate plugin_lib_t *libp;
14310Sstevel@tonic-gate
14320Sstevel@tonic-gate libp = lib_loc_p->libp;
14330Sstevel@tonic-gate #endif /* NDEBUG */
14340Sstevel@tonic-gate
14350Sstevel@tonic-gate assert(strcmp(libp->libpath, lib_loc_p->pathname) == 0);
14360Sstevel@tonic-gate assert(VALID_HSL_VERS(libp->plugin_vers));
14370Sstevel@tonic-gate
14380Sstevel@tonic-gate /*
14390Sstevel@tonic-gate * If a dynamic component was present, v1 plug-ins are not
14400Sstevel@tonic-gate * acceptable.
14410Sstevel@tonic-gate */
14420Sstevel@tonic-gate assert(dyncomp == NULL || libp->plugin_vers >= CFGA_HSL_V2);
14430Sstevel@tonic-gate
14440Sstevel@tonic-gate /*
14450Sstevel@tonic-gate * ap_physical is passed to plugins as their ap_id argument.
14460Sstevel@tonic-gate * Append dynamic component if any.
14470Sstevel@tonic-gate */
14480Sstevel@tonic-gate append_dyn(lib_loc_p->ap_physical, dyncomp,
14490Sstevel@tonic-gate sizeof (lib_loc_p->ap_physical));
14500Sstevel@tonic-gate }
14510Sstevel@tonic-gate
14520Sstevel@tonic-gate /* cleanup */
14530Sstevel@tonic-gate lib_loc_p->vers_req.v_min = INVALID_VERSION;
14540Sstevel@tonic-gate lib_loc_p->vers_req.v_max = INVALID_VERSION;
14550Sstevel@tonic-gate *lib_loc_p->ap_base = '\0';
14560Sstevel@tonic-gate
14570Sstevel@tonic-gate /*FALLTHRU*/
14580Sstevel@tonic-gate out:
14590Sstevel@tonic-gate S_FREE(apdup);
14600Sstevel@tonic-gate S_FREE(dyncomp);
14610Sstevel@tonic-gate if (ret != CFGA_OK) {
14620Sstevel@tonic-gate lib_loc_p->libp = NULL;
14630Sstevel@tonic-gate }
14640Sstevel@tonic-gate
14650Sstevel@tonic-gate assert(ret != CFGA_OK || lib_loc_p->libp != NULL);
14660Sstevel@tonic-gate
14670Sstevel@tonic-gate return (ret);
14680Sstevel@tonic-gate }
14690Sstevel@tonic-gate
1470*10923SEvan.Yan@Sun.COM /* load_lib - load library for non-SHP attachment point node */
14710Sstevel@tonic-gate static cfga_err_t
load_lib(di_node_t node,di_minor_t minor,lib_loc_t * libloc_p)14720Sstevel@tonic-gate load_lib(
14730Sstevel@tonic-gate di_node_t node,
14740Sstevel@tonic-gate di_minor_t minor,
14750Sstevel@tonic-gate lib_loc_t *libloc_p)
14760Sstevel@tonic-gate {
1477*10923SEvan.Yan@Sun.COM return (load_lib_impl(node, minor, NULL, libloc_p));
1478*10923SEvan.Yan@Sun.COM }
1479*10923SEvan.Yan@Sun.COM
1480*10923SEvan.Yan@Sun.COM /* load_lib_hp - load library for SHP attachment point node */
1481*10923SEvan.Yan@Sun.COM static cfga_err_t
load_lib_hp(di_node_t node,di_hp_t hp,lib_loc_t * libloc_p)1482*10923SEvan.Yan@Sun.COM load_lib_hp(
1483*10923SEvan.Yan@Sun.COM di_node_t node,
1484*10923SEvan.Yan@Sun.COM di_hp_t hp,
1485*10923SEvan.Yan@Sun.COM lib_loc_t *libloc_p)
1486*10923SEvan.Yan@Sun.COM {
1487*10923SEvan.Yan@Sun.COM return (load_lib_impl(node, NULL, hp, libloc_p));
1488*10923SEvan.Yan@Sun.COM }
1489*10923SEvan.Yan@Sun.COM
1490*10923SEvan.Yan@Sun.COM /*
1491*10923SEvan.Yan@Sun.COM * load_lib_impl - Given a library pathname, create a entry for it
1492*10923SEvan.Yan@Sun.COM * in the library list, * if one does not already exist, and read
1493*10923SEvan.Yan@Sun.COM * lock it to keep it there.
1494*10923SEvan.Yan@Sun.COM */
1495*10923SEvan.Yan@Sun.COM static cfga_err_t
load_lib_impl(di_node_t node,di_minor_t minor,di_hp_t hp,lib_loc_t * libloc_p)1496*10923SEvan.Yan@Sun.COM load_lib_impl(
1497*10923SEvan.Yan@Sun.COM di_node_t node,
1498*10923SEvan.Yan@Sun.COM di_minor_t minor,
1499*10923SEvan.Yan@Sun.COM di_hp_t hp,
1500*10923SEvan.Yan@Sun.COM lib_loc_t *libloc_p)
1501*10923SEvan.Yan@Sun.COM {
15020Sstevel@tonic-gate plugin_lib_t *libp, *list_libp;
15030Sstevel@tonic-gate char *devfs_path;
1504*10923SEvan.Yan@Sun.COM char *name;
1505*10923SEvan.Yan@Sun.COM
1506*10923SEvan.Yan@Sun.COM if (minor != DI_MINOR_NIL && hp != DI_HP_NIL)
1507*10923SEvan.Yan@Sun.COM return (CFGA_LIB_ERROR);
1508*10923SEvan.Yan@Sun.COM
1509*10923SEvan.Yan@Sun.COM if (minor != DI_MINOR_NIL)
1510*10923SEvan.Yan@Sun.COM name = di_minor_name(minor);
1511*10923SEvan.Yan@Sun.COM else
1512*10923SEvan.Yan@Sun.COM name = di_hp_name(hp);
15130Sstevel@tonic-gate
15140Sstevel@tonic-gate /*
15150Sstevel@tonic-gate * lock the library list
15160Sstevel@tonic-gate */
15170Sstevel@tonic-gate (void) mutex_lock(&plugin_list_lock);
15180Sstevel@tonic-gate
15190Sstevel@tonic-gate /*
15200Sstevel@tonic-gate * see if lib exist in list, if not, allocate a new one
15210Sstevel@tonic-gate */
15220Sstevel@tonic-gate list_libp = lib_in_list(libloc_p->pathname);
15230Sstevel@tonic-gate if (list_libp != NULL) {
15240Sstevel@tonic-gate hold_lib(list_libp);
15250Sstevel@tonic-gate (void) mutex_unlock(&plugin_list_lock);
15260Sstevel@tonic-gate
15270Sstevel@tonic-gate /* fill in logical and physical name in libloc_p */
15280Sstevel@tonic-gate libloc_p->libp = libp = list_libp;
1529*10923SEvan.Yan@Sun.COM if (minor != DI_MINOR_NIL) {
1530*10923SEvan.Yan@Sun.COM if (libp->vers_ops->mklog(node, minor, libp, libloc_p)
1531*10923SEvan.Yan@Sun.COM != CFGA_OK) {
1532*10923SEvan.Yan@Sun.COM rele_lib(list_libp);
1533*10923SEvan.Yan@Sun.COM return (CFGA_LIB_ERROR);
1534*10923SEvan.Yan@Sun.COM }
1535*10923SEvan.Yan@Sun.COM } else {
1536*10923SEvan.Yan@Sun.COM if (mklog_hp(node, hp, libp, libloc_p) != CFGA_OK) {
1537*10923SEvan.Yan@Sun.COM rele_lib(list_libp);
1538*10923SEvan.Yan@Sun.COM return (CFGA_LIB_ERROR);
1539*10923SEvan.Yan@Sun.COM }
15400Sstevel@tonic-gate }
15410Sstevel@tonic-gate
15420Sstevel@tonic-gate devfs_path = di_devfs_path(node);
15430Sstevel@tonic-gate (void) snprintf(libloc_p->ap_physical, MAXPATHLEN, "%s%s:%s",
1544*10923SEvan.Yan@Sun.COM DEVICES_DIR, devfs_path, name);
15450Sstevel@tonic-gate di_devfs_path_free(devfs_path);
15460Sstevel@tonic-gate
15470Sstevel@tonic-gate return (CFGA_OK);
15480Sstevel@tonic-gate }
15490Sstevel@tonic-gate
15500Sstevel@tonic-gate /* allocate a new plugin_lib_t structure */
15510Sstevel@tonic-gate libp = config_calloc_check(1, sizeof (plugin_lib_t), NULL);
15520Sstevel@tonic-gate if (libp == NULL) {
15530Sstevel@tonic-gate (void) mutex_unlock(&plugin_list_lock);
15540Sstevel@tonic-gate return (CFGA_LIB_ERROR);
15550Sstevel@tonic-gate }
15560Sstevel@tonic-gate
15570Sstevel@tonic-gate (void) snprintf(libp->libpath, sizeof (libp->libpath), "%s",
15580Sstevel@tonic-gate libloc_p->pathname);
15590Sstevel@tonic-gate
15600Sstevel@tonic-gate /*
15610Sstevel@tonic-gate * ensure that the lib is open and linked in
15620Sstevel@tonic-gate */
15630Sstevel@tonic-gate libp->handle = dlopen(libp->libpath, RTLD_NOW);
15640Sstevel@tonic-gate if (libp->handle == NULL) {
15650Sstevel@tonic-gate (void) mutex_unlock(&plugin_list_lock);
15660Sstevel@tonic-gate free(libp);
15670Sstevel@tonic-gate return (CFGA_NO_LIB);
15680Sstevel@tonic-gate }
15690Sstevel@tonic-gate
1570*10923SEvan.Yan@Sun.COM if (minor != DI_MINOR_NIL) {
1571*10923SEvan.Yan@Sun.COM if (resolve_lib_ref(libp, libloc_p) != CFGA_OK ||
1572*10923SEvan.Yan@Sun.COM libp->vers_ops->mklog(node, minor, libp, libloc_p)
1573*10923SEvan.Yan@Sun.COM != CFGA_OK) {
1574*10923SEvan.Yan@Sun.COM (void) mutex_unlock(&plugin_list_lock);
1575*10923SEvan.Yan@Sun.COM (void) dlclose(libp->handle);
1576*10923SEvan.Yan@Sun.COM free(libp);
1577*10923SEvan.Yan@Sun.COM return (CFGA_NO_LIB);
1578*10923SEvan.Yan@Sun.COM }
1579*10923SEvan.Yan@Sun.COM } else {
1580*10923SEvan.Yan@Sun.COM if (resolve_lib_ref(libp, libloc_p) != CFGA_OK ||
1581*10923SEvan.Yan@Sun.COM mklog_hp(node, hp, libp, libloc_p) != CFGA_OK) {
1582*10923SEvan.Yan@Sun.COM (void) mutex_unlock(&plugin_list_lock);
1583*10923SEvan.Yan@Sun.COM (void) dlclose(libp->handle);
1584*10923SEvan.Yan@Sun.COM free(libp);
1585*10923SEvan.Yan@Sun.COM return (CFGA_NO_LIB);
1586*10923SEvan.Yan@Sun.COM }
15870Sstevel@tonic-gate }
15880Sstevel@tonic-gate
15890Sstevel@tonic-gate /*
15900Sstevel@tonic-gate * link in new entry to the end of list
15910Sstevel@tonic-gate */
15920Sstevel@tonic-gate list_libp = &plugin_list;
15930Sstevel@tonic-gate while (list_libp->next != NULL)
15940Sstevel@tonic-gate list_libp = list_libp->next;
15950Sstevel@tonic-gate libp->next = list_libp->next;
15960Sstevel@tonic-gate list_libp->next = libp;
15970Sstevel@tonic-gate
15980Sstevel@tonic-gate /* Initialize refcnt to 1 */
15990Sstevel@tonic-gate libp->refcnt = 1;
16000Sstevel@tonic-gate (void) mutex_init(&libp->lock, USYNC_THREAD, NULL);
16010Sstevel@tonic-gate
16020Sstevel@tonic-gate (void) mutex_unlock(&plugin_list_lock);
16030Sstevel@tonic-gate
16040Sstevel@tonic-gate /*
16050Sstevel@tonic-gate * record libp and physical node name in the libloc struct
16060Sstevel@tonic-gate */
16070Sstevel@tonic-gate libloc_p->libp = libp;
16080Sstevel@tonic-gate devfs_path = di_devfs_path(node);
16090Sstevel@tonic-gate (void) snprintf(libloc_p->ap_physical, MAXPATHLEN, "%s%s:%s",
1610*10923SEvan.Yan@Sun.COM DEVICES_DIR, devfs_path, name);
16110Sstevel@tonic-gate di_devfs_path_free(devfs_path);
16120Sstevel@tonic-gate
16130Sstevel@tonic-gate return (CFGA_OK);
16140Sstevel@tonic-gate }
16150Sstevel@tonic-gate
16160Sstevel@tonic-gate
16170Sstevel@tonic-gate #define NUM_LIB_NAMES 2
16180Sstevel@tonic-gate
16190Sstevel@tonic-gate /*
1620*10923SEvan.Yan@Sun.COM * find_lib - find library for non-SHP attachment point node
16210Sstevel@tonic-gate */
16220Sstevel@tonic-gate static cfga_err_t
find_lib(di_node_t node,di_minor_t minor,lib_loc_t * libloc_p)16230Sstevel@tonic-gate find_lib(
16240Sstevel@tonic-gate di_node_t node,
16250Sstevel@tonic-gate di_minor_t minor,
16260Sstevel@tonic-gate lib_loc_t *libloc_p)
16270Sstevel@tonic-gate {
16280Sstevel@tonic-gate char name[NUM_LIB_NAMES][MAXPATHLEN];
16290Sstevel@tonic-gate char *class = NULL, *drv = NULL;
1630*10923SEvan.Yan@Sun.COM int i;
16310Sstevel@tonic-gate
16320Sstevel@tonic-gate
16330Sstevel@tonic-gate /* Make sure pathname and class is null if we fail */
1634*10923SEvan.Yan@Sun.COM *libloc_p->ap_class = *libloc_p->pathname = '\0';
16350Sstevel@tonic-gate
16360Sstevel@tonic-gate /*
16370Sstevel@tonic-gate * Initialize possible library tags.
16380Sstevel@tonic-gate */
16390Sstevel@tonic-gate
16400Sstevel@tonic-gate drv = di_driver_name(node);
16410Sstevel@tonic-gate class = get_class(minor);
16420Sstevel@tonic-gate
16430Sstevel@tonic-gate if (drv == NULL || class == NULL) {
16440Sstevel@tonic-gate return (CFGA_LIB_ERROR);
16450Sstevel@tonic-gate }
16460Sstevel@tonic-gate
16470Sstevel@tonic-gate i = 0;
16480Sstevel@tonic-gate (void) snprintf(&name[i++][0], sizeof (name[0]), "%s", drv);
16490Sstevel@tonic-gate (void) snprintf(&name[i++][0], sizeof (name[0]), "%s", class);
16500Sstevel@tonic-gate
16510Sstevel@tonic-gate /*
16520Sstevel@tonic-gate * Cycle through the array of names to find the library.
16530Sstevel@tonic-gate */
16540Sstevel@tonic-gate for (i = 0; i < NUM_LIB_NAMES; i++) {
16550Sstevel@tonic-gate
16560Sstevel@tonic-gate /* Attachment points may not have a class (i.e. are generic) */
16570Sstevel@tonic-gate if (name[i][0] == '\0') {
16580Sstevel@tonic-gate continue;
16590Sstevel@tonic-gate }
16600Sstevel@tonic-gate
1661*10923SEvan.Yan@Sun.COM if (find_lib_impl(name[i], libloc_p) == CFGA_OK)
1662*10923SEvan.Yan@Sun.COM goto found;
1663*10923SEvan.Yan@Sun.COM }
1664*10923SEvan.Yan@Sun.COM
1665*10923SEvan.Yan@Sun.COM return (CFGA_NO_LIB);
1666*10923SEvan.Yan@Sun.COM
1667*10923SEvan.Yan@Sun.COM found:
1668*10923SEvan.Yan@Sun.COM
1669*10923SEvan.Yan@Sun.COM /* Record class name (if any) */
1670*10923SEvan.Yan@Sun.COM (void) snprintf(libloc_p->ap_class, sizeof (libloc_p->ap_class), "%s",
1671*10923SEvan.Yan@Sun.COM class);
1672*10923SEvan.Yan@Sun.COM
1673*10923SEvan.Yan@Sun.COM return (CFGA_OK);
1674*10923SEvan.Yan@Sun.COM }
1675*10923SEvan.Yan@Sun.COM
1676*10923SEvan.Yan@Sun.COM /*
1677*10923SEvan.Yan@Sun.COM * find_lib_hp - find library for SHP attachment point
1678*10923SEvan.Yan@Sun.COM */
1679*10923SEvan.Yan@Sun.COM /*ARGSUSED*/
1680*10923SEvan.Yan@Sun.COM static cfga_err_t
find_lib_hp(di_node_t node,di_hp_t hp,lib_loc_t * libloc_p)1681*10923SEvan.Yan@Sun.COM find_lib_hp(
1682*10923SEvan.Yan@Sun.COM di_node_t node,
1683*10923SEvan.Yan@Sun.COM di_hp_t hp,
1684*10923SEvan.Yan@Sun.COM lib_loc_t *libloc_p)
1685*10923SEvan.Yan@Sun.COM {
1686*10923SEvan.Yan@Sun.COM char name[MAXPATHLEN];
1687*10923SEvan.Yan@Sun.COM char *class = NULL;
1688*10923SEvan.Yan@Sun.COM
1689*10923SEvan.Yan@Sun.COM
1690*10923SEvan.Yan@Sun.COM /* Make sure pathname and class is null if we fail */
1691*10923SEvan.Yan@Sun.COM *libloc_p->ap_class = *libloc_p->pathname = '\0';
1692*10923SEvan.Yan@Sun.COM
1693*10923SEvan.Yan@Sun.COM /*
1694*10923SEvan.Yan@Sun.COM * Initialize possible library tags.
1695*10923SEvan.Yan@Sun.COM *
1696*10923SEvan.Yan@Sun.COM * Only support PCI class for now, this will need to be
1697*10923SEvan.Yan@Sun.COM * changed as other plugins are migrated to SHP plugin.
1698*10923SEvan.Yan@Sun.COM */
1699*10923SEvan.Yan@Sun.COM class = "pci";
1700*10923SEvan.Yan@Sun.COM #if 0
1701*10923SEvan.Yan@Sun.COM /*
1702*10923SEvan.Yan@Sun.COM * No type check for now as PCI is the only class SHP plugin
1703*10923SEvan.Yan@Sun.COM * supports. In the future we'll need to enable the type check
1704*10923SEvan.Yan@Sun.COM * and set class accordingly, when non PCI plugins are migrated
1705*10923SEvan.Yan@Sun.COM * to SHP. In that case we'll probably need to add an additional
1706*10923SEvan.Yan@Sun.COM * interface between libcfgadm and the plugins, and SHP plugin will
1707*10923SEvan.Yan@Sun.COM * implement this interface which will translate the bus specific
1708*10923SEvan.Yan@Sun.COM * strings to standard classes that libcfgadm can recognize, for
1709*10923SEvan.Yan@Sun.COM * all the buses it supports, e.g. for pci/pcie it will translate
1710*10923SEvan.Yan@Sun.COM * PCIE_NATIVE_HP_TYPE to string "pci". We'll also need to bump up
1711*10923SEvan.Yan@Sun.COM * SHP plugin version to 3 to use the new interface.
1712*10923SEvan.Yan@Sun.COM */
1713*10923SEvan.Yan@Sun.COM class = di_hp_type(hp);
1714*10923SEvan.Yan@Sun.COM if ((strcmp(class, PCIE_NATIVE_HP_TYPE) == 0) ||
1715*10923SEvan.Yan@Sun.COM (strcmp(class, PCIE_ACPI_HP_TYPE) == 0) ||
1716*10923SEvan.Yan@Sun.COM (strcmp(class, PCIE_PCI_HP_TYPE) == 0)) {
1717*10923SEvan.Yan@Sun.COM class = "pci";
1718*10923SEvan.Yan@Sun.COM } else {
1719*10923SEvan.Yan@Sun.COM goto fail;
1720*10923SEvan.Yan@Sun.COM }
1721*10923SEvan.Yan@Sun.COM #endif
1722*10923SEvan.Yan@Sun.COM (void) snprintf(&name[0], sizeof (name), "%s", "shp");
1723*10923SEvan.Yan@Sun.COM
1724*10923SEvan.Yan@Sun.COM if (find_lib_impl(name, libloc_p) == CFGA_OK)
1725*10923SEvan.Yan@Sun.COM goto found;
1726*10923SEvan.Yan@Sun.COM fail:
1727*10923SEvan.Yan@Sun.COM return (CFGA_NO_LIB);
1728*10923SEvan.Yan@Sun.COM
1729*10923SEvan.Yan@Sun.COM found:
1730*10923SEvan.Yan@Sun.COM
1731*10923SEvan.Yan@Sun.COM /* Record class name (if any) */
1732*10923SEvan.Yan@Sun.COM (void) snprintf(libloc_p->ap_class, sizeof (libloc_p->ap_class), "%s",
1733*10923SEvan.Yan@Sun.COM class);
1734*10923SEvan.Yan@Sun.COM
1735*10923SEvan.Yan@Sun.COM return (CFGA_OK);
1736*10923SEvan.Yan@Sun.COM }
1737*10923SEvan.Yan@Sun.COM
1738*10923SEvan.Yan@Sun.COM /*
1739*10923SEvan.Yan@Sun.COM * find_lib_impl - Given an attachment point node find it's library
1740*10923SEvan.Yan@Sun.COM */
1741*10923SEvan.Yan@Sun.COM static cfga_err_t
find_lib_impl(char * name,lib_loc_t * libloc_p)1742*10923SEvan.Yan@Sun.COM find_lib_impl(
1743*10923SEvan.Yan@Sun.COM char *name,
1744*10923SEvan.Yan@Sun.COM lib_loc_t *libloc_p)
1745*10923SEvan.Yan@Sun.COM {
1746*10923SEvan.Yan@Sun.COM char lib[MAXPATHLEN];
1747*10923SEvan.Yan@Sun.COM struct stat lib_stat;
1748*10923SEvan.Yan@Sun.COM void *dlhandle = NULL;
1749*10923SEvan.Yan@Sun.COM static char plat_name[SYSINFO_LENGTH];
1750*10923SEvan.Yan@Sun.COM static char machine_name[SYSINFO_LENGTH];
1751*10923SEvan.Yan@Sun.COM static char arch_name[SYSINFO_LENGTH];
1752*10923SEvan.Yan@Sun.COM
1753*10923SEvan.Yan@Sun.COM /*
1754*10923SEvan.Yan@Sun.COM * Initialize machine name and arch name
1755*10923SEvan.Yan@Sun.COM */
1756*10923SEvan.Yan@Sun.COM if (strncmp("", machine_name, MAXPATHLEN) == 0) {
1757*10923SEvan.Yan@Sun.COM if (sysinfo(SI_PLATFORM, plat_name, SYSINFO_LENGTH) == -1) {
1758*10923SEvan.Yan@Sun.COM return (CFGA_ERROR);
17590Sstevel@tonic-gate }
1760*10923SEvan.Yan@Sun.COM if (sysinfo(SI_ARCHITECTURE, arch_name, SYSINFO_LENGTH) == -1) {
1761*10923SEvan.Yan@Sun.COM return (CFGA_ERROR);
17620Sstevel@tonic-gate }
1763*10923SEvan.Yan@Sun.COM if (sysinfo(SI_MACHINE, machine_name, SYSINFO_LENGTH) == -1) {
1764*10923SEvan.Yan@Sun.COM return (CFGA_ERROR);
17650Sstevel@tonic-gate }
17660Sstevel@tonic-gate }
17670Sstevel@tonic-gate
1768*10923SEvan.Yan@Sun.COM /*
1769*10923SEvan.Yan@Sun.COM * Try path based upon platform name
1770*10923SEvan.Yan@Sun.COM */
1771*10923SEvan.Yan@Sun.COM (void) snprintf(lib, sizeof (lib), "%s%s%s%s%s",
1772*10923SEvan.Yan@Sun.COM LIB_PATH_BASE1, plat_name, LIB_PATH_MIDDLE,
1773*10923SEvan.Yan@Sun.COM name, LIB_PATH_TAIL);
1774*10923SEvan.Yan@Sun.COM
1775*10923SEvan.Yan@Sun.COM if (stat(lib, &lib_stat) == 0) {
1776*10923SEvan.Yan@Sun.COM /* file exists, is it a lib */
1777*10923SEvan.Yan@Sun.COM dlhandle = dlopen(lib, RTLD_LAZY);
1778*10923SEvan.Yan@Sun.COM if (dlhandle != NULL) {
1779*10923SEvan.Yan@Sun.COM goto found;
1780*10923SEvan.Yan@Sun.COM }
1781*10923SEvan.Yan@Sun.COM }
1782*10923SEvan.Yan@Sun.COM
1783*10923SEvan.Yan@Sun.COM /*
1784*10923SEvan.Yan@Sun.COM * Try path based upon machine name
1785*10923SEvan.Yan@Sun.COM */
1786*10923SEvan.Yan@Sun.COM (void) snprintf(lib, sizeof (lib), "%s%s%s%s%s",
1787*10923SEvan.Yan@Sun.COM LIB_PATH_BASE1, machine_name, LIB_PATH_MIDDLE,
1788*10923SEvan.Yan@Sun.COM name, LIB_PATH_TAIL);
1789*10923SEvan.Yan@Sun.COM
1790*10923SEvan.Yan@Sun.COM
1791*10923SEvan.Yan@Sun.COM if (stat(lib, &lib_stat) == 0) {
1792*10923SEvan.Yan@Sun.COM /* file exists, is it a lib */
1793*10923SEvan.Yan@Sun.COM dlhandle = dlopen(lib, RTLD_LAZY);
1794*10923SEvan.Yan@Sun.COM if (dlhandle != NULL) {
1795*10923SEvan.Yan@Sun.COM goto found;
1796*10923SEvan.Yan@Sun.COM }
1797*10923SEvan.Yan@Sun.COM }
1798*10923SEvan.Yan@Sun.COM
1799*10923SEvan.Yan@Sun.COM /*
1800*10923SEvan.Yan@Sun.COM * Try path based upon arch name
1801*10923SEvan.Yan@Sun.COM */
1802*10923SEvan.Yan@Sun.COM (void) snprintf(lib, sizeof (lib), "%s%s%s%s%s",
1803*10923SEvan.Yan@Sun.COM LIB_PATH_BASE1, arch_name, LIB_PATH_MIDDLE,
1804*10923SEvan.Yan@Sun.COM name, LIB_PATH_TAIL);
1805*10923SEvan.Yan@Sun.COM
1806*10923SEvan.Yan@Sun.COM if (stat(lib, &lib_stat) == 0) {
1807*10923SEvan.Yan@Sun.COM /* file exists, is it a lib */
1808*10923SEvan.Yan@Sun.COM dlhandle = dlopen(lib, RTLD_LAZY);
1809*10923SEvan.Yan@Sun.COM if (dlhandle != NULL) {
1810*10923SEvan.Yan@Sun.COM goto found;
1811*10923SEvan.Yan@Sun.COM }
1812*10923SEvan.Yan@Sun.COM
1813*10923SEvan.Yan@Sun.COM }
1814*10923SEvan.Yan@Sun.COM
1815*10923SEvan.Yan@Sun.COM /*
1816*10923SEvan.Yan@Sun.COM * Try generic location
1817*10923SEvan.Yan@Sun.COM */
1818*10923SEvan.Yan@Sun.COM (void) snprintf(lib, sizeof (lib), "%s%s%s%s",
1819*10923SEvan.Yan@Sun.COM LIB_PATH_BASE2, LIB_PATH_MIDDLE, name, LIB_PATH_TAIL);
1820*10923SEvan.Yan@Sun.COM
1821*10923SEvan.Yan@Sun.COM if (stat(lib, &lib_stat) == 0) {
1822*10923SEvan.Yan@Sun.COM /* file exists, is it a lib */
1823*10923SEvan.Yan@Sun.COM dlhandle = dlopen(lib, RTLD_LAZY);
1824*10923SEvan.Yan@Sun.COM if (dlhandle != NULL) {
1825*10923SEvan.Yan@Sun.COM goto found;
1826*10923SEvan.Yan@Sun.COM }
1827*10923SEvan.Yan@Sun.COM
1828*10923SEvan.Yan@Sun.COM }
18290Sstevel@tonic-gate return (CFGA_NO_LIB);
18300Sstevel@tonic-gate
18310Sstevel@tonic-gate found:
18320Sstevel@tonic-gate /* we got one! */
18330Sstevel@tonic-gate (void) snprintf(libloc_p->pathname, sizeof (libloc_p->pathname), "%s",
18340Sstevel@tonic-gate lib);
18350Sstevel@tonic-gate
18360Sstevel@tonic-gate (void) dlclose(dlhandle);
18370Sstevel@tonic-gate
18380Sstevel@tonic-gate return (CFGA_OK);
18390Sstevel@tonic-gate }
18400Sstevel@tonic-gate
18410Sstevel@tonic-gate static cfga_err_t
lookup_cache(lib_loc_t * libloc_p)18420Sstevel@tonic-gate lookup_cache(lib_loc_t *libloc_p)
18430Sstevel@tonic-gate {
18440Sstevel@tonic-gate lib_cache_t *entry;
18450Sstevel@tonic-gate (void) mutex_lock(&lib_cache_lock);
18460Sstevel@tonic-gate entry = lib_cache;
18470Sstevel@tonic-gate while (entry) {
18480Sstevel@tonic-gate if (strcmp(entry->lc_ap_id, libloc_p->ap_base) == 0) {
18490Sstevel@tonic-gate plugin_lib_t *libp = entry->lc_libp;
18500Sstevel@tonic-gate libloc_p->libp = libp;
18510Sstevel@tonic-gate hold_lib(libp);
18520Sstevel@tonic-gate (void) strcpy(libloc_p->pathname, libp->libpath);
18530Sstevel@tonic-gate (void) strcpy(libloc_p->ap_physical,
18540Sstevel@tonic-gate entry->lc_ap_physical);
18550Sstevel@tonic-gate (void) strcpy(libloc_p->ap_logical,
18560Sstevel@tonic-gate entry->lc_ap_logical);
18570Sstevel@tonic-gate (void) mutex_unlock(&lib_cache_lock);
18580Sstevel@tonic-gate return (CFGA_OK);
18590Sstevel@tonic-gate }
18600Sstevel@tonic-gate entry = entry->lc_next;
18610Sstevel@tonic-gate }
18620Sstevel@tonic-gate (void) mutex_unlock(&lib_cache_lock);
18630Sstevel@tonic-gate
18640Sstevel@tonic-gate return (CFGA_ERROR);
18650Sstevel@tonic-gate }
18660Sstevel@tonic-gate
18670Sstevel@tonic-gate static void
update_cache(lib_loc_t * libloc_p)18680Sstevel@tonic-gate update_cache(lib_loc_t *libloc_p)
18690Sstevel@tonic-gate {
18700Sstevel@tonic-gate lib_cache_t *entry;
18710Sstevel@tonic-gate entry = config_calloc_check(1, sizeof (lib_cache_t), NULL);
18720Sstevel@tonic-gate if (entry == NULL)
18730Sstevel@tonic-gate return;
18740Sstevel@tonic-gate
18750Sstevel@tonic-gate entry->lc_ap_id = strdup(libloc_p->ap_base);
18760Sstevel@tonic-gate entry->lc_ap_physical = strdup(libloc_p->ap_physical);
18770Sstevel@tonic-gate entry->lc_ap_logical = strdup(libloc_p->ap_logical);
18780Sstevel@tonic-gate if ((entry->lc_ap_id == NULL) || (entry->lc_ap_physical == NULL) ||
18790Sstevel@tonic-gate (entry->lc_ap_logical == NULL)) {
18800Sstevel@tonic-gate free(entry->lc_ap_id);
18810Sstevel@tonic-gate free(entry->lc_ap_physical);
18820Sstevel@tonic-gate free(entry->lc_ap_logical);
18830Sstevel@tonic-gate free(entry);
18840Sstevel@tonic-gate return;
18850Sstevel@tonic-gate }
18860Sstevel@tonic-gate
18870Sstevel@tonic-gate (void) mutex_lock(&lib_cache_lock);
18880Sstevel@tonic-gate entry->lc_libp = libloc_p->libp;
18890Sstevel@tonic-gate entry->lc_next = lib_cache;
18900Sstevel@tonic-gate lib_cache = entry;
18910Sstevel@tonic-gate hold_lib(entry->lc_libp); /* prevent stale cache */
18920Sstevel@tonic-gate (void) mutex_unlock(&lib_cache_lock);
18930Sstevel@tonic-gate }
18940Sstevel@tonic-gate
18950Sstevel@tonic-gate static void
destroy_cache()18960Sstevel@tonic-gate destroy_cache()
18970Sstevel@tonic-gate {
18980Sstevel@tonic-gate lib_cache_t *entry, *next;
18990Sstevel@tonic-gate (void) mutex_lock(&lib_cache_lock);
19000Sstevel@tonic-gate entry = lib_cache;
19010Sstevel@tonic-gate while (entry) {
19020Sstevel@tonic-gate next = entry->lc_next;
19030Sstevel@tonic-gate rele_lib(entry->lc_libp);
19040Sstevel@tonic-gate free(entry->lc_ap_id);
19050Sstevel@tonic-gate free(entry->lc_ap_physical);
19060Sstevel@tonic-gate free(entry->lc_ap_logical);
19070Sstevel@tonic-gate free(entry);
19080Sstevel@tonic-gate entry = next;
19090Sstevel@tonic-gate }
19100Sstevel@tonic-gate (void) mutex_unlock(&lib_cache_lock);
19110Sstevel@tonic-gate }
19120Sstevel@tonic-gate
19130Sstevel@tonic-gate /*
19140Sstevel@tonic-gate * find_ap_common - locate a particular attachment point
19150Sstevel@tonic-gate */
19160Sstevel@tonic-gate static cfga_err_t
find_ap_common(lib_loc_t * libloc_p,const char * physpath,int (* fcn)(di_node_t node,di_minor_t minor,void * arg),int (* fcn_hp)(di_node_t node,di_hp_t hp,void * arg),char ** errstring)19170Sstevel@tonic-gate find_ap_common(
19180Sstevel@tonic-gate lib_loc_t *libloc_p,
19190Sstevel@tonic-gate const char *physpath,
19200Sstevel@tonic-gate int (*fcn)(di_node_t node, di_minor_t minor, void *arg),
1921*10923SEvan.Yan@Sun.COM int (*fcn_hp)(di_node_t node, di_hp_t hp, void *arg),
19220Sstevel@tonic-gate char **errstring)
19230Sstevel@tonic-gate {
19246852Scth di_node_t rnode, wnode;
19250Sstevel@tonic-gate char *cp, *rpath;
19260Sstevel@tonic-gate size_t len;
19270Sstevel@tonic-gate
19280Sstevel@tonic-gate if (lookup_cache(libloc_p) == CFGA_OK)
19290Sstevel@tonic-gate return (CFGA_OK);
19300Sstevel@tonic-gate
19310Sstevel@tonic-gate if ((rpath = config_calloc_check(1, strlen(physpath) + 1,
19320Sstevel@tonic-gate errstring)) == NULL) {
19330Sstevel@tonic-gate return (CFGA_LIB_ERROR);
19340Sstevel@tonic-gate }
19350Sstevel@tonic-gate
19360Sstevel@tonic-gate (void) strcpy(rpath, physpath);
19370Sstevel@tonic-gate
19380Sstevel@tonic-gate /* Remove devices prefix (if any) */
19390Sstevel@tonic-gate len = strlen(DEVICES_DIR);
19400Sstevel@tonic-gate if (strncmp(rpath, DEVICES_DIR SLASH, len + strlen(SLASH)) == 0) {
19410Sstevel@tonic-gate (void) memmove(rpath, rpath + len,
19420Sstevel@tonic-gate strlen(rpath + len) + 1);
19430Sstevel@tonic-gate }
19440Sstevel@tonic-gate
19450Sstevel@tonic-gate /* Remove dynamic component if any */
19460Sstevel@tonic-gate if ((cp = GET_DYN(rpath)) != NULL) {
19470Sstevel@tonic-gate *cp = '\0';
19480Sstevel@tonic-gate }
19490Sstevel@tonic-gate
19500Sstevel@tonic-gate /* Remove minor name (if any) */
19510Sstevel@tonic-gate if ((cp = strrchr(rpath, ':')) != NULL) {
19520Sstevel@tonic-gate *cp = '\0';
19530Sstevel@tonic-gate }
19540Sstevel@tonic-gate
19550Sstevel@tonic-gate /*
19560Sstevel@tonic-gate * begin walk of device tree
1957*10923SEvan.Yan@Sun.COM *
1958*10923SEvan.Yan@Sun.COM * Since we create minor nodes & dev links for both all PCI/PCIE
1959*10923SEvan.Yan@Sun.COM * connectors, but only create hp nodes for PCIE/PCISHPC connectors
1960*10923SEvan.Yan@Sun.COM * of the new framework, we should first match with hp nodes. If
1961*10923SEvan.Yan@Sun.COM * the ap_id refers to a PCIE/PCISHPC connector, we'll be able to
1962*10923SEvan.Yan@Sun.COM * find it here.
19630Sstevel@tonic-gate */
1964*10923SEvan.Yan@Sun.COM rnode = di_init("/", DINFOSUBTREE | DINFOHP);
19656852Scth if (rnode)
19666852Scth wnode = di_lookup_node(rnode, rpath);
19676852Scth else
19686852Scth wnode = DI_NODE_NIL;
19690Sstevel@tonic-gate
19706852Scth if (wnode == DI_NODE_NIL) {
19716852Scth if (rnode == DI_NODE_NIL) {
1972*10923SEvan.Yan@Sun.COM S_FREE(rpath);
19730Sstevel@tonic-gate config_err(errno, DI_INIT_FAILED, errstring);
19740Sstevel@tonic-gate return (CFGA_LIB_ERROR);
19756852Scth } else {
19766852Scth /*
1977*10923SEvan.Yan@Sun.COM * di_lookup_node() may fail, either because the
1978*10923SEvan.Yan@Sun.COM * ap_id does not exist, or because the ap_id refers
1979*10923SEvan.Yan@Sun.COM * to a legacy PCI slot, thus we'll not able to
1980*10923SEvan.Yan@Sun.COM * find node using DINFOHP, try to see if we can
1981*10923SEvan.Yan@Sun.COM * find one using DINFOCACHE.
19826852Scth */
19836852Scth di_fini(rnode);
1984*10923SEvan.Yan@Sun.COM goto find_minor;
19850Sstevel@tonic-gate }
19860Sstevel@tonic-gate }
19870Sstevel@tonic-gate
19880Sstevel@tonic-gate libloc_p->libp = NULL;
19890Sstevel@tonic-gate libloc_p->status = CFGA_APID_NOEXIST;
19900Sstevel@tonic-gate
1991*10923SEvan.Yan@Sun.COM (void) di_walk_hp(wnode, NULL, DI_HP_CONNECTOR,
1992*10923SEvan.Yan@Sun.COM libloc_p, fcn_hp);
19930Sstevel@tonic-gate
19940Sstevel@tonic-gate di_fini(rnode);
19950Sstevel@tonic-gate
1996*10923SEvan.Yan@Sun.COM /*
1997*10923SEvan.Yan@Sun.COM * Failed to find a matching hp node, try minor node.
1998*10923SEvan.Yan@Sun.COM */
1999*10923SEvan.Yan@Sun.COM if (libloc_p->libp == NULL) {
2000*10923SEvan.Yan@Sun.COM find_minor:
2001*10923SEvan.Yan@Sun.COM rnode = di_init("/", DINFOCACHE);
2002*10923SEvan.Yan@Sun.COM if (rnode)
2003*10923SEvan.Yan@Sun.COM wnode = di_lookup_node(rnode, rpath);
2004*10923SEvan.Yan@Sun.COM else
2005*10923SEvan.Yan@Sun.COM wnode = DI_NODE_NIL;
2006*10923SEvan.Yan@Sun.COM
2007*10923SEvan.Yan@Sun.COM if (wnode == DI_NODE_NIL) {
2008*10923SEvan.Yan@Sun.COM if (rnode == DI_NODE_NIL) {
2009*10923SEvan.Yan@Sun.COM S_FREE(rpath);
2010*10923SEvan.Yan@Sun.COM config_err(errno, DI_INIT_FAILED, errstring);
2011*10923SEvan.Yan@Sun.COM return (CFGA_LIB_ERROR);
2012*10923SEvan.Yan@Sun.COM } else {
2013*10923SEvan.Yan@Sun.COM /*
2014*10923SEvan.Yan@Sun.COM * di_lookup_node() may fail, because the
2015*10923SEvan.Yan@Sun.COM * ap_id does not exist.
2016*10923SEvan.Yan@Sun.COM */
2017*10923SEvan.Yan@Sun.COM S_FREE(rpath);
2018*10923SEvan.Yan@Sun.COM di_fini(rnode);
2019*10923SEvan.Yan@Sun.COM return (CFGA_APID_NOEXIST);
2020*10923SEvan.Yan@Sun.COM }
2021*10923SEvan.Yan@Sun.COM }
2022*10923SEvan.Yan@Sun.COM
2023*10923SEvan.Yan@Sun.COM libloc_p->libp = NULL;
2024*10923SEvan.Yan@Sun.COM libloc_p->status = CFGA_APID_NOEXIST;
2025*10923SEvan.Yan@Sun.COM
2026*10923SEvan.Yan@Sun.COM (void) di_walk_minor(wnode, "ddi_ctl:attachment_point",
2027*10923SEvan.Yan@Sun.COM DI_CHECK_ALIAS|DI_CHECK_INTERNAL_PATH,
2028*10923SEvan.Yan@Sun.COM libloc_p, fcn);
2029*10923SEvan.Yan@Sun.COM
2030*10923SEvan.Yan@Sun.COM di_fini(rnode);
2031*10923SEvan.Yan@Sun.COM }
2032*10923SEvan.Yan@Sun.COM
2033*10923SEvan.Yan@Sun.COM S_FREE(rpath);
2034*10923SEvan.Yan@Sun.COM
20350Sstevel@tonic-gate if (libloc_p->libp != NULL) {
20360Sstevel@tonic-gate update_cache(libloc_p);
20370Sstevel@tonic-gate return (CFGA_OK);
20380Sstevel@tonic-gate } else {
20390Sstevel@tonic-gate return (libloc_p->status);
20400Sstevel@tonic-gate }
20410Sstevel@tonic-gate }
20420Sstevel@tonic-gate
20430Sstevel@tonic-gate /*
2044*10923SEvan.Yan@Sun.COM * check_ap - called for each non-SHP attachment point found
2045*10923SEvan.Yan@Sun.COM */
2046*10923SEvan.Yan@Sun.COM static int
check_ap(di_node_t node,di_minor_t minor,void * arg)2047*10923SEvan.Yan@Sun.COM check_ap(
2048*10923SEvan.Yan@Sun.COM di_node_t node,
2049*10923SEvan.Yan@Sun.COM di_minor_t minor,
2050*10923SEvan.Yan@Sun.COM void *arg)
2051*10923SEvan.Yan@Sun.COM {
2052*10923SEvan.Yan@Sun.COM return (check_ap_impl(node, minor, NULL, arg));
2053*10923SEvan.Yan@Sun.COM }
2054*10923SEvan.Yan@Sun.COM
2055*10923SEvan.Yan@Sun.COM /*
2056*10923SEvan.Yan@Sun.COM * check_ap_hp - called for each SHP attachment point found
2057*10923SEvan.Yan@Sun.COM */
2058*10923SEvan.Yan@Sun.COM static int
check_ap_hp(di_node_t node,di_hp_t hp,void * arg)2059*10923SEvan.Yan@Sun.COM check_ap_hp(
2060*10923SEvan.Yan@Sun.COM di_node_t node,
2061*10923SEvan.Yan@Sun.COM di_hp_t hp,
2062*10923SEvan.Yan@Sun.COM void *arg)
2063*10923SEvan.Yan@Sun.COM {
2064*10923SEvan.Yan@Sun.COM return (check_ap_impl(node, NULL, hp, arg));
2065*10923SEvan.Yan@Sun.COM }
2066*10923SEvan.Yan@Sun.COM
2067*10923SEvan.Yan@Sun.COM /*
2068*10923SEvan.Yan@Sun.COM * check_ap_impl - called for each attachment point found
20690Sstevel@tonic-gate *
20700Sstevel@tonic-gate * This is used in cases where a particular attachment point
20710Sstevel@tonic-gate * or type of attachment point is specified via a logical name or ap_type.
20720Sstevel@tonic-gate * Not used for physical names or in the list case with no
20730Sstevel@tonic-gate * ap's specified.
20740Sstevel@tonic-gate */
20750Sstevel@tonic-gate static int
check_ap_impl(di_node_t node,di_minor_t minor,di_hp_t hp,void * arg)2076*10923SEvan.Yan@Sun.COM check_ap_impl(
20770Sstevel@tonic-gate di_node_t node,
20780Sstevel@tonic-gate di_minor_t minor,
2079*10923SEvan.Yan@Sun.COM di_hp_t hp,
20800Sstevel@tonic-gate void *arg)
20810Sstevel@tonic-gate {
20820Sstevel@tonic-gate char *cp = NULL;
20830Sstevel@tonic-gate char aptype[MAXPATHLEN];
20840Sstevel@tonic-gate char *recep_id = NULL;
20850Sstevel@tonic-gate char *node_minor;
20860Sstevel@tonic-gate char *drv_name;
20870Sstevel@tonic-gate char inst[MAXPATHLEN];
20880Sstevel@tonic-gate char inst2[MAXPATHLEN];
20890Sstevel@tonic-gate lib_loc_t *libloc_p;
20900Sstevel@tonic-gate int comparison_test;
20910Sstevel@tonic-gate int instance;
20920Sstevel@tonic-gate cfga_ap_types_t type;
20930Sstevel@tonic-gate
2094*10923SEvan.Yan@Sun.COM if (minor != DI_MINOR_NIL && hp != DI_HP_NIL)
2095*10923SEvan.Yan@Sun.COM return (DI_WALK_CONTINUE);
20960Sstevel@tonic-gate
20970Sstevel@tonic-gate libloc_p = (lib_loc_t *)arg;
20980Sstevel@tonic-gate
20990Sstevel@tonic-gate (void) snprintf(aptype, sizeof (aptype), "%s", libloc_p->ap_base);
21000Sstevel@tonic-gate
21010Sstevel@tonic-gate /*
21020Sstevel@tonic-gate * This routime handles only aptypes and driver based logical apids.
21030Sstevel@tonic-gate */
21040Sstevel@tonic-gate type = find_arg_type(aptype);
21050Sstevel@tonic-gate if (type == LOGICAL_DRV_AP) {
21060Sstevel@tonic-gate cp = strchr(aptype, ':');
21070Sstevel@tonic-gate *cp = '\0';
21080Sstevel@tonic-gate recep_id = cp+1;
21090Sstevel@tonic-gate cp--;
21100Sstevel@tonic-gate while (isdigit(*cp) && cp != aptype)
21110Sstevel@tonic-gate cp--;
21120Sstevel@tonic-gate cp++;
21130Sstevel@tonic-gate
21140Sstevel@tonic-gate (void) snprintf(inst, sizeof (inst), "%s", cp);
21150Sstevel@tonic-gate
21160Sstevel@tonic-gate *cp = '\0';
21170Sstevel@tonic-gate } else if (type != AP_TYPE) {
21180Sstevel@tonic-gate libloc_p->status = CFGA_APID_NOEXIST;
21190Sstevel@tonic-gate return (DI_WALK_CONTINUE);
21200Sstevel@tonic-gate }
21210Sstevel@tonic-gate
2122*10923SEvan.Yan@Sun.COM if (minor != DI_MINOR_NIL)
2123*10923SEvan.Yan@Sun.COM node_minor = di_minor_name(minor);
2124*10923SEvan.Yan@Sun.COM else
2125*10923SEvan.Yan@Sun.COM node_minor = di_hp_name(hp);
2126*10923SEvan.Yan@Sun.COM
21270Sstevel@tonic-gate drv_name = di_driver_name(node);
21280Sstevel@tonic-gate instance = di_instance(node);
21290Sstevel@tonic-gate
21300Sstevel@tonic-gate if (node_minor == NULL || drv_name == NULL || instance == -1) {
21310Sstevel@tonic-gate libloc_p->status = CFGA_APID_NOEXIST;
21320Sstevel@tonic-gate return (DI_WALK_CONTINUE);
21330Sstevel@tonic-gate }
21340Sstevel@tonic-gate
21350Sstevel@tonic-gate (void) sprintf(inst2, "%d", instance);
21360Sstevel@tonic-gate
21370Sstevel@tonic-gate /*
21380Sstevel@tonic-gate * If the base matches driver and instance try and find a lib for it,
21390Sstevel@tonic-gate * then load it. On any failure we continue the walk.
21400Sstevel@tonic-gate *
21410Sstevel@tonic-gate * driver based logical ap_ids are derived from driver name + instance.
21420Sstevel@tonic-gate * Ap_types are just partial driver names.
21430Sstevel@tonic-gate *
21440Sstevel@tonic-gate */
21450Sstevel@tonic-gate
21460Sstevel@tonic-gate comparison_test = 0;
21470Sstevel@tonic-gate if (type == AP_TYPE) {
21480Sstevel@tonic-gate if (strncmp(aptype, drv_name, strlen(aptype)) == 0) {
21490Sstevel@tonic-gate comparison_test = 1;
21500Sstevel@tonic-gate }
21510Sstevel@tonic-gate } else {
21520Sstevel@tonic-gate if (strcmp(aptype, drv_name) == 0 &&
21530Sstevel@tonic-gate strcmp(recep_id, node_minor) == 0 &&
21540Sstevel@tonic-gate strcmp(inst, inst2) == 0) {
21550Sstevel@tonic-gate comparison_test = 1;
21560Sstevel@tonic-gate }
21570Sstevel@tonic-gate }
21580Sstevel@tonic-gate
21590Sstevel@tonic-gate if (comparison_test) {
21600Sstevel@tonic-gate /*
21610Sstevel@tonic-gate * save the correct type of error so user does not get confused
21620Sstevel@tonic-gate */
2163*10923SEvan.Yan@Sun.COM if (minor != DI_MINOR_NIL) {
2164*10923SEvan.Yan@Sun.COM if (find_lib(node, minor, libloc_p) != CFGA_OK) {
2165*10923SEvan.Yan@Sun.COM libloc_p->status = CFGA_NO_LIB;
2166*10923SEvan.Yan@Sun.COM return (DI_WALK_CONTINUE);
2167*10923SEvan.Yan@Sun.COM }
2168*10923SEvan.Yan@Sun.COM if (load_lib(node, minor, libloc_p) != CFGA_OK) {
2169*10923SEvan.Yan@Sun.COM libloc_p->status = CFGA_LIB_ERROR;
2170*10923SEvan.Yan@Sun.COM return (DI_WALK_CONTINUE);
2171*10923SEvan.Yan@Sun.COM }
2172*10923SEvan.Yan@Sun.COM } else {
2173*10923SEvan.Yan@Sun.COM if (find_lib_hp(node, hp, libloc_p) != CFGA_OK) {
2174*10923SEvan.Yan@Sun.COM libloc_p->status = CFGA_NO_LIB;
2175*10923SEvan.Yan@Sun.COM return (DI_WALK_CONTINUE);
2176*10923SEvan.Yan@Sun.COM }
2177*10923SEvan.Yan@Sun.COM if (load_lib_hp(node, hp, libloc_p) != CFGA_OK) {
2178*10923SEvan.Yan@Sun.COM libloc_p->status = CFGA_LIB_ERROR;
2179*10923SEvan.Yan@Sun.COM return (DI_WALK_CONTINUE);
2180*10923SEvan.Yan@Sun.COM }
21810Sstevel@tonic-gate }
21820Sstevel@tonic-gate libloc_p->status = CFGA_OK;
21830Sstevel@tonic-gate return (DI_WALK_TERMINATE);
21840Sstevel@tonic-gate } else {
21850Sstevel@tonic-gate libloc_p->status = CFGA_APID_NOEXIST;
21860Sstevel@tonic-gate return (DI_WALK_CONTINUE);
21870Sstevel@tonic-gate }
21880Sstevel@tonic-gate }
21890Sstevel@tonic-gate
21900Sstevel@tonic-gate
21910Sstevel@tonic-gate /*
2192*10923SEvan.Yan@Sun.COM * check_ap_phys - called for each non-SHP attachment point found
21930Sstevel@tonic-gate */
21940Sstevel@tonic-gate static int
check_ap_phys(di_node_t node,di_minor_t minor,void * arg)21950Sstevel@tonic-gate check_ap_phys(
21960Sstevel@tonic-gate di_node_t node,
21970Sstevel@tonic-gate di_minor_t minor,
21980Sstevel@tonic-gate void *arg)
21990Sstevel@tonic-gate {
2200*10923SEvan.Yan@Sun.COM return (check_ap_phys_impl(node, minor, DI_HP_NIL, arg));
2201*10923SEvan.Yan@Sun.COM }
2202*10923SEvan.Yan@Sun.COM
2203*10923SEvan.Yan@Sun.COM /*
2204*10923SEvan.Yan@Sun.COM * check_ap_phys_hp - called for each SHP attachment point found
2205*10923SEvan.Yan@Sun.COM */
2206*10923SEvan.Yan@Sun.COM static int
check_ap_phys_hp(di_node_t node,di_hp_t hp,void * arg)2207*10923SEvan.Yan@Sun.COM check_ap_phys_hp(
2208*10923SEvan.Yan@Sun.COM di_node_t node,
2209*10923SEvan.Yan@Sun.COM di_hp_t hp,
2210*10923SEvan.Yan@Sun.COM void *arg)
2211*10923SEvan.Yan@Sun.COM {
2212*10923SEvan.Yan@Sun.COM return (check_ap_phys_impl(node, DI_HP_NIL, hp, arg));
2213*10923SEvan.Yan@Sun.COM }
2214*10923SEvan.Yan@Sun.COM
2215*10923SEvan.Yan@Sun.COM /*
2216*10923SEvan.Yan@Sun.COM * check_ap_phys_impl - called for each attachment point found
2217*10923SEvan.Yan@Sun.COM *
2218*10923SEvan.Yan@Sun.COM * This is used in cases where a particular attachment point
2219*10923SEvan.Yan@Sun.COM * is specified via a physical name. If the name matches then
2220*10923SEvan.Yan@Sun.COM * we try and find and load the library for it.
2221*10923SEvan.Yan@Sun.COM */
2222*10923SEvan.Yan@Sun.COM static int
check_ap_phys_impl(di_node_t node,di_minor_t minor,di_hp_t hp,void * arg)2223*10923SEvan.Yan@Sun.COM check_ap_phys_impl(
2224*10923SEvan.Yan@Sun.COM di_node_t node,
2225*10923SEvan.Yan@Sun.COM di_minor_t minor,
2226*10923SEvan.Yan@Sun.COM di_hp_t hp,
2227*10923SEvan.Yan@Sun.COM void *arg)
2228*10923SEvan.Yan@Sun.COM {
22290Sstevel@tonic-gate lib_loc_t *libloc_p;
22300Sstevel@tonic-gate char phys_name[MAXPATHLEN];
22310Sstevel@tonic-gate char *devfs_path;
22320Sstevel@tonic-gate char *minor_name;
22330Sstevel@tonic-gate
2234*10923SEvan.Yan@Sun.COM if (minor != DI_MINOR_NIL && hp != DI_HP_NIL)
2235*10923SEvan.Yan@Sun.COM return (DI_WALK_CONTINUE);
2236*10923SEvan.Yan@Sun.COM
22370Sstevel@tonic-gate libloc_p = (lib_loc_t *)arg;
22380Sstevel@tonic-gate devfs_path = di_devfs_path(node);
2239*10923SEvan.Yan@Sun.COM if (minor != DI_MINOR_NIL)
2240*10923SEvan.Yan@Sun.COM minor_name = di_minor_name(minor);
2241*10923SEvan.Yan@Sun.COM else
2242*10923SEvan.Yan@Sun.COM minor_name = di_hp_name(hp);
22430Sstevel@tonic-gate
22440Sstevel@tonic-gate if (devfs_path == NULL || minor_name == NULL) {
22450Sstevel@tonic-gate libloc_p->status = CFGA_APID_NOEXIST;
22460Sstevel@tonic-gate return (DI_WALK_CONTINUE);
22470Sstevel@tonic-gate }
22480Sstevel@tonic-gate
22490Sstevel@tonic-gate (void) snprintf(phys_name, sizeof (phys_name), "%s%s:%s",
22500Sstevel@tonic-gate DEVICES_DIR, devfs_path, minor_name);
22510Sstevel@tonic-gate
22520Sstevel@tonic-gate di_devfs_path_free(devfs_path);
22530Sstevel@tonic-gate
22540Sstevel@tonic-gate if (strcmp(phys_name, libloc_p->ap_base) == 0) {
2255*10923SEvan.Yan@Sun.COM if (minor != DI_MINOR_NIL) {
2256*10923SEvan.Yan@Sun.COM if (find_lib(node, minor, libloc_p) != CFGA_OK) {
2257*10923SEvan.Yan@Sun.COM libloc_p->status = CFGA_NO_LIB;
2258*10923SEvan.Yan@Sun.COM return (DI_WALK_CONTINUE);
2259*10923SEvan.Yan@Sun.COM }
2260*10923SEvan.Yan@Sun.COM if (load_lib(node, minor, libloc_p) != CFGA_OK) {
2261*10923SEvan.Yan@Sun.COM libloc_p->status = CFGA_LIB_ERROR;
2262*10923SEvan.Yan@Sun.COM return (DI_WALK_CONTINUE);
2263*10923SEvan.Yan@Sun.COM }
2264*10923SEvan.Yan@Sun.COM } else {
2265*10923SEvan.Yan@Sun.COM if (find_lib_hp(node, hp, libloc_p) != CFGA_OK) {
2266*10923SEvan.Yan@Sun.COM libloc_p->status = CFGA_NO_LIB;
2267*10923SEvan.Yan@Sun.COM return (DI_WALK_CONTINUE);
2268*10923SEvan.Yan@Sun.COM }
2269*10923SEvan.Yan@Sun.COM if (load_lib_hp(node, hp, libloc_p) != CFGA_OK) {
2270*10923SEvan.Yan@Sun.COM libloc_p->status = CFGA_LIB_ERROR;
2271*10923SEvan.Yan@Sun.COM return (DI_WALK_CONTINUE);
2272*10923SEvan.Yan@Sun.COM }
22730Sstevel@tonic-gate }
2274*10923SEvan.Yan@Sun.COM
22750Sstevel@tonic-gate libloc_p->status = CFGA_OK;
22760Sstevel@tonic-gate return (DI_WALK_TERMINATE);
22770Sstevel@tonic-gate } else {
22780Sstevel@tonic-gate libloc_p->status = CFGA_APID_NOEXIST;
22790Sstevel@tonic-gate return (DI_WALK_CONTINUE);
22800Sstevel@tonic-gate }
22810Sstevel@tonic-gate }
22820Sstevel@tonic-gate
22830Sstevel@tonic-gate /*
22840Sstevel@tonic-gate * lib_in_list
22850Sstevel@tonic-gate *
22860Sstevel@tonic-gate * See if library, as specified by the full pathname and controller
22870Sstevel@tonic-gate * instance number is already represented in the plugin library list.
22880Sstevel@tonic-gate * If the instance number is -1 it is ignored.
22890Sstevel@tonic-gate */
22900Sstevel@tonic-gate static plugin_lib_t *
lib_in_list(char * libpath)22910Sstevel@tonic-gate lib_in_list(char *libpath)
22920Sstevel@tonic-gate {
22930Sstevel@tonic-gate plugin_lib_t *libp = NULL;
22940Sstevel@tonic-gate
22950Sstevel@tonic-gate for (libp = plugin_list.next; libp != NULL; libp = libp->next) {
22960Sstevel@tonic-gate if (strncmp(libpath, libp->libpath, MAXPATHLEN) == 0) {
22970Sstevel@tonic-gate return (libp);
22980Sstevel@tonic-gate }
22990Sstevel@tonic-gate }
23000Sstevel@tonic-gate return (NULL);
23010Sstevel@tonic-gate }
23020Sstevel@tonic-gate
23030Sstevel@tonic-gate
23040Sstevel@tonic-gate
23050Sstevel@tonic-gate
23060Sstevel@tonic-gate /*
23070Sstevel@tonic-gate * Coalesce stat and list data into single array
23080Sstevel@tonic-gate */
23090Sstevel@tonic-gate static cfga_err_t
realloc_data_ext(cfga_list_data_t ** ap_id_list,int * nlistp,list_stat_t * lstatp)23100Sstevel@tonic-gate realloc_data_ext(
23110Sstevel@tonic-gate cfga_list_data_t **ap_id_list,
23120Sstevel@tonic-gate int *nlistp,
23130Sstevel@tonic-gate list_stat_t *lstatp)
23140Sstevel@tonic-gate {
23150Sstevel@tonic-gate int i, j;
23160Sstevel@tonic-gate stat_data_list_t *slp;
23170Sstevel@tonic-gate cfga_list_data_t *cldp;
23180Sstevel@tonic-gate array_list_t *alp;
23190Sstevel@tonic-gate cfga_err_t rc = CFGA_OK;
23200Sstevel@tonic-gate
23210Sstevel@tonic-gate
23220Sstevel@tonic-gate assert(*lstatp->countp >= 0);
23230Sstevel@tonic-gate
23240Sstevel@tonic-gate if (*lstatp->countp == 0) {
23250Sstevel@tonic-gate *ap_id_list = NULL;
23260Sstevel@tonic-gate *nlistp = 0;
23270Sstevel@tonic-gate return (CFGA_OK);
23280Sstevel@tonic-gate }
23290Sstevel@tonic-gate
23300Sstevel@tonic-gate /*
23310Sstevel@tonic-gate * allocate the array
23320Sstevel@tonic-gate */
23330Sstevel@tonic-gate if ((cldp = config_calloc_check(*lstatp->countp,
23340Sstevel@tonic-gate sizeof (cfga_list_data_t), lstatp->errstr)) == NULL) {
23350Sstevel@tonic-gate rc = CFGA_LIB_ERROR;
23360Sstevel@tonic-gate goto out;
23370Sstevel@tonic-gate }
23380Sstevel@tonic-gate
23390Sstevel@tonic-gate /*
23400Sstevel@tonic-gate * copy all the stat elements (if any) into the array
23410Sstevel@tonic-gate */
23420Sstevel@tonic-gate slp = lstatp->sdl;
23430Sstevel@tonic-gate for (i = 0; slp != NULL; i++) {
23440Sstevel@tonic-gate if (i >= *lstatp->countp) {
23450Sstevel@tonic-gate rc = CFGA_LIB_ERROR;
23460Sstevel@tonic-gate goto out;
23470Sstevel@tonic-gate }
23480Sstevel@tonic-gate stat_to_list(&cldp[i], &slp->stat_data);
23490Sstevel@tonic-gate slp = slp->next;
23500Sstevel@tonic-gate }
23510Sstevel@tonic-gate
23520Sstevel@tonic-gate /*
23530Sstevel@tonic-gate * copy all the list elements (if any) into the array
23540Sstevel@tonic-gate */
23550Sstevel@tonic-gate alp = lstatp->al;
23560Sstevel@tonic-gate for (; alp != NULL; ) {
23570Sstevel@tonic-gate if (i + alp->nelem > *lstatp->countp) {
23580Sstevel@tonic-gate rc = CFGA_LIB_ERROR;
23590Sstevel@tonic-gate goto out;
23600Sstevel@tonic-gate }
23610Sstevel@tonic-gate
23620Sstevel@tonic-gate for (j = 0; j < alp->nelem; i++, j++) {
23630Sstevel@tonic-gate cldp[i] = alp->array[j];
23640Sstevel@tonic-gate }
23650Sstevel@tonic-gate alp = alp->next;
23660Sstevel@tonic-gate }
23670Sstevel@tonic-gate
23680Sstevel@tonic-gate if (i != *lstatp->countp) {
23690Sstevel@tonic-gate rc = CFGA_LIB_ERROR;
23700Sstevel@tonic-gate } else {
23710Sstevel@tonic-gate rc = CFGA_OK;
23720Sstevel@tonic-gate }
23730Sstevel@tonic-gate
23740Sstevel@tonic-gate /*FALLTHRU*/
23750Sstevel@tonic-gate
23760Sstevel@tonic-gate out:
23770Sstevel@tonic-gate /* clean up */
23780Sstevel@tonic-gate lstat_free(lstatp);
23790Sstevel@tonic-gate
23800Sstevel@tonic-gate if (rc == CFGA_OK) {
23810Sstevel@tonic-gate *ap_id_list = cldp;
23820Sstevel@tonic-gate *nlistp = *lstatp->countp;
23830Sstevel@tonic-gate } else {
23840Sstevel@tonic-gate S_FREE(cldp);
23850Sstevel@tonic-gate *ap_id_list = NULL;
23860Sstevel@tonic-gate *nlistp = 0;
23870Sstevel@tonic-gate }
23880Sstevel@tonic-gate return (rc);
23890Sstevel@tonic-gate }
23900Sstevel@tonic-gate
23910Sstevel@tonic-gate /*
23920Sstevel@tonic-gate * The caller of this routine may supply a buffer through
23930Sstevel@tonic-gate * ap_id_list for returning data. Otherwise, this routine allocates the
23940Sstevel@tonic-gate * buffer.
23950Sstevel@tonic-gate */
23960Sstevel@tonic-gate static cfga_err_t
realloc_data(cfga_stat_data_t ** ap_id_list,int * nlistp,list_stat_t * lstatp)23970Sstevel@tonic-gate realloc_data(cfga_stat_data_t **ap_id_list, int *nlistp, list_stat_t *lstatp)
23980Sstevel@tonic-gate {
23990Sstevel@tonic-gate int i;
24000Sstevel@tonic-gate stat_data_list_t *slp;
24010Sstevel@tonic-gate cfga_stat_data_t *csdp, *buf;
24020Sstevel@tonic-gate cfga_err_t rc;
24030Sstevel@tonic-gate
24040Sstevel@tonic-gate
24050Sstevel@tonic-gate assert(*lstatp->countp >= 0);
24060Sstevel@tonic-gate
24070Sstevel@tonic-gate if (*lstatp->countp == 0) {
24080Sstevel@tonic-gate *nlistp = 0;
24090Sstevel@tonic-gate return (CFGA_OK);
24100Sstevel@tonic-gate }
24110Sstevel@tonic-gate
24120Sstevel@tonic-gate
24130Sstevel@tonic-gate /*
24140Sstevel@tonic-gate * allocate the array if caller does not supply one.
24150Sstevel@tonic-gate */
24160Sstevel@tonic-gate if (*ap_id_list == NULL) {
24170Sstevel@tonic-gate if ((buf = config_calloc_check(*lstatp->countp,
24180Sstevel@tonic-gate sizeof (cfga_stat_data_t), lstatp->errstr)) == NULL) {
24190Sstevel@tonic-gate rc = CFGA_LIB_ERROR;
24200Sstevel@tonic-gate goto out;
24210Sstevel@tonic-gate }
24220Sstevel@tonic-gate } else {
24230Sstevel@tonic-gate buf = *ap_id_list;
24240Sstevel@tonic-gate }
24250Sstevel@tonic-gate
24260Sstevel@tonic-gate /*
24270Sstevel@tonic-gate * copy the stat elements into the array
24280Sstevel@tonic-gate */
24290Sstevel@tonic-gate csdp = buf;
24300Sstevel@tonic-gate slp = lstatp->sdl;
24310Sstevel@tonic-gate for (i = 0; slp != NULL; i++) {
24320Sstevel@tonic-gate if (i >= *lstatp->countp) {
24330Sstevel@tonic-gate rc = CFGA_LIB_ERROR;
24340Sstevel@tonic-gate goto out;
24350Sstevel@tonic-gate }
24360Sstevel@tonic-gate *csdp++ = slp->stat_data;
24370Sstevel@tonic-gate slp = slp->next;
24380Sstevel@tonic-gate }
24390Sstevel@tonic-gate
24400Sstevel@tonic-gate rc = CFGA_OK;
24410Sstevel@tonic-gate
24420Sstevel@tonic-gate out:
24430Sstevel@tonic-gate if (rc == CFGA_OK) {
24440Sstevel@tonic-gate *nlistp = *lstatp->countp;
24450Sstevel@tonic-gate *ap_id_list = buf;
24460Sstevel@tonic-gate } else {
24470Sstevel@tonic-gate /*
24480Sstevel@tonic-gate * Free buffer only if we allocated it.
24490Sstevel@tonic-gate */
24500Sstevel@tonic-gate if (*ap_id_list == NULL) {
24510Sstevel@tonic-gate free(buf);
24520Sstevel@tonic-gate }
24530Sstevel@tonic-gate *nlistp = 0;
24540Sstevel@tonic-gate }
24550Sstevel@tonic-gate
24560Sstevel@tonic-gate assert(lstatp->al == NULL);
24570Sstevel@tonic-gate lstat_free(lstatp);
24580Sstevel@tonic-gate
24590Sstevel@tonic-gate return (rc);
24600Sstevel@tonic-gate }
24610Sstevel@tonic-gate
24620Sstevel@tonic-gate
24630Sstevel@tonic-gate /*
24640Sstevel@tonic-gate * list_common - walk the device tree and stat all attachment points.
24650Sstevel@tonic-gate */
24660Sstevel@tonic-gate static cfga_err_t
list_common(list_stat_t * lstatp,const char * class)24670Sstevel@tonic-gate list_common(list_stat_t *lstatp, const char *class)
24680Sstevel@tonic-gate {
24690Sstevel@tonic-gate di_node_t rnode;
24700Sstevel@tonic-gate char nodetype[MAXPATHLEN];
24710Sstevel@tonic-gate const char *l_class, *l_sep;
24720Sstevel@tonic-gate
24730Sstevel@tonic-gate /*
24740Sstevel@tonic-gate * May walk a subset of all attachment points in the device tree if
24750Sstevel@tonic-gate * a class is specified
24760Sstevel@tonic-gate */
24770Sstevel@tonic-gate if (class != NULL) {
24780Sstevel@tonic-gate l_sep = ":";
24790Sstevel@tonic-gate l_class = class;
24800Sstevel@tonic-gate } else {
24810Sstevel@tonic-gate l_sep = l_class = "";
24820Sstevel@tonic-gate }
24830Sstevel@tonic-gate
24840Sstevel@tonic-gate (void) snprintf(nodetype, sizeof (nodetype), "%s%s%s",
24850Sstevel@tonic-gate DDI_NT_ATTACHMENT_POINT, l_sep, l_class);
24860Sstevel@tonic-gate
2487*10923SEvan.Yan@Sun.COM /*
2488*10923SEvan.Yan@Sun.COM * Walk all hp nodes
2489*10923SEvan.Yan@Sun.COM */
2490*10923SEvan.Yan@Sun.COM if ((rnode = di_init("/", DINFOSUBTREE | DINFOHP)) == DI_NODE_NIL) {
2491*10923SEvan.Yan@Sun.COM config_err(errno, DI_INIT_FAILED, lstatp->errstr);
2492*10923SEvan.Yan@Sun.COM return (CFGA_LIB_ERROR);
2493*10923SEvan.Yan@Sun.COM }
2494*10923SEvan.Yan@Sun.COM /* No need to filter on class for now */
2495*10923SEvan.Yan@Sun.COM (void) di_walk_hp(rnode, NULL, DI_HP_CONNECTOR,
2496*10923SEvan.Yan@Sun.COM lstatp, do_list_common_hp);
2497*10923SEvan.Yan@Sun.COM
2498*10923SEvan.Yan@Sun.COM di_fini(rnode);
2499*10923SEvan.Yan@Sun.COM
2500*10923SEvan.Yan@Sun.COM /*
2501*10923SEvan.Yan@Sun.COM * Walk all minor nodes
2502*10923SEvan.Yan@Sun.COM * but exclude PCIE/PCIESHPC connectors which have been walked above.
2503*10923SEvan.Yan@Sun.COM */
2504*10923SEvan.Yan@Sun.COM if ((rnode = di_init("/", DINFOCACHE)) == DI_NODE_NIL) {
2505*10923SEvan.Yan@Sun.COM config_err(errno, DI_INIT_FAILED, lstatp->errstr);
2506*10923SEvan.Yan@Sun.COM return (CFGA_LIB_ERROR);
2507*10923SEvan.Yan@Sun.COM }
25080Sstevel@tonic-gate (void) di_walk_minor(rnode, nodetype,
25090Sstevel@tonic-gate DI_CHECK_ALIAS|DI_CHECK_INTERNAL_PATH, lstatp, do_list_common);
2510*10923SEvan.Yan@Sun.COM
25110Sstevel@tonic-gate di_fini(rnode);
25120Sstevel@tonic-gate
2513*10923SEvan.Yan@Sun.COM if (lstatp->shp_errstr != NULL) {
2514*10923SEvan.Yan@Sun.COM *(lstatp->errstr) = strdup(lstatp->shp_errstr);
2515*10923SEvan.Yan@Sun.COM free(lstatp->shp_errstr);
2516*10923SEvan.Yan@Sun.COM lstatp->shp_errstr = NULL;
2517*10923SEvan.Yan@Sun.COM }
2518*10923SEvan.Yan@Sun.COM
25190Sstevel@tonic-gate return (CFGA_OK);
25200Sstevel@tonic-gate }
25210Sstevel@tonic-gate
25220Sstevel@tonic-gate static void
config_err(int errnum,int err_type,char ** errstring)25230Sstevel@tonic-gate config_err(int errnum, int err_type, char **errstring)
25240Sstevel@tonic-gate {
25250Sstevel@tonic-gate char *p = NULL, *q = NULL;
25260Sstevel@tonic-gate char *syserr = NULL;
25270Sstevel@tonic-gate char syserr_num[20];
25280Sstevel@tonic-gate int len = 0;
25290Sstevel@tonic-gate
25300Sstevel@tonic-gate /*
25310Sstevel@tonic-gate * If errstring is null it means user in not interested in getting
25320Sstevel@tonic-gate * error status. So we don't do all the work
25330Sstevel@tonic-gate */
25340Sstevel@tonic-gate if (errstring == NULL) {
25350Sstevel@tonic-gate return;
25360Sstevel@tonic-gate }
25370Sstevel@tonic-gate
25380Sstevel@tonic-gate if (errnum != 0) {
25390Sstevel@tonic-gate syserr = strerror(errnum);
25400Sstevel@tonic-gate if (syserr == NULL) {
25410Sstevel@tonic-gate (void) sprintf(syserr_num, "errno=%d", errnum);
25420Sstevel@tonic-gate syserr = syserr_num;
25430Sstevel@tonic-gate }
25440Sstevel@tonic-gate } else
25450Sstevel@tonic-gate syserr = NULL;
25460Sstevel@tonic-gate
25470Sstevel@tonic-gate q = dgettext(TEXT_DOMAIN, err_strings[err_type]);
25480Sstevel@tonic-gate
25490Sstevel@tonic-gate len = strlen(q);
25500Sstevel@tonic-gate if (syserr != NULL) {
25510Sstevel@tonic-gate len += strlen(err_sep) + strlen(syserr);
25520Sstevel@tonic-gate }
25530Sstevel@tonic-gate
25540Sstevel@tonic-gate p = malloc(len + 1);
25550Sstevel@tonic-gate if (p == NULL) {
25560Sstevel@tonic-gate *errstring = NULL;
25570Sstevel@tonic-gate return;
25580Sstevel@tonic-gate }
25590Sstevel@tonic-gate
25600Sstevel@tonic-gate (void) strcpy(p, q);
25610Sstevel@tonic-gate if (syserr != NULL) {
25620Sstevel@tonic-gate (void) strcat(p, err_sep);
25630Sstevel@tonic-gate (void) strcat(p, syserr);
25640Sstevel@tonic-gate }
25650Sstevel@tonic-gate
25660Sstevel@tonic-gate *errstring = p;
25670Sstevel@tonic-gate }
25680Sstevel@tonic-gate
25690Sstevel@tonic-gate /*
2570*10923SEvan.Yan@Sun.COM * do_list_common - list non-SHP attachment point
25710Sstevel@tonic-gate */
25720Sstevel@tonic-gate static int
do_list_common(di_node_t node,di_minor_t minor,void * arg)25730Sstevel@tonic-gate do_list_common(
25740Sstevel@tonic-gate di_node_t node,
25750Sstevel@tonic-gate di_minor_t minor,
25760Sstevel@tonic-gate void *arg)
25770Sstevel@tonic-gate {
2578*10923SEvan.Yan@Sun.COM di_node_t rnode;
2579*10923SEvan.Yan@Sun.COM di_hp_t hp;
2580*10923SEvan.Yan@Sun.COM char *minor_name;
2581*10923SEvan.Yan@Sun.COM
2582*10923SEvan.Yan@Sun.COM minor_name = di_minor_name(minor);
2583*10923SEvan.Yan@Sun.COM
2584*10923SEvan.Yan@Sun.COM /*
2585*10923SEvan.Yan@Sun.COM * since PCIE/PCIHSHPC connectors have both hp nodes and minor nodes
2586*10923SEvan.Yan@Sun.COM * created for now, we need to specifically exclude these connectors
2587*10923SEvan.Yan@Sun.COM * during walking minor nodes.
2588*10923SEvan.Yan@Sun.COM */
2589*10923SEvan.Yan@Sun.COM if ((rnode = di_init(di_devfs_path(node), DINFOSUBTREE | DINFOHP))
2590*10923SEvan.Yan@Sun.COM == DI_NODE_NIL) {
2591*10923SEvan.Yan@Sun.COM return (DI_WALK_CONTINUE);
2592*10923SEvan.Yan@Sun.COM }
2593*10923SEvan.Yan@Sun.COM
2594*10923SEvan.Yan@Sun.COM for (hp = DI_HP_NIL; (hp = di_hp_next(rnode, hp)) != DI_HP_NIL; ) {
2595*10923SEvan.Yan@Sun.COM if (strcmp(di_hp_name(hp), minor_name) == 0) {
2596*10923SEvan.Yan@Sun.COM di_fini(rnode);
2597*10923SEvan.Yan@Sun.COM return (DI_WALK_CONTINUE);
2598*10923SEvan.Yan@Sun.COM }
2599*10923SEvan.Yan@Sun.COM }
2600*10923SEvan.Yan@Sun.COM
2601*10923SEvan.Yan@Sun.COM di_fini(rnode);
2602*10923SEvan.Yan@Sun.COM
2603*10923SEvan.Yan@Sun.COM return (do_list_common_impl(node, minor, NULL, arg));
2604*10923SEvan.Yan@Sun.COM }
2605*10923SEvan.Yan@Sun.COM
2606*10923SEvan.Yan@Sun.COM /*
2607*10923SEvan.Yan@Sun.COM * do_list_common_hp - list SHP attachment point
2608*10923SEvan.Yan@Sun.COM */
2609*10923SEvan.Yan@Sun.COM static int
do_list_common_hp(di_node_t node,di_hp_t hp,void * arg)2610*10923SEvan.Yan@Sun.COM do_list_common_hp(
2611*10923SEvan.Yan@Sun.COM di_node_t node,
2612*10923SEvan.Yan@Sun.COM di_hp_t hp,
2613*10923SEvan.Yan@Sun.COM void *arg)
2614*10923SEvan.Yan@Sun.COM {
2615*10923SEvan.Yan@Sun.COM return (do_list_common_impl(node, NULL, hp, arg));
2616*10923SEvan.Yan@Sun.COM }
2617*10923SEvan.Yan@Sun.COM
2618*10923SEvan.Yan@Sun.COM /*
2619*10923SEvan.Yan@Sun.COM * do_list_common_impl - Routine to list attachment point as part of
2620*10923SEvan.Yan@Sun.COM * a config_list opertion. Used by both v1 and v2 interfaces.
2621*10923SEvan.Yan@Sun.COM * This is somewhat similar to config_get_lib() and its helper routines
2622*10923SEvan.Yan@Sun.COM * except that the ap_ids are always physical and don't have dynamic
2623*10923SEvan.Yan@Sun.COM * components.
2624*10923SEvan.Yan@Sun.COM */
2625*10923SEvan.Yan@Sun.COM static int
do_list_common_impl(di_node_t node,di_minor_t minor,di_hp_t hp,void * arg)2626*10923SEvan.Yan@Sun.COM do_list_common_impl(
2627*10923SEvan.Yan@Sun.COM di_node_t node,
2628*10923SEvan.Yan@Sun.COM di_minor_t minor,
2629*10923SEvan.Yan@Sun.COM di_hp_t hp,
2630*10923SEvan.Yan@Sun.COM void *arg)
2631*10923SEvan.Yan@Sun.COM {
26320Sstevel@tonic-gate lib_loc_t lib_loc;
26330Sstevel@tonic-gate plugin_lib_t *libp;
26340Sstevel@tonic-gate list_stat_t *lstatp = NULL;
26350Sstevel@tonic-gate cfga_err_t ret = CFGA_ERROR;
26360Sstevel@tonic-gate
2637*10923SEvan.Yan@Sun.COM if (minor != DI_MINOR_NIL && hp != DI_HP_NIL)
2638*10923SEvan.Yan@Sun.COM return (DI_WALK_CONTINUE);
26390Sstevel@tonic-gate
26400Sstevel@tonic-gate lstatp = (list_stat_t *)arg;
26410Sstevel@tonic-gate
26420Sstevel@tonic-gate lib_loc.libp = NULL;
26430Sstevel@tonic-gate /*
26440Sstevel@tonic-gate * try and find a lib for this node
26450Sstevel@tonic-gate */
2646*10923SEvan.Yan@Sun.COM if (minor != DI_MINOR_NIL) {
2647*10923SEvan.Yan@Sun.COM ret = find_lib(node, minor, &lib_loc);
2648*10923SEvan.Yan@Sun.COM } else {
2649*10923SEvan.Yan@Sun.COM ret = find_lib_hp(node, hp, &lib_loc);
2650*10923SEvan.Yan@Sun.COM }
2651*10923SEvan.Yan@Sun.COM if (ret != CFGA_OK) {
26520Sstevel@tonic-gate return (DI_WALK_CONTINUE);
26530Sstevel@tonic-gate }
26540Sstevel@tonic-gate
26550Sstevel@tonic-gate /*
26560Sstevel@tonic-gate * Load all plugins. We will check compatibility later in this
26570Sstevel@tonic-gate * routine.
26580Sstevel@tonic-gate */
26590Sstevel@tonic-gate lib_loc.vers_req.v_min = CFGA_HSL_V1;
26600Sstevel@tonic-gate lib_loc.vers_req.v_max = CFGA_HSL_VERS;
26610Sstevel@tonic-gate
2662*10923SEvan.Yan@Sun.COM if (minor != DI_MINOR_NIL) {
2663*10923SEvan.Yan@Sun.COM ret = load_lib(node, minor, &lib_loc);
2664*10923SEvan.Yan@Sun.COM } else {
2665*10923SEvan.Yan@Sun.COM ret = load_lib_hp(node, hp, &lib_loc);
2666*10923SEvan.Yan@Sun.COM }
26670Sstevel@tonic-gate if (ret != CFGA_OK) {
26680Sstevel@tonic-gate return (DI_WALK_CONTINUE);
26690Sstevel@tonic-gate }
26700Sstevel@tonic-gate
26710Sstevel@tonic-gate libp = lib_loc.libp;
26720Sstevel@tonic-gate assert(libp != NULL);
26730Sstevel@tonic-gate
26740Sstevel@tonic-gate /*
26750Sstevel@tonic-gate * Note: For list type routines (list all attachment points in
26760Sstevel@tonic-gate * device tree) we don't pass errstring to the plugin, nor do we
26770Sstevel@tonic-gate * stop the walk if an error occurs in the plugin.
26780Sstevel@tonic-gate */
26790Sstevel@tonic-gate if (compat_plugin(&lstatp->use_vers, libp->plugin_vers)) {
2680*10923SEvan.Yan@Sun.COM if (minor != DI_MINOR_NIL) {
2681*10923SEvan.Yan@Sun.COM (void) libp->vers_ops->stat_plugin(lstatp,
2682*10923SEvan.Yan@Sun.COM &lib_loc, NULL);
2683*10923SEvan.Yan@Sun.COM } else {
2684*10923SEvan.Yan@Sun.COM /*
2685*10923SEvan.Yan@Sun.COM * If the underlying hotplug daemon is not enabled,
2686*10923SEvan.Yan@Sun.COM * the SHP attach points will not be shown, this
2687*10923SEvan.Yan@Sun.COM * could confuse the uesrs. We specifically pass the
2688*10923SEvan.Yan@Sun.COM * errstring to SHP plugin so that it can set the
2689*10923SEvan.Yan@Sun.COM * errstring accordingly in this case, giving users
2690*10923SEvan.Yan@Sun.COM * a hint.
2691*10923SEvan.Yan@Sun.COM */
2692*10923SEvan.Yan@Sun.COM ret = libp->vers_ops->stat_plugin(lstatp,
2693*10923SEvan.Yan@Sun.COM &lib_loc, lstatp->errstr);
2694*10923SEvan.Yan@Sun.COM if (ret == CFGA_NOTSUPP && *(lstatp->errstr) != NULL) {
2695*10923SEvan.Yan@Sun.COM if (lstatp->shp_errstr == NULL) {
2696*10923SEvan.Yan@Sun.COM lstatp->shp_errstr =
2697*10923SEvan.Yan@Sun.COM strdup(*(lstatp->errstr));
2698*10923SEvan.Yan@Sun.COM }
2699*10923SEvan.Yan@Sun.COM }
2700*10923SEvan.Yan@Sun.COM
2701*10923SEvan.Yan@Sun.COM if (*(lstatp->errstr) != NULL) {
2702*10923SEvan.Yan@Sun.COM free(*(lstatp->errstr));
2703*10923SEvan.Yan@Sun.COM *(lstatp->errstr) = NULL;
2704*10923SEvan.Yan@Sun.COM }
2705*10923SEvan.Yan@Sun.COM }
27060Sstevel@tonic-gate }
27070Sstevel@tonic-gate rele_lib(libp);
27080Sstevel@tonic-gate
27090Sstevel@tonic-gate return (DI_WALK_CONTINUE);
27100Sstevel@tonic-gate }
27110Sstevel@tonic-gate
27120Sstevel@tonic-gate /*
27130Sstevel@tonic-gate * stat_common - stat a user specified set of attachment points.
27140Sstevel@tonic-gate */
27150Sstevel@tonic-gate static cfga_err_t
stat_common(int num_ap_ids,char * const * ap_ids,const char * class,list_stat_t * lstatp)27160Sstevel@tonic-gate stat_common(
27170Sstevel@tonic-gate int num_ap_ids,
27180Sstevel@tonic-gate char *const *ap_ids,
27190Sstevel@tonic-gate const char *class,
27200Sstevel@tonic-gate list_stat_t *lstatp)
27210Sstevel@tonic-gate {
27220Sstevel@tonic-gate int i;
27230Sstevel@tonic-gate lib_loc_t libloc;
27240Sstevel@tonic-gate plugin_lib_t *libp;
27250Sstevel@tonic-gate cfga_err_t rc = CFGA_OK;
27260Sstevel@tonic-gate
27270Sstevel@tonic-gate
27280Sstevel@tonic-gate /*
27290Sstevel@tonic-gate * operate on each ap_id
27300Sstevel@tonic-gate */
27310Sstevel@tonic-gate for (i = 0; i < num_ap_ids; i++) {
27320Sstevel@tonic-gate libloc.libp = NULL;
27330Sstevel@tonic-gate if ((rc = config_get_lib(ap_ids[i], &libloc,
27340Sstevel@tonic-gate lstatp->errstr)) != CFGA_OK) {
27350Sstevel@tonic-gate break;
27360Sstevel@tonic-gate }
27370Sstevel@tonic-gate assert(libloc.libp != NULL);
27380Sstevel@tonic-gate libp = libloc.libp;
27390Sstevel@tonic-gate
27400Sstevel@tonic-gate /*
27410Sstevel@tonic-gate * do pre-filtering if requested
27420Sstevel@tonic-gate */
27430Sstevel@tonic-gate if (class != NULL && strcmp(libloc.ap_class, class)) {
27440Sstevel@tonic-gate rele_lib(libp);
27450Sstevel@tonic-gate continue;
27460Sstevel@tonic-gate }
27470Sstevel@tonic-gate
27480Sstevel@tonic-gate /*
27490Sstevel@tonic-gate * Unlike list type routines, while stat'ing specific
27500Sstevel@tonic-gate * attachment points we pass errstring to the plugins
27510Sstevel@tonic-gate * and halt if an error occurs in the plugin.
27520Sstevel@tonic-gate */
27530Sstevel@tonic-gate rc = libp->vers_ops->stat_plugin(lstatp, &libloc,
27540Sstevel@tonic-gate lstatp->errstr);
27550Sstevel@tonic-gate rele_lib(libp);
27560Sstevel@tonic-gate if (rc != CFGA_OK) {
27570Sstevel@tonic-gate break;
27580Sstevel@tonic-gate }
27590Sstevel@tonic-gate }
27600Sstevel@tonic-gate
27610Sstevel@tonic-gate if (rc != CFGA_OK) {
27620Sstevel@tonic-gate lstat_free(lstatp);
27630Sstevel@tonic-gate }
27640Sstevel@tonic-gate return (rc);
27650Sstevel@tonic-gate }
27660Sstevel@tonic-gate
27670Sstevel@tonic-gate /*ARGSUSED*/
27680Sstevel@tonic-gate static cfga_err_t
null_stat_plugin(list_stat_t * lstatp,lib_loc_t * libloc_p,char ** errstring)27690Sstevel@tonic-gate null_stat_plugin(list_stat_t *lstatp, lib_loc_t *libloc_p, char **errstring)
27700Sstevel@tonic-gate {
27710Sstevel@tonic-gate return (CFGA_OK);
27720Sstevel@tonic-gate }
27730Sstevel@tonic-gate
27740Sstevel@tonic-gate /*
27750Sstevel@tonic-gate * Pass errstring as a separate argument. Some higher level routines need
27760Sstevel@tonic-gate * it to be NULL.
27770Sstevel@tonic-gate */
27780Sstevel@tonic-gate static cfga_err_t
stat_plugin_v1(list_stat_t * lstatp,lib_loc_t * libloc_p,char ** errstring)27790Sstevel@tonic-gate stat_plugin_v1(list_stat_t *lstatp, lib_loc_t *libloc_p, char **errstring)
27800Sstevel@tonic-gate {
27810Sstevel@tonic-gate stat_data_list_t *slp, *slp2 = NULL;
27820Sstevel@tonic-gate cfga_err_t rc;
27830Sstevel@tonic-gate
27840Sstevel@tonic-gate /*
27850Sstevel@tonic-gate * allocate stat data buffer and list element
27860Sstevel@tonic-gate */
27870Sstevel@tonic-gate if ((slp = config_calloc_check(1, sizeof (stat_data_list_t),
27880Sstevel@tonic-gate errstring)) == NULL) {
27890Sstevel@tonic-gate return (CFGA_LIB_ERROR);
27900Sstevel@tonic-gate }
27910Sstevel@tonic-gate
27920Sstevel@tonic-gate /*
27930Sstevel@tonic-gate * Do the stat
27940Sstevel@tonic-gate */
27950Sstevel@tonic-gate errno = 0;
27960Sstevel@tonic-gate if ((rc = (*(libloc_p->libp->cfga_stat_p))(libloc_p->ap_physical,
27970Sstevel@tonic-gate &slp->stat_data, lstatp->opts, errstring)) != CFGA_OK) {
27980Sstevel@tonic-gate S_FREE(slp);
27990Sstevel@tonic-gate return (rc);
28000Sstevel@tonic-gate }
28010Sstevel@tonic-gate slp->next = NULL;
28020Sstevel@tonic-gate
28030Sstevel@tonic-gate /*
28040Sstevel@tonic-gate * Set up the logical and physical id's.
28050Sstevel@tonic-gate * For v1 interfaces, the generic library (libcfgadm) creates the
28060Sstevel@tonic-gate * ap_ids. mklog() is assumed to have been called in
28070Sstevel@tonic-gate * the caller of this routine.
28080Sstevel@tonic-gate */
28090Sstevel@tonic-gate (void) snprintf(slp->stat_data.ap_log_id, CFGA_AP_LOG_ID_LEN, "%s",
28100Sstevel@tonic-gate libloc_p->ap_logical);
28110Sstevel@tonic-gate
28120Sstevel@tonic-gate (void) snprintf(slp->stat_data.ap_phys_id, CFGA_AP_PHYS_ID_LEN, "%s",
28130Sstevel@tonic-gate libloc_p->ap_physical);
28140Sstevel@tonic-gate
28150Sstevel@tonic-gate /*
28160Sstevel@tonic-gate * link it in
28170Sstevel@tonic-gate */
28180Sstevel@tonic-gate if ((slp2 = lstatp->sdl) == NULL) {
28190Sstevel@tonic-gate lstatp->sdl = slp;
28200Sstevel@tonic-gate } else {
28210Sstevel@tonic-gate while (slp2->next != NULL)
28220Sstevel@tonic-gate slp2 = slp2->next;
28230Sstevel@tonic-gate slp2->next = slp;
28240Sstevel@tonic-gate }
28250Sstevel@tonic-gate
28260Sstevel@tonic-gate /* keep count */
28270Sstevel@tonic-gate (*lstatp->countp)++;
28280Sstevel@tonic-gate
28290Sstevel@tonic-gate return (CFGA_OK);
28300Sstevel@tonic-gate }
28310Sstevel@tonic-gate
28320Sstevel@tonic-gate static cfga_err_t
stat_plugin_v2(list_stat_t * lstatp,lib_loc_t * libloc_p,char ** errstring)28330Sstevel@tonic-gate stat_plugin_v2(list_stat_t *lstatp, lib_loc_t *libloc_p, char **errstring)
28340Sstevel@tonic-gate {
28350Sstevel@tonic-gate int i;
28360Sstevel@tonic-gate array_list_t *alp, *alp2 = NULL;
28370Sstevel@tonic-gate cfga_err_t rc;
28380Sstevel@tonic-gate char *class;
28390Sstevel@tonic-gate
28400Sstevel@tonic-gate /*
28410Sstevel@tonic-gate * allocate array list
28420Sstevel@tonic-gate */
28430Sstevel@tonic-gate if ((alp = config_calloc_check(1, sizeof (array_list_t),
28440Sstevel@tonic-gate errstring)) == NULL) {
28450Sstevel@tonic-gate return (CFGA_LIB_ERROR);
28460Sstevel@tonic-gate }
28470Sstevel@tonic-gate
28480Sstevel@tonic-gate alp->array = NULL;
28490Sstevel@tonic-gate alp->nelem = 0;
28500Sstevel@tonic-gate
28510Sstevel@tonic-gate /*
28520Sstevel@tonic-gate * The listopts argument is currently unused. Use NULL
28530Sstevel@tonic-gate */
28540Sstevel@tonic-gate errno = 0;
28550Sstevel@tonic-gate if ((rc = (*(libloc_p->libp->cfga_list_ext_p))(
28560Sstevel@tonic-gate libloc_p->ap_physical, &alp->array, &alp->nelem, lstatp->opts, NULL,
28570Sstevel@tonic-gate errstring, lstatp->flags)) != CFGA_OK || alp->nelem <= 0) {
28580Sstevel@tonic-gate S_FREE(alp);
28590Sstevel@tonic-gate return (rc);
28600Sstevel@tonic-gate }
28610Sstevel@tonic-gate alp->next = NULL;
28620Sstevel@tonic-gate
28630Sstevel@tonic-gate /*
28640Sstevel@tonic-gate * Set up the logical and physical id's if necessary.
28650Sstevel@tonic-gate * For v2 interfaces, the generic library (libcfgadm) creates the
28660Sstevel@tonic-gate * ap_ids only if there are no dynamic attachment points and the
28670Sstevel@tonic-gate * plug-in does not create the name itself. mklog() is
28680Sstevel@tonic-gate * assumed to have been called in the caller of this routine.
28690Sstevel@tonic-gate */
28700Sstevel@tonic-gate if (alp->nelem == 1) {
28710Sstevel@tonic-gate char cphys, clog;
28720Sstevel@tonic-gate
28730Sstevel@tonic-gate clog = (alp->array[0]).ap_log_id[0];
28740Sstevel@tonic-gate cphys = (alp->array[0]).ap_phys_id[0];
28750Sstevel@tonic-gate
28760Sstevel@tonic-gate if (clog == '\0') {
28770Sstevel@tonic-gate (void) snprintf((alp->array[0]).ap_log_id,
28780Sstevel@tonic-gate sizeof ((alp->array[0]).ap_log_id), "%s",
28790Sstevel@tonic-gate libloc_p->ap_logical);
28800Sstevel@tonic-gate }
28810Sstevel@tonic-gate
28820Sstevel@tonic-gate if (cphys == '\0') {
28830Sstevel@tonic-gate (void) snprintf((alp->array[0]).ap_phys_id,
28840Sstevel@tonic-gate sizeof ((alp->array[0]).ap_phys_id), "%s",
28850Sstevel@tonic-gate libloc_p->ap_physical);
28860Sstevel@tonic-gate }
28870Sstevel@tonic-gate }
28880Sstevel@tonic-gate
28890Sstevel@tonic-gate if (libloc_p->ap_class[0] == '\0') {
28900Sstevel@tonic-gate class = CFGA_NO_CLASS;
28910Sstevel@tonic-gate } else {
28920Sstevel@tonic-gate class = libloc_p->ap_class;
28930Sstevel@tonic-gate }
28940Sstevel@tonic-gate
28950Sstevel@tonic-gate /* Fill in the class information for all list elements */
28960Sstevel@tonic-gate for (i = 0; i < alp->nelem; i++) {
28970Sstevel@tonic-gate (void) snprintf((alp->array[i]).ap_class,
28980Sstevel@tonic-gate sizeof ((alp->array[i]).ap_class), "%s", class);
28990Sstevel@tonic-gate }
29000Sstevel@tonic-gate
29010Sstevel@tonic-gate /*
29020Sstevel@tonic-gate * link it in
29030Sstevel@tonic-gate */
29040Sstevel@tonic-gate if ((alp2 = lstatp->al) == NULL) {
29050Sstevel@tonic-gate lstatp->al = alp;
29060Sstevel@tonic-gate } else {
29070Sstevel@tonic-gate while (alp2->next != NULL)
29080Sstevel@tonic-gate alp2 = alp2->next;
29090Sstevel@tonic-gate alp2->next = alp;
29100Sstevel@tonic-gate }
29110Sstevel@tonic-gate
29120Sstevel@tonic-gate /* keep count */
29130Sstevel@tonic-gate (*lstatp->countp) += alp->nelem;
29140Sstevel@tonic-gate
29150Sstevel@tonic-gate return (CFGA_OK);
29160Sstevel@tonic-gate }
29170Sstevel@tonic-gate
29180Sstevel@tonic-gate /*
29190Sstevel@tonic-gate * Check if a plugin version is within requested limits.
29200Sstevel@tonic-gate */
29210Sstevel@tonic-gate static int
compat_plugin(vers_req_t * reqp,int plugin_vers)29220Sstevel@tonic-gate compat_plugin(vers_req_t *reqp, int plugin_vers)
29230Sstevel@tonic-gate {
29240Sstevel@tonic-gate
29250Sstevel@tonic-gate if (!VALID_HSL_VERS(reqp->v_min) || !VALID_HSL_VERS(reqp->v_max) ||
29260Sstevel@tonic-gate !VALID_HSL_VERS(plugin_vers)) {
29270Sstevel@tonic-gate return (0);
29280Sstevel@tonic-gate }
29290Sstevel@tonic-gate
29300Sstevel@tonic-gate if (plugin_vers < reqp->v_min || plugin_vers > reqp->v_max) {
29310Sstevel@tonic-gate return (0);
29320Sstevel@tonic-gate }
29330Sstevel@tonic-gate
29340Sstevel@tonic-gate
29350Sstevel@tonic-gate return (1);
29360Sstevel@tonic-gate }
29370Sstevel@tonic-gate
29380Sstevel@tonic-gate /*
29390Sstevel@tonic-gate * find_arg_type - determine if an argument is an ap_id or an ap_type.
29400Sstevel@tonic-gate * Adapted from cfgadm.c
29410Sstevel@tonic-gate */
29420Sstevel@tonic-gate static cfga_ap_types_t
find_arg_type(const char * ap_id)29430Sstevel@tonic-gate find_arg_type(const char *ap_id)
29440Sstevel@tonic-gate {
29450Sstevel@tonic-gate struct stat sbuf;
29460Sstevel@tonic-gate cfga_ap_types_t type = UNKNOWN_AP;
29470Sstevel@tonic-gate char *mkr = NULL;
29480Sstevel@tonic-gate size_t len;
29490Sstevel@tonic-gate int size_ap = 0, size_mkr = 0, digit = 0, i = 0;
29500Sstevel@tonic-gate char *cp, path[MAXPATHLEN], ap_base[MAXPATHLEN];
29510Sstevel@tonic-gate
29520Sstevel@tonic-gate
29530Sstevel@tonic-gate /*
29540Sstevel@tonic-gate * sanity checks
29550Sstevel@tonic-gate */
29560Sstevel@tonic-gate if (ap_id == NULL || *ap_id == '\0') {
29570Sstevel@tonic-gate
29580Sstevel@tonic-gate return (UNKNOWN_AP);
29590Sstevel@tonic-gate }
29600Sstevel@tonic-gate
29610Sstevel@tonic-gate /*
29620Sstevel@tonic-gate * Extract the base component
29630Sstevel@tonic-gate */
29640Sstevel@tonic-gate if ((cp = GET_DYN(ap_id)) != NULL) {
29650Sstevel@tonic-gate len = cp - ap_id;
29660Sstevel@tonic-gate } else {
29670Sstevel@tonic-gate len = strlen(ap_id);
29680Sstevel@tonic-gate }
29690Sstevel@tonic-gate
29700Sstevel@tonic-gate if (len >= sizeof (ap_base)) {
29710Sstevel@tonic-gate return (UNKNOWN_AP);
29720Sstevel@tonic-gate }
29730Sstevel@tonic-gate
29740Sstevel@tonic-gate /* Copy only the first "len" chars */
29750Sstevel@tonic-gate (void) strncpy(ap_base, ap_id, len);
29760Sstevel@tonic-gate ap_base[len] = '\0';
29770Sstevel@tonic-gate
29780Sstevel@tonic-gate /*
29790Sstevel@tonic-gate * If it starts with a slash and is stat-able its a physical.
29800Sstevel@tonic-gate */
29810Sstevel@tonic-gate if (*ap_base == '/' && stat(ap_base, &sbuf) == 0) {
29820Sstevel@tonic-gate return (PHYSICAL_AP);
29830Sstevel@tonic-gate }
29840Sstevel@tonic-gate
29850Sstevel@tonic-gate /*
29860Sstevel@tonic-gate * Is this a symlink in CFGA_DEV_DIR ?
29870Sstevel@tonic-gate */
29880Sstevel@tonic-gate (void) snprintf(path, sizeof (path), "%s%s",
29890Sstevel@tonic-gate CFGA_DEV_DIR SLASH, ap_base);
29900Sstevel@tonic-gate
29910Sstevel@tonic-gate if (lstat(path, &sbuf) == 0 && S_ISLNK(sbuf.st_mode) &&
29920Sstevel@tonic-gate stat(path, &sbuf) == 0) {
29930Sstevel@tonic-gate return (LOGICAL_LINK_AP);
29940Sstevel@tonic-gate }
29950Sstevel@tonic-gate
29960Sstevel@tonic-gate /*
29970Sstevel@tonic-gate * Check for ":" which is always present in an ap_id
29980Sstevel@tonic-gate * but not in an ap_type.
29990Sstevel@tonic-gate * we need to check that the characters right before the : are digits
30000Sstevel@tonic-gate * since an ap_id is of the form <name><instance>:<specific ap name>
30010Sstevel@tonic-gate */
30020Sstevel@tonic-gate if ((mkr = strchr(ap_base, ':')) == NULL) {
30030Sstevel@tonic-gate type = AP_TYPE;
30040Sstevel@tonic-gate } else {
30050Sstevel@tonic-gate size_ap = strlen(ap_base);
30060Sstevel@tonic-gate size_mkr = strlen(mkr);
30070Sstevel@tonic-gate mkr = ap_base;
30080Sstevel@tonic-gate
30090Sstevel@tonic-gate digit = 0;
30100Sstevel@tonic-gate for (i = size_ap - size_mkr - 1; i > 0; i--) {
30110Sstevel@tonic-gate if ((int)isdigit(mkr[i])) {
30120Sstevel@tonic-gate digit++;
30130Sstevel@tonic-gate break;
30140Sstevel@tonic-gate }
30150Sstevel@tonic-gate }
30160Sstevel@tonic-gate if (digit == 0) {
30170Sstevel@tonic-gate type = AP_TYPE;
30180Sstevel@tonic-gate } else {
30190Sstevel@tonic-gate type = LOGICAL_DRV_AP;
30200Sstevel@tonic-gate }
30210Sstevel@tonic-gate }
30220Sstevel@tonic-gate
30230Sstevel@tonic-gate return (type);
30240Sstevel@tonic-gate }
30250Sstevel@tonic-gate
30260Sstevel@tonic-gate /*ARGSUSED*/
30270Sstevel@tonic-gate static cfga_err_t
null_get_cond(lib_loc_t * liblocp,cfga_cond_t * condp,char ** errstring)30280Sstevel@tonic-gate null_get_cond(lib_loc_t *liblocp, cfga_cond_t *condp, char **errstring)
30290Sstevel@tonic-gate {
30300Sstevel@tonic-gate return (CFGA_OK);
30310Sstevel@tonic-gate }
30320Sstevel@tonic-gate
30330Sstevel@tonic-gate static cfga_err_t
get_cond_v1(lib_loc_t * liblocp,cfga_cond_t * condp,char ** errstring)30340Sstevel@tonic-gate get_cond_v1(lib_loc_t *liblocp, cfga_cond_t *condp, char **errstring)
30350Sstevel@tonic-gate {
30360Sstevel@tonic-gate plugin_lib_t *libp;
30370Sstevel@tonic-gate cfga_stat_data_t sdbuf;
30380Sstevel@tonic-gate cfga_err_t rc;
30390Sstevel@tonic-gate
30400Sstevel@tonic-gate
30410Sstevel@tonic-gate libp = liblocp->libp;
30420Sstevel@tonic-gate if (libp->plugin_vers != CFGA_HSL_V1) {
30430Sstevel@tonic-gate return (CFGA_LIB_ERROR);
30440Sstevel@tonic-gate }
30450Sstevel@tonic-gate
30460Sstevel@tonic-gate errno = 0;
30470Sstevel@tonic-gate if ((rc = (*liblocp->libp->cfga_stat_p)(
30480Sstevel@tonic-gate liblocp->ap_physical, &sdbuf, NULL, errstring))
30490Sstevel@tonic-gate == CFGA_OK) {
30500Sstevel@tonic-gate *condp = sdbuf.ap_cond;
30510Sstevel@tonic-gate } else {
30520Sstevel@tonic-gate *condp = CFGA_COND_UNKNOWN;
30530Sstevel@tonic-gate }
30540Sstevel@tonic-gate
30550Sstevel@tonic-gate return (rc);
30560Sstevel@tonic-gate }
30570Sstevel@tonic-gate
30580Sstevel@tonic-gate static cfga_err_t
get_cond_v2(lib_loc_t * liblocp,cfga_cond_t * condp,char ** errstring)30590Sstevel@tonic-gate get_cond_v2(lib_loc_t *liblocp, cfga_cond_t *condp, char **errstring)
30600Sstevel@tonic-gate {
30610Sstevel@tonic-gate int nelem;
30620Sstevel@tonic-gate plugin_lib_t *libp;
30630Sstevel@tonic-gate cfga_list_data_t *ldbufp;
30640Sstevel@tonic-gate cfga_err_t rc;
30650Sstevel@tonic-gate
30660Sstevel@tonic-gate
30670Sstevel@tonic-gate libp = liblocp->libp;
30680Sstevel@tonic-gate if (libp->plugin_vers != CFGA_HSL_V2) {
30690Sstevel@tonic-gate return (CFGA_LIB_ERROR);
30700Sstevel@tonic-gate }
30710Sstevel@tonic-gate
30720Sstevel@tonic-gate errno = 0;
30730Sstevel@tonic-gate nelem = 0;
30740Sstevel@tonic-gate ldbufp = NULL;
30750Sstevel@tonic-gate if ((rc = (*liblocp->libp->cfga_list_ext_p)(
30760Sstevel@tonic-gate liblocp->ap_physical, &ldbufp, &nelem, NULL, NULL,
30770Sstevel@tonic-gate errstring, 0)) == CFGA_OK) {
30780Sstevel@tonic-gate assert(nelem == 1 && ldbufp != NULL);
30790Sstevel@tonic-gate
30800Sstevel@tonic-gate *condp = ldbufp->ap_cond;
30810Sstevel@tonic-gate S_FREE(ldbufp);
30820Sstevel@tonic-gate } else {
30830Sstevel@tonic-gate *condp = CFGA_COND_UNKNOWN;
30840Sstevel@tonic-gate }
30850Sstevel@tonic-gate
30860Sstevel@tonic-gate return (rc);
30870Sstevel@tonic-gate }
30880Sstevel@tonic-gate
30890Sstevel@tonic-gate /* mask represents the flags accepted */
30900Sstevel@tonic-gate static cfga_err_t
check_flags(cfga_flags_t flags,cfga_flags_t mask,char ** errstring)30910Sstevel@tonic-gate check_flags(cfga_flags_t flags, cfga_flags_t mask, char **errstring)
30920Sstevel@tonic-gate {
30930Sstevel@tonic-gate if ((flags & ~mask) != 0) {
30940Sstevel@tonic-gate config_err(0, INVALID_ARGS, errstring);
30950Sstevel@tonic-gate return (CFGA_ERROR);
30960Sstevel@tonic-gate } else {
30970Sstevel@tonic-gate return (CFGA_OK);
30980Sstevel@tonic-gate }
30990Sstevel@tonic-gate }
31000Sstevel@tonic-gate
31010Sstevel@tonic-gate static cfga_err_t
check_apids(int num_ap_ids,char * const * ap_ids,char ** errstring)31020Sstevel@tonic-gate check_apids(int num_ap_ids, char *const *ap_ids, char **errstring)
31030Sstevel@tonic-gate {
31040Sstevel@tonic-gate if (num_ap_ids <= 0 || ap_ids == NULL) {
31050Sstevel@tonic-gate config_err(0, INVALID_ARGS, errstring);
31060Sstevel@tonic-gate return (CFGA_ERROR);
31070Sstevel@tonic-gate } else {
31080Sstevel@tonic-gate return (CFGA_OK);
31090Sstevel@tonic-gate }
31100Sstevel@tonic-gate }
31110Sstevel@tonic-gate
31120Sstevel@tonic-gate /*
31130Sstevel@tonic-gate * Returns the class or the empty string if attacment point has
31140Sstevel@tonic-gate * no class.
31150Sstevel@tonic-gate */
31160Sstevel@tonic-gate static char *
get_class(di_minor_t minor)31170Sstevel@tonic-gate get_class(di_minor_t minor)
31180Sstevel@tonic-gate {
31190Sstevel@tonic-gate char *cp, c;
31200Sstevel@tonic-gate size_t len;
31210Sstevel@tonic-gate
31220Sstevel@tonic-gate
31230Sstevel@tonic-gate if (minor == DI_MINOR_NIL) {
31240Sstevel@tonic-gate return (NULL);
31250Sstevel@tonic-gate }
31260Sstevel@tonic-gate
31270Sstevel@tonic-gate cp = di_minor_nodetype(minor);
31280Sstevel@tonic-gate if (cp == NULL) {
31290Sstevel@tonic-gate return (NULL);
31300Sstevel@tonic-gate }
31310Sstevel@tonic-gate
31320Sstevel@tonic-gate len = strlen(DDI_NT_ATTACHMENT_POINT);
31330Sstevel@tonic-gate if (strncmp(cp, DDI_NT_ATTACHMENT_POINT, len)) {
31340Sstevel@tonic-gate return (NULL);
31350Sstevel@tonic-gate }
31360Sstevel@tonic-gate
31370Sstevel@tonic-gate cp += len;
31380Sstevel@tonic-gate
31390Sstevel@tonic-gate c = *cp;
31400Sstevel@tonic-gate if (c != '\0' && c != ':') {
31410Sstevel@tonic-gate return (NULL);
31420Sstevel@tonic-gate }
31430Sstevel@tonic-gate
31440Sstevel@tonic-gate if (c == ':') {
31450Sstevel@tonic-gate cp++;
31460Sstevel@tonic-gate }
31470Sstevel@tonic-gate
31480Sstevel@tonic-gate return (cp);
31490Sstevel@tonic-gate
31500Sstevel@tonic-gate }
31510Sstevel@tonic-gate
31520Sstevel@tonic-gate /*
31530Sstevel@tonic-gate * Transform stat data to list data
31540Sstevel@tonic-gate */
31550Sstevel@tonic-gate static void
stat_to_list(cfga_list_data_t * lp,cfga_stat_data_t * statp)31560Sstevel@tonic-gate stat_to_list(cfga_list_data_t *lp, cfga_stat_data_t *statp)
31570Sstevel@tonic-gate {
31580Sstevel@tonic-gate
31590Sstevel@tonic-gate (void) snprintf(lp->ap_log_id, sizeof (lp->ap_log_id), "%s",
31600Sstevel@tonic-gate statp->ap_log_id);
31610Sstevel@tonic-gate
31620Sstevel@tonic-gate (void) snprintf(lp->ap_phys_id, sizeof (lp->ap_phys_id), "%s",
31630Sstevel@tonic-gate statp->ap_phys_id);
31640Sstevel@tonic-gate
31650Sstevel@tonic-gate (void) snprintf(lp->ap_class, sizeof (lp->ap_class), "%s",
31660Sstevel@tonic-gate CFGA_NO_CLASS);
31670Sstevel@tonic-gate
31680Sstevel@tonic-gate lp->ap_r_state = statp->ap_r_state;
31690Sstevel@tonic-gate lp->ap_o_state = statp->ap_o_state;
31700Sstevel@tonic-gate lp->ap_cond = statp->ap_cond;
31710Sstevel@tonic-gate lp->ap_busy = statp->ap_busy;
31720Sstevel@tonic-gate lp->ap_status_time = statp->ap_status_time;
31730Sstevel@tonic-gate
31740Sstevel@tonic-gate (void) snprintf(lp->ap_info, sizeof (lp->ap_info), "%s",
31750Sstevel@tonic-gate statp->ap_info);
31760Sstevel@tonic-gate (void) snprintf(lp->ap_type, sizeof (lp->ap_type), "%s",
31770Sstevel@tonic-gate statp->ap_type);
31780Sstevel@tonic-gate }
31790Sstevel@tonic-gate
31800Sstevel@tonic-gate static void
lstat_free(list_stat_t * lstatp)31810Sstevel@tonic-gate lstat_free(list_stat_t *lstatp)
31820Sstevel@tonic-gate {
31830Sstevel@tonic-gate stat_data_list_t *slp, *slp2;
31840Sstevel@tonic-gate array_list_t *ap, *ap2;
31850Sstevel@tonic-gate
31860Sstevel@tonic-gate slp = lstatp->sdl;
31870Sstevel@tonic-gate while (slp != NULL) {
31880Sstevel@tonic-gate slp2 = slp->next;
31890Sstevel@tonic-gate S_FREE(slp);
31900Sstevel@tonic-gate slp = slp2;
31910Sstevel@tonic-gate }
31920Sstevel@tonic-gate
31930Sstevel@tonic-gate lstatp->sdl = NULL;
31940Sstevel@tonic-gate
31950Sstevel@tonic-gate ap = lstatp->al;
31960Sstevel@tonic-gate while (ap != NULL) {
31970Sstevel@tonic-gate ap2 = ap->next;
31980Sstevel@tonic-gate S_FREE(ap->array);
31990Sstevel@tonic-gate S_FREE(ap);
32000Sstevel@tonic-gate ap = ap2;
32010Sstevel@tonic-gate }
32020Sstevel@tonic-gate
32030Sstevel@tonic-gate lstatp->al = NULL;
32040Sstevel@tonic-gate }
32050Sstevel@tonic-gate
32060Sstevel@tonic-gate static cfga_err_t
split_apid(char * ap_id,char ** dyncompp,char ** errstring)32070Sstevel@tonic-gate split_apid(char *ap_id, char **dyncompp, char **errstring)
32080Sstevel@tonic-gate {
32090Sstevel@tonic-gate char *cp;
32100Sstevel@tonic-gate
32110Sstevel@tonic-gate *dyncompp = NULL;
32120Sstevel@tonic-gate
32130Sstevel@tonic-gate if (ap_id == NULL) {
32140Sstevel@tonic-gate return (CFGA_ERROR);
32150Sstevel@tonic-gate }
32160Sstevel@tonic-gate
32170Sstevel@tonic-gate if ((cp = strstr(ap_id, CFGA_DYN_SEP)) == NULL) {
32180Sstevel@tonic-gate return (CFGA_OK);
32190Sstevel@tonic-gate }
32200Sstevel@tonic-gate
32210Sstevel@tonic-gate *cp = '\0';
32220Sstevel@tonic-gate cp += strlen(CFGA_DYN_SEP);
32230Sstevel@tonic-gate if ((*dyncompp = config_calloc_check(1, strlen(cp) + 1,
32240Sstevel@tonic-gate errstring)) == NULL) {
32250Sstevel@tonic-gate return (CFGA_LIB_ERROR);
32260Sstevel@tonic-gate }
32270Sstevel@tonic-gate (void) strcpy(*dyncompp, cp);
32280Sstevel@tonic-gate
32290Sstevel@tonic-gate return (CFGA_OK);
32300Sstevel@tonic-gate }
32310Sstevel@tonic-gate
32320Sstevel@tonic-gate static void
append_dyn(char * buf,const char * dyncomp,size_t blen)32330Sstevel@tonic-gate append_dyn(char *buf, const char *dyncomp, size_t blen)
32340Sstevel@tonic-gate {
32350Sstevel@tonic-gate if (dyncomp != NULL) {
32360Sstevel@tonic-gate char *cp = buf + strlen(buf);
32370Sstevel@tonic-gate size_t len = blen - strlen(buf);
32380Sstevel@tonic-gate
32390Sstevel@tonic-gate (void) snprintf(cp, len, "%s%s", CFGA_DYN_SEP,
32400Sstevel@tonic-gate dyncomp);
32410Sstevel@tonic-gate }
32420Sstevel@tonic-gate }
32430Sstevel@tonic-gate
32440Sstevel@tonic-gate /*
32450Sstevel@tonic-gate * Default implementation of cfga_ap_id_cmp. Works for most cases
32460Sstevel@tonic-gate * except for long hex number sequences like world-wide-name.
32470Sstevel@tonic-gate *
32480Sstevel@tonic-gate * This function compares the ap's in a generic way. It does so by
32490Sstevel@tonic-gate * determining the place of difference between the 2 aps. If the first
32500Sstevel@tonic-gate * difference is a digit, it attempts to obtain the numbers and compare them
32510Sstevel@tonic-gate * Otherwise it just compares the aps as strings
32520Sstevel@tonic-gate */
32530Sstevel@tonic-gate static int
default_ap_id_cmp(const char * ap_id1,const char * ap_id2)32540Sstevel@tonic-gate default_ap_id_cmp(const char *ap_id1, const char *ap_id2)
32550Sstevel@tonic-gate {
32560Sstevel@tonic-gate int i = 0;
32570Sstevel@tonic-gate
32580Sstevel@tonic-gate /*
32590Sstevel@tonic-gate * Search for first different char
32600Sstevel@tonic-gate */
32610Sstevel@tonic-gate while (ap_id1[i] == ap_id2[i] && ap_id1[i] != '\0')
32620Sstevel@tonic-gate i++;
32630Sstevel@tonic-gate
32640Sstevel@tonic-gate /*
32650Sstevel@tonic-gate * If one of the char is a digit, back up to where the
32660Sstevel@tonic-gate * number started, compare the number.
32670Sstevel@tonic-gate */
32680Sstevel@tonic-gate if (isdigit(ap_id1[i]) || isdigit(ap_id2[i])) {
32690Sstevel@tonic-gate while ((i > 0) && isdigit(ap_id1[i - 1]))
32700Sstevel@tonic-gate i--;
32710Sstevel@tonic-gate
32720Sstevel@tonic-gate if (isdigit(ap_id1[i]) && isdigit(ap_id2[i]))
32730Sstevel@tonic-gate return (atoi(ap_id1 + i) - atoi(ap_id2 + i));
32740Sstevel@tonic-gate }
32750Sstevel@tonic-gate
32760Sstevel@tonic-gate /* One of them isn't a number, compare the char */
32770Sstevel@tonic-gate return (ap_id1[i] - ap_id2[i]);
32780Sstevel@tonic-gate }
32790Sstevel@tonic-gate
32800Sstevel@tonic-gate static void
hold_lib(plugin_lib_t * libp)32810Sstevel@tonic-gate hold_lib(plugin_lib_t *libp)
32820Sstevel@tonic-gate {
32830Sstevel@tonic-gate assert(libp->refcnt >= 0);
32840Sstevel@tonic-gate (void) mutex_lock(&libp->lock);
32850Sstevel@tonic-gate libp->refcnt++;
32860Sstevel@tonic-gate (void) mutex_unlock(&libp->lock);
32870Sstevel@tonic-gate }
32880Sstevel@tonic-gate
32890Sstevel@tonic-gate static void
rele_lib(plugin_lib_t * libp)32900Sstevel@tonic-gate rele_lib(plugin_lib_t *libp)
32910Sstevel@tonic-gate {
32920Sstevel@tonic-gate assert(libp->refcnt > 0);
32930Sstevel@tonic-gate (void) mutex_lock(&libp->lock);
32940Sstevel@tonic-gate libp->refcnt--;
32950Sstevel@tonic-gate (void) mutex_unlock(&libp->lock);
32960Sstevel@tonic-gate }
3297