kfifo: move struct kfifo in place
[deliverable/linux.git] / drivers / infiniband / hw / cxgb3 / cxio_resource.c
CommitLineData
b038ced7
SW
1/*
2 * Copyright (c) 2006 Chelsio, Inc. All rights reserved.
b038ced7
SW
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32/* Crude resource management */
33#include <linux/kernel.h>
34#include <linux/random.h>
35#include <linux/slab.h>
36#include <linux/kfifo.h>
37#include <linux/spinlock.h>
38#include <linux/errno.h>
39#include "cxio_resource.h"
40#include "cxio_hal.h"
41
45465487 42static struct kfifo rhdl_fifo;
b038ced7
SW
43static spinlock_t rhdl_fifo_lock;
44
45#define RANDOM_SIZE 16
46
45465487 47static int __cxio_init_resource_fifo(struct kfifo *fifo,
b038ced7
SW
48 spinlock_t *fifo_lock,
49 u32 nr, u32 skip_low,
50 u32 skip_high,
51 int random)
52{
53 u32 i, j, entry = 0, idx;
54 u32 random_bytes;
55 u32 rarray[16];
56 spin_lock_init(fifo_lock);
57
45465487 58 if (kfifo_alloc(fifo, nr * sizeof(u32), GFP_KERNEL, fifo_lock))
b038ced7
SW
59 return -ENOMEM;
60
61 for (i = 0; i < skip_low + skip_high; i++)
45465487 62 __kfifo_put(fifo, (unsigned char *) &entry, sizeof(u32));
b038ced7
SW
63 if (random) {
64 j = 0;
65 random_bytes = random32();
66 for (i = 0; i < RANDOM_SIZE; i++)
67 rarray[i] = i + skip_low;
68 for (i = skip_low + RANDOM_SIZE; i < nr - skip_high; i++) {
69 if (j >= RANDOM_SIZE) {
70 j = 0;
71 random_bytes = random32();
72 }
73 idx = (random_bytes >> (j * 2)) & 0xF;
45465487 74 __kfifo_put(fifo,
b038ced7
SW
75 (unsigned char *) &rarray[idx],
76 sizeof(u32));
77 rarray[idx] = i;
78 j++;
79 }
80 for (i = 0; i < RANDOM_SIZE; i++)
45465487 81 __kfifo_put(fifo,
b038ced7
SW
82 (unsigned char *) &rarray[i],
83 sizeof(u32));
84 } else
85 for (i = skip_low; i < nr - skip_high; i++)
45465487 86 __kfifo_put(fifo, (unsigned char *) &i, sizeof(u32));
b038ced7
SW
87
88 for (i = 0; i < skip_low + skip_high; i++)
45465487 89 kfifo_get(fifo, (unsigned char *) &entry, sizeof(u32));
b038ced7
SW
90 return 0;
91}
92
45465487 93static int cxio_init_resource_fifo(struct kfifo *fifo, spinlock_t * fifo_lock,
b038ced7
SW
94 u32 nr, u32 skip_low, u32 skip_high)
95{
96 return (__cxio_init_resource_fifo(fifo, fifo_lock, nr, skip_low,
97 skip_high, 0));
98}
99
45465487 100static int cxio_init_resource_fifo_random(struct kfifo *fifo,
b038ced7
SW
101 spinlock_t * fifo_lock,
102 u32 nr, u32 skip_low, u32 skip_high)
103{
104
105 return (__cxio_init_resource_fifo(fifo, fifo_lock, nr, skip_low,
106 skip_high, 1));
107}
108
109static int cxio_init_qpid_fifo(struct cxio_rdev *rdev_p)
110{
111 u32 i;
112
113 spin_lock_init(&rdev_p->rscp->qpid_fifo_lock);
114
45465487 115 if (kfifo_alloc(&rdev_p->rscp->qpid_fifo, T3_MAX_NUM_QP * sizeof(u32),
b038ced7 116 GFP_KERNEL,
45465487 117 &rdev_p->rscp->qpid_fifo_lock))
b038ced7
SW
118 return -ENOMEM;
119
120 for (i = 16; i < T3_MAX_NUM_QP; i++)
121 if (!(i & rdev_p->qpmask))
45465487 122 __kfifo_put(&rdev_p->rscp->qpid_fifo,
b038ced7
SW
123 (unsigned char *) &i, sizeof(u32));
124 return 0;
125}
126
127int cxio_hal_init_rhdl_resource(u32 nr_rhdl)
128{
129 return cxio_init_resource_fifo(&rhdl_fifo, &rhdl_fifo_lock, nr_rhdl, 1,
130 0);
131}
132
133void cxio_hal_destroy_rhdl_resource(void)
134{
45465487 135 kfifo_free(&rhdl_fifo);
b038ced7
SW
136}
137
138/* nr_* must be power of 2 */
139int cxio_hal_init_resource(struct cxio_rdev *rdev_p,
140 u32 nr_tpt, u32 nr_pbl,
141 u32 nr_rqt, u32 nr_qpid, u32 nr_cqid, u32 nr_pdid)
142{
143 int err = 0;
144 struct cxio_hal_resource *rscp;
145
146 rscp = kmalloc(sizeof(*rscp), GFP_KERNEL);
147 if (!rscp)
148 return -ENOMEM;
149 rdev_p->rscp = rscp;
150 err = cxio_init_resource_fifo_random(&rscp->tpt_fifo,
151 &rscp->tpt_fifo_lock,
152 nr_tpt, 1, 0);
153 if (err)
154 goto tpt_err;
155 err = cxio_init_qpid_fifo(rdev_p);
156 if (err)
157 goto qpid_err;
158 err = cxio_init_resource_fifo(&rscp->cqid_fifo, &rscp->cqid_fifo_lock,
159 nr_cqid, 1, 0);
160 if (err)
161 goto cqid_err;
162 err = cxio_init_resource_fifo(&rscp->pdid_fifo, &rscp->pdid_fifo_lock,
163 nr_pdid, 1, 0);
164 if (err)
165 goto pdid_err;
166 return 0;
167pdid_err:
45465487 168 kfifo_free(&rscp->cqid_fifo);
b038ced7 169cqid_err:
45465487 170 kfifo_free(&rscp->qpid_fifo);
b038ced7 171qpid_err:
45465487 172 kfifo_free(&rscp->tpt_fifo);
b038ced7
SW
173tpt_err:
174 return -ENOMEM;
175}
176
177/*
178 * returns 0 if no resource available
179 */
2b540355 180static u32 cxio_hal_get_resource(struct kfifo *fifo)
b038ced7
SW
181{
182 u32 entry;
183 if (kfifo_get(fifo, (unsigned char *) &entry, sizeof(u32)))
184 return entry;
185 else
186 return 0; /* fifo emptry */
187}
188
2b540355 189static void cxio_hal_put_resource(struct kfifo *fifo, u32 entry)
b038ced7
SW
190{
191 BUG_ON(kfifo_put(fifo, (unsigned char *) &entry, sizeof(u32)) == 0);
192}
193
b038ced7
SW
194u32 cxio_hal_get_stag(struct cxio_hal_resource *rscp)
195{
45465487 196 return cxio_hal_get_resource(&rscp->tpt_fifo);
b038ced7
SW
197}
198
199void cxio_hal_put_stag(struct cxio_hal_resource *rscp, u32 stag)
200{
45465487 201 cxio_hal_put_resource(&rscp->tpt_fifo, stag);
b038ced7
SW
202}
203
204u32 cxio_hal_get_qpid(struct cxio_hal_resource *rscp)
205{
45465487 206 u32 qpid = cxio_hal_get_resource(&rscp->qpid_fifo);
33718363 207 PDBG("%s qpid 0x%x\n", __func__, qpid);
b038ced7
SW
208 return qpid;
209}
210
211void cxio_hal_put_qpid(struct cxio_hal_resource *rscp, u32 qpid)
212{
33718363 213 PDBG("%s qpid 0x%x\n", __func__, qpid);
45465487 214 cxio_hal_put_resource(&rscp->qpid_fifo, qpid);
b038ced7
SW
215}
216
217u32 cxio_hal_get_cqid(struct cxio_hal_resource *rscp)
218{
45465487 219 return cxio_hal_get_resource(&rscp->cqid_fifo);
b038ced7
SW
220}
221
222void cxio_hal_put_cqid(struct cxio_hal_resource *rscp, u32 cqid)
223{
45465487 224 cxio_hal_put_resource(&rscp->cqid_fifo, cqid);
b038ced7
SW
225}
226
227u32 cxio_hal_get_pdid(struct cxio_hal_resource *rscp)
228{
45465487 229 return cxio_hal_get_resource(&rscp->pdid_fifo);
b038ced7
SW
230}
231
232void cxio_hal_put_pdid(struct cxio_hal_resource *rscp, u32 pdid)
233{
45465487 234 cxio_hal_put_resource(&rscp->pdid_fifo, pdid);
b038ced7
SW
235}
236
237void cxio_hal_destroy_resource(struct cxio_hal_resource *rscp)
238{
45465487
SS
239 kfifo_free(&rscp->tpt_fifo);
240 kfifo_free(&rscp->cqid_fifo);
241 kfifo_free(&rscp->qpid_fifo);
242 kfifo_free(&rscp->pdid_fifo);
b038ced7
SW
243 kfree(rscp);
244}
245
246/*
247 * PBL Memory Manager. Uses Linux generic allocator.
248 */
249
250#define MIN_PBL_SHIFT 8 /* 256B == min PBL size (32 entries) */
b038ced7
SW
251
252u32 cxio_hal_pblpool_alloc(struct cxio_rdev *rdev_p, int size)
253{
254 unsigned long addr = gen_pool_alloc(rdev_p->pbl_pool, size);
33718363 255 PDBG("%s addr 0x%x size %d\n", __func__, (u32)addr, size);
b038ced7
SW
256 return (u32)addr;
257}
258
259void cxio_hal_pblpool_free(struct cxio_rdev *rdev_p, u32 addr, int size)
260{
33718363 261 PDBG("%s addr 0x%x size %d\n", __func__, addr, size);
b038ced7
SW
262 gen_pool_free(rdev_p->pbl_pool, (unsigned long)addr, size);
263}
264
265int cxio_hal_pblpool_create(struct cxio_rdev *rdev_p)
266{
0e991336
RD
267 unsigned pbl_start, pbl_chunk;
268
b038ced7 269 rdev_p->pbl_pool = gen_pool_create(MIN_PBL_SHIFT, -1);
0e991336
RD
270 if (!rdev_p->pbl_pool)
271 return -ENOMEM;
272
273 pbl_start = rdev_p->rnic_info.pbl_base;
274 pbl_chunk = rdev_p->rnic_info.pbl_top - pbl_start + 1;
275
276 while (pbl_start < rdev_p->rnic_info.pbl_top) {
277 pbl_chunk = min(rdev_p->rnic_info.pbl_top - pbl_start + 1,
278 pbl_chunk);
279 if (gen_pool_add(rdev_p->pbl_pool, pbl_start, pbl_chunk, -1)) {
280 PDBG("%s failed to add PBL chunk (%x/%x)\n",
281 __func__, pbl_start, pbl_chunk);
282 if (pbl_chunk <= 1024 << MIN_PBL_SHIFT) {
283 printk(KERN_WARNING MOD "%s: Failed to add all PBL chunks (%x/%x)\n",
284 __func__, pbl_start, rdev_p->rnic_info.pbl_top - pbl_start);
285 return 0;
286 }
287 pbl_chunk >>= 1;
288 } else {
289 PDBG("%s added PBL chunk (%x/%x)\n",
290 __func__, pbl_start, pbl_chunk);
291 pbl_start += pbl_chunk;
292 }
293 }
294
295 return 0;
b038ced7
SW
296}
297
298void cxio_hal_pblpool_destroy(struct cxio_rdev *rdev_p)
299{
300 gen_pool_destroy(rdev_p->pbl_pool);
301}
302
303/*
304 * RQT Memory Manager. Uses Linux generic allocator.
305 */
306
307#define MIN_RQT_SHIFT 10 /* 1KB == mini RQT size (16 entries) */
308#define RQT_CHUNK 2*1024*1024
309
310u32 cxio_hal_rqtpool_alloc(struct cxio_rdev *rdev_p, int size)
311{
312 unsigned long addr = gen_pool_alloc(rdev_p->rqt_pool, size << 6);
33718363 313 PDBG("%s addr 0x%x size %d\n", __func__, (u32)addr, size << 6);
b038ced7
SW
314 return (u32)addr;
315}
316
317void cxio_hal_rqtpool_free(struct cxio_rdev *rdev_p, u32 addr, int size)
318{
33718363 319 PDBG("%s addr 0x%x size %d\n", __func__, addr, size << 6);
b038ced7
SW
320 gen_pool_free(rdev_p->rqt_pool, (unsigned long)addr, size << 6);
321}
322
323int cxio_hal_rqtpool_create(struct cxio_rdev *rdev_p)
324{
325 unsigned long i;
326 rdev_p->rqt_pool = gen_pool_create(MIN_RQT_SHIFT, -1);
327 if (rdev_p->rqt_pool)
328 for (i = rdev_p->rnic_info.rqt_base;
329 i <= rdev_p->rnic_info.rqt_top - RQT_CHUNK + 1;
330 i += RQT_CHUNK)
331 gen_pool_add(rdev_p->rqt_pool, i, RQT_CHUNK, -1);
332 return rdev_p->rqt_pool ? 0 : -ENOMEM;
333}
334
335void cxio_hal_rqtpool_destroy(struct cxio_rdev *rdev_p)
336{
337 gen_pool_destroy(rdev_p->rqt_pool);
338}
This page took 0.370827 seconds and 5 git commands to generate.