1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2023, Intel Corporation. */
5 #include "ice_eswitch_br.h"
7 #include "ice_switch.h"
9 #include "ice_vf_vsi_vlan_ops.h"
10 #include "ice_trace.h"
12 #define ICE_ESW_BRIDGE_UPDATE_INTERVAL msecs_to_jiffies(1000)
14 static const struct rhashtable_params ice_fdb_ht_params = {
15 .key_offset = offsetof(struct ice_esw_br_fdb_entry, data),
16 .key_len = sizeof(struct ice_esw_br_fdb_data),
17 .head_offset = offsetof(struct ice_esw_br_fdb_entry, ht_node),
18 .automatic_shrinking = true,
21 static bool ice_eswitch_br_is_dev_valid(const struct net_device *dev)
23 /* Accept only PF netdev, PRs and LAG */
24 return ice_is_port_repr_netdev(dev) || netif_is_ice(dev) ||
25 netif_is_lag_master(dev);
28 static struct net_device *
29 ice_eswitch_br_get_uplink_from_lag(struct net_device *lag_dev)
31 struct net_device *lower;
32 struct list_head *iter;
34 netdev_for_each_lower_dev(lag_dev, lower, iter) {
35 if (netif_is_ice(lower))
42 static struct ice_esw_br_port *
43 ice_eswitch_br_netdev_to_port(struct net_device *dev)
45 if (ice_is_port_repr_netdev(dev)) {
46 struct ice_repr *repr = ice_netdev_to_repr(dev);
49 } else if (netif_is_ice(dev) || netif_is_lag_master(dev)) {
50 struct net_device *ice_dev;
53 if (netif_is_lag_master(dev))
54 ice_dev = ice_eswitch_br_get_uplink_from_lag(dev);
61 pf = ice_netdev_to_pf(ice_dev);
70 ice_eswitch_br_ingress_rule_setup(struct ice_adv_rule_info *rule_info,
71 u8 pf_id, u16 vf_vsi_idx)
73 rule_info->sw_act.vsi_handle = vf_vsi_idx;
74 rule_info->sw_act.flag |= ICE_FLTR_RX;
75 rule_info->sw_act.src = pf_id;
76 rule_info->priority = 2;
80 ice_eswitch_br_egress_rule_setup(struct ice_adv_rule_info *rule_info,
83 rule_info->sw_act.vsi_handle = pf_vsi_idx;
84 rule_info->sw_act.flag |= ICE_FLTR_TX;
85 rule_info->flags_info.act = ICE_SINGLE_ACT_LAN_ENABLE;
86 rule_info->flags_info.act_valid = true;
87 rule_info->priority = 2;
91 ice_eswitch_br_rule_delete(struct ice_hw *hw, struct ice_rule_query_data *rule)
98 err = ice_rem_adv_rule_by_id(hw, rule);
105 ice_eswitch_br_get_lkups_cnt(u16 vid)
107 return ice_eswitch_br_is_vid_valid(vid) ? 2 : 1;
111 ice_eswitch_br_add_vlan_lkup(struct ice_adv_lkup_elem *list, u16 vid)
113 if (ice_eswitch_br_is_vid_valid(vid)) {
114 list[1].type = ICE_VLAN_OFOS;
115 list[1].h_u.vlan_hdr.vlan = cpu_to_be16(vid & VLAN_VID_MASK);
116 list[1].m_u.vlan_hdr.vlan = cpu_to_be16(0xFFFF);
120 static struct ice_rule_query_data *
121 ice_eswitch_br_fwd_rule_create(struct ice_hw *hw, int vsi_idx, int port_type,
122 const unsigned char *mac, u16 vid)
124 struct ice_adv_rule_info rule_info = { 0 };
125 struct ice_rule_query_data *rule;
126 struct ice_adv_lkup_elem *list;
130 lkups_cnt = ice_eswitch_br_get_lkups_cnt(vid);
132 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
134 return ERR_PTR(-ENOMEM);
136 list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
143 case ICE_ESWITCH_BR_UPLINK_PORT:
144 ice_eswitch_br_egress_rule_setup(&rule_info, vsi_idx);
146 case ICE_ESWITCH_BR_VF_REPR_PORT:
147 ice_eswitch_br_ingress_rule_setup(&rule_info, hw->pf_id,
155 list[0].type = ICE_MAC_OFOS;
156 ether_addr_copy(list[0].h_u.eth_hdr.dst_addr, mac);
157 eth_broadcast_addr(list[0].m_u.eth_hdr.dst_addr);
159 ice_eswitch_br_add_vlan_lkup(list, vid);
161 rule_info.need_pass_l2 = true;
163 rule_info.sw_act.fltr_act = ICE_FWD_TO_VSI;
165 err = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info, rule);
181 static struct ice_rule_query_data *
182 ice_eswitch_br_guard_rule_create(struct ice_hw *hw, u16 vsi_idx,
183 const unsigned char *mac, u16 vid)
185 struct ice_adv_rule_info rule_info = { 0 };
186 struct ice_rule_query_data *rule;
187 struct ice_adv_lkup_elem *list;
191 lkups_cnt = ice_eswitch_br_get_lkups_cnt(vid);
193 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
197 list = kcalloc(lkups_cnt, sizeof(*list), GFP_ATOMIC);
201 list[0].type = ICE_MAC_OFOS;
202 ether_addr_copy(list[0].h_u.eth_hdr.src_addr, mac);
203 eth_broadcast_addr(list[0].m_u.eth_hdr.src_addr);
205 ice_eswitch_br_add_vlan_lkup(list, vid);
207 rule_info.allow_pass_l2 = true;
208 rule_info.sw_act.vsi_handle = vsi_idx;
209 rule_info.sw_act.fltr_act = ICE_NOP;
210 rule_info.priority = 2;
212 err = ice_add_adv_rule(hw, list, lkups_cnt, &rule_info, rule);
228 static struct ice_esw_br_flow *
229 ice_eswitch_br_flow_create(struct device *dev, struct ice_hw *hw, int vsi_idx,
230 int port_type, const unsigned char *mac, u16 vid)
232 struct ice_rule_query_data *fwd_rule, *guard_rule;
233 struct ice_esw_br_flow *flow;
236 flow = kzalloc(sizeof(*flow), GFP_KERNEL);
238 return ERR_PTR(-ENOMEM);
240 fwd_rule = ice_eswitch_br_fwd_rule_create(hw, vsi_idx, port_type, mac,
242 err = PTR_ERR_OR_ZERO(fwd_rule);
244 dev_err(dev, "Failed to create eswitch bridge %sgress forward rule, err: %d\n",
245 port_type == ICE_ESWITCH_BR_UPLINK_PORT ? "e" : "in",
250 guard_rule = ice_eswitch_br_guard_rule_create(hw, vsi_idx, mac, vid);
251 err = PTR_ERR_OR_ZERO(guard_rule);
253 dev_err(dev, "Failed to create eswitch bridge %sgress guard rule, err: %d\n",
254 port_type == ICE_ESWITCH_BR_UPLINK_PORT ? "e" : "in",
259 flow->fwd_rule = fwd_rule;
260 flow->guard_rule = guard_rule;
265 ice_eswitch_br_rule_delete(hw, fwd_rule);
272 static struct ice_esw_br_fdb_entry *
273 ice_eswitch_br_fdb_find(struct ice_esw_br *bridge, const unsigned char *mac,
276 struct ice_esw_br_fdb_data data = {
280 ether_addr_copy(data.addr, mac);
281 return rhashtable_lookup_fast(&bridge->fdb_ht, &data,
286 ice_eswitch_br_flow_delete(struct ice_pf *pf, struct ice_esw_br_flow *flow)
288 struct device *dev = ice_pf_to_dev(pf);
291 err = ice_eswitch_br_rule_delete(&pf->hw, flow->fwd_rule);
293 dev_err(dev, "Failed to delete FDB forward rule, err: %d\n",
296 err = ice_eswitch_br_rule_delete(&pf->hw, flow->guard_rule);
298 dev_err(dev, "Failed to delete FDB guard rule, err: %d\n",
304 static struct ice_esw_br_vlan *
305 ice_esw_br_port_vlan_lookup(struct ice_esw_br *bridge, u16 vsi_idx, u16 vid)
307 struct ice_pf *pf = bridge->br_offloads->pf;
308 struct device *dev = ice_pf_to_dev(pf);
309 struct ice_esw_br_port *port;
310 struct ice_esw_br_vlan *vlan;
312 port = xa_load(&bridge->ports, vsi_idx);
314 dev_info(dev, "Bridge port lookup failed (vsi=%u)\n", vsi_idx);
315 return ERR_PTR(-EINVAL);
318 vlan = xa_load(&port->vlans, vid);
320 dev_info(dev, "Bridge port vlan metadata lookup failed (vsi=%u)\n",
322 return ERR_PTR(-EINVAL);
329 ice_eswitch_br_fdb_entry_delete(struct ice_esw_br *bridge,
330 struct ice_esw_br_fdb_entry *fdb_entry)
332 struct ice_pf *pf = bridge->br_offloads->pf;
334 rhashtable_remove_fast(&bridge->fdb_ht, &fdb_entry->ht_node,
336 list_del(&fdb_entry->list);
338 ice_eswitch_br_flow_delete(pf, fdb_entry->flow);
344 ice_eswitch_br_fdb_offload_notify(struct net_device *dev,
345 const unsigned char *mac, u16 vid,
348 struct switchdev_notifier_fdb_info fdb_info = {
354 call_switchdev_notifiers(val, dev, &fdb_info.info, NULL);
358 ice_eswitch_br_fdb_entry_notify_and_cleanup(struct ice_esw_br *bridge,
359 struct ice_esw_br_fdb_entry *entry)
361 if (!(entry->flags & ICE_ESWITCH_BR_FDB_ADDED_BY_USER))
362 ice_eswitch_br_fdb_offload_notify(entry->dev, entry->data.addr,
364 SWITCHDEV_FDB_DEL_TO_BRIDGE);
365 ice_eswitch_br_fdb_entry_delete(bridge, entry);
369 ice_eswitch_br_fdb_entry_find_and_delete(struct ice_esw_br *bridge,
370 const unsigned char *mac, u16 vid)
372 struct ice_pf *pf = bridge->br_offloads->pf;
373 struct ice_esw_br_fdb_entry *fdb_entry;
374 struct device *dev = ice_pf_to_dev(pf);
376 fdb_entry = ice_eswitch_br_fdb_find(bridge, mac, vid);
378 dev_err(dev, "FDB entry with mac: %pM and vid: %u not found\n",
383 trace_ice_eswitch_br_fdb_entry_find_and_delete(fdb_entry);
384 ice_eswitch_br_fdb_entry_notify_and_cleanup(bridge, fdb_entry);
388 ice_eswitch_br_fdb_entry_create(struct net_device *netdev,
389 struct ice_esw_br_port *br_port,
391 const unsigned char *mac, u16 vid)
393 struct ice_esw_br *bridge = br_port->bridge;
394 struct ice_pf *pf = bridge->br_offloads->pf;
395 struct device *dev = ice_pf_to_dev(pf);
396 struct ice_esw_br_fdb_entry *fdb_entry;
397 struct ice_esw_br_flow *flow;
398 struct ice_esw_br_vlan *vlan;
399 struct ice_hw *hw = &pf->hw;
403 /* untagged filtering is not yet supported */
404 if (!(bridge->flags & ICE_ESWITCH_BR_VLAN_FILTERING) && vid)
407 if ((bridge->flags & ICE_ESWITCH_BR_VLAN_FILTERING)) {
408 vlan = ice_esw_br_port_vlan_lookup(bridge, br_port->vsi_idx,
411 dev_err(dev, "Failed to find vlan lookup, err: %ld\n",
417 fdb_entry = ice_eswitch_br_fdb_find(bridge, mac, vid);
419 ice_eswitch_br_fdb_entry_notify_and_cleanup(bridge, fdb_entry);
421 fdb_entry = kzalloc(sizeof(*fdb_entry), GFP_KERNEL);
427 flow = ice_eswitch_br_flow_create(dev, hw, br_port->vsi_idx,
428 br_port->type, mac, vid);
434 ether_addr_copy(fdb_entry->data.addr, mac);
435 fdb_entry->data.vid = vid;
436 fdb_entry->br_port = br_port;
437 fdb_entry->flow = flow;
438 fdb_entry->dev = netdev;
439 fdb_entry->last_use = jiffies;
440 event = SWITCHDEV_FDB_ADD_TO_BRIDGE;
443 fdb_entry->flags |= ICE_ESWITCH_BR_FDB_ADDED_BY_USER;
444 event = SWITCHDEV_FDB_OFFLOADED;
447 err = rhashtable_insert_fast(&bridge->fdb_ht, &fdb_entry->ht_node,
452 list_add(&fdb_entry->list, &bridge->fdb_list);
453 trace_ice_eswitch_br_fdb_entry_create(fdb_entry);
455 ice_eswitch_br_fdb_offload_notify(netdev, mac, vid, event);
460 ice_eswitch_br_flow_delete(pf, flow);
464 dev_err(dev, "Failed to create fdb entry, err: %d\n", err);
468 ice_eswitch_br_fdb_work_dealloc(struct ice_esw_br_fdb_work *fdb_work)
470 kfree(fdb_work->fdb_info.addr);
475 ice_eswitch_br_fdb_event_work(struct work_struct *work)
477 struct ice_esw_br_fdb_work *fdb_work = ice_work_to_fdb_work(work);
478 bool added_by_user = fdb_work->fdb_info.added_by_user;
479 const unsigned char *mac = fdb_work->fdb_info.addr;
480 u16 vid = fdb_work->fdb_info.vid;
481 struct ice_esw_br_port *br_port;
485 br_port = ice_eswitch_br_netdev_to_port(fdb_work->dev);
489 switch (fdb_work->event) {
490 case SWITCHDEV_FDB_ADD_TO_DEVICE:
491 ice_eswitch_br_fdb_entry_create(fdb_work->dev, br_port,
492 added_by_user, mac, vid);
494 case SWITCHDEV_FDB_DEL_TO_DEVICE:
495 ice_eswitch_br_fdb_entry_find_and_delete(br_port->bridge,
504 dev_put(fdb_work->dev);
505 ice_eswitch_br_fdb_work_dealloc(fdb_work);
508 static struct ice_esw_br_fdb_work *
509 ice_eswitch_br_fdb_work_alloc(struct switchdev_notifier_fdb_info *fdb_info,
510 struct net_device *dev,
513 struct ice_esw_br_fdb_work *work;
516 work = kzalloc(sizeof(*work), GFP_ATOMIC);
518 return ERR_PTR(-ENOMEM);
520 INIT_WORK(&work->work, ice_eswitch_br_fdb_event_work);
521 memcpy(&work->fdb_info, fdb_info, sizeof(work->fdb_info));
523 mac = kzalloc(ETH_ALEN, GFP_ATOMIC);
526 return ERR_PTR(-ENOMEM);
529 ether_addr_copy(mac, fdb_info->addr);
530 work->fdb_info.addr = mac;
538 ice_eswitch_br_switchdev_event(struct notifier_block *nb,
539 unsigned long event, void *ptr)
541 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
542 struct switchdev_notifier_fdb_info *fdb_info;
543 struct switchdev_notifier_info *info = ptr;
544 struct ice_esw_br_offloads *br_offloads;
545 struct ice_esw_br_fdb_work *work;
546 struct netlink_ext_ack *extack;
547 struct net_device *upper;
549 br_offloads = ice_nb_to_br_offloads(nb, switchdev_nb);
550 extack = switchdev_notifier_info_to_extack(ptr);
552 upper = netdev_master_upper_dev_get_rcu(dev);
556 if (!netif_is_bridge_master(upper))
559 if (!ice_eswitch_br_is_dev_valid(dev))
562 if (!ice_eswitch_br_netdev_to_port(dev))
566 case SWITCHDEV_FDB_ADD_TO_DEVICE:
567 case SWITCHDEV_FDB_DEL_TO_DEVICE:
568 fdb_info = container_of(info, typeof(*fdb_info), info);
570 work = ice_eswitch_br_fdb_work_alloc(fdb_info, dev, event);
572 NL_SET_ERR_MSG_MOD(extack, "Failed to init switchdev fdb work");
573 return notifier_from_errno(PTR_ERR(work));
577 queue_work(br_offloads->wq, &work->work);
585 void ice_eswitch_br_fdb_flush(struct ice_esw_br *bridge)
587 struct ice_esw_br_fdb_entry *entry, *tmp;
592 list_for_each_entry_safe(entry, tmp, &bridge->fdb_list, list)
593 ice_eswitch_br_fdb_entry_notify_and_cleanup(bridge, entry);
597 ice_eswitch_br_vlan_filtering_set(struct ice_esw_br *bridge, bool enable)
599 if (enable == !!(bridge->flags & ICE_ESWITCH_BR_VLAN_FILTERING))
602 ice_eswitch_br_fdb_flush(bridge);
604 bridge->flags |= ICE_ESWITCH_BR_VLAN_FILTERING;
606 bridge->flags &= ~ICE_ESWITCH_BR_VLAN_FILTERING;
610 ice_eswitch_br_clear_pvid(struct ice_esw_br_port *port)
612 struct ice_vlan port_vlan = ICE_VLAN(ETH_P_8021Q, port->pvid, 0);
613 struct ice_vsi_vlan_ops *vlan_ops;
615 vlan_ops = ice_get_compat_vsi_vlan_ops(port->vsi);
617 vlan_ops->del_vlan(port->vsi, &port_vlan);
618 vlan_ops->clear_port_vlan(port->vsi);
620 ice_vf_vsi_disable_port_vlan(port->vsi);
626 ice_eswitch_br_vlan_cleanup(struct ice_esw_br_port *port,
627 struct ice_esw_br_vlan *vlan)
629 struct ice_esw_br_fdb_entry *fdb_entry, *tmp;
630 struct ice_esw_br *bridge = port->bridge;
632 trace_ice_eswitch_br_vlan_cleanup(vlan);
634 list_for_each_entry_safe(fdb_entry, tmp, &bridge->fdb_list, list) {
635 if (vlan->vid == fdb_entry->data.vid)
636 ice_eswitch_br_fdb_entry_delete(bridge, fdb_entry);
639 xa_erase(&port->vlans, vlan->vid);
640 if (port->pvid == vlan->vid)
641 ice_eswitch_br_clear_pvid(port);
645 static void ice_eswitch_br_port_vlans_flush(struct ice_esw_br_port *port)
647 struct ice_esw_br_vlan *vlan;
650 xa_for_each(&port->vlans, index, vlan)
651 ice_eswitch_br_vlan_cleanup(port, vlan);
655 ice_eswitch_br_set_pvid(struct ice_esw_br_port *port,
656 struct ice_esw_br_vlan *vlan)
658 struct ice_vlan port_vlan = ICE_VLAN(ETH_P_8021Q, vlan->vid, 0);
659 struct device *dev = ice_pf_to_dev(port->vsi->back);
660 struct ice_vsi_vlan_ops *vlan_ops;
663 if (port->pvid == vlan->vid || vlan->vid == 1)
666 /* Setting port vlan on uplink isn't supported by hw */
667 if (port->type == ICE_ESWITCH_BR_UPLINK_PORT)
672 "Port VLAN (vsi=%u, vid=%u) already exists on the port, remove it before adding new one\n",
673 port->vsi_idx, port->pvid);
677 ice_vf_vsi_enable_port_vlan(port->vsi);
679 vlan_ops = ice_get_compat_vsi_vlan_ops(port->vsi);
680 err = vlan_ops->set_port_vlan(port->vsi, &port_vlan);
684 err = vlan_ops->add_vlan(port->vsi, &port_vlan);
688 ice_eswitch_br_port_vlans_flush(port);
689 port->pvid = vlan->vid;
694 static struct ice_esw_br_vlan *
695 ice_eswitch_br_vlan_create(u16 vid, u16 flags, struct ice_esw_br_port *port)
697 struct device *dev = ice_pf_to_dev(port->vsi->back);
698 struct ice_esw_br_vlan *vlan;
701 vlan = kzalloc(sizeof(*vlan), GFP_KERNEL);
703 return ERR_PTR(-ENOMEM);
707 if ((flags & BRIDGE_VLAN_INFO_PVID) &&
708 (flags & BRIDGE_VLAN_INFO_UNTAGGED)) {
709 err = ice_eswitch_br_set_pvid(port, vlan);
712 } else if ((flags & BRIDGE_VLAN_INFO_PVID) ||
713 (flags & BRIDGE_VLAN_INFO_UNTAGGED)) {
714 dev_info(dev, "VLAN push and pop are supported only simultaneously\n");
719 err = xa_insert(&port->vlans, vlan->vid, vlan, GFP_KERNEL);
723 trace_ice_eswitch_br_vlan_create(vlan);
729 ice_eswitch_br_clear_pvid(port);
736 ice_eswitch_br_port_vlan_add(struct ice_esw_br *bridge, u16 vsi_idx, u16 vid,
737 u16 flags, struct netlink_ext_ack *extack)
739 struct ice_esw_br_port *port;
740 struct ice_esw_br_vlan *vlan;
742 port = xa_load(&bridge->ports, vsi_idx);
747 dev_info(ice_pf_to_dev(port->vsi->back),
748 "Port VLAN (vsi=%u, vid=%d) exists on the port, remove it to add trunk VLANs\n",
749 port->vsi_idx, port->pvid);
753 vlan = xa_load(&port->vlans, vid);
755 if (vlan->flags == flags)
758 ice_eswitch_br_vlan_cleanup(port, vlan);
761 vlan = ice_eswitch_br_vlan_create(vid, flags, port);
763 NL_SET_ERR_MSG_FMT_MOD(extack, "Failed to create VLAN entry, vid: %u, vsi: %u",
765 return PTR_ERR(vlan);
772 ice_eswitch_br_port_vlan_del(struct ice_esw_br *bridge, u16 vsi_idx, u16 vid)
774 struct ice_esw_br_port *port;
775 struct ice_esw_br_vlan *vlan;
777 port = xa_load(&bridge->ports, vsi_idx);
781 vlan = xa_load(&port->vlans, vid);
785 ice_eswitch_br_vlan_cleanup(port, vlan);
789 ice_eswitch_br_port_obj_add(struct net_device *netdev, const void *ctx,
790 const struct switchdev_obj *obj,
791 struct netlink_ext_ack *extack)
793 struct ice_esw_br_port *br_port = ice_eswitch_br_netdev_to_port(netdev);
794 struct switchdev_obj_port_vlan *vlan;
801 case SWITCHDEV_OBJ_ID_PORT_VLAN:
802 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
803 err = ice_eswitch_br_port_vlan_add(br_port->bridge,
804 br_port->vsi_idx, vlan->vid,
805 vlan->flags, extack);
813 ice_eswitch_br_port_obj_del(struct net_device *netdev, const void *ctx,
814 const struct switchdev_obj *obj)
816 struct ice_esw_br_port *br_port = ice_eswitch_br_netdev_to_port(netdev);
817 struct switchdev_obj_port_vlan *vlan;
823 case SWITCHDEV_OBJ_ID_PORT_VLAN:
824 vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
825 ice_eswitch_br_port_vlan_del(br_port->bridge, br_port->vsi_idx,
834 ice_eswitch_br_port_obj_attr_set(struct net_device *netdev, const void *ctx,
835 const struct switchdev_attr *attr,
836 struct netlink_ext_ack *extack)
838 struct ice_esw_br_port *br_port = ice_eswitch_br_netdev_to_port(netdev);
844 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
845 ice_eswitch_br_vlan_filtering_set(br_port->bridge,
846 attr->u.vlan_filtering);
848 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
849 br_port->bridge->ageing_time =
850 clock_t_to_jiffies(attr->u.ageing_time);
858 ice_eswitch_br_event_blocking(struct notifier_block *nb, unsigned long event,
861 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
865 case SWITCHDEV_PORT_OBJ_ADD:
866 err = switchdev_handle_port_obj_add(dev, ptr,
867 ice_eswitch_br_is_dev_valid,
868 ice_eswitch_br_port_obj_add);
870 case SWITCHDEV_PORT_OBJ_DEL:
871 err = switchdev_handle_port_obj_del(dev, ptr,
872 ice_eswitch_br_is_dev_valid,
873 ice_eswitch_br_port_obj_del);
875 case SWITCHDEV_PORT_ATTR_SET:
876 err = switchdev_handle_port_attr_set(dev, ptr,
877 ice_eswitch_br_is_dev_valid,
878 ice_eswitch_br_port_obj_attr_set);
884 return notifier_from_errno(err);
888 ice_eswitch_br_port_deinit(struct ice_esw_br *bridge,
889 struct ice_esw_br_port *br_port)
891 struct ice_esw_br_fdb_entry *fdb_entry, *tmp;
892 struct ice_vsi *vsi = br_port->vsi;
894 list_for_each_entry_safe(fdb_entry, tmp, &bridge->fdb_list, list) {
895 if (br_port == fdb_entry->br_port)
896 ice_eswitch_br_fdb_entry_delete(bridge, fdb_entry);
899 if (br_port->type == ICE_ESWITCH_BR_UPLINK_PORT && vsi->back) {
900 vsi->back->br_port = NULL;
902 struct ice_repr *repr =
903 ice_repr_get(vsi->back, br_port->repr_id);
906 repr->br_port = NULL;
909 xa_erase(&bridge->ports, br_port->vsi_idx);
910 ice_eswitch_br_port_vlans_flush(br_port);
914 static struct ice_esw_br_port *
915 ice_eswitch_br_port_init(struct ice_esw_br *bridge)
917 struct ice_esw_br_port *br_port;
919 br_port = kzalloc(sizeof(*br_port), GFP_KERNEL);
921 return ERR_PTR(-ENOMEM);
923 xa_init(&br_port->vlans);
925 br_port->bridge = bridge;
931 ice_eswitch_br_vf_repr_port_init(struct ice_esw_br *bridge,
932 struct ice_repr *repr)
934 struct ice_esw_br_port *br_port;
937 br_port = ice_eswitch_br_port_init(bridge);
939 return PTR_ERR(br_port);
941 br_port->vsi = repr->src_vsi;
942 br_port->vsi_idx = br_port->vsi->idx;
943 br_port->type = ICE_ESWITCH_BR_VF_REPR_PORT;
944 br_port->repr_id = repr->id;
945 repr->br_port = br_port;
947 err = xa_insert(&bridge->ports, br_port->vsi_idx, br_port, GFP_KERNEL);
949 ice_eswitch_br_port_deinit(bridge, br_port);
957 ice_eswitch_br_uplink_port_init(struct ice_esw_br *bridge, struct ice_pf *pf)
959 struct ice_vsi *vsi = pf->eswitch.uplink_vsi;
960 struct ice_esw_br_port *br_port;
963 br_port = ice_eswitch_br_port_init(bridge);
965 return PTR_ERR(br_port);
968 br_port->vsi_idx = br_port->vsi->idx;
969 br_port->type = ICE_ESWITCH_BR_UPLINK_PORT;
970 pf->br_port = br_port;
972 err = xa_insert(&bridge->ports, br_port->vsi_idx, br_port, GFP_KERNEL);
974 ice_eswitch_br_port_deinit(bridge, br_port);
982 ice_eswitch_br_ports_flush(struct ice_esw_br *bridge)
984 struct ice_esw_br_port *port;
987 xa_for_each(&bridge->ports, i, port)
988 ice_eswitch_br_port_deinit(bridge, port);
992 ice_eswitch_br_deinit(struct ice_esw_br_offloads *br_offloads,
993 struct ice_esw_br *bridge)
998 /* Cleanup all the ports that were added asynchronously
999 * through NETDEV_CHANGEUPPER event.
1001 ice_eswitch_br_ports_flush(bridge);
1002 WARN_ON(!xa_empty(&bridge->ports));
1003 xa_destroy(&bridge->ports);
1004 rhashtable_destroy(&bridge->fdb_ht);
1006 br_offloads->bridge = NULL;
1010 static struct ice_esw_br *
1011 ice_eswitch_br_init(struct ice_esw_br_offloads *br_offloads, int ifindex)
1013 struct ice_esw_br *bridge;
1016 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
1018 return ERR_PTR(-ENOMEM);
1020 err = rhashtable_init(&bridge->fdb_ht, &ice_fdb_ht_params);
1023 return ERR_PTR(err);
1026 INIT_LIST_HEAD(&bridge->fdb_list);
1027 bridge->br_offloads = br_offloads;
1028 bridge->ifindex = ifindex;
1029 bridge->ageing_time = clock_t_to_jiffies(BR_DEFAULT_AGEING_TIME);
1030 xa_init(&bridge->ports);
1031 br_offloads->bridge = bridge;
1036 static struct ice_esw_br *
1037 ice_eswitch_br_get(struct ice_esw_br_offloads *br_offloads, int ifindex,
1038 struct netlink_ext_ack *extack)
1040 struct ice_esw_br *bridge = br_offloads->bridge;
1043 if (bridge->ifindex != ifindex) {
1044 NL_SET_ERR_MSG_MOD(extack,
1045 "Only one bridge is supported per eswitch");
1046 return ERR_PTR(-EOPNOTSUPP);
1051 /* Create the bridge if it doesn't exist yet */
1052 bridge = ice_eswitch_br_init(br_offloads, ifindex);
1054 NL_SET_ERR_MSG_MOD(extack, "Failed to init the bridge");
1060 ice_eswitch_br_verify_deinit(struct ice_esw_br_offloads *br_offloads,
1061 struct ice_esw_br *bridge)
1063 /* Remove the bridge if it exists and there are no ports left */
1064 if (!bridge || !xa_empty(&bridge->ports))
1067 ice_eswitch_br_deinit(br_offloads, bridge);
1071 ice_eswitch_br_port_unlink(struct ice_esw_br_offloads *br_offloads,
1072 struct net_device *dev, int ifindex,
1073 struct netlink_ext_ack *extack)
1075 struct ice_esw_br_port *br_port = ice_eswitch_br_netdev_to_port(dev);
1076 struct ice_esw_br *bridge;
1079 NL_SET_ERR_MSG_MOD(extack,
1080 "Port representor is not attached to any bridge");
1084 if (br_port->bridge->ifindex != ifindex) {
1085 NL_SET_ERR_MSG_MOD(extack,
1086 "Port representor is attached to another bridge");
1090 bridge = br_port->bridge;
1092 trace_ice_eswitch_br_port_unlink(br_port);
1093 ice_eswitch_br_port_deinit(br_port->bridge, br_port);
1094 ice_eswitch_br_verify_deinit(br_offloads, bridge);
1100 ice_eswitch_br_port_link(struct ice_esw_br_offloads *br_offloads,
1101 struct net_device *dev, int ifindex,
1102 struct netlink_ext_ack *extack)
1104 struct ice_esw_br *bridge;
1107 if (ice_eswitch_br_netdev_to_port(dev)) {
1108 NL_SET_ERR_MSG_MOD(extack,
1109 "Port is already attached to the bridge");
1113 bridge = ice_eswitch_br_get(br_offloads, ifindex, extack);
1115 return PTR_ERR(bridge);
1117 if (ice_is_port_repr_netdev(dev)) {
1118 struct ice_repr *repr = ice_netdev_to_repr(dev);
1120 err = ice_eswitch_br_vf_repr_port_init(bridge, repr);
1121 trace_ice_eswitch_br_port_link(repr->br_port);
1123 struct net_device *ice_dev;
1126 if (netif_is_lag_master(dev))
1127 ice_dev = ice_eswitch_br_get_uplink_from_lag(dev);
1134 pf = ice_netdev_to_pf(ice_dev);
1136 err = ice_eswitch_br_uplink_port_init(bridge, pf);
1137 trace_ice_eswitch_br_port_link(pf->br_port);
1140 NL_SET_ERR_MSG_MOD(extack, "Failed to init bridge port");
1147 ice_eswitch_br_verify_deinit(br_offloads, bridge);
1152 ice_eswitch_br_port_changeupper(struct notifier_block *nb, void *ptr)
1154 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1155 struct netdev_notifier_changeupper_info *info = ptr;
1156 struct ice_esw_br_offloads *br_offloads;
1157 struct netlink_ext_ack *extack;
1158 struct net_device *upper;
1160 br_offloads = ice_nb_to_br_offloads(nb, netdev_nb);
1162 if (!ice_eswitch_br_is_dev_valid(dev))
1165 upper = info->upper_dev;
1166 if (!netif_is_bridge_master(upper))
1169 extack = netdev_notifier_info_to_extack(&info->info);
1172 return ice_eswitch_br_port_link(br_offloads, dev,
1173 upper->ifindex, extack);
1175 return ice_eswitch_br_port_unlink(br_offloads, dev,
1176 upper->ifindex, extack);
1180 ice_eswitch_br_port_event(struct notifier_block *nb,
1181 unsigned long event, void *ptr)
1186 case NETDEV_CHANGEUPPER:
1187 err = ice_eswitch_br_port_changeupper(nb, ptr);
1191 return notifier_from_errno(err);
1195 ice_eswitch_br_offloads_dealloc(struct ice_pf *pf)
1197 struct ice_esw_br_offloads *br_offloads = pf->eswitch.br_offloads;
1204 ice_eswitch_br_deinit(br_offloads, br_offloads->bridge);
1206 pf->eswitch.br_offloads = NULL;
1210 static struct ice_esw_br_offloads *
1211 ice_eswitch_br_offloads_alloc(struct ice_pf *pf)
1213 struct ice_esw_br_offloads *br_offloads;
1217 if (pf->eswitch.br_offloads)
1218 return ERR_PTR(-EEXIST);
1220 br_offloads = kzalloc(sizeof(*br_offloads), GFP_KERNEL);
1222 return ERR_PTR(-ENOMEM);
1224 pf->eswitch.br_offloads = br_offloads;
1225 br_offloads->pf = pf;
1231 ice_eswitch_br_offloads_deinit(struct ice_pf *pf)
1233 struct ice_esw_br_offloads *br_offloads;
1235 br_offloads = pf->eswitch.br_offloads;
1239 cancel_delayed_work_sync(&br_offloads->update_work);
1240 unregister_netdevice_notifier(&br_offloads->netdev_nb);
1241 unregister_switchdev_blocking_notifier(&br_offloads->switchdev_blk);
1242 unregister_switchdev_notifier(&br_offloads->switchdev_nb);
1243 destroy_workqueue(br_offloads->wq);
1244 /* Although notifier block is unregistered just before,
1245 * so we don't get any new events, some events might be
1246 * already in progress. Hold the rtnl lock and wait for
1250 ice_eswitch_br_offloads_dealloc(pf);
1254 static void ice_eswitch_br_update(struct ice_esw_br_offloads *br_offloads)
1256 struct ice_esw_br *bridge = br_offloads->bridge;
1257 struct ice_esw_br_fdb_entry *entry, *tmp;
1263 list_for_each_entry_safe(entry, tmp, &bridge->fdb_list, list) {
1264 if (entry->flags & ICE_ESWITCH_BR_FDB_ADDED_BY_USER)
1267 if (time_is_after_eq_jiffies(entry->last_use +
1268 bridge->ageing_time))
1271 ice_eswitch_br_fdb_entry_notify_and_cleanup(bridge, entry);
1276 static void ice_eswitch_br_update_work(struct work_struct *work)
1278 struct ice_esw_br_offloads *br_offloads;
1280 br_offloads = ice_work_to_br_offloads(work);
1282 ice_eswitch_br_update(br_offloads);
1284 queue_delayed_work(br_offloads->wq, &br_offloads->update_work,
1285 ICE_ESW_BRIDGE_UPDATE_INTERVAL);
1289 ice_eswitch_br_offloads_init(struct ice_pf *pf)
1291 struct ice_esw_br_offloads *br_offloads;
1292 struct device *dev = ice_pf_to_dev(pf);
1296 br_offloads = ice_eswitch_br_offloads_alloc(pf);
1298 if (IS_ERR(br_offloads)) {
1299 dev_err(dev, "Failed to init eswitch bridge\n");
1300 return PTR_ERR(br_offloads);
1303 br_offloads->wq = alloc_ordered_workqueue("ice_bridge_wq", 0);
1304 if (!br_offloads->wq) {
1306 dev_err(dev, "Failed to allocate bridge workqueue\n");
1310 br_offloads->switchdev_nb.notifier_call =
1311 ice_eswitch_br_switchdev_event;
1312 err = register_switchdev_notifier(&br_offloads->switchdev_nb);
1315 "Failed to register switchdev notifier\n");
1316 goto err_reg_switchdev_nb;
1319 br_offloads->switchdev_blk.notifier_call =
1320 ice_eswitch_br_event_blocking;
1321 err = register_switchdev_blocking_notifier(&br_offloads->switchdev_blk);
1324 "Failed to register bridge blocking switchdev notifier\n");
1325 goto err_reg_switchdev_blk;
1328 br_offloads->netdev_nb.notifier_call = ice_eswitch_br_port_event;
1329 err = register_netdevice_notifier(&br_offloads->netdev_nb);
1332 "Failed to register bridge port event notifier\n");
1333 goto err_reg_netdev_nb;
1336 INIT_DELAYED_WORK(&br_offloads->update_work,
1337 ice_eswitch_br_update_work);
1338 queue_delayed_work(br_offloads->wq, &br_offloads->update_work,
1339 ICE_ESW_BRIDGE_UPDATE_INTERVAL);
1344 unregister_switchdev_blocking_notifier(&br_offloads->switchdev_blk);
1345 err_reg_switchdev_blk:
1346 unregister_switchdev_notifier(&br_offloads->switchdev_nb);
1347 err_reg_switchdev_nb:
1348 destroy_workqueue(br_offloads->wq);
1351 ice_eswitch_br_offloads_dealloc(pf);