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