]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/ethernet/mellanox/mlxsw/spectrum_switchdev.c
mlxsw: spectrum: Don't abort on first error when removing VLANs
[mirror_ubuntu-artful-kernel.git] / drivers / net / ethernet / mellanox / mlxsw / spectrum_switchdev.c
CommitLineData
56ade8fe
JP
1/*
2 * drivers/net/ethernet/mellanox/mlxsw/spectrum_switchdev.c
3 * Copyright (c) 2015 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2015 Jiri Pirko <jiri@mellanox.com>
5 * Copyright (c) 2015 Ido Schimmel <idosch@mellanox.com>
6 * Copyright (c) 2015 Elad Raz <eladr@mellanox.com>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include <linux/kernel.h>
38#include <linux/types.h>
39#include <linux/netdevice.h>
40#include <linux/etherdevice.h>
41#include <linux/slab.h>
42#include <linux/device.h>
43#include <linux/skbuff.h>
44#include <linux/if_vlan.h>
45#include <linux/if_bridge.h>
46#include <linux/workqueue.h>
47#include <linux/jiffies.h>
4f2c6ae5 48#include <linux/rtnetlink.h>
56ade8fe
JP
49#include <net/switchdev.h>
50
51#include "spectrum.h"
52#include "core.h"
53#include "reg.h"
54
e4b6f693
ER
55static u16 mlxsw_sp_port_vid_to_fid_get(struct mlxsw_sp_port *mlxsw_sp_port,
56 u16 vid)
57{
56918b6b 58 struct mlxsw_sp_fid *f = mlxsw_sp_vport_fid_get(mlxsw_sp_port);
e4b6f693
ER
59 u16 fid = vid;
60
56918b6b 61 fid = f ? f->fid : fid;
e4b6f693
ER
62
63 if (!fid)
64 fid = mlxsw_sp_port->pvid;
65
66 return fid;
67}
68
54a73201
IS
69static struct mlxsw_sp_port *
70mlxsw_sp_port_orig_get(struct net_device *dev,
71 struct mlxsw_sp_port *mlxsw_sp_port)
72{
73 struct mlxsw_sp_port *mlxsw_sp_vport;
74 u16 vid;
75
76 if (!is_vlan_dev(dev))
77 return mlxsw_sp_port;
78
79 vid = vlan_dev_vlan_id(dev);
80 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
81 WARN_ON(!mlxsw_sp_vport);
82
83 return mlxsw_sp_vport;
84}
85
56ade8fe
JP
86static int mlxsw_sp_port_attr_get(struct net_device *dev,
87 struct switchdev_attr *attr)
88{
89 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
90 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
91
54a73201
IS
92 mlxsw_sp_port = mlxsw_sp_port_orig_get(attr->orig_dev, mlxsw_sp_port);
93 if (!mlxsw_sp_port)
94 return -EINVAL;
95
56ade8fe
JP
96 switch (attr->id) {
97 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
98 attr->u.ppid.id_len = sizeof(mlxsw_sp->base_mac);
99 memcpy(&attr->u.ppid.id, &mlxsw_sp->base_mac,
100 attr->u.ppid.id_len);
101 break;
102 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
103 attr->u.brport_flags =
104 (mlxsw_sp_port->learning ? BR_LEARNING : 0) |
0293038e
IS
105 (mlxsw_sp_port->learning_sync ? BR_LEARNING_SYNC : 0) |
106 (mlxsw_sp_port->uc_flood ? BR_FLOOD : 0);
56ade8fe
JP
107 break;
108 default:
109 return -EOPNOTSUPP;
110 }
111
112 return 0;
113}
114
115static int mlxsw_sp_port_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
116 u8 state)
117{
118 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
119 enum mlxsw_reg_spms_state spms_state;
120 char *spms_pl;
121 u16 vid;
122 int err;
123
124 switch (state) {
56ade8fe
JP
125 case BR_STATE_FORWARDING:
126 spms_state = MLXSW_REG_SPMS_STATE_FORWARDING;
127 break;
56ade8fe
JP
128 case BR_STATE_LEARNING:
129 spms_state = MLXSW_REG_SPMS_STATE_LEARNING;
130 break;
45491133 131 case BR_STATE_LISTENING: /* fall-through */
9cb026eb 132 case BR_STATE_DISABLED: /* fall-through */
56ade8fe
JP
133 case BR_STATE_BLOCKING:
134 spms_state = MLXSW_REG_SPMS_STATE_DISCARDING;
135 break;
136 default:
137 BUG();
138 }
139
140 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
141 if (!spms_pl)
142 return -ENOMEM;
143 mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
54a73201
IS
144
145 if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
146 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
56ade8fe 147 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
54a73201
IS
148 } else {
149 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID)
150 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
151 }
56ade8fe
JP
152
153 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
154 kfree(spms_pl);
155 return err;
156}
157
158static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
159 struct switchdev_trans *trans,
160 u8 state)
161{
162 if (switchdev_trans_ph_prepare(trans))
163 return 0;
164
165 mlxsw_sp_port->stp_state = state;
166 return mlxsw_sp_port_stp_state_set(mlxsw_sp_port, state);
167}
168
0293038e 169static int __mlxsw_sp_port_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
c06a94ef 170 u16 idx_begin, u16 idx_end, bool set,
0293038e
IS
171 bool only_uc)
172{
173 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
7f71eb46
IS
174 u16 local_port = mlxsw_sp_port->local_port;
175 enum mlxsw_flood_table_type table_type;
c06a94ef 176 u16 range = idx_end - idx_begin + 1;
0293038e
IS
177 char *sftr_pl;
178 int err;
179
99724c18 180 if (mlxsw_sp_port_is_vport(mlxsw_sp_port))
7f71eb46 181 table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID;
99724c18 182 else
7f71eb46 183 table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST;
7f71eb46 184
0293038e
IS
185 sftr_pl = kmalloc(MLXSW_REG_SFTR_LEN, GFP_KERNEL);
186 if (!sftr_pl)
187 return -ENOMEM;
188
c06a94ef 189 mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_UC, idx_begin,
7f71eb46 190 table_type, range, local_port, set);
0293038e
IS
191 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl);
192 if (err)
193 goto buffer_out;
194
195 /* Flooding control allows one to decide whether a given port will
196 * flood unicast traffic for which there is no FDB entry.
197 */
198 if (only_uc)
199 goto buffer_out;
200
c06a94ef 201 mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_BM, idx_begin,
7f71eb46 202 table_type, range, local_port, set);
0293038e 203 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl);
28892865
IS
204 if (err)
205 goto err_flood_bm_set;
206 else
207 goto buffer_out;
0293038e 208
28892865
IS
209err_flood_bm_set:
210 mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_UC, idx_begin,
211 table_type, range, local_port, !set);
212 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl);
0293038e
IS
213buffer_out:
214 kfree(sftr_pl);
215 return err;
216}
217
218static int mlxsw_sp_port_uc_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
219 bool set)
220{
221 struct net_device *dev = mlxsw_sp_port->dev;
222 u16 vid, last_visited_vid;
223 int err;
224
54a73201 225 if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
41b996cc
IS
226 u16 fid = mlxsw_sp_vport_fid_get(mlxsw_sp_port)->fid;
227 u16 vfid = mlxsw_sp_fid_to_vfid(fid);
54a73201
IS
228
229 return __mlxsw_sp_port_flood_set(mlxsw_sp_port, vfid, vfid,
230 set, true);
231 }
232
0293038e
IS
233 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
234 err = __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid, vid, set,
235 true);
236 if (err) {
237 last_visited_vid = vid;
238 goto err_port_flood_set;
239 }
240 }
241
242 return 0;
243
244err_port_flood_set:
245 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, last_visited_vid)
246 __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid, vid, !set, true);
247 netdev_err(dev, "Failed to configure unicast flooding\n");
248 return err;
249}
250
e6060027 251int mlxsw_sp_vport_flood_set(struct mlxsw_sp_port *mlxsw_sp_vport, u16 fid,
47a0a9e6 252 bool set)
7f71eb46 253{
e6060027
IS
254 u16 vfid;
255
7f71eb46
IS
256 /* In case of vFIDs, index into the flooding table is relative to
257 * the start of the vFIDs range.
258 */
e6060027 259 vfid = mlxsw_sp_fid_to_vfid(fid);
19ae6124 260 return __mlxsw_sp_port_flood_set(mlxsw_sp_vport, vfid, vfid, set,
47a0a9e6 261 false);
7f71eb46
IS
262}
263
56ade8fe
JP
264static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
265 struct switchdev_trans *trans,
266 unsigned long brport_flags)
267{
0293038e
IS
268 unsigned long uc_flood = mlxsw_sp_port->uc_flood ? BR_FLOOD : 0;
269 bool set;
270 int err;
271
6c72a3d0
IS
272 if (!mlxsw_sp_port->bridged)
273 return -EINVAL;
274
56ade8fe
JP
275 if (switchdev_trans_ph_prepare(trans))
276 return 0;
277
0293038e
IS
278 if ((uc_flood ^ brport_flags) & BR_FLOOD) {
279 set = mlxsw_sp_port->uc_flood ? false : true;
280 err = mlxsw_sp_port_uc_flood_set(mlxsw_sp_port, set);
281 if (err)
282 return err;
283 }
284
285 mlxsw_sp_port->uc_flood = brport_flags & BR_FLOOD ? 1 : 0;
56ade8fe
JP
286 mlxsw_sp_port->learning = brport_flags & BR_LEARNING ? 1 : 0;
287 mlxsw_sp_port->learning_sync = brport_flags & BR_LEARNING_SYNC ? 1 : 0;
0293038e 288
56ade8fe
JP
289 return 0;
290}
291
292static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
293{
294 char sfdat_pl[MLXSW_REG_SFDAT_LEN];
295 int err;
296
297 mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
298 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
299 if (err)
300 return err;
301 mlxsw_sp->ageing_time = ageing_time;
302 return 0;
303}
304
305static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
306 struct switchdev_trans *trans,
135f9ece 307 unsigned long ageing_clock_t)
56ade8fe
JP
308{
309 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
135f9ece 310 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
56ade8fe
JP
311 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
312
869f63a4
IS
313 if (switchdev_trans_ph_prepare(trans)) {
314 if (ageing_time < MLXSW_SP_MIN_AGEING_TIME ||
315 ageing_time > MLXSW_SP_MAX_AGEING_TIME)
316 return -ERANGE;
317 else
318 return 0;
319 }
56ade8fe
JP
320
321 return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
322}
323
26a4ea0f
ER
324static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
325 struct switchdev_trans *trans,
326 struct net_device *orig_dev,
327 bool vlan_enabled)
328{
329 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
330
331 /* SWITCHDEV_TRANS_PREPARE phase */
332 if ((!vlan_enabled) && (mlxsw_sp->master_bridge.dev == orig_dev)) {
333 netdev_err(mlxsw_sp_port->dev, "Bridge must be vlan-aware\n");
334 return -EINVAL;
335 }
336
337 return 0;
338}
339
56ade8fe
JP
340static int mlxsw_sp_port_attr_set(struct net_device *dev,
341 const struct switchdev_attr *attr,
342 struct switchdev_trans *trans)
343{
344 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
345 int err = 0;
346
54a73201
IS
347 mlxsw_sp_port = mlxsw_sp_port_orig_get(attr->orig_dev, mlxsw_sp_port);
348 if (!mlxsw_sp_port)
349 return -EINVAL;
350
56ade8fe
JP
351 switch (attr->id) {
352 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
353 err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port, trans,
354 attr->u.stp_state);
355 break;
356 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
357 err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port, trans,
358 attr->u.brport_flags);
359 break;
360 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
361 err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port, trans,
362 attr->u.ageing_time);
363 break;
26a4ea0f
ER
364 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
365 err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port, trans,
366 attr->orig_dev,
367 attr->u.vlan_filtering);
368 break;
56ade8fe
JP
369 default:
370 err = -EOPNOTSUPP;
371 break;
372 }
373
374 return err;
375}
376
14d39461
IS
377static int mlxsw_sp_fid_op(struct mlxsw_sp *mlxsw_sp, u16 fid, bool create)
378{
379 char sfmr_pl[MLXSW_REG_SFMR_LEN];
380
381 mlxsw_reg_sfmr_pack(sfmr_pl, !create, fid, fid);
382 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfmr), sfmr_pl);
383}
384
385static int mlxsw_sp_fid_map(struct mlxsw_sp *mlxsw_sp, u16 fid, bool valid)
386{
387 enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_VID_TO_FID;
388 char svfa_pl[MLXSW_REG_SVFA_LEN];
389
390 mlxsw_reg_svfa_pack(svfa_pl, 0, mt, valid, fid, fid);
391 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svfa), svfa_pl);
392}
393
394static struct mlxsw_sp_fid *mlxsw_sp_fid_alloc(u16 fid)
395{
396 struct mlxsw_sp_fid *f;
397
398 f = kzalloc(sizeof(*f), GFP_KERNEL);
399 if (!f)
400 return NULL;
401
402 f->fid = fid;
403
404 return f;
405}
406
701b186e 407struct mlxsw_sp_fid *mlxsw_sp_fid_create(struct mlxsw_sp *mlxsw_sp, u16 fid)
14d39461
IS
408{
409 struct mlxsw_sp_fid *f;
410 int err;
411
412 err = mlxsw_sp_fid_op(mlxsw_sp, fid, true);
413 if (err)
414 return ERR_PTR(err);
415
416 /* Although all the ports member in the FID might be using a
417 * {Port, VID} to FID mapping, we create a global VID-to-FID
418 * mapping. This allows a port to transition to VLAN mode,
419 * knowing the global mapping exists.
420 */
421 err = mlxsw_sp_fid_map(mlxsw_sp, fid, true);
422 if (err)
423 goto err_fid_map;
424
425 f = mlxsw_sp_fid_alloc(fid);
426 if (!f) {
427 err = -ENOMEM;
428 goto err_allocate_fid;
429 }
430
431 list_add(&f->list, &mlxsw_sp->fids);
432
433 return f;
434
435err_allocate_fid:
436 mlxsw_sp_fid_map(mlxsw_sp, fid, false);
437err_fid_map:
438 mlxsw_sp_fid_op(mlxsw_sp, fid, false);
439 return ERR_PTR(err);
440}
441
701b186e 442void mlxsw_sp_fid_destroy(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_fid *f)
14d39461
IS
443{
444 u16 fid = f->fid;
445
446 list_del(&f->list);
447
99f44bb3
IS
448 if (f->r)
449 mlxsw_sp_rif_bridge_destroy(mlxsw_sp, f->r);
450
14d39461
IS
451 kfree(f);
452
8168287b
IS
453 mlxsw_sp_fid_map(mlxsw_sp, fid, false);
454
14d39461
IS
455 mlxsw_sp_fid_op(mlxsw_sp, fid, false);
456}
457
458static int __mlxsw_sp_port_fid_join(struct mlxsw_sp_port *mlxsw_sp_port,
459 u16 fid)
460{
461 struct mlxsw_sp_fid *f;
462
463 f = mlxsw_sp_fid_find(mlxsw_sp_port->mlxsw_sp, fid);
464 if (!f) {
465 f = mlxsw_sp_fid_create(mlxsw_sp_port->mlxsw_sp, fid);
466 if (IS_ERR(f))
467 return PTR_ERR(f);
468 }
469
470 f->ref_count++;
471
22305378
IS
472 netdev_dbg(mlxsw_sp_port->dev, "Joined FID=%d\n", fid);
473
14d39461
IS
474 return 0;
475}
476
477static void __mlxsw_sp_port_fid_leave(struct mlxsw_sp_port *mlxsw_sp_port,
478 u16 fid)
479{
480 struct mlxsw_sp_fid *f;
481
482 f = mlxsw_sp_fid_find(mlxsw_sp_port->mlxsw_sp, fid);
483 if (WARN_ON(!f))
484 return;
485
22305378
IS
486 netdev_dbg(mlxsw_sp_port->dev, "Left FID=%d\n", fid);
487
fe3f6d14
IS
488 mlxsw_sp_port_fdb_flush(mlxsw_sp_port, fid);
489
14d39461
IS
490 if (--f->ref_count == 0)
491 mlxsw_sp_fid_destroy(mlxsw_sp_port->mlxsw_sp, f);
492}
493
494static int mlxsw_sp_port_fid_map(struct mlxsw_sp_port *mlxsw_sp_port, u16 fid,
495 bool valid)
496{
497 enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID;
498
499 /* If port doesn't have vPorts, then it can use the global
500 * VID-to-FID mapping.
501 */
502 if (list_empty(&mlxsw_sp_port->vports_list))
503 return 0;
504
505 return mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, valid, fid, fid);
506}
507
508static int mlxsw_sp_port_fid_join(struct mlxsw_sp_port *mlxsw_sp_port,
509 u16 fid_begin, u16 fid_end)
510{
511 int fid, err;
512
513 for (fid = fid_begin; fid <= fid_end; fid++) {
514 err = __mlxsw_sp_port_fid_join(mlxsw_sp_port, fid);
515 if (err)
516 goto err_port_fid_join;
517 }
518
519 err = __mlxsw_sp_port_flood_set(mlxsw_sp_port, fid_begin, fid_end,
520 true, false);
521 if (err)
522 goto err_port_flood_set;
523
524 for (fid = fid_begin; fid <= fid_end; fid++) {
525 err = mlxsw_sp_port_fid_map(mlxsw_sp_port, fid, true);
526 if (err)
527 goto err_port_fid_map;
528 }
529
530 return 0;
531
532err_port_fid_map:
533 for (fid--; fid >= fid_begin; fid--)
534 mlxsw_sp_port_fid_map(mlxsw_sp_port, fid, false);
535 __mlxsw_sp_port_flood_set(mlxsw_sp_port, fid_begin, fid_end, false,
536 false);
537err_port_flood_set:
538 fid = fid_end;
539err_port_fid_join:
540 for (fid--; fid >= fid_begin; fid--)
541 __mlxsw_sp_port_fid_leave(mlxsw_sp_port, fid);
542 return err;
543}
544
545static void mlxsw_sp_port_fid_leave(struct mlxsw_sp_port *mlxsw_sp_port,
546 u16 fid_begin, u16 fid_end)
547{
548 int fid;
549
550 for (fid = fid_begin; fid <= fid_end; fid++)
551 mlxsw_sp_port_fid_map(mlxsw_sp_port, fid, false);
552
553 __mlxsw_sp_port_flood_set(mlxsw_sp_port, fid_begin, fid_end, false,
554 false);
555
556 for (fid = fid_begin; fid <= fid_end; fid++)
557 __mlxsw_sp_port_fid_leave(mlxsw_sp_port, fid);
558}
559
28a01d2d
IS
560static int __mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port,
561 u16 vid)
56ade8fe
JP
562{
563 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
564 char spvid_pl[MLXSW_REG_SPVID_LEN];
565
566 mlxsw_reg_spvid_pack(spvid_pl, mlxsw_sp_port->local_port, vid);
567 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvid), spvid_pl);
568}
569
28a01d2d
IS
570static int mlxsw_sp_port_allow_untagged_set(struct mlxsw_sp_port *mlxsw_sp_port,
571 bool allow)
572{
573 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
574 char spaft_pl[MLXSW_REG_SPAFT_LEN];
575
576 mlxsw_reg_spaft_pack(spaft_pl, mlxsw_sp_port->local_port, allow);
577 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spaft), spaft_pl);
578}
579
580int mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
581{
582 struct net_device *dev = mlxsw_sp_port->dev;
583 int err;
584
585 if (!vid) {
586 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port, false);
587 if (err) {
588 netdev_err(dev, "Failed to disallow untagged traffic\n");
589 return err;
590 }
591 } else {
592 err = __mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid);
593 if (err) {
594 netdev_err(dev, "Failed to set PVID\n");
595 return err;
596 }
597
598 /* Only allow if not already allowed. */
599 if (!mlxsw_sp_port->pvid) {
600 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port,
601 true);
602 if (err) {
603 netdev_err(dev, "Failed to allow untagged traffic\n");
604 goto err_port_allow_untagged_set;
605 }
606 }
607 }
608
609 mlxsw_sp_port->pvid = vid;
610 return 0;
611
612err_port_allow_untagged_set:
613 __mlxsw_sp_port_pvid_set(mlxsw_sp_port, mlxsw_sp_port->pvid);
614 return err;
615}
616
3b7ad5ec
IS
617static int __mlxsw_sp_port_vlans_set(struct mlxsw_sp_port *mlxsw_sp_port,
618 u16 vid_begin, u16 vid_end, bool is_member,
619 bool untagged)
620{
621 u16 vid, vid_e;
622 int err;
623
624 for (vid = vid_begin; vid <= vid_end;
625 vid += MLXSW_REG_SPVM_REC_MAX_COUNT) {
626 vid_e = min((u16) (vid + MLXSW_REG_SPVM_REC_MAX_COUNT - 1),
627 vid_end);
628
629 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid_e,
630 is_member, untagged);
631 if (err)
632 return err;
633 }
634
635 return 0;
636}
637
56ade8fe
JP
638static int __mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
639 u16 vid_begin, u16 vid_end,
640 bool flag_untagged, bool flag_pvid)
641{
56ade8fe 642 struct net_device *dev = mlxsw_sp_port->dev;
14d39461 643 u16 vid, old_pvid;
56ade8fe
JP
644 int err;
645
56ade8fe 646 if (!mlxsw_sp_port->bridged)
32d863fb 647 return -EINVAL;
56ade8fe 648
14d39461 649 err = mlxsw_sp_port_fid_join(mlxsw_sp_port, vid_begin, vid_end);
1b3433a9 650 if (err) {
14d39461
IS
651 netdev_err(dev, "Failed to join FIDs\n");
652 return err;
56ade8fe
JP
653 }
654
3b7ad5ec
IS
655 err = __mlxsw_sp_port_vlans_set(mlxsw_sp_port, vid_begin, vid_end,
656 true, flag_untagged);
657 if (err) {
658 netdev_err(dev, "Unable to add VIDs %d-%d\n", vid_begin,
659 vid_end);
b07a966c 660 goto err_port_vlans_set;
56ade8fe
JP
661 }
662
b07a966c
IS
663 old_pvid = mlxsw_sp_port->pvid;
664 if (flag_pvid && old_pvid != vid_begin) {
665 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid_begin);
56ade8fe 666 if (err) {
b07a966c
IS
667 netdev_err(dev, "Unable to add PVID %d\n", vid_begin);
668 goto err_port_pvid_set;
56ade8fe 669 }
28a01d2d
IS
670 } else if (!flag_pvid && old_pvid >= vid_begin && old_pvid <= vid_end) {
671 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, 0);
672 if (err) {
673 netdev_err(dev, "Unable to del PVID\n");
674 goto err_port_pvid_set;
675 }
56ade8fe
JP
676 }
677
678 /* Changing activity bits only if HW operation succeded */
fc1273af 679 for (vid = vid_begin; vid <= vid_end; vid++) {
56ade8fe 680 set_bit(vid, mlxsw_sp_port->active_vlans);
fc1273af
ER
681 if (flag_untagged)
682 set_bit(vid, mlxsw_sp_port->untagged_vlans);
683 else
684 clear_bit(vid, mlxsw_sp_port->untagged_vlans);
685 }
56ade8fe 686
b07a966c
IS
687 /* STP state change must be done after we set active VLANs */
688 err = mlxsw_sp_port_stp_state_set(mlxsw_sp_port,
689 mlxsw_sp_port->stp_state);
690 if (err) {
691 netdev_err(dev, "Failed to set STP state\n");
692 goto err_port_stp_state_set;
693 }
694
695 return 0;
696
b07a966c
IS
697err_port_stp_state_set:
698 for (vid = vid_begin; vid <= vid_end; vid++)
699 clear_bit(vid, mlxsw_sp_port->active_vlans);
700 if (old_pvid != mlxsw_sp_port->pvid)
701 mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid);
702err_port_pvid_set:
703 __mlxsw_sp_port_vlans_set(mlxsw_sp_port, vid_begin, vid_end, false,
704 false);
705err_port_vlans_set:
14d39461 706 mlxsw_sp_port_fid_leave(mlxsw_sp_port, vid_begin, vid_end);
b07a966c 707 return err;
56ade8fe
JP
708}
709
710static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
711 const struct switchdev_obj_port_vlan *vlan,
712 struct switchdev_trans *trans)
713{
e4a13055
ER
714 bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
715 bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
56ade8fe
JP
716
717 if (switchdev_trans_ph_prepare(trans))
718 return 0;
719
720 return __mlxsw_sp_port_vlans_add(mlxsw_sp_port,
721 vlan->vid_begin, vlan->vid_end,
e4a13055 722 flag_untagged, flag_pvid);
56ade8fe
JP
723}
724
8a1ab5d7 725static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
56ade8fe 726{
8a1ab5d7
JP
727 return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
728 MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY;
729}
730
731static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
732{
733 return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
734 MLXSW_REG_SFD_OP_WRITE_REMOVE;
735}
736
6e095fd4
IS
737static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
738 const char *mac, u16 fid, bool adding,
739 enum mlxsw_reg_sfd_rec_action action,
740 bool dynamic)
8a1ab5d7 741{
56ade8fe
JP
742 char *sfd_pl;
743 int err;
744
56ade8fe
JP
745 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
746 if (!sfd_pl)
747 return -ENOMEM;
748
8a1ab5d7
JP
749 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
750 mlxsw_reg_sfd_uc_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
6e095fd4 751 mac, fid, action, local_port);
8a1ab5d7
JP
752 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
753 kfree(sfd_pl);
754
755 return err;
756}
757
6e095fd4
IS
758static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
759 const char *mac, u16 fid, bool adding,
760 bool dynamic)
761{
762 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding,
763 MLXSW_REG_SFD_REC_ACTION_NOP, dynamic);
764}
765
766int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid,
767 bool adding)
768{
769 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding,
770 MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER,
771 false);
772}
773
8a1ab5d7 774static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
64771e31
IS
775 const char *mac, u16 fid, u16 lag_vid,
776 bool adding, bool dynamic)
8a1ab5d7
JP
777{
778 char *sfd_pl;
779 int err;
780
781 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
782 if (!sfd_pl)
783 return -ENOMEM;
784
785 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
786 mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
64771e31
IS
787 mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
788 lag_vid, lag_id);
8a1ab5d7 789 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
56ade8fe
JP
790 kfree(sfd_pl);
791
792 return err;
793}
794
795static int
796mlxsw_sp_port_fdb_static_add(struct mlxsw_sp_port *mlxsw_sp_port,
797 const struct switchdev_obj_port_fdb *fdb,
798 struct switchdev_trans *trans)
799{
e4b6f693 800 u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, fdb->vid);
64771e31 801 u16 lag_vid = 0;
8a1ab5d7 802
56ade8fe
JP
803 if (switchdev_trans_ph_prepare(trans))
804 return 0;
805
54a73201 806 if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
64771e31 807 lag_vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
54a73201
IS
808 }
809
8a1ab5d7 810 if (!mlxsw_sp_port->lagged)
2fa9d45e
JP
811 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp_port->mlxsw_sp,
812 mlxsw_sp_port->local_port,
9de6a80e 813 fdb->addr, fid, true, false);
8a1ab5d7
JP
814 else
815 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp_port->mlxsw_sp,
816 mlxsw_sp_port->lag_id,
64771e31
IS
817 fdb->addr, fid, lag_vid,
818 true, false);
56ade8fe
JP
819}
820
3a49b4fd
ER
821static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
822 u16 fid, u16 mid, bool adding)
823{
824 char *sfd_pl;
825 int err;
826
827 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
828 if (!sfd_pl)
829 return -ENOMEM;
830
831 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
832 mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
833 MLXSW_REG_SFD_REC_ACTION_NOP, mid);
834 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
835 kfree(sfd_pl);
836 return err;
837}
838
839static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 mid,
840 bool add, bool clear_all_ports)
841{
842 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
843 char *smid_pl;
844 int err, i;
845
846 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
847 if (!smid_pl)
848 return -ENOMEM;
849
850 mlxsw_reg_smid_pack(smid_pl, mid, mlxsw_sp_port->local_port, add);
851 if (clear_all_ports) {
852 for (i = 1; i < MLXSW_PORT_MAX_PORTS; i++)
853 if (mlxsw_sp->ports[i])
854 mlxsw_reg_smid_port_mask_set(smid_pl, i, 1);
855 }
856 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
857 kfree(smid_pl);
858 return err;
859}
860
861static struct mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp *mlxsw_sp,
862 const unsigned char *addr,
863 u16 vid)
864{
865 struct mlxsw_sp_mid *mid;
866
867 list_for_each_entry(mid, &mlxsw_sp->br_mids.list, list) {
868 if (ether_addr_equal(mid->addr, addr) && mid->vid == vid)
869 return mid;
870 }
871 return NULL;
872}
873
874static struct mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
875 const unsigned char *addr,
876 u16 vid)
877{
878 struct mlxsw_sp_mid *mid;
879 u16 mid_idx;
880
881 mid_idx = find_first_zero_bit(mlxsw_sp->br_mids.mapped,
882 MLXSW_SP_MID_MAX);
883 if (mid_idx == MLXSW_SP_MID_MAX)
884 return NULL;
885
886 mid = kzalloc(sizeof(*mid), GFP_KERNEL);
887 if (!mid)
888 return NULL;
889
890 set_bit(mid_idx, mlxsw_sp->br_mids.mapped);
891 ether_addr_copy(mid->addr, addr);
892 mid->vid = vid;
893 mid->mid = mid_idx;
894 mid->ref_count = 0;
895 list_add_tail(&mid->list, &mlxsw_sp->br_mids.list);
896
897 return mid;
898}
899
900static int __mlxsw_sp_mc_dec_ref(struct mlxsw_sp *mlxsw_sp,
901 struct mlxsw_sp_mid *mid)
902{
903 if (--mid->ref_count == 0) {
904 list_del(&mid->list);
905 clear_bit(mid->mid, mlxsw_sp->br_mids.mapped);
906 kfree(mid);
907 return 1;
908 }
909 return 0;
910}
911
912static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
913 const struct switchdev_obj_port_mdb *mdb,
914 struct switchdev_trans *trans)
915{
916 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
917 struct net_device *dev = mlxsw_sp_port->dev;
918 struct mlxsw_sp_mid *mid;
919 u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, mdb->vid);
920 int err = 0;
921
922 if (switchdev_trans_ph_prepare(trans))
923 return 0;
924
925 mid = __mlxsw_sp_mc_get(mlxsw_sp, mdb->addr, mdb->vid);
926 if (!mid) {
927 mid = __mlxsw_sp_mc_alloc(mlxsw_sp, mdb->addr, mdb->vid);
928 if (!mid) {
929 netdev_err(dev, "Unable to allocate MC group\n");
930 return -ENOMEM;
931 }
932 }
933 mid->ref_count++;
934
935 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true,
936 mid->ref_count == 1);
937 if (err) {
938 netdev_err(dev, "Unable to set SMID\n");
939 goto err_out;
940 }
941
942 if (mid->ref_count == 1) {
943 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mdb->addr, fid, mid->mid,
944 true);
945 if (err) {
946 netdev_err(dev, "Unable to set MC SFD\n");
947 goto err_out;
948 }
949 }
950
951 return 0;
952
953err_out:
954 __mlxsw_sp_mc_dec_ref(mlxsw_sp, mid);
955 return err;
956}
957
56ade8fe
JP
958static int mlxsw_sp_port_obj_add(struct net_device *dev,
959 const struct switchdev_obj *obj,
960 struct switchdev_trans *trans)
961{
962 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
963 int err = 0;
964
54a73201
IS
965 mlxsw_sp_port = mlxsw_sp_port_orig_get(obj->orig_dev, mlxsw_sp_port);
966 if (!mlxsw_sp_port)
967 return -EINVAL;
968
56ade8fe
JP
969 switch (obj->id) {
970 case SWITCHDEV_OBJ_ID_PORT_VLAN:
54a73201
IS
971 if (mlxsw_sp_port_is_vport(mlxsw_sp_port))
972 return 0;
973
56ade8fe
JP
974 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port,
975 SWITCHDEV_OBJ_PORT_VLAN(obj),
976 trans);
977 break;
61c503f9
JP
978 case SWITCHDEV_OBJ_ID_IPV4_FIB:
979 err = mlxsw_sp_router_fib4_add(mlxsw_sp_port,
980 SWITCHDEV_OBJ_IPV4_FIB(obj),
981 trans);
982 break;
56ade8fe
JP
983 case SWITCHDEV_OBJ_ID_PORT_FDB:
984 err = mlxsw_sp_port_fdb_static_add(mlxsw_sp_port,
985 SWITCHDEV_OBJ_PORT_FDB(obj),
986 trans);
987 break;
3a49b4fd
ER
988 case SWITCHDEV_OBJ_ID_PORT_MDB:
989 err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
990 SWITCHDEV_OBJ_PORT_MDB(obj),
991 trans);
992 break;
56ade8fe
JP
993 default:
994 err = -EOPNOTSUPP;
995 break;
996 }
997
998 return err;
999}
1000
56ade8fe 1001static int __mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
05978481 1002 u16 vid_begin, u16 vid_end)
56ade8fe 1003{
3b7ad5ec 1004 u16 vid, pvid;
56ade8fe 1005
05978481 1006 if (!mlxsw_sp_port->bridged)
32d863fb 1007 return -EINVAL;
56ade8fe 1008
06c071f6 1009 pvid = mlxsw_sp_port->pvid;
640be7b7
IS
1010 if (pvid >= vid_begin && pvid <= vid_end)
1011 mlxsw_sp_port_pvid_set(mlxsw_sp_port, 0);
56ade8fe 1012
640be7b7
IS
1013 __mlxsw_sp_port_vlans_set(mlxsw_sp_port, vid_begin, vid_end, false,
1014 false);
f7a8f6ce 1015
14d39461 1016 mlxsw_sp_port_fid_leave(mlxsw_sp_port, vid_begin, vid_end);
56ade8fe 1017
56ade8fe
JP
1018 /* Changing activity bits only if HW operation succeded */
1019 for (vid = vid_begin; vid <= vid_end; vid++)
1020 clear_bit(vid, mlxsw_sp_port->active_vlans);
1021
1022 return 0;
1023}
1024
1025static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1026 const struct switchdev_obj_port_vlan *vlan)
1027{
05978481
IS
1028 return __mlxsw_sp_port_vlans_del(mlxsw_sp_port, vlan->vid_begin,
1029 vlan->vid_end);
56ade8fe
JP
1030}
1031
4dc236c3
IS
1032void mlxsw_sp_port_active_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port)
1033{
1034 u16 vid;
1035
1036 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID)
05978481 1037 __mlxsw_sp_port_vlans_del(mlxsw_sp_port, vid, vid);
4dc236c3
IS
1038}
1039
56ade8fe
JP
1040static int
1041mlxsw_sp_port_fdb_static_del(struct mlxsw_sp_port *mlxsw_sp_port,
1042 const struct switchdev_obj_port_fdb *fdb)
1043{
e4b6f693 1044 u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, fdb->vid);
64771e31 1045 u16 lag_vid = 0;
9de6a80e 1046
54a73201 1047 if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
64771e31 1048 lag_vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
54a73201
IS
1049 }
1050
8a1ab5d7 1051 if (!mlxsw_sp_port->lagged)
2fa9d45e
JP
1052 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp_port->mlxsw_sp,
1053 mlxsw_sp_port->local_port,
9de6a80e 1054 fdb->addr, fid,
8a1ab5d7
JP
1055 false, false);
1056 else
1057 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp_port->mlxsw_sp,
1058 mlxsw_sp_port->lag_id,
64771e31 1059 fdb->addr, fid, lag_vid,
8a1ab5d7 1060 false, false);
56ade8fe
JP
1061}
1062
3a49b4fd
ER
1063static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1064 const struct switchdev_obj_port_mdb *mdb)
1065{
1066 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1067 struct net_device *dev = mlxsw_sp_port->dev;
1068 struct mlxsw_sp_mid *mid;
1069 u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, mdb->vid);
1070 u16 mid_idx;
1071 int err = 0;
1072
1073 mid = __mlxsw_sp_mc_get(mlxsw_sp, mdb->addr, mdb->vid);
1074 if (!mid) {
1075 netdev_err(dev, "Unable to remove port from MC DB\n");
1076 return -EINVAL;
1077 }
1078
1079 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false, false);
1080 if (err)
1081 netdev_err(dev, "Unable to remove port from SMID\n");
1082
1083 mid_idx = mid->mid;
1084 if (__mlxsw_sp_mc_dec_ref(mlxsw_sp, mid)) {
1085 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mdb->addr, fid, mid_idx,
1086 false);
1087 if (err)
1088 netdev_err(dev, "Unable to remove MC SFD\n");
1089 }
1090
1091 return err;
1092}
1093
56ade8fe
JP
1094static int mlxsw_sp_port_obj_del(struct net_device *dev,
1095 const struct switchdev_obj *obj)
1096{
1097 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1098 int err = 0;
1099
54a73201
IS
1100 mlxsw_sp_port = mlxsw_sp_port_orig_get(obj->orig_dev, mlxsw_sp_port);
1101 if (!mlxsw_sp_port)
1102 return -EINVAL;
1103
56ade8fe
JP
1104 switch (obj->id) {
1105 case SWITCHDEV_OBJ_ID_PORT_VLAN:
54a73201
IS
1106 if (mlxsw_sp_port_is_vport(mlxsw_sp_port))
1107 return 0;
1108
56ade8fe
JP
1109 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1110 SWITCHDEV_OBJ_PORT_VLAN(obj));
1111 break;
61c503f9
JP
1112 case SWITCHDEV_OBJ_ID_IPV4_FIB:
1113 err = mlxsw_sp_router_fib4_del(mlxsw_sp_port,
1114 SWITCHDEV_OBJ_IPV4_FIB(obj));
1115 break;
56ade8fe
JP
1116 case SWITCHDEV_OBJ_ID_PORT_FDB:
1117 err = mlxsw_sp_port_fdb_static_del(mlxsw_sp_port,
1118 SWITCHDEV_OBJ_PORT_FDB(obj));
1119 break;
3a49b4fd
ER
1120 case SWITCHDEV_OBJ_ID_PORT_MDB:
1121 err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1122 SWITCHDEV_OBJ_PORT_MDB(obj));
00ae40e7 1123 break;
56ade8fe
JP
1124 default:
1125 err = -EOPNOTSUPP;
1126 break;
1127 }
1128
1129 return err;
1130}
1131
8a1ab5d7
JP
1132static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
1133 u16 lag_id)
1134{
1135 struct mlxsw_sp_port *mlxsw_sp_port;
1136 int i;
1137
1138 for (i = 0; i < MLXSW_SP_PORT_PER_LAG_MAX; i++) {
1139 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
1140 if (mlxsw_sp_port)
1141 return mlxsw_sp_port;
1142 }
1143 return NULL;
1144}
1145
56ade8fe
JP
1146static int mlxsw_sp_port_fdb_dump(struct mlxsw_sp_port *mlxsw_sp_port,
1147 struct switchdev_obj_port_fdb *fdb,
304f5158
IS
1148 switchdev_obj_dump_cb_t *cb,
1149 struct net_device *orig_dev)
56ade8fe 1150{
8a1ab5d7 1151 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3f47f867 1152 struct mlxsw_sp_port *tmp;
56918b6b
IS
1153 struct mlxsw_sp_fid *f;
1154 u16 vport_fid;
56ade8fe
JP
1155 char *sfd_pl;
1156 char mac[ETH_ALEN];
9de6a80e 1157 u16 fid;
56ade8fe 1158 u8 local_port;
8a1ab5d7 1159 u16 lag_id;
56ade8fe
JP
1160 u8 num_rec;
1161 int stored_err = 0;
1162 int i;
1163 int err;
1164
1165 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1166 if (!sfd_pl)
1167 return -ENOMEM;
1168
56918b6b
IS
1169 f = mlxsw_sp_vport_fid_get(mlxsw_sp_port);
1170 vport_fid = f ? f->fid : 0;
54a73201 1171
56ade8fe
JP
1172 mlxsw_reg_sfd_pack(sfd_pl, MLXSW_REG_SFD_OP_QUERY_DUMP, 0);
1173 do {
1174 mlxsw_reg_sfd_num_rec_set(sfd_pl, MLXSW_REG_SFD_REC_MAX_COUNT);
8a1ab5d7 1175 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
56ade8fe
JP
1176 if (err)
1177 goto out;
1178
1179 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1180
1181 /* Even in case of error, we have to run the dump to the end
1182 * so the session in firmware is finished.
1183 */
1184 if (stored_err)
1185 continue;
1186
1187 for (i = 0; i < num_rec; i++) {
1188 switch (mlxsw_reg_sfd_rec_type_get(sfd_pl, i)) {
1189 case MLXSW_REG_SFD_REC_TYPE_UNICAST:
9de6a80e 1190 mlxsw_reg_sfd_uc_unpack(sfd_pl, i, mac, &fid,
56ade8fe
JP
1191 &local_port);
1192 if (local_port == mlxsw_sp_port->local_port) {
004f85ea
IS
1193 if (vport_fid && vport_fid == fid)
1194 fdb->vid = 0;
1195 else if (!vport_fid &&
1196 !mlxsw_sp_fid_is_vfid(fid))
54a73201 1197 fdb->vid = fid;
004f85ea
IS
1198 else
1199 continue;
56ade8fe
JP
1200 ether_addr_copy(fdb->addr, mac);
1201 fdb->ndm_state = NUD_REACHABLE;
56ade8fe
JP
1202 err = cb(&fdb->obj);
1203 if (err)
1204 stored_err = err;
1205 }
8a1ab5d7
JP
1206 break;
1207 case MLXSW_REG_SFD_REC_TYPE_UNICAST_LAG:
1208 mlxsw_reg_sfd_uc_lag_unpack(sfd_pl, i,
9de6a80e 1209 mac, &fid, &lag_id);
3f47f867
IS
1210 tmp = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
1211 if (tmp && tmp->local_port ==
1212 mlxsw_sp_port->local_port) {
304f5158
IS
1213 /* LAG records can only point to LAG
1214 * devices or VLAN devices on top.
1215 */
1216 if (!netif_is_lag_master(orig_dev) &&
1217 !is_vlan_dev(orig_dev))
1218 continue;
004f85ea
IS
1219 if (vport_fid && vport_fid == fid)
1220 fdb->vid = 0;
1221 else if (!vport_fid &&
1222 !mlxsw_sp_fid_is_vfid(fid))
54a73201 1223 fdb->vid = fid;
004f85ea
IS
1224 else
1225 continue;
8a1ab5d7
JP
1226 ether_addr_copy(fdb->addr, mac);
1227 fdb->ndm_state = NUD_REACHABLE;
8a1ab5d7
JP
1228 err = cb(&fdb->obj);
1229 if (err)
1230 stored_err = err;
1231 }
1232 break;
56ade8fe
JP
1233 }
1234 }
1235 } while (num_rec == MLXSW_REG_SFD_REC_MAX_COUNT);
1236
1237out:
1238 kfree(sfd_pl);
1239 return stored_err ? stored_err : err;
1240}
1241
1242static int mlxsw_sp_port_vlan_dump(struct mlxsw_sp_port *mlxsw_sp_port,
1243 struct switchdev_obj_port_vlan *vlan,
1244 switchdev_obj_dump_cb_t *cb)
1245{
1246 u16 vid;
1247 int err = 0;
1248
54a73201
IS
1249 if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
1250 vlan->flags = 0;
1251 vlan->vid_begin = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
1252 vlan->vid_end = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
1253 return cb(&vlan->obj);
1254 }
1255
56ade8fe
JP
1256 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
1257 vlan->flags = 0;
1258 if (vid == mlxsw_sp_port->pvid)
1259 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
fc1273af
ER
1260 if (test_bit(vid, mlxsw_sp_port->untagged_vlans))
1261 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
56ade8fe
JP
1262 vlan->vid_begin = vid;
1263 vlan->vid_end = vid;
1264 err = cb(&vlan->obj);
1265 if (err)
1266 break;
1267 }
1268 return err;
1269}
1270
1271static int mlxsw_sp_port_obj_dump(struct net_device *dev,
1272 struct switchdev_obj *obj,
1273 switchdev_obj_dump_cb_t *cb)
1274{
1275 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1276 int err = 0;
1277
54a73201
IS
1278 mlxsw_sp_port = mlxsw_sp_port_orig_get(obj->orig_dev, mlxsw_sp_port);
1279 if (!mlxsw_sp_port)
1280 return -EINVAL;
1281
56ade8fe
JP
1282 switch (obj->id) {
1283 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1284 err = mlxsw_sp_port_vlan_dump(mlxsw_sp_port,
1285 SWITCHDEV_OBJ_PORT_VLAN(obj), cb);
1286 break;
1287 case SWITCHDEV_OBJ_ID_PORT_FDB:
1288 err = mlxsw_sp_port_fdb_dump(mlxsw_sp_port,
304f5158
IS
1289 SWITCHDEV_OBJ_PORT_FDB(obj), cb,
1290 obj->orig_dev);
56ade8fe
JP
1291 break;
1292 default:
1293 err = -EOPNOTSUPP;
1294 break;
1295 }
1296
1297 return err;
1298}
1299
c7070fc4 1300static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = {
56ade8fe
JP
1301 .switchdev_port_attr_get = mlxsw_sp_port_attr_get,
1302 .switchdev_port_attr_set = mlxsw_sp_port_attr_set,
1303 .switchdev_port_obj_add = mlxsw_sp_port_obj_add,
1304 .switchdev_port_obj_del = mlxsw_sp_port_obj_del,
1305 .switchdev_port_obj_dump = mlxsw_sp_port_obj_dump,
1306};
1307
45827d78
IS
1308static void mlxsw_sp_fdb_call_notifiers(bool learning_sync, bool adding,
1309 char *mac, u16 vid,
8a1ab5d7
JP
1310 struct net_device *dev)
1311{
1312 struct switchdev_notifier_fdb_info info;
1313 unsigned long notifier_type;
1314
45827d78 1315 if (learning_sync) {
8a1ab5d7
JP
1316 info.addr = mac;
1317 info.vid = vid;
1318 notifier_type = adding ? SWITCHDEV_FDB_ADD : SWITCHDEV_FDB_DEL;
1319 call_switchdev_notifiers(notifier_type, dev, &info.info);
1320 }
1321}
1322
56ade8fe
JP
1323static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
1324 char *sfn_pl, int rec_index,
1325 bool adding)
1326{
1327 struct mlxsw_sp_port *mlxsw_sp_port;
1328 char mac[ETH_ALEN];
1329 u8 local_port;
9de6a80e 1330 u16 vid, fid;
12f1501e 1331 bool do_notification = true;
56ade8fe
JP
1332 int err;
1333
9de6a80e 1334 mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
56ade8fe
JP
1335 mlxsw_sp_port = mlxsw_sp->ports[local_port];
1336 if (!mlxsw_sp_port) {
1337 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
12f1501e 1338 goto just_remove;
56ade8fe
JP
1339 }
1340
aac78a44 1341 if (mlxsw_sp_fid_is_vfid(fid)) {
aac78a44
IS
1342 struct mlxsw_sp_port *mlxsw_sp_vport;
1343
d0ec875a
IS
1344 mlxsw_sp_vport = mlxsw_sp_port_vport_find_by_fid(mlxsw_sp_port,
1345 fid);
aac78a44
IS
1346 if (!mlxsw_sp_vport) {
1347 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching vPort following FDB notification\n");
12f1501e 1348 goto just_remove;
aac78a44 1349 }
004f85ea 1350 vid = 0;
aac78a44
IS
1351 /* Override the physical port with the vPort. */
1352 mlxsw_sp_port = mlxsw_sp_vport;
1353 } else {
1354 vid = fid;
1355 }
1356
12f1501e
JP
1357 adding = adding && mlxsw_sp_port->learning;
1358
1359do_fdb_op:
2fa9d45e 1360 err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
12f1501e 1361 adding, true);
56ade8fe
JP
1362 if (err) {
1363 if (net_ratelimit())
1364 netdev_err(mlxsw_sp_port->dev, "Failed to set FDB entry\n");
1365 return;
1366 }
1367
12f1501e
JP
1368 if (!do_notification)
1369 return;
45827d78 1370 mlxsw_sp_fdb_call_notifiers(mlxsw_sp_port->learning_sync,
8a1ab5d7 1371 adding, mac, vid, mlxsw_sp_port->dev);
12f1501e
JP
1372 return;
1373
1374just_remove:
1375 adding = false;
1376 do_notification = false;
1377 goto do_fdb_op;
8a1ab5d7 1378}
56ade8fe 1379
8a1ab5d7
JP
1380static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
1381 char *sfn_pl, int rec_index,
1382 bool adding)
1383{
1384 struct mlxsw_sp_port *mlxsw_sp_port;
e43aca22 1385 struct net_device *dev;
8a1ab5d7 1386 char mac[ETH_ALEN];
64771e31 1387 u16 lag_vid = 0;
8a1ab5d7 1388 u16 lag_id;
9de6a80e 1389 u16 vid, fid;
12f1501e 1390 bool do_notification = true;
8a1ab5d7
JP
1391 int err;
1392
9de6a80e 1393 mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
8a1ab5d7
JP
1394 mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
1395 if (!mlxsw_sp_port) {
1396 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
12f1501e 1397 goto just_remove;
56ade8fe 1398 }
8a1ab5d7 1399
aac78a44 1400 if (mlxsw_sp_fid_is_vfid(fid)) {
aac78a44
IS
1401 struct mlxsw_sp_port *mlxsw_sp_vport;
1402
d0ec875a
IS
1403 mlxsw_sp_vport = mlxsw_sp_port_vport_find_by_fid(mlxsw_sp_port,
1404 fid);
aac78a44
IS
1405 if (!mlxsw_sp_vport) {
1406 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching vPort following FDB notification\n");
12f1501e 1407 goto just_remove;
aac78a44
IS
1408 }
1409
004f85ea 1410 lag_vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport);
e43aca22 1411 dev = mlxsw_sp_vport->dev;
004f85ea 1412 vid = 0;
aac78a44
IS
1413 /* Override the physical port with the vPort. */
1414 mlxsw_sp_port = mlxsw_sp_vport;
1415 } else {
e43aca22 1416 dev = mlxsw_sp_lag_get(mlxsw_sp, lag_id)->dev;
aac78a44
IS
1417 vid = fid;
1418 }
1419
12f1501e
JP
1420 adding = adding && mlxsw_sp_port->learning;
1421
1422do_fdb_op:
64771e31 1423 err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
12f1501e 1424 adding, true);
8a1ab5d7
JP
1425 if (err) {
1426 if (net_ratelimit())
1427 netdev_err(mlxsw_sp_port->dev, "Failed to set FDB entry\n");
1428 return;
1429 }
1430
12f1501e
JP
1431 if (!do_notification)
1432 return;
45827d78 1433 mlxsw_sp_fdb_call_notifiers(mlxsw_sp_port->learning_sync, adding, mac,
e43aca22 1434 vid, dev);
12f1501e
JP
1435 return;
1436
1437just_remove:
1438 adding = false;
1439 do_notification = false;
1440 goto do_fdb_op;
56ade8fe
JP
1441}
1442
1443static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
1444 char *sfn_pl, int rec_index)
1445{
1446 switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
1447 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
1448 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
1449 rec_index, true);
1450 break;
1451 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
1452 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
1453 rec_index, false);
1454 break;
8a1ab5d7
JP
1455 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
1456 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
1457 rec_index, true);
1458 break;
1459 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
1460 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
1461 rec_index, false);
1462 break;
56ade8fe
JP
1463 }
1464}
1465
1466static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp)
1467{
dd9bdb04
JP
1468 mlxsw_core_schedule_dw(&mlxsw_sp->fdb_notify.dw,
1469 msecs_to_jiffies(mlxsw_sp->fdb_notify.interval));
56ade8fe
JP
1470}
1471
1472static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
1473{
1474 struct mlxsw_sp *mlxsw_sp;
1475 char *sfn_pl;
1476 u8 num_rec;
1477 int i;
1478 int err;
1479
1480 sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
1481 if (!sfn_pl)
1482 return;
1483
1484 mlxsw_sp = container_of(work, struct mlxsw_sp, fdb_notify.dw.work);
1485
4f2c6ae5 1486 rtnl_lock();
1803e0fb
IS
1487 mlxsw_reg_sfn_pack(sfn_pl);
1488 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
1489 if (err) {
1490 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
1491 goto out;
1492 }
1493 num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
1494 for (i = 0; i < num_rec; i++)
1495 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
56ade8fe 1496
1803e0fb 1497out:
4f2c6ae5 1498 rtnl_unlock();
56ade8fe
JP
1499 kfree(sfn_pl);
1500 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
1501}
1502
1503static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
1504{
1505 int err;
1506
1507 err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
1508 if (err) {
1509 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
1510 return err;
1511 }
1512 INIT_DELAYED_WORK(&mlxsw_sp->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
1513 mlxsw_sp->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
1514 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
1515 return 0;
1516}
1517
1518static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
1519{
1520 cancel_delayed_work_sync(&mlxsw_sp->fdb_notify.dw);
1521}
1522
56ade8fe
JP
1523int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
1524{
1525 return mlxsw_sp_fdb_init(mlxsw_sp);
1526}
1527
1528void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
1529{
1530 mlxsw_sp_fdb_fini(mlxsw_sp);
56ade8fe
JP
1531}
1532
56ade8fe
JP
1533void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port *mlxsw_sp_port)
1534{
1535 mlxsw_sp_port->dev->switchdev_ops = &mlxsw_sp_port_switchdev_ops;
1536}
1537
1538void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port)
1539{
1540}