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