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