brintos

brintos / linux-shallow public Read only

0
0
Text · 3.1 KiB · e2188b3 Raw
107 lines · c
1/*2 * Copyright (c) 2011 Chelsio Communications.  All rights reserved.3 *4 * This software is available to you under a choice of one of two5 * licenses.  You may choose to be licensed under the terms of the GNU6 * General Public License (GPL) Version 2, available from the file7 * COPYING in the main directory of this source tree, or the8 * OpenIB.org BSD license below:9 *10 *     Redistribution and use in source and binary forms, with or11 *     without modification, are permitted provided that the following12 *     conditions are met:13 *14 *      - Redistributions of source code must retain the above15 *        copyright notice, this list of conditions and the following16 *        disclaimer.17 *18 *      - Redistributions in binary form must reproduce the above19 *        copyright notice, this list of conditions and the following20 *        disclaimer in the documentation and/or other materials21 *        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 OF25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE30 * SOFTWARE.31 */32#include <linux/kernel.h>33#include <linux/random.h>34#include "iw_cxgb4.h"35 36#define RANDOM_SKIP 1637 38/*39 * Trivial bitmap-based allocator. If the random flag is set, the40 * allocator is designed to:41 * - pseudo-randomize the id returned such that it is not trivially predictable.42 * - avoid reuse of recently used id (at the expense of predictability)43 */44u32 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 += get_random_u32_below(RANDOM_SKIP);58		else59			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	} else65		obj = -1;66 67	spin_unlock_irqrestore(&alloc->lock, flags);68	return obj;69}70 71void 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 82int c4iw_id_table_alloc(struct c4iw_id_table *alloc, u32 start, u32 num,83			u32 reserved, u32 flags)84{85	alloc->start = start;86	alloc->flags = flags;87	if (flags & C4IW_ID_TABLE_F_RANDOM)88		alloc->last = get_random_u32_below(RANDOM_SKIP);89	else90		alloc->last = 0;91	alloc->max = num;92	spin_lock_init(&alloc->lock);93	alloc->table = bitmap_zalloc(num, GFP_KERNEL);94	if (!alloc->table)95		return -ENOMEM;96 97	if (!(alloc->flags & C4IW_ID_TABLE_F_EMPTY))98		bitmap_set(alloc->table, 0, reserved);99 100	return 0;101}102 103void c4iw_id_table_free(struct c4iw_id_table *alloc)104{105	bitmap_free(alloc->table);106}107