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