summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/suntpi.c
blob: 698fc90cd1186148cf0c656a3114567c88f97152 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include	<sys/types.h>
#include	<sys/kmem.h>
#include	<sys/bitmap.h>
#include	<sys/stream.h>
#include	<sys/strsubr.h>
#define	_SUN_TPI_VERSION	2
#include	<sys/tihdr.h>
#include	<sys/suntpi.h>

/*
 * Hash table parameters for tpi_provinfo_table.
 */
#define	TPI_HASH_BITS	4
#define	TPI_NHASH	(1 << TPI_HASH_BITS)

/*
 * Use the first element in the key for the hash.
 */
#define	TPI_HASH(p)	((((uintptr_t *)p)[0] >> tpi_hashshift) % TPI_NHASH)
/*
 * SAMESTR is a very confusing name. LAST_QUEUE is introduced for readability.
 */
#define	LAST_QUEUE(q)	(!SAMESTR(q))

static tpi_provinfo_t	*tpi_provinfo_table[TPI_NHASH];
static kmutex_t		tpi_provinfo_lock;
static int		tpi_hashshift;

/*
 * In most cases there is some transport provider (like tcp or udp) below
 * transport user (like timod or sockets). However, it is possible to construct
 * stream without transport provider (e.g. by pushing timod into FIFO). It is
 * hardly of any use, but this condition was observed with sparcv9 abi tests.
 * To count for such special case, a special tpi_nullprov static data is
 * provided to cache information about such degenerated null-transport case.
 */
static tpi_provinfo_t	tpi_nullprov;	/* Placeholder for null transport */

/*
 * Initialise the TPI support routines.  Called from strinit().
 */
void
tpi_init()
{
	mutex_init(&tpi_provinfo_lock, NULL, MUTEX_DEFAULT, NULL);

	/*
	 * Calculate the right shift for hashing a tpi_provinfo_t.
	 */
	tpi_hashshift = highbit(sizeof (tpi_provinfo_t));
}

/*
 * Generate a downstream signature given the write-side queue.  It
 * passes back the size of the generated key in *keylenp.  This routine
 * cannot multithread as it returns a pointer to a static data item.
 *
 * There is no way (in the current module loading infrastructure) to
 * _absolutely_ guarantee that the key below uniquely identifies an
 * arrangement of modules and drivers.  A module _might_ be unloaded and
 * another module _might_ be loaded such that the qi_minfo is at _exactly_
 * same kernel address, and then it _might_ be placed in a transport
 * provider stream in exactly the same configuration (modules above and
 * below all identical) - but it would take quite a few coincidences
 * and modules loading and unloading does not usually happen n times a
 * second...
 */
static void	*
tpi_makekey(queue_t *q, size_t *keylenp)
{
	static uintptr_t	*key	= NULL;
	int			i;

	ASSERT(q != NULL);
	ASSERT(MUTEX_HELD(&tpi_provinfo_lock));

	/* assert this queue is write queue and qprocson() is called before */
	ASSERT((q->q_flag & QREADR) == 0);
	ASSERT(q->q_next != NULL);

	/*
	 * This can be global because tpi_makekey is called with
	 * tpi_provinfo_lock.
	 */
	if (key == NULL)
		key = kmem_alloc((nstrpush + 1) * sizeof (uintptr_t), KM_SLEEP);

	ASSERT(key != NULL);

	/*
	 * Go down q_next to the driver, but no further.  We use the qi_minfo
	 * because we can find in from the queue and it is a stable part of
	 * any driver/module infrastructure.
	 */
	for (i = 0; !LAST_QUEUE(q) && (q = q->q_next) != NULL; ++i) {
		ASSERT(i < nstrpush + 1);
		key[i] = (uintptr_t)q->q_qinfo->qi_minfo;
	}

	/*
	 * Allocate the actual key with the proper length, and pass it
	 * all back.
	 */
	*keylenp = i * sizeof (uintptr_t);
	return ((void *)key);
}

/*
 * Find an existing provider entry given a queue pointer, or allocate a
 * new empty entry if not found.  Because this routine calls kmem_alloc
 * with KM_SLEEP, and because it traverses the q_next pointers of a stream
 * it must be called with a proper user context and within a perimeter
 * which protects the STREAM e.g. an open routine.  This routine always
 * returns a valid pointer.
 */
tpi_provinfo_t	*
tpi_findprov(queue_t *q)
{
	void		*key;
	size_t		keylen;
	tpi_provinfo_t	**tpp;

	mutex_enter(&tpi_provinfo_lock);

	/*
	 * Must hold tpi_provinfo_lock since tpi_makekey() returns a pointer
	 * to static data.
	 */
	key = tpi_makekey(WR(q), &keylen);

	if (keylen == 0) {
		/* there is nothing below us, return special nullprov entry */
		mutex_exit(&tpi_provinfo_lock);
		return (&tpi_nullprov);
	}

	/*
	 * Look for an existing entry, or the place to put a new one.
	 */
	for (tpp = &tpi_provinfo_table[TPI_HASH(key)]; *tpp != NULL;
	    tpp = &(*tpp)->tpi_next) {
		if ((*tpp)->tpi_keylen == keylen &&
		    bcmp((*tpp)->tpi_key, key, keylen) == 0) {
			mutex_exit(&tpi_provinfo_lock);
			return (*tpp);
		}
	}

	/*
	 * Allocate and fill in the new tpi_provinfo_t.
	 */
	*tpp = kmem_zalloc(sizeof (tpi_provinfo_t), KM_SLEEP);
	(*tpp)->tpi_key = kmem_alloc(keylen, KM_SLEEP);
	bcopy(key, (*tpp)->tpi_key, keylen);
	(*tpp)->tpi_keylen = keylen;
	mutex_init(&(*tpp)->tpi_lock, NULL, MUTEX_DEFAULT, NULL);

	mutex_exit(&tpi_provinfo_lock);
	return (*tpp);
}

/*
 * Allocate a TPI ACK reusing the old message if possible.
 */
mblk_t	*
tpi_ack_alloc(mblk_t *mp, size_t size, uchar_t db_type, t_scalar_t prim)
{
	mblk_t	*omp	= mp;

	if ((mp = reallocb(mp, size, 0)) == NULL) {
		freemsg(omp);
		return (NULL);
	}
	if (mp->b_cont != NULL) {
		freemsg(mp->b_cont);
		mp->b_cont = NULL;
	}
	mp->b_datap->db_type = db_type;
	mp->b_wptr = mp->b_rptr + size;
	((union T_primitives *)mp->b_rptr)->type = prim;
	return (mp);
}