blob: 593ad31be7490d6af8abf3f5c3929de925c0ca77 [file] [log] [blame]
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001/*
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>
Ido Schimmel4f2c6ae2016-01-27 15:16:43 +010048#include <linux/rtnetlink.h>
Ido Schimmel9b63ef882017-10-08 11:57:56 +020049#include <linux/netlink.h>
Jiri Pirko56ade8f2015-10-16 14:01:37 +020050#include <net/switchdev.h>
51
Yotam Gigic4db9532017-10-09 11:15:34 +020052#include "spectrum_router.h"
Jiri Pirko56ade8f2015-10-16 14:01:37 +020053#include "spectrum.h"
54#include "core.h"
55#include "reg.h"
56
Ido Schimmelc57529e2017-05-26 08:37:31 +020057struct mlxsw_sp_bridge_ops;
58
Ido Schimmel5f6935c2017-05-16 19:38:26 +020059struct 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;
Ido Schimmelc57529e2017-05-26 08:37:31 +020070 bool vlan_enabled_exists;
71 struct list_head bridges_list;
Ido Schimmel5f6935c2017-05-16 19:38:26 +020072 DECLARE_BITMAP(mids_bitmap, MLXSW_SP_MID_MAX);
Ido Schimmelc57529e2017-05-26 08:37:31 +020073 const struct mlxsw_sp_bridge_ops *bridge_8021q_ops;
74 const struct mlxsw_sp_bridge_ops *bridge_8021d_ops;
Ido Schimmel5f6935c2017-05-16 19:38:26 +020075};
76
Ido Schimmelc57529e2017-05-26 08:37:31 +020077struct mlxsw_sp_bridge_device {
78 struct net_device *dev;
79 struct list_head list;
80 struct list_head ports_list;
Nogah Frankelb80888a2017-09-20 16:15:04 +020081 struct list_head mids_list;
Ido Schimmelc57529e2017-05-26 08:37:31 +020082 u8 vlan_enabled:1,
Yotam Gigi593bc282017-10-09 11:15:35 +020083 multicast_enabled:1,
84 mrouter:1;
Ido Schimmelc57529e2017-05-26 08:37:31 +020085 const struct mlxsw_sp_bridge_ops *ops;
86};
Ido Schimmel5f6935c2017-05-16 19:38:26 +020087
Ido Schimmelc57529e2017-05-26 08:37:31 +020088struct 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};
Elad Raze4b6f692016-01-10 21:06:27 +0100103
Ido Schimmelc57529e2017-05-26 08:37:31 +0200104struct mlxsw_sp_bridge_vlan {
105 struct list_head list;
106 struct list_head port_vlan_list;
Ido Schimmel54a73202015-12-15 16:03:41 +0100107 u16 vid;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200108};
Ido Schimmel54a73202015-12-15 16:03:41 +0100109
Ido Schimmelc57529e2017-05-26 08:37:31 +0200110struct mlxsw_sp_bridge_ops {
111 int (*port_join)(struct mlxsw_sp_bridge_device *bridge_device,
112 struct mlxsw_sp_bridge_port *bridge_port,
Ido Schimmel9b63ef882017-10-08 11:57:56 +0200113 struct mlxsw_sp_port *mlxsw_sp_port,
114 struct netlink_ext_ack *extack);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200115 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);
Ido Schimmela1107482017-05-26 08:37:39 +0200118 struct mlxsw_sp_fid *
119 (*fid_get)(struct mlxsw_sp_bridge_device *bridge_device,
120 u16 vid);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200121};
122
123static int
124mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
125 struct mlxsw_sp_bridge_port *bridge_port,
126 u16 fid_index);
127
Nogah Frankel2e3496c2017-09-20 16:15:09 +0200128static void
Nogah Frankelbb5355b2017-09-20 16:15:12 +0200129mlxsw_sp_bridge_port_mdb_flush(struct mlxsw_sp_port *mlxsw_sp_port,
130 struct mlxsw_sp_bridge_port *bridge_port);
131
132static void
Nogah Frankel2e3496c2017-09-20 16:15:09 +0200133mlxsw_sp_bridge_mdb_mc_enable_sync(struct mlxsw_sp_port *mlxsw_sp_port,
134 struct mlxsw_sp_bridge_device
135 *bridge_device);
136
Nogah Frankel3ddda112017-09-20 16:15:14 +0200137static void
138mlxsw_sp_port_mrouter_update_mdb(struct mlxsw_sp_port *mlxsw_sp_port,
139 struct mlxsw_sp_bridge_port *bridge_port,
140 bool add);
141
Ido Schimmelc57529e2017-05-26 08:37:31 +0200142static struct mlxsw_sp_bridge_device *
143mlxsw_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
Ido Schimmel90045fc2017-12-25 09:05:33 +0100155bool 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
Ido Schimmelc57529e2017-05-26 08:37:31 +0200161static struct mlxsw_sp_bridge_device *
162mlxsw_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);
Nogah Frankel1e5d9432017-02-09 14:54:48 +0100172 }
173
Ido Schimmelc57529e2017-05-26 08:37:31 +0200174 bridge_device = kzalloc(sizeof(*bridge_device), GFP_KERNEL);
175 if (!bridge_device)
176 return ERR_PTR(-ENOMEM);
Ido Schimmel54a73202015-12-15 16:03:41 +0100177
Ido Schimmelc57529e2017-05-26 08:37:31 +0200178 bridge_device->dev = br_dev;
179 bridge_device->vlan_enabled = vlan_enabled;
180 bridge_device->multicast_enabled = br_multicast_enabled(br_dev);
Yotam Gigi593bc282017-10-09 11:15:35 +0200181 bridge_device->mrouter = br_multicast_router(br_dev);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200182 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 }
Nogah Frankelb80888a2017-09-20 16:15:04 +0200189 INIT_LIST_HEAD(&bridge_device->mids_list);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200190 list_add(&bridge_device->list, &bridge->bridges_list);
Ido Schimmel54a73202015-12-15 16:03:41 +0100191
Ido Schimmelc57529e2017-05-26 08:37:31 +0200192 return bridge_device;
193}
194
195static void
196mlxsw_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));
Nogah Frankelbb5355b2017-09-20 16:15:12 +0200203 WARN_ON(!list_empty(&bridge_device->mids_list));
Ido Schimmelc57529e2017-05-26 08:37:31 +0200204 kfree(bridge_device);
205}
206
207static struct mlxsw_sp_bridge_device *
208mlxsw_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
220static void
221mlxsw_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
228static 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
242static struct mlxsw_sp_bridge_port *
243mlxsw_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
259static struct mlxsw_sp_bridge_port *
260mlxsw_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;
Nogah Frankel9dad51b2017-09-20 16:15:11 +0200279 bridge_port->flags = BR_LEARNING | BR_FLOOD | BR_LEARNING_SYNC |
280 BR_MCAST_FLOOD;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200281 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
288static void
289mlxsw_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
296static bool
297mlxsw_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
314static struct mlxsw_sp_bridge_port *
315mlxsw_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
341err_bridge_port_create:
342 mlxsw_sp_bridge_device_put(bridge, bridge_device);
343 return ERR_PTR(err);
344}
345
346static 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
359static struct mlxsw_sp_port_vlan *
360mlxsw_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
383static struct mlxsw_sp_port_vlan*
384mlxsw_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
Ido Schimmela1107482017-05-26 08:37:39 +0200393 if (fid && mlxsw_sp_fid_index(fid) == fid_index)
Ido Schimmelc57529e2017-05-26 08:37:31 +0200394 return mlxsw_sp_port_vlan;
395 }
396
397 return NULL;
398}
399
400static struct mlxsw_sp_bridge_vlan *
401mlxsw_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
414static struct mlxsw_sp_bridge_vlan *
415mlxsw_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
430static void
431mlxsw_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
438static struct mlxsw_sp_bridge_vlan *
439mlxsw_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
450static 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
456static 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));
Ido Schimmel54a73202015-12-15 16:03:41 +0100467}
468
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200469static 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:
Ido Schimmelc57529e2017-05-26 08:37:31 +0200482 mlxsw_sp_port_bridge_flags_get(mlxsw_sp->bridge, attr->orig_dev,
483 &attr->u.brport_flags);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200484 break;
Arkadi Sharshevskyc7b566c2017-06-08 08:44:17 +0200485 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
Nogah Frankel9dad51b2017-09-20 16:15:11 +0200486 attr->u.brport_flags_support = BR_LEARNING | BR_FLOOD |
487 BR_MCAST_FLOOD;
Arkadi Sharshevskyc7b566c2017-06-08 08:44:17 +0200488 break;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200489 default:
490 return -EOPNOTSUPP;
491 }
492
493 return 0;
494}
495
Ido Schimmelc57529e2017-05-26 08:37:31 +0200496static int
497mlxsw_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
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200514static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
515 struct switchdev_trans *trans,
Ido Schimmelc57529e2017-05-26 08:37:31 +0200516 struct net_device *orig_dev,
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200517 u8 state)
518{
Ido Schimmelc57529e2017-05-26 08:37:31 +0200519 struct mlxsw_sp_bridge_port *bridge_port;
520 struct mlxsw_sp_bridge_vlan *bridge_vlan;
Ido Schimmel45bfe6b2017-05-16 19:38:32 +0200521 int err;
522
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200523 if (switchdev_trans_ph_prepare(trans))
524 return 0;
525
Ido Schimmelc57529e2017-05-26 08:37:31 +0200526 /* 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)
Ido Schimmel45bfe6b2017-05-16 19:38:32 +0200532 return 0;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200533
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;
Ido Schimmel45bfe6b2017-05-16 19:38:32 +0200539 }
540
Ido Schimmelc57529e2017-05-26 08:37:31 +0200541 bridge_port->stp_state = state;
Ido Schimmel45bfe6b2017-05-16 19:38:32 +0200542
543 return 0;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200544
545err_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;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200551}
552
Ido Schimmelc57529e2017-05-26 08:37:31 +0200553static int
554mlxsw_sp_port_bridge_vlan_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
555 struct mlxsw_sp_bridge_vlan *bridge_vlan,
Ido Schimmela1107482017-05-26 08:37:39 +0200556 enum mlxsw_sp_flood_type packet_type,
Ido Schimmelc57529e2017-05-26 08:37:31 +0200557 bool member)
Nogah Frankeleaa7df32017-02-09 14:54:43 +0100558{
Ido Schimmelc57529e2017-05-26 08:37:31 +0200559 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
Nogah Frankeleaa7df32017-02-09 14:54:43 +0100560
Ido Schimmelc57529e2017-05-26 08:37:31 +0200561 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;
Ido Schimmela1107482017-05-26 08:37:39 +0200565 return mlxsw_sp_fid_flood_set(mlxsw_sp_port_vlan->fid,
566 packet_type,
567 mlxsw_sp_port->local_port,
568 member);
Ido Schimmel54a73202015-12-15 16:03:41 +0100569 }
570
Ido Schimmelc57529e2017-05-26 08:37:31 +0200571 return 0;
572}
573
574static int
575mlxsw_sp_bridge_port_flood_table_set(struct mlxsw_sp_port *mlxsw_sp_port,
576 struct mlxsw_sp_bridge_port *bridge_port,
Ido Schimmela1107482017-05-26 08:37:39 +0200577 enum mlxsw_sp_flood_type packet_type,
Ido Schimmelc57529e2017-05-26 08:37:31 +0200578 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,
Ido Schimmela1107482017-05-26 08:37:39 +0200585 bridge_vlan,
586 packet_type,
Ido Schimmelc57529e2017-05-26 08:37:31 +0200587 member);
588 if (err)
589 goto err_port_bridge_vlan_flood_set;
Ido Schimmel02930382015-10-28 10:16:58 +0100590 }
591
592 return 0;
593
Ido Schimmelc57529e2017-05-26 08:37:31 +0200594err_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,
Ido Schimmela1107482017-05-26 08:37:39 +0200598 packet_type, !member);
Ido Schimmel02930382015-10-28 10:16:58 +0100599 return err;
600}
601
Ido Schimmelc57529e2017-05-26 08:37:31 +0200602static int
603mlxsw_sp_port_bridge_vlan_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
604 struct mlxsw_sp_bridge_vlan *bridge_vlan,
605 bool set)
Nogah Frankel90e0f0c2017-02-09 14:54:49 +0100606{
Ido Schimmelc57529e2017-05-26 08:37:31 +0200607 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
608 u16 vid = bridge_vlan->vid;
Nogah Frankel90e0f0c2017-02-09 14:54:49 +0100609
Ido Schimmelc57529e2017-05-26 08:37:31 +0200610 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;
Ido Schimmel7cbc4272017-05-16 19:38:33 +0200614 return mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, set);
Ido Schimmel89b548f2016-08-24 12:00:27 +0200615 }
616
Ido Schimmelc57529e2017-05-26 08:37:31 +0200617 return 0;
618}
619
620static int
621mlxsw_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);
Ido Schimmel89b548f2016-08-24 12:00:27 +0200631 if (err)
Ido Schimmelc57529e2017-05-26 08:37:31 +0200632 goto err_port_bridge_vlan_learning_set;
Ido Schimmel89b548f2016-08-24 12:00:27 +0200633 }
634
635 return 0;
636
Ido Schimmelc57529e2017-05-26 08:37:31 +0200637err_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);
Ido Schimmel89b548f2016-08-24 12:00:27 +0200642 return err;
643}
644
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200645static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
646 struct switchdev_trans *trans,
Ido Schimmelc57529e2017-05-26 08:37:31 +0200647 struct net_device *orig_dev,
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200648 unsigned long brport_flags)
649{
Ido Schimmelc57529e2017-05-26 08:37:31 +0200650 struct mlxsw_sp_bridge_port *bridge_port;
Ido Schimmel02930382015-10-28 10:16:58 +0100651 int err;
652
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200653 if (switchdev_trans_ph_prepare(trans))
654 return 0;
655
Ido Schimmelc57529e2017-05-26 08:37:31 +0200656 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
657 orig_dev);
Ido Schimmel17b334a2017-08-04 14:12:29 +0200658 if (!bridge_port)
659 return 0;
Ido Schimmel02930382015-10-28 10:16:58 +0100660
Ido Schimmelc57529e2017-05-26 08:37:31 +0200661 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
Ido Schimmela1107482017-05-26 08:37:39 +0200662 MLXSW_SP_FLOOD_TYPE_UC,
Ido Schimmelc57529e2017-05-26 08:37:31 +0200663 brport_flags & BR_FLOOD);
664 if (err)
665 return err;
Ido Schimmel89b548f2016-08-24 12:00:27 +0200666
Ido Schimmelc57529e2017-05-26 08:37:31 +0200667 err = mlxsw_sp_bridge_port_learning_set(mlxsw_sp_port, bridge_port,
668 brport_flags & BR_LEARNING);
669 if (err)
670 return err;
671
Nogah Frankel9dad51b2017-09-20 16:15:11 +0200672 if (bridge_port->bridge_device->multicast_enabled)
673 goto out;
Ido Schimmel02930382015-10-28 10:16:58 +0100674
Nogah Frankel9dad51b2017-09-20 16:15:11 +0200675 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
682out:
683 memcpy(&bridge_port->flags, &brport_flags, sizeof(brport_flags));
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200684 return 0;
685}
686
687static 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;
Ido Schimmel5f6935c2017-05-16 19:38:26 +0200696 mlxsw_sp->bridge->ageing_time = ageing_time;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200697 return 0;
698}
699
700static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
701 struct switchdev_trans *trans,
Jiri Pirko135f9ec2015-10-28 10:17:02 +0100702 unsigned long ageing_clock_t)
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200703{
704 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
Jiri Pirko135f9ec2015-10-28 10:17:02 +0100705 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200706 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
707
Ido Schimmel869f63a2016-03-08 12:59:33 -0800708 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 }
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200715
716 return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
717}
718
Elad Raz26a4ea02016-01-06 13:01:10 +0100719static 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;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200725 struct mlxsw_sp_bridge_device *bridge_device;
Elad Raz26a4ea02016-01-06 13:01:10 +0100726
Ido Schimmelc57529e2017-05-26 08:37:31 +0200727 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))
Elad Raz26a4ea02016-01-06 13:01:10 +0100732 return -EINVAL;
Elad Raz26a4ea02016-01-06 13:01:10 +0100733
Ido Schimmelc57529e2017-05-26 08:37:31 +0200734 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;
Elad Raz26a4ea02016-01-06 13:01:10 +0100739}
740
Nogah Frankeldff37b52017-09-20 16:15:01 +0200741static 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)
Nogah Frankel8ecd4592017-02-09 14:54:47 +0100745{
Ido Schimmelc57529e2017-05-26 08:37:31 +0200746 struct mlxsw_sp_bridge_port *bridge_port;
Nogah Frankel4eb6a3b2017-08-22 10:28:11 +0200747 int err;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200748
Nogah Frankel8ecd4592017-02-09 14:54:47 +0100749 if (switchdev_trans_ph_prepare(trans))
750 return 0;
751
Ido Schimmelc57529e2017-05-26 08:37:31 +0200752 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
753 orig_dev);
Ido Schimmel17b334a2017-08-04 14:12:29 +0200754 if (!bridge_port)
755 return 0;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200756
757 if (!bridge_port->bridge_device->multicast_enabled)
Nogah Frankel4eb6a3b2017-08-22 10:28:11 +0200758 goto out;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200759
Nogah Frankel4eb6a3b2017-08-22 10:28:11 +0200760 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
761 MLXSW_SP_FLOOD_TYPE_MC,
Nogah Frankeldff37b52017-09-20 16:15:01 +0200762 is_port_mrouter);
Nogah Frankel4eb6a3b2017-08-22 10:28:11 +0200763 if (err)
764 return err;
765
Nogah Frankel3ddda112017-09-20 16:15:14 +0200766 mlxsw_sp_port_mrouter_update_mdb(mlxsw_sp_port, bridge_port,
767 is_port_mrouter);
Nogah Frankel4eb6a3b2017-08-22 10:28:11 +0200768out:
Nogah Frankeldff37b52017-09-20 16:15:01 +0200769 bridge_port->mrouter = is_port_mrouter;
Nogah Frankel4eb6a3b2017-08-22 10:28:11 +0200770 return 0;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200771}
772
Nogah Frankel218a8f82017-09-20 16:15:10 +0200773static 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;
Nogah Frankel9dad51b2017-09-20 16:15:11 +0200778 return bridge_device->multicast_enabled ? bridge_port->mrouter :
779 bridge_port->flags & BR_MCAST_FLOOD;
Nogah Frankel218a8f82017-09-20 16:15:10 +0200780}
781
Ido Schimmelc57529e2017-05-26 08:37:31 +0200782static 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
Nogah Frankel2e3496c2017-09-20 16:15:09 +0200802 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
Ido Schimmelc57529e2017-05-26 08:37:31 +0200808 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
Ido Schimmela1107482017-05-26 08:37:39 +0200809 enum mlxsw_sp_flood_type packet_type = MLXSW_SP_FLOOD_TYPE_MC;
Nogah Frankel218a8f82017-09-20 16:15:10 +0200810 bool member = mlxsw_sp_mc_flood(bridge_port);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200811
812 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
Ido Schimmela1107482017-05-26 08:37:39 +0200813 bridge_port,
814 packet_type, member);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200815 if (err)
816 return err;
817 }
818
819 bridge_device->multicast_enabled = !mc_disabled;
Nogah Frankel8ecd4592017-02-09 14:54:47 +0100820
821 return 0;
822}
823
Yotam Gigi593bc282017-10-09 11:15:35 +0200824static 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
841static void
842mlxsw_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
852static int
853mlxsw_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
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200878static 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);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200883 int err;
Ido Schimmel54a73202015-12-15 16:03:41 +0100884
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200885 switch (attr->id) {
886 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
887 err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port, trans,
Ido Schimmelc57529e2017-05-26 08:37:31 +0200888 attr->orig_dev,
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200889 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,
Ido Schimmelc57529e2017-05-26 08:37:31 +0200893 attr->orig_dev,
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200894 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;
Elad Raz26a4ea02016-01-06 13:01:10 +0100900 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;
Nogah Frankel8ecd4592017-02-09 14:54:47 +0100905 case SWITCHDEV_ATTR_ID_PORT_MROUTER:
Nogah Frankeldff37b52017-09-20 16:15:01 +0200906 err = mlxsw_sp_port_attr_mrouter_set(mlxsw_sp_port, trans,
907 attr->orig_dev,
908 attr->u.mrouter);
Nogah Frankel8ecd4592017-02-09 14:54:47 +0100909 break;
Nogah Frankel90e0f0c2017-02-09 14:54:49 +0100910 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
911 err = mlxsw_sp_port_mc_disabled_set(mlxsw_sp_port, trans,
Ido Schimmelc57529e2017-05-26 08:37:31 +0200912 attr->orig_dev,
Nogah Frankel90e0f0c2017-02-09 14:54:49 +0100913 attr->u.mc_disabled);
914 break;
Yotam Gigi593bc282017-10-09 11:15:35 +0200915 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;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200920 default:
921 err = -EOPNOTSUPP;
922 break;
923 }
924
925 return err;
926}
927
Ido Schimmelc57529e2017-05-26 08:37:31 +0200928static int
929mlxsw_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;
Ido Schimmela1107482017-05-26 08:37:39 +0200933 struct mlxsw_sp_bridge_device *bridge_device;
934 u8 local_port = mlxsw_sp_port->local_port;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200935 u16 vid = mlxsw_sp_port_vlan->vid;
936 struct mlxsw_sp_fid *fid;
937 int err;
938
Ido Schimmela1107482017-05-26 08:37:39 +0200939 bridge_device = bridge_port->bridge_device;
940 fid = bridge_device->ops->fid_get(bridge_device, vid);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200941 if (IS_ERR(fid))
942 return PTR_ERR(fid);
943
Ido Schimmela1107482017-05-26 08:37:39 +0200944 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port,
945 bridge_port->flags & BR_FLOOD);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200946 if (err)
Ido Schimmela1107482017-05-26 08:37:39 +0200947 goto err_fid_uc_flood_set;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200948
Ido Schimmela1107482017-05-26 08:37:39 +0200949 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port,
950 mlxsw_sp_mc_flood(bridge_port));
Ido Schimmelc57529e2017-05-26 08:37:31 +0200951 if (err)
Ido Schimmela1107482017-05-26 08:37:39 +0200952 goto err_fid_mc_flood_set;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200953
Ido Schimmela1107482017-05-26 08:37:39 +0200954 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port,
955 true);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200956 if (err)
Ido Schimmela1107482017-05-26 08:37:39 +0200957 goto err_fid_bc_flood_set;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200958
Ido Schimmela1107482017-05-26 08:37:39 +0200959 err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200960 if (err)
Ido Schimmela1107482017-05-26 08:37:39 +0200961 goto err_fid_port_vid_map;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200962
963 mlxsw_sp_port_vlan->fid = fid;
964
965 return 0;
966
Ido Schimmela1107482017-05-26 08:37:39 +0200967err_fid_port_vid_map:
968 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
969err_fid_bc_flood_set:
970 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
971err_fid_mc_flood_set:
972 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
973err_fid_uc_flood_set:
974 mlxsw_sp_fid_put(fid);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200975 return err;
976}
977
978static void
979mlxsw_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;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200982 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
Ido Schimmela1107482017-05-26 08:37:39 +0200983 u8 local_port = mlxsw_sp_port->local_port;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200984 u16 vid = mlxsw_sp_port_vlan->vid;
985
986 mlxsw_sp_port_vlan->fid = NULL;
Ido Schimmela1107482017-05-26 08:37:39 +0200987 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);
Ido Schimmel14d39462016-06-20 23:04:15 +0200992}
993
Ido Schimmelfe9ccc72017-05-16 19:38:31 +0200994static u16
995mlxsw_sp_port_pvid_determine(const struct mlxsw_sp_port *mlxsw_sp_port,
996 u16 vid, bool is_pvid)
Ido Schimmel584d73d2016-08-24 12:00:26 +0200997{
Ido Schimmelfe9ccc72017-05-16 19:38:31 +0200998 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;
Ido Schimmel584d73d2016-08-24 12:00:26 +02001004}
1005
Ido Schimmelc57529e2017-05-26 08:37:31 +02001006static int
1007mlxsw_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
1048err_bridge_vlan_get:
1049 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
1050err_port_vid_stp_set:
1051 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
1052err_port_vid_learning_set:
1053 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
1054 return err;
1055}
1056
1057void
1058mlxsw_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;
Nogah Frankelbb5355b2017-09-20 16:15:12 +02001065 bool last_port, last_vlan;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001066
Ido Schimmela1107482017-05-26 08:37:39 +02001067 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
Ido Schimmelc57529e2017-05-26 08:37:31 +02001071 bridge_port = mlxsw_sp_port_vlan->bridge_port;
Nogah Frankelbb5355b2017-09-20 16:15:12 +02001072 last_vlan = list_is_singular(&bridge_port->vlans_list);
Ido Schimmelc57529e2017-05-26 08:37:31 +02001073 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
Nogah Frankelbb5355b2017-09-20 16:15:12 +02001074 last_port = list_is_singular(&bridge_vlan->port_vlan_list);
Ido Schimmelc57529e2017-05-26 08:37:31 +02001075
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);
Nogah Frankelbb5355b2017-09-20 16:15:12 +02001080 if (last_port)
Ido Schimmelc57529e2017-05-26 08:37:31 +02001081 mlxsw_sp_bridge_port_fdb_flush(mlxsw_sp_port->mlxsw_sp,
Ido Schimmela1107482017-05-26 08:37:39 +02001082 bridge_port,
1083 mlxsw_sp_fid_index(fid));
Nogah Frankelbb5355b2017-09-20 16:15:12 +02001084 if (last_vlan)
1085 mlxsw_sp_bridge_port_mdb_flush(mlxsw_sp_port, bridge_port);
1086
Ido Schimmelc57529e2017-05-26 08:37:31 +02001087 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
1093static int
1094mlxsw_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)
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001097{
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001098 u16 pvid = mlxsw_sp_port_pvid_determine(mlxsw_sp_port, vid, is_pvid);
Ido Schimmelc57529e2017-05-26 08:37:31 +02001099 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001100 u16 old_pvid = mlxsw_sp_port->pvid;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001101 int err;
1102
Ido Schimmelc57529e2017-05-26 08:37:31 +02001103 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);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001106
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001107 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true,
1108 is_untagged);
1109 if (err)
1110 goto err_port_vlan_set;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001111
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001112 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1113 if (err)
1114 goto err_port_pvid_set;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001115
Ido Schimmelc57529e2017-05-26 08:37:31 +02001116 err = mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001117 if (err)
Ido Schimmelc57529e2017-05-26 08:37:31 +02001118 goto err_port_vlan_bridge_join;
Ido Schimmel584d73d2016-08-24 12:00:26 +02001119
Ido Schimmelb07a9662015-11-19 12:27:40 +01001120 return 0;
1121
Ido Schimmelc57529e2017-05-26 08:37:31 +02001122err_port_vlan_bridge_join:
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001123 mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid);
Ido Schimmelb07a9662015-11-19 12:27:40 +01001124err_port_pvid_set:
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001125 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1126err_port_vlan_set:
Ido Schimmelc57529e2017-05-26 08:37:31 +02001127 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
Ido Schimmelb07a9662015-11-19 12:27:40 +01001128 return err;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001129}
1130
1131static 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{
Elad Raze4a13052016-01-06 13:01:09 +01001135 bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1136 bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001137 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;
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001140 u16 vid;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001141
1142 if (switchdev_trans_ph_prepare(trans))
1143 return 0;
1144
Ido Schimmelc57529e2017-05-26 08:37:31 +02001145 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
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001152 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1153 int err;
1154
Ido Schimmelc57529e2017-05-26 08:37:31 +02001155 err = mlxsw_sp_bridge_port_vlan_add(mlxsw_sp_port, bridge_port,
1156 vid, flag_untagged,
1157 flag_pvid);
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001158 if (err)
1159 return err;
1160 }
1161
1162 return 0;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001163}
1164
Ido Schimmelc57529e2017-05-26 08:37:31 +02001165static 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
1171static int
1172mlxsw_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
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001188static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001189{
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001190 return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
1191 MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY;
1192}
1193
1194static 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
Ido Schimmel6e095fd2016-07-04 08:23:13 +02001200static 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)
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001204{
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001205 char *sfd_pl;
1206 int err;
1207
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001208 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1209 if (!sfd_pl)
1210 return -ENOMEM;
1211
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001212 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),
Ido Schimmel6e095fd2016-07-04 08:23:13 +02001214 mac, fid, action, local_port);
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001215 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1216 kfree(sfd_pl);
1217
1218 return err;
1219}
1220
Ido Schimmel6e095fd2016-07-04 08:23:13 +02001221static 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
1229int 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
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001237static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
Ido Schimmel64771e32015-12-15 16:03:46 +01001238 const char *mac, u16 fid, u16 lag_vid,
1239 bool adding, bool dynamic)
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001240{
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),
Ido Schimmel64771e32015-12-15 16:03:46 +01001250 mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
1251 lag_vid, lag_id);
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001252 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001253 kfree(sfd_pl);
1254
1255 return err;
1256}
1257
1258static int
Arkadi Sharshevskyaf0613782017-06-08 08:44:20 +02001259mlxsw_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
Elad Raz3a49b4f2016-01-10 21:06:28 +01001295static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
Nogah Frankel5f9abc52017-09-20 16:15:05 +02001296 u16 fid, u16 mid_idx, bool adding)
Elad Raz3a49b4f2016-01-10 21:06:28 +01001297{
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,
Nogah Frankel5f9abc52017-09-20 16:15:05 +02001307 MLXSW_REG_SFD_REC_ACTION_NOP, mid_idx);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001308 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1309 kfree(sfd_pl);
1310 return err;
1311}
1312
Nogah Frankel2e3496c2017-09-20 16:15:09 +02001313static int mlxsw_sp_port_smid_full_entry(struct mlxsw_sp *mlxsw_sp, u16 mid_idx,
Yotam Gigic4db9532017-10-09 11:15:34 +02001314 long *ports_bitmap,
1315 bool set_router_port)
Elad Raz3a49b4f2016-01-10 21:06:28 +01001316{
Elad Raz3a49b4f2016-01-10 21:06:28 +01001317 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
Nogah Frankel5f9abc52017-09-20 16:15:05 +02001324 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);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001328 }
Nogah Frankel5f9abc52017-09-20 16:15:05 +02001329
Yotam Gigic4db9532017-10-09 11:15:34 +02001330 mlxsw_reg_smid_port_mask_set(smid_pl,
1331 mlxsw_sp_router_port(mlxsw_sp), 1);
1332
Nogah Frankel2e3496c2017-09-20 16:15:09 +02001333 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
Yotam Gigic4db9532017-10-09 11:15:34 +02001336 mlxsw_reg_smid_port_set(smid_pl, mlxsw_sp_router_port(mlxsw_sp),
1337 set_router_port);
1338
Nogah Frankel5f9abc52017-09-20 16:15:05 +02001339 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1340 kfree(smid_pl);
1341 return err;
1342}
1343
1344static 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);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001356 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1357 kfree(smid_pl);
1358 return err;
1359}
1360
Nogah Frankelb80888a2017-09-20 16:15:04 +02001361static struct
1362mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp_bridge_device *bridge_device,
1363 const unsigned char *addr,
1364 u16 fid)
Elad Raz3a49b4f2016-01-10 21:06:28 +01001365{
1366 struct mlxsw_sp_mid *mid;
1367
Nogah Frankelb80888a2017-09-20 16:15:04 +02001368 list_for_each_entry(mid, &bridge_device->mids_list, list) {
Ido Schimmel46d08472016-10-30 10:09:22 +01001369 if (ether_addr_equal(mid->addr, addr) && mid->fid == fid)
Elad Raz3a49b4f2016-01-10 21:06:28 +01001370 return mid;
1371 }
1372 return NULL;
1373}
1374
Nogah Frankel3fba8772017-09-20 16:15:13 +02001375static void
1376mlxsw_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
1400static void
1401mlxsw_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
Nogah Frankel73b433e2017-09-20 16:15:06 +02001416static bool
1417mlxsw_sp_mc_write_mdb_entry(struct mlxsw_sp *mlxsw_sp,
Nogah Frankel3fba8772017-09-20 16:15:13 +02001418 struct mlxsw_sp_mid *mid,
1419 struct mlxsw_sp_bridge_device *bridge_device)
Nogah Frankel73b433e2017-09-20 16:15:06 +02001420{
Nogah Frankel3fba8772017-09-20 16:15:13 +02001421 long *flood_bitmap;
1422 int num_of_ports;
1423 int alloc_size;
Nogah Frankel73b433e2017-09-20 16:15:06 +02001424 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
Nogah Frankel3fba8772017-09-20 16:15:13 +02001432 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
Nogah Frankel73b433e2017-09-20 16:15:06 +02001441 mid->mid = mid_idx;
Yotam Gigic4db9532017-10-09 11:15:34 +02001442 err = mlxsw_sp_port_smid_full_entry(mlxsw_sp, mid_idx, flood_bitmap,
Yotam Gigi593bc282017-10-09 11:15:35 +02001443 bridge_device->mrouter);
Nogah Frankel3fba8772017-09-20 16:15:13 +02001444 kfree(flood_bitmap);
Nogah Frankel73b433e2017-09-20 16:15:06 +02001445 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
1458static int mlxsw_sp_mc_remove_mdb_entry(struct mlxsw_sp *mlxsw_sp,
1459 struct mlxsw_sp_mid *mid)
1460{
Nogah Frankel846fd8a2017-09-20 16:15:08 +02001461 if (!mid->in_hw)
1462 return 0;
1463
Nogah Frankel73b433e2017-09-20 16:15:06 +02001464 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
Nogah Frankelb80888a2017-09-20 16:15:04 +02001470static struct
1471mlxsw_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)
Elad Raz3a49b4f2016-01-10 21:06:28 +01001475{
1476 struct mlxsw_sp_mid *mid;
Nogah Frankel4cdc35e2017-09-20 16:15:02 +02001477 size_t alloc_size;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001478
1479 mid = kzalloc(sizeof(*mid), GFP_KERNEL);
1480 if (!mid)
1481 return NULL;
1482
Nogah Frankel4cdc35e2017-09-20 16:15:02 +02001483 alloc_size = sizeof(unsigned long) *
1484 BITS_TO_LONGS(mlxsw_core_max_ports(mlxsw_sp->core));
Nogah Frankel4cdc35e2017-09-20 16:15:02 +02001485
Nogah Frankel73b433e2017-09-20 16:15:06 +02001486 mid->ports_in_mid = kzalloc(alloc_size, GFP_KERNEL);
1487 if (!mid->ports_in_mid)
1488 goto err_ports_in_mid_alloc;
1489
Elad Raz3a49b4f2016-01-10 21:06:28 +01001490 ether_addr_copy(mid->addr, addr);
Ido Schimmel46d08472016-10-30 10:09:22 +01001491 mid->fid = fid;
Nogah Frankel73b433e2017-09-20 16:15:06 +02001492 mid->in_hw = false;
Nogah Frankel846fd8a2017-09-20 16:15:08 +02001493
1494 if (!bridge_device->multicast_enabled)
1495 goto out;
1496
Nogah Frankel3fba8772017-09-20 16:15:13 +02001497 if (!mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid, bridge_device))
Nogah Frankel73b433e2017-09-20 16:15:06 +02001498 goto err_write_mdb_entry;
1499
Nogah Frankel846fd8a2017-09-20 16:15:08 +02001500out:
Nogah Frankelb80888a2017-09-20 16:15:04 +02001501 list_add_tail(&mid->list, &bridge_device->mids_list);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001502 return mid;
Nogah Frankel73b433e2017-09-20 16:15:06 +02001503
1504err_write_mdb_entry:
1505 kfree(mid->ports_in_mid);
1506err_ports_in_mid_alloc:
1507 kfree(mid);
1508 return NULL;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001509}
1510
Nogah Frankel0161b952017-09-20 16:15:03 +02001511static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port *mlxsw_sp_port,
1512 struct mlxsw_sp_mid *mid)
Elad Raz3a49b4f2016-01-10 21:06:28 +01001513{
Nogah Frankel4cdc35e2017-09-20 16:15:02 +02001514 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
Nogah Frankel73b433e2017-09-20 16:15:06 +02001515 int err = 0;
Nogah Frankel4cdc35e2017-09-20 16:15:02 +02001516
1517 clear_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
Nogah Frankel0161b952017-09-20 16:15:03 +02001518 if (bitmap_empty(mid->ports_in_mid,
1519 mlxsw_core_max_ports(mlxsw_sp->core))) {
Nogah Frankel73b433e2017-09-20 16:15:06 +02001520 err = mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001521 list_del(&mid->list);
Nogah Frankel4cdc35e2017-09-20 16:15:02 +02001522 kfree(mid->ports_in_mid);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001523 kfree(mid);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001524 }
Nogah Frankel73b433e2017-09-20 16:15:06 +02001525 return err;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001526}
1527
1528static 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;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001533 struct net_device *orig_dev = mdb->obj.orig_dev;
1534 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001535 struct net_device *dev = mlxsw_sp_port->dev;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001536 struct mlxsw_sp_bridge_device *bridge_device;
1537 struct mlxsw_sp_bridge_port *bridge_port;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001538 struct mlxsw_sp_mid *mid;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001539 u16 fid_index;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001540 int err = 0;
1541
1542 if (switchdev_trans_ph_prepare(trans))
1543 return 0;
1544
Ido Schimmelc57529e2017-05-26 08:37:31 +02001545 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
Ido Schimmel17b334a2017-08-04 14:12:29 +02001546 if (!bridge_port)
1547 return 0;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001548
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);
Ido Schimmel17b334a2017-08-04 14:12:29 +02001553 if (!mlxsw_sp_port_vlan)
1554 return 0;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001555
Ido Schimmela1107482017-05-26 08:37:39 +02001556 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
Ido Schimmelc57529e2017-05-26 08:37:31 +02001557
Nogah Frankelb80888a2017-09-20 16:15:04 +02001558 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001559 if (!mid) {
Nogah Frankelb80888a2017-09-20 16:15:04 +02001560 mid = __mlxsw_sp_mc_alloc(mlxsw_sp, bridge_device, mdb->addr,
1561 fid_index);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001562 if (!mid) {
1563 netdev_err(dev, "Unable to allocate MC group\n");
1564 return -ENOMEM;
1565 }
1566 }
Nogah Frankel4cdc35e2017-09-20 16:15:02 +02001567 set_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001568
Nogah Frankel846fd8a2017-09-20 16:15:08 +02001569 if (!bridge_device->multicast_enabled)
1570 return 0;
1571
Nogah Frankelded711c2017-09-20 16:15:16 +02001572 if (bridge_port->mrouter)
1573 return 0;
1574
Nogah Frankel5f9abc52017-09-20 16:15:05 +02001575 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001576 if (err) {
1577 netdev_err(dev, "Unable to set SMID\n");
1578 goto err_out;
1579 }
1580
Elad Raz3a49b4f2016-01-10 21:06:28 +01001581 return 0;
1582
1583err_out:
Nogah Frankel0161b952017-09-20 16:15:03 +02001584 mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001585 return err;
1586}
1587
Nogah Frankel2e3496c2017-09-20 16:15:09 +02001588static void
1589mlxsw_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)
Nogah Frankel3fba8772017-09-20 16:15:13 +02001601 mlxsw_sp_mc_write_mdb_entry(mlxsw_sp, mid,
1602 bridge_device);
Nogah Frankel2e3496c2017-09-20 16:15:09 +02001603 else
1604 mlxsw_sp_mc_remove_mdb_entry(mlxsw_sp, mid);
1605 }
1606}
1607
Nogah Frankel3ddda112017-09-20 16:15:14 +02001608static void
1609mlxsw_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
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001624static 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;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001637 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;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001642 default:
1643 err = -EOPNOTSUPP;
1644 break;
1645 }
1646
1647 return err;
1648}
1649
Ido Schimmelc57529e2017-05-26 08:37:31 +02001650static void
1651mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port *mlxsw_sp_port,
1652 struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001653{
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001654 u16 pvid = mlxsw_sp_port->pvid == vid ? 0 : vid;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001655 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001656
Ido Schimmelc57529e2017-05-26 08:37:31 +02001657 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);
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001662 mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1663 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
Ido Schimmelc57529e2017-05-26 08:37:31 +02001664 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001665}
1666
1667static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1668 const struct switchdev_obj_port_vlan *vlan)
1669{
Ido Schimmelc57529e2017-05-26 08:37:31 +02001670 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;
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001673 u16 vid;
1674
Ido Schimmelc57529e2017-05-26 08:37:31 +02001675 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
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001682 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++)
Ido Schimmelc57529e2017-05-26 08:37:31 +02001683 mlxsw_sp_bridge_port_vlan_del(mlxsw_sp_port, bridge_port, vid);
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001684
1685 return 0;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001686}
1687
Nogah Frankel061e55b2017-09-20 16:15:07 +02001688static 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
Nogah Frankel846fd8a2017-09-20 16:15:08 +02001696 if (bridge_port->bridge_device->multicast_enabled) {
Nogah Frankelded711c2017-09-20 16:15:16 +02001697 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 }
Nogah Frankel846fd8a2017-09-20 16:15:08 +02001703 }
Nogah Frankel061e55b2017-09-20 16:15:07 +02001704
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
Elad Raz3a49b4f2016-01-10 21:06:28 +01001712static 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;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001716 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;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001719 struct net_device *dev = mlxsw_sp_port->dev;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001720 struct mlxsw_sp_bridge_port *bridge_port;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001721 struct mlxsw_sp_mid *mid;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001722 u16 fid_index;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001723
Ido Schimmelc57529e2017-05-26 08:37:31 +02001724 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
Ido Schimmel17b334a2017-08-04 14:12:29 +02001725 if (!bridge_port)
1726 return 0;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001727
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);
Ido Schimmel17b334a2017-08-04 14:12:29 +02001732 if (!mlxsw_sp_port_vlan)
1733 return 0;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001734
Ido Schimmela1107482017-05-26 08:37:39 +02001735 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
Ido Schimmelc57529e2017-05-26 08:37:31 +02001736
Nogah Frankelb80888a2017-09-20 16:15:04 +02001737 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001738 if (!mid) {
1739 netdev_err(dev, "Unable to remove port from MC DB\n");
1740 return -EINVAL;
1741 }
1742
Nogah Frankel061e55b2017-09-20 16:15:07 +02001743 return __mlxsw_sp_port_mdb_del(mlxsw_sp_port, bridge_port, mid);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001744}
1745
Nogah Frankelbb5355b2017-09-20 16:15:12 +02001746static void
1747mlxsw_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);
Nogah Frankel01662772017-09-20 16:15:15 +02001759 } else if (bridge_device->multicast_enabled &&
1760 bridge_port->mrouter) {
1761 mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
Nogah Frankelbb5355b2017-09-20 16:15:12 +02001762 }
1763 }
1764}
1765
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001766static 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;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001777 case SWITCHDEV_OBJ_ID_PORT_MDB:
1778 err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1779 SWITCHDEV_OBJ_PORT_MDB(obj));
Dan Carpenter00ae40e2016-01-13 15:28:23 +03001780 break;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001781 default:
1782 err = -EOPNOTSUPP;
1783 break;
1784 }
1785
1786 return err;
1787}
1788
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001789static 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;
Jiri Pirkoc1a38312016-10-21 16:07:23 +02001793 u64 max_lag_members;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001794 int i;
1795
Jiri Pirkoc1a38312016-10-21 16:07:23 +02001796 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1797 MAX_LAG_MEMBERS);
1798 for (i = 0; i < max_lag_members; i++) {
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001799 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
Jiri Pirkoc7070fc2015-10-28 10:17:05 +01001806static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = {
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001807 .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,
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001811};
1812
Ido Schimmelc57529e2017-05-26 08:37:31 +02001813static int
1814mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1815 struct mlxsw_sp_bridge_port *bridge_port,
Ido Schimmel9b63ef882017-10-08 11:57:56 +02001816 struct mlxsw_sp_port *mlxsw_sp_port,
1817 struct netlink_ext_ack *extack)
Ido Schimmelc57529e2017-05-26 08:37:31 +02001818{
1819 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1820
Ido Schimmel9b63ef882017-10-08 11:57:56 +02001821 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");
Ido Schimmelc57529e2017-05-26 08:37:31 +02001823 return -EINVAL;
Ido Schimmel9b63ef882017-10-08 11:57:56 +02001824 }
Ido Schimmelc57529e2017-05-26 08:37:31 +02001825
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
1836static void
1837mlxsw_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
Ido Schimmela1107482017-05-26 08:37:39 +02001846static struct mlxsw_sp_fid *
1847mlxsw_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
Ido Schimmelc57529e2017-05-26 08:37:31 +02001855static 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,
Ido Schimmela1107482017-05-26 08:37:39 +02001858 .fid_get = mlxsw_sp_bridge_8021q_fid_get,
Ido Schimmelc57529e2017-05-26 08:37:31 +02001859};
1860
1861static bool
1862mlxsw_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
1878static int
1879mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1880 struct mlxsw_sp_bridge_port *bridge_port,
Ido Schimmel9b63ef882017-10-08 11:57:56 +02001881 struct mlxsw_sp_port *mlxsw_sp_port,
1882 struct netlink_ext_ack *extack)
Ido Schimmelc57529e2017-05-26 08:37:31 +02001883{
1884 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001885 u16 vid;
1886
Ido Schimmel9b63ef882017-10-08 11:57:56 +02001887 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");
Ido Schimmelc57529e2017-05-26 08:37:31 +02001889 return -EINVAL;
Ido Schimmel9b63ef882017-10-08 11:57:56 +02001890 }
Ido Schimmelc57529e2017-05-26 08:37:31 +02001891 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;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001896
1897 if (mlxsw_sp_port_is_br_member(mlxsw_sp_port, bridge_device->dev)) {
Ido Schimmel9b63ef882017-10-08 11:57:56 +02001898 NL_SET_ERR_MSG(extack, "spectrum: Can not bridge VLAN uppers of the same port");
Ido Schimmelc57529e2017-05-26 08:37:31 +02001899 return -EINVAL;
1900 }
1901
1902 /* Port is no longer usable as a router interface */
Ido Schimmela1107482017-05-26 08:37:39 +02001903 if (mlxsw_sp_port_vlan->fid)
1904 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
Ido Schimmelc57529e2017-05-26 08:37:31 +02001905
1906 return mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
1907}
1908
1909static void
1910mlxsw_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
Ido Schimmela1107482017-05-26 08:37:39 +02001924static struct mlxsw_sp_fid *
1925mlxsw_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
Ido Schimmelc57529e2017-05-26 08:37:31 +02001933static 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,
Ido Schimmela1107482017-05-26 08:37:39 +02001936 .fid_get = mlxsw_sp_bridge_8021d_fid_get,
Ido Schimmelc57529e2017-05-26 08:37:31 +02001937};
1938
1939int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port,
1940 struct net_device *brport_dev,
Ido Schimmel9b63ef882017-10-08 11:57:56 +02001941 struct net_device *br_dev,
1942 struct netlink_ext_ack *extack)
Ido Schimmelc57529e2017-05-26 08:37:31 +02001943{
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,
Ido Schimmel9b63ef882017-10-08 11:57:56 +02001955 mlxsw_sp_port, extack);
Ido Schimmelc57529e2017-05-26 08:37:31 +02001956 if (err)
1957 goto err_port_join;
1958
1959 return 0;
1960
1961err_port_join:
1962 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
1963 return err;
1964}
1965
1966void 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
Arkadi Sharshevsky1b40dc32017-06-08 08:44:19 +02001986static void
1987mlxsw_sp_fdb_call_notifiers(enum switchdev_notifier_type type,
1988 const char *mac, u16 vid,
1989 struct net_device *dev)
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001990{
1991 struct switchdev_notifier_fdb_info info;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001992
Arkadi Sharshevskya989cdb2017-06-08 08:44:16 +02001993 info.addr = mac;
1994 info.vid = vid;
Arkadi Sharshevsky1b40dc32017-06-08 08:44:19 +02001995 call_switchdev_notifiers(type, dev, &info.info);
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001996}
1997
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001998static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
1999 char *sfn_pl, int rec_index,
2000 bool adding)
2001{
Ido Schimmelc57529e2017-05-26 08:37:31 +02002002 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;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002005 struct mlxsw_sp_port *mlxsw_sp_port;
Arkadi Sharshevsky1b40dc32017-06-08 08:44:19 +02002006 enum switchdev_notifier_type type;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002007 char mac[ETH_ALEN];
2008 u8 local_port;
Ido Schimmel9de6a802015-12-15 16:03:40 +01002009 u16 vid, fid;
Jiri Pirko12f15012016-01-07 11:50:30 +01002010 bool do_notification = true;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002011 int err;
2012
Ido Schimmel9de6a802015-12-15 16:03:40 +01002013 mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002014 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");
Jiri Pirko12f15012016-01-07 11:50:30 +01002017 goto just_remove;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002018 }
2019
Ido Schimmelc57529e2017-05-26 08:37:31 +02002020 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;
Ido Schimmelaac78a42015-12-15 16:03:42 +01002024 }
2025
Ido Schimmelc57529e2017-05-26 08:37:31 +02002026 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
Jiri Pirko12f15012016-01-07 11:50:30 +01002035do_fdb_op:
Jiri Pirko2fa9d452016-01-07 11:50:29 +01002036 err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
Jiri Pirko12f15012016-01-07 11:50:30 +01002037 adding, true);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002038 if (err) {
Ido Schimmelc0e01ea2017-05-18 13:03:52 +02002039 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002040 return;
2041 }
2042
Jiri Pirko12f15012016-01-07 11:50:30 +01002043 if (!do_notification)
2044 return;
Arkadi Sharshevsky1b40dc32017-06-08 08:44:19 +02002045 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2046 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev);
Arkadi Sharshevskya989cdb2017-06-08 08:44:16 +02002047
Jiri Pirko12f15012016-01-07 11:50:30 +01002048 return;
2049
2050just_remove:
2051 adding = false;
2052 do_notification = false;
2053 goto do_fdb_op;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01002054}
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002055
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01002056static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
2057 char *sfn_pl, int rec_index,
2058 bool adding)
2059{
Ido Schimmelc57529e2017-05-26 08:37:31 +02002060 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;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01002063 struct mlxsw_sp_port *mlxsw_sp_port;
Arkadi Sharshevsky1b40dc32017-06-08 08:44:19 +02002064 enum switchdev_notifier_type type;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01002065 char mac[ETH_ALEN];
Ido Schimmel64771e32015-12-15 16:03:46 +01002066 u16 lag_vid = 0;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01002067 u16 lag_id;
Ido Schimmel9de6a802015-12-15 16:03:40 +01002068 u16 vid, fid;
Jiri Pirko12f15012016-01-07 11:50:30 +01002069 bool do_notification = true;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01002070 int err;
2071
Ido Schimmel9de6a802015-12-15 16:03:40 +01002072 mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01002073 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");
Jiri Pirko12f15012016-01-07 11:50:30 +01002076 goto just_remove;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002077 }
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01002078
Ido Schimmelc57529e2017-05-26 08:37:31 +02002079 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;
Ido Schimmelaac78a42015-12-15 16:03:42 +01002083 }
2084
Ido Schimmelc57529e2017-05-26 08:37:31 +02002085 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
Jiri Pirko12f15012016-01-07 11:50:30 +01002095do_fdb_op:
Ido Schimmel64771e32015-12-15 16:03:46 +01002096 err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
Jiri Pirko12f15012016-01-07 11:50:30 +01002097 adding, true);
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01002098 if (err) {
Ido Schimmelc0e01ea2017-05-18 13:03:52 +02002099 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01002100 return;
2101 }
2102
Jiri Pirko12f15012016-01-07 11:50:30 +01002103 if (!do_notification)
2104 return;
Arkadi Sharshevsky1b40dc32017-06-08 08:44:19 +02002105 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
2106 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev);
Arkadi Sharshevskya989cdb2017-06-08 08:44:16 +02002107
Jiri Pirko12f15012016-01-07 11:50:30 +01002108 return;
2109
2110just_remove:
2111 adding = false;
2112 do_notification = false;
2113 goto do_fdb_op;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002114}
2115
2116static 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;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01002128 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;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002136 }
2137}
2138
2139static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp)
2140{
Ido Schimmel5f6935c2017-05-16 19:38:26 +02002141 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));
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002145}
2146
2147static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
2148{
Ido Schimmel5f6935c2017-05-16 19:38:26 +02002149 struct mlxsw_sp_bridge *bridge;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002150 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
Ido Schimmel5f6935c2017-05-16 19:38:26 +02002160 bridge = container_of(work, struct mlxsw_sp_bridge, fdb_notify.dw.work);
2161 mlxsw_sp = bridge->mlxsw_sp;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002162
Ido Schimmel4f2c6ae2016-01-27 15:16:43 +01002163 rtnl_lock();
Ido Schimmel1803e0f2016-08-24 12:00:23 +02002164 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);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002173
Ido Schimmel1803e0f2016-08-24 12:00:23 +02002174out:
Ido Schimmel4f2c6ae2016-01-27 15:16:43 +01002175 rtnl_unlock();
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002176 kfree(sfn_pl);
2177 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2178}
2179
Arkadi Sharshevskyaf0613782017-06-08 08:44:20 +02002180struct 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
2187static 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
2217out:
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() */
2225static 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);
Ido Schimmel6f497932017-07-12 09:12:55 +02002249 if (!switchdev_work->fdb_info.addr)
2250 goto err_addr_alloc;
Arkadi Sharshevskyaf0613782017-06-08 08:44:20 +02002251 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;
Ido Schimmel6f497932017-07-12 09:12:55 +02002267
2268err_addr_alloc:
2269 kfree(switchdev_work);
2270 return NOTIFY_BAD;
Arkadi Sharshevskyaf0613782017-06-08 08:44:20 +02002271}
2272
2273static struct notifier_block mlxsw_sp_switchdev_notifier = {
2274 .notifier_call = mlxsw_sp_switchdev_event,
2275};
2276
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002277static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
2278{
Ido Schimmel5f6935c2017-05-16 19:38:26 +02002279 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002280 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 }
Arkadi Sharshevskyaf0613782017-06-08 08:44:20 +02002287
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
Ido Schimmel5f6935c2017-05-16 19:38:26 +02002294 INIT_DELAYED_WORK(&bridge->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
2295 bridge->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002296 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2297 return 0;
2298}
2299
2300static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
2301{
Ido Schimmel5f6935c2017-05-16 19:38:26 +02002302 cancel_delayed_work_sync(&mlxsw_sp->bridge->fdb_notify.dw);
Arkadi Sharshevskyaf0613782017-06-08 08:44:20 +02002303 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
2304
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002305}
2306
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002307int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
2308{
Ido Schimmel5f6935c2017-05-16 19:38:26 +02002309 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
Ido Schimmelc57529e2017-05-26 08:37:31 +02002317 INIT_LIST_HEAD(&mlxsw_sp->bridge->bridges_list);
Ido Schimmel5f6935c2017-05-16 19:38:26 +02002318
Ido Schimmelc57529e2017-05-26 08:37:31 +02002319 bridge->bridge_8021q_ops = &mlxsw_sp_bridge_8021q_ops;
2320 bridge->bridge_8021d_ops = &mlxsw_sp_bridge_8021d_ops;
2321
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002322 return mlxsw_sp_fdb_init(mlxsw_sp);
2323}
2324
2325void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
2326{
2327 mlxsw_sp_fdb_fini(mlxsw_sp);
Ido Schimmelc57529e2017-05-26 08:37:31 +02002328 WARN_ON(!list_empty(&mlxsw_sp->bridge->bridges_list));
Ido Schimmel5f6935c2017-05-16 19:38:26 +02002329 kfree(mlxsw_sp->bridge);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002330}
2331
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002332void 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
2337void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port)
2338{
2339}