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