Merge tag 'for_v5.7-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs
[linux-2.6-microblaze.git] / drivers / net / ethernet / mellanox / mlx5 / core / eswitch_offloads_termtbl.c
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 // Copyright (c) 2019 Mellanox Technologies.
3
4 #include <linux/mlx5/fs.h>
5 #include "eswitch.h"
6 #include "fs_core.h"
7
8 struct mlx5_termtbl_handle {
9         struct hlist_node termtbl_hlist;
10
11         struct mlx5_flow_table *termtbl;
12         struct mlx5_flow_act flow_act;
13         struct mlx5_flow_destination dest;
14
15         struct mlx5_flow_handle *rule;
16         int ref_count;
17 };
18
19 static u32
20 mlx5_eswitch_termtbl_hash(struct mlx5_flow_act *flow_act,
21                           struct mlx5_flow_destination *dest)
22 {
23         u32 hash;
24
25         hash = jhash_1word(flow_act->action, 0);
26         hash = jhash((const void *)&flow_act->vlan,
27                      sizeof(flow_act->vlan), hash);
28         hash = jhash((const void *)&dest->vport.num,
29                      sizeof(dest->vport.num), hash);
30         hash = jhash((const void *)&dest->vport.vhca_id,
31                      sizeof(dest->vport.num), hash);
32         if (dest->vport.pkt_reformat)
33                 hash = jhash(dest->vport.pkt_reformat,
34                              sizeof(*dest->vport.pkt_reformat),
35                              hash);
36         return hash;
37 }
38
39 static int
40 mlx5_eswitch_termtbl_cmp(struct mlx5_flow_act *flow_act1,
41                          struct mlx5_flow_destination *dest1,
42                          struct mlx5_flow_act *flow_act2,
43                          struct mlx5_flow_destination *dest2)
44 {
45         int ret;
46
47         ret = flow_act1->action != flow_act2->action ||
48               dest1->vport.num != dest2->vport.num ||
49               dest1->vport.vhca_id != dest2->vport.vhca_id ||
50               memcmp(&flow_act1->vlan, &flow_act2->vlan,
51                      sizeof(flow_act1->vlan));
52         if (ret)
53                 return ret;
54
55         return dest1->vport.pkt_reformat && dest2->vport.pkt_reformat ?
56                memcmp(dest1->vport.pkt_reformat, dest2->vport.pkt_reformat,
57                       sizeof(*dest1->vport.pkt_reformat)) : 0;
58 }
59
60 static int
61 mlx5_eswitch_termtbl_create(struct mlx5_core_dev *dev,
62                             struct mlx5_termtbl_handle *tt,
63                             struct mlx5_flow_act *flow_act)
64 {
65         struct mlx5_flow_table_attr ft_attr = {};
66         struct mlx5_flow_namespace *root_ns;
67         int err;
68
69         root_ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_FDB);
70         if (!root_ns) {
71                 esw_warn(dev, "Failed to get FDB flow namespace\n");
72                 return -EOPNOTSUPP;
73         }
74
75         /* As this is the terminating action then the termination table is the
76          * same prio as the slow path
77          */
78         ft_attr.flags = MLX5_FLOW_TABLE_TERMINATION |
79                         MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
80         ft_attr.prio = FDB_SLOW_PATH;
81         ft_attr.max_fte = 1;
82         ft_attr.autogroup.max_num_groups = 1;
83         tt->termtbl = mlx5_create_auto_grouped_flow_table(root_ns, &ft_attr);
84         if (IS_ERR(tt->termtbl)) {
85                 esw_warn(dev, "Failed to create termination table\n");
86                 return -EOPNOTSUPP;
87         }
88
89         tt->rule = mlx5_add_flow_rules(tt->termtbl, NULL, flow_act,
90                                        &tt->dest, 1);
91         if (IS_ERR(tt->rule)) {
92                 esw_warn(dev, "Failed to create termination table rule\n");
93                 goto add_flow_err;
94         }
95         return 0;
96
97 add_flow_err:
98         err = mlx5_destroy_flow_table(tt->termtbl);
99         if (err)
100                 esw_warn(dev, "Failed to destroy termination table\n");
101
102         return -EOPNOTSUPP;
103 }
104
105 static struct mlx5_termtbl_handle *
106 mlx5_eswitch_termtbl_get_create(struct mlx5_eswitch *esw,
107                                 struct mlx5_flow_act *flow_act,
108                                 struct mlx5_flow_destination *dest,
109                                 struct mlx5_esw_flow_attr *attr)
110 {
111         struct mlx5_termtbl_handle *tt;
112         bool found = false;
113         u32 hash_key;
114         int err;
115
116         mutex_lock(&esw->offloads.termtbl_mutex);
117         hash_key = mlx5_eswitch_termtbl_hash(flow_act, dest);
118         hash_for_each_possible(esw->offloads.termtbl_tbl, tt,
119                                termtbl_hlist, hash_key) {
120                 if (!mlx5_eswitch_termtbl_cmp(&tt->flow_act, &tt->dest,
121                                               flow_act, dest)) {
122                         found = true;
123                         break;
124                 }
125         }
126         if (found)
127                 goto tt_add_ref;
128
129         tt = kzalloc(sizeof(*tt), GFP_KERNEL);
130         if (!tt) {
131                 err = -ENOMEM;
132                 goto tt_create_err;
133         }
134
135         tt->dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
136         tt->dest.vport.num = dest->vport.num;
137         tt->dest.vport.vhca_id = dest->vport.vhca_id;
138         tt->dest.vport.flags = dest->vport.flags;
139         memcpy(&tt->flow_act, flow_act, sizeof(*flow_act));
140
141         err = mlx5_eswitch_termtbl_create(esw->dev, tt, flow_act);
142         if (err) {
143                 esw_warn(esw->dev, "Failed to create termination table\n");
144                 goto tt_create_err;
145         }
146         hash_add(esw->offloads.termtbl_tbl, &tt->termtbl_hlist, hash_key);
147 tt_add_ref:
148         tt->ref_count++;
149         mutex_unlock(&esw->offloads.termtbl_mutex);
150         return tt;
151 tt_create_err:
152         kfree(tt);
153         mutex_unlock(&esw->offloads.termtbl_mutex);
154         return ERR_PTR(err);
155 }
156
157 void
158 mlx5_eswitch_termtbl_put(struct mlx5_eswitch *esw,
159                          struct mlx5_termtbl_handle *tt)
160 {
161         mutex_lock(&esw->offloads.termtbl_mutex);
162         if (--tt->ref_count == 0)
163                 hash_del(&tt->termtbl_hlist);
164         mutex_unlock(&esw->offloads.termtbl_mutex);
165
166         if (!tt->ref_count) {
167                 mlx5_del_flow_rules(tt->rule);
168                 mlx5_destroy_flow_table(tt->termtbl);
169                 kfree(tt);
170         }
171 }
172
173 static bool mlx5_eswitch_termtbl_is_encap_reformat(struct mlx5_pkt_reformat *rt)
174 {
175         switch (rt->reformat_type) {
176         case MLX5_REFORMAT_TYPE_L2_TO_VXLAN:
177         case MLX5_REFORMAT_TYPE_L2_TO_NVGRE:
178         case MLX5_REFORMAT_TYPE_L2_TO_L2_TUNNEL:
179         case MLX5_REFORMAT_TYPE_L2_TO_L3_TUNNEL:
180                 return true;
181         default:
182                 return false;
183         }
184 }
185
186 static void
187 mlx5_eswitch_termtbl_actions_move(struct mlx5_flow_act *src,
188                                   struct mlx5_flow_act *dst)
189 {
190         if (src->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH) {
191                 src->action &= ~MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
192                 dst->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
193                 memcpy(&dst->vlan[0], &src->vlan[0], sizeof(src->vlan[0]));
194                 memset(&src->vlan[0], 0, sizeof(src->vlan[0]));
195
196                 if (src->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2) {
197                         src->action &= ~MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2;
198                         dst->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2;
199                         memcpy(&dst->vlan[1], &src->vlan[1], sizeof(src->vlan[1]));
200                         memset(&src->vlan[1], 0, sizeof(src->vlan[1]));
201                 }
202         }
203
204         if (src->action & MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT &&
205             mlx5_eswitch_termtbl_is_encap_reformat(src->pkt_reformat)) {
206                 src->action &= ~MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
207                 dst->action |= MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
208                 dst->pkt_reformat = src->pkt_reformat;
209                 src->pkt_reformat = NULL;
210         }
211 }
212
213 static bool mlx5_eswitch_offload_is_uplink_port(const struct mlx5_eswitch *esw,
214                                                 const struct mlx5_flow_spec *spec)
215 {
216         u16 port_mask, port_value;
217
218         if (MLX5_CAP_ESW_FLOWTABLE(esw->dev, flow_source))
219                 return spec->flow_context.flow_source ==
220                                         MLX5_FLOW_CONTEXT_FLOW_SOURCE_UPLINK;
221
222         port_mask = MLX5_GET(fte_match_param, spec->match_criteria,
223                              misc_parameters.source_port);
224         port_value = MLX5_GET(fte_match_param, spec->match_value,
225                               misc_parameters.source_port);
226         return (port_mask & port_value) == MLX5_VPORT_UPLINK;
227 }
228
229 bool
230 mlx5_eswitch_termtbl_required(struct mlx5_eswitch *esw,
231                               struct mlx5_esw_flow_attr *attr,
232                               struct mlx5_flow_act *flow_act,
233                               struct mlx5_flow_spec *spec)
234 {
235         int i;
236
237         if (!MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, termination_table) ||
238             attr->flags & MLX5_ESW_ATTR_FLAG_SLOW_PATH ||
239             !mlx5_eswitch_offload_is_uplink_port(esw, spec))
240                 return false;
241
242         /* push vlan on RX */
243         if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH)
244                 return true;
245
246         /* hairpin */
247         for (i = attr->split_count; i < attr->out_count; i++)
248                 if (attr->dests[i].rep->vport == MLX5_VPORT_UPLINK)
249                         return true;
250
251         return false;
252 }
253
254 struct mlx5_flow_handle *
255 mlx5_eswitch_add_termtbl_rule(struct mlx5_eswitch *esw,
256                               struct mlx5_flow_table *fdb,
257                               struct mlx5_flow_spec *spec,
258                               struct mlx5_esw_flow_attr *attr,
259                               struct mlx5_flow_act *flow_act,
260                               struct mlx5_flow_destination *dest,
261                               int num_dest)
262 {
263         struct mlx5_flow_act term_tbl_act = {};
264         struct mlx5_flow_handle *rule = NULL;
265         bool term_table_created = false;
266         int num_vport_dests = 0;
267         int i, curr_dest;
268
269         mlx5_eswitch_termtbl_actions_move(flow_act, &term_tbl_act);
270         term_tbl_act.action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
271
272         for (i = 0; i < num_dest; i++) {
273                 struct mlx5_termtbl_handle *tt;
274
275                 /* only vport destinations can be terminated */
276                 if (dest[i].type != MLX5_FLOW_DESTINATION_TYPE_VPORT)
277                         continue;
278
279                 /* get the terminating table for the action list */
280                 tt = mlx5_eswitch_termtbl_get_create(esw, &term_tbl_act,
281                                                      &dest[i], attr);
282                 if (IS_ERR(tt)) {
283                         esw_warn(esw->dev, "Failed to create termination table\n");
284                         goto revert_changes;
285                 }
286                 attr->dests[num_vport_dests].termtbl = tt;
287                 num_vport_dests++;
288
289                 /* link the destination with the termination table */
290                 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
291                 dest[i].ft = tt->termtbl;
292                 term_table_created = true;
293         }
294
295         /* at least one destination should reference a termination table */
296         if (!term_table_created)
297                 goto revert_changes;
298
299         /* create the FTE */
300         rule = mlx5_add_flow_rules(fdb, spec, flow_act, dest, num_dest);
301         if (IS_ERR(rule))
302                 goto revert_changes;
303
304         goto out;
305
306 revert_changes:
307         /* revert the changes that were made to the original flow_act
308          * and fall-back to the original rule actions
309          */
310         mlx5_eswitch_termtbl_actions_move(&term_tbl_act, flow_act);
311
312         for (curr_dest = 0; curr_dest < num_vport_dests; curr_dest++) {
313                 struct mlx5_termtbl_handle *tt = attr->dests[curr_dest].termtbl;
314
315                 /* search for the destination associated with the
316                  * current term table
317                  */
318                 for (i = 0; i < num_dest; i++) {
319                         if (dest[i].ft != tt->termtbl)
320                                 continue;
321
322                         memset(&dest[i], 0, sizeof(dest[i]));
323                         dest[i].type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
324                         dest[i].vport.num = tt->dest.vport.num;
325                         dest[i].vport.vhca_id = tt->dest.vport.vhca_id;
326                         mlx5_eswitch_termtbl_put(esw, tt);
327                         break;
328                 }
329         }
330         rule = mlx5_add_flow_rules(fdb, spec, flow_act, dest, num_dest);
331 out:
332         return rule;
333 }