1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
7 #define CREATE_TRACE_POINTS
8 #include "diag/qos_tracepoint.h"
10 /* Minimum supported BW share value by the HW is 1 Mbit/sec */
11 #define MLX5_MIN_BW_SHARE 1
13 #define MLX5_RATE_TO_BW_SHARE(rate, divider, limit) \
14 min_t(u32, max_t(u32, DIV_ROUND_UP(rate, divider), MLX5_MIN_BW_SHARE), limit)
16 struct mlx5_esw_rate_group {
21 struct list_head list;
24 static int esw_qos_tsar_config(struct mlx5_core_dev *dev, u32 *sched_ctx,
25 u32 parent_ix, u32 tsar_ix,
26 u32 max_rate, u32 bw_share)
30 if (!MLX5_CAP_GEN(dev, qos) || !MLX5_CAP_QOS(dev, esw_scheduling))
33 MLX5_SET(scheduling_context, sched_ctx, parent_element_id, parent_ix);
34 MLX5_SET(scheduling_context, sched_ctx, max_average_bw, max_rate);
35 MLX5_SET(scheduling_context, sched_ctx, bw_share, bw_share);
36 bitmask |= MODIFY_SCHEDULING_ELEMENT_IN_MODIFY_BITMASK_MAX_AVERAGE_BW;
37 bitmask |= MODIFY_SCHEDULING_ELEMENT_IN_MODIFY_BITMASK_BW_SHARE;
39 return mlx5_modify_scheduling_element_cmd(dev,
40 SCHEDULING_HIERARCHY_E_SWITCH,
46 static int esw_qos_group_config(struct mlx5_eswitch *esw, struct mlx5_esw_rate_group *group,
47 u32 max_rate, u32 bw_share, struct netlink_ext_ack *extack)
49 u32 sched_ctx[MLX5_ST_SZ_DW(scheduling_context)] = {};
50 struct mlx5_core_dev *dev = esw->dev;
53 err = esw_qos_tsar_config(dev, sched_ctx,
54 esw->qos.root_tsar_ix, group->tsar_ix,
57 NL_SET_ERR_MSG_MOD(extack, "E-Switch modify group TSAR element failed");
59 trace_mlx5_esw_group_qos_config(dev, group, group->tsar_ix, bw_share, max_rate);
64 static int esw_qos_vport_config(struct mlx5_eswitch *esw,
65 struct mlx5_vport *vport,
66 u32 max_rate, u32 bw_share,
67 struct netlink_ext_ack *extack)
69 u32 sched_ctx[MLX5_ST_SZ_DW(scheduling_context)] = {};
70 struct mlx5_esw_rate_group *group = vport->qos.group;
71 struct mlx5_core_dev *dev = esw->dev;
76 if (!vport->qos.enabled)
79 parent_tsar_ix = group ? group->tsar_ix : esw->qos.root_tsar_ix;
80 MLX5_SET(scheduling_context, sched_ctx, element_type,
81 SCHEDULING_CONTEXT_ELEMENT_TYPE_VPORT);
82 vport_elem = MLX5_ADDR_OF(scheduling_context, sched_ctx,
84 MLX5_SET(vport_element, vport_elem, vport_number, vport->vport);
86 err = esw_qos_tsar_config(dev, sched_ctx, parent_tsar_ix, vport->qos.esw_tsar_ix,
90 "E-Switch modify TSAR vport element failed (vport=%d,err=%d)\n",
92 NL_SET_ERR_MSG_MOD(extack, "E-Switch modify TSAR vport element failed");
96 trace_mlx5_esw_vport_qos_config(vport, bw_share, max_rate);
101 static u32 esw_qos_calculate_min_rate_divider(struct mlx5_eswitch *esw,
102 struct mlx5_esw_rate_group *group,
105 u32 fw_max_bw_share = MLX5_CAP_QOS(esw->dev, max_tsar_bw_share);
106 struct mlx5_vport *evport;
107 u32 max_guarantee = 0;
111 struct mlx5_esw_rate_group *group;
113 list_for_each_entry(group, &esw->qos.groups, list) {
114 if (group->min_rate < max_guarantee)
116 max_guarantee = group->min_rate;
119 mlx5_esw_for_each_vport(esw, i, evport) {
120 if (!evport->enabled || !evport->qos.enabled ||
121 evport->qos.group != group || evport->qos.min_rate < max_guarantee)
123 max_guarantee = evport->qos.min_rate;
128 return max_t(u32, max_guarantee / fw_max_bw_share, 1);
130 /* If vports min rate divider is 0 but their group has bw_share configured, then
131 * need to set bw_share for vports to minimal value.
133 if (!group_level && !max_guarantee && group && group->bw_share)
138 static u32 esw_qos_calc_bw_share(u32 min_rate, u32 divider, u32 fw_max)
141 return MLX5_RATE_TO_BW_SHARE(min_rate, divider, fw_max);
146 static int esw_qos_normalize_vports_min_rate(struct mlx5_eswitch *esw,
147 struct mlx5_esw_rate_group *group,
148 struct netlink_ext_ack *extack)
150 u32 fw_max_bw_share = MLX5_CAP_QOS(esw->dev, max_tsar_bw_share);
151 u32 divider = esw_qos_calculate_min_rate_divider(esw, group, false);
152 struct mlx5_vport *evport;
157 mlx5_esw_for_each_vport(esw, i, evport) {
158 if (!evport->enabled || !evport->qos.enabled || evport->qos.group != group)
160 bw_share = esw_qos_calc_bw_share(evport->qos.min_rate, divider, fw_max_bw_share);
162 if (bw_share == evport->qos.bw_share)
165 err = esw_qos_vport_config(esw, evport, evport->qos.max_rate, bw_share, extack);
169 evport->qos.bw_share = bw_share;
175 static int esw_qos_normalize_groups_min_rate(struct mlx5_eswitch *esw, u32 divider,
176 struct netlink_ext_ack *extack)
178 u32 fw_max_bw_share = MLX5_CAP_QOS(esw->dev, max_tsar_bw_share);
179 struct mlx5_esw_rate_group *group;
183 list_for_each_entry(group, &esw->qos.groups, list) {
184 bw_share = esw_qos_calc_bw_share(group->min_rate, divider, fw_max_bw_share);
186 if (bw_share == group->bw_share)
189 err = esw_qos_group_config(esw, group, group->max_rate, bw_share, extack);
193 group->bw_share = bw_share;
195 /* All the group's vports need to be set with default bw_share
196 * to enable them with QOS
198 err = esw_qos_normalize_vports_min_rate(esw, group, extack);
207 static int esw_qos_set_vport_min_rate(struct mlx5_eswitch *esw, struct mlx5_vport *evport,
208 u32 min_rate, struct netlink_ext_ack *extack)
210 u32 fw_max_bw_share, previous_min_rate;
211 bool min_rate_supported;
214 lockdep_assert_held(&esw->state_lock);
215 fw_max_bw_share = MLX5_CAP_QOS(esw->dev, max_tsar_bw_share);
216 min_rate_supported = MLX5_CAP_QOS(esw->dev, esw_bw_share) &&
217 fw_max_bw_share >= MLX5_MIN_BW_SHARE;
218 if (min_rate && !min_rate_supported)
220 if (min_rate == evport->qos.min_rate)
223 previous_min_rate = evport->qos.min_rate;
224 evport->qos.min_rate = min_rate;
225 err = esw_qos_normalize_vports_min_rate(esw, evport->qos.group, extack);
227 evport->qos.min_rate = previous_min_rate;
232 static int esw_qos_set_vport_max_rate(struct mlx5_eswitch *esw, struct mlx5_vport *evport,
233 u32 max_rate, struct netlink_ext_ack *extack)
235 u32 act_max_rate = max_rate;
236 bool max_rate_supported;
239 lockdep_assert_held(&esw->state_lock);
240 max_rate_supported = MLX5_CAP_QOS(esw->dev, esw_rate_limit);
242 if (max_rate && !max_rate_supported)
244 if (max_rate == evport->qos.max_rate)
247 /* If parent group has rate limit need to set to group
248 * value when new max rate is 0.
250 if (evport->qos.group && !max_rate)
251 act_max_rate = evport->qos.group->max_rate;
253 err = esw_qos_vport_config(esw, evport, act_max_rate, evport->qos.bw_share, extack);
256 evport->qos.max_rate = max_rate;
261 static int esw_qos_set_group_min_rate(struct mlx5_eswitch *esw, struct mlx5_esw_rate_group *group,
262 u32 min_rate, struct netlink_ext_ack *extack)
264 u32 fw_max_bw_share = MLX5_CAP_QOS(esw->dev, max_tsar_bw_share);
265 struct mlx5_core_dev *dev = esw->dev;
266 u32 previous_min_rate, divider;
269 if (!(MLX5_CAP_QOS(dev, esw_bw_share) && fw_max_bw_share >= MLX5_MIN_BW_SHARE))
272 if (min_rate == group->min_rate)
275 previous_min_rate = group->min_rate;
276 group->min_rate = min_rate;
277 divider = esw_qos_calculate_min_rate_divider(esw, group, true);
278 err = esw_qos_normalize_groups_min_rate(esw, divider, extack);
280 group->min_rate = previous_min_rate;
281 NL_SET_ERR_MSG_MOD(extack, "E-Switch group min rate setting failed");
283 /* Attempt restoring previous configuration */
284 divider = esw_qos_calculate_min_rate_divider(esw, group, true);
285 if (esw_qos_normalize_groups_min_rate(esw, divider, extack))
286 NL_SET_ERR_MSG_MOD(extack, "E-Switch BW share restore failed");
292 static int esw_qos_set_group_max_rate(struct mlx5_eswitch *esw,
293 struct mlx5_esw_rate_group *group,
294 u32 max_rate, struct netlink_ext_ack *extack)
296 struct mlx5_vport *vport;
300 if (group->max_rate == max_rate)
303 err = esw_qos_group_config(esw, group, max_rate, group->bw_share, extack);
307 group->max_rate = max_rate;
309 /* Any unlimited vports in the group should be set
310 * with the value of the group.
312 mlx5_esw_for_each_vport(esw, i, vport) {
313 if (!vport->enabled || !vport->qos.enabled ||
314 vport->qos.group != group || vport->qos.max_rate)
317 err = esw_qos_vport_config(esw, vport, max_rate, vport->qos.bw_share, extack);
319 NL_SET_ERR_MSG_MOD(extack,
320 "E-Switch vport implicit rate limit setting failed");
326 static int esw_qos_vport_create_sched_element(struct mlx5_eswitch *esw,
327 struct mlx5_vport *vport,
328 u32 max_rate, u32 bw_share)
330 u32 sched_ctx[MLX5_ST_SZ_DW(scheduling_context)] = {};
331 struct mlx5_esw_rate_group *group = vport->qos.group;
332 struct mlx5_core_dev *dev = esw->dev;
337 parent_tsar_ix = group ? group->tsar_ix : esw->qos.root_tsar_ix;
338 MLX5_SET(scheduling_context, sched_ctx, element_type,
339 SCHEDULING_CONTEXT_ELEMENT_TYPE_VPORT);
340 vport_elem = MLX5_ADDR_OF(scheduling_context, sched_ctx, element_attributes);
341 MLX5_SET(vport_element, vport_elem, vport_number, vport->vport);
342 MLX5_SET(scheduling_context, sched_ctx, parent_element_id, parent_tsar_ix);
343 MLX5_SET(scheduling_context, sched_ctx, max_average_bw, max_rate);
344 MLX5_SET(scheduling_context, sched_ctx, bw_share, bw_share);
346 err = mlx5_create_scheduling_element_cmd(dev,
347 SCHEDULING_HIERARCHY_E_SWITCH,
349 &vport->qos.esw_tsar_ix);
351 esw_warn(esw->dev, "E-Switch create TSAR vport element failed (vport=%d,err=%d)\n",
359 static int esw_qos_update_group_scheduling_element(struct mlx5_eswitch *esw,
360 struct mlx5_vport *vport,
361 struct mlx5_esw_rate_group *curr_group,
362 struct mlx5_esw_rate_group *new_group,
363 struct netlink_ext_ack *extack)
368 err = mlx5_destroy_scheduling_element_cmd(esw->dev,
369 SCHEDULING_HIERARCHY_E_SWITCH,
370 vport->qos.esw_tsar_ix);
372 NL_SET_ERR_MSG_MOD(extack, "E-Switch destroy TSAR vport element failed");
376 vport->qos.group = new_group;
377 max_rate = vport->qos.max_rate ? vport->qos.max_rate : new_group->max_rate;
379 /* If vport is unlimited, we set the group's value.
380 * Therefore, if the group is limited it will apply to
381 * the vport as well and if not, vport will remain unlimited.
383 err = esw_qos_vport_create_sched_element(esw, vport, max_rate, vport->qos.bw_share);
385 NL_SET_ERR_MSG_MOD(extack, "E-Switch vport group set failed.");
392 vport->qos.group = curr_group;
393 max_rate = vport->qos.max_rate ? vport->qos.max_rate : curr_group->max_rate;
394 if (esw_qos_vport_create_sched_element(esw, vport, max_rate, vport->qos.bw_share))
395 esw_warn(esw->dev, "E-Switch vport group restore failed (vport=%d)\n",
401 static int esw_qos_vport_update_group(struct mlx5_eswitch *esw,
402 struct mlx5_vport *vport,
403 struct mlx5_esw_rate_group *group,
404 struct netlink_ext_ack *extack)
406 struct mlx5_esw_rate_group *new_group, *curr_group;
412 curr_group = vport->qos.group;
413 new_group = group ?: esw->qos.group0;
414 if (curr_group == new_group)
417 err = esw_qos_update_group_scheduling_element(esw, vport, curr_group, new_group, extack);
421 /* Recalculate bw share weights of old and new groups */
422 if (vport->qos.bw_share || new_group->bw_share) {
423 esw_qos_normalize_vports_min_rate(esw, curr_group, extack);
424 esw_qos_normalize_vports_min_rate(esw, new_group, extack);
430 static struct mlx5_esw_rate_group *
431 __esw_qos_create_rate_group(struct mlx5_eswitch *esw, struct netlink_ext_ack *extack)
433 u32 tsar_ctx[MLX5_ST_SZ_DW(scheduling_context)] = {};
434 struct mlx5_esw_rate_group *group;
438 group = kzalloc(sizeof(*group), GFP_KERNEL);
440 return ERR_PTR(-ENOMEM);
442 MLX5_SET(scheduling_context, tsar_ctx, parent_element_id,
443 esw->qos.root_tsar_ix);
444 err = mlx5_create_scheduling_element_cmd(esw->dev,
445 SCHEDULING_HIERARCHY_E_SWITCH,
449 NL_SET_ERR_MSG_MOD(extack, "E-Switch create TSAR for group failed");
453 list_add_tail(&group->list, &esw->qos.groups);
455 divider = esw_qos_calculate_min_rate_divider(esw, group, true);
457 err = esw_qos_normalize_groups_min_rate(esw, divider, extack);
459 NL_SET_ERR_MSG_MOD(extack, "E-Switch groups normalization failed");
463 trace_mlx5_esw_group_qos_create(esw->dev, group, group->tsar_ix);
468 list_del(&group->list);
469 if (mlx5_destroy_scheduling_element_cmd(esw->dev,
470 SCHEDULING_HIERARCHY_E_SWITCH,
472 NL_SET_ERR_MSG_MOD(extack, "E-Switch destroy TSAR for group failed");
478 static int esw_qos_get(struct mlx5_eswitch *esw, struct netlink_ext_ack *extack);
479 static void esw_qos_put(struct mlx5_eswitch *esw);
481 static struct mlx5_esw_rate_group *
482 esw_qos_create_rate_group(struct mlx5_eswitch *esw, struct netlink_ext_ack *extack)
484 struct mlx5_esw_rate_group *group;
487 if (!MLX5_CAP_QOS(esw->dev, log_esw_max_sched_depth))
488 return ERR_PTR(-EOPNOTSUPP);
490 err = esw_qos_get(esw, extack);
494 group = __esw_qos_create_rate_group(esw, extack);
501 static int __esw_qos_destroy_rate_group(struct mlx5_eswitch *esw,
502 struct mlx5_esw_rate_group *group,
503 struct netlink_ext_ack *extack)
508 list_del(&group->list);
510 divider = esw_qos_calculate_min_rate_divider(esw, NULL, true);
511 err = esw_qos_normalize_groups_min_rate(esw, divider, extack);
513 NL_SET_ERR_MSG_MOD(extack, "E-Switch groups' normalization failed");
515 err = mlx5_destroy_scheduling_element_cmd(esw->dev,
516 SCHEDULING_HIERARCHY_E_SWITCH,
519 NL_SET_ERR_MSG_MOD(extack, "E-Switch destroy TSAR_ID failed");
521 trace_mlx5_esw_group_qos_destroy(esw->dev, group, group->tsar_ix);
528 static int esw_qos_destroy_rate_group(struct mlx5_eswitch *esw,
529 struct mlx5_esw_rate_group *group,
530 struct netlink_ext_ack *extack)
534 err = __esw_qos_destroy_rate_group(esw, group, extack);
540 static bool esw_qos_element_type_supported(struct mlx5_core_dev *dev, int type)
543 case SCHEDULING_CONTEXT_ELEMENT_TYPE_TSAR:
544 return MLX5_CAP_QOS(dev, esw_element_type) &
545 ELEMENT_TYPE_CAP_MASK_TASR;
546 case SCHEDULING_CONTEXT_ELEMENT_TYPE_VPORT:
547 return MLX5_CAP_QOS(dev, esw_element_type) &
548 ELEMENT_TYPE_CAP_MASK_VPORT;
549 case SCHEDULING_CONTEXT_ELEMENT_TYPE_VPORT_TC:
550 return MLX5_CAP_QOS(dev, esw_element_type) &
551 ELEMENT_TYPE_CAP_MASK_VPORT_TC;
552 case SCHEDULING_CONTEXT_ELEMENT_TYPE_PARA_VPORT_TC:
553 return MLX5_CAP_QOS(dev, esw_element_type) &
554 ELEMENT_TYPE_CAP_MASK_PARA_VPORT_TC;
559 static int esw_qos_create(struct mlx5_eswitch *esw, struct netlink_ext_ack *extack)
561 u32 tsar_ctx[MLX5_ST_SZ_DW(scheduling_context)] = {};
562 struct mlx5_core_dev *dev = esw->dev;
566 if (!MLX5_CAP_GEN(dev, qos) || !MLX5_CAP_QOS(dev, esw_scheduling))
569 if (!esw_qos_element_type_supported(dev, SCHEDULING_CONTEXT_ELEMENT_TYPE_TSAR))
572 MLX5_SET(scheduling_context, tsar_ctx, element_type,
573 SCHEDULING_CONTEXT_ELEMENT_TYPE_TSAR);
575 attr = MLX5_ADDR_OF(scheduling_context, tsar_ctx, element_attributes);
576 *attr = cpu_to_be32(TSAR_ELEMENT_TSAR_TYPE_DWRR << 16);
578 err = mlx5_create_scheduling_element_cmd(dev,
579 SCHEDULING_HIERARCHY_E_SWITCH,
581 &esw->qos.root_tsar_ix);
583 esw_warn(dev, "E-Switch create root TSAR failed (%d)\n", err);
587 INIT_LIST_HEAD(&esw->qos.groups);
588 if (MLX5_CAP_QOS(dev, log_esw_max_sched_depth)) {
589 esw->qos.group0 = __esw_qos_create_rate_group(esw, extack);
590 if (IS_ERR(esw->qos.group0)) {
591 esw_warn(dev, "E-Switch create rate group 0 failed (%ld)\n",
592 PTR_ERR(esw->qos.group0));
593 err = PTR_ERR(esw->qos.group0);
597 refcount_set(&esw->qos.refcnt, 1);
602 if (mlx5_destroy_scheduling_element_cmd(esw->dev, SCHEDULING_HIERARCHY_E_SWITCH,
603 esw->qos.root_tsar_ix))
604 esw_warn(esw->dev, "E-Switch destroy root TSAR failed.\n");
609 static void esw_qos_destroy(struct mlx5_eswitch *esw)
614 __esw_qos_destroy_rate_group(esw, esw->qos.group0, NULL);
616 err = mlx5_destroy_scheduling_element_cmd(esw->dev,
617 SCHEDULING_HIERARCHY_E_SWITCH,
618 esw->qos.root_tsar_ix);
620 esw_warn(esw->dev, "E-Switch destroy root TSAR failed (%d)\n", err);
623 static int esw_qos_get(struct mlx5_eswitch *esw, struct netlink_ext_ack *extack)
627 lockdep_assert_held(&esw->state_lock);
629 if (!refcount_inc_not_zero(&esw->qos.refcnt)) {
630 /* esw_qos_create() set refcount to 1 only on success.
631 * No need to decrement on failure.
633 err = esw_qos_create(esw, extack);
639 static void esw_qos_put(struct mlx5_eswitch *esw)
641 lockdep_assert_held(&esw->state_lock);
642 if (refcount_dec_and_test(&esw->qos.refcnt))
643 esw_qos_destroy(esw);
646 static int esw_qos_vport_enable(struct mlx5_eswitch *esw, struct mlx5_vport *vport,
647 u32 max_rate, u32 bw_share, struct netlink_ext_ack *extack)
651 lockdep_assert_held(&esw->state_lock);
652 if (vport->qos.enabled)
655 err = esw_qos_get(esw, extack);
659 vport->qos.group = esw->qos.group0;
661 err = esw_qos_vport_create_sched_element(esw, vport, max_rate, bw_share);
665 vport->qos.enabled = true;
666 trace_mlx5_esw_vport_qos_create(vport, bw_share, max_rate);
676 void mlx5_esw_qos_vport_disable(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
680 lockdep_assert_held(&esw->state_lock);
681 if (!vport->qos.enabled)
683 WARN(vport->qos.group && vport->qos.group != esw->qos.group0,
684 "Disabling QoS on port before detaching it from group");
686 err = mlx5_destroy_scheduling_element_cmd(esw->dev,
687 SCHEDULING_HIERARCHY_E_SWITCH,
688 vport->qos.esw_tsar_ix);
690 esw_warn(esw->dev, "E-Switch destroy TSAR vport element failed (vport=%d,err=%d)\n",
693 memset(&vport->qos, 0, sizeof(vport->qos));
694 trace_mlx5_esw_vport_qos_destroy(vport);
699 int mlx5_esw_qos_set_vport_rate(struct mlx5_eswitch *esw, struct mlx5_vport *vport,
700 u32 min_rate, u32 max_rate)
704 lockdep_assert_held(&esw->state_lock);
705 err = esw_qos_vport_enable(esw, vport, 0, 0, NULL);
709 err = esw_qos_set_vport_min_rate(esw, vport, min_rate, NULL);
711 err = esw_qos_set_vport_max_rate(esw, vport, max_rate, NULL);
716 int mlx5_esw_qos_modify_vport_rate(struct mlx5_eswitch *esw, u16 vport_num, u32 rate_mbps)
718 u32 ctx[MLX5_ST_SZ_DW(scheduling_context)] = {};
719 struct mlx5_vport *vport;
723 vport = mlx5_eswitch_get_vport(esw, vport_num);
725 return PTR_ERR(vport);
727 mutex_lock(&esw->state_lock);
728 if (!vport->qos.enabled) {
729 /* Eswitch QoS wasn't enabled yet. Enable it and vport QoS. */
730 err = esw_qos_vport_enable(esw, vport, rate_mbps, vport->qos.bw_share, NULL);
732 MLX5_SET(scheduling_context, ctx, max_average_bw, rate_mbps);
734 bitmask = MODIFY_SCHEDULING_ELEMENT_IN_MODIFY_BITMASK_MAX_AVERAGE_BW;
735 err = mlx5_modify_scheduling_element_cmd(esw->dev,
736 SCHEDULING_HIERARCHY_E_SWITCH,
738 vport->qos.esw_tsar_ix,
741 mutex_unlock(&esw->state_lock);
746 #define MLX5_LINKSPEED_UNIT 125000 /* 1Mbps in Bps */
748 /* Converts bytes per second value passed in a pointer into megabits per
749 * second, rewriting last. If converted rate exceed link speed or is not a
750 * fraction of Mbps - returns error.
752 static int esw_qos_devlink_rate_to_mbps(struct mlx5_core_dev *mdev, const char *name,
753 u64 *rate, struct netlink_ext_ack *extack)
755 u32 link_speed_max, reminder;
759 err = mlx5e_port_max_linkspeed(mdev, &link_speed_max);
761 NL_SET_ERR_MSG_MOD(extack, "Failed to get link maximum speed");
765 value = div_u64_rem(*rate, MLX5_LINKSPEED_UNIT, &reminder);
767 pr_err("%s rate value %lluBps not in link speed units of 1Mbps.\n",
769 NL_SET_ERR_MSG_MOD(extack, "TX rate value not in link speed units of 1Mbps");
773 if (value > link_speed_max) {
774 pr_err("%s rate value %lluMbps exceed link maximum speed %u.\n",
775 name, value, link_speed_max);
776 NL_SET_ERR_MSG_MOD(extack, "TX rate value exceed link maximum speed");
784 /* Eswitch devlink rate API */
786 int mlx5_esw_devlink_rate_leaf_tx_share_set(struct devlink_rate *rate_leaf, void *priv,
787 u64 tx_share, struct netlink_ext_ack *extack)
789 struct mlx5_vport *vport = priv;
790 struct mlx5_eswitch *esw;
793 esw = vport->dev->priv.eswitch;
794 if (!mlx5_esw_allowed(esw))
797 err = esw_qos_devlink_rate_to_mbps(vport->dev, "tx_share", &tx_share, extack);
801 mutex_lock(&esw->state_lock);
802 err = esw_qos_vport_enable(esw, vport, 0, 0, extack);
806 err = esw_qos_set_vport_min_rate(esw, vport, tx_share, extack);
808 mutex_unlock(&esw->state_lock);
812 int mlx5_esw_devlink_rate_leaf_tx_max_set(struct devlink_rate *rate_leaf, void *priv,
813 u64 tx_max, struct netlink_ext_ack *extack)
815 struct mlx5_vport *vport = priv;
816 struct mlx5_eswitch *esw;
819 esw = vport->dev->priv.eswitch;
820 if (!mlx5_esw_allowed(esw))
823 err = esw_qos_devlink_rate_to_mbps(vport->dev, "tx_max", &tx_max, extack);
827 mutex_lock(&esw->state_lock);
828 err = esw_qos_vport_enable(esw, vport, 0, 0, extack);
832 err = esw_qos_set_vport_max_rate(esw, vport, tx_max, extack);
834 mutex_unlock(&esw->state_lock);
838 int mlx5_esw_devlink_rate_node_tx_share_set(struct devlink_rate *rate_node, void *priv,
839 u64 tx_share, struct netlink_ext_ack *extack)
841 struct mlx5_core_dev *dev = devlink_priv(rate_node->devlink);
842 struct mlx5_eswitch *esw = dev->priv.eswitch;
843 struct mlx5_esw_rate_group *group = priv;
846 err = esw_qos_devlink_rate_to_mbps(dev, "tx_share", &tx_share, extack);
850 mutex_lock(&esw->state_lock);
851 err = esw_qos_set_group_min_rate(esw, group, tx_share, extack);
852 mutex_unlock(&esw->state_lock);
856 int mlx5_esw_devlink_rate_node_tx_max_set(struct devlink_rate *rate_node, void *priv,
857 u64 tx_max, struct netlink_ext_ack *extack)
859 struct mlx5_core_dev *dev = devlink_priv(rate_node->devlink);
860 struct mlx5_eswitch *esw = dev->priv.eswitch;
861 struct mlx5_esw_rate_group *group = priv;
864 err = esw_qos_devlink_rate_to_mbps(dev, "tx_max", &tx_max, extack);
868 mutex_lock(&esw->state_lock);
869 err = esw_qos_set_group_max_rate(esw, group, tx_max, extack);
870 mutex_unlock(&esw->state_lock);
874 int mlx5_esw_devlink_rate_node_new(struct devlink_rate *rate_node, void **priv,
875 struct netlink_ext_ack *extack)
877 struct mlx5_esw_rate_group *group;
878 struct mlx5_eswitch *esw;
881 esw = mlx5_devlink_eswitch_get(rate_node->devlink);
885 mutex_lock(&esw->state_lock);
886 if (esw->mode != MLX5_ESWITCH_OFFLOADS) {
887 NL_SET_ERR_MSG_MOD(extack,
888 "Rate node creation supported only in switchdev mode");
893 group = esw_qos_create_rate_group(esw, extack);
895 err = PTR_ERR(group);
901 mutex_unlock(&esw->state_lock);
905 int mlx5_esw_devlink_rate_node_del(struct devlink_rate *rate_node, void *priv,
906 struct netlink_ext_ack *extack)
908 struct mlx5_esw_rate_group *group = priv;
909 struct mlx5_eswitch *esw;
912 esw = mlx5_devlink_eswitch_get(rate_node->devlink);
916 mutex_lock(&esw->state_lock);
917 err = esw_qos_destroy_rate_group(esw, group, extack);
918 mutex_unlock(&esw->state_lock);
922 int mlx5_esw_qos_vport_update_group(struct mlx5_eswitch *esw,
923 struct mlx5_vport *vport,
924 struct mlx5_esw_rate_group *group,
925 struct netlink_ext_ack *extack)
929 mutex_lock(&esw->state_lock);
930 err = esw_qos_vport_enable(esw, vport, 0, 0, extack);
932 err = esw_qos_vport_update_group(esw, vport, group, extack);
933 mutex_unlock(&esw->state_lock);
937 int mlx5_esw_devlink_rate_parent_set(struct devlink_rate *devlink_rate,
938 struct devlink_rate *parent,
939 void *priv, void *parent_priv,
940 struct netlink_ext_ack *extack)
942 struct mlx5_esw_rate_group *group;
943 struct mlx5_vport *vport = priv;
946 return mlx5_esw_qos_vport_update_group(vport->dev->priv.eswitch,
947 vport, NULL, extack);
950 return mlx5_esw_qos_vport_update_group(vport->dev->priv.eswitch, vport, group, extack);