xref: /linux/drivers/net/ethernet/ti/cpsw_priv.c (revision b6459415b384cb829f0b2a4268f211c789f6cf0b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Texas Instruments Ethernet Switch Driver
4  *
5  * Copyright (C) 2019 Texas Instruments
6  */
7 
8 #include <linux/bpf.h>
9 #include <linux/bpf_trace.h>
10 #include <linux/if_ether.h>
11 #include <linux/if_vlan.h>
12 #include <linux/kmemleak.h>
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
15 #include <linux/net_tstamp.h>
16 #include <linux/of.h>
17 #include <linux/phy.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/skbuff.h>
21 #include <net/page_pool.h>
22 #include <net/pkt_cls.h>
23 
24 #include "cpsw.h"
25 #include "cpts.h"
26 #include "cpsw_ale.h"
27 #include "cpsw_priv.h"
28 #include "cpsw_sl.h"
29 #include "davinci_cpdma.h"
30 
31 #define CPTS_N_ETX_TS 4
32 
33 int (*cpsw_slave_index)(struct cpsw_common *cpsw, struct cpsw_priv *priv);
34 
35 void cpsw_intr_enable(struct cpsw_common *cpsw)
36 {
37 	writel_relaxed(0xFF, &cpsw->wr_regs->tx_en);
38 	writel_relaxed(0xFF, &cpsw->wr_regs->rx_en);
39 
40 	cpdma_ctlr_int_ctrl(cpsw->dma, true);
41 }
42 
43 void cpsw_intr_disable(struct cpsw_common *cpsw)
44 {
45 	writel_relaxed(0, &cpsw->wr_regs->tx_en);
46 	writel_relaxed(0, &cpsw->wr_regs->rx_en);
47 
48 	cpdma_ctlr_int_ctrl(cpsw->dma, false);
49 }
50 
51 void cpsw_tx_handler(void *token, int len, int status)
52 {
53 	struct cpsw_meta_xdp	*xmeta;
54 	struct xdp_frame	*xdpf;
55 	struct net_device	*ndev;
56 	struct netdev_queue	*txq;
57 	struct sk_buff		*skb;
58 	int			ch;
59 
60 	if (cpsw_is_xdpf_handle(token)) {
61 		xdpf = cpsw_handle_to_xdpf(token);
62 		xmeta = (void *)xdpf + CPSW_XMETA_OFFSET;
63 		ndev = xmeta->ndev;
64 		ch = xmeta->ch;
65 		xdp_return_frame(xdpf);
66 	} else {
67 		skb = token;
68 		ndev = skb->dev;
69 		ch = skb_get_queue_mapping(skb);
70 		cpts_tx_timestamp(ndev_to_cpsw(ndev)->cpts, skb);
71 		dev_kfree_skb_any(skb);
72 	}
73 
74 	/* Check whether the queue is stopped due to stalled tx dma, if the
75 	 * queue is stopped then start the queue as we have free desc for tx
76 	 */
77 	txq = netdev_get_tx_queue(ndev, ch);
78 	if (unlikely(netif_tx_queue_stopped(txq)))
79 		netif_tx_wake_queue(txq);
80 
81 	ndev->stats.tx_packets++;
82 	ndev->stats.tx_bytes += len;
83 }
84 
85 irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id)
86 {
87 	struct cpsw_common *cpsw = dev_id;
88 
89 	writel(0, &cpsw->wr_regs->tx_en);
90 	cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX);
91 
92 	if (cpsw->quirk_irq) {
93 		disable_irq_nosync(cpsw->irqs_table[1]);
94 		cpsw->tx_irq_disabled = true;
95 	}
96 
97 	napi_schedule(&cpsw->napi_tx);
98 	return IRQ_HANDLED;
99 }
100 
101 irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id)
102 {
103 	struct cpsw_common *cpsw = dev_id;
104 
105 	writel(0, &cpsw->wr_regs->rx_en);
106 	cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX);
107 
108 	if (cpsw->quirk_irq) {
109 		disable_irq_nosync(cpsw->irqs_table[0]);
110 		cpsw->rx_irq_disabled = true;
111 	}
112 
113 	napi_schedule(&cpsw->napi_rx);
114 	return IRQ_HANDLED;
115 }
116 
117 irqreturn_t cpsw_misc_interrupt(int irq, void *dev_id)
118 {
119 	struct cpsw_common *cpsw = dev_id;
120 
121 	writel(0, &cpsw->wr_regs->misc_en);
122 	cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_MISC);
123 	cpts_misc_interrupt(cpsw->cpts);
124 	writel(0x10, &cpsw->wr_regs->misc_en);
125 
126 	return IRQ_HANDLED;
127 }
128 
129 int cpsw_tx_mq_poll(struct napi_struct *napi_tx, int budget)
130 {
131 	struct cpsw_common	*cpsw = napi_to_cpsw(napi_tx);
132 	int			num_tx, cur_budget, ch;
133 	u32			ch_map;
134 	struct cpsw_vector	*txv;
135 
136 	/* process every unprocessed channel */
137 	ch_map = cpdma_ctrl_txchs_state(cpsw->dma);
138 	for (ch = 0, num_tx = 0; ch_map & 0xff; ch_map <<= 1, ch++) {
139 		if (!(ch_map & 0x80))
140 			continue;
141 
142 		txv = &cpsw->txv[ch];
143 		if (unlikely(txv->budget > budget - num_tx))
144 			cur_budget = budget - num_tx;
145 		else
146 			cur_budget = txv->budget;
147 
148 		num_tx += cpdma_chan_process(txv->ch, cur_budget);
149 		if (num_tx >= budget)
150 			break;
151 	}
152 
153 	if (num_tx < budget) {
154 		napi_complete(napi_tx);
155 		writel(0xff, &cpsw->wr_regs->tx_en);
156 	}
157 
158 	return num_tx;
159 }
160 
161 int cpsw_tx_poll(struct napi_struct *napi_tx, int budget)
162 {
163 	struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
164 	int num_tx;
165 
166 	num_tx = cpdma_chan_process(cpsw->txv[0].ch, budget);
167 	if (num_tx < budget) {
168 		napi_complete(napi_tx);
169 		writel(0xff, &cpsw->wr_regs->tx_en);
170 		if (cpsw->tx_irq_disabled) {
171 			cpsw->tx_irq_disabled = false;
172 			enable_irq(cpsw->irqs_table[1]);
173 		}
174 	}
175 
176 	return num_tx;
177 }
178 
179 int cpsw_rx_mq_poll(struct napi_struct *napi_rx, int budget)
180 {
181 	struct cpsw_common	*cpsw = napi_to_cpsw(napi_rx);
182 	int			num_rx, cur_budget, ch;
183 	u32			ch_map;
184 	struct cpsw_vector	*rxv;
185 
186 	/* process every unprocessed channel */
187 	ch_map = cpdma_ctrl_rxchs_state(cpsw->dma);
188 	for (ch = 0, num_rx = 0; ch_map; ch_map >>= 1, ch++) {
189 		if (!(ch_map & 0x01))
190 			continue;
191 
192 		rxv = &cpsw->rxv[ch];
193 		if (unlikely(rxv->budget > budget - num_rx))
194 			cur_budget = budget - num_rx;
195 		else
196 			cur_budget = rxv->budget;
197 
198 		num_rx += cpdma_chan_process(rxv->ch, cur_budget);
199 		if (num_rx >= budget)
200 			break;
201 	}
202 
203 	if (num_rx < budget) {
204 		napi_complete_done(napi_rx, num_rx);
205 		writel(0xff, &cpsw->wr_regs->rx_en);
206 	}
207 
208 	return num_rx;
209 }
210 
211 int cpsw_rx_poll(struct napi_struct *napi_rx, int budget)
212 {
213 	struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
214 	int num_rx;
215 
216 	num_rx = cpdma_chan_process(cpsw->rxv[0].ch, budget);
217 	if (num_rx < budget) {
218 		napi_complete_done(napi_rx, num_rx);
219 		writel(0xff, &cpsw->wr_regs->rx_en);
220 		if (cpsw->rx_irq_disabled) {
221 			cpsw->rx_irq_disabled = false;
222 			enable_irq(cpsw->irqs_table[0]);
223 		}
224 	}
225 
226 	return num_rx;
227 }
228 
229 void cpsw_rx_vlan_encap(struct sk_buff *skb)
230 {
231 	struct cpsw_priv *priv = netdev_priv(skb->dev);
232 	u32 rx_vlan_encap_hdr = *((u32 *)skb->data);
233 	struct cpsw_common *cpsw = priv->cpsw;
234 	u16 vtag, vid, prio, pkt_type;
235 
236 	/* Remove VLAN header encapsulation word */
237 	skb_pull(skb, CPSW_RX_VLAN_ENCAP_HDR_SIZE);
238 
239 	pkt_type = (rx_vlan_encap_hdr >>
240 		    CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT) &
241 		    CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK;
242 	/* Ignore unknown & Priority-tagged packets*/
243 	if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV ||
244 	    pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG)
245 		return;
246 
247 	vid = (rx_vlan_encap_hdr >>
248 	       CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT) &
249 	       VLAN_VID_MASK;
250 	/* Ignore vid 0 and pass packet as is */
251 	if (!vid)
252 		return;
253 
254 	/* Untag P0 packets if set for vlan */
255 	if (!cpsw_ale_get_vlan_p0_untag(cpsw->ale, vid)) {
256 		prio = (rx_vlan_encap_hdr >>
257 			CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT) &
258 			CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK;
259 
260 		vtag = (prio << VLAN_PRIO_SHIFT) | vid;
261 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
262 	}
263 
264 	/* strip vlan tag for VLAN-tagged packet */
265 	if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG) {
266 		memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
267 		skb_pull(skb, VLAN_HLEN);
268 	}
269 }
270 
271 void cpsw_set_slave_mac(struct cpsw_slave *slave, struct cpsw_priv *priv)
272 {
273 	slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
274 	slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
275 }
276 
277 void soft_reset(const char *module, void __iomem *reg)
278 {
279 	unsigned long timeout = jiffies + HZ;
280 
281 	writel_relaxed(1, reg);
282 	do {
283 		cpu_relax();
284 	} while ((readl_relaxed(reg) & 1) && time_after(timeout, jiffies));
285 
286 	WARN(readl_relaxed(reg) & 1, "failed to soft-reset %s\n", module);
287 }
288 
289 void cpsw_ndo_tx_timeout(struct net_device *ndev, unsigned int txqueue)
290 {
291 	struct cpsw_priv *priv = netdev_priv(ndev);
292 	struct cpsw_common *cpsw = priv->cpsw;
293 	int ch;
294 
295 	cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
296 	ndev->stats.tx_errors++;
297 	cpsw_intr_disable(cpsw);
298 	for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
299 		cpdma_chan_stop(cpsw->txv[ch].ch);
300 		cpdma_chan_start(cpsw->txv[ch].ch);
301 	}
302 
303 	cpsw_intr_enable(cpsw);
304 	netif_trans_update(ndev);
305 	netif_tx_wake_all_queues(ndev);
306 }
307 
308 static int cpsw_get_common_speed(struct cpsw_common *cpsw)
309 {
310 	int i, speed;
311 
312 	for (i = 0, speed = 0; i < cpsw->data.slaves; i++)
313 		if (cpsw->slaves[i].phy && cpsw->slaves[i].phy->link)
314 			speed += cpsw->slaves[i].phy->speed;
315 
316 	return speed;
317 }
318 
319 int cpsw_need_resplit(struct cpsw_common *cpsw)
320 {
321 	int i, rlim_ch_num;
322 	int speed, ch_rate;
323 
324 	/* re-split resources only in case speed was changed */
325 	speed = cpsw_get_common_speed(cpsw);
326 	if (speed == cpsw->speed || !speed)
327 		return 0;
328 
329 	cpsw->speed = speed;
330 
331 	for (i = 0, rlim_ch_num = 0; i < cpsw->tx_ch_num; i++) {
332 		ch_rate = cpdma_chan_get_rate(cpsw->txv[i].ch);
333 		if (!ch_rate)
334 			break;
335 
336 		rlim_ch_num++;
337 	}
338 
339 	/* cases not dependent on speed */
340 	if (!rlim_ch_num || rlim_ch_num == cpsw->tx_ch_num)
341 		return 0;
342 
343 	return 1;
344 }
345 
346 void cpsw_split_res(struct cpsw_common *cpsw)
347 {
348 	u32 consumed_rate = 0, bigest_rate = 0;
349 	struct cpsw_vector *txv = cpsw->txv;
350 	int i, ch_weight, rlim_ch_num = 0;
351 	int budget, bigest_rate_ch = 0;
352 	u32 ch_rate, max_rate;
353 	int ch_budget = 0;
354 
355 	for (i = 0; i < cpsw->tx_ch_num; i++) {
356 		ch_rate = cpdma_chan_get_rate(txv[i].ch);
357 		if (!ch_rate)
358 			continue;
359 
360 		rlim_ch_num++;
361 		consumed_rate += ch_rate;
362 	}
363 
364 	if (cpsw->tx_ch_num == rlim_ch_num) {
365 		max_rate = consumed_rate;
366 	} else if (!rlim_ch_num) {
367 		ch_budget = CPSW_POLL_WEIGHT / cpsw->tx_ch_num;
368 		bigest_rate = 0;
369 		max_rate = consumed_rate;
370 	} else {
371 		max_rate = cpsw->speed * 1000;
372 
373 		/* if max_rate is less then expected due to reduced link speed,
374 		 * split proportionally according next potential max speed
375 		 */
376 		if (max_rate < consumed_rate)
377 			max_rate *= 10;
378 
379 		if (max_rate < consumed_rate)
380 			max_rate *= 10;
381 
382 		ch_budget = (consumed_rate * CPSW_POLL_WEIGHT) / max_rate;
383 		ch_budget = (CPSW_POLL_WEIGHT - ch_budget) /
384 			    (cpsw->tx_ch_num - rlim_ch_num);
385 		bigest_rate = (max_rate - consumed_rate) /
386 			      (cpsw->tx_ch_num - rlim_ch_num);
387 	}
388 
389 	/* split tx weight/budget */
390 	budget = CPSW_POLL_WEIGHT;
391 	for (i = 0; i < cpsw->tx_ch_num; i++) {
392 		ch_rate = cpdma_chan_get_rate(txv[i].ch);
393 		if (ch_rate) {
394 			txv[i].budget = (ch_rate * CPSW_POLL_WEIGHT) / max_rate;
395 			if (!txv[i].budget)
396 				txv[i].budget++;
397 			if (ch_rate > bigest_rate) {
398 				bigest_rate_ch = i;
399 				bigest_rate = ch_rate;
400 			}
401 
402 			ch_weight = (ch_rate * 100) / max_rate;
403 			if (!ch_weight)
404 				ch_weight++;
405 			cpdma_chan_set_weight(cpsw->txv[i].ch, ch_weight);
406 		} else {
407 			txv[i].budget = ch_budget;
408 			if (!bigest_rate_ch)
409 				bigest_rate_ch = i;
410 			cpdma_chan_set_weight(cpsw->txv[i].ch, 0);
411 		}
412 
413 		budget -= txv[i].budget;
414 	}
415 
416 	if (budget)
417 		txv[bigest_rate_ch].budget += budget;
418 
419 	/* split rx budget */
420 	budget = CPSW_POLL_WEIGHT;
421 	ch_budget = budget / cpsw->rx_ch_num;
422 	for (i = 0; i < cpsw->rx_ch_num; i++) {
423 		cpsw->rxv[i].budget = ch_budget;
424 		budget -= ch_budget;
425 	}
426 
427 	if (budget)
428 		cpsw->rxv[0].budget += budget;
429 }
430 
431 int cpsw_init_common(struct cpsw_common *cpsw, void __iomem *ss_regs,
432 		     int ale_ageout, phys_addr_t desc_mem_phys,
433 		     int descs_pool_size)
434 {
435 	u32 slave_offset, sliver_offset, slave_size;
436 	struct cpsw_ale_params ale_params;
437 	struct cpsw_platform_data *data;
438 	struct cpdma_params dma_params;
439 	struct device *dev = cpsw->dev;
440 	struct device_node *cpts_node;
441 	void __iomem *cpts_regs;
442 	int ret = 0, i;
443 
444 	data = &cpsw->data;
445 	cpsw->rx_ch_num = 1;
446 	cpsw->tx_ch_num = 1;
447 
448 	cpsw->version = readl(&cpsw->regs->id_ver);
449 
450 	memset(&dma_params, 0, sizeof(dma_params));
451 	memset(&ale_params, 0, sizeof(ale_params));
452 
453 	switch (cpsw->version) {
454 	case CPSW_VERSION_1:
455 		cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET;
456 		cpts_regs	     = ss_regs + CPSW1_CPTS_OFFSET;
457 		cpsw->hw_stats	     = ss_regs + CPSW1_HW_STATS;
458 		dma_params.dmaregs   = ss_regs + CPSW1_CPDMA_OFFSET;
459 		dma_params.txhdp     = ss_regs + CPSW1_STATERAM_OFFSET;
460 		ale_params.ale_regs  = ss_regs + CPSW1_ALE_OFFSET;
461 		slave_offset         = CPSW1_SLAVE_OFFSET;
462 		slave_size           = CPSW1_SLAVE_SIZE;
463 		sliver_offset        = CPSW1_SLIVER_OFFSET;
464 		dma_params.desc_mem_phys = 0;
465 		break;
466 	case CPSW_VERSION_2:
467 	case CPSW_VERSION_3:
468 	case CPSW_VERSION_4:
469 		cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET;
470 		cpts_regs	     = ss_regs + CPSW2_CPTS_OFFSET;
471 		cpsw->hw_stats	     = ss_regs + CPSW2_HW_STATS;
472 		dma_params.dmaregs   = ss_regs + CPSW2_CPDMA_OFFSET;
473 		dma_params.txhdp     = ss_regs + CPSW2_STATERAM_OFFSET;
474 		ale_params.ale_regs  = ss_regs + CPSW2_ALE_OFFSET;
475 		slave_offset         = CPSW2_SLAVE_OFFSET;
476 		slave_size           = CPSW2_SLAVE_SIZE;
477 		sliver_offset        = CPSW2_SLIVER_OFFSET;
478 		dma_params.desc_mem_phys = desc_mem_phys;
479 		break;
480 	default:
481 		dev_err(dev, "unknown version 0x%08x\n", cpsw->version);
482 		return -ENODEV;
483 	}
484 
485 	for (i = 0; i < cpsw->data.slaves; i++) {
486 		struct cpsw_slave *slave = &cpsw->slaves[i];
487 		void __iomem		*regs = cpsw->regs;
488 
489 		slave->slave_num = i;
490 		slave->data	= &cpsw->data.slave_data[i];
491 		slave->regs	= regs + slave_offset;
492 		slave->port_vlan = slave->data->dual_emac_res_vlan;
493 		slave->mac_sl = cpsw_sl_get("cpsw", dev, regs + sliver_offset);
494 		if (IS_ERR(slave->mac_sl))
495 			return PTR_ERR(slave->mac_sl);
496 
497 		slave_offset  += slave_size;
498 		sliver_offset += SLIVER_SIZE;
499 	}
500 
501 	ale_params.dev			= dev;
502 	ale_params.ale_ageout		= ale_ageout;
503 	ale_params.ale_ports		= CPSW_ALE_PORTS_NUM;
504 	ale_params.dev_id		= "cpsw";
505 
506 	cpsw->ale = cpsw_ale_create(&ale_params);
507 	if (IS_ERR(cpsw->ale)) {
508 		dev_err(dev, "error initializing ale engine\n");
509 		return PTR_ERR(cpsw->ale);
510 	}
511 
512 	dma_params.dev		= dev;
513 	dma_params.rxthresh	= dma_params.dmaregs + CPDMA_RXTHRESH;
514 	dma_params.rxfree	= dma_params.dmaregs + CPDMA_RXFREE;
515 	dma_params.rxhdp	= dma_params.txhdp + CPDMA_RXHDP;
516 	dma_params.txcp		= dma_params.txhdp + CPDMA_TXCP;
517 	dma_params.rxcp		= dma_params.txhdp + CPDMA_RXCP;
518 
519 	dma_params.num_chan		= data->channels;
520 	dma_params.has_soft_reset	= true;
521 	dma_params.min_packet_size	= CPSW_MIN_PACKET_SIZE;
522 	dma_params.desc_mem_size	= data->bd_ram_size;
523 	dma_params.desc_align		= 16;
524 	dma_params.has_ext_regs		= true;
525 	dma_params.desc_hw_addr         = dma_params.desc_mem_phys;
526 	dma_params.bus_freq_mhz		= cpsw->bus_freq_mhz;
527 	dma_params.descs_pool_size	= descs_pool_size;
528 
529 	cpsw->dma = cpdma_ctlr_create(&dma_params);
530 	if (!cpsw->dma) {
531 		dev_err(dev, "error initializing dma\n");
532 		return -ENOMEM;
533 	}
534 
535 	cpts_node = of_get_child_by_name(cpsw->dev->of_node, "cpts");
536 	if (!cpts_node)
537 		cpts_node = cpsw->dev->of_node;
538 
539 	cpsw->cpts = cpts_create(cpsw->dev, cpts_regs, cpts_node,
540 				 CPTS_N_ETX_TS);
541 	if (IS_ERR(cpsw->cpts)) {
542 		ret = PTR_ERR(cpsw->cpts);
543 		cpdma_ctlr_destroy(cpsw->dma);
544 	}
545 	of_node_put(cpts_node);
546 
547 	return ret;
548 }
549 
550 #if IS_ENABLED(CONFIG_TI_CPTS)
551 
552 static void cpsw_hwtstamp_v1(struct cpsw_priv *priv)
553 {
554 	struct cpsw_common *cpsw = priv->cpsw;
555 	struct cpsw_slave *slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
556 	u32 ts_en, seq_id;
557 
558 	if (!priv->tx_ts_enabled && !priv->rx_ts_enabled) {
559 		slave_write(slave, 0, CPSW1_TS_CTL);
560 		return;
561 	}
562 
563 	seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
564 	ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
565 
566 	if (priv->tx_ts_enabled)
567 		ts_en |= CPSW_V1_TS_TX_EN;
568 
569 	if (priv->rx_ts_enabled)
570 		ts_en |= CPSW_V1_TS_RX_EN;
571 
572 	slave_write(slave, ts_en, CPSW1_TS_CTL);
573 	slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
574 }
575 
576 static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
577 {
578 	struct cpsw_common *cpsw = priv->cpsw;
579 	struct cpsw_slave *slave;
580 	u32 ctrl, mtype;
581 
582 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
583 
584 	ctrl = slave_read(slave, CPSW2_CONTROL);
585 	switch (cpsw->version) {
586 	case CPSW_VERSION_2:
587 		ctrl &= ~CTRL_V2_ALL_TS_MASK;
588 
589 		if (priv->tx_ts_enabled)
590 			ctrl |= CTRL_V2_TX_TS_BITS;
591 
592 		if (priv->rx_ts_enabled)
593 			ctrl |= CTRL_V2_RX_TS_BITS;
594 		break;
595 	case CPSW_VERSION_3:
596 	default:
597 		ctrl &= ~CTRL_V3_ALL_TS_MASK;
598 
599 		if (priv->tx_ts_enabled)
600 			ctrl |= CTRL_V3_TX_TS_BITS;
601 
602 		if (priv->rx_ts_enabled)
603 			ctrl |= CTRL_V3_RX_TS_BITS;
604 		break;
605 	}
606 
607 	mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
608 
609 	slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
610 	slave_write(slave, ctrl, CPSW2_CONTROL);
611 	writel_relaxed(ETH_P_1588, &cpsw->regs->ts_ltype);
612 	writel_relaxed(ETH_P_8021Q, &cpsw->regs->vlan_ltype);
613 }
614 
615 static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
616 {
617 	struct cpsw_priv *priv = netdev_priv(dev);
618 	struct cpsw_common *cpsw = priv->cpsw;
619 	struct hwtstamp_config cfg;
620 
621 	if (cpsw->version != CPSW_VERSION_1 &&
622 	    cpsw->version != CPSW_VERSION_2 &&
623 	    cpsw->version != CPSW_VERSION_3)
624 		return -EOPNOTSUPP;
625 
626 	if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
627 		return -EFAULT;
628 
629 	/* reserved for future extensions */
630 	if (cfg.flags)
631 		return -EINVAL;
632 
633 	if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
634 		return -ERANGE;
635 
636 	switch (cfg.rx_filter) {
637 	case HWTSTAMP_FILTER_NONE:
638 		priv->rx_ts_enabled = 0;
639 		break;
640 	case HWTSTAMP_FILTER_ALL:
641 	case HWTSTAMP_FILTER_NTP_ALL:
642 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
643 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
644 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
645 		return -ERANGE;
646 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
647 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
648 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
649 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
650 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
651 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
652 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
653 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
654 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
655 		priv->rx_ts_enabled = HWTSTAMP_FILTER_PTP_V2_EVENT;
656 		cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
657 		break;
658 	default:
659 		return -ERANGE;
660 	}
661 
662 	priv->tx_ts_enabled = cfg.tx_type == HWTSTAMP_TX_ON;
663 
664 	switch (cpsw->version) {
665 	case CPSW_VERSION_1:
666 		cpsw_hwtstamp_v1(priv);
667 		break;
668 	case CPSW_VERSION_2:
669 	case CPSW_VERSION_3:
670 		cpsw_hwtstamp_v2(priv);
671 		break;
672 	default:
673 		WARN_ON(1);
674 	}
675 
676 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
677 }
678 
679 static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
680 {
681 	struct cpsw_common *cpsw = ndev_to_cpsw(dev);
682 	struct cpsw_priv *priv = netdev_priv(dev);
683 	struct hwtstamp_config cfg;
684 
685 	if (cpsw->version != CPSW_VERSION_1 &&
686 	    cpsw->version != CPSW_VERSION_2 &&
687 	    cpsw->version != CPSW_VERSION_3)
688 		return -EOPNOTSUPP;
689 
690 	cfg.flags = 0;
691 	cfg.tx_type = priv->tx_ts_enabled ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
692 	cfg.rx_filter = priv->rx_ts_enabled;
693 
694 	return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
695 }
696 #else
697 static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
698 {
699 	return -EOPNOTSUPP;
700 }
701 
702 static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
703 {
704 	return -EOPNOTSUPP;
705 }
706 #endif /*CONFIG_TI_CPTS*/
707 
708 int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
709 {
710 	struct cpsw_priv *priv = netdev_priv(dev);
711 	struct cpsw_common *cpsw = priv->cpsw;
712 	int slave_no = cpsw_slave_index(cpsw, priv);
713 	struct phy_device *phy;
714 
715 	if (!netif_running(dev))
716 		return -EINVAL;
717 
718 	phy = cpsw->slaves[slave_no].phy;
719 
720 	if (!phy_has_hwtstamp(phy)) {
721 		switch (cmd) {
722 		case SIOCSHWTSTAMP:
723 			return cpsw_hwtstamp_set(dev, req);
724 		case SIOCGHWTSTAMP:
725 			return cpsw_hwtstamp_get(dev, req);
726 		}
727 	}
728 
729 	if (phy)
730 		return phy_mii_ioctl(phy, req, cmd);
731 
732 	return -EOPNOTSUPP;
733 }
734 
735 int cpsw_ndo_set_tx_maxrate(struct net_device *ndev, int queue, u32 rate)
736 {
737 	struct cpsw_priv *priv = netdev_priv(ndev);
738 	struct cpsw_common *cpsw = priv->cpsw;
739 	struct cpsw_slave *slave;
740 	u32 min_rate;
741 	u32 ch_rate;
742 	int i, ret;
743 
744 	ch_rate = netdev_get_tx_queue(ndev, queue)->tx_maxrate;
745 	if (ch_rate == rate)
746 		return 0;
747 
748 	ch_rate = rate * 1000;
749 	min_rate = cpdma_chan_get_min_rate(cpsw->dma);
750 	if ((ch_rate < min_rate && ch_rate)) {
751 		dev_err(priv->dev, "The channel rate cannot be less than %dMbps",
752 			min_rate);
753 		return -EINVAL;
754 	}
755 
756 	if (rate > cpsw->speed) {
757 		dev_err(priv->dev, "The channel rate cannot be more than 2Gbps");
758 		return -EINVAL;
759 	}
760 
761 	ret = pm_runtime_get_sync(cpsw->dev);
762 	if (ret < 0) {
763 		pm_runtime_put_noidle(cpsw->dev);
764 		return ret;
765 	}
766 
767 	ret = cpdma_chan_set_rate(cpsw->txv[queue].ch, ch_rate);
768 	pm_runtime_put(cpsw->dev);
769 
770 	if (ret)
771 		return ret;
772 
773 	/* update rates for slaves tx queues */
774 	for (i = 0; i < cpsw->data.slaves; i++) {
775 		slave = &cpsw->slaves[i];
776 		if (!slave->ndev)
777 			continue;
778 
779 		netdev_get_tx_queue(slave->ndev, queue)->tx_maxrate = rate;
780 	}
781 
782 	cpsw_split_res(cpsw);
783 	return ret;
784 }
785 
786 static int cpsw_tc_to_fifo(int tc, int num_tc)
787 {
788 	if (tc == num_tc - 1)
789 		return 0;
790 
791 	return CPSW_FIFO_SHAPERS_NUM - tc;
792 }
793 
794 bool cpsw_shp_is_off(struct cpsw_priv *priv)
795 {
796 	struct cpsw_common *cpsw = priv->cpsw;
797 	struct cpsw_slave *slave;
798 	u32 shift, mask, val;
799 
800 	val = readl_relaxed(&cpsw->regs->ptype);
801 
802 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
803 	shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num;
804 	mask = 7 << shift;
805 	val = val & mask;
806 
807 	return !val;
808 }
809 
810 static void cpsw_fifo_shp_on(struct cpsw_priv *priv, int fifo, int on)
811 {
812 	struct cpsw_common *cpsw = priv->cpsw;
813 	struct cpsw_slave *slave;
814 	u32 shift, mask, val;
815 
816 	val = readl_relaxed(&cpsw->regs->ptype);
817 
818 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
819 	shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num;
820 	mask = (1 << --fifo) << shift;
821 	val = on ? val | mask : val & ~mask;
822 
823 	writel_relaxed(val, &cpsw->regs->ptype);
824 }
825 
826 static int cpsw_set_fifo_bw(struct cpsw_priv *priv, int fifo, int bw)
827 {
828 	struct cpsw_common *cpsw = priv->cpsw;
829 	u32 val = 0, send_pct, shift;
830 	struct cpsw_slave *slave;
831 	int pct = 0, i;
832 
833 	if (bw > priv->shp_cfg_speed * 1000)
834 		goto err;
835 
836 	/* shaping has to stay enabled for highest fifos linearly
837 	 * and fifo bw no more then interface can allow
838 	 */
839 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
840 	send_pct = slave_read(slave, SEND_PERCENT);
841 	for (i = CPSW_FIFO_SHAPERS_NUM; i > 0; i--) {
842 		if (!bw) {
843 			if (i >= fifo || !priv->fifo_bw[i])
844 				continue;
845 
846 			dev_warn(priv->dev, "Prev FIFO%d is shaped", i);
847 			continue;
848 		}
849 
850 		if (!priv->fifo_bw[i] && i > fifo) {
851 			dev_err(priv->dev, "Upper FIFO%d is not shaped", i);
852 			return -EINVAL;
853 		}
854 
855 		shift = (i - 1) * 8;
856 		if (i == fifo) {
857 			send_pct &= ~(CPSW_PCT_MASK << shift);
858 			val = DIV_ROUND_UP(bw, priv->shp_cfg_speed * 10);
859 			if (!val)
860 				val = 1;
861 
862 			send_pct |= val << shift;
863 			pct += val;
864 			continue;
865 		}
866 
867 		if (priv->fifo_bw[i])
868 			pct += (send_pct >> shift) & CPSW_PCT_MASK;
869 	}
870 
871 	if (pct >= 100)
872 		goto err;
873 
874 	slave_write(slave, send_pct, SEND_PERCENT);
875 	priv->fifo_bw[fifo] = bw;
876 
877 	dev_warn(priv->dev, "set FIFO%d bw = %d\n", fifo,
878 		 DIV_ROUND_CLOSEST(val * priv->shp_cfg_speed, 100));
879 
880 	return 0;
881 err:
882 	dev_err(priv->dev, "Bandwidth doesn't fit in tc configuration");
883 	return -EINVAL;
884 }
885 
886 static int cpsw_set_fifo_rlimit(struct cpsw_priv *priv, int fifo, int bw)
887 {
888 	struct cpsw_common *cpsw = priv->cpsw;
889 	struct cpsw_slave *slave;
890 	u32 tx_in_ctl_rg, val;
891 	int ret;
892 
893 	ret = cpsw_set_fifo_bw(priv, fifo, bw);
894 	if (ret)
895 		return ret;
896 
897 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
898 	tx_in_ctl_rg = cpsw->version == CPSW_VERSION_1 ?
899 		       CPSW1_TX_IN_CTL : CPSW2_TX_IN_CTL;
900 
901 	if (!bw)
902 		cpsw_fifo_shp_on(priv, fifo, bw);
903 
904 	val = slave_read(slave, tx_in_ctl_rg);
905 	if (cpsw_shp_is_off(priv)) {
906 		/* disable FIFOs rate limited queues */
907 		val &= ~(0xf << CPSW_FIFO_RATE_EN_SHIFT);
908 
909 		/* set type of FIFO queues to normal priority mode */
910 		val &= ~(3 << CPSW_FIFO_QUEUE_TYPE_SHIFT);
911 
912 		/* set type of FIFO queues to be rate limited */
913 		if (bw)
914 			val |= 2 << CPSW_FIFO_QUEUE_TYPE_SHIFT;
915 		else
916 			priv->shp_cfg_speed = 0;
917 	}
918 
919 	/* toggle a FIFO rate limited queue */
920 	if (bw)
921 		val |= BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT);
922 	else
923 		val &= ~BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT);
924 	slave_write(slave, val, tx_in_ctl_rg);
925 
926 	/* FIFO transmit shape enable */
927 	cpsw_fifo_shp_on(priv, fifo, bw);
928 	return 0;
929 }
930 
931 /* Defaults:
932  * class A - prio 3
933  * class B - prio 2
934  * shaping for class A should be set first
935  */
936 static int cpsw_set_cbs(struct net_device *ndev,
937 			struct tc_cbs_qopt_offload *qopt)
938 {
939 	struct cpsw_priv *priv = netdev_priv(ndev);
940 	struct cpsw_common *cpsw = priv->cpsw;
941 	struct cpsw_slave *slave;
942 	int prev_speed = 0;
943 	int tc, ret, fifo;
944 	u32 bw = 0;
945 
946 	tc = netdev_txq_to_tc(priv->ndev, qopt->queue);
947 
948 	/* enable channels in backward order, as highest FIFOs must be rate
949 	 * limited first and for compliance with CPDMA rate limited channels
950 	 * that also used in bacward order. FIFO0 cannot be rate limited.
951 	 */
952 	fifo = cpsw_tc_to_fifo(tc, ndev->num_tc);
953 	if (!fifo) {
954 		dev_err(priv->dev, "Last tc%d can't be rate limited", tc);
955 		return -EINVAL;
956 	}
957 
958 	/* do nothing, it's disabled anyway */
959 	if (!qopt->enable && !priv->fifo_bw[fifo])
960 		return 0;
961 
962 	/* shapers can be set if link speed is known */
963 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
964 	if (slave->phy && slave->phy->link) {
965 		if (priv->shp_cfg_speed &&
966 		    priv->shp_cfg_speed != slave->phy->speed)
967 			prev_speed = priv->shp_cfg_speed;
968 
969 		priv->shp_cfg_speed = slave->phy->speed;
970 	}
971 
972 	if (!priv->shp_cfg_speed) {
973 		dev_err(priv->dev, "Link speed is not known");
974 		return -1;
975 	}
976 
977 	ret = pm_runtime_get_sync(cpsw->dev);
978 	if (ret < 0) {
979 		pm_runtime_put_noidle(cpsw->dev);
980 		return ret;
981 	}
982 
983 	bw = qopt->enable ? qopt->idleslope : 0;
984 	ret = cpsw_set_fifo_rlimit(priv, fifo, bw);
985 	if (ret) {
986 		priv->shp_cfg_speed = prev_speed;
987 		prev_speed = 0;
988 	}
989 
990 	if (bw && prev_speed)
991 		dev_warn(priv->dev,
992 			 "Speed was changed, CBS shaper speeds are changed!");
993 
994 	pm_runtime_put_sync(cpsw->dev);
995 	return ret;
996 }
997 
998 static int cpsw_set_mqprio(struct net_device *ndev, void *type_data)
999 {
1000 	struct tc_mqprio_qopt_offload *mqprio = type_data;
1001 	struct cpsw_priv *priv = netdev_priv(ndev);
1002 	struct cpsw_common *cpsw = priv->cpsw;
1003 	int fifo, num_tc, count, offset;
1004 	struct cpsw_slave *slave;
1005 	u32 tx_prio_map = 0;
1006 	int i, tc, ret;
1007 
1008 	num_tc = mqprio->qopt.num_tc;
1009 	if (num_tc > CPSW_TC_NUM)
1010 		return -EINVAL;
1011 
1012 	if (mqprio->mode != TC_MQPRIO_MODE_DCB)
1013 		return -EINVAL;
1014 
1015 	ret = pm_runtime_get_sync(cpsw->dev);
1016 	if (ret < 0) {
1017 		pm_runtime_put_noidle(cpsw->dev);
1018 		return ret;
1019 	}
1020 
1021 	if (num_tc) {
1022 		for (i = 0; i < 8; i++) {
1023 			tc = mqprio->qopt.prio_tc_map[i];
1024 			fifo = cpsw_tc_to_fifo(tc, num_tc);
1025 			tx_prio_map |= fifo << (4 * i);
1026 		}
1027 
1028 		netdev_set_num_tc(ndev, num_tc);
1029 		for (i = 0; i < num_tc; i++) {
1030 			count = mqprio->qopt.count[i];
1031 			offset = mqprio->qopt.offset[i];
1032 			netdev_set_tc_queue(ndev, i, count, offset);
1033 		}
1034 	}
1035 
1036 	if (!mqprio->qopt.hw) {
1037 		/* restore default configuration */
1038 		netdev_reset_tc(ndev);
1039 		tx_prio_map = TX_PRIORITY_MAPPING;
1040 	}
1041 
1042 	priv->mqprio_hw = mqprio->qopt.hw;
1043 
1044 	offset = cpsw->version == CPSW_VERSION_1 ?
1045 		 CPSW1_TX_PRI_MAP : CPSW2_TX_PRI_MAP;
1046 
1047 	slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
1048 	slave_write(slave, tx_prio_map, offset);
1049 
1050 	pm_runtime_put_sync(cpsw->dev);
1051 
1052 	return 0;
1053 }
1054 
1055 int cpsw_ndo_setup_tc(struct net_device *ndev, enum tc_setup_type type,
1056 		      void *type_data)
1057 {
1058 	switch (type) {
1059 	case TC_SETUP_QDISC_CBS:
1060 		return cpsw_set_cbs(ndev, type_data);
1061 
1062 	case TC_SETUP_QDISC_MQPRIO:
1063 		return cpsw_set_mqprio(ndev, type_data);
1064 
1065 	default:
1066 		return -EOPNOTSUPP;
1067 	}
1068 }
1069 
1070 void cpsw_cbs_resume(struct cpsw_slave *slave, struct cpsw_priv *priv)
1071 {
1072 	int fifo, bw;
1073 
1074 	for (fifo = CPSW_FIFO_SHAPERS_NUM; fifo > 0; fifo--) {
1075 		bw = priv->fifo_bw[fifo];
1076 		if (!bw)
1077 			continue;
1078 
1079 		cpsw_set_fifo_rlimit(priv, fifo, bw);
1080 	}
1081 }
1082 
1083 void cpsw_mqprio_resume(struct cpsw_slave *slave, struct cpsw_priv *priv)
1084 {
1085 	struct cpsw_common *cpsw = priv->cpsw;
1086 	u32 tx_prio_map = 0;
1087 	int i, tc, fifo;
1088 	u32 tx_prio_rg;
1089 
1090 	if (!priv->mqprio_hw)
1091 		return;
1092 
1093 	for (i = 0; i < 8; i++) {
1094 		tc = netdev_get_prio_tc_map(priv->ndev, i);
1095 		fifo = CPSW_FIFO_SHAPERS_NUM - tc;
1096 		tx_prio_map |= fifo << (4 * i);
1097 	}
1098 
1099 	tx_prio_rg = cpsw->version == CPSW_VERSION_1 ?
1100 		     CPSW1_TX_PRI_MAP : CPSW2_TX_PRI_MAP;
1101 
1102 	slave_write(slave, tx_prio_map, tx_prio_rg);
1103 }
1104 
1105 int cpsw_fill_rx_channels(struct cpsw_priv *priv)
1106 {
1107 	struct cpsw_common *cpsw = priv->cpsw;
1108 	struct cpsw_meta_xdp *xmeta;
1109 	struct page_pool *pool;
1110 	struct page *page;
1111 	int ch_buf_num;
1112 	int ch, i, ret;
1113 	dma_addr_t dma;
1114 
1115 	for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
1116 		pool = cpsw->page_pool[ch];
1117 		ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch);
1118 		for (i = 0; i < ch_buf_num; i++) {
1119 			page = page_pool_dev_alloc_pages(pool);
1120 			if (!page) {
1121 				cpsw_err(priv, ifup, "allocate rx page err\n");
1122 				return -ENOMEM;
1123 			}
1124 
1125 			xmeta = page_address(page) + CPSW_XMETA_OFFSET;
1126 			xmeta->ndev = priv->ndev;
1127 			xmeta->ch = ch;
1128 
1129 			dma = page_pool_get_dma_addr(page) + CPSW_HEADROOM;
1130 			ret = cpdma_chan_idle_submit_mapped(cpsw->rxv[ch].ch,
1131 							    page, dma,
1132 							    cpsw->rx_packet_max,
1133 							    0);
1134 			if (ret < 0) {
1135 				cpsw_err(priv, ifup,
1136 					 "cannot submit page to channel %d rx, error %d\n",
1137 					 ch, ret);
1138 				page_pool_recycle_direct(pool, page);
1139 				return ret;
1140 			}
1141 		}
1142 
1143 		cpsw_info(priv, ifup, "ch %d rx, submitted %d descriptors\n",
1144 			  ch, ch_buf_num);
1145 	}
1146 
1147 	return 0;
1148 }
1149 
1150 static struct page_pool *cpsw_create_page_pool(struct cpsw_common *cpsw,
1151 					       int size)
1152 {
1153 	struct page_pool_params pp_params;
1154 	struct page_pool *pool;
1155 
1156 	pp_params.order = 0;
1157 	pp_params.flags = PP_FLAG_DMA_MAP;
1158 	pp_params.pool_size = size;
1159 	pp_params.nid = NUMA_NO_NODE;
1160 	pp_params.dma_dir = DMA_BIDIRECTIONAL;
1161 	pp_params.dev = cpsw->dev;
1162 
1163 	pool = page_pool_create(&pp_params);
1164 	if (IS_ERR(pool))
1165 		dev_err(cpsw->dev, "cannot create rx page pool\n");
1166 
1167 	return pool;
1168 }
1169 
1170 static int cpsw_create_rx_pool(struct cpsw_common *cpsw, int ch)
1171 {
1172 	struct page_pool *pool;
1173 	int ret = 0, pool_size;
1174 
1175 	pool_size = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch);
1176 	pool = cpsw_create_page_pool(cpsw, pool_size);
1177 	if (IS_ERR(pool))
1178 		ret = PTR_ERR(pool);
1179 	else
1180 		cpsw->page_pool[ch] = pool;
1181 
1182 	return ret;
1183 }
1184 
1185 static int cpsw_ndev_create_xdp_rxq(struct cpsw_priv *priv, int ch)
1186 {
1187 	struct cpsw_common *cpsw = priv->cpsw;
1188 	struct xdp_rxq_info *rxq;
1189 	struct page_pool *pool;
1190 	int ret;
1191 
1192 	pool = cpsw->page_pool[ch];
1193 	rxq = &priv->xdp_rxq[ch];
1194 
1195 	ret = xdp_rxq_info_reg(rxq, priv->ndev, ch, 0);
1196 	if (ret)
1197 		return ret;
1198 
1199 	ret = xdp_rxq_info_reg_mem_model(rxq, MEM_TYPE_PAGE_POOL, pool);
1200 	if (ret)
1201 		xdp_rxq_info_unreg(rxq);
1202 
1203 	return ret;
1204 }
1205 
1206 static void cpsw_ndev_destroy_xdp_rxq(struct cpsw_priv *priv, int ch)
1207 {
1208 	struct xdp_rxq_info *rxq = &priv->xdp_rxq[ch];
1209 
1210 	if (!xdp_rxq_info_is_reg(rxq))
1211 		return;
1212 
1213 	xdp_rxq_info_unreg(rxq);
1214 }
1215 
1216 void cpsw_destroy_xdp_rxqs(struct cpsw_common *cpsw)
1217 {
1218 	struct net_device *ndev;
1219 	int i, ch;
1220 
1221 	for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
1222 		for (i = 0; i < cpsw->data.slaves; i++) {
1223 			ndev = cpsw->slaves[i].ndev;
1224 			if (!ndev)
1225 				continue;
1226 
1227 			cpsw_ndev_destroy_xdp_rxq(netdev_priv(ndev), ch);
1228 		}
1229 
1230 		page_pool_destroy(cpsw->page_pool[ch]);
1231 		cpsw->page_pool[ch] = NULL;
1232 	}
1233 }
1234 
1235 int cpsw_create_xdp_rxqs(struct cpsw_common *cpsw)
1236 {
1237 	struct net_device *ndev;
1238 	int i, ch, ret;
1239 
1240 	for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
1241 		ret = cpsw_create_rx_pool(cpsw, ch);
1242 		if (ret)
1243 			goto err_cleanup;
1244 
1245 		/* using same page pool is allowed as no running rx handlers
1246 		 * simultaneously for both ndevs
1247 		 */
1248 		for (i = 0; i < cpsw->data.slaves; i++) {
1249 			ndev = cpsw->slaves[i].ndev;
1250 			if (!ndev)
1251 				continue;
1252 
1253 			ret = cpsw_ndev_create_xdp_rxq(netdev_priv(ndev), ch);
1254 			if (ret)
1255 				goto err_cleanup;
1256 		}
1257 	}
1258 
1259 	return 0;
1260 
1261 err_cleanup:
1262 	cpsw_destroy_xdp_rxqs(cpsw);
1263 
1264 	return ret;
1265 }
1266 
1267 static int cpsw_xdp_prog_setup(struct cpsw_priv *priv, struct netdev_bpf *bpf)
1268 {
1269 	struct bpf_prog *prog = bpf->prog;
1270 
1271 	if (!priv->xdpi.prog && !prog)
1272 		return 0;
1273 
1274 	WRITE_ONCE(priv->xdp_prog, prog);
1275 
1276 	xdp_attachment_setup(&priv->xdpi, bpf);
1277 
1278 	return 0;
1279 }
1280 
1281 int cpsw_ndo_bpf(struct net_device *ndev, struct netdev_bpf *bpf)
1282 {
1283 	struct cpsw_priv *priv = netdev_priv(ndev);
1284 
1285 	switch (bpf->command) {
1286 	case XDP_SETUP_PROG:
1287 		return cpsw_xdp_prog_setup(priv, bpf);
1288 
1289 	default:
1290 		return -EINVAL;
1291 	}
1292 }
1293 
1294 int cpsw_xdp_tx_frame(struct cpsw_priv *priv, struct xdp_frame *xdpf,
1295 		      struct page *page, int port)
1296 {
1297 	struct cpsw_common *cpsw = priv->cpsw;
1298 	struct cpsw_meta_xdp *xmeta;
1299 	struct cpdma_chan *txch;
1300 	dma_addr_t dma;
1301 	int ret;
1302 
1303 	xmeta = (void *)xdpf + CPSW_XMETA_OFFSET;
1304 	xmeta->ndev = priv->ndev;
1305 	xmeta->ch = 0;
1306 	txch = cpsw->txv[0].ch;
1307 
1308 	if (page) {
1309 		dma = page_pool_get_dma_addr(page);
1310 		dma += xdpf->headroom + sizeof(struct xdp_frame);
1311 		ret = cpdma_chan_submit_mapped(txch, cpsw_xdpf_to_handle(xdpf),
1312 					       dma, xdpf->len, port);
1313 	} else {
1314 		if (sizeof(*xmeta) > xdpf->headroom)
1315 			return -EINVAL;
1316 
1317 		ret = cpdma_chan_submit(txch, cpsw_xdpf_to_handle(xdpf),
1318 					xdpf->data, xdpf->len, port);
1319 	}
1320 
1321 	if (ret)
1322 		priv->ndev->stats.tx_dropped++;
1323 
1324 	return ret;
1325 }
1326 
1327 int cpsw_run_xdp(struct cpsw_priv *priv, int ch, struct xdp_buff *xdp,
1328 		 struct page *page, int port, int *len)
1329 {
1330 	struct cpsw_common *cpsw = priv->cpsw;
1331 	struct net_device *ndev = priv->ndev;
1332 	int ret = CPSW_XDP_CONSUMED;
1333 	struct xdp_frame *xdpf;
1334 	struct bpf_prog *prog;
1335 	u32 act;
1336 
1337 	prog = READ_ONCE(priv->xdp_prog);
1338 	if (!prog)
1339 		return CPSW_XDP_PASS;
1340 
1341 	act = bpf_prog_run_xdp(prog, xdp);
1342 	/* XDP prog might have changed packet data and boundaries */
1343 	*len = xdp->data_end - xdp->data;
1344 
1345 	switch (act) {
1346 	case XDP_PASS:
1347 		ret = CPSW_XDP_PASS;
1348 		goto out;
1349 	case XDP_TX:
1350 		xdpf = xdp_convert_buff_to_frame(xdp);
1351 		if (unlikely(!xdpf))
1352 			goto drop;
1353 
1354 		if (cpsw_xdp_tx_frame(priv, xdpf, page, port))
1355 			xdp_return_frame_rx_napi(xdpf);
1356 		break;
1357 	case XDP_REDIRECT:
1358 		if (xdp_do_redirect(ndev, xdp, prog))
1359 			goto drop;
1360 
1361 		/*  Have to flush here, per packet, instead of doing it in bulk
1362 		 *  at the end of the napi handler. The RX devices on this
1363 		 *  particular hardware is sharing a common queue, so the
1364 		 *  incoming device might change per packet.
1365 		 */
1366 		xdp_do_flush_map();
1367 		break;
1368 	default:
1369 		bpf_warn_invalid_xdp_action(ndev, prog, act);
1370 		fallthrough;
1371 	case XDP_ABORTED:
1372 		trace_xdp_exception(ndev, prog, act);
1373 		fallthrough;	/* handle aborts by dropping packet */
1374 	case XDP_DROP:
1375 		ndev->stats.rx_bytes += *len;
1376 		ndev->stats.rx_packets++;
1377 		goto drop;
1378 	}
1379 
1380 	ndev->stats.rx_bytes += *len;
1381 	ndev->stats.rx_packets++;
1382 out:
1383 	return ret;
1384 drop:
1385 	page_pool_recycle_direct(cpsw->page_pool[ch], page);
1386 	return ret;
1387 }
1388