xref: /onnv-gate/usr/src/uts/common/os/modsubr.c (revision 12660:32abd267e35d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51993Sramat  * Common Development and Distribution License (the "License").
61993Sramat  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
2212588SJerry.Gilliam@Sun.COM  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include <sys/param.h>
260Sstevel@tonic-gate #include <sys/modctl.h>
270Sstevel@tonic-gate #include <sys/modhash.h>
280Sstevel@tonic-gate #include <sys/open.h>
290Sstevel@tonic-gate #include <sys/conf.h>
300Sstevel@tonic-gate #include <sys/errno.h>
310Sstevel@tonic-gate #include <sys/sysmacros.h>
320Sstevel@tonic-gate #include <sys/kmem.h>
330Sstevel@tonic-gate #include <sys/cmn_err.h>
340Sstevel@tonic-gate #include <sys/stat.h>
350Sstevel@tonic-gate #include <sys/mode.h>
360Sstevel@tonic-gate #include <sys/pathname.h>
370Sstevel@tonic-gate #include <sys/vnode.h>
380Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
390Sstevel@tonic-gate #include <sys/ddi_implfuncs.h>
400Sstevel@tonic-gate #include <sys/esunddi.h>
410Sstevel@tonic-gate #include <sys/sunddi.h>
420Sstevel@tonic-gate #include <sys/sunndi.h>
430Sstevel@tonic-gate #include <sys/systeminfo.h>
440Sstevel@tonic-gate #include <sys/hwconf.h>
450Sstevel@tonic-gate #include <sys/file.h>
460Sstevel@tonic-gate #include <sys/varargs.h>
470Sstevel@tonic-gate #include <sys/thread.h>
480Sstevel@tonic-gate #include <sys/cred.h>
490Sstevel@tonic-gate #include <sys/autoconf.h>
500Sstevel@tonic-gate #include <sys/kobj.h>
510Sstevel@tonic-gate #include <sys/consdev.h>
520Sstevel@tonic-gate #include <sys/systm.h>
530Sstevel@tonic-gate #include <sys/debug.h>
540Sstevel@tonic-gate #include <sys/atomic.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate extern struct dev_ops nodev_ops;
570Sstevel@tonic-gate extern struct dev_ops mod_nodev_ops;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate struct mod_noload {
600Sstevel@tonic-gate 	struct mod_noload *mn_next;
610Sstevel@tonic-gate 	char *mn_name;
620Sstevel@tonic-gate };
630Sstevel@tonic-gate 
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate  * Function prototypes
660Sstevel@tonic-gate  */
670Sstevel@tonic-gate static int init_stubs(struct modctl *, struct mod_modinfo *);
680Sstevel@tonic-gate static int nm_hash(char *);
690Sstevel@tonic-gate static void make_syscallname(char *, int);
700Sstevel@tonic-gate static void hwc_hash_init();
710Sstevel@tonic-gate static void hwc_hash(struct hwc_spec *, major_t);
720Sstevel@tonic-gate static void hwc_unhash(struct hwc_spec *);
730Sstevel@tonic-gate 
7410842SJerry.Gilliam@Sun.COM int
major_valid(major_t major)7512588SJerry.Gilliam@Sun.COM major_valid(major_t major)
7612588SJerry.Gilliam@Sun.COM {
7712588SJerry.Gilliam@Sun.COM 	return (major != DDI_MAJOR_T_NONE &&
7812588SJerry.Gilliam@Sun.COM 	    (major >= 0 && major < devcnt));
7912588SJerry.Gilliam@Sun.COM }
8012588SJerry.Gilliam@Sun.COM 
8112588SJerry.Gilliam@Sun.COM int
driver_installed(major_t major)8210842SJerry.Gilliam@Sun.COM driver_installed(major_t major)
8310842SJerry.Gilliam@Sun.COM {
8412588SJerry.Gilliam@Sun.COM 	return (major_valid(major) && devnamesp[major].dn_name != NULL);
8512588SJerry.Gilliam@Sun.COM }
8612588SJerry.Gilliam@Sun.COM 
8712588SJerry.Gilliam@Sun.COM int
driver_active(major_t major)8812588SJerry.Gilliam@Sun.COM driver_active(major_t major)
8912588SJerry.Gilliam@Sun.COM {
9012588SJerry.Gilliam@Sun.COM 	return (driver_installed(major) && !(devnamesp[major].dn_flags &
9110842SJerry.Gilliam@Sun.COM 	    (DN_DRIVER_REMOVED|DN_DRIVER_INACTIVE)));
9210842SJerry.Gilliam@Sun.COM }
9310842SJerry.Gilliam@Sun.COM 
940Sstevel@tonic-gate struct dev_ops *
mod_hold_dev_by_major(major_t major)950Sstevel@tonic-gate mod_hold_dev_by_major(major_t major)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	struct dev_ops **devopspp, *ops;
980Sstevel@tonic-gate 	int loaded;
990Sstevel@tonic-gate 	char *drvname;
1000Sstevel@tonic-gate 
10112588SJerry.Gilliam@Sun.COM 	if (!driver_active(major))
1020Sstevel@tonic-gate 		return (NULL);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	LOCK_DEV_OPS(&(devnamesp[major].dn_lock));
1050Sstevel@tonic-gate 	devopspp = &devopsp[major];
1060Sstevel@tonic-gate 	loaded = 1;
1070Sstevel@tonic-gate 	while (loaded && !CB_DRV_INSTALLED(*devopspp)) {
1080Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&(devnamesp[major].dn_lock));
1090Sstevel@tonic-gate 		drvname = mod_major_to_name(major);
1100Sstevel@tonic-gate 		if (drvname == NULL)
1110Sstevel@tonic-gate 			return (NULL);
1120Sstevel@tonic-gate 		loaded = (modload("drv", drvname) != -1);
1130Sstevel@tonic-gate 		LOCK_DEV_OPS(&(devnamesp[major].dn_lock));
1140Sstevel@tonic-gate 	}
1150Sstevel@tonic-gate 	if (loaded) {
1160Sstevel@tonic-gate 		INCR_DEV_OPS_REF(*devopspp);
1170Sstevel@tonic-gate 		ops = *devopspp;
1180Sstevel@tonic-gate 	} else {
1190Sstevel@tonic-gate 		ops = NULL;
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&(devnamesp[major].dn_lock));
1220Sstevel@tonic-gate 	return (ops);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate #ifdef	DEBUG_RELE
1260Sstevel@tonic-gate static int mod_rele_pause = DEBUG_RELE;
1270Sstevel@tonic-gate #endif	/* DEBUG_RELE */
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate void
mod_rele_dev_by_major(major_t major)1300Sstevel@tonic-gate mod_rele_dev_by_major(major_t major)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	struct dev_ops *ops;
1330Sstevel@tonic-gate 	struct devnames *dnp;
1340Sstevel@tonic-gate 
13512588SJerry.Gilliam@Sun.COM 	if (!driver_active(major))
1360Sstevel@tonic-gate 		return;
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	dnp = &devnamesp[major];
1390Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
1400Sstevel@tonic-gate 	ops = devopsp[major];
1410Sstevel@tonic-gate 	ASSERT(CB_DRV_INSTALLED(ops));
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate #ifdef	DEBUG_RELE
1440Sstevel@tonic-gate 	if (!DEV_OPS_HELD(ops))  {
1450Sstevel@tonic-gate 		char *s;
1460Sstevel@tonic-gate 		static char *msg = "mod_rele_dev_by_major: unheld driver!";
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 		printf("mod_rele_dev_by_major: Major dev <%u>, name <%s>\n",
1490Sstevel@tonic-gate 		    (uint_t)major,
1500Sstevel@tonic-gate 		    (s = mod_major_to_name(major)) ? s : "unknown");
1510Sstevel@tonic-gate 		if (mod_rele_pause)
1520Sstevel@tonic-gate 			debug_enter(msg);
1530Sstevel@tonic-gate 		else
1540Sstevel@tonic-gate 			printf("%s\n", msg);
1550Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
1560Sstevel@tonic-gate 		return;			/* XXX: Note changed behavior */
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate #endif	/* DEBUG_RELE */
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	if (!DEV_OPS_HELD(ops)) {
1620Sstevel@tonic-gate 		cmn_err(CE_PANIC,
1630Sstevel@tonic-gate 		    "mod_rele_dev_by_major: Unheld driver: major number <%u>",
1640Sstevel@tonic-gate 		    (uint_t)major);
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 	DECR_DEV_OPS_REF(ops);
1670Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate struct dev_ops *
mod_hold_dev_by_devi(dev_info_t * devi)1710Sstevel@tonic-gate mod_hold_dev_by_devi(dev_info_t *devi)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate 	major_t major;
1740Sstevel@tonic-gate 	char *name;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	name = ddi_get_name(devi);
1777009Scth 	if ((major = mod_name_to_major(name)) == DDI_MAJOR_T_NONE)
1780Sstevel@tonic-gate 		return (NULL);
1790Sstevel@tonic-gate 	return (mod_hold_dev_by_major(major));
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate void
mod_rele_dev_by_devi(dev_info_t * devi)1830Sstevel@tonic-gate mod_rele_dev_by_devi(dev_info_t *devi)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate 	major_t major;
1860Sstevel@tonic-gate 	char *name;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	name = ddi_get_name(devi);
1897009Scth 	if ((major = mod_name_to_major(name)) == DDI_MAJOR_T_NONE)
1900Sstevel@tonic-gate 		return;
1910Sstevel@tonic-gate 	mod_rele_dev_by_major(major);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate int
nomod_zero()1950Sstevel@tonic-gate nomod_zero()
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate 	return (0);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate int
nomod_minus_one()2010Sstevel@tonic-gate nomod_minus_one()
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate 	return (-1);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate int
nomod_einval()2070Sstevel@tonic-gate nomod_einval()
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate 	return (EINVAL);
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate void
nomod_void()2130Sstevel@tonic-gate nomod_void()
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate 	/* nothing */
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate /*
2190Sstevel@tonic-gate  * Install all the stubs for a module.
2200Sstevel@tonic-gate  * Return zero if there were no errors or an errno value.
2210Sstevel@tonic-gate  */
2220Sstevel@tonic-gate int
install_stubs_by_name(struct modctl * modp,char * name)2230Sstevel@tonic-gate install_stubs_by_name(struct modctl *modp, char *name)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate 	char *p;
2260Sstevel@tonic-gate 	char *filenamep;
2270Sstevel@tonic-gate 	char namebuf[MODMAXNAMELEN + 12];
2280Sstevel@tonic-gate 	struct mod_modinfo *mp;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	p = name;
2310Sstevel@tonic-gate 	filenamep = name;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	while (*p)
2340Sstevel@tonic-gate 		if (*p++ == '/')
2350Sstevel@tonic-gate 			filenamep = p;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	/*
2380Sstevel@tonic-gate 	 * Concatenate "name" with "_modname" then look up this symbol
2390Sstevel@tonic-gate 	 * in the kernel.  If not found, we're done.
2400Sstevel@tonic-gate 	 * If found, then find the "mod" info structure and call init_stubs().
2410Sstevel@tonic-gate 	 */
2420Sstevel@tonic-gate 	p = namebuf;
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	while (*filenamep && *filenamep != '.')
2450Sstevel@tonic-gate 		*p++ = *filenamep++;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	(void) strcpy(p, "_modinfo");
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	if ((mp = (struct mod_modinfo *)modgetsymvalue(namebuf, 1)) != 0)
2500Sstevel@tonic-gate 		return (init_stubs(modp, mp));
2510Sstevel@tonic-gate 	else
2520Sstevel@tonic-gate 		return (0);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate static int
init_stubs(struct modctl * modp,struct mod_modinfo * mp)2560Sstevel@tonic-gate init_stubs(struct modctl *modp, struct mod_modinfo *mp)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate 	struct mod_stub_info *sp;
2590Sstevel@tonic-gate 	int i;
2600Sstevel@tonic-gate 	ulong_t offset;
2610Sstevel@tonic-gate 	uintptr_t funcadr;
2620Sstevel@tonic-gate 	char *funcname;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	modp->mod_modinfo = mp;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	/*
2670Sstevel@tonic-gate 	 * Fill in all stubs for this module.  We can't be lazy, since
2680Sstevel@tonic-gate 	 * some calls could come in from interrupt level, and we
2690Sstevel@tonic-gate 	 * can't modlookup then (symbols may be paged out).
2700Sstevel@tonic-gate 	 */
2710Sstevel@tonic-gate 	sp = mp->modm_stubs;
2720Sstevel@tonic-gate 	for (i = 0; sp->mods_func_adr; i++, sp++) {
2730Sstevel@tonic-gate 		funcname = modgetsymname(sp->mods_stub_adr, &offset);
2740Sstevel@tonic-gate 		if (funcname == NULL) {
2754452Scth 			printf("init_stubs: couldn't find symbol "
2764452Scth 			    "in module %s\n", mp->modm_module_name);
2770Sstevel@tonic-gate 			return (EFAULT);
2780Sstevel@tonic-gate 		}
2790Sstevel@tonic-gate 		funcadr = kobj_lookup(modp->mod_mp, funcname);
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 		if (kobj_addrcheck(modp->mod_mp, (caddr_t)funcadr)) {
2820Sstevel@tonic-gate 			printf("%s:%s() not defined properly\n",
2834452Scth 			    mp->modm_module_name, funcname);
2840Sstevel@tonic-gate 			return (EFAULT);
2850Sstevel@tonic-gate 		}
2860Sstevel@tonic-gate 		sp->mods_func_adr = funcadr;
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 	mp->mp = modp;
2890Sstevel@tonic-gate 	return (0);
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate /*
2930Sstevel@tonic-gate  * modp->mod_modinfo has to be checked in these functions before
2940Sstevel@tonic-gate  * mod_stub_info is accessed because it's not guranteed that all
2950Sstevel@tonic-gate  * modules define mod_stub_info structures.
2960Sstevel@tonic-gate  */
2970Sstevel@tonic-gate void
install_stubs(struct modctl * modp)2980Sstevel@tonic-gate install_stubs(struct modctl *modp)
2990Sstevel@tonic-gate {
3000Sstevel@tonic-gate 	struct mod_stub_info *stub;
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	if (modp->mod_modinfo) {
3030Sstevel@tonic-gate 		membar_producer();
3040Sstevel@tonic-gate 		for (stub = modp->mod_modinfo->modm_stubs;
3050Sstevel@tonic-gate 		    stub->mods_func_adr; stub++) {
3060Sstevel@tonic-gate 			stub->mods_flag |= MODS_INSTALLED;
3070Sstevel@tonic-gate 		}
3080Sstevel@tonic-gate 		membar_producer();
3090Sstevel@tonic-gate 	}
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate void
uninstall_stubs(struct modctl * modp)3130Sstevel@tonic-gate uninstall_stubs(struct modctl *modp)
3140Sstevel@tonic-gate {
3150Sstevel@tonic-gate 	struct mod_stub_info *stub;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	if (modp->mod_modinfo) {
3180Sstevel@tonic-gate 		membar_producer();
3190Sstevel@tonic-gate 		for (stub = modp->mod_modinfo->modm_stubs;
3200Sstevel@tonic-gate 		    stub->mods_func_adr; stub++) {
3210Sstevel@tonic-gate 			stub->mods_flag &= ~MODS_INSTALLED;
3220Sstevel@tonic-gate 		}
3230Sstevel@tonic-gate 		membar_producer();
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate }
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate void
reset_stubs(struct modctl * modp)3280Sstevel@tonic-gate reset_stubs(struct modctl *modp)
3290Sstevel@tonic-gate {
3300Sstevel@tonic-gate 	struct mod_stub_info *stub;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	if (modp->mod_modinfo) {
3330Sstevel@tonic-gate 		for (stub = modp->mod_modinfo->modm_stubs;
3340Sstevel@tonic-gate 		    stub->mods_func_adr; stub++) {
3350Sstevel@tonic-gate 			if (stub->mods_flag & (MODS_WEAK | MODS_NOUNLOAD))
3360Sstevel@tonic-gate 				stub->mods_func_adr =
3370Sstevel@tonic-gate 				    (uintptr_t)stub->mods_errfcn;
3380Sstevel@tonic-gate 			else
3390Sstevel@tonic-gate 				stub->mods_func_adr =
3400Sstevel@tonic-gate 				    (uintptr_t)mod_hold_stub;
3410Sstevel@tonic-gate 		}
3420Sstevel@tonic-gate 		modp->mod_modinfo->mp = NULL;
3430Sstevel@tonic-gate 	}
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate struct modctl *
mod_getctl(struct modlinkage * modlp)3470Sstevel@tonic-gate mod_getctl(struct modlinkage *modlp)
3480Sstevel@tonic-gate {
3490Sstevel@tonic-gate 	struct modctl	*modp;
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	mutex_enter(&mod_lock);
3520Sstevel@tonic-gate 	modp = &modules;
3530Sstevel@tonic-gate 	do {
3540Sstevel@tonic-gate 		if (modp->mod_linkage == modlp) {
3550Sstevel@tonic-gate 			mutex_exit(&mod_lock);
3560Sstevel@tonic-gate 			return (modp);
3570Sstevel@tonic-gate 		}
3580Sstevel@tonic-gate 	} while ((modp = modp->mod_next) != &modules);
3590Sstevel@tonic-gate 	mutex_exit(&mod_lock);
3600Sstevel@tonic-gate 	return (NULL);
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate /*
3650Sstevel@tonic-gate  * Attach driver.conf info to devnames for a driver
3660Sstevel@tonic-gate  */
3670Sstevel@tonic-gate struct par_list *
impl_make_parlist(major_t major)3680Sstevel@tonic-gate impl_make_parlist(major_t major)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate 	int err;
3710Sstevel@tonic-gate 	struct par_list *pl = NULL, *tmp;
3720Sstevel@tonic-gate 	ddi_prop_t *props = NULL;
3730Sstevel@tonic-gate 	char *confname, *drvname;
3740Sstevel@tonic-gate 	struct devnames *dnp;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	dnp = &devnamesp[major];
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	ASSERT(mutex_owned(&dnp->dn_lock));
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	/*
3810Sstevel@tonic-gate 	 * If .conf file already parsed or driver removed, just return.
3820Sstevel@tonic-gate 	 * May return NULL.
3830Sstevel@tonic-gate 	 */
3840Sstevel@tonic-gate 	if (dnp->dn_flags & (DN_CONF_PARSED | DN_DRIVER_REMOVED))
3850Sstevel@tonic-gate 		return (dnp->dn_pl);
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	drvname = mod_major_to_name(major);
3880Sstevel@tonic-gate 	if (drvname == NULL)
3890Sstevel@tonic-gate 		return (NULL);
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	confname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
3920Sstevel@tonic-gate 	(void) snprintf(confname, MAXNAMELEN, "drv/%s.conf", drvname);
3930Sstevel@tonic-gate 	err = hwc_parse(confname, &pl, &props);
3940Sstevel@tonic-gate 	kmem_free(confname, MAXNAMELEN);
3950Sstevel@tonic-gate 	if (err)	/* file doesn't exist */
3960Sstevel@tonic-gate 		return (NULL);
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	/*
3990Sstevel@tonic-gate 	 * If there are global properties, reference it from dnp.
4000Sstevel@tonic-gate 	 */
4010Sstevel@tonic-gate 	if (props)
4020Sstevel@tonic-gate 		dnp->dn_global_prop_ptr = i_ddi_prop_list_create(props);
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	/*
4050Sstevel@tonic-gate 	 * Hash specs to be looked up by nexus drivers
4060Sstevel@tonic-gate 	 */
4070Sstevel@tonic-gate 	tmp = pl;
4080Sstevel@tonic-gate 	while (tmp) {
4090Sstevel@tonic-gate 		(void) hwc_hash(tmp->par_specs, major);
4100Sstevel@tonic-gate 		tmp = tmp->par_next;
4110Sstevel@tonic-gate 	}
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	if (!i_ddi_io_initialized()) {
4140Sstevel@tonic-gate 		if (i_ddi_prop_search(DDI_DEV_T_ANY, DDI_FORCEATTACH,
4150Sstevel@tonic-gate 		    DDI_PROP_TYPE_INT, &props))
4160Sstevel@tonic-gate 			dnp->dn_flags |= DN_FORCE_ATTACH;
4174452Scth 		if (i_ddi_prop_search(DDI_DEV_T_ANY, DDI_OPEN_RETURNS_EINTR,
4184452Scth 		    DDI_PROP_TYPE_INT, &props))
4194452Scth 			dnp->dn_flags |= DN_OPEN_RETURNS_EINTR;
4206640Scth 		if (i_ddi_prop_search(DDI_DEV_T_ANY, "scsi-size-clean",
4216640Scth 		    DDI_PROP_TYPE_INT, &props))
4226640Scth 			dnp->dn_flags |= DN_SCSI_SIZE_CLEAN;
4230Sstevel@tonic-gate 	}
4241993Sramat 
4251993Sramat 	if (i_ddi_prop_search(DDI_DEV_T_ANY, DDI_VHCI_CLASS,
4261993Sramat 	    DDI_PROP_TYPE_STRING, &props))
4271993Sramat 		dnp->dn_flags |= DN_PHCI_DRIVER;
4281993Sramat 
429*12660SJerry.Gilliam@Sun.COM 	if (i_ddi_prop_search(DDI_DEV_T_ANY, DDI_DEVID_REGISTRANT,
430*12660SJerry.Gilliam@Sun.COM 	    DDI_PROP_TYPE_INT, &props)) {
431*12660SJerry.Gilliam@Sun.COM 		dnp->dn_flags |= DN_DEVID_REGISTRANT;
432*12660SJerry.Gilliam@Sun.COM 	}
433*12660SJerry.Gilliam@Sun.COM 
4340Sstevel@tonic-gate 	dnp->dn_flags |= DN_CONF_PARSED;
4350Sstevel@tonic-gate 	dnp->dn_pl = pl;
4360Sstevel@tonic-gate 	return (pl);
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate /*
4400Sstevel@tonic-gate  * Destroy driver.conf info in devnames array for a driver
4410Sstevel@tonic-gate  */
4420Sstevel@tonic-gate int
impl_free_parlist(major_t major)4430Sstevel@tonic-gate impl_free_parlist(major_t major)
4440Sstevel@tonic-gate {
4450Sstevel@tonic-gate 	struct par_list *pl;
4460Sstevel@tonic-gate 	struct devnames *dnp = &devnamesp[major];
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	/*
4490Sstevel@tonic-gate 	 * Unref driver global property list. Don't destroy it
4500Sstevel@tonic-gate 	 * because some instances may still be referencing it.
4510Sstevel@tonic-gate 	 * The property list will be freed when the last ref
4520Sstevel@tonic-gate 	 * goes away.
4530Sstevel@tonic-gate 	 */
4540Sstevel@tonic-gate 	if (dnp->dn_global_prop_ptr) {
4550Sstevel@tonic-gate 		i_ddi_prop_list_rele(dnp->dn_global_prop_ptr, dnp);
4560Sstevel@tonic-gate 		dnp->dn_global_prop_ptr = NULL;
4570Sstevel@tonic-gate 	}
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 	/*
4600Sstevel@tonic-gate 	 * remove specs from hash table
4610Sstevel@tonic-gate 	 */
4620Sstevel@tonic-gate 	for (pl = dnp->dn_pl; pl; pl = pl->par_next)
4630Sstevel@tonic-gate 		hwc_unhash(pl->par_specs);
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	impl_delete_par_list(dnp->dn_pl);
4660Sstevel@tonic-gate 	dnp->dn_pl = NULL;
4670Sstevel@tonic-gate 	dnp->dn_flags &= ~DN_CONF_PARSED;
4680Sstevel@tonic-gate 	return (0);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate struct bind *mb_hashtab[MOD_BIND_HASHSIZE];
4720Sstevel@tonic-gate struct bind *sb_hashtab[MOD_BIND_HASHSIZE];
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate static int
nm_hash(char * name)4750Sstevel@tonic-gate nm_hash(char *name)
4760Sstevel@tonic-gate {
4770Sstevel@tonic-gate 	char c;
4780Sstevel@tonic-gate 	int hash = 0;
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 	for (c = *name++; c; c = *name++)
4810Sstevel@tonic-gate 		hash ^= c;
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	return (hash & MOD_BIND_HASHMASK);
4840Sstevel@tonic-gate }
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate void
clear_binding_hash(struct bind ** bhash)4870Sstevel@tonic-gate clear_binding_hash(struct bind **bhash)
4880Sstevel@tonic-gate {
4890Sstevel@tonic-gate 	int i;
4900Sstevel@tonic-gate 	struct bind *bp, *bp1;
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 	for (i = 0; i < MOD_BIND_HASHSIZE; i++) {
4930Sstevel@tonic-gate 		bp = bhash[i];
4940Sstevel@tonic-gate 		while (bp != NULL) {
4950Sstevel@tonic-gate 			kmem_free(bp->b_name, strlen(bp->b_name) + 1);
4960Sstevel@tonic-gate 			if (bp->b_bind_name) {
4970Sstevel@tonic-gate 				kmem_free(bp->b_bind_name,
4980Sstevel@tonic-gate 				    strlen(bp->b_bind_name) + 1);
4990Sstevel@tonic-gate 			}
5000Sstevel@tonic-gate 			bp1 = bp;
5010Sstevel@tonic-gate 			bp = bp->b_next;
5020Sstevel@tonic-gate 			kmem_free(bp1, sizeof (struct bind));
5030Sstevel@tonic-gate 		}
5040Sstevel@tonic-gate 		bhash[i] = NULL;
5050Sstevel@tonic-gate 	}
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate 
5088831SJerry.Gilliam@Sun.COM /* Find an mbind by name match (caller can ask for deleted match) */
5090Sstevel@tonic-gate static struct bind *
find_mbind(char * name,struct bind ** hashtab,int deleted)5108831SJerry.Gilliam@Sun.COM find_mbind(char *name, struct bind **hashtab, int deleted)
5110Sstevel@tonic-gate {
5128831SJerry.Gilliam@Sun.COM 	struct bind	*mb;
5130Sstevel@tonic-gate 
5148831SJerry.Gilliam@Sun.COM 	for (mb = hashtab[nm_hash(name)]; mb; mb = mb->b_next) {
5158831SJerry.Gilliam@Sun.COM 		if (deleted && (mb->b_num >= 0))
5168831SJerry.Gilliam@Sun.COM 			continue;			/* skip active */
5178831SJerry.Gilliam@Sun.COM 		if (!deleted && (mb->b_num < 0))
5188831SJerry.Gilliam@Sun.COM 			continue;			/* skip deleted */
5198831SJerry.Gilliam@Sun.COM 
5208831SJerry.Gilliam@Sun.COM 		/* return if name matches */
5218831SJerry.Gilliam@Sun.COM 		if (strcmp(name, mb->b_name) == 0) {
5220Sstevel@tonic-gate 			break;
5238831SJerry.Gilliam@Sun.COM 		}
5240Sstevel@tonic-gate 	}
5250Sstevel@tonic-gate 	return (mb);
5260Sstevel@tonic-gate }
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate /*
5290Sstevel@tonic-gate  * Create an entry for the given (name, major, bind_name) tuple in the
5300Sstevel@tonic-gate  * hash table supplied.  Reject the attempt to do so if 'name' is already
5310Sstevel@tonic-gate  * in the hash table.
5320Sstevel@tonic-gate  *
5330Sstevel@tonic-gate  * Does not provide synchronization, so use only during boot or with
5340Sstevel@tonic-gate  * externally provided locking.
5350Sstevel@tonic-gate  */
5360Sstevel@tonic-gate int
make_mbind(char * name,int num,char * bind_name,struct bind ** hashtab)5378831SJerry.Gilliam@Sun.COM make_mbind(char *name, int num, char *bind_name, struct bind **hashtab)
5380Sstevel@tonic-gate {
5398831SJerry.Gilliam@Sun.COM 	struct bind	*mb;
5408831SJerry.Gilliam@Sun.COM 	struct bind	**pmb;
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	ASSERT(hashtab != NULL);
5438831SJerry.Gilliam@Sun.COM 	ASSERT(num >= 0);
5440Sstevel@tonic-gate 
5458831SJerry.Gilliam@Sun.COM 	/* Fail if the key being added is already established */
5468831SJerry.Gilliam@Sun.COM 	if (find_mbind(name, hashtab, 0) != NULL)
5470Sstevel@tonic-gate 		return (-1);
5480Sstevel@tonic-gate 
5498831SJerry.Gilliam@Sun.COM 	/* Allocate new mbind */
5508831SJerry.Gilliam@Sun.COM 	mb = kmem_zalloc(sizeof (struct bind), KM_SLEEP);
5518831SJerry.Gilliam@Sun.COM 	mb->b_name = i_ddi_strdup(name, KM_SLEEP);
5528831SJerry.Gilliam@Sun.COM 	mb->b_num = num;
5538831SJerry.Gilliam@Sun.COM 	if (bind_name != NULL)
5548831SJerry.Gilliam@Sun.COM 		mb->b_bind_name = i_ddi_strdup(bind_name, KM_SLEEP);
5550Sstevel@tonic-gate 
5568831SJerry.Gilliam@Sun.COM 	/* Insert at head of hash */
5578831SJerry.Gilliam@Sun.COM 	pmb = &hashtab[nm_hash(name)];
5588831SJerry.Gilliam@Sun.COM 	mb->b_next = *pmb;
5598831SJerry.Gilliam@Sun.COM 	*pmb = mb;
5600Sstevel@tonic-gate 	return (0);
5610Sstevel@tonic-gate }
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate /*
5648831SJerry.Gilliam@Sun.COM  * Delete a binding from a binding-hash. Since there is no locking we
5658831SJerry.Gilliam@Sun.COM  * delete an mbind by making its b_num negative. We also support find_mbind
5668831SJerry.Gilliam@Sun.COM  * of deleted entries, so we still need deleted items on the list.
5670Sstevel@tonic-gate  */
5680Sstevel@tonic-gate void
delete_mbind(char * name,struct bind ** hashtab)5690Sstevel@tonic-gate delete_mbind(char *name, struct bind **hashtab)
5700Sstevel@tonic-gate {
5718831SJerry.Gilliam@Sun.COM 	struct bind	*mb;
5720Sstevel@tonic-gate 
5738831SJerry.Gilliam@Sun.COM 	for (mb = hashtab[nm_hash(name)]; mb; mb = mb->b_next) {
5748831SJerry.Gilliam@Sun.COM 		if ((mb->b_num >= 0) && (strcmp(name, mb->b_name) == 0)) {
5758831SJerry.Gilliam@Sun.COM 			/* delete by making b_num negative */
5768831SJerry.Gilliam@Sun.COM 			if (moddebug & MODDEBUG_BINDING) {
5778831SJerry.Gilliam@Sun.COM 				cmn_err(CE_CONT, "mbind: %s %d deleted\n",
5788831SJerry.Gilliam@Sun.COM 				    name, mb->b_num);
5790Sstevel@tonic-gate 			}
5808831SJerry.Gilliam@Sun.COM 			mb->b_num = -mb->b_num;
5818831SJerry.Gilliam@Sun.COM 			break;
5820Sstevel@tonic-gate 		}
5830Sstevel@tonic-gate 	}
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate 
5868831SJerry.Gilliam@Sun.COM /*
5878831SJerry.Gilliam@Sun.COM  * Delete all items in an mbind associated with specified num.
5888831SJerry.Gilliam@Sun.COM  * An example would be rem_drv deleting all aliases associated with a
5898831SJerry.Gilliam@Sun.COM  * driver major number.
5908831SJerry.Gilliam@Sun.COM  */
5918831SJerry.Gilliam@Sun.COM void
purge_mbind(int num,struct bind ** hashtab)5928831SJerry.Gilliam@Sun.COM purge_mbind(int num, struct bind **hashtab)
5938831SJerry.Gilliam@Sun.COM {
5948831SJerry.Gilliam@Sun.COM 	int		i;
5958831SJerry.Gilliam@Sun.COM 	struct bind	*mb;
5968831SJerry.Gilliam@Sun.COM 
5978831SJerry.Gilliam@Sun.COM 	/* search all hash lists for items that associated with 'num' */
5988831SJerry.Gilliam@Sun.COM 	for (i = 0; i < MOD_BIND_HASHSIZE; i++) {
5998831SJerry.Gilliam@Sun.COM 		for (mb = hashtab[i]; mb; mb = mb->b_next) {
6008831SJerry.Gilliam@Sun.COM 			if (mb->b_num == num) {
6018831SJerry.Gilliam@Sun.COM 				if (moddebug & MODDEBUG_BINDING)
6028831SJerry.Gilliam@Sun.COM 					cmn_err(CE_CONT,
6038831SJerry.Gilliam@Sun.COM 					    "mbind: %s %d purged\n",
6048831SJerry.Gilliam@Sun.COM 					    mb->b_name, num);
6058831SJerry.Gilliam@Sun.COM 				/* purge by changing the sign */
6068831SJerry.Gilliam@Sun.COM 				mb->b_num = -num;
6078831SJerry.Gilliam@Sun.COM 			}
6088831SJerry.Gilliam@Sun.COM 		}
6098831SJerry.Gilliam@Sun.COM 	}
6108831SJerry.Gilliam@Sun.COM }
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate major_t
mod_name_to_major(char * name)6130Sstevel@tonic-gate mod_name_to_major(char *name)
6140Sstevel@tonic-gate {
6158831SJerry.Gilliam@Sun.COM 	struct bind	*mbind;
6168831SJerry.Gilliam@Sun.COM 	major_t		maj;
6178831SJerry.Gilliam@Sun.COM 
6188831SJerry.Gilliam@Sun.COM 	/* Search for non-deleted match. */
6198831SJerry.Gilliam@Sun.COM 	if ((mbind = find_mbind(name, mb_hashtab, 0)) != NULL) {
6208831SJerry.Gilliam@Sun.COM 		if (moddebug & MODDEBUG_BINDING) {
6218831SJerry.Gilliam@Sun.COM 			if (find_mbind(name, mb_hashtab, 1))
6228831SJerry.Gilliam@Sun.COM 				cmn_err(CE_CONT,
6238831SJerry.Gilliam@Sun.COM 				    "'%s' has deleted match too\n", name);
6248831SJerry.Gilliam@Sun.COM 		}
6258831SJerry.Gilliam@Sun.COM 		return ((major_t)mbind->b_num);
6268831SJerry.Gilliam@Sun.COM 	}
6270Sstevel@tonic-gate 
6288831SJerry.Gilliam@Sun.COM 	/*
6298831SJerry.Gilliam@Sun.COM 	 * Search for deleted match: We may find that we have dependencies
6308831SJerry.Gilliam@Sun.COM 	 * on drivers that have been deleted (but the old driver may still
6318831SJerry.Gilliam@Sun.COM 	 * be bound to a node). These callers should be converted to use
6328831SJerry.Gilliam@Sun.COM 	 * ddi_driver_major(i.e. devi_major).
6338831SJerry.Gilliam@Sun.COM 	 */
6348831SJerry.Gilliam@Sun.COM 	if (moddebug & MODDEBUG_BINDING) {
6358831SJerry.Gilliam@Sun.COM 		if ((mbind = find_mbind(name, mb_hashtab, 1)) != NULL) {
6368831SJerry.Gilliam@Sun.COM 			maj = (major_t)(-(mbind->b_num));
6378831SJerry.Gilliam@Sun.COM 			cmn_err(CE_CONT, "Reference to deleted alias '%s' %d\n",
6388831SJerry.Gilliam@Sun.COM 			    name, maj);
6398831SJerry.Gilliam@Sun.COM 		}
6408831SJerry.Gilliam@Sun.COM 	}
6410Sstevel@tonic-gate 
6427009Scth 	return (DDI_MAJOR_T_NONE);
6430Sstevel@tonic-gate }
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate char *
mod_major_to_name(major_t major)6460Sstevel@tonic-gate mod_major_to_name(major_t major)
6470Sstevel@tonic-gate {
64810842SJerry.Gilliam@Sun.COM 	if (!driver_installed(major))
6490Sstevel@tonic-gate 		return (NULL);
6500Sstevel@tonic-gate 	return ((&devnamesp[major])->dn_name);
6510Sstevel@tonic-gate }
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate /*
6540Sstevel@tonic-gate  * Set up the devnames array.  Error check for duplicate entries.
6550Sstevel@tonic-gate  */
6560Sstevel@tonic-gate void
init_devnamesp(int size)6570Sstevel@tonic-gate init_devnamesp(int size)
6580Sstevel@tonic-gate {
6590Sstevel@tonic-gate 	int hshndx;
6600Sstevel@tonic-gate 	struct bind *bp;
6610Sstevel@tonic-gate 	static char dupwarn[] =
6620Sstevel@tonic-gate 	    "!Device entry \"%s %d\" conflicts with previous entry \"%s %d\" "
6630Sstevel@tonic-gate 	    "in /etc/name_to_major.";
6640Sstevel@tonic-gate 	static char badmaj[] = "The major number %u is invalid.";
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	ASSERT(size <= L_MAXMAJ32 && size > 0);
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	/*
6690Sstevel@tonic-gate 	 * Allocate the devnames array.  All mutexes and cv's will be
6700Sstevel@tonic-gate 	 * automagically initialized.
6710Sstevel@tonic-gate 	 */
6720Sstevel@tonic-gate 	devnamesp = kobj_zalloc(size * sizeof (struct devnames), KM_SLEEP);
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	/*
6750Sstevel@tonic-gate 	 * Stick the contents of mb_hashtab into the devnames array.  Warn if
6760Sstevel@tonic-gate 	 * two hash entries correspond to the same major number, or if a
6770Sstevel@tonic-gate 	 * major number is out of range.
6780Sstevel@tonic-gate 	 */
6790Sstevel@tonic-gate 	for (hshndx = 0; hshndx < MOD_BIND_HASHSIZE; hshndx++) {
6800Sstevel@tonic-gate 		for (bp = mb_hashtab[hshndx]; bp; bp = bp->b_next) {
68110842SJerry.Gilliam@Sun.COM 			if (make_devname(bp->b_name,
68210842SJerry.Gilliam@Sun.COM 			    (major_t)bp->b_num, 0) != 0) {
6830Sstevel@tonic-gate 				/*
6840Sstevel@tonic-gate 				 * If there is not an entry at b_num already,
6850Sstevel@tonic-gate 				 * then this must be a bad major number.
6860Sstevel@tonic-gate 				 */
6870Sstevel@tonic-gate 				char *nm = mod_major_to_name(bp->b_num);
6880Sstevel@tonic-gate 				if (nm == NULL) {
6890Sstevel@tonic-gate 					cmn_err(CE_WARN, badmaj,
6900Sstevel@tonic-gate 					    (uint_t)bp->b_num);
6910Sstevel@tonic-gate 				} else {
6920Sstevel@tonic-gate 					cmn_err(CE_WARN, dupwarn, bp->b_name,
6930Sstevel@tonic-gate 					    bp->b_num, nm, bp->b_num);
6940Sstevel@tonic-gate 				}
6950Sstevel@tonic-gate 			}
6960Sstevel@tonic-gate 		}
6970Sstevel@tonic-gate 	}
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	/* Initialize hash table for hwc_spec's */
7000Sstevel@tonic-gate 	hwc_hash_init();
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate int
make_devname(char * name,major_t major,int dn_flags)70410842SJerry.Gilliam@Sun.COM make_devname(char *name, major_t major, int dn_flags)
7050Sstevel@tonic-gate {
7060Sstevel@tonic-gate 	struct devnames *dnp;
7070Sstevel@tonic-gate 	char *copy;
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	/*
7100Sstevel@tonic-gate 	 * Until on-disk support for major nums > 14 bits arrives, fail
7110Sstevel@tonic-gate 	 * any major numbers that are too big.
7120Sstevel@tonic-gate 	 */
7130Sstevel@tonic-gate 	if (major > L_MAXMAJ32)
7140Sstevel@tonic-gate 		return (EINVAL);
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	dnp = &devnamesp[major];
7170Sstevel@tonic-gate 	LOCK_DEV_OPS(&dnp->dn_lock);
7180Sstevel@tonic-gate 	if (dnp->dn_name) {
7190Sstevel@tonic-gate 		if (strcmp(dnp->dn_name, name) != 0) {
7200Sstevel@tonic-gate 			/* Another driver already here */
7210Sstevel@tonic-gate 			UNLOCK_DEV_OPS(&dnp->dn_lock);
7220Sstevel@tonic-gate 			return (EINVAL);
7230Sstevel@tonic-gate 		}
7240Sstevel@tonic-gate 		/* Adding back a removed driver */
7250Sstevel@tonic-gate 		dnp->dn_flags &= ~DN_DRIVER_REMOVED;
72610842SJerry.Gilliam@Sun.COM 		dnp->dn_flags |= dn_flags;
7270Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
7280Sstevel@tonic-gate 		return (0);
7290Sstevel@tonic-gate 	}
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	/*
7320Sstevel@tonic-gate 	 * Check if flag is taken by getudev()
7330Sstevel@tonic-gate 	 */
7340Sstevel@tonic-gate 	if (dnp->dn_flags & DN_TAKEN_GETUDEV) {
7350Sstevel@tonic-gate 		UNLOCK_DEV_OPS(&dnp->dn_lock);
7360Sstevel@tonic-gate 		return (EINVAL);
7370Sstevel@tonic-gate 	}
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	copy = kmem_alloc(strlen(name) + 1, KM_SLEEP);
7400Sstevel@tonic-gate 	(void) strcpy(copy, name);
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	/* Make sure string is copied before setting dn_name */
7430Sstevel@tonic-gate 	membar_producer();
7440Sstevel@tonic-gate 	dnp->dn_name = copy;
74510842SJerry.Gilliam@Sun.COM 	dnp->dn_flags = dn_flags;
7460Sstevel@tonic-gate 	UNLOCK_DEV_OPS(&dnp->dn_lock);
7470Sstevel@tonic-gate 	return (0);
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate /*
7510Sstevel@tonic-gate  * Set up the syscallnames array.
7520Sstevel@tonic-gate  */
7530Sstevel@tonic-gate void
init_syscallnames(int size)7540Sstevel@tonic-gate init_syscallnames(int size)
7550Sstevel@tonic-gate {
7560Sstevel@tonic-gate 	int hshndx;
7570Sstevel@tonic-gate 	struct bind *bp;
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	syscallnames = kobj_zalloc(size * sizeof (char *), KM_SLEEP);
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 	for (hshndx = 0; hshndx < MOD_BIND_HASHSIZE; hshndx++) {
7620Sstevel@tonic-gate 		for (bp = sb_hashtab[hshndx]; bp; bp = bp->b_next) {
7636401Scg13442 			if (bp->b_num < 0 || bp->b_num >= size) {
7646401Scg13442 				cmn_err(CE_WARN,
7656401Scg13442 				    "!Couldn't add system call \"%s %d\". "
7666401Scg13442 				    "Value out of range (0..%d) in "
7676401Scg13442 				    "/etc/name_to_sysnum.",
7686401Scg13442 				    bp->b_name, bp->b_num, size - 1);
7696401Scg13442 				continue;
7706401Scg13442 			}
7710Sstevel@tonic-gate 			make_syscallname(bp->b_name, bp->b_num);
7720Sstevel@tonic-gate 		}
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate }
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate static void
make_syscallname(char * name,int sysno)7770Sstevel@tonic-gate make_syscallname(char *name, int sysno)
7780Sstevel@tonic-gate {
7790Sstevel@tonic-gate 	char **cp = &syscallnames[sysno];
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	if (*cp != NULL) {
7820Sstevel@tonic-gate 		cmn_err(CE_WARN, "!Couldn't add system call \"%s %d\". "
7830Sstevel@tonic-gate 		    "It conflicts with \"%s %d\" in /etc/name_to_sysnum.",
7840Sstevel@tonic-gate 		    name, sysno, *cp, sysno);
7850Sstevel@tonic-gate 		return;
7860Sstevel@tonic-gate 	}
7870Sstevel@tonic-gate 	*cp = kmem_alloc(strlen(name) + 1, KM_SLEEP);
7880Sstevel@tonic-gate 	(void) strcpy(*cp, name);
7890Sstevel@tonic-gate }
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate /*
7920Sstevel@tonic-gate  * Given a system call name, get its number.
7930Sstevel@tonic-gate  */
7940Sstevel@tonic-gate int
mod_getsysnum(char * name)7950Sstevel@tonic-gate mod_getsysnum(char *name)
7960Sstevel@tonic-gate {
7970Sstevel@tonic-gate 	struct bind *mbind;
7980Sstevel@tonic-gate 
7998831SJerry.Gilliam@Sun.COM 	if ((mbind = find_mbind(name, sb_hashtab, 0)) != NULL)
8000Sstevel@tonic-gate 		return (mbind->b_num);
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	return (-1);
8030Sstevel@tonic-gate }
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate /*
8060Sstevel@tonic-gate  * Given a system call number, get the system call name.
8070Sstevel@tonic-gate  */
8080Sstevel@tonic-gate char *
mod_getsysname(int sysnum)8090Sstevel@tonic-gate mod_getsysname(int sysnum)
8100Sstevel@tonic-gate {
8110Sstevel@tonic-gate 	return (syscallnames[sysnum]);
8120Sstevel@tonic-gate }
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate /*
8150Sstevel@tonic-gate  * Find the name of the module containing the specified pc.
8160Sstevel@tonic-gate  * Returns the name on success, "<unknown>" on failure.
8170Sstevel@tonic-gate  * No mod_lock locking is required because things are never deleted from
8180Sstevel@tonic-gate  * the &modules list.
8190Sstevel@tonic-gate  */
8200Sstevel@tonic-gate char *
mod_containing_pc(caddr_t pc)8210Sstevel@tonic-gate mod_containing_pc(caddr_t pc)
8220Sstevel@tonic-gate {
8230Sstevel@tonic-gate 	struct modctl	*mcp = &modules;
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate 	do {
8260Sstevel@tonic-gate 		if (mcp->mod_mp != NULL &&
8270Sstevel@tonic-gate 		    (size_t)pc - (size_t)mcp->mod_text < mcp->mod_text_size)
8280Sstevel@tonic-gate 			return (mcp->mod_modname);
8290Sstevel@tonic-gate 	} while ((mcp = mcp->mod_next) != &modules);
8300Sstevel@tonic-gate 	return ("<unknown>");
8310Sstevel@tonic-gate }
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate /*
8340Sstevel@tonic-gate  * Hash tables for hwc_spec
8350Sstevel@tonic-gate  *
8360Sstevel@tonic-gate  * The purpose of these hash tables are to allow the framework to discover
8370Sstevel@tonic-gate  * all possible .conf children for a given nexus. There are two hash tables.
8380Sstevel@tonic-gate  * One is hashed based on parent name, the on the class name. Each
8390Sstevel@tonic-gate  * driver.conf file translates to a list of hwc_spec's. Adding and
8400Sstevel@tonic-gate  * removing the entire list is an atomic operation, protected by
8410Sstevel@tonic-gate  * the hwc_hash_lock.
8420Sstevel@tonic-gate  *
8430Sstevel@tonic-gate  * What we get from all the hashing is the function hwc_get_child_spec().
8440Sstevel@tonic-gate  */
8450Sstevel@tonic-gate #define	HWC_SPEC_HASHSIZE	(1 << 6)	/* 64 */
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate static mod_hash_t *hwc_par_hash;	/* hash by parent name */
8480Sstevel@tonic-gate static mod_hash_t *hwc_class_hash;	/* hash by class name */
8490Sstevel@tonic-gate static kmutex_t hwc_hash_lock;		/* lock protecting hwc hashes */
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate /*
8520Sstevel@tonic-gate  * Initialize hash tables for parent and class specs
8530Sstevel@tonic-gate  */
8540Sstevel@tonic-gate static void
hwc_hash_init()8550Sstevel@tonic-gate hwc_hash_init()
8560Sstevel@tonic-gate {
8570Sstevel@tonic-gate 	hwc_par_hash = mod_hash_create_strhash("hwc parent spec hash",
8580Sstevel@tonic-gate 	    HWC_SPEC_HASHSIZE, mod_hash_null_valdtor);
8590Sstevel@tonic-gate 	hwc_class_hash = mod_hash_create_strhash("hwc class spec hash",
8600Sstevel@tonic-gate 	    HWC_SPEC_HASHSIZE, mod_hash_null_valdtor);
8610Sstevel@tonic-gate }
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate /*
8640Sstevel@tonic-gate  * Insert a spec into hash table. hwc_hash_lock must be held
8650Sstevel@tonic-gate  */
8660Sstevel@tonic-gate static void
hwc_hash_insert(struct hwc_spec * spec,char * name,mod_hash_t * hash)8670Sstevel@tonic-gate hwc_hash_insert(struct hwc_spec *spec, char *name, mod_hash_t *hash)
8680Sstevel@tonic-gate {
8690Sstevel@tonic-gate 	mod_hash_key_t key;
8700Sstevel@tonic-gate 	struct hwc_spec *entry = NULL;
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 	ASSERT(name != NULL);
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	if (mod_hash_find(hash, (mod_hash_key_t)name,
8750Sstevel@tonic-gate 	    (mod_hash_val_t)&entry) != 0) {
8760Sstevel@tonic-gate 		/* Name doesn't exist, insert a new key */
8770Sstevel@tonic-gate 		key = kmem_alloc(strlen(name) + 1, KM_SLEEP);
8780Sstevel@tonic-gate 		(void) strcpy((char *)key, name);
8790Sstevel@tonic-gate 		if (mod_hash_insert(hash, key, (mod_hash_val_t)spec) != 0) {
8800Sstevel@tonic-gate 			kmem_free(key, strlen(name) + 1);
8810Sstevel@tonic-gate 			cmn_err(CE_WARN, "hwc hash state inconsistent");
8820Sstevel@tonic-gate 		}
8830Sstevel@tonic-gate 		return;
8840Sstevel@tonic-gate 	}
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	/*
8870Sstevel@tonic-gate 	 * Name is already present, append spec to the list.
8880Sstevel@tonic-gate 	 * This is the case when driver.conf specifies multiple
8890Sstevel@tonic-gate 	 * nodes under a single parent or class.
8900Sstevel@tonic-gate 	 */
8910Sstevel@tonic-gate 	while (entry->hwc_hash_next)
8920Sstevel@tonic-gate 		entry = entry->hwc_hash_next;
8930Sstevel@tonic-gate 	entry->hwc_hash_next = spec;
8940Sstevel@tonic-gate }
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate /*
8970Sstevel@tonic-gate  * Remove a spec entry from spec hash table, the spec itself is
8980Sstevel@tonic-gate  * destroyed external to this function.
8990Sstevel@tonic-gate  */
9000Sstevel@tonic-gate static void
hwc_hash_remove(struct hwc_spec * spec,char * name,mod_hash_t * hash)9010Sstevel@tonic-gate hwc_hash_remove(struct hwc_spec *spec, char *name, mod_hash_t *hash)
9020Sstevel@tonic-gate {
9030Sstevel@tonic-gate 	char *key;
9040Sstevel@tonic-gate 	struct hwc_spec *entry;
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	ASSERT(name != NULL);
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 	if (mod_hash_find(hash, (mod_hash_key_t)name,
9090Sstevel@tonic-gate 	    (mod_hash_val_t)&entry) != 0) {
9100Sstevel@tonic-gate 		return;	/* name not found in hash */
9110Sstevel@tonic-gate 	}
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 	/*
9140Sstevel@tonic-gate 	 * If the head is the spec to be removed, either destroy the
9150Sstevel@tonic-gate 	 * entry or replace it with the remaining list.
9160Sstevel@tonic-gate 	 */
9170Sstevel@tonic-gate 	if (entry == spec) {
9180Sstevel@tonic-gate 		if (spec->hwc_hash_next == NULL) {
9190Sstevel@tonic-gate 			(void) mod_hash_destroy(hash, (mod_hash_key_t)name);
9200Sstevel@tonic-gate 			return;
9210Sstevel@tonic-gate 		}
9220Sstevel@tonic-gate 		key = kmem_alloc(strlen(name) + 1, KM_SLEEP);
9230Sstevel@tonic-gate 		(void) strcpy(key, name);
9240Sstevel@tonic-gate 		(void) mod_hash_replace(hash, (mod_hash_key_t)key,
9250Sstevel@tonic-gate 		    (mod_hash_val_t)spec->hwc_hash_next);
9260Sstevel@tonic-gate 		spec->hwc_hash_next = NULL;
9270Sstevel@tonic-gate 		return;
9280Sstevel@tonic-gate 	}
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	/*
9310Sstevel@tonic-gate 	 * If the head is not the one, look for the spec in the
9320Sstevel@tonic-gate 	 * hwc_hash_next linkage.
9330Sstevel@tonic-gate 	 */
9340Sstevel@tonic-gate 	while (entry->hwc_hash_next && (entry->hwc_hash_next != spec))
9350Sstevel@tonic-gate 		entry = entry->hwc_hash_next;
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	if (entry->hwc_hash_next) {
9380Sstevel@tonic-gate 		entry->hwc_hash_next = spec->hwc_hash_next;
9390Sstevel@tonic-gate 		spec->hwc_hash_next = NULL;
9400Sstevel@tonic-gate 	}
9410Sstevel@tonic-gate }
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate /*
9440Sstevel@tonic-gate  * Hash a list of specs based on either parent name or class name
9450Sstevel@tonic-gate  */
9460Sstevel@tonic-gate static void
hwc_hash(struct hwc_spec * spec_list,major_t major)9470Sstevel@tonic-gate hwc_hash(struct hwc_spec *spec_list, major_t major)
9480Sstevel@tonic-gate {
9490Sstevel@tonic-gate 	struct hwc_spec *spec = spec_list;
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	mutex_enter(&hwc_hash_lock);
9520Sstevel@tonic-gate 	while (spec) {
9530Sstevel@tonic-gate 		/* Put driver major here so parent can find it */
9540Sstevel@tonic-gate 		spec->hwc_major = major;
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 		if (spec->hwc_parent_name != NULL) {
9570Sstevel@tonic-gate 			hwc_hash_insert(spec, spec->hwc_parent_name,
9580Sstevel@tonic-gate 			    hwc_par_hash);
9590Sstevel@tonic-gate 		} else if (spec->hwc_class_name != NULL) {
9600Sstevel@tonic-gate 			hwc_hash_insert(spec, spec->hwc_class_name,
9610Sstevel@tonic-gate 			    hwc_class_hash);
9620Sstevel@tonic-gate 		} else {
9630Sstevel@tonic-gate 			cmn_err(CE_WARN,
9640Sstevel@tonic-gate 			    "hwc_hash: No class or parent specified");
9650Sstevel@tonic-gate 		}
9660Sstevel@tonic-gate 		spec = spec->hwc_next;
9670Sstevel@tonic-gate 	}
9680Sstevel@tonic-gate 	mutex_exit(&hwc_hash_lock);
9690Sstevel@tonic-gate }
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate /*
9720Sstevel@tonic-gate  * Remove a list of specs from hash tables. Don't destroy the specs yet.
9730Sstevel@tonic-gate  */
9740Sstevel@tonic-gate static void
hwc_unhash(struct hwc_spec * spec_list)9750Sstevel@tonic-gate hwc_unhash(struct hwc_spec *spec_list)
9760Sstevel@tonic-gate {
9770Sstevel@tonic-gate 	struct hwc_spec *spec = spec_list;
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 	mutex_enter(&hwc_hash_lock);
9800Sstevel@tonic-gate 	while (spec) {
9810Sstevel@tonic-gate 		if (spec->hwc_parent_name != NULL) {
9820Sstevel@tonic-gate 			hwc_hash_remove(spec, spec->hwc_parent_name,
9830Sstevel@tonic-gate 			    hwc_par_hash);
9840Sstevel@tonic-gate 		} else if (spec->hwc_class_name != NULL) {
9850Sstevel@tonic-gate 			hwc_hash_remove(spec, spec->hwc_class_name,
9860Sstevel@tonic-gate 			    hwc_class_hash);
9870Sstevel@tonic-gate 		} else {
9880Sstevel@tonic-gate 			cmn_err(CE_WARN,
9890Sstevel@tonic-gate 			    "hwc_unhash: No class or parent specified");
9900Sstevel@tonic-gate 		}
9910Sstevel@tonic-gate 		spec = spec->hwc_next;
9920Sstevel@tonic-gate 	}
9930Sstevel@tonic-gate 	mutex_exit(&hwc_hash_lock);
9940Sstevel@tonic-gate }
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate /*
9970Sstevel@tonic-gate  * Make a copy of specs in a hash entry and add to the end of listp.
9980Sstevel@tonic-gate  * Called by nexus to locate a list of child specs.
9990Sstevel@tonic-gate  *
10000Sstevel@tonic-gate  * entry is a list of hwc_spec chained together with hwc_hash_next.
10010Sstevel@tonic-gate  * listp points to list chained together with hwc_next.
10020Sstevel@tonic-gate  */
10030Sstevel@tonic-gate static void
hwc_spec_add(struct hwc_spec ** listp,struct hwc_spec * entry,major_t match_major)10040Sstevel@tonic-gate hwc_spec_add(struct hwc_spec **listp, struct hwc_spec *entry,
10050Sstevel@tonic-gate     major_t match_major)
10060Sstevel@tonic-gate {
10070Sstevel@tonic-gate 	/* Find the tail of the list */
10080Sstevel@tonic-gate 	while (*listp)
10090Sstevel@tonic-gate 		listp = &(*listp)->hwc_next;
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 	while (entry) {
10120Sstevel@tonic-gate 		struct hwc_spec *spec;
10130Sstevel@tonic-gate 
10147009Scth 		if ((match_major != DDI_MAJOR_T_NONE) &&
10150Sstevel@tonic-gate 		    (match_major != entry->hwc_major)) {
10160Sstevel@tonic-gate 			entry = entry->hwc_hash_next;
10170Sstevel@tonic-gate 			continue;
10180Sstevel@tonic-gate 		}
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 		/*
10210Sstevel@tonic-gate 		 * Allocate spec and copy the content of entry.
10220Sstevel@tonic-gate 		 * No need to copy class/parent name since caller
10230Sstevel@tonic-gate 		 * already knows the parent dip.
10240Sstevel@tonic-gate 		 */
10250Sstevel@tonic-gate 		spec = kmem_zalloc(sizeof (*spec), KM_SLEEP);
10260Sstevel@tonic-gate 		spec->hwc_devi_name = i_ddi_strdup(
10270Sstevel@tonic-gate 		    entry->hwc_devi_name, KM_SLEEP);
10280Sstevel@tonic-gate 		spec->hwc_major = entry->hwc_major;
10290Sstevel@tonic-gate 		spec->hwc_devi_sys_prop_ptr = i_ddi_prop_list_dup(
10300Sstevel@tonic-gate 		    entry->hwc_devi_sys_prop_ptr, KM_SLEEP);
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 		*listp = spec;
10330Sstevel@tonic-gate 		listp = &spec->hwc_next;
10340Sstevel@tonic-gate 		entry = entry->hwc_hash_next;
10350Sstevel@tonic-gate 	}
10360Sstevel@tonic-gate }
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate /*
10390Sstevel@tonic-gate  * Given a dip, find the list of child .conf specs from most specific
10400Sstevel@tonic-gate  * (parent pathname) to least specific (class name).
10410Sstevel@tonic-gate  *
10420Sstevel@tonic-gate  * This function allows top-down loading to be implemented without
10430Sstevel@tonic-gate  * changing the format of driver.conf file.
10440Sstevel@tonic-gate  */
10450Sstevel@tonic-gate struct hwc_spec *
hwc_get_child_spec(dev_info_t * dip,major_t match_major)10460Sstevel@tonic-gate hwc_get_child_spec(dev_info_t *dip, major_t match_major)
10470Sstevel@tonic-gate {
10480Sstevel@tonic-gate 	extern char *i_ddi_parname(dev_info_t *, char *);
10490Sstevel@tonic-gate 	extern int i_ddi_get_exported_classes(dev_info_t *, char ***);
10500Sstevel@tonic-gate 	extern void i_ddi_free_exported_classes(char **, int);
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	int i, nclass;
10530Sstevel@tonic-gate 	char **classes;
10540Sstevel@tonic-gate 	struct hwc_spec *list = NULL;
10550Sstevel@tonic-gate 	mod_hash_val_t val;
10560Sstevel@tonic-gate 	char *parname, *parname_buf;
10570Sstevel@tonic-gate 	char *deviname, *deviname_buf;
10580Sstevel@tonic-gate 	char *pathname, *pathname_buf;
10590Sstevel@tonic-gate 	char *bindname;
10600Sstevel@tonic-gate 	char *drvname;
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	pathname_buf = kmem_alloc(3 * MAXPATHLEN, KM_SLEEP);
10630Sstevel@tonic-gate 	deviname_buf = pathname_buf + MAXPATHLEN;
10640Sstevel@tonic-gate 	parname_buf = pathname_buf + (2 * MAXPATHLEN);
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	mutex_enter(&hwc_hash_lock);
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	/*
10690Sstevel@tonic-gate 	 * Lookup based on full path.
10700Sstevel@tonic-gate 	 * In the case of root node, ddi_pathname would return
10710Sstevel@tonic-gate 	 * null string so just skip calling it.
10720Sstevel@tonic-gate 	 * As the pathname always begins with /, no simpler
10730Sstevel@tonic-gate 	 * name can duplicate it.
10740Sstevel@tonic-gate 	 */
10750Sstevel@tonic-gate 	pathname = (dip == ddi_root_node()) ? "/" :
10764452Scth 	    ddi_pathname(dip, pathname_buf);
10770Sstevel@tonic-gate 	ASSERT(pathname != NULL);
10780Sstevel@tonic-gate 	ASSERT(*pathname == '/');
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	if (mod_hash_find(hwc_par_hash, (mod_hash_key_t)pathname, &val) == 0) {
10810Sstevel@tonic-gate 		hwc_spec_add(&list, (struct hwc_spec *)val, match_major);
10820Sstevel@tonic-gate 	}
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 	/*
10850Sstevel@tonic-gate 	 * Lookup nodename@address.
10860Sstevel@tonic-gate 	 * Note deviname cannot match pathname.
10870Sstevel@tonic-gate 	 */
10880Sstevel@tonic-gate 	deviname = ddi_deviname(dip, deviname_buf);
10890Sstevel@tonic-gate 	if (*deviname != '\0') {
10900Sstevel@tonic-gate 		/*
10910Sstevel@tonic-gate 		 * Skip leading / returned by ddi_deviname.
10920Sstevel@tonic-gate 		 */
10930Sstevel@tonic-gate 		ASSERT(*deviname == '/');
10940Sstevel@tonic-gate 		deviname++;
10954452Scth 		if ((*deviname != '\0') && (mod_hash_find(hwc_par_hash,
10964452Scth 		    (mod_hash_key_t)deviname, &val) == 0))
10974452Scth 			hwc_spec_add(&list,
10984452Scth 			    (struct hwc_spec *)val, match_major);
10990Sstevel@tonic-gate 	}
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 	/*
11020Sstevel@tonic-gate 	 * Lookup bindingname@address.
11030Sstevel@tonic-gate 	 * Take care not to perform duplicate lookups.
11040Sstevel@tonic-gate 	 */
11050Sstevel@tonic-gate 	parname = i_ddi_parname(dip, parname_buf);
11060Sstevel@tonic-gate 	if (*parname != '\0') {
11070Sstevel@tonic-gate 		ASSERT(*parname != '/');
11080Sstevel@tonic-gate 		if ((strcmp(parname, deviname) != 0) &&
11094452Scth 		    (mod_hash_find(hwc_par_hash,
11104452Scth 		    (mod_hash_key_t)parname, &val) == 0)) {
11114452Scth 			hwc_spec_add(&list,
11124452Scth 			    (struct hwc_spec *)val, match_major);
11130Sstevel@tonic-gate 		}
11140Sstevel@tonic-gate 	}
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	/*
11170Sstevel@tonic-gate 	 * Lookup driver binding name
11180Sstevel@tonic-gate 	 */
11190Sstevel@tonic-gate 	bindname = ddi_binding_name(dip);
11200Sstevel@tonic-gate 	ASSERT(*bindname != '/');
11210Sstevel@tonic-gate 	if ((strcmp(bindname, parname) != 0) &&
11220Sstevel@tonic-gate 	    (strcmp(bindname, deviname) != 0) &&
11230Sstevel@tonic-gate 	    (mod_hash_find(hwc_par_hash, (mod_hash_key_t)bindname, &val) == 0))
11240Sstevel@tonic-gate 		hwc_spec_add(&list, (struct hwc_spec *)val, match_major);
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	/*
11270Sstevel@tonic-gate 	 * Lookup driver name
11280Sstevel@tonic-gate 	 */
11290Sstevel@tonic-gate 	drvname = (char *)ddi_driver_name(dip);
11300Sstevel@tonic-gate 	ASSERT(*drvname != '/');
11310Sstevel@tonic-gate 	if ((strcmp(drvname, bindname) != 0) &&
11320Sstevel@tonic-gate 	    (strcmp(drvname, parname) != 0) &&
11330Sstevel@tonic-gate 	    (strcmp(drvname, deviname) != 0) &&
11340Sstevel@tonic-gate 	    (mod_hash_find(hwc_par_hash, (mod_hash_key_t)drvname, &val) == 0))
11350Sstevel@tonic-gate 		hwc_spec_add(&list, (struct hwc_spec *)val, match_major);
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	kmem_free(pathname_buf, 3 * MAXPATHLEN);
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	/*
11400Sstevel@tonic-gate 	 * Lookup classes exported by this node and lookup the
11410Sstevel@tonic-gate 	 * class hash table for all .conf specs
11420Sstevel@tonic-gate 	 */
11430Sstevel@tonic-gate 	nclass = i_ddi_get_exported_classes(dip, &classes);
11440Sstevel@tonic-gate 	for (i = 0; i < nclass; i++) {
11450Sstevel@tonic-gate 		if (mod_hash_find(hwc_class_hash, (mod_hash_key_t)classes[i],
11460Sstevel@tonic-gate 		    &val) == 0)
11470Sstevel@tonic-gate 			hwc_spec_add(&list, (struct hwc_spec *)val,
11480Sstevel@tonic-gate 			    match_major);
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate 	i_ddi_free_exported_classes(classes, nclass);
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	mutex_exit(&hwc_hash_lock);
11530Sstevel@tonic-gate 	return (list);
11540Sstevel@tonic-gate }
1155