Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / drivers / net / ethernet / mellanox / mlx5 / core / eswitch.c
1 /*
2  * Copyright (c) 2015, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include <linux/etherdevice.h>
34 #include <linux/mlx5/driver.h>
35 #include <linux/mlx5/mlx5_ifc.h>
36 #include <linux/mlx5/vport.h>
37 #include <linux/mlx5/fs.h>
38 #include "mlx5_core.h"
39 #include "eswitch.h"
40
41 #define UPLINK_VPORT 0xFFFF
42
43 enum {
44         MLX5_ACTION_NONE = 0,
45         MLX5_ACTION_ADD  = 1,
46         MLX5_ACTION_DEL  = 2,
47 };
48
49 /* E-Switch UC L2 table hash node */
50 struct esw_uc_addr {
51         struct l2addr_node node;
52         u32                table_index;
53         u32                vport;
54 };
55
56 /* Vport UC/MC hash node */
57 struct vport_addr {
58         struct l2addr_node     node;
59         u8                     action;
60         u32                    vport;
61         struct mlx5_flow_handle *flow_rule; /* SRIOV only */
62         /* A flag indicating that mac was added due to mc promiscuous vport */
63         bool mc_promisc;
64 };
65
66 enum {
67         UC_ADDR_CHANGE = BIT(0),
68         MC_ADDR_CHANGE = BIT(1),
69         PROMISC_CHANGE = BIT(3),
70 };
71
72 /* Vport context events */
73 #define SRIOV_VPORT_EVENTS (UC_ADDR_CHANGE | \
74                             MC_ADDR_CHANGE | \
75                             PROMISC_CHANGE)
76
77 static int arm_vport_context_events_cmd(struct mlx5_core_dev *dev, u16 vport,
78                                         u32 events_mask)
79 {
80         int in[MLX5_ST_SZ_DW(modify_nic_vport_context_in)]   = {0};
81         int out[MLX5_ST_SZ_DW(modify_nic_vport_context_out)] = {0};
82         void *nic_vport_ctx;
83
84         MLX5_SET(modify_nic_vport_context_in, in,
85                  opcode, MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
86         MLX5_SET(modify_nic_vport_context_in, in, field_select.change_event, 1);
87         MLX5_SET(modify_nic_vport_context_in, in, vport_number, vport);
88         if (vport)
89                 MLX5_SET(modify_nic_vport_context_in, in, other_vport, 1);
90         nic_vport_ctx = MLX5_ADDR_OF(modify_nic_vport_context_in,
91                                      in, nic_vport_context);
92
93         MLX5_SET(nic_vport_context, nic_vport_ctx, arm_change_event, 1);
94
95         if (events_mask & UC_ADDR_CHANGE)
96                 MLX5_SET(nic_vport_context, nic_vport_ctx,
97                          event_on_uc_address_change, 1);
98         if (events_mask & MC_ADDR_CHANGE)
99                 MLX5_SET(nic_vport_context, nic_vport_ctx,
100                          event_on_mc_address_change, 1);
101         if (events_mask & PROMISC_CHANGE)
102                 MLX5_SET(nic_vport_context, nic_vport_ctx,
103                          event_on_promisc_change, 1);
104
105         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
106 }
107
108 /* E-Switch vport context HW commands */
109 static int modify_esw_vport_context_cmd(struct mlx5_core_dev *dev, u16 vport,
110                                         void *in, int inlen)
111 {
112         u32 out[MLX5_ST_SZ_DW(modify_esw_vport_context_out)] = {0};
113
114         MLX5_SET(modify_esw_vport_context_in, in, opcode,
115                  MLX5_CMD_OP_MODIFY_ESW_VPORT_CONTEXT);
116         MLX5_SET(modify_esw_vport_context_in, in, vport_number, vport);
117         if (vport)
118                 MLX5_SET(modify_esw_vport_context_in, in, other_vport, 1);
119         return mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
120 }
121
122 static int modify_esw_vport_cvlan(struct mlx5_core_dev *dev, u32 vport,
123                                   u16 vlan, u8 qos, u8 set_flags)
124 {
125         u32 in[MLX5_ST_SZ_DW(modify_esw_vport_context_in)] = {0};
126
127         if (!MLX5_CAP_ESW(dev, vport_cvlan_strip) ||
128             !MLX5_CAP_ESW(dev, vport_cvlan_insert_if_not_exist))
129                 return -EOPNOTSUPP;
130
131         esw_debug(dev, "Set Vport[%d] VLAN %d qos %d set=%x\n",
132                   vport, vlan, qos, set_flags);
133
134         if (set_flags & SET_VLAN_STRIP)
135                 MLX5_SET(modify_esw_vport_context_in, in,
136                          esw_vport_context.vport_cvlan_strip, 1);
137
138         if (set_flags & SET_VLAN_INSERT) {
139                 /* insert only if no vlan in packet */
140                 MLX5_SET(modify_esw_vport_context_in, in,
141                          esw_vport_context.vport_cvlan_insert, 1);
142
143                 MLX5_SET(modify_esw_vport_context_in, in,
144                          esw_vport_context.cvlan_pcp, qos);
145                 MLX5_SET(modify_esw_vport_context_in, in,
146                          esw_vport_context.cvlan_id, vlan);
147         }
148
149         MLX5_SET(modify_esw_vport_context_in, in,
150                  field_select.vport_cvlan_strip, 1);
151         MLX5_SET(modify_esw_vport_context_in, in,
152                  field_select.vport_cvlan_insert, 1);
153
154         return modify_esw_vport_context_cmd(dev, vport, in, sizeof(in));
155 }
156
157 /* HW L2 Table (MPFS) management */
158 static int set_l2_table_entry_cmd(struct mlx5_core_dev *dev, u32 index,
159                                   u8 *mac, u8 vlan_valid, u16 vlan)
160 {
161         u32 in[MLX5_ST_SZ_DW(set_l2_table_entry_in)]   = {0};
162         u32 out[MLX5_ST_SZ_DW(set_l2_table_entry_out)] = {0};
163         u8 *in_mac_addr;
164
165         MLX5_SET(set_l2_table_entry_in, in, opcode,
166                  MLX5_CMD_OP_SET_L2_TABLE_ENTRY);
167         MLX5_SET(set_l2_table_entry_in, in, table_index, index);
168         MLX5_SET(set_l2_table_entry_in, in, vlan_valid, vlan_valid);
169         MLX5_SET(set_l2_table_entry_in, in, vlan, vlan);
170
171         in_mac_addr = MLX5_ADDR_OF(set_l2_table_entry_in, in, mac_address);
172         ether_addr_copy(&in_mac_addr[2], mac);
173
174         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
175 }
176
177 static int del_l2_table_entry_cmd(struct mlx5_core_dev *dev, u32 index)
178 {
179         u32 in[MLX5_ST_SZ_DW(delete_l2_table_entry_in)]   = {0};
180         u32 out[MLX5_ST_SZ_DW(delete_l2_table_entry_out)] = {0};
181
182         MLX5_SET(delete_l2_table_entry_in, in, opcode,
183                  MLX5_CMD_OP_DELETE_L2_TABLE_ENTRY);
184         MLX5_SET(delete_l2_table_entry_in, in, table_index, index);
185         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
186 }
187
188 static int alloc_l2_table_index(struct mlx5_l2_table *l2_table, u32 *ix)
189 {
190         int err = 0;
191
192         *ix = find_first_zero_bit(l2_table->bitmap, l2_table->size);
193         if (*ix >= l2_table->size)
194                 err = -ENOSPC;
195         else
196                 __set_bit(*ix, l2_table->bitmap);
197
198         return err;
199 }
200
201 static void free_l2_table_index(struct mlx5_l2_table *l2_table, u32 ix)
202 {
203         __clear_bit(ix, l2_table->bitmap);
204 }
205
206 static int set_l2_table_entry(struct mlx5_core_dev *dev, u8 *mac,
207                               u8 vlan_valid, u16 vlan,
208                               u32 *index)
209 {
210         struct mlx5_l2_table *l2_table = &dev->priv.eswitch->l2_table;
211         int err;
212
213         err = alloc_l2_table_index(l2_table, index);
214         if (err)
215                 return err;
216
217         err = set_l2_table_entry_cmd(dev, *index, mac, vlan_valid, vlan);
218         if (err)
219                 free_l2_table_index(l2_table, *index);
220
221         return err;
222 }
223
224 static void del_l2_table_entry(struct mlx5_core_dev *dev, u32 index)
225 {
226         struct mlx5_l2_table *l2_table = &dev->priv.eswitch->l2_table;
227
228         del_l2_table_entry_cmd(dev, index);
229         free_l2_table_index(l2_table, index);
230 }
231
232 /* E-Switch FDB */
233 static struct mlx5_flow_handle *
234 __esw_fdb_set_vport_rule(struct mlx5_eswitch *esw, u32 vport, bool rx_rule,
235                          u8 mac_c[ETH_ALEN], u8 mac_v[ETH_ALEN])
236 {
237         int match_header = (is_zero_ether_addr(mac_c) ? 0 :
238                             MLX5_MATCH_OUTER_HEADERS);
239         struct mlx5_flow_handle *flow_rule = NULL;
240         struct mlx5_flow_act flow_act = {0};
241         struct mlx5_flow_destination dest;
242         struct mlx5_flow_spec *spec;
243         void *mv_misc = NULL;
244         void *mc_misc = NULL;
245         u8 *dmac_v = NULL;
246         u8 *dmac_c = NULL;
247
248         if (rx_rule)
249                 match_header |= MLX5_MATCH_MISC_PARAMETERS;
250
251         spec = mlx5_vzalloc(sizeof(*spec));
252         if (!spec) {
253                 esw_warn(esw->dev, "FDB: Failed to alloc match parameters\n");
254                 return NULL;
255         }
256         dmac_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
257                               outer_headers.dmac_47_16);
258         dmac_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
259                               outer_headers.dmac_47_16);
260
261         if (match_header & MLX5_MATCH_OUTER_HEADERS) {
262                 ether_addr_copy(dmac_v, mac_v);
263                 ether_addr_copy(dmac_c, mac_c);
264         }
265
266         if (match_header & MLX5_MATCH_MISC_PARAMETERS) {
267                 mv_misc  = MLX5_ADDR_OF(fte_match_param, spec->match_value,
268                                         misc_parameters);
269                 mc_misc  = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
270                                         misc_parameters);
271                 MLX5_SET(fte_match_set_misc, mv_misc, source_port, UPLINK_VPORT);
272                 MLX5_SET_TO_ONES(fte_match_set_misc, mc_misc, source_port);
273         }
274
275         dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
276         dest.vport_num = vport;
277
278         esw_debug(esw->dev,
279                   "\tFDB add rule dmac_v(%pM) dmac_c(%pM) -> vport(%d)\n",
280                   dmac_v, dmac_c, vport);
281         spec->match_criteria_enable = match_header;
282         flow_act.action =  MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
283         flow_rule =
284                 mlx5_add_flow_rules(esw->fdb_table.fdb, spec,
285                                     &flow_act, &dest, 1);
286         if (IS_ERR(flow_rule)) {
287                 esw_warn(esw->dev,
288                          "FDB: Failed to add flow rule: dmac_v(%pM) dmac_c(%pM) -> vport(%d), err(%ld)\n",
289                          dmac_v, dmac_c, vport, PTR_ERR(flow_rule));
290                 flow_rule = NULL;
291         }
292
293         kvfree(spec);
294         return flow_rule;
295 }
296
297 static struct mlx5_flow_handle *
298 esw_fdb_set_vport_rule(struct mlx5_eswitch *esw, u8 mac[ETH_ALEN], u32 vport)
299 {
300         u8 mac_c[ETH_ALEN];
301
302         eth_broadcast_addr(mac_c);
303         return __esw_fdb_set_vport_rule(esw, vport, false, mac_c, mac);
304 }
305
306 static struct mlx5_flow_handle *
307 esw_fdb_set_vport_allmulti_rule(struct mlx5_eswitch *esw, u32 vport)
308 {
309         u8 mac_c[ETH_ALEN];
310         u8 mac_v[ETH_ALEN];
311
312         eth_zero_addr(mac_c);
313         eth_zero_addr(mac_v);
314         mac_c[0] = 0x01;
315         mac_v[0] = 0x01;
316         return __esw_fdb_set_vport_rule(esw, vport, false, mac_c, mac_v);
317 }
318
319 static struct mlx5_flow_handle *
320 esw_fdb_set_vport_promisc_rule(struct mlx5_eswitch *esw, u32 vport)
321 {
322         u8 mac_c[ETH_ALEN];
323         u8 mac_v[ETH_ALEN];
324
325         eth_zero_addr(mac_c);
326         eth_zero_addr(mac_v);
327         return __esw_fdb_set_vport_rule(esw, vport, true, mac_c, mac_v);
328 }
329
330 static int esw_create_legacy_fdb_table(struct mlx5_eswitch *esw, int nvports)
331 {
332         int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
333         struct mlx5_flow_table_attr ft_attr = {};
334         struct mlx5_core_dev *dev = esw->dev;
335         struct mlx5_flow_namespace *root_ns;
336         struct mlx5_flow_table *fdb;
337         struct mlx5_flow_group *g;
338         void *match_criteria;
339         int table_size;
340         u32 *flow_group_in;
341         u8 *dmac;
342         int err = 0;
343
344         esw_debug(dev, "Create FDB log_max_size(%d)\n",
345                   MLX5_CAP_ESW_FLOWTABLE_FDB(dev, log_max_ft_size));
346
347         root_ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_FDB);
348         if (!root_ns) {
349                 esw_warn(dev, "Failed to get FDB flow namespace\n");
350                 return -EOPNOTSUPP;
351         }
352
353         flow_group_in = mlx5_vzalloc(inlen);
354         if (!flow_group_in)
355                 return -ENOMEM;
356         memset(flow_group_in, 0, inlen);
357
358         table_size = BIT(MLX5_CAP_ESW_FLOWTABLE_FDB(dev, log_max_ft_size));
359
360         ft_attr.max_fte = table_size;
361         fdb = mlx5_create_flow_table(root_ns, &ft_attr);
362         if (IS_ERR(fdb)) {
363                 err = PTR_ERR(fdb);
364                 esw_warn(dev, "Failed to create FDB Table err %d\n", err);
365                 goto out;
366         }
367         esw->fdb_table.fdb = fdb;
368
369         /* Addresses group : Full match unicast/multicast addresses */
370         MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
371                  MLX5_MATCH_OUTER_HEADERS);
372         match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in, match_criteria);
373         dmac = MLX5_ADDR_OF(fte_match_param, match_criteria, outer_headers.dmac_47_16);
374         MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
375         /* Preserve 2 entries for allmulti and promisc rules*/
376         MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, table_size - 3);
377         eth_broadcast_addr(dmac);
378         g = mlx5_create_flow_group(fdb, flow_group_in);
379         if (IS_ERR(g)) {
380                 err = PTR_ERR(g);
381                 esw_warn(dev, "Failed to create flow group err(%d)\n", err);
382                 goto out;
383         }
384         esw->fdb_table.legacy.addr_grp = g;
385
386         /* Allmulti group : One rule that forwards any mcast traffic */
387         MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
388                  MLX5_MATCH_OUTER_HEADERS);
389         MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, table_size - 2);
390         MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, table_size - 2);
391         eth_zero_addr(dmac);
392         dmac[0] = 0x01;
393         g = mlx5_create_flow_group(fdb, flow_group_in);
394         if (IS_ERR(g)) {
395                 err = PTR_ERR(g);
396                 esw_warn(dev, "Failed to create allmulti flow group err(%d)\n", err);
397                 goto out;
398         }
399         esw->fdb_table.legacy.allmulti_grp = g;
400
401         /* Promiscuous group :
402          * One rule that forward all unmatched traffic from previous groups
403          */
404         eth_zero_addr(dmac);
405         MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable,
406                  MLX5_MATCH_MISC_PARAMETERS);
407         MLX5_SET_TO_ONES(fte_match_param, match_criteria, misc_parameters.source_port);
408         MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, table_size - 1);
409         MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, table_size - 1);
410         g = mlx5_create_flow_group(fdb, flow_group_in);
411         if (IS_ERR(g)) {
412                 err = PTR_ERR(g);
413                 esw_warn(dev, "Failed to create promisc flow group err(%d)\n", err);
414                 goto out;
415         }
416         esw->fdb_table.legacy.promisc_grp = g;
417
418 out:
419         if (err) {
420                 if (!IS_ERR_OR_NULL(esw->fdb_table.legacy.allmulti_grp)) {
421                         mlx5_destroy_flow_group(esw->fdb_table.legacy.allmulti_grp);
422                         esw->fdb_table.legacy.allmulti_grp = NULL;
423                 }
424                 if (!IS_ERR_OR_NULL(esw->fdb_table.legacy.addr_grp)) {
425                         mlx5_destroy_flow_group(esw->fdb_table.legacy.addr_grp);
426                         esw->fdb_table.legacy.addr_grp = NULL;
427                 }
428                 if (!IS_ERR_OR_NULL(esw->fdb_table.fdb)) {
429                         mlx5_destroy_flow_table(esw->fdb_table.fdb);
430                         esw->fdb_table.fdb = NULL;
431                 }
432         }
433
434         kvfree(flow_group_in);
435         return err;
436 }
437
438 static void esw_destroy_legacy_fdb_table(struct mlx5_eswitch *esw)
439 {
440         if (!esw->fdb_table.fdb)
441                 return;
442
443         esw_debug(esw->dev, "Destroy FDB Table\n");
444         mlx5_destroy_flow_group(esw->fdb_table.legacy.promisc_grp);
445         mlx5_destroy_flow_group(esw->fdb_table.legacy.allmulti_grp);
446         mlx5_destroy_flow_group(esw->fdb_table.legacy.addr_grp);
447         mlx5_destroy_flow_table(esw->fdb_table.fdb);
448         esw->fdb_table.fdb = NULL;
449         esw->fdb_table.legacy.addr_grp = NULL;
450         esw->fdb_table.legacy.allmulti_grp = NULL;
451         esw->fdb_table.legacy.promisc_grp = NULL;
452 }
453
454 /* E-Switch vport UC/MC lists management */
455 typedef int (*vport_addr_action)(struct mlx5_eswitch *esw,
456                                  struct vport_addr *vaddr);
457
458 static int esw_add_uc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
459 {
460         struct hlist_head *hash = esw->l2_table.l2_hash;
461         struct esw_uc_addr *esw_uc;
462         u8 *mac = vaddr->node.addr;
463         u32 vport = vaddr->vport;
464         int err;
465
466         esw_uc = l2addr_hash_find(hash, mac, struct esw_uc_addr);
467         if (esw_uc) {
468                 esw_warn(esw->dev,
469                          "Failed to set L2 mac(%pM) for vport(%d), mac is already in use by vport(%d)\n",
470                          mac, vport, esw_uc->vport);
471                 return -EEXIST;
472         }
473
474         esw_uc = l2addr_hash_add(hash, mac, struct esw_uc_addr, GFP_KERNEL);
475         if (!esw_uc)
476                 return -ENOMEM;
477         esw_uc->vport = vport;
478
479         err = set_l2_table_entry(esw->dev, mac, 0, 0, &esw_uc->table_index);
480         if (err)
481                 goto abort;
482
483         /* SRIOV is enabled: Forward UC MAC to vport */
484         if (esw->fdb_table.fdb && esw->mode == SRIOV_LEGACY)
485                 vaddr->flow_rule = esw_fdb_set_vport_rule(esw, mac, vport);
486
487         esw_debug(esw->dev, "\tADDED UC MAC: vport[%d] %pM index:%d fr(%p)\n",
488                   vport, mac, esw_uc->table_index, vaddr->flow_rule);
489         return err;
490 abort:
491         l2addr_hash_del(esw_uc);
492         return err;
493 }
494
495 static int esw_del_uc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
496 {
497         struct hlist_head *hash = esw->l2_table.l2_hash;
498         struct esw_uc_addr *esw_uc;
499         u8 *mac = vaddr->node.addr;
500         u32 vport = vaddr->vport;
501
502         esw_uc = l2addr_hash_find(hash, mac, struct esw_uc_addr);
503         if (!esw_uc || esw_uc->vport != vport) {
504                 esw_debug(esw->dev,
505                           "MAC(%pM) doesn't belong to vport (%d)\n",
506                           mac, vport);
507                 return -EINVAL;
508         }
509         esw_debug(esw->dev, "\tDELETE UC MAC: vport[%d] %pM index:%d fr(%p)\n",
510                   vport, mac, esw_uc->table_index, vaddr->flow_rule);
511
512         del_l2_table_entry(esw->dev, esw_uc->table_index);
513
514         if (vaddr->flow_rule)
515                 mlx5_del_flow_rules(vaddr->flow_rule);
516         vaddr->flow_rule = NULL;
517
518         l2addr_hash_del(esw_uc);
519         return 0;
520 }
521
522 static void update_allmulti_vports(struct mlx5_eswitch *esw,
523                                    struct vport_addr *vaddr,
524                                    struct esw_mc_addr *esw_mc)
525 {
526         u8 *mac = vaddr->node.addr;
527         u32 vport_idx = 0;
528
529         for (vport_idx = 0; vport_idx < esw->total_vports; vport_idx++) {
530                 struct mlx5_vport *vport = &esw->vports[vport_idx];
531                 struct hlist_head *vport_hash = vport->mc_list;
532                 struct vport_addr *iter_vaddr =
533                                         l2addr_hash_find(vport_hash,
534                                                          mac,
535                                                          struct vport_addr);
536                 if (IS_ERR_OR_NULL(vport->allmulti_rule) ||
537                     vaddr->vport == vport_idx)
538                         continue;
539                 switch (vaddr->action) {
540                 case MLX5_ACTION_ADD:
541                         if (iter_vaddr)
542                                 continue;
543                         iter_vaddr = l2addr_hash_add(vport_hash, mac,
544                                                      struct vport_addr,
545                                                      GFP_KERNEL);
546                         if (!iter_vaddr) {
547                                 esw_warn(esw->dev,
548                                          "ALL-MULTI: Failed to add MAC(%pM) to vport[%d] DB\n",
549                                          mac, vport_idx);
550                                 continue;
551                         }
552                         iter_vaddr->vport = vport_idx;
553                         iter_vaddr->flow_rule =
554                                         esw_fdb_set_vport_rule(esw,
555                                                                mac,
556                                                                vport_idx);
557                         iter_vaddr->mc_promisc = true;
558                         break;
559                 case MLX5_ACTION_DEL:
560                         if (!iter_vaddr)
561                                 continue;
562                         mlx5_del_flow_rules(iter_vaddr->flow_rule);
563                         l2addr_hash_del(iter_vaddr);
564                         break;
565                 }
566         }
567 }
568
569 static int esw_add_mc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
570 {
571         struct hlist_head *hash = esw->mc_table;
572         struct esw_mc_addr *esw_mc;
573         u8 *mac = vaddr->node.addr;
574         u32 vport = vaddr->vport;
575
576         if (!esw->fdb_table.fdb)
577                 return 0;
578
579         esw_mc = l2addr_hash_find(hash, mac, struct esw_mc_addr);
580         if (esw_mc)
581                 goto add;
582
583         esw_mc = l2addr_hash_add(hash, mac, struct esw_mc_addr, GFP_KERNEL);
584         if (!esw_mc)
585                 return -ENOMEM;
586
587         esw_mc->uplink_rule = /* Forward MC MAC to Uplink */
588                 esw_fdb_set_vport_rule(esw, mac, UPLINK_VPORT);
589
590         /* Add this multicast mac to all the mc promiscuous vports */
591         update_allmulti_vports(esw, vaddr, esw_mc);
592
593 add:
594         /* If the multicast mac is added as a result of mc promiscuous vport,
595          * don't increment the multicast ref count
596          */
597         if (!vaddr->mc_promisc)
598                 esw_mc->refcnt++;
599
600         /* Forward MC MAC to vport */
601         vaddr->flow_rule = esw_fdb_set_vport_rule(esw, mac, vport);
602         esw_debug(esw->dev,
603                   "\tADDED MC MAC: vport[%d] %pM fr(%p) refcnt(%d) uplinkfr(%p)\n",
604                   vport, mac, vaddr->flow_rule,
605                   esw_mc->refcnt, esw_mc->uplink_rule);
606         return 0;
607 }
608
609 static int esw_del_mc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
610 {
611         struct hlist_head *hash = esw->mc_table;
612         struct esw_mc_addr *esw_mc;
613         u8 *mac = vaddr->node.addr;
614         u32 vport = vaddr->vport;
615
616         if (!esw->fdb_table.fdb)
617                 return 0;
618
619         esw_mc = l2addr_hash_find(hash, mac, struct esw_mc_addr);
620         if (!esw_mc) {
621                 esw_warn(esw->dev,
622                          "Failed to find eswitch MC addr for MAC(%pM) vport(%d)",
623                          mac, vport);
624                 return -EINVAL;
625         }
626         esw_debug(esw->dev,
627                   "\tDELETE MC MAC: vport[%d] %pM fr(%p) refcnt(%d) uplinkfr(%p)\n",
628                   vport, mac, vaddr->flow_rule, esw_mc->refcnt,
629                   esw_mc->uplink_rule);
630
631         if (vaddr->flow_rule)
632                 mlx5_del_flow_rules(vaddr->flow_rule);
633         vaddr->flow_rule = NULL;
634
635         /* If the multicast mac is added as a result of mc promiscuous vport,
636          * don't decrement the multicast ref count.
637          */
638         if (vaddr->mc_promisc || (--esw_mc->refcnt > 0))
639                 return 0;
640
641         /* Remove this multicast mac from all the mc promiscuous vports */
642         update_allmulti_vports(esw, vaddr, esw_mc);
643
644         if (esw_mc->uplink_rule)
645                 mlx5_del_flow_rules(esw_mc->uplink_rule);
646
647         l2addr_hash_del(esw_mc);
648         return 0;
649 }
650
651 /* Apply vport UC/MC list to HW l2 table and FDB table */
652 static void esw_apply_vport_addr_list(struct mlx5_eswitch *esw,
653                                       u32 vport_num, int list_type)
654 {
655         struct mlx5_vport *vport = &esw->vports[vport_num];
656         bool is_uc = list_type == MLX5_NVPRT_LIST_TYPE_UC;
657         vport_addr_action vport_addr_add;
658         vport_addr_action vport_addr_del;
659         struct vport_addr *addr;
660         struct l2addr_node *node;
661         struct hlist_head *hash;
662         struct hlist_node *tmp;
663         int hi;
664
665         vport_addr_add = is_uc ? esw_add_uc_addr :
666                                  esw_add_mc_addr;
667         vport_addr_del = is_uc ? esw_del_uc_addr :
668                                  esw_del_mc_addr;
669
670         hash = is_uc ? vport->uc_list : vport->mc_list;
671         for_each_l2hash_node(node, tmp, hash, hi) {
672                 addr = container_of(node, struct vport_addr, node);
673                 switch (addr->action) {
674                 case MLX5_ACTION_ADD:
675                         vport_addr_add(esw, addr);
676                         addr->action = MLX5_ACTION_NONE;
677                         break;
678                 case MLX5_ACTION_DEL:
679                         vport_addr_del(esw, addr);
680                         l2addr_hash_del(addr);
681                         break;
682                 }
683         }
684 }
685
686 /* Sync vport UC/MC list from vport context */
687 static void esw_update_vport_addr_list(struct mlx5_eswitch *esw,
688                                        u32 vport_num, int list_type)
689 {
690         struct mlx5_vport *vport = &esw->vports[vport_num];
691         bool is_uc = list_type == MLX5_NVPRT_LIST_TYPE_UC;
692         u8 (*mac_list)[ETH_ALEN];
693         struct l2addr_node *node;
694         struct vport_addr *addr;
695         struct hlist_head *hash;
696         struct hlist_node *tmp;
697         int size;
698         int err;
699         int hi;
700         int i;
701
702         size = is_uc ? MLX5_MAX_UC_PER_VPORT(esw->dev) :
703                        MLX5_MAX_MC_PER_VPORT(esw->dev);
704
705         mac_list = kcalloc(size, ETH_ALEN, GFP_KERNEL);
706         if (!mac_list)
707                 return;
708
709         hash = is_uc ? vport->uc_list : vport->mc_list;
710
711         for_each_l2hash_node(node, tmp, hash, hi) {
712                 addr = container_of(node, struct vport_addr, node);
713                 addr->action = MLX5_ACTION_DEL;
714         }
715
716         if (!vport->enabled)
717                 goto out;
718
719         err = mlx5_query_nic_vport_mac_list(esw->dev, vport_num, list_type,
720                                             mac_list, &size);
721         if (err)
722                 goto out;
723         esw_debug(esw->dev, "vport[%d] context update %s list size (%d)\n",
724                   vport_num, is_uc ? "UC" : "MC", size);
725
726         for (i = 0; i < size; i++) {
727                 if (is_uc && !is_valid_ether_addr(mac_list[i]))
728                         continue;
729
730                 if (!is_uc && !is_multicast_ether_addr(mac_list[i]))
731                         continue;
732
733                 addr = l2addr_hash_find(hash, mac_list[i], struct vport_addr);
734                 if (addr) {
735                         addr->action = MLX5_ACTION_NONE;
736                         /* If this mac was previously added because of allmulti
737                          * promiscuous rx mode, its now converted to be original
738                          * vport mac.
739                          */
740                         if (addr->mc_promisc) {
741                                 struct esw_mc_addr *esw_mc =
742                                         l2addr_hash_find(esw->mc_table,
743                                                          mac_list[i],
744                                                          struct esw_mc_addr);
745                                 if (!esw_mc) {
746                                         esw_warn(esw->dev,
747                                                  "Failed to MAC(%pM) in mcast DB\n",
748                                                  mac_list[i]);
749                                         continue;
750                                 }
751                                 esw_mc->refcnt++;
752                                 addr->mc_promisc = false;
753                         }
754                         continue;
755                 }
756
757                 addr = l2addr_hash_add(hash, mac_list[i], struct vport_addr,
758                                        GFP_KERNEL);
759                 if (!addr) {
760                         esw_warn(esw->dev,
761                                  "Failed to add MAC(%pM) to vport[%d] DB\n",
762                                  mac_list[i], vport_num);
763                         continue;
764                 }
765                 addr->vport = vport_num;
766                 addr->action = MLX5_ACTION_ADD;
767         }
768 out:
769         kfree(mac_list);
770 }
771
772 /* Sync vport UC/MC list from vport context
773  * Must be called after esw_update_vport_addr_list
774  */
775 static void esw_update_vport_mc_promisc(struct mlx5_eswitch *esw, u32 vport_num)
776 {
777         struct mlx5_vport *vport = &esw->vports[vport_num];
778         struct l2addr_node *node;
779         struct vport_addr *addr;
780         struct hlist_head *hash;
781         struct hlist_node *tmp;
782         int hi;
783
784         hash = vport->mc_list;
785
786         for_each_l2hash_node(node, tmp, esw->mc_table, hi) {
787                 u8 *mac = node->addr;
788
789                 addr = l2addr_hash_find(hash, mac, struct vport_addr);
790                 if (addr) {
791                         if (addr->action == MLX5_ACTION_DEL)
792                                 addr->action = MLX5_ACTION_NONE;
793                         continue;
794                 }
795                 addr = l2addr_hash_add(hash, mac, struct vport_addr,
796                                        GFP_KERNEL);
797                 if (!addr) {
798                         esw_warn(esw->dev,
799                                  "Failed to add allmulti MAC(%pM) to vport[%d] DB\n",
800                                  mac, vport_num);
801                         continue;
802                 }
803                 addr->vport = vport_num;
804                 addr->action = MLX5_ACTION_ADD;
805                 addr->mc_promisc = true;
806         }
807 }
808
809 /* Apply vport rx mode to HW FDB table */
810 static void esw_apply_vport_rx_mode(struct mlx5_eswitch *esw, u32 vport_num,
811                                     bool promisc, bool mc_promisc)
812 {
813         struct esw_mc_addr *allmulti_addr = &esw->mc_promisc;
814         struct mlx5_vport *vport = &esw->vports[vport_num];
815
816         if (IS_ERR_OR_NULL(vport->allmulti_rule) != mc_promisc)
817                 goto promisc;
818
819         if (mc_promisc) {
820                 vport->allmulti_rule =
821                                 esw_fdb_set_vport_allmulti_rule(esw, vport_num);
822                 if (!allmulti_addr->uplink_rule)
823                         allmulti_addr->uplink_rule =
824                                 esw_fdb_set_vport_allmulti_rule(esw,
825                                                                 UPLINK_VPORT);
826                 allmulti_addr->refcnt++;
827         } else if (vport->allmulti_rule) {
828                 mlx5_del_flow_rules(vport->allmulti_rule);
829                 vport->allmulti_rule = NULL;
830
831                 if (--allmulti_addr->refcnt > 0)
832                         goto promisc;
833
834                 if (allmulti_addr->uplink_rule)
835                         mlx5_del_flow_rules(allmulti_addr->uplink_rule);
836                 allmulti_addr->uplink_rule = NULL;
837         }
838
839 promisc:
840         if (IS_ERR_OR_NULL(vport->promisc_rule) != promisc)
841                 return;
842
843         if (promisc) {
844                 vport->promisc_rule = esw_fdb_set_vport_promisc_rule(esw,
845                                                                      vport_num);
846         } else if (vport->promisc_rule) {
847                 mlx5_del_flow_rules(vport->promisc_rule);
848                 vport->promisc_rule = NULL;
849         }
850 }
851
852 /* Sync vport rx mode from vport context */
853 static void esw_update_vport_rx_mode(struct mlx5_eswitch *esw, u32 vport_num)
854 {
855         struct mlx5_vport *vport = &esw->vports[vport_num];
856         int promisc_all = 0;
857         int promisc_uc = 0;
858         int promisc_mc = 0;
859         int err;
860
861         err = mlx5_query_nic_vport_promisc(esw->dev,
862                                            vport_num,
863                                            &promisc_uc,
864                                            &promisc_mc,
865                                            &promisc_all);
866         if (err)
867                 return;
868         esw_debug(esw->dev, "vport[%d] context update rx mode promisc_all=%d, all_multi=%d\n",
869                   vport_num, promisc_all, promisc_mc);
870
871         if (!vport->info.trusted || !vport->enabled) {
872                 promisc_uc = 0;
873                 promisc_mc = 0;
874                 promisc_all = 0;
875         }
876
877         esw_apply_vport_rx_mode(esw, vport_num, promisc_all,
878                                 (promisc_all || promisc_mc));
879 }
880
881 static void esw_vport_change_handle_locked(struct mlx5_vport *vport)
882 {
883         struct mlx5_core_dev *dev = vport->dev;
884         struct mlx5_eswitch *esw = dev->priv.eswitch;
885         u8 mac[ETH_ALEN];
886
887         mlx5_query_nic_vport_mac_address(dev, vport->vport, mac);
888         esw_debug(dev, "vport[%d] Context Changed: perm mac: %pM\n",
889                   vport->vport, mac);
890
891         if (vport->enabled_events & UC_ADDR_CHANGE) {
892                 esw_update_vport_addr_list(esw, vport->vport,
893                                            MLX5_NVPRT_LIST_TYPE_UC);
894                 esw_apply_vport_addr_list(esw, vport->vport,
895                                           MLX5_NVPRT_LIST_TYPE_UC);
896         }
897
898         if (vport->enabled_events & MC_ADDR_CHANGE) {
899                 esw_update_vport_addr_list(esw, vport->vport,
900                                            MLX5_NVPRT_LIST_TYPE_MC);
901         }
902
903         if (vport->enabled_events & PROMISC_CHANGE) {
904                 esw_update_vport_rx_mode(esw, vport->vport);
905                 if (!IS_ERR_OR_NULL(vport->allmulti_rule))
906                         esw_update_vport_mc_promisc(esw, vport->vport);
907         }
908
909         if (vport->enabled_events & (PROMISC_CHANGE | MC_ADDR_CHANGE)) {
910                 esw_apply_vport_addr_list(esw, vport->vport,
911                                           MLX5_NVPRT_LIST_TYPE_MC);
912         }
913
914         esw_debug(esw->dev, "vport[%d] Context Changed: Done\n", vport->vport);
915         if (vport->enabled)
916                 arm_vport_context_events_cmd(dev, vport->vport,
917                                              vport->enabled_events);
918 }
919
920 static void esw_vport_change_handler(struct work_struct *work)
921 {
922         struct mlx5_vport *vport =
923                 container_of(work, struct mlx5_vport, vport_change_handler);
924         struct mlx5_eswitch *esw = vport->dev->priv.eswitch;
925
926         mutex_lock(&esw->state_lock);
927         esw_vport_change_handle_locked(vport);
928         mutex_unlock(&esw->state_lock);
929 }
930
931 static int esw_vport_enable_egress_acl(struct mlx5_eswitch *esw,
932                                        struct mlx5_vport *vport)
933 {
934         int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
935         struct mlx5_flow_group *vlan_grp = NULL;
936         struct mlx5_flow_group *drop_grp = NULL;
937         struct mlx5_core_dev *dev = esw->dev;
938         struct mlx5_flow_namespace *root_ns;
939         struct mlx5_flow_table *acl;
940         void *match_criteria;
941         u32 *flow_group_in;
942         /* The egress acl table contains 2 rules:
943          * 1)Allow traffic with vlan_tag=vst_vlan_id
944          * 2)Drop all other traffic.
945          */
946         int table_size = 2;
947         int err = 0;
948
949         if (!MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support))
950                 return -EOPNOTSUPP;
951
952         if (!IS_ERR_OR_NULL(vport->egress.acl))
953                 return 0;
954
955         esw_debug(dev, "Create vport[%d] egress ACL log_max_size(%d)\n",
956                   vport->vport, MLX5_CAP_ESW_EGRESS_ACL(dev, log_max_ft_size));
957
958         root_ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_ESW_EGRESS);
959         if (!root_ns) {
960                 esw_warn(dev, "Failed to get E-Switch egress flow namespace\n");
961                 return -EOPNOTSUPP;
962         }
963
964         flow_group_in = mlx5_vzalloc(inlen);
965         if (!flow_group_in)
966                 return -ENOMEM;
967
968         acl = mlx5_create_vport_flow_table(root_ns, 0, table_size, 0, vport->vport);
969         if (IS_ERR(acl)) {
970                 err = PTR_ERR(acl);
971                 esw_warn(dev, "Failed to create E-Switch vport[%d] egress flow Table, err(%d)\n",
972                          vport->vport, err);
973                 goto out;
974         }
975
976         MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable, MLX5_MATCH_OUTER_HEADERS);
977         match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in, match_criteria);
978         MLX5_SET_TO_ONES(fte_match_param, match_criteria, outer_headers.cvlan_tag);
979         MLX5_SET_TO_ONES(fte_match_param, match_criteria, outer_headers.first_vid);
980         MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
981         MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, 0);
982
983         vlan_grp = mlx5_create_flow_group(acl, flow_group_in);
984         if (IS_ERR(vlan_grp)) {
985                 err = PTR_ERR(vlan_grp);
986                 esw_warn(dev, "Failed to create E-Switch vport[%d] egress allowed vlans flow group, err(%d)\n",
987                          vport->vport, err);
988                 goto out;
989         }
990
991         memset(flow_group_in, 0, inlen);
992         MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 1);
993         MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, 1);
994         drop_grp = mlx5_create_flow_group(acl, flow_group_in);
995         if (IS_ERR(drop_grp)) {
996                 err = PTR_ERR(drop_grp);
997                 esw_warn(dev, "Failed to create E-Switch vport[%d] egress drop flow group, err(%d)\n",
998                          vport->vport, err);
999                 goto out;
1000         }
1001
1002         vport->egress.acl = acl;
1003         vport->egress.drop_grp = drop_grp;
1004         vport->egress.allowed_vlans_grp = vlan_grp;
1005 out:
1006         kvfree(flow_group_in);
1007         if (err && !IS_ERR_OR_NULL(vlan_grp))
1008                 mlx5_destroy_flow_group(vlan_grp);
1009         if (err && !IS_ERR_OR_NULL(acl))
1010                 mlx5_destroy_flow_table(acl);
1011         return err;
1012 }
1013
1014 static void esw_vport_cleanup_egress_rules(struct mlx5_eswitch *esw,
1015                                            struct mlx5_vport *vport)
1016 {
1017         if (!IS_ERR_OR_NULL(vport->egress.allowed_vlan))
1018                 mlx5_del_flow_rules(vport->egress.allowed_vlan);
1019
1020         if (!IS_ERR_OR_NULL(vport->egress.drop_rule))
1021                 mlx5_del_flow_rules(vport->egress.drop_rule);
1022
1023         vport->egress.allowed_vlan = NULL;
1024         vport->egress.drop_rule = NULL;
1025 }
1026
1027 static void esw_vport_disable_egress_acl(struct mlx5_eswitch *esw,
1028                                          struct mlx5_vport *vport)
1029 {
1030         if (IS_ERR_OR_NULL(vport->egress.acl))
1031                 return;
1032
1033         esw_debug(esw->dev, "Destroy vport[%d] E-Switch egress ACL\n", vport->vport);
1034
1035         esw_vport_cleanup_egress_rules(esw, vport);
1036         mlx5_destroy_flow_group(vport->egress.allowed_vlans_grp);
1037         mlx5_destroy_flow_group(vport->egress.drop_grp);
1038         mlx5_destroy_flow_table(vport->egress.acl);
1039         vport->egress.allowed_vlans_grp = NULL;
1040         vport->egress.drop_grp = NULL;
1041         vport->egress.acl = NULL;
1042 }
1043
1044 static int esw_vport_enable_ingress_acl(struct mlx5_eswitch *esw,
1045                                         struct mlx5_vport *vport)
1046 {
1047         int inlen = MLX5_ST_SZ_BYTES(create_flow_group_in);
1048         struct mlx5_core_dev *dev = esw->dev;
1049         struct mlx5_flow_namespace *root_ns;
1050         struct mlx5_flow_table *acl;
1051         struct mlx5_flow_group *g;
1052         void *match_criteria;
1053         u32 *flow_group_in;
1054         /* The ingress acl table contains 4 groups
1055          * (2 active rules at the same time -
1056          *      1 allow rule from one of the first 3 groups.
1057          *      1 drop rule from the last group):
1058          * 1)Allow untagged traffic with smac=original mac.
1059          * 2)Allow untagged traffic.
1060          * 3)Allow traffic with smac=original mac.
1061          * 4)Drop all other traffic.
1062          */
1063         int table_size = 4;
1064         int err = 0;
1065
1066         if (!MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support))
1067                 return -EOPNOTSUPP;
1068
1069         if (!IS_ERR_OR_NULL(vport->ingress.acl))
1070                 return 0;
1071
1072         esw_debug(dev, "Create vport[%d] ingress ACL log_max_size(%d)\n",
1073                   vport->vport, MLX5_CAP_ESW_INGRESS_ACL(dev, log_max_ft_size));
1074
1075         root_ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_ESW_INGRESS);
1076         if (!root_ns) {
1077                 esw_warn(dev, "Failed to get E-Switch ingress flow namespace\n");
1078                 return -EOPNOTSUPP;
1079         }
1080
1081         flow_group_in = mlx5_vzalloc(inlen);
1082         if (!flow_group_in)
1083                 return -ENOMEM;
1084
1085         acl = mlx5_create_vport_flow_table(root_ns, 0, table_size, 0, vport->vport);
1086         if (IS_ERR(acl)) {
1087                 err = PTR_ERR(acl);
1088                 esw_warn(dev, "Failed to create E-Switch vport[%d] ingress flow Table, err(%d)\n",
1089                          vport->vport, err);
1090                 goto out;
1091         }
1092         vport->ingress.acl = acl;
1093
1094         match_criteria = MLX5_ADDR_OF(create_flow_group_in, flow_group_in, match_criteria);
1095
1096         MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable, MLX5_MATCH_OUTER_HEADERS);
1097         MLX5_SET_TO_ONES(fte_match_param, match_criteria, outer_headers.cvlan_tag);
1098         MLX5_SET_TO_ONES(fte_match_param, match_criteria, outer_headers.smac_47_16);
1099         MLX5_SET_TO_ONES(fte_match_param, match_criteria, outer_headers.smac_15_0);
1100         MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 0);
1101         MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, 0);
1102
1103         g = mlx5_create_flow_group(acl, flow_group_in);
1104         if (IS_ERR(g)) {
1105                 err = PTR_ERR(g);
1106                 esw_warn(dev, "Failed to create E-Switch vport[%d] ingress untagged spoofchk flow group, err(%d)\n",
1107                          vport->vport, err);
1108                 goto out;
1109         }
1110         vport->ingress.allow_untagged_spoofchk_grp = g;
1111
1112         memset(flow_group_in, 0, inlen);
1113         MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable, MLX5_MATCH_OUTER_HEADERS);
1114         MLX5_SET_TO_ONES(fte_match_param, match_criteria, outer_headers.cvlan_tag);
1115         MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 1);
1116         MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, 1);
1117
1118         g = mlx5_create_flow_group(acl, flow_group_in);
1119         if (IS_ERR(g)) {
1120                 err = PTR_ERR(g);
1121                 esw_warn(dev, "Failed to create E-Switch vport[%d] ingress untagged flow group, err(%d)\n",
1122                          vport->vport, err);
1123                 goto out;
1124         }
1125         vport->ingress.allow_untagged_only_grp = g;
1126
1127         memset(flow_group_in, 0, inlen);
1128         MLX5_SET(create_flow_group_in, flow_group_in, match_criteria_enable, MLX5_MATCH_OUTER_HEADERS);
1129         MLX5_SET_TO_ONES(fte_match_param, match_criteria, outer_headers.smac_47_16);
1130         MLX5_SET_TO_ONES(fte_match_param, match_criteria, outer_headers.smac_15_0);
1131         MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 2);
1132         MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, 2);
1133
1134         g = mlx5_create_flow_group(acl, flow_group_in);
1135         if (IS_ERR(g)) {
1136                 err = PTR_ERR(g);
1137                 esw_warn(dev, "Failed to create E-Switch vport[%d] ingress spoofchk flow group, err(%d)\n",
1138                          vport->vport, err);
1139                 goto out;
1140         }
1141         vport->ingress.allow_spoofchk_only_grp = g;
1142
1143         memset(flow_group_in, 0, inlen);
1144         MLX5_SET(create_flow_group_in, flow_group_in, start_flow_index, 3);
1145         MLX5_SET(create_flow_group_in, flow_group_in, end_flow_index, 3);
1146
1147         g = mlx5_create_flow_group(acl, flow_group_in);
1148         if (IS_ERR(g)) {
1149                 err = PTR_ERR(g);
1150                 esw_warn(dev, "Failed to create E-Switch vport[%d] ingress drop flow group, err(%d)\n",
1151                          vport->vport, err);
1152                 goto out;
1153         }
1154         vport->ingress.drop_grp = g;
1155
1156 out:
1157         if (err) {
1158                 if (!IS_ERR_OR_NULL(vport->ingress.allow_spoofchk_only_grp))
1159                         mlx5_destroy_flow_group(
1160                                         vport->ingress.allow_spoofchk_only_grp);
1161                 if (!IS_ERR_OR_NULL(vport->ingress.allow_untagged_only_grp))
1162                         mlx5_destroy_flow_group(
1163                                         vport->ingress.allow_untagged_only_grp);
1164                 if (!IS_ERR_OR_NULL(vport->ingress.allow_untagged_spoofchk_grp))
1165                         mlx5_destroy_flow_group(
1166                                 vport->ingress.allow_untagged_spoofchk_grp);
1167                 if (!IS_ERR_OR_NULL(vport->ingress.acl))
1168                         mlx5_destroy_flow_table(vport->ingress.acl);
1169         }
1170
1171         kvfree(flow_group_in);
1172         return err;
1173 }
1174
1175 static void esw_vport_cleanup_ingress_rules(struct mlx5_eswitch *esw,
1176                                             struct mlx5_vport *vport)
1177 {
1178         if (!IS_ERR_OR_NULL(vport->ingress.drop_rule))
1179                 mlx5_del_flow_rules(vport->ingress.drop_rule);
1180
1181         if (!IS_ERR_OR_NULL(vport->ingress.allow_rule))
1182                 mlx5_del_flow_rules(vport->ingress.allow_rule);
1183
1184         vport->ingress.drop_rule = NULL;
1185         vport->ingress.allow_rule = NULL;
1186 }
1187
1188 static void esw_vport_disable_ingress_acl(struct mlx5_eswitch *esw,
1189                                           struct mlx5_vport *vport)
1190 {
1191         if (IS_ERR_OR_NULL(vport->ingress.acl))
1192                 return;
1193
1194         esw_debug(esw->dev, "Destroy vport[%d] E-Switch ingress ACL\n", vport->vport);
1195
1196         esw_vport_cleanup_ingress_rules(esw, vport);
1197         mlx5_destroy_flow_group(vport->ingress.allow_spoofchk_only_grp);
1198         mlx5_destroy_flow_group(vport->ingress.allow_untagged_only_grp);
1199         mlx5_destroy_flow_group(vport->ingress.allow_untagged_spoofchk_grp);
1200         mlx5_destroy_flow_group(vport->ingress.drop_grp);
1201         mlx5_destroy_flow_table(vport->ingress.acl);
1202         vport->ingress.acl = NULL;
1203         vport->ingress.drop_grp = NULL;
1204         vport->ingress.allow_spoofchk_only_grp = NULL;
1205         vport->ingress.allow_untagged_only_grp = NULL;
1206         vport->ingress.allow_untagged_spoofchk_grp = NULL;
1207 }
1208
1209 static int esw_vport_ingress_config(struct mlx5_eswitch *esw,
1210                                     struct mlx5_vport *vport)
1211 {
1212         struct mlx5_flow_act flow_act = {0};
1213         struct mlx5_flow_spec *spec;
1214         int err = 0;
1215         u8 *smac_v;
1216
1217         if (vport->info.spoofchk && !is_valid_ether_addr(vport->info.mac)) {
1218                 mlx5_core_warn(esw->dev,
1219                                "vport[%d] configure ingress rules failed, illegal mac with spoofchk\n",
1220                                vport->vport);
1221                 return -EPERM;
1222
1223         }
1224
1225         esw_vport_cleanup_ingress_rules(esw, vport);
1226
1227         if (!vport->info.vlan && !vport->info.qos && !vport->info.spoofchk) {
1228                 esw_vport_disable_ingress_acl(esw, vport);
1229                 return 0;
1230         }
1231
1232         err = esw_vport_enable_ingress_acl(esw, vport);
1233         if (err) {
1234                 mlx5_core_warn(esw->dev,
1235                                "failed to enable ingress acl (%d) on vport[%d]\n",
1236                                err, vport->vport);
1237                 return err;
1238         }
1239
1240         esw_debug(esw->dev,
1241                   "vport[%d] configure ingress rules, vlan(%d) qos(%d)\n",
1242                   vport->vport, vport->info.vlan, vport->info.qos);
1243
1244         spec = mlx5_vzalloc(sizeof(*spec));
1245         if (!spec) {
1246                 err = -ENOMEM;
1247                 esw_warn(esw->dev, "vport[%d] configure ingress rules failed, err(%d)\n",
1248                          vport->vport, err);
1249                 goto out;
1250         }
1251
1252         if (vport->info.vlan || vport->info.qos)
1253                 MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria, outer_headers.cvlan_tag);
1254
1255         if (vport->info.spoofchk) {
1256                 MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria, outer_headers.smac_47_16);
1257                 MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria, outer_headers.smac_15_0);
1258                 smac_v = MLX5_ADDR_OF(fte_match_param,
1259                                       spec->match_value,
1260                                       outer_headers.smac_47_16);
1261                 ether_addr_copy(smac_v, vport->info.mac);
1262         }
1263
1264         spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
1265         flow_act.action = MLX5_FLOW_CONTEXT_ACTION_ALLOW;
1266         vport->ingress.allow_rule =
1267                 mlx5_add_flow_rules(vport->ingress.acl, spec,
1268                                     &flow_act, NULL, 0);
1269         if (IS_ERR(vport->ingress.allow_rule)) {
1270                 err = PTR_ERR(vport->ingress.allow_rule);
1271                 esw_warn(esw->dev,
1272                          "vport[%d] configure ingress allow rule, err(%d)\n",
1273                          vport->vport, err);
1274                 vport->ingress.allow_rule = NULL;
1275                 goto out;
1276         }
1277
1278         memset(spec, 0, sizeof(*spec));
1279         flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
1280         vport->ingress.drop_rule =
1281                 mlx5_add_flow_rules(vport->ingress.acl, spec,
1282                                     &flow_act, NULL, 0);
1283         if (IS_ERR(vport->ingress.drop_rule)) {
1284                 err = PTR_ERR(vport->ingress.drop_rule);
1285                 esw_warn(esw->dev,
1286                          "vport[%d] configure ingress drop rule, err(%d)\n",
1287                          vport->vport, err);
1288                 vport->ingress.drop_rule = NULL;
1289                 goto out;
1290         }
1291
1292 out:
1293         if (err)
1294                 esw_vport_cleanup_ingress_rules(esw, vport);
1295         kvfree(spec);
1296         return err;
1297 }
1298
1299 static int esw_vport_egress_config(struct mlx5_eswitch *esw,
1300                                    struct mlx5_vport *vport)
1301 {
1302         struct mlx5_flow_act flow_act = {0};
1303         struct mlx5_flow_spec *spec;
1304         int err = 0;
1305
1306         esw_vport_cleanup_egress_rules(esw, vport);
1307
1308         if (!vport->info.vlan && !vport->info.qos) {
1309                 esw_vport_disable_egress_acl(esw, vport);
1310                 return 0;
1311         }
1312
1313         err = esw_vport_enable_egress_acl(esw, vport);
1314         if (err) {
1315                 mlx5_core_warn(esw->dev,
1316                                "failed to enable egress acl (%d) on vport[%d]\n",
1317                                err, vport->vport);
1318                 return err;
1319         }
1320
1321         esw_debug(esw->dev,
1322                   "vport[%d] configure egress rules, vlan(%d) qos(%d)\n",
1323                   vport->vport, vport->info.vlan, vport->info.qos);
1324
1325         spec = mlx5_vzalloc(sizeof(*spec));
1326         if (!spec) {
1327                 err = -ENOMEM;
1328                 esw_warn(esw->dev, "vport[%d] configure egress rules failed, err(%d)\n",
1329                          vport->vport, err);
1330                 goto out;
1331         }
1332
1333         /* Allowed vlan rule */
1334         MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria, outer_headers.cvlan_tag);
1335         MLX5_SET_TO_ONES(fte_match_param, spec->match_value, outer_headers.cvlan_tag);
1336         MLX5_SET_TO_ONES(fte_match_param, spec->match_criteria, outer_headers.first_vid);
1337         MLX5_SET(fte_match_param, spec->match_value, outer_headers.first_vid, vport->info.vlan);
1338
1339         spec->match_criteria_enable = MLX5_MATCH_OUTER_HEADERS;
1340         flow_act.action = MLX5_FLOW_CONTEXT_ACTION_ALLOW;
1341         vport->egress.allowed_vlan =
1342                 mlx5_add_flow_rules(vport->egress.acl, spec,
1343                                     &flow_act, NULL, 0);
1344         if (IS_ERR(vport->egress.allowed_vlan)) {
1345                 err = PTR_ERR(vport->egress.allowed_vlan);
1346                 esw_warn(esw->dev,
1347                          "vport[%d] configure egress allowed vlan rule failed, err(%d)\n",
1348                          vport->vport, err);
1349                 vport->egress.allowed_vlan = NULL;
1350                 goto out;
1351         }
1352
1353         /* Drop others rule (star rule) */
1354         memset(spec, 0, sizeof(*spec));
1355         flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
1356         vport->egress.drop_rule =
1357                 mlx5_add_flow_rules(vport->egress.acl, spec,
1358                                     &flow_act, NULL, 0);
1359         if (IS_ERR(vport->egress.drop_rule)) {
1360                 err = PTR_ERR(vport->egress.drop_rule);
1361                 esw_warn(esw->dev,
1362                          "vport[%d] configure egress drop rule failed, err(%d)\n",
1363                          vport->vport, err);
1364                 vport->egress.drop_rule = NULL;
1365         }
1366 out:
1367         kvfree(spec);
1368         return err;
1369 }
1370
1371 /* Vport QoS management */
1372 static int esw_create_tsar(struct mlx5_eswitch *esw)
1373 {
1374         u32 tsar_ctx[MLX5_ST_SZ_DW(scheduling_context)] = {0};
1375         struct mlx5_core_dev *dev = esw->dev;
1376         int err;
1377
1378         if (!MLX5_CAP_GEN(dev, qos) || !MLX5_CAP_QOS(dev, esw_scheduling))
1379                 return 0;
1380
1381         if (esw->qos.enabled)
1382                 return -EEXIST;
1383
1384         err = mlx5_create_scheduling_element_cmd(dev,
1385                                                  SCHEDULING_HIERARCHY_E_SWITCH,
1386                                                  &tsar_ctx,
1387                                                  &esw->qos.root_tsar_id);
1388         if (err) {
1389                 esw_warn(esw->dev, "E-Switch create TSAR failed (%d)\n", err);
1390                 return err;
1391         }
1392
1393         esw->qos.enabled = true;
1394         return 0;
1395 }
1396
1397 static void esw_destroy_tsar(struct mlx5_eswitch *esw)
1398 {
1399         int err;
1400
1401         if (!esw->qos.enabled)
1402                 return;
1403
1404         err = mlx5_destroy_scheduling_element_cmd(esw->dev,
1405                                                   SCHEDULING_HIERARCHY_E_SWITCH,
1406                                                   esw->qos.root_tsar_id);
1407         if (err)
1408                 esw_warn(esw->dev, "E-Switch destroy TSAR failed (%d)\n", err);
1409
1410         esw->qos.enabled = false;
1411 }
1412
1413 static int esw_vport_enable_qos(struct mlx5_eswitch *esw, int vport_num,
1414                                 u32 initial_max_rate, u32 initial_bw_share)
1415 {
1416         u32 sched_ctx[MLX5_ST_SZ_DW(scheduling_context)] = {0};
1417         struct mlx5_vport *vport = &esw->vports[vport_num];
1418         struct mlx5_core_dev *dev = esw->dev;
1419         void *vport_elem;
1420         int err = 0;
1421
1422         if (!esw->qos.enabled || !MLX5_CAP_GEN(dev, qos) ||
1423             !MLX5_CAP_QOS(dev, esw_scheduling))
1424                 return 0;
1425
1426         if (vport->qos.enabled)
1427                 return -EEXIST;
1428
1429         MLX5_SET(scheduling_context, &sched_ctx, element_type,
1430                  SCHEDULING_CONTEXT_ELEMENT_TYPE_VPORT);
1431         vport_elem = MLX5_ADDR_OF(scheduling_context, &sched_ctx,
1432                                   element_attributes);
1433         MLX5_SET(vport_element, vport_elem, vport_number, vport_num);
1434         MLX5_SET(scheduling_context, &sched_ctx, parent_element_id,
1435                  esw->qos.root_tsar_id);
1436         MLX5_SET(scheduling_context, &sched_ctx, max_average_bw,
1437                  initial_max_rate);
1438         MLX5_SET(scheduling_context, &sched_ctx, bw_share, initial_bw_share);
1439
1440         err = mlx5_create_scheduling_element_cmd(dev,
1441                                                  SCHEDULING_HIERARCHY_E_SWITCH,
1442                                                  &sched_ctx,
1443                                                  &vport->qos.esw_tsar_ix);
1444         if (err) {
1445                 esw_warn(esw->dev, "E-Switch create TSAR vport element failed (vport=%d,err=%d)\n",
1446                          vport_num, err);
1447                 return err;
1448         }
1449
1450         vport->qos.enabled = true;
1451         return 0;
1452 }
1453
1454 static void esw_vport_disable_qos(struct mlx5_eswitch *esw, int vport_num)
1455 {
1456         struct mlx5_vport *vport = &esw->vports[vport_num];
1457         int err = 0;
1458
1459         if (!vport->qos.enabled)
1460                 return;
1461
1462         err = mlx5_destroy_scheduling_element_cmd(esw->dev,
1463                                                   SCHEDULING_HIERARCHY_E_SWITCH,
1464                                                   vport->qos.esw_tsar_ix);
1465         if (err)
1466                 esw_warn(esw->dev, "E-Switch destroy TSAR vport element failed (vport=%d,err=%d)\n",
1467                          vport_num, err);
1468
1469         vport->qos.enabled = false;
1470 }
1471
1472 static int esw_vport_qos_config(struct mlx5_eswitch *esw, int vport_num,
1473                                 u32 max_rate, u32 bw_share)
1474 {
1475         u32 sched_ctx[MLX5_ST_SZ_DW(scheduling_context)] = {0};
1476         struct mlx5_vport *vport = &esw->vports[vport_num];
1477         struct mlx5_core_dev *dev = esw->dev;
1478         void *vport_elem;
1479         u32 bitmask = 0;
1480         int err = 0;
1481
1482         if (!MLX5_CAP_GEN(dev, qos) || !MLX5_CAP_QOS(dev, esw_scheduling))
1483                 return -EOPNOTSUPP;
1484
1485         if (!vport->qos.enabled)
1486                 return -EIO;
1487
1488         MLX5_SET(scheduling_context, &sched_ctx, element_type,
1489                  SCHEDULING_CONTEXT_ELEMENT_TYPE_VPORT);
1490         vport_elem = MLX5_ADDR_OF(scheduling_context, &sched_ctx,
1491                                   element_attributes);
1492         MLX5_SET(vport_element, vport_elem, vport_number, vport_num);
1493         MLX5_SET(scheduling_context, &sched_ctx, parent_element_id,
1494                  esw->qos.root_tsar_id);
1495         MLX5_SET(scheduling_context, &sched_ctx, max_average_bw,
1496                  max_rate);
1497         MLX5_SET(scheduling_context, &sched_ctx, bw_share, bw_share);
1498         bitmask |= MODIFY_SCHEDULING_ELEMENT_IN_MODIFY_BITMASK_MAX_AVERAGE_BW;
1499         bitmask |= MODIFY_SCHEDULING_ELEMENT_IN_MODIFY_BITMASK_BW_SHARE;
1500
1501         err = mlx5_modify_scheduling_element_cmd(dev,
1502                                                  SCHEDULING_HIERARCHY_E_SWITCH,
1503                                                  &sched_ctx,
1504                                                  vport->qos.esw_tsar_ix,
1505                                                  bitmask);
1506         if (err) {
1507                 esw_warn(esw->dev, "E-Switch modify TSAR vport element failed (vport=%d,err=%d)\n",
1508                          vport_num, err);
1509                 return err;
1510         }
1511
1512         return 0;
1513 }
1514
1515 static void node_guid_gen_from_mac(u64 *node_guid, u8 mac[ETH_ALEN])
1516 {
1517         ((u8 *)node_guid)[7] = mac[0];
1518         ((u8 *)node_guid)[6] = mac[1];
1519         ((u8 *)node_guid)[5] = mac[2];
1520         ((u8 *)node_guid)[4] = 0xff;
1521         ((u8 *)node_guid)[3] = 0xfe;
1522         ((u8 *)node_guid)[2] = mac[3];
1523         ((u8 *)node_guid)[1] = mac[4];
1524         ((u8 *)node_guid)[0] = mac[5];
1525 }
1526
1527 static void esw_apply_vport_conf(struct mlx5_eswitch *esw,
1528                                  struct mlx5_vport *vport)
1529 {
1530         int vport_num = vport->vport;
1531
1532         if (!vport_num)
1533                 return;
1534
1535         mlx5_modify_vport_admin_state(esw->dev,
1536                                       MLX5_QUERY_VPORT_STATE_IN_OP_MOD_ESW_VPORT,
1537                                       vport_num,
1538                                       vport->info.link_state);
1539         mlx5_modify_nic_vport_mac_address(esw->dev, vport_num, vport->info.mac);
1540         mlx5_modify_nic_vport_node_guid(esw->dev, vport_num, vport->info.node_guid);
1541         modify_esw_vport_cvlan(esw->dev, vport_num, vport->info.vlan, vport->info.qos,
1542                                (vport->info.vlan || vport->info.qos));
1543
1544         /* Only legacy mode needs ACLs */
1545         if (esw->mode == SRIOV_LEGACY) {
1546                 esw_vport_ingress_config(esw, vport);
1547                 esw_vport_egress_config(esw, vport);
1548         }
1549 }
1550
1551 static void esw_enable_vport(struct mlx5_eswitch *esw, int vport_num,
1552                              int enable_events)
1553 {
1554         struct mlx5_vport *vport = &esw->vports[vport_num];
1555
1556         mutex_lock(&esw->state_lock);
1557         WARN_ON(vport->enabled);
1558
1559         esw_debug(esw->dev, "Enabling VPORT(%d)\n", vport_num);
1560
1561         /* Restore old vport configuration */
1562         esw_apply_vport_conf(esw, vport);
1563
1564         /* Attach vport to the eswitch rate limiter */
1565         if (esw_vport_enable_qos(esw, vport_num, vport->info.max_rate,
1566                                  vport->qos.bw_share))
1567                 esw_warn(esw->dev, "Failed to attach vport %d to eswitch rate limiter", vport_num);
1568
1569         /* Sync with current vport context */
1570         vport->enabled_events = enable_events;
1571         vport->enabled = true;
1572
1573         /* only PF is trusted by default */
1574         if (!vport_num)
1575                 vport->info.trusted = true;
1576
1577         esw_vport_change_handle_locked(vport);
1578
1579         esw->enabled_vports++;
1580         esw_debug(esw->dev, "Enabled VPORT(%d)\n", vport_num);
1581         mutex_unlock(&esw->state_lock);
1582 }
1583
1584 static void esw_disable_vport(struct mlx5_eswitch *esw, int vport_num)
1585 {
1586         struct mlx5_vport *vport = &esw->vports[vport_num];
1587
1588         if (!vport->enabled)
1589                 return;
1590
1591         esw_debug(esw->dev, "Disabling vport(%d)\n", vport_num);
1592         /* Mark this vport as disabled to discard new events */
1593         vport->enabled = false;
1594
1595         synchronize_irq(mlx5_get_msix_vec(esw->dev, MLX5_EQ_VEC_ASYNC));
1596         /* Wait for current already scheduled events to complete */
1597         flush_workqueue(esw->work_queue);
1598         /* Disable events from this vport */
1599         arm_vport_context_events_cmd(esw->dev, vport->vport, 0);
1600         mutex_lock(&esw->state_lock);
1601         /* We don't assume VFs will cleanup after themselves.
1602          * Calling vport change handler while vport is disabled will cleanup
1603          * the vport resources.
1604          */
1605         esw_vport_change_handle_locked(vport);
1606         vport->enabled_events = 0;
1607         esw_vport_disable_qos(esw, vport_num);
1608         if (vport_num && esw->mode == SRIOV_LEGACY) {
1609                 mlx5_modify_vport_admin_state(esw->dev,
1610                                               MLX5_QUERY_VPORT_STATE_IN_OP_MOD_ESW_VPORT,
1611                                               vport_num,
1612                                               MLX5_ESW_VPORT_ADMIN_STATE_DOWN);
1613                 esw_vport_disable_egress_acl(esw, vport);
1614                 esw_vport_disable_ingress_acl(esw, vport);
1615         }
1616         esw->enabled_vports--;
1617         mutex_unlock(&esw->state_lock);
1618 }
1619
1620 /* Public E-Switch API */
1621 int mlx5_eswitch_enable_sriov(struct mlx5_eswitch *esw, int nvfs, int mode)
1622 {
1623         int err;
1624         int i, enabled_events;
1625
1626         if (!esw || !MLX5_CAP_GEN(esw->dev, vport_group_manager) ||
1627             MLX5_CAP_GEN(esw->dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
1628                 return 0;
1629
1630         if (!MLX5_CAP_GEN(esw->dev, eswitch_flow_table) ||
1631             !MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, ft_support)) {
1632                 esw_warn(esw->dev, "E-Switch FDB is not supported, aborting ...\n");
1633                 return -EOPNOTSUPP;
1634         }
1635
1636         if (!MLX5_CAP_ESW_INGRESS_ACL(esw->dev, ft_support))
1637                 esw_warn(esw->dev, "E-Switch ingress ACL is not supported by FW\n");
1638
1639         if (!MLX5_CAP_ESW_EGRESS_ACL(esw->dev, ft_support))
1640                 esw_warn(esw->dev, "E-Switch engress ACL is not supported by FW\n");
1641
1642         esw_info(esw->dev, "E-Switch enable SRIOV: nvfs(%d) mode (%d)\n", nvfs, mode);
1643         esw->mode = mode;
1644         esw_disable_vport(esw, 0);
1645
1646         if (mode == SRIOV_LEGACY)
1647                 err = esw_create_legacy_fdb_table(esw, nvfs + 1);
1648         else
1649                 err = esw_offloads_init(esw, nvfs + 1);
1650         if (err)
1651                 goto abort;
1652
1653         err = esw_create_tsar(esw);
1654         if (err)
1655                 esw_warn(esw->dev, "Failed to create eswitch TSAR");
1656
1657         enabled_events = (mode == SRIOV_LEGACY) ? SRIOV_VPORT_EVENTS : UC_ADDR_CHANGE;
1658         for (i = 0; i <= nvfs; i++)
1659                 esw_enable_vport(esw, i, enabled_events);
1660
1661         esw_info(esw->dev, "SRIOV enabled: active vports(%d)\n",
1662                  esw->enabled_vports);
1663         return 0;
1664
1665 abort:
1666         esw_enable_vport(esw, 0, UC_ADDR_CHANGE);
1667         esw->mode = SRIOV_NONE;
1668         return err;
1669 }
1670
1671 void mlx5_eswitch_disable_sriov(struct mlx5_eswitch *esw)
1672 {
1673         struct esw_mc_addr *mc_promisc;
1674         int nvports;
1675         int i;
1676
1677         if (!esw || !MLX5_CAP_GEN(esw->dev, vport_group_manager) ||
1678             MLX5_CAP_GEN(esw->dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
1679                 return;
1680
1681         esw_info(esw->dev, "disable SRIOV: active vports(%d) mode(%d)\n",
1682                  esw->enabled_vports, esw->mode);
1683
1684         mc_promisc = &esw->mc_promisc;
1685         nvports = esw->enabled_vports;
1686
1687         for (i = 0; i < esw->total_vports; i++)
1688                 esw_disable_vport(esw, i);
1689
1690         if (mc_promisc && mc_promisc->uplink_rule)
1691                 mlx5_del_flow_rules(mc_promisc->uplink_rule);
1692
1693         esw_destroy_tsar(esw);
1694
1695         if (esw->mode == SRIOV_LEGACY)
1696                 esw_destroy_legacy_fdb_table(esw);
1697         else if (esw->mode == SRIOV_OFFLOADS)
1698                 esw_offloads_cleanup(esw, nvports);
1699
1700         esw->mode = SRIOV_NONE;
1701         /* VPORT 0 (PF) must be enabled back with non-sriov configuration */
1702         esw_enable_vport(esw, 0, UC_ADDR_CHANGE);
1703 }
1704
1705 void mlx5_eswitch_attach(struct mlx5_eswitch *esw)
1706 {
1707         if (!esw || !MLX5_CAP_GEN(esw->dev, vport_group_manager) ||
1708             MLX5_CAP_GEN(esw->dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
1709                 return;
1710
1711         esw_enable_vport(esw, 0, UC_ADDR_CHANGE);
1712         /* VF Vports will be enabled when SRIOV is enabled */
1713 }
1714
1715 void mlx5_eswitch_detach(struct mlx5_eswitch *esw)
1716 {
1717         if (!esw || !MLX5_CAP_GEN(esw->dev, vport_group_manager) ||
1718             MLX5_CAP_GEN(esw->dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
1719                 return;
1720
1721         esw_disable_vport(esw, 0);
1722 }
1723
1724 int mlx5_eswitch_init(struct mlx5_core_dev *dev)
1725 {
1726         int l2_table_size = 1 << MLX5_CAP_GEN(dev, log_max_l2_table);
1727         int total_vports = MLX5_TOTAL_VPORTS(dev);
1728         struct mlx5_eswitch *esw;
1729         int vport_num;
1730         int err;
1731
1732         if (!MLX5_CAP_GEN(dev, vport_group_manager) ||
1733             MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
1734                 return 0;
1735
1736         esw_info(dev,
1737                  "Total vports %d, l2 table size(%d), per vport: max uc(%d) max mc(%d)\n",
1738                  total_vports, l2_table_size,
1739                  MLX5_MAX_UC_PER_VPORT(dev),
1740                  MLX5_MAX_MC_PER_VPORT(dev));
1741
1742         esw = kzalloc(sizeof(*esw), GFP_KERNEL);
1743         if (!esw)
1744                 return -ENOMEM;
1745
1746         esw->dev = dev;
1747
1748         esw->l2_table.bitmap = kcalloc(BITS_TO_LONGS(l2_table_size),
1749                                    sizeof(uintptr_t), GFP_KERNEL);
1750         if (!esw->l2_table.bitmap) {
1751                 err = -ENOMEM;
1752                 goto abort;
1753         }
1754         esw->l2_table.size = l2_table_size;
1755
1756         esw->work_queue = create_singlethread_workqueue("mlx5_esw_wq");
1757         if (!esw->work_queue) {
1758                 err = -ENOMEM;
1759                 goto abort;
1760         }
1761
1762         esw->vports = kcalloc(total_vports, sizeof(struct mlx5_vport),
1763                               GFP_KERNEL);
1764         if (!esw->vports) {
1765                 err = -ENOMEM;
1766                 goto abort;
1767         }
1768
1769         esw->offloads.vport_reps =
1770                 kzalloc(total_vports * sizeof(struct mlx5_eswitch_rep),
1771                         GFP_KERNEL);
1772         if (!esw->offloads.vport_reps) {
1773                 err = -ENOMEM;
1774                 goto abort;
1775         }
1776
1777         hash_init(esw->offloads.encap_tbl);
1778         mutex_init(&esw->state_lock);
1779
1780         for (vport_num = 0; vport_num < total_vports; vport_num++) {
1781                 struct mlx5_vport *vport = &esw->vports[vport_num];
1782
1783                 vport->vport = vport_num;
1784                 vport->info.link_state = MLX5_ESW_VPORT_ADMIN_STATE_AUTO;
1785                 vport->dev = dev;
1786                 INIT_WORK(&vport->vport_change_handler,
1787                           esw_vport_change_handler);
1788         }
1789
1790         esw->total_vports = total_vports;
1791         esw->enabled_vports = 0;
1792         esw->mode = SRIOV_NONE;
1793         esw->offloads.inline_mode = MLX5_INLINE_MODE_NONE;
1794         if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, encap) &&
1795             MLX5_CAP_ESW_FLOWTABLE_FDB(dev, decap))
1796                 esw->offloads.encap = DEVLINK_ESWITCH_ENCAP_MODE_BASIC;
1797         else
1798                 esw->offloads.encap = DEVLINK_ESWITCH_ENCAP_MODE_NONE;
1799
1800         dev->priv.eswitch = esw;
1801         return 0;
1802 abort:
1803         if (esw->work_queue)
1804                 destroy_workqueue(esw->work_queue);
1805         kfree(esw->l2_table.bitmap);
1806         kfree(esw->vports);
1807         kfree(esw->offloads.vport_reps);
1808         kfree(esw);
1809         return err;
1810 }
1811
1812 void mlx5_eswitch_cleanup(struct mlx5_eswitch *esw)
1813 {
1814         if (!esw || !MLX5_CAP_GEN(esw->dev, vport_group_manager) ||
1815             MLX5_CAP_GEN(esw->dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
1816                 return;
1817
1818         esw_info(esw->dev, "cleanup\n");
1819
1820         esw->dev->priv.eswitch = NULL;
1821         destroy_workqueue(esw->work_queue);
1822         kfree(esw->l2_table.bitmap);
1823         kfree(esw->offloads.vport_reps);
1824         kfree(esw->vports);
1825         kfree(esw);
1826 }
1827
1828 void mlx5_eswitch_vport_event(struct mlx5_eswitch *esw, struct mlx5_eqe *eqe)
1829 {
1830         struct mlx5_eqe_vport_change *vc_eqe = &eqe->data.vport_change;
1831         u16 vport_num = be16_to_cpu(vc_eqe->vport_num);
1832         struct mlx5_vport *vport;
1833
1834         if (!esw) {
1835                 pr_warn("MLX5 E-Switch: vport %d got an event while eswitch is not initialized\n",
1836                         vport_num);
1837                 return;
1838         }
1839
1840         vport = &esw->vports[vport_num];
1841         if (vport->enabled)
1842                 queue_work(esw->work_queue, &vport->vport_change_handler);
1843 }
1844
1845 /* Vport Administration */
1846 #define ESW_ALLOWED(esw) \
1847         (esw && MLX5_CAP_GEN(esw->dev, vport_group_manager) && mlx5_core_is_pf(esw->dev))
1848 #define LEGAL_VPORT(esw, vport) (vport >= 0 && vport < esw->total_vports)
1849
1850 int mlx5_eswitch_set_vport_mac(struct mlx5_eswitch *esw,
1851                                int vport, u8 mac[ETH_ALEN])
1852 {
1853         struct mlx5_vport *evport;
1854         u64 node_guid;
1855         int err = 0;
1856
1857         if (!ESW_ALLOWED(esw))
1858                 return -EPERM;
1859         if (!LEGAL_VPORT(esw, vport) || is_multicast_ether_addr(mac))
1860                 return -EINVAL;
1861
1862         mutex_lock(&esw->state_lock);
1863         evport = &esw->vports[vport];
1864
1865         if (evport->info.spoofchk && !is_valid_ether_addr(mac)) {
1866                 mlx5_core_warn(esw->dev,
1867                                "MAC invalidation is not allowed when spoofchk is on, vport(%d)\n",
1868                                vport);
1869                 err = -EPERM;
1870                 goto unlock;
1871         }
1872
1873         err = mlx5_modify_nic_vport_mac_address(esw->dev, vport, mac);
1874         if (err) {
1875                 mlx5_core_warn(esw->dev,
1876                                "Failed to mlx5_modify_nic_vport_mac vport(%d) err=(%d)\n",
1877                                vport, err);
1878                 goto unlock;
1879         }
1880
1881         node_guid_gen_from_mac(&node_guid, mac);
1882         err = mlx5_modify_nic_vport_node_guid(esw->dev, vport, node_guid);
1883         if (err)
1884                 mlx5_core_warn(esw->dev,
1885                                "Failed to set vport %d node guid, err = %d. RDMA_CM will not function properly for this VF.\n",
1886                                vport, err);
1887
1888         ether_addr_copy(evport->info.mac, mac);
1889         evport->info.node_guid = node_guid;
1890         if (evport->enabled && esw->mode == SRIOV_LEGACY)
1891                 err = esw_vport_ingress_config(esw, evport);
1892
1893 unlock:
1894         mutex_unlock(&esw->state_lock);
1895         return err;
1896 }
1897
1898 int mlx5_eswitch_set_vport_state(struct mlx5_eswitch *esw,
1899                                  int vport, int link_state)
1900 {
1901         struct mlx5_vport *evport;
1902         int err = 0;
1903
1904         if (!ESW_ALLOWED(esw))
1905                 return -EPERM;
1906         if (!LEGAL_VPORT(esw, vport))
1907                 return -EINVAL;
1908
1909         mutex_lock(&esw->state_lock);
1910         evport = &esw->vports[vport];
1911
1912         err = mlx5_modify_vport_admin_state(esw->dev,
1913                                             MLX5_QUERY_VPORT_STATE_IN_OP_MOD_ESW_VPORT,
1914                                             vport, link_state);
1915         if (err) {
1916                 mlx5_core_warn(esw->dev,
1917                                "Failed to set vport %d link state, err = %d",
1918                                vport, err);
1919                 goto unlock;
1920         }
1921
1922         evport->info.link_state = link_state;
1923
1924 unlock:
1925         mutex_unlock(&esw->state_lock);
1926         return 0;
1927 }
1928
1929 int mlx5_eswitch_get_vport_config(struct mlx5_eswitch *esw,
1930                                   int vport, struct ifla_vf_info *ivi)
1931 {
1932         struct mlx5_vport *evport;
1933
1934         if (!ESW_ALLOWED(esw))
1935                 return -EPERM;
1936         if (!LEGAL_VPORT(esw, vport))
1937                 return -EINVAL;
1938
1939         evport = &esw->vports[vport];
1940
1941         memset(ivi, 0, sizeof(*ivi));
1942         ivi->vf = vport - 1;
1943
1944         mutex_lock(&esw->state_lock);
1945         ether_addr_copy(ivi->mac, evport->info.mac);
1946         ivi->linkstate = evport->info.link_state;
1947         ivi->vlan = evport->info.vlan;
1948         ivi->qos = evport->info.qos;
1949         ivi->spoofchk = evport->info.spoofchk;
1950         ivi->trusted = evport->info.trusted;
1951         ivi->min_tx_rate = evport->info.min_rate;
1952         ivi->max_tx_rate = evport->info.max_rate;
1953         mutex_unlock(&esw->state_lock);
1954
1955         return 0;
1956 }
1957
1958 int __mlx5_eswitch_set_vport_vlan(struct mlx5_eswitch *esw,
1959                                   int vport, u16 vlan, u8 qos, u8 set_flags)
1960 {
1961         struct mlx5_vport *evport;
1962         int err = 0;
1963
1964         if (!ESW_ALLOWED(esw))
1965                 return -EPERM;
1966         if (!LEGAL_VPORT(esw, vport) || (vlan > 4095) || (qos > 7))
1967                 return -EINVAL;
1968
1969         mutex_lock(&esw->state_lock);
1970         evport = &esw->vports[vport];
1971
1972         err = modify_esw_vport_cvlan(esw->dev, vport, vlan, qos, set_flags);
1973         if (err)
1974                 goto unlock;
1975
1976         evport->info.vlan = vlan;
1977         evport->info.qos = qos;
1978         if (evport->enabled && esw->mode == SRIOV_LEGACY) {
1979                 err = esw_vport_ingress_config(esw, evport);
1980                 if (err)
1981                         goto unlock;
1982                 err = esw_vport_egress_config(esw, evport);
1983         }
1984
1985 unlock:
1986         mutex_unlock(&esw->state_lock);
1987         return err;
1988 }
1989
1990 int mlx5_eswitch_set_vport_vlan(struct mlx5_eswitch *esw,
1991                                 int vport, u16 vlan, u8 qos)
1992 {
1993         u8 set_flags = 0;
1994
1995         if (vlan || qos)
1996                 set_flags = SET_VLAN_STRIP | SET_VLAN_INSERT;
1997
1998         return __mlx5_eswitch_set_vport_vlan(esw, vport, vlan, qos, set_flags);
1999 }
2000
2001 int mlx5_eswitch_set_vport_spoofchk(struct mlx5_eswitch *esw,
2002                                     int vport, bool spoofchk)
2003 {
2004         struct mlx5_vport *evport;
2005         bool pschk;
2006         int err = 0;
2007
2008         if (!ESW_ALLOWED(esw))
2009                 return -EPERM;
2010         if (!LEGAL_VPORT(esw, vport))
2011                 return -EINVAL;
2012
2013         mutex_lock(&esw->state_lock);
2014         evport = &esw->vports[vport];
2015         pschk = evport->info.spoofchk;
2016         evport->info.spoofchk = spoofchk;
2017         if (evport->enabled && esw->mode == SRIOV_LEGACY)
2018                 err = esw_vport_ingress_config(esw, evport);
2019         if (err)
2020                 evport->info.spoofchk = pschk;
2021         mutex_unlock(&esw->state_lock);
2022
2023         return err;
2024 }
2025
2026 int mlx5_eswitch_set_vport_trust(struct mlx5_eswitch *esw,
2027                                  int vport, bool setting)
2028 {
2029         struct mlx5_vport *evport;
2030
2031         if (!ESW_ALLOWED(esw))
2032                 return -EPERM;
2033         if (!LEGAL_VPORT(esw, vport))
2034                 return -EINVAL;
2035
2036         mutex_lock(&esw->state_lock);
2037         evport = &esw->vports[vport];
2038         evport->info.trusted = setting;
2039         if (evport->enabled)
2040                 esw_vport_change_handle_locked(evport);
2041         mutex_unlock(&esw->state_lock);
2042
2043         return 0;
2044 }
2045
2046 static u32 calculate_vports_min_rate_divider(struct mlx5_eswitch *esw)
2047 {
2048         u32 fw_max_bw_share = MLX5_CAP_QOS(esw->dev, max_tsar_bw_share);
2049         struct mlx5_vport *evport;
2050         u32 max_guarantee = 0;
2051         int i;
2052
2053         for (i = 0; i <= esw->total_vports; i++) {
2054                 evport = &esw->vports[i];
2055                 if (!evport->enabled || evport->info.min_rate < max_guarantee)
2056                         continue;
2057                 max_guarantee = evport->info.min_rate;
2058         }
2059
2060         return max_t(u32, max_guarantee / fw_max_bw_share, 1);
2061 }
2062
2063 static int normalize_vports_min_rate(struct mlx5_eswitch *esw, u32 divider)
2064 {
2065         u32 fw_max_bw_share = MLX5_CAP_QOS(esw->dev, max_tsar_bw_share);
2066         struct mlx5_vport *evport;
2067         u32 vport_max_rate;
2068         u32 vport_min_rate;
2069         u32 bw_share;
2070         int err;
2071         int i;
2072
2073         for (i = 0; i <= esw->total_vports; i++) {
2074                 evport = &esw->vports[i];
2075                 if (!evport->enabled)
2076                         continue;
2077                 vport_min_rate = evport->info.min_rate;
2078                 vport_max_rate = evport->info.max_rate;
2079                 bw_share = MLX5_MIN_BW_SHARE;
2080
2081                 if (vport_min_rate)
2082                         bw_share = MLX5_RATE_TO_BW_SHARE(vport_min_rate,
2083                                                          divider,
2084                                                          fw_max_bw_share);
2085
2086                 if (bw_share == evport->qos.bw_share)
2087                         continue;
2088
2089                 err = esw_vport_qos_config(esw, i, vport_max_rate,
2090                                            bw_share);
2091                 if (!err)
2092                         evport->qos.bw_share = bw_share;
2093                 else
2094                         return err;
2095         }
2096
2097         return 0;
2098 }
2099
2100 int mlx5_eswitch_set_vport_rate(struct mlx5_eswitch *esw, int vport,
2101                                 u32 max_rate, u32 min_rate)
2102 {
2103         u32 fw_max_bw_share = MLX5_CAP_QOS(esw->dev, max_tsar_bw_share);
2104         bool min_rate_supported = MLX5_CAP_QOS(esw->dev, esw_bw_share) &&
2105                                         fw_max_bw_share >= MLX5_MIN_BW_SHARE;
2106         bool max_rate_supported = MLX5_CAP_QOS(esw->dev, esw_rate_limit);
2107         struct mlx5_vport *evport;
2108         u32 previous_min_rate;
2109         u32 divider;
2110         int err = 0;
2111
2112         if (!ESW_ALLOWED(esw))
2113                 return -EPERM;
2114         if (!LEGAL_VPORT(esw, vport))
2115                 return -EINVAL;
2116         if ((min_rate && !min_rate_supported) || (max_rate && !max_rate_supported))
2117                 return -EOPNOTSUPP;
2118
2119         mutex_lock(&esw->state_lock);
2120         evport = &esw->vports[vport];
2121
2122         if (min_rate == evport->info.min_rate)
2123                 goto set_max_rate;
2124
2125         previous_min_rate = evport->info.min_rate;
2126         evport->info.min_rate = min_rate;
2127         divider = calculate_vports_min_rate_divider(esw);
2128         err = normalize_vports_min_rate(esw, divider);
2129         if (err) {
2130                 evport->info.min_rate = previous_min_rate;
2131                 goto unlock;
2132         }
2133
2134 set_max_rate:
2135         if (max_rate == evport->info.max_rate)
2136                 goto unlock;
2137
2138         err = esw_vport_qos_config(esw, vport, max_rate, evport->qos.bw_share);
2139         if (!err)
2140                 evport->info.max_rate = max_rate;
2141
2142 unlock:
2143         mutex_unlock(&esw->state_lock);
2144         return err;
2145 }
2146
2147 int mlx5_eswitch_get_vport_stats(struct mlx5_eswitch *esw,
2148                                  int vport,
2149                                  struct ifla_vf_stats *vf_stats)
2150 {
2151         int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
2152         u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)] = {0};
2153         int err = 0;
2154         u32 *out;
2155
2156         if (!ESW_ALLOWED(esw))
2157                 return -EPERM;
2158         if (!LEGAL_VPORT(esw, vport))
2159                 return -EINVAL;
2160
2161         out = mlx5_vzalloc(outlen);
2162         if (!out)
2163                 return -ENOMEM;
2164
2165         MLX5_SET(query_vport_counter_in, in, opcode,
2166                  MLX5_CMD_OP_QUERY_VPORT_COUNTER);
2167         MLX5_SET(query_vport_counter_in, in, op_mod, 0);
2168         MLX5_SET(query_vport_counter_in, in, vport_number, vport);
2169         if (vport)
2170                 MLX5_SET(query_vport_counter_in, in, other_vport, 1);
2171
2172         memset(out, 0, outlen);
2173         err = mlx5_cmd_exec(esw->dev, in, sizeof(in), out, outlen);
2174         if (err)
2175                 goto free_out;
2176
2177         #define MLX5_GET_CTR(p, x) \
2178                 MLX5_GET64(query_vport_counter_out, p, x)
2179
2180         memset(vf_stats, 0, sizeof(*vf_stats));
2181         vf_stats->rx_packets =
2182                 MLX5_GET_CTR(out, received_eth_unicast.packets) +
2183                 MLX5_GET_CTR(out, received_eth_multicast.packets) +
2184                 MLX5_GET_CTR(out, received_eth_broadcast.packets);
2185
2186         vf_stats->rx_bytes =
2187                 MLX5_GET_CTR(out, received_eth_unicast.octets) +
2188                 MLX5_GET_CTR(out, received_eth_multicast.octets) +
2189                 MLX5_GET_CTR(out, received_eth_broadcast.octets);
2190
2191         vf_stats->tx_packets =
2192                 MLX5_GET_CTR(out, transmitted_eth_unicast.packets) +
2193                 MLX5_GET_CTR(out, transmitted_eth_multicast.packets) +
2194                 MLX5_GET_CTR(out, transmitted_eth_broadcast.packets);
2195
2196         vf_stats->tx_bytes =
2197                 MLX5_GET_CTR(out, transmitted_eth_unicast.octets) +
2198                 MLX5_GET_CTR(out, transmitted_eth_multicast.octets) +
2199                 MLX5_GET_CTR(out, transmitted_eth_broadcast.octets);
2200
2201         vf_stats->multicast =
2202                 MLX5_GET_CTR(out, received_eth_multicast.packets);
2203
2204         vf_stats->broadcast =
2205                 MLX5_GET_CTR(out, received_eth_broadcast.packets);
2206
2207 free_out:
2208         kvfree(out);
2209         return err;
2210 }