rl.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2013-2016, Mellanox Technologies. 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/module.h>
  34. #include <linux/mlx5/driver.h>
  35. #include <linux/mlx5/cmd.h>
  36. #include "mlx5_core.h"
  37. /* Finds an entry where we can register the given rate
  38. * If the rate already exists, return the entry where it is registered,
  39. * otherwise return the first available entry.
  40. * If the table is full, return NULL
  41. */
  42. static struct mlx5_rl_entry *find_rl_entry(struct mlx5_rl_table *table,
  43. u32 rate)
  44. {
  45. struct mlx5_rl_entry *ret_entry = NULL;
  46. bool empty_found = false;
  47. int i;
  48. for (i = 0; i < table->max_size; i++) {
  49. if (table->rl_entry[i].rate == rate)
  50. return &table->rl_entry[i];
  51. if (!empty_found && !table->rl_entry[i].rate) {
  52. empty_found = true;
  53. ret_entry = &table->rl_entry[i];
  54. }
  55. }
  56. return ret_entry;
  57. }
  58. static int mlx5_set_pp_rate_limit_cmd(struct mlx5_core_dev *dev,
  59. u32 rate, u16 index)
  60. {
  61. u32 in[MLX5_ST_SZ_DW(set_pp_rate_limit_in)] = {0};
  62. u32 out[MLX5_ST_SZ_DW(set_pp_rate_limit_out)] = {0};
  63. MLX5_SET(set_pp_rate_limit_in, in, opcode,
  64. MLX5_CMD_OP_SET_PP_RATE_LIMIT);
  65. MLX5_SET(set_pp_rate_limit_in, in, rate_limit_index, index);
  66. MLX5_SET(set_pp_rate_limit_in, in, rate_limit, rate);
  67. return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  68. }
  69. bool mlx5_rl_is_in_range(struct mlx5_core_dev *dev, u32 rate)
  70. {
  71. struct mlx5_rl_table *table = &dev->priv.rl_table;
  72. return (rate <= table->max_rate && rate >= table->min_rate);
  73. }
  74. EXPORT_SYMBOL(mlx5_rl_is_in_range);
  75. int mlx5_rl_add_rate(struct mlx5_core_dev *dev, u32 rate, u16 *index)
  76. {
  77. struct mlx5_rl_table *table = &dev->priv.rl_table;
  78. struct mlx5_rl_entry *entry;
  79. int err = 0;
  80. mutex_lock(&table->rl_lock);
  81. if (!rate || !mlx5_rl_is_in_range(dev, rate)) {
  82. mlx5_core_err(dev, "Invalid rate: %u, should be %u to %u\n",
  83. rate, table->min_rate, table->max_rate);
  84. err = -EINVAL;
  85. goto out;
  86. }
  87. entry = find_rl_entry(table, rate);
  88. if (!entry) {
  89. mlx5_core_err(dev, "Max number of %u rates reached\n",
  90. table->max_size);
  91. err = -ENOSPC;
  92. goto out;
  93. }
  94. if (entry->refcount) {
  95. /* rate already configured */
  96. entry->refcount++;
  97. } else {
  98. /* new rate limit */
  99. err = mlx5_set_pp_rate_limit_cmd(dev, rate, entry->index);
  100. if (err) {
  101. mlx5_core_err(dev, "Failed configuring rate: %u (%d)\n",
  102. rate, err);
  103. goto out;
  104. }
  105. entry->rate = rate;
  106. entry->refcount = 1;
  107. }
  108. *index = entry->index;
  109. out:
  110. mutex_unlock(&table->rl_lock);
  111. return err;
  112. }
  113. EXPORT_SYMBOL(mlx5_rl_add_rate);
  114. void mlx5_rl_remove_rate(struct mlx5_core_dev *dev, u32 rate)
  115. {
  116. struct mlx5_rl_table *table = &dev->priv.rl_table;
  117. struct mlx5_rl_entry *entry = NULL;
  118. /* 0 is a reserved value for unlimited rate */
  119. if (rate == 0)
  120. return;
  121. mutex_lock(&table->rl_lock);
  122. entry = find_rl_entry(table, rate);
  123. if (!entry || !entry->refcount) {
  124. mlx5_core_warn(dev, "Rate %u is not configured\n", rate);
  125. goto out;
  126. }
  127. entry->refcount--;
  128. if (!entry->refcount) {
  129. /* need to remove rate */
  130. mlx5_set_pp_rate_limit_cmd(dev, 0, entry->index);
  131. entry->rate = 0;
  132. }
  133. out:
  134. mutex_unlock(&table->rl_lock);
  135. }
  136. EXPORT_SYMBOL(mlx5_rl_remove_rate);
  137. int mlx5_init_rl_table(struct mlx5_core_dev *dev)
  138. {
  139. struct mlx5_rl_table *table = &dev->priv.rl_table;
  140. int i;
  141. mutex_init(&table->rl_lock);
  142. if (!MLX5_CAP_GEN(dev, qos) || !MLX5_CAP_QOS(dev, packet_pacing)) {
  143. table->max_size = 0;
  144. return 0;
  145. }
  146. /* First entry is reserved for unlimited rate */
  147. table->max_size = MLX5_CAP_QOS(dev, packet_pacing_rate_table_size) - 1;
  148. table->max_rate = MLX5_CAP_QOS(dev, packet_pacing_max_rate);
  149. table->min_rate = MLX5_CAP_QOS(dev, packet_pacing_min_rate);
  150. table->rl_entry = kcalloc(table->max_size, sizeof(struct mlx5_rl_entry),
  151. GFP_KERNEL);
  152. if (!table->rl_entry)
  153. return -ENOMEM;
  154. /* The index represents the index in HW rate limit table
  155. * Index 0 is reserved for unlimited rate
  156. */
  157. for (i = 0; i < table->max_size; i++)
  158. table->rl_entry[i].index = i + 1;
  159. /* Index 0 is reserved */
  160. mlx5_core_info(dev, "Rate limit: %u rates are supported, range: %uMbps to %uMbps\n",
  161. table->max_size,
  162. table->min_rate >> 10,
  163. table->max_rate >> 10);
  164. return 0;
  165. }
  166. void mlx5_cleanup_rl_table(struct mlx5_core_dev *dev)
  167. {
  168. struct mlx5_rl_table *table = &dev->priv.rl_table;
  169. int i;
  170. /* Clear all configured rates */
  171. for (i = 0; i < table->max_size; i++)
  172. if (table->rl_entry[i].rate)
  173. mlx5_set_pp_rate_limit_cmd(dev, 0,
  174. table->rl_entry[i].index);
  175. kfree(dev->priv.rl_table.rl_entry);
  176. }