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>
9 #define BYTES_IN_MBIT 125000
11 int mlx5e_qos_bytes_rate_check(struct mlx5_core_dev *mdev, u64 nbytes)
13 if (nbytes < BYTES_IN_MBIT) {
14 qos_warn(mdev, "Input rate (%llu Bytes/sec) below minimum supported (%u Bytes/sec)\n",
15 nbytes, BYTES_IN_MBIT);
21 static u32 mlx5e_qos_bytes2mbits(struct mlx5_core_dev *mdev, u64 nbytes)
23 return div_u64(nbytes, BYTES_IN_MBIT);
26 int mlx5e_qos_max_leaf_nodes(struct mlx5_core_dev *mdev)
28 return min(MLX5E_QOS_MAX_LEAF_NODES, mlx5_qos_max_leaf_nodes(mdev));
31 int mlx5e_qos_cur_leaf_nodes(struct mlx5e_priv *priv)
33 int last = find_last_bit(priv->htb.qos_used_qids, mlx5e_qos_max_leaf_nodes(priv->mdev));
35 return last == mlx5e_qos_max_leaf_nodes(priv->mdev) ? 0 : last + 1;
38 /* Software representation of the QoS tree (internal to this file) */
40 static int mlx5e_find_unused_qos_qid(struct mlx5e_priv *priv)
42 int size = mlx5e_qos_max_leaf_nodes(priv->mdev);
45 WARN_ONCE(!mutex_is_locked(&priv->state_lock), "%s: state_lock is not held\n", __func__);
46 res = find_first_zero_bit(priv->htb.qos_used_qids, size);
48 return res == size ? -ENOSPC : res;
51 struct mlx5e_qos_node {
52 struct hlist_node hnode;
54 struct mlx5e_qos_node *parent;
59 u32 classid; /* 16-bit, except root. */
63 #define MLX5E_QOS_QID_INNER 0xffff
64 #define MLX5E_HTB_CLASSID_ROOT 0xffffffff
66 static struct mlx5e_qos_node *
67 mlx5e_sw_node_create_leaf(struct mlx5e_priv *priv, u16 classid, u16 qid,
68 struct mlx5e_qos_node *parent)
70 struct mlx5e_qos_node *node;
72 node = kzalloc(sizeof(*node), GFP_KERNEL);
74 return ERR_PTR(-ENOMEM);
76 node->parent = parent;
79 __set_bit(qid, priv->htb.qos_used_qids);
81 node->classid = classid;
82 hash_add_rcu(priv->htb.qos_tc2node, &node->hnode, classid);
84 mlx5e_update_tx_netdev_queues(priv);
89 static struct mlx5e_qos_node *mlx5e_sw_node_create_root(struct mlx5e_priv *priv)
91 struct mlx5e_qos_node *node;
93 node = kzalloc(sizeof(*node), GFP_KERNEL);
95 return ERR_PTR(-ENOMEM);
97 node->qid = MLX5E_QOS_QID_INNER;
98 node->classid = MLX5E_HTB_CLASSID_ROOT;
99 hash_add_rcu(priv->htb.qos_tc2node, &node->hnode, node->classid);
104 static struct mlx5e_qos_node *mlx5e_sw_node_find(struct mlx5e_priv *priv, u32 classid)
106 struct mlx5e_qos_node *node = NULL;
108 hash_for_each_possible(priv->htb.qos_tc2node, node, hnode, classid) {
109 if (node->classid == classid)
116 static struct mlx5e_qos_node *mlx5e_sw_node_find_rcu(struct mlx5e_priv *priv, u32 classid)
118 struct mlx5e_qos_node *node = NULL;
120 hash_for_each_possible_rcu(priv->htb.qos_tc2node, node, hnode, classid) {
121 if (node->classid == classid)
128 static void mlx5e_sw_node_delete(struct mlx5e_priv *priv, struct mlx5e_qos_node *node)
130 hash_del_rcu(&node->hnode);
131 if (node->qid != MLX5E_QOS_QID_INNER) {
132 __clear_bit(node->qid, priv->htb.qos_used_qids);
133 mlx5e_update_tx_netdev_queues(priv);
135 kfree_rcu(node, rcu);
138 /* TX datapath API */
140 static u16 mlx5e_qid_from_qos(struct mlx5e_channels *chs, u16 qid)
142 /* These channel params are safe to access from the datapath, because:
143 * 1. This function is called only after checking priv->htb.maj_id != 0,
144 * and the number of queues can't change while HTB offload is active.
145 * 2. When priv->htb.maj_id becomes 0, synchronize_rcu waits for
146 * mlx5e_select_queue to finish while holding priv->state_lock,
147 * preventing other code from changing the number of queues.
149 bool is_ptp = MLX5E_GET_PFLAG(&chs->params, MLX5E_PFLAG_TX_PORT_TS);
151 return (chs->params.num_channels + is_ptp) * mlx5e_get_dcb_num_tc(&chs->params) + qid;
154 int mlx5e_get_txq_by_classid(struct mlx5e_priv *priv, u16 classid)
156 struct mlx5e_qos_node *node;
162 node = mlx5e_sw_node_find_rcu(priv, classid);
167 qid = READ_ONCE(node->qid);
168 if (qid == MLX5E_QOS_QID_INNER) {
172 res = mlx5e_qid_from_qos(&priv->channels, qid);
179 static struct mlx5e_txqsq *mlx5e_get_qos_sq(struct mlx5e_priv *priv, int qid)
181 struct mlx5e_params *params = &priv->channels.params;
182 struct mlx5e_txqsq __rcu **qos_sqs;
183 struct mlx5e_channel *c;
186 ix = qid % params->num_channels;
187 qid /= params->num_channels;
188 c = priv->channels.c[ix];
190 qos_sqs = mlx5e_state_dereference(priv, c->qos_sqs);
191 return mlx5e_state_dereference(priv, qos_sqs[qid]);
196 static int mlx5e_open_qos_sq(struct mlx5e_priv *priv, struct mlx5e_channels *chs,
197 struct mlx5e_qos_node *node)
199 struct mlx5e_create_cq_param ccp = {};
200 struct mlx5e_txqsq __rcu **qos_sqs;
201 struct mlx5e_sq_param param_sq;
202 struct mlx5e_cq_param param_cq;
203 int txq_ix, ix, qid, err = 0;
204 struct mlx5e_params *params;
205 struct mlx5e_channel *c;
206 struct mlx5e_txqsq *sq;
208 params = &chs->params;
210 txq_ix = mlx5e_qid_from_qos(chs, node->qid);
212 WARN_ON(node->qid > priv->htb.max_qos_sqs);
213 if (node->qid == priv->htb.max_qos_sqs) {
214 struct mlx5e_sq_stats *stats, **stats_list = NULL;
216 if (priv->htb.max_qos_sqs == 0) {
217 stats_list = kvcalloc(mlx5e_qos_max_leaf_nodes(priv->mdev),
223 stats = kzalloc(sizeof(*stats), GFP_KERNEL);
229 WRITE_ONCE(priv->htb.qos_sq_stats, stats_list);
230 WRITE_ONCE(priv->htb.qos_sq_stats[node->qid], stats);
231 /* Order max_qos_sqs increment after writing the array pointer.
232 * Pairs with smp_load_acquire in en_stats.c.
234 smp_store_release(&priv->htb.max_qos_sqs, priv->htb.max_qos_sqs + 1);
237 ix = node->qid % params->num_channels;
238 qid = node->qid / params->num_channels;
241 qos_sqs = mlx5e_state_dereference(priv, c->qos_sqs);
242 sq = kzalloc(sizeof(*sq), GFP_KERNEL);
247 mlx5e_build_create_cq_param(&ccp, c);
249 memset(¶m_sq, 0, sizeof(param_sq));
250 memset(¶m_cq, 0, sizeof(param_cq));
251 mlx5e_build_sq_param(priv->mdev, params, ¶m_sq);
252 mlx5e_build_tx_cq_param(priv->mdev, params, ¶m_cq);
253 err = mlx5e_open_cq(priv, params->tx_cq_moderation, ¶m_cq, &ccp, &sq->cq);
256 err = mlx5e_open_txqsq(c, priv->tisn[c->lag_port][0], txq_ix, params,
257 ¶m_sq, sq, 0, node->hw_id,
258 priv->htb.qos_sq_stats[node->qid]);
262 rcu_assign_pointer(qos_sqs[qid], sq);
267 mlx5e_close_cq(&sq->cq);
273 static void mlx5e_activate_qos_sq(struct mlx5e_priv *priv, struct mlx5e_qos_node *node)
275 struct mlx5e_txqsq *sq;
277 sq = mlx5e_get_qos_sq(priv, node->qid);
279 WRITE_ONCE(priv->txq2sq[mlx5e_qid_from_qos(&priv->channels, node->qid)], sq);
281 /* Make the change to txq2sq visible before the queue is started.
282 * As mlx5e_xmit runs under a spinlock, there is an implicit ACQUIRE,
283 * which pairs with this barrier.
287 qos_dbg(priv->mdev, "Activate QoS SQ qid %u\n", node->qid);
288 mlx5e_activate_txqsq(sq);
291 static void mlx5e_deactivate_qos_sq(struct mlx5e_priv *priv, u16 qid)
293 struct mlx5e_txqsq *sq;
295 sq = mlx5e_get_qos_sq(priv, qid);
296 if (!sq) /* Handle the case when the SQ failed to open. */
299 qos_dbg(priv->mdev, "Deactivate QoS SQ qid %u\n", qid);
300 mlx5e_deactivate_txqsq(sq);
302 /* The queue is disabled, no synchronization with datapath is needed. */
303 priv->txq2sq[mlx5e_qid_from_qos(&priv->channels, qid)] = NULL;
306 static void mlx5e_close_qos_sq(struct mlx5e_priv *priv, u16 qid)
308 struct mlx5e_txqsq __rcu **qos_sqs;
309 struct mlx5e_params *params;
310 struct mlx5e_channel *c;
311 struct mlx5e_txqsq *sq;
314 params = &priv->channels.params;
316 ix = qid % params->num_channels;
317 qid /= params->num_channels;
318 c = priv->channels.c[ix];
319 qos_sqs = mlx5e_state_dereference(priv, c->qos_sqs);
320 sq = rcu_replace_pointer(qos_sqs[qid], NULL, lockdep_is_held(&priv->state_lock));
321 if (!sq) /* Handle the case when the SQ failed to open. */
324 synchronize_rcu(); /* Sync with NAPI. */
326 mlx5e_close_txqsq(sq);
327 mlx5e_close_cq(&sq->cq);
331 void mlx5e_qos_close_queues(struct mlx5e_channel *c)
333 struct mlx5e_txqsq __rcu **qos_sqs;
336 qos_sqs = rcu_replace_pointer(c->qos_sqs, NULL, lockdep_is_held(&c->priv->state_lock));
339 synchronize_rcu(); /* Sync with NAPI. */
341 for (i = 0; i < c->qos_sqs_size; i++) {
342 struct mlx5e_txqsq *sq;
344 sq = mlx5e_state_dereference(c->priv, qos_sqs[i]);
345 if (!sq) /* Handle the case when the SQ failed to open. */
348 mlx5e_close_txqsq(sq);
349 mlx5e_close_cq(&sq->cq);
356 static void mlx5e_qos_close_all_queues(struct mlx5e_channels *chs)
360 for (i = 0; i < chs->num; i++)
361 mlx5e_qos_close_queues(chs->c[i]);
364 static int mlx5e_qos_alloc_queues(struct mlx5e_priv *priv, struct mlx5e_channels *chs)
369 qos_sqs_size = DIV_ROUND_UP(mlx5e_qos_max_leaf_nodes(priv->mdev), chs->num);
371 for (i = 0; i < chs->num; i++) {
372 struct mlx5e_txqsq **sqs;
374 sqs = kvcalloc(qos_sqs_size, sizeof(struct mlx5e_txqsq *), GFP_KERNEL);
378 WRITE_ONCE(chs->c[i]->qos_sqs_size, qos_sqs_size);
379 smp_wmb(); /* Pairs with mlx5e_napi_poll. */
380 rcu_assign_pointer(chs->c[i]->qos_sqs, sqs);
387 struct mlx5e_txqsq **sqs;
389 sqs = rcu_replace_pointer(chs->c[i]->qos_sqs, NULL,
390 lockdep_is_held(&priv->state_lock));
392 synchronize_rcu(); /* Sync with NAPI. */
398 int mlx5e_qos_open_queues(struct mlx5e_priv *priv, struct mlx5e_channels *chs)
400 struct mlx5e_qos_node *node = NULL;
403 if (!priv->htb.maj_id)
406 err = mlx5e_qos_alloc_queues(priv, chs);
410 hash_for_each(priv->htb.qos_tc2node, bkt, node, hnode) {
411 if (node->qid == MLX5E_QOS_QID_INNER)
413 err = mlx5e_open_qos_sq(priv, chs, node);
415 mlx5e_qos_close_all_queues(chs);
423 void mlx5e_qos_activate_queues(struct mlx5e_priv *priv)
425 struct mlx5e_qos_node *node = NULL;
428 hash_for_each(priv->htb.qos_tc2node, bkt, node, hnode) {
429 if (node->qid == MLX5E_QOS_QID_INNER)
431 mlx5e_activate_qos_sq(priv, node);
435 void mlx5e_qos_deactivate_queues(struct mlx5e_channel *c)
437 struct mlx5e_params *params = &c->priv->channels.params;
438 struct mlx5e_txqsq __rcu **qos_sqs;
441 qos_sqs = mlx5e_state_dereference(c->priv, c->qos_sqs);
445 for (i = 0; i < c->qos_sqs_size; i++) {
446 u16 qid = params->num_channels * i + c->ix;
447 struct mlx5e_txqsq *sq;
449 sq = mlx5e_state_dereference(c->priv, qos_sqs[i]);
450 if (!sq) /* Handle the case when the SQ failed to open. */
453 qos_dbg(c->mdev, "Deactivate QoS SQ qid %u\n", qid);
454 mlx5e_deactivate_txqsq(sq);
456 /* The queue is disabled, no synchronization with datapath is needed. */
457 c->priv->txq2sq[mlx5e_qid_from_qos(&c->priv->channels, qid)] = NULL;
461 static void mlx5e_qos_deactivate_all_queues(struct mlx5e_channels *chs)
465 for (i = 0; i < chs->num; i++)
466 mlx5e_qos_deactivate_queues(chs->c[i]);
471 int mlx5e_htb_root_add(struct mlx5e_priv *priv, u16 htb_maj_id, u16 htb_defcls,
472 struct netlink_ext_ack *extack)
474 struct mlx5e_qos_node *root;
478 qos_dbg(priv->mdev, "TC_HTB_CREATE handle %04x:, default :%04x\n", htb_maj_id, htb_defcls);
480 if (!mlx5_qos_is_supported(priv->mdev)) {
481 NL_SET_ERR_MSG_MOD(extack,
482 "Missing QoS capabilities. Try disabling SRIOV or use a supported device.");
486 opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
488 err = mlx5e_qos_alloc_queues(priv, &priv->channels);
493 root = mlx5e_sw_node_create_root(priv);
496 goto err_free_queues;
499 err = mlx5_qos_create_root_node(priv->mdev, &root->hw_id);
501 NL_SET_ERR_MSG_MOD(extack, "Firmware error. Try upgrading firmware.");
502 goto err_sw_node_delete;
505 WRITE_ONCE(priv->htb.defcls, htb_defcls);
506 /* Order maj_id after defcls - pairs with
507 * mlx5e_select_queue/mlx5e_select_htb_queues.
509 smp_store_release(&priv->htb.maj_id, htb_maj_id);
514 mlx5e_sw_node_delete(priv, root);
518 mlx5e_qos_close_all_queues(&priv->channels);
522 int mlx5e_htb_root_del(struct mlx5e_priv *priv)
524 struct mlx5e_qos_node *root;
527 qos_dbg(priv->mdev, "TC_HTB_DESTROY\n");
529 WRITE_ONCE(priv->htb.maj_id, 0);
530 synchronize_rcu(); /* Sync with mlx5e_select_htb_queue and TX data path. */
532 root = mlx5e_sw_node_find(priv, MLX5E_HTB_CLASSID_ROOT);
534 qos_err(priv->mdev, "Failed to find the root node in the QoS tree\n");
537 err = mlx5_qos_destroy_node(priv->mdev, root->hw_id);
539 qos_err(priv->mdev, "Failed to destroy root node %u, err = %d\n",
541 mlx5e_sw_node_delete(priv, root);
543 mlx5e_qos_deactivate_all_queues(&priv->channels);
544 mlx5e_qos_close_all_queues(&priv->channels);
549 static int mlx5e_htb_convert_rate(struct mlx5e_priv *priv, u64 rate,
550 struct mlx5e_qos_node *parent, u32 *bw_share)
554 while (parent->classid != MLX5E_HTB_CLASSID_ROOT && !parent->max_average_bw)
555 parent = parent->parent;
557 if (parent->max_average_bw)
558 share = div64_u64(div_u64(rate * 100, BYTES_IN_MBIT),
559 parent->max_average_bw);
563 *bw_share = share == 0 ? 1 : share > 100 ? 0 : share;
565 qos_dbg(priv->mdev, "Convert: rate %llu, parent ceil %llu -> bw_share %u\n",
566 rate, (u64)parent->max_average_bw * BYTES_IN_MBIT, *bw_share);
571 static void mlx5e_htb_convert_ceil(struct mlx5e_priv *priv, u64 ceil, u32 *max_average_bw)
573 *max_average_bw = div_u64(ceil, BYTES_IN_MBIT);
575 qos_dbg(priv->mdev, "Convert: ceil %llu -> max_average_bw %u\n",
576 ceil, *max_average_bw);
579 int mlx5e_htb_leaf_alloc_queue(struct mlx5e_priv *priv, u16 classid,
580 u32 parent_classid, u64 rate, u64 ceil,
581 struct netlink_ext_ack *extack)
583 struct mlx5e_qos_node *node, *parent;
587 qos_dbg(priv->mdev, "TC_HTB_LEAF_ALLOC_QUEUE classid %04x, parent %04x, rate %llu, ceil %llu\n",
588 classid, parent_classid, rate, ceil);
590 qid = mlx5e_find_unused_qos_qid(priv);
592 NL_SET_ERR_MSG_MOD(extack, "Maximum amount of leaf classes is reached.");
596 parent = mlx5e_sw_node_find(priv, parent_classid);
600 node = mlx5e_sw_node_create_leaf(priv, classid, qid, parent);
602 return PTR_ERR(node);
605 mlx5e_htb_convert_rate(priv, rate, node->parent, &node->bw_share);
606 mlx5e_htb_convert_ceil(priv, ceil, &node->max_average_bw);
608 err = mlx5_qos_create_leaf_node(priv->mdev, node->parent->hw_id,
609 node->bw_share, node->max_average_bw,
612 NL_SET_ERR_MSG_MOD(extack, "Firmware error when creating a leaf node.");
613 qos_err(priv->mdev, "Failed to create a leaf node (class %04x), err = %d\n",
615 mlx5e_sw_node_delete(priv, node);
619 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
620 err = mlx5e_open_qos_sq(priv, &priv->channels, node);
622 NL_SET_ERR_MSG_MOD(extack, "Error creating an SQ.");
623 qos_warn(priv->mdev, "Failed to create a QoS SQ (class %04x), err = %d\n",
626 mlx5e_activate_qos_sq(priv, node);
630 return mlx5e_qid_from_qos(&priv->channels, node->qid);
633 int mlx5e_htb_leaf_to_inner(struct mlx5e_priv *priv, u16 classid, u16 child_classid,
634 u64 rate, u64 ceil, struct netlink_ext_ack *extack)
636 struct mlx5e_qos_node *node, *child;
641 qos_dbg(priv->mdev, "TC_HTB_LEAF_TO_INNER classid %04x, upcoming child %04x, rate %llu, ceil %llu\n",
642 classid, child_classid, rate, ceil);
644 node = mlx5e_sw_node_find(priv, classid);
648 err = mlx5_qos_create_inner_node(priv->mdev, node->parent->hw_id,
649 node->bw_share, node->max_average_bw,
652 NL_SET_ERR_MSG_MOD(extack, "Firmware error when creating an inner node.");
653 qos_err(priv->mdev, "Failed to create an inner node (class %04x), err = %d\n",
658 /* Intentionally reuse the qid for the upcoming first child. */
659 child = mlx5e_sw_node_create_leaf(priv, child_classid, node->qid, node);
661 err = PTR_ERR(child);
662 goto err_destroy_hw_node;
666 mlx5e_htb_convert_rate(priv, rate, node, &child->bw_share);
667 mlx5e_htb_convert_ceil(priv, ceil, &child->max_average_bw);
669 err = mlx5_qos_create_leaf_node(priv->mdev, new_hw_id, child->bw_share,
670 child->max_average_bw, &child->hw_id);
672 NL_SET_ERR_MSG_MOD(extack, "Firmware error when creating a leaf node.");
673 qos_err(priv->mdev, "Failed to create a leaf node (class %04x), err = %d\n",
675 goto err_delete_sw_node;
681 /* Pairs with mlx5e_get_txq_by_classid. */
682 WRITE_ONCE(node->qid, MLX5E_QOS_QID_INNER);
684 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
685 mlx5e_deactivate_qos_sq(priv, qid);
686 mlx5e_close_qos_sq(priv, qid);
689 err = mlx5_qos_destroy_node(priv->mdev, node->hw_id);
690 if (err) /* Not fatal. */
691 qos_warn(priv->mdev, "Failed to destroy leaf node %u (class %04x), err = %d\n",
692 node->hw_id, classid, err);
694 node->hw_id = new_hw_id;
696 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
697 err = mlx5e_open_qos_sq(priv, &priv->channels, child);
699 NL_SET_ERR_MSG_MOD(extack, "Error creating an SQ.");
700 qos_warn(priv->mdev, "Failed to create a QoS SQ (class %04x), err = %d\n",
703 mlx5e_activate_qos_sq(priv, child);
710 child->qid = MLX5E_QOS_QID_INNER;
711 mlx5e_sw_node_delete(priv, child);
714 tmp_err = mlx5_qos_destroy_node(priv->mdev, new_hw_id);
715 if (tmp_err) /* Not fatal. */
716 qos_warn(priv->mdev, "Failed to roll back creation of an inner node %u (class %04x), err = %d\n",
717 new_hw_id, classid, tmp_err);
721 static struct mlx5e_qos_node *mlx5e_sw_node_find_by_qid(struct mlx5e_priv *priv, u16 qid)
723 struct mlx5e_qos_node *node = NULL;
726 hash_for_each(priv->htb.qos_tc2node, bkt, node, hnode)
727 if (node->qid == qid)
733 static void mlx5e_reactivate_qos_sq(struct mlx5e_priv *priv, u16 qid, struct netdev_queue *txq)
735 qos_dbg(priv->mdev, "Reactivate QoS SQ qid %u\n", qid);
736 netdev_tx_reset_queue(txq);
737 netif_tx_start_queue(txq);
740 static void mlx5e_reset_qdisc(struct net_device *dev, u16 qid)
742 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, qid);
743 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
748 spin_lock_bh(qdisc_lock(qdisc));
750 spin_unlock_bh(qdisc_lock(qdisc));
753 int mlx5e_htb_leaf_del(struct mlx5e_priv *priv, u16 *classid,
754 struct netlink_ext_ack *extack)
756 struct mlx5e_qos_node *node;
757 struct netdev_queue *txq;
762 qos_dbg(priv->mdev, "TC_HTB_LEAF_DEL classid %04x\n", *classid);
764 node = mlx5e_sw_node_find(priv, *classid);
768 /* Store qid for reuse. */
771 opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
773 txq = netdev_get_tx_queue(priv->netdev,
774 mlx5e_qid_from_qos(&priv->channels, qid));
775 mlx5e_deactivate_qos_sq(priv, qid);
776 mlx5e_close_qos_sq(priv, qid);
779 err = mlx5_qos_destroy_node(priv->mdev, node->hw_id);
780 if (err) /* Not fatal. */
781 qos_warn(priv->mdev, "Failed to destroy leaf node %u (class %04x), err = %d\n",
782 node->hw_id, *classid, err);
784 mlx5e_sw_node_delete(priv, node);
786 moved_qid = mlx5e_qos_cur_leaf_nodes(priv);
788 if (moved_qid == 0) {
789 /* The last QoS SQ was just destroyed. */
791 mlx5e_reactivate_qos_sq(priv, qid, txq);
796 if (moved_qid < qid) {
797 /* The highest QoS SQ was just destroyed. */
798 WARN(moved_qid != qid - 1, "Gaps in queue numeration: destroyed queue %u, the highest queue is %u",
801 mlx5e_reactivate_qos_sq(priv, qid, txq);
805 WARN(moved_qid == qid, "Can't move node with qid %u to itself", qid);
806 qos_dbg(priv->mdev, "Moving QoS SQ %u to %u\n", moved_qid, qid);
808 node = mlx5e_sw_node_find_by_qid(priv, moved_qid);
809 WARN(!node, "Could not find a node with qid %u to move to queue %u",
812 /* Stop traffic to the old queue. */
813 WRITE_ONCE(node->qid, MLX5E_QOS_QID_INNER);
814 __clear_bit(moved_qid, priv->htb.qos_used_qids);
817 txq = netdev_get_tx_queue(priv->netdev,
818 mlx5e_qid_from_qos(&priv->channels, moved_qid));
819 mlx5e_deactivate_qos_sq(priv, moved_qid);
820 mlx5e_close_qos_sq(priv, moved_qid);
823 /* Prevent packets from the old class from getting into the new one. */
824 mlx5e_reset_qdisc(priv->netdev, moved_qid);
826 __set_bit(qid, priv->htb.qos_used_qids);
827 WRITE_ONCE(node->qid, qid);
829 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
830 err = mlx5e_open_qos_sq(priv, &priv->channels, node);
832 NL_SET_ERR_MSG_MOD(extack, "Error creating an SQ.");
833 qos_warn(priv->mdev, "Failed to create a QoS SQ (class %04x) while moving qid %u to %u, err = %d\n",
834 node->classid, moved_qid, qid, err);
836 mlx5e_activate_qos_sq(priv, node);
840 mlx5e_update_tx_netdev_queues(priv);
842 mlx5e_reactivate_qos_sq(priv, moved_qid, txq);
844 *classid = node->classid;
848 int mlx5e_htb_leaf_del_last(struct mlx5e_priv *priv, u16 classid, bool force,
849 struct netlink_ext_ack *extack)
851 struct mlx5e_qos_node *node, *parent;
852 u32 old_hw_id, new_hw_id;
853 int err, saved_err = 0;
856 qos_dbg(priv->mdev, "TC_HTB_LEAF_DEL_LAST%s classid %04x\n",
857 force ? "_FORCE" : "", classid);
859 node = mlx5e_sw_node_find(priv, classid);
863 err = mlx5_qos_create_leaf_node(priv->mdev, node->parent->parent->hw_id,
864 node->parent->bw_share,
865 node->parent->max_average_bw,
868 NL_SET_ERR_MSG_MOD(extack, "Firmware error when creating a leaf node.");
869 qos_err(priv->mdev, "Failed to create a leaf node (class %04x), err = %d\n",
876 /* Store qid for reuse and prevent clearing the bit. */
878 /* Pairs with mlx5e_get_txq_by_classid. */
879 WRITE_ONCE(node->qid, MLX5E_QOS_QID_INNER);
881 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
882 mlx5e_deactivate_qos_sq(priv, qid);
883 mlx5e_close_qos_sq(priv, qid);
886 /* Prevent packets from the old class from getting into the new one. */
887 mlx5e_reset_qdisc(priv->netdev, qid);
889 err = mlx5_qos_destroy_node(priv->mdev, node->hw_id);
890 if (err) /* Not fatal. */
891 qos_warn(priv->mdev, "Failed to destroy leaf node %u (class %04x), err = %d\n",
892 node->hw_id, classid, err);
894 parent = node->parent;
895 mlx5e_sw_node_delete(priv, node);
898 WRITE_ONCE(node->qid, qid);
900 /* Early return on error in force mode. Parent will still be an inner
901 * node to be deleted by a following delete operation.
906 old_hw_id = node->hw_id;
907 node->hw_id = new_hw_id;
909 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
910 err = mlx5e_open_qos_sq(priv, &priv->channels, node);
912 NL_SET_ERR_MSG_MOD(extack, "Error creating an SQ.");
913 qos_warn(priv->mdev, "Failed to create a QoS SQ (class %04x), err = %d\n",
916 mlx5e_activate_qos_sq(priv, node);
920 err = mlx5_qos_destroy_node(priv->mdev, old_hw_id);
921 if (err) /* Not fatal. */
922 qos_warn(priv->mdev, "Failed to destroy leaf node %u (class %04x), err = %d\n",
923 node->hw_id, classid, err);
928 static int mlx5e_qos_update_children(struct mlx5e_priv *priv, struct mlx5e_qos_node *node,
929 struct netlink_ext_ack *extack)
931 struct mlx5e_qos_node *child;
935 hash_for_each(priv->htb.qos_tc2node, bkt, child, hnode) {
936 u32 old_bw_share = child->bw_share;
939 if (child->parent != node)
942 mlx5e_htb_convert_rate(priv, child->rate, node, &child->bw_share);
943 if (child->bw_share == old_bw_share)
946 err_one = mlx5_qos_update_node(priv->mdev, child->hw_id, child->bw_share,
947 child->max_average_bw, child->hw_id);
948 if (!err && err_one) {
951 NL_SET_ERR_MSG_MOD(extack, "Firmware error when modifying a child node.");
952 qos_err(priv->mdev, "Failed to modify a child node (class %04x), err = %d\n",
960 int mlx5e_htb_node_modify(struct mlx5e_priv *priv, u16 classid, u64 rate, u64 ceil,
961 struct netlink_ext_ack *extack)
963 u32 bw_share, max_average_bw;
964 struct mlx5e_qos_node *node;
965 bool ceil_changed = false;
968 qos_dbg(priv->mdev, "TC_HTB_LEAF_MODIFY classid %04x, rate %llu, ceil %llu\n",
969 classid, rate, ceil);
971 node = mlx5e_sw_node_find(priv, classid);
976 mlx5e_htb_convert_rate(priv, rate, node->parent, &bw_share);
977 mlx5e_htb_convert_ceil(priv, ceil, &max_average_bw);
979 err = mlx5_qos_update_node(priv->mdev, node->parent->hw_id, bw_share,
980 max_average_bw, node->hw_id);
982 NL_SET_ERR_MSG_MOD(extack, "Firmware error when modifying a node.");
983 qos_err(priv->mdev, "Failed to modify a node (class %04x), err = %d\n",
988 if (max_average_bw != node->max_average_bw)
991 node->bw_share = bw_share;
992 node->max_average_bw = max_average_bw;
995 err = mlx5e_qos_update_children(priv, node, extack);
1000 struct mlx5e_mqprio_rl {
1001 struct mlx5_core_dev *mdev;
1007 struct mlx5e_mqprio_rl *mlx5e_mqprio_rl_alloc(void)
1009 return kvzalloc(sizeof(struct mlx5e_mqprio_rl), GFP_KERNEL);
1012 void mlx5e_mqprio_rl_free(struct mlx5e_mqprio_rl *rl)
1017 int mlx5e_mqprio_rl_init(struct mlx5e_mqprio_rl *rl, struct mlx5_core_dev *mdev, u8 num_tc,
1023 if (!mlx5_qos_is_supported(mdev)) {
1024 qos_warn(mdev, "Missing QoS capabilities. Try disabling SRIOV or use a supported device.");
1027 if (num_tc > mlx5e_qos_max_leaf_nodes(mdev))
1031 rl->num_tc = num_tc;
1032 rl->leaves_id = kvcalloc(num_tc, sizeof(*rl->leaves_id), GFP_KERNEL);
1036 err = mlx5_qos_create_root_node(mdev, &rl->root_id);
1038 goto err_free_leaves;
1040 qos_dbg(mdev, "Root created, id %#x\n", rl->root_id);
1042 for (tc = 0; tc < num_tc; tc++) {
1045 max_average_bw = mlx5e_qos_bytes2mbits(mdev, max_rate[tc]);
1046 err = mlx5_qos_create_leaf_node(mdev, rl->root_id, 0, max_average_bw,
1047 &rl->leaves_id[tc]);
1049 goto err_destroy_leaves;
1051 qos_dbg(mdev, "Leaf[%d] created, id %#x, max average bw %u Mbits/sec\n",
1052 tc, rl->leaves_id[tc], max_average_bw);
1058 mlx5_qos_destroy_node(mdev, rl->leaves_id[tc]);
1059 mlx5_qos_destroy_node(mdev, rl->root_id);
1061 kvfree(rl->leaves_id);
1065 void mlx5e_mqprio_rl_cleanup(struct mlx5e_mqprio_rl *rl)
1069 for (tc = 0; tc < rl->num_tc; tc++)
1070 mlx5_qos_destroy_node(rl->mdev, rl->leaves_id[tc]);
1071 mlx5_qos_destroy_node(rl->mdev, rl->root_id);
1072 kvfree(rl->leaves_id);
1075 int mlx5e_mqprio_rl_get_node_hw_id(struct mlx5e_mqprio_rl *rl, int tc, u32 *hw_id)
1077 if (tc >= rl->num_tc)
1080 *hw_id = rl->leaves_id[tc];