1 // SPDX-License-Identifier: GPL-2.0
3 * Texas Instruments Ethernet Switch Driver
5 * Copyright (C) 2019 Texas Instruments
10 #include <linux/platform_device.h>
11 #include <linux/timer.h>
12 #include <linux/module.h>
13 #include <linux/irqreturn.h>
14 #include <linux/interrupt.h>
15 #include <linux/if_ether.h>
16 #include <linux/etherdevice.h>
17 #include <linux/net_tstamp.h>
18 #include <linux/phy.h>
19 #include <linux/phy/phy.h>
20 #include <linux/delay.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/gpio/consumer.h>
25 #include <linux/of_mdio.h>
26 #include <linux/of_net.h>
27 #include <linux/of_platform.h>
28 #include <linux/if_vlan.h>
29 #include <linux/kmemleak.h>
30 #include <linux/sys_soc.h>
32 #include <net/switchdev.h>
33 #include <net/page_pool/helpers.h>
34 #include <net/pkt_cls.h>
35 #include <net/devlink.h>
39 #include "cpsw_priv.h"
41 #include "cpsw_switchdev.h"
43 #include "davinci_cpdma.h"
45 #include <net/pkt_sched.h>
47 static int debug_level;
48 static int ale_ageout = CPSW_ALE_AGEOUT_DEFAULT;
49 static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
50 static int descs_pool_size = CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT;
53 struct cpsw_common *cpsw;
56 enum cpsw_devlink_param_id {
57 CPSW_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
58 CPSW_DL_PARAM_SWITCH_MODE,
59 CPSW_DL_PARAM_ALE_BYPASS,
62 /* struct cpsw_common is not needed, kept here for compatibility
63 * reasons witrh the old driver
65 static int cpsw_slave_index_priv(struct cpsw_common *cpsw,
66 struct cpsw_priv *priv)
68 if (priv->emac_port == HOST_PORT_NUM)
71 return priv->emac_port - 1;
74 static bool cpsw_is_switch_en(struct cpsw_common *cpsw)
76 return !cpsw->data.dual_emac;
79 static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
81 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
82 bool enable_uni = false;
85 if (cpsw_is_switch_en(cpsw))
88 /* Enabling promiscuous mode for one interface will be
89 * common for both the interface as the interface shares
90 * the same hardware resource.
92 for (i = 0; i < cpsw->data.slaves; i++)
93 if (cpsw->slaves[i].ndev &&
94 (cpsw->slaves[i].ndev->flags & IFF_PROMISC))
97 if (!enable && enable_uni) {
99 dev_dbg(cpsw->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
103 /* Enable unknown unicast, reg/unreg mcast */
104 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
105 ALE_P0_UNI_FLOOD, 1);
107 dev_dbg(cpsw->dev, "promiscuity enabled\n");
109 /* Disable unknown unicast */
110 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
111 ALE_P0_UNI_FLOOD, 0);
112 dev_dbg(cpsw->dev, "promiscuity disabled\n");
117 * cpsw_set_mc - adds multicast entry to the table if it's not added or deletes
118 * if it's not deleted
119 * @ndev: device to sync
120 * @addr: address to be added or deleted
121 * @vid: vlan id, if vid < 0 set/unset address for real device
122 * @add: add address if the flag is set or remove otherwise
124 static int cpsw_set_mc(struct net_device *ndev, const u8 *addr,
127 struct cpsw_priv *priv = netdev_priv(ndev);
128 struct cpsw_common *cpsw = priv->cpsw;
129 int mask, flags, ret, slave_no;
131 slave_no = cpsw_slave_index(cpsw, priv);
133 vid = cpsw->slaves[slave_no].port_vlan;
135 mask = ALE_PORT_HOST;
136 flags = vid ? ALE_VLAN : 0;
139 ret = cpsw_ale_add_mcast(cpsw->ale, addr, mask, flags, vid, 0);
141 ret = cpsw_ale_del_mcast(cpsw->ale, addr, 0, flags, vid);
146 static int cpsw_update_vlan_mc(struct net_device *vdev, int vid, void *ctx)
148 struct addr_sync_ctx *sync_ctx = ctx;
149 struct netdev_hw_addr *ha;
150 int found = 0, ret = 0;
152 if (!vdev || !(vdev->flags & IFF_UP))
155 /* vlan address is relevant if its sync_cnt != 0 */
156 netdev_for_each_mc_addr(ha, vdev) {
157 if (ether_addr_equal(ha->addr, sync_ctx->addr)) {
158 found = ha->sync_cnt;
164 sync_ctx->consumed++;
166 if (sync_ctx->flush) {
168 cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 0);
173 ret = cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 1);
178 static int cpsw_add_mc_addr(struct net_device *ndev, const u8 *addr, int num)
180 struct addr_sync_ctx sync_ctx;
183 sync_ctx.consumed = 0;
184 sync_ctx.addr = addr;
185 sync_ctx.ndev = ndev;
188 ret = vlan_for_each(ndev, cpsw_update_vlan_mc, &sync_ctx);
189 if (sync_ctx.consumed < num && !ret)
190 ret = cpsw_set_mc(ndev, addr, -1, 1);
195 static int cpsw_del_mc_addr(struct net_device *ndev, const u8 *addr, int num)
197 struct addr_sync_ctx sync_ctx;
199 sync_ctx.consumed = 0;
200 sync_ctx.addr = addr;
201 sync_ctx.ndev = ndev;
204 vlan_for_each(ndev, cpsw_update_vlan_mc, &sync_ctx);
205 if (sync_ctx.consumed == num)
206 cpsw_set_mc(ndev, addr, -1, 0);
211 static int cpsw_purge_vlan_mc(struct net_device *vdev, int vid, void *ctx)
213 struct addr_sync_ctx *sync_ctx = ctx;
214 struct netdev_hw_addr *ha;
217 if (!vdev || !(vdev->flags & IFF_UP))
220 /* vlan address is relevant if its sync_cnt != 0 */
221 netdev_for_each_mc_addr(ha, vdev) {
222 if (ether_addr_equal(ha->addr, sync_ctx->addr)) {
223 found = ha->sync_cnt;
231 sync_ctx->consumed++;
232 cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 0);
236 static int cpsw_purge_all_mc(struct net_device *ndev, const u8 *addr, int num)
238 struct addr_sync_ctx sync_ctx;
240 sync_ctx.addr = addr;
241 sync_ctx.ndev = ndev;
242 sync_ctx.consumed = 0;
244 vlan_for_each(ndev, cpsw_purge_vlan_mc, &sync_ctx);
245 if (sync_ctx.consumed < num)
246 cpsw_set_mc(ndev, addr, -1, 0);
251 static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
253 struct cpsw_priv *priv = netdev_priv(ndev);
254 struct cpsw_common *cpsw = priv->cpsw;
256 if (ndev->flags & IFF_PROMISC) {
257 /* Enable promiscuous mode */
258 cpsw_set_promiscious(ndev, true);
259 cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI, priv->emac_port);
263 /* Disable promiscuous mode */
264 cpsw_set_promiscious(ndev, false);
266 /* Restore allmulti on vlans if necessary */
267 cpsw_ale_set_allmulti(cpsw->ale,
268 ndev->flags & IFF_ALLMULTI, priv->emac_port);
270 /* add/remove mcast address either for real netdev or for vlan */
271 __hw_addr_ref_sync_dev(&ndev->mc, ndev, cpsw_add_mc_addr,
275 static unsigned int cpsw_rxbuf_total_len(unsigned int len)
277 len += CPSW_HEADROOM_NA;
278 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
280 return SKB_DATA_ALIGN(len);
283 static void cpsw_rx_handler(void *token, int len, int status)
285 struct page *new_page, *page = token;
286 void *pa = page_address(page);
287 int headroom = CPSW_HEADROOM_NA;
288 struct cpsw_meta_xdp *xmeta;
289 struct cpsw_common *cpsw;
290 struct net_device *ndev;
291 int port, ch, pkt_size;
292 struct cpsw_priv *priv;
293 struct page_pool *pool;
299 xmeta = pa + CPSW_XMETA_OFFSET;
300 cpsw = ndev_to_cpsw(xmeta->ndev);
302 pkt_size = cpsw->rx_packet_max;
306 port = CPDMA_RX_SOURCE_PORT(status);
308 ndev = cpsw->slaves[--port].ndev;
311 priv = netdev_priv(ndev);
312 pool = cpsw->page_pool[ch];
314 if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
315 /* In dual emac mode check for all interfaces */
316 if (cpsw->usage_count && status >= 0) {
317 /* The packet received is for the interface which
318 * is already down and the other interface is up
319 * and running, instead of freeing which results
320 * in reducing of the number of rx descriptor in
321 * DMA engine, requeue page back to cpdma.
327 /* the interface is going down, pages are purged */
328 page_pool_recycle_direct(pool, page);
332 new_page = page_pool_dev_alloc_pages(pool);
333 if (unlikely(!new_page)) {
335 ndev->stats.rx_dropped++;
339 if (priv->xdp_prog) {
342 xdp_init_buff(&xdp, PAGE_SIZE, &priv->xdp_rxq[ch]);
343 if (status & CPDMA_RX_VLAN_ENCAP) {
344 headroom += CPSW_RX_VLAN_ENCAP_HDR_SIZE;
345 size -= CPSW_RX_VLAN_ENCAP_HDR_SIZE;
348 xdp_prepare_buff(&xdp, pa, headroom, size, false);
350 ret = cpsw_run_xdp(priv, ch, &xdp, page, priv->emac_port, &len);
351 if (ret != CPSW_XDP_PASS)
354 headroom = xdp.data - xdp.data_hard_start;
356 /* XDP prog can modify vlan tag, so can't use encap header */
357 status &= ~CPDMA_RX_VLAN_ENCAP;
360 /* pass skb to netstack if no XDP prog or returned XDP_PASS */
361 skb = build_skb(pa, cpsw_rxbuf_total_len(pkt_size));
363 ndev->stats.rx_dropped++;
364 page_pool_recycle_direct(pool, page);
368 skb->offload_fwd_mark = priv->offload_fwd_mark;
369 skb_reserve(skb, headroom);
372 if (status & CPDMA_RX_VLAN_ENCAP)
373 cpsw_rx_vlan_encap(skb);
374 if (priv->rx_ts_enabled)
375 cpts_rx_timestamp(cpsw->cpts, skb);
376 skb->protocol = eth_type_trans(skb, ndev);
378 /* mark skb for recycling */
379 skb_mark_for_recycle(skb);
380 netif_receive_skb(skb);
382 ndev->stats.rx_bytes += len;
383 ndev->stats.rx_packets++;
386 xmeta = page_address(new_page) + CPSW_XMETA_OFFSET;
390 dma = page_pool_get_dma_addr(new_page) + CPSW_HEADROOM_NA;
391 ret = cpdma_chan_submit_mapped(cpsw->rxv[ch].ch, new_page, dma,
394 WARN_ON(ret == -ENOMEM);
395 page_pool_recycle_direct(pool, new_page);
399 static int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
402 struct cpsw_common *cpsw = priv->cpsw;
403 int unreg_mcast_mask = 0;
408 port_mask = (1 << priv->emac_port) | ALE_PORT_HOST;
410 mcast_mask = ALE_PORT_HOST;
411 if (priv->ndev->flags & IFF_ALLMULTI)
412 unreg_mcast_mask = mcast_mask;
414 ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
419 ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
420 HOST_PORT_NUM, ALE_VLAN, vid);
424 ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
425 mcast_mask, ALE_VLAN, vid, 0);
427 goto clean_vlan_ucast;
431 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
432 HOST_PORT_NUM, ALE_VLAN, vid);
434 cpsw_ale_del_vlan(cpsw->ale, vid, 0);
438 static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
439 __be16 proto, u16 vid)
441 struct cpsw_priv *priv = netdev_priv(ndev);
442 struct cpsw_common *cpsw = priv->cpsw;
445 if (cpsw_is_switch_en(cpsw)) {
446 dev_dbg(cpsw->dev, ".ndo_vlan_rx_add_vid called in switch mode\n");
450 if (vid == cpsw->data.default_vlan)
453 ret = pm_runtime_resume_and_get(cpsw->dev);
457 /* In dual EMAC, reserved VLAN id should not be used for
458 * creating VLAN interfaces as this can break the dual
459 * EMAC port separation
461 for (i = 0; i < cpsw->data.slaves; i++) {
462 if (cpsw->slaves[i].ndev &&
463 vid == cpsw->slaves[i].port_vlan) {
469 dev_dbg(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
470 ret = cpsw_add_vlan_ale_entry(priv, vid);
472 pm_runtime_put(cpsw->dev);
476 static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg)
478 struct cpsw_priv *priv = arg;
483 cpsw_ndo_vlan_rx_add_vid(priv->ndev, 0, vid);
487 /* restore resources after port reset */
488 static void cpsw_restore(struct cpsw_priv *priv)
490 struct cpsw_common *cpsw = priv->cpsw;
492 /* restore vlan configurations */
493 vlan_for_each(priv->ndev, cpsw_restore_vlans, priv);
495 /* restore MQPRIO offload */
496 cpsw_mqprio_resume(&cpsw->slaves[priv->emac_port - 1], priv);
498 /* restore CBS offload */
499 cpsw_cbs_resume(&cpsw->slaves[priv->emac_port - 1], priv);
501 cpsw_qos_clsflower_resume(priv);
504 static void cpsw_init_stp_ale_entry(struct cpsw_common *cpsw)
506 static const char stpa[] = {0x01, 0x80, 0xc2, 0x0, 0x0, 0x0};
508 cpsw_ale_add_mcast(cpsw->ale, stpa,
509 ALE_PORT_HOST, ALE_SUPER, 0,
510 ALE_MCAST_BLOCK_LEARN_FWD);
513 static void cpsw_init_host_port_switch(struct cpsw_common *cpsw)
515 int vlan = cpsw->data.default_vlan;
517 writel(CPSW_FIFO_NORMAL_MODE, &cpsw->host_port_regs->tx_in_ctl);
519 writel(vlan, &cpsw->host_port_regs->port_vlan);
521 cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
522 ALE_ALL_PORTS, ALE_ALL_PORTS,
523 ALE_PORT_1 | ALE_PORT_2);
525 cpsw_init_stp_ale_entry(cpsw);
527 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 1);
528 dev_dbg(cpsw->dev, "Set P0_UNI_FLOOD\n");
529 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 0);
532 static void cpsw_init_host_port_dual_mac(struct cpsw_common *cpsw)
534 int vlan = cpsw->data.default_vlan;
536 writel(CPSW_FIFO_DUAL_MAC_MODE, &cpsw->host_port_regs->tx_in_ctl);
538 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 0);
539 dev_dbg(cpsw->dev, "unset P0_UNI_FLOOD\n");
541 writel(vlan, &cpsw->host_port_regs->port_vlan);
543 cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
544 /* learning make no sense in dual_mac mode */
545 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 1);
548 static void cpsw_init_host_port(struct cpsw_priv *priv)
550 struct cpsw_common *cpsw = priv->cpsw;
553 /* soft reset the controller and initialize ale */
554 soft_reset("cpsw", &cpsw->regs->soft_reset);
555 cpsw_ale_start(cpsw->ale);
557 /* switch to vlan unaware mode */
558 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
559 CPSW_ALE_VLAN_AWARE);
560 control_reg = readl(&cpsw->regs->control);
561 control_reg |= CPSW_VLAN_AWARE | CPSW_RX_VLAN_ENCAP;
562 writel(control_reg, &cpsw->regs->control);
564 /* setup host port priority mapping */
565 writel_relaxed(CPDMA_TX_PRIORITY_MAP,
566 &cpsw->host_port_regs->cpdma_tx_pri_map);
567 writel_relaxed(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
569 /* disable priority elevation */
570 writel_relaxed(0, &cpsw->regs->ptype);
572 /* enable statistics collection only on all ports */
573 writel_relaxed(0x7, &cpsw->regs->stat_port_en);
575 /* Enable internal fifo flow control */
576 writel(0x7, &cpsw->regs->flow_control);
578 if (cpsw_is_switch_en(cpsw))
579 cpsw_init_host_port_switch(cpsw);
581 cpsw_init_host_port_dual_mac(cpsw);
583 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
584 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
587 static void cpsw_port_add_dual_emac_def_ale_entries(struct cpsw_priv *priv,
588 struct cpsw_slave *slave)
590 u32 port_mask = 1 << priv->emac_port | ALE_PORT_HOST;
591 struct cpsw_common *cpsw = priv->cpsw;
594 reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
596 slave_write(slave, slave->port_vlan, reg);
598 cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
599 port_mask, port_mask, 0);
600 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
601 ALE_PORT_HOST, ALE_VLAN, slave->port_vlan,
603 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
604 HOST_PORT_NUM, ALE_VLAN |
605 ALE_SECURE, slave->port_vlan);
606 cpsw_ale_control_set(cpsw->ale, priv->emac_port,
607 ALE_PORT_DROP_UNKNOWN_VLAN, 1);
608 /* learning make no sense in dual_mac mode */
609 cpsw_ale_control_set(cpsw->ale, priv->emac_port,
610 ALE_PORT_NOLEARN, 1);
613 static void cpsw_port_add_switch_def_ale_entries(struct cpsw_priv *priv,
614 struct cpsw_slave *slave)
616 u32 port_mask = 1 << priv->emac_port | ALE_PORT_HOST;
617 struct cpsw_common *cpsw = priv->cpsw;
620 cpsw_ale_control_set(cpsw->ale, priv->emac_port,
621 ALE_PORT_DROP_UNKNOWN_VLAN, 0);
622 cpsw_ale_control_set(cpsw->ale, priv->emac_port,
623 ALE_PORT_NOLEARN, 0);
624 /* disabling SA_UPDATE required to make stp work, without this setting
625 * Host MAC addresses will jump between ports.
626 * As per TRM MAC address can be defined as unicast supervisory (super)
627 * by setting both (ALE_BLOCKED | ALE_SECURE) which should prevent
628 * SA_UPDATE, but HW seems works incorrectly and setting ALE_SECURE
629 * causes STP packets to be dropped due to ingress filter
630 * if (source address found) and (secure) and
631 * (receive port number != port_number))
632 * then discard the packet
634 cpsw_ale_control_set(cpsw->ale, priv->emac_port,
635 ALE_PORT_NO_SA_UPDATE, 1);
637 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
638 port_mask, ALE_VLAN, slave->port_vlan,
640 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
641 HOST_PORT_NUM, ALE_VLAN, slave->port_vlan);
643 reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
645 slave_write(slave, slave->port_vlan, reg);
648 static void cpsw_adjust_link(struct net_device *ndev)
650 struct cpsw_priv *priv = netdev_priv(ndev);
651 struct cpsw_common *cpsw = priv->cpsw;
652 struct cpsw_slave *slave;
653 struct phy_device *phy;
656 slave = &cpsw->slaves[priv->emac_port - 1];
663 mac_control = CPSW_SL_CTL_GMII_EN;
665 if (phy->speed == 1000)
666 mac_control |= CPSW_SL_CTL_GIG;
668 mac_control |= CPSW_SL_CTL_FULLDUPLEX;
670 /* set speed_in input in case RMII mode is used in 100Mbps */
671 if (phy->speed == 100)
672 mac_control |= CPSW_SL_CTL_IFCTL_A;
673 /* in band mode only works in 10Mbps RGMII mode */
674 else if ((phy->speed == 10) && phy_interface_is_rgmii(phy))
675 mac_control |= CPSW_SL_CTL_EXT_EN; /* In Band mode */
678 mac_control |= CPSW_SL_CTL_RX_FLOW_EN;
681 mac_control |= CPSW_SL_CTL_TX_FLOW_EN;
683 if (mac_control != slave->mac_control)
684 cpsw_sl_ctl_set(slave->mac_sl, mac_control);
686 /* enable forwarding */
687 cpsw_ale_control_set(cpsw->ale, priv->emac_port,
688 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
690 netif_tx_wake_all_queues(ndev);
692 if (priv->shp_cfg_speed &&
693 priv->shp_cfg_speed != slave->phy->speed &&
694 !cpsw_shp_is_off(priv))
695 dev_warn(priv->dev, "Speed was changed, CBS shaper speeds are changed!");
697 netif_tx_stop_all_queues(ndev);
700 /* disable forwarding */
701 cpsw_ale_control_set(cpsw->ale, priv->emac_port,
702 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
704 cpsw_sl_wait_for_idle(slave->mac_sl, 100);
706 cpsw_sl_ctl_reset(slave->mac_sl);
709 if (mac_control != slave->mac_control)
710 phy_print_status(phy);
712 slave->mac_control = mac_control;
714 if (phy->link && cpsw_need_resplit(cpsw))
715 cpsw_split_res(cpsw);
718 static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
720 struct cpsw_common *cpsw = priv->cpsw;
721 struct phy_device *phy;
723 cpsw_sl_reset(slave->mac_sl, 100);
724 cpsw_sl_ctl_reset(slave->mac_sl);
726 /* setup priority mapping */
727 cpsw_sl_reg_write(slave->mac_sl, CPSW_SL_RX_PRI_MAP,
728 RX_PRIORITY_MAPPING);
730 switch (cpsw->version) {
732 slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
733 /* Increase RX FIFO size to 5 for supporting fullduplex
737 (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
738 CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS);
743 slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
744 /* Increase RX FIFO size to 5 for supporting fullduplex
748 (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
749 CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS);
753 /* setup max packet size, and mac address */
754 cpsw_sl_reg_write(slave->mac_sl, CPSW_SL_RX_MAXLEN,
755 cpsw->rx_packet_max);
756 cpsw_set_slave_mac(slave, priv);
758 slave->mac_control = 0; /* no link yet */
760 if (cpsw_is_switch_en(cpsw))
761 cpsw_port_add_switch_def_ale_entries(priv, slave);
763 cpsw_port_add_dual_emac_def_ale_entries(priv, slave);
765 if (!slave->data->phy_node)
766 dev_err(priv->dev, "no phy found on slave %d\n",
768 phy = of_phy_connect(priv->ndev, slave->data->phy_node,
769 &cpsw_adjust_link, 0, slave->data->phy_if);
771 dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n",
772 slave->data->phy_node,
777 phy->mac_managed_pm = true;
781 phy_attached_info(slave->phy);
783 phy_start(slave->phy);
785 /* Configure GMII_SEL register */
786 phy_set_mode_ext(slave->data->ifphy, PHY_MODE_ETHERNET,
787 slave->data->phy_if);
790 static int cpsw_ndo_stop(struct net_device *ndev)
792 struct cpsw_priv *priv = netdev_priv(ndev);
793 struct cpsw_common *cpsw = priv->cpsw;
794 struct cpsw_slave *slave;
796 cpsw_info(priv, ifdown, "shutting down ndev\n");
797 slave = &cpsw->slaves[priv->emac_port - 1];
799 phy_stop(slave->phy);
801 netif_tx_stop_all_queues(priv->ndev);
804 phy_disconnect(slave->phy);
808 __hw_addr_ref_unsync_dev(&ndev->mc, ndev, cpsw_purge_all_mc);
810 if (cpsw->usage_count <= 1) {
811 napi_disable(&cpsw->napi_rx);
812 napi_disable(&cpsw->napi_tx);
813 cpts_unregister(cpsw->cpts);
814 cpsw_intr_disable(cpsw);
815 cpdma_ctlr_stop(cpsw->dma);
816 cpsw_ale_stop(cpsw->ale);
817 cpsw_destroy_xdp_rxqs(cpsw);
820 if (cpsw_need_resplit(cpsw))
821 cpsw_split_res(cpsw);
824 pm_runtime_put_sync(cpsw->dev);
828 static int cpsw_ndo_open(struct net_device *ndev)
830 struct cpsw_priv *priv = netdev_priv(ndev);
831 struct cpsw_common *cpsw = priv->cpsw;
834 dev_info(priv->dev, "starting ndev. mode: %s\n",
835 cpsw_is_switch_en(cpsw) ? "switch" : "dual_mac");
836 ret = pm_runtime_resume_and_get(cpsw->dev);
840 /* Notify the stack of the actual queue counts. */
841 ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
843 dev_err(priv->dev, "cannot set real number of tx queues\n");
847 ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
849 dev_err(priv->dev, "cannot set real number of rx queues\n");
853 /* Initialize host and slave ports */
854 if (!cpsw->usage_count)
855 cpsw_init_host_port(priv);
856 cpsw_slave_open(&cpsw->slaves[priv->emac_port - 1], priv);
858 /* initialize shared resources for every ndev */
859 if (!cpsw->usage_count) {
860 /* create rxqs for both infs in dual mac as they use same pool
861 * and must be destroyed together when no users.
863 ret = cpsw_create_xdp_rxqs(cpsw);
867 ret = cpsw_fill_rx_channels(priv);
872 if (cpts_register(cpsw->cpts))
873 dev_err(priv->dev, "error registering cpts device\n");
875 writel(0x10, &cpsw->wr_regs->misc_en);
878 napi_enable(&cpsw->napi_rx);
879 napi_enable(&cpsw->napi_tx);
881 if (cpsw->tx_irq_disabled) {
882 cpsw->tx_irq_disabled = false;
883 enable_irq(cpsw->irqs_table[1]);
886 if (cpsw->rx_irq_disabled) {
887 cpsw->rx_irq_disabled = false;
888 enable_irq(cpsw->irqs_table[0]);
894 /* Enable Interrupt pacing if configured */
895 if (cpsw->coal_intvl != 0) {
896 struct ethtool_coalesce coal;
898 coal.rx_coalesce_usecs = cpsw->coal_intvl;
899 cpsw_set_coalesce(ndev, &coal, NULL, NULL);
902 cpdma_ctlr_start(cpsw->dma);
903 cpsw_intr_enable(cpsw);
912 pm_runtime_put_sync(cpsw->dev);
916 static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
917 struct net_device *ndev)
919 struct cpsw_priv *priv = netdev_priv(ndev);
920 struct cpsw_common *cpsw = priv->cpsw;
921 struct cpts *cpts = cpsw->cpts;
922 struct netdev_queue *txq;
923 struct cpdma_chan *txch;
926 if (skb_put_padto(skb, READ_ONCE(priv->tx_packet_min))) {
927 cpsw_err(priv, tx_err, "packet pad failed\n");
928 ndev->stats.tx_dropped++;
929 return NET_XMIT_DROP;
932 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
933 priv->tx_ts_enabled && cpts_can_timestamp(cpts, skb))
934 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
936 q_idx = skb_get_queue_mapping(skb);
937 if (q_idx >= cpsw->tx_ch_num)
938 q_idx = q_idx % cpsw->tx_ch_num;
940 txch = cpsw->txv[q_idx].ch;
941 txq = netdev_get_tx_queue(ndev, q_idx);
942 skb_tx_timestamp(skb);
943 ret = cpdma_chan_submit(txch, skb, skb->data, skb->len,
945 if (unlikely(ret != 0)) {
946 cpsw_err(priv, tx_err, "desc submit failed\n");
950 /* If there is no more tx desc left free then we need to
951 * tell the kernel to stop sending us tx frames.
953 if (unlikely(!cpdma_check_free_tx_desc(txch))) {
954 netif_tx_stop_queue(txq);
956 /* Barrier, so that stop_queue visible to other cpus */
957 smp_mb__after_atomic();
959 if (cpdma_check_free_tx_desc(txch))
960 netif_tx_wake_queue(txq);
965 ndev->stats.tx_dropped++;
966 netif_tx_stop_queue(txq);
968 /* Barrier, so that stop_queue visible to other cpus */
969 smp_mb__after_atomic();
971 if (cpdma_check_free_tx_desc(txch))
972 netif_tx_wake_queue(txq);
974 return NETDEV_TX_BUSY;
977 static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
979 struct sockaddr *addr = (struct sockaddr *)p;
980 struct cpsw_priv *priv = netdev_priv(ndev);
981 struct cpsw_common *cpsw = priv->cpsw;
986 slave_no = cpsw_slave_index(cpsw, priv);
987 if (!is_valid_ether_addr(addr->sa_data))
988 return -EADDRNOTAVAIL;
990 ret = pm_runtime_resume_and_get(cpsw->dev);
994 vid = cpsw->slaves[slave_no].port_vlan;
995 flags = ALE_VLAN | ALE_SECURE;
997 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
999 cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
1002 ether_addr_copy(priv->mac_addr, addr->sa_data);
1003 eth_hw_addr_set(ndev, priv->mac_addr);
1004 cpsw_set_slave_mac(&cpsw->slaves[slave_no], priv);
1006 pm_runtime_put(cpsw->dev);
1011 static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
1012 __be16 proto, u16 vid)
1014 struct cpsw_priv *priv = netdev_priv(ndev);
1015 struct cpsw_common *cpsw = priv->cpsw;
1019 if (cpsw_is_switch_en(cpsw)) {
1020 dev_dbg(cpsw->dev, "ndo del vlan is called in switch mode\n");
1024 if (vid == cpsw->data.default_vlan)
1027 ret = pm_runtime_resume_and_get(cpsw->dev);
1031 /* reset the return code as pm_runtime_get_sync() can return
1032 * non zero values as well.
1035 for (i = 0; i < cpsw->data.slaves; i++) {
1036 if (cpsw->slaves[i].ndev &&
1037 vid == cpsw->slaves[i].port_vlan) {
1043 dev_dbg(priv->dev, "removing vlanid %d from vlan filter\n", vid);
1044 ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
1046 dev_err(priv->dev, "cpsw_ale_del_vlan() failed: ret %d\n", ret);
1047 ret = cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
1048 HOST_PORT_NUM, ALE_VLAN, vid);
1050 dev_err(priv->dev, "cpsw_ale_del_ucast() failed: ret %d\n",
1052 ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
1055 dev_err(priv->dev, "cpsw_ale_del_mcast failed. ret %d\n",
1057 cpsw_ale_flush_multicast(cpsw->ale, ALE_PORT_HOST, vid);
1060 pm_runtime_put(cpsw->dev);
1064 static int cpsw_ndo_get_phys_port_name(struct net_device *ndev, char *name,
1067 struct cpsw_priv *priv = netdev_priv(ndev);
1070 err = snprintf(name, len, "p%d", priv->emac_port);
1078 #ifdef CONFIG_NET_POLL_CONTROLLER
1079 static void cpsw_ndo_poll_controller(struct net_device *ndev)
1081 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1083 cpsw_intr_disable(cpsw);
1084 cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
1085 cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
1086 cpsw_intr_enable(cpsw);
1090 static int cpsw_ndo_xdp_xmit(struct net_device *ndev, int n,
1091 struct xdp_frame **frames, u32 flags)
1093 struct cpsw_priv *priv = netdev_priv(ndev);
1094 struct xdp_frame *xdpf;
1097 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
1100 for (i = 0; i < n; i++) {
1102 if (xdpf->len < READ_ONCE(priv->tx_packet_min))
1105 if (cpsw_xdp_tx_frame(priv, xdpf, NULL, priv->emac_port))
1113 static int cpsw_get_port_parent_id(struct net_device *ndev,
1114 struct netdev_phys_item_id *ppid)
1116 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1118 ppid->id_len = sizeof(cpsw->base_mac);
1119 memcpy(&ppid->id, &cpsw->base_mac, ppid->id_len);
1124 static const struct net_device_ops cpsw_netdev_ops = {
1125 .ndo_open = cpsw_ndo_open,
1126 .ndo_stop = cpsw_ndo_stop,
1127 .ndo_start_xmit = cpsw_ndo_start_xmit,
1128 .ndo_set_mac_address = cpsw_ndo_set_mac_address,
1129 .ndo_eth_ioctl = cpsw_ndo_ioctl,
1130 .ndo_validate_addr = eth_validate_addr,
1131 .ndo_tx_timeout = cpsw_ndo_tx_timeout,
1132 .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
1133 .ndo_set_tx_maxrate = cpsw_ndo_set_tx_maxrate,
1134 #ifdef CONFIG_NET_POLL_CONTROLLER
1135 .ndo_poll_controller = cpsw_ndo_poll_controller,
1137 .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
1138 .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
1139 .ndo_setup_tc = cpsw_ndo_setup_tc,
1140 .ndo_get_phys_port_name = cpsw_ndo_get_phys_port_name,
1141 .ndo_bpf = cpsw_ndo_bpf,
1142 .ndo_xdp_xmit = cpsw_ndo_xdp_xmit,
1143 .ndo_get_port_parent_id = cpsw_get_port_parent_id,
1146 static void cpsw_get_drvinfo(struct net_device *ndev,
1147 struct ethtool_drvinfo *info)
1149 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1150 struct platform_device *pdev;
1152 pdev = to_platform_device(cpsw->dev);
1153 strscpy(info->driver, "cpsw-switch", sizeof(info->driver));
1154 strscpy(info->version, "2.0", sizeof(info->version));
1155 strscpy(info->bus_info, pdev->name, sizeof(info->bus_info));
1158 static int cpsw_set_pauseparam(struct net_device *ndev,
1159 struct ethtool_pauseparam *pause)
1161 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1162 struct cpsw_priv *priv = netdev_priv(ndev);
1165 slave_no = cpsw_slave_index(cpsw, priv);
1166 if (!cpsw->slaves[slave_no].phy)
1169 if (!phy_validate_pause(cpsw->slaves[slave_no].phy, pause))
1172 priv->rx_pause = pause->rx_pause ? true : false;
1173 priv->tx_pause = pause->tx_pause ? true : false;
1175 phy_set_asym_pause(cpsw->slaves[slave_no].phy,
1176 priv->rx_pause, priv->tx_pause);
1181 static int cpsw_set_channels(struct net_device *ndev,
1182 struct ethtool_channels *chs)
1184 return cpsw_set_channels_common(ndev, chs, cpsw_rx_handler);
1187 static const struct ethtool_ops cpsw_ethtool_ops = {
1188 .supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS,
1189 .get_drvinfo = cpsw_get_drvinfo,
1190 .get_msglevel = cpsw_get_msglevel,
1191 .set_msglevel = cpsw_set_msglevel,
1192 .get_link = ethtool_op_get_link,
1193 .get_ts_info = cpsw_get_ts_info,
1194 .get_coalesce = cpsw_get_coalesce,
1195 .set_coalesce = cpsw_set_coalesce,
1196 .get_sset_count = cpsw_get_sset_count,
1197 .get_strings = cpsw_get_strings,
1198 .get_ethtool_stats = cpsw_get_ethtool_stats,
1199 .get_pauseparam = cpsw_get_pauseparam,
1200 .set_pauseparam = cpsw_set_pauseparam,
1201 .get_wol = cpsw_get_wol,
1202 .set_wol = cpsw_set_wol,
1203 .get_regs_len = cpsw_get_regs_len,
1204 .get_regs = cpsw_get_regs,
1205 .begin = cpsw_ethtool_op_begin,
1206 .complete = cpsw_ethtool_op_complete,
1207 .get_channels = cpsw_get_channels,
1208 .set_channels = cpsw_set_channels,
1209 .get_link_ksettings = cpsw_get_link_ksettings,
1210 .set_link_ksettings = cpsw_set_link_ksettings,
1211 .get_eee = cpsw_get_eee,
1212 .set_eee = cpsw_set_eee,
1213 .nway_reset = cpsw_nway_reset,
1214 .get_ringparam = cpsw_get_ringparam,
1215 .set_ringparam = cpsw_set_ringparam,
1218 static int cpsw_probe_dt(struct cpsw_common *cpsw)
1220 struct device_node *node = cpsw->dev->of_node, *tmp_node, *port_np;
1221 struct cpsw_platform_data *data = &cpsw->data;
1222 struct device *dev = cpsw->dev;
1229 tmp_node = of_get_child_by_name(node, "ethernet-ports");
1232 data->slaves = of_get_child_count(tmp_node);
1233 if (data->slaves != CPSW_SLAVE_PORTS_NUM) {
1234 of_node_put(tmp_node);
1238 data->active_slave = 0;
1239 data->channels = CPSW_MAX_QUEUES;
1240 data->dual_emac = true;
1241 data->bd_ram_size = CPSW_BD_RAM_SIZE;
1242 data->mac_control = 0;
1244 data->slave_data = devm_kcalloc(dev, CPSW_SLAVE_PORTS_NUM,
1245 sizeof(struct cpsw_slave_data),
1247 if (!data->slave_data) {
1248 of_node_put(tmp_node);
1252 /* Populate all the child nodes here...
1254 ret = devm_of_platform_populate(dev);
1255 /* We do not want to force this, as in some cases may not have child */
1257 dev_warn(dev, "Doesn't have any child node\n");
1259 for_each_child_of_node(tmp_node, port_np) {
1260 struct cpsw_slave_data *slave_data;
1263 ret = of_property_read_u32(port_np, "reg", &port_id);
1265 dev_err(dev, "%pOF error reading port_id %d\n",
1270 if (!port_id || port_id > CPSW_SLAVE_PORTS_NUM) {
1271 dev_err(dev, "%pOF has invalid port_id %u\n",
1277 slave_data = &data->slave_data[port_id - 1];
1279 slave_data->disabled = !of_device_is_available(port_np);
1280 if (slave_data->disabled)
1283 slave_data->slave_node = port_np;
1284 slave_data->ifphy = devm_of_phy_get(dev, port_np, NULL);
1285 if (IS_ERR(slave_data->ifphy)) {
1286 ret = PTR_ERR(slave_data->ifphy);
1287 dev_err(dev, "%pOF: Error retrieving port phy: %d\n",
1292 if (of_phy_is_fixed_link(port_np)) {
1293 ret = of_phy_register_fixed_link(port_np);
1295 dev_err_probe(dev, ret, "%pOF failed to register fixed-link phy\n",
1299 slave_data->phy_node = of_node_get(port_np);
1301 slave_data->phy_node =
1302 of_parse_phandle(port_np, "phy-handle", 0);
1305 if (!slave_data->phy_node) {
1306 dev_err(dev, "%pOF no phy found\n", port_np);
1311 ret = of_get_phy_mode(port_np, &slave_data->phy_if);
1313 dev_err(dev, "%pOF read phy-mode err %d\n",
1318 ret = of_get_mac_address(port_np, slave_data->mac_addr);
1320 ret = ti_cm_get_macid(dev, port_id - 1,
1321 slave_data->mac_addr);
1326 if (of_property_read_u32(port_np, "ti,dual-emac-pvid",
1328 dev_err(dev, "%pOF Missing dual_emac_res_vlan in DT.\n",
1330 slave_data->dual_emac_res_vlan = port_id;
1331 dev_err(dev, "%pOF Using %d as Reserved VLAN\n",
1332 port_np, slave_data->dual_emac_res_vlan);
1334 slave_data->dual_emac_res_vlan = prop;
1338 of_node_put(tmp_node);
1342 of_node_put(port_np);
1343 of_node_put(tmp_node);
1347 static void cpsw_remove_dt(struct cpsw_common *cpsw)
1349 struct cpsw_platform_data *data = &cpsw->data;
1352 for (i = 0; i < cpsw->data.slaves; i++) {
1353 struct cpsw_slave_data *slave_data = &data->slave_data[i];
1354 struct device_node *port_np = slave_data->phy_node;
1357 if (of_phy_is_fixed_link(port_np))
1358 of_phy_deregister_fixed_link(port_np);
1360 of_node_put(port_np);
1365 static int cpsw_create_ports(struct cpsw_common *cpsw)
1367 struct cpsw_platform_data *data = &cpsw->data;
1368 struct net_device *ndev, *napi_ndev = NULL;
1369 struct device *dev = cpsw->dev;
1370 struct cpsw_priv *priv;
1373 for (i = 0; i < cpsw->data.slaves; i++) {
1374 struct cpsw_slave_data *slave_data = &data->slave_data[i];
1376 if (slave_data->disabled)
1379 ndev = devm_alloc_etherdev_mqs(dev, sizeof(struct cpsw_priv),
1383 dev_err(dev, "error allocating net_device\n");
1387 priv = netdev_priv(ndev);
1391 priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
1392 priv->emac_port = i + 1;
1393 priv->tx_packet_min = CPSW_MIN_PACKET_SIZE;
1395 if (is_valid_ether_addr(slave_data->mac_addr)) {
1396 ether_addr_copy(priv->mac_addr, slave_data->mac_addr);
1397 dev_info(cpsw->dev, "Detected MACID = %pM\n",
1400 eth_random_addr(slave_data->mac_addr);
1401 dev_info(cpsw->dev, "Random MACID = %pM\n",
1404 eth_hw_addr_set(ndev, slave_data->mac_addr);
1405 ether_addr_copy(priv->mac_addr, slave_data->mac_addr);
1407 cpsw->slaves[i].ndev = ndev;
1409 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER |
1410 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_NETNS_LOCAL | NETIF_F_HW_TC;
1412 ndev->xdp_features = NETDEV_XDP_ACT_BASIC |
1413 NETDEV_XDP_ACT_REDIRECT |
1414 NETDEV_XDP_ACT_NDO_XMIT;
1416 ndev->netdev_ops = &cpsw_netdev_ops;
1417 ndev->ethtool_ops = &cpsw_ethtool_ops;
1418 SET_NETDEV_DEV(ndev, dev);
1421 /* CPSW Host port CPDMA interface is shared between
1422 * ports and there is only one TX and one RX IRQs
1423 * available for all possible TX and RX channels
1426 netif_napi_add(ndev, &cpsw->napi_rx,
1427 cpsw->quirk_irq ? cpsw_rx_poll : cpsw_rx_mq_poll);
1428 netif_napi_add_tx(ndev, &cpsw->napi_tx,
1430 cpsw_tx_poll : cpsw_tx_mq_poll);
1439 static void cpsw_unregister_ports(struct cpsw_common *cpsw)
1443 for (i = 0; i < cpsw->data.slaves; i++) {
1444 if (!cpsw->slaves[i].ndev)
1447 unregister_netdev(cpsw->slaves[i].ndev);
1451 static int cpsw_register_ports(struct cpsw_common *cpsw)
1455 for (i = 0; i < cpsw->data.slaves; i++) {
1456 if (!cpsw->slaves[i].ndev)
1459 /* register the network device */
1460 ret = register_netdev(cpsw->slaves[i].ndev);
1463 "cpsw: err registering net device%d\n", i);
1464 cpsw->slaves[i].ndev = NULL;
1470 cpsw_unregister_ports(cpsw);
1474 bool cpsw_port_dev_check(const struct net_device *ndev)
1476 if (ndev->netdev_ops == &cpsw_netdev_ops) {
1477 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1479 return !cpsw->data.dual_emac;
1485 static void cpsw_port_offload_fwd_mark_update(struct cpsw_common *cpsw)
1490 if (!cpsw->ale_bypass &&
1491 (cpsw->br_members == (ALE_PORT_1 | ALE_PORT_2)))
1494 dev_dbg(cpsw->dev, "set offload_fwd_mark %d\n", set_val);
1496 for (i = 0; i < cpsw->data.slaves; i++) {
1497 struct net_device *sl_ndev = cpsw->slaves[i].ndev;
1498 struct cpsw_priv *priv = netdev_priv(sl_ndev);
1500 priv->offload_fwd_mark = set_val;
1504 static int cpsw_netdevice_port_link(struct net_device *ndev,
1505 struct net_device *br_ndev,
1506 struct netlink_ext_ack *extack)
1508 struct cpsw_priv *priv = netdev_priv(ndev);
1509 struct cpsw_common *cpsw = priv->cpsw;
1512 if (!cpsw->br_members) {
1513 cpsw->hw_bridge_dev = br_ndev;
1515 /* This is adding the port to a second bridge, this is
1518 if (cpsw->hw_bridge_dev != br_ndev)
1522 err = switchdev_bridge_port_offload(ndev, ndev, NULL, NULL, NULL,
1527 cpsw->br_members |= BIT(priv->emac_port);
1529 cpsw_port_offload_fwd_mark_update(cpsw);
1534 static void cpsw_netdevice_port_unlink(struct net_device *ndev)
1536 struct cpsw_priv *priv = netdev_priv(ndev);
1537 struct cpsw_common *cpsw = priv->cpsw;
1539 switchdev_bridge_port_unoffload(ndev, NULL, NULL, NULL);
1541 cpsw->br_members &= ~BIT(priv->emac_port);
1543 cpsw_port_offload_fwd_mark_update(cpsw);
1545 if (!cpsw->br_members)
1546 cpsw->hw_bridge_dev = NULL;
1549 /* netdev notifier */
1550 static int cpsw_netdevice_event(struct notifier_block *unused,
1551 unsigned long event, void *ptr)
1553 struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
1554 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1555 struct netdev_notifier_changeupper_info *info;
1556 int ret = NOTIFY_DONE;
1558 if (!cpsw_port_dev_check(ndev))
1562 case NETDEV_CHANGEUPPER:
1565 if (netif_is_bridge_master(info->upper_dev)) {
1567 ret = cpsw_netdevice_port_link(ndev,
1571 cpsw_netdevice_port_unlink(ndev);
1578 return notifier_from_errno(ret);
1581 static struct notifier_block cpsw_netdevice_nb __read_mostly = {
1582 .notifier_call = cpsw_netdevice_event,
1585 static int cpsw_register_notifiers(struct cpsw_common *cpsw)
1589 ret = register_netdevice_notifier(&cpsw_netdevice_nb);
1591 dev_err(cpsw->dev, "can't register netdevice notifier\n");
1595 ret = cpsw_switchdev_register_notifiers(cpsw);
1597 unregister_netdevice_notifier(&cpsw_netdevice_nb);
1602 static void cpsw_unregister_notifiers(struct cpsw_common *cpsw)
1604 cpsw_switchdev_unregister_notifiers(cpsw);
1605 unregister_netdevice_notifier(&cpsw_netdevice_nb);
1608 static const struct devlink_ops cpsw_devlink_ops = {
1611 static int cpsw_dl_switch_mode_get(struct devlink *dl, u32 id,
1612 struct devlink_param_gset_ctx *ctx)
1614 struct cpsw_devlink *dl_priv = devlink_priv(dl);
1615 struct cpsw_common *cpsw = dl_priv->cpsw;
1617 dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1619 if (id != CPSW_DL_PARAM_SWITCH_MODE)
1622 ctx->val.vbool = !cpsw->data.dual_emac;
1627 static int cpsw_dl_switch_mode_set(struct devlink *dl, u32 id,
1628 struct devlink_param_gset_ctx *ctx)
1630 struct cpsw_devlink *dl_priv = devlink_priv(dl);
1631 struct cpsw_common *cpsw = dl_priv->cpsw;
1632 int vlan = cpsw->data.default_vlan;
1633 bool switch_en = ctx->val.vbool;
1634 bool if_running = false;
1637 dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1639 if (id != CPSW_DL_PARAM_SWITCH_MODE)
1642 if (switch_en == !cpsw->data.dual_emac)
1645 if (!switch_en && cpsw->br_members) {
1646 dev_err(cpsw->dev, "Remove ports from BR before disabling switch mode\n");
1652 for (i = 0; i < cpsw->data.slaves; i++) {
1653 struct cpsw_slave *slave = &cpsw->slaves[i];
1654 struct net_device *sl_ndev = slave->ndev;
1656 if (!sl_ndev || !netif_running(sl_ndev))
1663 /* all ndevs are down */
1664 cpsw->data.dual_emac = !switch_en;
1665 for (i = 0; i < cpsw->data.slaves; i++) {
1666 struct cpsw_slave *slave = &cpsw->slaves[i];
1667 struct net_device *sl_ndev = slave->ndev;
1673 vlan = cpsw->data.default_vlan;
1675 vlan = slave->data->dual_emac_res_vlan;
1676 slave->port_vlan = vlan;
1682 dev_info(cpsw->dev, "Enable switch mode\n");
1684 /* enable bypass - no forwarding; all traffic goes to Host */
1685 cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1);
1687 /* clean up ALE table */
1688 cpsw_ale_control_set(cpsw->ale, 0, ALE_CLEAR, 1);
1689 cpsw_ale_control_get(cpsw->ale, 0, ALE_AGEOUT);
1691 cpsw_init_host_port_switch(cpsw);
1693 for (i = 0; i < cpsw->data.slaves; i++) {
1694 struct cpsw_slave *slave = &cpsw->slaves[i];
1695 struct net_device *sl_ndev = slave->ndev;
1696 struct cpsw_priv *priv;
1701 priv = netdev_priv(sl_ndev);
1702 slave->port_vlan = vlan;
1703 WRITE_ONCE(priv->tx_packet_min, CPSW_MIN_PACKET_SIZE_VLAN);
1704 if (netif_running(sl_ndev))
1705 cpsw_port_add_switch_def_ale_entries(priv,
1709 cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 0);
1710 cpsw->data.dual_emac = false;
1712 dev_info(cpsw->dev, "Disable switch mode\n");
1714 /* enable bypass - no forwarding; all traffic goes to Host */
1715 cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1);
1717 cpsw_ale_control_set(cpsw->ale, 0, ALE_CLEAR, 1);
1718 cpsw_ale_control_get(cpsw->ale, 0, ALE_AGEOUT);
1720 cpsw_init_host_port_dual_mac(cpsw);
1722 for (i = 0; i < cpsw->data.slaves; i++) {
1723 struct cpsw_slave *slave = &cpsw->slaves[i];
1724 struct net_device *sl_ndev = slave->ndev;
1725 struct cpsw_priv *priv;
1730 priv = netdev_priv(slave->ndev);
1731 slave->port_vlan = slave->data->dual_emac_res_vlan;
1732 WRITE_ONCE(priv->tx_packet_min, CPSW_MIN_PACKET_SIZE);
1733 cpsw_port_add_dual_emac_def_ale_entries(priv, slave);
1736 cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 0);
1737 cpsw->data.dual_emac = true;
1745 static int cpsw_dl_ale_ctrl_get(struct devlink *dl, u32 id,
1746 struct devlink_param_gset_ctx *ctx)
1748 struct cpsw_devlink *dl_priv = devlink_priv(dl);
1749 struct cpsw_common *cpsw = dl_priv->cpsw;
1751 dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1754 case CPSW_DL_PARAM_ALE_BYPASS:
1755 ctx->val.vbool = cpsw_ale_control_get(cpsw->ale, 0, ALE_BYPASS);
1764 static int cpsw_dl_ale_ctrl_set(struct devlink *dl, u32 id,
1765 struct devlink_param_gset_ctx *ctx)
1767 struct cpsw_devlink *dl_priv = devlink_priv(dl);
1768 struct cpsw_common *cpsw = dl_priv->cpsw;
1769 int ret = -EOPNOTSUPP;
1771 dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1774 case CPSW_DL_PARAM_ALE_BYPASS:
1775 ret = cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS,
1778 cpsw->ale_bypass = ctx->val.vbool;
1779 cpsw_port_offload_fwd_mark_update(cpsw);
1789 static const struct devlink_param cpsw_devlink_params[] = {
1790 DEVLINK_PARAM_DRIVER(CPSW_DL_PARAM_SWITCH_MODE,
1791 "switch_mode", DEVLINK_PARAM_TYPE_BOOL,
1792 BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1793 cpsw_dl_switch_mode_get, cpsw_dl_switch_mode_set,
1795 DEVLINK_PARAM_DRIVER(CPSW_DL_PARAM_ALE_BYPASS,
1796 "ale_bypass", DEVLINK_PARAM_TYPE_BOOL,
1797 BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1798 cpsw_dl_ale_ctrl_get, cpsw_dl_ale_ctrl_set, NULL),
1801 static int cpsw_register_devlink(struct cpsw_common *cpsw)
1803 struct device *dev = cpsw->dev;
1804 struct cpsw_devlink *dl_priv;
1807 cpsw->devlink = devlink_alloc(&cpsw_devlink_ops, sizeof(*dl_priv), dev);
1811 dl_priv = devlink_priv(cpsw->devlink);
1812 dl_priv->cpsw = cpsw;
1814 ret = devlink_params_register(cpsw->devlink, cpsw_devlink_params,
1815 ARRAY_SIZE(cpsw_devlink_params));
1817 dev_err(dev, "DL params reg fail ret:%d\n", ret);
1821 devlink_register(cpsw->devlink);
1825 devlink_free(cpsw->devlink);
1829 static void cpsw_unregister_devlink(struct cpsw_common *cpsw)
1831 devlink_unregister(cpsw->devlink);
1832 devlink_params_unregister(cpsw->devlink, cpsw_devlink_params,
1833 ARRAY_SIZE(cpsw_devlink_params));
1834 devlink_free(cpsw->devlink);
1837 static const struct of_device_id cpsw_of_mtable[] = {
1838 { .compatible = "ti,cpsw-switch"},
1839 { .compatible = "ti,am335x-cpsw-switch"},
1840 { .compatible = "ti,am4372-cpsw-switch"},
1841 { .compatible = "ti,dra7-cpsw-switch"},
1844 MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
1846 static const struct soc_device_attribute cpsw_soc_devices[] = {
1847 { .family = "AM33xx", .revision = "ES1.0"},
1851 static int cpsw_probe(struct platform_device *pdev)
1853 const struct soc_device_attribute *soc;
1854 struct device *dev = &pdev->dev;
1855 struct cpsw_common *cpsw;
1856 struct resource *ss_res;
1857 struct gpio_descs *mode;
1858 void __iomem *ss_regs;
1863 cpsw = devm_kzalloc(dev, sizeof(struct cpsw_common), GFP_KERNEL);
1867 cpsw_slave_index = cpsw_slave_index_priv;
1871 cpsw->slaves = devm_kcalloc(dev,
1872 CPSW_SLAVE_PORTS_NUM,
1873 sizeof(struct cpsw_slave),
1878 mode = devm_gpiod_get_array_optional(dev, "mode", GPIOD_OUT_LOW);
1880 ret = PTR_ERR(mode);
1881 dev_err(dev, "gpio request failed, ret %d\n", ret);
1885 clk = devm_clk_get(dev, "fck");
1888 dev_err(dev, "fck is not found %d\n", ret);
1891 cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
1893 ss_regs = devm_platform_get_and_ioremap_resource(pdev, 0, &ss_res);
1894 if (IS_ERR(ss_regs)) {
1895 ret = PTR_ERR(ss_regs);
1898 cpsw->regs = ss_regs;
1900 irq = platform_get_irq_byname(pdev, "rx");
1903 cpsw->irqs_table[0] = irq;
1905 irq = platform_get_irq_byname(pdev, "tx");
1908 cpsw->irqs_table[1] = irq;
1910 irq = platform_get_irq_byname(pdev, "misc");
1913 cpsw->misc_irq = irq;
1915 platform_set_drvdata(pdev, cpsw);
1916 /* This may be required here for child devices. */
1917 pm_runtime_enable(dev);
1919 /* Need to enable clocks with runtime PM api to access module
1922 ret = pm_runtime_resume_and_get(dev);
1924 pm_runtime_disable(dev);
1928 ret = cpsw_probe_dt(cpsw);
1932 soc = soc_device_match(cpsw_soc_devices);
1934 cpsw->quirk_irq = true;
1936 cpsw->rx_packet_max = rx_packet_max;
1937 cpsw->descs_pool_size = descs_pool_size;
1938 eth_random_addr(cpsw->base_mac);
1940 ret = cpsw_init_common(cpsw, ss_regs, ale_ageout,
1941 (u32 __force)ss_res->start + CPSW2_BD_OFFSET,
1946 cpsw->wr_regs = cpsw->version == CPSW_VERSION_1 ?
1947 ss_regs + CPSW1_WR_OFFSET :
1948 ss_regs + CPSW2_WR_OFFSET;
1950 ch = cpsw->quirk_irq ? 0 : 7;
1951 cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, ch, cpsw_tx_handler, 0);
1952 if (IS_ERR(cpsw->txv[0].ch)) {
1953 dev_err(dev, "error initializing tx dma channel\n");
1954 ret = PTR_ERR(cpsw->txv[0].ch);
1958 cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
1959 if (IS_ERR(cpsw->rxv[0].ch)) {
1960 dev_err(dev, "error initializing rx dma channel\n");
1961 ret = PTR_ERR(cpsw->rxv[0].ch);
1964 cpsw_split_res(cpsw);
1967 ret = cpsw_create_ports(cpsw);
1969 goto clean_unregister_netdev;
1971 /* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
1972 * MISC IRQs which are always kept disabled with this driver so
1973 * we will not request them.
1975 * If anyone wants to implement support for those, make sure to
1976 * first request and append them to irqs_table array.
1979 ret = devm_request_irq(dev, cpsw->irqs_table[0], cpsw_rx_interrupt,
1980 0, dev_name(dev), cpsw);
1982 dev_err(dev, "error attaching irq (%d)\n", ret);
1983 goto clean_unregister_netdev;
1986 ret = devm_request_irq(dev, cpsw->irqs_table[1], cpsw_tx_interrupt,
1987 0, dev_name(dev), cpsw);
1989 dev_err(dev, "error attaching irq (%d)\n", ret);
1990 goto clean_unregister_netdev;
1996 ret = devm_request_irq(dev, cpsw->misc_irq, cpsw_misc_interrupt,
1997 0, dev_name(&pdev->dev), cpsw);
1999 dev_err(dev, "error attaching misc irq (%d)\n", ret);
2000 goto clean_unregister_netdev;
2003 /* Enable misc CPTS evnt_pend IRQ */
2004 cpts_set_irqpoll(cpsw->cpts, false);
2007 ret = cpsw_register_notifiers(cpsw);
2009 goto clean_unregister_netdev;
2011 ret = cpsw_register_devlink(cpsw);
2013 goto clean_unregister_notifiers;
2015 ret = cpsw_register_ports(cpsw);
2017 goto clean_unregister_notifiers;
2019 dev_notice(dev, "initialized (regs %pa, pool size %d) hw_ver:%08X %d.%d (%d)\n",
2020 &ss_res->start, descs_pool_size,
2021 cpsw->version, CPSW_MAJOR_VERSION(cpsw->version),
2022 CPSW_MINOR_VERSION(cpsw->version),
2023 CPSW_RTL_VERSION(cpsw->version));
2025 pm_runtime_put(dev);
2029 clean_unregister_notifiers:
2030 cpsw_unregister_notifiers(cpsw);
2031 clean_unregister_netdev:
2032 cpsw_unregister_ports(cpsw);
2034 cpts_release(cpsw->cpts);
2035 cpdma_ctlr_destroy(cpsw->dma);
2037 cpsw_remove_dt(cpsw);
2038 pm_runtime_put_sync(dev);
2039 pm_runtime_disable(dev);
2043 static void cpsw_remove(struct platform_device *pdev)
2045 struct cpsw_common *cpsw = platform_get_drvdata(pdev);
2048 ret = pm_runtime_resume_and_get(&pdev->dev);
2050 /* Note, if this error path is taken, we're leaking some
2053 dev_err(&pdev->dev, "Failed to resume device (%pe)\n",
2058 cpsw_unregister_notifiers(cpsw);
2059 cpsw_unregister_devlink(cpsw);
2060 cpsw_unregister_ports(cpsw);
2062 cpts_release(cpsw->cpts);
2063 cpdma_ctlr_destroy(cpsw->dma);
2064 cpsw_remove_dt(cpsw);
2065 pm_runtime_put_sync(&pdev->dev);
2066 pm_runtime_disable(&pdev->dev);
2069 static int __maybe_unused cpsw_suspend(struct device *dev)
2071 struct cpsw_common *cpsw = dev_get_drvdata(dev);
2076 for (i = 0; i < cpsw->data.slaves; i++) {
2077 struct net_device *ndev = cpsw->slaves[i].ndev;
2079 if (!(ndev && netif_running(ndev)))
2082 cpsw_ndo_stop(ndev);
2087 /* Select sleep pin state */
2088 pinctrl_pm_select_sleep_state(dev);
2093 static int __maybe_unused cpsw_resume(struct device *dev)
2095 struct cpsw_common *cpsw = dev_get_drvdata(dev);
2098 /* Select default pin state */
2099 pinctrl_pm_select_default_state(dev);
2101 /* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
2104 for (i = 0; i < cpsw->data.slaves; i++) {
2105 struct net_device *ndev = cpsw->slaves[i].ndev;
2107 if (!(ndev && netif_running(ndev)))
2110 cpsw_ndo_open(ndev);
2118 static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
2120 static struct platform_driver cpsw_driver = {
2122 .name = "cpsw-switch",
2124 .of_match_table = cpsw_of_mtable,
2126 .probe = cpsw_probe,
2127 .remove_new = cpsw_remove,
2130 module_platform_driver(cpsw_driver);
2132 MODULE_LICENSE("GPL");
2133 MODULE_DESCRIPTION("TI CPSW switchdev Ethernet driver");