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