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