mlx5/core: E-Switch, Allocate ECPF vport if it's an eswitch manager
[platform/kernel/linux-rpi.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 <linux/mlx5/mpfs.h>
39 #include "esw/acl/lgcy.h"
40 #include "esw/legacy.h"
41 #include "esw/qos.h"
42 #include "mlx5_core.h"
43 #include "lib/eq.h"
44 #include "eswitch.h"
45 #include "fs_core.h"
46 #include "devlink.h"
47 #include "ecpf.h"
48 #include "en/mod_hdr.h"
49
50 enum {
51         MLX5_ACTION_NONE = 0,
52         MLX5_ACTION_ADD  = 1,
53         MLX5_ACTION_DEL  = 2,
54 };
55
56 /* Vport UC/MC hash node */
57 struct vport_addr {
58         struct l2addr_node     node;
59         u8                     action;
60         u16                    vport;
61         struct mlx5_flow_handle *flow_rule;
62         bool mpfs; /* UC MAC was added to MPFs */
63         /* A flag indicating that mac was added due to mc promiscuous vport */
64         bool mc_promisc;
65 };
66
67 static int mlx5_eswitch_check(const struct mlx5_core_dev *dev)
68 {
69         if (MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
70                 return -EOPNOTSUPP;
71
72         if (!MLX5_ESWITCH_MANAGER(dev))
73                 return -EOPNOTSUPP;
74
75         return 0;
76 }
77
78 struct mlx5_eswitch *mlx5_devlink_eswitch_get(struct devlink *devlink)
79 {
80         struct mlx5_core_dev *dev = devlink_priv(devlink);
81         int err;
82
83         err = mlx5_eswitch_check(dev);
84         if (err)
85                 return ERR_PTR(err);
86
87         return dev->priv.eswitch;
88 }
89
90 struct mlx5_vport *__must_check
91 mlx5_eswitch_get_vport(struct mlx5_eswitch *esw, u16 vport_num)
92 {
93         struct mlx5_vport *vport;
94
95         if (!esw)
96                 return ERR_PTR(-EPERM);
97
98         vport = xa_load(&esw->vports, vport_num);
99         if (!vport) {
100                 esw_debug(esw->dev, "vport out of range: num(0x%x)\n", vport_num);
101                 return ERR_PTR(-EINVAL);
102         }
103         return vport;
104 }
105
106 static int arm_vport_context_events_cmd(struct mlx5_core_dev *dev, u16 vport,
107                                         u32 events_mask)
108 {
109         u32 in[MLX5_ST_SZ_DW(modify_nic_vport_context_in)] = {};
110         void *nic_vport_ctx;
111
112         MLX5_SET(modify_nic_vport_context_in, in,
113                  opcode, MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
114         MLX5_SET(modify_nic_vport_context_in, in, field_select.change_event, 1);
115         MLX5_SET(modify_nic_vport_context_in, in, vport_number, vport);
116         if (vport || mlx5_core_is_ecpf(dev))
117                 MLX5_SET(modify_nic_vport_context_in, in, other_vport, 1);
118         nic_vport_ctx = MLX5_ADDR_OF(modify_nic_vport_context_in,
119                                      in, nic_vport_context);
120
121         MLX5_SET(nic_vport_context, nic_vport_ctx, arm_change_event, 1);
122
123         if (events_mask & MLX5_VPORT_UC_ADDR_CHANGE)
124                 MLX5_SET(nic_vport_context, nic_vport_ctx,
125                          event_on_uc_address_change, 1);
126         if (events_mask & MLX5_VPORT_MC_ADDR_CHANGE)
127                 MLX5_SET(nic_vport_context, nic_vport_ctx,
128                          event_on_mc_address_change, 1);
129         if (events_mask & MLX5_VPORT_PROMISC_CHANGE)
130                 MLX5_SET(nic_vport_context, nic_vport_ctx,
131                          event_on_promisc_change, 1);
132
133         return mlx5_cmd_exec_in(dev, modify_nic_vport_context, in);
134 }
135
136 /* E-Switch vport context HW commands */
137 int mlx5_eswitch_modify_esw_vport_context(struct mlx5_core_dev *dev, u16 vport,
138                                           bool other_vport, void *in)
139 {
140         MLX5_SET(modify_esw_vport_context_in, in, opcode,
141                  MLX5_CMD_OP_MODIFY_ESW_VPORT_CONTEXT);
142         MLX5_SET(modify_esw_vport_context_in, in, vport_number, vport);
143         MLX5_SET(modify_esw_vport_context_in, in, other_vport, other_vport);
144         return mlx5_cmd_exec_in(dev, modify_esw_vport_context, in);
145 }
146
147 static int modify_esw_vport_cvlan(struct mlx5_core_dev *dev, u16 vport,
148                                   u16 vlan, u8 qos, u8 set_flags)
149 {
150         u32 in[MLX5_ST_SZ_DW(modify_esw_vport_context_in)] = {};
151
152         if (!MLX5_CAP_ESW(dev, vport_cvlan_strip) ||
153             !MLX5_CAP_ESW(dev, vport_cvlan_insert_if_not_exist))
154                 return -EOPNOTSUPP;
155
156         esw_debug(dev, "Set Vport[%d] VLAN %d qos %d set=%x\n",
157                   vport, vlan, qos, set_flags);
158
159         if (set_flags & SET_VLAN_STRIP)
160                 MLX5_SET(modify_esw_vport_context_in, in,
161                          esw_vport_context.vport_cvlan_strip, 1);
162
163         if (set_flags & SET_VLAN_INSERT) {
164                 if (MLX5_CAP_ESW(dev, vport_cvlan_insert_always)) {
165                         /* insert either if vlan exist in packet or not */
166                         MLX5_SET(modify_esw_vport_context_in, in,
167                                  esw_vport_context.vport_cvlan_insert,
168                                  MLX5_VPORT_CVLAN_INSERT_ALWAYS);
169                 } else {
170                         /* insert only if no vlan in packet */
171                         MLX5_SET(modify_esw_vport_context_in, in,
172                                  esw_vport_context.vport_cvlan_insert,
173                                  MLX5_VPORT_CVLAN_INSERT_WHEN_NO_CVLAN);
174                 }
175                 MLX5_SET(modify_esw_vport_context_in, in,
176                          esw_vport_context.cvlan_pcp, qos);
177                 MLX5_SET(modify_esw_vport_context_in, in,
178                          esw_vport_context.cvlan_id, vlan);
179         }
180
181         MLX5_SET(modify_esw_vport_context_in, in,
182                  field_select.vport_cvlan_strip, 1);
183         MLX5_SET(modify_esw_vport_context_in, in,
184                  field_select.vport_cvlan_insert, 1);
185
186         return mlx5_eswitch_modify_esw_vport_context(dev, vport, true, in);
187 }
188
189 /* E-Switch FDB */
190 static struct mlx5_flow_handle *
191 __esw_fdb_set_vport_rule(struct mlx5_eswitch *esw, u16 vport, bool rx_rule,
192                          u8 mac_c[ETH_ALEN], u8 mac_v[ETH_ALEN])
193 {
194         int match_header = (is_zero_ether_addr(mac_c) ? 0 :
195                             MLX5_MATCH_OUTER_HEADERS);
196         struct mlx5_flow_handle *flow_rule = NULL;
197         struct mlx5_flow_act flow_act = {0};
198         struct mlx5_flow_destination dest = {};
199         struct mlx5_flow_spec *spec;
200         void *mv_misc = NULL;
201         void *mc_misc = NULL;
202         u8 *dmac_v = NULL;
203         u8 *dmac_c = NULL;
204
205         if (rx_rule)
206                 match_header |= MLX5_MATCH_MISC_PARAMETERS;
207
208         spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
209         if (!spec)
210                 return NULL;
211
212         dmac_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
213                               outer_headers.dmac_47_16);
214         dmac_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
215                               outer_headers.dmac_47_16);
216
217         if (match_header & MLX5_MATCH_OUTER_HEADERS) {
218                 ether_addr_copy(dmac_v, mac_v);
219                 ether_addr_copy(dmac_c, mac_c);
220         }
221
222         if (match_header & MLX5_MATCH_MISC_PARAMETERS) {
223                 mv_misc  = MLX5_ADDR_OF(fte_match_param, spec->match_value,
224                                         misc_parameters);
225                 mc_misc  = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
226                                         misc_parameters);
227                 MLX5_SET(fte_match_set_misc, mv_misc, source_port, MLX5_VPORT_UPLINK);
228                 MLX5_SET_TO_ONES(fte_match_set_misc, mc_misc, source_port);
229         }
230
231         dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
232         dest.vport.num = vport;
233
234         esw_debug(esw->dev,
235                   "\tFDB add rule dmac_v(%pM) dmac_c(%pM) -> vport(%d)\n",
236                   dmac_v, dmac_c, vport);
237         spec->match_criteria_enable = match_header;
238         flow_act.action =  MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
239         flow_rule =
240                 mlx5_add_flow_rules(esw->fdb_table.legacy.fdb, spec,
241                                     &flow_act, &dest, 1);
242         if (IS_ERR(flow_rule)) {
243                 esw_warn(esw->dev,
244                          "FDB: Failed to add flow rule: dmac_v(%pM) dmac_c(%pM) -> vport(%d), err(%ld)\n",
245                          dmac_v, dmac_c, vport, PTR_ERR(flow_rule));
246                 flow_rule = NULL;
247         }
248
249         kvfree(spec);
250         return flow_rule;
251 }
252
253 static struct mlx5_flow_handle *
254 esw_fdb_set_vport_rule(struct mlx5_eswitch *esw, u8 mac[ETH_ALEN], u16 vport)
255 {
256         u8 mac_c[ETH_ALEN];
257
258         eth_broadcast_addr(mac_c);
259         return __esw_fdb_set_vport_rule(esw, vport, false, mac_c, mac);
260 }
261
262 static struct mlx5_flow_handle *
263 esw_fdb_set_vport_allmulti_rule(struct mlx5_eswitch *esw, u16 vport)
264 {
265         u8 mac_c[ETH_ALEN];
266         u8 mac_v[ETH_ALEN];
267
268         eth_zero_addr(mac_c);
269         eth_zero_addr(mac_v);
270         mac_c[0] = 0x01;
271         mac_v[0] = 0x01;
272         return __esw_fdb_set_vport_rule(esw, vport, false, mac_c, mac_v);
273 }
274
275 static struct mlx5_flow_handle *
276 esw_fdb_set_vport_promisc_rule(struct mlx5_eswitch *esw, u16 vport)
277 {
278         u8 mac_c[ETH_ALEN];
279         u8 mac_v[ETH_ALEN];
280
281         eth_zero_addr(mac_c);
282         eth_zero_addr(mac_v);
283         return __esw_fdb_set_vport_rule(esw, vport, true, mac_c, mac_v);
284 }
285
286 /* E-Switch vport UC/MC lists management */
287 typedef int (*vport_addr_action)(struct mlx5_eswitch *esw,
288                                  struct vport_addr *vaddr);
289
290 static int esw_add_uc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
291 {
292         u8 *mac = vaddr->node.addr;
293         u16 vport = vaddr->vport;
294         int err;
295
296         /* Skip mlx5_mpfs_add_mac for eswitch_managers,
297          * it is already done by its netdev in mlx5e_execute_l2_action
298          */
299         if (mlx5_esw_is_manager_vport(esw, vport))
300                 goto fdb_add;
301
302         err = mlx5_mpfs_add_mac(esw->dev, mac);
303         if (err) {
304                 esw_warn(esw->dev,
305                          "Failed to add L2 table mac(%pM) for vport(0x%x), err(%d)\n",
306                          mac, vport, err);
307                 return err;
308         }
309         vaddr->mpfs = true;
310
311 fdb_add:
312         /* SRIOV is enabled: Forward UC MAC to vport */
313         if (esw->fdb_table.legacy.fdb && esw->mode == MLX5_ESWITCH_LEGACY) {
314                 vaddr->flow_rule = esw_fdb_set_vport_rule(esw, mac, vport);
315
316                 esw_debug(esw->dev, "\tADDED UC MAC: vport[%d] %pM fr(%p)\n",
317                           vport, mac, vaddr->flow_rule);
318         }
319
320         return 0;
321 }
322
323 static int esw_del_uc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
324 {
325         u8 *mac = vaddr->node.addr;
326         u16 vport = vaddr->vport;
327         int err = 0;
328
329         /* Skip mlx5_mpfs_del_mac for eswitch managers,
330          * it is already done by its netdev in mlx5e_execute_l2_action
331          */
332         if (!vaddr->mpfs || mlx5_esw_is_manager_vport(esw, vport))
333                 goto fdb_del;
334
335         err = mlx5_mpfs_del_mac(esw->dev, mac);
336         if (err)
337                 esw_warn(esw->dev,
338                          "Failed to del L2 table mac(%pM) for vport(%d), err(%d)\n",
339                          mac, vport, err);
340         vaddr->mpfs = false;
341
342 fdb_del:
343         if (vaddr->flow_rule)
344                 mlx5_del_flow_rules(vaddr->flow_rule);
345         vaddr->flow_rule = NULL;
346
347         return 0;
348 }
349
350 static void update_allmulti_vports(struct mlx5_eswitch *esw,
351                                    struct vport_addr *vaddr,
352                                    struct esw_mc_addr *esw_mc)
353 {
354         u8 *mac = vaddr->node.addr;
355         struct mlx5_vport *vport;
356         unsigned long i;
357         u16 vport_num;
358
359         mlx5_esw_for_each_vport(esw, i, vport) {
360                 struct hlist_head *vport_hash = vport->mc_list;
361                 struct vport_addr *iter_vaddr =
362                                         l2addr_hash_find(vport_hash,
363                                                          mac,
364                                                          struct vport_addr);
365                 vport_num = vport->vport;
366                 if (IS_ERR_OR_NULL(vport->allmulti_rule) ||
367                     vaddr->vport == vport_num)
368                         continue;
369                 switch (vaddr->action) {
370                 case MLX5_ACTION_ADD:
371                         if (iter_vaddr)
372                                 continue;
373                         iter_vaddr = l2addr_hash_add(vport_hash, mac,
374                                                      struct vport_addr,
375                                                      GFP_KERNEL);
376                         if (!iter_vaddr) {
377                                 esw_warn(esw->dev,
378                                          "ALL-MULTI: Failed to add MAC(%pM) to vport[%d] DB\n",
379                                          mac, vport_num);
380                                 continue;
381                         }
382                         iter_vaddr->vport = vport_num;
383                         iter_vaddr->flow_rule =
384                                         esw_fdb_set_vport_rule(esw,
385                                                                mac,
386                                                                vport_num);
387                         iter_vaddr->mc_promisc = true;
388                         break;
389                 case MLX5_ACTION_DEL:
390                         if (!iter_vaddr)
391                                 continue;
392                         mlx5_del_flow_rules(iter_vaddr->flow_rule);
393                         l2addr_hash_del(iter_vaddr);
394                         break;
395                 }
396         }
397 }
398
399 static int esw_add_mc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
400 {
401         struct hlist_head *hash = esw->mc_table;
402         struct esw_mc_addr *esw_mc;
403         u8 *mac = vaddr->node.addr;
404         u16 vport = vaddr->vport;
405
406         if (!esw->fdb_table.legacy.fdb)
407                 return 0;
408
409         esw_mc = l2addr_hash_find(hash, mac, struct esw_mc_addr);
410         if (esw_mc)
411                 goto add;
412
413         esw_mc = l2addr_hash_add(hash, mac, struct esw_mc_addr, GFP_KERNEL);
414         if (!esw_mc)
415                 return -ENOMEM;
416
417         esw_mc->uplink_rule = /* Forward MC MAC to Uplink */
418                 esw_fdb_set_vport_rule(esw, mac, MLX5_VPORT_UPLINK);
419
420         /* Add this multicast mac to all the mc promiscuous vports */
421         update_allmulti_vports(esw, vaddr, esw_mc);
422
423 add:
424         /* If the multicast mac is added as a result of mc promiscuous vport,
425          * don't increment the multicast ref count
426          */
427         if (!vaddr->mc_promisc)
428                 esw_mc->refcnt++;
429
430         /* Forward MC MAC to vport */
431         vaddr->flow_rule = esw_fdb_set_vport_rule(esw, mac, vport);
432         esw_debug(esw->dev,
433                   "\tADDED MC MAC: vport[%d] %pM fr(%p) refcnt(%d) uplinkfr(%p)\n",
434                   vport, mac, vaddr->flow_rule,
435                   esw_mc->refcnt, esw_mc->uplink_rule);
436         return 0;
437 }
438
439 static int esw_del_mc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
440 {
441         struct hlist_head *hash = esw->mc_table;
442         struct esw_mc_addr *esw_mc;
443         u8 *mac = vaddr->node.addr;
444         u16 vport = vaddr->vport;
445
446         if (!esw->fdb_table.legacy.fdb)
447                 return 0;
448
449         esw_mc = l2addr_hash_find(hash, mac, struct esw_mc_addr);
450         if (!esw_mc) {
451                 esw_warn(esw->dev,
452                          "Failed to find eswitch MC addr for MAC(%pM) vport(%d)",
453                          mac, vport);
454                 return -EINVAL;
455         }
456         esw_debug(esw->dev,
457                   "\tDELETE MC MAC: vport[%d] %pM fr(%p) refcnt(%d) uplinkfr(%p)\n",
458                   vport, mac, vaddr->flow_rule, esw_mc->refcnt,
459                   esw_mc->uplink_rule);
460
461         if (vaddr->flow_rule)
462                 mlx5_del_flow_rules(vaddr->flow_rule);
463         vaddr->flow_rule = NULL;
464
465         /* If the multicast mac is added as a result of mc promiscuous vport,
466          * don't decrement the multicast ref count.
467          */
468         if (vaddr->mc_promisc || (--esw_mc->refcnt > 0))
469                 return 0;
470
471         /* Remove this multicast mac from all the mc promiscuous vports */
472         update_allmulti_vports(esw, vaddr, esw_mc);
473
474         if (esw_mc->uplink_rule)
475                 mlx5_del_flow_rules(esw_mc->uplink_rule);
476
477         l2addr_hash_del(esw_mc);
478         return 0;
479 }
480
481 /* Apply vport UC/MC list to HW l2 table and FDB table */
482 static void esw_apply_vport_addr_list(struct mlx5_eswitch *esw,
483                                       struct mlx5_vport *vport, int list_type)
484 {
485         bool is_uc = list_type == MLX5_NVPRT_LIST_TYPE_UC;
486         vport_addr_action vport_addr_add;
487         vport_addr_action vport_addr_del;
488         struct vport_addr *addr;
489         struct l2addr_node *node;
490         struct hlist_head *hash;
491         struct hlist_node *tmp;
492         int hi;
493
494         vport_addr_add = is_uc ? esw_add_uc_addr :
495                                  esw_add_mc_addr;
496         vport_addr_del = is_uc ? esw_del_uc_addr :
497                                  esw_del_mc_addr;
498
499         hash = is_uc ? vport->uc_list : vport->mc_list;
500         for_each_l2hash_node(node, tmp, hash, hi) {
501                 addr = container_of(node, struct vport_addr, node);
502                 switch (addr->action) {
503                 case MLX5_ACTION_ADD:
504                         vport_addr_add(esw, addr);
505                         addr->action = MLX5_ACTION_NONE;
506                         break;
507                 case MLX5_ACTION_DEL:
508                         vport_addr_del(esw, addr);
509                         l2addr_hash_del(addr);
510                         break;
511                 }
512         }
513 }
514
515 /* Sync vport UC/MC list from vport context */
516 static void esw_update_vport_addr_list(struct mlx5_eswitch *esw,
517                                        struct mlx5_vport *vport, int list_type)
518 {
519         bool is_uc = list_type == MLX5_NVPRT_LIST_TYPE_UC;
520         u8 (*mac_list)[ETH_ALEN];
521         struct l2addr_node *node;
522         struct vport_addr *addr;
523         struct hlist_head *hash;
524         struct hlist_node *tmp;
525         int size;
526         int err;
527         int hi;
528         int i;
529
530         size = is_uc ? MLX5_MAX_UC_PER_VPORT(esw->dev) :
531                        MLX5_MAX_MC_PER_VPORT(esw->dev);
532
533         mac_list = kcalloc(size, ETH_ALEN, GFP_KERNEL);
534         if (!mac_list)
535                 return;
536
537         hash = is_uc ? vport->uc_list : vport->mc_list;
538
539         for_each_l2hash_node(node, tmp, hash, hi) {
540                 addr = container_of(node, struct vport_addr, node);
541                 addr->action = MLX5_ACTION_DEL;
542         }
543
544         if (!vport->enabled)
545                 goto out;
546
547         err = mlx5_query_nic_vport_mac_list(esw->dev, vport->vport, list_type,
548                                             mac_list, &size);
549         if (err)
550                 goto out;
551         esw_debug(esw->dev, "vport[%d] context update %s list size (%d)\n",
552                   vport->vport, is_uc ? "UC" : "MC", size);
553
554         for (i = 0; i < size; i++) {
555                 if (is_uc && !is_valid_ether_addr(mac_list[i]))
556                         continue;
557
558                 if (!is_uc && !is_multicast_ether_addr(mac_list[i]))
559                         continue;
560
561                 addr = l2addr_hash_find(hash, mac_list[i], struct vport_addr);
562                 if (addr) {
563                         addr->action = MLX5_ACTION_NONE;
564                         /* If this mac was previously added because of allmulti
565                          * promiscuous rx mode, its now converted to be original
566                          * vport mac.
567                          */
568                         if (addr->mc_promisc) {
569                                 struct esw_mc_addr *esw_mc =
570                                         l2addr_hash_find(esw->mc_table,
571                                                          mac_list[i],
572                                                          struct esw_mc_addr);
573                                 if (!esw_mc) {
574                                         esw_warn(esw->dev,
575                                                  "Failed to MAC(%pM) in mcast DB\n",
576                                                  mac_list[i]);
577                                         continue;
578                                 }
579                                 esw_mc->refcnt++;
580                                 addr->mc_promisc = false;
581                         }
582                         continue;
583                 }
584
585                 addr = l2addr_hash_add(hash, mac_list[i], struct vport_addr,
586                                        GFP_KERNEL);
587                 if (!addr) {
588                         esw_warn(esw->dev,
589                                  "Failed to add MAC(%pM) to vport[%d] DB\n",
590                                  mac_list[i], vport->vport);
591                         continue;
592                 }
593                 addr->vport = vport->vport;
594                 addr->action = MLX5_ACTION_ADD;
595         }
596 out:
597         kfree(mac_list);
598 }
599
600 /* Sync vport UC/MC list from vport context
601  * Must be called after esw_update_vport_addr_list
602  */
603 static void esw_update_vport_mc_promisc(struct mlx5_eswitch *esw,
604                                         struct mlx5_vport *vport)
605 {
606         struct l2addr_node *node;
607         struct vport_addr *addr;
608         struct hlist_head *hash;
609         struct hlist_node *tmp;
610         int hi;
611
612         hash = vport->mc_list;
613
614         for_each_l2hash_node(node, tmp, esw->mc_table, hi) {
615                 u8 *mac = node->addr;
616
617                 addr = l2addr_hash_find(hash, mac, struct vport_addr);
618                 if (addr) {
619                         if (addr->action == MLX5_ACTION_DEL)
620                                 addr->action = MLX5_ACTION_NONE;
621                         continue;
622                 }
623                 addr = l2addr_hash_add(hash, mac, struct vport_addr,
624                                        GFP_KERNEL);
625                 if (!addr) {
626                         esw_warn(esw->dev,
627                                  "Failed to add allmulti MAC(%pM) to vport[%d] DB\n",
628                                  mac, vport->vport);
629                         continue;
630                 }
631                 addr->vport = vport->vport;
632                 addr->action = MLX5_ACTION_ADD;
633                 addr->mc_promisc = true;
634         }
635 }
636
637 /* Apply vport rx mode to HW FDB table */
638 static void esw_apply_vport_rx_mode(struct mlx5_eswitch *esw,
639                                     struct mlx5_vport *vport,
640                                     bool promisc, bool mc_promisc)
641 {
642         struct esw_mc_addr *allmulti_addr = &esw->mc_promisc;
643
644         if (IS_ERR_OR_NULL(vport->allmulti_rule) != mc_promisc)
645                 goto promisc;
646
647         if (mc_promisc) {
648                 vport->allmulti_rule =
649                         esw_fdb_set_vport_allmulti_rule(esw, vport->vport);
650                 if (!allmulti_addr->uplink_rule)
651                         allmulti_addr->uplink_rule =
652                                 esw_fdb_set_vport_allmulti_rule(esw,
653                                                                 MLX5_VPORT_UPLINK);
654                 allmulti_addr->refcnt++;
655         } else if (vport->allmulti_rule) {
656                 mlx5_del_flow_rules(vport->allmulti_rule);
657                 vport->allmulti_rule = NULL;
658
659                 if (--allmulti_addr->refcnt > 0)
660                         goto promisc;
661
662                 if (allmulti_addr->uplink_rule)
663                         mlx5_del_flow_rules(allmulti_addr->uplink_rule);
664                 allmulti_addr->uplink_rule = NULL;
665         }
666
667 promisc:
668         if (IS_ERR_OR_NULL(vport->promisc_rule) != promisc)
669                 return;
670
671         if (promisc) {
672                 vport->promisc_rule =
673                         esw_fdb_set_vport_promisc_rule(esw, vport->vport);
674         } else if (vport->promisc_rule) {
675                 mlx5_del_flow_rules(vport->promisc_rule);
676                 vport->promisc_rule = NULL;
677         }
678 }
679
680 /* Sync vport rx mode from vport context */
681 static void esw_update_vport_rx_mode(struct mlx5_eswitch *esw,
682                                      struct mlx5_vport *vport)
683 {
684         int promisc_all = 0;
685         int promisc_uc = 0;
686         int promisc_mc = 0;
687         int err;
688
689         err = mlx5_query_nic_vport_promisc(esw->dev,
690                                            vport->vport,
691                                            &promisc_uc,
692                                            &promisc_mc,
693                                            &promisc_all);
694         if (err)
695                 return;
696         esw_debug(esw->dev, "vport[%d] context update rx mode promisc_all=%d, all_multi=%d\n",
697                   vport->vport, promisc_all, promisc_mc);
698
699         if (!vport->info.trusted || !vport->enabled) {
700                 promisc_uc = 0;
701                 promisc_mc = 0;
702                 promisc_all = 0;
703         }
704
705         esw_apply_vport_rx_mode(esw, vport, promisc_all,
706                                 (promisc_all || promisc_mc));
707 }
708
709 void esw_vport_change_handle_locked(struct mlx5_vport *vport)
710 {
711         struct mlx5_core_dev *dev = vport->dev;
712         struct mlx5_eswitch *esw = dev->priv.eswitch;
713         u8 mac[ETH_ALEN];
714
715         if (!MLX5_CAP_GEN(dev, log_max_l2_table))
716                 return;
717
718         mlx5_query_nic_vport_mac_address(dev, vport->vport, true, mac);
719         esw_debug(dev, "vport[%d] Context Changed: perm mac: %pM\n",
720                   vport->vport, mac);
721
722         if (vport->enabled_events & MLX5_VPORT_UC_ADDR_CHANGE) {
723                 esw_update_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_UC);
724                 esw_apply_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_UC);
725         }
726
727         if (vport->enabled_events & MLX5_VPORT_MC_ADDR_CHANGE)
728                 esw_update_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_MC);
729
730         if (vport->enabled_events & MLX5_VPORT_PROMISC_CHANGE) {
731                 esw_update_vport_rx_mode(esw, vport);
732                 if (!IS_ERR_OR_NULL(vport->allmulti_rule))
733                         esw_update_vport_mc_promisc(esw, vport);
734         }
735
736         if (vport->enabled_events & (MLX5_VPORT_PROMISC_CHANGE | MLX5_VPORT_MC_ADDR_CHANGE))
737                 esw_apply_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_MC);
738
739         esw_debug(esw->dev, "vport[%d] Context Changed: Done\n", vport->vport);
740         if (vport->enabled)
741                 arm_vport_context_events_cmd(dev, vport->vport,
742                                              vport->enabled_events);
743 }
744
745 static void esw_vport_change_handler(struct work_struct *work)
746 {
747         struct mlx5_vport *vport =
748                 container_of(work, struct mlx5_vport, vport_change_handler);
749         struct mlx5_eswitch *esw = vport->dev->priv.eswitch;
750
751         mutex_lock(&esw->state_lock);
752         esw_vport_change_handle_locked(vport);
753         mutex_unlock(&esw->state_lock);
754 }
755
756 static void node_guid_gen_from_mac(u64 *node_guid, const u8 *mac)
757 {
758         ((u8 *)node_guid)[7] = mac[0];
759         ((u8 *)node_guid)[6] = mac[1];
760         ((u8 *)node_guid)[5] = mac[2];
761         ((u8 *)node_guid)[4] = 0xff;
762         ((u8 *)node_guid)[3] = 0xfe;
763         ((u8 *)node_guid)[2] = mac[3];
764         ((u8 *)node_guid)[1] = mac[4];
765         ((u8 *)node_guid)[0] = mac[5];
766 }
767
768 static int esw_vport_setup_acl(struct mlx5_eswitch *esw,
769                                struct mlx5_vport *vport)
770 {
771         if (esw->mode == MLX5_ESWITCH_LEGACY)
772                 return esw_legacy_vport_acl_setup(esw, vport);
773         else
774                 return esw_vport_create_offloads_acl_tables(esw, vport);
775 }
776
777 static void esw_vport_cleanup_acl(struct mlx5_eswitch *esw,
778                                   struct mlx5_vport *vport)
779 {
780         if (esw->mode == MLX5_ESWITCH_LEGACY)
781                 esw_legacy_vport_acl_cleanup(esw, vport);
782         else
783                 esw_vport_destroy_offloads_acl_tables(esw, vport);
784 }
785
786 static int mlx5_esw_vport_caps_get(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
787 {
788         int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
789         void *query_ctx;
790         void *hca_caps;
791         int err;
792
793         if (!MLX5_CAP_GEN(esw->dev, vhca_resource_manager))
794                 return 0;
795
796         query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
797         if (!query_ctx)
798                 return -ENOMEM;
799
800         err = mlx5_vport_get_other_func_cap(esw->dev, vport->vport, query_ctx,
801                                             MLX5_CAP_GENERAL);
802         if (err)
803                 goto out_free;
804
805         hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
806         vport->info.roce_enabled = MLX5_GET(cmd_hca_cap, hca_caps, roce);
807
808         memset(query_ctx, 0, query_out_sz);
809         err = mlx5_vport_get_other_func_cap(esw->dev, vport->vport, query_ctx,
810                                             MLX5_CAP_GENERAL_2);
811         if (err)
812                 goto out_free;
813
814         hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
815         vport->info.mig_enabled = MLX5_GET(cmd_hca_cap_2, hca_caps, migratable);
816 out_free:
817         kfree(query_ctx);
818         return err;
819 }
820
821 static int esw_vport_setup(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
822 {
823         bool vst_mode_steering = esw_vst_mode_is_steering(esw);
824         u16 vport_num = vport->vport;
825         int flags;
826         int err;
827
828         err = esw_vport_setup_acl(esw, vport);
829         if (err)
830                 return err;
831
832         if (mlx5_esw_is_manager_vport(esw, vport_num))
833                 return 0;
834
835         err = mlx5_esw_vport_caps_get(esw, vport);
836         if (err)
837                 goto err_caps;
838
839         mlx5_modify_vport_admin_state(esw->dev,
840                                       MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
841                                       vport_num, 1,
842                                       vport->info.link_state);
843
844         /* Host PF has its own mac/guid. */
845         if (vport_num) {
846                 mlx5_modify_nic_vport_mac_address(esw->dev, vport_num,
847                                                   vport->info.mac);
848                 mlx5_modify_nic_vport_node_guid(esw->dev, vport_num,
849                                                 vport->info.node_guid);
850         }
851
852         flags = (vport->info.vlan || vport->info.qos) ?
853                 SET_VLAN_STRIP | SET_VLAN_INSERT : 0;
854         if (esw->mode == MLX5_ESWITCH_OFFLOADS || !vst_mode_steering)
855                 modify_esw_vport_cvlan(esw->dev, vport_num, vport->info.vlan,
856                                        vport->info.qos, flags);
857
858         return 0;
859
860 err_caps:
861         esw_vport_cleanup_acl(esw, vport);
862         return err;
863 }
864
865 /* Don't cleanup vport->info, it's needed to restore vport configuration */
866 static void esw_vport_cleanup(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
867 {
868         u16 vport_num = vport->vport;
869
870         if (!mlx5_esw_is_manager_vport(esw, vport_num))
871                 mlx5_modify_vport_admin_state(esw->dev,
872                                               MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
873                                               vport_num, 1,
874                                               MLX5_VPORT_ADMIN_STATE_DOWN);
875
876         mlx5_esw_qos_vport_disable(esw, vport);
877         esw_vport_cleanup_acl(esw, vport);
878 }
879
880 int mlx5_esw_vport_enable(struct mlx5_eswitch *esw, u16 vport_num,
881                           enum mlx5_eswitch_vport_event enabled_events)
882 {
883         struct mlx5_vport *vport;
884         int ret;
885
886         vport = mlx5_eswitch_get_vport(esw, vport_num);
887         if (IS_ERR(vport))
888                 return PTR_ERR(vport);
889
890         mutex_lock(&esw->state_lock);
891         WARN_ON(vport->enabled);
892
893         esw_debug(esw->dev, "Enabling VPORT(%d)\n", vport_num);
894
895         ret = esw_vport_setup(esw, vport);
896         if (ret)
897                 goto done;
898
899         /* Sync with current vport context */
900         vport->enabled_events = enabled_events;
901         vport->enabled = true;
902
903         /* Esw manager is trusted by default. Host PF (vport 0) is trusted as well
904          * in smartNIC as it's a vport group manager.
905          */
906         if (mlx5_esw_is_manager_vport(esw, vport_num) ||
907             (!vport_num && mlx5_core_is_ecpf(esw->dev)))
908                 vport->info.trusted = true;
909
910         if (!mlx5_esw_is_manager_vport(esw, vport->vport) &&
911             MLX5_CAP_GEN(esw->dev, vhca_resource_manager)) {
912                 ret = mlx5_esw_vport_vhca_id_set(esw, vport_num);
913                 if (ret)
914                         goto err_vhca_mapping;
915         }
916
917         /* External controller host PF has factory programmed MAC.
918          * Read it from the device.
919          */
920         if (mlx5_core_is_ecpf(esw->dev) && vport_num == MLX5_VPORT_PF)
921                 mlx5_query_nic_vport_mac_address(esw->dev, vport_num, true, vport->info.mac);
922
923         esw_vport_change_handle_locked(vport);
924
925         esw->enabled_vports++;
926         esw_debug(esw->dev, "Enabled VPORT(%d)\n", vport_num);
927 done:
928         mutex_unlock(&esw->state_lock);
929         return ret;
930
931 err_vhca_mapping:
932         esw_vport_cleanup(esw, vport);
933         mutex_unlock(&esw->state_lock);
934         return ret;
935 }
936
937 void mlx5_esw_vport_disable(struct mlx5_eswitch *esw, u16 vport_num)
938 {
939         struct mlx5_vport *vport;
940
941         vport = mlx5_eswitch_get_vport(esw, vport_num);
942         if (IS_ERR(vport))
943                 return;
944
945         mutex_lock(&esw->state_lock);
946         if (!vport->enabled)
947                 goto done;
948
949         esw_debug(esw->dev, "Disabling vport(%d)\n", vport_num);
950         /* Mark this vport as disabled to discard new events */
951         vport->enabled = false;
952
953         /* Disable events from this vport */
954         if (MLX5_CAP_GEN(esw->dev, log_max_l2_table))
955                 arm_vport_context_events_cmd(esw->dev, vport->vport, 0);
956
957         if (!mlx5_esw_is_manager_vport(esw, vport->vport) &&
958             MLX5_CAP_GEN(esw->dev, vhca_resource_manager))
959                 mlx5_esw_vport_vhca_id_clear(esw, vport_num);
960
961         /* We don't assume VFs will cleanup after themselves.
962          * Calling vport change handler while vport is disabled will cleanup
963          * the vport resources.
964          */
965         esw_vport_change_handle_locked(vport);
966         vport->enabled_events = 0;
967         esw_apply_vport_rx_mode(esw, vport, false, false);
968         esw_vport_cleanup(esw, vport);
969         esw->enabled_vports--;
970
971 done:
972         mutex_unlock(&esw->state_lock);
973 }
974
975 static int eswitch_vport_event(struct notifier_block *nb,
976                                unsigned long type, void *data)
977 {
978         struct mlx5_eswitch *esw = mlx5_nb_cof(nb, struct mlx5_eswitch, nb);
979         struct mlx5_eqe *eqe = data;
980         struct mlx5_vport *vport;
981         u16 vport_num;
982
983         vport_num = be16_to_cpu(eqe->data.vport_change.vport_num);
984         vport = mlx5_eswitch_get_vport(esw, vport_num);
985         if (!IS_ERR(vport))
986                 queue_work(esw->work_queue, &vport->vport_change_handler);
987         return NOTIFY_OK;
988 }
989
990 /**
991  * mlx5_esw_query_functions - Returns raw output about functions state
992  * @dev:        Pointer to device to query
993  *
994  * mlx5_esw_query_functions() allocates and returns functions changed
995  * raw output memory pointer from device on success. Otherwise returns ERR_PTR.
996  * Caller must free the memory using kvfree() when valid pointer is returned.
997  */
998 const u32 *mlx5_esw_query_functions(struct mlx5_core_dev *dev)
999 {
1000         int outlen = MLX5_ST_SZ_BYTES(query_esw_functions_out);
1001         u32 in[MLX5_ST_SZ_DW(query_esw_functions_in)] = {};
1002         u32 *out;
1003         int err;
1004
1005         out = kvzalloc(outlen, GFP_KERNEL);
1006         if (!out)
1007                 return ERR_PTR(-ENOMEM);
1008
1009         MLX5_SET(query_esw_functions_in, in, opcode,
1010                  MLX5_CMD_OP_QUERY_ESW_FUNCTIONS);
1011
1012         err = mlx5_cmd_exec(dev, in, sizeof(in), out, outlen);
1013         if (!err)
1014                 return out;
1015
1016         kvfree(out);
1017         return ERR_PTR(err);
1018 }
1019
1020 static void mlx5_eswitch_event_handlers_register(struct mlx5_eswitch *esw)
1021 {
1022         MLX5_NB_INIT(&esw->nb, eswitch_vport_event, NIC_VPORT_CHANGE);
1023         mlx5_eq_notifier_register(esw->dev, &esw->nb);
1024
1025         if (esw->mode == MLX5_ESWITCH_OFFLOADS && mlx5_eswitch_is_funcs_handler(esw->dev)) {
1026                 MLX5_NB_INIT(&esw->esw_funcs.nb, mlx5_esw_funcs_changed_handler,
1027                              ESW_FUNCTIONS_CHANGED);
1028                 mlx5_eq_notifier_register(esw->dev, &esw->esw_funcs.nb);
1029         }
1030 }
1031
1032 static void mlx5_eswitch_event_handlers_unregister(struct mlx5_eswitch *esw)
1033 {
1034         if (esw->mode == MLX5_ESWITCH_OFFLOADS && mlx5_eswitch_is_funcs_handler(esw->dev))
1035                 mlx5_eq_notifier_unregister(esw->dev, &esw->esw_funcs.nb);
1036
1037         mlx5_eq_notifier_unregister(esw->dev, &esw->nb);
1038
1039         flush_workqueue(esw->work_queue);
1040 }
1041
1042 static void mlx5_eswitch_clear_vf_vports_info(struct mlx5_eswitch *esw)
1043 {
1044         struct mlx5_vport *vport;
1045         unsigned long i;
1046
1047         mlx5_esw_for_each_vf_vport(esw, i, vport, esw->esw_funcs.num_vfs) {
1048                 memset(&vport->qos, 0, sizeof(vport->qos));
1049                 memset(&vport->info, 0, sizeof(vport->info));
1050                 vport->info.link_state = MLX5_VPORT_ADMIN_STATE_AUTO;
1051         }
1052 }
1053
1054 /* Public E-Switch API */
1055 int mlx5_eswitch_load_vport(struct mlx5_eswitch *esw, u16 vport_num,
1056                             enum mlx5_eswitch_vport_event enabled_events)
1057 {
1058         int err;
1059
1060         err = mlx5_esw_vport_enable(esw, vport_num, enabled_events);
1061         if (err)
1062                 return err;
1063
1064         err = esw_offloads_load_rep(esw, vport_num);
1065         if (err)
1066                 goto err_rep;
1067
1068         return err;
1069
1070 err_rep:
1071         mlx5_esw_vport_disable(esw, vport_num);
1072         return err;
1073 }
1074
1075 void mlx5_eswitch_unload_vport(struct mlx5_eswitch *esw, u16 vport_num)
1076 {
1077         esw_offloads_unload_rep(esw, vport_num);
1078         mlx5_esw_vport_disable(esw, vport_num);
1079 }
1080
1081 void mlx5_eswitch_unload_vf_vports(struct mlx5_eswitch *esw, u16 num_vfs)
1082 {
1083         struct mlx5_vport *vport;
1084         unsigned long i;
1085
1086         mlx5_esw_for_each_vf_vport(esw, i, vport, num_vfs) {
1087                 if (!vport->enabled)
1088                         continue;
1089                 mlx5_eswitch_unload_vport(esw, vport->vport);
1090         }
1091 }
1092
1093 int mlx5_eswitch_load_vf_vports(struct mlx5_eswitch *esw, u16 num_vfs,
1094                                 enum mlx5_eswitch_vport_event enabled_events)
1095 {
1096         struct mlx5_vport *vport;
1097         unsigned long i;
1098         int err;
1099
1100         mlx5_esw_for_each_vf_vport(esw, i, vport, num_vfs) {
1101                 err = mlx5_eswitch_load_vport(esw, vport->vport, enabled_events);
1102                 if (err)
1103                         goto vf_err;
1104         }
1105
1106         return 0;
1107
1108 vf_err:
1109         mlx5_eswitch_unload_vf_vports(esw, num_vfs);
1110         return err;
1111 }
1112
1113 static int host_pf_enable_hca(struct mlx5_core_dev *dev)
1114 {
1115         if (!mlx5_core_is_ecpf(dev))
1116                 return 0;
1117
1118         /* Once vport and representor are ready, take out the external host PF
1119          * out of initializing state. Enabling HCA clears the iser->initializing
1120          * bit and host PF driver loading can progress.
1121          */
1122         return mlx5_cmd_host_pf_enable_hca(dev);
1123 }
1124
1125 static void host_pf_disable_hca(struct mlx5_core_dev *dev)
1126 {
1127         if (!mlx5_core_is_ecpf(dev))
1128                 return;
1129
1130         mlx5_cmd_host_pf_disable_hca(dev);
1131 }
1132
1133 /* mlx5_eswitch_enable_pf_vf_vports() enables vports of PF, ECPF and VFs
1134  * whichever are present on the eswitch.
1135  */
1136 int
1137 mlx5_eswitch_enable_pf_vf_vports(struct mlx5_eswitch *esw,
1138                                  enum mlx5_eswitch_vport_event enabled_events)
1139 {
1140         int ret;
1141
1142         /* Enable PF vport */
1143         ret = mlx5_eswitch_load_vport(esw, MLX5_VPORT_PF, enabled_events);
1144         if (ret)
1145                 return ret;
1146
1147         /* Enable external host PF HCA */
1148         ret = host_pf_enable_hca(esw->dev);
1149         if (ret)
1150                 goto pf_hca_err;
1151
1152         /* Enable ECPF vport */
1153         if (mlx5_ecpf_vport_exists(esw->dev)) {
1154                 ret = mlx5_eswitch_load_vport(esw, MLX5_VPORT_ECPF, enabled_events);
1155                 if (ret)
1156                         goto ecpf_err;
1157         }
1158
1159         /* Enable VF vports */
1160         ret = mlx5_eswitch_load_vf_vports(esw, esw->esw_funcs.num_vfs,
1161                                           enabled_events);
1162         if (ret)
1163                 goto vf_err;
1164         return 0;
1165
1166 vf_err:
1167         if (mlx5_ecpf_vport_exists(esw->dev))
1168                 mlx5_eswitch_unload_vport(esw, MLX5_VPORT_ECPF);
1169 ecpf_err:
1170         host_pf_disable_hca(esw->dev);
1171 pf_hca_err:
1172         mlx5_eswitch_unload_vport(esw, MLX5_VPORT_PF);
1173         return ret;
1174 }
1175
1176 /* mlx5_eswitch_disable_pf_vf_vports() disables vports of PF, ECPF and VFs
1177  * whichever are previously enabled on the eswitch.
1178  */
1179 void mlx5_eswitch_disable_pf_vf_vports(struct mlx5_eswitch *esw)
1180 {
1181         mlx5_eswitch_unload_vf_vports(esw, esw->esw_funcs.num_vfs);
1182
1183         if (mlx5_ecpf_vport_exists(esw->dev))
1184                 mlx5_eswitch_unload_vport(esw, MLX5_VPORT_ECPF);
1185
1186         host_pf_disable_hca(esw->dev);
1187         mlx5_eswitch_unload_vport(esw, MLX5_VPORT_PF);
1188 }
1189
1190 static void mlx5_eswitch_get_devlink_param(struct mlx5_eswitch *esw)
1191 {
1192         struct devlink *devlink = priv_to_devlink(esw->dev);
1193         union devlink_param_value val;
1194         int err;
1195
1196         err = devl_param_driverinit_value_get(devlink,
1197                                               MLX5_DEVLINK_PARAM_ID_ESW_LARGE_GROUP_NUM,
1198                                               &val);
1199         if (!err) {
1200                 esw->params.large_group_num = val.vu32;
1201         } else {
1202                 esw_warn(esw->dev,
1203                          "Devlink can't get param fdb_large_groups, uses default (%d).\n",
1204                          ESW_OFFLOADS_DEFAULT_NUM_GROUPS);
1205                 esw->params.large_group_num = ESW_OFFLOADS_DEFAULT_NUM_GROUPS;
1206         }
1207 }
1208
1209 static void
1210 mlx5_eswitch_update_num_of_vfs(struct mlx5_eswitch *esw, int num_vfs)
1211 {
1212         const u32 *out;
1213
1214         if (num_vfs < 0)
1215                 return;
1216
1217         if (!mlx5_core_is_ecpf_esw_manager(esw->dev)) {
1218                 esw->esw_funcs.num_vfs = num_vfs;
1219                 return;
1220         }
1221
1222         out = mlx5_esw_query_functions(esw->dev);
1223         if (IS_ERR(out))
1224                 return;
1225
1226         esw->esw_funcs.num_vfs = MLX5_GET(query_esw_functions_out, out,
1227                                           host_params_context.host_num_of_vfs);
1228         kvfree(out);
1229 }
1230
1231 static void mlx5_esw_mode_change_notify(struct mlx5_eswitch *esw, u16 mode)
1232 {
1233         struct mlx5_esw_event_info info = {};
1234
1235         info.new_mode = mode;
1236
1237         blocking_notifier_call_chain(&esw->n_head, 0, &info);
1238 }
1239
1240 static int mlx5_esw_acls_ns_init(struct mlx5_eswitch *esw)
1241 {
1242         struct mlx5_core_dev *dev = esw->dev;
1243         int total_vports;
1244         int err;
1245
1246         if (esw->flags & MLX5_ESWITCH_VPORT_ACL_NS_CREATED)
1247                 return 0;
1248
1249         total_vports = mlx5_eswitch_get_total_vports(dev);
1250
1251         if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
1252                 err = mlx5_fs_egress_acls_init(dev, total_vports);
1253                 if (err)
1254                         return err;
1255         } else {
1256                 esw_warn(dev, "egress ACL is not supported by FW\n");
1257         }
1258
1259         if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
1260                 err = mlx5_fs_ingress_acls_init(dev, total_vports);
1261                 if (err)
1262                         goto err;
1263         } else {
1264                 esw_warn(dev, "ingress ACL is not supported by FW\n");
1265         }
1266         esw->flags |= MLX5_ESWITCH_VPORT_ACL_NS_CREATED;
1267         return 0;
1268
1269 err:
1270         if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support))
1271                 mlx5_fs_egress_acls_cleanup(dev);
1272         return err;
1273 }
1274
1275 static void mlx5_esw_acls_ns_cleanup(struct mlx5_eswitch *esw)
1276 {
1277         struct mlx5_core_dev *dev = esw->dev;
1278
1279         esw->flags &= ~MLX5_ESWITCH_VPORT_ACL_NS_CREATED;
1280         if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support))
1281                 mlx5_fs_ingress_acls_cleanup(dev);
1282         if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support))
1283                 mlx5_fs_egress_acls_cleanup(dev);
1284 }
1285
1286 /**
1287  * mlx5_eswitch_enable_locked - Enable eswitch
1288  * @esw:        Pointer to eswitch
1289  * @num_vfs:    Enable eswitch for given number of VFs. This is optional.
1290  *              Valid value are 0, > 0 and MLX5_ESWITCH_IGNORE_NUM_VFS.
1291  *              Caller should pass num_vfs > 0 when enabling eswitch for
1292  *              vf vports. Caller should pass num_vfs = 0, when eswitch
1293  *              is enabled without sriov VFs or when caller
1294  *              is unaware of the sriov state of the host PF on ECPF based
1295  *              eswitch. Caller should pass < 0 when num_vfs should be
1296  *              completely ignored. This is typically the case when eswitch
1297  *              is enabled without sriov regardless of PF/ECPF system.
1298  * mlx5_eswitch_enable_locked() Enables eswitch in either legacy or offloads
1299  * mode. If num_vfs >=0 is provided, it setup VF related eswitch vports.
1300  * It returns 0 on success or error code on failure.
1301  */
1302 int mlx5_eswitch_enable_locked(struct mlx5_eswitch *esw, int num_vfs)
1303 {
1304         int err;
1305
1306         lockdep_assert_held(&esw->mode_lock);
1307
1308         if (!MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, ft_support)) {
1309                 esw_warn(esw->dev, "FDB is not supported, aborting ...\n");
1310                 return -EOPNOTSUPP;
1311         }
1312
1313         mlx5_eswitch_get_devlink_param(esw);
1314
1315         err = mlx5_esw_acls_ns_init(esw);
1316         if (err)
1317                 return err;
1318
1319         mlx5_eswitch_update_num_of_vfs(esw, num_vfs);
1320
1321         if (esw->mode == MLX5_ESWITCH_LEGACY) {
1322                 err = esw_legacy_enable(esw);
1323         } else {
1324                 mlx5_rescan_drivers(esw->dev);
1325                 err = esw_offloads_enable(esw);
1326         }
1327
1328         if (err)
1329                 goto abort;
1330
1331         esw->fdb_table.flags |= MLX5_ESW_FDB_CREATED;
1332
1333         mlx5_eswitch_event_handlers_register(esw);
1334
1335         esw_info(esw->dev, "Enable: mode(%s), nvfs(%d), active vports(%d)\n",
1336                  esw->mode == MLX5_ESWITCH_LEGACY ? "LEGACY" : "OFFLOADS",
1337                  esw->esw_funcs.num_vfs, esw->enabled_vports);
1338
1339         mlx5_esw_mode_change_notify(esw, esw->mode);
1340
1341         return 0;
1342
1343 abort:
1344         mlx5_esw_acls_ns_cleanup(esw);
1345         return err;
1346 }
1347
1348 /**
1349  * mlx5_eswitch_enable - Enable eswitch
1350  * @esw:        Pointer to eswitch
1351  * @num_vfs:    Enable eswitch switch for given number of VFs.
1352  *              Caller must pass num_vfs > 0 when enabling eswitch for
1353  *              vf vports.
1354  * mlx5_eswitch_enable() returns 0 on success or error code on failure.
1355  */
1356 int mlx5_eswitch_enable(struct mlx5_eswitch *esw, int num_vfs)
1357 {
1358         bool toggle_lag;
1359         int ret;
1360
1361         if (!mlx5_esw_allowed(esw))
1362                 return 0;
1363
1364         devl_assert_locked(priv_to_devlink(esw->dev));
1365
1366         toggle_lag = !mlx5_esw_is_fdb_created(esw);
1367
1368         if (toggle_lag)
1369                 mlx5_lag_disable_change(esw->dev);
1370
1371         down_write(&esw->mode_lock);
1372         if (!mlx5_esw_is_fdb_created(esw)) {
1373                 ret = mlx5_eswitch_enable_locked(esw, num_vfs);
1374         } else {
1375                 enum mlx5_eswitch_vport_event vport_events;
1376
1377                 vport_events = (esw->mode == MLX5_ESWITCH_LEGACY) ?
1378                                         MLX5_LEGACY_SRIOV_VPORT_EVENTS : MLX5_VPORT_UC_ADDR_CHANGE;
1379                 ret = mlx5_eswitch_load_vf_vports(esw, num_vfs, vport_events);
1380                 if (!ret)
1381                         esw->esw_funcs.num_vfs = num_vfs;
1382         }
1383         up_write(&esw->mode_lock);
1384
1385         if (toggle_lag)
1386                 mlx5_lag_enable_change(esw->dev);
1387
1388         return ret;
1389 }
1390
1391 /* When disabling sriov, free driver level resources. */
1392 void mlx5_eswitch_disable_sriov(struct mlx5_eswitch *esw, bool clear_vf)
1393 {
1394         if (!mlx5_esw_allowed(esw))
1395                 return;
1396
1397         devl_assert_locked(priv_to_devlink(esw->dev));
1398         down_write(&esw->mode_lock);
1399         /* If driver is unloaded, this function is called twice by remove_one()
1400          * and mlx5_unload(). Prevent the second call.
1401          */
1402         if (!esw->esw_funcs.num_vfs && !clear_vf)
1403                 goto unlock;
1404
1405         esw_info(esw->dev, "Unload vfs: mode(%s), nvfs(%d), active vports(%d)\n",
1406                  esw->mode == MLX5_ESWITCH_LEGACY ? "LEGACY" : "OFFLOADS",
1407                  esw->esw_funcs.num_vfs, esw->enabled_vports);
1408
1409         mlx5_eswitch_unload_vf_vports(esw, esw->esw_funcs.num_vfs);
1410         if (clear_vf)
1411                 mlx5_eswitch_clear_vf_vports_info(esw);
1412
1413         if (esw->mode == MLX5_ESWITCH_OFFLOADS) {
1414                 struct devlink *devlink = priv_to_devlink(esw->dev);
1415
1416                 devl_rate_nodes_destroy(devlink);
1417         }
1418         /* Destroy legacy fdb when disabling sriov in legacy mode. */
1419         if (esw->mode == MLX5_ESWITCH_LEGACY)
1420                 mlx5_eswitch_disable_locked(esw);
1421
1422         esw->esw_funcs.num_vfs = 0;
1423
1424 unlock:
1425         up_write(&esw->mode_lock);
1426 }
1427
1428 /* Free resources for corresponding eswitch mode. It is called by devlink
1429  * when changing eswitch mode or modprobe when unloading driver.
1430  */
1431 void mlx5_eswitch_disable_locked(struct mlx5_eswitch *esw)
1432 {
1433         struct devlink *devlink = priv_to_devlink(esw->dev);
1434
1435         /* Notify eswitch users that it is exiting from current mode.
1436          * So that it can do necessary cleanup before the eswitch is disabled.
1437          */
1438         mlx5_esw_mode_change_notify(esw, MLX5_ESWITCH_LEGACY);
1439
1440         mlx5_eswitch_event_handlers_unregister(esw);
1441
1442         esw_info(esw->dev, "Disable: mode(%s), nvfs(%d), active vports(%d)\n",
1443                  esw->mode == MLX5_ESWITCH_LEGACY ? "LEGACY" : "OFFLOADS",
1444                  esw->esw_funcs.num_vfs, esw->enabled_vports);
1445
1446         if (esw->fdb_table.flags & MLX5_ESW_FDB_CREATED) {
1447                 esw->fdb_table.flags &= ~MLX5_ESW_FDB_CREATED;
1448                 if (esw->mode == MLX5_ESWITCH_OFFLOADS)
1449                         esw_offloads_disable(esw);
1450                 else if (esw->mode == MLX5_ESWITCH_LEGACY)
1451                         esw_legacy_disable(esw);
1452                 mlx5_esw_acls_ns_cleanup(esw);
1453         }
1454
1455         if (esw->mode == MLX5_ESWITCH_OFFLOADS)
1456                 devl_rate_nodes_destroy(devlink);
1457 }
1458
1459 void mlx5_eswitch_disable(struct mlx5_eswitch *esw)
1460 {
1461         if (!mlx5_esw_allowed(esw))
1462                 return;
1463
1464         devl_assert_locked(priv_to_devlink(esw->dev));
1465         mlx5_lag_disable_change(esw->dev);
1466         down_write(&esw->mode_lock);
1467         mlx5_eswitch_disable_locked(esw);
1468         esw->mode = MLX5_ESWITCH_LEGACY;
1469         up_write(&esw->mode_lock);
1470         mlx5_lag_enable_change(esw->dev);
1471 }
1472
1473 static int mlx5_query_hca_cap_host_pf(struct mlx5_core_dev *dev, void *out)
1474 {
1475         u16 opmod = (MLX5_CAP_GENERAL << 1) | (HCA_CAP_OPMOD_GET_MAX & 0x01);
1476         u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)] = {};
1477
1478         MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
1479         MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
1480         MLX5_SET(query_hca_cap_in, in, function_id, MLX5_VPORT_PF);
1481         MLX5_SET(query_hca_cap_in, in, other_function, true);
1482         return mlx5_cmd_exec_inout(dev, query_hca_cap, in, out);
1483 }
1484
1485 int mlx5_esw_sf_max_hpf_functions(struct mlx5_core_dev *dev, u16 *max_sfs, u16 *sf_base_id)
1486
1487 {
1488         int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
1489         void *query_ctx;
1490         void *hca_caps;
1491         int err;
1492
1493         if (!mlx5_core_is_ecpf(dev)) {
1494                 *max_sfs = 0;
1495                 return 0;
1496         }
1497
1498         query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
1499         if (!query_ctx)
1500                 return -ENOMEM;
1501
1502         err = mlx5_query_hca_cap_host_pf(dev, query_ctx);
1503         if (err)
1504                 goto out_free;
1505
1506         hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
1507         *max_sfs = MLX5_GET(cmd_hca_cap, hca_caps, max_num_sf);
1508         *sf_base_id = MLX5_GET(cmd_hca_cap, hca_caps, sf_base_id);
1509
1510 out_free:
1511         kfree(query_ctx);
1512         return err;
1513 }
1514
1515 static int mlx5_esw_vport_alloc(struct mlx5_eswitch *esw,
1516                                 int index, u16 vport_num)
1517 {
1518         struct mlx5_vport *vport;
1519         int err;
1520
1521         vport = kzalloc(sizeof(*vport), GFP_KERNEL);
1522         if (!vport)
1523                 return -ENOMEM;
1524
1525         vport->dev = esw->dev;
1526         vport->vport = vport_num;
1527         vport->index = index;
1528         vport->info.link_state = MLX5_VPORT_ADMIN_STATE_AUTO;
1529         INIT_WORK(&vport->vport_change_handler, esw_vport_change_handler);
1530         err = xa_insert(&esw->vports, vport_num, vport, GFP_KERNEL);
1531         if (err)
1532                 goto insert_err;
1533
1534         esw->total_vports++;
1535         return 0;
1536
1537 insert_err:
1538         kfree(vport);
1539         return err;
1540 }
1541
1542 static void mlx5_esw_vport_free(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
1543 {
1544         xa_erase(&esw->vports, vport->vport);
1545         kfree(vport);
1546 }
1547
1548 static void mlx5_esw_vports_cleanup(struct mlx5_eswitch *esw)
1549 {
1550         struct mlx5_vport *vport;
1551         unsigned long i;
1552
1553         mlx5_esw_for_each_vport(esw, i, vport)
1554                 mlx5_esw_vport_free(esw, vport);
1555         xa_destroy(&esw->vports);
1556 }
1557
1558 static int mlx5_esw_vports_init(struct mlx5_eswitch *esw)
1559 {
1560         struct mlx5_core_dev *dev = esw->dev;
1561         u16 max_host_pf_sfs;
1562         u16 base_sf_num;
1563         int idx = 0;
1564         int err;
1565         int i;
1566
1567         xa_init(&esw->vports);
1568
1569         err = mlx5_esw_vport_alloc(esw, idx, MLX5_VPORT_PF);
1570         if (err)
1571                 goto err;
1572         if (esw->first_host_vport == MLX5_VPORT_PF)
1573                 xa_set_mark(&esw->vports, idx, MLX5_ESW_VPT_HOST_FN);
1574         idx++;
1575
1576         for (i = 0; i < mlx5_core_max_vfs(dev); i++) {
1577                 err = mlx5_esw_vport_alloc(esw, idx, idx);
1578                 if (err)
1579                         goto err;
1580                 xa_set_mark(&esw->vports, idx, MLX5_ESW_VPT_VF);
1581                 xa_set_mark(&esw->vports, idx, MLX5_ESW_VPT_HOST_FN);
1582                 idx++;
1583         }
1584         base_sf_num = mlx5_sf_start_function_id(dev);
1585         for (i = 0; i < mlx5_sf_max_functions(dev); i++) {
1586                 err = mlx5_esw_vport_alloc(esw, idx, base_sf_num + i);
1587                 if (err)
1588                         goto err;
1589                 xa_set_mark(&esw->vports, base_sf_num + i, MLX5_ESW_VPT_SF);
1590                 idx++;
1591         }
1592
1593         err = mlx5_esw_sf_max_hpf_functions(dev, &max_host_pf_sfs, &base_sf_num);
1594         if (err)
1595                 goto err;
1596         for (i = 0; i < max_host_pf_sfs; i++) {
1597                 err = mlx5_esw_vport_alloc(esw, idx, base_sf_num + i);
1598                 if (err)
1599                         goto err;
1600                 xa_set_mark(&esw->vports, base_sf_num + i, MLX5_ESW_VPT_SF);
1601                 idx++;
1602         }
1603
1604         if (mlx5_ecpf_vport_exists(dev) ||
1605             mlx5_core_is_ecpf_esw_manager(dev)) {
1606                 err = mlx5_esw_vport_alloc(esw, idx, MLX5_VPORT_ECPF);
1607                 if (err)
1608                         goto err;
1609                 idx++;
1610         }
1611         err = mlx5_esw_vport_alloc(esw, idx, MLX5_VPORT_UPLINK);
1612         if (err)
1613                 goto err;
1614         return 0;
1615
1616 err:
1617         mlx5_esw_vports_cleanup(esw);
1618         return err;
1619 }
1620
1621 int mlx5_eswitch_init(struct mlx5_core_dev *dev)
1622 {
1623         struct mlx5_eswitch *esw;
1624         int err;
1625
1626         if (!MLX5_VPORT_MANAGER(dev) && !MLX5_ESWITCH_MANAGER(dev))
1627                 return 0;
1628
1629         esw = kzalloc(sizeof(*esw), GFP_KERNEL);
1630         if (!esw)
1631                 return -ENOMEM;
1632
1633         esw->dev = dev;
1634         esw->manager_vport = mlx5_eswitch_manager_vport(dev);
1635         esw->first_host_vport = mlx5_eswitch_first_host_vport_num(dev);
1636
1637         esw->work_queue = create_singlethread_workqueue("mlx5_esw_wq");
1638         if (!esw->work_queue) {
1639                 err = -ENOMEM;
1640                 goto abort;
1641         }
1642
1643         err = mlx5_esw_vports_init(esw);
1644         if (err)
1645                 goto abort;
1646
1647         err = esw_offloads_init(esw);
1648         if (err)
1649                 goto reps_err;
1650
1651         mutex_init(&esw->offloads.encap_tbl_lock);
1652         hash_init(esw->offloads.encap_tbl);
1653         mutex_init(&esw->offloads.decap_tbl_lock);
1654         hash_init(esw->offloads.decap_tbl);
1655         mlx5e_mod_hdr_tbl_init(&esw->offloads.mod_hdr);
1656         atomic64_set(&esw->offloads.num_flows, 0);
1657         ida_init(&esw->offloads.vport_metadata_ida);
1658         xa_init_flags(&esw->offloads.vhca_map, XA_FLAGS_ALLOC);
1659         mutex_init(&esw->state_lock);
1660         init_rwsem(&esw->mode_lock);
1661         refcount_set(&esw->qos.refcnt, 0);
1662
1663         esw->enabled_vports = 0;
1664         esw->mode = MLX5_ESWITCH_LEGACY;
1665         esw->offloads.inline_mode = MLX5_INLINE_MODE_NONE;
1666         if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, reformat) &&
1667             MLX5_CAP_ESW_FLOWTABLE_FDB(dev, decap))
1668                 esw->offloads.encap = DEVLINK_ESWITCH_ENCAP_MODE_BASIC;
1669         else
1670                 esw->offloads.encap = DEVLINK_ESWITCH_ENCAP_MODE_NONE;
1671         if (MLX5_ESWITCH_MANAGER(dev) &&
1672             mlx5_esw_vport_match_metadata_supported(esw))
1673                 esw->flags |= MLX5_ESWITCH_VPORT_MATCH_METADATA;
1674
1675         dev->priv.eswitch = esw;
1676         BLOCKING_INIT_NOTIFIER_HEAD(&esw->n_head);
1677
1678         esw_info(dev,
1679                  "Total vports %d, per vport: max uc(%d) max mc(%d)\n",
1680                  esw->total_vports,
1681                  MLX5_MAX_UC_PER_VPORT(dev),
1682                  MLX5_MAX_MC_PER_VPORT(dev));
1683         return 0;
1684
1685 reps_err:
1686         mlx5_esw_vports_cleanup(esw);
1687 abort:
1688         if (esw->work_queue)
1689                 destroy_workqueue(esw->work_queue);
1690         kfree(esw);
1691         return err;
1692 }
1693
1694 void mlx5_eswitch_cleanup(struct mlx5_eswitch *esw)
1695 {
1696         if (!esw)
1697                 return;
1698
1699         esw_info(esw->dev, "cleanup\n");
1700
1701         esw->dev->priv.eswitch = NULL;
1702         destroy_workqueue(esw->work_queue);
1703         WARN_ON(refcount_read(&esw->qos.refcnt));
1704         mutex_destroy(&esw->state_lock);
1705         WARN_ON(!xa_empty(&esw->offloads.vhca_map));
1706         xa_destroy(&esw->offloads.vhca_map);
1707         ida_destroy(&esw->offloads.vport_metadata_ida);
1708         mlx5e_mod_hdr_tbl_destroy(&esw->offloads.mod_hdr);
1709         mutex_destroy(&esw->offloads.encap_tbl_lock);
1710         mutex_destroy(&esw->offloads.decap_tbl_lock);
1711         esw_offloads_cleanup(esw);
1712         mlx5_esw_vports_cleanup(esw);
1713         kfree(esw);
1714 }
1715
1716 /* Vport Administration */
1717 static int
1718 mlx5_esw_set_vport_mac_locked(struct mlx5_eswitch *esw,
1719                               struct mlx5_vport *evport, const u8 *mac)
1720 {
1721         u16 vport_num = evport->vport;
1722         u64 node_guid;
1723         int err = 0;
1724
1725         if (is_multicast_ether_addr(mac))
1726                 return -EINVAL;
1727
1728         if (evport->info.spoofchk && !is_valid_ether_addr(mac))
1729                 mlx5_core_warn(esw->dev,
1730                                "Set invalid MAC while spoofchk is on, vport(%d)\n",
1731                                vport_num);
1732
1733         err = mlx5_modify_nic_vport_mac_address(esw->dev, vport_num, mac);
1734         if (err) {
1735                 mlx5_core_warn(esw->dev,
1736                                "Failed to mlx5_modify_nic_vport_mac vport(%d) err=(%d)\n",
1737                                vport_num, err);
1738                 return err;
1739         }
1740
1741         node_guid_gen_from_mac(&node_guid, mac);
1742         err = mlx5_modify_nic_vport_node_guid(esw->dev, vport_num, node_guid);
1743         if (err)
1744                 mlx5_core_warn(esw->dev,
1745                                "Failed to set vport %d node guid, err = %d. RDMA_CM will not function properly for this VF.\n",
1746                                vport_num, err);
1747
1748         ether_addr_copy(evport->info.mac, mac);
1749         evport->info.node_guid = node_guid;
1750         if (evport->enabled && esw->mode == MLX5_ESWITCH_LEGACY)
1751                 err = esw_acl_ingress_lgcy_setup(esw, evport);
1752
1753         return err;
1754 }
1755
1756 int mlx5_eswitch_set_vport_mac(struct mlx5_eswitch *esw,
1757                                u16 vport, const u8 *mac)
1758 {
1759         struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
1760         int err = 0;
1761
1762         if (IS_ERR(evport))
1763                 return PTR_ERR(evport);
1764
1765         mutex_lock(&esw->state_lock);
1766         err = mlx5_esw_set_vport_mac_locked(esw, evport, mac);
1767         mutex_unlock(&esw->state_lock);
1768         return err;
1769 }
1770
1771 static bool mlx5_esw_check_port_type(struct mlx5_eswitch *esw, u16 vport_num, xa_mark_t mark)
1772 {
1773         struct mlx5_vport *vport;
1774
1775         vport = mlx5_eswitch_get_vport(esw, vport_num);
1776         if (IS_ERR(vport))
1777                 return false;
1778
1779         return xa_get_mark(&esw->vports, vport_num, mark);
1780 }
1781
1782 bool mlx5_eswitch_is_vf_vport(struct mlx5_eswitch *esw, u16 vport_num)
1783 {
1784         return mlx5_esw_check_port_type(esw, vport_num, MLX5_ESW_VPT_VF);
1785 }
1786
1787 bool mlx5_esw_is_sf_vport(struct mlx5_eswitch *esw, u16 vport_num)
1788 {
1789         return mlx5_esw_check_port_type(esw, vport_num, MLX5_ESW_VPT_SF);
1790 }
1791
1792 int mlx5_eswitch_set_vport_state(struct mlx5_eswitch *esw,
1793                                  u16 vport, int link_state)
1794 {
1795         struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
1796         int opmod = MLX5_VPORT_STATE_OP_MOD_ESW_VPORT;
1797         int other_vport = 1;
1798         int err = 0;
1799
1800         if (!mlx5_esw_allowed(esw))
1801                 return -EPERM;
1802         if (IS_ERR(evport))
1803                 return PTR_ERR(evport);
1804
1805         if (vport == MLX5_VPORT_UPLINK) {
1806                 opmod = MLX5_VPORT_STATE_OP_MOD_UPLINK;
1807                 other_vport = 0;
1808                 vport = 0;
1809         }
1810         mutex_lock(&esw->state_lock);
1811         if (esw->mode != MLX5_ESWITCH_LEGACY) {
1812                 err = -EOPNOTSUPP;
1813                 goto unlock;
1814         }
1815
1816         err = mlx5_modify_vport_admin_state(esw->dev, opmod, vport, other_vport, link_state);
1817         if (err) {
1818                 mlx5_core_warn(esw->dev, "Failed to set vport %d link state, opmod = %d, err = %d",
1819                                vport, opmod, err);
1820                 goto unlock;
1821         }
1822
1823         evport->info.link_state = link_state;
1824
1825 unlock:
1826         mutex_unlock(&esw->state_lock);
1827         return err;
1828 }
1829
1830 int mlx5_eswitch_get_vport_config(struct mlx5_eswitch *esw,
1831                                   u16 vport, struct ifla_vf_info *ivi)
1832 {
1833         struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
1834
1835         if (IS_ERR(evport))
1836                 return PTR_ERR(evport);
1837
1838         memset(ivi, 0, sizeof(*ivi));
1839         ivi->vf = vport - 1;
1840
1841         mutex_lock(&esw->state_lock);
1842         ether_addr_copy(ivi->mac, evport->info.mac);
1843         ivi->linkstate = evport->info.link_state;
1844         ivi->vlan = evport->info.vlan;
1845         ivi->qos = evport->info.qos;
1846         ivi->spoofchk = evport->info.spoofchk;
1847         ivi->trusted = evport->info.trusted;
1848         if (evport->qos.enabled) {
1849                 ivi->min_tx_rate = evport->qos.min_rate;
1850                 ivi->max_tx_rate = evport->qos.max_rate;
1851         }
1852         mutex_unlock(&esw->state_lock);
1853
1854         return 0;
1855 }
1856
1857 int __mlx5_eswitch_set_vport_vlan(struct mlx5_eswitch *esw,
1858                                   u16 vport, u16 vlan, u8 qos, u8 set_flags)
1859 {
1860         struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
1861         bool vst_mode_steering = esw_vst_mode_is_steering(esw);
1862         int err = 0;
1863
1864         if (IS_ERR(evport))
1865                 return PTR_ERR(evport);
1866         if (vlan > 4095 || qos > 7)
1867                 return -EINVAL;
1868
1869         if (esw->mode == MLX5_ESWITCH_OFFLOADS || !vst_mode_steering) {
1870                 err = modify_esw_vport_cvlan(esw->dev, vport, vlan, qos, set_flags);
1871                 if (err)
1872                         return err;
1873         }
1874
1875         evport->info.vlan = vlan;
1876         evport->info.qos = qos;
1877         if (evport->enabled && esw->mode == MLX5_ESWITCH_LEGACY) {
1878                 err = esw_acl_ingress_lgcy_setup(esw, evport);
1879                 if (err)
1880                         return err;
1881                 err = esw_acl_egress_lgcy_setup(esw, evport);
1882         }
1883
1884         return err;
1885 }
1886
1887 int mlx5_eswitch_get_vport_stats(struct mlx5_eswitch *esw,
1888                                  u16 vport_num,
1889                                  struct ifla_vf_stats *vf_stats)
1890 {
1891         struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
1892         int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
1893         u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)] = {};
1894         struct mlx5_vport_drop_stats stats = {};
1895         int err = 0;
1896         u32 *out;
1897
1898         if (IS_ERR(vport))
1899                 return PTR_ERR(vport);
1900
1901         out = kvzalloc(outlen, GFP_KERNEL);
1902         if (!out)
1903                 return -ENOMEM;
1904
1905         MLX5_SET(query_vport_counter_in, in, opcode,
1906                  MLX5_CMD_OP_QUERY_VPORT_COUNTER);
1907         MLX5_SET(query_vport_counter_in, in, op_mod, 0);
1908         MLX5_SET(query_vport_counter_in, in, vport_number, vport->vport);
1909         MLX5_SET(query_vport_counter_in, in, other_vport, 1);
1910
1911         err = mlx5_cmd_exec_inout(esw->dev, query_vport_counter, in, out);
1912         if (err)
1913                 goto free_out;
1914
1915         #define MLX5_GET_CTR(p, x) \
1916                 MLX5_GET64(query_vport_counter_out, p, x)
1917
1918         memset(vf_stats, 0, sizeof(*vf_stats));
1919         vf_stats->rx_packets =
1920                 MLX5_GET_CTR(out, received_eth_unicast.packets) +
1921                 MLX5_GET_CTR(out, received_ib_unicast.packets) +
1922                 MLX5_GET_CTR(out, received_eth_multicast.packets) +
1923                 MLX5_GET_CTR(out, received_ib_multicast.packets) +
1924                 MLX5_GET_CTR(out, received_eth_broadcast.packets);
1925
1926         vf_stats->rx_bytes =
1927                 MLX5_GET_CTR(out, received_eth_unicast.octets) +
1928                 MLX5_GET_CTR(out, received_ib_unicast.octets) +
1929                 MLX5_GET_CTR(out, received_eth_multicast.octets) +
1930                 MLX5_GET_CTR(out, received_ib_multicast.octets) +
1931                 MLX5_GET_CTR(out, received_eth_broadcast.octets);
1932
1933         vf_stats->tx_packets =
1934                 MLX5_GET_CTR(out, transmitted_eth_unicast.packets) +
1935                 MLX5_GET_CTR(out, transmitted_ib_unicast.packets) +
1936                 MLX5_GET_CTR(out, transmitted_eth_multicast.packets) +
1937                 MLX5_GET_CTR(out, transmitted_ib_multicast.packets) +
1938                 MLX5_GET_CTR(out, transmitted_eth_broadcast.packets);
1939
1940         vf_stats->tx_bytes =
1941                 MLX5_GET_CTR(out, transmitted_eth_unicast.octets) +
1942                 MLX5_GET_CTR(out, transmitted_ib_unicast.octets) +
1943                 MLX5_GET_CTR(out, transmitted_eth_multicast.octets) +
1944                 MLX5_GET_CTR(out, transmitted_ib_multicast.octets) +
1945                 MLX5_GET_CTR(out, transmitted_eth_broadcast.octets);
1946
1947         vf_stats->multicast =
1948                 MLX5_GET_CTR(out, received_eth_multicast.packets) +
1949                 MLX5_GET_CTR(out, received_ib_multicast.packets);
1950
1951         vf_stats->broadcast =
1952                 MLX5_GET_CTR(out, received_eth_broadcast.packets);
1953
1954         err = mlx5_esw_query_vport_drop_stats(esw->dev, vport, &stats);
1955         if (err)
1956                 goto free_out;
1957         vf_stats->rx_dropped = stats.rx_dropped;
1958         vf_stats->tx_dropped = stats.tx_dropped;
1959
1960 free_out:
1961         kvfree(out);
1962         return err;
1963 }
1964
1965 u8 mlx5_eswitch_mode(const struct mlx5_core_dev *dev)
1966 {
1967         struct mlx5_eswitch *esw = dev->priv.eswitch;
1968
1969         return mlx5_esw_allowed(esw) ? esw->mode : MLX5_ESWITCH_LEGACY;
1970 }
1971 EXPORT_SYMBOL_GPL(mlx5_eswitch_mode);
1972
1973 enum devlink_eswitch_encap_mode
1974 mlx5_eswitch_get_encap_mode(const struct mlx5_core_dev *dev)
1975 {
1976         struct mlx5_eswitch *esw;
1977
1978         esw = dev->priv.eswitch;
1979         return (mlx5_eswitch_mode(dev) == MLX5_ESWITCH_OFFLOADS)  ? esw->offloads.encap :
1980                 DEVLINK_ESWITCH_ENCAP_MODE_NONE;
1981 }
1982 EXPORT_SYMBOL(mlx5_eswitch_get_encap_mode);
1983
1984 bool mlx5_esw_multipath_prereq(struct mlx5_core_dev *dev0,
1985                                struct mlx5_core_dev *dev1)
1986 {
1987         return (dev0->priv.eswitch->mode == MLX5_ESWITCH_OFFLOADS &&
1988                 dev1->priv.eswitch->mode == MLX5_ESWITCH_OFFLOADS);
1989 }
1990
1991 int mlx5_esw_event_notifier_register(struct mlx5_eswitch *esw, struct notifier_block *nb)
1992 {
1993         return blocking_notifier_chain_register(&esw->n_head, nb);
1994 }
1995
1996 void mlx5_esw_event_notifier_unregister(struct mlx5_eswitch *esw, struct notifier_block *nb)
1997 {
1998         blocking_notifier_chain_unregister(&esw->n_head, nb);
1999 }
2000
2001 /**
2002  * mlx5_esw_hold() - Try to take a read lock on esw mode lock.
2003  * @mdev: mlx5 core device.
2004  *
2005  * Should be called by esw resources callers.
2006  *
2007  * Return: true on success or false.
2008  */
2009 bool mlx5_esw_hold(struct mlx5_core_dev *mdev)
2010 {
2011         struct mlx5_eswitch *esw = mdev->priv.eswitch;
2012
2013         /* e.g. VF doesn't have eswitch so nothing to do */
2014         if (!mlx5_esw_allowed(esw))
2015                 return true;
2016
2017         if (down_read_trylock(&esw->mode_lock) != 0)
2018                 return true;
2019
2020         return false;
2021 }
2022
2023 /**
2024  * mlx5_esw_release() - Release a read lock on esw mode lock.
2025  * @mdev: mlx5 core device.
2026  */
2027 void mlx5_esw_release(struct mlx5_core_dev *mdev)
2028 {
2029         struct mlx5_eswitch *esw = mdev->priv.eswitch;
2030
2031         if (mlx5_esw_allowed(esw))
2032                 up_read(&esw->mode_lock);
2033 }
2034
2035 /**
2036  * mlx5_esw_get() - Increase esw user count.
2037  * @mdev: mlx5 core device.
2038  */
2039 void mlx5_esw_get(struct mlx5_core_dev *mdev)
2040 {
2041         struct mlx5_eswitch *esw = mdev->priv.eswitch;
2042
2043         if (mlx5_esw_allowed(esw))
2044                 atomic64_inc(&esw->user_count);
2045 }
2046
2047 /**
2048  * mlx5_esw_put() - Decrease esw user count.
2049  * @mdev: mlx5 core device.
2050  */
2051 void mlx5_esw_put(struct mlx5_core_dev *mdev)
2052 {
2053         struct mlx5_eswitch *esw = mdev->priv.eswitch;
2054
2055         if (mlx5_esw_allowed(esw))
2056                 atomic64_dec_if_positive(&esw->user_count);
2057 }
2058
2059 /**
2060  * mlx5_esw_try_lock() - Take a write lock on esw mode lock.
2061  * @esw: eswitch device.
2062  *
2063  * Should be called by esw mode change routine.
2064  *
2065  * Return:
2066  * * 0       - esw mode if successfully locked and refcount is 0.
2067  * * -EBUSY  - refcount is not 0.
2068  * * -EINVAL - In the middle of switching mode or lock is already held.
2069  */
2070 int mlx5_esw_try_lock(struct mlx5_eswitch *esw)
2071 {
2072         if (down_write_trylock(&esw->mode_lock) == 0)
2073                 return -EINVAL;
2074
2075         if (atomic64_read(&esw->user_count) > 0) {
2076                 up_write(&esw->mode_lock);
2077                 return -EBUSY;
2078         }
2079
2080         return esw->mode;
2081 }
2082
2083 /**
2084  * mlx5_esw_unlock() - Release write lock on esw mode lock
2085  * @esw: eswitch device.
2086  */
2087 void mlx5_esw_unlock(struct mlx5_eswitch *esw)
2088 {
2089         up_write(&esw->mode_lock);
2090 }
2091
2092 /**
2093  * mlx5_eswitch_get_total_vports - Get total vports of the eswitch
2094  *
2095  * @dev: Pointer to core device
2096  *
2097  * mlx5_eswitch_get_total_vports returns total number of eswitch vports.
2098  */
2099 u16 mlx5_eswitch_get_total_vports(const struct mlx5_core_dev *dev)
2100 {
2101         struct mlx5_eswitch *esw;
2102
2103         esw = dev->priv.eswitch;
2104         return mlx5_esw_allowed(esw) ? esw->total_vports : 0;
2105 }
2106 EXPORT_SYMBOL_GPL(mlx5_eswitch_get_total_vports);
2107
2108 /**
2109  * mlx5_eswitch_get_core_dev - Get the mdev device
2110  * @esw : eswitch device.
2111  *
2112  * Return the mellanox core device which manages the eswitch.
2113  */
2114 struct mlx5_core_dev *mlx5_eswitch_get_core_dev(struct mlx5_eswitch *esw)
2115 {
2116         return mlx5_esw_allowed(esw) ? esw->dev : NULL;
2117 }
2118 EXPORT_SYMBOL(mlx5_eswitch_get_core_dev);