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 			dev_dbg(dev, "Waiting for DPCON\n");
3043 			err = -EPROBE_DEFER;
3044 		} else {
3045 			dev_info(dev, "Not enough DPCONs, will go on as-is\n");
3046 		}
3047 		return ERR_PTR(err);
3048 	}
3049 
3050 	err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
3051 	if (err) {
3052 		dev_err(dev, "dpcon_open() failed\n");
3053 		goto free;
3054 	}
3055 
3056 	err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
3057 	if (err) {
3058 		dev_err(dev, "dpcon_reset() failed\n");
3059 		goto close;
3060 	}
3061 
3062 	err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
3063 	if (err) {
3064 		dev_err(dev, "dpcon_enable() failed\n");
3065 		goto close;
3066 	}
3067 
3068 	return dpcon;
3069 
3070 close:
3071 	dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
3072 free:
3073 	fsl_mc_object_free(dpcon);
3074 
3075 	return ERR_PTR(err);
3076 }
3077 
3078 static void dpaa2_eth_free_dpcon(struct dpaa2_eth_priv *priv,
3079 				 struct fsl_mc_device *dpcon)
3080 {
3081 	dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
3082 	dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
3083 	fsl_mc_object_free(dpcon);
3084 }
3085 
3086 static struct dpaa2_eth_channel *dpaa2_eth_alloc_channel(struct dpaa2_eth_priv *priv)
3087 {
3088 	struct dpaa2_eth_channel *channel;
3089 	struct dpcon_attr attr;
3090 	struct device *dev = priv->net_dev->dev.parent;
3091 	int err;
3092 
3093 	channel = kzalloc(sizeof(*channel), GFP_KERNEL);
3094 	if (!channel)
3095 		return NULL;
3096 
3097 	channel->dpcon = dpaa2_eth_setup_dpcon(priv);
3098 	if (IS_ERR(channel->dpcon)) {
3099 		err = PTR_ERR(channel->dpcon);
3100 		goto err_setup;
3101 	}
3102 
3103 	err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
3104 				   &attr);
3105 	if (err) {
3106 		dev_err(dev, "dpcon_get_attributes() failed\n");
3107 		goto err_get_attr;
3108 	}
3109 
3110 	channel->dpcon_id = attr.id;
3111 	channel->ch_id = attr.qbman_ch_id;
3112 	channel->priv = priv;
3113 
3114 	return channel;
3115 
3116 err_get_attr:
3117 	dpaa2_eth_free_dpcon(priv, channel->dpcon);
3118 err_setup:
3119 	kfree(channel);
3120 	return ERR_PTR(err);
3121 }
3122 
3123 static void dpaa2_eth_free_channel(struct dpaa2_eth_priv *priv,
3124 				   struct dpaa2_eth_channel *channel)
3125 {
3126 	dpaa2_eth_free_dpcon(priv, channel->dpcon);
3127 	kfree(channel);
3128 }
3129 
3130 /* DPIO setup: allocate and configure QBMan channels, setup core affinity
3131  * and register data availability notifications
3132  */
3133 static int dpaa2_eth_setup_dpio(struct dpaa2_eth_priv *priv)
3134 {
3135 	struct dpaa2_io_notification_ctx *nctx;
3136 	struct dpaa2_eth_channel *channel;
3137 	struct dpcon_notification_cfg dpcon_notif_cfg;
3138 	struct device *dev = priv->net_dev->dev.parent;
3139 	int i, err;
3140 
3141 	/* We want the ability to spread ingress traffic (RX, TX conf) to as
3142 	 * many cores as possible, so we need one channel for each core
3143 	 * (unless there's fewer queues than cores, in which case the extra
3144 	 * channels would be wasted).
3145 	 * Allocate one channel per core and register it to the core's
3146 	 * affine DPIO. If not enough channels are available for all cores
3147 	 * or if some cores don't have an affine DPIO, there will be no
3148 	 * ingress frame processing on those cores.
3149 	 */
3150 	cpumask_clear(&priv->dpio_cpumask);
3151 	for_each_online_cpu(i) {
3152 		/* Try to allocate a channel */
3153 		channel = dpaa2_eth_alloc_channel(priv);
3154 		if (IS_ERR_OR_NULL(channel)) {
3155 			err = PTR_ERR_OR_ZERO(channel);
3156 			if (err == -EPROBE_DEFER)
3157 				dev_dbg(dev, "waiting for affine channel\n");
3158 			else
3159 				dev_info(dev,
3160 					 "No affine channel for cpu %d and above\n", i);
3161 			goto err_alloc_ch;
3162 		}
3163 
3164 		priv->channel[priv->num_channels] = channel;
3165 
3166 		nctx = &channel->nctx;
3167 		nctx->is_cdan = 1;
3168 		nctx->cb = dpaa2_eth_cdan_cb;
3169 		nctx->id = channel->ch_id;
3170 		nctx->desired_cpu = i;
3171 
3172 		/* Register the new context */
3173 		channel->dpio = dpaa2_io_service_select(i);
3174 		err = dpaa2_io_service_register(channel->dpio, nctx, dev);
3175 		if (err) {
3176 			dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
3177 			/* If no affine DPIO for this core, there's probably
3178 			 * none available for next cores either. Signal we want
3179 			 * to retry later, in case the DPIO devices weren't
3180 			 * probed yet.
3181 			 */
3182 			err = -EPROBE_DEFER;
3183 			goto err_service_reg;
3184 		}
3185 
3186 		/* Register DPCON notification with MC */
3187 		dpcon_notif_cfg.dpio_id = nctx->dpio_id;
3188 		dpcon_notif_cfg.priority = 0;
3189 		dpcon_notif_cfg.user_ctx = nctx->qman64;
3190 		err = dpcon_set_notification(priv->mc_io, 0,
3191 					     channel->dpcon->mc_handle,
3192 					     &dpcon_notif_cfg);
3193 		if (err) {
3194 			dev_err(dev, "dpcon_set_notification failed()\n");
3195 			goto err_set_cdan;
3196 		}
3197 
3198 		/* If we managed to allocate a channel and also found an affine
3199 		 * DPIO for this core, add it to the final mask
3200 		 */
3201 		cpumask_set_cpu(i, &priv->dpio_cpumask);
3202 		priv->num_channels++;
3203 
3204 		/* Stop if we already have enough channels to accommodate all
3205 		 * RX and TX conf queues
3206 		 */
3207 		if (priv->num_channels == priv->dpni_attrs.num_queues)
3208 			break;
3209 	}
3210 
3211 	return 0;
3212 
3213 err_set_cdan:
3214 	dpaa2_io_service_deregister(channel->dpio, nctx, dev);
3215 err_service_reg:
3216 	dpaa2_eth_free_channel(priv, channel);
3217 err_alloc_ch:
3218 	if (err == -EPROBE_DEFER) {
3219 		for (i = 0; i < priv->num_channels; i++) {
3220 			channel = priv->channel[i];
3221 			nctx = &channel->nctx;
3222 			dpaa2_io_service_deregister(channel->dpio, nctx, dev);
3223 			dpaa2_eth_free_channel(priv, channel);
3224 		}
3225 		priv->num_channels = 0;
3226 		return err;
3227 	}
3228 
3229 	if (cpumask_empty(&priv->dpio_cpumask)) {
3230 		dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
3231 		return -ENODEV;
3232 	}
3233 
3234 	dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
3235 		 cpumask_pr_args(&priv->dpio_cpumask));
3236 
3237 	return 0;
3238 }
3239 
3240 static void dpaa2_eth_free_dpio(struct dpaa2_eth_priv *priv)
3241 {
3242 	struct device *dev = priv->net_dev->dev.parent;
3243 	struct dpaa2_eth_channel *ch;
3244 	int i;
3245 
3246 	/* deregister CDAN notifications and free channels */
3247 	for (i = 0; i < priv->num_channels; i++) {
3248 		ch = priv->channel[i];
3249 		dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev);
3250 		dpaa2_eth_free_channel(priv, ch);
3251 	}
3252 }
3253 
3254 static struct dpaa2_eth_channel *dpaa2_eth_get_affine_channel(struct dpaa2_eth_priv *priv,
3255 							      int cpu)
3256 {
3257 	struct device *dev = priv->net_dev->dev.parent;
3258 	int i;
3259 
3260 	for (i = 0; i < priv->num_channels; i++)
3261 		if (priv->channel[i]->nctx.desired_cpu == cpu)
3262 			return priv->channel[i];
3263 
3264 	/* We should never get here. Issue a warning and return
3265 	 * the first channel, because it's still better than nothing
3266 	 */
3267 	dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
3268 
3269 	return priv->channel[0];
3270 }
3271 
3272 static void dpaa2_eth_set_fq_affinity(struct dpaa2_eth_priv *priv)
3273 {
3274 	struct device *dev = priv->net_dev->dev.parent;
3275 	struct dpaa2_eth_fq *fq;
3276 	int rx_cpu, txc_cpu;
3277 	int i;
3278 
3279 	/* For each FQ, pick one channel/CPU to deliver frames to.
3280 	 * This may well change at runtime, either through irqbalance or
3281 	 * through direct user intervention.
3282 	 */
3283 	rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
3284 
3285 	for (i = 0; i < priv->num_fqs; i++) {
3286 		fq = &priv->fq[i];
3287 		switch (fq->type) {
3288 		case DPAA2_RX_FQ:
3289 		case DPAA2_RX_ERR_FQ:
3290 			fq->target_cpu = rx_cpu;
3291 			rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
3292 			if (rx_cpu >= nr_cpu_ids)
3293 				rx_cpu = cpumask_first(&priv->dpio_cpumask);
3294 			break;
3295 		case DPAA2_TX_CONF_FQ:
3296 			fq->target_cpu = txc_cpu;
3297 			txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
3298 			if (txc_cpu >= nr_cpu_ids)
3299 				txc_cpu = cpumask_first(&priv->dpio_cpumask);
3300 			break;
3301 		default:
3302 			dev_err(dev, "Unknown FQ type: %d\n", fq->type);
3303 		}
3304 		fq->channel = dpaa2_eth_get_affine_channel(priv, fq->target_cpu);
3305 	}
3306 
3307 	update_xps(priv);
3308 }
3309 
3310 static void dpaa2_eth_setup_fqs(struct dpaa2_eth_priv *priv)
3311 {
3312 	int i, j;
3313 
3314 	/* We have one TxConf FQ per Tx flow.
3315 	 * The number of Tx and Rx queues is the same.
3316 	 * Tx queues come first in the fq array.
3317 	 */
3318 	for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
3319 		priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
3320 		priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
3321 		priv->fq[priv->num_fqs++].flowid = (u16)i;
3322 	}
3323 
3324 	for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
3325 		for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
3326 			priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
3327 			priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
3328 			priv->fq[priv->num_fqs].tc = (u8)j;
3329 			priv->fq[priv->num_fqs++].flowid = (u16)i;
3330 		}
3331 	}
3332 
3333 	/* We have exactly one Rx error queue per DPNI */
3334 	priv->fq[priv->num_fqs].type = DPAA2_RX_ERR_FQ;
3335 	priv->fq[priv->num_fqs++].consume = dpaa2_eth_rx_err;
3336 
3337 	/* For each FQ, decide on which core to process incoming frames */
3338 	dpaa2_eth_set_fq_affinity(priv);
3339 }
3340 
3341 /* Allocate and configure a buffer pool */
3342 struct dpaa2_eth_bp *dpaa2_eth_allocate_dpbp(struct dpaa2_eth_priv *priv)
3343 {
3344 	struct device *dev = priv->net_dev->dev.parent;
3345 	struct fsl_mc_device *dpbp_dev;
3346 	struct dpbp_attr dpbp_attrs;
3347 	struct dpaa2_eth_bp *bp;
3348 	int err;
3349 
3350 	err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
3351 				     &dpbp_dev);
3352 	if (err) {
3353 		if (err == -ENXIO)
3354 			err = -EPROBE_DEFER;
3355 		else
3356 			dev_err(dev, "DPBP device allocation failed\n");
3357 		return ERR_PTR(err);
3358 	}
3359 
3360 	bp = kzalloc(sizeof(*bp), GFP_KERNEL);
3361 	if (!bp) {
3362 		err = -ENOMEM;
3363 		goto err_alloc;
3364 	}
3365 
3366 	err = dpbp_open(priv->mc_io, 0, dpbp_dev->obj_desc.id,
3367 			&dpbp_dev->mc_handle);
3368 	if (err) {
3369 		dev_err(dev, "dpbp_open() failed\n");
3370 		goto err_open;
3371 	}
3372 
3373 	err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
3374 	if (err) {
3375 		dev_err(dev, "dpbp_reset() failed\n");
3376 		goto err_reset;
3377 	}
3378 
3379 	err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
3380 	if (err) {
3381 		dev_err(dev, "dpbp_enable() failed\n");
3382 		goto err_enable;
3383 	}
3384 
3385 	err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
3386 				  &dpbp_attrs);
3387 	if (err) {
3388 		dev_err(dev, "dpbp_get_attributes() failed\n");
3389 		goto err_get_attr;
3390 	}
3391 
3392 	bp->dev = dpbp_dev;
3393 	bp->bpid = dpbp_attrs.bpid;
3394 
3395 	return bp;
3396 
3397 err_get_attr:
3398 	dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
3399 err_enable:
3400 err_reset:
3401 	dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
3402 err_open:
3403 	kfree(bp);
3404 err_alloc:
3405 	fsl_mc_object_free(dpbp_dev);
3406 
3407 	return ERR_PTR(err);
3408 }
3409 
3410 static int dpaa2_eth_setup_default_dpbp(struct dpaa2_eth_priv *priv)
3411 {
3412 	struct dpaa2_eth_bp *bp;
3413 	int i;
3414 
3415 	bp = dpaa2_eth_allocate_dpbp(priv);
3416 	if (IS_ERR(bp))
3417 		return PTR_ERR(bp);
3418 
3419 	priv->bp[DPAA2_ETH_DEFAULT_BP_IDX] = bp;
3420 	priv->num_bps++;
3421 
3422 	for (i = 0; i < priv->num_channels; i++)
3423 		priv->channel[i]->bp = bp;
3424 
3425 	return 0;
3426 }
3427 
3428 void dpaa2_eth_free_dpbp(struct dpaa2_eth_priv *priv, struct dpaa2_eth_bp *bp)
3429 {
3430 	int idx_bp;
3431 
3432 	/* Find the index at which this BP is stored */
3433 	for (idx_bp = 0; idx_bp < priv->num_bps; idx_bp++)
3434 		if (priv->bp[idx_bp] == bp)
3435 			break;
3436 
3437 	/* Drain the pool and disable the associated MC object */
3438 	dpaa2_eth_drain_pool(priv, bp->bpid);
3439 	dpbp_disable(priv->mc_io, 0, bp->dev->mc_handle);
3440 	dpbp_close(priv->mc_io, 0, bp->dev->mc_handle);
3441 	fsl_mc_object_free(bp->dev);
3442 	kfree(bp);
3443 
3444 	/* Move the last in use DPBP over in this position */
3445 	priv->bp[idx_bp] = priv->bp[priv->num_bps - 1];
3446 	priv->num_bps--;
3447 }
3448 
3449 static void dpaa2_eth_free_dpbps(struct dpaa2_eth_priv *priv)
3450 {
3451 	int i;
3452 
3453 	for (i = 0; i < priv->num_bps; i++)
3454 		dpaa2_eth_free_dpbp(priv, priv->bp[i]);
3455 }
3456 
3457 static int dpaa2_eth_set_buffer_layout(struct dpaa2_eth_priv *priv)
3458 {
3459 	struct device *dev = priv->net_dev->dev.parent;
3460 	struct dpni_buffer_layout buf_layout = {0};
3461 	u16 rx_buf_align;
3462 	int err;
3463 
3464 	/* We need to check for WRIOP version 1.0.0, but depending on the MC
3465 	 * version, this number is not always provided correctly on rev1.
3466 	 * We need to check for both alternatives in this situation.
3467 	 */
3468 	if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
3469 	    priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
3470 		rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
3471 	else
3472 		rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
3473 
3474 	/* We need to ensure that the buffer size seen by WRIOP is a multiple
3475 	 * of 64 or 256 bytes depending on the WRIOP version.
3476 	 */
3477 	priv->rx_buf_size = ALIGN_DOWN(DPAA2_ETH_RX_BUF_SIZE, rx_buf_align);
3478 
3479 	/* tx buffer */
3480 	buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
3481 	buf_layout.pass_timestamp = true;
3482 	buf_layout.pass_frame_status = true;
3483 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
3484 			     DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
3485 			     DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
3486 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3487 				     DPNI_QUEUE_TX, &buf_layout);
3488 	if (err) {
3489 		dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
3490 		return err;
3491 	}
3492 
3493 	/* tx-confirm buffer */
3494 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
3495 			     DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
3496 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3497 				     DPNI_QUEUE_TX_CONFIRM, &buf_layout);
3498 	if (err) {
3499 		dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
3500 		return err;
3501 	}
3502 
3503 	/* Now that we've set our tx buffer layout, retrieve the minimum
3504 	 * required tx data offset.
3505 	 */
3506 	err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
3507 				      &priv->tx_data_offset);
3508 	if (err) {
3509 		dev_err(dev, "dpni_get_tx_data_offset() failed\n");
3510 		return err;
3511 	}
3512 
3513 	if ((priv->tx_data_offset % 64) != 0)
3514 		dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
3515 			 priv->tx_data_offset);
3516 
3517 	/* rx buffer */
3518 	buf_layout.pass_frame_status = true;
3519 	buf_layout.pass_parser_result = true;
3520 	buf_layout.data_align = rx_buf_align;
3521 	buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
3522 	buf_layout.private_data_size = 0;
3523 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
3524 			     DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
3525 			     DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
3526 			     DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
3527 			     DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
3528 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3529 				     DPNI_QUEUE_RX, &buf_layout);
3530 	if (err) {
3531 		dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
3532 		return err;
3533 	}
3534 
3535 	return 0;
3536 }
3537 
3538 #define DPNI_ENQUEUE_FQID_VER_MAJOR	7
3539 #define DPNI_ENQUEUE_FQID_VER_MINOR	9
3540 
3541 static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv,
3542 				       struct dpaa2_eth_fq *fq,
3543 				       struct dpaa2_fd *fd, u8 prio,
3544 				       u32 num_frames __always_unused,
3545 				       int *frames_enqueued)
3546 {
3547 	int err;
3548 
3549 	err = dpaa2_io_service_enqueue_qd(fq->channel->dpio,
3550 					  priv->tx_qdid, prio,
3551 					  fq->tx_qdbin, fd);
3552 	if (!err && frames_enqueued)
3553 		*frames_enqueued = 1;
3554 	return err;
3555 }
3556 
3557 static inline int dpaa2_eth_enqueue_fq_multiple(struct dpaa2_eth_priv *priv,
3558 						struct dpaa2_eth_fq *fq,
3559 						struct dpaa2_fd *fd,
3560 						u8 prio, u32 num_frames,
3561 						int *frames_enqueued)
3562 {
3563 	int err;
3564 
3565 	err = dpaa2_io_service_enqueue_multiple_fq(fq->channel->dpio,
3566 						   fq->tx_fqid[prio],
3567 						   fd, num_frames);
3568 
3569 	if (err == 0)
3570 		return -EBUSY;
3571 
3572 	if (frames_enqueued)
3573 		*frames_enqueued = err;
3574 	return 0;
3575 }
3576 
3577 static void dpaa2_eth_set_enqueue_mode(struct dpaa2_eth_priv *priv)
3578 {
3579 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3580 				   DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3581 		priv->enqueue = dpaa2_eth_enqueue_qd;
3582 	else
3583 		priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
3584 }
3585 
3586 static int dpaa2_eth_set_pause(struct dpaa2_eth_priv *priv)
3587 {
3588 	struct device *dev = priv->net_dev->dev.parent;
3589 	struct dpni_link_cfg link_cfg = {0};
3590 	int err;
3591 
3592 	/* Get the default link options so we don't override other flags */
3593 	err = dpni_get_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
3594 	if (err) {
3595 		dev_err(dev, "dpni_get_link_cfg() failed\n");
3596 		return err;
3597 	}
3598 
3599 	/* By default, enable both Rx and Tx pause frames */
3600 	link_cfg.options |= DPNI_LINK_OPT_PAUSE;
3601 	link_cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
3602 	err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
3603 	if (err) {
3604 		dev_err(dev, "dpni_set_link_cfg() failed\n");
3605 		return err;
3606 	}
3607 
3608 	priv->link_state.options = link_cfg.options;
3609 
3610 	return 0;
3611 }
3612 
3613 static void dpaa2_eth_update_tx_fqids(struct dpaa2_eth_priv *priv)
3614 {
3615 	struct dpni_queue_id qid = {0};
3616 	struct dpaa2_eth_fq *fq;
3617 	struct dpni_queue queue;
3618 	int i, j, err;
3619 
3620 	/* We only use Tx FQIDs for FQID-based enqueue, so check
3621 	 * if DPNI version supports it before updating FQIDs
3622 	 */
3623 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3624 				   DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3625 		return;
3626 
3627 	for (i = 0; i < priv->num_fqs; i++) {
3628 		fq = &priv->fq[i];
3629 		if (fq->type != DPAA2_TX_CONF_FQ)
3630 			continue;
3631 		for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
3632 			err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3633 					     DPNI_QUEUE_TX, j, fq->flowid,
3634 					     &queue, &qid);
3635 			if (err)
3636 				goto out_err;
3637 
3638 			fq->tx_fqid[j] = qid.fqid;
3639 			if (fq->tx_fqid[j] == 0)
3640 				goto out_err;
3641 		}
3642 	}
3643 
3644 	priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
3645 
3646 	return;
3647 
3648 out_err:
3649 	netdev_info(priv->net_dev,
3650 		    "Error reading Tx FQID, fallback to QDID-based enqueue\n");
3651 	priv->enqueue = dpaa2_eth_enqueue_qd;
3652 }
3653 
3654 /* Configure ingress classification based on VLAN PCP */
3655 static int dpaa2_eth_set_vlan_qos(struct dpaa2_eth_priv *priv)
3656 {
3657 	struct device *dev = priv->net_dev->dev.parent;
3658 	struct dpkg_profile_cfg kg_cfg = {0};
3659 	struct dpni_qos_tbl_cfg qos_cfg = {0};
3660 	struct dpni_rule_cfg key_params;
3661 	void *dma_mem, *key, *mask;
3662 	u8 key_size = 2;	/* VLAN TCI field */
3663 	int i, pcp, err;
3664 
3665 	/* VLAN-based classification only makes sense if we have multiple
3666 	 * traffic classes.
3667 	 * Also, we need to extract just the 3-bit PCP field from the VLAN
3668 	 * header and we can only do that by using a mask
3669 	 */
3670 	if (dpaa2_eth_tc_count(priv) == 1 || !dpaa2_eth_fs_mask_enabled(priv)) {
3671 		dev_dbg(dev, "VLAN-based QoS classification not supported\n");
3672 		return -EOPNOTSUPP;
3673 	}
3674 
3675 	dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
3676 	if (!dma_mem)
3677 		return -ENOMEM;
3678 
3679 	kg_cfg.num_extracts = 1;
3680 	kg_cfg.extracts[0].type = DPKG_EXTRACT_FROM_HDR;
3681 	kg_cfg.extracts[0].extract.from_hdr.prot = NET_PROT_VLAN;
3682 	kg_cfg.extracts[0].extract.from_hdr.type = DPKG_FULL_FIELD;
3683 	kg_cfg.extracts[0].extract.from_hdr.field = NH_FLD_VLAN_TCI;
3684 
3685 	err = dpni_prepare_key_cfg(&kg_cfg, dma_mem);
3686 	if (err) {
3687 		dev_err(dev, "dpni_prepare_key_cfg failed\n");
3688 		goto out_free_tbl;
3689 	}
3690 
3691 	/* set QoS table */
3692 	qos_cfg.default_tc = 0;
3693 	qos_cfg.discard_on_miss = 0;
3694 	qos_cfg.key_cfg_iova = dma_map_single(dev, dma_mem,
3695 					      DPAA2_CLASSIFIER_DMA_SIZE,
3696 					      DMA_TO_DEVICE);
3697 	if (dma_mapping_error(dev, qos_cfg.key_cfg_iova)) {
3698 		dev_err(dev, "QoS table DMA mapping failed\n");
3699 		err = -ENOMEM;
3700 		goto out_free_tbl;
3701 	}
3702 
3703 	err = dpni_set_qos_table(priv->mc_io, 0, priv->mc_token, &qos_cfg);
3704 	if (err) {
3705 		dev_err(dev, "dpni_set_qos_table failed\n");
3706 		goto out_unmap_tbl;
3707 	}
3708 
3709 	/* Add QoS table entries */
3710 	key = kzalloc(key_size * 2, GFP_KERNEL);
3711 	if (!key) {
3712 		err = -ENOMEM;
3713 		goto out_unmap_tbl;
3714 	}
3715 	mask = key + key_size;
3716 	*(__be16 *)mask = cpu_to_be16(VLAN_PRIO_MASK);
3717 
3718 	key_params.key_iova = dma_map_single(dev, key, key_size * 2,
3719 					     DMA_TO_DEVICE);
3720 	if (dma_mapping_error(dev, key_params.key_iova)) {
3721 		dev_err(dev, "Qos table entry DMA mapping failed\n");
3722 		err = -ENOMEM;
3723 		goto out_free_key;
3724 	}
3725 
3726 	key_params.mask_iova = key_params.key_iova + key_size;
3727 	key_params.key_size = key_size;
3728 
3729 	/* We add rules for PCP-based distribution starting with highest
3730 	 * priority (VLAN PCP = 7). If this DPNI doesn't have enough traffic
3731 	 * classes to accommodate all priority levels, the lowest ones end up
3732 	 * on TC 0 which was configured as default
3733 	 */
3734 	for (i = dpaa2_eth_tc_count(priv) - 1, pcp = 7; i >= 0; i--, pcp--) {
3735 		*(__be16 *)key = cpu_to_be16(pcp << VLAN_PRIO_SHIFT);
3736 		dma_sync_single_for_device(dev, key_params.key_iova,
3737 					   key_size * 2, DMA_TO_DEVICE);
3738 
3739 		err = dpni_add_qos_entry(priv->mc_io, 0, priv->mc_token,
3740 					 &key_params, i, i);
3741 		if (err) {
3742 			dev_err(dev, "dpni_add_qos_entry failed\n");
3743 			dpni_clear_qos_table(priv->mc_io, 0, priv->mc_token);
3744 			goto out_unmap_key;
3745 		}
3746 	}
3747 
3748 	priv->vlan_cls_enabled = true;
3749 
3750 	/* Table and key memory is not persistent, clean everything up after
3751 	 * configuration is finished
3752 	 */
3753 out_unmap_key:
3754 	dma_unmap_single(dev, key_params.key_iova, key_size * 2, DMA_TO_DEVICE);
3755 out_free_key:
3756 	kfree(key);
3757 out_unmap_tbl:
3758 	dma_unmap_single(dev, qos_cfg.key_cfg_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3759 			 DMA_TO_DEVICE);
3760 out_free_tbl:
3761 	kfree(dma_mem);
3762 
3763 	return err;
3764 }
3765 
3766 /* Configure the DPNI object this interface is associated with */
3767 static int dpaa2_eth_setup_dpni(struct fsl_mc_device *ls_dev)
3768 {
3769 	struct device *dev = &ls_dev->dev;
3770 	struct dpaa2_eth_priv *priv;
3771 	struct net_device *net_dev;
3772 	int err;
3773 
3774 	net_dev = dev_get_drvdata(dev);
3775 	priv = netdev_priv(net_dev);
3776 
3777 	/* get a handle for the DPNI object */
3778 	err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
3779 	if (err) {
3780 		dev_err(dev, "dpni_open() failed\n");
3781 		return err;
3782 	}
3783 
3784 	/* Check if we can work with this DPNI object */
3785 	err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
3786 				   &priv->dpni_ver_minor);
3787 	if (err) {
3788 		dev_err(dev, "dpni_get_api_version() failed\n");
3789 		goto close;
3790 	}
3791 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
3792 		dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
3793 			priv->dpni_ver_major, priv->dpni_ver_minor,
3794 			DPNI_VER_MAJOR, DPNI_VER_MINOR);
3795 		err = -ENOTSUPP;
3796 		goto close;
3797 	}
3798 
3799 	ls_dev->mc_io = priv->mc_io;
3800 	ls_dev->mc_handle = priv->mc_token;
3801 
3802 	err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3803 	if (err) {
3804 		dev_err(dev, "dpni_reset() failed\n");
3805 		goto close;
3806 	}
3807 
3808 	err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
3809 				  &priv->dpni_attrs);
3810 	if (err) {
3811 		dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
3812 		goto close;
3813 	}
3814 
3815 	err = dpaa2_eth_set_buffer_layout(priv);
3816 	if (err)
3817 		goto close;
3818 
3819 	dpaa2_eth_set_enqueue_mode(priv);
3820 
3821 	/* Enable pause frame support */
3822 	if (dpaa2_eth_has_pause_support(priv)) {
3823 		err = dpaa2_eth_set_pause(priv);
3824 		if (err)
3825 			goto close;
3826 	}
3827 
3828 	err = dpaa2_eth_set_vlan_qos(priv);
3829 	if (err && err != -EOPNOTSUPP)
3830 		goto close;
3831 
3832 	priv->cls_rules = devm_kcalloc(dev, dpaa2_eth_fs_count(priv),
3833 				       sizeof(struct dpaa2_eth_cls_rule),
3834 				       GFP_KERNEL);
3835 	if (!priv->cls_rules) {
3836 		err = -ENOMEM;
3837 		goto close;
3838 	}
3839 
3840 	return 0;
3841 
3842 close:
3843 	dpni_close(priv->mc_io, 0, priv->mc_token);
3844 
3845 	return err;
3846 }
3847 
3848 static void dpaa2_eth_free_dpni(struct dpaa2_eth_priv *priv)
3849 {
3850 	int err;
3851 
3852 	err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3853 	if (err)
3854 		netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
3855 			    err);
3856 
3857 	dpni_close(priv->mc_io, 0, priv->mc_token);
3858 }
3859 
3860 static int dpaa2_eth_setup_rx_flow(struct dpaa2_eth_priv *priv,
3861 				   struct dpaa2_eth_fq *fq)
3862 {
3863 	struct device *dev = priv->net_dev->dev.parent;
3864 	struct dpni_queue queue;
3865 	struct dpni_queue_id qid;
3866 	int err;
3867 
3868 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3869 			     DPNI_QUEUE_RX, fq->tc, fq->flowid, &queue, &qid);
3870 	if (err) {
3871 		dev_err(dev, "dpni_get_queue(RX) failed\n");
3872 		return err;
3873 	}
3874 
3875 	fq->fqid = qid.fqid;
3876 
3877 	queue.destination.id = fq->channel->dpcon_id;
3878 	queue.destination.type = DPNI_DEST_DPCON;
3879 	queue.destination.priority = 1;
3880 	queue.user_context = (u64)(uintptr_t)fq;
3881 	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3882 			     DPNI_QUEUE_RX, fq->tc, fq->flowid,
3883 			     DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3884 			     &queue);
3885 	if (err) {
3886 		dev_err(dev, "dpni_set_queue(RX) failed\n");
3887 		return err;
3888 	}
3889 
3890 	/* xdp_rxq setup */
3891 	/* only once for each channel */
3892 	if (fq->tc > 0)
3893 		return 0;
3894 
3895 	err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev,
3896 			       fq->flowid, 0);
3897 	if (err) {
3898 		dev_err(dev, "xdp_rxq_info_reg failed\n");
3899 		return err;
3900 	}
3901 
3902 	err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq,
3903 					 MEM_TYPE_PAGE_ORDER0, NULL);
3904 	if (err) {
3905 		dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n");
3906 		return err;
3907 	}
3908 
3909 	return 0;
3910 }
3911 
3912 static int dpaa2_eth_setup_tx_flow(struct dpaa2_eth_priv *priv,
3913 				   struct dpaa2_eth_fq *fq)
3914 {
3915 	struct device *dev = priv->net_dev->dev.parent;
3916 	struct dpni_queue queue;
3917 	struct dpni_queue_id qid;
3918 	int i, err;
3919 
3920 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3921 		err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3922 				     DPNI_QUEUE_TX, i, fq->flowid,
3923 				     &queue, &qid);
3924 		if (err) {
3925 			dev_err(dev, "dpni_get_queue(TX) failed\n");
3926 			return err;
3927 		}
3928 		fq->tx_fqid[i] = qid.fqid;
3929 	}
3930 
3931 	/* All Tx queues belonging to the same flowid have the same qdbin */
3932 	fq->tx_qdbin = qid.qdbin;
3933 
3934 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3935 			     DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3936 			     &queue, &qid);
3937 	if (err) {
3938 		dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
3939 		return err;
3940 	}
3941 
3942 	fq->fqid = qid.fqid;
3943 
3944 	queue.destination.id = fq->channel->dpcon_id;
3945 	queue.destination.type = DPNI_DEST_DPCON;
3946 	queue.destination.priority = 0;
3947 	queue.user_context = (u64)(uintptr_t)fq;
3948 	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3949 			     DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3950 			     DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3951 			     &queue);
3952 	if (err) {
3953 		dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
3954 		return err;
3955 	}
3956 
3957 	return 0;
3958 }
3959 
3960 static int setup_rx_err_flow(struct dpaa2_eth_priv *priv,
3961 			     struct dpaa2_eth_fq *fq)
3962 {
3963 	struct device *dev = priv->net_dev->dev.parent;
3964 	struct dpni_queue q = { { 0 } };
3965 	struct dpni_queue_id qid;
3966 	u8 q_opt = DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST;
3967 	int err;
3968 
3969 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3970 			     DPNI_QUEUE_RX_ERR, 0, 0, &q, &qid);
3971 	if (err) {
3972 		dev_err(dev, "dpni_get_queue() failed (%d)\n", err);
3973 		return err;
3974 	}
3975 
3976 	fq->fqid = qid.fqid;
3977 
3978 	q.destination.id = fq->channel->dpcon_id;
3979 	q.destination.type = DPNI_DEST_DPCON;
3980 	q.destination.priority = 1;
3981 	q.user_context = (u64)(uintptr_t)fq;
3982 	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3983 			     DPNI_QUEUE_RX_ERR, 0, 0, q_opt, &q);
3984 	if (err) {
3985 		dev_err(dev, "dpni_set_queue() failed (%d)\n", err);
3986 		return err;
3987 	}
3988 
3989 	return 0;
3990 }
3991 
3992 /* Supported header fields for Rx hash distribution key */
3993 static const struct dpaa2_eth_dist_fields dist_fields[] = {
3994 	{
3995 		/* L2 header */
3996 		.rxnfc_field = RXH_L2DA,
3997 		.cls_prot = NET_PROT_ETH,
3998 		.cls_field = NH_FLD_ETH_DA,
3999 		.id = DPAA2_ETH_DIST_ETHDST,
4000 		.size = 6,
4001 	}, {
4002 		.cls_prot = NET_PROT_ETH,
4003 		.cls_field = NH_FLD_ETH_SA,
4004 		.id = DPAA2_ETH_DIST_ETHSRC,
4005 		.size = 6,
4006 	}, {
4007 		/* This is the last ethertype field parsed:
4008 		 * depending on frame format, it can be the MAC ethertype
4009 		 * or the VLAN etype.
4010 		 */
4011 		.cls_prot = NET_PROT_ETH,
4012 		.cls_field = NH_FLD_ETH_TYPE,
4013 		.id = DPAA2_ETH_DIST_ETHTYPE,
4014 		.size = 2,
4015 	}, {
4016 		/* VLAN header */
4017 		.rxnfc_field = RXH_VLAN,
4018 		.cls_prot = NET_PROT_VLAN,
4019 		.cls_field = NH_FLD_VLAN_TCI,
4020 		.id = DPAA2_ETH_DIST_VLAN,
4021 		.size = 2,
4022 	}, {
4023 		/* IP header */
4024 		.rxnfc_field = RXH_IP_SRC,
4025 		.cls_prot = NET_PROT_IP,
4026 		.cls_field = NH_FLD_IP_SRC,
4027 		.id = DPAA2_ETH_DIST_IPSRC,
4028 		.size = 4,
4029 	}, {
4030 		.rxnfc_field = RXH_IP_DST,
4031 		.cls_prot = NET_PROT_IP,
4032 		.cls_field = NH_FLD_IP_DST,
4033 		.id = DPAA2_ETH_DIST_IPDST,
4034 		.size = 4,
4035 	}, {
4036 		.rxnfc_field = RXH_L3_PROTO,
4037 		.cls_prot = NET_PROT_IP,
4038 		.cls_field = NH_FLD_IP_PROTO,
4039 		.id = DPAA2_ETH_DIST_IPPROTO,
4040 		.size = 1,
4041 	}, {
4042 		/* Using UDP ports, this is functionally equivalent to raw
4043 		 * byte pairs from L4 header.
4044 		 */
4045 		.rxnfc_field = RXH_L4_B_0_1,
4046 		.cls_prot = NET_PROT_UDP,
4047 		.cls_field = NH_FLD_UDP_PORT_SRC,
4048 		.id = DPAA2_ETH_DIST_L4SRC,
4049 		.size = 2,
4050 	}, {
4051 		.rxnfc_field = RXH_L4_B_2_3,
4052 		.cls_prot = NET_PROT_UDP,
4053 		.cls_field = NH_FLD_UDP_PORT_DST,
4054 		.id = DPAA2_ETH_DIST_L4DST,
4055 		.size = 2,
4056 	},
4057 };
4058 
4059 /* Configure the Rx hash key using the legacy API */
4060 static int dpaa2_eth_config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
4061 {
4062 	struct device *dev = priv->net_dev->dev.parent;
4063 	struct dpni_rx_tc_dist_cfg dist_cfg;
4064 	int i, err = 0;
4065 
4066 	memset(&dist_cfg, 0, sizeof(dist_cfg));
4067 
4068 	dist_cfg.key_cfg_iova = key;
4069 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
4070 	dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
4071 
4072 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
4073 		err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token,
4074 					  i, &dist_cfg);
4075 		if (err) {
4076 			dev_err(dev, "dpni_set_rx_tc_dist failed\n");
4077 			break;
4078 		}
4079 	}
4080 
4081 	return err;
4082 }
4083 
4084 /* Configure the Rx hash key using the new API */
4085 static int dpaa2_eth_config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
4086 {
4087 	struct device *dev = priv->net_dev->dev.parent;
4088 	struct dpni_rx_dist_cfg dist_cfg;
4089 	int i, err = 0;
4090 
4091 	memset(&dist_cfg, 0, sizeof(dist_cfg));
4092 
4093 	dist_cfg.key_cfg_iova = key;
4094 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
4095 	dist_cfg.enable = 1;
4096 
4097 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
4098 		dist_cfg.tc = i;
4099 		err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token,
4100 					    &dist_cfg);
4101 		if (err) {
4102 			dev_err(dev, "dpni_set_rx_hash_dist failed\n");
4103 			break;
4104 		}
4105 
4106 		/* If the flow steering / hashing key is shared between all
4107 		 * traffic classes, install it just once
4108 		 */
4109 		if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
4110 			break;
4111 	}
4112 
4113 	return err;
4114 }
4115 
4116 /* Configure the Rx flow classification key */
4117 static int dpaa2_eth_config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
4118 {
4119 	struct device *dev = priv->net_dev->dev.parent;
4120 	struct dpni_rx_dist_cfg dist_cfg;
4121 	int i, err = 0;
4122 
4123 	memset(&dist_cfg, 0, sizeof(dist_cfg));
4124 
4125 	dist_cfg.key_cfg_iova = key;
4126 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
4127 	dist_cfg.enable = 1;
4128 
4129 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
4130 		dist_cfg.tc = i;
4131 		err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token,
4132 					  &dist_cfg);
4133 		if (err) {
4134 			dev_err(dev, "dpni_set_rx_fs_dist failed\n");
4135 			break;
4136 		}
4137 
4138 		/* If the flow steering / hashing key is shared between all
4139 		 * traffic classes, install it just once
4140 		 */
4141 		if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
4142 			break;
4143 	}
4144 
4145 	return err;
4146 }
4147 
4148 /* Size of the Rx flow classification key */
4149 int dpaa2_eth_cls_key_size(u64 fields)
4150 {
4151 	int i, size = 0;
4152 
4153 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
4154 		if (!(fields & dist_fields[i].id))
4155 			continue;
4156 		size += dist_fields[i].size;
4157 	}
4158 
4159 	return size;
4160 }
4161 
4162 /* Offset of header field in Rx classification key */
4163 int dpaa2_eth_cls_fld_off(int prot, int field)
4164 {
4165 	int i, off = 0;
4166 
4167 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
4168 		if (dist_fields[i].cls_prot == prot &&
4169 		    dist_fields[i].cls_field == field)
4170 			return off;
4171 		off += dist_fields[i].size;
4172 	}
4173 
4174 	WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
4175 	return 0;
4176 }
4177 
4178 /* Prune unused fields from the classification rule.
4179  * Used when masking is not supported
4180  */
4181 void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields)
4182 {
4183 	int off = 0, new_off = 0;
4184 	int i, size;
4185 
4186 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
4187 		size = dist_fields[i].size;
4188 		if (dist_fields[i].id & fields) {
4189 			memcpy(key_mem + new_off, key_mem + off, size);
4190 			new_off += size;
4191 		}
4192 		off += size;
4193 	}
4194 }
4195 
4196 /* Set Rx distribution (hash or flow classification) key
4197  * flags is a combination of RXH_ bits
4198  */
4199 static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
4200 				  enum dpaa2_eth_rx_dist type, u64 flags)
4201 {
4202 	struct device *dev = net_dev->dev.parent;
4203 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4204 	struct dpkg_profile_cfg cls_cfg;
4205 	u32 rx_hash_fields = 0;
4206 	dma_addr_t key_iova;
4207 	u8 *dma_mem;
4208 	int i;
4209 	int err = 0;
4210 
4211 	memset(&cls_cfg, 0, sizeof(cls_cfg));
4212 
4213 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
4214 		struct dpkg_extract *key =
4215 			&cls_cfg.extracts[cls_cfg.num_extracts];
4216 
4217 		/* For both Rx hashing and classification keys
4218 		 * we set only the selected fields.
4219 		 */
4220 		if (!(flags & dist_fields[i].id))
4221 			continue;
4222 		if (type == DPAA2_ETH_RX_DIST_HASH)
4223 			rx_hash_fields |= dist_fields[i].rxnfc_field;
4224 
4225 		if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
4226 			dev_err(dev, "error adding key extraction rule, too many rules?\n");
4227 			return -E2BIG;
4228 		}
4229 
4230 		key->type = DPKG_EXTRACT_FROM_HDR;
4231 		key->extract.from_hdr.prot = dist_fields[i].cls_prot;
4232 		key->extract.from_hdr.type = DPKG_FULL_FIELD;
4233 		key->extract.from_hdr.field = dist_fields[i].cls_field;
4234 		cls_cfg.num_extracts++;
4235 	}
4236 
4237 	dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
4238 	if (!dma_mem)
4239 		return -ENOMEM;
4240 
4241 	err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
4242 	if (err) {
4243 		dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
4244 		goto free_key;
4245 	}
4246 
4247 	/* Prepare for setting the rx dist */
4248 	key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
4249 				  DMA_TO_DEVICE);
4250 	if (dma_mapping_error(dev, key_iova)) {
4251 		dev_err(dev, "DMA mapping failed\n");
4252 		err = -ENOMEM;
4253 		goto free_key;
4254 	}
4255 
4256 	if (type == DPAA2_ETH_RX_DIST_HASH) {
4257 		if (dpaa2_eth_has_legacy_dist(priv))
4258 			err = dpaa2_eth_config_legacy_hash_key(priv, key_iova);
4259 		else
4260 			err = dpaa2_eth_config_hash_key(priv, key_iova);
4261 	} else {
4262 		err = dpaa2_eth_config_cls_key(priv, key_iova);
4263 	}
4264 
4265 	dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
4266 			 DMA_TO_DEVICE);
4267 	if (!err && type == DPAA2_ETH_RX_DIST_HASH)
4268 		priv->rx_hash_fields = rx_hash_fields;
4269 
4270 free_key:
4271 	kfree(dma_mem);
4272 	return err;
4273 }
4274 
4275 int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
4276 {
4277 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4278 	u64 key = 0;
4279 	int i;
4280 
4281 	if (!dpaa2_eth_hash_enabled(priv))
4282 		return -EOPNOTSUPP;
4283 
4284 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
4285 		if (dist_fields[i].rxnfc_field & flags)
4286 			key |= dist_fields[i].id;
4287 
4288 	return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, key);
4289 }
4290 
4291 int dpaa2_eth_set_cls(struct net_device *net_dev, u64 flags)
4292 {
4293 	return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_CLS, flags);
4294 }
4295 
4296 static int dpaa2_eth_set_default_cls(struct dpaa2_eth_priv *priv)
4297 {
4298 	struct device *dev = priv->net_dev->dev.parent;
4299 	int err;
4300 
4301 	/* Check if we actually support Rx flow classification */
4302 	if (dpaa2_eth_has_legacy_dist(priv)) {
4303 		dev_dbg(dev, "Rx cls not supported by current MC version\n");
4304 		return -EOPNOTSUPP;
4305 	}
4306 
4307 	if (!dpaa2_eth_fs_enabled(priv)) {
4308 		dev_dbg(dev, "Rx cls disabled in DPNI options\n");
4309 		return -EOPNOTSUPP;
4310 	}
4311 
4312 	if (!dpaa2_eth_hash_enabled(priv)) {
4313 		dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
4314 		return -EOPNOTSUPP;
4315 	}
4316 
4317 	/* If there is no support for masking in the classification table,
4318 	 * we don't set a default key, as it will depend on the rules
4319 	 * added by the user at runtime.
4320 	 */
4321 	if (!dpaa2_eth_fs_mask_enabled(priv))
4322 		goto out;
4323 
4324 	err = dpaa2_eth_set_cls(priv->net_dev, DPAA2_ETH_DIST_ALL);
4325 	if (err)
4326 		return err;
4327 
4328 out:
4329 	priv->rx_cls_enabled = 1;
4330 
4331 	return 0;
4332 }
4333 
4334 /* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
4335  * frame queues and channels
4336  */
4337 static int dpaa2_eth_bind_dpni(struct dpaa2_eth_priv *priv)
4338 {
4339 	struct dpaa2_eth_bp *bp = priv->bp[DPAA2_ETH_DEFAULT_BP_IDX];
4340 	struct net_device *net_dev = priv->net_dev;
4341 	struct dpni_pools_cfg pools_params = { 0 };
4342 	struct device *dev = net_dev->dev.parent;
4343 	struct dpni_error_cfg err_cfg;
4344 	int err = 0;
4345 	int i;
4346 
4347 	pools_params.num_dpbp = 1;
4348 	pools_params.pools[0].dpbp_id = bp->dev->obj_desc.id;
4349 	pools_params.pools[0].backup_pool = 0;
4350 	pools_params.pools[0].buffer_size = priv->rx_buf_size;
4351 	err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
4352 	if (err) {
4353 		dev_err(dev, "dpni_set_pools() failed\n");
4354 		return err;
4355 	}
4356 
4357 	/* have the interface implicitly distribute traffic based on
4358 	 * the default hash key
4359 	 */
4360 	err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
4361 	if (err && err != -EOPNOTSUPP)
4362 		dev_err(dev, "Failed to configure hashing\n");
4363 
4364 	/* Configure the flow classification key; it includes all
4365 	 * supported header fields and cannot be modified at runtime
4366 	 */
4367 	err = dpaa2_eth_set_default_cls(priv);
4368 	if (err && err != -EOPNOTSUPP)
4369 		dev_err(dev, "Failed to configure Rx classification key\n");
4370 
4371 	/* Configure handling of error frames */
4372 	err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
4373 	err_cfg.set_frame_annotation = 1;
4374 	err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
4375 	err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
4376 				       &err_cfg);
4377 	if (err) {
4378 		dev_err(dev, "dpni_set_errors_behavior failed\n");
4379 		return err;
4380 	}
4381 
4382 	/* Configure Rx and Tx conf queues to generate CDANs */
4383 	for (i = 0; i < priv->num_fqs; i++) {
4384 		switch (priv->fq[i].type) {
4385 		case DPAA2_RX_FQ:
4386 			err = dpaa2_eth_setup_rx_flow(priv, &priv->fq[i]);
4387 			break;
4388 		case DPAA2_TX_CONF_FQ:
4389 			err = dpaa2_eth_setup_tx_flow(priv, &priv->fq[i]);
4390 			break;
4391 		case DPAA2_RX_ERR_FQ:
4392 			err = setup_rx_err_flow(priv, &priv->fq[i]);
4393 			break;
4394 		default:
4395 			dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
4396 			return -EINVAL;
4397 		}
4398 		if (err)
4399 			return err;
4400 	}
4401 
4402 	err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
4403 			    DPNI_QUEUE_TX, &priv->tx_qdid);
4404 	if (err) {
4405 		dev_err(dev, "dpni_get_qdid() failed\n");
4406 		return err;
4407 	}
4408 
4409 	return 0;
4410 }
4411 
4412 /* Allocate rings for storing incoming frame descriptors */
4413 static int dpaa2_eth_alloc_rings(struct dpaa2_eth_priv *priv)
4414 {
4415 	struct net_device *net_dev = priv->net_dev;
4416 	struct device *dev = net_dev->dev.parent;
4417 	int i;
4418 
4419 	for (i = 0; i < priv->num_channels; i++) {
4420 		priv->channel[i]->store =
4421 			dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
4422 		if (!priv->channel[i]->store) {
4423 			netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
4424 			goto err_ring;
4425 		}
4426 	}
4427 
4428 	return 0;
4429 
4430 err_ring:
4431 	for (i = 0; i < priv->num_channels; i++) {
4432 		if (!priv->channel[i]->store)
4433 			break;
4434 		dpaa2_io_store_destroy(priv->channel[i]->store);
4435 	}
4436 
4437 	return -ENOMEM;
4438 }
4439 
4440 static void dpaa2_eth_free_rings(struct dpaa2_eth_priv *priv)
4441 {
4442 	int i;
4443 
4444 	for (i = 0; i < priv->num_channels; i++)
4445 		dpaa2_io_store_destroy(priv->channel[i]->store);
4446 }
4447 
4448 static int dpaa2_eth_set_mac_addr(struct dpaa2_eth_priv *priv)
4449 {
4450 	struct net_device *net_dev = priv->net_dev;
4451 	struct device *dev = net_dev->dev.parent;
4452 	u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
4453 	int err;
4454 
4455 	/* Get firmware address, if any */
4456 	err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
4457 	if (err) {
4458 		dev_err(dev, "dpni_get_port_mac_addr() failed\n");
4459 		return err;
4460 	}
4461 
4462 	/* Get DPNI attributes address, if any */
4463 	err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
4464 					dpni_mac_addr);
4465 	if (err) {
4466 		dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
4467 		return err;
4468 	}
4469 
4470 	/* First check if firmware has any address configured by bootloader */
4471 	if (!is_zero_ether_addr(mac_addr)) {
4472 		/* If the DPMAC addr != DPNI addr, update it */
4473 		if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
4474 			err = dpni_set_primary_mac_addr(priv->mc_io, 0,
4475 							priv->mc_token,
4476 							mac_addr);
4477 			if (err) {
4478 				dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
4479 				return err;
4480 			}
4481 		}
4482 		eth_hw_addr_set(net_dev, mac_addr);
4483 	} else if (is_zero_ether_addr(dpni_mac_addr)) {
4484 		/* No MAC address configured, fill in net_dev->dev_addr
4485 		 * with a random one
4486 		 */
4487 		eth_hw_addr_random(net_dev);
4488 		dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
4489 
4490 		err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
4491 						net_dev->dev_addr);
4492 		if (err) {
4493 			dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
4494 			return err;
4495 		}
4496 
4497 		/* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
4498 		 * practical purposes, this will be our "permanent" mac address,
4499 		 * at least until the next reboot. This move will also permit
4500 		 * register_netdevice() to properly fill up net_dev->perm_addr.
4501 		 */
4502 		net_dev->addr_assign_type = NET_ADDR_PERM;
4503 	} else {
4504 		/* NET_ADDR_PERM is default, all we have to do is
4505 		 * fill in the device addr.
4506 		 */
4507 		eth_hw_addr_set(net_dev, dpni_mac_addr);
4508 	}
4509 
4510 	return 0;
4511 }
4512 
4513 static int dpaa2_eth_netdev_init(struct net_device *net_dev)
4514 {
4515 	struct device *dev = net_dev->dev.parent;
4516 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4517 	u32 options = priv->dpni_attrs.options;
4518 	u64 supported = 0, not_supported = 0;
4519 	u8 bcast_addr[ETH_ALEN];
4520 	u8 num_queues;
4521 	int err;
4522 
4523 	net_dev->netdev_ops = &dpaa2_eth_ops;
4524 	net_dev->ethtool_ops = &dpaa2_ethtool_ops;
4525 
4526 	err = dpaa2_eth_set_mac_addr(priv);
4527 	if (err)
4528 		return err;
4529 
4530 	/* Explicitly add the broadcast address to the MAC filtering table */
4531 	eth_broadcast_addr(bcast_addr);
4532 	err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
4533 	if (err) {
4534 		dev_err(dev, "dpni_add_mac_addr() failed\n");
4535 		return err;
4536 	}
4537 
4538 	/* Set MTU upper limit; lower limit is 68B (default value) */
4539 	net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
4540 	err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
4541 					DPAA2_ETH_MFL);
4542 	if (err) {
4543 		dev_err(dev, "dpni_set_max_frame_length() failed\n");
4544 		return err;
4545 	}
4546 
4547 	/* Set actual number of queues in the net device */
4548 	num_queues = dpaa2_eth_queue_count(priv);
4549 	err = netif_set_real_num_tx_queues(net_dev, num_queues);
4550 	if (err) {
4551 		dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
4552 		return err;
4553 	}
4554 	err = netif_set_real_num_rx_queues(net_dev, num_queues);
4555 	if (err) {
4556 		dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
4557 		return err;
4558 	}
4559 
4560 	dpaa2_eth_detect_features(priv);
4561 
4562 	/* Capabilities listing */
4563 	supported |= IFF_LIVE_ADDR_CHANGE;
4564 
4565 	if (options & DPNI_OPT_NO_MAC_FILTER)
4566 		not_supported |= IFF_UNICAST_FLT;
4567 	else
4568 		supported |= IFF_UNICAST_FLT;
4569 
4570 	net_dev->priv_flags |= supported;
4571 	net_dev->priv_flags &= ~not_supported;
4572 
4573 	/* Features */
4574 	net_dev->features = NETIF_F_RXCSUM |
4575 			    NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
4576 			    NETIF_F_SG | NETIF_F_HIGHDMA |
4577 			    NETIF_F_LLTX | NETIF_F_HW_TC | NETIF_F_TSO;
4578 	net_dev->gso_max_segs = DPAA2_ETH_ENQUEUE_MAX_FDS;
4579 	net_dev->hw_features = net_dev->features;
4580 
4581 	if (priv->dpni_attrs.vlan_filter_entries)
4582 		net_dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4583 
4584 	return 0;
4585 }
4586 
4587 static int dpaa2_eth_poll_link_state(void *arg)
4588 {
4589 	struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
4590 	int err;
4591 
4592 	while (!kthread_should_stop()) {
4593 		err = dpaa2_eth_link_state_update(priv);
4594 		if (unlikely(err))
4595 			return err;
4596 
4597 		msleep(DPAA2_ETH_LINK_STATE_REFRESH);
4598 	}
4599 
4600 	return 0;
4601 }
4602 
4603 static int dpaa2_eth_connect_mac(struct dpaa2_eth_priv *priv)
4604 {
4605 	struct fsl_mc_device *dpni_dev, *dpmac_dev;
4606 	struct dpaa2_mac *mac;
4607 	int err;
4608 
4609 	dpni_dev = to_fsl_mc_device(priv->net_dev->dev.parent);
4610 	dpmac_dev = fsl_mc_get_endpoint(dpni_dev, 0);
4611 
4612 	if (PTR_ERR(dpmac_dev) == -EPROBE_DEFER) {
4613 		netdev_dbg(priv->net_dev, "waiting for mac\n");
4614 		return PTR_ERR(dpmac_dev);
4615 	}
4616 
4617 	if (IS_ERR(dpmac_dev) || dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type)
4618 		return 0;
4619 
4620 	mac = kzalloc(sizeof(struct dpaa2_mac), GFP_KERNEL);
4621 	if (!mac)
4622 		return -ENOMEM;
4623 
4624 	mac->mc_dev = dpmac_dev;
4625 	mac->mc_io = priv->mc_io;
4626 	mac->net_dev = priv->net_dev;
4627 
4628 	err = dpaa2_mac_open(mac);
4629 	if (err)
4630 		goto err_free_mac;
4631 	priv->mac = mac;
4632 
4633 	if (dpaa2_eth_is_type_phy(priv)) {
4634 		err = dpaa2_mac_connect(mac);
4635 		if (err) {
4636 			if (err == -EPROBE_DEFER)
4637 				netdev_dbg(priv->net_dev,
4638 					   "could not connect to MAC\n");
4639 			else
4640 				netdev_err(priv->net_dev,
4641 					   "Error connecting to the MAC endpoint: %pe",
4642 					   ERR_PTR(err));
4643 			goto err_close_mac;
4644 		}
4645 	}
4646 
4647 	return 0;
4648 
4649 err_close_mac:
4650 	dpaa2_mac_close(mac);
4651 	priv->mac = NULL;
4652 err_free_mac:
4653 	kfree(mac);
4654 	return err;
4655 }
4656 
4657 static void dpaa2_eth_disconnect_mac(struct dpaa2_eth_priv *priv)
4658 {
4659 	if (dpaa2_eth_is_type_phy(priv))
4660 		dpaa2_mac_disconnect(priv->mac);
4661 
4662 	if (!dpaa2_eth_has_mac(priv))
4663 		return;
4664 
4665 	dpaa2_mac_close(priv->mac);
4666 	kfree(priv->mac);
4667 	priv->mac = NULL;
4668 }
4669 
4670 static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
4671 {
4672 	u32 status = ~0;
4673 	struct device *dev = (struct device *)arg;
4674 	struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
4675 	struct net_device *net_dev = dev_get_drvdata(dev);
4676 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4677 	int err;
4678 
4679 	err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
4680 				  DPNI_IRQ_INDEX, &status);
4681 	if (unlikely(err)) {
4682 		netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
4683 		return IRQ_HANDLED;
4684 	}
4685 
4686 	if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
4687 		dpaa2_eth_link_state_update(netdev_priv(net_dev));
4688 
4689 	if (status & DPNI_IRQ_EVENT_ENDPOINT_CHANGED) {
4690 		dpaa2_eth_set_mac_addr(netdev_priv(net_dev));
4691 		dpaa2_eth_update_tx_fqids(priv);
4692 
4693 		rtnl_lock();
4694 		if (dpaa2_eth_has_mac(priv))
4695 			dpaa2_eth_disconnect_mac(priv);
4696 		else
4697 			dpaa2_eth_connect_mac(priv);
4698 		rtnl_unlock();
4699 	}
4700 
4701 	return IRQ_HANDLED;
4702 }
4703 
4704 static int dpaa2_eth_setup_irqs(struct fsl_mc_device *ls_dev)
4705 {
4706 	int err = 0;
4707 	struct fsl_mc_device_irq *irq;
4708 
4709 	err = fsl_mc_allocate_irqs(ls_dev);
4710 	if (err) {
4711 		dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
4712 		return err;
4713 	}
4714 
4715 	irq = ls_dev->irqs[0];
4716 	err = devm_request_threaded_irq(&ls_dev->dev, irq->virq,
4717 					NULL, dpni_irq0_handler_thread,
4718 					IRQF_NO_SUSPEND | IRQF_ONESHOT,
4719 					dev_name(&ls_dev->dev), &ls_dev->dev);
4720 	if (err < 0) {
4721 		dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
4722 		goto free_mc_irq;
4723 	}
4724 
4725 	err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
4726 				DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED |
4727 				DPNI_IRQ_EVENT_ENDPOINT_CHANGED);
4728 	if (err < 0) {
4729 		dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
4730 		goto free_irq;
4731 	}
4732 
4733 	err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
4734 				  DPNI_IRQ_INDEX, 1);
4735 	if (err < 0) {
4736 		dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
4737 		goto free_irq;
4738 	}
4739 
4740 	return 0;
4741 
4742 free_irq:
4743 	devm_free_irq(&ls_dev->dev, irq->virq, &ls_dev->dev);
4744 free_mc_irq:
4745 	fsl_mc_free_irqs(ls_dev);
4746 
4747 	return err;
4748 }
4749 
4750 static void dpaa2_eth_add_ch_napi(struct dpaa2_eth_priv *priv)
4751 {
4752 	int i;
4753 	struct dpaa2_eth_channel *ch;
4754 
4755 	for (i = 0; i < priv->num_channels; i++) {
4756 		ch = priv->channel[i];
4757 		/* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
4758 		netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll);
4759 	}
4760 }
4761 
4762 static void dpaa2_eth_del_ch_napi(struct dpaa2_eth_priv *priv)
4763 {
4764 	int i;
4765 	struct dpaa2_eth_channel *ch;
4766 
4767 	for (i = 0; i < priv->num_channels; i++) {
4768 		ch = priv->channel[i];
4769 		netif_napi_del(&ch->napi);
4770 	}
4771 }
4772 
4773 static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
4774 {
4775 	struct device *dev;
4776 	struct net_device *net_dev = NULL;
4777 	struct dpaa2_eth_priv *priv = NULL;
4778 	int err = 0;
4779 
4780 	dev = &dpni_dev->dev;
4781 
4782 	/* Net device */
4783 	net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_NETDEV_QUEUES);
4784 	if (!net_dev) {
4785 		dev_err(dev, "alloc_etherdev_mq() failed\n");
4786 		return -ENOMEM;
4787 	}
4788 
4789 	SET_NETDEV_DEV(net_dev, dev);
4790 	dev_set_drvdata(dev, net_dev);
4791 
4792 	priv = netdev_priv(net_dev);
4793 	priv->net_dev = net_dev;
4794 	SET_NETDEV_DEVLINK_PORT(net_dev, &priv->devlink_port);
4795 
4796 	priv->iommu_domain = iommu_get_domain_for_dev(dev);
4797 
4798 	priv->tx_tstamp_type = HWTSTAMP_TX_OFF;
4799 	priv->rx_tstamp = false;
4800 
4801 	priv->dpaa2_ptp_wq = alloc_workqueue("dpaa2_ptp_wq", 0, 0);
4802 	if (!priv->dpaa2_ptp_wq) {
4803 		err = -ENOMEM;
4804 		goto err_wq_alloc;
4805 	}
4806 
4807 	INIT_WORK(&priv->tx_onestep_tstamp, dpaa2_eth_tx_onestep_tstamp);
4808 	mutex_init(&priv->onestep_tstamp_lock);
4809 	skb_queue_head_init(&priv->tx_skbs);
4810 
4811 	priv->rx_copybreak = DPAA2_ETH_DEFAULT_COPYBREAK;
4812 
4813 	/* Obtain a MC portal */
4814 	err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
4815 				     &priv->mc_io);
4816 	if (err) {
4817 		if (err == -ENXIO) {
4818 			dev_dbg(dev, "waiting for MC portal\n");
4819 			err = -EPROBE_DEFER;
4820 		} else {
4821 			dev_err(dev, "MC portal allocation failed\n");
4822 		}
4823 		goto err_portal_alloc;
4824 	}
4825 
4826 	/* MC objects initialization and configuration */
4827 	err = dpaa2_eth_setup_dpni(dpni_dev);
4828 	if (err)
4829 		goto err_dpni_setup;
4830 
4831 	err = dpaa2_eth_setup_dpio(priv);
4832 	if (err)
4833 		goto err_dpio_setup;
4834 
4835 	dpaa2_eth_setup_fqs(priv);
4836 
4837 	err = dpaa2_eth_setup_default_dpbp(priv);
4838 	if (err)
4839 		goto err_dpbp_setup;
4840 
4841 	err = dpaa2_eth_bind_dpni(priv);
4842 	if (err)
4843 		goto err_bind;
4844 
4845 	/* Add a NAPI context for each channel */
4846 	dpaa2_eth_add_ch_napi(priv);
4847 
4848 	/* Percpu statistics */
4849 	priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
4850 	if (!priv->percpu_stats) {
4851 		dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
4852 		err = -ENOMEM;
4853 		goto err_alloc_percpu_stats;
4854 	}
4855 	priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
4856 	if (!priv->percpu_extras) {
4857 		dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
4858 		err = -ENOMEM;
4859 		goto err_alloc_percpu_extras;
4860 	}
4861 
4862 	priv->sgt_cache = alloc_percpu(*priv->sgt_cache);
4863 	if (!priv->sgt_cache) {
4864 		dev_err(dev, "alloc_percpu(sgt_cache) failed\n");
4865 		err = -ENOMEM;
4866 		goto err_alloc_sgt_cache;
4867 	}
4868 
4869 	priv->fd = alloc_percpu(*priv->fd);
4870 	if (!priv->fd) {
4871 		dev_err(dev, "alloc_percpu(fds) failed\n");
4872 		err = -ENOMEM;
4873 		goto err_alloc_fds;
4874 	}
4875 
4876 	err = dpaa2_eth_netdev_init(net_dev);
4877 	if (err)
4878 		goto err_netdev_init;
4879 
4880 	/* Configure checksum offload based on current interface flags */
4881 	err = dpaa2_eth_set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
4882 	if (err)
4883 		goto err_csum;
4884 
4885 	err = dpaa2_eth_set_tx_csum(priv,
4886 				    !!(net_dev->features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
4887 	if (err)
4888 		goto err_csum;
4889 
4890 	err = dpaa2_eth_alloc_rings(priv);
4891 	if (err)
4892 		goto err_alloc_rings;
4893 
4894 #ifdef CONFIG_FSL_DPAA2_ETH_DCB
4895 	if (dpaa2_eth_has_pause_support(priv) && priv->vlan_cls_enabled) {
4896 		priv->dcbx_mode = DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE;
4897 		net_dev->dcbnl_ops = &dpaa2_eth_dcbnl_ops;
4898 	} else {
4899 		dev_dbg(dev, "PFC not supported\n");
4900 	}
4901 #endif
4902 
4903 	err = dpaa2_eth_setup_irqs(dpni_dev);
4904 	if (err) {
4905 		netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
4906 		priv->poll_thread = kthread_run(dpaa2_eth_poll_link_state, priv,
4907 						"%s_poll_link", net_dev->name);
4908 		if (IS_ERR(priv->poll_thread)) {
4909 			dev_err(dev, "Error starting polling thread\n");
4910 			goto err_poll_thread;
4911 		}
4912 		priv->do_link_poll = true;
4913 	}
4914 
4915 	err = dpaa2_eth_connect_mac(priv);
4916 	if (err)
4917 		goto err_connect_mac;
4918 
4919 	err = dpaa2_eth_dl_alloc(priv);
4920 	if (err)
4921 		goto err_dl_register;
4922 
4923 	err = dpaa2_eth_dl_traps_register(priv);
4924 	if (err)
4925 		goto err_dl_trap_register;
4926 
4927 	err = dpaa2_eth_dl_port_add(priv);
4928 	if (err)
4929 		goto err_dl_port_add;
4930 
4931 	err = register_netdev(net_dev);
4932 	if (err < 0) {
4933 		dev_err(dev, "register_netdev() failed\n");
4934 		goto err_netdev_reg;
4935 	}
4936 
4937 #ifdef CONFIG_DEBUG_FS
4938 	dpaa2_dbg_add(priv);
4939 #endif
4940 
4941 	dpaa2_eth_dl_register(priv);
4942 	dev_info(dev, "Probed interface %s\n", net_dev->name);
4943 	return 0;
4944 
4945 err_netdev_reg:
4946 	dpaa2_eth_dl_port_del(priv);
4947 err_dl_port_add:
4948 	dpaa2_eth_dl_traps_unregister(priv);
4949 err_dl_trap_register:
4950 	dpaa2_eth_dl_free(priv);
4951 err_dl_register:
4952 	dpaa2_eth_disconnect_mac(priv);
4953 err_connect_mac:
4954 	if (priv->do_link_poll)
4955 		kthread_stop(priv->poll_thread);
4956 	else
4957 		fsl_mc_free_irqs(dpni_dev);
4958 err_poll_thread:
4959 	dpaa2_eth_free_rings(priv);
4960 err_alloc_rings:
4961 err_csum:
4962 err_netdev_init:
4963 	free_percpu(priv->fd);
4964 err_alloc_fds:
4965 	free_percpu(priv->sgt_cache);
4966 err_alloc_sgt_cache:
4967 	free_percpu(priv->percpu_extras);
4968 err_alloc_percpu_extras:
4969 	free_percpu(priv->percpu_stats);
4970 err_alloc_percpu_stats:
4971 	dpaa2_eth_del_ch_napi(priv);
4972 err_bind:
4973 	dpaa2_eth_free_dpbps(priv);
4974 err_dpbp_setup:
4975 	dpaa2_eth_free_dpio(priv);
4976 err_dpio_setup:
4977 	dpaa2_eth_free_dpni(priv);
4978 err_dpni_setup:
4979 	fsl_mc_portal_free(priv->mc_io);
4980 err_portal_alloc:
4981 	destroy_workqueue(priv->dpaa2_ptp_wq);
4982 err_wq_alloc:
4983 	dev_set_drvdata(dev, NULL);
4984 	free_netdev(net_dev);
4985 
4986 	return err;
4987 }
4988 
4989 static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
4990 {
4991 	struct device *dev;
4992 	struct net_device *net_dev;
4993 	struct dpaa2_eth_priv *priv;
4994 
4995 	dev = &ls_dev->dev;
4996 	net_dev = dev_get_drvdata(dev);
4997 	priv = netdev_priv(net_dev);
4998 
4999 	dpaa2_eth_dl_unregister(priv);
5000 
5001 #ifdef CONFIG_DEBUG_FS
5002 	dpaa2_dbg_remove(priv);
5003 #endif
5004 
5005 	unregister_netdev(net_dev);
5006 	rtnl_lock();
5007 	dpaa2_eth_disconnect_mac(priv);
5008 	rtnl_unlock();
5009 
5010 	dpaa2_eth_dl_port_del(priv);
5011 	dpaa2_eth_dl_traps_unregister(priv);
5012 	dpaa2_eth_dl_free(priv);
5013 
5014 	if (priv->do_link_poll)
5015 		kthread_stop(priv->poll_thread);
5016 	else
5017 		fsl_mc_free_irqs(ls_dev);
5018 
5019 	dpaa2_eth_free_rings(priv);
5020 	free_percpu(priv->fd);
5021 	free_percpu(priv->sgt_cache);
5022 	free_percpu(priv->percpu_stats);
5023 	free_percpu(priv->percpu_extras);
5024 
5025 	dpaa2_eth_del_ch_napi(priv);
5026 	dpaa2_eth_free_dpbps(priv);
5027 	dpaa2_eth_free_dpio(priv);
5028 	dpaa2_eth_free_dpni(priv);
5029 	if (priv->onestep_reg_base)
5030 		iounmap(priv->onestep_reg_base);
5031 
5032 	fsl_mc_portal_free(priv->mc_io);
5033 
5034 	destroy_workqueue(priv->dpaa2_ptp_wq);
5035 
5036 	dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
5037 
5038 	free_netdev(net_dev);
5039 
5040 	return 0;
5041 }
5042 
5043 static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
5044 	{
5045 		.vendor = FSL_MC_VENDOR_FREESCALE,
5046 		.obj_type = "dpni",
5047 	},
5048 	{ .vendor = 0x0 }
5049 };
5050 MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
5051 
5052 static struct fsl_mc_driver dpaa2_eth_driver = {
5053 	.driver = {
5054 		.name = KBUILD_MODNAME,
5055 		.owner = THIS_MODULE,
5056 	},
5057 	.probe = dpaa2_eth_probe,
5058 	.remove = dpaa2_eth_remove,
5059 	.match_id_table = dpaa2_eth_match_id_table
5060 };
5061 
5062 static int __init dpaa2_eth_driver_init(void)
5063 {
5064 	int err;
5065 
5066 	dpaa2_eth_dbg_init();
5067 	err = fsl_mc_driver_register(&dpaa2_eth_driver);
5068 	if (err) {
5069 		dpaa2_eth_dbg_exit();
5070 		return err;
5071 	}
5072 
5073 	return 0;
5074 }
5075 
5076 static void __exit dpaa2_eth_driver_exit(void)
5077 {
5078 	dpaa2_eth_dbg_exit();
5079 	fsl_mc_driver_unregister(&dpaa2_eth_driver);
5080 }
5081 
5082 module_init(dpaa2_eth_driver_init);
5083 module_exit(dpaa2_eth_driver_exit);
5084