1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /* Copyright 2014-2016 Freescale Semiconductor Inc.
3  * Copyright 2016-2022 NXP
4  */
5 #include <linux/init.h>
6 #include <linux/module.h>
7 #include <linux/platform_device.h>
8 #include <linux/etherdevice.h>
9 #include <linux/of_net.h>
10 #include <linux/interrupt.h>
11 #include <linux/msi.h>
12 #include <linux/kthread.h>
13 #include <linux/iommu.h>
14 #include <linux/fsl/mc.h>
15 #include <linux/bpf.h>
16 #include <linux/bpf_trace.h>
17 #include <linux/fsl/ptp_qoriq.h>
18 #include <linux/ptp_classify.h>
19 #include <net/pkt_cls.h>
20 #include <net/sock.h>
21 #include <net/tso.h>
22 #include <net/xdp_sock_drv.h>
23 
24 #include "dpaa2-eth.h"
25 
26 /* CREATE_TRACE_POINTS only needs to be defined once. Other dpa files
27  * using trace events only need to #include <trace/events/sched.h>
28  */
29 #define CREATE_TRACE_POINTS
30 #include "dpaa2-eth-trace.h"
31 
32 MODULE_LICENSE("Dual BSD/GPL");
33 MODULE_AUTHOR("Freescale Semiconductor, Inc");
34 MODULE_DESCRIPTION("Freescale DPAA2 Ethernet Driver");
35 
36 struct ptp_qoriq *dpaa2_ptp;
37 EXPORT_SYMBOL(dpaa2_ptp);
38 
39 static void dpaa2_eth_detect_features(struct dpaa2_eth_priv *priv)
40 {
41 	priv->features = 0;
42 
43 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_PTP_ONESTEP_VER_MAJOR,
44 				   DPNI_PTP_ONESTEP_VER_MINOR) >= 0)
45 		priv->features |= DPAA2_ETH_FEATURE_ONESTEP_CFG_DIRECT;
46 }
47 
48 static void dpaa2_update_ptp_onestep_indirect(struct dpaa2_eth_priv *priv,
49 					      u32 offset, u8 udp)
50 {
51 	struct dpni_single_step_cfg cfg;
52 
53 	cfg.en = 1;
54 	cfg.ch_update = udp;
55 	cfg.offset = offset;
56 	cfg.peer_delay = 0;
57 
58 	if (dpni_set_single_step_cfg(priv->mc_io, 0, priv->mc_token, &cfg))
59 		WARN_ONCE(1, "Failed to set single step register");
60 }
61 
62 static void dpaa2_update_ptp_onestep_direct(struct dpaa2_eth_priv *priv,
63 					    u32 offset, u8 udp)
64 {
65 	u32 val = 0;
66 
67 	val = DPAA2_PTP_SINGLE_STEP_ENABLE |
68 	       DPAA2_PTP_SINGLE_CORRECTION_OFF(offset);
69 
70 	if (udp)
71 		val |= DPAA2_PTP_SINGLE_STEP_CH;
72 
73 	if (priv->onestep_reg_base)
74 		writel(val, priv->onestep_reg_base);
75 }
76 
77 static void dpaa2_ptp_onestep_reg_update_method(struct dpaa2_eth_priv *priv)
78 {
79 	struct device *dev = priv->net_dev->dev.parent;
80 	struct dpni_single_step_cfg ptp_cfg;
81 
82 	priv->dpaa2_set_onestep_params_cb = dpaa2_update_ptp_onestep_indirect;
83 
84 	if (!(priv->features & DPAA2_ETH_FEATURE_ONESTEP_CFG_DIRECT))
85 		return;
86 
87 	if (dpni_get_single_step_cfg(priv->mc_io, 0,
88 				     priv->mc_token, &ptp_cfg)) {
89 		dev_err(dev, "dpni_get_single_step_cfg cannot retrieve onestep reg, falling back to indirect update\n");
90 		return;
91 	}
92 
93 	if (!ptp_cfg.ptp_onestep_reg_base) {
94 		dev_err(dev, "1588 onestep reg not available, falling back to indirect update\n");
95 		return;
96 	}
97 
98 	priv->onestep_reg_base = ioremap(ptp_cfg.ptp_onestep_reg_base,
99 					 sizeof(u32));
100 	if (!priv->onestep_reg_base) {
101 		dev_err(dev, "1588 onestep reg cannot be mapped, falling back to indirect update\n");
102 		return;
103 	}
104 
105 	priv->dpaa2_set_onestep_params_cb = dpaa2_update_ptp_onestep_direct;
106 }
107 
108 void *dpaa2_iova_to_virt(struct iommu_domain *domain,
109 			 dma_addr_t iova_addr)
110 {
111 	phys_addr_t phys_addr;
112 
113 	phys_addr = domain ? iommu_iova_to_phys(domain, iova_addr) : iova_addr;
114 
115 	return phys_to_virt(phys_addr);
116 }
117 
118 static void dpaa2_eth_validate_rx_csum(struct dpaa2_eth_priv *priv,
119 				       u32 fd_status,
120 				       struct sk_buff *skb)
121 {
122 	skb_checksum_none_assert(skb);
123 
124 	/* HW checksum validation is disabled, nothing to do here */
125 	if (!(priv->net_dev->features & NETIF_F_RXCSUM))
126 		return;
127 
128 	/* Read checksum validation bits */
129 	if (!((fd_status & DPAA2_FAS_L3CV) &&
130 	      (fd_status & DPAA2_FAS_L4CV)))
131 		return;
132 
133 	/* Inform the stack there's no need to compute L3/L4 csum anymore */
134 	skb->ip_summed = CHECKSUM_UNNECESSARY;
135 }
136 
137 /* Free a received FD.
138  * Not to be used for Tx conf FDs or on any other paths.
139  */
140 static void dpaa2_eth_free_rx_fd(struct dpaa2_eth_priv *priv,
141 				 const struct dpaa2_fd *fd,
142 				 void *vaddr)
143 {
144 	struct device *dev = priv->net_dev->dev.parent;
145 	dma_addr_t addr = dpaa2_fd_get_addr(fd);
146 	u8 fd_format = dpaa2_fd_get_format(fd);
147 	struct dpaa2_sg_entry *sgt;
148 	void *sg_vaddr;
149 	int i;
150 
151 	/* If single buffer frame, just free the data buffer */
152 	if (fd_format == dpaa2_fd_single)
153 		goto free_buf;
154 	else if (fd_format != dpaa2_fd_sg)
155 		/* We don't support any other format */
156 		return;
157 
158 	/* For S/G frames, we first need to free all SG entries
159 	 * except the first one, which was taken care of already
160 	 */
161 	sgt = vaddr + dpaa2_fd_get_offset(fd);
162 	for (i = 1; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
163 		addr = dpaa2_sg_get_addr(&sgt[i]);
164 		sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
165 		dma_unmap_page(dev, addr, priv->rx_buf_size,
166 			       DMA_BIDIRECTIONAL);
167 
168 		free_pages((unsigned long)sg_vaddr, 0);
169 		if (dpaa2_sg_is_final(&sgt[i]))
170 			break;
171 	}
172 
173 free_buf:
174 	free_pages((unsigned long)vaddr, 0);
175 }
176 
177 /* Build a linear skb based on a single-buffer frame descriptor */
178 static struct sk_buff *dpaa2_eth_build_linear_skb(struct dpaa2_eth_channel *ch,
179 						  const struct dpaa2_fd *fd,
180 						  void *fd_vaddr)
181 {
182 	struct sk_buff *skb = NULL;
183 	u16 fd_offset = dpaa2_fd_get_offset(fd);
184 	u32 fd_length = dpaa2_fd_get_len(fd);
185 
186 	ch->buf_count--;
187 
188 	skb = build_skb(fd_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
189 	if (unlikely(!skb))
190 		return NULL;
191 
192 	skb_reserve(skb, fd_offset);
193 	skb_put(skb, fd_length);
194 
195 	return skb;
196 }
197 
198 /* Build a non linear (fragmented) skb based on a S/G table */
199 static struct sk_buff *dpaa2_eth_build_frag_skb(struct dpaa2_eth_priv *priv,
200 						struct dpaa2_eth_channel *ch,
201 						struct dpaa2_sg_entry *sgt)
202 {
203 	struct sk_buff *skb = NULL;
204 	struct device *dev = priv->net_dev->dev.parent;
205 	void *sg_vaddr;
206 	dma_addr_t sg_addr;
207 	u16 sg_offset;
208 	u32 sg_length;
209 	struct page *page, *head_page;
210 	int page_offset;
211 	int i;
212 
213 	for (i = 0; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
214 		struct dpaa2_sg_entry *sge = &sgt[i];
215 
216 		/* NOTE: We only support SG entries in dpaa2_sg_single format,
217 		 * but this is the only format we may receive from HW anyway
218 		 */
219 
220 		/* Get the address and length from the S/G entry */
221 		sg_addr = dpaa2_sg_get_addr(sge);
222 		sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, sg_addr);
223 		dma_unmap_page(dev, sg_addr, priv->rx_buf_size,
224 			       DMA_BIDIRECTIONAL);
225 
226 		sg_length = dpaa2_sg_get_len(sge);
227 
228 		if (i == 0) {
229 			/* We build the skb around the first data buffer */
230 			skb = build_skb(sg_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
231 			if (unlikely(!skb)) {
232 				/* Free the first SG entry now, since we already
233 				 * unmapped it and obtained the virtual address
234 				 */
235 				free_pages((unsigned long)sg_vaddr, 0);
236 
237 				/* We still need to subtract the buffers used
238 				 * by this FD from our software counter
239 				 */
240 				while (!dpaa2_sg_is_final(&sgt[i]) &&
241 				       i < DPAA2_ETH_MAX_SG_ENTRIES)
242 					i++;
243 				break;
244 			}
245 
246 			sg_offset = dpaa2_sg_get_offset(sge);
247 			skb_reserve(skb, sg_offset);
248 			skb_put(skb, sg_length);
249 		} else {
250 			/* Rest of the data buffers are stored as skb frags */
251 			page = virt_to_page(sg_vaddr);
252 			head_page = virt_to_head_page(sg_vaddr);
253 
254 			/* Offset in page (which may be compound).
255 			 * Data in subsequent SG entries is stored from the
256 			 * beginning of the buffer, so we don't need to add the
257 			 * sg_offset.
258 			 */
259 			page_offset = ((unsigned long)sg_vaddr &
260 				(PAGE_SIZE - 1)) +
261 				(page_address(page) - page_address(head_page));
262 
263 			skb_add_rx_frag(skb, i - 1, head_page, page_offset,
264 					sg_length, priv->rx_buf_size);
265 		}
266 
267 		if (dpaa2_sg_is_final(sge))
268 			break;
269 	}
270 
271 	WARN_ONCE(i == DPAA2_ETH_MAX_SG_ENTRIES, "Final bit not set in SGT");
272 
273 	/* Count all data buffers + SG table buffer */
274 	ch->buf_count -= i + 2;
275 
276 	return skb;
277 }
278 
279 /* Free buffers acquired from the buffer pool or which were meant to
280  * be released in the pool
281  */
282 static void dpaa2_eth_free_bufs(struct dpaa2_eth_priv *priv, u64 *buf_array,
283 				int count, bool xsk_zc)
284 {
285 	struct device *dev = priv->net_dev->dev.parent;
286 	struct dpaa2_eth_swa *swa;
287 	struct xdp_buff *xdp_buff;
288 	void *vaddr;
289 	int i;
290 
291 	for (i = 0; i < count; i++) {
292 		vaddr = dpaa2_iova_to_virt(priv->iommu_domain, buf_array[i]);
293 
294 		if (!xsk_zc) {
295 			dma_unmap_page(dev, buf_array[i], priv->rx_buf_size,
296 				       DMA_BIDIRECTIONAL);
297 			free_pages((unsigned long)vaddr, 0);
298 		} else {
299 			swa = (struct dpaa2_eth_swa *)
300 				(vaddr + DPAA2_ETH_RX_HWA_SIZE);
301 			xdp_buff = swa->xsk.xdp_buff;
302 			xsk_buff_free(xdp_buff);
303 		}
304 	}
305 }
306 
307 void dpaa2_eth_recycle_buf(struct dpaa2_eth_priv *priv,
308 			   struct dpaa2_eth_channel *ch,
309 			   dma_addr_t addr)
310 {
311 	int retries = 0;
312 	int err;
313 
314 	ch->recycled_bufs[ch->recycled_bufs_cnt++] = addr;
315 	if (ch->recycled_bufs_cnt < DPAA2_ETH_BUFS_PER_CMD)
316 		return;
317 
318 	while ((err = dpaa2_io_service_release(ch->dpio, ch->bp->bpid,
319 					       ch->recycled_bufs,
320 					       ch->recycled_bufs_cnt)) == -EBUSY) {
321 		if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
322 			break;
323 		cpu_relax();
324 	}
325 
326 	if (err) {
327 		dpaa2_eth_free_bufs(priv, ch->recycled_bufs,
328 				    ch->recycled_bufs_cnt, ch->xsk_zc);
329 		ch->buf_count -= ch->recycled_bufs_cnt;
330 	}
331 
332 	ch->recycled_bufs_cnt = 0;
333 }
334 
335 static int dpaa2_eth_xdp_flush(struct dpaa2_eth_priv *priv,
336 			       struct dpaa2_eth_fq *fq,
337 			       struct dpaa2_eth_xdp_fds *xdp_fds)
338 {
339 	int total_enqueued = 0, retries = 0, enqueued;
340 	struct dpaa2_eth_drv_stats *percpu_extras;
341 	int num_fds, err, max_retries;
342 	struct dpaa2_fd *fds;
343 
344 	percpu_extras = this_cpu_ptr(priv->percpu_extras);
345 
346 	/* try to enqueue all the FDs until the max number of retries is hit */
347 	fds = xdp_fds->fds;
348 	num_fds = xdp_fds->num;
349 	max_retries = num_fds * DPAA2_ETH_ENQUEUE_RETRIES;
350 	while (total_enqueued < num_fds && retries < max_retries) {
351 		err = priv->enqueue(priv, fq, &fds[total_enqueued],
352 				    0, num_fds - total_enqueued, &enqueued);
353 		if (err == -EBUSY) {
354 			percpu_extras->tx_portal_busy += ++retries;
355 			continue;
356 		}
357 		total_enqueued += enqueued;
358 	}
359 	xdp_fds->num = 0;
360 
361 	return total_enqueued;
362 }
363 
364 static void dpaa2_eth_xdp_tx_flush(struct dpaa2_eth_priv *priv,
365 				   struct dpaa2_eth_channel *ch,
366 				   struct dpaa2_eth_fq *fq)
367 {
368 	struct rtnl_link_stats64 *percpu_stats;
369 	struct dpaa2_fd *fds;
370 	int enqueued, i;
371 
372 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
373 
374 	// enqueue the array of XDP_TX frames
375 	enqueued = dpaa2_eth_xdp_flush(priv, fq, &fq->xdp_tx_fds);
376 
377 	/* update statistics */
378 	percpu_stats->tx_packets += enqueued;
379 	fds = fq->xdp_tx_fds.fds;
380 	for (i = 0; i < enqueued; i++) {
381 		percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
382 		ch->stats.xdp_tx++;
383 	}
384 	for (i = enqueued; i < fq->xdp_tx_fds.num; i++) {
385 		dpaa2_eth_recycle_buf(priv, ch, dpaa2_fd_get_addr(&fds[i]));
386 		percpu_stats->tx_errors++;
387 		ch->stats.xdp_tx_err++;
388 	}
389 	fq->xdp_tx_fds.num = 0;
390 }
391 
392 void dpaa2_eth_xdp_enqueue(struct dpaa2_eth_priv *priv,
393 			   struct dpaa2_eth_channel *ch,
394 			   struct dpaa2_fd *fd,
395 			   void *buf_start, u16 queue_id)
396 {
397 	struct dpaa2_faead *faead;
398 	struct dpaa2_fd *dest_fd;
399 	struct dpaa2_eth_fq *fq;
400 	u32 ctrl, frc;
401 
402 	/* Mark the egress frame hardware annotation area as valid */
403 	frc = dpaa2_fd_get_frc(fd);
404 	dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
405 	dpaa2_fd_set_ctrl(fd, DPAA2_FD_CTRL_ASAL);
406 
407 	/* Instruct hardware to release the FD buffer directly into
408 	 * the buffer pool once transmission is completed, instead of
409 	 * sending a Tx confirmation frame to us
410 	 */
411 	ctrl = DPAA2_FAEAD_A4V | DPAA2_FAEAD_A2V | DPAA2_FAEAD_EBDDV;
412 	faead = dpaa2_get_faead(buf_start, false);
413 	faead->ctrl = cpu_to_le32(ctrl);
414 	faead->conf_fqid = 0;
415 
416 	fq = &priv->fq[queue_id];
417 	dest_fd = &fq->xdp_tx_fds.fds[fq->xdp_tx_fds.num++];
418 	memcpy(dest_fd, fd, sizeof(*dest_fd));
419 
420 	if (fq->xdp_tx_fds.num < DEV_MAP_BULK_SIZE)
421 		return;
422 
423 	dpaa2_eth_xdp_tx_flush(priv, ch, fq);
424 }
425 
426 static u32 dpaa2_eth_run_xdp(struct dpaa2_eth_priv *priv,
427 			     struct dpaa2_eth_channel *ch,
428 			     struct dpaa2_eth_fq *rx_fq,
429 			     struct dpaa2_fd *fd, void *vaddr)
430 {
431 	dma_addr_t addr = dpaa2_fd_get_addr(fd);
432 	struct bpf_prog *xdp_prog;
433 	struct xdp_buff xdp;
434 	u32 xdp_act = XDP_PASS;
435 	int err, offset;
436 
437 	xdp_prog = READ_ONCE(ch->xdp.prog);
438 	if (!xdp_prog)
439 		goto out;
440 
441 	offset = dpaa2_fd_get_offset(fd) - XDP_PACKET_HEADROOM;
442 	xdp_init_buff(&xdp, DPAA2_ETH_RX_BUF_RAW_SIZE - offset, &ch->xdp_rxq);
443 	xdp_prepare_buff(&xdp, vaddr + offset, XDP_PACKET_HEADROOM,
444 			 dpaa2_fd_get_len(fd), false);
445 
446 	xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
447 
448 	/* xdp.data pointer may have changed */
449 	dpaa2_fd_set_offset(fd, xdp.data - vaddr);
450 	dpaa2_fd_set_len(fd, xdp.data_end - xdp.data);
451 
452 	switch (xdp_act) {
453 	case XDP_PASS:
454 		break;
455 	case XDP_TX:
456 		dpaa2_eth_xdp_enqueue(priv, ch, fd, vaddr, rx_fq->flowid);
457 		break;
458 	default:
459 		bpf_warn_invalid_xdp_action(priv->net_dev, xdp_prog, xdp_act);
460 		fallthrough;
461 	case XDP_ABORTED:
462 		trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
463 		fallthrough;
464 	case XDP_DROP:
465 		dpaa2_eth_recycle_buf(priv, ch, addr);
466 		ch->stats.xdp_drop++;
467 		break;
468 	case XDP_REDIRECT:
469 		dma_unmap_page(priv->net_dev->dev.parent, addr,
470 			       priv->rx_buf_size, DMA_BIDIRECTIONAL);
471 		ch->buf_count--;
472 
473 		/* Allow redirect use of full headroom */
474 		xdp.data_hard_start = vaddr;
475 		xdp.frame_sz = DPAA2_ETH_RX_BUF_RAW_SIZE;
476 
477 		err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog);
478 		if (unlikely(err)) {
479 			addr = dma_map_page(priv->net_dev->dev.parent,
480 					    virt_to_page(vaddr), 0,
481 					    priv->rx_buf_size, DMA_BIDIRECTIONAL);
482 			if (unlikely(dma_mapping_error(priv->net_dev->dev.parent, addr))) {
483 				free_pages((unsigned long)vaddr, 0);
484 			} else {
485 				ch->buf_count++;
486 				dpaa2_eth_recycle_buf(priv, ch, addr);
487 			}
488 			ch->stats.xdp_drop++;
489 		} else {
490 			ch->stats.xdp_redirect++;
491 		}
492 		break;
493 	}
494 
495 	ch->xdp.res |= xdp_act;
496 out:
497 	return xdp_act;
498 }
499 
500 struct sk_buff *dpaa2_eth_alloc_skb(struct dpaa2_eth_priv *priv,
501 				    struct dpaa2_eth_channel *ch,
502 				    const struct dpaa2_fd *fd, u32 fd_length,
503 				    void *fd_vaddr)
504 {
505 	u16 fd_offset = dpaa2_fd_get_offset(fd);
506 	struct sk_buff *skb = NULL;
507 	unsigned int skb_len;
508 
509 	skb_len = fd_length + dpaa2_eth_needed_headroom(NULL);
510 
511 	skb = napi_alloc_skb(&ch->napi, skb_len);
512 	if (!skb)
513 		return NULL;
514 
515 	skb_reserve(skb, dpaa2_eth_needed_headroom(NULL));
516 	skb_put(skb, fd_length);
517 
518 	memcpy(skb->data, fd_vaddr + fd_offset, fd_length);
519 
520 	dpaa2_eth_recycle_buf(priv, ch, dpaa2_fd_get_addr(fd));
521 
522 	return skb;
523 }
524 
525 static struct sk_buff *dpaa2_eth_copybreak(struct dpaa2_eth_channel *ch,
526 					   const struct dpaa2_fd *fd,
527 					   void *fd_vaddr)
528 {
529 	struct dpaa2_eth_priv *priv = ch->priv;
530 	u32 fd_length = dpaa2_fd_get_len(fd);
531 
532 	if (fd_length > priv->rx_copybreak)
533 		return NULL;
534 
535 	return dpaa2_eth_alloc_skb(priv, ch, fd, fd_length, fd_vaddr);
536 }
537 
538 void dpaa2_eth_receive_skb(struct dpaa2_eth_priv *priv,
539 			   struct dpaa2_eth_channel *ch,
540 			   const struct dpaa2_fd *fd, void *vaddr,
541 			   struct dpaa2_eth_fq *fq,
542 			   struct rtnl_link_stats64 *percpu_stats,
543 			   struct sk_buff *skb)
544 {
545 	struct dpaa2_fas *fas;
546 	u32 status = 0;
547 
548 	fas = dpaa2_get_fas(vaddr, false);
549 	prefetch(fas);
550 	prefetch(skb->data);
551 
552 	/* Get the timestamp value */
553 	if (priv->rx_tstamp) {
554 		struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
555 		__le64 *ts = dpaa2_get_ts(vaddr, false);
556 		u64 ns;
557 
558 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
559 
560 		ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
561 		shhwtstamps->hwtstamp = ns_to_ktime(ns);
562 	}
563 
564 	/* Check if we need to validate the L4 csum */
565 	if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) {
566 		status = le32_to_cpu(fas->status);
567 		dpaa2_eth_validate_rx_csum(priv, status, skb);
568 	}
569 
570 	skb->protocol = eth_type_trans(skb, priv->net_dev);
571 	skb_record_rx_queue(skb, fq->flowid);
572 
573 	percpu_stats->rx_packets++;
574 	percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
575 	ch->stats.bytes_per_cdan += dpaa2_fd_get_len(fd);
576 
577 	list_add_tail(&skb->list, ch->rx_list);
578 }
579 
580 /* Main Rx frame processing routine */
581 void dpaa2_eth_rx(struct dpaa2_eth_priv *priv,
582 		  struct dpaa2_eth_channel *ch,
583 		  const struct dpaa2_fd *fd,
584 		  struct dpaa2_eth_fq *fq)
585 {
586 	dma_addr_t addr = dpaa2_fd_get_addr(fd);
587 	u8 fd_format = dpaa2_fd_get_format(fd);
588 	void *vaddr;
589 	struct sk_buff *skb;
590 	struct rtnl_link_stats64 *percpu_stats;
591 	struct dpaa2_eth_drv_stats *percpu_extras;
592 	struct device *dev = priv->net_dev->dev.parent;
593 	void *buf_data;
594 	u32 xdp_act;
595 
596 	/* Tracing point */
597 	trace_dpaa2_rx_fd(priv->net_dev, fd);
598 
599 	vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
600 	dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
601 				DMA_BIDIRECTIONAL);
602 
603 	buf_data = vaddr + dpaa2_fd_get_offset(fd);
604 	prefetch(buf_data);
605 
606 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
607 	percpu_extras = this_cpu_ptr(priv->percpu_extras);
608 
609 	if (fd_format == dpaa2_fd_single) {
610 		xdp_act = dpaa2_eth_run_xdp(priv, ch, fq, (struct dpaa2_fd *)fd, vaddr);
611 		if (xdp_act != XDP_PASS) {
612 			percpu_stats->rx_packets++;
613 			percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
614 			return;
615 		}
616 
617 		skb = dpaa2_eth_copybreak(ch, fd, vaddr);
618 		if (!skb) {
619 			dma_unmap_page(dev, addr, priv->rx_buf_size,
620 				       DMA_BIDIRECTIONAL);
621 			skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr);
622 		}
623 	} else if (fd_format == dpaa2_fd_sg) {
624 		WARN_ON(priv->xdp_prog);
625 
626 		dma_unmap_page(dev, addr, priv->rx_buf_size,
627 			       DMA_BIDIRECTIONAL);
628 		skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data);
629 		free_pages((unsigned long)vaddr, 0);
630 		percpu_extras->rx_sg_frames++;
631 		percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd);
632 	} else {
633 		/* We don't support any other format */
634 		goto err_frame_format;
635 	}
636 
637 	if (unlikely(!skb))
638 		goto err_build_skb;
639 
640 	dpaa2_eth_receive_skb(priv, ch, fd, vaddr, fq, percpu_stats, skb);
641 	return;
642 
643 err_build_skb:
644 	dpaa2_eth_free_rx_fd(priv, fd, vaddr);
645 err_frame_format:
646 	percpu_stats->rx_dropped++;
647 }
648 
649 /* Processing of Rx frames received on the error FQ
650  * We check and print the error bits and then free the frame
651  */
652 static void dpaa2_eth_rx_err(struct dpaa2_eth_priv *priv,
653 			     struct dpaa2_eth_channel *ch,
654 			     const struct dpaa2_fd *fd,
655 			     struct dpaa2_eth_fq *fq __always_unused)
656 {
657 	struct device *dev = priv->net_dev->dev.parent;
658 	dma_addr_t addr = dpaa2_fd_get_addr(fd);
659 	u8 fd_format = dpaa2_fd_get_format(fd);
660 	struct rtnl_link_stats64 *percpu_stats;
661 	struct dpaa2_eth_trap_item *trap_item;
662 	struct dpaa2_fapr *fapr;
663 	struct sk_buff *skb;
664 	void *buf_data;
665 	void *vaddr;
666 
667 	vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
668 	dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
669 				DMA_BIDIRECTIONAL);
670 
671 	buf_data = vaddr + dpaa2_fd_get_offset(fd);
672 
673 	if (fd_format == dpaa2_fd_single) {
674 		dma_unmap_page(dev, addr, priv->rx_buf_size,
675 			       DMA_BIDIRECTIONAL);
676 		skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr);
677 	} else if (fd_format == dpaa2_fd_sg) {
678 		dma_unmap_page(dev, addr, priv->rx_buf_size,
679 			       DMA_BIDIRECTIONAL);
680 		skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data);
681 		free_pages((unsigned long)vaddr, 0);
682 	} else {
683 		/* We don't support any other format */
684 		dpaa2_eth_free_rx_fd(priv, fd, vaddr);
685 		goto err_frame_format;
686 	}
687 
688 	fapr = dpaa2_get_fapr(vaddr, false);
689 	trap_item = dpaa2_eth_dl_get_trap(priv, fapr);
690 	if (trap_item)
691 		devlink_trap_report(priv->devlink, skb, trap_item->trap_ctx,
692 				    &priv->devlink_port, NULL);
693 	consume_skb(skb);
694 
695 err_frame_format:
696 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
697 	percpu_stats->rx_errors++;
698 	ch->buf_count--;
699 }
700 
701 /* Consume all frames pull-dequeued into the store. This is the simplest way to
702  * make sure we don't accidentally issue another volatile dequeue which would
703  * overwrite (leak) frames already in the store.
704  *
705  * Observance of NAPI budget is not our concern, leaving that to the caller.
706  */
707 static int dpaa2_eth_consume_frames(struct dpaa2_eth_channel *ch,
708 				    struct dpaa2_eth_fq **src)
709 {
710 	struct dpaa2_eth_priv *priv = ch->priv;
711 	struct dpaa2_eth_fq *fq = NULL;
712 	struct dpaa2_dq *dq;
713 	const struct dpaa2_fd *fd;
714 	int cleaned = 0, retries = 0;
715 	int is_last;
716 
717 	do {
718 		dq = dpaa2_io_store_next(ch->store, &is_last);
719 		if (unlikely(!dq)) {
720 			/* If we're here, we *must* have placed a
721 			 * volatile dequeue comnmand, so keep reading through
722 			 * the store until we get some sort of valid response
723 			 * token (either a valid frame or an "empty dequeue")
724 			 */
725 			if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES) {
726 				netdev_err_once(priv->net_dev,
727 						"Unable to read a valid dequeue response\n");
728 				return -ETIMEDOUT;
729 			}
730 			continue;
731 		}
732 
733 		fd = dpaa2_dq_fd(dq);
734 		fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq);
735 
736 		fq->consume(priv, ch, fd, fq);
737 		cleaned++;
738 		retries = 0;
739 	} while (!is_last);
740 
741 	if (!cleaned)
742 		return 0;
743 
744 	fq->stats.frames += cleaned;
745 	ch->stats.frames += cleaned;
746 	ch->stats.frames_per_cdan += cleaned;
747 
748 	/* A dequeue operation only pulls frames from a single queue
749 	 * into the store. Return the frame queue as an out param.
750 	 */
751 	if (src)
752 		*src = fq;
753 
754 	return cleaned;
755 }
756 
757 static int dpaa2_eth_ptp_parse(struct sk_buff *skb,
758 			       u8 *msgtype, u8 *twostep, u8 *udp,
759 			       u16 *correction_offset,
760 			       u16 *origintimestamp_offset)
761 {
762 	unsigned int ptp_class;
763 	struct ptp_header *hdr;
764 	unsigned int type;
765 	u8 *base;
766 
767 	ptp_class = ptp_classify_raw(skb);
768 	if (ptp_class == PTP_CLASS_NONE)
769 		return -EINVAL;
770 
771 	hdr = ptp_parse_header(skb, ptp_class);
772 	if (!hdr)
773 		return -EINVAL;
774 
775 	*msgtype = ptp_get_msgtype(hdr, ptp_class);
776 	*twostep = hdr->flag_field[0] & 0x2;
777 
778 	type = ptp_class & PTP_CLASS_PMASK;
779 	if (type == PTP_CLASS_IPV4 ||
780 	    type == PTP_CLASS_IPV6)
781 		*udp = 1;
782 	else
783 		*udp = 0;
784 
785 	base = skb_mac_header(skb);
786 	*correction_offset = (u8 *)&hdr->correction - base;
787 	*origintimestamp_offset = (u8 *)hdr + sizeof(struct ptp_header) - base;
788 
789 	return 0;
790 }
791 
792 /* Configure the egress frame annotation for timestamp update */
793 static void dpaa2_eth_enable_tx_tstamp(struct dpaa2_eth_priv *priv,
794 				       struct dpaa2_fd *fd,
795 				       void *buf_start,
796 				       struct sk_buff *skb)
797 {
798 	struct ptp_tstamp origin_timestamp;
799 	u8 msgtype, twostep, udp;
800 	struct dpaa2_faead *faead;
801 	struct dpaa2_fas *fas;
802 	struct timespec64 ts;
803 	u16 offset1, offset2;
804 	u32 ctrl, frc;
805 	__le64 *ns;
806 	u8 *data;
807 
808 	/* Mark the egress frame annotation area as valid */
809 	frc = dpaa2_fd_get_frc(fd);
810 	dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
811 
812 	/* Set hardware annotation size */
813 	ctrl = dpaa2_fd_get_ctrl(fd);
814 	dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL);
815 
816 	/* enable UPD (update prepanded data) bit in FAEAD field of
817 	 * hardware frame annotation area
818 	 */
819 	ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD;
820 	faead = dpaa2_get_faead(buf_start, true);
821 	faead->ctrl = cpu_to_le32(ctrl);
822 
823 	if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
824 		if (dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
825 					&offset1, &offset2) ||
826 		    msgtype != PTP_MSGTYPE_SYNC || twostep) {
827 			WARN_ONCE(1, "Bad packet for one-step timestamping\n");
828 			return;
829 		}
830 
831 		/* Mark the frame annotation status as valid */
832 		frc = dpaa2_fd_get_frc(fd);
833 		dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FASV);
834 
835 		/* Mark the PTP flag for one step timestamping */
836 		fas = dpaa2_get_fas(buf_start, true);
837 		fas->status = cpu_to_le32(DPAA2_FAS_PTP);
838 
839 		dpaa2_ptp->caps.gettime64(&dpaa2_ptp->caps, &ts);
840 		ns = dpaa2_get_ts(buf_start, true);
841 		*ns = cpu_to_le64(timespec64_to_ns(&ts) /
842 				  DPAA2_PTP_CLK_PERIOD_NS);
843 
844 		/* Update current time to PTP message originTimestamp field */
845 		ns_to_ptp_tstamp(&origin_timestamp, le64_to_cpup(ns));
846 		data = skb_mac_header(skb);
847 		*(__be16 *)(data + offset2) = htons(origin_timestamp.sec_msb);
848 		*(__be32 *)(data + offset2 + 2) =
849 			htonl(origin_timestamp.sec_lsb);
850 		*(__be32 *)(data + offset2 + 6) = htonl(origin_timestamp.nsec);
851 
852 		if (priv->ptp_correction_off == offset1)
853 			return;
854 
855 		priv->dpaa2_set_onestep_params_cb(priv, offset1, udp);
856 		priv->ptp_correction_off = offset1;
857 
858 	}
859 }
860 
861 void *dpaa2_eth_sgt_get(struct dpaa2_eth_priv *priv)
862 {
863 	struct dpaa2_eth_sgt_cache *sgt_cache;
864 	void *sgt_buf = NULL;
865 	int sgt_buf_size;
866 
867 	sgt_cache = this_cpu_ptr(priv->sgt_cache);
868 	sgt_buf_size = priv->tx_data_offset +
869 		DPAA2_ETH_SG_ENTRIES_MAX * sizeof(struct dpaa2_sg_entry);
870 
871 	if (sgt_cache->count == 0)
872 		sgt_buf = napi_alloc_frag_align(sgt_buf_size, DPAA2_ETH_TX_BUF_ALIGN);
873 	else
874 		sgt_buf = sgt_cache->buf[--sgt_cache->count];
875 	if (!sgt_buf)
876 		return NULL;
877 
878 	memset(sgt_buf, 0, sgt_buf_size);
879 
880 	return sgt_buf;
881 }
882 
883 void dpaa2_eth_sgt_recycle(struct dpaa2_eth_priv *priv, void *sgt_buf)
884 {
885 	struct dpaa2_eth_sgt_cache *sgt_cache;
886 
887 	sgt_cache = this_cpu_ptr(priv->sgt_cache);
888 	if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE)
889 		skb_free_frag(sgt_buf);
890 	else
891 		sgt_cache->buf[sgt_cache->count++] = sgt_buf;
892 }
893 
894 /* Create a frame descriptor based on a fragmented skb */
895 static int dpaa2_eth_build_sg_fd(struct dpaa2_eth_priv *priv,
896 				 struct sk_buff *skb,
897 				 struct dpaa2_fd *fd,
898 				 void **swa_addr)
899 {
900 	struct device *dev = priv->net_dev->dev.parent;
901 	void *sgt_buf = NULL;
902 	dma_addr_t addr;
903 	int nr_frags = skb_shinfo(skb)->nr_frags;
904 	struct dpaa2_sg_entry *sgt;
905 	int i, err;
906 	int sgt_buf_size;
907 	struct scatterlist *scl, *crt_scl;
908 	int num_sg;
909 	int num_dma_bufs;
910 	struct dpaa2_eth_swa *swa;
911 
912 	/* Create and map scatterlist.
913 	 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have
914 	 * to go beyond nr_frags+1.
915 	 * Note: We don't support chained scatterlists
916 	 */
917 	if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1))
918 		return -EINVAL;
919 
920 	scl = kmalloc_array(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC);
921 	if (unlikely(!scl))
922 		return -ENOMEM;
923 
924 	sg_init_table(scl, nr_frags + 1);
925 	num_sg = skb_to_sgvec(skb, scl, 0, skb->len);
926 	if (unlikely(num_sg < 0)) {
927 		err = -ENOMEM;
928 		goto dma_map_sg_failed;
929 	}
930 	num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
931 	if (unlikely(!num_dma_bufs)) {
932 		err = -ENOMEM;
933 		goto dma_map_sg_failed;
934 	}
935 
936 	/* Prepare the HW SGT structure */
937 	sgt_buf_size = priv->tx_data_offset +
938 		       sizeof(struct dpaa2_sg_entry) *  num_dma_bufs;
939 	sgt_buf = dpaa2_eth_sgt_get(priv);
940 	if (unlikely(!sgt_buf)) {
941 		err = -ENOMEM;
942 		goto sgt_buf_alloc_failed;
943 	}
944 
945 	sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
946 
947 	/* Fill in the HW SGT structure.
948 	 *
949 	 * sgt_buf is zeroed out, so the following fields are implicit
950 	 * in all sgt entries:
951 	 *   - offset is 0
952 	 *   - format is 'dpaa2_sg_single'
953 	 */
954 	for_each_sg(scl, crt_scl, num_dma_bufs, i) {
955 		dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl));
956 		dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl));
957 	}
958 	dpaa2_sg_set_final(&sgt[i - 1], true);
959 
960 	/* Store the skb backpointer in the SGT buffer.
961 	 * Fit the scatterlist and the number of buffers alongside the
962 	 * skb backpointer in the software annotation area. We'll need
963 	 * all of them on Tx Conf.
964 	 */
965 	*swa_addr = (void *)sgt_buf;
966 	swa = (struct dpaa2_eth_swa *)sgt_buf;
967 	swa->type = DPAA2_ETH_SWA_SG;
968 	swa->sg.skb = skb;
969 	swa->sg.scl = scl;
970 	swa->sg.num_sg = num_sg;
971 	swa->sg.sgt_size = sgt_buf_size;
972 
973 	/* Separately map the SGT buffer */
974 	addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
975 	if (unlikely(dma_mapping_error(dev, addr))) {
976 		err = -ENOMEM;
977 		goto dma_map_single_failed;
978 	}
979 	memset(fd, 0, sizeof(struct dpaa2_fd));
980 	dpaa2_fd_set_offset(fd, priv->tx_data_offset);
981 	dpaa2_fd_set_format(fd, dpaa2_fd_sg);
982 	dpaa2_fd_set_addr(fd, addr);
983 	dpaa2_fd_set_len(fd, skb->len);
984 	dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
985 
986 	return 0;
987 
988 dma_map_single_failed:
989 	dpaa2_eth_sgt_recycle(priv, sgt_buf);
990 sgt_buf_alloc_failed:
991 	dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
992 dma_map_sg_failed:
993 	kfree(scl);
994 	return err;
995 }
996 
997 /* Create a SG frame descriptor based on a linear skb.
998  *
999  * This function is used on the Tx path when the skb headroom is not large
1000  * enough for the HW requirements, thus instead of realloc-ing the skb we
1001  * create a SG frame descriptor with only one entry.
1002  */
1003 static int dpaa2_eth_build_sg_fd_single_buf(struct dpaa2_eth_priv *priv,
1004 					    struct sk_buff *skb,
1005 					    struct dpaa2_fd *fd,
1006 					    void **swa_addr)
1007 {
1008 	struct device *dev = priv->net_dev->dev.parent;
1009 	struct dpaa2_sg_entry *sgt;
1010 	struct dpaa2_eth_swa *swa;
1011 	dma_addr_t addr, sgt_addr;
1012 	void *sgt_buf = NULL;
1013 	int sgt_buf_size;
1014 	int err;
1015 
1016 	/* Prepare the HW SGT structure */
1017 	sgt_buf_size = priv->tx_data_offset + sizeof(struct dpaa2_sg_entry);
1018 	sgt_buf = dpaa2_eth_sgt_get(priv);
1019 	if (unlikely(!sgt_buf))
1020 		return -ENOMEM;
1021 	sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
1022 
1023 	addr = dma_map_single(dev, skb->data, skb->len, DMA_BIDIRECTIONAL);
1024 	if (unlikely(dma_mapping_error(dev, addr))) {
1025 		err = -ENOMEM;
1026 		goto data_map_failed;
1027 	}
1028 
1029 	/* Fill in the HW SGT structure */
1030 	dpaa2_sg_set_addr(sgt, addr);
1031 	dpaa2_sg_set_len(sgt, skb->len);
1032 	dpaa2_sg_set_final(sgt, true);
1033 
1034 	/* Store the skb backpointer in the SGT buffer */
1035 	*swa_addr = (void *)sgt_buf;
1036 	swa = (struct dpaa2_eth_swa *)sgt_buf;
1037 	swa->type = DPAA2_ETH_SWA_SINGLE;
1038 	swa->single.skb = skb;
1039 	swa->single.sgt_size = sgt_buf_size;
1040 
1041 	/* Separately map the SGT buffer */
1042 	sgt_addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
1043 	if (unlikely(dma_mapping_error(dev, sgt_addr))) {
1044 		err = -ENOMEM;
1045 		goto sgt_map_failed;
1046 	}
1047 
1048 	memset(fd, 0, sizeof(struct dpaa2_fd));
1049 	dpaa2_fd_set_offset(fd, priv->tx_data_offset);
1050 	dpaa2_fd_set_format(fd, dpaa2_fd_sg);
1051 	dpaa2_fd_set_addr(fd, sgt_addr);
1052 	dpaa2_fd_set_len(fd, skb->len);
1053 	dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
1054 
1055 	return 0;
1056 
1057 sgt_map_failed:
1058 	dma_unmap_single(dev, addr, skb->len, DMA_BIDIRECTIONAL);
1059 data_map_failed:
1060 	dpaa2_eth_sgt_recycle(priv, sgt_buf);
1061 
1062 	return err;
1063 }
1064 
1065 /* Create a frame descriptor based on a linear skb */
1066 static int dpaa2_eth_build_single_fd(struct dpaa2_eth_priv *priv,
1067 				     struct sk_buff *skb,
1068 				     struct dpaa2_fd *fd,
1069 				     void **swa_addr)
1070 {
1071 	struct device *dev = priv->net_dev->dev.parent;
1072 	u8 *buffer_start, *aligned_start;
1073 	struct dpaa2_eth_swa *swa;
1074 	dma_addr_t addr;
1075 
1076 	buffer_start = skb->data - dpaa2_eth_needed_headroom(skb);
1077 
1078 	/* If there's enough room to align the FD address, do it.
1079 	 * It will help hardware optimize accesses.
1080 	 */
1081 	aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
1082 				  DPAA2_ETH_TX_BUF_ALIGN);
1083 	if (aligned_start >= skb->head)
1084 		buffer_start = aligned_start;
1085 
1086 	/* Store a backpointer to the skb at the beginning of the buffer
1087 	 * (in the private data area) such that we can release it
1088 	 * on Tx confirm
1089 	 */
1090 	*swa_addr = (void *)buffer_start;
1091 	swa = (struct dpaa2_eth_swa *)buffer_start;
1092 	swa->type = DPAA2_ETH_SWA_SINGLE;
1093 	swa->single.skb = skb;
1094 
1095 	addr = dma_map_single(dev, buffer_start,
1096 			      skb_tail_pointer(skb) - buffer_start,
1097 			      DMA_BIDIRECTIONAL);
1098 	if (unlikely(dma_mapping_error(dev, addr)))
1099 		return -ENOMEM;
1100 
1101 	memset(fd, 0, sizeof(struct dpaa2_fd));
1102 	dpaa2_fd_set_addr(fd, addr);
1103 	dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start));
1104 	dpaa2_fd_set_len(fd, skb->len);
1105 	dpaa2_fd_set_format(fd, dpaa2_fd_single);
1106 	dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
1107 
1108 	return 0;
1109 }
1110 
1111 /* FD freeing routine on the Tx path
1112  *
1113  * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb
1114  * back-pointed to is also freed.
1115  * This can be called either from dpaa2_eth_tx_conf() or on the error path of
1116  * dpaa2_eth_tx().
1117  */
1118 void dpaa2_eth_free_tx_fd(struct dpaa2_eth_priv *priv,
1119 			  struct dpaa2_eth_channel *ch,
1120 			  struct dpaa2_eth_fq *fq,
1121 			  const struct dpaa2_fd *fd, bool in_napi)
1122 {
1123 	struct device *dev = priv->net_dev->dev.parent;
1124 	dma_addr_t fd_addr, sg_addr;
1125 	struct sk_buff *skb = NULL;
1126 	unsigned char *buffer_start;
1127 	struct dpaa2_eth_swa *swa;
1128 	u8 fd_format = dpaa2_fd_get_format(fd);
1129 	u32 fd_len = dpaa2_fd_get_len(fd);
1130 	struct dpaa2_sg_entry *sgt;
1131 	int should_free_skb = 1;
1132 	void *tso_hdr;
1133 	int i;
1134 
1135 	fd_addr = dpaa2_fd_get_addr(fd);
1136 	buffer_start = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr);
1137 	swa = (struct dpaa2_eth_swa *)buffer_start;
1138 
1139 	if (fd_format == dpaa2_fd_single) {
1140 		if (swa->type == DPAA2_ETH_SWA_SINGLE) {
1141 			skb = swa->single.skb;
1142 			/* Accessing the skb buffer is safe before dma unmap,
1143 			 * because we didn't map the actual skb shell.
1144 			 */
1145 			dma_unmap_single(dev, fd_addr,
1146 					 skb_tail_pointer(skb) - buffer_start,
1147 					 DMA_BIDIRECTIONAL);
1148 		} else {
1149 			WARN_ONCE(swa->type != DPAA2_ETH_SWA_XDP, "Wrong SWA type");
1150 			dma_unmap_single(dev, fd_addr, swa->xdp.dma_size,
1151 					 DMA_BIDIRECTIONAL);
1152 		}
1153 	} else if (fd_format == dpaa2_fd_sg) {
1154 		if (swa->type == DPAA2_ETH_SWA_SG) {
1155 			skb = swa->sg.skb;
1156 
1157 			/* Unmap the scatterlist */
1158 			dma_unmap_sg(dev, swa->sg.scl, swa->sg.num_sg,
1159 				     DMA_BIDIRECTIONAL);
1160 			kfree(swa->sg.scl);
1161 
1162 			/* Unmap the SGT buffer */
1163 			dma_unmap_single(dev, fd_addr, swa->sg.sgt_size,
1164 					 DMA_BIDIRECTIONAL);
1165 		} else if (swa->type == DPAA2_ETH_SWA_SW_TSO) {
1166 			skb = swa->tso.skb;
1167 
1168 			sgt = (struct dpaa2_sg_entry *)(buffer_start +
1169 							priv->tx_data_offset);
1170 
1171 			/* Unmap the SGT buffer */
1172 			dma_unmap_single(dev, fd_addr, swa->tso.sgt_size,
1173 					 DMA_BIDIRECTIONAL);
1174 
1175 			/* Unmap and free the header */
1176 			tso_hdr = dpaa2_iova_to_virt(priv->iommu_domain, dpaa2_sg_get_addr(sgt));
1177 			dma_unmap_single(dev, dpaa2_sg_get_addr(sgt), TSO_HEADER_SIZE,
1178 					 DMA_TO_DEVICE);
1179 			kfree(tso_hdr);
1180 
1181 			/* Unmap the other SG entries for the data */
1182 			for (i = 1; i < swa->tso.num_sg; i++)
1183 				dma_unmap_single(dev, dpaa2_sg_get_addr(&sgt[i]),
1184 						 dpaa2_sg_get_len(&sgt[i]), DMA_TO_DEVICE);
1185 
1186 			if (!swa->tso.is_last_fd)
1187 				should_free_skb = 0;
1188 		} else if (swa->type == DPAA2_ETH_SWA_XSK) {
1189 			/* Unmap the SGT Buffer */
1190 			dma_unmap_single(dev, fd_addr, swa->xsk.sgt_size,
1191 					 DMA_BIDIRECTIONAL);
1192 		} else {
1193 			skb = swa->single.skb;
1194 
1195 			/* Unmap the SGT Buffer */
1196 			dma_unmap_single(dev, fd_addr, swa->single.sgt_size,
1197 					 DMA_BIDIRECTIONAL);
1198 
1199 			sgt = (struct dpaa2_sg_entry *)(buffer_start +
1200 							priv->tx_data_offset);
1201 			sg_addr = dpaa2_sg_get_addr(sgt);
1202 			dma_unmap_single(dev, sg_addr, skb->len, DMA_BIDIRECTIONAL);
1203 		}
1204 	} else {
1205 		netdev_dbg(priv->net_dev, "Invalid FD format\n");
1206 		return;
1207 	}
1208 
1209 	if (swa->type == DPAA2_ETH_SWA_XSK) {
1210 		ch->xsk_tx_pkts_sent++;
1211 		dpaa2_eth_sgt_recycle(priv, buffer_start);
1212 		return;
1213 	}
1214 
1215 	if (swa->type != DPAA2_ETH_SWA_XDP && in_napi) {
1216 		fq->dq_frames++;
1217 		fq->dq_bytes += fd_len;
1218 	}
1219 
1220 	if (swa->type == DPAA2_ETH_SWA_XDP) {
1221 		xdp_return_frame(swa->xdp.xdpf);
1222 		return;
1223 	}
1224 
1225 	/* Get the timestamp value */
1226 	if (swa->type != DPAA2_ETH_SWA_SW_TSO) {
1227 		if (skb->cb[0] == TX_TSTAMP) {
1228 			struct skb_shared_hwtstamps shhwtstamps;
1229 			__le64 *ts = dpaa2_get_ts(buffer_start, true);
1230 			u64 ns;
1231 
1232 			memset(&shhwtstamps, 0, sizeof(shhwtstamps));
1233 
1234 			ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
1235 			shhwtstamps.hwtstamp = ns_to_ktime(ns);
1236 			skb_tstamp_tx(skb, &shhwtstamps);
1237 		} else if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
1238 			mutex_unlock(&priv->onestep_tstamp_lock);
1239 		}
1240 	}
1241 
1242 	/* Free SGT buffer allocated on tx */
1243 	if (fd_format != dpaa2_fd_single)
1244 		dpaa2_eth_sgt_recycle(priv, buffer_start);
1245 
1246 	/* Move on with skb release. If we are just confirming multiple FDs
1247 	 * from the same TSO skb then only the last one will need to free the
1248 	 * skb.
1249 	 */
1250 	if (should_free_skb)
1251 		napi_consume_skb(skb, in_napi);
1252 }
1253 
1254 static int dpaa2_eth_build_gso_fd(struct dpaa2_eth_priv *priv,
1255 				  struct sk_buff *skb, struct dpaa2_fd *fd,
1256 				  int *num_fds, u32 *total_fds_len)
1257 {
1258 	struct device *dev = priv->net_dev->dev.parent;
1259 	int hdr_len, total_len, data_left, fd_len;
1260 	int num_sge, err, i, sgt_buf_size;
1261 	struct dpaa2_fd *fd_start = fd;
1262 	struct dpaa2_sg_entry *sgt;
1263 	struct dpaa2_eth_swa *swa;
1264 	dma_addr_t sgt_addr, addr;
1265 	dma_addr_t tso_hdr_dma;
1266 	unsigned int index = 0;
1267 	struct tso_t tso;
1268 	char *tso_hdr;
1269 	void *sgt_buf;
1270 
1271 	/* Initialize the TSO handler, and prepare the first payload */
1272 	hdr_len = tso_start(skb, &tso);
1273 	*total_fds_len = 0;
1274 
1275 	total_len = skb->len - hdr_len;
1276 	while (total_len > 0) {
1277 		/* Prepare the HW SGT structure for this frame */
1278 		sgt_buf = dpaa2_eth_sgt_get(priv);
1279 		if (unlikely(!sgt_buf)) {
1280 			netdev_err(priv->net_dev, "dpaa2_eth_sgt_get() failed\n");
1281 			err = -ENOMEM;
1282 			goto err_sgt_get;
1283 		}
1284 		sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
1285 
1286 		/* Determine the data length of this frame */
1287 		data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
1288 		total_len -= data_left;
1289 		fd_len = data_left + hdr_len;
1290 
1291 		/* Prepare packet headers: MAC + IP + TCP */
1292 		tso_hdr = kmalloc(TSO_HEADER_SIZE, GFP_ATOMIC);
1293 		if (!tso_hdr) {
1294 			err =  -ENOMEM;
1295 			goto err_alloc_tso_hdr;
1296 		}
1297 
1298 		tso_build_hdr(skb, tso_hdr, &tso, data_left, total_len == 0);
1299 		tso_hdr_dma = dma_map_single(dev, tso_hdr, TSO_HEADER_SIZE, DMA_TO_DEVICE);
1300 		if (dma_mapping_error(dev, tso_hdr_dma)) {
1301 			netdev_err(priv->net_dev, "dma_map_single(tso_hdr) failed\n");
1302 			err = -ENOMEM;
1303 			goto err_map_tso_hdr;
1304 		}
1305 
1306 		/* Setup the SG entry for the header */
1307 		dpaa2_sg_set_addr(sgt, tso_hdr_dma);
1308 		dpaa2_sg_set_len(sgt, hdr_len);
1309 		dpaa2_sg_set_final(sgt, data_left <= 0);
1310 
1311 		/* Compose the SG entries for each fragment of data */
1312 		num_sge = 1;
1313 		while (data_left > 0) {
1314 			int size;
1315 
1316 			/* Move to the next SG entry */
1317 			sgt++;
1318 			size = min_t(int, tso.size, data_left);
1319 
1320 			addr = dma_map_single(dev, tso.data, size, DMA_TO_DEVICE);
1321 			if (dma_mapping_error(dev, addr)) {
1322 				netdev_err(priv->net_dev, "dma_map_single(tso.data) failed\n");
1323 				err = -ENOMEM;
1324 				goto err_map_data;
1325 			}
1326 			dpaa2_sg_set_addr(sgt, addr);
1327 			dpaa2_sg_set_len(sgt, size);
1328 			dpaa2_sg_set_final(sgt, size == data_left);
1329 
1330 			num_sge++;
1331 
1332 			/* Build the data for the __next__ fragment */
1333 			data_left -= size;
1334 			tso_build_data(skb, &tso, size);
1335 		}
1336 
1337 		/* Store the skb backpointer in the SGT buffer */
1338 		sgt_buf_size = priv->tx_data_offset + num_sge * sizeof(struct dpaa2_sg_entry);
1339 		swa = (struct dpaa2_eth_swa *)sgt_buf;
1340 		swa->type = DPAA2_ETH_SWA_SW_TSO;
1341 		swa->tso.skb = skb;
1342 		swa->tso.num_sg = num_sge;
1343 		swa->tso.sgt_size = sgt_buf_size;
1344 		swa->tso.is_last_fd = total_len == 0 ? 1 : 0;
1345 
1346 		/* Separately map the SGT buffer */
1347 		sgt_addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
1348 		if (unlikely(dma_mapping_error(dev, sgt_addr))) {
1349 			netdev_err(priv->net_dev, "dma_map_single(sgt_buf) failed\n");
1350 			err = -ENOMEM;
1351 			goto err_map_sgt;
1352 		}
1353 
1354 		/* Setup the frame descriptor */
1355 		memset(fd, 0, sizeof(struct dpaa2_fd));
1356 		dpaa2_fd_set_offset(fd, priv->tx_data_offset);
1357 		dpaa2_fd_set_format(fd, dpaa2_fd_sg);
1358 		dpaa2_fd_set_addr(fd, sgt_addr);
1359 		dpaa2_fd_set_len(fd, fd_len);
1360 		dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
1361 
1362 		*total_fds_len += fd_len;
1363 		/* Advance to the next frame descriptor */
1364 		fd++;
1365 		index++;
1366 	}
1367 
1368 	*num_fds = index;
1369 
1370 	return 0;
1371 
1372 err_map_sgt:
1373 err_map_data:
1374 	/* Unmap all the data S/G entries for the current FD */
1375 	sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
1376 	for (i = 1; i < num_sge; i++)
1377 		dma_unmap_single(dev, dpaa2_sg_get_addr(&sgt[i]),
1378 				 dpaa2_sg_get_len(&sgt[i]), DMA_TO_DEVICE);
1379 
1380 	/* Unmap the header entry */
1381 	dma_unmap_single(dev, tso_hdr_dma, TSO_HEADER_SIZE, DMA_TO_DEVICE);
1382 err_map_tso_hdr:
1383 	kfree(tso_hdr);
1384 err_alloc_tso_hdr:
1385 	dpaa2_eth_sgt_recycle(priv, sgt_buf);
1386 err_sgt_get:
1387 	/* Free all the other FDs that were already fully created */
1388 	for (i = 0; i < index; i++)
1389 		dpaa2_eth_free_tx_fd(priv, NULL, NULL, &fd_start[i], false);
1390 
1391 	return err;
1392 }
1393 
1394 static netdev_tx_t __dpaa2_eth_tx(struct sk_buff *skb,
1395 				  struct net_device *net_dev)
1396 {
1397 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1398 	int total_enqueued = 0, retries = 0, enqueued;
1399 	struct dpaa2_eth_drv_stats *percpu_extras;
1400 	struct rtnl_link_stats64 *percpu_stats;
1401 	unsigned int needed_headroom;
1402 	int num_fds = 1, max_retries;
1403 	struct dpaa2_eth_fq *fq;
1404 	struct netdev_queue *nq;
1405 	struct dpaa2_fd *fd;
1406 	u16 queue_mapping;
1407 	void *swa = NULL;
1408 	u8 prio = 0;
1409 	int err, i;
1410 	u32 fd_len;
1411 
1412 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
1413 	percpu_extras = this_cpu_ptr(priv->percpu_extras);
1414 	fd = (this_cpu_ptr(priv->fd))->array;
1415 
1416 	needed_headroom = dpaa2_eth_needed_headroom(skb);
1417 
1418 	/* We'll be holding a back-reference to the skb until Tx Confirmation;
1419 	 * we don't want that overwritten by a concurrent Tx with a cloned skb.
1420 	 */
1421 	skb = skb_unshare(skb, GFP_ATOMIC);
1422 	if (unlikely(!skb)) {
1423 		/* skb_unshare() has already freed the skb */
1424 		percpu_stats->tx_dropped++;
1425 		return NETDEV_TX_OK;
1426 	}
1427 
1428 	/* Setup the FD fields */
1429 
1430 	if (skb_is_gso(skb)) {
1431 		err = dpaa2_eth_build_gso_fd(priv, skb, fd, &num_fds, &fd_len);
1432 		percpu_extras->tx_sg_frames += num_fds;
1433 		percpu_extras->tx_sg_bytes += fd_len;
1434 		percpu_extras->tx_tso_frames += num_fds;
1435 		percpu_extras->tx_tso_bytes += fd_len;
1436 	} else if (skb_is_nonlinear(skb)) {
1437 		err = dpaa2_eth_build_sg_fd(priv, skb, fd, &swa);
1438 		percpu_extras->tx_sg_frames++;
1439 		percpu_extras->tx_sg_bytes += skb->len;
1440 		fd_len = dpaa2_fd_get_len(fd);
1441 	} else if (skb_headroom(skb) < needed_headroom) {
1442 		err = dpaa2_eth_build_sg_fd_single_buf(priv, skb, fd, &swa);
1443 		percpu_extras->tx_sg_frames++;
1444 		percpu_extras->tx_sg_bytes += skb->len;
1445 		percpu_extras->tx_converted_sg_frames++;
1446 		percpu_extras->tx_converted_sg_bytes += skb->len;
1447 		fd_len = dpaa2_fd_get_len(fd);
1448 	} else {
1449 		err = dpaa2_eth_build_single_fd(priv, skb, fd, &swa);
1450 		fd_len = dpaa2_fd_get_len(fd);
1451 	}
1452 
1453 	if (unlikely(err)) {
1454 		percpu_stats->tx_dropped++;
1455 		goto err_build_fd;
1456 	}
1457 
1458 	if (swa && skb->cb[0])
1459 		dpaa2_eth_enable_tx_tstamp(priv, fd, swa, skb);
1460 
1461 	/* Tracing point */
1462 	for (i = 0; i < num_fds; i++)
1463 		trace_dpaa2_tx_fd(net_dev, &fd[i]);
1464 
1465 	/* TxConf FQ selection relies on queue id from the stack.
1466 	 * In case of a forwarded frame from another DPNI interface, we choose
1467 	 * a queue affined to the same core that processed the Rx frame
1468 	 */
1469 	queue_mapping = skb_get_queue_mapping(skb);
1470 
1471 	if (net_dev->num_tc) {
1472 		prio = netdev_txq_to_tc(net_dev, queue_mapping);
1473 		/* Hardware interprets priority level 0 as being the highest,
1474 		 * so we need to do a reverse mapping to the netdev tc index
1475 		 */
1476 		prio = net_dev->num_tc - prio - 1;
1477 		/* We have only one FQ array entry for all Tx hardware queues
1478 		 * with the same flow id (but different priority levels)
1479 		 */
1480 		queue_mapping %= dpaa2_eth_queue_count(priv);
1481 	}
1482 	fq = &priv->fq[queue_mapping];
1483 	nq = netdev_get_tx_queue(net_dev, queue_mapping);
1484 	netdev_tx_sent_queue(nq, fd_len);
1485 
1486 	/* Everything that happens after this enqueues might race with
1487 	 * the Tx confirmation callback for this frame
1488 	 */
1489 	max_retries = num_fds * DPAA2_ETH_ENQUEUE_RETRIES;
1490 	while (total_enqueued < num_fds && retries < max_retries) {
1491 		err = priv->enqueue(priv, fq, &fd[total_enqueued],
1492 				    prio, num_fds - total_enqueued, &enqueued);
1493 		if (err == -EBUSY) {
1494 			retries++;
1495 			continue;
1496 		}
1497 
1498 		total_enqueued += enqueued;
1499 	}
1500 	percpu_extras->tx_portal_busy += retries;
1501 
1502 	if (unlikely(err < 0)) {
1503 		percpu_stats->tx_errors++;
1504 		/* Clean up everything, including freeing the skb */
1505 		dpaa2_eth_free_tx_fd(priv, NULL, fq, fd, false);
1506 		netdev_tx_completed_queue(nq, 1, fd_len);
1507 	} else {
1508 		percpu_stats->tx_packets += total_enqueued;
1509 		percpu_stats->tx_bytes += fd_len;
1510 	}
1511 
1512 	return NETDEV_TX_OK;
1513 
1514 err_build_fd:
1515 	dev_kfree_skb(skb);
1516 
1517 	return NETDEV_TX_OK;
1518 }
1519 
1520 static void dpaa2_eth_tx_onestep_tstamp(struct work_struct *work)
1521 {
1522 	struct dpaa2_eth_priv *priv = container_of(work, struct dpaa2_eth_priv,
1523 						   tx_onestep_tstamp);
1524 	struct sk_buff *skb;
1525 
1526 	while (true) {
1527 		skb = skb_dequeue(&priv->tx_skbs);
1528 		if (!skb)
1529 			return;
1530 
1531 		/* Lock just before TX one-step timestamping packet,
1532 		 * and release the lock in dpaa2_eth_free_tx_fd when
1533 		 * confirm the packet has been sent on hardware, or
1534 		 * when clean up during transmit failure.
1535 		 */
1536 		mutex_lock(&priv->onestep_tstamp_lock);
1537 		__dpaa2_eth_tx(skb, priv->net_dev);
1538 	}
1539 }
1540 
1541 static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev)
1542 {
1543 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1544 	u8 msgtype, twostep, udp;
1545 	u16 offset1, offset2;
1546 
1547 	/* Utilize skb->cb[0] for timestamping request per skb */
1548 	skb->cb[0] = 0;
1549 
1550 	if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && dpaa2_ptp) {
1551 		if (priv->tx_tstamp_type == HWTSTAMP_TX_ON)
1552 			skb->cb[0] = TX_TSTAMP;
1553 		else if (priv->tx_tstamp_type == HWTSTAMP_TX_ONESTEP_SYNC)
1554 			skb->cb[0] = TX_TSTAMP_ONESTEP_SYNC;
1555 	}
1556 
1557 	/* TX for one-step timestamping PTP Sync packet */
1558 	if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
1559 		if (!dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
1560 					 &offset1, &offset2))
1561 			if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0) {
1562 				skb_queue_tail(&priv->tx_skbs, skb);
1563 				queue_work(priv->dpaa2_ptp_wq,
1564 					   &priv->tx_onestep_tstamp);
1565 				return NETDEV_TX_OK;
1566 			}
1567 		/* Use two-step timestamping if not one-step timestamping
1568 		 * PTP Sync packet
1569 		 */
1570 		skb->cb[0] = TX_TSTAMP;
1571 	}
1572 
1573 	/* TX for other packets */
1574 	return __dpaa2_eth_tx(skb, net_dev);
1575 }
1576 
1577 /* Tx confirmation frame processing routine */
1578 static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
1579 			      struct dpaa2_eth_channel *ch,
1580 			      const struct dpaa2_fd *fd,
1581 			      struct dpaa2_eth_fq *fq)
1582 {
1583 	struct rtnl_link_stats64 *percpu_stats;
1584 	struct dpaa2_eth_drv_stats *percpu_extras;
1585 	u32 fd_len = dpaa2_fd_get_len(fd);
1586 	u32 fd_errors;
1587 
1588 	/* Tracing point */
1589 	trace_dpaa2_tx_conf_fd(priv->net_dev, fd);
1590 
1591 	percpu_extras = this_cpu_ptr(priv->percpu_extras);
1592 	percpu_extras->tx_conf_frames++;
1593 	percpu_extras->tx_conf_bytes += fd_len;
1594 	ch->stats.bytes_per_cdan += fd_len;
1595 
1596 	/* Check frame errors in the FD field */
1597 	fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK;
1598 	dpaa2_eth_free_tx_fd(priv, ch, fq, fd, true);
1599 
1600 	if (likely(!fd_errors))
1601 		return;
1602 
1603 	if (net_ratelimit())
1604 		netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n",
1605 			   fd_errors);
1606 
1607 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
1608 	/* Tx-conf logically pertains to the egress path. */
1609 	percpu_stats->tx_errors++;
1610 }
1611 
1612 static int dpaa2_eth_set_rx_vlan_filtering(struct dpaa2_eth_priv *priv,
1613 					   bool enable)
1614 {
1615 	int err;
1616 
1617 	err = dpni_enable_vlan_filter(priv->mc_io, 0, priv->mc_token, enable);
1618 
1619 	if (err) {
1620 		netdev_err(priv->net_dev,
1621 			   "dpni_enable_vlan_filter failed\n");
1622 		return err;
1623 	}
1624 
1625 	return 0;
1626 }
1627 
1628 static int dpaa2_eth_set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
1629 {
1630 	int err;
1631 
1632 	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1633 			       DPNI_OFF_RX_L3_CSUM, enable);
1634 	if (err) {
1635 		netdev_err(priv->net_dev,
1636 			   "dpni_set_offload(RX_L3_CSUM) failed\n");
1637 		return err;
1638 	}
1639 
1640 	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1641 			       DPNI_OFF_RX_L4_CSUM, enable);
1642 	if (err) {
1643 		netdev_err(priv->net_dev,
1644 			   "dpni_set_offload(RX_L4_CSUM) failed\n");
1645 		return err;
1646 	}
1647 
1648 	return 0;
1649 }
1650 
1651 static int dpaa2_eth_set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
1652 {
1653 	int err;
1654 
1655 	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1656 			       DPNI_OFF_TX_L3_CSUM, enable);
1657 	if (err) {
1658 		netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
1659 		return err;
1660 	}
1661 
1662 	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1663 			       DPNI_OFF_TX_L4_CSUM, enable);
1664 	if (err) {
1665 		netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
1666 		return err;
1667 	}
1668 
1669 	return 0;
1670 }
1671 
1672 /* Perform a single release command to add buffers
1673  * to the specified buffer pool
1674  */
1675 static int dpaa2_eth_add_bufs(struct dpaa2_eth_priv *priv,
1676 			      struct dpaa2_eth_channel *ch)
1677 {
1678 	struct xdp_buff *xdp_buffs[DPAA2_ETH_BUFS_PER_CMD];
1679 	struct device *dev = priv->net_dev->dev.parent;
1680 	u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
1681 	struct dpaa2_eth_swa *swa;
1682 	struct page *page;
1683 	dma_addr_t addr;
1684 	int retries = 0;
1685 	int i = 0, err;
1686 	u32 batch;
1687 
1688 	/* Allocate buffers visible to WRIOP */
1689 	if (!ch->xsk_zc) {
1690 		for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
1691 			/* Also allocate skb shared info and alignment padding.
1692 			 * There is one page for each Rx buffer. WRIOP sees
1693 			 * the entire page except for a tailroom reserved for
1694 			 * skb shared info
1695 			 */
1696 			page = dev_alloc_pages(0);
1697 			if (!page)
1698 				goto err_alloc;
1699 
1700 			addr = dma_map_page(dev, page, 0, priv->rx_buf_size,
1701 					    DMA_BIDIRECTIONAL);
1702 			if (unlikely(dma_mapping_error(dev, addr)))
1703 				goto err_map;
1704 
1705 			buf_array[i] = addr;
1706 
1707 			/* tracing point */
1708 			trace_dpaa2_eth_buf_seed(priv->net_dev,
1709 						 page_address(page),
1710 						 DPAA2_ETH_RX_BUF_RAW_SIZE,
1711 						 addr, priv->rx_buf_size,
1712 						 ch->bp->bpid);
1713 		}
1714 	} else if (xsk_buff_can_alloc(ch->xsk_pool, DPAA2_ETH_BUFS_PER_CMD)) {
1715 		/* Allocate XSK buffers for AF_XDP fast path in batches
1716 		 * of DPAA2_ETH_BUFS_PER_CMD. Bail out if the UMEM cannot
1717 		 * provide enough buffers at the moment
1718 		 */
1719 		batch = xsk_buff_alloc_batch(ch->xsk_pool, xdp_buffs,
1720 					     DPAA2_ETH_BUFS_PER_CMD);
1721 		if (!batch)
1722 			goto err_alloc;
1723 
1724 		for (i = 0; i < batch; i++) {
1725 			swa = (struct dpaa2_eth_swa *)(xdp_buffs[i]->data_hard_start +
1726 						       DPAA2_ETH_RX_HWA_SIZE);
1727 			swa->xsk.xdp_buff = xdp_buffs[i];
1728 
1729 			addr = xsk_buff_xdp_get_frame_dma(xdp_buffs[i]);
1730 			if (unlikely(dma_mapping_error(dev, addr)))
1731 				goto err_map;
1732 
1733 			buf_array[i] = addr;
1734 
1735 			trace_dpaa2_xsk_buf_seed(priv->net_dev,
1736 						 xdp_buffs[i]->data_hard_start,
1737 						 DPAA2_ETH_RX_BUF_RAW_SIZE,
1738 						 addr, priv->rx_buf_size,
1739 						 ch->bp->bpid);
1740 		}
1741 	}
1742 
1743 release_bufs:
1744 	/* In case the portal is busy, retry until successful */
1745 	while ((err = dpaa2_io_service_release(ch->dpio, ch->bp->bpid,
1746 					       buf_array, i)) == -EBUSY) {
1747 		if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
1748 			break;
1749 		cpu_relax();
1750 	}
1751 
1752 	/* If release command failed, clean up and bail out;
1753 	 * not much else we can do about it
1754 	 */
1755 	if (err) {
1756 		dpaa2_eth_free_bufs(priv, buf_array, i, ch->xsk_zc);
1757 		return 0;
1758 	}
1759 
1760 	return i;
1761 
1762 err_map:
1763 	if (!ch->xsk_zc) {
1764 		__free_pages(page, 0);
1765 	} else {
1766 		for (; i < batch; i++)
1767 			xsk_buff_free(xdp_buffs[i]);
1768 	}
1769 err_alloc:
1770 	/* If we managed to allocate at least some buffers,
1771 	 * release them to hardware
1772 	 */
1773 	if (i)
1774 		goto release_bufs;
1775 
1776 	return 0;
1777 }
1778 
1779 static int dpaa2_eth_seed_pool(struct dpaa2_eth_priv *priv,
1780 			       struct dpaa2_eth_channel *ch)
1781 {
1782 	int i;
1783 	int new_count;
1784 
1785 	for (i = 0; i < DPAA2_ETH_NUM_BUFS; i += DPAA2_ETH_BUFS_PER_CMD) {
1786 		new_count = dpaa2_eth_add_bufs(priv, ch);
1787 		ch->buf_count += new_count;
1788 
1789 		if (new_count < DPAA2_ETH_BUFS_PER_CMD)
1790 			return -ENOMEM;
1791 	}
1792 
1793 	return 0;
1794 }
1795 
1796 static void dpaa2_eth_seed_pools(struct dpaa2_eth_priv *priv)
1797 {
1798 	struct net_device *net_dev = priv->net_dev;
1799 	struct dpaa2_eth_channel *channel;
1800 	int i, err = 0;
1801 
1802 	for (i = 0; i < priv->num_channels; i++) {
1803 		channel = priv->channel[i];
1804 
1805 		err = dpaa2_eth_seed_pool(priv, channel);
1806 
1807 		/* Not much to do; the buffer pool, though not filled up,
1808 		 * may still contain some buffers which would enable us
1809 		 * to limp on.
1810 		 */
1811 		if (err)
1812 			netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
1813 				   channel->bp->dev->obj_desc.id,
1814 				   channel->bp->bpid);
1815 	}
1816 }
1817 
1818 /*
1819  * Drain the specified number of buffers from one of the DPNI's private buffer
1820  * pools.
1821  * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
1822  */
1823 static void dpaa2_eth_drain_bufs(struct dpaa2_eth_priv *priv, int bpid,
1824 				 int count)
1825 {
1826 	u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
1827 	bool xsk_zc = false;
1828 	int retries = 0;
1829 	int i, ret;
1830 
1831 	for (i = 0; i < priv->num_channels; i++)
1832 		if (priv->channel[i]->bp->bpid == bpid)
1833 			xsk_zc = priv->channel[i]->xsk_zc;
1834 
1835 	do {
1836 		ret = dpaa2_io_service_acquire(NULL, bpid, buf_array, count);
1837 		if (ret < 0) {
1838 			if (ret == -EBUSY &&
1839 			    retries++ < DPAA2_ETH_SWP_BUSY_RETRIES)
1840 				continue;
1841 			netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
1842 			return;
1843 		}
1844 		dpaa2_eth_free_bufs(priv, buf_array, ret, xsk_zc);
1845 		retries = 0;
1846 	} while (ret);
1847 }
1848 
1849 static void dpaa2_eth_drain_pool(struct dpaa2_eth_priv *priv, int bpid)
1850 {
1851 	int i;
1852 
1853 	/* Drain the buffer pool */
1854 	dpaa2_eth_drain_bufs(priv, bpid, DPAA2_ETH_BUFS_PER_CMD);
1855 	dpaa2_eth_drain_bufs(priv, bpid, 1);
1856 
1857 	/* Setup to zero the buffer count of all channels which were
1858 	 * using this buffer pool.
1859 	 */
1860 	for (i = 0; i < priv->num_channels; i++)
1861 		if (priv->channel[i]->bp->bpid == bpid)
1862 			priv->channel[i]->buf_count = 0;
1863 }
1864 
1865 static void dpaa2_eth_drain_pools(struct dpaa2_eth_priv *priv)
1866 {
1867 	int i;
1868 
1869 	for (i = 0; i < priv->num_bps; i++)
1870 		dpaa2_eth_drain_pool(priv, priv->bp[i]->bpid);
1871 }
1872 
1873 /* Function is called from softirq context only, so we don't need to guard
1874  * the access to percpu count
1875  */
1876 static int dpaa2_eth_refill_pool(struct dpaa2_eth_priv *priv,
1877 				 struct dpaa2_eth_channel *ch)
1878 {
1879 	int new_count;
1880 
1881 	if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
1882 		return 0;
1883 
1884 	do {
1885 		new_count = dpaa2_eth_add_bufs(priv, ch);
1886 		if (unlikely(!new_count)) {
1887 			/* Out of memory; abort for now, we'll try later on */
1888 			break;
1889 		}
1890 		ch->buf_count += new_count;
1891 	} while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
1892 
1893 	if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
1894 		return -ENOMEM;
1895 
1896 	return 0;
1897 }
1898 
1899 static void dpaa2_eth_sgt_cache_drain(struct dpaa2_eth_priv *priv)
1900 {
1901 	struct dpaa2_eth_sgt_cache *sgt_cache;
1902 	u16 count;
1903 	int k, i;
1904 
1905 	for_each_possible_cpu(k) {
1906 		sgt_cache = per_cpu_ptr(priv->sgt_cache, k);
1907 		count = sgt_cache->count;
1908 
1909 		for (i = 0; i < count; i++)
1910 			skb_free_frag(sgt_cache->buf[i]);
1911 		sgt_cache->count = 0;
1912 	}
1913 }
1914 
1915 static int dpaa2_eth_pull_channel(struct dpaa2_eth_channel *ch)
1916 {
1917 	int err;
1918 	int dequeues = -1;
1919 
1920 	/* Retry while portal is busy */
1921 	do {
1922 		err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
1923 						    ch->store);
1924 		dequeues++;
1925 		cpu_relax();
1926 	} while (err == -EBUSY && dequeues < DPAA2_ETH_SWP_BUSY_RETRIES);
1927 
1928 	ch->stats.dequeue_portal_busy += dequeues;
1929 	if (unlikely(err))
1930 		ch->stats.pull_err++;
1931 
1932 	return err;
1933 }
1934 
1935 /* NAPI poll routine
1936  *
1937  * Frames are dequeued from the QMan channel associated with this NAPI context.
1938  * Rx, Tx confirmation and (if configured) Rx error frames all count
1939  * towards the NAPI budget.
1940  */
1941 static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
1942 {
1943 	struct dpaa2_eth_channel *ch;
1944 	struct dpaa2_eth_priv *priv;
1945 	int rx_cleaned = 0, txconf_cleaned = 0;
1946 	struct dpaa2_eth_fq *fq, *txc_fq = NULL;
1947 	struct netdev_queue *nq;
1948 	int store_cleaned, work_done;
1949 	bool work_done_zc = false;
1950 	struct list_head rx_list;
1951 	int retries = 0;
1952 	u16 flowid;
1953 	int err;
1954 
1955 	ch = container_of(napi, struct dpaa2_eth_channel, napi);
1956 	ch->xdp.res = 0;
1957 	priv = ch->priv;
1958 
1959 	INIT_LIST_HEAD(&rx_list);
1960 	ch->rx_list = &rx_list;
1961 
1962 	if (ch->xsk_zc) {
1963 		work_done_zc = dpaa2_xsk_tx(priv, ch);
1964 		/* If we reached the XSK Tx per NAPI threshold, we're done */
1965 		if (work_done_zc) {
1966 			work_done = budget;
1967 			goto out;
1968 		}
1969 	}
1970 
1971 	do {
1972 		err = dpaa2_eth_pull_channel(ch);
1973 		if (unlikely(err))
1974 			break;
1975 
1976 		/* Refill pool if appropriate */
1977 		dpaa2_eth_refill_pool(priv, ch);
1978 
1979 		store_cleaned = dpaa2_eth_consume_frames(ch, &fq);
1980 		if (store_cleaned <= 0)
1981 			break;
1982 		if (fq->type == DPAA2_RX_FQ) {
1983 			rx_cleaned += store_cleaned;
1984 			flowid = fq->flowid;
1985 		} else {
1986 			txconf_cleaned += store_cleaned;
1987 			/* We have a single Tx conf FQ on this channel */
1988 			txc_fq = fq;
1989 		}
1990 
1991 		/* If we either consumed the whole NAPI budget with Rx frames
1992 		 * or we reached the Tx confirmations threshold, we're done.
1993 		 */
1994 		if (rx_cleaned >= budget ||
1995 		    txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
1996 			work_done = budget;
1997 			goto out;
1998 		}
1999 	} while (store_cleaned);
2000 
2001 	/* Update NET DIM with the values for this CDAN */
2002 	dpaa2_io_update_net_dim(ch->dpio, ch->stats.frames_per_cdan,
2003 				ch->stats.bytes_per_cdan);
2004 	ch->stats.frames_per_cdan = 0;
2005 	ch->stats.bytes_per_cdan = 0;
2006 
2007 	/* We didn't consume the entire budget, so finish napi and
2008 	 * re-enable data availability notifications
2009 	 */
2010 	napi_complete_done(napi, rx_cleaned);
2011 	do {
2012 		err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
2013 		cpu_relax();
2014 	} while (err == -EBUSY && retries++ < DPAA2_ETH_SWP_BUSY_RETRIES);
2015 	WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
2016 		  ch->nctx.desired_cpu);
2017 
2018 	work_done = max(rx_cleaned, 1);
2019 
2020 out:
2021 	netif_receive_skb_list(ch->rx_list);
2022 
2023 	if (ch->xsk_tx_pkts_sent) {
2024 		xsk_tx_completed(ch->xsk_pool, ch->xsk_tx_pkts_sent);
2025 		ch->xsk_tx_pkts_sent = 0;
2026 	}
2027 
2028 	if (txc_fq && txc_fq->dq_frames) {
2029 		nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid);
2030 		netdev_tx_completed_queue(nq, txc_fq->dq_frames,
2031 					  txc_fq->dq_bytes);
2032 		txc_fq->dq_frames = 0;
2033 		txc_fq->dq_bytes = 0;
2034 	}
2035 
2036 	if (ch->xdp.res & XDP_REDIRECT)
2037 		xdp_do_flush_map();
2038 	else if (rx_cleaned && ch->xdp.res & XDP_TX)
2039 		dpaa2_eth_xdp_tx_flush(priv, ch, &priv->fq[flowid]);
2040 
2041 	return work_done;
2042 }
2043 
2044 static void dpaa2_eth_enable_ch_napi(struct dpaa2_eth_priv *priv)
2045 {
2046 	struct dpaa2_eth_channel *ch;
2047 	int i;
2048 
2049 	for (i = 0; i < priv->num_channels; i++) {
2050 		ch = priv->channel[i];
2051 		napi_enable(&ch->napi);
2052 	}
2053 }
2054 
2055 static void dpaa2_eth_disable_ch_napi(struct dpaa2_eth_priv *priv)
2056 {
2057 	struct dpaa2_eth_channel *ch;
2058 	int i;
2059 
2060 	for (i = 0; i < priv->num_channels; i++) {
2061 		ch = priv->channel[i];
2062 		napi_disable(&ch->napi);
2063 	}
2064 }
2065 
2066 void dpaa2_eth_set_rx_taildrop(struct dpaa2_eth_priv *priv,
2067 			       bool tx_pause, bool pfc)
2068 {
2069 	struct dpni_taildrop td = {0};
2070 	struct dpaa2_eth_fq *fq;
2071 	int i, err;
2072 
2073 	/* FQ taildrop: threshold is in bytes, per frame queue. Enabled if
2074 	 * flow control is disabled (as it might interfere with either the
2075 	 * buffer pool depletion trigger for pause frames or with the group
2076 	 * congestion trigger for PFC frames)
2077 	 */
2078 	td.enable = !tx_pause;
2079 	if (priv->rx_fqtd_enabled == td.enable)
2080 		goto set_cgtd;
2081 
2082 	td.threshold = DPAA2_ETH_FQ_TAILDROP_THRESH;
2083 	td.units = DPNI_CONGESTION_UNIT_BYTES;
2084 
2085 	for (i = 0; i < priv->num_fqs; i++) {
2086 		fq = &priv->fq[i];
2087 		if (fq->type != DPAA2_RX_FQ)
2088 			continue;
2089 		err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
2090 					DPNI_CP_QUEUE, DPNI_QUEUE_RX,
2091 					fq->tc, fq->flowid, &td);
2092 		if (err) {
2093 			netdev_err(priv->net_dev,
2094 				   "dpni_set_taildrop(FQ) failed\n");
2095 			return;
2096 		}
2097 	}
2098 
2099 	priv->rx_fqtd_enabled = td.enable;
2100 
2101 set_cgtd:
2102 	/* Congestion group taildrop: threshold is in frames, per group
2103 	 * of FQs belonging to the same traffic class
2104 	 * Enabled if general Tx pause disabled or if PFCs are enabled
2105 	 * (congestion group threhsold for PFC generation is lower than the
2106 	 * CG taildrop threshold, so it won't interfere with it; we also
2107 	 * want frames in non-PFC enabled traffic classes to be kept in check)
2108 	 */
2109 	td.enable = !tx_pause || pfc;
2110 	if (priv->rx_cgtd_enabled == td.enable)
2111 		return;
2112 
2113 	td.threshold = DPAA2_ETH_CG_TAILDROP_THRESH(priv);
2114 	td.units = DPNI_CONGESTION_UNIT_FRAMES;
2115 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
2116 		err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
2117 					DPNI_CP_GROUP, DPNI_QUEUE_RX,
2118 					i, 0, &td);
2119 		if (err) {
2120 			netdev_err(priv->net_dev,
2121 				   "dpni_set_taildrop(CG) failed\n");
2122 			return;
2123 		}
2124 	}
2125 
2126 	priv->rx_cgtd_enabled = td.enable;
2127 }
2128 
2129 static int dpaa2_eth_link_state_update(struct dpaa2_eth_priv *priv)
2130 {
2131 	struct dpni_link_state state = {0};
2132 	bool tx_pause;
2133 	int err;
2134 
2135 	err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
2136 	if (unlikely(err)) {
2137 		netdev_err(priv->net_dev,
2138 			   "dpni_get_link_state() failed\n");
2139 		return err;
2140 	}
2141 
2142 	/* If Tx pause frame settings have changed, we need to update
2143 	 * Rx FQ taildrop configuration as well. We configure taildrop
2144 	 * only when pause frame generation is disabled.
2145 	 */
2146 	tx_pause = dpaa2_eth_tx_pause_enabled(state.options);
2147 	dpaa2_eth_set_rx_taildrop(priv, tx_pause, priv->pfc_enabled);
2148 
2149 	/* When we manage the MAC/PHY using phylink there is no need
2150 	 * to manually update the netif_carrier.
2151 	 */
2152 	if (dpaa2_eth_is_type_phy(priv))
2153 		goto out;
2154 
2155 	/* Chech link state; speed / duplex changes are not treated yet */
2156 	if (priv->link_state.up == state.up)
2157 		goto out;
2158 
2159 	if (state.up) {
2160 		netif_carrier_on(priv->net_dev);
2161 		netif_tx_start_all_queues(priv->net_dev);
2162 	} else {
2163 		netif_tx_stop_all_queues(priv->net_dev);
2164 		netif_carrier_off(priv->net_dev);
2165 	}
2166 
2167 	netdev_info(priv->net_dev, "Link Event: state %s\n",
2168 		    state.up ? "up" : "down");
2169 
2170 out:
2171 	priv->link_state = state;
2172 
2173 	return 0;
2174 }
2175 
2176 static int dpaa2_eth_open(struct net_device *net_dev)
2177 {
2178 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2179 	int err;
2180 
2181 	dpaa2_eth_seed_pools(priv);
2182 
2183 	if (!dpaa2_eth_is_type_phy(priv)) {
2184 		/* We'll only start the txqs when the link is actually ready;
2185 		 * make sure we don't race against the link up notification,
2186 		 * which may come immediately after dpni_enable();
2187 		 */
2188 		netif_tx_stop_all_queues(net_dev);
2189 
2190 		/* Also, explicitly set carrier off, otherwise
2191 		 * netif_carrier_ok() will return true and cause 'ip link show'
2192 		 * to report the LOWER_UP flag, even though the link
2193 		 * notification wasn't even received.
2194 		 */
2195 		netif_carrier_off(net_dev);
2196 	}
2197 	dpaa2_eth_enable_ch_napi(priv);
2198 
2199 	err = dpni_enable(priv->mc_io, 0, priv->mc_token);
2200 	if (err < 0) {
2201 		netdev_err(net_dev, "dpni_enable() failed\n");
2202 		goto enable_err;
2203 	}
2204 
2205 	if (dpaa2_eth_is_type_phy(priv)) {
2206 		dpaa2_mac_start(priv->mac);
2207 		phylink_start(priv->mac->phylink);
2208 	}
2209 
2210 	return 0;
2211 
2212 enable_err:
2213 	dpaa2_eth_disable_ch_napi(priv);
2214 	dpaa2_eth_drain_pools(priv);
2215 	return err;
2216 }
2217 
2218 /* Total number of in-flight frames on ingress queues */
2219 static u32 dpaa2_eth_ingress_fq_count(struct dpaa2_eth_priv *priv)
2220 {
2221 	struct dpaa2_eth_fq *fq;
2222 	u32 fcnt = 0, bcnt = 0, total = 0;
2223 	int i, err;
2224 
2225 	for (i = 0; i < priv->num_fqs; i++) {
2226 		fq = &priv->fq[i];
2227 		err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
2228 		if (err) {
2229 			netdev_warn(priv->net_dev, "query_fq_count failed");
2230 			break;
2231 		}
2232 		total += fcnt;
2233 	}
2234 
2235 	return total;
2236 }
2237 
2238 static void dpaa2_eth_wait_for_ingress_fq_empty(struct dpaa2_eth_priv *priv)
2239 {
2240 	int retries = 10;
2241 	u32 pending;
2242 
2243 	do {
2244 		pending = dpaa2_eth_ingress_fq_count(priv);
2245 		if (pending)
2246 			msleep(100);
2247 	} while (pending && --retries);
2248 }
2249 
2250 #define DPNI_TX_PENDING_VER_MAJOR	7
2251 #define DPNI_TX_PENDING_VER_MINOR	13
2252 static void dpaa2_eth_wait_for_egress_fq_empty(struct dpaa2_eth_priv *priv)
2253 {
2254 	union dpni_statistics stats;
2255 	int retries = 10;
2256 	int err;
2257 
2258 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_TX_PENDING_VER_MAJOR,
2259 				   DPNI_TX_PENDING_VER_MINOR) < 0)
2260 		goto out;
2261 
2262 	do {
2263 		err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token, 6,
2264 					  &stats);
2265 		if (err)
2266 			goto out;
2267 		if (stats.page_6.tx_pending_frames == 0)
2268 			return;
2269 	} while (--retries);
2270 
2271 out:
2272 	msleep(500);
2273 }
2274 
2275 static int dpaa2_eth_stop(struct net_device *net_dev)
2276 {
2277 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2278 	int dpni_enabled = 0;
2279 	int retries = 10;
2280 
2281 	if (dpaa2_eth_is_type_phy(priv)) {
2282 		phylink_stop(priv->mac->phylink);
2283 		dpaa2_mac_stop(priv->mac);
2284 	} else {
2285 		netif_tx_stop_all_queues(net_dev);
2286 		netif_carrier_off(net_dev);
2287 	}
2288 
2289 	/* On dpni_disable(), the MC firmware will:
2290 	 * - stop MAC Rx and wait for all Rx frames to be enqueued to software
2291 	 * - cut off WRIOP dequeues from egress FQs and wait until transmission
2292 	 * of all in flight Tx frames is finished (and corresponding Tx conf
2293 	 * frames are enqueued back to software)
2294 	 *
2295 	 * Before calling dpni_disable(), we wait for all Tx frames to arrive
2296 	 * on WRIOP. After it finishes, wait until all remaining frames on Rx
2297 	 * and Tx conf queues are consumed on NAPI poll.
2298 	 */
2299 	dpaa2_eth_wait_for_egress_fq_empty(priv);
2300 
2301 	do {
2302 		dpni_disable(priv->mc_io, 0, priv->mc_token);
2303 		dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
2304 		if (dpni_enabled)
2305 			/* Allow the hardware some slack */
2306 			msleep(100);
2307 	} while (dpni_enabled && --retries);
2308 	if (!retries) {
2309 		netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
2310 		/* Must go on and disable NAPI nonetheless, so we don't crash at
2311 		 * the next "ifconfig up"
2312 		 */
2313 	}
2314 
2315 	dpaa2_eth_wait_for_ingress_fq_empty(priv);
2316 	dpaa2_eth_disable_ch_napi(priv);
2317 
2318 	/* Empty the buffer pool */
2319 	dpaa2_eth_drain_pools(priv);
2320 
2321 	/* Empty the Scatter-Gather Buffer cache */
2322 	dpaa2_eth_sgt_cache_drain(priv);
2323 
2324 	return 0;
2325 }
2326 
2327 static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
2328 {
2329 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2330 	struct device *dev = net_dev->dev.parent;
2331 	int err;
2332 
2333 	err = eth_mac_addr(net_dev, addr);
2334 	if (err < 0) {
2335 		dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
2336 		return err;
2337 	}
2338 
2339 	err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
2340 					net_dev->dev_addr);
2341 	if (err) {
2342 		dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
2343 		return err;
2344 	}
2345 
2346 	return 0;
2347 }
2348 
2349 /** Fill in counters maintained by the GPP driver. These may be different from
2350  * the hardware counters obtained by ethtool.
2351  */
2352 static void dpaa2_eth_get_stats(struct net_device *net_dev,
2353 				struct rtnl_link_stats64 *stats)
2354 {
2355 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2356 	struct rtnl_link_stats64 *percpu_stats;
2357 	u64 *cpustats;
2358 	u64 *netstats = (u64 *)stats;
2359 	int i, j;
2360 	int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
2361 
2362 	for_each_possible_cpu(i) {
2363 		percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
2364 		cpustats = (u64 *)percpu_stats;
2365 		for (j = 0; j < num; j++)
2366 			netstats[j] += cpustats[j];
2367 	}
2368 }
2369 
2370 /* Copy mac unicast addresses from @net_dev to @priv.
2371  * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
2372  */
2373 static void dpaa2_eth_add_uc_hw_addr(const struct net_device *net_dev,
2374 				     struct dpaa2_eth_priv *priv)
2375 {
2376 	struct netdev_hw_addr *ha;
2377 	int err;
2378 
2379 	netdev_for_each_uc_addr(ha, net_dev) {
2380 		err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
2381 					ha->addr);
2382 		if (err)
2383 			netdev_warn(priv->net_dev,
2384 				    "Could not add ucast MAC %pM to the filtering table (err %d)\n",
2385 				    ha->addr, err);
2386 	}
2387 }
2388 
2389 /* Copy mac multicast addresses from @net_dev to @priv
2390  * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
2391  */
2392 static void dpaa2_eth_add_mc_hw_addr(const struct net_device *net_dev,
2393 				     struct dpaa2_eth_priv *priv)
2394 {
2395 	struct netdev_hw_addr *ha;
2396 	int err;
2397 
2398 	netdev_for_each_mc_addr(ha, net_dev) {
2399 		err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
2400 					ha->addr);
2401 		if (err)
2402 			netdev_warn(priv->net_dev,
2403 				    "Could not add mcast MAC %pM to the filtering table (err %d)\n",
2404 				    ha->addr, err);
2405 	}
2406 }
2407 
2408 static int dpaa2_eth_rx_add_vid(struct net_device *net_dev,
2409 				__be16 vlan_proto, u16 vid)
2410 {
2411 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2412 	int err;
2413 
2414 	err = dpni_add_vlan_id(priv->mc_io, 0, priv->mc_token,
2415 			       vid, 0, 0, 0);
2416 
2417 	if (err) {
2418 		netdev_warn(priv->net_dev,
2419 			    "Could not add the vlan id %u\n",
2420 			    vid);
2421 		return err;
2422 	}
2423 
2424 	return 0;
2425 }
2426 
2427 static int dpaa2_eth_rx_kill_vid(struct net_device *net_dev,
2428 				 __be16 vlan_proto, u16 vid)
2429 {
2430 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2431 	int err;
2432 
2433 	err = dpni_remove_vlan_id(priv->mc_io, 0, priv->mc_token, vid);
2434 
2435 	if (err) {
2436 		netdev_warn(priv->net_dev,
2437 			    "Could not remove the vlan id %u\n",
2438 			    vid);
2439 		return err;
2440 	}
2441 
2442 	return 0;
2443 }
2444 
2445 static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
2446 {
2447 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2448 	int uc_count = netdev_uc_count(net_dev);
2449 	int mc_count = netdev_mc_count(net_dev);
2450 	u8 max_mac = priv->dpni_attrs.mac_filter_entries;
2451 	u32 options = priv->dpni_attrs.options;
2452 	u16 mc_token = priv->mc_token;
2453 	struct fsl_mc_io *mc_io = priv->mc_io;
2454 	int err;
2455 
2456 	/* Basic sanity checks; these probably indicate a misconfiguration */
2457 	if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
2458 		netdev_info(net_dev,
2459 			    "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
2460 			    max_mac);
2461 
2462 	/* Force promiscuous if the uc or mc counts exceed our capabilities. */
2463 	if (uc_count > max_mac) {
2464 		netdev_info(net_dev,
2465 			    "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
2466 			    uc_count, max_mac);
2467 		goto force_promisc;
2468 	}
2469 	if (mc_count + uc_count > max_mac) {
2470 		netdev_info(net_dev,
2471 			    "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
2472 			    uc_count + mc_count, max_mac);
2473 		goto force_mc_promisc;
2474 	}
2475 
2476 	/* Adjust promisc settings due to flag combinations */
2477 	if (net_dev->flags & IFF_PROMISC)
2478 		goto force_promisc;
2479 	if (net_dev->flags & IFF_ALLMULTI) {
2480 		/* First, rebuild unicast filtering table. This should be done
2481 		 * in promisc mode, in order to avoid frame loss while we
2482 		 * progressively add entries to the table.
2483 		 * We don't know whether we had been in promisc already, and
2484 		 * making an MC call to find out is expensive; so set uc promisc
2485 		 * nonetheless.
2486 		 */
2487 		err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2488 		if (err)
2489 			netdev_warn(net_dev, "Can't set uc promisc\n");
2490 
2491 		/* Actual uc table reconstruction. */
2492 		err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
2493 		if (err)
2494 			netdev_warn(net_dev, "Can't clear uc filters\n");
2495 		dpaa2_eth_add_uc_hw_addr(net_dev, priv);
2496 
2497 		/* Finally, clear uc promisc and set mc promisc as requested. */
2498 		err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
2499 		if (err)
2500 			netdev_warn(net_dev, "Can't clear uc promisc\n");
2501 		goto force_mc_promisc;
2502 	}
2503 
2504 	/* Neither unicast, nor multicast promisc will be on... eventually.
2505 	 * For now, rebuild mac filtering tables while forcing both of them on.
2506 	 */
2507 	err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2508 	if (err)
2509 		netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
2510 	err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
2511 	if (err)
2512 		netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
2513 
2514 	/* Actual mac filtering tables reconstruction */
2515 	err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
2516 	if (err)
2517 		netdev_warn(net_dev, "Can't clear mac filters\n");
2518 	dpaa2_eth_add_mc_hw_addr(net_dev, priv);
2519 	dpaa2_eth_add_uc_hw_addr(net_dev, priv);
2520 
2521 	/* Now we can clear both ucast and mcast promisc, without risking
2522 	 * to drop legitimate frames anymore.
2523 	 */
2524 	err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
2525 	if (err)
2526 		netdev_warn(net_dev, "Can't clear ucast promisc\n");
2527 	err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
2528 	if (err)
2529 		netdev_warn(net_dev, "Can't clear mcast promisc\n");
2530 
2531 	return;
2532 
2533 force_promisc:
2534 	err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2535 	if (err)
2536 		netdev_warn(net_dev, "Can't set ucast promisc\n");
2537 force_mc_promisc:
2538 	err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
2539 	if (err)
2540 		netdev_warn(net_dev, "Can't set mcast promisc\n");
2541 }
2542 
2543 static int dpaa2_eth_set_features(struct net_device *net_dev,
2544 				  netdev_features_t features)
2545 {
2546 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2547 	netdev_features_t changed = features ^ net_dev->features;
2548 	bool enable;
2549 	int err;
2550 
2551 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
2552 		enable = !!(features & NETIF_F_HW_VLAN_CTAG_FILTER);
2553 		err = dpaa2_eth_set_rx_vlan_filtering(priv, enable);
2554 		if (err)
2555 			return err;
2556 	}
2557 
2558 	if (changed & NETIF_F_RXCSUM) {
2559 		enable = !!(features & NETIF_F_RXCSUM);
2560 		err = dpaa2_eth_set_rx_csum(priv, enable);
2561 		if (err)
2562 			return err;
2563 	}
2564 
2565 	if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
2566 		enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
2567 		err = dpaa2_eth_set_tx_csum(priv, enable);
2568 		if (err)
2569 			return err;
2570 	}
2571 
2572 	return 0;
2573 }
2574 
2575 static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2576 {
2577 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
2578 	struct hwtstamp_config config;
2579 
2580 	if (!dpaa2_ptp)
2581 		return -EINVAL;
2582 
2583 	if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
2584 		return -EFAULT;
2585 
2586 	switch (config.tx_type) {
2587 	case HWTSTAMP_TX_OFF:
2588 	case HWTSTAMP_TX_ON:
2589 	case HWTSTAMP_TX_ONESTEP_SYNC:
2590 		priv->tx_tstamp_type = config.tx_type;
2591 		break;
2592 	default:
2593 		return -ERANGE;
2594 	}
2595 
2596 	if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
2597 		priv->rx_tstamp = false;
2598 	} else {
2599 		priv->rx_tstamp = true;
2600 		/* TS is set for all frame types, not only those requested */
2601 		config.rx_filter = HWTSTAMP_FILTER_ALL;
2602 	}
2603 
2604 	if (priv->tx_tstamp_type == HWTSTAMP_TX_ONESTEP_SYNC)
2605 		dpaa2_ptp_onestep_reg_update_method(priv);
2606 
2607 	return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
2608 			-EFAULT : 0;
2609 }
2610 
2611 static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2612 {
2613 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
2614 
2615 	if (cmd == SIOCSHWTSTAMP)
2616 		return dpaa2_eth_ts_ioctl(dev, rq, cmd);
2617 
2618 	if (dpaa2_eth_is_type_phy(priv))
2619 		return phylink_mii_ioctl(priv->mac->phylink, rq, cmd);
2620 
2621 	return -EOPNOTSUPP;
2622 }
2623 
2624 static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu)
2625 {
2626 	int mfl, linear_mfl;
2627 
2628 	mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
2629 	linear_mfl = priv->rx_buf_size - DPAA2_ETH_RX_HWA_SIZE -
2630 		     dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM;
2631 
2632 	if (mfl > linear_mfl) {
2633 		netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
2634 			    linear_mfl - VLAN_ETH_HLEN);
2635 		return false;
2636 	}
2637 
2638 	return true;
2639 }
2640 
2641 static int dpaa2_eth_set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
2642 {
2643 	int mfl, err;
2644 
2645 	/* We enforce a maximum Rx frame length based on MTU only if we have
2646 	 * an XDP program attached (in order to avoid Rx S/G frames).
2647 	 * Otherwise, we accept all incoming frames as long as they are not
2648 	 * larger than maximum size supported in hardware
2649 	 */
2650 	if (has_xdp)
2651 		mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
2652 	else
2653 		mfl = DPAA2_ETH_MFL;
2654 
2655 	err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl);
2656 	if (err) {
2657 		netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n");
2658 		return err;
2659 	}
2660 
2661 	return 0;
2662 }
2663 
2664 static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu)
2665 {
2666 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
2667 	int err;
2668 
2669 	if (!priv->xdp_prog)
2670 		goto out;
2671 
2672 	if (!xdp_mtu_valid(priv, new_mtu))
2673 		return -EINVAL;
2674 
2675 	err = dpaa2_eth_set_rx_mfl(priv, new_mtu, true);
2676 	if (err)
2677 		return err;
2678 
2679 out:
2680 	dev->mtu = new_mtu;
2681 	return 0;
2682 }
2683 
2684 static int dpaa2_eth_update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp)
2685 {
2686 	struct dpni_buffer_layout buf_layout = {0};
2687 	int err;
2688 
2689 	err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token,
2690 				     DPNI_QUEUE_RX, &buf_layout);
2691 	if (err) {
2692 		netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n");
2693 		return err;
2694 	}
2695 
2696 	/* Reserve extra headroom for XDP header size changes */
2697 	buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) +
2698 				    (has_xdp ? XDP_PACKET_HEADROOM : 0);
2699 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM;
2700 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2701 				     DPNI_QUEUE_RX, &buf_layout);
2702 	if (err) {
2703 		netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n");
2704 		return err;
2705 	}
2706 
2707 	return 0;
2708 }
2709 
2710 static int dpaa2_eth_setup_xdp(struct net_device *dev, struct bpf_prog *prog)
2711 {
2712 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
2713 	struct dpaa2_eth_channel *ch;
2714 	struct bpf_prog *old;
2715 	bool up, need_update;
2716 	int i, err;
2717 
2718 	if (prog && !xdp_mtu_valid(priv, dev->mtu))
2719 		return -EINVAL;
2720 
2721 	if (prog)
2722 		bpf_prog_add(prog, priv->num_channels);
2723 
2724 	up = netif_running(dev);
2725 	need_update = (!!priv->xdp_prog != !!prog);
2726 
2727 	if (up)
2728 		dev_close(dev);
2729 
2730 	/* While in xdp mode, enforce a maximum Rx frame size based on MTU.
2731 	 * Also, when switching between xdp/non-xdp modes we need to reconfigure
2732 	 * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop,
2733 	 * so we are sure no old format buffers will be used from now on.
2734 	 */
2735 	if (need_update) {
2736 		err = dpaa2_eth_set_rx_mfl(priv, dev->mtu, !!prog);
2737 		if (err)
2738 			goto out_err;
2739 		err = dpaa2_eth_update_rx_buffer_headroom(priv, !!prog);
2740 		if (err)
2741 			goto out_err;
2742 	}
2743 
2744 	old = xchg(&priv->xdp_prog, prog);
2745 	if (old)
2746 		bpf_prog_put(old);
2747 
2748 	for (i = 0; i < priv->num_channels; i++) {
2749 		ch = priv->channel[i];
2750 		old = xchg(&ch->xdp.prog, prog);
2751 		if (old)
2752 			bpf_prog_put(old);
2753 	}
2754 
2755 	if (up) {
2756 		err = dev_open(dev, NULL);
2757 		if (err)
2758 			return err;
2759 	}
2760 
2761 	return 0;
2762 
2763 out_err:
2764 	if (prog)
2765 		bpf_prog_sub(prog, priv->num_channels);
2766 	if (up)
2767 		dev_open(dev, NULL);
2768 
2769 	return err;
2770 }
2771 
2772 static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2773 {
2774 	switch (xdp->command) {
2775 	case XDP_SETUP_PROG:
2776 		return dpaa2_eth_setup_xdp(dev, xdp->prog);
2777 	case XDP_SETUP_XSK_POOL:
2778 		return dpaa2_xsk_setup_pool(dev, xdp->xsk.pool, xdp->xsk.queue_id);
2779 	default:
2780 		return -EINVAL;
2781 	}
2782 
2783 	return 0;
2784 }
2785 
2786 static int dpaa2_eth_xdp_create_fd(struct net_device *net_dev,
2787 				   struct xdp_frame *xdpf,
2788 				   struct dpaa2_fd *fd)
2789 {
2790 	struct device *dev = net_dev->dev.parent;
2791 	unsigned int needed_headroom;
2792 	struct dpaa2_eth_swa *swa;
2793 	void *buffer_start, *aligned_start;
2794 	dma_addr_t addr;
2795 
2796 	/* We require a minimum headroom to be able to transmit the frame.
2797 	 * Otherwise return an error and let the original net_device handle it
2798 	 */
2799 	needed_headroom = dpaa2_eth_needed_headroom(NULL);
2800 	if (xdpf->headroom < needed_headroom)
2801 		return -EINVAL;
2802 
2803 	/* Setup the FD fields */
2804 	memset(fd, 0, sizeof(*fd));
2805 
2806 	/* Align FD address, if possible */
2807 	buffer_start = xdpf->data - needed_headroom;
2808 	aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
2809 				  DPAA2_ETH_TX_BUF_ALIGN);
2810 	if (aligned_start >= xdpf->data - xdpf->headroom)
2811 		buffer_start = aligned_start;
2812 
2813 	swa = (struct dpaa2_eth_swa *)buffer_start;
2814 	/* fill in necessary fields here */
2815 	swa->type = DPAA2_ETH_SWA_XDP;
2816 	swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start;
2817 	swa->xdp.xdpf = xdpf;
2818 
2819 	addr = dma_map_single(dev, buffer_start,
2820 			      swa->xdp.dma_size,
2821 			      DMA_BIDIRECTIONAL);
2822 	if (unlikely(dma_mapping_error(dev, addr)))
2823 		return -ENOMEM;
2824 
2825 	dpaa2_fd_set_addr(fd, addr);
2826 	dpaa2_fd_set_offset(fd, xdpf->data - buffer_start);
2827 	dpaa2_fd_set_len(fd, xdpf->len);
2828 	dpaa2_fd_set_format(fd, dpaa2_fd_single);
2829 	dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
2830 
2831 	return 0;
2832 }
2833 
2834 static int dpaa2_eth_xdp_xmit(struct net_device *net_dev, int n,
2835 			      struct xdp_frame **frames, u32 flags)
2836 {
2837 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2838 	struct dpaa2_eth_xdp_fds *xdp_redirect_fds;
2839 	struct rtnl_link_stats64 *percpu_stats;
2840 	struct dpaa2_eth_fq *fq;
2841 	struct dpaa2_fd *fds;
2842 	int enqueued, i, err;
2843 
2844 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2845 		return -EINVAL;
2846 
2847 	if (!netif_running(net_dev))
2848 		return -ENETDOWN;
2849 
2850 	fq = &priv->fq[smp_processor_id()];
2851 	xdp_redirect_fds = &fq->xdp_redirect_fds;
2852 	fds = xdp_redirect_fds->fds;
2853 
2854 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
2855 
2856 	/* create a FD for each xdp_frame in the list received */
2857 	for (i = 0; i < n; i++) {
2858 		err = dpaa2_eth_xdp_create_fd(net_dev, frames[i], &fds[i]);
2859 		if (err)
2860 			break;
2861 	}
2862 	xdp_redirect_fds->num = i;
2863 
2864 	/* enqueue all the frame descriptors */
2865 	enqueued = dpaa2_eth_xdp_flush(priv, fq, xdp_redirect_fds);
2866 
2867 	/* update statistics */
2868 	percpu_stats->tx_packets += enqueued;
2869 	for (i = 0; i < enqueued; i++)
2870 		percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
2871 
2872 	return enqueued;
2873 }
2874 
2875 static int update_xps(struct dpaa2_eth_priv *priv)
2876 {
2877 	struct net_device *net_dev = priv->net_dev;
2878 	struct cpumask xps_mask;
2879 	struct dpaa2_eth_fq *fq;
2880 	int i, num_queues, netdev_queues;
2881 	int err = 0;
2882 
2883 	num_queues = dpaa2_eth_queue_count(priv);
2884 	netdev_queues = (net_dev->num_tc ? : 1) * num_queues;
2885 
2886 	/* The first <num_queues> entries in priv->fq array are Tx/Tx conf
2887 	 * queues, so only process those
2888 	 */
2889 	for (i = 0; i < netdev_queues; i++) {
2890 		fq = &priv->fq[i % num_queues];
2891 
2892 		cpumask_clear(&xps_mask);
2893 		cpumask_set_cpu(fq->target_cpu, &xps_mask);
2894 
2895 		err = netif_set_xps_queue(net_dev, &xps_mask, i);
2896 		if (err) {
2897 			netdev_warn_once(net_dev, "Error setting XPS queue\n");
2898 			break;
2899 		}
2900 	}
2901 
2902 	return err;
2903 }
2904 
2905 static int dpaa2_eth_setup_mqprio(struct net_device *net_dev,
2906 				  struct tc_mqprio_qopt *mqprio)
2907 {
2908 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2909 	u8 num_tc, num_queues;
2910 	int i;
2911 
2912 	mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
2913 	num_queues = dpaa2_eth_queue_count(priv);
2914 	num_tc = mqprio->num_tc;
2915 
2916 	if (num_tc == net_dev->num_tc)
2917 		return 0;
2918 
2919 	if (num_tc  > dpaa2_eth_tc_count(priv)) {
2920 		netdev_err(net_dev, "Max %d traffic classes supported\n",
2921 			   dpaa2_eth_tc_count(priv));
2922 		return -EOPNOTSUPP;
2923 	}
2924 
2925 	if (!num_tc) {
2926 		netdev_reset_tc(net_dev);
2927 		netif_set_real_num_tx_queues(net_dev, num_queues);
2928 		goto out;
2929 	}
2930 
2931 	netdev_set_num_tc(net_dev, num_tc);
2932 	netif_set_real_num_tx_queues(net_dev, num_tc * num_queues);
2933 
2934 	for (i = 0; i < num_tc; i++)
2935 		netdev_set_tc_queue(net_dev, i, num_queues, i * num_queues);
2936 
2937 out:
2938 	update_xps(priv);
2939 
2940 	return 0;
2941 }
2942 
2943 #define bps_to_mbits(rate) (div_u64((rate), 1000000) * 8)
2944 
2945 static int dpaa2_eth_setup_tbf(struct net_device *net_dev, struct tc_tbf_qopt_offload *p)
2946 {
2947 	struct tc_tbf_qopt_offload_replace_params *cfg = &p->replace_params;
2948 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2949 	struct dpni_tx_shaping_cfg tx_cr_shaper = { 0 };
2950 	struct dpni_tx_shaping_cfg tx_er_shaper = { 0 };
2951 	int err;
2952 
2953 	if (p->command == TC_TBF_STATS)
2954 		return -EOPNOTSUPP;
2955 
2956 	/* Only per port Tx shaping */
2957 	if (p->parent != TC_H_ROOT)
2958 		return -EOPNOTSUPP;
2959 
2960 	if (p->command == TC_TBF_REPLACE) {
2961 		if (cfg->max_size > DPAA2_ETH_MAX_BURST_SIZE) {
2962 			netdev_err(net_dev, "burst size cannot be greater than %d\n",
2963 				   DPAA2_ETH_MAX_BURST_SIZE);
2964 			return -EINVAL;
2965 		}
2966 
2967 		tx_cr_shaper.max_burst_size = cfg->max_size;
2968 		/* The TBF interface is in bytes/s, whereas DPAA2 expects the
2969 		 * rate in Mbits/s
2970 		 */
2971 		tx_cr_shaper.rate_limit = bps_to_mbits(cfg->rate.rate_bytes_ps);
2972 	}
2973 
2974 	err = dpni_set_tx_shaping(priv->mc_io, 0, priv->mc_token, &tx_cr_shaper,
2975 				  &tx_er_shaper, 0);
2976 	if (err) {
2977 		netdev_err(net_dev, "dpni_set_tx_shaping() = %d\n", err);
2978 		return err;
2979 	}
2980 
2981 	return 0;
2982 }
2983 
2984 static int dpaa2_eth_setup_tc(struct net_device *net_dev,
2985 			      enum tc_setup_type type, void *type_data)
2986 {
2987 	switch (type) {
2988 	case TC_SETUP_QDISC_MQPRIO:
2989 		return dpaa2_eth_setup_mqprio(net_dev, type_data);
2990 	case TC_SETUP_QDISC_TBF:
2991 		return dpaa2_eth_setup_tbf(net_dev, type_data);
2992 	default:
2993 		return -EOPNOTSUPP;
2994 	}
2995 }
2996 
2997 static const struct net_device_ops dpaa2_eth_ops = {
2998 	.ndo_open = dpaa2_eth_open,
2999 	.ndo_start_xmit = dpaa2_eth_tx,
3000 	.ndo_stop = dpaa2_eth_stop,
3001 	.ndo_set_mac_address = dpaa2_eth_set_addr,
3002 	.ndo_get_stats64 = dpaa2_eth_get_stats,
3003 	.ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
3004 	.ndo_set_features = dpaa2_eth_set_features,
3005 	.ndo_eth_ioctl = dpaa2_eth_ioctl,
3006 	.ndo_change_mtu = dpaa2_eth_change_mtu,
3007 	.ndo_bpf = dpaa2_eth_xdp,
3008 	.ndo_xdp_xmit = dpaa2_eth_xdp_xmit,
3009 	.ndo_xsk_wakeup = dpaa2_xsk_wakeup,
3010 	.ndo_setup_tc = dpaa2_eth_setup_tc,
3011 	.ndo_vlan_rx_add_vid = dpaa2_eth_rx_add_vid,
3012 	.ndo_vlan_rx_kill_vid = dpaa2_eth_rx_kill_vid
3013 };
3014 
3015 static void dpaa2_eth_cdan_cb(struct dpaa2_io_notification_ctx *ctx)
3016 {
3017 	struct dpaa2_eth_channel *ch;
3018 
3019 	ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
3020 
3021 	/* Update NAPI statistics */
3022 	ch->stats.cdan++;
3023 
3024 	/* NAPI can also be scheduled from the AF_XDP Tx path. Mark a missed
3025 	 * so that it can be rescheduled again.
3026 	 */
3027 	if (!napi_if_scheduled_mark_missed(&ch->napi))
3028 		napi_schedule(&ch->napi);
3029 }
3030 
3031 /* Allocate and configure a DPCON object */
3032 static struct fsl_mc_device *dpaa2_eth_setup_dpcon(struct dpaa2_eth_priv *priv)
3033 {
3034 	struct fsl_mc_device *dpcon;
3035 	struct device *dev = priv->net_dev->dev.parent;
3036 	int err;
3037 
3038 	err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
3039 				     FSL_MC_POOL_DPCON, &dpcon);
3040 	if (err) {
3041 		if (err == -ENXIO)
3042 			err = -EPROBE_DEFER;
3043 		else
3044 			dev_info(dev, "Not enough DPCONs, will go on as-is\n");
3045 		return ERR_PTR(err);
3046 	}
3047 
3048 	err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
3049 	if (err) {
3050 		dev_err(dev, "dpcon_open() failed\n");
3051 		goto free;
3052 	}
3053 
3054 	err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
3055 	if (err) {
3056 		dev_err(dev, "dpcon_reset() failed\n");
3057 		goto close;
3058 	}
3059 
3060 	err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
3061 	if (err) {
3062 		dev_err(dev, "dpcon_enable() failed\n");
3063 		goto close;
3064 	}
3065 
3066 	return dpcon;
3067 
3068 close:
3069 	dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
3070 free:
3071 	fsl_mc_object_free(dpcon);
3072 
3073 	return ERR_PTR(err);
3074 }
3075 
3076 static void dpaa2_eth_free_dpcon(struct dpaa2_eth_priv *priv,
3077 				 struct fsl_mc_device *dpcon)
3078 {
3079 	dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
3080 	dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
3081 	fsl_mc_object_free(dpcon);
3082 }
3083 
3084 static struct dpaa2_eth_channel *dpaa2_eth_alloc_channel(struct dpaa2_eth_priv *priv)
3085 {
3086 	struct dpaa2_eth_channel *channel;
3087 	struct dpcon_attr attr;
3088 	struct device *dev = priv->net_dev->dev.parent;
3089 	int err;
3090 
3091 	channel = kzalloc(sizeof(*channel), GFP_KERNEL);
3092 	if (!channel)
3093 		return NULL;
3094 
3095 	channel->dpcon = dpaa2_eth_setup_dpcon(priv);
3096 	if (IS_ERR(channel->dpcon)) {
3097 		err = PTR_ERR(channel->dpcon);
3098 		goto err_setup;
3099 	}
3100 
3101 	err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
3102 				   &attr);
3103 	if (err) {
3104 		dev_err(dev, "dpcon_get_attributes() failed\n");
3105 		goto err_get_attr;
3106 	}
3107 
3108 	channel->dpcon_id = attr.id;
3109 	channel->ch_id = attr.qbman_ch_id;
3110 	channel->priv = priv;
3111 
3112 	return channel;
3113 
3114 err_get_attr:
3115 	dpaa2_eth_free_dpcon(priv, channel->dpcon);
3116 err_setup:
3117 	kfree(channel);
3118 	return ERR_PTR(err);
3119 }
3120 
3121 static void dpaa2_eth_free_channel(struct dpaa2_eth_priv *priv,
3122 				   struct dpaa2_eth_channel *channel)
3123 {
3124 	dpaa2_eth_free_dpcon(priv, channel->dpcon);
3125 	kfree(channel);
3126 }
3127 
3128 /* DPIO setup: allocate and configure QBMan channels, setup core affinity
3129  * and register data availability notifications
3130  */
3131 static int dpaa2_eth_setup_dpio(struct dpaa2_eth_priv *priv)
3132 {
3133 	struct dpaa2_io_notification_ctx *nctx;
3134 	struct dpaa2_eth_channel *channel;
3135 	struct dpcon_notification_cfg dpcon_notif_cfg;
3136 	struct device *dev = priv->net_dev->dev.parent;
3137 	int i, err;
3138 
3139 	/* We want the ability to spread ingress traffic (RX, TX conf) to as
3140 	 * many cores as possible, so we need one channel for each core
3141 	 * (unless there's fewer queues than cores, in which case the extra
3142 	 * channels would be wasted).
3143 	 * Allocate one channel per core and register it to the core's
3144 	 * affine DPIO. If not enough channels are available for all cores
3145 	 * or if some cores don't have an affine DPIO, there will be no
3146 	 * ingress frame processing on those cores.
3147 	 */
3148 	cpumask_clear(&priv->dpio_cpumask);
3149 	for_each_online_cpu(i) {
3150 		/* Try to allocate a channel */
3151 		channel = dpaa2_eth_alloc_channel(priv);
3152 		if (IS_ERR_OR_NULL(channel)) {
3153 			err = PTR_ERR_OR_ZERO(channel);
3154 			if (err != -EPROBE_DEFER)
3155 				dev_info(dev,
3156 					 "No affine channel for cpu %d and above\n", i);
3157 			goto err_alloc_ch;
3158 		}
3159 
3160 		priv->channel[priv->num_channels] = channel;
3161 
3162 		nctx = &channel->nctx;
3163 		nctx->is_cdan = 1;
3164 		nctx->cb = dpaa2_eth_cdan_cb;
3165 		nctx->id = channel->ch_id;
3166 		nctx->desired_cpu = i;
3167 
3168 		/* Register the new context */
3169 		channel->dpio = dpaa2_io_service_select(i);
3170 		err = dpaa2_io_service_register(channel->dpio, nctx, dev);
3171 		if (err) {
3172 			dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
3173 			/* If no affine DPIO for this core, there's probably
3174 			 * none available for next cores either. Signal we want
3175 			 * to retry later, in case the DPIO devices weren't
3176 			 * probed yet.
3177 			 */
3178 			err = -EPROBE_DEFER;
3179 			goto err_service_reg;
3180 		}
3181 
3182 		/* Register DPCON notification with MC */
3183 		dpcon_notif_cfg.dpio_id = nctx->dpio_id;
3184 		dpcon_notif_cfg.priority = 0;
3185 		dpcon_notif_cfg.user_ctx = nctx->qman64;
3186 		err = dpcon_set_notification(priv->mc_io, 0,
3187 					     channel->dpcon->mc_handle,
3188 					     &dpcon_notif_cfg);
3189 		if (err) {
3190 			dev_err(dev, "dpcon_set_notification failed()\n");
3191 			goto err_set_cdan;
3192 		}
3193 
3194 		/* If we managed to allocate a channel and also found an affine
3195 		 * DPIO for this core, add it to the final mask
3196 		 */
3197 		cpumask_set_cpu(i, &priv->dpio_cpumask);
3198 		priv->num_channels++;
3199 
3200 		/* Stop if we already have enough channels to accommodate all
3201 		 * RX and TX conf queues
3202 		 */
3203 		if (priv->num_channels == priv->dpni_attrs.num_queues)
3204 			break;
3205 	}
3206 
3207 	return 0;
3208 
3209 err_set_cdan:
3210 	dpaa2_io_service_deregister(channel->dpio, nctx, dev);
3211 err_service_reg:
3212 	dpaa2_eth_free_channel(priv, channel);
3213 err_alloc_ch:
3214 	if (err == -EPROBE_DEFER) {
3215 		for (i = 0; i < priv->num_channels; i++) {
3216 			channel = priv->channel[i];
3217 			nctx = &channel->nctx;
3218 			dpaa2_io_service_deregister(channel->dpio, nctx, dev);
3219 			dpaa2_eth_free_channel(priv, channel);
3220 		}
3221 		priv->num_channels = 0;
3222 		return err;
3223 	}
3224 
3225 	if (cpumask_empty(&priv->dpio_cpumask)) {
3226 		dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
3227 		return -ENODEV;
3228 	}
3229 
3230 	dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
3231 		 cpumask_pr_args(&priv->dpio_cpumask));
3232 
3233 	return 0;
3234 }
3235 
3236 static void dpaa2_eth_free_dpio(struct dpaa2_eth_priv *priv)
3237 {
3238 	struct device *dev = priv->net_dev->dev.parent;
3239 	struct dpaa2_eth_channel *ch;
3240 	int i;
3241 
3242 	/* deregister CDAN notifications and free channels */
3243 	for (i = 0; i < priv->num_channels; i++) {
3244 		ch = priv->channel[i];
3245 		dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev);
3246 		dpaa2_eth_free_channel(priv, ch);
3247 	}
3248 }
3249 
3250 static struct dpaa2_eth_channel *dpaa2_eth_get_affine_channel(struct dpaa2_eth_priv *priv,
3251 							      int cpu)
3252 {
3253 	struct device *dev = priv->net_dev->dev.parent;
3254 	int i;
3255 
3256 	for (i = 0; i < priv->num_channels; i++)
3257 		if (priv->channel[i]->nctx.desired_cpu == cpu)
3258 			return priv->channel[i];
3259 
3260 	/* We should never get here. Issue a warning and return
3261 	 * the first channel, because it's still better than nothing
3262 	 */
3263 	dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
3264 
3265 	return priv->channel[0];
3266 }
3267 
3268 static void dpaa2_eth_set_fq_affinity(struct dpaa2_eth_priv *priv)
3269 {
3270 	struct device *dev = priv->net_dev->dev.parent;
3271 	struct dpaa2_eth_fq *fq;
3272 	int rx_cpu, txc_cpu;
3273 	int i;
3274 
3275 	/* For each FQ, pick one channel/CPU to deliver frames to.
3276 	 * This may well change at runtime, either through irqbalance or
3277 	 * through direct user intervention.
3278 	 */
3279 	rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
3280 
3281 	for (i = 0; i < priv->num_fqs; i++) {
3282 		fq = &priv->fq[i];
3283 		switch (fq->type) {
3284 		case DPAA2_RX_FQ:
3285 		case DPAA2_RX_ERR_FQ:
3286 			fq->target_cpu = rx_cpu;
3287 			rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
3288 			if (rx_cpu >= nr_cpu_ids)
3289 				rx_cpu = cpumask_first(&priv->dpio_cpumask);
3290 			break;
3291 		case DPAA2_TX_CONF_FQ:
3292 			fq->target_cpu = txc_cpu;
3293 			txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
3294 			if (txc_cpu >= nr_cpu_ids)
3295 				txc_cpu = cpumask_first(&priv->dpio_cpumask);
3296 			break;
3297 		default:
3298 			dev_err(dev, "Unknown FQ type: %d\n", fq->type);
3299 		}
3300 		fq->channel = dpaa2_eth_get_affine_channel(priv, fq->target_cpu);
3301 	}
3302 
3303 	update_xps(priv);
3304 }
3305 
3306 static void dpaa2_eth_setup_fqs(struct dpaa2_eth_priv *priv)
3307 {
3308 	int i, j;
3309 
3310 	/* We have one TxConf FQ per Tx flow.
3311 	 * The number of Tx and Rx queues is the same.
3312 	 * Tx queues come first in the fq array.
3313 	 */
3314 	for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
3315 		priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
3316 		priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
3317 		priv->fq[priv->num_fqs++].flowid = (u16)i;
3318 	}
3319 
3320 	for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
3321 		for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
3322 			priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
3323 			priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
3324 			priv->fq[priv->num_fqs].tc = (u8)j;
3325 			priv->fq[priv->num_fqs++].flowid = (u16)i;
3326 		}
3327 	}
3328 
3329 	/* We have exactly one Rx error queue per DPNI */
3330 	priv->fq[priv->num_fqs].type = DPAA2_RX_ERR_FQ;
3331 	priv->fq[priv->num_fqs++].consume = dpaa2_eth_rx_err;
3332 
3333 	/* For each FQ, decide on which core to process incoming frames */
3334 	dpaa2_eth_set_fq_affinity(priv);
3335 }
3336 
3337 /* Allocate and configure a buffer pool */
3338 struct dpaa2_eth_bp *dpaa2_eth_allocate_dpbp(struct dpaa2_eth_priv *priv)
3339 {
3340 	struct device *dev = priv->net_dev->dev.parent;
3341 	struct fsl_mc_device *dpbp_dev;
3342 	struct dpbp_attr dpbp_attrs;
3343 	struct dpaa2_eth_bp *bp;
3344 	int err;
3345 
3346 	err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
3347 				     &dpbp_dev);
3348 	if (err) {
3349 		if (err == -ENXIO)
3350 			err = -EPROBE_DEFER;
3351 		else
3352 			dev_err(dev, "DPBP device allocation failed\n");
3353 		return ERR_PTR(err);
3354 	}
3355 
3356 	bp = kzalloc(sizeof(*bp), GFP_KERNEL);
3357 	if (!bp) {
3358 		err = -ENOMEM;
3359 		goto err_alloc;
3360 	}
3361 
3362 	err = dpbp_open(priv->mc_io, 0, dpbp_dev->obj_desc.id,
3363 			&dpbp_dev->mc_handle);
3364 	if (err) {
3365 		dev_err(dev, "dpbp_open() failed\n");
3366 		goto err_open;
3367 	}
3368 
3369 	err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
3370 	if (err) {
3371 		dev_err(dev, "dpbp_reset() failed\n");
3372 		goto err_reset;
3373 	}
3374 
3375 	err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
3376 	if (err) {
3377 		dev_err(dev, "dpbp_enable() failed\n");
3378 		goto err_enable;
3379 	}
3380 
3381 	err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
3382 				  &dpbp_attrs);
3383 	if (err) {
3384 		dev_err(dev, "dpbp_get_attributes() failed\n");
3385 		goto err_get_attr;
3386 	}
3387 
3388 	bp->dev = dpbp_dev;
3389 	bp->bpid = dpbp_attrs.bpid;
3390 
3391 	return bp;
3392 
3393 err_get_attr:
3394 	dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
3395 err_enable:
3396 err_reset:
3397 	dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
3398 err_open:
3399 	kfree(bp);
3400 err_alloc:
3401 	fsl_mc_object_free(dpbp_dev);
3402 
3403 	return ERR_PTR(err);
3404 }
3405 
3406 static int dpaa2_eth_setup_default_dpbp(struct dpaa2_eth_priv *priv)
3407 {
3408 	struct dpaa2_eth_bp *bp;
3409 	int i;
3410 
3411 	bp = dpaa2_eth_allocate_dpbp(priv);
3412 	if (IS_ERR(bp))
3413 		return PTR_ERR(bp);
3414 
3415 	priv->bp[DPAA2_ETH_DEFAULT_BP_IDX] = bp;
3416 	priv->num_bps++;
3417 
3418 	for (i = 0; i < priv->num_channels; i++)
3419 		priv->channel[i]->bp = bp;
3420 
3421 	return 0;
3422 }
3423 
3424 void dpaa2_eth_free_dpbp(struct dpaa2_eth_priv *priv, struct dpaa2_eth_bp *bp)
3425 {
3426 	int idx_bp;
3427 
3428 	/* Find the index at which this BP is stored */
3429 	for (idx_bp = 0; idx_bp < priv->num_bps; idx_bp++)
3430 		if (priv->bp[idx_bp] == bp)
3431 			break;
3432 
3433 	/* Drain the pool and disable the associated MC object */
3434 	dpaa2_eth_drain_pool(priv, bp->bpid);
3435 	dpbp_disable(priv->mc_io, 0, bp->dev->mc_handle);
3436 	dpbp_close(priv->mc_io, 0, bp->dev->mc_handle);
3437 	fsl_mc_object_free(bp->dev);
3438 	kfree(bp);
3439 
3440 	/* Move the last in use DPBP over in this position */
3441 	priv->bp[idx_bp] = priv->bp[priv->num_bps - 1];
3442 	priv->num_bps--;
3443 }
3444 
3445 static void dpaa2_eth_free_dpbps(struct dpaa2_eth_priv *priv)
3446 {
3447 	int i;
3448 
3449 	for (i = 0; i < priv->num_bps; i++)
3450 		dpaa2_eth_free_dpbp(priv, priv->bp[i]);
3451 }
3452 
3453 static int dpaa2_eth_set_buffer_layout(struct dpaa2_eth_priv *priv)
3454 {
3455 	struct device *dev = priv->net_dev->dev.parent;
3456 	struct dpni_buffer_layout buf_layout = {0};
3457 	u16 rx_buf_align;
3458 	int err;
3459 
3460 	/* We need to check for WRIOP version 1.0.0, but depending on the MC
3461 	 * version, this number is not always provided correctly on rev1.
3462 	 * We need to check for both alternatives in this situation.
3463 	 */
3464 	if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
3465 	    priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
3466 		rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
3467 	else
3468 		rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
3469 
3470 	/* We need to ensure that the buffer size seen by WRIOP is a multiple
3471 	 * of 64 or 256 bytes depending on the WRIOP version.
3472 	 */
3473 	priv->rx_buf_size = ALIGN_DOWN(DPAA2_ETH_RX_BUF_SIZE, rx_buf_align);
3474 
3475 	/* tx buffer */
3476 	buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
3477 	buf_layout.pass_timestamp = true;
3478 	buf_layout.pass_frame_status = true;
3479 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
3480 			     DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
3481 			     DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
3482 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3483 				     DPNI_QUEUE_TX, &buf_layout);
3484 	if (err) {
3485 		dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
3486 		return err;
3487 	}
3488 
3489 	/* tx-confirm buffer */
3490 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
3491 			     DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
3492 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3493 				     DPNI_QUEUE_TX_CONFIRM, &buf_layout);
3494 	if (err) {
3495 		dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
3496 		return err;
3497 	}
3498 
3499 	/* Now that we've set our tx buffer layout, retrieve the minimum
3500 	 * required tx data offset.
3501 	 */
3502 	err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
3503 				      &priv->tx_data_offset);
3504 	if (err) {
3505 		dev_err(dev, "dpni_get_tx_data_offset() failed\n");
3506 		return err;
3507 	}
3508 
3509 	if ((priv->tx_data_offset % 64) != 0)
3510 		dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
3511 			 priv->tx_data_offset);
3512 
3513 	/* rx buffer */
3514 	buf_layout.pass_frame_status = true;
3515 	buf_layout.pass_parser_result = true;
3516 	buf_layout.data_align = rx_buf_align;
3517 	buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
3518 	buf_layout.private_data_size = 0;
3519 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
3520 			     DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
3521 			     DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
3522 			     DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
3523 			     DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
3524 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3525 				     DPNI_QUEUE_RX, &buf_layout);
3526 	if (err) {
3527 		dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
3528 		return err;
3529 	}
3530 
3531 	return 0;
3532 }
3533 
3534 #define DPNI_ENQUEUE_FQID_VER_MAJOR	7
3535 #define DPNI_ENQUEUE_FQID_VER_MINOR	9
3536 
3537 static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv,
3538 				       struct dpaa2_eth_fq *fq,
3539 				       struct dpaa2_fd *fd, u8 prio,
3540 				       u32 num_frames __always_unused,
3541 				       int *frames_enqueued)
3542 {
3543 	int err;
3544 
3545 	err = dpaa2_io_service_enqueue_qd(fq->channel->dpio,
3546 					  priv->tx_qdid, prio,
3547 					  fq->tx_qdbin, fd);
3548 	if (!err && frames_enqueued)
3549 		*frames_enqueued = 1;
3550 	return err;
3551 }
3552 
3553 static inline int dpaa2_eth_enqueue_fq_multiple(struct dpaa2_eth_priv *priv,
3554 						struct dpaa2_eth_fq *fq,
3555 						struct dpaa2_fd *fd,
3556 						u8 prio, u32 num_frames,
3557 						int *frames_enqueued)
3558 {
3559 	int err;
3560 
3561 	err = dpaa2_io_service_enqueue_multiple_fq(fq->channel->dpio,
3562 						   fq->tx_fqid[prio],
3563 						   fd, num_frames);
3564 
3565 	if (err == 0)
3566 		return -EBUSY;
3567 
3568 	if (frames_enqueued)
3569 		*frames_enqueued = err;
3570 	return 0;
3571 }
3572 
3573 static void dpaa2_eth_set_enqueue_mode(struct dpaa2_eth_priv *priv)
3574 {
3575 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3576 				   DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3577 		priv->enqueue = dpaa2_eth_enqueue_qd;
3578 	else
3579 		priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
3580 }
3581 
3582 static int dpaa2_eth_set_pause(struct dpaa2_eth_priv *priv)
3583 {
3584 	struct device *dev = priv->net_dev->dev.parent;
3585 	struct dpni_link_cfg link_cfg = {0};
3586 	int err;
3587 
3588 	/* Get the default link options so we don't override other flags */
3589 	err = dpni_get_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
3590 	if (err) {
3591 		dev_err(dev, "dpni_get_link_cfg() failed\n");
3592 		return err;
3593 	}
3594 
3595 	/* By default, enable both Rx and Tx pause frames */
3596 	link_cfg.options |= DPNI_LINK_OPT_PAUSE;
3597 	link_cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
3598 	err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
3599 	if (err) {
3600 		dev_err(dev, "dpni_set_link_cfg() failed\n");
3601 		return err;
3602 	}
3603 
3604 	priv->link_state.options = link_cfg.options;
3605 
3606 	return 0;
3607 }
3608 
3609 static void dpaa2_eth_update_tx_fqids(struct dpaa2_eth_priv *priv)
3610 {
3611 	struct dpni_queue_id qid = {0};
3612 	struct dpaa2_eth_fq *fq;
3613 	struct dpni_queue queue;
3614 	int i, j, err;
3615 
3616 	/* We only use Tx FQIDs for FQID-based enqueue, so check
3617 	 * if DPNI version supports it before updating FQIDs
3618 	 */
3619 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3620 				   DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3621 		return;
3622 
3623 	for (i = 0; i < priv->num_fqs; i++) {
3624 		fq = &priv->fq[i];
3625 		if (fq->type != DPAA2_TX_CONF_FQ)
3626 			continue;
3627 		for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
3628 			err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3629 					     DPNI_QUEUE_TX, j, fq->flowid,
3630 					     &queue, &qid);
3631 			if (err)
3632 				goto out_err;
3633 
3634 			fq->tx_fqid[j] = qid.fqid;
3635 			if (fq->tx_fqid[j] == 0)
3636 				goto out_err;
3637 		}
3638 	}
3639 
3640 	priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
3641 
3642 	return;
3643 
3644 out_err:
3645 	netdev_info(priv->net_dev,
3646 		    "Error reading Tx FQID, fallback to QDID-based enqueue\n");
3647 	priv->enqueue = dpaa2_eth_enqueue_qd;
3648 }
3649 
3650 /* Configure ingress classification based on VLAN PCP */
3651 static int dpaa2_eth_set_vlan_qos(struct dpaa2_eth_priv *priv)
3652 {
3653 	struct device *dev = priv->net_dev->dev.parent;
3654 	struct dpkg_profile_cfg kg_cfg = {0};
3655 	struct dpni_qos_tbl_cfg qos_cfg = {0};
3656 	struct dpni_rule_cfg key_params;
3657 	void *dma_mem, *key, *mask;
3658 	u8 key_size = 2;	/* VLAN TCI field */
3659 	int i, pcp, err;
3660 
3661 	/* VLAN-based classification only makes sense if we have multiple
3662 	 * traffic classes.
3663 	 * Also, we need to extract just the 3-bit PCP field from the VLAN
3664 	 * header and we can only do that by using a mask
3665 	 */
3666 	if (dpaa2_eth_tc_count(priv) == 1 || !dpaa2_eth_fs_mask_enabled(priv)) {
3667 		dev_dbg(dev, "VLAN-based QoS classification not supported\n");
3668 		return -EOPNOTSUPP;
3669 	}
3670 
3671 	dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
3672 	if (!dma_mem)
3673 		return -ENOMEM;
3674 
3675 	kg_cfg.num_extracts = 1;
3676 	kg_cfg.extracts[0].type = DPKG_EXTRACT_FROM_HDR;
3677 	kg_cfg.extracts[0].extract.from_hdr.prot = NET_PROT_VLAN;
3678 	kg_cfg.extracts[0].extract.from_hdr.type = DPKG_FULL_FIELD;
3679 	kg_cfg.extracts[0].extract.from_hdr.field = NH_FLD_VLAN_TCI;
3680 
3681 	err = dpni_prepare_key_cfg(&kg_cfg, dma_mem);
3682 	if (err) {
3683 		dev_err(dev, "dpni_prepare_key_cfg failed\n");
3684 		goto out_free_tbl;
3685 	}
3686 
3687 	/* set QoS table */
3688 	qos_cfg.default_tc = 0;
3689 	qos_cfg.discard_on_miss = 0;
3690 	qos_cfg.key_cfg_iova = dma_map_single(dev, dma_mem,
3691 					      DPAA2_CLASSIFIER_DMA_SIZE,
3692 					      DMA_TO_DEVICE);
3693 	if (dma_mapping_error(dev, qos_cfg.key_cfg_iova)) {
3694 		dev_err(dev, "QoS table DMA mapping failed\n");
3695 		err = -ENOMEM;
3696 		goto out_free_tbl;
3697 	}
3698 
3699 	err = dpni_set_qos_table(priv->mc_io, 0, priv->mc_token, &qos_cfg);
3700 	if (err) {
3701 		dev_err(dev, "dpni_set_qos_table failed\n");
3702 		goto out_unmap_tbl;
3703 	}
3704 
3705 	/* Add QoS table entries */
3706 	key = kzalloc(key_size * 2, GFP_KERNEL);
3707 	if (!key) {
3708 		err = -ENOMEM;
3709 		goto out_unmap_tbl;
3710 	}
3711 	mask = key + key_size;
3712 	*(__be16 *)mask = cpu_to_be16(VLAN_PRIO_MASK);
3713 
3714 	key_params.key_iova = dma_map_single(dev, key, key_size * 2,
3715 					     DMA_TO_DEVICE);
3716 	if (dma_mapping_error(dev, key_params.key_iova)) {
3717 		dev_err(dev, "Qos table entry DMA mapping failed\n");
3718 		err = -ENOMEM;
3719 		goto out_free_key;
3720 	}
3721 
3722 	key_params.mask_iova = key_params.key_iova + key_size;
3723 	key_params.key_size = key_size;
3724 
3725 	/* We add rules for PCP-based distribution starting with highest
3726 	 * priority (VLAN PCP = 7). If this DPNI doesn't have enough traffic
3727 	 * classes to accommodate all priority levels, the lowest ones end up
3728 	 * on TC 0 which was configured as default
3729 	 */
3730 	for (i = dpaa2_eth_tc_count(priv) - 1, pcp = 7; i >= 0; i--, pcp--) {
3731 		*(__be16 *)key = cpu_to_be16(pcp << VLAN_PRIO_SHIFT);
3732 		dma_sync_single_for_device(dev, key_params.key_iova,
3733 					   key_size * 2, DMA_TO_DEVICE);
3734 
3735 		err = dpni_add_qos_entry(priv->mc_io, 0, priv->mc_token,
3736 					 &key_params, i, i);
3737 		if (err) {
3738 			dev_err(dev, "dpni_add_qos_entry failed\n");
3739 			dpni_clear_qos_table(priv->mc_io, 0, priv->mc_token);
3740 			goto out_unmap_key;
3741 		}
3742 	}
3743 
3744 	priv->vlan_cls_enabled = true;
3745 
3746 	/* Table and key memory is not persistent, clean everything up after
3747 	 * configuration is finished
3748 	 */
3749 out_unmap_key:
3750 	dma_unmap_single(dev, key_params.key_iova, key_size * 2, DMA_TO_DEVICE);
3751 out_free_key:
3752 	kfree(key);
3753 out_unmap_tbl:
3754 	dma_unmap_single(dev, qos_cfg.key_cfg_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3755 			 DMA_TO_DEVICE);
3756 out_free_tbl:
3757 	kfree(dma_mem);
3758 
3759 	return err;
3760 }
3761 
3762 /* Configure the DPNI object this interface is associated with */
3763 static int dpaa2_eth_setup_dpni(struct fsl_mc_device *ls_dev)
3764 {
3765 	struct device *dev = &ls_dev->dev;
3766 	struct dpaa2_eth_priv *priv;
3767 	struct net_device *net_dev;
3768 	int err;
3769 
3770 	net_dev = dev_get_drvdata(dev);
3771 	priv = netdev_priv(net_dev);
3772 
3773 	/* get a handle for the DPNI object */
3774 	err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
3775 	if (err) {
3776 		dev_err(dev, "dpni_open() failed\n");
3777 		return err;
3778 	}
3779 
3780 	/* Check if we can work with this DPNI object */
3781 	err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
3782 				   &priv->dpni_ver_minor);
3783 	if (err) {
3784 		dev_err(dev, "dpni_get_api_version() failed\n");
3785 		goto close;
3786 	}
3787 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
3788 		dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
3789 			priv->dpni_ver_major, priv->dpni_ver_minor,
3790 			DPNI_VER_MAJOR, DPNI_VER_MINOR);
3791 		err = -ENOTSUPP;
3792 		goto close;
3793 	}
3794 
3795 	ls_dev->mc_io = priv->mc_io;
3796 	ls_dev->mc_handle = priv->mc_token;
3797 
3798 	err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3799 	if (err) {
3800 		dev_err(dev, "dpni_reset() failed\n");
3801 		goto close;
3802 	}
3803 
3804 	err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
3805 				  &priv->dpni_attrs);
3806 	if (err) {
3807 		dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
3808 		goto close;
3809 	}
3810 
3811 	err = dpaa2_eth_set_buffer_layout(priv);
3812 	if (err)
3813 		goto close;
3814 
3815 	dpaa2_eth_set_enqueue_mode(priv);
3816 
3817 	/* Enable pause frame support */
3818 	if (dpaa2_eth_has_pause_support(priv)) {
3819 		err = dpaa2_eth_set_pause(priv);
3820 		if (err)
3821 			goto close;
3822 	}
3823 
3824 	err = dpaa2_eth_set_vlan_qos(priv);
3825 	if (err && err != -EOPNOTSUPP)
3826 		goto close;
3827 
3828 	priv->cls_rules = devm_kcalloc(dev, dpaa2_eth_fs_count(priv),
3829 				       sizeof(struct dpaa2_eth_cls_rule),
3830 				       GFP_KERNEL);
3831 	if (!priv->cls_rules) {
3832 		err = -ENOMEM;
3833 		goto close;
3834 	}
3835 
3836 	return 0;
3837 
3838 close:
3839 	dpni_close(priv->mc_io, 0, priv->mc_token);
3840 
3841 	return err;
3842 }
3843 
3844 static void dpaa2_eth_free_dpni(struct dpaa2_eth_priv *priv)
3845 {
3846 	int err;
3847 
3848 	err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3849 	if (err)
3850 		netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
3851 			    err);
3852 
3853 	dpni_close(priv->mc_io, 0, priv->mc_token);
3854 }
3855 
3856 static int dpaa2_eth_setup_rx_flow(struct dpaa2_eth_priv *priv,
3857 				   struct dpaa2_eth_fq *fq)
3858 {
3859 	struct device *dev = priv->net_dev->dev.parent;
3860 	struct dpni_queue queue;
3861 	struct dpni_queue_id qid;
3862 	int err;
3863 
3864 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3865 			     DPNI_QUEUE_RX, fq->tc, fq->flowid, &queue, &qid);
3866 	if (err) {
3867 		dev_err(dev, "dpni_get_queue(RX) failed\n");
3868 		return err;
3869 	}
3870 
3871 	fq->fqid = qid.fqid;
3872 
3873 	queue.destination.id = fq->channel->dpcon_id;
3874 	queue.destination.type = DPNI_DEST_DPCON;
3875 	queue.destination.priority = 1;
3876 	queue.user_context = (u64)(uintptr_t)fq;
3877 	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3878 			     DPNI_QUEUE_RX, fq->tc, fq->flowid,
3879 			     DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3880 			     &queue);
3881 	if (err) {
3882 		dev_err(dev, "dpni_set_queue(RX) failed\n");
3883 		return err;
3884 	}
3885 
3886 	/* xdp_rxq setup */
3887 	/* only once for each channel */
3888 	if (fq->tc > 0)
3889 		return 0;
3890 
3891 	err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev,
3892 			       fq->flowid, 0);
3893 	if (err) {
3894 		dev_err(dev, "xdp_rxq_info_reg failed\n");
3895 		return err;
3896 	}
3897 
3898 	err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq,
3899 					 MEM_TYPE_PAGE_ORDER0, NULL);
3900 	if (err) {
3901 		dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n");
3902 		return err;
3903 	}
3904 
3905 	return 0;
3906 }
3907 
3908 static int dpaa2_eth_setup_tx_flow(struct dpaa2_eth_priv *priv,
3909 				   struct dpaa2_eth_fq *fq)
3910 {
3911 	struct device *dev = priv->net_dev->dev.parent;
3912 	struct dpni_queue queue;
3913 	struct dpni_queue_id qid;
3914 	int i, err;
3915 
3916 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3917 		err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3918 				     DPNI_QUEUE_TX, i, fq->flowid,
3919 				     &queue, &qid);
3920 		if (err) {
3921 			dev_err(dev, "dpni_get_queue(TX) failed\n");
3922 			return err;
3923 		}
3924 		fq->tx_fqid[i] = qid.fqid;
3925 	}
3926 
3927 	/* All Tx queues belonging to the same flowid have the same qdbin */
3928 	fq->tx_qdbin = qid.qdbin;
3929 
3930 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3931 			     DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3932 			     &queue, &qid);
3933 	if (err) {
3934 		dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
3935 		return err;
3936 	}
3937 
3938 	fq->fqid = qid.fqid;
3939 
3940 	queue.destination.id = fq->channel->dpcon_id;
3941 	queue.destination.type = DPNI_DEST_DPCON;
3942 	queue.destination.priority = 0;
3943 	queue.user_context = (u64)(uintptr_t)fq;
3944 	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3945 			     DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3946 			     DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3947 			     &queue);
3948 	if (err) {
3949 		dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
3950 		return err;
3951 	}
3952 
3953 	return 0;
3954 }
3955 
3956 static int setup_rx_err_flow(struct dpaa2_eth_priv *priv,
3957 			     struct dpaa2_eth_fq *fq)
3958 {
3959 	struct device *dev = priv->net_dev->dev.parent;
3960 	struct dpni_queue q = { { 0 } };
3961 	struct dpni_queue_id qid;
3962 	u8 q_opt = DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST;
3963 	int err;
3964 
3965 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3966 			     DPNI_QUEUE_RX_ERR, 0, 0, &q, &qid);
3967 	if (err) {
3968 		dev_err(dev, "dpni_get_queue() failed (%d)\n", err);
3969 		return err;
3970 	}
3971 
3972 	fq->fqid = qid.fqid;
3973 
3974 	q.destination.id = fq->channel->dpcon_id;
3975 	q.destination.type = DPNI_DEST_DPCON;
3976 	q.destination.priority = 1;
3977 	q.user_context = (u64)(uintptr_t)fq;
3978 	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3979 			     DPNI_QUEUE_RX_ERR, 0, 0, q_opt, &q);
3980 	if (err) {
3981 		dev_err(dev, "dpni_set_queue() failed (%d)\n", err);
3982 		return err;
3983 	}
3984 
3985 	return 0;
3986 }
3987 
3988 /* Supported header fields for Rx hash distribution key */
3989 static const struct dpaa2_eth_dist_fields dist_fields[] = {
3990 	{
3991 		/* L2 header */
3992 		.rxnfc_field = RXH_L2DA,
3993 		.cls_prot = NET_PROT_ETH,
3994 		.cls_field = NH_FLD_ETH_DA,
3995 		.id = DPAA2_ETH_DIST_ETHDST,
3996 		.size = 6,
3997 	}, {
3998 		.cls_prot = NET_PROT_ETH,
3999 		.cls_field = NH_FLD_ETH_SA,
4000 		.id = DPAA2_ETH_DIST_ETHSRC,
4001 		.size = 6,
4002 	}, {
4003 		/* This is the last ethertype field parsed:
4004 		 * depending on frame format, it can be the MAC ethertype
4005 		 * or the VLAN etype.
4006 		 */
4007 		.cls_prot = NET_PROT_ETH,
4008 		.cls_field = NH_FLD_ETH_TYPE,
4009 		.id = DPAA2_ETH_DIST_ETHTYPE,
4010 		.size = 2,
4011 	}, {
4012 		/* VLAN header */
4013 		.rxnfc_field = RXH_VLAN,
4014 		.cls_prot = NET_PROT_VLAN,
4015 		.cls_field = NH_FLD_VLAN_TCI,
4016 		.id = DPAA2_ETH_DIST_VLAN,
4017 		.size = 2,
4018 	}, {
4019 		/* IP header */
4020 		.rxnfc_field = RXH_IP_SRC,
4021 		.cls_prot = NET_PROT_IP,
4022 		.cls_field = NH_FLD_IP_SRC,
4023 		.id = DPAA2_ETH_DIST_IPSRC,
4024 		.size = 4,
4025 	}, {
4026 		.rxnfc_field = RXH_IP_DST,
4027 		.cls_prot = NET_PROT_IP,
4028 		.cls_field = NH_FLD_IP_DST,
4029 		.id = DPAA2_ETH_DIST_IPDST,
4030 		.size = 4,
4031 	}, {
4032 		.rxnfc_field = RXH_L3_PROTO,
4033 		.cls_prot = NET_PROT_IP,
4034 		.cls_field = NH_FLD_IP_PROTO,
4035 		.id = DPAA2_ETH_DIST_IPPROTO,
4036 		.size = 1,
4037 	}, {
4038 		/* Using UDP ports, this is functionally equivalent to raw
4039 		 * byte pairs from L4 header.
4040 		 */
4041 		.rxnfc_field = RXH_L4_B_0_1,
4042 		.cls_prot = NET_PROT_UDP,
4043 		.cls_field = NH_FLD_UDP_PORT_SRC,
4044 		.id = DPAA2_ETH_DIST_L4SRC,
4045 		.size = 2,
4046 	}, {
4047 		.rxnfc_field = RXH_L4_B_2_3,
4048 		.cls_prot = NET_PROT_UDP,
4049 		.cls_field = NH_FLD_UDP_PORT_DST,
4050 		.id = DPAA2_ETH_DIST_L4DST,
4051 		.size = 2,
4052 	},
4053 };
4054 
4055 /* Configure the Rx hash key using the legacy API */
4056 static int dpaa2_eth_config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
4057 {
4058 	struct device *dev = priv->net_dev->dev.parent;
4059 	struct dpni_rx_tc_dist_cfg dist_cfg;
4060 	int i, err = 0;
4061 
4062 	memset(&dist_cfg, 0, sizeof(dist_cfg));
4063 
4064 	dist_cfg.key_cfg_iova = key;
4065 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
4066 	dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
4067 
4068 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
4069 		err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token,
4070 					  i, &dist_cfg);
4071 		if (err) {
4072 			dev_err(dev, "dpni_set_rx_tc_dist failed\n");
4073 			break;
4074 		}
4075 	}
4076 
4077 	return err;
4078 }
4079 
4080 /* Configure the Rx hash key using the new API */
4081 static int dpaa2_eth_config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
4082 {
4083 	struct device *dev = priv->net_dev->dev.parent;
4084 	struct dpni_rx_dist_cfg dist_cfg;
4085 	int i, err = 0;
4086 
4087 	memset(&dist_cfg, 0, sizeof(dist_cfg));
4088 
4089 	dist_cfg.key_cfg_iova = key;
4090 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
4091 	dist_cfg.enable = 1;
4092 
4093 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
4094 		dist_cfg.tc = i;
4095 		err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token,
4096 					    &dist_cfg);
4097 		if (err) {
4098 			dev_err(dev, "dpni_set_rx_hash_dist failed\n");
4099 			break;
4100 		}
4101 
4102 		/* If the flow steering / hashing key is shared between all
4103 		 * traffic classes, install it just once
4104 		 */
4105 		if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
4106 			break;
4107 	}
4108 
4109 	return err;
4110 }
4111 
4112 /* Configure the Rx flow classification key */
4113 static int dpaa2_eth_config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
4114 {
4115 	struct device *dev = priv->net_dev->dev.parent;
4116 	struct dpni_rx_dist_cfg dist_cfg;
4117 	int i, err = 0;
4118 
4119 	memset(&dist_cfg, 0, sizeof(dist_cfg));
4120 
4121 	dist_cfg.key_cfg_iova = key;
4122 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
4123 	dist_cfg.enable = 1;
4124 
4125 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
4126 		dist_cfg.tc = i;
4127 		err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token,
4128 					  &dist_cfg);
4129 		if (err) {
4130 			dev_err(dev, "dpni_set_rx_fs_dist failed\n");
4131 			break;
4132 		}
4133 
4134 		/* If the flow steering / hashing key is shared between all
4135 		 * traffic classes, install it just once
4136 		 */
4137 		if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
4138 			break;
4139 	}
4140 
4141 	return err;
4142 }
4143 
4144 /* Size of the Rx flow classification key */
4145 int dpaa2_eth_cls_key_size(u64 fields)
4146 {
4147 	int i, size = 0;
4148 
4149 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
4150 		if (!(fields & dist_fields[i].id))
4151 			continue;
4152 		size += dist_fields[i].size;
4153 	}
4154 
4155 	return size;
4156 }
4157 
4158 /* Offset of header field in Rx classification key */
4159 int dpaa2_eth_cls_fld_off(int prot, int field)
4160 {
4161 	int i, off = 0;
4162 
4163 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
4164 		if (dist_fields[i].cls_prot == prot &&
4165 		    dist_fields[i].cls_field == field)
4166 			return off;
4167 		off += dist_fields[i].size;
4168 	}
4169 
4170 	WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
4171 	return 0;
4172 }
4173 
4174 /* Prune unused fields from the classification rule.
4175  * Used when masking is not supported
4176  */
4177 void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields)
4178 {
4179 	int off = 0, new_off = 0;
4180 	int i, size;
4181 
4182 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
4183 		size = dist_fields[i].size;
4184 		if (dist_fields[i].id & fields) {
4185 			memcpy(key_mem + new_off, key_mem + off, size);
4186 			new_off += size;
4187 		}
4188 		off += size;
4189 	}
4190 }
4191 
4192 /* Set Rx distribution (hash or flow classification) key
4193  * flags is a combination of RXH_ bits
4194  */
4195 static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
4196 				  enum dpaa2_eth_rx_dist type, u64 flags)
4197 {
4198 	struct device *dev = net_dev->dev.parent;
4199 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4200 	struct dpkg_profile_cfg cls_cfg;
4201 	u32 rx_hash_fields = 0;
4202 	dma_addr_t key_iova;
4203 	u8 *dma_mem;
4204 	int i;
4205 	int err = 0;
4206 
4207 	memset(&cls_cfg, 0, sizeof(cls_cfg));
4208 
4209 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
4210 		struct dpkg_extract *key =
4211 			&cls_cfg.extracts[cls_cfg.num_extracts];
4212 
4213 		/* For both Rx hashing and classification keys
4214 		 * we set only the selected fields.
4215 		 */
4216 		if (!(flags & dist_fields[i].id))
4217 			continue;
4218 		if (type == DPAA2_ETH_RX_DIST_HASH)
4219 			rx_hash_fields |= dist_fields[i].rxnfc_field;
4220 
4221 		if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
4222 			dev_err(dev, "error adding key extraction rule, too many rules?\n");
4223 			return -E2BIG;
4224 		}
4225 
4226 		key->type = DPKG_EXTRACT_FROM_HDR;
4227 		key->extract.from_hdr.prot = dist_fields[i].cls_prot;
4228 		key->extract.from_hdr.type = DPKG_FULL_FIELD;
4229 		key->extract.from_hdr.field = dist_fields[i].cls_field;
4230 		cls_cfg.num_extracts++;
4231 	}
4232 
4233 	dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
4234 	if (!dma_mem)
4235 		return -ENOMEM;
4236 
4237 	err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
4238 	if (err) {
4239 		dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
4240 		goto free_key;
4241 	}
4242 
4243 	/* Prepare for setting the rx dist */
4244 	key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
4245 				  DMA_TO_DEVICE);
4246 	if (dma_mapping_error(dev, key_iova)) {
4247 		dev_err(dev, "DMA mapping failed\n");
4248 		err = -ENOMEM;
4249 		goto free_key;
4250 	}
4251 
4252 	if (type == DPAA2_ETH_RX_DIST_HASH) {
4253 		if (dpaa2_eth_has_legacy_dist(priv))
4254 			err = dpaa2_eth_config_legacy_hash_key(priv, key_iova);
4255 		else
4256 			err = dpaa2_eth_config_hash_key(priv, key_iova);
4257 	} else {
4258 		err = dpaa2_eth_config_cls_key(priv, key_iova);
4259 	}
4260 
4261 	dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
4262 			 DMA_TO_DEVICE);
4263 	if (!err && type == DPAA2_ETH_RX_DIST_HASH)
4264 		priv->rx_hash_fields = rx_hash_fields;
4265 
4266 free_key:
4267 	kfree(dma_mem);
4268 	return err;
4269 }
4270 
4271 int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
4272 {
4273 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4274 	u64 key = 0;
4275 	int i;
4276 
4277 	if (!dpaa2_eth_hash_enabled(priv))
4278 		return -EOPNOTSUPP;
4279 
4280 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
4281 		if (dist_fields[i].rxnfc_field & flags)
4282 			key |= dist_fields[i].id;
4283 
4284 	return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, key);
4285 }
4286 
4287 int dpaa2_eth_set_cls(struct net_device *net_dev, u64 flags)
4288 {
4289 	return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_CLS, flags);
4290 }
4291 
4292 static int dpaa2_eth_set_default_cls(struct dpaa2_eth_priv *priv)
4293 {
4294 	struct device *dev = priv->net_dev->dev.parent;
4295 	int err;
4296 
4297 	/* Check if we actually support Rx flow classification */
4298 	if (dpaa2_eth_has_legacy_dist(priv)) {
4299 		dev_dbg(dev, "Rx cls not supported by current MC version\n");
4300 		return -EOPNOTSUPP;
4301 	}
4302 
4303 	if (!dpaa2_eth_fs_enabled(priv)) {
4304 		dev_dbg(dev, "Rx cls disabled in DPNI options\n");
4305 		return -EOPNOTSUPP;
4306 	}
4307 
4308 	if (!dpaa2_eth_hash_enabled(priv)) {
4309 		dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
4310 		return -EOPNOTSUPP;
4311 	}
4312 
4313 	/* If there is no support for masking in the classification table,
4314 	 * we don't set a default key, as it will depend on the rules
4315 	 * added by the user at runtime.
4316 	 */
4317 	if (!dpaa2_eth_fs_mask_enabled(priv))
4318 		goto out;
4319 
4320 	err = dpaa2_eth_set_cls(priv->net_dev, DPAA2_ETH_DIST_ALL);
4321 	if (err)
4322 		return err;
4323 
4324 out:
4325 	priv->rx_cls_enabled = 1;
4326 
4327 	return 0;
4328 }
4329 
4330 /* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
4331  * frame queues and channels
4332  */
4333 static int dpaa2_eth_bind_dpni(struct dpaa2_eth_priv *priv)
4334 {
4335 	struct dpaa2_eth_bp *bp = priv->bp[DPAA2_ETH_DEFAULT_BP_IDX];
4336 	struct net_device *net_dev = priv->net_dev;
4337 	struct dpni_pools_cfg pools_params = { 0 };
4338 	struct device *dev = net_dev->dev.parent;
4339 	struct dpni_error_cfg err_cfg;
4340 	int err = 0;
4341 	int i;
4342 
4343 	pools_params.num_dpbp = 1;
4344 	pools_params.pools[0].dpbp_id = bp->dev->obj_desc.id;
4345 	pools_params.pools[0].backup_pool = 0;
4346 	pools_params.pools[0].buffer_size = priv->rx_buf_size;
4347 	err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
4348 	if (err) {
4349 		dev_err(dev, "dpni_set_pools() failed\n");
4350 		return err;
4351 	}
4352 
4353 	/* have the interface implicitly distribute traffic based on
4354 	 * the default hash key
4355 	 */
4356 	err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
4357 	if (err && err != -EOPNOTSUPP)
4358 		dev_err(dev, "Failed to configure hashing\n");
4359 
4360 	/* Configure the flow classification key; it includes all
4361 	 * supported header fields and cannot be modified at runtime
4362 	 */
4363 	err = dpaa2_eth_set_default_cls(priv);
4364 	if (err && err != -EOPNOTSUPP)
4365 		dev_err(dev, "Failed to configure Rx classification key\n");
4366 
4367 	/* Configure handling of error frames */
4368 	err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
4369 	err_cfg.set_frame_annotation = 1;
4370 	err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
4371 	err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
4372 				       &err_cfg);
4373 	if (err) {
4374 		dev_err(dev, "dpni_set_errors_behavior failed\n");
4375 		return err;
4376 	}
4377 
4378 	/* Configure Rx and Tx conf queues to generate CDANs */
4379 	for (i = 0; i < priv->num_fqs; i++) {
4380 		switch (priv->fq[i].type) {
4381 		case DPAA2_RX_FQ:
4382 			err = dpaa2_eth_setup_rx_flow(priv, &priv->fq[i]);
4383 			break;
4384 		case DPAA2_TX_CONF_FQ:
4385 			err = dpaa2_eth_setup_tx_flow(priv, &priv->fq[i]);
4386 			break;
4387 		case DPAA2_RX_ERR_FQ:
4388 			err = setup_rx_err_flow(priv, &priv->fq[i]);
4389 			break;
4390 		default:
4391 			dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
4392 			return -EINVAL;
4393 		}
4394 		if (err)
4395 			return err;
4396 	}
4397 
4398 	err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
4399 			    DPNI_QUEUE_TX, &priv->tx_qdid);
4400 	if (err) {
4401 		dev_err(dev, "dpni_get_qdid() failed\n");
4402 		return err;
4403 	}
4404 
4405 	return 0;
4406 }
4407 
4408 /* Allocate rings for storing incoming frame descriptors */
4409 static int dpaa2_eth_alloc_rings(struct dpaa2_eth_priv *priv)
4410 {
4411 	struct net_device *net_dev = priv->net_dev;
4412 	struct device *dev = net_dev->dev.parent;
4413 	int i;
4414 
4415 	for (i = 0; i < priv->num_channels; i++) {
4416 		priv->channel[i]->store =
4417 			dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
4418 		if (!priv->channel[i]->store) {
4419 			netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
4420 			goto err_ring;
4421 		}
4422 	}
4423 
4424 	return 0;
4425 
4426 err_ring:
4427 	for (i = 0; i < priv->num_channels; i++) {
4428 		if (!priv->channel[i]->store)
4429 			break;
4430 		dpaa2_io_store_destroy(priv->channel[i]->store);
4431 	}
4432 
4433 	return -ENOMEM;
4434 }
4435 
4436 static void dpaa2_eth_free_rings(struct dpaa2_eth_priv *priv)
4437 {
4438 	int i;
4439 
4440 	for (i = 0; i < priv->num_channels; i++)
4441 		dpaa2_io_store_destroy(priv->channel[i]->store);
4442 }
4443 
4444 static int dpaa2_eth_set_mac_addr(struct dpaa2_eth_priv *priv)
4445 {
4446 	struct net_device *net_dev = priv->net_dev;
4447 	struct device *dev = net_dev->dev.parent;
4448 	u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
4449 	int err;
4450 
4451 	/* Get firmware address, if any */
4452 	err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
4453 	if (err) {
4454 		dev_err(dev, "dpni_get_port_mac_addr() failed\n");
4455 		return err;
4456 	}
4457 
4458 	/* Get DPNI attributes address, if any */
4459 	err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
4460 					dpni_mac_addr);
4461 	if (err) {
4462 		dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
4463 		return err;
4464 	}
4465 
4466 	/* First check if firmware has any address configured by bootloader */
4467 	if (!is_zero_ether_addr(mac_addr)) {
4468 		/* If the DPMAC addr != DPNI addr, update it */
4469 		if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
4470 			err = dpni_set_primary_mac_addr(priv->mc_io, 0,
4471 							priv->mc_token,
4472 							mac_addr);
4473 			if (err) {
4474 				dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
4475 				return err;
4476 			}
4477 		}
4478 		eth_hw_addr_set(net_dev, mac_addr);
4479 	} else if (is_zero_ether_addr(dpni_mac_addr)) {
4480 		/* No MAC address configured, fill in net_dev->dev_addr
4481 		 * with a random one
4482 		 */
4483 		eth_hw_addr_random(net_dev);
4484 		dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
4485 
4486 		err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
4487 						net_dev->dev_addr);
4488 		if (err) {
4489 			dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
4490 			return err;
4491 		}
4492 
4493 		/* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
4494 		 * practical purposes, this will be our "permanent" mac address,
4495 		 * at least until the next reboot. This move will also permit
4496 		 * register_netdevice() to properly fill up net_dev->perm_addr.
4497 		 */
4498 		net_dev->addr_assign_type = NET_ADDR_PERM;
4499 	} else {
4500 		/* NET_ADDR_PERM is default, all we have to do is
4501 		 * fill in the device addr.
4502 		 */
4503 		eth_hw_addr_set(net_dev, dpni_mac_addr);
4504 	}
4505 
4506 	return 0;
4507 }
4508 
4509 static int dpaa2_eth_netdev_init(struct net_device *net_dev)
4510 {
4511 	struct device *dev = net_dev->dev.parent;
4512 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4513 	u32 options = priv->dpni_attrs.options;
4514 	u64 supported = 0, not_supported = 0;
4515 	u8 bcast_addr[ETH_ALEN];
4516 	u8 num_queues;
4517 	int err;
4518 
4519 	net_dev->netdev_ops = &dpaa2_eth_ops;
4520 	net_dev->ethtool_ops = &dpaa2_ethtool_ops;
4521 
4522 	err = dpaa2_eth_set_mac_addr(priv);
4523 	if (err)
4524 		return err;
4525 
4526 	/* Explicitly add the broadcast address to the MAC filtering table */
4527 	eth_broadcast_addr(bcast_addr);
4528 	err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
4529 	if (err) {
4530 		dev_err(dev, "dpni_add_mac_addr() failed\n");
4531 		return err;
4532 	}
4533 
4534 	/* Set MTU upper limit; lower limit is 68B (default value) */
4535 	net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
4536 	err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
4537 					DPAA2_ETH_MFL);
4538 	if (err) {
4539 		dev_err(dev, "dpni_set_max_frame_length() failed\n");
4540 		return err;
4541 	}
4542 
4543 	/* Set actual number of queues in the net device */
4544 	num_queues = dpaa2_eth_queue_count(priv);
4545 	err = netif_set_real_num_tx_queues(net_dev, num_queues);
4546 	if (err) {
4547 		dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
4548 		return err;
4549 	}
4550 	err = netif_set_real_num_rx_queues(net_dev, num_queues);
4551 	if (err) {
4552 		dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
4553 		return err;
4554 	}
4555 
4556 	dpaa2_eth_detect_features(priv);
4557 
4558 	/* Capabilities listing */
4559 	supported |= IFF_LIVE_ADDR_CHANGE;
4560 
4561 	if (options & DPNI_OPT_NO_MAC_FILTER)
4562 		not_supported |= IFF_UNICAST_FLT;
4563 	else
4564 		supported |= IFF_UNICAST_FLT;
4565 
4566 	net_dev->priv_flags |= supported;
4567 	net_dev->priv_flags &= ~not_supported;
4568 
4569 	/* Features */
4570 	net_dev->features = NETIF_F_RXCSUM |
4571 			    NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
4572 			    NETIF_F_SG | NETIF_F_HIGHDMA |
4573 			    NETIF_F_LLTX | NETIF_F_HW_TC | NETIF_F_TSO;
4574 	net_dev->gso_max_segs = DPAA2_ETH_ENQUEUE_MAX_FDS;
4575 	net_dev->hw_features = net_dev->features;
4576 
4577 	if (priv->dpni_attrs.vlan_filter_entries)
4578 		net_dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4579 
4580 	return 0;
4581 }
4582 
4583 static int dpaa2_eth_poll_link_state(void *arg)
4584 {
4585 	struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
4586 	int err;
4587 
4588 	while (!kthread_should_stop()) {
4589 		err = dpaa2_eth_link_state_update(priv);
4590 		if (unlikely(err))
4591 			return err;
4592 
4593 		msleep(DPAA2_ETH_LINK_STATE_REFRESH);
4594 	}
4595 
4596 	return 0;
4597 }
4598 
4599 static int dpaa2_eth_connect_mac(struct dpaa2_eth_priv *priv)
4600 {
4601 	struct fsl_mc_device *dpni_dev, *dpmac_dev;
4602 	struct dpaa2_mac *mac;
4603 	int err;
4604 
4605 	dpni_dev = to_fsl_mc_device(priv->net_dev->dev.parent);
4606 	dpmac_dev = fsl_mc_get_endpoint(dpni_dev, 0);
4607 
4608 	if (PTR_ERR(dpmac_dev) == -EPROBE_DEFER)
4609 		return PTR_ERR(dpmac_dev);
4610 
4611 	if (IS_ERR(dpmac_dev) || dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type)
4612 		return 0;
4613 
4614 	mac = kzalloc(sizeof(struct dpaa2_mac), GFP_KERNEL);
4615 	if (!mac)
4616 		return -ENOMEM;
4617 
4618 	mac->mc_dev = dpmac_dev;
4619 	mac->mc_io = priv->mc_io;
4620 	mac->net_dev = priv->net_dev;
4621 
4622 	err = dpaa2_mac_open(mac);
4623 	if (err)
4624 		goto err_free_mac;
4625 	priv->mac = mac;
4626 
4627 	if (dpaa2_eth_is_type_phy(priv)) {
4628 		err = dpaa2_mac_connect(mac);
4629 		if (err && err != -EPROBE_DEFER)
4630 			netdev_err(priv->net_dev, "Error connecting to the MAC endpoint: %pe",
4631 				   ERR_PTR(err));
4632 		if (err)
4633 			goto err_close_mac;
4634 	}
4635 
4636 	return 0;
4637 
4638 err_close_mac:
4639 	dpaa2_mac_close(mac);
4640 	priv->mac = NULL;
4641 err_free_mac:
4642 	kfree(mac);
4643 	return err;
4644 }
4645 
4646 static void dpaa2_eth_disconnect_mac(struct dpaa2_eth_priv *priv)
4647 {
4648 	if (dpaa2_eth_is_type_phy(priv))
4649 		dpaa2_mac_disconnect(priv->mac);
4650 
4651 	if (!dpaa2_eth_has_mac(priv))
4652 		return;
4653 
4654 	dpaa2_mac_close(priv->mac);
4655 	kfree(priv->mac);
4656 	priv->mac = NULL;
4657 }
4658 
4659 static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
4660 {
4661 	u32 status = ~0;
4662 	struct device *dev = (struct device *)arg;
4663 	struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
4664 	struct net_device *net_dev = dev_get_drvdata(dev);
4665 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4666 	int err;
4667 
4668 	err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
4669 				  DPNI_IRQ_INDEX, &status);
4670 	if (unlikely(err)) {
4671 		netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
4672 		return IRQ_HANDLED;
4673 	}
4674 
4675 	if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
4676 		dpaa2_eth_link_state_update(netdev_priv(net_dev));
4677 
4678 	if (status & DPNI_IRQ_EVENT_ENDPOINT_CHANGED) {
4679 		dpaa2_eth_set_mac_addr(netdev_priv(net_dev));
4680 		dpaa2_eth_update_tx_fqids(priv);
4681 
4682 		rtnl_lock();
4683 		if (dpaa2_eth_has_mac(priv))
4684 			dpaa2_eth_disconnect_mac(priv);
4685 		else
4686 			dpaa2_eth_connect_mac(priv);
4687 		rtnl_unlock();
4688 	}
4689 
4690 	return IRQ_HANDLED;
4691 }
4692 
4693 static int dpaa2_eth_setup_irqs(struct fsl_mc_device *ls_dev)
4694 {
4695 	int err = 0;
4696 	struct fsl_mc_device_irq *irq;
4697 
4698 	err = fsl_mc_allocate_irqs(ls_dev);
4699 	if (err) {
4700 		dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
4701 		return err;
4702 	}
4703 
4704 	irq = ls_dev->irqs[0];
4705 	err = devm_request_threaded_irq(&ls_dev->dev, irq->virq,
4706 					NULL, dpni_irq0_handler_thread,
4707 					IRQF_NO_SUSPEND | IRQF_ONESHOT,
4708 					dev_name(&ls_dev->dev), &ls_dev->dev);
4709 	if (err < 0) {
4710 		dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
4711 		goto free_mc_irq;
4712 	}
4713 
4714 	err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
4715 				DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED |
4716 				DPNI_IRQ_EVENT_ENDPOINT_CHANGED);
4717 	if (err < 0) {
4718 		dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
4719 		goto free_irq;
4720 	}
4721 
4722 	err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
4723 				  DPNI_IRQ_INDEX, 1);
4724 	if (err < 0) {
4725 		dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
4726 		goto free_irq;
4727 	}
4728 
4729 	return 0;
4730 
4731 free_irq:
4732 	devm_free_irq(&ls_dev->dev, irq->virq, &ls_dev->dev);
4733 free_mc_irq:
4734 	fsl_mc_free_irqs(ls_dev);
4735 
4736 	return err;
4737 }
4738 
4739 static void dpaa2_eth_add_ch_napi(struct dpaa2_eth_priv *priv)
4740 {
4741 	int i;
4742 	struct dpaa2_eth_channel *ch;
4743 
4744 	for (i = 0; i < priv->num_channels; i++) {
4745 		ch = priv->channel[i];
4746 		/* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
4747 		netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll);
4748 	}
4749 }
4750 
4751 static void dpaa2_eth_del_ch_napi(struct dpaa2_eth_priv *priv)
4752 {
4753 	int i;
4754 	struct dpaa2_eth_channel *ch;
4755 
4756 	for (i = 0; i < priv->num_channels; i++) {
4757 		ch = priv->channel[i];
4758 		netif_napi_del(&ch->napi);
4759 	}
4760 }
4761 
4762 static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
4763 {
4764 	struct device *dev;
4765 	struct net_device *net_dev = NULL;
4766 	struct dpaa2_eth_priv *priv = NULL;
4767 	int err = 0;
4768 
4769 	dev = &dpni_dev->dev;
4770 
4771 	/* Net device */
4772 	net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_NETDEV_QUEUES);
4773 	if (!net_dev) {
4774 		dev_err(dev, "alloc_etherdev_mq() failed\n");
4775 		return -ENOMEM;
4776 	}
4777 
4778 	SET_NETDEV_DEV(net_dev, dev);
4779 	dev_set_drvdata(dev, net_dev);
4780 
4781 	priv = netdev_priv(net_dev);
4782 	priv->net_dev = net_dev;
4783 
4784 	priv->iommu_domain = iommu_get_domain_for_dev(dev);
4785 
4786 	priv->tx_tstamp_type = HWTSTAMP_TX_OFF;
4787 	priv->rx_tstamp = false;
4788 
4789 	priv->dpaa2_ptp_wq = alloc_workqueue("dpaa2_ptp_wq", 0, 0);
4790 	if (!priv->dpaa2_ptp_wq) {
4791 		err = -ENOMEM;
4792 		goto err_wq_alloc;
4793 	}
4794 
4795 	INIT_WORK(&priv->tx_onestep_tstamp, dpaa2_eth_tx_onestep_tstamp);
4796 	mutex_init(&priv->onestep_tstamp_lock);
4797 	skb_queue_head_init(&priv->tx_skbs);
4798 
4799 	priv->rx_copybreak = DPAA2_ETH_DEFAULT_COPYBREAK;
4800 
4801 	/* Obtain a MC portal */
4802 	err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
4803 				     &priv->mc_io);
4804 	if (err) {
4805 		if (err == -ENXIO)
4806 			err = -EPROBE_DEFER;
4807 		else
4808 			dev_err(dev, "MC portal allocation failed\n");
4809 		goto err_portal_alloc;
4810 	}
4811 
4812 	/* MC objects initialization and configuration */
4813 	err = dpaa2_eth_setup_dpni(dpni_dev);
4814 	if (err)
4815 		goto err_dpni_setup;
4816 
4817 	err = dpaa2_eth_setup_dpio(priv);
4818 	if (err)
4819 		goto err_dpio_setup;
4820 
4821 	dpaa2_eth_setup_fqs(priv);
4822 
4823 	err = dpaa2_eth_setup_default_dpbp(priv);
4824 	if (err)
4825 		goto err_dpbp_setup;
4826 
4827 	err = dpaa2_eth_bind_dpni(priv);
4828 	if (err)
4829 		goto err_bind;
4830 
4831 	/* Add a NAPI context for each channel */
4832 	dpaa2_eth_add_ch_napi(priv);
4833 
4834 	/* Percpu statistics */
4835 	priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
4836 	if (!priv->percpu_stats) {
4837 		dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
4838 		err = -ENOMEM;
4839 		goto err_alloc_percpu_stats;
4840 	}
4841 	priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
4842 	if (!priv->percpu_extras) {
4843 		dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
4844 		err = -ENOMEM;
4845 		goto err_alloc_percpu_extras;
4846 	}
4847 
4848 	priv->sgt_cache = alloc_percpu(*priv->sgt_cache);
4849 	if (!priv->sgt_cache) {
4850 		dev_err(dev, "alloc_percpu(sgt_cache) failed\n");
4851 		err = -ENOMEM;
4852 		goto err_alloc_sgt_cache;
4853 	}
4854 
4855 	priv->fd = alloc_percpu(*priv->fd);
4856 	if (!priv->fd) {
4857 		dev_err(dev, "alloc_percpu(fds) failed\n");
4858 		err = -ENOMEM;
4859 		goto err_alloc_fds;
4860 	}
4861 
4862 	err = dpaa2_eth_netdev_init(net_dev);
4863 	if (err)
4864 		goto err_netdev_init;
4865 
4866 	/* Configure checksum offload based on current interface flags */
4867 	err = dpaa2_eth_set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
4868 	if (err)
4869 		goto err_csum;
4870 
4871 	err = dpaa2_eth_set_tx_csum(priv,
4872 				    !!(net_dev->features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
4873 	if (err)
4874 		goto err_csum;
4875 
4876 	err = dpaa2_eth_alloc_rings(priv);
4877 	if (err)
4878 		goto err_alloc_rings;
4879 
4880 #ifdef CONFIG_FSL_DPAA2_ETH_DCB
4881 	if (dpaa2_eth_has_pause_support(priv) && priv->vlan_cls_enabled) {
4882 		priv->dcbx_mode = DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE;
4883 		net_dev->dcbnl_ops = &dpaa2_eth_dcbnl_ops;
4884 	} else {
4885 		dev_dbg(dev, "PFC not supported\n");
4886 	}
4887 #endif
4888 
4889 	err = dpaa2_eth_setup_irqs(dpni_dev);
4890 	if (err) {
4891 		netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
4892 		priv->poll_thread = kthread_run(dpaa2_eth_poll_link_state, priv,
4893 						"%s_poll_link", net_dev->name);
4894 		if (IS_ERR(priv->poll_thread)) {
4895 			dev_err(dev, "Error starting polling thread\n");
4896 			goto err_poll_thread;
4897 		}
4898 		priv->do_link_poll = true;
4899 	}
4900 
4901 	err = dpaa2_eth_connect_mac(priv);
4902 	if (err)
4903 		goto err_connect_mac;
4904 
4905 	err = dpaa2_eth_dl_alloc(priv);
4906 	if (err)
4907 		goto err_dl_register;
4908 
4909 	err = dpaa2_eth_dl_traps_register(priv);
4910 	if (err)
4911 		goto err_dl_trap_register;
4912 
4913 	err = dpaa2_eth_dl_port_add(priv);
4914 	if (err)
4915 		goto err_dl_port_add;
4916 
4917 	err = register_netdev(net_dev);
4918 	if (err < 0) {
4919 		dev_err(dev, "register_netdev() failed\n");
4920 		goto err_netdev_reg;
4921 	}
4922 
4923 #ifdef CONFIG_DEBUG_FS
4924 	dpaa2_dbg_add(priv);
4925 #endif
4926 
4927 	dpaa2_eth_dl_register(priv);
4928 	dev_info(dev, "Probed interface %s\n", net_dev->name);
4929 	return 0;
4930 
4931 err_netdev_reg:
4932 	dpaa2_eth_dl_port_del(priv);
4933 err_dl_port_add:
4934 	dpaa2_eth_dl_traps_unregister(priv);
4935 err_dl_trap_register:
4936 	dpaa2_eth_dl_free(priv);
4937 err_dl_register:
4938 	dpaa2_eth_disconnect_mac(priv);
4939 err_connect_mac:
4940 	if (priv->do_link_poll)
4941 		kthread_stop(priv->poll_thread);
4942 	else
4943 		fsl_mc_free_irqs(dpni_dev);
4944 err_poll_thread:
4945 	dpaa2_eth_free_rings(priv);
4946 err_alloc_rings:
4947 err_csum:
4948 err_netdev_init:
4949 	free_percpu(priv->fd);
4950 err_alloc_fds:
4951 	free_percpu(priv->sgt_cache);
4952 err_alloc_sgt_cache:
4953 	free_percpu(priv->percpu_extras);
4954 err_alloc_percpu_extras:
4955 	free_percpu(priv->percpu_stats);
4956 err_alloc_percpu_stats:
4957 	dpaa2_eth_del_ch_napi(priv);
4958 err_bind:
4959 	dpaa2_eth_free_dpbps(priv);
4960 err_dpbp_setup:
4961 	dpaa2_eth_free_dpio(priv);
4962 err_dpio_setup:
4963 	dpaa2_eth_free_dpni(priv);
4964 err_dpni_setup:
4965 	fsl_mc_portal_free(priv->mc_io);
4966 err_portal_alloc:
4967 	destroy_workqueue(priv->dpaa2_ptp_wq);
4968 err_wq_alloc:
4969 	dev_set_drvdata(dev, NULL);
4970 	free_netdev(net_dev);
4971 
4972 	return err;
4973 }
4974 
4975 static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
4976 {
4977 	struct device *dev;
4978 	struct net_device *net_dev;
4979 	struct dpaa2_eth_priv *priv;
4980 
4981 	dev = &ls_dev->dev;
4982 	net_dev = dev_get_drvdata(dev);
4983 	priv = netdev_priv(net_dev);
4984 
4985 	dpaa2_eth_dl_unregister(priv);
4986 
4987 #ifdef CONFIG_DEBUG_FS
4988 	dpaa2_dbg_remove(priv);
4989 #endif
4990 
4991 	unregister_netdev(net_dev);
4992 	rtnl_lock();
4993 	dpaa2_eth_disconnect_mac(priv);
4994 	rtnl_unlock();
4995 
4996 	dpaa2_eth_dl_port_del(priv);
4997 	dpaa2_eth_dl_traps_unregister(priv);
4998 	dpaa2_eth_dl_free(priv);
4999 
5000 	if (priv->do_link_poll)
5001 		kthread_stop(priv->poll_thread);
5002 	else
5003 		fsl_mc_free_irqs(ls_dev);
5004 
5005 	dpaa2_eth_free_rings(priv);
5006 	free_percpu(priv->fd);
5007 	free_percpu(priv->sgt_cache);
5008 	free_percpu(priv->percpu_stats);
5009 	free_percpu(priv->percpu_extras);
5010 
5011 	dpaa2_eth_del_ch_napi(priv);
5012 	dpaa2_eth_free_dpbps(priv);
5013 	dpaa2_eth_free_dpio(priv);
5014 	dpaa2_eth_free_dpni(priv);
5015 	if (priv->onestep_reg_base)
5016 		iounmap(priv->onestep_reg_base);
5017 
5018 	fsl_mc_portal_free(priv->mc_io);
5019 
5020 	destroy_workqueue(priv->dpaa2_ptp_wq);
5021 
5022 	dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
5023 
5024 	free_netdev(net_dev);
5025 
5026 	return 0;
5027 }
5028 
5029 static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
5030 	{
5031 		.vendor = FSL_MC_VENDOR_FREESCALE,
5032 		.obj_type = "dpni",
5033 	},
5034 	{ .vendor = 0x0 }
5035 };
5036 MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
5037 
5038 static struct fsl_mc_driver dpaa2_eth_driver = {
5039 	.driver = {
5040 		.name = KBUILD_MODNAME,
5041 		.owner = THIS_MODULE,
5042 	},
5043 	.probe = dpaa2_eth_probe,
5044 	.remove = dpaa2_eth_remove,
5045 	.match_id_table = dpaa2_eth_match_id_table
5046 };
5047 
5048 static int __init dpaa2_eth_driver_init(void)
5049 {
5050 	int err;
5051 
5052 	dpaa2_eth_dbg_init();
5053 	err = fsl_mc_driver_register(&dpaa2_eth_driver);
5054 	if (err) {
5055 		dpaa2_eth_dbg_exit();
5056 		return err;
5057 	}
5058 
5059 	return 0;
5060 }
5061 
5062 static void __exit dpaa2_eth_driver_exit(void)
5063 {
5064 	dpaa2_eth_dbg_exit();
5065 	fsl_mc_driver_unregister(&dpaa2_eth_driver);
5066 }
5067 
5068 module_init(dpaa2_eth_driver_init);
5069 module_exit(dpaa2_eth_driver_exit);
5070