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