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. Uses Linux generic allocator. */
253 #define MIN_PBL_SHIFT 8 /* 256B == min PBL size (32 entries) */
255 u32 c4iw_pblpool_alloc(struct c4iw_rdev *rdev, int size)
257 unsigned long addr = gen_pool_alloc(rdev->pbl_pool, size);
258 CTR3(KTR_IW_CXGBE, "%s addr 0x%x size %d", __func__, (u32)addr, size);
259 mutex_lock(&rdev->stats.lock);
261 rdev->stats.pbl.cur += roundup(size, 1 << MIN_PBL_SHIFT);
262 if (rdev->stats.pbl.cur > rdev->stats.pbl.max)
263 rdev->stats.pbl.max = rdev->stats.pbl.cur;
265 rdev->stats.pbl.fail++;
266 mutex_unlock(&rdev->stats.lock);
270 void c4iw_pblpool_free(struct c4iw_rdev *rdev, u32 addr, int size)
272 CTR3(KTR_IW_CXGBE, "%s addr 0x%x size %d", __func__, addr, size);
273 mutex_lock(&rdev->stats.lock);
274 rdev->stats.pbl.cur -= roundup(size, 1 << MIN_PBL_SHIFT);
275 mutex_unlock(&rdev->stats.lock);
276 gen_pool_free(rdev->pbl_pool, (unsigned long)addr, size);
279 int c4iw_pblpool_create(struct c4iw_rdev *rdev)
281 rdev->pbl_pool = gen_pool_create(rdev->adap->vres.pbl.start,
283 rdev->adap->vres.pbl.size);
290 void c4iw_pblpool_destroy(struct c4iw_rdev *rdev)
292 gen_pool_destroy(rdev->pbl_pool);
295 /* RQT Memory Manager. Uses Linux generic allocator. */
297 #define MIN_RQT_SHIFT 10 /* 1KB == min RQT size (16 entries) */
299 u32 c4iw_rqtpool_alloc(struct c4iw_rdev *rdev, int size)
301 unsigned long addr = gen_pool_alloc(rdev->rqt_pool, size << 6);
302 CTR3(KTR_IW_CXGBE, "%s addr 0x%x size %d", __func__, (u32)addr,
305 printf("%s: Out of RQT memory\n",
306 device_get_nameunit(rdev->adap->dev));
307 mutex_lock(&rdev->stats.lock);
309 rdev->stats.rqt.cur += roundup(size << 6, 1 << MIN_RQT_SHIFT);
310 if (rdev->stats.rqt.cur > rdev->stats.rqt.max)
311 rdev->stats.rqt.max = rdev->stats.rqt.cur;
313 rdev->stats.rqt.fail++;
314 mutex_unlock(&rdev->stats.lock);
318 void c4iw_rqtpool_free(struct c4iw_rdev *rdev, u32 addr, int size)
320 CTR3(KTR_IW_CXGBE, "%s addr 0x%x size %d", __func__, addr, size << 6);
321 mutex_lock(&rdev->stats.lock);
322 rdev->stats.rqt.cur -= roundup(size << 6, 1 << MIN_RQT_SHIFT);
323 mutex_unlock(&rdev->stats.lock);
324 gen_pool_free(rdev->rqt_pool, (unsigned long)addr, size << 6);
327 int c4iw_rqtpool_create(struct c4iw_rdev *rdev)
329 rdev->rqt_pool = gen_pool_create(rdev->adap->vres.rq.start,
331 rdev->adap->vres.rq.size);
338 void c4iw_rqtpool_destroy(struct c4iw_rdev *rdev)
340 gen_pool_destroy(rdev->rqt_pool);