net: dsa: bcm_sf2: Get VLAN_PORT_MASK from b53_device
[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_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
265                                       bool set)
266 {
267         u16 vid;
268         int err;
269
270         if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
271                 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
272
273                 return __mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, vid,
274                                                         set);
275         }
276
277         for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
278                 err = __mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, vid,
279                                                        set);
280                 if (err)
281                         goto err_port_vid_learning_set;
282         }
283
284         return 0;
285
286 err_port_vid_learning_set:
287         for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID)
288                 __mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, vid, !set);
289         return err;
290 }
291
292 static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
293                                            struct switchdev_trans *trans,
294                                            unsigned long brport_flags)
295 {
296         unsigned long learning = mlxsw_sp_port->learning ? BR_LEARNING : 0;
297         unsigned long uc_flood = mlxsw_sp_port->uc_flood ? BR_FLOOD : 0;
298         int err;
299
300         if (!mlxsw_sp_port->bridged)
301                 return -EINVAL;
302
303         if (switchdev_trans_ph_prepare(trans))
304                 return 0;
305
306         if ((uc_flood ^ brport_flags) & BR_FLOOD) {
307                 err = mlxsw_sp_port_uc_flood_set(mlxsw_sp_port,
308                                                  !mlxsw_sp_port->uc_flood);
309                 if (err)
310                         return err;
311         }
312
313         if ((learning ^ brport_flags) & BR_LEARNING) {
314                 err = mlxsw_sp_port_learning_set(mlxsw_sp_port,
315                                                  !mlxsw_sp_port->learning);
316                 if (err)
317                         goto err_port_learning_set;
318         }
319
320         mlxsw_sp_port->uc_flood = brport_flags & BR_FLOOD ? 1 : 0;
321         mlxsw_sp_port->learning = brport_flags & BR_LEARNING ? 1 : 0;
322         mlxsw_sp_port->learning_sync = brport_flags & BR_LEARNING_SYNC ? 1 : 0;
323
324         return 0;
325
326 err_port_learning_set:
327         if ((uc_flood ^ brport_flags) & BR_FLOOD)
328                 mlxsw_sp_port_uc_flood_set(mlxsw_sp_port,
329                                            mlxsw_sp_port->uc_flood);
330         return err;
331 }
332
333 static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
334 {
335         char sfdat_pl[MLXSW_REG_SFDAT_LEN];
336         int err;
337
338         mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
339         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
340         if (err)
341                 return err;
342         mlxsw_sp->ageing_time = ageing_time;
343         return 0;
344 }
345
346 static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
347                                             struct switchdev_trans *trans,
348                                             unsigned long ageing_clock_t)
349 {
350         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
351         unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
352         u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
353
354         if (switchdev_trans_ph_prepare(trans)) {
355                 if (ageing_time < MLXSW_SP_MIN_AGEING_TIME ||
356                     ageing_time > MLXSW_SP_MAX_AGEING_TIME)
357                         return -ERANGE;
358                 else
359                         return 0;
360         }
361
362         return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
363 }
364
365 static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
366                                           struct switchdev_trans *trans,
367                                           struct net_device *orig_dev,
368                                           bool vlan_enabled)
369 {
370         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
371
372         /* SWITCHDEV_TRANS_PREPARE phase */
373         if ((!vlan_enabled) && (mlxsw_sp->master_bridge.dev == orig_dev)) {
374                 netdev_err(mlxsw_sp_port->dev, "Bridge must be vlan-aware\n");
375                 return -EINVAL;
376         }
377
378         return 0;
379 }
380
381 static int mlxsw_sp_port_attr_set(struct net_device *dev,
382                                   const struct switchdev_attr *attr,
383                                   struct switchdev_trans *trans)
384 {
385         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
386         int err = 0;
387
388         mlxsw_sp_port = mlxsw_sp_port_orig_get(attr->orig_dev, mlxsw_sp_port);
389         if (!mlxsw_sp_port)
390                 return -EINVAL;
391
392         switch (attr->id) {
393         case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
394                 err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port, trans,
395                                                        attr->u.stp_state);
396                 break;
397         case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
398                 err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port, trans,
399                                                       attr->u.brport_flags);
400                 break;
401         case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
402                 err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port, trans,
403                                                        attr->u.ageing_time);
404                 break;
405         case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
406                 err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port, trans,
407                                                      attr->orig_dev,
408                                                      attr->u.vlan_filtering);
409                 break;
410         default:
411                 err = -EOPNOTSUPP;
412                 break;
413         }
414
415         return err;
416 }
417
418 static int mlxsw_sp_fid_op(struct mlxsw_sp *mlxsw_sp, u16 fid, bool create)
419 {
420         char sfmr_pl[MLXSW_REG_SFMR_LEN];
421
422         mlxsw_reg_sfmr_pack(sfmr_pl, !create, fid, fid);
423         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfmr), sfmr_pl);
424 }
425
426 static int mlxsw_sp_fid_map(struct mlxsw_sp *mlxsw_sp, u16 fid, bool valid)
427 {
428         enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_VID_TO_FID;
429         char svfa_pl[MLXSW_REG_SVFA_LEN];
430
431         mlxsw_reg_svfa_pack(svfa_pl, 0, mt, valid, fid, fid);
432         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svfa), svfa_pl);
433 }
434
435 static struct mlxsw_sp_fid *mlxsw_sp_fid_alloc(u16 fid)
436 {
437         struct mlxsw_sp_fid *f;
438
439         f = kzalloc(sizeof(*f), GFP_KERNEL);
440         if (!f)
441                 return NULL;
442
443         f->fid = fid;
444
445         return f;
446 }
447
448 struct mlxsw_sp_fid *mlxsw_sp_fid_create(struct mlxsw_sp *mlxsw_sp, u16 fid)
449 {
450         struct mlxsw_sp_fid *f;
451         int err;
452
453         err = mlxsw_sp_fid_op(mlxsw_sp, fid, true);
454         if (err)
455                 return ERR_PTR(err);
456
457         /* Although all the ports member in the FID might be using a
458          * {Port, VID} to FID mapping, we create a global VID-to-FID
459          * mapping. This allows a port to transition to VLAN mode,
460          * knowing the global mapping exists.
461          */
462         err = mlxsw_sp_fid_map(mlxsw_sp, fid, true);
463         if (err)
464                 goto err_fid_map;
465
466         f = mlxsw_sp_fid_alloc(fid);
467         if (!f) {
468                 err = -ENOMEM;
469                 goto err_allocate_fid;
470         }
471
472         list_add(&f->list, &mlxsw_sp->fids);
473
474         return f;
475
476 err_allocate_fid:
477         mlxsw_sp_fid_map(mlxsw_sp, fid, false);
478 err_fid_map:
479         mlxsw_sp_fid_op(mlxsw_sp, fid, false);
480         return ERR_PTR(err);
481 }
482
483 void mlxsw_sp_fid_destroy(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_fid *f)
484 {
485         u16 fid = f->fid;
486
487         list_del(&f->list);
488
489         if (f->r)
490                 mlxsw_sp_rif_bridge_destroy(mlxsw_sp, f->r);
491
492         kfree(f);
493
494         mlxsw_sp_fid_map(mlxsw_sp, fid, false);
495
496         mlxsw_sp_fid_op(mlxsw_sp, fid, false);
497 }
498
499 static int __mlxsw_sp_port_fid_join(struct mlxsw_sp_port *mlxsw_sp_port,
500                                     u16 fid)
501 {
502         struct mlxsw_sp_fid *f;
503
504         f = mlxsw_sp_fid_find(mlxsw_sp_port->mlxsw_sp, fid);
505         if (!f) {
506                 f = mlxsw_sp_fid_create(mlxsw_sp_port->mlxsw_sp, fid);
507                 if (IS_ERR(f))
508                         return PTR_ERR(f);
509         }
510
511         f->ref_count++;
512
513         netdev_dbg(mlxsw_sp_port->dev, "Joined FID=%d\n", fid);
514
515         return 0;
516 }
517
518 static void __mlxsw_sp_port_fid_leave(struct mlxsw_sp_port *mlxsw_sp_port,
519                                       u16 fid)
520 {
521         struct mlxsw_sp_fid *f;
522
523         f = mlxsw_sp_fid_find(mlxsw_sp_port->mlxsw_sp, fid);
524         if (WARN_ON(!f))
525                 return;
526
527         netdev_dbg(mlxsw_sp_port->dev, "Left FID=%d\n", fid);
528
529         mlxsw_sp_port_fdb_flush(mlxsw_sp_port, fid);
530
531         if (--f->ref_count == 0)
532                 mlxsw_sp_fid_destroy(mlxsw_sp_port->mlxsw_sp, f);
533 }
534
535 static int mlxsw_sp_port_fid_map(struct mlxsw_sp_port *mlxsw_sp_port, u16 fid,
536                                  bool valid)
537 {
538         enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID;
539
540         /* If port doesn't have vPorts, then it can use the global
541          * VID-to-FID mapping.
542          */
543         if (list_empty(&mlxsw_sp_port->vports_list))
544                 return 0;
545
546         return mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, valid, fid, fid);
547 }
548
549 static int mlxsw_sp_port_fid_join(struct mlxsw_sp_port *mlxsw_sp_port,
550                                   u16 fid_begin, u16 fid_end)
551 {
552         int fid, err;
553
554         for (fid = fid_begin; fid <= fid_end; fid++) {
555                 err = __mlxsw_sp_port_fid_join(mlxsw_sp_port, fid);
556                 if (err)
557                         goto err_port_fid_join;
558         }
559
560         err = __mlxsw_sp_port_flood_set(mlxsw_sp_port, fid_begin, fid_end,
561                                         true, false);
562         if (err)
563                 goto err_port_flood_set;
564
565         for (fid = fid_begin; fid <= fid_end; fid++) {
566                 err = mlxsw_sp_port_fid_map(mlxsw_sp_port, fid, true);
567                 if (err)
568                         goto err_port_fid_map;
569         }
570
571         return 0;
572
573 err_port_fid_map:
574         for (fid--; fid >= fid_begin; fid--)
575                 mlxsw_sp_port_fid_map(mlxsw_sp_port, fid, false);
576         __mlxsw_sp_port_flood_set(mlxsw_sp_port, fid_begin, fid_end, false,
577                                   false);
578 err_port_flood_set:
579         fid = fid_end;
580 err_port_fid_join:
581         for (fid--; fid >= fid_begin; fid--)
582                 __mlxsw_sp_port_fid_leave(mlxsw_sp_port, fid);
583         return err;
584 }
585
586 static void mlxsw_sp_port_fid_leave(struct mlxsw_sp_port *mlxsw_sp_port,
587                                     u16 fid_begin, u16 fid_end)
588 {
589         int fid;
590
591         for (fid = fid_begin; fid <= fid_end; fid++)
592                 mlxsw_sp_port_fid_map(mlxsw_sp_port, fid, false);
593
594         __mlxsw_sp_port_flood_set(mlxsw_sp_port, fid_begin, fid_end, false,
595                                   false);
596
597         for (fid = fid_begin; fid <= fid_end; fid++)
598                 __mlxsw_sp_port_fid_leave(mlxsw_sp_port, fid);
599 }
600
601 static int __mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port,
602                                     u16 vid)
603 {
604         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
605         char spvid_pl[MLXSW_REG_SPVID_LEN];
606
607         mlxsw_reg_spvid_pack(spvid_pl, mlxsw_sp_port->local_port, vid);
608         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvid), spvid_pl);
609 }
610
611 static int mlxsw_sp_port_allow_untagged_set(struct mlxsw_sp_port *mlxsw_sp_port,
612                                             bool allow)
613 {
614         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
615         char spaft_pl[MLXSW_REG_SPAFT_LEN];
616
617         mlxsw_reg_spaft_pack(spaft_pl, mlxsw_sp_port->local_port, allow);
618         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spaft), spaft_pl);
619 }
620
621 int mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
622 {
623         struct net_device *dev = mlxsw_sp_port->dev;
624         int err;
625
626         if (!vid) {
627                 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port, false);
628                 if (err) {
629                         netdev_err(dev, "Failed to disallow untagged traffic\n");
630                         return err;
631                 }
632         } else {
633                 err = __mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid);
634                 if (err) {
635                         netdev_err(dev, "Failed to set PVID\n");
636                         return err;
637                 }
638
639                 /* Only allow if not already allowed. */
640                 if (!mlxsw_sp_port->pvid) {
641                         err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port,
642                                                                true);
643                         if (err) {
644                                 netdev_err(dev, "Failed to allow untagged traffic\n");
645                                 goto err_port_allow_untagged_set;
646                         }
647                 }
648         }
649
650         mlxsw_sp_port->pvid = vid;
651         return 0;
652
653 err_port_allow_untagged_set:
654         __mlxsw_sp_port_pvid_set(mlxsw_sp_port, mlxsw_sp_port->pvid);
655         return err;
656 }
657
658 static int __mlxsw_sp_port_vlans_set(struct mlxsw_sp_port *mlxsw_sp_port,
659                                      u16 vid_begin, u16 vid_end, bool is_member,
660                                      bool untagged)
661 {
662         u16 vid, vid_e;
663         int err;
664
665         for (vid = vid_begin; vid <= vid_end;
666              vid += MLXSW_REG_SPVM_REC_MAX_COUNT) {
667                 vid_e = min((u16) (vid + MLXSW_REG_SPVM_REC_MAX_COUNT - 1),
668                             vid_end);
669
670                 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid_e,
671                                              is_member, untagged);
672                 if (err)
673                         return err;
674         }
675
676         return 0;
677 }
678
679 static int mlxsw_sp_port_vid_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
680                                           u16 vid_begin, u16 vid_end,
681                                           bool learn_enable)
682 {
683         u16 vid, vid_e;
684         int err;
685
686         for (vid = vid_begin; vid <= vid_end;
687              vid += MLXSW_REG_SPVMLR_REC_MAX_COUNT) {
688                 vid_e = min((u16) (vid + MLXSW_REG_SPVMLR_REC_MAX_COUNT - 1),
689                             vid_end);
690
691                 err = __mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid,
692                                                        vid_e, learn_enable);
693                 if (err)
694                         return err;
695         }
696
697         return 0;
698 }
699
700 static int __mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
701                                      u16 vid_begin, u16 vid_end,
702                                      bool flag_untagged, bool flag_pvid)
703 {
704         struct net_device *dev = mlxsw_sp_port->dev;
705         u16 vid, old_pvid;
706         int err;
707
708         if (!mlxsw_sp_port->bridged)
709                 return -EINVAL;
710
711         err = mlxsw_sp_port_fid_join(mlxsw_sp_port, vid_begin, vid_end);
712         if (err) {
713                 netdev_err(dev, "Failed to join FIDs\n");
714                 return err;
715         }
716
717         err = __mlxsw_sp_port_vlans_set(mlxsw_sp_port, vid_begin, vid_end,
718                                         true, flag_untagged);
719         if (err) {
720                 netdev_err(dev, "Unable to add VIDs %d-%d\n", vid_begin,
721                            vid_end);
722                 goto err_port_vlans_set;
723         }
724
725         old_pvid = mlxsw_sp_port->pvid;
726         if (flag_pvid && old_pvid != vid_begin) {
727                 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid_begin);
728                 if (err) {
729                         netdev_err(dev, "Unable to add PVID %d\n", vid_begin);
730                         goto err_port_pvid_set;
731                 }
732         } else if (!flag_pvid && old_pvid >= vid_begin && old_pvid <= vid_end) {
733                 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, 0);
734                 if (err) {
735                         netdev_err(dev, "Unable to del PVID\n");
736                         goto err_port_pvid_set;
737                 }
738         }
739
740         err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid_begin, vid_end,
741                                              mlxsw_sp_port->learning);
742         if (err) {
743                 netdev_err(dev, "Failed to set learning for VIDs %d-%d\n",
744                            vid_begin, vid_end);
745                 goto err_port_vid_learning_set;
746         }
747
748         /* Changing activity bits only if HW operation succeded */
749         for (vid = vid_begin; vid <= vid_end; vid++) {
750                 set_bit(vid, mlxsw_sp_port->active_vlans);
751                 if (flag_untagged)
752                         set_bit(vid, mlxsw_sp_port->untagged_vlans);
753                 else
754                         clear_bit(vid, mlxsw_sp_port->untagged_vlans);
755         }
756
757         /* STP state change must be done after we set active VLANs */
758         err = mlxsw_sp_port_stp_state_set(mlxsw_sp_port,
759                                           mlxsw_sp_port->stp_state);
760         if (err) {
761                 netdev_err(dev, "Failed to set STP state\n");
762                 goto err_port_stp_state_set;
763         }
764
765         return 0;
766
767 err_port_stp_state_set:
768         for (vid = vid_begin; vid <= vid_end; vid++)
769                 clear_bit(vid, mlxsw_sp_port->active_vlans);
770         mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid_begin, vid_end,
771                                        false);
772 err_port_vid_learning_set:
773         if (old_pvid != mlxsw_sp_port->pvid)
774                 mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid);
775 err_port_pvid_set:
776         __mlxsw_sp_port_vlans_set(mlxsw_sp_port, vid_begin, vid_end, false,
777                                   false);
778 err_port_vlans_set:
779         mlxsw_sp_port_fid_leave(mlxsw_sp_port, vid_begin, vid_end);
780         return err;
781 }
782
783 static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
784                                    const struct switchdev_obj_port_vlan *vlan,
785                                    struct switchdev_trans *trans)
786 {
787         bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
788         bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
789
790         if (switchdev_trans_ph_prepare(trans))
791                 return 0;
792
793         return __mlxsw_sp_port_vlans_add(mlxsw_sp_port,
794                                          vlan->vid_begin, vlan->vid_end,
795                                          flag_untagged, flag_pvid);
796 }
797
798 static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
799 {
800         return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
801                          MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY;
802 }
803
804 static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
805 {
806         return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
807                         MLXSW_REG_SFD_OP_WRITE_REMOVE;
808 }
809
810 static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
811                                      const char *mac, u16 fid, bool adding,
812                                      enum mlxsw_reg_sfd_rec_action action,
813                                      bool dynamic)
814 {
815         char *sfd_pl;
816         int err;
817
818         sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
819         if (!sfd_pl)
820                 return -ENOMEM;
821
822         mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
823         mlxsw_reg_sfd_uc_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
824                               mac, fid, action, local_port);
825         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
826         kfree(sfd_pl);
827
828         return err;
829 }
830
831 static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
832                                    const char *mac, u16 fid, bool adding,
833                                    bool dynamic)
834 {
835         return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding,
836                                          MLXSW_REG_SFD_REC_ACTION_NOP, dynamic);
837 }
838
839 int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid,
840                         bool adding)
841 {
842         return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding,
843                                          MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER,
844                                          false);
845 }
846
847 static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
848                                        const char *mac, u16 fid, u16 lag_vid,
849                                        bool adding, bool dynamic)
850 {
851         char *sfd_pl;
852         int err;
853
854         sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
855         if (!sfd_pl)
856                 return -ENOMEM;
857
858         mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
859         mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
860                                   mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
861                                   lag_vid, lag_id);
862         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
863         kfree(sfd_pl);
864
865         return err;
866 }
867
868 static int
869 mlxsw_sp_port_fdb_static_add(struct mlxsw_sp_port *mlxsw_sp_port,
870                              const struct switchdev_obj_port_fdb *fdb,
871                              struct switchdev_trans *trans)
872 {
873         u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, fdb->vid);
874         u16 lag_vid = 0;
875
876         if (switchdev_trans_ph_prepare(trans))
877                 return 0;
878
879         if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
880                 lag_vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
881         }
882
883         if (!mlxsw_sp_port->lagged)
884                 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp_port->mlxsw_sp,
885                                                mlxsw_sp_port->local_port,
886                                                fdb->addr, fid, true, false);
887         else
888                 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp_port->mlxsw_sp,
889                                                    mlxsw_sp_port->lag_id,
890                                                    fdb->addr, fid, lag_vid,
891                                                    true, false);
892 }
893
894 static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
895                                 u16 fid, u16 mid, bool adding)
896 {
897         char *sfd_pl;
898         int err;
899
900         sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
901         if (!sfd_pl)
902                 return -ENOMEM;
903
904         mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
905         mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
906                               MLXSW_REG_SFD_REC_ACTION_NOP, mid);
907         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
908         kfree(sfd_pl);
909         return err;
910 }
911
912 static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 mid,
913                                   bool add, bool clear_all_ports)
914 {
915         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
916         char *smid_pl;
917         int err, i;
918
919         smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
920         if (!smid_pl)
921                 return -ENOMEM;
922
923         mlxsw_reg_smid_pack(smid_pl, mid, mlxsw_sp_port->local_port, add);
924         if (clear_all_ports) {
925                 for (i = 1; i < MLXSW_PORT_MAX_PORTS; i++)
926                         if (mlxsw_sp->ports[i])
927                                 mlxsw_reg_smid_port_mask_set(smid_pl, i, 1);
928         }
929         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
930         kfree(smid_pl);
931         return err;
932 }
933
934 static struct mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp *mlxsw_sp,
935                                               const unsigned char *addr,
936                                               u16 vid)
937 {
938         struct mlxsw_sp_mid *mid;
939
940         list_for_each_entry(mid, &mlxsw_sp->br_mids.list, list) {
941                 if (ether_addr_equal(mid->addr, addr) && mid->vid == vid)
942                         return mid;
943         }
944         return NULL;
945 }
946
947 static struct mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
948                                                 const unsigned char *addr,
949                                                 u16 vid)
950 {
951         struct mlxsw_sp_mid *mid;
952         u16 mid_idx;
953
954         mid_idx = find_first_zero_bit(mlxsw_sp->br_mids.mapped,
955                                       MLXSW_SP_MID_MAX);
956         if (mid_idx == MLXSW_SP_MID_MAX)
957                 return NULL;
958
959         mid = kzalloc(sizeof(*mid), GFP_KERNEL);
960         if (!mid)
961                 return NULL;
962
963         set_bit(mid_idx, mlxsw_sp->br_mids.mapped);
964         ether_addr_copy(mid->addr, addr);
965         mid->vid = vid;
966         mid->mid = mid_idx;
967         mid->ref_count = 0;
968         list_add_tail(&mid->list, &mlxsw_sp->br_mids.list);
969
970         return mid;
971 }
972
973 static int __mlxsw_sp_mc_dec_ref(struct mlxsw_sp *mlxsw_sp,
974                                  struct mlxsw_sp_mid *mid)
975 {
976         if (--mid->ref_count == 0) {
977                 list_del(&mid->list);
978                 clear_bit(mid->mid, mlxsw_sp->br_mids.mapped);
979                 kfree(mid);
980                 return 1;
981         }
982         return 0;
983 }
984
985 static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
986                                  const struct switchdev_obj_port_mdb *mdb,
987                                  struct switchdev_trans *trans)
988 {
989         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
990         struct net_device *dev = mlxsw_sp_port->dev;
991         struct mlxsw_sp_mid *mid;
992         u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, mdb->vid);
993         int err = 0;
994
995         if (switchdev_trans_ph_prepare(trans))
996                 return 0;
997
998         mid = __mlxsw_sp_mc_get(mlxsw_sp, mdb->addr, mdb->vid);
999         if (!mid) {
1000                 mid = __mlxsw_sp_mc_alloc(mlxsw_sp, mdb->addr, mdb->vid);
1001                 if (!mid) {
1002                         netdev_err(dev, "Unable to allocate MC group\n");
1003                         return -ENOMEM;
1004                 }
1005         }
1006         mid->ref_count++;
1007
1008         err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true,
1009                                      mid->ref_count == 1);
1010         if (err) {
1011                 netdev_err(dev, "Unable to set SMID\n");
1012                 goto err_out;
1013         }
1014
1015         if (mid->ref_count == 1) {
1016                 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mdb->addr, fid, mid->mid,
1017                                            true);
1018                 if (err) {
1019                         netdev_err(dev, "Unable to set MC SFD\n");
1020                         goto err_out;
1021                 }
1022         }
1023
1024         return 0;
1025
1026 err_out:
1027         __mlxsw_sp_mc_dec_ref(mlxsw_sp, mid);
1028         return err;
1029 }
1030
1031 static int mlxsw_sp_port_obj_add(struct net_device *dev,
1032                                  const struct switchdev_obj *obj,
1033                                  struct switchdev_trans *trans)
1034 {
1035         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1036         int err = 0;
1037
1038         mlxsw_sp_port = mlxsw_sp_port_orig_get(obj->orig_dev, mlxsw_sp_port);
1039         if (!mlxsw_sp_port)
1040                 return -EINVAL;
1041
1042         switch (obj->id) {
1043         case SWITCHDEV_OBJ_ID_PORT_VLAN:
1044                 if (mlxsw_sp_port_is_vport(mlxsw_sp_port))
1045                         return 0;
1046
1047                 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port,
1048                                               SWITCHDEV_OBJ_PORT_VLAN(obj),
1049                                               trans);
1050                 break;
1051         case SWITCHDEV_OBJ_ID_IPV4_FIB:
1052                 err = mlxsw_sp_router_fib4_add(mlxsw_sp_port,
1053                                                SWITCHDEV_OBJ_IPV4_FIB(obj),
1054                                                trans);
1055                 break;
1056         case SWITCHDEV_OBJ_ID_PORT_FDB:
1057                 err = mlxsw_sp_port_fdb_static_add(mlxsw_sp_port,
1058                                                    SWITCHDEV_OBJ_PORT_FDB(obj),
1059                                                    trans);
1060                 break;
1061         case SWITCHDEV_OBJ_ID_PORT_MDB:
1062                 err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
1063                                             SWITCHDEV_OBJ_PORT_MDB(obj),
1064                                             trans);
1065                 break;
1066         default:
1067                 err = -EOPNOTSUPP;
1068                 break;
1069         }
1070
1071         return err;
1072 }
1073
1074 static int __mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1075                                      u16 vid_begin, u16 vid_end)
1076 {
1077         u16 vid, pvid;
1078
1079         if (!mlxsw_sp_port->bridged)
1080                 return -EINVAL;
1081
1082         mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid_begin, vid_end,
1083                                        false);
1084
1085         pvid = mlxsw_sp_port->pvid;
1086         if (pvid >= vid_begin && pvid <= vid_end)
1087                 mlxsw_sp_port_pvid_set(mlxsw_sp_port, 0);
1088
1089         __mlxsw_sp_port_vlans_set(mlxsw_sp_port, vid_begin, vid_end, false,
1090                                   false);
1091
1092         mlxsw_sp_port_fid_leave(mlxsw_sp_port, vid_begin, vid_end);
1093
1094         /* Changing activity bits only if HW operation succeded */
1095         for (vid = vid_begin; vid <= vid_end; vid++)
1096                 clear_bit(vid, mlxsw_sp_port->active_vlans);
1097
1098         return 0;
1099 }
1100
1101 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1102                                    const struct switchdev_obj_port_vlan *vlan)
1103 {
1104         return __mlxsw_sp_port_vlans_del(mlxsw_sp_port, vlan->vid_begin,
1105                                          vlan->vid_end);
1106 }
1107
1108 void mlxsw_sp_port_active_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port)
1109 {
1110         u16 vid;
1111
1112         for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID)
1113                 __mlxsw_sp_port_vlans_del(mlxsw_sp_port, vid, vid);
1114 }
1115
1116 static int
1117 mlxsw_sp_port_fdb_static_del(struct mlxsw_sp_port *mlxsw_sp_port,
1118                              const struct switchdev_obj_port_fdb *fdb)
1119 {
1120         u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, fdb->vid);
1121         u16 lag_vid = 0;
1122
1123         if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
1124                 lag_vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
1125         }
1126
1127         if (!mlxsw_sp_port->lagged)
1128                 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp_port->mlxsw_sp,
1129                                                mlxsw_sp_port->local_port,
1130                                                fdb->addr, fid,
1131                                                false, false);
1132         else
1133                 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp_port->mlxsw_sp,
1134                                                    mlxsw_sp_port->lag_id,
1135                                                    fdb->addr, fid, lag_vid,
1136                                                    false, false);
1137 }
1138
1139 static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1140                                  const struct switchdev_obj_port_mdb *mdb)
1141 {
1142         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1143         struct net_device *dev = mlxsw_sp_port->dev;
1144         struct mlxsw_sp_mid *mid;
1145         u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, mdb->vid);
1146         u16 mid_idx;
1147         int err = 0;
1148
1149         mid = __mlxsw_sp_mc_get(mlxsw_sp, mdb->addr, mdb->vid);
1150         if (!mid) {
1151                 netdev_err(dev, "Unable to remove port from MC DB\n");
1152                 return -EINVAL;
1153         }
1154
1155         err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false, false);
1156         if (err)
1157                 netdev_err(dev, "Unable to remove port from SMID\n");
1158
1159         mid_idx = mid->mid;
1160         if (__mlxsw_sp_mc_dec_ref(mlxsw_sp, mid)) {
1161                 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mdb->addr, fid, mid_idx,
1162                                            false);
1163                 if (err)
1164                         netdev_err(dev, "Unable to remove MC SFD\n");
1165         }
1166
1167         return err;
1168 }
1169
1170 static int mlxsw_sp_port_obj_del(struct net_device *dev,
1171                                  const struct switchdev_obj *obj)
1172 {
1173         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1174         int err = 0;
1175
1176         mlxsw_sp_port = mlxsw_sp_port_orig_get(obj->orig_dev, mlxsw_sp_port);
1177         if (!mlxsw_sp_port)
1178                 return -EINVAL;
1179
1180         switch (obj->id) {
1181         case SWITCHDEV_OBJ_ID_PORT_VLAN:
1182                 if (mlxsw_sp_port_is_vport(mlxsw_sp_port))
1183                         return 0;
1184
1185                 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1186                                               SWITCHDEV_OBJ_PORT_VLAN(obj));
1187                 break;
1188         case SWITCHDEV_OBJ_ID_IPV4_FIB:
1189                 err = mlxsw_sp_router_fib4_del(mlxsw_sp_port,
1190                                                SWITCHDEV_OBJ_IPV4_FIB(obj));
1191                 break;
1192         case SWITCHDEV_OBJ_ID_PORT_FDB:
1193                 err = mlxsw_sp_port_fdb_static_del(mlxsw_sp_port,
1194                                                    SWITCHDEV_OBJ_PORT_FDB(obj));
1195                 break;
1196         case SWITCHDEV_OBJ_ID_PORT_MDB:
1197                 err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1198                                             SWITCHDEV_OBJ_PORT_MDB(obj));
1199                 break;
1200         default:
1201                 err = -EOPNOTSUPP;
1202                 break;
1203         }
1204
1205         return err;
1206 }
1207
1208 static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
1209                                                    u16 lag_id)
1210 {
1211         struct mlxsw_sp_port *mlxsw_sp_port;
1212         int i;
1213
1214         for (i = 0; i < MLXSW_SP_PORT_PER_LAG_MAX; i++) {
1215                 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
1216                 if (mlxsw_sp_port)
1217                         return mlxsw_sp_port;
1218         }
1219         return NULL;
1220 }
1221
1222 static int mlxsw_sp_port_fdb_dump(struct mlxsw_sp_port *mlxsw_sp_port,
1223                                   struct switchdev_obj_port_fdb *fdb,
1224                                   switchdev_obj_dump_cb_t *cb,
1225                                   struct net_device *orig_dev)
1226 {
1227         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1228         struct mlxsw_sp_port *tmp;
1229         struct mlxsw_sp_fid *f;
1230         u16 vport_fid;
1231         char *sfd_pl;
1232         char mac[ETH_ALEN];
1233         u16 fid;
1234         u8 local_port;
1235         u16 lag_id;
1236         u8 num_rec;
1237         int stored_err = 0;
1238         int i;
1239         int err;
1240
1241         sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1242         if (!sfd_pl)
1243                 return -ENOMEM;
1244
1245         f = mlxsw_sp_vport_fid_get(mlxsw_sp_port);
1246         vport_fid = f ? f->fid : 0;
1247
1248         mlxsw_reg_sfd_pack(sfd_pl, MLXSW_REG_SFD_OP_QUERY_DUMP, 0);
1249         do {
1250                 mlxsw_reg_sfd_num_rec_set(sfd_pl, MLXSW_REG_SFD_REC_MAX_COUNT);
1251                 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1252                 if (err)
1253                         goto out;
1254
1255                 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1256
1257                 /* Even in case of error, we have to run the dump to the end
1258                  * so the session in firmware is finished.
1259                  */
1260                 if (stored_err)
1261                         continue;
1262
1263                 for (i = 0; i < num_rec; i++) {
1264                         switch (mlxsw_reg_sfd_rec_type_get(sfd_pl, i)) {
1265                         case MLXSW_REG_SFD_REC_TYPE_UNICAST:
1266                                 mlxsw_reg_sfd_uc_unpack(sfd_pl, i, mac, &fid,
1267                                                         &local_port);
1268                                 if (local_port == mlxsw_sp_port->local_port) {
1269                                         if (vport_fid && vport_fid == fid)
1270                                                 fdb->vid = 0;
1271                                         else if (!vport_fid &&
1272                                                  !mlxsw_sp_fid_is_vfid(fid))
1273                                                 fdb->vid = fid;
1274                                         else
1275                                                 continue;
1276                                         ether_addr_copy(fdb->addr, mac);
1277                                         fdb->ndm_state = NUD_REACHABLE;
1278                                         err = cb(&fdb->obj);
1279                                         if (err)
1280                                                 stored_err = err;
1281                                 }
1282                                 break;
1283                         case MLXSW_REG_SFD_REC_TYPE_UNICAST_LAG:
1284                                 mlxsw_reg_sfd_uc_lag_unpack(sfd_pl, i,
1285                                                             mac, &fid, &lag_id);
1286                                 tmp = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
1287                                 if (tmp && tmp->local_port ==
1288                                     mlxsw_sp_port->local_port) {
1289                                         /* LAG records can only point to LAG
1290                                          * devices or VLAN devices on top.
1291                                          */
1292                                         if (!netif_is_lag_master(orig_dev) &&
1293                                             !is_vlan_dev(orig_dev))
1294                                                 continue;
1295                                         if (vport_fid && vport_fid == fid)
1296                                                 fdb->vid = 0;
1297                                         else if (!vport_fid &&
1298                                                  !mlxsw_sp_fid_is_vfid(fid))
1299                                                 fdb->vid = fid;
1300                                         else
1301                                                 continue;
1302                                         ether_addr_copy(fdb->addr, mac);
1303                                         fdb->ndm_state = NUD_REACHABLE;
1304                                         err = cb(&fdb->obj);
1305                                         if (err)
1306                                                 stored_err = err;
1307                                 }
1308                                 break;
1309                         }
1310                 }
1311         } while (num_rec == MLXSW_REG_SFD_REC_MAX_COUNT);
1312
1313 out:
1314         kfree(sfd_pl);
1315         return stored_err ? stored_err : err;
1316 }
1317
1318 static int mlxsw_sp_port_vlan_dump(struct mlxsw_sp_port *mlxsw_sp_port,
1319                                    struct switchdev_obj_port_vlan *vlan,
1320                                    switchdev_obj_dump_cb_t *cb)
1321 {
1322         u16 vid;
1323         int err = 0;
1324
1325         if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
1326                 vlan->flags = 0;
1327                 vlan->vid_begin = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
1328                 vlan->vid_end = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
1329                 return cb(&vlan->obj);
1330         }
1331
1332         for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
1333                 vlan->flags = 0;
1334                 if (vid == mlxsw_sp_port->pvid)
1335                         vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1336                 if (test_bit(vid, mlxsw_sp_port->untagged_vlans))
1337                         vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1338                 vlan->vid_begin = vid;
1339                 vlan->vid_end = vid;
1340                 err = cb(&vlan->obj);
1341                 if (err)
1342                         break;
1343         }
1344         return err;
1345 }
1346
1347 static int mlxsw_sp_port_obj_dump(struct net_device *dev,
1348                                   struct switchdev_obj *obj,
1349                                   switchdev_obj_dump_cb_t *cb)
1350 {
1351         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1352         int err = 0;
1353
1354         mlxsw_sp_port = mlxsw_sp_port_orig_get(obj->orig_dev, mlxsw_sp_port);
1355         if (!mlxsw_sp_port)
1356                 return -EINVAL;
1357
1358         switch (obj->id) {
1359         case SWITCHDEV_OBJ_ID_PORT_VLAN:
1360                 err = mlxsw_sp_port_vlan_dump(mlxsw_sp_port,
1361                                               SWITCHDEV_OBJ_PORT_VLAN(obj), cb);
1362                 break;
1363         case SWITCHDEV_OBJ_ID_PORT_FDB:
1364                 err = mlxsw_sp_port_fdb_dump(mlxsw_sp_port,
1365                                              SWITCHDEV_OBJ_PORT_FDB(obj), cb,
1366                                              obj->orig_dev);
1367                 break;
1368         default:
1369                 err = -EOPNOTSUPP;
1370                 break;
1371         }
1372
1373         return err;
1374 }
1375
1376 static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = {
1377         .switchdev_port_attr_get        = mlxsw_sp_port_attr_get,
1378         .switchdev_port_attr_set        = mlxsw_sp_port_attr_set,
1379         .switchdev_port_obj_add         = mlxsw_sp_port_obj_add,
1380         .switchdev_port_obj_del         = mlxsw_sp_port_obj_del,
1381         .switchdev_port_obj_dump        = mlxsw_sp_port_obj_dump,
1382 };
1383
1384 static void mlxsw_sp_fdb_call_notifiers(bool learning_sync, bool adding,
1385                                         char *mac, u16 vid,
1386                                         struct net_device *dev)
1387 {
1388         struct switchdev_notifier_fdb_info info;
1389         unsigned long notifier_type;
1390
1391         if (learning_sync) {
1392                 info.addr = mac;
1393                 info.vid = vid;
1394                 notifier_type = adding ? SWITCHDEV_FDB_ADD : SWITCHDEV_FDB_DEL;
1395                 call_switchdev_notifiers(notifier_type, dev, &info.info);
1396         }
1397 }
1398
1399 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
1400                                             char *sfn_pl, int rec_index,
1401                                             bool adding)
1402 {
1403         struct mlxsw_sp_port *mlxsw_sp_port;
1404         char mac[ETH_ALEN];
1405         u8 local_port;
1406         u16 vid, fid;
1407         bool do_notification = true;
1408         int err;
1409
1410         mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
1411         mlxsw_sp_port = mlxsw_sp->ports[local_port];
1412         if (!mlxsw_sp_port) {
1413                 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
1414                 goto just_remove;
1415         }
1416
1417         if (mlxsw_sp_fid_is_vfid(fid)) {
1418                 struct mlxsw_sp_port *mlxsw_sp_vport;
1419
1420                 mlxsw_sp_vport = mlxsw_sp_port_vport_find_by_fid(mlxsw_sp_port,
1421                                                                  fid);
1422                 if (!mlxsw_sp_vport) {
1423                         netdev_err(mlxsw_sp_port->dev, "Failed to find a matching vPort following FDB notification\n");
1424                         goto just_remove;
1425                 }
1426                 vid = 0;
1427                 /* Override the physical port with the vPort. */
1428                 mlxsw_sp_port = mlxsw_sp_vport;
1429         } else {
1430                 vid = fid;
1431         }
1432
1433 do_fdb_op:
1434         err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
1435                                       adding, true);
1436         if (err) {
1437                 if (net_ratelimit())
1438                         netdev_err(mlxsw_sp_port->dev, "Failed to set FDB entry\n");
1439                 return;
1440         }
1441
1442         if (!do_notification)
1443                 return;
1444         mlxsw_sp_fdb_call_notifiers(mlxsw_sp_port->learning_sync,
1445                                     adding, mac, vid, mlxsw_sp_port->dev);
1446         return;
1447
1448 just_remove:
1449         adding = false;
1450         do_notification = false;
1451         goto do_fdb_op;
1452 }
1453
1454 static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
1455                                                 char *sfn_pl, int rec_index,
1456                                                 bool adding)
1457 {
1458         struct mlxsw_sp_port *mlxsw_sp_port;
1459         struct net_device *dev;
1460         char mac[ETH_ALEN];
1461         u16 lag_vid = 0;
1462         u16 lag_id;
1463         u16 vid, fid;
1464         bool do_notification = true;
1465         int err;
1466
1467         mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
1468         mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
1469         if (!mlxsw_sp_port) {
1470                 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
1471                 goto just_remove;
1472         }
1473
1474         if (mlxsw_sp_fid_is_vfid(fid)) {
1475                 struct mlxsw_sp_port *mlxsw_sp_vport;
1476
1477                 mlxsw_sp_vport = mlxsw_sp_port_vport_find_by_fid(mlxsw_sp_port,
1478                                                                  fid);
1479                 if (!mlxsw_sp_vport) {
1480                         netdev_err(mlxsw_sp_port->dev, "Failed to find a matching vPort following FDB notification\n");
1481                         goto just_remove;
1482                 }
1483
1484                 lag_vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport);
1485                 dev = mlxsw_sp_vport->dev;
1486                 vid = 0;
1487                 /* Override the physical port with the vPort. */
1488                 mlxsw_sp_port = mlxsw_sp_vport;
1489         } else {
1490                 dev = mlxsw_sp_lag_get(mlxsw_sp, lag_id)->dev;
1491                 vid = fid;
1492         }
1493
1494 do_fdb_op:
1495         err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
1496                                           adding, true);
1497         if (err) {
1498                 if (net_ratelimit())
1499                         netdev_err(mlxsw_sp_port->dev, "Failed to set FDB entry\n");
1500                 return;
1501         }
1502
1503         if (!do_notification)
1504                 return;
1505         mlxsw_sp_fdb_call_notifiers(mlxsw_sp_port->learning_sync, adding, mac,
1506                                     vid, dev);
1507         return;
1508
1509 just_remove:
1510         adding = false;
1511         do_notification = false;
1512         goto do_fdb_op;
1513 }
1514
1515 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
1516                                             char *sfn_pl, int rec_index)
1517 {
1518         switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
1519         case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
1520                 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
1521                                                 rec_index, true);
1522                 break;
1523         case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
1524                 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
1525                                                 rec_index, false);
1526                 break;
1527         case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
1528                 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
1529                                                     rec_index, true);
1530                 break;
1531         case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
1532                 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
1533                                                     rec_index, false);
1534                 break;
1535         }
1536 }
1537
1538 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp)
1539 {
1540         mlxsw_core_schedule_dw(&mlxsw_sp->fdb_notify.dw,
1541                                msecs_to_jiffies(mlxsw_sp->fdb_notify.interval));
1542 }
1543
1544 static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
1545 {
1546         struct mlxsw_sp *mlxsw_sp;
1547         char *sfn_pl;
1548         u8 num_rec;
1549         int i;
1550         int err;
1551
1552         sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
1553         if (!sfn_pl)
1554                 return;
1555
1556         mlxsw_sp = container_of(work, struct mlxsw_sp, fdb_notify.dw.work);
1557
1558         rtnl_lock();
1559         mlxsw_reg_sfn_pack(sfn_pl);
1560         err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
1561         if (err) {
1562                 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
1563                 goto out;
1564         }
1565         num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
1566         for (i = 0; i < num_rec; i++)
1567                 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
1568
1569 out:
1570         rtnl_unlock();
1571         kfree(sfn_pl);
1572         mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
1573 }
1574
1575 static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
1576 {
1577         int err;
1578
1579         err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
1580         if (err) {
1581                 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
1582                 return err;
1583         }
1584         INIT_DELAYED_WORK(&mlxsw_sp->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
1585         mlxsw_sp->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
1586         mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
1587         return 0;
1588 }
1589
1590 static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
1591 {
1592         cancel_delayed_work_sync(&mlxsw_sp->fdb_notify.dw);
1593 }
1594
1595 int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
1596 {
1597         return mlxsw_sp_fdb_init(mlxsw_sp);
1598 }
1599
1600 void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
1601 {
1602         mlxsw_sp_fdb_fini(mlxsw_sp);
1603 }
1604
1605 void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port *mlxsw_sp_port)
1606 {
1607         mlxsw_sp_port->dev->switchdev_ops = &mlxsw_sp_port_switchdev_ops;
1608 }
1609
1610 void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port)
1611 {
1612 }