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