18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Copyright (c) 2011 Chelsio Communications. All rights reserved. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two 58c2ecf20Sopenharmony_ci * licenses. You may choose to be licensed under the terms of the GNU 68c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file 78c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the 88c2ecf20Sopenharmony_ci * OpenIB.org BSD license below: 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or 118c2ecf20Sopenharmony_ci * without modification, are permitted provided that the following 128c2ecf20Sopenharmony_ci * conditions are met: 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci * - Redistributions of source code must retain the above 158c2ecf20Sopenharmony_ci * copyright notice, this list of conditions and the following 168c2ecf20Sopenharmony_ci * disclaimer. 178c2ecf20Sopenharmony_ci * 188c2ecf20Sopenharmony_ci * - Redistributions in binary form must reproduce the above 198c2ecf20Sopenharmony_ci * copyright notice, this list of conditions and the following 208c2ecf20Sopenharmony_ci * disclaimer in the documentation and/or other materials 218c2ecf20Sopenharmony_ci * provided with the distribution. 228c2ecf20Sopenharmony_ci * 238c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 248c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 258c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 268c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 278c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 288c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 298c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 308c2ecf20Sopenharmony_ci * SOFTWARE. 318c2ecf20Sopenharmony_ci */ 328c2ecf20Sopenharmony_ci#include <linux/kernel.h> 338c2ecf20Sopenharmony_ci#include <linux/random.h> 348c2ecf20Sopenharmony_ci#include "iw_cxgb4.h" 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci#define RANDOM_SKIP 16 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci/* 398c2ecf20Sopenharmony_ci * Trivial bitmap-based allocator. If the random flag is set, the 408c2ecf20Sopenharmony_ci * allocator is designed to: 418c2ecf20Sopenharmony_ci * - pseudo-randomize the id returned such that it is not trivially predictable. 428c2ecf20Sopenharmony_ci * - avoid reuse of recently used id (at the expense of predictability) 438c2ecf20Sopenharmony_ci */ 448c2ecf20Sopenharmony_ciu32 c4iw_id_alloc(struct c4iw_id_table *alloc) 458c2ecf20Sopenharmony_ci{ 468c2ecf20Sopenharmony_ci unsigned long flags; 478c2ecf20Sopenharmony_ci u32 obj; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci spin_lock_irqsave(&alloc->lock, flags); 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci obj = find_next_zero_bit(alloc->table, alloc->max, alloc->last); 528c2ecf20Sopenharmony_ci if (obj >= alloc->max) 538c2ecf20Sopenharmony_ci obj = find_first_zero_bit(alloc->table, alloc->max); 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci if (obj < alloc->max) { 568c2ecf20Sopenharmony_ci if (alloc->flags & C4IW_ID_TABLE_F_RANDOM) 578c2ecf20Sopenharmony_ci alloc->last += prandom_u32() % RANDOM_SKIP; 588c2ecf20Sopenharmony_ci else 598c2ecf20Sopenharmony_ci alloc->last = obj + 1; 608c2ecf20Sopenharmony_ci if (alloc->last >= alloc->max) 618c2ecf20Sopenharmony_ci alloc->last = 0; 628c2ecf20Sopenharmony_ci set_bit(obj, alloc->table); 638c2ecf20Sopenharmony_ci obj += alloc->start; 648c2ecf20Sopenharmony_ci } else 658c2ecf20Sopenharmony_ci obj = -1; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&alloc->lock, flags); 688c2ecf20Sopenharmony_ci return obj; 698c2ecf20Sopenharmony_ci} 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_civoid c4iw_id_free(struct c4iw_id_table *alloc, u32 obj) 728c2ecf20Sopenharmony_ci{ 738c2ecf20Sopenharmony_ci unsigned long flags; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci obj -= alloc->start; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci spin_lock_irqsave(&alloc->lock, flags); 788c2ecf20Sopenharmony_ci clear_bit(obj, alloc->table); 798c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&alloc->lock, flags); 808c2ecf20Sopenharmony_ci} 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ciint c4iw_id_table_alloc(struct c4iw_id_table *alloc, u32 start, u32 num, 838c2ecf20Sopenharmony_ci u32 reserved, u32 flags) 848c2ecf20Sopenharmony_ci{ 858c2ecf20Sopenharmony_ci int i; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci alloc->start = start; 888c2ecf20Sopenharmony_ci alloc->flags = flags; 898c2ecf20Sopenharmony_ci if (flags & C4IW_ID_TABLE_F_RANDOM) 908c2ecf20Sopenharmony_ci alloc->last = prandom_u32() % RANDOM_SKIP; 918c2ecf20Sopenharmony_ci else 928c2ecf20Sopenharmony_ci alloc->last = 0; 938c2ecf20Sopenharmony_ci alloc->max = num; 948c2ecf20Sopenharmony_ci spin_lock_init(&alloc->lock); 958c2ecf20Sopenharmony_ci alloc->table = kmalloc_array(BITS_TO_LONGS(num), sizeof(long), 968c2ecf20Sopenharmony_ci GFP_KERNEL); 978c2ecf20Sopenharmony_ci if (!alloc->table) 988c2ecf20Sopenharmony_ci return -ENOMEM; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci bitmap_zero(alloc->table, num); 1018c2ecf20Sopenharmony_ci if (!(alloc->flags & C4IW_ID_TABLE_F_EMPTY)) 1028c2ecf20Sopenharmony_ci for (i = 0; i < reserved; ++i) 1038c2ecf20Sopenharmony_ci set_bit(i, alloc->table); 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci return 0; 1068c2ecf20Sopenharmony_ci} 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_civoid c4iw_id_table_free(struct c4iw_id_table *alloc) 1098c2ecf20Sopenharmony_ci{ 1108c2ecf20Sopenharmony_ci kfree(alloc->table); 1118c2ecf20Sopenharmony_ci} 112