e97652c40d13a40fad9bb71e2c554b9d3933bef5
[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                 return 0;
1023
1024         err = mlxsw_sp_port_vlan_fid_join(mlxsw_sp_port_vlan, bridge_port);
1025         if (err)
1026                 return err;
1027
1028         err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid,
1029                                              bridge_port->flags & BR_LEARNING);
1030         if (err)
1031                 goto err_port_vid_learning_set;
1032
1033         err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid,
1034                                         bridge_port->stp_state);
1035         if (err)
1036                 goto err_port_vid_stp_set;
1037
1038         bridge_vlan = mlxsw_sp_bridge_vlan_get(bridge_port, vid);
1039         if (!bridge_vlan) {
1040                 err = -ENOMEM;
1041                 goto err_bridge_vlan_get;
1042         }
1043
1044         list_add(&mlxsw_sp_port_vlan->bridge_vlan_node,
1045                  &bridge_vlan->port_vlan_list);
1046
1047         mlxsw_sp_bridge_port_get(mlxsw_sp_port->mlxsw_sp->bridge,
1048                                  bridge_port->dev);
1049         mlxsw_sp_port_vlan->bridge_port = bridge_port;
1050
1051         return 0;
1052
1053 err_bridge_vlan_get:
1054         mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1055 err_port_vid_stp_set:
1056         mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1057 err_port_vid_learning_set:
1058         mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1059         return err;
1060 }
1061
1062 void
1063 mlxsw_sp_port_vlan_bridge_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1064 {
1065         struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1066         struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
1067         struct mlxsw_sp_bridge_vlan *bridge_vlan;
1068         struct mlxsw_sp_bridge_port *bridge_port;
1069         u16 vid = mlxsw_sp_port_vlan->vid;
1070         bool last_port, last_vlan;
1071
1072         if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021Q &&
1073                     mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021D))
1074                 return;
1075
1076         bridge_port = mlxsw_sp_port_vlan->bridge_port;
1077         last_vlan = list_is_singular(&bridge_port->vlans_list);
1078         bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
1079         last_port = list_is_singular(&bridge_vlan->port_vlan_list);
1080
1081         list_del(&mlxsw_sp_port_vlan->bridge_vlan_node);
1082         mlxsw_sp_bridge_vlan_put(bridge_vlan);
1083         mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1084         mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1085         if (last_port)
1086                 mlxsw_sp_bridge_port_fdb_flush(mlxsw_sp_port->mlxsw_sp,
1087                                                bridge_port,
1088                                                mlxsw_sp_fid_index(fid));
1089         if (last_vlan)
1090                 mlxsw_sp_bridge_port_mdb_flush(mlxsw_sp_port, bridge_port);
1091
1092         mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1093
1094         mlxsw_sp_bridge_port_put(mlxsw_sp_port->mlxsw_sp->bridge, bridge_port);
1095         mlxsw_sp_port_vlan->bridge_port = NULL;
1096 }
1097
1098 static int
1099 mlxsw_sp_bridge_port_vlan_add(struct mlxsw_sp_port *mlxsw_sp_port,
1100                               struct mlxsw_sp_bridge_port *bridge_port,
1101                               u16 vid, bool is_untagged, bool is_pvid)
1102 {
1103         u16 pvid = mlxsw_sp_port_pvid_determine(mlxsw_sp_port, vid, is_pvid);
1104         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1105         u16 old_pvid = mlxsw_sp_port->pvid;
1106         int err;
1107
1108         mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_get(mlxsw_sp_port, vid);
1109         if (IS_ERR(mlxsw_sp_port_vlan))
1110                 return PTR_ERR(mlxsw_sp_port_vlan);
1111
1112         err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true,
1113                                      is_untagged);
1114         if (err)
1115                 goto err_port_vlan_set;
1116
1117         err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1118         if (err)
1119                 goto err_port_pvid_set;
1120
1121         err = mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
1122         if (err)
1123                 goto err_port_vlan_bridge_join;
1124
1125         return 0;
1126
1127 err_port_vlan_bridge_join:
1128         mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid);
1129 err_port_pvid_set:
1130         mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1131 err_port_vlan_set:
1132         mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1133         return err;
1134 }
1135
1136 static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
1137                                    const struct switchdev_obj_port_vlan *vlan,
1138                                    struct switchdev_trans *trans)
1139 {
1140         bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1141         bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1142         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1143         struct net_device *orig_dev = vlan->obj.orig_dev;
1144         struct mlxsw_sp_bridge_port *bridge_port;
1145         u16 vid;
1146
1147         if (netif_is_bridge_master(orig_dev))
1148                 return -EOPNOTSUPP;
1149
1150         if (switchdev_trans_ph_prepare(trans))
1151                 return 0;
1152
1153         bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1154         if (WARN_ON(!bridge_port))
1155                 return -EINVAL;
1156
1157         if (!bridge_port->bridge_device->vlan_enabled)
1158                 return 0;
1159
1160         for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1161                 int err;
1162
1163                 err = mlxsw_sp_bridge_port_vlan_add(mlxsw_sp_port, bridge_port,
1164                                                     vid, flag_untagged,
1165                                                     flag_pvid);
1166                 if (err)
1167                         return err;
1168         }
1169
1170         return 0;
1171 }
1172
1173 static enum mlxsw_reg_sfdf_flush_type mlxsw_sp_fdb_flush_type(bool lagged)
1174 {
1175         return lagged ? MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID :
1176                         MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID;
1177 }
1178
1179 static int
1180 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
1181                                struct mlxsw_sp_bridge_port *bridge_port,
1182                                u16 fid_index)
1183 {
1184         bool lagged = bridge_port->lagged;
1185         char sfdf_pl[MLXSW_REG_SFDF_LEN];
1186         u16 system_port;
1187
1188         system_port = lagged ? bridge_port->lag_id : bridge_port->system_port;
1189         mlxsw_reg_sfdf_pack(sfdf_pl, mlxsw_sp_fdb_flush_type(lagged));
1190         mlxsw_reg_sfdf_fid_set(sfdf_pl, fid_index);
1191         mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl, system_port);
1192
1193         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl);
1194 }
1195
1196 static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
1197 {
1198         return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
1199                          MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY;
1200 }
1201
1202 static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
1203 {
1204         return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
1205                         MLXSW_REG_SFD_OP_WRITE_REMOVE;
1206 }
1207
1208 static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1209                                      const char *mac, u16 fid, bool adding,
1210                                      enum mlxsw_reg_sfd_rec_action action,
1211                                      bool dynamic)
1212 {
1213         char *sfd_pl;
1214         u8 num_rec;
1215         int err;
1216
1217         sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1218         if (!sfd_pl)
1219                 return -ENOMEM;
1220
1221         mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1222         mlxsw_reg_sfd_uc_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1223                               mac, fid, action, local_port);
1224         num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1225         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1226         if (err)
1227                 goto out;
1228
1229         if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1230                 err = -EBUSY;
1231
1232 out:
1233         kfree(sfd_pl);
1234         return err;
1235 }
1236
1237 static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1238                                    const char *mac, u16 fid, bool adding,
1239                                    bool dynamic)
1240 {
1241         return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding,
1242                                          MLXSW_REG_SFD_REC_ACTION_NOP, dynamic);
1243 }
1244
1245 int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid,
1246                         bool adding)
1247 {
1248         return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding,
1249                                          MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER,
1250                                          false);
1251 }
1252
1253 static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
1254                                        const char *mac, u16 fid, u16 lag_vid,
1255                                        bool adding, bool dynamic)
1256 {
1257         char *sfd_pl;
1258         u8 num_rec;
1259         int err;
1260
1261         sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1262         if (!sfd_pl)
1263                 return -ENOMEM;
1264
1265         mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1266         mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1267                                   mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
1268                                   lag_vid, lag_id);
1269         num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1270         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1271         if (err)
1272                 goto out;
1273
1274         if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1275                 err = -EBUSY;
1276
1277 out:
1278         kfree(sfd_pl);
1279         return err;
1280 }
1281
1282 static int
1283 mlxsw_sp_port_fdb_set(struct mlxsw_sp_port *mlxsw_sp_port,
1284                       struct switchdev_notifier_fdb_info *fdb_info, bool adding)
1285 {
1286         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1287         struct net_device *orig_dev = fdb_info->info.dev;
1288         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1289         struct mlxsw_sp_bridge_device *bridge_device;
1290         struct mlxsw_sp_bridge_port *bridge_port;
1291         u16 fid_index, vid;
1292
1293         bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1294         if (!bridge_port)
1295                 return -EINVAL;
1296
1297         bridge_device = bridge_port->bridge_device;
1298         mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1299                                                                bridge_device,
1300                                                                fdb_info->vid);
1301         if (!mlxsw_sp_port_vlan)
1302                 return 0;
1303
1304         fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1305         vid = mlxsw_sp_port_vlan->vid;
1306
1307         if (!bridge_port->lagged)
1308                 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp,
1309                                                bridge_port->system_port,
1310                                                fdb_info->addr, fid_index,
1311                                                adding, false);
1312         else
1313                 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp,
1314                                                    bridge_port->lag_id,
1315                                                    fdb_info->addr, fid_index,
1316                                                    vid, adding, false);
1317 }
1318
1319 static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
1320                                 u16 fid, u16 mid_idx, bool adding)
1321 {
1322         char *sfd_pl;
1323         u8 num_rec;
1324         int err;
1325
1326         sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1327         if (!sfd_pl)
1328                 return -ENOMEM;
1329
1330         mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1331         mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
1332                               MLXSW_REG_SFD_REC_ACTION_NOP, mid_idx);
1333         num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1334         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1335         if (err)
1336                 goto out;
1337
1338         if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1339                 err = -EBUSY;
1340
1341 out:
1342         kfree(sfd_pl);
1343         return err;
1344 }
1345
1346 static int mlxsw_sp_port_smid_full_entry(struct mlxsw_sp *mlxsw_sp, u16 mid_idx,
1347                                          long *ports_bitmap,
1348                                          bool set_router_port)
1349 {
1350         char *smid_pl;
1351         int err, i;
1352
1353         smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1354         if (!smid_pl)
1355                 return -ENOMEM;
1356
1357         mlxsw_reg_smid_pack(smid_pl, mid_idx, 0, false);
1358         for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++) {
1359                 if (mlxsw_sp->ports[i])
1360                         mlxsw_reg_smid_port_mask_set(smid_pl, i, 1);
1361         }
1362
1363         mlxsw_reg_smid_port_mask_set(smid_pl,
1364                                      mlxsw_sp_router_port(mlxsw_sp), 1);
1365
1366         for_each_set_bit(i, ports_bitmap, mlxsw_core_max_ports(mlxsw_sp->core))
1367                 mlxsw_reg_smid_port_set(smid_pl, i, 1);
1368
1369         mlxsw_reg_smid_port_set(smid_pl, mlxsw_sp_router_port(mlxsw_sp),
1370                                 set_router_port);
1371
1372         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1373         kfree(smid_pl);
1374         return err;
1375 }
1376
1377 static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port,
1378                                   u16 mid_idx, bool add)
1379 {
1380         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1381         char *smid_pl;
1382         int err;
1383
1384         smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1385         if (!smid_pl)
1386                 return -ENOMEM;
1387
1388         mlxsw_reg_smid_pack(smid_pl, mid_idx, mlxsw_sp_port->local_port, add);
1389         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1390         kfree(smid_pl);
1391         return err;
1392 }
1393
1394 static struct
1395 mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp_bridge_device *bridge_device,
1396                                 const unsigned char *addr,
1397                                 u16 fid)
1398 {
1399         struct mlxsw_sp_mid *mid;
1400
1401         list_for_each_entry(mid, &bridge_device->mids_list, list) {
1402                 if (ether_addr_equal(mid->addr, addr) && mid->fid == fid)
1403                         return mid;
1404         }
1405         return NULL;
1406 }
1407
1408 static void
1409 mlxsw_sp_bridge_port_get_ports_bitmap(struct mlxsw_sp *mlxsw_sp,
1410                                       struct mlxsw_sp_bridge_port *bridge_port,
1411                                       unsigned long *ports_bitmap)
1412 {
1413         struct mlxsw_sp_port *mlxsw_sp_port;
1414         u64 max_lag_members, i;
1415         int lag_id;
1416
1417         if (!bridge_port->lagged) {
1418                 set_bit(bridge_port->system_port, ports_bitmap);
1419         } else {
1420                 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1421                                                      MAX_LAG_MEMBERS);
1422                 lag_id = bridge_port->lag_id;
1423                 for (i = 0; i < max_lag_members; i++) {
1424                         mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp,
1425                                                                  lag_id, i);
1426                         if (mlxsw_sp_port)
1427                                 set_bit(mlxsw_sp_port->local_port,
1428                                         ports_bitmap);
1429                 }
1430         }
1431 }
1432
1433 static void
1434 mlxsw_sp_mc_get_mrouters_bitmap(unsigned long *flood_bitmap,
1435                                 struct mlxsw_sp_bridge_device *bridge_device,
1436                                 struct mlxsw_sp *mlxsw_sp)
1437 {
1438         struct mlxsw_sp_bridge_port *bridge_port;
1439
1440         list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
1441                 if (bridge_port->mrouter) {
1442                         mlxsw_sp_bridge_port_get_ports_bitmap(mlxsw_sp,
1443                                                               bridge_port,
1444                                                               flood_bitmap);
1445                 }
1446         }
1447 }
1448
1449 static bool
1450 mlxsw_sp_mc_write_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1451                             struct mlxsw_sp_mid *mid,
1452                             struct mlxsw_sp_bridge_device *bridge_device)
1453 {
1454         long *flood_bitmap;
1455         int num_of_ports;
1456         int alloc_size;
1457         u16 mid_idx;
1458         int err;
1459
1460         mid_idx = find_first_zero_bit(mlxsw_sp->bridge->mids_bitmap,
1461                                       MLXSW_SP_MID_MAX);
1462         if (mid_idx == MLXSW_SP_MID_MAX)
1463                 return false;
1464
1465         num_of_ports = mlxsw_core_max_ports(mlxsw_sp->core);
1466         alloc_size = sizeof(long) * BITS_TO_LONGS(num_of_ports);
1467         flood_bitmap = kzalloc(alloc_size, GFP_KERNEL);
1468         if (!flood_bitmap)
1469                 return false;
1470
1471         bitmap_copy(flood_bitmap,  mid->ports_in_mid, num_of_ports);
1472         mlxsw_sp_mc_get_mrouters_bitmap(flood_bitmap, bridge_device, mlxsw_sp);
1473
1474         mid->mid = mid_idx;
1475         err = mlxsw_sp_port_smid_full_entry(mlxsw_sp, mid_idx, flood_bitmap,
1476                                             bridge_device->mrouter);
1477         kfree(flood_bitmap);
1478         if (err)
1479                 return false;
1480
1481         err = mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid_idx,
1482                                    true);
1483         if (err)
1484                 return false;
1485
1486         set_bit(mid_idx, mlxsw_sp->bridge->mids_bitmap);
1487         mid->in_hw = true;
1488         return true;
1489 }
1490
1491 static int mlxsw_sp_mc_remove_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1492                                         struct mlxsw_sp_mid *mid)
1493 {
1494         if (!mid->in_hw)
1495                 return 0;
1496
1497         clear_bit(mid->mid, mlxsw_sp->bridge->mids_bitmap);
1498         mid->in_hw = false;
1499         return mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid->mid,
1500                                     false);
1501 }
1502
1503 static struct
1504 mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
1505                                   struct mlxsw_sp_bridge_device *bridge_device,
1506                                   const unsigned char *addr,
1507                                   u16 fid)
1508 {
1509         struct mlxsw_sp_mid *mid;
1510         size_t alloc_size;
1511
1512         mid = kzalloc(sizeof(*mid), GFP_KERNEL);
1513         if (!mid)
1514                 return NULL;
1515
1516         alloc_size = sizeof(unsigned long) *
1517                      BITS_TO_LONGS(mlxsw_core_max_ports(mlxsw_sp->core));
1518
1519         mid->ports_in_mid = kzalloc(alloc_size, GFP_KERNEL);
1520         if (!mid->ports_in_mid)
1521                 goto err_ports_in_mid_alloc;
1522
1523         ether_addr_copy(mid->addr, addr);
1524         mid->fid = fid;
1525         mid->in_hw = false;
1526
1527         if (!bridge_device->multicast_enabled)
1528                 goto out;
1529
1530         if (!mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid, bridge_device))
1531                 goto err_write_mdb_entry;
1532
1533 out:
1534         list_add_tail(&mid->list, &bridge_device->mids_list);
1535         return mid;
1536
1537 err_write_mdb_entry:
1538         kfree(mid->ports_in_mid);
1539 err_ports_in_mid_alloc:
1540         kfree(mid);
1541         return NULL;
1542 }
1543
1544 static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port *mlxsw_sp_port,
1545                                          struct mlxsw_sp_mid *mid)
1546 {
1547         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1548         int err = 0;
1549
1550         clear_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1551         if (bitmap_empty(mid->ports_in_mid,
1552                          mlxsw_core_max_ports(mlxsw_sp->core))) {
1553                 err = mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1554                 list_del(&mid->list);
1555                 kfree(mid->ports_in_mid);
1556                 kfree(mid);
1557         }
1558         return err;
1559 }
1560
1561 static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
1562                                  const struct switchdev_obj_port_mdb *mdb,
1563                                  struct switchdev_trans *trans)
1564 {
1565         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1566         struct net_device *orig_dev = mdb->obj.orig_dev;
1567         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1568         struct net_device *dev = mlxsw_sp_port->dev;
1569         struct mlxsw_sp_bridge_device *bridge_device;
1570         struct mlxsw_sp_bridge_port *bridge_port;
1571         struct mlxsw_sp_mid *mid;
1572         u16 fid_index;
1573         int err = 0;
1574
1575         if (switchdev_trans_ph_prepare(trans))
1576                 return 0;
1577
1578         bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1579         if (!bridge_port)
1580                 return 0;
1581
1582         bridge_device = bridge_port->bridge_device;
1583         mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1584                                                                bridge_device,
1585                                                                mdb->vid);
1586         if (!mlxsw_sp_port_vlan)
1587                 return 0;
1588
1589         fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1590
1591         mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1592         if (!mid) {
1593                 mid = __mlxsw_sp_mc_alloc(mlxsw_sp, bridge_device, mdb->addr,
1594                                           fid_index);
1595                 if (!mid) {
1596                         netdev_err(dev, "Unable to allocate MC group\n");
1597                         return -ENOMEM;
1598                 }
1599         }
1600         set_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1601
1602         if (!bridge_device->multicast_enabled)
1603                 return 0;
1604
1605         if (bridge_port->mrouter)
1606                 return 0;
1607
1608         err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true);
1609         if (err) {
1610                 netdev_err(dev, "Unable to set SMID\n");
1611                 goto err_out;
1612         }
1613
1614         return 0;
1615
1616 err_out:
1617         mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1618         return err;
1619 }
1620
1621 static void
1622 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
1623                                    struct mlxsw_sp_bridge_device
1624                                    *bridge_device)
1625 {
1626         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1627         struct mlxsw_sp_mid *mid;
1628         bool mc_enabled;
1629
1630         mc_enabled = bridge_device->multicast_enabled;
1631
1632         list_for_each_entry(mid, &bridge_device->mids_list, list) {
1633                 if (mc_enabled)
1634                         mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid,
1635                                                     bridge_device);
1636                 else
1637                         mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1638         }
1639 }
1640
1641 static void
1642 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
1643                                  struct mlxsw_sp_bridge_port *bridge_port,
1644                                  bool add)
1645 {
1646         struct mlxsw_sp_bridge_device *bridge_device;
1647         struct mlxsw_sp_mid *mid;
1648
1649         bridge_device = bridge_port->bridge_device;
1650
1651         list_for_each_entry(mid, &bridge_device->mids_list, list) {
1652                 if (!test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid))
1653                         mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, add);
1654         }
1655 }
1656
1657 struct mlxsw_sp_span_respin_work {
1658         struct work_struct work;
1659         struct mlxsw_sp *mlxsw_sp;
1660 };
1661
1662 static void mlxsw_sp_span_respin_work(struct work_struct *work)
1663 {
1664         struct mlxsw_sp_span_respin_work *respin_work =
1665                 container_of(work, struct mlxsw_sp_span_respin_work, work);
1666
1667         rtnl_lock();
1668         mlxsw_sp_span_respin(respin_work->mlxsw_sp);
1669         rtnl_unlock();
1670         kfree(respin_work);
1671 }
1672
1673 static void mlxsw_sp_span_respin_schedule(struct mlxsw_sp *mlxsw_sp)
1674 {
1675         struct mlxsw_sp_span_respin_work *respin_work;
1676
1677         respin_work = kzalloc(sizeof(*respin_work), GFP_ATOMIC);
1678         if (!respin_work)
1679                 return;
1680
1681         INIT_WORK(&respin_work->work, mlxsw_sp_span_respin_work);
1682         respin_work->mlxsw_sp = mlxsw_sp;
1683
1684         mlxsw_core_schedule_work(&respin_work->work);
1685 }
1686
1687 static int mlxsw_sp_port_obj_add(struct net_device *dev,
1688                                  const struct switchdev_obj *obj,
1689                                  struct switchdev_trans *trans)
1690 {
1691         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1692         const struct switchdev_obj_port_vlan *vlan;
1693         int err = 0;
1694
1695         switch (obj->id) {
1696         case SWITCHDEV_OBJ_ID_PORT_VLAN:
1697                 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
1698                 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port, vlan, trans);
1699
1700                 if (switchdev_trans_ph_prepare(trans)) {
1701                         /* The event is emitted before the changes are actually
1702                          * applied to the bridge. Therefore schedule the respin
1703                          * call for later, so that the respin logic sees the
1704                          * updated bridge state.
1705                          */
1706                         mlxsw_sp_span_respin_schedule(mlxsw_sp_port->mlxsw_sp);
1707                 }
1708                 break;
1709         case SWITCHDEV_OBJ_ID_PORT_MDB:
1710                 err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
1711                                             SWITCHDEV_OBJ_PORT_MDB(obj),
1712                                             trans);
1713                 break;
1714         default:
1715                 err = -EOPNOTSUPP;
1716                 break;
1717         }
1718
1719         return err;
1720 }
1721
1722 static void
1723 mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port *mlxsw_sp_port,
1724                               struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
1725 {
1726         u16 pvid = mlxsw_sp_port->pvid == vid ? 0 : vid;
1727         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1728
1729         mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1730         if (WARN_ON(!mlxsw_sp_port_vlan))
1731                 return;
1732
1733         mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1734         mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1735         mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1736         mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1737 }
1738
1739 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1740                                    const struct switchdev_obj_port_vlan *vlan)
1741 {
1742         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1743         struct net_device *orig_dev = vlan->obj.orig_dev;
1744         struct mlxsw_sp_bridge_port *bridge_port;
1745         u16 vid;
1746
1747         if (netif_is_bridge_master(orig_dev))
1748                 return -EOPNOTSUPP;
1749
1750         bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1751         if (WARN_ON(!bridge_port))
1752                 return -EINVAL;
1753
1754         if (!bridge_port->bridge_device->vlan_enabled)
1755                 return 0;
1756
1757         for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++)
1758                 mlxsw_sp_bridge_port_vlan_del(mlxsw_sp_port, bridge_port, vid);
1759
1760         return 0;
1761 }
1762
1763 static int
1764 __mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1765                         struct mlxsw_sp_bridge_port *bridge_port,
1766                         struct mlxsw_sp_mid *mid)
1767 {
1768         struct net_device *dev = mlxsw_sp_port->dev;
1769         int err;
1770
1771         if (bridge_port->bridge_device->multicast_enabled &&
1772             !bridge_port->mrouter) {
1773                 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1774                 if (err)
1775                         netdev_err(dev, "Unable to remove port from SMID\n");
1776         }
1777
1778         err = mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1779         if (err)
1780                 netdev_err(dev, "Unable to remove MC SFD\n");
1781
1782         return err;
1783 }
1784
1785 static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1786                                  const struct switchdev_obj_port_mdb *mdb)
1787 {
1788         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1789         struct net_device *orig_dev = mdb->obj.orig_dev;
1790         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1791         struct mlxsw_sp_bridge_device *bridge_device;
1792         struct net_device *dev = mlxsw_sp_port->dev;
1793         struct mlxsw_sp_bridge_port *bridge_port;
1794         struct mlxsw_sp_mid *mid;
1795         u16 fid_index;
1796
1797         bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1798         if (!bridge_port)
1799                 return 0;
1800
1801         bridge_device = bridge_port->bridge_device;
1802         mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1803                                                                bridge_device,
1804                                                                mdb->vid);
1805         if (!mlxsw_sp_port_vlan)
1806                 return 0;
1807
1808         fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1809
1810         mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1811         if (!mid) {
1812                 netdev_err(dev, "Unable to remove port from MC DB\n");
1813                 return -EINVAL;
1814         }
1815
1816         return __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port, mid);
1817 }
1818
1819 static void
1820 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
1821                                struct mlxsw_sp_bridge_port *bridge_port)
1822 {
1823         struct mlxsw_sp_bridge_device *bridge_device;
1824         struct mlxsw_sp_mid *mid, *tmp;
1825
1826         bridge_device = bridge_port->bridge_device;
1827
1828         list_for_each_entry_safe(mid, tmp, &bridge_device->mids_list, list) {
1829                 if (test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid)) {
1830                         __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port,
1831                                                 mid);
1832                 } else if (bridge_device->multicast_enabled &&
1833                            bridge_port->mrouter) {
1834                         mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1835                 }
1836         }
1837 }
1838
1839 static int mlxsw_sp_port_obj_del(struct net_device *dev,
1840                                  const struct switchdev_obj *obj)
1841 {
1842         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1843         int err = 0;
1844
1845         switch (obj->id) {
1846         case SWITCHDEV_OBJ_ID_PORT_VLAN:
1847                 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1848                                               SWITCHDEV_OBJ_PORT_VLAN(obj));
1849                 break;
1850         case SWITCHDEV_OBJ_ID_PORT_MDB:
1851                 err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1852                                             SWITCHDEV_OBJ_PORT_MDB(obj));
1853                 break;
1854         default:
1855                 err = -EOPNOTSUPP;
1856                 break;
1857         }
1858
1859         mlxsw_sp_span_respin_schedule(mlxsw_sp_port->mlxsw_sp);
1860
1861         return err;
1862 }
1863
1864 static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
1865                                                    u16 lag_id)
1866 {
1867         struct mlxsw_sp_port *mlxsw_sp_port;
1868         u64 max_lag_members;
1869         int i;
1870
1871         max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1872                                              MAX_LAG_MEMBERS);
1873         for (i = 0; i < max_lag_members; i++) {
1874                 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
1875                 if (mlxsw_sp_port)
1876                         return mlxsw_sp_port;
1877         }
1878         return NULL;
1879 }
1880
1881 static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = {
1882         .switchdev_port_attr_get        = mlxsw_sp_port_attr_get,
1883         .switchdev_port_attr_set        = mlxsw_sp_port_attr_set,
1884         .switchdev_port_obj_add         = mlxsw_sp_port_obj_add,
1885         .switchdev_port_obj_del         = mlxsw_sp_port_obj_del,
1886 };
1887
1888 static int
1889 mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1890                                 struct mlxsw_sp_bridge_port *bridge_port,
1891                                 struct mlxsw_sp_port *mlxsw_sp_port,
1892                                 struct netlink_ext_ack *extack)
1893 {
1894         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1895
1896         if (is_vlan_dev(bridge_port->dev)) {
1897                 NL_SET_ERR_MSG_MOD(extack, "Can not enslave a VLAN device to a VLAN-aware bridge");
1898                 return -EINVAL;
1899         }
1900
1901         mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, 1);
1902         if (WARN_ON(!mlxsw_sp_port_vlan))
1903                 return -EINVAL;
1904
1905         /* Let VLAN-aware bridge take care of its own VLANs */
1906         mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1907
1908         return 0;
1909 }
1910
1911 static void
1912 mlxsw_sp_bridge_8021q_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
1913                                  struct mlxsw_sp_bridge_port *bridge_port,
1914                                  struct mlxsw_sp_port *mlxsw_sp_port)
1915 {
1916         mlxsw_sp_port_vlan_get(mlxsw_sp_port, 1);
1917         /* Make sure untagged frames are allowed to ingress */
1918         mlxsw_sp_port_pvid_set(mlxsw_sp_port, 1);
1919 }
1920
1921 static struct mlxsw_sp_fid *
1922 mlxsw_sp_bridge_8021q_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
1923                               u16 vid)
1924 {
1925         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
1926
1927         return mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
1928 }
1929
1930 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021q_ops = {
1931         .port_join      = mlxsw_sp_bridge_8021q_port_join,
1932         .port_leave     = mlxsw_sp_bridge_8021q_port_leave,
1933         .fid_get        = mlxsw_sp_bridge_8021q_fid_get,
1934 };
1935
1936 static bool
1937 mlxsw_sp_port_is_br_member(const struct mlxsw_sp_port *mlxsw_sp_port,
1938                            const struct net_device *br_dev)
1939 {
1940         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1941
1942         list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
1943                             list) {
1944                 if (mlxsw_sp_port_vlan->bridge_port &&
1945                     mlxsw_sp_port_vlan->bridge_port->bridge_device->dev ==
1946                     br_dev)
1947                         return true;
1948         }
1949
1950         return false;
1951 }
1952
1953 static int
1954 mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1955                                 struct mlxsw_sp_bridge_port *bridge_port,
1956                                 struct mlxsw_sp_port *mlxsw_sp_port,
1957                                 struct netlink_ext_ack *extack)
1958 {
1959         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1960         struct net_device *dev = bridge_port->dev;
1961         u16 vid;
1962
1963         vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 1;
1964         mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1965         if (WARN_ON(!mlxsw_sp_port_vlan))
1966                 return -EINVAL;
1967
1968         if (mlxsw_sp_port_is_br_member(mlxsw_sp_port, bridge_device->dev)) {
1969                 NL_SET_ERR_MSG_MOD(extack, "Can not bridge VLAN uppers of the same port");
1970                 return -EINVAL;
1971         }
1972
1973         /* Port is no longer usable as a router interface */
1974         if (mlxsw_sp_port_vlan->fid)
1975                 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
1976
1977         return mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
1978 }
1979
1980 static void
1981 mlxsw_sp_bridge_8021d_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
1982                                  struct mlxsw_sp_bridge_port *bridge_port,
1983                                  struct mlxsw_sp_port *mlxsw_sp_port)
1984 {
1985         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1986         struct net_device *dev = bridge_port->dev;
1987         u16 vid;
1988
1989         vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 1;
1990         mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1991         if (WARN_ON(!mlxsw_sp_port_vlan))
1992                 return;
1993
1994         mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1995 }
1996
1997 static struct mlxsw_sp_fid *
1998 mlxsw_sp_bridge_8021d_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
1999                               u16 vid)
2000 {
2001         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2002
2003         return mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
2004 }
2005
2006 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021d_ops = {
2007         .port_join      = mlxsw_sp_bridge_8021d_port_join,
2008         .port_leave     = mlxsw_sp_bridge_8021d_port_leave,
2009         .fid_get        = mlxsw_sp_bridge_8021d_fid_get,
2010 };
2011
2012 int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port,
2013                               struct net_device *brport_dev,
2014                               struct net_device *br_dev,
2015                               struct netlink_ext_ack *extack)
2016 {
2017         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2018         struct mlxsw_sp_bridge_device *bridge_device;
2019         struct mlxsw_sp_bridge_port *bridge_port;
2020         int err;
2021
2022         bridge_port = mlxsw_sp_bridge_port_get(mlxsw_sp->bridge, brport_dev);
2023         if (IS_ERR(bridge_port))
2024                 return PTR_ERR(bridge_port);
2025         bridge_device = bridge_port->bridge_device;
2026
2027         err = bridge_device->ops->port_join(bridge_device, bridge_port,
2028                                             mlxsw_sp_port, extack);
2029         if (err)
2030                 goto err_port_join;
2031
2032         return 0;
2033
2034 err_port_join:
2035         mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2036         return err;
2037 }
2038
2039 void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port,
2040                                 struct net_device *brport_dev,
2041                                 struct net_device *br_dev)
2042 {
2043         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2044         struct mlxsw_sp_bridge_device *bridge_device;
2045         struct mlxsw_sp_bridge_port *bridge_port;
2046
2047         bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2048         if (!bridge_device)
2049                 return;
2050         bridge_port = __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
2051         if (!bridge_port)
2052                 return;
2053
2054         bridge_device->ops->port_leave(bridge_device, bridge_port,
2055                                        mlxsw_sp_port);
2056         mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2057 }
2058
2059 static void
2060 mlxsw_sp_fdb_call_notifiers(enum switchdev_notifier_type type,
2061                             const char *mac, u16 vid,
2062                             struct net_device *dev)
2063 {
2064         struct switchdev_notifier_fdb_info info;
2065
2066         info.addr = mac;
2067         info.vid = vid;
2068         call_switchdev_notifiers(type, dev, &info.info);
2069 }
2070
2071 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
2072                                             char *sfn_pl, int rec_index,
2073                                             bool adding)
2074 {
2075         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2076         struct mlxsw_sp_bridge_device *bridge_device;
2077         struct mlxsw_sp_bridge_port *bridge_port;
2078         struct mlxsw_sp_port *mlxsw_sp_port;
2079         enum switchdev_notifier_type type;
2080         char mac[ETH_ALEN];
2081         u8 local_port;
2082         u16 vid, fid;
2083         bool do_notification = true;
2084         int err;
2085
2086         mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
2087         mlxsw_sp_port = mlxsw_sp->ports[local_port];
2088         if (!mlxsw_sp_port) {
2089                 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
2090                 goto just_remove;
2091         }
2092
2093         mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2094         if (!mlxsw_sp_port_vlan) {
2095                 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2096                 goto just_remove;
2097         }
2098
2099         bridge_port = mlxsw_sp_port_vlan->bridge_port;
2100         if (!bridge_port) {
2101                 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2102                 goto just_remove;
2103         }
2104
2105         bridge_device = bridge_port->bridge_device;
2106         vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2107
2108 do_fdb_op:
2109         err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
2110                                       adding, true);
2111         if (err) {
2112                 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2113                 return;
2114         }
2115
2116         if (!do_notification)
2117                 return;
2118         type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2119         mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev);
2120
2121         return;
2122
2123 just_remove:
2124         adding = false;
2125         do_notification = false;
2126         goto do_fdb_op;
2127 }
2128
2129 static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
2130                                                 char *sfn_pl, int rec_index,
2131                                                 bool adding)
2132 {
2133         struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2134         struct mlxsw_sp_bridge_device *bridge_device;
2135         struct mlxsw_sp_bridge_port *bridge_port;
2136         struct mlxsw_sp_port *mlxsw_sp_port;
2137         enum switchdev_notifier_type type;
2138         char mac[ETH_ALEN];
2139         u16 lag_vid = 0;
2140         u16 lag_id;
2141         u16 vid, fid;
2142         bool do_notification = true;
2143         int err;
2144
2145         mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
2146         mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
2147         if (!mlxsw_sp_port) {
2148                 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
2149                 goto just_remove;
2150         }
2151
2152         mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2153         if (!mlxsw_sp_port_vlan) {
2154                 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2155                 goto just_remove;
2156         }
2157
2158         bridge_port = mlxsw_sp_port_vlan->bridge_port;
2159         if (!bridge_port) {
2160                 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2161                 goto just_remove;
2162         }
2163
2164         bridge_device = bridge_port->bridge_device;
2165         vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2166         lag_vid = mlxsw_sp_port_vlan->vid;
2167
2168 do_fdb_op:
2169         err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
2170                                           adding, true);
2171         if (err) {
2172                 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2173                 return;
2174         }
2175
2176         if (!do_notification)
2177                 return;
2178         type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2179         mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev);
2180
2181         return;
2182
2183 just_remove:
2184         adding = false;
2185         do_notification = false;
2186         goto do_fdb_op;
2187 }
2188
2189 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
2190                                             char *sfn_pl, int rec_index)
2191 {
2192         switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
2193         case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
2194                 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2195                                                 rec_index, true);
2196                 break;
2197         case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
2198                 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2199                                                 rec_index, false);
2200                 break;
2201         case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
2202                 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2203                                                     rec_index, true);
2204                 break;
2205         case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
2206                 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2207                                                     rec_index, false);
2208                 break;
2209         }
2210 }
2211
2212 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp)
2213 {
2214         struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2215
2216         mlxsw_core_schedule_dw(&bridge->fdb_notify.dw,
2217                                msecs_to_jiffies(bridge->fdb_notify.interval));
2218 }
2219
2220 static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
2221 {
2222         struct mlxsw_sp_bridge *bridge;
2223         struct mlxsw_sp *mlxsw_sp;
2224         char *sfn_pl;
2225         u8 num_rec;
2226         int i;
2227         int err;
2228
2229         sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
2230         if (!sfn_pl)
2231                 return;
2232
2233         bridge = container_of(work, struct mlxsw_sp_bridge, fdb_notify.dw.work);
2234         mlxsw_sp = bridge->mlxsw_sp;
2235
2236         rtnl_lock();
2237         mlxsw_reg_sfn_pack(sfn_pl);
2238         err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
2239         if (err) {
2240                 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
2241                 goto out;
2242         }
2243         num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
2244         for (i = 0; i < num_rec; i++)
2245                 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
2246
2247 out:
2248         rtnl_unlock();
2249         kfree(sfn_pl);
2250         mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2251 }
2252
2253 struct mlxsw_sp_switchdev_event_work {
2254         struct work_struct work;
2255         struct switchdev_notifier_fdb_info fdb_info;
2256         struct net_device *dev;
2257         unsigned long event;
2258 };
2259
2260 static void mlxsw_sp_switchdev_event_work(struct work_struct *work)
2261 {
2262         struct mlxsw_sp_switchdev_event_work *switchdev_work =
2263                 container_of(work, struct mlxsw_sp_switchdev_event_work, work);
2264         struct net_device *dev = switchdev_work->dev;
2265         struct switchdev_notifier_fdb_info *fdb_info;
2266         struct mlxsw_sp_port *mlxsw_sp_port;
2267         int err;
2268
2269         rtnl_lock();
2270         mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
2271         if (!mlxsw_sp_port)
2272                 goto out;
2273
2274         switch (switchdev_work->event) {
2275         case SWITCHDEV_FDB_ADD_TO_DEVICE:
2276                 fdb_info = &switchdev_work->fdb_info;
2277                 if (!fdb_info->added_by_user)
2278                         break;
2279                 err = mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, true);
2280                 if (err)
2281                         break;
2282                 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2283                                             fdb_info->addr,
2284                                             fdb_info->vid, dev);
2285                 break;
2286         case SWITCHDEV_FDB_DEL_TO_DEVICE:
2287                 fdb_info = &switchdev_work->fdb_info;
2288                 if (!fdb_info->added_by_user)
2289                         break;
2290                 mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, false);
2291                 break;
2292         case SWITCHDEV_FDB_ADD_TO_BRIDGE: /* fall through */
2293         case SWITCHDEV_FDB_DEL_TO_BRIDGE:
2294                 /* These events are only used to potentially update an existing
2295                  * SPAN mirror.
2296                  */
2297                 break;
2298         }
2299
2300         mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
2301
2302 out:
2303         rtnl_unlock();
2304         kfree(switchdev_work->fdb_info.addr);
2305         kfree(switchdev_work);
2306         dev_put(dev);
2307 }
2308
2309 /* Called under rcu_read_lock() */
2310 static int mlxsw_sp_switchdev_event(struct notifier_block *unused,
2311                                     unsigned long event, void *ptr)
2312 {
2313         struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2314         struct mlxsw_sp_switchdev_event_work *switchdev_work;
2315         struct switchdev_notifier_fdb_info *fdb_info = ptr;
2316
2317         if (!mlxsw_sp_port_dev_lower_find_rcu(dev))
2318                 return NOTIFY_DONE;
2319
2320         switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2321         if (!switchdev_work)
2322                 return NOTIFY_BAD;
2323
2324         INIT_WORK(&switchdev_work->work, mlxsw_sp_switchdev_event_work);
2325         switchdev_work->dev = dev;
2326         switchdev_work->event = event;
2327
2328         switch (event) {
2329         case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
2330         case SWITCHDEV_FDB_DEL_TO_DEVICE: /* fall through */
2331         case SWITCHDEV_FDB_ADD_TO_BRIDGE: /* fall through */
2332         case SWITCHDEV_FDB_DEL_TO_BRIDGE:
2333                 memcpy(&switchdev_work->fdb_info, ptr,
2334                        sizeof(switchdev_work->fdb_info));
2335                 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
2336                 if (!switchdev_work->fdb_info.addr)
2337                         goto err_addr_alloc;
2338                 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
2339                                 fdb_info->addr);
2340                 /* Take a reference on the device. This can be either
2341                  * upper device containig mlxsw_sp_port or just a
2342                  * mlxsw_sp_port
2343                  */
2344                 dev_hold(dev);
2345                 break;
2346         default:
2347                 kfree(switchdev_work);
2348                 return NOTIFY_DONE;
2349         }
2350
2351         mlxsw_core_schedule_work(&switchdev_work->work);
2352
2353         return NOTIFY_DONE;
2354
2355 err_addr_alloc:
2356         kfree(switchdev_work);
2357         return NOTIFY_BAD;
2358 }
2359
2360 static struct notifier_block mlxsw_sp_switchdev_notifier = {
2361         .notifier_call = mlxsw_sp_switchdev_event,
2362 };
2363
2364 u8
2365 mlxsw_sp_bridge_port_stp_state(struct mlxsw_sp_bridge_port *bridge_port)
2366 {
2367         return bridge_port->stp_state;
2368 }
2369
2370 static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
2371 {
2372         struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2373         int err;
2374
2375         err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
2376         if (err) {
2377                 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
2378                 return err;
2379         }
2380
2381         err = register_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
2382         if (err) {
2383                 dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev notifier\n");
2384                 return err;
2385         }
2386
2387         INIT_DELAYED_WORK(&bridge->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
2388         bridge->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
2389         mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2390         return 0;
2391 }
2392
2393 static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
2394 {
2395         cancel_delayed_work_sync(&mlxsw_sp->bridge->fdb_notify.dw);
2396         unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
2397
2398 }
2399
2400 int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
2401 {
2402         struct mlxsw_sp_bridge *bridge;
2403
2404         bridge = kzalloc(sizeof(*mlxsw_sp->bridge), GFP_KERNEL);
2405         if (!bridge)
2406                 return -ENOMEM;
2407         mlxsw_sp->bridge = bridge;
2408         bridge->mlxsw_sp = mlxsw_sp;
2409
2410         INIT_LIST_HEAD(&mlxsw_sp->bridge->bridges_list);
2411
2412         bridge->bridge_8021q_ops = &mlxsw_sp_bridge_8021q_ops;
2413         bridge->bridge_8021d_ops = &mlxsw_sp_bridge_8021d_ops;
2414
2415         return mlxsw_sp_fdb_init(mlxsw_sp);
2416 }
2417
2418 void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
2419 {
2420         mlxsw_sp_fdb_fini(mlxsw_sp);
2421         WARN_ON(!list_empty(&mlxsw_sp->bridge->bridges_list));
2422         kfree(mlxsw_sp->bridge);
2423 }
2424
2425 void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port *mlxsw_sp_port)
2426 {
2427         mlxsw_sp_port->dev->switchdev_ops = &mlxsw_sp_port_switchdev_ops;
2428 }
2429
2430 void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port)
2431 {
2432 }