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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*133Sdmick 
23*133Sdmick /* Portions Copyright 2005 Cyril Plisko */
24*133Sdmick 
250Sstevel@tonic-gate /*
26*133Sdmick  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
270Sstevel@tonic-gate  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <errno.h>
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <string.h>
360Sstevel@tonic-gate #include <locale.h>
370Sstevel@tonic-gate #include <langinfo.h>
380Sstevel@tonic-gate #include <time.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #if	!defined(DEBUG)
410Sstevel@tonic-gate #define	NDEBUG	1
420Sstevel@tonic-gate #else
430Sstevel@tonic-gate #undef	NDEBUG
440Sstevel@tonic-gate #endif
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #include <assert.h>
470Sstevel@tonic-gate #include <sys/types.h>
480Sstevel@tonic-gate #include <sys/stat.h>
490Sstevel@tonic-gate #include <sys/param.h>
500Sstevel@tonic-gate #include <dlfcn.h>
510Sstevel@tonic-gate #include <synch.h>
520Sstevel@tonic-gate #include <sys/systeminfo.h>
530Sstevel@tonic-gate #include <sys/sunddi.h>
540Sstevel@tonic-gate #include <libdevinfo.h>
550Sstevel@tonic-gate #include <unistd.h>
560Sstevel@tonic-gate #include <stdarg.h>
570Sstevel@tonic-gate #include <limits.h>
580Sstevel@tonic-gate #include <ftw.h>
590Sstevel@tonic-gate #include <ctype.h>
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #define	CFGA_PLUGIN_LIB
620Sstevel@tonic-gate #include <config_admin.h>
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /* Limit size of sysinfo return */
650Sstevel@tonic-gate #define	SYSINFO_LENGTH	256
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*
680Sstevel@tonic-gate  * Attachment point specifier types.
690Sstevel@tonic-gate  */
700Sstevel@tonic-gate typedef enum {
710Sstevel@tonic-gate 	UNKNOWN_AP,
720Sstevel@tonic-gate 	LOGICAL_LINK_AP,
730Sstevel@tonic-gate 	LOGICAL_DRV_AP,
740Sstevel@tonic-gate 	PHYSICAL_AP,
750Sstevel@tonic-gate 	AP_TYPE
760Sstevel@tonic-gate } cfga_ap_types_t;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate static char *listopt_array[] = {
790Sstevel@tonic-gate 
800Sstevel@tonic-gate #define	LISTOPT_CLASS	0
810Sstevel@tonic-gate 	"class",
820Sstevel@tonic-gate 	NULL
830Sstevel@tonic-gate };
840Sstevel@tonic-gate 
850Sstevel@tonic-gate typedef struct {
860Sstevel@tonic-gate 	int v_min;	/* Min acceptable version */
870Sstevel@tonic-gate 	int v_max;	/* Max acceptable version */
880Sstevel@tonic-gate } vers_req_t;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate #define	INVALID_VERSION		-1
910Sstevel@tonic-gate #define	VALID_HSL_VERS(v)	(((v) >= CFGA_HSL_V1) && \
920Sstevel@tonic-gate 				((v) <= CFGA_HSL_VERS))
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /*
950Sstevel@tonic-gate  * Incomplete definition
960Sstevel@tonic-gate  */
970Sstevel@tonic-gate struct cfga_vers_ops;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  * Structure that contains plugin library information.
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate typedef struct plugin_lib {
1030Sstevel@tonic-gate 	struct	plugin_lib *next;	/* pointer to next */
1040Sstevel@tonic-gate 	mutex_t	lock;			/* protects refcnt */
1050Sstevel@tonic-gate 	int	refcnt;			/* reference count */
1060Sstevel@tonic-gate 	void	*handle;		/* handle from dlopen */
1070Sstevel@tonic-gate 	cfga_err_t	(*cfga_change_state_p)();
1080Sstevel@tonic-gate 	cfga_err_t	(*cfga_private_func_p)();
1090Sstevel@tonic-gate 	cfga_err_t	(*cfga_test_p)();
1100Sstevel@tonic-gate 	cfga_err_t	(*cfga_stat_p)();
1110Sstevel@tonic-gate 	cfga_err_t	(*cfga_list_p)();
1120Sstevel@tonic-gate 	cfga_err_t	(*cfga_help_p)();
1130Sstevel@tonic-gate 	int		(*cfga_ap_id_cmp_p)();
1140Sstevel@tonic-gate 	cfga_err_t	(*cfga_list_ext_p)();	/* For V2 plug-ins only */
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	int		plugin_vers;	/* actual plugin version */
1170Sstevel@tonic-gate 	struct cfga_vers_ops *vers_ops;	/* version dependant routines */
1180Sstevel@tonic-gate 	char	libpath[MAXPATHLEN];	/* full pathname to lib */
1190Sstevel@tonic-gate } plugin_lib_t;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate static plugin_lib_t plugin_list;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate typedef struct lib_cache {
1240Sstevel@tonic-gate 	struct lib_cache *lc_next;
1250Sstevel@tonic-gate 	plugin_lib_t *lc_libp;
1260Sstevel@tonic-gate 	char *lc_ap_id;
1270Sstevel@tonic-gate 	char *lc_ap_physical;	/* physical ap_id */
1280Sstevel@tonic-gate 	char *lc_ap_logical;	/* logical ap_id */
1290Sstevel@tonic-gate } lib_cache_t;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate static lib_cache_t *lib_cache;
1320Sstevel@tonic-gate static mutex_t lib_cache_lock;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate  * Library locator data struct - used to pass down through the device
1360Sstevel@tonic-gate  * tree walking code.
1370Sstevel@tonic-gate  */
1380Sstevel@tonic-gate typedef struct lib_locator {
1390Sstevel@tonic-gate 	char	ap_base[MAXPATHLEN];
1400Sstevel@tonic-gate 	char	ap_logical[CFGA_LOG_EXT_LEN];
1410Sstevel@tonic-gate 	char	ap_physical[CFGA_PHYS_EXT_LEN];
1420Sstevel@tonic-gate 	char	ap_class[CFGA_CLASS_LEN];
1430Sstevel@tonic-gate 	char	pathname[MAXPATHLEN];
1440Sstevel@tonic-gate 	plugin_lib_t *libp;
1450Sstevel@tonic-gate 	cfga_err_t status;
1460Sstevel@tonic-gate 	vers_req_t vers_req;	/* plug-in version required */
1470Sstevel@tonic-gate } lib_loc_t;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate /*
1500Sstevel@tonic-gate  * linked list of cfga_stat_data structs - used for
1510Sstevel@tonic-gate  * config_list
1520Sstevel@tonic-gate  */
1530Sstevel@tonic-gate typedef struct stat_data_list {
1540Sstevel@tonic-gate 	struct stat_data_list	*next;
1550Sstevel@tonic-gate 	cfga_stat_data_t	stat_data;
1560Sstevel@tonic-gate } stat_data_list_t;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate /*
1590Sstevel@tonic-gate  * linked list of arrays. Each array represents a bunch
1600Sstevel@tonic-gate  * of list_data_t structures returned by a single call
1610Sstevel@tonic-gate  * to a plugin's cfga_list_ext() routine.
1620Sstevel@tonic-gate  */
1630Sstevel@tonic-gate typedef struct array_list {
1640Sstevel@tonic-gate 	struct array_list	*next;
1650Sstevel@tonic-gate 	cfga_list_data_t	*array;
1660Sstevel@tonic-gate 	int			nelem;
1670Sstevel@tonic-gate } array_list_t;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate  * encapsulate config_list args to get them through the tree
1710Sstevel@tonic-gate  * walking code
1720Sstevel@tonic-gate  */
1730Sstevel@tonic-gate typedef struct list_stat {
1740Sstevel@tonic-gate 	const char *opts;	/* Hardware specific options */
1750Sstevel@tonic-gate 	char **errstr;
1760Sstevel@tonic-gate 	cfga_flags_t flags;
1770Sstevel@tonic-gate 	int	*countp;	/* Total number of list and stat structures */
1780Sstevel@tonic-gate 	stat_data_list_t *sdl;	/* Linked list of stat structures */
1790Sstevel@tonic-gate 	array_list_t *al;	/* Linked list of arrays of list structures */
1800Sstevel@tonic-gate 	vers_req_t use_vers;	/* plugin versions to be stat'ed */
1810Sstevel@tonic-gate } list_stat_t;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate  * Internal operations for libcfgadm which are version dependant
1850Sstevel@tonic-gate  */
1860Sstevel@tonic-gate struct cfga_vers_ops {
1870Sstevel@tonic-gate 	cfga_err_t (*resolve_lib)(plugin_lib_t *libp);
1880Sstevel@tonic-gate 	cfga_err_t (*stat_plugin)(list_stat_t *, lib_loc_t *, char **errstring);
1890Sstevel@tonic-gate 	cfga_err_t (*mklog)(di_node_t, di_minor_t, plugin_lib_t *,
1900Sstevel@tonic-gate 	    lib_loc_t *liblocp);
1910Sstevel@tonic-gate 	cfga_err_t (*get_cond)(lib_loc_t *, cfga_cond_t *, char **);
1920Sstevel@tonic-gate };
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate /*
1960Sstevel@tonic-gate  * Lock to protect list of libraries
1970Sstevel@tonic-gate  */
1980Sstevel@tonic-gate static mutex_t plugin_list_lock;
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate /*
2010Sstevel@tonic-gate  * Forward declarations
2020Sstevel@tonic-gate  */
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate static const char *__config_strerror(cfga_err_t);
2050Sstevel@tonic-gate static void *config_calloc_check(size_t, size_t, char **);
2060Sstevel@tonic-gate static cfga_err_t resolve_lib_ref(plugin_lib_t *, lib_loc_t *);
2070Sstevel@tonic-gate static cfga_err_t config_get_lib(const char *, lib_loc_t *, char **);
2080Sstevel@tonic-gate static int check_ap(di_node_t, di_minor_t, void *);
2090Sstevel@tonic-gate static int check_ap_phys(di_node_t, di_minor_t, void *);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate static cfga_err_t find_ap_common(lib_loc_t *libloc_p, const char *rootpath,
2120Sstevel@tonic-gate     int (*fcn)(di_node_t node, di_minor_t minor, void *arg), char **errstring);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate static plugin_lib_t *lib_in_list(char *);
2150Sstevel@tonic-gate static cfga_err_t load_lib(di_node_t, di_minor_t, lib_loc_t *);
2160Sstevel@tonic-gate extern void bcopy(const void *, void *, size_t);
2170Sstevel@tonic-gate static void config_err(int, int, char **);
2180Sstevel@tonic-gate static void hold_lib(plugin_lib_t *);
2190Sstevel@tonic-gate static void rele_lib(plugin_lib_t *);
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate static cfga_err_t parse_listopt(char *listopts, char **classpp,
2220Sstevel@tonic-gate     char **errstring);
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate static cfga_err_t list_common(list_stat_t *lstatp, const char *class);
2250Sstevel@tonic-gate static int do_list_common(di_node_t node, di_minor_t minor, void *arg);
2260Sstevel@tonic-gate static cfga_err_t stat_common(int num_ap_ids, char *const *ap_ids,
2270Sstevel@tonic-gate     const char *class, list_stat_t *lstatp);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate static cfga_err_t null_resolve(plugin_lib_t *libp);
2300Sstevel@tonic-gate static cfga_err_t resolve_v1(plugin_lib_t *libp);
2310Sstevel@tonic-gate static cfga_err_t resolve_v2(plugin_lib_t *libp);
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate static cfga_err_t mklog_common(di_node_t node, di_minor_t minor,
2340Sstevel@tonic-gate     lib_loc_t *liblocp, size_t len);
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate static cfga_err_t null_mklog(di_node_t node, di_minor_t minor,
2370Sstevel@tonic-gate     plugin_lib_t *libp, lib_loc_t *liblocp);
2380Sstevel@tonic-gate static cfga_err_t mklog_v1(di_node_t node, di_minor_t minor,
2390Sstevel@tonic-gate     plugin_lib_t *libp, lib_loc_t *liblocp);
2400Sstevel@tonic-gate static cfga_err_t mklog_v2(di_node_t node, di_minor_t minor,
2410Sstevel@tonic-gate     plugin_lib_t *libp, lib_loc_t *liblocp);
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate static cfga_err_t null_stat_plugin(list_stat_t *lstatp, lib_loc_t *libloc_p,
2440Sstevel@tonic-gate     char **errstring);
2450Sstevel@tonic-gate static cfga_err_t stat_plugin_v2(list_stat_t *lstat, lib_loc_t *libloc_p,
2460Sstevel@tonic-gate     char **errstring);
2470Sstevel@tonic-gate static cfga_err_t stat_plugin_v1(list_stat_t *lstat, lib_loc_t *libloc_p,
2480Sstevel@tonic-gate     char **errstring);
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate static cfga_err_t null_get_cond(lib_loc_t *liblocp, cfga_cond_t *condp,
2510Sstevel@tonic-gate     char **errstring);
2520Sstevel@tonic-gate static cfga_err_t get_cond_v1(lib_loc_t *liblocp, cfga_cond_t *condp,
2530Sstevel@tonic-gate     char **errstring);
2540Sstevel@tonic-gate static cfga_err_t get_cond_v2(lib_loc_t *liblocp, cfga_cond_t *condp,
2550Sstevel@tonic-gate     char **errstring);
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate static cfga_err_t realloc_data(cfga_stat_data_t **ap_id_list,
2580Sstevel@tonic-gate     int *nlistp, list_stat_t *lstatp);
2590Sstevel@tonic-gate static cfga_err_t realloc_data_ext(cfga_list_data_t **ap_id_list,
2600Sstevel@tonic-gate     int *nlistp, list_stat_t *lstatp);
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate static void stat_to_list(cfga_list_data_t *lp, cfga_stat_data_t *statp);
2630Sstevel@tonic-gate static void lstat_free(list_stat_t *lstatp);
2640Sstevel@tonic-gate static cfga_ap_types_t find_arg_type(const char *ap_id);
2650Sstevel@tonic-gate static int compat_plugin(vers_req_t *reqp, int plugin_vers);
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate static cfga_err_t check_flags(cfga_flags_t flags, cfga_flags_t mask,
2680Sstevel@tonic-gate     char **errstring);
2690Sstevel@tonic-gate static cfga_err_t check_apids(int num_ap_ids, char *const *ap_ids,
2700Sstevel@tonic-gate     char **errstring);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate static char *get_class(di_minor_t minor);
2730Sstevel@tonic-gate static cfga_err_t split_apid(char *ap_id, char **dyncompp, char **errstring);
2740Sstevel@tonic-gate static void append_dyn(char *buf, const char *dyncomp, size_t blen);
2750Sstevel@tonic-gate static int default_ap_id_cmp(const char *ap_id1, const char *ap_id2);
2760Sstevel@tonic-gate static void destroy_cache();
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate /*
2790Sstevel@tonic-gate  * Plugin library search path helpers
2800Sstevel@tonic-gate  */
2810Sstevel@tonic-gate #define	LIB_PATH_BASE1	"/usr/platform/"
2820Sstevel@tonic-gate #define	LIB_PATH_BASE2	"/usr"
283*133Sdmick #if defined(__sparcv9)
2840Sstevel@tonic-gate #define	LIB_PATH_MIDDLE	"/lib/cfgadm/sparcv9/"
285*133Sdmick #elif defined(__amd64)
286*133Sdmick #define	LIB_PATH_MIDDLE "/lib/cfgadm/amd64/"
2870Sstevel@tonic-gate #else
2880Sstevel@tonic-gate #define	LIB_PATH_MIDDLE	"/lib/cfgadm/"
2890Sstevel@tonic-gate #endif
2900Sstevel@tonic-gate #define	LIB_PATH_TAIL	".so.1"
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
2940Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
2950Sstevel@tonic-gate #endif
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate /*
2980Sstevel@tonic-gate  * Defined constants
2990Sstevel@tonic-gate  */
3000Sstevel@tonic-gate #define	DEVICES_DIR		"/devices"
3010Sstevel@tonic-gate #define	DOT_DOT_DEVICES		"../devices"
3020Sstevel@tonic-gate #define	CFGA_DEV_DIR		"/dev/cfg"
3030Sstevel@tonic-gate #define	SLASH			"/"
3040Sstevel@tonic-gate #define	S_FREE(x)	(((x) != NULL) ? (free(x), (x) = NULL) : (void *)0)
3050Sstevel@tonic-gate #define	GET_DYN(a)	(strstr((a), CFGA_DYN_SEP))
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate #define	CFGA_NO_CLASS		"none"
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate /*
3100Sstevel@tonic-gate  * Error strings
3110Sstevel@tonic-gate  */
3120Sstevel@tonic-gate #define	DI_INIT_FAILED	1
3130Sstevel@tonic-gate #define	ALLOC_FAILED	2
3140Sstevel@tonic-gate #define	INVALID_ARGS	3
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate static char *
3170Sstevel@tonic-gate err_strings[] = {
3180Sstevel@tonic-gate 	NULL,
3190Sstevel@tonic-gate 	"Device library initialize failed",
3200Sstevel@tonic-gate 	"Memory allocation failed",
3210Sstevel@tonic-gate 	"Invalid argument(s)"
3220Sstevel@tonic-gate };
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate static const char err_sep[] = ": ";
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate /*
3280Sstevel@tonic-gate  * Table of version dependant routines
3290Sstevel@tonic-gate  */
3300Sstevel@tonic-gate static struct cfga_vers_ops cfga_vers_ops[CFGA_HSL_VERS + 1] = {
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate {null_resolve,	null_stat_plugin,	null_mklog,	null_get_cond	},
3330Sstevel@tonic-gate {resolve_v1,	stat_plugin_v1,		mklog_v1,	get_cond_v1	},
3340Sstevel@tonic-gate {resolve_v2,	stat_plugin_v2,		mklog_v2,	get_cond_v2	}
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate };
3370Sstevel@tonic-gate #define	VERS_ARRAY_SZ	(sizeof (cfga_vers_ops)/sizeof (cfga_vers_ops[0]))
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate /*
3410Sstevel@tonic-gate  * Public interfaces for libcfgadm, as documented in config_admin.3x
3420Sstevel@tonic-gate  */
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate /*
3450Sstevel@tonic-gate  * config_change_state
3460Sstevel@tonic-gate  */
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate cfga_err_t
3490Sstevel@tonic-gate config_change_state(
3500Sstevel@tonic-gate 	cfga_cmd_t state_change_cmd,
3510Sstevel@tonic-gate 	int num_ap_ids,
3520Sstevel@tonic-gate 	char *const *ap_id,
3530Sstevel@tonic-gate 	const char *options,
3540Sstevel@tonic-gate 	struct cfga_confirm *confp,
3550Sstevel@tonic-gate 	struct cfga_msg *msgp,
3560Sstevel@tonic-gate 	char **errstring,
3570Sstevel@tonic-gate 	cfga_flags_t flags)
3580Sstevel@tonic-gate {
3590Sstevel@tonic-gate 	/*
3600Sstevel@tonic-gate 	 * for each arg -
3610Sstevel@tonic-gate 	 *  load hs library,
3620Sstevel@tonic-gate 	 *  if force
3630Sstevel@tonic-gate 	 *    call cfga_state_change_func
3640Sstevel@tonic-gate 	 *    return status
3650Sstevel@tonic-gate 	 *  else
3660Sstevel@tonic-gate 	 *    call it's cfga_stat
3670Sstevel@tonic-gate 	 *    check condition
3680Sstevel@tonic-gate 	 *    call cfga_state_change_func
3690Sstevel@tonic-gate 	 *    return status
3700Sstevel@tonic-gate 	 */
3710Sstevel@tonic-gate 	int i;
3720Sstevel@tonic-gate 	lib_loc_t libloc;
3730Sstevel@tonic-gate 	plugin_lib_t *libp;
3740Sstevel@tonic-gate 	cfga_cond_t cond;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	cfga_err_t retval = CFGA_OK;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	/* Sanity checks */
3790Sstevel@tonic-gate 	if (state_change_cmd == CFGA_CMD_NONE)
3800Sstevel@tonic-gate 		return (retval);
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	if ((state_change_cmd < CFGA_CMD_NONE) ||
3830Sstevel@tonic-gate 	    (state_change_cmd > CFGA_CMD_UNCONFIGURE))
3840Sstevel@tonic-gate 		return (CFGA_INVAL);
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	if (errstring != NULL) {
3870Sstevel@tonic-gate 		*errstring = NULL;
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	if (check_flags(flags, CFGA_FLAG_FORCE | CFGA_FLAG_VERBOSE, errstring)
3910Sstevel@tonic-gate 	    != CFGA_OK) {
3920Sstevel@tonic-gate 		return (CFGA_ERROR);
3930Sstevel@tonic-gate 	}
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	if (check_apids(num_ap_ids, ap_id, errstring) != CFGA_OK) {
3960Sstevel@tonic-gate 		return (CFGA_ERROR);
3970Sstevel@tonic-gate 	}
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	/*
4000Sstevel@tonic-gate 	 * operate on each ap_id
4010Sstevel@tonic-gate 	 */
4020Sstevel@tonic-gate 	for (i = 0; (i < num_ap_ids) && (retval == CFGA_OK); i++) {
4030Sstevel@tonic-gate 		libloc.libp = NULL;
4040Sstevel@tonic-gate 		if ((retval = config_get_lib(ap_id[i], &libloc, errstring)) !=
4050Sstevel@tonic-gate 		    CFGA_OK) {
4060Sstevel@tonic-gate 			break;
4070Sstevel@tonic-gate 		}
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 		libp = libloc.libp;
4100Sstevel@tonic-gate 		if ((flags & CFGA_FLAG_FORCE) ||
4110Sstevel@tonic-gate 		    (state_change_cmd == CFGA_CMD_UNLOAD) ||
4120Sstevel@tonic-gate 		    (state_change_cmd == CFGA_CMD_DISCONNECT) ||
4130Sstevel@tonic-gate 		    (state_change_cmd == CFGA_CMD_UNCONFIGURE)) {
4140Sstevel@tonic-gate 			errno = 0;
4150Sstevel@tonic-gate 			retval = (*libp->cfga_change_state_p)
4160Sstevel@tonic-gate 			    (state_change_cmd, libloc.ap_physical, options,
4170Sstevel@tonic-gate 			    confp, msgp, errstring, flags);
4180Sstevel@tonic-gate 		} else {
4190Sstevel@tonic-gate 			/*
4200Sstevel@tonic-gate 			 * Need to check condition before proceeding in
4210Sstevel@tonic-gate 			 * the "configure direction"
4220Sstevel@tonic-gate 			 */
4230Sstevel@tonic-gate 			if ((retval = libp->vers_ops->get_cond(&libloc, &cond,
4240Sstevel@tonic-gate 			    errstring)) != CFGA_OK) {
4250Sstevel@tonic-gate 				break;
4260Sstevel@tonic-gate 			}
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 			if (cond == CFGA_COND_OK || cond == CFGA_COND_UNKNOWN) {
4290Sstevel@tonic-gate 				errno = 0;
4300Sstevel@tonic-gate 				retval =
4310Sstevel@tonic-gate 				    (*libp->cfga_change_state_p)(
4320Sstevel@tonic-gate 				    state_change_cmd,
4330Sstevel@tonic-gate 				    libloc.ap_physical, options,
4340Sstevel@tonic-gate 				    confp, msgp, errstring,
4350Sstevel@tonic-gate 				    flags);
4360Sstevel@tonic-gate 			} else {
4370Sstevel@tonic-gate 				retval = CFGA_INSUFFICENT_CONDITION;
4380Sstevel@tonic-gate 			}
4390Sstevel@tonic-gate 		}
4400Sstevel@tonic-gate 		rele_lib(libp);
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	return (retval);
4440Sstevel@tonic-gate }
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate /*
4470Sstevel@tonic-gate  * config_private_func
4480Sstevel@tonic-gate  */
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate cfga_err_t
4510Sstevel@tonic-gate config_private_func(
4520Sstevel@tonic-gate 	const char *function,
4530Sstevel@tonic-gate 	int num_ap_ids,
4540Sstevel@tonic-gate 	char *const *ap_ids,
4550Sstevel@tonic-gate 	const char *options,
4560Sstevel@tonic-gate 	struct cfga_confirm *confp,
4570Sstevel@tonic-gate 	struct cfga_msg *msgp,
4580Sstevel@tonic-gate 	char **errstring,
4590Sstevel@tonic-gate 	cfga_flags_t flags)
4600Sstevel@tonic-gate {
4610Sstevel@tonic-gate 	int i;
4620Sstevel@tonic-gate 	lib_loc_t libloc;
4630Sstevel@tonic-gate 	cfga_err_t retval = CFGA_OK;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	if (errstring != NULL) {
4670Sstevel@tonic-gate 		*errstring = NULL;
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	if (check_flags(flags, CFGA_FLAG_FORCE | CFGA_FLAG_VERBOSE, errstring)
4710Sstevel@tonic-gate 	    != CFGA_OK) {
4720Sstevel@tonic-gate 		return (CFGA_ERROR);
4730Sstevel@tonic-gate 	}
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	if (check_apids(num_ap_ids, ap_ids, errstring) != CFGA_OK) {
4760Sstevel@tonic-gate 		return (CFGA_ERROR);
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	/*
4800Sstevel@tonic-gate 	 * operate on each ap_id
4810Sstevel@tonic-gate 	 */
4820Sstevel@tonic-gate 	for (i = 0; (i < num_ap_ids) && (retval == CFGA_OK); i++) {
4830Sstevel@tonic-gate 		libloc.libp = NULL;
4840Sstevel@tonic-gate 		if ((retval = config_get_lib(ap_ids[i], &libloc, errstring)) !=
4850Sstevel@tonic-gate 		    CFGA_OK)  {
4860Sstevel@tonic-gate 			return (retval);
4870Sstevel@tonic-gate 		}
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 		errno = 0;
4900Sstevel@tonic-gate 		retval = (*libloc.libp->cfga_private_func_p)(function,
4910Sstevel@tonic-gate 		    libloc.ap_physical, options, confp, msgp, errstring,
4920Sstevel@tonic-gate 		    flags);
4930Sstevel@tonic-gate 		rele_lib(libloc.libp);
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	return (retval);
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate /*
5010Sstevel@tonic-gate  * config_test
5020Sstevel@tonic-gate  */
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate cfga_err_t
5050Sstevel@tonic-gate config_test(
5060Sstevel@tonic-gate 	int num_ap_ids,
5070Sstevel@tonic-gate 	char *const *ap_ids,
5080Sstevel@tonic-gate 	const char *options,
5090Sstevel@tonic-gate 	struct cfga_msg *msgp,
5100Sstevel@tonic-gate 	char **errstring,
5110Sstevel@tonic-gate 	cfga_flags_t flags)
5120Sstevel@tonic-gate {
5130Sstevel@tonic-gate 	int i;
5140Sstevel@tonic-gate 	lib_loc_t libloc;
5150Sstevel@tonic-gate 	cfga_err_t retval = CFGA_OK;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	if (errstring != NULL) {
5180Sstevel@tonic-gate 		*errstring = NULL;
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	if (check_flags(flags, CFGA_FLAG_FORCE | CFGA_FLAG_VERBOSE, errstring)
5220Sstevel@tonic-gate 	    != CFGA_OK) {
5230Sstevel@tonic-gate 		return (CFGA_ERROR);
5240Sstevel@tonic-gate 	}
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	if (check_apids(num_ap_ids, ap_ids, errstring) != CFGA_OK) {
5270Sstevel@tonic-gate 		return (CFGA_ERROR);
5280Sstevel@tonic-gate 	}
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	/*
5310Sstevel@tonic-gate 	 * operate on each ap_id
5320Sstevel@tonic-gate 	 */
5330Sstevel@tonic-gate 	for (i = 0; (i < num_ap_ids) && (retval == CFGA_OK); i++) {
5340Sstevel@tonic-gate 		libloc.libp = NULL;
5350Sstevel@tonic-gate 		if ((retval = config_get_lib(ap_ids[i], &libloc, errstring)) !=
5360Sstevel@tonic-gate 		    CFGA_OK) {
5370Sstevel@tonic-gate 			return (retval);
5380Sstevel@tonic-gate 		}
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 		errno = 0;
5410Sstevel@tonic-gate 		retval = (*libloc.libp->cfga_test_p)(libloc.ap_physical,
5420Sstevel@tonic-gate 		    options, msgp, errstring, flags);
5430Sstevel@tonic-gate 		rele_lib(libloc.libp);
5440Sstevel@tonic-gate 	}
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	return (retval);
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate cfga_err_t
5500Sstevel@tonic-gate config_stat(
5510Sstevel@tonic-gate 	int num_ap_ids,
5520Sstevel@tonic-gate 	char *const *ap_ids,
5530Sstevel@tonic-gate 	struct cfga_stat_data *buf,
5540Sstevel@tonic-gate 	const char *options,
5550Sstevel@tonic-gate 	char **errstring)
5560Sstevel@tonic-gate {
5570Sstevel@tonic-gate 	int nstat, n, i;
5580Sstevel@tonic-gate 	list_stat_t lstat = {NULL};
5590Sstevel@tonic-gate 	cfga_err_t rc = CFGA_OK;
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	if (check_apids(num_ap_ids, ap_ids, errstring) != CFGA_OK) {
5620Sstevel@tonic-gate 		return (CFGA_ERROR);
5630Sstevel@tonic-gate 	}
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	/*
5660Sstevel@tonic-gate 	 * V1 entry points don't support dynamic attachment points
5670Sstevel@tonic-gate 	 */
5680Sstevel@tonic-gate 	for (i = 0; i < num_ap_ids; i++) {
5690Sstevel@tonic-gate 		if (GET_DYN(ap_ids[i]) != NULL) {
5700Sstevel@tonic-gate 			return (CFGA_APID_NOEXIST);
5710Sstevel@tonic-gate 		}
5720Sstevel@tonic-gate 	}
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	nstat = n = 0;
5760Sstevel@tonic-gate 	lstat.countp = &nstat;
5770Sstevel@tonic-gate 	lstat.opts = options;
5780Sstevel@tonic-gate 	lstat.errstr = errstring;
5790Sstevel@tonic-gate 	/*
5800Sstevel@tonic-gate 	 * This is a V1 interface which can use only V1 plugins
5810Sstevel@tonic-gate 	 */
5820Sstevel@tonic-gate 	lstat.use_vers.v_max = lstat.use_vers.v_min = CFGA_HSL_V1;
5830Sstevel@tonic-gate 
5840Sstevel@tonic-gate 	rc = stat_common(num_ap_ids, ap_ids, NULL, &lstat);
5850Sstevel@tonic-gate 	if (rc == CFGA_OK) {
5860Sstevel@tonic-gate 		assert(*lstat.countp == num_ap_ids);
5870Sstevel@tonic-gate 		rc = realloc_data(&buf, &n, &lstat);
5880Sstevel@tonic-gate 	}
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 	return (rc);
5910Sstevel@tonic-gate }
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate /*
5940Sstevel@tonic-gate  * config_list
5950Sstevel@tonic-gate  */
5960Sstevel@tonic-gate cfga_err_t
5970Sstevel@tonic-gate config_list(
5980Sstevel@tonic-gate 	struct cfga_stat_data **ap_id_list,
5990Sstevel@tonic-gate 	int *nlistp,
6000Sstevel@tonic-gate 	const char *options,
6010Sstevel@tonic-gate 	char **errstring)
6020Sstevel@tonic-gate {
6030Sstevel@tonic-gate 	int nstat;
6040Sstevel@tonic-gate 	list_stat_t lstat = {NULL};
6050Sstevel@tonic-gate 	cfga_err_t retval = CFGA_ERROR;
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	if (errstring != NULL) {
6080Sstevel@tonic-gate 		*errstring = NULL;
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	nstat = 0;
6120Sstevel@tonic-gate 	lstat.countp = &nstat;
6130Sstevel@tonic-gate 	lstat.opts = options;
6140Sstevel@tonic-gate 	lstat.errstr = errstring;
6150Sstevel@tonic-gate 	/*
6160Sstevel@tonic-gate 	 * This is a V1 interface which can use only V1 plugins
6170Sstevel@tonic-gate 	 */
6180Sstevel@tonic-gate 	lstat.use_vers.v_max = lstat.use_vers.v_min = CFGA_HSL_V1;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	*ap_id_list = NULL;
6220Sstevel@tonic-gate 	*nlistp = 0;
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	/*
6250Sstevel@tonic-gate 	 * V1 interfaces don't support prefiltering, no class
6260Sstevel@tonic-gate 	 * specified.
6270Sstevel@tonic-gate 	 */
6280Sstevel@tonic-gate 	retval = list_common(&lstat, NULL);
6290Sstevel@tonic-gate 	if (retval == CFGA_OK) {
6300Sstevel@tonic-gate 		retval = realloc_data(ap_id_list, nlistp, &lstat);
6310Sstevel@tonic-gate 	}
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	assert((ap_id_list != NULL && *nlistp != 0) ||
6340Sstevel@tonic-gate 	    (ap_id_list == NULL && *nlistp == 0));
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	if (retval == CFGA_OK && *nlistp == 0) {
6370Sstevel@tonic-gate 		return (CFGA_NOTSUPP);
6380Sstevel@tonic-gate 	} else {
6390Sstevel@tonic-gate 		return (retval);
6400Sstevel@tonic-gate 	}
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate /*
6450Sstevel@tonic-gate  * config_list_ext
6460Sstevel@tonic-gate  */
6470Sstevel@tonic-gate cfga_err_t
6480Sstevel@tonic-gate config_list_ext(
6490Sstevel@tonic-gate 	int num_ap_ids,
6500Sstevel@tonic-gate 	char *const *ap_ids,
6510Sstevel@tonic-gate 	struct cfga_list_data **ap_id_list,
6520Sstevel@tonic-gate 	int *nlistp,
6530Sstevel@tonic-gate 	const char *options,
6540Sstevel@tonic-gate 	const char *listopts,
6550Sstevel@tonic-gate 	char **errstring,
6560Sstevel@tonic-gate 	cfga_flags_t flags)
6570Sstevel@tonic-gate {
6580Sstevel@tonic-gate 	int nstat, list, prefilter;
6590Sstevel@tonic-gate 	list_stat_t lstat = {NULL};
6600Sstevel@tonic-gate 	char *class;
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	cfga_err_t rc = CFGA_ERROR;
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	*nlistp = 0;
6650Sstevel@tonic-gate 	*ap_id_list = NULL;
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 	if (errstring != NULL) {
6680Sstevel@tonic-gate 		*errstring = NULL;
6690Sstevel@tonic-gate 	}
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	if (check_flags(flags, CFGA_FLAG_LIST_ALL, errstring) != CFGA_OK) {
6720Sstevel@tonic-gate 		return (CFGA_ERROR);
6730Sstevel@tonic-gate 	}
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	class = NULL;
6760Sstevel@tonic-gate 	if ((rc = parse_listopt((char *)listopts, &class, errstring))
6770Sstevel@tonic-gate 	    != CFGA_OK) {
6780Sstevel@tonic-gate 		return (rc);
6790Sstevel@tonic-gate 	}
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	prefilter = (class == NULL) ? 0 : 1;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	nstat = 0;
6840Sstevel@tonic-gate 	lstat.countp = &nstat;
6850Sstevel@tonic-gate 	lstat.opts = options;
6860Sstevel@tonic-gate 	lstat.errstr = errstring;
6870Sstevel@tonic-gate 	lstat.flags = flags;
6880Sstevel@tonic-gate 	/*
6890Sstevel@tonic-gate 	 * We support both V1 and V2 plugins through this entry
6900Sstevel@tonic-gate 	 * point.
6910Sstevel@tonic-gate 	 */
6920Sstevel@tonic-gate 	lstat.use_vers.v_min = CFGA_HSL_V1;
6930Sstevel@tonic-gate 	lstat.use_vers.v_max = CFGA_HSL_V2;
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	list = 0;
6960Sstevel@tonic-gate 	if (num_ap_ids == 0 && ap_ids == NULL) {
6970Sstevel@tonic-gate 		/*
6980Sstevel@tonic-gate 		 * discover and stat all attachment points
6990Sstevel@tonic-gate 		 */
7000Sstevel@tonic-gate 		list = 1;
7010Sstevel@tonic-gate 		rc = list_common(&lstat, class);
7020Sstevel@tonic-gate 	} else if (num_ap_ids > 0 && ap_ids != NULL) {
7030Sstevel@tonic-gate 		/*
7040Sstevel@tonic-gate 		 * Stat specified attachment points. With dynamic expansion
7050Sstevel@tonic-gate 		 * more data may be returned than was specified by user.
7060Sstevel@tonic-gate 		 */
7070Sstevel@tonic-gate 		rc = stat_common(num_ap_ids, ap_ids, class, &lstat);
7080Sstevel@tonic-gate 	} else {
7090Sstevel@tonic-gate 		rc = CFGA_ERROR;
7100Sstevel@tonic-gate 	}
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 	S_FREE(class);
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	if (rc != CFGA_OK) {
7150Sstevel@tonic-gate 		return (rc);
7160Sstevel@tonic-gate 	}
7170Sstevel@tonic-gate 
7180Sstevel@tonic-gate 	rc = realloc_data_ext(ap_id_list, nlistp, &lstat);
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 	assert((ap_id_list != NULL && *nlistp != 0) ||
7210Sstevel@tonic-gate 	    (ap_id_list == NULL && *nlistp == 0));
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	/*
7240Sstevel@tonic-gate 	 * For the list command notify user if no attachment
7250Sstevel@tonic-gate 	 * point is found in the system.
7260Sstevel@tonic-gate 	 *
7270Sstevel@tonic-gate 	 */
7280Sstevel@tonic-gate 	if (list && rc == CFGA_OK && *nlistp == 0) {
7290Sstevel@tonic-gate 		/*
7300Sstevel@tonic-gate 		 * If attachment points are being prefiltered, absence of data
7310Sstevel@tonic-gate 		 * does not imply that config. admin. is not
7320Sstevel@tonic-gate 		 * supported by the system.
7330Sstevel@tonic-gate 		 */
7340Sstevel@tonic-gate 		if (prefilter) {
7350Sstevel@tonic-gate 			/*
7360Sstevel@tonic-gate 			 * Prefiltering: requested class is absent
7370Sstevel@tonic-gate 			 */
7380Sstevel@tonic-gate 			return (CFGA_APID_NOEXIST);
7390Sstevel@tonic-gate 		} else {
7400Sstevel@tonic-gate 			/*
7410Sstevel@tonic-gate 			 * No attachment points in system
7420Sstevel@tonic-gate 			 */
7430Sstevel@tonic-gate 			return (CFGA_NOTSUPP);
7440Sstevel@tonic-gate 		}
7450Sstevel@tonic-gate 	} else {
7460Sstevel@tonic-gate 		return (rc);
7470Sstevel@tonic-gate 	}
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate /*
7520Sstevel@tonic-gate  * config_unload_libs
7530Sstevel@tonic-gate  *
7540Sstevel@tonic-gate  * Attempts to remove all libs on the plugin list.
7550Sstevel@tonic-gate  */
7560Sstevel@tonic-gate void
7570Sstevel@tonic-gate config_unload_libs()
7580Sstevel@tonic-gate {
7590Sstevel@tonic-gate 	plugin_lib_t *libp, *prev = &plugin_list, *next = NULL;
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	/* destroy cache entries to remove refcnt agains plugins */
7620Sstevel@tonic-gate 	destroy_cache();
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	(void) mutex_lock(&plugin_list_lock);
7650Sstevel@tonic-gate 	for (libp = plugin_list.next; libp != NULL; libp = next) {
7660Sstevel@tonic-gate 		next = libp->next;
7670Sstevel@tonic-gate 		(void) mutex_lock(&libp->lock);
7680Sstevel@tonic-gate 		if (libp->refcnt) {
7690Sstevel@tonic-gate 			(void) mutex_unlock(&libp->lock);
7700Sstevel@tonic-gate 			prev = libp;
7710Sstevel@tonic-gate 			continue;
7720Sstevel@tonic-gate 		}
7730Sstevel@tonic-gate 		(void) mutex_unlock(&libp->lock);
7740Sstevel@tonic-gate 		prev->next = next;
7750Sstevel@tonic-gate 		(void) dlclose(libp->handle);
7760Sstevel@tonic-gate 		(void) mutex_destroy(&libp->lock);
7770Sstevel@tonic-gate 		free(libp);
7780Sstevel@tonic-gate 	}
7790Sstevel@tonic-gate 	(void) mutex_unlock(&plugin_list_lock);
7800Sstevel@tonic-gate }
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate /*
7830Sstevel@tonic-gate  * config_ap_id_cmp
7840Sstevel@tonic-gate  */
7850Sstevel@tonic-gate int
7860Sstevel@tonic-gate config_ap_id_cmp(
7870Sstevel@tonic-gate 	const cfga_ap_log_id_t ap1,
7880Sstevel@tonic-gate 	const cfga_ap_log_id_t ap2)
7890Sstevel@tonic-gate {
7900Sstevel@tonic-gate 	int ret;
7910Sstevel@tonic-gate 	lib_loc_t libloc;
7920Sstevel@tonic-gate 	char apstat1[CFGA_PHYS_EXT_LEN];
7930Sstevel@tonic-gate 	char apstat2[CFGA_PHYS_EXT_LEN];
7940Sstevel@tonic-gate 	char *sep1, *sep2;
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	/*
7970Sstevel@tonic-gate 	 * Extract static ap_ids
7980Sstevel@tonic-gate 	 */
7990Sstevel@tonic-gate 	(void) strlcpy(apstat1, ap1, sizeof (apstat1));
8000Sstevel@tonic-gate 	(void) strlcpy(apstat2, ap2, sizeof (apstat2));
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	sep1 = GET_DYN(apstat1);
8030Sstevel@tonic-gate 	sep2 = GET_DYN(apstat2);
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	if (sep1)
8060Sstevel@tonic-gate 		*sep1 = '\0';
8070Sstevel@tonic-gate 	if (sep2)
8080Sstevel@tonic-gate 		*sep2 = '\0';
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	/*
8110Sstevel@tonic-gate 	 * Use the default comparator for static ap_ids
8120Sstevel@tonic-gate 	 */
8130Sstevel@tonic-gate 	ret = default_ap_id_cmp(apstat1, apstat2);
8140Sstevel@tonic-gate 	if (ret)
8150Sstevel@tonic-gate 		return (ret);
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	/*
8180Sstevel@tonic-gate 	 * static components match. They belong to
8190Sstevel@tonic-gate 	 * the same static ap_id. Check if both are dynamic
8200Sstevel@tonic-gate 	 * If not, static < dynamic.
8210Sstevel@tonic-gate 	 */
8220Sstevel@tonic-gate 	if ((sep1 == NULL) ^ (sep2 == NULL))
8230Sstevel@tonic-gate 		return (sep1 ? 1 : -1);
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	/*
8260Sstevel@tonic-gate 	 * If both are static, then ap1 = ap2
8270Sstevel@tonic-gate 	 */
8280Sstevel@tonic-gate 	if (sep1 == NULL)
8290Sstevel@tonic-gate 		return (0);
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	/*
8320Sstevel@tonic-gate 	 * Both are dynamic and belong to same static ap_id.
8330Sstevel@tonic-gate 	 * Use the plugin comparator
8340Sstevel@tonic-gate 	 */
8350Sstevel@tonic-gate 	libloc.libp = NULL;
8360Sstevel@tonic-gate 	if (config_get_lib(ap1, &libloc, NULL) != CFGA_OK) {
8370Sstevel@tonic-gate 		return (strncmp(sep1, sep2, CFGA_PHYS_EXT_LEN));
8380Sstevel@tonic-gate 	}
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 	ret = (*libloc.libp->cfga_ap_id_cmp_p)(ap1, ap2);
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	rele_lib(libloc.libp);
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	return (ret);
8450Sstevel@tonic-gate }
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate /*
8480Sstevel@tonic-gate  * config_strerror
8490Sstevel@tonic-gate  */
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate const char *
8520Sstevel@tonic-gate config_strerror(cfga_err_t cfgerrnum)
8530Sstevel@tonic-gate {
8540Sstevel@tonic-gate 	const char *ep = NULL;
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 	if ((cfgerrnum < CFGA_OK) || (cfgerrnum > CFGA_ATTR_INVAL))
8570Sstevel@tonic-gate 		return (NULL);
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	ep = __config_strerror(cfgerrnum);
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	return ((ep != NULL) ? dgettext(TEXT_DOMAIN, ep) : NULL);
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate /*
8650Sstevel@tonic-gate  * config_help
8660Sstevel@tonic-gate  */
8670Sstevel@tonic-gate cfga_err_t
8680Sstevel@tonic-gate config_help(
8690Sstevel@tonic-gate 	int num_ap_ids,
8700Sstevel@tonic-gate 	char *const *ap_ids,
8710Sstevel@tonic-gate 	struct cfga_msg *msgp,
8720Sstevel@tonic-gate 	const char *options,
8730Sstevel@tonic-gate 	cfga_flags_t flags)
8740Sstevel@tonic-gate {
8750Sstevel@tonic-gate 	int i;
8760Sstevel@tonic-gate 	lib_loc_t libloc;
8770Sstevel@tonic-gate 	cfga_err_t retval = CFGA_OK;
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	if (check_flags(flags, CFGA_FLAG_FORCE | CFGA_FLAG_VERBOSE, NULL)
8800Sstevel@tonic-gate 	    != CFGA_OK) {
8810Sstevel@tonic-gate 		return (CFGA_ERROR);
8820Sstevel@tonic-gate 	}
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	if (num_ap_ids < 0) {
8850Sstevel@tonic-gate 		return (CFGA_ERROR);
8860Sstevel@tonic-gate 	}
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	if (num_ap_ids > 0 && ap_ids == NULL) {
8890Sstevel@tonic-gate 		return (CFGA_ERROR);
8900Sstevel@tonic-gate 	}
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	/*
8930Sstevel@tonic-gate 	 * operate on each ap_id
8940Sstevel@tonic-gate 	 */
8950Sstevel@tonic-gate 	for (i = 0; (i < num_ap_ids) && (retval == CFGA_OK); i++) {
8960Sstevel@tonic-gate 		libloc.libp = NULL;
8970Sstevel@tonic-gate 		if ((retval = config_get_lib(ap_ids[i], &libloc,
8980Sstevel@tonic-gate 		    NULL)) != CFGA_OK) {
8990Sstevel@tonic-gate 			return (retval);
9000Sstevel@tonic-gate 		}
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 		errno = 0;
9030Sstevel@tonic-gate 		retval = (*libloc.libp->cfga_help_p)(msgp, options, flags);
9040Sstevel@tonic-gate 		rele_lib(libloc.libp);
9050Sstevel@tonic-gate 	}
9060Sstevel@tonic-gate 	return (retval);
9070Sstevel@tonic-gate }
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate /*
9100Sstevel@tonic-gate  * Private support routines for the public interfaces
9110Sstevel@tonic-gate  */
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate static const char *
9140Sstevel@tonic-gate __config_strerror(cfga_err_t cfgerrnum)
9150Sstevel@tonic-gate {
9160Sstevel@tonic-gate 	const char *ep = NULL;
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	switch (cfgerrnum) {
9190Sstevel@tonic-gate 	case CFGA_OK:
9200Sstevel@tonic-gate 		ep = "Configuration operation succeeded";
9210Sstevel@tonic-gate 		break;
9220Sstevel@tonic-gate 	case CFGA_NACK:
9230Sstevel@tonic-gate 		ep = "Configuration operation cancelled";
9240Sstevel@tonic-gate 		break;
9250Sstevel@tonic-gate 	case CFGA_INVAL:
9260Sstevel@tonic-gate 		ep = "Configuration operation invalid";
9270Sstevel@tonic-gate 		break;
9280Sstevel@tonic-gate 	case CFGA_NOTSUPP:
9290Sstevel@tonic-gate 		ep = "Configuration administration not supported";
9300Sstevel@tonic-gate 		break;
9310Sstevel@tonic-gate 	case CFGA_OPNOTSUPP:
9320Sstevel@tonic-gate 		ep = "Configuration operation not supported";
9330Sstevel@tonic-gate 		break;
9340Sstevel@tonic-gate 	case CFGA_PRIV:
9350Sstevel@tonic-gate 		ep = "Insufficient privileges";
9360Sstevel@tonic-gate 		break;
9370Sstevel@tonic-gate 	case CFGA_BUSY:
9380Sstevel@tonic-gate 		ep = "Component system is busy, try again";
9390Sstevel@tonic-gate 		break;
9400Sstevel@tonic-gate 	case CFGA_SYSTEM_BUSY:
9410Sstevel@tonic-gate 		ep = "System is busy, try again";
9420Sstevel@tonic-gate 		break;
9430Sstevel@tonic-gate 	case CFGA_DATA_ERROR:
9440Sstevel@tonic-gate 		ep = "Data error";
9450Sstevel@tonic-gate 		break;
9460Sstevel@tonic-gate 	case CFGA_LIB_ERROR:
9470Sstevel@tonic-gate 		ep = "Library error";
9480Sstevel@tonic-gate 		break;
9490Sstevel@tonic-gate 	case CFGA_NO_LIB:
9500Sstevel@tonic-gate 		ep = "No Library found";
9510Sstevel@tonic-gate 		break;
9520Sstevel@tonic-gate 	case CFGA_INSUFFICENT_CONDITION:
9530Sstevel@tonic-gate 		ep = "Insufficient condition";
9540Sstevel@tonic-gate 		break;
9550Sstevel@tonic-gate 	case CFGA_ERROR:
9560Sstevel@tonic-gate 		ep = "Hardware specific failure";
9570Sstevel@tonic-gate 		break;
9580Sstevel@tonic-gate 	case CFGA_APID_NOEXIST:
9590Sstevel@tonic-gate 		ep = "Attachment point not found";
9600Sstevel@tonic-gate 		break;
9610Sstevel@tonic-gate 	case CFGA_ATTR_INVAL:
9620Sstevel@tonic-gate 		ep = "No attachment point with specified attributes found";
9630Sstevel@tonic-gate 		break;
9640Sstevel@tonic-gate 	default:
9650Sstevel@tonic-gate 		ep = NULL;
9660Sstevel@tonic-gate 		break;
9670Sstevel@tonic-gate 	}
9680Sstevel@tonic-gate 	return (ep);
9690Sstevel@tonic-gate }
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate /*
9720Sstevel@tonic-gate  * listopts is a string in the getsubopt(3C) style:
9730Sstevel@tonic-gate  *	name1=value1,name2=value2,
9740Sstevel@tonic-gate  */
9750Sstevel@tonic-gate static cfga_err_t
9760Sstevel@tonic-gate parse_listopt(char *listopts, char **classpp, char **errstring)
9770Sstevel@tonic-gate {
9780Sstevel@tonic-gate 	char *bufp, *optp, *val = NULL;
9790Sstevel@tonic-gate 	cfga_err_t rc = CFGA_ERROR;
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	*classpp = NULL;
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	/*
9840Sstevel@tonic-gate 	 * NULL is a legal value for listopts
9850Sstevel@tonic-gate 	 */
9860Sstevel@tonic-gate 	if (listopts == NULL) {
9870Sstevel@tonic-gate 		return (CFGA_OK);
9880Sstevel@tonic-gate 	}
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	if ((bufp = config_calloc_check(1, strlen(listopts) + 1, errstring))
9910Sstevel@tonic-gate 	    == NULL) {
9920Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
9930Sstevel@tonic-gate 	}
9940Sstevel@tonic-gate 	(void) strcpy(bufp, listopts);
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	optp = bufp; /* getsubopt() modifies its argument */
9970Sstevel@tonic-gate 	while (*optp != '\0') {
9980Sstevel@tonic-gate 		switch (getsubopt(&optp, listopt_array, &val)) {
9990Sstevel@tonic-gate 		case LISTOPT_CLASS:
10000Sstevel@tonic-gate 			if (val == NULL || *classpp != NULL) {
10010Sstevel@tonic-gate 				rc = CFGA_ERROR;
10020Sstevel@tonic-gate 				goto out;
10030Sstevel@tonic-gate 			}
10040Sstevel@tonic-gate 			if ((*classpp = config_calloc_check(1, strlen(val) + 1,
10050Sstevel@tonic-gate 			    errstring)) == NULL) {
10060Sstevel@tonic-gate 				rc = CFGA_LIB_ERROR;
10070Sstevel@tonic-gate 				goto out;
10080Sstevel@tonic-gate 			}
10090Sstevel@tonic-gate 			(void) strcpy(*classpp, val);
10100Sstevel@tonic-gate 			break;
10110Sstevel@tonic-gate 		default:
10120Sstevel@tonic-gate 			rc = CFGA_ERROR;
10130Sstevel@tonic-gate 			goto out;
10140Sstevel@tonic-gate 		}
10150Sstevel@tonic-gate 	}
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	rc = CFGA_OK;
10180Sstevel@tonic-gate 	/*FALLTHRU*/
10190Sstevel@tonic-gate out:
10200Sstevel@tonic-gate 	S_FREE(bufp);
10210Sstevel@tonic-gate 	if (rc != CFGA_OK) {
10220Sstevel@tonic-gate 		S_FREE(*classpp);
10230Sstevel@tonic-gate 	}
10240Sstevel@tonic-gate 	return (rc);
10250Sstevel@tonic-gate }
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate /*ARGSUSED*/
10280Sstevel@tonic-gate static cfga_err_t
10290Sstevel@tonic-gate null_mklog(
10300Sstevel@tonic-gate 	di_node_t node,
10310Sstevel@tonic-gate 	di_minor_t minor,
10320Sstevel@tonic-gate 	plugin_lib_t *libp,
10330Sstevel@tonic-gate 	lib_loc_t *liblocp)
10340Sstevel@tonic-gate {
10350Sstevel@tonic-gate 	return (CFGA_OK);
10360Sstevel@tonic-gate }
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate static cfga_err_t
10390Sstevel@tonic-gate mklog_v1(
10400Sstevel@tonic-gate 	di_node_t node,
10410Sstevel@tonic-gate 	di_minor_t minor,
10420Sstevel@tonic-gate 	plugin_lib_t *libp,
10430Sstevel@tonic-gate 	lib_loc_t *liblocp)
10440Sstevel@tonic-gate {
10450Sstevel@tonic-gate 	const size_t len = CFGA_AP_LOG_ID_LEN;
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	assert(len <=  sizeof (liblocp->ap_logical));
10480Sstevel@tonic-gate 
10490Sstevel@tonic-gate 	if (libp->plugin_vers != CFGA_HSL_V1) {
10500Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
10510Sstevel@tonic-gate 	}
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	return (mklog_common(node, minor, liblocp, len));
10540Sstevel@tonic-gate }
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate /*
10580Sstevel@tonic-gate  * Obtain the devlink from a /devices path
10590Sstevel@tonic-gate  */
10600Sstevel@tonic-gate static int
10610Sstevel@tonic-gate get_link(di_devlink_t devlink, void *arg)
10620Sstevel@tonic-gate {
10630Sstevel@tonic-gate 	char *linkp = (char *)arg;
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 	(void) snprintf(linkp, CFGA_LOG_EXT_LEN, "%s",
10660Sstevel@tonic-gate 	    di_devlink_path(devlink));
10670Sstevel@tonic-gate 	return (DI_WALK_TERMINATE);
10680Sstevel@tonic-gate }
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate static cfga_err_t
10710Sstevel@tonic-gate mklog_v2(
10720Sstevel@tonic-gate 	di_node_t node,
10730Sstevel@tonic-gate 	di_minor_t minor,
10740Sstevel@tonic-gate 	plugin_lib_t *libp,
10750Sstevel@tonic-gate 	lib_loc_t *liblocp)
10760Sstevel@tonic-gate {
10770Sstevel@tonic-gate 	const size_t len = CFGA_LOG_EXT_LEN;
10780Sstevel@tonic-gate 	di_devlink_handle_t hdl;
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	assert(len <=  sizeof (liblocp->ap_logical));
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate 	if (libp->plugin_vers != CFGA_HSL_V2) {
10830Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
10840Sstevel@tonic-gate 	}
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	/* open devlink database */
10870Sstevel@tonic-gate 	if ((hdl = di_devlink_init(NULL, 0)) == NULL) {
10880Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
10890Sstevel@tonic-gate 	}
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	liblocp->ap_logical[0] = '\0';
10920Sstevel@tonic-gate 	(void) di_devlink_walk(hdl, NULL,
10930Sstevel@tonic-gate 	    liblocp->ap_physical + strlen(DEVICES_DIR),
10940Sstevel@tonic-gate 	    DI_PRIMARY_LINK, (void *)liblocp->ap_logical, get_link);
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 	(void) di_devlink_fini(&hdl);
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	if (liblocp->ap_logical[0] != '\0')
10990Sstevel@tonic-gate 		return (CFGA_OK);
11000Sstevel@tonic-gate 	return (mklog_common(node, minor, liblocp, len));
11010Sstevel@tonic-gate }
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate /*
11040Sstevel@tonic-gate  * mklog_common - make a logical name from the driver and instance
11050Sstevel@tonic-gate  */
11060Sstevel@tonic-gate static cfga_err_t
11070Sstevel@tonic-gate mklog_common(
11080Sstevel@tonic-gate 	di_node_t node,
11090Sstevel@tonic-gate 	di_minor_t minor,
11100Sstevel@tonic-gate 	lib_loc_t *libloc_p,
11110Sstevel@tonic-gate 	size_t len)
11120Sstevel@tonic-gate {
11130Sstevel@tonic-gate 	int inst;
11140Sstevel@tonic-gate 	char *drv, *minor_name;
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	drv = di_driver_name(node);
11170Sstevel@tonic-gate 	inst = di_instance(node);
11180Sstevel@tonic-gate 	minor_name = di_minor_name(minor);
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 	errno = 0;
11210Sstevel@tonic-gate 	if (drv != NULL && inst != -1 && minor_name != NULL &&
11220Sstevel@tonic-gate 	    snprintf(libloc_p->ap_logical, len, "%s%d:%s", drv, inst,
11230Sstevel@tonic-gate 	    minor_name) < len) {	/* snprintf returns strlen */
11240Sstevel@tonic-gate 		return (CFGA_OK);
11250Sstevel@tonic-gate 	}
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	return (CFGA_LIB_ERROR);
11280Sstevel@tonic-gate }
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate /*
11310Sstevel@tonic-gate  * resolve_lib_ref - relocate to use plugin lib
11320Sstevel@tonic-gate  */
11330Sstevel@tonic-gate static cfga_err_t
11340Sstevel@tonic-gate resolve_lib_ref(
11350Sstevel@tonic-gate 	plugin_lib_t *libp,
11360Sstevel@tonic-gate 	lib_loc_t *libloc_p)
11370Sstevel@tonic-gate {
11380Sstevel@tonic-gate 	void *sym;
11390Sstevel@tonic-gate 	void *libhdlp = libp->handle;
11400Sstevel@tonic-gate 	int plug_vers;
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	if ((sym = dlsym(libhdlp, "cfga_version")) == NULL) {
11430Sstevel@tonic-gate 		/*
11440Sstevel@tonic-gate 		 * Version symbol not defined, must be the first version
11450Sstevel@tonic-gate 		 */
11460Sstevel@tonic-gate 		plug_vers = CFGA_HSL_V1;
11470Sstevel@tonic-gate 	} else {
11480Sstevel@tonic-gate 		plug_vers =   *((int *)sym);
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	/*
11520Sstevel@tonic-gate 	 * Check if plugin version matches request.
11530Sstevel@tonic-gate 	 */
11540Sstevel@tonic-gate 	if (!compat_plugin(&libloc_p->vers_req, plug_vers)) {
11550Sstevel@tonic-gate 		return (CFGA_NO_LIB);
11560Sstevel@tonic-gate 	}
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	/*
11590Sstevel@tonic-gate 	 * Record the plugin version and setup version dependant routines
11600Sstevel@tonic-gate 	 */
11610Sstevel@tonic-gate 	assert(plug_vers < VERS_ARRAY_SZ);
11620Sstevel@tonic-gate 	libp->plugin_vers = plug_vers;
11630Sstevel@tonic-gate 	libp->vers_ops = &cfga_vers_ops[plug_vers];
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate 	/* resolve symbols common to all versions */
11660Sstevel@tonic-gate 	if ((sym = dlsym(libhdlp, "cfga_change_state")) == NULL) {
11670Sstevel@tonic-gate 		perror("dlsym: cfga_change_state");
11680Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
11690Sstevel@tonic-gate 	} else
11700Sstevel@tonic-gate 		libp->cfga_change_state_p = (cfga_err_t (*)(cfga_cmd_t,
11710Sstevel@tonic-gate 		    const char *, const char *, struct cfga_confirm *,
11720Sstevel@tonic-gate 		    struct cfga_msg *, char **, cfga_flags_t)) sym;
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 	if ((sym = dlsym(libhdlp, "cfga_private_func")) == NULL) {
11750Sstevel@tonic-gate 		perror("dlsym: cfga_private_func");
11760Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
11770Sstevel@tonic-gate 	} else
11780Sstevel@tonic-gate 		libp->cfga_private_func_p = (cfga_err_t (*)(const char *,
11790Sstevel@tonic-gate 		    const char *, const char *, struct cfga_confirm *,
11800Sstevel@tonic-gate 		    struct cfga_msg *, char **, cfga_flags_t))sym;
11810Sstevel@tonic-gate 
11820Sstevel@tonic-gate 	if ((sym = dlsym(libhdlp, "cfga_test")) == NULL) {
11830Sstevel@tonic-gate 		perror("dlsym: cfga_test");
11840Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
11850Sstevel@tonic-gate 	} else
11860Sstevel@tonic-gate 		libp->cfga_test_p = (cfga_err_t (*)(const char *, const char *,
11870Sstevel@tonic-gate 		    struct cfga_msg *, char **, cfga_flags_t))sym;
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate 	if ((sym = dlsym(libhdlp, "cfga_help")) == NULL) {
11900Sstevel@tonic-gate 		perror("dlsym: cfga_help");
11910Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
11920Sstevel@tonic-gate 	} else
11930Sstevel@tonic-gate 		libp->cfga_help_p = (cfga_err_t (*)(struct cfga_msg *,
11940Sstevel@tonic-gate 		    const char *, cfga_flags_t))sym;
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	if ((sym = dlsym(libhdlp, "cfga_ap_id_cmp")) == NULL) {
11970Sstevel@tonic-gate 		libp->cfga_ap_id_cmp_p = default_ap_id_cmp;
11980Sstevel@tonic-gate 	} else
11990Sstevel@tonic-gate 		libp->cfga_ap_id_cmp_p = (int (*)(const
12000Sstevel@tonic-gate 		    cfga_ap_log_id_t, const cfga_ap_log_id_t))sym;
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 	/* Resolve version specific symbols */
12030Sstevel@tonic-gate 	return (libp->vers_ops->resolve_lib(libp));
12040Sstevel@tonic-gate }
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate /*ARGSUSED*/
12070Sstevel@tonic-gate static cfga_err_t
12080Sstevel@tonic-gate null_resolve(plugin_lib_t *libp)
12090Sstevel@tonic-gate {
12100Sstevel@tonic-gate 	return (CFGA_OK);
12110Sstevel@tonic-gate }
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate static cfga_err_t
12140Sstevel@tonic-gate resolve_v1(plugin_lib_t *libp)
12150Sstevel@tonic-gate {
12160Sstevel@tonic-gate 	void *sym, *libhdlp = libp->handle;
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 	if (libp->plugin_vers != CFGA_HSL_V1) {
12200Sstevel@tonic-gate 		return (CFGA_NO_LIB);
12210Sstevel@tonic-gate 	}
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 	if ((sym = dlsym(libhdlp, "cfga_stat")) == NULL) {
12240Sstevel@tonic-gate 		perror("dlsym: cfga_stat");
12250Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
12260Sstevel@tonic-gate 	} else
12270Sstevel@tonic-gate 		libp->cfga_stat_p = (cfga_err_t (*)(const char *,
12280Sstevel@tonic-gate 		    struct cfga_stat_data *, const char *,
12290Sstevel@tonic-gate 		    char **))sym;
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 	if ((sym = dlsym(libhdlp, "cfga_list")) == NULL) {
12320Sstevel@tonic-gate 		perror("dlsym: cfga_list");
12330Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
12340Sstevel@tonic-gate 	} else
12350Sstevel@tonic-gate 		libp->cfga_list_p = (cfga_err_t (*)(struct cfga_stat_data **,
12360Sstevel@tonic-gate 		    int *, const char *, char **))sym;
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	return (CFGA_OK);
12390Sstevel@tonic-gate }
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate static cfga_err_t
12420Sstevel@tonic-gate resolve_v2(plugin_lib_t *libp)
12430Sstevel@tonic-gate {
12440Sstevel@tonic-gate 	void *sym;
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 	if (libp->plugin_vers != CFGA_HSL_V2) {
12480Sstevel@tonic-gate 		return (CFGA_NO_LIB);
12490Sstevel@tonic-gate 	}
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	if ((sym = dlsym(libp->handle, "cfga_list_ext")) == NULL) {
12520Sstevel@tonic-gate 		perror("dlsym: cfga_list_ext");
12530Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
12540Sstevel@tonic-gate 	} else {
12550Sstevel@tonic-gate 		libp->cfga_list_ext_p = (cfga_err_t (*)(const char *,
12560Sstevel@tonic-gate 		    struct cfga_list_data **, int *, const char *,
12570Sstevel@tonic-gate 		    const char *, char **, cfga_flags_t))sym;
12580Sstevel@tonic-gate 		return (CFGA_OK);
12590Sstevel@tonic-gate 	}
12600Sstevel@tonic-gate }
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate /*
12630Sstevel@tonic-gate  * config_calloc_check - perform allocation, check result and
12640Sstevel@tonic-gate  * set error string
12650Sstevel@tonic-gate  */
12660Sstevel@tonic-gate static void *
12670Sstevel@tonic-gate config_calloc_check(
12680Sstevel@tonic-gate 	size_t nelem,
12690Sstevel@tonic-gate 	size_t elsize,
12700Sstevel@tonic-gate 	char **errstring)
12710Sstevel@tonic-gate {
12720Sstevel@tonic-gate 	void *p;
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 	p = calloc(nelem, elsize);
12750Sstevel@tonic-gate 	if (p == NULL) {
12760Sstevel@tonic-gate 		config_err(0, ALLOC_FAILED, errstring);
12770Sstevel@tonic-gate 	}
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 	return (p);
12800Sstevel@tonic-gate }
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 
12830Sstevel@tonic-gate /*
12840Sstevel@tonic-gate  * config_get_lib - given an ap_id find the library name
12850Sstevel@tonic-gate  *	If successful, the plugin library is held.
12860Sstevel@tonic-gate  */
12870Sstevel@tonic-gate static cfga_err_t
12880Sstevel@tonic-gate config_get_lib(
12890Sstevel@tonic-gate 	const char *ap_id,
12900Sstevel@tonic-gate 	lib_loc_t *lib_loc_p,
12910Sstevel@tonic-gate 	char **errstring)
12920Sstevel@tonic-gate {
12930Sstevel@tonic-gate 	char *dyncomp, path[PATH_MAX];
12940Sstevel@tonic-gate 	char *apdup;
12950Sstevel@tonic-gate 	cfga_ap_types_t type = UNKNOWN_AP;
12960Sstevel@tonic-gate 	cfga_err_t ret = CFGA_ERROR;
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 	if (ap_id == NULL) {
12990Sstevel@tonic-gate 		config_err(0, INVALID_ARGS, errstring);
13000Sstevel@tonic-gate 		return (ret);
13010Sstevel@tonic-gate 	}
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 	lib_loc_p->libp = NULL;
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 	if ((apdup = config_calloc_check(1, strlen(ap_id) + 1, errstring))
13060Sstevel@tonic-gate 	    == NULL) {
13070Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
13080Sstevel@tonic-gate 	}
13090Sstevel@tonic-gate 	(void) strcpy(apdup, ap_id);
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	/*
13120Sstevel@tonic-gate 	 * Separate into base and dynamic components
13130Sstevel@tonic-gate 	 */
13140Sstevel@tonic-gate 	if ((ret = split_apid(apdup, &dyncomp, errstring)) != CFGA_OK) {
13150Sstevel@tonic-gate 		goto out;
13160Sstevel@tonic-gate 	}
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 	/*
13190Sstevel@tonic-gate 	 * No upper limit on version
13200Sstevel@tonic-gate 	 */
13210Sstevel@tonic-gate 	lib_loc_p->vers_req.v_max = CFGA_HSL_VERS;
13220Sstevel@tonic-gate 	if (dyncomp != NULL) {
13230Sstevel@tonic-gate 		/*
13240Sstevel@tonic-gate 		 * We need atleast version 2 of the plug-in library
13250Sstevel@tonic-gate 		 * interface since the ap_id has a dynamic component.
13260Sstevel@tonic-gate 		 */
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate 		lib_loc_p->vers_req.v_min = CFGA_HSL_V2;
13290Sstevel@tonic-gate 	} else {
13300Sstevel@tonic-gate 		lib_loc_p->vers_req.v_min = CFGA_HSL_V1;
13310Sstevel@tonic-gate 	}
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 	/*
13340Sstevel@tonic-gate 	 * If the ap_id is a devlink in CFGA_DEV_DIR, follow link
13350Sstevel@tonic-gate 	 * to get the physical ap_id.
13360Sstevel@tonic-gate 	 */
13370Sstevel@tonic-gate 	if ((type = find_arg_type(apdup)) == LOGICAL_LINK_AP) {
13380Sstevel@tonic-gate 		(void) snprintf(lib_loc_p->ap_base, sizeof (lib_loc_p->ap_base),
13390Sstevel@tonic-gate 		    "%s%s", CFGA_DEV_DIR SLASH, apdup);
13400Sstevel@tonic-gate 	}
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 	path[sizeof (path) - 1] = '\0';
13430Sstevel@tonic-gate 	if (type == LOGICAL_LINK_AP && realpath(lib_loc_p->ap_base, path)
13440Sstevel@tonic-gate 	    != NULL) {
13450Sstevel@tonic-gate 		(void) snprintf(lib_loc_p->ap_base, sizeof (lib_loc_p->ap_base),
13460Sstevel@tonic-gate 		    "%s", path);
13470Sstevel@tonic-gate 	} else {
13480Sstevel@tonic-gate 		(void) snprintf(lib_loc_p->ap_base, sizeof (lib_loc_p->ap_base),
13490Sstevel@tonic-gate 		    "%s", apdup);
13500Sstevel@tonic-gate 	}
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 	/*
13540Sstevel@tonic-gate 	 * find and load the library
13550Sstevel@tonic-gate 	 * The base component of the ap_id is used to locate the plug-in
13560Sstevel@tonic-gate 	 */
13570Sstevel@tonic-gate 	if ((type = find_arg_type(lib_loc_p->ap_base)) == PHYSICAL_AP) {
13580Sstevel@tonic-gate 		/*
13590Sstevel@tonic-gate 		 * physical ap_id: Use ap_base as root for tree walk
13600Sstevel@tonic-gate 		 * A link based apid (logical) will resolve to a physical
13610Sstevel@tonic-gate 		 * ap_id.
13620Sstevel@tonic-gate 		 */
13630Sstevel@tonic-gate 		ret = find_ap_common(lib_loc_p, lib_loc_p->ap_base,
13640Sstevel@tonic-gate 		    check_ap_phys, errstring);
13650Sstevel@tonic-gate 	} else if ((type == LOGICAL_DRV_AP) ||
13660Sstevel@tonic-gate 	    (type == AP_TYPE && dyncomp == NULL)) {
13670Sstevel@tonic-gate 		/*
13680Sstevel@tonic-gate 		 * logical ap_id or ap_type: Use "/" as root for tree walk
13690Sstevel@tonic-gate 		 * Note: an aptype cannot have a dynamic component
13700Sstevel@tonic-gate 		 */
13710Sstevel@tonic-gate 		ret = find_ap_common(lib_loc_p, "/", check_ap, errstring);
13720Sstevel@tonic-gate 	} else {
13730Sstevel@tonic-gate 		ret = CFGA_APID_NOEXIST;
13740Sstevel@tonic-gate 	}
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate 	if (ret == CFGA_OK) {
13770Sstevel@tonic-gate #ifndef	NDEBUG
13780Sstevel@tonic-gate 		/*
13790Sstevel@tonic-gate 		 * variables used by assert() only which is disabled
13800Sstevel@tonic-gate 		 * by defining NDEBUG (see top of this file)
13810Sstevel@tonic-gate 		 */
13820Sstevel@tonic-gate 		plugin_lib_t *libp;
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 		libp = lib_loc_p->libp;
13850Sstevel@tonic-gate #endif	/* NDEBUG */
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 		assert(strcmp(libp->libpath, lib_loc_p->pathname) == 0);
13880Sstevel@tonic-gate 		assert(VALID_HSL_VERS(libp->plugin_vers));
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 		/*
13910Sstevel@tonic-gate 		 * If a dynamic component was present, v1 plug-ins are not
13920Sstevel@tonic-gate 		 * acceptable.
13930Sstevel@tonic-gate 		 */
13940Sstevel@tonic-gate 		assert(dyncomp == NULL || libp->plugin_vers >= CFGA_HSL_V2);
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 		/*
13970Sstevel@tonic-gate 		 * ap_physical is passed to plugins as their ap_id argument.
13980Sstevel@tonic-gate 		 * Append dynamic component if any.
13990Sstevel@tonic-gate 		 */
14000Sstevel@tonic-gate 		append_dyn(lib_loc_p->ap_physical, dyncomp,
14010Sstevel@tonic-gate 		    sizeof (lib_loc_p->ap_physical));
14020Sstevel@tonic-gate 	}
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 	/* cleanup */
14050Sstevel@tonic-gate 	lib_loc_p->vers_req.v_min = INVALID_VERSION;
14060Sstevel@tonic-gate 	lib_loc_p->vers_req.v_max = INVALID_VERSION;
14070Sstevel@tonic-gate 	*lib_loc_p->ap_base = '\0';
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 	/*FALLTHRU*/
14100Sstevel@tonic-gate out:
14110Sstevel@tonic-gate 	S_FREE(apdup);
14120Sstevel@tonic-gate 	S_FREE(dyncomp);
14130Sstevel@tonic-gate 	if (ret != CFGA_OK) {
14140Sstevel@tonic-gate 		lib_loc_p->libp = NULL;
14150Sstevel@tonic-gate 	}
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 	assert(ret != CFGA_OK || lib_loc_p->libp != NULL);
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	return (ret);
14200Sstevel@tonic-gate }
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate /*
14240Sstevel@tonic-gate  * load_lib - Given a library pathname, create a entry for it
14250Sstevel@tonic-gate  * in the library list, if one does not already exist, and read
14260Sstevel@tonic-gate  * lock it to keep it there.
14270Sstevel@tonic-gate  */
14280Sstevel@tonic-gate static cfga_err_t
14290Sstevel@tonic-gate load_lib(
14300Sstevel@tonic-gate 	di_node_t node,
14310Sstevel@tonic-gate 	di_minor_t minor,
14320Sstevel@tonic-gate 	lib_loc_t *libloc_p)
14330Sstevel@tonic-gate {
14340Sstevel@tonic-gate 	plugin_lib_t *libp, *list_libp;
14350Sstevel@tonic-gate 	char *devfs_path;
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate 	/*
14380Sstevel@tonic-gate 	 * lock the library list
14390Sstevel@tonic-gate 	 */
14400Sstevel@tonic-gate 	(void) mutex_lock(&plugin_list_lock);
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate 	/*
14430Sstevel@tonic-gate 	 * see if lib exist in list, if not, allocate a new one
14440Sstevel@tonic-gate 	 */
14450Sstevel@tonic-gate 	list_libp = lib_in_list(libloc_p->pathname);
14460Sstevel@tonic-gate 	if (list_libp != NULL) {
14470Sstevel@tonic-gate 		hold_lib(list_libp);
14480Sstevel@tonic-gate 		(void) mutex_unlock(&plugin_list_lock);
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate 		/* fill in logical and physical name in libloc_p */
14510Sstevel@tonic-gate 		libloc_p->libp = libp = list_libp;
14520Sstevel@tonic-gate 		if (libp->vers_ops->mklog(node, minor, libp, libloc_p)
14530Sstevel@tonic-gate 		    != CFGA_OK) {
14540Sstevel@tonic-gate 			rele_lib(list_libp);
14550Sstevel@tonic-gate 			return (CFGA_LIB_ERROR);
14560Sstevel@tonic-gate 		}
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate 		devfs_path = di_devfs_path(node);
14590Sstevel@tonic-gate 		(void) snprintf(libloc_p->ap_physical, MAXPATHLEN, "%s%s:%s",
14600Sstevel@tonic-gate 		    DEVICES_DIR, devfs_path, di_minor_name(minor));
14610Sstevel@tonic-gate 		di_devfs_path_free(devfs_path);
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate 		return (CFGA_OK);
14640Sstevel@tonic-gate 	}
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate 	/* allocate a new plugin_lib_t structure */
14670Sstevel@tonic-gate 	libp = config_calloc_check(1, sizeof (plugin_lib_t), NULL);
14680Sstevel@tonic-gate 	if (libp == NULL) {
14690Sstevel@tonic-gate 		(void) mutex_unlock(&plugin_list_lock);
14700Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
14710Sstevel@tonic-gate 	}
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 	(void) snprintf(libp->libpath, sizeof (libp->libpath), "%s",
14740Sstevel@tonic-gate 	    libloc_p->pathname);
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 	/*
14770Sstevel@tonic-gate 	 * ensure that the lib is open and linked in
14780Sstevel@tonic-gate 	 */
14790Sstevel@tonic-gate 	libp->handle = dlopen(libp->libpath, RTLD_NOW);
14800Sstevel@tonic-gate 	if (libp->handle == NULL) {
14810Sstevel@tonic-gate 		(void) mutex_unlock(&plugin_list_lock);
14820Sstevel@tonic-gate 		free(libp);
14830Sstevel@tonic-gate 		return (CFGA_NO_LIB);
14840Sstevel@tonic-gate 	}
14850Sstevel@tonic-gate 
14860Sstevel@tonic-gate 	if (resolve_lib_ref(libp, libloc_p) != CFGA_OK ||
14870Sstevel@tonic-gate 	    libp->vers_ops->mklog(node, minor, libp, libloc_p) != CFGA_OK) {
14880Sstevel@tonic-gate 		(void) mutex_unlock(&plugin_list_lock);
14890Sstevel@tonic-gate 		(void) dlclose(libp->handle);
14900Sstevel@tonic-gate 		free(libp);
14910Sstevel@tonic-gate 		return (CFGA_NO_LIB);
14920Sstevel@tonic-gate 	}
14930Sstevel@tonic-gate 
14940Sstevel@tonic-gate 	/*
14950Sstevel@tonic-gate 	 * link in new entry to the end of list
14960Sstevel@tonic-gate 	 */
14970Sstevel@tonic-gate 	list_libp = &plugin_list;
14980Sstevel@tonic-gate 	while (list_libp->next != NULL)
14990Sstevel@tonic-gate 		list_libp = list_libp->next;
15000Sstevel@tonic-gate 	libp->next = list_libp->next;
15010Sstevel@tonic-gate 	list_libp->next = libp;
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 	/* Initialize refcnt to 1 */
15040Sstevel@tonic-gate 	libp->refcnt = 1;
15050Sstevel@tonic-gate 	(void) mutex_init(&libp->lock, USYNC_THREAD, NULL);
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 	(void) mutex_unlock(&plugin_list_lock);
15080Sstevel@tonic-gate 
15090Sstevel@tonic-gate 	/*
15100Sstevel@tonic-gate 	 * record libp and physical node name in the libloc struct
15110Sstevel@tonic-gate 	 */
15120Sstevel@tonic-gate 	libloc_p->libp = libp;
15130Sstevel@tonic-gate 	devfs_path = di_devfs_path(node);
15140Sstevel@tonic-gate 	(void) snprintf(libloc_p->ap_physical, MAXPATHLEN, "%s%s:%s",
15150Sstevel@tonic-gate 	    DEVICES_DIR, devfs_path, di_minor_name(minor));
15160Sstevel@tonic-gate 	di_devfs_path_free(devfs_path);
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate 	return (CFGA_OK);
15190Sstevel@tonic-gate }
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate #define	NUM_LIB_NAMES   2
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate /*
15250Sstevel@tonic-gate  * find_lib - Given an attachment point node find it's library
15260Sstevel@tonic-gate  *
15270Sstevel@tonic-gate  */
15280Sstevel@tonic-gate static cfga_err_t
15290Sstevel@tonic-gate find_lib(
15300Sstevel@tonic-gate 	di_node_t node,
15310Sstevel@tonic-gate 	di_minor_t minor,
15320Sstevel@tonic-gate 	lib_loc_t *libloc_p)
15330Sstevel@tonic-gate {
15340Sstevel@tonic-gate 	char lib[MAXPATHLEN];
15350Sstevel@tonic-gate 	char name[NUM_LIB_NAMES][MAXPATHLEN];
15360Sstevel@tonic-gate 	struct stat lib_stat;
15370Sstevel@tonic-gate 	void *dlhandle = NULL;
15380Sstevel@tonic-gate 	static char machine_name[SYSINFO_LENGTH];
15390Sstevel@tonic-gate 	static char arch_name[SYSINFO_LENGTH];
15400Sstevel@tonic-gate 	int i = 0;
15410Sstevel@tonic-gate 	char *class = NULL, *drv = NULL;
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	/* Make sure pathname and class is null if we fail */
15450Sstevel@tonic-gate 	*libloc_p->ap_class = *libloc_p->pathname = *lib = '\0';
15460Sstevel@tonic-gate 
15470Sstevel@tonic-gate 	/*
15480Sstevel@tonic-gate 	 * Initialize machine name and arch name
15490Sstevel@tonic-gate 	 */
15500Sstevel@tonic-gate 	if (strncmp("", machine_name, MAXPATHLEN) == 0) {
15510Sstevel@tonic-gate 		if (sysinfo(SI_MACHINE, machine_name, SYSINFO_LENGTH) == -1) {
15520Sstevel@tonic-gate 			return (CFGA_ERROR);
15530Sstevel@tonic-gate 		}
15540Sstevel@tonic-gate 		if (sysinfo(SI_ARCHITECTURE, arch_name, SYSINFO_LENGTH) == -1) {
15550Sstevel@tonic-gate 			return (CFGA_ERROR);
15560Sstevel@tonic-gate 		}
15570Sstevel@tonic-gate 	}
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 	/*
15600Sstevel@tonic-gate 	 * Initialize possible library tags.
15610Sstevel@tonic-gate 	 */
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 	drv = di_driver_name(node);
15640Sstevel@tonic-gate 	class = get_class(minor);
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 	if (drv == NULL || class == NULL) {
15670Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
15680Sstevel@tonic-gate 	}
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 	i = 0;
15710Sstevel@tonic-gate 	(void) snprintf(&name[i++][0], sizeof (name[0]), "%s", drv);
15720Sstevel@tonic-gate 	(void) snprintf(&name[i++][0], sizeof (name[0]), "%s", class);
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	/*
15750Sstevel@tonic-gate 	 * Cycle through the array of names to find the library.
15760Sstevel@tonic-gate 	 */
15770Sstevel@tonic-gate 	for (i = 0; i < NUM_LIB_NAMES; i++) {
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 		/* Attachment points may not have a class (i.e. are generic) */
15800Sstevel@tonic-gate 		if (name[i][0] == '\0') {
15810Sstevel@tonic-gate 			continue;
15820Sstevel@tonic-gate 		}
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 		/*
15850Sstevel@tonic-gate 		 * Try path based upon machine name
15860Sstevel@tonic-gate 		 */
15870Sstevel@tonic-gate 		(void) snprintf(lib, sizeof (lib), "%s%s%s%s%s",
15880Sstevel@tonic-gate 		    LIB_PATH_BASE1, machine_name, LIB_PATH_MIDDLE,
15890Sstevel@tonic-gate 		    name[i], LIB_PATH_TAIL);
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 		if (stat(lib, &lib_stat) == 0) {
15930Sstevel@tonic-gate 			/* file exists, is it a lib */
15940Sstevel@tonic-gate 			dlhandle = dlopen(lib, RTLD_LAZY);
15950Sstevel@tonic-gate 			if (dlhandle != NULL) {
15960Sstevel@tonic-gate 				goto found;
15970Sstevel@tonic-gate 			}
15980Sstevel@tonic-gate 		}
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 		/*
16010Sstevel@tonic-gate 		 * Try path based upon arch name
16020Sstevel@tonic-gate 		 */
16030Sstevel@tonic-gate 		(void) snprintf(lib, sizeof (lib), "%s%s%s%s%s",
16040Sstevel@tonic-gate 		    LIB_PATH_BASE1, arch_name, LIB_PATH_MIDDLE,
16050Sstevel@tonic-gate 		    name[i], LIB_PATH_TAIL);
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate 		if (stat(lib, &lib_stat) == 0) {
16080Sstevel@tonic-gate 			/* file exists, is it a lib */
16090Sstevel@tonic-gate 			dlhandle = dlopen(lib, RTLD_LAZY);
16100Sstevel@tonic-gate 			if (dlhandle != NULL) {
16110Sstevel@tonic-gate 				goto found;
16120Sstevel@tonic-gate 			}
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 		}
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate 		/*
16170Sstevel@tonic-gate 		 * Try generic location
16180Sstevel@tonic-gate 		 */
16190Sstevel@tonic-gate 		(void) snprintf(lib, sizeof (lib), "%s%s%s%s",
16200Sstevel@tonic-gate 		    LIB_PATH_BASE2, LIB_PATH_MIDDLE, name[i], LIB_PATH_TAIL);
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate 		if (stat(lib, &lib_stat) == 0) {
16250Sstevel@tonic-gate 			/* file exists, is it a lib */
16260Sstevel@tonic-gate 			dlhandle = dlopen(lib, RTLD_LAZY);
16270Sstevel@tonic-gate 			if (dlhandle != NULL) {
16280Sstevel@tonic-gate 				goto found;
16290Sstevel@tonic-gate 			}
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 		}
16320Sstevel@tonic-gate 	}
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 
16350Sstevel@tonic-gate 	return (CFGA_NO_LIB);
16360Sstevel@tonic-gate 
16370Sstevel@tonic-gate found:
16380Sstevel@tonic-gate 	/* we got one! */
16390Sstevel@tonic-gate 	(void) snprintf(libloc_p->pathname, sizeof (libloc_p->pathname), "%s",
16400Sstevel@tonic-gate 	    lib);
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate 	(void) dlclose(dlhandle);
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate 	/* Record class name (if any) */
16450Sstevel@tonic-gate 	(void) snprintf(libloc_p->ap_class, sizeof (libloc_p->ap_class), "%s",
16460Sstevel@tonic-gate 	    class);
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 	return (CFGA_OK);
16490Sstevel@tonic-gate }
16500Sstevel@tonic-gate 
16510Sstevel@tonic-gate static cfga_err_t
16520Sstevel@tonic-gate lookup_cache(lib_loc_t *libloc_p)
16530Sstevel@tonic-gate {
16540Sstevel@tonic-gate 	lib_cache_t *entry;
16550Sstevel@tonic-gate 	(void) mutex_lock(&lib_cache_lock);
16560Sstevel@tonic-gate 	entry = lib_cache;
16570Sstevel@tonic-gate 	while (entry) {
16580Sstevel@tonic-gate 		if (strcmp(entry->lc_ap_id, libloc_p->ap_base) == 0) {
16590Sstevel@tonic-gate 			plugin_lib_t *libp = entry->lc_libp;
16600Sstevel@tonic-gate 			libloc_p->libp = libp;
16610Sstevel@tonic-gate 			hold_lib(libp);
16620Sstevel@tonic-gate 			(void) strcpy(libloc_p->pathname, libp->libpath);
16630Sstevel@tonic-gate 			(void) strcpy(libloc_p->ap_physical,
16640Sstevel@tonic-gate 			    entry->lc_ap_physical);
16650Sstevel@tonic-gate 			(void) strcpy(libloc_p->ap_logical,
16660Sstevel@tonic-gate 			    entry->lc_ap_logical);
16670Sstevel@tonic-gate 			(void) mutex_unlock(&lib_cache_lock);
16680Sstevel@tonic-gate 			return (CFGA_OK);
16690Sstevel@tonic-gate 		}
16700Sstevel@tonic-gate 		entry = entry->lc_next;
16710Sstevel@tonic-gate 	}
16720Sstevel@tonic-gate 	(void) mutex_unlock(&lib_cache_lock);
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 	return (CFGA_ERROR);
16750Sstevel@tonic-gate }
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate static void
16780Sstevel@tonic-gate update_cache(lib_loc_t *libloc_p)
16790Sstevel@tonic-gate {
16800Sstevel@tonic-gate 	lib_cache_t *entry;
16810Sstevel@tonic-gate 	entry = config_calloc_check(1, sizeof (lib_cache_t), NULL);
16820Sstevel@tonic-gate 	if (entry == NULL)
16830Sstevel@tonic-gate 		return;
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate 	entry->lc_ap_id = strdup(libloc_p->ap_base);
16860Sstevel@tonic-gate 	entry->lc_ap_physical = strdup(libloc_p->ap_physical);
16870Sstevel@tonic-gate 	entry->lc_ap_logical = strdup(libloc_p->ap_logical);
16880Sstevel@tonic-gate 	if ((entry->lc_ap_id == NULL) || (entry->lc_ap_physical == NULL) ||
16890Sstevel@tonic-gate 	    (entry->lc_ap_logical == NULL)) {
16900Sstevel@tonic-gate 		free(entry->lc_ap_id);
16910Sstevel@tonic-gate 		free(entry->lc_ap_physical);
16920Sstevel@tonic-gate 		free(entry->lc_ap_logical);
16930Sstevel@tonic-gate 		free(entry);
16940Sstevel@tonic-gate 		return;
16950Sstevel@tonic-gate 	}
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 	(void) mutex_lock(&lib_cache_lock);
16980Sstevel@tonic-gate 	entry->lc_libp = libloc_p->libp;
16990Sstevel@tonic-gate 	entry->lc_next = lib_cache;
17000Sstevel@tonic-gate 	lib_cache = entry;
17010Sstevel@tonic-gate 	hold_lib(entry->lc_libp);	/* prevent stale cache */
17020Sstevel@tonic-gate 	(void) mutex_unlock(&lib_cache_lock);
17030Sstevel@tonic-gate }
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate static void
17060Sstevel@tonic-gate destroy_cache()
17070Sstevel@tonic-gate {
17080Sstevel@tonic-gate 	lib_cache_t *entry, *next;
17090Sstevel@tonic-gate 	(void) mutex_lock(&lib_cache_lock);
17100Sstevel@tonic-gate 	entry = lib_cache;
17110Sstevel@tonic-gate 	while (entry) {
17120Sstevel@tonic-gate 		next = entry->lc_next;
17130Sstevel@tonic-gate 		rele_lib(entry->lc_libp);
17140Sstevel@tonic-gate 		free(entry->lc_ap_id);
17150Sstevel@tonic-gate 		free(entry->lc_ap_physical);
17160Sstevel@tonic-gate 		free(entry->lc_ap_logical);
17170Sstevel@tonic-gate 		free(entry);
17180Sstevel@tonic-gate 		entry = next;
17190Sstevel@tonic-gate 	}
17200Sstevel@tonic-gate 	(void) mutex_unlock(&lib_cache_lock);
17210Sstevel@tonic-gate }
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate /*
17240Sstevel@tonic-gate  * find_ap_common - locate a particular attachment point
17250Sstevel@tonic-gate  */
17260Sstevel@tonic-gate static cfga_err_t
17270Sstevel@tonic-gate find_ap_common(
17280Sstevel@tonic-gate 	lib_loc_t *libloc_p,
17290Sstevel@tonic-gate 	const char *physpath,
17300Sstevel@tonic-gate 	int (*fcn)(di_node_t node, di_minor_t minor, void *arg),
17310Sstevel@tonic-gate 	char **errstring)
17320Sstevel@tonic-gate {
17330Sstevel@tonic-gate 	di_node_t rnode;
17340Sstevel@tonic-gate 	char *cp, *rpath;
17350Sstevel@tonic-gate 	size_t len;
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 	if (lookup_cache(libloc_p) == CFGA_OK)
17380Sstevel@tonic-gate 		return (CFGA_OK);
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 	if ((rpath = config_calloc_check(1, strlen(physpath) + 1,
17410Sstevel@tonic-gate 	    errstring)) == NULL) {
17420Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
17430Sstevel@tonic-gate 	}
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 	(void) strcpy(rpath, physpath);
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate 	/* Remove devices prefix (if any) */
17480Sstevel@tonic-gate 	len = strlen(DEVICES_DIR);
17490Sstevel@tonic-gate 	if (strncmp(rpath, DEVICES_DIR SLASH, len + strlen(SLASH)) == 0) {
17500Sstevel@tonic-gate 		(void) memmove(rpath, rpath + len,
17510Sstevel@tonic-gate 		    strlen(rpath + len) + 1);
17520Sstevel@tonic-gate 	}
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 	/* Remove dynamic component if any */
17550Sstevel@tonic-gate 	if ((cp = GET_DYN(rpath)) != NULL) {
17560Sstevel@tonic-gate 		*cp = '\0';
17570Sstevel@tonic-gate 	}
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate 	/* Remove minor name (if any) */
17600Sstevel@tonic-gate 	if ((cp = strrchr(rpath, ':')) != NULL) {
17610Sstevel@tonic-gate 		*cp = '\0';
17620Sstevel@tonic-gate 	}
17630Sstevel@tonic-gate 
17640Sstevel@tonic-gate 	/*
17650Sstevel@tonic-gate 	 * begin walk of device tree
17660Sstevel@tonic-gate 	 */
17670Sstevel@tonic-gate 	rnode = di_init(rpath, DINFOCPYALL);
17680Sstevel@tonic-gate 	S_FREE(rpath);
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate 	if (rnode == DI_NODE_NIL) {
17710Sstevel@tonic-gate 		/*
17720Sstevel@tonic-gate 		 * di_init() may fail because the ap_id does not exist
17730Sstevel@tonic-gate 		 */
17740Sstevel@tonic-gate 		if (errno == EINVAL || errno == ENXIO) {
17750Sstevel@tonic-gate 			return (CFGA_APID_NOEXIST);
17760Sstevel@tonic-gate 		} else {
17770Sstevel@tonic-gate 			config_err(errno, DI_INIT_FAILED, errstring);
17780Sstevel@tonic-gate 			return (CFGA_LIB_ERROR);
17790Sstevel@tonic-gate 		}
17800Sstevel@tonic-gate 	}
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 	libloc_p->libp = NULL;
17830Sstevel@tonic-gate 	libloc_p->status = CFGA_APID_NOEXIST;
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate 	(void) di_walk_minor(rnode, "ddi_ctl:attachment_point",
17860Sstevel@tonic-gate 	    DI_CHECK_ALIAS|DI_CHECK_INTERNAL_PATH,
17870Sstevel@tonic-gate 	    libloc_p, fcn);
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 	di_fini(rnode);
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate 	if (libloc_p->libp != NULL) {
17920Sstevel@tonic-gate 		update_cache(libloc_p);
17930Sstevel@tonic-gate 		return (CFGA_OK);
17940Sstevel@tonic-gate 	} else {
17950Sstevel@tonic-gate 		return (libloc_p->status);
17960Sstevel@tonic-gate 	}
17970Sstevel@tonic-gate }
17980Sstevel@tonic-gate 
17990Sstevel@tonic-gate /*
18000Sstevel@tonic-gate  * check_ap - called for each attachment point found
18010Sstevel@tonic-gate  *
18020Sstevel@tonic-gate  * This is used in cases where a particular attachment point
18030Sstevel@tonic-gate  * or type of attachment point is specified via a logical name or ap_type.
18040Sstevel@tonic-gate  * Not used for physical names or in the list case with no
18050Sstevel@tonic-gate  * ap's specified.
18060Sstevel@tonic-gate  */
18070Sstevel@tonic-gate 
18080Sstevel@tonic-gate static int
18090Sstevel@tonic-gate check_ap(
18100Sstevel@tonic-gate 	di_node_t node,
18110Sstevel@tonic-gate 	di_minor_t minor,
18120Sstevel@tonic-gate 	void *arg)
18130Sstevel@tonic-gate {
18140Sstevel@tonic-gate 	char *cp = NULL;
18150Sstevel@tonic-gate 	char aptype[MAXPATHLEN];
18160Sstevel@tonic-gate 	char *recep_id = NULL;
18170Sstevel@tonic-gate 	char *node_minor;
18180Sstevel@tonic-gate 	char *drv_name;
18190Sstevel@tonic-gate 	char inst[MAXPATHLEN];
18200Sstevel@tonic-gate 	char inst2[MAXPATHLEN];
18210Sstevel@tonic-gate 	lib_loc_t *libloc_p;
18220Sstevel@tonic-gate 	int comparison_test;
18230Sstevel@tonic-gate 	int instance;
18240Sstevel@tonic-gate 	cfga_ap_types_t type;
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate 	libloc_p = (lib_loc_t *)arg;
18280Sstevel@tonic-gate 
18290Sstevel@tonic-gate 	(void) snprintf(aptype, sizeof (aptype), "%s", libloc_p->ap_base);
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate 	/*
18320Sstevel@tonic-gate 	 * This routime handles only aptypes and driver based logical apids.
18330Sstevel@tonic-gate 	 */
18340Sstevel@tonic-gate 	type = find_arg_type(aptype);
18350Sstevel@tonic-gate 	if (type == LOGICAL_DRV_AP) {
18360Sstevel@tonic-gate 		cp = strchr(aptype, ':');
18370Sstevel@tonic-gate 		*cp = '\0';
18380Sstevel@tonic-gate 		recep_id =  cp+1;
18390Sstevel@tonic-gate 		cp--;
18400Sstevel@tonic-gate 		while (isdigit(*cp) && cp != aptype)
18410Sstevel@tonic-gate 			cp--;
18420Sstevel@tonic-gate 		cp++;
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate 		(void) snprintf(inst, sizeof (inst), "%s", cp);
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 		*cp = '\0';
18470Sstevel@tonic-gate 	} else if (type != AP_TYPE) {
18480Sstevel@tonic-gate 		libloc_p->status = CFGA_APID_NOEXIST;
18490Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
18500Sstevel@tonic-gate 	}
18510Sstevel@tonic-gate 
18520Sstevel@tonic-gate 	node_minor = di_minor_name(minor);
18530Sstevel@tonic-gate 	drv_name = di_driver_name(node);
18540Sstevel@tonic-gate 	instance = di_instance(node);
18550Sstevel@tonic-gate 
18560Sstevel@tonic-gate 	if (node_minor == NULL || drv_name == NULL || instance == -1) {
18570Sstevel@tonic-gate 		libloc_p->status = CFGA_APID_NOEXIST;
18580Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
18590Sstevel@tonic-gate 	}
18600Sstevel@tonic-gate 
18610Sstevel@tonic-gate 	(void) sprintf(inst2, "%d", instance);
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate 	/*
18640Sstevel@tonic-gate 	 * If the base matches driver and instance try and find a lib for it,
18650Sstevel@tonic-gate 	 * then load it. On any failure we continue the walk.
18660Sstevel@tonic-gate 	 *
18670Sstevel@tonic-gate 	 * driver based logical ap_ids are derived from driver name + instance.
18680Sstevel@tonic-gate 	 * Ap_types are just partial driver names.
18690Sstevel@tonic-gate 	 *
18700Sstevel@tonic-gate 	 */
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate 	comparison_test = 0;
18730Sstevel@tonic-gate 	if (type == AP_TYPE) {
18740Sstevel@tonic-gate 		if (strncmp(aptype, drv_name, strlen(aptype)) == 0) {
18750Sstevel@tonic-gate 			comparison_test = 1;
18760Sstevel@tonic-gate 		}
18770Sstevel@tonic-gate 	} else {
18780Sstevel@tonic-gate 		if (strcmp(aptype, drv_name) == 0 &&
18790Sstevel@tonic-gate 		    strcmp(recep_id, node_minor) == 0 &&
18800Sstevel@tonic-gate 		    strcmp(inst, inst2) == 0) {
18810Sstevel@tonic-gate 			comparison_test = 1;
18820Sstevel@tonic-gate 		}
18830Sstevel@tonic-gate 	}
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate 	if (comparison_test) {
18860Sstevel@tonic-gate 		/*
18870Sstevel@tonic-gate 		 * save the correct type of error so user does not get confused
18880Sstevel@tonic-gate 		 */
18890Sstevel@tonic-gate 		if (find_lib(node, minor, libloc_p) != CFGA_OK) {
18900Sstevel@tonic-gate 			libloc_p->status = CFGA_NO_LIB;
18910Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
18920Sstevel@tonic-gate 		}
18930Sstevel@tonic-gate 		if (load_lib(node, minor, libloc_p) != CFGA_OK) {
18940Sstevel@tonic-gate 			libloc_p->status = CFGA_LIB_ERROR;
18950Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
18960Sstevel@tonic-gate 		}
18970Sstevel@tonic-gate 		libloc_p->status = CFGA_OK;
18980Sstevel@tonic-gate 		return (DI_WALK_TERMINATE);
18990Sstevel@tonic-gate 	} else {
19000Sstevel@tonic-gate 		libloc_p->status = CFGA_APID_NOEXIST;
19010Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
19020Sstevel@tonic-gate 	}
19030Sstevel@tonic-gate }
19040Sstevel@tonic-gate 
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate /*
19070Sstevel@tonic-gate  * check_ap_phys - called for each attachment point found
19080Sstevel@tonic-gate  *
19090Sstevel@tonic-gate  * This is used in cases where a particular attachment point
19100Sstevel@tonic-gate  * is specified via a physical name. If the name matches then
19110Sstevel@tonic-gate  * we try and find and load the library for it.
19120Sstevel@tonic-gate  */
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate static int
19150Sstevel@tonic-gate check_ap_phys(
19160Sstevel@tonic-gate 	di_node_t node,
19170Sstevel@tonic-gate 	di_minor_t minor,
19180Sstevel@tonic-gate 	void *arg)
19190Sstevel@tonic-gate {
19200Sstevel@tonic-gate 	lib_loc_t *libloc_p;
19210Sstevel@tonic-gate 	char phys_name[MAXPATHLEN];
19220Sstevel@tonic-gate 	char *devfs_path;
19230Sstevel@tonic-gate 	char *minor_name;
19240Sstevel@tonic-gate 
19250Sstevel@tonic-gate 	libloc_p = (lib_loc_t *)arg;
19260Sstevel@tonic-gate 	devfs_path = di_devfs_path(node);
19270Sstevel@tonic-gate 	minor_name = di_minor_name(minor);
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate 	if (devfs_path == NULL || minor_name == NULL) {
19300Sstevel@tonic-gate 		libloc_p->status = CFGA_APID_NOEXIST;
19310Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
19320Sstevel@tonic-gate 	}
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate 	(void) snprintf(phys_name, sizeof (phys_name), "%s%s:%s",
19350Sstevel@tonic-gate 	    DEVICES_DIR, devfs_path, minor_name);
19360Sstevel@tonic-gate 
19370Sstevel@tonic-gate 	di_devfs_path_free(devfs_path);
19380Sstevel@tonic-gate 
19390Sstevel@tonic-gate 	if (strcmp(phys_name, libloc_p->ap_base) == 0) {
19400Sstevel@tonic-gate 		if (find_lib(node, minor, libloc_p) != CFGA_OK) {
19410Sstevel@tonic-gate 			libloc_p->status = CFGA_NO_LIB;
19420Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
19430Sstevel@tonic-gate 		}
19440Sstevel@tonic-gate 		if (load_lib(node, minor, libloc_p) != CFGA_OK) {
19450Sstevel@tonic-gate 			libloc_p->status = CFGA_LIB_ERROR;
19460Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
19470Sstevel@tonic-gate 		}
19480Sstevel@tonic-gate 		libloc_p->status = CFGA_OK;
19490Sstevel@tonic-gate 		return (DI_WALK_TERMINATE);
19500Sstevel@tonic-gate 	} else {
19510Sstevel@tonic-gate 		libloc_p->status = CFGA_APID_NOEXIST;
19520Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
19530Sstevel@tonic-gate 	}
19540Sstevel@tonic-gate }
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate /*
19570Sstevel@tonic-gate  * lib_in_list
19580Sstevel@tonic-gate  *
19590Sstevel@tonic-gate  * See if library, as specified by the full pathname and controller
19600Sstevel@tonic-gate  * instance number is already represented in the plugin library list.
19610Sstevel@tonic-gate  * If the instance number is -1 it is ignored.
19620Sstevel@tonic-gate  */
19630Sstevel@tonic-gate static plugin_lib_t *
19640Sstevel@tonic-gate lib_in_list(char *libpath)
19650Sstevel@tonic-gate {
19660Sstevel@tonic-gate 	plugin_lib_t *libp = NULL;
19670Sstevel@tonic-gate 
19680Sstevel@tonic-gate 	for (libp = plugin_list.next; libp != NULL; libp = libp->next) {
19690Sstevel@tonic-gate 		if (strncmp(libpath, libp->libpath, MAXPATHLEN) == 0) {
19700Sstevel@tonic-gate 			return (libp);
19710Sstevel@tonic-gate 		}
19720Sstevel@tonic-gate 	}
19730Sstevel@tonic-gate 	return (NULL);
19740Sstevel@tonic-gate }
19750Sstevel@tonic-gate 
19760Sstevel@tonic-gate 
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate 
19790Sstevel@tonic-gate /*
19800Sstevel@tonic-gate  * Coalesce stat and list data into single array
19810Sstevel@tonic-gate  */
19820Sstevel@tonic-gate static cfga_err_t
19830Sstevel@tonic-gate realloc_data_ext(
19840Sstevel@tonic-gate 	cfga_list_data_t **ap_id_list,
19850Sstevel@tonic-gate 	int *nlistp,
19860Sstevel@tonic-gate 	list_stat_t *lstatp)
19870Sstevel@tonic-gate {
19880Sstevel@tonic-gate 	int i, j;
19890Sstevel@tonic-gate 	stat_data_list_t *slp;
19900Sstevel@tonic-gate 	cfga_list_data_t *cldp;
19910Sstevel@tonic-gate 	array_list_t *alp;
19920Sstevel@tonic-gate 	cfga_err_t rc = CFGA_OK;
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 	assert(*lstatp->countp >= 0);
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate 	if (*lstatp->countp == 0) {
19980Sstevel@tonic-gate 		*ap_id_list = NULL;
19990Sstevel@tonic-gate 		*nlistp = 0;
20000Sstevel@tonic-gate 		return (CFGA_OK);
20010Sstevel@tonic-gate 	}
20020Sstevel@tonic-gate 
20030Sstevel@tonic-gate 	/*
20040Sstevel@tonic-gate 	 * allocate the array
20050Sstevel@tonic-gate 	 */
20060Sstevel@tonic-gate 	if ((cldp = config_calloc_check(*lstatp->countp,
20070Sstevel@tonic-gate 	    sizeof (cfga_list_data_t), lstatp->errstr)) == NULL) {
20080Sstevel@tonic-gate 		rc = CFGA_LIB_ERROR;
20090Sstevel@tonic-gate 		goto out;
20100Sstevel@tonic-gate 	}
20110Sstevel@tonic-gate 
20120Sstevel@tonic-gate 	/*
20130Sstevel@tonic-gate 	 * copy all the stat elements (if any) into the array
20140Sstevel@tonic-gate 	 */
20150Sstevel@tonic-gate 	slp = lstatp->sdl;
20160Sstevel@tonic-gate 	for (i = 0; slp != NULL; i++) {
20170Sstevel@tonic-gate 		if (i >= *lstatp->countp) {
20180Sstevel@tonic-gate 			rc = CFGA_LIB_ERROR;
20190Sstevel@tonic-gate 			goto out;
20200Sstevel@tonic-gate 		}
20210Sstevel@tonic-gate 		stat_to_list(&cldp[i], &slp->stat_data);
20220Sstevel@tonic-gate 		slp = slp->next;
20230Sstevel@tonic-gate 	}
20240Sstevel@tonic-gate 
20250Sstevel@tonic-gate 	/*
20260Sstevel@tonic-gate 	 * copy all the list elements (if any) into the array
20270Sstevel@tonic-gate 	 */
20280Sstevel@tonic-gate 	alp = lstatp->al;
20290Sstevel@tonic-gate 	for (; alp != NULL; ) {
20300Sstevel@tonic-gate 		if (i + alp->nelem > *lstatp->countp) {
20310Sstevel@tonic-gate 			rc = CFGA_LIB_ERROR;
20320Sstevel@tonic-gate 			goto out;
20330Sstevel@tonic-gate 		}
20340Sstevel@tonic-gate 
20350Sstevel@tonic-gate 		for (j = 0; j < alp->nelem; i++, j++) {
20360Sstevel@tonic-gate 			cldp[i] = alp->array[j];
20370Sstevel@tonic-gate 		}
20380Sstevel@tonic-gate 		alp = alp->next;
20390Sstevel@tonic-gate 	}
20400Sstevel@tonic-gate 
20410Sstevel@tonic-gate 	if (i != *lstatp->countp) {
20420Sstevel@tonic-gate 		rc = CFGA_LIB_ERROR;
20430Sstevel@tonic-gate 	} else {
20440Sstevel@tonic-gate 		rc = CFGA_OK;
20450Sstevel@tonic-gate 	}
20460Sstevel@tonic-gate 
20470Sstevel@tonic-gate 	/*FALLTHRU*/
20480Sstevel@tonic-gate 
20490Sstevel@tonic-gate out:
20500Sstevel@tonic-gate 	/* clean up */
20510Sstevel@tonic-gate 	lstat_free(lstatp);
20520Sstevel@tonic-gate 
20530Sstevel@tonic-gate 	if (rc == CFGA_OK) {
20540Sstevel@tonic-gate 		*ap_id_list = cldp;
20550Sstevel@tonic-gate 		*nlistp = *lstatp->countp;
20560Sstevel@tonic-gate 	} else {
20570Sstevel@tonic-gate 		S_FREE(cldp);
20580Sstevel@tonic-gate 		*ap_id_list = NULL;
20590Sstevel@tonic-gate 		*nlistp = 0;
20600Sstevel@tonic-gate 	}
20610Sstevel@tonic-gate 	return (rc);
20620Sstevel@tonic-gate }
20630Sstevel@tonic-gate 
20640Sstevel@tonic-gate /*
20650Sstevel@tonic-gate  * The caller of this routine may supply a buffer through
20660Sstevel@tonic-gate  * ap_id_list for returning data. Otherwise, this routine allocates the
20670Sstevel@tonic-gate  * buffer.
20680Sstevel@tonic-gate  */
20690Sstevel@tonic-gate static cfga_err_t
20700Sstevel@tonic-gate realloc_data(cfga_stat_data_t **ap_id_list, int *nlistp, list_stat_t *lstatp)
20710Sstevel@tonic-gate {
20720Sstevel@tonic-gate 	int i;
20730Sstevel@tonic-gate 	stat_data_list_t *slp;
20740Sstevel@tonic-gate 	cfga_stat_data_t *csdp, *buf;
20750Sstevel@tonic-gate 	cfga_err_t rc;
20760Sstevel@tonic-gate 
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 	assert(*lstatp->countp >= 0);
20790Sstevel@tonic-gate 
20800Sstevel@tonic-gate 	if (*lstatp->countp == 0) {
20810Sstevel@tonic-gate 		*nlistp = 0;
20820Sstevel@tonic-gate 		return (CFGA_OK);
20830Sstevel@tonic-gate 	}
20840Sstevel@tonic-gate 
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 	/*
20870Sstevel@tonic-gate 	 * allocate the array if caller does not supply one.
20880Sstevel@tonic-gate 	 */
20890Sstevel@tonic-gate 	if (*ap_id_list == NULL) {
20900Sstevel@tonic-gate 		if ((buf = config_calloc_check(*lstatp->countp,
20910Sstevel@tonic-gate 		    sizeof (cfga_stat_data_t), lstatp->errstr)) == NULL) {
20920Sstevel@tonic-gate 			rc = CFGA_LIB_ERROR;
20930Sstevel@tonic-gate 			goto out;
20940Sstevel@tonic-gate 		}
20950Sstevel@tonic-gate 	} else {
20960Sstevel@tonic-gate 		buf = *ap_id_list;
20970Sstevel@tonic-gate 	}
20980Sstevel@tonic-gate 
20990Sstevel@tonic-gate 	/*
21000Sstevel@tonic-gate 	 * copy the stat elements into the array
21010Sstevel@tonic-gate 	 */
21020Sstevel@tonic-gate 	csdp = buf;
21030Sstevel@tonic-gate 	slp = lstatp->sdl;
21040Sstevel@tonic-gate 	for (i = 0; slp != NULL; i++) {
21050Sstevel@tonic-gate 		if (i >= *lstatp->countp) {
21060Sstevel@tonic-gate 			rc = CFGA_LIB_ERROR;
21070Sstevel@tonic-gate 			goto out;
21080Sstevel@tonic-gate 		}
21090Sstevel@tonic-gate 		*csdp++ = slp->stat_data;
21100Sstevel@tonic-gate 		slp = slp->next;
21110Sstevel@tonic-gate 	}
21120Sstevel@tonic-gate 
21130Sstevel@tonic-gate 	rc = CFGA_OK;
21140Sstevel@tonic-gate 
21150Sstevel@tonic-gate out:
21160Sstevel@tonic-gate 	if (rc == CFGA_OK) {
21170Sstevel@tonic-gate 		*nlistp = *lstatp->countp;
21180Sstevel@tonic-gate 		*ap_id_list = buf;
21190Sstevel@tonic-gate 	} else {
21200Sstevel@tonic-gate 		/*
21210Sstevel@tonic-gate 		 * Free buffer only if we allocated it.
21220Sstevel@tonic-gate 		 */
21230Sstevel@tonic-gate 		if (*ap_id_list == NULL) {
21240Sstevel@tonic-gate 			free(buf);
21250Sstevel@tonic-gate 		}
21260Sstevel@tonic-gate 		*nlistp = 0;
21270Sstevel@tonic-gate 	}
21280Sstevel@tonic-gate 
21290Sstevel@tonic-gate 	assert(lstatp->al == NULL);
21300Sstevel@tonic-gate 	lstat_free(lstatp);
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate 	return (rc);
21330Sstevel@tonic-gate }
21340Sstevel@tonic-gate 
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate /*
21370Sstevel@tonic-gate  * list_common - walk the device tree and stat all attachment points.
21380Sstevel@tonic-gate  */
21390Sstevel@tonic-gate static cfga_err_t
21400Sstevel@tonic-gate list_common(list_stat_t *lstatp, const char *class)
21410Sstevel@tonic-gate {
21420Sstevel@tonic-gate 	di_node_t rnode;
21430Sstevel@tonic-gate 	char nodetype[MAXPATHLEN];
21440Sstevel@tonic-gate 	const char *l_class, *l_sep;
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate 
21470Sstevel@tonic-gate 	/*
21480Sstevel@tonic-gate 	 * begin walk of device tree
21490Sstevel@tonic-gate 	 */
21500Sstevel@tonic-gate 	if ((rnode = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) {
21510Sstevel@tonic-gate 		config_err(errno, DI_INIT_FAILED, lstatp->errstr);
21520Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
21530Sstevel@tonic-gate 	}
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate 	/*
21560Sstevel@tonic-gate 	 * May walk a subset of all attachment points in the device tree if
21570Sstevel@tonic-gate 	 * a class is specified
21580Sstevel@tonic-gate 	 */
21590Sstevel@tonic-gate 	if (class != NULL) {
21600Sstevel@tonic-gate 		l_sep = ":";
21610Sstevel@tonic-gate 		l_class = class;
21620Sstevel@tonic-gate 	} else {
21630Sstevel@tonic-gate 		l_sep = l_class = "";
21640Sstevel@tonic-gate 	}
21650Sstevel@tonic-gate 
21660Sstevel@tonic-gate 	(void) snprintf(nodetype, sizeof (nodetype), "%s%s%s",
21670Sstevel@tonic-gate 	    DDI_NT_ATTACHMENT_POINT, l_sep, l_class);
21680Sstevel@tonic-gate 
21690Sstevel@tonic-gate 	(void) di_walk_minor(rnode, nodetype,
21700Sstevel@tonic-gate 	    DI_CHECK_ALIAS|DI_CHECK_INTERNAL_PATH, lstatp, do_list_common);
21710Sstevel@tonic-gate 	di_fini(rnode);
21720Sstevel@tonic-gate 
21730Sstevel@tonic-gate 	return (CFGA_OK);
21740Sstevel@tonic-gate }
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate static void
21770Sstevel@tonic-gate config_err(int errnum, int err_type, char **errstring)
21780Sstevel@tonic-gate {
21790Sstevel@tonic-gate 	char *p = NULL, *q = NULL;
21800Sstevel@tonic-gate 	char *syserr = NULL;
21810Sstevel@tonic-gate 	char syserr_num[20];
21820Sstevel@tonic-gate 	int len = 0;
21830Sstevel@tonic-gate 
21840Sstevel@tonic-gate 	/*
21850Sstevel@tonic-gate 	 * If errstring is null it means user in not interested in getting
21860Sstevel@tonic-gate 	 * error status. So we don't do all the work
21870Sstevel@tonic-gate 	 */
21880Sstevel@tonic-gate 	if (errstring == NULL) {
21890Sstevel@tonic-gate 		return;
21900Sstevel@tonic-gate 	}
21910Sstevel@tonic-gate 
21920Sstevel@tonic-gate 	if (errnum != 0) {
21930Sstevel@tonic-gate 		syserr = strerror(errnum);
21940Sstevel@tonic-gate 		if (syserr == NULL) {
21950Sstevel@tonic-gate 			(void) sprintf(syserr_num, "errno=%d", errnum);
21960Sstevel@tonic-gate 			syserr = syserr_num;
21970Sstevel@tonic-gate 		}
21980Sstevel@tonic-gate 	} else
21990Sstevel@tonic-gate 		syserr = NULL;
22000Sstevel@tonic-gate 
22010Sstevel@tonic-gate 	q = dgettext(TEXT_DOMAIN, err_strings[err_type]);
22020Sstevel@tonic-gate 
22030Sstevel@tonic-gate 	len = strlen(q);
22040Sstevel@tonic-gate 	if (syserr != NULL) {
22050Sstevel@tonic-gate 		len += strlen(err_sep) + strlen(syserr);
22060Sstevel@tonic-gate 	}
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate 	p = malloc(len + 1);
22090Sstevel@tonic-gate 	if (p == NULL) {
22100Sstevel@tonic-gate 		*errstring = NULL;
22110Sstevel@tonic-gate 		return;
22120Sstevel@tonic-gate 	}
22130Sstevel@tonic-gate 
22140Sstevel@tonic-gate 	(void) strcpy(p, q);
22150Sstevel@tonic-gate 	if (syserr != NULL) {
22160Sstevel@tonic-gate 		(void) strcat(p, err_sep);
22170Sstevel@tonic-gate 		(void) strcat(p, syserr);
22180Sstevel@tonic-gate 	}
22190Sstevel@tonic-gate 
22200Sstevel@tonic-gate 	*errstring  = p;
22210Sstevel@tonic-gate }
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate /*
22240Sstevel@tonic-gate  * do_list_common - Routine to list attachment point as part of
22250Sstevel@tonic-gate  * a config_list opertion. Used by both v1 and v2 interfaces.
22260Sstevel@tonic-gate  * This is somewhat similar to config_get_lib() and its helper routines
22270Sstevel@tonic-gate  * except that the ap_ids are always physical and don't have dynamic
22280Sstevel@tonic-gate  * components.
22290Sstevel@tonic-gate  */
22300Sstevel@tonic-gate static int
22310Sstevel@tonic-gate do_list_common(
22320Sstevel@tonic-gate 	di_node_t node,
22330Sstevel@tonic-gate 	di_minor_t minor,
22340Sstevel@tonic-gate 	void *arg)
22350Sstevel@tonic-gate {
22360Sstevel@tonic-gate 	lib_loc_t lib_loc;
22370Sstevel@tonic-gate 	plugin_lib_t *libp;
22380Sstevel@tonic-gate 	list_stat_t *lstatp = NULL;
22390Sstevel@tonic-gate 	cfga_err_t ret = CFGA_ERROR;
22400Sstevel@tonic-gate 
22410Sstevel@tonic-gate 
22420Sstevel@tonic-gate 	lstatp = (list_stat_t *)arg;
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate 	lib_loc.libp = NULL;
22450Sstevel@tonic-gate 	/*
22460Sstevel@tonic-gate 	 * try and find a lib for this node
22470Sstevel@tonic-gate 	 */
22480Sstevel@tonic-gate 	if (find_lib(node, minor, &lib_loc) != CFGA_OK) {
22490Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
22500Sstevel@tonic-gate 	}
22510Sstevel@tonic-gate 
22520Sstevel@tonic-gate 	/*
22530Sstevel@tonic-gate 	 * Load all plugins. We will check compatibility later in this
22540Sstevel@tonic-gate 	 * routine.
22550Sstevel@tonic-gate 	 */
22560Sstevel@tonic-gate 	lib_loc.vers_req.v_min = CFGA_HSL_V1;
22570Sstevel@tonic-gate 	lib_loc.vers_req.v_max = CFGA_HSL_VERS;
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 	ret = load_lib(node, minor, &lib_loc);
22600Sstevel@tonic-gate 	if (ret != CFGA_OK) {
22610Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
22620Sstevel@tonic-gate 	}
22630Sstevel@tonic-gate 
22640Sstevel@tonic-gate 	libp = lib_loc.libp;
22650Sstevel@tonic-gate 	assert(libp != NULL);
22660Sstevel@tonic-gate 
22670Sstevel@tonic-gate 	/*
22680Sstevel@tonic-gate 	 * Note: For list type routines (list all attachment points in
22690Sstevel@tonic-gate 	 * device tree) we don't pass errstring to the plugin, nor do we
22700Sstevel@tonic-gate 	 * stop the walk if an error occurs in the plugin.
22710Sstevel@tonic-gate 	 */
22720Sstevel@tonic-gate 	if (compat_plugin(&lstatp->use_vers, libp->plugin_vers)) {
22730Sstevel@tonic-gate 		(void) libp->vers_ops->stat_plugin(lstatp, &lib_loc, NULL);
22740Sstevel@tonic-gate 	}
22750Sstevel@tonic-gate 	rele_lib(libp);
22760Sstevel@tonic-gate 
22770Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
22780Sstevel@tonic-gate }
22790Sstevel@tonic-gate 
22800Sstevel@tonic-gate /*
22810Sstevel@tonic-gate  * stat_common - stat a user specified set of attachment points.
22820Sstevel@tonic-gate  */
22830Sstevel@tonic-gate static cfga_err_t
22840Sstevel@tonic-gate stat_common(
22850Sstevel@tonic-gate 	int num_ap_ids,
22860Sstevel@tonic-gate 	char *const *ap_ids,
22870Sstevel@tonic-gate 	const char *class,
22880Sstevel@tonic-gate 	list_stat_t *lstatp)
22890Sstevel@tonic-gate {
22900Sstevel@tonic-gate 	int i;
22910Sstevel@tonic-gate 	lib_loc_t libloc;
22920Sstevel@tonic-gate 	plugin_lib_t *libp;
22930Sstevel@tonic-gate 	cfga_err_t rc = CFGA_OK;
22940Sstevel@tonic-gate 
22950Sstevel@tonic-gate 
22960Sstevel@tonic-gate 	/*
22970Sstevel@tonic-gate 	 * operate on each ap_id
22980Sstevel@tonic-gate 	 */
22990Sstevel@tonic-gate 	for (i = 0; i < num_ap_ids; i++) {
23000Sstevel@tonic-gate 		libloc.libp = NULL;
23010Sstevel@tonic-gate 		if ((rc = config_get_lib(ap_ids[i], &libloc,
23020Sstevel@tonic-gate 		    lstatp->errstr)) != CFGA_OK) {
23030Sstevel@tonic-gate 			break;
23040Sstevel@tonic-gate 		}
23050Sstevel@tonic-gate 		assert(libloc.libp != NULL);
23060Sstevel@tonic-gate 		libp = libloc.libp;
23070Sstevel@tonic-gate 
23080Sstevel@tonic-gate 		/*
23090Sstevel@tonic-gate 		 * do pre-filtering if requested
23100Sstevel@tonic-gate 		 */
23110Sstevel@tonic-gate 		if (class != NULL && strcmp(libloc.ap_class, class)) {
23120Sstevel@tonic-gate 			rele_lib(libp);
23130Sstevel@tonic-gate 			continue;
23140Sstevel@tonic-gate 		}
23150Sstevel@tonic-gate 
23160Sstevel@tonic-gate 		/*
23170Sstevel@tonic-gate 		 * Unlike list type routines, while stat'ing specific
23180Sstevel@tonic-gate 		 * attachment points we pass errstring to the plugins
23190Sstevel@tonic-gate 		 * and halt if an error occurs in the plugin.
23200Sstevel@tonic-gate 		 */
23210Sstevel@tonic-gate 		rc = libp->vers_ops->stat_plugin(lstatp, &libloc,
23220Sstevel@tonic-gate 		    lstatp->errstr);
23230Sstevel@tonic-gate 		rele_lib(libp);
23240Sstevel@tonic-gate 		if (rc != CFGA_OK) {
23250Sstevel@tonic-gate 			break;
23260Sstevel@tonic-gate 		}
23270Sstevel@tonic-gate 	}
23280Sstevel@tonic-gate 
23290Sstevel@tonic-gate 	if (rc != CFGA_OK) {
23300Sstevel@tonic-gate 		lstat_free(lstatp);
23310Sstevel@tonic-gate 	}
23320Sstevel@tonic-gate 	return (rc);
23330Sstevel@tonic-gate }
23340Sstevel@tonic-gate 
23350Sstevel@tonic-gate /*ARGSUSED*/
23360Sstevel@tonic-gate static cfga_err_t
23370Sstevel@tonic-gate null_stat_plugin(list_stat_t *lstatp, lib_loc_t *libloc_p, char **errstring)
23380Sstevel@tonic-gate {
23390Sstevel@tonic-gate 	return (CFGA_OK);
23400Sstevel@tonic-gate }
23410Sstevel@tonic-gate 
23420Sstevel@tonic-gate /*
23430Sstevel@tonic-gate  * Pass errstring as a separate argument. Some higher level routines need
23440Sstevel@tonic-gate  * it to be NULL.
23450Sstevel@tonic-gate  */
23460Sstevel@tonic-gate static cfga_err_t
23470Sstevel@tonic-gate stat_plugin_v1(list_stat_t *lstatp, lib_loc_t *libloc_p, char **errstring)
23480Sstevel@tonic-gate {
23490Sstevel@tonic-gate 	stat_data_list_t *slp, *slp2 = NULL;
23500Sstevel@tonic-gate 	cfga_err_t rc;
23510Sstevel@tonic-gate 
23520Sstevel@tonic-gate 	/*
23530Sstevel@tonic-gate 	 * allocate stat data buffer and list element
23540Sstevel@tonic-gate 	 */
23550Sstevel@tonic-gate 	if ((slp = config_calloc_check(1, sizeof (stat_data_list_t),
23560Sstevel@tonic-gate 	    errstring)) == NULL) {
23570Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
23580Sstevel@tonic-gate 	}
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate 	/*
23610Sstevel@tonic-gate 	 * Do the stat
23620Sstevel@tonic-gate 	 */
23630Sstevel@tonic-gate 	errno = 0;
23640Sstevel@tonic-gate 	if ((rc = (*(libloc_p->libp->cfga_stat_p))(libloc_p->ap_physical,
23650Sstevel@tonic-gate 	    &slp->stat_data, lstatp->opts, errstring)) != CFGA_OK) {
23660Sstevel@tonic-gate 		S_FREE(slp);
23670Sstevel@tonic-gate 		return (rc);
23680Sstevel@tonic-gate 	}
23690Sstevel@tonic-gate 	slp->next = NULL;
23700Sstevel@tonic-gate 
23710Sstevel@tonic-gate 	/*
23720Sstevel@tonic-gate 	 * Set up the logical and physical id's.
23730Sstevel@tonic-gate 	 * For v1 interfaces, the generic library (libcfgadm) creates the
23740Sstevel@tonic-gate 	 * ap_ids. mklog() is assumed to have been called in
23750Sstevel@tonic-gate 	 * the caller of this routine.
23760Sstevel@tonic-gate 	 */
23770Sstevel@tonic-gate 	(void) snprintf(slp->stat_data.ap_log_id, CFGA_AP_LOG_ID_LEN, "%s",
23780Sstevel@tonic-gate 	    libloc_p->ap_logical);
23790Sstevel@tonic-gate 
23800Sstevel@tonic-gate 	(void) snprintf(slp->stat_data.ap_phys_id, CFGA_AP_PHYS_ID_LEN, "%s",
23810Sstevel@tonic-gate 	    libloc_p->ap_physical);
23820Sstevel@tonic-gate 
23830Sstevel@tonic-gate 	/*
23840Sstevel@tonic-gate 	 * link it in
23850Sstevel@tonic-gate 	 */
23860Sstevel@tonic-gate 	if ((slp2 = lstatp->sdl) == NULL) {
23870Sstevel@tonic-gate 		lstatp->sdl = slp;
23880Sstevel@tonic-gate 	} else {
23890Sstevel@tonic-gate 		while (slp2->next != NULL)
23900Sstevel@tonic-gate 			slp2 = slp2->next;
23910Sstevel@tonic-gate 		slp2->next = slp;
23920Sstevel@tonic-gate 	}
23930Sstevel@tonic-gate 
23940Sstevel@tonic-gate 	/* keep count */
23950Sstevel@tonic-gate 	(*lstatp->countp)++;
23960Sstevel@tonic-gate 
23970Sstevel@tonic-gate 	return (CFGA_OK);
23980Sstevel@tonic-gate }
23990Sstevel@tonic-gate 
24000Sstevel@tonic-gate static cfga_err_t
24010Sstevel@tonic-gate stat_plugin_v2(list_stat_t *lstatp, lib_loc_t *libloc_p, char **errstring)
24020Sstevel@tonic-gate {
24030Sstevel@tonic-gate 	int i;
24040Sstevel@tonic-gate 	array_list_t *alp, *alp2 = NULL;
24050Sstevel@tonic-gate 	cfga_err_t rc;
24060Sstevel@tonic-gate 	char *class;
24070Sstevel@tonic-gate 
24080Sstevel@tonic-gate 	/*
24090Sstevel@tonic-gate 	 * allocate array list
24100Sstevel@tonic-gate 	 */
24110Sstevel@tonic-gate 	if ((alp = config_calloc_check(1, sizeof (array_list_t),
24120Sstevel@tonic-gate 	    errstring)) == NULL) {
24130Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
24140Sstevel@tonic-gate 	}
24150Sstevel@tonic-gate 
24160Sstevel@tonic-gate 	alp->array = NULL;
24170Sstevel@tonic-gate 	alp->nelem = 0;
24180Sstevel@tonic-gate 
24190Sstevel@tonic-gate 	/*
24200Sstevel@tonic-gate 	 * The listopts argument is currently unused. Use NULL
24210Sstevel@tonic-gate 	 */
24220Sstevel@tonic-gate 	errno = 0;
24230Sstevel@tonic-gate 	if ((rc = (*(libloc_p->libp->cfga_list_ext_p))(
24240Sstevel@tonic-gate 	    libloc_p->ap_physical, &alp->array, &alp->nelem, lstatp->opts, NULL,
24250Sstevel@tonic-gate 	    errstring, lstatp->flags)) != CFGA_OK || alp->nelem <= 0) {
24260Sstevel@tonic-gate 		S_FREE(alp);
24270Sstevel@tonic-gate 		return (rc);
24280Sstevel@tonic-gate 	}
24290Sstevel@tonic-gate 	alp->next = NULL;
24300Sstevel@tonic-gate 
24310Sstevel@tonic-gate 	/*
24320Sstevel@tonic-gate 	 * Set up the logical and physical id's if necessary.
24330Sstevel@tonic-gate 	 * For v2 interfaces, the generic library (libcfgadm) creates the
24340Sstevel@tonic-gate 	 * ap_ids only if there are no dynamic attachment points and the
24350Sstevel@tonic-gate 	 * plug-in does not create the name itself.  mklog() is
24360Sstevel@tonic-gate 	 * assumed to have been called in the caller of this routine.
24370Sstevel@tonic-gate 	 */
24380Sstevel@tonic-gate 	if (alp->nelem == 1) {
24390Sstevel@tonic-gate 		char cphys, clog;
24400Sstevel@tonic-gate 
24410Sstevel@tonic-gate 		clog = (alp->array[0]).ap_log_id[0];
24420Sstevel@tonic-gate 		cphys = (alp->array[0]).ap_phys_id[0];
24430Sstevel@tonic-gate 
24440Sstevel@tonic-gate 		if (clog == '\0') {
24450Sstevel@tonic-gate 			(void) snprintf((alp->array[0]).ap_log_id,
24460Sstevel@tonic-gate 			    sizeof ((alp->array[0]).ap_log_id), "%s",
24470Sstevel@tonic-gate 			    libloc_p->ap_logical);
24480Sstevel@tonic-gate 		}
24490Sstevel@tonic-gate 
24500Sstevel@tonic-gate 		if (cphys == '\0') {
24510Sstevel@tonic-gate 			(void) snprintf((alp->array[0]).ap_phys_id,
24520Sstevel@tonic-gate 			    sizeof ((alp->array[0]).ap_phys_id), "%s",
24530Sstevel@tonic-gate 			    libloc_p->ap_physical);
24540Sstevel@tonic-gate 		}
24550Sstevel@tonic-gate 	}
24560Sstevel@tonic-gate 
24570Sstevel@tonic-gate 	if (libloc_p->ap_class[0] == '\0') {
24580Sstevel@tonic-gate 		class = CFGA_NO_CLASS;
24590Sstevel@tonic-gate 	} else {
24600Sstevel@tonic-gate 		class = libloc_p->ap_class;
24610Sstevel@tonic-gate 	}
24620Sstevel@tonic-gate 
24630Sstevel@tonic-gate 	/* Fill in the class information for all list elements */
24640Sstevel@tonic-gate 	for (i = 0; i < alp->nelem; i++) {
24650Sstevel@tonic-gate 		(void) snprintf((alp->array[i]).ap_class,
24660Sstevel@tonic-gate 		    sizeof ((alp->array[i]).ap_class), "%s", class);
24670Sstevel@tonic-gate 	}
24680Sstevel@tonic-gate 
24690Sstevel@tonic-gate 	/*
24700Sstevel@tonic-gate 	 * link it in
24710Sstevel@tonic-gate 	 */
24720Sstevel@tonic-gate 	if ((alp2 = lstatp->al) == NULL) {
24730Sstevel@tonic-gate 		lstatp->al = alp;
24740Sstevel@tonic-gate 	} else {
24750Sstevel@tonic-gate 		while (alp2->next != NULL)
24760Sstevel@tonic-gate 			alp2 = alp2->next;
24770Sstevel@tonic-gate 		alp2->next = alp;
24780Sstevel@tonic-gate 	}
24790Sstevel@tonic-gate 
24800Sstevel@tonic-gate 	/* keep count */
24810Sstevel@tonic-gate 	(*lstatp->countp) += alp->nelem;
24820Sstevel@tonic-gate 
24830Sstevel@tonic-gate 	return (CFGA_OK);
24840Sstevel@tonic-gate }
24850Sstevel@tonic-gate 
24860Sstevel@tonic-gate /*
24870Sstevel@tonic-gate  * Check if a plugin version is within requested limits.
24880Sstevel@tonic-gate  */
24890Sstevel@tonic-gate static int
24900Sstevel@tonic-gate compat_plugin(vers_req_t *reqp, int plugin_vers)
24910Sstevel@tonic-gate {
24920Sstevel@tonic-gate 
24930Sstevel@tonic-gate 	if (!VALID_HSL_VERS(reqp->v_min) || !VALID_HSL_VERS(reqp->v_max) ||
24940Sstevel@tonic-gate 	    !VALID_HSL_VERS(plugin_vers)) {
24950Sstevel@tonic-gate 		return (0);
24960Sstevel@tonic-gate 	}
24970Sstevel@tonic-gate 
24980Sstevel@tonic-gate 	if (plugin_vers < reqp->v_min || plugin_vers > reqp->v_max) {
24990Sstevel@tonic-gate 		return (0);
25000Sstevel@tonic-gate 	}
25010Sstevel@tonic-gate 
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate 	return (1);
25040Sstevel@tonic-gate }
25050Sstevel@tonic-gate 
25060Sstevel@tonic-gate /*
25070Sstevel@tonic-gate  * find_arg_type - determine if an argument is an ap_id or an ap_type.
25080Sstevel@tonic-gate  * Adapted from cfgadm.c
25090Sstevel@tonic-gate  */
25100Sstevel@tonic-gate static cfga_ap_types_t
25110Sstevel@tonic-gate find_arg_type(const char *ap_id)
25120Sstevel@tonic-gate {
25130Sstevel@tonic-gate 	struct stat sbuf;
25140Sstevel@tonic-gate 	cfga_ap_types_t type = UNKNOWN_AP;
25150Sstevel@tonic-gate 	char *mkr = NULL;
25160Sstevel@tonic-gate 	size_t len;
25170Sstevel@tonic-gate 	int size_ap = 0, size_mkr = 0, digit = 0, i = 0;
25180Sstevel@tonic-gate 	char *cp, path[MAXPATHLEN], ap_base[MAXPATHLEN];
25190Sstevel@tonic-gate 
25200Sstevel@tonic-gate 
25210Sstevel@tonic-gate 	/*
25220Sstevel@tonic-gate 	 * sanity checks
25230Sstevel@tonic-gate 	 */
25240Sstevel@tonic-gate 	if (ap_id == NULL || *ap_id == '\0') {
25250Sstevel@tonic-gate 
25260Sstevel@tonic-gate 		return (UNKNOWN_AP);
25270Sstevel@tonic-gate 	}
25280Sstevel@tonic-gate 
25290Sstevel@tonic-gate 	/*
25300Sstevel@tonic-gate 	 * Extract the base component
25310Sstevel@tonic-gate 	 */
25320Sstevel@tonic-gate 	if ((cp = GET_DYN(ap_id)) != NULL) {
25330Sstevel@tonic-gate 		len = cp - ap_id;
25340Sstevel@tonic-gate 	} else {
25350Sstevel@tonic-gate 		len = strlen(ap_id);
25360Sstevel@tonic-gate 	}
25370Sstevel@tonic-gate 
25380Sstevel@tonic-gate 	if (len >= sizeof (ap_base)) {
25390Sstevel@tonic-gate 		return (UNKNOWN_AP);
25400Sstevel@tonic-gate 	}
25410Sstevel@tonic-gate 
25420Sstevel@tonic-gate 	/* Copy only the first "len" chars */
25430Sstevel@tonic-gate 	(void) strncpy(ap_base, ap_id, len);
25440Sstevel@tonic-gate 	ap_base[len] = '\0';
25450Sstevel@tonic-gate 
25460Sstevel@tonic-gate 	/*
25470Sstevel@tonic-gate 	 * If it starts with a slash and is stat-able its a physical.
25480Sstevel@tonic-gate 	 */
25490Sstevel@tonic-gate 	if (*ap_base == '/' && stat(ap_base, &sbuf) == 0) {
25500Sstevel@tonic-gate 		return (PHYSICAL_AP);
25510Sstevel@tonic-gate 	}
25520Sstevel@tonic-gate 
25530Sstevel@tonic-gate 	/*
25540Sstevel@tonic-gate 	 * Is this a symlink in CFGA_DEV_DIR ?
25550Sstevel@tonic-gate 	 */
25560Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s%s",
25570Sstevel@tonic-gate 	    CFGA_DEV_DIR SLASH, ap_base);
25580Sstevel@tonic-gate 
25590Sstevel@tonic-gate 	if (lstat(path, &sbuf) == 0 && S_ISLNK(sbuf.st_mode) &&
25600Sstevel@tonic-gate 	    stat(path, &sbuf) == 0) {
25610Sstevel@tonic-gate 		return (LOGICAL_LINK_AP);
25620Sstevel@tonic-gate 	}
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate 	/*
25650Sstevel@tonic-gate 	 * Check for ":" which is always present in an ap_id
25660Sstevel@tonic-gate 	 * but not in an ap_type.
25670Sstevel@tonic-gate 	 * we need to check that the characters right before the : are digits
25680Sstevel@tonic-gate 	 * since an ap_id is of the form <name><instance>:<specific ap name>
25690Sstevel@tonic-gate 	 */
25700Sstevel@tonic-gate 	if ((mkr = strchr(ap_base, ':')) == NULL)  {
25710Sstevel@tonic-gate 		type = AP_TYPE;
25720Sstevel@tonic-gate 	} else {
25730Sstevel@tonic-gate 		size_ap = strlen(ap_base);
25740Sstevel@tonic-gate 		size_mkr = strlen(mkr);
25750Sstevel@tonic-gate 		mkr = ap_base;
25760Sstevel@tonic-gate 
25770Sstevel@tonic-gate 		digit = 0;
25780Sstevel@tonic-gate 		for (i = size_ap - size_mkr - 1;  i > 0; i--) {
25790Sstevel@tonic-gate 			if ((int)isdigit(mkr[i])) {
25800Sstevel@tonic-gate 				digit++;
25810Sstevel@tonic-gate 				break;
25820Sstevel@tonic-gate 			}
25830Sstevel@tonic-gate 		}
25840Sstevel@tonic-gate 		if (digit == 0) {
25850Sstevel@tonic-gate 			type = AP_TYPE;
25860Sstevel@tonic-gate 		} else {
25870Sstevel@tonic-gate 			type = LOGICAL_DRV_AP;
25880Sstevel@tonic-gate 		}
25890Sstevel@tonic-gate 	}
25900Sstevel@tonic-gate 
25910Sstevel@tonic-gate 	return (type);
25920Sstevel@tonic-gate }
25930Sstevel@tonic-gate 
25940Sstevel@tonic-gate /*ARGSUSED*/
25950Sstevel@tonic-gate static cfga_err_t
25960Sstevel@tonic-gate null_get_cond(lib_loc_t *liblocp, cfga_cond_t *condp, char **errstring)
25970Sstevel@tonic-gate {
25980Sstevel@tonic-gate 	return (CFGA_OK);
25990Sstevel@tonic-gate }
26000Sstevel@tonic-gate 
26010Sstevel@tonic-gate static cfga_err_t
26020Sstevel@tonic-gate get_cond_v1(lib_loc_t *liblocp, cfga_cond_t *condp, char **errstring)
26030Sstevel@tonic-gate {
26040Sstevel@tonic-gate 	plugin_lib_t *libp;
26050Sstevel@tonic-gate 	cfga_stat_data_t sdbuf;
26060Sstevel@tonic-gate 	cfga_err_t rc;
26070Sstevel@tonic-gate 
26080Sstevel@tonic-gate 
26090Sstevel@tonic-gate 	libp = liblocp->libp;
26100Sstevel@tonic-gate 	if (libp->plugin_vers != CFGA_HSL_V1) {
26110Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
26120Sstevel@tonic-gate 	}
26130Sstevel@tonic-gate 
26140Sstevel@tonic-gate 	errno = 0;
26150Sstevel@tonic-gate 	if ((rc = (*liblocp->libp->cfga_stat_p)(
26160Sstevel@tonic-gate 	    liblocp->ap_physical, &sdbuf, NULL, errstring))
26170Sstevel@tonic-gate 	    == CFGA_OK) {
26180Sstevel@tonic-gate 		*condp = sdbuf.ap_cond;
26190Sstevel@tonic-gate 	} else {
26200Sstevel@tonic-gate 		*condp = CFGA_COND_UNKNOWN;
26210Sstevel@tonic-gate 	}
26220Sstevel@tonic-gate 
26230Sstevel@tonic-gate 	return (rc);
26240Sstevel@tonic-gate }
26250Sstevel@tonic-gate 
26260Sstevel@tonic-gate static cfga_err_t
26270Sstevel@tonic-gate get_cond_v2(lib_loc_t *liblocp, cfga_cond_t *condp, char **errstring)
26280Sstevel@tonic-gate {
26290Sstevel@tonic-gate 	int nelem;
26300Sstevel@tonic-gate 	plugin_lib_t *libp;
26310Sstevel@tonic-gate 	cfga_list_data_t *ldbufp;
26320Sstevel@tonic-gate 	cfga_err_t rc;
26330Sstevel@tonic-gate 
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate 	libp = liblocp->libp;
26360Sstevel@tonic-gate 	if (libp->plugin_vers != CFGA_HSL_V2) {
26370Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
26380Sstevel@tonic-gate 	}
26390Sstevel@tonic-gate 
26400Sstevel@tonic-gate 	errno = 0;
26410Sstevel@tonic-gate 	nelem = 0;
26420Sstevel@tonic-gate 	ldbufp = NULL;
26430Sstevel@tonic-gate 	if ((rc = (*liblocp->libp->cfga_list_ext_p)(
26440Sstevel@tonic-gate 	    liblocp->ap_physical, &ldbufp, &nelem, NULL, NULL,
26450Sstevel@tonic-gate 	    errstring, 0)) == CFGA_OK) {
26460Sstevel@tonic-gate 		assert(nelem == 1 && ldbufp != NULL);
26470Sstevel@tonic-gate 
26480Sstevel@tonic-gate 		*condp = ldbufp->ap_cond;
26490Sstevel@tonic-gate 		S_FREE(ldbufp);
26500Sstevel@tonic-gate 	} else {
26510Sstevel@tonic-gate 		*condp = CFGA_COND_UNKNOWN;
26520Sstevel@tonic-gate 	}
26530Sstevel@tonic-gate 
26540Sstevel@tonic-gate 	return (rc);
26550Sstevel@tonic-gate }
26560Sstevel@tonic-gate 
26570Sstevel@tonic-gate /* mask represents the flags accepted */
26580Sstevel@tonic-gate static cfga_err_t
26590Sstevel@tonic-gate check_flags(cfga_flags_t flags, cfga_flags_t mask, char **errstring)
26600Sstevel@tonic-gate {
26610Sstevel@tonic-gate 	if ((flags & ~mask) != 0) {
26620Sstevel@tonic-gate 		config_err(0, INVALID_ARGS, errstring);
26630Sstevel@tonic-gate 		return (CFGA_ERROR);
26640Sstevel@tonic-gate 	} else {
26650Sstevel@tonic-gate 		return (CFGA_OK);
26660Sstevel@tonic-gate 	}
26670Sstevel@tonic-gate }
26680Sstevel@tonic-gate 
26690Sstevel@tonic-gate static cfga_err_t
26700Sstevel@tonic-gate check_apids(int num_ap_ids, char *const *ap_ids, char **errstring)
26710Sstevel@tonic-gate {
26720Sstevel@tonic-gate 	if (num_ap_ids <= 0 || ap_ids == NULL) {
26730Sstevel@tonic-gate 		config_err(0, INVALID_ARGS, errstring);
26740Sstevel@tonic-gate 		return (CFGA_ERROR);
26750Sstevel@tonic-gate 	} else {
26760Sstevel@tonic-gate 		return (CFGA_OK);
26770Sstevel@tonic-gate 	}
26780Sstevel@tonic-gate }
26790Sstevel@tonic-gate 
26800Sstevel@tonic-gate /*
26810Sstevel@tonic-gate  * Returns the class or the empty string if attacment point has
26820Sstevel@tonic-gate  * no class.
26830Sstevel@tonic-gate  */
26840Sstevel@tonic-gate static char *
26850Sstevel@tonic-gate get_class(di_minor_t minor)
26860Sstevel@tonic-gate {
26870Sstevel@tonic-gate 	char *cp, c;
26880Sstevel@tonic-gate 	size_t len;
26890Sstevel@tonic-gate 
26900Sstevel@tonic-gate 
26910Sstevel@tonic-gate 	if (minor == DI_MINOR_NIL) {
26920Sstevel@tonic-gate 		return (NULL);
26930Sstevel@tonic-gate 	}
26940Sstevel@tonic-gate 
26950Sstevel@tonic-gate 	cp = di_minor_nodetype(minor);
26960Sstevel@tonic-gate 	if (cp == NULL) {
26970Sstevel@tonic-gate 		return (NULL);
26980Sstevel@tonic-gate 	}
26990Sstevel@tonic-gate 
27000Sstevel@tonic-gate 	len = strlen(DDI_NT_ATTACHMENT_POINT);
27010Sstevel@tonic-gate 	if (strncmp(cp, DDI_NT_ATTACHMENT_POINT, len)) {
27020Sstevel@tonic-gate 		return (NULL);
27030Sstevel@tonic-gate 	}
27040Sstevel@tonic-gate 
27050Sstevel@tonic-gate 	cp += len;
27060Sstevel@tonic-gate 
27070Sstevel@tonic-gate 	c = *cp;
27080Sstevel@tonic-gate 	if (c != '\0' && c != ':') {
27090Sstevel@tonic-gate 		return (NULL);
27100Sstevel@tonic-gate 	}
27110Sstevel@tonic-gate 
27120Sstevel@tonic-gate 	if (c == ':') {
27130Sstevel@tonic-gate 		cp++;
27140Sstevel@tonic-gate 	}
27150Sstevel@tonic-gate 
27160Sstevel@tonic-gate 	return (cp);
27170Sstevel@tonic-gate 
27180Sstevel@tonic-gate }
27190Sstevel@tonic-gate 
27200Sstevel@tonic-gate /*
27210Sstevel@tonic-gate  * Transform stat data to list data
27220Sstevel@tonic-gate  */
27230Sstevel@tonic-gate static void
27240Sstevel@tonic-gate stat_to_list(cfga_list_data_t *lp, cfga_stat_data_t *statp)
27250Sstevel@tonic-gate {
27260Sstevel@tonic-gate 
27270Sstevel@tonic-gate 	(void) snprintf(lp->ap_log_id, sizeof (lp->ap_log_id), "%s",
27280Sstevel@tonic-gate 	    statp->ap_log_id);
27290Sstevel@tonic-gate 
27300Sstevel@tonic-gate 	(void) snprintf(lp->ap_phys_id, sizeof (lp->ap_phys_id), "%s",
27310Sstevel@tonic-gate 	    statp->ap_phys_id);
27320Sstevel@tonic-gate 
27330Sstevel@tonic-gate 	(void) snprintf(lp->ap_class, sizeof (lp->ap_class), "%s",
27340Sstevel@tonic-gate 	    CFGA_NO_CLASS);
27350Sstevel@tonic-gate 
27360Sstevel@tonic-gate 	lp->ap_r_state = statp->ap_r_state;
27370Sstevel@tonic-gate 	lp->ap_o_state = statp->ap_o_state;
27380Sstevel@tonic-gate 	lp->ap_cond = statp->ap_cond;
27390Sstevel@tonic-gate 	lp->ap_busy = statp->ap_busy;
27400Sstevel@tonic-gate 	lp->ap_status_time = statp->ap_status_time;
27410Sstevel@tonic-gate 
27420Sstevel@tonic-gate 	(void) snprintf(lp->ap_info, sizeof (lp->ap_info), "%s",
27430Sstevel@tonic-gate 	    statp->ap_info);
27440Sstevel@tonic-gate 	(void) snprintf(lp->ap_type, sizeof (lp->ap_type), "%s",
27450Sstevel@tonic-gate 	    statp->ap_type);
27460Sstevel@tonic-gate }
27470Sstevel@tonic-gate 
27480Sstevel@tonic-gate static void
27490Sstevel@tonic-gate lstat_free(list_stat_t *lstatp)
27500Sstevel@tonic-gate {
27510Sstevel@tonic-gate 	stat_data_list_t *slp, *slp2;
27520Sstevel@tonic-gate 	array_list_t *ap, *ap2;
27530Sstevel@tonic-gate 
27540Sstevel@tonic-gate 	slp = lstatp->sdl;
27550Sstevel@tonic-gate 	while (slp != NULL) {
27560Sstevel@tonic-gate 		slp2 = slp->next;
27570Sstevel@tonic-gate 		S_FREE(slp);
27580Sstevel@tonic-gate 		slp = slp2;
27590Sstevel@tonic-gate 	}
27600Sstevel@tonic-gate 
27610Sstevel@tonic-gate 	lstatp->sdl = NULL;
27620Sstevel@tonic-gate 
27630Sstevel@tonic-gate 	ap = lstatp->al;
27640Sstevel@tonic-gate 	while (ap != NULL) {
27650Sstevel@tonic-gate 		ap2 = ap->next;
27660Sstevel@tonic-gate 		S_FREE(ap->array);
27670Sstevel@tonic-gate 		S_FREE(ap);
27680Sstevel@tonic-gate 		ap = ap2;
27690Sstevel@tonic-gate 	}
27700Sstevel@tonic-gate 
27710Sstevel@tonic-gate 	lstatp->al = NULL;
27720Sstevel@tonic-gate }
27730Sstevel@tonic-gate 
27740Sstevel@tonic-gate static cfga_err_t
27750Sstevel@tonic-gate split_apid(char *ap_id, char **dyncompp, char **errstring)
27760Sstevel@tonic-gate {
27770Sstevel@tonic-gate 	char *cp;
27780Sstevel@tonic-gate 
27790Sstevel@tonic-gate 	*dyncompp = NULL;
27800Sstevel@tonic-gate 
27810Sstevel@tonic-gate 	if (ap_id == NULL) {
27820Sstevel@tonic-gate 		return (CFGA_ERROR);
27830Sstevel@tonic-gate 	}
27840Sstevel@tonic-gate 
27850Sstevel@tonic-gate 	if ((cp = strstr(ap_id, CFGA_DYN_SEP)) == NULL) {
27860Sstevel@tonic-gate 		return (CFGA_OK);
27870Sstevel@tonic-gate 	}
27880Sstevel@tonic-gate 
27890Sstevel@tonic-gate 	*cp = '\0';
27900Sstevel@tonic-gate 	cp += strlen(CFGA_DYN_SEP);
27910Sstevel@tonic-gate 	if ((*dyncompp = config_calloc_check(1, strlen(cp) + 1,
27920Sstevel@tonic-gate 	    errstring)) == NULL) {
27930Sstevel@tonic-gate 		return (CFGA_LIB_ERROR);
27940Sstevel@tonic-gate 	}
27950Sstevel@tonic-gate 	(void) strcpy(*dyncompp, cp);
27960Sstevel@tonic-gate 
27970Sstevel@tonic-gate 	return (CFGA_OK);
27980Sstevel@tonic-gate }
27990Sstevel@tonic-gate 
28000Sstevel@tonic-gate static void
28010Sstevel@tonic-gate append_dyn(char *buf, const char *dyncomp, size_t blen)
28020Sstevel@tonic-gate {
28030Sstevel@tonic-gate 	if (dyncomp != NULL) {
28040Sstevel@tonic-gate 		char *cp = buf + strlen(buf);
28050Sstevel@tonic-gate 		size_t len = blen - strlen(buf);
28060Sstevel@tonic-gate 
28070Sstevel@tonic-gate 		(void) snprintf(cp, len, "%s%s", CFGA_DYN_SEP,
28080Sstevel@tonic-gate 		    dyncomp);
28090Sstevel@tonic-gate 	}
28100Sstevel@tonic-gate }
28110Sstevel@tonic-gate 
28120Sstevel@tonic-gate /*
28130Sstevel@tonic-gate  * Default implementation of cfga_ap_id_cmp. Works for most cases
28140Sstevel@tonic-gate  * except for long hex number sequences like world-wide-name.
28150Sstevel@tonic-gate  *
28160Sstevel@tonic-gate  * This function compares the ap's in a generic way.  It does so by
28170Sstevel@tonic-gate  * determining the place of difference between the 2 aps.  If the first
28180Sstevel@tonic-gate  * difference is a digit, it attempts to obtain the numbers and compare them
28190Sstevel@tonic-gate  * Otherwise it just compares the aps as strings
28200Sstevel@tonic-gate  */
28210Sstevel@tonic-gate static int
28220Sstevel@tonic-gate default_ap_id_cmp(const char *ap_id1, const char *ap_id2)
28230Sstevel@tonic-gate {
28240Sstevel@tonic-gate 	int i = 0;
28250Sstevel@tonic-gate 
28260Sstevel@tonic-gate 	/*
28270Sstevel@tonic-gate 	 * Search for first different char
28280Sstevel@tonic-gate 	 */
28290Sstevel@tonic-gate 	while (ap_id1[i] == ap_id2[i] && ap_id1[i] != '\0')
28300Sstevel@tonic-gate 		i++;
28310Sstevel@tonic-gate 
28320Sstevel@tonic-gate 	/*
28330Sstevel@tonic-gate 	 * If one of the char is a digit, back up to where the
28340Sstevel@tonic-gate 	 * number started, compare the number.
28350Sstevel@tonic-gate 	 */
28360Sstevel@tonic-gate 	if (isdigit(ap_id1[i]) || isdigit(ap_id2[i])) {
28370Sstevel@tonic-gate 		while ((i > 0) && isdigit(ap_id1[i - 1]))
28380Sstevel@tonic-gate 			i--;
28390Sstevel@tonic-gate 
28400Sstevel@tonic-gate 		if (isdigit(ap_id1[i]) && isdigit(ap_id2[i]))
28410Sstevel@tonic-gate 			return (atoi(ap_id1 + i) - atoi(ap_id2 + i));
28420Sstevel@tonic-gate 	}
28430Sstevel@tonic-gate 
28440Sstevel@tonic-gate 	/* One of them isn't a number, compare the char */
28450Sstevel@tonic-gate 	return (ap_id1[i] - ap_id2[i]);
28460Sstevel@tonic-gate }
28470Sstevel@tonic-gate 
28480Sstevel@tonic-gate static void
28490Sstevel@tonic-gate hold_lib(plugin_lib_t *libp)
28500Sstevel@tonic-gate {
28510Sstevel@tonic-gate 	assert(libp->refcnt >= 0);
28520Sstevel@tonic-gate 	(void) mutex_lock(&libp->lock);
28530Sstevel@tonic-gate 	libp->refcnt++;
28540Sstevel@tonic-gate 	(void) mutex_unlock(&libp->lock);
28550Sstevel@tonic-gate }
28560Sstevel@tonic-gate 
28570Sstevel@tonic-gate static void
28580Sstevel@tonic-gate rele_lib(plugin_lib_t *libp)
28590Sstevel@tonic-gate {
28600Sstevel@tonic-gate 	assert(libp->refcnt > 0);
28610Sstevel@tonic-gate 	(void) mutex_lock(&libp->lock);
28620Sstevel@tonic-gate 	libp->refcnt--;
28630Sstevel@tonic-gate 	(void) mutex_unlock(&libp->lock);
28640Sstevel@tonic-gate }
2865