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 int mlxsw_sp_port_attr_get(struct net_device *dev,
56 struct switchdev_attr *attr)
58 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
59 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
62 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
63 attr->u.ppid.id_len = sizeof(mlxsw_sp->base_mac);
64 memcpy(&attr->u.ppid.id, &mlxsw_sp->base_mac,
67 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
68 attr->u.brport_flags =
69 (mlxsw_sp_port->learning ? BR_LEARNING : 0) |
70 (mlxsw_sp_port->learning_sync ? BR_LEARNING_SYNC : 0) |
71 (mlxsw_sp_port->uc_flood ? BR_FLOOD : 0);
80 static int mlxsw_sp_port_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
83 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
84 enum mlxsw_reg_spms_state spms_state;
90 case BR_STATE_DISABLED: /* fall-through */
91 case BR_STATE_FORWARDING:
92 spms_state = MLXSW_REG_SPMS_STATE_FORWARDING;
94 case BR_STATE_LISTENING: /* fall-through */
95 case BR_STATE_LEARNING:
96 spms_state = MLXSW_REG_SPMS_STATE_LEARNING;
98 case BR_STATE_BLOCKING:
99 spms_state = MLXSW_REG_SPMS_STATE_DISCARDING;
105 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
108 mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
109 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID)
110 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
112 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
117 static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
118 struct switchdev_trans *trans,
121 if (switchdev_trans_ph_prepare(trans))
124 mlxsw_sp_port->stp_state = state;
125 return mlxsw_sp_port_stp_state_set(mlxsw_sp_port, state);
128 static int __mlxsw_sp_port_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
129 u16 fid_begin, u16 fid_end, bool set,
132 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
133 u16 range = fid_end - fid_begin + 1;
137 sftr_pl = kmalloc(MLXSW_REG_SFTR_LEN, GFP_KERNEL);
141 mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_UC, fid_begin,
142 MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST, range,
143 mlxsw_sp_port->local_port, set);
144 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl);
148 /* Flooding control allows one to decide whether a given port will
149 * flood unicast traffic for which there is no FDB entry.
154 mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_BM, fid_begin,
155 MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST, range,
156 mlxsw_sp_port->local_port, set);
157 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl);
164 static int mlxsw_sp_port_uc_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
167 struct net_device *dev = mlxsw_sp_port->dev;
168 u16 vid, last_visited_vid;
171 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
172 err = __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid, vid, set,
175 last_visited_vid = vid;
176 goto err_port_flood_set;
183 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, last_visited_vid)
184 __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid, vid, !set, true);
185 netdev_err(dev, "Failed to configure unicast flooding\n");
189 static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
190 struct switchdev_trans *trans,
191 unsigned long brport_flags)
193 unsigned long uc_flood = mlxsw_sp_port->uc_flood ? BR_FLOOD : 0;
197 if (switchdev_trans_ph_prepare(trans))
200 if ((uc_flood ^ brport_flags) & BR_FLOOD) {
201 set = mlxsw_sp_port->uc_flood ? false : true;
202 err = mlxsw_sp_port_uc_flood_set(mlxsw_sp_port, set);
207 mlxsw_sp_port->uc_flood = brport_flags & BR_FLOOD ? 1 : 0;
208 mlxsw_sp_port->learning = brport_flags & BR_LEARNING ? 1 : 0;
209 mlxsw_sp_port->learning_sync = brport_flags & BR_LEARNING_SYNC ? 1 : 0;
214 static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
216 char sfdat_pl[MLXSW_REG_SFDAT_LEN];
219 mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
220 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
223 mlxsw_sp->ageing_time = ageing_time;
227 static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
228 struct switchdev_trans *trans,
229 unsigned long ageing_clock_t)
231 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
232 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
233 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
235 if (switchdev_trans_ph_prepare(trans))
238 return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
241 static int mlxsw_sp_port_attr_set(struct net_device *dev,
242 const struct switchdev_attr *attr,
243 struct switchdev_trans *trans)
245 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
249 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
250 err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port, trans,
253 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
254 err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port, trans,
255 attr->u.brport_flags);
257 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
258 err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port, trans,
259 attr->u.ageing_time);
269 static int mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
271 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
272 char spvid_pl[MLXSW_REG_SPVID_LEN];
274 mlxsw_reg_spvid_pack(spvid_pl, mlxsw_sp_port->local_port, vid);
275 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvid), spvid_pl);
278 static int mlxsw_sp_fid_create(struct mlxsw_sp *mlxsw_sp, u16 fid)
280 char sfmr_pl[MLXSW_REG_SFMR_LEN];
283 mlxsw_reg_sfmr_pack(sfmr_pl, MLXSW_REG_SFMR_OP_CREATE_FID, fid, fid);
284 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfmr), sfmr_pl);
289 set_bit(fid, mlxsw_sp->active_fids);
293 static void mlxsw_sp_fid_destroy(struct mlxsw_sp *mlxsw_sp, u16 fid)
295 char sfmr_pl[MLXSW_REG_SFMR_LEN];
297 clear_bit(fid, mlxsw_sp->active_fids);
299 mlxsw_reg_sfmr_pack(sfmr_pl, MLXSW_REG_SFMR_OP_DESTROY_FID,
301 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfmr), sfmr_pl);
304 static int mlxsw_sp_port_fid_map(struct mlxsw_sp_port *mlxsw_sp_port, u16 fid)
306 enum mlxsw_reg_svfa_mt mt;
308 if (mlxsw_sp_port->nr_vfids)
309 mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID;
311 mt = MLXSW_REG_SVFA_MT_VID_TO_FID;
313 return mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, true, fid, fid);
316 static int mlxsw_sp_port_fid_unmap(struct mlxsw_sp_port *mlxsw_sp_port, u16 fid)
318 enum mlxsw_reg_svfa_mt mt;
320 if (!mlxsw_sp_port->nr_vfids)
323 mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID;
324 return mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, false, fid, fid);
327 static int mlxsw_sp_port_add_vids(struct net_device *dev, u16 vid_begin,
333 for (vid = vid_begin; vid <= vid_end; vid++) {
334 err = mlxsw_sp_port_add_vid(dev, 0, vid);
336 goto err_port_add_vid;
341 for (vid--; vid >= vid_begin; vid--)
342 mlxsw_sp_port_kill_vid(dev, 0, vid);
346 static int __mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
347 u16 vid_begin, u16 vid_end,
348 bool flag_untagged, bool flag_pvid)
350 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
351 struct net_device *dev = mlxsw_sp_port->dev;
352 enum mlxsw_reg_svfa_mt mt;
356 /* In case this is invoked with BRIDGE_FLAGS_SELF and port is
357 * not bridged, then packets ingressing through the port with
358 * the specified VIDs will be directed to CPU.
360 if (!mlxsw_sp_port->bridged)
361 return mlxsw_sp_port_add_vids(dev, vid_begin, vid_end);
363 for (vid = vid_begin; vid <= vid_end; vid++) {
364 if (!test_bit(vid, mlxsw_sp->active_fids)) {
365 err = mlxsw_sp_fid_create(mlxsw_sp, vid);
367 netdev_err(dev, "Failed to create FID=%d\n",
372 /* When creating a FID, we set a VID to FID mapping
373 * regardless of the port's mode.
375 mt = MLXSW_REG_SVFA_MT_VID_TO_FID;
376 err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt,
379 netdev_err(dev, "Failed to create FID=VID=%d mapping\n",
385 /* Set FID mapping according to port's mode */
386 err = mlxsw_sp_port_fid_map(mlxsw_sp_port, vid);
388 netdev_err(dev, "Failed to map FID=%d", vid);
393 err = __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid_begin, vid_end,
396 netdev_err(dev, "Failed to configure flooding\n");
400 for (vid = vid_begin; vid <= vid_end;
401 vid += MLXSW_REG_SPVM_REC_MAX_COUNT) {
402 vid_e = min((u16) (vid + MLXSW_REG_SPVM_REC_MAX_COUNT - 1),
405 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid_e, true,
408 netdev_err(mlxsw_sp_port->dev, "Unable to add VIDs %d-%d\n",
415 if (flag_pvid && mlxsw_sp_port->pvid != vid) {
416 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid);
418 netdev_err(mlxsw_sp_port->dev, "Unable to add PVID %d\n",
422 mlxsw_sp_port->pvid = vid;
425 /* Changing activity bits only if HW operation succeded */
426 for (vid = vid_begin; vid <= vid_end; vid++)
427 set_bit(vid, mlxsw_sp_port->active_vlans);
429 return mlxsw_sp_port_stp_state_set(mlxsw_sp_port,
430 mlxsw_sp_port->stp_state);
433 static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
434 const struct switchdev_obj_port_vlan *vlan,
435 struct switchdev_trans *trans)
437 bool untagged_flag = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
438 bool pvid_flag = vlan->flags & BRIDGE_VLAN_INFO_PVID;
440 if (switchdev_trans_ph_prepare(trans))
443 return __mlxsw_sp_port_vlans_add(mlxsw_sp_port,
444 vlan->vid_begin, vlan->vid_end,
445 untagged_flag, pvid_flag);
448 static int mlxsw_sp_port_fdb_op(struct mlxsw_sp_port *mlxsw_sp_port,
449 const char *mac, u16 vid, bool adding,
452 enum mlxsw_reg_sfd_rec_policy policy;
453 enum mlxsw_reg_sfd_op op;
458 vid = mlxsw_sp_port->pvid;
460 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
464 policy = dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
465 MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY;
466 op = adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
467 MLXSW_REG_SFD_OP_WRITE_REMOVE;
468 mlxsw_reg_sfd_pack(sfd_pl, op, 0);
469 mlxsw_reg_sfd_uc_pack(sfd_pl, 0, policy,
470 mac, vid, MLXSW_REG_SFD_REC_ACTION_NOP,
471 mlxsw_sp_port->local_port);
472 err = mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(sfd),
480 mlxsw_sp_port_fdb_static_add(struct mlxsw_sp_port *mlxsw_sp_port,
481 const struct switchdev_obj_port_fdb *fdb,
482 struct switchdev_trans *trans)
484 if (switchdev_trans_ph_prepare(trans))
487 return mlxsw_sp_port_fdb_op(mlxsw_sp_port, fdb->addr, fdb->vid,
491 static int mlxsw_sp_port_obj_add(struct net_device *dev,
492 const struct switchdev_obj *obj,
493 struct switchdev_trans *trans)
495 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
499 case SWITCHDEV_OBJ_ID_PORT_VLAN:
500 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port,
501 SWITCHDEV_OBJ_PORT_VLAN(obj),
504 case SWITCHDEV_OBJ_ID_PORT_FDB:
505 err = mlxsw_sp_port_fdb_static_add(mlxsw_sp_port,
506 SWITCHDEV_OBJ_PORT_FDB(obj),
517 static int mlxsw_sp_port_kill_vids(struct net_device *dev, u16 vid_begin,
523 for (vid = vid_begin; vid <= vid_end; vid++) {
524 err = mlxsw_sp_port_kill_vid(dev, 0, vid);
532 static int __mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
533 u16 vid_begin, u16 vid_end, bool init)
535 struct net_device *dev = mlxsw_sp_port->dev;
539 /* In case this is invoked with BRIDGE_FLAGS_SELF and port is
540 * not bridged, then prevent packets ingressing through the
541 * port with the specified VIDs from being trapped to CPU.
543 if (!init && !mlxsw_sp_port->bridged)
544 return mlxsw_sp_port_kill_vids(dev, vid_begin, vid_end);
546 for (vid = vid_begin; vid <= vid_end;
547 vid += MLXSW_REG_SPVM_REC_MAX_COUNT) {
548 vid_e = min((u16) (vid + MLXSW_REG_SPVM_REC_MAX_COUNT - 1),
550 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid_e, false,
553 netdev_err(mlxsw_sp_port->dev, "Unable to del VIDs %d-%d\n",
559 if ((mlxsw_sp_port->pvid >= vid_begin) &&
560 (mlxsw_sp_port->pvid <= vid_end)) {
561 /* Default VLAN is always 1 */
562 mlxsw_sp_port->pvid = 1;
563 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port,
564 mlxsw_sp_port->pvid);
566 netdev_err(mlxsw_sp_port->dev, "Unable to del PVID %d\n",
575 err = __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid_begin, vid_end,
578 netdev_err(dev, "Failed to clear flooding\n");
582 for (vid = vid_begin; vid <= vid_end; vid++) {
583 /* Remove FID mapping in case of Virtual mode */
584 err = mlxsw_sp_port_fid_unmap(mlxsw_sp_port, vid);
586 netdev_err(dev, "Failed to unmap FID=%d", vid);
592 /* Changing activity bits only if HW operation succeded */
593 for (vid = vid_begin; vid <= vid_end; vid++)
594 clear_bit(vid, mlxsw_sp_port->active_vlans);
599 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
600 const struct switchdev_obj_port_vlan *vlan)
602 return __mlxsw_sp_port_vlans_del(mlxsw_sp_port,
603 vlan->vid_begin, vlan->vid_end, false);
607 mlxsw_sp_port_fdb_static_del(struct mlxsw_sp_port *mlxsw_sp_port,
608 const struct switchdev_obj_port_fdb *fdb)
610 return mlxsw_sp_port_fdb_op(mlxsw_sp_port, fdb->addr, fdb->vid,
614 static int mlxsw_sp_port_obj_del(struct net_device *dev,
615 const struct switchdev_obj *obj)
617 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
621 case SWITCHDEV_OBJ_ID_PORT_VLAN:
622 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
623 SWITCHDEV_OBJ_PORT_VLAN(obj));
625 case SWITCHDEV_OBJ_ID_PORT_FDB:
626 err = mlxsw_sp_port_fdb_static_del(mlxsw_sp_port,
627 SWITCHDEV_OBJ_PORT_FDB(obj));
637 static int mlxsw_sp_port_fdb_dump(struct mlxsw_sp_port *mlxsw_sp_port,
638 struct switchdev_obj_port_fdb *fdb,
639 switchdev_obj_dump_cb_t *cb)
650 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
654 mlxsw_reg_sfd_pack(sfd_pl, MLXSW_REG_SFD_OP_QUERY_DUMP, 0);
656 mlxsw_reg_sfd_num_rec_set(sfd_pl, MLXSW_REG_SFD_REC_MAX_COUNT);
657 err = mlxsw_reg_query(mlxsw_sp_port->mlxsw_sp->core,
658 MLXSW_REG(sfd), sfd_pl);
662 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
664 /* Even in case of error, we have to run the dump to the end
665 * so the session in firmware is finished.
670 for (i = 0; i < num_rec; i++) {
671 switch (mlxsw_reg_sfd_rec_type_get(sfd_pl, i)) {
672 case MLXSW_REG_SFD_REC_TYPE_UNICAST:
673 mlxsw_reg_sfd_uc_unpack(sfd_pl, i, mac, &vid,
675 if (local_port == mlxsw_sp_port->local_port) {
676 ether_addr_copy(fdb->addr, mac);
677 fdb->ndm_state = NUD_REACHABLE;
685 } while (num_rec == MLXSW_REG_SFD_REC_MAX_COUNT);
689 return stored_err ? stored_err : err;
692 static int mlxsw_sp_port_vlan_dump(struct mlxsw_sp_port *mlxsw_sp_port,
693 struct switchdev_obj_port_vlan *vlan,
694 switchdev_obj_dump_cb_t *cb)
699 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
701 if (vid == mlxsw_sp_port->pvid)
702 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
703 vlan->vid_begin = vid;
705 err = cb(&vlan->obj);
712 static int mlxsw_sp_port_obj_dump(struct net_device *dev,
713 struct switchdev_obj *obj,
714 switchdev_obj_dump_cb_t *cb)
716 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
720 case SWITCHDEV_OBJ_ID_PORT_VLAN:
721 err = mlxsw_sp_port_vlan_dump(mlxsw_sp_port,
722 SWITCHDEV_OBJ_PORT_VLAN(obj), cb);
724 case SWITCHDEV_OBJ_ID_PORT_FDB:
725 err = mlxsw_sp_port_fdb_dump(mlxsw_sp_port,
726 SWITCHDEV_OBJ_PORT_FDB(obj), cb);
736 static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = {
737 .switchdev_port_attr_get = mlxsw_sp_port_attr_get,
738 .switchdev_port_attr_set = mlxsw_sp_port_attr_set,
739 .switchdev_port_obj_add = mlxsw_sp_port_obj_add,
740 .switchdev_port_obj_del = mlxsw_sp_port_obj_del,
741 .switchdev_port_obj_dump = mlxsw_sp_port_obj_dump,
744 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
745 char *sfn_pl, int rec_index,
748 struct mlxsw_sp_port *mlxsw_sp_port;
754 mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &vid, &local_port);
755 mlxsw_sp_port = mlxsw_sp->ports[local_port];
756 if (!mlxsw_sp_port) {
757 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
761 err = mlxsw_sp_port_fdb_op(mlxsw_sp_port, mac, vid,
762 adding && mlxsw_sp_port->learning, true);
765 netdev_err(mlxsw_sp_port->dev, "Failed to set FDB entry\n");
769 if (mlxsw_sp_port->learning && mlxsw_sp_port->learning_sync) {
770 struct switchdev_notifier_fdb_info info;
771 unsigned long notifier_type;
775 notifier_type = adding ? SWITCHDEV_FDB_ADD : SWITCHDEV_FDB_DEL;
776 call_switchdev_notifiers(notifier_type, mlxsw_sp_port->dev,
781 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
782 char *sfn_pl, int rec_index)
784 switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
785 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
786 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
789 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
790 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
796 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp)
798 schedule_delayed_work(&mlxsw_sp->fdb_notify.dw,
799 msecs_to_jiffies(mlxsw_sp->fdb_notify.interval));
802 static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
804 struct mlxsw_sp *mlxsw_sp;
810 sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
814 mlxsw_sp = container_of(work, struct mlxsw_sp, fdb_notify.dw.work);
818 mlxsw_reg_sfn_pack(sfn_pl);
819 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
821 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
824 num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
825 for (i = 0; i < num_rec; i++)
826 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
832 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
835 static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
839 err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
841 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
844 INIT_DELAYED_WORK(&mlxsw_sp->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
845 mlxsw_sp->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
846 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
850 static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
852 cancel_delayed_work_sync(&mlxsw_sp->fdb_notify.dw);
855 static void mlxsw_sp_fids_fini(struct mlxsw_sp *mlxsw_sp)
859 for_each_set_bit(fid, mlxsw_sp->active_fids, VLAN_N_VID)
860 mlxsw_sp_fid_destroy(mlxsw_sp, fid);
863 int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
865 return mlxsw_sp_fdb_init(mlxsw_sp);
868 void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
870 mlxsw_sp_fdb_fini(mlxsw_sp);
871 mlxsw_sp_fids_fini(mlxsw_sp);
874 int mlxsw_sp_port_vlan_init(struct mlxsw_sp_port *mlxsw_sp_port)
876 struct net_device *dev = mlxsw_sp_port->dev;
879 /* Allow only untagged packets to ingress and tag them internally
882 mlxsw_sp_port->pvid = 1;
883 err = __mlxsw_sp_port_vlans_del(mlxsw_sp_port, 0, VLAN_N_VID, true);
885 netdev_err(dev, "Unable to init VLANs\n");
889 /* Add implicit VLAN interface in the device, so that untagged
890 * packets will be classified to the default vFID.
892 err = mlxsw_sp_port_add_vid(dev, 0, 1);
894 netdev_err(dev, "Failed to configure default vFID\n");
899 void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port *mlxsw_sp_port)
901 mlxsw_sp_port->dev->switchdev_ops = &mlxsw_sp_port_switchdev_ops;
904 void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port)