Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux-2.6-block.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 1205 char *sfd_pl;
0a8a1bf1 1206 u8 num_rec;
56ade8fe
JP
1207 int err;
1208
56ade8fe
JP
1209 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1210 if (!sfd_pl)
1211 return -ENOMEM;
1212
8a1ab5d7
JP
1213 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1214 mlxsw_reg_sfd_uc_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
6e095fd4 1215 mac, fid, action, local_port);
0a8a1bf1 1216 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
8a1ab5d7 1217 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
0a8a1bf1
ST
1218 if (err)
1219 goto out;
8a1ab5d7 1220
0a8a1bf1
ST
1221 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1222 err = -EBUSY;
1223
1224out:
1225 kfree(sfd_pl);
8a1ab5d7
JP
1226 return err;
1227}
1228
6e095fd4
IS
1229static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1230 const char *mac, u16 fid, bool adding,
1231 bool dynamic)
1232{
1233 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding,
1234 MLXSW_REG_SFD_REC_ACTION_NOP, dynamic);
1235}
1236
1237int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid,
1238 bool adding)
1239{
1240 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding,
1241 MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER,
1242 false);
1243}
1244
8a1ab5d7 1245static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
64771e31
IS
1246 const char *mac, u16 fid, u16 lag_vid,
1247 bool adding, bool dynamic)
8a1ab5d7
JP
1248{
1249 char *sfd_pl;
0a8a1bf1 1250 u8 num_rec;
8a1ab5d7
JP
1251 int err;
1252
1253 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1254 if (!sfd_pl)
1255 return -ENOMEM;
1256
1257 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1258 mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
64771e31
IS
1259 mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
1260 lag_vid, lag_id);
0a8a1bf1 1261 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
8a1ab5d7 1262 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
0a8a1bf1
ST
1263 if (err)
1264 goto out;
56ade8fe 1265
0a8a1bf1
ST
1266 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1267 err = -EBUSY;
1268
1269out:
1270 kfree(sfd_pl);
56ade8fe
JP
1271 return err;
1272}
1273
af061378
AS
1274static int
1275mlxsw_sp_port_fdb_set(struct mlxsw_sp_port *mlxsw_sp_port,
1276 struct switchdev_notifier_fdb_info *fdb_info, bool adding)
1277{
1278 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1279 struct net_device *orig_dev = fdb_info->info.dev;
1280 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1281 struct mlxsw_sp_bridge_device *bridge_device;
1282 struct mlxsw_sp_bridge_port *bridge_port;
1283 u16 fid_index, vid;
1284
1285 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1286 if (!bridge_port)
1287 return -EINVAL;
1288
1289 bridge_device = bridge_port->bridge_device;
1290 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1291 bridge_device,
1292 fdb_info->vid);
1293 if (!mlxsw_sp_port_vlan)
1294 return 0;
1295
1296 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1297 vid = mlxsw_sp_port_vlan->vid;
1298
1299 if (!bridge_port->lagged)
1300 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp,
1301 bridge_port->system_port,
1302 fdb_info->addr, fid_index,
1303 adding, false);
1304 else
1305 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp,
1306 bridge_port->lag_id,
1307 fdb_info->addr, fid_index,
1308 vid, adding, false);
1309}
1310
3a49b4fd 1311static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
5f9abc59 1312 u16 fid, u16 mid_idx, bool adding)
3a49b4fd
ER
1313{
1314 char *sfd_pl;
0a8a1bf1 1315 u8 num_rec;
3a49b4fd
ER
1316 int err;
1317
1318 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1319 if (!sfd_pl)
1320 return -ENOMEM;
1321
1322 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1323 mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
5f9abc59 1324 MLXSW_REG_SFD_REC_ACTION_NOP, mid_idx);
0a8a1bf1 1325 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
3a49b4fd 1326 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
0a8a1bf1
ST
1327 if (err)
1328 goto out;
1329
1330 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1331 err = -EBUSY;
1332
1333out:
3a49b4fd
ER
1334 kfree(sfd_pl);
1335 return err;
1336}
1337
2e3496cd 1338static int mlxsw_sp_port_smid_full_entry(struct mlxsw_sp *mlxsw_sp, u16 mid_idx,
c4db953f
YG
1339 long *ports_bitmap,
1340 bool set_router_port)
3a49b4fd 1341{
3a49b4fd
ER
1342 char *smid_pl;
1343 int err, i;
1344
1345 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1346 if (!smid_pl)
1347 return -ENOMEM;
1348
5f9abc59
NF
1349 mlxsw_reg_smid_pack(smid_pl, mid_idx, 0, false);
1350 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++) {
1351 if (mlxsw_sp->ports[i])
1352 mlxsw_reg_smid_port_mask_set(smid_pl, i, 1);
3a49b4fd 1353 }
5f9abc59 1354
c4db953f
YG
1355 mlxsw_reg_smid_port_mask_set(smid_pl,
1356 mlxsw_sp_router_port(mlxsw_sp), 1);
1357
2e3496cd
NF
1358 for_each_set_bit(i, ports_bitmap, mlxsw_core_max_ports(mlxsw_sp->core))
1359 mlxsw_reg_smid_port_set(smid_pl, i, 1);
1360
c4db953f
YG
1361 mlxsw_reg_smid_port_set(smid_pl, mlxsw_sp_router_port(mlxsw_sp),
1362 set_router_port);
1363
5f9abc59
NF
1364 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1365 kfree(smid_pl);
1366 return err;
1367}
1368
1369static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port,
1370 u16 mid_idx, bool add)
1371{
1372 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1373 char *smid_pl;
1374 int err;
1375
1376 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1377 if (!smid_pl)
1378 return -ENOMEM;
1379
1380 mlxsw_reg_smid_pack(smid_pl, mid_idx, mlxsw_sp_port->local_port, add);
3a49b4fd
ER
1381 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1382 kfree(smid_pl);
1383 return err;
1384}
1385
b80888a9
NF
1386static struct
1387mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp_bridge_device *bridge_device,
1388 const unsigned char *addr,
1389 u16 fid)
3a49b4fd
ER
1390{
1391 struct mlxsw_sp_mid *mid;
1392
b80888a9 1393 list_for_each_entry(mid, &bridge_device->mids_list, list) {
46d0847c 1394 if (ether_addr_equal(mid->addr, addr) && mid->fid == fid)
3a49b4fd
ER
1395 return mid;
1396 }
1397 return NULL;
1398}
1399
3fba877c
NF
1400static void
1401mlxsw_sp_bridge_port_get_ports_bitmap(struct mlxsw_sp *mlxsw_sp,
1402 struct mlxsw_sp_bridge_port *bridge_port,
1403 unsigned long *ports_bitmap)
1404{
1405 struct mlxsw_sp_port *mlxsw_sp_port;
1406 u64 max_lag_members, i;
1407 int lag_id;
1408
1409 if (!bridge_port->lagged) {
1410 set_bit(bridge_port->system_port, ports_bitmap);
1411 } else {
1412 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1413 MAX_LAG_MEMBERS);
1414 lag_id = bridge_port->lag_id;
1415 for (i = 0; i < max_lag_members; i++) {
1416 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp,
1417 lag_id, i);
1418 if (mlxsw_sp_port)
1419 set_bit(mlxsw_sp_port->local_port,
1420 ports_bitmap);
1421 }
1422 }
1423}
1424
1425static void
1426mlxsw_sp_mc_get_mrouters_bitmap(unsigned long *flood_bitmap,
1427 struct mlxsw_sp_bridge_device *bridge_device,
1428 struct mlxsw_sp *mlxsw_sp)
1429{
1430 struct mlxsw_sp_bridge_port *bridge_port;
1431
1432 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
1433 if (bridge_port->mrouter) {
1434 mlxsw_sp_bridge_port_get_ports_bitmap(mlxsw_sp,
1435 bridge_port,
1436 flood_bitmap);
1437 }
1438 }
1439}
1440
73b433e8
NF
1441static bool
1442mlxsw_sp_mc_write_mdb_entry(struct mlxsw_sp *mlxsw_sp,
3fba877c
NF
1443 struct mlxsw_sp_mid *mid,
1444 struct mlxsw_sp_bridge_device *bridge_device)
73b433e8 1445{
3fba877c
NF
1446 long *flood_bitmap;
1447 int num_of_ports;
1448 int alloc_size;
73b433e8
NF
1449 u16 mid_idx;
1450 int err;
1451
1452 mid_idx = find_first_zero_bit(mlxsw_sp->bridge->mids_bitmap,
1453 MLXSW_SP_MID_MAX);
1454 if (mid_idx == MLXSW_SP_MID_MAX)
1455 return false;
1456
3fba877c
NF
1457 num_of_ports = mlxsw_core_max_ports(mlxsw_sp->core);
1458 alloc_size = sizeof(long) * BITS_TO_LONGS(num_of_ports);
1459 flood_bitmap = kzalloc(alloc_size, GFP_KERNEL);
1460 if (!flood_bitmap)
1461 return false;
1462
1463 bitmap_copy(flood_bitmap, mid->ports_in_mid, num_of_ports);
1464 mlxsw_sp_mc_get_mrouters_bitmap(flood_bitmap, bridge_device, mlxsw_sp);
1465
73b433e8 1466 mid->mid = mid_idx;
c4db953f 1467 err = mlxsw_sp_port_smid_full_entry(mlxsw_sp, mid_idx, flood_bitmap,
593bc28a 1468 bridge_device->mrouter);
3fba877c 1469 kfree(flood_bitmap);
73b433e8
NF
1470 if (err)
1471 return false;
1472
1473 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid_idx,
1474 true);
1475 if (err)
1476 return false;
1477
1478 set_bit(mid_idx, mlxsw_sp->bridge->mids_bitmap);
1479 mid->in_hw = true;
1480 return true;
1481}
1482
1483static int mlxsw_sp_mc_remove_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1484 struct mlxsw_sp_mid *mid)
1485{
846fd8a0
NF
1486 if (!mid->in_hw)
1487 return 0;
1488
73b433e8
NF
1489 clear_bit(mid->mid, mlxsw_sp->bridge->mids_bitmap);
1490 mid->in_hw = false;
1491 return mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid->mid,
1492 false);
1493}
1494
b80888a9
NF
1495static struct
1496mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
1497 struct mlxsw_sp_bridge_device *bridge_device,
1498 const unsigned char *addr,
1499 u16 fid)
3a49b4fd
ER
1500{
1501 struct mlxsw_sp_mid *mid;
4cdc35e4 1502 size_t alloc_size;
3a49b4fd
ER
1503
1504 mid = kzalloc(sizeof(*mid), GFP_KERNEL);
1505 if (!mid)
1506 return NULL;
1507
4cdc35e4
NF
1508 alloc_size = sizeof(unsigned long) *
1509 BITS_TO_LONGS(mlxsw_core_max_ports(mlxsw_sp->core));
73b433e8 1510
4cdc35e4 1511 mid->ports_in_mid = kzalloc(alloc_size, GFP_KERNEL);
73b433e8
NF
1512 if (!mid->ports_in_mid)
1513 goto err_ports_in_mid_alloc;
4cdc35e4 1514
3a49b4fd 1515 ether_addr_copy(mid->addr, addr);
46d0847c 1516 mid->fid = fid;
73b433e8 1517 mid->in_hw = false;
846fd8a0
NF
1518
1519 if (!bridge_device->multicast_enabled)
1520 goto out;
1521
3fba877c 1522 if (!mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid, bridge_device))
73b433e8
NF
1523 goto err_write_mdb_entry;
1524
846fd8a0 1525out:
b80888a9 1526 list_add_tail(&mid->list, &bridge_device->mids_list);
3a49b4fd 1527 return mid;
73b433e8
NF
1528
1529err_write_mdb_entry:
1530 kfree(mid->ports_in_mid);
1531err_ports_in_mid_alloc:
1532 kfree(mid);
1533 return NULL;
3a49b4fd
ER
1534}
1535
0161b950
NF
1536static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port *mlxsw_sp_port,
1537 struct mlxsw_sp_mid *mid)
3a49b4fd 1538{
4cdc35e4 1539 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
73b433e8 1540 int err = 0;
4cdc35e4
NF
1541
1542 clear_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
0161b950
NF
1543 if (bitmap_empty(mid->ports_in_mid,
1544 mlxsw_core_max_ports(mlxsw_sp->core))) {
73b433e8 1545 err = mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
3a49b4fd 1546 list_del(&mid->list);
4cdc35e4 1547 kfree(mid->ports_in_mid);
3a49b4fd 1548 kfree(mid);
3a49b4fd 1549 }
73b433e8 1550 return err;
3a49b4fd
ER
1551}
1552
1553static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
1554 const struct switchdev_obj_port_mdb *mdb,
1555 struct switchdev_trans *trans)
1556{
1557 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
c57529e1
IS
1558 struct net_device *orig_dev = mdb->obj.orig_dev;
1559 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
3a49b4fd 1560 struct net_device *dev = mlxsw_sp_port->dev;
c57529e1
IS
1561 struct mlxsw_sp_bridge_device *bridge_device;
1562 struct mlxsw_sp_bridge_port *bridge_port;
3a49b4fd 1563 struct mlxsw_sp_mid *mid;
c57529e1 1564 u16 fid_index;
3a49b4fd
ER
1565 int err = 0;
1566
1567 if (switchdev_trans_ph_prepare(trans))
1568 return 0;
1569
c57529e1 1570 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
17b334a8
IS
1571 if (!bridge_port)
1572 return 0;
c57529e1
IS
1573
1574 bridge_device = bridge_port->bridge_device;
1575 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1576 bridge_device,
1577 mdb->vid);
17b334a8
IS
1578 if (!mlxsw_sp_port_vlan)
1579 return 0;
c57529e1 1580
a1107487 1581 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
c57529e1 1582
b80888a9 1583 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
3a49b4fd 1584 if (!mid) {
b80888a9
NF
1585 mid = __mlxsw_sp_mc_alloc(mlxsw_sp, bridge_device, mdb->addr,
1586 fid_index);
3a49b4fd
ER
1587 if (!mid) {
1588 netdev_err(dev, "Unable to allocate MC group\n");
1589 return -ENOMEM;
1590 }
1591 }
4cdc35e4 1592 set_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
3a49b4fd 1593
846fd8a0
NF
1594 if (!bridge_device->multicast_enabled)
1595 return 0;
1596
ded711c8
NF
1597 if (bridge_port->mrouter)
1598 return 0;
1599
5f9abc59 1600 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true);
3a49b4fd
ER
1601 if (err) {
1602 netdev_err(dev, "Unable to set SMID\n");
1603 goto err_out;
1604 }
1605
3a49b4fd
ER
1606 return 0;
1607
1608err_out:
0161b950 1609 mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
3a49b4fd
ER
1610 return err;
1611}
1612
2e3496cd
NF
1613static void
1614mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
1615 struct mlxsw_sp_bridge_device
1616 *bridge_device)
1617{
1618 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1619 struct mlxsw_sp_mid *mid;
1620 bool mc_enabled;
1621
1622 mc_enabled = bridge_device->multicast_enabled;
1623
1624 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1625 if (mc_enabled)
3fba877c
NF
1626 mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid,
1627 bridge_device);
2e3496cd
NF
1628 else
1629 mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1630 }
1631}
1632
3ddda117
NF
1633static void
1634mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
1635 struct mlxsw_sp_bridge_port *bridge_port,
1636 bool add)
1637{
1638 struct mlxsw_sp_bridge_device *bridge_device;
1639 struct mlxsw_sp_mid *mid;
1640
1641 bridge_device = bridge_port->bridge_device;
1642
1643 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1644 if (!test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid))
1645 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, add);
1646 }
1647}
1648
56ade8fe
JP
1649static int mlxsw_sp_port_obj_add(struct net_device *dev,
1650 const struct switchdev_obj *obj,
1651 struct switchdev_trans *trans)
1652{
1653 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1654 int err = 0;
1655
1656 switch (obj->id) {
1657 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1658 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port,
1659 SWITCHDEV_OBJ_PORT_VLAN(obj),
1660 trans);
1661 break;
3a49b4fd
ER
1662 case SWITCHDEV_OBJ_ID_PORT_MDB:
1663 err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
1664 SWITCHDEV_OBJ_PORT_MDB(obj),
1665 trans);
1666 break;
56ade8fe
JP
1667 default:
1668 err = -EOPNOTSUPP;
1669 break;
1670 }
1671
1672 return err;
1673}
1674
c57529e1
IS
1675static void
1676mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port *mlxsw_sp_port,
1677 struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
56ade8fe 1678{
fe9ccc78 1679 u16 pvid = mlxsw_sp_port->pvid == vid ? 0 : vid;
c57529e1 1680 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
fe9ccc78 1681
c57529e1
IS
1682 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1683 if (WARN_ON(!mlxsw_sp_port_vlan))
1684 return;
1685
1686 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
fe9ccc78
IS
1687 mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1688 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
c57529e1 1689 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
56ade8fe
JP
1690}
1691
1692static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1693 const struct switchdev_obj_port_vlan *vlan)
1694{
c57529e1
IS
1695 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1696 struct net_device *orig_dev = vlan->obj.orig_dev;
1697 struct mlxsw_sp_bridge_port *bridge_port;
fe9ccc78
IS
1698 u16 vid;
1699
c57529e1
IS
1700 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1701 if (WARN_ON(!bridge_port))
1702 return -EINVAL;
fe9ccc78 1703
c57529e1
IS
1704 if (!bridge_port->bridge_device->vlan_enabled)
1705 return 0;
56ade8fe 1706
c57529e1
IS
1707 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++)
1708 mlxsw_sp_bridge_port_vlan_del(mlxsw_sp_port, bridge_port, vid);
4dc236c3 1709
c57529e1 1710 return 0;
4dc236c3
IS
1711}
1712
061e55bf
NF
1713static int
1714__mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1715 struct mlxsw_sp_bridge_port *bridge_port,
1716 struct mlxsw_sp_mid *mid)
1717{
1718 struct net_device *dev = mlxsw_sp_port->dev;
1719 int err;
1720
c7f46cca
IS
1721 if (bridge_port->bridge_device->multicast_enabled &&
1722 !bridge_port->mrouter) {
1723 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1724 if (err)
1725 netdev_err(dev, "Unable to remove port from SMID\n");
846fd8a0 1726 }
061e55bf
NF
1727
1728 err = mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1729 if (err)
1730 netdev_err(dev, "Unable to remove MC SFD\n");
1731
1732 return err;
1733}
1734
3a49b4fd
ER
1735static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1736 const struct switchdev_obj_port_mdb *mdb)
1737{
1738 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
c57529e1
IS
1739 struct net_device *orig_dev = mdb->obj.orig_dev;
1740 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1741 struct mlxsw_sp_bridge_device *bridge_device;
3a49b4fd 1742 struct net_device *dev = mlxsw_sp_port->dev;
c57529e1 1743 struct mlxsw_sp_bridge_port *bridge_port;
3a49b4fd 1744 struct mlxsw_sp_mid *mid;
c57529e1 1745 u16 fid_index;
3a49b4fd 1746
c57529e1 1747 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
17b334a8
IS
1748 if (!bridge_port)
1749 return 0;
c57529e1
IS
1750
1751 bridge_device = bridge_port->bridge_device;
1752 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1753 bridge_device,
1754 mdb->vid);
17b334a8
IS
1755 if (!mlxsw_sp_port_vlan)
1756 return 0;
c57529e1 1757
a1107487 1758 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
c57529e1 1759
b80888a9 1760 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
3a49b4fd
ER
1761 if (!mid) {
1762 netdev_err(dev, "Unable to remove port from MC DB\n");
1763 return -EINVAL;
1764 }
1765
061e55bf 1766 return __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port, mid);
3a49b4fd
ER
1767}
1768
bb5355b2
NF
1769static void
1770mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
1771 struct mlxsw_sp_bridge_port *bridge_port)
1772{
1773 struct mlxsw_sp_bridge_device *bridge_device;
1774 struct mlxsw_sp_mid *mid, *tmp;
1775
1776 bridge_device = bridge_port->bridge_device;
1777
1778 list_for_each_entry_safe(mid, tmp, &bridge_device->mids_list, list) {
1779 if (test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid)) {
1780 __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port,
1781 mid);
01662777
NF
1782 } else if (bridge_device->multicast_enabled &&
1783 bridge_port->mrouter) {
1784 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
bb5355b2
NF
1785 }
1786 }
1787}
1788
56ade8fe
JP
1789static int mlxsw_sp_port_obj_del(struct net_device *dev,
1790 const struct switchdev_obj *obj)
1791{
1792 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1793 int err = 0;
1794
1795 switch (obj->id) {
1796 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1797 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1798 SWITCHDEV_OBJ_PORT_VLAN(obj));
1799 break;
3a49b4fd
ER
1800 case SWITCHDEV_OBJ_ID_PORT_MDB:
1801 err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1802 SWITCHDEV_OBJ_PORT_MDB(obj));
00ae40e7 1803 break;
56ade8fe
JP
1804 default:
1805 err = -EOPNOTSUPP;
1806 break;
1807 }
1808
1809 return err;
1810}
1811
8a1ab5d7
JP
1812static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
1813 u16 lag_id)
1814{
1815 struct mlxsw_sp_port *mlxsw_sp_port;
c1a38311 1816 u64 max_lag_members;
8a1ab5d7
JP
1817 int i;
1818
c1a38311
JP
1819 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1820 MAX_LAG_MEMBERS);
1821 for (i = 0; i < max_lag_members; i++) {
8a1ab5d7
JP
1822 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
1823 if (mlxsw_sp_port)
1824 return mlxsw_sp_port;
1825 }
1826 return NULL;
1827}
1828
c7070fc4 1829static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = {
56ade8fe
JP
1830 .switchdev_port_attr_get = mlxsw_sp_port_attr_get,
1831 .switchdev_port_attr_set = mlxsw_sp_port_attr_set,
1832 .switchdev_port_obj_add = mlxsw_sp_port_obj_add,
1833 .switchdev_port_obj_del = mlxsw_sp_port_obj_del,
56ade8fe
JP
1834};
1835
c57529e1
IS
1836static int
1837mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1838 struct mlxsw_sp_bridge_port *bridge_port,
9b63ef88
IS
1839 struct mlxsw_sp_port *mlxsw_sp_port,
1840 struct netlink_ext_ack *extack)
c57529e1
IS
1841{
1842 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1843
9b63ef88 1844 if (is_vlan_dev(bridge_port->dev)) {
6c677750 1845 NL_SET_ERR_MSG_MOD(extack, "Can not enslave a VLAN device to a VLAN-aware bridge");
c57529e1 1846 return -EINVAL;
9b63ef88 1847 }
c57529e1
IS
1848
1849 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, 1);
1850 if (WARN_ON(!mlxsw_sp_port_vlan))
1851 return -EINVAL;
1852
1853 /* Let VLAN-aware bridge take care of its own VLANs */
1854 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1855
1856 return 0;
1857}
1858
1859static void
1860mlxsw_sp_bridge_8021q_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
1861 struct mlxsw_sp_bridge_port *bridge_port,
1862 struct mlxsw_sp_port *mlxsw_sp_port)
1863{
1864 mlxsw_sp_port_vlan_get(mlxsw_sp_port, 1);
1865 /* Make sure untagged frames are allowed to ingress */
1866 mlxsw_sp_port_pvid_set(mlxsw_sp_port, 1);
1867}
1868
a1107487
IS
1869static struct mlxsw_sp_fid *
1870mlxsw_sp_bridge_8021q_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
1871 u16 vid)
1872{
1873 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
1874
1875 return mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
1876}
1877
c57529e1
IS
1878static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021q_ops = {
1879 .port_join = mlxsw_sp_bridge_8021q_port_join,
1880 .port_leave = mlxsw_sp_bridge_8021q_port_leave,
a1107487 1881 .fid_get = mlxsw_sp_bridge_8021q_fid_get,
c57529e1
IS
1882};
1883
1884static bool
1885mlxsw_sp_port_is_br_member(const struct mlxsw_sp_port *mlxsw_sp_port,
1886 const struct net_device *br_dev)
1887{
1888 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1889
1890 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
1891 list) {
1892 if (mlxsw_sp_port_vlan->bridge_port &&
1893 mlxsw_sp_port_vlan->bridge_port->bridge_device->dev ==
1894 br_dev)
1895 return true;
1896 }
1897
1898 return false;
1899}
1900
1901static int
1902mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1903 struct mlxsw_sp_bridge_port *bridge_port,
9b63ef88
IS
1904 struct mlxsw_sp_port *mlxsw_sp_port,
1905 struct netlink_ext_ack *extack)
c57529e1
IS
1906{
1907 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
65b53bfd 1908 struct net_device *dev = bridge_port->dev;
c57529e1
IS
1909 u16 vid;
1910
65b53bfd 1911 vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 1;
c57529e1
IS
1912 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1913 if (WARN_ON(!mlxsw_sp_port_vlan))
1914 return -EINVAL;
c57529e1
IS
1915
1916 if (mlxsw_sp_port_is_br_member(mlxsw_sp_port, bridge_device->dev)) {
6c677750 1917 NL_SET_ERR_MSG_MOD(extack, "Can not bridge VLAN uppers of the same port");
c57529e1
IS
1918 return -EINVAL;
1919 }
1920
1921 /* Port is no longer usable as a router interface */
a1107487
IS
1922 if (mlxsw_sp_port_vlan->fid)
1923 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
c57529e1
IS
1924
1925 return mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
1926}
1927
1928static void
1929mlxsw_sp_bridge_8021d_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
1930 struct mlxsw_sp_bridge_port *bridge_port,
1931 struct mlxsw_sp_port *mlxsw_sp_port)
1932{
1933 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
65b53bfd
IS
1934 struct net_device *dev = bridge_port->dev;
1935 u16 vid;
c57529e1 1936
65b53bfd 1937 vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 1;
c57529e1
IS
1938 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1939 if (WARN_ON(!mlxsw_sp_port_vlan))
1940 return;
1941
1942 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1943}
1944
a1107487
IS
1945static struct mlxsw_sp_fid *
1946mlxsw_sp_bridge_8021d_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
1947 u16 vid)
1948{
1949 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
1950
1951 return mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
1952}
1953
c57529e1
IS
1954static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021d_ops = {
1955 .port_join = mlxsw_sp_bridge_8021d_port_join,
1956 .port_leave = mlxsw_sp_bridge_8021d_port_leave,
a1107487 1957 .fid_get = mlxsw_sp_bridge_8021d_fid_get,
c57529e1
IS
1958};
1959
1960int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port,
1961 struct net_device *brport_dev,
9b63ef88
IS
1962 struct net_device *br_dev,
1963 struct netlink_ext_ack *extack)
c57529e1
IS
1964{
1965 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1966 struct mlxsw_sp_bridge_device *bridge_device;
1967 struct mlxsw_sp_bridge_port *bridge_port;
1968 int err;
1969
1970 bridge_port = mlxsw_sp_bridge_port_get(mlxsw_sp->bridge, brport_dev);
1971 if (IS_ERR(bridge_port))
1972 return PTR_ERR(bridge_port);
1973 bridge_device = bridge_port->bridge_device;
1974
1975 err = bridge_device->ops->port_join(bridge_device, bridge_port,
9b63ef88 1976 mlxsw_sp_port, extack);
c57529e1
IS
1977 if (err)
1978 goto err_port_join;
1979
1980 return 0;
1981
1982err_port_join:
1983 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
1984 return err;
1985}
1986
1987void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port,
1988 struct net_device *brport_dev,
1989 struct net_device *br_dev)
1990{
1991 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1992 struct mlxsw_sp_bridge_device *bridge_device;
1993 struct mlxsw_sp_bridge_port *bridge_port;
1994
1995 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
1996 if (!bridge_device)
1997 return;
1998 bridge_port = __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
1999 if (!bridge_port)
2000 return;
2001
2002 bridge_device->ops->port_leave(bridge_device, bridge_port,
2003 mlxsw_sp_port);
2004 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2005}
2006
1b40dc3d
AS
2007static void
2008mlxsw_sp_fdb_call_notifiers(enum switchdev_notifier_type type,
2009 const char *mac, u16 vid,
2010 struct net_device *dev)
8a1ab5d7
JP
2011{
2012 struct switchdev_notifier_fdb_info info;
8a1ab5d7 2013
a989cdb4
AS
2014 info.addr = mac;
2015 info.vid = vid;
1b40dc3d 2016 call_switchdev_notifiers(type, dev, &info.info);
8a1ab5d7
JP
2017}
2018
56ade8fe
JP
2019static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
2020 char *sfn_pl, int rec_index,
2021 bool adding)
2022{
c57529e1
IS
2023 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2024 struct mlxsw_sp_bridge_device *bridge_device;
2025 struct mlxsw_sp_bridge_port *bridge_port;
56ade8fe 2026 struct mlxsw_sp_port *mlxsw_sp_port;
1b40dc3d 2027 enum switchdev_notifier_type type;
56ade8fe
JP
2028 char mac[ETH_ALEN];
2029 u8 local_port;
9de6a80e 2030 u16 vid, fid;
12f1501e 2031 bool do_notification = true;
56ade8fe
JP
2032 int err;
2033
9de6a80e 2034 mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
56ade8fe
JP
2035 mlxsw_sp_port = mlxsw_sp->ports[local_port];
2036 if (!mlxsw_sp_port) {
2037 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
12f1501e 2038 goto just_remove;
56ade8fe
JP
2039 }
2040
c57529e1
IS
2041 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2042 if (!mlxsw_sp_port_vlan) {
2043 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2044 goto just_remove;
2045 }
aac78a44 2046
c57529e1
IS
2047 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2048 if (!bridge_port) {
2049 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2050 goto just_remove;
aac78a44
IS
2051 }
2052
c57529e1
IS
2053 bridge_device = bridge_port->bridge_device;
2054 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2055
12f1501e 2056do_fdb_op:
2fa9d45e 2057 err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
12f1501e 2058 adding, true);
56ade8fe 2059 if (err) {
c0e01eac 2060 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
56ade8fe
JP
2061 return;
2062 }
2063
12f1501e
JP
2064 if (!do_notification)
2065 return;
1b40dc3d
AS
2066 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2067 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev);
a989cdb4 2068
12f1501e
JP
2069 return;
2070
2071just_remove:
2072 adding = false;
2073 do_notification = false;
2074 goto do_fdb_op;
8a1ab5d7 2075}
56ade8fe 2076
8a1ab5d7
JP
2077static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
2078 char *sfn_pl, int rec_index,
2079 bool adding)
2080{
c57529e1
IS
2081 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2082 struct mlxsw_sp_bridge_device *bridge_device;
2083 struct mlxsw_sp_bridge_port *bridge_port;
8a1ab5d7 2084 struct mlxsw_sp_port *mlxsw_sp_port;
1b40dc3d 2085 enum switchdev_notifier_type type;
8a1ab5d7 2086 char mac[ETH_ALEN];
64771e31 2087 u16 lag_vid = 0;
8a1ab5d7 2088 u16 lag_id;
9de6a80e 2089 u16 vid, fid;
12f1501e 2090 bool do_notification = true;
8a1ab5d7
JP
2091 int err;
2092
9de6a80e 2093 mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
8a1ab5d7
JP
2094 mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
2095 if (!mlxsw_sp_port) {
2096 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
12f1501e 2097 goto just_remove;
56ade8fe 2098 }
8a1ab5d7 2099
c57529e1
IS
2100 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2101 if (!mlxsw_sp_port_vlan) {
2102 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2103 goto just_remove;
2104 }
aac78a44 2105
c57529e1
IS
2106 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2107 if (!bridge_port) {
2108 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2109 goto just_remove;
aac78a44
IS
2110 }
2111
c57529e1
IS
2112 bridge_device = bridge_port->bridge_device;
2113 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2114 lag_vid = mlxsw_sp_port_vlan->vid;
2115
12f1501e 2116do_fdb_op:
64771e31 2117 err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
12f1501e 2118 adding, true);
8a1ab5d7 2119 if (err) {
c0e01eac 2120 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
8a1ab5d7
JP
2121 return;
2122 }
2123
12f1501e
JP
2124 if (!do_notification)
2125 return;
1b40dc3d
AS
2126 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2127 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev);
a989cdb4 2128
12f1501e
JP
2129 return;
2130
2131just_remove:
2132 adding = false;
2133 do_notification = false;
2134 goto do_fdb_op;
56ade8fe
JP
2135}
2136
2137static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
2138 char *sfn_pl, int rec_index)
2139{
2140 switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
2141 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
2142 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2143 rec_index, true);
2144 break;
2145 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
2146 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2147 rec_index, false);
2148 break;
8a1ab5d7
JP
2149 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
2150 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2151 rec_index, true);
2152 break;
2153 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
2154 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2155 rec_index, false);
2156 break;
56ade8fe
JP
2157 }
2158}
2159
2160static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp)
2161{
5f6935c6
IS
2162 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2163
2164 mlxsw_core_schedule_dw(&bridge->fdb_notify.dw,
2165 msecs_to_jiffies(bridge->fdb_notify.interval));
56ade8fe
JP
2166}
2167
2168static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
2169{
5f6935c6 2170 struct mlxsw_sp_bridge *bridge;
56ade8fe
JP
2171 struct mlxsw_sp *mlxsw_sp;
2172 char *sfn_pl;
2173 u8 num_rec;
2174 int i;
2175 int err;
2176
2177 sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
2178 if (!sfn_pl)
2179 return;
2180
5f6935c6
IS
2181 bridge = container_of(work, struct mlxsw_sp_bridge, fdb_notify.dw.work);
2182 mlxsw_sp = bridge->mlxsw_sp;
56ade8fe 2183
4f2c6ae5 2184 rtnl_lock();
1803e0fb
IS
2185 mlxsw_reg_sfn_pack(sfn_pl);
2186 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
2187 if (err) {
2188 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
2189 goto out;
2190 }
2191 num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
2192 for (i = 0; i < num_rec; i++)
2193 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
56ade8fe 2194
1803e0fb 2195out:
4f2c6ae5 2196 rtnl_unlock();
56ade8fe
JP
2197 kfree(sfn_pl);
2198 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2199}
2200
af061378
AS
2201struct mlxsw_sp_switchdev_event_work {
2202 struct work_struct work;
2203 struct switchdev_notifier_fdb_info fdb_info;
2204 struct net_device *dev;
2205 unsigned long event;
2206};
2207
2208static void mlxsw_sp_switchdev_event_work(struct work_struct *work)
2209{
2210 struct mlxsw_sp_switchdev_event_work *switchdev_work =
2211 container_of(work, struct mlxsw_sp_switchdev_event_work, work);
2212 struct net_device *dev = switchdev_work->dev;
2213 struct switchdev_notifier_fdb_info *fdb_info;
2214 struct mlxsw_sp_port *mlxsw_sp_port;
2215 int err;
2216
2217 rtnl_lock();
2218 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
2219 if (!mlxsw_sp_port)
2220 goto out;
2221
2222 switch (switchdev_work->event) {
2223 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2224 fdb_info = &switchdev_work->fdb_info;
2225 err = mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, true);
2226 if (err)
2227 break;
2228 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2229 fdb_info->addr,
2230 fdb_info->vid, dev);
2231 break;
2232 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2233 fdb_info = &switchdev_work->fdb_info;
2234 mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, false);
2235 break;
2236 }
2237
2238out:
2239 rtnl_unlock();
2240 kfree(switchdev_work->fdb_info.addr);
2241 kfree(switchdev_work);
2242 dev_put(dev);
2243}
2244
2245/* Called under rcu_read_lock() */
2246static int mlxsw_sp_switchdev_event(struct notifier_block *unused,
2247 unsigned long event, void *ptr)
2248{
2249 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2250 struct mlxsw_sp_switchdev_event_work *switchdev_work;
2251 struct switchdev_notifier_fdb_info *fdb_info = ptr;
2252
2253 if (!mlxsw_sp_port_dev_lower_find_rcu(dev))
2254 return NOTIFY_DONE;
2255
2256 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2257 if (!switchdev_work)
2258 return NOTIFY_BAD;
2259
2260 INIT_WORK(&switchdev_work->work, mlxsw_sp_switchdev_event_work);
2261 switchdev_work->dev = dev;
2262 switchdev_work->event = event;
2263
2264 switch (event) {
2265 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
2266 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2267 memcpy(&switchdev_work->fdb_info, ptr,
2268 sizeof(switchdev_work->fdb_info));
2269 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
6f497930
IS
2270 if (!switchdev_work->fdb_info.addr)
2271 goto err_addr_alloc;
af061378
AS
2272 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
2273 fdb_info->addr);
2274 /* Take a reference on the device. This can be either
2275 * upper device containig mlxsw_sp_port or just a
2276 * mlxsw_sp_port
2277 */
2278 dev_hold(dev);
2279 break;
2280 default:
2281 kfree(switchdev_work);
2282 return NOTIFY_DONE;
2283 }
2284
2285 mlxsw_core_schedule_work(&switchdev_work->work);
2286
2287 return NOTIFY_DONE;
6f497930
IS
2288
2289err_addr_alloc:
2290 kfree(switchdev_work);
2291 return NOTIFY_BAD;
af061378
AS
2292}
2293
2294static struct notifier_block mlxsw_sp_switchdev_notifier = {
2295 .notifier_call = mlxsw_sp_switchdev_event,
2296};
2297
56ade8fe
JP
2298static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
2299{
5f6935c6 2300 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
56ade8fe
JP
2301 int err;
2302
2303 err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
2304 if (err) {
2305 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
2306 return err;
2307 }
af061378
AS
2308
2309 err = register_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
2310 if (err) {
2311 dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev notifier\n");
2312 return err;
2313 }
2314
5f6935c6
IS
2315 INIT_DELAYED_WORK(&bridge->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
2316 bridge->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
56ade8fe
JP
2317 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2318 return 0;
2319}
2320
2321static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
2322{
5f6935c6 2323 cancel_delayed_work_sync(&mlxsw_sp->bridge->fdb_notify.dw);
af061378
AS
2324 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
2325
56ade8fe
JP
2326}
2327
56ade8fe
JP
2328int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
2329{
5f6935c6
IS
2330 struct mlxsw_sp_bridge *bridge;
2331
2332 bridge = kzalloc(sizeof(*mlxsw_sp->bridge), GFP_KERNEL);
2333 if (!bridge)
2334 return -ENOMEM;
2335 mlxsw_sp->bridge = bridge;
2336 bridge->mlxsw_sp = mlxsw_sp;
2337
c57529e1 2338 INIT_LIST_HEAD(&mlxsw_sp->bridge->bridges_list);
5f6935c6 2339
c57529e1
IS
2340 bridge->bridge_8021q_ops = &mlxsw_sp_bridge_8021q_ops;
2341 bridge->bridge_8021d_ops = &mlxsw_sp_bridge_8021d_ops;
2342
56ade8fe
JP
2343 return mlxsw_sp_fdb_init(mlxsw_sp);
2344}
2345
2346void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
2347{
2348 mlxsw_sp_fdb_fini(mlxsw_sp);
c57529e1 2349 WARN_ON(!list_empty(&mlxsw_sp->bridge->bridges_list));
5f6935c6 2350 kfree(mlxsw_sp->bridge);
56ade8fe
JP
2351}
2352
56ade8fe
JP
2353void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port *mlxsw_sp_port)
2354{
2355 mlxsw_sp_port->dev->switchdev_ops = &mlxsw_sp_port_switchdev_ops;
2356}
2357
2358void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port)
2359{
2360}