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