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