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