blob: 1b948d192d3242f5156249dc051ab1d8c824e1b5 [file] [log] [blame]
Steve Wisecfdda9d2010-04-21 15:30:06 -07001/*
2 * Copyright (c) 2009-2010 Chelsio, Inc. All rights reserved.
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 <linux/genalloc.h>
Manuel Zerpies3cbe1822011-06-16 12:10:40 +000040#include <linux/ratelimit.h>
Steve Wisecfdda9d2010-04-21 15:30:06 -070041#include "iw_cxgb4.h"
42
43#define RANDOM_SIZE 16
44
45static int __c4iw_init_resource_fifo(struct kfifo *fifo,
46 spinlock_t *fifo_lock,
47 u32 nr, u32 skip_low,
48 u32 skip_high,
49 int random)
50{
51 u32 i, j, entry = 0, idx;
52 u32 random_bytes;
53 u32 rarray[16];
54 spin_lock_init(fifo_lock);
55
56 if (kfifo_alloc(fifo, nr * sizeof(u32), GFP_KERNEL))
57 return -ENOMEM;
58
59 for (i = 0; i < skip_low + skip_high; i++)
60 kfifo_in(fifo, (unsigned char *) &entry, sizeof(u32));
61 if (random) {
62 j = 0;
63 random_bytes = random32();
64 for (i = 0; i < RANDOM_SIZE; i++)
65 rarray[i] = i + skip_low;
66 for (i = skip_low + RANDOM_SIZE; i < nr - skip_high; i++) {
67 if (j >= RANDOM_SIZE) {
68 j = 0;
69 random_bytes = random32();
70 }
71 idx = (random_bytes >> (j * 2)) & 0xF;
72 kfifo_in(fifo,
73 (unsigned char *) &rarray[idx],
74 sizeof(u32));
75 rarray[idx] = i;
76 j++;
77 }
78 for (i = 0; i < RANDOM_SIZE; i++)
79 kfifo_in(fifo,
80 (unsigned char *) &rarray[i],
81 sizeof(u32));
82 } else
83 for (i = skip_low; i < nr - skip_high; i++)
84 kfifo_in(fifo, (unsigned char *) &i, sizeof(u32));
85
86 for (i = 0; i < skip_low + skip_high; i++)
87 if (kfifo_out_locked(fifo, (unsigned char *) &entry,
88 sizeof(u32), fifo_lock))
89 break;
90 return 0;
91}
92
93static int c4iw_init_resource_fifo(struct kfifo *fifo, spinlock_t * fifo_lock,
94 u32 nr, u32 skip_low, u32 skip_high)
95{
96 return __c4iw_init_resource_fifo(fifo, fifo_lock, nr, skip_low,
97 skip_high, 0);
98}
99
100static int c4iw_init_resource_fifo_random(struct kfifo *fifo,
101 spinlock_t *fifo_lock,
102 u32 nr, u32 skip_low, u32 skip_high)
103{
104 return __c4iw_init_resource_fifo(fifo, fifo_lock, nr, skip_low,
105 skip_high, 1);
106}
107
108static int c4iw_init_qid_fifo(struct c4iw_rdev *rdev)
109{
110 u32 i;
111
112 spin_lock_init(&rdev->resource.qid_fifo_lock);
113
Steve Wise93fb72e2010-06-23 15:46:55 +0000114 if (kfifo_alloc(&rdev->resource.qid_fifo, rdev->lldi.vr->qp.size *
115 sizeof(u32), GFP_KERNEL))
Steve Wisecfdda9d2010-04-21 15:30:06 -0700116 return -ENOMEM;
117
Steve Wise93fb72e2010-06-23 15:46:55 +0000118 for (i = rdev->lldi.vr->qp.start;
119 i < rdev->lldi.vr->qp.start + rdev->lldi.vr->qp.size; i++)
Steve Wisecfdda9d2010-04-21 15:30:06 -0700120 if (!(i & rdev->qpmask))
121 kfifo_in(&rdev->resource.qid_fifo,
122 (unsigned char *) &i, sizeof(u32));
123 return 0;
124}
125
126/* nr_* must be power of 2 */
127int c4iw_init_resource(struct c4iw_rdev *rdev, u32 nr_tpt, u32 nr_pdid)
128{
129 int err = 0;
130 err = c4iw_init_resource_fifo_random(&rdev->resource.tpt_fifo,
131 &rdev->resource.tpt_fifo_lock,
132 nr_tpt, 1, 0);
133 if (err)
134 goto tpt_err;
135 err = c4iw_init_qid_fifo(rdev);
136 if (err)
137 goto qid_err;
138 err = c4iw_init_resource_fifo(&rdev->resource.pdid_fifo,
139 &rdev->resource.pdid_fifo_lock,
140 nr_pdid, 1, 0);
141 if (err)
142 goto pdid_err;
143 return 0;
144pdid_err:
145 kfifo_free(&rdev->resource.qid_fifo);
146qid_err:
147 kfifo_free(&rdev->resource.tpt_fifo);
148tpt_err:
149 return -ENOMEM;
150}
151
152/*
153 * returns 0 if no resource available
154 */
155u32 c4iw_get_resource(struct kfifo *fifo, spinlock_t *lock)
156{
157 u32 entry;
158 if (kfifo_out_locked(fifo, (unsigned char *) &entry, sizeof(u32), lock))
159 return entry;
160 else
161 return 0;
162}
163
164void c4iw_put_resource(struct kfifo *fifo, u32 entry, spinlock_t *lock)
165{
166 PDBG("%s entry 0x%x\n", __func__, entry);
167 kfifo_in_locked(fifo, (unsigned char *) &entry, sizeof(u32), lock);
168}
169
170u32 c4iw_get_cqid(struct c4iw_rdev *rdev, struct c4iw_dev_ucontext *uctx)
171{
172 struct c4iw_qid_list *entry;
173 u32 qid;
174 int i;
175
176 mutex_lock(&uctx->lock);
177 if (!list_empty(&uctx->cqids)) {
178 entry = list_entry(uctx->cqids.next, struct c4iw_qid_list,
179 entry);
180 list_del(&entry->entry);
181 qid = entry->qid;
182 kfree(entry);
183 } else {
184 qid = c4iw_get_resource(&rdev->resource.qid_fifo,
185 &rdev->resource.qid_fifo_lock);
186 if (!qid)
187 goto out;
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530188 mutex_lock(&rdev->stats.lock);
189 rdev->stats.qid.cur += rdev->qpmask + 1;
190 mutex_unlock(&rdev->stats.lock);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700191 for (i = qid+1; i & rdev->qpmask; i++) {
192 entry = kmalloc(sizeof *entry, GFP_KERNEL);
193 if (!entry)
194 goto out;
195 entry->qid = i;
196 list_add_tail(&entry->entry, &uctx->cqids);
197 }
198
199 /*
200 * now put the same ids on the qp list since they all
201 * map to the same db/gts page.
202 */
203 entry = kmalloc(sizeof *entry, GFP_KERNEL);
204 if (!entry)
205 goto out;
206 entry->qid = qid;
207 list_add_tail(&entry->entry, &uctx->qpids);
208 for (i = qid+1; i & rdev->qpmask; i++) {
209 entry = kmalloc(sizeof *entry, GFP_KERNEL);
210 if (!entry)
211 goto out;
212 entry->qid = i;
213 list_add_tail(&entry->entry, &uctx->qpids);
214 }
215 }
216out:
217 mutex_unlock(&uctx->lock);
218 PDBG("%s qid 0x%x\n", __func__, qid);
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530219 mutex_lock(&rdev->stats.lock);
220 if (rdev->stats.qid.cur > rdev->stats.qid.max)
221 rdev->stats.qid.max = rdev->stats.qid.cur;
222 mutex_unlock(&rdev->stats.lock);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700223 return qid;
224}
225
226void c4iw_put_cqid(struct c4iw_rdev *rdev, u32 qid,
227 struct c4iw_dev_ucontext *uctx)
228{
229 struct c4iw_qid_list *entry;
230
231 entry = kmalloc(sizeof *entry, GFP_KERNEL);
232 if (!entry)
233 return;
234 PDBG("%s qid 0x%x\n", __func__, qid);
235 entry->qid = qid;
236 mutex_lock(&uctx->lock);
237 list_add_tail(&entry->entry, &uctx->cqids);
238 mutex_unlock(&uctx->lock);
239}
240
241u32 c4iw_get_qpid(struct c4iw_rdev *rdev, struct c4iw_dev_ucontext *uctx)
242{
243 struct c4iw_qid_list *entry;
244 u32 qid;
245 int i;
246
247 mutex_lock(&uctx->lock);
248 if (!list_empty(&uctx->qpids)) {
249 entry = list_entry(uctx->qpids.next, struct c4iw_qid_list,
250 entry);
251 list_del(&entry->entry);
252 qid = entry->qid;
253 kfree(entry);
254 } else {
255 qid = c4iw_get_resource(&rdev->resource.qid_fifo,
256 &rdev->resource.qid_fifo_lock);
257 if (!qid)
258 goto out;
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530259 mutex_lock(&rdev->stats.lock);
260 rdev->stats.qid.cur += rdev->qpmask + 1;
261 mutex_unlock(&rdev->stats.lock);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700262 for (i = qid+1; i & rdev->qpmask; i++) {
263 entry = kmalloc(sizeof *entry, GFP_KERNEL);
264 if (!entry)
265 goto out;
266 entry->qid = i;
267 list_add_tail(&entry->entry, &uctx->qpids);
268 }
269
270 /*
271 * now put the same ids on the cq list since they all
272 * map to the same db/gts page.
273 */
274 entry = kmalloc(sizeof *entry, GFP_KERNEL);
275 if (!entry)
276 goto out;
277 entry->qid = qid;
278 list_add_tail(&entry->entry, &uctx->cqids);
279 for (i = qid; i & rdev->qpmask; i++) {
280 entry = kmalloc(sizeof *entry, GFP_KERNEL);
281 if (!entry)
282 goto out;
283 entry->qid = i;
284 list_add_tail(&entry->entry, &uctx->cqids);
285 }
286 }
287out:
288 mutex_unlock(&uctx->lock);
289 PDBG("%s qid 0x%x\n", __func__, qid);
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530290 mutex_lock(&rdev->stats.lock);
291 if (rdev->stats.qid.cur > rdev->stats.qid.max)
292 rdev->stats.qid.max = rdev->stats.qid.cur;
293 mutex_unlock(&rdev->stats.lock);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700294 return qid;
295}
296
297void c4iw_put_qpid(struct c4iw_rdev *rdev, u32 qid,
298 struct c4iw_dev_ucontext *uctx)
299{
300 struct c4iw_qid_list *entry;
301
302 entry = kmalloc(sizeof *entry, GFP_KERNEL);
303 if (!entry)
304 return;
305 PDBG("%s qid 0x%x\n", __func__, qid);
306 entry->qid = qid;
307 mutex_lock(&uctx->lock);
308 list_add_tail(&entry->entry, &uctx->qpids);
309 mutex_unlock(&uctx->lock);
310}
311
312void c4iw_destroy_resource(struct c4iw_resource *rscp)
313{
314 kfifo_free(&rscp->tpt_fifo);
315 kfifo_free(&rscp->qid_fifo);
316 kfifo_free(&rscp->pdid_fifo);
317}
318
319/*
320 * PBL Memory Manager. Uses Linux generic allocator.
321 */
322
323#define MIN_PBL_SHIFT 8 /* 256B == min PBL size (32 entries) */
324
325u32 c4iw_pblpool_alloc(struct c4iw_rdev *rdev, int size)
326{
327 unsigned long addr = gen_pool_alloc(rdev->pbl_pool, size);
328 PDBG("%s addr 0x%x size %d\n", __func__, (u32)addr, size);
Manuel Zerpies3cbe1822011-06-16 12:10:40 +0000329 if (!addr)
330 printk_ratelimited(KERN_WARNING MOD "%s: Out of PBL memory\n",
Steve Wise05fb9622010-09-10 11:15:14 -0500331 pci_name(rdev->lldi.pdev));
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530332 if (addr) {
333 mutex_lock(&rdev->stats.lock);
334 rdev->stats.pbl.cur += roundup(size, 1 << MIN_PBL_SHIFT);
335 if (rdev->stats.pbl.cur > rdev->stats.pbl.max)
336 rdev->stats.pbl.max = rdev->stats.pbl.cur;
337 mutex_unlock(&rdev->stats.lock);
338 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700339 return (u32)addr;
340}
341
342void c4iw_pblpool_free(struct c4iw_rdev *rdev, u32 addr, int size)
343{
344 PDBG("%s addr 0x%x size %d\n", __func__, addr, size);
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530345 mutex_lock(&rdev->stats.lock);
346 rdev->stats.pbl.cur -= roundup(size, 1 << MIN_PBL_SHIFT);
347 mutex_unlock(&rdev->stats.lock);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700348 gen_pool_free(rdev->pbl_pool, (unsigned long)addr, size);
349}
350
351int c4iw_pblpool_create(struct c4iw_rdev *rdev)
352{
353 unsigned pbl_start, pbl_chunk, pbl_top;
354
355 rdev->pbl_pool = gen_pool_create(MIN_PBL_SHIFT, -1);
356 if (!rdev->pbl_pool)
357 return -ENOMEM;
358
359 pbl_start = rdev->lldi.vr->pbl.start;
360 pbl_chunk = rdev->lldi.vr->pbl.size;
361 pbl_top = pbl_start + pbl_chunk;
362
363 while (pbl_start < pbl_top) {
364 pbl_chunk = min(pbl_top - pbl_start + 1, pbl_chunk);
365 if (gen_pool_add(rdev->pbl_pool, pbl_start, pbl_chunk, -1)) {
366 PDBG("%s failed to add PBL chunk (%x/%x)\n",
367 __func__, pbl_start, pbl_chunk);
368 if (pbl_chunk <= 1024 << MIN_PBL_SHIFT) {
369 printk(KERN_WARNING MOD
370 "Failed to add all PBL chunks (%x/%x)\n",
371 pbl_start,
372 pbl_top - pbl_start);
373 return 0;
374 }
375 pbl_chunk >>= 1;
376 } else {
377 PDBG("%s added PBL chunk (%x/%x)\n",
378 __func__, pbl_start, pbl_chunk);
379 pbl_start += pbl_chunk;
380 }
381 }
382
383 return 0;
384}
385
386void c4iw_pblpool_destroy(struct c4iw_rdev *rdev)
387{
388 gen_pool_destroy(rdev->pbl_pool);
389}
390
391/*
392 * RQT Memory Manager. Uses Linux generic allocator.
393 */
394
395#define MIN_RQT_SHIFT 10 /* 1KB == min RQT size (16 entries) */
396
397u32 c4iw_rqtpool_alloc(struct c4iw_rdev *rdev, int size)
398{
399 unsigned long addr = gen_pool_alloc(rdev->rqt_pool, size << 6);
400 PDBG("%s addr 0x%x size %d\n", __func__, (u32)addr, size << 6);
Manuel Zerpies3cbe1822011-06-16 12:10:40 +0000401 if (!addr)
402 printk_ratelimited(KERN_WARNING MOD "%s: Out of RQT memory\n",
Steve Wise05fb9622010-09-10 11:15:14 -0500403 pci_name(rdev->lldi.pdev));
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530404 if (addr) {
405 mutex_lock(&rdev->stats.lock);
406 rdev->stats.rqt.cur += roundup(size << 6, 1 << MIN_RQT_SHIFT);
407 if (rdev->stats.rqt.cur > rdev->stats.rqt.max)
408 rdev->stats.rqt.max = rdev->stats.rqt.cur;
409 mutex_unlock(&rdev->stats.lock);
410 }
Steve Wisecfdda9d2010-04-21 15:30:06 -0700411 return (u32)addr;
412}
413
414void c4iw_rqtpool_free(struct c4iw_rdev *rdev, u32 addr, int size)
415{
416 PDBG("%s addr 0x%x size %d\n", __func__, addr, size << 6);
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530417 mutex_lock(&rdev->stats.lock);
418 rdev->stats.rqt.cur -= roundup(size << 6, 1 << MIN_RQT_SHIFT);
419 mutex_unlock(&rdev->stats.lock);
Steve Wisecfdda9d2010-04-21 15:30:06 -0700420 gen_pool_free(rdev->rqt_pool, (unsigned long)addr, size << 6);
421}
422
423int c4iw_rqtpool_create(struct c4iw_rdev *rdev)
424{
425 unsigned rqt_start, rqt_chunk, rqt_top;
426
427 rdev->rqt_pool = gen_pool_create(MIN_RQT_SHIFT, -1);
428 if (!rdev->rqt_pool)
429 return -ENOMEM;
430
431 rqt_start = rdev->lldi.vr->rq.start;
432 rqt_chunk = rdev->lldi.vr->rq.size;
433 rqt_top = rqt_start + rqt_chunk;
434
435 while (rqt_start < rqt_top) {
436 rqt_chunk = min(rqt_top - rqt_start + 1, rqt_chunk);
437 if (gen_pool_add(rdev->rqt_pool, rqt_start, rqt_chunk, -1)) {
438 PDBG("%s failed to add RQT chunk (%x/%x)\n",
439 __func__, rqt_start, rqt_chunk);
440 if (rqt_chunk <= 1024 << MIN_RQT_SHIFT) {
441 printk(KERN_WARNING MOD
442 "Failed to add all RQT chunks (%x/%x)\n",
443 rqt_start, rqt_top - rqt_start);
444 return 0;
445 }
446 rqt_chunk >>= 1;
447 } else {
448 PDBG("%s added RQT chunk (%x/%x)\n",
449 __func__, rqt_start, rqt_chunk);
450 rqt_start += rqt_chunk;
451 }
452 }
453 return 0;
454}
455
456void c4iw_rqtpool_destroy(struct c4iw_rdev *rdev)
457{
458 gen_pool_destroy(rdev->rqt_pool);
459}
Steve Wisec6d7b262010-09-13 11:23:57 -0500460
461/*
462 * On-Chip QP Memory.
463 */
464#define MIN_OCQP_SHIFT 12 /* 4KB == min ocqp size */
465
466u32 c4iw_ocqp_pool_alloc(struct c4iw_rdev *rdev, int size)
467{
468 unsigned long addr = gen_pool_alloc(rdev->ocqp_pool, size);
469 PDBG("%s addr 0x%x size %d\n", __func__, (u32)addr, size);
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530470 if (addr) {
471 mutex_lock(&rdev->stats.lock);
472 rdev->stats.ocqp.cur += roundup(size, 1 << MIN_OCQP_SHIFT);
473 if (rdev->stats.ocqp.cur > rdev->stats.ocqp.max)
474 rdev->stats.ocqp.max = rdev->stats.ocqp.cur;
475 mutex_unlock(&rdev->stats.lock);
476 }
Steve Wisec6d7b262010-09-13 11:23:57 -0500477 return (u32)addr;
478}
479
480void c4iw_ocqp_pool_free(struct c4iw_rdev *rdev, u32 addr, int size)
481{
482 PDBG("%s addr 0x%x size %d\n", __func__, addr, size);
Vipul Pandya8d81ef32012-05-18 15:29:27 +0530483 mutex_lock(&rdev->stats.lock);
484 rdev->stats.ocqp.cur -= roundup(size, 1 << MIN_OCQP_SHIFT);
485 mutex_unlock(&rdev->stats.lock);
Steve Wisec6d7b262010-09-13 11:23:57 -0500486 gen_pool_free(rdev->ocqp_pool, (unsigned long)addr, size);
487}
488
489int c4iw_ocqp_pool_create(struct c4iw_rdev *rdev)
490{
491 unsigned start, chunk, top;
492
493 rdev->ocqp_pool = gen_pool_create(MIN_OCQP_SHIFT, -1);
494 if (!rdev->ocqp_pool)
495 return -ENOMEM;
496
497 start = rdev->lldi.vr->ocq.start;
498 chunk = rdev->lldi.vr->ocq.size;
499 top = start + chunk;
500
501 while (start < top) {
502 chunk = min(top - start + 1, chunk);
503 if (gen_pool_add(rdev->ocqp_pool, start, chunk, -1)) {
504 PDBG("%s failed to add OCQP chunk (%x/%x)\n",
505 __func__, start, chunk);
506 if (chunk <= 1024 << MIN_OCQP_SHIFT) {
507 printk(KERN_WARNING MOD
508 "Failed to add all OCQP chunks (%x/%x)\n",
509 start, top - start);
510 return 0;
511 }
512 chunk >>= 1;
513 } else {
514 PDBG("%s added OCQP chunk (%x/%x)\n",
515 __func__, start, chunk);
516 start += chunk;
517 }
518 }
519 return 0;
520}
521
522void c4iw_ocqp_pool_destroy(struct c4iw_rdev *rdev)
523{
524 gen_pool_destroy(rdev->ocqp_pool);
525}