1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2020, Mellanox Technologies inc. All rights reserved. */
3 #include <net/sch_generic.h>
4
5 #include <net/pkt_cls.h>
6 #include "en.h"
7 #include "params.h"
8 #include "../qos.h"
9 #include "en/htb.h"
10
11 struct qos_sq_callback_params {
12 struct mlx5e_priv *priv;
13 struct mlx5e_channels *chs;
14 };
15
mlx5e_qos_bytes_rate_check(struct mlx5_core_dev * mdev,u64 nbytes)16 int mlx5e_qos_bytes_rate_check(struct mlx5_core_dev *mdev, u64 nbytes)
17 {
18 if (nbytes < BYTES_IN_MBIT) {
19 qos_warn(mdev, "Input rate (%llu Bytes/sec) below minimum supported (%u Bytes/sec)\n",
20 nbytes, BYTES_IN_MBIT);
21 return -EINVAL;
22 }
23 return 0;
24 }
25
mlx5e_qos_bytes2mbits(struct mlx5_core_dev * mdev,u64 nbytes)26 static u32 mlx5e_qos_bytes2mbits(struct mlx5_core_dev *mdev, u64 nbytes)
27 {
28 return div_u64(nbytes, BYTES_IN_MBIT);
29 }
30
mlx5e_qos_max_leaf_nodes(struct mlx5_core_dev * mdev)31 int mlx5e_qos_max_leaf_nodes(struct mlx5_core_dev *mdev)
32 {
33 return min(MLX5E_QOS_MAX_LEAF_NODES, mlx5_qos_max_leaf_nodes(mdev));
34 }
35
36 /* TX datapath API */
37
mlx5e_qid_from_qos(struct mlx5e_channels * chs,u16 qid)38 u16 mlx5e_qid_from_qos(struct mlx5e_channels *chs, u16 qid)
39 {
40 /* These channel params are safe to access from the datapath, because:
41 * 1. This function is called only after checking selq->htb_maj_id != 0,
42 * and the number of queues can't change while HTB offload is active.
43 * 2. When selq->htb_maj_id becomes 0, synchronize_rcu waits for
44 * mlx5e_select_queue to finish while holding priv->state_lock,
45 * preventing other code from changing the number of queues.
46 */
47 bool is_ptp = MLX5E_GET_PFLAG(&chs->params, MLX5E_PFLAG_TX_PORT_TS);
48
49 return (chs->params.num_channels + is_ptp) * mlx5e_get_dcb_num_tc(&chs->params) + qid;
50 }
51
52 /* SQ lifecycle */
53
mlx5e_get_qos_sq(struct mlx5e_priv * priv,int qid)54 static struct mlx5e_txqsq *mlx5e_get_qos_sq(struct mlx5e_priv *priv, int qid)
55 {
56 struct mlx5e_params *params = &priv->channels.params;
57 struct mlx5e_txqsq __rcu **qos_sqs;
58 struct mlx5e_channel *c;
59 int ix;
60
61 ix = qid % params->num_channels;
62 qid /= params->num_channels;
63 c = priv->channels.c[ix];
64
65 qos_sqs = mlx5e_state_dereference(priv, c->qos_sqs);
66 return mlx5e_state_dereference(priv, qos_sqs[qid]);
67 }
68
mlx5e_open_qos_sq(struct mlx5e_priv * priv,struct mlx5e_channels * chs,u16 node_qid,u32 hw_id)69 int mlx5e_open_qos_sq(struct mlx5e_priv *priv, struct mlx5e_channels *chs,
70 u16 node_qid, u32 hw_id)
71 {
72 struct mlx5e_create_cq_param ccp = {};
73 struct mlx5e_txqsq __rcu **qos_sqs;
74 struct mlx5e_sq_param param_sq;
75 struct mlx5e_cq_param param_cq;
76 int txq_ix, ix, qid, err = 0;
77 struct mlx5e_params *params;
78 struct mlx5e_channel *c;
79 struct mlx5e_txqsq *sq;
80
81 params = &chs->params;
82
83 txq_ix = mlx5e_qid_from_qos(chs, node_qid);
84
85 WARN_ON(node_qid >= mlx5e_htb_cur_leaf_nodes(priv->htb));
86 if (!priv->htb_qos_sq_stats) {
87 struct mlx5e_sq_stats **stats_list;
88
89 stats_list = kvcalloc(mlx5e_qos_max_leaf_nodes(priv->mdev),
90 sizeof(*stats_list), GFP_KERNEL);
91 if (!stats_list)
92 return -ENOMEM;
93
94 WRITE_ONCE(priv->htb_qos_sq_stats, stats_list);
95 }
96
97 if (!priv->htb_qos_sq_stats[node_qid]) {
98 struct mlx5e_sq_stats *stats;
99
100 stats = kzalloc(sizeof(*stats), GFP_KERNEL);
101 if (!stats)
102 return -ENOMEM;
103
104 WRITE_ONCE(priv->htb_qos_sq_stats[node_qid], stats);
105 /* Order htb_max_qos_sqs increment after writing the array pointer.
106 * Pairs with smp_load_acquire in en_stats.c.
107 */
108 smp_store_release(&priv->htb_max_qos_sqs, priv->htb_max_qos_sqs + 1);
109 }
110
111 ix = node_qid % params->num_channels;
112 qid = node_qid / params->num_channels;
113 c = chs->c[ix];
114
115 qos_sqs = mlx5e_state_dereference(priv, c->qos_sqs);
116 sq = kzalloc(sizeof(*sq), GFP_KERNEL);
117
118 if (!sq)
119 return -ENOMEM;
120
121 mlx5e_build_create_cq_param(&ccp, c);
122
123 memset(¶m_sq, 0, sizeof(param_sq));
124 memset(¶m_cq, 0, sizeof(param_cq));
125 mlx5e_build_sq_param(priv->mdev, params, ¶m_sq);
126 mlx5e_build_tx_cq_param(priv->mdev, params, ¶m_cq);
127 err = mlx5e_open_cq(priv, params->tx_cq_moderation, ¶m_cq, &ccp, &sq->cq);
128 if (err)
129 goto err_free_sq;
130 err = mlx5e_open_txqsq(c, priv->tisn[c->lag_port][0], txq_ix, params,
131 ¶m_sq, sq, 0, hw_id,
132 priv->htb_qos_sq_stats[node_qid]);
133 if (err)
134 goto err_close_cq;
135
136 rcu_assign_pointer(qos_sqs[qid], sq);
137
138 return 0;
139
140 err_close_cq:
141 mlx5e_close_cq(&sq->cq);
142 err_free_sq:
143 kfree(sq);
144 return err;
145 }
146
mlx5e_open_qos_sq_cb_wrapper(void * data,u16 node_qid,u32 hw_id)147 static int mlx5e_open_qos_sq_cb_wrapper(void *data, u16 node_qid, u32 hw_id)
148 {
149 struct qos_sq_callback_params *cb_params = data;
150
151 return mlx5e_open_qos_sq(cb_params->priv, cb_params->chs, node_qid, hw_id);
152 }
153
mlx5e_activate_qos_sq(void * data,u16 node_qid,u32 hw_id)154 int mlx5e_activate_qos_sq(void *data, u16 node_qid, u32 hw_id)
155 {
156 struct mlx5e_priv *priv = data;
157 struct mlx5e_txqsq *sq;
158 u16 qid;
159
160 sq = mlx5e_get_qos_sq(priv, node_qid);
161
162 qid = mlx5e_qid_from_qos(&priv->channels, node_qid);
163
164 /* If it's a new queue, it will be marked as started at this point.
165 * Stop it before updating txq2sq.
166 */
167 mlx5e_tx_disable_queue(netdev_get_tx_queue(priv->netdev, qid));
168
169 priv->txq2sq[qid] = sq;
170
171 /* Make the change to txq2sq visible before the queue is started.
172 * As mlx5e_xmit runs under a spinlock, there is an implicit ACQUIRE,
173 * which pairs with this barrier.
174 */
175 smp_wmb();
176
177 qos_dbg(priv->mdev, "Activate QoS SQ qid %u\n", node_qid);
178 mlx5e_activate_txqsq(sq);
179
180 return 0;
181 }
182
mlx5e_deactivate_qos_sq(struct mlx5e_priv * priv,u16 qid)183 void mlx5e_deactivate_qos_sq(struct mlx5e_priv *priv, u16 qid)
184 {
185 struct mlx5e_txqsq *sq;
186
187 sq = mlx5e_get_qos_sq(priv, qid);
188 if (!sq) /* Handle the case when the SQ failed to open. */
189 return;
190
191 qos_dbg(priv->mdev, "Deactivate QoS SQ qid %u\n", qid);
192 mlx5e_deactivate_txqsq(sq);
193
194 priv->txq2sq[mlx5e_qid_from_qos(&priv->channels, qid)] = NULL;
195
196 /* Make the change to txq2sq visible before the queue is started again.
197 * As mlx5e_xmit runs under a spinlock, there is an implicit ACQUIRE,
198 * which pairs with this barrier.
199 */
200 smp_wmb();
201 }
202
mlx5e_close_qos_sq(struct mlx5e_priv * priv,u16 qid)203 void mlx5e_close_qos_sq(struct mlx5e_priv *priv, u16 qid)
204 {
205 struct mlx5e_txqsq __rcu **qos_sqs;
206 struct mlx5e_params *params;
207 struct mlx5e_channel *c;
208 struct mlx5e_txqsq *sq;
209 int ix;
210
211 params = &priv->channels.params;
212
213 ix = qid % params->num_channels;
214 qid /= params->num_channels;
215 c = priv->channels.c[ix];
216 qos_sqs = mlx5e_state_dereference(priv, c->qos_sqs);
217 sq = rcu_replace_pointer(qos_sqs[qid], NULL, lockdep_is_held(&priv->state_lock));
218 if (!sq) /* Handle the case when the SQ failed to open. */
219 return;
220
221 synchronize_rcu(); /* Sync with NAPI. */
222
223 mlx5e_close_txqsq(sq);
224 mlx5e_close_cq(&sq->cq);
225 kfree(sq);
226 }
227
mlx5e_qos_close_queues(struct mlx5e_channel * c)228 void mlx5e_qos_close_queues(struct mlx5e_channel *c)
229 {
230 struct mlx5e_txqsq __rcu **qos_sqs;
231 int i;
232
233 qos_sqs = rcu_replace_pointer(c->qos_sqs, NULL, lockdep_is_held(&c->priv->state_lock));
234 if (!qos_sqs)
235 return;
236 synchronize_rcu(); /* Sync with NAPI. */
237
238 for (i = 0; i < c->qos_sqs_size; i++) {
239 struct mlx5e_txqsq *sq;
240
241 sq = mlx5e_state_dereference(c->priv, qos_sqs[i]);
242 if (!sq) /* Handle the case when the SQ failed to open. */
243 continue;
244
245 mlx5e_close_txqsq(sq);
246 mlx5e_close_cq(&sq->cq);
247 kfree(sq);
248 }
249
250 kvfree(qos_sqs);
251 }
252
mlx5e_qos_close_all_queues(struct mlx5e_channels * chs)253 void mlx5e_qos_close_all_queues(struct mlx5e_channels *chs)
254 {
255 int i;
256
257 for (i = 0; i < chs->num; i++)
258 mlx5e_qos_close_queues(chs->c[i]);
259 }
260
mlx5e_qos_alloc_queues(struct mlx5e_priv * priv,struct mlx5e_channels * chs)261 int mlx5e_qos_alloc_queues(struct mlx5e_priv *priv, struct mlx5e_channels *chs)
262 {
263 u16 qos_sqs_size;
264 int i;
265
266 qos_sqs_size = DIV_ROUND_UP(mlx5e_qos_max_leaf_nodes(priv->mdev), chs->num);
267
268 for (i = 0; i < chs->num; i++) {
269 struct mlx5e_txqsq **sqs;
270
271 sqs = kvcalloc(qos_sqs_size, sizeof(struct mlx5e_txqsq *), GFP_KERNEL);
272 if (!sqs)
273 goto err_free;
274
275 WRITE_ONCE(chs->c[i]->qos_sqs_size, qos_sqs_size);
276 smp_wmb(); /* Pairs with mlx5e_napi_poll. */
277 rcu_assign_pointer(chs->c[i]->qos_sqs, sqs);
278 }
279
280 return 0;
281
282 err_free:
283 while (--i >= 0) {
284 struct mlx5e_txqsq **sqs;
285
286 sqs = rcu_replace_pointer(chs->c[i]->qos_sqs, NULL,
287 lockdep_is_held(&priv->state_lock));
288
289 synchronize_rcu(); /* Sync with NAPI. */
290 kvfree(sqs);
291 }
292 return -ENOMEM;
293 }
294
mlx5e_qos_open_queues(struct mlx5e_priv * priv,struct mlx5e_channels * chs)295 int mlx5e_qos_open_queues(struct mlx5e_priv *priv, struct mlx5e_channels *chs)
296 {
297 struct qos_sq_callback_params callback_params;
298 int err;
299
300 err = mlx5e_qos_alloc_queues(priv, chs);
301 if (err)
302 return err;
303
304 callback_params.priv = priv;
305 callback_params.chs = chs;
306
307 err = mlx5e_htb_enumerate_leaves(priv->htb, mlx5e_open_qos_sq_cb_wrapper, &callback_params);
308 if (err) {
309 mlx5e_qos_close_all_queues(chs);
310 return err;
311 }
312
313 return 0;
314 }
315
mlx5e_qos_activate_queues(struct mlx5e_priv * priv)316 void mlx5e_qos_activate_queues(struct mlx5e_priv *priv)
317 {
318 mlx5e_htb_enumerate_leaves(priv->htb, mlx5e_activate_qos_sq, priv);
319 }
320
mlx5e_qos_deactivate_queues(struct mlx5e_channel * c)321 void mlx5e_qos_deactivate_queues(struct mlx5e_channel *c)
322 {
323 struct mlx5e_params *params = &c->priv->channels.params;
324 struct mlx5e_txqsq __rcu **qos_sqs;
325 int i;
326
327 qos_sqs = mlx5e_state_dereference(c->priv, c->qos_sqs);
328 if (!qos_sqs)
329 return;
330
331 for (i = 0; i < c->qos_sqs_size; i++) {
332 u16 qid = params->num_channels * i + c->ix;
333 struct mlx5e_txqsq *sq;
334
335 sq = mlx5e_state_dereference(c->priv, qos_sqs[i]);
336 if (!sq) /* Handle the case when the SQ failed to open. */
337 continue;
338
339 qos_dbg(c->mdev, "Deactivate QoS SQ qid %u\n", qid);
340 mlx5e_deactivate_txqsq(sq);
341
342 /* The queue is disabled, no synchronization with datapath is needed. */
343 c->priv->txq2sq[mlx5e_qid_from_qos(&c->priv->channels, qid)] = NULL;
344 }
345 }
346
mlx5e_qos_deactivate_all_queues(struct mlx5e_channels * chs)347 void mlx5e_qos_deactivate_all_queues(struct mlx5e_channels *chs)
348 {
349 int i;
350
351 for (i = 0; i < chs->num; i++)
352 mlx5e_qos_deactivate_queues(chs->c[i]);
353 }
354
mlx5e_reactivate_qos_sq(struct mlx5e_priv * priv,u16 qid,struct netdev_queue * txq)355 void mlx5e_reactivate_qos_sq(struct mlx5e_priv *priv, u16 qid, struct netdev_queue *txq)
356 {
357 qos_dbg(priv->mdev, "Reactivate QoS SQ qid %u\n", qid);
358 netdev_tx_reset_queue(txq);
359 netif_tx_start_queue(txq);
360 }
361
mlx5e_reset_qdisc(struct net_device * dev,u16 qid)362 void mlx5e_reset_qdisc(struct net_device *dev, u16 qid)
363 {
364 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, qid);
365 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
366
367 if (!qdisc)
368 return;
369
370 spin_lock_bh(qdisc_lock(qdisc));
371 qdisc_reset(qdisc);
372 spin_unlock_bh(qdisc_lock(qdisc));
373 }
374
mlx5e_htb_setup_tc(struct mlx5e_priv * priv,struct tc_htb_qopt_offload * htb_qopt)375 int mlx5e_htb_setup_tc(struct mlx5e_priv *priv, struct tc_htb_qopt_offload *htb_qopt)
376 {
377 struct mlx5e_htb *htb = priv->htb;
378 int res;
379
380 if (!htb && htb_qopt->command != TC_HTB_CREATE)
381 return -EINVAL;
382
383 if (htb_qopt->prio || htb_qopt->quantum) {
384 NL_SET_ERR_MSG_MOD(htb_qopt->extack,
385 "prio and quantum parameters are not supported by device with HTB offload enabled.");
386 return -EOPNOTSUPP;
387 }
388
389 switch (htb_qopt->command) {
390 case TC_HTB_CREATE:
391 if (!mlx5_qos_is_supported(priv->mdev)) {
392 NL_SET_ERR_MSG_MOD(htb_qopt->extack,
393 "Missing QoS capabilities. Try disabling SRIOV or use a supported device.");
394 return -EOPNOTSUPP;
395 }
396 priv->htb = mlx5e_htb_alloc();
397 htb = priv->htb;
398 if (!htb)
399 return -ENOMEM;
400 res = mlx5e_htb_init(htb, htb_qopt, priv->netdev, priv->mdev, &priv->selq, priv);
401 if (res) {
402 mlx5e_htb_free(htb);
403 priv->htb = NULL;
404 }
405 return res;
406 case TC_HTB_DESTROY:
407 mlx5e_htb_cleanup(htb);
408 mlx5e_htb_free(htb);
409 priv->htb = NULL;
410 return 0;
411 case TC_HTB_LEAF_ALLOC_QUEUE:
412 res = mlx5e_htb_leaf_alloc_queue(htb, htb_qopt->classid, htb_qopt->parent_classid,
413 htb_qopt->rate, htb_qopt->ceil, htb_qopt->extack);
414 if (res < 0)
415 return res;
416 htb_qopt->qid = res;
417 return 0;
418 case TC_HTB_LEAF_TO_INNER:
419 return mlx5e_htb_leaf_to_inner(htb, htb_qopt->parent_classid, htb_qopt->classid,
420 htb_qopt->rate, htb_qopt->ceil, htb_qopt->extack);
421 case TC_HTB_LEAF_DEL:
422 return mlx5e_htb_leaf_del(htb, &htb_qopt->classid, htb_qopt->extack);
423 case TC_HTB_LEAF_DEL_LAST:
424 case TC_HTB_LEAF_DEL_LAST_FORCE:
425 return mlx5e_htb_leaf_del_last(htb, htb_qopt->classid,
426 htb_qopt->command == TC_HTB_LEAF_DEL_LAST_FORCE,
427 htb_qopt->extack);
428 case TC_HTB_NODE_MODIFY:
429 return mlx5e_htb_node_modify(htb, htb_qopt->classid, htb_qopt->rate, htb_qopt->ceil,
430 htb_qopt->extack);
431 case TC_HTB_LEAF_QUERY_QUEUE:
432 res = mlx5e_htb_get_txq_by_classid(htb, htb_qopt->classid);
433 if (res < 0)
434 return res;
435 htb_qopt->qid = res;
436 return 0;
437 default:
438 return -EOPNOTSUPP;
439 }
440 }
441
442 struct mlx5e_mqprio_rl {
443 struct mlx5_core_dev *mdev;
444 u32 root_id;
445 u32 *leaves_id;
446 u8 num_tc;
447 };
448
mlx5e_mqprio_rl_alloc(void)449 struct mlx5e_mqprio_rl *mlx5e_mqprio_rl_alloc(void)
450 {
451 return kvzalloc(sizeof(struct mlx5e_mqprio_rl), GFP_KERNEL);
452 }
453
mlx5e_mqprio_rl_free(struct mlx5e_mqprio_rl * rl)454 void mlx5e_mqprio_rl_free(struct mlx5e_mqprio_rl *rl)
455 {
456 kvfree(rl);
457 }
458
mlx5e_mqprio_rl_init(struct mlx5e_mqprio_rl * rl,struct mlx5_core_dev * mdev,u8 num_tc,u64 max_rate[])459 int mlx5e_mqprio_rl_init(struct mlx5e_mqprio_rl *rl, struct mlx5_core_dev *mdev, u8 num_tc,
460 u64 max_rate[])
461 {
462 int err;
463 int tc;
464
465 if (!mlx5_qos_is_supported(mdev)) {
466 qos_warn(mdev, "Missing QoS capabilities. Try disabling SRIOV or use a supported device.");
467 return -EOPNOTSUPP;
468 }
469 if (num_tc > mlx5e_qos_max_leaf_nodes(mdev))
470 return -EINVAL;
471
472 rl->mdev = mdev;
473 rl->num_tc = num_tc;
474 rl->leaves_id = kvcalloc(num_tc, sizeof(*rl->leaves_id), GFP_KERNEL);
475 if (!rl->leaves_id)
476 return -ENOMEM;
477
478 err = mlx5_qos_create_root_node(mdev, &rl->root_id);
479 if (err)
480 goto err_free_leaves;
481
482 qos_dbg(mdev, "Root created, id %#x\n", rl->root_id);
483
484 for (tc = 0; tc < num_tc; tc++) {
485 u32 max_average_bw;
486
487 max_average_bw = mlx5e_qos_bytes2mbits(mdev, max_rate[tc]);
488 err = mlx5_qos_create_leaf_node(mdev, rl->root_id, 0, max_average_bw,
489 &rl->leaves_id[tc]);
490 if (err)
491 goto err_destroy_leaves;
492
493 qos_dbg(mdev, "Leaf[%d] created, id %#x, max average bw %u Mbits/sec\n",
494 tc, rl->leaves_id[tc], max_average_bw);
495 }
496 return 0;
497
498 err_destroy_leaves:
499 while (--tc >= 0)
500 mlx5_qos_destroy_node(mdev, rl->leaves_id[tc]);
501 mlx5_qos_destroy_node(mdev, rl->root_id);
502 err_free_leaves:
503 kvfree(rl->leaves_id);
504 return err;
505 }
506
mlx5e_mqprio_rl_cleanup(struct mlx5e_mqprio_rl * rl)507 void mlx5e_mqprio_rl_cleanup(struct mlx5e_mqprio_rl *rl)
508 {
509 int tc;
510
511 for (tc = 0; tc < rl->num_tc; tc++)
512 mlx5_qos_destroy_node(rl->mdev, rl->leaves_id[tc]);
513 mlx5_qos_destroy_node(rl->mdev, rl->root_id);
514 kvfree(rl->leaves_id);
515 }
516
mlx5e_mqprio_rl_get_node_hw_id(struct mlx5e_mqprio_rl * rl,int tc,u32 * hw_id)517 int mlx5e_mqprio_rl_get_node_hw_id(struct mlx5e_mqprio_rl *rl, int tc, u32 *hw_id)
518 {
519 if (tc >= rl->num_tc)
520 return -EINVAL;
521
522 *hw_id = rl->leaves_id[tc];
523 return 0;
524 }
525