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