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