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 */ 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) 5750bea5c0SAndrew Morton alloc->last += prandom_u32() % RANDOM_SKIP; 58ec3eead2SVipul Pandya else 59ec3eead2SVipul Pandya alloc->last = obj + 1; 60ec3eead2SVipul Pandya if (alloc->last >= alloc->max) 61ec3eead2SVipul Pandya alloc->last = 0; 62ec3eead2SVipul Pandya 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 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 BUG_ON((int)obj < 0); 77ec3eead2SVipul Pandya 78ec3eead2SVipul Pandya spin_lock_irqsave(&alloc->lock, flags); 79ec3eead2SVipul Pandya clear_bit(obj, alloc->table); 80ec3eead2SVipul Pandya spin_unlock_irqrestore(&alloc->lock, flags); 81ec3eead2SVipul Pandya } 82ec3eead2SVipul Pandya 83ec3eead2SVipul Pandya int c4iw_id_table_alloc(struct c4iw_id_table *alloc, u32 start, u32 num, 84ec3eead2SVipul Pandya u32 reserved, u32 flags) 85ec3eead2SVipul Pandya { 86ec3eead2SVipul Pandya int i; 87ec3eead2SVipul Pandya 88ec3eead2SVipul Pandya alloc->start = start; 89ec3eead2SVipul Pandya alloc->flags = flags; 90ec3eead2SVipul Pandya if (flags & C4IW_ID_TABLE_F_RANDOM) 9150bea5c0SAndrew Morton alloc->last = prandom_u32() % RANDOM_SKIP; 92ec3eead2SVipul Pandya else 93ec3eead2SVipul Pandya alloc->last = 0; 94ec3eead2SVipul Pandya alloc->max = num; 95ec3eead2SVipul Pandya spin_lock_init(&alloc->lock); 96ec3eead2SVipul Pandya alloc->table = kmalloc(BITS_TO_LONGS(num) * sizeof(long), 97ec3eead2SVipul Pandya GFP_KERNEL); 98ec3eead2SVipul Pandya if (!alloc->table) 99ec3eead2SVipul Pandya return -ENOMEM; 100ec3eead2SVipul Pandya 101ec3eead2SVipul Pandya bitmap_zero(alloc->table, num); 102ec3eead2SVipul Pandya if (!(alloc->flags & C4IW_ID_TABLE_F_EMPTY)) 103ec3eead2SVipul Pandya for (i = 0; i < reserved; ++i) 104ec3eead2SVipul Pandya set_bit(i, alloc->table); 105ec3eead2SVipul Pandya 106ec3eead2SVipul Pandya return 0; 107ec3eead2SVipul Pandya } 108ec3eead2SVipul Pandya 109ec3eead2SVipul Pandya void c4iw_id_table_free(struct c4iw_id_table *alloc) 110ec3eead2SVipul Pandya { 111ec3eead2SVipul Pandya kfree(alloc->table); 112ec3eead2SVipul Pandya } 113