2 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #ifndef __MLX5_EN_RL_H__
29 #define __MLX5_EN_RL_H__
31 #include <sys/param.h>
33 #include <sys/mutex.h>
36 #include <sys/condvar.h>
37 #include <sys/interrupt.h>
38 #include <sys/unistd.h>
40 #include <sys/queue.h>
42 #define MLX5E_RL_MAX_WORKERS 128 /* limited by Toeplitz hash */
43 #define MLX5E_RL_MAX_TX_RATES (64 * 1024) /* software limit */
44 #define MLX5E_RL_DEF_SQ_PER_WORKER (12 * 1024) /* software limit */
45 #define MLX5E_RL_MAX_SQS (120 * 1024) /* software limit */
47 #define MLX5E_RL_TX_COAL_USEC_DEFAULT 32
48 #define MLX5E_RL_TX_COAL_PKTS_DEFAULT 4
49 #define MLX5E_RL_TX_COAL_MODE_DEFAULT 0
50 #define MLX5E_RL_TX_COMP_FACT_DEFAULT 1
52 #define MLX5E_RL_WORKER_LOCK(rlw) mtx_lock(&(rlw)->mtx)
53 #define MLX5E_RL_WORKER_UNLOCK(rlw) mtx_unlock(&(rlw)->mtx)
55 #define MLX5E_RL_RLOCK(rl) sx_slock(&(rl)->rl_sxlock)
56 #define MLX5E_RL_RUNLOCK(rl) sx_sunlock(&(rl)->rl_sxlock)
58 #define MLX5E_RL_WLOCK(rl) sx_xlock(&(rl)->rl_sxlock)
59 #define MLX5E_RL_WUNLOCK(rl) sx_xunlock(&(rl)->rl_sxlock)
61 #define MLX5E_RL_PARAMS(m) \
62 m(+1, u64 tx_queue_size, "tx_queue_size", "Default send queue size") \
63 m(+1, u64 tx_coalesce_usecs, "tx_coalesce_usecs", "Limit in usec for joining TX packets") \
64 m(+1, u64 tx_coalesce_pkts, "tx_coalesce_pkts", "Maximum number of TX packets to join") \
65 m(+1, u64 tx_coalesce_mode, "tx_coalesce_mode", "0: EQE mode 1: CQE mode") \
66 m(+1, u64 tx_completion_fact, "tx_completion_fact", "1..MAX: Completion event ratio") \
67 m(+1, u64 tx_completion_fact_max, "tx_completion_fact_max", "Maximum completion event ratio") \
68 m(+1, u64 tx_worker_threads_max, "tx_worker_threads_max", "Max number of TX worker threads") \
69 m(+1, u64 tx_worker_threads_def, "tx_worker_threads_def", "Default number of TX worker threads") \
70 m(+1, u64 tx_channels_per_worker_max, "tx_channels_per_worker_max", "Max number of TX channels per worker") \
71 m(+1, u64 tx_channels_per_worker_def, "tx_channels_per_worker_def", "Default number of TX channels per worker") \
72 m(+1, u64 tx_rates_max, "tx_rates_max", "Max number of TX rates") \
73 m(+1, u64 tx_rates_def, "tx_rates_def", "Default number of TX rates") \
74 m(+1, u64 tx_limit_min, "tx_limit_min", "Minimum TX rate in bits/s") \
75 m(+1, u64 tx_limit_max, "tx_limit_max", "Maximum TX rate in bits/s") \
76 m(+1, u64 tx_burst_size, "tx_burst_size", "Current burst size in number of packets. A value of zero means use firmware default.") \
77 m(+1, u64 tx_burst_size_max, "tx_burst_size_max", "Maximum burst size in number of packets") \
78 m(+1, u64 tx_burst_size_min, "tx_burst_size_min", "Minimum burst size in number of packets")
80 #define MLX5E_RL_PARAMS_NUM (0 MLX5E_RL_PARAMS(MLX5E_STATS_COUNT))
82 #define MLX5E_RL_STATS(m) \
83 m(+1, u64 tx_allocate_resource_failure, "tx_allocate_resource_failure", "Number of times firmware resource allocation failed") \
84 m(+1, u64 tx_add_new_rate_failure, "tx_add_new_rate_failure", "Number of times adding a new firmware rate failed") \
85 m(+1, u64 tx_modify_rate_failure, "tx_modify_rate_failure", "Number of times modifying a firmware rate failed") \
86 m(+1, u64 tx_active_connections, "tx_active_connections", "Number of active connections") \
87 m(+1, u64 tx_open_queues, "tx_open_queues", "Number of open TX queues") \
88 m(+1, u64 tx_available_resource_failure, "tx_available_resource_failure", "Number of times TX resources were not available")
90 #define MLX5E_RL_STATS_NUM (0 MLX5E_RL_STATS(MLX5E_STATS_COUNT))
92 #define MLX5E_RL_TABLE_PARAMS(m) \
93 m(+1, u64 tx_limit_add, "tx_limit_add", "Add TX rate limit in bits/s to empty slot") \
94 m(+1, u64 tx_limit_clr, "tx_limit_clr", "Clear all TX rates in table") \
95 m(+1, u64 tx_allowed_deviation, "tx_allowed_deviation", "Relative rate deviation allowed in 1/1000") \
96 m(+1, u64 tx_allowed_deviation_min, "tx_allowed_deviation_min", "Minimum allowed rate deviation in 1/1000") \
97 m(+1, u64 tx_allowed_deviation_max, "tx_allowed_deviation_max", "Maximum allowed rate deviation in 1/1000")
99 #define MLX5E_RL_TABLE_PARAMS_NUM (0 MLX5E_RL_TABLE_PARAMS(MLX5E_STATS_COUNT))
101 #define MLX5E_RL_PARAMS_INDEX(n) \
102 (__offsetof(struct mlx5e_rl_params, n) / sizeof(uint64_t))
106 /* Indicates channel's state */
114 struct mlx5e_rl_stats {
116 MLX5E_RL_STATS(MLX5E_STATS_VAR)
119 struct mlx5e_rl_params {
121 MLX5E_RL_PARAMS(MLX5E_STATS_VAR)
123 MLX5E_RL_TABLE_PARAMS(MLX5E_STATS_VAR)
126 struct mlx5e_rl_channel_param {
127 struct mlx5e_sq_param sq;
128 struct mlx5e_cq_param cq;
131 struct mlx5e_rl_channel {
132 struct m_snd_tag m_snd_tag;
133 STAILQ_ENTRY(mlx5e_rl_channel) entry;
134 struct mlx5e_sq * volatile sq;
135 struct mlx5e_rl_worker *worker;
143 struct mlx5e_rl_worker {
146 STAILQ_HEAD(, mlx5e_rl_channel) index_list_head;
147 STAILQ_HEAD(, mlx5e_rl_channel) process_head;
148 struct mlx5e_priv *priv;
149 struct mlx5e_rl_channel *channels;
150 unsigned worker_done;
153 struct mlx5e_rl_priv_data {
155 struct sysctl_ctx_list ctx;
156 struct mlx5e_rl_channel_param chan_param;
157 struct mlx5e_rl_params param;
158 struct mlx5e_rl_stats stats;
159 struct mlx5_uar sq_uar;
160 struct mlx5e_rl_worker *workers;
161 struct mlx5e_priv *priv;
162 uint64_t *rate_limit_table;
167 int mlx5e_rl_init(struct mlx5e_priv *priv);
168 void mlx5e_rl_cleanup(struct mlx5e_priv *priv);
169 if_snd_tag_alloc_t mlx5e_rl_snd_tag_alloc;
170 if_snd_tag_modify_t mlx5e_rl_snd_tag_modify;
171 if_snd_tag_query_t mlx5e_rl_snd_tag_query;
172 if_snd_tag_free_t mlx5e_rl_snd_tag_free;
174 #endif /* __MLX5_EN_RL_H__ */