1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2015-2018 Mellanox Technologies. All rights reserved */
3
4 #include <linux/kernel.h>
5 #include <linux/types.h>
6 #include <linux/netdevice.h>
7 #include <linux/etherdevice.h>
8 #include <linux/slab.h>
9 #include <linux/device.h>
10 #include <linux/skbuff.h>
11 #include <linux/if_vlan.h>
12 #include <linux/if_bridge.h>
13 #include <linux/workqueue.h>
14 #include <linux/jiffies.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/netlink.h>
17 #include <net/switchdev.h>
18 #include <net/vxlan.h>
19
20 #include "spectrum_span.h"
21 #include "spectrum_switchdev.h"
22 #include "spectrum.h"
23 #include "core.h"
24 #include "reg.h"
25
26 struct mlxsw_sp_bridge_ops;
27
28 struct mlxsw_sp_bridge {
29 struct mlxsw_sp *mlxsw_sp;
30 struct {
31 struct delayed_work dw;
32 #define MLXSW_SP_DEFAULT_LEARNING_INTERVAL 100
33 unsigned int interval; /* ms */
34 } fdb_notify;
35 #define MLXSW_SP_MIN_AGEING_TIME 10
36 #define MLXSW_SP_MAX_AGEING_TIME 1000000
37 #define MLXSW_SP_DEFAULT_AGEING_TIME 300
38 u32 ageing_time;
39 bool vlan_enabled_exists;
40 struct list_head bridges_list;
41 DECLARE_BITMAP(mids_bitmap, MLXSW_SP_MID_MAX);
42 const struct mlxsw_sp_bridge_ops *bridge_8021q_ops;
43 const struct mlxsw_sp_bridge_ops *bridge_8021d_ops;
44 const struct mlxsw_sp_bridge_ops *bridge_8021ad_ops;
45 };
46
47 struct mlxsw_sp_bridge_device {
48 struct net_device *dev;
49 struct list_head list;
50 struct list_head ports_list;
51 struct list_head mids_list;
52 u8 vlan_enabled:1,
53 multicast_enabled:1,
54 mrouter:1;
55 const struct mlxsw_sp_bridge_ops *ops;
56 };
57
58 struct mlxsw_sp_bridge_port {
59 struct net_device *dev;
60 struct mlxsw_sp_bridge_device *bridge_device;
61 struct list_head list;
62 struct list_head vlans_list;
63 unsigned int ref_count;
64 u8 stp_state;
65 unsigned long flags;
66 bool mrouter;
67 bool lagged;
68 union {
69 u16 lag_id;
70 u16 system_port;
71 };
72 };
73
74 struct mlxsw_sp_bridge_vlan {
75 struct list_head list;
76 struct list_head port_vlan_list;
77 u16 vid;
78 };
79
80 struct mlxsw_sp_bridge_ops {
81 int (*port_join)(struct mlxsw_sp_bridge_device *bridge_device,
82 struct mlxsw_sp_bridge_port *bridge_port,
83 struct mlxsw_sp_port *mlxsw_sp_port,
84 struct netlink_ext_ack *extack);
85 void (*port_leave)(struct mlxsw_sp_bridge_device *bridge_device,
86 struct mlxsw_sp_bridge_port *bridge_port,
87 struct mlxsw_sp_port *mlxsw_sp_port);
88 int (*vxlan_join)(struct mlxsw_sp_bridge_device *bridge_device,
89 const struct net_device *vxlan_dev, u16 vid,
90 struct netlink_ext_ack *extack);
91 struct mlxsw_sp_fid *
92 (*fid_get)(struct mlxsw_sp_bridge_device *bridge_device,
93 u16 vid, struct netlink_ext_ack *extack);
94 struct mlxsw_sp_fid *
95 (*fid_lookup)(struct mlxsw_sp_bridge_device *bridge_device,
96 u16 vid);
97 u16 (*fid_vid)(struct mlxsw_sp_bridge_device *bridge_device,
98 const struct mlxsw_sp_fid *fid);
99 };
100
101 struct mlxsw_sp_switchdev_ops {
102 void (*init)(struct mlxsw_sp *mlxsw_sp);
103 };
104
105 static int
106 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
107 struct mlxsw_sp_bridge_port *bridge_port,
108 u16 fid_index);
109
110 static void
111 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
112 struct mlxsw_sp_bridge_port *bridge_port);
113
114 static void
115 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
116 struct mlxsw_sp_bridge_device
117 *bridge_device);
118
119 static void
120 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
121 struct mlxsw_sp_bridge_port *bridge_port,
122 bool add);
123
124 static struct mlxsw_sp_bridge_device *
mlxsw_sp_bridge_device_find(const struct mlxsw_sp_bridge * bridge,const struct net_device * br_dev)125 mlxsw_sp_bridge_device_find(const struct mlxsw_sp_bridge *bridge,
126 const struct net_device *br_dev)
127 {
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;
135 }
136
mlxsw_sp_bridge_device_is_offloaded(const struct mlxsw_sp * mlxsw_sp,const struct net_device * br_dev)137 bool mlxsw_sp_bridge_device_is_offloaded(const struct mlxsw_sp *mlxsw_sp,
138 const struct net_device *br_dev)
139 {
140 return !!mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
141 }
142
mlxsw_sp_bridge_device_upper_rif_destroy(struct net_device * dev,struct netdev_nested_priv * priv)143 static int mlxsw_sp_bridge_device_upper_rif_destroy(struct net_device *dev,
144 struct netdev_nested_priv *priv)
145 {
146 struct mlxsw_sp *mlxsw_sp = priv->data;
147
148 mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, dev);
149 return 0;
150 }
151
mlxsw_sp_bridge_device_rifs_destroy(struct mlxsw_sp * mlxsw_sp,struct net_device * dev)152 static void mlxsw_sp_bridge_device_rifs_destroy(struct mlxsw_sp *mlxsw_sp,
153 struct net_device *dev)
154 {
155 struct netdev_nested_priv priv = {
156 .data = (void *)mlxsw_sp,
157 };
158
159 mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, dev);
160 netdev_walk_all_upper_dev_rcu(dev,
161 mlxsw_sp_bridge_device_upper_rif_destroy,
162 &priv);
163 }
164
mlxsw_sp_bridge_device_vxlan_init(struct mlxsw_sp_bridge * bridge,struct net_device * br_dev,struct netlink_ext_ack * extack)165 static int mlxsw_sp_bridge_device_vxlan_init(struct mlxsw_sp_bridge *bridge,
166 struct net_device *br_dev,
167 struct netlink_ext_ack *extack)
168 {
169 struct net_device *dev, *stop_dev;
170 struct list_head *iter;
171 int err;
172
173 netdev_for_each_lower_dev(br_dev, dev, iter) {
174 if (netif_is_vxlan(dev) && netif_running(dev)) {
175 err = mlxsw_sp_bridge_vxlan_join(bridge->mlxsw_sp,
176 br_dev, dev, 0,
177 extack);
178 if (err) {
179 stop_dev = dev;
180 goto err_vxlan_join;
181 }
182 }
183 }
184
185 return 0;
186
187 err_vxlan_join:
188 netdev_for_each_lower_dev(br_dev, dev, iter) {
189 if (netif_is_vxlan(dev) && netif_running(dev)) {
190 if (stop_dev == dev)
191 break;
192 mlxsw_sp_bridge_vxlan_leave(bridge->mlxsw_sp, dev);
193 }
194 }
195 return err;
196 }
197
mlxsw_sp_bridge_device_vxlan_fini(struct mlxsw_sp_bridge * bridge,struct net_device * br_dev)198 static void mlxsw_sp_bridge_device_vxlan_fini(struct mlxsw_sp_bridge *bridge,
199 struct net_device *br_dev)
200 {
201 struct net_device *dev;
202 struct list_head *iter;
203
204 netdev_for_each_lower_dev(br_dev, dev, iter) {
205 if (netif_is_vxlan(dev) && netif_running(dev))
206 mlxsw_sp_bridge_vxlan_leave(bridge->mlxsw_sp, dev);
207 }
208 }
209
210 static struct mlxsw_sp_bridge_device *
mlxsw_sp_bridge_device_create(struct mlxsw_sp_bridge * bridge,struct net_device * br_dev,struct netlink_ext_ack * extack)211 mlxsw_sp_bridge_device_create(struct mlxsw_sp_bridge *bridge,
212 struct net_device *br_dev,
213 struct netlink_ext_ack *extack)
214 {
215 struct device *dev = bridge->mlxsw_sp->bus_info->dev;
216 struct mlxsw_sp_bridge_device *bridge_device;
217 bool vlan_enabled = br_vlan_enabled(br_dev);
218 int err;
219
220 if (vlan_enabled && bridge->vlan_enabled_exists) {
221 dev_err(dev, "Only one VLAN-aware bridge is supported\n");
222 NL_SET_ERR_MSG_MOD(extack, "Only one VLAN-aware bridge is supported");
223 return ERR_PTR(-EINVAL);
224 }
225
226 bridge_device = kzalloc(sizeof(*bridge_device), GFP_KERNEL);
227 if (!bridge_device)
228 return ERR_PTR(-ENOMEM);
229
230 bridge_device->dev = br_dev;
231 bridge_device->vlan_enabled = vlan_enabled;
232 bridge_device->multicast_enabled = br_multicast_enabled(br_dev);
233 bridge_device->mrouter = br_multicast_router(br_dev);
234 INIT_LIST_HEAD(&bridge_device->ports_list);
235 if (vlan_enabled) {
236 u16 proto;
237
238 bridge->vlan_enabled_exists = true;
239 br_vlan_get_proto(br_dev, &proto);
240 if (proto == ETH_P_8021AD)
241 bridge_device->ops = bridge->bridge_8021ad_ops;
242 else
243 bridge_device->ops = bridge->bridge_8021q_ops;
244 } else {
245 bridge_device->ops = bridge->bridge_8021d_ops;
246 }
247 INIT_LIST_HEAD(&bridge_device->mids_list);
248 list_add(&bridge_device->list, &bridge->bridges_list);
249
250 /* It is possible we already have VXLAN devices enslaved to the bridge.
251 * In which case, we need to replay their configuration as if they were
252 * just now enslaved to the bridge.
253 */
254 err = mlxsw_sp_bridge_device_vxlan_init(bridge, br_dev, extack);
255 if (err)
256 goto err_vxlan_init;
257
258 return bridge_device;
259
260 err_vxlan_init:
261 list_del(&bridge_device->list);
262 if (bridge_device->vlan_enabled)
263 bridge->vlan_enabled_exists = false;
264 kfree(bridge_device);
265 return ERR_PTR(err);
266 }
267
268 static void
mlxsw_sp_bridge_device_destroy(struct mlxsw_sp_bridge * bridge,struct mlxsw_sp_bridge_device * bridge_device)269 mlxsw_sp_bridge_device_destroy(struct mlxsw_sp_bridge *bridge,
270 struct mlxsw_sp_bridge_device *bridge_device)
271 {
272 mlxsw_sp_bridge_device_vxlan_fini(bridge, bridge_device->dev);
273 mlxsw_sp_bridge_device_rifs_destroy(bridge->mlxsw_sp,
274 bridge_device->dev);
275 list_del(&bridge_device->list);
276 if (bridge_device->vlan_enabled)
277 bridge->vlan_enabled_exists = false;
278 WARN_ON(!list_empty(&bridge_device->ports_list));
279 WARN_ON(!list_empty(&bridge_device->mids_list));
280 kfree(bridge_device);
281 }
282
283 static struct mlxsw_sp_bridge_device *
mlxsw_sp_bridge_device_get(struct mlxsw_sp_bridge * bridge,struct net_device * br_dev,struct netlink_ext_ack * extack)284 mlxsw_sp_bridge_device_get(struct mlxsw_sp_bridge *bridge,
285 struct net_device *br_dev,
286 struct netlink_ext_ack *extack)
287 {
288 struct mlxsw_sp_bridge_device *bridge_device;
289
290 bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
291 if (bridge_device)
292 return bridge_device;
293
294 return mlxsw_sp_bridge_device_create(bridge, br_dev, extack);
295 }
296
297 static void
mlxsw_sp_bridge_device_put(struct mlxsw_sp_bridge * bridge,struct mlxsw_sp_bridge_device * bridge_device)298 mlxsw_sp_bridge_device_put(struct mlxsw_sp_bridge *bridge,
299 struct mlxsw_sp_bridge_device *bridge_device)
300 {
301 if (list_empty(&bridge_device->ports_list))
302 mlxsw_sp_bridge_device_destroy(bridge, bridge_device);
303 }
304
305 static struct mlxsw_sp_bridge_port *
__mlxsw_sp_bridge_port_find(const struct mlxsw_sp_bridge_device * bridge_device,const struct net_device * brport_dev)306 __mlxsw_sp_bridge_port_find(const struct mlxsw_sp_bridge_device *bridge_device,
307 const struct net_device *brport_dev)
308 {
309 struct mlxsw_sp_bridge_port *bridge_port;
310
311 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
312 if (bridge_port->dev == brport_dev)
313 return bridge_port;
314 }
315
316 return NULL;
317 }
318
319 struct mlxsw_sp_bridge_port *
mlxsw_sp_bridge_port_find(struct mlxsw_sp_bridge * bridge,struct net_device * brport_dev)320 mlxsw_sp_bridge_port_find(struct mlxsw_sp_bridge *bridge,
321 struct net_device *brport_dev)
322 {
323 struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
324 struct mlxsw_sp_bridge_device *bridge_device;
325
326 if (!br_dev)
327 return NULL;
328
329 bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
330 if (!bridge_device)
331 return NULL;
332
333 return __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
334 }
335
336 static struct mlxsw_sp_bridge_port *
mlxsw_sp_bridge_port_create(struct mlxsw_sp_bridge_device * bridge_device,struct net_device * brport_dev,struct netlink_ext_ack * extack)337 mlxsw_sp_bridge_port_create(struct mlxsw_sp_bridge_device *bridge_device,
338 struct net_device *brport_dev,
339 struct netlink_ext_ack *extack)
340 {
341 struct mlxsw_sp_bridge_port *bridge_port;
342 struct mlxsw_sp_port *mlxsw_sp_port;
343 int err;
344
345 bridge_port = kzalloc(sizeof(*bridge_port), GFP_KERNEL);
346 if (!bridge_port)
347 return ERR_PTR(-ENOMEM);
348
349 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(brport_dev);
350 bridge_port->lagged = mlxsw_sp_port->lagged;
351 if (bridge_port->lagged)
352 bridge_port->lag_id = mlxsw_sp_port->lag_id;
353 else
354 bridge_port->system_port = mlxsw_sp_port->local_port;
355 bridge_port->dev = brport_dev;
356 bridge_port->bridge_device = bridge_device;
357 bridge_port->stp_state = BR_STATE_DISABLED;
358 bridge_port->flags = BR_LEARNING | BR_FLOOD | BR_LEARNING_SYNC |
359 BR_MCAST_FLOOD;
360 INIT_LIST_HEAD(&bridge_port->vlans_list);
361 list_add(&bridge_port->list, &bridge_device->ports_list);
362 bridge_port->ref_count = 1;
363
364 err = switchdev_bridge_port_offload(brport_dev, mlxsw_sp_port->dev,
365 NULL, NULL, NULL, false, extack);
366 if (err)
367 goto err_switchdev_offload;
368
369 return bridge_port;
370
371 err_switchdev_offload:
372 list_del(&bridge_port->list);
373 kfree(bridge_port);
374 return ERR_PTR(err);
375 }
376
377 static void
mlxsw_sp_bridge_port_destroy(struct mlxsw_sp_bridge_port * bridge_port)378 mlxsw_sp_bridge_port_destroy(struct mlxsw_sp_bridge_port *bridge_port)
379 {
380 switchdev_bridge_port_unoffload(bridge_port->dev, NULL, NULL, NULL);
381 list_del(&bridge_port->list);
382 WARN_ON(!list_empty(&bridge_port->vlans_list));
383 kfree(bridge_port);
384 }
385
386 static struct mlxsw_sp_bridge_port *
mlxsw_sp_bridge_port_get(struct mlxsw_sp_bridge * bridge,struct net_device * brport_dev,struct netlink_ext_ack * extack)387 mlxsw_sp_bridge_port_get(struct mlxsw_sp_bridge *bridge,
388 struct net_device *brport_dev,
389 struct netlink_ext_ack *extack)
390 {
391 struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
392 struct mlxsw_sp_bridge_device *bridge_device;
393 struct mlxsw_sp_bridge_port *bridge_port;
394 int err;
395
396 bridge_port = mlxsw_sp_bridge_port_find(bridge, brport_dev);
397 if (bridge_port) {
398 bridge_port->ref_count++;
399 return bridge_port;
400 }
401
402 bridge_device = mlxsw_sp_bridge_device_get(bridge, br_dev, extack);
403 if (IS_ERR(bridge_device))
404 return ERR_CAST(bridge_device);
405
406 bridge_port = mlxsw_sp_bridge_port_create(bridge_device, brport_dev,
407 extack);
408 if (IS_ERR(bridge_port)) {
409 err = PTR_ERR(bridge_port);
410 goto err_bridge_port_create;
411 }
412
413 return bridge_port;
414
415 err_bridge_port_create:
416 mlxsw_sp_bridge_device_put(bridge, bridge_device);
417 return ERR_PTR(err);
418 }
419
mlxsw_sp_bridge_port_put(struct mlxsw_sp_bridge * bridge,struct mlxsw_sp_bridge_port * bridge_port)420 static void mlxsw_sp_bridge_port_put(struct mlxsw_sp_bridge *bridge,
421 struct mlxsw_sp_bridge_port *bridge_port)
422 {
423 struct mlxsw_sp_bridge_device *bridge_device;
424
425 if (--bridge_port->ref_count != 0)
426 return;
427 bridge_device = bridge_port->bridge_device;
428 mlxsw_sp_bridge_port_destroy(bridge_port);
429 mlxsw_sp_bridge_device_put(bridge, bridge_device);
430 }
431
432 static struct mlxsw_sp_port_vlan *
mlxsw_sp_port_vlan_find_by_bridge(struct mlxsw_sp_port * mlxsw_sp_port,const struct mlxsw_sp_bridge_device * bridge_device,u16 vid)433 mlxsw_sp_port_vlan_find_by_bridge(struct mlxsw_sp_port *mlxsw_sp_port,
434 const struct mlxsw_sp_bridge_device *
435 bridge_device,
436 u16 vid)
437 {
438 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
439
440 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
441 list) {
442 if (!mlxsw_sp_port_vlan->bridge_port)
443 continue;
444 if (mlxsw_sp_port_vlan->bridge_port->bridge_device !=
445 bridge_device)
446 continue;
447 if (bridge_device->vlan_enabled &&
448 mlxsw_sp_port_vlan->vid != vid)
449 continue;
450 return mlxsw_sp_port_vlan;
451 }
452
453 return NULL;
454 }
455
456 static struct mlxsw_sp_port_vlan*
mlxsw_sp_port_vlan_find_by_fid(struct mlxsw_sp_port * mlxsw_sp_port,u16 fid_index)457 mlxsw_sp_port_vlan_find_by_fid(struct mlxsw_sp_port *mlxsw_sp_port,
458 u16 fid_index)
459 {
460 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
461
462 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
463 list) {
464 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
465
466 if (fid && mlxsw_sp_fid_index(fid) == fid_index)
467 return mlxsw_sp_port_vlan;
468 }
469
470 return NULL;
471 }
472
473 static struct mlxsw_sp_bridge_vlan *
mlxsw_sp_bridge_vlan_find(const struct mlxsw_sp_bridge_port * bridge_port,u16 vid)474 mlxsw_sp_bridge_vlan_find(const struct mlxsw_sp_bridge_port *bridge_port,
475 u16 vid)
476 {
477 struct mlxsw_sp_bridge_vlan *bridge_vlan;
478
479 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
480 if (bridge_vlan->vid == vid)
481 return bridge_vlan;
482 }
483
484 return NULL;
485 }
486
487 static struct mlxsw_sp_bridge_vlan *
mlxsw_sp_bridge_vlan_create(struct mlxsw_sp_bridge_port * bridge_port,u16 vid)488 mlxsw_sp_bridge_vlan_create(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
489 {
490 struct mlxsw_sp_bridge_vlan *bridge_vlan;
491
492 bridge_vlan = kzalloc(sizeof(*bridge_vlan), GFP_KERNEL);
493 if (!bridge_vlan)
494 return NULL;
495
496 INIT_LIST_HEAD(&bridge_vlan->port_vlan_list);
497 bridge_vlan->vid = vid;
498 list_add(&bridge_vlan->list, &bridge_port->vlans_list);
499
500 return bridge_vlan;
501 }
502
503 static void
mlxsw_sp_bridge_vlan_destroy(struct mlxsw_sp_bridge_vlan * bridge_vlan)504 mlxsw_sp_bridge_vlan_destroy(struct mlxsw_sp_bridge_vlan *bridge_vlan)
505 {
506 list_del(&bridge_vlan->list);
507 WARN_ON(!list_empty(&bridge_vlan->port_vlan_list));
508 kfree(bridge_vlan);
509 }
510
511 static struct mlxsw_sp_bridge_vlan *
mlxsw_sp_bridge_vlan_get(struct mlxsw_sp_bridge_port * bridge_port,u16 vid)512 mlxsw_sp_bridge_vlan_get(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
513 {
514 struct mlxsw_sp_bridge_vlan *bridge_vlan;
515
516 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
517 if (bridge_vlan)
518 return bridge_vlan;
519
520 return mlxsw_sp_bridge_vlan_create(bridge_port, vid);
521 }
522
mlxsw_sp_bridge_vlan_put(struct mlxsw_sp_bridge_vlan * bridge_vlan)523 static void mlxsw_sp_bridge_vlan_put(struct mlxsw_sp_bridge_vlan *bridge_vlan)
524 {
525 if (list_empty(&bridge_vlan->port_vlan_list))
526 mlxsw_sp_bridge_vlan_destroy(bridge_vlan);
527 }
528
529 static int
mlxsw_sp_port_bridge_vlan_stp_set(struct mlxsw_sp_port * mlxsw_sp_port,struct mlxsw_sp_bridge_vlan * bridge_vlan,u8 state)530 mlxsw_sp_port_bridge_vlan_stp_set(struct mlxsw_sp_port *mlxsw_sp_port,
531 struct mlxsw_sp_bridge_vlan *bridge_vlan,
532 u8 state)
533 {
534 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
535
536 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
537 bridge_vlan_node) {
538 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
539 continue;
540 return mlxsw_sp_port_vid_stp_set(mlxsw_sp_port,
541 bridge_vlan->vid, state);
542 }
543
544 return 0;
545 }
546
mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port * mlxsw_sp_port,struct net_device * orig_dev,u8 state)547 static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
548 struct net_device *orig_dev,
549 u8 state)
550 {
551 struct mlxsw_sp_bridge_port *bridge_port;
552 struct mlxsw_sp_bridge_vlan *bridge_vlan;
553 int err;
554
555 /* It's possible we failed to enslave the port, yet this
556 * operation is executed due to it being deferred.
557 */
558 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
559 orig_dev);
560 if (!bridge_port)
561 return 0;
562
563 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
564 err = mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port,
565 bridge_vlan, state);
566 if (err)
567 goto err_port_bridge_vlan_stp_set;
568 }
569
570 bridge_port->stp_state = state;
571
572 return 0;
573
574 err_port_bridge_vlan_stp_set:
575 list_for_each_entry_continue_reverse(bridge_vlan,
576 &bridge_port->vlans_list, list)
577 mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port, bridge_vlan,
578 bridge_port->stp_state);
579 return err;
580 }
581
582 static int
mlxsw_sp_port_bridge_vlan_flood_set(struct mlxsw_sp_port * mlxsw_sp_port,struct mlxsw_sp_bridge_vlan * bridge_vlan,enum mlxsw_sp_flood_type packet_type,bool member)583 mlxsw_sp_port_bridge_vlan_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
584 struct mlxsw_sp_bridge_vlan *bridge_vlan,
585 enum mlxsw_sp_flood_type packet_type,
586 bool member)
587 {
588 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
589
590 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
591 bridge_vlan_node) {
592 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
593 continue;
594 return mlxsw_sp_fid_flood_set(mlxsw_sp_port_vlan->fid,
595 packet_type,
596 mlxsw_sp_port->local_port,
597 member);
598 }
599
600 return 0;
601 }
602
603 static int
mlxsw_sp_bridge_port_flood_table_set(struct mlxsw_sp_port * mlxsw_sp_port,struct mlxsw_sp_bridge_port * bridge_port,enum mlxsw_sp_flood_type packet_type,bool member)604 mlxsw_sp_bridge_port_flood_table_set(struct mlxsw_sp_port *mlxsw_sp_port,
605 struct mlxsw_sp_bridge_port *bridge_port,
606 enum mlxsw_sp_flood_type packet_type,
607 bool member)
608 {
609 struct mlxsw_sp_bridge_vlan *bridge_vlan;
610 int err;
611
612 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
613 err = mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port,
614 bridge_vlan,
615 packet_type,
616 member);
617 if (err)
618 goto err_port_bridge_vlan_flood_set;
619 }
620
621 return 0;
622
623 err_port_bridge_vlan_flood_set:
624 list_for_each_entry_continue_reverse(bridge_vlan,
625 &bridge_port->vlans_list, list)
626 mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port, bridge_vlan,
627 packet_type, !member);
628 return err;
629 }
630
631 static int
mlxsw_sp_port_bridge_vlan_learning_set(struct mlxsw_sp_port * mlxsw_sp_port,struct mlxsw_sp_bridge_vlan * bridge_vlan,bool set)632 mlxsw_sp_port_bridge_vlan_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
633 struct mlxsw_sp_bridge_vlan *bridge_vlan,
634 bool set)
635 {
636 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
637 u16 vid = bridge_vlan->vid;
638
639 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
640 bridge_vlan_node) {
641 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
642 continue;
643 return mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, set);
644 }
645
646 return 0;
647 }
648
649 static int
mlxsw_sp_bridge_port_learning_set(struct mlxsw_sp_port * mlxsw_sp_port,struct mlxsw_sp_bridge_port * bridge_port,bool set)650 mlxsw_sp_bridge_port_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
651 struct mlxsw_sp_bridge_port *bridge_port,
652 bool set)
653 {
654 struct mlxsw_sp_bridge_vlan *bridge_vlan;
655 int err;
656
657 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
658 err = mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
659 bridge_vlan, set);
660 if (err)
661 goto err_port_bridge_vlan_learning_set;
662 }
663
664 return 0;
665
666 err_port_bridge_vlan_learning_set:
667 list_for_each_entry_continue_reverse(bridge_vlan,
668 &bridge_port->vlans_list, list)
669 mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
670 bridge_vlan, !set);
671 return err;
672 }
673
674 static int
mlxsw_sp_port_attr_br_pre_flags_set(struct mlxsw_sp_port * mlxsw_sp_port,struct switchdev_brport_flags flags)675 mlxsw_sp_port_attr_br_pre_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
676 struct switchdev_brport_flags flags)
677 {
678 if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD))
679 return -EINVAL;
680
681 return 0;
682 }
683
mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port * mlxsw_sp_port,struct net_device * orig_dev,struct switchdev_brport_flags flags)684 static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
685 struct net_device *orig_dev,
686 struct switchdev_brport_flags flags)
687 {
688 struct mlxsw_sp_bridge_port *bridge_port;
689 int err;
690
691 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
692 orig_dev);
693 if (!bridge_port)
694 return 0;
695
696 if (flags.mask & BR_FLOOD) {
697 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
698 bridge_port,
699 MLXSW_SP_FLOOD_TYPE_UC,
700 flags.val & BR_FLOOD);
701 if (err)
702 return err;
703 }
704
705 if (flags.mask & BR_LEARNING) {
706 err = mlxsw_sp_bridge_port_learning_set(mlxsw_sp_port,
707 bridge_port,
708 flags.val & BR_LEARNING);
709 if (err)
710 return err;
711 }
712
713 if (bridge_port->bridge_device->multicast_enabled)
714 goto out;
715
716 if (flags.mask & BR_MCAST_FLOOD) {
717 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
718 bridge_port,
719 MLXSW_SP_FLOOD_TYPE_MC,
720 flags.val & BR_MCAST_FLOOD);
721 if (err)
722 return err;
723 }
724
725 out:
726 memcpy(&bridge_port->flags, &flags.val, sizeof(flags.val));
727 return 0;
728 }
729
mlxsw_sp_ageing_set(struct mlxsw_sp * mlxsw_sp,u32 ageing_time)730 static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
731 {
732 char sfdat_pl[MLXSW_REG_SFDAT_LEN];
733 int err;
734
735 mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
736 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
737 if (err)
738 return err;
739 mlxsw_sp->bridge->ageing_time = ageing_time;
740 return 0;
741 }
742
mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port * mlxsw_sp_port,unsigned long ageing_clock_t)743 static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
744 unsigned long ageing_clock_t)
745 {
746 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
747 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
748 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
749
750 if (ageing_time < MLXSW_SP_MIN_AGEING_TIME ||
751 ageing_time > MLXSW_SP_MAX_AGEING_TIME)
752 return -ERANGE;
753
754 return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
755 }
756
mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port * mlxsw_sp_port,struct net_device * orig_dev,bool vlan_enabled)757 static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
758 struct net_device *orig_dev,
759 bool vlan_enabled)
760 {
761 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
762 struct mlxsw_sp_bridge_device *bridge_device;
763
764 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
765 if (WARN_ON(!bridge_device))
766 return -EINVAL;
767
768 if (bridge_device->vlan_enabled == vlan_enabled)
769 return 0;
770
771 netdev_err(bridge_device->dev, "VLAN filtering can't be changed for existing bridge\n");
772 return -EINVAL;
773 }
774
mlxsw_sp_port_attr_br_vlan_proto_set(struct mlxsw_sp_port * mlxsw_sp_port,struct net_device * orig_dev,u16 vlan_proto)775 static int mlxsw_sp_port_attr_br_vlan_proto_set(struct mlxsw_sp_port *mlxsw_sp_port,
776 struct net_device *orig_dev,
777 u16 vlan_proto)
778 {
779 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
780 struct mlxsw_sp_bridge_device *bridge_device;
781
782 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
783 if (WARN_ON(!bridge_device))
784 return -EINVAL;
785
786 netdev_err(bridge_device->dev, "VLAN protocol can't be changed on existing bridge\n");
787 return -EINVAL;
788 }
789
mlxsw_sp_port_attr_mrouter_set(struct mlxsw_sp_port * mlxsw_sp_port,struct net_device * orig_dev,bool is_port_mrouter)790 static int mlxsw_sp_port_attr_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
791 struct net_device *orig_dev,
792 bool is_port_mrouter)
793 {
794 struct mlxsw_sp_bridge_port *bridge_port;
795 int err;
796
797 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
798 orig_dev);
799 if (!bridge_port)
800 return 0;
801
802 if (!bridge_port->bridge_device->multicast_enabled)
803 goto out;
804
805 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
806 MLXSW_SP_FLOOD_TYPE_MC,
807 is_port_mrouter);
808 if (err)
809 return err;
810
811 mlxsw_sp_port_mrouter_update_mdb(mlxsw_sp_port, bridge_port,
812 is_port_mrouter);
813 out:
814 bridge_port->mrouter = is_port_mrouter;
815 return 0;
816 }
817
mlxsw_sp_mc_flood(const struct mlxsw_sp_bridge_port * bridge_port)818 static bool mlxsw_sp_mc_flood(const struct mlxsw_sp_bridge_port *bridge_port)
819 {
820 const struct mlxsw_sp_bridge_device *bridge_device;
821
822 bridge_device = bridge_port->bridge_device;
823 return bridge_device->multicast_enabled ? bridge_port->mrouter :
824 bridge_port->flags & BR_MCAST_FLOOD;
825 }
826
mlxsw_sp_port_mc_disabled_set(struct mlxsw_sp_port * mlxsw_sp_port,struct net_device * orig_dev,bool mc_disabled)827 static int mlxsw_sp_port_mc_disabled_set(struct mlxsw_sp_port *mlxsw_sp_port,
828 struct net_device *orig_dev,
829 bool mc_disabled)
830 {
831 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
832 struct mlxsw_sp_bridge_device *bridge_device;
833 struct mlxsw_sp_bridge_port *bridge_port;
834 int err;
835
836 /* It's possible we failed to enslave the port, yet this
837 * operation is executed due to it being deferred.
838 */
839 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
840 if (!bridge_device)
841 return 0;
842
843 if (bridge_device->multicast_enabled != !mc_disabled) {
844 bridge_device->multicast_enabled = !mc_disabled;
845 mlxsw_sp_bridge_mdb_mc_enable_sync(mlxsw_sp_port,
846 bridge_device);
847 }
848
849 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
850 enum mlxsw_sp_flood_type packet_type = MLXSW_SP_FLOOD_TYPE_MC;
851 bool member = mlxsw_sp_mc_flood(bridge_port);
852
853 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
854 bridge_port,
855 packet_type, member);
856 if (err)
857 return err;
858 }
859
860 bridge_device->multicast_enabled = !mc_disabled;
861
862 return 0;
863 }
864
mlxsw_sp_smid_router_port_set(struct mlxsw_sp * mlxsw_sp,u16 mid_idx,bool add)865 static int mlxsw_sp_smid_router_port_set(struct mlxsw_sp *mlxsw_sp,
866 u16 mid_idx, bool add)
867 {
868 char *smid_pl;
869 int err;
870
871 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
872 if (!smid_pl)
873 return -ENOMEM;
874
875 mlxsw_reg_smid_pack(smid_pl, mid_idx,
876 mlxsw_sp_router_port(mlxsw_sp), add);
877 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
878 kfree(smid_pl);
879 return err;
880 }
881
882 static void
mlxsw_sp_bridge_mrouter_update_mdb(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_bridge_device * bridge_device,bool add)883 mlxsw_sp_bridge_mrouter_update_mdb(struct mlxsw_sp *mlxsw_sp,
884 struct mlxsw_sp_bridge_device *bridge_device,
885 bool add)
886 {
887 struct mlxsw_sp_mid *mid;
888
889 list_for_each_entry(mid, &bridge_device->mids_list, list)
890 mlxsw_sp_smid_router_port_set(mlxsw_sp, mid->mid, add);
891 }
892
893 static int
mlxsw_sp_port_attr_br_mrouter_set(struct mlxsw_sp_port * mlxsw_sp_port,struct net_device * orig_dev,bool is_mrouter)894 mlxsw_sp_port_attr_br_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
895 struct net_device *orig_dev,
896 bool is_mrouter)
897 {
898 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
899 struct mlxsw_sp_bridge_device *bridge_device;
900
901 /* It's possible we failed to enslave the port, yet this
902 * operation is executed due to it being deferred.
903 */
904 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
905 if (!bridge_device)
906 return 0;
907
908 if (bridge_device->mrouter != is_mrouter)
909 mlxsw_sp_bridge_mrouter_update_mdb(mlxsw_sp, bridge_device,
910 is_mrouter);
911 bridge_device->mrouter = is_mrouter;
912 return 0;
913 }
914
mlxsw_sp_port_attr_set(struct net_device * dev,const void * ctx,const struct switchdev_attr * attr,struct netlink_ext_ack * extack)915 static int mlxsw_sp_port_attr_set(struct net_device *dev, const void *ctx,
916 const struct switchdev_attr *attr,
917 struct netlink_ext_ack *extack)
918 {
919 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
920 int err;
921
922 switch (attr->id) {
923 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
924 err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port,
925 attr->orig_dev,
926 attr->u.stp_state);
927 break;
928 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
929 err = mlxsw_sp_port_attr_br_pre_flags_set(mlxsw_sp_port,
930 attr->u.brport_flags);
931 break;
932 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
933 err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port,
934 attr->orig_dev,
935 attr->u.brport_flags);
936 break;
937 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
938 err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port,
939 attr->u.ageing_time);
940 break;
941 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
942 err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port,
943 attr->orig_dev,
944 attr->u.vlan_filtering);
945 break;
946 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL:
947 err = mlxsw_sp_port_attr_br_vlan_proto_set(mlxsw_sp_port,
948 attr->orig_dev,
949 attr->u.vlan_protocol);
950 break;
951 case SWITCHDEV_ATTR_ID_PORT_MROUTER:
952 err = mlxsw_sp_port_attr_mrouter_set(mlxsw_sp_port,
953 attr->orig_dev,
954 attr->u.mrouter);
955 break;
956 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
957 err = mlxsw_sp_port_mc_disabled_set(mlxsw_sp_port,
958 attr->orig_dev,
959 attr->u.mc_disabled);
960 break;
961 case SWITCHDEV_ATTR_ID_BRIDGE_MROUTER:
962 err = mlxsw_sp_port_attr_br_mrouter_set(mlxsw_sp_port,
963 attr->orig_dev,
964 attr->u.mrouter);
965 break;
966 default:
967 err = -EOPNOTSUPP;
968 break;
969 }
970
971 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
972
973 return err;
974 }
975
976 static int
mlxsw_sp_port_vlan_fid_join(struct mlxsw_sp_port_vlan * mlxsw_sp_port_vlan,struct mlxsw_sp_bridge_port * bridge_port,struct netlink_ext_ack * extack)977 mlxsw_sp_port_vlan_fid_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
978 struct mlxsw_sp_bridge_port *bridge_port,
979 struct netlink_ext_ack *extack)
980 {
981 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
982 struct mlxsw_sp_bridge_device *bridge_device;
983 u8 local_port = mlxsw_sp_port->local_port;
984 u16 vid = mlxsw_sp_port_vlan->vid;
985 struct mlxsw_sp_fid *fid;
986 int err;
987
988 bridge_device = bridge_port->bridge_device;
989 fid = bridge_device->ops->fid_get(bridge_device, vid, extack);
990 if (IS_ERR(fid))
991 return PTR_ERR(fid);
992
993 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port,
994 bridge_port->flags & BR_FLOOD);
995 if (err)
996 goto err_fid_uc_flood_set;
997
998 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port,
999 mlxsw_sp_mc_flood(bridge_port));
1000 if (err)
1001 goto err_fid_mc_flood_set;
1002
1003 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port,
1004 true);
1005 if (err)
1006 goto err_fid_bc_flood_set;
1007
1008 err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
1009 if (err)
1010 goto err_fid_port_vid_map;
1011
1012 mlxsw_sp_port_vlan->fid = fid;
1013
1014 return 0;
1015
1016 err_fid_port_vid_map:
1017 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
1018 err_fid_bc_flood_set:
1019 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
1020 err_fid_mc_flood_set:
1021 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
1022 err_fid_uc_flood_set:
1023 mlxsw_sp_fid_put(fid);
1024 return err;
1025 }
1026
1027 static void
mlxsw_sp_port_vlan_fid_leave(struct mlxsw_sp_port_vlan * mlxsw_sp_port_vlan)1028 mlxsw_sp_port_vlan_fid_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1029 {
1030 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1031 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
1032 u8 local_port = mlxsw_sp_port->local_port;
1033 u16 vid = mlxsw_sp_port_vlan->vid;
1034
1035 mlxsw_sp_port_vlan->fid = NULL;
1036 mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
1037 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
1038 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
1039 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
1040 mlxsw_sp_fid_put(fid);
1041 }
1042
1043 static u16
mlxsw_sp_port_pvid_determine(const struct mlxsw_sp_port * mlxsw_sp_port,u16 vid,bool is_pvid)1044 mlxsw_sp_port_pvid_determine(const struct mlxsw_sp_port *mlxsw_sp_port,
1045 u16 vid, bool is_pvid)
1046 {
1047 if (is_pvid)
1048 return vid;
1049 else if (mlxsw_sp_port->pvid == vid)
1050 return 0; /* Dis-allow untagged packets */
1051 else
1052 return mlxsw_sp_port->pvid;
1053 }
1054
1055 static int
mlxsw_sp_port_vlan_bridge_join(struct mlxsw_sp_port_vlan * mlxsw_sp_port_vlan,struct mlxsw_sp_bridge_port * bridge_port,struct netlink_ext_ack * extack)1056 mlxsw_sp_port_vlan_bridge_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
1057 struct mlxsw_sp_bridge_port *bridge_port,
1058 struct netlink_ext_ack *extack)
1059 {
1060 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1061 struct mlxsw_sp_bridge_vlan *bridge_vlan;
1062 u16 vid = mlxsw_sp_port_vlan->vid;
1063 int err;
1064
1065 /* No need to continue if only VLAN flags were changed */
1066 if (mlxsw_sp_port_vlan->bridge_port)
1067 return 0;
1068
1069 err = mlxsw_sp_port_vlan_fid_join(mlxsw_sp_port_vlan, bridge_port,
1070 extack);
1071 if (err)
1072 return err;
1073
1074 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid,
1075 bridge_port->flags & BR_LEARNING);
1076 if (err)
1077 goto err_port_vid_learning_set;
1078
1079 err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid,
1080 bridge_port->stp_state);
1081 if (err)
1082 goto err_port_vid_stp_set;
1083
1084 bridge_vlan = mlxsw_sp_bridge_vlan_get(bridge_port, vid);
1085 if (!bridge_vlan) {
1086 err = -ENOMEM;
1087 goto err_bridge_vlan_get;
1088 }
1089
1090 list_add(&mlxsw_sp_port_vlan->bridge_vlan_node,
1091 &bridge_vlan->port_vlan_list);
1092
1093 mlxsw_sp_bridge_port_get(mlxsw_sp_port->mlxsw_sp->bridge,
1094 bridge_port->dev, extack);
1095 mlxsw_sp_port_vlan->bridge_port = bridge_port;
1096
1097 return 0;
1098
1099 err_bridge_vlan_get:
1100 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1101 err_port_vid_stp_set:
1102 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1103 err_port_vid_learning_set:
1104 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1105 return err;
1106 }
1107
1108 void
mlxsw_sp_port_vlan_bridge_leave(struct mlxsw_sp_port_vlan * mlxsw_sp_port_vlan)1109 mlxsw_sp_port_vlan_bridge_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1110 {
1111 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1112 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
1113 struct mlxsw_sp_bridge_vlan *bridge_vlan;
1114 struct mlxsw_sp_bridge_port *bridge_port;
1115 u16 vid = mlxsw_sp_port_vlan->vid;
1116 bool last_port, last_vlan;
1117
1118 if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021Q &&
1119 mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021D))
1120 return;
1121
1122 bridge_port = mlxsw_sp_port_vlan->bridge_port;
1123 last_vlan = list_is_singular(&bridge_port->vlans_list);
1124 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
1125 last_port = list_is_singular(&bridge_vlan->port_vlan_list);
1126
1127 list_del(&mlxsw_sp_port_vlan->bridge_vlan_node);
1128 mlxsw_sp_bridge_vlan_put(bridge_vlan);
1129 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1130 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1131 if (last_port)
1132 mlxsw_sp_bridge_port_fdb_flush(mlxsw_sp_port->mlxsw_sp,
1133 bridge_port,
1134 mlxsw_sp_fid_index(fid));
1135 if (last_vlan)
1136 mlxsw_sp_bridge_port_mdb_flush(mlxsw_sp_port, bridge_port);
1137
1138 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1139
1140 mlxsw_sp_bridge_port_put(mlxsw_sp_port->mlxsw_sp->bridge, bridge_port);
1141 mlxsw_sp_port_vlan->bridge_port = NULL;
1142 }
1143
1144 static int
mlxsw_sp_bridge_port_vlan_add(struct mlxsw_sp_port * mlxsw_sp_port,struct mlxsw_sp_bridge_port * bridge_port,u16 vid,bool is_untagged,bool is_pvid,struct netlink_ext_ack * extack)1145 mlxsw_sp_bridge_port_vlan_add(struct mlxsw_sp_port *mlxsw_sp_port,
1146 struct mlxsw_sp_bridge_port *bridge_port,
1147 u16 vid, bool is_untagged, bool is_pvid,
1148 struct netlink_ext_ack *extack)
1149 {
1150 u16 pvid = mlxsw_sp_port_pvid_determine(mlxsw_sp_port, vid, is_pvid);
1151 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1152 u16 old_pvid = mlxsw_sp_port->pvid;
1153 u16 proto;
1154 int err;
1155
1156 /* The only valid scenario in which a port-vlan already exists, is if
1157 * the VLAN flags were changed and the port-vlan is associated with the
1158 * correct bridge port
1159 */
1160 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1161 if (mlxsw_sp_port_vlan &&
1162 mlxsw_sp_port_vlan->bridge_port != bridge_port)
1163 return -EEXIST;
1164
1165 if (!mlxsw_sp_port_vlan) {
1166 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_create(mlxsw_sp_port,
1167 vid);
1168 if (IS_ERR(mlxsw_sp_port_vlan))
1169 return PTR_ERR(mlxsw_sp_port_vlan);
1170 }
1171
1172 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true,
1173 is_untagged);
1174 if (err)
1175 goto err_port_vlan_set;
1176
1177 br_vlan_get_proto(bridge_port->bridge_device->dev, &proto);
1178 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid, proto);
1179 if (err)
1180 goto err_port_pvid_set;
1181
1182 err = mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port,
1183 extack);
1184 if (err)
1185 goto err_port_vlan_bridge_join;
1186
1187 return 0;
1188
1189 err_port_vlan_bridge_join:
1190 mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid, proto);
1191 err_port_pvid_set:
1192 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1193 err_port_vlan_set:
1194 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1195 return err;
1196 }
1197
1198 static int
mlxsw_sp_br_ban_rif_pvid_change(struct mlxsw_sp * mlxsw_sp,const struct net_device * br_dev,const struct switchdev_obj_port_vlan * vlan)1199 mlxsw_sp_br_ban_rif_pvid_change(struct mlxsw_sp *mlxsw_sp,
1200 const struct net_device *br_dev,
1201 const struct switchdev_obj_port_vlan *vlan)
1202 {
1203 u16 pvid;
1204
1205 pvid = mlxsw_sp_rif_vid(mlxsw_sp, br_dev);
1206 if (!pvid)
1207 return 0;
1208
1209 if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
1210 if (vlan->vid != pvid) {
1211 netdev_err(br_dev, "Can't change PVID, it's used by router interface\n");
1212 return -EBUSY;
1213 }
1214 } else {
1215 if (vlan->vid == pvid) {
1216 netdev_err(br_dev, "Can't remove PVID, it's used by router interface\n");
1217 return -EBUSY;
1218 }
1219 }
1220
1221 return 0;
1222 }
1223
mlxsw_sp_port_vlans_add(struct mlxsw_sp_port * mlxsw_sp_port,const struct switchdev_obj_port_vlan * vlan,struct netlink_ext_ack * extack)1224 static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
1225 const struct switchdev_obj_port_vlan *vlan,
1226 struct netlink_ext_ack *extack)
1227 {
1228 bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1229 bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1230 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1231 struct net_device *orig_dev = vlan->obj.orig_dev;
1232 struct mlxsw_sp_bridge_port *bridge_port;
1233
1234 if (netif_is_bridge_master(orig_dev)) {
1235 int err = 0;
1236
1237 if ((vlan->flags & BRIDGE_VLAN_INFO_BRENTRY) &&
1238 br_vlan_enabled(orig_dev))
1239 err = mlxsw_sp_br_ban_rif_pvid_change(mlxsw_sp,
1240 orig_dev, vlan);
1241 if (!err)
1242 err = -EOPNOTSUPP;
1243 return err;
1244 }
1245
1246 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1247 if (WARN_ON(!bridge_port))
1248 return -EINVAL;
1249
1250 if (!bridge_port->bridge_device->vlan_enabled)
1251 return 0;
1252
1253 return mlxsw_sp_bridge_port_vlan_add(mlxsw_sp_port, bridge_port,
1254 vlan->vid, flag_untagged,
1255 flag_pvid, extack);
1256 }
1257
mlxsw_sp_fdb_flush_type(bool lagged)1258 static enum mlxsw_reg_sfdf_flush_type mlxsw_sp_fdb_flush_type(bool lagged)
1259 {
1260 return lagged ? MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID :
1261 MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID;
1262 }
1263
1264 static int
mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_bridge_port * bridge_port,u16 fid_index)1265 mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
1266 struct mlxsw_sp_bridge_port *bridge_port,
1267 u16 fid_index)
1268 {
1269 bool lagged = bridge_port->lagged;
1270 char sfdf_pl[MLXSW_REG_SFDF_LEN];
1271 u16 system_port;
1272
1273 system_port = lagged ? bridge_port->lag_id : bridge_port->system_port;
1274 mlxsw_reg_sfdf_pack(sfdf_pl, mlxsw_sp_fdb_flush_type(lagged));
1275 mlxsw_reg_sfdf_fid_set(sfdf_pl, fid_index);
1276 mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl, system_port);
1277
1278 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl);
1279 }
1280
mlxsw_sp_sfd_rec_policy(bool dynamic)1281 static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
1282 {
1283 return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
1284 MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_MLAG;
1285 }
1286
mlxsw_sp_sfd_op(bool adding)1287 static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
1288 {
1289 return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
1290 MLXSW_REG_SFD_OP_WRITE_REMOVE;
1291 }
1292
mlxsw_sp_port_fdb_tunnel_uc_op(struct mlxsw_sp * mlxsw_sp,const char * mac,u16 fid,enum mlxsw_sp_l3proto proto,const union mlxsw_sp_l3addr * addr,bool adding,bool dynamic)1293 static int mlxsw_sp_port_fdb_tunnel_uc_op(struct mlxsw_sp *mlxsw_sp,
1294 const char *mac, u16 fid,
1295 enum mlxsw_sp_l3proto proto,
1296 const union mlxsw_sp_l3addr *addr,
1297 bool adding, bool dynamic)
1298 {
1299 enum mlxsw_reg_sfd_uc_tunnel_protocol sfd_proto;
1300 char *sfd_pl;
1301 u8 num_rec;
1302 u32 uip;
1303 int err;
1304
1305 switch (proto) {
1306 case MLXSW_SP_L3_PROTO_IPV4:
1307 uip = be32_to_cpu(addr->addr4);
1308 sfd_proto = MLXSW_REG_SFD_UC_TUNNEL_PROTOCOL_IPV4;
1309 break;
1310 case MLXSW_SP_L3_PROTO_IPV6:
1311 default:
1312 WARN_ON(1);
1313 return -EOPNOTSUPP;
1314 }
1315
1316 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1317 if (!sfd_pl)
1318 return -ENOMEM;
1319
1320 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1321 mlxsw_reg_sfd_uc_tunnel_pack(sfd_pl, 0,
1322 mlxsw_sp_sfd_rec_policy(dynamic), mac, fid,
1323 MLXSW_REG_SFD_REC_ACTION_NOP, uip,
1324 sfd_proto);
1325 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1326 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1327 if (err)
1328 goto out;
1329
1330 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1331 err = -EBUSY;
1332
1333 out:
1334 kfree(sfd_pl);
1335 return err;
1336 }
1337
__mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp * mlxsw_sp,u8 local_port,const char * mac,u16 fid,bool adding,enum mlxsw_reg_sfd_rec_action action,enum mlxsw_reg_sfd_rec_policy policy)1338 static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1339 const char *mac, u16 fid, bool adding,
1340 enum mlxsw_reg_sfd_rec_action action,
1341 enum mlxsw_reg_sfd_rec_policy policy)
1342 {
1343 char *sfd_pl;
1344 u8 num_rec;
1345 int err;
1346
1347 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1348 if (!sfd_pl)
1349 return -ENOMEM;
1350
1351 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1352 mlxsw_reg_sfd_uc_pack(sfd_pl, 0, policy, mac, fid, action, local_port);
1353 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1354 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1355 if (err)
1356 goto out;
1357
1358 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1359 err = -EBUSY;
1360
1361 out:
1362 kfree(sfd_pl);
1363 return err;
1364 }
1365
mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp * mlxsw_sp,u8 local_port,const char * mac,u16 fid,bool adding,bool dynamic)1366 static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1367 const char *mac, u16 fid, bool adding,
1368 bool dynamic)
1369 {
1370 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding,
1371 MLXSW_REG_SFD_REC_ACTION_NOP,
1372 mlxsw_sp_sfd_rec_policy(dynamic));
1373 }
1374
mlxsw_sp_rif_fdb_op(struct mlxsw_sp * mlxsw_sp,const char * mac,u16 fid,bool adding)1375 int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid,
1376 bool adding)
1377 {
1378 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding,
1379 MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER,
1380 MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY);
1381 }
1382
mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp * mlxsw_sp,u16 lag_id,const char * mac,u16 fid,u16 lag_vid,bool adding,bool dynamic)1383 static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
1384 const char *mac, u16 fid, u16 lag_vid,
1385 bool adding, bool dynamic)
1386 {
1387 char *sfd_pl;
1388 u8 num_rec;
1389 int err;
1390
1391 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1392 if (!sfd_pl)
1393 return -ENOMEM;
1394
1395 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1396 mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1397 mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
1398 lag_vid, lag_id);
1399 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1400 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1401 if (err)
1402 goto out;
1403
1404 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1405 err = -EBUSY;
1406
1407 out:
1408 kfree(sfd_pl);
1409 return err;
1410 }
1411
1412 static int
mlxsw_sp_port_fdb_set(struct mlxsw_sp_port * mlxsw_sp_port,struct switchdev_notifier_fdb_info * fdb_info,bool adding)1413 mlxsw_sp_port_fdb_set(struct mlxsw_sp_port *mlxsw_sp_port,
1414 struct switchdev_notifier_fdb_info *fdb_info, bool adding)
1415 {
1416 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1417 struct net_device *orig_dev = fdb_info->info.dev;
1418 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1419 struct mlxsw_sp_bridge_device *bridge_device;
1420 struct mlxsw_sp_bridge_port *bridge_port;
1421 u16 fid_index, vid;
1422
1423 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1424 if (!bridge_port)
1425 return -EINVAL;
1426
1427 bridge_device = bridge_port->bridge_device;
1428 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1429 bridge_device,
1430 fdb_info->vid);
1431 if (!mlxsw_sp_port_vlan)
1432 return 0;
1433
1434 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1435 vid = mlxsw_sp_port_vlan->vid;
1436
1437 if (!bridge_port->lagged)
1438 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp,
1439 bridge_port->system_port,
1440 fdb_info->addr, fid_index,
1441 adding, false);
1442 else
1443 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp,
1444 bridge_port->lag_id,
1445 fdb_info->addr, fid_index,
1446 vid, adding, false);
1447 }
1448
mlxsw_sp_port_mdb_op(struct mlxsw_sp * mlxsw_sp,const char * addr,u16 fid,u16 mid_idx,bool adding)1449 static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
1450 u16 fid, u16 mid_idx, bool adding)
1451 {
1452 char *sfd_pl;
1453 u8 num_rec;
1454 int err;
1455
1456 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1457 if (!sfd_pl)
1458 return -ENOMEM;
1459
1460 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1461 mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
1462 MLXSW_REG_SFD_REC_ACTION_NOP, mid_idx);
1463 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1464 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1465 if (err)
1466 goto out;
1467
1468 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1469 err = -EBUSY;
1470
1471 out:
1472 kfree(sfd_pl);
1473 return err;
1474 }
1475
mlxsw_sp_port_smid_full_entry(struct mlxsw_sp * mlxsw_sp,u16 mid_idx,long * ports_bitmap,bool set_router_port)1476 static int mlxsw_sp_port_smid_full_entry(struct mlxsw_sp *mlxsw_sp, u16 mid_idx,
1477 long *ports_bitmap,
1478 bool set_router_port)
1479 {
1480 char *smid_pl;
1481 int err, i;
1482
1483 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1484 if (!smid_pl)
1485 return -ENOMEM;
1486
1487 mlxsw_reg_smid_pack(smid_pl, mid_idx, 0, false);
1488 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++) {
1489 if (mlxsw_sp->ports[i])
1490 mlxsw_reg_smid_port_mask_set(smid_pl, i, 1);
1491 }
1492
1493 mlxsw_reg_smid_port_mask_set(smid_pl,
1494 mlxsw_sp_router_port(mlxsw_sp), 1);
1495
1496 for_each_set_bit(i, ports_bitmap, mlxsw_core_max_ports(mlxsw_sp->core))
1497 mlxsw_reg_smid_port_set(smid_pl, i, 1);
1498
1499 mlxsw_reg_smid_port_set(smid_pl, mlxsw_sp_router_port(mlxsw_sp),
1500 set_router_port);
1501
1502 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1503 kfree(smid_pl);
1504 return err;
1505 }
1506
mlxsw_sp_port_smid_set(struct mlxsw_sp_port * mlxsw_sp_port,u16 mid_idx,bool add)1507 static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port,
1508 u16 mid_idx, bool add)
1509 {
1510 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1511 char *smid_pl;
1512 int err;
1513
1514 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1515 if (!smid_pl)
1516 return -ENOMEM;
1517
1518 mlxsw_reg_smid_pack(smid_pl, mid_idx, mlxsw_sp_port->local_port, add);
1519 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1520 kfree(smid_pl);
1521 return err;
1522 }
1523
1524 static struct
__mlxsw_sp_mc_get(struct mlxsw_sp_bridge_device * bridge_device,const unsigned char * addr,u16 fid)1525 mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp_bridge_device *bridge_device,
1526 const unsigned char *addr,
1527 u16 fid)
1528 {
1529 struct mlxsw_sp_mid *mid;
1530
1531 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1532 if (ether_addr_equal(mid->addr, addr) && mid->fid == fid)
1533 return mid;
1534 }
1535 return NULL;
1536 }
1537
1538 static void
mlxsw_sp_bridge_port_get_ports_bitmap(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_bridge_port * bridge_port,unsigned long * ports_bitmap)1539 mlxsw_sp_bridge_port_get_ports_bitmap(struct mlxsw_sp *mlxsw_sp,
1540 struct mlxsw_sp_bridge_port *bridge_port,
1541 unsigned long *ports_bitmap)
1542 {
1543 struct mlxsw_sp_port *mlxsw_sp_port;
1544 u64 max_lag_members, i;
1545 int lag_id;
1546
1547 if (!bridge_port->lagged) {
1548 set_bit(bridge_port->system_port, ports_bitmap);
1549 } else {
1550 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1551 MAX_LAG_MEMBERS);
1552 lag_id = bridge_port->lag_id;
1553 for (i = 0; i < max_lag_members; i++) {
1554 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp,
1555 lag_id, i);
1556 if (mlxsw_sp_port)
1557 set_bit(mlxsw_sp_port->local_port,
1558 ports_bitmap);
1559 }
1560 }
1561 }
1562
1563 static void
mlxsw_sp_mc_get_mrouters_bitmap(unsigned long * flood_bitmap,struct mlxsw_sp_bridge_device * bridge_device,struct mlxsw_sp * mlxsw_sp)1564 mlxsw_sp_mc_get_mrouters_bitmap(unsigned long *flood_bitmap,
1565 struct mlxsw_sp_bridge_device *bridge_device,
1566 struct mlxsw_sp *mlxsw_sp)
1567 {
1568 struct mlxsw_sp_bridge_port *bridge_port;
1569
1570 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
1571 if (bridge_port->mrouter) {
1572 mlxsw_sp_bridge_port_get_ports_bitmap(mlxsw_sp,
1573 bridge_port,
1574 flood_bitmap);
1575 }
1576 }
1577 }
1578
1579 static bool
mlxsw_sp_mc_write_mdb_entry(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_mid * mid,struct mlxsw_sp_bridge_device * bridge_device)1580 mlxsw_sp_mc_write_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1581 struct mlxsw_sp_mid *mid,
1582 struct mlxsw_sp_bridge_device *bridge_device)
1583 {
1584 long *flood_bitmap;
1585 int num_of_ports;
1586 u16 mid_idx;
1587 int err;
1588
1589 mid_idx = find_first_zero_bit(mlxsw_sp->bridge->mids_bitmap,
1590 MLXSW_SP_MID_MAX);
1591 if (mid_idx == MLXSW_SP_MID_MAX)
1592 return false;
1593
1594 num_of_ports = mlxsw_core_max_ports(mlxsw_sp->core);
1595 flood_bitmap = bitmap_alloc(num_of_ports, GFP_KERNEL);
1596 if (!flood_bitmap)
1597 return false;
1598
1599 bitmap_copy(flood_bitmap, mid->ports_in_mid, num_of_ports);
1600 mlxsw_sp_mc_get_mrouters_bitmap(flood_bitmap, bridge_device, mlxsw_sp);
1601
1602 mid->mid = mid_idx;
1603 err = mlxsw_sp_port_smid_full_entry(mlxsw_sp, mid_idx, flood_bitmap,
1604 bridge_device->mrouter);
1605 bitmap_free(flood_bitmap);
1606 if (err)
1607 return false;
1608
1609 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid_idx,
1610 true);
1611 if (err)
1612 return false;
1613
1614 set_bit(mid_idx, mlxsw_sp->bridge->mids_bitmap);
1615 mid->in_hw = true;
1616 return true;
1617 }
1618
mlxsw_sp_mc_remove_mdb_entry(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_mid * mid)1619 static int mlxsw_sp_mc_remove_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1620 struct mlxsw_sp_mid *mid)
1621 {
1622 if (!mid->in_hw)
1623 return 0;
1624
1625 clear_bit(mid->mid, mlxsw_sp->bridge->mids_bitmap);
1626 mid->in_hw = false;
1627 return mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid->mid,
1628 false);
1629 }
1630
1631 static struct
__mlxsw_sp_mc_alloc(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_bridge_device * bridge_device,const unsigned char * addr,u16 fid)1632 mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
1633 struct mlxsw_sp_bridge_device *bridge_device,
1634 const unsigned char *addr,
1635 u16 fid)
1636 {
1637 struct mlxsw_sp_mid *mid;
1638
1639 mid = kzalloc(sizeof(*mid), GFP_KERNEL);
1640 if (!mid)
1641 return NULL;
1642
1643 mid->ports_in_mid = bitmap_zalloc(mlxsw_core_max_ports(mlxsw_sp->core),
1644 GFP_KERNEL);
1645 if (!mid->ports_in_mid)
1646 goto err_ports_in_mid_alloc;
1647
1648 ether_addr_copy(mid->addr, addr);
1649 mid->fid = fid;
1650 mid->in_hw = false;
1651
1652 if (!bridge_device->multicast_enabled)
1653 goto out;
1654
1655 if (!mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid, bridge_device))
1656 goto err_write_mdb_entry;
1657
1658 out:
1659 list_add_tail(&mid->list, &bridge_device->mids_list);
1660 return mid;
1661
1662 err_write_mdb_entry:
1663 bitmap_free(mid->ports_in_mid);
1664 err_ports_in_mid_alloc:
1665 kfree(mid);
1666 return NULL;
1667 }
1668
mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port * mlxsw_sp_port,struct mlxsw_sp_mid * mid)1669 static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port *mlxsw_sp_port,
1670 struct mlxsw_sp_mid *mid)
1671 {
1672 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1673 int err = 0;
1674
1675 clear_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1676 if (bitmap_empty(mid->ports_in_mid,
1677 mlxsw_core_max_ports(mlxsw_sp->core))) {
1678 err = mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1679 list_del(&mid->list);
1680 bitmap_free(mid->ports_in_mid);
1681 kfree(mid);
1682 }
1683 return err;
1684 }
1685
mlxsw_sp_port_mdb_add(struct mlxsw_sp_port * mlxsw_sp_port,const struct switchdev_obj_port_mdb * mdb)1686 static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
1687 const struct switchdev_obj_port_mdb *mdb)
1688 {
1689 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1690 struct net_device *orig_dev = mdb->obj.orig_dev;
1691 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1692 struct net_device *dev = mlxsw_sp_port->dev;
1693 struct mlxsw_sp_bridge_device *bridge_device;
1694 struct mlxsw_sp_bridge_port *bridge_port;
1695 struct mlxsw_sp_mid *mid;
1696 u16 fid_index;
1697 int err = 0;
1698
1699 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1700 if (!bridge_port)
1701 return 0;
1702
1703 bridge_device = bridge_port->bridge_device;
1704 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1705 bridge_device,
1706 mdb->vid);
1707 if (!mlxsw_sp_port_vlan)
1708 return 0;
1709
1710 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1711
1712 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1713 if (!mid) {
1714 mid = __mlxsw_sp_mc_alloc(mlxsw_sp, bridge_device, mdb->addr,
1715 fid_index);
1716 if (!mid) {
1717 netdev_err(dev, "Unable to allocate MC group\n");
1718 return -ENOMEM;
1719 }
1720 }
1721 set_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1722
1723 if (!bridge_device->multicast_enabled)
1724 return 0;
1725
1726 if (bridge_port->mrouter)
1727 return 0;
1728
1729 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true);
1730 if (err) {
1731 netdev_err(dev, "Unable to set SMID\n");
1732 goto err_out;
1733 }
1734
1735 return 0;
1736
1737 err_out:
1738 mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1739 return err;
1740 }
1741
1742 static void
mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port * mlxsw_sp_port,struct mlxsw_sp_bridge_device * bridge_device)1743 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
1744 struct mlxsw_sp_bridge_device
1745 *bridge_device)
1746 {
1747 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1748 struct mlxsw_sp_mid *mid;
1749 bool mc_enabled;
1750
1751 mc_enabled = bridge_device->multicast_enabled;
1752
1753 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1754 if (mc_enabled)
1755 mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid,
1756 bridge_device);
1757 else
1758 mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1759 }
1760 }
1761
1762 static void
mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port * mlxsw_sp_port,struct mlxsw_sp_bridge_port * bridge_port,bool add)1763 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
1764 struct mlxsw_sp_bridge_port *bridge_port,
1765 bool add)
1766 {
1767 struct mlxsw_sp_bridge_device *bridge_device;
1768 struct mlxsw_sp_mid *mid;
1769
1770 bridge_device = bridge_port->bridge_device;
1771
1772 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1773 if (!test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid))
1774 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, add);
1775 }
1776 }
1777
mlxsw_sp_port_obj_add(struct net_device * dev,const void * ctx,const struct switchdev_obj * obj,struct netlink_ext_ack * extack)1778 static int mlxsw_sp_port_obj_add(struct net_device *dev, const void *ctx,
1779 const struct switchdev_obj *obj,
1780 struct netlink_ext_ack *extack)
1781 {
1782 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1783 const struct switchdev_obj_port_vlan *vlan;
1784 int err = 0;
1785
1786 switch (obj->id) {
1787 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1788 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
1789
1790 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port, vlan, extack);
1791
1792 /* The event is emitted before the changes are actually
1793 * applied to the bridge. Therefore schedule the respin
1794 * call for later, so that the respin logic sees the
1795 * updated bridge state.
1796 */
1797 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
1798 break;
1799 case SWITCHDEV_OBJ_ID_PORT_MDB:
1800 err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
1801 SWITCHDEV_OBJ_PORT_MDB(obj));
1802 break;
1803 default:
1804 err = -EOPNOTSUPP;
1805 break;
1806 }
1807
1808 return err;
1809 }
1810
1811 static void
mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port * mlxsw_sp_port,struct mlxsw_sp_bridge_port * bridge_port,u16 vid)1812 mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port *mlxsw_sp_port,
1813 struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
1814 {
1815 u16 pvid = mlxsw_sp_port->pvid == vid ? 0 : mlxsw_sp_port->pvid;
1816 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1817 u16 proto;
1818
1819 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1820 if (WARN_ON(!mlxsw_sp_port_vlan))
1821 return;
1822
1823 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1824 br_vlan_get_proto(bridge_port->bridge_device->dev, &proto);
1825 mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid, proto);
1826 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1827 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1828 }
1829
mlxsw_sp_port_vlans_del(struct mlxsw_sp_port * mlxsw_sp_port,const struct switchdev_obj_port_vlan * vlan)1830 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1831 const struct switchdev_obj_port_vlan *vlan)
1832 {
1833 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1834 struct net_device *orig_dev = vlan->obj.orig_dev;
1835 struct mlxsw_sp_bridge_port *bridge_port;
1836
1837 if (netif_is_bridge_master(orig_dev))
1838 return -EOPNOTSUPP;
1839
1840 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1841 if (WARN_ON(!bridge_port))
1842 return -EINVAL;
1843
1844 if (!bridge_port->bridge_device->vlan_enabled)
1845 return 0;
1846
1847 mlxsw_sp_bridge_port_vlan_del(mlxsw_sp_port, bridge_port, vlan->vid);
1848
1849 return 0;
1850 }
1851
1852 static int
__mlxsw_sp_port_mdb_del(struct mlxsw_sp_port * mlxsw_sp_port,struct mlxsw_sp_bridge_port * bridge_port,struct mlxsw_sp_mid * mid)1853 __mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1854 struct mlxsw_sp_bridge_port *bridge_port,
1855 struct mlxsw_sp_mid *mid)
1856 {
1857 struct net_device *dev = mlxsw_sp_port->dev;
1858 int err;
1859
1860 if (bridge_port->bridge_device->multicast_enabled &&
1861 !bridge_port->mrouter) {
1862 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1863 if (err)
1864 netdev_err(dev, "Unable to remove port from SMID\n");
1865 }
1866
1867 err = mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1868 if (err)
1869 netdev_err(dev, "Unable to remove MC SFD\n");
1870
1871 return err;
1872 }
1873
mlxsw_sp_port_mdb_del(struct mlxsw_sp_port * mlxsw_sp_port,const struct switchdev_obj_port_mdb * mdb)1874 static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1875 const struct switchdev_obj_port_mdb *mdb)
1876 {
1877 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1878 struct net_device *orig_dev = mdb->obj.orig_dev;
1879 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1880 struct mlxsw_sp_bridge_device *bridge_device;
1881 struct net_device *dev = mlxsw_sp_port->dev;
1882 struct mlxsw_sp_bridge_port *bridge_port;
1883 struct mlxsw_sp_mid *mid;
1884 u16 fid_index;
1885
1886 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1887 if (!bridge_port)
1888 return 0;
1889
1890 bridge_device = bridge_port->bridge_device;
1891 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1892 bridge_device,
1893 mdb->vid);
1894 if (!mlxsw_sp_port_vlan)
1895 return 0;
1896
1897 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1898
1899 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1900 if (!mid) {
1901 netdev_err(dev, "Unable to remove port from MC DB\n");
1902 return -EINVAL;
1903 }
1904
1905 return __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port, mid);
1906 }
1907
1908 static void
mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port * mlxsw_sp_port,struct mlxsw_sp_bridge_port * bridge_port)1909 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
1910 struct mlxsw_sp_bridge_port *bridge_port)
1911 {
1912 struct mlxsw_sp_bridge_device *bridge_device;
1913 struct mlxsw_sp_mid *mid, *tmp;
1914
1915 bridge_device = bridge_port->bridge_device;
1916
1917 list_for_each_entry_safe(mid, tmp, &bridge_device->mids_list, list) {
1918 if (test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid)) {
1919 __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port,
1920 mid);
1921 } else if (bridge_device->multicast_enabled &&
1922 bridge_port->mrouter) {
1923 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1924 }
1925 }
1926 }
1927
mlxsw_sp_port_obj_del(struct net_device * dev,const void * ctx,const struct switchdev_obj * obj)1928 static int mlxsw_sp_port_obj_del(struct net_device *dev, const void *ctx,
1929 const struct switchdev_obj *obj)
1930 {
1931 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1932 int err = 0;
1933
1934 switch (obj->id) {
1935 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1936 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1937 SWITCHDEV_OBJ_PORT_VLAN(obj));
1938 break;
1939 case SWITCHDEV_OBJ_ID_PORT_MDB:
1940 err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1941 SWITCHDEV_OBJ_PORT_MDB(obj));
1942 break;
1943 default:
1944 err = -EOPNOTSUPP;
1945 break;
1946 }
1947
1948 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
1949
1950 return err;
1951 }
1952
mlxsw_sp_lag_rep_port(struct mlxsw_sp * mlxsw_sp,u16 lag_id)1953 static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
1954 u16 lag_id)
1955 {
1956 struct mlxsw_sp_port *mlxsw_sp_port;
1957 u64 max_lag_members;
1958 int i;
1959
1960 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1961 MAX_LAG_MEMBERS);
1962 for (i = 0; i < max_lag_members; i++) {
1963 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
1964 if (mlxsw_sp_port)
1965 return mlxsw_sp_port;
1966 }
1967 return NULL;
1968 }
1969
1970 static int
mlxsw_sp_bridge_vlan_aware_port_join(struct mlxsw_sp_bridge_port * bridge_port,struct mlxsw_sp_port * mlxsw_sp_port,struct netlink_ext_ack * extack)1971 mlxsw_sp_bridge_vlan_aware_port_join(struct mlxsw_sp_bridge_port *bridge_port,
1972 struct mlxsw_sp_port *mlxsw_sp_port,
1973 struct netlink_ext_ack *extack)
1974 {
1975 if (is_vlan_dev(bridge_port->dev)) {
1976 NL_SET_ERR_MSG_MOD(extack, "Can not enslave a VLAN device to a VLAN-aware bridge");
1977 return -EINVAL;
1978 }
1979
1980 /* Port is no longer usable as a router interface */
1981 if (mlxsw_sp_port->default_vlan->fid)
1982 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port->default_vlan);
1983
1984 return 0;
1985 }
1986
1987 static int
mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device * bridge_device,struct mlxsw_sp_bridge_port * bridge_port,struct mlxsw_sp_port * mlxsw_sp_port,struct netlink_ext_ack * extack)1988 mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1989 struct mlxsw_sp_bridge_port *bridge_port,
1990 struct mlxsw_sp_port *mlxsw_sp_port,
1991 struct netlink_ext_ack *extack)
1992 {
1993 return mlxsw_sp_bridge_vlan_aware_port_join(bridge_port, mlxsw_sp_port,
1994 extack);
1995 }
1996
1997 static void
mlxsw_sp_bridge_vlan_aware_port_leave(struct mlxsw_sp_port * mlxsw_sp_port)1998 mlxsw_sp_bridge_vlan_aware_port_leave(struct mlxsw_sp_port *mlxsw_sp_port)
1999 {
2000 /* Make sure untagged frames are allowed to ingress */
2001 mlxsw_sp_port_pvid_set(mlxsw_sp_port, MLXSW_SP_DEFAULT_VID,
2002 ETH_P_8021Q);
2003 }
2004
2005 static void
mlxsw_sp_bridge_8021q_port_leave(struct mlxsw_sp_bridge_device * bridge_device,struct mlxsw_sp_bridge_port * bridge_port,struct mlxsw_sp_port * mlxsw_sp_port)2006 mlxsw_sp_bridge_8021q_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2007 struct mlxsw_sp_bridge_port *bridge_port,
2008 struct mlxsw_sp_port *mlxsw_sp_port)
2009 {
2010 mlxsw_sp_bridge_vlan_aware_port_leave(mlxsw_sp_port);
2011 }
2012
2013 static int
mlxsw_sp_bridge_vlan_aware_vxlan_join(struct mlxsw_sp_bridge_device * bridge_device,const struct net_device * vxlan_dev,u16 vid,u16 ethertype,struct netlink_ext_ack * extack)2014 mlxsw_sp_bridge_vlan_aware_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2015 const struct net_device *vxlan_dev,
2016 u16 vid, u16 ethertype,
2017 struct netlink_ext_ack *extack)
2018 {
2019 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2020 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
2021 struct mlxsw_sp_nve_params params = {
2022 .type = MLXSW_SP_NVE_TYPE_VXLAN,
2023 .vni = vxlan->cfg.vni,
2024 .dev = vxlan_dev,
2025 .ethertype = ethertype,
2026 };
2027 struct mlxsw_sp_fid *fid;
2028 int err;
2029
2030 /* If the VLAN is 0, we need to find the VLAN that is configured as
2031 * PVID and egress untagged on the bridge port of the VxLAN device.
2032 * It is possible no such VLAN exists
2033 */
2034 if (!vid) {
2035 err = mlxsw_sp_vxlan_mapped_vid(vxlan_dev, &vid);
2036 if (err || !vid)
2037 return err;
2038 }
2039
2040 fid = mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
2041 if (IS_ERR(fid)) {
2042 NL_SET_ERR_MSG_MOD(extack, "Failed to create 802.1Q FID");
2043 return PTR_ERR(fid);
2044 }
2045
2046 if (mlxsw_sp_fid_vni_is_set(fid)) {
2047 NL_SET_ERR_MSG_MOD(extack, "VNI is already set on FID");
2048 err = -EINVAL;
2049 goto err_vni_exists;
2050 }
2051
2052 err = mlxsw_sp_nve_fid_enable(mlxsw_sp, fid, ¶ms, extack);
2053 if (err)
2054 goto err_nve_fid_enable;
2055
2056 return 0;
2057
2058 err_nve_fid_enable:
2059 err_vni_exists:
2060 mlxsw_sp_fid_put(fid);
2061 return err;
2062 }
2063
2064 static int
mlxsw_sp_bridge_8021q_vxlan_join(struct mlxsw_sp_bridge_device * bridge_device,const struct net_device * vxlan_dev,u16 vid,struct netlink_ext_ack * extack)2065 mlxsw_sp_bridge_8021q_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2066 const struct net_device *vxlan_dev, u16 vid,
2067 struct netlink_ext_ack *extack)
2068 {
2069 return mlxsw_sp_bridge_vlan_aware_vxlan_join(bridge_device, vxlan_dev,
2070 vid, ETH_P_8021Q, extack);
2071 }
2072
2073 static struct net_device *
mlxsw_sp_bridge_8021q_vxlan_dev_find(struct net_device * br_dev,u16 vid)2074 mlxsw_sp_bridge_8021q_vxlan_dev_find(struct net_device *br_dev, u16 vid)
2075 {
2076 struct net_device *dev;
2077 struct list_head *iter;
2078
2079 netdev_for_each_lower_dev(br_dev, dev, iter) {
2080 u16 pvid;
2081 int err;
2082
2083 if (!netif_is_vxlan(dev))
2084 continue;
2085
2086 err = mlxsw_sp_vxlan_mapped_vid(dev, &pvid);
2087 if (err || pvid != vid)
2088 continue;
2089
2090 return dev;
2091 }
2092
2093 return NULL;
2094 }
2095
2096 static struct mlxsw_sp_fid *
mlxsw_sp_bridge_8021q_fid_get(struct mlxsw_sp_bridge_device * bridge_device,u16 vid,struct netlink_ext_ack * extack)2097 mlxsw_sp_bridge_8021q_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
2098 u16 vid, struct netlink_ext_ack *extack)
2099 {
2100 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2101
2102 return mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
2103 }
2104
2105 static struct mlxsw_sp_fid *
mlxsw_sp_bridge_8021q_fid_lookup(struct mlxsw_sp_bridge_device * bridge_device,u16 vid)2106 mlxsw_sp_bridge_8021q_fid_lookup(struct mlxsw_sp_bridge_device *bridge_device,
2107 u16 vid)
2108 {
2109 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2110
2111 return mlxsw_sp_fid_8021q_lookup(mlxsw_sp, vid);
2112 }
2113
2114 static u16
mlxsw_sp_bridge_8021q_fid_vid(struct mlxsw_sp_bridge_device * bridge_device,const struct mlxsw_sp_fid * fid)2115 mlxsw_sp_bridge_8021q_fid_vid(struct mlxsw_sp_bridge_device *bridge_device,
2116 const struct mlxsw_sp_fid *fid)
2117 {
2118 return mlxsw_sp_fid_8021q_vid(fid);
2119 }
2120
2121 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021q_ops = {
2122 .port_join = mlxsw_sp_bridge_8021q_port_join,
2123 .port_leave = mlxsw_sp_bridge_8021q_port_leave,
2124 .vxlan_join = mlxsw_sp_bridge_8021q_vxlan_join,
2125 .fid_get = mlxsw_sp_bridge_8021q_fid_get,
2126 .fid_lookup = mlxsw_sp_bridge_8021q_fid_lookup,
2127 .fid_vid = mlxsw_sp_bridge_8021q_fid_vid,
2128 };
2129
2130 static bool
mlxsw_sp_port_is_br_member(const struct mlxsw_sp_port * mlxsw_sp_port,const struct net_device * br_dev)2131 mlxsw_sp_port_is_br_member(const struct mlxsw_sp_port *mlxsw_sp_port,
2132 const struct net_device *br_dev)
2133 {
2134 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2135
2136 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
2137 list) {
2138 if (mlxsw_sp_port_vlan->bridge_port &&
2139 mlxsw_sp_port_vlan->bridge_port->bridge_device->dev ==
2140 br_dev)
2141 return true;
2142 }
2143
2144 return false;
2145 }
2146
2147 static int
mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device * bridge_device,struct mlxsw_sp_bridge_port * bridge_port,struct mlxsw_sp_port * mlxsw_sp_port,struct netlink_ext_ack * extack)2148 mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device *bridge_device,
2149 struct mlxsw_sp_bridge_port *bridge_port,
2150 struct mlxsw_sp_port *mlxsw_sp_port,
2151 struct netlink_ext_ack *extack)
2152 {
2153 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2154 struct net_device *dev = bridge_port->dev;
2155 u16 vid;
2156
2157 vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : MLXSW_SP_DEFAULT_VID;
2158 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
2159 if (WARN_ON(!mlxsw_sp_port_vlan))
2160 return -EINVAL;
2161
2162 if (mlxsw_sp_port_is_br_member(mlxsw_sp_port, bridge_device->dev)) {
2163 NL_SET_ERR_MSG_MOD(extack, "Can not bridge VLAN uppers of the same port");
2164 return -EINVAL;
2165 }
2166
2167 /* Port is no longer usable as a router interface */
2168 if (mlxsw_sp_port_vlan->fid)
2169 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
2170
2171 return mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port,
2172 extack);
2173 }
2174
2175 static void
mlxsw_sp_bridge_8021d_port_leave(struct mlxsw_sp_bridge_device * bridge_device,struct mlxsw_sp_bridge_port * bridge_port,struct mlxsw_sp_port * mlxsw_sp_port)2176 mlxsw_sp_bridge_8021d_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2177 struct mlxsw_sp_bridge_port *bridge_port,
2178 struct mlxsw_sp_port *mlxsw_sp_port)
2179 {
2180 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2181 struct net_device *dev = bridge_port->dev;
2182 u16 vid;
2183
2184 vid = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : MLXSW_SP_DEFAULT_VID;
2185 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
2186 if (!mlxsw_sp_port_vlan || !mlxsw_sp_port_vlan->bridge_port)
2187 return;
2188
2189 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
2190 }
2191
2192 static int
mlxsw_sp_bridge_8021d_vxlan_join(struct mlxsw_sp_bridge_device * bridge_device,const struct net_device * vxlan_dev,u16 vid,struct netlink_ext_ack * extack)2193 mlxsw_sp_bridge_8021d_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2194 const struct net_device *vxlan_dev, u16 vid,
2195 struct netlink_ext_ack *extack)
2196 {
2197 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2198 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
2199 struct mlxsw_sp_nve_params params = {
2200 .type = MLXSW_SP_NVE_TYPE_VXLAN,
2201 .vni = vxlan->cfg.vni,
2202 .dev = vxlan_dev,
2203 .ethertype = ETH_P_8021Q,
2204 };
2205 struct mlxsw_sp_fid *fid;
2206 int err;
2207
2208 fid = mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
2209 if (IS_ERR(fid)) {
2210 NL_SET_ERR_MSG_MOD(extack, "Failed to create 802.1D FID");
2211 return -EINVAL;
2212 }
2213
2214 if (mlxsw_sp_fid_vni_is_set(fid)) {
2215 NL_SET_ERR_MSG_MOD(extack, "VNI is already set on FID");
2216 err = -EINVAL;
2217 goto err_vni_exists;
2218 }
2219
2220 err = mlxsw_sp_nve_fid_enable(mlxsw_sp, fid, ¶ms, extack);
2221 if (err)
2222 goto err_nve_fid_enable;
2223
2224 return 0;
2225
2226 err_nve_fid_enable:
2227 err_vni_exists:
2228 mlxsw_sp_fid_put(fid);
2229 return err;
2230 }
2231
2232 static struct mlxsw_sp_fid *
mlxsw_sp_bridge_8021d_fid_get(struct mlxsw_sp_bridge_device * bridge_device,u16 vid,struct netlink_ext_ack * extack)2233 mlxsw_sp_bridge_8021d_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
2234 u16 vid, struct netlink_ext_ack *extack)
2235 {
2236 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2237
2238 return mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
2239 }
2240
2241 static struct mlxsw_sp_fid *
mlxsw_sp_bridge_8021d_fid_lookup(struct mlxsw_sp_bridge_device * bridge_device,u16 vid)2242 mlxsw_sp_bridge_8021d_fid_lookup(struct mlxsw_sp_bridge_device *bridge_device,
2243 u16 vid)
2244 {
2245 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
2246
2247 /* The only valid VLAN for a VLAN-unaware bridge is 0 */
2248 if (vid)
2249 return NULL;
2250
2251 return mlxsw_sp_fid_8021d_lookup(mlxsw_sp, bridge_device->dev->ifindex);
2252 }
2253
2254 static u16
mlxsw_sp_bridge_8021d_fid_vid(struct mlxsw_sp_bridge_device * bridge_device,const struct mlxsw_sp_fid * fid)2255 mlxsw_sp_bridge_8021d_fid_vid(struct mlxsw_sp_bridge_device *bridge_device,
2256 const struct mlxsw_sp_fid *fid)
2257 {
2258 return 0;
2259 }
2260
2261 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021d_ops = {
2262 .port_join = mlxsw_sp_bridge_8021d_port_join,
2263 .port_leave = mlxsw_sp_bridge_8021d_port_leave,
2264 .vxlan_join = mlxsw_sp_bridge_8021d_vxlan_join,
2265 .fid_get = mlxsw_sp_bridge_8021d_fid_get,
2266 .fid_lookup = mlxsw_sp_bridge_8021d_fid_lookup,
2267 .fid_vid = mlxsw_sp_bridge_8021d_fid_vid,
2268 };
2269
2270 static int
mlxsw_sp_bridge_8021ad_port_join(struct mlxsw_sp_bridge_device * bridge_device,struct mlxsw_sp_bridge_port * bridge_port,struct mlxsw_sp_port * mlxsw_sp_port,struct netlink_ext_ack * extack)2271 mlxsw_sp_bridge_8021ad_port_join(struct mlxsw_sp_bridge_device *bridge_device,
2272 struct mlxsw_sp_bridge_port *bridge_port,
2273 struct mlxsw_sp_port *mlxsw_sp_port,
2274 struct netlink_ext_ack *extack)
2275 {
2276 int err;
2277
2278 err = mlxsw_sp_port_vlan_classification_set(mlxsw_sp_port, true, false);
2279 if (err)
2280 return err;
2281
2282 err = mlxsw_sp_bridge_vlan_aware_port_join(bridge_port, mlxsw_sp_port,
2283 extack);
2284 if (err)
2285 goto err_bridge_vlan_aware_port_join;
2286
2287 return 0;
2288
2289 err_bridge_vlan_aware_port_join:
2290 mlxsw_sp_port_vlan_classification_set(mlxsw_sp_port, false, true);
2291 return err;
2292 }
2293
2294 static void
mlxsw_sp_bridge_8021ad_port_leave(struct mlxsw_sp_bridge_device * bridge_device,struct mlxsw_sp_bridge_port * bridge_port,struct mlxsw_sp_port * mlxsw_sp_port)2295 mlxsw_sp_bridge_8021ad_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2296 struct mlxsw_sp_bridge_port *bridge_port,
2297 struct mlxsw_sp_port *mlxsw_sp_port)
2298 {
2299 mlxsw_sp_bridge_vlan_aware_port_leave(mlxsw_sp_port);
2300 mlxsw_sp_port_vlan_classification_set(mlxsw_sp_port, false, true);
2301 }
2302
2303 static int
mlxsw_sp_bridge_8021ad_vxlan_join(struct mlxsw_sp_bridge_device * bridge_device,const struct net_device * vxlan_dev,u16 vid,struct netlink_ext_ack * extack)2304 mlxsw_sp_bridge_8021ad_vxlan_join(struct mlxsw_sp_bridge_device *bridge_device,
2305 const struct net_device *vxlan_dev, u16 vid,
2306 struct netlink_ext_ack *extack)
2307 {
2308 return mlxsw_sp_bridge_vlan_aware_vxlan_join(bridge_device, vxlan_dev,
2309 vid, ETH_P_8021AD, extack);
2310 }
2311
2312 static const struct mlxsw_sp_bridge_ops mlxsw_sp1_bridge_8021ad_ops = {
2313 .port_join = mlxsw_sp_bridge_8021ad_port_join,
2314 .port_leave = mlxsw_sp_bridge_8021ad_port_leave,
2315 .vxlan_join = mlxsw_sp_bridge_8021ad_vxlan_join,
2316 .fid_get = mlxsw_sp_bridge_8021q_fid_get,
2317 .fid_lookup = mlxsw_sp_bridge_8021q_fid_lookup,
2318 .fid_vid = mlxsw_sp_bridge_8021q_fid_vid,
2319 };
2320
2321 static int
mlxsw_sp2_bridge_8021ad_port_join(struct mlxsw_sp_bridge_device * bridge_device,struct mlxsw_sp_bridge_port * bridge_port,struct mlxsw_sp_port * mlxsw_sp_port,struct netlink_ext_ack * extack)2322 mlxsw_sp2_bridge_8021ad_port_join(struct mlxsw_sp_bridge_device *bridge_device,
2323 struct mlxsw_sp_bridge_port *bridge_port,
2324 struct mlxsw_sp_port *mlxsw_sp_port,
2325 struct netlink_ext_ack *extack)
2326 {
2327 int err;
2328
2329 /* The EtherType of decapsulated packets is determined at the egress
2330 * port to allow 802.1d and 802.1ad bridges with VXLAN devices to
2331 * co-exist.
2332 */
2333 err = mlxsw_sp_port_egress_ethtype_set(mlxsw_sp_port, ETH_P_8021AD);
2334 if (err)
2335 return err;
2336
2337 err = mlxsw_sp_bridge_8021ad_port_join(bridge_device, bridge_port,
2338 mlxsw_sp_port, extack);
2339 if (err)
2340 goto err_bridge_8021ad_port_join;
2341
2342 return 0;
2343
2344 err_bridge_8021ad_port_join:
2345 mlxsw_sp_port_egress_ethtype_set(mlxsw_sp_port, ETH_P_8021Q);
2346 return err;
2347 }
2348
2349 static void
mlxsw_sp2_bridge_8021ad_port_leave(struct mlxsw_sp_bridge_device * bridge_device,struct mlxsw_sp_bridge_port * bridge_port,struct mlxsw_sp_port * mlxsw_sp_port)2350 mlxsw_sp2_bridge_8021ad_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
2351 struct mlxsw_sp_bridge_port *bridge_port,
2352 struct mlxsw_sp_port *mlxsw_sp_port)
2353 {
2354 mlxsw_sp_bridge_8021ad_port_leave(bridge_device, bridge_port,
2355 mlxsw_sp_port);
2356 mlxsw_sp_port_egress_ethtype_set(mlxsw_sp_port, ETH_P_8021Q);
2357 }
2358
2359 static const struct mlxsw_sp_bridge_ops mlxsw_sp2_bridge_8021ad_ops = {
2360 .port_join = mlxsw_sp2_bridge_8021ad_port_join,
2361 .port_leave = mlxsw_sp2_bridge_8021ad_port_leave,
2362 .vxlan_join = mlxsw_sp_bridge_8021ad_vxlan_join,
2363 .fid_get = mlxsw_sp_bridge_8021q_fid_get,
2364 .fid_lookup = mlxsw_sp_bridge_8021q_fid_lookup,
2365 .fid_vid = mlxsw_sp_bridge_8021q_fid_vid,
2366 };
2367
mlxsw_sp_port_bridge_join(struct mlxsw_sp_port * mlxsw_sp_port,struct net_device * brport_dev,struct net_device * br_dev,struct netlink_ext_ack * extack)2368 int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port,
2369 struct net_device *brport_dev,
2370 struct net_device *br_dev,
2371 struct netlink_ext_ack *extack)
2372 {
2373 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2374 struct mlxsw_sp_bridge_device *bridge_device;
2375 struct mlxsw_sp_bridge_port *bridge_port;
2376 int err;
2377
2378 bridge_port = mlxsw_sp_bridge_port_get(mlxsw_sp->bridge, brport_dev,
2379 extack);
2380 if (IS_ERR(bridge_port))
2381 return PTR_ERR(bridge_port);
2382 bridge_device = bridge_port->bridge_device;
2383
2384 err = bridge_device->ops->port_join(bridge_device, bridge_port,
2385 mlxsw_sp_port, extack);
2386 if (err)
2387 goto err_port_join;
2388
2389 return 0;
2390
2391 err_port_join:
2392 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2393 return err;
2394 }
2395
mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port * mlxsw_sp_port,struct net_device * brport_dev,struct net_device * br_dev)2396 void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port,
2397 struct net_device *brport_dev,
2398 struct net_device *br_dev)
2399 {
2400 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2401 struct mlxsw_sp_bridge_device *bridge_device;
2402 struct mlxsw_sp_bridge_port *bridge_port;
2403
2404 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2405 if (!bridge_device)
2406 return;
2407 bridge_port = __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
2408 if (!bridge_port)
2409 return;
2410
2411 bridge_device->ops->port_leave(bridge_device, bridge_port,
2412 mlxsw_sp_port);
2413 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2414 }
2415
mlxsw_sp_bridge_vxlan_join(struct mlxsw_sp * mlxsw_sp,const struct net_device * br_dev,const struct net_device * vxlan_dev,u16 vid,struct netlink_ext_ack * extack)2416 int mlxsw_sp_bridge_vxlan_join(struct mlxsw_sp *mlxsw_sp,
2417 const struct net_device *br_dev,
2418 const struct net_device *vxlan_dev, u16 vid,
2419 struct netlink_ext_ack *extack)
2420 {
2421 struct mlxsw_sp_bridge_device *bridge_device;
2422
2423 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2424 if (WARN_ON(!bridge_device))
2425 return -EINVAL;
2426
2427 return bridge_device->ops->vxlan_join(bridge_device, vxlan_dev, vid,
2428 extack);
2429 }
2430
mlxsw_sp_bridge_vxlan_leave(struct mlxsw_sp * mlxsw_sp,const struct net_device * vxlan_dev)2431 void mlxsw_sp_bridge_vxlan_leave(struct mlxsw_sp *mlxsw_sp,
2432 const struct net_device *vxlan_dev)
2433 {
2434 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
2435 struct mlxsw_sp_fid *fid;
2436
2437 /* If the VxLAN device is down, then the FID does not have a VNI */
2438 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan->cfg.vni);
2439 if (!fid)
2440 return;
2441
2442 mlxsw_sp_nve_fid_disable(mlxsw_sp, fid);
2443 /* Drop both the reference we just took during lookup and the reference
2444 * the VXLAN device took.
2445 */
2446 mlxsw_sp_fid_put(fid);
2447 mlxsw_sp_fid_put(fid);
2448 }
2449
2450 static void
mlxsw_sp_switchdev_vxlan_addr_convert(const union vxlan_addr * vxlan_addr,enum mlxsw_sp_l3proto * proto,union mlxsw_sp_l3addr * addr)2451 mlxsw_sp_switchdev_vxlan_addr_convert(const union vxlan_addr *vxlan_addr,
2452 enum mlxsw_sp_l3proto *proto,
2453 union mlxsw_sp_l3addr *addr)
2454 {
2455 if (vxlan_addr->sa.sa_family == AF_INET) {
2456 addr->addr4 = vxlan_addr->sin.sin_addr.s_addr;
2457 *proto = MLXSW_SP_L3_PROTO_IPV4;
2458 } else {
2459 addr->addr6 = vxlan_addr->sin6.sin6_addr;
2460 *proto = MLXSW_SP_L3_PROTO_IPV6;
2461 }
2462 }
2463
2464 static void
mlxsw_sp_switchdev_addr_vxlan_convert(enum mlxsw_sp_l3proto proto,const union mlxsw_sp_l3addr * addr,union vxlan_addr * vxlan_addr)2465 mlxsw_sp_switchdev_addr_vxlan_convert(enum mlxsw_sp_l3proto proto,
2466 const union mlxsw_sp_l3addr *addr,
2467 union vxlan_addr *vxlan_addr)
2468 {
2469 switch (proto) {
2470 case MLXSW_SP_L3_PROTO_IPV4:
2471 vxlan_addr->sa.sa_family = AF_INET;
2472 vxlan_addr->sin.sin_addr.s_addr = addr->addr4;
2473 break;
2474 case MLXSW_SP_L3_PROTO_IPV6:
2475 vxlan_addr->sa.sa_family = AF_INET6;
2476 vxlan_addr->sin6.sin6_addr = addr->addr6;
2477 break;
2478 }
2479 }
2480
mlxsw_sp_fdb_vxlan_call_notifiers(struct net_device * dev,const char * mac,enum mlxsw_sp_l3proto proto,union mlxsw_sp_l3addr * addr,__be32 vni,bool adding)2481 static void mlxsw_sp_fdb_vxlan_call_notifiers(struct net_device *dev,
2482 const char *mac,
2483 enum mlxsw_sp_l3proto proto,
2484 union mlxsw_sp_l3addr *addr,
2485 __be32 vni, bool adding)
2486 {
2487 struct switchdev_notifier_vxlan_fdb_info info;
2488 struct vxlan_dev *vxlan = netdev_priv(dev);
2489 enum switchdev_notifier_type type;
2490
2491 type = adding ? SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE :
2492 SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE;
2493 mlxsw_sp_switchdev_addr_vxlan_convert(proto, addr, &info.remote_ip);
2494 info.remote_port = vxlan->cfg.dst_port;
2495 info.remote_vni = vni;
2496 info.remote_ifindex = 0;
2497 ether_addr_copy(info.eth_addr, mac);
2498 info.vni = vni;
2499 info.offloaded = adding;
2500 call_switchdev_notifiers(type, dev, &info.info, NULL);
2501 }
2502
mlxsw_sp_fdb_nve_call_notifiers(struct net_device * dev,const char * mac,enum mlxsw_sp_l3proto proto,union mlxsw_sp_l3addr * addr,__be32 vni,bool adding)2503 static void mlxsw_sp_fdb_nve_call_notifiers(struct net_device *dev,
2504 const char *mac,
2505 enum mlxsw_sp_l3proto proto,
2506 union mlxsw_sp_l3addr *addr,
2507 __be32 vni,
2508 bool adding)
2509 {
2510 if (netif_is_vxlan(dev))
2511 mlxsw_sp_fdb_vxlan_call_notifiers(dev, mac, proto, addr, vni,
2512 adding);
2513 }
2514
2515 static void
mlxsw_sp_fdb_call_notifiers(enum switchdev_notifier_type type,const char * mac,u16 vid,struct net_device * dev,bool offloaded)2516 mlxsw_sp_fdb_call_notifiers(enum switchdev_notifier_type type,
2517 const char *mac, u16 vid,
2518 struct net_device *dev, bool offloaded)
2519 {
2520 struct switchdev_notifier_fdb_info info = {};
2521
2522 info.addr = mac;
2523 info.vid = vid;
2524 info.offloaded = offloaded;
2525 call_switchdev_notifiers(type, dev, &info.info, NULL);
2526 }
2527
mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp * mlxsw_sp,char * sfn_pl,int rec_index,bool adding)2528 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
2529 char *sfn_pl, int rec_index,
2530 bool adding)
2531 {
2532 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
2533 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2534 struct mlxsw_sp_bridge_device *bridge_device;
2535 struct mlxsw_sp_bridge_port *bridge_port;
2536 struct mlxsw_sp_port *mlxsw_sp_port;
2537 enum switchdev_notifier_type type;
2538 char mac[ETH_ALEN];
2539 u8 local_port;
2540 u16 vid, fid;
2541 bool do_notification = true;
2542 int err;
2543
2544 mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
2545
2546 if (WARN_ON_ONCE(local_port >= max_ports))
2547 return;
2548 mlxsw_sp_port = mlxsw_sp->ports[local_port];
2549 if (!mlxsw_sp_port) {
2550 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
2551 goto just_remove;
2552 }
2553
2554 if (mlxsw_sp_fid_is_dummy(mlxsw_sp, fid))
2555 goto just_remove;
2556
2557 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2558 if (!mlxsw_sp_port_vlan) {
2559 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2560 goto just_remove;
2561 }
2562
2563 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2564 if (!bridge_port) {
2565 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2566 goto just_remove;
2567 }
2568
2569 bridge_device = bridge_port->bridge_device;
2570 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2571
2572 do_fdb_op:
2573 err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
2574 adding, true);
2575 if (err) {
2576 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2577 return;
2578 }
2579
2580 if (!do_notification)
2581 return;
2582 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2583 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev, adding);
2584
2585 return;
2586
2587 just_remove:
2588 adding = false;
2589 do_notification = false;
2590 goto do_fdb_op;
2591 }
2592
mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp * mlxsw_sp,char * sfn_pl,int rec_index,bool adding)2593 static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
2594 char *sfn_pl, int rec_index,
2595 bool adding)
2596 {
2597 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2598 struct mlxsw_sp_bridge_device *bridge_device;
2599 struct mlxsw_sp_bridge_port *bridge_port;
2600 struct mlxsw_sp_port *mlxsw_sp_port;
2601 enum switchdev_notifier_type type;
2602 char mac[ETH_ALEN];
2603 u16 lag_vid = 0;
2604 u16 lag_id;
2605 u16 vid, fid;
2606 bool do_notification = true;
2607 int err;
2608
2609 mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
2610 mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
2611 if (!mlxsw_sp_port) {
2612 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
2613 goto just_remove;
2614 }
2615
2616 if (mlxsw_sp_fid_is_dummy(mlxsw_sp, fid))
2617 goto just_remove;
2618
2619 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2620 if (!mlxsw_sp_port_vlan) {
2621 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2622 goto just_remove;
2623 }
2624
2625 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2626 if (!bridge_port) {
2627 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2628 goto just_remove;
2629 }
2630
2631 bridge_device = bridge_port->bridge_device;
2632 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2633 lag_vid = mlxsw_sp_fid_lag_vid_valid(mlxsw_sp_port_vlan->fid) ?
2634 mlxsw_sp_port_vlan->vid : 0;
2635
2636 do_fdb_op:
2637 err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
2638 adding, true);
2639 if (err) {
2640 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2641 return;
2642 }
2643
2644 if (!do_notification)
2645 return;
2646 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2647 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev, adding);
2648
2649 return;
2650
2651 just_remove:
2652 adding = false;
2653 do_notification = false;
2654 goto do_fdb_op;
2655 }
2656
2657 static int
__mlxsw_sp_fdb_notify_mac_uc_tunnel_process(struct mlxsw_sp * mlxsw_sp,const struct mlxsw_sp_fid * fid,bool adding,struct net_device ** nve_dev,u16 * p_vid,__be32 * p_vni)2658 __mlxsw_sp_fdb_notify_mac_uc_tunnel_process(struct mlxsw_sp *mlxsw_sp,
2659 const struct mlxsw_sp_fid *fid,
2660 bool adding,
2661 struct net_device **nve_dev,
2662 u16 *p_vid, __be32 *p_vni)
2663 {
2664 struct mlxsw_sp_bridge_device *bridge_device;
2665 struct net_device *br_dev, *dev;
2666 int nve_ifindex;
2667 int err;
2668
2669 err = mlxsw_sp_fid_nve_ifindex(fid, &nve_ifindex);
2670 if (err)
2671 return err;
2672
2673 err = mlxsw_sp_fid_vni(fid, p_vni);
2674 if (err)
2675 return err;
2676
2677 dev = __dev_get_by_index(mlxsw_sp_net(mlxsw_sp), nve_ifindex);
2678 if (!dev)
2679 return -EINVAL;
2680 *nve_dev = dev;
2681
2682 if (!netif_running(dev))
2683 return -EINVAL;
2684
2685 if (adding && !br_port_flag_is_set(dev, BR_LEARNING))
2686 return -EINVAL;
2687
2688 if (adding && netif_is_vxlan(dev)) {
2689 struct vxlan_dev *vxlan = netdev_priv(dev);
2690
2691 if (!(vxlan->cfg.flags & VXLAN_F_LEARN))
2692 return -EINVAL;
2693 }
2694
2695 br_dev = netdev_master_upper_dev_get(dev);
2696 if (!br_dev)
2697 return -EINVAL;
2698
2699 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2700 if (!bridge_device)
2701 return -EINVAL;
2702
2703 *p_vid = bridge_device->ops->fid_vid(bridge_device, fid);
2704
2705 return 0;
2706 }
2707
mlxsw_sp_fdb_notify_mac_uc_tunnel_process(struct mlxsw_sp * mlxsw_sp,char * sfn_pl,int rec_index,bool adding)2708 static void mlxsw_sp_fdb_notify_mac_uc_tunnel_process(struct mlxsw_sp *mlxsw_sp,
2709 char *sfn_pl,
2710 int rec_index,
2711 bool adding)
2712 {
2713 enum mlxsw_reg_sfn_uc_tunnel_protocol sfn_proto;
2714 enum switchdev_notifier_type type;
2715 struct net_device *nve_dev;
2716 union mlxsw_sp_l3addr addr;
2717 struct mlxsw_sp_fid *fid;
2718 char mac[ETH_ALEN];
2719 u16 fid_index, vid;
2720 __be32 vni;
2721 u32 uip;
2722 int err;
2723
2724 mlxsw_reg_sfn_uc_tunnel_unpack(sfn_pl, rec_index, mac, &fid_index,
2725 &uip, &sfn_proto);
2726
2727 fid = mlxsw_sp_fid_lookup_by_index(mlxsw_sp, fid_index);
2728 if (!fid)
2729 goto err_fid_lookup;
2730
2731 err = mlxsw_sp_nve_learned_ip_resolve(mlxsw_sp, uip,
2732 (enum mlxsw_sp_l3proto) sfn_proto,
2733 &addr);
2734 if (err)
2735 goto err_ip_resolve;
2736
2737 err = __mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp, fid, adding,
2738 &nve_dev, &vid, &vni);
2739 if (err)
2740 goto err_fdb_process;
2741
2742 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, mac, fid_index,
2743 (enum mlxsw_sp_l3proto) sfn_proto,
2744 &addr, adding, true);
2745 if (err)
2746 goto err_fdb_op;
2747
2748 mlxsw_sp_fdb_nve_call_notifiers(nve_dev, mac,
2749 (enum mlxsw_sp_l3proto) sfn_proto,
2750 &addr, vni, adding);
2751
2752 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE :
2753 SWITCHDEV_FDB_DEL_TO_BRIDGE;
2754 mlxsw_sp_fdb_call_notifiers(type, mac, vid, nve_dev, adding);
2755
2756 mlxsw_sp_fid_put(fid);
2757
2758 return;
2759
2760 err_fdb_op:
2761 err_fdb_process:
2762 err_ip_resolve:
2763 mlxsw_sp_fid_put(fid);
2764 err_fid_lookup:
2765 /* Remove an FDB entry in case we cannot process it. Otherwise the
2766 * device will keep sending the same notification over and over again.
2767 */
2768 mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, mac, fid_index,
2769 (enum mlxsw_sp_l3proto) sfn_proto, &addr,
2770 false, true);
2771 }
2772
mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp * mlxsw_sp,char * sfn_pl,int rec_index)2773 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
2774 char *sfn_pl, int rec_index)
2775 {
2776 switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
2777 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
2778 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2779 rec_index, true);
2780 break;
2781 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
2782 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2783 rec_index, false);
2784 break;
2785 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
2786 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2787 rec_index, true);
2788 break;
2789 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
2790 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2791 rec_index, false);
2792 break;
2793 case MLXSW_REG_SFN_REC_TYPE_LEARNED_UNICAST_TUNNEL:
2794 mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp, sfn_pl,
2795 rec_index, true);
2796 break;
2797 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_UNICAST_TUNNEL:
2798 mlxsw_sp_fdb_notify_mac_uc_tunnel_process(mlxsw_sp, sfn_pl,
2799 rec_index, false);
2800 break;
2801 }
2802 }
2803
mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp * mlxsw_sp,bool no_delay)2804 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp,
2805 bool no_delay)
2806 {
2807 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2808 unsigned int interval = no_delay ? 0 : bridge->fdb_notify.interval;
2809
2810 mlxsw_core_schedule_dw(&bridge->fdb_notify.dw,
2811 msecs_to_jiffies(interval));
2812 }
2813
2814 #define MLXSW_SP_FDB_SFN_QUERIES_PER_SESSION 10
2815
mlxsw_sp_fdb_notify_work(struct work_struct * work)2816 static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
2817 {
2818 struct mlxsw_sp_bridge *bridge;
2819 struct mlxsw_sp *mlxsw_sp;
2820 char *sfn_pl;
2821 int queries;
2822 u8 num_rec;
2823 int i;
2824 int err;
2825
2826 sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
2827 if (!sfn_pl)
2828 return;
2829
2830 bridge = container_of(work, struct mlxsw_sp_bridge, fdb_notify.dw.work);
2831 mlxsw_sp = bridge->mlxsw_sp;
2832
2833 rtnl_lock();
2834 queries = MLXSW_SP_FDB_SFN_QUERIES_PER_SESSION;
2835 while (queries > 0) {
2836 mlxsw_reg_sfn_pack(sfn_pl);
2837 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
2838 if (err) {
2839 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
2840 goto out;
2841 }
2842 num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
2843 for (i = 0; i < num_rec; i++)
2844 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
2845 if (num_rec != MLXSW_REG_SFN_REC_MAX_COUNT)
2846 goto out;
2847 queries--;
2848 }
2849
2850 out:
2851 rtnl_unlock();
2852 kfree(sfn_pl);
2853 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp, !queries);
2854 }
2855
2856 struct mlxsw_sp_switchdev_event_work {
2857 struct work_struct work;
2858 union {
2859 struct switchdev_notifier_fdb_info fdb_info;
2860 struct switchdev_notifier_vxlan_fdb_info vxlan_fdb_info;
2861 };
2862 struct net_device *dev;
2863 unsigned long event;
2864 };
2865
2866 static void
mlxsw_sp_switchdev_bridge_vxlan_fdb_event(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_switchdev_event_work * switchdev_work,struct mlxsw_sp_fid * fid,__be32 vni)2867 mlxsw_sp_switchdev_bridge_vxlan_fdb_event(struct mlxsw_sp *mlxsw_sp,
2868 struct mlxsw_sp_switchdev_event_work *
2869 switchdev_work,
2870 struct mlxsw_sp_fid *fid, __be32 vni)
2871 {
2872 struct switchdev_notifier_vxlan_fdb_info vxlan_fdb_info;
2873 struct switchdev_notifier_fdb_info *fdb_info;
2874 struct net_device *dev = switchdev_work->dev;
2875 enum mlxsw_sp_l3proto proto;
2876 union mlxsw_sp_l3addr addr;
2877 int err;
2878
2879 fdb_info = &switchdev_work->fdb_info;
2880 err = vxlan_fdb_find_uc(dev, fdb_info->addr, vni, &vxlan_fdb_info);
2881 if (err)
2882 return;
2883
2884 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info.remote_ip,
2885 &proto, &addr);
2886
2887 switch (switchdev_work->event) {
2888 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2889 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp,
2890 vxlan_fdb_info.eth_addr,
2891 mlxsw_sp_fid_index(fid),
2892 proto, &addr, true, false);
2893 if (err)
2894 return;
2895 vxlan_fdb_info.offloaded = true;
2896 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2897 &vxlan_fdb_info.info, NULL);
2898 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2899 vxlan_fdb_info.eth_addr,
2900 fdb_info->vid, dev, true);
2901 break;
2902 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2903 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp,
2904 vxlan_fdb_info.eth_addr,
2905 mlxsw_sp_fid_index(fid),
2906 proto, &addr, false,
2907 false);
2908 vxlan_fdb_info.offloaded = false;
2909 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
2910 &vxlan_fdb_info.info, NULL);
2911 break;
2912 }
2913 }
2914
2915 static void
mlxsw_sp_switchdev_bridge_nve_fdb_event(struct mlxsw_sp_switchdev_event_work * switchdev_work)2916 mlxsw_sp_switchdev_bridge_nve_fdb_event(struct mlxsw_sp_switchdev_event_work *
2917 switchdev_work)
2918 {
2919 struct mlxsw_sp_bridge_device *bridge_device;
2920 struct net_device *dev = switchdev_work->dev;
2921 struct net_device *br_dev;
2922 struct mlxsw_sp *mlxsw_sp;
2923 struct mlxsw_sp_fid *fid;
2924 __be32 vni;
2925 int err;
2926
2927 if (switchdev_work->event != SWITCHDEV_FDB_ADD_TO_DEVICE &&
2928 switchdev_work->event != SWITCHDEV_FDB_DEL_TO_DEVICE)
2929 return;
2930
2931 if (switchdev_work->event == SWITCHDEV_FDB_ADD_TO_DEVICE &&
2932 (!switchdev_work->fdb_info.added_by_user ||
2933 switchdev_work->fdb_info.is_local))
2934 return;
2935
2936 if (!netif_running(dev))
2937 return;
2938 br_dev = netdev_master_upper_dev_get(dev);
2939 if (!br_dev)
2940 return;
2941 if (!netif_is_bridge_master(br_dev))
2942 return;
2943 mlxsw_sp = mlxsw_sp_lower_get(br_dev);
2944 if (!mlxsw_sp)
2945 return;
2946 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2947 if (!bridge_device)
2948 return;
2949
2950 fid = bridge_device->ops->fid_lookup(bridge_device,
2951 switchdev_work->fdb_info.vid);
2952 if (!fid)
2953 return;
2954
2955 err = mlxsw_sp_fid_vni(fid, &vni);
2956 if (err)
2957 goto out;
2958
2959 mlxsw_sp_switchdev_bridge_vxlan_fdb_event(mlxsw_sp, switchdev_work, fid,
2960 vni);
2961
2962 out:
2963 mlxsw_sp_fid_put(fid);
2964 }
2965
mlxsw_sp_switchdev_bridge_fdb_event_work(struct work_struct * work)2966 static void mlxsw_sp_switchdev_bridge_fdb_event_work(struct work_struct *work)
2967 {
2968 struct mlxsw_sp_switchdev_event_work *switchdev_work =
2969 container_of(work, struct mlxsw_sp_switchdev_event_work, work);
2970 struct net_device *dev = switchdev_work->dev;
2971 struct switchdev_notifier_fdb_info *fdb_info;
2972 struct mlxsw_sp_port *mlxsw_sp_port;
2973 int err;
2974
2975 rtnl_lock();
2976 if (netif_is_vxlan(dev)) {
2977 mlxsw_sp_switchdev_bridge_nve_fdb_event(switchdev_work);
2978 goto out;
2979 }
2980
2981 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
2982 if (!mlxsw_sp_port)
2983 goto out;
2984
2985 switch (switchdev_work->event) {
2986 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2987 fdb_info = &switchdev_work->fdb_info;
2988 if (!fdb_info->added_by_user || fdb_info->is_local)
2989 break;
2990 err = mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, true);
2991 if (err)
2992 break;
2993 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2994 fdb_info->addr,
2995 fdb_info->vid, dev, true);
2996 break;
2997 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2998 fdb_info = &switchdev_work->fdb_info;
2999 mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, false);
3000 break;
3001 case SWITCHDEV_FDB_ADD_TO_BRIDGE:
3002 case SWITCHDEV_FDB_DEL_TO_BRIDGE:
3003 /* These events are only used to potentially update an existing
3004 * SPAN mirror.
3005 */
3006 break;
3007 }
3008
3009 mlxsw_sp_span_respin(mlxsw_sp_port->mlxsw_sp);
3010
3011 out:
3012 rtnl_unlock();
3013 kfree(switchdev_work->fdb_info.addr);
3014 kfree(switchdev_work);
3015 dev_put(dev);
3016 }
3017
3018 static void
mlxsw_sp_switchdev_vxlan_fdb_add(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_switchdev_event_work * switchdev_work)3019 mlxsw_sp_switchdev_vxlan_fdb_add(struct mlxsw_sp *mlxsw_sp,
3020 struct mlxsw_sp_switchdev_event_work *
3021 switchdev_work)
3022 {
3023 struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
3024 struct mlxsw_sp_bridge_device *bridge_device;
3025 struct net_device *dev = switchdev_work->dev;
3026 u8 all_zeros_mac[ETH_ALEN] = { 0 };
3027 enum mlxsw_sp_l3proto proto;
3028 union mlxsw_sp_l3addr addr;
3029 struct net_device *br_dev;
3030 struct mlxsw_sp_fid *fid;
3031 u16 vid;
3032 int err;
3033
3034 vxlan_fdb_info = &switchdev_work->vxlan_fdb_info;
3035 br_dev = netdev_master_upper_dev_get(dev);
3036
3037 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3038 if (!bridge_device)
3039 return;
3040
3041 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan_fdb_info->vni);
3042 if (!fid)
3043 return;
3044
3045 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info->remote_ip,
3046 &proto, &addr);
3047
3048 if (ether_addr_equal(vxlan_fdb_info->eth_addr, all_zeros_mac)) {
3049 err = mlxsw_sp_nve_flood_ip_add(mlxsw_sp, fid, proto, &addr);
3050 if (err) {
3051 mlxsw_sp_fid_put(fid);
3052 return;
3053 }
3054 vxlan_fdb_info->offloaded = true;
3055 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
3056 &vxlan_fdb_info->info, NULL);
3057 mlxsw_sp_fid_put(fid);
3058 return;
3059 }
3060
3061 /* The device has a single FDB table, whereas Linux has two - one
3062 * in the bridge driver and another in the VxLAN driver. We only
3063 * program an entry to the device if the MAC points to the VxLAN
3064 * device in the bridge's FDB table
3065 */
3066 vid = bridge_device->ops->fid_vid(bridge_device, fid);
3067 if (br_fdb_find_port(br_dev, vxlan_fdb_info->eth_addr, vid) != dev)
3068 goto err_br_fdb_find;
3069
3070 err = mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, vxlan_fdb_info->eth_addr,
3071 mlxsw_sp_fid_index(fid), proto,
3072 &addr, true, false);
3073 if (err)
3074 goto err_fdb_tunnel_uc_op;
3075 vxlan_fdb_info->offloaded = true;
3076 call_switchdev_notifiers(SWITCHDEV_VXLAN_FDB_OFFLOADED, dev,
3077 &vxlan_fdb_info->info, NULL);
3078 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
3079 vxlan_fdb_info->eth_addr, vid, dev, true);
3080
3081 mlxsw_sp_fid_put(fid);
3082
3083 return;
3084
3085 err_fdb_tunnel_uc_op:
3086 err_br_fdb_find:
3087 mlxsw_sp_fid_put(fid);
3088 }
3089
3090 static void
mlxsw_sp_switchdev_vxlan_fdb_del(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_switchdev_event_work * switchdev_work)3091 mlxsw_sp_switchdev_vxlan_fdb_del(struct mlxsw_sp *mlxsw_sp,
3092 struct mlxsw_sp_switchdev_event_work *
3093 switchdev_work)
3094 {
3095 struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
3096 struct mlxsw_sp_bridge_device *bridge_device;
3097 struct net_device *dev = switchdev_work->dev;
3098 struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3099 u8 all_zeros_mac[ETH_ALEN] = { 0 };
3100 enum mlxsw_sp_l3proto proto;
3101 union mlxsw_sp_l3addr addr;
3102 struct mlxsw_sp_fid *fid;
3103 u16 vid;
3104
3105 vxlan_fdb_info = &switchdev_work->vxlan_fdb_info;
3106
3107 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3108 if (!bridge_device)
3109 return;
3110
3111 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vxlan_fdb_info->vni);
3112 if (!fid)
3113 return;
3114
3115 mlxsw_sp_switchdev_vxlan_addr_convert(&vxlan_fdb_info->remote_ip,
3116 &proto, &addr);
3117
3118 if (ether_addr_equal(vxlan_fdb_info->eth_addr, all_zeros_mac)) {
3119 mlxsw_sp_nve_flood_ip_del(mlxsw_sp, fid, proto, &addr);
3120 mlxsw_sp_fid_put(fid);
3121 return;
3122 }
3123
3124 mlxsw_sp_port_fdb_tunnel_uc_op(mlxsw_sp, vxlan_fdb_info->eth_addr,
3125 mlxsw_sp_fid_index(fid), proto, &addr,
3126 false, false);
3127 vid = bridge_device->ops->fid_vid(bridge_device, fid);
3128 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
3129 vxlan_fdb_info->eth_addr, vid, dev, false);
3130
3131 mlxsw_sp_fid_put(fid);
3132 }
3133
mlxsw_sp_switchdev_vxlan_fdb_event_work(struct work_struct * work)3134 static void mlxsw_sp_switchdev_vxlan_fdb_event_work(struct work_struct *work)
3135 {
3136 struct mlxsw_sp_switchdev_event_work *switchdev_work =
3137 container_of(work, struct mlxsw_sp_switchdev_event_work, work);
3138 struct net_device *dev = switchdev_work->dev;
3139 struct mlxsw_sp *mlxsw_sp;
3140 struct net_device *br_dev;
3141
3142 rtnl_lock();
3143
3144 if (!netif_running(dev))
3145 goto out;
3146 br_dev = netdev_master_upper_dev_get(dev);
3147 if (!br_dev)
3148 goto out;
3149 if (!netif_is_bridge_master(br_dev))
3150 goto out;
3151 mlxsw_sp = mlxsw_sp_lower_get(br_dev);
3152 if (!mlxsw_sp)
3153 goto out;
3154
3155 switch (switchdev_work->event) {
3156 case SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE:
3157 mlxsw_sp_switchdev_vxlan_fdb_add(mlxsw_sp, switchdev_work);
3158 break;
3159 case SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE:
3160 mlxsw_sp_switchdev_vxlan_fdb_del(mlxsw_sp, switchdev_work);
3161 break;
3162 }
3163
3164 out:
3165 rtnl_unlock();
3166 kfree(switchdev_work);
3167 dev_put(dev);
3168 }
3169
3170 static int
mlxsw_sp_switchdev_vxlan_work_prepare(struct mlxsw_sp_switchdev_event_work * switchdev_work,struct switchdev_notifier_info * info)3171 mlxsw_sp_switchdev_vxlan_work_prepare(struct mlxsw_sp_switchdev_event_work *
3172 switchdev_work,
3173 struct switchdev_notifier_info *info)
3174 {
3175 struct vxlan_dev *vxlan = netdev_priv(switchdev_work->dev);
3176 struct switchdev_notifier_vxlan_fdb_info *vxlan_fdb_info;
3177 struct vxlan_config *cfg = &vxlan->cfg;
3178 struct netlink_ext_ack *extack;
3179
3180 extack = switchdev_notifier_info_to_extack(info);
3181 vxlan_fdb_info = container_of(info,
3182 struct switchdev_notifier_vxlan_fdb_info,
3183 info);
3184
3185 if (vxlan_fdb_info->remote_port != cfg->dst_port) {
3186 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Non-default remote port is not supported");
3187 return -EOPNOTSUPP;
3188 }
3189 if (vxlan_fdb_info->remote_vni != cfg->vni ||
3190 vxlan_fdb_info->vni != cfg->vni) {
3191 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Non-default VNI is not supported");
3192 return -EOPNOTSUPP;
3193 }
3194 if (vxlan_fdb_info->remote_ifindex) {
3195 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Local interface is not supported");
3196 return -EOPNOTSUPP;
3197 }
3198 if (is_multicast_ether_addr(vxlan_fdb_info->eth_addr)) {
3199 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Multicast MAC addresses not supported");
3200 return -EOPNOTSUPP;
3201 }
3202 if (vxlan_addr_multicast(&vxlan_fdb_info->remote_ip)) {
3203 NL_SET_ERR_MSG_MOD(extack, "VxLAN: FDB: Multicast destination IP is not supported");
3204 return -EOPNOTSUPP;
3205 }
3206
3207 switchdev_work->vxlan_fdb_info = *vxlan_fdb_info;
3208
3209 return 0;
3210 }
3211
3212 /* Called under rcu_read_lock() */
mlxsw_sp_switchdev_event(struct notifier_block * unused,unsigned long event,void * ptr)3213 static int mlxsw_sp_switchdev_event(struct notifier_block *unused,
3214 unsigned long event, void *ptr)
3215 {
3216 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3217 struct mlxsw_sp_switchdev_event_work *switchdev_work;
3218 struct switchdev_notifier_fdb_info *fdb_info;
3219 struct switchdev_notifier_info *info = ptr;
3220 struct net_device *br_dev;
3221 int err;
3222
3223 if (event == SWITCHDEV_PORT_ATTR_SET) {
3224 err = switchdev_handle_port_attr_set(dev, ptr,
3225 mlxsw_sp_port_dev_check,
3226 mlxsw_sp_port_attr_set);
3227 return notifier_from_errno(err);
3228 }
3229
3230 /* Tunnel devices are not our uppers, so check their master instead */
3231 br_dev = netdev_master_upper_dev_get_rcu(dev);
3232 if (!br_dev)
3233 return NOTIFY_DONE;
3234 if (!netif_is_bridge_master(br_dev))
3235 return NOTIFY_DONE;
3236 if (!mlxsw_sp_port_dev_lower_find_rcu(br_dev))
3237 return NOTIFY_DONE;
3238
3239 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
3240 if (!switchdev_work)
3241 return NOTIFY_BAD;
3242
3243 switchdev_work->dev = dev;
3244 switchdev_work->event = event;
3245
3246 switch (event) {
3247 case SWITCHDEV_FDB_ADD_TO_DEVICE:
3248 case SWITCHDEV_FDB_DEL_TO_DEVICE:
3249 case SWITCHDEV_FDB_ADD_TO_BRIDGE:
3250 case SWITCHDEV_FDB_DEL_TO_BRIDGE:
3251 fdb_info = container_of(info,
3252 struct switchdev_notifier_fdb_info,
3253 info);
3254 INIT_WORK(&switchdev_work->work,
3255 mlxsw_sp_switchdev_bridge_fdb_event_work);
3256 memcpy(&switchdev_work->fdb_info, ptr,
3257 sizeof(switchdev_work->fdb_info));
3258 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
3259 if (!switchdev_work->fdb_info.addr)
3260 goto err_addr_alloc;
3261 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
3262 fdb_info->addr);
3263 /* Take a reference on the device. This can be either
3264 * upper device containig mlxsw_sp_port or just a
3265 * mlxsw_sp_port
3266 */
3267 dev_hold(dev);
3268 break;
3269 case SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE:
3270 case SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE:
3271 INIT_WORK(&switchdev_work->work,
3272 mlxsw_sp_switchdev_vxlan_fdb_event_work);
3273 err = mlxsw_sp_switchdev_vxlan_work_prepare(switchdev_work,
3274 info);
3275 if (err)
3276 goto err_vxlan_work_prepare;
3277 dev_hold(dev);
3278 break;
3279 default:
3280 kfree(switchdev_work);
3281 return NOTIFY_DONE;
3282 }
3283
3284 mlxsw_core_schedule_work(&switchdev_work->work);
3285
3286 return NOTIFY_DONE;
3287
3288 err_vxlan_work_prepare:
3289 err_addr_alloc:
3290 kfree(switchdev_work);
3291 return NOTIFY_BAD;
3292 }
3293
3294 struct notifier_block mlxsw_sp_switchdev_notifier = {
3295 .notifier_call = mlxsw_sp_switchdev_event,
3296 };
3297
3298 static int
mlxsw_sp_switchdev_vxlan_vlan_add(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_bridge_device * bridge_device,const struct net_device * vxlan_dev,u16 vid,bool flag_untagged,bool flag_pvid,struct netlink_ext_ack * extack)3299 mlxsw_sp_switchdev_vxlan_vlan_add(struct mlxsw_sp *mlxsw_sp,
3300 struct mlxsw_sp_bridge_device *bridge_device,
3301 const struct net_device *vxlan_dev, u16 vid,
3302 bool flag_untagged, bool flag_pvid,
3303 struct netlink_ext_ack *extack)
3304 {
3305 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
3306 __be32 vni = vxlan->cfg.vni;
3307 struct mlxsw_sp_fid *fid;
3308 u16 old_vid;
3309 int err;
3310
3311 /* We cannot have the same VLAN as PVID and egress untagged on multiple
3312 * VxLAN devices. Note that we get this notification before the VLAN is
3313 * actually added to the bridge's database, so it is not possible for
3314 * the lookup function to return 'vxlan_dev'
3315 */
3316 if (flag_untagged && flag_pvid &&
3317 mlxsw_sp_bridge_8021q_vxlan_dev_find(bridge_device->dev, vid)) {
3318 NL_SET_ERR_MSG_MOD(extack, "VLAN already mapped to a different VNI");
3319 return -EINVAL;
3320 }
3321
3322 if (!netif_running(vxlan_dev))
3323 return 0;
3324
3325 /* First case: FID is not associated with this VNI, but the new VLAN
3326 * is both PVID and egress untagged. Need to enable NVE on the FID, if
3327 * it exists
3328 */
3329 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vni);
3330 if (!fid) {
3331 if (!flag_untagged || !flag_pvid)
3332 return 0;
3333 return bridge_device->ops->vxlan_join(bridge_device, vxlan_dev,
3334 vid, extack);
3335 }
3336
3337 /* Second case: FID is associated with the VNI and the VLAN associated
3338 * with the FID is the same as the notified VLAN. This means the flags
3339 * (PVID / egress untagged) were toggled and that NVE should be
3340 * disabled on the FID
3341 */
3342 old_vid = mlxsw_sp_fid_8021q_vid(fid);
3343 if (vid == old_vid) {
3344 if (WARN_ON(flag_untagged && flag_pvid)) {
3345 mlxsw_sp_fid_put(fid);
3346 return -EINVAL;
3347 }
3348 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, vxlan_dev);
3349 mlxsw_sp_fid_put(fid);
3350 return 0;
3351 }
3352
3353 /* Third case: A new VLAN was configured on the VxLAN device, but this
3354 * VLAN is not PVID, so there is nothing to do.
3355 */
3356 if (!flag_pvid) {
3357 mlxsw_sp_fid_put(fid);
3358 return 0;
3359 }
3360
3361 /* Fourth case: Thew new VLAN is PVID, which means the VLAN currently
3362 * mapped to the VNI should be unmapped
3363 */
3364 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, vxlan_dev);
3365 mlxsw_sp_fid_put(fid);
3366
3367 /* Fifth case: The new VLAN is also egress untagged, which means the
3368 * VLAN needs to be mapped to the VNI
3369 */
3370 if (!flag_untagged)
3371 return 0;
3372
3373 err = bridge_device->ops->vxlan_join(bridge_device, vxlan_dev, vid, extack);
3374 if (err)
3375 goto err_vxlan_join;
3376
3377 return 0;
3378
3379 err_vxlan_join:
3380 bridge_device->ops->vxlan_join(bridge_device, vxlan_dev, old_vid, NULL);
3381 return err;
3382 }
3383
3384 static void
mlxsw_sp_switchdev_vxlan_vlan_del(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_bridge_device * bridge_device,const struct net_device * vxlan_dev,u16 vid)3385 mlxsw_sp_switchdev_vxlan_vlan_del(struct mlxsw_sp *mlxsw_sp,
3386 struct mlxsw_sp_bridge_device *bridge_device,
3387 const struct net_device *vxlan_dev, u16 vid)
3388 {
3389 struct vxlan_dev *vxlan = netdev_priv(vxlan_dev);
3390 __be32 vni = vxlan->cfg.vni;
3391 struct mlxsw_sp_fid *fid;
3392
3393 if (!netif_running(vxlan_dev))
3394 return;
3395
3396 fid = mlxsw_sp_fid_lookup_by_vni(mlxsw_sp, vni);
3397 if (!fid)
3398 return;
3399
3400 /* A different VLAN than the one mapped to the VNI is deleted */
3401 if (mlxsw_sp_fid_8021q_vid(fid) != vid)
3402 goto out;
3403
3404 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, vxlan_dev);
3405
3406 out:
3407 mlxsw_sp_fid_put(fid);
3408 }
3409
3410 static int
mlxsw_sp_switchdev_vxlan_vlans_add(struct net_device * vxlan_dev,struct switchdev_notifier_port_obj_info * port_obj_info)3411 mlxsw_sp_switchdev_vxlan_vlans_add(struct net_device *vxlan_dev,
3412 struct switchdev_notifier_port_obj_info *
3413 port_obj_info)
3414 {
3415 struct switchdev_obj_port_vlan *vlan =
3416 SWITCHDEV_OBJ_PORT_VLAN(port_obj_info->obj);
3417 bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
3418 bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
3419 struct mlxsw_sp_bridge_device *bridge_device;
3420 struct netlink_ext_ack *extack;
3421 struct mlxsw_sp *mlxsw_sp;
3422 struct net_device *br_dev;
3423
3424 extack = switchdev_notifier_info_to_extack(&port_obj_info->info);
3425 br_dev = netdev_master_upper_dev_get(vxlan_dev);
3426 if (!br_dev)
3427 return 0;
3428
3429 mlxsw_sp = mlxsw_sp_lower_get(br_dev);
3430 if (!mlxsw_sp)
3431 return 0;
3432
3433 port_obj_info->handled = true;
3434
3435 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3436 if (!bridge_device)
3437 return -EINVAL;
3438
3439 if (!bridge_device->vlan_enabled)
3440 return 0;
3441
3442 return mlxsw_sp_switchdev_vxlan_vlan_add(mlxsw_sp, bridge_device,
3443 vxlan_dev, vlan->vid,
3444 flag_untagged,
3445 flag_pvid, extack);
3446 }
3447
3448 static void
mlxsw_sp_switchdev_vxlan_vlans_del(struct net_device * vxlan_dev,struct switchdev_notifier_port_obj_info * port_obj_info)3449 mlxsw_sp_switchdev_vxlan_vlans_del(struct net_device *vxlan_dev,
3450 struct switchdev_notifier_port_obj_info *
3451 port_obj_info)
3452 {
3453 struct switchdev_obj_port_vlan *vlan =
3454 SWITCHDEV_OBJ_PORT_VLAN(port_obj_info->obj);
3455 struct mlxsw_sp_bridge_device *bridge_device;
3456 struct mlxsw_sp *mlxsw_sp;
3457 struct net_device *br_dev;
3458
3459 br_dev = netdev_master_upper_dev_get(vxlan_dev);
3460 if (!br_dev)
3461 return;
3462
3463 mlxsw_sp = mlxsw_sp_lower_get(br_dev);
3464 if (!mlxsw_sp)
3465 return;
3466
3467 port_obj_info->handled = true;
3468
3469 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
3470 if (!bridge_device)
3471 return;
3472
3473 if (!bridge_device->vlan_enabled)
3474 return;
3475
3476 mlxsw_sp_switchdev_vxlan_vlan_del(mlxsw_sp, bridge_device, vxlan_dev,
3477 vlan->vid);
3478 }
3479
3480 static int
mlxsw_sp_switchdev_handle_vxlan_obj_add(struct net_device * vxlan_dev,struct switchdev_notifier_port_obj_info * port_obj_info)3481 mlxsw_sp_switchdev_handle_vxlan_obj_add(struct net_device *vxlan_dev,
3482 struct switchdev_notifier_port_obj_info *
3483 port_obj_info)
3484 {
3485 int err = 0;
3486
3487 switch (port_obj_info->obj->id) {
3488 case SWITCHDEV_OBJ_ID_PORT_VLAN:
3489 err = mlxsw_sp_switchdev_vxlan_vlans_add(vxlan_dev,
3490 port_obj_info);
3491 break;
3492 default:
3493 break;
3494 }
3495
3496 return err;
3497 }
3498
3499 static void
mlxsw_sp_switchdev_handle_vxlan_obj_del(struct net_device * vxlan_dev,struct switchdev_notifier_port_obj_info * port_obj_info)3500 mlxsw_sp_switchdev_handle_vxlan_obj_del(struct net_device *vxlan_dev,
3501 struct switchdev_notifier_port_obj_info *
3502 port_obj_info)
3503 {
3504 switch (port_obj_info->obj->id) {
3505 case SWITCHDEV_OBJ_ID_PORT_VLAN:
3506 mlxsw_sp_switchdev_vxlan_vlans_del(vxlan_dev, port_obj_info);
3507 break;
3508 default:
3509 break;
3510 }
3511 }
3512
mlxsw_sp_switchdev_blocking_event(struct notifier_block * unused,unsigned long event,void * ptr)3513 static int mlxsw_sp_switchdev_blocking_event(struct notifier_block *unused,
3514 unsigned long event, void *ptr)
3515 {
3516 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
3517 int err = 0;
3518
3519 switch (event) {
3520 case SWITCHDEV_PORT_OBJ_ADD:
3521 if (netif_is_vxlan(dev))
3522 err = mlxsw_sp_switchdev_handle_vxlan_obj_add(dev, ptr);
3523 else
3524 err = switchdev_handle_port_obj_add(dev, ptr,
3525 mlxsw_sp_port_dev_check,
3526 mlxsw_sp_port_obj_add);
3527 return notifier_from_errno(err);
3528 case SWITCHDEV_PORT_OBJ_DEL:
3529 if (netif_is_vxlan(dev))
3530 mlxsw_sp_switchdev_handle_vxlan_obj_del(dev, ptr);
3531 else
3532 err = switchdev_handle_port_obj_del(dev, ptr,
3533 mlxsw_sp_port_dev_check,
3534 mlxsw_sp_port_obj_del);
3535 return notifier_from_errno(err);
3536 case SWITCHDEV_PORT_ATTR_SET:
3537 err = switchdev_handle_port_attr_set(dev, ptr,
3538 mlxsw_sp_port_dev_check,
3539 mlxsw_sp_port_attr_set);
3540 return notifier_from_errno(err);
3541 }
3542
3543 return NOTIFY_DONE;
3544 }
3545
3546 static struct notifier_block mlxsw_sp_switchdev_blocking_notifier = {
3547 .notifier_call = mlxsw_sp_switchdev_blocking_event,
3548 };
3549
3550 u8
mlxsw_sp_bridge_port_stp_state(struct mlxsw_sp_bridge_port * bridge_port)3551 mlxsw_sp_bridge_port_stp_state(struct mlxsw_sp_bridge_port *bridge_port)
3552 {
3553 return bridge_port->stp_state;
3554 }
3555
mlxsw_sp_fdb_init(struct mlxsw_sp * mlxsw_sp)3556 static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
3557 {
3558 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
3559 struct notifier_block *nb;
3560 int err;
3561
3562 err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
3563 if (err) {
3564 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
3565 return err;
3566 }
3567
3568 err = register_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
3569 if (err) {
3570 dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev notifier\n");
3571 return err;
3572 }
3573
3574 nb = &mlxsw_sp_switchdev_blocking_notifier;
3575 err = register_switchdev_blocking_notifier(nb);
3576 if (err) {
3577 dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev blocking notifier\n");
3578 goto err_register_switchdev_blocking_notifier;
3579 }
3580
3581 INIT_DELAYED_WORK(&bridge->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
3582 bridge->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
3583 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp, false);
3584 return 0;
3585
3586 err_register_switchdev_blocking_notifier:
3587 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
3588 return err;
3589 }
3590
mlxsw_sp_fdb_fini(struct mlxsw_sp * mlxsw_sp)3591 static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
3592 {
3593 struct notifier_block *nb;
3594
3595 cancel_delayed_work_sync(&mlxsw_sp->bridge->fdb_notify.dw);
3596
3597 nb = &mlxsw_sp_switchdev_blocking_notifier;
3598 unregister_switchdev_blocking_notifier(nb);
3599
3600 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
3601 }
3602
mlxsw_sp1_switchdev_init(struct mlxsw_sp * mlxsw_sp)3603 static void mlxsw_sp1_switchdev_init(struct mlxsw_sp *mlxsw_sp)
3604 {
3605 mlxsw_sp->bridge->bridge_8021ad_ops = &mlxsw_sp1_bridge_8021ad_ops;
3606 }
3607
3608 const struct mlxsw_sp_switchdev_ops mlxsw_sp1_switchdev_ops = {
3609 .init = mlxsw_sp1_switchdev_init,
3610 };
3611
mlxsw_sp2_switchdev_init(struct mlxsw_sp * mlxsw_sp)3612 static void mlxsw_sp2_switchdev_init(struct mlxsw_sp *mlxsw_sp)
3613 {
3614 mlxsw_sp->bridge->bridge_8021ad_ops = &mlxsw_sp2_bridge_8021ad_ops;
3615 }
3616
3617 const struct mlxsw_sp_switchdev_ops mlxsw_sp2_switchdev_ops = {
3618 .init = mlxsw_sp2_switchdev_init,
3619 };
3620
mlxsw_sp_switchdev_init(struct mlxsw_sp * mlxsw_sp)3621 int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
3622 {
3623 struct mlxsw_sp_bridge *bridge;
3624
3625 bridge = kzalloc(sizeof(*mlxsw_sp->bridge), GFP_KERNEL);
3626 if (!bridge)
3627 return -ENOMEM;
3628 mlxsw_sp->bridge = bridge;
3629 bridge->mlxsw_sp = mlxsw_sp;
3630
3631 INIT_LIST_HEAD(&mlxsw_sp->bridge->bridges_list);
3632
3633 bridge->bridge_8021q_ops = &mlxsw_sp_bridge_8021q_ops;
3634 bridge->bridge_8021d_ops = &mlxsw_sp_bridge_8021d_ops;
3635
3636 mlxsw_sp->switchdev_ops->init(mlxsw_sp);
3637
3638 return mlxsw_sp_fdb_init(mlxsw_sp);
3639 }
3640
mlxsw_sp_switchdev_fini(struct mlxsw_sp * mlxsw_sp)3641 void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
3642 {
3643 mlxsw_sp_fdb_fini(mlxsw_sp);
3644 WARN_ON(!list_empty(&mlxsw_sp->bridge->bridges_list));
3645 kfree(mlxsw_sp->bridge);
3646 }
3647
3648