xref: /illumos-gate/usr/src/uts/common/os/modsubr.c (revision 5f61829a)
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
552cac543Sramat  * Common Development and Distribution License (the "License").
652cac543Sramat  * 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 /*
22c8742f64SJerry Gilliam  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <sys/param.h>
267c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
277c478bd9Sstevel@tonic-gate #include <sys/modhash.h>
287c478bd9Sstevel@tonic-gate #include <sys/open.h>
297c478bd9Sstevel@tonic-gate #include <sys/conf.h>
307c478bd9Sstevel@tonic-gate #include <sys/errno.h>
317c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
327c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
337c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
347c478bd9Sstevel@tonic-gate #include <sys/stat.h>
357c478bd9Sstevel@tonic-gate #include <sys/mode.h>
367c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
377c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
387c478bd9Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
397c478bd9Sstevel@tonic-gate #include <sys/ddi_implfuncs.h>
407c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
417c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
427c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
437c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
447c478bd9Sstevel@tonic-gate #include <sys/hwconf.h>
457c478bd9Sstevel@tonic-gate #include <sys/file.h>
467c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
477c478bd9Sstevel@tonic-gate #include <sys/thread.h>
487c478bd9Sstevel@tonic-gate #include <sys/cred.h>
497c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
507c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
517c478bd9Sstevel@tonic-gate #include <sys/consdev.h>
527c478bd9Sstevel@tonic-gate #include <sys/systm.h>
537c478bd9Sstevel@tonic-gate #include <sys/debug.h>
547c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate extern struct dev_ops nodev_ops;
577c478bd9Sstevel@tonic-gate extern struct dev_ops mod_nodev_ops;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate struct mod_noload {
607c478bd9Sstevel@tonic-gate 	struct mod_noload *mn_next;
617c478bd9Sstevel@tonic-gate 	char *mn_name;
627c478bd9Sstevel@tonic-gate };
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * Function prototypes
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate static int init_stubs(struct modctl *, struct mod_modinfo *);
68*5f61829aSRobert Mustacchi static int nm_hash(const char *);
69*5f61829aSRobert Mustacchi static void make_syscallname(const char *, int);
707c478bd9Sstevel@tonic-gate static void hwc_hash_init();
717c478bd9Sstevel@tonic-gate static void hwc_hash(struct hwc_spec *, major_t);
727c478bd9Sstevel@tonic-gate static void hwc_unhash(struct hwc_spec *);
737c478bd9Sstevel@tonic-gate 
74c8742f64SJerry Gilliam int
major_valid(major_t major)75c8742f64SJerry Gilliam major_valid(major_t major)
76c8742f64SJerry Gilliam {
773df2e8b2SRobert Mustacchi 	return (major != DDI_MAJOR_T_NONE && major < devcnt);
78c8742f64SJerry Gilliam }
79c8742f64SJerry Gilliam 
80c9cc1492SJerry Gilliam int
driver_installed(major_t major)81c9cc1492SJerry Gilliam driver_installed(major_t major)
82c9cc1492SJerry Gilliam {
83c8742f64SJerry Gilliam 	return (major_valid(major) && devnamesp[major].dn_name != NULL);
84c8742f64SJerry Gilliam }
85c8742f64SJerry Gilliam 
86c8742f64SJerry Gilliam int
driver_active(major_t major)87c8742f64SJerry Gilliam driver_active(major_t major)
88c8742f64SJerry Gilliam {
89c8742f64SJerry Gilliam 	return (driver_installed(major) && !(devnamesp[major].dn_flags &
90c9cc1492SJerry Gilliam 	    (DN_DRIVER_REMOVED|DN_DRIVER_INACTIVE)));
91c9cc1492SJerry Gilliam }
92c9cc1492SJerry Gilliam 
937c478bd9Sstevel@tonic-gate struct dev_ops *
mod_hold_dev_by_major(major_t major)947c478bd9Sstevel@tonic-gate mod_hold_dev_by_major(major_t major)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate 	struct dev_ops **devopspp, *ops;
977c478bd9Sstevel@tonic-gate 	int loaded;
987c478bd9Sstevel@tonic-gate 	char *drvname;
997c478bd9Sstevel@tonic-gate 
100c8742f64SJerry Gilliam 	if (!driver_active(major))
1017c478bd9Sstevel@tonic-gate 		return (NULL);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&(devnamesp[major].dn_lock));
1047c478bd9Sstevel@tonic-gate 	devopspp = &devopsp[major];
1057c478bd9Sstevel@tonic-gate 	loaded = 1;
1067c478bd9Sstevel@tonic-gate 	while (loaded && !CB_DRV_INSTALLED(*devopspp)) {
1077c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&(devnamesp[major].dn_lock));
1087c478bd9Sstevel@tonic-gate 		drvname = mod_major_to_name(major);
1097c478bd9Sstevel@tonic-gate 		if (drvname == NULL)
1107c478bd9Sstevel@tonic-gate 			return (NULL);
1117c478bd9Sstevel@tonic-gate 		loaded = (modload("drv", drvname) != -1);
1127c478bd9Sstevel@tonic-gate 		LOCK_DEV_OPS(&(devnamesp[major].dn_lock));
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 	if (loaded) {
1157c478bd9Sstevel@tonic-gate 		INCR_DEV_OPS_REF(*devopspp);
1167c478bd9Sstevel@tonic-gate 		ops = *devopspp;
1177c478bd9Sstevel@tonic-gate 	} else {
1187c478bd9Sstevel@tonic-gate 		ops = NULL;
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(devnamesp[major].dn_lock));
1217c478bd9Sstevel@tonic-gate 	return (ops);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate #ifdef	DEBUG_RELE
1257c478bd9Sstevel@tonic-gate static int mod_rele_pause = DEBUG_RELE;
1267c478bd9Sstevel@tonic-gate #endif	/* DEBUG_RELE */
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate void
mod_rele_dev_by_major(major_t major)1297c478bd9Sstevel@tonic-gate mod_rele_dev_by_major(major_t major)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	struct dev_ops *ops;
1327c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
1337c478bd9Sstevel@tonic-gate 
134c8742f64SJerry Gilliam 	if (!driver_active(major))
1357c478bd9Sstevel@tonic-gate 		return;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
1387c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
1397c478bd9Sstevel@tonic-gate 	ops = devopsp[major];
1407c478bd9Sstevel@tonic-gate 	ASSERT(CB_DRV_INSTALLED(ops));
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate #ifdef	DEBUG_RELE
1437c478bd9Sstevel@tonic-gate 	if (!DEV_OPS_HELD(ops))  {
1447c478bd9Sstevel@tonic-gate 		char *s;
1457c478bd9Sstevel@tonic-gate 		static char *msg = "mod_rele_dev_by_major: unheld driver!";
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 		printf("mod_rele_dev_by_major: Major dev <%u>, name <%s>\n",
1487c478bd9Sstevel@tonic-gate 		    (uint_t)major,
1497c478bd9Sstevel@tonic-gate 		    (s = mod_major_to_name(major)) ? s : "unknown");
1507c478bd9Sstevel@tonic-gate 		if (mod_rele_pause)
1517c478bd9Sstevel@tonic-gate 			debug_enter(msg);
1527c478bd9Sstevel@tonic-gate 		else
1537c478bd9Sstevel@tonic-gate 			printf("%s\n", msg);
1547c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
1557c478bd9Sstevel@tonic-gate 		return;			/* XXX: Note changed behavior */
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate #endif	/* DEBUG_RELE */
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	if (!DEV_OPS_HELD(ops)) {
1617c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC,
1627c478bd9Sstevel@tonic-gate 		    "mod_rele_dev_by_major: Unheld driver: major number <%u>",
1637c478bd9Sstevel@tonic-gate 		    (uint_t)major);
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 	DECR_DEV_OPS_REF(ops);
1667c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate struct dev_ops *
mod_hold_dev_by_devi(dev_info_t * devi)1707c478bd9Sstevel@tonic-gate mod_hold_dev_by_devi(dev_info_t *devi)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	major_t major;
1737c478bd9Sstevel@tonic-gate 	char *name;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	name = ddi_get_name(devi);
176a204de77Scth 	if ((major = mod_name_to_major(name)) == DDI_MAJOR_T_NONE)
1777c478bd9Sstevel@tonic-gate 		return (NULL);
1787c478bd9Sstevel@tonic-gate 	return (mod_hold_dev_by_major(major));
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate void
mod_rele_dev_by_devi(dev_info_t * devi)1827c478bd9Sstevel@tonic-gate mod_rele_dev_by_devi(dev_info_t *devi)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate 	major_t major;
1857c478bd9Sstevel@tonic-gate 	char *name;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	name = ddi_get_name(devi);
188a204de77Scth 	if ((major = mod_name_to_major(name)) == DDI_MAJOR_T_NONE)
1897c478bd9Sstevel@tonic-gate 		return;
1907c478bd9Sstevel@tonic-gate 	mod_rele_dev_by_major(major);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate int
nomod_zero()1947c478bd9Sstevel@tonic-gate nomod_zero()
1957c478bd9Sstevel@tonic-gate {
1967c478bd9Sstevel@tonic-gate 	return (0);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate int
nomod_minus_one()2007c478bd9Sstevel@tonic-gate nomod_minus_one()
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	return (-1);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate int
nomod_einval()2067c478bd9Sstevel@tonic-gate nomod_einval()
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	return (EINVAL);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate void
nomod_void()2127c478bd9Sstevel@tonic-gate nomod_void()
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	/* nothing */
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate  * Install all the stubs for a module.
2197c478bd9Sstevel@tonic-gate  * Return zero if there were no errors or an errno value.
2207c478bd9Sstevel@tonic-gate  */
2217c478bd9Sstevel@tonic-gate int
install_stubs_by_name(struct modctl * modp,const char * name)222*5f61829aSRobert Mustacchi install_stubs_by_name(struct modctl *modp, const char *name)
2237c478bd9Sstevel@tonic-gate {
2247c478bd9Sstevel@tonic-gate 	char *p;
225*5f61829aSRobert Mustacchi 	const char *filenamep, *iter;
2267c478bd9Sstevel@tonic-gate 	char namebuf[MODMAXNAMELEN + 12];
2277c478bd9Sstevel@tonic-gate 	struct mod_modinfo *mp;
2287c478bd9Sstevel@tonic-gate 
229*5f61829aSRobert Mustacchi 	iter = name;
2307c478bd9Sstevel@tonic-gate 	filenamep = name;
2317c478bd9Sstevel@tonic-gate 
232*5f61829aSRobert Mustacchi 	while (*iter) {
233*5f61829aSRobert Mustacchi 		if (*iter++ == '/')
234*5f61829aSRobert Mustacchi 			filenamep = iter;
235*5f61829aSRobert Mustacchi 	}
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	/*
2387c478bd9Sstevel@tonic-gate 	 * Concatenate "name" with "_modname" then look up this symbol
2397c478bd9Sstevel@tonic-gate 	 * in the kernel.  If not found, we're done.
2407c478bd9Sstevel@tonic-gate 	 * If found, then find the "mod" info structure and call init_stubs().
2417c478bd9Sstevel@tonic-gate 	 */
2427c478bd9Sstevel@tonic-gate 	p = namebuf;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	while (*filenamep && *filenamep != '.')
2457c478bd9Sstevel@tonic-gate 		*p++ = *filenamep++;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	(void) strcpy(p, "_modinfo");
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	if ((mp = (struct mod_modinfo *)modgetsymvalue(namebuf, 1)) != 0)
2507c478bd9Sstevel@tonic-gate 		return (init_stubs(modp, mp));
2517c478bd9Sstevel@tonic-gate 	else
2527c478bd9Sstevel@tonic-gate 		return (0);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate static int
init_stubs(struct modctl * modp,struct mod_modinfo * mp)2567c478bd9Sstevel@tonic-gate init_stubs(struct modctl *modp, struct mod_modinfo *mp)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	struct mod_stub_info *sp;
2597c478bd9Sstevel@tonic-gate 	int i;
2607c478bd9Sstevel@tonic-gate 	ulong_t offset;
2617c478bd9Sstevel@tonic-gate 	uintptr_t funcadr;
2627c478bd9Sstevel@tonic-gate 	char *funcname;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	modp->mod_modinfo = mp;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	/*
2677c478bd9Sstevel@tonic-gate 	 * Fill in all stubs for this module.  We can't be lazy, since
2687c478bd9Sstevel@tonic-gate 	 * some calls could come in from interrupt level, and we
2697c478bd9Sstevel@tonic-gate 	 * can't modlookup then (symbols may be paged out).
2707c478bd9Sstevel@tonic-gate 	 */
2717c478bd9Sstevel@tonic-gate 	sp = mp->modm_stubs;
2727c478bd9Sstevel@tonic-gate 	for (i = 0; sp->mods_func_adr; i++, sp++) {
2737c478bd9Sstevel@tonic-gate 		funcname = modgetsymname(sp->mods_stub_adr, &offset);
2747c478bd9Sstevel@tonic-gate 		if (funcname == NULL) {
275e099bf07Scth 			printf("init_stubs: couldn't find symbol "
276e099bf07Scth 			    "in module %s\n", mp->modm_module_name);
2777c478bd9Sstevel@tonic-gate 			return (EFAULT);
2787c478bd9Sstevel@tonic-gate 		}
2797c478bd9Sstevel@tonic-gate 		funcadr = kobj_lookup(modp->mod_mp, funcname);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 		if (kobj_addrcheck(modp->mod_mp, (caddr_t)funcadr)) {
2827c478bd9Sstevel@tonic-gate 			printf("%s:%s() not defined properly\n",
283e099bf07Scth 			    mp->modm_module_name, funcname);
2847c478bd9Sstevel@tonic-gate 			return (EFAULT);
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 		sp->mods_func_adr = funcadr;
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 	mp->mp = modp;
2897c478bd9Sstevel@tonic-gate 	return (0);
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate  * modp->mod_modinfo has to be checked in these functions before
2947c478bd9Sstevel@tonic-gate  * mod_stub_info is accessed because it's not guranteed that all
2957c478bd9Sstevel@tonic-gate  * modules define mod_stub_info structures.
2967c478bd9Sstevel@tonic-gate  */
2977c478bd9Sstevel@tonic-gate void
install_stubs(struct modctl * modp)2987c478bd9Sstevel@tonic-gate install_stubs(struct modctl *modp)
2997c478bd9Sstevel@tonic-gate {
3007c478bd9Sstevel@tonic-gate 	struct mod_stub_info *stub;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	if (modp->mod_modinfo) {
3037c478bd9Sstevel@tonic-gate 		membar_producer();
3047c478bd9Sstevel@tonic-gate 		for (stub = modp->mod_modinfo->modm_stubs;
3057c478bd9Sstevel@tonic-gate 		    stub->mods_func_adr; stub++) {
3067c478bd9Sstevel@tonic-gate 			stub->mods_flag |= MODS_INSTALLED;
3077c478bd9Sstevel@tonic-gate 		}
3087c478bd9Sstevel@tonic-gate 		membar_producer();
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate void
uninstall_stubs(struct modctl * modp)3137c478bd9Sstevel@tonic-gate uninstall_stubs(struct modctl *modp)
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate 	struct mod_stub_info *stub;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	if (modp->mod_modinfo) {
3187c478bd9Sstevel@tonic-gate 		membar_producer();
3197c478bd9Sstevel@tonic-gate 		for (stub = modp->mod_modinfo->modm_stubs;
3207c478bd9Sstevel@tonic-gate 		    stub->mods_func_adr; stub++) {
3217c478bd9Sstevel@tonic-gate 			stub->mods_flag &= ~MODS_INSTALLED;
3227c478bd9Sstevel@tonic-gate 		}
3237c478bd9Sstevel@tonic-gate 		membar_producer();
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate void
reset_stubs(struct modctl * modp)3287c478bd9Sstevel@tonic-gate reset_stubs(struct modctl *modp)
3297c478bd9Sstevel@tonic-gate {
3307c478bd9Sstevel@tonic-gate 	struct mod_stub_info *stub;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	if (modp->mod_modinfo) {
3337c478bd9Sstevel@tonic-gate 		for (stub = modp->mod_modinfo->modm_stubs;
3347c478bd9Sstevel@tonic-gate 		    stub->mods_func_adr; stub++) {
3357c478bd9Sstevel@tonic-gate 			if (stub->mods_flag & (MODS_WEAK | MODS_NOUNLOAD))
3367c478bd9Sstevel@tonic-gate 				stub->mods_func_adr =
3377c478bd9Sstevel@tonic-gate 				    (uintptr_t)stub->mods_errfcn;
3387c478bd9Sstevel@tonic-gate 			else
3397c478bd9Sstevel@tonic-gate 				stub->mods_func_adr =
3407c478bd9Sstevel@tonic-gate 				    (uintptr_t)mod_hold_stub;
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 		modp->mod_modinfo->mp = NULL;
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate struct modctl *
mod_getctl(struct modlinkage * modlp)3477c478bd9Sstevel@tonic-gate mod_getctl(struct modlinkage *modlp)
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	struct modctl	*modp;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	mutex_enter(&mod_lock);
3527c478bd9Sstevel@tonic-gate 	modp = &modules;
3537c478bd9Sstevel@tonic-gate 	do {
3547c478bd9Sstevel@tonic-gate 		if (modp->mod_linkage == modlp) {
3557c478bd9Sstevel@tonic-gate 			mutex_exit(&mod_lock);
3567c478bd9Sstevel@tonic-gate 			return (modp);
3577c478bd9Sstevel@tonic-gate 		}
3587c478bd9Sstevel@tonic-gate 	} while ((modp = modp->mod_next) != &modules);
3597c478bd9Sstevel@tonic-gate 	mutex_exit(&mod_lock);
3607c478bd9Sstevel@tonic-gate 	return (NULL);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate  * Attach driver.conf info to devnames for a driver
3667c478bd9Sstevel@tonic-gate  */
3677c478bd9Sstevel@tonic-gate struct par_list *
impl_make_parlist(major_t major)3687c478bd9Sstevel@tonic-gate impl_make_parlist(major_t major)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	int err;
3717c478bd9Sstevel@tonic-gate 	struct par_list *pl = NULL, *tmp;
3727c478bd9Sstevel@tonic-gate 	ddi_prop_t *props = NULL;
3737c478bd9Sstevel@tonic-gate 	char *confname, *drvname;
3747c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	/*
3817c478bd9Sstevel@tonic-gate 	 * If .conf file already parsed or driver removed, just return.
3827c478bd9Sstevel@tonic-gate 	 * May return NULL.
3837c478bd9Sstevel@tonic-gate 	 */
3847c478bd9Sstevel@tonic-gate 	if (dnp->dn_flags & (DN_CONF_PARSED | DN_DRIVER_REMOVED))
3857c478bd9Sstevel@tonic-gate 		return (dnp->dn_pl);
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	drvname = mod_major_to_name(major);
3887c478bd9Sstevel@tonic-gate 	if (drvname == NULL)
3897c478bd9Sstevel@tonic-gate 		return (NULL);
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	confname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
3927c478bd9Sstevel@tonic-gate 	(void) snprintf(confname, MAXNAMELEN, "drv/%s.conf", drvname);
3937c478bd9Sstevel@tonic-gate 	err = hwc_parse(confname, &pl, &props);
3947c478bd9Sstevel@tonic-gate 	kmem_free(confname, MAXNAMELEN);
3957c478bd9Sstevel@tonic-gate 	if (err)	/* file doesn't exist */
3967c478bd9Sstevel@tonic-gate 		return (NULL);
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	/*
3997c478bd9Sstevel@tonic-gate 	 * If there are global properties, reference it from dnp.
4007c478bd9Sstevel@tonic-gate 	 */
4017c478bd9Sstevel@tonic-gate 	if (props)
4027c478bd9Sstevel@tonic-gate 		dnp->dn_global_prop_ptr = i_ddi_prop_list_create(props);
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	/*
4057c478bd9Sstevel@tonic-gate 	 * Hash specs to be looked up by nexus drivers
4067c478bd9Sstevel@tonic-gate 	 */
4077c478bd9Sstevel@tonic-gate 	tmp = pl;
4087c478bd9Sstevel@tonic-gate 	while (tmp) {
4097c478bd9Sstevel@tonic-gate 		(void) hwc_hash(tmp->par_specs, major);
4107c478bd9Sstevel@tonic-gate 		tmp = tmp->par_next;
4117c478bd9Sstevel@tonic-gate 	}
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	if (!i_ddi_io_initialized()) {
4147c478bd9Sstevel@tonic-gate 		if (i_ddi_prop_search(DDI_DEV_T_ANY, DDI_FORCEATTACH,
4157c478bd9Sstevel@tonic-gate 		    DDI_PROP_TYPE_INT, &props))
4167c478bd9Sstevel@tonic-gate 			dnp->dn_flags |= DN_FORCE_ATTACH;
417e099bf07Scth 		if (i_ddi_prop_search(DDI_DEV_T_ANY, DDI_OPEN_RETURNS_EINTR,
418e099bf07Scth 		    DDI_PROP_TYPE_INT, &props))
419e099bf07Scth 			dnp->dn_flags |= DN_OPEN_RETURNS_EINTR;
420602ca9eaScth 		if (i_ddi_prop_search(DDI_DEV_T_ANY, "scsi-size-clean",
421602ca9eaScth 		    DDI_PROP_TYPE_INT, &props))
422602ca9eaScth 			dnp->dn_flags |= DN_SCSI_SIZE_CLEAN;
4237c478bd9Sstevel@tonic-gate 	}
42452cac543Sramat 
42552cac543Sramat 	if (i_ddi_prop_search(DDI_DEV_T_ANY, DDI_VHCI_CLASS,
42652cac543Sramat 	    DDI_PROP_TYPE_STRING, &props))
42752cac543Sramat 		dnp->dn_flags |= DN_PHCI_DRIVER;
42852cac543Sramat 
4296f25ad7fSJerry Gilliam 	if (i_ddi_prop_search(DDI_DEV_T_ANY, DDI_DEVID_REGISTRANT,
4306f25ad7fSJerry Gilliam 	    DDI_PROP_TYPE_INT, &props)) {
4316f25ad7fSJerry Gilliam 		dnp->dn_flags |= DN_DEVID_REGISTRANT;
4326f25ad7fSJerry Gilliam 	}
4336f25ad7fSJerry Gilliam 
4347c478bd9Sstevel@tonic-gate 	dnp->dn_flags |= DN_CONF_PARSED;
4357c478bd9Sstevel@tonic-gate 	dnp->dn_pl = pl;
4367c478bd9Sstevel@tonic-gate 	return (pl);
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate /*
4407c478bd9Sstevel@tonic-gate  * Destroy driver.conf info in devnames array for a driver
4417c478bd9Sstevel@tonic-gate  */
4427c478bd9Sstevel@tonic-gate int
impl_free_parlist(major_t major)4437c478bd9Sstevel@tonic-gate impl_free_parlist(major_t major)
4447c478bd9Sstevel@tonic-gate {
4457c478bd9Sstevel@tonic-gate 	struct par_list *pl;
4467c478bd9Sstevel@tonic-gate 	struct devnames *dnp = &devnamesp[major];
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	/*
4497c478bd9Sstevel@tonic-gate 	 * Unref driver global property list. Don't destroy it
4507c478bd9Sstevel@tonic-gate 	 * because some instances may still be referencing it.
4517c478bd9Sstevel@tonic-gate 	 * The property list will be freed when the last ref
4527c478bd9Sstevel@tonic-gate 	 * goes away.
4537c478bd9Sstevel@tonic-gate 	 */
4547c478bd9Sstevel@tonic-gate 	if (dnp->dn_global_prop_ptr) {
4557c478bd9Sstevel@tonic-gate 		i_ddi_prop_list_rele(dnp->dn_global_prop_ptr, dnp);
4567c478bd9Sstevel@tonic-gate 		dnp->dn_global_prop_ptr = NULL;
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	/*
4607c478bd9Sstevel@tonic-gate 	 * remove specs from hash table
4617c478bd9Sstevel@tonic-gate 	 */
4627c478bd9Sstevel@tonic-gate 	for (pl = dnp->dn_pl; pl; pl = pl->par_next)
4637c478bd9Sstevel@tonic-gate 		hwc_unhash(pl->par_specs);
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	impl_delete_par_list(dnp->dn_pl);
4667c478bd9Sstevel@tonic-gate 	dnp->dn_pl = NULL;
4677c478bd9Sstevel@tonic-gate 	dnp->dn_flags &= ~DN_CONF_PARSED;
4687c478bd9Sstevel@tonic-gate 	return (0);
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate struct bind *mb_hashtab[MOD_BIND_HASHSIZE];
4727c478bd9Sstevel@tonic-gate struct bind *sb_hashtab[MOD_BIND_HASHSIZE];
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate static int
nm_hash(const char * name)475*5f61829aSRobert Mustacchi nm_hash(const char *name)
4767c478bd9Sstevel@tonic-gate {
4777c478bd9Sstevel@tonic-gate 	char c;
4787c478bd9Sstevel@tonic-gate 	int hash = 0;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	for (c = *name++; c; c = *name++)
4817c478bd9Sstevel@tonic-gate 		hash ^= c;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 	return (hash & MOD_BIND_HASHMASK);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate void
clear_binding_hash(struct bind ** bhash)4877c478bd9Sstevel@tonic-gate clear_binding_hash(struct bind **bhash)
4887c478bd9Sstevel@tonic-gate {
4897c478bd9Sstevel@tonic-gate 	int i;
4907c478bd9Sstevel@tonic-gate 	struct bind *bp, *bp1;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	for (i = 0; i < MOD_BIND_HASHSIZE; i++) {
4937c478bd9Sstevel@tonic-gate 		bp = bhash[i];
4947c478bd9Sstevel@tonic-gate 		while (bp != NULL) {
4957c478bd9Sstevel@tonic-gate 			kmem_free(bp->b_name, strlen(bp->b_name) + 1);
4967c478bd9Sstevel@tonic-gate 			if (bp->b_bind_name) {
4977c478bd9Sstevel@tonic-gate 				kmem_free(bp->b_bind_name,
4987c478bd9Sstevel@tonic-gate 				    strlen(bp->b_bind_name) + 1);
4997c478bd9Sstevel@tonic-gate 			}
5007c478bd9Sstevel@tonic-gate 			bp1 = bp;
5017c478bd9Sstevel@tonic-gate 			bp = bp->b_next;
5027c478bd9Sstevel@tonic-gate 			kmem_free(bp1, sizeof (struct bind));
5037c478bd9Sstevel@tonic-gate 		}
5047c478bd9Sstevel@tonic-gate 		bhash[i] = NULL;
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate 
5086532b960SJerry Gilliam /* Find an mbind by name match (caller can ask for deleted match) */
5097c478bd9Sstevel@tonic-gate static struct bind *
find_mbind(const char * name,struct bind ** hashtab,int deleted)510*5f61829aSRobert Mustacchi find_mbind(const char *name, struct bind **hashtab, int deleted)
5117c478bd9Sstevel@tonic-gate {
5126532b960SJerry Gilliam 	struct bind	*mb;
5136532b960SJerry Gilliam 
5146532b960SJerry Gilliam 	for (mb = hashtab[nm_hash(name)]; mb; mb = mb->b_next) {
5156532b960SJerry Gilliam 		if (deleted && (mb->b_num >= 0))
5166532b960SJerry Gilliam 			continue;			/* skip active */
5176532b960SJerry Gilliam 		if (!deleted && (mb->b_num < 0))
5186532b960SJerry Gilliam 			continue;			/* skip deleted */
5197c478bd9Sstevel@tonic-gate 
5206532b960SJerry Gilliam 		/* return if name matches */
5216532b960SJerry Gilliam 		if (strcmp(name, mb->b_name) == 0) {
5227c478bd9Sstevel@tonic-gate 			break;
5236532b960SJerry Gilliam 		}
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate 	return (mb);
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate /*
5297c478bd9Sstevel@tonic-gate  * Create an entry for the given (name, major, bind_name) tuple in the
5307c478bd9Sstevel@tonic-gate  * hash table supplied.  Reject the attempt to do so if 'name' is already
5317c478bd9Sstevel@tonic-gate  * in the hash table.
5327c478bd9Sstevel@tonic-gate  *
5337c478bd9Sstevel@tonic-gate  * Does not provide synchronization, so use only during boot or with
5347c478bd9Sstevel@tonic-gate  * externally provided locking.
5357c478bd9Sstevel@tonic-gate  */
5367c478bd9Sstevel@tonic-gate int
make_mbind(char * name,int num,char * bind_name,struct bind ** hashtab)5376532b960SJerry Gilliam make_mbind(char *name, int num, char *bind_name, struct bind **hashtab)
5387c478bd9Sstevel@tonic-gate {
5396532b960SJerry Gilliam 	struct bind	*mb;
5406532b960SJerry Gilliam 	struct bind	**pmb;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	ASSERT(hashtab != NULL);
5436532b960SJerry Gilliam 	ASSERT(num >= 0);
5447c478bd9Sstevel@tonic-gate 
5456532b960SJerry Gilliam 	/* Fail if the key being added is already established */
5466532b960SJerry Gilliam 	if (find_mbind(name, hashtab, 0) != NULL)
5477c478bd9Sstevel@tonic-gate 		return (-1);
5487c478bd9Sstevel@tonic-gate 
5496532b960SJerry Gilliam 	/* Allocate new mbind */
5506532b960SJerry Gilliam 	mb = kmem_zalloc(sizeof (struct bind), KM_SLEEP);
5516532b960SJerry Gilliam 	mb->b_name = i_ddi_strdup(name, KM_SLEEP);
5526532b960SJerry Gilliam 	mb->b_num = num;
5536532b960SJerry Gilliam 	if (bind_name != NULL)
5546532b960SJerry Gilliam 		mb->b_bind_name = i_ddi_strdup(bind_name, KM_SLEEP);
5556532b960SJerry Gilliam 
5566532b960SJerry Gilliam 	/* Insert at head of hash */
5576532b960SJerry Gilliam 	pmb = &hashtab[nm_hash(name)];
5586532b960SJerry Gilliam 	mb->b_next = *pmb;
5596532b960SJerry Gilliam 	*pmb = mb;
5607c478bd9Sstevel@tonic-gate 	return (0);
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate /*
5646532b960SJerry Gilliam  * Delete a binding from a binding-hash. Since there is no locking we
5656532b960SJerry Gilliam  * delete an mbind by making its b_num negative. We also support find_mbind
5666532b960SJerry Gilliam  * of deleted entries, so we still need deleted items on the list.
5677c478bd9Sstevel@tonic-gate  */
5687c478bd9Sstevel@tonic-gate void
delete_mbind(const char * name,struct bind ** hashtab)569*5f61829aSRobert Mustacchi delete_mbind(const char *name, struct bind **hashtab)
5707c478bd9Sstevel@tonic-gate {
5716532b960SJerry Gilliam 	struct bind	*mb;
5726532b960SJerry Gilliam 
5736532b960SJerry Gilliam 	for (mb = hashtab[nm_hash(name)]; mb; mb = mb->b_next) {
5746532b960SJerry Gilliam 		if ((mb->b_num >= 0) && (strcmp(name, mb->b_name) == 0)) {
5756532b960SJerry Gilliam 			/* delete by making b_num negative */
5766532b960SJerry Gilliam 			if (moddebug & MODDEBUG_BINDING) {
5776532b960SJerry Gilliam 				cmn_err(CE_CONT, "mbind: %s %d deleted\n",
5786532b960SJerry Gilliam 				    name, mb->b_num);
5797c478bd9Sstevel@tonic-gate 			}
5806532b960SJerry Gilliam 			mb->b_num = -mb->b_num;
5816532b960SJerry Gilliam 			break;
5827c478bd9Sstevel@tonic-gate 		}
5837c478bd9Sstevel@tonic-gate 	}
5846532b960SJerry Gilliam }
5857c478bd9Sstevel@tonic-gate 
5866532b960SJerry Gilliam /*
5876532b960SJerry Gilliam  * Delete all items in an mbind associated with specified num.
5886532b960SJerry Gilliam  * An example would be rem_drv deleting all aliases associated with a
5896532b960SJerry Gilliam  * driver major number.
5906532b960SJerry Gilliam  */
5916532b960SJerry Gilliam void
purge_mbind(int num,struct bind ** hashtab)5926532b960SJerry Gilliam purge_mbind(int num, struct bind **hashtab)
5936532b960SJerry Gilliam {
5946532b960SJerry Gilliam 	int		i;
5956532b960SJerry Gilliam 	struct bind	*mb;
5966532b960SJerry Gilliam 
5976532b960SJerry Gilliam 	/* search all hash lists for items that associated with 'num' */
5986532b960SJerry Gilliam 	for (i = 0; i < MOD_BIND_HASHSIZE; i++) {
5996532b960SJerry Gilliam 		for (mb = hashtab[i]; mb; mb = mb->b_next) {
6006532b960SJerry Gilliam 			if (mb->b_num == num) {
6016532b960SJerry Gilliam 				if (moddebug & MODDEBUG_BINDING)
6026532b960SJerry Gilliam 					cmn_err(CE_CONT,
6036532b960SJerry Gilliam 					    "mbind: %s %d purged\n",
6046532b960SJerry Gilliam 					    mb->b_name, num);
6056532b960SJerry Gilliam 				/* purge by changing the sign */
6066532b960SJerry Gilliam 				mb->b_num = -num;
6076532b960SJerry Gilliam 			}
6086532b960SJerry Gilliam 		}
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate major_t
mod_name_to_major(const char * name)613*5f61829aSRobert Mustacchi mod_name_to_major(const char *name)
6147c478bd9Sstevel@tonic-gate {
6156532b960SJerry Gilliam 	struct bind	*mbind;
6166532b960SJerry Gilliam 	major_t		maj;
6176532b960SJerry Gilliam 
6186532b960SJerry Gilliam 	/* Search for non-deleted match. */
6196532b960SJerry Gilliam 	if ((mbind = find_mbind(name, mb_hashtab, 0)) != NULL) {
6206532b960SJerry Gilliam 		if (moddebug & MODDEBUG_BINDING) {
6216532b960SJerry Gilliam 			if (find_mbind(name, mb_hashtab, 1))
6226532b960SJerry Gilliam 				cmn_err(CE_CONT,
6236532b960SJerry Gilliam 				    "'%s' has deleted match too\n", name);
6246532b960SJerry Gilliam 		}
6257c478bd9Sstevel@tonic-gate 		return ((major_t)mbind->b_num);
6266532b960SJerry Gilliam 	}
6276532b960SJerry Gilliam 
6286532b960SJerry Gilliam 	/*
6296532b960SJerry Gilliam 	 * Search for deleted match: We may find that we have dependencies
6306532b960SJerry Gilliam 	 * on drivers that have been deleted (but the old driver may still
6316532b960SJerry Gilliam 	 * be bound to a node). These callers should be converted to use
6326532b960SJerry Gilliam 	 * ddi_driver_major(i.e. devi_major).
6336532b960SJerry Gilliam 	 */
6346532b960SJerry Gilliam 	if (moddebug & MODDEBUG_BINDING) {
6356532b960SJerry Gilliam 		if ((mbind = find_mbind(name, mb_hashtab, 1)) != NULL) {
6366532b960SJerry Gilliam 			maj = (major_t)(-(mbind->b_num));
6376532b960SJerry Gilliam 			cmn_err(CE_CONT, "Reference to deleted alias '%s' %d\n",
6386532b960SJerry Gilliam 			    name, maj);
6396532b960SJerry Gilliam 		}
6406532b960SJerry Gilliam 	}
6417c478bd9Sstevel@tonic-gate 
642a204de77Scth 	return (DDI_MAJOR_T_NONE);
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate char *
mod_major_to_name(major_t major)6467c478bd9Sstevel@tonic-gate mod_major_to_name(major_t major)
6477c478bd9Sstevel@tonic-gate {
648c9cc1492SJerry Gilliam 	if (!driver_installed(major))
6497c478bd9Sstevel@tonic-gate 		return (NULL);
6507c478bd9Sstevel@tonic-gate 	return ((&devnamesp[major])->dn_name);
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate /*
6547c478bd9Sstevel@tonic-gate  * Set up the devnames array.  Error check for duplicate entries.
6557c478bd9Sstevel@tonic-gate  */
6567c478bd9Sstevel@tonic-gate void
init_devnamesp(int size)6577c478bd9Sstevel@tonic-gate init_devnamesp(int size)
6587c478bd9Sstevel@tonic-gate {
6597c478bd9Sstevel@tonic-gate 	int hshndx;
6607c478bd9Sstevel@tonic-gate 	struct bind *bp;
6617c478bd9Sstevel@tonic-gate 	static char dupwarn[] =
6627c478bd9Sstevel@tonic-gate 	    "!Device entry \"%s %d\" conflicts with previous entry \"%s %d\" "
6637c478bd9Sstevel@tonic-gate 	    "in /etc/name_to_major.";
6647c478bd9Sstevel@tonic-gate 	static char badmaj[] = "The major number %u is invalid.";
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	ASSERT(size <= L_MAXMAJ32 && size > 0);
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	/*
6697c478bd9Sstevel@tonic-gate 	 * Allocate the devnames array.  All mutexes and cv's will be
6707c478bd9Sstevel@tonic-gate 	 * automagically initialized.
6717c478bd9Sstevel@tonic-gate 	 */
6727c478bd9Sstevel@tonic-gate 	devnamesp = kobj_zalloc(size * sizeof (struct devnames), KM_SLEEP);
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	/*
6757c478bd9Sstevel@tonic-gate 	 * Stick the contents of mb_hashtab into the devnames array.  Warn if
6767c478bd9Sstevel@tonic-gate 	 * two hash entries correspond to the same major number, or if a
6777c478bd9Sstevel@tonic-gate 	 * major number is out of range.
6787c478bd9Sstevel@tonic-gate 	 */
6797c478bd9Sstevel@tonic-gate 	for (hshndx = 0; hshndx < MOD_BIND_HASHSIZE; hshndx++) {
6807c478bd9Sstevel@tonic-gate 		for (bp = mb_hashtab[hshndx]; bp; bp = bp->b_next) {
681c9cc1492SJerry Gilliam 			if (make_devname(bp->b_name,
682c9cc1492SJerry Gilliam 			    (major_t)bp->b_num, 0) != 0) {
6837c478bd9Sstevel@tonic-gate 				/*
6847c478bd9Sstevel@tonic-gate 				 * If there is not an entry at b_num already,
6857c478bd9Sstevel@tonic-gate 				 * then this must be a bad major number.
6867c478bd9Sstevel@tonic-gate 				 */
6877c478bd9Sstevel@tonic-gate 				char *nm = mod_major_to_name(bp->b_num);
6887c478bd9Sstevel@tonic-gate 				if (nm == NULL) {
6897c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, badmaj,
6907c478bd9Sstevel@tonic-gate 					    (uint_t)bp->b_num);
6917c478bd9Sstevel@tonic-gate 				} else {
6927c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, dupwarn, bp->b_name,
6937c478bd9Sstevel@tonic-gate 					    bp->b_num, nm, bp->b_num);
6947c478bd9Sstevel@tonic-gate 				}
6957c478bd9Sstevel@tonic-gate 			}
6967c478bd9Sstevel@tonic-gate 		}
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	/* Initialize hash table for hwc_spec's */
7007c478bd9Sstevel@tonic-gate 	hwc_hash_init();
7017c478bd9Sstevel@tonic-gate }
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate int
make_devname(const char * name,major_t major,int dn_flags)704*5f61829aSRobert Mustacchi make_devname(const char *name, major_t major, int dn_flags)
7057c478bd9Sstevel@tonic-gate {
7067c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
7077c478bd9Sstevel@tonic-gate 	char *copy;
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 	/*
7107c478bd9Sstevel@tonic-gate 	 * Until on-disk support for major nums > 14 bits arrives, fail
7117c478bd9Sstevel@tonic-gate 	 * any major numbers that are too big.
7127c478bd9Sstevel@tonic-gate 	 */
7137c478bd9Sstevel@tonic-gate 	if (major > L_MAXMAJ32)
7147c478bd9Sstevel@tonic-gate 		return (EINVAL);
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
7177c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
7187c478bd9Sstevel@tonic-gate 	if (dnp->dn_name) {
7197c478bd9Sstevel@tonic-gate 		if (strcmp(dnp->dn_name, name) != 0) {
7207c478bd9Sstevel@tonic-gate 			/* Another driver already here */
7217c478bd9Sstevel@tonic-gate 			UNLOCK_DEV_OPS(&dnp->dn_lock);
7227c478bd9Sstevel@tonic-gate 			return (EINVAL);
7237c478bd9Sstevel@tonic-gate 		}
7247c478bd9Sstevel@tonic-gate 		/* Adding back a removed driver */
7257c478bd9Sstevel@tonic-gate 		dnp->dn_flags &= ~DN_DRIVER_REMOVED;
726c9cc1492SJerry Gilliam 		dnp->dn_flags |= dn_flags;
7277c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
7287c478bd9Sstevel@tonic-gate 		return (0);
7297c478bd9Sstevel@tonic-gate 	}
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	/*
7327c478bd9Sstevel@tonic-gate 	 * Check if flag is taken by getudev()
7337c478bd9Sstevel@tonic-gate 	 */
7347c478bd9Sstevel@tonic-gate 	if (dnp->dn_flags & DN_TAKEN_GETUDEV) {
7357c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
7367c478bd9Sstevel@tonic-gate 		return (EINVAL);
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	copy = kmem_alloc(strlen(name) + 1, KM_SLEEP);
7407c478bd9Sstevel@tonic-gate 	(void) strcpy(copy, name);
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	/* Make sure string is copied before setting dn_name */
7437c478bd9Sstevel@tonic-gate 	membar_producer();
7447c478bd9Sstevel@tonic-gate 	dnp->dn_name = copy;
745c9cc1492SJerry Gilliam 	dnp->dn_flags = dn_flags;
7467c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7477c478bd9Sstevel@tonic-gate 	return (0);
7487c478bd9Sstevel@tonic-gate }
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate /*
7517c478bd9Sstevel@tonic-gate  * Set up the syscallnames array.
7527c478bd9Sstevel@tonic-gate  */
7537c478bd9Sstevel@tonic-gate void
init_syscallnames(int size)7547c478bd9Sstevel@tonic-gate init_syscallnames(int size)
7557c478bd9Sstevel@tonic-gate {
7567c478bd9Sstevel@tonic-gate 	int hshndx;
7577c478bd9Sstevel@tonic-gate 	struct bind *bp;
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	syscallnames = kobj_zalloc(size * sizeof (char *), KM_SLEEP);
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	for (hshndx = 0; hshndx < MOD_BIND_HASHSIZE; hshndx++) {
7627c478bd9Sstevel@tonic-gate 		for (bp = sb_hashtab[hshndx]; bp; bp = bp->b_next) {
7637730e2acScg 			if (bp->b_num < 0 || bp->b_num >= size) {
7647730e2acScg 				cmn_err(CE_WARN,
7657730e2acScg 				    "!Couldn't add system call \"%s %d\". "
7667730e2acScg 				    "Value out of range (0..%d) in "
7677730e2acScg 				    "/etc/name_to_sysnum.",
7687730e2acScg 				    bp->b_name, bp->b_num, size - 1);
7697730e2acScg 				continue;
7707730e2acScg 			}
7717c478bd9Sstevel@tonic-gate 			make_syscallname(bp->b_name, bp->b_num);
7727c478bd9Sstevel@tonic-gate 		}
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate }
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate static void
make_syscallname(const char * name,int sysno)777*5f61829aSRobert Mustacchi make_syscallname(const char *name, int sysno)
7787c478bd9Sstevel@tonic-gate {
7797c478bd9Sstevel@tonic-gate 	char **cp = &syscallnames[sysno];
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	if (*cp != NULL) {
7827c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "!Couldn't add system call \"%s %d\". "
7837c478bd9Sstevel@tonic-gate 		    "It conflicts with \"%s %d\" in /etc/name_to_sysnum.",
7847c478bd9Sstevel@tonic-gate 		    name, sysno, *cp, sysno);
7857c478bd9Sstevel@tonic-gate 		return;
7867c478bd9Sstevel@tonic-gate 	}
7877c478bd9Sstevel@tonic-gate 	*cp = kmem_alloc(strlen(name) + 1, KM_SLEEP);
7887c478bd9Sstevel@tonic-gate 	(void) strcpy(*cp, name);
7897c478bd9Sstevel@tonic-gate }
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate /*
7927c478bd9Sstevel@tonic-gate  * Given a system call name, get its number.
7937c478bd9Sstevel@tonic-gate  */
7947c478bd9Sstevel@tonic-gate int
mod_getsysnum(const char * name)795*5f61829aSRobert Mustacchi mod_getsysnum(const char *name)
7967c478bd9Sstevel@tonic-gate {
7977c478bd9Sstevel@tonic-gate 	struct bind *mbind;
7987c478bd9Sstevel@tonic-gate 
7996532b960SJerry Gilliam 	if ((mbind = find_mbind(name, sb_hashtab, 0)) != NULL)
8007c478bd9Sstevel@tonic-gate 		return (mbind->b_num);
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	return (-1);
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate /*
8067c478bd9Sstevel@tonic-gate  * Given a system call number, get the system call name.
8077c478bd9Sstevel@tonic-gate  */
8087c478bd9Sstevel@tonic-gate char *
mod_getsysname(int sysnum)8097c478bd9Sstevel@tonic-gate mod_getsysname(int sysnum)
8107c478bd9Sstevel@tonic-gate {
8117c478bd9Sstevel@tonic-gate 	return (syscallnames[sysnum]);
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate /*
8157c478bd9Sstevel@tonic-gate  * Find the name of the module containing the specified pc.
8167c478bd9Sstevel@tonic-gate  * Returns the name on success, "<unknown>" on failure.
8177c478bd9Sstevel@tonic-gate  * No mod_lock locking is required because things are never deleted from
8187c478bd9Sstevel@tonic-gate  * the &modules list.
8197c478bd9Sstevel@tonic-gate  */
8207c478bd9Sstevel@tonic-gate char *
mod_containing_pc(caddr_t pc)8217c478bd9Sstevel@tonic-gate mod_containing_pc(caddr_t pc)
8227c478bd9Sstevel@tonic-gate {
8237c478bd9Sstevel@tonic-gate 	struct modctl	*mcp = &modules;
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	do {
8267c478bd9Sstevel@tonic-gate 		if (mcp->mod_mp != NULL &&
8277c478bd9Sstevel@tonic-gate 		    (size_t)pc - (size_t)mcp->mod_text < mcp->mod_text_size)
8287c478bd9Sstevel@tonic-gate 			return (mcp->mod_modname);
8297c478bd9Sstevel@tonic-gate 	} while ((mcp = mcp->mod_next) != &modules);
8307c478bd9Sstevel@tonic-gate 	return ("<unknown>");
8317c478bd9Sstevel@tonic-gate }
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate /*
8347c478bd9Sstevel@tonic-gate  * Hash tables for hwc_spec
8357c478bd9Sstevel@tonic-gate  *
8367c478bd9Sstevel@tonic-gate  * The purpose of these hash tables are to allow the framework to discover
8377c478bd9Sstevel@tonic-gate  * all possible .conf children for a given nexus. There are two hash tables.
8387c478bd9Sstevel@tonic-gate  * One is hashed based on parent name, the on the class name. Each
8397c478bd9Sstevel@tonic-gate  * driver.conf file translates to a list of hwc_spec's. Adding and
8407c478bd9Sstevel@tonic-gate  * removing the entire list is an atomic operation, protected by
8417c478bd9Sstevel@tonic-gate  * the hwc_hash_lock.
8427c478bd9Sstevel@tonic-gate  *
8437c478bd9Sstevel@tonic-gate  * What we get from all the hashing is the function hwc_get_child_spec().
8447c478bd9Sstevel@tonic-gate  */
8457c478bd9Sstevel@tonic-gate #define	HWC_SPEC_HASHSIZE	(1 << 6)	/* 64 */
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate static mod_hash_t *hwc_par_hash;	/* hash by parent name */
8487c478bd9Sstevel@tonic-gate static mod_hash_t *hwc_class_hash;	/* hash by class name */
8497c478bd9Sstevel@tonic-gate static kmutex_t hwc_hash_lock;		/* lock protecting hwc hashes */
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate /*
8527c478bd9Sstevel@tonic-gate  * Initialize hash tables for parent and class specs
8537c478bd9Sstevel@tonic-gate  */
8547c478bd9Sstevel@tonic-gate static void
hwc_hash_init()8557c478bd9Sstevel@tonic-gate hwc_hash_init()
8567c478bd9Sstevel@tonic-gate {
8577c478bd9Sstevel@tonic-gate 	hwc_par_hash = mod_hash_create_strhash("hwc parent spec hash",
8587c478bd9Sstevel@tonic-gate 	    HWC_SPEC_HASHSIZE, mod_hash_null_valdtor);
8597c478bd9Sstevel@tonic-gate 	hwc_class_hash = mod_hash_create_strhash("hwc class spec hash",
8607c478bd9Sstevel@tonic-gate 	    HWC_SPEC_HASHSIZE, mod_hash_null_valdtor);
8617c478bd9Sstevel@tonic-gate }
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate /*
8647c478bd9Sstevel@tonic-gate  * Insert a spec into hash table. hwc_hash_lock must be held
8657c478bd9Sstevel@tonic-gate  */
8667c478bd9Sstevel@tonic-gate static void
hwc_hash_insert(struct hwc_spec * spec,const char * name,mod_hash_t * hash)867*5f61829aSRobert Mustacchi hwc_hash_insert(struct hwc_spec *spec, const char *name, mod_hash_t *hash)
8687c478bd9Sstevel@tonic-gate {
8697c478bd9Sstevel@tonic-gate 	mod_hash_key_t key;
8707c478bd9Sstevel@tonic-gate 	struct hwc_spec *entry = NULL;
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	ASSERT(name != NULL);
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	if (mod_hash_find(hash, (mod_hash_key_t)name,
8757c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)&entry) != 0) {
8767c478bd9Sstevel@tonic-gate 		/* Name doesn't exist, insert a new key */
8777c478bd9Sstevel@tonic-gate 		key = kmem_alloc(strlen(name) + 1, KM_SLEEP);
8787c478bd9Sstevel@tonic-gate 		(void) strcpy((char *)key, name);
8797c478bd9Sstevel@tonic-gate 		if (mod_hash_insert(hash, key, (mod_hash_val_t)spec) != 0) {
8807c478bd9Sstevel@tonic-gate 			kmem_free(key, strlen(name) + 1);
8817c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "hwc hash state inconsistent");
8827c478bd9Sstevel@tonic-gate 		}
8837c478bd9Sstevel@tonic-gate 		return;
8847c478bd9Sstevel@tonic-gate 	}
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	/*
8877c478bd9Sstevel@tonic-gate 	 * Name is already present, append spec to the list.
8887c478bd9Sstevel@tonic-gate 	 * This is the case when driver.conf specifies multiple
8897c478bd9Sstevel@tonic-gate 	 * nodes under a single parent or class.
8907c478bd9Sstevel@tonic-gate 	 */
8917c478bd9Sstevel@tonic-gate 	while (entry->hwc_hash_next)
8927c478bd9Sstevel@tonic-gate 		entry = entry->hwc_hash_next;
8937c478bd9Sstevel@tonic-gate 	entry->hwc_hash_next = spec;
8947c478bd9Sstevel@tonic-gate }
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate /*
8977c478bd9Sstevel@tonic-gate  * Remove a spec entry from spec hash table, the spec itself is
8987c478bd9Sstevel@tonic-gate  * destroyed external to this function.
8997c478bd9Sstevel@tonic-gate  */
9007c478bd9Sstevel@tonic-gate static void
hwc_hash_remove(struct hwc_spec * spec,const char * name,mod_hash_t * hash)901*5f61829aSRobert Mustacchi hwc_hash_remove(struct hwc_spec *spec, const char *name, mod_hash_t *hash)
9027c478bd9Sstevel@tonic-gate {
9037c478bd9Sstevel@tonic-gate 	char *key;
9047c478bd9Sstevel@tonic-gate 	struct hwc_spec *entry;
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	ASSERT(name != NULL);
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	if (mod_hash_find(hash, (mod_hash_key_t)name,
9097c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)&entry) != 0) {
9107c478bd9Sstevel@tonic-gate 		return;	/* name not found in hash */
9117c478bd9Sstevel@tonic-gate 	}
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	/*
9147c478bd9Sstevel@tonic-gate 	 * If the head is the spec to be removed, either destroy the
9157c478bd9Sstevel@tonic-gate 	 * entry or replace it with the remaining list.
9167c478bd9Sstevel@tonic-gate 	 */
9177c478bd9Sstevel@tonic-gate 	if (entry == spec) {
9187c478bd9Sstevel@tonic-gate 		if (spec->hwc_hash_next == NULL) {
9197c478bd9Sstevel@tonic-gate 			(void) mod_hash_destroy(hash, (mod_hash_key_t)name);
9207c478bd9Sstevel@tonic-gate 			return;
9217c478bd9Sstevel@tonic-gate 		}
9227c478bd9Sstevel@tonic-gate 		key = kmem_alloc(strlen(name) + 1, KM_SLEEP);
9237c478bd9Sstevel@tonic-gate 		(void) strcpy(key, name);
9247c478bd9Sstevel@tonic-gate 		(void) mod_hash_replace(hash, (mod_hash_key_t)key,
9257c478bd9Sstevel@tonic-gate 		    (mod_hash_val_t)spec->hwc_hash_next);
9267c478bd9Sstevel@tonic-gate 		spec->hwc_hash_next = NULL;
9277c478bd9Sstevel@tonic-gate 		return;
9287c478bd9Sstevel@tonic-gate 	}
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	/*
9317c478bd9Sstevel@tonic-gate 	 * If the head is not the one, look for the spec in the
9327c478bd9Sstevel@tonic-gate 	 * hwc_hash_next linkage.
9337c478bd9Sstevel@tonic-gate 	 */
9347c478bd9Sstevel@tonic-gate 	while (entry->hwc_hash_next && (entry->hwc_hash_next != spec))
9357c478bd9Sstevel@tonic-gate 		entry = entry->hwc_hash_next;
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	if (entry->hwc_hash_next) {
9387c478bd9Sstevel@tonic-gate 		entry->hwc_hash_next = spec->hwc_hash_next;
9397c478bd9Sstevel@tonic-gate 		spec->hwc_hash_next = NULL;
9407c478bd9Sstevel@tonic-gate 	}
9417c478bd9Sstevel@tonic-gate }
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate /*
9447c478bd9Sstevel@tonic-gate  * Hash a list of specs based on either parent name or class name
9457c478bd9Sstevel@tonic-gate  */
9467c478bd9Sstevel@tonic-gate static void
hwc_hash(struct hwc_spec * spec_list,major_t major)9477c478bd9Sstevel@tonic-gate hwc_hash(struct hwc_spec *spec_list, major_t major)
9487c478bd9Sstevel@tonic-gate {
9497c478bd9Sstevel@tonic-gate 	struct hwc_spec *spec = spec_list;
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	mutex_enter(&hwc_hash_lock);
9527c478bd9Sstevel@tonic-gate 	while (spec) {
9537c478bd9Sstevel@tonic-gate 		/* Put driver major here so parent can find it */
9547c478bd9Sstevel@tonic-gate 		spec->hwc_major = major;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 		if (spec->hwc_parent_name != NULL) {
9577c478bd9Sstevel@tonic-gate 			hwc_hash_insert(spec, spec->hwc_parent_name,
9587c478bd9Sstevel@tonic-gate 			    hwc_par_hash);
9597c478bd9Sstevel@tonic-gate 		} else if (spec->hwc_class_name != NULL) {
9607c478bd9Sstevel@tonic-gate 			hwc_hash_insert(spec, spec->hwc_class_name,
9617c478bd9Sstevel@tonic-gate 			    hwc_class_hash);
9627c478bd9Sstevel@tonic-gate 		} else {
9637c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
9647c478bd9Sstevel@tonic-gate 			    "hwc_hash: No class or parent specified");
9657c478bd9Sstevel@tonic-gate 		}
9667c478bd9Sstevel@tonic-gate 		spec = spec->hwc_next;
9677c478bd9Sstevel@tonic-gate 	}
9687c478bd9Sstevel@tonic-gate 	mutex_exit(&hwc_hash_lock);
9697c478bd9Sstevel@tonic-gate }
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate /*
9727c478bd9Sstevel@tonic-gate  * Remove a list of specs from hash tables. Don't destroy the specs yet.
9737c478bd9Sstevel@tonic-gate  */
9747c478bd9Sstevel@tonic-gate static void
hwc_unhash(struct hwc_spec * spec_list)9757c478bd9Sstevel@tonic-gate hwc_unhash(struct hwc_spec *spec_list)
9767c478bd9Sstevel@tonic-gate {
9777c478bd9Sstevel@tonic-gate 	struct hwc_spec *spec = spec_list;
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	mutex_enter(&hwc_hash_lock);
9807c478bd9Sstevel@tonic-gate 	while (spec) {
9817c478bd9Sstevel@tonic-gate 		if (spec->hwc_parent_name != NULL) {
9827c478bd9Sstevel@tonic-gate 			hwc_hash_remove(spec, spec->hwc_parent_name,
9837c478bd9Sstevel@tonic-gate 			    hwc_par_hash);
9847c478bd9Sstevel@tonic-gate 		} else if (spec->hwc_class_name != NULL) {
9857c478bd9Sstevel@tonic-gate 			hwc_hash_remove(spec, spec->hwc_class_name,
9867c478bd9Sstevel@tonic-gate 			    hwc_class_hash);
9877c478bd9Sstevel@tonic-gate 		} else {
9887c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
9897c478bd9Sstevel@tonic-gate 			    "hwc_unhash: No class or parent specified");
9907c478bd9Sstevel@tonic-gate 		}
9917c478bd9Sstevel@tonic-gate 		spec = spec->hwc_next;
9927c478bd9Sstevel@tonic-gate 	}
9937c478bd9Sstevel@tonic-gate 	mutex_exit(&hwc_hash_lock);
9947c478bd9Sstevel@tonic-gate }
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate /*
9977c478bd9Sstevel@tonic-gate  * Make a copy of specs in a hash entry and add to the end of listp.
9987c478bd9Sstevel@tonic-gate  * Called by nexus to locate a list of child specs.
9997c478bd9Sstevel@tonic-gate  *
10007c478bd9Sstevel@tonic-gate  * entry is a list of hwc_spec chained together with hwc_hash_next.
10017c478bd9Sstevel@tonic-gate  * listp points to list chained together with hwc_next.
10027c478bd9Sstevel@tonic-gate  */
10037c478bd9Sstevel@tonic-gate static void
hwc_spec_add(struct hwc_spec ** listp,struct hwc_spec * entry,major_t match_major)10047c478bd9Sstevel@tonic-gate hwc_spec_add(struct hwc_spec **listp, struct hwc_spec *entry,
10057c478bd9Sstevel@tonic-gate     major_t match_major)
10067c478bd9Sstevel@tonic-gate {
10077c478bd9Sstevel@tonic-gate 	/* Find the tail of the list */
10087c478bd9Sstevel@tonic-gate 	while (*listp)
10097c478bd9Sstevel@tonic-gate 		listp = &(*listp)->hwc_next;
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	while (entry) {
10127c478bd9Sstevel@tonic-gate 		struct hwc_spec *spec;
10137c478bd9Sstevel@tonic-gate 
1014a204de77Scth 		if ((match_major != DDI_MAJOR_T_NONE) &&
10157c478bd9Sstevel@tonic-gate 		    (match_major != entry->hwc_major)) {
10167c478bd9Sstevel@tonic-gate 			entry = entry->hwc_hash_next;
10177c478bd9Sstevel@tonic-gate 			continue;
10187c478bd9Sstevel@tonic-gate 		}
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 		/*
10217c478bd9Sstevel@tonic-gate 		 * Allocate spec and copy the content of entry.
10227c478bd9Sstevel@tonic-gate 		 * No need to copy class/parent name since caller
10237c478bd9Sstevel@tonic-gate 		 * already knows the parent dip.
10247c478bd9Sstevel@tonic-gate 		 */
10257c478bd9Sstevel@tonic-gate 		spec = kmem_zalloc(sizeof (*spec), KM_SLEEP);
10267c478bd9Sstevel@tonic-gate 		spec->hwc_devi_name = i_ddi_strdup(
10277c478bd9Sstevel@tonic-gate 		    entry->hwc_devi_name, KM_SLEEP);
10287c478bd9Sstevel@tonic-gate 		spec->hwc_major = entry->hwc_major;
10297c478bd9Sstevel@tonic-gate 		spec->hwc_devi_sys_prop_ptr = i_ddi_prop_list_dup(
10307c478bd9Sstevel@tonic-gate 		    entry->hwc_devi_sys_prop_ptr, KM_SLEEP);
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 		*listp = spec;
10337c478bd9Sstevel@tonic-gate 		listp = &spec->hwc_next;
10347c478bd9Sstevel@tonic-gate 		entry = entry->hwc_hash_next;
10357c478bd9Sstevel@tonic-gate 	}
10367c478bd9Sstevel@tonic-gate }
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate /*
10397c478bd9Sstevel@tonic-gate  * Given a dip, find the list of child .conf specs from most specific
10407c478bd9Sstevel@tonic-gate  * (parent pathname) to least specific (class name).
10417c478bd9Sstevel@tonic-gate  *
10427c478bd9Sstevel@tonic-gate  * This function allows top-down loading to be implemented without
10437c478bd9Sstevel@tonic-gate  * changing the format of driver.conf file.
10447c478bd9Sstevel@tonic-gate  */
10457c478bd9Sstevel@tonic-gate struct hwc_spec *
hwc_get_child_spec(dev_info_t * dip,major_t match_major)10467c478bd9Sstevel@tonic-gate hwc_get_child_spec(dev_info_t *dip, major_t match_major)
10477c478bd9Sstevel@tonic-gate {
10487c478bd9Sstevel@tonic-gate 	extern char *i_ddi_parname(dev_info_t *, char *);
10497c478bd9Sstevel@tonic-gate 	extern int i_ddi_get_exported_classes(dev_info_t *, char ***);
10507c478bd9Sstevel@tonic-gate 	extern void i_ddi_free_exported_classes(char **, int);
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	int i, nclass;
10537c478bd9Sstevel@tonic-gate 	char **classes;
10547c478bd9Sstevel@tonic-gate 	struct hwc_spec *list = NULL;
10557c478bd9Sstevel@tonic-gate 	mod_hash_val_t val;
10567c478bd9Sstevel@tonic-gate 	char *parname, *parname_buf;
10577c478bd9Sstevel@tonic-gate 	char *deviname, *deviname_buf;
10587c478bd9Sstevel@tonic-gate 	char *pathname, *pathname_buf;
10597c478bd9Sstevel@tonic-gate 	char *bindname;
10607c478bd9Sstevel@tonic-gate 	char *drvname;
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 	pathname_buf = kmem_alloc(3 * MAXPATHLEN, KM_SLEEP);
10637c478bd9Sstevel@tonic-gate 	deviname_buf = pathname_buf + MAXPATHLEN;
10647c478bd9Sstevel@tonic-gate 	parname_buf = pathname_buf + (2 * MAXPATHLEN);
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	mutex_enter(&hwc_hash_lock);
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	/*
10697c478bd9Sstevel@tonic-gate 	 * Lookup based on full path.
10707c478bd9Sstevel@tonic-gate 	 * In the case of root node, ddi_pathname would return
10717c478bd9Sstevel@tonic-gate 	 * null string so just skip calling it.
10727c478bd9Sstevel@tonic-gate 	 * As the pathname always begins with /, no simpler
10737c478bd9Sstevel@tonic-gate 	 * name can duplicate it.
10747c478bd9Sstevel@tonic-gate 	 */
10757c478bd9Sstevel@tonic-gate 	pathname = (dip == ddi_root_node()) ? "/" :
1076e099bf07Scth 	    ddi_pathname(dip, pathname_buf);
10777c478bd9Sstevel@tonic-gate 	ASSERT(pathname != NULL);
10787c478bd9Sstevel@tonic-gate 	ASSERT(*pathname == '/');
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	if (mod_hash_find(hwc_par_hash, (mod_hash_key_t)pathname, &val) == 0) {
10817c478bd9Sstevel@tonic-gate 		hwc_spec_add(&list, (struct hwc_spec *)val, match_major);
10827c478bd9Sstevel@tonic-gate 	}
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	/*
10857c478bd9Sstevel@tonic-gate 	 * Lookup nodename@address.
10867c478bd9Sstevel@tonic-gate 	 * Note deviname cannot match pathname.
10877c478bd9Sstevel@tonic-gate 	 */
10887c478bd9Sstevel@tonic-gate 	deviname = ddi_deviname(dip, deviname_buf);
10897c478bd9Sstevel@tonic-gate 	if (*deviname != '\0') {
10907c478bd9Sstevel@tonic-gate 		/*
10917c478bd9Sstevel@tonic-gate 		 * Skip leading / returned by ddi_deviname.
10927c478bd9Sstevel@tonic-gate 		 */
10937c478bd9Sstevel@tonic-gate 		ASSERT(*deviname == '/');
10947c478bd9Sstevel@tonic-gate 		deviname++;
1095e099bf07Scth 		if ((*deviname != '\0') && (mod_hash_find(hwc_par_hash,
1096e099bf07Scth 		    (mod_hash_key_t)deviname, &val) == 0))
1097e099bf07Scth 			hwc_spec_add(&list,
1098e099bf07Scth 			    (struct hwc_spec *)val, match_major);
10997c478bd9Sstevel@tonic-gate 	}
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	/*
11027c478bd9Sstevel@tonic-gate 	 * Lookup bindingname@address.
11037c478bd9Sstevel@tonic-gate 	 * Take care not to perform duplicate lookups.
11047c478bd9Sstevel@tonic-gate 	 */
11057c478bd9Sstevel@tonic-gate 	parname = i_ddi_parname(dip, parname_buf);
11067c478bd9Sstevel@tonic-gate 	if (*parname != '\0') {
11077c478bd9Sstevel@tonic-gate 		ASSERT(*parname != '/');
11087c478bd9Sstevel@tonic-gate 		if ((strcmp(parname, deviname) != 0) &&
1109e099bf07Scth 		    (mod_hash_find(hwc_par_hash,
1110e099bf07Scth 		    (mod_hash_key_t)parname, &val) == 0)) {
1111e099bf07Scth 			hwc_spec_add(&list,
1112e099bf07Scth 			    (struct hwc_spec *)val, match_major);
11137c478bd9Sstevel@tonic-gate 		}
11147c478bd9Sstevel@tonic-gate 	}
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	/*
11177c478bd9Sstevel@tonic-gate 	 * Lookup driver binding name
11187c478bd9Sstevel@tonic-gate 	 */
11197c478bd9Sstevel@tonic-gate 	bindname = ddi_binding_name(dip);
11207c478bd9Sstevel@tonic-gate 	ASSERT(*bindname != '/');
11217c478bd9Sstevel@tonic-gate 	if ((strcmp(bindname, parname) != 0) &&
11227c478bd9Sstevel@tonic-gate 	    (strcmp(bindname, deviname) != 0) &&
11237c478bd9Sstevel@tonic-gate 	    (mod_hash_find(hwc_par_hash, (mod_hash_key_t)bindname, &val) == 0))
11247c478bd9Sstevel@tonic-gate 		hwc_spec_add(&list, (struct hwc_spec *)val, match_major);
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	/*
11277c478bd9Sstevel@tonic-gate 	 * Lookup driver name
11287c478bd9Sstevel@tonic-gate 	 */
11297c478bd9Sstevel@tonic-gate 	drvname = (char *)ddi_driver_name(dip);
11307c478bd9Sstevel@tonic-gate 	ASSERT(*drvname != '/');
11317c478bd9Sstevel@tonic-gate 	if ((strcmp(drvname, bindname) != 0) &&
11327c478bd9Sstevel@tonic-gate 	    (strcmp(drvname, parname) != 0) &&
11337c478bd9Sstevel@tonic-gate 	    (strcmp(drvname, deviname) != 0) &&
11347c478bd9Sstevel@tonic-gate 	    (mod_hash_find(hwc_par_hash, (mod_hash_key_t)drvname, &val) == 0))
11357c478bd9Sstevel@tonic-gate 		hwc_spec_add(&list, (struct hwc_spec *)val, match_major);
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 	kmem_free(pathname_buf, 3 * MAXPATHLEN);
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 	/*
11407c478bd9Sstevel@tonic-gate 	 * Lookup classes exported by this node and lookup the
11417c478bd9Sstevel@tonic-gate 	 * class hash table for all .conf specs
11427c478bd9Sstevel@tonic-gate 	 */
11437c478bd9Sstevel@tonic-gate 	nclass = i_ddi_get_exported_classes(dip, &classes);
11447c478bd9Sstevel@tonic-gate 	for (i = 0; i < nclass; i++) {
11457c478bd9Sstevel@tonic-gate 		if (mod_hash_find(hwc_class_hash, (mod_hash_key_t)classes[i],
11467c478bd9Sstevel@tonic-gate 		    &val) == 0)
11477c478bd9Sstevel@tonic-gate 			hwc_spec_add(&list, (struct hwc_spec *)val,
11487c478bd9Sstevel@tonic-gate 			    match_major);
11497c478bd9Sstevel@tonic-gate 	}
11507c478bd9Sstevel@tonic-gate 	i_ddi_free_exported_classes(classes, nclass);
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	mutex_exit(&hwc_hash_lock);
11537c478bd9Sstevel@tonic-gate 	return (list);
11547c478bd9Sstevel@tonic-gate }
1155