2 * Copyright (c) 2009-2013 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 <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
39 #include <linux/spinlock.h>
42 static int c4iw_init_qid_table(struct c4iw_rdev *rdev)
46 if (c4iw_id_table_alloc(&rdev->resource.qid_table,
47 rdev->adap->vres.qp.start,
48 rdev->adap->vres.qp.size,
49 rdev->adap->vres.qp.size, 0)) {
50 printf("%s: return ENOMEM\n", __func__);
54 for (i = rdev->adap->vres.qp.start;
55 i < rdev->adap->vres.qp.start + rdev->adap->vres.qp.size; i++)
56 if (!(i & rdev->qpmask))
57 c4iw_id_free(&rdev->resource.qid_table, i);
61 /* nr_* must be power of 2 */
62 int c4iw_init_resource(struct c4iw_rdev *rdev, u32 nr_tpt, u32 nr_pdid)
65 err = c4iw_id_table_alloc(&rdev->resource.tpt_table, 0, nr_tpt, 1,
66 C4IW_ID_TABLE_F_RANDOM);
69 err = c4iw_init_qid_table(rdev);
72 err = c4iw_id_table_alloc(&rdev->resource.pdid_table, 0,
78 c4iw_id_table_free(&rdev->resource.qid_table);
80 c4iw_id_table_free(&rdev->resource.tpt_table);
86 * returns 0 if no resource available
88 u32 c4iw_get_resource(struct c4iw_id_table *id_table)
91 entry = c4iw_id_alloc(id_table);
92 if (entry == (u32)(-1)) {
98 void c4iw_put_resource(struct c4iw_id_table *id_table, u32 entry)
100 CTR2(KTR_IW_CXGBE, "%s entry 0x%x", __func__, entry);
101 c4iw_id_free(id_table, entry);
104 u32 c4iw_get_cqid(struct c4iw_rdev *rdev, struct c4iw_dev_ucontext *uctx)
106 struct c4iw_qid_list *entry;
110 mutex_lock(&uctx->lock);
111 if (!list_empty(&uctx->cqids)) {
112 entry = list_entry(uctx->cqids.next, struct c4iw_qid_list,
114 list_del(&entry->entry);
118 qid = c4iw_get_resource(&rdev->resource.qid_table);
121 mutex_lock(&rdev->stats.lock);
122 rdev->stats.qid.cur += rdev->qpmask + 1;
123 mutex_unlock(&rdev->stats.lock);
124 for (i = qid+1; i & rdev->qpmask; i++) {
125 entry = kmalloc(sizeof *entry, GFP_KERNEL);
129 list_add_tail(&entry->entry, &uctx->cqids);
133 * now put the same ids on the qp list since they all
134 * map to the same db/gts page.
136 entry = kmalloc(sizeof *entry, GFP_KERNEL);
140 list_add_tail(&entry->entry, &uctx->qpids);
141 for (i = qid+1; i & rdev->qpmask; i++) {
142 entry = kmalloc(sizeof *entry, GFP_KERNEL);
146 list_add_tail(&entry->entry, &uctx->qpids);
150 mutex_unlock(&uctx->lock);
151 CTR2(KTR_IW_CXGBE, "%s: qid 0x%x", __func__, qid);
152 mutex_lock(&rdev->stats.lock);
153 if (rdev->stats.qid.cur > rdev->stats.qid.max)
154 rdev->stats.qid.max = rdev->stats.qid.cur;
155 mutex_unlock(&rdev->stats.lock);
159 void c4iw_put_cqid(struct c4iw_rdev *rdev, u32 qid,
160 struct c4iw_dev_ucontext *uctx)
162 struct c4iw_qid_list *entry;
164 entry = kmalloc(sizeof *entry, GFP_KERNEL);
167 CTR2(KTR_IW_CXGBE, "%s qid 0x%x", __func__, qid);
169 mutex_lock(&uctx->lock);
170 list_add_tail(&entry->entry, &uctx->cqids);
171 mutex_unlock(&uctx->lock);
174 u32 c4iw_get_qpid(struct c4iw_rdev *rdev, struct c4iw_dev_ucontext *uctx)
176 struct c4iw_qid_list *entry;
180 mutex_lock(&uctx->lock);
181 if (!list_empty(&uctx->qpids)) {
182 entry = list_entry(uctx->qpids.next, struct c4iw_qid_list,
184 list_del(&entry->entry);
188 qid = c4iw_get_resource(&rdev->resource.qid_table);
191 mutex_lock(&rdev->stats.lock);
192 rdev->stats.qid.cur += rdev->qpmask + 1;
193 mutex_unlock(&rdev->stats.lock);
194 for (i = qid+1; i & rdev->qpmask; i++) {
195 entry = kmalloc(sizeof *entry, GFP_KERNEL);
199 list_add_tail(&entry->entry, &uctx->qpids);
203 * now put the same ids on the cq list since they all
204 * map to the same db/gts page.
206 entry = kmalloc(sizeof *entry, GFP_KERNEL);
210 list_add_tail(&entry->entry, &uctx->cqids);
211 for (i = qid; i & rdev->qpmask; i++) {
212 entry = kmalloc(sizeof *entry, GFP_KERNEL);
216 list_add_tail(&entry->entry, &uctx->cqids);
220 mutex_unlock(&uctx->lock);
221 CTR2(KTR_IW_CXGBE, "%s qid 0x%x", __func__, qid);
222 mutex_lock(&rdev->stats.lock);
223 if (rdev->stats.qid.cur > rdev->stats.qid.max)
224 rdev->stats.qid.max = rdev->stats.qid.cur;
225 mutex_unlock(&rdev->stats.lock);
229 void c4iw_put_qpid(struct c4iw_rdev *rdev, u32 qid,
230 struct c4iw_dev_ucontext *uctx)
232 struct c4iw_qid_list *entry;
234 entry = kmalloc(sizeof *entry, GFP_KERNEL);
237 CTR2(KTR_IW_CXGBE, "%s qid 0x%x", __func__, qid);
239 mutex_lock(&uctx->lock);
240 list_add_tail(&entry->entry, &uctx->qpids);
241 mutex_unlock(&uctx->lock);
244 void c4iw_destroy_resource(struct c4iw_resource *rscp)
246 c4iw_id_table_free(&rscp->tpt_table);
247 c4iw_id_table_free(&rscp->qid_table);
248 c4iw_id_table_free(&rscp->pdid_table);
251 /* PBL Memory Manager. */
253 #define MIN_PBL_SHIFT 5 /* 32B == min PBL size (4 entries) */
255 u32 c4iw_pblpool_alloc(struct c4iw_rdev *rdev, int size)
259 vmem_xalloc(rdev->pbl_arena, roundup(size, (1 << MIN_PBL_SHIFT)),
260 4, 0, 0, VMEM_ADDR_MIN, VMEM_ADDR_MAX,
261 M_FIRSTFIT|M_NOWAIT, &addr);
262 CTR3(KTR_IW_CXGBE, "%s addr 0x%x size %d", __func__, (u32)addr, size);
263 mutex_lock(&rdev->stats.lock);
265 rdev->stats.pbl.cur += roundup(size, 1 << MIN_PBL_SHIFT);
266 if (rdev->stats.pbl.cur > rdev->stats.pbl.max)
267 rdev->stats.pbl.max = rdev->stats.pbl.cur;
269 rdev->stats.pbl.fail++;
270 mutex_unlock(&rdev->stats.lock);
274 void c4iw_pblpool_free(struct c4iw_rdev *rdev, u32 addr, int size)
276 CTR3(KTR_IW_CXGBE, "%s addr 0x%x size %d", __func__, addr, size);
277 mutex_lock(&rdev->stats.lock);
278 rdev->stats.pbl.cur -= roundup(size, 1 << MIN_PBL_SHIFT);
279 mutex_unlock(&rdev->stats.lock);
280 vmem_xfree(rdev->pbl_arena, addr, roundup(size,(1 << MIN_PBL_SHIFT)));
283 int c4iw_pblpool_create(struct c4iw_rdev *rdev)
285 rdev->pbl_arena = vmem_create("PBL_MEM_POOL",
286 rdev->adap->vres.pbl.start,
287 rdev->adap->vres.pbl.size,
288 1, 0, M_FIRSTFIT| M_NOWAIT);
289 if (!rdev->pbl_arena)
295 void c4iw_pblpool_destroy(struct c4iw_rdev *rdev)
297 vmem_destroy(rdev->pbl_arena);
300 /* RQT Memory Manager. */
302 #define MIN_RQT_SHIFT 10 /* 1KB == min RQT size (16 entries) */
304 u32 c4iw_rqtpool_alloc(struct c4iw_rdev *rdev, int size)
308 vmem_xalloc(rdev->rqt_arena,
309 roundup((size << 6),(1 << MIN_RQT_SHIFT)),
310 4, 0, 0, VMEM_ADDR_MIN, VMEM_ADDR_MAX,
311 M_FIRSTFIT|M_NOWAIT, &addr);
312 CTR3(KTR_IW_CXGBE, "%s addr 0x%x size %d", __func__, (u32)addr,
315 printf("%s: Out of RQT memory\n",
316 device_get_nameunit(rdev->adap->dev));
317 mutex_lock(&rdev->stats.lock);
319 rdev->stats.rqt.cur += roundup(size << 6, 1 << MIN_RQT_SHIFT);
320 if (rdev->stats.rqt.cur > rdev->stats.rqt.max)
321 rdev->stats.rqt.max = rdev->stats.rqt.cur;
323 rdev->stats.rqt.fail++;
324 mutex_unlock(&rdev->stats.lock);
328 void c4iw_rqtpool_free(struct c4iw_rdev *rdev, u32 addr, int size)
330 CTR3(KTR_IW_CXGBE, "%s addr 0x%x size %d", __func__, addr, size << 6);
331 mutex_lock(&rdev->stats.lock);
332 rdev->stats.rqt.cur -= roundup(size << 6, 1 << MIN_RQT_SHIFT);
333 mutex_unlock(&rdev->stats.lock);
334 vmem_xfree(rdev->rqt_arena, addr,
335 roundup((size << 6),(1 << MIN_RQT_SHIFT)));
338 int c4iw_rqtpool_create(struct c4iw_rdev *rdev)
340 rdev->rqt_arena = vmem_create("RQT_MEM_POOL",
341 rdev->adap->vres.rq.start,
342 rdev->adap->vres.rq.size,
343 1, 0, M_FIRSTFIT| M_NOWAIT);
344 if (!rdev->rqt_arena)
350 void c4iw_rqtpool_destroy(struct c4iw_rdev *rdev)
352 vmem_destroy(rdev->rqt_arena);