rl.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. /* Scheduling element fw management */
  38. int mlx5_create_scheduling_element_cmd(struct mlx5_core_dev *dev, u8 hierarchy,
  39. void *ctx, u32 *element_id)
  40. {
  41. u32 in[MLX5_ST_SZ_DW(create_scheduling_element_in)] = {0};
  42. u32 out[MLX5_ST_SZ_DW(create_scheduling_element_in)] = {0};
  43. void *schedc;
  44. int err;
  45. schedc = MLX5_ADDR_OF(create_scheduling_element_in, in,
  46. scheduling_context);
  47. MLX5_SET(create_scheduling_element_in, in, opcode,
  48. MLX5_CMD_OP_CREATE_SCHEDULING_ELEMENT);
  49. MLX5_SET(create_scheduling_element_in, in, scheduling_hierarchy,
  50. hierarchy);
  51. memcpy(schedc, ctx, MLX5_ST_SZ_BYTES(scheduling_context));
  52. err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  53. if (err)
  54. return err;
  55. *element_id = MLX5_GET(create_scheduling_element_out, out,
  56. scheduling_element_id);
  57. return 0;
  58. }
  59. int mlx5_modify_scheduling_element_cmd(struct mlx5_core_dev *dev, u8 hierarchy,
  60. void *ctx, u32 element_id,
  61. u32 modify_bitmask)
  62. {
  63. u32 in[MLX5_ST_SZ_DW(modify_scheduling_element_in)] = {0};
  64. u32 out[MLX5_ST_SZ_DW(modify_scheduling_element_in)] = {0};
  65. void *schedc;
  66. schedc = MLX5_ADDR_OF(modify_scheduling_element_in, in,
  67. scheduling_context);
  68. MLX5_SET(modify_scheduling_element_in, in, opcode,
  69. MLX5_CMD_OP_MODIFY_SCHEDULING_ELEMENT);
  70. MLX5_SET(modify_scheduling_element_in, in, scheduling_element_id,
  71. element_id);
  72. MLX5_SET(modify_scheduling_element_in, in, modify_bitmask,
  73. modify_bitmask);
  74. MLX5_SET(modify_scheduling_element_in, in, scheduling_hierarchy,
  75. hierarchy);
  76. memcpy(schedc, ctx, MLX5_ST_SZ_BYTES(scheduling_context));
  77. return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  78. }
  79. int mlx5_destroy_scheduling_element_cmd(struct mlx5_core_dev *dev, u8 hierarchy,
  80. u32 element_id)
  81. {
  82. u32 in[MLX5_ST_SZ_DW(destroy_scheduling_element_in)] = {0};
  83. u32 out[MLX5_ST_SZ_DW(destroy_scheduling_element_in)] = {0};
  84. MLX5_SET(destroy_scheduling_element_in, in, opcode,
  85. MLX5_CMD_OP_DESTROY_SCHEDULING_ELEMENT);
  86. MLX5_SET(destroy_scheduling_element_in, in, scheduling_element_id,
  87. element_id);
  88. MLX5_SET(destroy_scheduling_element_in, in, scheduling_hierarchy,
  89. hierarchy);
  90. return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  91. }
  92. /* Finds an entry where we can register the given rate
  93. * If the rate already exists, return the entry where it is registered,
  94. * otherwise return the first available entry.
  95. * If the table is full, return NULL
  96. */
  97. static struct mlx5_rl_entry *find_rl_entry(struct mlx5_rl_table *table,
  98. struct mlx5_rate_limit *rl)
  99. {
  100. struct mlx5_rl_entry *ret_entry = NULL;
  101. bool empty_found = false;
  102. int i;
  103. for (i = 0; i < table->max_size; i++) {
  104. if (mlx5_rl_are_equal(&table->rl_entry[i].rl, rl))
  105. return &table->rl_entry[i];
  106. if (!empty_found && !table->rl_entry[i].rl.rate) {
  107. empty_found = true;
  108. ret_entry = &table->rl_entry[i];
  109. }
  110. }
  111. return ret_entry;
  112. }
  113. static int mlx5_set_pp_rate_limit_cmd(struct mlx5_core_dev *dev,
  114. u16 index,
  115. struct mlx5_rate_limit *rl)
  116. {
  117. u32 in[MLX5_ST_SZ_DW(set_pp_rate_limit_in)] = {0};
  118. u32 out[MLX5_ST_SZ_DW(set_pp_rate_limit_out)] = {0};
  119. MLX5_SET(set_pp_rate_limit_in, in, opcode,
  120. MLX5_CMD_OP_SET_PP_RATE_LIMIT);
  121. MLX5_SET(set_pp_rate_limit_in, in, rate_limit_index, index);
  122. MLX5_SET(set_pp_rate_limit_in, in, rate_limit, rl->rate);
  123. MLX5_SET(set_pp_rate_limit_in, in, burst_upper_bound, rl->max_burst_sz);
  124. MLX5_SET(set_pp_rate_limit_in, in, typical_packet_size, rl->typical_pkt_sz);
  125. return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
  126. }
  127. bool mlx5_rl_is_in_range(struct mlx5_core_dev *dev, u32 rate)
  128. {
  129. struct mlx5_rl_table *table = &dev->priv.rl_table;
  130. return (rate <= table->max_rate && rate >= table->min_rate);
  131. }
  132. EXPORT_SYMBOL(mlx5_rl_is_in_range);
  133. bool mlx5_rl_are_equal(struct mlx5_rate_limit *rl_0,
  134. struct mlx5_rate_limit *rl_1)
  135. {
  136. return ((rl_0->rate == rl_1->rate) &&
  137. (rl_0->max_burst_sz == rl_1->max_burst_sz) &&
  138. (rl_0->typical_pkt_sz == rl_1->typical_pkt_sz));
  139. }
  140. EXPORT_SYMBOL(mlx5_rl_are_equal);
  141. int mlx5_rl_add_rate(struct mlx5_core_dev *dev, u16 *index,
  142. struct mlx5_rate_limit *rl)
  143. {
  144. struct mlx5_rl_table *table = &dev->priv.rl_table;
  145. struct mlx5_rl_entry *entry;
  146. int err = 0;
  147. mutex_lock(&table->rl_lock);
  148. if (!rl->rate || !mlx5_rl_is_in_range(dev, rl->rate)) {
  149. mlx5_core_err(dev, "Invalid rate: %u, should be %u to %u\n",
  150. rl->rate, table->min_rate, table->max_rate);
  151. err = -EINVAL;
  152. goto out;
  153. }
  154. entry = find_rl_entry(table, rl);
  155. if (!entry) {
  156. mlx5_core_err(dev, "Max number of %u rates reached\n",
  157. table->max_size);
  158. err = -ENOSPC;
  159. goto out;
  160. }
  161. if (entry->refcount) {
  162. /* rate already configured */
  163. entry->refcount++;
  164. } else {
  165. /* new rate limit */
  166. err = mlx5_set_pp_rate_limit_cmd(dev, entry->index, rl);
  167. if (err) {
  168. mlx5_core_err(dev, "Failed configuring rate limit(err %d): \
  169. rate %u, max_burst_sz %u, typical_pkt_sz %u\n",
  170. err, rl->rate, rl->max_burst_sz,
  171. rl->typical_pkt_sz);
  172. goto out;
  173. }
  174. entry->rl = *rl;
  175. entry->refcount = 1;
  176. }
  177. *index = entry->index;
  178. out:
  179. mutex_unlock(&table->rl_lock);
  180. return err;
  181. }
  182. EXPORT_SYMBOL(mlx5_rl_add_rate);
  183. void mlx5_rl_remove_rate(struct mlx5_core_dev *dev, struct mlx5_rate_limit *rl)
  184. {
  185. struct mlx5_rl_table *table = &dev->priv.rl_table;
  186. struct mlx5_rl_entry *entry = NULL;
  187. struct mlx5_rate_limit reset_rl = {0};
  188. /* 0 is a reserved value for unlimited rate */
  189. if (rl->rate == 0)
  190. return;
  191. mutex_lock(&table->rl_lock);
  192. entry = find_rl_entry(table, rl);
  193. if (!entry || !entry->refcount) {
  194. mlx5_core_warn(dev, "Rate %u, max_burst_sz %u typical_pkt_sz %u \
  195. are not configured\n",
  196. rl->rate, rl->max_burst_sz, rl->typical_pkt_sz);
  197. goto out;
  198. }
  199. entry->refcount--;
  200. if (!entry->refcount) {
  201. /* need to remove rate */
  202. mlx5_set_pp_rate_limit_cmd(dev, entry->index, &reset_rl);
  203. entry->rl = reset_rl;
  204. }
  205. out:
  206. mutex_unlock(&table->rl_lock);
  207. }
  208. EXPORT_SYMBOL(mlx5_rl_remove_rate);
  209. int mlx5_init_rl_table(struct mlx5_core_dev *dev)
  210. {
  211. struct mlx5_rl_table *table = &dev->priv.rl_table;
  212. int i;
  213. mutex_init(&table->rl_lock);
  214. if (!MLX5_CAP_GEN(dev, qos) || !MLX5_CAP_QOS(dev, packet_pacing)) {
  215. table->max_size = 0;
  216. return 0;
  217. }
  218. /* First entry is reserved for unlimited rate */
  219. table->max_size = MLX5_CAP_QOS(dev, packet_pacing_rate_table_size) - 1;
  220. table->max_rate = MLX5_CAP_QOS(dev, packet_pacing_max_rate);
  221. table->min_rate = MLX5_CAP_QOS(dev, packet_pacing_min_rate);
  222. table->rl_entry = kcalloc(table->max_size, sizeof(struct mlx5_rl_entry),
  223. GFP_KERNEL);
  224. if (!table->rl_entry)
  225. return -ENOMEM;
  226. /* The index represents the index in HW rate limit table
  227. * Index 0 is reserved for unlimited rate
  228. */
  229. for (i = 0; i < table->max_size; i++)
  230. table->rl_entry[i].index = i + 1;
  231. /* Index 0 is reserved */
  232. mlx5_core_info(dev, "Rate limit: %u rates are supported, range: %uMbps to %uMbps\n",
  233. table->max_size,
  234. table->min_rate >> 10,
  235. table->max_rate >> 10);
  236. return 0;
  237. }
  238. void mlx5_cleanup_rl_table(struct mlx5_core_dev *dev)
  239. {
  240. struct mlx5_rl_table *table = &dev->priv.rl_table;
  241. struct mlx5_rate_limit rl = {0};
  242. int i;
  243. /* Clear all configured rates */
  244. for (i = 0; i < table->max_size; i++)
  245. if (table->rl_entry[i].rl.rate)
  246. mlx5_set_pp_rate_limit_cmd(dev, table->rl_entry[i].index,
  247. &rl);
  248. kfree(dev->priv.rl_table.rl_entry);
  249. }