mlxsw: spectrum_switchdev: Fix port_vlan refcounting
[muen/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 <linux/netlink.h>
50 #include <net/switchdev.h>
51
52 #include "spectrum_span.h"
53 #include "spectrum_router.h"
54 #include "spectrum_switchdev.h"
55 #include "spectrum.h"
56 #include "core.h"
57 #include "reg.h"
58
59 struct mlxsw_sp_bridge_ops;
60
61 struct mlxsw_sp_bridge {
62         struct mlxsw_sp *mlxsw_sp;
63         struct {
64                 struct delayed_work dw;
65 #define MLXSW_SP_DEFAULT_LEARNING_INTERVAL 100
66                 unsigned int interval; /* ms */
67         } fdb_notify;
68 #define MLXSW_SP_MIN_AGEING_TIME 10
69 #define MLXSW_SP_MAX_AGEING_TIME 1000000
70 #define MLXSW_SP_DEFAULT_AGEING_TIME 300
71         u32 ageing_time;
72         bool vlan_enabled_exists;
73         struct list_head bridges_list;
74         DECLARE_BITMAP(mids_bitmap, MLXSW_SP_MID_MAX);
75         const struct mlxsw_sp_bridge_ops *bridge_8021q_ops;
76         const struct mlxsw_sp_bridge_ops *bridge_8021d_ops;
77 };
78
79 struct mlxsw_sp_bridge_device {
80         struct net_device *dev;
81         struct list_head list;
82         struct list_head ports_list;
83         struct list_head mids_list;
84         u8 vlan_enabled:1,
85            multicast_enabled:1,
86            mrouter:1;
87         const struct mlxsw_sp_bridge_ops *ops;
88 };
89
90 struct mlxsw_sp_bridge_port {
91         struct net_device *dev;
92         struct mlxsw_sp_bridge_device *bridge_device;
93         struct list_head list;
94         struct list_head vlans_list;
95         unsigned int ref_count;
96         u8 stp_state;
97         unsigned long flags;
98         bool mrouter;
99         bool lagged;
100         union {
101                 u16 lag_id;
102                 u16 system_port;
103         };
104 };
105
106 struct mlxsw_sp_bridge_vlan {
107         struct list_head list;
108         struct list_head port_vlan_list;
109         u16 vid;
110 };
111
112 struct mlxsw_sp_bridge_ops {
113         int (*port_join)(struct mlxsw_sp_bridge_device *bridge_device,
114                          struct mlxsw_sp_bridge_port *bridge_port,
115                          struct mlxsw_sp_port *mlxsw_sp_port,
116                          struct netlink_ext_ack *extack);
117         void (*port_leave)(struct mlxsw_sp_bridge_device *bridge_device,
118                            struct mlxsw_sp_bridge_port *bridge_port,
119                            struct mlxsw_sp_port *mlxsw_sp_port);
120         struct mlxsw_sp_fid *
121                 (*fid_get)(struct mlxsw_sp_bridge_device *bridge_device,
122                            u16 vid);
123 };
124
125 static int
126 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
127                                struct mlxsw_sp_bridge_port *bridge_port,
128                                u16 fid_index);
129
130 static void
131 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
132                                struct mlxsw_sp_bridge_port *bridge_port);
133
134 static void
135 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
136                                    struct mlxsw_sp_bridge_device
137                                    *bridge_device);
138
139 static void
140 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
141                                  struct mlxsw_sp_bridge_port *bridge_port,
142                                  bool add);
143
144 static struct mlxsw_sp_bridge_device *
145 mlxsw_sp_bridge_device_find(const struct mlxsw_sp_bridge *bridge,
146                             const struct net_device *br_dev)
147 {
148         struct mlxsw_sp_bridge_device *bridge_device;
149
150         list_for_each_entry(bridge_device, &bridge->bridges_list, list)
151                 if (bridge_device->dev == br_dev)
152                         return bridge_device;
153
154         return NULL;
155 }
156
157 bool mlxsw_sp_bridge_device_is_offloaded(const struct mlxsw_sp *mlxsw_sp,
158                                          const struct net_device *br_dev)
159 {
160         return !!mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
161 }
162
163 static struct mlxsw_sp_bridge_device *
164 mlxsw_sp_bridge_device_create(struct mlxsw_sp_bridge *bridge,
165                               struct net_device *br_dev)
166 {
167         struct device *dev = bridge->mlxsw_sp->bus_info->dev;
168         struct mlxsw_sp_bridge_device *bridge_device;
169         bool vlan_enabled = br_vlan_enabled(br_dev);
170
171         if (vlan_enabled && bridge->vlan_enabled_exists) {
172                 dev_err(dev, "Only one VLAN-aware bridge is supported\n");
173                 return ERR_PTR(-EINVAL);
174         }
175
176         bridge_device = kzalloc(sizeof(*bridge_device), GFP_KERNEL);
177         if (!bridge_device)
178                 return ERR_PTR(-ENOMEM);
179
180         bridge_device->dev = br_dev;
181         bridge_device->vlan_enabled = vlan_enabled;
182         bridge_device->multicast_enabled = br_multicast_enabled(br_dev);
183         bridge_device->mrouter = br_multicast_router(br_dev);
184         INIT_LIST_HEAD(&bridge_device->ports_list);
185         if (vlan_enabled) {
186                 bridge->vlan_enabled_exists = true;
187                 bridge_device->ops = bridge->bridge_8021q_ops;
188         } else {
189                 bridge_device->ops = bridge->bridge_8021d_ops;
190         }
191         INIT_LIST_HEAD(&bridge_device->mids_list);
192         list_add(&bridge_device->list, &bridge->bridges_list);
193
194         return bridge_device;
195 }
196
197 static void
198 mlxsw_sp_bridge_device_destroy(struct mlxsw_sp_bridge *bridge,
199                                struct mlxsw_sp_bridge_device *bridge_device)
200 {
201         list_del(&bridge_device->list);
202         if (bridge_device->vlan_enabled)
203                 bridge->vlan_enabled_exists = false;
204         WARN_ON(!list_empty(&bridge_device->ports_list));
205         WARN_ON(!list_empty(&bridge_device->mids_list));
206         kfree(bridge_device);
207 }
208
209 static struct mlxsw_sp_bridge_device *
210 mlxsw_sp_bridge_device_get(struct mlxsw_sp_bridge *bridge,
211                            struct net_device *br_dev)
212 {
213         struct mlxsw_sp_bridge_device *bridge_device;
214
215         bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
216         if (bridge_device)
217                 return bridge_device;
218
219         return mlxsw_sp_bridge_device_create(bridge, br_dev);
220 }
221
222 static void
223 mlxsw_sp_bridge_device_put(struct mlxsw_sp_bridge *bridge,
224                            struct mlxsw_sp_bridge_device *bridge_device)
225 {
226         if (list_empty(&bridge_device->ports_list))
227                 mlxsw_sp_bridge_device_destroy(bridge, bridge_device);
228 }
229
230 static struct mlxsw_sp_bridge_port *
231 __mlxsw_sp_bridge_port_find(const struct mlxsw_sp_bridge_device *bridge_device,
232                             const struct net_device *brport_dev)
233 {
234         struct mlxsw_sp_bridge_port *bridge_port;
235
236         list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
237                 if (bridge_port->dev == brport_dev)
238                         return bridge_port;
239         }
240
241         return NULL;
242 }
243
244 struct mlxsw_sp_bridge_port *
245 mlxsw_sp_bridge_port_find(struct mlxsw_sp_bridge *bridge,
246                           struct net_device *brport_dev)
247 {
248         struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
249         struct mlxsw_sp_bridge_device *bridge_device;
250
251         if (!br_dev)
252                 return NULL;
253
254         bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
255         if (!bridge_device)
256                 return NULL;
257
258         return __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
259 }
260
261 static struct mlxsw_sp_bridge_port *
262 mlxsw_sp_bridge_port_create(struct mlxsw_sp_bridge_device *bridge_device,
263                             struct net_device *brport_dev)
264 {
265         struct mlxsw_sp_bridge_port *bridge_port;
266         struct mlxsw_sp_port *mlxsw_sp_port;
267
268         bridge_port = kzalloc(sizeof(*bridge_port), GFP_KERNEL);
269         if (!bridge_port)
270                 return NULL;
271
272         mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(brport_dev);
273         bridge_port->lagged = mlxsw_sp_port->lagged;
274         if (bridge_port->lagged)
275                 bridge_port->lag_id = mlxsw_sp_port->lag_id;
276         else
277                 bridge_port->system_port = mlxsw_sp_port->local_port;
278         bridge_port->dev = brport_dev;
279         bridge_port->bridge_device = bridge_device;
280         bridge_port->stp_state = BR_STATE_DISABLED;
281         bridge_port->flags = BR_LEARNING | BR_FLOOD | BR_LEARNING_SYNC |
282                              BR_MCAST_FLOOD;
283         INIT_LIST_HEAD(&bridge_port->vlans_list);
284         list_add(&bridge_port->list, &bridge_device->ports_list);
285         bridge_port->ref_count = 1;
286
287         return bridge_port;
288 }
289
290 static void
291 mlxsw_sp_bridge_port_destroy(struct mlxsw_sp_bridge_port *bridge_port)
292 {
293         list_del(&bridge_port->list);
294         WARN_ON(!list_empty(&bridge_port->vlans_list));
295         kfree(bridge_port);
296 }
297
298 static bool
299 mlxsw_sp_bridge_port_should_destroy(const struct mlxsw_sp_bridge_port *
300                                     bridge_port)
301 {
302         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_port->dev);
303
304         /* In case ports were pulled from out of a bridged LAG, then
305          * it's possible the reference count isn't zero, yet the bridge
306          * port should be destroyed, as it's no longer an upper of ours.
307          */
308         if (!mlxsw_sp && list_empty(&bridge_port->vlans_list))
309                 return true;
310         else if (bridge_port->ref_count == 0)
311                 return true;
312         else
313                 return false;
314 }
315
316 static struct mlxsw_sp_bridge_port *
317 mlxsw_sp_bridge_port_get(struct mlxsw_sp_bridge *bridge,
318                          struct net_device *brport_dev)
319 {
320         struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
321         struct mlxsw_sp_bridge_device *bridge_device;
322         struct mlxsw_sp_bridge_port *bridge_port;
323         int err;
324
325         bridge_port = mlxsw_sp_bridge_port_find(bridge, brport_dev);
326         if (bridge_port) {
327                 bridge_port->ref_count++;
328                 return bridge_port;
329         }
330
331         bridge_device = mlxsw_sp_bridge_device_get(bridge, br_dev);
332         if (IS_ERR(bridge_device))
333                 return ERR_CAST(bridge_device);
334
335         bridge_port = mlxsw_sp_bridge_port_create(bridge_device, brport_dev);
336         if (!bridge_port) {
337                 err = -ENOMEM;
338                 goto err_bridge_port_create;
339         }
340
341         return bridge_port;
342
343 err_bridge_port_create:
344         mlxsw_sp_bridge_device_put(bridge, bridge_device);
345         return ERR_PTR(err);
346 }
347
348 static void mlxsw_sp_bridge_port_put(struct mlxsw_sp_bridge *bridge,
349                                      struct mlxsw_sp_bridge_port *bridge_port)
350 {
351         struct mlxsw_sp_bridge_device *bridge_device;
352
353         bridge_port->ref_count--;
354         if (!mlxsw_sp_bridge_port_should_destroy(bridge_port))
355                 return;
356         bridge_device = bridge_port->bridge_device;
357         mlxsw_sp_bridge_port_destroy(bridge_port);
358         mlxsw_sp_bridge_device_put(bridge, bridge_device);
359 }
360
361 static struct mlxsw_sp_port_vlan *
362 mlxsw_sp_port_vlan_find_by_bridge(struct mlxsw_sp_port *mlxsw_sp_port,
363                                   const struct mlxsw_sp_bridge_device *
364                                   bridge_device,
365                                   u16 vid)
366 {
367         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
368
369         list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
370                             list) {
371                 if (!mlxsw_sp_port_vlan->bridge_port)
372                         continue;
373                 if (mlxsw_sp_port_vlan->bridge_port->bridge_device !=
374                     bridge_device)
375                         continue;
376                 if (bridge_device->vlan_enabled &&
377                     mlxsw_sp_port_vlan->vid != vid)
378                         continue;
379                 return mlxsw_sp_port_vlan;
380         }
381
382         return NULL;
383 }
384
385 static struct mlxsw_sp_port_vlan*
386 mlxsw_sp_port_vlan_find_by_fid(struct mlxsw_sp_port *mlxsw_sp_port,
387                                u16 fid_index)
388 {
389         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
390
391         list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
392                             list) {
393                 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
394
395                 if (fid && mlxsw_sp_fid_index(fid) == fid_index)
396                         return mlxsw_sp_port_vlan;
397         }
398
399         return NULL;
400 }
401
402 static struct mlxsw_sp_bridge_vlan *
403 mlxsw_sp_bridge_vlan_find(const struct mlxsw_sp_bridge_port *bridge_port,
404                           u16 vid)
405 {
406         struct mlxsw_sp_bridge_vlan *bridge_vlan;
407
408         list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
409                 if (bridge_vlan->vid == vid)
410                         return bridge_vlan;
411         }
412
413         return NULL;
414 }
415
416 static struct mlxsw_sp_bridge_vlan *
417 mlxsw_sp_bridge_vlan_create(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
418 {
419         struct mlxsw_sp_bridge_vlan *bridge_vlan;
420
421         bridge_vlan = kzalloc(sizeof(*bridge_vlan), GFP_KERNEL);
422         if (!bridge_vlan)
423                 return NULL;
424
425         INIT_LIST_HEAD(&bridge_vlan->port_vlan_list);
426         bridge_vlan->vid = vid;
427         list_add(&bridge_vlan->list, &bridge_port->vlans_list);
428
429         return bridge_vlan;
430 }
431
432 static void
433 mlxsw_sp_bridge_vlan_destroy(struct mlxsw_sp_bridge_vlan *bridge_vlan)
434 {
435         list_del(&bridge_vlan->list);
436         WARN_ON(!list_empty(&bridge_vlan->port_vlan_list));
437         kfree(bridge_vlan);
438 }
439
440 static struct mlxsw_sp_bridge_vlan *
441 mlxsw_sp_bridge_vlan_get(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
442 {
443         struct mlxsw_sp_bridge_vlan *bridge_vlan;
444
445         bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
446         if (bridge_vlan)
447                 return bridge_vlan;
448
449         return mlxsw_sp_bridge_vlan_create(bridge_port, vid);
450 }
451
452 static void mlxsw_sp_bridge_vlan_put(struct mlxsw_sp_bridge_vlan *bridge_vlan)
453 {
454         if (list_empty(&bridge_vlan->port_vlan_list))
455                 mlxsw_sp_bridge_vlan_destroy(bridge_vlan);
456 }
457
458 static void mlxsw_sp_port_bridge_flags_get(struct mlxsw_sp_bridge *bridge,
459                                            struct net_device *dev,
460                                            unsigned long *brport_flags)
461 {
462         struct mlxsw_sp_bridge_port *bridge_port;
463
464         bridge_port = mlxsw_sp_bridge_port_find(bridge, dev);
465         if (WARN_ON(!bridge_port))
466                 return;
467
468         memcpy(brport_flags, &bridge_port->flags, sizeof(*brport_flags));
469 }
470
471 static int mlxsw_sp_port_attr_get(struct net_device *dev,
472                                   struct switchdev_attr *attr)
473 {
474         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
475         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
476
477         switch (attr->id) {
478         case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
479                 attr->u.ppid.id_len = sizeof(mlxsw_sp->base_mac);
480                 memcpy(&attr->u.ppid.id, &mlxsw_sp->base_mac,
481                        attr->u.ppid.id_len);
482                 break;
483         case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
484                 mlxsw_sp_port_bridge_flags_get(mlxsw_sp->bridge, attr->orig_dev,
485                                                &attr->u.brport_flags);
486                 break;
487         case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
488                 attr->u.brport_flags_support = BR_LEARNING | BR_FLOOD |
489                                                BR_MCAST_FLOOD;
490                 break;
491         default:
492                 return -EOPNOTSUPP;
493         }
494
495         return 0;
496 }
497
498 static int
499 mlxsw_sp_port_bridge_vlan_stp_set(struct mlxsw_sp_port *mlxsw_sp_port,
500                                   struct mlxsw_sp_bridge_vlan *bridge_vlan,
501                                   u8 state)
502 {
503         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
504
505         list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
506                             bridge_vlan_node) {
507                 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
508                         continue;
509                 return mlxsw_sp_port_vid_stp_set(mlxsw_sp_port,
510                                                  bridge_vlan->vid, state);
511         }
512
513         return 0;
514 }
515
516 static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
517                                             struct switchdev_trans *trans,
518                                             struct net_device *orig_dev,
519                                             u8 state)
520 {
521         struct mlxsw_sp_bridge_port *bridge_port;
522         struct mlxsw_sp_bridge_vlan *bridge_vlan;
523         int err;
524
525         if (switchdev_trans_ph_prepare(trans))
526                 return 0;
527
528         /* It's possible we failed to enslave the port, yet this
529          * operation is executed due to it being deferred.
530          */
531         bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
532                                                 orig_dev);
533         if (!bridge_port)
534                 return 0;
535
536         list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
537                 err = mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port,
538                                                         bridge_vlan, state);
539                 if (err)
540                         goto err_port_bridge_vlan_stp_set;
541         }
542
543         bridge_port->stp_state = state;
544
545         return 0;
546
547 err_port_bridge_vlan_stp_set:
548         list_for_each_entry_continue_reverse(bridge_vlan,
549                                              &bridge_port->vlans_list, list)
550                 mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port, bridge_vlan,
551                                                   bridge_port->stp_state);
552         return err;
553 }
554
555 static int
556 mlxsw_sp_port_bridge_vlan_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
557                                     struct mlxsw_sp_bridge_vlan *bridge_vlan,
558                                     enum mlxsw_sp_flood_type packet_type,
559                                     bool member)
560 {
561         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
562
563         list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
564                             bridge_vlan_node) {
565                 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
566                         continue;
567                 return mlxsw_sp_fid_flood_set(mlxsw_sp_port_vlan->fid,
568                                               packet_type,
569                                               mlxsw_sp_port->local_port,
570                                               member);
571         }
572
573         return 0;
574 }
575
576 static int
577 mlxsw_sp_bridge_port_flood_table_set(struct mlxsw_sp_port *mlxsw_sp_port,
578                                      struct mlxsw_sp_bridge_port *bridge_port,
579                                      enum mlxsw_sp_flood_type packet_type,
580                                      bool member)
581 {
582         struct mlxsw_sp_bridge_vlan *bridge_vlan;
583         int err;
584
585         list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
586                 err = mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port,
587                                                           bridge_vlan,
588                                                           packet_type,
589                                                           member);
590                 if (err)
591                         goto err_port_bridge_vlan_flood_set;
592         }
593
594         return 0;
595
596 err_port_bridge_vlan_flood_set:
597         list_for_each_entry_continue_reverse(bridge_vlan,
598                                              &bridge_port->vlans_list, list)
599                 mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port, bridge_vlan,
600                                                     packet_type, !member);
601         return err;
602 }
603
604 static int
605 mlxsw_sp_port_bridge_vlan_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
606                                        struct mlxsw_sp_bridge_vlan *bridge_vlan,
607                                        bool set)
608 {
609         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
610         u16 vid = bridge_vlan->vid;
611
612         list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
613                             bridge_vlan_node) {
614                 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
615                         continue;
616                 return mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, set);
617         }
618
619         return 0;
620 }
621
622 static int
623 mlxsw_sp_bridge_port_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
624                                   struct mlxsw_sp_bridge_port *bridge_port,
625                                   bool set)
626 {
627         struct mlxsw_sp_bridge_vlan *bridge_vlan;
628         int err;
629
630         list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
631                 err = mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
632                                                              bridge_vlan, set);
633                 if (err)
634                         goto err_port_bridge_vlan_learning_set;
635         }
636
637         return 0;
638
639 err_port_bridge_vlan_learning_set:
640         list_for_each_entry_continue_reverse(bridge_vlan,
641                                              &bridge_port->vlans_list, list)
642                 mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
643                                                        bridge_vlan, !set);
644         return err;
645 }
646
647 static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
648                                            struct switchdev_trans *trans,
649                                            struct net_device *orig_dev,
650                                            unsigned long brport_flags)
651 {
652         struct mlxsw_sp_bridge_port *bridge_port;
653         int err;
654
655         if (switchdev_trans_ph_prepare(trans))
656                 return 0;
657
658         bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
659                                                 orig_dev);
660         if (!bridge_port)
661                 return 0;
662
663         err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
664                                                    MLXSW_SP_FLOOD_TYPE_UC,
665                                                    brport_flags & BR_FLOOD);
666         if (err)
667                 return err;
668
669         err = mlxsw_sp_bridge_port_learning_set(mlxsw_sp_port, bridge_port,
670                                                 brport_flags & BR_LEARNING);
671         if (err)
672                 return err;
673
674         if (bridge_port->bridge_device->multicast_enabled)
675                 goto out;
676
677         err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
678                                                    MLXSW_SP_FLOOD_TYPE_MC,
679                                                    brport_flags &
680                                                    BR_MCAST_FLOOD);
681         if (err)
682                 return err;
683
684 out:
685         memcpy(&bridge_port->flags, &brport_flags, sizeof(brport_flags));
686         return 0;
687 }
688
689 static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
690 {
691         char sfdat_pl[MLXSW_REG_SFDAT_LEN];
692         int err;
693
694         mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
695         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
696         if (err)
697                 return err;
698         mlxsw_sp->bridge->ageing_time = ageing_time;
699         return 0;
700 }
701
702 static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
703                                             struct switchdev_trans *trans,
704                                             unsigned long ageing_clock_t)
705 {
706         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
707         unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
708         u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
709
710         if (switchdev_trans_ph_prepare(trans)) {
711                 if (ageing_time < MLXSW_SP_MIN_AGEING_TIME ||
712                     ageing_time > MLXSW_SP_MAX_AGEING_TIME)
713                         return -ERANGE;
714                 else
715                         return 0;
716         }
717
718         return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
719 }
720
721 static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
722                                           struct switchdev_trans *trans,
723                                           struct net_device *orig_dev,
724                                           bool vlan_enabled)
725 {
726         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
727         struct mlxsw_sp_bridge_device *bridge_device;
728
729         if (!switchdev_trans_ph_prepare(trans))
730                 return 0;
731
732         bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
733         if (WARN_ON(!bridge_device))
734                 return -EINVAL;
735
736         if (bridge_device->vlan_enabled == vlan_enabled)
737                 return 0;
738
739         netdev_err(bridge_device->dev, "VLAN filtering can't be changed for existing bridge\n");
740         return -EINVAL;
741 }
742
743 static int mlxsw_sp_port_attr_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
744                                           struct switchdev_trans *trans,
745                                           struct net_device *orig_dev,
746                                           bool is_port_mrouter)
747 {
748         struct mlxsw_sp_bridge_port *bridge_port;
749         int err;
750
751         if (switchdev_trans_ph_prepare(trans))
752                 return 0;
753
754         bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
755                                                 orig_dev);
756         if (!bridge_port)
757                 return 0;
758
759         if (!bridge_port->bridge_device->multicast_enabled)
760                 goto out;
761
762         err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
763                                                    MLXSW_SP_FLOOD_TYPE_MC,
764                                                    is_port_mrouter);
765         if (err)
766                 return err;
767
768         mlxsw_sp_port_mrouter_update_mdb(mlxsw_sp_port, bridge_port,
769                                          is_port_mrouter);
770 out:
771         bridge_port->mrouter = is_port_mrouter;
772         return 0;
773 }
774
775 static bool mlxsw_sp_mc_flood(const struct mlxsw_sp_bridge_port *bridge_port)
776 {
777         const struct mlxsw_sp_bridge_device *bridge_device;
778
779         bridge_device = bridge_port->bridge_device;
780         return bridge_device->multicast_enabled ? bridge_port->mrouter :
781                                         bridge_port->flags & BR_MCAST_FLOOD;
782 }
783
784 static int mlxsw_sp_port_mc_disabled_set(struct mlxsw_sp_port *mlxsw_sp_port,
785                                          struct switchdev_trans *trans,
786                                          struct net_device *orig_dev,
787                                          bool mc_disabled)
788 {
789         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
790         struct mlxsw_sp_bridge_device *bridge_device;
791         struct mlxsw_sp_bridge_port *bridge_port;
792         int err;
793
794         if (switchdev_trans_ph_prepare(trans))
795                 return 0;
796
797         /* It's possible we failed to enslave the port, yet this
798          * operation is executed due to it being deferred.
799          */
800         bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
801         if (!bridge_device)
802                 return 0;
803
804         if (bridge_device->multicast_enabled != !mc_disabled) {
805                 bridge_device->multicast_enabled = !mc_disabled;
806                 mlxsw_sp_bridge_mdb_mc_enable_sync(mlxsw_sp_port,
807                                                    bridge_device);
808         }
809
810         list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
811                 enum mlxsw_sp_flood_type packet_type = MLXSW_SP_FLOOD_TYPE_MC;
812                 bool member = mlxsw_sp_mc_flood(bridge_port);
813
814                 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
815                                                            bridge_port,
816                                                            packet_type, member);
817                 if (err)
818                         return err;
819         }
820
821         bridge_device->multicast_enabled = !mc_disabled;
822
823         return 0;
824 }
825
826 static int mlxsw_sp_smid_router_port_set(struct mlxsw_sp *mlxsw_sp,
827                                          u16 mid_idx, bool add)
828 {
829         char *smid_pl;
830         int err;
831
832         smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
833         if (!smid_pl)
834                 return -ENOMEM;
835
836         mlxsw_reg_smid_pack(smid_pl, mid_idx,
837                             mlxsw_sp_router_port(mlxsw_sp), add);
838         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
839         kfree(smid_pl);
840         return err;
841 }
842
843 static void
844 mlxsw_sp_bridge_mrouter_update_mdb(struct mlxsw_sp *mlxsw_sp,
845                                    struct mlxsw_sp_bridge_device *bridge_device,
846                                    bool add)
847 {
848         struct mlxsw_sp_mid *mid;
849
850         list_for_each_entry(mid, &bridge_device->mids_list, list)
851                 mlxsw_sp_smid_router_port_set(mlxsw_sp, mid->mid, add);
852 }
853
854 static int
855 mlxsw_sp_port_attr_br_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
856                                   struct switchdev_trans *trans,
857                                   struct net_device *orig_dev,
858                                   bool is_mrouter)
859 {
860         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
861         struct mlxsw_sp_bridge_device *bridge_device;
862
863         if (switchdev_trans_ph_prepare(trans))
864                 return 0;
865
866         /* It's possible we failed to enslave the port, yet this
867          * operation is executed due to it being deferred.
868          */
869         bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
870         if (!bridge_device)
871                 return 0;
872
873         if (bridge_device->mrouter != is_mrouter)
874                 mlxsw_sp_bridge_mrouter_update_mdb(mlxsw_sp, bridge_device,
875                                                    is_mrouter);
876         bridge_device->mrouter = is_mrouter;
877         return 0;
878 }
879
880 static int mlxsw_sp_port_attr_set(struct net_device *dev,
881                                   const struct switchdev_attr *attr,
882                                   struct switchdev_trans *trans)
883 {
884         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
885         int err;
886
887         switch (attr->id) {
888         case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
889                 err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port, trans,
890                                                        attr->orig_dev,
891                                                        attr->u.stp_state);
892                 break;
893         case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
894                 err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port, trans,
895                                                       attr->orig_dev,
896                                                       attr->u.brport_flags);
897                 break;
898         case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
899                 err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port, trans,
900                                                        attr->u.ageing_time);
901                 break;
902         case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
903                 err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port, trans,
904                                                      attr->orig_dev,
905                                                      attr->u.vlan_filtering);
906                 break;
907         case SWITCHDEV_ATTR_ID_PORT_MROUTER:
908                 err = mlxsw_sp_port_attr_mrouter_set(mlxsw_sp_port, trans,
909                                                      attr->orig_dev,
910                                                      attr->u.mrouter);
911                 break;
912         case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
913                 err = mlxsw_sp_port_mc_disabled_set(mlxsw_sp_port, trans,
914                                                     attr->orig_dev,
915                                                     attr->u.mc_disabled);
916                 break;
917         case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
918                 err = mlxsw_sp_port_attr_br_mrouter_set(mlxsw_sp_port, trans,
919                                                         attr->orig_dev,
920                                                         attr->u.mrouter);
921                 break;
922         default:
923                 err = -EOPNOTSUPP;
924                 break;
925         }
926
927         if (switchdev_trans_ph_commit(trans))
928                 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
929
930         return err;
931 }
932
933 static int
934 mlxsw_sp_port_vlan_fid_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
935                             struct mlxsw_sp_bridge_port *bridge_port)
936 {
937         struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
938         struct mlxsw_sp_bridge_device *bridge_device;
939         u8 local_port = mlxsw_sp_port->local_port;
940         u16 vid = mlxsw_sp_port_vlan->vid;
941         struct mlxsw_sp_fid *fid;
942         int err;
943
944         bridge_device = bridge_port->bridge_device;
945         fid = bridge_device->ops->fid_get(bridge_device, vid);
946         if (IS_ERR(fid))
947                 return PTR_ERR(fid);
948
949         err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port,
950                                      bridge_port->flags & BR_FLOOD);
951         if (err)
952                 goto err_fid_uc_flood_set;
953
954         err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port,
955                                      mlxsw_sp_mc_flood(bridge_port));
956         if (err)
957                 goto err_fid_mc_flood_set;
958
959         err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port,
960                                      true);
961         if (err)
962                 goto err_fid_bc_flood_set;
963
964         err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
965         if (err)
966                 goto err_fid_port_vid_map;
967
968         mlxsw_sp_port_vlan->fid = fid;
969
970         return 0;
971
972 err_fid_port_vid_map:
973         mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
974 err_fid_bc_flood_set:
975         mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
976 err_fid_mc_flood_set:
977         mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
978 err_fid_uc_flood_set:
979         mlxsw_sp_fid_put(fid);
980         return err;
981 }
982
983 static void
984 mlxsw_sp_port_vlan_fid_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
985 {
986         struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
987         struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
988         u8 local_port = mlxsw_sp_port->local_port;
989         u16 vid = mlxsw_sp_port_vlan->vid;
990
991         mlxsw_sp_port_vlan->fid = NULL;
992         mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
993         mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
994         mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
995         mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
996         mlxsw_sp_fid_put(fid);
997 }
998
999 static u16
1000 mlxsw_sp_port_pvid_determine(const struct mlxsw_sp_port *mlxsw_sp_port,
1001                              u16 vid, bool is_pvid)
1002 {
1003         if (is_pvid)
1004                 return vid;
1005         else if (mlxsw_sp_port->pvid == vid)
1006                 return 0;       /* Dis-allow untagged packets */
1007         else
1008                 return mlxsw_sp_port->pvid;
1009 }
1010
1011 static int
1012 mlxsw_sp_port_vlan_bridge_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
1013                                struct mlxsw_sp_bridge_port *bridge_port)
1014 {
1015         struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1016         struct mlxsw_sp_bridge_vlan *bridge_vlan;
1017         u16 vid = mlxsw_sp_port_vlan->vid;
1018         int err;
1019
1020         /* No need to continue if only VLAN flags were changed */
1021         if (mlxsw_sp_port_vlan->bridge_port) {
1022                 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1023                 return 0;
1024         }
1025
1026         err = mlxsw_sp_port_vlan_fid_join(mlxsw_sp_port_vlan, bridge_port);
1027         if (err)
1028                 return err;
1029
1030         err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid,
1031                                              bridge_port->flags & BR_LEARNING);
1032         if (err)
1033                 goto err_port_vid_learning_set;
1034
1035         err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid,
1036                                         bridge_port->stp_state);
1037         if (err)
1038                 goto err_port_vid_stp_set;
1039
1040         bridge_vlan = mlxsw_sp_bridge_vlan_get(bridge_port, vid);
1041         if (!bridge_vlan) {
1042                 err = -ENOMEM;
1043                 goto err_bridge_vlan_get;
1044         }
1045
1046         list_add(&mlxsw_sp_port_vlan->bridge_vlan_node,
1047                  &bridge_vlan->port_vlan_list);
1048
1049         mlxsw_sp_bridge_port_get(mlxsw_sp_port->mlxsw_sp->bridge,
1050                                  bridge_port->dev);
1051         mlxsw_sp_port_vlan->bridge_port = bridge_port;
1052
1053         return 0;
1054
1055 err_bridge_vlan_get:
1056         mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1057 err_port_vid_stp_set:
1058         mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1059 err_port_vid_learning_set:
1060         mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1061         return err;
1062 }
1063
1064 void
1065 mlxsw_sp_port_vlan_bridge_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1066 {
1067         struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1068         struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
1069         struct mlxsw_sp_bridge_vlan *bridge_vlan;
1070         struct mlxsw_sp_bridge_port *bridge_port;
1071         u16 vid = mlxsw_sp_port_vlan->vid;
1072         bool last_port, last_vlan;
1073
1074         if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021Q &&
1075                     mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021D))
1076                 return;
1077
1078         bridge_port = mlxsw_sp_port_vlan->bridge_port;
1079         last_vlan = list_is_singular(&bridge_port->vlans_list);
1080         bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
1081         last_port = list_is_singular(&bridge_vlan->port_vlan_list);
1082
1083         list_del(&mlxsw_sp_port_vlan->bridge_vlan_node);
1084         mlxsw_sp_bridge_vlan_put(bridge_vlan);
1085         mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1086         mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1087         if (last_port)
1088                 mlxsw_sp_bridge_port_fdb_flush(mlxsw_sp_port->mlxsw_sp,
1089                                                bridge_port,
1090                                                mlxsw_sp_fid_index(fid));
1091         if (last_vlan)
1092                 mlxsw_sp_bridge_port_mdb_flush(mlxsw_sp_port, bridge_port);
1093
1094         mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1095
1096         mlxsw_sp_bridge_port_put(mlxsw_sp_port->mlxsw_sp->bridge, bridge_port);
1097         mlxsw_sp_port_vlan->bridge_port = NULL;
1098 }
1099
1100 static int
1101 mlxsw_sp_bridge_port_vlan_add(struct mlxsw_sp_port *mlxsw_sp_port,
1102                               struct mlxsw_sp_bridge_port *bridge_port,
1103                               u16 vid, bool is_untagged, bool is_pvid)
1104 {
1105         u16 pvid = mlxsw_sp_port_pvid_determine(mlxsw_sp_port, vid, is_pvid);
1106         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1107         u16 old_pvid = mlxsw_sp_port->pvid;
1108         int err;
1109
1110         mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_get(mlxsw_sp_port, vid);
1111         if (IS_ERR(mlxsw_sp_port_vlan))
1112                 return PTR_ERR(mlxsw_sp_port_vlan);
1113
1114         err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true,
1115                                      is_untagged);
1116         if (err)
1117                 goto err_port_vlan_set;
1118
1119         err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1120         if (err)
1121                 goto err_port_pvid_set;
1122
1123         err = mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
1124         if (err)
1125                 goto err_port_vlan_bridge_join;
1126
1127         return 0;
1128
1129 err_port_vlan_bridge_join:
1130         mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid);
1131 err_port_pvid_set:
1132         mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1133 err_port_vlan_set:
1134         mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1135         return err;
1136 }
1137
1138 static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
1139                                    const struct switchdev_obj_port_vlan *vlan,
1140                                    struct switchdev_trans *trans)
1141 {
1142         bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1143         bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1144         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1145         struct net_device *orig_dev = vlan->obj.orig_dev;
1146         struct mlxsw_sp_bridge_port *bridge_port;
1147         u16 vid;
1148
1149         if (netif_is_bridge_master(orig_dev))
1150                 return -EOPNOTSUPP;
1151
1152         if (switchdev_trans_ph_prepare(trans))
1153                 return 0;
1154
1155         bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1156         if (WARN_ON(!bridge_port))
1157                 return -EINVAL;
1158
1159         if (!bridge_port->bridge_device->vlan_enabled)
1160                 return 0;
1161
1162         for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1163                 int err;
1164
1165                 err = mlxsw_sp_bridge_port_vlan_add(mlxsw_sp_port, bridge_port,
1166                                                     vid, flag_untagged,
1167                                                     flag_pvid);
1168                 if (err)
1169                         return err;
1170         }
1171
1172         return 0;
1173 }
1174
1175 static enum mlxsw_reg_sfdf_flush_type mlxsw_sp_fdb_flush_type(bool lagged)
1176 {
1177         return lagged ? MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID :
1178                         MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID;
1179 }
1180
1181 static int
1182 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
1183                                struct mlxsw_sp_bridge_port *bridge_port,
1184                                u16 fid_index)
1185 {
1186         bool lagged = bridge_port->lagged;
1187         char sfdf_pl[MLXSW_REG_SFDF_LEN];
1188         u16 system_port;
1189
1190         system_port = lagged ? bridge_port->lag_id : bridge_port->system_port;
1191         mlxsw_reg_sfdf_pack(sfdf_pl, mlxsw_sp_fdb_flush_type(lagged));
1192         mlxsw_reg_sfdf_fid_set(sfdf_pl, fid_index);
1193         mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl, system_port);
1194
1195         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl);
1196 }
1197
1198 static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
1199 {
1200         return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
1201                          MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY;
1202 }
1203
1204 static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
1205 {
1206         return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
1207                         MLXSW_REG_SFD_OP_WRITE_REMOVE;
1208 }
1209
1210 static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1211                                      const char *mac, u16 fid, bool adding,
1212                                      enum mlxsw_reg_sfd_rec_action action,
1213                                      bool dynamic)
1214 {
1215         char *sfd_pl;
1216         u8 num_rec;
1217         int err;
1218
1219         sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1220         if (!sfd_pl)
1221                 return -ENOMEM;
1222
1223         mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1224         mlxsw_reg_sfd_uc_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1225                               mac, fid, action, local_port);
1226         num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1227         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1228         if (err)
1229                 goto out;
1230
1231         if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1232                 err = -EBUSY;
1233
1234 out:
1235         kfree(sfd_pl);
1236         return err;
1237 }
1238
1239 static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1240                                    const char *mac, u16 fid, bool adding,
1241                                    bool dynamic)
1242 {
1243         return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding,
1244                                          MLXSW_REG_SFD_REC_ACTION_NOP, dynamic);
1245 }
1246
1247 int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid,
1248                         bool adding)
1249 {
1250         return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding,
1251                                          MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER,
1252                                          false);
1253 }
1254
1255 static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
1256                                        const char *mac, u16 fid, u16 lag_vid,
1257                                        bool adding, bool dynamic)
1258 {
1259         char *sfd_pl;
1260         u8 num_rec;
1261         int err;
1262
1263         sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1264         if (!sfd_pl)
1265                 return -ENOMEM;
1266
1267         mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1268         mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1269                                   mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
1270                                   lag_vid, lag_id);
1271         num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1272         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1273         if (err)
1274                 goto out;
1275
1276         if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1277                 err = -EBUSY;
1278
1279 out:
1280         kfree(sfd_pl);
1281         return err;
1282 }
1283
1284 static int
1285 mlxsw_sp_port_fdb_set(struct mlxsw_sp_port *mlxsw_sp_port,
1286                       struct switchdev_notifier_fdb_info *fdb_info, bool adding)
1287 {
1288         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1289         struct net_device *orig_dev = fdb_info->info.dev;
1290         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1291         struct mlxsw_sp_bridge_device *bridge_device;
1292         struct mlxsw_sp_bridge_port *bridge_port;
1293         u16 fid_index, vid;
1294
1295         bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1296         if (!bridge_port)
1297                 return -EINVAL;
1298
1299         bridge_device = bridge_port->bridge_device;
1300         mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1301                                                                bridge_device,
1302                                                                fdb_info->vid);
1303         if (!mlxsw_sp_port_vlan)
1304                 return 0;
1305
1306         fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1307         vid = mlxsw_sp_port_vlan->vid;
1308
1309         if (!bridge_port->lagged)
1310                 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp,
1311                                                bridge_port->system_port,
1312                                                fdb_info->addr, fid_index,
1313                                                adding, false);
1314         else
1315                 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp,
1316                                                    bridge_port->lag_id,
1317                                                    fdb_info->addr, fid_index,
1318                                                    vid, adding, false);
1319 }
1320
1321 static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
1322                                 u16 fid, u16 mid_idx, bool adding)
1323 {
1324         char *sfd_pl;
1325         u8 num_rec;
1326         int err;
1327
1328         sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1329         if (!sfd_pl)
1330                 return -ENOMEM;
1331
1332         mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1333         mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
1334                               MLXSW_REG_SFD_REC_ACTION_NOP, mid_idx);
1335         num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1336         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1337         if (err)
1338                 goto out;
1339
1340         if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1341                 err = -EBUSY;
1342
1343 out:
1344         kfree(sfd_pl);
1345         return err;
1346 }
1347
1348 static int mlxsw_sp_port_smid_full_entry(struct mlxsw_sp *mlxsw_sp, u16 mid_idx,
1349                                          long *ports_bitmap,
1350                                          bool set_router_port)
1351 {
1352         char *smid_pl;
1353         int err, i;
1354
1355         smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1356         if (!smid_pl)
1357                 return -ENOMEM;
1358
1359         mlxsw_reg_smid_pack(smid_pl, mid_idx, 0, false);
1360         for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++) {
1361                 if (mlxsw_sp->ports[i])
1362                         mlxsw_reg_smid_port_mask_set(smid_pl, i, 1);
1363         }
1364
1365         mlxsw_reg_smid_port_mask_set(smid_pl,
1366                                      mlxsw_sp_router_port(mlxsw_sp), 1);
1367
1368         for_each_set_bit(i, ports_bitmap, mlxsw_core_max_ports(mlxsw_sp->core))
1369                 mlxsw_reg_smid_port_set(smid_pl, i, 1);
1370
1371         mlxsw_reg_smid_port_set(smid_pl, mlxsw_sp_router_port(mlxsw_sp),
1372                                 set_router_port);
1373
1374         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1375         kfree(smid_pl);
1376         return err;
1377 }
1378
1379 static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port,
1380                                   u16 mid_idx, bool add)
1381 {
1382         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1383         char *smid_pl;
1384         int err;
1385
1386         smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1387         if (!smid_pl)
1388                 return -ENOMEM;
1389
1390         mlxsw_reg_smid_pack(smid_pl, mid_idx, mlxsw_sp_port->local_port, add);
1391         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1392         kfree(smid_pl);
1393         return err;
1394 }
1395
1396 static struct
1397 mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp_bridge_device *bridge_device,
1398                                 const unsigned char *addr,
1399                                 u16 fid)
1400 {
1401         struct mlxsw_sp_mid *mid;
1402
1403         list_for_each_entry(mid, &bridge_device->mids_list, list) {
1404                 if (ether_addr_equal(mid->addr, addr) && mid->fid == fid)
1405                         return mid;
1406         }
1407         return NULL;
1408 }
1409
1410 static void
1411 mlxsw_sp_bridge_port_get_ports_bitmap(struct mlxsw_sp *mlxsw_sp,
1412                                       struct mlxsw_sp_bridge_port *bridge_port,
1413                                       unsigned long *ports_bitmap)
1414 {
1415         struct mlxsw_sp_port *mlxsw_sp_port;
1416         u64 max_lag_members, i;
1417         int lag_id;
1418
1419         if (!bridge_port->lagged) {
1420                 set_bit(bridge_port->system_port, ports_bitmap);
1421         } else {
1422                 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1423                                                      MAX_LAG_MEMBERS);
1424                 lag_id = bridge_port->lag_id;
1425                 for (i = 0; i < max_lag_members; i++) {
1426                         mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp,
1427                                                                  lag_id, i);
1428                         if (mlxsw_sp_port)
1429                                 set_bit(mlxsw_sp_port->local_port,
1430                                         ports_bitmap);
1431                 }
1432         }
1433 }
1434
1435 static void
1436 mlxsw_sp_mc_get_mrouters_bitmap(unsigned long *flood_bitmap,
1437                                 struct mlxsw_sp_bridge_device *bridge_device,
1438                                 struct mlxsw_sp *mlxsw_sp)
1439 {
1440         struct mlxsw_sp_bridge_port *bridge_port;
1441
1442         list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
1443                 if (bridge_port->mrouter) {
1444                         mlxsw_sp_bridge_port_get_ports_bitmap(mlxsw_sp,
1445                                                               bridge_port,
1446                                                               flood_bitmap);
1447                 }
1448         }
1449 }
1450
1451 static bool
1452 mlxsw_sp_mc_write_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1453                             struct mlxsw_sp_mid *mid,
1454                             struct mlxsw_sp_bridge_device *bridge_device)
1455 {
1456         long *flood_bitmap;
1457         int num_of_ports;
1458         int alloc_size;
1459         u16 mid_idx;
1460         int err;
1461
1462         mid_idx = find_first_zero_bit(mlxsw_sp->bridge->mids_bitmap,
1463                                       MLXSW_SP_MID_MAX);
1464         if (mid_idx == MLXSW_SP_MID_MAX)
1465                 return false;
1466
1467         num_of_ports = mlxsw_core_max_ports(mlxsw_sp->core);
1468         alloc_size = sizeof(long) * BITS_TO_LONGS(num_of_ports);
1469         flood_bitmap = kzalloc(alloc_size, GFP_KERNEL);
1470         if (!flood_bitmap)
1471                 return false;
1472
1473         bitmap_copy(flood_bitmap,  mid->ports_in_mid, num_of_ports);
1474         mlxsw_sp_mc_get_mrouters_bitmap(flood_bitmap, bridge_device, mlxsw_sp);
1475
1476         mid->mid = mid_idx;
1477         err = mlxsw_sp_port_smid_full_entry(mlxsw_sp, mid_idx, flood_bitmap,
1478                                             bridge_device->mrouter);
1479         kfree(flood_bitmap);
1480         if (err)
1481                 return false;
1482
1483         err = mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid_idx,
1484                                    true);
1485         if (err)
1486                 return false;
1487
1488         set_bit(mid_idx, mlxsw_sp->bridge->mids_bitmap);
1489         mid->in_hw = true;
1490         return true;
1491 }
1492
1493 static int mlxsw_sp_mc_remove_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1494                                         struct mlxsw_sp_mid *mid)
1495 {
1496         if (!mid->in_hw)
1497                 return 0;
1498
1499         clear_bit(mid->mid, mlxsw_sp->bridge->mids_bitmap);
1500         mid->in_hw = false;
1501         return mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid->mid,
1502                                     false);
1503 }
1504
1505 static struct
1506 mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
1507                                   struct mlxsw_sp_bridge_device *bridge_device,
1508                                   const unsigned char *addr,
1509                                   u16 fid)
1510 {
1511         struct mlxsw_sp_mid *mid;
1512         size_t alloc_size;
1513
1514         mid = kzalloc(sizeof(*mid), GFP_KERNEL);
1515         if (!mid)
1516                 return NULL;
1517
1518         alloc_size = sizeof(unsigned long) *
1519                      BITS_TO_LONGS(mlxsw_core_max_ports(mlxsw_sp->core));
1520
1521         mid->ports_in_mid = kzalloc(alloc_size, GFP_KERNEL);
1522         if (!mid->ports_in_mid)
1523                 goto err_ports_in_mid_alloc;
1524
1525         ether_addr_copy(mid->addr, addr);
1526         mid->fid = fid;
1527         mid->in_hw = false;
1528
1529         if (!bridge_device->multicast_enabled)
1530                 goto out;
1531
1532         if (!mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid, bridge_device))
1533                 goto err_write_mdb_entry;
1534
1535 out:
1536         list_add_tail(&mid->list, &bridge_device->mids_list);
1537         return mid;
1538
1539 err_write_mdb_entry:
1540         kfree(mid->ports_in_mid);
1541 err_ports_in_mid_alloc:
1542         kfree(mid);
1543         return NULL;
1544 }
1545
1546 static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port *mlxsw_sp_port,
1547                                          struct mlxsw_sp_mid *mid)
1548 {
1549         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1550         int err = 0;
1551
1552         clear_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1553         if (bitmap_empty(mid->ports_in_mid,
1554                          mlxsw_core_max_ports(mlxsw_sp->core))) {
1555                 err = mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1556                 list_del(&mid->list);
1557                 kfree(mid->ports_in_mid);
1558                 kfree(mid);
1559         }
1560         return err;
1561 }
1562
1563 static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
1564                                  const struct switchdev_obj_port_mdb *mdb,
1565                                  struct switchdev_trans *trans)
1566 {
1567         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1568         struct net_device *orig_dev = mdb->obj.orig_dev;
1569         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1570         struct net_device *dev = mlxsw_sp_port->dev;
1571         struct mlxsw_sp_bridge_device *bridge_device;
1572         struct mlxsw_sp_bridge_port *bridge_port;
1573         struct mlxsw_sp_mid *mid;
1574         u16 fid_index;
1575         int err = 0;
1576
1577         if (switchdev_trans_ph_prepare(trans))
1578                 return 0;
1579
1580         bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1581         if (!bridge_port)
1582                 return 0;
1583
1584         bridge_device = bridge_port->bridge_device;
1585         mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1586                                                                bridge_device,
1587                                                                mdb->vid);
1588         if (!mlxsw_sp_port_vlan)
1589                 return 0;
1590
1591         fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1592
1593         mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1594         if (!mid) {
1595                 mid = __mlxsw_sp_mc_alloc(mlxsw_sp, bridge_device, mdb->addr,
1596                                           fid_index);
1597                 if (!mid) {
1598                         netdev_err(dev, "Unable to allocate MC group\n");
1599                         return -ENOMEM;
1600                 }
1601         }
1602         set_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1603
1604         if (!bridge_device->multicast_enabled)
1605                 return 0;
1606
1607         if (bridge_port->mrouter)
1608                 return 0;
1609
1610         err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true);
1611         if (err) {
1612                 netdev_err(dev, "Unable to set SMID\n");
1613                 goto err_out;
1614         }
1615
1616         return 0;
1617
1618 err_out:
1619         mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1620         return err;
1621 }
1622
1623 static void
1624 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
1625                                    struct mlxsw_sp_bridge_device
1626                                    *bridge_device)
1627 {
1628         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1629         struct mlxsw_sp_mid *mid;
1630         bool mc_enabled;
1631
1632         mc_enabled = bridge_device->multicast_enabled;
1633
1634         list_for_each_entry(mid, &bridge_device->mids_list, list) {
1635                 if (mc_enabled)
1636                         mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid,
1637                                                     bridge_device);
1638                 else
1639                         mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1640         }
1641 }
1642
1643 static void
1644 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
1645                                  struct mlxsw_sp_bridge_port *bridge_port,
1646                                  bool add)
1647 {
1648         struct mlxsw_sp_bridge_device *bridge_device;
1649         struct mlxsw_sp_mid *mid;
1650
1651         bridge_device = bridge_port->bridge_device;
1652
1653         list_for_each_entry(mid, &bridge_device->mids_list, list) {
1654                 if (!test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid))
1655                         mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, add);
1656         }
1657 }
1658
1659 struct mlxsw_sp_span_respin_work {
1660         struct work_struct work;
1661         struct mlxsw_sp *mlxsw_sp;
1662 };
1663
1664 static void mlxsw_sp_span_respin_work(struct work_struct *work)
1665 {
1666         struct mlxsw_sp_span_respin_work *respin_work =
1667                 container_of(work, struct mlxsw_sp_span_respin_work, work);
1668
1669         rtnl_lock();
1670         mlxsw_sp_span_respin(respin_work->mlxsw_sp);
1671         rtnl_unlock();
1672         kfree(respin_work);
1673 }
1674
1675 static void mlxsw_sp_span_respin_schedule(struct mlxsw_sp *mlxsw_sp)
1676 {
1677         struct mlxsw_sp_span_respin_work *respin_work;
1678
1679         respin_work = kzalloc(sizeof(*respin_work), GFP_ATOMIC);
1680         if (!respin_work)
1681                 return;
1682
1683         INIT_WORK(&respin_work->work, mlxsw_sp_span_respin_work);
1684         respin_work->mlxsw_sp = mlxsw_sp;
1685
1686         mlxsw_core_schedule_work(&respin_work->work);
1687 }
1688
1689 static int mlxsw_sp_port_obj_add(struct net_device *dev,
1690                                  const struct switchdev_obj *obj,
1691                                  struct switchdev_trans *trans)
1692 {
1693         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1694         const struct switchdev_obj_port_vlan *vlan;
1695         int err = 0;
1696
1697         switch (obj->id) {
1698         case SWITCHDEV_OBJ_ID_PORT_VLAN:
1699                 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
1700                 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port, vlan, trans);
1701
1702                 if (switchdev_trans_ph_prepare(trans)) {
1703                         /* The event is emitted before the changes are actually
1704                          * applied to the bridge. Therefore schedule the respin
1705                          * call for later, so that the respin logic sees the
1706                          * updated bridge state.
1707                          */
1708                         mlxsw_sp_span_respin_schedule(mlxsw_sp_port->mlxsw_sp);
1709                 }
1710                 break;
1711         case SWITCHDEV_OBJ_ID_PORT_MDB:
1712                 err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
1713                                             SWITCHDEV_OBJ_PORT_MDB(obj),
1714                                             trans);
1715                 break;
1716         default:
1717                 err = -EOPNOTSUPP;
1718                 break;
1719         }
1720
1721         return err;
1722 }
1723
1724 static void
1725 mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port *mlxsw_sp_port,
1726                               struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
1727 {
1728         u16 pvid = mlxsw_sp_port->pvid == vid ? 0 : vid;
1729         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1730
1731         mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1732         if (WARN_ON(!mlxsw_sp_port_vlan))
1733                 return;
1734
1735         mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1736         mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1737         mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1738         mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1739 }
1740
1741 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1742                                    const struct switchdev_obj_port_vlan *vlan)
1743 {
1744         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1745         struct net_device *orig_dev = vlan->obj.orig_dev;
1746         struct mlxsw_sp_bridge_port *bridge_port;
1747         u16 vid;
1748
1749         if (netif_is_bridge_master(orig_dev))
1750                 return -EOPNOTSUPP;
1751
1752         bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1753         if (WARN_ON(!bridge_port))
1754                 return -EINVAL;
1755
1756         if (!bridge_port->bridge_device->vlan_enabled)
1757                 return 0;
1758
1759         for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++)
1760                 mlxsw_sp_bridge_port_vlan_del(mlxsw_sp_port, bridge_port, vid);
1761
1762         return 0;
1763 }
1764
1765 static int
1766 __mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1767                         struct mlxsw_sp_bridge_port *bridge_port,
1768                         struct mlxsw_sp_mid *mid)
1769 {
1770         struct net_device *dev = mlxsw_sp_port->dev;
1771         int err;
1772
1773         if (bridge_port->bridge_device->multicast_enabled &&
1774             !bridge_port->mrouter) {
1775                 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1776                 if (err)
1777                         netdev_err(dev, "Unable to remove port from SMID\n");
1778         }
1779
1780         err = mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1781         if (err)
1782                 netdev_err(dev, "Unable to remove MC SFD\n");
1783
1784         return err;
1785 }
1786
1787 static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1788                                  const struct switchdev_obj_port_mdb *mdb)
1789 {
1790         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1791         struct net_device *orig_dev = mdb->obj.orig_dev;
1792         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1793         struct mlxsw_sp_bridge_device *bridge_device;
1794         struct net_device *dev = mlxsw_sp_port->dev;
1795         struct mlxsw_sp_bridge_port *bridge_port;
1796         struct mlxsw_sp_mid *mid;
1797         u16 fid_index;
1798
1799         bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1800         if (!bridge_port)
1801                 return 0;
1802
1803         bridge_device = bridge_port->bridge_device;
1804         mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1805                                                                bridge_device,
1806                                                                mdb->vid);
1807         if (!mlxsw_sp_port_vlan)
1808                 return 0;
1809
1810         fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1811
1812         mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1813         if (!mid) {
1814                 netdev_err(dev, "Unable to remove port from MC DB\n");
1815                 return -EINVAL;
1816         }
1817
1818         return __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port, mid);
1819 }
1820
1821 static void
1822 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
1823                                struct mlxsw_sp_bridge_port *bridge_port)
1824 {
1825         struct mlxsw_sp_bridge_device *bridge_device;
1826         struct mlxsw_sp_mid *mid, *tmp;
1827
1828         bridge_device = bridge_port->bridge_device;
1829
1830         list_for_each_entry_safe(mid, tmp, &bridge_device->mids_list, list) {
1831                 if (test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid)) {
1832                         __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port,
1833                                                 mid);
1834                 } else if (bridge_device->multicast_enabled &&
1835                            bridge_port->mrouter) {
1836                         mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1837                 }
1838         }
1839 }
1840
1841 static int mlxsw_sp_port_obj_del(struct net_device *dev,
1842                                  const struct switchdev_obj *obj)
1843 {
1844         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1845         int err = 0;
1846
1847         switch (obj->id) {
1848         case SWITCHDEV_OBJ_ID_PORT_VLAN:
1849                 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1850                                               SWITCHDEV_OBJ_PORT_VLAN(obj));
1851                 break;
1852         case SWITCHDEV_OBJ_ID_PORT_MDB:
1853                 err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1854                                             SWITCHDEV_OBJ_PORT_MDB(obj));
1855                 break;
1856         default:
1857                 err = -EOPNOTSUPP;
1858                 break;
1859         }
1860
1861         mlxsw_sp_span_respin_schedule(mlxsw_sp_port->mlxsw_sp);
1862
1863         return err;
1864 }
1865
1866 static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
1867                                                    u16 lag_id)
1868 {
1869         struct mlxsw_sp_port *mlxsw_sp_port;
1870         u64 max_lag_members;
1871         int i;
1872
1873         max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1874                                              MAX_LAG_MEMBERS);
1875         for (i = 0; i < max_lag_members; i++) {
1876                 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
1877                 if (mlxsw_sp_port)
1878                         return mlxsw_sp_port;
1879         }
1880         return NULL;
1881 }
1882
1883 static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = {
1884         .switchdev_port_attr_get        = mlxsw_sp_port_attr_get,
1885         .switchdev_port_attr_set        = mlxsw_sp_port_attr_set,
1886         .switchdev_port_obj_add         = mlxsw_sp_port_obj_add,
1887         .switchdev_port_obj_del         = mlxsw_sp_port_obj_del,
1888 };
1889
1890 static int
1891 mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1892                                 struct mlxsw_sp_bridge_port *bridge_port,
1893                                 struct mlxsw_sp_port *mlxsw_sp_port,
1894                                 struct netlink_ext_ack *extack)
1895 {
1896         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1897
1898         if (is_vlan_dev(bridge_port->dev)) {
1899                 NL_SET_ERR_MSG_MOD(extack, "Can not enslave a VLAN device to a VLAN-aware bridge");
1900                 return -EINVAL;
1901         }
1902
1903         mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, 1);
1904         if (WARN_ON(!mlxsw_sp_port_vlan))
1905                 return -EINVAL;
1906
1907         /* Let VLAN-aware bridge take care of its own VLANs */
1908         mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1909
1910         return 0;
1911 }
1912
1913 static void
1914 mlxsw_sp_bridge_8021q_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
1915                                  struct mlxsw_sp_bridge_port *bridge_port,
1916                                  struct mlxsw_sp_port *mlxsw_sp_port)
1917 {
1918         mlxsw_sp_port_vlan_get(mlxsw_sp_port, 1);
1919         /* Make sure untagged frames are allowed to ingress */
1920         mlxsw_sp_port_pvid_set(mlxsw_sp_port, 1);
1921 }
1922
1923 static struct mlxsw_sp_fid *
1924 mlxsw_sp_bridge_8021q_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
1925                               u16 vid)
1926 {
1927         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
1928
1929         return mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
1930 }
1931
1932 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021q_ops = {
1933         .port_join      = mlxsw_sp_bridge_8021q_port_join,
1934         .port_leave     = mlxsw_sp_bridge_8021q_port_leave,
1935         .fid_get        = mlxsw_sp_bridge_8021q_fid_get,
1936 };
1937
1938 static bool
1939 mlxsw_sp_port_is_br_member(const struct mlxsw_sp_port *mlxsw_sp_port,
1940                            const struct net_device *br_dev)
1941 {
1942         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1943
1944         list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
1945                             list) {
1946                 if (mlxsw_sp_port_vlan->bridge_port &&
1947                     mlxsw_sp_port_vlan->bridge_port->bridge_device->dev ==
1948                     br_dev)
1949                         return true;
1950         }
1951
1952         return false;
1953 }
1954
1955 static int
1956 mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1957                                 struct mlxsw_sp_bridge_port *bridge_port,
1958                                 struct mlxsw_sp_port *mlxsw_sp_port,
1959                                 struct netlink_ext_ack *extack)
1960 {
1961         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1962         struct net_device *dev = bridge_port->dev;
1963         u16 vid;
1964
1965         vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 1;
1966         mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1967         if (WARN_ON(!mlxsw_sp_port_vlan))
1968                 return -EINVAL;
1969
1970         if (mlxsw_sp_port_is_br_member(mlxsw_sp_port, bridge_device->dev)) {
1971                 NL_SET_ERR_MSG_MOD(extack, "Can not bridge VLAN uppers of the same port");
1972                 return -EINVAL;
1973         }
1974
1975         /* Port is no longer usable as a router interface */
1976         if (mlxsw_sp_port_vlan->fid)
1977                 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
1978
1979         return mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
1980 }
1981
1982 static void
1983 mlxsw_sp_bridge_8021d_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
1984                                  struct mlxsw_sp_bridge_port *bridge_port,
1985                                  struct mlxsw_sp_port *mlxsw_sp_port)
1986 {
1987         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1988         struct net_device *dev = bridge_port->dev;
1989         u16 vid;
1990
1991         vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 1;
1992         mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1993         if (WARN_ON(!mlxsw_sp_port_vlan))
1994                 return;
1995
1996         mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1997 }
1998
1999 static struct mlxsw_sp_fid *
2000 mlxsw_sp_bridge_8021d_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
2001                               u16 vid)
2002 {
2003         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2004
2005         return mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
2006 }
2007
2008 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021d_ops = {
2009         .port_join      = mlxsw_sp_bridge_8021d_port_join,
2010         .port_leave     = mlxsw_sp_bridge_8021d_port_leave,
2011         .fid_get        = mlxsw_sp_bridge_8021d_fid_get,
2012 };
2013
2014 int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port,
2015                               struct net_device *brport_dev,
2016                               struct net_device *br_dev,
2017                               struct netlink_ext_ack *extack)
2018 {
2019         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2020         struct mlxsw_sp_bridge_device *bridge_device;
2021         struct mlxsw_sp_bridge_port *bridge_port;
2022         int err;
2023
2024         bridge_port = mlxsw_sp_bridge_port_get(mlxsw_sp->bridge, brport_dev);
2025         if (IS_ERR(bridge_port))
2026                 return PTR_ERR(bridge_port);
2027         bridge_device = bridge_port->bridge_device;
2028
2029         err = bridge_device->ops->port_join(bridge_device, bridge_port,
2030                                             mlxsw_sp_port, extack);
2031         if (err)
2032                 goto err_port_join;
2033
2034         return 0;
2035
2036 err_port_join:
2037         mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2038         return err;
2039 }
2040
2041 void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port,
2042                                 struct net_device *brport_dev,
2043                                 struct net_device *br_dev)
2044 {
2045         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2046         struct mlxsw_sp_bridge_device *bridge_device;
2047         struct mlxsw_sp_bridge_port *bridge_port;
2048
2049         bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2050         if (!bridge_device)
2051                 return;
2052         bridge_port = __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
2053         if (!bridge_port)
2054                 return;
2055
2056         bridge_device->ops->port_leave(bridge_device, bridge_port,
2057                                        mlxsw_sp_port);
2058         mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2059 }
2060
2061 static void
2062 mlxsw_sp_fdb_call_notifiers(enum switchdev_notifier_type type,
2063                             const char *mac, u16 vid,
2064                             struct net_device *dev)
2065 {
2066         struct switchdev_notifier_fdb_info info;
2067
2068         info.addr = mac;
2069         info.vid = vid;
2070         call_switchdev_notifiers(type, dev, &info.info);
2071 }
2072
2073 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
2074                                             char *sfn_pl, int rec_index,
2075                                             bool adding)
2076 {
2077         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2078         struct mlxsw_sp_bridge_device *bridge_device;
2079         struct mlxsw_sp_bridge_port *bridge_port;
2080         struct mlxsw_sp_port *mlxsw_sp_port;
2081         enum switchdev_notifier_type type;
2082         char mac[ETH_ALEN];
2083         u8 local_port;
2084         u16 vid, fid;
2085         bool do_notification = true;
2086         int err;
2087
2088         mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
2089         mlxsw_sp_port = mlxsw_sp->ports[local_port];
2090         if (!mlxsw_sp_port) {
2091                 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
2092                 goto just_remove;
2093         }
2094
2095         mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2096         if (!mlxsw_sp_port_vlan) {
2097                 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2098                 goto just_remove;
2099         }
2100
2101         bridge_port = mlxsw_sp_port_vlan->bridge_port;
2102         if (!bridge_port) {
2103                 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2104                 goto just_remove;
2105         }
2106
2107         bridge_device = bridge_port->bridge_device;
2108         vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2109
2110 do_fdb_op:
2111         err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
2112                                       adding, true);
2113         if (err) {
2114                 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2115                 return;
2116         }
2117
2118         if (!do_notification)
2119                 return;
2120         type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2121         mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev);
2122
2123         return;
2124
2125 just_remove:
2126         adding = false;
2127         do_notification = false;
2128         goto do_fdb_op;
2129 }
2130
2131 static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
2132                                                 char *sfn_pl, int rec_index,
2133                                                 bool adding)
2134 {
2135         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2136         struct mlxsw_sp_bridge_device *bridge_device;
2137         struct mlxsw_sp_bridge_port *bridge_port;
2138         struct mlxsw_sp_port *mlxsw_sp_port;
2139         enum switchdev_notifier_type type;
2140         char mac[ETH_ALEN];
2141         u16 lag_vid = 0;
2142         u16 lag_id;
2143         u16 vid, fid;
2144         bool do_notification = true;
2145         int err;
2146
2147         mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
2148         mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
2149         if (!mlxsw_sp_port) {
2150                 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
2151                 goto just_remove;
2152         }
2153
2154         mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2155         if (!mlxsw_sp_port_vlan) {
2156                 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2157                 goto just_remove;
2158         }
2159
2160         bridge_port = mlxsw_sp_port_vlan->bridge_port;
2161         if (!bridge_port) {
2162                 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2163                 goto just_remove;
2164         }
2165
2166         bridge_device = bridge_port->bridge_device;
2167         vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2168         lag_vid = mlxsw_sp_port_vlan->vid;
2169
2170 do_fdb_op:
2171         err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
2172                                           adding, true);
2173         if (err) {
2174                 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2175                 return;
2176         }
2177
2178         if (!do_notification)
2179                 return;
2180         type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2181         mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev);
2182
2183         return;
2184
2185 just_remove:
2186         adding = false;
2187         do_notification = false;
2188         goto do_fdb_op;
2189 }
2190
2191 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
2192                                             char *sfn_pl, int rec_index)
2193 {
2194         switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
2195         case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
2196                 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2197                                                 rec_index, true);
2198                 break;
2199         case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
2200                 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2201                                                 rec_index, false);
2202                 break;
2203         case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
2204                 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2205                                                     rec_index, true);
2206                 break;
2207         case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
2208                 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2209                                                     rec_index, false);
2210                 break;
2211         }
2212 }
2213
2214 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp)
2215 {
2216         struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2217
2218         mlxsw_core_schedule_dw(&bridge->fdb_notify.dw,
2219                                msecs_to_jiffies(bridge->fdb_notify.interval));
2220 }
2221
2222 static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
2223 {
2224         struct mlxsw_sp_bridge *bridge;
2225         struct mlxsw_sp *mlxsw_sp;
2226         char *sfn_pl;
2227         u8 num_rec;
2228         int i;
2229         int err;
2230
2231         sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
2232         if (!sfn_pl)
2233                 return;
2234
2235         bridge = container_of(work, struct mlxsw_sp_bridge, fdb_notify.dw.work);
2236         mlxsw_sp = bridge->mlxsw_sp;
2237
2238         rtnl_lock();
2239         mlxsw_reg_sfn_pack(sfn_pl);
2240         err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
2241         if (err) {
2242                 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
2243                 goto out;
2244         }
2245         num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
2246         for (i = 0; i < num_rec; i++)
2247                 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
2248
2249 out:
2250         rtnl_unlock();
2251         kfree(sfn_pl);
2252         mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2253 }
2254
2255 struct mlxsw_sp_switchdev_event_work {
2256         struct work_struct work;
2257         struct switchdev_notifier_fdb_info fdb_info;
2258         struct net_device *dev;
2259         unsigned long event;
2260 };
2261
2262 static void mlxsw_sp_switchdev_event_work(struct work_struct *work)
2263 {
2264         struct mlxsw_sp_switchdev_event_work *switchdev_work =
2265                 container_of(work, struct mlxsw_sp_switchdev_event_work, work);
2266         struct net_device *dev = switchdev_work->dev;
2267         struct switchdev_notifier_fdb_info *fdb_info;
2268         struct mlxsw_sp_port *mlxsw_sp_port;
2269         int err;
2270
2271         rtnl_lock();
2272         mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
2273         if (!mlxsw_sp_port)
2274                 goto out;
2275
2276         switch (switchdev_work->event) {
2277         case SWITCHDEV_FDB_ADD_TO_DEVICE:
2278                 fdb_info = &switchdev_work->fdb_info;
2279                 if (!fdb_info->added_by_user)
2280                         break;
2281                 err = mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, true);
2282                 if (err)
2283                         break;
2284                 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2285                                             fdb_info->addr,
2286                                             fdb_info->vid, dev);
2287                 break;
2288         case SWITCHDEV_FDB_DEL_TO_DEVICE:
2289                 fdb_info = &switchdev_work->fdb_info;
2290                 if (!fdb_info->added_by_user)
2291                         break;
2292                 mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, false);
2293                 break;
2294         case SWITCHDEV_FDB_ADD_TO_BRIDGE: /* fall through */
2295         case SWITCHDEV_FDB_DEL_TO_BRIDGE:
2296                 /* These events are only used to potentially update an existing
2297                  * SPAN mirror.
2298                  */
2299                 break;
2300         }
2301
2302         mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
2303
2304 out:
2305         rtnl_unlock();
2306         kfree(switchdev_work->fdb_info.addr);
2307         kfree(switchdev_work);
2308         dev_put(dev);
2309 }
2310
2311 /* Called under rcu_read_lock() */
2312 static int mlxsw_sp_switchdev_event(struct notifier_block *unused,
2313                                     unsigned long event, void *ptr)
2314 {
2315         struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2316         struct mlxsw_sp_switchdev_event_work *switchdev_work;
2317         struct switchdev_notifier_fdb_info *fdb_info = ptr;
2318
2319         if (!mlxsw_sp_port_dev_lower_find_rcu(dev))
2320                 return NOTIFY_DONE;
2321
2322         switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2323         if (!switchdev_work)
2324                 return NOTIFY_BAD;
2325
2326         INIT_WORK(&switchdev_work->work, mlxsw_sp_switchdev_event_work);
2327         switchdev_work->dev = dev;
2328         switchdev_work->event = event;
2329
2330         switch (event) {
2331         case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
2332         case SWITCHDEV_FDB_DEL_TO_DEVICE: /* fall through */
2333         case SWITCHDEV_FDB_ADD_TO_BRIDGE: /* fall through */
2334         case SWITCHDEV_FDB_DEL_TO_BRIDGE:
2335                 memcpy(&switchdev_work->fdb_info, ptr,
2336                        sizeof(switchdev_work->fdb_info));
2337                 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
2338                 if (!switchdev_work->fdb_info.addr)
2339                         goto err_addr_alloc;
2340                 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
2341                                 fdb_info->addr);
2342                 /* Take a reference on the device. This can be either
2343                  * upper device containig mlxsw_sp_port or just a
2344                  * mlxsw_sp_port
2345                  */
2346                 dev_hold(dev);
2347                 break;
2348         default:
2349                 kfree(switchdev_work);
2350                 return NOTIFY_DONE;
2351         }
2352
2353         mlxsw_core_schedule_work(&switchdev_work->work);
2354
2355         return NOTIFY_DONE;
2356
2357 err_addr_alloc:
2358         kfree(switchdev_work);
2359         return NOTIFY_BAD;
2360 }
2361
2362 static struct notifier_block mlxsw_sp_switchdev_notifier = {
2363         .notifier_call = mlxsw_sp_switchdev_event,
2364 };
2365
2366 u8
2367 mlxsw_sp_bridge_port_stp_state(struct mlxsw_sp_bridge_port *bridge_port)
2368 {
2369         return bridge_port->stp_state;
2370 }
2371
2372 static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
2373 {
2374         struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2375         int err;
2376
2377         err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
2378         if (err) {
2379                 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
2380                 return err;
2381         }
2382
2383         err = register_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
2384         if (err) {
2385                 dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev notifier\n");
2386                 return err;
2387         }
2388
2389         INIT_DELAYED_WORK(&bridge->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
2390         bridge->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
2391         mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2392         return 0;
2393 }
2394
2395 static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
2396 {
2397         cancel_delayed_work_sync(&mlxsw_sp->bridge->fdb_notify.dw);
2398         unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
2399
2400 }
2401
2402 int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
2403 {
2404         struct mlxsw_sp_bridge *bridge;
2405
2406         bridge = kzalloc(sizeof(*mlxsw_sp->bridge), GFP_KERNEL);
2407         if (!bridge)
2408                 return -ENOMEM;
2409         mlxsw_sp->bridge = bridge;
2410         bridge->mlxsw_sp = mlxsw_sp;
2411
2412         INIT_LIST_HEAD(&mlxsw_sp->bridge->bridges_list);
2413
2414         bridge->bridge_8021q_ops = &mlxsw_sp_bridge_8021q_ops;
2415         bridge->bridge_8021d_ops = &mlxsw_sp_bridge_8021d_ops;
2416
2417         return mlxsw_sp_fdb_init(mlxsw_sp);
2418 }
2419
2420 void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
2421 {
2422         mlxsw_sp_fdb_fini(mlxsw_sp);
2423         WARN_ON(!list_empty(&mlxsw_sp->bridge->bridges_list));
2424         kfree(mlxsw_sp->bridge);
2425 }
2426
2427 void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port *mlxsw_sp_port)
2428 {
2429         mlxsw_sp_port->dev->switchdev_ops = &mlxsw_sp_port_switchdev_ops;
2430 }
2431
2432 void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port)
2433 {
2434 }