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 /* Hardware treats 0 as "unlimited", set at least 1. */
574 *max_average_bw = max_t(u32, div_u64(ceil, BYTES_IN_MBIT), 1);
576 qos_dbg(priv->mdev, "Convert: ceil %llu -> max_average_bw %u\n",
577 ceil, *max_average_bw);
580 int mlx5e_htb_leaf_alloc_queue(struct mlx5e_priv *priv, u16 classid,
581 u32 parent_classid, u64 rate, u64 ceil,
582 struct netlink_ext_ack *extack)
584 struct mlx5e_qos_node *node, *parent;
588 qos_dbg(priv->mdev, "TC_HTB_LEAF_ALLOC_QUEUE classid %04x, parent %04x, rate %llu, ceil %llu\n",
589 classid, parent_classid, rate, ceil);
591 qid = mlx5e_find_unused_qos_qid(priv);
593 NL_SET_ERR_MSG_MOD(extack, "Maximum amount of leaf classes is reached.");
597 parent = mlx5e_sw_node_find(priv, parent_classid);
601 node = mlx5e_sw_node_create_leaf(priv, classid, qid, parent);
603 return PTR_ERR(node);
606 mlx5e_htb_convert_rate(priv, rate, node->parent, &node->bw_share);
607 mlx5e_htb_convert_ceil(priv, ceil, &node->max_average_bw);
609 err = mlx5_qos_create_leaf_node(priv->mdev, node->parent->hw_id,
610 node->bw_share, node->max_average_bw,
613 NL_SET_ERR_MSG_MOD(extack, "Firmware error when creating a leaf node.");
614 qos_err(priv->mdev, "Failed to create a leaf node (class %04x), err = %d\n",
616 mlx5e_sw_node_delete(priv, node);
620 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
621 err = mlx5e_open_qos_sq(priv, &priv->channels, node);
623 NL_SET_ERR_MSG_MOD(extack, "Error creating an SQ.");
624 qos_warn(priv->mdev, "Failed to create a QoS SQ (class %04x), err = %d\n",
627 mlx5e_activate_qos_sq(priv, node);
631 return mlx5e_qid_from_qos(&priv->channels, node->qid);
634 int mlx5e_htb_leaf_to_inner(struct mlx5e_priv *priv, u16 classid, u16 child_classid,
635 u64 rate, u64 ceil, struct netlink_ext_ack *extack)
637 struct mlx5e_qos_node *node, *child;
642 qos_dbg(priv->mdev, "TC_HTB_LEAF_TO_INNER classid %04x, upcoming child %04x, rate %llu, ceil %llu\n",
643 classid, child_classid, rate, ceil);
645 node = mlx5e_sw_node_find(priv, classid);
649 err = mlx5_qos_create_inner_node(priv->mdev, node->parent->hw_id,
650 node->bw_share, node->max_average_bw,
653 NL_SET_ERR_MSG_MOD(extack, "Firmware error when creating an inner node.");
654 qos_err(priv->mdev, "Failed to create an inner node (class %04x), err = %d\n",
659 /* Intentionally reuse the qid for the upcoming first child. */
660 child = mlx5e_sw_node_create_leaf(priv, child_classid, node->qid, node);
662 err = PTR_ERR(child);
663 goto err_destroy_hw_node;
667 mlx5e_htb_convert_rate(priv, rate, node, &child->bw_share);
668 mlx5e_htb_convert_ceil(priv, ceil, &child->max_average_bw);
670 err = mlx5_qos_create_leaf_node(priv->mdev, new_hw_id, child->bw_share,
671 child->max_average_bw, &child->hw_id);
673 NL_SET_ERR_MSG_MOD(extack, "Firmware error when creating a leaf node.");
674 qos_err(priv->mdev, "Failed to create a leaf node (class %04x), err = %d\n",
676 goto err_delete_sw_node;
682 /* Pairs with mlx5e_get_txq_by_classid. */
683 WRITE_ONCE(node->qid, MLX5E_QOS_QID_INNER);
685 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
686 mlx5e_deactivate_qos_sq(priv, qid);
687 mlx5e_close_qos_sq(priv, qid);
690 err = mlx5_qos_destroy_node(priv->mdev, node->hw_id);
691 if (err) /* Not fatal. */
692 qos_warn(priv->mdev, "Failed to destroy leaf node %u (class %04x), err = %d\n",
693 node->hw_id, classid, err);
695 node->hw_id = new_hw_id;
697 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
698 err = mlx5e_open_qos_sq(priv, &priv->channels, child);
700 NL_SET_ERR_MSG_MOD(extack, "Error creating an SQ.");
701 qos_warn(priv->mdev, "Failed to create a QoS SQ (class %04x), err = %d\n",
704 mlx5e_activate_qos_sq(priv, child);
711 child->qid = MLX5E_QOS_QID_INNER;
712 mlx5e_sw_node_delete(priv, child);
715 tmp_err = mlx5_qos_destroy_node(priv->mdev, new_hw_id);
716 if (tmp_err) /* Not fatal. */
717 qos_warn(priv->mdev, "Failed to roll back creation of an inner node %u (class %04x), err = %d\n",
718 new_hw_id, classid, tmp_err);
722 static struct mlx5e_qos_node *mlx5e_sw_node_find_by_qid(struct mlx5e_priv *priv, u16 qid)
724 struct mlx5e_qos_node *node = NULL;
727 hash_for_each(priv->htb.qos_tc2node, bkt, node, hnode)
728 if (node->qid == qid)
734 static void mlx5e_reactivate_qos_sq(struct mlx5e_priv *priv, u16 qid, struct netdev_queue *txq)
736 qos_dbg(priv->mdev, "Reactivate QoS SQ qid %u\n", qid);
737 netdev_tx_reset_queue(txq);
738 netif_tx_start_queue(txq);
741 static void mlx5e_reset_qdisc(struct net_device *dev, u16 qid)
743 struct netdev_queue *dev_queue = netdev_get_tx_queue(dev, qid);
744 struct Qdisc *qdisc = dev_queue->qdisc_sleeping;
749 spin_lock_bh(qdisc_lock(qdisc));
751 spin_unlock_bh(qdisc_lock(qdisc));
754 int mlx5e_htb_leaf_del(struct mlx5e_priv *priv, u16 *classid,
755 struct netlink_ext_ack *extack)
757 struct mlx5e_qos_node *node;
758 struct netdev_queue *txq;
763 qos_dbg(priv->mdev, "TC_HTB_LEAF_DEL classid %04x\n", *classid);
765 node = mlx5e_sw_node_find(priv, *classid);
769 /* Store qid for reuse. */
772 opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
774 txq = netdev_get_tx_queue(priv->netdev,
775 mlx5e_qid_from_qos(&priv->channels, qid));
776 mlx5e_deactivate_qos_sq(priv, qid);
777 mlx5e_close_qos_sq(priv, qid);
780 err = mlx5_qos_destroy_node(priv->mdev, node->hw_id);
781 if (err) /* Not fatal. */
782 qos_warn(priv->mdev, "Failed to destroy leaf node %u (class %04x), err = %d\n",
783 node->hw_id, *classid, err);
785 mlx5e_sw_node_delete(priv, node);
787 moved_qid = mlx5e_qos_cur_leaf_nodes(priv);
789 if (moved_qid == 0) {
790 /* The last QoS SQ was just destroyed. */
792 mlx5e_reactivate_qos_sq(priv, qid, txq);
797 if (moved_qid < qid) {
798 /* The highest QoS SQ was just destroyed. */
799 WARN(moved_qid != qid - 1, "Gaps in queue numeration: destroyed queue %u, the highest queue is %u",
802 mlx5e_reactivate_qos_sq(priv, qid, txq);
806 WARN(moved_qid == qid, "Can't move node with qid %u to itself", qid);
807 qos_dbg(priv->mdev, "Moving QoS SQ %u to %u\n", moved_qid, qid);
809 node = mlx5e_sw_node_find_by_qid(priv, moved_qid);
810 WARN(!node, "Could not find a node with qid %u to move to queue %u",
813 /* Stop traffic to the old queue. */
814 WRITE_ONCE(node->qid, MLX5E_QOS_QID_INNER);
815 __clear_bit(moved_qid, priv->htb.qos_used_qids);
818 txq = netdev_get_tx_queue(priv->netdev,
819 mlx5e_qid_from_qos(&priv->channels, moved_qid));
820 mlx5e_deactivate_qos_sq(priv, moved_qid);
821 mlx5e_close_qos_sq(priv, moved_qid);
824 /* Prevent packets from the old class from getting into the new one. */
825 mlx5e_reset_qdisc(priv->netdev, moved_qid);
827 __set_bit(qid, priv->htb.qos_used_qids);
828 WRITE_ONCE(node->qid, qid);
830 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
831 err = mlx5e_open_qos_sq(priv, &priv->channels, node);
833 NL_SET_ERR_MSG_MOD(extack, "Error creating an SQ.");
834 qos_warn(priv->mdev, "Failed to create a QoS SQ (class %04x) while moving qid %u to %u, err = %d\n",
835 node->classid, moved_qid, qid, err);
837 mlx5e_activate_qos_sq(priv, node);
841 mlx5e_update_tx_netdev_queues(priv);
843 mlx5e_reactivate_qos_sq(priv, moved_qid, txq);
845 *classid = node->classid;
849 int mlx5e_htb_leaf_del_last(struct mlx5e_priv *priv, u16 classid, bool force,
850 struct netlink_ext_ack *extack)
852 struct mlx5e_qos_node *node, *parent;
853 u32 old_hw_id, new_hw_id;
854 int err, saved_err = 0;
857 qos_dbg(priv->mdev, "TC_HTB_LEAF_DEL_LAST%s classid %04x\n",
858 force ? "_FORCE" : "", classid);
860 node = mlx5e_sw_node_find(priv, classid);
864 err = mlx5_qos_create_leaf_node(priv->mdev, node->parent->parent->hw_id,
865 node->parent->bw_share,
866 node->parent->max_average_bw,
869 NL_SET_ERR_MSG_MOD(extack, "Firmware error when creating a leaf node.");
870 qos_err(priv->mdev, "Failed to create a leaf node (class %04x), err = %d\n",
877 /* Store qid for reuse and prevent clearing the bit. */
879 /* Pairs with mlx5e_get_txq_by_classid. */
880 WRITE_ONCE(node->qid, MLX5E_QOS_QID_INNER);
882 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
883 mlx5e_deactivate_qos_sq(priv, qid);
884 mlx5e_close_qos_sq(priv, qid);
887 /* Prevent packets from the old class from getting into the new one. */
888 mlx5e_reset_qdisc(priv->netdev, qid);
890 err = mlx5_qos_destroy_node(priv->mdev, node->hw_id);
891 if (err) /* Not fatal. */
892 qos_warn(priv->mdev, "Failed to destroy leaf node %u (class %04x), err = %d\n",
893 node->hw_id, classid, err);
895 parent = node->parent;
896 mlx5e_sw_node_delete(priv, node);
899 WRITE_ONCE(node->qid, qid);
901 /* Early return on error in force mode. Parent will still be an inner
902 * node to be deleted by a following delete operation.
907 old_hw_id = node->hw_id;
908 node->hw_id = new_hw_id;
910 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
911 err = mlx5e_open_qos_sq(priv, &priv->channels, node);
913 NL_SET_ERR_MSG_MOD(extack, "Error creating an SQ.");
914 qos_warn(priv->mdev, "Failed to create a QoS SQ (class %04x), err = %d\n",
917 mlx5e_activate_qos_sq(priv, node);
921 err = mlx5_qos_destroy_node(priv->mdev, old_hw_id);
922 if (err) /* Not fatal. */
923 qos_warn(priv->mdev, "Failed to destroy leaf node %u (class %04x), err = %d\n",
924 node->hw_id, classid, err);
929 static int mlx5e_qos_update_children(struct mlx5e_priv *priv, struct mlx5e_qos_node *node,
930 struct netlink_ext_ack *extack)
932 struct mlx5e_qos_node *child;
936 hash_for_each(priv->htb.qos_tc2node, bkt, child, hnode) {
937 u32 old_bw_share = child->bw_share;
940 if (child->parent != node)
943 mlx5e_htb_convert_rate(priv, child->rate, node, &child->bw_share);
944 if (child->bw_share == old_bw_share)
947 err_one = mlx5_qos_update_node(priv->mdev, child->hw_id, child->bw_share,
948 child->max_average_bw, child->hw_id);
949 if (!err && err_one) {
952 NL_SET_ERR_MSG_MOD(extack, "Firmware error when modifying a child node.");
953 qos_err(priv->mdev, "Failed to modify a child node (class %04x), err = %d\n",
961 int mlx5e_htb_node_modify(struct mlx5e_priv *priv, u16 classid, u64 rate, u64 ceil,
962 struct netlink_ext_ack *extack)
964 u32 bw_share, max_average_bw;
965 struct mlx5e_qos_node *node;
966 bool ceil_changed = false;
969 qos_dbg(priv->mdev, "TC_HTB_LEAF_MODIFY classid %04x, rate %llu, ceil %llu\n",
970 classid, rate, ceil);
972 node = mlx5e_sw_node_find(priv, classid);
977 mlx5e_htb_convert_rate(priv, rate, node->parent, &bw_share);
978 mlx5e_htb_convert_ceil(priv, ceil, &max_average_bw);
980 err = mlx5_qos_update_node(priv->mdev, node->parent->hw_id, bw_share,
981 max_average_bw, node->hw_id);
983 NL_SET_ERR_MSG_MOD(extack, "Firmware error when modifying a node.");
984 qos_err(priv->mdev, "Failed to modify a node (class %04x), err = %d\n",
989 if (max_average_bw != node->max_average_bw)
992 node->bw_share = bw_share;
993 node->max_average_bw = max_average_bw;
996 err = mlx5e_qos_update_children(priv, node, extack);
1001 struct mlx5e_mqprio_rl {
1002 struct mlx5_core_dev *mdev;
1008 struct mlx5e_mqprio_rl *mlx5e_mqprio_rl_alloc(void)
1010 return kvzalloc(sizeof(struct mlx5e_mqprio_rl), GFP_KERNEL);
1013 void mlx5e_mqprio_rl_free(struct mlx5e_mqprio_rl *rl)
1018 int mlx5e_mqprio_rl_init(struct mlx5e_mqprio_rl *rl, struct mlx5_core_dev *mdev, u8 num_tc,
1024 if (!mlx5_qos_is_supported(mdev)) {
1025 qos_warn(mdev, "Missing QoS capabilities. Try disabling SRIOV or use a supported device.");
1028 if (num_tc > mlx5e_qos_max_leaf_nodes(mdev))
1032 rl->num_tc = num_tc;
1033 rl->leaves_id = kvcalloc(num_tc, sizeof(*rl->leaves_id), GFP_KERNEL);
1037 err = mlx5_qos_create_root_node(mdev, &rl->root_id);
1039 goto err_free_leaves;
1041 qos_dbg(mdev, "Root created, id %#x\n", rl->root_id);
1043 for (tc = 0; tc < num_tc; tc++) {
1046 max_average_bw = mlx5e_qos_bytes2mbits(mdev, max_rate[tc]);
1047 err = mlx5_qos_create_leaf_node(mdev, rl->root_id, 0, max_average_bw,
1048 &rl->leaves_id[tc]);
1050 goto err_destroy_leaves;
1052 qos_dbg(mdev, "Leaf[%d] created, id %#x, max average bw %u Mbits/sec\n",
1053 tc, rl->leaves_id[tc], max_average_bw);
1059 mlx5_qos_destroy_node(mdev, rl->leaves_id[tc]);
1060 mlx5_qos_destroy_node(mdev, rl->root_id);
1062 kvfree(rl->leaves_id);
1066 void mlx5e_mqprio_rl_cleanup(struct mlx5e_mqprio_rl *rl)
1070 for (tc = 0; tc < rl->num_tc; tc++)
1071 mlx5_qos_destroy_node(rl->mdev, rl->leaves_id[tc]);
1072 mlx5_qos_destroy_node(rl->mdev, rl->root_id);
1073 kvfree(rl->leaves_id);
1076 int mlx5e_mqprio_rl_get_node_hw_id(struct mlx5e_mqprio_rl *rl, int tc, u32 *hw_id)
1078 if (tc >= rl->num_tc)
1081 *hw_id = rl->leaves_id[tc];