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