1ec3eead2SVipul Pandya /*
2ec3eead2SVipul Pandya * Copyright (c) 2011 Chelsio Communications. All rights reserved.
3ec3eead2SVipul Pandya *
4ec3eead2SVipul Pandya * This software is available to you under a choice of one of two
5ec3eead2SVipul Pandya * licenses. You may choose to be licensed under the terms of the GNU
6ec3eead2SVipul Pandya * General Public License (GPL) Version 2, available from the file
7ec3eead2SVipul Pandya * COPYING in the main directory of this source tree, or the
8ec3eead2SVipul Pandya * OpenIB.org BSD license below:
9ec3eead2SVipul Pandya *
10ec3eead2SVipul Pandya * Redistribution and use in source and binary forms, with or
11ec3eead2SVipul Pandya * without modification, are permitted provided that the following
12ec3eead2SVipul Pandya * conditions are met:
13ec3eead2SVipul Pandya *
14ec3eead2SVipul Pandya * - Redistributions of source code must retain the above
15ec3eead2SVipul Pandya * copyright notice, this list of conditions and the following
16ec3eead2SVipul Pandya * disclaimer.
17ec3eead2SVipul Pandya *
18ec3eead2SVipul Pandya * - Redistributions in binary form must reproduce the above
19ec3eead2SVipul Pandya * copyright notice, this list of conditions and the following
20ec3eead2SVipul Pandya * disclaimer in the documentation and/or other materials
21ec3eead2SVipul Pandya * provided with the distribution.
22ec3eead2SVipul Pandya *
23ec3eead2SVipul Pandya * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24ec3eead2SVipul Pandya * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25ec3eead2SVipul Pandya * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26ec3eead2SVipul Pandya * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27ec3eead2SVipul Pandya * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28ec3eead2SVipul Pandya * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29ec3eead2SVipul Pandya * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30ec3eead2SVipul Pandya * SOFTWARE.
31ec3eead2SVipul Pandya */
32ec3eead2SVipul Pandya #include <linux/kernel.h>
33ec3eead2SVipul Pandya #include <linux/random.h>
34ec3eead2SVipul Pandya #include "iw_cxgb4.h"
35ec3eead2SVipul Pandya
36ec3eead2SVipul Pandya #define RANDOM_SKIP 16
37ec3eead2SVipul Pandya
38ec3eead2SVipul Pandya /*
39ec3eead2SVipul Pandya * Trivial bitmap-based allocator. If the random flag is set, the
40ec3eead2SVipul Pandya * allocator is designed to:
41ec3eead2SVipul Pandya * - pseudo-randomize the id returned such that it is not trivially predictable.
42ec3eead2SVipul Pandya * - avoid reuse of recently used id (at the expense of predictability)
43ec3eead2SVipul Pandya */
c4iw_id_alloc(struct c4iw_id_table * alloc)44ec3eead2SVipul Pandya u32 c4iw_id_alloc(struct c4iw_id_table *alloc)
45ec3eead2SVipul Pandya {
46ec3eead2SVipul Pandya unsigned long flags;
47ec3eead2SVipul Pandya u32 obj;
48ec3eead2SVipul Pandya
49ec3eead2SVipul Pandya spin_lock_irqsave(&alloc->lock, flags);
50ec3eead2SVipul Pandya
51ec3eead2SVipul Pandya obj = find_next_zero_bit(alloc->table, alloc->max, alloc->last);
52ec3eead2SVipul Pandya if (obj >= alloc->max)
53ec3eead2SVipul Pandya obj = find_first_zero_bit(alloc->table, alloc->max);
54ec3eead2SVipul Pandya
55ec3eead2SVipul Pandya if (obj < alloc->max) {
56ec3eead2SVipul Pandya if (alloc->flags & C4IW_ID_TABLE_F_RANDOM)
57*8032bf12SJason A. Donenfeld alloc->last += get_random_u32_below(RANDOM_SKIP);
58ec3eead2SVipul Pandya else
59ec3eead2SVipul Pandya alloc->last = obj + 1;
60ec3eead2SVipul Pandya if (alloc->last >= alloc->max)
61ec3eead2SVipul Pandya alloc->last = 0;
62223b4d5cSChristophe JAILLET __set_bit(obj, alloc->table);
63ec3eead2SVipul Pandya obj += alloc->start;
64ec3eead2SVipul Pandya } else
65ec3eead2SVipul Pandya obj = -1;
66ec3eead2SVipul Pandya
67ec3eead2SVipul Pandya spin_unlock_irqrestore(&alloc->lock, flags);
68ec3eead2SVipul Pandya return obj;
69ec3eead2SVipul Pandya }
70ec3eead2SVipul Pandya
c4iw_id_free(struct c4iw_id_table * alloc,u32 obj)71ec3eead2SVipul Pandya void c4iw_id_free(struct c4iw_id_table *alloc, u32 obj)
72ec3eead2SVipul Pandya {
73ec3eead2SVipul Pandya unsigned long flags;
74ec3eead2SVipul Pandya
75ec3eead2SVipul Pandya obj -= alloc->start;
76ec3eead2SVipul Pandya
77ec3eead2SVipul Pandya spin_lock_irqsave(&alloc->lock, flags);
78223b4d5cSChristophe JAILLET __clear_bit(obj, alloc->table);
79ec3eead2SVipul Pandya spin_unlock_irqrestore(&alloc->lock, flags);
80ec3eead2SVipul Pandya }
81ec3eead2SVipul Pandya
c4iw_id_table_alloc(struct c4iw_id_table * alloc,u32 start,u32 num,u32 reserved,u32 flags)82ec3eead2SVipul Pandya int c4iw_id_table_alloc(struct c4iw_id_table *alloc, u32 start, u32 num,
83ec3eead2SVipul Pandya u32 reserved, u32 flags)
84ec3eead2SVipul Pandya {
85ec3eead2SVipul Pandya alloc->start = start;
86ec3eead2SVipul Pandya alloc->flags = flags;
87ec3eead2SVipul Pandya if (flags & C4IW_ID_TABLE_F_RANDOM)
88*8032bf12SJason A. Donenfeld alloc->last = get_random_u32_below(RANDOM_SKIP);
89ec3eead2SVipul Pandya else
90ec3eead2SVipul Pandya alloc->last = 0;
91ec3eead2SVipul Pandya alloc->max = num;
92ec3eead2SVipul Pandya spin_lock_init(&alloc->lock);
93d4fdc383SChristophe JAILLET alloc->table = bitmap_zalloc(num, GFP_KERNEL);
94ec3eead2SVipul Pandya if (!alloc->table)
95ec3eead2SVipul Pandya return -ENOMEM;
96ec3eead2SVipul Pandya
97ec3eead2SVipul Pandya if (!(alloc->flags & C4IW_ID_TABLE_F_EMPTY))
98967a578aSChristophe JAILLET bitmap_set(alloc->table, 0, reserved);
99ec3eead2SVipul Pandya
100ec3eead2SVipul Pandya return 0;
101ec3eead2SVipul Pandya }
102ec3eead2SVipul Pandya
c4iw_id_table_free(struct c4iw_id_table * alloc)103ec3eead2SVipul Pandya void c4iw_id_table_free(struct c4iw_id_table *alloc)
104ec3eead2SVipul Pandya {
105d4fdc383SChristophe JAILLET bitmap_free(alloc->table);
106ec3eead2SVipul Pandya }
107