xref: /illumos-gate/usr/src/uts/common/os/dacf.c (revision 7bcaeddb)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5d62bc4baSyz  * Common Development and Distribution License (the "License").
6d62bc4baSyz  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22d62bc4baSyz  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * DACF: device autoconfiguration support
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  * DACF provides a fast, lightweight policy engine for the I/O subsystem.
307c478bd9Sstevel@tonic-gate  * This policy engine provides a mechanism for auto-configuring and
317c478bd9Sstevel@tonic-gate  * auto-unconfiguring devices.
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * After a device is attach(9E)ed, additional configuration may be needed in
347c478bd9Sstevel@tonic-gate  * order to make the device available for use by the system.  For example,
357c478bd9Sstevel@tonic-gate  * STREAMS modules may need to be pushed atop the driver in order to create
367c478bd9Sstevel@tonic-gate  * a STREAMS stack.  If the device is to be removed from the system, these
377c478bd9Sstevel@tonic-gate  * configuration operations need to be undone, and the device prepared for
387c478bd9Sstevel@tonic-gate  * detach(9E).
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * It is desirable to move the implementation of such policies outside of the
417c478bd9Sstevel@tonic-gate  * kernel proper, since such operations are typically infrequent.  To this end,
427c478bd9Sstevel@tonic-gate  * DACF manages kernel modules in (module_path)/dacf directories.  These adhere
437c478bd9Sstevel@tonic-gate  * to the api defined in sys/dacf.h, and register sets of configuration
447c478bd9Sstevel@tonic-gate  * operations.  The kernel loads these modules when the operations they
457c478bd9Sstevel@tonic-gate  * implement are needed, and can unload them at any time thereafter.
467c478bd9Sstevel@tonic-gate  * Implementing configuration operations in external modules can also increase
477c478bd9Sstevel@tonic-gate  * code reuse.
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  * DACF provides a policy database which associates
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  *   (device descr., kernel action) --> (configuration operation, parameters)
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * - Device description is matching rule, for example:
547c478bd9Sstevel@tonic-gate  * 	minor-nodetype="ddi_keyboard"
557c478bd9Sstevel@tonic-gate  * - Kernel action is a reference to a dacf kernel hook.
567c478bd9Sstevel@tonic-gate  *      currently supported are "post-attach" and "pre-detach"
577c478bd9Sstevel@tonic-gate  * - Configuration action is a reference to a module and a set of operations
587c478bd9Sstevel@tonic-gate  *      within the module, for example:  consconfig:kbd_config
597c478bd9Sstevel@tonic-gate  * - Parameters is a list of name="value" parameters to be passed to the
607c478bd9Sstevel@tonic-gate  *      configuration operation when invoked.
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  * The contents of the rules database are loaded from /etc/dacf.conf upon boot.
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  * DACF kernel hooks are comprised of a call into the rule-matching engine,
657c478bd9Sstevel@tonic-gate  * using parameters from the hook in order find a matching rule.  If one is
667c478bd9Sstevel@tonic-gate  * found, the framework can invoke the configuration operation immediately, or
677c478bd9Sstevel@tonic-gate  * defer doing so until later, by putting the rule on a 'reservation list.'
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include <sys/param.h>
717c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
727c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
737c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
747c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
757c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
767c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
777c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
787c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
797c478bd9Sstevel@tonic-gate #include <sys/modhash.h>
807c478bd9Sstevel@tonic-gate #include <sys/dacf.h>
817c478bd9Sstevel@tonic-gate #include <sys/dacf_impl.h>
827c478bd9Sstevel@tonic-gate #include <sys/systm.h>
837c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
847c478bd9Sstevel@tonic-gate #include <sys/debug.h>
857c478bd9Sstevel@tonic-gate #include <sys/log.h>
867c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h>
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * Enumeration of the ops exported by the dacf framework.
907c478bd9Sstevel@tonic-gate  *
917c478bd9Sstevel@tonic-gate  * To add a new op to the framework, add it to this list, update dacf.h,
927c478bd9Sstevel@tonic-gate  * (don't miss DACF_NUM_OPIDS) and modify dacf_rule_matrix.
937c478bd9Sstevel@tonic-gate  *
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate typedef struct dacf_opmap {
967c478bd9Sstevel@tonic-gate 	const char *name;
977c478bd9Sstevel@tonic-gate 	dacf_opid_t id;
987c478bd9Sstevel@tonic-gate } dacf_opmap_t;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate static dacf_opmap_t dacf_ops[] = {
1017c478bd9Sstevel@tonic-gate 	{ "post-attach",	DACF_OPID_POSTATTACH		},
1027c478bd9Sstevel@tonic-gate 	{ "pre-detach",		DACF_OPID_PREDETACH		},
1037c478bd9Sstevel@tonic-gate 	{ NULL,			0				},
1047c478bd9Sstevel@tonic-gate };
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate  * Enumeration of the options exported by the dacf framework (currently none).
1087c478bd9Sstevel@tonic-gate  *
1097c478bd9Sstevel@tonic-gate  * To add a new option, add it to this array.
1107c478bd9Sstevel@tonic-gate  */
1117c478bd9Sstevel@tonic-gate typedef struct dacf_opt {
1127c478bd9Sstevel@tonic-gate 	const char *optname;
1137c478bd9Sstevel@tonic-gate 	uint_t optmask;
1147c478bd9Sstevel@tonic-gate } dacf_opt_t;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate static dacf_opt_t dacf_options[] = {
1177c478bd9Sstevel@tonic-gate #ifdef DEBUG
1187c478bd9Sstevel@tonic-gate 	{ "testopt", 		1		},
1197c478bd9Sstevel@tonic-gate 	{ "testopt2", 		2		},
1207c478bd9Sstevel@tonic-gate #endif
1217c478bd9Sstevel@tonic-gate 	{ NULL, 		0		},
1227c478bd9Sstevel@tonic-gate };
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate static char kmod_name[] = "__kernel";
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * Enumeration of the device specifiers exported by the dacf framework.
1287c478bd9Sstevel@tonic-gate  *
1297c478bd9Sstevel@tonic-gate  * To add a new devspec to the framework, add it to this list, update dacf.h,
1307c478bd9Sstevel@tonic-gate  * (don't miss DACF_NUM_DEVSPECS), modify dacf_rule_matrix, and modify
1317c478bd9Sstevel@tonic-gate  * dacf_match().
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate typedef struct dacf_ds {
1347c478bd9Sstevel@tonic-gate 	const char *name;
1357c478bd9Sstevel@tonic-gate 	dacf_devspec_t id;
1367c478bd9Sstevel@tonic-gate } dacf_ds_t;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate static dacf_ds_t dacf_devspecs[] = {
1397c478bd9Sstevel@tonic-gate 	{ "minor-nodetype", 	DACF_DS_MIN_NT 		},
1407c478bd9Sstevel@tonic-gate 	{ "driver-minorname", 	DACF_DS_DRV_MNAME	},
1417c478bd9Sstevel@tonic-gate 	{ "device-path",	DACF_DS_DEV_PATH	},
1427e12ceb3SToomas Soome 	{ NULL,			DACF_DS_ERROR		},
1437c478bd9Sstevel@tonic-gate };
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate mod_hash_t *posta_mntype, *posta_mname, *posta_devname;	/* post-attach */
1467c478bd9Sstevel@tonic-gate mod_hash_t *pred_mntype, *pred_mname, *pred_devname;	/* pre-detach */
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate mod_hash_t *dacf_module_hash;
1497c478bd9Sstevel@tonic-gate mod_hash_t *dacf_info_hash;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate  * This is the lookup table for the hash tables that dacf manages.  Given an
1537c478bd9Sstevel@tonic-gate  * op id and devspec type, one can obtain the hash for that type of data.
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate mod_hash_t **dacf_rule_matrix[DACF_NUM_OPIDS][DACF_NUM_DEVSPECS] = {
1567c478bd9Sstevel@tonic-gate 	{ &posta_mntype, 	&posta_mname,	&posta_devname	},
1577c478bd9Sstevel@tonic-gate 	{ &pred_mntype,		&pred_mname,	&pred_devname	},
1587c478bd9Sstevel@tonic-gate };
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate kmutex_t dacf_lock;
1617c478bd9Sstevel@tonic-gate kmutex_t dacf_module_lock;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate int dacfdebug = 0;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate static dacf_rule_t *dacf_rule_ctor(char *, char *, char *, dacf_opid_t,
1667c478bd9Sstevel@tonic-gate     uint_t, dacf_arg_t *);
1677c478bd9Sstevel@tonic-gate static mod_hash_t *dacf_get_op_hash(dacf_opid_t, dacf_devspec_t);
1687c478bd9Sstevel@tonic-gate static void dacf_rule_val_dtor(mod_hash_val_t);
1697c478bd9Sstevel@tonic-gate static void dacf_destroy_opsets(dacf_module_t *module);
1707c478bd9Sstevel@tonic-gate static void dacf_opset_copy(dacf_opset_t *dst, dacf_opset_t *src);
1717c478bd9Sstevel@tonic-gate static void dprintf(const char *, ...) __KPRINTFLIKE(1);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
1747c478bd9Sstevel@tonic-gate static void
dprintf(const char * format,...)1757c478bd9Sstevel@tonic-gate dprintf(const char *format, ...)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	va_list alist;
1787c478bd9Sstevel@tonic-gate 	char dp_buf[256], *dpbp;
1797c478bd9Sstevel@tonic-gate 	if (dacfdebug & DACF_DBG_MSGS) {
1807c478bd9Sstevel@tonic-gate 		va_start(alist, format);
1817c478bd9Sstevel@tonic-gate 		/*
1827c478bd9Sstevel@tonic-gate 		 * sprintf up the string that is 'dacf debug: <the message>'
1837c478bd9Sstevel@tonic-gate 		 */
1847c478bd9Sstevel@tonic-gate 		(void) sprintf(dp_buf, "dacf debug: ");
1857c478bd9Sstevel@tonic-gate 		dpbp = &(dp_buf[strlen(dp_buf)]);
1867c478bd9Sstevel@tonic-gate 		(void) vsnprintf(dpbp, sizeof (dp_buf) - strlen(dp_buf),
1877c478bd9Sstevel@tonic-gate 		    format, alist);
1887c478bd9Sstevel@tonic-gate 		printf(dp_buf);
1897c478bd9Sstevel@tonic-gate 		va_end(alist);
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate /*
1947c478bd9Sstevel@tonic-gate  * dacf_init()
1957c478bd9Sstevel@tonic-gate  * 	initialize the dacf framework by creating the various hash tables.
1967c478bd9Sstevel@tonic-gate  */
1977c478bd9Sstevel@tonic-gate void
dacf_init()1987c478bd9Sstevel@tonic-gate dacf_init()
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	int i, j;
2017c478bd9Sstevel@tonic-gate 	char hbuf[40];
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	dprintf("dacf_init: creating hashmatrix\n");
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate #ifdef DEBUG
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * Sanity check that DACF_NUM_DEVSPECS and the devspecs are in sync
2107c478bd9Sstevel@tonic-gate 	 */
2117c478bd9Sstevel@tonic-gate 	for (i = 0; dacf_devspecs[i].name != NULL; i++)
2127c478bd9Sstevel@tonic-gate 		continue;
2137c478bd9Sstevel@tonic-gate 	ASSERT(i == DACF_NUM_DEVSPECS);
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	/*
2167c478bd9Sstevel@tonic-gate 	 * Sanity check that DACF_NUM_OPIDS and the dacf_ops are in sync
2177c478bd9Sstevel@tonic-gate 	 */
2187c478bd9Sstevel@tonic-gate 	for (i = 0; dacf_ops[i].name != NULL; i++)
2197c478bd9Sstevel@tonic-gate 		continue;
2207c478bd9Sstevel@tonic-gate 	ASSERT(i == DACF_NUM_OPIDS);
2217c478bd9Sstevel@tonic-gate #endif
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	for (i = 0; i < DACF_NUM_OPIDS; i++) {
2247c478bd9Sstevel@tonic-gate 		for (j = 0; j < DACF_NUM_DEVSPECS; j++) {
2257c478bd9Sstevel@tonic-gate 			if (dacf_rule_matrix[i][j] == NULL) {
2267c478bd9Sstevel@tonic-gate 				continue;
2277c478bd9Sstevel@tonic-gate 			}
2287c478bd9Sstevel@tonic-gate 			/*
2297c478bd9Sstevel@tonic-gate 			 * Set up a hash table with no key destructor.  The
2307c478bd9Sstevel@tonic-gate 			 * keys are carried in the rule_t, so the val_dtor
2317c478bd9Sstevel@tonic-gate 			 * will take care of the key as well.
2327c478bd9Sstevel@tonic-gate 			 */
2337c478bd9Sstevel@tonic-gate 			(void) snprintf(hbuf, sizeof (hbuf),
2347c478bd9Sstevel@tonic-gate 			    "dacf hashmatrix [%d][%d]", i, j);
2357c478bd9Sstevel@tonic-gate 			*(dacf_rule_matrix[i][j]) = mod_hash_create_extended(
2367c478bd9Sstevel@tonic-gate 			    hbuf,			/* hash name */
2377c478bd9Sstevel@tonic-gate 			    DACF_RULE_HASHSIZE,		/* # hash elems */
2387c478bd9Sstevel@tonic-gate 			    mod_hash_null_keydtor,	/* key dtor */
2397c478bd9Sstevel@tonic-gate 			    dacf_rule_val_dtor,		/* value dtor */
2407c478bd9Sstevel@tonic-gate 			    mod_hash_bystr, NULL, 	/* hash alg & data */
2417c478bd9Sstevel@tonic-gate 			    mod_hash_strkey_cmp,	/* key comparator */
2427c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	dprintf("dacf_init: creating module_hash\n");
2477c478bd9Sstevel@tonic-gate 	/*
2487c478bd9Sstevel@tonic-gate 	 * dacf_module_hash stores the currently registered dacf modules
2497c478bd9Sstevel@tonic-gate 	 * by name.
2507c478bd9Sstevel@tonic-gate 	 */
2517c478bd9Sstevel@tonic-gate 	dacf_module_hash = mod_hash_create_strhash("dacf module hash",
2527c478bd9Sstevel@tonic-gate 	    DACF_MODULE_HASHSIZE, mod_hash_null_valdtor);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	dprintf("dacf_init: creating info_hash\n");
2557c478bd9Sstevel@tonic-gate 	/*
2567c478bd9Sstevel@tonic-gate 	 * dacf_info_hash stores pointers to data that modules can associate
2577c478bd9Sstevel@tonic-gate 	 * on a per minornode basis.  The type of data stored is opaque to the
2587c478bd9Sstevel@tonic-gate 	 * framework-- thus there is no destructor supplied.
2597c478bd9Sstevel@tonic-gate 	 */
2607c478bd9Sstevel@tonic-gate 	dacf_info_hash = mod_hash_create_ptrhash("dacf info hash",
2617c478bd9Sstevel@tonic-gate 	    DACF_INFO_HASHSIZE, mod_hash_null_valdtor,
2627c478bd9Sstevel@tonic-gate 	    sizeof (struct ddi_minor_data));
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	/*
2677c478bd9Sstevel@tonic-gate 	 * Register the '__kernel' module.
2687c478bd9Sstevel@tonic-gate 	 *
2697c478bd9Sstevel@tonic-gate 	 * These are operations that are provided by the kernel, not by a
2707c478bd9Sstevel@tonic-gate 	 * module.  We just feed the framework a dacfsw structure; it will get
2717c478bd9Sstevel@tonic-gate 	 * marked as 'loaded' by dacf_module_register(), and will always be
2727c478bd9Sstevel@tonic-gate 	 * available.
2737c478bd9Sstevel@tonic-gate 	 */
2747c478bd9Sstevel@tonic-gate 	(void) dacf_module_register(kmod_name, &kmod_dacfsw);
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	(void) read_dacf_binding_file(NULL);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	dprintf("dacf_init: dacf is ready\n");
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate /*
2827c478bd9Sstevel@tonic-gate  * dacf_clear_rules()
2837c478bd9Sstevel@tonic-gate  * 	clear the dacf rule database.  This is typically done in advance of
2847c478bd9Sstevel@tonic-gate  * 	rereading the dacf binding file.
2857c478bd9Sstevel@tonic-gate  */
2867c478bd9Sstevel@tonic-gate void
dacf_clear_rules()2877c478bd9Sstevel@tonic-gate dacf_clear_rules()
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate 	int i, j;
2907c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dacf_lock));
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	for (i = 0; i < DACF_NUM_OPIDS; i++) {
2937c478bd9Sstevel@tonic-gate 		for (j = 0; j < DACF_NUM_DEVSPECS; j++) {
2947c478bd9Sstevel@tonic-gate 			if ((dacf_rule_matrix[i][j] != NULL) &&
2957c478bd9Sstevel@tonic-gate 			    (*(dacf_rule_matrix[i][j]) != NULL)) {
2967c478bd9Sstevel@tonic-gate 				mod_hash_clear(*(dacf_rule_matrix[i][j]));
2977c478bd9Sstevel@tonic-gate 			}
2987c478bd9Sstevel@tonic-gate 		}
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate /*
3037c478bd9Sstevel@tonic-gate  * dacf_rule_insert()
3047c478bd9Sstevel@tonic-gate  *	Create an entry in the dacf rule database.
3057c478bd9Sstevel@tonic-gate  *	If 'module' is null, the kernel is the 'module'. (see dacf_rule_ctor()).
3067c478bd9Sstevel@tonic-gate  */
3077c478bd9Sstevel@tonic-gate int
dacf_rule_insert(dacf_devspec_t devspec_type,char * devspec_data,char * module,char * opset,dacf_opid_t opid,uint_t opts,dacf_arg_t * op_args)3087c478bd9Sstevel@tonic-gate dacf_rule_insert(dacf_devspec_t devspec_type, char *devspec_data,
3097c478bd9Sstevel@tonic-gate     char *module, char *opset, dacf_opid_t opid, uint_t opts,
3107c478bd9Sstevel@tonic-gate     dacf_arg_t *op_args)
3117c478bd9Sstevel@tonic-gate {
3127c478bd9Sstevel@tonic-gate 	dacf_rule_t *rule;
3137c478bd9Sstevel@tonic-gate 	mod_hash_t *hash;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	ASSERT(devspec_type != DACF_DS_ERROR);
3167c478bd9Sstevel@tonic-gate 	ASSERT(devspec_data);
3177c478bd9Sstevel@tonic-gate 	ASSERT(opset);
3187c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dacf_lock));
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	dprintf("dacf_rule_insert called: %s=\"%s\", %s:%s, %s\n",
3217c478bd9Sstevel@tonic-gate 	    dacf_devspec_to_str(devspec_type), devspec_data,
3227c478bd9Sstevel@tonic-gate 	    module ? module : "[kernel]", opset, dacf_opid_to_str(opid));
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	/*
3257c478bd9Sstevel@tonic-gate 	 * Fetch the hash table associated with this op-name and devspec-type.
3267c478bd9Sstevel@tonic-gate 	 * Some ops may not support all devspec-types, since they may be
3277c478bd9Sstevel@tonic-gate 	 * meaningless, so hash may be null.
3287c478bd9Sstevel@tonic-gate 	 */
3297c478bd9Sstevel@tonic-gate 	hash = dacf_get_op_hash(opid, devspec_type);
3307c478bd9Sstevel@tonic-gate 	if (hash == NULL) {
3317c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "!dacf dev-spec '%s' does not support op '%s'",
3327c478bd9Sstevel@tonic-gate 		    dacf_devspec_to_str(devspec_type), dacf_opid_to_str(opid));
3337c478bd9Sstevel@tonic-gate 		return (-1);
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	/*
3377c478bd9Sstevel@tonic-gate 	 * Allocate a rule  and fill it in, take a hold on it.
3387c478bd9Sstevel@tonic-gate 	 */
3397c478bd9Sstevel@tonic-gate 	rule = dacf_rule_ctor(devspec_data, module, opset, opid, opts,
3407c478bd9Sstevel@tonic-gate 	    op_args);
3417c478bd9Sstevel@tonic-gate 	dacf_rule_hold(rule);
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	if (mod_hash_insert(hash, (mod_hash_key_t)rule->r_devspec_data,
3447c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)rule) != 0) {
3457c478bd9Sstevel@tonic-gate 		/*
3467c478bd9Sstevel@tonic-gate 		 * We failed, so release hold.  This will cause the rule and
3477c478bd9Sstevel@tonic-gate 		 * associated data to get nuked.
3487c478bd9Sstevel@tonic-gate 		 */
3497c478bd9Sstevel@tonic-gate 		dacf_rule_rele(rule);
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "!dacf rule %s='%s' %s:%s %s duplicates "
3527c478bd9Sstevel@tonic-gate 		    "another rule, ignored", dacf_devspec_to_str(devspec_type),
3537c478bd9Sstevel@tonic-gate 		    devspec_data, module, opset, dacf_opid_to_str(opid));
3547c478bd9Sstevel@tonic-gate 		return (-1);
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 	return (0);
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate /*
3607c478bd9Sstevel@tonic-gate  * dacf_rule_ctor()
3617c478bd9Sstevel@tonic-gate  * 	Allocate and fill out entries in a dacf_rule_t.
3627c478bd9Sstevel@tonic-gate  */
3637c478bd9Sstevel@tonic-gate static dacf_rule_t *
dacf_rule_ctor(char * device_spec,char * module,char * opset,dacf_opid_t opid,uint_t opts,dacf_arg_t * op_args)3647c478bd9Sstevel@tonic-gate dacf_rule_ctor(char *device_spec, char *module, char *opset, dacf_opid_t opid,
3657c478bd9Sstevel@tonic-gate     uint_t opts, dacf_arg_t *op_args)
3667c478bd9Sstevel@tonic-gate {
3677c478bd9Sstevel@tonic-gate 	dacf_rule_t *rule;
3687c478bd9Sstevel@tonic-gate 	dacf_arg_t *p;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	rule = kmem_alloc(sizeof (dacf_rule_t), KM_SLEEP);
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	/*
3737c478bd9Sstevel@tonic-gate 	 * Fill in the entries
3747c478bd9Sstevel@tonic-gate 	 */
3757c478bd9Sstevel@tonic-gate 	rule->r_devspec_data = kmem_alloc(strlen(device_spec) + 1, KM_SLEEP);
3767c478bd9Sstevel@tonic-gate 	(void) strcpy(rule->r_devspec_data, device_spec);
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	/*
3797c478bd9Sstevel@tonic-gate 	 * If module is 'null' we set it to __kernel, meaning that this op
3807c478bd9Sstevel@tonic-gate 	 * is implemented by the kernel.
3817c478bd9Sstevel@tonic-gate 	 */
3827c478bd9Sstevel@tonic-gate 	if (module == NULL) {
3837c478bd9Sstevel@tonic-gate 		module = kmod_name;
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	rule->r_module = kmem_alloc(strlen(module) + 1, KM_SLEEP);
3877c478bd9Sstevel@tonic-gate 	(void) strcpy(rule->r_module, module);
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	rule->r_opset = kmem_alloc(strlen(opset) + 1, KM_SLEEP);
3907c478bd9Sstevel@tonic-gate 	(void) strcpy(rule->r_opset, opset);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	rule->r_refs = 0;	/* no refs yet */
3937c478bd9Sstevel@tonic-gate 	rule->r_opts = opts;
3947c478bd9Sstevel@tonic-gate 	rule->r_opid = opid;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	rule->r_args = NULL;
3977c478bd9Sstevel@tonic-gate 	p = op_args;
3987c478bd9Sstevel@tonic-gate 	while (p != NULL) {
3997c478bd9Sstevel@tonic-gate 		ASSERT(p->arg_name);
4007c478bd9Sstevel@tonic-gate 		ASSERT(p->arg_val);
4017c478bd9Sstevel@tonic-gate 		/*
4027c478bd9Sstevel@tonic-gate 		 * dacf_arg_insert() should always succeed, since we're copying
4037c478bd9Sstevel@tonic-gate 		 * another (already duplicate-free) list.
4047c478bd9Sstevel@tonic-gate 		 */
4057c478bd9Sstevel@tonic-gate 		(void) dacf_arg_insert(&rule->r_args, p->arg_name, p->arg_val);
4067c478bd9Sstevel@tonic-gate 		p = p->arg_next;
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	return (rule);
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate /*
4137c478bd9Sstevel@tonic-gate  * dacf_rule_val_dtor()
4147c478bd9Sstevel@tonic-gate  * 	This is the destructor for dacf_rule_t's in the rule database.  It
4157c478bd9Sstevel@tonic-gate  * 	simply does a dacf_rule_rele() on the rule.  This function will take
4167c478bd9Sstevel@tonic-gate  * 	care of destroying the rule if its ref count has dropped to 0.
4177c478bd9Sstevel@tonic-gate  */
4187c478bd9Sstevel@tonic-gate static void
dacf_rule_val_dtor(mod_hash_val_t val)4197c478bd9Sstevel@tonic-gate dacf_rule_val_dtor(mod_hash_val_t val)
4207c478bd9Sstevel@tonic-gate {
4217c478bd9Sstevel@tonic-gate 	ASSERT((void *)val != NULL);
4227c478bd9Sstevel@tonic-gate 	dacf_rule_rele((dacf_rule_t *)val);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate /*
4267c478bd9Sstevel@tonic-gate  * dacf_rule_destroy()
4277c478bd9Sstevel@tonic-gate  * 	destroy a dacf_rule_t
4287c478bd9Sstevel@tonic-gate  */
4297c478bd9Sstevel@tonic-gate void
dacf_rule_destroy(dacf_rule_t * rule)4307c478bd9Sstevel@tonic-gate dacf_rule_destroy(dacf_rule_t *rule)
4317c478bd9Sstevel@tonic-gate {
4327c478bd9Sstevel@tonic-gate 	ASSERT(rule->r_refs == 0);
4337c478bd9Sstevel@tonic-gate 	/*
4347c478bd9Sstevel@tonic-gate 	 * Free arguments.
4357c478bd9Sstevel@tonic-gate 	 */
4367c478bd9Sstevel@tonic-gate 	dacf_arglist_delete(&(rule->r_args));
4377c478bd9Sstevel@tonic-gate 	kmem_free(rule->r_devspec_data, strlen(rule->r_devspec_data) + 1);
4387c478bd9Sstevel@tonic-gate 	/*
4397c478bd9Sstevel@tonic-gate 	 * Module may be null for a kernel-managed op-set
4407c478bd9Sstevel@tonic-gate 	 */
4417c478bd9Sstevel@tonic-gate 	kmem_free(rule->r_module, strlen(rule->r_module) + 1);
4427c478bd9Sstevel@tonic-gate 	kmem_free(rule->r_opset, strlen(rule->r_opset) + 1);
4437c478bd9Sstevel@tonic-gate 	kmem_free(rule, sizeof (dacf_rule_t));
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate /*
4477c478bd9Sstevel@tonic-gate  * dacf_rule_hold()
4487c478bd9Sstevel@tonic-gate  * 	dacf rules are ref-counted.  This function increases the reference
4497c478bd9Sstevel@tonic-gate  * 	count on an rule.
4507c478bd9Sstevel@tonic-gate  */
4517c478bd9Sstevel@tonic-gate void
dacf_rule_hold(dacf_rule_t * rule)4527c478bd9Sstevel@tonic-gate dacf_rule_hold(dacf_rule_t *rule)
4537c478bd9Sstevel@tonic-gate {
4547c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dacf_lock));
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	rule->r_refs++;
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate /*
4607c478bd9Sstevel@tonic-gate  * dacf_rule_rele()
4617c478bd9Sstevel@tonic-gate  * 	drop the ref count on an rule, and destroy the rule if its
4627c478bd9Sstevel@tonic-gate  * 	ref count drops to 0.
4637c478bd9Sstevel@tonic-gate  */
4647c478bd9Sstevel@tonic-gate void
dacf_rule_rele(dacf_rule_t * rule)4657c478bd9Sstevel@tonic-gate dacf_rule_rele(dacf_rule_t *rule)
4667c478bd9Sstevel@tonic-gate {
4677c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dacf_lock));
4687c478bd9Sstevel@tonic-gate 	ASSERT(rule->r_refs > 0);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	rule->r_refs--;
4717c478bd9Sstevel@tonic-gate 	if (rule->r_refs == 0) {
4727c478bd9Sstevel@tonic-gate 		dacf_rule_destroy(rule);
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate /*
4777c478bd9Sstevel@tonic-gate  * dacf_rsrv_make()
4787c478bd9Sstevel@tonic-gate  * 	add an rule to a reservation list to be processed later.
4797c478bd9Sstevel@tonic-gate  */
4807c478bd9Sstevel@tonic-gate void
dacf_rsrv_make(dacf_rsrvlist_t * rsrv,dacf_rule_t * rule,void * info,dacf_rsrvlist_t ** list)4817c478bd9Sstevel@tonic-gate dacf_rsrv_make(dacf_rsrvlist_t *rsrv, dacf_rule_t *rule, void *info,
4827c478bd9Sstevel@tonic-gate     dacf_rsrvlist_t **list)
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate 	dacf_infohdl_t ihdl = info;
4857c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dacf_lock));
4867c478bd9Sstevel@tonic-gate 	ASSERT(info && rule && list);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	/*
4897c478bd9Sstevel@tonic-gate 	 * Bump the ref count on rule, so it won't get freed as long as it's on
4907c478bd9Sstevel@tonic-gate 	 * this reservation list.
4917c478bd9Sstevel@tonic-gate 	 */
4927c478bd9Sstevel@tonic-gate 	dacf_rule_hold(rule);
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	rsrv->rsrv_rule = rule;
4957c478bd9Sstevel@tonic-gate 	rsrv->rsrv_ihdl = ihdl;
4967c478bd9Sstevel@tonic-gate 	rsrv->rsrv_result = DDI_SUCCESS;
4977c478bd9Sstevel@tonic-gate 	rsrv->rsrv_next = *list;
4987c478bd9Sstevel@tonic-gate 	*list = rsrv;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	dprintf("dacf: reservation made\n");
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate /*
5047c478bd9Sstevel@tonic-gate  * dacf_clr_rsrvs()
5057c478bd9Sstevel@tonic-gate  * 	clear reservation list of operations of type 'op'
5067c478bd9Sstevel@tonic-gate  */
5077c478bd9Sstevel@tonic-gate void
dacf_clr_rsrvs(dev_info_t * devi,dacf_opid_t op)5087c478bd9Sstevel@tonic-gate dacf_clr_rsrvs(dev_info_t *devi, dacf_opid_t op)
5097c478bd9Sstevel@tonic-gate {
5107c478bd9Sstevel@tonic-gate 	dacf_process_rsrvs(&(DEVI(devi)->devi_dacf_tasks), op, DACF_PROC_RELE);
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate /*
5147c478bd9Sstevel@tonic-gate  * dacf_process_rsrvs()
5157c478bd9Sstevel@tonic-gate  * 	iterate across a locked reservation list, processing each element
5167c478bd9Sstevel@tonic-gate  * 	which matches 'op' according to 'flags'.
5177c478bd9Sstevel@tonic-gate  *
5187c478bd9Sstevel@tonic-gate  * 	if DACF_PROC_INVOKE is specified, the elements that match 'op'
5197c478bd9Sstevel@tonic-gate  * 	will have their operations invoked.  The return value from that
5207c478bd9Sstevel@tonic-gate  * 	operation is placed in the rsrv_result field of the dacf_rsrvlist_t
5217c478bd9Sstevel@tonic-gate  */
5227c478bd9Sstevel@tonic-gate void
dacf_process_rsrvs(dacf_rsrvlist_t ** list,dacf_opid_t op,int flags)5237c478bd9Sstevel@tonic-gate dacf_process_rsrvs(dacf_rsrvlist_t **list, dacf_opid_t op, int flags)
5247c478bd9Sstevel@tonic-gate {
5257c478bd9Sstevel@tonic-gate 	dacf_rsrvlist_t *p, *dp;
5267c478bd9Sstevel@tonic-gate 	dacf_rsrvlist_t **prevptr;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dacf_lock));
5297c478bd9Sstevel@tonic-gate 	ASSERT(list);
5307c478bd9Sstevel@tonic-gate 	ASSERT(flags != 0);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	if (*list == NULL)
5337c478bd9Sstevel@tonic-gate 		return;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	dprintf("dacf_process_rsrvs: opid = %d, flags = 0x%x\n", op, flags);
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	/*
5387c478bd9Sstevel@tonic-gate 	 * Walk the list, finding rules whose opid's match op, and performing
5397c478bd9Sstevel@tonic-gate 	 * the work described by 'flags'.
5407c478bd9Sstevel@tonic-gate 	 */
5417c478bd9Sstevel@tonic-gate 	prevptr = list;
5427c478bd9Sstevel@tonic-gate 	for (p = *list; p != NULL; ) {
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 		if (p->rsrv_rule->r_opid != op) {
5457c478bd9Sstevel@tonic-gate 			prevptr = &(p->rsrv_next);
5467c478bd9Sstevel@tonic-gate 			p = p->rsrv_next;
5477c478bd9Sstevel@tonic-gate 			continue;
5487c478bd9Sstevel@tonic-gate 		}
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 		if (flags & DACF_PROC_INVOKE) {
5517c478bd9Sstevel@tonic-gate 			p->rsrv_result = dacf_op_invoke(p->rsrv_rule,
5527c478bd9Sstevel@tonic-gate 			    p->rsrv_ihdl, 0);
5537c478bd9Sstevel@tonic-gate 		}
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 		if (flags & DACF_PROC_RELE) {
5567c478bd9Sstevel@tonic-gate 			*prevptr = p->rsrv_next;
5577c478bd9Sstevel@tonic-gate 			dp = p;
5587c478bd9Sstevel@tonic-gate 			p = p->rsrv_next;
5597c478bd9Sstevel@tonic-gate 			dacf_rule_rele(dp->rsrv_rule);
5607c478bd9Sstevel@tonic-gate 			kmem_free(dp, sizeof (dacf_rsrvlist_t));
5617c478bd9Sstevel@tonic-gate 		} else {
5627c478bd9Sstevel@tonic-gate 			prevptr = &(p->rsrv_next);
5637c478bd9Sstevel@tonic-gate 			p = p->rsrv_next;
5647c478bd9Sstevel@tonic-gate 		}
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate }
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate /*
5697c478bd9Sstevel@tonic-gate  * dacf_get_op_hash()
5707c478bd9Sstevel@tonic-gate  * 	Given an op name, (i.e. "post-attach" or "pre-detach") and a
5717c478bd9Sstevel@tonic-gate  * 	devspec-type, return the hash that represents that op indexed
5727c478bd9Sstevel@tonic-gate  * 	by that devspec.
5737c478bd9Sstevel@tonic-gate  */
5747c478bd9Sstevel@tonic-gate static mod_hash_t *
dacf_get_op_hash(dacf_opid_t op,dacf_devspec_t ds_type)5757c478bd9Sstevel@tonic-gate dacf_get_op_hash(dacf_opid_t op, dacf_devspec_t ds_type)
5767c478bd9Sstevel@tonic-gate {
5777c478bd9Sstevel@tonic-gate 	ASSERT(op <= DACF_NUM_OPIDS && op > 0);
5787c478bd9Sstevel@tonic-gate 	ASSERT(ds_type <= DACF_NUM_DEVSPECS && ds_type > 0);
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	/*
5817c478bd9Sstevel@tonic-gate 	 * dacf_rule_matrix is an array of pointers to pointers to hashes.
5827c478bd9Sstevel@tonic-gate 	 */
5837c478bd9Sstevel@tonic-gate 	if (dacf_rule_matrix[op - 1][ds_type - 1] == NULL) {
5847c478bd9Sstevel@tonic-gate 		return (NULL);
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 	return (*(dacf_rule_matrix[op - 1][ds_type - 1]));
5877c478bd9Sstevel@tonic-gate }
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate /*
5907c478bd9Sstevel@tonic-gate  * dacf_arg_insert()
5917c478bd9Sstevel@tonic-gate  * 	Create and insert an entry in an argument list.
5927c478bd9Sstevel@tonic-gate  * 	Returns -1 if the argument name is a duplicate of another already
5937c478bd9Sstevel@tonic-gate  * 	present in the hash.
5947c478bd9Sstevel@tonic-gate  */
5957c478bd9Sstevel@tonic-gate int
dacf_arg_insert(dacf_arg_t ** list,char * name,char * val)5967c478bd9Sstevel@tonic-gate dacf_arg_insert(dacf_arg_t **list, char *name, char *val)
5977c478bd9Sstevel@tonic-gate {
5987c478bd9Sstevel@tonic-gate 	dacf_arg_t *arg;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	/*
6017c478bd9Sstevel@tonic-gate 	 * Don't allow duplicates.
6027c478bd9Sstevel@tonic-gate 	 */
6037c478bd9Sstevel@tonic-gate 	for (arg = *list; arg != NULL; arg = arg->arg_next) {
6047c478bd9Sstevel@tonic-gate 		if (strcmp(arg->arg_name, name) == 0) {
6057c478bd9Sstevel@tonic-gate 			return (-1);
6067c478bd9Sstevel@tonic-gate 		}
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	arg = kmem_alloc(sizeof (dacf_arg_t), KM_SLEEP);
6107c478bd9Sstevel@tonic-gate 	arg->arg_name = kmem_alloc(strlen(name) + 1, KM_SLEEP);
6117c478bd9Sstevel@tonic-gate 	(void) strcpy(arg->arg_name, name);
6127c478bd9Sstevel@tonic-gate 	arg->arg_val = kmem_alloc(strlen(val) + 1, KM_SLEEP);
6137c478bd9Sstevel@tonic-gate 	(void) strcpy(arg->arg_val, val);
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	arg->arg_next = *list;
6167c478bd9Sstevel@tonic-gate 	*list = arg;
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 	return (0);
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate /*
6227c478bd9Sstevel@tonic-gate  * dacf_arglist_delete()
6237c478bd9Sstevel@tonic-gate  * 	free all the elements of a list of dacf_arg_t's.
6247c478bd9Sstevel@tonic-gate  */
6257c478bd9Sstevel@tonic-gate void
dacf_arglist_delete(dacf_arg_t ** list)6267c478bd9Sstevel@tonic-gate dacf_arglist_delete(dacf_arg_t **list)
6277c478bd9Sstevel@tonic-gate {
6287c478bd9Sstevel@tonic-gate 	dacf_arg_t *arg, *narg;
6297c478bd9Sstevel@tonic-gate 	arg = *list;
6307c478bd9Sstevel@tonic-gate 	while (arg != NULL) {
6317c478bd9Sstevel@tonic-gate 		narg = arg->arg_next;
6327c478bd9Sstevel@tonic-gate 		kmem_free(arg->arg_name, strlen(arg->arg_name) + 1);
6337c478bd9Sstevel@tonic-gate 		kmem_free(arg->arg_val, strlen(arg->arg_val) + 1);
6347c478bd9Sstevel@tonic-gate 		kmem_free(arg, sizeof (dacf_arg_t));
6357c478bd9Sstevel@tonic-gate 		arg = narg;
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 	*list = NULL;
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate /*
6417c478bd9Sstevel@tonic-gate  * dacf_match()
6427c478bd9Sstevel@tonic-gate  * 	Match a device-spec to a rule.
6437c478bd9Sstevel@tonic-gate  */
6447c478bd9Sstevel@tonic-gate dacf_rule_t *
dacf_match(dacf_opid_t op,dacf_devspec_t ds,const void * match_info)645*7bcaeddbSRobert Mustacchi dacf_match(dacf_opid_t op, dacf_devspec_t ds, const void *match_info)
6467c478bd9Sstevel@tonic-gate {
6477c478bd9Sstevel@tonic-gate 	dacf_rule_t *rule;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dacf_lock));
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	if (mod_hash_find(dacf_get_op_hash(op, ds), (mod_hash_key_t)match_info,
6527c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t *)&rule) == 0) {
6537c478bd9Sstevel@tonic-gate 		return (rule);
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	return (NULL);	/* Not Found */
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate /*
6607c478bd9Sstevel@tonic-gate  * dacf_module_register()
6617c478bd9Sstevel@tonic-gate  * 	register a module with the framework.  Use when a module gets loaded,
6627c478bd9Sstevel@tonic-gate  * 	or for the kernel to register a "virtual" module (i.e. a "module"
6637c478bd9Sstevel@tonic-gate  * 	which the kernel provides).  Makes a copy of the interface description
6647c478bd9Sstevel@tonic-gate  * 	provided by the module.
6657c478bd9Sstevel@tonic-gate  */
6667c478bd9Sstevel@tonic-gate int
dacf_module_register(char * mod_name,struct dacfsw * sw)6677c478bd9Sstevel@tonic-gate dacf_module_register(char *mod_name, struct dacfsw *sw)
6687c478bd9Sstevel@tonic-gate {
6697c478bd9Sstevel@tonic-gate 	char *str;
6707c478bd9Sstevel@tonic-gate 	size_t i, nelems;
6717c478bd9Sstevel@tonic-gate 	dacf_module_t *module;
6727c478bd9Sstevel@tonic-gate 	dacf_opset_t *opsarray;
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	if (sw == NULL) {
6757c478bd9Sstevel@tonic-gate 		return (EINVAL);
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	if (sw->dacf_rev != DACF_MODREV_1) {
6797c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "dacf: module '%s' exports unsupported "
6807c478bd9Sstevel@tonic-gate 		    "version %d interface, not registered\n", mod_name,
6817c478bd9Sstevel@tonic-gate 		    sw->dacf_rev);
6827c478bd9Sstevel@tonic-gate 		return (EINVAL);
6837c478bd9Sstevel@tonic-gate 	}
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	/*
6867c478bd9Sstevel@tonic-gate 	 * count how many opsets are provided.
6877c478bd9Sstevel@tonic-gate 	 */
6887c478bd9Sstevel@tonic-gate 	for (nelems = 0; sw->dacf_opsets[nelems].opset_name != NULL; nelems++)
6897c478bd9Sstevel@tonic-gate 		;
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	dprintf("dacf_module_register: found %lu opsets\n", nelems);
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	/*
6947c478bd9Sstevel@tonic-gate 	 * Temporary: It's ok for the kernel dacf_sw to have no opsets, since
6957c478bd9Sstevel@tonic-gate 	 * we don't have any opsets to export yet (in NON-DEBUG).
6967c478bd9Sstevel@tonic-gate 	 */
6977c478bd9Sstevel@tonic-gate 	if ((nelems == 0) && (sw != &kmod_dacfsw)) {
6987c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "dacf module %s exports no opsets, "
6997c478bd9Sstevel@tonic-gate 		    "not registered.\n", mod_name);
7007c478bd9Sstevel@tonic-gate 		return (EINVAL);
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	/*
7047c478bd9Sstevel@tonic-gate 	 * Look to see if the module has been previously registered with the
7057c478bd9Sstevel@tonic-gate 	 * framework.  If so, we can fail with EBUSY.
7067c478bd9Sstevel@tonic-gate 	 */
7077c478bd9Sstevel@tonic-gate 	if (mod_hash_find(dacf_module_hash, (mod_hash_key_t)mod_name,
7087c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)&module) == 0) {
7097c478bd9Sstevel@tonic-gate 		/*
7107c478bd9Sstevel@tonic-gate 		 * See if it is loaded currently
7117c478bd9Sstevel@tonic-gate 		 */
7127c478bd9Sstevel@tonic-gate 		rw_enter(&module->dm_lock, RW_WRITER);
7137c478bd9Sstevel@tonic-gate 		if (module->dm_loaded) {
7147c478bd9Sstevel@tonic-gate 			rw_exit(&module->dm_lock);
7157c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "dacf module '%s' is "
7167c478bd9Sstevel@tonic-gate 			    "already registered.", mod_name);
7177c478bd9Sstevel@tonic-gate 			return (EBUSY);
7187c478bd9Sstevel@tonic-gate 		}
7197c478bd9Sstevel@tonic-gate 	} else {
7207c478bd9Sstevel@tonic-gate 		/*
7217c478bd9Sstevel@tonic-gate 		 * This is the first time we've ever seen the module; stick
7227c478bd9Sstevel@tonic-gate 		 * it into the module hash.  If that fails, we've had a
7237c478bd9Sstevel@tonic-gate 		 * race between two threads, both trying to insert the same
7247c478bd9Sstevel@tonic-gate 		 * new module.  It's safe to stick the module into the
7257c478bd9Sstevel@tonic-gate 		 * hash only partly filled in, since dm_lock protects the
7267c478bd9Sstevel@tonic-gate 		 * structure, and we've got that write-locked.
7277c478bd9Sstevel@tonic-gate 		 */
7287c478bd9Sstevel@tonic-gate 		module = kmem_zalloc(sizeof (dacf_module_t), KM_SLEEP);
7297c478bd9Sstevel@tonic-gate 		str = kmem_alloc(strlen(mod_name) + 1, KM_SLEEP);
7307c478bd9Sstevel@tonic-gate 		(void) strcpy(str, mod_name);
7317c478bd9Sstevel@tonic-gate 		rw_enter(&module->dm_lock, RW_WRITER);
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 		if (mod_hash_insert(dacf_module_hash, (mod_hash_key_t)str,
7347c478bd9Sstevel@tonic-gate 		    (mod_hash_val_t)module) != 0) {
7357c478bd9Sstevel@tonic-gate 			rw_exit(&module->dm_lock);
7367c478bd9Sstevel@tonic-gate 			kmem_free(str, strlen(str) + 1);
7377c478bd9Sstevel@tonic-gate 			kmem_free(module, sizeof (dacf_module_t));
7387c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "dacf module '%s' is "
7397c478bd9Sstevel@tonic-gate 			    "already registered.", mod_name);
7407c478bd9Sstevel@tonic-gate 			return (EBUSY);
7417c478bd9Sstevel@tonic-gate 		}
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 	/*
7447c478bd9Sstevel@tonic-gate 	 * In either case (first time we've seen it or not), the module is
7457c478bd9Sstevel@tonic-gate 	 * not loaded, and we hold it write-locked.
7467c478bd9Sstevel@tonic-gate 	 */
7477c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&module->dm_lock));
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	/*
7507c478bd9Sstevel@tonic-gate 	 * Alloc array of opsets for this module.  Add one for the final
7517c478bd9Sstevel@tonic-gate 	 * NULL entry
7527c478bd9Sstevel@tonic-gate 	 */
7537c478bd9Sstevel@tonic-gate 	opsarray = kmem_zalloc(sizeof (dacf_opset_t) * (nelems + 1), KM_SLEEP);
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	for (i = 0; i < nelems; i++) {
7567c478bd9Sstevel@tonic-gate 		dacf_opset_copy(&(opsarray[i]), &(sw->dacf_opsets[i]));
7577c478bd9Sstevel@tonic-gate 		ASSERT(opsarray[i].opset_name != NULL);
7587c478bd9Sstevel@tonic-gate 		ASSERT(opsarray[i].opset_ops != NULL);
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 	opsarray[nelems].opset_name = NULL;
7617c478bd9Sstevel@tonic-gate 	opsarray[nelems].opset_ops = NULL;
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	ASSERT(module->dm_opsets == NULL);	/* see dacf_destroy_opsets() */
7647c478bd9Sstevel@tonic-gate 	module->dm_opsets = opsarray;
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	if (dacfdebug & DACF_DBG_MSGS) {
7677c478bd9Sstevel@tonic-gate 		dprintf("%s registered.\n", mod_name);
7687c478bd9Sstevel@tonic-gate 		for (i = 0; i < nelems; i++) {
7697c478bd9Sstevel@tonic-gate 			dprintf("registered %s\n", opsarray[i].opset_name);
7707c478bd9Sstevel@tonic-gate 		}
7717c478bd9Sstevel@tonic-gate 	}
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	module->dm_loaded = 1;
7747c478bd9Sstevel@tonic-gate 	rw_exit(&module->dm_lock);
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	return (0);
7777c478bd9Sstevel@tonic-gate }
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate /*
7807c478bd9Sstevel@tonic-gate  * dacf_module_unregister()
7817c478bd9Sstevel@tonic-gate  * 	remove a module from the framework, and free framework-allocated
7827c478bd9Sstevel@tonic-gate  * 	resources.
7837c478bd9Sstevel@tonic-gate  */
7847c478bd9Sstevel@tonic-gate int
dacf_module_unregister(char * mod_name)7857c478bd9Sstevel@tonic-gate dacf_module_unregister(char *mod_name)
7867c478bd9Sstevel@tonic-gate {
7877c478bd9Sstevel@tonic-gate 	dacf_module_t *module;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	/*
7907c478bd9Sstevel@tonic-gate 	 * Can't unregister __kernel, since there is no real way to get it
7917c478bd9Sstevel@tonic-gate 	 * back-- Once it gets marked with dm_loaded == 0, the kernel will
7927c478bd9Sstevel@tonic-gate 	 * try to modload() if it is ever needed, which will fail utterly,
7937c478bd9Sstevel@tonic-gate 	 * and send op_invoke into a loop in it's modload logic
7947c478bd9Sstevel@tonic-gate 	 *
7957c478bd9Sstevel@tonic-gate 	 * If this is behavior is ever needed in the future, we can just
7967c478bd9Sstevel@tonic-gate 	 * add a flag indicating that this module is really a fake.
7977c478bd9Sstevel@tonic-gate 	 */
7987c478bd9Sstevel@tonic-gate 	ASSERT(strcmp(mod_name, kmod_name) != 0);
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	dprintf("dacf_module_unregister: called for '%s'!\n", mod_name);
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	/*
8037c478bd9Sstevel@tonic-gate 	 * If NOAUL_DACF is set, or we try to get a write-lock on dm_lock and
8047c478bd9Sstevel@tonic-gate 	 * that fails, return EBUSY, and fail to unregister.
8057c478bd9Sstevel@tonic-gate 	 */
8067c478bd9Sstevel@tonic-gate 	if (mod_hash_find(dacf_module_hash, (mod_hash_key_t)mod_name,
8077c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)&module) == 0) {
8087c478bd9Sstevel@tonic-gate 		if ((moddebug & MODDEBUG_NOAUL_DACF) ||
8097c478bd9Sstevel@tonic-gate 		    !rw_tryenter(&module->dm_lock, RW_WRITER)) {
8107c478bd9Sstevel@tonic-gate 			return (EBUSY);
8117c478bd9Sstevel@tonic-gate 		}
8127c478bd9Sstevel@tonic-gate 	} else {
8137c478bd9Sstevel@tonic-gate 		return (EINVAL);
8147c478bd9Sstevel@tonic-gate 	}
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&module->dm_lock));
8177c478bd9Sstevel@tonic-gate 	dacf_destroy_opsets(module);
8187c478bd9Sstevel@tonic-gate 	module->dm_loaded = 0;
8197c478bd9Sstevel@tonic-gate 	rw_exit(&module->dm_lock);
8207c478bd9Sstevel@tonic-gate 	return (0);
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate /*
8247c478bd9Sstevel@tonic-gate  * dacf_destroy_opsets()
8257c478bd9Sstevel@tonic-gate  * 	given a module, destroy all of it's associated op-sets.
8267c478bd9Sstevel@tonic-gate  */
8277c478bd9Sstevel@tonic-gate static void
dacf_destroy_opsets(dacf_module_t * module)8287c478bd9Sstevel@tonic-gate dacf_destroy_opsets(dacf_module_t *module)
8297c478bd9Sstevel@tonic-gate {
8307c478bd9Sstevel@tonic-gate 	dacf_opset_t *array = module->dm_opsets;
8317c478bd9Sstevel@tonic-gate 	dacf_opset_t *p;
8327c478bd9Sstevel@tonic-gate 	int i;
8337c478bd9Sstevel@tonic-gate 	size_t nelems;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&module->dm_lock));
8367c478bd9Sstevel@tonic-gate 	ASSERT(module->dm_loaded == 1);
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	for (i = 0; array[i].opset_name != NULL; i++) {
8397c478bd9Sstevel@tonic-gate 		p = &(array[i]);
8407c478bd9Sstevel@tonic-gate 		kmem_free(p->opset_name, strlen(p->opset_name) + 1);
8417c478bd9Sstevel@tonic-gate 		/*
8427c478bd9Sstevel@tonic-gate 		 * count nelems in opset_ops
8437c478bd9Sstevel@tonic-gate 		 */
8447c478bd9Sstevel@tonic-gate 		for (nelems = 0; ; nelems++) {
8457c478bd9Sstevel@tonic-gate 			if (p->opset_ops[nelems].op_id == DACF_OPID_END) {
8467c478bd9Sstevel@tonic-gate 				break;
8477c478bd9Sstevel@tonic-gate 			}
8487c478bd9Sstevel@tonic-gate 		}
8497c478bd9Sstevel@tonic-gate 		/*
8507c478bd9Sstevel@tonic-gate 		 * Free the array of op ptrs.
8517c478bd9Sstevel@tonic-gate 		 */
8527c478bd9Sstevel@tonic-gate 		kmem_free(p->opset_ops, sizeof (dacf_op_t) * (nelems + 1));
8537c478bd9Sstevel@tonic-gate 	}
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	/*
8567c478bd9Sstevel@tonic-gate 	 * i has counted how big array is; +1 to account for the last element.
8577c478bd9Sstevel@tonic-gate 	 */
8587c478bd9Sstevel@tonic-gate 	kmem_free(array, (sizeof (dacf_opset_t)) * (i + 1));
8597c478bd9Sstevel@tonic-gate 	module->dm_opsets = NULL;
8607c478bd9Sstevel@tonic-gate }
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate /*
8637c478bd9Sstevel@tonic-gate  * dacf_opset_copy()
8647c478bd9Sstevel@tonic-gate  * 	makes a copy of a dacf_opset_t.
8657c478bd9Sstevel@tonic-gate  */
8667c478bd9Sstevel@tonic-gate static void
dacf_opset_copy(dacf_opset_t * dst,dacf_opset_t * src)8677c478bd9Sstevel@tonic-gate dacf_opset_copy(dacf_opset_t *dst, dacf_opset_t *src)
8687c478bd9Sstevel@tonic-gate {
8697c478bd9Sstevel@tonic-gate 	size_t nelems, i;
8707c478bd9Sstevel@tonic-gate 	ASSERT(src && dst);
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	dprintf("dacf_opset_copy: called\n");
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	dst->opset_name = kmem_alloc(strlen(src->opset_name) + 1, KM_SLEEP);
8757c478bd9Sstevel@tonic-gate 	(void) strcpy(dst->opset_name, src->opset_name);
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	dprintf("dacf_opset_copy: counting ops\n");
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	for (nelems = 0; ; nelems++) {
8807c478bd9Sstevel@tonic-gate 		if ((src->opset_ops[nelems].op_id == DACF_OPID_END) ||
8817c478bd9Sstevel@tonic-gate 		    (src->opset_ops[nelems].op_func == NULL)) {
8827c478bd9Sstevel@tonic-gate 			break;
8837c478bd9Sstevel@tonic-gate 		}
8847c478bd9Sstevel@tonic-gate 	}
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	dprintf("dacf_opset_copy: found %lu ops\n", nelems);
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	dst->opset_ops = kmem_alloc(sizeof (dacf_op_t) * (nelems + 1),
8897c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	dprintf("dacf_opset_copy: copying ops\n");
8927c478bd9Sstevel@tonic-gate 	for (i = 0; i < nelems; i++) {
8937c478bd9Sstevel@tonic-gate 		dst->opset_ops[i].op_id = src->opset_ops[i].op_id;
8947c478bd9Sstevel@tonic-gate 		dst->opset_ops[i].op_func = src->opset_ops[i].op_func;
8957c478bd9Sstevel@tonic-gate 	}
8967c478bd9Sstevel@tonic-gate 	dst->opset_ops[nelems].op_id = DACF_OPID_END;
8977c478bd9Sstevel@tonic-gate 	dst->opset_ops[nelems].op_func = NULL;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	dprintf("dacf_opset_copy: done copying ops\n");
9007c478bd9Sstevel@tonic-gate }
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate int dacf_modload_laps = 0;	/* just a diagnostic aid */
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate /*
9057c478bd9Sstevel@tonic-gate  * dacf_op_invoke()
9067c478bd9Sstevel@tonic-gate  *	Invoke a op in a opset in a module given the rule to invoke.
9077c478bd9Sstevel@tonic-gate  *
9087c478bd9Sstevel@tonic-gate  *	If the return value of dacf_op_invoke is 0, then rval contains the
9097c478bd9Sstevel@tonic-gate  *	return value of the _op_ being invoked. Otherwise, dacf_op_invoke's
9107c478bd9Sstevel@tonic-gate  *	return value indicates why the op invocation failed.
9117c478bd9Sstevel@tonic-gate  */
9127c478bd9Sstevel@tonic-gate int
dacf_op_invoke(dacf_rule_t * rule,dacf_infohdl_t info_hdl,int flags)9137c478bd9Sstevel@tonic-gate dacf_op_invoke(dacf_rule_t *rule, dacf_infohdl_t info_hdl, int flags)
9147c478bd9Sstevel@tonic-gate {
9157c478bd9Sstevel@tonic-gate 	dacf_module_t *module;
9167c478bd9Sstevel@tonic-gate 	dacf_opset_t *opsarray;
9177c478bd9Sstevel@tonic-gate 	dacf_opset_t *opset;
9187c478bd9Sstevel@tonic-gate 	dacf_op_t *op = NULL;
9197c478bd9Sstevel@tonic-gate 	dacf_opid_t op_id;
9207c478bd9Sstevel@tonic-gate 	dacf_arghdl_t arg_hdl;
9217c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
9227c478bd9Sstevel@tonic-gate 	int i, rval = -1;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	ASSERT(rule);
9257c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&dacf_lock));
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	op_id = rule->r_opid;
9287c478bd9Sstevel@tonic-gate 	dprintf("dacf_op_invoke: opid=%d\n", op_id);
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	/*
9317c478bd9Sstevel@tonic-gate 	 * Take laps, trying to load the dacf module.  For the case of kernel-
9327c478bd9Sstevel@tonic-gate 	 * provided operations, __kernel will be found in the hash table, and
9337c478bd9Sstevel@tonic-gate 	 * no modload will be needed.
9347c478bd9Sstevel@tonic-gate 	 */
9357c478bd9Sstevel@tonic-gate 	for (;;) {
9367c478bd9Sstevel@tonic-gate 		if (mod_hash_find(dacf_module_hash,
9377c478bd9Sstevel@tonic-gate 		    (mod_hash_key_t)rule->r_module,
9387c478bd9Sstevel@tonic-gate 		    (mod_hash_val_t *)&module) == 0) {
9397c478bd9Sstevel@tonic-gate 			rw_enter(&module->dm_lock, RW_READER);
9407c478bd9Sstevel@tonic-gate 			/*
9417c478bd9Sstevel@tonic-gate 			 * Found the module, and it is loaded.
9427c478bd9Sstevel@tonic-gate 			 */
9437c478bd9Sstevel@tonic-gate 			if (module->dm_loaded != 0) {
9447c478bd9Sstevel@tonic-gate 				break;
9457c478bd9Sstevel@tonic-gate 			}
9467c478bd9Sstevel@tonic-gate 			rw_exit(&module->dm_lock);
9477c478bd9Sstevel@tonic-gate 		}
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 		/*
9507c478bd9Sstevel@tonic-gate 		 * If we're here, either: 1) it's not in the hash, or 2) it is,
9517c478bd9Sstevel@tonic-gate 		 * but dm_loaded is 0, meaning the module needs to be loaded.
9527c478bd9Sstevel@tonic-gate 		 */
9537c478bd9Sstevel@tonic-gate 		dprintf("dacf_op_invoke: calling modload\n");
9547c478bd9Sstevel@tonic-gate 		if (modload("dacf", rule->r_module) < 0) {
9557c478bd9Sstevel@tonic-gate 			return (DACF_ERR_MOD_NOTFOUND);
9567c478bd9Sstevel@tonic-gate 		}
9577c478bd9Sstevel@tonic-gate 		dacf_modload_laps++;
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&module->dm_lock));
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	opsarray = module->dm_opsets;
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate 	/*
9657c478bd9Sstevel@tonic-gate 	 * Loop through the opsets exported by this module, and find the one
9667c478bd9Sstevel@tonic-gate 	 * we care about.
9677c478bd9Sstevel@tonic-gate 	 */
9687c478bd9Sstevel@tonic-gate 	opset = NULL;
9697c478bd9Sstevel@tonic-gate 	for (i = 0; opsarray[i].opset_name != NULL; i++) {
9707c478bd9Sstevel@tonic-gate 		if (strcmp(opsarray[i].opset_name, rule->r_opset) == 0) {
9717c478bd9Sstevel@tonic-gate 			opset = &opsarray[i];
9727c478bd9Sstevel@tonic-gate 			break;
9737c478bd9Sstevel@tonic-gate 		}
9747c478bd9Sstevel@tonic-gate 	}
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	if (opset == NULL) {
9777c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "!dacf: couldn't invoke op, opset '%s' not "
9787c478bd9Sstevel@tonic-gate 		    "found in module '%s'", rule->r_opset, rule->r_module);
9797c478bd9Sstevel@tonic-gate 		rw_exit(&module->dm_lock);
9807c478bd9Sstevel@tonic-gate 		return (DACF_ERR_OPSET_NOTFOUND);
9817c478bd9Sstevel@tonic-gate 	}
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	arg_hdl = (dacf_arghdl_t)rule->r_args;
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 	/*
9867c478bd9Sstevel@tonic-gate 	 * Call the appropriate routine in the target by looping across the
9877c478bd9Sstevel@tonic-gate 	 * ops until we find the one whose id matches opid.
9887c478bd9Sstevel@tonic-gate 	 */
9897c478bd9Sstevel@tonic-gate 	op = NULL;
9907c478bd9Sstevel@tonic-gate 	for (i = 0; opset->opset_ops[i].op_id != DACF_OPID_END; i++) {
9917c478bd9Sstevel@tonic-gate 		if (opset->opset_ops[i].op_id == op_id) {
9927c478bd9Sstevel@tonic-gate 			op = &(opset->opset_ops[i]);
9937c478bd9Sstevel@tonic-gate 			break;
9947c478bd9Sstevel@tonic-gate 		}
9957c478bd9Sstevel@tonic-gate 	}
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	if (op == NULL) {
9987c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "!dacf: couldn't invoke op, op '%s' not found "
9997c478bd9Sstevel@tonic-gate 		    "in opset '%s' in module '%s'", dacf_opid_to_str(op_id),
10007c478bd9Sstevel@tonic-gate 		    rule->r_opset, rule->r_module);
10017c478bd9Sstevel@tonic-gate 		rw_exit(&module->dm_lock);
10027c478bd9Sstevel@tonic-gate 		return (DACF_ERR_OP_NOTFOUND);
10037c478bd9Sstevel@tonic-gate 	}
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	dprintf("dacf_op_invoke: found op, invoking...\n");
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 	/*
10087c478bd9Sstevel@tonic-gate 	 * Drop dacf_lock here, so that op_func's that cause drivers to
10097c478bd9Sstevel@tonic-gate 	 * get loaded don't wedge the system when they try to acquire dacf_lock
10107c478bd9Sstevel@tonic-gate 	 * to do matching.
10117c478bd9Sstevel@tonic-gate 	 *
10127c478bd9Sstevel@tonic-gate 	 * Mark that an invoke is happening to prevent recursive invokes
10137c478bd9Sstevel@tonic-gate 	 */
10147c478bd9Sstevel@tonic-gate 	dip = ((struct ddi_minor_data *)info_hdl)->dip;
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
10177c478bd9Sstevel@tonic-gate 	DEVI_SET_INVOKING_DACF(dip);
10187c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	mutex_exit(&dacf_lock);
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	rval = op->op_func(info_hdl, arg_hdl, flags);
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	mutex_enter(&dacf_lock);
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	/*
10277c478bd9Sstevel@tonic-gate 	 * Completed the invocation against module, so let go of it.
10287c478bd9Sstevel@tonic-gate 	 */
10297c478bd9Sstevel@tonic-gate 	mutex_enter(&(DEVI(dip)->devi_lock));
10307c478bd9Sstevel@tonic-gate 	DEVI_CLR_INVOKING_DACF(dip);
10317c478bd9Sstevel@tonic-gate 	mutex_exit(&(DEVI(dip)->devi_lock));
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	/*
10347c478bd9Sstevel@tonic-gate 	 * Drop our r-lock on the module, now that we no longer need the module
10357c478bd9Sstevel@tonic-gate 	 * to stay loaded.
10367c478bd9Sstevel@tonic-gate 	 */
10377c478bd9Sstevel@tonic-gate 	rw_exit(&module->dm_lock);
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	if (rval == DACF_SUCCESS) {
10407c478bd9Sstevel@tonic-gate 		return (DACF_SUCCESS);
10417c478bd9Sstevel@tonic-gate 	} else {
10427c478bd9Sstevel@tonic-gate 		return (DACF_ERR_OP_FAILED);
10437c478bd9Sstevel@tonic-gate 	}
10447c478bd9Sstevel@tonic-gate }
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate /*
10477c478bd9Sstevel@tonic-gate  * dacf_get_devspec()
10487c478bd9Sstevel@tonic-gate  * 	given a devspec-type as a string, return a corresponding dacf_devspec_t
10497c478bd9Sstevel@tonic-gate  */
10507c478bd9Sstevel@tonic-gate dacf_devspec_t
dacf_get_devspec(char * name)10517c478bd9Sstevel@tonic-gate dacf_get_devspec(char *name)
10527c478bd9Sstevel@tonic-gate {
10537c478bd9Sstevel@tonic-gate 	dacf_ds_t *p = &dacf_devspecs[0];
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	while (p->name != NULL) {
10567c478bd9Sstevel@tonic-gate 		if (strcmp(p->name, name) == 0) {
10577c478bd9Sstevel@tonic-gate 			return (p->id);
10587c478bd9Sstevel@tonic-gate 		}
10597c478bd9Sstevel@tonic-gate 		p++;
10607c478bd9Sstevel@tonic-gate 	}
10617c478bd9Sstevel@tonic-gate 	return (DACF_DS_ERROR);
10627c478bd9Sstevel@tonic-gate }
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate /*
10657c478bd9Sstevel@tonic-gate  * dacf_devspec_to_str()
10667c478bd9Sstevel@tonic-gate  * 	given a dacf_devspec_t, return a pointer to the human readable string
10677c478bd9Sstevel@tonic-gate  * 	representation of that device specifier.
10687c478bd9Sstevel@tonic-gate  */
10697c478bd9Sstevel@tonic-gate const char *
dacf_devspec_to_str(dacf_devspec_t ds)10707c478bd9Sstevel@tonic-gate dacf_devspec_to_str(dacf_devspec_t ds)
10717c478bd9Sstevel@tonic-gate {
10727c478bd9Sstevel@tonic-gate 	dacf_ds_t *p = &dacf_devspecs[0];
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	while (p->name != NULL) {
10757c478bd9Sstevel@tonic-gate 		if (p->id == ds) {
10767c478bd9Sstevel@tonic-gate 			return (p->name);
10777c478bd9Sstevel@tonic-gate 		}
10787c478bd9Sstevel@tonic-gate 		p++;
10797c478bd9Sstevel@tonic-gate 	}
10807c478bd9Sstevel@tonic-gate 	return (NULL);
10817c478bd9Sstevel@tonic-gate }
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate /*
10847c478bd9Sstevel@tonic-gate  * dacf_get_op()
10857c478bd9Sstevel@tonic-gate  * 	given a op name, returns the corresponding dacf_opid_t.
10867c478bd9Sstevel@tonic-gate  */
10877c478bd9Sstevel@tonic-gate dacf_opid_t
dacf_get_op(char * name)10887c478bd9Sstevel@tonic-gate dacf_get_op(char *name)
10897c478bd9Sstevel@tonic-gate {
10907c478bd9Sstevel@tonic-gate 	dacf_opmap_t *p = &dacf_ops[0];
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	while (p->name != NULL) {
10937c478bd9Sstevel@tonic-gate 		if (strcmp(p->name, name) == 0) {
10947c478bd9Sstevel@tonic-gate 			return (p->id);
10957c478bd9Sstevel@tonic-gate 		}
10967c478bd9Sstevel@tonic-gate 		p++;
10977c478bd9Sstevel@tonic-gate 	}
10987c478bd9Sstevel@tonic-gate 	return (DACF_OPID_ERROR);
10997c478bd9Sstevel@tonic-gate }
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate /*
11027c478bd9Sstevel@tonic-gate  * dacf_opid_to_str()
11037c478bd9Sstevel@tonic-gate  * 	given a dacf_opid_t, return the human-readable op-name.
11047c478bd9Sstevel@tonic-gate  */
11057c478bd9Sstevel@tonic-gate const char *
dacf_opid_to_str(dacf_opid_t tid)11067c478bd9Sstevel@tonic-gate dacf_opid_to_str(dacf_opid_t tid)
11077c478bd9Sstevel@tonic-gate {
11087c478bd9Sstevel@tonic-gate 	dacf_opmap_t *p = &dacf_ops[0];
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 	while (p->name != NULL) {
11117c478bd9Sstevel@tonic-gate 		if (p->id == tid) {
11127c478bd9Sstevel@tonic-gate 			return (p->name);
11137c478bd9Sstevel@tonic-gate 		}
11147c478bd9Sstevel@tonic-gate 		p++;
11157c478bd9Sstevel@tonic-gate 	}
11167c478bd9Sstevel@tonic-gate 	return (NULL);
11177c478bd9Sstevel@tonic-gate }
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate /*
11207c478bd9Sstevel@tonic-gate  * dacf_getopt()
11217c478bd9Sstevel@tonic-gate  * 	given an option specified as a string, add it to the bit-field of
11227c478bd9Sstevel@tonic-gate  * 	options given.  Returns -1 if the option is unrecognized.
11237c478bd9Sstevel@tonic-gate  */
11247c478bd9Sstevel@tonic-gate int
dacf_getopt(char * opt_str,uint_t * opts)11257c478bd9Sstevel@tonic-gate dacf_getopt(char *opt_str, uint_t *opts)
11267c478bd9Sstevel@tonic-gate {
11277c478bd9Sstevel@tonic-gate 	dacf_opt_t *p = &dacf_options[0];
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	/*
11307c478bd9Sstevel@tonic-gate 	 * Look through the list for the option given
11317c478bd9Sstevel@tonic-gate 	 */
11327c478bd9Sstevel@tonic-gate 	while (p->optname != NULL) {
11337c478bd9Sstevel@tonic-gate 		if (strcmp(opt_str, p->optname) == 0) {
11347c478bd9Sstevel@tonic-gate 			*opts |= p->optmask;
11357c478bd9Sstevel@tonic-gate 			return (0);
11367c478bd9Sstevel@tonic-gate 		}
11377c478bd9Sstevel@tonic-gate 		p++;
11387c478bd9Sstevel@tonic-gate 	}
11397c478bd9Sstevel@tonic-gate 	return (-1);
11407c478bd9Sstevel@tonic-gate }
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate /*
11457c478bd9Sstevel@tonic-gate  * This family of functions forms the dacf interface which is exported to
11467c478bd9Sstevel@tonic-gate  * kernel/dacf modules.  Modules _should_not_ use any dacf_* functions
11477c478bd9Sstevel@tonic-gate  * presented above this point.
11487c478bd9Sstevel@tonic-gate  *
11497c478bd9Sstevel@tonic-gate  * Note: These routines use a dacf_infohdl_t to struct ddi_minor_data * and
11507c478bd9Sstevel@tonic-gate  * assume that the resulting pointer is not to an alias node.  That is true
11517c478bd9Sstevel@tonic-gate  * because dacf_op_invoke guarantees it by first resolving the alias.
11527c478bd9Sstevel@tonic-gate  */
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate /*
11557c478bd9Sstevel@tonic-gate  * dacf_minor_name()
11567c478bd9Sstevel@tonic-gate  * 	given a dacf_infohdl_t, obtain the minor name of the device instance
11577c478bd9Sstevel@tonic-gate  * 	being configured.
11587c478bd9Sstevel@tonic-gate  */
11597c478bd9Sstevel@tonic-gate const char *
dacf_minor_name(dacf_infohdl_t info_hdl)11607c478bd9Sstevel@tonic-gate dacf_minor_name(dacf_infohdl_t info_hdl)
11617c478bd9Sstevel@tonic-gate {
11627c478bd9Sstevel@tonic-gate 	struct ddi_minor_data *dmdp = (struct ddi_minor_data *)info_hdl;
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	return (dmdp->ddm_name);
11657c478bd9Sstevel@tonic-gate }
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate /*
11687c478bd9Sstevel@tonic-gate  * dacf_minor_number()
11697c478bd9Sstevel@tonic-gate  * 	given a dacf_infohdl_t, obtain the device minor number of the instance
11707c478bd9Sstevel@tonic-gate  * 	being configured.
11717c478bd9Sstevel@tonic-gate  */
11727c478bd9Sstevel@tonic-gate minor_t
dacf_minor_number(dacf_infohdl_t info_hdl)11737c478bd9Sstevel@tonic-gate dacf_minor_number(dacf_infohdl_t info_hdl)
11747c478bd9Sstevel@tonic-gate {
11757c478bd9Sstevel@tonic-gate 	struct ddi_minor_data *dmdp = (struct ddi_minor_data *)info_hdl;
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	return (getminor(dmdp->ddm_dev));
11787c478bd9Sstevel@tonic-gate }
11797c478bd9Sstevel@tonic-gate 
1180d62bc4baSyz /*
1181d62bc4baSyz  * dacf_get_dev()
1182d62bc4baSyz  *	given a dacf_infohdl_t, obtain the dev_t of the instance being
1183d62bc4baSyz  *	configured.
1184d62bc4baSyz  */
1185d62bc4baSyz dev_t
dacf_get_dev(dacf_infohdl_t info_hdl)1186d62bc4baSyz dacf_get_dev(dacf_infohdl_t info_hdl)
1187d62bc4baSyz {
1188d62bc4baSyz 	struct ddi_minor_data *dmdp = (struct ddi_minor_data *)info_hdl;
1189d62bc4baSyz 
1190d62bc4baSyz 	return (dmdp->ddm_dev);
1191d62bc4baSyz }
1192d62bc4baSyz 
11937c478bd9Sstevel@tonic-gate /*
11947c478bd9Sstevel@tonic-gate  * dacf_driver_name()
11957c478bd9Sstevel@tonic-gate  * 	given a dacf_infohdl_t, obtain the device driver name of the device
11967c478bd9Sstevel@tonic-gate  * 	instance being configured.
11977c478bd9Sstevel@tonic-gate  */
11987c478bd9Sstevel@tonic-gate const char *
dacf_driver_name(dacf_infohdl_t info_hdl)11997c478bd9Sstevel@tonic-gate dacf_driver_name(dacf_infohdl_t info_hdl)
12007c478bd9Sstevel@tonic-gate {
12017c478bd9Sstevel@tonic-gate 	struct ddi_minor_data *dmdp = (struct ddi_minor_data *)info_hdl;
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	return (ddi_driver_name(dmdp->dip));
12047c478bd9Sstevel@tonic-gate }
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate /*
12077c478bd9Sstevel@tonic-gate  * dacf_devinfo_node()
12087c478bd9Sstevel@tonic-gate  * 	given a dacf_infohdl_t, obtain the dev_info_t of the device instance
12097c478bd9Sstevel@tonic-gate  * 	being configured.
12107c478bd9Sstevel@tonic-gate  */
12117c478bd9Sstevel@tonic-gate dev_info_t *
dacf_devinfo_node(dacf_infohdl_t info_hdl)12127c478bd9Sstevel@tonic-gate dacf_devinfo_node(dacf_infohdl_t info_hdl)
12137c478bd9Sstevel@tonic-gate {
12147c478bd9Sstevel@tonic-gate 	struct ddi_minor_data *dmdp = (struct ddi_minor_data *)info_hdl;
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 	return (dmdp->dip);
12177c478bd9Sstevel@tonic-gate }
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate /*
12207c478bd9Sstevel@tonic-gate  * dacf_get_arg()
12217c478bd9Sstevel@tonic-gate  * 	given the dacf_arghdl_t passed to a op and the name of an argument,
12227c478bd9Sstevel@tonic-gate  * 	return the value of that argument.
12237c478bd9Sstevel@tonic-gate  *
12247c478bd9Sstevel@tonic-gate  * 	returns NULL if the argument is not found.
12257c478bd9Sstevel@tonic-gate  */
12267c478bd9Sstevel@tonic-gate const char *
dacf_get_arg(dacf_arghdl_t arghdl,char * arg_name)12277c478bd9Sstevel@tonic-gate dacf_get_arg(dacf_arghdl_t arghdl, char *arg_name)
12287c478bd9Sstevel@tonic-gate {
12297c478bd9Sstevel@tonic-gate 	dacf_arg_t *arg_list = (dacf_arg_t *)arghdl;
12307c478bd9Sstevel@tonic-gate 	ASSERT(arg_name);
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	while (arg_list != NULL) {
12337c478bd9Sstevel@tonic-gate 		if (strcmp(arg_list->arg_name, arg_name) == 0) {
12347c478bd9Sstevel@tonic-gate 			return (arg_list->arg_val);
12357c478bd9Sstevel@tonic-gate 		}
12367c478bd9Sstevel@tonic-gate 		arg_list = arg_list->arg_next;
12377c478bd9Sstevel@tonic-gate 	}
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 	return (NULL);
12407c478bd9Sstevel@tonic-gate }
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate /*
12437c478bd9Sstevel@tonic-gate  * dacf_store_info()
12447c478bd9Sstevel@tonic-gate  * 	associate instance-specific data with a device instance.  Future
12457c478bd9Sstevel@tonic-gate  * 	configuration ops invoked for this instance can retrieve this data using
12467c478bd9Sstevel@tonic-gate  * 	dacf_retrieve_info() below.  Modules are responsible for cleaning up
12477c478bd9Sstevel@tonic-gate  * 	this data as appropriate, and should store NULL as the value of 'data'
12487c478bd9Sstevel@tonic-gate  * 	when the data is no longer valid.
12497c478bd9Sstevel@tonic-gate  */
12507c478bd9Sstevel@tonic-gate void
dacf_store_info(dacf_infohdl_t info_hdl,void * data)12517c478bd9Sstevel@tonic-gate dacf_store_info(dacf_infohdl_t info_hdl, void *data)
12527c478bd9Sstevel@tonic-gate {
12537c478bd9Sstevel@tonic-gate 	struct ddi_minor_data *dmdp = (struct ddi_minor_data *)info_hdl;
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 	/*
12567c478bd9Sstevel@tonic-gate 	 * If the client is 'storing NULL' we can represent that by blowing
12577c478bd9Sstevel@tonic-gate 	 * the info entry out of the hash.
12587c478bd9Sstevel@tonic-gate 	 */
12597c478bd9Sstevel@tonic-gate 	if (data == NULL) {
12607c478bd9Sstevel@tonic-gate 		(void) mod_hash_destroy(dacf_info_hash, (mod_hash_key_t)dmdp);
12617c478bd9Sstevel@tonic-gate 	} else {
12627c478bd9Sstevel@tonic-gate 		/*
12637c478bd9Sstevel@tonic-gate 		 * mod_hash_replace can only fail on out of memory, but we sleep
12647c478bd9Sstevel@tonic-gate 		 * for memory in this hash, so it is safe to ignore the retval.
12657c478bd9Sstevel@tonic-gate 		 */
12667c478bd9Sstevel@tonic-gate 		(void) mod_hash_replace(dacf_info_hash, (mod_hash_key_t)dmdp,
12677c478bd9Sstevel@tonic-gate 		    (mod_hash_val_t)data);
12687c478bd9Sstevel@tonic-gate 	}
12697c478bd9Sstevel@tonic-gate }
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate /*
12727c478bd9Sstevel@tonic-gate  * dacf_retrieve_info()
12737c478bd9Sstevel@tonic-gate  * 	retrieve instance-specific data associated with a device instance.
12747c478bd9Sstevel@tonic-gate  */
12757c478bd9Sstevel@tonic-gate void *
dacf_retrieve_info(dacf_infohdl_t info_hdl)12767c478bd9Sstevel@tonic-gate dacf_retrieve_info(dacf_infohdl_t info_hdl)
12777c478bd9Sstevel@tonic-gate {
12787c478bd9Sstevel@tonic-gate 	struct ddi_minor_data *dmdp = (struct ddi_minor_data *)info_hdl;
12797c478bd9Sstevel@tonic-gate 	void *data;
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	if (mod_hash_find(dacf_info_hash, (mod_hash_key_t)dmdp,
1282d62bc4baSyz 	    (mod_hash_val_t *)&data) != 0) {
12837c478bd9Sstevel@tonic-gate 		return (NULL);
12847c478bd9Sstevel@tonic-gate 	}
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	return (data);
12877c478bd9Sstevel@tonic-gate }
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate /*
12907c478bd9Sstevel@tonic-gate  * dacf_makevp()
12917c478bd9Sstevel@tonic-gate  * 	make a vnode for the specified dacf_infohdl_t.
12927c478bd9Sstevel@tonic-gate  */
12937c478bd9Sstevel@tonic-gate struct vnode *
dacf_makevp(dacf_infohdl_t info_hdl)12947c478bd9Sstevel@tonic-gate dacf_makevp(dacf_infohdl_t info_hdl)
12957c478bd9Sstevel@tonic-gate {
12967c478bd9Sstevel@tonic-gate 	struct ddi_minor_data *dmdp = (struct ddi_minor_data *)info_hdl;
12977c478bd9Sstevel@tonic-gate 	struct vnode	*vp;
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	vp = makespecvp(dmdp->ddm_dev, VCHR);
13007c478bd9Sstevel@tonic-gate 	spec_assoc_vp_with_devi(vp, dmdp->dip);
13017c478bd9Sstevel@tonic-gate 	return (vp);
13027c478bd9Sstevel@tonic-gate }
1303