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