2 * Copyright (c) 2006 Chelsio, Inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
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"
42 static struct kfifo rhdl_fifo;
43 static spinlock_t rhdl_fifo_lock;
45 #define RANDOM_SIZE 16
47 static int __cxio_init_resource_fifo(struct kfifo *fifo,
48 spinlock_t *fifo_lock,
53 u32 i, j, entry = 0, idx;
56 spin_lock_init(fifo_lock);
58 if (kfifo_alloc(fifo, nr * sizeof(u32), GFP_KERNEL))
61 for (i = 0; i < skip_low + skip_high; i++)
62 kfifo_put(fifo, (unsigned char *) &entry, sizeof(u32));
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) {
71 random_bytes = random32();
73 idx = (random_bytes >> (j * 2)) & 0xF;
75 (unsigned char *) &rarray[idx],
80 for (i = 0; i < RANDOM_SIZE; i++)
82 (unsigned char *) &rarray[i],
85 for (i = skip_low; i < nr - skip_high; i++)
86 kfifo_put(fifo, (unsigned char *) &i, sizeof(u32));
88 for (i = 0; i < skip_low + skip_high; i++)
89 kfifo_get_locked(fifo, (unsigned char *) &entry,
90 sizeof(u32), fifo_lock);
94 static int cxio_init_resource_fifo(struct kfifo *fifo, spinlock_t * fifo_lock,
95 u32 nr, u32 skip_low, u32 skip_high)
97 return (__cxio_init_resource_fifo(fifo, fifo_lock, nr, skip_low,
101 static int cxio_init_resource_fifo_random(struct kfifo *fifo,
102 spinlock_t * fifo_lock,
103 u32 nr, u32 skip_low, u32 skip_high)
106 return (__cxio_init_resource_fifo(fifo, fifo_lock, nr, skip_low,
110 static int cxio_init_qpid_fifo(struct cxio_rdev *rdev_p)
114 spin_lock_init(&rdev_p->rscp->qpid_fifo_lock);
116 if (kfifo_alloc(&rdev_p->rscp->qpid_fifo, T3_MAX_NUM_QP * sizeof(u32),
120 for (i = 16; i < T3_MAX_NUM_QP; i++)
121 if (!(i & rdev_p->qpmask))
122 kfifo_put(&rdev_p->rscp->qpid_fifo,
123 (unsigned char *) &i, sizeof(u32));
127 int cxio_hal_init_rhdl_resource(u32 nr_rhdl)
129 return cxio_init_resource_fifo(&rhdl_fifo, &rhdl_fifo_lock, nr_rhdl, 1,
133 void cxio_hal_destroy_rhdl_resource(void)
135 kfifo_free(&rhdl_fifo);
138 /* nr_* must be power of 2 */
139 int 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)
144 struct cxio_hal_resource *rscp;
146 rscp = kmalloc(sizeof(*rscp), GFP_KERNEL);
150 err = cxio_init_resource_fifo_random(&rscp->tpt_fifo,
151 &rscp->tpt_fifo_lock,
155 err = cxio_init_qpid_fifo(rdev_p);
158 err = cxio_init_resource_fifo(&rscp->cqid_fifo, &rscp->cqid_fifo_lock,
162 err = cxio_init_resource_fifo(&rscp->pdid_fifo, &rscp->pdid_fifo_lock,
168 kfifo_free(&rscp->cqid_fifo);
170 kfifo_free(&rscp->qpid_fifo);
172 kfifo_free(&rscp->tpt_fifo);
178 * returns 0 if no resource available
180 static u32 cxio_hal_get_resource(struct kfifo *fifo, spinlock_t * lock)
183 if (kfifo_get_locked(fifo, (unsigned char *) &entry, sizeof(u32), lock))
186 return 0; /* fifo emptry */
189 static void cxio_hal_put_resource(struct kfifo *fifo, spinlock_t * lock,
193 kfifo_put_locked(fifo, (unsigned char *) &entry, sizeof(u32), lock)
197 u32 cxio_hal_get_stag(struct cxio_hal_resource *rscp)
199 return cxio_hal_get_resource(&rscp->tpt_fifo, &rscp->tpt_fifo_lock);
202 void cxio_hal_put_stag(struct cxio_hal_resource *rscp, u32 stag)
204 cxio_hal_put_resource(&rscp->tpt_fifo, &rscp->tpt_fifo_lock, stag);
207 u32 cxio_hal_get_qpid(struct cxio_hal_resource *rscp)
209 u32 qpid = cxio_hal_get_resource(&rscp->qpid_fifo,
210 &rscp->qpid_fifo_lock);
211 PDBG("%s qpid 0x%x\n", __func__, qpid);
215 void cxio_hal_put_qpid(struct cxio_hal_resource *rscp, u32 qpid)
217 PDBG("%s qpid 0x%x\n", __func__, qpid);
218 cxio_hal_put_resource(&rscp->qpid_fifo, &rscp->qpid_fifo_lock, qpid);
221 u32 cxio_hal_get_cqid(struct cxio_hal_resource *rscp)
223 return cxio_hal_get_resource(&rscp->cqid_fifo, &rscp->cqid_fifo_lock);
226 void cxio_hal_put_cqid(struct cxio_hal_resource *rscp, u32 cqid)
228 cxio_hal_put_resource(&rscp->cqid_fifo, &rscp->cqid_fifo_lock, cqid);
231 u32 cxio_hal_get_pdid(struct cxio_hal_resource *rscp)
233 return cxio_hal_get_resource(&rscp->pdid_fifo, &rscp->pdid_fifo_lock);
236 void cxio_hal_put_pdid(struct cxio_hal_resource *rscp, u32 pdid)
238 cxio_hal_put_resource(&rscp->pdid_fifo, &rscp->pdid_fifo_lock, pdid);
241 void cxio_hal_destroy_resource(struct cxio_hal_resource *rscp)
243 kfifo_free(&rscp->tpt_fifo);
244 kfifo_free(&rscp->cqid_fifo);
245 kfifo_free(&rscp->qpid_fifo);
246 kfifo_free(&rscp->pdid_fifo);
251 * PBL Memory Manager. Uses Linux generic allocator.
254 #define MIN_PBL_SHIFT 8 /* 256B == min PBL size (32 entries) */
256 u32 cxio_hal_pblpool_alloc(struct cxio_rdev *rdev_p, int size)
258 unsigned long addr = gen_pool_alloc(rdev_p->pbl_pool, size);
259 PDBG("%s addr 0x%x size %d\n", __func__, (u32)addr, size);
263 void cxio_hal_pblpool_free(struct cxio_rdev *rdev_p, u32 addr, int size)
265 PDBG("%s addr 0x%x size %d\n", __func__, addr, size);
266 gen_pool_free(rdev_p->pbl_pool, (unsigned long)addr, size);
269 int cxio_hal_pblpool_create(struct cxio_rdev *rdev_p)
271 unsigned pbl_start, pbl_chunk;
273 rdev_p->pbl_pool = gen_pool_create(MIN_PBL_SHIFT, -1);
274 if (!rdev_p->pbl_pool)
277 pbl_start = rdev_p->rnic_info.pbl_base;
278 pbl_chunk = rdev_p->rnic_info.pbl_top - pbl_start + 1;
280 while (pbl_start < rdev_p->rnic_info.pbl_top) {
281 pbl_chunk = min(rdev_p->rnic_info.pbl_top - pbl_start + 1,
283 if (gen_pool_add(rdev_p->pbl_pool, pbl_start, pbl_chunk, -1)) {
284 PDBG("%s failed to add PBL chunk (%x/%x)\n",
285 __func__, pbl_start, pbl_chunk);
286 if (pbl_chunk <= 1024 << MIN_PBL_SHIFT) {
287 printk(KERN_WARNING MOD "%s: Failed to add all PBL chunks (%x/%x)\n",
288 __func__, pbl_start, rdev_p->rnic_info.pbl_top - pbl_start);
293 PDBG("%s added PBL chunk (%x/%x)\n",
294 __func__, pbl_start, pbl_chunk);
295 pbl_start += pbl_chunk;
302 void cxio_hal_pblpool_destroy(struct cxio_rdev *rdev_p)
304 gen_pool_destroy(rdev_p->pbl_pool);
308 * RQT Memory Manager. Uses Linux generic allocator.
311 #define MIN_RQT_SHIFT 10 /* 1KB == mini RQT size (16 entries) */
312 #define RQT_CHUNK 2*1024*1024
314 u32 cxio_hal_rqtpool_alloc(struct cxio_rdev *rdev_p, int size)
316 unsigned long addr = gen_pool_alloc(rdev_p->rqt_pool, size << 6);
317 PDBG("%s addr 0x%x size %d\n", __func__, (u32)addr, size << 6);
321 void cxio_hal_rqtpool_free(struct cxio_rdev *rdev_p, u32 addr, int size)
323 PDBG("%s addr 0x%x size %d\n", __func__, addr, size << 6);
324 gen_pool_free(rdev_p->rqt_pool, (unsigned long)addr, size << 6);
327 int cxio_hal_rqtpool_create(struct cxio_rdev *rdev_p)
330 rdev_p->rqt_pool = gen_pool_create(MIN_RQT_SHIFT, -1);
331 if (rdev_p->rqt_pool)
332 for (i = rdev_p->rnic_info.rqt_base;
333 i <= rdev_p->rnic_info.rqt_top - RQT_CHUNK + 1;
335 gen_pool_add(rdev_p->rqt_pool, i, RQT_CHUNK, -1);
336 return rdev_p->rqt_pool ? 0 : -ENOMEM;
339 void cxio_hal_rqtpool_destroy(struct cxio_rdev *rdev_p)
341 gen_pool_destroy(rdev_p->rqt_pool);