1256694Snp/* 2256694Snp * Copyright (c) 2011-2013 Chelsio Communications. All rights reserved. 3256694Snp * 4256694Snp * This software is available to you under a choice of one of two 5256694Snp * licenses. You may choose to be licensed under the terms of the GNU 6256694Snp * General Public License (GPL) Version 2, available from the file 7256694Snp * COPYING in the main directory of this source tree, or the 8256694Snp * OpenIB.org BSD license below: 9256694Snp * 10256694Snp * Redistribution and use in source and binary forms, with or 11256694Snp * without modification, are permitted provided that the following 12256694Snp * conditions are met: 13256694Snp * 14256694Snp * - Redistributions of source code must retain the above 15256694Snp * copyright notice, this list of conditions and the following 16256694Snp * disclaimer. 17256694Snp * 18256694Snp * - Redistributions in binary form must reproduce the above 19256694Snp * copyright notice, this list of conditions and the following 20256694Snp * disclaimer in the documentation and/or other materials 21256694Snp * provided with the distribution. 22256694Snp * 23256694Snp * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24256694Snp * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25256694Snp * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26256694Snp * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27256694Snp * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28256694Snp * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29256694Snp * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30256694Snp * SOFTWARE. 31256694Snp */ 32256694Snp#include <sys/cdefs.h> 33256694Snp__FBSDID("$FreeBSD$"); 34256694Snp 35256694Snp#include "opt_inet.h" 36256694Snp 37256694Snp#ifdef TCP_OFFLOAD 38256694Snp#include <sys/libkern.h> 39256694Snp#include "iw_cxgbe.h" 40256694Snp 41256694Snp#define RANDOM_SKIP 16 42256694Snp 43256694Snp/* 44256694Snp * Trivial bitmap-based allocator. If the random flag is set, the 45256694Snp * allocator is designed to: 46256694Snp * - pseudo-randomize the id returned such that it is not trivially predictable. 47256694Snp * - avoid reuse of recently used id (at the expense of predictability) 48256694Snp */ 49256694Snpu32 c4iw_id_alloc(struct c4iw_id_table *alloc) 50256694Snp{ 51256694Snp unsigned long flags; 52256694Snp u32 obj; 53256694Snp 54256694Snp spin_lock_irqsave(&alloc->lock, flags); 55256694Snp 56256694Snp obj = find_next_zero_bit(alloc->table, alloc->max, alloc->last); 57256694Snp if (obj >= alloc->max) 58256694Snp obj = find_first_zero_bit(alloc->table, alloc->max); 59256694Snp 60256694Snp if (obj < alloc->max) { 61256694Snp if (alloc->flags & C4IW_ID_TABLE_F_RANDOM) 62256694Snp alloc->last += arc4random() % RANDOM_SKIP; 63256694Snp else 64256694Snp alloc->last = obj + 1; 65256694Snp if (alloc->last >= alloc->max) 66256694Snp alloc->last = 0; 67256694Snp set_bit(obj, alloc->table); 68256694Snp obj += alloc->start; 69256694Snp } else 70256694Snp obj = -1; 71256694Snp 72256694Snp spin_unlock_irqrestore(&alloc->lock, flags); 73256694Snp return obj; 74256694Snp} 75256694Snp 76256694Snpvoid c4iw_id_free(struct c4iw_id_table *alloc, u32 obj) 77256694Snp{ 78256694Snp unsigned long flags; 79256694Snp 80256694Snp obj -= alloc->start; 81256694Snp BUG_ON((int)obj < 0); 82256694Snp 83256694Snp spin_lock_irqsave(&alloc->lock, flags); 84256694Snp clear_bit(obj, alloc->table); 85256694Snp spin_unlock_irqrestore(&alloc->lock, flags); 86256694Snp} 87256694Snp 88256694Snpint c4iw_id_table_alloc(struct c4iw_id_table *alloc, u32 start, u32 num, 89256694Snp u32 reserved, u32 flags) 90256694Snp{ 91256694Snp int i; 92256694Snp 93256694Snp alloc->start = start; 94256694Snp alloc->flags = flags; 95256694Snp if (flags & C4IW_ID_TABLE_F_RANDOM) 96256694Snp alloc->last = arc4random() % RANDOM_SKIP; 97256694Snp else 98256694Snp alloc->last = 0; 99256694Snp alloc->max = num; 100256694Snp spin_lock_init(&alloc->lock); 101256694Snp alloc->table = kmalloc(BITS_TO_LONGS(num) * sizeof(long), 102256694Snp GFP_KERNEL); 103256694Snp if (!alloc->table) 104256694Snp return -ENOMEM; 105256694Snp 106256694Snp bitmap_zero(alloc->table, num); 107256694Snp if (!(alloc->flags & C4IW_ID_TABLE_F_EMPTY)) 108256694Snp for (i = 0; i < reserved; ++i) 109256694Snp set_bit(i, alloc->table); 110256694Snp 111256694Snp return 0; 112256694Snp} 113256694Snp 114256694Snpvoid c4iw_id_table_free(struct c4iw_id_table *alloc) 115256694Snp{ 116256694Snp kfree(alloc->table); 117256694Snp} 118256694Snp#endif 119