cxio_resource.c 9.2 KB
Newer Older
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
/*
 * Copyright (c) 2006 Chelsio, Inc. All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
/* Crude resource management */
#include <linux/kernel.h>
#include <linux/random.h>
#include <linux/slab.h>
#include <linux/kfifo.h>
#include <linux/spinlock.h>
#include <linux/errno.h>
#include "cxio_resource.h"
#include "cxio_hal.h"

42
static struct kfifo rhdl_fifo;
43 44 45 46
static spinlock_t rhdl_fifo_lock;

#define RANDOM_SIZE 16

47
static int __cxio_init_resource_fifo(struct kfifo *fifo,
48 49 50 51 52 53 54 55 56 57
				   spinlock_t *fifo_lock,
				   u32 nr, u32 skip_low,
				   u32 skip_high,
				   int random)
{
	u32 i, j, entry = 0, idx;
	u32 random_bytes;
	u32 rarray[16];
	spin_lock_init(fifo_lock);

S
Stefani Seibold 已提交
58
	if (kfifo_alloc(fifo, nr * sizeof(u32), GFP_KERNEL))
59 60 61
		return -ENOMEM;

	for (i = 0; i < skip_low + skip_high; i++)
62
		kfifo_in(fifo, (unsigned char *) &entry, sizeof(u32));
63 64 65 66 67 68 69 70 71 72 73
	if (random) {
		j = 0;
		random_bytes = random32();
		for (i = 0; i < RANDOM_SIZE; i++)
			rarray[i] = i + skip_low;
		for (i = skip_low + RANDOM_SIZE; i < nr - skip_high; i++) {
			if (j >= RANDOM_SIZE) {
				j = 0;
				random_bytes = random32();
			}
			idx = (random_bytes >> (j * 2)) & 0xF;
74
			kfifo_in(fifo,
75 76 77 78 79 80
				(unsigned char *) &rarray[idx],
				sizeof(u32));
			rarray[idx] = i;
			j++;
		}
		for (i = 0; i < RANDOM_SIZE; i++)
81
			kfifo_in(fifo,
82 83 84 85
				(unsigned char *) &rarray[i],
				sizeof(u32));
	} else
		for (i = skip_low; i < nr - skip_high; i++)
86
			kfifo_in(fifo, (unsigned char *) &i, sizeof(u32));
87 88

	for (i = 0; i < skip_low + skip_high; i++)
S
Stefani Seibold 已提交
89 90 91
		if (kfifo_out_locked(fifo, (unsigned char *) &entry,
				sizeof(u32), fifo_lock) != sizeof(u32))
					break;
92 93 94
	return 0;
}

95
static int cxio_init_resource_fifo(struct kfifo *fifo, spinlock_t * fifo_lock,
96 97 98 99 100 101
				   u32 nr, u32 skip_low, u32 skip_high)
{
	return (__cxio_init_resource_fifo(fifo, fifo_lock, nr, skip_low,
					  skip_high, 0));
}

102
static int cxio_init_resource_fifo_random(struct kfifo *fifo,
103 104 105 106 107 108 109 110 111 112 113 114 115 116
				   spinlock_t * fifo_lock,
				   u32 nr, u32 skip_low, u32 skip_high)
{

	return (__cxio_init_resource_fifo(fifo, fifo_lock, nr, skip_low,
					  skip_high, 1));
}

static int cxio_init_qpid_fifo(struct cxio_rdev *rdev_p)
{
	u32 i;

	spin_lock_init(&rdev_p->rscp->qpid_fifo_lock);

117
	if (kfifo_alloc(&rdev_p->rscp->qpid_fifo, T3_MAX_NUM_QP * sizeof(u32),
S
Stefani Seibold 已提交
118
					      GFP_KERNEL))
119 120 121 122
		return -ENOMEM;

	for (i = 16; i < T3_MAX_NUM_QP; i++)
		if (!(i & rdev_p->qpmask))
123
			kfifo_in(&rdev_p->rscp->qpid_fifo,
124 125 126 127 128 129 130 131 132 133 134 135
				    (unsigned char *) &i, sizeof(u32));
	return 0;
}

int cxio_hal_init_rhdl_resource(u32 nr_rhdl)
{
	return cxio_init_resource_fifo(&rhdl_fifo, &rhdl_fifo_lock, nr_rhdl, 1,
				       0);
}

void cxio_hal_destroy_rhdl_resource(void)
{
136
	kfifo_free(&rhdl_fifo);
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
}

/* nr_* must be power of 2 */
int cxio_hal_init_resource(struct cxio_rdev *rdev_p,
			   u32 nr_tpt, u32 nr_pbl,
			   u32 nr_rqt, u32 nr_qpid, u32 nr_cqid, u32 nr_pdid)
{
	int err = 0;
	struct cxio_hal_resource *rscp;

	rscp = kmalloc(sizeof(*rscp), GFP_KERNEL);
	if (!rscp)
		return -ENOMEM;
	rdev_p->rscp = rscp;
	err = cxio_init_resource_fifo_random(&rscp->tpt_fifo,
				      &rscp->tpt_fifo_lock,
				      nr_tpt, 1, 0);
	if (err)
		goto tpt_err;
	err = cxio_init_qpid_fifo(rdev_p);
	if (err)
		goto qpid_err;
	err = cxio_init_resource_fifo(&rscp->cqid_fifo, &rscp->cqid_fifo_lock,
				      nr_cqid, 1, 0);
	if (err)
		goto cqid_err;
	err = cxio_init_resource_fifo(&rscp->pdid_fifo, &rscp->pdid_fifo_lock,
				      nr_pdid, 1, 0);
	if (err)
		goto pdid_err;
	return 0;
pdid_err:
169
	kfifo_free(&rscp->cqid_fifo);
170
cqid_err:
171
	kfifo_free(&rscp->qpid_fifo);
172
qpid_err:
173
	kfifo_free(&rscp->tpt_fifo);
174 175 176 177 178 179 180
tpt_err:
	return -ENOMEM;
}

/*
 * returns 0 if no resource available
 */
S
Stefani Seibold 已提交
181
static u32 cxio_hal_get_resource(struct kfifo *fifo, spinlock_t * lock)
182 183
{
	u32 entry;
184
	if (kfifo_out_locked(fifo, (unsigned char *) &entry, sizeof(u32), lock))
185 186 187 188 189
		return entry;
	else
		return 0;	/* fifo emptry */
}

S
Stefani Seibold 已提交
190 191
static void cxio_hal_put_resource(struct kfifo *fifo, spinlock_t * lock,
		u32 entry)
192
{
S
Stefani Seibold 已提交
193
	BUG_ON(
194
	kfifo_in_locked(fifo, (unsigned char *) &entry, sizeof(u32), lock)
S
Stefani Seibold 已提交
195
	== 0);
196 197 198 199
}

u32 cxio_hal_get_stag(struct cxio_hal_resource *rscp)
{
S
Stefani Seibold 已提交
200
	return cxio_hal_get_resource(&rscp->tpt_fifo, &rscp->tpt_fifo_lock);
201 202 203 204
}

void cxio_hal_put_stag(struct cxio_hal_resource *rscp, u32 stag)
{
S
Stefani Seibold 已提交
205
	cxio_hal_put_resource(&rscp->tpt_fifo, &rscp->tpt_fifo_lock, stag);
206 207 208 209
}

u32 cxio_hal_get_qpid(struct cxio_hal_resource *rscp)
{
S
Stefani Seibold 已提交
210 211
	u32 qpid = cxio_hal_get_resource(&rscp->qpid_fifo,
			&rscp->qpid_fifo_lock);
212
	PDBG("%s qpid 0x%x\n", __func__, qpid);
213 214 215 216 217
	return qpid;
}

void cxio_hal_put_qpid(struct cxio_hal_resource *rscp, u32 qpid)
{
218
	PDBG("%s qpid 0x%x\n", __func__, qpid);
S
Stefani Seibold 已提交
219
	cxio_hal_put_resource(&rscp->qpid_fifo, &rscp->qpid_fifo_lock, qpid);
220 221 222 223
}

u32 cxio_hal_get_cqid(struct cxio_hal_resource *rscp)
{
S
Stefani Seibold 已提交
224
	return cxio_hal_get_resource(&rscp->cqid_fifo, &rscp->cqid_fifo_lock);
225 226 227 228
}

void cxio_hal_put_cqid(struct cxio_hal_resource *rscp, u32 cqid)
{
S
Stefani Seibold 已提交
229
	cxio_hal_put_resource(&rscp->cqid_fifo, &rscp->cqid_fifo_lock, cqid);
230 231 232 233
}

u32 cxio_hal_get_pdid(struct cxio_hal_resource *rscp)
{
S
Stefani Seibold 已提交
234
	return cxio_hal_get_resource(&rscp->pdid_fifo, &rscp->pdid_fifo_lock);
235 236 237 238
}

void cxio_hal_put_pdid(struct cxio_hal_resource *rscp, u32 pdid)
{
S
Stefani Seibold 已提交
239
	cxio_hal_put_resource(&rscp->pdid_fifo, &rscp->pdid_fifo_lock, pdid);
240 241 242 243
}

void cxio_hal_destroy_resource(struct cxio_hal_resource *rscp)
{
244 245 246 247
	kfifo_free(&rscp->tpt_fifo);
	kfifo_free(&rscp->cqid_fifo);
	kfifo_free(&rscp->qpid_fifo);
	kfifo_free(&rscp->pdid_fifo);
248 249 250 251 252 253 254 255 256 257 258 259
	kfree(rscp);
}

/*
 * PBL Memory Manager.  Uses Linux generic allocator.
 */

#define MIN_PBL_SHIFT 8			/* 256B == min PBL size (32 entries) */

u32 cxio_hal_pblpool_alloc(struct cxio_rdev *rdev_p, int size)
{
	unsigned long addr = gen_pool_alloc(rdev_p->pbl_pool, size);
260
	PDBG("%s addr 0x%x size %d\n", __func__, (u32)addr, size);
261 262 263 264 265
	return (u32)addr;
}

void cxio_hal_pblpool_free(struct cxio_rdev *rdev_p, u32 addr, int size)
{
266
	PDBG("%s addr 0x%x size %d\n", __func__, addr, size);
267 268 269 270 271
	gen_pool_free(rdev_p->pbl_pool, (unsigned long)addr, size);
}

int cxio_hal_pblpool_create(struct cxio_rdev *rdev_p)
{
272 273
	unsigned pbl_start, pbl_chunk;

274
	rdev_p->pbl_pool = gen_pool_create(MIN_PBL_SHIFT, -1);
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
	if (!rdev_p->pbl_pool)
		return -ENOMEM;

	pbl_start = rdev_p->rnic_info.pbl_base;
	pbl_chunk = rdev_p->rnic_info.pbl_top - pbl_start + 1;

	while (pbl_start < rdev_p->rnic_info.pbl_top) {
		pbl_chunk = min(rdev_p->rnic_info.pbl_top - pbl_start + 1,
				pbl_chunk);
		if (gen_pool_add(rdev_p->pbl_pool, pbl_start, pbl_chunk, -1)) {
			PDBG("%s failed to add PBL chunk (%x/%x)\n",
			     __func__, pbl_start, pbl_chunk);
			if (pbl_chunk <= 1024 << MIN_PBL_SHIFT) {
				printk(KERN_WARNING MOD "%s: Failed to add all PBL chunks (%x/%x)\n",
				       __func__, pbl_start, rdev_p->rnic_info.pbl_top - pbl_start);
				return 0;
			}
			pbl_chunk >>= 1;
		} else {
			PDBG("%s added PBL chunk (%x/%x)\n",
			     __func__, pbl_start, pbl_chunk);
			pbl_start += pbl_chunk;
		}
	}

	return 0;
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
}

void cxio_hal_pblpool_destroy(struct cxio_rdev *rdev_p)
{
	gen_pool_destroy(rdev_p->pbl_pool);
}

/*
 * RQT Memory Manager.  Uses Linux generic allocator.
 */

#define MIN_RQT_SHIFT 10	/* 1KB == mini RQT size (16 entries) */
#define RQT_CHUNK 2*1024*1024

u32 cxio_hal_rqtpool_alloc(struct cxio_rdev *rdev_p, int size)
{
	unsigned long addr = gen_pool_alloc(rdev_p->rqt_pool, size << 6);
318
	PDBG("%s addr 0x%x size %d\n", __func__, (u32)addr, size << 6);
319 320 321 322 323
	return (u32)addr;
}

void cxio_hal_rqtpool_free(struct cxio_rdev *rdev_p, u32 addr, int size)
{
324
	PDBG("%s addr 0x%x size %d\n", __func__, addr, size << 6);
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
	gen_pool_free(rdev_p->rqt_pool, (unsigned long)addr, size << 6);
}

int cxio_hal_rqtpool_create(struct cxio_rdev *rdev_p)
{
	unsigned long i;
	rdev_p->rqt_pool = gen_pool_create(MIN_RQT_SHIFT, -1);
	if (rdev_p->rqt_pool)
		for (i = rdev_p->rnic_info.rqt_base;
		     i <= rdev_p->rnic_info.rqt_top - RQT_CHUNK + 1;
		     i += RQT_CHUNK)
			gen_pool_add(rdev_p->rqt_pool, i, RQT_CHUNK, -1);
	return rdev_p->rqt_pool ? 0 : -ENOMEM;
}

void cxio_hal_rqtpool_destroy(struct cxio_rdev *rdev_p)
{
	gen_pool_destroy(rdev_p->rqt_pool);
}