spectrum_cnt.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * drivers/net/ethernet/mellanox/mlxsw/spectrum_cnt.c
  3. * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
  4. * Copyright (c) 2017 Arkadi Sharshevsky <arkadis@mellanox.com>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the names of the copyright holders nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * Alternatively, this software may be distributed under the terms of the
  19. * GNU General Public License ("GPL") version 2 as published by the Free
  20. * Software Foundation.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/bitops.h>
  36. #include "spectrum_cnt.h"
  37. #define MLXSW_SP_COUNTER_POOL_BANK_SIZE 4096
  38. struct mlxsw_sp_counter_sub_pool {
  39. unsigned int base_index;
  40. unsigned int size;
  41. unsigned int entry_size;
  42. unsigned int bank_count;
  43. };
  44. struct mlxsw_sp_counter_pool {
  45. unsigned int pool_size;
  46. unsigned long *usage; /* Usage bitmap */
  47. struct mlxsw_sp_counter_sub_pool *sub_pools;
  48. };
  49. static struct mlxsw_sp_counter_sub_pool mlxsw_sp_counter_sub_pools[] = {
  50. [MLXSW_SP_COUNTER_SUB_POOL_FLOW] = {
  51. .bank_count = 6,
  52. },
  53. [MLXSW_SP_COUNTER_SUB_POOL_RIF] = {
  54. .bank_count = 2,
  55. }
  56. };
  57. static int mlxsw_sp_counter_pool_validate(struct mlxsw_sp *mlxsw_sp)
  58. {
  59. unsigned int total_bank_config = 0;
  60. unsigned int pool_size;
  61. int i;
  62. pool_size = MLXSW_CORE_RES_GET(mlxsw_sp->core, COUNTER_POOL_SIZE);
  63. /* Check config is valid, no bank over subscription */
  64. for (i = 0; i < ARRAY_SIZE(mlxsw_sp_counter_sub_pools); i++)
  65. total_bank_config += mlxsw_sp_counter_sub_pools[i].bank_count;
  66. if (total_bank_config > pool_size / MLXSW_SP_COUNTER_POOL_BANK_SIZE + 1)
  67. return -EINVAL;
  68. return 0;
  69. }
  70. static int mlxsw_sp_counter_sub_pools_prepare(struct mlxsw_sp *mlxsw_sp)
  71. {
  72. struct mlxsw_sp_counter_sub_pool *sub_pool;
  73. /* Prepare generic flow pool*/
  74. sub_pool = &mlxsw_sp_counter_sub_pools[MLXSW_SP_COUNTER_SUB_POOL_FLOW];
  75. if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, COUNTER_SIZE_PACKETS_BYTES))
  76. return -EIO;
  77. sub_pool->entry_size = MLXSW_CORE_RES_GET(mlxsw_sp->core,
  78. COUNTER_SIZE_PACKETS_BYTES);
  79. /* Prepare erif pool*/
  80. sub_pool = &mlxsw_sp_counter_sub_pools[MLXSW_SP_COUNTER_SUB_POOL_RIF];
  81. if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, COUNTER_SIZE_ROUTER_BASIC))
  82. return -EIO;
  83. sub_pool->entry_size = MLXSW_CORE_RES_GET(mlxsw_sp->core,
  84. COUNTER_SIZE_ROUTER_BASIC);
  85. return 0;
  86. }
  87. int mlxsw_sp_counter_pool_init(struct mlxsw_sp *mlxsw_sp)
  88. {
  89. struct mlxsw_sp_counter_sub_pool *sub_pool;
  90. struct mlxsw_sp_counter_pool *pool;
  91. unsigned int base_index;
  92. unsigned int map_size;
  93. int i;
  94. int err;
  95. if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, COUNTER_POOL_SIZE))
  96. return -EIO;
  97. err = mlxsw_sp_counter_pool_validate(mlxsw_sp);
  98. if (err)
  99. return err;
  100. err = mlxsw_sp_counter_sub_pools_prepare(mlxsw_sp);
  101. if (err)
  102. return err;
  103. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  104. if (!pool)
  105. return -ENOMEM;
  106. pool->pool_size = MLXSW_CORE_RES_GET(mlxsw_sp->core, COUNTER_POOL_SIZE);
  107. map_size = BITS_TO_LONGS(pool->pool_size) * sizeof(unsigned long);
  108. pool->usage = kzalloc(map_size, GFP_KERNEL);
  109. if (!pool->usage) {
  110. err = -ENOMEM;
  111. goto err_usage_alloc;
  112. }
  113. pool->sub_pools = mlxsw_sp_counter_sub_pools;
  114. /* Allocation is based on bank count which should be
  115. * specified for each sub pool statically.
  116. */
  117. base_index = 0;
  118. for (i = 0; i < ARRAY_SIZE(mlxsw_sp_counter_sub_pools); i++) {
  119. sub_pool = &pool->sub_pools[i];
  120. sub_pool->size = sub_pool->bank_count *
  121. MLXSW_SP_COUNTER_POOL_BANK_SIZE;
  122. sub_pool->base_index = base_index;
  123. base_index += sub_pool->size;
  124. /* The last bank can't be fully used */
  125. if (sub_pool->base_index + sub_pool->size > pool->pool_size)
  126. sub_pool->size = pool->pool_size - sub_pool->base_index;
  127. }
  128. mlxsw_sp->counter_pool = pool;
  129. return 0;
  130. err_usage_alloc:
  131. kfree(pool);
  132. return err;
  133. }
  134. void mlxsw_sp_counter_pool_fini(struct mlxsw_sp *mlxsw_sp)
  135. {
  136. struct mlxsw_sp_counter_pool *pool = mlxsw_sp->counter_pool;
  137. WARN_ON(find_first_bit(pool->usage, pool->pool_size) !=
  138. pool->pool_size);
  139. kfree(pool->usage);
  140. kfree(pool);
  141. }
  142. int mlxsw_sp_counter_alloc(struct mlxsw_sp *mlxsw_sp,
  143. enum mlxsw_sp_counter_sub_pool_id sub_pool_id,
  144. unsigned int *p_counter_index)
  145. {
  146. struct mlxsw_sp_counter_pool *pool = mlxsw_sp->counter_pool;
  147. struct mlxsw_sp_counter_sub_pool *sub_pool;
  148. unsigned int entry_index;
  149. unsigned int stop_index;
  150. int i;
  151. sub_pool = &mlxsw_sp_counter_sub_pools[sub_pool_id];
  152. stop_index = sub_pool->base_index + sub_pool->size;
  153. entry_index = sub_pool->base_index;
  154. entry_index = find_next_zero_bit(pool->usage, stop_index, entry_index);
  155. if (entry_index == stop_index)
  156. return -ENOBUFS;
  157. /* The sub-pools can contain non-integer number of entries
  158. * so we must check for overflow
  159. */
  160. if (entry_index + sub_pool->entry_size > stop_index)
  161. return -ENOBUFS;
  162. for (i = 0; i < sub_pool->entry_size; i++)
  163. __set_bit(entry_index + i, pool->usage);
  164. *p_counter_index = entry_index;
  165. return 0;
  166. }
  167. void mlxsw_sp_counter_free(struct mlxsw_sp *mlxsw_sp,
  168. enum mlxsw_sp_counter_sub_pool_id sub_pool_id,
  169. unsigned int counter_index)
  170. {
  171. struct mlxsw_sp_counter_pool *pool = mlxsw_sp->counter_pool;
  172. struct mlxsw_sp_counter_sub_pool *sub_pool;
  173. int i;
  174. if (WARN_ON(counter_index >= pool->pool_size))
  175. return;
  176. sub_pool = &mlxsw_sp_counter_sub_pools[sub_pool_id];
  177. for (i = 0; i < sub_pool->entry_size; i++)
  178. __clear_bit(counter_index + i, pool->usage);
  179. }