]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/dev/cxgbe/iw_cxgbe/id_table.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / dev / cxgbe / iw_cxgbe / id_table.c
1 /*
2  * Copyright (c) 2011-2013 Chelsio Communications.  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 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_inet.h"
36
37 #ifdef TCP_OFFLOAD
38 #include <sys/libkern.h>
39 #include "iw_cxgbe.h"
40
41 #define RANDOM_SKIP 16
42
43 /*
44  * Trivial bitmap-based allocator. If the random flag is set, the
45  * allocator is designed to:
46  * - pseudo-randomize the id returned such that it is not trivially predictable.
47  * - avoid reuse of recently used id (at the expense of predictability)
48  */
49 u32 c4iw_id_alloc(struct c4iw_id_table *alloc)
50 {
51         unsigned long flags;
52         u32 obj;
53
54         spin_lock_irqsave(&alloc->lock, flags);
55
56         obj = find_next_zero_bit(alloc->table, alloc->max, alloc->last);
57         if (obj >= alloc->max)
58                 obj = find_first_zero_bit(alloc->table, alloc->max);
59
60         if (obj < alloc->max) {
61                 if (alloc->flags & C4IW_ID_TABLE_F_RANDOM)
62                         alloc->last += arc4random() % RANDOM_SKIP;
63                 else
64                         alloc->last = obj + 1;
65                 if (alloc->last >= alloc->max)
66                         alloc->last = 0;
67                 set_bit(obj, alloc->table);
68                 obj += alloc->start;
69         } else
70                 obj = -1;
71
72         spin_unlock_irqrestore(&alloc->lock, flags);
73         return obj;
74 }
75
76 void c4iw_id_free(struct c4iw_id_table *alloc, u32 obj)
77 {
78         unsigned long flags;
79
80         obj -= alloc->start;
81         BUG_ON((int)obj < 0);
82
83         spin_lock_irqsave(&alloc->lock, flags);
84         clear_bit(obj, alloc->table);
85         spin_unlock_irqrestore(&alloc->lock, flags);
86 }
87
88 int c4iw_id_table_alloc(struct c4iw_id_table *alloc, u32 start, u32 num,
89                         u32 reserved, u32 flags)
90 {
91         int i;
92
93         alloc->start = start;
94         alloc->flags = flags;
95         if (flags & C4IW_ID_TABLE_F_RANDOM)
96                 alloc->last = arc4random() % RANDOM_SKIP;
97         else
98                 alloc->last = 0;
99         alloc->max  = num;
100         spin_lock_init(&alloc->lock);
101         alloc->table = kmalloc(BITS_TO_LONGS(num) * sizeof(long),
102                                 GFP_KERNEL);
103         if (!alloc->table)
104                 return -ENOMEM;
105
106         bitmap_zero(alloc->table, num);
107         if (!(alloc->flags & C4IW_ID_TABLE_F_EMPTY))
108                 for (i = 0; i < reserved; ++i)
109                         set_bit(i, alloc->table);
110
111         return 0;
112 }
113
114 void c4iw_id_table_free(struct c4iw_id_table *alloc)
115 {
116         kfree(alloc->table);
117 }
118 #endif