Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / net / ethernet / mellanox / mlxsw / spectrum_switchdev.c
blob161bcdc012f07652eac9165e42e299457da05c7b
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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
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>
52 #include "spectrum_router.h"
53 #include "spectrum.h"
54 #include "core.h"
55 #include "reg.h"
57 struct mlxsw_sp_bridge_ops;
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;
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;
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;
104 struct mlxsw_sp_bridge_vlan {
105 struct list_head list;
106 struct list_head port_vlan_list;
107 u16 vid;
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);
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);
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);
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);
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);
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)
146 struct mlxsw_sp_bridge_device *bridge_device;
148 list_for_each_entry(bridge_device, &bridge->bridges_list, list)
149 if (bridge_device->dev == br_dev)
150 return bridge_device;
152 return NULL;
155 bool mlxsw_sp_bridge_device_is_offloaded(const struct mlxsw_sp *mlxsw_sp,
156 const struct net_device *br_dev)
158 return !!mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
161 static struct mlxsw_sp_bridge_device *
162 mlxsw_sp_bridge_device_create(struct mlxsw_sp_bridge *bridge,
163 struct net_device *br_dev)
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);
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);
174 bridge_device = kzalloc(sizeof(*bridge_device), GFP_KERNEL);
175 if (!bridge_device)
176 return ERR_PTR(-ENOMEM);
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;
189 INIT_LIST_HEAD(&bridge_device->mids_list);
190 list_add(&bridge_device->list, &bridge->bridges_list);
192 return bridge_device;
195 static void
196 mlxsw_sp_bridge_device_destroy(struct mlxsw_sp_bridge *bridge,
197 struct mlxsw_sp_bridge_device *bridge_device)
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);
207 static struct mlxsw_sp_bridge_device *
208 mlxsw_sp_bridge_device_get(struct mlxsw_sp_bridge *bridge,
209 struct net_device *br_dev)
211 struct mlxsw_sp_bridge_device *bridge_device;
213 bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
214 if (bridge_device)
215 return bridge_device;
217 return mlxsw_sp_bridge_device_create(bridge, br_dev);
220 static void
221 mlxsw_sp_bridge_device_put(struct mlxsw_sp_bridge *bridge,
222 struct mlxsw_sp_bridge_device *bridge_device)
224 if (list_empty(&bridge_device->ports_list))
225 mlxsw_sp_bridge_device_destroy(bridge, bridge_device);
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)
232 struct mlxsw_sp_bridge_port *bridge_port;
234 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
235 if (bridge_port->dev == brport_dev)
236 return bridge_port;
239 return NULL;
242 static struct mlxsw_sp_bridge_port *
243 mlxsw_sp_bridge_port_find(struct mlxsw_sp_bridge *bridge,
244 struct net_device *brport_dev)
246 struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
247 struct mlxsw_sp_bridge_device *bridge_device;
249 if (!br_dev)
250 return NULL;
252 bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
253 if (!bridge_device)
254 return NULL;
256 return __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
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)
263 struct mlxsw_sp_bridge_port *bridge_port;
264 struct mlxsw_sp_port *mlxsw_sp_port;
266 bridge_port = kzalloc(sizeof(*bridge_port), GFP_KERNEL);
267 if (!bridge_port)
268 return NULL;
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;
285 return bridge_port;
288 static void
289 mlxsw_sp_bridge_port_destroy(struct mlxsw_sp_bridge_port *bridge_port)
291 list_del(&bridge_port->list);
292 WARN_ON(!list_empty(&bridge_port->vlans_list));
293 kfree(bridge_port);
296 static bool
297 mlxsw_sp_bridge_port_should_destroy(const struct mlxsw_sp_bridge_port *
298 bridge_port)
300 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_port->dev);
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.
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;
314 static struct mlxsw_sp_bridge_port *
315 mlxsw_sp_bridge_port_get(struct mlxsw_sp_bridge *bridge,
316 struct net_device *brport_dev)
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;
323 bridge_port = mlxsw_sp_bridge_port_find(bridge, brport_dev);
324 if (bridge_port) {
325 bridge_port->ref_count++;
326 return bridge_port;
329 bridge_device = mlxsw_sp_bridge_device_get(bridge, br_dev);
330 if (IS_ERR(bridge_device))
331 return ERR_CAST(bridge_device);
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;
339 return bridge_port;
341 err_bridge_port_create:
342 mlxsw_sp_bridge_device_put(bridge, bridge_device);
343 return ERR_PTR(err);
346 static void mlxsw_sp_bridge_port_put(struct mlxsw_sp_bridge *bridge,
347 struct mlxsw_sp_bridge_port *bridge_port)
349 struct mlxsw_sp_bridge_device *bridge_device;
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);
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)
365 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
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;
380 return NULL;
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)
387 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
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;
393 if (fid && mlxsw_sp_fid_index(fid) == fid_index)
394 return mlxsw_sp_port_vlan;
397 return NULL;
400 static struct mlxsw_sp_bridge_vlan *
401 mlxsw_sp_bridge_vlan_find(const struct mlxsw_sp_bridge_port *bridge_port,
402 u16 vid)
404 struct mlxsw_sp_bridge_vlan *bridge_vlan;
406 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
407 if (bridge_vlan->vid == vid)
408 return bridge_vlan;
411 return NULL;
414 static struct mlxsw_sp_bridge_vlan *
415 mlxsw_sp_bridge_vlan_create(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
417 struct mlxsw_sp_bridge_vlan *bridge_vlan;
419 bridge_vlan = kzalloc(sizeof(*bridge_vlan), GFP_KERNEL);
420 if (!bridge_vlan)
421 return NULL;
423 INIT_LIST_HEAD(&bridge_vlan->port_vlan_list);
424 bridge_vlan->vid = vid;
425 list_add(&bridge_vlan->list, &bridge_port->vlans_list);
427 return bridge_vlan;
430 static void
431 mlxsw_sp_bridge_vlan_destroy(struct mlxsw_sp_bridge_vlan *bridge_vlan)
433 list_del(&bridge_vlan->list);
434 WARN_ON(!list_empty(&bridge_vlan->port_vlan_list));
435 kfree(bridge_vlan);
438 static struct mlxsw_sp_bridge_vlan *
439 mlxsw_sp_bridge_vlan_get(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
441 struct mlxsw_sp_bridge_vlan *bridge_vlan;
443 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
444 if (bridge_vlan)
445 return bridge_vlan;
447 return mlxsw_sp_bridge_vlan_create(bridge_port, vid);
450 static void mlxsw_sp_bridge_vlan_put(struct mlxsw_sp_bridge_vlan *bridge_vlan)
452 if (list_empty(&bridge_vlan->port_vlan_list))
453 mlxsw_sp_bridge_vlan_destroy(bridge_vlan);
456 static void mlxsw_sp_port_bridge_flags_get(struct mlxsw_sp_bridge *bridge,
457 struct net_device *dev,
458 unsigned long *brport_flags)
460 struct mlxsw_sp_bridge_port *bridge_port;
462 bridge_port = mlxsw_sp_bridge_port_find(bridge, dev);
463 if (WARN_ON(!bridge_port))
464 return;
466 memcpy(brport_flags, &bridge_port->flags, sizeof(*brport_flags));
469 static int mlxsw_sp_port_attr_get(struct net_device *dev,
470 struct switchdev_attr *attr)
472 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
473 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
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;
493 return 0;
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)
501 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
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);
511 return 0;
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)
519 struct mlxsw_sp_bridge_port *bridge_port;
520 struct mlxsw_sp_bridge_vlan *bridge_vlan;
521 int err;
523 if (switchdev_trans_ph_prepare(trans))
524 return 0;
526 /* It's possible we failed to enslave the port, yet this
527 * operation is executed due to it being deferred.
529 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
530 orig_dev);
531 if (!bridge_port)
532 return 0;
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;
541 bridge_port->stp_state = state;
543 return 0;
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;
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)
559 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
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);
571 return 0;
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)
580 struct mlxsw_sp_bridge_vlan *bridge_vlan;
581 int err;
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;
592 return 0;
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;
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)
607 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
608 u16 vid = bridge_vlan->vid;
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);
617 return 0;
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)
625 struct mlxsw_sp_bridge_vlan *bridge_vlan;
626 int err;
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;
635 return 0;
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;
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)
650 struct mlxsw_sp_bridge_port *bridge_port;
651 int err;
653 if (switchdev_trans_ph_prepare(trans))
654 return 0;
656 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
657 orig_dev);
658 if (!bridge_port)
659 return 0;
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;
667 err = mlxsw_sp_bridge_port_learning_set(mlxsw_sp_port, bridge_port,
668 brport_flags & BR_LEARNING);
669 if (err)
670 return err;
672 if (bridge_port->bridge_device->multicast_enabled)
673 goto out;
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;
682 out:
683 memcpy(&bridge_port->flags, &brport_flags, sizeof(brport_flags));
684 return 0;
687 static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
689 char sfdat_pl[MLXSW_REG_SFDAT_LEN];
690 int err;
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;
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)
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;
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;
716 return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
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)
724 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
725 struct mlxsw_sp_bridge_device *bridge_device;
727 if (!switchdev_trans_ph_prepare(trans))
728 return 0;
730 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
731 if (WARN_ON(!bridge_device))
732 return -EINVAL;
734 if (bridge_device->vlan_enabled == vlan_enabled)
735 return 0;
737 netdev_err(bridge_device->dev, "VLAN filtering can't be changed for existing bridge\n");
738 return -EINVAL;
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)
746 struct mlxsw_sp_bridge_port *bridge_port;
747 int err;
749 if (switchdev_trans_ph_prepare(trans))
750 return 0;
752 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
753 orig_dev);
754 if (!bridge_port)
755 return 0;
757 if (!bridge_port->bridge_device->multicast_enabled)
758 goto out;
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;
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;
773 static bool mlxsw_sp_mc_flood(const struct mlxsw_sp_bridge_port *bridge_port)
775 const struct mlxsw_sp_bridge_device *bridge_device;
777 bridge_device = bridge_port->bridge_device;
778 return bridge_device->multicast_enabled ? bridge_port->mrouter :
779 bridge_port->flags & BR_MCAST_FLOOD;
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)
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;
792 if (switchdev_trans_ph_prepare(trans))
793 return 0;
795 /* It's possible we failed to enslave the port, yet this
796 * operation is executed due to it being deferred.
798 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
799 if (!bridge_device)
800 return 0;
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);
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);
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;
819 bridge_device->multicast_enabled = !mc_disabled;
821 return 0;
824 static int mlxsw_sp_smid_router_port_set(struct mlxsw_sp *mlxsw_sp,
825 u16 mid_idx, bool add)
827 char *smid_pl;
828 int err;
830 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
831 if (!smid_pl)
832 return -ENOMEM;
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;
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)
846 struct mlxsw_sp_mid *mid;
848 list_for_each_entry(mid, &bridge_device->mids_list, list)
849 mlxsw_sp_smid_router_port_set(mlxsw_sp, mid->mid, add);
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)
858 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
859 struct mlxsw_sp_bridge_device *bridge_device;
861 if (switchdev_trans_ph_prepare(trans))
862 return 0;
864 /* It's possible we failed to enslave the port, yet this
865 * operation is executed due to it being deferred.
867 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
868 if (!bridge_device)
869 return 0;
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;
878 static int mlxsw_sp_port_attr_set(struct net_device *dev,
879 const struct switchdev_attr *attr,
880 struct switchdev_trans *trans)
882 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
883 int err;
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;
925 return err;
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)
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;
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);
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;
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;
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;
959 err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
960 if (err)
961 goto err_fid_port_vid_map;
963 mlxsw_sp_port_vlan->fid = fid;
965 return 0;
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;
978 static void
979 mlxsw_sp_port_vlan_fid_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
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;
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);
994 static u16
995 mlxsw_sp_port_pvid_determine(const struct mlxsw_sp_port *mlxsw_sp_port,
996 u16 vid, bool is_pvid)
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;
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)
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;
1015 /* No need to continue if only VLAN flags were changed */
1016 if (mlxsw_sp_port_vlan->bridge_port)
1017 return 0;
1019 err = mlxsw_sp_port_vlan_fid_join(mlxsw_sp_port_vlan, bridge_port);
1020 if (err)
1021 return err;
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;
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;
1033 bridge_vlan = mlxsw_sp_bridge_vlan_get(bridge_port, vid);
1034 if (!bridge_vlan) {
1035 err = -ENOMEM;
1036 goto err_bridge_vlan_get;
1039 list_add(&mlxsw_sp_port_vlan->bridge_vlan_node,
1040 &bridge_vlan->port_vlan_list);
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;
1046 return 0;
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;
1057 void
1058 mlxsw_sp_port_vlan_bridge_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
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;
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;
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);
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);
1087 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1089 mlxsw_sp_bridge_port_put(mlxsw_sp_port->mlxsw_sp->bridge, bridge_port);
1090 mlxsw_sp_port_vlan->bridge_port = NULL;
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)
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;
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);
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;
1112 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1113 if (err)
1114 goto err_port_pvid_set;
1116 err = mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
1117 if (err)
1118 goto err_port_vlan_bridge_join;
1120 return 0;
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;
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)
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;
1142 if (switchdev_trans_ph_prepare(trans))
1143 return 0;
1145 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1146 if (WARN_ON(!bridge_port))
1147 return -EINVAL;
1149 if (!bridge_port->bridge_device->vlan_enabled)
1150 return 0;
1152 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1153 int err;
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;
1162 return 0;
1165 static enum mlxsw_reg_sfdf_flush_type mlxsw_sp_fdb_flush_type(bool lagged)
1167 return lagged ? MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID :
1168 MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID;
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)
1176 bool lagged = bridge_port->lagged;
1177 char sfdf_pl[MLXSW_REG_SFDF_LEN];
1178 u16 system_port;
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);
1185 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl);
1188 static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
1190 return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
1191 MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY;
1194 static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
1196 return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
1197 MLXSW_REG_SFD_OP_WRITE_REMOVE;
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)
1205 char *sfd_pl;
1206 u8 num_rec;
1207 int err;
1209 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1210 if (!sfd_pl)
1211 return -ENOMEM;
1213 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1214 mlxsw_reg_sfd_uc_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1215 mac, fid, action, local_port);
1216 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1217 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1218 if (err)
1219 goto out;
1221 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1222 err = -EBUSY;
1224 out:
1225 kfree(sfd_pl);
1226 return err;
1229 static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1230 const char *mac, u16 fid, bool adding,
1231 bool dynamic)
1233 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding,
1234 MLXSW_REG_SFD_REC_ACTION_NOP, dynamic);
1237 int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid,
1238 bool adding)
1240 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding,
1241 MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER,
1242 false);
1245 static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
1246 const char *mac, u16 fid, u16 lag_vid,
1247 bool adding, bool dynamic)
1249 char *sfd_pl;
1250 u8 num_rec;
1251 int err;
1253 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1254 if (!sfd_pl)
1255 return -ENOMEM;
1257 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1258 mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
1259 mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
1260 lag_vid, lag_id);
1261 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1262 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1263 if (err)
1264 goto out;
1266 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1267 err = -EBUSY;
1269 out:
1270 kfree(sfd_pl);
1271 return err;
1274 static int
1275 mlxsw_sp_port_fdb_set(struct mlxsw_sp_port *mlxsw_sp_port,
1276 struct switchdev_notifier_fdb_info *fdb_info, bool adding)
1278 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1279 struct net_device *orig_dev = fdb_info->info.dev;
1280 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1281 struct mlxsw_sp_bridge_device *bridge_device;
1282 struct mlxsw_sp_bridge_port *bridge_port;
1283 u16 fid_index, vid;
1285 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1286 if (!bridge_port)
1287 return -EINVAL;
1289 bridge_device = bridge_port->bridge_device;
1290 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1291 bridge_device,
1292 fdb_info->vid);
1293 if (!mlxsw_sp_port_vlan)
1294 return 0;
1296 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1297 vid = mlxsw_sp_port_vlan->vid;
1299 if (!bridge_port->lagged)
1300 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp,
1301 bridge_port->system_port,
1302 fdb_info->addr, fid_index,
1303 adding, false);
1304 else
1305 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp,
1306 bridge_port->lag_id,
1307 fdb_info->addr, fid_index,
1308 vid, adding, false);
1311 static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
1312 u16 fid, u16 mid_idx, bool adding)
1314 char *sfd_pl;
1315 u8 num_rec;
1316 int err;
1318 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1319 if (!sfd_pl)
1320 return -ENOMEM;
1322 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1323 mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
1324 MLXSW_REG_SFD_REC_ACTION_NOP, mid_idx);
1325 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1326 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1327 if (err)
1328 goto out;
1330 if (num_rec != mlxsw_reg_sfd_num_rec_get(sfd_pl))
1331 err = -EBUSY;
1333 out:
1334 kfree(sfd_pl);
1335 return err;
1338 static int mlxsw_sp_port_smid_full_entry(struct mlxsw_sp *mlxsw_sp, u16 mid_idx,
1339 long *ports_bitmap,
1340 bool set_router_port)
1342 char *smid_pl;
1343 int err, i;
1345 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1346 if (!smid_pl)
1347 return -ENOMEM;
1349 mlxsw_reg_smid_pack(smid_pl, mid_idx, 0, false);
1350 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++) {
1351 if (mlxsw_sp->ports[i])
1352 mlxsw_reg_smid_port_mask_set(smid_pl, i, 1);
1355 mlxsw_reg_smid_port_mask_set(smid_pl,
1356 mlxsw_sp_router_port(mlxsw_sp), 1);
1358 for_each_set_bit(i, ports_bitmap, mlxsw_core_max_ports(mlxsw_sp->core))
1359 mlxsw_reg_smid_port_set(smid_pl, i, 1);
1361 mlxsw_reg_smid_port_set(smid_pl, mlxsw_sp_router_port(mlxsw_sp),
1362 set_router_port);
1364 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1365 kfree(smid_pl);
1366 return err;
1369 static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port,
1370 u16 mid_idx, bool add)
1372 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1373 char *smid_pl;
1374 int err;
1376 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1377 if (!smid_pl)
1378 return -ENOMEM;
1380 mlxsw_reg_smid_pack(smid_pl, mid_idx, mlxsw_sp_port->local_port, add);
1381 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1382 kfree(smid_pl);
1383 return err;
1386 static struct
1387 mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp_bridge_device *bridge_device,
1388 const unsigned char *addr,
1389 u16 fid)
1391 struct mlxsw_sp_mid *mid;
1393 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1394 if (ether_addr_equal(mid->addr, addr) && mid->fid == fid)
1395 return mid;
1397 return NULL;
1400 static void
1401 mlxsw_sp_bridge_port_get_ports_bitmap(struct mlxsw_sp *mlxsw_sp,
1402 struct mlxsw_sp_bridge_port *bridge_port,
1403 unsigned long *ports_bitmap)
1405 struct mlxsw_sp_port *mlxsw_sp_port;
1406 u64 max_lag_members, i;
1407 int lag_id;
1409 if (!bridge_port->lagged) {
1410 set_bit(bridge_port->system_port, ports_bitmap);
1411 } else {
1412 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1413 MAX_LAG_MEMBERS);
1414 lag_id = bridge_port->lag_id;
1415 for (i = 0; i < max_lag_members; i++) {
1416 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp,
1417 lag_id, i);
1418 if (mlxsw_sp_port)
1419 set_bit(mlxsw_sp_port->local_port,
1420 ports_bitmap);
1425 static void
1426 mlxsw_sp_mc_get_mrouters_bitmap(unsigned long *flood_bitmap,
1427 struct mlxsw_sp_bridge_device *bridge_device,
1428 struct mlxsw_sp *mlxsw_sp)
1430 struct mlxsw_sp_bridge_port *bridge_port;
1432 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
1433 if (bridge_port->mrouter) {
1434 mlxsw_sp_bridge_port_get_ports_bitmap(mlxsw_sp,
1435 bridge_port,
1436 flood_bitmap);
1441 static bool
1442 mlxsw_sp_mc_write_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1443 struct mlxsw_sp_mid *mid,
1444 struct mlxsw_sp_bridge_device *bridge_device)
1446 long *flood_bitmap;
1447 int num_of_ports;
1448 int alloc_size;
1449 u16 mid_idx;
1450 int err;
1452 mid_idx = find_first_zero_bit(mlxsw_sp->bridge->mids_bitmap,
1453 MLXSW_SP_MID_MAX);
1454 if (mid_idx == MLXSW_SP_MID_MAX)
1455 return false;
1457 num_of_ports = mlxsw_core_max_ports(mlxsw_sp->core);
1458 alloc_size = sizeof(long) * BITS_TO_LONGS(num_of_ports);
1459 flood_bitmap = kzalloc(alloc_size, GFP_KERNEL);
1460 if (!flood_bitmap)
1461 return false;
1463 bitmap_copy(flood_bitmap, mid->ports_in_mid, num_of_ports);
1464 mlxsw_sp_mc_get_mrouters_bitmap(flood_bitmap, bridge_device, mlxsw_sp);
1466 mid->mid = mid_idx;
1467 err = mlxsw_sp_port_smid_full_entry(mlxsw_sp, mid_idx, flood_bitmap,
1468 bridge_device->mrouter);
1469 kfree(flood_bitmap);
1470 if (err)
1471 return false;
1473 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid_idx,
1474 true);
1475 if (err)
1476 return false;
1478 set_bit(mid_idx, mlxsw_sp->bridge->mids_bitmap);
1479 mid->in_hw = true;
1480 return true;
1483 static int mlxsw_sp_mc_remove_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1484 struct mlxsw_sp_mid *mid)
1486 if (!mid->in_hw)
1487 return 0;
1489 clear_bit(mid->mid, mlxsw_sp->bridge->mids_bitmap);
1490 mid->in_hw = false;
1491 return mlxsw_sp_port_mdb_op(mlxsw_sp, mid->addr, mid->fid, mid->mid,
1492 false);
1495 static struct
1496 mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
1497 struct mlxsw_sp_bridge_device *bridge_device,
1498 const unsigned char *addr,
1499 u16 fid)
1501 struct mlxsw_sp_mid *mid;
1502 size_t alloc_size;
1504 mid = kzalloc(sizeof(*mid), GFP_KERNEL);
1505 if (!mid)
1506 return NULL;
1508 alloc_size = sizeof(unsigned long) *
1509 BITS_TO_LONGS(mlxsw_core_max_ports(mlxsw_sp->core));
1511 mid->ports_in_mid = kzalloc(alloc_size, GFP_KERNEL);
1512 if (!mid->ports_in_mid)
1513 goto err_ports_in_mid_alloc;
1515 ether_addr_copy(mid->addr, addr);
1516 mid->fid = fid;
1517 mid->in_hw = false;
1519 if (!bridge_device->multicast_enabled)
1520 goto out;
1522 if (!mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid, bridge_device))
1523 goto err_write_mdb_entry;
1525 out:
1526 list_add_tail(&mid->list, &bridge_device->mids_list);
1527 return mid;
1529 err_write_mdb_entry:
1530 kfree(mid->ports_in_mid);
1531 err_ports_in_mid_alloc:
1532 kfree(mid);
1533 return NULL;
1536 static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port *mlxsw_sp_port,
1537 struct mlxsw_sp_mid *mid)
1539 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1540 int err = 0;
1542 clear_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1543 if (bitmap_empty(mid->ports_in_mid,
1544 mlxsw_core_max_ports(mlxsw_sp->core))) {
1545 err = mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1546 list_del(&mid->list);
1547 kfree(mid->ports_in_mid);
1548 kfree(mid);
1550 return err;
1553 static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
1554 const struct switchdev_obj_port_mdb *mdb,
1555 struct switchdev_trans *trans)
1557 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1558 struct net_device *orig_dev = mdb->obj.orig_dev;
1559 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1560 struct net_device *dev = mlxsw_sp_port->dev;
1561 struct mlxsw_sp_bridge_device *bridge_device;
1562 struct mlxsw_sp_bridge_port *bridge_port;
1563 struct mlxsw_sp_mid *mid;
1564 u16 fid_index;
1565 int err = 0;
1567 if (switchdev_trans_ph_prepare(trans))
1568 return 0;
1570 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1571 if (!bridge_port)
1572 return 0;
1574 bridge_device = bridge_port->bridge_device;
1575 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1576 bridge_device,
1577 mdb->vid);
1578 if (!mlxsw_sp_port_vlan)
1579 return 0;
1581 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1583 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1584 if (!mid) {
1585 mid = __mlxsw_sp_mc_alloc(mlxsw_sp, bridge_device, mdb->addr,
1586 fid_index);
1587 if (!mid) {
1588 netdev_err(dev, "Unable to allocate MC group\n");
1589 return -ENOMEM;
1592 set_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
1594 if (!bridge_device->multicast_enabled)
1595 return 0;
1597 if (bridge_port->mrouter)
1598 return 0;
1600 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true);
1601 if (err) {
1602 netdev_err(dev, "Unable to set SMID\n");
1603 goto err_out;
1606 return 0;
1608 err_out:
1609 mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1610 return err;
1613 static void
1614 mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
1615 struct mlxsw_sp_bridge_device
1616 *bridge_device)
1618 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1619 struct mlxsw_sp_mid *mid;
1620 bool mc_enabled;
1622 mc_enabled = bridge_device->multicast_enabled;
1624 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1625 if (mc_enabled)
1626 mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid,
1627 bridge_device);
1628 else
1629 mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1633 static void
1634 mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
1635 struct mlxsw_sp_bridge_port *bridge_port,
1636 bool add)
1638 struct mlxsw_sp_bridge_device *bridge_device;
1639 struct mlxsw_sp_mid *mid;
1641 bridge_device = bridge_port->bridge_device;
1643 list_for_each_entry(mid, &bridge_device->mids_list, list) {
1644 if (!test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid))
1645 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, add);
1649 static int mlxsw_sp_port_obj_add(struct net_device *dev,
1650 const struct switchdev_obj *obj,
1651 struct switchdev_trans *trans)
1653 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1654 int err = 0;
1656 switch (obj->id) {
1657 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1658 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port,
1659 SWITCHDEV_OBJ_PORT_VLAN(obj),
1660 trans);
1661 break;
1662 case SWITCHDEV_OBJ_ID_PORT_MDB:
1663 err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
1664 SWITCHDEV_OBJ_PORT_MDB(obj),
1665 trans);
1666 break;
1667 default:
1668 err = -EOPNOTSUPP;
1669 break;
1672 return err;
1675 static void
1676 mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port *mlxsw_sp_port,
1677 struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
1679 u16 pvid = mlxsw_sp_port->pvid == vid ? 0 : vid;
1680 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1682 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1683 if (WARN_ON(!mlxsw_sp_port_vlan))
1684 return;
1686 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1687 mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1688 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1689 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1692 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1693 const struct switchdev_obj_port_vlan *vlan)
1695 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1696 struct net_device *orig_dev = vlan->obj.orig_dev;
1697 struct mlxsw_sp_bridge_port *bridge_port;
1698 u16 vid;
1700 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1701 if (WARN_ON(!bridge_port))
1702 return -EINVAL;
1704 if (!bridge_port->bridge_device->vlan_enabled)
1705 return 0;
1707 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++)
1708 mlxsw_sp_bridge_port_vlan_del(mlxsw_sp_port, bridge_port, vid);
1710 return 0;
1713 static int
1714 __mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1715 struct mlxsw_sp_bridge_port *bridge_port,
1716 struct mlxsw_sp_mid *mid)
1718 struct net_device *dev = mlxsw_sp_port->dev;
1719 int err;
1721 if (bridge_port->bridge_device->multicast_enabled) {
1722 if (bridge_port->bridge_device->multicast_enabled) {
1723 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid,
1724 false);
1725 if (err)
1726 netdev_err(dev, "Unable to remove port from SMID\n");
1730 err = mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
1731 if (err)
1732 netdev_err(dev, "Unable to remove MC SFD\n");
1734 return err;
1737 static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1738 const struct switchdev_obj_port_mdb *mdb)
1740 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1741 struct net_device *orig_dev = mdb->obj.orig_dev;
1742 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1743 struct mlxsw_sp_bridge_device *bridge_device;
1744 struct net_device *dev = mlxsw_sp_port->dev;
1745 struct mlxsw_sp_bridge_port *bridge_port;
1746 struct mlxsw_sp_mid *mid;
1747 u16 fid_index;
1749 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1750 if (!bridge_port)
1751 return 0;
1753 bridge_device = bridge_port->bridge_device;
1754 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1755 bridge_device,
1756 mdb->vid);
1757 if (!mlxsw_sp_port_vlan)
1758 return 0;
1760 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1762 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
1763 if (!mid) {
1764 netdev_err(dev, "Unable to remove port from MC DB\n");
1765 return -EINVAL;
1768 return __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port, mid);
1771 static void
1772 mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
1773 struct mlxsw_sp_bridge_port *bridge_port)
1775 struct mlxsw_sp_bridge_device *bridge_device;
1776 struct mlxsw_sp_mid *mid, *tmp;
1778 bridge_device = bridge_port->bridge_device;
1780 list_for_each_entry_safe(mid, tmp, &bridge_device->mids_list, list) {
1781 if (test_bit(mlxsw_sp_port->local_port, mid->ports_in_mid)) {
1782 __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port,
1783 mid);
1784 } else if (bridge_device->multicast_enabled &&
1785 bridge_port->mrouter) {
1786 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
1791 static int mlxsw_sp_port_obj_del(struct net_device *dev,
1792 const struct switchdev_obj *obj)
1794 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1795 int err = 0;
1797 switch (obj->id) {
1798 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1799 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1800 SWITCHDEV_OBJ_PORT_VLAN(obj));
1801 break;
1802 case SWITCHDEV_OBJ_ID_PORT_MDB:
1803 err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1804 SWITCHDEV_OBJ_PORT_MDB(obj));
1805 break;
1806 default:
1807 err = -EOPNOTSUPP;
1808 break;
1811 return err;
1814 static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
1815 u16 lag_id)
1817 struct mlxsw_sp_port *mlxsw_sp_port;
1818 u64 max_lag_members;
1819 int i;
1821 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1822 MAX_LAG_MEMBERS);
1823 for (i = 0; i < max_lag_members; i++) {
1824 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
1825 if (mlxsw_sp_port)
1826 return mlxsw_sp_port;
1828 return NULL;
1831 static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = {
1832 .switchdev_port_attr_get = mlxsw_sp_port_attr_get,
1833 .switchdev_port_attr_set = mlxsw_sp_port_attr_set,
1834 .switchdev_port_obj_add = mlxsw_sp_port_obj_add,
1835 .switchdev_port_obj_del = mlxsw_sp_port_obj_del,
1838 static int
1839 mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1840 struct mlxsw_sp_bridge_port *bridge_port,
1841 struct mlxsw_sp_port *mlxsw_sp_port,
1842 struct netlink_ext_ack *extack)
1844 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1846 if (is_vlan_dev(bridge_port->dev)) {
1847 NL_SET_ERR_MSG(extack, "spectrum: Can not enslave a VLAN device to a VLAN-aware bridge");
1848 return -EINVAL;
1851 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, 1);
1852 if (WARN_ON(!mlxsw_sp_port_vlan))
1853 return -EINVAL;
1855 /* Let VLAN-aware bridge take care of its own VLANs */
1856 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1858 return 0;
1861 static void
1862 mlxsw_sp_bridge_8021q_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
1863 struct mlxsw_sp_bridge_port *bridge_port,
1864 struct mlxsw_sp_port *mlxsw_sp_port)
1866 mlxsw_sp_port_vlan_get(mlxsw_sp_port, 1);
1867 /* Make sure untagged frames are allowed to ingress */
1868 mlxsw_sp_port_pvid_set(mlxsw_sp_port, 1);
1871 static struct mlxsw_sp_fid *
1872 mlxsw_sp_bridge_8021q_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
1873 u16 vid)
1875 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
1877 return mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
1880 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021q_ops = {
1881 .port_join = mlxsw_sp_bridge_8021q_port_join,
1882 .port_leave = mlxsw_sp_bridge_8021q_port_leave,
1883 .fid_get = mlxsw_sp_bridge_8021q_fid_get,
1886 static bool
1887 mlxsw_sp_port_is_br_member(const struct mlxsw_sp_port *mlxsw_sp_port,
1888 const struct net_device *br_dev)
1890 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1892 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
1893 list) {
1894 if (mlxsw_sp_port_vlan->bridge_port &&
1895 mlxsw_sp_port_vlan->bridge_port->bridge_device->dev ==
1896 br_dev)
1897 return true;
1900 return false;
1903 static int
1904 mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1905 struct mlxsw_sp_bridge_port *bridge_port,
1906 struct mlxsw_sp_port *mlxsw_sp_port,
1907 struct netlink_ext_ack *extack)
1909 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1910 u16 vid;
1912 if (!is_vlan_dev(bridge_port->dev)) {
1913 NL_SET_ERR_MSG(extack, "spectrum: Only VLAN devices can be enslaved to a VLAN-unaware bridge");
1914 return -EINVAL;
1916 vid = vlan_dev_vlan_id(bridge_port->dev);
1918 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1919 if (WARN_ON(!mlxsw_sp_port_vlan))
1920 return -EINVAL;
1922 if (mlxsw_sp_port_is_br_member(mlxsw_sp_port, bridge_device->dev)) {
1923 NL_SET_ERR_MSG(extack, "spectrum: Can not bridge VLAN uppers of the same port");
1924 return -EINVAL;
1927 /* Port is no longer usable as a router interface */
1928 if (mlxsw_sp_port_vlan->fid)
1929 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
1931 return mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
1934 static void
1935 mlxsw_sp_bridge_8021d_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
1936 struct mlxsw_sp_bridge_port *bridge_port,
1937 struct mlxsw_sp_port *mlxsw_sp_port)
1939 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1940 u16 vid = vlan_dev_vlan_id(bridge_port->dev);
1942 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1943 if (WARN_ON(!mlxsw_sp_port_vlan))
1944 return;
1946 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1949 static struct mlxsw_sp_fid *
1950 mlxsw_sp_bridge_8021d_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
1951 u16 vid)
1953 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
1955 return mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
1958 static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021d_ops = {
1959 .port_join = mlxsw_sp_bridge_8021d_port_join,
1960 .port_leave = mlxsw_sp_bridge_8021d_port_leave,
1961 .fid_get = mlxsw_sp_bridge_8021d_fid_get,
1964 int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port,
1965 struct net_device *brport_dev,
1966 struct net_device *br_dev,
1967 struct netlink_ext_ack *extack)
1969 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1970 struct mlxsw_sp_bridge_device *bridge_device;
1971 struct mlxsw_sp_bridge_port *bridge_port;
1972 int err;
1974 bridge_port = mlxsw_sp_bridge_port_get(mlxsw_sp->bridge, brport_dev);
1975 if (IS_ERR(bridge_port))
1976 return PTR_ERR(bridge_port);
1977 bridge_device = bridge_port->bridge_device;
1979 err = bridge_device->ops->port_join(bridge_device, bridge_port,
1980 mlxsw_sp_port, extack);
1981 if (err)
1982 goto err_port_join;
1984 return 0;
1986 err_port_join:
1987 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
1988 return err;
1991 void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port,
1992 struct net_device *brport_dev,
1993 struct net_device *br_dev)
1995 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1996 struct mlxsw_sp_bridge_device *bridge_device;
1997 struct mlxsw_sp_bridge_port *bridge_port;
1999 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
2000 if (!bridge_device)
2001 return;
2002 bridge_port = __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
2003 if (!bridge_port)
2004 return;
2006 bridge_device->ops->port_leave(bridge_device, bridge_port,
2007 mlxsw_sp_port);
2008 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
2011 static void
2012 mlxsw_sp_fdb_call_notifiers(enum switchdev_notifier_type type,
2013 const char *mac, u16 vid,
2014 struct net_device *dev)
2016 struct switchdev_notifier_fdb_info info;
2018 info.addr = mac;
2019 info.vid = vid;
2020 call_switchdev_notifiers(type, dev, &info.info);
2023 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
2024 char *sfn_pl, int rec_index,
2025 bool adding)
2027 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2028 struct mlxsw_sp_bridge_device *bridge_device;
2029 struct mlxsw_sp_bridge_port *bridge_port;
2030 struct mlxsw_sp_port *mlxsw_sp_port;
2031 enum switchdev_notifier_type type;
2032 char mac[ETH_ALEN];
2033 u8 local_port;
2034 u16 vid, fid;
2035 bool do_notification = true;
2036 int err;
2038 mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
2039 mlxsw_sp_port = mlxsw_sp->ports[local_port];
2040 if (!mlxsw_sp_port) {
2041 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
2042 goto just_remove;
2045 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2046 if (!mlxsw_sp_port_vlan) {
2047 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2048 goto just_remove;
2051 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2052 if (!bridge_port) {
2053 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2054 goto just_remove;
2057 bridge_device = bridge_port->bridge_device;
2058 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2060 do_fdb_op:
2061 err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
2062 adding, true);
2063 if (err) {
2064 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2065 return;
2068 if (!do_notification)
2069 return;
2070 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2071 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev);
2073 return;
2075 just_remove:
2076 adding = false;
2077 do_notification = false;
2078 goto do_fdb_op;
2081 static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
2082 char *sfn_pl, int rec_index,
2083 bool adding)
2085 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
2086 struct mlxsw_sp_bridge_device *bridge_device;
2087 struct mlxsw_sp_bridge_port *bridge_port;
2088 struct mlxsw_sp_port *mlxsw_sp_port;
2089 enum switchdev_notifier_type type;
2090 char mac[ETH_ALEN];
2091 u16 lag_vid = 0;
2092 u16 lag_id;
2093 u16 vid, fid;
2094 bool do_notification = true;
2095 int err;
2097 mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
2098 mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
2099 if (!mlxsw_sp_port) {
2100 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
2101 goto just_remove;
2104 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
2105 if (!mlxsw_sp_port_vlan) {
2106 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
2107 goto just_remove;
2110 bridge_port = mlxsw_sp_port_vlan->bridge_port;
2111 if (!bridge_port) {
2112 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
2113 goto just_remove;
2116 bridge_device = bridge_port->bridge_device;
2117 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
2118 lag_vid = mlxsw_sp_port_vlan->vid;
2120 do_fdb_op:
2121 err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
2122 adding, true);
2123 if (err) {
2124 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
2125 return;
2128 if (!do_notification)
2129 return;
2130 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2131 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev);
2133 return;
2135 just_remove:
2136 adding = false;
2137 do_notification = false;
2138 goto do_fdb_op;
2141 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
2142 char *sfn_pl, int rec_index)
2144 switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
2145 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
2146 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2147 rec_index, true);
2148 break;
2149 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
2150 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
2151 rec_index, false);
2152 break;
2153 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
2154 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2155 rec_index, true);
2156 break;
2157 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
2158 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
2159 rec_index, false);
2160 break;
2164 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp)
2166 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2168 mlxsw_core_schedule_dw(&bridge->fdb_notify.dw,
2169 msecs_to_jiffies(bridge->fdb_notify.interval));
2172 static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
2174 struct mlxsw_sp_bridge *bridge;
2175 struct mlxsw_sp *mlxsw_sp;
2176 char *sfn_pl;
2177 u8 num_rec;
2178 int i;
2179 int err;
2181 sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
2182 if (!sfn_pl)
2183 return;
2185 bridge = container_of(work, struct mlxsw_sp_bridge, fdb_notify.dw.work);
2186 mlxsw_sp = bridge->mlxsw_sp;
2188 rtnl_lock();
2189 mlxsw_reg_sfn_pack(sfn_pl);
2190 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
2191 if (err) {
2192 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
2193 goto out;
2195 num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
2196 for (i = 0; i < num_rec; i++)
2197 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
2199 out:
2200 rtnl_unlock();
2201 kfree(sfn_pl);
2202 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2205 struct mlxsw_sp_switchdev_event_work {
2206 struct work_struct work;
2207 struct switchdev_notifier_fdb_info fdb_info;
2208 struct net_device *dev;
2209 unsigned long event;
2212 static void mlxsw_sp_switchdev_event_work(struct work_struct *work)
2214 struct mlxsw_sp_switchdev_event_work *switchdev_work =
2215 container_of(work, struct mlxsw_sp_switchdev_event_work, work);
2216 struct net_device *dev = switchdev_work->dev;
2217 struct switchdev_notifier_fdb_info *fdb_info;
2218 struct mlxsw_sp_port *mlxsw_sp_port;
2219 int err;
2221 rtnl_lock();
2222 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
2223 if (!mlxsw_sp_port)
2224 goto out;
2226 switch (switchdev_work->event) {
2227 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2228 fdb_info = &switchdev_work->fdb_info;
2229 err = mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, true);
2230 if (err)
2231 break;
2232 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
2233 fdb_info->addr,
2234 fdb_info->vid, dev);
2235 break;
2236 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2237 fdb_info = &switchdev_work->fdb_info;
2238 mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, false);
2239 break;
2242 out:
2243 rtnl_unlock();
2244 kfree(switchdev_work->fdb_info.addr);
2245 kfree(switchdev_work);
2246 dev_put(dev);
2249 /* Called under rcu_read_lock() */
2250 static int mlxsw_sp_switchdev_event(struct notifier_block *unused,
2251 unsigned long event, void *ptr)
2253 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2254 struct mlxsw_sp_switchdev_event_work *switchdev_work;
2255 struct switchdev_notifier_fdb_info *fdb_info = ptr;
2257 if (!mlxsw_sp_port_dev_lower_find_rcu(dev))
2258 return NOTIFY_DONE;
2260 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2261 if (!switchdev_work)
2262 return NOTIFY_BAD;
2264 INIT_WORK(&switchdev_work->work, mlxsw_sp_switchdev_event_work);
2265 switchdev_work->dev = dev;
2266 switchdev_work->event = event;
2268 switch (event) {
2269 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
2270 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2271 memcpy(&switchdev_work->fdb_info, ptr,
2272 sizeof(switchdev_work->fdb_info));
2273 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
2274 if (!switchdev_work->fdb_info.addr)
2275 goto err_addr_alloc;
2276 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
2277 fdb_info->addr);
2278 /* Take a reference on the device. This can be either
2279 * upper device containig mlxsw_sp_port or just a
2280 * mlxsw_sp_port
2282 dev_hold(dev);
2283 break;
2284 default:
2285 kfree(switchdev_work);
2286 return NOTIFY_DONE;
2289 mlxsw_core_schedule_work(&switchdev_work->work);
2291 return NOTIFY_DONE;
2293 err_addr_alloc:
2294 kfree(switchdev_work);
2295 return NOTIFY_BAD;
2298 static struct notifier_block mlxsw_sp_switchdev_notifier = {
2299 .notifier_call = mlxsw_sp_switchdev_event,
2302 static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
2304 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
2305 int err;
2307 err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
2308 if (err) {
2309 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
2310 return err;
2313 err = register_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
2314 if (err) {
2315 dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev notifier\n");
2316 return err;
2319 INIT_DELAYED_WORK(&bridge->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
2320 bridge->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
2321 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2322 return 0;
2325 static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
2327 cancel_delayed_work_sync(&mlxsw_sp->bridge->fdb_notify.dw);
2328 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
2332 int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
2334 struct mlxsw_sp_bridge *bridge;
2336 bridge = kzalloc(sizeof(*mlxsw_sp->bridge), GFP_KERNEL);
2337 if (!bridge)
2338 return -ENOMEM;
2339 mlxsw_sp->bridge = bridge;
2340 bridge->mlxsw_sp = mlxsw_sp;
2342 INIT_LIST_HEAD(&mlxsw_sp->bridge->bridges_list);
2344 bridge->bridge_8021q_ops = &mlxsw_sp_bridge_8021q_ops;
2345 bridge->bridge_8021d_ops = &mlxsw_sp_bridge_8021d_ops;
2347 return mlxsw_sp_fdb_init(mlxsw_sp);
2350 void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
2352 mlxsw_sp_fdb_fini(mlxsw_sp);
2353 WARN_ON(!list_empty(&mlxsw_sp->bridge->bridges_list));
2354 kfree(mlxsw_sp->bridge);
2357 void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port *mlxsw_sp_port)
2359 mlxsw_sp_port->dev->switchdev_ops = &mlxsw_sp_port_switchdev_ops;
2362 void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port)