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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
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>
55 static u16 mlxsw_sp_port_vid_to_fid_get(struct mlxsw_sp_port *mlxsw_sp_port,
60 if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
61 u16 vfid = mlxsw_sp_vport_vfid_get(mlxsw_sp_port);
63 fid = mlxsw_sp_vfid_to_fid(vfid);
67 fid = mlxsw_sp_port->pvid;
72 static struct mlxsw_sp_port *
73 mlxsw_sp_port_orig_get(struct net_device *dev,
74 struct mlxsw_sp_port *mlxsw_sp_port)
76 struct mlxsw_sp_port *mlxsw_sp_vport;
79 if (!is_vlan_dev(dev))
82 vid = vlan_dev_vlan_id(dev);
83 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
84 WARN_ON(!mlxsw_sp_vport);
86 return mlxsw_sp_vport;
89 static int mlxsw_sp_port_attr_get(struct net_device *dev,
90 struct switchdev_attr *attr)
92 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
93 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
95 mlxsw_sp_port = mlxsw_sp_port_orig_get(attr->orig_dev, mlxsw_sp_port);
100 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
101 attr->u.ppid.id_len = sizeof(mlxsw_sp->base_mac);
102 memcpy(&attr->u.ppid.id, &mlxsw_sp->base_mac,
103 attr->u.ppid.id_len);
105 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
106 attr->u.brport_flags =
107 (mlxsw_sp_port->learning ? BR_LEARNING : 0) |
108 (mlxsw_sp_port->learning_sync ? BR_LEARNING_SYNC : 0) |
109 (mlxsw_sp_port->uc_flood ? BR_FLOOD : 0);
118 static int mlxsw_sp_port_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
121 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
122 enum mlxsw_reg_spms_state spms_state;
128 case BR_STATE_FORWARDING:
129 spms_state = MLXSW_REG_SPMS_STATE_FORWARDING;
131 case BR_STATE_LEARNING:
132 spms_state = MLXSW_REG_SPMS_STATE_LEARNING;
134 case BR_STATE_LISTENING: /* fall-through */
135 case BR_STATE_DISABLED: /* fall-through */
136 case BR_STATE_BLOCKING:
137 spms_state = MLXSW_REG_SPMS_STATE_DISCARDING;
143 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
146 mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
148 if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
149 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
150 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
152 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID)
153 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
156 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
161 static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
162 struct switchdev_trans *trans,
165 if (switchdev_trans_ph_prepare(trans))
168 mlxsw_sp_port->stp_state = state;
169 return mlxsw_sp_port_stp_state_set(mlxsw_sp_port, state);
172 static bool mlxsw_sp_vfid_is_vport_br(u16 vfid)
174 return vfid >= MLXSW_SP_VFID_PORT_MAX;
177 static int __mlxsw_sp_port_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
178 u16 idx_begin, u16 idx_end, bool set,
181 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
182 u16 local_port = mlxsw_sp_port->local_port;
183 enum mlxsw_flood_table_type table_type;
184 u16 range = idx_end - idx_begin + 1;
188 if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
189 table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID;
190 if (mlxsw_sp_vfid_is_vport_br(idx_begin))
191 local_port = mlxsw_sp_port->local_port;
193 local_port = MLXSW_PORT_CPU_PORT;
195 table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST;
198 sftr_pl = kmalloc(MLXSW_REG_SFTR_LEN, GFP_KERNEL);
202 mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_UC, idx_begin,
203 table_type, range, local_port, set);
204 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl);
208 /* Flooding control allows one to decide whether a given port will
209 * flood unicast traffic for which there is no FDB entry.
214 mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_BM, idx_begin,
215 table_type, range, local_port, set);
216 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl);
223 static int mlxsw_sp_port_uc_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
226 struct net_device *dev = mlxsw_sp_port->dev;
227 u16 vid, last_visited_vid;
230 if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
231 u16 vfid = mlxsw_sp_vport_vfid_get(mlxsw_sp_port);
233 return __mlxsw_sp_port_flood_set(mlxsw_sp_port, vfid, vfid,
237 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
238 err = __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid, vid, set,
241 last_visited_vid = vid;
242 goto err_port_flood_set;
249 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, last_visited_vid)
250 __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid, vid, !set, true);
251 netdev_err(dev, "Failed to configure unicast flooding\n");
255 int mlxsw_sp_vport_flood_set(struct mlxsw_sp_port *mlxsw_sp_vport, u16 vfid,
256 bool set, bool only_uc)
258 /* In case of vFIDs, index into the flooding table is relative to
259 * the start of the vFIDs range.
261 return __mlxsw_sp_port_flood_set(mlxsw_sp_vport, vfid, vfid, set,
265 static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
266 struct switchdev_trans *trans,
267 unsigned long brport_flags)
269 unsigned long uc_flood = mlxsw_sp_port->uc_flood ? BR_FLOOD : 0;
273 if (!mlxsw_sp_port->bridged)
276 if (switchdev_trans_ph_prepare(trans))
279 if ((uc_flood ^ brport_flags) & BR_FLOOD) {
280 set = mlxsw_sp_port->uc_flood ? false : true;
281 err = mlxsw_sp_port_uc_flood_set(mlxsw_sp_port, set);
286 mlxsw_sp_port->uc_flood = brport_flags & BR_FLOOD ? 1 : 0;
287 mlxsw_sp_port->learning = brport_flags & BR_LEARNING ? 1 : 0;
288 mlxsw_sp_port->learning_sync = brport_flags & BR_LEARNING_SYNC ? 1 : 0;
293 static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
295 char sfdat_pl[MLXSW_REG_SFDAT_LEN];
298 mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
299 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
302 mlxsw_sp->ageing_time = ageing_time;
306 static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
307 struct switchdev_trans *trans,
308 unsigned long ageing_clock_t)
310 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
311 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
312 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
314 if (switchdev_trans_ph_prepare(trans)) {
315 if (ageing_time < MLXSW_SP_MIN_AGEING_TIME ||
316 ageing_time > MLXSW_SP_MAX_AGEING_TIME)
322 return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
325 static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
326 struct switchdev_trans *trans,
327 struct net_device *orig_dev,
330 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
332 /* SWITCHDEV_TRANS_PREPARE phase */
333 if ((!vlan_enabled) && (mlxsw_sp->master_bridge.dev == orig_dev)) {
334 netdev_err(mlxsw_sp_port->dev, "Bridge must be vlan-aware\n");
341 static int mlxsw_sp_port_attr_set(struct net_device *dev,
342 const struct switchdev_attr *attr,
343 struct switchdev_trans *trans)
345 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
348 mlxsw_sp_port = mlxsw_sp_port_orig_get(attr->orig_dev, mlxsw_sp_port);
353 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
354 err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port, trans,
357 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
358 err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port, trans,
359 attr->u.brport_flags);
361 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
362 err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port, trans,
363 attr->u.ageing_time);
365 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
366 err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port, trans,
368 attr->u.vlan_filtering);
378 static int __mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port,
381 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
382 char spvid_pl[MLXSW_REG_SPVID_LEN];
384 mlxsw_reg_spvid_pack(spvid_pl, mlxsw_sp_port->local_port, vid);
385 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvid), spvid_pl);
388 static int mlxsw_sp_port_allow_untagged_set(struct mlxsw_sp_port *mlxsw_sp_port,
391 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
392 char spaft_pl[MLXSW_REG_SPAFT_LEN];
394 mlxsw_reg_spaft_pack(spaft_pl, mlxsw_sp_port->local_port, allow);
395 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spaft), spaft_pl);
398 int mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
400 struct net_device *dev = mlxsw_sp_port->dev;
404 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port, false);
406 netdev_err(dev, "Failed to disallow untagged traffic\n");
410 err = __mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid);
412 netdev_err(dev, "Failed to set PVID\n");
416 /* Only allow if not already allowed. */
417 if (!mlxsw_sp_port->pvid) {
418 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port,
421 netdev_err(dev, "Failed to allow untagged traffic\n");
422 goto err_port_allow_untagged_set;
427 mlxsw_sp_port->pvid = vid;
430 err_port_allow_untagged_set:
431 __mlxsw_sp_port_pvid_set(mlxsw_sp_port, mlxsw_sp_port->pvid);
435 static int mlxsw_sp_fid_create(struct mlxsw_sp *mlxsw_sp, u16 fid)
437 char sfmr_pl[MLXSW_REG_SFMR_LEN];
440 mlxsw_reg_sfmr_pack(sfmr_pl, MLXSW_REG_SFMR_OP_CREATE_FID, fid, fid);
441 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfmr), sfmr_pl);
446 set_bit(fid, mlxsw_sp->active_fids);
450 static void mlxsw_sp_fid_destroy(struct mlxsw_sp *mlxsw_sp, u16 fid)
452 char sfmr_pl[MLXSW_REG_SFMR_LEN];
454 clear_bit(fid, mlxsw_sp->active_fids);
456 mlxsw_reg_sfmr_pack(sfmr_pl, MLXSW_REG_SFMR_OP_DESTROY_FID,
458 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfmr), sfmr_pl);
461 static int mlxsw_sp_port_fid_map(struct mlxsw_sp_port *mlxsw_sp_port, u16 fid)
463 enum mlxsw_reg_svfa_mt mt;
465 if (!list_empty(&mlxsw_sp_port->vports_list))
466 mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID;
468 mt = MLXSW_REG_SVFA_MT_VID_TO_FID;
470 return mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, true, fid, fid);
473 static int mlxsw_sp_port_fid_unmap(struct mlxsw_sp_port *mlxsw_sp_port, u16 fid)
475 enum mlxsw_reg_svfa_mt mt;
477 if (list_empty(&mlxsw_sp_port->vports_list))
480 mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID;
481 return mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, false, fid, fid);
484 static int mlxsw_sp_port_add_vids(struct net_device *dev, u16 vid_begin,
490 for (vid = vid_begin; vid <= vid_end; vid++) {
491 err = mlxsw_sp_port_add_vid(dev, 0, vid);
493 goto err_port_add_vid;
498 for (vid--; vid >= vid_begin; vid--)
499 mlxsw_sp_port_kill_vid(dev, 0, vid);
503 static int __mlxsw_sp_port_vlans_set(struct mlxsw_sp_port *mlxsw_sp_port,
504 u16 vid_begin, u16 vid_end, bool is_member,
510 for (vid = vid_begin; vid <= vid_end;
511 vid += MLXSW_REG_SPVM_REC_MAX_COUNT) {
512 vid_e = min((u16) (vid + MLXSW_REG_SPVM_REC_MAX_COUNT - 1),
515 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid_e,
516 is_member, untagged);
524 static int __mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
525 u16 vid_begin, u16 vid_end,
526 bool flag_untagged, bool flag_pvid)
528 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
529 struct net_device *dev = mlxsw_sp_port->dev;
530 u16 vid, last_visited_vid, old_pvid;
531 enum mlxsw_reg_svfa_mt mt;
534 /* In case this is invoked with BRIDGE_FLAGS_SELF and port is
535 * not bridged, then packets ingressing through the port with
536 * the specified VIDs will be directed to CPU.
538 if (!mlxsw_sp_port->bridged)
539 return mlxsw_sp_port_add_vids(dev, vid_begin, vid_end);
541 for (vid = vid_begin; vid <= vid_end; vid++) {
542 if (!test_bit(vid, mlxsw_sp->active_fids)) {
543 err = mlxsw_sp_fid_create(mlxsw_sp, vid);
545 netdev_err(dev, "Failed to create FID=%d\n",
550 /* When creating a FID, we set a VID to FID mapping
551 * regardless of the port's mode.
553 mt = MLXSW_REG_SVFA_MT_VID_TO_FID;
554 err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt,
557 netdev_err(dev, "Failed to create FID=VID=%d mapping\n",
559 goto err_port_vid_to_fid_set;
564 /* Set FID mapping according to port's mode */
565 for (vid = vid_begin; vid <= vid_end; vid++) {
566 err = mlxsw_sp_port_fid_map(mlxsw_sp_port, vid);
568 netdev_err(dev, "Failed to map FID=%d", vid);
569 last_visited_vid = --vid;
570 goto err_port_fid_map;
574 err = __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid_begin, vid_end,
577 netdev_err(dev, "Failed to configure flooding\n");
578 goto err_port_flood_set;
581 err = __mlxsw_sp_port_vlans_set(mlxsw_sp_port, vid_begin, vid_end,
582 true, flag_untagged);
584 netdev_err(dev, "Unable to add VIDs %d-%d\n", vid_begin,
586 goto err_port_vlans_set;
589 old_pvid = mlxsw_sp_port->pvid;
590 if (flag_pvid && old_pvid != vid_begin) {
591 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid_begin);
593 netdev_err(dev, "Unable to add PVID %d\n", vid_begin);
594 goto err_port_pvid_set;
596 } else if (!flag_pvid && old_pvid >= vid_begin && old_pvid <= vid_end) {
597 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, 0);
599 netdev_err(dev, "Unable to del PVID\n");
600 goto err_port_pvid_set;
604 /* Changing activity bits only if HW operation succeded */
605 for (vid = vid_begin; vid <= vid_end; vid++) {
606 set_bit(vid, mlxsw_sp_port->active_vlans);
608 set_bit(vid, mlxsw_sp_port->untagged_vlans);
610 clear_bit(vid, mlxsw_sp_port->untagged_vlans);
613 /* STP state change must be done after we set active VLANs */
614 err = mlxsw_sp_port_stp_state_set(mlxsw_sp_port,
615 mlxsw_sp_port->stp_state);
617 netdev_err(dev, "Failed to set STP state\n");
618 goto err_port_stp_state_set;
623 err_port_vid_to_fid_set:
624 mlxsw_sp_fid_destroy(mlxsw_sp, vid);
627 err_port_stp_state_set:
628 for (vid = vid_begin; vid <= vid_end; vid++)
629 clear_bit(vid, mlxsw_sp_port->active_vlans);
630 if (old_pvid != mlxsw_sp_port->pvid)
631 mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid);
633 __mlxsw_sp_port_vlans_set(mlxsw_sp_port, vid_begin, vid_end, false,
636 __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid_begin, vid_end, false,
639 last_visited_vid = vid_end;
641 for (vid = last_visited_vid; vid >= vid_begin; vid--)
642 mlxsw_sp_port_fid_unmap(mlxsw_sp_port, vid);
646 static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
647 const struct switchdev_obj_port_vlan *vlan,
648 struct switchdev_trans *trans)
650 bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
651 bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
653 if (switchdev_trans_ph_prepare(trans))
656 return __mlxsw_sp_port_vlans_add(mlxsw_sp_port,
657 vlan->vid_begin, vlan->vid_end,
658 flag_untagged, flag_pvid);
661 static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
663 return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
664 MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY;
667 static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
669 return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
670 MLXSW_REG_SFD_OP_WRITE_REMOVE;
673 static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
674 const char *mac, u16 fid, bool adding,
680 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
684 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
685 mlxsw_reg_sfd_uc_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
686 mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
688 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
694 static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
695 const char *mac, u16 fid, u16 lag_vid,
696 bool adding, bool dynamic)
701 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
705 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
706 mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
707 mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
709 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
716 mlxsw_sp_port_fdb_static_add(struct mlxsw_sp_port *mlxsw_sp_port,
717 const struct switchdev_obj_port_fdb *fdb,
718 struct switchdev_trans *trans)
720 u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, fdb->vid);
723 if (switchdev_trans_ph_prepare(trans))
726 if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
727 lag_vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
730 if (!mlxsw_sp_port->lagged)
731 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp_port->mlxsw_sp,
732 mlxsw_sp_port->local_port,
733 fdb->addr, fid, true, false);
735 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp_port->mlxsw_sp,
736 mlxsw_sp_port->lag_id,
737 fdb->addr, fid, lag_vid,
741 static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
742 u16 fid, u16 mid, bool adding)
747 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
751 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
752 mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
753 MLXSW_REG_SFD_REC_ACTION_NOP, mid);
754 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
759 static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 mid,
760 bool add, bool clear_all_ports)
762 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
766 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
770 mlxsw_reg_smid_pack(smid_pl, mid, mlxsw_sp_port->local_port, add);
771 if (clear_all_ports) {
772 for (i = 1; i < MLXSW_PORT_MAX_PORTS; i++)
773 if (mlxsw_sp->ports[i])
774 mlxsw_reg_smid_port_mask_set(smid_pl, i, 1);
776 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
781 static struct mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp *mlxsw_sp,
782 const unsigned char *addr,
785 struct mlxsw_sp_mid *mid;
787 list_for_each_entry(mid, &mlxsw_sp->br_mids.list, list) {
788 if (ether_addr_equal(mid->addr, addr) && mid->vid == vid)
794 static struct mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
795 const unsigned char *addr,
798 struct mlxsw_sp_mid *mid;
801 mid_idx = find_first_zero_bit(mlxsw_sp->br_mids.mapped,
803 if (mid_idx == MLXSW_SP_MID_MAX)
806 mid = kzalloc(sizeof(*mid), GFP_KERNEL);
810 set_bit(mid_idx, mlxsw_sp->br_mids.mapped);
811 ether_addr_copy(mid->addr, addr);
815 list_add_tail(&mid->list, &mlxsw_sp->br_mids.list);
820 static int __mlxsw_sp_mc_dec_ref(struct mlxsw_sp *mlxsw_sp,
821 struct mlxsw_sp_mid *mid)
823 if (--mid->ref_count == 0) {
824 list_del(&mid->list);
825 clear_bit(mid->mid, mlxsw_sp->br_mids.mapped);
832 static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
833 const struct switchdev_obj_port_mdb *mdb,
834 struct switchdev_trans *trans)
836 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
837 struct net_device *dev = mlxsw_sp_port->dev;
838 struct mlxsw_sp_mid *mid;
839 u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, mdb->vid);
842 if (switchdev_trans_ph_prepare(trans))
845 mid = __mlxsw_sp_mc_get(mlxsw_sp, mdb->addr, mdb->vid);
847 mid = __mlxsw_sp_mc_alloc(mlxsw_sp, mdb->addr, mdb->vid);
849 netdev_err(dev, "Unable to allocate MC group\n");
855 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true,
856 mid->ref_count == 1);
858 netdev_err(dev, "Unable to set SMID\n");
862 if (mid->ref_count == 1) {
863 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mdb->addr, fid, mid->mid,
866 netdev_err(dev, "Unable to set MC SFD\n");
874 __mlxsw_sp_mc_dec_ref(mlxsw_sp, mid);
878 static int mlxsw_sp_port_obj_add(struct net_device *dev,
879 const struct switchdev_obj *obj,
880 struct switchdev_trans *trans)
882 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
885 mlxsw_sp_port = mlxsw_sp_port_orig_get(obj->orig_dev, mlxsw_sp_port);
890 case SWITCHDEV_OBJ_ID_PORT_VLAN:
891 if (mlxsw_sp_port_is_vport(mlxsw_sp_port))
894 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port,
895 SWITCHDEV_OBJ_PORT_VLAN(obj),
898 case SWITCHDEV_OBJ_ID_PORT_FDB:
899 err = mlxsw_sp_port_fdb_static_add(mlxsw_sp_port,
900 SWITCHDEV_OBJ_PORT_FDB(obj),
903 case SWITCHDEV_OBJ_ID_PORT_MDB:
904 err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
905 SWITCHDEV_OBJ_PORT_MDB(obj),
916 static int mlxsw_sp_port_kill_vids(struct net_device *dev, u16 vid_begin,
922 for (vid = vid_begin; vid <= vid_end; vid++) {
923 err = mlxsw_sp_port_kill_vid(dev, 0, vid);
931 static int __mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
932 u16 vid_begin, u16 vid_end, bool init)
934 struct net_device *dev = mlxsw_sp_port->dev;
938 /* In case this is invoked with BRIDGE_FLAGS_SELF and port is
939 * not bridged, then prevent packets ingressing through the
940 * port with the specified VIDs from being trapped to CPU.
942 if (!init && !mlxsw_sp_port->bridged)
943 return mlxsw_sp_port_kill_vids(dev, vid_begin, vid_end);
945 err = __mlxsw_sp_port_vlans_set(mlxsw_sp_port, vid_begin, vid_end,
948 netdev_err(dev, "Unable to del VIDs %d-%d\n", vid_begin,
956 pvid = mlxsw_sp_port->pvid;
957 if (pvid >= vid_begin && pvid <= vid_end) {
958 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, 0);
960 netdev_err(dev, "Unable to del PVID %d\n", pvid);
965 err = __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid_begin, vid_end,
968 netdev_err(dev, "Failed to clear flooding\n");
972 for (vid = vid_begin; vid <= vid_end; vid++) {
973 /* Remove FID mapping in case of Virtual mode */
974 err = mlxsw_sp_port_fid_unmap(mlxsw_sp_port, vid);
976 netdev_err(dev, "Failed to unmap FID=%d", vid);
982 /* Changing activity bits only if HW operation succeded */
983 for (vid = vid_begin; vid <= vid_end; vid++)
984 clear_bit(vid, mlxsw_sp_port->active_vlans);
989 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
990 const struct switchdev_obj_port_vlan *vlan)
992 return __mlxsw_sp_port_vlans_del(mlxsw_sp_port,
993 vlan->vid_begin, vlan->vid_end, false);
996 void mlxsw_sp_port_active_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port)
1000 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID)
1001 __mlxsw_sp_port_vlans_del(mlxsw_sp_port, vid, vid, false);
1005 mlxsw_sp_port_fdb_static_del(struct mlxsw_sp_port *mlxsw_sp_port,
1006 const struct switchdev_obj_port_fdb *fdb)
1008 u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, fdb->vid);
1011 if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
1012 lag_vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
1015 if (!mlxsw_sp_port->lagged)
1016 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp_port->mlxsw_sp,
1017 mlxsw_sp_port->local_port,
1021 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp_port->mlxsw_sp,
1022 mlxsw_sp_port->lag_id,
1023 fdb->addr, fid, lag_vid,
1027 static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1028 const struct switchdev_obj_port_mdb *mdb)
1030 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1031 struct net_device *dev = mlxsw_sp_port->dev;
1032 struct mlxsw_sp_mid *mid;
1033 u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, mdb->vid);
1037 mid = __mlxsw_sp_mc_get(mlxsw_sp, mdb->addr, mdb->vid);
1039 netdev_err(dev, "Unable to remove port from MC DB\n");
1043 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false, false);
1045 netdev_err(dev, "Unable to remove port from SMID\n");
1048 if (__mlxsw_sp_mc_dec_ref(mlxsw_sp, mid)) {
1049 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mdb->addr, fid, mid_idx,
1052 netdev_err(dev, "Unable to remove MC SFD\n");
1058 static int mlxsw_sp_port_obj_del(struct net_device *dev,
1059 const struct switchdev_obj *obj)
1061 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1064 mlxsw_sp_port = mlxsw_sp_port_orig_get(obj->orig_dev, mlxsw_sp_port);
1069 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1070 if (mlxsw_sp_port_is_vport(mlxsw_sp_port))
1073 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1074 SWITCHDEV_OBJ_PORT_VLAN(obj));
1076 case SWITCHDEV_OBJ_ID_PORT_FDB:
1077 err = mlxsw_sp_port_fdb_static_del(mlxsw_sp_port,
1078 SWITCHDEV_OBJ_PORT_FDB(obj));
1080 case SWITCHDEV_OBJ_ID_PORT_MDB:
1081 err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1082 SWITCHDEV_OBJ_PORT_MDB(obj));
1092 static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
1095 struct mlxsw_sp_port *mlxsw_sp_port;
1098 for (i = 0; i < MLXSW_SP_PORT_PER_LAG_MAX; i++) {
1099 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
1101 return mlxsw_sp_port;
1106 static int mlxsw_sp_port_fdb_dump(struct mlxsw_sp_port *mlxsw_sp_port,
1107 struct switchdev_obj_port_fdb *fdb,
1108 switchdev_obj_dump_cb_t *cb,
1109 struct net_device *orig_dev)
1111 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1112 struct mlxsw_sp_port *tmp;
1124 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1128 if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
1131 tmp = mlxsw_sp_vport_vfid_get(mlxsw_sp_port);
1132 vport_fid = mlxsw_sp_vfid_to_fid(tmp);
1135 mlxsw_reg_sfd_pack(sfd_pl, MLXSW_REG_SFD_OP_QUERY_DUMP, 0);
1137 mlxsw_reg_sfd_num_rec_set(sfd_pl, MLXSW_REG_SFD_REC_MAX_COUNT);
1138 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
1142 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1144 /* Even in case of error, we have to run the dump to the end
1145 * so the session in firmware is finished.
1150 for (i = 0; i < num_rec; i++) {
1151 switch (mlxsw_reg_sfd_rec_type_get(sfd_pl, i)) {
1152 case MLXSW_REG_SFD_REC_TYPE_UNICAST:
1153 mlxsw_reg_sfd_uc_unpack(sfd_pl, i, mac, &fid,
1155 if (local_port == mlxsw_sp_port->local_port) {
1156 if (vport_fid && vport_fid == fid)
1158 else if (!vport_fid &&
1159 !mlxsw_sp_fid_is_vfid(fid))
1163 ether_addr_copy(fdb->addr, mac);
1164 fdb->ndm_state = NUD_REACHABLE;
1165 err = cb(&fdb->obj);
1170 case MLXSW_REG_SFD_REC_TYPE_UNICAST_LAG:
1171 mlxsw_reg_sfd_uc_lag_unpack(sfd_pl, i,
1172 mac, &fid, &lag_id);
1173 tmp = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
1174 if (tmp && tmp->local_port ==
1175 mlxsw_sp_port->local_port) {
1176 /* LAG records can only point to LAG
1177 * devices or VLAN devices on top.
1179 if (!netif_is_lag_master(orig_dev) &&
1180 !is_vlan_dev(orig_dev))
1182 if (vport_fid && vport_fid == fid)
1184 else if (!vport_fid &&
1185 !mlxsw_sp_fid_is_vfid(fid))
1189 ether_addr_copy(fdb->addr, mac);
1190 fdb->ndm_state = NUD_REACHABLE;
1191 err = cb(&fdb->obj);
1198 } while (num_rec == MLXSW_REG_SFD_REC_MAX_COUNT);
1202 return stored_err ? stored_err : err;
1205 static int mlxsw_sp_port_vlan_dump(struct mlxsw_sp_port *mlxsw_sp_port,
1206 struct switchdev_obj_port_vlan *vlan,
1207 switchdev_obj_dump_cb_t *cb)
1212 if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
1214 vlan->vid_begin = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
1215 vlan->vid_end = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
1216 return cb(&vlan->obj);
1219 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
1221 if (vid == mlxsw_sp_port->pvid)
1222 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1223 if (test_bit(vid, mlxsw_sp_port->untagged_vlans))
1224 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1225 vlan->vid_begin = vid;
1226 vlan->vid_end = vid;
1227 err = cb(&vlan->obj);
1234 static int mlxsw_sp_port_obj_dump(struct net_device *dev,
1235 struct switchdev_obj *obj,
1236 switchdev_obj_dump_cb_t *cb)
1238 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1241 mlxsw_sp_port = mlxsw_sp_port_orig_get(obj->orig_dev, mlxsw_sp_port);
1246 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1247 err = mlxsw_sp_port_vlan_dump(mlxsw_sp_port,
1248 SWITCHDEV_OBJ_PORT_VLAN(obj), cb);
1250 case SWITCHDEV_OBJ_ID_PORT_FDB:
1251 err = mlxsw_sp_port_fdb_dump(mlxsw_sp_port,
1252 SWITCHDEV_OBJ_PORT_FDB(obj), cb,
1263 static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = {
1264 .switchdev_port_attr_get = mlxsw_sp_port_attr_get,
1265 .switchdev_port_attr_set = mlxsw_sp_port_attr_set,
1266 .switchdev_port_obj_add = mlxsw_sp_port_obj_add,
1267 .switchdev_port_obj_del = mlxsw_sp_port_obj_del,
1268 .switchdev_port_obj_dump = mlxsw_sp_port_obj_dump,
1271 static void mlxsw_sp_fdb_call_notifiers(bool learning_sync, bool adding,
1273 struct net_device *dev)
1275 struct switchdev_notifier_fdb_info info;
1276 unsigned long notifier_type;
1278 if (learning_sync) {
1281 notifier_type = adding ? SWITCHDEV_FDB_ADD : SWITCHDEV_FDB_DEL;
1282 call_switchdev_notifiers(notifier_type, dev, &info.info);
1286 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
1287 char *sfn_pl, int rec_index,
1290 struct mlxsw_sp_port *mlxsw_sp_port;
1294 bool do_notification = true;
1297 mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
1298 mlxsw_sp_port = mlxsw_sp->ports[local_port];
1299 if (!mlxsw_sp_port) {
1300 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
1304 if (mlxsw_sp_fid_is_vfid(fid)) {
1305 u16 vfid = mlxsw_sp_fid_to_vfid(fid);
1306 struct mlxsw_sp_port *mlxsw_sp_vport;
1308 mlxsw_sp_vport = mlxsw_sp_port_vport_find_by_vfid(mlxsw_sp_port,
1310 if (!mlxsw_sp_vport) {
1311 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching vPort following FDB notification\n");
1315 /* Override the physical port with the vPort. */
1316 mlxsw_sp_port = mlxsw_sp_vport;
1321 adding = adding && mlxsw_sp_port->learning;
1324 err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
1327 if (net_ratelimit())
1328 netdev_err(mlxsw_sp_port->dev, "Failed to set FDB entry\n");
1332 if (!do_notification)
1334 mlxsw_sp_fdb_call_notifiers(mlxsw_sp_port->learning_sync,
1335 adding, mac, vid, mlxsw_sp_port->dev);
1340 do_notification = false;
1344 static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
1345 char *sfn_pl, int rec_index,
1348 struct mlxsw_sp_port *mlxsw_sp_port;
1349 struct net_device *dev;
1354 bool do_notification = true;
1357 mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
1358 mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
1359 if (!mlxsw_sp_port) {
1360 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
1364 if (mlxsw_sp_fid_is_vfid(fid)) {
1365 u16 vfid = mlxsw_sp_fid_to_vfid(fid);
1366 struct mlxsw_sp_port *mlxsw_sp_vport;
1368 mlxsw_sp_vport = mlxsw_sp_port_vport_find_by_vfid(mlxsw_sp_port,
1370 if (!mlxsw_sp_vport) {
1371 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching vPort following FDB notification\n");
1375 lag_vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport);
1376 dev = mlxsw_sp_vport->dev;
1378 /* Override the physical port with the vPort. */
1379 mlxsw_sp_port = mlxsw_sp_vport;
1381 dev = mlxsw_sp_lag_get(mlxsw_sp, lag_id)->dev;
1385 adding = adding && mlxsw_sp_port->learning;
1388 err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
1391 if (net_ratelimit())
1392 netdev_err(mlxsw_sp_port->dev, "Failed to set FDB entry\n");
1396 if (!do_notification)
1398 mlxsw_sp_fdb_call_notifiers(mlxsw_sp_port->learning_sync, adding, mac,
1404 do_notification = false;
1408 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
1409 char *sfn_pl, int rec_index)
1411 switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
1412 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
1413 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
1416 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
1417 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
1420 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
1421 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
1424 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
1425 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
1431 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp)
1433 schedule_delayed_work(&mlxsw_sp->fdb_notify.dw,
1434 msecs_to_jiffies(mlxsw_sp->fdb_notify.interval));
1437 static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
1439 struct mlxsw_sp *mlxsw_sp;
1445 sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
1449 mlxsw_sp = container_of(work, struct mlxsw_sp, fdb_notify.dw.work);
1453 mlxsw_reg_sfn_pack(sfn_pl);
1454 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
1456 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
1459 num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
1460 for (i = 0; i < num_rec; i++)
1461 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
1467 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
1470 static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
1474 err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
1476 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
1479 INIT_DELAYED_WORK(&mlxsw_sp->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
1480 mlxsw_sp->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
1481 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
1485 static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
1487 cancel_delayed_work_sync(&mlxsw_sp->fdb_notify.dw);
1490 static void mlxsw_sp_fids_fini(struct mlxsw_sp *mlxsw_sp)
1494 for_each_set_bit(fid, mlxsw_sp->active_fids, VLAN_N_VID)
1495 mlxsw_sp_fid_destroy(mlxsw_sp, fid);
1498 int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
1500 return mlxsw_sp_fdb_init(mlxsw_sp);
1503 void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
1505 mlxsw_sp_fdb_fini(mlxsw_sp);
1506 mlxsw_sp_fids_fini(mlxsw_sp);
1509 int mlxsw_sp_port_vlan_init(struct mlxsw_sp_port *mlxsw_sp_port)
1511 struct net_device *dev = mlxsw_sp_port->dev;
1514 /* Allow only untagged packets to ingress and tag them internally
1517 mlxsw_sp_port->pvid = 1;
1518 err = __mlxsw_sp_port_vlans_del(mlxsw_sp_port, 0, VLAN_N_VID - 1,
1521 netdev_err(dev, "Unable to init VLANs\n");
1525 /* Add implicit VLAN interface in the device, so that untagged
1526 * packets will be classified to the default vFID.
1528 err = mlxsw_sp_port_add_vid(dev, 0, 1);
1530 netdev_err(dev, "Failed to configure default vFID\n");
1535 void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port *mlxsw_sp_port)
1537 mlxsw_sp_port->dev->switchdev_ops = &mlxsw_sp_port_switchdev_ops;
1540 void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port)