xref: /titanic_51/usr/src/uts/common/os/modsubr.c (revision c8742f6496e41bcf40b54b09b48835c42f91e574)
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 /*
22*c8742f64SJerry 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 *);
687c478bd9Sstevel@tonic-gate static int nm_hash(char *);
697c478bd9Sstevel@tonic-gate static void make_syscallname(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 
74c9cc1492SJerry Gilliam int
75*c8742f64SJerry Gilliam major_valid(major_t major)
76*c8742f64SJerry Gilliam {
77*c8742f64SJerry Gilliam 	return (major != DDI_MAJOR_T_NONE &&
78*c8742f64SJerry Gilliam 	    (major >= 0 && major < devcnt));
79*c8742f64SJerry Gilliam }
80*c8742f64SJerry Gilliam 
81*c8742f64SJerry Gilliam int
82c9cc1492SJerry Gilliam driver_installed(major_t major)
83c9cc1492SJerry Gilliam {
84*c8742f64SJerry Gilliam 	return (major_valid(major) && devnamesp[major].dn_name != NULL);
85*c8742f64SJerry Gilliam }
86*c8742f64SJerry Gilliam 
87*c8742f64SJerry Gilliam int
88*c8742f64SJerry Gilliam driver_active(major_t major)
89*c8742f64SJerry Gilliam {
90*c8742f64SJerry Gilliam 	return (driver_installed(major) && !(devnamesp[major].dn_flags &
91c9cc1492SJerry Gilliam 	    (DN_DRIVER_REMOVED|DN_DRIVER_INACTIVE)));
92c9cc1492SJerry Gilliam }
93c9cc1492SJerry Gilliam 
947c478bd9Sstevel@tonic-gate struct dev_ops *
957c478bd9Sstevel@tonic-gate mod_hold_dev_by_major(major_t major)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	struct dev_ops **devopspp, *ops;
987c478bd9Sstevel@tonic-gate 	int loaded;
997c478bd9Sstevel@tonic-gate 	char *drvname;
1007c478bd9Sstevel@tonic-gate 
101*c8742f64SJerry Gilliam 	if (!driver_active(major))
1027c478bd9Sstevel@tonic-gate 		return (NULL);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&(devnamesp[major].dn_lock));
1057c478bd9Sstevel@tonic-gate 	devopspp = &devopsp[major];
1067c478bd9Sstevel@tonic-gate 	loaded = 1;
1077c478bd9Sstevel@tonic-gate 	while (loaded && !CB_DRV_INSTALLED(*devopspp)) {
1087c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&(devnamesp[major].dn_lock));
1097c478bd9Sstevel@tonic-gate 		drvname = mod_major_to_name(major);
1107c478bd9Sstevel@tonic-gate 		if (drvname == NULL)
1117c478bd9Sstevel@tonic-gate 			return (NULL);
1127c478bd9Sstevel@tonic-gate 		loaded = (modload("drv", drvname) != -1);
1137c478bd9Sstevel@tonic-gate 		LOCK_DEV_OPS(&(devnamesp[major].dn_lock));
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 	if (loaded) {
1167c478bd9Sstevel@tonic-gate 		INCR_DEV_OPS_REF(*devopspp);
1177c478bd9Sstevel@tonic-gate 		ops = *devopspp;
1187c478bd9Sstevel@tonic-gate 	} else {
1197c478bd9Sstevel@tonic-gate 		ops = NULL;
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(devnamesp[major].dn_lock));
1227c478bd9Sstevel@tonic-gate 	return (ops);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #ifdef	DEBUG_RELE
1267c478bd9Sstevel@tonic-gate static int mod_rele_pause = DEBUG_RELE;
1277c478bd9Sstevel@tonic-gate #endif	/* DEBUG_RELE */
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate void
1307c478bd9Sstevel@tonic-gate mod_rele_dev_by_major(major_t major)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	struct dev_ops *ops;
1337c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
1347c478bd9Sstevel@tonic-gate 
135*c8742f64SJerry Gilliam 	if (!driver_active(major))
1367c478bd9Sstevel@tonic-gate 		return;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
1397c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
1407c478bd9Sstevel@tonic-gate 	ops = devopsp[major];
1417c478bd9Sstevel@tonic-gate 	ASSERT(CB_DRV_INSTALLED(ops));
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate #ifdef	DEBUG_RELE
1447c478bd9Sstevel@tonic-gate 	if (!DEV_OPS_HELD(ops))  {
1457c478bd9Sstevel@tonic-gate 		char *s;
1467c478bd9Sstevel@tonic-gate 		static char *msg = "mod_rele_dev_by_major: unheld driver!";
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 		printf("mod_rele_dev_by_major: Major dev <%u>, name <%s>\n",
1497c478bd9Sstevel@tonic-gate 		    (uint_t)major,
1507c478bd9Sstevel@tonic-gate 		    (s = mod_major_to_name(major)) ? s : "unknown");
1517c478bd9Sstevel@tonic-gate 		if (mod_rele_pause)
1527c478bd9Sstevel@tonic-gate 			debug_enter(msg);
1537c478bd9Sstevel@tonic-gate 		else
1547c478bd9Sstevel@tonic-gate 			printf("%s\n", msg);
1557c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
1567c478bd9Sstevel@tonic-gate 		return;			/* XXX: Note changed behavior */
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate #endif	/* DEBUG_RELE */
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	if (!DEV_OPS_HELD(ops)) {
1627c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC,
1637c478bd9Sstevel@tonic-gate 		    "mod_rele_dev_by_major: Unheld driver: major number <%u>",
1647c478bd9Sstevel@tonic-gate 		    (uint_t)major);
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 	DECR_DEV_OPS_REF(ops);
1677c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
1687c478bd9Sstevel@tonic-gate }
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate struct dev_ops *
1717c478bd9Sstevel@tonic-gate mod_hold_dev_by_devi(dev_info_t *devi)
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate 	major_t major;
1747c478bd9Sstevel@tonic-gate 	char *name;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	name = ddi_get_name(devi);
177a204de77Scth 	if ((major = mod_name_to_major(name)) == DDI_MAJOR_T_NONE)
1787c478bd9Sstevel@tonic-gate 		return (NULL);
1797c478bd9Sstevel@tonic-gate 	return (mod_hold_dev_by_major(major));
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate void
1837c478bd9Sstevel@tonic-gate mod_rele_dev_by_devi(dev_info_t *devi)
1847c478bd9Sstevel@tonic-gate {
1857c478bd9Sstevel@tonic-gate 	major_t major;
1867c478bd9Sstevel@tonic-gate 	char *name;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	name = ddi_get_name(devi);
189a204de77Scth 	if ((major = mod_name_to_major(name)) == DDI_MAJOR_T_NONE)
1907c478bd9Sstevel@tonic-gate 		return;
1917c478bd9Sstevel@tonic-gate 	mod_rele_dev_by_major(major);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate int
1957c478bd9Sstevel@tonic-gate nomod_zero()
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	return (0);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate int
2017c478bd9Sstevel@tonic-gate nomod_minus_one()
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	return (-1);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate int
2077c478bd9Sstevel@tonic-gate nomod_einval()
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	return (EINVAL);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate void
2137c478bd9Sstevel@tonic-gate nomod_void()
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	/* nothing */
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate  * Install all the stubs for a module.
2207c478bd9Sstevel@tonic-gate  * Return zero if there were no errors or an errno value.
2217c478bd9Sstevel@tonic-gate  */
2227c478bd9Sstevel@tonic-gate int
2237c478bd9Sstevel@tonic-gate install_stubs_by_name(struct modctl *modp, char *name)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	char *p;
2267c478bd9Sstevel@tonic-gate 	char *filenamep;
2277c478bd9Sstevel@tonic-gate 	char namebuf[MODMAXNAMELEN + 12];
2287c478bd9Sstevel@tonic-gate 	struct mod_modinfo *mp;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	p = name;
2317c478bd9Sstevel@tonic-gate 	filenamep = name;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	while (*p)
2347c478bd9Sstevel@tonic-gate 		if (*p++ == '/')
2357c478bd9Sstevel@tonic-gate 			filenamep = p;
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
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",
2837c478bd9Sstevel@tonic-gate 			    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
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
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
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 *
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 *
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 
4297c478bd9Sstevel@tonic-gate 	dnp->dn_flags |= DN_CONF_PARSED;
4307c478bd9Sstevel@tonic-gate 	dnp->dn_pl = pl;
4317c478bd9Sstevel@tonic-gate 	return (pl);
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate /*
4357c478bd9Sstevel@tonic-gate  * Destroy driver.conf info in devnames array for a driver
4367c478bd9Sstevel@tonic-gate  */
4377c478bd9Sstevel@tonic-gate int
4387c478bd9Sstevel@tonic-gate impl_free_parlist(major_t major)
4397c478bd9Sstevel@tonic-gate {
4407c478bd9Sstevel@tonic-gate 	struct par_list *pl;
4417c478bd9Sstevel@tonic-gate 	struct devnames *dnp = &devnamesp[major];
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	/*
4447c478bd9Sstevel@tonic-gate 	 * Unref driver global property list. Don't destroy it
4457c478bd9Sstevel@tonic-gate 	 * because some instances may still be referencing it.
4467c478bd9Sstevel@tonic-gate 	 * The property list will be freed when the last ref
4477c478bd9Sstevel@tonic-gate 	 * goes away.
4487c478bd9Sstevel@tonic-gate 	 */
4497c478bd9Sstevel@tonic-gate 	if (dnp->dn_global_prop_ptr) {
4507c478bd9Sstevel@tonic-gate 		i_ddi_prop_list_rele(dnp->dn_global_prop_ptr, dnp);
4517c478bd9Sstevel@tonic-gate 		dnp->dn_global_prop_ptr = NULL;
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	/*
4557c478bd9Sstevel@tonic-gate 	 * remove specs from hash table
4567c478bd9Sstevel@tonic-gate 	 */
4577c478bd9Sstevel@tonic-gate 	for (pl = dnp->dn_pl; pl; pl = pl->par_next)
4587c478bd9Sstevel@tonic-gate 		hwc_unhash(pl->par_specs);
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	impl_delete_par_list(dnp->dn_pl);
4617c478bd9Sstevel@tonic-gate 	dnp->dn_pl = NULL;
4627c478bd9Sstevel@tonic-gate 	dnp->dn_flags &= ~DN_CONF_PARSED;
4637c478bd9Sstevel@tonic-gate 	return (0);
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate struct bind *mb_hashtab[MOD_BIND_HASHSIZE];
4677c478bd9Sstevel@tonic-gate struct bind *sb_hashtab[MOD_BIND_HASHSIZE];
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate static int
4707c478bd9Sstevel@tonic-gate nm_hash(char *name)
4717c478bd9Sstevel@tonic-gate {
4727c478bd9Sstevel@tonic-gate 	char c;
4737c478bd9Sstevel@tonic-gate 	int hash = 0;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	for (c = *name++; c; c = *name++)
4767c478bd9Sstevel@tonic-gate 		hash ^= c;
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	return (hash & MOD_BIND_HASHMASK);
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate void
4827c478bd9Sstevel@tonic-gate clear_binding_hash(struct bind **bhash)
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate 	int i;
4857c478bd9Sstevel@tonic-gate 	struct bind *bp, *bp1;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	for (i = 0; i < MOD_BIND_HASHSIZE; i++) {
4887c478bd9Sstevel@tonic-gate 		bp = bhash[i];
4897c478bd9Sstevel@tonic-gate 		while (bp != NULL) {
4907c478bd9Sstevel@tonic-gate 			kmem_free(bp->b_name, strlen(bp->b_name) + 1);
4917c478bd9Sstevel@tonic-gate 			if (bp->b_bind_name) {
4927c478bd9Sstevel@tonic-gate 				kmem_free(bp->b_bind_name,
4937c478bd9Sstevel@tonic-gate 				    strlen(bp->b_bind_name) + 1);
4947c478bd9Sstevel@tonic-gate 			}
4957c478bd9Sstevel@tonic-gate 			bp1 = bp;
4967c478bd9Sstevel@tonic-gate 			bp = bp->b_next;
4977c478bd9Sstevel@tonic-gate 			kmem_free(bp1, sizeof (struct bind));
4987c478bd9Sstevel@tonic-gate 		}
4997c478bd9Sstevel@tonic-gate 		bhash[i] = NULL;
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5036532b960SJerry Gilliam /* Find an mbind by name match (caller can ask for deleted match) */
5047c478bd9Sstevel@tonic-gate static struct bind *
5056532b960SJerry Gilliam find_mbind(char *name, struct bind **hashtab, int deleted)
5067c478bd9Sstevel@tonic-gate {
5077c478bd9Sstevel@tonic-gate 	struct bind	*mb;
5087c478bd9Sstevel@tonic-gate 
5096532b960SJerry Gilliam 	for (mb = hashtab[nm_hash(name)]; mb; mb = mb->b_next) {
5106532b960SJerry Gilliam 		if (deleted && (mb->b_num >= 0))
5116532b960SJerry Gilliam 			continue;			/* skip active */
5126532b960SJerry Gilliam 		if (!deleted && (mb->b_num < 0))
5136532b960SJerry Gilliam 			continue;			/* skip deleted */
5146532b960SJerry Gilliam 
5156532b960SJerry Gilliam 		/* return if name matches */
5166532b960SJerry Gilliam 		if (strcmp(name, mb->b_name) == 0) {
5177c478bd9Sstevel@tonic-gate 			break;
5187c478bd9Sstevel@tonic-gate 		}
5196532b960SJerry Gilliam 	}
5207c478bd9Sstevel@tonic-gate 	return (mb);
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate /*
5247c478bd9Sstevel@tonic-gate  * Create an entry for the given (name, major, bind_name) tuple in the
5257c478bd9Sstevel@tonic-gate  * hash table supplied.  Reject the attempt to do so if 'name' is already
5267c478bd9Sstevel@tonic-gate  * in the hash table.
5277c478bd9Sstevel@tonic-gate  *
5287c478bd9Sstevel@tonic-gate  * Does not provide synchronization, so use only during boot or with
5297c478bd9Sstevel@tonic-gate  * externally provided locking.
5307c478bd9Sstevel@tonic-gate  */
5317c478bd9Sstevel@tonic-gate int
5326532b960SJerry Gilliam make_mbind(char *name, int num, char *bind_name, struct bind **hashtab)
5337c478bd9Sstevel@tonic-gate {
5346532b960SJerry Gilliam 	struct bind	*mb;
5356532b960SJerry Gilliam 	struct bind	**pmb;
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	ASSERT(hashtab != NULL);
5386532b960SJerry Gilliam 	ASSERT(num >= 0);
5397c478bd9Sstevel@tonic-gate 
5406532b960SJerry Gilliam 	/* Fail if the key being added is already established */
5416532b960SJerry Gilliam 	if (find_mbind(name, hashtab, 0) != NULL)
5427c478bd9Sstevel@tonic-gate 		return (-1);
5437c478bd9Sstevel@tonic-gate 
5446532b960SJerry Gilliam 	/* Allocate new mbind */
5456532b960SJerry Gilliam 	mb = kmem_zalloc(sizeof (struct bind), KM_SLEEP);
5466532b960SJerry Gilliam 	mb->b_name = i_ddi_strdup(name, KM_SLEEP);
5476532b960SJerry Gilliam 	mb->b_num = num;
5486532b960SJerry Gilliam 	if (bind_name != NULL)
5496532b960SJerry Gilliam 		mb->b_bind_name = i_ddi_strdup(bind_name, KM_SLEEP);
5507c478bd9Sstevel@tonic-gate 
5516532b960SJerry Gilliam 	/* Insert at head of hash */
5526532b960SJerry Gilliam 	pmb = &hashtab[nm_hash(name)];
5536532b960SJerry Gilliam 	mb->b_next = *pmb;
5546532b960SJerry Gilliam 	*pmb = mb;
5557c478bd9Sstevel@tonic-gate 	return (0);
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate /*
5596532b960SJerry Gilliam  * Delete a binding from a binding-hash. Since there is no locking we
5606532b960SJerry Gilliam  * delete an mbind by making its b_num negative. We also support find_mbind
5616532b960SJerry Gilliam  * of deleted entries, so we still need deleted items on the list.
5627c478bd9Sstevel@tonic-gate  */
5637c478bd9Sstevel@tonic-gate void
5647c478bd9Sstevel@tonic-gate delete_mbind(char *name, struct bind **hashtab)
5657c478bd9Sstevel@tonic-gate {
5666532b960SJerry Gilliam 	struct bind	*mb;
5677c478bd9Sstevel@tonic-gate 
5686532b960SJerry Gilliam 	for (mb = hashtab[nm_hash(name)]; mb; mb = mb->b_next) {
5696532b960SJerry Gilliam 		if ((mb->b_num >= 0) && (strcmp(name, mb->b_name) == 0)) {
5706532b960SJerry Gilliam 			/* delete by making b_num negative */
5716532b960SJerry Gilliam 			if (moddebug & MODDEBUG_BINDING) {
5726532b960SJerry Gilliam 				cmn_err(CE_CONT, "mbind: %s %d deleted\n",
5736532b960SJerry Gilliam 				    name, mb->b_num);
5746532b960SJerry Gilliam 			}
5756532b960SJerry Gilliam 			mb->b_num = -mb->b_num;
5767c478bd9Sstevel@tonic-gate 			break;
5777c478bd9Sstevel@tonic-gate 		}
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate 
5816532b960SJerry Gilliam /*
5826532b960SJerry Gilliam  * Delete all items in an mbind associated with specified num.
5836532b960SJerry Gilliam  * An example would be rem_drv deleting all aliases associated with a
5846532b960SJerry Gilliam  * driver major number.
5856532b960SJerry Gilliam  */
5866532b960SJerry Gilliam void
5876532b960SJerry Gilliam purge_mbind(int num, struct bind **hashtab)
5886532b960SJerry Gilliam {
5896532b960SJerry Gilliam 	int		i;
5906532b960SJerry Gilliam 	struct bind	*mb;
5917c478bd9Sstevel@tonic-gate 
5926532b960SJerry Gilliam 	/* search all hash lists for items that associated with 'num' */
5936532b960SJerry Gilliam 	for (i = 0; i < MOD_BIND_HASHSIZE; i++) {
5946532b960SJerry Gilliam 		for (mb = hashtab[i]; mb; mb = mb->b_next) {
5956532b960SJerry Gilliam 			if (mb->b_num == num) {
5966532b960SJerry Gilliam 				if (moddebug & MODDEBUG_BINDING)
5976532b960SJerry Gilliam 					cmn_err(CE_CONT,
5986532b960SJerry Gilliam 					    "mbind: %s %d purged\n",
5996532b960SJerry Gilliam 					    mb->b_name, num);
6006532b960SJerry Gilliam 				/* purge by changing the sign */
6016532b960SJerry Gilliam 				mb->b_num = -num;
6026532b960SJerry Gilliam 			}
6036532b960SJerry Gilliam 		}
6046532b960SJerry Gilliam 	}
6056532b960SJerry Gilliam }
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate major_t
6087c478bd9Sstevel@tonic-gate mod_name_to_major(char *name)
6097c478bd9Sstevel@tonic-gate {
6107c478bd9Sstevel@tonic-gate 	struct bind	*mbind;
6116532b960SJerry Gilliam 	major_t		maj;
6127c478bd9Sstevel@tonic-gate 
6136532b960SJerry Gilliam 	/* Search for non-deleted match. */
6146532b960SJerry Gilliam 	if ((mbind = find_mbind(name, mb_hashtab, 0)) != NULL) {
6156532b960SJerry Gilliam 		if (moddebug & MODDEBUG_BINDING) {
6166532b960SJerry Gilliam 			if (find_mbind(name, mb_hashtab, 1))
6176532b960SJerry Gilliam 				cmn_err(CE_CONT,
6186532b960SJerry Gilliam 				    "'%s' has deleted match too\n", name);
6196532b960SJerry Gilliam 		}
6207c478bd9Sstevel@tonic-gate 		return ((major_t)mbind->b_num);
6216532b960SJerry Gilliam 	}
6226532b960SJerry Gilliam 
6236532b960SJerry Gilliam 	/*
6246532b960SJerry Gilliam 	 * Search for deleted match: We may find that we have dependencies
6256532b960SJerry Gilliam 	 * on drivers that have been deleted (but the old driver may still
6266532b960SJerry Gilliam 	 * be bound to a node). These callers should be converted to use
6276532b960SJerry Gilliam 	 * ddi_driver_major(i.e. devi_major).
6286532b960SJerry Gilliam 	 */
6296532b960SJerry Gilliam 	if (moddebug & MODDEBUG_BINDING) {
6306532b960SJerry Gilliam 		if ((mbind = find_mbind(name, mb_hashtab, 1)) != NULL) {
6316532b960SJerry Gilliam 			maj = (major_t)(-(mbind->b_num));
6326532b960SJerry Gilliam 			cmn_err(CE_CONT, "Reference to deleted alias '%s' %d\n",
6336532b960SJerry Gilliam 			    name, maj);
6346532b960SJerry Gilliam 		}
6356532b960SJerry Gilliam 	}
6367c478bd9Sstevel@tonic-gate 
637a204de77Scth 	return (DDI_MAJOR_T_NONE);
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate char *
6417c478bd9Sstevel@tonic-gate mod_major_to_name(major_t major)
6427c478bd9Sstevel@tonic-gate {
643c9cc1492SJerry Gilliam 	if (!driver_installed(major))
6447c478bd9Sstevel@tonic-gate 		return (NULL);
6457c478bd9Sstevel@tonic-gate 	return ((&devnamesp[major])->dn_name);
6467c478bd9Sstevel@tonic-gate }
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate /*
6497c478bd9Sstevel@tonic-gate  * Set up the devnames array.  Error check for duplicate entries.
6507c478bd9Sstevel@tonic-gate  */
6517c478bd9Sstevel@tonic-gate void
6527c478bd9Sstevel@tonic-gate init_devnamesp(int size)
6537c478bd9Sstevel@tonic-gate {
6547c478bd9Sstevel@tonic-gate 	int hshndx;
6557c478bd9Sstevel@tonic-gate 	struct bind *bp;
6567c478bd9Sstevel@tonic-gate 	static char dupwarn[] =
6577c478bd9Sstevel@tonic-gate 	    "!Device entry \"%s %d\" conflicts with previous entry \"%s %d\" "
6587c478bd9Sstevel@tonic-gate 	    "in /etc/name_to_major.";
6597c478bd9Sstevel@tonic-gate 	static char badmaj[] = "The major number %u is invalid.";
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	ASSERT(size <= L_MAXMAJ32 && size > 0);
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	/*
6647c478bd9Sstevel@tonic-gate 	 * Allocate the devnames array.  All mutexes and cv's will be
6657c478bd9Sstevel@tonic-gate 	 * automagically initialized.
6667c478bd9Sstevel@tonic-gate 	 */
6677c478bd9Sstevel@tonic-gate 	devnamesp = kobj_zalloc(size * sizeof (struct devnames), KM_SLEEP);
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	/*
6707c478bd9Sstevel@tonic-gate 	 * Stick the contents of mb_hashtab into the devnames array.  Warn if
6717c478bd9Sstevel@tonic-gate 	 * two hash entries correspond to the same major number, or if a
6727c478bd9Sstevel@tonic-gate 	 * major number is out of range.
6737c478bd9Sstevel@tonic-gate 	 */
6747c478bd9Sstevel@tonic-gate 	for (hshndx = 0; hshndx < MOD_BIND_HASHSIZE; hshndx++) {
6757c478bd9Sstevel@tonic-gate 		for (bp = mb_hashtab[hshndx]; bp; bp = bp->b_next) {
676c9cc1492SJerry Gilliam 			if (make_devname(bp->b_name,
677c9cc1492SJerry Gilliam 			    (major_t)bp->b_num, 0) != 0) {
6787c478bd9Sstevel@tonic-gate 				/*
6797c478bd9Sstevel@tonic-gate 				 * If there is not an entry at b_num already,
6807c478bd9Sstevel@tonic-gate 				 * then this must be a bad major number.
6817c478bd9Sstevel@tonic-gate 				 */
6827c478bd9Sstevel@tonic-gate 				char *nm = mod_major_to_name(bp->b_num);
6837c478bd9Sstevel@tonic-gate 				if (nm == NULL) {
6847c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, badmaj,
6857c478bd9Sstevel@tonic-gate 					    (uint_t)bp->b_num);
6867c478bd9Sstevel@tonic-gate 				} else {
6877c478bd9Sstevel@tonic-gate 					cmn_err(CE_WARN, dupwarn, bp->b_name,
6887c478bd9Sstevel@tonic-gate 					    bp->b_num, nm, bp->b_num);
6897c478bd9Sstevel@tonic-gate 				}
6907c478bd9Sstevel@tonic-gate 			}
6917c478bd9Sstevel@tonic-gate 		}
6927c478bd9Sstevel@tonic-gate 	}
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	/* Initialize hash table for hwc_spec's */
6957c478bd9Sstevel@tonic-gate 	hwc_hash_init();
6967c478bd9Sstevel@tonic-gate }
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate int
699c9cc1492SJerry Gilliam make_devname(char *name, major_t major, int dn_flags)
7007c478bd9Sstevel@tonic-gate {
7017c478bd9Sstevel@tonic-gate 	struct devnames *dnp;
7027c478bd9Sstevel@tonic-gate 	char *copy;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	/*
7057c478bd9Sstevel@tonic-gate 	 * Until on-disk support for major nums > 14 bits arrives, fail
7067c478bd9Sstevel@tonic-gate 	 * any major numbers that are too big.
7077c478bd9Sstevel@tonic-gate 	 */
7087c478bd9Sstevel@tonic-gate 	if (major > L_MAXMAJ32)
7097c478bd9Sstevel@tonic-gate 		return (EINVAL);
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	dnp = &devnamesp[major];
7127c478bd9Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
7137c478bd9Sstevel@tonic-gate 	if (dnp->dn_name) {
7147c478bd9Sstevel@tonic-gate 		if (strcmp(dnp->dn_name, name) != 0) {
7157c478bd9Sstevel@tonic-gate 			/* Another driver already here */
7167c478bd9Sstevel@tonic-gate 			UNLOCK_DEV_OPS(&dnp->dn_lock);
7177c478bd9Sstevel@tonic-gate 			return (EINVAL);
7187c478bd9Sstevel@tonic-gate 		}
7197c478bd9Sstevel@tonic-gate 		/* Adding back a removed driver */
7207c478bd9Sstevel@tonic-gate 		dnp->dn_flags &= ~DN_DRIVER_REMOVED;
721c9cc1492SJerry Gilliam 		dnp->dn_flags |= dn_flags;
7227c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
7237c478bd9Sstevel@tonic-gate 		return (0);
7247c478bd9Sstevel@tonic-gate 	}
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	/*
7277c478bd9Sstevel@tonic-gate 	 * Check if flag is taken by getudev()
7287c478bd9Sstevel@tonic-gate 	 */
7297c478bd9Sstevel@tonic-gate 	if (dnp->dn_flags & DN_TAKEN_GETUDEV) {
7307c478bd9Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
7317c478bd9Sstevel@tonic-gate 		return (EINVAL);
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	copy = kmem_alloc(strlen(name) + 1, KM_SLEEP);
7357c478bd9Sstevel@tonic-gate 	(void) strcpy(copy, name);
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	/* Make sure string is copied before setting dn_name */
7387c478bd9Sstevel@tonic-gate 	membar_producer();
7397c478bd9Sstevel@tonic-gate 	dnp->dn_name = copy;
740c9cc1492SJerry Gilliam 	dnp->dn_flags = dn_flags;
7417c478bd9Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7427c478bd9Sstevel@tonic-gate 	return (0);
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate /*
7467c478bd9Sstevel@tonic-gate  * Set up the syscallnames array.
7477c478bd9Sstevel@tonic-gate  */
7487c478bd9Sstevel@tonic-gate void
7497c478bd9Sstevel@tonic-gate init_syscallnames(int size)
7507c478bd9Sstevel@tonic-gate {
7517c478bd9Sstevel@tonic-gate 	int hshndx;
7527c478bd9Sstevel@tonic-gate 	struct bind *bp;
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	syscallnames = kobj_zalloc(size * sizeof (char *), KM_SLEEP);
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	for (hshndx = 0; hshndx < MOD_BIND_HASHSIZE; hshndx++) {
7577c478bd9Sstevel@tonic-gate 		for (bp = sb_hashtab[hshndx]; bp; bp = bp->b_next) {
7587730e2acScg13442 			if (bp->b_num < 0 || bp->b_num >= size) {
7597730e2acScg13442 				cmn_err(CE_WARN,
7607730e2acScg13442 				    "!Couldn't add system call \"%s %d\". "
7617730e2acScg13442 				    "Value out of range (0..%d) in "
7627730e2acScg13442 				    "/etc/name_to_sysnum.",
7637730e2acScg13442 				    bp->b_name, bp->b_num, size - 1);
7647730e2acScg13442 				continue;
7657730e2acScg13442 			}
7667c478bd9Sstevel@tonic-gate 			make_syscallname(bp->b_name, bp->b_num);
7677c478bd9Sstevel@tonic-gate 		}
7687c478bd9Sstevel@tonic-gate 	}
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate static void
7727c478bd9Sstevel@tonic-gate make_syscallname(char *name, int sysno)
7737c478bd9Sstevel@tonic-gate {
7747c478bd9Sstevel@tonic-gate 	char **cp = &syscallnames[sysno];
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	if (*cp != NULL) {
7777c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "!Couldn't add system call \"%s %d\". "
7787c478bd9Sstevel@tonic-gate 		    "It conflicts with \"%s %d\" in /etc/name_to_sysnum.",
7797c478bd9Sstevel@tonic-gate 		    name, sysno, *cp, sysno);
7807c478bd9Sstevel@tonic-gate 		return;
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 	*cp = kmem_alloc(strlen(name) + 1, KM_SLEEP);
7837c478bd9Sstevel@tonic-gate 	(void) strcpy(*cp, name);
7847c478bd9Sstevel@tonic-gate }
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate /*
7877c478bd9Sstevel@tonic-gate  * Given a system call name, get its number.
7887c478bd9Sstevel@tonic-gate  */
7897c478bd9Sstevel@tonic-gate int
7907c478bd9Sstevel@tonic-gate mod_getsysnum(char *name)
7917c478bd9Sstevel@tonic-gate {
7927c478bd9Sstevel@tonic-gate 	struct bind *mbind;
7937c478bd9Sstevel@tonic-gate 
7946532b960SJerry Gilliam 	if ((mbind = find_mbind(name, sb_hashtab, 0)) != NULL)
7957c478bd9Sstevel@tonic-gate 		return (mbind->b_num);
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	return (-1);
7987c478bd9Sstevel@tonic-gate }
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate /*
8017c478bd9Sstevel@tonic-gate  * Given a system call number, get the system call name.
8027c478bd9Sstevel@tonic-gate  */
8037c478bd9Sstevel@tonic-gate char *
8047c478bd9Sstevel@tonic-gate mod_getsysname(int sysnum)
8057c478bd9Sstevel@tonic-gate {
8067c478bd9Sstevel@tonic-gate 	return (syscallnames[sysnum]);
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate /*
8107c478bd9Sstevel@tonic-gate  * Find the name of the module containing the specified pc.
8117c478bd9Sstevel@tonic-gate  * Returns the name on success, "<unknown>" on failure.
8127c478bd9Sstevel@tonic-gate  * No mod_lock locking is required because things are never deleted from
8137c478bd9Sstevel@tonic-gate  * the &modules list.
8147c478bd9Sstevel@tonic-gate  */
8157c478bd9Sstevel@tonic-gate char *
8167c478bd9Sstevel@tonic-gate mod_containing_pc(caddr_t pc)
8177c478bd9Sstevel@tonic-gate {
8187c478bd9Sstevel@tonic-gate 	struct modctl	*mcp = &modules;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	do {
8217c478bd9Sstevel@tonic-gate 		if (mcp->mod_mp != NULL &&
8227c478bd9Sstevel@tonic-gate 		    (size_t)pc - (size_t)mcp->mod_text < mcp->mod_text_size)
8237c478bd9Sstevel@tonic-gate 			return (mcp->mod_modname);
8247c478bd9Sstevel@tonic-gate 	} while ((mcp = mcp->mod_next) != &modules);
8257c478bd9Sstevel@tonic-gate 	return ("<unknown>");
8267c478bd9Sstevel@tonic-gate }
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate /*
8297c478bd9Sstevel@tonic-gate  * Hash tables for hwc_spec
8307c478bd9Sstevel@tonic-gate  *
8317c478bd9Sstevel@tonic-gate  * The purpose of these hash tables are to allow the framework to discover
8327c478bd9Sstevel@tonic-gate  * all possible .conf children for a given nexus. There are two hash tables.
8337c478bd9Sstevel@tonic-gate  * One is hashed based on parent name, the on the class name. Each
8347c478bd9Sstevel@tonic-gate  * driver.conf file translates to a list of hwc_spec's. Adding and
8357c478bd9Sstevel@tonic-gate  * removing the entire list is an atomic operation, protected by
8367c478bd9Sstevel@tonic-gate  * the hwc_hash_lock.
8377c478bd9Sstevel@tonic-gate  *
8387c478bd9Sstevel@tonic-gate  * What we get from all the hashing is the function hwc_get_child_spec().
8397c478bd9Sstevel@tonic-gate  */
8407c478bd9Sstevel@tonic-gate #define	HWC_SPEC_HASHSIZE	(1 << 6)	/* 64 */
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate static mod_hash_t *hwc_par_hash;	/* hash by parent name */
8437c478bd9Sstevel@tonic-gate static mod_hash_t *hwc_class_hash;	/* hash by class name */
8447c478bd9Sstevel@tonic-gate static kmutex_t hwc_hash_lock;		/* lock protecting hwc hashes */
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate /*
8477c478bd9Sstevel@tonic-gate  * Initialize hash tables for parent and class specs
8487c478bd9Sstevel@tonic-gate  */
8497c478bd9Sstevel@tonic-gate static void
8507c478bd9Sstevel@tonic-gate hwc_hash_init()
8517c478bd9Sstevel@tonic-gate {
8527c478bd9Sstevel@tonic-gate 	hwc_par_hash = mod_hash_create_strhash("hwc parent spec hash",
8537c478bd9Sstevel@tonic-gate 	    HWC_SPEC_HASHSIZE, mod_hash_null_valdtor);
8547c478bd9Sstevel@tonic-gate 	hwc_class_hash = mod_hash_create_strhash("hwc class spec hash",
8557c478bd9Sstevel@tonic-gate 	    HWC_SPEC_HASHSIZE, mod_hash_null_valdtor);
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate /*
8597c478bd9Sstevel@tonic-gate  * Insert a spec into hash table. hwc_hash_lock must be held
8607c478bd9Sstevel@tonic-gate  */
8617c478bd9Sstevel@tonic-gate static void
8627c478bd9Sstevel@tonic-gate hwc_hash_insert(struct hwc_spec *spec, char *name, mod_hash_t *hash)
8637c478bd9Sstevel@tonic-gate {
8647c478bd9Sstevel@tonic-gate 	mod_hash_key_t key;
8657c478bd9Sstevel@tonic-gate 	struct hwc_spec *entry = NULL;
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	ASSERT(name != NULL);
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	if (mod_hash_find(hash, (mod_hash_key_t)name,
8707c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)&entry) != 0) {
8717c478bd9Sstevel@tonic-gate 		/* Name doesn't exist, insert a new key */
8727c478bd9Sstevel@tonic-gate 		key = kmem_alloc(strlen(name) + 1, KM_SLEEP);
8737c478bd9Sstevel@tonic-gate 		(void) strcpy((char *)key, name);
8747c478bd9Sstevel@tonic-gate 		if (mod_hash_insert(hash, key, (mod_hash_val_t)spec) != 0) {
8757c478bd9Sstevel@tonic-gate 			kmem_free(key, strlen(name) + 1);
8767c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN, "hwc hash state inconsistent");
8777c478bd9Sstevel@tonic-gate 		}
8787c478bd9Sstevel@tonic-gate 		return;
8797c478bd9Sstevel@tonic-gate 	}
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	/*
8827c478bd9Sstevel@tonic-gate 	 * Name is already present, append spec to the list.
8837c478bd9Sstevel@tonic-gate 	 * This is the case when driver.conf specifies multiple
8847c478bd9Sstevel@tonic-gate 	 * nodes under a single parent or class.
8857c478bd9Sstevel@tonic-gate 	 */
8867c478bd9Sstevel@tonic-gate 	while (entry->hwc_hash_next)
8877c478bd9Sstevel@tonic-gate 		entry = entry->hwc_hash_next;
8887c478bd9Sstevel@tonic-gate 	entry->hwc_hash_next = spec;
8897c478bd9Sstevel@tonic-gate }
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate /*
8927c478bd9Sstevel@tonic-gate  * Remove a spec entry from spec hash table, the spec itself is
8937c478bd9Sstevel@tonic-gate  * destroyed external to this function.
8947c478bd9Sstevel@tonic-gate  */
8957c478bd9Sstevel@tonic-gate static void
8967c478bd9Sstevel@tonic-gate hwc_hash_remove(struct hwc_spec *spec, char *name, mod_hash_t *hash)
8977c478bd9Sstevel@tonic-gate {
8987c478bd9Sstevel@tonic-gate 	char *key;
8997c478bd9Sstevel@tonic-gate 	struct hwc_spec *entry;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	ASSERT(name != NULL);
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	if (mod_hash_find(hash, (mod_hash_key_t)name,
9047c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)&entry) != 0) {
9057c478bd9Sstevel@tonic-gate 		return;	/* name not found in hash */
9067c478bd9Sstevel@tonic-gate 	}
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	/*
9097c478bd9Sstevel@tonic-gate 	 * If the head is the spec to be removed, either destroy the
9107c478bd9Sstevel@tonic-gate 	 * entry or replace it with the remaining list.
9117c478bd9Sstevel@tonic-gate 	 */
9127c478bd9Sstevel@tonic-gate 	if (entry == spec) {
9137c478bd9Sstevel@tonic-gate 		if (spec->hwc_hash_next == NULL) {
9147c478bd9Sstevel@tonic-gate 			(void) mod_hash_destroy(hash, (mod_hash_key_t)name);
9157c478bd9Sstevel@tonic-gate 			return;
9167c478bd9Sstevel@tonic-gate 		}
9177c478bd9Sstevel@tonic-gate 		key = kmem_alloc(strlen(name) + 1, KM_SLEEP);
9187c478bd9Sstevel@tonic-gate 		(void) strcpy(key, name);
9197c478bd9Sstevel@tonic-gate 		(void) mod_hash_replace(hash, (mod_hash_key_t)key,
9207c478bd9Sstevel@tonic-gate 		    (mod_hash_val_t)spec->hwc_hash_next);
9217c478bd9Sstevel@tonic-gate 		spec->hwc_hash_next = NULL;
9227c478bd9Sstevel@tonic-gate 		return;
9237c478bd9Sstevel@tonic-gate 	}
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	/*
9267c478bd9Sstevel@tonic-gate 	 * If the head is not the one, look for the spec in the
9277c478bd9Sstevel@tonic-gate 	 * hwc_hash_next linkage.
9287c478bd9Sstevel@tonic-gate 	 */
9297c478bd9Sstevel@tonic-gate 	while (entry->hwc_hash_next && (entry->hwc_hash_next != spec))
9307c478bd9Sstevel@tonic-gate 		entry = entry->hwc_hash_next;
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	if (entry->hwc_hash_next) {
9337c478bd9Sstevel@tonic-gate 		entry->hwc_hash_next = spec->hwc_hash_next;
9347c478bd9Sstevel@tonic-gate 		spec->hwc_hash_next = NULL;
9357c478bd9Sstevel@tonic-gate 	}
9367c478bd9Sstevel@tonic-gate }
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate /*
9397c478bd9Sstevel@tonic-gate  * Hash a list of specs based on either parent name or class name
9407c478bd9Sstevel@tonic-gate  */
9417c478bd9Sstevel@tonic-gate static void
9427c478bd9Sstevel@tonic-gate hwc_hash(struct hwc_spec *spec_list, major_t major)
9437c478bd9Sstevel@tonic-gate {
9447c478bd9Sstevel@tonic-gate 	struct hwc_spec *spec = spec_list;
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	mutex_enter(&hwc_hash_lock);
9477c478bd9Sstevel@tonic-gate 	while (spec) {
9487c478bd9Sstevel@tonic-gate 		/* Put driver major here so parent can find it */
9497c478bd9Sstevel@tonic-gate 		spec->hwc_major = major;
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 		if (spec->hwc_parent_name != NULL) {
9527c478bd9Sstevel@tonic-gate 			hwc_hash_insert(spec, spec->hwc_parent_name,
9537c478bd9Sstevel@tonic-gate 			    hwc_par_hash);
9547c478bd9Sstevel@tonic-gate 		} else if (spec->hwc_class_name != NULL) {
9557c478bd9Sstevel@tonic-gate 			hwc_hash_insert(spec, spec->hwc_class_name,
9567c478bd9Sstevel@tonic-gate 			    hwc_class_hash);
9577c478bd9Sstevel@tonic-gate 		} else {
9587c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
9597c478bd9Sstevel@tonic-gate 			    "hwc_hash: No class or parent specified");
9607c478bd9Sstevel@tonic-gate 		}
9617c478bd9Sstevel@tonic-gate 		spec = spec->hwc_next;
9627c478bd9Sstevel@tonic-gate 	}
9637c478bd9Sstevel@tonic-gate 	mutex_exit(&hwc_hash_lock);
9647c478bd9Sstevel@tonic-gate }
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate /*
9677c478bd9Sstevel@tonic-gate  * Remove a list of specs from hash tables. Don't destroy the specs yet.
9687c478bd9Sstevel@tonic-gate  */
9697c478bd9Sstevel@tonic-gate static void
9707c478bd9Sstevel@tonic-gate hwc_unhash(struct hwc_spec *spec_list)
9717c478bd9Sstevel@tonic-gate {
9727c478bd9Sstevel@tonic-gate 	struct hwc_spec *spec = spec_list;
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	mutex_enter(&hwc_hash_lock);
9757c478bd9Sstevel@tonic-gate 	while (spec) {
9767c478bd9Sstevel@tonic-gate 		if (spec->hwc_parent_name != NULL) {
9777c478bd9Sstevel@tonic-gate 			hwc_hash_remove(spec, spec->hwc_parent_name,
9787c478bd9Sstevel@tonic-gate 			    hwc_par_hash);
9797c478bd9Sstevel@tonic-gate 		} else if (spec->hwc_class_name != NULL) {
9807c478bd9Sstevel@tonic-gate 			hwc_hash_remove(spec, spec->hwc_class_name,
9817c478bd9Sstevel@tonic-gate 			    hwc_class_hash);
9827c478bd9Sstevel@tonic-gate 		} else {
9837c478bd9Sstevel@tonic-gate 			cmn_err(CE_WARN,
9847c478bd9Sstevel@tonic-gate 			    "hwc_unhash: No class or parent specified");
9857c478bd9Sstevel@tonic-gate 		}
9867c478bd9Sstevel@tonic-gate 		spec = spec->hwc_next;
9877c478bd9Sstevel@tonic-gate 	}
9887c478bd9Sstevel@tonic-gate 	mutex_exit(&hwc_hash_lock);
9897c478bd9Sstevel@tonic-gate }
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate /*
9927c478bd9Sstevel@tonic-gate  * Make a copy of specs in a hash entry and add to the end of listp.
9937c478bd9Sstevel@tonic-gate  * Called by nexus to locate a list of child specs.
9947c478bd9Sstevel@tonic-gate  *
9957c478bd9Sstevel@tonic-gate  * entry is a list of hwc_spec chained together with hwc_hash_next.
9967c478bd9Sstevel@tonic-gate  * listp points to list chained together with hwc_next.
9977c478bd9Sstevel@tonic-gate  */
9987c478bd9Sstevel@tonic-gate static void
9997c478bd9Sstevel@tonic-gate hwc_spec_add(struct hwc_spec **listp, struct hwc_spec *entry,
10007c478bd9Sstevel@tonic-gate     major_t match_major)
10017c478bd9Sstevel@tonic-gate {
10027c478bd9Sstevel@tonic-gate 	/* Find the tail of the list */
10037c478bd9Sstevel@tonic-gate 	while (*listp)
10047c478bd9Sstevel@tonic-gate 		listp = &(*listp)->hwc_next;
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	while (entry) {
10077c478bd9Sstevel@tonic-gate 		struct hwc_spec *spec;
10087c478bd9Sstevel@tonic-gate 
1009a204de77Scth 		if ((match_major != DDI_MAJOR_T_NONE) &&
10107c478bd9Sstevel@tonic-gate 		    (match_major != entry->hwc_major)) {
10117c478bd9Sstevel@tonic-gate 			entry = entry->hwc_hash_next;
10127c478bd9Sstevel@tonic-gate 			continue;
10137c478bd9Sstevel@tonic-gate 		}
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 		/*
10167c478bd9Sstevel@tonic-gate 		 * Allocate spec and copy the content of entry.
10177c478bd9Sstevel@tonic-gate 		 * No need to copy class/parent name since caller
10187c478bd9Sstevel@tonic-gate 		 * already knows the parent dip.
10197c478bd9Sstevel@tonic-gate 		 */
10207c478bd9Sstevel@tonic-gate 		spec = kmem_zalloc(sizeof (*spec), KM_SLEEP);
10217c478bd9Sstevel@tonic-gate 		spec->hwc_devi_name = i_ddi_strdup(
10227c478bd9Sstevel@tonic-gate 		    entry->hwc_devi_name, KM_SLEEP);
10237c478bd9Sstevel@tonic-gate 		spec->hwc_major = entry->hwc_major;
10247c478bd9Sstevel@tonic-gate 		spec->hwc_devi_sys_prop_ptr = i_ddi_prop_list_dup(
10257c478bd9Sstevel@tonic-gate 		    entry->hwc_devi_sys_prop_ptr, KM_SLEEP);
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 		*listp = spec;
10287c478bd9Sstevel@tonic-gate 		listp = &spec->hwc_next;
10297c478bd9Sstevel@tonic-gate 		entry = entry->hwc_hash_next;
10307c478bd9Sstevel@tonic-gate 	}
10317c478bd9Sstevel@tonic-gate }
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate /*
10347c478bd9Sstevel@tonic-gate  * Given a dip, find the list of child .conf specs from most specific
10357c478bd9Sstevel@tonic-gate  * (parent pathname) to least specific (class name).
10367c478bd9Sstevel@tonic-gate  *
10377c478bd9Sstevel@tonic-gate  * This function allows top-down loading to be implemented without
10387c478bd9Sstevel@tonic-gate  * changing the format of driver.conf file.
10397c478bd9Sstevel@tonic-gate  */
10407c478bd9Sstevel@tonic-gate struct hwc_spec *
10417c478bd9Sstevel@tonic-gate hwc_get_child_spec(dev_info_t *dip, major_t match_major)
10427c478bd9Sstevel@tonic-gate {
10437c478bd9Sstevel@tonic-gate 	extern char *i_ddi_parname(dev_info_t *, char *);
10447c478bd9Sstevel@tonic-gate 	extern int i_ddi_get_exported_classes(dev_info_t *, char ***);
10457c478bd9Sstevel@tonic-gate 	extern void i_ddi_free_exported_classes(char **, int);
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 	int i, nclass;
10487c478bd9Sstevel@tonic-gate 	char **classes;
10497c478bd9Sstevel@tonic-gate 	struct hwc_spec *list = NULL;
10507c478bd9Sstevel@tonic-gate 	mod_hash_val_t val;
10517c478bd9Sstevel@tonic-gate 	char *parname, *parname_buf;
10527c478bd9Sstevel@tonic-gate 	char *deviname, *deviname_buf;
10537c478bd9Sstevel@tonic-gate 	char *pathname, *pathname_buf;
10547c478bd9Sstevel@tonic-gate 	char *bindname;
10557c478bd9Sstevel@tonic-gate 	char *drvname;
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	pathname_buf = kmem_alloc(3 * MAXPATHLEN, KM_SLEEP);
10587c478bd9Sstevel@tonic-gate 	deviname_buf = pathname_buf + MAXPATHLEN;
10597c478bd9Sstevel@tonic-gate 	parname_buf = pathname_buf + (2 * MAXPATHLEN);
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	mutex_enter(&hwc_hash_lock);
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	/*
10647c478bd9Sstevel@tonic-gate 	 * Lookup based on full path.
10657c478bd9Sstevel@tonic-gate 	 * In the case of root node, ddi_pathname would return
10667c478bd9Sstevel@tonic-gate 	 * null string so just skip calling it.
10677c478bd9Sstevel@tonic-gate 	 * As the pathname always begins with /, no simpler
10687c478bd9Sstevel@tonic-gate 	 * name can duplicate it.
10697c478bd9Sstevel@tonic-gate 	 */
10707c478bd9Sstevel@tonic-gate 	pathname = (dip == ddi_root_node()) ? "/" :
10717c478bd9Sstevel@tonic-gate 	    ddi_pathname(dip, pathname_buf);
10727c478bd9Sstevel@tonic-gate 	ASSERT(pathname != NULL);
10737c478bd9Sstevel@tonic-gate 	ASSERT(*pathname == '/');
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	if (mod_hash_find(hwc_par_hash, (mod_hash_key_t)pathname, &val) == 0) {
10767c478bd9Sstevel@tonic-gate 		hwc_spec_add(&list, (struct hwc_spec *)val, match_major);
10777c478bd9Sstevel@tonic-gate 	}
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	/*
10807c478bd9Sstevel@tonic-gate 	 * Lookup nodename@address.
10817c478bd9Sstevel@tonic-gate 	 * Note deviname cannot match pathname.
10827c478bd9Sstevel@tonic-gate 	 */
10837c478bd9Sstevel@tonic-gate 	deviname = ddi_deviname(dip, deviname_buf);
10847c478bd9Sstevel@tonic-gate 	if (*deviname != '\0') {
10857c478bd9Sstevel@tonic-gate 		/*
10867c478bd9Sstevel@tonic-gate 		 * Skip leading / returned by ddi_deviname.
10877c478bd9Sstevel@tonic-gate 		 */
10887c478bd9Sstevel@tonic-gate 		ASSERT(*deviname == '/');
10897c478bd9Sstevel@tonic-gate 		deviname++;
1090e099bf07Scth 		if ((*deviname != '\0') && (mod_hash_find(hwc_par_hash,
10917c478bd9Sstevel@tonic-gate 		    (mod_hash_key_t)deviname, &val) == 0))
10927c478bd9Sstevel@tonic-gate 			hwc_spec_add(&list,
10937c478bd9Sstevel@tonic-gate 			    (struct hwc_spec *)val, match_major);
10947c478bd9Sstevel@tonic-gate 	}
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	/*
10977c478bd9Sstevel@tonic-gate 	 * Lookup bindingname@address.
10987c478bd9Sstevel@tonic-gate 	 * Take care not to perform duplicate lookups.
10997c478bd9Sstevel@tonic-gate 	 */
11007c478bd9Sstevel@tonic-gate 	parname = i_ddi_parname(dip, parname_buf);
11017c478bd9Sstevel@tonic-gate 	if (*parname != '\0') {
11027c478bd9Sstevel@tonic-gate 		ASSERT(*parname != '/');
11037c478bd9Sstevel@tonic-gate 		if ((strcmp(parname, deviname) != 0) &&
11047c478bd9Sstevel@tonic-gate 		    (mod_hash_find(hwc_par_hash,
11057c478bd9Sstevel@tonic-gate 		    (mod_hash_key_t)parname, &val) == 0)) {
11067c478bd9Sstevel@tonic-gate 			hwc_spec_add(&list,
11077c478bd9Sstevel@tonic-gate 			    (struct hwc_spec *)val, match_major);
11087c478bd9Sstevel@tonic-gate 		}
11097c478bd9Sstevel@tonic-gate 	}
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	/*
11127c478bd9Sstevel@tonic-gate 	 * Lookup driver binding name
11137c478bd9Sstevel@tonic-gate 	 */
11147c478bd9Sstevel@tonic-gate 	bindname = ddi_binding_name(dip);
11157c478bd9Sstevel@tonic-gate 	ASSERT(*bindname != '/');
11167c478bd9Sstevel@tonic-gate 	if ((strcmp(bindname, parname) != 0) &&
11177c478bd9Sstevel@tonic-gate 	    (strcmp(bindname, deviname) != 0) &&
11187c478bd9Sstevel@tonic-gate 	    (mod_hash_find(hwc_par_hash, (mod_hash_key_t)bindname, &val) == 0))
11197c478bd9Sstevel@tonic-gate 		hwc_spec_add(&list, (struct hwc_spec *)val, match_major);
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	/*
11227c478bd9Sstevel@tonic-gate 	 * Lookup driver name
11237c478bd9Sstevel@tonic-gate 	 */
11247c478bd9Sstevel@tonic-gate 	drvname = (char *)ddi_driver_name(dip);
11257c478bd9Sstevel@tonic-gate 	ASSERT(*drvname != '/');
11267c478bd9Sstevel@tonic-gate 	if ((strcmp(drvname, bindname) != 0) &&
11277c478bd9Sstevel@tonic-gate 	    (strcmp(drvname, parname) != 0) &&
11287c478bd9Sstevel@tonic-gate 	    (strcmp(drvname, deviname) != 0) &&
11297c478bd9Sstevel@tonic-gate 	    (mod_hash_find(hwc_par_hash, (mod_hash_key_t)drvname, &val) == 0))
11307c478bd9Sstevel@tonic-gate 		hwc_spec_add(&list, (struct hwc_spec *)val, match_major);
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	kmem_free(pathname_buf, 3 * MAXPATHLEN);
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	/*
11357c478bd9Sstevel@tonic-gate 	 * Lookup classes exported by this node and lookup the
11367c478bd9Sstevel@tonic-gate 	 * class hash table for all .conf specs
11377c478bd9Sstevel@tonic-gate 	 */
11387c478bd9Sstevel@tonic-gate 	nclass = i_ddi_get_exported_classes(dip, &classes);
11397c478bd9Sstevel@tonic-gate 	for (i = 0; i < nclass; i++) {
11407c478bd9Sstevel@tonic-gate 		if (mod_hash_find(hwc_class_hash, (mod_hash_key_t)classes[i],
11417c478bd9Sstevel@tonic-gate 		    &val) == 0)
11427c478bd9Sstevel@tonic-gate 			hwc_spec_add(&list, (struct hwc_spec *)val,
11437c478bd9Sstevel@tonic-gate 			    match_major);
11447c478bd9Sstevel@tonic-gate 	}
11457c478bd9Sstevel@tonic-gate 	i_ddi_free_exported_classes(classes, nclass);
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	mutex_exit(&hwc_hash_lock);
11487c478bd9Sstevel@tonic-gate 	return (list);
11497c478bd9Sstevel@tonic-gate }
1150