xref: /titanic_50/usr/src/uts/common/io/str_conf.c (revision 1a5e258f5471356ca102c7176637cdce45bac147)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/param.h>
297c478bd9Sstevel@tonic-gate #include <sys/systm.h>
307c478bd9Sstevel@tonic-gate #include <sys/conf.h>
317c478bd9Sstevel@tonic-gate #include <sys/stream.h>
327c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
337c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
347c478bd9Sstevel@tonic-gate #include <sys/modhash.h>
357c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
387c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
397c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * This module provides the framework that manage STREAMS modules.
437c478bd9Sstevel@tonic-gate  * fmodsw_alloc() is called from modconf.c as a result of a module calling
447c478bd9Sstevel@tonic-gate  * mod_install() and fmodsw_free() is called as the result of the module
457c478bd9Sstevel@tonic-gate  * calling mod_remove().
467c478bd9Sstevel@tonic-gate  * fmodsw_find() will find the fmodsw_impl_t structure relating to a named
477c478bd9Sstevel@tonic-gate  * module. There is no equivalent of driver major numbers for modules; the
487c478bd9Sstevel@tonic-gate  * the database of fmodsw_impl_t structures is purely keyed by name and
497c478bd9Sstevel@tonic-gate  * is hence a hash table to keep lookup cost to a minimum.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * fmodsw_hash is the hash table that will be used to map module names to
547c478bd9Sstevel@tonic-gate  * their fmodsw_impl_t structures. The hash function requires that the value is
557c478bd9Sstevel@tonic-gate  * a power of 2 so this definition specifies the log of the hash table size.
567c478bd9Sstevel@tonic-gate  */
577c478bd9Sstevel@tonic-gate #define	FMODSW_LOG_HASHSZ	8
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * Hash table and associated reader-writer lock
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  * NOTE: Because the lock is global data, it is initialized to zero and hence
637c478bd9Sstevel@tonic-gate  *       a call to rw_init() is not required. Similarly all the pointers in
647c478bd9Sstevel@tonic-gate  *       the hash table will be implicitly initialized to NULL.
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate #define	FMODSW_HASHSZ		(1 << FMODSW_LOG_HASHSZ)
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate static fmodsw_impl_t	*fmodsw_hash[FMODSW_HASHSZ];
697c478bd9Sstevel@tonic-gate static krwlock_t	fmodsw_lock;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * Debug code:
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  * This is not conditionally compiled since it may be useful to third
757c478bd9Sstevel@tonic-gate  * parties when developing new modules.
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #define	BUFSZ	512
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #define	FMODSW_INIT		0x00000001
817c478bd9Sstevel@tonic-gate #define	FMODSW_REGISTER		0x00000002
827c478bd9Sstevel@tonic-gate #define	FMODSW_UNREGISTER	0x00000004
837c478bd9Sstevel@tonic-gate #define	FMODSW_FIND		0x00000008
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate uint32_t	fmodsw_debug_flags = 0x00000000;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static void fmodsw_dprintf(uint_t flag, const char *fmt, ...) __KPRINTFLIKE(2);
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /* PRINTFLIKE2 */
907c478bd9Sstevel@tonic-gate static void
i_fmodsw_dprintf(uint_t flag,const char * fmt,...)917c478bd9Sstevel@tonic-gate i_fmodsw_dprintf(uint_t flag, const char *fmt, ...)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	va_list	alist;
947c478bd9Sstevel@tonic-gate 	char	buf[BUFSZ + 1];
957c478bd9Sstevel@tonic-gate 	char	*ptr;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	if (fmodsw_debug_flags & flag) {
987c478bd9Sstevel@tonic-gate 		va_start(alist, fmt);
997c478bd9Sstevel@tonic-gate 		ptr = buf;
1007c478bd9Sstevel@tonic-gate 		(void) sprintf(ptr, "strmod debug: ");
1017c478bd9Sstevel@tonic-gate 		ptr += strlen(buf);
1027c478bd9Sstevel@tonic-gate 		(void) vsnprintf(ptr, buf + BUFSZ - ptr, fmt, alist);
1037c478bd9Sstevel@tonic-gate 		printf(buf);
1047c478bd9Sstevel@tonic-gate 		va_end(alist);
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate  * Local functions:
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate #define	FMODSW_HASH(_key) \
1147c478bd9Sstevel@tonic-gate 	(uint_t)(((_key[0] << 4) | (_key[1] & 0x0f)) & (FMODSW_HASHSZ - 1))
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate #define	FMODSW_KEYCMP(_k1, _k2, _match)					\
1177c478bd9Sstevel@tonic-gate 	{								\
1187c478bd9Sstevel@tonic-gate 		char	*p1 = (char *)(_k1);				\
1197c478bd9Sstevel@tonic-gate 		char	*p2 = (char *)(_k2);				\
1207c478bd9Sstevel@tonic-gate 									\
1217c478bd9Sstevel@tonic-gate 		while (*p1 == *p2++) {					\
1227c478bd9Sstevel@tonic-gate 			if (*p1++ == '\0') {				\
1237c478bd9Sstevel@tonic-gate 				goto _match;				\
1247c478bd9Sstevel@tonic-gate 			}						\
1257c478bd9Sstevel@tonic-gate 		}							\
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate static int
i_fmodsw_hash_insert(fmodsw_impl_t * fp)1297c478bd9Sstevel@tonic-gate i_fmodsw_hash_insert(fmodsw_impl_t *fp)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	uint_t		bucket;
1327c478bd9Sstevel@tonic-gate 	fmodsw_impl_t	**pp;
1337c478bd9Sstevel@tonic-gate 	fmodsw_impl_t	*p;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	ASSERT(rw_write_held(&fmodsw_lock));
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	bucket = FMODSW_HASH(fp->f_name);
1387c478bd9Sstevel@tonic-gate 	for (pp = &(fmodsw_hash[bucket]); (p = *pp) != NULL;
1397c478bd9Sstevel@tonic-gate 	    pp = &(p->f_next))
1407c478bd9Sstevel@tonic-gate 		FMODSW_KEYCMP(p->f_name, fp->f_name, found);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	fp->f_next = p;
1437c478bd9Sstevel@tonic-gate 	*pp = fp;
1447c478bd9Sstevel@tonic-gate 	return (0);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate found:
1477c478bd9Sstevel@tonic-gate 	return (EEXIST);
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate static int
i_fmodsw_hash_remove(const char * name,fmodsw_impl_t ** fpp)1517c478bd9Sstevel@tonic-gate i_fmodsw_hash_remove(const char *name, fmodsw_impl_t **fpp)
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate 	uint_t		bucket;
1547c478bd9Sstevel@tonic-gate 	fmodsw_impl_t	**pp;
1557c478bd9Sstevel@tonic-gate 	fmodsw_impl_t	*p;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	ASSERT(rw_write_held(&fmodsw_lock));
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	bucket = FMODSW_HASH(name);
1607c478bd9Sstevel@tonic-gate 	for (pp = &(fmodsw_hash[bucket]); (p = *pp) != NULL;
1617c478bd9Sstevel@tonic-gate 	    pp = &(p->f_next))
1627c478bd9Sstevel@tonic-gate 		FMODSW_KEYCMP(p->f_name, name, found);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	return (ENOENT);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate found:
1677c478bd9Sstevel@tonic-gate 	if (p->f_ref > 0)
1687c478bd9Sstevel@tonic-gate 		return (EBUSY);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	*pp = p->f_next;
1717c478bd9Sstevel@tonic-gate 	*fpp = p;
1727c478bd9Sstevel@tonic-gate 	return (0);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate static int
i_fmodsw_hash_find(const char * name,fmodsw_impl_t ** fpp)1767c478bd9Sstevel@tonic-gate i_fmodsw_hash_find(const char *name, fmodsw_impl_t **fpp)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	uint_t		bucket;
1797c478bd9Sstevel@tonic-gate 	fmodsw_impl_t	*p;
1807c478bd9Sstevel@tonic-gate 	int		rc = 0;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	ASSERT(rw_read_held(&fmodsw_lock));
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	bucket = FMODSW_HASH(name);
1857c478bd9Sstevel@tonic-gate 	for (p = fmodsw_hash[bucket]; p != NULL; p = p->f_next)
1867c478bd9Sstevel@tonic-gate 		FMODSW_KEYCMP(p->f_name, name, found);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	rc = ENOENT;
1897c478bd9Sstevel@tonic-gate found:
1907c478bd9Sstevel@tonic-gate 	*fpp = p;
1917c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1927c478bd9Sstevel@tonic-gate 	if (p != NULL)
1937c478bd9Sstevel@tonic-gate 		p->f_hits++;
1947c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	return (rc);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate /*
2017c478bd9Sstevel@tonic-gate  * Exported functions:
2027c478bd9Sstevel@tonic-gate  */
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate int
fmodsw_register(const char * name,struct streamtab * str,int flag)2057c478bd9Sstevel@tonic-gate fmodsw_register(const char *name, struct streamtab *str, int flag)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	fmodsw_impl_t	*fp;
2087c478bd9Sstevel@tonic-gate 	int		len;
2097c478bd9Sstevel@tonic-gate 	int		err;
2107c478bd9Sstevel@tonic-gate 	uint_t	qflag;
2117c478bd9Sstevel@tonic-gate 	uint_t	sqtype;
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	if ((len = strlen(name)) > FMNAMESZ)
2147c478bd9Sstevel@tonic-gate 		return (EINVAL);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	if ((fp = kmem_zalloc(sizeof (fmodsw_impl_t), KM_NOSLEEP)) == NULL)
2177c478bd9Sstevel@tonic-gate 		return (ENOMEM);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	(void) strncpy(fp->f_name, name, len);
2207c478bd9Sstevel@tonic-gate 	fp->f_name[len] = '\0';
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	if ((err = devflg_to_qflag(str, flag, &qflag, &sqtype)) != 0)
2237c478bd9Sstevel@tonic-gate 		goto failed;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	fp->f_str = str;
2267c478bd9Sstevel@tonic-gate 	fp->f_qflag = qflag;
2277c478bd9Sstevel@tonic-gate 	fp->f_sqtype = sqtype;
2287c478bd9Sstevel@tonic-gate 	if (qflag & (QPERMOD | QMTOUTPERIM))
2297c478bd9Sstevel@tonic-gate 		fp->f_dmp = hold_dm(str, qflag, sqtype);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	rw_enter(&fmodsw_lock, RW_WRITER);
2327c478bd9Sstevel@tonic-gate 	if ((err = i_fmodsw_hash_insert(fp)) != 0) {
2337c478bd9Sstevel@tonic-gate 		rw_exit(&fmodsw_lock);
2347c478bd9Sstevel@tonic-gate 		goto failed;
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 	rw_exit(&fmodsw_lock);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	i_fmodsw_dprintf(FMODSW_REGISTER, "registered module '%s'\n", name);
2397c478bd9Sstevel@tonic-gate 	return (0);
2407c478bd9Sstevel@tonic-gate failed:
2417c478bd9Sstevel@tonic-gate 	i_fmodsw_dprintf(FMODSW_REGISTER, "failed to register module '%s'\n",
2427c478bd9Sstevel@tonic-gate 	    name);
2437c478bd9Sstevel@tonic-gate 	if (fp->f_dmp != NULL)
2447c478bd9Sstevel@tonic-gate 		rele_dm(fp->f_dmp);
2457c478bd9Sstevel@tonic-gate 	kmem_free(fp, sizeof (fmodsw_impl_t));
2467c478bd9Sstevel@tonic-gate 	return (err);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate int
fmodsw_unregister(const char * name)2507c478bd9Sstevel@tonic-gate fmodsw_unregister(const char *name)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	fmodsw_impl_t	*fp;
2537c478bd9Sstevel@tonic-gate 	int		err;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	rw_enter(&fmodsw_lock, RW_WRITER);
2567c478bd9Sstevel@tonic-gate 	if ((err = i_fmodsw_hash_remove(name, &fp)) != 0) {
2577c478bd9Sstevel@tonic-gate 		rw_exit(&fmodsw_lock);
2587c478bd9Sstevel@tonic-gate 		goto failed;
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 	rw_exit(&fmodsw_lock);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	if (fp->f_dmp != NULL)
2637c478bd9Sstevel@tonic-gate 		rele_dm(fp->f_dmp);
2647c478bd9Sstevel@tonic-gate 	kmem_free(fp, sizeof (fmodsw_impl_t));
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	i_fmodsw_dprintf(FMODSW_UNREGISTER, "unregistered module '%s'\n",
2677c478bd9Sstevel@tonic-gate 	    name);
2687c478bd9Sstevel@tonic-gate 	return (0);
2697c478bd9Sstevel@tonic-gate failed:
2707c478bd9Sstevel@tonic-gate 	i_fmodsw_dprintf(FMODSW_UNREGISTER, "failed to unregister module "
2717c478bd9Sstevel@tonic-gate 	    "'%s'\n", name);
2727c478bd9Sstevel@tonic-gate 	return (err);
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate fmodsw_impl_t *
fmodsw_find(const char * name,fmodsw_flags_t flags)2767c478bd9Sstevel@tonic-gate fmodsw_find(const char *name, fmodsw_flags_t flags)
2777c478bd9Sstevel@tonic-gate {
2787c478bd9Sstevel@tonic-gate 	fmodsw_impl_t	*fp;
2797c478bd9Sstevel@tonic-gate 	int		id;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate try_again:
2827c478bd9Sstevel@tonic-gate 	rw_enter(&fmodsw_lock, RW_READER);
2837c478bd9Sstevel@tonic-gate 	if (i_fmodsw_hash_find(name, &fp) == 0) {
2847c478bd9Sstevel@tonic-gate 		if (flags & FMODSW_HOLD) {
285*1a5e258fSJosef 'Jeff' Sipek 			atomic_inc_32(&(fp->f_ref));	/* lock must be held */
2867c478bd9Sstevel@tonic-gate 			ASSERT(fp->f_ref > 0);
2877c478bd9Sstevel@tonic-gate 		}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 		rw_exit(&fmodsw_lock);
2907c478bd9Sstevel@tonic-gate 		return (fp);
2917c478bd9Sstevel@tonic-gate 	}
2927c478bd9Sstevel@tonic-gate 	rw_exit(&fmodsw_lock);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	if (flags & FMODSW_LOAD) {
2957c478bd9Sstevel@tonic-gate 		if ((id = modload("strmod", (char *)name)) != -1) {
2967c478bd9Sstevel@tonic-gate 			i_fmodsw_dprintf(FMODSW_FIND,
2977c478bd9Sstevel@tonic-gate 			    "module '%s' loaded: id = %d\n", name, id);
2987c478bd9Sstevel@tonic-gate 			goto try_again;
2997c478bd9Sstevel@tonic-gate 		}
3007c478bd9Sstevel@tonic-gate 	}
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	return (NULL);
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate void
fmodsw_rele(fmodsw_impl_t * fp)3067c478bd9Sstevel@tonic-gate fmodsw_rele(fmodsw_impl_t *fp)
3077c478bd9Sstevel@tonic-gate {
3087c478bd9Sstevel@tonic-gate 	ASSERT(fp->f_ref > 0);
309*1a5e258fSJosef 'Jeff' Sipek 	atomic_dec_32(&(fp->f_ref));
3107c478bd9Sstevel@tonic-gate }
311