1 // SPDX-License-Identifier: GPL-2.0
3 * DPAA2 Ethernet Switch driver
5 * Copyright 2014-2016 Freescale Semiconductor Inc.
6 * Copyright 2017-2021 NXP
10 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/kthread.h>
14 #include <linux/workqueue.h>
15 #include <linux/iommu.h>
16 #include <net/pkt_cls.h>
18 #include <linux/fsl/mc.h>
20 #include "dpaa2-switch.h"
22 /* Minimal supported DPSW version */
23 #define DPSW_MIN_VER_MAJOR 8
24 #define DPSW_MIN_VER_MINOR 9
26 #define DEFAULT_VLAN_ID 1
28 static u16 dpaa2_switch_port_get_fdb_id(struct ethsw_port_priv *port_priv)
30 return port_priv->fdb->fdb_id;
33 static struct dpaa2_switch_fdb *dpaa2_switch_fdb_get_unused(struct ethsw_core *ethsw)
37 for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
38 if (!ethsw->fdbs[i].in_use)
39 return ðsw->fdbs[i];
43 static struct dpaa2_switch_filter_block *
44 dpaa2_switch_filter_block_get_unused(struct ethsw_core *ethsw)
48 for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
49 if (!ethsw->filter_blocks[i].in_use)
50 return ðsw->filter_blocks[i];
54 static u16 dpaa2_switch_port_set_fdb(struct ethsw_port_priv *port_priv,
55 struct net_device *bridge_dev)
57 struct ethsw_port_priv *other_port_priv = NULL;
58 struct dpaa2_switch_fdb *fdb;
59 struct net_device *other_dev;
60 struct list_head *iter;
62 /* If we leave a bridge (bridge_dev is NULL), find an unused
66 fdb = dpaa2_switch_fdb_get_unused(port_priv->ethsw_data);
68 /* If there is no unused FDB, we must be the last port that
69 * leaves the last bridge, all the others are standalone. We
70 * can just keep the FDB that we already have.
74 port_priv->fdb->bridge_dev = NULL;
79 port_priv->fdb->in_use = true;
80 port_priv->fdb->bridge_dev = NULL;
84 /* The below call to netdev_for_each_lower_dev() demands the RTNL lock
85 * being held. Assert on it so that it's easier to catch new code
86 * paths that reach this point without the RTNL lock.
90 /* If part of a bridge, use the FDB of the first dpaa2 switch interface
91 * to be present in that bridge
93 netdev_for_each_lower_dev(bridge_dev, other_dev, iter) {
94 if (!dpaa2_switch_port_dev_check(other_dev))
97 if (other_dev == port_priv->netdev)
100 other_port_priv = netdev_priv(other_dev);
104 /* The current port is about to change its FDB to the one used by the
105 * first port that joined the bridge.
107 if (other_port_priv) {
108 /* The previous FDB is about to become unused, since the
109 * interface is no longer standalone.
111 port_priv->fdb->in_use = false;
112 port_priv->fdb->bridge_dev = NULL;
114 /* Get a reference to the new FDB */
115 port_priv->fdb = other_port_priv->fdb;
118 /* Keep track of the new upper bridge device */
119 port_priv->fdb->bridge_dev = bridge_dev;
124 static void dpaa2_switch_fdb_get_flood_cfg(struct ethsw_core *ethsw, u16 fdb_id,
125 enum dpsw_flood_type type,
126 struct dpsw_egress_flood_cfg *cfg)
130 memset(cfg, 0, sizeof(*cfg));
132 /* Add all the DPAA2 switch ports found in the same bridging domain to
133 * the egress flooding domain
135 for (j = 0; j < ethsw->sw_attr.num_ifs; j++) {
136 if (!ethsw->ports[j])
138 if (ethsw->ports[j]->fdb->fdb_id != fdb_id)
141 if (type == DPSW_BROADCAST && ethsw->ports[j]->bcast_flood)
142 cfg->if_id[i++] = ethsw->ports[j]->idx;
143 else if (type == DPSW_FLOODING && ethsw->ports[j]->ucast_flood)
144 cfg->if_id[i++] = ethsw->ports[j]->idx;
147 /* Add the CTRL interface to the egress flooding domain */
148 cfg->if_id[i++] = ethsw->sw_attr.num_ifs;
150 cfg->fdb_id = fdb_id;
151 cfg->flood_type = type;
155 static int dpaa2_switch_fdb_set_egress_flood(struct ethsw_core *ethsw, u16 fdb_id)
157 struct dpsw_egress_flood_cfg flood_cfg;
160 /* Setup broadcast flooding domain */
161 dpaa2_switch_fdb_get_flood_cfg(ethsw, fdb_id, DPSW_BROADCAST, &flood_cfg);
162 err = dpsw_set_egress_flood(ethsw->mc_io, 0, ethsw->dpsw_handle,
165 dev_err(ethsw->dev, "dpsw_set_egress_flood() = %d\n", err);
169 /* Setup unknown flooding domain */
170 dpaa2_switch_fdb_get_flood_cfg(ethsw, fdb_id, DPSW_FLOODING, &flood_cfg);
171 err = dpsw_set_egress_flood(ethsw->mc_io, 0, ethsw->dpsw_handle,
174 dev_err(ethsw->dev, "dpsw_set_egress_flood() = %d\n", err);
181 static void *dpaa2_iova_to_virt(struct iommu_domain *domain,
182 dma_addr_t iova_addr)
184 phys_addr_t phys_addr;
186 phys_addr = domain ? iommu_iova_to_phys(domain, iova_addr) : iova_addr;
188 return phys_to_virt(phys_addr);
191 static int dpaa2_switch_add_vlan(struct ethsw_port_priv *port_priv, u16 vid)
193 struct ethsw_core *ethsw = port_priv->ethsw_data;
194 struct dpsw_vlan_cfg vcfg = {0};
197 vcfg.fdb_id = dpaa2_switch_port_get_fdb_id(port_priv);
198 err = dpsw_vlan_add(ethsw->mc_io, 0,
199 ethsw->dpsw_handle, vid, &vcfg);
201 dev_err(ethsw->dev, "dpsw_vlan_add err %d\n", err);
204 ethsw->vlans[vid] = ETHSW_VLAN_MEMBER;
209 static bool dpaa2_switch_port_is_up(struct ethsw_port_priv *port_priv)
211 struct net_device *netdev = port_priv->netdev;
212 struct dpsw_link_state state;
215 err = dpsw_if_get_link_state(port_priv->ethsw_data->mc_io, 0,
216 port_priv->ethsw_data->dpsw_handle,
217 port_priv->idx, &state);
219 netdev_err(netdev, "dpsw_if_get_link_state() err %d\n", err);
223 WARN_ONCE(state.up > 1, "Garbage read into link_state");
225 return state.up ? true : false;
228 static int dpaa2_switch_port_set_pvid(struct ethsw_port_priv *port_priv, u16 pvid)
230 struct ethsw_core *ethsw = port_priv->ethsw_data;
231 struct net_device *netdev = port_priv->netdev;
232 struct dpsw_tci_cfg tci_cfg = { 0 };
236 err = dpsw_if_get_tci(ethsw->mc_io, 0, ethsw->dpsw_handle,
237 port_priv->idx, &tci_cfg);
239 netdev_err(netdev, "dpsw_if_get_tci err %d\n", err);
243 tci_cfg.vlan_id = pvid;
245 /* Interface needs to be down to change PVID */
246 up = dpaa2_switch_port_is_up(port_priv);
248 err = dpsw_if_disable(ethsw->mc_io, 0,
252 netdev_err(netdev, "dpsw_if_disable err %d\n", err);
257 err = dpsw_if_set_tci(ethsw->mc_io, 0, ethsw->dpsw_handle,
258 port_priv->idx, &tci_cfg);
260 netdev_err(netdev, "dpsw_if_set_tci err %d\n", err);
264 /* Delete previous PVID info and mark the new one */
265 port_priv->vlans[port_priv->pvid] &= ~ETHSW_VLAN_PVID;
266 port_priv->vlans[pvid] |= ETHSW_VLAN_PVID;
267 port_priv->pvid = pvid;
271 ret = dpsw_if_enable(ethsw->mc_io, 0,
275 netdev_err(netdev, "dpsw_if_enable err %d\n", ret);
283 static int dpaa2_switch_port_add_vlan(struct ethsw_port_priv *port_priv,
286 struct ethsw_core *ethsw = port_priv->ethsw_data;
287 struct net_device *netdev = port_priv->netdev;
288 struct dpsw_vlan_if_cfg vcfg = {0};
291 if (port_priv->vlans[vid]) {
292 netdev_err(netdev, "VLAN %d already configured\n", vid);
296 /* If hit, this VLAN rule will lead the packet into the FDB table
297 * specified in the vlan configuration below
300 vcfg.if_id[0] = port_priv->idx;
301 vcfg.fdb_id = dpaa2_switch_port_get_fdb_id(port_priv);
302 vcfg.options |= DPSW_VLAN_ADD_IF_OPT_FDB_ID;
303 err = dpsw_vlan_add_if(ethsw->mc_io, 0, ethsw->dpsw_handle, vid, &vcfg);
305 netdev_err(netdev, "dpsw_vlan_add_if err %d\n", err);
309 port_priv->vlans[vid] = ETHSW_VLAN_MEMBER;
311 if (flags & BRIDGE_VLAN_INFO_UNTAGGED) {
312 err = dpsw_vlan_add_if_untagged(ethsw->mc_io, 0,
317 "dpsw_vlan_add_if_untagged err %d\n", err);
320 port_priv->vlans[vid] |= ETHSW_VLAN_UNTAGGED;
323 if (flags & BRIDGE_VLAN_INFO_PVID) {
324 err = dpaa2_switch_port_set_pvid(port_priv, vid);
332 static enum dpsw_stp_state br_stp_state_to_dpsw(u8 state)
335 case BR_STATE_DISABLED:
336 return DPSW_STP_STATE_DISABLED;
337 case BR_STATE_LISTENING:
338 return DPSW_STP_STATE_LISTENING;
339 case BR_STATE_LEARNING:
340 return DPSW_STP_STATE_LEARNING;
341 case BR_STATE_FORWARDING:
342 return DPSW_STP_STATE_FORWARDING;
343 case BR_STATE_BLOCKING:
344 return DPSW_STP_STATE_BLOCKING;
346 return DPSW_STP_STATE_DISABLED;
350 static int dpaa2_switch_port_set_stp_state(struct ethsw_port_priv *port_priv, u8 state)
352 struct dpsw_stp_cfg stp_cfg = {0};
356 if (!netif_running(port_priv->netdev) || state == port_priv->stp_state)
357 return 0; /* Nothing to do */
359 stp_cfg.state = br_stp_state_to_dpsw(state);
360 for (vid = 0; vid <= VLAN_VID_MASK; vid++) {
361 if (port_priv->vlans[vid] & ETHSW_VLAN_MEMBER) {
362 stp_cfg.vlan_id = vid;
363 err = dpsw_if_set_stp(port_priv->ethsw_data->mc_io, 0,
364 port_priv->ethsw_data->dpsw_handle,
365 port_priv->idx, &stp_cfg);
367 netdev_err(port_priv->netdev,
368 "dpsw_if_set_stp err %d\n", err);
374 port_priv->stp_state = state;
379 static int dpaa2_switch_dellink(struct ethsw_core *ethsw, u16 vid)
381 struct ethsw_port_priv *ppriv_local = NULL;
384 if (!ethsw->vlans[vid])
387 err = dpsw_vlan_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, vid);
389 dev_err(ethsw->dev, "dpsw_vlan_remove err %d\n", err);
392 ethsw->vlans[vid] = 0;
394 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
395 ppriv_local = ethsw->ports[i];
397 ppriv_local->vlans[vid] = 0;
403 static int dpaa2_switch_port_fdb_add_uc(struct ethsw_port_priv *port_priv,
404 const unsigned char *addr)
406 struct dpsw_fdb_unicast_cfg entry = {0};
410 entry.if_egress = port_priv->idx;
411 entry.type = DPSW_FDB_ENTRY_STATIC;
412 ether_addr_copy(entry.mac_addr, addr);
414 fdb_id = dpaa2_switch_port_get_fdb_id(port_priv);
415 err = dpsw_fdb_add_unicast(port_priv->ethsw_data->mc_io, 0,
416 port_priv->ethsw_data->dpsw_handle,
419 netdev_err(port_priv->netdev,
420 "dpsw_fdb_add_unicast err %d\n", err);
424 static int dpaa2_switch_port_fdb_del_uc(struct ethsw_port_priv *port_priv,
425 const unsigned char *addr)
427 struct dpsw_fdb_unicast_cfg entry = {0};
431 entry.if_egress = port_priv->idx;
432 entry.type = DPSW_FDB_ENTRY_STATIC;
433 ether_addr_copy(entry.mac_addr, addr);
435 fdb_id = dpaa2_switch_port_get_fdb_id(port_priv);
436 err = dpsw_fdb_remove_unicast(port_priv->ethsw_data->mc_io, 0,
437 port_priv->ethsw_data->dpsw_handle,
439 /* Silently discard error for calling multiple times the del command */
440 if (err && err != -ENXIO)
441 netdev_err(port_priv->netdev,
442 "dpsw_fdb_remove_unicast err %d\n", err);
446 static int dpaa2_switch_port_fdb_add_mc(struct ethsw_port_priv *port_priv,
447 const unsigned char *addr)
449 struct dpsw_fdb_multicast_cfg entry = {0};
453 ether_addr_copy(entry.mac_addr, addr);
454 entry.type = DPSW_FDB_ENTRY_STATIC;
456 entry.if_id[0] = port_priv->idx;
458 fdb_id = dpaa2_switch_port_get_fdb_id(port_priv);
459 err = dpsw_fdb_add_multicast(port_priv->ethsw_data->mc_io, 0,
460 port_priv->ethsw_data->dpsw_handle,
462 /* Silently discard error for calling multiple times the add command */
463 if (err && err != -ENXIO)
464 netdev_err(port_priv->netdev, "dpsw_fdb_add_multicast err %d\n",
469 static int dpaa2_switch_port_fdb_del_mc(struct ethsw_port_priv *port_priv,
470 const unsigned char *addr)
472 struct dpsw_fdb_multicast_cfg entry = {0};
476 ether_addr_copy(entry.mac_addr, addr);
477 entry.type = DPSW_FDB_ENTRY_STATIC;
479 entry.if_id[0] = port_priv->idx;
481 fdb_id = dpaa2_switch_port_get_fdb_id(port_priv);
482 err = dpsw_fdb_remove_multicast(port_priv->ethsw_data->mc_io, 0,
483 port_priv->ethsw_data->dpsw_handle,
485 /* Silently discard error for calling multiple times the del command */
486 if (err && err != -ENAVAIL)
487 netdev_err(port_priv->netdev,
488 "dpsw_fdb_remove_multicast err %d\n", err);
492 static void dpaa2_switch_port_get_stats(struct net_device *netdev,
493 struct rtnl_link_stats64 *stats)
495 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
499 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
500 port_priv->ethsw_data->dpsw_handle,
502 DPSW_CNT_ING_FRAME, &stats->rx_packets);
506 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
507 port_priv->ethsw_data->dpsw_handle,
509 DPSW_CNT_EGR_FRAME, &stats->tx_packets);
513 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
514 port_priv->ethsw_data->dpsw_handle,
516 DPSW_CNT_ING_BYTE, &stats->rx_bytes);
520 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
521 port_priv->ethsw_data->dpsw_handle,
523 DPSW_CNT_EGR_BYTE, &stats->tx_bytes);
527 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
528 port_priv->ethsw_data->dpsw_handle,
530 DPSW_CNT_ING_FRAME_DISCARD,
535 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
536 port_priv->ethsw_data->dpsw_handle,
538 DPSW_CNT_ING_FLTR_FRAME,
542 stats->rx_dropped += tmp;
544 err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
545 port_priv->ethsw_data->dpsw_handle,
547 DPSW_CNT_EGR_FRAME_DISCARD,
555 netdev_err(netdev, "dpsw_if_get_counter err %d\n", err);
558 static bool dpaa2_switch_port_has_offload_stats(const struct net_device *netdev,
561 return (attr_id == IFLA_OFFLOAD_XSTATS_CPU_HIT);
564 static int dpaa2_switch_port_get_offload_stats(int attr_id,
565 const struct net_device *netdev,
569 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
570 dpaa2_switch_port_get_stats((struct net_device *)netdev, sp);
577 static int dpaa2_switch_port_change_mtu(struct net_device *netdev, int mtu)
579 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
582 err = dpsw_if_set_max_frame_length(port_priv->ethsw_data->mc_io,
584 port_priv->ethsw_data->dpsw_handle,
586 (u16)ETHSW_L2_MAX_FRM(mtu));
589 "dpsw_if_set_max_frame_length() err %d\n", err);
597 static int dpaa2_switch_port_link_state_update(struct net_device *netdev)
599 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
600 struct dpsw_link_state state;
603 /* When we manage the MAC/PHY using phylink there is no need
604 * to manually update the netif_carrier.
605 * We can avoid locking because we are called from the "link changed"
606 * IRQ handler, which is the same as the "endpoint changed" IRQ handler
607 * (the writer to port_priv->mac), so we cannot race with it.
609 if (dpaa2_mac_is_type_phy(port_priv->mac))
612 /* Interrupts are received even though no one issued an 'ifconfig up'
613 * on the switch interface. Ignore these link state update interrupts
615 if (!netif_running(netdev))
618 err = dpsw_if_get_link_state(port_priv->ethsw_data->mc_io, 0,
619 port_priv->ethsw_data->dpsw_handle,
620 port_priv->idx, &state);
622 netdev_err(netdev, "dpsw_if_get_link_state() err %d\n", err);
626 WARN_ONCE(state.up > 1, "Garbage read into link_state");
628 if (state.up != port_priv->link_state) {
630 netif_carrier_on(netdev);
631 netif_tx_start_all_queues(netdev);
633 netif_carrier_off(netdev);
634 netif_tx_stop_all_queues(netdev);
636 port_priv->link_state = state.up;
642 /* Manage all NAPI instances for the control interface.
644 * We only have one RX queue and one Tx Conf queue for all
645 * switch ports. Therefore, we only need to enable the NAPI instance once, the
646 * first time one of the switch ports runs .dev_open().
649 static void dpaa2_switch_enable_ctrl_if_napi(struct ethsw_core *ethsw)
653 /* Access to the ethsw->napi_users relies on the RTNL lock */
656 /* a new interface is using the NAPI instance */
659 /* if there is already a user of the instance, return */
660 if (ethsw->napi_users > 1)
663 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++)
664 napi_enable(ðsw->fq[i].napi);
667 static void dpaa2_switch_disable_ctrl_if_napi(struct ethsw_core *ethsw)
671 /* Access to the ethsw->napi_users relies on the RTNL lock */
674 /* If we are not the last interface using the NAPI, return */
676 if (ethsw->napi_users)
679 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++)
680 napi_disable(ðsw->fq[i].napi);
683 static int dpaa2_switch_port_open(struct net_device *netdev)
685 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
686 struct ethsw_core *ethsw = port_priv->ethsw_data;
689 mutex_lock(&port_priv->mac_lock);
691 if (!dpaa2_switch_port_is_type_phy(port_priv)) {
692 /* Explicitly set carrier off, otherwise
693 * netif_carrier_ok() will return true and cause 'ip link show'
694 * to report the LOWER_UP flag, even though the link
695 * notification wasn't even received.
697 netif_carrier_off(netdev);
700 err = dpsw_if_enable(port_priv->ethsw_data->mc_io, 0,
701 port_priv->ethsw_data->dpsw_handle,
704 mutex_unlock(&port_priv->mac_lock);
705 netdev_err(netdev, "dpsw_if_enable err %d\n", err);
709 dpaa2_switch_enable_ctrl_if_napi(ethsw);
711 if (dpaa2_switch_port_is_type_phy(port_priv))
712 dpaa2_mac_start(port_priv->mac);
714 mutex_unlock(&port_priv->mac_lock);
719 static int dpaa2_switch_port_stop(struct net_device *netdev)
721 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
722 struct ethsw_core *ethsw = port_priv->ethsw_data;
725 mutex_lock(&port_priv->mac_lock);
727 if (dpaa2_switch_port_is_type_phy(port_priv)) {
728 dpaa2_mac_stop(port_priv->mac);
730 netif_tx_stop_all_queues(netdev);
731 netif_carrier_off(netdev);
734 mutex_unlock(&port_priv->mac_lock);
736 err = dpsw_if_disable(port_priv->ethsw_data->mc_io, 0,
737 port_priv->ethsw_data->dpsw_handle,
740 netdev_err(netdev, "dpsw_if_disable err %d\n", err);
744 dpaa2_switch_disable_ctrl_if_napi(ethsw);
749 static int dpaa2_switch_port_parent_id(struct net_device *dev,
750 struct netdev_phys_item_id *ppid)
752 struct ethsw_port_priv *port_priv = netdev_priv(dev);
755 ppid->id[0] = port_priv->ethsw_data->dev_id;
760 static int dpaa2_switch_port_get_phys_name(struct net_device *netdev, char *name,
763 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
766 err = snprintf(name, len, "p%d", port_priv->idx);
773 struct ethsw_dump_ctx {
774 struct net_device *dev;
776 struct netlink_callback *cb;
780 static int dpaa2_switch_fdb_dump_nl(struct fdb_dump_entry *entry,
781 struct ethsw_dump_ctx *dump)
783 int is_dynamic = entry->type & DPSW_FDB_ENTRY_DINAMIC;
784 u32 portid = NETLINK_CB(dump->cb->skb).portid;
785 u32 seq = dump->cb->nlh->nlmsg_seq;
786 struct nlmsghdr *nlh;
789 if (dump->idx < dump->cb->args[2])
792 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
793 sizeof(*ndm), NLM_F_MULTI);
797 ndm = nlmsg_data(nlh);
798 ndm->ndm_family = AF_BRIDGE;
801 ndm->ndm_flags = NTF_SELF;
803 ndm->ndm_ifindex = dump->dev->ifindex;
804 ndm->ndm_state = is_dynamic ? NUD_REACHABLE : NUD_NOARP;
806 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac_addr))
807 goto nla_put_failure;
809 nlmsg_end(dump->skb, nlh);
816 nlmsg_cancel(dump->skb, nlh);
820 static int dpaa2_switch_port_fdb_valid_entry(struct fdb_dump_entry *entry,
821 struct ethsw_port_priv *port_priv)
823 int idx = port_priv->idx;
826 if (entry->type & DPSW_FDB_ENTRY_TYPE_UNICAST)
827 valid = entry->if_info == port_priv->idx;
829 valid = entry->if_mask[idx / 8] & BIT(idx % 8);
834 static int dpaa2_switch_fdb_iterate(struct ethsw_port_priv *port_priv,
835 dpaa2_switch_fdb_cb_t cb, void *data)
837 struct net_device *net_dev = port_priv->netdev;
838 struct ethsw_core *ethsw = port_priv->ethsw_data;
839 struct device *dev = net_dev->dev.parent;
840 struct fdb_dump_entry *fdb_entries;
841 struct fdb_dump_entry fdb_entry;
842 dma_addr_t fdb_dump_iova;
849 fdb_dump_size = ethsw->sw_attr.max_fdb_entries * sizeof(fdb_entry);
850 dma_mem = kzalloc(fdb_dump_size, GFP_KERNEL);
854 fdb_dump_iova = dma_map_single(dev, dma_mem, fdb_dump_size,
856 if (dma_mapping_error(dev, fdb_dump_iova)) {
857 netdev_err(net_dev, "dma_map_single() failed\n");
862 fdb_id = dpaa2_switch_port_get_fdb_id(port_priv);
863 err = dpsw_fdb_dump(ethsw->mc_io, 0, ethsw->dpsw_handle, fdb_id,
864 fdb_dump_iova, fdb_dump_size, &num_fdb_entries);
866 netdev_err(net_dev, "dpsw_fdb_dump() = %d\n", err);
870 dma_unmap_single(dev, fdb_dump_iova, fdb_dump_size, DMA_FROM_DEVICE);
872 fdb_entries = (struct fdb_dump_entry *)dma_mem;
873 for (i = 0; i < num_fdb_entries; i++) {
874 fdb_entry = fdb_entries[i];
876 err = cb(port_priv, &fdb_entry, data);
887 dma_unmap_single(dev, fdb_dump_iova, fdb_dump_size, DMA_TO_DEVICE);
893 static int dpaa2_switch_fdb_entry_dump(struct ethsw_port_priv *port_priv,
894 struct fdb_dump_entry *fdb_entry,
897 if (!dpaa2_switch_port_fdb_valid_entry(fdb_entry, port_priv))
900 return dpaa2_switch_fdb_dump_nl(fdb_entry, data);
903 static int dpaa2_switch_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
904 struct net_device *net_dev,
905 struct net_device *filter_dev, int *idx)
907 struct ethsw_port_priv *port_priv = netdev_priv(net_dev);
908 struct ethsw_dump_ctx dump = {
916 err = dpaa2_switch_fdb_iterate(port_priv, dpaa2_switch_fdb_entry_dump, &dump);
922 static int dpaa2_switch_fdb_entry_fast_age(struct ethsw_port_priv *port_priv,
923 struct fdb_dump_entry *fdb_entry,
924 void *data __always_unused)
926 if (!dpaa2_switch_port_fdb_valid_entry(fdb_entry, port_priv))
929 if (!(fdb_entry->type & DPSW_FDB_ENTRY_TYPE_DYNAMIC))
932 if (fdb_entry->type & DPSW_FDB_ENTRY_TYPE_UNICAST)
933 dpaa2_switch_port_fdb_del_uc(port_priv, fdb_entry->mac_addr);
935 dpaa2_switch_port_fdb_del_mc(port_priv, fdb_entry->mac_addr);
940 static void dpaa2_switch_port_fast_age(struct ethsw_port_priv *port_priv)
942 dpaa2_switch_fdb_iterate(port_priv,
943 dpaa2_switch_fdb_entry_fast_age, NULL);
946 static int dpaa2_switch_port_vlan_add(struct net_device *netdev, __be16 proto,
949 struct switchdev_obj_port_vlan vlan = {
950 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
952 .obj.orig_dev = netdev,
953 /* This API only allows programming tagged, non-PVID VIDs */
957 return dpaa2_switch_port_vlans_add(netdev, &vlan);
960 static int dpaa2_switch_port_vlan_kill(struct net_device *netdev, __be16 proto,
963 struct switchdev_obj_port_vlan vlan = {
964 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
966 .obj.orig_dev = netdev,
967 /* This API only allows programming tagged, non-PVID VIDs */
971 return dpaa2_switch_port_vlans_del(netdev, &vlan);
974 static int dpaa2_switch_port_set_mac_addr(struct ethsw_port_priv *port_priv)
976 struct ethsw_core *ethsw = port_priv->ethsw_data;
977 struct net_device *net_dev = port_priv->netdev;
978 struct device *dev = net_dev->dev.parent;
979 u8 mac_addr[ETH_ALEN];
982 if (!(ethsw->features & ETHSW_FEATURE_MAC_ADDR))
985 /* Get firmware address, if any */
986 err = dpsw_if_get_port_mac_addr(ethsw->mc_io, 0, ethsw->dpsw_handle,
987 port_priv->idx, mac_addr);
989 dev_err(dev, "dpsw_if_get_port_mac_addr() failed\n");
993 /* First check if firmware has any address configured by bootloader */
994 if (!is_zero_ether_addr(mac_addr)) {
995 eth_hw_addr_set(net_dev, mac_addr);
997 /* No MAC address configured, fill in net_dev->dev_addr
1000 eth_hw_addr_random(net_dev);
1001 dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
1003 /* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
1004 * practical purposes, this will be our "permanent" mac address,
1005 * at least until the next reboot. This move will also permit
1006 * register_netdevice() to properly fill up net_dev->perm_addr.
1008 net_dev->addr_assign_type = NET_ADDR_PERM;
1014 static void dpaa2_switch_free_fd(const struct ethsw_core *ethsw,
1015 const struct dpaa2_fd *fd)
1017 struct device *dev = ethsw->dev;
1018 unsigned char *buffer_start;
1019 struct sk_buff **skbh, *skb;
1022 fd_addr = dpaa2_fd_get_addr(fd);
1023 skbh = dpaa2_iova_to_virt(ethsw->iommu_domain, fd_addr);
1026 buffer_start = (unsigned char *)skbh;
1028 dma_unmap_single(dev, fd_addr,
1029 skb_tail_pointer(skb) - buffer_start,
1032 /* Move on with skb release */
1036 static int dpaa2_switch_build_single_fd(struct ethsw_core *ethsw,
1037 struct sk_buff *skb,
1038 struct dpaa2_fd *fd)
1040 struct device *dev = ethsw->dev;
1041 struct sk_buff **skbh;
1046 buff_start = PTR_ALIGN(skb->data - DPAA2_SWITCH_TX_DATA_OFFSET -
1047 DPAA2_SWITCH_TX_BUF_ALIGN,
1048 DPAA2_SWITCH_TX_BUF_ALIGN);
1050 /* Clear FAS to have consistent values for TX confirmation. It is
1051 * located in the first 8 bytes of the buffer's hardware annotation
1054 hwa = buff_start + DPAA2_SWITCH_SWA_SIZE;
1057 /* Store a backpointer to the skb at the beginning of the buffer
1058 * (in the private data area) such that we can release it
1061 skbh = (struct sk_buff **)buff_start;
1064 addr = dma_map_single(dev, buff_start,
1065 skb_tail_pointer(skb) - buff_start,
1067 if (unlikely(dma_mapping_error(dev, addr)))
1070 /* Setup the FD fields */
1071 memset(fd, 0, sizeof(*fd));
1073 dpaa2_fd_set_addr(fd, addr);
1074 dpaa2_fd_set_offset(fd, (u16)(skb->data - buff_start));
1075 dpaa2_fd_set_len(fd, skb->len);
1076 dpaa2_fd_set_format(fd, dpaa2_fd_single);
1081 static netdev_tx_t dpaa2_switch_port_tx(struct sk_buff *skb,
1082 struct net_device *net_dev)
1084 struct ethsw_port_priv *port_priv = netdev_priv(net_dev);
1085 struct ethsw_core *ethsw = port_priv->ethsw_data;
1086 int retries = DPAA2_SWITCH_SWP_BUSY_RETRIES;
1090 if (unlikely(skb_headroom(skb) < DPAA2_SWITCH_NEEDED_HEADROOM)) {
1093 ns = skb_realloc_headroom(skb, DPAA2_SWITCH_NEEDED_HEADROOM);
1094 if (unlikely(!ns)) {
1095 net_err_ratelimited("%s: Error reallocating skb headroom\n", net_dev->name);
1098 dev_consume_skb_any(skb);
1102 /* We'll be holding a back-reference to the skb until Tx confirmation */
1103 skb = skb_unshare(skb, GFP_ATOMIC);
1104 if (unlikely(!skb)) {
1105 /* skb_unshare() has already freed the skb */
1106 net_err_ratelimited("%s: Error copying the socket buffer\n", net_dev->name);
1110 /* At this stage, we do not support non-linear skbs so just try to
1111 * linearize the skb and if that's not working, just drop the packet.
1113 err = skb_linearize(skb);
1115 net_err_ratelimited("%s: skb_linearize error (%d)!\n", net_dev->name, err);
1119 err = dpaa2_switch_build_single_fd(ethsw, skb, &fd);
1120 if (unlikely(err)) {
1121 net_err_ratelimited("%s: ethsw_build_*_fd() %d\n", net_dev->name, err);
1126 err = dpaa2_io_service_enqueue_qd(NULL,
1130 } while (err == -EBUSY && retries);
1132 if (unlikely(err < 0)) {
1133 dpaa2_switch_free_fd(ethsw, &fd);
1137 return NETDEV_TX_OK;
1142 return NETDEV_TX_OK;
1146 dpaa2_switch_setup_tc_cls_flower(struct dpaa2_switch_filter_block *filter_block,
1147 struct flow_cls_offload *f)
1149 switch (f->command) {
1150 case FLOW_CLS_REPLACE:
1151 return dpaa2_switch_cls_flower_replace(filter_block, f);
1152 case FLOW_CLS_DESTROY:
1153 return dpaa2_switch_cls_flower_destroy(filter_block, f);
1160 dpaa2_switch_setup_tc_cls_matchall(struct dpaa2_switch_filter_block *block,
1161 struct tc_cls_matchall_offload *f)
1163 switch (f->command) {
1164 case TC_CLSMATCHALL_REPLACE:
1165 return dpaa2_switch_cls_matchall_replace(block, f);
1166 case TC_CLSMATCHALL_DESTROY:
1167 return dpaa2_switch_cls_matchall_destroy(block, f);
1173 static int dpaa2_switch_port_setup_tc_block_cb_ig(enum tc_setup_type type,
1178 case TC_SETUP_CLSFLOWER:
1179 return dpaa2_switch_setup_tc_cls_flower(cb_priv, type_data);
1180 case TC_SETUP_CLSMATCHALL:
1181 return dpaa2_switch_setup_tc_cls_matchall(cb_priv, type_data);
1187 static LIST_HEAD(dpaa2_switch_block_cb_list);
1190 dpaa2_switch_port_acl_tbl_bind(struct ethsw_port_priv *port_priv,
1191 struct dpaa2_switch_filter_block *block)
1193 struct ethsw_core *ethsw = port_priv->ethsw_data;
1194 struct net_device *netdev = port_priv->netdev;
1195 struct dpsw_acl_if_cfg acl_if_cfg;
1198 if (port_priv->filter_block)
1201 acl_if_cfg.if_id[0] = port_priv->idx;
1202 acl_if_cfg.num_ifs = 1;
1203 err = dpsw_acl_add_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
1204 block->acl_id, &acl_if_cfg);
1206 netdev_err(netdev, "dpsw_acl_add_if err %d\n", err);
1210 block->ports |= BIT(port_priv->idx);
1211 port_priv->filter_block = block;
1217 dpaa2_switch_port_acl_tbl_unbind(struct ethsw_port_priv *port_priv,
1218 struct dpaa2_switch_filter_block *block)
1220 struct ethsw_core *ethsw = port_priv->ethsw_data;
1221 struct net_device *netdev = port_priv->netdev;
1222 struct dpsw_acl_if_cfg acl_if_cfg;
1225 if (port_priv->filter_block != block)
1228 acl_if_cfg.if_id[0] = port_priv->idx;
1229 acl_if_cfg.num_ifs = 1;
1230 err = dpsw_acl_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
1231 block->acl_id, &acl_if_cfg);
1233 netdev_err(netdev, "dpsw_acl_add_if err %d\n", err);
1237 block->ports &= ~BIT(port_priv->idx);
1238 port_priv->filter_block = NULL;
1242 static int dpaa2_switch_port_block_bind(struct ethsw_port_priv *port_priv,
1243 struct dpaa2_switch_filter_block *block)
1245 struct dpaa2_switch_filter_block *old_block = port_priv->filter_block;
1248 /* Offload all the mirror entries found in the block on this new port
1251 err = dpaa2_switch_block_offload_mirror(block, port_priv);
1255 /* If the port is already bound to this ACL table then do nothing. This
1256 * can happen when this port is the first one to join a tc block
1258 if (port_priv->filter_block == block)
1261 err = dpaa2_switch_port_acl_tbl_unbind(port_priv, old_block);
1265 /* Mark the previous ACL table as being unused if this was the last
1266 * port that was using it.
1268 if (old_block->ports == 0)
1269 old_block->in_use = false;
1271 return dpaa2_switch_port_acl_tbl_bind(port_priv, block);
1275 dpaa2_switch_port_block_unbind(struct ethsw_port_priv *port_priv,
1276 struct dpaa2_switch_filter_block *block)
1278 struct ethsw_core *ethsw = port_priv->ethsw_data;
1279 struct dpaa2_switch_filter_block *new_block;
1282 /* Unoffload all the mirror entries found in the block from the
1285 err = dpaa2_switch_block_unoffload_mirror(block, port_priv);
1289 /* We are the last port that leaves a block (an ACL table).
1290 * We'll continue to use this table.
1292 if (block->ports == BIT(port_priv->idx))
1295 err = dpaa2_switch_port_acl_tbl_unbind(port_priv, block);
1299 if (block->ports == 0)
1300 block->in_use = false;
1302 new_block = dpaa2_switch_filter_block_get_unused(ethsw);
1303 new_block->in_use = true;
1304 return dpaa2_switch_port_acl_tbl_bind(port_priv, new_block);
1307 static int dpaa2_switch_setup_tc_block_bind(struct net_device *netdev,
1308 struct flow_block_offload *f)
1310 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1311 struct ethsw_core *ethsw = port_priv->ethsw_data;
1312 struct dpaa2_switch_filter_block *filter_block;
1313 struct flow_block_cb *block_cb;
1314 bool register_block = false;
1317 block_cb = flow_block_cb_lookup(f->block,
1318 dpaa2_switch_port_setup_tc_block_cb_ig,
1322 /* If the filter block is not already known, then this port
1323 * must be the first to join it. In this case, we can just
1324 * continue to use our private table
1326 filter_block = port_priv->filter_block;
1328 block_cb = flow_block_cb_alloc(dpaa2_switch_port_setup_tc_block_cb_ig,
1329 ethsw, filter_block, NULL);
1330 if (IS_ERR(block_cb))
1331 return PTR_ERR(block_cb);
1333 register_block = true;
1335 filter_block = flow_block_cb_priv(block_cb);
1338 flow_block_cb_incref(block_cb);
1339 err = dpaa2_switch_port_block_bind(port_priv, filter_block);
1341 goto err_block_bind;
1343 if (register_block) {
1344 flow_block_cb_add(block_cb, f);
1345 list_add_tail(&block_cb->driver_list,
1346 &dpaa2_switch_block_cb_list);
1352 if (!flow_block_cb_decref(block_cb))
1353 flow_block_cb_free(block_cb);
1357 static void dpaa2_switch_setup_tc_block_unbind(struct net_device *netdev,
1358 struct flow_block_offload *f)
1360 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1361 struct ethsw_core *ethsw = port_priv->ethsw_data;
1362 struct dpaa2_switch_filter_block *filter_block;
1363 struct flow_block_cb *block_cb;
1366 block_cb = flow_block_cb_lookup(f->block,
1367 dpaa2_switch_port_setup_tc_block_cb_ig,
1372 filter_block = flow_block_cb_priv(block_cb);
1373 err = dpaa2_switch_port_block_unbind(port_priv, filter_block);
1374 if (!err && !flow_block_cb_decref(block_cb)) {
1375 flow_block_cb_remove(block_cb, f);
1376 list_del(&block_cb->driver_list);
1380 static int dpaa2_switch_setup_tc_block(struct net_device *netdev,
1381 struct flow_block_offload *f)
1383 if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1386 f->driver_block_list = &dpaa2_switch_block_cb_list;
1388 switch (f->command) {
1389 case FLOW_BLOCK_BIND:
1390 return dpaa2_switch_setup_tc_block_bind(netdev, f);
1391 case FLOW_BLOCK_UNBIND:
1392 dpaa2_switch_setup_tc_block_unbind(netdev, f);
1399 static int dpaa2_switch_port_setup_tc(struct net_device *netdev,
1400 enum tc_setup_type type,
1404 case TC_SETUP_BLOCK: {
1405 return dpaa2_switch_setup_tc_block(netdev, type_data);
1414 static const struct net_device_ops dpaa2_switch_port_ops = {
1415 .ndo_open = dpaa2_switch_port_open,
1416 .ndo_stop = dpaa2_switch_port_stop,
1418 .ndo_set_mac_address = eth_mac_addr,
1419 .ndo_get_stats64 = dpaa2_switch_port_get_stats,
1420 .ndo_change_mtu = dpaa2_switch_port_change_mtu,
1421 .ndo_has_offload_stats = dpaa2_switch_port_has_offload_stats,
1422 .ndo_get_offload_stats = dpaa2_switch_port_get_offload_stats,
1423 .ndo_fdb_dump = dpaa2_switch_port_fdb_dump,
1424 .ndo_vlan_rx_add_vid = dpaa2_switch_port_vlan_add,
1425 .ndo_vlan_rx_kill_vid = dpaa2_switch_port_vlan_kill,
1427 .ndo_start_xmit = dpaa2_switch_port_tx,
1428 .ndo_get_port_parent_id = dpaa2_switch_port_parent_id,
1429 .ndo_get_phys_port_name = dpaa2_switch_port_get_phys_name,
1430 .ndo_setup_tc = dpaa2_switch_port_setup_tc,
1433 bool dpaa2_switch_port_dev_check(const struct net_device *netdev)
1435 return netdev->netdev_ops == &dpaa2_switch_port_ops;
1438 static int dpaa2_switch_port_connect_mac(struct ethsw_port_priv *port_priv)
1440 struct fsl_mc_device *dpsw_port_dev, *dpmac_dev;
1441 struct dpaa2_mac *mac;
1444 dpsw_port_dev = to_fsl_mc_device(port_priv->netdev->dev.parent);
1445 dpmac_dev = fsl_mc_get_endpoint(dpsw_port_dev, port_priv->idx);
1447 if (PTR_ERR(dpmac_dev) == -EPROBE_DEFER)
1448 return PTR_ERR(dpmac_dev);
1450 if (IS_ERR(dpmac_dev) || dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type)
1453 mac = kzalloc(sizeof(*mac), GFP_KERNEL);
1457 mac->mc_dev = dpmac_dev;
1458 mac->mc_io = port_priv->ethsw_data->mc_io;
1459 mac->net_dev = port_priv->netdev;
1461 err = dpaa2_mac_open(mac);
1465 if (dpaa2_mac_is_type_phy(mac)) {
1466 err = dpaa2_mac_connect(mac);
1468 netdev_err(port_priv->netdev,
1469 "Error connecting to the MAC endpoint %pe\n",
1475 mutex_lock(&port_priv->mac_lock);
1476 port_priv->mac = mac;
1477 mutex_unlock(&port_priv->mac_lock);
1482 dpaa2_mac_close(mac);
1488 static void dpaa2_switch_port_disconnect_mac(struct ethsw_port_priv *port_priv)
1490 struct dpaa2_mac *mac;
1492 mutex_lock(&port_priv->mac_lock);
1493 mac = port_priv->mac;
1494 port_priv->mac = NULL;
1495 mutex_unlock(&port_priv->mac_lock);
1500 if (dpaa2_mac_is_type_phy(mac))
1501 dpaa2_mac_disconnect(mac);
1503 dpaa2_mac_close(mac);
1507 static irqreturn_t dpaa2_switch_irq0_handler_thread(int irq_num, void *arg)
1509 struct device *dev = (struct device *)arg;
1510 struct ethsw_core *ethsw = dev_get_drvdata(dev);
1511 struct ethsw_port_priv *port_priv;
1516 err = dpsw_get_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle,
1517 DPSW_IRQ_INDEX_IF, &status);
1519 dev_err(dev, "Can't get irq status (err %d)\n", err);
1523 if_id = (status & 0xFFFF0000) >> 16;
1524 port_priv = ethsw->ports[if_id];
1526 if (status & DPSW_IRQ_EVENT_LINK_CHANGED)
1527 dpaa2_switch_port_link_state_update(port_priv->netdev);
1529 if (status & DPSW_IRQ_EVENT_ENDPOINT_CHANGED) {
1530 dpaa2_switch_port_set_mac_addr(port_priv);
1531 /* We can avoid locking because the "endpoint changed" IRQ
1532 * handler is the only one who changes priv->mac at runtime,
1533 * so we are not racing with anyone.
1535 had_mac = !!port_priv->mac;
1537 dpaa2_switch_port_disconnect_mac(port_priv);
1539 dpaa2_switch_port_connect_mac(port_priv);
1542 err = dpsw_clear_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle,
1543 DPSW_IRQ_INDEX_IF, status);
1545 dev_err(dev, "Can't clear irq status (err %d)\n", err);
1551 static int dpaa2_switch_setup_irqs(struct fsl_mc_device *sw_dev)
1553 u32 mask = DPSW_IRQ_EVENT_LINK_CHANGED | DPSW_IRQ_EVENT_ENDPOINT_CHANGED;
1554 struct device *dev = &sw_dev->dev;
1555 struct ethsw_core *ethsw = dev_get_drvdata(dev);
1556 struct fsl_mc_device_irq *irq;
1559 err = fsl_mc_allocate_irqs(sw_dev);
1561 dev_err(dev, "MC irqs allocation failed\n");
1565 if (WARN_ON(sw_dev->obj_desc.irq_count != DPSW_IRQ_NUM)) {
1570 err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
1571 DPSW_IRQ_INDEX_IF, 0);
1573 dev_err(dev, "dpsw_set_irq_enable err %d\n", err);
1577 irq = sw_dev->irqs[DPSW_IRQ_INDEX_IF];
1579 err = devm_request_threaded_irq(dev, irq->virq, NULL,
1580 dpaa2_switch_irq0_handler_thread,
1581 IRQF_NO_SUSPEND | IRQF_ONESHOT,
1582 dev_name(dev), dev);
1584 dev_err(dev, "devm_request_threaded_irq(): %d\n", err);
1588 err = dpsw_set_irq_mask(ethsw->mc_io, 0, ethsw->dpsw_handle,
1589 DPSW_IRQ_INDEX_IF, mask);
1591 dev_err(dev, "dpsw_set_irq_mask(): %d\n", err);
1595 err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
1596 DPSW_IRQ_INDEX_IF, 1);
1598 dev_err(dev, "dpsw_set_irq_enable(): %d\n", err);
1605 devm_free_irq(dev, irq->virq, dev);
1607 fsl_mc_free_irqs(sw_dev);
1611 static void dpaa2_switch_teardown_irqs(struct fsl_mc_device *sw_dev)
1613 struct device *dev = &sw_dev->dev;
1614 struct ethsw_core *ethsw = dev_get_drvdata(dev);
1617 err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
1618 DPSW_IRQ_INDEX_IF, 0);
1620 dev_err(dev, "dpsw_set_irq_enable err %d\n", err);
1622 fsl_mc_free_irqs(sw_dev);
1625 static int dpaa2_switch_port_set_learning(struct ethsw_port_priv *port_priv, bool enable)
1627 struct ethsw_core *ethsw = port_priv->ethsw_data;
1628 enum dpsw_learning_mode learn_mode;
1632 learn_mode = DPSW_LEARNING_MODE_HW;
1634 learn_mode = DPSW_LEARNING_MODE_DIS;
1636 err = dpsw_if_set_learning_mode(ethsw->mc_io, 0, ethsw->dpsw_handle,
1637 port_priv->idx, learn_mode);
1639 netdev_err(port_priv->netdev, "dpsw_if_set_learning_mode err %d\n", err);
1642 dpaa2_switch_port_fast_age(port_priv);
1647 static int dpaa2_switch_port_attr_stp_state_set(struct net_device *netdev,
1650 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1653 err = dpaa2_switch_port_set_stp_state(port_priv, state);
1658 case BR_STATE_DISABLED:
1659 case BR_STATE_BLOCKING:
1660 case BR_STATE_LISTENING:
1661 err = dpaa2_switch_port_set_learning(port_priv, false);
1663 case BR_STATE_LEARNING:
1664 case BR_STATE_FORWARDING:
1665 err = dpaa2_switch_port_set_learning(port_priv,
1666 port_priv->learn_ena);
1673 static int dpaa2_switch_port_flood(struct ethsw_port_priv *port_priv,
1674 struct switchdev_brport_flags flags)
1676 struct ethsw_core *ethsw = port_priv->ethsw_data;
1678 if (flags.mask & BR_BCAST_FLOOD)
1679 port_priv->bcast_flood = !!(flags.val & BR_BCAST_FLOOD);
1681 if (flags.mask & BR_FLOOD)
1682 port_priv->ucast_flood = !!(flags.val & BR_FLOOD);
1684 return dpaa2_switch_fdb_set_egress_flood(ethsw, port_priv->fdb->fdb_id);
1687 static int dpaa2_switch_port_pre_bridge_flags(struct net_device *netdev,
1688 struct switchdev_brport_flags flags,
1689 struct netlink_ext_ack *extack)
1691 if (flags.mask & ~(BR_LEARNING | BR_BCAST_FLOOD | BR_FLOOD |
1695 if (flags.mask & (BR_FLOOD | BR_MCAST_FLOOD)) {
1696 bool multicast = !!(flags.val & BR_MCAST_FLOOD);
1697 bool unicast = !!(flags.val & BR_FLOOD);
1699 if (unicast != multicast) {
1700 NL_SET_ERR_MSG_MOD(extack,
1701 "Cannot configure multicast flooding independently of unicast");
1709 static int dpaa2_switch_port_bridge_flags(struct net_device *netdev,
1710 struct switchdev_brport_flags flags,
1711 struct netlink_ext_ack *extack)
1713 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1716 if (flags.mask & BR_LEARNING) {
1717 bool learn_ena = !!(flags.val & BR_LEARNING);
1719 err = dpaa2_switch_port_set_learning(port_priv, learn_ena);
1722 port_priv->learn_ena = learn_ena;
1725 if (flags.mask & (BR_BCAST_FLOOD | BR_FLOOD | BR_MCAST_FLOOD)) {
1726 err = dpaa2_switch_port_flood(port_priv, flags);
1734 static int dpaa2_switch_port_attr_set(struct net_device *netdev, const void *ctx,
1735 const struct switchdev_attr *attr,
1736 struct netlink_ext_ack *extack)
1741 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
1742 err = dpaa2_switch_port_attr_stp_state_set(netdev,
1745 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
1746 if (!attr->u.vlan_filtering) {
1747 NL_SET_ERR_MSG_MOD(extack,
1748 "The DPAA2 switch does not support VLAN-unaware operation");
1752 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
1753 err = dpaa2_switch_port_pre_bridge_flags(netdev, attr->u.brport_flags, extack);
1755 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
1756 err = dpaa2_switch_port_bridge_flags(netdev, attr->u.brport_flags, extack);
1766 int dpaa2_switch_port_vlans_add(struct net_device *netdev,
1767 const struct switchdev_obj_port_vlan *vlan)
1769 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1770 struct ethsw_core *ethsw = port_priv->ethsw_data;
1771 struct dpsw_attr *attr = ðsw->sw_attr;
1774 /* Make sure that the VLAN is not already configured
1775 * on the switch port
1777 if (port_priv->vlans[vlan->vid] & ETHSW_VLAN_MEMBER) {
1778 netdev_err(netdev, "VLAN %d already configured\n", vlan->vid);
1782 /* Check if there is space for a new VLAN */
1783 err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
1786 netdev_err(netdev, "dpsw_get_attributes err %d\n", err);
1789 if (attr->max_vlans - attr->num_vlans < 1)
1792 /* Check if there is space for a new VLAN */
1793 err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
1796 netdev_err(netdev, "dpsw_get_attributes err %d\n", err);
1799 if (attr->max_vlans - attr->num_vlans < 1)
1802 if (!port_priv->ethsw_data->vlans[vlan->vid]) {
1803 /* this is a new VLAN */
1804 err = dpaa2_switch_add_vlan(port_priv, vlan->vid);
1808 port_priv->ethsw_data->vlans[vlan->vid] |= ETHSW_VLAN_GLOBAL;
1811 return dpaa2_switch_port_add_vlan(port_priv, vlan->vid, vlan->flags);
1814 static int dpaa2_switch_port_lookup_address(struct net_device *netdev, int is_uc,
1815 const unsigned char *addr)
1817 struct netdev_hw_addr_list *list = (is_uc) ? &netdev->uc : &netdev->mc;
1818 struct netdev_hw_addr *ha;
1820 netif_addr_lock_bh(netdev);
1821 list_for_each_entry(ha, &list->list, list) {
1822 if (ether_addr_equal(ha->addr, addr)) {
1823 netif_addr_unlock_bh(netdev);
1827 netif_addr_unlock_bh(netdev);
1831 static int dpaa2_switch_port_mdb_add(struct net_device *netdev,
1832 const struct switchdev_obj_port_mdb *mdb)
1834 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1837 /* Check if address is already set on this port */
1838 if (dpaa2_switch_port_lookup_address(netdev, 0, mdb->addr))
1841 err = dpaa2_switch_port_fdb_add_mc(port_priv, mdb->addr);
1845 err = dev_mc_add(netdev, mdb->addr);
1847 netdev_err(netdev, "dev_mc_add err %d\n", err);
1848 dpaa2_switch_port_fdb_del_mc(port_priv, mdb->addr);
1854 static int dpaa2_switch_port_obj_add(struct net_device *netdev,
1855 const struct switchdev_obj *obj)
1860 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1861 err = dpaa2_switch_port_vlans_add(netdev,
1862 SWITCHDEV_OBJ_PORT_VLAN(obj));
1864 case SWITCHDEV_OBJ_ID_PORT_MDB:
1865 err = dpaa2_switch_port_mdb_add(netdev,
1866 SWITCHDEV_OBJ_PORT_MDB(obj));
1876 static int dpaa2_switch_port_del_vlan(struct ethsw_port_priv *port_priv, u16 vid)
1878 struct ethsw_core *ethsw = port_priv->ethsw_data;
1879 struct net_device *netdev = port_priv->netdev;
1880 struct dpsw_vlan_if_cfg vcfg;
1883 if (!port_priv->vlans[vid])
1886 if (port_priv->vlans[vid] & ETHSW_VLAN_PVID) {
1887 /* If we are deleting the PVID of a port, use VLAN 4095 instead
1888 * as we are sure that neither the bridge nor the 8021q module
1891 err = dpaa2_switch_port_set_pvid(port_priv, 4095);
1897 vcfg.if_id[0] = port_priv->idx;
1898 if (port_priv->vlans[vid] & ETHSW_VLAN_UNTAGGED) {
1899 err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0,
1904 "dpsw_vlan_remove_if_untagged err %d\n",
1907 port_priv->vlans[vid] &= ~ETHSW_VLAN_UNTAGGED;
1910 if (port_priv->vlans[vid] & ETHSW_VLAN_MEMBER) {
1911 err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
1915 "dpsw_vlan_remove_if err %d\n", err);
1918 port_priv->vlans[vid] &= ~ETHSW_VLAN_MEMBER;
1920 /* Delete VLAN from switch if it is no longer configured on
1923 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
1924 if (ethsw->ports[i] &&
1925 ethsw->ports[i]->vlans[vid] & ETHSW_VLAN_MEMBER)
1926 return 0; /* Found a port member in VID */
1929 ethsw->vlans[vid] &= ~ETHSW_VLAN_GLOBAL;
1931 err = dpaa2_switch_dellink(ethsw, vid);
1939 int dpaa2_switch_port_vlans_del(struct net_device *netdev,
1940 const struct switchdev_obj_port_vlan *vlan)
1942 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1944 if (netif_is_bridge_master(vlan->obj.orig_dev))
1947 return dpaa2_switch_port_del_vlan(port_priv, vlan->vid);
1950 static int dpaa2_switch_port_mdb_del(struct net_device *netdev,
1951 const struct switchdev_obj_port_mdb *mdb)
1953 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1956 if (!dpaa2_switch_port_lookup_address(netdev, 0, mdb->addr))
1959 err = dpaa2_switch_port_fdb_del_mc(port_priv, mdb->addr);
1963 err = dev_mc_del(netdev, mdb->addr);
1965 netdev_err(netdev, "dev_mc_del err %d\n", err);
1972 static int dpaa2_switch_port_obj_del(struct net_device *netdev,
1973 const struct switchdev_obj *obj)
1978 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1979 err = dpaa2_switch_port_vlans_del(netdev, SWITCHDEV_OBJ_PORT_VLAN(obj));
1981 case SWITCHDEV_OBJ_ID_PORT_MDB:
1982 err = dpaa2_switch_port_mdb_del(netdev, SWITCHDEV_OBJ_PORT_MDB(obj));
1991 static int dpaa2_switch_port_attr_set_event(struct net_device *netdev,
1992 struct switchdev_notifier_port_attr_info *ptr)
1996 err = switchdev_handle_port_attr_set(netdev, ptr,
1997 dpaa2_switch_port_dev_check,
1998 dpaa2_switch_port_attr_set);
1999 return notifier_from_errno(err);
2002 static int dpaa2_switch_port_bridge_join(struct net_device *netdev,
2003 struct net_device *upper_dev,
2004 struct netlink_ext_ack *extack)
2006 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
2007 struct dpaa2_switch_fdb *old_fdb = port_priv->fdb;
2008 struct ethsw_core *ethsw = port_priv->ethsw_data;
2012 /* Delete the previously manually installed VLAN 1 */
2013 err = dpaa2_switch_port_del_vlan(port_priv, 1);
2017 dpaa2_switch_port_set_fdb(port_priv, upper_dev);
2019 /* Inherit the initial bridge port learning state */
2020 learn_ena = br_port_flag_is_set(netdev, BR_LEARNING);
2021 err = dpaa2_switch_port_set_learning(port_priv, learn_ena);
2022 port_priv->learn_ena = learn_ena;
2024 /* Setup the egress flood policy (broadcast, unknown unicast) */
2025 err = dpaa2_switch_fdb_set_egress_flood(ethsw, port_priv->fdb->fdb_id);
2027 goto err_egress_flood;
2029 /* Recreate the egress flood domain of the FDB that we just left. */
2030 err = dpaa2_switch_fdb_set_egress_flood(ethsw, old_fdb->fdb_id);
2032 goto err_egress_flood;
2034 err = switchdev_bridge_port_offload(netdev, netdev, NULL,
2035 NULL, NULL, false, extack);
2037 goto err_switchdev_offload;
2041 err_switchdev_offload:
2043 dpaa2_switch_port_set_fdb(port_priv, NULL);
2047 static int dpaa2_switch_port_clear_rxvlan(struct net_device *vdev, int vid, void *arg)
2049 __be16 vlan_proto = htons(ETH_P_8021Q);
2052 vlan_proto = vlan_dev_vlan_proto(vdev);
2054 return dpaa2_switch_port_vlan_kill(arg, vlan_proto, vid);
2057 static int dpaa2_switch_port_restore_rxvlan(struct net_device *vdev, int vid, void *arg)
2059 __be16 vlan_proto = htons(ETH_P_8021Q);
2062 vlan_proto = vlan_dev_vlan_proto(vdev);
2064 return dpaa2_switch_port_vlan_add(arg, vlan_proto, vid);
2067 static void dpaa2_switch_port_pre_bridge_leave(struct net_device *netdev)
2069 switchdev_bridge_port_unoffload(netdev, NULL, NULL, NULL);
2072 static int dpaa2_switch_port_bridge_leave(struct net_device *netdev)
2074 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
2075 struct dpaa2_switch_fdb *old_fdb = port_priv->fdb;
2076 struct ethsw_core *ethsw = port_priv->ethsw_data;
2079 /* First of all, fast age any learn FDB addresses on this switch port */
2080 dpaa2_switch_port_fast_age(port_priv);
2082 /* Clear all RX VLANs installed through vlan_vid_add() either as VLAN
2083 * upper devices or otherwise from the FDB table that we are about to
2086 err = vlan_for_each(netdev, dpaa2_switch_port_clear_rxvlan, netdev);
2088 netdev_err(netdev, "Unable to clear RX VLANs from old FDB table, err (%d)\n", err);
2090 dpaa2_switch_port_set_fdb(port_priv, NULL);
2092 /* Restore all RX VLANs into the new FDB table that we just joined */
2093 err = vlan_for_each(netdev, dpaa2_switch_port_restore_rxvlan, netdev);
2095 netdev_err(netdev, "Unable to restore RX VLANs to the new FDB, err (%d)\n", err);
2097 /* Reset the flooding state to denote that this port can send any
2098 * packet in standalone mode. With this, we are also ensuring that any
2099 * later bridge join will have the flooding flag on.
2101 port_priv->bcast_flood = true;
2102 port_priv->ucast_flood = true;
2104 /* Setup the egress flood policy (broadcast, unknown unicast).
2105 * When the port is not under a bridge, only the CTRL interface is part
2106 * of the flooding domain besides the actual port
2108 err = dpaa2_switch_fdb_set_egress_flood(ethsw, port_priv->fdb->fdb_id);
2112 /* Recreate the egress flood domain of the FDB that we just left */
2113 err = dpaa2_switch_fdb_set_egress_flood(ethsw, old_fdb->fdb_id);
2117 /* No HW learning when not under a bridge */
2118 err = dpaa2_switch_port_set_learning(port_priv, false);
2121 port_priv->learn_ena = false;
2123 /* Add the VLAN 1 as PVID when not under a bridge. We need this since
2124 * the dpaa2 switch interfaces are not capable to be VLAN unaware
2126 return dpaa2_switch_port_add_vlan(port_priv, DEFAULT_VLAN_ID,
2127 BRIDGE_VLAN_INFO_UNTAGGED | BRIDGE_VLAN_INFO_PVID);
2130 static int dpaa2_switch_prevent_bridging_with_8021q_upper(struct net_device *netdev)
2132 struct net_device *upper_dev;
2133 struct list_head *iter;
2135 /* RCU read lock not necessary because we have write-side protection
2136 * (rtnl_mutex), however a non-rcu iterator does not exist.
2138 netdev_for_each_upper_dev_rcu(netdev, upper_dev, iter)
2139 if (is_vlan_dev(upper_dev))
2146 dpaa2_switch_prechangeupper_sanity_checks(struct net_device *netdev,
2147 struct net_device *upper_dev,
2148 struct netlink_ext_ack *extack)
2150 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
2151 struct ethsw_port_priv *other_port_priv;
2152 struct net_device *other_dev;
2153 struct list_head *iter;
2156 if (!br_vlan_enabled(upper_dev)) {
2157 NL_SET_ERR_MSG_MOD(extack, "Cannot join a VLAN-unaware bridge");
2161 err = dpaa2_switch_prevent_bridging_with_8021q_upper(netdev);
2163 NL_SET_ERR_MSG_MOD(extack,
2164 "Cannot join a bridge while VLAN uppers are present");
2168 netdev_for_each_lower_dev(upper_dev, other_dev, iter) {
2169 if (!dpaa2_switch_port_dev_check(other_dev))
2172 other_port_priv = netdev_priv(other_dev);
2173 if (other_port_priv->ethsw_data != port_priv->ethsw_data) {
2174 NL_SET_ERR_MSG_MOD(extack,
2175 "Interface from a different DPSW is in the bridge already");
2183 static int dpaa2_switch_port_prechangeupper(struct net_device *netdev,
2184 struct netdev_notifier_changeupper_info *info)
2186 struct netlink_ext_ack *extack;
2187 struct net_device *upper_dev;
2190 if (!dpaa2_switch_port_dev_check(netdev))
2193 extack = netdev_notifier_info_to_extack(&info->info);
2194 upper_dev = info->upper_dev;
2195 if (netif_is_bridge_master(upper_dev)) {
2196 err = dpaa2_switch_prechangeupper_sanity_checks(netdev,
2203 dpaa2_switch_port_pre_bridge_leave(netdev);
2209 static int dpaa2_switch_port_changeupper(struct net_device *netdev,
2210 struct netdev_notifier_changeupper_info *info)
2212 struct netlink_ext_ack *extack;
2213 struct net_device *upper_dev;
2215 if (!dpaa2_switch_port_dev_check(netdev))
2218 extack = netdev_notifier_info_to_extack(&info->info);
2220 upper_dev = info->upper_dev;
2221 if (netif_is_bridge_master(upper_dev)) {
2223 return dpaa2_switch_port_bridge_join(netdev,
2227 return dpaa2_switch_port_bridge_leave(netdev);
2233 static int dpaa2_switch_port_netdevice_event(struct notifier_block *nb,
2234 unsigned long event, void *ptr)
2236 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
2240 case NETDEV_PRECHANGEUPPER:
2241 err = dpaa2_switch_port_prechangeupper(netdev, ptr);
2243 return notifier_from_errno(err);
2246 case NETDEV_CHANGEUPPER:
2247 err = dpaa2_switch_port_changeupper(netdev, ptr);
2249 return notifier_from_errno(err);
2257 struct ethsw_switchdev_event_work {
2258 struct work_struct work;
2259 struct switchdev_notifier_fdb_info fdb_info;
2260 struct net_device *dev;
2261 unsigned long event;
2264 static void dpaa2_switch_event_work(struct work_struct *work)
2266 struct ethsw_switchdev_event_work *switchdev_work =
2267 container_of(work, struct ethsw_switchdev_event_work, work);
2268 struct net_device *dev = switchdev_work->dev;
2269 struct switchdev_notifier_fdb_info *fdb_info;
2273 fdb_info = &switchdev_work->fdb_info;
2275 switch (switchdev_work->event) {
2276 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2277 if (!fdb_info->added_by_user || fdb_info->is_local)
2279 if (is_unicast_ether_addr(fdb_info->addr))
2280 err = dpaa2_switch_port_fdb_add_uc(netdev_priv(dev),
2283 err = dpaa2_switch_port_fdb_add_mc(netdev_priv(dev),
2287 fdb_info->offloaded = true;
2288 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
2289 &fdb_info->info, NULL);
2291 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2292 if (!fdb_info->added_by_user || fdb_info->is_local)
2294 if (is_unicast_ether_addr(fdb_info->addr))
2295 dpaa2_switch_port_fdb_del_uc(netdev_priv(dev), fdb_info->addr);
2297 dpaa2_switch_port_fdb_del_mc(netdev_priv(dev), fdb_info->addr);
2302 kfree(switchdev_work->fdb_info.addr);
2303 kfree(switchdev_work);
2307 /* Called under rcu_read_lock() */
2308 static int dpaa2_switch_port_event(struct notifier_block *nb,
2309 unsigned long event, void *ptr)
2311 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2312 struct ethsw_port_priv *port_priv = netdev_priv(dev);
2313 struct ethsw_switchdev_event_work *switchdev_work;
2314 struct switchdev_notifier_fdb_info *fdb_info = ptr;
2315 struct ethsw_core *ethsw = port_priv->ethsw_data;
2317 if (event == SWITCHDEV_PORT_ATTR_SET)
2318 return dpaa2_switch_port_attr_set_event(dev, ptr);
2320 if (!dpaa2_switch_port_dev_check(dev))
2323 switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
2324 if (!switchdev_work)
2327 INIT_WORK(&switchdev_work->work, dpaa2_switch_event_work);
2328 switchdev_work->dev = dev;
2329 switchdev_work->event = event;
2332 case SWITCHDEV_FDB_ADD_TO_DEVICE:
2333 case SWITCHDEV_FDB_DEL_TO_DEVICE:
2334 memcpy(&switchdev_work->fdb_info, ptr,
2335 sizeof(switchdev_work->fdb_info));
2336 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
2337 if (!switchdev_work->fdb_info.addr)
2338 goto err_addr_alloc;
2340 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
2343 /* Take a reference on the device to avoid being freed. */
2347 kfree(switchdev_work);
2351 queue_work(ethsw->workqueue, &switchdev_work->work);
2356 kfree(switchdev_work);
2360 static int dpaa2_switch_port_obj_event(unsigned long event,
2361 struct net_device *netdev,
2362 struct switchdev_notifier_port_obj_info *port_obj_info)
2364 int err = -EOPNOTSUPP;
2366 if (!dpaa2_switch_port_dev_check(netdev))
2370 case SWITCHDEV_PORT_OBJ_ADD:
2371 err = dpaa2_switch_port_obj_add(netdev, port_obj_info->obj);
2373 case SWITCHDEV_PORT_OBJ_DEL:
2374 err = dpaa2_switch_port_obj_del(netdev, port_obj_info->obj);
2378 port_obj_info->handled = true;
2379 return notifier_from_errno(err);
2382 static int dpaa2_switch_port_blocking_event(struct notifier_block *nb,
2383 unsigned long event, void *ptr)
2385 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
2388 case SWITCHDEV_PORT_OBJ_ADD:
2389 case SWITCHDEV_PORT_OBJ_DEL:
2390 return dpaa2_switch_port_obj_event(event, dev, ptr);
2391 case SWITCHDEV_PORT_ATTR_SET:
2392 return dpaa2_switch_port_attr_set_event(dev, ptr);
2398 /* Build a linear skb based on a single-buffer frame descriptor */
2399 static struct sk_buff *dpaa2_switch_build_linear_skb(struct ethsw_core *ethsw,
2400 const struct dpaa2_fd *fd)
2402 u16 fd_offset = dpaa2_fd_get_offset(fd);
2403 dma_addr_t addr = dpaa2_fd_get_addr(fd);
2404 u32 fd_length = dpaa2_fd_get_len(fd);
2405 struct device *dev = ethsw->dev;
2406 struct sk_buff *skb = NULL;
2409 fd_vaddr = dpaa2_iova_to_virt(ethsw->iommu_domain, addr);
2410 dma_unmap_page(dev, addr, DPAA2_SWITCH_RX_BUF_SIZE,
2413 skb = build_skb(fd_vaddr, DPAA2_SWITCH_RX_BUF_SIZE +
2414 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
2415 if (unlikely(!skb)) {
2416 dev_err(dev, "build_skb() failed\n");
2420 skb_reserve(skb, fd_offset);
2421 skb_put(skb, fd_length);
2428 static void dpaa2_switch_tx_conf(struct dpaa2_switch_fq *fq,
2429 const struct dpaa2_fd *fd)
2431 dpaa2_switch_free_fd(fq->ethsw, fd);
2434 static void dpaa2_switch_rx(struct dpaa2_switch_fq *fq,
2435 const struct dpaa2_fd *fd)
2437 struct ethsw_core *ethsw = fq->ethsw;
2438 struct ethsw_port_priv *port_priv;
2439 struct net_device *netdev;
2440 struct vlan_ethhdr *hdr;
2441 struct sk_buff *skb;
2445 /* get switch ingress interface ID */
2446 if_id = upper_32_bits(dpaa2_fd_get_flc(fd)) & 0x0000FFFF;
2448 if (if_id >= ethsw->sw_attr.num_ifs) {
2449 dev_err(ethsw->dev, "Frame received from unknown interface!\n");
2452 port_priv = ethsw->ports[if_id];
2453 netdev = port_priv->netdev;
2455 /* build the SKB based on the FD received */
2456 if (dpaa2_fd_get_format(fd) != dpaa2_fd_single) {
2457 if (net_ratelimit()) {
2458 netdev_err(netdev, "Received invalid frame format\n");
2463 skb = dpaa2_switch_build_linear_skb(ethsw, fd);
2467 skb_reset_mac_header(skb);
2469 /* Remove the VLAN header if the packet that we just received has a vid
2470 * equal to the port PVIDs. Since the dpaa2-switch can operate only in
2471 * VLAN-aware mode and no alterations are made on the packet when it's
2472 * redirected/mirrored to the control interface, we are sure that there
2473 * will always be a VLAN header present.
2475 hdr = vlan_eth_hdr(skb);
2476 vid = ntohs(hdr->h_vlan_TCI) & VLAN_VID_MASK;
2477 if (vid == port_priv->pvid) {
2478 err = __skb_vlan_pop(skb, &vlan_tci);
2480 dev_info(ethsw->dev, "__skb_vlan_pop() returned %d", err);
2486 skb->protocol = eth_type_trans(skb, skb->dev);
2488 /* Setup the offload_fwd_mark only if the port is under a bridge */
2489 skb->offload_fwd_mark = !!(port_priv->fdb->bridge_dev);
2491 netif_receive_skb(skb);
2496 dpaa2_switch_free_fd(ethsw, fd);
2499 static void dpaa2_switch_detect_features(struct ethsw_core *ethsw)
2501 ethsw->features = 0;
2503 if (ethsw->major > 8 || (ethsw->major == 8 && ethsw->minor >= 6))
2504 ethsw->features |= ETHSW_FEATURE_MAC_ADDR;
2507 static int dpaa2_switch_setup_fqs(struct ethsw_core *ethsw)
2509 struct dpsw_ctrl_if_attr ctrl_if_attr;
2510 struct device *dev = ethsw->dev;
2514 err = dpsw_ctrl_if_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
2517 dev_err(dev, "dpsw_ctrl_if_get_attributes() = %d\n", err);
2521 ethsw->fq[i].fqid = ctrl_if_attr.rx_fqid;
2522 ethsw->fq[i].ethsw = ethsw;
2523 ethsw->fq[i++].type = DPSW_QUEUE_RX;
2525 ethsw->fq[i].fqid = ctrl_if_attr.tx_err_conf_fqid;
2526 ethsw->fq[i].ethsw = ethsw;
2527 ethsw->fq[i++].type = DPSW_QUEUE_TX_ERR_CONF;
2532 /* Free buffers acquired from the buffer pool or which were meant to
2533 * be released in the pool
2535 static void dpaa2_switch_free_bufs(struct ethsw_core *ethsw, u64 *buf_array, int count)
2537 struct device *dev = ethsw->dev;
2541 for (i = 0; i < count; i++) {
2542 vaddr = dpaa2_iova_to_virt(ethsw->iommu_domain, buf_array[i]);
2543 dma_unmap_page(dev, buf_array[i], DPAA2_SWITCH_RX_BUF_SIZE,
2545 free_pages((unsigned long)vaddr, 0);
2549 /* Perform a single release command to add buffers
2550 * to the specified buffer pool
2552 static int dpaa2_switch_add_bufs(struct ethsw_core *ethsw, u16 bpid)
2554 struct device *dev = ethsw->dev;
2555 u64 buf_array[BUFS_PER_CMD];
2562 for (i = 0; i < BUFS_PER_CMD; i++) {
2563 /* Allocate one page for each Rx buffer. WRIOP sees
2564 * the entire page except for a tailroom reserved for
2567 page = dev_alloc_pages(0);
2569 dev_err(dev, "buffer allocation failed\n");
2573 addr = dma_map_page(dev, page, 0, DPAA2_SWITCH_RX_BUF_SIZE,
2575 if (dma_mapping_error(dev, addr)) {
2576 dev_err(dev, "dma_map_single() failed\n");
2579 buf_array[i] = addr;
2583 /* In case the portal is busy, retry until successful or
2586 while ((err = dpaa2_io_service_release(NULL, bpid,
2587 buf_array, i)) == -EBUSY) {
2588 if (retries++ >= DPAA2_SWITCH_SWP_BUSY_RETRIES)
2594 /* If release command failed, clean up and bail out. */
2596 dpaa2_switch_free_bufs(ethsw, buf_array, i);
2603 __free_pages(page, 0);
2605 /* If we managed to allocate at least some buffers,
2606 * release them to hardware
2614 static int dpaa2_switch_refill_bp(struct ethsw_core *ethsw)
2616 int *count = ðsw->buf_count;
2620 if (unlikely(*count < DPAA2_ETHSW_REFILL_THRESH)) {
2622 new_count = dpaa2_switch_add_bufs(ethsw, ethsw->bpid);
2623 if (unlikely(!new_count)) {
2624 /* Out of memory; abort for now, we'll
2629 *count += new_count;
2630 } while (*count < DPAA2_ETHSW_NUM_BUFS);
2632 if (unlikely(*count < DPAA2_ETHSW_NUM_BUFS))
2639 static int dpaa2_switch_seed_bp(struct ethsw_core *ethsw)
2643 for (i = 0; i < DPAA2_ETHSW_NUM_BUFS; i += BUFS_PER_CMD) {
2644 count = ðsw->buf_count;
2645 *count += dpaa2_switch_add_bufs(ethsw, ethsw->bpid);
2647 if (unlikely(*count < BUFS_PER_CMD))
2654 static void dpaa2_switch_drain_bp(struct ethsw_core *ethsw)
2656 u64 buf_array[BUFS_PER_CMD];
2660 ret = dpaa2_io_service_acquire(NULL, ethsw->bpid,
2661 buf_array, BUFS_PER_CMD);
2664 "dpaa2_io_service_acquire() = %d\n", ret);
2667 dpaa2_switch_free_bufs(ethsw, buf_array, ret);
2672 static int dpaa2_switch_setup_dpbp(struct ethsw_core *ethsw)
2674 struct dpsw_ctrl_if_pools_cfg dpsw_ctrl_if_pools_cfg = { 0 };
2675 struct device *dev = ethsw->dev;
2676 struct fsl_mc_device *dpbp_dev;
2677 struct dpbp_attr dpbp_attrs;
2680 err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
2684 err = -EPROBE_DEFER;
2686 dev_err(dev, "DPBP device allocation failed\n");
2689 ethsw->dpbp_dev = dpbp_dev;
2691 err = dpbp_open(ethsw->mc_io, 0, dpbp_dev->obj_desc.id,
2692 &dpbp_dev->mc_handle);
2694 dev_err(dev, "dpbp_open() failed\n");
2698 err = dpbp_reset(ethsw->mc_io, 0, dpbp_dev->mc_handle);
2700 dev_err(dev, "dpbp_reset() failed\n");
2704 err = dpbp_enable(ethsw->mc_io, 0, dpbp_dev->mc_handle);
2706 dev_err(dev, "dpbp_enable() failed\n");
2710 err = dpbp_get_attributes(ethsw->mc_io, 0, dpbp_dev->mc_handle,
2713 dev_err(dev, "dpbp_get_attributes() failed\n");
2717 dpsw_ctrl_if_pools_cfg.num_dpbp = 1;
2718 dpsw_ctrl_if_pools_cfg.pools[0].dpbp_id = dpbp_attrs.id;
2719 dpsw_ctrl_if_pools_cfg.pools[0].buffer_size = DPAA2_SWITCH_RX_BUF_SIZE;
2720 dpsw_ctrl_if_pools_cfg.pools[0].backup_pool = 0;
2722 err = dpsw_ctrl_if_set_pools(ethsw->mc_io, 0, ethsw->dpsw_handle,
2723 &dpsw_ctrl_if_pools_cfg);
2725 dev_err(dev, "dpsw_ctrl_if_set_pools() failed\n");
2728 ethsw->bpid = dpbp_attrs.id;
2733 dpbp_disable(ethsw->mc_io, 0, dpbp_dev->mc_handle);
2736 dpbp_close(ethsw->mc_io, 0, dpbp_dev->mc_handle);
2738 fsl_mc_object_free(dpbp_dev);
2742 static void dpaa2_switch_free_dpbp(struct ethsw_core *ethsw)
2744 dpbp_disable(ethsw->mc_io, 0, ethsw->dpbp_dev->mc_handle);
2745 dpbp_close(ethsw->mc_io, 0, ethsw->dpbp_dev->mc_handle);
2746 fsl_mc_object_free(ethsw->dpbp_dev);
2749 static int dpaa2_switch_alloc_rings(struct ethsw_core *ethsw)
2753 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) {
2754 ethsw->fq[i].store =
2755 dpaa2_io_store_create(DPAA2_SWITCH_STORE_SIZE,
2757 if (!ethsw->fq[i].store) {
2758 dev_err(ethsw->dev, "dpaa2_io_store_create failed\n");
2760 dpaa2_io_store_destroy(ethsw->fq[i].store);
2768 static void dpaa2_switch_destroy_rings(struct ethsw_core *ethsw)
2772 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++)
2773 dpaa2_io_store_destroy(ethsw->fq[i].store);
2776 static int dpaa2_switch_pull_fq(struct dpaa2_switch_fq *fq)
2778 int err, retries = 0;
2780 /* Try to pull from the FQ while the portal is busy and we didn't hit
2781 * the maximum number fo retries
2784 err = dpaa2_io_service_pull_fq(NULL, fq->fqid, fq->store);
2786 } while (err == -EBUSY && retries++ < DPAA2_SWITCH_SWP_BUSY_RETRIES);
2789 dev_err(fq->ethsw->dev, "dpaa2_io_service_pull err %d", err);
2794 /* Consume all frames pull-dequeued into the store */
2795 static int dpaa2_switch_store_consume(struct dpaa2_switch_fq *fq)
2797 struct ethsw_core *ethsw = fq->ethsw;
2798 int cleaned = 0, is_last;
2799 struct dpaa2_dq *dq;
2803 /* Get the next available FD from the store */
2804 dq = dpaa2_io_store_next(fq->store, &is_last);
2805 if (unlikely(!dq)) {
2806 if (retries++ >= DPAA2_SWITCH_SWP_BUSY_RETRIES) {
2807 dev_err_once(ethsw->dev,
2808 "No valid dequeue response\n");
2814 if (fq->type == DPSW_QUEUE_RX)
2815 dpaa2_switch_rx(fq, dpaa2_dq_fd(dq));
2817 dpaa2_switch_tx_conf(fq, dpaa2_dq_fd(dq));
2825 /* NAPI poll routine */
2826 static int dpaa2_switch_poll(struct napi_struct *napi, int budget)
2828 int err, cleaned = 0, store_cleaned, work_done;
2829 struct dpaa2_switch_fq *fq;
2832 fq = container_of(napi, struct dpaa2_switch_fq, napi);
2835 err = dpaa2_switch_pull_fq(fq);
2839 /* Refill pool if appropriate */
2840 dpaa2_switch_refill_bp(fq->ethsw);
2842 store_cleaned = dpaa2_switch_store_consume(fq);
2843 cleaned += store_cleaned;
2845 if (cleaned >= budget) {
2850 } while (store_cleaned);
2852 /* We didn't consume the entire budget, so finish napi and re-enable
2853 * data availability notifications
2855 napi_complete_done(napi, cleaned);
2857 err = dpaa2_io_service_rearm(NULL, &fq->nctx);
2859 } while (err == -EBUSY && retries++ < DPAA2_SWITCH_SWP_BUSY_RETRIES);
2861 work_done = max(cleaned, 1);
2867 static void dpaa2_switch_fqdan_cb(struct dpaa2_io_notification_ctx *nctx)
2869 struct dpaa2_switch_fq *fq;
2871 fq = container_of(nctx, struct dpaa2_switch_fq, nctx);
2873 napi_schedule(&fq->napi);
2876 static int dpaa2_switch_setup_dpio(struct ethsw_core *ethsw)
2878 struct dpsw_ctrl_if_queue_cfg queue_cfg;
2879 struct dpaa2_io_notification_ctx *nctx;
2882 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++) {
2883 nctx = ðsw->fq[i].nctx;
2885 /* Register a new software context for the FQID.
2886 * By using NULL as the first parameter, we specify that we do
2887 * not care on which cpu are interrupts received for this queue
2890 nctx->id = ethsw->fq[i].fqid;
2891 nctx->desired_cpu = DPAA2_IO_ANY_CPU;
2892 nctx->cb = dpaa2_switch_fqdan_cb;
2893 err = dpaa2_io_service_register(NULL, nctx, ethsw->dev);
2895 err = -EPROBE_DEFER;
2899 queue_cfg.options = DPSW_CTRL_IF_QUEUE_OPT_DEST |
2900 DPSW_CTRL_IF_QUEUE_OPT_USER_CTX;
2901 queue_cfg.dest_cfg.dest_type = DPSW_CTRL_IF_DEST_DPIO;
2902 queue_cfg.dest_cfg.dest_id = nctx->dpio_id;
2903 queue_cfg.dest_cfg.priority = 0;
2904 queue_cfg.user_ctx = nctx->qman64;
2906 err = dpsw_ctrl_if_set_queue(ethsw->mc_io, 0,
2917 dpaa2_io_service_deregister(NULL, nctx, ethsw->dev);
2919 for (j = 0; j < i; j++)
2920 dpaa2_io_service_deregister(NULL, ðsw->fq[j].nctx,
2926 static void dpaa2_switch_free_dpio(struct ethsw_core *ethsw)
2930 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++)
2931 dpaa2_io_service_deregister(NULL, ðsw->fq[i].nctx,
2935 static int dpaa2_switch_ctrl_if_setup(struct ethsw_core *ethsw)
2939 /* setup FQs for Rx and Tx Conf */
2940 err = dpaa2_switch_setup_fqs(ethsw);
2944 /* setup the buffer pool needed on the Rx path */
2945 err = dpaa2_switch_setup_dpbp(ethsw);
2949 err = dpaa2_switch_alloc_rings(ethsw);
2953 err = dpaa2_switch_setup_dpio(ethsw);
2955 goto err_destroy_rings;
2957 err = dpaa2_switch_seed_bp(ethsw);
2959 goto err_deregister_dpio;
2961 err = dpsw_ctrl_if_enable(ethsw->mc_io, 0, ethsw->dpsw_handle);
2963 dev_err(ethsw->dev, "dpsw_ctrl_if_enable err %d\n", err);
2964 goto err_drain_dpbp;
2970 dpaa2_switch_drain_bp(ethsw);
2971 err_deregister_dpio:
2972 dpaa2_switch_free_dpio(ethsw);
2974 dpaa2_switch_destroy_rings(ethsw);
2976 dpaa2_switch_free_dpbp(ethsw);
2981 static void dpaa2_switch_remove_port(struct ethsw_core *ethsw,
2984 struct ethsw_port_priv *port_priv = ethsw->ports[port_idx];
2986 dpaa2_switch_port_disconnect_mac(port_priv);
2987 free_netdev(port_priv->netdev);
2988 ethsw->ports[port_idx] = NULL;
2991 static int dpaa2_switch_init(struct fsl_mc_device *sw_dev)
2993 struct device *dev = &sw_dev->dev;
2994 struct ethsw_core *ethsw = dev_get_drvdata(dev);
2995 struct dpsw_vlan_if_cfg vcfg = {0};
2996 struct dpsw_tci_cfg tci_cfg = {0};
2997 struct dpsw_stp_cfg stp_cfg;
3001 ethsw->dev_id = sw_dev->obj_desc.id;
3003 err = dpsw_open(ethsw->mc_io, 0, ethsw->dev_id, ðsw->dpsw_handle);
3005 dev_err(dev, "dpsw_open err %d\n", err);
3009 err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
3012 dev_err(dev, "dpsw_get_attributes err %d\n", err);
3016 err = dpsw_get_api_version(ethsw->mc_io, 0,
3020 dev_err(dev, "dpsw_get_api_version err %d\n", err);
3024 /* Minimum supported DPSW version check */
3025 if (ethsw->major < DPSW_MIN_VER_MAJOR ||
3026 (ethsw->major == DPSW_MIN_VER_MAJOR &&
3027 ethsw->minor < DPSW_MIN_VER_MINOR)) {
3028 dev_err(dev, "DPSW version %d:%d not supported. Use firmware 10.28.0 or greater.\n",
3029 ethsw->major, ethsw->minor);
3034 if (!dpaa2_switch_supports_cpu_traffic(ethsw)) {
3039 dpaa2_switch_detect_features(ethsw);
3041 err = dpsw_reset(ethsw->mc_io, 0, ethsw->dpsw_handle);
3043 dev_err(dev, "dpsw_reset err %d\n", err);
3047 stp_cfg.vlan_id = DEFAULT_VLAN_ID;
3048 stp_cfg.state = DPSW_STP_STATE_FORWARDING;
3050 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
3051 err = dpsw_if_disable(ethsw->mc_io, 0, ethsw->dpsw_handle, i);
3053 dev_err(dev, "dpsw_if_disable err %d\n", err);
3057 err = dpsw_if_set_stp(ethsw->mc_io, 0, ethsw->dpsw_handle, i,
3060 dev_err(dev, "dpsw_if_set_stp err %d for port %d\n",
3065 /* Switch starts with all ports configured to VLAN 1. Need to
3066 * remove this setting to allow configuration at bridge join
3070 err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0, ethsw->dpsw_handle,
3071 DEFAULT_VLAN_ID, &vcfg);
3073 dev_err(dev, "dpsw_vlan_remove_if_untagged err %d\n",
3078 tci_cfg.vlan_id = 4095;
3079 err = dpsw_if_set_tci(ethsw->mc_io, 0, ethsw->dpsw_handle, i, &tci_cfg);
3081 dev_err(dev, "dpsw_if_set_tci err %d\n", err);
3085 err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
3086 DEFAULT_VLAN_ID, &vcfg);
3088 dev_err(dev, "dpsw_vlan_remove_if err %d\n", err);
3093 err = dpsw_vlan_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, DEFAULT_VLAN_ID);
3095 dev_err(dev, "dpsw_vlan_remove err %d\n", err);
3099 ethsw->workqueue = alloc_ordered_workqueue("%s_%d_ordered",
3100 WQ_MEM_RECLAIM, "ethsw",
3102 if (!ethsw->workqueue) {
3107 err = dpsw_fdb_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, 0);
3109 goto err_destroy_ordered_workqueue;
3111 err = dpaa2_switch_ctrl_if_setup(ethsw);
3113 goto err_destroy_ordered_workqueue;
3117 err_destroy_ordered_workqueue:
3118 destroy_workqueue(ethsw->workqueue);
3121 dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle);
3125 /* Add an ACL to redirect frames with specific destination MAC address to
3128 static int dpaa2_switch_port_trap_mac_addr(struct ethsw_port_priv *port_priv,
3131 struct dpaa2_switch_acl_entry acl_entry = {0};
3133 /* Match on the destination MAC address */
3134 ether_addr_copy(acl_entry.key.match.l2_dest_mac, mac);
3135 eth_broadcast_addr(acl_entry.key.mask.l2_dest_mac);
3138 acl_entry.cfg.precedence = 0;
3139 acl_entry.cfg.result.action = DPSW_ACL_ACTION_REDIRECT_TO_CTRL_IF;
3141 return dpaa2_switch_acl_entry_add(port_priv->filter_block, &acl_entry);
3144 static int dpaa2_switch_port_init(struct ethsw_port_priv *port_priv, u16 port)
3146 const char stpa[ETH_ALEN] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x00};
3147 struct switchdev_obj_port_vlan vlan = {
3148 .obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
3149 .vid = DEFAULT_VLAN_ID,
3150 .flags = BRIDGE_VLAN_INFO_UNTAGGED | BRIDGE_VLAN_INFO_PVID,
3152 struct net_device *netdev = port_priv->netdev;
3153 struct ethsw_core *ethsw = port_priv->ethsw_data;
3154 struct dpaa2_switch_filter_block *filter_block;
3155 struct dpsw_fdb_cfg fdb_cfg = {0};
3156 struct dpsw_if_attr dpsw_if_attr;
3157 struct dpaa2_switch_fdb *fdb;
3158 struct dpsw_acl_cfg acl_cfg;
3159 u16 fdb_id, acl_tbl_id;
3162 /* Get the Tx queue for this specific port */
3163 err = dpsw_if_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
3164 port_priv->idx, &dpsw_if_attr);
3166 netdev_err(netdev, "dpsw_if_get_attributes err %d\n", err);
3169 port_priv->tx_qdid = dpsw_if_attr.qdid;
3171 /* Create a FDB table for this particular switch port */
3172 fdb_cfg.num_fdb_entries = ethsw->sw_attr.max_fdb_entries / ethsw->sw_attr.num_ifs;
3173 err = dpsw_fdb_add(ethsw->mc_io, 0, ethsw->dpsw_handle,
3176 netdev_err(netdev, "dpsw_fdb_add err %d\n", err);
3180 /* Find an unused dpaa2_switch_fdb structure and use it */
3181 fdb = dpaa2_switch_fdb_get_unused(ethsw);
3182 fdb->fdb_id = fdb_id;
3184 fdb->bridge_dev = NULL;
3185 port_priv->fdb = fdb;
3187 /* We need to add VLAN 1 as the PVID on this port until it is under a
3188 * bridge since the DPAA2 switch is not able to handle the traffic in a
3189 * VLAN unaware fashion
3191 err = dpaa2_switch_port_vlans_add(netdev, &vlan);
3195 /* Setup the egress flooding domains (broadcast, unknown unicast */
3196 err = dpaa2_switch_fdb_set_egress_flood(ethsw, port_priv->fdb->fdb_id);
3200 /* Create an ACL table to be used by this switch port */
3201 acl_cfg.max_entries = DPAA2_ETHSW_PORT_MAX_ACL_ENTRIES;
3202 err = dpsw_acl_add(ethsw->mc_io, 0, ethsw->dpsw_handle,
3203 &acl_tbl_id, &acl_cfg);
3205 netdev_err(netdev, "dpsw_acl_add err %d\n", err);
3209 filter_block = dpaa2_switch_filter_block_get_unused(ethsw);
3210 filter_block->ethsw = ethsw;
3211 filter_block->acl_id = acl_tbl_id;
3212 filter_block->in_use = true;
3213 filter_block->num_acl_rules = 0;
3214 INIT_LIST_HEAD(&filter_block->acl_entries);
3215 INIT_LIST_HEAD(&filter_block->mirror_entries);
3217 err = dpaa2_switch_port_acl_tbl_bind(port_priv, filter_block);
3221 err = dpaa2_switch_port_trap_mac_addr(port_priv, stpa);
3228 static void dpaa2_switch_ctrl_if_teardown(struct ethsw_core *ethsw)
3230 dpsw_ctrl_if_disable(ethsw->mc_io, 0, ethsw->dpsw_handle);
3231 dpaa2_switch_free_dpio(ethsw);
3232 dpaa2_switch_destroy_rings(ethsw);
3233 dpaa2_switch_drain_bp(ethsw);
3234 dpaa2_switch_free_dpbp(ethsw);
3237 static void dpaa2_switch_teardown(struct fsl_mc_device *sw_dev)
3239 struct device *dev = &sw_dev->dev;
3240 struct ethsw_core *ethsw = dev_get_drvdata(dev);
3243 dpaa2_switch_ctrl_if_teardown(ethsw);
3245 destroy_workqueue(ethsw->workqueue);
3247 err = dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle);
3249 dev_warn(dev, "dpsw_close err %d\n", err);
3252 static void dpaa2_switch_remove(struct fsl_mc_device *sw_dev)
3254 struct ethsw_port_priv *port_priv;
3255 struct ethsw_core *ethsw;
3260 ethsw = dev_get_drvdata(dev);
3262 dpaa2_switch_teardown_irqs(sw_dev);
3264 dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle);
3266 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
3267 port_priv = ethsw->ports[i];
3268 unregister_netdev(port_priv->netdev);
3269 dpaa2_switch_remove_port(ethsw, i);
3273 kfree(ethsw->filter_blocks);
3274 kfree(ethsw->ports);
3276 dpaa2_switch_teardown(sw_dev);
3278 fsl_mc_portal_free(ethsw->mc_io);
3282 dev_set_drvdata(dev, NULL);
3285 static int dpaa2_switch_probe_port(struct ethsw_core *ethsw,
3288 struct ethsw_port_priv *port_priv;
3289 struct device *dev = ethsw->dev;
3290 struct net_device *port_netdev;
3293 port_netdev = alloc_etherdev(sizeof(struct ethsw_port_priv));
3295 dev_err(dev, "alloc_etherdev error\n");
3299 port_priv = netdev_priv(port_netdev);
3300 port_priv->netdev = port_netdev;
3301 port_priv->ethsw_data = ethsw;
3303 mutex_init(&port_priv->mac_lock);
3305 port_priv->idx = port_idx;
3306 port_priv->stp_state = BR_STATE_FORWARDING;
3308 SET_NETDEV_DEV(port_netdev, dev);
3309 port_netdev->netdev_ops = &dpaa2_switch_port_ops;
3310 port_netdev->ethtool_ops = &dpaa2_switch_port_ethtool_ops;
3312 port_netdev->needed_headroom = DPAA2_SWITCH_NEEDED_HEADROOM;
3314 port_priv->bcast_flood = true;
3315 port_priv->ucast_flood = true;
3317 /* Set MTU limits */
3318 port_netdev->min_mtu = ETH_MIN_MTU;
3319 port_netdev->max_mtu = ETHSW_MAX_FRAME_LENGTH;
3321 /* Populate the private port structure so that later calls to
3322 * dpaa2_switch_port_init() can use it.
3324 ethsw->ports[port_idx] = port_priv;
3326 /* The DPAA2 switch's ingress path depends on the VLAN table,
3327 * thus we are not able to disable VLAN filtering.
3329 port_netdev->features = NETIF_F_HW_VLAN_CTAG_FILTER |
3330 NETIF_F_HW_VLAN_STAG_FILTER |
3332 port_netdev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
3334 err = dpaa2_switch_port_init(port_priv, port_idx);
3336 goto err_port_probe;
3338 err = dpaa2_switch_port_set_mac_addr(port_priv);
3340 goto err_port_probe;
3342 err = dpaa2_switch_port_set_learning(port_priv, false);
3344 goto err_port_probe;
3345 port_priv->learn_ena = false;
3347 err = dpaa2_switch_port_connect_mac(port_priv);
3349 goto err_port_probe;
3354 free_netdev(port_netdev);
3355 ethsw->ports[port_idx] = NULL;
3360 static int dpaa2_switch_probe(struct fsl_mc_device *sw_dev)
3362 struct device *dev = &sw_dev->dev;
3363 struct ethsw_core *ethsw;
3366 /* Allocate switch core*/
3367 ethsw = kzalloc(sizeof(*ethsw), GFP_KERNEL);
3373 ethsw->iommu_domain = iommu_get_domain_for_dev(dev);
3374 dev_set_drvdata(dev, ethsw);
3376 err = fsl_mc_portal_allocate(sw_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
3380 err = -EPROBE_DEFER;
3382 dev_err(dev, "fsl_mc_portal_allocate err %d\n", err);
3383 goto err_free_drvdata;
3386 err = dpaa2_switch_init(sw_dev);
3388 goto err_free_cmdport;
3390 ethsw->ports = kcalloc(ethsw->sw_attr.num_ifs, sizeof(*ethsw->ports),
3392 if (!(ethsw->ports)) {
3397 ethsw->fdbs = kcalloc(ethsw->sw_attr.num_ifs, sizeof(*ethsw->fdbs),
3401 goto err_free_ports;
3404 ethsw->filter_blocks = kcalloc(ethsw->sw_attr.num_ifs,
3405 sizeof(*ethsw->filter_blocks),
3407 if (!ethsw->filter_blocks) {
3412 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
3413 err = dpaa2_switch_probe_port(ethsw, i);
3415 goto err_free_netdev;
3418 /* Add a NAPI instance for each of the Rx queues. The first port's
3419 * net_device will be associated with the instances since we do not have
3420 * different queues for each switch ports.
3422 for (i = 0; i < DPAA2_SWITCH_RX_NUM_FQS; i++)
3423 netif_napi_add(ethsw->ports[0]->netdev, ðsw->fq[i].napi,
3427 err = dpaa2_switch_setup_irqs(sw_dev);
3431 /* By convention, if the mirror port is equal to the number of switch
3432 * interfaces, then mirroring of any kind is disabled.
3434 ethsw->mirror_port = ethsw->sw_attr.num_ifs;
3436 /* Register the netdev only when the entire setup is done and the
3437 * switch port interfaces are ready to receive traffic
3439 for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
3440 err = register_netdev(ethsw->ports[i]->netdev);
3442 dev_err(dev, "register_netdev error %d\n", err);
3443 goto err_unregister_ports;
3449 err_unregister_ports:
3450 for (i--; i >= 0; i--)
3451 unregister_netdev(ethsw->ports[i]->netdev);
3452 dpaa2_switch_teardown_irqs(sw_dev);
3454 dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle);
3456 for (i--; i >= 0; i--)
3457 dpaa2_switch_remove_port(ethsw, i);
3458 kfree(ethsw->filter_blocks);
3462 kfree(ethsw->ports);
3465 dpaa2_switch_teardown(sw_dev);
3468 fsl_mc_portal_free(ethsw->mc_io);
3472 dev_set_drvdata(dev, NULL);
3477 static const struct fsl_mc_device_id dpaa2_switch_match_id_table[] = {
3479 .vendor = FSL_MC_VENDOR_FREESCALE,
3484 MODULE_DEVICE_TABLE(fslmc, dpaa2_switch_match_id_table);
3486 static struct fsl_mc_driver dpaa2_switch_drv = {
3488 .name = KBUILD_MODNAME,
3490 .probe = dpaa2_switch_probe,
3491 .remove = dpaa2_switch_remove,
3492 .match_id_table = dpaa2_switch_match_id_table
3495 static struct notifier_block dpaa2_switch_port_nb __read_mostly = {
3496 .notifier_call = dpaa2_switch_port_netdevice_event,
3499 static struct notifier_block dpaa2_switch_port_switchdev_nb = {
3500 .notifier_call = dpaa2_switch_port_event,
3503 static struct notifier_block dpaa2_switch_port_switchdev_blocking_nb = {
3504 .notifier_call = dpaa2_switch_port_blocking_event,
3507 static int dpaa2_switch_register_notifiers(void)
3511 err = register_netdevice_notifier(&dpaa2_switch_port_nb);
3513 pr_err("dpaa2-switch: failed to register net_device notifier (%d)\n", err);
3517 err = register_switchdev_notifier(&dpaa2_switch_port_switchdev_nb);
3519 pr_err("dpaa2-switch: failed to register switchdev notifier (%d)\n", err);
3520 goto err_switchdev_nb;
3523 err = register_switchdev_blocking_notifier(&dpaa2_switch_port_switchdev_blocking_nb);
3525 pr_err("dpaa2-switch: failed to register switchdev blocking notifier (%d)\n", err);
3526 goto err_switchdev_blocking_nb;
3531 err_switchdev_blocking_nb:
3532 unregister_switchdev_notifier(&dpaa2_switch_port_switchdev_nb);
3534 unregister_netdevice_notifier(&dpaa2_switch_port_nb);
3539 static void dpaa2_switch_unregister_notifiers(void)
3543 err = unregister_switchdev_blocking_notifier(&dpaa2_switch_port_switchdev_blocking_nb);
3545 pr_err("dpaa2-switch: failed to unregister switchdev blocking notifier (%d)\n",
3548 err = unregister_switchdev_notifier(&dpaa2_switch_port_switchdev_nb);
3550 pr_err("dpaa2-switch: failed to unregister switchdev notifier (%d)\n", err);
3552 err = unregister_netdevice_notifier(&dpaa2_switch_port_nb);
3554 pr_err("dpaa2-switch: failed to unregister net_device notifier (%d)\n", err);
3557 static int __init dpaa2_switch_driver_init(void)
3561 err = fsl_mc_driver_register(&dpaa2_switch_drv);
3565 err = dpaa2_switch_register_notifiers();
3567 fsl_mc_driver_unregister(&dpaa2_switch_drv);
3574 static void __exit dpaa2_switch_driver_exit(void)
3576 dpaa2_switch_unregister_notifiers();
3577 fsl_mc_driver_unregister(&dpaa2_switch_drv);
3580 module_init(dpaa2_switch_driver_init);
3581 module_exit(dpaa2_switch_driver_exit);
3583 MODULE_LICENSE("GPL v2");
3584 MODULE_DESCRIPTION("DPAA2 Ethernet Switch Driver");