mlxsw: Disable vlan_filtering for non .1D bridge
[cascardo/linux.git] / drivers / net / ethernet / mellanox / mlxsw / spectrum_switchdev.c
1 /*
2  * drivers/net/ethernet/mellanox/mlxsw/spectrum_switchdev.c
3  * Copyright (c) 2015 Mellanox Technologies. All rights reserved.
4  * Copyright (c) 2015 Jiri Pirko <jiri@mellanox.com>
5  * Copyright (c) 2015 Ido Schimmel <idosch@mellanox.com>
6  * Copyright (c) 2015 Elad Raz <eladr@mellanox.com>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the names of the copyright holders nor the names of its
17  *    contributors may be used to endorse or promote products derived from
18  *    this software without specific prior written permission.
19  *
20  * Alternatively, this software may be distributed under the terms of the
21  * GNU General Public License ("GPL") version 2 as published by the Free
22  * Software Foundation.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36
37 #include <linux/kernel.h>
38 #include <linux/types.h>
39 #include <linux/netdevice.h>
40 #include <linux/etherdevice.h>
41 #include <linux/slab.h>
42 #include <linux/device.h>
43 #include <linux/skbuff.h>
44 #include <linux/if_vlan.h>
45 #include <linux/if_bridge.h>
46 #include <linux/workqueue.h>
47 #include <linux/jiffies.h>
48 #include <net/switchdev.h>
49
50 #include "spectrum.h"
51 #include "core.h"
52 #include "reg.h"
53
54 static struct mlxsw_sp_port *
55 mlxsw_sp_port_orig_get(struct net_device *dev,
56                        struct mlxsw_sp_port *mlxsw_sp_port)
57 {
58         struct mlxsw_sp_port *mlxsw_sp_vport;
59         u16 vid;
60
61         if (!is_vlan_dev(dev))
62                 return mlxsw_sp_port;
63
64         vid = vlan_dev_vlan_id(dev);
65         mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
66         WARN_ON(!mlxsw_sp_vport);
67
68         return mlxsw_sp_vport;
69 }
70
71 static int mlxsw_sp_port_attr_get(struct net_device *dev,
72                                   struct switchdev_attr *attr)
73 {
74         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
75         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
76
77         mlxsw_sp_port = mlxsw_sp_port_orig_get(attr->orig_dev, mlxsw_sp_port);
78         if (!mlxsw_sp_port)
79                 return -EINVAL;
80
81         switch (attr->id) {
82         case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
83                 attr->u.ppid.id_len = sizeof(mlxsw_sp->base_mac);
84                 memcpy(&attr->u.ppid.id, &mlxsw_sp->base_mac,
85                        attr->u.ppid.id_len);
86                 break;
87         case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
88                 attr->u.brport_flags =
89                         (mlxsw_sp_port->learning ? BR_LEARNING : 0) |
90                         (mlxsw_sp_port->learning_sync ? BR_LEARNING_SYNC : 0) |
91                         (mlxsw_sp_port->uc_flood ? BR_FLOOD : 0);
92                 break;
93         default:
94                 return -EOPNOTSUPP;
95         }
96
97         return 0;
98 }
99
100 static int mlxsw_sp_port_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
101                                        u8 state)
102 {
103         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
104         enum mlxsw_reg_spms_state spms_state;
105         char *spms_pl;
106         u16 vid;
107         int err;
108
109         switch (state) {
110         case BR_STATE_DISABLED: /* fall-through */
111         case BR_STATE_FORWARDING:
112                 spms_state = MLXSW_REG_SPMS_STATE_FORWARDING;
113                 break;
114         case BR_STATE_LISTENING: /* fall-through */
115         case BR_STATE_LEARNING:
116                 spms_state = MLXSW_REG_SPMS_STATE_LEARNING;
117                 break;
118         case BR_STATE_BLOCKING:
119                 spms_state = MLXSW_REG_SPMS_STATE_DISCARDING;
120                 break;
121         default:
122                 BUG();
123         }
124
125         spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
126         if (!spms_pl)
127                 return -ENOMEM;
128         mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
129
130         if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
131                 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
132                 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
133         } else {
134                 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID)
135                         mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
136         }
137
138         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
139         kfree(spms_pl);
140         return err;
141 }
142
143 static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
144                                             struct switchdev_trans *trans,
145                                             u8 state)
146 {
147         if (switchdev_trans_ph_prepare(trans))
148                 return 0;
149
150         mlxsw_sp_port->stp_state = state;
151         return mlxsw_sp_port_stp_state_set(mlxsw_sp_port, state);
152 }
153
154 static bool mlxsw_sp_vfid_is_vport_br(u16 vfid)
155 {
156         return vfid >= MLXSW_SP_VFID_PORT_MAX;
157 }
158
159 static int __mlxsw_sp_port_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
160                                      u16 idx_begin, u16 idx_end, bool set,
161                                      bool only_uc)
162 {
163         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
164         u16 local_port = mlxsw_sp_port->local_port;
165         enum mlxsw_flood_table_type table_type;
166         u16 range = idx_end - idx_begin + 1;
167         char *sftr_pl;
168         int err;
169
170         if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
171                 table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID;
172                 if (mlxsw_sp_vfid_is_vport_br(idx_begin))
173                         local_port = mlxsw_sp_port->local_port;
174                 else
175                         local_port = MLXSW_PORT_CPU_PORT;
176         } else {
177                 table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST;
178         }
179
180         sftr_pl = kmalloc(MLXSW_REG_SFTR_LEN, GFP_KERNEL);
181         if (!sftr_pl)
182                 return -ENOMEM;
183
184         mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_UC, idx_begin,
185                             table_type, range, local_port, set);
186         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl);
187         if (err)
188                 goto buffer_out;
189
190         /* Flooding control allows one to decide whether a given port will
191          * flood unicast traffic for which there is no FDB entry.
192          */
193         if (only_uc)
194                 goto buffer_out;
195
196         mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_BM, idx_begin,
197                             table_type, range, local_port, set);
198         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl);
199
200 buffer_out:
201         kfree(sftr_pl);
202         return err;
203 }
204
205 static int mlxsw_sp_port_uc_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
206                                       bool set)
207 {
208         struct net_device *dev = mlxsw_sp_port->dev;
209         u16 vid, last_visited_vid;
210         int err;
211
212         if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
213                 u16 vfid = mlxsw_sp_vport_vfid_get(mlxsw_sp_port);
214
215                 return  __mlxsw_sp_port_flood_set(mlxsw_sp_port, vfid, vfid,
216                                                   set, true);
217         }
218
219         for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
220                 err = __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid, vid, set,
221                                                 true);
222                 if (err) {
223                         last_visited_vid = vid;
224                         goto err_port_flood_set;
225                 }
226         }
227
228         return 0;
229
230 err_port_flood_set:
231         for_each_set_bit(vid, mlxsw_sp_port->active_vlans, last_visited_vid)
232                 __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid, vid, !set, true);
233         netdev_err(dev, "Failed to configure unicast flooding\n");
234         return err;
235 }
236
237 int mlxsw_sp_vport_flood_set(struct mlxsw_sp_port *mlxsw_sp_vport, u16 vfid,
238                              bool set, bool only_uc)
239 {
240         /* In case of vFIDs, index into the flooding table is relative to
241          * the start of the vFIDs range.
242          */
243         return __mlxsw_sp_port_flood_set(mlxsw_sp_vport, vfid, vfid, set,
244                                          only_uc);
245 }
246
247 static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
248                                            struct switchdev_trans *trans,
249                                            unsigned long brport_flags)
250 {
251         unsigned long uc_flood = mlxsw_sp_port->uc_flood ? BR_FLOOD : 0;
252         bool set;
253         int err;
254
255         if (!mlxsw_sp_port->bridged)
256                 return -EINVAL;
257
258         if (switchdev_trans_ph_prepare(trans))
259                 return 0;
260
261         if ((uc_flood ^ brport_flags) & BR_FLOOD) {
262                 set = mlxsw_sp_port->uc_flood ? false : true;
263                 err = mlxsw_sp_port_uc_flood_set(mlxsw_sp_port, set);
264                 if (err)
265                         return err;
266         }
267
268         mlxsw_sp_port->uc_flood = brport_flags & BR_FLOOD ? 1 : 0;
269         mlxsw_sp_port->learning = brport_flags & BR_LEARNING ? 1 : 0;
270         mlxsw_sp_port->learning_sync = brport_flags & BR_LEARNING_SYNC ? 1 : 0;
271
272         return 0;
273 }
274
275 static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
276 {
277         char sfdat_pl[MLXSW_REG_SFDAT_LEN];
278         int err;
279
280         mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
281         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
282         if (err)
283                 return err;
284         mlxsw_sp->ageing_time = ageing_time;
285         return 0;
286 }
287
288 static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
289                                             struct switchdev_trans *trans,
290                                             unsigned long ageing_clock_t)
291 {
292         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
293         unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
294         u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
295
296         if (switchdev_trans_ph_prepare(trans))
297                 return 0;
298
299         return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
300 }
301
302 static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
303                                           struct switchdev_trans *trans,
304                                           struct net_device *orig_dev,
305                                           bool vlan_enabled)
306 {
307         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
308
309         /* SWITCHDEV_TRANS_PREPARE phase */
310         if ((!vlan_enabled) && (mlxsw_sp->master_bridge.dev == orig_dev)) {
311                 netdev_err(mlxsw_sp_port->dev, "Bridge must be vlan-aware\n");
312                 return -EINVAL;
313         }
314
315         return 0;
316 }
317
318 static int mlxsw_sp_port_attr_set(struct net_device *dev,
319                                   const struct switchdev_attr *attr,
320                                   struct switchdev_trans *trans)
321 {
322         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
323         int err = 0;
324
325         mlxsw_sp_port = mlxsw_sp_port_orig_get(attr->orig_dev, mlxsw_sp_port);
326         if (!mlxsw_sp_port)
327                 return -EINVAL;
328
329         switch (attr->id) {
330         case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
331                 err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port, trans,
332                                                        attr->u.stp_state);
333                 break;
334         case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
335                 err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port, trans,
336                                                       attr->u.brport_flags);
337                 break;
338         case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
339                 err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port, trans,
340                                                        attr->u.ageing_time);
341                 break;
342         case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
343                 err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port, trans,
344                                                      attr->orig_dev,
345                                                      attr->u.vlan_filtering);
346                 break;
347         default:
348                 err = -EOPNOTSUPP;
349                 break;
350         }
351
352         return err;
353 }
354
355 static int mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
356 {
357         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
358         char spvid_pl[MLXSW_REG_SPVID_LEN];
359
360         mlxsw_reg_spvid_pack(spvid_pl, mlxsw_sp_port->local_port, vid);
361         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvid), spvid_pl);
362 }
363
364 static int mlxsw_sp_fid_create(struct mlxsw_sp *mlxsw_sp, u16 fid)
365 {
366         char sfmr_pl[MLXSW_REG_SFMR_LEN];
367         int err;
368
369         mlxsw_reg_sfmr_pack(sfmr_pl, MLXSW_REG_SFMR_OP_CREATE_FID, fid, fid);
370         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfmr), sfmr_pl);
371
372         if (err)
373                 return err;
374
375         set_bit(fid, mlxsw_sp->active_fids);
376         return 0;
377 }
378
379 static void mlxsw_sp_fid_destroy(struct mlxsw_sp *mlxsw_sp, u16 fid)
380 {
381         char sfmr_pl[MLXSW_REG_SFMR_LEN];
382
383         clear_bit(fid, mlxsw_sp->active_fids);
384
385         mlxsw_reg_sfmr_pack(sfmr_pl, MLXSW_REG_SFMR_OP_DESTROY_FID,
386                             fid, fid);
387         mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfmr), sfmr_pl);
388 }
389
390 static int mlxsw_sp_port_fid_map(struct mlxsw_sp_port *mlxsw_sp_port, u16 fid)
391 {
392         enum mlxsw_reg_svfa_mt mt;
393
394         if (!list_empty(&mlxsw_sp_port->vports_list))
395                 mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID;
396         else
397                 mt = MLXSW_REG_SVFA_MT_VID_TO_FID;
398
399         return mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, true, fid, fid);
400 }
401
402 static int mlxsw_sp_port_fid_unmap(struct mlxsw_sp_port *mlxsw_sp_port, u16 fid)
403 {
404         enum mlxsw_reg_svfa_mt mt;
405
406         if (list_empty(&mlxsw_sp_port->vports_list))
407                 return 0;
408
409         mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID;
410         return mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, false, fid, fid);
411 }
412
413 static int mlxsw_sp_port_add_vids(struct net_device *dev, u16 vid_begin,
414                                   u16 vid_end)
415 {
416         u16 vid;
417         int err;
418
419         for (vid = vid_begin; vid <= vid_end; vid++) {
420                 err = mlxsw_sp_port_add_vid(dev, 0, vid);
421                 if (err)
422                         goto err_port_add_vid;
423         }
424         return 0;
425
426 err_port_add_vid:
427         for (vid--; vid >= vid_begin; vid--)
428                 mlxsw_sp_port_kill_vid(dev, 0, vid);
429         return err;
430 }
431
432 static int __mlxsw_sp_port_vlans_set(struct mlxsw_sp_port *mlxsw_sp_port,
433                                      u16 vid_begin, u16 vid_end, bool is_member,
434                                      bool untagged)
435 {
436         u16 vid, vid_e;
437         int err;
438
439         for (vid = vid_begin; vid <= vid_end;
440              vid += MLXSW_REG_SPVM_REC_MAX_COUNT) {
441                 vid_e = min((u16) (vid + MLXSW_REG_SPVM_REC_MAX_COUNT - 1),
442                             vid_end);
443
444                 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid_e,
445                                              is_member, untagged);
446                 if (err)
447                         return err;
448         }
449
450         return 0;
451 }
452
453 static int __mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
454                                      u16 vid_begin, u16 vid_end,
455                                      bool flag_untagged, bool flag_pvid)
456 {
457         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
458         struct net_device *dev = mlxsw_sp_port->dev;
459         u16 vid, last_visited_vid, old_pvid;
460         enum mlxsw_reg_svfa_mt mt;
461         int err;
462
463         /* In case this is invoked with BRIDGE_FLAGS_SELF and port is
464          * not bridged, then packets ingressing through the port with
465          * the specified VIDs will be directed to CPU.
466          */
467         if (!mlxsw_sp_port->bridged)
468                 return mlxsw_sp_port_add_vids(dev, vid_begin, vid_end);
469
470         for (vid = vid_begin; vid <= vid_end; vid++) {
471                 if (!test_bit(vid, mlxsw_sp->active_fids)) {
472                         err = mlxsw_sp_fid_create(mlxsw_sp, vid);
473                         if (err) {
474                                 netdev_err(dev, "Failed to create FID=%d\n",
475                                            vid);
476                                 return err;
477                         }
478
479                         /* When creating a FID, we set a VID to FID mapping
480                          * regardless of the port's mode.
481                          */
482                         mt = MLXSW_REG_SVFA_MT_VID_TO_FID;
483                         err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt,
484                                                            true, vid, vid);
485                         if (err) {
486                                 netdev_err(dev, "Failed to create FID=VID=%d mapping\n",
487                                            vid);
488                                 goto err_port_vid_to_fid_set;
489                         }
490                 }
491         }
492
493         /* Set FID mapping according to port's mode */
494         for (vid = vid_begin; vid <= vid_end; vid++) {
495                 err = mlxsw_sp_port_fid_map(mlxsw_sp_port, vid);
496                 if (err) {
497                         netdev_err(dev, "Failed to map FID=%d", vid);
498                         last_visited_vid = --vid;
499                         goto err_port_fid_map;
500                 }
501         }
502
503         err = __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid_begin, vid_end,
504                                         true, false);
505         if (err) {
506                 netdev_err(dev, "Failed to configure flooding\n");
507                 goto err_port_flood_set;
508         }
509
510         err = __mlxsw_sp_port_vlans_set(mlxsw_sp_port, vid_begin, vid_end,
511                                         true, flag_untagged);
512         if (err) {
513                 netdev_err(dev, "Unable to add VIDs %d-%d\n", vid_begin,
514                            vid_end);
515                 goto err_port_vlans_set;
516         }
517
518         old_pvid = mlxsw_sp_port->pvid;
519         if (flag_pvid && old_pvid != vid_begin) {
520                 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid_begin);
521                 if (err) {
522                         netdev_err(dev, "Unable to add PVID %d\n", vid_begin);
523                         goto err_port_pvid_set;
524                 }
525                 mlxsw_sp_port->pvid = vid_begin;
526         }
527
528         /* Changing activity bits only if HW operation succeded */
529         for (vid = vid_begin; vid <= vid_end; vid++)
530                 set_bit(vid, mlxsw_sp_port->active_vlans);
531
532         /* STP state change must be done after we set active VLANs */
533         err = mlxsw_sp_port_stp_state_set(mlxsw_sp_port,
534                                           mlxsw_sp_port->stp_state);
535         if (err) {
536                 netdev_err(dev, "Failed to set STP state\n");
537                 goto err_port_stp_state_set;
538         }
539
540         return 0;
541
542 err_port_vid_to_fid_set:
543         mlxsw_sp_fid_destroy(mlxsw_sp, vid);
544         return err;
545
546 err_port_stp_state_set:
547         for (vid = vid_begin; vid <= vid_end; vid++)
548                 clear_bit(vid, mlxsw_sp_port->active_vlans);
549         if (old_pvid != mlxsw_sp_port->pvid)
550                 mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid);
551 err_port_pvid_set:
552         __mlxsw_sp_port_vlans_set(mlxsw_sp_port, vid_begin, vid_end, false,
553                                   false);
554 err_port_vlans_set:
555         __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid_begin, vid_end, false,
556                                   false);
557 err_port_flood_set:
558         last_visited_vid = vid_end;
559 err_port_fid_map:
560         for (vid = last_visited_vid; vid >= vid_begin; vid--)
561                 mlxsw_sp_port_fid_unmap(mlxsw_sp_port, vid);
562         return err;
563 }
564
565 static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
566                                    const struct switchdev_obj_port_vlan *vlan,
567                                    struct switchdev_trans *trans)
568 {
569         bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
570         bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
571
572         if (switchdev_trans_ph_prepare(trans))
573                 return 0;
574
575         return __mlxsw_sp_port_vlans_add(mlxsw_sp_port,
576                                          vlan->vid_begin, vlan->vid_end,
577                                          flag_untagged, flag_pvid);
578 }
579
580 static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
581 {
582         return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
583                          MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY;
584 }
585
586 static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
587 {
588         return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
589                         MLXSW_REG_SFD_OP_WRITE_REMOVE;
590 }
591
592 static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp_port *mlxsw_sp_port,
593                                    const char *mac, u16 fid, bool adding,
594                                    bool dynamic)
595 {
596         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
597         char *sfd_pl;
598         int err;
599
600         sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
601         if (!sfd_pl)
602                 return -ENOMEM;
603
604         mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
605         mlxsw_reg_sfd_uc_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
606                               mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
607                               mlxsw_sp_port->local_port);
608         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
609         kfree(sfd_pl);
610
611         return err;
612 }
613
614 static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
615                                        const char *mac, u16 fid, u16 lag_vid,
616                                        bool adding, bool dynamic)
617 {
618         char *sfd_pl;
619         int err;
620
621         sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
622         if (!sfd_pl)
623                 return -ENOMEM;
624
625         mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
626         mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
627                                   mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
628                                   lag_vid, lag_id);
629         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
630         kfree(sfd_pl);
631
632         return err;
633 }
634
635 static int
636 mlxsw_sp_port_fdb_static_add(struct mlxsw_sp_port *mlxsw_sp_port,
637                              const struct switchdev_obj_port_fdb *fdb,
638                              struct switchdev_trans *trans)
639 {
640         u16 fid = fdb->vid;
641         u16 lag_vid = 0;
642
643         if (switchdev_trans_ph_prepare(trans))
644                 return 0;
645
646         if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
647                 u16 vfid = mlxsw_sp_vport_vfid_get(mlxsw_sp_port);
648
649                 fid = mlxsw_sp_vfid_to_fid(vfid);
650                 lag_vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
651         }
652
653         if (!fid)
654                 fid = mlxsw_sp_port->pvid;
655
656         if (!mlxsw_sp_port->lagged)
657                 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp_port,
658                                                fdb->addr, fid, true, false);
659         else
660                 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp_port->mlxsw_sp,
661                                                    mlxsw_sp_port->lag_id,
662                                                    fdb->addr, fid, lag_vid,
663                                                    true, false);
664 }
665
666 static int mlxsw_sp_port_obj_add(struct net_device *dev,
667                                  const struct switchdev_obj *obj,
668                                  struct switchdev_trans *trans)
669 {
670         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
671         int err = 0;
672
673         mlxsw_sp_port = mlxsw_sp_port_orig_get(obj->orig_dev, mlxsw_sp_port);
674         if (!mlxsw_sp_port)
675                 return -EINVAL;
676
677         switch (obj->id) {
678         case SWITCHDEV_OBJ_ID_PORT_VLAN:
679                 if (mlxsw_sp_port_is_vport(mlxsw_sp_port))
680                         return 0;
681
682                 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port,
683                                               SWITCHDEV_OBJ_PORT_VLAN(obj),
684                                               trans);
685                 break;
686         case SWITCHDEV_OBJ_ID_PORT_FDB:
687                 err = mlxsw_sp_port_fdb_static_add(mlxsw_sp_port,
688                                                    SWITCHDEV_OBJ_PORT_FDB(obj),
689                                                    trans);
690                 break;
691         default:
692                 err = -EOPNOTSUPP;
693                 break;
694         }
695
696         return err;
697 }
698
699 static int mlxsw_sp_port_kill_vids(struct net_device *dev, u16 vid_begin,
700                                    u16 vid_end)
701 {
702         u16 vid;
703         int err;
704
705         for (vid = vid_begin; vid <= vid_end; vid++) {
706                 err = mlxsw_sp_port_kill_vid(dev, 0, vid);
707                 if (err)
708                         return err;
709         }
710
711         return 0;
712 }
713
714 static int __mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
715                                      u16 vid_begin, u16 vid_end, bool init)
716 {
717         struct net_device *dev = mlxsw_sp_port->dev;
718         u16 vid, pvid;
719         int err;
720
721         /* In case this is invoked with BRIDGE_FLAGS_SELF and port is
722          * not bridged, then prevent packets ingressing through the
723          * port with the specified VIDs from being trapped to CPU.
724          */
725         if (!init && !mlxsw_sp_port->bridged)
726                 return mlxsw_sp_port_kill_vids(dev, vid_begin, vid_end);
727
728         err = __mlxsw_sp_port_vlans_set(mlxsw_sp_port, vid_begin, vid_end,
729                                         false, false);
730         if (err) {
731                 netdev_err(dev, "Unable to del VIDs %d-%d\n", vid_begin,
732                            vid_end);
733                 return err;
734         }
735
736         pvid = mlxsw_sp_port->pvid;
737         if (pvid >= vid_begin && pvid <= vid_end && pvid != 1) {
738                 /* Default VLAN is always 1 */
739                 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, 1);
740                 if (err) {
741                         netdev_err(dev, "Unable to del PVID %d\n", pvid);
742                         return err;
743                 }
744                 mlxsw_sp_port->pvid = 1;
745         }
746
747         if (init)
748                 goto out;
749
750         err = __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid_begin, vid_end,
751                                         false, false);
752         if (err) {
753                 netdev_err(dev, "Failed to clear flooding\n");
754                 return err;
755         }
756
757         for (vid = vid_begin; vid <= vid_end; vid++) {
758                 /* Remove FID mapping in case of Virtual mode */
759                 err = mlxsw_sp_port_fid_unmap(mlxsw_sp_port, vid);
760                 if (err) {
761                         netdev_err(dev, "Failed to unmap FID=%d", vid);
762                         return err;
763                 }
764         }
765
766 out:
767         /* Changing activity bits only if HW operation succeded */
768         for (vid = vid_begin; vid <= vid_end; vid++)
769                 clear_bit(vid, mlxsw_sp_port->active_vlans);
770
771         return 0;
772 }
773
774 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
775                                    const struct switchdev_obj_port_vlan *vlan)
776 {
777         return __mlxsw_sp_port_vlans_del(mlxsw_sp_port,
778                                          vlan->vid_begin, vlan->vid_end, false);
779 }
780
781 static int
782 mlxsw_sp_port_fdb_static_del(struct mlxsw_sp_port *mlxsw_sp_port,
783                              const struct switchdev_obj_port_fdb *fdb)
784 {
785         u16 fid = fdb->vid;
786         u16 lag_vid = 0;
787
788         if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
789                 u16 vfid = mlxsw_sp_vport_vfid_get(mlxsw_sp_port);
790
791                 fid = mlxsw_sp_vfid_to_fid(vfid);
792                 lag_vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
793         }
794
795         if (!mlxsw_sp_port->lagged)
796                 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp_port,
797                                                fdb->addr, fid,
798                                                false, false);
799         else
800                 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp_port->mlxsw_sp,
801                                                    mlxsw_sp_port->lag_id,
802                                                    fdb->addr, fid, lag_vid,
803                                                    false, false);
804 }
805
806 static int mlxsw_sp_port_obj_del(struct net_device *dev,
807                                  const struct switchdev_obj *obj)
808 {
809         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
810         int err = 0;
811
812         mlxsw_sp_port = mlxsw_sp_port_orig_get(obj->orig_dev, mlxsw_sp_port);
813         if (!mlxsw_sp_port)
814                 return -EINVAL;
815
816         switch (obj->id) {
817         case SWITCHDEV_OBJ_ID_PORT_VLAN:
818                 if (mlxsw_sp_port_is_vport(mlxsw_sp_port))
819                         return 0;
820
821                 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
822                                               SWITCHDEV_OBJ_PORT_VLAN(obj));
823                 break;
824         case SWITCHDEV_OBJ_ID_PORT_FDB:
825                 err = mlxsw_sp_port_fdb_static_del(mlxsw_sp_port,
826                                                    SWITCHDEV_OBJ_PORT_FDB(obj));
827                 break;
828         default:
829                 err = -EOPNOTSUPP;
830                 break;
831         }
832
833         return err;
834 }
835
836 static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
837                                                    u16 lag_id)
838 {
839         struct mlxsw_sp_port *mlxsw_sp_port;
840         int i;
841
842         for (i = 0; i < MLXSW_SP_PORT_PER_LAG_MAX; i++) {
843                 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
844                 if (mlxsw_sp_port)
845                         return mlxsw_sp_port;
846         }
847         return NULL;
848 }
849
850 static int mlxsw_sp_port_fdb_dump(struct mlxsw_sp_port *mlxsw_sp_port,
851                                   struct switchdev_obj_port_fdb *fdb,
852                                   switchdev_obj_dump_cb_t *cb)
853 {
854         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
855         u16 vport_vid = 0, vport_fid = 0;
856         char *sfd_pl;
857         char mac[ETH_ALEN];
858         u16 fid;
859         u8 local_port;
860         u16 lag_id;
861         u8 num_rec;
862         int stored_err = 0;
863         int i;
864         int err;
865
866         sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
867         if (!sfd_pl)
868                 return -ENOMEM;
869
870         if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
871                 u16 tmp;
872
873                 tmp = mlxsw_sp_vport_vfid_get(mlxsw_sp_port);
874                 vport_fid = mlxsw_sp_vfid_to_fid(tmp);
875                 vport_vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
876         }
877
878         mlxsw_reg_sfd_pack(sfd_pl, MLXSW_REG_SFD_OP_QUERY_DUMP, 0);
879         do {
880                 mlxsw_reg_sfd_num_rec_set(sfd_pl, MLXSW_REG_SFD_REC_MAX_COUNT);
881                 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
882                 if (err)
883                         goto out;
884
885                 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
886
887                 /* Even in case of error, we have to run the dump to the end
888                  * so the session in firmware is finished.
889                  */
890                 if (stored_err)
891                         continue;
892
893                 for (i = 0; i < num_rec; i++) {
894                         switch (mlxsw_reg_sfd_rec_type_get(sfd_pl, i)) {
895                         case MLXSW_REG_SFD_REC_TYPE_UNICAST:
896                                 mlxsw_reg_sfd_uc_unpack(sfd_pl, i, mac, &fid,
897                                                         &local_port);
898                                 if (local_port == mlxsw_sp_port->local_port) {
899                                         if (vport_fid && vport_fid != fid)
900                                                 continue;
901                                         else if (vport_fid)
902                                                 fdb->vid = vport_vid;
903                                         else
904                                                 fdb->vid = fid;
905                                         ether_addr_copy(fdb->addr, mac);
906                                         fdb->ndm_state = NUD_REACHABLE;
907                                         err = cb(&fdb->obj);
908                                         if (err)
909                                                 stored_err = err;
910                                 }
911                                 break;
912                         case MLXSW_REG_SFD_REC_TYPE_UNICAST_LAG:
913                                 mlxsw_reg_sfd_uc_lag_unpack(sfd_pl, i,
914                                                             mac, &fid, &lag_id);
915                                 if (mlxsw_sp_port ==
916                                     mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id)) {
917                                         if (vport_fid && vport_fid != fid)
918                                                 continue;
919                                         else if (vport_fid)
920                                                 fdb->vid = vport_vid;
921                                         else
922                                                 fdb->vid = fid;
923                                         ether_addr_copy(fdb->addr, mac);
924                                         fdb->ndm_state = NUD_REACHABLE;
925                                         err = cb(&fdb->obj);
926                                         if (err)
927                                                 stored_err = err;
928                                 }
929                                 break;
930                         }
931                 }
932         } while (num_rec == MLXSW_REG_SFD_REC_MAX_COUNT);
933
934 out:
935         kfree(sfd_pl);
936         return stored_err ? stored_err : err;
937 }
938
939 static int mlxsw_sp_port_vlan_dump(struct mlxsw_sp_port *mlxsw_sp_port,
940                                    struct switchdev_obj_port_vlan *vlan,
941                                    switchdev_obj_dump_cb_t *cb)
942 {
943         u16 vid;
944         int err = 0;
945
946         if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
947                 vlan->flags = 0;
948                 vlan->vid_begin = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
949                 vlan->vid_end = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
950                 return cb(&vlan->obj);
951         }
952
953         for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
954                 vlan->flags = 0;
955                 if (vid == mlxsw_sp_port->pvid)
956                         vlan->flags |= BRIDGE_VLAN_INFO_PVID;
957                 vlan->vid_begin = vid;
958                 vlan->vid_end = vid;
959                 err = cb(&vlan->obj);
960                 if (err)
961                         break;
962         }
963         return err;
964 }
965
966 static int mlxsw_sp_port_obj_dump(struct net_device *dev,
967                                   struct switchdev_obj *obj,
968                                   switchdev_obj_dump_cb_t *cb)
969 {
970         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
971         int err = 0;
972
973         mlxsw_sp_port = mlxsw_sp_port_orig_get(obj->orig_dev, mlxsw_sp_port);
974         if (!mlxsw_sp_port)
975                 return -EINVAL;
976
977         switch (obj->id) {
978         case SWITCHDEV_OBJ_ID_PORT_VLAN:
979                 err = mlxsw_sp_port_vlan_dump(mlxsw_sp_port,
980                                               SWITCHDEV_OBJ_PORT_VLAN(obj), cb);
981                 break;
982         case SWITCHDEV_OBJ_ID_PORT_FDB:
983                 err = mlxsw_sp_port_fdb_dump(mlxsw_sp_port,
984                                              SWITCHDEV_OBJ_PORT_FDB(obj), cb);
985                 break;
986         default:
987                 err = -EOPNOTSUPP;
988                 break;
989         }
990
991         return err;
992 }
993
994 static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = {
995         .switchdev_port_attr_get        = mlxsw_sp_port_attr_get,
996         .switchdev_port_attr_set        = mlxsw_sp_port_attr_set,
997         .switchdev_port_obj_add         = mlxsw_sp_port_obj_add,
998         .switchdev_port_obj_del         = mlxsw_sp_port_obj_del,
999         .switchdev_port_obj_dump        = mlxsw_sp_port_obj_dump,
1000 };
1001
1002 static void mlxsw_sp_fdb_call_notifiers(bool learning, bool learning_sync,
1003                                         bool adding, char *mac, u16 vid,
1004                                         struct net_device *dev)
1005 {
1006         struct switchdev_notifier_fdb_info info;
1007         unsigned long notifier_type;
1008
1009         if (learning && learning_sync) {
1010                 info.addr = mac;
1011                 info.vid = vid;
1012                 notifier_type = adding ? SWITCHDEV_FDB_ADD : SWITCHDEV_FDB_DEL;
1013                 call_switchdev_notifiers(notifier_type, dev, &info.info);
1014         }
1015 }
1016
1017 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
1018                                             char *sfn_pl, int rec_index,
1019                                             bool adding)
1020 {
1021         struct mlxsw_sp_port *mlxsw_sp_port;
1022         char mac[ETH_ALEN];
1023         u8 local_port;
1024         u16 vid, fid;
1025         int err;
1026
1027         mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
1028         mlxsw_sp_port = mlxsw_sp->ports[local_port];
1029         if (!mlxsw_sp_port) {
1030                 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
1031                 return;
1032         }
1033
1034         if (mlxsw_sp_fid_is_vfid(fid)) {
1035                 u16 vfid = mlxsw_sp_fid_to_vfid(fid);
1036                 struct mlxsw_sp_port *mlxsw_sp_vport;
1037
1038                 mlxsw_sp_vport = mlxsw_sp_port_vport_find_by_vfid(mlxsw_sp_port,
1039                                                                   vfid);
1040                 if (!mlxsw_sp_vport) {
1041                         netdev_err(mlxsw_sp_port->dev, "Failed to find a matching vPort following FDB notification\n");
1042                         return;
1043                 }
1044
1045                 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport);
1046                 /* Override the physical port with the vPort. */
1047                 mlxsw_sp_port = mlxsw_sp_vport;
1048         } else {
1049                 vid = fid;
1050         }
1051
1052         err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp_port, mac, fid,
1053                                       adding && mlxsw_sp_port->learning, true);
1054         if (err) {
1055                 if (net_ratelimit())
1056                         netdev_err(mlxsw_sp_port->dev, "Failed to set FDB entry\n");
1057                 return;
1058         }
1059
1060         mlxsw_sp_fdb_call_notifiers(mlxsw_sp_port->learning,
1061                                     mlxsw_sp_port->learning_sync,
1062                                     adding, mac, vid, mlxsw_sp_port->dev);
1063 }
1064
1065 static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
1066                                                 char *sfn_pl, int rec_index,
1067                                                 bool adding)
1068 {
1069         struct mlxsw_sp_port *mlxsw_sp_port;
1070         char mac[ETH_ALEN];
1071         u16 lag_vid = 0;
1072         u16 lag_id;
1073         u16 vid, fid;
1074         int err;
1075
1076         mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
1077         mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
1078         if (!mlxsw_sp_port) {
1079                 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
1080                 return;
1081         }
1082
1083         if (mlxsw_sp_fid_is_vfid(fid)) {
1084                 u16 vfid = mlxsw_sp_fid_to_vfid(fid);
1085                 struct mlxsw_sp_port *mlxsw_sp_vport;
1086
1087                 mlxsw_sp_vport = mlxsw_sp_port_vport_find_by_vfid(mlxsw_sp_port,
1088                                                                   vfid);
1089                 if (!mlxsw_sp_vport) {
1090                         netdev_err(mlxsw_sp_port->dev, "Failed to find a matching vPort following FDB notification\n");
1091                         return;
1092                 }
1093
1094                 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport);
1095                 lag_vid = vid;
1096                 /* Override the physical port with the vPort. */
1097                 mlxsw_sp_port = mlxsw_sp_vport;
1098         } else {
1099                 vid = fid;
1100         }
1101
1102         err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
1103                                           adding && mlxsw_sp_port->learning,
1104                                           true);
1105         if (err) {
1106                 if (net_ratelimit())
1107                         netdev_err(mlxsw_sp_port->dev, "Failed to set FDB entry\n");
1108                 return;
1109         }
1110
1111         mlxsw_sp_fdb_call_notifiers(mlxsw_sp_port->learning,
1112                                     mlxsw_sp_port->learning_sync,
1113                                     adding, mac, vid,
1114                                     mlxsw_sp_lag_get(mlxsw_sp, lag_id)->dev);
1115 }
1116
1117 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
1118                                             char *sfn_pl, int rec_index)
1119 {
1120         switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
1121         case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
1122                 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
1123                                                 rec_index, true);
1124                 break;
1125         case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
1126                 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
1127                                                 rec_index, false);
1128                 break;
1129         case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
1130                 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
1131                                                     rec_index, true);
1132                 break;
1133         case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
1134                 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
1135                                                     rec_index, false);
1136                 break;
1137         }
1138 }
1139
1140 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp)
1141 {
1142         schedule_delayed_work(&mlxsw_sp->fdb_notify.dw,
1143                               msecs_to_jiffies(mlxsw_sp->fdb_notify.interval));
1144 }
1145
1146 static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
1147 {
1148         struct mlxsw_sp *mlxsw_sp;
1149         char *sfn_pl;
1150         u8 num_rec;
1151         int i;
1152         int err;
1153
1154         sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
1155         if (!sfn_pl)
1156                 return;
1157
1158         mlxsw_sp = container_of(work, struct mlxsw_sp, fdb_notify.dw.work);
1159
1160         do {
1161                 mlxsw_reg_sfn_pack(sfn_pl);
1162                 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
1163                 if (err) {
1164                         dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
1165                         break;
1166                 }
1167                 num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
1168                 for (i = 0; i < num_rec; i++)
1169                         mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
1170
1171         } while (num_rec);
1172
1173         kfree(sfn_pl);
1174         mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
1175 }
1176
1177 static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
1178 {
1179         int err;
1180
1181         err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
1182         if (err) {
1183                 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
1184                 return err;
1185         }
1186         INIT_DELAYED_WORK(&mlxsw_sp->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
1187         mlxsw_sp->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
1188         mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
1189         return 0;
1190 }
1191
1192 static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
1193 {
1194         cancel_delayed_work_sync(&mlxsw_sp->fdb_notify.dw);
1195 }
1196
1197 static void mlxsw_sp_fids_fini(struct mlxsw_sp *mlxsw_sp)
1198 {
1199         u16 fid;
1200
1201         for_each_set_bit(fid, mlxsw_sp->active_fids, VLAN_N_VID)
1202                 mlxsw_sp_fid_destroy(mlxsw_sp, fid);
1203 }
1204
1205 int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
1206 {
1207         return mlxsw_sp_fdb_init(mlxsw_sp);
1208 }
1209
1210 void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
1211 {
1212         mlxsw_sp_fdb_fini(mlxsw_sp);
1213         mlxsw_sp_fids_fini(mlxsw_sp);
1214 }
1215
1216 int mlxsw_sp_port_vlan_init(struct mlxsw_sp_port *mlxsw_sp_port)
1217 {
1218         struct net_device *dev = mlxsw_sp_port->dev;
1219         int err;
1220
1221         /* Allow only untagged packets to ingress and tag them internally
1222          * with VID 1.
1223          */
1224         mlxsw_sp_port->pvid = 1;
1225         err = __mlxsw_sp_port_vlans_del(mlxsw_sp_port, 0, VLAN_N_VID - 1,
1226                                         true);
1227         if (err) {
1228                 netdev_err(dev, "Unable to init VLANs\n");
1229                 return err;
1230         }
1231
1232         /* Add implicit VLAN interface in the device, so that untagged
1233          * packets will be classified to the default vFID.
1234          */
1235         err = mlxsw_sp_port_add_vid(dev, 0, 1);
1236         if (err)
1237                 netdev_err(dev, "Failed to configure default vFID\n");
1238
1239         return err;
1240 }
1241
1242 void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port *mlxsw_sp_port)
1243 {
1244         mlxsw_sp_port->dev->switchdev_ops = &mlxsw_sp_port_switchdev_ops;
1245 }
1246
1247 void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port)
1248 {
1249 }