This source file includes following definitions.
- c4iw_id_alloc
- c4iw_id_free
- c4iw_id_table_alloc
- c4iw_id_table_free
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 #include <linux/kernel.h>
33 #include <linux/random.h>
34 #include "iw_cxgb4.h"
35
36 #define RANDOM_SKIP 16
37
38
39
40
41
42
43
44 u32 c4iw_id_alloc(struct c4iw_id_table *alloc)
45 {
46 unsigned long flags;
47 u32 obj;
48
49 spin_lock_irqsave(&alloc->lock, flags);
50
51 obj = find_next_zero_bit(alloc->table, alloc->max, alloc->last);
52 if (obj >= alloc->max)
53 obj = find_first_zero_bit(alloc->table, alloc->max);
54
55 if (obj < alloc->max) {
56 if (alloc->flags & C4IW_ID_TABLE_F_RANDOM)
57 alloc->last += prandom_u32() % RANDOM_SKIP;
58 else
59 alloc->last = obj + 1;
60 if (alloc->last >= alloc->max)
61 alloc->last = 0;
62 set_bit(obj, alloc->table);
63 obj += alloc->start;
64 } else
65 obj = -1;
66
67 spin_unlock_irqrestore(&alloc->lock, flags);
68 return obj;
69 }
70
71 void c4iw_id_free(struct c4iw_id_table *alloc, u32 obj)
72 {
73 unsigned long flags;
74
75 obj -= alloc->start;
76
77 spin_lock_irqsave(&alloc->lock, flags);
78 clear_bit(obj, alloc->table);
79 spin_unlock_irqrestore(&alloc->lock, flags);
80 }
81
82 int c4iw_id_table_alloc(struct c4iw_id_table *alloc, u32 start, u32 num,
83 u32 reserved, u32 flags)
84 {
85 int i;
86
87 alloc->start = start;
88 alloc->flags = flags;
89 if (flags & C4IW_ID_TABLE_F_RANDOM)
90 alloc->last = prandom_u32() % RANDOM_SKIP;
91 else
92 alloc->last = 0;
93 alloc->max = num;
94 spin_lock_init(&alloc->lock);
95 alloc->table = kmalloc_array(BITS_TO_LONGS(num), sizeof(long),
96 GFP_KERNEL);
97 if (!alloc->table)
98 return -ENOMEM;
99
100 bitmap_zero(alloc->table, num);
101 if (!(alloc->flags & C4IW_ID_TABLE_F_EMPTY))
102 for (i = 0; i < reserved; ++i)
103 set_bit(i, alloc->table);
104
105 return 0;
106 }
107
108 void c4iw_id_table_free(struct c4iw_id_table *alloc)
109 {
110 kfree(alloc->table);
111 }