blob: 09ead97d94429c40b1022e298a23101b7ce7296d [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>
Jiri Pirko56ade8f2015-10-16 14:01:37 +020049#include <net/switchdev.h>
50
51#include "spectrum.h"
52#include "core.h"
53#include "reg.h"
54
Ido Schimmelc57529e2017-05-26 08:37:31 +020055struct mlxsw_sp_bridge_ops;
56
Ido Schimmel5f6935c2017-05-16 19:38:26 +020057struct mlxsw_sp_bridge {
58 struct mlxsw_sp *mlxsw_sp;
59 struct {
60 struct delayed_work dw;
61#define MLXSW_SP_DEFAULT_LEARNING_INTERVAL 100
62 unsigned int interval; /* ms */
63 } fdb_notify;
64#define MLXSW_SP_MIN_AGEING_TIME 10
65#define MLXSW_SP_MAX_AGEING_TIME 1000000
66#define MLXSW_SP_DEFAULT_AGEING_TIME 300
67 u32 ageing_time;
Ido Schimmelc57529e2017-05-26 08:37:31 +020068 bool vlan_enabled_exists;
69 struct list_head bridges_list;
Ido Schimmel5f6935c2017-05-16 19:38:26 +020070 DECLARE_BITMAP(mids_bitmap, MLXSW_SP_MID_MAX);
Ido Schimmelc57529e2017-05-26 08:37:31 +020071 const struct mlxsw_sp_bridge_ops *bridge_8021q_ops;
72 const struct mlxsw_sp_bridge_ops *bridge_8021d_ops;
Ido Schimmel5f6935c2017-05-16 19:38:26 +020073};
74
Ido Schimmelc57529e2017-05-26 08:37:31 +020075struct mlxsw_sp_bridge_device {
76 struct net_device *dev;
77 struct list_head list;
78 struct list_head ports_list;
Nogah Frankelb80888a2017-09-20 16:15:04 +020079 struct list_head mids_list;
Ido Schimmelc57529e2017-05-26 08:37:31 +020080 u8 vlan_enabled:1,
81 multicast_enabled:1;
82 const struct mlxsw_sp_bridge_ops *ops;
83};
Ido Schimmel5f6935c2017-05-16 19:38:26 +020084
Ido Schimmelc57529e2017-05-26 08:37:31 +020085struct mlxsw_sp_bridge_port {
86 struct net_device *dev;
87 struct mlxsw_sp_bridge_device *bridge_device;
88 struct list_head list;
89 struct list_head vlans_list;
90 unsigned int ref_count;
91 u8 stp_state;
92 unsigned long flags;
93 bool mrouter;
94 bool lagged;
95 union {
96 u16 lag_id;
97 u16 system_port;
98 };
99};
Elad Raze4b6f692016-01-10 21:06:27 +0100100
Ido Schimmelc57529e2017-05-26 08:37:31 +0200101struct mlxsw_sp_bridge_vlan {
102 struct list_head list;
103 struct list_head port_vlan_list;
Ido Schimmel54a73202015-12-15 16:03:41 +0100104 u16 vid;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200105};
Ido Schimmel54a73202015-12-15 16:03:41 +0100106
Ido Schimmelc57529e2017-05-26 08:37:31 +0200107struct mlxsw_sp_bridge_ops {
108 int (*port_join)(struct mlxsw_sp_bridge_device *bridge_device,
109 struct mlxsw_sp_bridge_port *bridge_port,
110 struct mlxsw_sp_port *mlxsw_sp_port);
111 void (*port_leave)(struct mlxsw_sp_bridge_device *bridge_device,
112 struct mlxsw_sp_bridge_port *bridge_port,
113 struct mlxsw_sp_port *mlxsw_sp_port);
Ido Schimmela1107482017-05-26 08:37:39 +0200114 struct mlxsw_sp_fid *
115 (*fid_get)(struct mlxsw_sp_bridge_device *bridge_device,
116 u16 vid);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200117};
118
119static int
120mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
121 struct mlxsw_sp_bridge_port *bridge_port,
122 u16 fid_index);
123
124static struct mlxsw_sp_bridge_device *
125mlxsw_sp_bridge_device_find(const struct mlxsw_sp_bridge *bridge,
126 const struct net_device *br_dev)
127{
128 struct mlxsw_sp_bridge_device *bridge_device;
129
130 list_for_each_entry(bridge_device, &bridge->bridges_list, list)
131 if (bridge_device->dev == br_dev)
132 return bridge_device;
133
134 return NULL;
135}
136
137static struct mlxsw_sp_bridge_device *
138mlxsw_sp_bridge_device_create(struct mlxsw_sp_bridge *bridge,
139 struct net_device *br_dev)
140{
141 struct device *dev = bridge->mlxsw_sp->bus_info->dev;
142 struct mlxsw_sp_bridge_device *bridge_device;
143 bool vlan_enabled = br_vlan_enabled(br_dev);
144
145 if (vlan_enabled && bridge->vlan_enabled_exists) {
146 dev_err(dev, "Only one VLAN-aware bridge is supported\n");
147 return ERR_PTR(-EINVAL);
Nogah Frankel1e5d9432017-02-09 14:54:48 +0100148 }
149
Ido Schimmelc57529e2017-05-26 08:37:31 +0200150 bridge_device = kzalloc(sizeof(*bridge_device), GFP_KERNEL);
151 if (!bridge_device)
152 return ERR_PTR(-ENOMEM);
Ido Schimmel54a73202015-12-15 16:03:41 +0100153
Ido Schimmelc57529e2017-05-26 08:37:31 +0200154 bridge_device->dev = br_dev;
155 bridge_device->vlan_enabled = vlan_enabled;
156 bridge_device->multicast_enabled = br_multicast_enabled(br_dev);
157 INIT_LIST_HEAD(&bridge_device->ports_list);
158 if (vlan_enabled) {
159 bridge->vlan_enabled_exists = true;
160 bridge_device->ops = bridge->bridge_8021q_ops;
161 } else {
162 bridge_device->ops = bridge->bridge_8021d_ops;
163 }
Nogah Frankelb80888a2017-09-20 16:15:04 +0200164 INIT_LIST_HEAD(&bridge_device->mids_list);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200165 list_add(&bridge_device->list, &bridge->bridges_list);
Ido Schimmel54a73202015-12-15 16:03:41 +0100166
Ido Schimmelc57529e2017-05-26 08:37:31 +0200167 return bridge_device;
168}
169
170static void
171mlxsw_sp_bridge_device_destroy(struct mlxsw_sp_bridge *bridge,
172 struct mlxsw_sp_bridge_device *bridge_device)
173{
Nogah Frankelb80888a2017-09-20 16:15:04 +0200174 struct mlxsw_sp_mid *mid, *tmp;
175
Ido Schimmelc57529e2017-05-26 08:37:31 +0200176 list_del(&bridge_device->list);
177 if (bridge_device->vlan_enabled)
178 bridge->vlan_enabled_exists = false;
179 WARN_ON(!list_empty(&bridge_device->ports_list));
Nogah Frankelb80888a2017-09-20 16:15:04 +0200180 list_for_each_entry_safe(mid, tmp, &bridge_device->mids_list, list) {
181 list_del(&mid->list);
182 clear_bit(mid->mid, bridge->mids_bitmap);
183 kfree(mid);
184 }
Ido Schimmelc57529e2017-05-26 08:37:31 +0200185 kfree(bridge_device);
186}
187
188static struct mlxsw_sp_bridge_device *
189mlxsw_sp_bridge_device_get(struct mlxsw_sp_bridge *bridge,
190 struct net_device *br_dev)
191{
192 struct mlxsw_sp_bridge_device *bridge_device;
193
194 bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
195 if (bridge_device)
196 return bridge_device;
197
198 return mlxsw_sp_bridge_device_create(bridge, br_dev);
199}
200
201static void
202mlxsw_sp_bridge_device_put(struct mlxsw_sp_bridge *bridge,
203 struct mlxsw_sp_bridge_device *bridge_device)
204{
205 if (list_empty(&bridge_device->ports_list))
206 mlxsw_sp_bridge_device_destroy(bridge, bridge_device);
207}
208
209static struct mlxsw_sp_bridge_port *
210__mlxsw_sp_bridge_port_find(const struct mlxsw_sp_bridge_device *bridge_device,
211 const struct net_device *brport_dev)
212{
213 struct mlxsw_sp_bridge_port *bridge_port;
214
215 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
216 if (bridge_port->dev == brport_dev)
217 return bridge_port;
218 }
219
220 return NULL;
221}
222
223static struct mlxsw_sp_bridge_port *
224mlxsw_sp_bridge_port_find(struct mlxsw_sp_bridge *bridge,
225 struct net_device *brport_dev)
226{
227 struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
228 struct mlxsw_sp_bridge_device *bridge_device;
229
230 if (!br_dev)
231 return NULL;
232
233 bridge_device = mlxsw_sp_bridge_device_find(bridge, br_dev);
234 if (!bridge_device)
235 return NULL;
236
237 return __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
238}
239
240static struct mlxsw_sp_bridge_port *
241mlxsw_sp_bridge_port_create(struct mlxsw_sp_bridge_device *bridge_device,
242 struct net_device *brport_dev)
243{
244 struct mlxsw_sp_bridge_port *bridge_port;
245 struct mlxsw_sp_port *mlxsw_sp_port;
246
247 bridge_port = kzalloc(sizeof(*bridge_port), GFP_KERNEL);
248 if (!bridge_port)
249 return NULL;
250
251 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(brport_dev);
252 bridge_port->lagged = mlxsw_sp_port->lagged;
253 if (bridge_port->lagged)
254 bridge_port->lag_id = mlxsw_sp_port->lag_id;
255 else
256 bridge_port->system_port = mlxsw_sp_port->local_port;
257 bridge_port->dev = brport_dev;
258 bridge_port->bridge_device = bridge_device;
259 bridge_port->stp_state = BR_STATE_DISABLED;
260 bridge_port->flags = BR_LEARNING | BR_FLOOD | BR_LEARNING_SYNC;
261 INIT_LIST_HEAD(&bridge_port->vlans_list);
262 list_add(&bridge_port->list, &bridge_device->ports_list);
263 bridge_port->ref_count = 1;
264
265 return bridge_port;
266}
267
268static void
269mlxsw_sp_bridge_port_destroy(struct mlxsw_sp_bridge_port *bridge_port)
270{
271 list_del(&bridge_port->list);
272 WARN_ON(!list_empty(&bridge_port->vlans_list));
273 kfree(bridge_port);
274}
275
276static bool
277mlxsw_sp_bridge_port_should_destroy(const struct mlxsw_sp_bridge_port *
278 bridge_port)
279{
280 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_port->dev);
281
282 /* In case ports were pulled from out of a bridged LAG, then
283 * it's possible the reference count isn't zero, yet the bridge
284 * port should be destroyed, as it's no longer an upper of ours.
285 */
286 if (!mlxsw_sp && list_empty(&bridge_port->vlans_list))
287 return true;
288 else if (bridge_port->ref_count == 0)
289 return true;
290 else
291 return false;
292}
293
294static struct mlxsw_sp_bridge_port *
295mlxsw_sp_bridge_port_get(struct mlxsw_sp_bridge *bridge,
296 struct net_device *brport_dev)
297{
298 struct net_device *br_dev = netdev_master_upper_dev_get(brport_dev);
299 struct mlxsw_sp_bridge_device *bridge_device;
300 struct mlxsw_sp_bridge_port *bridge_port;
301 int err;
302
303 bridge_port = mlxsw_sp_bridge_port_find(bridge, brport_dev);
304 if (bridge_port) {
305 bridge_port->ref_count++;
306 return bridge_port;
307 }
308
309 bridge_device = mlxsw_sp_bridge_device_get(bridge, br_dev);
310 if (IS_ERR(bridge_device))
311 return ERR_CAST(bridge_device);
312
313 bridge_port = mlxsw_sp_bridge_port_create(bridge_device, brport_dev);
314 if (!bridge_port) {
315 err = -ENOMEM;
316 goto err_bridge_port_create;
317 }
318
319 return bridge_port;
320
321err_bridge_port_create:
322 mlxsw_sp_bridge_device_put(bridge, bridge_device);
323 return ERR_PTR(err);
324}
325
326static void mlxsw_sp_bridge_port_put(struct mlxsw_sp_bridge *bridge,
327 struct mlxsw_sp_bridge_port *bridge_port)
328{
329 struct mlxsw_sp_bridge_device *bridge_device;
330
331 bridge_port->ref_count--;
332 if (!mlxsw_sp_bridge_port_should_destroy(bridge_port))
333 return;
334 bridge_device = bridge_port->bridge_device;
335 mlxsw_sp_bridge_port_destroy(bridge_port);
336 mlxsw_sp_bridge_device_put(bridge, bridge_device);
337}
338
339static struct mlxsw_sp_port_vlan *
340mlxsw_sp_port_vlan_find_by_bridge(struct mlxsw_sp_port *mlxsw_sp_port,
341 const struct mlxsw_sp_bridge_device *
342 bridge_device,
343 u16 vid)
344{
345 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
346
347 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
348 list) {
349 if (!mlxsw_sp_port_vlan->bridge_port)
350 continue;
351 if (mlxsw_sp_port_vlan->bridge_port->bridge_device !=
352 bridge_device)
353 continue;
354 if (bridge_device->vlan_enabled &&
355 mlxsw_sp_port_vlan->vid != vid)
356 continue;
357 return mlxsw_sp_port_vlan;
358 }
359
360 return NULL;
361}
362
363static struct mlxsw_sp_port_vlan*
364mlxsw_sp_port_vlan_find_by_fid(struct mlxsw_sp_port *mlxsw_sp_port,
365 u16 fid_index)
366{
367 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
368
369 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
370 list) {
371 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
372
Ido Schimmela1107482017-05-26 08:37:39 +0200373 if (fid && mlxsw_sp_fid_index(fid) == fid_index)
Ido Schimmelc57529e2017-05-26 08:37:31 +0200374 return mlxsw_sp_port_vlan;
375 }
376
377 return NULL;
378}
379
380static struct mlxsw_sp_bridge_vlan *
381mlxsw_sp_bridge_vlan_find(const struct mlxsw_sp_bridge_port *bridge_port,
382 u16 vid)
383{
384 struct mlxsw_sp_bridge_vlan *bridge_vlan;
385
386 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
387 if (bridge_vlan->vid == vid)
388 return bridge_vlan;
389 }
390
391 return NULL;
392}
393
394static struct mlxsw_sp_bridge_vlan *
395mlxsw_sp_bridge_vlan_create(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
396{
397 struct mlxsw_sp_bridge_vlan *bridge_vlan;
398
399 bridge_vlan = kzalloc(sizeof(*bridge_vlan), GFP_KERNEL);
400 if (!bridge_vlan)
401 return NULL;
402
403 INIT_LIST_HEAD(&bridge_vlan->port_vlan_list);
404 bridge_vlan->vid = vid;
405 list_add(&bridge_vlan->list, &bridge_port->vlans_list);
406
407 return bridge_vlan;
408}
409
410static void
411mlxsw_sp_bridge_vlan_destroy(struct mlxsw_sp_bridge_vlan *bridge_vlan)
412{
413 list_del(&bridge_vlan->list);
414 WARN_ON(!list_empty(&bridge_vlan->port_vlan_list));
415 kfree(bridge_vlan);
416}
417
418static struct mlxsw_sp_bridge_vlan *
419mlxsw_sp_bridge_vlan_get(struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
420{
421 struct mlxsw_sp_bridge_vlan *bridge_vlan;
422
423 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
424 if (bridge_vlan)
425 return bridge_vlan;
426
427 return mlxsw_sp_bridge_vlan_create(bridge_port, vid);
428}
429
430static void mlxsw_sp_bridge_vlan_put(struct mlxsw_sp_bridge_vlan *bridge_vlan)
431{
432 if (list_empty(&bridge_vlan->port_vlan_list))
433 mlxsw_sp_bridge_vlan_destroy(bridge_vlan);
434}
435
436static void mlxsw_sp_port_bridge_flags_get(struct mlxsw_sp_bridge *bridge,
437 struct net_device *dev,
438 unsigned long *brport_flags)
439{
440 struct mlxsw_sp_bridge_port *bridge_port;
441
442 bridge_port = mlxsw_sp_bridge_port_find(bridge, dev);
443 if (WARN_ON(!bridge_port))
444 return;
445
446 memcpy(brport_flags, &bridge_port->flags, sizeof(*brport_flags));
Ido Schimmel54a73202015-12-15 16:03:41 +0100447}
448
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200449static int mlxsw_sp_port_attr_get(struct net_device *dev,
450 struct switchdev_attr *attr)
451{
452 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
453 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
454
455 switch (attr->id) {
456 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
457 attr->u.ppid.id_len = sizeof(mlxsw_sp->base_mac);
458 memcpy(&attr->u.ppid.id, &mlxsw_sp->base_mac,
459 attr->u.ppid.id_len);
460 break;
461 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
Ido Schimmelc57529e2017-05-26 08:37:31 +0200462 mlxsw_sp_port_bridge_flags_get(mlxsw_sp->bridge, attr->orig_dev,
463 &attr->u.brport_flags);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200464 break;
Arkadi Sharshevskyc7b566c2017-06-08 08:44:17 +0200465 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
466 attr->u.brport_flags_support = BR_LEARNING | BR_FLOOD;
467 break;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200468 default:
469 return -EOPNOTSUPP;
470 }
471
472 return 0;
473}
474
Ido Schimmelc57529e2017-05-26 08:37:31 +0200475static int
476mlxsw_sp_port_bridge_vlan_stp_set(struct mlxsw_sp_port *mlxsw_sp_port,
477 struct mlxsw_sp_bridge_vlan *bridge_vlan,
478 u8 state)
479{
480 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
481
482 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
483 bridge_vlan_node) {
484 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
485 continue;
486 return mlxsw_sp_port_vid_stp_set(mlxsw_sp_port,
487 bridge_vlan->vid, state);
488 }
489
490 return 0;
491}
492
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200493static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
494 struct switchdev_trans *trans,
Ido Schimmelc57529e2017-05-26 08:37:31 +0200495 struct net_device *orig_dev,
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200496 u8 state)
497{
Ido Schimmelc57529e2017-05-26 08:37:31 +0200498 struct mlxsw_sp_bridge_port *bridge_port;
499 struct mlxsw_sp_bridge_vlan *bridge_vlan;
Ido Schimmel45bfe6b2017-05-16 19:38:32 +0200500 int err;
501
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200502 if (switchdev_trans_ph_prepare(trans))
503 return 0;
504
Ido Schimmelc57529e2017-05-26 08:37:31 +0200505 /* It's possible we failed to enslave the port, yet this
506 * operation is executed due to it being deferred.
507 */
508 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
509 orig_dev);
510 if (!bridge_port)
Ido Schimmel45bfe6b2017-05-16 19:38:32 +0200511 return 0;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200512
513 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
514 err = mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port,
515 bridge_vlan, state);
516 if (err)
517 goto err_port_bridge_vlan_stp_set;
Ido Schimmel45bfe6b2017-05-16 19:38:32 +0200518 }
519
Ido Schimmelc57529e2017-05-26 08:37:31 +0200520 bridge_port->stp_state = state;
Ido Schimmel45bfe6b2017-05-16 19:38:32 +0200521
522 return 0;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200523
524err_port_bridge_vlan_stp_set:
525 list_for_each_entry_continue_reverse(bridge_vlan,
526 &bridge_port->vlans_list, list)
527 mlxsw_sp_port_bridge_vlan_stp_set(mlxsw_sp_port, bridge_vlan,
528 bridge_port->stp_state);
529 return err;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200530}
531
Ido Schimmelc57529e2017-05-26 08:37:31 +0200532static int
533mlxsw_sp_port_bridge_vlan_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
534 struct mlxsw_sp_bridge_vlan *bridge_vlan,
Ido Schimmela1107482017-05-26 08:37:39 +0200535 enum mlxsw_sp_flood_type packet_type,
Ido Schimmelc57529e2017-05-26 08:37:31 +0200536 bool member)
Nogah Frankeleaa7df32017-02-09 14:54:43 +0100537{
Ido Schimmelc57529e2017-05-26 08:37:31 +0200538 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
Nogah Frankeleaa7df32017-02-09 14:54:43 +0100539
Ido Schimmelc57529e2017-05-26 08:37:31 +0200540 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
541 bridge_vlan_node) {
542 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
543 continue;
Ido Schimmela1107482017-05-26 08:37:39 +0200544 return mlxsw_sp_fid_flood_set(mlxsw_sp_port_vlan->fid,
545 packet_type,
546 mlxsw_sp_port->local_port,
547 member);
Ido Schimmel54a73202015-12-15 16:03:41 +0100548 }
549
Ido Schimmelc57529e2017-05-26 08:37:31 +0200550 return 0;
551}
552
553static int
554mlxsw_sp_bridge_port_flood_table_set(struct mlxsw_sp_port *mlxsw_sp_port,
555 struct mlxsw_sp_bridge_port *bridge_port,
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)
558{
559 struct mlxsw_sp_bridge_vlan *bridge_vlan;
560 int err;
561
562 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
563 err = mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port,
Ido Schimmela1107482017-05-26 08:37:39 +0200564 bridge_vlan,
565 packet_type,
Ido Schimmelc57529e2017-05-26 08:37:31 +0200566 member);
567 if (err)
568 goto err_port_bridge_vlan_flood_set;
Ido Schimmel02930382015-10-28 10:16:58 +0100569 }
570
571 return 0;
572
Ido Schimmelc57529e2017-05-26 08:37:31 +0200573err_port_bridge_vlan_flood_set:
574 list_for_each_entry_continue_reverse(bridge_vlan,
575 &bridge_port->vlans_list, list)
576 mlxsw_sp_port_bridge_vlan_flood_set(mlxsw_sp_port, bridge_vlan,
Ido Schimmela1107482017-05-26 08:37:39 +0200577 packet_type, !member);
Ido Schimmel02930382015-10-28 10:16:58 +0100578 return err;
579}
580
Ido Schimmelc57529e2017-05-26 08:37:31 +0200581static int
582mlxsw_sp_port_bridge_vlan_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
583 struct mlxsw_sp_bridge_vlan *bridge_vlan,
584 bool set)
Nogah Frankel90e0f0c2017-02-09 14:54:49 +0100585{
Ido Schimmelc57529e2017-05-26 08:37:31 +0200586 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
587 u16 vid = bridge_vlan->vid;
Nogah Frankel90e0f0c2017-02-09 14:54:49 +0100588
Ido Schimmelc57529e2017-05-26 08:37:31 +0200589 list_for_each_entry(mlxsw_sp_port_vlan, &bridge_vlan->port_vlan_list,
590 bridge_vlan_node) {
591 if (mlxsw_sp_port_vlan->mlxsw_sp_port != mlxsw_sp_port)
592 continue;
Ido Schimmel7cbc4272017-05-16 19:38:33 +0200593 return mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, set);
Ido Schimmel89b548f2016-08-24 12:00:27 +0200594 }
595
Ido Schimmelc57529e2017-05-26 08:37:31 +0200596 return 0;
597}
598
599static int
600mlxsw_sp_bridge_port_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
601 struct mlxsw_sp_bridge_port *bridge_port,
602 bool set)
603{
604 struct mlxsw_sp_bridge_vlan *bridge_vlan;
605 int err;
606
607 list_for_each_entry(bridge_vlan, &bridge_port->vlans_list, list) {
608 err = mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
609 bridge_vlan, set);
Ido Schimmel89b548f2016-08-24 12:00:27 +0200610 if (err)
Ido Schimmelc57529e2017-05-26 08:37:31 +0200611 goto err_port_bridge_vlan_learning_set;
Ido Schimmel89b548f2016-08-24 12:00:27 +0200612 }
613
614 return 0;
615
Ido Schimmelc57529e2017-05-26 08:37:31 +0200616err_port_bridge_vlan_learning_set:
617 list_for_each_entry_continue_reverse(bridge_vlan,
618 &bridge_port->vlans_list, list)
619 mlxsw_sp_port_bridge_vlan_learning_set(mlxsw_sp_port,
620 bridge_vlan, !set);
Ido Schimmel89b548f2016-08-24 12:00:27 +0200621 return err;
622}
623
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200624static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
625 struct switchdev_trans *trans,
Ido Schimmelc57529e2017-05-26 08:37:31 +0200626 struct net_device *orig_dev,
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200627 unsigned long brport_flags)
628{
Ido Schimmelc57529e2017-05-26 08:37:31 +0200629 struct mlxsw_sp_bridge_port *bridge_port;
Ido Schimmel02930382015-10-28 10:16:58 +0100630 int err;
631
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200632 if (switchdev_trans_ph_prepare(trans))
633 return 0;
634
Ido Schimmelc57529e2017-05-26 08:37:31 +0200635 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
636 orig_dev);
Ido Schimmel17b334a2017-08-04 14:12:29 +0200637 if (!bridge_port)
638 return 0;
Ido Schimmel02930382015-10-28 10:16:58 +0100639
Ido Schimmelc57529e2017-05-26 08:37:31 +0200640 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
Ido Schimmela1107482017-05-26 08:37:39 +0200641 MLXSW_SP_FLOOD_TYPE_UC,
Ido Schimmelc57529e2017-05-26 08:37:31 +0200642 brport_flags & BR_FLOOD);
643 if (err)
644 return err;
Ido Schimmel89b548f2016-08-24 12:00:27 +0200645
Ido Schimmelc57529e2017-05-26 08:37:31 +0200646 err = mlxsw_sp_bridge_port_learning_set(mlxsw_sp_port, bridge_port,
647 brport_flags & BR_LEARNING);
648 if (err)
649 return err;
650
651 memcpy(&bridge_port->flags, &brport_flags, sizeof(brport_flags));
Ido Schimmel02930382015-10-28 10:16:58 +0100652
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200653 return 0;
654}
655
656static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
657{
658 char sfdat_pl[MLXSW_REG_SFDAT_LEN];
659 int err;
660
661 mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
662 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
663 if (err)
664 return err;
Ido Schimmel5f6935c2017-05-16 19:38:26 +0200665 mlxsw_sp->bridge->ageing_time = ageing_time;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200666 return 0;
667}
668
669static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
670 struct switchdev_trans *trans,
Jiri Pirko135f9ec2015-10-28 10:17:02 +0100671 unsigned long ageing_clock_t)
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200672{
673 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
Jiri Pirko135f9ec2015-10-28 10:17:02 +0100674 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200675 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
676
Ido Schimmel869f63a2016-03-08 12:59:33 -0800677 if (switchdev_trans_ph_prepare(trans)) {
678 if (ageing_time < MLXSW_SP_MIN_AGEING_TIME ||
679 ageing_time > MLXSW_SP_MAX_AGEING_TIME)
680 return -ERANGE;
681 else
682 return 0;
683 }
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200684
685 return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
686}
687
Elad Raz26a4ea02016-01-06 13:01:10 +0100688static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
689 struct switchdev_trans *trans,
690 struct net_device *orig_dev,
691 bool vlan_enabled)
692{
693 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200694 struct mlxsw_sp_bridge_device *bridge_device;
Elad Raz26a4ea02016-01-06 13:01:10 +0100695
Ido Schimmelc57529e2017-05-26 08:37:31 +0200696 if (!switchdev_trans_ph_prepare(trans))
697 return 0;
698
699 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
700 if (WARN_ON(!bridge_device))
Elad Raz26a4ea02016-01-06 13:01:10 +0100701 return -EINVAL;
Elad Raz26a4ea02016-01-06 13:01:10 +0100702
Ido Schimmelc57529e2017-05-26 08:37:31 +0200703 if (bridge_device->vlan_enabled == vlan_enabled)
704 return 0;
705
706 netdev_err(bridge_device->dev, "VLAN filtering can't be changed for existing bridge\n");
707 return -EINVAL;
Elad Raz26a4ea02016-01-06 13:01:10 +0100708}
709
Nogah Frankeldff37b52017-09-20 16:15:01 +0200710static int mlxsw_sp_port_attr_mrouter_set(struct mlxsw_sp_port *mlxsw_sp_port,
711 struct switchdev_trans *trans,
712 struct net_device *orig_dev,
713 bool is_port_mrouter)
Nogah Frankel8ecd4592017-02-09 14:54:47 +0100714{
Ido Schimmelc57529e2017-05-26 08:37:31 +0200715 struct mlxsw_sp_bridge_port *bridge_port;
Nogah Frankel4eb6a3b2017-08-22 10:28:11 +0200716 int err;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200717
Nogah Frankel8ecd4592017-02-09 14:54:47 +0100718 if (switchdev_trans_ph_prepare(trans))
719 return 0;
720
Ido Schimmelc57529e2017-05-26 08:37:31 +0200721 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp_port->mlxsw_sp->bridge,
722 orig_dev);
Ido Schimmel17b334a2017-08-04 14:12:29 +0200723 if (!bridge_port)
724 return 0;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200725
726 if (!bridge_port->bridge_device->multicast_enabled)
Nogah Frankel4eb6a3b2017-08-22 10:28:11 +0200727 goto out;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200728
Nogah Frankel4eb6a3b2017-08-22 10:28:11 +0200729 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port, bridge_port,
730 MLXSW_SP_FLOOD_TYPE_MC,
Nogah Frankeldff37b52017-09-20 16:15:01 +0200731 is_port_mrouter);
Nogah Frankel4eb6a3b2017-08-22 10:28:11 +0200732 if (err)
733 return err;
734
735out:
Nogah Frankeldff37b52017-09-20 16:15:01 +0200736 bridge_port->mrouter = is_port_mrouter;
Nogah Frankel4eb6a3b2017-08-22 10:28:11 +0200737 return 0;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200738}
739
740static int mlxsw_sp_port_mc_disabled_set(struct mlxsw_sp_port *mlxsw_sp_port,
741 struct switchdev_trans *trans,
742 struct net_device *orig_dev,
743 bool mc_disabled)
744{
745 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
746 struct mlxsw_sp_bridge_device *bridge_device;
747 struct mlxsw_sp_bridge_port *bridge_port;
748 int err;
749
750 if (switchdev_trans_ph_prepare(trans))
751 return 0;
752
753 /* It's possible we failed to enslave the port, yet this
754 * operation is executed due to it being deferred.
755 */
756 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, orig_dev);
757 if (!bridge_device)
758 return 0;
759
760 list_for_each_entry(bridge_port, &bridge_device->ports_list, list) {
Ido Schimmela1107482017-05-26 08:37:39 +0200761 enum mlxsw_sp_flood_type packet_type = MLXSW_SP_FLOOD_TYPE_MC;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200762 bool member = mc_disabled ? true : bridge_port->mrouter;
763
764 err = mlxsw_sp_bridge_port_flood_table_set(mlxsw_sp_port,
Ido Schimmela1107482017-05-26 08:37:39 +0200765 bridge_port,
766 packet_type, member);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200767 if (err)
768 return err;
769 }
770
771 bridge_device->multicast_enabled = !mc_disabled;
Nogah Frankel8ecd4592017-02-09 14:54:47 +0100772
773 return 0;
774}
775
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200776static int mlxsw_sp_port_attr_set(struct net_device *dev,
777 const struct switchdev_attr *attr,
778 struct switchdev_trans *trans)
779{
780 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200781 int err;
Ido Schimmel54a73202015-12-15 16:03:41 +0100782
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200783 switch (attr->id) {
784 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
785 err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port, trans,
Ido Schimmelc57529e2017-05-26 08:37:31 +0200786 attr->orig_dev,
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200787 attr->u.stp_state);
788 break;
789 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
790 err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port, trans,
Ido Schimmelc57529e2017-05-26 08:37:31 +0200791 attr->orig_dev,
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200792 attr->u.brport_flags);
793 break;
794 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
795 err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port, trans,
796 attr->u.ageing_time);
797 break;
Elad Raz26a4ea02016-01-06 13:01:10 +0100798 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
799 err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port, trans,
800 attr->orig_dev,
801 attr->u.vlan_filtering);
802 break;
Nogah Frankel8ecd4592017-02-09 14:54:47 +0100803 case SWITCHDEV_ATTR_ID_PORT_MROUTER:
Nogah Frankeldff37b52017-09-20 16:15:01 +0200804 err = mlxsw_sp_port_attr_mrouter_set(mlxsw_sp_port, trans,
805 attr->orig_dev,
806 attr->u.mrouter);
Nogah Frankel8ecd4592017-02-09 14:54:47 +0100807 break;
Nogah Frankel90e0f0c2017-02-09 14:54:49 +0100808 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
809 err = mlxsw_sp_port_mc_disabled_set(mlxsw_sp_port, trans,
Ido Schimmelc57529e2017-05-26 08:37:31 +0200810 attr->orig_dev,
Nogah Frankel90e0f0c2017-02-09 14:54:49 +0100811 attr->u.mc_disabled);
812 break;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200813 default:
814 err = -EOPNOTSUPP;
815 break;
816 }
817
818 return err;
819}
820
Ido Schimmelc57529e2017-05-26 08:37:31 +0200821static bool mlxsw_sp_mc_flood(const struct mlxsw_sp_bridge_port *bridge_port)
822{
823 const struct mlxsw_sp_bridge_device *bridge_device;
824
825 bridge_device = bridge_port->bridge_device;
826 return !bridge_device->multicast_enabled ? true : bridge_port->mrouter;
827}
828
Ido Schimmelc57529e2017-05-26 08:37:31 +0200829static int
830mlxsw_sp_port_vlan_fid_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
831 struct mlxsw_sp_bridge_port *bridge_port)
832{
833 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
Ido Schimmela1107482017-05-26 08:37:39 +0200834 struct mlxsw_sp_bridge_device *bridge_device;
835 u8 local_port = mlxsw_sp_port->local_port;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200836 u16 vid = mlxsw_sp_port_vlan->vid;
837 struct mlxsw_sp_fid *fid;
838 int err;
839
Ido Schimmela1107482017-05-26 08:37:39 +0200840 bridge_device = bridge_port->bridge_device;
841 fid = bridge_device->ops->fid_get(bridge_device, vid);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200842 if (IS_ERR(fid))
843 return PTR_ERR(fid);
844
Ido Schimmela1107482017-05-26 08:37:39 +0200845 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port,
846 bridge_port->flags & BR_FLOOD);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200847 if (err)
Ido Schimmela1107482017-05-26 08:37:39 +0200848 goto err_fid_uc_flood_set;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200849
Ido Schimmela1107482017-05-26 08:37:39 +0200850 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port,
851 mlxsw_sp_mc_flood(bridge_port));
Ido Schimmelc57529e2017-05-26 08:37:31 +0200852 if (err)
Ido Schimmela1107482017-05-26 08:37:39 +0200853 goto err_fid_mc_flood_set;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200854
Ido Schimmela1107482017-05-26 08:37:39 +0200855 err = mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port,
856 true);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200857 if (err)
Ido Schimmela1107482017-05-26 08:37:39 +0200858 goto err_fid_bc_flood_set;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200859
Ido Schimmela1107482017-05-26 08:37:39 +0200860 err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200861 if (err)
Ido Schimmela1107482017-05-26 08:37:39 +0200862 goto err_fid_port_vid_map;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200863
864 mlxsw_sp_port_vlan->fid = fid;
865
866 return 0;
867
Ido Schimmela1107482017-05-26 08:37:39 +0200868err_fid_port_vid_map:
869 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
870err_fid_bc_flood_set:
871 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
872err_fid_mc_flood_set:
873 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
874err_fid_uc_flood_set:
875 mlxsw_sp_fid_put(fid);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200876 return err;
877}
878
879static void
880mlxsw_sp_port_vlan_fid_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
881{
882 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200883 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
Ido Schimmela1107482017-05-26 08:37:39 +0200884 u8 local_port = mlxsw_sp_port->local_port;
Ido Schimmelc57529e2017-05-26 08:37:31 +0200885 u16 vid = mlxsw_sp_port_vlan->vid;
886
887 mlxsw_sp_port_vlan->fid = NULL;
Ido Schimmela1107482017-05-26 08:37:39 +0200888 mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
889 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_BC, local_port, false);
890 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_MC, local_port, false);
891 mlxsw_sp_fid_flood_set(fid, MLXSW_SP_FLOOD_TYPE_UC, local_port, false);
892 mlxsw_sp_fid_put(fid);
Ido Schimmel14d39462016-06-20 23:04:15 +0200893}
894
Ido Schimmelfe9ccc72017-05-16 19:38:31 +0200895static u16
896mlxsw_sp_port_pvid_determine(const struct mlxsw_sp_port *mlxsw_sp_port,
897 u16 vid, bool is_pvid)
Ido Schimmel584d73d2016-08-24 12:00:26 +0200898{
Ido Schimmelfe9ccc72017-05-16 19:38:31 +0200899 if (is_pvid)
900 return vid;
901 else if (mlxsw_sp_port->pvid == vid)
902 return 0; /* Dis-allow untagged packets */
903 else
904 return mlxsw_sp_port->pvid;
Ido Schimmel584d73d2016-08-24 12:00:26 +0200905}
906
Ido Schimmelc57529e2017-05-26 08:37:31 +0200907static int
908mlxsw_sp_port_vlan_bridge_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
909 struct mlxsw_sp_bridge_port *bridge_port)
910{
911 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
912 struct mlxsw_sp_bridge_vlan *bridge_vlan;
913 u16 vid = mlxsw_sp_port_vlan->vid;
914 int err;
915
916 /* No need to continue if only VLAN flags were changed */
917 if (mlxsw_sp_port_vlan->bridge_port)
918 return 0;
919
920 err = mlxsw_sp_port_vlan_fid_join(mlxsw_sp_port_vlan, bridge_port);
921 if (err)
922 return err;
923
924 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid,
925 bridge_port->flags & BR_LEARNING);
926 if (err)
927 goto err_port_vid_learning_set;
928
929 err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid,
930 bridge_port->stp_state);
931 if (err)
932 goto err_port_vid_stp_set;
933
934 bridge_vlan = mlxsw_sp_bridge_vlan_get(bridge_port, vid);
935 if (!bridge_vlan) {
936 err = -ENOMEM;
937 goto err_bridge_vlan_get;
938 }
939
940 list_add(&mlxsw_sp_port_vlan->bridge_vlan_node,
941 &bridge_vlan->port_vlan_list);
942
943 mlxsw_sp_bridge_port_get(mlxsw_sp_port->mlxsw_sp->bridge,
944 bridge_port->dev);
945 mlxsw_sp_port_vlan->bridge_port = bridge_port;
946
947 return 0;
948
949err_bridge_vlan_get:
950 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
951err_port_vid_stp_set:
952 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
953err_port_vid_learning_set:
954 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
955 return err;
956}
957
958void
959mlxsw_sp_port_vlan_bridge_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
960{
961 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
962 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
963 struct mlxsw_sp_bridge_vlan *bridge_vlan;
964 struct mlxsw_sp_bridge_port *bridge_port;
965 u16 vid = mlxsw_sp_port_vlan->vid;
966 bool last;
967
Ido Schimmela1107482017-05-26 08:37:39 +0200968 if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021Q &&
969 mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_8021D))
970 return;
971
Ido Schimmelc57529e2017-05-26 08:37:31 +0200972 bridge_port = mlxsw_sp_port_vlan->bridge_port;
973 bridge_vlan = mlxsw_sp_bridge_vlan_find(bridge_port, vid);
974 last = list_is_singular(&bridge_vlan->port_vlan_list);
975
976 list_del(&mlxsw_sp_port_vlan->bridge_vlan_node);
977 mlxsw_sp_bridge_vlan_put(bridge_vlan);
978 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_DISABLED);
979 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
980 if (last)
981 mlxsw_sp_bridge_port_fdb_flush(mlxsw_sp_port->mlxsw_sp,
Ido Schimmela1107482017-05-26 08:37:39 +0200982 bridge_port,
983 mlxsw_sp_fid_index(fid));
Ido Schimmelc57529e2017-05-26 08:37:31 +0200984 mlxsw_sp_port_vlan_fid_leave(mlxsw_sp_port_vlan);
985
986 mlxsw_sp_bridge_port_put(mlxsw_sp_port->mlxsw_sp->bridge, bridge_port);
987 mlxsw_sp_port_vlan->bridge_port = NULL;
988}
989
990static int
991mlxsw_sp_bridge_port_vlan_add(struct mlxsw_sp_port *mlxsw_sp_port,
992 struct mlxsw_sp_bridge_port *bridge_port,
993 u16 vid, bool is_untagged, bool is_pvid)
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200994{
Ido Schimmelfe9ccc72017-05-16 19:38:31 +0200995 u16 pvid = mlxsw_sp_port_pvid_determine(mlxsw_sp_port, vid, is_pvid);
Ido Schimmelc57529e2017-05-26 08:37:31 +0200996 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
Ido Schimmelfe9ccc72017-05-16 19:38:31 +0200997 u16 old_pvid = mlxsw_sp_port->pvid;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200998 int err;
999
Ido Schimmelc57529e2017-05-26 08:37:31 +02001000 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_get(mlxsw_sp_port, vid);
1001 if (IS_ERR(mlxsw_sp_port_vlan))
1002 return PTR_ERR(mlxsw_sp_port_vlan);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001003
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001004 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true,
1005 is_untagged);
1006 if (err)
1007 goto err_port_vlan_set;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001008
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001009 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1010 if (err)
1011 goto err_port_pvid_set;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001012
Ido Schimmelc57529e2017-05-26 08:37:31 +02001013 err = mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001014 if (err)
Ido Schimmelc57529e2017-05-26 08:37:31 +02001015 goto err_port_vlan_bridge_join;
Ido Schimmel584d73d2016-08-24 12:00:26 +02001016
Ido Schimmelb07a9662015-11-19 12:27:40 +01001017 return 0;
1018
Ido Schimmelc57529e2017-05-26 08:37:31 +02001019err_port_vlan_bridge_join:
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001020 mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid);
Ido Schimmelb07a9662015-11-19 12:27:40 +01001021err_port_pvid_set:
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001022 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1023err_port_vlan_set:
Ido Schimmelc57529e2017-05-26 08:37:31 +02001024 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
Ido Schimmelb07a9662015-11-19 12:27:40 +01001025 return err;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001026}
1027
1028static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
1029 const struct switchdev_obj_port_vlan *vlan,
1030 struct switchdev_trans *trans)
1031{
Elad Raze4a13052016-01-06 13:01:09 +01001032 bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1033 bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001034 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1035 struct net_device *orig_dev = vlan->obj.orig_dev;
1036 struct mlxsw_sp_bridge_port *bridge_port;
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001037 u16 vid;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001038
1039 if (switchdev_trans_ph_prepare(trans))
1040 return 0;
1041
Ido Schimmelc57529e2017-05-26 08:37:31 +02001042 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1043 if (WARN_ON(!bridge_port))
1044 return -EINVAL;
1045
1046 if (!bridge_port->bridge_device->vlan_enabled)
1047 return 0;
1048
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001049 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1050 int err;
1051
Ido Schimmelc57529e2017-05-26 08:37:31 +02001052 err = mlxsw_sp_bridge_port_vlan_add(mlxsw_sp_port, bridge_port,
1053 vid, flag_untagged,
1054 flag_pvid);
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001055 if (err)
1056 return err;
1057 }
1058
1059 return 0;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001060}
1061
Ido Schimmelc57529e2017-05-26 08:37:31 +02001062static enum mlxsw_reg_sfdf_flush_type mlxsw_sp_fdb_flush_type(bool lagged)
1063{
1064 return lagged ? MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID :
1065 MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID;
1066}
1067
1068static int
1069mlxsw_sp_bridge_port_fdb_flush(struct mlxsw_sp *mlxsw_sp,
1070 struct mlxsw_sp_bridge_port *bridge_port,
1071 u16 fid_index)
1072{
1073 bool lagged = bridge_port->lagged;
1074 char sfdf_pl[MLXSW_REG_SFDF_LEN];
1075 u16 system_port;
1076
1077 system_port = lagged ? bridge_port->lag_id : bridge_port->system_port;
1078 mlxsw_reg_sfdf_pack(sfdf_pl, mlxsw_sp_fdb_flush_type(lagged));
1079 mlxsw_reg_sfdf_fid_set(sfdf_pl, fid_index);
1080 mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl, system_port);
1081
1082 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl);
1083}
1084
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001085static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001086{
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001087 return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
1088 MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY;
1089}
1090
1091static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
1092{
1093 return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
1094 MLXSW_REG_SFD_OP_WRITE_REMOVE;
1095}
1096
Ido Schimmel6e095fd2016-07-04 08:23:13 +02001097static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1098 const char *mac, u16 fid, bool adding,
1099 enum mlxsw_reg_sfd_rec_action action,
1100 bool dynamic)
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001101{
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001102 char *sfd_pl;
1103 int err;
1104
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001105 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1106 if (!sfd_pl)
1107 return -ENOMEM;
1108
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001109 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1110 mlxsw_reg_sfd_uc_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
Ido Schimmel6e095fd2016-07-04 08:23:13 +02001111 mac, fid, action, local_port);
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001112 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1113 kfree(sfd_pl);
1114
1115 return err;
1116}
1117
Ido Schimmel6e095fd2016-07-04 08:23:13 +02001118static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
1119 const char *mac, u16 fid, bool adding,
1120 bool dynamic)
1121{
1122 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding,
1123 MLXSW_REG_SFD_REC_ACTION_NOP, dynamic);
1124}
1125
1126int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid,
1127 bool adding)
1128{
1129 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding,
1130 MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER,
1131 false);
1132}
1133
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001134static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
Ido Schimmel64771e32015-12-15 16:03:46 +01001135 const char *mac, u16 fid, u16 lag_vid,
1136 bool adding, bool dynamic)
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001137{
1138 char *sfd_pl;
1139 int err;
1140
1141 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1142 if (!sfd_pl)
1143 return -ENOMEM;
1144
1145 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1146 mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
Ido Schimmel64771e32015-12-15 16:03:46 +01001147 mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
1148 lag_vid, lag_id);
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001149 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001150 kfree(sfd_pl);
1151
1152 return err;
1153}
1154
1155static int
Arkadi Sharshevskyaf0613782017-06-08 08:44:20 +02001156mlxsw_sp_port_fdb_set(struct mlxsw_sp_port *mlxsw_sp_port,
1157 struct switchdev_notifier_fdb_info *fdb_info, bool adding)
1158{
1159 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1160 struct net_device *orig_dev = fdb_info->info.dev;
1161 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1162 struct mlxsw_sp_bridge_device *bridge_device;
1163 struct mlxsw_sp_bridge_port *bridge_port;
1164 u16 fid_index, vid;
1165
1166 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1167 if (!bridge_port)
1168 return -EINVAL;
1169
1170 bridge_device = bridge_port->bridge_device;
1171 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1172 bridge_device,
1173 fdb_info->vid);
1174 if (!mlxsw_sp_port_vlan)
1175 return 0;
1176
1177 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
1178 vid = mlxsw_sp_port_vlan->vid;
1179
1180 if (!bridge_port->lagged)
1181 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp,
1182 bridge_port->system_port,
1183 fdb_info->addr, fid_index,
1184 adding, false);
1185 else
1186 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp,
1187 bridge_port->lag_id,
1188 fdb_info->addr, fid_index,
1189 vid, adding, false);
1190}
1191
Elad Raz3a49b4f2016-01-10 21:06:28 +01001192static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
Nogah Frankel5f9abc52017-09-20 16:15:05 +02001193 u16 fid, u16 mid_idx, bool adding)
Elad Raz3a49b4f2016-01-10 21:06:28 +01001194{
1195 char *sfd_pl;
1196 int err;
1197
1198 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1199 if (!sfd_pl)
1200 return -ENOMEM;
1201
1202 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
1203 mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
Nogah Frankel5f9abc52017-09-20 16:15:05 +02001204 MLXSW_REG_SFD_REC_ACTION_NOP, mid_idx);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001205 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1206 kfree(sfd_pl);
1207 return err;
1208}
1209
Nogah Frankel5f9abc52017-09-20 16:15:05 +02001210/* clean the an entry from the HW and write there a full new entry */
1211static int mlxsw_sp_port_smid_full_entry(struct mlxsw_sp *mlxsw_sp,
1212 u16 mid_idx)
Elad Raz3a49b4f2016-01-10 21:06:28 +01001213{
Elad Raz3a49b4f2016-01-10 21:06:28 +01001214 char *smid_pl;
1215 int err, i;
1216
1217 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1218 if (!smid_pl)
1219 return -ENOMEM;
1220
Nogah Frankel5f9abc52017-09-20 16:15:05 +02001221 mlxsw_reg_smid_pack(smid_pl, mid_idx, 0, false);
1222 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++) {
1223 if (mlxsw_sp->ports[i])
1224 mlxsw_reg_smid_port_mask_set(smid_pl, i, 1);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001225 }
Nogah Frankel5f9abc52017-09-20 16:15:05 +02001226
1227 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1228 kfree(smid_pl);
1229 return err;
1230}
1231
1232static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port,
1233 u16 mid_idx, bool add)
1234{
1235 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1236 char *smid_pl;
1237 int err;
1238
1239 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
1240 if (!smid_pl)
1241 return -ENOMEM;
1242
1243 mlxsw_reg_smid_pack(smid_pl, mid_idx, mlxsw_sp_port->local_port, add);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001244 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
1245 kfree(smid_pl);
1246 return err;
1247}
1248
Nogah Frankelb80888a2017-09-20 16:15:04 +02001249static struct
1250mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp_bridge_device *bridge_device,
1251 const unsigned char *addr,
1252 u16 fid)
Elad Raz3a49b4f2016-01-10 21:06:28 +01001253{
1254 struct mlxsw_sp_mid *mid;
1255
Nogah Frankelb80888a2017-09-20 16:15:04 +02001256 list_for_each_entry(mid, &bridge_device->mids_list, list) {
Ido Schimmel46d08472016-10-30 10:09:22 +01001257 if (ether_addr_equal(mid->addr, addr) && mid->fid == fid)
Elad Raz3a49b4f2016-01-10 21:06:28 +01001258 return mid;
1259 }
1260 return NULL;
1261}
1262
Nogah Frankelb80888a2017-09-20 16:15:04 +02001263static struct
1264mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
1265 struct mlxsw_sp_bridge_device *bridge_device,
1266 const unsigned char *addr,
1267 u16 fid)
Elad Raz3a49b4f2016-01-10 21:06:28 +01001268{
1269 struct mlxsw_sp_mid *mid;
Nogah Frankel4cdc35e2017-09-20 16:15:02 +02001270 size_t alloc_size;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001271 u16 mid_idx;
1272
Ido Schimmel5f6935c2017-05-16 19:38:26 +02001273 mid_idx = find_first_zero_bit(mlxsw_sp->bridge->mids_bitmap,
Elad Raz3a49b4f2016-01-10 21:06:28 +01001274 MLXSW_SP_MID_MAX);
1275 if (mid_idx == MLXSW_SP_MID_MAX)
1276 return NULL;
1277
1278 mid = kzalloc(sizeof(*mid), GFP_KERNEL);
1279 if (!mid)
1280 return NULL;
1281
Nogah Frankel4cdc35e2017-09-20 16:15:02 +02001282 alloc_size = sizeof(unsigned long) *
1283 BITS_TO_LONGS(mlxsw_core_max_ports(mlxsw_sp->core));
1284 mid->ports_in_mid = kzalloc(alloc_size, GFP_KERNEL);
1285 if (!mid->ports_in_mid) {
1286 kfree(mid);
1287 return NULL;
1288 }
1289
Ido Schimmel5f6935c2017-05-16 19:38:26 +02001290 set_bit(mid_idx, mlxsw_sp->bridge->mids_bitmap);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001291 ether_addr_copy(mid->addr, addr);
Ido Schimmel46d08472016-10-30 10:09:22 +01001292 mid->fid = fid;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001293 mid->mid = mid_idx;
Nogah Frankelb80888a2017-09-20 16:15:04 +02001294 list_add_tail(&mid->list, &bridge_device->mids_list);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001295
1296 return mid;
1297}
1298
Nogah Frankel0161b952017-09-20 16:15:03 +02001299static int mlxsw_sp_port_remove_from_mid(struct mlxsw_sp_port *mlxsw_sp_port,
1300 struct mlxsw_sp_mid *mid)
Elad Raz3a49b4f2016-01-10 21:06:28 +01001301{
Nogah Frankel4cdc35e2017-09-20 16:15:02 +02001302 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1303
1304 clear_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
Nogah Frankel0161b952017-09-20 16:15:03 +02001305 if (bitmap_empty(mid->ports_in_mid,
1306 mlxsw_core_max_ports(mlxsw_sp->core))) {
Elad Raz3a49b4f2016-01-10 21:06:28 +01001307 list_del(&mid->list);
Ido Schimmel5f6935c2017-05-16 19:38:26 +02001308 clear_bit(mid->mid, mlxsw_sp->bridge->mids_bitmap);
Nogah Frankel4cdc35e2017-09-20 16:15:02 +02001309 kfree(mid->ports_in_mid);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001310 kfree(mid);
1311 return 1;
1312 }
1313 return 0;
1314}
1315
1316static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
1317 const struct switchdev_obj_port_mdb *mdb,
1318 struct switchdev_trans *trans)
1319{
1320 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001321 struct net_device *orig_dev = mdb->obj.orig_dev;
1322 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001323 struct net_device *dev = mlxsw_sp_port->dev;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001324 struct mlxsw_sp_bridge_device *bridge_device;
1325 struct mlxsw_sp_bridge_port *bridge_port;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001326 struct mlxsw_sp_mid *mid;
Nogah Frankel0161b952017-09-20 16:15:03 +02001327 bool is_new_mid = false;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001328 u16 fid_index;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001329 int err = 0;
1330
1331 if (switchdev_trans_ph_prepare(trans))
1332 return 0;
1333
Ido Schimmelc57529e2017-05-26 08:37:31 +02001334 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
Ido Schimmel17b334a2017-08-04 14:12:29 +02001335 if (!bridge_port)
1336 return 0;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001337
1338 bridge_device = bridge_port->bridge_device;
1339 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1340 bridge_device,
1341 mdb->vid);
Ido Schimmel17b334a2017-08-04 14:12:29 +02001342 if (!mlxsw_sp_port_vlan)
1343 return 0;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001344
Ido Schimmela1107482017-05-26 08:37:39 +02001345 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
Ido Schimmelc57529e2017-05-26 08:37:31 +02001346
Nogah Frankelb80888a2017-09-20 16:15:04 +02001347 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001348 if (!mid) {
Nogah Frankelb80888a2017-09-20 16:15:04 +02001349 mid = __mlxsw_sp_mc_alloc(mlxsw_sp, bridge_device, mdb->addr,
1350 fid_index);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001351 if (!mid) {
1352 netdev_err(dev, "Unable to allocate MC group\n");
1353 return -ENOMEM;
1354 }
Nogah Frankel0161b952017-09-20 16:15:03 +02001355 is_new_mid = true;
Nogah Frankel5f9abc52017-09-20 16:15:05 +02001356 mlxsw_sp_port_smid_full_entry(mlxsw_sp, mid->mid);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001357 }
Nogah Frankel4cdc35e2017-09-20 16:15:02 +02001358 set_bit(mlxsw_sp_port->local_port, mid->ports_in_mid);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001359
Nogah Frankel5f9abc52017-09-20 16:15:05 +02001360 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001361 if (err) {
1362 netdev_err(dev, "Unable to set SMID\n");
1363 goto err_out;
1364 }
1365
Nogah Frankel0161b952017-09-20 16:15:03 +02001366 if (is_new_mid) {
Ido Schimmelc57529e2017-05-26 08:37:31 +02001367 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mdb->addr, fid_index,
1368 mid->mid, true);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001369 if (err) {
1370 netdev_err(dev, "Unable to set MC SFD\n");
1371 goto err_out;
1372 }
1373 }
1374
1375 return 0;
1376
1377err_out:
Nogah Frankel0161b952017-09-20 16:15:03 +02001378 mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001379 return err;
1380}
1381
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001382static int mlxsw_sp_port_obj_add(struct net_device *dev,
1383 const struct switchdev_obj *obj,
1384 struct switchdev_trans *trans)
1385{
1386 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1387 int err = 0;
1388
1389 switch (obj->id) {
1390 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1391 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port,
1392 SWITCHDEV_OBJ_PORT_VLAN(obj),
1393 trans);
1394 break;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001395 case SWITCHDEV_OBJ_ID_PORT_MDB:
1396 err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
1397 SWITCHDEV_OBJ_PORT_MDB(obj),
1398 trans);
1399 break;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001400 default:
1401 err = -EOPNOTSUPP;
1402 break;
1403 }
1404
1405 return err;
1406}
1407
Ido Schimmelc57529e2017-05-26 08:37:31 +02001408static void
1409mlxsw_sp_bridge_port_vlan_del(struct mlxsw_sp_port *mlxsw_sp_port,
1410 struct mlxsw_sp_bridge_port *bridge_port, u16 vid)
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001411{
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001412 u16 pvid = mlxsw_sp_port->pvid == vid ? 0 : vid;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001413 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001414
Ido Schimmelc57529e2017-05-26 08:37:31 +02001415 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1416 if (WARN_ON(!mlxsw_sp_port_vlan))
1417 return;
1418
1419 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001420 mlxsw_sp_port_pvid_set(mlxsw_sp_port, pvid);
1421 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
Ido Schimmelc57529e2017-05-26 08:37:31 +02001422 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001423}
1424
1425static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1426 const struct switchdev_obj_port_vlan *vlan)
1427{
Ido Schimmelc57529e2017-05-26 08:37:31 +02001428 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1429 struct net_device *orig_dev = vlan->obj.orig_dev;
1430 struct mlxsw_sp_bridge_port *bridge_port;
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001431 u16 vid;
1432
Ido Schimmelc57529e2017-05-26 08:37:31 +02001433 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
1434 if (WARN_ON(!bridge_port))
1435 return -EINVAL;
1436
1437 if (!bridge_port->bridge_device->vlan_enabled)
1438 return 0;
1439
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001440 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++)
Ido Schimmelc57529e2017-05-26 08:37:31 +02001441 mlxsw_sp_bridge_port_vlan_del(mlxsw_sp_port, bridge_port, vid);
Ido Schimmelfe9ccc72017-05-16 19:38:31 +02001442
1443 return 0;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001444}
1445
Elad Raz3a49b4f2016-01-10 21:06:28 +01001446static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1447 const struct switchdev_obj_port_mdb *mdb)
1448{
1449 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001450 struct net_device *orig_dev = mdb->obj.orig_dev;
1451 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1452 struct mlxsw_sp_bridge_device *bridge_device;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001453 struct net_device *dev = mlxsw_sp_port->dev;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001454 struct mlxsw_sp_bridge_port *bridge_port;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001455 struct mlxsw_sp_mid *mid;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001456 u16 fid_index;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001457 u16 mid_idx;
1458 int err = 0;
1459
Ido Schimmelc57529e2017-05-26 08:37:31 +02001460 bridge_port = mlxsw_sp_bridge_port_find(mlxsw_sp->bridge, orig_dev);
Ido Schimmel17b334a2017-08-04 14:12:29 +02001461 if (!bridge_port)
1462 return 0;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001463
1464 bridge_device = bridge_port->bridge_device;
1465 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_bridge(mlxsw_sp_port,
1466 bridge_device,
1467 mdb->vid);
Ido Schimmel17b334a2017-08-04 14:12:29 +02001468 if (!mlxsw_sp_port_vlan)
1469 return 0;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001470
Ido Schimmela1107482017-05-26 08:37:39 +02001471 fid_index = mlxsw_sp_fid_index(mlxsw_sp_port_vlan->fid);
Ido Schimmelc57529e2017-05-26 08:37:31 +02001472
Nogah Frankelb80888a2017-09-20 16:15:04 +02001473 mid = __mlxsw_sp_mc_get(bridge_device, mdb->addr, fid_index);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001474 if (!mid) {
1475 netdev_err(dev, "Unable to remove port from MC DB\n");
1476 return -EINVAL;
1477 }
1478
Nogah Frankel5f9abc52017-09-20 16:15:05 +02001479 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001480 if (err)
1481 netdev_err(dev, "Unable to remove port from SMID\n");
1482
1483 mid_idx = mid->mid;
Nogah Frankel0161b952017-09-20 16:15:03 +02001484 if (mlxsw_sp_port_remove_from_mid(mlxsw_sp_port, mid)) {
Ido Schimmelc57529e2017-05-26 08:37:31 +02001485 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mdb->addr, fid_index,
1486 mid_idx, false);
Elad Raz3a49b4f2016-01-10 21:06:28 +01001487 if (err)
1488 netdev_err(dev, "Unable to remove MC SFD\n");
1489 }
1490
1491 return err;
1492}
1493
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001494static int mlxsw_sp_port_obj_del(struct net_device *dev,
1495 const struct switchdev_obj *obj)
1496{
1497 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1498 int err = 0;
1499
1500 switch (obj->id) {
1501 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1502 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1503 SWITCHDEV_OBJ_PORT_VLAN(obj));
1504 break;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001505 case SWITCHDEV_OBJ_ID_PORT_MDB:
1506 err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1507 SWITCHDEV_OBJ_PORT_MDB(obj));
Dan Carpenter00ae40e2016-01-13 15:28:23 +03001508 break;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001509 default:
1510 err = -EOPNOTSUPP;
1511 break;
1512 }
1513
1514 return err;
1515}
1516
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001517static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
1518 u16 lag_id)
1519{
1520 struct mlxsw_sp_port *mlxsw_sp_port;
Jiri Pirkoc1a38312016-10-21 16:07:23 +02001521 u64 max_lag_members;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001522 int i;
1523
Jiri Pirkoc1a38312016-10-21 16:07:23 +02001524 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
1525 MAX_LAG_MEMBERS);
1526 for (i = 0; i < max_lag_members; i++) {
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001527 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
1528 if (mlxsw_sp_port)
1529 return mlxsw_sp_port;
1530 }
1531 return NULL;
1532}
1533
Jiri Pirkoc7070fc2015-10-28 10:17:05 +01001534static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = {
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001535 .switchdev_port_attr_get = mlxsw_sp_port_attr_get,
1536 .switchdev_port_attr_set = mlxsw_sp_port_attr_set,
1537 .switchdev_port_obj_add = mlxsw_sp_port_obj_add,
1538 .switchdev_port_obj_del = mlxsw_sp_port_obj_del,
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001539};
1540
Ido Schimmelc57529e2017-05-26 08:37:31 +02001541static int
1542mlxsw_sp_bridge_8021q_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1543 struct mlxsw_sp_bridge_port *bridge_port,
1544 struct mlxsw_sp_port *mlxsw_sp_port)
1545{
1546 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1547
1548 if (is_vlan_dev(bridge_port->dev))
1549 return -EINVAL;
1550
1551 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, 1);
1552 if (WARN_ON(!mlxsw_sp_port_vlan))
1553 return -EINVAL;
1554
1555 /* Let VLAN-aware bridge take care of its own VLANs */
1556 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1557
1558 return 0;
1559}
1560
1561static void
1562mlxsw_sp_bridge_8021q_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
1563 struct mlxsw_sp_bridge_port *bridge_port,
1564 struct mlxsw_sp_port *mlxsw_sp_port)
1565{
1566 mlxsw_sp_port_vlan_get(mlxsw_sp_port, 1);
1567 /* Make sure untagged frames are allowed to ingress */
1568 mlxsw_sp_port_pvid_set(mlxsw_sp_port, 1);
1569}
1570
Ido Schimmela1107482017-05-26 08:37:39 +02001571static struct mlxsw_sp_fid *
1572mlxsw_sp_bridge_8021q_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
1573 u16 vid)
1574{
1575 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
1576
1577 return mlxsw_sp_fid_8021q_get(mlxsw_sp, vid);
1578}
1579
Ido Schimmelc57529e2017-05-26 08:37:31 +02001580static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021q_ops = {
1581 .port_join = mlxsw_sp_bridge_8021q_port_join,
1582 .port_leave = mlxsw_sp_bridge_8021q_port_leave,
Ido Schimmela1107482017-05-26 08:37:39 +02001583 .fid_get = mlxsw_sp_bridge_8021q_fid_get,
Ido Schimmelc57529e2017-05-26 08:37:31 +02001584};
1585
1586static bool
1587mlxsw_sp_port_is_br_member(const struct mlxsw_sp_port *mlxsw_sp_port,
1588 const struct net_device *br_dev)
1589{
1590 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1591
1592 list_for_each_entry(mlxsw_sp_port_vlan, &mlxsw_sp_port->vlans_list,
1593 list) {
1594 if (mlxsw_sp_port_vlan->bridge_port &&
1595 mlxsw_sp_port_vlan->bridge_port->bridge_device->dev ==
1596 br_dev)
1597 return true;
1598 }
1599
1600 return false;
1601}
1602
1603static int
1604mlxsw_sp_bridge_8021d_port_join(struct mlxsw_sp_bridge_device *bridge_device,
1605 struct mlxsw_sp_bridge_port *bridge_port,
1606 struct mlxsw_sp_port *mlxsw_sp_port)
1607{
1608 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001609 u16 vid;
1610
1611 if (!is_vlan_dev(bridge_port->dev))
1612 return -EINVAL;
1613 vid = vlan_dev_vlan_id(bridge_port->dev);
1614
1615 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1616 if (WARN_ON(!mlxsw_sp_port_vlan))
1617 return -EINVAL;
Ido Schimmelc57529e2017-05-26 08:37:31 +02001618
1619 if (mlxsw_sp_port_is_br_member(mlxsw_sp_port, bridge_device->dev)) {
1620 netdev_err(mlxsw_sp_port->dev, "Can't bridge VLAN uppers of the same port\n");
1621 return -EINVAL;
1622 }
1623
1624 /* Port is no longer usable as a router interface */
Ido Schimmela1107482017-05-26 08:37:39 +02001625 if (mlxsw_sp_port_vlan->fid)
1626 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
Ido Schimmelc57529e2017-05-26 08:37:31 +02001627
1628 return mlxsw_sp_port_vlan_bridge_join(mlxsw_sp_port_vlan, bridge_port);
1629}
1630
1631static void
1632mlxsw_sp_bridge_8021d_port_leave(struct mlxsw_sp_bridge_device *bridge_device,
1633 struct mlxsw_sp_bridge_port *bridge_port,
1634 struct mlxsw_sp_port *mlxsw_sp_port)
1635{
1636 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1637 u16 vid = vlan_dev_vlan_id(bridge_port->dev);
1638
1639 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1640 if (WARN_ON(!mlxsw_sp_port_vlan))
1641 return;
1642
1643 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1644}
1645
Ido Schimmela1107482017-05-26 08:37:39 +02001646static struct mlxsw_sp_fid *
1647mlxsw_sp_bridge_8021d_fid_get(struct mlxsw_sp_bridge_device *bridge_device,
1648 u16 vid)
1649{
1650 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(bridge_device->dev);
1651
1652 return mlxsw_sp_fid_8021d_get(mlxsw_sp, bridge_device->dev->ifindex);
1653}
1654
Ido Schimmelc57529e2017-05-26 08:37:31 +02001655static const struct mlxsw_sp_bridge_ops mlxsw_sp_bridge_8021d_ops = {
1656 .port_join = mlxsw_sp_bridge_8021d_port_join,
1657 .port_leave = mlxsw_sp_bridge_8021d_port_leave,
Ido Schimmela1107482017-05-26 08:37:39 +02001658 .fid_get = mlxsw_sp_bridge_8021d_fid_get,
Ido Schimmelc57529e2017-05-26 08:37:31 +02001659};
1660
1661int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port,
1662 struct net_device *brport_dev,
1663 struct net_device *br_dev)
1664{
1665 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1666 struct mlxsw_sp_bridge_device *bridge_device;
1667 struct mlxsw_sp_bridge_port *bridge_port;
1668 int err;
1669
1670 bridge_port = mlxsw_sp_bridge_port_get(mlxsw_sp->bridge, brport_dev);
1671 if (IS_ERR(bridge_port))
1672 return PTR_ERR(bridge_port);
1673 bridge_device = bridge_port->bridge_device;
1674
1675 err = bridge_device->ops->port_join(bridge_device, bridge_port,
1676 mlxsw_sp_port);
1677 if (err)
1678 goto err_port_join;
1679
1680 return 0;
1681
1682err_port_join:
1683 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
1684 return err;
1685}
1686
1687void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port,
1688 struct net_device *brport_dev,
1689 struct net_device *br_dev)
1690{
1691 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1692 struct mlxsw_sp_bridge_device *bridge_device;
1693 struct mlxsw_sp_bridge_port *bridge_port;
1694
1695 bridge_device = mlxsw_sp_bridge_device_find(mlxsw_sp->bridge, br_dev);
1696 if (!bridge_device)
1697 return;
1698 bridge_port = __mlxsw_sp_bridge_port_find(bridge_device, brport_dev);
1699 if (!bridge_port)
1700 return;
1701
1702 bridge_device->ops->port_leave(bridge_device, bridge_port,
1703 mlxsw_sp_port);
1704 mlxsw_sp_bridge_port_put(mlxsw_sp->bridge, bridge_port);
1705}
1706
Arkadi Sharshevsky1b40dc32017-06-08 08:44:19 +02001707static void
1708mlxsw_sp_fdb_call_notifiers(enum switchdev_notifier_type type,
1709 const char *mac, u16 vid,
1710 struct net_device *dev)
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001711{
1712 struct switchdev_notifier_fdb_info info;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001713
Arkadi Sharshevskya989cdb2017-06-08 08:44:16 +02001714 info.addr = mac;
1715 info.vid = vid;
Arkadi Sharshevsky1b40dc32017-06-08 08:44:19 +02001716 call_switchdev_notifiers(type, dev, &info.info);
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001717}
1718
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001719static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
1720 char *sfn_pl, int rec_index,
1721 bool adding)
1722{
Ido Schimmelc57529e2017-05-26 08:37:31 +02001723 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1724 struct mlxsw_sp_bridge_device *bridge_device;
1725 struct mlxsw_sp_bridge_port *bridge_port;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001726 struct mlxsw_sp_port *mlxsw_sp_port;
Arkadi Sharshevsky1b40dc32017-06-08 08:44:19 +02001727 enum switchdev_notifier_type type;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001728 char mac[ETH_ALEN];
1729 u8 local_port;
Ido Schimmel9de6a802015-12-15 16:03:40 +01001730 u16 vid, fid;
Jiri Pirko12f15012016-01-07 11:50:30 +01001731 bool do_notification = true;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001732 int err;
1733
Ido Schimmel9de6a802015-12-15 16:03:40 +01001734 mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001735 mlxsw_sp_port = mlxsw_sp->ports[local_port];
1736 if (!mlxsw_sp_port) {
1737 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
Jiri Pirko12f15012016-01-07 11:50:30 +01001738 goto just_remove;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001739 }
1740
Ido Schimmelc57529e2017-05-26 08:37:31 +02001741 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
1742 if (!mlxsw_sp_port_vlan) {
1743 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
1744 goto just_remove;
Ido Schimmelaac78a42015-12-15 16:03:42 +01001745 }
1746
Ido Schimmelc57529e2017-05-26 08:37:31 +02001747 bridge_port = mlxsw_sp_port_vlan->bridge_port;
1748 if (!bridge_port) {
1749 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
1750 goto just_remove;
1751 }
1752
1753 bridge_device = bridge_port->bridge_device;
1754 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
1755
Jiri Pirko12f15012016-01-07 11:50:30 +01001756do_fdb_op:
Jiri Pirko2fa9d452016-01-07 11:50:29 +01001757 err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
Jiri Pirko12f15012016-01-07 11:50:30 +01001758 adding, true);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001759 if (err) {
Ido Schimmelc0e01ea2017-05-18 13:03:52 +02001760 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001761 return;
1762 }
1763
Jiri Pirko12f15012016-01-07 11:50:30 +01001764 if (!do_notification)
1765 return;
Arkadi Sharshevsky1b40dc32017-06-08 08:44:19 +02001766 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
1767 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev);
Arkadi Sharshevskya989cdb2017-06-08 08:44:16 +02001768
Jiri Pirko12f15012016-01-07 11:50:30 +01001769 return;
1770
1771just_remove:
1772 adding = false;
1773 do_notification = false;
1774 goto do_fdb_op;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001775}
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001776
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001777static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
1778 char *sfn_pl, int rec_index,
1779 bool adding)
1780{
Ido Schimmelc57529e2017-05-26 08:37:31 +02001781 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1782 struct mlxsw_sp_bridge_device *bridge_device;
1783 struct mlxsw_sp_bridge_port *bridge_port;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001784 struct mlxsw_sp_port *mlxsw_sp_port;
Arkadi Sharshevsky1b40dc32017-06-08 08:44:19 +02001785 enum switchdev_notifier_type type;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001786 char mac[ETH_ALEN];
Ido Schimmel64771e32015-12-15 16:03:46 +01001787 u16 lag_vid = 0;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001788 u16 lag_id;
Ido Schimmel9de6a802015-12-15 16:03:40 +01001789 u16 vid, fid;
Jiri Pirko12f15012016-01-07 11:50:30 +01001790 bool do_notification = true;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001791 int err;
1792
Ido Schimmel9de6a802015-12-15 16:03:40 +01001793 mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001794 mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
1795 if (!mlxsw_sp_port) {
1796 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
Jiri Pirko12f15012016-01-07 11:50:30 +01001797 goto just_remove;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001798 }
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001799
Ido Schimmelc57529e2017-05-26 08:37:31 +02001800 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_fid(mlxsw_sp_port, fid);
1801 if (!mlxsw_sp_port_vlan) {
1802 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching {Port, VID} following FDB notification\n");
1803 goto just_remove;
Ido Schimmelaac78a42015-12-15 16:03:42 +01001804 }
1805
Ido Schimmelc57529e2017-05-26 08:37:31 +02001806 bridge_port = mlxsw_sp_port_vlan->bridge_port;
1807 if (!bridge_port) {
1808 netdev_err(mlxsw_sp_port->dev, "{Port, VID} not associated with a bridge\n");
1809 goto just_remove;
1810 }
1811
1812 bridge_device = bridge_port->bridge_device;
1813 vid = bridge_device->vlan_enabled ? mlxsw_sp_port_vlan->vid : 0;
1814 lag_vid = mlxsw_sp_port_vlan->vid;
1815
Jiri Pirko12f15012016-01-07 11:50:30 +01001816do_fdb_op:
Ido Schimmel64771e32015-12-15 16:03:46 +01001817 err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
Jiri Pirko12f15012016-01-07 11:50:30 +01001818 adding, true);
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001819 if (err) {
Ido Schimmelc0e01ea2017-05-18 13:03:52 +02001820 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to set FDB entry\n");
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001821 return;
1822 }
1823
Jiri Pirko12f15012016-01-07 11:50:30 +01001824 if (!do_notification)
1825 return;
Arkadi Sharshevsky1b40dc32017-06-08 08:44:19 +02001826 type = adding ? SWITCHDEV_FDB_ADD_TO_BRIDGE : SWITCHDEV_FDB_DEL_TO_BRIDGE;
1827 mlxsw_sp_fdb_call_notifiers(type, mac, vid, bridge_port->dev);
Arkadi Sharshevskya989cdb2017-06-08 08:44:16 +02001828
Jiri Pirko12f15012016-01-07 11:50:30 +01001829 return;
1830
1831just_remove:
1832 adding = false;
1833 do_notification = false;
1834 goto do_fdb_op;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001835}
1836
1837static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
1838 char *sfn_pl, int rec_index)
1839{
1840 switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
1841 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
1842 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
1843 rec_index, true);
1844 break;
1845 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
1846 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
1847 rec_index, false);
1848 break;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001849 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
1850 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
1851 rec_index, true);
1852 break;
1853 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
1854 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
1855 rec_index, false);
1856 break;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001857 }
1858}
1859
1860static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp)
1861{
Ido Schimmel5f6935c2017-05-16 19:38:26 +02001862 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
1863
1864 mlxsw_core_schedule_dw(&bridge->fdb_notify.dw,
1865 msecs_to_jiffies(bridge->fdb_notify.interval));
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001866}
1867
1868static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
1869{
Ido Schimmel5f6935c2017-05-16 19:38:26 +02001870 struct mlxsw_sp_bridge *bridge;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001871 struct mlxsw_sp *mlxsw_sp;
1872 char *sfn_pl;
1873 u8 num_rec;
1874 int i;
1875 int err;
1876
1877 sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
1878 if (!sfn_pl)
1879 return;
1880
Ido Schimmel5f6935c2017-05-16 19:38:26 +02001881 bridge = container_of(work, struct mlxsw_sp_bridge, fdb_notify.dw.work);
1882 mlxsw_sp = bridge->mlxsw_sp;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001883
Ido Schimmel4f2c6ae2016-01-27 15:16:43 +01001884 rtnl_lock();
Ido Schimmel1803e0f2016-08-24 12:00:23 +02001885 mlxsw_reg_sfn_pack(sfn_pl);
1886 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
1887 if (err) {
1888 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
1889 goto out;
1890 }
1891 num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
1892 for (i = 0; i < num_rec; i++)
1893 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001894
Ido Schimmel1803e0f2016-08-24 12:00:23 +02001895out:
Ido Schimmel4f2c6ae2016-01-27 15:16:43 +01001896 rtnl_unlock();
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001897 kfree(sfn_pl);
1898 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
1899}
1900
Arkadi Sharshevskyaf0613782017-06-08 08:44:20 +02001901struct mlxsw_sp_switchdev_event_work {
1902 struct work_struct work;
1903 struct switchdev_notifier_fdb_info fdb_info;
1904 struct net_device *dev;
1905 unsigned long event;
1906};
1907
1908static void mlxsw_sp_switchdev_event_work(struct work_struct *work)
1909{
1910 struct mlxsw_sp_switchdev_event_work *switchdev_work =
1911 container_of(work, struct mlxsw_sp_switchdev_event_work, work);
1912 struct net_device *dev = switchdev_work->dev;
1913 struct switchdev_notifier_fdb_info *fdb_info;
1914 struct mlxsw_sp_port *mlxsw_sp_port;
1915 int err;
1916
1917 rtnl_lock();
1918 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
1919 if (!mlxsw_sp_port)
1920 goto out;
1921
1922 switch (switchdev_work->event) {
1923 case SWITCHDEV_FDB_ADD_TO_DEVICE:
1924 fdb_info = &switchdev_work->fdb_info;
1925 err = mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, true);
1926 if (err)
1927 break;
1928 mlxsw_sp_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED,
1929 fdb_info->addr,
1930 fdb_info->vid, dev);
1931 break;
1932 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1933 fdb_info = &switchdev_work->fdb_info;
1934 mlxsw_sp_port_fdb_set(mlxsw_sp_port, fdb_info, false);
1935 break;
1936 }
1937
1938out:
1939 rtnl_unlock();
1940 kfree(switchdev_work->fdb_info.addr);
1941 kfree(switchdev_work);
1942 dev_put(dev);
1943}
1944
1945/* Called under rcu_read_lock() */
1946static int mlxsw_sp_switchdev_event(struct notifier_block *unused,
1947 unsigned long event, void *ptr)
1948{
1949 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1950 struct mlxsw_sp_switchdev_event_work *switchdev_work;
1951 struct switchdev_notifier_fdb_info *fdb_info = ptr;
1952
1953 if (!mlxsw_sp_port_dev_lower_find_rcu(dev))
1954 return NOTIFY_DONE;
1955
1956 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1957 if (!switchdev_work)
1958 return NOTIFY_BAD;
1959
1960 INIT_WORK(&switchdev_work->work, mlxsw_sp_switchdev_event_work);
1961 switchdev_work->dev = dev;
1962 switchdev_work->event = event;
1963
1964 switch (event) {
1965 case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
1966 case SWITCHDEV_FDB_DEL_TO_DEVICE:
1967 memcpy(&switchdev_work->fdb_info, ptr,
1968 sizeof(switchdev_work->fdb_info));
1969 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
Ido Schimmel6f497932017-07-12 09:12:55 +02001970 if (!switchdev_work->fdb_info.addr)
1971 goto err_addr_alloc;
Arkadi Sharshevskyaf0613782017-06-08 08:44:20 +02001972 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1973 fdb_info->addr);
1974 /* Take a reference on the device. This can be either
1975 * upper device containig mlxsw_sp_port or just a
1976 * mlxsw_sp_port
1977 */
1978 dev_hold(dev);
1979 break;
1980 default:
1981 kfree(switchdev_work);
1982 return NOTIFY_DONE;
1983 }
1984
1985 mlxsw_core_schedule_work(&switchdev_work->work);
1986
1987 return NOTIFY_DONE;
Ido Schimmel6f497932017-07-12 09:12:55 +02001988
1989err_addr_alloc:
1990 kfree(switchdev_work);
1991 return NOTIFY_BAD;
Arkadi Sharshevskyaf0613782017-06-08 08:44:20 +02001992}
1993
1994static struct notifier_block mlxsw_sp_switchdev_notifier = {
1995 .notifier_call = mlxsw_sp_switchdev_event,
1996};
1997
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001998static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
1999{
Ido Schimmel5f6935c2017-05-16 19:38:26 +02002000 struct mlxsw_sp_bridge *bridge = mlxsw_sp->bridge;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002001 int err;
2002
2003 err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
2004 if (err) {
2005 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
2006 return err;
2007 }
Arkadi Sharshevskyaf0613782017-06-08 08:44:20 +02002008
2009 err = register_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
2010 if (err) {
2011 dev_err(mlxsw_sp->bus_info->dev, "Failed to register switchdev notifier\n");
2012 return err;
2013 }
2014
Ido Schimmel5f6935c2017-05-16 19:38:26 +02002015 INIT_DELAYED_WORK(&bridge->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
2016 bridge->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002017 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
2018 return 0;
2019}
2020
2021static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
2022{
Ido Schimmel5f6935c2017-05-16 19:38:26 +02002023 cancel_delayed_work_sync(&mlxsw_sp->bridge->fdb_notify.dw);
Arkadi Sharshevskyaf0613782017-06-08 08:44:20 +02002024 unregister_switchdev_notifier(&mlxsw_sp_switchdev_notifier);
2025
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002026}
2027
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002028int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
2029{
Ido Schimmel5f6935c2017-05-16 19:38:26 +02002030 struct mlxsw_sp_bridge *bridge;
2031
2032 bridge = kzalloc(sizeof(*mlxsw_sp->bridge), GFP_KERNEL);
2033 if (!bridge)
2034 return -ENOMEM;
2035 mlxsw_sp->bridge = bridge;
2036 bridge->mlxsw_sp = mlxsw_sp;
2037
Ido Schimmelc57529e2017-05-26 08:37:31 +02002038 INIT_LIST_HEAD(&mlxsw_sp->bridge->bridges_list);
Ido Schimmel5f6935c2017-05-16 19:38:26 +02002039
Ido Schimmelc57529e2017-05-26 08:37:31 +02002040 bridge->bridge_8021q_ops = &mlxsw_sp_bridge_8021q_ops;
2041 bridge->bridge_8021d_ops = &mlxsw_sp_bridge_8021d_ops;
2042
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002043 return mlxsw_sp_fdb_init(mlxsw_sp);
2044}
2045
2046void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
2047{
2048 mlxsw_sp_fdb_fini(mlxsw_sp);
Ido Schimmelc57529e2017-05-26 08:37:31 +02002049 WARN_ON(!list_empty(&mlxsw_sp->bridge->bridges_list));
Ido Schimmel5f6935c2017-05-16 19:38:26 +02002050 kfree(mlxsw_sp->bridge);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002051}
2052
Jiri Pirko56ade8f2015-10-16 14:01:37 +02002053void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port *mlxsw_sp_port)
2054{
2055 mlxsw_sp_port->dev->switchdev_ops = &mlxsw_sp_port_switchdev_ops;
2056}
2057
2058void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port)
2059{
2060}