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