Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Texas Instruments Ethernet Switch Driver
   4 *
   5 * Copyright (C) 2019 Texas Instruments
   6 */
   7
   8#include <linux/io.h>
   9#include <linux/clk.h>
  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>
  24#include <linux/of.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>
  31
  32#include <net/switchdev.h>
  33#include <net/page_pool/helpers.h>
  34#include <net/pkt_cls.h>
  35#include <net/devlink.h>
  36
  37#include "cpsw.h"
  38#include "cpsw_ale.h"
  39#include "cpsw_priv.h"
  40#include "cpsw_sl.h"
  41#include "cpsw_switchdev.h"
  42#include "cpts.h"
  43#include "davinci_cpdma.h"
  44
  45#include <net/pkt_sched.h>
  46
  47static int debug_level;
  48static int ale_ageout = CPSW_ALE_AGEOUT_DEFAULT;
  49static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
  50static int descs_pool_size = CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT;
  51
  52struct cpsw_devlink {
  53	struct cpsw_common *cpsw;
  54};
  55
  56enum 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,
  60};
  61
  62/* struct cpsw_common is not needed, kept here for compatibility
  63 * reasons witrh the old driver
  64 */
  65static int cpsw_slave_index_priv(struct cpsw_common *cpsw,
  66				 struct cpsw_priv *priv)
  67{
  68	if (priv->emac_port == HOST_PORT_NUM)
  69		return -1;
  70
  71	return priv->emac_port - 1;
  72}
  73
  74static bool cpsw_is_switch_en(struct cpsw_common *cpsw)
  75{
  76	return !cpsw->data.dual_emac;
  77}
  78
  79static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
  80{
  81	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  82	bool enable_uni = false;
  83	int i;
  84
  85	if (cpsw_is_switch_en(cpsw))
  86		return;
  87
  88	/* Enabling promiscuous mode for one interface will be
  89	 * common for both the interface as the interface shares
  90	 * the same hardware resource.
  91	 */
  92	for (i = 0; i < cpsw->data.slaves; i++)
  93		if (cpsw->slaves[i].ndev &&
  94		    (cpsw->slaves[i].ndev->flags & IFF_PROMISC))
  95			enable_uni = true;
  96
  97	if (!enable && enable_uni) {
  98		enable = enable_uni;
  99		dev_dbg(cpsw->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
 100	}
 101
 102	if (enable) {
 103		/* Enable unknown unicast, reg/unreg mcast */
 104		cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
 105				     ALE_P0_UNI_FLOOD, 1);
 106
 107		dev_dbg(cpsw->dev, "promiscuity enabled\n");
 108	} else {
 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");
 113	}
 114}
 115
 116/**
 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
 123 */
 124static int cpsw_set_mc(struct net_device *ndev, const u8 *addr,
 125		       int vid, int add)
 126{
 127	struct cpsw_priv *priv = netdev_priv(ndev);
 128	struct cpsw_common *cpsw = priv->cpsw;
 129	int mask, flags, ret, slave_no;
 130
 131	slave_no = cpsw_slave_index(cpsw, priv);
 132	if (vid < 0)
 133		vid = cpsw->slaves[slave_no].port_vlan;
 134
 135	mask =  ALE_PORT_HOST;
 136	flags = vid ? ALE_VLAN : 0;
 137
 138	if (add)
 139		ret = cpsw_ale_add_mcast(cpsw->ale, addr, mask, flags, vid, 0);
 140	else
 141		ret = cpsw_ale_del_mcast(cpsw->ale, addr, 0, flags, vid);
 142
 143	return ret;
 144}
 145
 146static int cpsw_update_vlan_mc(struct net_device *vdev, int vid, void *ctx)
 147{
 148	struct addr_sync_ctx *sync_ctx = ctx;
 149	struct netdev_hw_addr *ha;
 150	int found = 0, ret = 0;
 151
 152	if (!vdev || !(vdev->flags & IFF_UP))
 153		return 0;
 154
 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;
 159			break;
 160		}
 161	}
 162
 163	if (found)
 164		sync_ctx->consumed++;
 165
 166	if (sync_ctx->flush) {
 167		if (!found)
 168			cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 0);
 169		return 0;
 170	}
 171
 172	if (found)
 173		ret = cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 1);
 174
 175	return ret;
 176}
 177
 178static int cpsw_add_mc_addr(struct net_device *ndev, const u8 *addr, int num)
 179{
 180	struct addr_sync_ctx sync_ctx;
 181	int ret;
 182
 183	sync_ctx.consumed = 0;
 184	sync_ctx.addr = addr;
 185	sync_ctx.ndev = ndev;
 186	sync_ctx.flush = 0;
 187
 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);
 191
 192	return ret;
 193}
 194
 195static int cpsw_del_mc_addr(struct net_device *ndev, const u8 *addr, int num)
 196{
 197	struct addr_sync_ctx sync_ctx;
 198
 199	sync_ctx.consumed = 0;
 200	sync_ctx.addr = addr;
 201	sync_ctx.ndev = ndev;
 202	sync_ctx.flush = 1;
 203
 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);
 207
 208	return 0;
 209}
 210
 211static int cpsw_purge_vlan_mc(struct net_device *vdev, int vid, void *ctx)
 212{
 213	struct addr_sync_ctx *sync_ctx = ctx;
 214	struct netdev_hw_addr *ha;
 215	int found = 0;
 216
 217	if (!vdev || !(vdev->flags & IFF_UP))
 218		return 0;
 219
 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;
 224			break;
 225		}
 226	}
 227
 228	if (!found)
 229		return 0;
 230
 231	sync_ctx->consumed++;
 232	cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 0);
 233	return 0;
 234}
 235
 236static int cpsw_purge_all_mc(struct net_device *ndev, const u8 *addr, int num)
 237{
 238	struct addr_sync_ctx sync_ctx;
 239
 240	sync_ctx.addr = addr;
 241	sync_ctx.ndev = ndev;
 242	sync_ctx.consumed = 0;
 243
 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);
 247
 248	return 0;
 249}
 250
 251static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
 252{
 253	struct cpsw_priv *priv = netdev_priv(ndev);
 254	struct cpsw_common *cpsw = priv->cpsw;
 255
 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);
 260		return;
 261	}
 262
 263	/* Disable promiscuous mode */
 264	cpsw_set_promiscious(ndev, false);
 265
 266	/* Restore allmulti on vlans if necessary */
 267	cpsw_ale_set_allmulti(cpsw->ale,
 268			      ndev->flags & IFF_ALLMULTI, priv->emac_port);
 269
 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,
 272			       cpsw_del_mc_addr);
 273}
 274
 275static unsigned int cpsw_rxbuf_total_len(unsigned int len)
 276{
 277	len += CPSW_HEADROOM_NA;
 278	len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
 279
 280	return SKB_DATA_ALIGN(len);
 281}
 282
 283static void cpsw_rx_handler(void *token, int len, int status)
 284{
 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;
 294	struct sk_buff *skb;
 295	struct xdp_buff xdp;
 296	int ret = 0;
 297	dma_addr_t dma;
 298
 299	xmeta = pa + CPSW_XMETA_OFFSET;
 300	cpsw = ndev_to_cpsw(xmeta->ndev);
 301	ndev = xmeta->ndev;
 302	pkt_size = cpsw->rx_packet_max;
 303	ch = xmeta->ch;
 304
 305	if (status >= 0) {
 306		port = CPDMA_RX_SOURCE_PORT(status);
 307		if (port)
 308			ndev = cpsw->slaves[--port].ndev;
 309	}
 310
 311	priv = netdev_priv(ndev);
 312	pool = cpsw->page_pool[ch];
 313
 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.
 322			 */
 323			new_page = page;
 324			goto requeue;
 325		}
 326
 327		/* the interface is going down, pages are purged */
 328		page_pool_recycle_direct(pool, page);
 329		return;
 330	}
 331
 332	new_page = page_pool_dev_alloc_pages(pool);
 333	if (unlikely(!new_page)) {
 334		new_page = page;
 335		ndev->stats.rx_dropped++;
 336		goto requeue;
 337	}
 338
 339	if (priv->xdp_prog) {
 340		int size = len;
 341
 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;
 346		}
 347
 348		xdp_prepare_buff(&xdp, pa, headroom, size, false);
 349
 350		ret = cpsw_run_xdp(priv, ch, &xdp, page, priv->emac_port, &len);
 351		if (ret != CPSW_XDP_PASS)
 352			goto requeue;
 353
 354		headroom = xdp.data - xdp.data_hard_start;
 355
 356		/* XDP prog can modify vlan tag, so can't use encap header */
 357		status &= ~CPDMA_RX_VLAN_ENCAP;
 358	}
 359
 360	/* pass skb to netstack if no XDP prog or returned XDP_PASS */
 361	skb = build_skb(pa, cpsw_rxbuf_total_len(pkt_size));
 362	if (!skb) {
 363		ndev->stats.rx_dropped++;
 364		page_pool_recycle_direct(pool, page);
 365		goto requeue;
 366	}
 367
 368	skb->offload_fwd_mark = priv->offload_fwd_mark;
 369	skb_reserve(skb, headroom);
 370	skb_put(skb, len);
 371	skb->dev = ndev;
 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);
 377
 378	/* mark skb for recycling */
 379	skb_mark_for_recycle(skb);
 380	netif_receive_skb(skb);
 381
 382	ndev->stats.rx_bytes += len;
 383	ndev->stats.rx_packets++;
 384
 385requeue:
 386	xmeta = page_address(new_page) + CPSW_XMETA_OFFSET;
 387	xmeta->ndev = ndev;
 388	xmeta->ch = ch;
 389
 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,
 392				       pkt_size, 0);
 393	if (ret < 0) {
 394		WARN_ON(ret == -ENOMEM);
 395		page_pool_recycle_direct(pool, new_page);
 396	}
 397}
 398
 399static int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
 400				   unsigned short vid)
 401{
 402	struct cpsw_common *cpsw = priv->cpsw;
 403	int unreg_mcast_mask = 0;
 404	int mcast_mask;
 405	u32 port_mask;
 406	int ret;
 407
 408	port_mask = (1 << priv->emac_port) | ALE_PORT_HOST;
 409
 410	mcast_mask = ALE_PORT_HOST;
 411	if (priv->ndev->flags & IFF_ALLMULTI)
 412		unreg_mcast_mask = mcast_mask;
 413
 414	ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
 415				unreg_mcast_mask);
 416	if (ret != 0)
 417		return ret;
 418
 419	ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
 420				 HOST_PORT_NUM, ALE_VLAN, vid);
 421	if (ret != 0)
 422		goto clean_vid;
 423
 424	ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
 425				 mcast_mask, ALE_VLAN, vid, 0);
 426	if (ret != 0)
 427		goto clean_vlan_ucast;
 428	return 0;
 429
 430clean_vlan_ucast:
 431	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
 432			   HOST_PORT_NUM, ALE_VLAN, vid);
 433clean_vid:
 434	cpsw_ale_del_vlan(cpsw->ale, vid, 0);
 435	return ret;
 436}
 437
 438static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
 439				    __be16 proto, u16 vid)
 440{
 441	struct cpsw_priv *priv = netdev_priv(ndev);
 442	struct cpsw_common *cpsw = priv->cpsw;
 443	int ret, i;
 444
 445	if (cpsw_is_switch_en(cpsw)) {
 446		dev_dbg(cpsw->dev, ".ndo_vlan_rx_add_vid called in switch mode\n");
 447		return 0;
 448	}
 449
 450	if (vid == cpsw->data.default_vlan)
 451		return 0;
 452
 453	ret = pm_runtime_resume_and_get(cpsw->dev);
 454	if (ret < 0)
 455		return ret;
 456
 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
 460	 */
 461	for (i = 0; i < cpsw->data.slaves; i++) {
 462		if (cpsw->slaves[i].ndev &&
 463		    vid == cpsw->slaves[i].port_vlan) {
 464			ret = -EINVAL;
 465			goto err;
 466		}
 467	}
 468
 469	dev_dbg(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
 470	ret = cpsw_add_vlan_ale_entry(priv, vid);
 471err:
 472	pm_runtime_put(cpsw->dev);
 473	return ret;
 474}
 475
 476static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg)
 477{
 478	struct cpsw_priv *priv = arg;
 479
 480	if (!vdev || !vid)
 481		return 0;
 482
 483	cpsw_ndo_vlan_rx_add_vid(priv->ndev, 0, vid);
 484	return 0;
 485}
 486
 487/* restore resources after port reset */
 488static void cpsw_restore(struct cpsw_priv *priv)
 489{
 490	struct cpsw_common *cpsw = priv->cpsw;
 491
 492	/* restore vlan configurations */
 493	vlan_for_each(priv->ndev, cpsw_restore_vlans, priv);
 494
 495	/* restore MQPRIO offload */
 496	cpsw_mqprio_resume(&cpsw->slaves[priv->emac_port - 1], priv);
 497
 498	/* restore CBS offload */
 499	cpsw_cbs_resume(&cpsw->slaves[priv->emac_port - 1], priv);
 500
 501	cpsw_qos_clsflower_resume(priv);
 502}
 503
 504static void cpsw_init_stp_ale_entry(struct cpsw_common *cpsw)
 505{
 506	static const char stpa[] = {0x01, 0x80, 0xc2, 0x0, 0x0, 0x0};
 507
 508	cpsw_ale_add_mcast(cpsw->ale, stpa,
 509			   ALE_PORT_HOST, ALE_SUPER, 0,
 510			   ALE_MCAST_BLOCK_LEARN_FWD);
 511}
 512
 513static void cpsw_init_host_port_switch(struct cpsw_common *cpsw)
 514{
 515	int vlan = cpsw->data.default_vlan;
 516
 517	writel(CPSW_FIFO_NORMAL_MODE, &cpsw->host_port_regs->tx_in_ctl);
 518
 519	writel(vlan, &cpsw->host_port_regs->port_vlan);
 520
 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);
 524
 525	cpsw_init_stp_ale_entry(cpsw);
 526
 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);
 530}
 531
 532static void cpsw_init_host_port_dual_mac(struct cpsw_common *cpsw)
 533{
 534	int vlan = cpsw->data.default_vlan;
 535
 536	writel(CPSW_FIFO_DUAL_MAC_MODE, &cpsw->host_port_regs->tx_in_ctl);
 537
 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");
 540
 541	writel(vlan, &cpsw->host_port_regs->port_vlan);
 542
 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);
 546}
 547
 548static void cpsw_init_host_port(struct cpsw_priv *priv)
 549{
 550	struct cpsw_common *cpsw = priv->cpsw;
 551	u32 control_reg;
 552
 553	/* soft reset the controller and initialize ale */
 554	soft_reset("cpsw", &cpsw->regs->soft_reset);
 555	cpsw_ale_start(cpsw->ale);
 556
 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);
 563
 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);
 568
 569	/* disable priority elevation */
 570	writel_relaxed(0, &cpsw->regs->ptype);
 571
 572	/* enable statistics collection only on all ports */
 573	writel_relaxed(0x7, &cpsw->regs->stat_port_en);
 574
 575	/* Enable internal fifo flow control */
 576	writel(0x7, &cpsw->regs->flow_control);
 577
 578	if (cpsw_is_switch_en(cpsw))
 579		cpsw_init_host_port_switch(cpsw);
 580	else
 581		cpsw_init_host_port_dual_mac(cpsw);
 582
 583	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
 584			     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
 585}
 586
 587static void cpsw_port_add_dual_emac_def_ale_entries(struct cpsw_priv *priv,
 588						    struct cpsw_slave *slave)
 589{
 590	u32 port_mask = 1 << priv->emac_port | ALE_PORT_HOST;
 591	struct cpsw_common *cpsw = priv->cpsw;
 592	u32 reg;
 593
 594	reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
 595	       CPSW2_PORT_VLAN;
 596	slave_write(slave, slave->port_vlan, reg);
 597
 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,
 602			   ALE_MCAST_FWD);
 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);
 611}
 612
 613static void cpsw_port_add_switch_def_ale_entries(struct cpsw_priv *priv,
 614						 struct cpsw_slave *slave)
 615{
 616	u32 port_mask = 1 << priv->emac_port | ALE_PORT_HOST;
 617	struct cpsw_common *cpsw = priv->cpsw;
 618	u32 reg;
 619
 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
 633	 */
 634	cpsw_ale_control_set(cpsw->ale, priv->emac_port,
 635			     ALE_PORT_NO_SA_UPDATE, 1);
 636
 637	cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
 638			   port_mask, ALE_VLAN, slave->port_vlan,
 639			   ALE_MCAST_FWD_2);
 640	cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
 641			   HOST_PORT_NUM, ALE_VLAN, slave->port_vlan);
 642
 643	reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
 644	       CPSW2_PORT_VLAN;
 645	slave_write(slave, slave->port_vlan, reg);
 646}
 647
 648static void cpsw_adjust_link(struct net_device *ndev)
 649{
 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;
 654	u32 mac_control = 0;
 655
 656	slave = &cpsw->slaves[priv->emac_port - 1];
 657	phy = slave->phy;
 658
 659	if (!phy)
 660		return;
 661
 662	if (phy->link) {
 663		mac_control = CPSW_SL_CTL_GMII_EN;
 664
 665		if (phy->speed == 1000)
 666			mac_control |= CPSW_SL_CTL_GIG;
 667		if (phy->duplex)
 668			mac_control |= CPSW_SL_CTL_FULLDUPLEX;
 669
 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 */
 676
 677		if (priv->rx_pause)
 678			mac_control |= CPSW_SL_CTL_RX_FLOW_EN;
 679
 680		if (priv->tx_pause)
 681			mac_control |= CPSW_SL_CTL_TX_FLOW_EN;
 682
 683		if (mac_control != slave->mac_control)
 684			cpsw_sl_ctl_set(slave->mac_sl, mac_control);
 685
 686		/* enable forwarding */
 687		cpsw_ale_control_set(cpsw->ale, priv->emac_port,
 688				     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
 689
 690		netif_tx_wake_all_queues(ndev);
 691
 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!");
 696	} else {
 697		netif_tx_stop_all_queues(ndev);
 698
 699		mac_control = 0;
 700		/* disable forwarding */
 701		cpsw_ale_control_set(cpsw->ale, priv->emac_port,
 702				     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
 703
 704		cpsw_sl_wait_for_idle(slave->mac_sl, 100);
 705
 706		cpsw_sl_ctl_reset(slave->mac_sl);
 707	}
 708
 709	if (mac_control != slave->mac_control)
 710		phy_print_status(phy);
 711
 712	slave->mac_control = mac_control;
 713
 714	if (phy->link && cpsw_need_resplit(cpsw))
 715		cpsw_split_res(cpsw);
 716}
 717
 718static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
 719{
 720	struct cpsw_common *cpsw = priv->cpsw;
 721	struct phy_device *phy;
 722
 723	cpsw_sl_reset(slave->mac_sl, 100);
 724	cpsw_sl_ctl_reset(slave->mac_sl);
 725
 726	/* setup priority mapping */
 727	cpsw_sl_reg_write(slave->mac_sl, CPSW_SL_RX_PRI_MAP,
 728			  RX_PRIORITY_MAPPING);
 729
 730	switch (cpsw->version) {
 731	case CPSW_VERSION_1:
 732		slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
 733		/* Increase RX FIFO size to 5 for supporting fullduplex
 734		 * flow control mode
 735		 */
 736		slave_write(slave,
 737			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
 738			    CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS);
 739		break;
 740	case CPSW_VERSION_2:
 741	case CPSW_VERSION_3:
 742	case CPSW_VERSION_4:
 743		slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
 744		/* Increase RX FIFO size to 5 for supporting fullduplex
 745		 * flow control mode
 746		 */
 747		slave_write(slave,
 748			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
 749			    CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS);
 750		break;
 751	}
 752
 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);
 757
 758	slave->mac_control = 0;	/* no link yet */
 759
 760	if (cpsw_is_switch_en(cpsw))
 761		cpsw_port_add_switch_def_ale_entries(priv, slave);
 762	else
 763		cpsw_port_add_dual_emac_def_ale_entries(priv, slave);
 764
 765	if (!slave->data->phy_node)
 766		dev_err(priv->dev, "no phy found on slave %d\n",
 767			slave->slave_num);
 768	phy = of_phy_connect(priv->ndev, slave->data->phy_node,
 769			     &cpsw_adjust_link, 0, slave->data->phy_if);
 770	if (!phy) {
 771		dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n",
 772			slave->data->phy_node,
 773			slave->slave_num);
 774		return;
 775	}
 776
 777	phy->mac_managed_pm = true;
 778
 779	slave->phy = phy;
 780
 781	phy_attached_info(slave->phy);
 782
 783	phy_start(slave->phy);
 784
 785	/* Configure GMII_SEL register */
 786	phy_set_mode_ext(slave->data->ifphy, PHY_MODE_ETHERNET,
 787			 slave->data->phy_if);
 788}
 789
 790static int cpsw_ndo_stop(struct net_device *ndev)
 791{
 792	struct cpsw_priv *priv = netdev_priv(ndev);
 793	struct cpsw_common *cpsw = priv->cpsw;
 794	struct cpsw_slave *slave;
 795
 796	cpsw_info(priv, ifdown, "shutting down ndev\n");
 797	slave = &cpsw->slaves[priv->emac_port - 1];
 798	if (slave->phy)
 799		phy_stop(slave->phy);
 800
 801	netif_tx_stop_all_queues(priv->ndev);
 802
 803	if (slave->phy) {
 804		phy_disconnect(slave->phy);
 805		slave->phy = NULL;
 806	}
 807
 808	__hw_addr_ref_unsync_dev(&ndev->mc, ndev, cpsw_purge_all_mc);
 809
 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);
 818	}
 819
 820	if (cpsw_need_resplit(cpsw))
 821		cpsw_split_res(cpsw);
 822
 823	cpsw->usage_count--;
 824	pm_runtime_put_sync(cpsw->dev);
 825	return 0;
 826}
 827
 828static int cpsw_ndo_open(struct net_device *ndev)
 829{
 830	struct cpsw_priv *priv = netdev_priv(ndev);
 831	struct cpsw_common *cpsw = priv->cpsw;
 832	int ret;
 833
 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);
 837	if (ret < 0)
 838		return ret;
 839
 840	/* Notify the stack of the actual queue counts. */
 841	ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
 842	if (ret) {
 843		dev_err(priv->dev, "cannot set real number of tx queues\n");
 844		goto pm_cleanup;
 845	}
 846
 847	ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
 848	if (ret) {
 849		dev_err(priv->dev, "cannot set real number of rx queues\n");
 850		goto pm_cleanup;
 851	}
 852
 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);
 857
 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.
 862		 */
 863		ret = cpsw_create_xdp_rxqs(cpsw);
 864		if (ret < 0)
 865			goto err_cleanup;
 866
 867		ret = cpsw_fill_rx_channels(priv);
 868		if (ret < 0)
 869			goto err_cleanup;
 870
 871		if (cpsw->cpts) {
 872			if (cpts_register(cpsw->cpts))
 873				dev_err(priv->dev, "error registering cpts device\n");
 874			else
 875				writel(0x10, &cpsw->wr_regs->misc_en);
 876		}
 877
 878		napi_enable(&cpsw->napi_rx);
 879		napi_enable(&cpsw->napi_tx);
 880
 881		if (cpsw->tx_irq_disabled) {
 882			cpsw->tx_irq_disabled = false;
 883			enable_irq(cpsw->irqs_table[1]);
 884		}
 885
 886		if (cpsw->rx_irq_disabled) {
 887			cpsw->rx_irq_disabled = false;
 888			enable_irq(cpsw->irqs_table[0]);
 889		}
 890	}
 891
 892	cpsw_restore(priv);
 893
 894	/* Enable Interrupt pacing if configured */
 895	if (cpsw->coal_intvl != 0) {
 896		struct ethtool_coalesce coal;
 897
 898		coal.rx_coalesce_usecs = cpsw->coal_intvl;
 899		cpsw_set_coalesce(ndev, &coal, NULL, NULL);
 900	}
 901
 902	cpdma_ctlr_start(cpsw->dma);
 903	cpsw_intr_enable(cpsw);
 904	cpsw->usage_count++;
 905
 906	return 0;
 907
 908err_cleanup:
 909	cpsw_ndo_stop(ndev);
 910
 911pm_cleanup:
 912	pm_runtime_put_sync(cpsw->dev);
 913	return ret;
 914}
 915
 916static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
 917				       struct net_device *ndev)
 918{
 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;
 924	int ret, q_idx;
 925
 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;
 930	}
 931
 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;
 935
 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;
 939
 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,
 944				priv->emac_port);
 945	if (unlikely(ret != 0)) {
 946		cpsw_err(priv, tx_err, "desc submit failed\n");
 947		goto fail;
 948	}
 949
 950	/* If there is no more tx desc left free then we need to
 951	 * tell the kernel to stop sending us tx frames.
 952	 */
 953	if (unlikely(!cpdma_check_free_tx_desc(txch))) {
 954		netif_tx_stop_queue(txq);
 955
 956		/* Barrier, so that stop_queue visible to other cpus */
 957		smp_mb__after_atomic();
 958
 959		if (cpdma_check_free_tx_desc(txch))
 960			netif_tx_wake_queue(txq);
 961	}
 962
 963	return NETDEV_TX_OK;
 964fail:
 965	ndev->stats.tx_dropped++;
 966	netif_tx_stop_queue(txq);
 967
 968	/* Barrier, so that stop_queue visible to other cpus */
 969	smp_mb__after_atomic();
 970
 971	if (cpdma_check_free_tx_desc(txch))
 972		netif_tx_wake_queue(txq);
 973
 974	return NETDEV_TX_BUSY;
 975}
 976
 977static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
 978{
 979	struct sockaddr *addr = (struct sockaddr *)p;
 980	struct cpsw_priv *priv = netdev_priv(ndev);
 981	struct cpsw_common *cpsw = priv->cpsw;
 982	int ret, slave_no;
 983	int flags = 0;
 984	u16 vid = 0;
 985
 986	slave_no = cpsw_slave_index(cpsw, priv);
 987	if (!is_valid_ether_addr(addr->sa_data))
 988		return -EADDRNOTAVAIL;
 989
 990	ret = pm_runtime_resume_and_get(cpsw->dev);
 991	if (ret < 0)
 992		return ret;
 993
 994	vid = cpsw->slaves[slave_no].port_vlan;
 995	flags = ALE_VLAN | ALE_SECURE;
 996
 997	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
 998			   flags, vid);
 999	cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
1000			   flags, vid);
1001
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);
1005
1006	pm_runtime_put(cpsw->dev);
1007
1008	return 0;
1009}
1010
1011static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
1012				     __be16 proto, u16 vid)
1013{
1014	struct cpsw_priv *priv = netdev_priv(ndev);
1015	struct cpsw_common *cpsw = priv->cpsw;
1016	int ret;
1017	int i;
1018
1019	if (cpsw_is_switch_en(cpsw)) {
1020		dev_dbg(cpsw->dev, "ndo del vlan is called in switch mode\n");
1021		return 0;
1022	}
1023
1024	if (vid == cpsw->data.default_vlan)
1025		return 0;
1026
1027	ret = pm_runtime_resume_and_get(cpsw->dev);
1028	if (ret < 0)
1029		return ret;
1030
1031	/* reset the return code as pm_runtime_get_sync() can return
1032	 * non zero values as well.
1033	 */
1034	ret = 0;
1035	for (i = 0; i < cpsw->data.slaves; i++) {
1036		if (cpsw->slaves[i].ndev &&
1037		    vid == cpsw->slaves[i].port_vlan) {
1038			ret = -EINVAL;
1039			goto err;
1040		}
1041	}
1042
1043	dev_dbg(priv->dev, "removing vlanid %d from vlan filter\n", vid);
1044	ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
1045	if (ret)
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);
1049	if (ret)
1050		dev_err(priv->dev, "cpsw_ale_del_ucast() failed: ret %d\n",
1051			ret);
1052	ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
1053				 0, ALE_VLAN, vid);
1054	if (ret)
1055		dev_err(priv->dev, "cpsw_ale_del_mcast failed. ret %d\n",
1056			ret);
1057	cpsw_ale_flush_multicast(cpsw->ale, ALE_PORT_HOST, vid);
1058	ret = 0;
1059err:
1060	pm_runtime_put(cpsw->dev);
1061	return ret;
1062}
1063
1064static int cpsw_ndo_get_phys_port_name(struct net_device *ndev, char *name,
1065				       size_t len)
1066{
1067	struct cpsw_priv *priv = netdev_priv(ndev);
1068	int err;
1069
1070	err = snprintf(name, len, "p%d", priv->emac_port);
1071
1072	if (err >= len)
1073		return -EINVAL;
1074
1075	return 0;
1076}
1077
1078#ifdef CONFIG_NET_POLL_CONTROLLER
1079static void cpsw_ndo_poll_controller(struct net_device *ndev)
1080{
1081	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1082
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);
1087}
1088#endif
1089
1090static int cpsw_ndo_xdp_xmit(struct net_device *ndev, int n,
1091			     struct xdp_frame **frames, u32 flags)
1092{
1093	struct cpsw_priv *priv = netdev_priv(ndev);
1094	struct xdp_frame *xdpf;
1095	int i, nxmit = 0;
1096
1097	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
1098		return -EINVAL;
1099
1100	for (i = 0; i < n; i++) {
1101		xdpf = frames[i];
1102		if (xdpf->len < READ_ONCE(priv->tx_packet_min))
1103			break;
1104
1105		if (cpsw_xdp_tx_frame(priv, xdpf, NULL, priv->emac_port))
1106			break;
1107		nxmit++;
1108	}
1109
1110	return nxmit;
1111}
1112
1113static int cpsw_get_port_parent_id(struct net_device *ndev,
1114				   struct netdev_phys_item_id *ppid)
1115{
1116	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1117
1118	ppid->id_len = sizeof(cpsw->base_mac);
1119	memcpy(&ppid->id, &cpsw->base_mac, ppid->id_len);
1120
1121	return 0;
1122}
1123
1124static 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,
1136#endif
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,
1144};
1145
1146static void cpsw_get_drvinfo(struct net_device *ndev,
1147			     struct ethtool_drvinfo *info)
1148{
1149	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1150	struct platform_device *pdev;
1151
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));
1156}
1157
1158static int cpsw_set_pauseparam(struct net_device *ndev,
1159			       struct ethtool_pauseparam *pause)
1160{
1161	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1162	struct cpsw_priv *priv = netdev_priv(ndev);
1163	int slave_no;
1164
1165	slave_no = cpsw_slave_index(cpsw, priv);
1166	if (!cpsw->slaves[slave_no].phy)
1167		return -EINVAL;
1168
1169	if (!phy_validate_pause(cpsw->slaves[slave_no].phy, pause))
1170		return -EINVAL;
1171
1172	priv->rx_pause = pause->rx_pause ? true : false;
1173	priv->tx_pause = pause->tx_pause ? true : false;
1174
1175	phy_set_asym_pause(cpsw->slaves[slave_no].phy,
1176			   priv->rx_pause, priv->tx_pause);
1177
1178	return 0;
1179}
1180
1181static int cpsw_set_channels(struct net_device *ndev,
1182			     struct ethtool_channels *chs)
1183{
1184	return cpsw_set_channels_common(ndev, chs, cpsw_rx_handler);
1185}
1186
1187static 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,
1216};
1217
1218static int cpsw_probe_dt(struct cpsw_common *cpsw)
1219{
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;
1223	int ret;
1224	u32 prop;
1225
1226	if (!node)
1227		return -EINVAL;
1228
1229	tmp_node = of_get_child_by_name(node, "ethernet-ports");
1230	if (!tmp_node)
1231		return -ENOENT;
1232	data->slaves = of_get_child_count(tmp_node);
1233	if (data->slaves != CPSW_SLAVE_PORTS_NUM) {
1234		of_node_put(tmp_node);
1235		return -ENOENT;
1236	}
1237
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;
1243
1244	data->slave_data = devm_kcalloc(dev, CPSW_SLAVE_PORTS_NUM,
1245					sizeof(struct cpsw_slave_data),
1246					GFP_KERNEL);
1247	if (!data->slave_data) {
1248		of_node_put(tmp_node);
1249		return -ENOMEM;
1250	}
1251
1252	/* Populate all the child nodes here...
1253	 */
1254	ret = devm_of_platform_populate(dev);
1255	/* We do not want to force this, as in some cases may not have child */
1256	if (ret)
1257		dev_warn(dev, "Doesn't have any child node\n");
1258
1259	for_each_child_of_node(tmp_node, port_np) {
1260		struct cpsw_slave_data *slave_data;
1261		u32 port_id;
1262
1263		ret = of_property_read_u32(port_np, "reg", &port_id);
1264		if (ret < 0) {
1265			dev_err(dev, "%pOF error reading port_id %d\n",
1266				port_np, ret);
1267			goto err_node_put;
1268		}
1269
1270		if (!port_id || port_id > CPSW_SLAVE_PORTS_NUM) {
1271			dev_err(dev, "%pOF has invalid port_id %u\n",
1272				port_np, port_id);
1273			ret = -EINVAL;
1274			goto err_node_put;
1275		}
1276
1277		slave_data = &data->slave_data[port_id - 1];
1278
1279		slave_data->disabled = !of_device_is_available(port_np);
1280		if (slave_data->disabled)
1281			continue;
1282
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",
1288				port_np, ret);
1289			goto err_node_put;
1290		}
1291
1292		if (of_phy_is_fixed_link(port_np)) {
1293			ret = of_phy_register_fixed_link(port_np);
1294			if (ret) {
1295				dev_err_probe(dev, ret, "%pOF failed to register fixed-link phy\n",
1296					      port_np);
1297				goto err_node_put;
1298			}
1299			slave_data->phy_node = of_node_get(port_np);
1300		} else {
1301			slave_data->phy_node =
1302				of_parse_phandle(port_np, "phy-handle", 0);
1303		}
1304
1305		if (!slave_data->phy_node) {
1306			dev_err(dev, "%pOF no phy found\n", port_np);
1307			ret = -ENODEV;
1308			goto err_node_put;
1309		}
1310
1311		ret = of_get_phy_mode(port_np, &slave_data->phy_if);
1312		if (ret) {
1313			dev_err(dev, "%pOF read phy-mode err %d\n",
1314				port_np, ret);
1315			goto err_node_put;
1316		}
1317
1318		ret = of_get_mac_address(port_np, slave_data->mac_addr);
1319		if (ret) {
1320			ret = ti_cm_get_macid(dev, port_id - 1,
1321					      slave_data->mac_addr);
1322			if (ret)
1323				goto err_node_put;
1324		}
1325
1326		if (of_property_read_u32(port_np, "ti,dual-emac-pvid",
1327					 &prop)) {
1328			dev_err(dev, "%pOF Missing dual_emac_res_vlan in DT.\n",
1329				port_np);
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);
1333		} else {
1334			slave_data->dual_emac_res_vlan = prop;
1335		}
1336	}
1337
1338	of_node_put(tmp_node);
1339	return 0;
1340
1341err_node_put:
1342	of_node_put(port_np);
1343	of_node_put(tmp_node);
1344	return ret;
1345}
1346
1347static void cpsw_remove_dt(struct cpsw_common *cpsw)
1348{
1349	struct cpsw_platform_data *data = &cpsw->data;
1350	int i = 0;
1351
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;
1355
1356		if (port_np) {
1357			if (of_phy_is_fixed_link(port_np))
1358				of_phy_deregister_fixed_link(port_np);
1359
1360			of_node_put(port_np);
1361		}
1362	}
1363}
1364
1365static int cpsw_create_ports(struct cpsw_common *cpsw)
1366{
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;
1371	int ret = 0, i = 0;
1372
1373	for (i = 0; i < cpsw->data.slaves; i++) {
1374		struct cpsw_slave_data *slave_data = &data->slave_data[i];
1375
1376		if (slave_data->disabled)
1377			continue;
1378
1379		ndev = devm_alloc_etherdev_mqs(dev, sizeof(struct cpsw_priv),
1380					       CPSW_MAX_QUEUES,
1381					       CPSW_MAX_QUEUES);
1382		if (!ndev) {
1383			dev_err(dev, "error allocating net_device\n");
1384			return -ENOMEM;
1385		}
1386
1387		priv = netdev_priv(ndev);
1388		priv->cpsw = cpsw;
1389		priv->ndev = ndev;
1390		priv->dev  = dev;
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;
1394
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",
1398				 priv->mac_addr);
1399		} else {
1400			eth_random_addr(slave_data->mac_addr);
1401			dev_info(cpsw->dev, "Random MACID = %pM\n",
1402				 priv->mac_addr);
1403		}
1404		eth_hw_addr_set(ndev, slave_data->mac_addr);
1405		ether_addr_copy(priv->mac_addr, slave_data->mac_addr);
1406
1407		cpsw->slaves[i].ndev = ndev;
1408
1409		ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER |
1410				  NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_TC;
1411		ndev->netns_local = true;
1412
1413		ndev->xdp_features = NETDEV_XDP_ACT_BASIC |
1414				     NETDEV_XDP_ACT_REDIRECT |
1415				     NETDEV_XDP_ACT_NDO_XMIT;
1416
1417		ndev->netdev_ops = &cpsw_netdev_ops;
1418		ndev->ethtool_ops = &cpsw_ethtool_ops;
1419		SET_NETDEV_DEV(ndev, dev);
1420
1421		if (!napi_ndev) {
1422			/* CPSW Host port CPDMA interface is shared between
1423			 * ports and there is only one TX and one RX IRQs
1424			 * available for all possible TX and RX channels
1425			 * accordingly.
1426			 */
1427			netif_napi_add(ndev, &cpsw->napi_rx,
1428				       cpsw->quirk_irq ? cpsw_rx_poll : cpsw_rx_mq_poll);
1429			netif_napi_add_tx(ndev, &cpsw->napi_tx,
1430					  cpsw->quirk_irq ?
1431					  cpsw_tx_poll : cpsw_tx_mq_poll);
1432		}
1433
1434		napi_ndev = ndev;
1435	}
1436
1437	return ret;
1438}
1439
1440static void cpsw_unregister_ports(struct cpsw_common *cpsw)
1441{
1442	int i = 0;
1443
1444	for (i = 0; i < cpsw->data.slaves; i++) {
1445		if (!cpsw->slaves[i].ndev)
1446			continue;
1447
1448		unregister_netdev(cpsw->slaves[i].ndev);
1449	}
1450}
1451
1452static int cpsw_register_ports(struct cpsw_common *cpsw)
1453{
1454	int ret = 0, i = 0;
1455
1456	for (i = 0; i < cpsw->data.slaves; i++) {
1457		if (!cpsw->slaves[i].ndev)
1458			continue;
1459
1460		/* register the network device */
1461		ret = register_netdev(cpsw->slaves[i].ndev);
1462		if (ret) {
1463			dev_err(cpsw->dev,
1464				"cpsw: err registering net device%d\n", i);
1465			cpsw->slaves[i].ndev = NULL;
1466			break;
1467		}
1468	}
1469
1470	if (ret)
1471		cpsw_unregister_ports(cpsw);
1472	return ret;
1473}
1474
1475bool cpsw_port_dev_check(const struct net_device *ndev)
1476{
1477	if (ndev->netdev_ops == &cpsw_netdev_ops) {
1478		struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1479
1480		return !cpsw->data.dual_emac;
1481	}
1482
1483	return false;
1484}
1485
1486static void cpsw_port_offload_fwd_mark_update(struct cpsw_common *cpsw)
1487{
1488	int set_val = 0;
1489	int i;
1490
1491	if (!cpsw->ale_bypass &&
1492	    (cpsw->br_members == (ALE_PORT_1 | ALE_PORT_2)))
1493		set_val = 1;
1494
1495	dev_dbg(cpsw->dev, "set offload_fwd_mark %d\n", set_val);
1496
1497	for (i = 0; i < cpsw->data.slaves; i++) {
1498		struct net_device *sl_ndev = cpsw->slaves[i].ndev;
1499		struct cpsw_priv *priv = netdev_priv(sl_ndev);
1500
1501		priv->offload_fwd_mark = set_val;
1502	}
1503}
1504
1505static int cpsw_netdevice_port_link(struct net_device *ndev,
1506				    struct net_device *br_ndev,
1507				    struct netlink_ext_ack *extack)
1508{
1509	struct cpsw_priv *priv = netdev_priv(ndev);
1510	struct cpsw_common *cpsw = priv->cpsw;
1511	int err;
1512
1513	if (!cpsw->br_members) {
1514		cpsw->hw_bridge_dev = br_ndev;
1515	} else {
1516		/* This is adding the port to a second bridge, this is
1517		 * unsupported
1518		 */
1519		if (cpsw->hw_bridge_dev != br_ndev)
1520			return -EOPNOTSUPP;
1521	}
1522
1523	err = switchdev_bridge_port_offload(ndev, ndev, NULL, NULL, NULL,
1524					    false, extack);
1525	if (err)
1526		return err;
1527
1528	cpsw->br_members |= BIT(priv->emac_port);
1529
1530	cpsw_port_offload_fwd_mark_update(cpsw);
1531
1532	return NOTIFY_DONE;
1533}
1534
1535static void cpsw_netdevice_port_unlink(struct net_device *ndev)
1536{
1537	struct cpsw_priv *priv = netdev_priv(ndev);
1538	struct cpsw_common *cpsw = priv->cpsw;
1539
1540	switchdev_bridge_port_unoffload(ndev, NULL, NULL, NULL);
1541
1542	cpsw->br_members &= ~BIT(priv->emac_port);
1543
1544	cpsw_port_offload_fwd_mark_update(cpsw);
1545
1546	if (!cpsw->br_members)
1547		cpsw->hw_bridge_dev = NULL;
1548}
1549
1550/* netdev notifier */
1551static int cpsw_netdevice_event(struct notifier_block *unused,
1552				unsigned long event, void *ptr)
1553{
1554	struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
1555	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1556	struct netdev_notifier_changeupper_info *info;
1557	int ret = NOTIFY_DONE;
1558
1559	if (!cpsw_port_dev_check(ndev))
1560		return NOTIFY_DONE;
1561
1562	switch (event) {
1563	case NETDEV_CHANGEUPPER:
1564		info = ptr;
1565
1566		if (netif_is_bridge_master(info->upper_dev)) {
1567			if (info->linking)
1568				ret = cpsw_netdevice_port_link(ndev,
1569							       info->upper_dev,
1570							       extack);
1571			else
1572				cpsw_netdevice_port_unlink(ndev);
1573		}
1574		break;
1575	default:
1576		return NOTIFY_DONE;
1577	}
1578
1579	return notifier_from_errno(ret);
1580}
1581
1582static struct notifier_block cpsw_netdevice_nb __read_mostly = {
1583	.notifier_call = cpsw_netdevice_event,
1584};
1585
1586static int cpsw_register_notifiers(struct cpsw_common *cpsw)
1587{
1588	int ret = 0;
1589
1590	ret = register_netdevice_notifier(&cpsw_netdevice_nb);
1591	if (ret) {
1592		dev_err(cpsw->dev, "can't register netdevice notifier\n");
1593		return ret;
1594	}
1595
1596	ret = cpsw_switchdev_register_notifiers(cpsw);
1597	if (ret)
1598		unregister_netdevice_notifier(&cpsw_netdevice_nb);
1599
1600	return ret;
1601}
1602
1603static void cpsw_unregister_notifiers(struct cpsw_common *cpsw)
1604{
1605	cpsw_switchdev_unregister_notifiers(cpsw);
1606	unregister_netdevice_notifier(&cpsw_netdevice_nb);
1607}
1608
1609static const struct devlink_ops cpsw_devlink_ops = {
1610};
1611
1612static int cpsw_dl_switch_mode_get(struct devlink *dl, u32 id,
1613				   struct devlink_param_gset_ctx *ctx)
1614{
1615	struct cpsw_devlink *dl_priv = devlink_priv(dl);
1616	struct cpsw_common *cpsw = dl_priv->cpsw;
1617
1618	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1619
1620	if (id != CPSW_DL_PARAM_SWITCH_MODE)
1621		return  -EOPNOTSUPP;
1622
1623	ctx->val.vbool = !cpsw->data.dual_emac;
1624
1625	return 0;
1626}
1627
1628static int cpsw_dl_switch_mode_set(struct devlink *dl, u32 id,
1629				   struct devlink_param_gset_ctx *ctx,
1630				   struct netlink_ext_ack *extack)
1631{
1632	struct cpsw_devlink *dl_priv = devlink_priv(dl);
1633	struct cpsw_common *cpsw = dl_priv->cpsw;
1634	int vlan = cpsw->data.default_vlan;
1635	bool switch_en = ctx->val.vbool;
1636	bool if_running = false;
1637	int i;
1638
1639	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1640
1641	if (id != CPSW_DL_PARAM_SWITCH_MODE)
1642		return  -EOPNOTSUPP;
1643
1644	if (switch_en == !cpsw->data.dual_emac)
1645		return 0;
1646
1647	if (!switch_en && cpsw->br_members) {
1648		dev_err(cpsw->dev, "Remove ports from BR before disabling switch mode\n");
1649		return -EINVAL;
1650	}
1651
1652	rtnl_lock();
1653
1654	for (i = 0; i < cpsw->data.slaves; i++) {
1655		struct cpsw_slave *slave = &cpsw->slaves[i];
1656		struct net_device *sl_ndev = slave->ndev;
1657
1658		if (!sl_ndev || !netif_running(sl_ndev))
1659			continue;
1660
1661		if_running = true;
1662	}
1663
1664	if (!if_running) {
1665		/* all ndevs are down */
1666		cpsw->data.dual_emac = !switch_en;
1667		for (i = 0; i < cpsw->data.slaves; i++) {
1668			struct cpsw_slave *slave = &cpsw->slaves[i];
1669			struct net_device *sl_ndev = slave->ndev;
1670
1671			if (!sl_ndev)
1672				continue;
1673
1674			if (switch_en)
1675				vlan = cpsw->data.default_vlan;
1676			else
1677				vlan = slave->data->dual_emac_res_vlan;
1678			slave->port_vlan = vlan;
1679		}
1680		goto exit;
1681	}
1682
1683	if (switch_en) {
1684		dev_info(cpsw->dev, "Enable switch mode\n");
1685
1686		/* enable bypass - no forwarding; all traffic goes to Host */
1687		cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1);
1688
1689		/* clean up ALE table */
1690		cpsw_ale_control_set(cpsw->ale, 0, ALE_CLEAR, 1);
1691		cpsw_ale_control_get(cpsw->ale, 0, ALE_AGEOUT);
1692
1693		cpsw_init_host_port_switch(cpsw);
1694
1695		for (i = 0; i < cpsw->data.slaves; i++) {
1696			struct cpsw_slave *slave = &cpsw->slaves[i];
1697			struct net_device *sl_ndev = slave->ndev;
1698			struct cpsw_priv *priv;
1699
1700			if (!sl_ndev)
1701				continue;
1702
1703			priv = netdev_priv(sl_ndev);
1704			slave->port_vlan = vlan;
1705			WRITE_ONCE(priv->tx_packet_min, CPSW_MIN_PACKET_SIZE_VLAN);
1706			if (netif_running(sl_ndev))
1707				cpsw_port_add_switch_def_ale_entries(priv,
1708								     slave);
1709		}
1710
1711		cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 0);
1712		cpsw->data.dual_emac = false;
1713	} else {
1714		dev_info(cpsw->dev, "Disable switch mode\n");
1715
1716		/* enable bypass - no forwarding; all traffic goes to Host */
1717		cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1);
1718
1719		cpsw_ale_control_set(cpsw->ale, 0, ALE_CLEAR, 1);
1720		cpsw_ale_control_get(cpsw->ale, 0, ALE_AGEOUT);
1721
1722		cpsw_init_host_port_dual_mac(cpsw);
1723
1724		for (i = 0; i < cpsw->data.slaves; i++) {
1725			struct cpsw_slave *slave = &cpsw->slaves[i];
1726			struct net_device *sl_ndev = slave->ndev;
1727			struct cpsw_priv *priv;
1728
1729			if (!sl_ndev)
1730				continue;
1731
1732			priv = netdev_priv(slave->ndev);
1733			slave->port_vlan = slave->data->dual_emac_res_vlan;
1734			WRITE_ONCE(priv->tx_packet_min, CPSW_MIN_PACKET_SIZE);
1735			cpsw_port_add_dual_emac_def_ale_entries(priv, slave);
1736		}
1737
1738		cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 0);
1739		cpsw->data.dual_emac = true;
1740	}
1741exit:
1742	rtnl_unlock();
1743
1744	return 0;
1745}
1746
1747static int cpsw_dl_ale_ctrl_get(struct devlink *dl, u32 id,
1748				struct devlink_param_gset_ctx *ctx)
1749{
1750	struct cpsw_devlink *dl_priv = devlink_priv(dl);
1751	struct cpsw_common *cpsw = dl_priv->cpsw;
1752
1753	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1754
1755	switch (id) {
1756	case CPSW_DL_PARAM_ALE_BYPASS:
1757		ctx->val.vbool = cpsw_ale_control_get(cpsw->ale, 0, ALE_BYPASS);
1758		break;
1759	default:
1760		return -EOPNOTSUPP;
1761	}
1762
1763	return 0;
1764}
1765
1766static int cpsw_dl_ale_ctrl_set(struct devlink *dl, u32 id,
1767				struct devlink_param_gset_ctx *ctx,
1768				struct netlink_ext_ack *extack)
1769{
1770	struct cpsw_devlink *dl_priv = devlink_priv(dl);
1771	struct cpsw_common *cpsw = dl_priv->cpsw;
1772	int ret = -EOPNOTSUPP;
1773
1774	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1775
1776	switch (id) {
1777	case CPSW_DL_PARAM_ALE_BYPASS:
1778		ret = cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS,
1779					   ctx->val.vbool);
1780		if (!ret) {
1781			cpsw->ale_bypass = ctx->val.vbool;
1782			cpsw_port_offload_fwd_mark_update(cpsw);
1783		}
1784		break;
1785	default:
1786		return -EOPNOTSUPP;
1787	}
1788
1789	return 0;
1790}
1791
1792static const struct devlink_param cpsw_devlink_params[] = {
1793	DEVLINK_PARAM_DRIVER(CPSW_DL_PARAM_SWITCH_MODE,
1794			     "switch_mode", DEVLINK_PARAM_TYPE_BOOL,
1795			     BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1796			     cpsw_dl_switch_mode_get, cpsw_dl_switch_mode_set,
1797			     NULL),
1798	DEVLINK_PARAM_DRIVER(CPSW_DL_PARAM_ALE_BYPASS,
1799			     "ale_bypass", DEVLINK_PARAM_TYPE_BOOL,
1800			     BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1801			     cpsw_dl_ale_ctrl_get, cpsw_dl_ale_ctrl_set, NULL),
1802};
1803
1804static int cpsw_register_devlink(struct cpsw_common *cpsw)
1805{
1806	struct device *dev = cpsw->dev;
1807	struct cpsw_devlink *dl_priv;
1808	int ret = 0;
1809
1810	cpsw->devlink = devlink_alloc(&cpsw_devlink_ops, sizeof(*dl_priv), dev);
1811	if (!cpsw->devlink)
1812		return -ENOMEM;
1813
1814	dl_priv = devlink_priv(cpsw->devlink);
1815	dl_priv->cpsw = cpsw;
1816
1817	ret = devlink_params_register(cpsw->devlink, cpsw_devlink_params,
1818				      ARRAY_SIZE(cpsw_devlink_params));
1819	if (ret) {
1820		dev_err(dev, "DL params reg fail ret:%d\n", ret);
1821		goto dl_unreg;
1822	}
1823
1824	devlink_register(cpsw->devlink);
1825	return ret;
1826
1827dl_unreg:
1828	devlink_free(cpsw->devlink);
1829	return ret;
1830}
1831
1832static void cpsw_unregister_devlink(struct cpsw_common *cpsw)
1833{
1834	devlink_unregister(cpsw->devlink);
1835	devlink_params_unregister(cpsw->devlink, cpsw_devlink_params,
1836				  ARRAY_SIZE(cpsw_devlink_params));
1837	devlink_free(cpsw->devlink);
1838}
1839
1840static const struct of_device_id cpsw_of_mtable[] = {
1841	{ .compatible = "ti,cpsw-switch"},
1842	{ .compatible = "ti,am335x-cpsw-switch"},
1843	{ .compatible = "ti,am4372-cpsw-switch"},
1844	{ .compatible = "ti,dra7-cpsw-switch"},
1845	{ /* sentinel */ },
1846};
1847MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
1848
1849static const struct soc_device_attribute cpsw_soc_devices[] = {
1850	{ .family = "AM33xx", .revision = "ES1.0"},
1851	{ /* sentinel */ }
1852};
1853
1854static int cpsw_probe(struct platform_device *pdev)
1855{
1856	const struct soc_device_attribute *soc;
1857	struct device *dev = &pdev->dev;
1858	struct cpsw_common *cpsw;
1859	struct resource *ss_res;
1860	struct gpio_descs *mode;
1861	void __iomem *ss_regs;
1862	int ret = 0, ch;
1863	struct clk *clk;
1864	int irq;
1865
1866	cpsw = devm_kzalloc(dev, sizeof(struct cpsw_common), GFP_KERNEL);
1867	if (!cpsw)
1868		return -ENOMEM;
1869
1870	cpsw_slave_index = cpsw_slave_index_priv;
1871
1872	cpsw->dev = dev;
1873
1874	cpsw->slaves = devm_kcalloc(dev,
1875				    CPSW_SLAVE_PORTS_NUM,
1876				    sizeof(struct cpsw_slave),
1877				    GFP_KERNEL);
1878	if (!cpsw->slaves)
1879		return -ENOMEM;
1880
1881	mode = devm_gpiod_get_array_optional(dev, "mode", GPIOD_OUT_LOW);
1882	if (IS_ERR(mode)) {
1883		ret = PTR_ERR(mode);
1884		dev_err(dev, "gpio request failed, ret %d\n", ret);
1885		return ret;
1886	}
1887
1888	clk = devm_clk_get(dev, "fck");
1889	if (IS_ERR(clk)) {
1890		ret = PTR_ERR(clk);
1891		dev_err(dev, "fck is not found %d\n", ret);
1892		return ret;
1893	}
1894	cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
1895
1896	ss_regs = devm_platform_get_and_ioremap_resource(pdev, 0, &ss_res);
1897	if (IS_ERR(ss_regs)) {
1898		ret = PTR_ERR(ss_regs);
1899		return ret;
1900	}
1901	cpsw->regs = ss_regs;
1902
1903	irq = platform_get_irq_byname(pdev, "rx");
1904	if (irq < 0)
1905		return irq;
1906	cpsw->irqs_table[0] = irq;
1907
1908	irq = platform_get_irq_byname(pdev, "tx");
1909	if (irq < 0)
1910		return irq;
1911	cpsw->irqs_table[1] = irq;
1912
1913	irq = platform_get_irq_byname(pdev, "misc");
1914	if (irq <= 0)
1915		return irq;
1916	cpsw->misc_irq = irq;
1917
1918	platform_set_drvdata(pdev, cpsw);
1919	/* This may be required here for child devices. */
1920	pm_runtime_enable(dev);
1921
1922	/* Need to enable clocks with runtime PM api to access module
1923	 * registers
1924	 */
1925	ret = pm_runtime_resume_and_get(dev);
1926	if (ret < 0) {
1927		pm_runtime_disable(dev);
1928		return ret;
1929	}
1930
1931	ret = cpsw_probe_dt(cpsw);
1932	if (ret)
1933		goto clean_dt_ret;
1934
1935	soc = soc_device_match(cpsw_soc_devices);
1936	if (soc)
1937		cpsw->quirk_irq = true;
1938
1939	cpsw->rx_packet_max = rx_packet_max;
1940	cpsw->descs_pool_size = descs_pool_size;
1941	eth_random_addr(cpsw->base_mac);
1942
1943	ret = cpsw_init_common(cpsw, ss_regs, ale_ageout,
1944			       (u32 __force)ss_res->start + CPSW2_BD_OFFSET,
1945			       descs_pool_size);
1946	if (ret)
1947		goto clean_dt_ret;
1948
1949	cpsw->wr_regs = cpsw->version == CPSW_VERSION_1 ?
1950			ss_regs + CPSW1_WR_OFFSET :
1951			ss_regs + CPSW2_WR_OFFSET;
1952
1953	ch = cpsw->quirk_irq ? 0 : 7;
1954	cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, ch, cpsw_tx_handler, 0);
1955	if (IS_ERR(cpsw->txv[0].ch)) {
1956		dev_err(dev, "error initializing tx dma channel\n");
1957		ret = PTR_ERR(cpsw->txv[0].ch);
1958		goto clean_cpts;
1959	}
1960
1961	cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
1962	if (IS_ERR(cpsw->rxv[0].ch)) {
1963		dev_err(dev, "error initializing rx dma channel\n");
1964		ret = PTR_ERR(cpsw->rxv[0].ch);
1965		goto clean_cpts;
1966	}
1967	cpsw_split_res(cpsw);
1968
1969	/* setup netdevs */
1970	ret = cpsw_create_ports(cpsw);
1971	if (ret)
1972		goto clean_unregister_netdev;
1973
1974	/* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
1975	 * MISC IRQs which are always kept disabled with this driver so
1976	 * we will not request them.
1977	 *
1978	 * If anyone wants to implement support for those, make sure to
1979	 * first request and append them to irqs_table array.
1980	 */
1981
1982	ret = devm_request_irq(dev, cpsw->irqs_table[0], cpsw_rx_interrupt,
1983			       0, dev_name(dev), cpsw);
1984	if (ret < 0) {
1985		dev_err(dev, "error attaching irq (%d)\n", ret);
1986		goto clean_unregister_netdev;
1987	}
1988
1989	ret = devm_request_irq(dev, cpsw->irqs_table[1], cpsw_tx_interrupt,
1990			       0, dev_name(dev), cpsw);
1991	if (ret < 0) {
1992		dev_err(dev, "error attaching irq (%d)\n", ret);
1993		goto clean_unregister_netdev;
1994	}
1995
1996	if (!cpsw->cpts)
1997		goto skip_cpts;
1998
1999	ret = devm_request_irq(dev, cpsw->misc_irq, cpsw_misc_interrupt,
2000			       0, dev_name(&pdev->dev), cpsw);
2001	if (ret < 0) {
2002		dev_err(dev, "error attaching misc irq (%d)\n", ret);
2003		goto clean_unregister_netdev;
2004	}
2005
2006	/* Enable misc CPTS evnt_pend IRQ */
2007	cpts_set_irqpoll(cpsw->cpts, false);
2008
2009skip_cpts:
2010	ret = cpsw_register_notifiers(cpsw);
2011	if (ret)
2012		goto clean_unregister_netdev;
2013
2014	ret = cpsw_register_devlink(cpsw);
2015	if (ret)
2016		goto clean_unregister_notifiers;
2017
2018	ret = cpsw_register_ports(cpsw);
2019	if (ret)
2020		goto clean_unregister_notifiers;
2021
2022	dev_notice(dev, "initialized (regs %pa, pool size %d) hw_ver:%08X %d.%d (%d)\n",
2023		   &ss_res->start, descs_pool_size,
2024		   cpsw->version, CPSW_MAJOR_VERSION(cpsw->version),
2025		   CPSW_MINOR_VERSION(cpsw->version),
2026		   CPSW_RTL_VERSION(cpsw->version));
2027
2028	pm_runtime_put(dev);
2029
2030	return 0;
2031
2032clean_unregister_notifiers:
2033	cpsw_unregister_notifiers(cpsw);
2034clean_unregister_netdev:
2035	cpsw_unregister_ports(cpsw);
2036clean_cpts:
2037	cpts_release(cpsw->cpts);
2038	cpdma_ctlr_destroy(cpsw->dma);
2039clean_dt_ret:
2040	cpsw_remove_dt(cpsw);
2041	pm_runtime_put_sync(dev);
2042	pm_runtime_disable(dev);
2043	return ret;
2044}
2045
2046static void cpsw_remove(struct platform_device *pdev)
2047{
2048	struct cpsw_common *cpsw = platform_get_drvdata(pdev);
2049	int ret;
2050
2051	ret = pm_runtime_resume_and_get(&pdev->dev);
2052	if (ret < 0) {
2053		/* Note, if this error path is taken, we're leaking some
2054		 * resources.
2055		 */
2056		dev_err(&pdev->dev, "Failed to resume device (%pe)\n",
2057			ERR_PTR(ret));
2058		return;
2059	}
2060
2061	cpsw_unregister_notifiers(cpsw);
2062	cpsw_unregister_devlink(cpsw);
2063	cpsw_unregister_ports(cpsw);
2064
2065	cpts_release(cpsw->cpts);
2066	cpdma_ctlr_destroy(cpsw->dma);
2067	cpsw_remove_dt(cpsw);
2068	pm_runtime_put_sync(&pdev->dev);
2069	pm_runtime_disable(&pdev->dev);
 
2070}
2071
2072static int __maybe_unused cpsw_suspend(struct device *dev)
2073{
2074	struct cpsw_common *cpsw = dev_get_drvdata(dev);
2075	int i;
2076
2077	rtnl_lock();
2078
2079	for (i = 0; i < cpsw->data.slaves; i++) {
2080		struct net_device *ndev = cpsw->slaves[i].ndev;
2081
2082		if (!(ndev && netif_running(ndev)))
2083			continue;
2084
2085		cpsw_ndo_stop(ndev);
2086	}
2087
2088	rtnl_unlock();
2089
2090	/* Select sleep pin state */
2091	pinctrl_pm_select_sleep_state(dev);
2092
2093	return 0;
2094}
2095
2096static int __maybe_unused cpsw_resume(struct device *dev)
2097{
2098	struct cpsw_common *cpsw = dev_get_drvdata(dev);
2099	int i;
2100
2101	/* Select default pin state */
2102	pinctrl_pm_select_default_state(dev);
2103
2104	/* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
2105	rtnl_lock();
2106
2107	for (i = 0; i < cpsw->data.slaves; i++) {
2108		struct net_device *ndev = cpsw->slaves[i].ndev;
2109
2110		if (!(ndev && netif_running(ndev)))
2111			continue;
2112
2113		cpsw_ndo_open(ndev);
2114	}
2115
2116	rtnl_unlock();
2117
2118	return 0;
2119}
2120
2121static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
2122
2123static struct platform_driver cpsw_driver = {
2124	.driver = {
2125		.name	 = "cpsw-switch",
2126		.pm	 = &cpsw_pm_ops,
2127		.of_match_table = cpsw_of_mtable,
2128	},
2129	.probe = cpsw_probe,
2130	.remove = cpsw_remove,
2131};
2132
2133module_platform_driver(cpsw_driver);
2134
2135MODULE_LICENSE("GPL");
2136MODULE_DESCRIPTION("TI CPSW switchdev Ethernet driver");
v6.2
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Texas Instruments Ethernet Switch Driver
   4 *
   5 * Copyright (C) 2019 Texas Instruments
   6 */
   7
   8#include <linux/io.h>
   9#include <linux/clk.h>
 
  10#include <linux/timer.h>
  11#include <linux/module.h>
  12#include <linux/irqreturn.h>
  13#include <linux/interrupt.h>
  14#include <linux/if_ether.h>
  15#include <linux/etherdevice.h>
  16#include <linux/net_tstamp.h>
  17#include <linux/phy.h>
  18#include <linux/phy/phy.h>
  19#include <linux/delay.h>
  20#include <linux/pinctrl/consumer.h>
  21#include <linux/pm_runtime.h>
  22#include <linux/gpio/consumer.h>
  23#include <linux/of.h>
  24#include <linux/of_mdio.h>
  25#include <linux/of_net.h>
  26#include <linux/of_device.h>
  27#include <linux/if_vlan.h>
  28#include <linux/kmemleak.h>
  29#include <linux/sys_soc.h>
  30
  31#include <net/switchdev.h>
  32#include <net/page_pool.h>
  33#include <net/pkt_cls.h>
  34#include <net/devlink.h>
  35
  36#include "cpsw.h"
  37#include "cpsw_ale.h"
  38#include "cpsw_priv.h"
  39#include "cpsw_sl.h"
  40#include "cpsw_switchdev.h"
  41#include "cpts.h"
  42#include "davinci_cpdma.h"
  43
  44#include <net/pkt_sched.h>
  45
  46static int debug_level;
  47static int ale_ageout = CPSW_ALE_AGEOUT_DEFAULT;
  48static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
  49static int descs_pool_size = CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT;
  50
  51struct cpsw_devlink {
  52	struct cpsw_common *cpsw;
  53};
  54
  55enum cpsw_devlink_param_id {
  56	CPSW_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
  57	CPSW_DL_PARAM_SWITCH_MODE,
  58	CPSW_DL_PARAM_ALE_BYPASS,
  59};
  60
  61/* struct cpsw_common is not needed, kept here for compatibility
  62 * reasons witrh the old driver
  63 */
  64static int cpsw_slave_index_priv(struct cpsw_common *cpsw,
  65				 struct cpsw_priv *priv)
  66{
  67	if (priv->emac_port == HOST_PORT_NUM)
  68		return -1;
  69
  70	return priv->emac_port - 1;
  71}
  72
  73static bool cpsw_is_switch_en(struct cpsw_common *cpsw)
  74{
  75	return !cpsw->data.dual_emac;
  76}
  77
  78static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
  79{
  80	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
  81	bool enable_uni = false;
  82	int i;
  83
  84	if (cpsw_is_switch_en(cpsw))
  85		return;
  86
  87	/* Enabling promiscuous mode for one interface will be
  88	 * common for both the interface as the interface shares
  89	 * the same hardware resource.
  90	 */
  91	for (i = 0; i < cpsw->data.slaves; i++)
  92		if (cpsw->slaves[i].ndev &&
  93		    (cpsw->slaves[i].ndev->flags & IFF_PROMISC))
  94			enable_uni = true;
  95
  96	if (!enable && enable_uni) {
  97		enable = enable_uni;
  98		dev_dbg(cpsw->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
  99	}
 100
 101	if (enable) {
 102		/* Enable unknown unicast, reg/unreg mcast */
 103		cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
 104				     ALE_P0_UNI_FLOOD, 1);
 105
 106		dev_dbg(cpsw->dev, "promiscuity enabled\n");
 107	} else {
 108		/* Disable unknown unicast */
 109		cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
 110				     ALE_P0_UNI_FLOOD, 0);
 111		dev_dbg(cpsw->dev, "promiscuity disabled\n");
 112	}
 113}
 114
 115/**
 116 * cpsw_set_mc - adds multicast entry to the table if it's not added or deletes
 117 * if it's not deleted
 118 * @ndev: device to sync
 119 * @addr: address to be added or deleted
 120 * @vid: vlan id, if vid < 0 set/unset address for real device
 121 * @add: add address if the flag is set or remove otherwise
 122 */
 123static int cpsw_set_mc(struct net_device *ndev, const u8 *addr,
 124		       int vid, int add)
 125{
 126	struct cpsw_priv *priv = netdev_priv(ndev);
 127	struct cpsw_common *cpsw = priv->cpsw;
 128	int mask, flags, ret, slave_no;
 129
 130	slave_no = cpsw_slave_index(cpsw, priv);
 131	if (vid < 0)
 132		vid = cpsw->slaves[slave_no].port_vlan;
 133
 134	mask =  ALE_PORT_HOST;
 135	flags = vid ? ALE_VLAN : 0;
 136
 137	if (add)
 138		ret = cpsw_ale_add_mcast(cpsw->ale, addr, mask, flags, vid, 0);
 139	else
 140		ret = cpsw_ale_del_mcast(cpsw->ale, addr, 0, flags, vid);
 141
 142	return ret;
 143}
 144
 145static int cpsw_update_vlan_mc(struct net_device *vdev, int vid, void *ctx)
 146{
 147	struct addr_sync_ctx *sync_ctx = ctx;
 148	struct netdev_hw_addr *ha;
 149	int found = 0, ret = 0;
 150
 151	if (!vdev || !(vdev->flags & IFF_UP))
 152		return 0;
 153
 154	/* vlan address is relevant if its sync_cnt != 0 */
 155	netdev_for_each_mc_addr(ha, vdev) {
 156		if (ether_addr_equal(ha->addr, sync_ctx->addr)) {
 157			found = ha->sync_cnt;
 158			break;
 159		}
 160	}
 161
 162	if (found)
 163		sync_ctx->consumed++;
 164
 165	if (sync_ctx->flush) {
 166		if (!found)
 167			cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 0);
 168		return 0;
 169	}
 170
 171	if (found)
 172		ret = cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 1);
 173
 174	return ret;
 175}
 176
 177static int cpsw_add_mc_addr(struct net_device *ndev, const u8 *addr, int num)
 178{
 179	struct addr_sync_ctx sync_ctx;
 180	int ret;
 181
 182	sync_ctx.consumed = 0;
 183	sync_ctx.addr = addr;
 184	sync_ctx.ndev = ndev;
 185	sync_ctx.flush = 0;
 186
 187	ret = vlan_for_each(ndev, cpsw_update_vlan_mc, &sync_ctx);
 188	if (sync_ctx.consumed < num && !ret)
 189		ret = cpsw_set_mc(ndev, addr, -1, 1);
 190
 191	return ret;
 192}
 193
 194static int cpsw_del_mc_addr(struct net_device *ndev, const u8 *addr, int num)
 195{
 196	struct addr_sync_ctx sync_ctx;
 197
 198	sync_ctx.consumed = 0;
 199	sync_ctx.addr = addr;
 200	sync_ctx.ndev = ndev;
 201	sync_ctx.flush = 1;
 202
 203	vlan_for_each(ndev, cpsw_update_vlan_mc, &sync_ctx);
 204	if (sync_ctx.consumed == num)
 205		cpsw_set_mc(ndev, addr, -1, 0);
 206
 207	return 0;
 208}
 209
 210static int cpsw_purge_vlan_mc(struct net_device *vdev, int vid, void *ctx)
 211{
 212	struct addr_sync_ctx *sync_ctx = ctx;
 213	struct netdev_hw_addr *ha;
 214	int found = 0;
 215
 216	if (!vdev || !(vdev->flags & IFF_UP))
 217		return 0;
 218
 219	/* vlan address is relevant if its sync_cnt != 0 */
 220	netdev_for_each_mc_addr(ha, vdev) {
 221		if (ether_addr_equal(ha->addr, sync_ctx->addr)) {
 222			found = ha->sync_cnt;
 223			break;
 224		}
 225	}
 226
 227	if (!found)
 228		return 0;
 229
 230	sync_ctx->consumed++;
 231	cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 0);
 232	return 0;
 233}
 234
 235static int cpsw_purge_all_mc(struct net_device *ndev, const u8 *addr, int num)
 236{
 237	struct addr_sync_ctx sync_ctx;
 238
 239	sync_ctx.addr = addr;
 240	sync_ctx.ndev = ndev;
 241	sync_ctx.consumed = 0;
 242
 243	vlan_for_each(ndev, cpsw_purge_vlan_mc, &sync_ctx);
 244	if (sync_ctx.consumed < num)
 245		cpsw_set_mc(ndev, addr, -1, 0);
 246
 247	return 0;
 248}
 249
 250static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
 251{
 252	struct cpsw_priv *priv = netdev_priv(ndev);
 253	struct cpsw_common *cpsw = priv->cpsw;
 254
 255	if (ndev->flags & IFF_PROMISC) {
 256		/* Enable promiscuous mode */
 257		cpsw_set_promiscious(ndev, true);
 258		cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI, priv->emac_port);
 259		return;
 260	}
 261
 262	/* Disable promiscuous mode */
 263	cpsw_set_promiscious(ndev, false);
 264
 265	/* Restore allmulti on vlans if necessary */
 266	cpsw_ale_set_allmulti(cpsw->ale,
 267			      ndev->flags & IFF_ALLMULTI, priv->emac_port);
 268
 269	/* add/remove mcast address either for real netdev or for vlan */
 270	__hw_addr_ref_sync_dev(&ndev->mc, ndev, cpsw_add_mc_addr,
 271			       cpsw_del_mc_addr);
 272}
 273
 274static unsigned int cpsw_rxbuf_total_len(unsigned int len)
 275{
 276	len += CPSW_HEADROOM_NA;
 277	len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
 278
 279	return SKB_DATA_ALIGN(len);
 280}
 281
 282static void cpsw_rx_handler(void *token, int len, int status)
 283{
 284	struct page *new_page, *page = token;
 285	void *pa = page_address(page);
 286	int headroom = CPSW_HEADROOM_NA;
 287	struct cpsw_meta_xdp *xmeta;
 288	struct cpsw_common *cpsw;
 289	struct net_device *ndev;
 290	int port, ch, pkt_size;
 291	struct cpsw_priv *priv;
 292	struct page_pool *pool;
 293	struct sk_buff *skb;
 294	struct xdp_buff xdp;
 295	int ret = 0;
 296	dma_addr_t dma;
 297
 298	xmeta = pa + CPSW_XMETA_OFFSET;
 299	cpsw = ndev_to_cpsw(xmeta->ndev);
 300	ndev = xmeta->ndev;
 301	pkt_size = cpsw->rx_packet_max;
 302	ch = xmeta->ch;
 303
 304	if (status >= 0) {
 305		port = CPDMA_RX_SOURCE_PORT(status);
 306		if (port)
 307			ndev = cpsw->slaves[--port].ndev;
 308	}
 309
 310	priv = netdev_priv(ndev);
 311	pool = cpsw->page_pool[ch];
 312
 313	if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
 314		/* In dual emac mode check for all interfaces */
 315		if (cpsw->usage_count && status >= 0) {
 316			/* The packet received is for the interface which
 317			 * is already down and the other interface is up
 318			 * and running, instead of freeing which results
 319			 * in reducing of the number of rx descriptor in
 320			 * DMA engine, requeue page back to cpdma.
 321			 */
 322			new_page = page;
 323			goto requeue;
 324		}
 325
 326		/* the interface is going down, pages are purged */
 327		page_pool_recycle_direct(pool, page);
 328		return;
 329	}
 330
 331	new_page = page_pool_dev_alloc_pages(pool);
 332	if (unlikely(!new_page)) {
 333		new_page = page;
 334		ndev->stats.rx_dropped++;
 335		goto requeue;
 336	}
 337
 338	if (priv->xdp_prog) {
 339		int size = len;
 340
 341		xdp_init_buff(&xdp, PAGE_SIZE, &priv->xdp_rxq[ch]);
 342		if (status & CPDMA_RX_VLAN_ENCAP) {
 343			headroom += CPSW_RX_VLAN_ENCAP_HDR_SIZE;
 344			size -= CPSW_RX_VLAN_ENCAP_HDR_SIZE;
 345		}
 346
 347		xdp_prepare_buff(&xdp, pa, headroom, size, false);
 348
 349		ret = cpsw_run_xdp(priv, ch, &xdp, page, priv->emac_port, &len);
 350		if (ret != CPSW_XDP_PASS)
 351			goto requeue;
 352
 353		headroom = xdp.data - xdp.data_hard_start;
 354
 355		/* XDP prog can modify vlan tag, so can't use encap header */
 356		status &= ~CPDMA_RX_VLAN_ENCAP;
 357	}
 358
 359	/* pass skb to netstack if no XDP prog or returned XDP_PASS */
 360	skb = build_skb(pa, cpsw_rxbuf_total_len(pkt_size));
 361	if (!skb) {
 362		ndev->stats.rx_dropped++;
 363		page_pool_recycle_direct(pool, page);
 364		goto requeue;
 365	}
 366
 367	skb->offload_fwd_mark = priv->offload_fwd_mark;
 368	skb_reserve(skb, headroom);
 369	skb_put(skb, len);
 370	skb->dev = ndev;
 371	if (status & CPDMA_RX_VLAN_ENCAP)
 372		cpsw_rx_vlan_encap(skb);
 373	if (priv->rx_ts_enabled)
 374		cpts_rx_timestamp(cpsw->cpts, skb);
 375	skb->protocol = eth_type_trans(skb, ndev);
 376
 377	/* mark skb for recycling */
 378	skb_mark_for_recycle(skb);
 379	netif_receive_skb(skb);
 380
 381	ndev->stats.rx_bytes += len;
 382	ndev->stats.rx_packets++;
 383
 384requeue:
 385	xmeta = page_address(new_page) + CPSW_XMETA_OFFSET;
 386	xmeta->ndev = ndev;
 387	xmeta->ch = ch;
 388
 389	dma = page_pool_get_dma_addr(new_page) + CPSW_HEADROOM_NA;
 390	ret = cpdma_chan_submit_mapped(cpsw->rxv[ch].ch, new_page, dma,
 391				       pkt_size, 0);
 392	if (ret < 0) {
 393		WARN_ON(ret == -ENOMEM);
 394		page_pool_recycle_direct(pool, new_page);
 395	}
 396}
 397
 398static int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
 399				   unsigned short vid)
 400{
 401	struct cpsw_common *cpsw = priv->cpsw;
 402	int unreg_mcast_mask = 0;
 403	int mcast_mask;
 404	u32 port_mask;
 405	int ret;
 406
 407	port_mask = (1 << priv->emac_port) | ALE_PORT_HOST;
 408
 409	mcast_mask = ALE_PORT_HOST;
 410	if (priv->ndev->flags & IFF_ALLMULTI)
 411		unreg_mcast_mask = mcast_mask;
 412
 413	ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
 414				unreg_mcast_mask);
 415	if (ret != 0)
 416		return ret;
 417
 418	ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
 419				 HOST_PORT_NUM, ALE_VLAN, vid);
 420	if (ret != 0)
 421		goto clean_vid;
 422
 423	ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
 424				 mcast_mask, ALE_VLAN, vid, 0);
 425	if (ret != 0)
 426		goto clean_vlan_ucast;
 427	return 0;
 428
 429clean_vlan_ucast:
 430	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
 431			   HOST_PORT_NUM, ALE_VLAN, vid);
 432clean_vid:
 433	cpsw_ale_del_vlan(cpsw->ale, vid, 0);
 434	return ret;
 435}
 436
 437static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
 438				    __be16 proto, u16 vid)
 439{
 440	struct cpsw_priv *priv = netdev_priv(ndev);
 441	struct cpsw_common *cpsw = priv->cpsw;
 442	int ret, i;
 443
 444	if (cpsw_is_switch_en(cpsw)) {
 445		dev_dbg(cpsw->dev, ".ndo_vlan_rx_add_vid called in switch mode\n");
 446		return 0;
 447	}
 448
 449	if (vid == cpsw->data.default_vlan)
 450		return 0;
 451
 452	ret = pm_runtime_resume_and_get(cpsw->dev);
 453	if (ret < 0)
 454		return ret;
 455
 456	/* In dual EMAC, reserved VLAN id should not be used for
 457	 * creating VLAN interfaces as this can break the dual
 458	 * EMAC port separation
 459	 */
 460	for (i = 0; i < cpsw->data.slaves; i++) {
 461		if (cpsw->slaves[i].ndev &&
 462		    vid == cpsw->slaves[i].port_vlan) {
 463			ret = -EINVAL;
 464			goto err;
 465		}
 466	}
 467
 468	dev_dbg(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
 469	ret = cpsw_add_vlan_ale_entry(priv, vid);
 470err:
 471	pm_runtime_put(cpsw->dev);
 472	return ret;
 473}
 474
 475static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg)
 476{
 477	struct cpsw_priv *priv = arg;
 478
 479	if (!vdev || !vid)
 480		return 0;
 481
 482	cpsw_ndo_vlan_rx_add_vid(priv->ndev, 0, vid);
 483	return 0;
 484}
 485
 486/* restore resources after port reset */
 487static void cpsw_restore(struct cpsw_priv *priv)
 488{
 489	struct cpsw_common *cpsw = priv->cpsw;
 490
 491	/* restore vlan configurations */
 492	vlan_for_each(priv->ndev, cpsw_restore_vlans, priv);
 493
 494	/* restore MQPRIO offload */
 495	cpsw_mqprio_resume(&cpsw->slaves[priv->emac_port - 1], priv);
 496
 497	/* restore CBS offload */
 498	cpsw_cbs_resume(&cpsw->slaves[priv->emac_port - 1], priv);
 499
 500	cpsw_qos_clsflower_resume(priv);
 501}
 502
 503static void cpsw_init_stp_ale_entry(struct cpsw_common *cpsw)
 504{
 505	static const char stpa[] = {0x01, 0x80, 0xc2, 0x0, 0x0, 0x0};
 506
 507	cpsw_ale_add_mcast(cpsw->ale, stpa,
 508			   ALE_PORT_HOST, ALE_SUPER, 0,
 509			   ALE_MCAST_BLOCK_LEARN_FWD);
 510}
 511
 512static void cpsw_init_host_port_switch(struct cpsw_common *cpsw)
 513{
 514	int vlan = cpsw->data.default_vlan;
 515
 516	writel(CPSW_FIFO_NORMAL_MODE, &cpsw->host_port_regs->tx_in_ctl);
 517
 518	writel(vlan, &cpsw->host_port_regs->port_vlan);
 519
 520	cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
 521			  ALE_ALL_PORTS, ALE_ALL_PORTS,
 522			  ALE_PORT_1 | ALE_PORT_2);
 523
 524	cpsw_init_stp_ale_entry(cpsw);
 525
 526	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 1);
 527	dev_dbg(cpsw->dev, "Set P0_UNI_FLOOD\n");
 528	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 0);
 529}
 530
 531static void cpsw_init_host_port_dual_mac(struct cpsw_common *cpsw)
 532{
 533	int vlan = cpsw->data.default_vlan;
 534
 535	writel(CPSW_FIFO_DUAL_MAC_MODE, &cpsw->host_port_regs->tx_in_ctl);
 536
 537	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 0);
 538	dev_dbg(cpsw->dev, "unset P0_UNI_FLOOD\n");
 539
 540	writel(vlan, &cpsw->host_port_regs->port_vlan);
 541
 542	cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
 543	/* learning make no sense in dual_mac mode */
 544	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 1);
 545}
 546
 547static void cpsw_init_host_port(struct cpsw_priv *priv)
 548{
 549	struct cpsw_common *cpsw = priv->cpsw;
 550	u32 control_reg;
 551
 552	/* soft reset the controller and initialize ale */
 553	soft_reset("cpsw", &cpsw->regs->soft_reset);
 554	cpsw_ale_start(cpsw->ale);
 555
 556	/* switch to vlan unaware mode */
 557	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
 558			     CPSW_ALE_VLAN_AWARE);
 559	control_reg = readl(&cpsw->regs->control);
 560	control_reg |= CPSW_VLAN_AWARE | CPSW_RX_VLAN_ENCAP;
 561	writel(control_reg, &cpsw->regs->control);
 562
 563	/* setup host port priority mapping */
 564	writel_relaxed(CPDMA_TX_PRIORITY_MAP,
 565		       &cpsw->host_port_regs->cpdma_tx_pri_map);
 566	writel_relaxed(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
 567
 568	/* disable priority elevation */
 569	writel_relaxed(0, &cpsw->regs->ptype);
 570
 571	/* enable statistics collection only on all ports */
 572	writel_relaxed(0x7, &cpsw->regs->stat_port_en);
 573
 574	/* Enable internal fifo flow control */
 575	writel(0x7, &cpsw->regs->flow_control);
 576
 577	if (cpsw_is_switch_en(cpsw))
 578		cpsw_init_host_port_switch(cpsw);
 579	else
 580		cpsw_init_host_port_dual_mac(cpsw);
 581
 582	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
 583			     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
 584}
 585
 586static void cpsw_port_add_dual_emac_def_ale_entries(struct cpsw_priv *priv,
 587						    struct cpsw_slave *slave)
 588{
 589	u32 port_mask = 1 << priv->emac_port | ALE_PORT_HOST;
 590	struct cpsw_common *cpsw = priv->cpsw;
 591	u32 reg;
 592
 593	reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
 594	       CPSW2_PORT_VLAN;
 595	slave_write(slave, slave->port_vlan, reg);
 596
 597	cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
 598			  port_mask, port_mask, 0);
 599	cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
 600			   ALE_PORT_HOST, ALE_VLAN, slave->port_vlan,
 601			   ALE_MCAST_FWD);
 602	cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
 603			   HOST_PORT_NUM, ALE_VLAN |
 604			   ALE_SECURE, slave->port_vlan);
 605	cpsw_ale_control_set(cpsw->ale, priv->emac_port,
 606			     ALE_PORT_DROP_UNKNOWN_VLAN, 1);
 607	/* learning make no sense in dual_mac mode */
 608	cpsw_ale_control_set(cpsw->ale, priv->emac_port,
 609			     ALE_PORT_NOLEARN, 1);
 610}
 611
 612static void cpsw_port_add_switch_def_ale_entries(struct cpsw_priv *priv,
 613						 struct cpsw_slave *slave)
 614{
 615	u32 port_mask = 1 << priv->emac_port | ALE_PORT_HOST;
 616	struct cpsw_common *cpsw = priv->cpsw;
 617	u32 reg;
 618
 619	cpsw_ale_control_set(cpsw->ale, priv->emac_port,
 620			     ALE_PORT_DROP_UNKNOWN_VLAN, 0);
 621	cpsw_ale_control_set(cpsw->ale, priv->emac_port,
 622			     ALE_PORT_NOLEARN, 0);
 623	/* disabling SA_UPDATE required to make stp work, without this setting
 624	 * Host MAC addresses will jump between ports.
 625	 * As per TRM MAC address can be defined as unicast supervisory (super)
 626	 * by setting both (ALE_BLOCKED | ALE_SECURE) which should prevent
 627	 * SA_UPDATE, but HW seems works incorrectly and setting ALE_SECURE
 628	 * causes STP packets to be dropped due to ingress filter
 629	 *	if (source address found) and (secure) and
 630	 *	   (receive port number != port_number))
 631	 *	   then discard the packet
 632	 */
 633	cpsw_ale_control_set(cpsw->ale, priv->emac_port,
 634			     ALE_PORT_NO_SA_UPDATE, 1);
 635
 636	cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
 637			   port_mask, ALE_VLAN, slave->port_vlan,
 638			   ALE_MCAST_FWD_2);
 639	cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
 640			   HOST_PORT_NUM, ALE_VLAN, slave->port_vlan);
 641
 642	reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
 643	       CPSW2_PORT_VLAN;
 644	slave_write(slave, slave->port_vlan, reg);
 645}
 646
 647static void cpsw_adjust_link(struct net_device *ndev)
 648{
 649	struct cpsw_priv *priv = netdev_priv(ndev);
 650	struct cpsw_common *cpsw = priv->cpsw;
 651	struct cpsw_slave *slave;
 652	struct phy_device *phy;
 653	u32 mac_control = 0;
 654
 655	slave = &cpsw->slaves[priv->emac_port - 1];
 656	phy = slave->phy;
 657
 658	if (!phy)
 659		return;
 660
 661	if (phy->link) {
 662		mac_control = CPSW_SL_CTL_GMII_EN;
 663
 664		if (phy->speed == 1000)
 665			mac_control |= CPSW_SL_CTL_GIG;
 666		if (phy->duplex)
 667			mac_control |= CPSW_SL_CTL_FULLDUPLEX;
 668
 669		/* set speed_in input in case RMII mode is used in 100Mbps */
 670		if (phy->speed == 100)
 671			mac_control |= CPSW_SL_CTL_IFCTL_A;
 672		/* in band mode only works in 10Mbps RGMII mode */
 673		else if ((phy->speed == 10) && phy_interface_is_rgmii(phy))
 674			mac_control |= CPSW_SL_CTL_EXT_EN; /* In Band mode */
 675
 676		if (priv->rx_pause)
 677			mac_control |= CPSW_SL_CTL_RX_FLOW_EN;
 678
 679		if (priv->tx_pause)
 680			mac_control |= CPSW_SL_CTL_TX_FLOW_EN;
 681
 682		if (mac_control != slave->mac_control)
 683			cpsw_sl_ctl_set(slave->mac_sl, mac_control);
 684
 685		/* enable forwarding */
 686		cpsw_ale_control_set(cpsw->ale, priv->emac_port,
 687				     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
 688
 689		netif_tx_wake_all_queues(ndev);
 690
 691		if (priv->shp_cfg_speed &&
 692		    priv->shp_cfg_speed != slave->phy->speed &&
 693		    !cpsw_shp_is_off(priv))
 694			dev_warn(priv->dev, "Speed was changed, CBS shaper speeds are changed!");
 695	} else {
 696		netif_tx_stop_all_queues(ndev);
 697
 698		mac_control = 0;
 699		/* disable forwarding */
 700		cpsw_ale_control_set(cpsw->ale, priv->emac_port,
 701				     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
 702
 703		cpsw_sl_wait_for_idle(slave->mac_sl, 100);
 704
 705		cpsw_sl_ctl_reset(slave->mac_sl);
 706	}
 707
 708	if (mac_control != slave->mac_control)
 709		phy_print_status(phy);
 710
 711	slave->mac_control = mac_control;
 712
 713	if (phy->link && cpsw_need_resplit(cpsw))
 714		cpsw_split_res(cpsw);
 715}
 716
 717static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
 718{
 719	struct cpsw_common *cpsw = priv->cpsw;
 720	struct phy_device *phy;
 721
 722	cpsw_sl_reset(slave->mac_sl, 100);
 723	cpsw_sl_ctl_reset(slave->mac_sl);
 724
 725	/* setup priority mapping */
 726	cpsw_sl_reg_write(slave->mac_sl, CPSW_SL_RX_PRI_MAP,
 727			  RX_PRIORITY_MAPPING);
 728
 729	switch (cpsw->version) {
 730	case CPSW_VERSION_1:
 731		slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
 732		/* Increase RX FIFO size to 5 for supporting fullduplex
 733		 * flow control mode
 734		 */
 735		slave_write(slave,
 736			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
 737			    CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS);
 738		break;
 739	case CPSW_VERSION_2:
 740	case CPSW_VERSION_3:
 741	case CPSW_VERSION_4:
 742		slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
 743		/* Increase RX FIFO size to 5 for supporting fullduplex
 744		 * flow control mode
 745		 */
 746		slave_write(slave,
 747			    (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
 748			    CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS);
 749		break;
 750	}
 751
 752	/* setup max packet size, and mac address */
 753	cpsw_sl_reg_write(slave->mac_sl, CPSW_SL_RX_MAXLEN,
 754			  cpsw->rx_packet_max);
 755	cpsw_set_slave_mac(slave, priv);
 756
 757	slave->mac_control = 0;	/* no link yet */
 758
 759	if (cpsw_is_switch_en(cpsw))
 760		cpsw_port_add_switch_def_ale_entries(priv, slave);
 761	else
 762		cpsw_port_add_dual_emac_def_ale_entries(priv, slave);
 763
 764	if (!slave->data->phy_node)
 765		dev_err(priv->dev, "no phy found on slave %d\n",
 766			slave->slave_num);
 767	phy = of_phy_connect(priv->ndev, slave->data->phy_node,
 768			     &cpsw_adjust_link, 0, slave->data->phy_if);
 769	if (!phy) {
 770		dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n",
 771			slave->data->phy_node,
 772			slave->slave_num);
 773		return;
 774	}
 
 
 
 775	slave->phy = phy;
 776
 777	phy_attached_info(slave->phy);
 778
 779	phy_start(slave->phy);
 780
 781	/* Configure GMII_SEL register */
 782	phy_set_mode_ext(slave->data->ifphy, PHY_MODE_ETHERNET,
 783			 slave->data->phy_if);
 784}
 785
 786static int cpsw_ndo_stop(struct net_device *ndev)
 787{
 788	struct cpsw_priv *priv = netdev_priv(ndev);
 789	struct cpsw_common *cpsw = priv->cpsw;
 790	struct cpsw_slave *slave;
 791
 792	cpsw_info(priv, ifdown, "shutting down ndev\n");
 793	slave = &cpsw->slaves[priv->emac_port - 1];
 794	if (slave->phy)
 795		phy_stop(slave->phy);
 796
 797	netif_tx_stop_all_queues(priv->ndev);
 798
 799	if (slave->phy) {
 800		phy_disconnect(slave->phy);
 801		slave->phy = NULL;
 802	}
 803
 804	__hw_addr_ref_unsync_dev(&ndev->mc, ndev, cpsw_purge_all_mc);
 805
 806	if (cpsw->usage_count <= 1) {
 807		napi_disable(&cpsw->napi_rx);
 808		napi_disable(&cpsw->napi_tx);
 809		cpts_unregister(cpsw->cpts);
 810		cpsw_intr_disable(cpsw);
 811		cpdma_ctlr_stop(cpsw->dma);
 812		cpsw_ale_stop(cpsw->ale);
 813		cpsw_destroy_xdp_rxqs(cpsw);
 814	}
 815
 816	if (cpsw_need_resplit(cpsw))
 817		cpsw_split_res(cpsw);
 818
 819	cpsw->usage_count--;
 820	pm_runtime_put_sync(cpsw->dev);
 821	return 0;
 822}
 823
 824static int cpsw_ndo_open(struct net_device *ndev)
 825{
 826	struct cpsw_priv *priv = netdev_priv(ndev);
 827	struct cpsw_common *cpsw = priv->cpsw;
 828	int ret;
 829
 830	dev_info(priv->dev, "starting ndev. mode: %s\n",
 831		 cpsw_is_switch_en(cpsw) ? "switch" : "dual_mac");
 832	ret = pm_runtime_resume_and_get(cpsw->dev);
 833	if (ret < 0)
 834		return ret;
 835
 836	/* Notify the stack of the actual queue counts. */
 837	ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
 838	if (ret) {
 839		dev_err(priv->dev, "cannot set real number of tx queues\n");
 840		goto pm_cleanup;
 841	}
 842
 843	ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
 844	if (ret) {
 845		dev_err(priv->dev, "cannot set real number of rx queues\n");
 846		goto pm_cleanup;
 847	}
 848
 849	/* Initialize host and slave ports */
 850	if (!cpsw->usage_count)
 851		cpsw_init_host_port(priv);
 852	cpsw_slave_open(&cpsw->slaves[priv->emac_port - 1], priv);
 853
 854	/* initialize shared resources for every ndev */
 855	if (!cpsw->usage_count) {
 856		/* create rxqs for both infs in dual mac as they use same pool
 857		 * and must be destroyed together when no users.
 858		 */
 859		ret = cpsw_create_xdp_rxqs(cpsw);
 860		if (ret < 0)
 861			goto err_cleanup;
 862
 863		ret = cpsw_fill_rx_channels(priv);
 864		if (ret < 0)
 865			goto err_cleanup;
 866
 867		if (cpsw->cpts) {
 868			if (cpts_register(cpsw->cpts))
 869				dev_err(priv->dev, "error registering cpts device\n");
 870			else
 871				writel(0x10, &cpsw->wr_regs->misc_en);
 872		}
 873
 874		napi_enable(&cpsw->napi_rx);
 875		napi_enable(&cpsw->napi_tx);
 876
 877		if (cpsw->tx_irq_disabled) {
 878			cpsw->tx_irq_disabled = false;
 879			enable_irq(cpsw->irqs_table[1]);
 880		}
 881
 882		if (cpsw->rx_irq_disabled) {
 883			cpsw->rx_irq_disabled = false;
 884			enable_irq(cpsw->irqs_table[0]);
 885		}
 886	}
 887
 888	cpsw_restore(priv);
 889
 890	/* Enable Interrupt pacing if configured */
 891	if (cpsw->coal_intvl != 0) {
 892		struct ethtool_coalesce coal;
 893
 894		coal.rx_coalesce_usecs = cpsw->coal_intvl;
 895		cpsw_set_coalesce(ndev, &coal, NULL, NULL);
 896	}
 897
 898	cpdma_ctlr_start(cpsw->dma);
 899	cpsw_intr_enable(cpsw);
 900	cpsw->usage_count++;
 901
 902	return 0;
 903
 904err_cleanup:
 905	cpsw_ndo_stop(ndev);
 906
 907pm_cleanup:
 908	pm_runtime_put_sync(cpsw->dev);
 909	return ret;
 910}
 911
 912static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
 913				       struct net_device *ndev)
 914{
 915	struct cpsw_priv *priv = netdev_priv(ndev);
 916	struct cpsw_common *cpsw = priv->cpsw;
 917	struct cpts *cpts = cpsw->cpts;
 918	struct netdev_queue *txq;
 919	struct cpdma_chan *txch;
 920	int ret, q_idx;
 921
 922	if (skb_put_padto(skb, READ_ONCE(priv->tx_packet_min))) {
 923		cpsw_err(priv, tx_err, "packet pad failed\n");
 924		ndev->stats.tx_dropped++;
 925		return NET_XMIT_DROP;
 926	}
 927
 928	if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
 929	    priv->tx_ts_enabled && cpts_can_timestamp(cpts, skb))
 930		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
 931
 932	q_idx = skb_get_queue_mapping(skb);
 933	if (q_idx >= cpsw->tx_ch_num)
 934		q_idx = q_idx % cpsw->tx_ch_num;
 935
 936	txch = cpsw->txv[q_idx].ch;
 937	txq = netdev_get_tx_queue(ndev, q_idx);
 938	skb_tx_timestamp(skb);
 939	ret = cpdma_chan_submit(txch, skb, skb->data, skb->len,
 940				priv->emac_port);
 941	if (unlikely(ret != 0)) {
 942		cpsw_err(priv, tx_err, "desc submit failed\n");
 943		goto fail;
 944	}
 945
 946	/* If there is no more tx desc left free then we need to
 947	 * tell the kernel to stop sending us tx frames.
 948	 */
 949	if (unlikely(!cpdma_check_free_tx_desc(txch))) {
 950		netif_tx_stop_queue(txq);
 951
 952		/* Barrier, so that stop_queue visible to other cpus */
 953		smp_mb__after_atomic();
 954
 955		if (cpdma_check_free_tx_desc(txch))
 956			netif_tx_wake_queue(txq);
 957	}
 958
 959	return NETDEV_TX_OK;
 960fail:
 961	ndev->stats.tx_dropped++;
 962	netif_tx_stop_queue(txq);
 963
 964	/* Barrier, so that stop_queue visible to other cpus */
 965	smp_mb__after_atomic();
 966
 967	if (cpdma_check_free_tx_desc(txch))
 968		netif_tx_wake_queue(txq);
 969
 970	return NETDEV_TX_BUSY;
 971}
 972
 973static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
 974{
 975	struct sockaddr *addr = (struct sockaddr *)p;
 976	struct cpsw_priv *priv = netdev_priv(ndev);
 977	struct cpsw_common *cpsw = priv->cpsw;
 978	int ret, slave_no;
 979	int flags = 0;
 980	u16 vid = 0;
 981
 982	slave_no = cpsw_slave_index(cpsw, priv);
 983	if (!is_valid_ether_addr(addr->sa_data))
 984		return -EADDRNOTAVAIL;
 985
 986	ret = pm_runtime_resume_and_get(cpsw->dev);
 987	if (ret < 0)
 988		return ret;
 989
 990	vid = cpsw->slaves[slave_no].port_vlan;
 991	flags = ALE_VLAN | ALE_SECURE;
 992
 993	cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
 994			   flags, vid);
 995	cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
 996			   flags, vid);
 997
 998	ether_addr_copy(priv->mac_addr, addr->sa_data);
 999	eth_hw_addr_set(ndev, priv->mac_addr);
1000	cpsw_set_slave_mac(&cpsw->slaves[slave_no], priv);
1001
1002	pm_runtime_put(cpsw->dev);
1003
1004	return 0;
1005}
1006
1007static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
1008				     __be16 proto, u16 vid)
1009{
1010	struct cpsw_priv *priv = netdev_priv(ndev);
1011	struct cpsw_common *cpsw = priv->cpsw;
1012	int ret;
1013	int i;
1014
1015	if (cpsw_is_switch_en(cpsw)) {
1016		dev_dbg(cpsw->dev, "ndo del vlan is called in switch mode\n");
1017		return 0;
1018	}
1019
1020	if (vid == cpsw->data.default_vlan)
1021		return 0;
1022
1023	ret = pm_runtime_resume_and_get(cpsw->dev);
1024	if (ret < 0)
1025		return ret;
1026
1027	/* reset the return code as pm_runtime_get_sync() can return
1028	 * non zero values as well.
1029	 */
1030	ret = 0;
1031	for (i = 0; i < cpsw->data.slaves; i++) {
1032		if (cpsw->slaves[i].ndev &&
1033		    vid == cpsw->slaves[i].port_vlan) {
1034			ret = -EINVAL;
1035			goto err;
1036		}
1037	}
1038
1039	dev_dbg(priv->dev, "removing vlanid %d from vlan filter\n", vid);
1040	ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
1041	if (ret)
1042		dev_err(priv->dev, "cpsw_ale_del_vlan() failed: ret %d\n", ret);
1043	ret = cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
1044				 HOST_PORT_NUM, ALE_VLAN, vid);
1045	if (ret)
1046		dev_err(priv->dev, "cpsw_ale_del_ucast() failed: ret %d\n",
1047			ret);
1048	ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
1049				 0, ALE_VLAN, vid);
1050	if (ret)
1051		dev_err(priv->dev, "cpsw_ale_del_mcast failed. ret %d\n",
1052			ret);
1053	cpsw_ale_flush_multicast(cpsw->ale, ALE_PORT_HOST, vid);
1054	ret = 0;
1055err:
1056	pm_runtime_put(cpsw->dev);
1057	return ret;
1058}
1059
1060static int cpsw_ndo_get_phys_port_name(struct net_device *ndev, char *name,
1061				       size_t len)
1062{
1063	struct cpsw_priv *priv = netdev_priv(ndev);
1064	int err;
1065
1066	err = snprintf(name, len, "p%d", priv->emac_port);
1067
1068	if (err >= len)
1069		return -EINVAL;
1070
1071	return 0;
1072}
1073
1074#ifdef CONFIG_NET_POLL_CONTROLLER
1075static void cpsw_ndo_poll_controller(struct net_device *ndev)
1076{
1077	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1078
1079	cpsw_intr_disable(cpsw);
1080	cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
1081	cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
1082	cpsw_intr_enable(cpsw);
1083}
1084#endif
1085
1086static int cpsw_ndo_xdp_xmit(struct net_device *ndev, int n,
1087			     struct xdp_frame **frames, u32 flags)
1088{
1089	struct cpsw_priv *priv = netdev_priv(ndev);
1090	struct xdp_frame *xdpf;
1091	int i, nxmit = 0;
1092
1093	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
1094		return -EINVAL;
1095
1096	for (i = 0; i < n; i++) {
1097		xdpf = frames[i];
1098		if (xdpf->len < READ_ONCE(priv->tx_packet_min))
1099			break;
1100
1101		if (cpsw_xdp_tx_frame(priv, xdpf, NULL, priv->emac_port))
1102			break;
1103		nxmit++;
1104	}
1105
1106	return nxmit;
1107}
1108
1109static int cpsw_get_port_parent_id(struct net_device *ndev,
1110				   struct netdev_phys_item_id *ppid)
1111{
1112	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1113
1114	ppid->id_len = sizeof(cpsw->base_mac);
1115	memcpy(&ppid->id, &cpsw->base_mac, ppid->id_len);
1116
1117	return 0;
1118}
1119
1120static const struct net_device_ops cpsw_netdev_ops = {
1121	.ndo_open		= cpsw_ndo_open,
1122	.ndo_stop		= cpsw_ndo_stop,
1123	.ndo_start_xmit		= cpsw_ndo_start_xmit,
1124	.ndo_set_mac_address	= cpsw_ndo_set_mac_address,
1125	.ndo_eth_ioctl		= cpsw_ndo_ioctl,
1126	.ndo_validate_addr	= eth_validate_addr,
1127	.ndo_tx_timeout		= cpsw_ndo_tx_timeout,
1128	.ndo_set_rx_mode	= cpsw_ndo_set_rx_mode,
1129	.ndo_set_tx_maxrate	= cpsw_ndo_set_tx_maxrate,
1130#ifdef CONFIG_NET_POLL_CONTROLLER
1131	.ndo_poll_controller	= cpsw_ndo_poll_controller,
1132#endif
1133	.ndo_vlan_rx_add_vid	= cpsw_ndo_vlan_rx_add_vid,
1134	.ndo_vlan_rx_kill_vid	= cpsw_ndo_vlan_rx_kill_vid,
1135	.ndo_setup_tc           = cpsw_ndo_setup_tc,
1136	.ndo_get_phys_port_name = cpsw_ndo_get_phys_port_name,
1137	.ndo_bpf		= cpsw_ndo_bpf,
1138	.ndo_xdp_xmit		= cpsw_ndo_xdp_xmit,
1139	.ndo_get_port_parent_id	= cpsw_get_port_parent_id,
1140};
1141
1142static void cpsw_get_drvinfo(struct net_device *ndev,
1143			     struct ethtool_drvinfo *info)
1144{
1145	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1146	struct platform_device *pdev;
1147
1148	pdev = to_platform_device(cpsw->dev);
1149	strscpy(info->driver, "cpsw-switch", sizeof(info->driver));
1150	strscpy(info->version, "2.0", sizeof(info->version));
1151	strscpy(info->bus_info, pdev->name, sizeof(info->bus_info));
1152}
1153
1154static int cpsw_set_pauseparam(struct net_device *ndev,
1155			       struct ethtool_pauseparam *pause)
1156{
1157	struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1158	struct cpsw_priv *priv = netdev_priv(ndev);
1159	int slave_no;
1160
1161	slave_no = cpsw_slave_index(cpsw, priv);
1162	if (!cpsw->slaves[slave_no].phy)
1163		return -EINVAL;
1164
1165	if (!phy_validate_pause(cpsw->slaves[slave_no].phy, pause))
1166		return -EINVAL;
1167
1168	priv->rx_pause = pause->rx_pause ? true : false;
1169	priv->tx_pause = pause->tx_pause ? true : false;
1170
1171	phy_set_asym_pause(cpsw->slaves[slave_no].phy,
1172			   priv->rx_pause, priv->tx_pause);
1173
1174	return 0;
1175}
1176
1177static int cpsw_set_channels(struct net_device *ndev,
1178			     struct ethtool_channels *chs)
1179{
1180	return cpsw_set_channels_common(ndev, chs, cpsw_rx_handler);
1181}
1182
1183static const struct ethtool_ops cpsw_ethtool_ops = {
1184	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS,
1185	.get_drvinfo		= cpsw_get_drvinfo,
1186	.get_msglevel		= cpsw_get_msglevel,
1187	.set_msglevel		= cpsw_set_msglevel,
1188	.get_link		= ethtool_op_get_link,
1189	.get_ts_info		= cpsw_get_ts_info,
1190	.get_coalesce		= cpsw_get_coalesce,
1191	.set_coalesce		= cpsw_set_coalesce,
1192	.get_sset_count		= cpsw_get_sset_count,
1193	.get_strings		= cpsw_get_strings,
1194	.get_ethtool_stats	= cpsw_get_ethtool_stats,
1195	.get_pauseparam		= cpsw_get_pauseparam,
1196	.set_pauseparam		= cpsw_set_pauseparam,
1197	.get_wol		= cpsw_get_wol,
1198	.set_wol		= cpsw_set_wol,
1199	.get_regs_len		= cpsw_get_regs_len,
1200	.get_regs		= cpsw_get_regs,
1201	.begin			= cpsw_ethtool_op_begin,
1202	.complete		= cpsw_ethtool_op_complete,
1203	.get_channels		= cpsw_get_channels,
1204	.set_channels		= cpsw_set_channels,
1205	.get_link_ksettings	= cpsw_get_link_ksettings,
1206	.set_link_ksettings	= cpsw_set_link_ksettings,
1207	.get_eee		= cpsw_get_eee,
1208	.set_eee		= cpsw_set_eee,
1209	.nway_reset		= cpsw_nway_reset,
1210	.get_ringparam		= cpsw_get_ringparam,
1211	.set_ringparam		= cpsw_set_ringparam,
1212};
1213
1214static int cpsw_probe_dt(struct cpsw_common *cpsw)
1215{
1216	struct device_node *node = cpsw->dev->of_node, *tmp_node, *port_np;
1217	struct cpsw_platform_data *data = &cpsw->data;
1218	struct device *dev = cpsw->dev;
1219	int ret;
1220	u32 prop;
1221
1222	if (!node)
1223		return -EINVAL;
1224
1225	tmp_node = of_get_child_by_name(node, "ethernet-ports");
1226	if (!tmp_node)
1227		return -ENOENT;
1228	data->slaves = of_get_child_count(tmp_node);
1229	if (data->slaves != CPSW_SLAVE_PORTS_NUM) {
1230		of_node_put(tmp_node);
1231		return -ENOENT;
1232	}
1233
1234	data->active_slave = 0;
1235	data->channels = CPSW_MAX_QUEUES;
1236	data->dual_emac = true;
1237	data->bd_ram_size = CPSW_BD_RAM_SIZE;
1238	data->mac_control = 0;
1239
1240	data->slave_data = devm_kcalloc(dev, CPSW_SLAVE_PORTS_NUM,
1241					sizeof(struct cpsw_slave_data),
1242					GFP_KERNEL);
1243	if (!data->slave_data) {
1244		of_node_put(tmp_node);
1245		return -ENOMEM;
1246	}
1247
1248	/* Populate all the child nodes here...
1249	 */
1250	ret = devm_of_platform_populate(dev);
1251	/* We do not want to force this, as in some cases may not have child */
1252	if (ret)
1253		dev_warn(dev, "Doesn't have any child node\n");
1254
1255	for_each_child_of_node(tmp_node, port_np) {
1256		struct cpsw_slave_data *slave_data;
1257		u32 port_id;
1258
1259		ret = of_property_read_u32(port_np, "reg", &port_id);
1260		if (ret < 0) {
1261			dev_err(dev, "%pOF error reading port_id %d\n",
1262				port_np, ret);
1263			goto err_node_put;
1264		}
1265
1266		if (!port_id || port_id > CPSW_SLAVE_PORTS_NUM) {
1267			dev_err(dev, "%pOF has invalid port_id %u\n",
1268				port_np, port_id);
1269			ret = -EINVAL;
1270			goto err_node_put;
1271		}
1272
1273		slave_data = &data->slave_data[port_id - 1];
1274
1275		slave_data->disabled = !of_device_is_available(port_np);
1276		if (slave_data->disabled)
1277			continue;
1278
1279		slave_data->slave_node = port_np;
1280		slave_data->ifphy = devm_of_phy_get(dev, port_np, NULL);
1281		if (IS_ERR(slave_data->ifphy)) {
1282			ret = PTR_ERR(slave_data->ifphy);
1283			dev_err(dev, "%pOF: Error retrieving port phy: %d\n",
1284				port_np, ret);
1285			goto err_node_put;
1286		}
1287
1288		if (of_phy_is_fixed_link(port_np)) {
1289			ret = of_phy_register_fixed_link(port_np);
1290			if (ret) {
1291				dev_err_probe(dev, ret, "%pOF failed to register fixed-link phy\n",
1292					      port_np);
1293				goto err_node_put;
1294			}
1295			slave_data->phy_node = of_node_get(port_np);
1296		} else {
1297			slave_data->phy_node =
1298				of_parse_phandle(port_np, "phy-handle", 0);
1299		}
1300
1301		if (!slave_data->phy_node) {
1302			dev_err(dev, "%pOF no phy found\n", port_np);
1303			ret = -ENODEV;
1304			goto err_node_put;
1305		}
1306
1307		ret = of_get_phy_mode(port_np, &slave_data->phy_if);
1308		if (ret) {
1309			dev_err(dev, "%pOF read phy-mode err %d\n",
1310				port_np, ret);
1311			goto err_node_put;
1312		}
1313
1314		ret = of_get_mac_address(port_np, slave_data->mac_addr);
1315		if (ret) {
1316			ret = ti_cm_get_macid(dev, port_id - 1,
1317					      slave_data->mac_addr);
1318			if (ret)
1319				goto err_node_put;
1320		}
1321
1322		if (of_property_read_u32(port_np, "ti,dual-emac-pvid",
1323					 &prop)) {
1324			dev_err(dev, "%pOF Missing dual_emac_res_vlan in DT.\n",
1325				port_np);
1326			slave_data->dual_emac_res_vlan = port_id;
1327			dev_err(dev, "%pOF Using %d as Reserved VLAN\n",
1328				port_np, slave_data->dual_emac_res_vlan);
1329		} else {
1330			slave_data->dual_emac_res_vlan = prop;
1331		}
1332	}
1333
1334	of_node_put(tmp_node);
1335	return 0;
1336
1337err_node_put:
1338	of_node_put(port_np);
1339	of_node_put(tmp_node);
1340	return ret;
1341}
1342
1343static void cpsw_remove_dt(struct cpsw_common *cpsw)
1344{
1345	struct cpsw_platform_data *data = &cpsw->data;
1346	int i = 0;
1347
1348	for (i = 0; i < cpsw->data.slaves; i++) {
1349		struct cpsw_slave_data *slave_data = &data->slave_data[i];
1350		struct device_node *port_np = slave_data->phy_node;
1351
1352		if (port_np) {
1353			if (of_phy_is_fixed_link(port_np))
1354				of_phy_deregister_fixed_link(port_np);
1355
1356			of_node_put(port_np);
1357		}
1358	}
1359}
1360
1361static int cpsw_create_ports(struct cpsw_common *cpsw)
1362{
1363	struct cpsw_platform_data *data = &cpsw->data;
1364	struct net_device *ndev, *napi_ndev = NULL;
1365	struct device *dev = cpsw->dev;
1366	struct cpsw_priv *priv;
1367	int ret = 0, i = 0;
1368
1369	for (i = 0; i < cpsw->data.slaves; i++) {
1370		struct cpsw_slave_data *slave_data = &data->slave_data[i];
1371
1372		if (slave_data->disabled)
1373			continue;
1374
1375		ndev = devm_alloc_etherdev_mqs(dev, sizeof(struct cpsw_priv),
1376					       CPSW_MAX_QUEUES,
1377					       CPSW_MAX_QUEUES);
1378		if (!ndev) {
1379			dev_err(dev, "error allocating net_device\n");
1380			return -ENOMEM;
1381		}
1382
1383		priv = netdev_priv(ndev);
1384		priv->cpsw = cpsw;
1385		priv->ndev = ndev;
1386		priv->dev  = dev;
1387		priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
1388		priv->emac_port = i + 1;
1389		priv->tx_packet_min = CPSW_MIN_PACKET_SIZE;
1390
1391		if (is_valid_ether_addr(slave_data->mac_addr)) {
1392			ether_addr_copy(priv->mac_addr, slave_data->mac_addr);
1393			dev_info(cpsw->dev, "Detected MACID = %pM\n",
1394				 priv->mac_addr);
1395		} else {
1396			eth_random_addr(slave_data->mac_addr);
1397			dev_info(cpsw->dev, "Random MACID = %pM\n",
1398				 priv->mac_addr);
1399		}
1400		eth_hw_addr_set(ndev, slave_data->mac_addr);
1401		ether_addr_copy(priv->mac_addr, slave_data->mac_addr);
1402
1403		cpsw->slaves[i].ndev = ndev;
1404
1405		ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER |
1406				  NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_NETNS_LOCAL | NETIF_F_HW_TC;
 
 
 
 
 
1407
1408		ndev->netdev_ops = &cpsw_netdev_ops;
1409		ndev->ethtool_ops = &cpsw_ethtool_ops;
1410		SET_NETDEV_DEV(ndev, dev);
1411
1412		if (!napi_ndev) {
1413			/* CPSW Host port CPDMA interface is shared between
1414			 * ports and there is only one TX and one RX IRQs
1415			 * available for all possible TX and RX channels
1416			 * accordingly.
1417			 */
1418			netif_napi_add(ndev, &cpsw->napi_rx,
1419				       cpsw->quirk_irq ? cpsw_rx_poll : cpsw_rx_mq_poll);
1420			netif_napi_add_tx(ndev, &cpsw->napi_tx,
1421					  cpsw->quirk_irq ?
1422					  cpsw_tx_poll : cpsw_tx_mq_poll);
1423		}
1424
1425		napi_ndev = ndev;
1426	}
1427
1428	return ret;
1429}
1430
1431static void cpsw_unregister_ports(struct cpsw_common *cpsw)
1432{
1433	int i = 0;
1434
1435	for (i = 0; i < cpsw->data.slaves; i++) {
1436		if (!cpsw->slaves[i].ndev)
1437			continue;
1438
1439		unregister_netdev(cpsw->slaves[i].ndev);
1440	}
1441}
1442
1443static int cpsw_register_ports(struct cpsw_common *cpsw)
1444{
1445	int ret = 0, i = 0;
1446
1447	for (i = 0; i < cpsw->data.slaves; i++) {
1448		if (!cpsw->slaves[i].ndev)
1449			continue;
1450
1451		/* register the network device */
1452		ret = register_netdev(cpsw->slaves[i].ndev);
1453		if (ret) {
1454			dev_err(cpsw->dev,
1455				"cpsw: err registering net device%d\n", i);
1456			cpsw->slaves[i].ndev = NULL;
1457			break;
1458		}
1459	}
1460
1461	if (ret)
1462		cpsw_unregister_ports(cpsw);
1463	return ret;
1464}
1465
1466bool cpsw_port_dev_check(const struct net_device *ndev)
1467{
1468	if (ndev->netdev_ops == &cpsw_netdev_ops) {
1469		struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1470
1471		return !cpsw->data.dual_emac;
1472	}
1473
1474	return false;
1475}
1476
1477static void cpsw_port_offload_fwd_mark_update(struct cpsw_common *cpsw)
1478{
1479	int set_val = 0;
1480	int i;
1481
1482	if (!cpsw->ale_bypass &&
1483	    (cpsw->br_members == (ALE_PORT_1 | ALE_PORT_2)))
1484		set_val = 1;
1485
1486	dev_dbg(cpsw->dev, "set offload_fwd_mark %d\n", set_val);
1487
1488	for (i = 0; i < cpsw->data.slaves; i++) {
1489		struct net_device *sl_ndev = cpsw->slaves[i].ndev;
1490		struct cpsw_priv *priv = netdev_priv(sl_ndev);
1491
1492		priv->offload_fwd_mark = set_val;
1493	}
1494}
1495
1496static int cpsw_netdevice_port_link(struct net_device *ndev,
1497				    struct net_device *br_ndev,
1498				    struct netlink_ext_ack *extack)
1499{
1500	struct cpsw_priv *priv = netdev_priv(ndev);
1501	struct cpsw_common *cpsw = priv->cpsw;
1502	int err;
1503
1504	if (!cpsw->br_members) {
1505		cpsw->hw_bridge_dev = br_ndev;
1506	} else {
1507		/* This is adding the port to a second bridge, this is
1508		 * unsupported
1509		 */
1510		if (cpsw->hw_bridge_dev != br_ndev)
1511			return -EOPNOTSUPP;
1512	}
1513
1514	err = switchdev_bridge_port_offload(ndev, ndev, NULL, NULL, NULL,
1515					    false, extack);
1516	if (err)
1517		return err;
1518
1519	cpsw->br_members |= BIT(priv->emac_port);
1520
1521	cpsw_port_offload_fwd_mark_update(cpsw);
1522
1523	return NOTIFY_DONE;
1524}
1525
1526static void cpsw_netdevice_port_unlink(struct net_device *ndev)
1527{
1528	struct cpsw_priv *priv = netdev_priv(ndev);
1529	struct cpsw_common *cpsw = priv->cpsw;
1530
1531	switchdev_bridge_port_unoffload(ndev, NULL, NULL, NULL);
1532
1533	cpsw->br_members &= ~BIT(priv->emac_port);
1534
1535	cpsw_port_offload_fwd_mark_update(cpsw);
1536
1537	if (!cpsw->br_members)
1538		cpsw->hw_bridge_dev = NULL;
1539}
1540
1541/* netdev notifier */
1542static int cpsw_netdevice_event(struct notifier_block *unused,
1543				unsigned long event, void *ptr)
1544{
1545	struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
1546	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1547	struct netdev_notifier_changeupper_info *info;
1548	int ret = NOTIFY_DONE;
1549
1550	if (!cpsw_port_dev_check(ndev))
1551		return NOTIFY_DONE;
1552
1553	switch (event) {
1554	case NETDEV_CHANGEUPPER:
1555		info = ptr;
1556
1557		if (netif_is_bridge_master(info->upper_dev)) {
1558			if (info->linking)
1559				ret = cpsw_netdevice_port_link(ndev,
1560							       info->upper_dev,
1561							       extack);
1562			else
1563				cpsw_netdevice_port_unlink(ndev);
1564		}
1565		break;
1566	default:
1567		return NOTIFY_DONE;
1568	}
1569
1570	return notifier_from_errno(ret);
1571}
1572
1573static struct notifier_block cpsw_netdevice_nb __read_mostly = {
1574	.notifier_call = cpsw_netdevice_event,
1575};
1576
1577static int cpsw_register_notifiers(struct cpsw_common *cpsw)
1578{
1579	int ret = 0;
1580
1581	ret = register_netdevice_notifier(&cpsw_netdevice_nb);
1582	if (ret) {
1583		dev_err(cpsw->dev, "can't register netdevice notifier\n");
1584		return ret;
1585	}
1586
1587	ret = cpsw_switchdev_register_notifiers(cpsw);
1588	if (ret)
1589		unregister_netdevice_notifier(&cpsw_netdevice_nb);
1590
1591	return ret;
1592}
1593
1594static void cpsw_unregister_notifiers(struct cpsw_common *cpsw)
1595{
1596	cpsw_switchdev_unregister_notifiers(cpsw);
1597	unregister_netdevice_notifier(&cpsw_netdevice_nb);
1598}
1599
1600static const struct devlink_ops cpsw_devlink_ops = {
1601};
1602
1603static int cpsw_dl_switch_mode_get(struct devlink *dl, u32 id,
1604				   struct devlink_param_gset_ctx *ctx)
1605{
1606	struct cpsw_devlink *dl_priv = devlink_priv(dl);
1607	struct cpsw_common *cpsw = dl_priv->cpsw;
1608
1609	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1610
1611	if (id != CPSW_DL_PARAM_SWITCH_MODE)
1612		return  -EOPNOTSUPP;
1613
1614	ctx->val.vbool = !cpsw->data.dual_emac;
1615
1616	return 0;
1617}
1618
1619static int cpsw_dl_switch_mode_set(struct devlink *dl, u32 id,
1620				   struct devlink_param_gset_ctx *ctx)
 
1621{
1622	struct cpsw_devlink *dl_priv = devlink_priv(dl);
1623	struct cpsw_common *cpsw = dl_priv->cpsw;
1624	int vlan = cpsw->data.default_vlan;
1625	bool switch_en = ctx->val.vbool;
1626	bool if_running = false;
1627	int i;
1628
1629	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1630
1631	if (id != CPSW_DL_PARAM_SWITCH_MODE)
1632		return  -EOPNOTSUPP;
1633
1634	if (switch_en == !cpsw->data.dual_emac)
1635		return 0;
1636
1637	if (!switch_en && cpsw->br_members) {
1638		dev_err(cpsw->dev, "Remove ports from BR before disabling switch mode\n");
1639		return -EINVAL;
1640	}
1641
1642	rtnl_lock();
1643
1644	for (i = 0; i < cpsw->data.slaves; i++) {
1645		struct cpsw_slave *slave = &cpsw->slaves[i];
1646		struct net_device *sl_ndev = slave->ndev;
1647
1648		if (!sl_ndev || !netif_running(sl_ndev))
1649			continue;
1650
1651		if_running = true;
1652	}
1653
1654	if (!if_running) {
1655		/* all ndevs are down */
1656		cpsw->data.dual_emac = !switch_en;
1657		for (i = 0; i < cpsw->data.slaves; i++) {
1658			struct cpsw_slave *slave = &cpsw->slaves[i];
1659			struct net_device *sl_ndev = slave->ndev;
1660
1661			if (!sl_ndev)
1662				continue;
1663
1664			if (switch_en)
1665				vlan = cpsw->data.default_vlan;
1666			else
1667				vlan = slave->data->dual_emac_res_vlan;
1668			slave->port_vlan = vlan;
1669		}
1670		goto exit;
1671	}
1672
1673	if (switch_en) {
1674		dev_info(cpsw->dev, "Enable switch mode\n");
1675
1676		/* enable bypass - no forwarding; all traffic goes to Host */
1677		cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1);
1678
1679		/* clean up ALE table */
1680		cpsw_ale_control_set(cpsw->ale, 0, ALE_CLEAR, 1);
1681		cpsw_ale_control_get(cpsw->ale, 0, ALE_AGEOUT);
1682
1683		cpsw_init_host_port_switch(cpsw);
1684
1685		for (i = 0; i < cpsw->data.slaves; i++) {
1686			struct cpsw_slave *slave = &cpsw->slaves[i];
1687			struct net_device *sl_ndev = slave->ndev;
1688			struct cpsw_priv *priv;
1689
1690			if (!sl_ndev)
1691				continue;
1692
1693			priv = netdev_priv(sl_ndev);
1694			slave->port_vlan = vlan;
1695			WRITE_ONCE(priv->tx_packet_min, CPSW_MIN_PACKET_SIZE_VLAN);
1696			if (netif_running(sl_ndev))
1697				cpsw_port_add_switch_def_ale_entries(priv,
1698								     slave);
1699		}
1700
1701		cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 0);
1702		cpsw->data.dual_emac = false;
1703	} else {
1704		dev_info(cpsw->dev, "Disable switch mode\n");
1705
1706		/* enable bypass - no forwarding; all traffic goes to Host */
1707		cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1);
1708
1709		cpsw_ale_control_set(cpsw->ale, 0, ALE_CLEAR, 1);
1710		cpsw_ale_control_get(cpsw->ale, 0, ALE_AGEOUT);
1711
1712		cpsw_init_host_port_dual_mac(cpsw);
1713
1714		for (i = 0; i < cpsw->data.slaves; i++) {
1715			struct cpsw_slave *slave = &cpsw->slaves[i];
1716			struct net_device *sl_ndev = slave->ndev;
1717			struct cpsw_priv *priv;
1718
1719			if (!sl_ndev)
1720				continue;
1721
1722			priv = netdev_priv(slave->ndev);
1723			slave->port_vlan = slave->data->dual_emac_res_vlan;
1724			WRITE_ONCE(priv->tx_packet_min, CPSW_MIN_PACKET_SIZE);
1725			cpsw_port_add_dual_emac_def_ale_entries(priv, slave);
1726		}
1727
1728		cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 0);
1729		cpsw->data.dual_emac = true;
1730	}
1731exit:
1732	rtnl_unlock();
1733
1734	return 0;
1735}
1736
1737static int cpsw_dl_ale_ctrl_get(struct devlink *dl, u32 id,
1738				struct devlink_param_gset_ctx *ctx)
1739{
1740	struct cpsw_devlink *dl_priv = devlink_priv(dl);
1741	struct cpsw_common *cpsw = dl_priv->cpsw;
1742
1743	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1744
1745	switch (id) {
1746	case CPSW_DL_PARAM_ALE_BYPASS:
1747		ctx->val.vbool = cpsw_ale_control_get(cpsw->ale, 0, ALE_BYPASS);
1748		break;
1749	default:
1750		return -EOPNOTSUPP;
1751	}
1752
1753	return 0;
1754}
1755
1756static int cpsw_dl_ale_ctrl_set(struct devlink *dl, u32 id,
1757				struct devlink_param_gset_ctx *ctx)
 
1758{
1759	struct cpsw_devlink *dl_priv = devlink_priv(dl);
1760	struct cpsw_common *cpsw = dl_priv->cpsw;
1761	int ret = -EOPNOTSUPP;
1762
1763	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
1764
1765	switch (id) {
1766	case CPSW_DL_PARAM_ALE_BYPASS:
1767		ret = cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS,
1768					   ctx->val.vbool);
1769		if (!ret) {
1770			cpsw->ale_bypass = ctx->val.vbool;
1771			cpsw_port_offload_fwd_mark_update(cpsw);
1772		}
1773		break;
1774	default:
1775		return -EOPNOTSUPP;
1776	}
1777
1778	return 0;
1779}
1780
1781static const struct devlink_param cpsw_devlink_params[] = {
1782	DEVLINK_PARAM_DRIVER(CPSW_DL_PARAM_SWITCH_MODE,
1783			     "switch_mode", DEVLINK_PARAM_TYPE_BOOL,
1784			     BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1785			     cpsw_dl_switch_mode_get, cpsw_dl_switch_mode_set,
1786			     NULL),
1787	DEVLINK_PARAM_DRIVER(CPSW_DL_PARAM_ALE_BYPASS,
1788			     "ale_bypass", DEVLINK_PARAM_TYPE_BOOL,
1789			     BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1790			     cpsw_dl_ale_ctrl_get, cpsw_dl_ale_ctrl_set, NULL),
1791};
1792
1793static int cpsw_register_devlink(struct cpsw_common *cpsw)
1794{
1795	struct device *dev = cpsw->dev;
1796	struct cpsw_devlink *dl_priv;
1797	int ret = 0;
1798
1799	cpsw->devlink = devlink_alloc(&cpsw_devlink_ops, sizeof(*dl_priv), dev);
1800	if (!cpsw->devlink)
1801		return -ENOMEM;
1802
1803	dl_priv = devlink_priv(cpsw->devlink);
1804	dl_priv->cpsw = cpsw;
1805
1806	ret = devlink_params_register(cpsw->devlink, cpsw_devlink_params,
1807				      ARRAY_SIZE(cpsw_devlink_params));
1808	if (ret) {
1809		dev_err(dev, "DL params reg fail ret:%d\n", ret);
1810		goto dl_unreg;
1811	}
1812
1813	devlink_register(cpsw->devlink);
1814	return ret;
1815
1816dl_unreg:
1817	devlink_free(cpsw->devlink);
1818	return ret;
1819}
1820
1821static void cpsw_unregister_devlink(struct cpsw_common *cpsw)
1822{
1823	devlink_unregister(cpsw->devlink);
1824	devlink_params_unregister(cpsw->devlink, cpsw_devlink_params,
1825				  ARRAY_SIZE(cpsw_devlink_params));
1826	devlink_free(cpsw->devlink);
1827}
1828
1829static const struct of_device_id cpsw_of_mtable[] = {
1830	{ .compatible = "ti,cpsw-switch"},
1831	{ .compatible = "ti,am335x-cpsw-switch"},
1832	{ .compatible = "ti,am4372-cpsw-switch"},
1833	{ .compatible = "ti,dra7-cpsw-switch"},
1834	{ /* sentinel */ },
1835};
1836MODULE_DEVICE_TABLE(of, cpsw_of_mtable);
1837
1838static const struct soc_device_attribute cpsw_soc_devices[] = {
1839	{ .family = "AM33xx", .revision = "ES1.0"},
1840	{ /* sentinel */ }
1841};
1842
1843static int cpsw_probe(struct platform_device *pdev)
1844{
1845	const struct soc_device_attribute *soc;
1846	struct device *dev = &pdev->dev;
1847	struct cpsw_common *cpsw;
1848	struct resource *ss_res;
1849	struct gpio_descs *mode;
1850	void __iomem *ss_regs;
1851	int ret = 0, ch;
1852	struct clk *clk;
1853	int irq;
1854
1855	cpsw = devm_kzalloc(dev, sizeof(struct cpsw_common), GFP_KERNEL);
1856	if (!cpsw)
1857		return -ENOMEM;
1858
1859	cpsw_slave_index = cpsw_slave_index_priv;
1860
1861	cpsw->dev = dev;
1862
1863	cpsw->slaves = devm_kcalloc(dev,
1864				    CPSW_SLAVE_PORTS_NUM,
1865				    sizeof(struct cpsw_slave),
1866				    GFP_KERNEL);
1867	if (!cpsw->slaves)
1868		return -ENOMEM;
1869
1870	mode = devm_gpiod_get_array_optional(dev, "mode", GPIOD_OUT_LOW);
1871	if (IS_ERR(mode)) {
1872		ret = PTR_ERR(mode);
1873		dev_err(dev, "gpio request failed, ret %d\n", ret);
1874		return ret;
1875	}
1876
1877	clk = devm_clk_get(dev, "fck");
1878	if (IS_ERR(clk)) {
1879		ret = PTR_ERR(clk);
1880		dev_err(dev, "fck is not found %d\n", ret);
1881		return ret;
1882	}
1883	cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000;
1884
1885	ss_regs = devm_platform_get_and_ioremap_resource(pdev, 0, &ss_res);
1886	if (IS_ERR(ss_regs)) {
1887		ret = PTR_ERR(ss_regs);
1888		return ret;
1889	}
1890	cpsw->regs = ss_regs;
1891
1892	irq = platform_get_irq_byname(pdev, "rx");
1893	if (irq < 0)
1894		return irq;
1895	cpsw->irqs_table[0] = irq;
1896
1897	irq = platform_get_irq_byname(pdev, "tx");
1898	if (irq < 0)
1899		return irq;
1900	cpsw->irqs_table[1] = irq;
1901
1902	irq = platform_get_irq_byname(pdev, "misc");
1903	if (irq <= 0)
1904		return irq;
1905	cpsw->misc_irq = irq;
1906
1907	platform_set_drvdata(pdev, cpsw);
1908	/* This may be required here for child devices. */
1909	pm_runtime_enable(dev);
1910
1911	/* Need to enable clocks with runtime PM api to access module
1912	 * registers
1913	 */
1914	ret = pm_runtime_resume_and_get(dev);
1915	if (ret < 0) {
1916		pm_runtime_disable(dev);
1917		return ret;
1918	}
1919
1920	ret = cpsw_probe_dt(cpsw);
1921	if (ret)
1922		goto clean_dt_ret;
1923
1924	soc = soc_device_match(cpsw_soc_devices);
1925	if (soc)
1926		cpsw->quirk_irq = true;
1927
1928	cpsw->rx_packet_max = rx_packet_max;
1929	cpsw->descs_pool_size = descs_pool_size;
1930	eth_random_addr(cpsw->base_mac);
1931
1932	ret = cpsw_init_common(cpsw, ss_regs, ale_ageout,
1933			       (u32 __force)ss_res->start + CPSW2_BD_OFFSET,
1934			       descs_pool_size);
1935	if (ret)
1936		goto clean_dt_ret;
1937
1938	cpsw->wr_regs = cpsw->version == CPSW_VERSION_1 ?
1939			ss_regs + CPSW1_WR_OFFSET :
1940			ss_regs + CPSW2_WR_OFFSET;
1941
1942	ch = cpsw->quirk_irq ? 0 : 7;
1943	cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, ch, cpsw_tx_handler, 0);
1944	if (IS_ERR(cpsw->txv[0].ch)) {
1945		dev_err(dev, "error initializing tx dma channel\n");
1946		ret = PTR_ERR(cpsw->txv[0].ch);
1947		goto clean_cpts;
1948	}
1949
1950	cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1);
1951	if (IS_ERR(cpsw->rxv[0].ch)) {
1952		dev_err(dev, "error initializing rx dma channel\n");
1953		ret = PTR_ERR(cpsw->rxv[0].ch);
1954		goto clean_cpts;
1955	}
1956	cpsw_split_res(cpsw);
1957
1958	/* setup netdevs */
1959	ret = cpsw_create_ports(cpsw);
1960	if (ret)
1961		goto clean_unregister_netdev;
1962
1963	/* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and
1964	 * MISC IRQs which are always kept disabled with this driver so
1965	 * we will not request them.
1966	 *
1967	 * If anyone wants to implement support for those, make sure to
1968	 * first request and append them to irqs_table array.
1969	 */
1970
1971	ret = devm_request_irq(dev, cpsw->irqs_table[0], cpsw_rx_interrupt,
1972			       0, dev_name(dev), cpsw);
1973	if (ret < 0) {
1974		dev_err(dev, "error attaching irq (%d)\n", ret);
1975		goto clean_unregister_netdev;
1976	}
1977
1978	ret = devm_request_irq(dev, cpsw->irqs_table[1], cpsw_tx_interrupt,
1979			       0, dev_name(dev), cpsw);
1980	if (ret < 0) {
1981		dev_err(dev, "error attaching irq (%d)\n", ret);
1982		goto clean_unregister_netdev;
1983	}
1984
1985	if (!cpsw->cpts)
1986		goto skip_cpts;
1987
1988	ret = devm_request_irq(dev, cpsw->misc_irq, cpsw_misc_interrupt,
1989			       0, dev_name(&pdev->dev), cpsw);
1990	if (ret < 0) {
1991		dev_err(dev, "error attaching misc irq (%d)\n", ret);
1992		goto clean_unregister_netdev;
1993	}
1994
1995	/* Enable misc CPTS evnt_pend IRQ */
1996	cpts_set_irqpoll(cpsw->cpts, false);
1997
1998skip_cpts:
1999	ret = cpsw_register_notifiers(cpsw);
2000	if (ret)
2001		goto clean_unregister_netdev;
2002
2003	ret = cpsw_register_devlink(cpsw);
2004	if (ret)
2005		goto clean_unregister_notifiers;
2006
2007	ret = cpsw_register_ports(cpsw);
2008	if (ret)
2009		goto clean_unregister_notifiers;
2010
2011	dev_notice(dev, "initialized (regs %pa, pool size %d) hw_ver:%08X %d.%d (%d)\n",
2012		   &ss_res->start, descs_pool_size,
2013		   cpsw->version, CPSW_MAJOR_VERSION(cpsw->version),
2014		   CPSW_MINOR_VERSION(cpsw->version),
2015		   CPSW_RTL_VERSION(cpsw->version));
2016
2017	pm_runtime_put(dev);
2018
2019	return 0;
2020
2021clean_unregister_notifiers:
2022	cpsw_unregister_notifiers(cpsw);
2023clean_unregister_netdev:
2024	cpsw_unregister_ports(cpsw);
2025clean_cpts:
2026	cpts_release(cpsw->cpts);
2027	cpdma_ctlr_destroy(cpsw->dma);
2028clean_dt_ret:
2029	cpsw_remove_dt(cpsw);
2030	pm_runtime_put_sync(dev);
2031	pm_runtime_disable(dev);
2032	return ret;
2033}
2034
2035static int cpsw_remove(struct platform_device *pdev)
2036{
2037	struct cpsw_common *cpsw = platform_get_drvdata(pdev);
2038	int ret;
2039
2040	ret = pm_runtime_resume_and_get(&pdev->dev);
2041	if (ret < 0)
2042		return ret;
 
 
 
 
 
 
2043
2044	cpsw_unregister_notifiers(cpsw);
2045	cpsw_unregister_devlink(cpsw);
2046	cpsw_unregister_ports(cpsw);
2047
2048	cpts_release(cpsw->cpts);
2049	cpdma_ctlr_destroy(cpsw->dma);
2050	cpsw_remove_dt(cpsw);
2051	pm_runtime_put_sync(&pdev->dev);
2052	pm_runtime_disable(&pdev->dev);
2053	return 0;
2054}
2055
2056static int __maybe_unused cpsw_suspend(struct device *dev)
2057{
2058	struct cpsw_common *cpsw = dev_get_drvdata(dev);
2059	int i;
2060
2061	rtnl_lock();
2062
2063	for (i = 0; i < cpsw->data.slaves; i++) {
2064		struct net_device *ndev = cpsw->slaves[i].ndev;
2065
2066		if (!(ndev && netif_running(ndev)))
2067			continue;
2068
2069		cpsw_ndo_stop(ndev);
2070	}
2071
2072	rtnl_unlock();
2073
2074	/* Select sleep pin state */
2075	pinctrl_pm_select_sleep_state(dev);
2076
2077	return 0;
2078}
2079
2080static int __maybe_unused cpsw_resume(struct device *dev)
2081{
2082	struct cpsw_common *cpsw = dev_get_drvdata(dev);
2083	int i;
2084
2085	/* Select default pin state */
2086	pinctrl_pm_select_default_state(dev);
2087
2088	/* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */
2089	rtnl_lock();
2090
2091	for (i = 0; i < cpsw->data.slaves; i++) {
2092		struct net_device *ndev = cpsw->slaves[i].ndev;
2093
2094		if (!(ndev && netif_running(ndev)))
2095			continue;
2096
2097		cpsw_ndo_open(ndev);
2098	}
2099
2100	rtnl_unlock();
2101
2102	return 0;
2103}
2104
2105static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume);
2106
2107static struct platform_driver cpsw_driver = {
2108	.driver = {
2109		.name	 = "cpsw-switch",
2110		.pm	 = &cpsw_pm_ops,
2111		.of_match_table = cpsw_of_mtable,
2112	},
2113	.probe = cpsw_probe,
2114	.remove = cpsw_remove,
2115};
2116
2117module_platform_driver(cpsw_driver);
2118
2119MODULE_LICENSE("GPL");
2120MODULE_DESCRIPTION("TI CPSW switchdev Ethernet driver");