xref: /freebsd/sys/dev/mlx5/mlx5_en/en_rl.h (revision a7623790fb345e6dc986dfd31df0ace115e6f2e4)
1 /*-
2  * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 
28 #ifndef __MLX5_EN_RL_H__
29 #define __MLX5_EN_RL_H__
30 
31 #include <sys/param.h>
32 #include <sys/lock.h>
33 #include <sys/mutex.h>
34 #include <sys/sx.h>
35 #include <sys/proc.h>
36 #include <sys/condvar.h>
37 #include <sys/interrupt.h>
38 #include <sys/unistd.h>
39 
40 #include <sys/queue.h>
41 
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 */
46 
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
51 
52 #define	MLX5E_RL_WORKER_LOCK(rlw)		mtx_lock(&(rlw)->mtx)
53 #define	MLX5E_RL_WORKER_UNLOCK(rlw)		mtx_unlock(&(rlw)->mtx)
54 
55 #define	MLX5E_RL_RLOCK(rl) sx_slock(&(rl)->rl_sxlock)
56 #define	MLX5E_RL_RUNLOCK(rl) sx_sunlock(&(rl)->rl_sxlock)
57 
58 #define	MLX5E_RL_WLOCK(rl) sx_xlock(&(rl)->rl_sxlock)
59 #define	MLX5E_RL_WUNLOCK(rl) sx_xunlock(&(rl)->rl_sxlock)
60 
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")
79 
80 #define	MLX5E_RL_PARAMS_NUM (0 MLX5E_RL_PARAMS(MLX5E_STATS_COUNT))
81 
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")
89 
90 #define MLX5E_RL_STATS_NUM (0 MLX5E_RL_STATS(MLX5E_STATS_COUNT))
91 
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")
98 
99 #define	MLX5E_RL_TABLE_PARAMS_NUM (0 MLX5E_RL_TABLE_PARAMS(MLX5E_STATS_COUNT))
100 
101 #define	MLX5E_RL_PARAMS_INDEX(n)			\
102     (__offsetof(struct mlx5e_rl_params, n) / sizeof(uint64_t))
103 
104 struct mlx5e_priv;
105 
106 /* Indicates channel's state */
107 enum {
108 	MLX5E_RL_ST_FREE,
109 	MLX5E_RL_ST_USED,
110 	MLX5E_RL_ST_MODIFY,
111 	MLX5E_RL_ST_DESTROY,
112 };
113 
114 struct mlx5e_rl_stats {
115 	u64	arg [0];
116 	MLX5E_RL_STATS(MLX5E_STATS_VAR)
117 };
118 
119 struct mlx5e_rl_params {
120 	u64	arg [0];
121 	MLX5E_RL_PARAMS(MLX5E_STATS_VAR)
122 	u64	table_arg [0];
123 	MLX5E_RL_TABLE_PARAMS(MLX5E_STATS_VAR)
124 };
125 
126 struct mlx5e_rl_channel_param {
127 	struct mlx5e_sq_param sq;
128 	struct mlx5e_cq_param cq;
129 };
130 
131 struct mlx5e_rl_channel {
132 	struct m_snd_tag tag;
133 	STAILQ_ENTRY(mlx5e_rl_channel) entry;
134 	struct mlx5e_sq * volatile sq;
135 	struct mlx5e_rl_worker *worker;
136 	uint64_t new_rate;
137 	uint64_t init_rate;
138 	uint64_t last_rate;
139 	uint16_t last_burst;
140 	uint16_t state;
141 };
142 
143 struct mlx5e_rl_worker {
144 	struct mtx mtx;
145 	struct cv cv;
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;
151 };
152 
153 struct mlx5e_rl_priv_data {
154 	struct sx rl_sxlock;
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;
163 	unsigned opened;
164 	uint32_t tisn;
165 };
166 
167 int mlx5e_rl_init(struct mlx5e_priv *priv);
168 void mlx5e_rl_cleanup(struct mlx5e_priv *priv);
169 void mlx5e_rl_refresh_sq_inline(struct mlx5e_rl_priv_data *rl);
170 
171 if_snd_tag_alloc_t mlx5e_rl_snd_tag_alloc;
172 if_snd_tag_modify_t mlx5e_rl_snd_tag_modify;
173 if_snd_tag_query_t mlx5e_rl_snd_tag_query;
174 if_snd_tag_free_t mlx5e_rl_snd_tag_free;
175 
176 #endif		/* __MLX5_EN_RL_H__ */
177