1*6707Sbrutus /*
2*6707Sbrutus * CDDL HEADER START
3*6707Sbrutus *
4*6707Sbrutus * The contents of this file are subject to the terms of the
5*6707Sbrutus * Common Development and Distribution License (the "License").
6*6707Sbrutus * You may not use this file except in compliance with the License.
7*6707Sbrutus *
8*6707Sbrutus * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*6707Sbrutus * or http://www.opensolaris.org/os/licensing.
10*6707Sbrutus * See the License for the specific language governing permissions
11*6707Sbrutus * and limitations under the License.
12*6707Sbrutus *
13*6707Sbrutus * When distributing Covered Code, include this CDDL HEADER in each
14*6707Sbrutus * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*6707Sbrutus * If applicable, add the following below this CDDL HEADER, with the
16*6707Sbrutus * fields enclosed by brackets "[]" replaced with your own identifying
17*6707Sbrutus * information: Portions Copyright [yyyy] [name of copyright owner]
18*6707Sbrutus *
19*6707Sbrutus * CDDL HEADER END
20*6707Sbrutus */
21*6707Sbrutus
22*6707Sbrutus /*
23*6707Sbrutus * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24*6707Sbrutus * Use is subject to license terms.
25*6707Sbrutus */
26*6707Sbrutus
27*6707Sbrutus #pragma ident "%Z%%M% %I% %E% SMI"
28*6707Sbrutus
29*6707Sbrutus #include <sys/kmem.h>
30*6707Sbrutus #include <sys/types.h>
31*6707Sbrutus #include <sys/conf.h>
32*6707Sbrutus #include <sys/ddi.h>
33*6707Sbrutus #include <sys/sunddi.h>
34*6707Sbrutus
35*6707Sbrutus #include <sys/ioat.h>
36*6707Sbrutus
37*6707Sbrutus
38*6707Sbrutus /* structure used to keep track of resources */
39*6707Sbrutus typedef struct ioat_rs_s {
40*6707Sbrutus /*
41*6707Sbrutus * Bounds of resource allocation. We will start allocating at rs_min
42*6707Sbrutus * and rollover at rs_max+1 (rs_max is included). e.g. for rs_min=0
43*6707Sbrutus * and rs_max=7, we will have 8 total resources which can be alloced.
44*6707Sbrutus */
45*6707Sbrutus uint_t rs_min;
46*6707Sbrutus uint_t rs_max;
47*6707Sbrutus
48*6707Sbrutus /*
49*6707Sbrutus * rs_free points to an array of 64-bit values used to track resource
50*6707Sbrutus * allocation. rs_free_size is the free buffer size in bytes.
51*6707Sbrutus */
52*6707Sbrutus uint64_t *rs_free;
53*6707Sbrutus uint_t rs_free_size;
54*6707Sbrutus
55*6707Sbrutus /*
56*6707Sbrutus * last tracks the last alloc'd resource. This allows us to do a round
57*6707Sbrutus * robin allocation.
58*6707Sbrutus */
59*6707Sbrutus uint_t rs_last;
60*6707Sbrutus
61*6707Sbrutus kmutex_t rs_mutex;
62*6707Sbrutus } ioat_rs_t;
63*6707Sbrutus
64*6707Sbrutus
65*6707Sbrutus /*
66*6707Sbrutus * ioat_rs_init()
67*6707Sbrutus * Initialize the resource structure. This structure will be protected
68*6707Sbrutus * by a mutex at the iblock_cookie passed in. init() returns a handle to be
69*6707Sbrutus * used for the rest of the resource functions. This code is written assuming
70*6707Sbrutus * that min_val will be close to 0. Therefore, we will allocate the free
71*6707Sbrutus * buffer only taking max_val into account.
72*6707Sbrutus */
73*6707Sbrutus void
ioat_rs_init(ioat_state_t * state,uint_t min_val,uint_t max_val,ioat_rs_hdl_t * handle)74*6707Sbrutus ioat_rs_init(ioat_state_t *state, uint_t min_val, uint_t max_val,
75*6707Sbrutus ioat_rs_hdl_t *handle)
76*6707Sbrutus {
77*6707Sbrutus ioat_rs_t *rstruct;
78*6707Sbrutus uint_t array_size;
79*6707Sbrutus uint_t index;
80*6707Sbrutus
81*6707Sbrutus
82*6707Sbrutus ASSERT(handle != NULL);
83*6707Sbrutus ASSERT(min_val < max_val);
84*6707Sbrutus
85*6707Sbrutus /* alloc space for resource structure */
86*6707Sbrutus rstruct = kmem_alloc(sizeof (ioat_rs_t), KM_SLEEP);
87*6707Sbrutus
88*6707Sbrutus /*
89*6707Sbrutus * Test to see if the max value is 64-bit aligned. If so, we don't need
90*6707Sbrutus * to allocate an extra 64-bit word. alloc space for free buffer
91*6707Sbrutus * (8 bytes per uint64_t).
92*6707Sbrutus */
93*6707Sbrutus if ((max_val & 0x3F) == 0) {
94*6707Sbrutus rstruct->rs_free_size = (max_val >> 6) * 8;
95*6707Sbrutus } else {
96*6707Sbrutus rstruct->rs_free_size = ((max_val >> 6) + 1) * 8;
97*6707Sbrutus }
98*6707Sbrutus rstruct->rs_free = kmem_alloc(rstruct->rs_free_size, KM_SLEEP);
99*6707Sbrutus
100*6707Sbrutus /* Initialize resource structure */
101*6707Sbrutus rstruct->rs_min = min_val;
102*6707Sbrutus rstruct->rs_last = min_val;
103*6707Sbrutus rstruct->rs_max = max_val;
104*6707Sbrutus mutex_init(&rstruct->rs_mutex, NULL, MUTEX_DRIVER,
105*6707Sbrutus state->is_iblock_cookie);
106*6707Sbrutus
107*6707Sbrutus /* Mark all resources as free */
108*6707Sbrutus array_size = rstruct->rs_free_size >> 3;
109*6707Sbrutus for (index = 0; index < array_size; index++) {
110*6707Sbrutus rstruct->rs_free[index] = (uint64_t)0xFFFFFFFFFFFFFFFF;
111*6707Sbrutus }
112*6707Sbrutus
113*6707Sbrutus /* setup handle which is returned from this function */
114*6707Sbrutus *handle = rstruct;
115*6707Sbrutus }
116*6707Sbrutus
117*6707Sbrutus
118*6707Sbrutus /*
119*6707Sbrutus * ioat_rs_fini()
120*6707Sbrutus * Frees up the space allocated in init(). Notice that a pointer to the
121*6707Sbrutus * handle is used for the parameter. fini() will set the handle to NULL
122*6707Sbrutus * before returning.
123*6707Sbrutus */
124*6707Sbrutus void
ioat_rs_fini(ioat_rs_hdl_t * handle)125*6707Sbrutus ioat_rs_fini(ioat_rs_hdl_t *handle)
126*6707Sbrutus {
127*6707Sbrutus ioat_rs_t *rstruct;
128*6707Sbrutus
129*6707Sbrutus
130*6707Sbrutus ASSERT(handle != NULL);
131*6707Sbrutus
132*6707Sbrutus rstruct = (ioat_rs_t *)*handle;
133*6707Sbrutus
134*6707Sbrutus mutex_destroy(&rstruct->rs_mutex);
135*6707Sbrutus kmem_free(rstruct->rs_free, rstruct->rs_free_size);
136*6707Sbrutus kmem_free(rstruct, sizeof (ioat_rs_t));
137*6707Sbrutus
138*6707Sbrutus /* set handle to null. This helps catch bugs. */
139*6707Sbrutus *handle = NULL;
140*6707Sbrutus }
141*6707Sbrutus
142*6707Sbrutus
143*6707Sbrutus /*
144*6707Sbrutus * ioat_rs_alloc()
145*6707Sbrutus * alloc a resource. If alloc fails, we are out of resources.
146*6707Sbrutus */
147*6707Sbrutus int
ioat_rs_alloc(ioat_rs_hdl_t handle,uint_t * resource)148*6707Sbrutus ioat_rs_alloc(ioat_rs_hdl_t handle, uint_t *resource)
149*6707Sbrutus {
150*6707Sbrutus ioat_rs_t *rstruct;
151*6707Sbrutus uint_t array_idx;
152*6707Sbrutus uint64_t free;
153*6707Sbrutus uint_t index;
154*6707Sbrutus uint_t last;
155*6707Sbrutus uint_t min;
156*6707Sbrutus uint_t max;
157*6707Sbrutus
158*6707Sbrutus
159*6707Sbrutus ASSERT(handle != NULL);
160*6707Sbrutus ASSERT(resource != NULL);
161*6707Sbrutus
162*6707Sbrutus rstruct = (ioat_rs_t *)handle;
163*6707Sbrutus
164*6707Sbrutus mutex_enter(&rstruct->rs_mutex);
165*6707Sbrutus min = rstruct->rs_min;
166*6707Sbrutus max = rstruct->rs_max;
167*6707Sbrutus
168*6707Sbrutus /*
169*6707Sbrutus * Find a free resource. This will return out of the loop once it finds
170*6707Sbrutus * a free resource. There are a total of 'max'-'min'+1 resources.
171*6707Sbrutus * Performs a round robin allocation.
172*6707Sbrutus */
173*6707Sbrutus for (index = min; index <= max; index++) {
174*6707Sbrutus
175*6707Sbrutus array_idx = rstruct->rs_last >> 6;
176*6707Sbrutus free = rstruct->rs_free[array_idx];
177*6707Sbrutus last = rstruct->rs_last & 0x3F;
178*6707Sbrutus
179*6707Sbrutus /* if the next resource to check is free */
180*6707Sbrutus if ((free & ((uint64_t)1 << last)) != 0) {
181*6707Sbrutus /* we are using this resource */
182*6707Sbrutus *resource = rstruct->rs_last;
183*6707Sbrutus
184*6707Sbrutus /* take it out of the free list */
185*6707Sbrutus rstruct->rs_free[array_idx] &= ~((uint64_t)1 << last);
186*6707Sbrutus
187*6707Sbrutus /*
188*6707Sbrutus * increment the last count so we start checking the
189*6707Sbrutus * next resource on the next alloc(). Note the rollover
190*6707Sbrutus * at 'max'+1.
191*6707Sbrutus */
192*6707Sbrutus rstruct->rs_last++;
193*6707Sbrutus if (rstruct->rs_last > max) {
194*6707Sbrutus rstruct->rs_last = rstruct->rs_min;
195*6707Sbrutus }
196*6707Sbrutus
197*6707Sbrutus /* unlock the resource structure */
198*6707Sbrutus mutex_exit(&rstruct->rs_mutex);
199*6707Sbrutus
200*6707Sbrutus return (DDI_SUCCESS);
201*6707Sbrutus }
202*6707Sbrutus
203*6707Sbrutus /*
204*6707Sbrutus * This resource is not free, lets go to the next one. Note the
205*6707Sbrutus * rollover at 'max'.
206*6707Sbrutus */
207*6707Sbrutus rstruct->rs_last++;
208*6707Sbrutus if (rstruct->rs_last > max) {
209*6707Sbrutus rstruct->rs_last = rstruct->rs_min;
210*6707Sbrutus }
211*6707Sbrutus }
212*6707Sbrutus
213*6707Sbrutus mutex_exit(&rstruct->rs_mutex);
214*6707Sbrutus
215*6707Sbrutus return (DDI_FAILURE);
216*6707Sbrutus }
217*6707Sbrutus
218*6707Sbrutus
219*6707Sbrutus /*
220*6707Sbrutus * ioat_rs_free()
221*6707Sbrutus * Free the previously alloc'd resource. Once a resource has been free'd,
222*6707Sbrutus * it can be used again when alloc is called.
223*6707Sbrutus */
224*6707Sbrutus void
ioat_rs_free(ioat_rs_hdl_t handle,uint_t resource)225*6707Sbrutus ioat_rs_free(ioat_rs_hdl_t handle, uint_t resource)
226*6707Sbrutus {
227*6707Sbrutus ioat_rs_t *rstruct;
228*6707Sbrutus uint_t array_idx;
229*6707Sbrutus uint_t offset;
230*6707Sbrutus
231*6707Sbrutus
232*6707Sbrutus ASSERT(handle != NULL);
233*6707Sbrutus
234*6707Sbrutus rstruct = (ioat_rs_t *)handle;
235*6707Sbrutus ASSERT(resource >= rstruct->rs_min);
236*6707Sbrutus ASSERT(resource <= rstruct->rs_max);
237*6707Sbrutus
238*6707Sbrutus mutex_enter(&rstruct->rs_mutex);
239*6707Sbrutus
240*6707Sbrutus /* Put the resource back in the free list */
241*6707Sbrutus array_idx = resource >> 6;
242*6707Sbrutus offset = resource & 0x3F;
243*6707Sbrutus rstruct->rs_free[array_idx] |= ((uint64_t)1 << offset);
244*6707Sbrutus
245*6707Sbrutus mutex_exit(&rstruct->rs_mutex);
246*6707Sbrutus }
247