File Index
Files and directories
Sign in
FILE

cpsw.c

File preview with syntax highlighting.

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