• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/bpf_trace.h>
9 #include <linux/clk.h>
10 #include <linux/etherdevice.h>
11 #include <linux/if_vlan.h>
12 #include <linux/interrupt.h>
13 #include <linux/irqdomain.h>
14 #include <linux/kernel.h>
15 #include <linux/kmemleak.h>
16 #include <linux/module.h>
17 #include <linux/netdevice.h>
18 #include <linux/net_tstamp.h>
19 #include <linux/of.h>
20 #include <linux/of_mdio.h>
21 #include <linux/of_net.h>
22 #include <linux/of_device.h>
23 #include <linux/of_platform.h>
24 #include <linux/phylink.h>
25 #include <linux/phy/phy.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/regmap.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/mfd/syscon.h>
31 #include <linux/sys_soc.h>
32 #include <linux/dma/ti-cppi5.h>
33 #include <linux/dma/k3-udma-glue.h>
34 #include <net/page_pool/helpers.h>
35 #include <net/switchdev.h>
36 
37 #include "cpsw_ale.h"
38 #include "cpsw_sl.h"
39 #include "am65-cpsw-nuss.h"
40 #include "am65-cpsw-switchdev.h"
41 #include "k3-cppi-desc-pool.h"
42 #include "am65-cpts.h"
43 
44 #define AM65_CPSW_SS_BASE	0x0
45 #define AM65_CPSW_SGMII_BASE	0x100
46 #define AM65_CPSW_XGMII_BASE	0x2100
47 #define AM65_CPSW_CPSW_NU_BASE	0x20000
48 #define AM65_CPSW_NU_PORTS_BASE	0x1000
49 #define AM65_CPSW_NU_FRAM_BASE	0x12000
50 #define AM65_CPSW_NU_STATS_BASE	0x1a000
51 #define AM65_CPSW_NU_ALE_BASE	0x1e000
52 #define AM65_CPSW_NU_CPTS_BASE	0x1d000
53 
54 #define AM65_CPSW_NU_PORTS_OFFSET	0x1000
55 #define AM65_CPSW_NU_STATS_PORT_OFFSET	0x200
56 #define AM65_CPSW_NU_FRAM_PORT_OFFSET	0x200
57 
58 #define AM65_CPSW_MAX_PORTS	8
59 
60 #define AM65_CPSW_MIN_PACKET_SIZE	VLAN_ETH_ZLEN
61 #define AM65_CPSW_MAX_PACKET_SIZE	2024
62 
63 #define AM65_CPSW_REG_CTL		0x004
64 #define AM65_CPSW_REG_STAT_PORT_EN	0x014
65 #define AM65_CPSW_REG_PTYPE		0x018
66 
67 #define AM65_CPSW_P0_REG_CTL			0x004
68 #define AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET	0x008
69 
70 #define AM65_CPSW_PORT_REG_PRI_CTL		0x01c
71 #define AM65_CPSW_PORT_REG_RX_PRI_MAP		0x020
72 #define AM65_CPSW_PORT_REG_RX_MAXLEN		0x024
73 
74 #define AM65_CPSW_PORTN_REG_SA_L		0x308
75 #define AM65_CPSW_PORTN_REG_SA_H		0x30c
76 #define AM65_CPSW_PORTN_REG_TS_CTL              0x310
77 #define AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG	0x314
78 #define AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG	0x318
79 #define AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2       0x31C
80 
81 #define AM65_CPSW_SGMII_CONTROL_REG		0x010
82 #define AM65_CPSW_SGMII_MR_ADV_ABILITY_REG	0x018
83 #define AM65_CPSW_SGMII_CONTROL_MR_AN_ENABLE	BIT(0)
84 
85 #define AM65_CPSW_CTL_VLAN_AWARE		BIT(1)
86 #define AM65_CPSW_CTL_P0_ENABLE			BIT(2)
87 #define AM65_CPSW_CTL_P0_TX_CRC_REMOVE		BIT(13)
88 #define AM65_CPSW_CTL_P0_RX_PAD			BIT(14)
89 
90 /* AM65_CPSW_P0_REG_CTL */
91 #define AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN	BIT(0)
92 #define AM65_CPSW_P0_REG_CTL_RX_REMAP_VLAN	BIT(16)
93 
94 /* AM65_CPSW_PORT_REG_PRI_CTL */
95 #define AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN	BIT(8)
96 
97 /* AM65_CPSW_PN_TS_CTL register fields */
98 #define AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN		BIT(4)
99 #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN	BIT(5)
100 #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT2_EN	BIT(6)
101 #define AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN		BIT(7)
102 #define AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN		BIT(10)
103 #define AM65_CPSW_PN_TS_CTL_TX_HOST_TS_EN	BIT(11)
104 #define AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT	16
105 
106 #define AM65_CPSW_PN_TS_CTL_RX_ANX_F_EN		BIT(0)
107 #define AM65_CPSW_PN_TS_CTL_RX_VLAN_LT1_EN	BIT(1)
108 #define AM65_CPSW_PN_TS_CTL_RX_VLAN_LT2_EN	BIT(2)
109 #define AM65_CPSW_PN_TS_CTL_RX_ANX_D_EN		BIT(3)
110 #define AM65_CPSW_PN_TS_CTL_RX_ANX_E_EN		BIT(9)
111 
112 /* AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG register fields */
113 #define AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT	16
114 
115 /* AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2 */
116 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107	BIT(16)
117 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129	BIT(17)
118 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130	BIT(18)
119 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131	BIT(19)
120 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132	BIT(20)
121 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319	BIT(21)
122 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320	BIT(22)
123 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO BIT(23)
124 
125 /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
126 #define AM65_CPSW_TS_EVENT_MSG_TYPE_BITS (BIT(0) | BIT(1) | BIT(2) | BIT(3))
127 
128 #define AM65_CPSW_TS_SEQ_ID_OFFSET (0x1e)
129 
130 #define AM65_CPSW_TS_TX_ANX_ALL_EN		\
131 	(AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN |	\
132 	 AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN |	\
133 	 AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN)
134 
135 #define AM65_CPSW_TS_RX_ANX_ALL_EN		\
136 	(AM65_CPSW_PN_TS_CTL_RX_ANX_D_EN |	\
137 	 AM65_CPSW_PN_TS_CTL_RX_ANX_E_EN |	\
138 	 AM65_CPSW_PN_TS_CTL_RX_ANX_F_EN)
139 
140 #define AM65_CPSW_ALE_AGEOUT_DEFAULT	30
141 /* Number of TX/RX descriptors per channel/flow */
142 #define AM65_CPSW_MAX_TX_DESC	500
143 #define AM65_CPSW_MAX_RX_DESC	500
144 
145 #define AM65_CPSW_NAV_PS_DATA_SIZE 16
146 #define AM65_CPSW_NAV_SW_DATA_SIZE 16
147 
148 #define AM65_CPSW_DEBUG	(NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK | \
149 			 NETIF_MSG_IFUP	| NETIF_MSG_PROBE | NETIF_MSG_IFDOWN | \
150 			 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR)
151 
152 #define AM65_CPSW_DEFAULT_TX_CHNS	8
153 #define AM65_CPSW_DEFAULT_RX_CHN_FLOWS	1
154 
155 /* CPPI streaming packet interface */
156 #define AM65_CPSW_CPPI_TX_FLOW_ID  0x3FFF
157 #define AM65_CPSW_CPPI_TX_PKT_TYPE 0x7
158 
159 /* XDP */
160 #define AM65_CPSW_XDP_CONSUMED BIT(1)
161 #define AM65_CPSW_XDP_REDIRECT BIT(0)
162 #define AM65_CPSW_XDP_PASS     0
163 
164 /* Include headroom compatible with both skb and xdpf */
165 #define AM65_CPSW_HEADROOM_NA (max(NET_SKB_PAD, XDP_PACKET_HEADROOM) + NET_IP_ALIGN)
166 #define AM65_CPSW_HEADROOM ALIGN(AM65_CPSW_HEADROOM_NA, sizeof(long))
167 
am65_cpsw_port_set_sl_mac(struct am65_cpsw_port * slave,const u8 * dev_addr)168 static void am65_cpsw_port_set_sl_mac(struct am65_cpsw_port *slave,
169 				      const u8 *dev_addr)
170 {
171 	u32 mac_hi = (dev_addr[0] << 0) | (dev_addr[1] << 8) |
172 		     (dev_addr[2] << 16) | (dev_addr[3] << 24);
173 	u32 mac_lo = (dev_addr[4] << 0) | (dev_addr[5] << 8);
174 
175 	writel(mac_hi, slave->port_base + AM65_CPSW_PORTN_REG_SA_H);
176 	writel(mac_lo, slave->port_base + AM65_CPSW_PORTN_REG_SA_L);
177 }
178 
am65_cpsw_sl_ctl_reset(struct am65_cpsw_port * port)179 static void am65_cpsw_sl_ctl_reset(struct am65_cpsw_port *port)
180 {
181 	cpsw_sl_reset(port->slave.mac_sl, 100);
182 	/* Max length register has to be restored after MAC SL reset */
183 	writel(AM65_CPSW_MAX_PACKET_SIZE,
184 	       port->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN);
185 }
186 
am65_cpsw_nuss_get_ver(struct am65_cpsw_common * common)187 static void am65_cpsw_nuss_get_ver(struct am65_cpsw_common *common)
188 {
189 	common->nuss_ver = readl(common->ss_base);
190 	common->cpsw_ver = readl(common->cpsw_base);
191 	dev_info(common->dev,
192 		 "initializing am65 cpsw nuss version 0x%08X, cpsw version 0x%08X Ports: %u quirks:%08x\n",
193 		common->nuss_ver,
194 		common->cpsw_ver,
195 		common->port_num + 1,
196 		common->pdata.quirks);
197 }
198 
am65_cpsw_nuss_ndo_slave_add_vid(struct net_device * ndev,__be16 proto,u16 vid)199 static int am65_cpsw_nuss_ndo_slave_add_vid(struct net_device *ndev,
200 					    __be16 proto, u16 vid)
201 {
202 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
203 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
204 	u32 port_mask, unreg_mcast = 0;
205 	int ret;
206 
207 	if (!common->is_emac_mode)
208 		return 0;
209 
210 	if (!netif_running(ndev) || !vid)
211 		return 0;
212 
213 	ret = pm_runtime_resume_and_get(common->dev);
214 	if (ret < 0)
215 		return ret;
216 
217 	port_mask = BIT(port->port_id) | ALE_PORT_HOST;
218 	if (!vid)
219 		unreg_mcast = port_mask;
220 	dev_info(common->dev, "Adding vlan %d to vlan filter\n", vid);
221 	ret = cpsw_ale_vlan_add_modify(common->ale, vid, port_mask,
222 				       unreg_mcast, port_mask, 0);
223 
224 	pm_runtime_put(common->dev);
225 	return ret;
226 }
227 
am65_cpsw_nuss_ndo_slave_kill_vid(struct net_device * ndev,__be16 proto,u16 vid)228 static int am65_cpsw_nuss_ndo_slave_kill_vid(struct net_device *ndev,
229 					     __be16 proto, u16 vid)
230 {
231 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
232 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
233 	int ret;
234 
235 	if (!common->is_emac_mode)
236 		return 0;
237 
238 	if (!netif_running(ndev) || !vid)
239 		return 0;
240 
241 	ret = pm_runtime_resume_and_get(common->dev);
242 	if (ret < 0)
243 		return ret;
244 
245 	dev_info(common->dev, "Removing vlan %d from vlan filter\n", vid);
246 	ret = cpsw_ale_del_vlan(common->ale, vid,
247 				BIT(port->port_id) | ALE_PORT_HOST);
248 
249 	pm_runtime_put(common->dev);
250 	return ret;
251 }
252 
am65_cpsw_slave_set_promisc(struct am65_cpsw_port * port,bool promisc)253 static void am65_cpsw_slave_set_promisc(struct am65_cpsw_port *port,
254 					bool promisc)
255 {
256 	struct am65_cpsw_common *common = port->common;
257 
258 	if (promisc && !common->is_emac_mode) {
259 		dev_dbg(common->dev, "promisc mode requested in switch mode");
260 		return;
261 	}
262 
263 	if (promisc) {
264 		/* Enable promiscuous mode */
265 		cpsw_ale_control_set(common->ale, port->port_id,
266 				     ALE_PORT_MACONLY_CAF, 1);
267 		dev_dbg(common->dev, "promisc enabled\n");
268 	} else {
269 		/* Disable promiscuous mode */
270 		cpsw_ale_control_set(common->ale, port->port_id,
271 				     ALE_PORT_MACONLY_CAF, 0);
272 		dev_dbg(common->dev, "promisc disabled\n");
273 	}
274 }
275 
am65_cpsw_nuss_ndo_slave_set_rx_mode(struct net_device * ndev)276 static void am65_cpsw_nuss_ndo_slave_set_rx_mode(struct net_device *ndev)
277 {
278 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
279 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
280 	u32 port_mask;
281 	bool promisc;
282 
283 	promisc = !!(ndev->flags & IFF_PROMISC);
284 	am65_cpsw_slave_set_promisc(port, promisc);
285 
286 	if (promisc)
287 		return;
288 
289 	/* Restore allmulti on vlans if necessary */
290 	cpsw_ale_set_allmulti(common->ale,
291 			      ndev->flags & IFF_ALLMULTI, port->port_id);
292 
293 	port_mask = ALE_PORT_HOST;
294 	/* Clear all mcast from ALE */
295 	cpsw_ale_flush_multicast(common->ale, port_mask, -1);
296 
297 	if (!netdev_mc_empty(ndev)) {
298 		struct netdev_hw_addr *ha;
299 
300 		/* program multicast address list into ALE register */
301 		netdev_for_each_mc_addr(ha, ndev) {
302 			cpsw_ale_add_mcast(common->ale, ha->addr,
303 					   port_mask, 0, 0, 0);
304 		}
305 	}
306 }
307 
am65_cpsw_nuss_ndo_host_tx_timeout(struct net_device * ndev,unsigned int txqueue)308 static void am65_cpsw_nuss_ndo_host_tx_timeout(struct net_device *ndev,
309 					       unsigned int txqueue)
310 {
311 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
312 	struct am65_cpsw_tx_chn *tx_chn;
313 	struct netdev_queue *netif_txq;
314 	unsigned long trans_start;
315 
316 	netif_txq = netdev_get_tx_queue(ndev, txqueue);
317 	tx_chn = &common->tx_chns[txqueue];
318 	trans_start = READ_ONCE(netif_txq->trans_start);
319 
320 	netdev_err(ndev, "txq:%d DRV_XOFF:%d tmo:%u dql_avail:%d free_desc:%zu\n",
321 		   txqueue,
322 		   netif_tx_queue_stopped(netif_txq),
323 		   jiffies_to_msecs(jiffies - trans_start),
324 		   netdev_queue_dql_avail(netif_txq),
325 		   k3_cppi_desc_pool_avail(tx_chn->desc_pool));
326 
327 	if (netif_tx_queue_stopped(netif_txq)) {
328 		/* try recover if stopped by us */
329 		txq_trans_update(netif_txq);
330 		netif_tx_wake_queue(netif_txq);
331 	}
332 }
333 
am65_cpsw_nuss_rx_push(struct am65_cpsw_common * common,struct page * page,u32 flow_idx)334 static int am65_cpsw_nuss_rx_push(struct am65_cpsw_common *common,
335 				  struct page *page, u32 flow_idx)
336 {
337 	struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
338 	struct cppi5_host_desc_t *desc_rx;
339 	struct device *dev = common->dev;
340 	struct am65_cpsw_swdata *swdata;
341 	dma_addr_t desc_dma;
342 	dma_addr_t buf_dma;
343 
344 	desc_rx = k3_cppi_desc_pool_alloc(rx_chn->desc_pool);
345 	if (!desc_rx) {
346 		dev_err(dev, "Failed to allocate RXFDQ descriptor\n");
347 		return -ENOMEM;
348 	}
349 	desc_dma = k3_cppi_desc_pool_virt2dma(rx_chn->desc_pool, desc_rx);
350 
351 	buf_dma = dma_map_single(rx_chn->dma_dev,
352 				 page_address(page) + AM65_CPSW_HEADROOM,
353 				 AM65_CPSW_MAX_PACKET_SIZE, DMA_FROM_DEVICE);
354 	if (unlikely(dma_mapping_error(rx_chn->dma_dev, buf_dma))) {
355 		k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx);
356 		dev_err(dev, "Failed to map rx buffer\n");
357 		return -EINVAL;
358 	}
359 
360 	cppi5_hdesc_init(desc_rx, CPPI5_INFO0_HDESC_EPIB_PRESENT,
361 			 AM65_CPSW_NAV_PS_DATA_SIZE);
362 	k3_udma_glue_rx_dma_to_cppi5_addr(rx_chn->rx_chn, &buf_dma);
363 	cppi5_hdesc_attach_buf(desc_rx, buf_dma, AM65_CPSW_MAX_PACKET_SIZE,
364 			       buf_dma, AM65_CPSW_MAX_PACKET_SIZE);
365 	swdata = cppi5_hdesc_get_swdata(desc_rx);
366 	swdata->page = page;
367 	swdata->flow_id = flow_idx;
368 
369 	return k3_udma_glue_push_rx_chn(rx_chn->rx_chn, flow_idx,
370 					desc_rx, desc_dma);
371 }
372 
am65_cpsw_nuss_set_p0_ptype(struct am65_cpsw_common * common)373 void am65_cpsw_nuss_set_p0_ptype(struct am65_cpsw_common *common)
374 {
375 	struct am65_cpsw_host *host_p = am65_common_get_host(common);
376 	u32 val, pri_map;
377 
378 	/* P0 set Receive Priority Type */
379 	val = readl(host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL);
380 
381 	if (common->pf_p0_rx_ptype_rrobin) {
382 		val |= AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN;
383 		/* Enet Ports fifos works in fixed priority mode only, so
384 		 * reset P0_Rx_Pri_Map so all packet will go in Enet fifo 0
385 		 */
386 		pri_map = 0x0;
387 	} else {
388 		val &= ~AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN;
389 		/* restore P0_Rx_Pri_Map */
390 		pri_map = 0x76543210;
391 	}
392 
393 	writel(pri_map, host_p->port_base + AM65_CPSW_PORT_REG_RX_PRI_MAP);
394 	writel(val, host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL);
395 }
396 
397 static void am65_cpsw_init_host_port_switch(struct am65_cpsw_common *common);
398 static void am65_cpsw_init_host_port_emac(struct am65_cpsw_common *common);
399 static void am65_cpsw_init_port_switch_ale(struct am65_cpsw_port *port);
400 static void am65_cpsw_init_port_emac_ale(struct am65_cpsw_port *port);
401 
am65_cpsw_destroy_xdp_rxqs(struct am65_cpsw_common * common)402 static void am65_cpsw_destroy_xdp_rxqs(struct am65_cpsw_common *common)
403 {
404 	struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
405 	struct am65_cpsw_rx_flow *flow;
406 	struct xdp_rxq_info *rxq;
407 	int id, port;
408 
409 	for (id = 0; id < common->rx_ch_num_flows; id++) {
410 		flow = &rx_chn->flows[id];
411 
412 		for (port = 0; port < common->port_num; port++) {
413 			if (!common->ports[port].ndev)
414 				continue;
415 
416 			rxq = &common->ports[port].xdp_rxq[id];
417 
418 			if (xdp_rxq_info_is_reg(rxq))
419 				xdp_rxq_info_unreg(rxq);
420 		}
421 
422 		if (flow->page_pool) {
423 			page_pool_destroy(flow->page_pool);
424 			flow->page_pool = NULL;
425 		}
426 	}
427 }
428 
am65_cpsw_create_xdp_rxqs(struct am65_cpsw_common * common)429 static int am65_cpsw_create_xdp_rxqs(struct am65_cpsw_common *common)
430 {
431 	struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
432 	struct page_pool_params pp_params = {
433 		.flags = PP_FLAG_DMA_MAP,
434 		.order = 0,
435 		.pool_size = AM65_CPSW_MAX_RX_DESC,
436 		.nid = dev_to_node(common->dev),
437 		.dev = common->dev,
438 		.dma_dir = DMA_BIDIRECTIONAL,
439 		/* .napi set dynamically */
440 	};
441 	struct am65_cpsw_rx_flow *flow;
442 	struct xdp_rxq_info *rxq;
443 	struct page_pool *pool;
444 	int id, port, ret;
445 
446 	for (id = 0; id < common->rx_ch_num_flows; id++) {
447 		flow = &rx_chn->flows[id];
448 		pp_params.napi = &flow->napi_rx;
449 		pool = page_pool_create(&pp_params);
450 		if (IS_ERR(pool)) {
451 			ret = PTR_ERR(pool);
452 			goto err;
453 		}
454 
455 		flow->page_pool = pool;
456 
457 		/* using same page pool is allowed as no running rx handlers
458 		 * simultaneously for both ndevs
459 		 */
460 		for (port = 0; port < common->port_num; port++) {
461 			if (!common->ports[port].ndev)
462 				continue;
463 
464 			rxq = &common->ports[port].xdp_rxq[id];
465 
466 			ret = xdp_rxq_info_reg(rxq, common->ports[port].ndev,
467 					       id, flow->napi_rx.napi_id);
468 			if (ret)
469 				goto err;
470 
471 			ret = xdp_rxq_info_reg_mem_model(rxq,
472 							 MEM_TYPE_PAGE_POOL,
473 							 pool);
474 			if (ret)
475 				goto err;
476 		}
477 	}
478 
479 	return 0;
480 
481 err:
482 	am65_cpsw_destroy_xdp_rxqs(common);
483 	return ret;
484 }
485 
am65_cpsw_nuss_desc_idx(struct k3_cppi_desc_pool * desc_pool,void * desc,unsigned char dsize_log2)486 static int am65_cpsw_nuss_desc_idx(struct k3_cppi_desc_pool *desc_pool,
487 				   void *desc,
488 				   unsigned char dsize_log2)
489 {
490 	void *pool_addr = k3_cppi_desc_pool_cpuaddr(desc_pool);
491 
492 	return (desc - pool_addr) >> dsize_log2;
493 }
494 
am65_cpsw_nuss_set_buf_type(struct am65_cpsw_tx_chn * tx_chn,struct cppi5_host_desc_t * desc,enum am65_cpsw_tx_buf_type buf_type)495 static void am65_cpsw_nuss_set_buf_type(struct am65_cpsw_tx_chn *tx_chn,
496 					struct cppi5_host_desc_t *desc,
497 					enum am65_cpsw_tx_buf_type buf_type)
498 {
499 	int desc_idx;
500 
501 	desc_idx = am65_cpsw_nuss_desc_idx(tx_chn->desc_pool, desc,
502 					   tx_chn->dsize_log2);
503 	k3_cppi_desc_pool_desc_info_set(tx_chn->desc_pool, desc_idx,
504 					(void *)buf_type);
505 }
506 
am65_cpsw_nuss_buf_type(struct am65_cpsw_tx_chn * tx_chn,dma_addr_t desc_dma)507 static enum am65_cpsw_tx_buf_type am65_cpsw_nuss_buf_type(struct am65_cpsw_tx_chn *tx_chn,
508 							  dma_addr_t desc_dma)
509 {
510 	struct cppi5_host_desc_t *desc_tx;
511 	int desc_idx;
512 
513 	desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool, desc_dma);
514 	desc_idx = am65_cpsw_nuss_desc_idx(tx_chn->desc_pool, desc_tx,
515 					   tx_chn->dsize_log2);
516 
517 	return (enum am65_cpsw_tx_buf_type)k3_cppi_desc_pool_desc_info(tx_chn->desc_pool,
518 								       desc_idx);
519 }
520 
am65_cpsw_put_page(struct am65_cpsw_rx_flow * flow,struct page * page,bool allow_direct)521 static inline void am65_cpsw_put_page(struct am65_cpsw_rx_flow *flow,
522 				      struct page *page,
523 				      bool allow_direct)
524 {
525 	page_pool_put_full_page(flow->page_pool, page, allow_direct);
526 }
527 
am65_cpsw_nuss_rx_cleanup(void * data,dma_addr_t desc_dma)528 static void am65_cpsw_nuss_rx_cleanup(void *data, dma_addr_t desc_dma)
529 {
530 	struct am65_cpsw_rx_chn *rx_chn = data;
531 	struct cppi5_host_desc_t *desc_rx;
532 	struct am65_cpsw_swdata *swdata;
533 	dma_addr_t buf_dma;
534 	struct page *page;
535 	u32 buf_dma_len;
536 	u32 flow_id;
537 
538 	desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma);
539 	swdata = cppi5_hdesc_get_swdata(desc_rx);
540 	page = swdata->page;
541 	flow_id = swdata->flow_id;
542 	cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len);
543 	k3_udma_glue_rx_cppi5_to_dma_addr(rx_chn->rx_chn, &buf_dma);
544 	dma_unmap_single(rx_chn->dma_dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE);
545 	k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx);
546 
547 	am65_cpsw_put_page(&rx_chn->flows[flow_id], page, false);
548 }
549 
am65_cpsw_nuss_xmit_free(struct am65_cpsw_tx_chn * tx_chn,struct cppi5_host_desc_t * desc)550 static void am65_cpsw_nuss_xmit_free(struct am65_cpsw_tx_chn *tx_chn,
551 				     struct cppi5_host_desc_t *desc)
552 {
553 	struct cppi5_host_desc_t *first_desc, *next_desc;
554 	dma_addr_t buf_dma, next_desc_dma;
555 	u32 buf_dma_len;
556 
557 	first_desc = desc;
558 	next_desc = first_desc;
559 
560 	cppi5_hdesc_get_obuf(first_desc, &buf_dma, &buf_dma_len);
561 	k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &buf_dma);
562 
563 	dma_unmap_single(tx_chn->dma_dev, buf_dma, buf_dma_len, DMA_TO_DEVICE);
564 
565 	next_desc_dma = cppi5_hdesc_get_next_hbdesc(first_desc);
566 	k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &next_desc_dma);
567 	while (next_desc_dma) {
568 		next_desc = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool,
569 						       next_desc_dma);
570 		cppi5_hdesc_get_obuf(next_desc, &buf_dma, &buf_dma_len);
571 		k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &buf_dma);
572 
573 		dma_unmap_page(tx_chn->dma_dev, buf_dma, buf_dma_len,
574 			       DMA_TO_DEVICE);
575 
576 		next_desc_dma = cppi5_hdesc_get_next_hbdesc(next_desc);
577 		k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &next_desc_dma);
578 
579 		k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc);
580 	}
581 
582 	k3_cppi_desc_pool_free(tx_chn->desc_pool, first_desc);
583 }
584 
am65_cpsw_nuss_tx_cleanup(void * data,dma_addr_t desc_dma)585 static void am65_cpsw_nuss_tx_cleanup(void *data, dma_addr_t desc_dma)
586 {
587 	struct am65_cpsw_tx_chn *tx_chn = data;
588 	enum am65_cpsw_tx_buf_type buf_type;
589 	struct cppi5_host_desc_t *desc_tx;
590 	struct xdp_frame *xdpf;
591 	struct sk_buff *skb;
592 	void **swdata;
593 
594 	desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool, desc_dma);
595 	swdata = cppi5_hdesc_get_swdata(desc_tx);
596 	buf_type = am65_cpsw_nuss_buf_type(tx_chn, desc_dma);
597 	if (buf_type == AM65_CPSW_TX_BUF_TYPE_SKB) {
598 		skb = *(swdata);
599 		dev_kfree_skb_any(skb);
600 	} else {
601 		xdpf = *(swdata);
602 		xdp_return_frame(xdpf);
603 	}
604 
605 	am65_cpsw_nuss_xmit_free(tx_chn, desc_tx);
606 }
607 
am65_cpsw_build_skb(void * page_addr,struct net_device * ndev,unsigned int len,unsigned int headroom)608 static struct sk_buff *am65_cpsw_build_skb(void *page_addr,
609 					   struct net_device *ndev,
610 					   unsigned int len,
611 					   unsigned int headroom)
612 {
613 	struct sk_buff *skb;
614 
615 	skb = build_skb(page_addr, len);
616 	if (unlikely(!skb))
617 		return NULL;
618 
619 	skb_reserve(skb, headroom);
620 	skb->dev = ndev;
621 
622 	return skb;
623 }
624 
am65_cpsw_nuss_common_open(struct am65_cpsw_common * common)625 static int am65_cpsw_nuss_common_open(struct am65_cpsw_common *common)
626 {
627 	struct am65_cpsw_host *host_p = am65_common_get_host(common);
628 	struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
629 	struct am65_cpsw_tx_chn *tx_chn = common->tx_chns;
630 	int port_idx, i, ret, tx, flow_idx;
631 	struct am65_cpsw_rx_flow *flow;
632 	u32 val, port_mask;
633 	struct page *page;
634 
635 	if (common->usage_count)
636 		return 0;
637 
638 	/* Control register */
639 	writel(AM65_CPSW_CTL_P0_ENABLE | AM65_CPSW_CTL_P0_TX_CRC_REMOVE |
640 	       AM65_CPSW_CTL_VLAN_AWARE | AM65_CPSW_CTL_P0_RX_PAD,
641 	       common->cpsw_base + AM65_CPSW_REG_CTL);
642 	/* Max length register */
643 	writel(AM65_CPSW_MAX_PACKET_SIZE,
644 	       host_p->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN);
645 	/* set base flow_id */
646 	writel(common->rx_flow_id_base,
647 	       host_p->port_base + AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET);
648 	writel(AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN | AM65_CPSW_P0_REG_CTL_RX_REMAP_VLAN,
649 	       host_p->port_base + AM65_CPSW_P0_REG_CTL);
650 
651 	am65_cpsw_nuss_set_p0_ptype(common);
652 
653 	/* enable statistic */
654 	val = BIT(HOST_PORT_NUM);
655 	for (port_idx = 0; port_idx < common->port_num; port_idx++) {
656 		struct am65_cpsw_port *port = &common->ports[port_idx];
657 
658 		if (!port->disabled)
659 			val |=  BIT(port->port_id);
660 	}
661 	writel(val, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN);
662 
663 	/* disable priority elevation */
664 	writel(0, common->cpsw_base + AM65_CPSW_REG_PTYPE);
665 
666 	cpsw_ale_start(common->ale);
667 
668 	/* limit to one RX flow only */
669 	cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
670 			     ALE_DEFAULT_THREAD_ID, 0);
671 	cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
672 			     ALE_DEFAULT_THREAD_ENABLE, 1);
673 	/* switch to vlan unaware mode */
674 	cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_VLAN_AWARE, 1);
675 	cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
676 			     ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
677 
678 	/* default vlan cfg: create mask based on enabled ports */
679 	port_mask = GENMASK(common->port_num, 0) &
680 		    ~common->disabled_ports_mask;
681 
682 	cpsw_ale_add_vlan(common->ale, 0, port_mask,
683 			  port_mask, port_mask,
684 			  port_mask & ~ALE_PORT_HOST);
685 
686 	if (common->is_emac_mode)
687 		am65_cpsw_init_host_port_emac(common);
688 	else
689 		am65_cpsw_init_host_port_switch(common);
690 
691 	am65_cpsw_qos_tx_p0_rate_init(common);
692 
693 	ret = am65_cpsw_create_xdp_rxqs(common);
694 	if (ret) {
695 		dev_err(common->dev, "Failed to create XDP rx queues\n");
696 		return ret;
697 	}
698 
699 	for (flow_idx = 0; flow_idx < common->rx_ch_num_flows; flow_idx++) {
700 		flow = &rx_chn->flows[flow_idx];
701 		for (i = 0; i < AM65_CPSW_MAX_RX_DESC; i++) {
702 			page = page_pool_dev_alloc_pages(flow->page_pool);
703 			if (!page) {
704 				dev_err(common->dev, "cannot allocate page in flow %d\n",
705 					flow_idx);
706 				ret = -ENOMEM;
707 				goto fail_rx;
708 			}
709 
710 			ret = am65_cpsw_nuss_rx_push(common, page, flow_idx);
711 			if (ret < 0) {
712 				dev_err(common->dev,
713 					"cannot submit page to rx channel flow %d, error %d\n",
714 					flow_idx, ret);
715 				am65_cpsw_put_page(flow, page, false);
716 				goto fail_rx;
717 			}
718 		}
719 	}
720 
721 	ret = k3_udma_glue_enable_rx_chn(rx_chn->rx_chn);
722 	if (ret) {
723 		dev_err(common->dev, "couldn't enable rx chn: %d\n", ret);
724 		goto fail_rx;
725 	}
726 
727 	for (i = 0; i < common->rx_ch_num_flows ; i++) {
728 		napi_enable(&rx_chn->flows[i].napi_rx);
729 		if (rx_chn->flows[i].irq_disabled) {
730 			rx_chn->flows[i].irq_disabled = false;
731 			enable_irq(rx_chn->flows[i].irq);
732 		}
733 	}
734 
735 	for (tx = 0; tx < common->tx_ch_num; tx++) {
736 		ret = k3_udma_glue_enable_tx_chn(tx_chn[tx].tx_chn);
737 		if (ret) {
738 			dev_err(common->dev, "couldn't enable tx chn %d: %d\n",
739 				tx, ret);
740 			tx--;
741 			goto fail_tx;
742 		}
743 		napi_enable(&tx_chn[tx].napi_tx);
744 	}
745 
746 	dev_dbg(common->dev, "cpsw_nuss started\n");
747 	return 0;
748 
749 fail_tx:
750 	while (tx >= 0) {
751 		napi_disable(&tx_chn[tx].napi_tx);
752 		k3_udma_glue_disable_tx_chn(tx_chn[tx].tx_chn);
753 		tx--;
754 	}
755 
756 	for (flow_idx = 0; i < common->rx_ch_num_flows; flow_idx++) {
757 		flow = &rx_chn->flows[flow_idx];
758 		if (!flow->irq_disabled) {
759 			disable_irq(flow->irq);
760 			flow->irq_disabled = true;
761 		}
762 		napi_disable(&flow->napi_rx);
763 	}
764 
765 	k3_udma_glue_disable_rx_chn(rx_chn->rx_chn);
766 
767 fail_rx:
768 	for (i = 0; i < common->rx_ch_num_flows; i++)
769 		k3_udma_glue_reset_rx_chn(rx_chn->rx_chn, i, rx_chn,
770 					  am65_cpsw_nuss_rx_cleanup, !!i);
771 
772 	am65_cpsw_destroy_xdp_rxqs(common);
773 
774 	return ret;
775 }
776 
am65_cpsw_nuss_common_stop(struct am65_cpsw_common * common)777 static int am65_cpsw_nuss_common_stop(struct am65_cpsw_common *common)
778 {
779 	struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
780 	struct am65_cpsw_tx_chn *tx_chn = common->tx_chns;
781 	int i;
782 
783 	if (common->usage_count != 1)
784 		return 0;
785 
786 	cpsw_ale_control_set(common->ale, HOST_PORT_NUM,
787 			     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
788 
789 	/* shutdown tx channels */
790 	atomic_set(&common->tdown_cnt, common->tx_ch_num);
791 	/* ensure new tdown_cnt value is visible */
792 	smp_mb__after_atomic();
793 	reinit_completion(&common->tdown_complete);
794 
795 	for (i = 0; i < common->tx_ch_num; i++)
796 		k3_udma_glue_tdown_tx_chn(tx_chn[i].tx_chn, false);
797 
798 	i = wait_for_completion_timeout(&common->tdown_complete,
799 					msecs_to_jiffies(1000));
800 	if (!i)
801 		dev_err(common->dev, "tx timeout\n");
802 	for (i = 0; i < common->tx_ch_num; i++) {
803 		napi_disable(&tx_chn[i].napi_tx);
804 		hrtimer_cancel(&tx_chn[i].tx_hrtimer);
805 	}
806 
807 	for (i = 0; i < common->tx_ch_num; i++) {
808 		k3_udma_glue_reset_tx_chn(tx_chn[i].tx_chn, &tx_chn[i],
809 					  am65_cpsw_nuss_tx_cleanup);
810 		k3_udma_glue_disable_tx_chn(tx_chn[i].tx_chn);
811 	}
812 
813 	reinit_completion(&common->tdown_complete);
814 	k3_udma_glue_tdown_rx_chn(rx_chn->rx_chn, true);
815 
816 	if (common->pdata.quirks & AM64_CPSW_QUIRK_DMA_RX_TDOWN_IRQ) {
817 		i = wait_for_completion_timeout(&common->tdown_complete, msecs_to_jiffies(1000));
818 		if (!i)
819 			dev_err(common->dev, "rx teardown timeout\n");
820 	}
821 
822 	for (i = common->rx_ch_num_flows - 1; i >= 0; i--) {
823 		napi_disable(&rx_chn->flows[i].napi_rx);
824 		hrtimer_cancel(&rx_chn->flows[i].rx_hrtimer);
825 		k3_udma_glue_reset_rx_chn(rx_chn->rx_chn, i, rx_chn,
826 					  am65_cpsw_nuss_rx_cleanup, !!i);
827 	}
828 
829 	k3_udma_glue_disable_rx_chn(rx_chn->rx_chn);
830 
831 	cpsw_ale_stop(common->ale);
832 
833 	writel(0, common->cpsw_base + AM65_CPSW_REG_CTL);
834 	writel(0, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN);
835 
836 	am65_cpsw_destroy_xdp_rxqs(common);
837 
838 	dev_dbg(common->dev, "cpsw_nuss stopped\n");
839 	return 0;
840 }
841 
am65_cpsw_nuss_ndo_slave_stop(struct net_device * ndev)842 static int am65_cpsw_nuss_ndo_slave_stop(struct net_device *ndev)
843 {
844 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
845 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
846 	int ret;
847 
848 	phylink_stop(port->slave.phylink);
849 
850 	netif_tx_stop_all_queues(ndev);
851 
852 	phylink_disconnect_phy(port->slave.phylink);
853 
854 	ret = am65_cpsw_nuss_common_stop(common);
855 	if (ret)
856 		return ret;
857 
858 	common->usage_count--;
859 	pm_runtime_put(common->dev);
860 	return 0;
861 }
862 
cpsw_restore_vlans(struct net_device * vdev,int vid,void * arg)863 static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg)
864 {
865 	struct am65_cpsw_port *port = arg;
866 
867 	if (!vdev)
868 		return 0;
869 
870 	return am65_cpsw_nuss_ndo_slave_add_vid(port->ndev, 0, vid);
871 }
872 
am65_cpsw_nuss_ndo_slave_open(struct net_device * ndev)873 static int am65_cpsw_nuss_ndo_slave_open(struct net_device *ndev)
874 {
875 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
876 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
877 	int ret, i;
878 	u32 reg;
879 
880 	ret = pm_runtime_resume_and_get(common->dev);
881 	if (ret < 0)
882 		return ret;
883 
884 	/* Idle MAC port */
885 	cpsw_sl_ctl_set(port->slave.mac_sl, CPSW_SL_CTL_CMD_IDLE);
886 	cpsw_sl_wait_for_idle(port->slave.mac_sl, 100);
887 	cpsw_sl_ctl_reset(port->slave.mac_sl);
888 
889 	/* soft reset MAC */
890 	cpsw_sl_reg_write(port->slave.mac_sl, CPSW_SL_SOFT_RESET, 1);
891 	mdelay(1);
892 	reg = cpsw_sl_reg_read(port->slave.mac_sl, CPSW_SL_SOFT_RESET);
893 	if (reg) {
894 		dev_err(common->dev, "soft RESET didn't complete\n");
895 		ret = -ETIMEDOUT;
896 		goto runtime_put;
897 	}
898 
899 	/* Notify the stack of the actual queue counts. */
900 	ret = netif_set_real_num_tx_queues(ndev, common->tx_ch_num);
901 	if (ret) {
902 		dev_err(common->dev, "cannot set real number of tx queues\n");
903 		goto runtime_put;
904 	}
905 
906 	ret = netif_set_real_num_rx_queues(ndev, common->rx_ch_num_flows);
907 	if (ret) {
908 		dev_err(common->dev, "cannot set real number of rx queues\n");
909 		goto runtime_put;
910 	}
911 
912 	for (i = 0; i < common->tx_ch_num; i++) {
913 		struct netdev_queue *txq = netdev_get_tx_queue(ndev, i);
914 
915 		netdev_tx_reset_queue(txq);
916 		txq->tx_maxrate =  common->tx_chns[i].rate_mbps;
917 	}
918 
919 	ret = am65_cpsw_nuss_common_open(common);
920 	if (ret)
921 		goto runtime_put;
922 
923 	common->usage_count++;
924 
925 	am65_cpsw_port_set_sl_mac(port, ndev->dev_addr);
926 
927 	if (common->is_emac_mode)
928 		am65_cpsw_init_port_emac_ale(port);
929 	else
930 		am65_cpsw_init_port_switch_ale(port);
931 
932 	/* mac_sl should be configured via phy-link interface */
933 	am65_cpsw_sl_ctl_reset(port);
934 
935 	ret = phylink_of_phy_connect(port->slave.phylink, port->slave.port_np, 0);
936 	if (ret)
937 		goto error_cleanup;
938 
939 	/* restore vlan configurations */
940 	vlan_for_each(ndev, cpsw_restore_vlans, port);
941 
942 	phylink_start(port->slave.phylink);
943 
944 	return 0;
945 
946 error_cleanup:
947 	am65_cpsw_nuss_ndo_slave_stop(ndev);
948 	return ret;
949 
950 runtime_put:
951 	pm_runtime_put(common->dev);
952 	return ret;
953 }
954 
am65_cpsw_xdp_tx_frame(struct net_device * ndev,struct am65_cpsw_tx_chn * tx_chn,struct xdp_frame * xdpf,enum am65_cpsw_tx_buf_type buf_type)955 static int am65_cpsw_xdp_tx_frame(struct net_device *ndev,
956 				  struct am65_cpsw_tx_chn *tx_chn,
957 				  struct xdp_frame *xdpf,
958 				  enum am65_cpsw_tx_buf_type buf_type)
959 {
960 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
961 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
962 	struct cppi5_host_desc_t *host_desc;
963 	struct netdev_queue *netif_txq;
964 	dma_addr_t dma_desc, dma_buf;
965 	u32 pkt_len = xdpf->len;
966 	void **swdata;
967 	int ret;
968 
969 	host_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool);
970 	if (unlikely(!host_desc)) {
971 		ndev->stats.tx_dropped++;
972 		return AM65_CPSW_XDP_CONSUMED;	/* drop */
973 	}
974 
975 	am65_cpsw_nuss_set_buf_type(tx_chn, host_desc, buf_type);
976 
977 	dma_buf = dma_map_single(tx_chn->dma_dev, xdpf->data,
978 				 pkt_len, DMA_TO_DEVICE);
979 	if (unlikely(dma_mapping_error(tx_chn->dma_dev, dma_buf))) {
980 		ndev->stats.tx_dropped++;
981 		ret = AM65_CPSW_XDP_CONSUMED;	/* drop */
982 		goto pool_free;
983 	}
984 
985 	cppi5_hdesc_init(host_desc, CPPI5_INFO0_HDESC_EPIB_PRESENT,
986 			 AM65_CPSW_NAV_PS_DATA_SIZE);
987 	cppi5_hdesc_set_pkttype(host_desc, AM65_CPSW_CPPI_TX_PKT_TYPE);
988 	cppi5_hdesc_set_pktlen(host_desc, pkt_len);
989 	cppi5_desc_set_pktids(&host_desc->hdr, 0, AM65_CPSW_CPPI_TX_FLOW_ID);
990 	cppi5_desc_set_tags_ids(&host_desc->hdr, 0, port->port_id);
991 
992 	k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &dma_buf);
993 	cppi5_hdesc_attach_buf(host_desc, dma_buf, pkt_len, dma_buf, pkt_len);
994 
995 	swdata = cppi5_hdesc_get_swdata(host_desc);
996 	*(swdata) = xdpf;
997 
998 	/* Report BQL before sending the packet */
999 	netif_txq = netdev_get_tx_queue(ndev, tx_chn->id);
1000 	netdev_tx_sent_queue(netif_txq, pkt_len);
1001 
1002 	dma_desc = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool, host_desc);
1003 	if (AM65_CPSW_IS_CPSW2G(common)) {
1004 		ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, host_desc,
1005 					       dma_desc);
1006 	} else {
1007 		spin_lock_bh(&tx_chn->lock);
1008 		ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, host_desc,
1009 					       dma_desc);
1010 		spin_unlock_bh(&tx_chn->lock);
1011 	}
1012 	if (ret) {
1013 		/* Inform BQL */
1014 		netdev_tx_completed_queue(netif_txq, 1, pkt_len);
1015 		ndev->stats.tx_errors++;
1016 		ret = AM65_CPSW_XDP_CONSUMED; /* drop */
1017 		goto dma_unmap;
1018 	}
1019 
1020 	return 0;
1021 
1022 dma_unmap:
1023 	k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &dma_buf);
1024 	dma_unmap_single(tx_chn->dma_dev, dma_buf, pkt_len, DMA_TO_DEVICE);
1025 pool_free:
1026 	k3_cppi_desc_pool_free(tx_chn->desc_pool, host_desc);
1027 	return ret;
1028 }
1029 
am65_cpsw_run_xdp(struct am65_cpsw_rx_flow * flow,struct am65_cpsw_port * port,struct xdp_buff * xdp,int cpu,int * len)1030 static int am65_cpsw_run_xdp(struct am65_cpsw_rx_flow *flow,
1031 			     struct am65_cpsw_port *port,
1032 			     struct xdp_buff *xdp,
1033 			     int cpu, int *len)
1034 {
1035 	struct am65_cpsw_common *common = flow->common;
1036 	struct am65_cpsw_ndev_priv *ndev_priv;
1037 	struct net_device *ndev = port->ndev;
1038 	struct am65_cpsw_ndev_stats *stats;
1039 	int ret = AM65_CPSW_XDP_CONSUMED;
1040 	struct am65_cpsw_tx_chn *tx_chn;
1041 	struct netdev_queue *netif_txq;
1042 	struct xdp_frame *xdpf;
1043 	struct bpf_prog *prog;
1044 	struct page *page;
1045 	u32 act;
1046 	int err;
1047 
1048 	prog = READ_ONCE(port->xdp_prog);
1049 	if (!prog)
1050 		return AM65_CPSW_XDP_PASS;
1051 
1052 	act = bpf_prog_run_xdp(prog, xdp);
1053 	/* XDP prog might have changed packet data and boundaries */
1054 	*len = xdp->data_end - xdp->data;
1055 
1056 	ndev_priv = netdev_priv(ndev);
1057 	stats = this_cpu_ptr(ndev_priv->stats);
1058 
1059 	switch (act) {
1060 	case XDP_PASS:
1061 		ret = AM65_CPSW_XDP_PASS;
1062 		goto out;
1063 	case XDP_TX:
1064 		tx_chn = &common->tx_chns[cpu % AM65_CPSW_MAX_QUEUES];
1065 		netif_txq = netdev_get_tx_queue(ndev, tx_chn->id);
1066 
1067 		xdpf = xdp_convert_buff_to_frame(xdp);
1068 		if (unlikely(!xdpf))
1069 			goto drop;
1070 
1071 		__netif_tx_lock(netif_txq, cpu);
1072 		err = am65_cpsw_xdp_tx_frame(ndev, tx_chn, xdpf,
1073 					     AM65_CPSW_TX_BUF_TYPE_XDP_TX);
1074 		__netif_tx_unlock(netif_txq);
1075 		if (err)
1076 			goto drop;
1077 
1078 		u64_stats_update_begin(&stats->syncp);
1079 		stats->rx_bytes += *len;
1080 		stats->rx_packets++;
1081 		u64_stats_update_end(&stats->syncp);
1082 		ret = AM65_CPSW_XDP_CONSUMED;
1083 		goto out;
1084 	case XDP_REDIRECT:
1085 		if (unlikely(xdp_do_redirect(ndev, xdp, prog)))
1086 			goto drop;
1087 
1088 		u64_stats_update_begin(&stats->syncp);
1089 		stats->rx_bytes += *len;
1090 		stats->rx_packets++;
1091 		u64_stats_update_end(&stats->syncp);
1092 		ret = AM65_CPSW_XDP_REDIRECT;
1093 		goto out;
1094 	default:
1095 		bpf_warn_invalid_xdp_action(ndev, prog, act);
1096 		fallthrough;
1097 	case XDP_ABORTED:
1098 drop:
1099 		trace_xdp_exception(ndev, prog, act);
1100 		fallthrough;
1101 	case XDP_DROP:
1102 		ndev->stats.rx_dropped++;
1103 	}
1104 
1105 	page = virt_to_head_page(xdp->data);
1106 	am65_cpsw_put_page(flow, page, true);
1107 
1108 out:
1109 	return ret;
1110 }
1111 
1112 /* RX psdata[2] word format - checksum information */
1113 #define AM65_CPSW_RX_PSD_CSUM_ADD	GENMASK(15, 0)
1114 #define AM65_CPSW_RX_PSD_CSUM_ERR	BIT(16)
1115 #define AM65_CPSW_RX_PSD_IS_FRAGMENT	BIT(17)
1116 #define AM65_CPSW_RX_PSD_IS_TCP		BIT(18)
1117 #define AM65_CPSW_RX_PSD_IPV6_VALID	BIT(19)
1118 #define AM65_CPSW_RX_PSD_IPV4_VALID	BIT(20)
1119 
am65_cpsw_nuss_rx_csum(struct sk_buff * skb,u32 csum_info)1120 static void am65_cpsw_nuss_rx_csum(struct sk_buff *skb, u32 csum_info)
1121 {
1122 	/* HW can verify IPv4/IPv6 TCP/UDP packets checksum
1123 	 * csum information provides in psdata[2] word:
1124 	 * AM65_CPSW_RX_PSD_CSUM_ERR bit - indicates csum error
1125 	 * AM65_CPSW_RX_PSD_IPV6_VALID and AM65_CPSW_RX_PSD_IPV4_VALID
1126 	 * bits - indicates IPv4/IPv6 packet
1127 	 * AM65_CPSW_RX_PSD_IS_FRAGMENT bit - indicates fragmented packet
1128 	 * AM65_CPSW_RX_PSD_CSUM_ADD has value 0xFFFF for non fragmented packets
1129 	 * or csum value for fragmented packets if !AM65_CPSW_RX_PSD_CSUM_ERR
1130 	 */
1131 	skb_checksum_none_assert(skb);
1132 
1133 	if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM)))
1134 		return;
1135 
1136 	if ((csum_info & (AM65_CPSW_RX_PSD_IPV6_VALID |
1137 			  AM65_CPSW_RX_PSD_IPV4_VALID)) &&
1138 			  !(csum_info & AM65_CPSW_RX_PSD_CSUM_ERR)) {
1139 		/* csum for fragmented packets is unsupported */
1140 		if (!(csum_info & AM65_CPSW_RX_PSD_IS_FRAGMENT))
1141 			skb->ip_summed = CHECKSUM_UNNECESSARY;
1142 	}
1143 }
1144 
am65_cpsw_nuss_rx_packets(struct am65_cpsw_rx_flow * flow,int cpu,int * xdp_state)1145 static int am65_cpsw_nuss_rx_packets(struct am65_cpsw_rx_flow *flow,
1146 				     int cpu, int *xdp_state)
1147 {
1148 	struct am65_cpsw_rx_chn *rx_chn = &flow->common->rx_chns;
1149 	u32 buf_dma_len, pkt_len, port_id = 0, csum_info;
1150 	struct am65_cpsw_common *common = flow->common;
1151 	struct am65_cpsw_ndev_priv *ndev_priv;
1152 	struct am65_cpsw_ndev_stats *stats;
1153 	struct cppi5_host_desc_t *desc_rx;
1154 	struct device *dev = common->dev;
1155 	struct am65_cpsw_swdata *swdata;
1156 	struct page *page, *new_page;
1157 	dma_addr_t desc_dma, buf_dma;
1158 	struct am65_cpsw_port *port;
1159 	struct net_device *ndev;
1160 	u32 flow_idx = flow->id;
1161 	struct sk_buff *skb;
1162 	struct xdp_buff	xdp;
1163 	int headroom, ret;
1164 	void *page_addr;
1165 	u32 *psdata;
1166 
1167 	*xdp_state = AM65_CPSW_XDP_PASS;
1168 	ret = k3_udma_glue_pop_rx_chn(rx_chn->rx_chn, flow_idx, &desc_dma);
1169 	if (ret) {
1170 		if (ret != -ENODATA)
1171 			dev_err(dev, "RX: pop chn fail %d\n", ret);
1172 		return ret;
1173 	}
1174 
1175 	if (cppi5_desc_is_tdcm(desc_dma)) {
1176 		dev_dbg(dev, "%s RX tdown flow: %u\n", __func__, flow_idx);
1177 		if (common->pdata.quirks & AM64_CPSW_QUIRK_DMA_RX_TDOWN_IRQ)
1178 			complete(&common->tdown_complete);
1179 		return 0;
1180 	}
1181 
1182 	desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma);
1183 	dev_dbg(dev, "%s flow_idx: %u desc %pad\n",
1184 		__func__, flow_idx, &desc_dma);
1185 
1186 	swdata = cppi5_hdesc_get_swdata(desc_rx);
1187 	page = swdata->page;
1188 	page_addr = page_address(page);
1189 	cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len);
1190 	k3_udma_glue_rx_cppi5_to_dma_addr(rx_chn->rx_chn, &buf_dma);
1191 	pkt_len = cppi5_hdesc_get_pktlen(desc_rx);
1192 	cppi5_desc_get_tags_ids(&desc_rx->hdr, &port_id, NULL);
1193 	dev_dbg(dev, "%s rx port_id:%d\n", __func__, port_id);
1194 	port = am65_common_get_port(common, port_id);
1195 	ndev = port->ndev;
1196 	psdata = cppi5_hdesc_get_psdata(desc_rx);
1197 	csum_info = psdata[2];
1198 	dev_dbg(dev, "%s rx csum_info:%#x\n", __func__, csum_info);
1199 
1200 	dma_unmap_single(rx_chn->dma_dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE);
1201 	k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx);
1202 
1203 	if (port->xdp_prog) {
1204 		xdp_init_buff(&xdp, PAGE_SIZE, &port->xdp_rxq[flow->id]);
1205 		xdp_prepare_buff(&xdp, page_addr, AM65_CPSW_HEADROOM,
1206 				 pkt_len, false);
1207 		*xdp_state = am65_cpsw_run_xdp(flow, port, &xdp,
1208 					       cpu, &pkt_len);
1209 		if (*xdp_state != AM65_CPSW_XDP_PASS)
1210 			goto allocate;
1211 
1212 		headroom = xdp.data - xdp.data_hard_start;
1213 	} else {
1214 		headroom = AM65_CPSW_HEADROOM;
1215 	}
1216 
1217 	skb = am65_cpsw_build_skb(page_addr, ndev,
1218 				  PAGE_SIZE, headroom);
1219 	if (unlikely(!skb)) {
1220 		new_page = page;
1221 		goto requeue;
1222 	}
1223 
1224 	ndev_priv = netdev_priv(ndev);
1225 	am65_cpsw_nuss_set_offload_fwd_mark(skb, ndev_priv->offload_fwd_mark);
1226 	skb_put(skb, pkt_len);
1227 	if (port->rx_ts_enabled)
1228 		am65_cpts_rx_timestamp(common->cpts, skb);
1229 	skb_mark_for_recycle(skb);
1230 	skb->protocol = eth_type_trans(skb, ndev);
1231 	am65_cpsw_nuss_rx_csum(skb, csum_info);
1232 	napi_gro_receive(&flow->napi_rx, skb);
1233 
1234 	stats = this_cpu_ptr(ndev_priv->stats);
1235 
1236 	u64_stats_update_begin(&stats->syncp);
1237 	stats->rx_packets++;
1238 	stats->rx_bytes += pkt_len;
1239 	u64_stats_update_end(&stats->syncp);
1240 
1241 allocate:
1242 	new_page = page_pool_dev_alloc_pages(flow->page_pool);
1243 	if (unlikely(!new_page)) {
1244 		dev_err(dev, "page alloc failed\n");
1245 		return -ENOMEM;
1246 	}
1247 
1248 	if (netif_dormant(ndev)) {
1249 		am65_cpsw_put_page(flow, new_page, true);
1250 		ndev->stats.rx_dropped++;
1251 		return 0;
1252 	}
1253 
1254 requeue:
1255 	ret = am65_cpsw_nuss_rx_push(common, new_page, flow_idx);
1256 	if (WARN_ON(ret < 0)) {
1257 		am65_cpsw_put_page(flow, new_page, true);
1258 		ndev->stats.rx_errors++;
1259 		ndev->stats.rx_dropped++;
1260 	}
1261 
1262 	return ret;
1263 }
1264 
am65_cpsw_nuss_rx_timer_callback(struct hrtimer * timer)1265 static enum hrtimer_restart am65_cpsw_nuss_rx_timer_callback(struct hrtimer *timer)
1266 {
1267 	struct am65_cpsw_rx_flow *flow = container_of(timer,
1268 						      struct am65_cpsw_rx_flow,
1269 						      rx_hrtimer);
1270 
1271 	enable_irq(flow->irq);
1272 	return HRTIMER_NORESTART;
1273 }
1274 
am65_cpsw_nuss_rx_poll(struct napi_struct * napi_rx,int budget)1275 static int am65_cpsw_nuss_rx_poll(struct napi_struct *napi_rx, int budget)
1276 {
1277 	struct am65_cpsw_rx_flow *flow = am65_cpsw_napi_to_rx_flow(napi_rx);
1278 	struct am65_cpsw_common *common = flow->common;
1279 	int cpu = smp_processor_id();
1280 	int xdp_state_or = 0;
1281 	int cur_budget, ret;
1282 	int xdp_state;
1283 	int num_rx = 0;
1284 
1285 	/* process only this flow */
1286 	cur_budget = budget;
1287 	while (cur_budget--) {
1288 		ret = am65_cpsw_nuss_rx_packets(flow, cpu, &xdp_state);
1289 		xdp_state_or |= xdp_state;
1290 		if (ret)
1291 			break;
1292 		num_rx++;
1293 	}
1294 
1295 	if (xdp_state_or & AM65_CPSW_XDP_REDIRECT)
1296 		xdp_do_flush();
1297 
1298 	dev_dbg(common->dev, "%s num_rx:%d %d\n", __func__, num_rx, budget);
1299 
1300 	if (num_rx < budget && napi_complete_done(napi_rx, num_rx)) {
1301 		if (flow->irq_disabled) {
1302 			flow->irq_disabled = false;
1303 			if (unlikely(flow->rx_pace_timeout)) {
1304 				hrtimer_start(&flow->rx_hrtimer,
1305 					      ns_to_ktime(flow->rx_pace_timeout),
1306 					      HRTIMER_MODE_REL_PINNED);
1307 			} else {
1308 				enable_irq(flow->irq);
1309 			}
1310 		}
1311 	}
1312 
1313 	return num_rx;
1314 }
1315 
1316 static struct sk_buff *
am65_cpsw_nuss_tx_compl_packet_skb(struct am65_cpsw_tx_chn * tx_chn,dma_addr_t desc_dma)1317 am65_cpsw_nuss_tx_compl_packet_skb(struct am65_cpsw_tx_chn *tx_chn,
1318 				   dma_addr_t desc_dma)
1319 {
1320 	struct am65_cpsw_ndev_priv *ndev_priv;
1321 	struct am65_cpsw_ndev_stats *stats;
1322 	struct cppi5_host_desc_t *desc_tx;
1323 	struct net_device *ndev;
1324 	struct sk_buff *skb;
1325 	void **swdata;
1326 
1327 	desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool,
1328 					     desc_dma);
1329 	swdata = cppi5_hdesc_get_swdata(desc_tx);
1330 	skb = *(swdata);
1331 	am65_cpsw_nuss_xmit_free(tx_chn, desc_tx);
1332 
1333 	ndev = skb->dev;
1334 
1335 	am65_cpts_tx_timestamp(tx_chn->common->cpts, skb);
1336 
1337 	ndev_priv = netdev_priv(ndev);
1338 	stats = this_cpu_ptr(ndev_priv->stats);
1339 	u64_stats_update_begin(&stats->syncp);
1340 	stats->tx_packets++;
1341 	stats->tx_bytes += skb->len;
1342 	u64_stats_update_end(&stats->syncp);
1343 
1344 	return skb;
1345 }
1346 
1347 static struct xdp_frame *
am65_cpsw_nuss_tx_compl_packet_xdp(struct am65_cpsw_common * common,struct am65_cpsw_tx_chn * tx_chn,dma_addr_t desc_dma,struct net_device ** ndev)1348 am65_cpsw_nuss_tx_compl_packet_xdp(struct am65_cpsw_common *common,
1349 				   struct am65_cpsw_tx_chn *tx_chn,
1350 				   dma_addr_t desc_dma,
1351 				   struct net_device **ndev)
1352 {
1353 	struct am65_cpsw_ndev_priv *ndev_priv;
1354 	struct am65_cpsw_ndev_stats *stats;
1355 	struct cppi5_host_desc_t *desc_tx;
1356 	struct am65_cpsw_port *port;
1357 	struct xdp_frame *xdpf;
1358 	u32 port_id = 0;
1359 	void **swdata;
1360 
1361 	desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool, desc_dma);
1362 	cppi5_desc_get_tags_ids(&desc_tx->hdr, NULL, &port_id);
1363 	swdata = cppi5_hdesc_get_swdata(desc_tx);
1364 	xdpf = *(swdata);
1365 	am65_cpsw_nuss_xmit_free(tx_chn, desc_tx);
1366 
1367 	port = am65_common_get_port(common, port_id);
1368 	*ndev = port->ndev;
1369 
1370 	ndev_priv = netdev_priv(*ndev);
1371 	stats = this_cpu_ptr(ndev_priv->stats);
1372 	u64_stats_update_begin(&stats->syncp);
1373 	stats->tx_packets++;
1374 	stats->tx_bytes += xdpf->len;
1375 	u64_stats_update_end(&stats->syncp);
1376 
1377 	return xdpf;
1378 }
1379 
am65_cpsw_nuss_tx_wake(struct am65_cpsw_tx_chn * tx_chn,struct net_device * ndev,struct netdev_queue * netif_txq)1380 static void am65_cpsw_nuss_tx_wake(struct am65_cpsw_tx_chn *tx_chn, struct net_device *ndev,
1381 				   struct netdev_queue *netif_txq)
1382 {
1383 	if (netif_tx_queue_stopped(netif_txq)) {
1384 		/* Check whether the queue is stopped due to stalled
1385 		 * tx dma, if the queue is stopped then wake the queue
1386 		 * as we have free desc for tx
1387 		 */
1388 		__netif_tx_lock(netif_txq, smp_processor_id());
1389 		if (netif_running(ndev) &&
1390 		    (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >= MAX_SKB_FRAGS))
1391 			netif_tx_wake_queue(netif_txq);
1392 
1393 		__netif_tx_unlock(netif_txq);
1394 	}
1395 }
1396 
am65_cpsw_nuss_tx_compl_packets(struct am65_cpsw_common * common,int chn,unsigned int budget,bool * tdown)1397 static int am65_cpsw_nuss_tx_compl_packets(struct am65_cpsw_common *common,
1398 					   int chn, unsigned int budget, bool *tdown)
1399 {
1400 	enum am65_cpsw_tx_buf_type buf_type;
1401 	struct device *dev = common->dev;
1402 	struct am65_cpsw_tx_chn *tx_chn;
1403 	struct netdev_queue *netif_txq;
1404 	unsigned int total_bytes = 0;
1405 	struct net_device *ndev;
1406 	struct xdp_frame *xdpf;
1407 	struct sk_buff *skb;
1408 	dma_addr_t desc_dma;
1409 	int res, num_tx = 0;
1410 
1411 	tx_chn = &common->tx_chns[chn];
1412 
1413 	while (true) {
1414 		spin_lock(&tx_chn->lock);
1415 		res = k3_udma_glue_pop_tx_chn(tx_chn->tx_chn, &desc_dma);
1416 		spin_unlock(&tx_chn->lock);
1417 		if (res == -ENODATA)
1418 			break;
1419 
1420 		if (cppi5_desc_is_tdcm(desc_dma)) {
1421 			if (atomic_dec_and_test(&common->tdown_cnt))
1422 				complete(&common->tdown_complete);
1423 			*tdown = true;
1424 			break;
1425 		}
1426 
1427 		buf_type = am65_cpsw_nuss_buf_type(tx_chn, desc_dma);
1428 		if (buf_type == AM65_CPSW_TX_BUF_TYPE_SKB) {
1429 			skb = am65_cpsw_nuss_tx_compl_packet_skb(tx_chn, desc_dma);
1430 			ndev = skb->dev;
1431 			total_bytes = skb->len;
1432 			napi_consume_skb(skb, budget);
1433 		} else {
1434 			xdpf = am65_cpsw_nuss_tx_compl_packet_xdp(common, tx_chn,
1435 								  desc_dma, &ndev);
1436 			total_bytes = xdpf->len;
1437 			if (buf_type == AM65_CPSW_TX_BUF_TYPE_XDP_TX)
1438 				xdp_return_frame_rx_napi(xdpf);
1439 			else
1440 				xdp_return_frame(xdpf);
1441 		}
1442 		num_tx++;
1443 
1444 		netif_txq = netdev_get_tx_queue(ndev, chn);
1445 
1446 		netdev_tx_completed_queue(netif_txq, num_tx, total_bytes);
1447 
1448 		am65_cpsw_nuss_tx_wake(tx_chn, ndev, netif_txq);
1449 	}
1450 
1451 	dev_dbg(dev, "%s:%u pkt:%d\n", __func__, chn, num_tx);
1452 
1453 	return num_tx;
1454 }
1455 
am65_cpsw_nuss_tx_compl_packets_2g(struct am65_cpsw_common * common,int chn,unsigned int budget,bool * tdown)1456 static int am65_cpsw_nuss_tx_compl_packets_2g(struct am65_cpsw_common *common,
1457 					      int chn, unsigned int budget, bool *tdown)
1458 {
1459 	enum am65_cpsw_tx_buf_type buf_type;
1460 	struct device *dev = common->dev;
1461 	struct am65_cpsw_tx_chn *tx_chn;
1462 	struct netdev_queue *netif_txq;
1463 	unsigned int total_bytes = 0;
1464 	struct net_device *ndev;
1465 	struct xdp_frame *xdpf;
1466 	struct sk_buff *skb;
1467 	dma_addr_t desc_dma;
1468 	int res, num_tx = 0;
1469 
1470 	tx_chn = &common->tx_chns[chn];
1471 
1472 	while (true) {
1473 		res = k3_udma_glue_pop_tx_chn(tx_chn->tx_chn, &desc_dma);
1474 		if (res == -ENODATA)
1475 			break;
1476 
1477 		if (cppi5_desc_is_tdcm(desc_dma)) {
1478 			if (atomic_dec_and_test(&common->tdown_cnt))
1479 				complete(&common->tdown_complete);
1480 			*tdown = true;
1481 			break;
1482 		}
1483 
1484 		buf_type = am65_cpsw_nuss_buf_type(tx_chn, desc_dma);
1485 		if (buf_type == AM65_CPSW_TX_BUF_TYPE_SKB) {
1486 			skb = am65_cpsw_nuss_tx_compl_packet_skb(tx_chn, desc_dma);
1487 			ndev = skb->dev;
1488 			total_bytes += skb->len;
1489 			napi_consume_skb(skb, budget);
1490 		} else {
1491 			xdpf = am65_cpsw_nuss_tx_compl_packet_xdp(common, tx_chn,
1492 								  desc_dma, &ndev);
1493 			total_bytes += xdpf->len;
1494 			if (buf_type == AM65_CPSW_TX_BUF_TYPE_XDP_TX)
1495 				xdp_return_frame_rx_napi(xdpf);
1496 			else
1497 				xdp_return_frame(xdpf);
1498 		}
1499 		num_tx++;
1500 	}
1501 
1502 	if (!num_tx)
1503 		return 0;
1504 
1505 	netif_txq = netdev_get_tx_queue(ndev, chn);
1506 
1507 	netdev_tx_completed_queue(netif_txq, num_tx, total_bytes);
1508 
1509 	am65_cpsw_nuss_tx_wake(tx_chn, ndev, netif_txq);
1510 
1511 	dev_dbg(dev, "%s:%u pkt:%d\n", __func__, chn, num_tx);
1512 
1513 	return num_tx;
1514 }
1515 
am65_cpsw_nuss_tx_timer_callback(struct hrtimer * timer)1516 static enum hrtimer_restart am65_cpsw_nuss_tx_timer_callback(struct hrtimer *timer)
1517 {
1518 	struct am65_cpsw_tx_chn *tx_chns =
1519 			container_of(timer, struct am65_cpsw_tx_chn, tx_hrtimer);
1520 
1521 	enable_irq(tx_chns->irq);
1522 	return HRTIMER_NORESTART;
1523 }
1524 
am65_cpsw_nuss_tx_poll(struct napi_struct * napi_tx,int budget)1525 static int am65_cpsw_nuss_tx_poll(struct napi_struct *napi_tx, int budget)
1526 {
1527 	struct am65_cpsw_tx_chn *tx_chn = am65_cpsw_napi_to_tx_chn(napi_tx);
1528 	bool tdown = false;
1529 	int num_tx;
1530 
1531 	if (AM65_CPSW_IS_CPSW2G(tx_chn->common))
1532 		num_tx = am65_cpsw_nuss_tx_compl_packets_2g(tx_chn->common, tx_chn->id,
1533 							    budget, &tdown);
1534 	else
1535 		num_tx = am65_cpsw_nuss_tx_compl_packets(tx_chn->common,
1536 							 tx_chn->id, budget, &tdown);
1537 
1538 	if (num_tx >= budget)
1539 		return budget;
1540 
1541 	if (napi_complete_done(napi_tx, num_tx)) {
1542 		if (unlikely(tx_chn->tx_pace_timeout && !tdown)) {
1543 			hrtimer_start(&tx_chn->tx_hrtimer,
1544 				      ns_to_ktime(tx_chn->tx_pace_timeout),
1545 				      HRTIMER_MODE_REL_PINNED);
1546 		} else {
1547 			enable_irq(tx_chn->irq);
1548 		}
1549 	}
1550 
1551 	return 0;
1552 }
1553 
am65_cpsw_nuss_rx_irq(int irq,void * dev_id)1554 static irqreturn_t am65_cpsw_nuss_rx_irq(int irq, void *dev_id)
1555 {
1556 	struct am65_cpsw_rx_flow *flow = dev_id;
1557 
1558 	flow->irq_disabled = true;
1559 	disable_irq_nosync(irq);
1560 	napi_schedule(&flow->napi_rx);
1561 
1562 	return IRQ_HANDLED;
1563 }
1564 
am65_cpsw_nuss_tx_irq(int irq,void * dev_id)1565 static irqreturn_t am65_cpsw_nuss_tx_irq(int irq, void *dev_id)
1566 {
1567 	struct am65_cpsw_tx_chn *tx_chn = dev_id;
1568 
1569 	disable_irq_nosync(irq);
1570 	napi_schedule(&tx_chn->napi_tx);
1571 
1572 	return IRQ_HANDLED;
1573 }
1574 
am65_cpsw_nuss_ndo_slave_xmit(struct sk_buff * skb,struct net_device * ndev)1575 static netdev_tx_t am65_cpsw_nuss_ndo_slave_xmit(struct sk_buff *skb,
1576 						 struct net_device *ndev)
1577 {
1578 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
1579 	struct cppi5_host_desc_t *first_desc, *next_desc, *cur_desc;
1580 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1581 	struct device *dev = common->dev;
1582 	struct am65_cpsw_tx_chn *tx_chn;
1583 	struct netdev_queue *netif_txq;
1584 	dma_addr_t desc_dma, buf_dma;
1585 	int ret, q_idx, i;
1586 	void **swdata;
1587 	u32 *psdata;
1588 	u32 pkt_len;
1589 
1590 	/* padding enabled in hw */
1591 	pkt_len = skb_headlen(skb);
1592 
1593 	/* SKB TX timestamp */
1594 	if (port->tx_ts_enabled)
1595 		am65_cpts_prep_tx_timestamp(common->cpts, skb);
1596 
1597 	q_idx = skb_get_queue_mapping(skb);
1598 	dev_dbg(dev, "%s skb_queue:%d\n", __func__, q_idx);
1599 
1600 	tx_chn = &common->tx_chns[q_idx];
1601 	netif_txq = netdev_get_tx_queue(ndev, q_idx);
1602 
1603 	/* Map the linear buffer */
1604 	buf_dma = dma_map_single(tx_chn->dma_dev, skb->data, pkt_len,
1605 				 DMA_TO_DEVICE);
1606 	if (unlikely(dma_mapping_error(tx_chn->dma_dev, buf_dma))) {
1607 		dev_err(dev, "Failed to map tx skb buffer\n");
1608 		ndev->stats.tx_errors++;
1609 		goto err_free_skb;
1610 	}
1611 
1612 	first_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool);
1613 	if (!first_desc) {
1614 		dev_dbg(dev, "Failed to allocate descriptor\n");
1615 		dma_unmap_single(tx_chn->dma_dev, buf_dma, pkt_len,
1616 				 DMA_TO_DEVICE);
1617 		goto busy_stop_q;
1618 	}
1619 
1620 	am65_cpsw_nuss_set_buf_type(tx_chn, first_desc,
1621 				    AM65_CPSW_TX_BUF_TYPE_SKB);
1622 
1623 	cppi5_hdesc_init(first_desc, CPPI5_INFO0_HDESC_EPIB_PRESENT,
1624 			 AM65_CPSW_NAV_PS_DATA_SIZE);
1625 	cppi5_desc_set_pktids(&first_desc->hdr, 0, AM65_CPSW_CPPI_TX_FLOW_ID);
1626 	cppi5_hdesc_set_pkttype(first_desc, AM65_CPSW_CPPI_TX_PKT_TYPE);
1627 	cppi5_desc_set_tags_ids(&first_desc->hdr, 0, port->port_id);
1628 
1629 	k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &buf_dma);
1630 	cppi5_hdesc_attach_buf(first_desc, buf_dma, pkt_len, buf_dma, pkt_len);
1631 	swdata = cppi5_hdesc_get_swdata(first_desc);
1632 	*(swdata) = skb;
1633 	psdata = cppi5_hdesc_get_psdata(first_desc);
1634 
1635 	/* HW csum offload if enabled */
1636 	psdata[2] = 0;
1637 	if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
1638 		unsigned int cs_start, cs_offset;
1639 
1640 		cs_start = skb_transport_offset(skb);
1641 		cs_offset = cs_start + skb->csum_offset;
1642 		/* HW numerates bytes starting from 1 */
1643 		psdata[2] = ((cs_offset + 1) << 24) |
1644 			    ((cs_start + 1) << 16) | (skb->len - cs_start);
1645 		dev_dbg(dev, "%s tx psdata:%#x\n", __func__, psdata[2]);
1646 	}
1647 
1648 	if (!skb_is_nonlinear(skb))
1649 		goto done_tx;
1650 
1651 	dev_dbg(dev, "fragmented SKB\n");
1652 
1653 	/* Handle the case where skb is fragmented in pages */
1654 	cur_desc = first_desc;
1655 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1656 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1657 		u32 frag_size = skb_frag_size(frag);
1658 
1659 		next_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool);
1660 		if (!next_desc) {
1661 			dev_err(dev, "Failed to allocate descriptor\n");
1662 			goto busy_free_descs;
1663 		}
1664 
1665 		am65_cpsw_nuss_set_buf_type(tx_chn, next_desc,
1666 					    AM65_CPSW_TX_BUF_TYPE_SKB);
1667 
1668 		buf_dma = skb_frag_dma_map(tx_chn->dma_dev, frag, 0, frag_size,
1669 					   DMA_TO_DEVICE);
1670 		if (unlikely(dma_mapping_error(tx_chn->dma_dev, buf_dma))) {
1671 			dev_err(dev, "Failed to map tx skb page\n");
1672 			k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc);
1673 			ndev->stats.tx_errors++;
1674 			goto err_free_descs;
1675 		}
1676 
1677 		cppi5_hdesc_reset_hbdesc(next_desc);
1678 		k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &buf_dma);
1679 		cppi5_hdesc_attach_buf(next_desc,
1680 				       buf_dma, frag_size, buf_dma, frag_size);
1681 
1682 		desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool,
1683 						      next_desc);
1684 		k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &desc_dma);
1685 		cppi5_hdesc_link_hbdesc(cur_desc, desc_dma);
1686 
1687 		pkt_len += frag_size;
1688 		cur_desc = next_desc;
1689 	}
1690 	WARN_ON(pkt_len != skb->len);
1691 
1692 done_tx:
1693 	skb_tx_timestamp(skb);
1694 
1695 	/* report bql before sending packet */
1696 	netdev_tx_sent_queue(netif_txq, pkt_len);
1697 
1698 	cppi5_hdesc_set_pktlen(first_desc, pkt_len);
1699 	desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool, first_desc);
1700 	if (AM65_CPSW_IS_CPSW2G(common)) {
1701 		ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, first_desc, desc_dma);
1702 	} else {
1703 		spin_lock_bh(&tx_chn->lock);
1704 		ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, first_desc, desc_dma);
1705 		spin_unlock_bh(&tx_chn->lock);
1706 	}
1707 	if (ret) {
1708 		dev_err(dev, "can't push desc %d\n", ret);
1709 		/* inform bql */
1710 		netdev_tx_completed_queue(netif_txq, 1, pkt_len);
1711 		ndev->stats.tx_errors++;
1712 		goto err_free_descs;
1713 	}
1714 
1715 	if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) < MAX_SKB_FRAGS) {
1716 		netif_tx_stop_queue(netif_txq);
1717 		/* Barrier, so that stop_queue visible to other cpus */
1718 		smp_mb__after_atomic();
1719 		dev_dbg(dev, "netif_tx_stop_queue %d\n", q_idx);
1720 
1721 		/* re-check for smp */
1722 		if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >=
1723 		    MAX_SKB_FRAGS) {
1724 			netif_tx_wake_queue(netif_txq);
1725 			dev_dbg(dev, "netif_tx_wake_queue %d\n", q_idx);
1726 		}
1727 	}
1728 
1729 	return NETDEV_TX_OK;
1730 
1731 err_free_descs:
1732 	am65_cpsw_nuss_xmit_free(tx_chn, first_desc);
1733 err_free_skb:
1734 	ndev->stats.tx_dropped++;
1735 	dev_kfree_skb_any(skb);
1736 	return NETDEV_TX_OK;
1737 
1738 busy_free_descs:
1739 	am65_cpsw_nuss_xmit_free(tx_chn, first_desc);
1740 busy_stop_q:
1741 	netif_tx_stop_queue(netif_txq);
1742 	return NETDEV_TX_BUSY;
1743 }
1744 
am65_cpsw_nuss_ndo_slave_set_mac_address(struct net_device * ndev,void * addr)1745 static int am65_cpsw_nuss_ndo_slave_set_mac_address(struct net_device *ndev,
1746 						    void *addr)
1747 {
1748 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
1749 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1750 	struct sockaddr *sockaddr = (struct sockaddr *)addr;
1751 	int ret;
1752 
1753 	ret = eth_prepare_mac_addr_change(ndev, addr);
1754 	if (ret < 0)
1755 		return ret;
1756 
1757 	ret = pm_runtime_resume_and_get(common->dev);
1758 	if (ret < 0)
1759 		return ret;
1760 
1761 	cpsw_ale_del_ucast(common->ale, ndev->dev_addr,
1762 			   HOST_PORT_NUM, 0, 0);
1763 	cpsw_ale_add_ucast(common->ale, sockaddr->sa_data,
1764 			   HOST_PORT_NUM, ALE_SECURE, 0);
1765 
1766 	am65_cpsw_port_set_sl_mac(port, addr);
1767 	eth_commit_mac_addr_change(ndev, sockaddr);
1768 
1769 	pm_runtime_put(common->dev);
1770 
1771 	return 0;
1772 }
1773 
am65_cpsw_nuss_hwtstamp_set(struct net_device * ndev,struct ifreq * ifr)1774 static int am65_cpsw_nuss_hwtstamp_set(struct net_device *ndev,
1775 				       struct ifreq *ifr)
1776 {
1777 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1778 	u32 ts_ctrl, seq_id, ts_ctrl_ltype2, ts_vlan_ltype;
1779 	struct hwtstamp_config cfg;
1780 
1781 	if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS))
1782 		return -EOPNOTSUPP;
1783 
1784 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
1785 		return -EFAULT;
1786 
1787 	/* TX HW timestamp */
1788 	switch (cfg.tx_type) {
1789 	case HWTSTAMP_TX_OFF:
1790 	case HWTSTAMP_TX_ON:
1791 		break;
1792 	default:
1793 		return -ERANGE;
1794 	}
1795 
1796 	switch (cfg.rx_filter) {
1797 	case HWTSTAMP_FILTER_NONE:
1798 		port->rx_ts_enabled = false;
1799 		break;
1800 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1801 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1802 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1803 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1804 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1805 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1806 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
1807 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
1808 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1809 		port->rx_ts_enabled = true;
1810 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
1811 		break;
1812 	case HWTSTAMP_FILTER_ALL:
1813 	case HWTSTAMP_FILTER_SOME:
1814 	case HWTSTAMP_FILTER_NTP_ALL:
1815 		return -EOPNOTSUPP;
1816 	default:
1817 		return -ERANGE;
1818 	}
1819 
1820 	port->tx_ts_enabled = (cfg.tx_type == HWTSTAMP_TX_ON);
1821 
1822 	/* cfg TX timestamp */
1823 	seq_id = (AM65_CPSW_TS_SEQ_ID_OFFSET <<
1824 		  AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT) | ETH_P_1588;
1825 
1826 	ts_vlan_ltype = ETH_P_8021Q;
1827 
1828 	ts_ctrl_ltype2 = ETH_P_1588 |
1829 			 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107 |
1830 			 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129 |
1831 			 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130 |
1832 			 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131 |
1833 			 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132 |
1834 			 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319 |
1835 			 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320 |
1836 			 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO;
1837 
1838 	ts_ctrl = AM65_CPSW_TS_EVENT_MSG_TYPE_BITS <<
1839 		  AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT;
1840 
1841 	if (port->tx_ts_enabled)
1842 		ts_ctrl |= AM65_CPSW_TS_TX_ANX_ALL_EN |
1843 			   AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN;
1844 
1845 	if (port->rx_ts_enabled)
1846 		ts_ctrl |= AM65_CPSW_TS_RX_ANX_ALL_EN |
1847 			   AM65_CPSW_PN_TS_CTL_RX_VLAN_LT1_EN;
1848 
1849 	writel(seq_id, port->port_base + AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG);
1850 	writel(ts_vlan_ltype, port->port_base +
1851 	       AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG);
1852 	writel(ts_ctrl_ltype2, port->port_base +
1853 	       AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2);
1854 	writel(ts_ctrl, port->port_base + AM65_CPSW_PORTN_REG_TS_CTL);
1855 
1856 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1857 }
1858 
am65_cpsw_nuss_hwtstamp_get(struct net_device * ndev,struct ifreq * ifr)1859 static int am65_cpsw_nuss_hwtstamp_get(struct net_device *ndev,
1860 				       struct ifreq *ifr)
1861 {
1862 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1863 	struct hwtstamp_config cfg;
1864 
1865 	if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS))
1866 		return -EOPNOTSUPP;
1867 
1868 	cfg.flags = 0;
1869 	cfg.tx_type = port->tx_ts_enabled ?
1870 		      HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
1871 	cfg.rx_filter = port->rx_ts_enabled ?
1872 			HWTSTAMP_FILTER_PTP_V2_EVENT : HWTSTAMP_FILTER_NONE;
1873 
1874 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
1875 }
1876 
am65_cpsw_nuss_ndo_slave_ioctl(struct net_device * ndev,struct ifreq * req,int cmd)1877 static int am65_cpsw_nuss_ndo_slave_ioctl(struct net_device *ndev,
1878 					  struct ifreq *req, int cmd)
1879 {
1880 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1881 
1882 	if (!netif_running(ndev))
1883 		return -EINVAL;
1884 
1885 	switch (cmd) {
1886 	case SIOCSHWTSTAMP:
1887 		return am65_cpsw_nuss_hwtstamp_set(ndev, req);
1888 	case SIOCGHWTSTAMP:
1889 		return am65_cpsw_nuss_hwtstamp_get(ndev, req);
1890 	}
1891 
1892 	return phylink_mii_ioctl(port->slave.phylink, req, cmd);
1893 }
1894 
am65_cpsw_nuss_ndo_get_stats(struct net_device * dev,struct rtnl_link_stats64 * stats)1895 static void am65_cpsw_nuss_ndo_get_stats(struct net_device *dev,
1896 					 struct rtnl_link_stats64 *stats)
1897 {
1898 	struct am65_cpsw_ndev_priv *ndev_priv = netdev_priv(dev);
1899 	unsigned int start;
1900 	int cpu;
1901 
1902 	for_each_possible_cpu(cpu) {
1903 		struct am65_cpsw_ndev_stats *cpu_stats;
1904 		u64 rx_packets;
1905 		u64 rx_bytes;
1906 		u64 tx_packets;
1907 		u64 tx_bytes;
1908 
1909 		cpu_stats = per_cpu_ptr(ndev_priv->stats, cpu);
1910 		do {
1911 			start = u64_stats_fetch_begin(&cpu_stats->syncp);
1912 			rx_packets = cpu_stats->rx_packets;
1913 			rx_bytes   = cpu_stats->rx_bytes;
1914 			tx_packets = cpu_stats->tx_packets;
1915 			tx_bytes   = cpu_stats->tx_bytes;
1916 		} while (u64_stats_fetch_retry(&cpu_stats->syncp, start));
1917 
1918 		stats->rx_packets += rx_packets;
1919 		stats->rx_bytes   += rx_bytes;
1920 		stats->tx_packets += tx_packets;
1921 		stats->tx_bytes   += tx_bytes;
1922 	}
1923 
1924 	stats->rx_errors	= dev->stats.rx_errors;
1925 	stats->rx_dropped	= dev->stats.rx_dropped;
1926 	stats->tx_dropped	= dev->stats.tx_dropped;
1927 }
1928 
am65_cpsw_xdp_prog_setup(struct net_device * ndev,struct bpf_prog * prog)1929 static int am65_cpsw_xdp_prog_setup(struct net_device *ndev,
1930 				    struct bpf_prog *prog)
1931 {
1932 	struct am65_cpsw_port *port = am65_ndev_to_port(ndev);
1933 	bool running = netif_running(ndev);
1934 	struct bpf_prog *old_prog;
1935 
1936 	if (running)
1937 		am65_cpsw_nuss_ndo_slave_stop(ndev);
1938 
1939 	old_prog = xchg(&port->xdp_prog, prog);
1940 	if (old_prog)
1941 		bpf_prog_put(old_prog);
1942 
1943 	if (running)
1944 		return am65_cpsw_nuss_ndo_slave_open(ndev);
1945 
1946 	return 0;
1947 }
1948 
am65_cpsw_ndo_bpf(struct net_device * ndev,struct netdev_bpf * bpf)1949 static int am65_cpsw_ndo_bpf(struct net_device *ndev, struct netdev_bpf *bpf)
1950 {
1951 	switch (bpf->command) {
1952 	case XDP_SETUP_PROG:
1953 		return am65_cpsw_xdp_prog_setup(ndev, bpf->prog);
1954 	default:
1955 		return -EINVAL;
1956 	}
1957 }
1958 
am65_cpsw_ndo_xdp_xmit(struct net_device * ndev,int n,struct xdp_frame ** frames,u32 flags)1959 static int am65_cpsw_ndo_xdp_xmit(struct net_device *ndev, int n,
1960 				  struct xdp_frame **frames, u32 flags)
1961 {
1962 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
1963 	struct am65_cpsw_tx_chn *tx_chn;
1964 	struct netdev_queue *netif_txq;
1965 	int cpu = smp_processor_id();
1966 	int i, nxmit = 0;
1967 
1968 	tx_chn = &common->tx_chns[cpu % common->tx_ch_num];
1969 	netif_txq = netdev_get_tx_queue(ndev, tx_chn->id);
1970 
1971 	__netif_tx_lock(netif_txq, cpu);
1972 	for (i = 0; i < n; i++) {
1973 		if (am65_cpsw_xdp_tx_frame(ndev, tx_chn, frames[i],
1974 					   AM65_CPSW_TX_BUF_TYPE_XDP_NDO))
1975 			break;
1976 		nxmit++;
1977 	}
1978 	__netif_tx_unlock(netif_txq);
1979 
1980 	return nxmit;
1981 }
1982 
1983 static const struct net_device_ops am65_cpsw_nuss_netdev_ops = {
1984 	.ndo_open		= am65_cpsw_nuss_ndo_slave_open,
1985 	.ndo_stop		= am65_cpsw_nuss_ndo_slave_stop,
1986 	.ndo_start_xmit		= am65_cpsw_nuss_ndo_slave_xmit,
1987 	.ndo_set_rx_mode	= am65_cpsw_nuss_ndo_slave_set_rx_mode,
1988 	.ndo_get_stats64        = am65_cpsw_nuss_ndo_get_stats,
1989 	.ndo_validate_addr	= eth_validate_addr,
1990 	.ndo_set_mac_address	= am65_cpsw_nuss_ndo_slave_set_mac_address,
1991 	.ndo_tx_timeout		= am65_cpsw_nuss_ndo_host_tx_timeout,
1992 	.ndo_vlan_rx_add_vid	= am65_cpsw_nuss_ndo_slave_add_vid,
1993 	.ndo_vlan_rx_kill_vid	= am65_cpsw_nuss_ndo_slave_kill_vid,
1994 	.ndo_eth_ioctl		= am65_cpsw_nuss_ndo_slave_ioctl,
1995 	.ndo_setup_tc           = am65_cpsw_qos_ndo_setup_tc,
1996 	.ndo_set_tx_maxrate	= am65_cpsw_qos_ndo_tx_p0_set_maxrate,
1997 	.ndo_bpf		= am65_cpsw_ndo_bpf,
1998 	.ndo_xdp_xmit		= am65_cpsw_ndo_xdp_xmit,
1999 };
2000 
am65_cpsw_disable_phy(struct phy * phy)2001 static void am65_cpsw_disable_phy(struct phy *phy)
2002 {
2003 	phy_power_off(phy);
2004 	phy_exit(phy);
2005 }
2006 
am65_cpsw_enable_phy(struct phy * phy)2007 static int am65_cpsw_enable_phy(struct phy *phy)
2008 {
2009 	int ret;
2010 
2011 	ret = phy_init(phy);
2012 	if (ret < 0)
2013 		return ret;
2014 
2015 	ret = phy_power_on(phy);
2016 	if (ret < 0) {
2017 		phy_exit(phy);
2018 		return ret;
2019 	}
2020 
2021 	return 0;
2022 }
2023 
am65_cpsw_disable_serdes_phy(struct am65_cpsw_common * common)2024 static void am65_cpsw_disable_serdes_phy(struct am65_cpsw_common *common)
2025 {
2026 	struct am65_cpsw_port *port;
2027 	struct phy *phy;
2028 	int i;
2029 
2030 	for (i = 0; i < common->port_num; i++) {
2031 		port = &common->ports[i];
2032 		phy = port->slave.serdes_phy;
2033 		if (phy)
2034 			am65_cpsw_disable_phy(phy);
2035 	}
2036 }
2037 
am65_cpsw_init_serdes_phy(struct device * dev,struct device_node * port_np,struct am65_cpsw_port * port)2038 static int am65_cpsw_init_serdes_phy(struct device *dev, struct device_node *port_np,
2039 				     struct am65_cpsw_port *port)
2040 {
2041 	const char *name = "serdes";
2042 	struct phy *phy;
2043 	int ret;
2044 
2045 	phy = devm_of_phy_optional_get(dev, port_np, name);
2046 	if (IS_ERR_OR_NULL(phy))
2047 		return PTR_ERR_OR_ZERO(phy);
2048 
2049 	/* Serdes PHY exists. Store it. */
2050 	port->slave.serdes_phy = phy;
2051 
2052 	ret =  am65_cpsw_enable_phy(phy);
2053 	if (ret < 0)
2054 		goto err_phy;
2055 
2056 	return 0;
2057 
2058 err_phy:
2059 	devm_phy_put(dev, phy);
2060 	return ret;
2061 }
2062 
am65_cpsw_nuss_mac_config(struct phylink_config * config,unsigned int mode,const struct phylink_link_state * state)2063 static void am65_cpsw_nuss_mac_config(struct phylink_config *config, unsigned int mode,
2064 				      const struct phylink_link_state *state)
2065 {
2066 	struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data,
2067 							  phylink_config);
2068 	struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave);
2069 	struct am65_cpsw_common *common = port->common;
2070 
2071 	if (common->pdata.extra_modes & BIT(state->interface)) {
2072 		if (state->interface == PHY_INTERFACE_MODE_SGMII) {
2073 			writel(ADVERTISE_SGMII,
2074 			       port->sgmii_base + AM65_CPSW_SGMII_MR_ADV_ABILITY_REG);
2075 			cpsw_sl_ctl_set(port->slave.mac_sl, CPSW_SL_CTL_EXT_EN);
2076 		} else {
2077 			cpsw_sl_ctl_clr(port->slave.mac_sl, CPSW_SL_CTL_EXT_EN);
2078 		}
2079 
2080 		if (state->interface == PHY_INTERFACE_MODE_USXGMII) {
2081 			cpsw_sl_ctl_set(port->slave.mac_sl,
2082 					CPSW_SL_CTL_XGIG | CPSW_SL_CTL_XGMII_EN);
2083 		} else {
2084 			cpsw_sl_ctl_clr(port->slave.mac_sl,
2085 					CPSW_SL_CTL_XGIG | CPSW_SL_CTL_XGMII_EN);
2086 		}
2087 
2088 		writel(AM65_CPSW_SGMII_CONTROL_MR_AN_ENABLE,
2089 		       port->sgmii_base + AM65_CPSW_SGMII_CONTROL_REG);
2090 	}
2091 }
2092 
am65_cpsw_nuss_mac_link_down(struct phylink_config * config,unsigned int mode,phy_interface_t interface)2093 static void am65_cpsw_nuss_mac_link_down(struct phylink_config *config, unsigned int mode,
2094 					 phy_interface_t interface)
2095 {
2096 	struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data,
2097 							  phylink_config);
2098 	struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave);
2099 	struct am65_cpsw_common *common = port->common;
2100 	struct net_device *ndev = port->ndev;
2101 	u32 mac_control;
2102 	int tmo;
2103 
2104 	/* disable forwarding */
2105 	cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
2106 
2107 	cpsw_sl_ctl_set(port->slave.mac_sl, CPSW_SL_CTL_CMD_IDLE);
2108 
2109 	tmo = cpsw_sl_wait_for_idle(port->slave.mac_sl, 100);
2110 	dev_dbg(common->dev, "down msc_sl %08x tmo %d\n",
2111 		cpsw_sl_reg_read(port->slave.mac_sl, CPSW_SL_MACSTATUS), tmo);
2112 
2113 	/* All the bits that am65_cpsw_nuss_mac_link_up() can possibly set */
2114 	mac_control = CPSW_SL_CTL_GMII_EN | CPSW_SL_CTL_GIG | CPSW_SL_CTL_IFCTL_A |
2115 		      CPSW_SL_CTL_FULLDUPLEX | CPSW_SL_CTL_RX_FLOW_EN | CPSW_SL_CTL_TX_FLOW_EN;
2116 	/* If interface mode is RGMII, CPSW_SL_CTL_EXT_EN might have been set for 10 Mbps */
2117 	if (phy_interface_mode_is_rgmii(interface))
2118 		mac_control |= CPSW_SL_CTL_EXT_EN;
2119 	/* Only clear those bits that can be set by am65_cpsw_nuss_mac_link_up() */
2120 	cpsw_sl_ctl_clr(port->slave.mac_sl, mac_control);
2121 
2122 	am65_cpsw_qos_link_down(ndev);
2123 	netif_tx_stop_all_queues(ndev);
2124 }
2125 
am65_cpsw_nuss_mac_link_up(struct phylink_config * config,struct phy_device * phy,unsigned int mode,phy_interface_t interface,int speed,int duplex,bool tx_pause,bool rx_pause)2126 static void am65_cpsw_nuss_mac_link_up(struct phylink_config *config, struct phy_device *phy,
2127 				       unsigned int mode, phy_interface_t interface, int speed,
2128 				       int duplex, bool tx_pause, bool rx_pause)
2129 {
2130 	struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data,
2131 							  phylink_config);
2132 	struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave);
2133 	struct am65_cpsw_common *common = port->common;
2134 	u32 mac_control = CPSW_SL_CTL_GMII_EN;
2135 	struct net_device *ndev = port->ndev;
2136 
2137 	/* Bring the port out of idle state */
2138 	cpsw_sl_ctl_clr(port->slave.mac_sl, CPSW_SL_CTL_CMD_IDLE);
2139 
2140 	if (speed == SPEED_1000)
2141 		mac_control |= CPSW_SL_CTL_GIG;
2142 	/* TODO: Verify whether in-band is necessary for 10 Mbps RGMII */
2143 	if (speed == SPEED_10 && phy_interface_mode_is_rgmii(interface))
2144 		/* Can be used with in band mode only */
2145 		mac_control |= CPSW_SL_CTL_EXT_EN;
2146 	if (speed == SPEED_100 && interface == PHY_INTERFACE_MODE_RMII)
2147 		mac_control |= CPSW_SL_CTL_IFCTL_A;
2148 	if (duplex)
2149 		mac_control |= CPSW_SL_CTL_FULLDUPLEX;
2150 
2151 	/* rx_pause/tx_pause */
2152 	if (rx_pause)
2153 		mac_control |= CPSW_SL_CTL_TX_FLOW_EN;
2154 
2155 	if (tx_pause)
2156 		mac_control |= CPSW_SL_CTL_RX_FLOW_EN;
2157 
2158 	cpsw_sl_ctl_set(port->slave.mac_sl, mac_control);
2159 
2160 	/* enable forwarding */
2161 	cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
2162 
2163 	am65_cpsw_qos_link_up(ndev, speed);
2164 	netif_tx_wake_all_queues(ndev);
2165 }
2166 
2167 static const struct phylink_mac_ops am65_cpsw_phylink_mac_ops = {
2168 	.mac_config = am65_cpsw_nuss_mac_config,
2169 	.mac_link_down = am65_cpsw_nuss_mac_link_down,
2170 	.mac_link_up = am65_cpsw_nuss_mac_link_up,
2171 };
2172 
am65_cpsw_nuss_slave_disable_unused(struct am65_cpsw_port * port)2173 static void am65_cpsw_nuss_slave_disable_unused(struct am65_cpsw_port *port)
2174 {
2175 	struct am65_cpsw_common *common = port->common;
2176 
2177 	if (!port->disabled)
2178 		return;
2179 
2180 	cpsw_ale_control_set(common->ale, port->port_id,
2181 			     ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
2182 
2183 	cpsw_sl_reset(port->slave.mac_sl, 100);
2184 	cpsw_sl_ctl_reset(port->slave.mac_sl);
2185 }
2186 
am65_cpsw_nuss_free_tx_chns(void * data)2187 static void am65_cpsw_nuss_free_tx_chns(void *data)
2188 {
2189 	struct am65_cpsw_common *common = data;
2190 	int i;
2191 
2192 	for (i = 0; i < common->tx_ch_num; i++) {
2193 		struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
2194 
2195 		if (!IS_ERR_OR_NULL(tx_chn->desc_pool))
2196 			k3_cppi_desc_pool_destroy(tx_chn->desc_pool);
2197 
2198 		if (!IS_ERR_OR_NULL(tx_chn->tx_chn))
2199 			k3_udma_glue_release_tx_chn(tx_chn->tx_chn);
2200 
2201 		memset(tx_chn, 0, sizeof(*tx_chn));
2202 	}
2203 }
2204 
am65_cpsw_nuss_remove_tx_chns(struct am65_cpsw_common * common)2205 static void am65_cpsw_nuss_remove_tx_chns(struct am65_cpsw_common *common)
2206 {
2207 	struct device *dev = common->dev;
2208 	int i;
2209 
2210 	common->tx_ch_rate_msk = 0;
2211 	for (i = 0; i < common->tx_ch_num; i++) {
2212 		struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
2213 
2214 		if (tx_chn->irq > 0)
2215 			devm_free_irq(dev, tx_chn->irq, tx_chn);
2216 
2217 		netif_napi_del(&tx_chn->napi_tx);
2218 	}
2219 
2220 	am65_cpsw_nuss_free_tx_chns(common);
2221 }
2222 
am65_cpsw_nuss_ndev_add_tx_napi(struct am65_cpsw_common * common)2223 static int am65_cpsw_nuss_ndev_add_tx_napi(struct am65_cpsw_common *common)
2224 {
2225 	struct device *dev = common->dev;
2226 	struct am65_cpsw_tx_chn *tx_chn;
2227 	int i, ret = 0;
2228 
2229 	for (i = 0; i < common->tx_ch_num; i++) {
2230 		tx_chn = &common->tx_chns[i];
2231 
2232 		hrtimer_init(&tx_chn->tx_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
2233 		tx_chn->tx_hrtimer.function = &am65_cpsw_nuss_tx_timer_callback;
2234 
2235 		netif_napi_add_tx(common->dma_ndev, &tx_chn->napi_tx,
2236 				  am65_cpsw_nuss_tx_poll);
2237 
2238 		ret = devm_request_irq(dev, tx_chn->irq,
2239 				       am65_cpsw_nuss_tx_irq,
2240 				       IRQF_TRIGGER_HIGH,
2241 				       tx_chn->tx_chn_name, tx_chn);
2242 		if (ret) {
2243 			dev_err(dev, "failure requesting tx%u irq %u, %d\n",
2244 				tx_chn->id, tx_chn->irq, ret);
2245 			goto err;
2246 		}
2247 	}
2248 
2249 	return 0;
2250 
2251 err:
2252 	netif_napi_del(&tx_chn->napi_tx);
2253 	for (--i; i >= 0; i--) {
2254 		tx_chn = &common->tx_chns[i];
2255 		devm_free_irq(dev, tx_chn->irq, tx_chn);
2256 		netif_napi_del(&tx_chn->napi_tx);
2257 	}
2258 
2259 	return ret;
2260 }
2261 
am65_cpsw_nuss_init_tx_chns(struct am65_cpsw_common * common)2262 static int am65_cpsw_nuss_init_tx_chns(struct am65_cpsw_common *common)
2263 {
2264 	u32  max_desc_num = ALIGN(AM65_CPSW_MAX_TX_DESC, MAX_SKB_FRAGS);
2265 	struct k3_udma_glue_tx_channel_cfg tx_cfg = { 0 };
2266 	struct device *dev = common->dev;
2267 	struct k3_ring_cfg ring_cfg = {
2268 		.elm_size = K3_RINGACC_RING_ELSIZE_8,
2269 		.mode = K3_RINGACC_RING_MODE_RING,
2270 		.flags = 0
2271 	};
2272 	u32 hdesc_size, hdesc_size_out;
2273 	int i, ret = 0;
2274 
2275 	hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE,
2276 					   AM65_CPSW_NAV_SW_DATA_SIZE);
2277 
2278 	tx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE;
2279 	tx_cfg.tx_cfg = ring_cfg;
2280 	tx_cfg.txcq_cfg = ring_cfg;
2281 	tx_cfg.tx_cfg.size = max_desc_num;
2282 	tx_cfg.txcq_cfg.size = max_desc_num;
2283 
2284 	for (i = 0; i < common->tx_ch_num; i++) {
2285 		struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i];
2286 
2287 		snprintf(tx_chn->tx_chn_name,
2288 			 sizeof(tx_chn->tx_chn_name), "tx%d", i);
2289 
2290 		spin_lock_init(&tx_chn->lock);
2291 		tx_chn->common = common;
2292 		tx_chn->id = i;
2293 		tx_chn->descs_num = max_desc_num;
2294 
2295 		tx_chn->tx_chn =
2296 			k3_udma_glue_request_tx_chn(dev,
2297 						    tx_chn->tx_chn_name,
2298 						    &tx_cfg);
2299 		if (IS_ERR(tx_chn->tx_chn)) {
2300 			ret = dev_err_probe(dev, PTR_ERR(tx_chn->tx_chn),
2301 					    "Failed to request tx dma channel\n");
2302 			goto err;
2303 		}
2304 		tx_chn->dma_dev = k3_udma_glue_tx_get_dma_device(tx_chn->tx_chn);
2305 
2306 		tx_chn->desc_pool = k3_cppi_desc_pool_create_name(tx_chn->dma_dev,
2307 								  tx_chn->descs_num,
2308 								  hdesc_size,
2309 								  tx_chn->tx_chn_name);
2310 		if (IS_ERR(tx_chn->desc_pool)) {
2311 			ret = PTR_ERR(tx_chn->desc_pool);
2312 			dev_err(dev, "Failed to create poll %d\n", ret);
2313 			goto err;
2314 		}
2315 
2316 		hdesc_size_out = k3_cppi_desc_pool_desc_size(tx_chn->desc_pool);
2317 		tx_chn->dsize_log2 = __fls(hdesc_size_out);
2318 		WARN_ON(hdesc_size_out != (1 << tx_chn->dsize_log2));
2319 
2320 		tx_chn->irq = k3_udma_glue_tx_get_irq(tx_chn->tx_chn);
2321 		if (tx_chn->irq < 0) {
2322 			dev_err(dev, "Failed to get tx dma irq %d\n",
2323 				tx_chn->irq);
2324 			ret = tx_chn->irq;
2325 			goto err;
2326 		}
2327 
2328 		snprintf(tx_chn->tx_chn_name,
2329 			 sizeof(tx_chn->tx_chn_name), "%s-tx%d",
2330 			 dev_name(dev), tx_chn->id);
2331 	}
2332 
2333 	ret = am65_cpsw_nuss_ndev_add_tx_napi(common);
2334 	if (ret) {
2335 		dev_err(dev, "Failed to add tx NAPI %d\n", ret);
2336 		goto err;
2337 	}
2338 
2339 	return 0;
2340 
2341 err:
2342 	am65_cpsw_nuss_free_tx_chns(common);
2343 
2344 	return ret;
2345 }
2346 
am65_cpsw_nuss_free_rx_chns(void * data)2347 static void am65_cpsw_nuss_free_rx_chns(void *data)
2348 {
2349 	struct am65_cpsw_common *common = data;
2350 	struct am65_cpsw_rx_chn *rx_chn;
2351 
2352 	rx_chn = &common->rx_chns;
2353 
2354 	if (!IS_ERR_OR_NULL(rx_chn->desc_pool))
2355 		k3_cppi_desc_pool_destroy(rx_chn->desc_pool);
2356 
2357 	if (!IS_ERR_OR_NULL(rx_chn->rx_chn))
2358 		k3_udma_glue_release_rx_chn(rx_chn->rx_chn);
2359 }
2360 
am65_cpsw_nuss_remove_rx_chns(struct am65_cpsw_common * common)2361 static void am65_cpsw_nuss_remove_rx_chns(struct am65_cpsw_common *common)
2362 {
2363 	struct device *dev = common->dev;
2364 	struct am65_cpsw_rx_chn *rx_chn;
2365 	struct am65_cpsw_rx_flow *flows;
2366 	int i;
2367 
2368 	rx_chn = &common->rx_chns;
2369 	flows = rx_chn->flows;
2370 
2371 	for (i = 0; i < common->rx_ch_num_flows; i++) {
2372 		if (!(flows[i].irq < 0))
2373 			devm_free_irq(dev, flows[i].irq, &flows[i]);
2374 		netif_napi_del(&flows[i].napi_rx);
2375 	}
2376 
2377 	am65_cpsw_nuss_free_rx_chns(common);
2378 
2379 	common->rx_flow_id_base = -1;
2380 }
2381 
am65_cpsw_nuss_init_rx_chns(struct am65_cpsw_common * common)2382 static int am65_cpsw_nuss_init_rx_chns(struct am65_cpsw_common *common)
2383 {
2384 	struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns;
2385 	struct k3_udma_glue_rx_channel_cfg rx_cfg = { 0 };
2386 	u32  max_desc_num = AM65_CPSW_MAX_RX_DESC;
2387 	struct device *dev = common->dev;
2388 	struct am65_cpsw_rx_flow *flow;
2389 	u32 hdesc_size, hdesc_size_out;
2390 	u32 fdqring_id;
2391 	int i, ret = 0;
2392 
2393 	hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE,
2394 					   AM65_CPSW_NAV_SW_DATA_SIZE);
2395 
2396 	rx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE;
2397 	rx_cfg.flow_id_num = common->rx_ch_num_flows;
2398 	rx_cfg.flow_id_base = common->rx_flow_id_base;
2399 
2400 	/* init all flows */
2401 	rx_chn->dev = dev;
2402 	rx_chn->descs_num = max_desc_num * rx_cfg.flow_id_num;
2403 
2404 	for (i = 0; i < common->rx_ch_num_flows; i++) {
2405 		flow = &rx_chn->flows[i];
2406 		flow->page_pool = NULL;
2407 	}
2408 
2409 	rx_chn->rx_chn = k3_udma_glue_request_rx_chn(dev, "rx", &rx_cfg);
2410 	if (IS_ERR(rx_chn->rx_chn)) {
2411 		ret = dev_err_probe(dev, PTR_ERR(rx_chn->rx_chn),
2412 				    "Failed to request rx dma channel\n");
2413 		goto err;
2414 	}
2415 	rx_chn->dma_dev = k3_udma_glue_rx_get_dma_device(rx_chn->rx_chn);
2416 
2417 	rx_chn->desc_pool = k3_cppi_desc_pool_create_name(rx_chn->dma_dev,
2418 							  rx_chn->descs_num,
2419 							  hdesc_size, "rx");
2420 	if (IS_ERR(rx_chn->desc_pool)) {
2421 		ret = PTR_ERR(rx_chn->desc_pool);
2422 		dev_err(dev, "Failed to create rx poll %d\n", ret);
2423 		goto err;
2424 	}
2425 
2426 	hdesc_size_out = k3_cppi_desc_pool_desc_size(rx_chn->desc_pool);
2427 	rx_chn->dsize_log2 = __fls(hdesc_size_out);
2428 	WARN_ON(hdesc_size_out != (1 << rx_chn->dsize_log2));
2429 
2430 	common->rx_flow_id_base =
2431 			k3_udma_glue_rx_get_flow_id_base(rx_chn->rx_chn);
2432 	dev_info(dev, "set new flow-id-base %u\n", common->rx_flow_id_base);
2433 
2434 	fdqring_id = K3_RINGACC_RING_ID_ANY;
2435 	for (i = 0; i < rx_cfg.flow_id_num; i++) {
2436 		struct k3_ring_cfg rxring_cfg = {
2437 			.elm_size = K3_RINGACC_RING_ELSIZE_8,
2438 			.mode = K3_RINGACC_RING_MODE_RING,
2439 			.flags = 0,
2440 		};
2441 		struct k3_ring_cfg fdqring_cfg = {
2442 			.elm_size = K3_RINGACC_RING_ELSIZE_8,
2443 			.flags = K3_RINGACC_RING_SHARED,
2444 		};
2445 		struct k3_udma_glue_rx_flow_cfg rx_flow_cfg = {
2446 			.rx_cfg = rxring_cfg,
2447 			.rxfdq_cfg = fdqring_cfg,
2448 			.ring_rxq_id = K3_RINGACC_RING_ID_ANY,
2449 			.src_tag_lo_sel =
2450 				K3_UDMA_GLUE_SRC_TAG_LO_USE_REMOTE_SRC_TAG,
2451 		};
2452 
2453 		flow = &rx_chn->flows[i];
2454 		flow->id = i;
2455 		flow->common = common;
2456 		flow->irq = -EINVAL;
2457 
2458 		rx_flow_cfg.ring_rxfdq0_id = fdqring_id;
2459 		rx_flow_cfg.rx_cfg.size = max_desc_num;
2460 		/* share same FDQ for all flows */
2461 		rx_flow_cfg.rxfdq_cfg.size = max_desc_num * rx_cfg.flow_id_num;
2462 		rx_flow_cfg.rxfdq_cfg.mode = common->pdata.fdqring_mode;
2463 
2464 		ret = k3_udma_glue_rx_flow_init(rx_chn->rx_chn,
2465 						i, &rx_flow_cfg);
2466 		if (ret) {
2467 			dev_err(dev, "Failed to init rx flow%d %d\n", i, ret);
2468 			goto err_flow;
2469 		}
2470 		if (!i)
2471 			fdqring_id =
2472 				k3_udma_glue_rx_flow_get_fdq_id(rx_chn->rx_chn,
2473 								i);
2474 
2475 		flow->irq = k3_udma_glue_rx_get_irq(rx_chn->rx_chn, i);
2476 		if (flow->irq <= 0) {
2477 			dev_err(dev, "Failed to get rx dma irq %d\n",
2478 				flow->irq);
2479 			ret = flow->irq;
2480 			goto err_flow;
2481 		}
2482 
2483 		snprintf(flow->name,
2484 			 sizeof(flow->name), "%s-rx%d",
2485 			 dev_name(dev), i);
2486 		hrtimer_init(&flow->rx_hrtimer, CLOCK_MONOTONIC,
2487 			     HRTIMER_MODE_REL_PINNED);
2488 		flow->rx_hrtimer.function = &am65_cpsw_nuss_rx_timer_callback;
2489 
2490 		netif_napi_add(common->dma_ndev, &flow->napi_rx,
2491 			       am65_cpsw_nuss_rx_poll);
2492 
2493 		ret = devm_request_irq(dev, flow->irq,
2494 				       am65_cpsw_nuss_rx_irq,
2495 				       IRQF_TRIGGER_HIGH,
2496 				       flow->name, flow);
2497 		if (ret) {
2498 			dev_err(dev, "failure requesting rx %d irq %u, %d\n",
2499 				i, flow->irq, ret);
2500 			flow->irq = -EINVAL;
2501 			goto err_request_irq;
2502 		}
2503 	}
2504 
2505 	/* setup classifier to route priorities to flows */
2506 	cpsw_ale_classifier_setup_default(common->ale, common->rx_ch_num_flows);
2507 
2508 	return 0;
2509 
2510 err_request_irq:
2511 	netif_napi_del(&flow->napi_rx);
2512 
2513 err_flow:
2514 	for (--i; i >= 0; i--) {
2515 		flow = &rx_chn->flows[i];
2516 		devm_free_irq(dev, flow->irq, flow);
2517 		netif_napi_del(&flow->napi_rx);
2518 	}
2519 
2520 err:
2521 	am65_cpsw_nuss_free_rx_chns(common);
2522 
2523 	return ret;
2524 }
2525 
am65_cpsw_nuss_init_host_p(struct am65_cpsw_common * common)2526 static int am65_cpsw_nuss_init_host_p(struct am65_cpsw_common *common)
2527 {
2528 	struct am65_cpsw_host *host_p = am65_common_get_host(common);
2529 
2530 	host_p->common = common;
2531 	host_p->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE;
2532 	host_p->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE;
2533 
2534 	return 0;
2535 }
2536 
am65_cpsw_am654_get_efuse_macid(struct device_node * of_node,int slave,u8 * mac_addr)2537 static int am65_cpsw_am654_get_efuse_macid(struct device_node *of_node,
2538 					   int slave, u8 *mac_addr)
2539 {
2540 	u32 mac_lo, mac_hi, offset;
2541 	struct regmap *syscon;
2542 	int ret;
2543 
2544 	syscon = syscon_regmap_lookup_by_phandle(of_node, "ti,syscon-efuse");
2545 	if (IS_ERR(syscon)) {
2546 		if (PTR_ERR(syscon) == -ENODEV)
2547 			return 0;
2548 		return PTR_ERR(syscon);
2549 	}
2550 
2551 	ret = of_property_read_u32_index(of_node, "ti,syscon-efuse", 1,
2552 					 &offset);
2553 	if (ret)
2554 		return ret;
2555 
2556 	regmap_read(syscon, offset, &mac_lo);
2557 	regmap_read(syscon, offset + 4, &mac_hi);
2558 
2559 	mac_addr[0] = (mac_hi >> 8) & 0xff;
2560 	mac_addr[1] = mac_hi & 0xff;
2561 	mac_addr[2] = (mac_lo >> 24) & 0xff;
2562 	mac_addr[3] = (mac_lo >> 16) & 0xff;
2563 	mac_addr[4] = (mac_lo >> 8) & 0xff;
2564 	mac_addr[5] = mac_lo & 0xff;
2565 
2566 	return 0;
2567 }
2568 
am65_cpsw_init_cpts(struct am65_cpsw_common * common)2569 static int am65_cpsw_init_cpts(struct am65_cpsw_common *common)
2570 {
2571 	struct device *dev = common->dev;
2572 	struct device_node *node;
2573 	struct am65_cpts *cpts;
2574 	void __iomem *reg_base;
2575 
2576 	if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS))
2577 		return 0;
2578 
2579 	node = of_get_child_by_name(dev->of_node, "cpts");
2580 	if (!node) {
2581 		dev_err(dev, "%s cpts not found\n", __func__);
2582 		return -ENOENT;
2583 	}
2584 
2585 	reg_base = common->cpsw_base + AM65_CPSW_NU_CPTS_BASE;
2586 	cpts = am65_cpts_create(dev, reg_base, node);
2587 	if (IS_ERR(cpts)) {
2588 		int ret = PTR_ERR(cpts);
2589 
2590 		of_node_put(node);
2591 		dev_err(dev, "cpts create err %d\n", ret);
2592 		return ret;
2593 	}
2594 	common->cpts = cpts;
2595 	/* Forbid PM runtime if CPTS is running.
2596 	 * K3 CPSWxG modules may completely lose context during ON->OFF
2597 	 * transitions depending on integration.
2598 	 * AM65x/J721E MCU CPSW2G: false
2599 	 * J721E MAIN_CPSW9G: true
2600 	 */
2601 	pm_runtime_forbid(dev);
2602 
2603 	return 0;
2604 }
2605 
am65_cpsw_nuss_init_slave_ports(struct am65_cpsw_common * common)2606 static int am65_cpsw_nuss_init_slave_ports(struct am65_cpsw_common *common)
2607 {
2608 	struct device_node *node, *port_np;
2609 	struct device *dev = common->dev;
2610 	int ret;
2611 
2612 	node = of_get_child_by_name(dev->of_node, "ethernet-ports");
2613 	if (!node)
2614 		return -ENOENT;
2615 
2616 	for_each_child_of_node(node, port_np) {
2617 		struct am65_cpsw_port *port;
2618 		u32 port_id;
2619 
2620 		/* it is not a slave port node, continue */
2621 		if (strcmp(port_np->name, "port"))
2622 			continue;
2623 
2624 		ret = of_property_read_u32(port_np, "reg", &port_id);
2625 		if (ret < 0) {
2626 			dev_err(dev, "%pOF error reading port_id %d\n",
2627 				port_np, ret);
2628 			goto of_node_put;
2629 		}
2630 
2631 		if (!port_id || port_id > common->port_num) {
2632 			dev_err(dev, "%pOF has invalid port_id %u %s\n",
2633 				port_np, port_id, port_np->name);
2634 			ret = -EINVAL;
2635 			goto of_node_put;
2636 		}
2637 
2638 		port = am65_common_get_port(common, port_id);
2639 		port->port_id = port_id;
2640 		port->common = common;
2641 		port->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE +
2642 				  AM65_CPSW_NU_PORTS_OFFSET * (port_id);
2643 		if (common->pdata.extra_modes)
2644 			port->sgmii_base = common->ss_base + AM65_CPSW_SGMII_BASE * (port_id);
2645 		port->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE +
2646 				  (AM65_CPSW_NU_STATS_PORT_OFFSET * port_id);
2647 		port->name = of_get_property(port_np, "label", NULL);
2648 		port->fetch_ram_base =
2649 				common->cpsw_base + AM65_CPSW_NU_FRAM_BASE +
2650 				(AM65_CPSW_NU_FRAM_PORT_OFFSET * (port_id - 1));
2651 
2652 		port->slave.mac_sl = cpsw_sl_get("am65", dev, port->port_base);
2653 		if (IS_ERR(port->slave.mac_sl)) {
2654 			ret = PTR_ERR(port->slave.mac_sl);
2655 			goto of_node_put;
2656 		}
2657 
2658 		port->disabled = !of_device_is_available(port_np);
2659 		if (port->disabled) {
2660 			common->disabled_ports_mask |= BIT(port->port_id);
2661 			continue;
2662 		}
2663 
2664 		port->slave.ifphy = devm_of_phy_get(dev, port_np, NULL);
2665 		if (IS_ERR(port->slave.ifphy)) {
2666 			ret = PTR_ERR(port->slave.ifphy);
2667 			dev_err(dev, "%pOF error retrieving port phy: %d\n",
2668 				port_np, ret);
2669 			goto of_node_put;
2670 		}
2671 
2672 		/* Initialize the Serdes PHY for the port */
2673 		ret = am65_cpsw_init_serdes_phy(dev, port_np, port);
2674 		if (ret)
2675 			goto of_node_put;
2676 
2677 		port->slave.mac_only =
2678 				of_property_read_bool(port_np, "ti,mac-only");
2679 
2680 		/* get phy/link info */
2681 		port->slave.port_np = of_node_get(port_np);
2682 		ret = of_get_phy_mode(port_np, &port->slave.phy_if);
2683 		if (ret) {
2684 			dev_err(dev, "%pOF read phy-mode err %d\n",
2685 				port_np, ret);
2686 			goto of_node_put;
2687 		}
2688 
2689 		ret = phy_set_mode_ext(port->slave.ifphy, PHY_MODE_ETHERNET, port->slave.phy_if);
2690 		if (ret)
2691 			goto of_node_put;
2692 
2693 		ret = of_get_mac_address(port_np, port->slave.mac_addr);
2694 		if (ret == -EPROBE_DEFER) {
2695 			goto of_node_put;
2696 		} else if (ret) {
2697 			am65_cpsw_am654_get_efuse_macid(port_np,
2698 							port->port_id,
2699 							port->slave.mac_addr);
2700 			if (!is_valid_ether_addr(port->slave.mac_addr)) {
2701 				eth_random_addr(port->slave.mac_addr);
2702 				dev_info(dev, "Use random MAC address\n");
2703 			}
2704 		}
2705 
2706 		/* Reset all Queue priorities to 0 */
2707 		writel(0, port->port_base + AM65_CPSW_PN_REG_TX_PRI_MAP);
2708 	}
2709 	of_node_put(node);
2710 
2711 	/* is there at least one ext.port */
2712 	if (!(~common->disabled_ports_mask & GENMASK(common->port_num, 1))) {
2713 		dev_err(dev, "No Ext. port are available\n");
2714 		return -ENODEV;
2715 	}
2716 
2717 	return 0;
2718 
2719 of_node_put:
2720 	of_node_put(port_np);
2721 	of_node_put(node);
2722 	return ret;
2723 }
2724 
am65_cpsw_pcpu_stats_free(void * data)2725 static void am65_cpsw_pcpu_stats_free(void *data)
2726 {
2727 	struct am65_cpsw_ndev_stats __percpu *stats = data;
2728 
2729 	free_percpu(stats);
2730 }
2731 
am65_cpsw_nuss_phylink_cleanup(struct am65_cpsw_common * common)2732 static void am65_cpsw_nuss_phylink_cleanup(struct am65_cpsw_common *common)
2733 {
2734 	struct am65_cpsw_port *port;
2735 	int i;
2736 
2737 	for (i = 0; i < common->port_num; i++) {
2738 		port = &common->ports[i];
2739 		if (port->slave.phylink)
2740 			phylink_destroy(port->slave.phylink);
2741 	}
2742 }
2743 
am65_cpsw_remove_dt(struct am65_cpsw_common * common)2744 static void am65_cpsw_remove_dt(struct am65_cpsw_common *common)
2745 {
2746 	struct am65_cpsw_port *port;
2747 	int i;
2748 
2749 	for (i = 0; i < common->port_num; i++) {
2750 		port = &common->ports[i];
2751 		of_node_put(port->slave.port_np);
2752 	}
2753 }
2754 
2755 static int
am65_cpsw_nuss_init_port_ndev(struct am65_cpsw_common * common,u32 port_idx)2756 am65_cpsw_nuss_init_port_ndev(struct am65_cpsw_common *common, u32 port_idx)
2757 {
2758 	struct am65_cpsw_ndev_priv *ndev_priv;
2759 	struct device *dev = common->dev;
2760 	struct am65_cpsw_port *port;
2761 	struct phylink *phylink;
2762 	int ret;
2763 
2764 	port = &common->ports[port_idx];
2765 
2766 	if (port->disabled)
2767 		return 0;
2768 
2769 	/* alloc netdev */
2770 	port->ndev = alloc_etherdev_mqs(sizeof(struct am65_cpsw_ndev_priv),
2771 					AM65_CPSW_MAX_QUEUES,
2772 					AM65_CPSW_MAX_QUEUES);
2773 	if (!port->ndev) {
2774 		dev_err(dev, "error allocating slave net_device %u\n",
2775 			port->port_id);
2776 		return -ENOMEM;
2777 	}
2778 
2779 	ndev_priv = netdev_priv(port->ndev);
2780 	ndev_priv->port = port;
2781 	ndev_priv->msg_enable = AM65_CPSW_DEBUG;
2782 	mutex_init(&ndev_priv->mm_lock);
2783 	port->qos.link_speed = SPEED_UNKNOWN;
2784 	SET_NETDEV_DEV(port->ndev, dev);
2785 	port->ndev->dev.of_node = port->slave.port_np;
2786 
2787 	eth_hw_addr_set(port->ndev, port->slave.mac_addr);
2788 
2789 	port->ndev->min_mtu = AM65_CPSW_MIN_PACKET_SIZE;
2790 	port->ndev->max_mtu = AM65_CPSW_MAX_PACKET_SIZE -
2791 			      (VLAN_ETH_HLEN + ETH_FCS_LEN);
2792 	port->ndev->hw_features = NETIF_F_SG |
2793 				  NETIF_F_RXCSUM |
2794 				  NETIF_F_HW_CSUM |
2795 				  NETIF_F_HW_TC;
2796 	port->ndev->features = port->ndev->hw_features |
2797 			       NETIF_F_HW_VLAN_CTAG_FILTER;
2798 	port->ndev->xdp_features = NETDEV_XDP_ACT_BASIC |
2799 				   NETDEV_XDP_ACT_REDIRECT |
2800 				   NETDEV_XDP_ACT_NDO_XMIT;
2801 	port->ndev->vlan_features |=  NETIF_F_SG;
2802 	port->ndev->netdev_ops = &am65_cpsw_nuss_netdev_ops;
2803 	port->ndev->ethtool_ops = &am65_cpsw_ethtool_ops_slave;
2804 
2805 	/* Configuring Phylink */
2806 	port->slave.phylink_config.dev = &port->ndev->dev;
2807 	port->slave.phylink_config.type = PHYLINK_NETDEV;
2808 	port->slave.phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_10 | MAC_100 |
2809 						      MAC_1000FD | MAC_5000FD;
2810 	port->slave.phylink_config.mac_managed_pm = true; /* MAC does PM */
2811 
2812 	switch (port->slave.phy_if) {
2813 	case PHY_INTERFACE_MODE_RGMII:
2814 	case PHY_INTERFACE_MODE_RGMII_ID:
2815 	case PHY_INTERFACE_MODE_RGMII_RXID:
2816 	case PHY_INTERFACE_MODE_RGMII_TXID:
2817 		phy_interface_set_rgmii(port->slave.phylink_config.supported_interfaces);
2818 		break;
2819 
2820 	case PHY_INTERFACE_MODE_RMII:
2821 		__set_bit(PHY_INTERFACE_MODE_RMII,
2822 			  port->slave.phylink_config.supported_interfaces);
2823 		break;
2824 
2825 	case PHY_INTERFACE_MODE_QSGMII:
2826 	case PHY_INTERFACE_MODE_SGMII:
2827 	case PHY_INTERFACE_MODE_USXGMII:
2828 		if (common->pdata.extra_modes & BIT(port->slave.phy_if)) {
2829 			__set_bit(port->slave.phy_if,
2830 				  port->slave.phylink_config.supported_interfaces);
2831 		} else {
2832 			dev_err(dev, "selected phy-mode is not supported\n");
2833 			return -EOPNOTSUPP;
2834 		}
2835 		break;
2836 
2837 	default:
2838 		dev_err(dev, "selected phy-mode is not supported\n");
2839 		return -EOPNOTSUPP;
2840 	}
2841 
2842 	phylink = phylink_create(&port->slave.phylink_config,
2843 				 of_fwnode_handle(port->slave.port_np),
2844 				 port->slave.phy_if,
2845 				 &am65_cpsw_phylink_mac_ops);
2846 	if (IS_ERR(phylink))
2847 		return PTR_ERR(phylink);
2848 
2849 	port->slave.phylink = phylink;
2850 
2851 	/* Disable TX checksum offload by default due to HW bug */
2852 	if (common->pdata.quirks & AM65_CPSW_QUIRK_I2027_NO_TX_CSUM)
2853 		port->ndev->features &= ~NETIF_F_HW_CSUM;
2854 
2855 	ndev_priv->stats = netdev_alloc_pcpu_stats(struct am65_cpsw_ndev_stats);
2856 	if (!ndev_priv->stats)
2857 		return -ENOMEM;
2858 
2859 	ret = devm_add_action_or_reset(dev, am65_cpsw_pcpu_stats_free,
2860 				       ndev_priv->stats);
2861 	if (ret)
2862 		dev_err(dev, "failed to add percpu stat free action %d\n", ret);
2863 
2864 	port->xdp_prog = NULL;
2865 
2866 	if (!common->dma_ndev)
2867 		common->dma_ndev = port->ndev;
2868 
2869 	return ret;
2870 }
2871 
am65_cpsw_nuss_init_ndevs(struct am65_cpsw_common * common)2872 static int am65_cpsw_nuss_init_ndevs(struct am65_cpsw_common *common)
2873 {
2874 	int ret;
2875 	int i;
2876 
2877 	for (i = 0; i < common->port_num; i++) {
2878 		ret = am65_cpsw_nuss_init_port_ndev(common, i);
2879 		if (ret)
2880 			return ret;
2881 	}
2882 
2883 	return ret;
2884 }
2885 
am65_cpsw_nuss_cleanup_ndev(struct am65_cpsw_common * common)2886 static void am65_cpsw_nuss_cleanup_ndev(struct am65_cpsw_common *common)
2887 {
2888 	struct am65_cpsw_port *port;
2889 	int i;
2890 
2891 	for (i = 0; i < common->port_num; i++) {
2892 		port = &common->ports[i];
2893 		if (!port->ndev)
2894 			continue;
2895 		if (port->ndev->reg_state == NETREG_REGISTERED)
2896 			unregister_netdev(port->ndev);
2897 		free_netdev(port->ndev);
2898 		port->ndev = NULL;
2899 	}
2900 }
2901 
am65_cpsw_port_offload_fwd_mark_update(struct am65_cpsw_common * common)2902 static void am65_cpsw_port_offload_fwd_mark_update(struct am65_cpsw_common *common)
2903 {
2904 	int set_val = 0;
2905 	int i;
2906 
2907 	if (common->br_members == (GENMASK(common->port_num, 1) & ~common->disabled_ports_mask))
2908 		set_val = 1;
2909 
2910 	dev_dbg(common->dev, "set offload_fwd_mark %d\n", set_val);
2911 
2912 	for (i = 1; i <= common->port_num; i++) {
2913 		struct am65_cpsw_port *port = am65_common_get_port(common, i);
2914 		struct am65_cpsw_ndev_priv *priv;
2915 
2916 		if (!port->ndev)
2917 			continue;
2918 
2919 		priv = am65_ndev_to_priv(port->ndev);
2920 		priv->offload_fwd_mark = set_val;
2921 	}
2922 }
2923 
am65_cpsw_port_dev_check(const struct net_device * ndev)2924 bool am65_cpsw_port_dev_check(const struct net_device *ndev)
2925 {
2926 	if (ndev->netdev_ops == &am65_cpsw_nuss_netdev_ops) {
2927 		struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
2928 
2929 		return !common->is_emac_mode;
2930 	}
2931 
2932 	return false;
2933 }
2934 
am65_cpsw_netdevice_port_link(struct net_device * ndev,struct net_device * br_ndev,struct netlink_ext_ack * extack)2935 static int am65_cpsw_netdevice_port_link(struct net_device *ndev,
2936 					 struct net_device *br_ndev,
2937 					 struct netlink_ext_ack *extack)
2938 {
2939 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
2940 	struct am65_cpsw_ndev_priv *priv = am65_ndev_to_priv(ndev);
2941 	int err;
2942 
2943 	if (!common->br_members) {
2944 		common->hw_bridge_dev = br_ndev;
2945 	} else {
2946 		/* This is adding the port to a second bridge, this is
2947 		 * unsupported
2948 		 */
2949 		if (common->hw_bridge_dev != br_ndev)
2950 			return -EOPNOTSUPP;
2951 	}
2952 
2953 	err = switchdev_bridge_port_offload(ndev, ndev, NULL, NULL, NULL,
2954 					    false, extack);
2955 	if (err)
2956 		return err;
2957 
2958 	common->br_members |= BIT(priv->port->port_id);
2959 
2960 	am65_cpsw_port_offload_fwd_mark_update(common);
2961 
2962 	return NOTIFY_DONE;
2963 }
2964 
am65_cpsw_netdevice_port_unlink(struct net_device * ndev)2965 static void am65_cpsw_netdevice_port_unlink(struct net_device *ndev)
2966 {
2967 	struct am65_cpsw_common *common = am65_ndev_to_common(ndev);
2968 	struct am65_cpsw_ndev_priv *priv = am65_ndev_to_priv(ndev);
2969 
2970 	switchdev_bridge_port_unoffload(ndev, NULL, NULL, NULL);
2971 
2972 	common->br_members &= ~BIT(priv->port->port_id);
2973 
2974 	am65_cpsw_port_offload_fwd_mark_update(common);
2975 
2976 	if (!common->br_members)
2977 		common->hw_bridge_dev = NULL;
2978 }
2979 
2980 /* netdev notifier */
am65_cpsw_netdevice_event(struct notifier_block * unused,unsigned long event,void * ptr)2981 static int am65_cpsw_netdevice_event(struct notifier_block *unused,
2982 				     unsigned long event, void *ptr)
2983 {
2984 	struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
2985 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
2986 	struct netdev_notifier_changeupper_info *info;
2987 	int ret = NOTIFY_DONE;
2988 
2989 	if (!am65_cpsw_port_dev_check(ndev))
2990 		return NOTIFY_DONE;
2991 
2992 	switch (event) {
2993 	case NETDEV_CHANGEUPPER:
2994 		info = ptr;
2995 
2996 		if (netif_is_bridge_master(info->upper_dev)) {
2997 			if (info->linking)
2998 				ret = am65_cpsw_netdevice_port_link(ndev,
2999 								    info->upper_dev,
3000 								    extack);
3001 			else
3002 				am65_cpsw_netdevice_port_unlink(ndev);
3003 		}
3004 		break;
3005 	default:
3006 		return NOTIFY_DONE;
3007 	}
3008 
3009 	return notifier_from_errno(ret);
3010 }
3011 
am65_cpsw_register_notifiers(struct am65_cpsw_common * cpsw)3012 static int am65_cpsw_register_notifiers(struct am65_cpsw_common *cpsw)
3013 {
3014 	int ret = 0;
3015 
3016 	if (AM65_CPSW_IS_CPSW2G(cpsw) ||
3017 	    !IS_REACHABLE(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV))
3018 		return 0;
3019 
3020 	cpsw->am65_cpsw_netdevice_nb.notifier_call = &am65_cpsw_netdevice_event;
3021 	ret = register_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb);
3022 	if (ret) {
3023 		dev_err(cpsw->dev, "can't register netdevice notifier\n");
3024 		return ret;
3025 	}
3026 
3027 	ret = am65_cpsw_switchdev_register_notifiers(cpsw);
3028 	if (ret)
3029 		unregister_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb);
3030 
3031 	return ret;
3032 }
3033 
am65_cpsw_unregister_notifiers(struct am65_cpsw_common * cpsw)3034 static void am65_cpsw_unregister_notifiers(struct am65_cpsw_common *cpsw)
3035 {
3036 	if (AM65_CPSW_IS_CPSW2G(cpsw) ||
3037 	    !IS_REACHABLE(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV))
3038 		return;
3039 
3040 	am65_cpsw_switchdev_unregister_notifiers(cpsw);
3041 	unregister_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb);
3042 }
3043 
3044 static const struct devlink_ops am65_cpsw_devlink_ops = {};
3045 
am65_cpsw_init_stp_ale_entry(struct am65_cpsw_common * cpsw)3046 static void am65_cpsw_init_stp_ale_entry(struct am65_cpsw_common *cpsw)
3047 {
3048 	cpsw_ale_add_mcast(cpsw->ale, eth_stp_addr, ALE_PORT_HOST, ALE_SUPER, 0,
3049 			   ALE_MCAST_BLOCK_LEARN_FWD);
3050 }
3051 
am65_cpsw_init_host_port_switch(struct am65_cpsw_common * common)3052 static void am65_cpsw_init_host_port_switch(struct am65_cpsw_common *common)
3053 {
3054 	struct am65_cpsw_host *host = am65_common_get_host(common);
3055 
3056 	writel(common->default_vlan, host->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
3057 
3058 	am65_cpsw_init_stp_ale_entry(common);
3059 
3060 	cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 1);
3061 	dev_dbg(common->dev, "Set P0_UNI_FLOOD\n");
3062 	cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 0);
3063 }
3064 
am65_cpsw_init_host_port_emac(struct am65_cpsw_common * common)3065 static void am65_cpsw_init_host_port_emac(struct am65_cpsw_common *common)
3066 {
3067 	struct am65_cpsw_host *host = am65_common_get_host(common);
3068 
3069 	writel(0, host->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
3070 
3071 	cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 0);
3072 	dev_dbg(common->dev, "unset P0_UNI_FLOOD\n");
3073 
3074 	/* learning make no sense in multi-mac mode */
3075 	cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 1);
3076 }
3077 
am65_cpsw_dl_switch_mode_get(struct devlink * dl,u32 id,struct devlink_param_gset_ctx * ctx)3078 static int am65_cpsw_dl_switch_mode_get(struct devlink *dl, u32 id,
3079 					struct devlink_param_gset_ctx *ctx)
3080 {
3081 	struct am65_cpsw_devlink *dl_priv = devlink_priv(dl);
3082 	struct am65_cpsw_common *common = dl_priv->common;
3083 
3084 	dev_dbg(common->dev, "%s id:%u\n", __func__, id);
3085 
3086 	if (id != AM65_CPSW_DL_PARAM_SWITCH_MODE)
3087 		return -EOPNOTSUPP;
3088 
3089 	ctx->val.vbool = !common->is_emac_mode;
3090 
3091 	return 0;
3092 }
3093 
am65_cpsw_init_port_emac_ale(struct am65_cpsw_port * port)3094 static void am65_cpsw_init_port_emac_ale(struct  am65_cpsw_port *port)
3095 {
3096 	struct am65_cpsw_slave_data *slave = &port->slave;
3097 	struct am65_cpsw_common *common = port->common;
3098 	u32 port_mask;
3099 
3100 	writel(slave->port_vlan, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
3101 
3102 	if (slave->mac_only)
3103 		/* enable mac-only mode on port */
3104 		cpsw_ale_control_set(common->ale, port->port_id,
3105 				     ALE_PORT_MACONLY, 1);
3106 
3107 	cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_NOLEARN, 1);
3108 
3109 	port_mask = BIT(port->port_id) | ALE_PORT_HOST;
3110 
3111 	cpsw_ale_add_ucast(common->ale, port->ndev->dev_addr,
3112 			   HOST_PORT_NUM, ALE_SECURE, slave->port_vlan);
3113 	cpsw_ale_add_mcast(common->ale, port->ndev->broadcast,
3114 			   port_mask, ALE_VLAN, slave->port_vlan, ALE_MCAST_FWD_2);
3115 }
3116 
am65_cpsw_init_port_switch_ale(struct am65_cpsw_port * port)3117 static void am65_cpsw_init_port_switch_ale(struct am65_cpsw_port *port)
3118 {
3119 	struct am65_cpsw_slave_data *slave = &port->slave;
3120 	struct am65_cpsw_common *cpsw = port->common;
3121 	u32 port_mask;
3122 
3123 	cpsw_ale_control_set(cpsw->ale, port->port_id,
3124 			     ALE_PORT_NOLEARN, 0);
3125 
3126 	cpsw_ale_add_ucast(cpsw->ale, port->ndev->dev_addr,
3127 			   HOST_PORT_NUM, ALE_SECURE | ALE_BLOCKED | ALE_VLAN,
3128 			   slave->port_vlan);
3129 
3130 	port_mask = BIT(port->port_id) | ALE_PORT_HOST;
3131 
3132 	cpsw_ale_add_mcast(cpsw->ale, port->ndev->broadcast,
3133 			   port_mask, ALE_VLAN, slave->port_vlan,
3134 			   ALE_MCAST_FWD_2);
3135 
3136 	writel(slave->port_vlan, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
3137 
3138 	cpsw_ale_control_set(cpsw->ale, port->port_id,
3139 			     ALE_PORT_MACONLY, 0);
3140 }
3141 
am65_cpsw_dl_switch_mode_set(struct devlink * dl,u32 id,struct devlink_param_gset_ctx * ctx,struct netlink_ext_ack * extack)3142 static int am65_cpsw_dl_switch_mode_set(struct devlink *dl, u32 id,
3143 					struct devlink_param_gset_ctx *ctx,
3144 					struct netlink_ext_ack *extack)
3145 {
3146 	struct am65_cpsw_devlink *dl_priv = devlink_priv(dl);
3147 	struct am65_cpsw_common *cpsw = dl_priv->common;
3148 	bool switch_en = ctx->val.vbool;
3149 	bool if_running = false;
3150 	int i;
3151 
3152 	dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id);
3153 
3154 	if (id != AM65_CPSW_DL_PARAM_SWITCH_MODE)
3155 		return -EOPNOTSUPP;
3156 
3157 	if (switch_en == !cpsw->is_emac_mode)
3158 		return 0;
3159 
3160 	if (!switch_en && cpsw->br_members) {
3161 		dev_err(cpsw->dev, "Remove ports from bridge before disabling switch mode\n");
3162 		return -EINVAL;
3163 	}
3164 
3165 	rtnl_lock();
3166 
3167 	cpsw->is_emac_mode = !switch_en;
3168 
3169 	for (i = 0; i < cpsw->port_num; i++) {
3170 		struct net_device *sl_ndev = cpsw->ports[i].ndev;
3171 
3172 		if (!sl_ndev || !netif_running(sl_ndev))
3173 			continue;
3174 
3175 		if_running = true;
3176 	}
3177 
3178 	if (!if_running) {
3179 		/* all ndevs are down */
3180 		for (i = 0; i < cpsw->port_num; i++) {
3181 			struct net_device *sl_ndev = cpsw->ports[i].ndev;
3182 			struct am65_cpsw_slave_data *slave;
3183 
3184 			if (!sl_ndev)
3185 				continue;
3186 
3187 			slave = am65_ndev_to_slave(sl_ndev);
3188 			if (switch_en)
3189 				slave->port_vlan = cpsw->default_vlan;
3190 			else
3191 				slave->port_vlan = 0;
3192 		}
3193 
3194 		goto exit;
3195 	}
3196 
3197 	cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1);
3198 	/* clean up ALE table */
3199 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_CLEAR, 1);
3200 	cpsw_ale_control_get(cpsw->ale, HOST_PORT_NUM, ALE_AGEOUT);
3201 
3202 	if (switch_en) {
3203 		dev_info(cpsw->dev, "Enable switch mode\n");
3204 
3205 		am65_cpsw_init_host_port_switch(cpsw);
3206 
3207 		for (i = 0; i < cpsw->port_num; i++) {
3208 			struct net_device *sl_ndev = cpsw->ports[i].ndev;
3209 			struct am65_cpsw_slave_data *slave;
3210 			struct am65_cpsw_port *port;
3211 
3212 			if (!sl_ndev)
3213 				continue;
3214 
3215 			port = am65_ndev_to_port(sl_ndev);
3216 			slave = am65_ndev_to_slave(sl_ndev);
3217 			slave->port_vlan = cpsw->default_vlan;
3218 
3219 			if (netif_running(sl_ndev))
3220 				am65_cpsw_init_port_switch_ale(port);
3221 		}
3222 
3223 	} else {
3224 		dev_info(cpsw->dev, "Disable switch mode\n");
3225 
3226 		am65_cpsw_init_host_port_emac(cpsw);
3227 
3228 		for (i = 0; i < cpsw->port_num; i++) {
3229 			struct net_device *sl_ndev = cpsw->ports[i].ndev;
3230 			struct am65_cpsw_port *port;
3231 
3232 			if (!sl_ndev)
3233 				continue;
3234 
3235 			port = am65_ndev_to_port(sl_ndev);
3236 			port->slave.port_vlan = 0;
3237 			if (netif_running(sl_ndev))
3238 				am65_cpsw_init_port_emac_ale(port);
3239 		}
3240 	}
3241 	cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_BYPASS, 0);
3242 exit:
3243 	rtnl_unlock();
3244 
3245 	return 0;
3246 }
3247 
3248 static const struct devlink_param am65_cpsw_devlink_params[] = {
3249 	DEVLINK_PARAM_DRIVER(AM65_CPSW_DL_PARAM_SWITCH_MODE, "switch_mode",
3250 			     DEVLINK_PARAM_TYPE_BOOL,
3251 			     BIT(DEVLINK_PARAM_CMODE_RUNTIME),
3252 			     am65_cpsw_dl_switch_mode_get,
3253 			     am65_cpsw_dl_switch_mode_set, NULL),
3254 };
3255 
am65_cpsw_nuss_register_devlink(struct am65_cpsw_common * common)3256 static int am65_cpsw_nuss_register_devlink(struct am65_cpsw_common *common)
3257 {
3258 	struct devlink_port_attrs attrs = {};
3259 	struct am65_cpsw_devlink *dl_priv;
3260 	struct device *dev = common->dev;
3261 	struct devlink_port *dl_port;
3262 	struct am65_cpsw_port *port;
3263 	int ret = 0;
3264 	int i;
3265 
3266 	common->devlink =
3267 		devlink_alloc(&am65_cpsw_devlink_ops, sizeof(*dl_priv), dev);
3268 	if (!common->devlink)
3269 		return -ENOMEM;
3270 
3271 	dl_priv = devlink_priv(common->devlink);
3272 	dl_priv->common = common;
3273 
3274 	/* Provide devlink hook to switch mode when multiple external ports
3275 	 * are present NUSS switchdev driver is enabled.
3276 	 */
3277 	if (!AM65_CPSW_IS_CPSW2G(common) &&
3278 	    IS_ENABLED(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV)) {
3279 		ret = devlink_params_register(common->devlink,
3280 					      am65_cpsw_devlink_params,
3281 					      ARRAY_SIZE(am65_cpsw_devlink_params));
3282 		if (ret) {
3283 			dev_err(dev, "devlink params reg fail ret:%d\n", ret);
3284 			goto dl_unreg;
3285 		}
3286 	}
3287 
3288 	for (i = 1; i <= common->port_num; i++) {
3289 		port = am65_common_get_port(common, i);
3290 		dl_port = &port->devlink_port;
3291 
3292 		if (port->ndev)
3293 			attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
3294 		else
3295 			attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED;
3296 		attrs.phys.port_number = port->port_id;
3297 		attrs.switch_id.id_len = sizeof(resource_size_t);
3298 		memcpy(attrs.switch_id.id, common->switch_id, attrs.switch_id.id_len);
3299 		devlink_port_attrs_set(dl_port, &attrs);
3300 
3301 		ret = devlink_port_register(common->devlink, dl_port, port->port_id);
3302 		if (ret) {
3303 			dev_err(dev, "devlink_port reg fail for port %d, ret:%d\n",
3304 				port->port_id, ret);
3305 			goto dl_port_unreg;
3306 		}
3307 	}
3308 	devlink_register(common->devlink);
3309 	return ret;
3310 
3311 dl_port_unreg:
3312 	for (i = i - 1; i >= 1; i--) {
3313 		port = am65_common_get_port(common, i);
3314 		dl_port = &port->devlink_port;
3315 
3316 		devlink_port_unregister(dl_port);
3317 	}
3318 dl_unreg:
3319 	devlink_free(common->devlink);
3320 	return ret;
3321 }
3322 
am65_cpsw_unregister_devlink(struct am65_cpsw_common * common)3323 static void am65_cpsw_unregister_devlink(struct am65_cpsw_common *common)
3324 {
3325 	struct devlink_port *dl_port;
3326 	struct am65_cpsw_port *port;
3327 	int i;
3328 
3329 	devlink_unregister(common->devlink);
3330 
3331 	for (i = 1; i <= common->port_num; i++) {
3332 		port = am65_common_get_port(common, i);
3333 		dl_port = &port->devlink_port;
3334 
3335 		devlink_port_unregister(dl_port);
3336 	}
3337 
3338 	if (!AM65_CPSW_IS_CPSW2G(common) &&
3339 	    IS_ENABLED(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV))
3340 		devlink_params_unregister(common->devlink,
3341 					  am65_cpsw_devlink_params,
3342 					  ARRAY_SIZE(am65_cpsw_devlink_params));
3343 
3344 	devlink_free(common->devlink);
3345 }
3346 
am65_cpsw_nuss_register_ndevs(struct am65_cpsw_common * common)3347 static int am65_cpsw_nuss_register_ndevs(struct am65_cpsw_common *common)
3348 {
3349 	struct am65_cpsw_rx_chn *rx_chan = &common->rx_chns;
3350 	struct am65_cpsw_tx_chn *tx_chan = common->tx_chns;
3351 	struct device *dev = common->dev;
3352 	struct am65_cpsw_port *port;
3353 	int ret = 0, i;
3354 
3355 	/* init tx channels */
3356 	ret = am65_cpsw_nuss_init_tx_chns(common);
3357 	if (ret)
3358 		return ret;
3359 	ret = am65_cpsw_nuss_init_rx_chns(common);
3360 	if (ret)
3361 		goto err_remove_tx;
3362 
3363 	/* The DMA Channels are not guaranteed to be in a clean state.
3364 	 * Reset and disable them to ensure that they are back to the
3365 	 * clean state and ready to be used.
3366 	 */
3367 	for (i = 0; i < common->tx_ch_num; i++) {
3368 		k3_udma_glue_reset_tx_chn(tx_chan[i].tx_chn, &tx_chan[i],
3369 					  am65_cpsw_nuss_tx_cleanup);
3370 		k3_udma_glue_disable_tx_chn(tx_chan[i].tx_chn);
3371 	}
3372 
3373 	for (i = 0; i < common->rx_ch_num_flows; i++)
3374 		k3_udma_glue_reset_rx_chn(rx_chan->rx_chn, i,
3375 					  rx_chan,
3376 					  am65_cpsw_nuss_rx_cleanup, !!i);
3377 
3378 	k3_udma_glue_disable_rx_chn(rx_chan->rx_chn);
3379 
3380 	ret = am65_cpsw_nuss_register_devlink(common);
3381 	if (ret)
3382 		goto err_remove_rx;
3383 
3384 	for (i = 0; i < common->port_num; i++) {
3385 		port = &common->ports[i];
3386 
3387 		if (!port->ndev)
3388 			continue;
3389 
3390 		SET_NETDEV_DEVLINK_PORT(port->ndev, &port->devlink_port);
3391 
3392 		ret = register_netdev(port->ndev);
3393 		if (ret) {
3394 			dev_err(dev, "error registering slave net device%i %d\n",
3395 				i, ret);
3396 			goto err_cleanup_ndev;
3397 		}
3398 	}
3399 
3400 	ret = am65_cpsw_register_notifiers(common);
3401 	if (ret)
3402 		goto err_cleanup_ndev;
3403 
3404 	/* can't auto unregister ndev using devm_add_action() due to
3405 	 * devres release sequence in DD core for DMA
3406 	 */
3407 
3408 	return 0;
3409 
3410 err_cleanup_ndev:
3411 	am65_cpsw_nuss_cleanup_ndev(common);
3412 	am65_cpsw_unregister_devlink(common);
3413 err_remove_rx:
3414 	am65_cpsw_nuss_remove_rx_chns(common);
3415 err_remove_tx:
3416 	am65_cpsw_nuss_remove_tx_chns(common);
3417 
3418 	return ret;
3419 }
3420 
am65_cpsw_nuss_update_tx_rx_chns(struct am65_cpsw_common * common,int num_tx,int num_rx)3421 int am65_cpsw_nuss_update_tx_rx_chns(struct am65_cpsw_common *common,
3422 				     int num_tx, int num_rx)
3423 {
3424 	int ret;
3425 
3426 	am65_cpsw_nuss_remove_tx_chns(common);
3427 	am65_cpsw_nuss_remove_rx_chns(common);
3428 
3429 	common->tx_ch_num = num_tx;
3430 	common->rx_ch_num_flows = num_rx;
3431 	ret = am65_cpsw_nuss_init_tx_chns(common);
3432 	if (ret)
3433 		return ret;
3434 
3435 	ret = am65_cpsw_nuss_init_rx_chns(common);
3436 	if (ret)
3437 		am65_cpsw_nuss_remove_tx_chns(common);
3438 
3439 	return ret;
3440 }
3441 
3442 struct am65_cpsw_soc_pdata {
3443 	u32	quirks_dis;
3444 };
3445 
3446 static const struct am65_cpsw_soc_pdata am65x_soc_sr2_0 = {
3447 	.quirks_dis = AM65_CPSW_QUIRK_I2027_NO_TX_CSUM,
3448 };
3449 
3450 static const struct soc_device_attribute am65_cpsw_socinfo[] = {
3451 	{ .family = "AM65X",
3452 	  .revision = "SR2.0",
3453 	  .data = &am65x_soc_sr2_0
3454 	},
3455 	{/* sentinel */}
3456 };
3457 
3458 static const struct am65_cpsw_pdata am65x_sr1_0 = {
3459 	.quirks = AM65_CPSW_QUIRK_I2027_NO_TX_CSUM,
3460 	.ale_dev_id = "am65x-cpsw2g",
3461 	.fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE,
3462 };
3463 
3464 static const struct am65_cpsw_pdata j721e_pdata = {
3465 	.quirks = 0,
3466 	.ale_dev_id = "am65x-cpsw2g",
3467 	.fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE,
3468 };
3469 
3470 static const struct am65_cpsw_pdata am64x_cpswxg_pdata = {
3471 	.quirks = AM64_CPSW_QUIRK_DMA_RX_TDOWN_IRQ,
3472 	.ale_dev_id = "am64-cpswxg",
3473 	.fdqring_mode = K3_RINGACC_RING_MODE_RING,
3474 };
3475 
3476 static const struct am65_cpsw_pdata j7200_cpswxg_pdata = {
3477 	.quirks = 0,
3478 	.ale_dev_id = "am64-cpswxg",
3479 	.fdqring_mode = K3_RINGACC_RING_MODE_RING,
3480 	.extra_modes = BIT(PHY_INTERFACE_MODE_QSGMII) | BIT(PHY_INTERFACE_MODE_SGMII),
3481 };
3482 
3483 static const struct am65_cpsw_pdata j721e_cpswxg_pdata = {
3484 	.quirks = 0,
3485 	.ale_dev_id = "am64-cpswxg",
3486 	.fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE,
3487 	.extra_modes = BIT(PHY_INTERFACE_MODE_QSGMII) | BIT(PHY_INTERFACE_MODE_SGMII),
3488 };
3489 
3490 static const struct am65_cpsw_pdata j784s4_cpswxg_pdata = {
3491 	.quirks = 0,
3492 	.ale_dev_id = "am64-cpswxg",
3493 	.fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE,
3494 	.extra_modes = BIT(PHY_INTERFACE_MODE_QSGMII) | BIT(PHY_INTERFACE_MODE_SGMII) |
3495 		       BIT(PHY_INTERFACE_MODE_USXGMII),
3496 };
3497 
3498 static const struct of_device_id am65_cpsw_nuss_of_mtable[] = {
3499 	{ .compatible = "ti,am654-cpsw-nuss", .data = &am65x_sr1_0},
3500 	{ .compatible = "ti,j721e-cpsw-nuss", .data = &j721e_pdata},
3501 	{ .compatible = "ti,am642-cpsw-nuss", .data = &am64x_cpswxg_pdata},
3502 	{ .compatible = "ti,j7200-cpswxg-nuss", .data = &j7200_cpswxg_pdata},
3503 	{ .compatible = "ti,j721e-cpswxg-nuss", .data = &j721e_cpswxg_pdata},
3504 	{ .compatible = "ti,j784s4-cpswxg-nuss", .data = &j784s4_cpswxg_pdata},
3505 	{ /* sentinel */ },
3506 };
3507 MODULE_DEVICE_TABLE(of, am65_cpsw_nuss_of_mtable);
3508 
am65_cpsw_nuss_apply_socinfo(struct am65_cpsw_common * common)3509 static void am65_cpsw_nuss_apply_socinfo(struct am65_cpsw_common *common)
3510 {
3511 	const struct soc_device_attribute *soc;
3512 
3513 	soc = soc_device_match(am65_cpsw_socinfo);
3514 	if (soc && soc->data) {
3515 		const struct am65_cpsw_soc_pdata *socdata = soc->data;
3516 
3517 		/* disable quirks */
3518 		common->pdata.quirks &= ~socdata->quirks_dis;
3519 	}
3520 }
3521 
am65_cpsw_nuss_probe(struct platform_device * pdev)3522 static int am65_cpsw_nuss_probe(struct platform_device *pdev)
3523 {
3524 	struct cpsw_ale_params ale_params = { 0 };
3525 	const struct of_device_id *of_id;
3526 	struct device *dev = &pdev->dev;
3527 	struct am65_cpsw_common *common;
3528 	struct device_node *node;
3529 	struct resource *res;
3530 	struct clk *clk;
3531 	int ale_entries;
3532 	u64 id_temp;
3533 	int ret, i;
3534 
3535 	common = devm_kzalloc(dev, sizeof(struct am65_cpsw_common), GFP_KERNEL);
3536 	if (!common)
3537 		return -ENOMEM;
3538 	common->dev = dev;
3539 
3540 	of_id = of_match_device(am65_cpsw_nuss_of_mtable, dev);
3541 	if (!of_id)
3542 		return -EINVAL;
3543 	common->pdata = *(const struct am65_cpsw_pdata *)of_id->data;
3544 
3545 	am65_cpsw_nuss_apply_socinfo(common);
3546 
3547 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cpsw_nuss");
3548 	common->ss_base = devm_ioremap_resource(&pdev->dev, res);
3549 	if (IS_ERR(common->ss_base))
3550 		return PTR_ERR(common->ss_base);
3551 	common->cpsw_base = common->ss_base + AM65_CPSW_CPSW_NU_BASE;
3552 	/* Use device's physical base address as switch id */
3553 	id_temp = cpu_to_be64(res->start);
3554 	memcpy(common->switch_id, &id_temp, sizeof(res->start));
3555 
3556 	node = of_get_child_by_name(dev->of_node, "ethernet-ports");
3557 	if (!node)
3558 		return -ENOENT;
3559 	common->port_num = of_get_child_count(node);
3560 	of_node_put(node);
3561 	if (common->port_num < 1 || common->port_num > AM65_CPSW_MAX_PORTS)
3562 		return -ENOENT;
3563 
3564 	common->rx_flow_id_base = -1;
3565 	init_completion(&common->tdown_complete);
3566 	common->tx_ch_num = AM65_CPSW_DEFAULT_TX_CHNS;
3567 	common->rx_ch_num_flows = AM65_CPSW_DEFAULT_RX_CHN_FLOWS;
3568 	common->pf_p0_rx_ptype_rrobin = true;
3569 	common->default_vlan = 1;
3570 
3571 	common->ports = devm_kcalloc(dev, common->port_num,
3572 				     sizeof(*common->ports),
3573 				     GFP_KERNEL);
3574 	if (!common->ports)
3575 		return -ENOMEM;
3576 
3577 	clk = devm_clk_get(dev, "fck");
3578 	if (IS_ERR(clk))
3579 		return dev_err_probe(dev, PTR_ERR(clk), "getting fck clock\n");
3580 	common->bus_freq = clk_get_rate(clk);
3581 
3582 	pm_runtime_enable(dev);
3583 	ret = pm_runtime_resume_and_get(dev);
3584 	if (ret < 0) {
3585 		pm_runtime_disable(dev);
3586 		return ret;
3587 	}
3588 
3589 	am65_cpsw_nuss_get_ver(common);
3590 
3591 	ret = am65_cpsw_nuss_init_host_p(common);
3592 	if (ret)
3593 		goto err_pm_clear;
3594 
3595 	ret = am65_cpsw_nuss_init_slave_ports(common);
3596 	if (ret)
3597 		goto err_pm_clear;
3598 
3599 	node = of_get_child_by_name(dev->of_node, "mdio");
3600 	if (!node) {
3601 		dev_warn(dev, "MDIO node not found\n");
3602 	} else if (of_device_is_available(node)) {
3603 		struct platform_device *mdio_pdev;
3604 
3605 		mdio_pdev = of_platform_device_create(node, NULL, dev);
3606 		if (!mdio_pdev) {
3607 			ret = -ENODEV;
3608 			goto err_pm_clear;
3609 		}
3610 
3611 		common->mdio_dev =  &mdio_pdev->dev;
3612 	}
3613 	of_node_put(node);
3614 
3615 	/* init common data */
3616 	ale_params.dev = dev;
3617 	ale_params.ale_ageout = AM65_CPSW_ALE_AGEOUT_DEFAULT;
3618 	ale_params.ale_ports = common->port_num + 1;
3619 	ale_params.ale_regs = common->cpsw_base + AM65_CPSW_NU_ALE_BASE;
3620 	ale_params.dev_id = common->pdata.ale_dev_id;
3621 	ale_params.bus_freq = common->bus_freq;
3622 
3623 	common->ale = cpsw_ale_create(&ale_params);
3624 	if (IS_ERR(common->ale)) {
3625 		dev_err(dev, "error initializing ale engine\n");
3626 		ret = PTR_ERR(common->ale);
3627 		goto err_of_clear;
3628 	}
3629 
3630 	ale_entries = common->ale->params.ale_entries;
3631 	common->ale_context = devm_kzalloc(dev,
3632 					   ale_entries * ALE_ENTRY_WORDS * sizeof(u32),
3633 					   GFP_KERNEL);
3634 	ret = am65_cpsw_init_cpts(common);
3635 	if (ret)
3636 		goto err_of_clear;
3637 
3638 	/* init ports */
3639 	for (i = 0; i < common->port_num; i++)
3640 		am65_cpsw_nuss_slave_disable_unused(&common->ports[i]);
3641 
3642 	dev_set_drvdata(dev, common);
3643 
3644 	common->is_emac_mode = true;
3645 
3646 	ret = am65_cpsw_nuss_init_ndevs(common);
3647 	if (ret)
3648 		goto err_ndevs_clear;
3649 
3650 	ret = am65_cpsw_nuss_register_ndevs(common);
3651 	if (ret)
3652 		goto err_ndevs_clear;
3653 
3654 	pm_runtime_put(dev);
3655 	return 0;
3656 
3657 err_ndevs_clear:
3658 	am65_cpsw_nuss_cleanup_ndev(common);
3659 	am65_cpsw_nuss_phylink_cleanup(common);
3660 	am65_cpts_release(common->cpts);
3661 	am65_cpsw_remove_dt(common);
3662 err_of_clear:
3663 	if (common->mdio_dev)
3664 		of_platform_device_destroy(common->mdio_dev, NULL);
3665 err_pm_clear:
3666 	pm_runtime_put_sync(dev);
3667 	pm_runtime_disable(dev);
3668 	return ret;
3669 }
3670 
am65_cpsw_nuss_remove(struct platform_device * pdev)3671 static void am65_cpsw_nuss_remove(struct platform_device *pdev)
3672 {
3673 	struct device *dev = &pdev->dev;
3674 	struct am65_cpsw_common *common;
3675 	int ret;
3676 
3677 	common = dev_get_drvdata(dev);
3678 
3679 	ret = pm_runtime_resume_and_get(&pdev->dev);
3680 	if (ret < 0) {
3681 		/* Note, if this error path is taken, we're leaking some
3682 		 * resources.
3683 		 */
3684 		dev_err(&pdev->dev, "Failed to resume device (%pe)\n",
3685 			ERR_PTR(ret));
3686 		return;
3687 	}
3688 
3689 	am65_cpsw_unregister_notifiers(common);
3690 
3691 	/* must unregister ndevs here because DD release_driver routine calls
3692 	 * dma_deconfigure(dev) before devres_release_all(dev)
3693 	 */
3694 	am65_cpsw_nuss_cleanup_ndev(common);
3695 	am65_cpsw_unregister_devlink(common);
3696 	am65_cpsw_nuss_remove_rx_chns(common);
3697 	am65_cpsw_nuss_remove_tx_chns(common);
3698 	am65_cpsw_nuss_phylink_cleanup(common);
3699 	am65_cpts_release(common->cpts);
3700 	am65_cpsw_disable_serdes_phy(common);
3701 	am65_cpsw_remove_dt(common);
3702 
3703 	if (common->mdio_dev)
3704 		of_platform_device_destroy(common->mdio_dev, NULL);
3705 
3706 	pm_runtime_put_sync(&pdev->dev);
3707 	pm_runtime_disable(&pdev->dev);
3708 }
3709 
am65_cpsw_nuss_suspend(struct device * dev)3710 static int am65_cpsw_nuss_suspend(struct device *dev)
3711 {
3712 	struct am65_cpsw_common *common = dev_get_drvdata(dev);
3713 	struct am65_cpsw_host *host_p = am65_common_get_host(common);
3714 	struct am65_cpsw_port *port;
3715 	struct net_device *ndev;
3716 	int i, ret;
3717 
3718 	cpsw_ale_dump(common->ale, common->ale_context);
3719 	host_p->vid_context = readl(host_p->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
3720 	for (i = 0; i < common->port_num; i++) {
3721 		port = &common->ports[i];
3722 		ndev = port->ndev;
3723 
3724 		if (!ndev)
3725 			continue;
3726 
3727 		port->vid_context = readl(port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
3728 		netif_device_detach(ndev);
3729 		if (netif_running(ndev)) {
3730 			rtnl_lock();
3731 			ret = am65_cpsw_nuss_ndo_slave_stop(ndev);
3732 			rtnl_unlock();
3733 			if (ret < 0) {
3734 				netdev_err(ndev, "failed to stop: %d", ret);
3735 				return ret;
3736 			}
3737 		}
3738 	}
3739 
3740 	am65_cpts_suspend(common->cpts);
3741 
3742 	am65_cpsw_nuss_remove_rx_chns(common);
3743 	am65_cpsw_nuss_remove_tx_chns(common);
3744 
3745 	return 0;
3746 }
3747 
am65_cpsw_nuss_resume(struct device * dev)3748 static int am65_cpsw_nuss_resume(struct device *dev)
3749 {
3750 	struct am65_cpsw_common *common = dev_get_drvdata(dev);
3751 	struct am65_cpsw_host *host_p = am65_common_get_host(common);
3752 	struct am65_cpsw_port *port;
3753 	struct net_device *ndev;
3754 	int i, ret;
3755 
3756 	ret = am65_cpsw_nuss_init_tx_chns(common);
3757 	if (ret)
3758 		return ret;
3759 	ret = am65_cpsw_nuss_init_rx_chns(common);
3760 	if (ret) {
3761 		am65_cpsw_nuss_remove_tx_chns(common);
3762 		return ret;
3763 	}
3764 
3765 	/* If RX IRQ was disabled before suspend, keep it disabled */
3766 	for (i = 0; i < common->rx_ch_num_flows; i++) {
3767 		if (common->rx_chns.flows[i].irq_disabled)
3768 			disable_irq(common->rx_chns.flows[i].irq);
3769 	}
3770 
3771 	am65_cpts_resume(common->cpts);
3772 
3773 	for (i = 0; i < common->port_num; i++) {
3774 		port = &common->ports[i];
3775 		ndev = port->ndev;
3776 
3777 		if (!ndev)
3778 			continue;
3779 
3780 		if (netif_running(ndev)) {
3781 			rtnl_lock();
3782 			ret = am65_cpsw_nuss_ndo_slave_open(ndev);
3783 			rtnl_unlock();
3784 			if (ret < 0) {
3785 				netdev_err(ndev, "failed to start: %d", ret);
3786 				return ret;
3787 			}
3788 		}
3789 
3790 		netif_device_attach(ndev);
3791 		writel(port->vid_context, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
3792 	}
3793 
3794 	writel(host_p->vid_context, host_p->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET);
3795 	cpsw_ale_restore(common->ale, common->ale_context);
3796 
3797 	return 0;
3798 }
3799 
3800 static const struct dev_pm_ops am65_cpsw_nuss_dev_pm_ops = {
3801 	SYSTEM_SLEEP_PM_OPS(am65_cpsw_nuss_suspend, am65_cpsw_nuss_resume)
3802 };
3803 
3804 static struct platform_driver am65_cpsw_nuss_driver = {
3805 	.driver = {
3806 		.name	 = AM65_CPSW_DRV_NAME,
3807 		.of_match_table = am65_cpsw_nuss_of_mtable,
3808 		.pm = &am65_cpsw_nuss_dev_pm_ops,
3809 	},
3810 	.probe = am65_cpsw_nuss_probe,
3811 	.remove_new = am65_cpsw_nuss_remove,
3812 };
3813 
3814 module_platform_driver(am65_cpsw_nuss_driver);
3815 
3816 MODULE_LICENSE("GPL v2");
3817 MODULE_AUTHOR("Grygorii Strashko <grygorii.strashko@ti.com>");
3818 MODULE_DESCRIPTION("TI AM65 CPSW Ethernet driver");
3819