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