1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
5 #include <linux/slab.h>
6 #include <linux/netdevice.h>
7 #include <linux/rcupdate.h>
11 struct mlx5e_selq_params {
12 unsigned int num_regular_queues;
13 unsigned int num_channels;
19 int mlx5e_selq_init(struct mlx5e_selq *selq, struct mutex *state_lock)
21 struct mlx5e_selq_params *init_params;
23 selq->state_lock = state_lock;
25 selq->standby = kvzalloc(sizeof(*selq->standby), GFP_KERNEL);
29 init_params = kvzalloc(sizeof(*selq->active), GFP_KERNEL);
31 kvfree(selq->standby);
35 /* Assign dummy values, so that mlx5e_select_queue won't crash. */
36 *init_params = (struct mlx5e_selq_params) {
37 .num_regular_queues = 1,
43 rcu_assign_pointer(selq->active, init_params);
48 void mlx5e_selq_cleanup(struct mlx5e_selq *selq)
50 WARN_ON_ONCE(selq->is_prepared);
52 kvfree(selq->standby);
54 selq->is_prepared = true;
56 mlx5e_selq_apply(selq);
58 kvfree(selq->standby);
62 void mlx5e_selq_prepare(struct mlx5e_selq *selq, struct mlx5e_params *params, bool htb)
64 lockdep_assert_held(selq->state_lock);
65 WARN_ON_ONCE(selq->is_prepared);
67 selq->is_prepared = true;
69 selq->standby->num_channels = params->num_channels;
70 selq->standby->num_tcs = mlx5e_get_dcb_num_tc(params);
71 selq->standby->num_regular_queues =
72 selq->standby->num_channels * selq->standby->num_tcs;
73 selq->standby->is_htb = htb;
74 selq->standby->is_ptp = MLX5E_GET_PFLAG(params, MLX5E_PFLAG_TX_PORT_TS);
77 void mlx5e_selq_apply(struct mlx5e_selq *selq)
79 struct mlx5e_selq_params *old_params;
81 WARN_ON_ONCE(!selq->is_prepared);
83 selq->is_prepared = false;
85 old_params = rcu_replace_pointer(selq->active, selq->standby,
86 lockdep_is_held(selq->state_lock));
87 synchronize_net(); /* Wait until ndo_select_queue starts emitting correct values. */
88 selq->standby = old_params;
91 void mlx5e_selq_cancel(struct mlx5e_selq *selq)
93 lockdep_assert_held(selq->state_lock);
94 WARN_ON_ONCE(!selq->is_prepared);
96 selq->is_prepared = false;
99 #ifdef CONFIG_MLX5_CORE_EN_DCB
100 static int mlx5e_get_dscp_up(struct mlx5e_priv *priv, struct sk_buff *skb)
104 if (skb->protocol == htons(ETH_P_IP))
105 dscp_cp = ipv4_get_dsfield(ip_hdr(skb)) >> 2;
106 else if (skb->protocol == htons(ETH_P_IPV6))
107 dscp_cp = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2;
109 return priv->dcbx_dp.dscp2prio[dscp_cp];
113 static u16 mlx5e_select_ptpsq(struct net_device *dev, struct sk_buff *skb,
114 struct mlx5e_selq_params *selq)
116 struct mlx5e_priv *priv = netdev_priv(dev);
119 if (selq->num_tcs <= 1)
122 #ifdef CONFIG_MLX5_CORE_EN_DCB
123 if (priv->dcbx_dp.trust_state == MLX5_QPTS_TRUST_DSCP)
124 up = mlx5e_get_dscp_up(priv, skb);
127 if (skb_vlan_tag_present(skb))
128 up = skb_vlan_tag_get_prio(skb);
131 return selq->num_regular_queues + up;
134 static int mlx5e_select_htb_queue(struct mlx5e_priv *priv, struct sk_buff *skb)
138 /* Order maj_id before defcls - pairs with mlx5e_htb_root_add. */
139 if ((TC_H_MAJ(skb->priority) >> 16) == smp_load_acquire(&priv->htb.maj_id))
140 classid = TC_H_MIN(skb->priority);
142 classid = READ_ONCE(priv->htb.defcls);
147 return mlx5e_get_txq_by_classid(priv, classid);
150 u16 mlx5e_select_queue(struct net_device *dev, struct sk_buff *skb,
151 struct net_device *sb_dev)
153 struct mlx5e_priv *priv = netdev_priv(dev);
154 struct mlx5e_selq_params *selq;
158 selq = rcu_dereference_bh(priv->selq.active);
160 /* This is a workaround needed only for the mlx5e_netdev_change_profile
161 * flow that zeroes out the whole priv without unregistering the netdev
162 * and without preventing ndo_select_queue from being called.
167 if (unlikely(selq->is_ptp || selq->is_htb)) {
168 if (unlikely(selq->is_htb)) {
169 txq_ix = mlx5e_select_htb_queue(priv, skb);
174 if (unlikely(selq->is_ptp && mlx5e_use_ptpsq(skb)))
175 return mlx5e_select_ptpsq(dev, skb, selq);
177 txq_ix = netdev_pick_tx(dev, skb, NULL);
178 /* Fix netdev_pick_tx() not to choose ptp_channel and HTB txqs.
179 * If they are selected, switch to regular queues.
180 * Driver to select these queues only at mlx5e_select_ptpsq()
181 * and mlx5e_select_htb_queue().
183 if (unlikely(txq_ix >= selq->num_regular_queues))
184 txq_ix %= selq->num_regular_queues;
186 txq_ix = netdev_pick_tx(dev, skb, NULL);
189 if (selq->num_tcs <= 1)
192 #ifdef CONFIG_MLX5_CORE_EN_DCB
193 if (priv->dcbx_dp.trust_state == MLX5_QPTS_TRUST_DSCP)
194 up = mlx5e_get_dscp_up(priv, skb);
197 if (skb_vlan_tag_present(skb))
198 up = skb_vlan_tag_get_prio(skb);
200 /* Normalize any picked txq_ix to [0, num_channels),
201 * So we can return a txq_ix that matches the channel and
204 return txq_ix % selq->num_channels + up * selq->num_channels;