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