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