id_table.c 3.2 KB

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