xref: /titanic_50/usr/src/uts/sun4v/io/vio_util.c (revision 575bd8a230f2c20b1878ebb473c30a12f0eb1b2e)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/sysmacros.h>
31 #include <sys/cmn_err.h>
32 #include <sys/errno.h>
33 #include <sys/kmem.h>
34 #include <sys/ksynch.h>
35 #include <sys/stream.h>
36 #include <sys/ddi.h>
37 #include <sys/sunddi.h>
38 #include <sys/vio_util.h>
39 
40 /*
41  * Create a pool of mblks from which future vio_allocb() requests
42  * will be serviced.
43  *
44  * NOTE: num_mblks has to non-zero and a power-of-2
45  *
46  * Returns 0 on success or EINVAL if num_mblks is zero or not
47  * a power of 2.
48  */
49 int
50 vio_create_mblks(uint64_t num_mblks, size_t mblk_size, vio_mblk_pool_t **poolp)
51 {
52 	vio_mblk_pool_t		*vmplp;
53 	vio_mblk_t		*vmp;
54 	uint8_t			*datap;
55 	int			i;
56 
57 	if (!(num_mblks) || (!ISP2(num_mblks))) {
58 		*poolp = 0;
59 		return (EINVAL);
60 	}
61 
62 	vmplp = kmem_zalloc(sizeof (*vmplp), KM_SLEEP);
63 	vmplp->quelen = num_mblks;
64 	vmplp->quemask = num_mblks - 1; /* expects quelen is power-of-2 */
65 	vmplp->mblk_size = mblk_size;
66 
67 	mutex_init(&vmplp->hlock, NULL, MUTEX_DRIVER,
68 	    DDI_INTR_PRI(DDI_INTR_SOFTPRI_DEFAULT));
69 	mutex_init(&vmplp->tlock, NULL, MUTEX_DRIVER,
70 	    DDI_INTR_PRI(DDI_INTR_SOFTPRI_DEFAULT));
71 
72 	vmplp->basep = kmem_zalloc(num_mblks * sizeof (vio_mblk_t), KM_SLEEP);
73 	vmplp->datap = kmem_zalloc(num_mblks * mblk_size, KM_SLEEP);
74 	vmplp->nextp = NULL;
75 
76 	/* create a queue of pointers to free vio_mblk_t's */
77 	vmplp->quep = kmem_zalloc(vmplp->quelen *
78 	    sizeof (vio_mblk_t *), KM_SLEEP);
79 	vmplp->head = 0;
80 	vmplp->tail =  0;
81 
82 	for (i = 0, datap = vmplp->datap; i < num_mblks; i++) {
83 
84 		vmp = &(vmplp->basep[i]);
85 		vmp->vmplp = vmplp;
86 		vmp->datap = datap;
87 		vmp->reclaim.free_func = vio_freeb;
88 		vmp->reclaim.free_arg = (caddr_t)vmp;
89 		vmp->mp = desballoc(vmp->datap, mblk_size, BPRI_MED,
90 		    &vmp->reclaim);
91 
92 		if (vmp->mp == NULL)
93 			continue;
94 
95 		/* put this vmp on the free stack */
96 		vmplp->quep[vmplp->tail] = vmp;
97 		vmplp->tail = (vmplp->tail + 1) & vmplp->quemask;
98 
99 		datap += mblk_size;
100 	}
101 
102 	*poolp = vmplp;
103 	return (0);
104 }
105 
106 /*
107  * Destroy the pool of mblks. This can only succeed when
108  * all allocated mblks have been returned to the pool.
109  *
110  * It is up to the caller to ensure that no further mblks are
111  * requested from the pool after destroy has been invoked.
112  *
113  * Returns 0 on success, EINVAL if handle is invalid, or
114  * EBUSY if not all mblks reclaimed yet.
115  */
116 int
117 vio_destroy_mblks(vio_mblk_pool_t *vmplp)
118 {
119 	uint64_t i;
120 	uint64_t num_mblks;
121 	vio_mblk_t *vmp;
122 
123 	if (vmplp == NULL)
124 		return (EINVAL);
125 
126 	/*
127 	 * We can only destroy the pool once all the mblks have
128 	 * been reclaimed.
129 	 */
130 	if (vmplp->head != vmplp->tail) {
131 		/* some mblks still in use */
132 		return (EBUSY);
133 	}
134 
135 	num_mblks = vmplp->quelen;
136 
137 	/*
138 	 * Set pool flag to tell vio_freeb() which is invoked from freeb(),
139 	 * that it is being called in the context of vio_destroy_mblks().
140 	 * This results in freeing only mblk_t and dblk_t structures for
141 	 * each mp. The associated data buffers are freed below as one big
142 	 * chunk through kmem_free(vmplp->datap).
143 	 */
144 	vmplp->flag |= VMPL_FLAG_DESTROYING;
145 	for (i = 0; i < num_mblks; i++) {
146 		vmp = &(vmplp->basep[i]);
147 		if (vmp->mp)
148 			freeb(vmp->mp);
149 	}
150 	vmplp->flag &= ~(VMPL_FLAG_DESTROYING);
151 
152 	kmem_free(vmplp->basep, num_mblks * sizeof (vio_mblk_t));
153 	kmem_free(vmplp->datap, num_mblks * vmplp->mblk_size);
154 	kmem_free(vmplp->quep, num_mblks * sizeof (vio_mblk_t *));
155 
156 	mutex_destroy(&vmplp->hlock);
157 	mutex_destroy(&vmplp->tlock);
158 
159 	kmem_free(vmplp, sizeof (*vmplp));
160 
161 	return (0);
162 }
163 
164 /*
165  * Allocate a mblk from the free pool if one is available.
166  * Otherwise returns NULL.
167  */
168 mblk_t *
169 vio_allocb(vio_mblk_pool_t *vmplp)
170 {
171 	vio_mblk_t	*vmp = NULL;
172 	mblk_t		*mp = NULL;
173 	uint32_t	head;
174 
175 	mutex_enter(&vmplp->hlock);
176 	head = (vmplp->head + 1) & vmplp->quemask;
177 	if (head != vmplp->tail) {
178 		/* we have free mblks */
179 		vmp = vmplp->quep[vmplp->head];
180 		mp = vmp->mp;
181 		vmplp->head = head;
182 	}
183 	mutex_exit(&vmplp->hlock);
184 
185 	return (mp);
186 }
187 
188 /*
189  * Return a mblk to the free pool. Invoked when the upper IP
190  * layers do freemsg() etc on the mblk they were passed.
191  */
192 void
193 vio_freeb(void *arg)
194 {
195 	vio_mblk_t	*vmp = (vio_mblk_t *)arg;
196 	vio_mblk_pool_t	*vmplp = vmp->vmplp;
197 
198 	if (vmplp->flag & VMPL_FLAG_DESTROYING) {
199 		/*
200 		 * This flag indicates that freeb() is being called from
201 		 * vio_destroy_mblks().
202 		 * We don't need to alloc a new mblk_t/dblk_t pair for
203 		 * this data buffer, return from here and the data buffer
204 		 * itself will be freed in vio_destroy_mblks().
205 		 */
206 		return;
207 	}
208 
209 	vmp->mp = desballoc(vmp->datap, vmplp->mblk_size,
210 	    BPRI_MED, &vmp->reclaim);
211 
212 	mutex_enter(&vmplp->tlock);
213 	vmplp->quep[vmplp->tail] = vmp;
214 	vmplp->tail = (vmplp->tail + 1) & vmplp->quemask;
215 	mutex_exit(&vmplp->tlock);
216 }
217 
218 /*
219  * Create a multiple pools of mblks from which future vio_allocb()
220  * or vio_multipool_allocb() requests will be serviced.
221  *
222  * Arguments:
223  *	vmultip -- A pointer to vio_multi_pool_t structure.
224  *	num_pools -- Number of the pools.
225  *	... -- Variable arguments consisting a list of buffer sizes for
226  *		each pool and list of number of buffers for each pool.
227  *
228  * NOTE: The restrictions of vio_create_mblks() apply to this interface also.
229  *
230  * Returns 0 on success or an error returned by vio_create_mblks().
231  */
232 int
233 vio_init_multipools(vio_multi_pool_t *vmultip, int num_pools, ...)
234 {
235 	int		i;
236 	int		status;
237 	char		*tbuf;
238 	va_list		vap;
239 	vio_mblk_pool_t *fvmp = NULL;
240 
241 	/*
242 	 * Allocate memory for all of the following in one allocation.
243 	 * 	bufsz_tbl -- sizeof (uint32_t) * num_pools
244 	 * 	nbuf_tbl  -- sizeof (uint32_t) * num_pools
245 	 *	vmpp	  -- sizeof (vio_mblk_pool_t *) * numpools
246 	 */
247 	vmultip->tbsz = (sizeof (uint32_t) * num_pools) +
248 	    (sizeof (uint32_t) * num_pools) +
249 	    (sizeof (vio_mblk_pool_t *) * num_pools);
250 	tbuf = kmem_zalloc(vmultip->tbsz, KM_SLEEP);
251 	vmultip->bufsz_tbl = (uint32_t *)tbuf;
252 	vmultip->nbuf_tbl = (uint32_t *)(tbuf +
253 	    (sizeof (uint32_t) * num_pools));
254 	vmultip->vmpp = (vio_mblk_pool_t **)(tbuf +
255 	    (sizeof (uint32_t) * num_pools * 2));
256 	vmultip->num_pools = num_pools;
257 
258 	/* initialize the array first */
259 	va_start(vap, num_pools);
260 	for (i = 0; i < num_pools; i++) {
261 		vmultip->bufsz_tbl[i] = va_arg(vap, uint32_t);
262 	}
263 	for (i = 0; i < num_pools; i++) {
264 		vmultip->nbuf_tbl[i] = va_arg(vap, uint32_t);
265 	}
266 	va_end(vap);
267 
268 	for (i = 0; i < vmultip->num_pools; i++) {
269 		status = vio_create_mblks(vmultip->nbuf_tbl[i],
270 		    vmultip->bufsz_tbl[i], &vmultip->vmpp[i]);
271 		if (status != 0) {
272 			vio_destroy_multipools(vmultip, &fvmp);
273 			/* We expect to free the pools without failure here */
274 			ASSERT(fvmp == NULL);
275 			return (status);
276 		}
277 	}
278 	return (0);
279 }
280 
281 /*
282  * Destroy the multiple pools of mblks. This can only succeed when
283  * all allocated mblks have been returned to the pool.
284  *
285  * If a pool of mblks couldn't be destroyed, then the failed vio_mblk_pool_t
286  * pointers are returned via th fvmp list. Its the caller's
287  * responsibility to check this list and free them later at an appropriate
288  * time with vio_destroy_mblks().
289  *
290  * Arguments:
291  *	vmultip -- A pointer to vio_multi_pool_t structure.
292  *	fvmp -- A list in which the pools that couldn't be destroyed are
293  *		returned.
294  */
295 void
296 vio_destroy_multipools(vio_multi_pool_t *vmultip, vio_mblk_pool_t **fvmp)
297 {
298 	int i;
299 	vio_mblk_pool_t *vmp;
300 
301 	for (i = 0; i < vmultip->num_pools; i++) {
302 		if ((vmp = vmultip->vmpp[i]) != NULL) {
303 			if (vio_destroy_mblks(vmp)) {
304 				/*
305 				 * if we cannot reclaim all mblks, then
306 				 * return the pool in the failed vmp
307 				 * list(fvmp).
308 				 */
309 				vmp->nextp =  *fvmp;
310 				*fvmp = vmp;
311 			}
312 		}
313 	}
314 	kmem_free(vmultip->bufsz_tbl, vmultip->tbsz);
315 	vmultip->bufsz_tbl = NULL;
316 	vmultip->nbuf_tbl = NULL;
317 	vmultip->vmpp = NULL;
318 }
319 
320 
321 /*
322  * Allocate an mblk from one of the free pools, but tries the pool that
323  * best fits size requested first.
324  */
325 mblk_t *
326 vio_multipool_allocb(vio_multi_pool_t *vmultip, size_t size)
327 {
328 	int i;
329 	mblk_t *mp = NULL;
330 
331 	/* Try allocating any size that fits */
332 	for (i = 0; i < vmultip->num_pools; i++) {
333 		if (size > vmultip->bufsz_tbl[i]) {
334 			continue;
335 		}
336 		mp = vio_allocb(vmultip->vmpp[i]);
337 		if (mp != NULL) {
338 			break;
339 		}
340 	}
341 	return (mp);
342 }
343 
344 /*
345  * -----------------------------------------------------------------------------
346  * LDoms versioning functions
347  *
348  * Future work: the version negotiating code in the various VIO drivers
349  * could be made common and placed here.
350  */
351 
352 /*
353  * Description:
354  *	This function checks to see if the supplied version tuple (major,minor)
355  *	is supported by the version 'ver', negotiated during the handshake
356  *	between the client and the server (ver).
357  *
358  * Assumption:
359  *	This function assumes that backward compatability is not broken in
360  *	newer minor versions of the protocol (e.g. v1.5 & v1.1 support v1.0)
361  *
362  * Return Value:
363  *	B_TRUE		- The (major,minor) version is supported
364  *	B_FALSE		- not supported
365  */
366 boolean_t
367 vio_ver_is_supported(vio_ver_t ver, uint16_t major, uint16_t minor)
368 {
369 	if ((ver.major == major) && (ver.minor >= minor))
370 		return (B_TRUE);
371 
372 	return (B_FALSE);
373 }
374