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