1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /* Copyright 2014-2016 Freescale Semiconductor Inc.
3  * Copyright 2016-2020 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, priv->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 	int i;
1101 
1102 	fd_addr = dpaa2_fd_get_addr(fd);
1103 	buffer_start = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr);
1104 	swa = (struct dpaa2_eth_swa *)buffer_start;
1105 
1106 	if (fd_format == dpaa2_fd_single) {
1107 		if (swa->type == DPAA2_ETH_SWA_SINGLE) {
1108 			skb = swa->single.skb;
1109 			/* Accessing the skb buffer is safe before dma unmap,
1110 			 * because we didn't map the actual skb shell.
1111 			 */
1112 			dma_unmap_single(dev, fd_addr,
1113 					 skb_tail_pointer(skb) - buffer_start,
1114 					 DMA_BIDIRECTIONAL);
1115 		} else {
1116 			WARN_ONCE(swa->type != DPAA2_ETH_SWA_XDP, "Wrong SWA type");
1117 			dma_unmap_single(dev, fd_addr, swa->xdp.dma_size,
1118 					 DMA_BIDIRECTIONAL);
1119 		}
1120 	} else if (fd_format == dpaa2_fd_sg) {
1121 		if (swa->type == DPAA2_ETH_SWA_SG) {
1122 			skb = swa->sg.skb;
1123 
1124 			/* Unmap the scatterlist */
1125 			dma_unmap_sg(dev, swa->sg.scl, swa->sg.num_sg,
1126 				     DMA_BIDIRECTIONAL);
1127 			kfree(swa->sg.scl);
1128 
1129 			/* Unmap the SGT buffer */
1130 			dma_unmap_single(dev, fd_addr, swa->sg.sgt_size,
1131 					 DMA_BIDIRECTIONAL);
1132 		} else if (swa->type == DPAA2_ETH_SWA_SW_TSO) {
1133 			skb = swa->tso.skb;
1134 
1135 			sgt = (struct dpaa2_sg_entry *)(buffer_start +
1136 							priv->tx_data_offset);
1137 
1138 			/* Unmap and free the header */
1139 			dma_unmap_single(dev, dpaa2_sg_get_addr(sgt), TSO_HEADER_SIZE,
1140 					 DMA_TO_DEVICE);
1141 			kfree(dpaa2_iova_to_virt(priv->iommu_domain, dpaa2_sg_get_addr(sgt)));
1142 
1143 			/* Unmap the other SG entries for the data */
1144 			for (i = 1; i < swa->tso.num_sg; i++)
1145 				dma_unmap_single(dev, dpaa2_sg_get_addr(&sgt[i]),
1146 						 dpaa2_sg_get_len(&sgt[i]), DMA_TO_DEVICE);
1147 
1148 			/* Unmap the SGT buffer */
1149 			dma_unmap_single(dev, fd_addr, swa->sg.sgt_size,
1150 					 DMA_BIDIRECTIONAL);
1151 
1152 			if (!swa->tso.is_last_fd)
1153 				should_free_skb = 0;
1154 		} else {
1155 			skb = swa->single.skb;
1156 
1157 			/* Unmap the SGT Buffer */
1158 			dma_unmap_single(dev, fd_addr, swa->single.sgt_size,
1159 					 DMA_BIDIRECTIONAL);
1160 
1161 			sgt = (struct dpaa2_sg_entry *)(buffer_start +
1162 							priv->tx_data_offset);
1163 			sg_addr = dpaa2_sg_get_addr(sgt);
1164 			dma_unmap_single(dev, sg_addr, skb->len, DMA_BIDIRECTIONAL);
1165 		}
1166 	} else {
1167 		netdev_dbg(priv->net_dev, "Invalid FD format\n");
1168 		return;
1169 	}
1170 
1171 	if (swa->type != DPAA2_ETH_SWA_XDP && in_napi) {
1172 		fq->dq_frames++;
1173 		fq->dq_bytes += fd_len;
1174 	}
1175 
1176 	if (swa->type == DPAA2_ETH_SWA_XDP) {
1177 		xdp_return_frame(swa->xdp.xdpf);
1178 		return;
1179 	}
1180 
1181 	/* Get the timestamp value */
1182 	if (swa->type != DPAA2_ETH_SWA_SW_TSO) {
1183 		if (skb->cb[0] == TX_TSTAMP) {
1184 			struct skb_shared_hwtstamps shhwtstamps;
1185 			__le64 *ts = dpaa2_get_ts(buffer_start, true);
1186 			u64 ns;
1187 
1188 			memset(&shhwtstamps, 0, sizeof(shhwtstamps));
1189 
1190 			ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
1191 			shhwtstamps.hwtstamp = ns_to_ktime(ns);
1192 			skb_tstamp_tx(skb, &shhwtstamps);
1193 		} else if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
1194 			mutex_unlock(&priv->onestep_tstamp_lock);
1195 		}
1196 	}
1197 
1198 	/* Free SGT buffer allocated on tx */
1199 	if (fd_format != dpaa2_fd_single)
1200 		dpaa2_eth_sgt_recycle(priv, buffer_start);
1201 
1202 	/* Move on with skb release. If we are just confirming multiple FDs
1203 	 * from the same TSO skb then only the last one will need to free the
1204 	 * skb.
1205 	 */
1206 	if (should_free_skb)
1207 		napi_consume_skb(skb, in_napi);
1208 }
1209 
1210 static int dpaa2_eth_build_gso_fd(struct dpaa2_eth_priv *priv,
1211 				  struct sk_buff *skb, struct dpaa2_fd *fd,
1212 				  int *num_fds, u32 *total_fds_len)
1213 {
1214 	struct device *dev = priv->net_dev->dev.parent;
1215 	int hdr_len, total_len, data_left, fd_len;
1216 	int num_sge, err, i, sgt_buf_size;
1217 	struct dpaa2_fd *fd_start = fd;
1218 	struct dpaa2_sg_entry *sgt;
1219 	struct dpaa2_eth_swa *swa;
1220 	dma_addr_t sgt_addr, addr;
1221 	dma_addr_t tso_hdr_dma;
1222 	unsigned int index = 0;
1223 	struct tso_t tso;
1224 	char *tso_hdr;
1225 	void *sgt_buf;
1226 
1227 	/* Initialize the TSO handler, and prepare the first payload */
1228 	hdr_len = tso_start(skb, &tso);
1229 	*total_fds_len = 0;
1230 
1231 	total_len = skb->len - hdr_len;
1232 	while (total_len > 0) {
1233 		/* Prepare the HW SGT structure for this frame */
1234 		sgt_buf = dpaa2_eth_sgt_get(priv);
1235 		if (unlikely(!sgt_buf)) {
1236 			netdev_err(priv->net_dev, "dpaa2_eth_sgt_get() failed\n");
1237 			err = -ENOMEM;
1238 			goto err_sgt_get;
1239 		}
1240 		sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
1241 
1242 		/* Determine the data length of this frame */
1243 		data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
1244 		total_len -= data_left;
1245 		fd_len = data_left + hdr_len;
1246 
1247 		/* Prepare packet headers: MAC + IP + TCP */
1248 		tso_hdr = kmalloc(TSO_HEADER_SIZE, GFP_ATOMIC);
1249 		if (!tso_hdr) {
1250 			err =  -ENOMEM;
1251 			goto err_alloc_tso_hdr;
1252 		}
1253 
1254 		tso_build_hdr(skb, tso_hdr, &tso, data_left, total_len == 0);
1255 		tso_hdr_dma = dma_map_single(dev, tso_hdr, TSO_HEADER_SIZE, DMA_TO_DEVICE);
1256 		if (dma_mapping_error(dev, tso_hdr_dma)) {
1257 			netdev_err(priv->net_dev, "dma_map_single(tso_hdr) failed\n");
1258 			err = -ENOMEM;
1259 			goto err_map_tso_hdr;
1260 		}
1261 
1262 		/* Setup the SG entry for the header */
1263 		dpaa2_sg_set_addr(sgt, tso_hdr_dma);
1264 		dpaa2_sg_set_len(sgt, hdr_len);
1265 		dpaa2_sg_set_final(sgt, data_left <= 0);
1266 
1267 		/* Compose the SG entries for each fragment of data */
1268 		num_sge = 1;
1269 		while (data_left > 0) {
1270 			int size;
1271 
1272 			/* Move to the next SG entry */
1273 			sgt++;
1274 			size = min_t(int, tso.size, data_left);
1275 
1276 			addr = dma_map_single(dev, tso.data, size, DMA_TO_DEVICE);
1277 			if (dma_mapping_error(dev, addr)) {
1278 				netdev_err(priv->net_dev, "dma_map_single(tso.data) failed\n");
1279 				err = -ENOMEM;
1280 				goto err_map_data;
1281 			}
1282 			dpaa2_sg_set_addr(sgt, addr);
1283 			dpaa2_sg_set_len(sgt, size);
1284 			dpaa2_sg_set_final(sgt, size == data_left);
1285 
1286 			num_sge++;
1287 
1288 			/* Build the data for the __next__ fragment */
1289 			data_left -= size;
1290 			tso_build_data(skb, &tso, size);
1291 		}
1292 
1293 		/* Store the skb backpointer in the SGT buffer */
1294 		sgt_buf_size = priv->tx_data_offset + num_sge * sizeof(struct dpaa2_sg_entry);
1295 		swa = (struct dpaa2_eth_swa *)sgt_buf;
1296 		swa->type = DPAA2_ETH_SWA_SW_TSO;
1297 		swa->tso.skb = skb;
1298 		swa->tso.num_sg = num_sge;
1299 		swa->tso.sgt_size = sgt_buf_size;
1300 		swa->tso.is_last_fd = total_len == 0 ? 1 : 0;
1301 
1302 		/* Separately map the SGT buffer */
1303 		sgt_addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
1304 		if (unlikely(dma_mapping_error(dev, sgt_addr))) {
1305 			netdev_err(priv->net_dev, "dma_map_single(sgt_buf) failed\n");
1306 			err = -ENOMEM;
1307 			goto err_map_sgt;
1308 		}
1309 
1310 		/* Setup the frame descriptor */
1311 		memset(fd, 0, sizeof(struct dpaa2_fd));
1312 		dpaa2_fd_set_offset(fd, priv->tx_data_offset);
1313 		dpaa2_fd_set_format(fd, dpaa2_fd_sg);
1314 		dpaa2_fd_set_addr(fd, sgt_addr);
1315 		dpaa2_fd_set_len(fd, fd_len);
1316 		dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
1317 
1318 		*total_fds_len += fd_len;
1319 		/* Advance to the next frame descriptor */
1320 		fd++;
1321 		index++;
1322 	}
1323 
1324 	*num_fds = index;
1325 
1326 	return 0;
1327 
1328 err_map_sgt:
1329 err_map_data:
1330 	/* Unmap all the data S/G entries for the current FD */
1331 	sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
1332 	for (i = 1; i < num_sge; i++)
1333 		dma_unmap_single(dev, dpaa2_sg_get_addr(&sgt[i]),
1334 				 dpaa2_sg_get_len(&sgt[i]), DMA_TO_DEVICE);
1335 
1336 	/* Unmap the header entry */
1337 	dma_unmap_single(dev, tso_hdr_dma, TSO_HEADER_SIZE, DMA_TO_DEVICE);
1338 err_map_tso_hdr:
1339 	kfree(tso_hdr);
1340 err_alloc_tso_hdr:
1341 	dpaa2_eth_sgt_recycle(priv, sgt_buf);
1342 err_sgt_get:
1343 	/* Free all the other FDs that were already fully created */
1344 	for (i = 0; i < index; i++)
1345 		dpaa2_eth_free_tx_fd(priv, NULL, &fd_start[i], false);
1346 
1347 	return err;
1348 }
1349 
1350 static netdev_tx_t __dpaa2_eth_tx(struct sk_buff *skb,
1351 				  struct net_device *net_dev)
1352 {
1353 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1354 	int total_enqueued = 0, retries = 0, enqueued;
1355 	struct dpaa2_eth_drv_stats *percpu_extras;
1356 	struct rtnl_link_stats64 *percpu_stats;
1357 	unsigned int needed_headroom;
1358 	int num_fds = 1, max_retries;
1359 	struct dpaa2_eth_fq *fq;
1360 	struct netdev_queue *nq;
1361 	struct dpaa2_fd *fd;
1362 	u16 queue_mapping;
1363 	void *swa = NULL;
1364 	u8 prio = 0;
1365 	int err, i;
1366 	u32 fd_len;
1367 
1368 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
1369 	percpu_extras = this_cpu_ptr(priv->percpu_extras);
1370 	fd = (this_cpu_ptr(priv->fd))->array;
1371 
1372 	needed_headroom = dpaa2_eth_needed_headroom(skb);
1373 
1374 	/* We'll be holding a back-reference to the skb until Tx Confirmation;
1375 	 * we don't want that overwritten by a concurrent Tx with a cloned skb.
1376 	 */
1377 	skb = skb_unshare(skb, GFP_ATOMIC);
1378 	if (unlikely(!skb)) {
1379 		/* skb_unshare() has already freed the skb */
1380 		percpu_stats->tx_dropped++;
1381 		return NETDEV_TX_OK;
1382 	}
1383 
1384 	/* Setup the FD fields */
1385 
1386 	if (skb_is_gso(skb)) {
1387 		err = dpaa2_eth_build_gso_fd(priv, skb, fd, &num_fds, &fd_len);
1388 		percpu_extras->tx_sg_frames += num_fds;
1389 		percpu_extras->tx_sg_bytes += fd_len;
1390 		percpu_extras->tx_tso_frames += num_fds;
1391 		percpu_extras->tx_tso_bytes += fd_len;
1392 	} else if (skb_is_nonlinear(skb)) {
1393 		err = dpaa2_eth_build_sg_fd(priv, skb, fd, &swa);
1394 		percpu_extras->tx_sg_frames++;
1395 		percpu_extras->tx_sg_bytes += skb->len;
1396 		fd_len = dpaa2_fd_get_len(fd);
1397 	} else if (skb_headroom(skb) < needed_headroom) {
1398 		err = dpaa2_eth_build_sg_fd_single_buf(priv, skb, fd, &swa);
1399 		percpu_extras->tx_sg_frames++;
1400 		percpu_extras->tx_sg_bytes += skb->len;
1401 		percpu_extras->tx_converted_sg_frames++;
1402 		percpu_extras->tx_converted_sg_bytes += skb->len;
1403 		fd_len = dpaa2_fd_get_len(fd);
1404 	} else {
1405 		err = dpaa2_eth_build_single_fd(priv, skb, fd, &swa);
1406 		fd_len = dpaa2_fd_get_len(fd);
1407 	}
1408 
1409 	if (unlikely(err)) {
1410 		percpu_stats->tx_dropped++;
1411 		goto err_build_fd;
1412 	}
1413 
1414 	if (swa && skb->cb[0])
1415 		dpaa2_eth_enable_tx_tstamp(priv, fd, swa, skb);
1416 
1417 	/* Tracing point */
1418 	for (i = 0; i < num_fds; i++)
1419 		trace_dpaa2_tx_fd(net_dev, &fd[i]);
1420 
1421 	/* TxConf FQ selection relies on queue id from the stack.
1422 	 * In case of a forwarded frame from another DPNI interface, we choose
1423 	 * a queue affined to the same core that processed the Rx frame
1424 	 */
1425 	queue_mapping = skb_get_queue_mapping(skb);
1426 
1427 	if (net_dev->num_tc) {
1428 		prio = netdev_txq_to_tc(net_dev, queue_mapping);
1429 		/* Hardware interprets priority level 0 as being the highest,
1430 		 * so we need to do a reverse mapping to the netdev tc index
1431 		 */
1432 		prio = net_dev->num_tc - prio - 1;
1433 		/* We have only one FQ array entry for all Tx hardware queues
1434 		 * with the same flow id (but different priority levels)
1435 		 */
1436 		queue_mapping %= dpaa2_eth_queue_count(priv);
1437 	}
1438 	fq = &priv->fq[queue_mapping];
1439 	nq = netdev_get_tx_queue(net_dev, queue_mapping);
1440 	netdev_tx_sent_queue(nq, fd_len);
1441 
1442 	/* Everything that happens after this enqueues might race with
1443 	 * the Tx confirmation callback for this frame
1444 	 */
1445 	max_retries = num_fds * DPAA2_ETH_ENQUEUE_RETRIES;
1446 	while (total_enqueued < num_fds && retries < max_retries) {
1447 		err = priv->enqueue(priv, fq, &fd[total_enqueued],
1448 				    prio, num_fds - total_enqueued, &enqueued);
1449 		if (err == -EBUSY) {
1450 			retries++;
1451 			continue;
1452 		}
1453 
1454 		total_enqueued += enqueued;
1455 	}
1456 	percpu_extras->tx_portal_busy += retries;
1457 
1458 	if (unlikely(err < 0)) {
1459 		percpu_stats->tx_errors++;
1460 		/* Clean up everything, including freeing the skb */
1461 		dpaa2_eth_free_tx_fd(priv, fq, fd, false);
1462 		netdev_tx_completed_queue(nq, 1, fd_len);
1463 	} else {
1464 		percpu_stats->tx_packets += total_enqueued;
1465 		percpu_stats->tx_bytes += fd_len;
1466 	}
1467 
1468 	return NETDEV_TX_OK;
1469 
1470 err_build_fd:
1471 	dev_kfree_skb(skb);
1472 
1473 	return NETDEV_TX_OK;
1474 }
1475 
1476 static void dpaa2_eth_tx_onestep_tstamp(struct work_struct *work)
1477 {
1478 	struct dpaa2_eth_priv *priv = container_of(work, struct dpaa2_eth_priv,
1479 						   tx_onestep_tstamp);
1480 	struct sk_buff *skb;
1481 
1482 	while (true) {
1483 		skb = skb_dequeue(&priv->tx_skbs);
1484 		if (!skb)
1485 			return;
1486 
1487 		/* Lock just before TX one-step timestamping packet,
1488 		 * and release the lock in dpaa2_eth_free_tx_fd when
1489 		 * confirm the packet has been sent on hardware, or
1490 		 * when clean up during transmit failure.
1491 		 */
1492 		mutex_lock(&priv->onestep_tstamp_lock);
1493 		__dpaa2_eth_tx(skb, priv->net_dev);
1494 	}
1495 }
1496 
1497 static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev)
1498 {
1499 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1500 	u8 msgtype, twostep, udp;
1501 	u16 offset1, offset2;
1502 
1503 	/* Utilize skb->cb[0] for timestamping request per skb */
1504 	skb->cb[0] = 0;
1505 
1506 	if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && dpaa2_ptp) {
1507 		if (priv->tx_tstamp_type == HWTSTAMP_TX_ON)
1508 			skb->cb[0] = TX_TSTAMP;
1509 		else if (priv->tx_tstamp_type == HWTSTAMP_TX_ONESTEP_SYNC)
1510 			skb->cb[0] = TX_TSTAMP_ONESTEP_SYNC;
1511 	}
1512 
1513 	/* TX for one-step timestamping PTP Sync packet */
1514 	if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
1515 		if (!dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
1516 					 &offset1, &offset2))
1517 			if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0) {
1518 				skb_queue_tail(&priv->tx_skbs, skb);
1519 				queue_work(priv->dpaa2_ptp_wq,
1520 					   &priv->tx_onestep_tstamp);
1521 				return NETDEV_TX_OK;
1522 			}
1523 		/* Use two-step timestamping if not one-step timestamping
1524 		 * PTP Sync packet
1525 		 */
1526 		skb->cb[0] = TX_TSTAMP;
1527 	}
1528 
1529 	/* TX for other packets */
1530 	return __dpaa2_eth_tx(skb, net_dev);
1531 }
1532 
1533 /* Tx confirmation frame processing routine */
1534 static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
1535 			      struct dpaa2_eth_channel *ch,
1536 			      const struct dpaa2_fd *fd,
1537 			      struct dpaa2_eth_fq *fq)
1538 {
1539 	struct rtnl_link_stats64 *percpu_stats;
1540 	struct dpaa2_eth_drv_stats *percpu_extras;
1541 	u32 fd_len = dpaa2_fd_get_len(fd);
1542 	u32 fd_errors;
1543 
1544 	/* Tracing point */
1545 	trace_dpaa2_tx_conf_fd(priv->net_dev, fd);
1546 
1547 	percpu_extras = this_cpu_ptr(priv->percpu_extras);
1548 	percpu_extras->tx_conf_frames++;
1549 	percpu_extras->tx_conf_bytes += fd_len;
1550 	ch->stats.bytes_per_cdan += fd_len;
1551 
1552 	/* Check frame errors in the FD field */
1553 	fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK;
1554 	dpaa2_eth_free_tx_fd(priv, fq, fd, true);
1555 
1556 	if (likely(!fd_errors))
1557 		return;
1558 
1559 	if (net_ratelimit())
1560 		netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n",
1561 			   fd_errors);
1562 
1563 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
1564 	/* Tx-conf logically pertains to the egress path. */
1565 	percpu_stats->tx_errors++;
1566 }
1567 
1568 static int dpaa2_eth_set_rx_vlan_filtering(struct dpaa2_eth_priv *priv,
1569 					   bool enable)
1570 {
1571 	int err;
1572 
1573 	err = dpni_enable_vlan_filter(priv->mc_io, 0, priv->mc_token, enable);
1574 
1575 	if (err) {
1576 		netdev_err(priv->net_dev,
1577 			   "dpni_enable_vlan_filter failed\n");
1578 		return err;
1579 	}
1580 
1581 	return 0;
1582 }
1583 
1584 static int dpaa2_eth_set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
1585 {
1586 	int err;
1587 
1588 	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1589 			       DPNI_OFF_RX_L3_CSUM, enable);
1590 	if (err) {
1591 		netdev_err(priv->net_dev,
1592 			   "dpni_set_offload(RX_L3_CSUM) failed\n");
1593 		return err;
1594 	}
1595 
1596 	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1597 			       DPNI_OFF_RX_L4_CSUM, enable);
1598 	if (err) {
1599 		netdev_err(priv->net_dev,
1600 			   "dpni_set_offload(RX_L4_CSUM) failed\n");
1601 		return err;
1602 	}
1603 
1604 	return 0;
1605 }
1606 
1607 static int dpaa2_eth_set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
1608 {
1609 	int err;
1610 
1611 	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1612 			       DPNI_OFF_TX_L3_CSUM, enable);
1613 	if (err) {
1614 		netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
1615 		return err;
1616 	}
1617 
1618 	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1619 			       DPNI_OFF_TX_L4_CSUM, enable);
1620 	if (err) {
1621 		netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
1622 		return err;
1623 	}
1624 
1625 	return 0;
1626 }
1627 
1628 /* Perform a single release command to add buffers
1629  * to the specified buffer pool
1630  */
1631 static int dpaa2_eth_add_bufs(struct dpaa2_eth_priv *priv,
1632 			      struct dpaa2_eth_channel *ch, u16 bpid)
1633 {
1634 	struct device *dev = priv->net_dev->dev.parent;
1635 	u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
1636 	struct page *page;
1637 	dma_addr_t addr;
1638 	int retries = 0;
1639 	int i, err;
1640 
1641 	for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
1642 		/* Allocate buffer visible to WRIOP + skb shared info +
1643 		 * alignment padding
1644 		 */
1645 		/* allocate one page for each Rx buffer. WRIOP sees
1646 		 * the entire page except for a tailroom reserved for
1647 		 * skb shared info
1648 		 */
1649 		page = dev_alloc_pages(0);
1650 		if (!page)
1651 			goto err_alloc;
1652 
1653 		addr = dma_map_page(dev, page, 0, priv->rx_buf_size,
1654 				    DMA_BIDIRECTIONAL);
1655 		if (unlikely(dma_mapping_error(dev, addr)))
1656 			goto err_map;
1657 
1658 		buf_array[i] = addr;
1659 
1660 		/* tracing point */
1661 		trace_dpaa2_eth_buf_seed(priv->net_dev,
1662 					 page, DPAA2_ETH_RX_BUF_RAW_SIZE,
1663 					 addr, priv->rx_buf_size,
1664 					 bpid);
1665 	}
1666 
1667 release_bufs:
1668 	/* In case the portal is busy, retry until successful */
1669 	while ((err = dpaa2_io_service_release(ch->dpio, bpid,
1670 					       buf_array, i)) == -EBUSY) {
1671 		if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
1672 			break;
1673 		cpu_relax();
1674 	}
1675 
1676 	/* If release command failed, clean up and bail out;
1677 	 * not much else we can do about it
1678 	 */
1679 	if (err) {
1680 		dpaa2_eth_free_bufs(priv, buf_array, i);
1681 		return 0;
1682 	}
1683 
1684 	return i;
1685 
1686 err_map:
1687 	__free_pages(page, 0);
1688 err_alloc:
1689 	/* If we managed to allocate at least some buffers,
1690 	 * release them to hardware
1691 	 */
1692 	if (i)
1693 		goto release_bufs;
1694 
1695 	return 0;
1696 }
1697 
1698 static int dpaa2_eth_seed_pool(struct dpaa2_eth_priv *priv, u16 bpid)
1699 {
1700 	int i, j;
1701 	int new_count;
1702 
1703 	for (j = 0; j < priv->num_channels; j++) {
1704 		for (i = 0; i < DPAA2_ETH_NUM_BUFS;
1705 		     i += DPAA2_ETH_BUFS_PER_CMD) {
1706 			new_count = dpaa2_eth_add_bufs(priv, priv->channel[j], bpid);
1707 			priv->channel[j]->buf_count += new_count;
1708 
1709 			if (new_count < DPAA2_ETH_BUFS_PER_CMD) {
1710 				return -ENOMEM;
1711 			}
1712 		}
1713 	}
1714 
1715 	return 0;
1716 }
1717 
1718 /*
1719  * Drain the specified number of buffers from the DPNI's private buffer pool.
1720  * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
1721  */
1722 static void dpaa2_eth_drain_bufs(struct dpaa2_eth_priv *priv, int count)
1723 {
1724 	u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
1725 	int retries = 0;
1726 	int ret;
1727 
1728 	do {
1729 		ret = dpaa2_io_service_acquire(NULL, priv->bpid,
1730 					       buf_array, count);
1731 		if (ret < 0) {
1732 			if (ret == -EBUSY &&
1733 			    retries++ < DPAA2_ETH_SWP_BUSY_RETRIES)
1734 				continue;
1735 			netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
1736 			return;
1737 		}
1738 		dpaa2_eth_free_bufs(priv, buf_array, ret);
1739 		retries = 0;
1740 	} while (ret);
1741 }
1742 
1743 static void dpaa2_eth_drain_pool(struct dpaa2_eth_priv *priv)
1744 {
1745 	int i;
1746 
1747 	dpaa2_eth_drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD);
1748 	dpaa2_eth_drain_bufs(priv, 1);
1749 
1750 	for (i = 0; i < priv->num_channels; i++)
1751 		priv->channel[i]->buf_count = 0;
1752 }
1753 
1754 /* Function is called from softirq context only, so we don't need to guard
1755  * the access to percpu count
1756  */
1757 static int dpaa2_eth_refill_pool(struct dpaa2_eth_priv *priv,
1758 				 struct dpaa2_eth_channel *ch,
1759 				 u16 bpid)
1760 {
1761 	int new_count;
1762 
1763 	if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
1764 		return 0;
1765 
1766 	do {
1767 		new_count = dpaa2_eth_add_bufs(priv, ch, bpid);
1768 		if (unlikely(!new_count)) {
1769 			/* Out of memory; abort for now, we'll try later on */
1770 			break;
1771 		}
1772 		ch->buf_count += new_count;
1773 	} while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
1774 
1775 	if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
1776 		return -ENOMEM;
1777 
1778 	return 0;
1779 }
1780 
1781 static void dpaa2_eth_sgt_cache_drain(struct dpaa2_eth_priv *priv)
1782 {
1783 	struct dpaa2_eth_sgt_cache *sgt_cache;
1784 	u16 count;
1785 	int k, i;
1786 
1787 	for_each_possible_cpu(k) {
1788 		sgt_cache = per_cpu_ptr(priv->sgt_cache, k);
1789 		count = sgt_cache->count;
1790 
1791 		for (i = 0; i < count; i++)
1792 			skb_free_frag(sgt_cache->buf[i]);
1793 		sgt_cache->count = 0;
1794 	}
1795 }
1796 
1797 static int dpaa2_eth_pull_channel(struct dpaa2_eth_channel *ch)
1798 {
1799 	int err;
1800 	int dequeues = -1;
1801 
1802 	/* Retry while portal is busy */
1803 	do {
1804 		err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
1805 						    ch->store);
1806 		dequeues++;
1807 		cpu_relax();
1808 	} while (err == -EBUSY && dequeues < DPAA2_ETH_SWP_BUSY_RETRIES);
1809 
1810 	ch->stats.dequeue_portal_busy += dequeues;
1811 	if (unlikely(err))
1812 		ch->stats.pull_err++;
1813 
1814 	return err;
1815 }
1816 
1817 /* NAPI poll routine
1818  *
1819  * Frames are dequeued from the QMan channel associated with this NAPI context.
1820  * Rx, Tx confirmation and (if configured) Rx error frames all count
1821  * towards the NAPI budget.
1822  */
1823 static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
1824 {
1825 	struct dpaa2_eth_channel *ch;
1826 	struct dpaa2_eth_priv *priv;
1827 	int rx_cleaned = 0, txconf_cleaned = 0;
1828 	struct dpaa2_eth_fq *fq, *txc_fq = NULL;
1829 	struct netdev_queue *nq;
1830 	int store_cleaned, work_done;
1831 	struct list_head rx_list;
1832 	int retries = 0;
1833 	u16 flowid;
1834 	int err;
1835 
1836 	ch = container_of(napi, struct dpaa2_eth_channel, napi);
1837 	ch->xdp.res = 0;
1838 	priv = ch->priv;
1839 
1840 	INIT_LIST_HEAD(&rx_list);
1841 	ch->rx_list = &rx_list;
1842 
1843 	do {
1844 		err = dpaa2_eth_pull_channel(ch);
1845 		if (unlikely(err))
1846 			break;
1847 
1848 		/* Refill pool if appropriate */
1849 		dpaa2_eth_refill_pool(priv, ch, priv->bpid);
1850 
1851 		store_cleaned = dpaa2_eth_consume_frames(ch, &fq);
1852 		if (store_cleaned <= 0)
1853 			break;
1854 		if (fq->type == DPAA2_RX_FQ) {
1855 			rx_cleaned += store_cleaned;
1856 			flowid = fq->flowid;
1857 		} else {
1858 			txconf_cleaned += store_cleaned;
1859 			/* We have a single Tx conf FQ on this channel */
1860 			txc_fq = fq;
1861 		}
1862 
1863 		/* If we either consumed the whole NAPI budget with Rx frames
1864 		 * or we reached the Tx confirmations threshold, we're done.
1865 		 */
1866 		if (rx_cleaned >= budget ||
1867 		    txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
1868 			work_done = budget;
1869 			goto out;
1870 		}
1871 	} while (store_cleaned);
1872 
1873 	/* Update NET DIM with the values for this CDAN */
1874 	dpaa2_io_update_net_dim(ch->dpio, ch->stats.frames_per_cdan,
1875 				ch->stats.bytes_per_cdan);
1876 	ch->stats.frames_per_cdan = 0;
1877 	ch->stats.bytes_per_cdan = 0;
1878 
1879 	/* We didn't consume the entire budget, so finish napi and
1880 	 * re-enable data availability notifications
1881 	 */
1882 	napi_complete_done(napi, rx_cleaned);
1883 	do {
1884 		err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
1885 		cpu_relax();
1886 	} while (err == -EBUSY && retries++ < DPAA2_ETH_SWP_BUSY_RETRIES);
1887 	WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
1888 		  ch->nctx.desired_cpu);
1889 
1890 	work_done = max(rx_cleaned, 1);
1891 
1892 out:
1893 	netif_receive_skb_list(ch->rx_list);
1894 
1895 	if (txc_fq && txc_fq->dq_frames) {
1896 		nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid);
1897 		netdev_tx_completed_queue(nq, txc_fq->dq_frames,
1898 					  txc_fq->dq_bytes);
1899 		txc_fq->dq_frames = 0;
1900 		txc_fq->dq_bytes = 0;
1901 	}
1902 
1903 	if (ch->xdp.res & XDP_REDIRECT)
1904 		xdp_do_flush_map();
1905 	else if (rx_cleaned && ch->xdp.res & XDP_TX)
1906 		dpaa2_eth_xdp_tx_flush(priv, ch, &priv->fq[flowid]);
1907 
1908 	return work_done;
1909 }
1910 
1911 static void dpaa2_eth_enable_ch_napi(struct dpaa2_eth_priv *priv)
1912 {
1913 	struct dpaa2_eth_channel *ch;
1914 	int i;
1915 
1916 	for (i = 0; i < priv->num_channels; i++) {
1917 		ch = priv->channel[i];
1918 		napi_enable(&ch->napi);
1919 	}
1920 }
1921 
1922 static void dpaa2_eth_disable_ch_napi(struct dpaa2_eth_priv *priv)
1923 {
1924 	struct dpaa2_eth_channel *ch;
1925 	int i;
1926 
1927 	for (i = 0; i < priv->num_channels; i++) {
1928 		ch = priv->channel[i];
1929 		napi_disable(&ch->napi);
1930 	}
1931 }
1932 
1933 void dpaa2_eth_set_rx_taildrop(struct dpaa2_eth_priv *priv,
1934 			       bool tx_pause, bool pfc)
1935 {
1936 	struct dpni_taildrop td = {0};
1937 	struct dpaa2_eth_fq *fq;
1938 	int i, err;
1939 
1940 	/* FQ taildrop: threshold is in bytes, per frame queue. Enabled if
1941 	 * flow control is disabled (as it might interfere with either the
1942 	 * buffer pool depletion trigger for pause frames or with the group
1943 	 * congestion trigger for PFC frames)
1944 	 */
1945 	td.enable = !tx_pause;
1946 	if (priv->rx_fqtd_enabled == td.enable)
1947 		goto set_cgtd;
1948 
1949 	td.threshold = DPAA2_ETH_FQ_TAILDROP_THRESH;
1950 	td.units = DPNI_CONGESTION_UNIT_BYTES;
1951 
1952 	for (i = 0; i < priv->num_fqs; i++) {
1953 		fq = &priv->fq[i];
1954 		if (fq->type != DPAA2_RX_FQ)
1955 			continue;
1956 		err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
1957 					DPNI_CP_QUEUE, DPNI_QUEUE_RX,
1958 					fq->tc, fq->flowid, &td);
1959 		if (err) {
1960 			netdev_err(priv->net_dev,
1961 				   "dpni_set_taildrop(FQ) failed\n");
1962 			return;
1963 		}
1964 	}
1965 
1966 	priv->rx_fqtd_enabled = td.enable;
1967 
1968 set_cgtd:
1969 	/* Congestion group taildrop: threshold is in frames, per group
1970 	 * of FQs belonging to the same traffic class
1971 	 * Enabled if general Tx pause disabled or if PFCs are enabled
1972 	 * (congestion group threhsold for PFC generation is lower than the
1973 	 * CG taildrop threshold, so it won't interfere with it; we also
1974 	 * want frames in non-PFC enabled traffic classes to be kept in check)
1975 	 */
1976 	td.enable = !tx_pause || pfc;
1977 	if (priv->rx_cgtd_enabled == td.enable)
1978 		return;
1979 
1980 	td.threshold = DPAA2_ETH_CG_TAILDROP_THRESH(priv);
1981 	td.units = DPNI_CONGESTION_UNIT_FRAMES;
1982 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
1983 		err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
1984 					DPNI_CP_GROUP, DPNI_QUEUE_RX,
1985 					i, 0, &td);
1986 		if (err) {
1987 			netdev_err(priv->net_dev,
1988 				   "dpni_set_taildrop(CG) failed\n");
1989 			return;
1990 		}
1991 	}
1992 
1993 	priv->rx_cgtd_enabled = td.enable;
1994 }
1995 
1996 static int dpaa2_eth_link_state_update(struct dpaa2_eth_priv *priv)
1997 {
1998 	struct dpni_link_state state = {0};
1999 	bool tx_pause;
2000 	int err;
2001 
2002 	err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
2003 	if (unlikely(err)) {
2004 		netdev_err(priv->net_dev,
2005 			   "dpni_get_link_state() failed\n");
2006 		return err;
2007 	}
2008 
2009 	/* If Tx pause frame settings have changed, we need to update
2010 	 * Rx FQ taildrop configuration as well. We configure taildrop
2011 	 * only when pause frame generation is disabled.
2012 	 */
2013 	tx_pause = dpaa2_eth_tx_pause_enabled(state.options);
2014 	dpaa2_eth_set_rx_taildrop(priv, tx_pause, priv->pfc_enabled);
2015 
2016 	/* When we manage the MAC/PHY using phylink there is no need
2017 	 * to manually update the netif_carrier.
2018 	 */
2019 	if (dpaa2_eth_is_type_phy(priv))
2020 		goto out;
2021 
2022 	/* Chech link state; speed / duplex changes are not treated yet */
2023 	if (priv->link_state.up == state.up)
2024 		goto out;
2025 
2026 	if (state.up) {
2027 		netif_carrier_on(priv->net_dev);
2028 		netif_tx_start_all_queues(priv->net_dev);
2029 	} else {
2030 		netif_tx_stop_all_queues(priv->net_dev);
2031 		netif_carrier_off(priv->net_dev);
2032 	}
2033 
2034 	netdev_info(priv->net_dev, "Link Event: state %s\n",
2035 		    state.up ? "up" : "down");
2036 
2037 out:
2038 	priv->link_state = state;
2039 
2040 	return 0;
2041 }
2042 
2043 static int dpaa2_eth_open(struct net_device *net_dev)
2044 {
2045 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2046 	int err;
2047 
2048 	err = dpaa2_eth_seed_pool(priv, priv->bpid);
2049 	if (err) {
2050 		/* Not much to do; the buffer pool, though not filled up,
2051 		 * may still contain some buffers which would enable us
2052 		 * to limp on.
2053 		 */
2054 		netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
2055 			   priv->dpbp_dev->obj_desc.id, priv->bpid);
2056 	}
2057 
2058 	if (!dpaa2_eth_is_type_phy(priv)) {
2059 		/* We'll only start the txqs when the link is actually ready;
2060 		 * make sure we don't race against the link up notification,
2061 		 * which may come immediately after dpni_enable();
2062 		 */
2063 		netif_tx_stop_all_queues(net_dev);
2064 
2065 		/* Also, explicitly set carrier off, otherwise
2066 		 * netif_carrier_ok() will return true and cause 'ip link show'
2067 		 * to report the LOWER_UP flag, even though the link
2068 		 * notification wasn't even received.
2069 		 */
2070 		netif_carrier_off(net_dev);
2071 	}
2072 	dpaa2_eth_enable_ch_napi(priv);
2073 
2074 	err = dpni_enable(priv->mc_io, 0, priv->mc_token);
2075 	if (err < 0) {
2076 		netdev_err(net_dev, "dpni_enable() failed\n");
2077 		goto enable_err;
2078 	}
2079 
2080 	if (dpaa2_eth_is_type_phy(priv))
2081 		phylink_start(priv->mac->phylink);
2082 
2083 	return 0;
2084 
2085 enable_err:
2086 	dpaa2_eth_disable_ch_napi(priv);
2087 	dpaa2_eth_drain_pool(priv);
2088 	return err;
2089 }
2090 
2091 /* Total number of in-flight frames on ingress queues */
2092 static u32 dpaa2_eth_ingress_fq_count(struct dpaa2_eth_priv *priv)
2093 {
2094 	struct dpaa2_eth_fq *fq;
2095 	u32 fcnt = 0, bcnt = 0, total = 0;
2096 	int i, err;
2097 
2098 	for (i = 0; i < priv->num_fqs; i++) {
2099 		fq = &priv->fq[i];
2100 		err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
2101 		if (err) {
2102 			netdev_warn(priv->net_dev, "query_fq_count failed");
2103 			break;
2104 		}
2105 		total += fcnt;
2106 	}
2107 
2108 	return total;
2109 }
2110 
2111 static void dpaa2_eth_wait_for_ingress_fq_empty(struct dpaa2_eth_priv *priv)
2112 {
2113 	int retries = 10;
2114 	u32 pending;
2115 
2116 	do {
2117 		pending = dpaa2_eth_ingress_fq_count(priv);
2118 		if (pending)
2119 			msleep(100);
2120 	} while (pending && --retries);
2121 }
2122 
2123 #define DPNI_TX_PENDING_VER_MAJOR	7
2124 #define DPNI_TX_PENDING_VER_MINOR	13
2125 static void dpaa2_eth_wait_for_egress_fq_empty(struct dpaa2_eth_priv *priv)
2126 {
2127 	union dpni_statistics stats;
2128 	int retries = 10;
2129 	int err;
2130 
2131 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_TX_PENDING_VER_MAJOR,
2132 				   DPNI_TX_PENDING_VER_MINOR) < 0)
2133 		goto out;
2134 
2135 	do {
2136 		err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token, 6,
2137 					  &stats);
2138 		if (err)
2139 			goto out;
2140 		if (stats.page_6.tx_pending_frames == 0)
2141 			return;
2142 	} while (--retries);
2143 
2144 out:
2145 	msleep(500);
2146 }
2147 
2148 static int dpaa2_eth_stop(struct net_device *net_dev)
2149 {
2150 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2151 	int dpni_enabled = 0;
2152 	int retries = 10;
2153 
2154 	if (dpaa2_eth_is_type_phy(priv)) {
2155 		phylink_stop(priv->mac->phylink);
2156 	} else {
2157 		netif_tx_stop_all_queues(net_dev);
2158 		netif_carrier_off(net_dev);
2159 	}
2160 
2161 	/* On dpni_disable(), the MC firmware will:
2162 	 * - stop MAC Rx and wait for all Rx frames to be enqueued to software
2163 	 * - cut off WRIOP dequeues from egress FQs and wait until transmission
2164 	 * of all in flight Tx frames is finished (and corresponding Tx conf
2165 	 * frames are enqueued back to software)
2166 	 *
2167 	 * Before calling dpni_disable(), we wait for all Tx frames to arrive
2168 	 * on WRIOP. After it finishes, wait until all remaining frames on Rx
2169 	 * and Tx conf queues are consumed on NAPI poll.
2170 	 */
2171 	dpaa2_eth_wait_for_egress_fq_empty(priv);
2172 
2173 	do {
2174 		dpni_disable(priv->mc_io, 0, priv->mc_token);
2175 		dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
2176 		if (dpni_enabled)
2177 			/* Allow the hardware some slack */
2178 			msleep(100);
2179 	} while (dpni_enabled && --retries);
2180 	if (!retries) {
2181 		netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
2182 		/* Must go on and disable NAPI nonetheless, so we don't crash at
2183 		 * the next "ifconfig up"
2184 		 */
2185 	}
2186 
2187 	dpaa2_eth_wait_for_ingress_fq_empty(priv);
2188 	dpaa2_eth_disable_ch_napi(priv);
2189 
2190 	/* Empty the buffer pool */
2191 	dpaa2_eth_drain_pool(priv);
2192 
2193 	/* Empty the Scatter-Gather Buffer cache */
2194 	dpaa2_eth_sgt_cache_drain(priv);
2195 
2196 	return 0;
2197 }
2198 
2199 static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
2200 {
2201 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2202 	struct device *dev = net_dev->dev.parent;
2203 	int err;
2204 
2205 	err = eth_mac_addr(net_dev, addr);
2206 	if (err < 0) {
2207 		dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
2208 		return err;
2209 	}
2210 
2211 	err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
2212 					net_dev->dev_addr);
2213 	if (err) {
2214 		dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
2215 		return err;
2216 	}
2217 
2218 	return 0;
2219 }
2220 
2221 /** Fill in counters maintained by the GPP driver. These may be different from
2222  * the hardware counters obtained by ethtool.
2223  */
2224 static void dpaa2_eth_get_stats(struct net_device *net_dev,
2225 				struct rtnl_link_stats64 *stats)
2226 {
2227 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2228 	struct rtnl_link_stats64 *percpu_stats;
2229 	u64 *cpustats;
2230 	u64 *netstats = (u64 *)stats;
2231 	int i, j;
2232 	int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
2233 
2234 	for_each_possible_cpu(i) {
2235 		percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
2236 		cpustats = (u64 *)percpu_stats;
2237 		for (j = 0; j < num; j++)
2238 			netstats[j] += cpustats[j];
2239 	}
2240 }
2241 
2242 /* Copy mac unicast addresses from @net_dev to @priv.
2243  * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
2244  */
2245 static void dpaa2_eth_add_uc_hw_addr(const struct net_device *net_dev,
2246 				     struct dpaa2_eth_priv *priv)
2247 {
2248 	struct netdev_hw_addr *ha;
2249 	int err;
2250 
2251 	netdev_for_each_uc_addr(ha, net_dev) {
2252 		err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
2253 					ha->addr);
2254 		if (err)
2255 			netdev_warn(priv->net_dev,
2256 				    "Could not add ucast MAC %pM to the filtering table (err %d)\n",
2257 				    ha->addr, err);
2258 	}
2259 }
2260 
2261 /* Copy mac multicast addresses from @net_dev to @priv
2262  * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
2263  */
2264 static void dpaa2_eth_add_mc_hw_addr(const struct net_device *net_dev,
2265 				     struct dpaa2_eth_priv *priv)
2266 {
2267 	struct netdev_hw_addr *ha;
2268 	int err;
2269 
2270 	netdev_for_each_mc_addr(ha, net_dev) {
2271 		err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
2272 					ha->addr);
2273 		if (err)
2274 			netdev_warn(priv->net_dev,
2275 				    "Could not add mcast MAC %pM to the filtering table (err %d)\n",
2276 				    ha->addr, err);
2277 	}
2278 }
2279 
2280 static int dpaa2_eth_rx_add_vid(struct net_device *net_dev,
2281 				__be16 vlan_proto, u16 vid)
2282 {
2283 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2284 	int err;
2285 
2286 	err = dpni_add_vlan_id(priv->mc_io, 0, priv->mc_token,
2287 			       vid, 0, 0, 0);
2288 
2289 	if (err) {
2290 		netdev_warn(priv->net_dev,
2291 			    "Could not add the vlan id %u\n",
2292 			    vid);
2293 		return err;
2294 	}
2295 
2296 	return 0;
2297 }
2298 
2299 static int dpaa2_eth_rx_kill_vid(struct net_device *net_dev,
2300 				 __be16 vlan_proto, u16 vid)
2301 {
2302 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2303 	int err;
2304 
2305 	err = dpni_remove_vlan_id(priv->mc_io, 0, priv->mc_token, vid);
2306 
2307 	if (err) {
2308 		netdev_warn(priv->net_dev,
2309 			    "Could not remove the vlan id %u\n",
2310 			    vid);
2311 		return err;
2312 	}
2313 
2314 	return 0;
2315 }
2316 
2317 static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
2318 {
2319 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2320 	int uc_count = netdev_uc_count(net_dev);
2321 	int mc_count = netdev_mc_count(net_dev);
2322 	u8 max_mac = priv->dpni_attrs.mac_filter_entries;
2323 	u32 options = priv->dpni_attrs.options;
2324 	u16 mc_token = priv->mc_token;
2325 	struct fsl_mc_io *mc_io = priv->mc_io;
2326 	int err;
2327 
2328 	/* Basic sanity checks; these probably indicate a misconfiguration */
2329 	if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
2330 		netdev_info(net_dev,
2331 			    "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
2332 			    max_mac);
2333 
2334 	/* Force promiscuous if the uc or mc counts exceed our capabilities. */
2335 	if (uc_count > max_mac) {
2336 		netdev_info(net_dev,
2337 			    "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
2338 			    uc_count, max_mac);
2339 		goto force_promisc;
2340 	}
2341 	if (mc_count + uc_count > max_mac) {
2342 		netdev_info(net_dev,
2343 			    "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
2344 			    uc_count + mc_count, max_mac);
2345 		goto force_mc_promisc;
2346 	}
2347 
2348 	/* Adjust promisc settings due to flag combinations */
2349 	if (net_dev->flags & IFF_PROMISC)
2350 		goto force_promisc;
2351 	if (net_dev->flags & IFF_ALLMULTI) {
2352 		/* First, rebuild unicast filtering table. This should be done
2353 		 * in promisc mode, in order to avoid frame loss while we
2354 		 * progressively add entries to the table.
2355 		 * We don't know whether we had been in promisc already, and
2356 		 * making an MC call to find out is expensive; so set uc promisc
2357 		 * nonetheless.
2358 		 */
2359 		err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2360 		if (err)
2361 			netdev_warn(net_dev, "Can't set uc promisc\n");
2362 
2363 		/* Actual uc table reconstruction. */
2364 		err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
2365 		if (err)
2366 			netdev_warn(net_dev, "Can't clear uc filters\n");
2367 		dpaa2_eth_add_uc_hw_addr(net_dev, priv);
2368 
2369 		/* Finally, clear uc promisc and set mc promisc as requested. */
2370 		err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
2371 		if (err)
2372 			netdev_warn(net_dev, "Can't clear uc promisc\n");
2373 		goto force_mc_promisc;
2374 	}
2375 
2376 	/* Neither unicast, nor multicast promisc will be on... eventually.
2377 	 * For now, rebuild mac filtering tables while forcing both of them on.
2378 	 */
2379 	err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2380 	if (err)
2381 		netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
2382 	err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
2383 	if (err)
2384 		netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
2385 
2386 	/* Actual mac filtering tables reconstruction */
2387 	err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
2388 	if (err)
2389 		netdev_warn(net_dev, "Can't clear mac filters\n");
2390 	dpaa2_eth_add_mc_hw_addr(net_dev, priv);
2391 	dpaa2_eth_add_uc_hw_addr(net_dev, priv);
2392 
2393 	/* Now we can clear both ucast and mcast promisc, without risking
2394 	 * to drop legitimate frames anymore.
2395 	 */
2396 	err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
2397 	if (err)
2398 		netdev_warn(net_dev, "Can't clear ucast promisc\n");
2399 	err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
2400 	if (err)
2401 		netdev_warn(net_dev, "Can't clear mcast promisc\n");
2402 
2403 	return;
2404 
2405 force_promisc:
2406 	err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2407 	if (err)
2408 		netdev_warn(net_dev, "Can't set ucast promisc\n");
2409 force_mc_promisc:
2410 	err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
2411 	if (err)
2412 		netdev_warn(net_dev, "Can't set mcast promisc\n");
2413 }
2414 
2415 static int dpaa2_eth_set_features(struct net_device *net_dev,
2416 				  netdev_features_t features)
2417 {
2418 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2419 	netdev_features_t changed = features ^ net_dev->features;
2420 	bool enable;
2421 	int err;
2422 
2423 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
2424 		enable = !!(features & NETIF_F_HW_VLAN_CTAG_FILTER);
2425 		err = dpaa2_eth_set_rx_vlan_filtering(priv, enable);
2426 		if (err)
2427 			return err;
2428 	}
2429 
2430 	if (changed & NETIF_F_RXCSUM) {
2431 		enable = !!(features & NETIF_F_RXCSUM);
2432 		err = dpaa2_eth_set_rx_csum(priv, enable);
2433 		if (err)
2434 			return err;
2435 	}
2436 
2437 	if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
2438 		enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
2439 		err = dpaa2_eth_set_tx_csum(priv, enable);
2440 		if (err)
2441 			return err;
2442 	}
2443 
2444 	return 0;
2445 }
2446 
2447 static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2448 {
2449 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
2450 	struct hwtstamp_config config;
2451 
2452 	if (!dpaa2_ptp)
2453 		return -EINVAL;
2454 
2455 	if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
2456 		return -EFAULT;
2457 
2458 	switch (config.tx_type) {
2459 	case HWTSTAMP_TX_OFF:
2460 	case HWTSTAMP_TX_ON:
2461 	case HWTSTAMP_TX_ONESTEP_SYNC:
2462 		priv->tx_tstamp_type = config.tx_type;
2463 		break;
2464 	default:
2465 		return -ERANGE;
2466 	}
2467 
2468 	if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
2469 		priv->rx_tstamp = false;
2470 	} else {
2471 		priv->rx_tstamp = true;
2472 		/* TS is set for all frame types, not only those requested */
2473 		config.rx_filter = HWTSTAMP_FILTER_ALL;
2474 	}
2475 
2476 	if (priv->tx_tstamp_type == HWTSTAMP_TX_ONESTEP_SYNC)
2477 		dpaa2_ptp_onestep_reg_update_method(priv);
2478 
2479 	return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
2480 			-EFAULT : 0;
2481 }
2482 
2483 static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2484 {
2485 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
2486 
2487 	if (cmd == SIOCSHWTSTAMP)
2488 		return dpaa2_eth_ts_ioctl(dev, rq, cmd);
2489 
2490 	if (dpaa2_eth_is_type_phy(priv))
2491 		return phylink_mii_ioctl(priv->mac->phylink, rq, cmd);
2492 
2493 	return -EOPNOTSUPP;
2494 }
2495 
2496 static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu)
2497 {
2498 	int mfl, linear_mfl;
2499 
2500 	mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
2501 	linear_mfl = priv->rx_buf_size - DPAA2_ETH_RX_HWA_SIZE -
2502 		     dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM;
2503 
2504 	if (mfl > linear_mfl) {
2505 		netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
2506 			    linear_mfl - VLAN_ETH_HLEN);
2507 		return false;
2508 	}
2509 
2510 	return true;
2511 }
2512 
2513 static int dpaa2_eth_set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
2514 {
2515 	int mfl, err;
2516 
2517 	/* We enforce a maximum Rx frame length based on MTU only if we have
2518 	 * an XDP program attached (in order to avoid Rx S/G frames).
2519 	 * Otherwise, we accept all incoming frames as long as they are not
2520 	 * larger than maximum size supported in hardware
2521 	 */
2522 	if (has_xdp)
2523 		mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
2524 	else
2525 		mfl = DPAA2_ETH_MFL;
2526 
2527 	err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl);
2528 	if (err) {
2529 		netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n");
2530 		return err;
2531 	}
2532 
2533 	return 0;
2534 }
2535 
2536 static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu)
2537 {
2538 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
2539 	int err;
2540 
2541 	if (!priv->xdp_prog)
2542 		goto out;
2543 
2544 	if (!xdp_mtu_valid(priv, new_mtu))
2545 		return -EINVAL;
2546 
2547 	err = dpaa2_eth_set_rx_mfl(priv, new_mtu, true);
2548 	if (err)
2549 		return err;
2550 
2551 out:
2552 	dev->mtu = new_mtu;
2553 	return 0;
2554 }
2555 
2556 static int dpaa2_eth_update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp)
2557 {
2558 	struct dpni_buffer_layout buf_layout = {0};
2559 	int err;
2560 
2561 	err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token,
2562 				     DPNI_QUEUE_RX, &buf_layout);
2563 	if (err) {
2564 		netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n");
2565 		return err;
2566 	}
2567 
2568 	/* Reserve extra headroom for XDP header size changes */
2569 	buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) +
2570 				    (has_xdp ? XDP_PACKET_HEADROOM : 0);
2571 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM;
2572 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2573 				     DPNI_QUEUE_RX, &buf_layout);
2574 	if (err) {
2575 		netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n");
2576 		return err;
2577 	}
2578 
2579 	return 0;
2580 }
2581 
2582 static int dpaa2_eth_setup_xdp(struct net_device *dev, struct bpf_prog *prog)
2583 {
2584 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
2585 	struct dpaa2_eth_channel *ch;
2586 	struct bpf_prog *old;
2587 	bool up, need_update;
2588 	int i, err;
2589 
2590 	if (prog && !xdp_mtu_valid(priv, dev->mtu))
2591 		return -EINVAL;
2592 
2593 	if (prog)
2594 		bpf_prog_add(prog, priv->num_channels);
2595 
2596 	up = netif_running(dev);
2597 	need_update = (!!priv->xdp_prog != !!prog);
2598 
2599 	if (up)
2600 		dpaa2_eth_stop(dev);
2601 
2602 	/* While in xdp mode, enforce a maximum Rx frame size based on MTU.
2603 	 * Also, when switching between xdp/non-xdp modes we need to reconfigure
2604 	 * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop,
2605 	 * so we are sure no old format buffers will be used from now on.
2606 	 */
2607 	if (need_update) {
2608 		err = dpaa2_eth_set_rx_mfl(priv, dev->mtu, !!prog);
2609 		if (err)
2610 			goto out_err;
2611 		err = dpaa2_eth_update_rx_buffer_headroom(priv, !!prog);
2612 		if (err)
2613 			goto out_err;
2614 	}
2615 
2616 	old = xchg(&priv->xdp_prog, prog);
2617 	if (old)
2618 		bpf_prog_put(old);
2619 
2620 	for (i = 0; i < priv->num_channels; i++) {
2621 		ch = priv->channel[i];
2622 		old = xchg(&ch->xdp.prog, prog);
2623 		if (old)
2624 			bpf_prog_put(old);
2625 	}
2626 
2627 	if (up) {
2628 		err = dpaa2_eth_open(dev);
2629 		if (err)
2630 			return err;
2631 	}
2632 
2633 	return 0;
2634 
2635 out_err:
2636 	if (prog)
2637 		bpf_prog_sub(prog, priv->num_channels);
2638 	if (up)
2639 		dpaa2_eth_open(dev);
2640 
2641 	return err;
2642 }
2643 
2644 static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2645 {
2646 	switch (xdp->command) {
2647 	case XDP_SETUP_PROG:
2648 		return dpaa2_eth_setup_xdp(dev, xdp->prog);
2649 	default:
2650 		return -EINVAL;
2651 	}
2652 
2653 	return 0;
2654 }
2655 
2656 static int dpaa2_eth_xdp_create_fd(struct net_device *net_dev,
2657 				   struct xdp_frame *xdpf,
2658 				   struct dpaa2_fd *fd)
2659 {
2660 	struct device *dev = net_dev->dev.parent;
2661 	unsigned int needed_headroom;
2662 	struct dpaa2_eth_swa *swa;
2663 	void *buffer_start, *aligned_start;
2664 	dma_addr_t addr;
2665 
2666 	/* We require a minimum headroom to be able to transmit the frame.
2667 	 * Otherwise return an error and let the original net_device handle it
2668 	 */
2669 	needed_headroom = dpaa2_eth_needed_headroom(NULL);
2670 	if (xdpf->headroom < needed_headroom)
2671 		return -EINVAL;
2672 
2673 	/* Setup the FD fields */
2674 	memset(fd, 0, sizeof(*fd));
2675 
2676 	/* Align FD address, if possible */
2677 	buffer_start = xdpf->data - needed_headroom;
2678 	aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
2679 				  DPAA2_ETH_TX_BUF_ALIGN);
2680 	if (aligned_start >= xdpf->data - xdpf->headroom)
2681 		buffer_start = aligned_start;
2682 
2683 	swa = (struct dpaa2_eth_swa *)buffer_start;
2684 	/* fill in necessary fields here */
2685 	swa->type = DPAA2_ETH_SWA_XDP;
2686 	swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start;
2687 	swa->xdp.xdpf = xdpf;
2688 
2689 	addr = dma_map_single(dev, buffer_start,
2690 			      swa->xdp.dma_size,
2691 			      DMA_BIDIRECTIONAL);
2692 	if (unlikely(dma_mapping_error(dev, addr)))
2693 		return -ENOMEM;
2694 
2695 	dpaa2_fd_set_addr(fd, addr);
2696 	dpaa2_fd_set_offset(fd, xdpf->data - buffer_start);
2697 	dpaa2_fd_set_len(fd, xdpf->len);
2698 	dpaa2_fd_set_format(fd, dpaa2_fd_single);
2699 	dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
2700 
2701 	return 0;
2702 }
2703 
2704 static int dpaa2_eth_xdp_xmit(struct net_device *net_dev, int n,
2705 			      struct xdp_frame **frames, u32 flags)
2706 {
2707 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2708 	struct dpaa2_eth_xdp_fds *xdp_redirect_fds;
2709 	struct rtnl_link_stats64 *percpu_stats;
2710 	struct dpaa2_eth_fq *fq;
2711 	struct dpaa2_fd *fds;
2712 	int enqueued, i, err;
2713 
2714 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2715 		return -EINVAL;
2716 
2717 	if (!netif_running(net_dev))
2718 		return -ENETDOWN;
2719 
2720 	fq = &priv->fq[smp_processor_id()];
2721 	xdp_redirect_fds = &fq->xdp_redirect_fds;
2722 	fds = xdp_redirect_fds->fds;
2723 
2724 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
2725 
2726 	/* create a FD for each xdp_frame in the list received */
2727 	for (i = 0; i < n; i++) {
2728 		err = dpaa2_eth_xdp_create_fd(net_dev, frames[i], &fds[i]);
2729 		if (err)
2730 			break;
2731 	}
2732 	xdp_redirect_fds->num = i;
2733 
2734 	/* enqueue all the frame descriptors */
2735 	enqueued = dpaa2_eth_xdp_flush(priv, fq, xdp_redirect_fds);
2736 
2737 	/* update statistics */
2738 	percpu_stats->tx_packets += enqueued;
2739 	for (i = 0; i < enqueued; i++)
2740 		percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
2741 
2742 	return enqueued;
2743 }
2744 
2745 static int update_xps(struct dpaa2_eth_priv *priv)
2746 {
2747 	struct net_device *net_dev = priv->net_dev;
2748 	struct cpumask xps_mask;
2749 	struct dpaa2_eth_fq *fq;
2750 	int i, num_queues, netdev_queues;
2751 	int err = 0;
2752 
2753 	num_queues = dpaa2_eth_queue_count(priv);
2754 	netdev_queues = (net_dev->num_tc ? : 1) * num_queues;
2755 
2756 	/* The first <num_queues> entries in priv->fq array are Tx/Tx conf
2757 	 * queues, so only process those
2758 	 */
2759 	for (i = 0; i < netdev_queues; i++) {
2760 		fq = &priv->fq[i % num_queues];
2761 
2762 		cpumask_clear(&xps_mask);
2763 		cpumask_set_cpu(fq->target_cpu, &xps_mask);
2764 
2765 		err = netif_set_xps_queue(net_dev, &xps_mask, i);
2766 		if (err) {
2767 			netdev_warn_once(net_dev, "Error setting XPS queue\n");
2768 			break;
2769 		}
2770 	}
2771 
2772 	return err;
2773 }
2774 
2775 static int dpaa2_eth_setup_mqprio(struct net_device *net_dev,
2776 				  struct tc_mqprio_qopt *mqprio)
2777 {
2778 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2779 	u8 num_tc, num_queues;
2780 	int i;
2781 
2782 	mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
2783 	num_queues = dpaa2_eth_queue_count(priv);
2784 	num_tc = mqprio->num_tc;
2785 
2786 	if (num_tc == net_dev->num_tc)
2787 		return 0;
2788 
2789 	if (num_tc  > dpaa2_eth_tc_count(priv)) {
2790 		netdev_err(net_dev, "Max %d traffic classes supported\n",
2791 			   dpaa2_eth_tc_count(priv));
2792 		return -EOPNOTSUPP;
2793 	}
2794 
2795 	if (!num_tc) {
2796 		netdev_reset_tc(net_dev);
2797 		netif_set_real_num_tx_queues(net_dev, num_queues);
2798 		goto out;
2799 	}
2800 
2801 	netdev_set_num_tc(net_dev, num_tc);
2802 	netif_set_real_num_tx_queues(net_dev, num_tc * num_queues);
2803 
2804 	for (i = 0; i < num_tc; i++)
2805 		netdev_set_tc_queue(net_dev, i, num_queues, i * num_queues);
2806 
2807 out:
2808 	update_xps(priv);
2809 
2810 	return 0;
2811 }
2812 
2813 #define bps_to_mbits(rate) (div_u64((rate), 1000000) * 8)
2814 
2815 static int dpaa2_eth_setup_tbf(struct net_device *net_dev, struct tc_tbf_qopt_offload *p)
2816 {
2817 	struct tc_tbf_qopt_offload_replace_params *cfg = &p->replace_params;
2818 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2819 	struct dpni_tx_shaping_cfg tx_cr_shaper = { 0 };
2820 	struct dpni_tx_shaping_cfg tx_er_shaper = { 0 };
2821 	int err;
2822 
2823 	if (p->command == TC_TBF_STATS)
2824 		return -EOPNOTSUPP;
2825 
2826 	/* Only per port Tx shaping */
2827 	if (p->parent != TC_H_ROOT)
2828 		return -EOPNOTSUPP;
2829 
2830 	if (p->command == TC_TBF_REPLACE) {
2831 		if (cfg->max_size > DPAA2_ETH_MAX_BURST_SIZE) {
2832 			netdev_err(net_dev, "burst size cannot be greater than %d\n",
2833 				   DPAA2_ETH_MAX_BURST_SIZE);
2834 			return -EINVAL;
2835 		}
2836 
2837 		tx_cr_shaper.max_burst_size = cfg->max_size;
2838 		/* The TBF interface is in bytes/s, whereas DPAA2 expects the
2839 		 * rate in Mbits/s
2840 		 */
2841 		tx_cr_shaper.rate_limit = bps_to_mbits(cfg->rate.rate_bytes_ps);
2842 	}
2843 
2844 	err = dpni_set_tx_shaping(priv->mc_io, 0, priv->mc_token, &tx_cr_shaper,
2845 				  &tx_er_shaper, 0);
2846 	if (err) {
2847 		netdev_err(net_dev, "dpni_set_tx_shaping() = %d\n", err);
2848 		return err;
2849 	}
2850 
2851 	return 0;
2852 }
2853 
2854 static int dpaa2_eth_setup_tc(struct net_device *net_dev,
2855 			      enum tc_setup_type type, void *type_data)
2856 {
2857 	switch (type) {
2858 	case TC_SETUP_QDISC_MQPRIO:
2859 		return dpaa2_eth_setup_mqprio(net_dev, type_data);
2860 	case TC_SETUP_QDISC_TBF:
2861 		return dpaa2_eth_setup_tbf(net_dev, type_data);
2862 	default:
2863 		return -EOPNOTSUPP;
2864 	}
2865 }
2866 
2867 static const struct net_device_ops dpaa2_eth_ops = {
2868 	.ndo_open = dpaa2_eth_open,
2869 	.ndo_start_xmit = dpaa2_eth_tx,
2870 	.ndo_stop = dpaa2_eth_stop,
2871 	.ndo_set_mac_address = dpaa2_eth_set_addr,
2872 	.ndo_get_stats64 = dpaa2_eth_get_stats,
2873 	.ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
2874 	.ndo_set_features = dpaa2_eth_set_features,
2875 	.ndo_eth_ioctl = dpaa2_eth_ioctl,
2876 	.ndo_change_mtu = dpaa2_eth_change_mtu,
2877 	.ndo_bpf = dpaa2_eth_xdp,
2878 	.ndo_xdp_xmit = dpaa2_eth_xdp_xmit,
2879 	.ndo_setup_tc = dpaa2_eth_setup_tc,
2880 	.ndo_vlan_rx_add_vid = dpaa2_eth_rx_add_vid,
2881 	.ndo_vlan_rx_kill_vid = dpaa2_eth_rx_kill_vid
2882 };
2883 
2884 static void dpaa2_eth_cdan_cb(struct dpaa2_io_notification_ctx *ctx)
2885 {
2886 	struct dpaa2_eth_channel *ch;
2887 
2888 	ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
2889 
2890 	/* Update NAPI statistics */
2891 	ch->stats.cdan++;
2892 
2893 	napi_schedule(&ch->napi);
2894 }
2895 
2896 /* Allocate and configure a DPCON object */
2897 static struct fsl_mc_device *dpaa2_eth_setup_dpcon(struct dpaa2_eth_priv *priv)
2898 {
2899 	struct fsl_mc_device *dpcon;
2900 	struct device *dev = priv->net_dev->dev.parent;
2901 	int err;
2902 
2903 	err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
2904 				     FSL_MC_POOL_DPCON, &dpcon);
2905 	if (err) {
2906 		if (err == -ENXIO)
2907 			err = -EPROBE_DEFER;
2908 		else
2909 			dev_info(dev, "Not enough DPCONs, will go on as-is\n");
2910 		return ERR_PTR(err);
2911 	}
2912 
2913 	err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
2914 	if (err) {
2915 		dev_err(dev, "dpcon_open() failed\n");
2916 		goto free;
2917 	}
2918 
2919 	err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
2920 	if (err) {
2921 		dev_err(dev, "dpcon_reset() failed\n");
2922 		goto close;
2923 	}
2924 
2925 	err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
2926 	if (err) {
2927 		dev_err(dev, "dpcon_enable() failed\n");
2928 		goto close;
2929 	}
2930 
2931 	return dpcon;
2932 
2933 close:
2934 	dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
2935 free:
2936 	fsl_mc_object_free(dpcon);
2937 
2938 	return ERR_PTR(err);
2939 }
2940 
2941 static void dpaa2_eth_free_dpcon(struct dpaa2_eth_priv *priv,
2942 				 struct fsl_mc_device *dpcon)
2943 {
2944 	dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
2945 	dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
2946 	fsl_mc_object_free(dpcon);
2947 }
2948 
2949 static struct dpaa2_eth_channel *dpaa2_eth_alloc_channel(struct dpaa2_eth_priv *priv)
2950 {
2951 	struct dpaa2_eth_channel *channel;
2952 	struct dpcon_attr attr;
2953 	struct device *dev = priv->net_dev->dev.parent;
2954 	int err;
2955 
2956 	channel = kzalloc(sizeof(*channel), GFP_KERNEL);
2957 	if (!channel)
2958 		return NULL;
2959 
2960 	channel->dpcon = dpaa2_eth_setup_dpcon(priv);
2961 	if (IS_ERR(channel->dpcon)) {
2962 		err = PTR_ERR(channel->dpcon);
2963 		goto err_setup;
2964 	}
2965 
2966 	err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
2967 				   &attr);
2968 	if (err) {
2969 		dev_err(dev, "dpcon_get_attributes() failed\n");
2970 		goto err_get_attr;
2971 	}
2972 
2973 	channel->dpcon_id = attr.id;
2974 	channel->ch_id = attr.qbman_ch_id;
2975 	channel->priv = priv;
2976 
2977 	return channel;
2978 
2979 err_get_attr:
2980 	dpaa2_eth_free_dpcon(priv, channel->dpcon);
2981 err_setup:
2982 	kfree(channel);
2983 	return ERR_PTR(err);
2984 }
2985 
2986 static void dpaa2_eth_free_channel(struct dpaa2_eth_priv *priv,
2987 				   struct dpaa2_eth_channel *channel)
2988 {
2989 	dpaa2_eth_free_dpcon(priv, channel->dpcon);
2990 	kfree(channel);
2991 }
2992 
2993 /* DPIO setup: allocate and configure QBMan channels, setup core affinity
2994  * and register data availability notifications
2995  */
2996 static int dpaa2_eth_setup_dpio(struct dpaa2_eth_priv *priv)
2997 {
2998 	struct dpaa2_io_notification_ctx *nctx;
2999 	struct dpaa2_eth_channel *channel;
3000 	struct dpcon_notification_cfg dpcon_notif_cfg;
3001 	struct device *dev = priv->net_dev->dev.parent;
3002 	int i, err;
3003 
3004 	/* We want the ability to spread ingress traffic (RX, TX conf) to as
3005 	 * many cores as possible, so we need one channel for each core
3006 	 * (unless there's fewer queues than cores, in which case the extra
3007 	 * channels would be wasted).
3008 	 * Allocate one channel per core and register it to the core's
3009 	 * affine DPIO. If not enough channels are available for all cores
3010 	 * or if some cores don't have an affine DPIO, there will be no
3011 	 * ingress frame processing on those cores.
3012 	 */
3013 	cpumask_clear(&priv->dpio_cpumask);
3014 	for_each_online_cpu(i) {
3015 		/* Try to allocate a channel */
3016 		channel = dpaa2_eth_alloc_channel(priv);
3017 		if (IS_ERR_OR_NULL(channel)) {
3018 			err = PTR_ERR_OR_ZERO(channel);
3019 			if (err != -EPROBE_DEFER)
3020 				dev_info(dev,
3021 					 "No affine channel for cpu %d and above\n", i);
3022 			goto err_alloc_ch;
3023 		}
3024 
3025 		priv->channel[priv->num_channels] = channel;
3026 
3027 		nctx = &channel->nctx;
3028 		nctx->is_cdan = 1;
3029 		nctx->cb = dpaa2_eth_cdan_cb;
3030 		nctx->id = channel->ch_id;
3031 		nctx->desired_cpu = i;
3032 
3033 		/* Register the new context */
3034 		channel->dpio = dpaa2_io_service_select(i);
3035 		err = dpaa2_io_service_register(channel->dpio, nctx, dev);
3036 		if (err) {
3037 			dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
3038 			/* If no affine DPIO for this core, there's probably
3039 			 * none available for next cores either. Signal we want
3040 			 * to retry later, in case the DPIO devices weren't
3041 			 * probed yet.
3042 			 */
3043 			err = -EPROBE_DEFER;
3044 			goto err_service_reg;
3045 		}
3046 
3047 		/* Register DPCON notification with MC */
3048 		dpcon_notif_cfg.dpio_id = nctx->dpio_id;
3049 		dpcon_notif_cfg.priority = 0;
3050 		dpcon_notif_cfg.user_ctx = nctx->qman64;
3051 		err = dpcon_set_notification(priv->mc_io, 0,
3052 					     channel->dpcon->mc_handle,
3053 					     &dpcon_notif_cfg);
3054 		if (err) {
3055 			dev_err(dev, "dpcon_set_notification failed()\n");
3056 			goto err_set_cdan;
3057 		}
3058 
3059 		/* If we managed to allocate a channel and also found an affine
3060 		 * DPIO for this core, add it to the final mask
3061 		 */
3062 		cpumask_set_cpu(i, &priv->dpio_cpumask);
3063 		priv->num_channels++;
3064 
3065 		/* Stop if we already have enough channels to accommodate all
3066 		 * RX and TX conf queues
3067 		 */
3068 		if (priv->num_channels == priv->dpni_attrs.num_queues)
3069 			break;
3070 	}
3071 
3072 	return 0;
3073 
3074 err_set_cdan:
3075 	dpaa2_io_service_deregister(channel->dpio, nctx, dev);
3076 err_service_reg:
3077 	dpaa2_eth_free_channel(priv, channel);
3078 err_alloc_ch:
3079 	if (err == -EPROBE_DEFER) {
3080 		for (i = 0; i < priv->num_channels; i++) {
3081 			channel = priv->channel[i];
3082 			nctx = &channel->nctx;
3083 			dpaa2_io_service_deregister(channel->dpio, nctx, dev);
3084 			dpaa2_eth_free_channel(priv, channel);
3085 		}
3086 		priv->num_channels = 0;
3087 		return err;
3088 	}
3089 
3090 	if (cpumask_empty(&priv->dpio_cpumask)) {
3091 		dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
3092 		return -ENODEV;
3093 	}
3094 
3095 	dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
3096 		 cpumask_pr_args(&priv->dpio_cpumask));
3097 
3098 	return 0;
3099 }
3100 
3101 static void dpaa2_eth_free_dpio(struct dpaa2_eth_priv *priv)
3102 {
3103 	struct device *dev = priv->net_dev->dev.parent;
3104 	struct dpaa2_eth_channel *ch;
3105 	int i;
3106 
3107 	/* deregister CDAN notifications and free channels */
3108 	for (i = 0; i < priv->num_channels; i++) {
3109 		ch = priv->channel[i];
3110 		dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev);
3111 		dpaa2_eth_free_channel(priv, ch);
3112 	}
3113 }
3114 
3115 static struct dpaa2_eth_channel *dpaa2_eth_get_affine_channel(struct dpaa2_eth_priv *priv,
3116 							      int cpu)
3117 {
3118 	struct device *dev = priv->net_dev->dev.parent;
3119 	int i;
3120 
3121 	for (i = 0; i < priv->num_channels; i++)
3122 		if (priv->channel[i]->nctx.desired_cpu == cpu)
3123 			return priv->channel[i];
3124 
3125 	/* We should never get here. Issue a warning and return
3126 	 * the first channel, because it's still better than nothing
3127 	 */
3128 	dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
3129 
3130 	return priv->channel[0];
3131 }
3132 
3133 static void dpaa2_eth_set_fq_affinity(struct dpaa2_eth_priv *priv)
3134 {
3135 	struct device *dev = priv->net_dev->dev.parent;
3136 	struct dpaa2_eth_fq *fq;
3137 	int rx_cpu, txc_cpu;
3138 	int i;
3139 
3140 	/* For each FQ, pick one channel/CPU to deliver frames to.
3141 	 * This may well change at runtime, either through irqbalance or
3142 	 * through direct user intervention.
3143 	 */
3144 	rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
3145 
3146 	for (i = 0; i < priv->num_fqs; i++) {
3147 		fq = &priv->fq[i];
3148 		switch (fq->type) {
3149 		case DPAA2_RX_FQ:
3150 		case DPAA2_RX_ERR_FQ:
3151 			fq->target_cpu = rx_cpu;
3152 			rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
3153 			if (rx_cpu >= nr_cpu_ids)
3154 				rx_cpu = cpumask_first(&priv->dpio_cpumask);
3155 			break;
3156 		case DPAA2_TX_CONF_FQ:
3157 			fq->target_cpu = txc_cpu;
3158 			txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
3159 			if (txc_cpu >= nr_cpu_ids)
3160 				txc_cpu = cpumask_first(&priv->dpio_cpumask);
3161 			break;
3162 		default:
3163 			dev_err(dev, "Unknown FQ type: %d\n", fq->type);
3164 		}
3165 		fq->channel = dpaa2_eth_get_affine_channel(priv, fq->target_cpu);
3166 	}
3167 
3168 	update_xps(priv);
3169 }
3170 
3171 static void dpaa2_eth_setup_fqs(struct dpaa2_eth_priv *priv)
3172 {
3173 	int i, j;
3174 
3175 	/* We have one TxConf FQ per Tx flow.
3176 	 * The number of Tx and Rx queues is the same.
3177 	 * Tx queues come first in the fq array.
3178 	 */
3179 	for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
3180 		priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
3181 		priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
3182 		priv->fq[priv->num_fqs++].flowid = (u16)i;
3183 	}
3184 
3185 	for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
3186 		for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
3187 			priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
3188 			priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
3189 			priv->fq[priv->num_fqs].tc = (u8)j;
3190 			priv->fq[priv->num_fqs++].flowid = (u16)i;
3191 		}
3192 	}
3193 
3194 	/* We have exactly one Rx error queue per DPNI */
3195 	priv->fq[priv->num_fqs].type = DPAA2_RX_ERR_FQ;
3196 	priv->fq[priv->num_fqs++].consume = dpaa2_eth_rx_err;
3197 
3198 	/* For each FQ, decide on which core to process incoming frames */
3199 	dpaa2_eth_set_fq_affinity(priv);
3200 }
3201 
3202 /* Allocate and configure one buffer pool for each interface */
3203 static int dpaa2_eth_setup_dpbp(struct dpaa2_eth_priv *priv)
3204 {
3205 	int err;
3206 	struct fsl_mc_device *dpbp_dev;
3207 	struct device *dev = priv->net_dev->dev.parent;
3208 	struct dpbp_attr dpbp_attrs;
3209 
3210 	err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
3211 				     &dpbp_dev);
3212 	if (err) {
3213 		if (err == -ENXIO)
3214 			err = -EPROBE_DEFER;
3215 		else
3216 			dev_err(dev, "DPBP device allocation failed\n");
3217 		return err;
3218 	}
3219 
3220 	priv->dpbp_dev = dpbp_dev;
3221 
3222 	err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id,
3223 			&dpbp_dev->mc_handle);
3224 	if (err) {
3225 		dev_err(dev, "dpbp_open() failed\n");
3226 		goto err_open;
3227 	}
3228 
3229 	err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
3230 	if (err) {
3231 		dev_err(dev, "dpbp_reset() failed\n");
3232 		goto err_reset;
3233 	}
3234 
3235 	err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
3236 	if (err) {
3237 		dev_err(dev, "dpbp_enable() failed\n");
3238 		goto err_enable;
3239 	}
3240 
3241 	err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
3242 				  &dpbp_attrs);
3243 	if (err) {
3244 		dev_err(dev, "dpbp_get_attributes() failed\n");
3245 		goto err_get_attr;
3246 	}
3247 	priv->bpid = dpbp_attrs.bpid;
3248 
3249 	return 0;
3250 
3251 err_get_attr:
3252 	dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
3253 err_enable:
3254 err_reset:
3255 	dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
3256 err_open:
3257 	fsl_mc_object_free(dpbp_dev);
3258 
3259 	return err;
3260 }
3261 
3262 static void dpaa2_eth_free_dpbp(struct dpaa2_eth_priv *priv)
3263 {
3264 	dpaa2_eth_drain_pool(priv);
3265 	dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
3266 	dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
3267 	fsl_mc_object_free(priv->dpbp_dev);
3268 }
3269 
3270 static int dpaa2_eth_set_buffer_layout(struct dpaa2_eth_priv *priv)
3271 {
3272 	struct device *dev = priv->net_dev->dev.parent;
3273 	struct dpni_buffer_layout buf_layout = {0};
3274 	u16 rx_buf_align;
3275 	int err;
3276 
3277 	/* We need to check for WRIOP version 1.0.0, but depending on the MC
3278 	 * version, this number is not always provided correctly on rev1.
3279 	 * We need to check for both alternatives in this situation.
3280 	 */
3281 	if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
3282 	    priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
3283 		rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
3284 	else
3285 		rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
3286 
3287 	/* We need to ensure that the buffer size seen by WRIOP is a multiple
3288 	 * of 64 or 256 bytes depending on the WRIOP version.
3289 	 */
3290 	priv->rx_buf_size = ALIGN_DOWN(DPAA2_ETH_RX_BUF_SIZE, rx_buf_align);
3291 
3292 	/* tx buffer */
3293 	buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
3294 	buf_layout.pass_timestamp = true;
3295 	buf_layout.pass_frame_status = true;
3296 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
3297 			     DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
3298 			     DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
3299 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3300 				     DPNI_QUEUE_TX, &buf_layout);
3301 	if (err) {
3302 		dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
3303 		return err;
3304 	}
3305 
3306 	/* tx-confirm buffer */
3307 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
3308 			     DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
3309 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3310 				     DPNI_QUEUE_TX_CONFIRM, &buf_layout);
3311 	if (err) {
3312 		dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
3313 		return err;
3314 	}
3315 
3316 	/* Now that we've set our tx buffer layout, retrieve the minimum
3317 	 * required tx data offset.
3318 	 */
3319 	err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
3320 				      &priv->tx_data_offset);
3321 	if (err) {
3322 		dev_err(dev, "dpni_get_tx_data_offset() failed\n");
3323 		return err;
3324 	}
3325 
3326 	if ((priv->tx_data_offset % 64) != 0)
3327 		dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
3328 			 priv->tx_data_offset);
3329 
3330 	/* rx buffer */
3331 	buf_layout.pass_frame_status = true;
3332 	buf_layout.pass_parser_result = true;
3333 	buf_layout.data_align = rx_buf_align;
3334 	buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
3335 	buf_layout.private_data_size = 0;
3336 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
3337 			     DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
3338 			     DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
3339 			     DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
3340 			     DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
3341 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3342 				     DPNI_QUEUE_RX, &buf_layout);
3343 	if (err) {
3344 		dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
3345 		return err;
3346 	}
3347 
3348 	return 0;
3349 }
3350 
3351 #define DPNI_ENQUEUE_FQID_VER_MAJOR	7
3352 #define DPNI_ENQUEUE_FQID_VER_MINOR	9
3353 
3354 static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv,
3355 				       struct dpaa2_eth_fq *fq,
3356 				       struct dpaa2_fd *fd, u8 prio,
3357 				       u32 num_frames __always_unused,
3358 				       int *frames_enqueued)
3359 {
3360 	int err;
3361 
3362 	err = dpaa2_io_service_enqueue_qd(fq->channel->dpio,
3363 					  priv->tx_qdid, prio,
3364 					  fq->tx_qdbin, fd);
3365 	if (!err && frames_enqueued)
3366 		*frames_enqueued = 1;
3367 	return err;
3368 }
3369 
3370 static inline int dpaa2_eth_enqueue_fq_multiple(struct dpaa2_eth_priv *priv,
3371 						struct dpaa2_eth_fq *fq,
3372 						struct dpaa2_fd *fd,
3373 						u8 prio, u32 num_frames,
3374 						int *frames_enqueued)
3375 {
3376 	int err;
3377 
3378 	err = dpaa2_io_service_enqueue_multiple_fq(fq->channel->dpio,
3379 						   fq->tx_fqid[prio],
3380 						   fd, num_frames);
3381 
3382 	if (err == 0)
3383 		return -EBUSY;
3384 
3385 	if (frames_enqueued)
3386 		*frames_enqueued = err;
3387 	return 0;
3388 }
3389 
3390 static void dpaa2_eth_set_enqueue_mode(struct dpaa2_eth_priv *priv)
3391 {
3392 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3393 				   DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3394 		priv->enqueue = dpaa2_eth_enqueue_qd;
3395 	else
3396 		priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
3397 }
3398 
3399 static int dpaa2_eth_set_pause(struct dpaa2_eth_priv *priv)
3400 {
3401 	struct device *dev = priv->net_dev->dev.parent;
3402 	struct dpni_link_cfg link_cfg = {0};
3403 	int err;
3404 
3405 	/* Get the default link options so we don't override other flags */
3406 	err = dpni_get_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
3407 	if (err) {
3408 		dev_err(dev, "dpni_get_link_cfg() failed\n");
3409 		return err;
3410 	}
3411 
3412 	/* By default, enable both Rx and Tx pause frames */
3413 	link_cfg.options |= DPNI_LINK_OPT_PAUSE;
3414 	link_cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
3415 	err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
3416 	if (err) {
3417 		dev_err(dev, "dpni_set_link_cfg() failed\n");
3418 		return err;
3419 	}
3420 
3421 	priv->link_state.options = link_cfg.options;
3422 
3423 	return 0;
3424 }
3425 
3426 static void dpaa2_eth_update_tx_fqids(struct dpaa2_eth_priv *priv)
3427 {
3428 	struct dpni_queue_id qid = {0};
3429 	struct dpaa2_eth_fq *fq;
3430 	struct dpni_queue queue;
3431 	int i, j, err;
3432 
3433 	/* We only use Tx FQIDs for FQID-based enqueue, so check
3434 	 * if DPNI version supports it before updating FQIDs
3435 	 */
3436 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3437 				   DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3438 		return;
3439 
3440 	for (i = 0; i < priv->num_fqs; i++) {
3441 		fq = &priv->fq[i];
3442 		if (fq->type != DPAA2_TX_CONF_FQ)
3443 			continue;
3444 		for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
3445 			err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3446 					     DPNI_QUEUE_TX, j, fq->flowid,
3447 					     &queue, &qid);
3448 			if (err)
3449 				goto out_err;
3450 
3451 			fq->tx_fqid[j] = qid.fqid;
3452 			if (fq->tx_fqid[j] == 0)
3453 				goto out_err;
3454 		}
3455 	}
3456 
3457 	priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
3458 
3459 	return;
3460 
3461 out_err:
3462 	netdev_info(priv->net_dev,
3463 		    "Error reading Tx FQID, fallback to QDID-based enqueue\n");
3464 	priv->enqueue = dpaa2_eth_enqueue_qd;
3465 }
3466 
3467 /* Configure ingress classification based on VLAN PCP */
3468 static int dpaa2_eth_set_vlan_qos(struct dpaa2_eth_priv *priv)
3469 {
3470 	struct device *dev = priv->net_dev->dev.parent;
3471 	struct dpkg_profile_cfg kg_cfg = {0};
3472 	struct dpni_qos_tbl_cfg qos_cfg = {0};
3473 	struct dpni_rule_cfg key_params;
3474 	void *dma_mem, *key, *mask;
3475 	u8 key_size = 2;	/* VLAN TCI field */
3476 	int i, pcp, err;
3477 
3478 	/* VLAN-based classification only makes sense if we have multiple
3479 	 * traffic classes.
3480 	 * Also, we need to extract just the 3-bit PCP field from the VLAN
3481 	 * header and we can only do that by using a mask
3482 	 */
3483 	if (dpaa2_eth_tc_count(priv) == 1 || !dpaa2_eth_fs_mask_enabled(priv)) {
3484 		dev_dbg(dev, "VLAN-based QoS classification not supported\n");
3485 		return -EOPNOTSUPP;
3486 	}
3487 
3488 	dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
3489 	if (!dma_mem)
3490 		return -ENOMEM;
3491 
3492 	kg_cfg.num_extracts = 1;
3493 	kg_cfg.extracts[0].type = DPKG_EXTRACT_FROM_HDR;
3494 	kg_cfg.extracts[0].extract.from_hdr.prot = NET_PROT_VLAN;
3495 	kg_cfg.extracts[0].extract.from_hdr.type = DPKG_FULL_FIELD;
3496 	kg_cfg.extracts[0].extract.from_hdr.field = NH_FLD_VLAN_TCI;
3497 
3498 	err = dpni_prepare_key_cfg(&kg_cfg, dma_mem);
3499 	if (err) {
3500 		dev_err(dev, "dpni_prepare_key_cfg failed\n");
3501 		goto out_free_tbl;
3502 	}
3503 
3504 	/* set QoS table */
3505 	qos_cfg.default_tc = 0;
3506 	qos_cfg.discard_on_miss = 0;
3507 	qos_cfg.key_cfg_iova = dma_map_single(dev, dma_mem,
3508 					      DPAA2_CLASSIFIER_DMA_SIZE,
3509 					      DMA_TO_DEVICE);
3510 	if (dma_mapping_error(dev, qos_cfg.key_cfg_iova)) {
3511 		dev_err(dev, "QoS table DMA mapping failed\n");
3512 		err = -ENOMEM;
3513 		goto out_free_tbl;
3514 	}
3515 
3516 	err = dpni_set_qos_table(priv->mc_io, 0, priv->mc_token, &qos_cfg);
3517 	if (err) {
3518 		dev_err(dev, "dpni_set_qos_table failed\n");
3519 		goto out_unmap_tbl;
3520 	}
3521 
3522 	/* Add QoS table entries */
3523 	key = kzalloc(key_size * 2, GFP_KERNEL);
3524 	if (!key) {
3525 		err = -ENOMEM;
3526 		goto out_unmap_tbl;
3527 	}
3528 	mask = key + key_size;
3529 	*(__be16 *)mask = cpu_to_be16(VLAN_PRIO_MASK);
3530 
3531 	key_params.key_iova = dma_map_single(dev, key, key_size * 2,
3532 					     DMA_TO_DEVICE);
3533 	if (dma_mapping_error(dev, key_params.key_iova)) {
3534 		dev_err(dev, "Qos table entry DMA mapping failed\n");
3535 		err = -ENOMEM;
3536 		goto out_free_key;
3537 	}
3538 
3539 	key_params.mask_iova = key_params.key_iova + key_size;
3540 	key_params.key_size = key_size;
3541 
3542 	/* We add rules for PCP-based distribution starting with highest
3543 	 * priority (VLAN PCP = 7). If this DPNI doesn't have enough traffic
3544 	 * classes to accommodate all priority levels, the lowest ones end up
3545 	 * on TC 0 which was configured as default
3546 	 */
3547 	for (i = dpaa2_eth_tc_count(priv) - 1, pcp = 7; i >= 0; i--, pcp--) {
3548 		*(__be16 *)key = cpu_to_be16(pcp << VLAN_PRIO_SHIFT);
3549 		dma_sync_single_for_device(dev, key_params.key_iova,
3550 					   key_size * 2, DMA_TO_DEVICE);
3551 
3552 		err = dpni_add_qos_entry(priv->mc_io, 0, priv->mc_token,
3553 					 &key_params, i, i);
3554 		if (err) {
3555 			dev_err(dev, "dpni_add_qos_entry failed\n");
3556 			dpni_clear_qos_table(priv->mc_io, 0, priv->mc_token);
3557 			goto out_unmap_key;
3558 		}
3559 	}
3560 
3561 	priv->vlan_cls_enabled = true;
3562 
3563 	/* Table and key memory is not persistent, clean everything up after
3564 	 * configuration is finished
3565 	 */
3566 out_unmap_key:
3567 	dma_unmap_single(dev, key_params.key_iova, key_size * 2, DMA_TO_DEVICE);
3568 out_free_key:
3569 	kfree(key);
3570 out_unmap_tbl:
3571 	dma_unmap_single(dev, qos_cfg.key_cfg_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3572 			 DMA_TO_DEVICE);
3573 out_free_tbl:
3574 	kfree(dma_mem);
3575 
3576 	return err;
3577 }
3578 
3579 /* Configure the DPNI object this interface is associated with */
3580 static int dpaa2_eth_setup_dpni(struct fsl_mc_device *ls_dev)
3581 {
3582 	struct device *dev = &ls_dev->dev;
3583 	struct dpaa2_eth_priv *priv;
3584 	struct net_device *net_dev;
3585 	int err;
3586 
3587 	net_dev = dev_get_drvdata(dev);
3588 	priv = netdev_priv(net_dev);
3589 
3590 	/* get a handle for the DPNI object */
3591 	err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
3592 	if (err) {
3593 		dev_err(dev, "dpni_open() failed\n");
3594 		return err;
3595 	}
3596 
3597 	/* Check if we can work with this DPNI object */
3598 	err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
3599 				   &priv->dpni_ver_minor);
3600 	if (err) {
3601 		dev_err(dev, "dpni_get_api_version() failed\n");
3602 		goto close;
3603 	}
3604 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
3605 		dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
3606 			priv->dpni_ver_major, priv->dpni_ver_minor,
3607 			DPNI_VER_MAJOR, DPNI_VER_MINOR);
3608 		err = -ENOTSUPP;
3609 		goto close;
3610 	}
3611 
3612 	ls_dev->mc_io = priv->mc_io;
3613 	ls_dev->mc_handle = priv->mc_token;
3614 
3615 	err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3616 	if (err) {
3617 		dev_err(dev, "dpni_reset() failed\n");
3618 		goto close;
3619 	}
3620 
3621 	err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
3622 				  &priv->dpni_attrs);
3623 	if (err) {
3624 		dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
3625 		goto close;
3626 	}
3627 
3628 	err = dpaa2_eth_set_buffer_layout(priv);
3629 	if (err)
3630 		goto close;
3631 
3632 	dpaa2_eth_set_enqueue_mode(priv);
3633 
3634 	/* Enable pause frame support */
3635 	if (dpaa2_eth_has_pause_support(priv)) {
3636 		err = dpaa2_eth_set_pause(priv);
3637 		if (err)
3638 			goto close;
3639 	}
3640 
3641 	err = dpaa2_eth_set_vlan_qos(priv);
3642 	if (err && err != -EOPNOTSUPP)
3643 		goto close;
3644 
3645 	priv->cls_rules = devm_kcalloc(dev, dpaa2_eth_fs_count(priv),
3646 				       sizeof(struct dpaa2_eth_cls_rule),
3647 				       GFP_KERNEL);
3648 	if (!priv->cls_rules) {
3649 		err = -ENOMEM;
3650 		goto close;
3651 	}
3652 
3653 	return 0;
3654 
3655 close:
3656 	dpni_close(priv->mc_io, 0, priv->mc_token);
3657 
3658 	return err;
3659 }
3660 
3661 static void dpaa2_eth_free_dpni(struct dpaa2_eth_priv *priv)
3662 {
3663 	int err;
3664 
3665 	err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3666 	if (err)
3667 		netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
3668 			    err);
3669 
3670 	dpni_close(priv->mc_io, 0, priv->mc_token);
3671 }
3672 
3673 static int dpaa2_eth_setup_rx_flow(struct dpaa2_eth_priv *priv,
3674 				   struct dpaa2_eth_fq *fq)
3675 {
3676 	struct device *dev = priv->net_dev->dev.parent;
3677 	struct dpni_queue queue;
3678 	struct dpni_queue_id qid;
3679 	int err;
3680 
3681 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3682 			     DPNI_QUEUE_RX, fq->tc, fq->flowid, &queue, &qid);
3683 	if (err) {
3684 		dev_err(dev, "dpni_get_queue(RX) failed\n");
3685 		return err;
3686 	}
3687 
3688 	fq->fqid = qid.fqid;
3689 
3690 	queue.destination.id = fq->channel->dpcon_id;
3691 	queue.destination.type = DPNI_DEST_DPCON;
3692 	queue.destination.priority = 1;
3693 	queue.user_context = (u64)(uintptr_t)fq;
3694 	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3695 			     DPNI_QUEUE_RX, fq->tc, fq->flowid,
3696 			     DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3697 			     &queue);
3698 	if (err) {
3699 		dev_err(dev, "dpni_set_queue(RX) failed\n");
3700 		return err;
3701 	}
3702 
3703 	/* xdp_rxq setup */
3704 	/* only once for each channel */
3705 	if (fq->tc > 0)
3706 		return 0;
3707 
3708 	err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev,
3709 			       fq->flowid, 0);
3710 	if (err) {
3711 		dev_err(dev, "xdp_rxq_info_reg failed\n");
3712 		return err;
3713 	}
3714 
3715 	err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq,
3716 					 MEM_TYPE_PAGE_ORDER0, NULL);
3717 	if (err) {
3718 		dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n");
3719 		return err;
3720 	}
3721 
3722 	return 0;
3723 }
3724 
3725 static int dpaa2_eth_setup_tx_flow(struct dpaa2_eth_priv *priv,
3726 				   struct dpaa2_eth_fq *fq)
3727 {
3728 	struct device *dev = priv->net_dev->dev.parent;
3729 	struct dpni_queue queue;
3730 	struct dpni_queue_id qid;
3731 	int i, err;
3732 
3733 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3734 		err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3735 				     DPNI_QUEUE_TX, i, fq->flowid,
3736 				     &queue, &qid);
3737 		if (err) {
3738 			dev_err(dev, "dpni_get_queue(TX) failed\n");
3739 			return err;
3740 		}
3741 		fq->tx_fqid[i] = qid.fqid;
3742 	}
3743 
3744 	/* All Tx queues belonging to the same flowid have the same qdbin */
3745 	fq->tx_qdbin = qid.qdbin;
3746 
3747 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3748 			     DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3749 			     &queue, &qid);
3750 	if (err) {
3751 		dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
3752 		return err;
3753 	}
3754 
3755 	fq->fqid = qid.fqid;
3756 
3757 	queue.destination.id = fq->channel->dpcon_id;
3758 	queue.destination.type = DPNI_DEST_DPCON;
3759 	queue.destination.priority = 0;
3760 	queue.user_context = (u64)(uintptr_t)fq;
3761 	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3762 			     DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3763 			     DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3764 			     &queue);
3765 	if (err) {
3766 		dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
3767 		return err;
3768 	}
3769 
3770 	return 0;
3771 }
3772 
3773 static int setup_rx_err_flow(struct dpaa2_eth_priv *priv,
3774 			     struct dpaa2_eth_fq *fq)
3775 {
3776 	struct device *dev = priv->net_dev->dev.parent;
3777 	struct dpni_queue q = { { 0 } };
3778 	struct dpni_queue_id qid;
3779 	u8 q_opt = DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST;
3780 	int err;
3781 
3782 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3783 			     DPNI_QUEUE_RX_ERR, 0, 0, &q, &qid);
3784 	if (err) {
3785 		dev_err(dev, "dpni_get_queue() failed (%d)\n", err);
3786 		return err;
3787 	}
3788 
3789 	fq->fqid = qid.fqid;
3790 
3791 	q.destination.id = fq->channel->dpcon_id;
3792 	q.destination.type = DPNI_DEST_DPCON;
3793 	q.destination.priority = 1;
3794 	q.user_context = (u64)(uintptr_t)fq;
3795 	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3796 			     DPNI_QUEUE_RX_ERR, 0, 0, q_opt, &q);
3797 	if (err) {
3798 		dev_err(dev, "dpni_set_queue() failed (%d)\n", err);
3799 		return err;
3800 	}
3801 
3802 	return 0;
3803 }
3804 
3805 /* Supported header fields for Rx hash distribution key */
3806 static const struct dpaa2_eth_dist_fields dist_fields[] = {
3807 	{
3808 		/* L2 header */
3809 		.rxnfc_field = RXH_L2DA,
3810 		.cls_prot = NET_PROT_ETH,
3811 		.cls_field = NH_FLD_ETH_DA,
3812 		.id = DPAA2_ETH_DIST_ETHDST,
3813 		.size = 6,
3814 	}, {
3815 		.cls_prot = NET_PROT_ETH,
3816 		.cls_field = NH_FLD_ETH_SA,
3817 		.id = DPAA2_ETH_DIST_ETHSRC,
3818 		.size = 6,
3819 	}, {
3820 		/* This is the last ethertype field parsed:
3821 		 * depending on frame format, it can be the MAC ethertype
3822 		 * or the VLAN etype.
3823 		 */
3824 		.cls_prot = NET_PROT_ETH,
3825 		.cls_field = NH_FLD_ETH_TYPE,
3826 		.id = DPAA2_ETH_DIST_ETHTYPE,
3827 		.size = 2,
3828 	}, {
3829 		/* VLAN header */
3830 		.rxnfc_field = RXH_VLAN,
3831 		.cls_prot = NET_PROT_VLAN,
3832 		.cls_field = NH_FLD_VLAN_TCI,
3833 		.id = DPAA2_ETH_DIST_VLAN,
3834 		.size = 2,
3835 	}, {
3836 		/* IP header */
3837 		.rxnfc_field = RXH_IP_SRC,
3838 		.cls_prot = NET_PROT_IP,
3839 		.cls_field = NH_FLD_IP_SRC,
3840 		.id = DPAA2_ETH_DIST_IPSRC,
3841 		.size = 4,
3842 	}, {
3843 		.rxnfc_field = RXH_IP_DST,
3844 		.cls_prot = NET_PROT_IP,
3845 		.cls_field = NH_FLD_IP_DST,
3846 		.id = DPAA2_ETH_DIST_IPDST,
3847 		.size = 4,
3848 	}, {
3849 		.rxnfc_field = RXH_L3_PROTO,
3850 		.cls_prot = NET_PROT_IP,
3851 		.cls_field = NH_FLD_IP_PROTO,
3852 		.id = DPAA2_ETH_DIST_IPPROTO,
3853 		.size = 1,
3854 	}, {
3855 		/* Using UDP ports, this is functionally equivalent to raw
3856 		 * byte pairs from L4 header.
3857 		 */
3858 		.rxnfc_field = RXH_L4_B_0_1,
3859 		.cls_prot = NET_PROT_UDP,
3860 		.cls_field = NH_FLD_UDP_PORT_SRC,
3861 		.id = DPAA2_ETH_DIST_L4SRC,
3862 		.size = 2,
3863 	}, {
3864 		.rxnfc_field = RXH_L4_B_2_3,
3865 		.cls_prot = NET_PROT_UDP,
3866 		.cls_field = NH_FLD_UDP_PORT_DST,
3867 		.id = DPAA2_ETH_DIST_L4DST,
3868 		.size = 2,
3869 	},
3870 };
3871 
3872 /* Configure the Rx hash key using the legacy API */
3873 static int dpaa2_eth_config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
3874 {
3875 	struct device *dev = priv->net_dev->dev.parent;
3876 	struct dpni_rx_tc_dist_cfg dist_cfg;
3877 	int i, err = 0;
3878 
3879 	memset(&dist_cfg, 0, sizeof(dist_cfg));
3880 
3881 	dist_cfg.key_cfg_iova = key;
3882 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3883 	dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
3884 
3885 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3886 		err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token,
3887 					  i, &dist_cfg);
3888 		if (err) {
3889 			dev_err(dev, "dpni_set_rx_tc_dist failed\n");
3890 			break;
3891 		}
3892 	}
3893 
3894 	return err;
3895 }
3896 
3897 /* Configure the Rx hash key using the new API */
3898 static int dpaa2_eth_config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
3899 {
3900 	struct device *dev = priv->net_dev->dev.parent;
3901 	struct dpni_rx_dist_cfg dist_cfg;
3902 	int i, err = 0;
3903 
3904 	memset(&dist_cfg, 0, sizeof(dist_cfg));
3905 
3906 	dist_cfg.key_cfg_iova = key;
3907 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3908 	dist_cfg.enable = 1;
3909 
3910 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3911 		dist_cfg.tc = i;
3912 		err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token,
3913 					    &dist_cfg);
3914 		if (err) {
3915 			dev_err(dev, "dpni_set_rx_hash_dist failed\n");
3916 			break;
3917 		}
3918 
3919 		/* If the flow steering / hashing key is shared between all
3920 		 * traffic classes, install it just once
3921 		 */
3922 		if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
3923 			break;
3924 	}
3925 
3926 	return err;
3927 }
3928 
3929 /* Configure the Rx flow classification key */
3930 static int dpaa2_eth_config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
3931 {
3932 	struct device *dev = priv->net_dev->dev.parent;
3933 	struct dpni_rx_dist_cfg dist_cfg;
3934 	int i, err = 0;
3935 
3936 	memset(&dist_cfg, 0, sizeof(dist_cfg));
3937 
3938 	dist_cfg.key_cfg_iova = key;
3939 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3940 	dist_cfg.enable = 1;
3941 
3942 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3943 		dist_cfg.tc = i;
3944 		err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token,
3945 					  &dist_cfg);
3946 		if (err) {
3947 			dev_err(dev, "dpni_set_rx_fs_dist failed\n");
3948 			break;
3949 		}
3950 
3951 		/* If the flow steering / hashing key is shared between all
3952 		 * traffic classes, install it just once
3953 		 */
3954 		if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
3955 			break;
3956 	}
3957 
3958 	return err;
3959 }
3960 
3961 /* Size of the Rx flow classification key */
3962 int dpaa2_eth_cls_key_size(u64 fields)
3963 {
3964 	int i, size = 0;
3965 
3966 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3967 		if (!(fields & dist_fields[i].id))
3968 			continue;
3969 		size += dist_fields[i].size;
3970 	}
3971 
3972 	return size;
3973 }
3974 
3975 /* Offset of header field in Rx classification key */
3976 int dpaa2_eth_cls_fld_off(int prot, int field)
3977 {
3978 	int i, off = 0;
3979 
3980 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3981 		if (dist_fields[i].cls_prot == prot &&
3982 		    dist_fields[i].cls_field == field)
3983 			return off;
3984 		off += dist_fields[i].size;
3985 	}
3986 
3987 	WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
3988 	return 0;
3989 }
3990 
3991 /* Prune unused fields from the classification rule.
3992  * Used when masking is not supported
3993  */
3994 void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields)
3995 {
3996 	int off = 0, new_off = 0;
3997 	int i, size;
3998 
3999 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
4000 		size = dist_fields[i].size;
4001 		if (dist_fields[i].id & fields) {
4002 			memcpy(key_mem + new_off, key_mem + off, size);
4003 			new_off += size;
4004 		}
4005 		off += size;
4006 	}
4007 }
4008 
4009 /* Set Rx distribution (hash or flow classification) key
4010  * flags is a combination of RXH_ bits
4011  */
4012 static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
4013 				  enum dpaa2_eth_rx_dist type, u64 flags)
4014 {
4015 	struct device *dev = net_dev->dev.parent;
4016 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4017 	struct dpkg_profile_cfg cls_cfg;
4018 	u32 rx_hash_fields = 0;
4019 	dma_addr_t key_iova;
4020 	u8 *dma_mem;
4021 	int i;
4022 	int err = 0;
4023 
4024 	memset(&cls_cfg, 0, sizeof(cls_cfg));
4025 
4026 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
4027 		struct dpkg_extract *key =
4028 			&cls_cfg.extracts[cls_cfg.num_extracts];
4029 
4030 		/* For both Rx hashing and classification keys
4031 		 * we set only the selected fields.
4032 		 */
4033 		if (!(flags & dist_fields[i].id))
4034 			continue;
4035 		if (type == DPAA2_ETH_RX_DIST_HASH)
4036 			rx_hash_fields |= dist_fields[i].rxnfc_field;
4037 
4038 		if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
4039 			dev_err(dev, "error adding key extraction rule, too many rules?\n");
4040 			return -E2BIG;
4041 		}
4042 
4043 		key->type = DPKG_EXTRACT_FROM_HDR;
4044 		key->extract.from_hdr.prot = dist_fields[i].cls_prot;
4045 		key->extract.from_hdr.type = DPKG_FULL_FIELD;
4046 		key->extract.from_hdr.field = dist_fields[i].cls_field;
4047 		cls_cfg.num_extracts++;
4048 	}
4049 
4050 	dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
4051 	if (!dma_mem)
4052 		return -ENOMEM;
4053 
4054 	err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
4055 	if (err) {
4056 		dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
4057 		goto free_key;
4058 	}
4059 
4060 	/* Prepare for setting the rx dist */
4061 	key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
4062 				  DMA_TO_DEVICE);
4063 	if (dma_mapping_error(dev, key_iova)) {
4064 		dev_err(dev, "DMA mapping failed\n");
4065 		err = -ENOMEM;
4066 		goto free_key;
4067 	}
4068 
4069 	if (type == DPAA2_ETH_RX_DIST_HASH) {
4070 		if (dpaa2_eth_has_legacy_dist(priv))
4071 			err = dpaa2_eth_config_legacy_hash_key(priv, key_iova);
4072 		else
4073 			err = dpaa2_eth_config_hash_key(priv, key_iova);
4074 	} else {
4075 		err = dpaa2_eth_config_cls_key(priv, key_iova);
4076 	}
4077 
4078 	dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
4079 			 DMA_TO_DEVICE);
4080 	if (!err && type == DPAA2_ETH_RX_DIST_HASH)
4081 		priv->rx_hash_fields = rx_hash_fields;
4082 
4083 free_key:
4084 	kfree(dma_mem);
4085 	return err;
4086 }
4087 
4088 int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
4089 {
4090 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4091 	u64 key = 0;
4092 	int i;
4093 
4094 	if (!dpaa2_eth_hash_enabled(priv))
4095 		return -EOPNOTSUPP;
4096 
4097 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
4098 		if (dist_fields[i].rxnfc_field & flags)
4099 			key |= dist_fields[i].id;
4100 
4101 	return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, key);
4102 }
4103 
4104 int dpaa2_eth_set_cls(struct net_device *net_dev, u64 flags)
4105 {
4106 	return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_CLS, flags);
4107 }
4108 
4109 static int dpaa2_eth_set_default_cls(struct dpaa2_eth_priv *priv)
4110 {
4111 	struct device *dev = priv->net_dev->dev.parent;
4112 	int err;
4113 
4114 	/* Check if we actually support Rx flow classification */
4115 	if (dpaa2_eth_has_legacy_dist(priv)) {
4116 		dev_dbg(dev, "Rx cls not supported by current MC version\n");
4117 		return -EOPNOTSUPP;
4118 	}
4119 
4120 	if (!dpaa2_eth_fs_enabled(priv)) {
4121 		dev_dbg(dev, "Rx cls disabled in DPNI options\n");
4122 		return -EOPNOTSUPP;
4123 	}
4124 
4125 	if (!dpaa2_eth_hash_enabled(priv)) {
4126 		dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
4127 		return -EOPNOTSUPP;
4128 	}
4129 
4130 	/* If there is no support for masking in the classification table,
4131 	 * we don't set a default key, as it will depend on the rules
4132 	 * added by the user at runtime.
4133 	 */
4134 	if (!dpaa2_eth_fs_mask_enabled(priv))
4135 		goto out;
4136 
4137 	err = dpaa2_eth_set_cls(priv->net_dev, DPAA2_ETH_DIST_ALL);
4138 	if (err)
4139 		return err;
4140 
4141 out:
4142 	priv->rx_cls_enabled = 1;
4143 
4144 	return 0;
4145 }
4146 
4147 /* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
4148  * frame queues and channels
4149  */
4150 static int dpaa2_eth_bind_dpni(struct dpaa2_eth_priv *priv)
4151 {
4152 	struct net_device *net_dev = priv->net_dev;
4153 	struct device *dev = net_dev->dev.parent;
4154 	struct dpni_pools_cfg pools_params;
4155 	struct dpni_error_cfg err_cfg;
4156 	int err = 0;
4157 	int i;
4158 
4159 	pools_params.num_dpbp = 1;
4160 	pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id;
4161 	pools_params.pools[0].backup_pool = 0;
4162 	pools_params.pools[0].buffer_size = priv->rx_buf_size;
4163 	err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
4164 	if (err) {
4165 		dev_err(dev, "dpni_set_pools() failed\n");
4166 		return err;
4167 	}
4168 
4169 	/* have the interface implicitly distribute traffic based on
4170 	 * the default hash key
4171 	 */
4172 	err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
4173 	if (err && err != -EOPNOTSUPP)
4174 		dev_err(dev, "Failed to configure hashing\n");
4175 
4176 	/* Configure the flow classification key; it includes all
4177 	 * supported header fields and cannot be modified at runtime
4178 	 */
4179 	err = dpaa2_eth_set_default_cls(priv);
4180 	if (err && err != -EOPNOTSUPP)
4181 		dev_err(dev, "Failed to configure Rx classification key\n");
4182 
4183 	/* Configure handling of error frames */
4184 	err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
4185 	err_cfg.set_frame_annotation = 1;
4186 	err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
4187 	err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
4188 				       &err_cfg);
4189 	if (err) {
4190 		dev_err(dev, "dpni_set_errors_behavior failed\n");
4191 		return err;
4192 	}
4193 
4194 	/* Configure Rx and Tx conf queues to generate CDANs */
4195 	for (i = 0; i < priv->num_fqs; i++) {
4196 		switch (priv->fq[i].type) {
4197 		case DPAA2_RX_FQ:
4198 			err = dpaa2_eth_setup_rx_flow(priv, &priv->fq[i]);
4199 			break;
4200 		case DPAA2_TX_CONF_FQ:
4201 			err = dpaa2_eth_setup_tx_flow(priv, &priv->fq[i]);
4202 			break;
4203 		case DPAA2_RX_ERR_FQ:
4204 			err = setup_rx_err_flow(priv, &priv->fq[i]);
4205 			break;
4206 		default:
4207 			dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
4208 			return -EINVAL;
4209 		}
4210 		if (err)
4211 			return err;
4212 	}
4213 
4214 	err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
4215 			    DPNI_QUEUE_TX, &priv->tx_qdid);
4216 	if (err) {
4217 		dev_err(dev, "dpni_get_qdid() failed\n");
4218 		return err;
4219 	}
4220 
4221 	return 0;
4222 }
4223 
4224 /* Allocate rings for storing incoming frame descriptors */
4225 static int dpaa2_eth_alloc_rings(struct dpaa2_eth_priv *priv)
4226 {
4227 	struct net_device *net_dev = priv->net_dev;
4228 	struct device *dev = net_dev->dev.parent;
4229 	int i;
4230 
4231 	for (i = 0; i < priv->num_channels; i++) {
4232 		priv->channel[i]->store =
4233 			dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
4234 		if (!priv->channel[i]->store) {
4235 			netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
4236 			goto err_ring;
4237 		}
4238 	}
4239 
4240 	return 0;
4241 
4242 err_ring:
4243 	for (i = 0; i < priv->num_channels; i++) {
4244 		if (!priv->channel[i]->store)
4245 			break;
4246 		dpaa2_io_store_destroy(priv->channel[i]->store);
4247 	}
4248 
4249 	return -ENOMEM;
4250 }
4251 
4252 static void dpaa2_eth_free_rings(struct dpaa2_eth_priv *priv)
4253 {
4254 	int i;
4255 
4256 	for (i = 0; i < priv->num_channels; i++)
4257 		dpaa2_io_store_destroy(priv->channel[i]->store);
4258 }
4259 
4260 static int dpaa2_eth_set_mac_addr(struct dpaa2_eth_priv *priv)
4261 {
4262 	struct net_device *net_dev = priv->net_dev;
4263 	struct device *dev = net_dev->dev.parent;
4264 	u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
4265 	int err;
4266 
4267 	/* Get firmware address, if any */
4268 	err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
4269 	if (err) {
4270 		dev_err(dev, "dpni_get_port_mac_addr() failed\n");
4271 		return err;
4272 	}
4273 
4274 	/* Get DPNI attributes address, if any */
4275 	err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
4276 					dpni_mac_addr);
4277 	if (err) {
4278 		dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
4279 		return err;
4280 	}
4281 
4282 	/* First check if firmware has any address configured by bootloader */
4283 	if (!is_zero_ether_addr(mac_addr)) {
4284 		/* If the DPMAC addr != DPNI addr, update it */
4285 		if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
4286 			err = dpni_set_primary_mac_addr(priv->mc_io, 0,
4287 							priv->mc_token,
4288 							mac_addr);
4289 			if (err) {
4290 				dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
4291 				return err;
4292 			}
4293 		}
4294 		eth_hw_addr_set(net_dev, mac_addr);
4295 	} else if (is_zero_ether_addr(dpni_mac_addr)) {
4296 		/* No MAC address configured, fill in net_dev->dev_addr
4297 		 * with a random one
4298 		 */
4299 		eth_hw_addr_random(net_dev);
4300 		dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
4301 
4302 		err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
4303 						net_dev->dev_addr);
4304 		if (err) {
4305 			dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
4306 			return err;
4307 		}
4308 
4309 		/* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
4310 		 * practical purposes, this will be our "permanent" mac address,
4311 		 * at least until the next reboot. This move will also permit
4312 		 * register_netdevice() to properly fill up net_dev->perm_addr.
4313 		 */
4314 		net_dev->addr_assign_type = NET_ADDR_PERM;
4315 	} else {
4316 		/* NET_ADDR_PERM is default, all we have to do is
4317 		 * fill in the device addr.
4318 		 */
4319 		eth_hw_addr_set(net_dev, dpni_mac_addr);
4320 	}
4321 
4322 	return 0;
4323 }
4324 
4325 static int dpaa2_eth_netdev_init(struct net_device *net_dev)
4326 {
4327 	struct device *dev = net_dev->dev.parent;
4328 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4329 	u32 options = priv->dpni_attrs.options;
4330 	u64 supported = 0, not_supported = 0;
4331 	u8 bcast_addr[ETH_ALEN];
4332 	u8 num_queues;
4333 	int err;
4334 
4335 	net_dev->netdev_ops = &dpaa2_eth_ops;
4336 	net_dev->ethtool_ops = &dpaa2_ethtool_ops;
4337 
4338 	err = dpaa2_eth_set_mac_addr(priv);
4339 	if (err)
4340 		return err;
4341 
4342 	/* Explicitly add the broadcast address to the MAC filtering table */
4343 	eth_broadcast_addr(bcast_addr);
4344 	err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
4345 	if (err) {
4346 		dev_err(dev, "dpni_add_mac_addr() failed\n");
4347 		return err;
4348 	}
4349 
4350 	/* Set MTU upper limit; lower limit is 68B (default value) */
4351 	net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
4352 	err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
4353 					DPAA2_ETH_MFL);
4354 	if (err) {
4355 		dev_err(dev, "dpni_set_max_frame_length() failed\n");
4356 		return err;
4357 	}
4358 
4359 	/* Set actual number of queues in the net device */
4360 	num_queues = dpaa2_eth_queue_count(priv);
4361 	err = netif_set_real_num_tx_queues(net_dev, num_queues);
4362 	if (err) {
4363 		dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
4364 		return err;
4365 	}
4366 	err = netif_set_real_num_rx_queues(net_dev, num_queues);
4367 	if (err) {
4368 		dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
4369 		return err;
4370 	}
4371 
4372 	dpaa2_eth_detect_features(priv);
4373 
4374 	/* Capabilities listing */
4375 	supported |= IFF_LIVE_ADDR_CHANGE;
4376 
4377 	if (options & DPNI_OPT_NO_MAC_FILTER)
4378 		not_supported |= IFF_UNICAST_FLT;
4379 	else
4380 		supported |= IFF_UNICAST_FLT;
4381 
4382 	net_dev->priv_flags |= supported;
4383 	net_dev->priv_flags &= ~not_supported;
4384 
4385 	/* Features */
4386 	net_dev->features = NETIF_F_RXCSUM |
4387 			    NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
4388 			    NETIF_F_SG | NETIF_F_HIGHDMA |
4389 			    NETIF_F_LLTX | NETIF_F_HW_TC | NETIF_F_TSO;
4390 	net_dev->gso_max_segs = DPAA2_ETH_ENQUEUE_MAX_FDS;
4391 	net_dev->hw_features = net_dev->features;
4392 
4393 	if (priv->dpni_attrs.vlan_filter_entries)
4394 		net_dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4395 
4396 	return 0;
4397 }
4398 
4399 static int dpaa2_eth_poll_link_state(void *arg)
4400 {
4401 	struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
4402 	int err;
4403 
4404 	while (!kthread_should_stop()) {
4405 		err = dpaa2_eth_link_state_update(priv);
4406 		if (unlikely(err))
4407 			return err;
4408 
4409 		msleep(DPAA2_ETH_LINK_STATE_REFRESH);
4410 	}
4411 
4412 	return 0;
4413 }
4414 
4415 static int dpaa2_eth_connect_mac(struct dpaa2_eth_priv *priv)
4416 {
4417 	struct fsl_mc_device *dpni_dev, *dpmac_dev;
4418 	struct dpaa2_mac *mac;
4419 	int err;
4420 
4421 	dpni_dev = to_fsl_mc_device(priv->net_dev->dev.parent);
4422 	dpmac_dev = fsl_mc_get_endpoint(dpni_dev, 0);
4423 
4424 	if (PTR_ERR(dpmac_dev) == -EPROBE_DEFER)
4425 		return PTR_ERR(dpmac_dev);
4426 
4427 	if (IS_ERR(dpmac_dev) || dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type)
4428 		return 0;
4429 
4430 	mac = kzalloc(sizeof(struct dpaa2_mac), GFP_KERNEL);
4431 	if (!mac)
4432 		return -ENOMEM;
4433 
4434 	mac->mc_dev = dpmac_dev;
4435 	mac->mc_io = priv->mc_io;
4436 	mac->net_dev = priv->net_dev;
4437 
4438 	err = dpaa2_mac_open(mac);
4439 	if (err)
4440 		goto err_free_mac;
4441 	priv->mac = mac;
4442 
4443 	if (dpaa2_eth_is_type_phy(priv)) {
4444 		err = dpaa2_mac_connect(mac);
4445 		if (err && err != -EPROBE_DEFER)
4446 			netdev_err(priv->net_dev, "Error connecting to the MAC endpoint: %pe",
4447 				   ERR_PTR(err));
4448 		if (err)
4449 			goto err_close_mac;
4450 	}
4451 
4452 	return 0;
4453 
4454 err_close_mac:
4455 	dpaa2_mac_close(mac);
4456 	priv->mac = NULL;
4457 err_free_mac:
4458 	kfree(mac);
4459 	return err;
4460 }
4461 
4462 static void dpaa2_eth_disconnect_mac(struct dpaa2_eth_priv *priv)
4463 {
4464 	if (dpaa2_eth_is_type_phy(priv))
4465 		dpaa2_mac_disconnect(priv->mac);
4466 
4467 	if (!dpaa2_eth_has_mac(priv))
4468 		return;
4469 
4470 	dpaa2_mac_close(priv->mac);
4471 	kfree(priv->mac);
4472 	priv->mac = NULL;
4473 }
4474 
4475 static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
4476 {
4477 	u32 status = ~0;
4478 	struct device *dev = (struct device *)arg;
4479 	struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
4480 	struct net_device *net_dev = dev_get_drvdata(dev);
4481 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4482 	int err;
4483 
4484 	err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
4485 				  DPNI_IRQ_INDEX, &status);
4486 	if (unlikely(err)) {
4487 		netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
4488 		return IRQ_HANDLED;
4489 	}
4490 
4491 	if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
4492 		dpaa2_eth_link_state_update(netdev_priv(net_dev));
4493 
4494 	if (status & DPNI_IRQ_EVENT_ENDPOINT_CHANGED) {
4495 		dpaa2_eth_set_mac_addr(netdev_priv(net_dev));
4496 		dpaa2_eth_update_tx_fqids(priv);
4497 
4498 		rtnl_lock();
4499 		if (dpaa2_eth_has_mac(priv))
4500 			dpaa2_eth_disconnect_mac(priv);
4501 		else
4502 			dpaa2_eth_connect_mac(priv);
4503 		rtnl_unlock();
4504 	}
4505 
4506 	return IRQ_HANDLED;
4507 }
4508 
4509 static int dpaa2_eth_setup_irqs(struct fsl_mc_device *ls_dev)
4510 {
4511 	int err = 0;
4512 	struct fsl_mc_device_irq *irq;
4513 
4514 	err = fsl_mc_allocate_irqs(ls_dev);
4515 	if (err) {
4516 		dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
4517 		return err;
4518 	}
4519 
4520 	irq = ls_dev->irqs[0];
4521 	err = devm_request_threaded_irq(&ls_dev->dev, irq->virq,
4522 					NULL, dpni_irq0_handler_thread,
4523 					IRQF_NO_SUSPEND | IRQF_ONESHOT,
4524 					dev_name(&ls_dev->dev), &ls_dev->dev);
4525 	if (err < 0) {
4526 		dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
4527 		goto free_mc_irq;
4528 	}
4529 
4530 	err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
4531 				DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED |
4532 				DPNI_IRQ_EVENT_ENDPOINT_CHANGED);
4533 	if (err < 0) {
4534 		dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
4535 		goto free_irq;
4536 	}
4537 
4538 	err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
4539 				  DPNI_IRQ_INDEX, 1);
4540 	if (err < 0) {
4541 		dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
4542 		goto free_irq;
4543 	}
4544 
4545 	return 0;
4546 
4547 free_irq:
4548 	devm_free_irq(&ls_dev->dev, irq->virq, &ls_dev->dev);
4549 free_mc_irq:
4550 	fsl_mc_free_irqs(ls_dev);
4551 
4552 	return err;
4553 }
4554 
4555 static void dpaa2_eth_add_ch_napi(struct dpaa2_eth_priv *priv)
4556 {
4557 	int i;
4558 	struct dpaa2_eth_channel *ch;
4559 
4560 	for (i = 0; i < priv->num_channels; i++) {
4561 		ch = priv->channel[i];
4562 		/* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
4563 		netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll,
4564 			       NAPI_POLL_WEIGHT);
4565 	}
4566 }
4567 
4568 static void dpaa2_eth_del_ch_napi(struct dpaa2_eth_priv *priv)
4569 {
4570 	int i;
4571 	struct dpaa2_eth_channel *ch;
4572 
4573 	for (i = 0; i < priv->num_channels; i++) {
4574 		ch = priv->channel[i];
4575 		netif_napi_del(&ch->napi);
4576 	}
4577 }
4578 
4579 static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
4580 {
4581 	struct device *dev;
4582 	struct net_device *net_dev = NULL;
4583 	struct dpaa2_eth_priv *priv = NULL;
4584 	int err = 0;
4585 
4586 	dev = &dpni_dev->dev;
4587 
4588 	/* Net device */
4589 	net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_NETDEV_QUEUES);
4590 	if (!net_dev) {
4591 		dev_err(dev, "alloc_etherdev_mq() failed\n");
4592 		return -ENOMEM;
4593 	}
4594 
4595 	SET_NETDEV_DEV(net_dev, dev);
4596 	dev_set_drvdata(dev, net_dev);
4597 
4598 	priv = netdev_priv(net_dev);
4599 	priv->net_dev = net_dev;
4600 
4601 	priv->iommu_domain = iommu_get_domain_for_dev(dev);
4602 
4603 	priv->tx_tstamp_type = HWTSTAMP_TX_OFF;
4604 	priv->rx_tstamp = false;
4605 
4606 	priv->dpaa2_ptp_wq = alloc_workqueue("dpaa2_ptp_wq", 0, 0);
4607 	if (!priv->dpaa2_ptp_wq) {
4608 		err = -ENOMEM;
4609 		goto err_wq_alloc;
4610 	}
4611 
4612 	INIT_WORK(&priv->tx_onestep_tstamp, dpaa2_eth_tx_onestep_tstamp);
4613 	mutex_init(&priv->onestep_tstamp_lock);
4614 	skb_queue_head_init(&priv->tx_skbs);
4615 
4616 	priv->rx_copybreak = DPAA2_ETH_DEFAULT_COPYBREAK;
4617 
4618 	/* Obtain a MC portal */
4619 	err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
4620 				     &priv->mc_io);
4621 	if (err) {
4622 		if (err == -ENXIO)
4623 			err = -EPROBE_DEFER;
4624 		else
4625 			dev_err(dev, "MC portal allocation failed\n");
4626 		goto err_portal_alloc;
4627 	}
4628 
4629 	/* MC objects initialization and configuration */
4630 	err = dpaa2_eth_setup_dpni(dpni_dev);
4631 	if (err)
4632 		goto err_dpni_setup;
4633 
4634 	err = dpaa2_eth_setup_dpio(priv);
4635 	if (err)
4636 		goto err_dpio_setup;
4637 
4638 	dpaa2_eth_setup_fqs(priv);
4639 
4640 	err = dpaa2_eth_setup_dpbp(priv);
4641 	if (err)
4642 		goto err_dpbp_setup;
4643 
4644 	err = dpaa2_eth_bind_dpni(priv);
4645 	if (err)
4646 		goto err_bind;
4647 
4648 	/* Add a NAPI context for each channel */
4649 	dpaa2_eth_add_ch_napi(priv);
4650 
4651 	/* Percpu statistics */
4652 	priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
4653 	if (!priv->percpu_stats) {
4654 		dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
4655 		err = -ENOMEM;
4656 		goto err_alloc_percpu_stats;
4657 	}
4658 	priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
4659 	if (!priv->percpu_extras) {
4660 		dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
4661 		err = -ENOMEM;
4662 		goto err_alloc_percpu_extras;
4663 	}
4664 
4665 	priv->sgt_cache = alloc_percpu(*priv->sgt_cache);
4666 	if (!priv->sgt_cache) {
4667 		dev_err(dev, "alloc_percpu(sgt_cache) failed\n");
4668 		err = -ENOMEM;
4669 		goto err_alloc_sgt_cache;
4670 	}
4671 
4672 	priv->fd = alloc_percpu(*priv->fd);
4673 	if (!priv->fd) {
4674 		dev_err(dev, "alloc_percpu(fds) failed\n");
4675 		err = -ENOMEM;
4676 		goto err_alloc_fds;
4677 	}
4678 
4679 	err = dpaa2_eth_netdev_init(net_dev);
4680 	if (err)
4681 		goto err_netdev_init;
4682 
4683 	/* Configure checksum offload based on current interface flags */
4684 	err = dpaa2_eth_set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
4685 	if (err)
4686 		goto err_csum;
4687 
4688 	err = dpaa2_eth_set_tx_csum(priv,
4689 				    !!(net_dev->features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
4690 	if (err)
4691 		goto err_csum;
4692 
4693 	err = dpaa2_eth_alloc_rings(priv);
4694 	if (err)
4695 		goto err_alloc_rings;
4696 
4697 #ifdef CONFIG_FSL_DPAA2_ETH_DCB
4698 	if (dpaa2_eth_has_pause_support(priv) && priv->vlan_cls_enabled) {
4699 		priv->dcbx_mode = DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE;
4700 		net_dev->dcbnl_ops = &dpaa2_eth_dcbnl_ops;
4701 	} else {
4702 		dev_dbg(dev, "PFC not supported\n");
4703 	}
4704 #endif
4705 
4706 	err = dpaa2_eth_setup_irqs(dpni_dev);
4707 	if (err) {
4708 		netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
4709 		priv->poll_thread = kthread_run(dpaa2_eth_poll_link_state, priv,
4710 						"%s_poll_link", net_dev->name);
4711 		if (IS_ERR(priv->poll_thread)) {
4712 			dev_err(dev, "Error starting polling thread\n");
4713 			goto err_poll_thread;
4714 		}
4715 		priv->do_link_poll = true;
4716 	}
4717 
4718 	err = dpaa2_eth_connect_mac(priv);
4719 	if (err)
4720 		goto err_connect_mac;
4721 
4722 	err = dpaa2_eth_dl_alloc(priv);
4723 	if (err)
4724 		goto err_dl_register;
4725 
4726 	err = dpaa2_eth_dl_traps_register(priv);
4727 	if (err)
4728 		goto err_dl_trap_register;
4729 
4730 	err = dpaa2_eth_dl_port_add(priv);
4731 	if (err)
4732 		goto err_dl_port_add;
4733 
4734 	err = register_netdev(net_dev);
4735 	if (err < 0) {
4736 		dev_err(dev, "register_netdev() failed\n");
4737 		goto err_netdev_reg;
4738 	}
4739 
4740 #ifdef CONFIG_DEBUG_FS
4741 	dpaa2_dbg_add(priv);
4742 #endif
4743 
4744 	dpaa2_eth_dl_register(priv);
4745 	dev_info(dev, "Probed interface %s\n", net_dev->name);
4746 	return 0;
4747 
4748 err_netdev_reg:
4749 	dpaa2_eth_dl_port_del(priv);
4750 err_dl_port_add:
4751 	dpaa2_eth_dl_traps_unregister(priv);
4752 err_dl_trap_register:
4753 	dpaa2_eth_dl_free(priv);
4754 err_dl_register:
4755 	dpaa2_eth_disconnect_mac(priv);
4756 err_connect_mac:
4757 	if (priv->do_link_poll)
4758 		kthread_stop(priv->poll_thread);
4759 	else
4760 		fsl_mc_free_irqs(dpni_dev);
4761 err_poll_thread:
4762 	dpaa2_eth_free_rings(priv);
4763 err_alloc_rings:
4764 err_csum:
4765 err_netdev_init:
4766 	free_percpu(priv->fd);
4767 err_alloc_fds:
4768 	free_percpu(priv->sgt_cache);
4769 err_alloc_sgt_cache:
4770 	free_percpu(priv->percpu_extras);
4771 err_alloc_percpu_extras:
4772 	free_percpu(priv->percpu_stats);
4773 err_alloc_percpu_stats:
4774 	dpaa2_eth_del_ch_napi(priv);
4775 err_bind:
4776 	dpaa2_eth_free_dpbp(priv);
4777 err_dpbp_setup:
4778 	dpaa2_eth_free_dpio(priv);
4779 err_dpio_setup:
4780 	dpaa2_eth_free_dpni(priv);
4781 err_dpni_setup:
4782 	fsl_mc_portal_free(priv->mc_io);
4783 err_portal_alloc:
4784 	destroy_workqueue(priv->dpaa2_ptp_wq);
4785 err_wq_alloc:
4786 	dev_set_drvdata(dev, NULL);
4787 	free_netdev(net_dev);
4788 
4789 	return err;
4790 }
4791 
4792 static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
4793 {
4794 	struct device *dev;
4795 	struct net_device *net_dev;
4796 	struct dpaa2_eth_priv *priv;
4797 
4798 	dev = &ls_dev->dev;
4799 	net_dev = dev_get_drvdata(dev);
4800 	priv = netdev_priv(net_dev);
4801 
4802 	dpaa2_eth_dl_unregister(priv);
4803 
4804 #ifdef CONFIG_DEBUG_FS
4805 	dpaa2_dbg_remove(priv);
4806 #endif
4807 
4808 	unregister_netdev(net_dev);
4809 	rtnl_lock();
4810 	dpaa2_eth_disconnect_mac(priv);
4811 	rtnl_unlock();
4812 
4813 	dpaa2_eth_dl_port_del(priv);
4814 	dpaa2_eth_dl_traps_unregister(priv);
4815 	dpaa2_eth_dl_free(priv);
4816 
4817 	if (priv->do_link_poll)
4818 		kthread_stop(priv->poll_thread);
4819 	else
4820 		fsl_mc_free_irqs(ls_dev);
4821 
4822 	dpaa2_eth_free_rings(priv);
4823 	free_percpu(priv->fd);
4824 	free_percpu(priv->sgt_cache);
4825 	free_percpu(priv->percpu_stats);
4826 	free_percpu(priv->percpu_extras);
4827 
4828 	dpaa2_eth_del_ch_napi(priv);
4829 	dpaa2_eth_free_dpbp(priv);
4830 	dpaa2_eth_free_dpio(priv);
4831 	dpaa2_eth_free_dpni(priv);
4832 	if (priv->onestep_reg_base)
4833 		iounmap(priv->onestep_reg_base);
4834 
4835 	fsl_mc_portal_free(priv->mc_io);
4836 
4837 	destroy_workqueue(priv->dpaa2_ptp_wq);
4838 
4839 	dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
4840 
4841 	free_netdev(net_dev);
4842 
4843 	return 0;
4844 }
4845 
4846 static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
4847 	{
4848 		.vendor = FSL_MC_VENDOR_FREESCALE,
4849 		.obj_type = "dpni",
4850 	},
4851 	{ .vendor = 0x0 }
4852 };
4853 MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
4854 
4855 static struct fsl_mc_driver dpaa2_eth_driver = {
4856 	.driver = {
4857 		.name = KBUILD_MODNAME,
4858 		.owner = THIS_MODULE,
4859 	},
4860 	.probe = dpaa2_eth_probe,
4861 	.remove = dpaa2_eth_remove,
4862 	.match_id_table = dpaa2_eth_match_id_table
4863 };
4864 
4865 static int __init dpaa2_eth_driver_init(void)
4866 {
4867 	int err;
4868 
4869 	dpaa2_eth_dbg_init();
4870 	err = fsl_mc_driver_register(&dpaa2_eth_driver);
4871 	if (err) {
4872 		dpaa2_eth_dbg_exit();
4873 		return err;
4874 	}
4875 
4876 	return 0;
4877 }
4878 
4879 static void __exit dpaa2_eth_driver_exit(void)
4880 {
4881 	dpaa2_eth_dbg_exit();
4882 	fsl_mc_driver_unregister(&dpaa2_eth_driver);
4883 }
4884 
4885 module_init(dpaa2_eth_driver_init);
4886 module_exit(dpaa2_eth_driver_exit);
4887