xref: /illumos-gate/usr/src/uts/common/io/suntpi.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include	<sys/types.h>
28 #include	<sys/kmem.h>
29 #include	<sys/bitmap.h>
30 #include	<sys/stream.h>
31 #include	<sys/strsubr.h>
32 #define	_SUN_TPI_VERSION	2
33 #include	<sys/tihdr.h>
34 #include	<sys/suntpi.h>
35 
36 /*
37  * Hash table parameters for tpi_provinfo_table.
38  */
39 #define	TPI_HASH_BITS	4
40 #define	TPI_NHASH	(1 << TPI_HASH_BITS)
41 
42 /*
43  * Use the first element in the key for the hash.
44  */
45 #define	TPI_HASH(p)	((((uintptr_t *)p)[0] >> tpi_hashshift) % TPI_NHASH)
46 /*
47  * SAMESTR is a very confusing name. LAST_QUEUE is introduced for readability.
48  */
49 #define	LAST_QUEUE(q)	(!SAMESTR(q))
50 
51 static tpi_provinfo_t	*tpi_provinfo_table[TPI_NHASH];
52 static kmutex_t		tpi_provinfo_lock;
53 static int		tpi_hashshift;
54 
55 /*
56  * In most cases there is some transport provider (like tcp or udp) below
57  * transport user (like timod or sockets). However, it is possible to construct
58  * stream without transport provider (e.g. by pushing timod into FIFO). It is
59  * hardly of any use, but this condition was observed with sparcv9 abi tests.
60  * To count for such special case, a special tpi_nullprov static data is
61  * provided to cache information about such degenerated null-transport case.
62  */
63 static tpi_provinfo_t	tpi_nullprov;	/* Placeholder for null transport */
64 
65 /*
66  * Initialise the TPI support routines.  Called from strinit().
67  */
68 void
69 tpi_init()
70 {
71 	mutex_init(&tpi_provinfo_lock, NULL, MUTEX_DEFAULT, NULL);
72 
73 	/*
74 	 * Calculate the right shift for hashing a tpi_provinfo_t.
75 	 */
76 	tpi_hashshift = highbit(sizeof (tpi_provinfo_t));
77 }
78 
79 /*
80  * Generate a downstream signature given the write-side queue.  It
81  * passes back the size of the generated key in *keylenp.  This routine
82  * cannot multithread as it returns a pointer to a static data item.
83  *
84  * There is no way (in the current module loading infrastructure) to
85  * _absolutely_ guarantee that the key below uniquely identifies an
86  * arrangement of modules and drivers.  A module _might_ be unloaded and
87  * another module _might_ be loaded such that the qi_minfo is at _exactly_
88  * same kernel address, and then it _might_ be placed in a transport
89  * provider stream in exactly the same configuration (modules above and
90  * below all identical) - but it would take quite a few coincidences
91  * and modules loading and unloading does not usually happen n times a
92  * second...
93  */
94 static void	*
95 tpi_makekey(queue_t *q, size_t *keylenp)
96 {
97 	static uintptr_t	*key	= NULL;
98 	int			i;
99 
100 	ASSERT(q != NULL);
101 	ASSERT(MUTEX_HELD(&tpi_provinfo_lock));
102 
103 	/* assert this queue is write queue and qprocson() is called before */
104 	ASSERT((q->q_flag & QREADR) == 0);
105 	ASSERT(q->q_next != NULL);
106 
107 	/*
108 	 * This can be global because tpi_makekey is called with
109 	 * tpi_provinfo_lock.
110 	 */
111 	if (key == NULL)
112 		key = kmem_alloc((nstrpush + 1) * sizeof (uintptr_t), KM_SLEEP);
113 
114 	ASSERT(key != NULL);
115 
116 	/*
117 	 * Go down q_next to the driver, but no further.  We use the qi_minfo
118 	 * because we can find in from the queue and it is a stable part of
119 	 * any driver/module infrastructure.
120 	 */
121 	for (i = 0; !LAST_QUEUE(q) && (q = q->q_next) != NULL; ++i) {
122 		ASSERT(i < nstrpush + 1);
123 		key[i] = (uintptr_t)q->q_qinfo->qi_minfo;
124 	}
125 
126 	/*
127 	 * Allocate the actual key with the proper length, and pass it
128 	 * all back.
129 	 */
130 	*keylenp = i * sizeof (uintptr_t);
131 	return ((void *)key);
132 }
133 
134 /*
135  * Find an existing provider entry given a queue pointer, or allocate a
136  * new empty entry if not found.  Because this routine calls kmem_alloc
137  * with KM_SLEEP, and because it traverses the q_next pointers of a stream
138  * it must be called with a proper user context and within a perimeter
139  * which protects the STREAM e.g. an open routine.  This routine always
140  * returns a valid pointer.
141  */
142 tpi_provinfo_t	*
143 tpi_findprov(queue_t *q)
144 {
145 	void		*key;
146 	size_t		keylen;
147 	tpi_provinfo_t	**tpp;
148 
149 	mutex_enter(&tpi_provinfo_lock);
150 
151 	/*
152 	 * Must hold tpi_provinfo_lock since tpi_makekey() returns a pointer
153 	 * to static data.
154 	 */
155 	key = tpi_makekey(WR(q), &keylen);
156 
157 	if (keylen == 0) {
158 		/* there is nothing below us, return special nullprov entry */
159 		mutex_exit(&tpi_provinfo_lock);
160 		return (&tpi_nullprov);
161 	}
162 
163 	/*
164 	 * Look for an existing entry, or the place to put a new one.
165 	 */
166 	for (tpp = &tpi_provinfo_table[TPI_HASH(key)]; *tpp != NULL;
167 	    tpp = &(*tpp)->tpi_next) {
168 		if ((*tpp)->tpi_keylen == keylen &&
169 		    bcmp((*tpp)->tpi_key, key, keylen) == 0) {
170 			mutex_exit(&tpi_provinfo_lock);
171 			return (*tpp);
172 		}
173 	}
174 
175 	/*
176 	 * Allocate and fill in the new tpi_provinfo_t.
177 	 */
178 	*tpp = kmem_zalloc(sizeof (tpi_provinfo_t), KM_SLEEP);
179 	(*tpp)->tpi_key = kmem_alloc(keylen, KM_SLEEP);
180 	bcopy(key, (*tpp)->tpi_key, keylen);
181 	(*tpp)->tpi_keylen = keylen;
182 	mutex_init(&(*tpp)->tpi_lock, NULL, MUTEX_DEFAULT, NULL);
183 
184 	mutex_exit(&tpi_provinfo_lock);
185 	return (*tpp);
186 }
187 
188 /*
189  * Allocate a TPI ACK reusing the old message if possible.
190  */
191 mblk_t	*
192 tpi_ack_alloc(mblk_t *mp, size_t size, uchar_t db_type, t_scalar_t prim)
193 {
194 	mblk_t	*omp	= mp;
195 
196 	if ((mp = reallocb(mp, size, 0)) == NULL) {
197 		freemsg(omp);
198 		return (NULL);
199 	}
200 	if (mp->b_cont != NULL) {
201 		freemsg(mp->b_cont);
202 		mp->b_cont = NULL;
203 	}
204 	mp->b_datap->db_type = db_type;
205 	mp->b_wptr = mp->b_rptr + size;
206 	((union T_primitives *)mp->b_rptr)->type = prim;
207 	return (mp);
208 }
209