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