1 // SPDX-License-Identifier: GPL-2.0
2 /* Texas Instruments K3 AM65 Ethernet Switch SubSystem Driver
3  *
4  * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com/
5  *
6  */
7 
8 #include <linux/clk.h>
9 #include <linux/etherdevice.h>
10 #include <linux/if_vlan.h>
11 #include <linux/interrupt.h>
12 #include <linux/irqdomain.h>
13 #include <linux/kernel.h>
14 #include <linux/kmemleak.h>
15 #include <linux/module.h>
16 #include <linux/netdevice.h>
17 #include <linux/net_tstamp.h>
18 #include <linux/of.h>
19 #include <linux/of_mdio.h>
20 #include <linux/of_net.h>
21 #include <linux/of_device.h>
22 #include <linux/phylink.h>
23 #include <linux/phy/phy.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/regmap.h>
27 #include <linux/mfd/syscon.h>
28 #include <linux/sys_soc.h>
29 #include <linux/dma/ti-cppi5.h>
30 #include <linux/dma/k3-udma-glue.h>
31 #include <net/switchdev.h>
32 
33 #include "cpsw_ale.h"
34 #include "cpsw_sl.h"
35 #include "am65-cpsw-nuss.h"
36 #include "am65-cpsw-switchdev.h"
37 #include "k3-cppi-desc-pool.h"
38 #include "am65-cpts.h"
39 
40 #define AM65_CPSW_SS_BASE	0x0
41 #define AM65_CPSW_SGMII_BASE	0x100
42 #define AM65_CPSW_XGMII_BASE	0x2100
43 #define AM65_CPSW_CPSW_NU_BASE	0x20000
44 #define AM65_CPSW_NU_PORTS_BASE	0x1000
45 #define AM65_CPSW_NU_FRAM_BASE	0x12000
46 #define AM65_CPSW_NU_STATS_BASE	0x1a000
47 #define AM65_CPSW_NU_ALE_BASE	0x1e000
48 #define AM65_CPSW_NU_CPTS_BASE	0x1d000
49 
50 #define AM65_CPSW_NU_PORTS_OFFSET	0x1000
51 #define AM65_CPSW_NU_STATS_PORT_OFFSET	0x200
52 #define AM65_CPSW_NU_FRAM_PORT_OFFSET	0x200
53 
54 #define AM65_CPSW_MAX_PORTS	8
55 
56 #define AM65_CPSW_MIN_PACKET_SIZE	VLAN_ETH_ZLEN
57 #define AM65_CPSW_MAX_PACKET_SIZE	(VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
58 
59 #define AM65_CPSW_REG_CTL		0x004
60 #define AM65_CPSW_REG_STAT_PORT_EN	0x014
61 #define AM65_CPSW_REG_PTYPE		0x018
62 
63 #define AM65_CPSW_P0_REG_CTL			0x004
64 #define AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET	0x008
65 
66 #define AM65_CPSW_PORT_REG_PRI_CTL		0x01c
67 #define AM65_CPSW_PORT_REG_RX_PRI_MAP		0x020
68 #define AM65_CPSW_PORT_REG_RX_MAXLEN		0x024
69 
70 #define AM65_CPSW_PORTN_REG_SA_L		0x308
71 #define AM65_CPSW_PORTN_REG_SA_H		0x30c
72 #define AM65_CPSW_PORTN_REG_TS_CTL              0x310
73 #define AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG	0x314
74 #define AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG	0x318
75 #define AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2       0x31C
76 
77 #define AM65_CPSW_SGMII_CONTROL_REG		0x010
78 #define AM65_CPSW_SGMII_CONTROL_MR_AN_ENABLE	BIT(0)
79 
80 #define AM65_CPSW_CTL_VLAN_AWARE		BIT(1)
81 #define AM65_CPSW_CTL_P0_ENABLE			BIT(2)
82 #define AM65_CPSW_CTL_P0_TX_CRC_REMOVE		BIT(13)
83 #define AM65_CPSW_CTL_P0_RX_PAD			BIT(14)
84 
85 /* AM65_CPSW_P0_REG_CTL */
86 #define AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN	BIT(0)
87 
88 /* AM65_CPSW_PORT_REG_PRI_CTL */
89 #define AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN	BIT(8)
90 
91 /* AM65_CPSW_PN_TS_CTL register fields */
92 #define AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN		BIT(4)
93 #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN	BIT(5)
94 #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT2_EN	BIT(6)
95 #define AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN		BIT(7)
96 #define AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN		BIT(10)
97 #define AM65_CPSW_PN_TS_CTL_TX_HOST_TS_EN	BIT(11)
98 #define AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT	16
99 
100 /* AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG register fields */
101 #define AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT	16
102 
103 /* AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2 */
104 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107	BIT(16)
105 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129	BIT(17)
106 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130	BIT(18)
107 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131	BIT(19)
108 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132	BIT(20)
109 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319	BIT(21)
110 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320	BIT(22)
111 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO BIT(23)
112 
113 /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
114 #define AM65_CPSW_TS_EVENT_MSG_TYPE_BITS (BIT(0) | BIT(1) | BIT(2) | BIT(3))
115 
116 #define AM65_CPSW_TS_SEQ_ID_OFFSET (0x1e)
117 
118 #define AM65_CPSW_TS_TX_ANX_ALL_EN		\
119 	(AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN |	\
120 	 AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN |	\
121 	 AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN)
122 
123 #define AM65_CPSW_ALE_AGEOUT_DEFAULT	30
124 /* Number of TX/RX descriptors */
125 #define AM65_CPSW_MAX_TX_DESC	500
126 #define AM65_CPSW_MAX_RX_DESC	500
127 
128 #define AM65_CPSW_NAV_PS_DATA_SIZE 16
129 #define AM65_CPSW_NAV_SW_DATA_SIZE 16
130 
131 #define AM65_CPSW_DEBUG	(NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK | \
132 			 NETIF_MSG_IFUP	| NETIF_MSG_PROBE | NETIF_MSG_IFDOWN | \
133 			 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
134 
135 static void am65_cpsw_port_set_sl_mac(struct am65_cpsw_port *slave,
136 				      const u8 *dev_addr)
137 {
138 	u32 mac_hi = (dev_addr[0] << 0) | (dev_addr[1] << 8) |
139 		     (dev_addr[2] << 16) | (dev_addr[3] << 24);
140 	u32 mac_lo = (dev_addr[4] << 0) | (dev_addr[5] << 8);
141 
142 	writel(mac_hi, slave->port_base + AM65_CPSW_PORTN_REG_SA_H);
143 	writel(mac_lo, slave->port_base + AM65_CPSW_PORTN_REG_SA_L);
144 }
145 
146 static void am65_cpsw_sl_ctl_reset(struct am65_cpsw_port *port)
147 {
148 	cpsw_sl_reset(port->slave.mac_sl, 100);
149 	/* Max length register has to be restored after MAC SL reset */
150 	writel(AM65_CPSW_MAX_PACKET_SIZE,
151 	       port->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN);
152 }
153 
154 static void am65_cpsw_nuss_get_ver(struct am65_cpsw_common *common)
155 {
156 	common->nuss_ver = readl(common->ss_base);
157 	common->cpsw_ver = readl(common->cpsw_base);
158 	dev_info(common->dev,
159 		 "initializing am65 cpsw nuss version 0x%08X, cpsw version 0x%08X Ports: %u quirks:%08x\n",
160 		common->nuss_ver,
161 		common->cpsw_ver,
162 		common->port_num + 1,
163 		common->pdata.quirks);
164 }
165 
166 static int am65_cpsw_nuss_ndo_slave_add_vid(struct net_device *ndev,
167 					    __be16 proto, u16 vid)
168 {
169 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
170 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
171 	u32 port_mask, unreg_mcast = 0;
172 	int ret;
173 
174 	if (!common->is_emac_mode)
175 		return 0;
176 
177 	if (!netif_running(ndev) || !vid)
178 		return 0;
179 
180 	ret = pm_runtime_resume_and_get(common->dev);
181 	if (ret < 0)
182 		return ret;
183 
184 	port_mask = BIT(port->port_id) | ALE_PORT_HOST;
185 	if (!vid)
186 		unreg_mcast = port_mask;
187 	dev_info(common->dev, "Adding vlan %d to vlan filter\n", vid);
188 	ret = cpsw_ale_vlan_add_modify(common->ale, vid, port_mask,
189 				       unreg_mcast, port_mask, 0);
190 
191 	pm_runtime_put(common->dev);
192 	return ret;
193 }
194 
195 static int am65_cpsw_nuss_ndo_slave_kill_vid(struct net_device *ndev,
196 					     __be16 proto, u16 vid)
197 {
198 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
199 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
200 	int ret;
201 
202 	if (!common->is_emac_mode)
203 		return 0;
204 
205 	if (!netif_running(ndev) || !vid)
206 		return 0;
207 
208 	ret = pm_runtime_resume_and_get(common->dev);
209 	if (ret < 0)
210 		return ret;
211 
212 	dev_info(common->dev, "Removing vlan %d from vlan filter\n", vid);
213 	ret = cpsw_ale_del_vlan(common->ale, vid,
214 				BIT(port->port_id) | ALE_PORT_HOST);
215 
216 	pm_runtime_put(common->dev);
217 	return ret;
218 }
219 
220 static void am65_cpsw_slave_set_promisc(struct am65_cpsw_port *port,
221 					bool promisc)
222 {
223 	struct am65_cpsw_common *common = port->common;
224 
225 	if (promisc && !common->is_emac_mode) {
226 		dev_dbg(common->dev, "promisc mode requested in switch mode");
227 		return;
228 	}
229 
230 	if (promisc) {
231 		/* Enable promiscuous mode */
232 		cpsw_ale_control_set(common->ale, port->port_id,
233 				     ALE_PORT_MACONLY_CAF, 1);
234 		dev_dbg(common->dev, "promisc enabled\n");
235 	} else {
236 		/* Disable promiscuous mode */
237 		cpsw_ale_control_set(common->ale, port->port_id,
238 				     ALE_PORT_MACONLY_CAF, 0);
239 		dev_dbg(common->dev, "promisc disabled\n");
240 	}
241 }
242 
243 static void am65_cpsw_nuss_ndo_slave_set_rx_mode(struct net_device *ndev)
244 {
245 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
246 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
247 	u32 port_mask;
248 	bool promisc;
249 
250 	promisc = !!(ndev->flags & IFF_PROMISC);
251 	am65_cpsw_slave_set_promisc(port, promisc);
252 
253 	if (promisc)
254 		return;
255 
256 	/* Restore allmulti on vlans if necessary */
257 	cpsw_ale_set_allmulti(common->ale,
258 			      ndev->flags & IFF_ALLMULTI, port->port_id);
259 
260 	port_mask = ALE_PORT_HOST;
261 	/* Clear all mcast from ALE */
262 	cpsw_ale_flush_multicast(common->ale, port_mask, -1);
263 
264 	if (!netdev_mc_empty(ndev)) {
265 		struct netdev_hw_addr *ha;
266 
267 		/* program multicast address list into ALE register */
268 		netdev_for_each_mc_addr(ha, ndev) {
269 			cpsw_ale_add_mcast(common->ale, ha->addr,
270 					   port_mask, 0, 0, 0);
271 		}
272 	}
273 }
274 
275 static void am65_cpsw_nuss_ndo_host_tx_timeout(struct net_device *ndev,
276 					       unsigned int txqueue)
277 {
278 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
279 	struct am65_cpsw_tx_chn *tx_chn;
280 	struct netdev_queue *netif_txq;
281 	unsigned long trans_start;
282 
283 	netif_txq = netdev_get_tx_queue(ndev, txqueue);
284 	tx_chn = &common->tx_chns[txqueue];
285 	trans_start = READ_ONCE(netif_txq->trans_start);
286 
287 	netdev_err(ndev, "txq:%d DRV_XOFF:%d tmo:%u dql_avail:%d free_desc:%zu\n",
288 		   txqueue,
289 		   netif_tx_queue_stopped(netif_txq),
290 		   jiffies_to_msecs(jiffies - trans_start),
291 		   dql_avail(&netif_txq->dql),
292 		   k3_cppi_desc_pool_avail(tx_chn->desc_pool));
293 
294 	if (netif_tx_queue_stopped(netif_txq)) {
295 		/* try recover if stopped by us */
296 		txq_trans_update(netif_txq);
297 		netif_tx_wake_queue(netif_txq);
298 	}
299 }
300 
301 static int am65_cpsw_nuss_rx_push(struct am65_cpsw_common *common,
302 				  struct sk_buff *skb)
303 {
304 	struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
305 	struct cppi5_host_desc_t *desc_rx;
306 	struct device *dev = common->dev;
307 	u32 pkt_len = skb_tailroom(skb);
308 	dma_addr_t desc_dma;
309 	dma_addr_t buf_dma;
310 	void *swdata;
311 
312 	desc_rx = k3_cppi_desc_pool_alloc(rx_chn->desc_pool);
313 	if (!desc_rx) {
314 		dev_err(dev, "Failed to allocate RXFDQ descriptor\n");
315 		return -ENOMEM;
316 	}
317 	desc_dma = k3_cppi_desc_pool_virt2dma(rx_chn->desc_pool, desc_rx);
318 
319 	buf_dma = dma_map_single(rx_chn->dma_dev, skb->data, pkt_len,
320 				 DMA_FROM_DEVICE);
321 	if (unlikely(dma_mapping_error(rx_chn->dma_dev, buf_dma))) {
322 		k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx);
323 		dev_err(dev, "Failed to map rx skb buffer\n");
324 		return -EINVAL;
325 	}
326 
327 	cppi5_hdesc_init(desc_rx, CPPI5_INFO0_HDESC_EPIB_PRESENT,
328 			 AM65_CPSW_NAV_PS_DATA_SIZE);
329 	k3_udma_glue_rx_dma_to_cppi5_addr(rx_chn->rx_chn, &buf_dma);
330 	cppi5_hdesc_attach_buf(desc_rx, buf_dma, skb_tailroom(skb), buf_dma, skb_tailroom(skb));
331 	swdata = cppi5_hdesc_get_swdata(desc_rx);
332 	*((void **)swdata) = skb;
333 
334 	return k3_udma_glue_push_rx_chn(rx_chn->rx_chn, 0, desc_rx, desc_dma);
335 }
336 
337 void am65_cpsw_nuss_set_p0_ptype(struct am65_cpsw_common *common)
338 {
339 	struct am65_cpsw_host *host_p = am65_common_get_host(common);
340 	u32 val, pri_map;
341 
342 	/* P0 set Receive Priority Type */
343 	val = readl(host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL);
344 
345 	if (common->pf_p0_rx_ptype_rrobin) {
346 		val |= AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN;
347 		/* Enet Ports fifos works in fixed priority mode only, so
348 		 * reset P0_Rx_Pri_Map so all packet will go in Enet fifo 0
349 		 */
350 		pri_map = 0x0;
351 	} else {
352 		val &= ~AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN;
353 		/* restore P0_Rx_Pri_Map */
354 		pri_map = 0x76543210;
355 	}
356 
357 	writel(pri_map, host_p->port_base + AM65_CPSW_PORT_REG_RX_PRI_MAP);
358 	writel(val, host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL);
359 }
360 
361 static void am65_cpsw_init_host_port_switch(struct am65_cpsw_common *common);
362 static void am65_cpsw_init_host_port_emac(struct am65_cpsw_common *common);
363 static void am65_cpsw_init_port_switch_ale(struct am65_cpsw_port *port);
364 static void am65_cpsw_init_port_emac_ale(struct am65_cpsw_port *port);
365 
366 static int am65_cpsw_nuss_common_open(struct am65_cpsw_common *common)
367 {
368 	struct am65_cpsw_host *host_p = am65_common_get_host(common);
369 	int port_idx, i, ret;
370 	struct sk_buff *skb;
371 	u32 val, port_mask;
372 
373 	if (common->usage_count)
374 		return 0;
375 
376 	/* Control register */
377 	writel(AM65_CPSW_CTL_P0_ENABLE | AM65_CPSW_CTL_P0_TX_CRC_REMOVE |
378 	       AM65_CPSW_CTL_VLAN_AWARE | AM65_CPSW_CTL_P0_RX_PAD,
379 	       common->cpsw_base + AM65_CPSW_REG_CTL);
380 	/* Max length register */
381 	writel(AM65_CPSW_MAX_PACKET_SIZE,
382 	       host_p->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN);
383 	/* set base flow_id */
384 	writel(common->rx_flow_id_base,
385 	       host_p->port_base + AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET);
386 	/* en tx crc offload */
387 	writel(AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN, host_p->port_base + AM65_CPSW_P0_REG_CTL);
388 
389 	am65_cpsw_nuss_set_p0_ptype(common);
390 
391 	/* enable statistic */
392 	val = BIT(HOST_PORT_NUM);
393 	for (port_idx = 0; port_idx < common->port_num; port_idx++) {
394 		struct am65_cpsw_port *port = &common->ports[port_idx];
395 
396 		if (!port->disabled)
397 			val |=  BIT(port->port_id);
398 	}
399 	writel(val, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN);
400 
401 	/* disable priority elevation */
402 	writel(0, common->cpsw_base + AM65_CPSW_REG_PTYPE);
403 
404 	cpsw_ale_start(common->ale);
405 
406 	/* limit to one RX flow only */
407 	cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
408 			     ALE_DEFAULT_THREAD_ID, 0);
409 	cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
410 			     ALE_DEFAULT_THREAD_ENABLE, 1);
411 	/* switch to vlan unaware mode */
412 	cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_VLAN_AWARE, 1);
413 	cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
414 			     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
415 
416 	/* default vlan cfg: create mask based on enabled ports */
417 	port_mask = GENMASK(common->port_num, 0) &
418 		    ~common->disabled_ports_mask;
419 
420 	cpsw_ale_add_vlan(common->ale, 0, port_mask,
421 			  port_mask, port_mask,
422 			  port_mask & ~ALE_PORT_HOST);
423 
424 	if (common->is_emac_mode)
425 		am65_cpsw_init_host_port_emac(common);
426 	else
427 		am65_cpsw_init_host_port_switch(common);
428 
429 	for (i = 0; i < common->rx_chns.descs_num; i++) {
430 		skb = __netdev_alloc_skb_ip_align(NULL,
431 						  AM65_CPSW_MAX_PACKET_SIZE,
432 						  GFP_KERNEL);
433 		if (!skb) {
434 			dev_err(common->dev, "cannot allocate skb\n");
435 			return -ENOMEM;
436 		}
437 
438 		ret = am65_cpsw_nuss_rx_push(common, skb);
439 		if (ret < 0) {
440 			dev_err(common->dev,
441 				"cannot submit skb to channel rx, error %d\n",
442 				ret);
443 			kfree_skb(skb);
444 			return ret;
445 		}
446 		kmemleak_not_leak(skb);
447 	}
448 	k3_udma_glue_enable_rx_chn(common->rx_chns.rx_chn);
449 
450 	for (i = 0; i < common->tx_ch_num; i++) {
451 		ret = k3_udma_glue_enable_tx_chn(common->tx_chns[i].tx_chn);
452 		if (ret)
453 			return ret;
454 		napi_enable(&common->tx_chns[i].napi_tx);
455 	}
456 
457 	napi_enable(&common->napi_rx);
458 	if (common->rx_irq_disabled) {
459 		common->rx_irq_disabled = false;
460 		enable_irq(common->rx_chns.irq);
461 	}
462 
463 	dev_dbg(common->dev, "cpsw_nuss started\n");
464 	return 0;
465 }
466 
467 static void am65_cpsw_nuss_tx_cleanup(void *data, dma_addr_t desc_dma);
468 static void am65_cpsw_nuss_rx_cleanup(void *data, dma_addr_t desc_dma);
469 
470 static int am65_cpsw_nuss_common_stop(struct am65_cpsw_common *common)
471 {
472 	int i;
473 
474 	if (common->usage_count != 1)
475 		return 0;
476 
477 	cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
478 			     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
479 
480 	/* shutdown tx channels */
481 	atomic_set(&common->tdown_cnt, common->tx_ch_num);
482 	/* ensure new tdown_cnt value is visible */
483 	smp_mb__after_atomic();
484 	reinit_completion(&common->tdown_complete);
485 
486 	for (i = 0; i < common->tx_ch_num; i++)
487 		k3_udma_glue_tdown_tx_chn(common->tx_chns[i].tx_chn, false);
488 
489 	i = wait_for_completion_timeout(&common->tdown_complete,
490 					msecs_to_jiffies(1000));
491 	if (!i)
492 		dev_err(common->dev, "tx timeout\n");
493 	for (i = 0; i < common->tx_ch_num; i++)
494 		napi_disable(&common->tx_chns[i].napi_tx);
495 
496 	for (i = 0; i < common->tx_ch_num; i++) {
497 		k3_udma_glue_reset_tx_chn(common->tx_chns[i].tx_chn,
498 					  &common->tx_chns[i],
499 					  am65_cpsw_nuss_tx_cleanup);
500 		k3_udma_glue_disable_tx_chn(common->tx_chns[i].tx_chn);
501 	}
502 
503 	k3_udma_glue_tdown_rx_chn(common->rx_chns.rx_chn, true);
504 	napi_disable(&common->napi_rx);
505 
506 	for (i = 0; i < AM65_CPSW_MAX_RX_FLOWS; i++)
507 		k3_udma_glue_reset_rx_chn(common->rx_chns.rx_chn, i,
508 					  &common->rx_chns,
509 					  am65_cpsw_nuss_rx_cleanup, !!i);
510 
511 	k3_udma_glue_disable_rx_chn(common->rx_chns.rx_chn);
512 
513 	cpsw_ale_stop(common->ale);
514 
515 	writel(0, common->cpsw_base + AM65_CPSW_REG_CTL);
516 	writel(0, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN);
517 
518 	dev_dbg(common->dev, "cpsw_nuss stopped\n");
519 	return 0;
520 }
521 
522 static int am65_cpsw_nuss_ndo_slave_stop(struct net_device *ndev)
523 {
524 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
525 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
526 	int ret;
527 
528 	phylink_stop(port->slave.phylink);
529 
530 	netif_tx_stop_all_queues(ndev);
531 
532 	phylink_disconnect_phy(port->slave.phylink);
533 
534 	ret = am65_cpsw_nuss_common_stop(common);
535 	if (ret)
536 		return ret;
537 
538 	common->usage_count--;
539 	pm_runtime_put(common->dev);
540 	return 0;
541 }
542 
543 static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg)
544 {
545 	struct am65_cpsw_port *port = arg;
546 
547 	if (!vdev)
548 		return 0;
549 
550 	return am65_cpsw_nuss_ndo_slave_add_vid(port->ndev, 0, vid);
551 }
552 
553 static int am65_cpsw_nuss_ndo_slave_open(struct net_device *ndev)
554 {
555 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
556 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
557 	int ret, i;
558 
559 	ret = pm_runtime_resume_and_get(common->dev);
560 	if (ret < 0)
561 		return ret;
562 
563 	/* Notify the stack of the actual queue counts. */
564 	ret = netif_set_real_num_tx_queues(ndev, common->tx_ch_num);
565 	if (ret) {
566 		dev_err(common->dev, "cannot set real number of tx queues\n");
567 		return ret;
568 	}
569 
570 	ret = netif_set_real_num_rx_queues(ndev, AM65_CPSW_MAX_RX_QUEUES);
571 	if (ret) {
572 		dev_err(common->dev, "cannot set real number of rx queues\n");
573 		return ret;
574 	}
575 
576 	for (i = 0; i < common->tx_ch_num; i++)
577 		netdev_tx_reset_queue(netdev_get_tx_queue(ndev, i));
578 
579 	ret = am65_cpsw_nuss_common_open(common);
580 	if (ret)
581 		return ret;
582 
583 	common->usage_count++;
584 
585 	am65_cpsw_port_set_sl_mac(port, ndev->dev_addr);
586 
587 	if (common->is_emac_mode)
588 		am65_cpsw_init_port_emac_ale(port);
589 	else
590 		am65_cpsw_init_port_switch_ale(port);
591 
592 	/* mac_sl should be configured via phy-link interface */
593 	am65_cpsw_sl_ctl_reset(port);
594 
595 	ret = phylink_of_phy_connect(port->slave.phylink, port->slave.phy_node, 0);
596 	if (ret)
597 		goto error_cleanup;
598 
599 	/* restore vlan configurations */
600 	vlan_for_each(ndev, cpsw_restore_vlans, port);
601 
602 	phylink_start(port->slave.phylink);
603 
604 	return 0;
605 
606 error_cleanup:
607 	am65_cpsw_nuss_ndo_slave_stop(ndev);
608 	return ret;
609 }
610 
611 static void am65_cpsw_nuss_rx_cleanup(void *data, dma_addr_t desc_dma)
612 {
613 	struct am65_cpsw_rx_chn *rx_chn = data;
614 	struct cppi5_host_desc_t *desc_rx;
615 	struct sk_buff *skb;
616 	dma_addr_t buf_dma;
617 	u32 buf_dma_len;
618 	void **swdata;
619 
620 	desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma);
621 	swdata = cppi5_hdesc_get_swdata(desc_rx);
622 	skb = *swdata;
623 	cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len);
624 	k3_udma_glue_rx_cppi5_to_dma_addr(rx_chn->rx_chn, &buf_dma);
625 
626 	dma_unmap_single(rx_chn->dma_dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE);
627 	k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx);
628 
629 	dev_kfree_skb_any(skb);
630 }
631 
632 static void am65_cpsw_nuss_rx_ts(struct sk_buff *skb, u32 *psdata)
633 {
634 	struct skb_shared_hwtstamps *ssh;
635 	u64 ns;
636 
637 	ns = ((u64)psdata[1] << 32) | psdata[0];
638 
639 	ssh = skb_hwtstamps(skb);
640 	memset(ssh, 0, sizeof(*ssh));
641 	ssh->hwtstamp = ns_to_ktime(ns);
642 }
643 
644 /* RX psdata[2] word format - checksum information */
645 #define AM65_CPSW_RX_PSD_CSUM_ADD	GENMASK(15, 0)
646 #define AM65_CPSW_RX_PSD_CSUM_ERR	BIT(16)
647 #define AM65_CPSW_RX_PSD_IS_FRAGMENT	BIT(17)
648 #define AM65_CPSW_RX_PSD_IS_TCP		BIT(18)
649 #define AM65_CPSW_RX_PSD_IPV6_VALID	BIT(19)
650 #define AM65_CPSW_RX_PSD_IPV4_VALID	BIT(20)
651 
652 static void am65_cpsw_nuss_rx_csum(struct sk_buff *skb, u32 csum_info)
653 {
654 	/* HW can verify IPv4/IPv6 TCP/UDP packets checksum
655 	 * csum information provides in psdata[2] word:
656 	 * AM65_CPSW_RX_PSD_CSUM_ERR bit - indicates csum error
657 	 * AM65_CPSW_RX_PSD_IPV6_VALID and AM65_CPSW_RX_PSD_IPV4_VALID
658 	 * bits - indicates IPv4/IPv6 packet
659 	 * AM65_CPSW_RX_PSD_IS_FRAGMENT bit - indicates fragmented packet
660 	 * AM65_CPSW_RX_PSD_CSUM_ADD has value 0xFFFF for non fragmented packets
661 	 * or csum value for fragmented packets if !AM65_CPSW_RX_PSD_CSUM_ERR
662 	 */
663 	skb_checksum_none_assert(skb);
664 
665 	if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM)))
666 		return;
667 
668 	if ((csum_info & (AM65_CPSW_RX_PSD_IPV6_VALID |
669 			  AM65_CPSW_RX_PSD_IPV4_VALID)) &&
670 			  !(csum_info & AM65_CPSW_RX_PSD_CSUM_ERR)) {
671 		/* csum for fragmented packets is unsupported */
672 		if (!(csum_info & AM65_CPSW_RX_PSD_IS_FRAGMENT))
673 			skb->ip_summed = CHECKSUM_UNNECESSARY;
674 	}
675 }
676 
677 static int am65_cpsw_nuss_rx_packets(struct am65_cpsw_common *common,
678 				     u32 flow_idx)
679 {
680 	struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
681 	u32 buf_dma_len, pkt_len, port_id = 0, csum_info;
682 	struct am65_cpsw_ndev_priv *ndev_priv;
683 	struct am65_cpsw_ndev_stats *stats;
684 	struct cppi5_host_desc_t *desc_rx;
685 	struct device *dev = common->dev;
686 	struct sk_buff *skb, *new_skb;
687 	dma_addr_t desc_dma, buf_dma;
688 	struct am65_cpsw_port *port;
689 	struct net_device *ndev;
690 	void **swdata;
691 	u32 *psdata;
692 	int ret = 0;
693 
694 	ret = k3_udma_glue_pop_rx_chn(rx_chn->rx_chn, flow_idx, &desc_dma);
695 	if (ret) {
696 		if (ret != -ENODATA)
697 			dev_err(dev, "RX: pop chn fail %d\n", ret);
698 		return ret;
699 	}
700 
701 	if (cppi5_desc_is_tdcm(desc_dma)) {
702 		dev_dbg(dev, "%s RX tdown flow: %u\n", __func__, flow_idx);
703 		return 0;
704 	}
705 
706 	desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma);
707 	dev_dbg(dev, "%s flow_idx: %u desc %pad\n",
708 		__func__, flow_idx, &desc_dma);
709 
710 	swdata = cppi5_hdesc_get_swdata(desc_rx);
711 	skb = *swdata;
712 	cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len);
713 	k3_udma_glue_rx_cppi5_to_dma_addr(rx_chn->rx_chn, &buf_dma);
714 	pkt_len = cppi5_hdesc_get_pktlen(desc_rx);
715 	cppi5_desc_get_tags_ids(&desc_rx->hdr, &port_id, NULL);
716 	dev_dbg(dev, "%s rx port_id:%d\n", __func__, port_id);
717 	port = am65_common_get_port(common, port_id);
718 	ndev = port->ndev;
719 	skb->dev = ndev;
720 
721 	psdata = cppi5_hdesc_get_psdata(desc_rx);
722 	/* add RX timestamp */
723 	if (port->rx_ts_enabled)
724 		am65_cpsw_nuss_rx_ts(skb, psdata);
725 	csum_info = psdata[2];
726 	dev_dbg(dev, "%s rx csum_info:%#x\n", __func__, csum_info);
727 
728 	dma_unmap_single(rx_chn->dma_dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE);
729 
730 	k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx);
731 
732 	new_skb = netdev_alloc_skb_ip_align(ndev, AM65_CPSW_MAX_PACKET_SIZE);
733 	if (new_skb) {
734 		ndev_priv = netdev_priv(ndev);
735 		am65_cpsw_nuss_set_offload_fwd_mark(skb, ndev_priv->offload_fwd_mark);
736 		skb_put(skb, pkt_len);
737 		skb->protocol = eth_type_trans(skb, ndev);
738 		am65_cpsw_nuss_rx_csum(skb, csum_info);
739 		napi_gro_receive(&common->napi_rx, skb);
740 
741 		stats = this_cpu_ptr(ndev_priv->stats);
742 
743 		u64_stats_update_begin(&stats->syncp);
744 		stats->rx_packets++;
745 		stats->rx_bytes += pkt_len;
746 		u64_stats_update_end(&stats->syncp);
747 		kmemleak_not_leak(new_skb);
748 	} else {
749 		ndev->stats.rx_dropped++;
750 		new_skb = skb;
751 	}
752 
753 	if (netif_dormant(ndev)) {
754 		dev_kfree_skb_any(new_skb);
755 		ndev->stats.rx_dropped++;
756 		return 0;
757 	}
758 
759 	ret = am65_cpsw_nuss_rx_push(common, new_skb);
760 	if (WARN_ON(ret < 0)) {
761 		dev_kfree_skb_any(new_skb);
762 		ndev->stats.rx_errors++;
763 		ndev->stats.rx_dropped++;
764 	}
765 
766 	return ret;
767 }
768 
769 static int am65_cpsw_nuss_rx_poll(struct napi_struct *napi_rx, int budget)
770 {
771 	struct am65_cpsw_common *common = am65_cpsw_napi_to_common(napi_rx);
772 	int flow = AM65_CPSW_MAX_RX_FLOWS;
773 	int cur_budget, ret;
774 	int num_rx = 0;
775 
776 	/* process every flow */
777 	while (flow--) {
778 		cur_budget = budget - num_rx;
779 
780 		while (cur_budget--) {
781 			ret = am65_cpsw_nuss_rx_packets(common, flow);
782 			if (ret)
783 				break;
784 			num_rx++;
785 		}
786 
787 		if (num_rx >= budget)
788 			break;
789 	}
790 
791 	dev_dbg(common->dev, "%s num_rx:%d %d\n", __func__, num_rx, budget);
792 
793 	if (num_rx < budget && napi_complete_done(napi_rx, num_rx)) {
794 		if (common->rx_irq_disabled) {
795 			common->rx_irq_disabled = false;
796 			enable_irq(common->rx_chns.irq);
797 		}
798 	}
799 
800 	return num_rx;
801 }
802 
803 static void am65_cpsw_nuss_xmit_free(struct am65_cpsw_tx_chn *tx_chn,
804 				     struct cppi5_host_desc_t *desc)
805 {
806 	struct cppi5_host_desc_t *first_desc, *next_desc;
807 	dma_addr_t buf_dma, next_desc_dma;
808 	u32 buf_dma_len;
809 
810 	first_desc = desc;
811 	next_desc = first_desc;
812 
813 	cppi5_hdesc_get_obuf(first_desc, &buf_dma, &buf_dma_len);
814 	k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &buf_dma);
815 
816 	dma_unmap_single(tx_chn->dma_dev, buf_dma, buf_dma_len, DMA_TO_DEVICE);
817 
818 	next_desc_dma = cppi5_hdesc_get_next_hbdesc(first_desc);
819 	k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &next_desc_dma);
820 	while (next_desc_dma) {
821 		next_desc = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool,
822 						       next_desc_dma);
823 		cppi5_hdesc_get_obuf(next_desc, &buf_dma, &buf_dma_len);
824 		k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &buf_dma);
825 
826 		dma_unmap_page(tx_chn->dma_dev, buf_dma, buf_dma_len,
827 			       DMA_TO_DEVICE);
828 
829 		next_desc_dma = cppi5_hdesc_get_next_hbdesc(next_desc);
830 		k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &next_desc_dma);
831 
832 		k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc);
833 	}
834 
835 	k3_cppi_desc_pool_free(tx_chn->desc_pool, first_desc);
836 }
837 
838 static void am65_cpsw_nuss_tx_cleanup(void *data, dma_addr_t desc_dma)
839 {
840 	struct am65_cpsw_tx_chn *tx_chn = data;
841 	struct cppi5_host_desc_t *desc_tx;
842 	struct sk_buff *skb;
843 	void **swdata;
844 
845 	desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool, desc_dma);
846 	swdata = cppi5_hdesc_get_swdata(desc_tx);
847 	skb = *(swdata);
848 	am65_cpsw_nuss_xmit_free(tx_chn, desc_tx);
849 
850 	dev_kfree_skb_any(skb);
851 }
852 
853 static struct sk_buff *
854 am65_cpsw_nuss_tx_compl_packet(struct am65_cpsw_tx_chn *tx_chn,
855 			       dma_addr_t desc_dma)
856 {
857 	struct am65_cpsw_ndev_priv *ndev_priv;
858 	struct am65_cpsw_ndev_stats *stats;
859 	struct cppi5_host_desc_t *desc_tx;
860 	struct net_device *ndev;
861 	struct sk_buff *skb;
862 	void **swdata;
863 
864 	desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool,
865 					     desc_dma);
866 	swdata = cppi5_hdesc_get_swdata(desc_tx);
867 	skb = *(swdata);
868 	am65_cpsw_nuss_xmit_free(tx_chn, desc_tx);
869 
870 	ndev = skb->dev;
871 
872 	am65_cpts_tx_timestamp(tx_chn->common->cpts, skb);
873 
874 	ndev_priv = netdev_priv(ndev);
875 	stats = this_cpu_ptr(ndev_priv->stats);
876 	u64_stats_update_begin(&stats->syncp);
877 	stats->tx_packets++;
878 	stats->tx_bytes += skb->len;
879 	u64_stats_update_end(&stats->syncp);
880 
881 	return skb;
882 }
883 
884 static void am65_cpsw_nuss_tx_wake(struct am65_cpsw_tx_chn *tx_chn, struct net_device *ndev,
885 				   struct netdev_queue *netif_txq)
886 {
887 	if (netif_tx_queue_stopped(netif_txq)) {
888 		/* Check whether the queue is stopped due to stalled
889 		 * tx dma, if the queue is stopped then wake the queue
890 		 * as we have free desc for tx
891 		 */
892 		__netif_tx_lock(netif_txq, smp_processor_id());
893 		if (netif_running(ndev) &&
894 		    (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >= MAX_SKB_FRAGS))
895 			netif_tx_wake_queue(netif_txq);
896 
897 		__netif_tx_unlock(netif_txq);
898 	}
899 }
900 
901 static int am65_cpsw_nuss_tx_compl_packets(struct am65_cpsw_common *common,
902 					   int chn, unsigned int budget)
903 {
904 	struct device *dev = common->dev;
905 	struct am65_cpsw_tx_chn *tx_chn;
906 	struct netdev_queue *netif_txq;
907 	unsigned int total_bytes = 0;
908 	struct net_device *ndev;
909 	struct sk_buff *skb;
910 	dma_addr_t desc_dma;
911 	int res, num_tx = 0;
912 
913 	tx_chn = &common->tx_chns[chn];
914 
915 	while (true) {
916 		spin_lock(&tx_chn->lock);
917 		res = k3_udma_glue_pop_tx_chn(tx_chn->tx_chn, &desc_dma);
918 		spin_unlock(&tx_chn->lock);
919 		if (res == -ENODATA)
920 			break;
921 
922 		if (cppi5_desc_is_tdcm(desc_dma)) {
923 			if (atomic_dec_and_test(&common->tdown_cnt))
924 				complete(&common->tdown_complete);
925 			break;
926 		}
927 
928 		skb = am65_cpsw_nuss_tx_compl_packet(tx_chn, desc_dma);
929 		total_bytes = skb->len;
930 		ndev = skb->dev;
931 		napi_consume_skb(skb, budget);
932 		num_tx++;
933 
934 		netif_txq = netdev_get_tx_queue(ndev, chn);
935 
936 		netdev_tx_completed_queue(netif_txq, num_tx, total_bytes);
937 
938 		am65_cpsw_nuss_tx_wake(tx_chn, ndev, netif_txq);
939 	}
940 
941 	dev_dbg(dev, "%s:%u pkt:%d\n", __func__, chn, num_tx);
942 
943 	return num_tx;
944 }
945 
946 static int am65_cpsw_nuss_tx_compl_packets_2g(struct am65_cpsw_common *common,
947 					      int chn, unsigned int budget)
948 {
949 	struct device *dev = common->dev;
950 	struct am65_cpsw_tx_chn *tx_chn;
951 	struct netdev_queue *netif_txq;
952 	unsigned int total_bytes = 0;
953 	struct net_device *ndev;
954 	struct sk_buff *skb;
955 	dma_addr_t desc_dma;
956 	int res, num_tx = 0;
957 
958 	tx_chn = &common->tx_chns[chn];
959 
960 	while (true) {
961 		res = k3_udma_glue_pop_tx_chn(tx_chn->tx_chn, &desc_dma);
962 		if (res == -ENODATA)
963 			break;
964 
965 		if (cppi5_desc_is_tdcm(desc_dma)) {
966 			if (atomic_dec_and_test(&common->tdown_cnt))
967 				complete(&common->tdown_complete);
968 			break;
969 		}
970 
971 		skb = am65_cpsw_nuss_tx_compl_packet(tx_chn, desc_dma);
972 
973 		ndev = skb->dev;
974 		total_bytes += skb->len;
975 		napi_consume_skb(skb, budget);
976 		num_tx++;
977 	}
978 
979 	if (!num_tx)
980 		return 0;
981 
982 	netif_txq = netdev_get_tx_queue(ndev, chn);
983 
984 	netdev_tx_completed_queue(netif_txq, num_tx, total_bytes);
985 
986 	am65_cpsw_nuss_tx_wake(tx_chn, ndev, netif_txq);
987 
988 	dev_dbg(dev, "%s:%u pkt:%d\n", __func__, chn, num_tx);
989 
990 	return num_tx;
991 }
992 
993 static int am65_cpsw_nuss_tx_poll(struct napi_struct *napi_tx, int budget)
994 {
995 	struct am65_cpsw_tx_chn *tx_chn = am65_cpsw_napi_to_tx_chn(napi_tx);
996 	int num_tx;
997 
998 	if (AM65_CPSW_IS_CPSW2G(tx_chn->common))
999 		num_tx = am65_cpsw_nuss_tx_compl_packets_2g(tx_chn->common, tx_chn->id, budget);
1000 	else
1001 		num_tx = am65_cpsw_nuss_tx_compl_packets(tx_chn->common, tx_chn->id, budget);
1002 
1003 	if (num_tx >= budget)
1004 		return budget;
1005 
1006 	if (napi_complete_done(napi_tx, num_tx))
1007 		enable_irq(tx_chn->irq);
1008 
1009 	return 0;
1010 }
1011 
1012 static irqreturn_t am65_cpsw_nuss_rx_irq(int irq, void *dev_id)
1013 {
1014 	struct am65_cpsw_common *common = dev_id;
1015 
1016 	common->rx_irq_disabled = true;
1017 	disable_irq_nosync(irq);
1018 	napi_schedule(&common->napi_rx);
1019 
1020 	return IRQ_HANDLED;
1021 }
1022 
1023 static irqreturn_t am65_cpsw_nuss_tx_irq(int irq, void *dev_id)
1024 {
1025 	struct am65_cpsw_tx_chn *tx_chn = dev_id;
1026 
1027 	disable_irq_nosync(irq);
1028 	napi_schedule(&tx_chn->napi_tx);
1029 
1030 	return IRQ_HANDLED;
1031 }
1032 
1033 static netdev_tx_t am65_cpsw_nuss_ndo_slave_xmit(struct sk_buff *skb,
1034 						 struct net_device *ndev)
1035 {
1036 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
1037 	struct cppi5_host_desc_t *first_desc, *next_desc, *cur_desc;
1038 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1039 	struct device *dev = common->dev;
1040 	struct am65_cpsw_tx_chn *tx_chn;
1041 	struct netdev_queue *netif_txq;
1042 	dma_addr_t desc_dma, buf_dma;
1043 	int ret, q_idx, i;
1044 	void **swdata;
1045 	u32 *psdata;
1046 	u32 pkt_len;
1047 
1048 	/* padding enabled in hw */
1049 	pkt_len = skb_headlen(skb);
1050 
1051 	/* SKB TX timestamp */
1052 	if (port->tx_ts_enabled)
1053 		am65_cpts_prep_tx_timestamp(common->cpts, skb);
1054 
1055 	q_idx = skb_get_queue_mapping(skb);
1056 	dev_dbg(dev, "%s skb_queue:%d\n", __func__, q_idx);
1057 
1058 	tx_chn = &common->tx_chns[q_idx];
1059 	netif_txq = netdev_get_tx_queue(ndev, q_idx);
1060 
1061 	/* Map the linear buffer */
1062 	buf_dma = dma_map_single(tx_chn->dma_dev, skb->data, pkt_len,
1063 				 DMA_TO_DEVICE);
1064 	if (unlikely(dma_mapping_error(tx_chn->dma_dev, buf_dma))) {
1065 		dev_err(dev, "Failed to map tx skb buffer\n");
1066 		ndev->stats.tx_errors++;
1067 		goto err_free_skb;
1068 	}
1069 
1070 	first_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool);
1071 	if (!first_desc) {
1072 		dev_dbg(dev, "Failed to allocate descriptor\n");
1073 		dma_unmap_single(tx_chn->dma_dev, buf_dma, pkt_len,
1074 				 DMA_TO_DEVICE);
1075 		goto busy_stop_q;
1076 	}
1077 
1078 	cppi5_hdesc_init(first_desc, CPPI5_INFO0_HDESC_EPIB_PRESENT,
1079 			 AM65_CPSW_NAV_PS_DATA_SIZE);
1080 	cppi5_desc_set_pktids(&first_desc->hdr, 0, 0x3FFF);
1081 	cppi5_hdesc_set_pkttype(first_desc, 0x7);
1082 	cppi5_desc_set_tags_ids(&first_desc->hdr, 0, port->port_id);
1083 
1084 	k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &buf_dma);
1085 	cppi5_hdesc_attach_buf(first_desc, buf_dma, pkt_len, buf_dma, pkt_len);
1086 	swdata = cppi5_hdesc_get_swdata(first_desc);
1087 	*(swdata) = skb;
1088 	psdata = cppi5_hdesc_get_psdata(first_desc);
1089 
1090 	/* HW csum offload if enabled */
1091 	psdata[2] = 0;
1092 	if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
1093 		unsigned int cs_start, cs_offset;
1094 
1095 		cs_start = skb_transport_offset(skb);
1096 		cs_offset = cs_start + skb->csum_offset;
1097 		/* HW numerates bytes starting from 1 */
1098 		psdata[2] = ((cs_offset + 1) << 24) |
1099 			    ((cs_start + 1) << 16) | (skb->len - cs_start);
1100 		dev_dbg(dev, "%s tx psdata:%#x\n", __func__, psdata[2]);
1101 	}
1102 
1103 	if (!skb_is_nonlinear(skb))
1104 		goto done_tx;
1105 
1106 	dev_dbg(dev, "fragmented SKB\n");
1107 
1108 	/* Handle the case where skb is fragmented in pages */
1109 	cur_desc = first_desc;
1110 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1111 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1112 		u32 frag_size = skb_frag_size(frag);
1113 
1114 		next_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool);
1115 		if (!next_desc) {
1116 			dev_err(dev, "Failed to allocate descriptor\n");
1117 			goto busy_free_descs;
1118 		}
1119 
1120 		buf_dma = skb_frag_dma_map(tx_chn->dma_dev, frag, 0, frag_size,
1121 					   DMA_TO_DEVICE);
1122 		if (unlikely(dma_mapping_error(tx_chn->dma_dev, buf_dma))) {
1123 			dev_err(dev, "Failed to map tx skb page\n");
1124 			k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc);
1125 			ndev->stats.tx_errors++;
1126 			goto err_free_descs;
1127 		}
1128 
1129 		cppi5_hdesc_reset_hbdesc(next_desc);
1130 		k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &buf_dma);
1131 		cppi5_hdesc_attach_buf(next_desc,
1132 				       buf_dma, frag_size, buf_dma, frag_size);
1133 
1134 		desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool,
1135 						      next_desc);
1136 		k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &desc_dma);
1137 		cppi5_hdesc_link_hbdesc(cur_desc, desc_dma);
1138 
1139 		pkt_len += frag_size;
1140 		cur_desc = next_desc;
1141 	}
1142 	WARN_ON(pkt_len != skb->len);
1143 
1144 done_tx:
1145 	skb_tx_timestamp(skb);
1146 
1147 	/* report bql before sending packet */
1148 	netdev_tx_sent_queue(netif_txq, pkt_len);
1149 
1150 	cppi5_hdesc_set_pktlen(first_desc, pkt_len);
1151 	desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool, first_desc);
1152 	if (AM65_CPSW_IS_CPSW2G(common)) {
1153 		ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, first_desc, desc_dma);
1154 	} else {
1155 		spin_lock_bh(&tx_chn->lock);
1156 		ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, first_desc, desc_dma);
1157 		spin_unlock_bh(&tx_chn->lock);
1158 	}
1159 	if (ret) {
1160 		dev_err(dev, "can't push desc %d\n", ret);
1161 		/* inform bql */
1162 		netdev_tx_completed_queue(netif_txq, 1, pkt_len);
1163 		ndev->stats.tx_errors++;
1164 		goto err_free_descs;
1165 	}
1166 
1167 	if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) < MAX_SKB_FRAGS) {
1168 		netif_tx_stop_queue(netif_txq);
1169 		/* Barrier, so that stop_queue visible to other cpus */
1170 		smp_mb__after_atomic();
1171 		dev_dbg(dev, "netif_tx_stop_queue %d\n", q_idx);
1172 
1173 		/* re-check for smp */
1174 		if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >=
1175 		    MAX_SKB_FRAGS) {
1176 			netif_tx_wake_queue(netif_txq);
1177 			dev_dbg(dev, "netif_tx_wake_queue %d\n", q_idx);
1178 		}
1179 	}
1180 
1181 	return NETDEV_TX_OK;
1182 
1183 err_free_descs:
1184 	am65_cpsw_nuss_xmit_free(tx_chn, first_desc);
1185 err_free_skb:
1186 	ndev->stats.tx_dropped++;
1187 	dev_kfree_skb_any(skb);
1188 	return NETDEV_TX_OK;
1189 
1190 busy_free_descs:
1191 	am65_cpsw_nuss_xmit_free(tx_chn, first_desc);
1192 busy_stop_q:
1193 	netif_tx_stop_queue(netif_txq);
1194 	return NETDEV_TX_BUSY;
1195 }
1196 
1197 static int am65_cpsw_nuss_ndo_slave_set_mac_address(struct net_device *ndev,
1198 						    void *addr)
1199 {
1200 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
1201 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1202 	struct sockaddr *sockaddr = (struct sockaddr *)addr;
1203 	int ret;
1204 
1205 	ret = eth_prepare_mac_addr_change(ndev, addr);
1206 	if (ret < 0)
1207 		return ret;
1208 
1209 	ret = pm_runtime_resume_and_get(common->dev);
1210 	if (ret < 0)
1211 		return ret;
1212 
1213 	cpsw_ale_del_ucast(common->ale, ndev->dev_addr,
1214 			   HOST_PORT_NUM, 0, 0);
1215 	cpsw_ale_add_ucast(common->ale, sockaddr->sa_data,
1216 			   HOST_PORT_NUM, ALE_SECURE, 0);
1217 
1218 	am65_cpsw_port_set_sl_mac(port, addr);
1219 	eth_commit_mac_addr_change(ndev, sockaddr);
1220 
1221 	pm_runtime_put(common->dev);
1222 
1223 	return 0;
1224 }
1225 
1226 static int am65_cpsw_nuss_hwtstamp_set(struct net_device *ndev,
1227 				       struct ifreq *ifr)
1228 {
1229 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
1230 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1231 	u32 ts_ctrl, seq_id, ts_ctrl_ltype2, ts_vlan_ltype;
1232 	struct hwtstamp_config cfg;
1233 
1234 	if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS))
1235 		return -EOPNOTSUPP;
1236 
1237 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1238 		return -EFAULT;
1239 
1240 	/* TX HW timestamp */
1241 	switch (cfg.tx_type) {
1242 	case HWTSTAMP_TX_OFF:
1243 	case HWTSTAMP_TX_ON:
1244 		break;
1245 	default:
1246 		return -ERANGE;
1247 	}
1248 
1249 	switch (cfg.rx_filter) {
1250 	case HWTSTAMP_FILTER_NONE:
1251 		port->rx_ts_enabled = false;
1252 		break;
1253 	case HWTSTAMP_FILTER_ALL:
1254 	case HWTSTAMP_FILTER_SOME:
1255 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1256 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1257 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1258 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1259 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1260 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1261 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1262 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1263 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1264 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
1265 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
1266 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1267 	case HWTSTAMP_FILTER_NTP_ALL:
1268 		port->rx_ts_enabled = true;
1269 		cfg.rx_filter = HWTSTAMP_FILTER_ALL;
1270 		break;
1271 	default:
1272 		return -ERANGE;
1273 	}
1274 
1275 	port->tx_ts_enabled = (cfg.tx_type == HWTSTAMP_TX_ON);
1276 
1277 	/* cfg TX timestamp */
1278 	seq_id = (AM65_CPSW_TS_SEQ_ID_OFFSET <<
1279 		  AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT) | ETH_P_1588;
1280 
1281 	ts_vlan_ltype = ETH_P_8021Q;
1282 
1283 	ts_ctrl_ltype2 = ETH_P_1588 |
1284 			 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107 |
1285 			 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129 |
1286 			 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130 |
1287 			 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131 |
1288 			 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132 |
1289 			 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319 |
1290 			 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320 |
1291 			 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO;
1292 
1293 	ts_ctrl = AM65_CPSW_TS_EVENT_MSG_TYPE_BITS <<
1294 		  AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT;
1295 
1296 	if (port->tx_ts_enabled)
1297 		ts_ctrl |= AM65_CPSW_TS_TX_ANX_ALL_EN |
1298 			   AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN;
1299 
1300 	writel(seq_id, port->port_base + AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG);
1301 	writel(ts_vlan_ltype, port->port_base +
1302 	       AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG);
1303 	writel(ts_ctrl_ltype2, port->port_base +
1304 	       AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2);
1305 	writel(ts_ctrl, port->port_base + AM65_CPSW_PORTN_REG_TS_CTL);
1306 
1307 	/* en/dis RX timestamp */
1308 	am65_cpts_rx_enable(common->cpts, port->rx_ts_enabled);
1309 
1310 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1311 }
1312 
1313 static int am65_cpsw_nuss_hwtstamp_get(struct net_device *ndev,
1314 				       struct ifreq *ifr)
1315 {
1316 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1317 	struct hwtstamp_config cfg;
1318 
1319 	if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS))
1320 		return -EOPNOTSUPP;
1321 
1322 	cfg.flags = 0;
1323 	cfg.tx_type = port->tx_ts_enabled ?
1324 		      HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
1325 	cfg.rx_filter = port->rx_ts_enabled ?
1326 			HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE;
1327 
1328 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1329 }
1330 
1331 static int am65_cpsw_nuss_ndo_slave_ioctl(struct net_device *ndev,
1332 					  struct ifreq *req, int cmd)
1333 {
1334 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1335 
1336 	if (!netif_running(ndev))
1337 		return -EINVAL;
1338 
1339 	switch (cmd) {
1340 	case SIOCSHWTSTAMP:
1341 		return am65_cpsw_nuss_hwtstamp_set(ndev, req);
1342 	case SIOCGHWTSTAMP:
1343 		return am65_cpsw_nuss_hwtstamp_get(ndev, req);
1344 	}
1345 
1346 	return phylink_mii_ioctl(port->slave.phylink, req, cmd);
1347 }
1348 
1349 static void am65_cpsw_nuss_ndo_get_stats(struct net_device *dev,
1350 					 struct rtnl_link_stats64 *stats)
1351 {
1352 	struct am65_cpsw_ndev_priv *ndev_priv = netdev_priv(dev);
1353 	unsigned int start;
1354 	int cpu;
1355 
1356 	for_each_possible_cpu(cpu) {
1357 		struct am65_cpsw_ndev_stats *cpu_stats;
1358 		u64 rx_packets;
1359 		u64 rx_bytes;
1360 		u64 tx_packets;
1361 		u64 tx_bytes;
1362 
1363 		cpu_stats = per_cpu_ptr(ndev_priv->stats, cpu);
1364 		do {
1365 			start = u64_stats_fetch_begin(&cpu_stats->syncp);
1366 			rx_packets = cpu_stats->rx_packets;
1367 			rx_bytes   = cpu_stats->rx_bytes;
1368 			tx_packets = cpu_stats->tx_packets;
1369 			tx_bytes   = cpu_stats->tx_bytes;
1370 		} while (u64_stats_fetch_retry(&cpu_stats->syncp, start));
1371 
1372 		stats->rx_packets += rx_packets;
1373 		stats->rx_bytes   += rx_bytes;
1374 		stats->tx_packets += tx_packets;
1375 		stats->tx_bytes   += tx_bytes;
1376 	}
1377 
1378 	stats->rx_errors	= dev->stats.rx_errors;
1379 	stats->rx_dropped	= dev->stats.rx_dropped;
1380 	stats->tx_dropped	= dev->stats.tx_dropped;
1381 }
1382 
1383 static const struct net_device_ops am65_cpsw_nuss_netdev_ops = {
1384 	.ndo_open		= am65_cpsw_nuss_ndo_slave_open,
1385 	.ndo_stop		= am65_cpsw_nuss_ndo_slave_stop,
1386 	.ndo_start_xmit		= am65_cpsw_nuss_ndo_slave_xmit,
1387 	.ndo_set_rx_mode	= am65_cpsw_nuss_ndo_slave_set_rx_mode,
1388 	.ndo_get_stats64        = am65_cpsw_nuss_ndo_get_stats,
1389 	.ndo_validate_addr	= eth_validate_addr,
1390 	.ndo_set_mac_address	= am65_cpsw_nuss_ndo_slave_set_mac_address,
1391 	.ndo_tx_timeout		= am65_cpsw_nuss_ndo_host_tx_timeout,
1392 	.ndo_vlan_rx_add_vid	= am65_cpsw_nuss_ndo_slave_add_vid,
1393 	.ndo_vlan_rx_kill_vid	= am65_cpsw_nuss_ndo_slave_kill_vid,
1394 	.ndo_eth_ioctl		= am65_cpsw_nuss_ndo_slave_ioctl,
1395 	.ndo_setup_tc           = am65_cpsw_qos_ndo_setup_tc,
1396 };
1397 
1398 static void am65_cpsw_nuss_mac_config(struct phylink_config *config, unsigned int mode,
1399 				      const struct phylink_link_state *state)
1400 {
1401 	struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data,
1402 							  phylink_config);
1403 	struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave);
1404 	struct am65_cpsw_common *common = port->common;
1405 
1406 	if (common->pdata.extra_modes & BIT(state->interface))
1407 		writel(AM65_CPSW_SGMII_CONTROL_MR_AN_ENABLE,
1408 		       port->sgmii_base + AM65_CPSW_SGMII_CONTROL_REG);
1409 }
1410 
1411 static void am65_cpsw_nuss_mac_link_down(struct phylink_config *config, unsigned int mode,
1412 					 phy_interface_t interface)
1413 {
1414 	struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data,
1415 							  phylink_config);
1416 	struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave);
1417 	struct am65_cpsw_common *common = port->common;
1418 	struct net_device *ndev = port->ndev;
1419 	int tmo;
1420 
1421 	/* disable forwarding */
1422 	cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1423 
1424 	cpsw_sl_ctl_set(port->slave.mac_sl, CPSW_SL_CTL_CMD_IDLE);
1425 
1426 	tmo = cpsw_sl_wait_for_idle(port->slave.mac_sl, 100);
1427 	dev_dbg(common->dev, "down msc_sl %08x tmo %d\n",
1428 		cpsw_sl_reg_read(port->slave.mac_sl, CPSW_SL_MACSTATUS), tmo);
1429 
1430 	cpsw_sl_ctl_reset(port->slave.mac_sl);
1431 
1432 	am65_cpsw_qos_link_down(ndev);
1433 	netif_tx_stop_all_queues(ndev);
1434 }
1435 
1436 static void am65_cpsw_nuss_mac_link_up(struct phylink_config *config, struct phy_device *phy,
1437 				       unsigned int mode, phy_interface_t interface, int speed,
1438 				       int duplex, bool tx_pause, bool rx_pause)
1439 {
1440 	struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data,
1441 							  phylink_config);
1442 	struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave);
1443 	struct am65_cpsw_common *common = port->common;
1444 	u32 mac_control = CPSW_SL_CTL_GMII_EN;
1445 	struct net_device *ndev = port->ndev;
1446 
1447 	if (speed == SPEED_1000)
1448 		mac_control |= CPSW_SL_CTL_GIG;
1449 	if (speed == SPEED_10 && interface == PHY_INTERFACE_MODE_RGMII)
1450 		/* Can be used with in band mode only */
1451 		mac_control |= CPSW_SL_CTL_EXT_EN;
1452 	if (speed == SPEED_100 && interface == PHY_INTERFACE_MODE_RMII)
1453 		mac_control |= CPSW_SL_CTL_IFCTL_A;
1454 	if (duplex)
1455 		mac_control |= CPSW_SL_CTL_FULLDUPLEX;
1456 
1457 	/* rx_pause/tx_pause */
1458 	if (rx_pause)
1459 		mac_control |= CPSW_SL_CTL_RX_FLOW_EN;
1460 
1461 	if (tx_pause)
1462 		mac_control |= CPSW_SL_CTL_TX_FLOW_EN;
1463 
1464 	cpsw_sl_ctl_set(port->slave.mac_sl, mac_control);
1465 
1466 	/* enable forwarding */
1467 	cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1468 
1469 	am65_cpsw_qos_link_up(ndev, speed);
1470 	netif_tx_wake_all_queues(ndev);
1471 }
1472 
1473 static const struct phylink_mac_ops am65_cpsw_phylink_mac_ops = {
1474 	.validate = phylink_generic_validate,
1475 	.mac_config = am65_cpsw_nuss_mac_config,
1476 	.mac_link_down = am65_cpsw_nuss_mac_link_down,
1477 	.mac_link_up = am65_cpsw_nuss_mac_link_up,
1478 };
1479 
1480 static void am65_cpsw_nuss_slave_disable_unused(struct am65_cpsw_port *port)
1481 {
1482 	struct am65_cpsw_common *common = port->common;
1483 
1484 	if (!port->disabled)
1485 		return;
1486 
1487 	cpsw_ale_control_set(common->ale, port->port_id,
1488 			     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1489 
1490 	cpsw_sl_reset(port->slave.mac_sl, 100);
1491 	cpsw_sl_ctl_reset(port->slave.mac_sl);
1492 }
1493 
1494 static void am65_cpsw_nuss_free_tx_chns(void *data)
1495 {
1496 	struct am65_cpsw_common *common = data;
1497 	int i;
1498 
1499 	for (i = 0; i < common->tx_ch_num; i++) {
1500 		struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
1501 
1502 		if (!IS_ERR_OR_NULL(tx_chn->desc_pool))
1503 			k3_cppi_desc_pool_destroy(tx_chn->desc_pool);
1504 
1505 		if (!IS_ERR_OR_NULL(tx_chn->tx_chn))
1506 			k3_udma_glue_release_tx_chn(tx_chn->tx_chn);
1507 
1508 		memset(tx_chn, 0, sizeof(*tx_chn));
1509 	}
1510 }
1511 
1512 void am65_cpsw_nuss_remove_tx_chns(struct am65_cpsw_common *common)
1513 {
1514 	struct device *dev = common->dev;
1515 	int i;
1516 
1517 	devm_remove_action(dev, am65_cpsw_nuss_free_tx_chns, common);
1518 
1519 	for (i = 0; i < common->tx_ch_num; i++) {
1520 		struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
1521 
1522 		if (tx_chn->irq)
1523 			devm_free_irq(dev, tx_chn->irq, tx_chn);
1524 
1525 		netif_napi_del(&tx_chn->napi_tx);
1526 
1527 		if (!IS_ERR_OR_NULL(tx_chn->desc_pool))
1528 			k3_cppi_desc_pool_destroy(tx_chn->desc_pool);
1529 
1530 		if (!IS_ERR_OR_NULL(tx_chn->tx_chn))
1531 			k3_udma_glue_release_tx_chn(tx_chn->tx_chn);
1532 
1533 		memset(tx_chn, 0, sizeof(*tx_chn));
1534 	}
1535 }
1536 
1537 static int am65_cpsw_nuss_init_tx_chns(struct am65_cpsw_common *common)
1538 {
1539 	u32  max_desc_num = ALIGN(AM65_CPSW_MAX_TX_DESC, MAX_SKB_FRAGS);
1540 	struct k3_udma_glue_tx_channel_cfg tx_cfg = { 0 };
1541 	struct device *dev = common->dev;
1542 	struct k3_ring_cfg ring_cfg = {
1543 		.elm_size = K3_RINGACC_RING_ELSIZE_8,
1544 		.mode = K3_RINGACC_RING_MODE_RING,
1545 		.flags = 0
1546 	};
1547 	u32 hdesc_size;
1548 	int i, ret = 0;
1549 
1550 	hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE,
1551 					   AM65_CPSW_NAV_SW_DATA_SIZE);
1552 
1553 	tx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE;
1554 	tx_cfg.tx_cfg = ring_cfg;
1555 	tx_cfg.txcq_cfg = ring_cfg;
1556 	tx_cfg.tx_cfg.size = max_desc_num;
1557 	tx_cfg.txcq_cfg.size = max_desc_num;
1558 
1559 	for (i = 0; i < common->tx_ch_num; i++) {
1560 		struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
1561 
1562 		snprintf(tx_chn->tx_chn_name,
1563 			 sizeof(tx_chn->tx_chn_name), "tx%d", i);
1564 
1565 		spin_lock_init(&tx_chn->lock);
1566 		tx_chn->common = common;
1567 		tx_chn->id = i;
1568 		tx_chn->descs_num = max_desc_num;
1569 
1570 		tx_chn->tx_chn =
1571 			k3_udma_glue_request_tx_chn(dev,
1572 						    tx_chn->tx_chn_name,
1573 						    &tx_cfg);
1574 		if (IS_ERR(tx_chn->tx_chn)) {
1575 			ret = dev_err_probe(dev, PTR_ERR(tx_chn->tx_chn),
1576 					    "Failed to request tx dma channel\n");
1577 			goto err;
1578 		}
1579 		tx_chn->dma_dev = k3_udma_glue_tx_get_dma_device(tx_chn->tx_chn);
1580 
1581 		tx_chn->desc_pool = k3_cppi_desc_pool_create_name(tx_chn->dma_dev,
1582 								  tx_chn->descs_num,
1583 								  hdesc_size,
1584 								  tx_chn->tx_chn_name);
1585 		if (IS_ERR(tx_chn->desc_pool)) {
1586 			ret = PTR_ERR(tx_chn->desc_pool);
1587 			dev_err(dev, "Failed to create poll %d\n", ret);
1588 			goto err;
1589 		}
1590 
1591 		tx_chn->irq = k3_udma_glue_tx_get_irq(tx_chn->tx_chn);
1592 		if (tx_chn->irq <= 0) {
1593 			dev_err(dev, "Failed to get tx dma irq %d\n",
1594 				tx_chn->irq);
1595 			goto err;
1596 		}
1597 
1598 		snprintf(tx_chn->tx_chn_name,
1599 			 sizeof(tx_chn->tx_chn_name), "%s-tx%d",
1600 			 dev_name(dev), tx_chn->id);
1601 	}
1602 
1603 err:
1604 	i = devm_add_action(dev, am65_cpsw_nuss_free_tx_chns, common);
1605 	if (i) {
1606 		dev_err(dev, "Failed to add free_tx_chns action %d\n", i);
1607 		return i;
1608 	}
1609 
1610 	return ret;
1611 }
1612 
1613 static void am65_cpsw_nuss_free_rx_chns(void *data)
1614 {
1615 	struct am65_cpsw_common *common = data;
1616 	struct am65_cpsw_rx_chn *rx_chn;
1617 
1618 	rx_chn = &common->rx_chns;
1619 
1620 	if (!IS_ERR_OR_NULL(rx_chn->desc_pool))
1621 		k3_cppi_desc_pool_destroy(rx_chn->desc_pool);
1622 
1623 	if (!IS_ERR_OR_NULL(rx_chn->rx_chn))
1624 		k3_udma_glue_release_rx_chn(rx_chn->rx_chn);
1625 }
1626 
1627 static int am65_cpsw_nuss_init_rx_chns(struct am65_cpsw_common *common)
1628 {
1629 	struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
1630 	struct k3_udma_glue_rx_channel_cfg rx_cfg = { 0 };
1631 	u32  max_desc_num = AM65_CPSW_MAX_RX_DESC;
1632 	struct device *dev = common->dev;
1633 	u32 hdesc_size;
1634 	u32 fdqring_id;
1635 	int i, ret = 0;
1636 
1637 	hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE,
1638 					   AM65_CPSW_NAV_SW_DATA_SIZE);
1639 
1640 	rx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE;
1641 	rx_cfg.flow_id_num = AM65_CPSW_MAX_RX_FLOWS;
1642 	rx_cfg.flow_id_base = common->rx_flow_id_base;
1643 
1644 	/* init all flows */
1645 	rx_chn->dev = dev;
1646 	rx_chn->descs_num = max_desc_num;
1647 
1648 	rx_chn->rx_chn = k3_udma_glue_request_rx_chn(dev, "rx", &rx_cfg);
1649 	if (IS_ERR(rx_chn->rx_chn)) {
1650 		ret = dev_err_probe(dev, PTR_ERR(rx_chn->rx_chn),
1651 				    "Failed to request rx dma channel\n");
1652 		goto err;
1653 	}
1654 	rx_chn->dma_dev = k3_udma_glue_rx_get_dma_device(rx_chn->rx_chn);
1655 
1656 	rx_chn->desc_pool = k3_cppi_desc_pool_create_name(rx_chn->dma_dev,
1657 							  rx_chn->descs_num,
1658 							  hdesc_size, "rx");
1659 	if (IS_ERR(rx_chn->desc_pool)) {
1660 		ret = PTR_ERR(rx_chn->desc_pool);
1661 		dev_err(dev, "Failed to create rx poll %d\n", ret);
1662 		goto err;
1663 	}
1664 
1665 	common->rx_flow_id_base =
1666 			k3_udma_glue_rx_get_flow_id_base(rx_chn->rx_chn);
1667 	dev_info(dev, "set new flow-id-base %u\n", common->rx_flow_id_base);
1668 
1669 	fdqring_id = K3_RINGACC_RING_ID_ANY;
1670 	for (i = 0; i < rx_cfg.flow_id_num; i++) {
1671 		struct k3_ring_cfg rxring_cfg = {
1672 			.elm_size = K3_RINGACC_RING_ELSIZE_8,
1673 			.mode = K3_RINGACC_RING_MODE_RING,
1674 			.flags = 0,
1675 		};
1676 		struct k3_ring_cfg fdqring_cfg = {
1677 			.elm_size = K3_RINGACC_RING_ELSIZE_8,
1678 			.flags = K3_RINGACC_RING_SHARED,
1679 		};
1680 		struct k3_udma_glue_rx_flow_cfg rx_flow_cfg = {
1681 			.rx_cfg = rxring_cfg,
1682 			.rxfdq_cfg = fdqring_cfg,
1683 			.ring_rxq_id = K3_RINGACC_RING_ID_ANY,
1684 			.src_tag_lo_sel =
1685 				K3_UDMA_GLUE_SRC_TAG_LO_USE_REMOTE_SRC_TAG,
1686 		};
1687 
1688 		rx_flow_cfg.ring_rxfdq0_id = fdqring_id;
1689 		rx_flow_cfg.rx_cfg.size = max_desc_num;
1690 		rx_flow_cfg.rxfdq_cfg.size = max_desc_num;
1691 		rx_flow_cfg.rxfdq_cfg.mode = common->pdata.fdqring_mode;
1692 
1693 		ret = k3_udma_glue_rx_flow_init(rx_chn->rx_chn,
1694 						i, &rx_flow_cfg);
1695 		if (ret) {
1696 			dev_err(dev, "Failed to init rx flow%d %d\n", i, ret);
1697 			goto err;
1698 		}
1699 		if (!i)
1700 			fdqring_id =
1701 				k3_udma_glue_rx_flow_get_fdq_id(rx_chn->rx_chn,
1702 								i);
1703 
1704 		rx_chn->irq = k3_udma_glue_rx_get_irq(rx_chn->rx_chn, i);
1705 
1706 		if (rx_chn->irq <= 0) {
1707 			dev_err(dev, "Failed to get rx dma irq %d\n",
1708 				rx_chn->irq);
1709 			ret = -ENXIO;
1710 			goto err;
1711 		}
1712 	}
1713 
1714 err:
1715 	i = devm_add_action(dev, am65_cpsw_nuss_free_rx_chns, common);
1716 	if (i) {
1717 		dev_err(dev, "Failed to add free_rx_chns action %d\n", i);
1718 		return i;
1719 	}
1720 
1721 	return ret;
1722 }
1723 
1724 static int am65_cpsw_nuss_init_host_p(struct am65_cpsw_common *common)
1725 {
1726 	struct am65_cpsw_host *host_p = am65_common_get_host(common);
1727 
1728 	host_p->common = common;
1729 	host_p->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE;
1730 	host_p->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE;
1731 
1732 	return 0;
1733 }
1734 
1735 static int am65_cpsw_am654_get_efuse_macid(struct device_node *of_node,
1736 					   int slave, u8 *mac_addr)
1737 {
1738 	u32 mac_lo, mac_hi, offset;
1739 	struct regmap *syscon;
1740 	int ret;
1741 
1742 	syscon = syscon_regmap_lookup_by_phandle(of_node, "ti,syscon-efuse");
1743 	if (IS_ERR(syscon)) {
1744 		if (PTR_ERR(syscon) == -ENODEV)
1745 			return 0;
1746 		return PTR_ERR(syscon);
1747 	}
1748 
1749 	ret = of_property_read_u32_index(of_node, "ti,syscon-efuse", 1,
1750 					 &offset);
1751 	if (ret)
1752 		return ret;
1753 
1754 	regmap_read(syscon, offset, &mac_lo);
1755 	regmap_read(syscon, offset + 4, &mac_hi);
1756 
1757 	mac_addr[0] = (mac_hi >> 8) & 0xff;
1758 	mac_addr[1] = mac_hi & 0xff;
1759 	mac_addr[2] = (mac_lo >> 24) & 0xff;
1760 	mac_addr[3] = (mac_lo >> 16) & 0xff;
1761 	mac_addr[4] = (mac_lo >> 8) & 0xff;
1762 	mac_addr[5] = mac_lo & 0xff;
1763 
1764 	return 0;
1765 }
1766 
1767 static int am65_cpsw_init_cpts(struct am65_cpsw_common *common)
1768 {
1769 	struct device *dev = common->dev;
1770 	struct device_node *node;
1771 	struct am65_cpts *cpts;
1772 	void __iomem *reg_base;
1773 
1774 	if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS))
1775 		return 0;
1776 
1777 	node = of_get_child_by_name(dev->of_node, "cpts");
1778 	if (!node) {
1779 		dev_err(dev, "%s cpts not found\n", __func__);
1780 		return -ENOENT;
1781 	}
1782 
1783 	reg_base = common->cpsw_base + AM65_CPSW_NU_CPTS_BASE;
1784 	cpts = am65_cpts_create(dev, reg_base, node);
1785 	if (IS_ERR(cpts)) {
1786 		int ret = PTR_ERR(cpts);
1787 
1788 		of_node_put(node);
1789 		if (ret == -EOPNOTSUPP) {
1790 			dev_info(dev, "cpts disabled\n");
1791 			return 0;
1792 		}
1793 
1794 		dev_err(dev, "cpts create err %d\n", ret);
1795 		return ret;
1796 	}
1797 	common->cpts = cpts;
1798 	/* Forbid PM runtime if CPTS is running.
1799 	 * K3 CPSWxG modules may completely lose context during ON->OFF
1800 	 * transitions depending on integration.
1801 	 * AM65x/J721E MCU CPSW2G: false
1802 	 * J721E MAIN_CPSW9G: true
1803 	 */
1804 	pm_runtime_forbid(dev);
1805 
1806 	return 0;
1807 }
1808 
1809 static int am65_cpsw_nuss_init_slave_ports(struct am65_cpsw_common *common)
1810 {
1811 	struct device_node *node, *port_np;
1812 	struct device *dev = common->dev;
1813 	int ret;
1814 
1815 	node = of_get_child_by_name(dev->of_node, "ethernet-ports");
1816 	if (!node)
1817 		return -ENOENT;
1818 
1819 	for_each_child_of_node(node, port_np) {
1820 		struct am65_cpsw_port *port;
1821 		u32 port_id;
1822 
1823 		/* it is not a slave port node, continue */
1824 		if (strcmp(port_np->name, "port"))
1825 			continue;
1826 
1827 		ret = of_property_read_u32(port_np, "reg", &port_id);
1828 		if (ret < 0) {
1829 			dev_err(dev, "%pOF error reading port_id %d\n",
1830 				port_np, ret);
1831 			goto of_node_put;
1832 		}
1833 
1834 		if (!port_id || port_id > common->port_num) {
1835 			dev_err(dev, "%pOF has invalid port_id %u %s\n",
1836 				port_np, port_id, port_np->name);
1837 			ret = -EINVAL;
1838 			goto of_node_put;
1839 		}
1840 
1841 		port = am65_common_get_port(common, port_id);
1842 		port->port_id = port_id;
1843 		port->common = common;
1844 		port->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE +
1845 				  AM65_CPSW_NU_PORTS_OFFSET * (port_id);
1846 		if (common->pdata.extra_modes)
1847 			port->sgmii_base = common->ss_base + AM65_CPSW_SGMII_BASE * (port_id);
1848 		port->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE +
1849 				  (AM65_CPSW_NU_STATS_PORT_OFFSET * port_id);
1850 		port->name = of_get_property(port_np, "label", NULL);
1851 		port->fetch_ram_base =
1852 				common->cpsw_base + AM65_CPSW_NU_FRAM_BASE +
1853 				(AM65_CPSW_NU_FRAM_PORT_OFFSET * (port_id - 1));
1854 
1855 		port->slave.mac_sl = cpsw_sl_get("am65", dev, port->port_base);
1856 		if (IS_ERR(port->slave.mac_sl)) {
1857 			ret = PTR_ERR(port->slave.mac_sl);
1858 			goto of_node_put;
1859 		}
1860 
1861 		port->disabled = !of_device_is_available(port_np);
1862 		if (port->disabled) {
1863 			common->disabled_ports_mask |= BIT(port->port_id);
1864 			continue;
1865 		}
1866 
1867 		port->slave.ifphy = devm_of_phy_get(dev, port_np, NULL);
1868 		if (IS_ERR(port->slave.ifphy)) {
1869 			ret = PTR_ERR(port->slave.ifphy);
1870 			dev_err(dev, "%pOF error retrieving port phy: %d\n",
1871 				port_np, ret);
1872 			goto of_node_put;
1873 		}
1874 
1875 		port->slave.mac_only =
1876 				of_property_read_bool(port_np, "ti,mac-only");
1877 
1878 		/* get phy/link info */
1879 		port->slave.phy_node = port_np;
1880 		ret = of_get_phy_mode(port_np, &port->slave.phy_if);
1881 		if (ret) {
1882 			dev_err(dev, "%pOF read phy-mode err %d\n",
1883 				port_np, ret);
1884 			goto of_node_put;
1885 		}
1886 
1887 		ret = phy_set_mode_ext(port->slave.ifphy, PHY_MODE_ETHERNET, port->slave.phy_if);
1888 		if (ret)
1889 			goto of_node_put;
1890 
1891 		ret = of_get_mac_address(port_np, port->slave.mac_addr);
1892 		if (ret) {
1893 			am65_cpsw_am654_get_efuse_macid(port_np,
1894 							port->port_id,
1895 							port->slave.mac_addr);
1896 			if (!is_valid_ether_addr(port->slave.mac_addr)) {
1897 				eth_random_addr(port->slave.mac_addr);
1898 				dev_err(dev, "Use random MAC address\n");
1899 			}
1900 		}
1901 	}
1902 	of_node_put(node);
1903 
1904 	/* is there at least one ext.port */
1905 	if (!(~common->disabled_ports_mask & GENMASK(common->port_num, 1))) {
1906 		dev_err(dev, "No Ext. port are available\n");
1907 		return -ENODEV;
1908 	}
1909 
1910 	return 0;
1911 
1912 of_node_put:
1913 	of_node_put(port_np);
1914 	of_node_put(node);
1915 	return ret;
1916 }
1917 
1918 static void am65_cpsw_pcpu_stats_free(void *data)
1919 {
1920 	struct am65_cpsw_ndev_stats __percpu *stats = data;
1921 
1922 	free_percpu(stats);
1923 }
1924 
1925 static void am65_cpsw_nuss_phylink_cleanup(struct am65_cpsw_common *common)
1926 {
1927 	struct am65_cpsw_port *port;
1928 	int i;
1929 
1930 	for (i = 0; i < common->port_num; i++) {
1931 		port = &common->ports[i];
1932 		if (port->slave.phylink)
1933 			phylink_destroy(port->slave.phylink);
1934 	}
1935 }
1936 
1937 static int
1938 am65_cpsw_nuss_init_port_ndev(struct am65_cpsw_common *common, u32 port_idx)
1939 {
1940 	struct am65_cpsw_ndev_priv *ndev_priv;
1941 	struct device *dev = common->dev;
1942 	struct am65_cpsw_port *port;
1943 	struct phylink *phylink;
1944 	int ret;
1945 
1946 	port = &common->ports[port_idx];
1947 
1948 	if (port->disabled)
1949 		return 0;
1950 
1951 	/* alloc netdev */
1952 	port->ndev = devm_alloc_etherdev_mqs(common->dev,
1953 					     sizeof(struct am65_cpsw_ndev_priv),
1954 					     AM65_CPSW_MAX_TX_QUEUES,
1955 					     AM65_CPSW_MAX_RX_QUEUES);
1956 	if (!port->ndev) {
1957 		dev_err(dev, "error allocating slave net_device %u\n",
1958 			port->port_id);
1959 		return -ENOMEM;
1960 	}
1961 
1962 	ndev_priv = netdev_priv(port->ndev);
1963 	ndev_priv->port = port;
1964 	ndev_priv->msg_enable = AM65_CPSW_DEBUG;
1965 	SET_NETDEV_DEV(port->ndev, dev);
1966 
1967 	eth_hw_addr_set(port->ndev, port->slave.mac_addr);
1968 
1969 	port->ndev->min_mtu = AM65_CPSW_MIN_PACKET_SIZE;
1970 	port->ndev->max_mtu = AM65_CPSW_MAX_PACKET_SIZE;
1971 	port->ndev->hw_features = NETIF_F_SG |
1972 				  NETIF_F_RXCSUM |
1973 				  NETIF_F_HW_CSUM |
1974 				  NETIF_F_HW_TC;
1975 	port->ndev->features = port->ndev->hw_features |
1976 			       NETIF_F_HW_VLAN_CTAG_FILTER;
1977 	port->ndev->vlan_features |=  NETIF_F_SG;
1978 	port->ndev->netdev_ops = &am65_cpsw_nuss_netdev_ops;
1979 	port->ndev->ethtool_ops = &am65_cpsw_ethtool_ops_slave;
1980 
1981 	/* Configuring Phylink */
1982 	port->slave.phylink_config.dev = &port->ndev->dev;
1983 	port->slave.phylink_config.type = PHYLINK_NETDEV;
1984 	port->slave.phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_10 | MAC_100 | MAC_1000FD;
1985 
1986 	if (phy_interface_mode_is_rgmii(port->slave.phy_if)) {
1987 		phy_interface_set_rgmii(port->slave.phylink_config.supported_interfaces);
1988 	} else if (port->slave.phy_if == PHY_INTERFACE_MODE_RMII) {
1989 		__set_bit(PHY_INTERFACE_MODE_RMII,
1990 			  port->slave.phylink_config.supported_interfaces);
1991 	} else if (common->pdata.extra_modes & BIT(port->slave.phy_if)) {
1992 		__set_bit(PHY_INTERFACE_MODE_QSGMII,
1993 			  port->slave.phylink_config.supported_interfaces);
1994 	} else {
1995 		dev_err(dev, "selected phy-mode is not supported\n");
1996 		return -EOPNOTSUPP;
1997 	}
1998 
1999 	phylink = phylink_create(&port->slave.phylink_config,
2000 				 of_node_to_fwnode(port->slave.phy_node),
2001 				 port->slave.phy_if,
2002 				 &am65_cpsw_phylink_mac_ops);
2003 	if (IS_ERR(phylink))
2004 		return PTR_ERR(phylink);
2005 
2006 	port->slave.phylink = phylink;
2007 
2008 	/* Disable TX checksum offload by default due to HW bug */
2009 	if (common->pdata.quirks & AM65_CPSW_QUIRK_I2027_NO_TX_CSUM)
2010 		port->ndev->features &= ~NETIF_F_HW_CSUM;
2011 
2012 	ndev_priv->stats = netdev_alloc_pcpu_stats(struct am65_cpsw_ndev_stats);
2013 	if (!ndev_priv->stats)
2014 		return -ENOMEM;
2015 
2016 	ret = devm_add_action_or_reset(dev, am65_cpsw_pcpu_stats_free,
2017 				       ndev_priv->stats);
2018 	if (ret)
2019 		dev_err(dev, "failed to add percpu stat free action %d\n", ret);
2020 
2021 	if (!common->dma_ndev)
2022 		common->dma_ndev = port->ndev;
2023 
2024 	return ret;
2025 }
2026 
2027 static int am65_cpsw_nuss_init_ndevs(struct am65_cpsw_common *common)
2028 {
2029 	int ret;
2030 	int i;
2031 
2032 	for (i = 0; i < common->port_num; i++) {
2033 		ret = am65_cpsw_nuss_init_port_ndev(common, i);
2034 		if (ret)
2035 			return ret;
2036 	}
2037 
2038 	netif_napi_add(common->dma_ndev, &common->napi_rx,
2039 		       am65_cpsw_nuss_rx_poll);
2040 
2041 	return ret;
2042 }
2043 
2044 static int am65_cpsw_nuss_ndev_add_tx_napi(struct am65_cpsw_common *common)
2045 {
2046 	struct device *dev = common->dev;
2047 	int i, ret = 0;
2048 
2049 	for (i = 0; i < common->tx_ch_num; i++) {
2050 		struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
2051 
2052 		netif_napi_add_tx(common->dma_ndev, &tx_chn->napi_tx,
2053 				  am65_cpsw_nuss_tx_poll);
2054 
2055 		ret = devm_request_irq(dev, tx_chn->irq,
2056 				       am65_cpsw_nuss_tx_irq,
2057 				       IRQF_TRIGGER_HIGH,
2058 				       tx_chn->tx_chn_name, tx_chn);
2059 		if (ret) {
2060 			dev_err(dev, "failure requesting tx%u irq %u, %d\n",
2061 				tx_chn->id, tx_chn->irq, ret);
2062 			goto err;
2063 		}
2064 	}
2065 
2066 err:
2067 	return ret;
2068 }
2069 
2070 static void am65_cpsw_nuss_cleanup_ndev(struct am65_cpsw_common *common)
2071 {
2072 	struct am65_cpsw_port *port;
2073 	int i;
2074 
2075 	for (i = 0; i < common->port_num; i++) {
2076 		port = &common->ports[i];
2077 		if (port->ndev)
2078 			unregister_netdev(port->ndev);
2079 	}
2080 }
2081 
2082 static void am65_cpsw_port_offload_fwd_mark_update(struct am65_cpsw_common *common)
2083 {
2084 	int set_val = 0;
2085 	int i;
2086 
2087 	if (common->br_members == (GENMASK(common->port_num, 1) & ~common->disabled_ports_mask))
2088 		set_val = 1;
2089 
2090 	dev_dbg(common->dev, "set offload_fwd_mark %d\n", set_val);
2091 
2092 	for (i = 1; i <= common->port_num; i++) {
2093 		struct am65_cpsw_port *port = am65_common_get_port(common, i);
2094 		struct am65_cpsw_ndev_priv *priv;
2095 
2096 		if (!port->ndev)
2097 			continue;
2098 
2099 		priv = am65_ndev_to_priv(port->ndev);
2100 		priv->offload_fwd_mark = set_val;
2101 	}
2102 }
2103 
2104 bool am65_cpsw_port_dev_check(const struct net_device *ndev)
2105 {
2106 	if (ndev->netdev_ops == &am65_cpsw_nuss_netdev_ops) {
2107 		struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
2108 
2109 		return !common->is_emac_mode;
2110 	}
2111 
2112 	return false;
2113 }
2114 
2115 static int am65_cpsw_netdevice_port_link(struct net_device *ndev,
2116 					 struct net_device *br_ndev,
2117 					 struct netlink_ext_ack *extack)
2118 {
2119 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
2120 	struct am65_cpsw_ndev_priv *priv = am65_ndev_to_priv(ndev);
2121 	int err;
2122 
2123 	if (!common->br_members) {
2124 		common->hw_bridge_dev = br_ndev;
2125 	} else {
2126 		/* This is adding the port to a second bridge, this is
2127 		 * unsupported
2128 		 */
2129 		if (common->hw_bridge_dev != br_ndev)
2130 			return -EOPNOTSUPP;
2131 	}
2132 
2133 	err = switchdev_bridge_port_offload(ndev, ndev, NULL, NULL, NULL,
2134 					    false, extack);
2135 	if (err)
2136 		return err;
2137 
2138 	common->br_members |= BIT(priv->port->port_id);
2139 
2140 	am65_cpsw_port_offload_fwd_mark_update(common);
2141 
2142 	return NOTIFY_DONE;
2143 }
2144 
2145 static void am65_cpsw_netdevice_port_unlink(struct net_device *ndev)
2146 {
2147 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
2148 	struct am65_cpsw_ndev_priv *priv = am65_ndev_to_priv(ndev);
2149 
2150 	switchdev_bridge_port_unoffload(ndev, NULL, NULL, NULL);
2151 
2152 	common->br_members &= ~BIT(priv->port->port_id);
2153 
2154 	am65_cpsw_port_offload_fwd_mark_update(common);
2155 
2156 	if (!common->br_members)
2157 		common->hw_bridge_dev = NULL;
2158 }
2159 
2160 /* netdev notifier */
2161 static int am65_cpsw_netdevice_event(struct notifier_block *unused,
2162 				     unsigned long event, void *ptr)
2163 {
2164 	struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
2165 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
2166 	struct netdev_notifier_changeupper_info *info;
2167 	int ret = NOTIFY_DONE;
2168 
2169 	if (!am65_cpsw_port_dev_check(ndev))
2170 		return NOTIFY_DONE;
2171 
2172 	switch (event) {
2173 	case NETDEV_CHANGEUPPER:
2174 		info = ptr;
2175 
2176 		if (netif_is_bridge_master(info->upper_dev)) {
2177 			if (info->linking)
2178 				ret = am65_cpsw_netdevice_port_link(ndev,
2179 								    info->upper_dev,
2180 								    extack);
2181 			else
2182 				am65_cpsw_netdevice_port_unlink(ndev);
2183 		}
2184 		break;
2185 	default:
2186 		return NOTIFY_DONE;
2187 	}
2188 
2189 	return notifier_from_errno(ret);
2190 }
2191 
2192 static int am65_cpsw_register_notifiers(struct am65_cpsw_common *cpsw)
2193 {
2194 	int ret = 0;
2195 
2196 	if (AM65_CPSW_IS_CPSW2G(cpsw) ||
2197 	    !IS_REACHABLE(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV))
2198 		return 0;
2199 
2200 	cpsw->am65_cpsw_netdevice_nb.notifier_call = &am65_cpsw_netdevice_event;
2201 	ret = register_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb);
2202 	if (ret) {
2203 		dev_err(cpsw->dev, "can't register netdevice notifier\n");
2204 		return ret;
2205 	}
2206 
2207 	ret = am65_cpsw_switchdev_register_notifiers(cpsw);
2208 	if (ret)
2209 		unregister_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb);
2210 
2211 	return ret;
2212 }
2213 
2214 static void am65_cpsw_unregister_notifiers(struct am65_cpsw_common *cpsw)
2215 {
2216 	if (AM65_CPSW_IS_CPSW2G(cpsw) ||
2217 	    !IS_REACHABLE(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV))
2218 		return;
2219 
2220 	am65_cpsw_switchdev_unregister_notifiers(cpsw);
2221 	unregister_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb);
2222 }
2223 
2224 static const struct devlink_ops am65_cpsw_devlink_ops = {};
2225 
2226 static void am65_cpsw_init_stp_ale_entry(struct am65_cpsw_common *cpsw)
2227 {
2228 	cpsw_ale_add_mcast(cpsw->ale, eth_stp_addr, ALE_PORT_HOST, ALE_SUPER, 0,
2229 			   ALE_MCAST_BLOCK_LEARN_FWD);
2230 }
2231 
2232 static void am65_cpsw_init_host_port_switch(struct am65_cpsw_common *common)
2233 {
2234 	struct am65_cpsw_host *host = am65_common_get_host(common);
2235 
2236 	writel(common->default_vlan, host->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
2237 
2238 	am65_cpsw_init_stp_ale_entry(common);
2239 
2240 	cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 1);
2241 	dev_dbg(common->dev, "Set P0_UNI_FLOOD\n");
2242 	cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 0);
2243 }
2244 
2245 static void am65_cpsw_init_host_port_emac(struct am65_cpsw_common *common)
2246 {
2247 	struct am65_cpsw_host *host = am65_common_get_host(common);
2248 
2249 	writel(0, host->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
2250 
2251 	cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 0);
2252 	dev_dbg(common->dev, "unset P0_UNI_FLOOD\n");
2253 
2254 	/* learning make no sense in multi-mac mode */
2255 	cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 1);
2256 }
2257 
2258 static int am65_cpsw_dl_switch_mode_get(struct devlink *dl, u32 id,
2259 					struct devlink_param_gset_ctx *ctx)
2260 {
2261 	struct am65_cpsw_devlink *dl_priv = devlink_priv(dl);
2262 	struct am65_cpsw_common *common = dl_priv->common;
2263 
2264 	dev_dbg(common->dev, "%s id:%u\n", __func__, id);
2265 
2266 	if (id != AM65_CPSW_DL_PARAM_SWITCH_MODE)
2267 		return -EOPNOTSUPP;
2268 
2269 	ctx->val.vbool = !common->is_emac_mode;
2270 
2271 	return 0;
2272 }
2273 
2274 static void am65_cpsw_init_port_emac_ale(struct  am65_cpsw_port *port)
2275 {
2276 	struct am65_cpsw_slave_data *slave = &port->slave;
2277 	struct am65_cpsw_common *common = port->common;
2278 	u32 port_mask;
2279 
2280 	writel(slave->port_vlan, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
2281 
2282 	if (slave->mac_only)
2283 		/* enable mac-only mode on port */
2284 		cpsw_ale_control_set(common->ale, port->port_id,
2285 				     ALE_PORT_MACONLY, 1);
2286 
2287 	cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_NOLEARN, 1);
2288 
2289 	port_mask = BIT(port->port_id) | ALE_PORT_HOST;
2290 
2291 	cpsw_ale_add_ucast(common->ale, port->ndev->dev_addr,
2292 			   HOST_PORT_NUM, ALE_SECURE, slave->port_vlan);
2293 	cpsw_ale_add_mcast(common->ale, port->ndev->broadcast,
2294 			   port_mask, ALE_VLAN, slave->port_vlan, ALE_MCAST_FWD_2);
2295 }
2296 
2297 static void am65_cpsw_init_port_switch_ale(struct am65_cpsw_port *port)
2298 {
2299 	struct am65_cpsw_slave_data *slave = &port->slave;
2300 	struct am65_cpsw_common *cpsw = port->common;
2301 	u32 port_mask;
2302 
2303 	cpsw_ale_control_set(cpsw->ale, port->port_id,
2304 			     ALE_PORT_NOLEARN, 0);
2305 
2306 	cpsw_ale_add_ucast(cpsw->ale, port->ndev->dev_addr,
2307 			   HOST_PORT_NUM, ALE_SECURE | ALE_BLOCKED | ALE_VLAN,
2308 			   slave->port_vlan);
2309 
2310 	port_mask = BIT(port->port_id) | ALE_PORT_HOST;
2311 
2312 	cpsw_ale_add_mcast(cpsw->ale, port->ndev->broadcast,
2313 			   port_mask, ALE_VLAN, slave->port_vlan,
2314 			   ALE_MCAST_FWD_2);
2315 
2316 	writel(slave->port_vlan, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
2317 
2318 	cpsw_ale_control_set(cpsw->ale, port->port_id,
2319 			     ALE_PORT_MACONLY, 0);
2320 }
2321 
2322 static int am65_cpsw_dl_switch_mode_set(struct devlink *dl, u32 id,
2323 					struct devlink_param_gset_ctx *ctx)
2324 {
2325 	struct am65_cpsw_devlink *dl_priv = devlink_priv(dl);
2326 	struct am65_cpsw_common *cpsw = dl_priv->common;
2327 	bool switch_en = ctx->val.vbool;
2328 	bool if_running = false;
2329 	int i;
2330 
2331 	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
2332 
2333 	if (id != AM65_CPSW_DL_PARAM_SWITCH_MODE)
2334 		return -EOPNOTSUPP;
2335 
2336 	if (switch_en == !cpsw->is_emac_mode)
2337 		return 0;
2338 
2339 	if (!switch_en && cpsw->br_members) {
2340 		dev_err(cpsw->dev, "Remove ports from bridge before disabling switch mode\n");
2341 		return -EINVAL;
2342 	}
2343 
2344 	rtnl_lock();
2345 
2346 	cpsw->is_emac_mode = !switch_en;
2347 
2348 	for (i = 0; i < cpsw->port_num; i++) {
2349 		struct net_device *sl_ndev = cpsw->ports[i].ndev;
2350 
2351 		if (!sl_ndev || !netif_running(sl_ndev))
2352 			continue;
2353 
2354 		if_running = true;
2355 	}
2356 
2357 	if (!if_running) {
2358 		/* all ndevs are down */
2359 		for (i = 0; i < cpsw->port_num; i++) {
2360 			struct net_device *sl_ndev = cpsw->ports[i].ndev;
2361 			struct am65_cpsw_slave_data *slave;
2362 
2363 			if (!sl_ndev)
2364 				continue;
2365 
2366 			slave = am65_ndev_to_slave(sl_ndev);
2367 			if (switch_en)
2368 				slave->port_vlan = cpsw->default_vlan;
2369 			else
2370 				slave->port_vlan = 0;
2371 		}
2372 
2373 		goto exit;
2374 	}
2375 
2376 	cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1);
2377 	/* clean up ALE table */
2378 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_CLEAR, 1);
2379 	cpsw_ale_control_get(cpsw->ale, HOST_PORT_NUM, ALE_AGEOUT);
2380 
2381 	if (switch_en) {
2382 		dev_info(cpsw->dev, "Enable switch mode\n");
2383 
2384 		am65_cpsw_init_host_port_switch(cpsw);
2385 
2386 		for (i = 0; i < cpsw->port_num; i++) {
2387 			struct net_device *sl_ndev = cpsw->ports[i].ndev;
2388 			struct am65_cpsw_slave_data *slave;
2389 			struct am65_cpsw_port *port;
2390 
2391 			if (!sl_ndev)
2392 				continue;
2393 
2394 			port = am65_ndev_to_port(sl_ndev);
2395 			slave = am65_ndev_to_slave(sl_ndev);
2396 			slave->port_vlan = cpsw->default_vlan;
2397 
2398 			if (netif_running(sl_ndev))
2399 				am65_cpsw_init_port_switch_ale(port);
2400 		}
2401 
2402 	} else {
2403 		dev_info(cpsw->dev, "Disable switch mode\n");
2404 
2405 		am65_cpsw_init_host_port_emac(cpsw);
2406 
2407 		for (i = 0; i < cpsw->port_num; i++) {
2408 			struct net_device *sl_ndev = cpsw->ports[i].ndev;
2409 			struct am65_cpsw_port *port;
2410 
2411 			if (!sl_ndev)
2412 				continue;
2413 
2414 			port = am65_ndev_to_port(sl_ndev);
2415 			port->slave.port_vlan = 0;
2416 			if (netif_running(sl_ndev))
2417 				am65_cpsw_init_port_emac_ale(port);
2418 		}
2419 	}
2420 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_BYPASS, 0);
2421 exit:
2422 	rtnl_unlock();
2423 
2424 	return 0;
2425 }
2426 
2427 static const struct devlink_param am65_cpsw_devlink_params[] = {
2428 	DEVLINK_PARAM_DRIVER(AM65_CPSW_DL_PARAM_SWITCH_MODE, "switch_mode",
2429 			     DEVLINK_PARAM_TYPE_BOOL,
2430 			     BIT(DEVLINK_PARAM_CMODE_RUNTIME),
2431 			     am65_cpsw_dl_switch_mode_get,
2432 			     am65_cpsw_dl_switch_mode_set, NULL),
2433 };
2434 
2435 static int am65_cpsw_nuss_register_devlink(struct am65_cpsw_common *common)
2436 {
2437 	struct devlink_port_attrs attrs = {};
2438 	struct am65_cpsw_devlink *dl_priv;
2439 	struct device *dev = common->dev;
2440 	struct devlink_port *dl_port;
2441 	struct am65_cpsw_port *port;
2442 	int ret = 0;
2443 	int i;
2444 
2445 	common->devlink =
2446 		devlink_alloc(&am65_cpsw_devlink_ops, sizeof(*dl_priv), dev);
2447 	if (!common->devlink)
2448 		return -ENOMEM;
2449 
2450 	dl_priv = devlink_priv(common->devlink);
2451 	dl_priv->common = common;
2452 
2453 	/* Provide devlink hook to switch mode when multiple external ports
2454 	 * are present NUSS switchdev driver is enabled.
2455 	 */
2456 	if (!AM65_CPSW_IS_CPSW2G(common) &&
2457 	    IS_ENABLED(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV)) {
2458 		ret = devlink_params_register(common->devlink,
2459 					      am65_cpsw_devlink_params,
2460 					      ARRAY_SIZE(am65_cpsw_devlink_params));
2461 		if (ret) {
2462 			dev_err(dev, "devlink params reg fail ret:%d\n", ret);
2463 			goto dl_unreg;
2464 		}
2465 	}
2466 
2467 	for (i = 1; i <= common->port_num; i++) {
2468 		port = am65_common_get_port(common, i);
2469 		dl_port = &port->devlink_port;
2470 
2471 		if (port->ndev)
2472 			attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
2473 		else
2474 			attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
2475 		attrs.phys.port_number = port->port_id;
2476 		attrs.switch_id.id_len = sizeof(resource_size_t);
2477 		memcpy(attrs.switch_id.id, common->switch_id, attrs.switch_id.id_len);
2478 		devlink_port_attrs_set(dl_port, &attrs);
2479 
2480 		ret = devlink_port_register(common->devlink, dl_port, port->port_id);
2481 		if (ret) {
2482 			dev_err(dev, "devlink_port reg fail for port %d, ret:%d\n",
2483 				port->port_id, ret);
2484 			goto dl_port_unreg;
2485 		}
2486 	}
2487 	devlink_register(common->devlink);
2488 	return ret;
2489 
2490 dl_port_unreg:
2491 	for (i = i - 1; i >= 1; i--) {
2492 		port = am65_common_get_port(common, i);
2493 		dl_port = &port->devlink_port;
2494 
2495 		devlink_port_unregister(dl_port);
2496 	}
2497 dl_unreg:
2498 	devlink_free(common->devlink);
2499 	return ret;
2500 }
2501 
2502 static void am65_cpsw_unregister_devlink(struct am65_cpsw_common *common)
2503 {
2504 	struct devlink_port *dl_port;
2505 	struct am65_cpsw_port *port;
2506 	int i;
2507 
2508 	devlink_unregister(common->devlink);
2509 
2510 	for (i = 1; i <= common->port_num; i++) {
2511 		port = am65_common_get_port(common, i);
2512 		dl_port = &port->devlink_port;
2513 
2514 		devlink_port_unregister(dl_port);
2515 	}
2516 
2517 	if (!AM65_CPSW_IS_CPSW2G(common) &&
2518 	    IS_ENABLED(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV))
2519 		devlink_params_unregister(common->devlink,
2520 					  am65_cpsw_devlink_params,
2521 					  ARRAY_SIZE(am65_cpsw_devlink_params));
2522 
2523 	devlink_free(common->devlink);
2524 }
2525 
2526 static int am65_cpsw_nuss_register_ndevs(struct am65_cpsw_common *common)
2527 {
2528 	struct device *dev = common->dev;
2529 	struct am65_cpsw_port *port;
2530 	int ret = 0, i;
2531 
2532 	ret = am65_cpsw_nuss_ndev_add_tx_napi(common);
2533 	if (ret)
2534 		return ret;
2535 
2536 	ret = devm_request_irq(dev, common->rx_chns.irq,
2537 			       am65_cpsw_nuss_rx_irq,
2538 			       IRQF_TRIGGER_HIGH, dev_name(dev), common);
2539 	if (ret) {
2540 		dev_err(dev, "failure requesting rx irq %u, %d\n",
2541 			common->rx_chns.irq, ret);
2542 		return ret;
2543 	}
2544 
2545 	ret = am65_cpsw_nuss_register_devlink(common);
2546 	if (ret)
2547 		return ret;
2548 
2549 	for (i = 0; i < common->port_num; i++) {
2550 		port = &common->ports[i];
2551 
2552 		if (!port->ndev)
2553 			continue;
2554 
2555 		SET_NETDEV_DEVLINK_PORT(port->ndev, &port->devlink_port);
2556 
2557 		ret = register_netdev(port->ndev);
2558 		if (ret) {
2559 			dev_err(dev, "error registering slave net device%i %d\n",
2560 				i, ret);
2561 			goto err_cleanup_ndev;
2562 		}
2563 	}
2564 
2565 	ret = am65_cpsw_register_notifiers(common);
2566 	if (ret)
2567 		goto err_cleanup_ndev;
2568 
2569 	/* can't auto unregister ndev using devm_add_action() due to
2570 	 * devres release sequence in DD core for DMA
2571 	 */
2572 
2573 	return 0;
2574 
2575 err_cleanup_ndev:
2576 	am65_cpsw_nuss_cleanup_ndev(common);
2577 	am65_cpsw_unregister_devlink(common);
2578 
2579 	return ret;
2580 }
2581 
2582 int am65_cpsw_nuss_update_tx_chns(struct am65_cpsw_common *common, int num_tx)
2583 {
2584 	int ret;
2585 
2586 	common->tx_ch_num = num_tx;
2587 	ret = am65_cpsw_nuss_init_tx_chns(common);
2588 	if (ret)
2589 		return ret;
2590 
2591 	return am65_cpsw_nuss_ndev_add_tx_napi(common);
2592 }
2593 
2594 struct am65_cpsw_soc_pdata {
2595 	u32	quirks_dis;
2596 };
2597 
2598 static const struct am65_cpsw_soc_pdata am65x_soc_sr2_0 = {
2599 	.quirks_dis = AM65_CPSW_QUIRK_I2027_NO_TX_CSUM,
2600 };
2601 
2602 static const struct soc_device_attribute am65_cpsw_socinfo[] = {
2603 	{ .family = "AM65X",
2604 	  .revision = "SR2.0",
2605 	  .data = &am65x_soc_sr2_0
2606 	},
2607 	{/* sentinel */}
2608 };
2609 
2610 static const struct am65_cpsw_pdata am65x_sr1_0 = {
2611 	.quirks = AM65_CPSW_QUIRK_I2027_NO_TX_CSUM,
2612 	.ale_dev_id = "am65x-cpsw2g",
2613 	.fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE,
2614 };
2615 
2616 static const struct am65_cpsw_pdata j721e_pdata = {
2617 	.quirks = 0,
2618 	.ale_dev_id = "am65x-cpsw2g",
2619 	.fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE,
2620 };
2621 
2622 static const struct am65_cpsw_pdata am64x_cpswxg_pdata = {
2623 	.quirks = 0,
2624 	.ale_dev_id = "am64-cpswxg",
2625 	.fdqring_mode = K3_RINGACC_RING_MODE_RING,
2626 };
2627 
2628 static const struct am65_cpsw_pdata j7200_cpswxg_pdata = {
2629 	.quirks = 0,
2630 	.ale_dev_id = "am64-cpswxg",
2631 	.fdqring_mode = K3_RINGACC_RING_MODE_RING,
2632 	.extra_modes = BIT(PHY_INTERFACE_MODE_QSGMII),
2633 };
2634 
2635 static const struct of_device_id am65_cpsw_nuss_of_mtable[] = {
2636 	{ .compatible = "ti,am654-cpsw-nuss", .data = &am65x_sr1_0},
2637 	{ .compatible = "ti,j721e-cpsw-nuss", .data = &j721e_pdata},
2638 	{ .compatible = "ti,am642-cpsw-nuss", .data = &am64x_cpswxg_pdata},
2639 	{ .compatible = "ti,j7200-cpswxg-nuss", .data = &j7200_cpswxg_pdata},
2640 	{ /* sentinel */ },
2641 };
2642 MODULE_DEVICE_TABLE(of, am65_cpsw_nuss_of_mtable);
2643 
2644 static void am65_cpsw_nuss_apply_socinfo(struct am65_cpsw_common *common)
2645 {
2646 	const struct soc_device_attribute *soc;
2647 
2648 	soc = soc_device_match(am65_cpsw_socinfo);
2649 	if (soc && soc->data) {
2650 		const struct am65_cpsw_soc_pdata *socdata = soc->data;
2651 
2652 		/* disable quirks */
2653 		common->pdata.quirks &= ~socdata->quirks_dis;
2654 	}
2655 }
2656 
2657 static int am65_cpsw_nuss_probe(struct platform_device *pdev)
2658 {
2659 	struct cpsw_ale_params ale_params = { 0 };
2660 	const struct of_device_id *of_id;
2661 	struct device *dev = &pdev->dev;
2662 	struct am65_cpsw_common *common;
2663 	struct device_node *node;
2664 	struct resource *res;
2665 	struct clk *clk;
2666 	u64 id_temp;
2667 	int ret, i;
2668 
2669 	common = devm_kzalloc(dev, sizeof(struct am65_cpsw_common), GFP_KERNEL);
2670 	if (!common)
2671 		return -ENOMEM;
2672 	common->dev = dev;
2673 
2674 	of_id = of_match_device(am65_cpsw_nuss_of_mtable, dev);
2675 	if (!of_id)
2676 		return -EINVAL;
2677 	common->pdata = *(const struct am65_cpsw_pdata *)of_id->data;
2678 
2679 	am65_cpsw_nuss_apply_socinfo(common);
2680 
2681 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cpsw_nuss");
2682 	common->ss_base = devm_ioremap_resource(&pdev->dev, res);
2683 	if (IS_ERR(common->ss_base))
2684 		return PTR_ERR(common->ss_base);
2685 	common->cpsw_base = common->ss_base + AM65_CPSW_CPSW_NU_BASE;
2686 	/* Use device's physical base address as switch id */
2687 	id_temp = cpu_to_be64(res->start);
2688 	memcpy(common->switch_id, &id_temp, sizeof(res->start));
2689 
2690 	node = of_get_child_by_name(dev->of_node, "ethernet-ports");
2691 	if (!node)
2692 		return -ENOENT;
2693 	common->port_num = of_get_child_count(node);
2694 	of_node_put(node);
2695 	if (common->port_num < 1 || common->port_num > AM65_CPSW_MAX_PORTS)
2696 		return -ENOENT;
2697 
2698 	common->rx_flow_id_base = -1;
2699 	init_completion(&common->tdown_complete);
2700 	common->tx_ch_num = 1;
2701 	common->pf_p0_rx_ptype_rrobin = false;
2702 	common->default_vlan = 1;
2703 
2704 	common->ports = devm_kcalloc(dev, common->port_num,
2705 				     sizeof(*common->ports),
2706 				     GFP_KERNEL);
2707 	if (!common->ports)
2708 		return -ENOMEM;
2709 
2710 	clk = devm_clk_get(dev, "fck");
2711 	if (IS_ERR(clk))
2712 		return dev_err_probe(dev, PTR_ERR(clk), "getting fck clock\n");
2713 	common->bus_freq = clk_get_rate(clk);
2714 
2715 	pm_runtime_enable(dev);
2716 	ret = pm_runtime_resume_and_get(dev);
2717 	if (ret < 0) {
2718 		pm_runtime_disable(dev);
2719 		return ret;
2720 	}
2721 
2722 	node = of_get_child_by_name(dev->of_node, "mdio");
2723 	if (!node) {
2724 		dev_warn(dev, "MDIO node not found\n");
2725 	} else if (of_device_is_available(node)) {
2726 		struct platform_device *mdio_pdev;
2727 
2728 		mdio_pdev = of_platform_device_create(node, NULL, dev);
2729 		if (!mdio_pdev) {
2730 			ret = -ENODEV;
2731 			goto err_pm_clear;
2732 		}
2733 
2734 		common->mdio_dev =  &mdio_pdev->dev;
2735 	}
2736 	of_node_put(node);
2737 
2738 	am65_cpsw_nuss_get_ver(common);
2739 
2740 	/* init tx channels */
2741 	ret = am65_cpsw_nuss_init_tx_chns(common);
2742 	if (ret)
2743 		goto err_of_clear;
2744 	ret = am65_cpsw_nuss_init_rx_chns(common);
2745 	if (ret)
2746 		goto err_of_clear;
2747 
2748 	ret = am65_cpsw_nuss_init_host_p(common);
2749 	if (ret)
2750 		goto err_of_clear;
2751 
2752 	ret = am65_cpsw_nuss_init_slave_ports(common);
2753 	if (ret)
2754 		goto err_of_clear;
2755 
2756 	/* init common data */
2757 	ale_params.dev = dev;
2758 	ale_params.ale_ageout = AM65_CPSW_ALE_AGEOUT_DEFAULT;
2759 	ale_params.ale_ports = common->port_num + 1;
2760 	ale_params.ale_regs = common->cpsw_base + AM65_CPSW_NU_ALE_BASE;
2761 	ale_params.dev_id = common->pdata.ale_dev_id;
2762 	ale_params.bus_freq = common->bus_freq;
2763 
2764 	common->ale = cpsw_ale_create(&ale_params);
2765 	if (IS_ERR(common->ale)) {
2766 		dev_err(dev, "error initializing ale engine\n");
2767 		ret = PTR_ERR(common->ale);
2768 		goto err_of_clear;
2769 	}
2770 
2771 	ret = am65_cpsw_init_cpts(common);
2772 	if (ret)
2773 		goto err_of_clear;
2774 
2775 	/* init ports */
2776 	for (i = 0; i < common->port_num; i++)
2777 		am65_cpsw_nuss_slave_disable_unused(&common->ports[i]);
2778 
2779 	dev_set_drvdata(dev, common);
2780 
2781 	common->is_emac_mode = true;
2782 
2783 	ret = am65_cpsw_nuss_init_ndevs(common);
2784 	if (ret)
2785 		goto err_free_phylink;
2786 
2787 	ret = am65_cpsw_nuss_register_ndevs(common);
2788 	if (ret)
2789 		goto err_free_phylink;
2790 
2791 	pm_runtime_put(dev);
2792 	return 0;
2793 
2794 err_free_phylink:
2795 	am65_cpsw_nuss_phylink_cleanup(common);
2796 err_of_clear:
2797 	of_platform_device_destroy(common->mdio_dev, NULL);
2798 err_pm_clear:
2799 	pm_runtime_put_sync(dev);
2800 	pm_runtime_disable(dev);
2801 	return ret;
2802 }
2803 
2804 static int am65_cpsw_nuss_remove(struct platform_device *pdev)
2805 {
2806 	struct device *dev = &pdev->dev;
2807 	struct am65_cpsw_common *common;
2808 	int ret;
2809 
2810 	common = dev_get_drvdata(dev);
2811 
2812 	ret = pm_runtime_resume_and_get(&pdev->dev);
2813 	if (ret < 0)
2814 		return ret;
2815 
2816 	am65_cpsw_nuss_phylink_cleanup(common);
2817 	am65_cpsw_unregister_devlink(common);
2818 	am65_cpsw_unregister_notifiers(common);
2819 
2820 	/* must unregister ndevs here because DD release_driver routine calls
2821 	 * dma_deconfigure(dev) before devres_release_all(dev)
2822 	 */
2823 	am65_cpsw_nuss_cleanup_ndev(common);
2824 
2825 	of_platform_device_destroy(common->mdio_dev, NULL);
2826 
2827 	pm_runtime_put_sync(&pdev->dev);
2828 	pm_runtime_disable(&pdev->dev);
2829 	return 0;
2830 }
2831 
2832 static struct platform_driver am65_cpsw_nuss_driver = {
2833 	.driver = {
2834 		.name	 = AM65_CPSW_DRV_NAME,
2835 		.of_match_table = am65_cpsw_nuss_of_mtable,
2836 	},
2837 	.probe = am65_cpsw_nuss_probe,
2838 	.remove = am65_cpsw_nuss_remove,
2839 };
2840 
2841 module_platform_driver(am65_cpsw_nuss_driver);
2842 
2843 MODULE_LICENSE("GPL v2");
2844 MODULE_AUTHOR("Grygorii Strashko <grygorii.strashko@ti.com>");
2845 MODULE_DESCRIPTION("TI AM65 CPSW Ethernet driver");
2846