]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/mellanox/mlxsw/spectrum_switchdev.c
mlxsw: spectrum: Make port flood update more generic
[karo-tx-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 <linux/rtnetlink.h>
49 #include <net/switchdev.h>
50
51 #include "spectrum.h"
52 #include "core.h"
53 #include "reg.h"
54
55 static u16 mlxsw_sp_port_vid_to_fid_get(struct mlxsw_sp_port *mlxsw_sp_port,
56                                         u16 vid)
57 {
58         struct mlxsw_sp_fid *f = mlxsw_sp_vport_fid_get(mlxsw_sp_port);
59         u16 fid = vid;
60
61         fid = f ? f->fid : fid;
62
63         if (!fid)
64                 fid = mlxsw_sp_port->pvid;
65
66         return fid;
67 }
68
69 static struct mlxsw_sp_port *
70 mlxsw_sp_port_orig_get(struct net_device *dev,
71                        struct mlxsw_sp_port *mlxsw_sp_port)
72 {
73         struct mlxsw_sp_port *mlxsw_sp_vport;
74         u16 vid;
75
76         if (!is_vlan_dev(dev))
77                 return mlxsw_sp_port;
78
79         vid = vlan_dev_vlan_id(dev);
80         mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
81         WARN_ON(!mlxsw_sp_vport);
82
83         return mlxsw_sp_vport;
84 }
85
86 static int mlxsw_sp_port_attr_get(struct net_device *dev,
87                                   struct switchdev_attr *attr)
88 {
89         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
90         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
91
92         mlxsw_sp_port = mlxsw_sp_port_orig_get(attr->orig_dev, mlxsw_sp_port);
93         if (!mlxsw_sp_port)
94                 return -EINVAL;
95
96         switch (attr->id) {
97         case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
98                 attr->u.ppid.id_len = sizeof(mlxsw_sp->base_mac);
99                 memcpy(&attr->u.ppid.id, &mlxsw_sp->base_mac,
100                        attr->u.ppid.id_len);
101                 break;
102         case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
103                 attr->u.brport_flags =
104                         (mlxsw_sp_port->learning ? BR_LEARNING : 0) |
105                         (mlxsw_sp_port->learning_sync ? BR_LEARNING_SYNC : 0) |
106                         (mlxsw_sp_port->uc_flood ? BR_FLOOD : 0);
107                 break;
108         default:
109                 return -EOPNOTSUPP;
110         }
111
112         return 0;
113 }
114
115 static int mlxsw_sp_port_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
116                                        u8 state)
117 {
118         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
119         enum mlxsw_reg_spms_state spms_state;
120         char *spms_pl;
121         u16 vid;
122         int err;
123
124         switch (state) {
125         case BR_STATE_FORWARDING:
126                 spms_state = MLXSW_REG_SPMS_STATE_FORWARDING;
127                 break;
128         case BR_STATE_LEARNING:
129                 spms_state = MLXSW_REG_SPMS_STATE_LEARNING;
130                 break;
131         case BR_STATE_LISTENING: /* fall-through */
132         case BR_STATE_DISABLED: /* fall-through */
133         case BR_STATE_BLOCKING:
134                 spms_state = MLXSW_REG_SPMS_STATE_DISCARDING;
135                 break;
136         default:
137                 BUG();
138         }
139
140         spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
141         if (!spms_pl)
142                 return -ENOMEM;
143         mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
144
145         if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
146                 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
147                 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
148         } else {
149                 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID)
150                         mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
151         }
152
153         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
154         kfree(spms_pl);
155         return err;
156 }
157
158 static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
159                                             struct switchdev_trans *trans,
160                                             u8 state)
161 {
162         if (switchdev_trans_ph_prepare(trans))
163                 return 0;
164
165         mlxsw_sp_port->stp_state = state;
166         return mlxsw_sp_port_stp_state_set(mlxsw_sp_port, state);
167 }
168
169 static int __mlxsw_sp_port_flood_table_set(struct mlxsw_sp_port *mlxsw_sp_port,
170                                            u16 idx_begin, u16 idx_end,
171                                            enum mlxsw_sp_flood_table table,
172                                            bool set)
173 {
174         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
175         u16 local_port = mlxsw_sp_port->local_port;
176         enum mlxsw_flood_table_type table_type;
177         u16 range = idx_end - idx_begin + 1;
178         char *sftr_pl;
179         int err;
180
181         if (mlxsw_sp_port_is_vport(mlxsw_sp_port))
182                 table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID;
183         else
184                 table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST;
185
186         sftr_pl = kmalloc(MLXSW_REG_SFTR_LEN, GFP_KERNEL);
187         if (!sftr_pl)
188                 return -ENOMEM;
189
190         mlxsw_reg_sftr_pack(sftr_pl, table, idx_begin,
191                             table_type, range, local_port, set);
192         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl);
193
194         kfree(sftr_pl);
195         return err;
196 }
197
198 static int __mlxsw_sp_port_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
199                                      u16 idx_begin, u16 idx_end, bool uc_set,
200                                      bool bm_set)
201 {
202         int err;
203
204         err = __mlxsw_sp_port_flood_table_set(mlxsw_sp_port, idx_begin, idx_end,
205                                               MLXSW_SP_FLOOD_TABLE_UC, uc_set);
206         if (err)
207                 return err;
208
209         err = __mlxsw_sp_port_flood_table_set(mlxsw_sp_port, idx_begin, idx_end,
210                                               MLXSW_SP_FLOOD_TABLE_BM, bm_set);
211         if (err)
212                 goto err_flood_bm_set;
213
214         return 0;
215
216 err_flood_bm_set:
217         __mlxsw_sp_port_flood_table_set(mlxsw_sp_port, idx_begin, idx_end,
218                                         MLXSW_SP_FLOOD_TABLE_UC, !uc_set);
219         return err;
220 }
221
222 static int mlxsw_sp_port_flood_table_set(struct mlxsw_sp_port *mlxsw_sp_port,
223                                          enum mlxsw_sp_flood_table table,
224                                          bool set)
225 {
226         struct net_device *dev = mlxsw_sp_port->dev;
227         u16 vid, last_visited_vid;
228         int err;
229
230         if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
231                 u16 fid = mlxsw_sp_vport_fid_get(mlxsw_sp_port)->fid;
232                 u16 vfid = mlxsw_sp_fid_to_vfid(fid);
233
234                 return __mlxsw_sp_port_flood_table_set(mlxsw_sp_port, vfid,
235                                                        vfid, table, set);
236         }
237
238         for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
239                 err = __mlxsw_sp_port_flood_table_set(mlxsw_sp_port, vid, vid,
240                                                       table, set);
241                 if (err) {
242                         last_visited_vid = vid;
243                         goto err_port_flood_set;
244                 }
245         }
246
247         return 0;
248
249 err_port_flood_set:
250         for_each_set_bit(vid, mlxsw_sp_port->active_vlans, last_visited_vid)
251                 __mlxsw_sp_port_flood_table_set(mlxsw_sp_port, vid, vid, table,
252                                                 !set);
253         netdev_err(dev, "Failed to configure unicast flooding\n");
254         return err;
255 }
256
257 int mlxsw_sp_vport_flood_set(struct mlxsw_sp_port *mlxsw_sp_vport, u16 fid,
258                              bool set)
259 {
260         u16 vfid;
261
262         /* In case of vFIDs, index into the flooding table is relative to
263          * the start of the vFIDs range.
264          */
265         vfid = mlxsw_sp_fid_to_vfid(fid);
266         return __mlxsw_sp_port_flood_set(mlxsw_sp_vport, vfid, vfid, set, set);
267 }
268
269 static int mlxsw_sp_port_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
270                                       bool set)
271 {
272         u16 vid;
273         int err;
274
275         if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
276                 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
277
278                 return __mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, vid,
279                                                         set);
280         }
281
282         for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
283                 err = __mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, vid,
284                                                        set);
285                 if (err)
286                         goto err_port_vid_learning_set;
287         }
288
289         return 0;
290
291 err_port_vid_learning_set:
292         for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID)
293                 __mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, vid, !set);
294         return err;
295 }
296
297 static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
298                                            struct switchdev_trans *trans,
299                                            unsigned long brport_flags)
300 {
301         unsigned long learning = mlxsw_sp_port->learning ? BR_LEARNING : 0;
302         unsigned long uc_flood = mlxsw_sp_port->uc_flood ? BR_FLOOD : 0;
303         int err;
304
305         if (!mlxsw_sp_port->bridged)
306                 return -EINVAL;
307
308         if (switchdev_trans_ph_prepare(trans))
309                 return 0;
310
311         if ((uc_flood ^ brport_flags) & BR_FLOOD) {
312                 err = mlxsw_sp_port_flood_table_set(mlxsw_sp_port,
313                                                     MLXSW_SP_FLOOD_TABLE_UC,
314                                                     !mlxsw_sp_port->uc_flood);
315                 if (err)
316                         return err;
317         }
318
319         if ((learning ^ brport_flags) & BR_LEARNING) {
320                 err = mlxsw_sp_port_learning_set(mlxsw_sp_port,
321                                                  !mlxsw_sp_port->learning);
322                 if (err)
323                         goto err_port_learning_set;
324         }
325
326         mlxsw_sp_port->uc_flood = brport_flags & BR_FLOOD ? 1 : 0;
327         mlxsw_sp_port->learning = brport_flags & BR_LEARNING ? 1 : 0;
328         mlxsw_sp_port->learning_sync = brport_flags & BR_LEARNING_SYNC ? 1 : 0;
329
330         return 0;
331
332 err_port_learning_set:
333         if ((uc_flood ^ brport_flags) & BR_FLOOD)
334                 mlxsw_sp_port_flood_table_set(mlxsw_sp_port,
335                                               MLXSW_SP_FLOOD_TABLE_UC,
336                                               mlxsw_sp_port->uc_flood);
337         return err;
338 }
339
340 static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
341 {
342         char sfdat_pl[MLXSW_REG_SFDAT_LEN];
343         int err;
344
345         mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
346         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
347         if (err)
348                 return err;
349         mlxsw_sp->ageing_time = ageing_time;
350         return 0;
351 }
352
353 static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
354                                             struct switchdev_trans *trans,
355                                             unsigned long ageing_clock_t)
356 {
357         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
358         unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
359         u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
360
361         if (switchdev_trans_ph_prepare(trans)) {
362                 if (ageing_time < MLXSW_SP_MIN_AGEING_TIME ||
363                     ageing_time > MLXSW_SP_MAX_AGEING_TIME)
364                         return -ERANGE;
365                 else
366                         return 0;
367         }
368
369         return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
370 }
371
372 static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
373                                           struct switchdev_trans *trans,
374                                           struct net_device *orig_dev,
375                                           bool vlan_enabled)
376 {
377         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
378
379         /* SWITCHDEV_TRANS_PREPARE phase */
380         if ((!vlan_enabled) && (mlxsw_sp->master_bridge.dev == orig_dev)) {
381                 netdev_err(mlxsw_sp_port->dev, "Bridge must be vlan-aware\n");
382                 return -EINVAL;
383         }
384
385         return 0;
386 }
387
388 static int mlxsw_sp_port_attr_set(struct net_device *dev,
389                                   const struct switchdev_attr *attr,
390                                   struct switchdev_trans *trans)
391 {
392         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
393         int err = 0;
394
395         mlxsw_sp_port = mlxsw_sp_port_orig_get(attr->orig_dev, mlxsw_sp_port);
396         if (!mlxsw_sp_port)
397                 return -EINVAL;
398
399         switch (attr->id) {
400         case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
401                 err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port, trans,
402                                                        attr->u.stp_state);
403                 break;
404         case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
405                 err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port, trans,
406                                                       attr->u.brport_flags);
407                 break;
408         case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
409                 err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port, trans,
410                                                        attr->u.ageing_time);
411                 break;
412         case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
413                 err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port, trans,
414                                                      attr->orig_dev,
415                                                      attr->u.vlan_filtering);
416                 break;
417         default:
418                 err = -EOPNOTSUPP;
419                 break;
420         }
421
422         return err;
423 }
424
425 static int mlxsw_sp_fid_op(struct mlxsw_sp *mlxsw_sp, u16 fid, bool create)
426 {
427         char sfmr_pl[MLXSW_REG_SFMR_LEN];
428
429         mlxsw_reg_sfmr_pack(sfmr_pl, !create, fid, fid);
430         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfmr), sfmr_pl);
431 }
432
433 static int mlxsw_sp_fid_map(struct mlxsw_sp *mlxsw_sp, u16 fid, bool valid)
434 {
435         enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_VID_TO_FID;
436         char svfa_pl[MLXSW_REG_SVFA_LEN];
437
438         mlxsw_reg_svfa_pack(svfa_pl, 0, mt, valid, fid, fid);
439         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svfa), svfa_pl);
440 }
441
442 static struct mlxsw_sp_fid *mlxsw_sp_fid_alloc(u16 fid)
443 {
444         struct mlxsw_sp_fid *f;
445
446         f = kzalloc(sizeof(*f), GFP_KERNEL);
447         if (!f)
448                 return NULL;
449
450         f->fid = fid;
451
452         return f;
453 }
454
455 struct mlxsw_sp_fid *mlxsw_sp_fid_create(struct mlxsw_sp *mlxsw_sp, u16 fid)
456 {
457         struct mlxsw_sp_fid *f;
458         int err;
459
460         err = mlxsw_sp_fid_op(mlxsw_sp, fid, true);
461         if (err)
462                 return ERR_PTR(err);
463
464         /* Although all the ports member in the FID might be using a
465          * {Port, VID} to FID mapping, we create a global VID-to-FID
466          * mapping. This allows a port to transition to VLAN mode,
467          * knowing the global mapping exists.
468          */
469         err = mlxsw_sp_fid_map(mlxsw_sp, fid, true);
470         if (err)
471                 goto err_fid_map;
472
473         f = mlxsw_sp_fid_alloc(fid);
474         if (!f) {
475                 err = -ENOMEM;
476                 goto err_allocate_fid;
477         }
478
479         list_add(&f->list, &mlxsw_sp->fids);
480
481         return f;
482
483 err_allocate_fid:
484         mlxsw_sp_fid_map(mlxsw_sp, fid, false);
485 err_fid_map:
486         mlxsw_sp_fid_op(mlxsw_sp, fid, false);
487         return ERR_PTR(err);
488 }
489
490 void mlxsw_sp_fid_destroy(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_fid *f)
491 {
492         u16 fid = f->fid;
493
494         list_del(&f->list);
495
496         if (f->r)
497                 mlxsw_sp_rif_bridge_destroy(mlxsw_sp, f->r);
498
499         kfree(f);
500
501         mlxsw_sp_fid_map(mlxsw_sp, fid, false);
502
503         mlxsw_sp_fid_op(mlxsw_sp, fid, false);
504 }
505
506 static int __mlxsw_sp_port_fid_join(struct mlxsw_sp_port *mlxsw_sp_port,
507                                     u16 fid)
508 {
509         struct mlxsw_sp_fid *f;
510
511         if (test_bit(fid, mlxsw_sp_port->active_vlans))
512                 return 0;
513
514         f = mlxsw_sp_fid_find(mlxsw_sp_port->mlxsw_sp, fid);
515         if (!f) {
516                 f = mlxsw_sp_fid_create(mlxsw_sp_port->mlxsw_sp, fid);
517                 if (IS_ERR(f))
518                         return PTR_ERR(f);
519         }
520
521         f->ref_count++;
522
523         netdev_dbg(mlxsw_sp_port->dev, "Joined FID=%d\n", fid);
524
525         return 0;
526 }
527
528 static void __mlxsw_sp_port_fid_leave(struct mlxsw_sp_port *mlxsw_sp_port,
529                                       u16 fid)
530 {
531         struct mlxsw_sp_fid *f;
532
533         f = mlxsw_sp_fid_find(mlxsw_sp_port->mlxsw_sp, fid);
534         if (WARN_ON(!f))
535                 return;
536
537         netdev_dbg(mlxsw_sp_port->dev, "Left FID=%d\n", fid);
538
539         mlxsw_sp_port_fdb_flush(mlxsw_sp_port, fid);
540
541         if (--f->ref_count == 0)
542                 mlxsw_sp_fid_destroy(mlxsw_sp_port->mlxsw_sp, f);
543 }
544
545 static int mlxsw_sp_port_fid_map(struct mlxsw_sp_port *mlxsw_sp_port, u16 fid,
546                                  bool valid)
547 {
548         enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID;
549
550         /* If port doesn't have vPorts, then it can use the global
551          * VID-to-FID mapping.
552          */
553         if (list_empty(&mlxsw_sp_port->vports_list))
554                 return 0;
555
556         return mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, valid, fid, fid);
557 }
558
559 static int mlxsw_sp_port_fid_join(struct mlxsw_sp_port *mlxsw_sp_port,
560                                   u16 fid_begin, u16 fid_end)
561 {
562         int fid, err;
563
564         for (fid = fid_begin; fid <= fid_end; fid++) {
565                 err = __mlxsw_sp_port_fid_join(mlxsw_sp_port, fid);
566                 if (err)
567                         goto err_port_fid_join;
568         }
569
570         err = __mlxsw_sp_port_flood_set(mlxsw_sp_port, fid_begin, fid_end,
571                                         mlxsw_sp_port->uc_flood, true);
572         if (err)
573                 goto err_port_flood_set;
574
575         for (fid = fid_begin; fid <= fid_end; fid++) {
576                 err = mlxsw_sp_port_fid_map(mlxsw_sp_port, fid, true);
577                 if (err)
578                         goto err_port_fid_map;
579         }
580
581         return 0;
582
583 err_port_fid_map:
584         for (fid--; fid >= fid_begin; fid--)
585                 mlxsw_sp_port_fid_map(mlxsw_sp_port, fid, false);
586         __mlxsw_sp_port_flood_set(mlxsw_sp_port, fid_begin, fid_end, false,
587                                   false);
588 err_port_flood_set:
589         fid = fid_end;
590 err_port_fid_join:
591         for (fid--; fid >= fid_begin; fid--)
592                 __mlxsw_sp_port_fid_leave(mlxsw_sp_port, fid);
593         return err;
594 }
595
596 static void mlxsw_sp_port_fid_leave(struct mlxsw_sp_port *mlxsw_sp_port,
597                                     u16 fid_begin, u16 fid_end)
598 {
599         int fid;
600
601         for (fid = fid_begin; fid <= fid_end; fid++)
602                 mlxsw_sp_port_fid_map(mlxsw_sp_port, fid, false);
603
604         __mlxsw_sp_port_flood_set(mlxsw_sp_port, fid_begin, fid_end, false,
605                                   false);
606
607         for (fid = fid_begin; fid <= fid_end; fid++)
608                 __mlxsw_sp_port_fid_leave(mlxsw_sp_port, fid);
609 }
610
611 static int __mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port,
612                                     u16 vid)
613 {
614         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
615         char spvid_pl[MLXSW_REG_SPVID_LEN];
616
617         mlxsw_reg_spvid_pack(spvid_pl, mlxsw_sp_port->local_port, vid);
618         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvid), spvid_pl);
619 }
620
621 static int mlxsw_sp_port_allow_untagged_set(struct mlxsw_sp_port *mlxsw_sp_port,
622                                             bool allow)
623 {
624         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
625         char spaft_pl[MLXSW_REG_SPAFT_LEN];
626
627         mlxsw_reg_spaft_pack(spaft_pl, mlxsw_sp_port->local_port, allow);
628         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spaft), spaft_pl);
629 }
630
631 int mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
632 {
633         struct net_device *dev = mlxsw_sp_port->dev;
634         int err;
635
636         if (!vid) {
637                 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port, false);
638                 if (err) {
639                         netdev_err(dev, "Failed to disallow untagged traffic\n");
640                         return err;
641                 }
642         } else {
643                 err = __mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid);
644                 if (err) {
645                         netdev_err(dev, "Failed to set PVID\n");
646                         return err;
647                 }
648
649                 /* Only allow if not already allowed. */
650                 if (!mlxsw_sp_port->pvid) {
651                         err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port,
652                                                                true);
653                         if (err) {
654                                 netdev_err(dev, "Failed to allow untagged traffic\n");
655                                 goto err_port_allow_untagged_set;
656                         }
657                 }
658         }
659
660         mlxsw_sp_port->pvid = vid;
661         return 0;
662
663 err_port_allow_untagged_set:
664         __mlxsw_sp_port_pvid_set(mlxsw_sp_port, mlxsw_sp_port->pvid);
665         return err;
666 }
667
668 static int __mlxsw_sp_port_vlans_set(struct mlxsw_sp_port *mlxsw_sp_port,
669                                      u16 vid_begin, u16 vid_end, bool is_member,
670                                      bool untagged)
671 {
672         u16 vid, vid_e;
673         int err;
674
675         for (vid = vid_begin; vid <= vid_end;
676              vid += MLXSW_REG_SPVM_REC_MAX_COUNT) {
677                 vid_e = min((u16) (vid + MLXSW_REG_SPVM_REC_MAX_COUNT - 1),
678                             vid_end);
679
680                 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid_e,
681                                              is_member, untagged);
682                 if (err)
683                         return err;
684         }
685
686         return 0;
687 }
688
689 static int mlxsw_sp_port_vid_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
690                                           u16 vid_begin, u16 vid_end,
691                                           bool learn_enable)
692 {
693         u16 vid, vid_e;
694         int err;
695
696         for (vid = vid_begin; vid <= vid_end;
697              vid += MLXSW_REG_SPVMLR_REC_MAX_COUNT) {
698                 vid_e = min((u16) (vid + MLXSW_REG_SPVMLR_REC_MAX_COUNT - 1),
699                             vid_end);
700
701                 err = __mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid,
702                                                        vid_e, learn_enable);
703                 if (err)
704                         return err;
705         }
706
707         return 0;
708 }
709
710 static int __mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
711                                      u16 vid_begin, u16 vid_end,
712                                      bool flag_untagged, bool flag_pvid)
713 {
714         struct net_device *dev = mlxsw_sp_port->dev;
715         u16 vid, old_pvid;
716         int err;
717
718         if (!mlxsw_sp_port->bridged)
719                 return -EINVAL;
720
721         err = mlxsw_sp_port_fid_join(mlxsw_sp_port, vid_begin, vid_end);
722         if (err) {
723                 netdev_err(dev, "Failed to join FIDs\n");
724                 return err;
725         }
726
727         err = __mlxsw_sp_port_vlans_set(mlxsw_sp_port, vid_begin, vid_end,
728                                         true, flag_untagged);
729         if (err) {
730                 netdev_err(dev, "Unable to add VIDs %d-%d\n", vid_begin,
731                            vid_end);
732                 goto err_port_vlans_set;
733         }
734
735         old_pvid = mlxsw_sp_port->pvid;
736         if (flag_pvid && old_pvid != vid_begin) {
737                 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid_begin);
738                 if (err) {
739                         netdev_err(dev, "Unable to add PVID %d\n", vid_begin);
740                         goto err_port_pvid_set;
741                 }
742         } else if (!flag_pvid && old_pvid >= vid_begin && old_pvid <= vid_end) {
743                 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, 0);
744                 if (err) {
745                         netdev_err(dev, "Unable to del PVID\n");
746                         goto err_port_pvid_set;
747                 }
748         }
749
750         err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid_begin, vid_end,
751                                              mlxsw_sp_port->learning);
752         if (err) {
753                 netdev_err(dev, "Failed to set learning for VIDs %d-%d\n",
754                            vid_begin, vid_end);
755                 goto err_port_vid_learning_set;
756         }
757
758         /* Changing activity bits only if HW operation succeded */
759         for (vid = vid_begin; vid <= vid_end; vid++) {
760                 set_bit(vid, mlxsw_sp_port->active_vlans);
761                 if (flag_untagged)
762                         set_bit(vid, mlxsw_sp_port->untagged_vlans);
763                 else
764                         clear_bit(vid, mlxsw_sp_port->untagged_vlans);
765         }
766
767         /* STP state change must be done after we set active VLANs */
768         err = mlxsw_sp_port_stp_state_set(mlxsw_sp_port,
769                                           mlxsw_sp_port->stp_state);
770         if (err) {
771                 netdev_err(dev, "Failed to set STP state\n");
772                 goto err_port_stp_state_set;
773         }
774
775         return 0;
776
777 err_port_stp_state_set:
778         for (vid = vid_begin; vid <= vid_end; vid++)
779                 clear_bit(vid, mlxsw_sp_port->active_vlans);
780         mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid_begin, vid_end,
781                                        false);
782 err_port_vid_learning_set:
783         if (old_pvid != mlxsw_sp_port->pvid)
784                 mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid);
785 err_port_pvid_set:
786         __mlxsw_sp_port_vlans_set(mlxsw_sp_port, vid_begin, vid_end, false,
787                                   false);
788 err_port_vlans_set:
789         mlxsw_sp_port_fid_leave(mlxsw_sp_port, vid_begin, vid_end);
790         return err;
791 }
792
793 static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
794                                    const struct switchdev_obj_port_vlan *vlan,
795                                    struct switchdev_trans *trans)
796 {
797         bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
798         bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
799
800         if (switchdev_trans_ph_prepare(trans))
801                 return 0;
802
803         return __mlxsw_sp_port_vlans_add(mlxsw_sp_port,
804                                          vlan->vid_begin, vlan->vid_end,
805                                          flag_untagged, flag_pvid);
806 }
807
808 static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
809 {
810         return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
811                          MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY;
812 }
813
814 static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
815 {
816         return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
817                         MLXSW_REG_SFD_OP_WRITE_REMOVE;
818 }
819
820 static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
821                                      const char *mac, u16 fid, bool adding,
822                                      enum mlxsw_reg_sfd_rec_action action,
823                                      bool dynamic)
824 {
825         char *sfd_pl;
826         int err;
827
828         sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
829         if (!sfd_pl)
830                 return -ENOMEM;
831
832         mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
833         mlxsw_reg_sfd_uc_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
834                               mac, fid, action, local_port);
835         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
836         kfree(sfd_pl);
837
838         return err;
839 }
840
841 static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
842                                    const char *mac, u16 fid, bool adding,
843                                    bool dynamic)
844 {
845         return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding,
846                                          MLXSW_REG_SFD_REC_ACTION_NOP, dynamic);
847 }
848
849 int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid,
850                         bool adding)
851 {
852         return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding,
853                                          MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER,
854                                          false);
855 }
856
857 static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
858                                        const char *mac, u16 fid, u16 lag_vid,
859                                        bool adding, bool dynamic)
860 {
861         char *sfd_pl;
862         int err;
863
864         sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
865         if (!sfd_pl)
866                 return -ENOMEM;
867
868         mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
869         mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
870                                   mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
871                                   lag_vid, lag_id);
872         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
873         kfree(sfd_pl);
874
875         return err;
876 }
877
878 static int
879 mlxsw_sp_port_fdb_static_add(struct mlxsw_sp_port *mlxsw_sp_port,
880                              const struct switchdev_obj_port_fdb *fdb,
881                              struct switchdev_trans *trans)
882 {
883         u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, fdb->vid);
884         u16 lag_vid = 0;
885
886         if (switchdev_trans_ph_prepare(trans))
887                 return 0;
888
889         if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
890                 lag_vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
891         }
892
893         if (!mlxsw_sp_port->lagged)
894                 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp_port->mlxsw_sp,
895                                                mlxsw_sp_port->local_port,
896                                                fdb->addr, fid, true, false);
897         else
898                 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp_port->mlxsw_sp,
899                                                    mlxsw_sp_port->lag_id,
900                                                    fdb->addr, fid, lag_vid,
901                                                    true, false);
902 }
903
904 static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
905                                 u16 fid, u16 mid, bool adding)
906 {
907         char *sfd_pl;
908         int err;
909
910         sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
911         if (!sfd_pl)
912                 return -ENOMEM;
913
914         mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
915         mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
916                               MLXSW_REG_SFD_REC_ACTION_NOP, mid);
917         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
918         kfree(sfd_pl);
919         return err;
920 }
921
922 static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 mid,
923                                   bool add, bool clear_all_ports)
924 {
925         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
926         char *smid_pl;
927         int err, i;
928
929         smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
930         if (!smid_pl)
931                 return -ENOMEM;
932
933         mlxsw_reg_smid_pack(smid_pl, mid, mlxsw_sp_port->local_port, add);
934         if (clear_all_ports) {
935                 for (i = 1; i < MLXSW_PORT_MAX_PORTS; i++)
936                         if (mlxsw_sp->ports[i])
937                                 mlxsw_reg_smid_port_mask_set(smid_pl, i, 1);
938         }
939         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
940         kfree(smid_pl);
941         return err;
942 }
943
944 static struct mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp *mlxsw_sp,
945                                               const unsigned char *addr,
946                                               u16 fid)
947 {
948         struct mlxsw_sp_mid *mid;
949
950         list_for_each_entry(mid, &mlxsw_sp->br_mids.list, list) {
951                 if (ether_addr_equal(mid->addr, addr) && mid->fid == fid)
952                         return mid;
953         }
954         return NULL;
955 }
956
957 static struct mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
958                                                 const unsigned char *addr,
959                                                 u16 fid)
960 {
961         struct mlxsw_sp_mid *mid;
962         u16 mid_idx;
963
964         mid_idx = find_first_zero_bit(mlxsw_sp->br_mids.mapped,
965                                       MLXSW_SP_MID_MAX);
966         if (mid_idx == MLXSW_SP_MID_MAX)
967                 return NULL;
968
969         mid = kzalloc(sizeof(*mid), GFP_KERNEL);
970         if (!mid)
971                 return NULL;
972
973         set_bit(mid_idx, mlxsw_sp->br_mids.mapped);
974         ether_addr_copy(mid->addr, addr);
975         mid->fid = fid;
976         mid->mid = mid_idx;
977         mid->ref_count = 0;
978         list_add_tail(&mid->list, &mlxsw_sp->br_mids.list);
979
980         return mid;
981 }
982
983 static int __mlxsw_sp_mc_dec_ref(struct mlxsw_sp *mlxsw_sp,
984                                  struct mlxsw_sp_mid *mid)
985 {
986         if (--mid->ref_count == 0) {
987                 list_del(&mid->list);
988                 clear_bit(mid->mid, mlxsw_sp->br_mids.mapped);
989                 kfree(mid);
990                 return 1;
991         }
992         return 0;
993 }
994
995 static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
996                                  const struct switchdev_obj_port_mdb *mdb,
997                                  struct switchdev_trans *trans)
998 {
999         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1000         struct net_device *dev = mlxsw_sp_port->dev;
1001         struct mlxsw_sp_mid *mid;
1002         u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, mdb->vid);
1003         int err = 0;
1004
1005         if (switchdev_trans_ph_prepare(trans))
1006                 return 0;
1007
1008         mid = __mlxsw_sp_mc_get(mlxsw_sp, mdb->addr, fid);
1009         if (!mid) {
1010                 mid = __mlxsw_sp_mc_alloc(mlxsw_sp, mdb->addr, fid);
1011                 if (!mid) {
1012                         netdev_err(dev, "Unable to allocate MC group\n");
1013                         return -ENOMEM;
1014                 }
1015         }
1016         mid->ref_count++;
1017
1018         err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true,
1019                                      mid->ref_count == 1);
1020         if (err) {
1021                 netdev_err(dev, "Unable to set SMID\n");
1022                 goto err_out;
1023         }
1024
1025         if (mid->ref_count == 1) {
1026                 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mdb->addr, fid, mid->mid,
1027                                            true);
1028                 if (err) {
1029                         netdev_err(dev, "Unable to set MC SFD\n");
1030                         goto err_out;
1031                 }
1032         }
1033
1034         return 0;
1035
1036 err_out:
1037         __mlxsw_sp_mc_dec_ref(mlxsw_sp, mid);
1038         return err;
1039 }
1040
1041 static int mlxsw_sp_port_obj_add(struct net_device *dev,
1042                                  const struct switchdev_obj *obj,
1043                                  struct switchdev_trans *trans)
1044 {
1045         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1046         int err = 0;
1047
1048         mlxsw_sp_port = mlxsw_sp_port_orig_get(obj->orig_dev, mlxsw_sp_port);
1049         if (!mlxsw_sp_port)
1050                 return -EINVAL;
1051
1052         switch (obj->id) {
1053         case SWITCHDEV_OBJ_ID_PORT_VLAN:
1054                 if (mlxsw_sp_port_is_vport(mlxsw_sp_port))
1055                         return 0;
1056
1057                 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port,
1058                                               SWITCHDEV_OBJ_PORT_VLAN(obj),
1059                                               trans);
1060                 break;
1061         case SWITCHDEV_OBJ_ID_PORT_FDB:
1062                 err = mlxsw_sp_port_fdb_static_add(mlxsw_sp_port,
1063                                                    SWITCHDEV_OBJ_PORT_FDB(obj),
1064                                                    trans);
1065                 break;
1066         case SWITCHDEV_OBJ_ID_PORT_MDB:
1067                 err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
1068                                             SWITCHDEV_OBJ_PORT_MDB(obj),
1069                                             trans);
1070                 break;
1071         default:
1072                 err = -EOPNOTSUPP;
1073                 break;
1074         }
1075
1076         return err;
1077 }
1078
1079 static int __mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1080                                      u16 vid_begin, u16 vid_end)
1081 {
1082         u16 vid, pvid;
1083
1084         if (!mlxsw_sp_port->bridged)
1085                 return -EINVAL;
1086
1087         mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid_begin, vid_end,
1088                                        false);
1089
1090         pvid = mlxsw_sp_port->pvid;
1091         if (pvid >= vid_begin && pvid <= vid_end)
1092                 mlxsw_sp_port_pvid_set(mlxsw_sp_port, 0);
1093
1094         __mlxsw_sp_port_vlans_set(mlxsw_sp_port, vid_begin, vid_end, false,
1095                                   false);
1096
1097         mlxsw_sp_port_fid_leave(mlxsw_sp_port, vid_begin, vid_end);
1098
1099         /* Changing activity bits only if HW operation succeded */
1100         for (vid = vid_begin; vid <= vid_end; vid++)
1101                 clear_bit(vid, mlxsw_sp_port->active_vlans);
1102
1103         return 0;
1104 }
1105
1106 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1107                                    const struct switchdev_obj_port_vlan *vlan)
1108 {
1109         return __mlxsw_sp_port_vlans_del(mlxsw_sp_port, vlan->vid_begin,
1110                                          vlan->vid_end);
1111 }
1112
1113 void mlxsw_sp_port_active_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port)
1114 {
1115         u16 vid;
1116
1117         for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID)
1118                 __mlxsw_sp_port_vlans_del(mlxsw_sp_port, vid, vid);
1119 }
1120
1121 static int
1122 mlxsw_sp_port_fdb_static_del(struct mlxsw_sp_port *mlxsw_sp_port,
1123                              const struct switchdev_obj_port_fdb *fdb)
1124 {
1125         u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, fdb->vid);
1126         u16 lag_vid = 0;
1127
1128         if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
1129                 lag_vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
1130         }
1131
1132         if (!mlxsw_sp_port->lagged)
1133                 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp_port->mlxsw_sp,
1134                                                mlxsw_sp_port->local_port,
1135                                                fdb->addr, fid,
1136                                                false, false);
1137         else
1138                 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp_port->mlxsw_sp,
1139                                                    mlxsw_sp_port->lag_id,
1140                                                    fdb->addr, fid, lag_vid,
1141                                                    false, false);
1142 }
1143
1144 static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1145                                  const struct switchdev_obj_port_mdb *mdb)
1146 {
1147         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1148         struct net_device *dev = mlxsw_sp_port->dev;
1149         struct mlxsw_sp_mid *mid;
1150         u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, mdb->vid);
1151         u16 mid_idx;
1152         int err = 0;
1153
1154         mid = __mlxsw_sp_mc_get(mlxsw_sp, mdb->addr, fid);
1155         if (!mid) {
1156                 netdev_err(dev, "Unable to remove port from MC DB\n");
1157                 return -EINVAL;
1158         }
1159
1160         err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false, false);
1161         if (err)
1162                 netdev_err(dev, "Unable to remove port from SMID\n");
1163
1164         mid_idx = mid->mid;
1165         if (__mlxsw_sp_mc_dec_ref(mlxsw_sp, mid)) {
1166                 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mdb->addr, fid, mid_idx,
1167                                            false);
1168                 if (err)
1169                         netdev_err(dev, "Unable to remove MC SFD\n");
1170         }
1171
1172         return err;
1173 }
1174
1175 static int mlxsw_sp_port_obj_del(struct net_device *dev,
1176                                  const struct switchdev_obj *obj)
1177 {
1178         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1179         int err = 0;
1180
1181         mlxsw_sp_port = mlxsw_sp_port_orig_get(obj->orig_dev, mlxsw_sp_port);
1182         if (!mlxsw_sp_port)
1183                 return -EINVAL;
1184
1185         switch (obj->id) {
1186         case SWITCHDEV_OBJ_ID_PORT_VLAN:
1187                 if (mlxsw_sp_port_is_vport(mlxsw_sp_port))
1188                         return 0;
1189
1190                 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1191                                               SWITCHDEV_OBJ_PORT_VLAN(obj));
1192                 break;
1193         case SWITCHDEV_OBJ_ID_PORT_FDB:
1194                 err = mlxsw_sp_port_fdb_static_del(mlxsw_sp_port,
1195                                                    SWITCHDEV_OBJ_PORT_FDB(obj));
1196                 break;
1197         case SWITCHDEV_OBJ_ID_PORT_MDB:
1198                 err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1199                                             SWITCHDEV_OBJ_PORT_MDB(obj));
1200                 break;
1201         default:
1202                 err = -EOPNOTSUPP;
1203                 break;
1204         }
1205
1206         return err;
1207 }
1208
1209 static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
1210                                                    u16 lag_id)
1211 {
1212         struct mlxsw_sp_port *mlxsw_sp_port;
1213         u64 max_lag_members;
1214         int i;
1215
1216         max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1217                                              MAX_LAG_MEMBERS);
1218         for (i = 0; i < max_lag_members; i++) {
1219                 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
1220                 if (mlxsw_sp_port)
1221                         return mlxsw_sp_port;
1222         }
1223         return NULL;
1224 }
1225
1226 static int mlxsw_sp_port_fdb_dump(struct mlxsw_sp_port *mlxsw_sp_port,
1227                                   struct switchdev_obj_port_fdb *fdb,
1228                                   switchdev_obj_dump_cb_t *cb,
1229                                   struct net_device *orig_dev)
1230 {
1231         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1232         struct mlxsw_sp_port *tmp;
1233         struct mlxsw_sp_fid *f;
1234         u16 vport_fid;
1235         char *sfd_pl;
1236         char mac[ETH_ALEN];
1237         u16 fid;
1238         u8 local_port;
1239         u16 lag_id;
1240         u8 num_rec;
1241         int stored_err = 0;
1242         int i;
1243         int err;
1244
1245         sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1246         if (!sfd_pl)
1247                 return -ENOMEM;
1248
1249         f = mlxsw_sp_vport_fid_get(mlxsw_sp_port);
1250         vport_fid = f ? f->fid : 0;
1251
1252         mlxsw_reg_sfd_pack(sfd_pl, MLXSW_REG_SFD_OP_QUERY_DUMP, 0);
1253         do {
1254                 mlxsw_reg_sfd_num_rec_set(sfd_pl, MLXSW_REG_SFD_REC_MAX_COUNT);
1255                 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1256                 if (err)
1257                         goto out;
1258
1259                 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1260
1261                 /* Even in case of error, we have to run the dump to the end
1262                  * so the session in firmware is finished.
1263                  */
1264                 if (stored_err)
1265                         continue;
1266
1267                 for (i = 0; i < num_rec; i++) {
1268                         switch (mlxsw_reg_sfd_rec_type_get(sfd_pl, i)) {
1269                         case MLXSW_REG_SFD_REC_TYPE_UNICAST:
1270                                 mlxsw_reg_sfd_uc_unpack(sfd_pl, i, mac, &fid,
1271                                                         &local_port);
1272                                 if (local_port == mlxsw_sp_port->local_port) {
1273                                         if (vport_fid && vport_fid == fid)
1274                                                 fdb->vid = 0;
1275                                         else if (!vport_fid &&
1276                                                  !mlxsw_sp_fid_is_vfid(fid))
1277                                                 fdb->vid = fid;
1278                                         else
1279                                                 continue;
1280                                         ether_addr_copy(fdb->addr, mac);
1281                                         fdb->ndm_state = NUD_REACHABLE;
1282                                         err = cb(&fdb->obj);
1283                                         if (err)
1284                                                 stored_err = err;
1285                                 }
1286                                 break;
1287                         case MLXSW_REG_SFD_REC_TYPE_UNICAST_LAG:
1288                                 mlxsw_reg_sfd_uc_lag_unpack(sfd_pl, i,
1289                                                             mac, &fid, &lag_id);
1290                                 tmp = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
1291                                 if (tmp && tmp->local_port ==
1292                                     mlxsw_sp_port->local_port) {
1293                                         /* LAG records can only point to LAG
1294                                          * devices or VLAN devices on top.
1295                                          */
1296                                         if (!netif_is_lag_master(orig_dev) &&
1297                                             !is_vlan_dev(orig_dev))
1298                                                 continue;
1299                                         if (vport_fid && vport_fid == fid)
1300                                                 fdb->vid = 0;
1301                                         else if (!vport_fid &&
1302                                                  !mlxsw_sp_fid_is_vfid(fid))
1303                                                 fdb->vid = fid;
1304                                         else
1305                                                 continue;
1306                                         ether_addr_copy(fdb->addr, mac);
1307                                         fdb->ndm_state = NUD_REACHABLE;
1308                                         err = cb(&fdb->obj);
1309                                         if (err)
1310                                                 stored_err = err;
1311                                 }
1312                                 break;
1313                         }
1314                 }
1315         } while (num_rec == MLXSW_REG_SFD_REC_MAX_COUNT);
1316
1317 out:
1318         kfree(sfd_pl);
1319         return stored_err ? stored_err : err;
1320 }
1321
1322 static int mlxsw_sp_port_vlan_dump(struct mlxsw_sp_port *mlxsw_sp_port,
1323                                    struct switchdev_obj_port_vlan *vlan,
1324                                    switchdev_obj_dump_cb_t *cb)
1325 {
1326         u16 vid;
1327         int err = 0;
1328
1329         if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
1330                 vlan->flags = 0;
1331                 vlan->vid_begin = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
1332                 vlan->vid_end = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
1333                 return cb(&vlan->obj);
1334         }
1335
1336         for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
1337                 vlan->flags = 0;
1338                 if (vid == mlxsw_sp_port->pvid)
1339                         vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1340                 if (test_bit(vid, mlxsw_sp_port->untagged_vlans))
1341                         vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1342                 vlan->vid_begin = vid;
1343                 vlan->vid_end = vid;
1344                 err = cb(&vlan->obj);
1345                 if (err)
1346                         break;
1347         }
1348         return err;
1349 }
1350
1351 static int mlxsw_sp_port_obj_dump(struct net_device *dev,
1352                                   struct switchdev_obj *obj,
1353                                   switchdev_obj_dump_cb_t *cb)
1354 {
1355         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1356         int err = 0;
1357
1358         mlxsw_sp_port = mlxsw_sp_port_orig_get(obj->orig_dev, mlxsw_sp_port);
1359         if (!mlxsw_sp_port)
1360                 return -EINVAL;
1361
1362         switch (obj->id) {
1363         case SWITCHDEV_OBJ_ID_PORT_VLAN:
1364                 err = mlxsw_sp_port_vlan_dump(mlxsw_sp_port,
1365                                               SWITCHDEV_OBJ_PORT_VLAN(obj), cb);
1366                 break;
1367         case SWITCHDEV_OBJ_ID_PORT_FDB:
1368                 err = mlxsw_sp_port_fdb_dump(mlxsw_sp_port,
1369                                              SWITCHDEV_OBJ_PORT_FDB(obj), cb,
1370                                              obj->orig_dev);
1371                 break;
1372         default:
1373                 err = -EOPNOTSUPP;
1374                 break;
1375         }
1376
1377         return err;
1378 }
1379
1380 static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = {
1381         .switchdev_port_attr_get        = mlxsw_sp_port_attr_get,
1382         .switchdev_port_attr_set        = mlxsw_sp_port_attr_set,
1383         .switchdev_port_obj_add         = mlxsw_sp_port_obj_add,
1384         .switchdev_port_obj_del         = mlxsw_sp_port_obj_del,
1385         .switchdev_port_obj_dump        = mlxsw_sp_port_obj_dump,
1386 };
1387
1388 static void mlxsw_sp_fdb_call_notifiers(bool learning_sync, bool adding,
1389                                         char *mac, u16 vid,
1390                                         struct net_device *dev)
1391 {
1392         struct switchdev_notifier_fdb_info info;
1393         unsigned long notifier_type;
1394
1395         if (learning_sync) {
1396                 info.addr = mac;
1397                 info.vid = vid;
1398                 notifier_type = adding ? SWITCHDEV_FDB_ADD : SWITCHDEV_FDB_DEL;
1399                 call_switchdev_notifiers(notifier_type, dev, &info.info);
1400         }
1401 }
1402
1403 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
1404                                             char *sfn_pl, int rec_index,
1405                                             bool adding)
1406 {
1407         struct mlxsw_sp_port *mlxsw_sp_port;
1408         char mac[ETH_ALEN];
1409         u8 local_port;
1410         u16 vid, fid;
1411         bool do_notification = true;
1412         int err;
1413
1414         mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
1415         mlxsw_sp_port = mlxsw_sp->ports[local_port];
1416         if (!mlxsw_sp_port) {
1417                 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
1418                 goto just_remove;
1419         }
1420
1421         if (mlxsw_sp_fid_is_vfid(fid)) {
1422                 struct mlxsw_sp_port *mlxsw_sp_vport;
1423
1424                 mlxsw_sp_vport = mlxsw_sp_port_vport_find_by_fid(mlxsw_sp_port,
1425                                                                  fid);
1426                 if (!mlxsw_sp_vport) {
1427                         netdev_err(mlxsw_sp_port->dev, "Failed to find a matching vPort following FDB notification\n");
1428                         goto just_remove;
1429                 }
1430                 vid = 0;
1431                 /* Override the physical port with the vPort. */
1432                 mlxsw_sp_port = mlxsw_sp_vport;
1433         } else {
1434                 vid = fid;
1435         }
1436
1437 do_fdb_op:
1438         err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
1439                                       adding, true);
1440         if (err) {
1441                 if (net_ratelimit())
1442                         netdev_err(mlxsw_sp_port->dev, "Failed to set FDB entry\n");
1443                 return;
1444         }
1445
1446         if (!do_notification)
1447                 return;
1448         mlxsw_sp_fdb_call_notifiers(mlxsw_sp_port->learning_sync,
1449                                     adding, mac, vid, mlxsw_sp_port->dev);
1450         return;
1451
1452 just_remove:
1453         adding = false;
1454         do_notification = false;
1455         goto do_fdb_op;
1456 }
1457
1458 static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
1459                                                 char *sfn_pl, int rec_index,
1460                                                 bool adding)
1461 {
1462         struct mlxsw_sp_port *mlxsw_sp_port;
1463         struct net_device *dev;
1464         char mac[ETH_ALEN];
1465         u16 lag_vid = 0;
1466         u16 lag_id;
1467         u16 vid, fid;
1468         bool do_notification = true;
1469         int err;
1470
1471         mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
1472         mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
1473         if (!mlxsw_sp_port) {
1474                 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
1475                 goto just_remove;
1476         }
1477
1478         if (mlxsw_sp_fid_is_vfid(fid)) {
1479                 struct mlxsw_sp_port *mlxsw_sp_vport;
1480
1481                 mlxsw_sp_vport = mlxsw_sp_port_vport_find_by_fid(mlxsw_sp_port,
1482                                                                  fid);
1483                 if (!mlxsw_sp_vport) {
1484                         netdev_err(mlxsw_sp_port->dev, "Failed to find a matching vPort following FDB notification\n");
1485                         goto just_remove;
1486                 }
1487
1488                 lag_vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport);
1489                 dev = mlxsw_sp_vport->dev;
1490                 vid = 0;
1491                 /* Override the physical port with the vPort. */
1492                 mlxsw_sp_port = mlxsw_sp_vport;
1493         } else {
1494                 dev = mlxsw_sp_lag_get(mlxsw_sp, lag_id)->dev;
1495                 vid = fid;
1496         }
1497
1498 do_fdb_op:
1499         err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
1500                                           adding, true);
1501         if (err) {
1502                 if (net_ratelimit())
1503                         netdev_err(mlxsw_sp_port->dev, "Failed to set FDB entry\n");
1504                 return;
1505         }
1506
1507         if (!do_notification)
1508                 return;
1509         mlxsw_sp_fdb_call_notifiers(mlxsw_sp_port->learning_sync, adding, mac,
1510                                     vid, dev);
1511         return;
1512
1513 just_remove:
1514         adding = false;
1515         do_notification = false;
1516         goto do_fdb_op;
1517 }
1518
1519 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
1520                                             char *sfn_pl, int rec_index)
1521 {
1522         switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
1523         case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
1524                 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
1525                                                 rec_index, true);
1526                 break;
1527         case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
1528                 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
1529                                                 rec_index, false);
1530                 break;
1531         case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
1532                 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
1533                                                     rec_index, true);
1534                 break;
1535         case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
1536                 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
1537                                                     rec_index, false);
1538                 break;
1539         }
1540 }
1541
1542 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp)
1543 {
1544         mlxsw_core_schedule_dw(&mlxsw_sp->fdb_notify.dw,
1545                                msecs_to_jiffies(mlxsw_sp->fdb_notify.interval));
1546 }
1547
1548 static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
1549 {
1550         struct mlxsw_sp *mlxsw_sp;
1551         char *sfn_pl;
1552         u8 num_rec;
1553         int i;
1554         int err;
1555
1556         sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
1557         if (!sfn_pl)
1558                 return;
1559
1560         mlxsw_sp = container_of(work, struct mlxsw_sp, fdb_notify.dw.work);
1561
1562         rtnl_lock();
1563         mlxsw_reg_sfn_pack(sfn_pl);
1564         err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
1565         if (err) {
1566                 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
1567                 goto out;
1568         }
1569         num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
1570         for (i = 0; i < num_rec; i++)
1571                 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
1572
1573 out:
1574         rtnl_unlock();
1575         kfree(sfn_pl);
1576         mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
1577 }
1578
1579 static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
1580 {
1581         int err;
1582
1583         err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
1584         if (err) {
1585                 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
1586                 return err;
1587         }
1588         INIT_DELAYED_WORK(&mlxsw_sp->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
1589         mlxsw_sp->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
1590         mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
1591         return 0;
1592 }
1593
1594 static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
1595 {
1596         cancel_delayed_work_sync(&mlxsw_sp->fdb_notify.dw);
1597 }
1598
1599 int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
1600 {
1601         return mlxsw_sp_fdb_init(mlxsw_sp);
1602 }
1603
1604 void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
1605 {
1606         mlxsw_sp_fdb_fini(mlxsw_sp);
1607 }
1608
1609 void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port *mlxsw_sp_port)
1610 {
1611         mlxsw_sp_port->dev->switchdev_ops = &mlxsw_sp_port_switchdev_ops;
1612 }
1613
1614 void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port)
1615 {
1616 }