1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /* Copyright 2014-2016 Freescale Semiconductor Inc.
3  * Copyright 2016-2017 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/net_tstamp.h>
15 #include <linux/fsl/mc.h>
16 #include <linux/bpf.h>
17 #include <linux/bpf_trace.h>
18 #include <net/sock.h>
19 
20 #include "dpaa2-eth.h"
21 
22 /* CREATE_TRACE_POINTS only needs to be defined once. Other dpa files
23  * using trace events only need to #include <trace/events/sched.h>
24  */
25 #define CREATE_TRACE_POINTS
26 #include "dpaa2-eth-trace.h"
27 
28 MODULE_LICENSE("Dual BSD/GPL");
29 MODULE_AUTHOR("Freescale Semiconductor, Inc");
30 MODULE_DESCRIPTION("Freescale DPAA2 Ethernet Driver");
31 
32 static void *dpaa2_iova_to_virt(struct iommu_domain *domain,
33 				dma_addr_t iova_addr)
34 {
35 	phys_addr_t phys_addr;
36 
37 	phys_addr = domain ? iommu_iova_to_phys(domain, iova_addr) : iova_addr;
38 
39 	return phys_to_virt(phys_addr);
40 }
41 
42 static void validate_rx_csum(struct dpaa2_eth_priv *priv,
43 			     u32 fd_status,
44 			     struct sk_buff *skb)
45 {
46 	skb_checksum_none_assert(skb);
47 
48 	/* HW checksum validation is disabled, nothing to do here */
49 	if (!(priv->net_dev->features & NETIF_F_RXCSUM))
50 		return;
51 
52 	/* Read checksum validation bits */
53 	if (!((fd_status & DPAA2_FAS_L3CV) &&
54 	      (fd_status & DPAA2_FAS_L4CV)))
55 		return;
56 
57 	/* Inform the stack there's no need to compute L3/L4 csum anymore */
58 	skb->ip_summed = CHECKSUM_UNNECESSARY;
59 }
60 
61 /* Free a received FD.
62  * Not to be used for Tx conf FDs or on any other paths.
63  */
64 static void free_rx_fd(struct dpaa2_eth_priv *priv,
65 		       const struct dpaa2_fd *fd,
66 		       void *vaddr)
67 {
68 	struct device *dev = priv->net_dev->dev.parent;
69 	dma_addr_t addr = dpaa2_fd_get_addr(fd);
70 	u8 fd_format = dpaa2_fd_get_format(fd);
71 	struct dpaa2_sg_entry *sgt;
72 	void *sg_vaddr;
73 	int i;
74 
75 	/* If single buffer frame, just free the data buffer */
76 	if (fd_format == dpaa2_fd_single)
77 		goto free_buf;
78 	else if (fd_format != dpaa2_fd_sg)
79 		/* We don't support any other format */
80 		return;
81 
82 	/* For S/G frames, we first need to free all SG entries
83 	 * except the first one, which was taken care of already
84 	 */
85 	sgt = vaddr + dpaa2_fd_get_offset(fd);
86 	for (i = 1; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
87 		addr = dpaa2_sg_get_addr(&sgt[i]);
88 		sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
89 		dma_unmap_page(dev, addr, DPAA2_ETH_RX_BUF_SIZE,
90 			       DMA_BIDIRECTIONAL);
91 
92 		free_pages((unsigned long)sg_vaddr, 0);
93 		if (dpaa2_sg_is_final(&sgt[i]))
94 			break;
95 	}
96 
97 free_buf:
98 	free_pages((unsigned long)vaddr, 0);
99 }
100 
101 /* Build a linear skb based on a single-buffer frame descriptor */
102 static struct sk_buff *build_linear_skb(struct dpaa2_eth_channel *ch,
103 					const struct dpaa2_fd *fd,
104 					void *fd_vaddr)
105 {
106 	struct sk_buff *skb = NULL;
107 	u16 fd_offset = dpaa2_fd_get_offset(fd);
108 	u32 fd_length = dpaa2_fd_get_len(fd);
109 
110 	ch->buf_count--;
111 
112 	skb = build_skb(fd_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
113 	if (unlikely(!skb))
114 		return NULL;
115 
116 	skb_reserve(skb, fd_offset);
117 	skb_put(skb, fd_length);
118 
119 	return skb;
120 }
121 
122 /* Build a non linear (fragmented) skb based on a S/G table */
123 static struct sk_buff *build_frag_skb(struct dpaa2_eth_priv *priv,
124 				      struct dpaa2_eth_channel *ch,
125 				      struct dpaa2_sg_entry *sgt)
126 {
127 	struct sk_buff *skb = NULL;
128 	struct device *dev = priv->net_dev->dev.parent;
129 	void *sg_vaddr;
130 	dma_addr_t sg_addr;
131 	u16 sg_offset;
132 	u32 sg_length;
133 	struct page *page, *head_page;
134 	int page_offset;
135 	int i;
136 
137 	for (i = 0; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
138 		struct dpaa2_sg_entry *sge = &sgt[i];
139 
140 		/* NOTE: We only support SG entries in dpaa2_sg_single format,
141 		 * but this is the only format we may receive from HW anyway
142 		 */
143 
144 		/* Get the address and length from the S/G entry */
145 		sg_addr = dpaa2_sg_get_addr(sge);
146 		sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, sg_addr);
147 		dma_unmap_page(dev, sg_addr, DPAA2_ETH_RX_BUF_SIZE,
148 			       DMA_BIDIRECTIONAL);
149 
150 		sg_length = dpaa2_sg_get_len(sge);
151 
152 		if (i == 0) {
153 			/* We build the skb around the first data buffer */
154 			skb = build_skb(sg_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
155 			if (unlikely(!skb)) {
156 				/* Free the first SG entry now, since we already
157 				 * unmapped it and obtained the virtual address
158 				 */
159 				free_pages((unsigned long)sg_vaddr, 0);
160 
161 				/* We still need to subtract the buffers used
162 				 * by this FD from our software counter
163 				 */
164 				while (!dpaa2_sg_is_final(&sgt[i]) &&
165 				       i < DPAA2_ETH_MAX_SG_ENTRIES)
166 					i++;
167 				break;
168 			}
169 
170 			sg_offset = dpaa2_sg_get_offset(sge);
171 			skb_reserve(skb, sg_offset);
172 			skb_put(skb, sg_length);
173 		} else {
174 			/* Rest of the data buffers are stored as skb frags */
175 			page = virt_to_page(sg_vaddr);
176 			head_page = virt_to_head_page(sg_vaddr);
177 
178 			/* Offset in page (which may be compound).
179 			 * Data in subsequent SG entries is stored from the
180 			 * beginning of the buffer, so we don't need to add the
181 			 * sg_offset.
182 			 */
183 			page_offset = ((unsigned long)sg_vaddr &
184 				(PAGE_SIZE - 1)) +
185 				(page_address(page) - page_address(head_page));
186 
187 			skb_add_rx_frag(skb, i - 1, head_page, page_offset,
188 					sg_length, DPAA2_ETH_RX_BUF_SIZE);
189 		}
190 
191 		if (dpaa2_sg_is_final(sge))
192 			break;
193 	}
194 
195 	WARN_ONCE(i == DPAA2_ETH_MAX_SG_ENTRIES, "Final bit not set in SGT");
196 
197 	/* Count all data buffers + SG table buffer */
198 	ch->buf_count -= i + 2;
199 
200 	return skb;
201 }
202 
203 /* Free buffers acquired from the buffer pool or which were meant to
204  * be released in the pool
205  */
206 static void free_bufs(struct dpaa2_eth_priv *priv, u64 *buf_array, int count)
207 {
208 	struct device *dev = priv->net_dev->dev.parent;
209 	void *vaddr;
210 	int i;
211 
212 	for (i = 0; i < count; i++) {
213 		vaddr = dpaa2_iova_to_virt(priv->iommu_domain, buf_array[i]);
214 		dma_unmap_page(dev, buf_array[i], DPAA2_ETH_RX_BUF_SIZE,
215 			       DMA_BIDIRECTIONAL);
216 		free_pages((unsigned long)vaddr, 0);
217 	}
218 }
219 
220 static void xdp_release_buf(struct dpaa2_eth_priv *priv,
221 			    struct dpaa2_eth_channel *ch,
222 			    dma_addr_t addr)
223 {
224 	int err;
225 
226 	ch->xdp.drop_bufs[ch->xdp.drop_cnt++] = addr;
227 	if (ch->xdp.drop_cnt < DPAA2_ETH_BUFS_PER_CMD)
228 		return;
229 
230 	while ((err = dpaa2_io_service_release(ch->dpio, priv->bpid,
231 					       ch->xdp.drop_bufs,
232 					       ch->xdp.drop_cnt)) == -EBUSY)
233 		cpu_relax();
234 
235 	if (err) {
236 		free_bufs(priv, ch->xdp.drop_bufs, ch->xdp.drop_cnt);
237 		ch->buf_count -= ch->xdp.drop_cnt;
238 	}
239 
240 	ch->xdp.drop_cnt = 0;
241 }
242 
243 static int xdp_enqueue(struct dpaa2_eth_priv *priv, struct dpaa2_fd *fd,
244 		       void *buf_start, u16 queue_id)
245 {
246 	struct dpaa2_eth_fq *fq;
247 	struct dpaa2_faead *faead;
248 	u32 ctrl, frc;
249 	int i, err;
250 
251 	/* Mark the egress frame hardware annotation area as valid */
252 	frc = dpaa2_fd_get_frc(fd);
253 	dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
254 	dpaa2_fd_set_ctrl(fd, DPAA2_FD_CTRL_ASAL);
255 
256 	/* Instruct hardware to release the FD buffer directly into
257 	 * the buffer pool once transmission is completed, instead of
258 	 * sending a Tx confirmation frame to us
259 	 */
260 	ctrl = DPAA2_FAEAD_A4V | DPAA2_FAEAD_A2V | DPAA2_FAEAD_EBDDV;
261 	faead = dpaa2_get_faead(buf_start, false);
262 	faead->ctrl = cpu_to_le32(ctrl);
263 	faead->conf_fqid = 0;
264 
265 	fq = &priv->fq[queue_id];
266 	for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
267 		err = priv->enqueue(priv, fq, fd, 0);
268 		if (err != -EBUSY)
269 			break;
270 	}
271 
272 	return err;
273 }
274 
275 static u32 run_xdp(struct dpaa2_eth_priv *priv,
276 		   struct dpaa2_eth_channel *ch,
277 		   struct dpaa2_eth_fq *rx_fq,
278 		   struct dpaa2_fd *fd, void *vaddr)
279 {
280 	dma_addr_t addr = dpaa2_fd_get_addr(fd);
281 	struct rtnl_link_stats64 *percpu_stats;
282 	struct bpf_prog *xdp_prog;
283 	struct xdp_buff xdp;
284 	u32 xdp_act = XDP_PASS;
285 	int err;
286 
287 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
288 
289 	rcu_read_lock();
290 
291 	xdp_prog = READ_ONCE(ch->xdp.prog);
292 	if (!xdp_prog)
293 		goto out;
294 
295 	xdp.data = vaddr + dpaa2_fd_get_offset(fd);
296 	xdp.data_end = xdp.data + dpaa2_fd_get_len(fd);
297 	xdp.data_hard_start = xdp.data - XDP_PACKET_HEADROOM;
298 	xdp_set_data_meta_invalid(&xdp);
299 	xdp.rxq = &ch->xdp_rxq;
300 
301 	xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
302 
303 	/* xdp.data pointer may have changed */
304 	dpaa2_fd_set_offset(fd, xdp.data - vaddr);
305 	dpaa2_fd_set_len(fd, xdp.data_end - xdp.data);
306 
307 	switch (xdp_act) {
308 	case XDP_PASS:
309 		break;
310 	case XDP_TX:
311 		err = xdp_enqueue(priv, fd, vaddr, rx_fq->flowid);
312 		if (err) {
313 			xdp_release_buf(priv, ch, addr);
314 			percpu_stats->tx_errors++;
315 			ch->stats.xdp_tx_err++;
316 		} else {
317 			percpu_stats->tx_packets++;
318 			percpu_stats->tx_bytes += dpaa2_fd_get_len(fd);
319 			ch->stats.xdp_tx++;
320 		}
321 		break;
322 	default:
323 		bpf_warn_invalid_xdp_action(xdp_act);
324 		/* fall through */
325 	case XDP_ABORTED:
326 		trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
327 		/* fall through */
328 	case XDP_DROP:
329 		xdp_release_buf(priv, ch, addr);
330 		ch->stats.xdp_drop++;
331 		break;
332 	case XDP_REDIRECT:
333 		dma_unmap_page(priv->net_dev->dev.parent, addr,
334 			       DPAA2_ETH_RX_BUF_SIZE, DMA_BIDIRECTIONAL);
335 		ch->buf_count--;
336 		xdp.data_hard_start = vaddr;
337 		err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog);
338 		if (unlikely(err))
339 			ch->stats.xdp_drop++;
340 		else
341 			ch->stats.xdp_redirect++;
342 		break;
343 	}
344 
345 	ch->xdp.res |= xdp_act;
346 out:
347 	rcu_read_unlock();
348 	return xdp_act;
349 }
350 
351 /* Main Rx frame processing routine */
352 static void dpaa2_eth_rx(struct dpaa2_eth_priv *priv,
353 			 struct dpaa2_eth_channel *ch,
354 			 const struct dpaa2_fd *fd,
355 			 struct dpaa2_eth_fq *fq)
356 {
357 	dma_addr_t addr = dpaa2_fd_get_addr(fd);
358 	u8 fd_format = dpaa2_fd_get_format(fd);
359 	void *vaddr;
360 	struct sk_buff *skb;
361 	struct rtnl_link_stats64 *percpu_stats;
362 	struct dpaa2_eth_drv_stats *percpu_extras;
363 	struct device *dev = priv->net_dev->dev.parent;
364 	struct dpaa2_fas *fas;
365 	void *buf_data;
366 	u32 status = 0;
367 	u32 xdp_act;
368 
369 	/* Tracing point */
370 	trace_dpaa2_rx_fd(priv->net_dev, fd);
371 
372 	vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
373 	dma_sync_single_for_cpu(dev, addr, DPAA2_ETH_RX_BUF_SIZE,
374 				DMA_BIDIRECTIONAL);
375 
376 	fas = dpaa2_get_fas(vaddr, false);
377 	prefetch(fas);
378 	buf_data = vaddr + dpaa2_fd_get_offset(fd);
379 	prefetch(buf_data);
380 
381 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
382 	percpu_extras = this_cpu_ptr(priv->percpu_extras);
383 
384 	if (fd_format == dpaa2_fd_single) {
385 		xdp_act = run_xdp(priv, ch, fq, (struct dpaa2_fd *)fd, vaddr);
386 		if (xdp_act != XDP_PASS) {
387 			percpu_stats->rx_packets++;
388 			percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
389 			return;
390 		}
391 
392 		dma_unmap_page(dev, addr, DPAA2_ETH_RX_BUF_SIZE,
393 			       DMA_BIDIRECTIONAL);
394 		skb = build_linear_skb(ch, fd, vaddr);
395 	} else if (fd_format == dpaa2_fd_sg) {
396 		WARN_ON(priv->xdp_prog);
397 
398 		dma_unmap_page(dev, addr, DPAA2_ETH_RX_BUF_SIZE,
399 			       DMA_BIDIRECTIONAL);
400 		skb = build_frag_skb(priv, ch, buf_data);
401 		free_pages((unsigned long)vaddr, 0);
402 		percpu_extras->rx_sg_frames++;
403 		percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd);
404 	} else {
405 		/* We don't support any other format */
406 		goto err_frame_format;
407 	}
408 
409 	if (unlikely(!skb))
410 		goto err_build_skb;
411 
412 	prefetch(skb->data);
413 
414 	/* Get the timestamp value */
415 	if (priv->rx_tstamp) {
416 		struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
417 		__le64 *ts = dpaa2_get_ts(vaddr, false);
418 		u64 ns;
419 
420 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
421 
422 		ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
423 		shhwtstamps->hwtstamp = ns_to_ktime(ns);
424 	}
425 
426 	/* Check if we need to validate the L4 csum */
427 	if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) {
428 		status = le32_to_cpu(fas->status);
429 		validate_rx_csum(priv, status, skb);
430 	}
431 
432 	skb->protocol = eth_type_trans(skb, priv->net_dev);
433 	skb_record_rx_queue(skb, fq->flowid);
434 
435 	percpu_stats->rx_packets++;
436 	percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
437 
438 	list_add_tail(&skb->list, ch->rx_list);
439 
440 	return;
441 
442 err_build_skb:
443 	free_rx_fd(priv, fd, vaddr);
444 err_frame_format:
445 	percpu_stats->rx_dropped++;
446 }
447 
448 /* Consume all frames pull-dequeued into the store. This is the simplest way to
449  * make sure we don't accidentally issue another volatile dequeue which would
450  * overwrite (leak) frames already in the store.
451  *
452  * Observance of NAPI budget is not our concern, leaving that to the caller.
453  */
454 static int consume_frames(struct dpaa2_eth_channel *ch,
455 			  struct dpaa2_eth_fq **src)
456 {
457 	struct dpaa2_eth_priv *priv = ch->priv;
458 	struct dpaa2_eth_fq *fq = NULL;
459 	struct dpaa2_dq *dq;
460 	const struct dpaa2_fd *fd;
461 	int cleaned = 0;
462 	int is_last;
463 
464 	do {
465 		dq = dpaa2_io_store_next(ch->store, &is_last);
466 		if (unlikely(!dq)) {
467 			/* If we're here, we *must* have placed a
468 			 * volatile dequeue comnmand, so keep reading through
469 			 * the store until we get some sort of valid response
470 			 * token (either a valid frame or an "empty dequeue")
471 			 */
472 			continue;
473 		}
474 
475 		fd = dpaa2_dq_fd(dq);
476 		fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq);
477 
478 		fq->consume(priv, ch, fd, fq);
479 		cleaned++;
480 	} while (!is_last);
481 
482 	if (!cleaned)
483 		return 0;
484 
485 	fq->stats.frames += cleaned;
486 
487 	/* A dequeue operation only pulls frames from a single queue
488 	 * into the store. Return the frame queue as an out param.
489 	 */
490 	if (src)
491 		*src = fq;
492 
493 	return cleaned;
494 }
495 
496 /* Configure the egress frame annotation for timestamp update */
497 static void enable_tx_tstamp(struct dpaa2_fd *fd, void *buf_start)
498 {
499 	struct dpaa2_faead *faead;
500 	u32 ctrl, frc;
501 
502 	/* Mark the egress frame annotation area as valid */
503 	frc = dpaa2_fd_get_frc(fd);
504 	dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
505 
506 	/* Set hardware annotation size */
507 	ctrl = dpaa2_fd_get_ctrl(fd);
508 	dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL);
509 
510 	/* enable UPD (update prepanded data) bit in FAEAD field of
511 	 * hardware frame annotation area
512 	 */
513 	ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD;
514 	faead = dpaa2_get_faead(buf_start, true);
515 	faead->ctrl = cpu_to_le32(ctrl);
516 }
517 
518 /* Create a frame descriptor based on a fragmented skb */
519 static int build_sg_fd(struct dpaa2_eth_priv *priv,
520 		       struct sk_buff *skb,
521 		       struct dpaa2_fd *fd)
522 {
523 	struct device *dev = priv->net_dev->dev.parent;
524 	void *sgt_buf = NULL;
525 	dma_addr_t addr;
526 	int nr_frags = skb_shinfo(skb)->nr_frags;
527 	struct dpaa2_sg_entry *sgt;
528 	int i, err;
529 	int sgt_buf_size;
530 	struct scatterlist *scl, *crt_scl;
531 	int num_sg;
532 	int num_dma_bufs;
533 	struct dpaa2_eth_swa *swa;
534 
535 	/* Create and map scatterlist.
536 	 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have
537 	 * to go beyond nr_frags+1.
538 	 * Note: We don't support chained scatterlists
539 	 */
540 	if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1))
541 		return -EINVAL;
542 
543 	scl = kcalloc(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC);
544 	if (unlikely(!scl))
545 		return -ENOMEM;
546 
547 	sg_init_table(scl, nr_frags + 1);
548 	num_sg = skb_to_sgvec(skb, scl, 0, skb->len);
549 	num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
550 	if (unlikely(!num_dma_bufs)) {
551 		err = -ENOMEM;
552 		goto dma_map_sg_failed;
553 	}
554 
555 	/* Prepare the HW SGT structure */
556 	sgt_buf_size = priv->tx_data_offset +
557 		       sizeof(struct dpaa2_sg_entry) *  num_dma_bufs;
558 	sgt_buf = netdev_alloc_frag(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN);
559 	if (unlikely(!sgt_buf)) {
560 		err = -ENOMEM;
561 		goto sgt_buf_alloc_failed;
562 	}
563 	sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN);
564 	memset(sgt_buf, 0, sgt_buf_size);
565 
566 	sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
567 
568 	/* Fill in the HW SGT structure.
569 	 *
570 	 * sgt_buf is zeroed out, so the following fields are implicit
571 	 * in all sgt entries:
572 	 *   - offset is 0
573 	 *   - format is 'dpaa2_sg_single'
574 	 */
575 	for_each_sg(scl, crt_scl, num_dma_bufs, i) {
576 		dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl));
577 		dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl));
578 	}
579 	dpaa2_sg_set_final(&sgt[i - 1], true);
580 
581 	/* Store the skb backpointer in the SGT buffer.
582 	 * Fit the scatterlist and the number of buffers alongside the
583 	 * skb backpointer in the software annotation area. We'll need
584 	 * all of them on Tx Conf.
585 	 */
586 	swa = (struct dpaa2_eth_swa *)sgt_buf;
587 	swa->type = DPAA2_ETH_SWA_SG;
588 	swa->sg.skb = skb;
589 	swa->sg.scl = scl;
590 	swa->sg.num_sg = num_sg;
591 	swa->sg.sgt_size = sgt_buf_size;
592 
593 	/* Separately map the SGT buffer */
594 	addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
595 	if (unlikely(dma_mapping_error(dev, addr))) {
596 		err = -ENOMEM;
597 		goto dma_map_single_failed;
598 	}
599 	dpaa2_fd_set_offset(fd, priv->tx_data_offset);
600 	dpaa2_fd_set_format(fd, dpaa2_fd_sg);
601 	dpaa2_fd_set_addr(fd, addr);
602 	dpaa2_fd_set_len(fd, skb->len);
603 	dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
604 
605 	if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
606 		enable_tx_tstamp(fd, sgt_buf);
607 
608 	return 0;
609 
610 dma_map_single_failed:
611 	skb_free_frag(sgt_buf);
612 sgt_buf_alloc_failed:
613 	dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
614 dma_map_sg_failed:
615 	kfree(scl);
616 	return err;
617 }
618 
619 /* Create a frame descriptor based on a linear skb */
620 static int build_single_fd(struct dpaa2_eth_priv *priv,
621 			   struct sk_buff *skb,
622 			   struct dpaa2_fd *fd)
623 {
624 	struct device *dev = priv->net_dev->dev.parent;
625 	u8 *buffer_start, *aligned_start;
626 	struct dpaa2_eth_swa *swa;
627 	dma_addr_t addr;
628 
629 	buffer_start = skb->data - dpaa2_eth_needed_headroom(priv, skb);
630 
631 	/* If there's enough room to align the FD address, do it.
632 	 * It will help hardware optimize accesses.
633 	 */
634 	aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
635 				  DPAA2_ETH_TX_BUF_ALIGN);
636 	if (aligned_start >= skb->head)
637 		buffer_start = aligned_start;
638 
639 	/* Store a backpointer to the skb at the beginning of the buffer
640 	 * (in the private data area) such that we can release it
641 	 * on Tx confirm
642 	 */
643 	swa = (struct dpaa2_eth_swa *)buffer_start;
644 	swa->type = DPAA2_ETH_SWA_SINGLE;
645 	swa->single.skb = skb;
646 
647 	addr = dma_map_single(dev, buffer_start,
648 			      skb_tail_pointer(skb) - buffer_start,
649 			      DMA_BIDIRECTIONAL);
650 	if (unlikely(dma_mapping_error(dev, addr)))
651 		return -ENOMEM;
652 
653 	dpaa2_fd_set_addr(fd, addr);
654 	dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start));
655 	dpaa2_fd_set_len(fd, skb->len);
656 	dpaa2_fd_set_format(fd, dpaa2_fd_single);
657 	dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
658 
659 	if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
660 		enable_tx_tstamp(fd, buffer_start);
661 
662 	return 0;
663 }
664 
665 /* FD freeing routine on the Tx path
666  *
667  * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb
668  * back-pointed to is also freed.
669  * This can be called either from dpaa2_eth_tx_conf() or on the error path of
670  * dpaa2_eth_tx().
671  */
672 static void free_tx_fd(const struct dpaa2_eth_priv *priv,
673 		       struct dpaa2_eth_fq *fq,
674 		       const struct dpaa2_fd *fd, bool in_napi)
675 {
676 	struct device *dev = priv->net_dev->dev.parent;
677 	dma_addr_t fd_addr;
678 	struct sk_buff *skb = NULL;
679 	unsigned char *buffer_start;
680 	struct dpaa2_eth_swa *swa;
681 	u8 fd_format = dpaa2_fd_get_format(fd);
682 	u32 fd_len = dpaa2_fd_get_len(fd);
683 
684 	fd_addr = dpaa2_fd_get_addr(fd);
685 	buffer_start = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr);
686 	swa = (struct dpaa2_eth_swa *)buffer_start;
687 
688 	if (fd_format == dpaa2_fd_single) {
689 		if (swa->type == DPAA2_ETH_SWA_SINGLE) {
690 			skb = swa->single.skb;
691 			/* Accessing the skb buffer is safe before dma unmap,
692 			 * because we didn't map the actual skb shell.
693 			 */
694 			dma_unmap_single(dev, fd_addr,
695 					 skb_tail_pointer(skb) - buffer_start,
696 					 DMA_BIDIRECTIONAL);
697 		} else {
698 			WARN_ONCE(swa->type != DPAA2_ETH_SWA_XDP, "Wrong SWA type");
699 			dma_unmap_single(dev, fd_addr, swa->xdp.dma_size,
700 					 DMA_BIDIRECTIONAL);
701 		}
702 	} else if (fd_format == dpaa2_fd_sg) {
703 		skb = swa->sg.skb;
704 
705 		/* Unmap the scatterlist */
706 		dma_unmap_sg(dev, swa->sg.scl, swa->sg.num_sg,
707 			     DMA_BIDIRECTIONAL);
708 		kfree(swa->sg.scl);
709 
710 		/* Unmap the SGT buffer */
711 		dma_unmap_single(dev, fd_addr, swa->sg.sgt_size,
712 				 DMA_BIDIRECTIONAL);
713 	} else {
714 		netdev_dbg(priv->net_dev, "Invalid FD format\n");
715 		return;
716 	}
717 
718 	if (swa->type != DPAA2_ETH_SWA_XDP && in_napi) {
719 		fq->dq_frames++;
720 		fq->dq_bytes += fd_len;
721 	}
722 
723 	if (swa->type == DPAA2_ETH_SWA_XDP) {
724 		xdp_return_frame(swa->xdp.xdpf);
725 		return;
726 	}
727 
728 	/* Get the timestamp value */
729 	if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
730 		struct skb_shared_hwtstamps shhwtstamps;
731 		__le64 *ts = dpaa2_get_ts(buffer_start, true);
732 		u64 ns;
733 
734 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
735 
736 		ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
737 		shhwtstamps.hwtstamp = ns_to_ktime(ns);
738 		skb_tstamp_tx(skb, &shhwtstamps);
739 	}
740 
741 	/* Free SGT buffer allocated on tx */
742 	if (fd_format != dpaa2_fd_single)
743 		skb_free_frag(buffer_start);
744 
745 	/* Move on with skb release */
746 	napi_consume_skb(skb, in_napi);
747 }
748 
749 static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev)
750 {
751 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
752 	struct dpaa2_fd fd;
753 	struct rtnl_link_stats64 *percpu_stats;
754 	struct dpaa2_eth_drv_stats *percpu_extras;
755 	struct dpaa2_eth_fq *fq;
756 	struct netdev_queue *nq;
757 	u16 queue_mapping;
758 	unsigned int needed_headroom;
759 	u32 fd_len;
760 	int err, i;
761 
762 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
763 	percpu_extras = this_cpu_ptr(priv->percpu_extras);
764 
765 	needed_headroom = dpaa2_eth_needed_headroom(priv, skb);
766 	if (skb_headroom(skb) < needed_headroom) {
767 		struct sk_buff *ns;
768 
769 		ns = skb_realloc_headroom(skb, needed_headroom);
770 		if (unlikely(!ns)) {
771 			percpu_stats->tx_dropped++;
772 			goto err_alloc_headroom;
773 		}
774 		percpu_extras->tx_reallocs++;
775 
776 		if (skb->sk)
777 			skb_set_owner_w(ns, skb->sk);
778 
779 		dev_kfree_skb(skb);
780 		skb = ns;
781 	}
782 
783 	/* We'll be holding a back-reference to the skb until Tx Confirmation;
784 	 * we don't want that overwritten by a concurrent Tx with a cloned skb.
785 	 */
786 	skb = skb_unshare(skb, GFP_ATOMIC);
787 	if (unlikely(!skb)) {
788 		/* skb_unshare() has already freed the skb */
789 		percpu_stats->tx_dropped++;
790 		return NETDEV_TX_OK;
791 	}
792 
793 	/* Setup the FD fields */
794 	memset(&fd, 0, sizeof(fd));
795 
796 	if (skb_is_nonlinear(skb)) {
797 		err = build_sg_fd(priv, skb, &fd);
798 		percpu_extras->tx_sg_frames++;
799 		percpu_extras->tx_sg_bytes += skb->len;
800 	} else {
801 		err = build_single_fd(priv, skb, &fd);
802 	}
803 
804 	if (unlikely(err)) {
805 		percpu_stats->tx_dropped++;
806 		goto err_build_fd;
807 	}
808 
809 	/* Tracing point */
810 	trace_dpaa2_tx_fd(net_dev, &fd);
811 
812 	/* TxConf FQ selection relies on queue id from the stack.
813 	 * In case of a forwarded frame from another DPNI interface, we choose
814 	 * a queue affined to the same core that processed the Rx frame
815 	 */
816 	queue_mapping = skb_get_queue_mapping(skb);
817 	fq = &priv->fq[queue_mapping];
818 	for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
819 		err = priv->enqueue(priv, fq, &fd, 0);
820 		if (err != -EBUSY)
821 			break;
822 	}
823 	percpu_extras->tx_portal_busy += i;
824 	if (unlikely(err < 0)) {
825 		percpu_stats->tx_errors++;
826 		/* Clean up everything, including freeing the skb */
827 		free_tx_fd(priv, fq, &fd, false);
828 	} else {
829 		fd_len = dpaa2_fd_get_len(&fd);
830 		percpu_stats->tx_packets++;
831 		percpu_stats->tx_bytes += fd_len;
832 
833 		nq = netdev_get_tx_queue(net_dev, queue_mapping);
834 		netdev_tx_sent_queue(nq, fd_len);
835 	}
836 
837 	return NETDEV_TX_OK;
838 
839 err_build_fd:
840 err_alloc_headroom:
841 	dev_kfree_skb(skb);
842 
843 	return NETDEV_TX_OK;
844 }
845 
846 /* Tx confirmation frame processing routine */
847 static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
848 			      struct dpaa2_eth_channel *ch __always_unused,
849 			      const struct dpaa2_fd *fd,
850 			      struct dpaa2_eth_fq *fq)
851 {
852 	struct rtnl_link_stats64 *percpu_stats;
853 	struct dpaa2_eth_drv_stats *percpu_extras;
854 	u32 fd_len = dpaa2_fd_get_len(fd);
855 	u32 fd_errors;
856 
857 	/* Tracing point */
858 	trace_dpaa2_tx_conf_fd(priv->net_dev, fd);
859 
860 	percpu_extras = this_cpu_ptr(priv->percpu_extras);
861 	percpu_extras->tx_conf_frames++;
862 	percpu_extras->tx_conf_bytes += fd_len;
863 
864 	/* Check frame errors in the FD field */
865 	fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK;
866 	free_tx_fd(priv, fq, fd, true);
867 
868 	if (likely(!fd_errors))
869 		return;
870 
871 	if (net_ratelimit())
872 		netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n",
873 			   fd_errors);
874 
875 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
876 	/* Tx-conf logically pertains to the egress path. */
877 	percpu_stats->tx_errors++;
878 }
879 
880 static int set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
881 {
882 	int err;
883 
884 	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
885 			       DPNI_OFF_RX_L3_CSUM, enable);
886 	if (err) {
887 		netdev_err(priv->net_dev,
888 			   "dpni_set_offload(RX_L3_CSUM) failed\n");
889 		return err;
890 	}
891 
892 	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
893 			       DPNI_OFF_RX_L4_CSUM, enable);
894 	if (err) {
895 		netdev_err(priv->net_dev,
896 			   "dpni_set_offload(RX_L4_CSUM) failed\n");
897 		return err;
898 	}
899 
900 	return 0;
901 }
902 
903 static int set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
904 {
905 	int err;
906 
907 	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
908 			       DPNI_OFF_TX_L3_CSUM, enable);
909 	if (err) {
910 		netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
911 		return err;
912 	}
913 
914 	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
915 			       DPNI_OFF_TX_L4_CSUM, enable);
916 	if (err) {
917 		netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
918 		return err;
919 	}
920 
921 	return 0;
922 }
923 
924 /* Perform a single release command to add buffers
925  * to the specified buffer pool
926  */
927 static int add_bufs(struct dpaa2_eth_priv *priv,
928 		    struct dpaa2_eth_channel *ch, u16 bpid)
929 {
930 	struct device *dev = priv->net_dev->dev.parent;
931 	u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
932 	struct page *page;
933 	dma_addr_t addr;
934 	int i, err;
935 
936 	for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
937 		/* Allocate buffer visible to WRIOP + skb shared info +
938 		 * alignment padding
939 		 */
940 		/* allocate one page for each Rx buffer. WRIOP sees
941 		 * the entire page except for a tailroom reserved for
942 		 * skb shared info
943 		 */
944 		page = dev_alloc_pages(0);
945 		if (!page)
946 			goto err_alloc;
947 
948 		addr = dma_map_page(dev, page, 0, DPAA2_ETH_RX_BUF_SIZE,
949 				    DMA_BIDIRECTIONAL);
950 		if (unlikely(dma_mapping_error(dev, addr)))
951 			goto err_map;
952 
953 		buf_array[i] = addr;
954 
955 		/* tracing point */
956 		trace_dpaa2_eth_buf_seed(priv->net_dev,
957 					 page, DPAA2_ETH_RX_BUF_RAW_SIZE,
958 					 addr, DPAA2_ETH_RX_BUF_SIZE,
959 					 bpid);
960 	}
961 
962 release_bufs:
963 	/* In case the portal is busy, retry until successful */
964 	while ((err = dpaa2_io_service_release(ch->dpio, bpid,
965 					       buf_array, i)) == -EBUSY)
966 		cpu_relax();
967 
968 	/* If release command failed, clean up and bail out;
969 	 * not much else we can do about it
970 	 */
971 	if (err) {
972 		free_bufs(priv, buf_array, i);
973 		return 0;
974 	}
975 
976 	return i;
977 
978 err_map:
979 	__free_pages(page, 0);
980 err_alloc:
981 	/* If we managed to allocate at least some buffers,
982 	 * release them to hardware
983 	 */
984 	if (i)
985 		goto release_bufs;
986 
987 	return 0;
988 }
989 
990 static int seed_pool(struct dpaa2_eth_priv *priv, u16 bpid)
991 {
992 	int i, j;
993 	int new_count;
994 
995 	/* This is the lazy seeding of Rx buffer pools.
996 	 * dpaa2_add_bufs() is also used on the Rx hotpath and calls
997 	 * napi_alloc_frag(). The trouble with that is that it in turn ends up
998 	 * calling this_cpu_ptr(), which mandates execution in atomic context.
999 	 * Rather than splitting up the code, do a one-off preempt disable.
1000 	 */
1001 	preempt_disable();
1002 	for (j = 0; j < priv->num_channels; j++) {
1003 		for (i = 0; i < DPAA2_ETH_NUM_BUFS;
1004 		     i += DPAA2_ETH_BUFS_PER_CMD) {
1005 			new_count = add_bufs(priv, priv->channel[j], bpid);
1006 			priv->channel[j]->buf_count += new_count;
1007 
1008 			if (new_count < DPAA2_ETH_BUFS_PER_CMD) {
1009 				preempt_enable();
1010 				return -ENOMEM;
1011 			}
1012 		}
1013 	}
1014 	preempt_enable();
1015 
1016 	return 0;
1017 }
1018 
1019 /**
1020  * Drain the specified number of buffers from the DPNI's private buffer pool.
1021  * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
1022  */
1023 static void drain_bufs(struct dpaa2_eth_priv *priv, int count)
1024 {
1025 	u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
1026 	int ret;
1027 
1028 	do {
1029 		ret = dpaa2_io_service_acquire(NULL, priv->bpid,
1030 					       buf_array, count);
1031 		if (ret < 0) {
1032 			netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
1033 			return;
1034 		}
1035 		free_bufs(priv, buf_array, ret);
1036 	} while (ret);
1037 }
1038 
1039 static void drain_pool(struct dpaa2_eth_priv *priv)
1040 {
1041 	int i;
1042 
1043 	drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD);
1044 	drain_bufs(priv, 1);
1045 
1046 	for (i = 0; i < priv->num_channels; i++)
1047 		priv->channel[i]->buf_count = 0;
1048 }
1049 
1050 /* Function is called from softirq context only, so we don't need to guard
1051  * the access to percpu count
1052  */
1053 static int refill_pool(struct dpaa2_eth_priv *priv,
1054 		       struct dpaa2_eth_channel *ch,
1055 		       u16 bpid)
1056 {
1057 	int new_count;
1058 
1059 	if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
1060 		return 0;
1061 
1062 	do {
1063 		new_count = add_bufs(priv, ch, bpid);
1064 		if (unlikely(!new_count)) {
1065 			/* Out of memory; abort for now, we'll try later on */
1066 			break;
1067 		}
1068 		ch->buf_count += new_count;
1069 	} while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
1070 
1071 	if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
1072 		return -ENOMEM;
1073 
1074 	return 0;
1075 }
1076 
1077 static int pull_channel(struct dpaa2_eth_channel *ch)
1078 {
1079 	int err;
1080 	int dequeues = -1;
1081 
1082 	/* Retry while portal is busy */
1083 	do {
1084 		err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
1085 						    ch->store);
1086 		dequeues++;
1087 		cpu_relax();
1088 	} while (err == -EBUSY);
1089 
1090 	ch->stats.dequeue_portal_busy += dequeues;
1091 	if (unlikely(err))
1092 		ch->stats.pull_err++;
1093 
1094 	return err;
1095 }
1096 
1097 /* NAPI poll routine
1098  *
1099  * Frames are dequeued from the QMan channel associated with this NAPI context.
1100  * Rx, Tx confirmation and (if configured) Rx error frames all count
1101  * towards the NAPI budget.
1102  */
1103 static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
1104 {
1105 	struct dpaa2_eth_channel *ch;
1106 	struct dpaa2_eth_priv *priv;
1107 	int rx_cleaned = 0, txconf_cleaned = 0;
1108 	struct dpaa2_eth_fq *fq, *txc_fq = NULL;
1109 	struct netdev_queue *nq;
1110 	int store_cleaned, work_done;
1111 	struct list_head rx_list;
1112 	int err;
1113 
1114 	ch = container_of(napi, struct dpaa2_eth_channel, napi);
1115 	ch->xdp.res = 0;
1116 	priv = ch->priv;
1117 
1118 	INIT_LIST_HEAD(&rx_list);
1119 	ch->rx_list = &rx_list;
1120 
1121 	do {
1122 		err = pull_channel(ch);
1123 		if (unlikely(err))
1124 			break;
1125 
1126 		/* Refill pool if appropriate */
1127 		refill_pool(priv, ch, priv->bpid);
1128 
1129 		store_cleaned = consume_frames(ch, &fq);
1130 		if (!store_cleaned)
1131 			break;
1132 		if (fq->type == DPAA2_RX_FQ) {
1133 			rx_cleaned += store_cleaned;
1134 		} else {
1135 			txconf_cleaned += store_cleaned;
1136 			/* We have a single Tx conf FQ on this channel */
1137 			txc_fq = fq;
1138 		}
1139 
1140 		/* If we either consumed the whole NAPI budget with Rx frames
1141 		 * or we reached the Tx confirmations threshold, we're done.
1142 		 */
1143 		if (rx_cleaned >= budget ||
1144 		    txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
1145 			work_done = budget;
1146 			goto out;
1147 		}
1148 	} while (store_cleaned);
1149 
1150 	/* We didn't consume the entire budget, so finish napi and
1151 	 * re-enable data availability notifications
1152 	 */
1153 	napi_complete_done(napi, rx_cleaned);
1154 	do {
1155 		err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
1156 		cpu_relax();
1157 	} while (err == -EBUSY);
1158 	WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
1159 		  ch->nctx.desired_cpu);
1160 
1161 	work_done = max(rx_cleaned, 1);
1162 
1163 out:
1164 	netif_receive_skb_list(ch->rx_list);
1165 
1166 	if (txc_fq && txc_fq->dq_frames) {
1167 		nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid);
1168 		netdev_tx_completed_queue(nq, txc_fq->dq_frames,
1169 					  txc_fq->dq_bytes);
1170 		txc_fq->dq_frames = 0;
1171 		txc_fq->dq_bytes = 0;
1172 	}
1173 
1174 	if (ch->xdp.res & XDP_REDIRECT)
1175 		xdp_do_flush_map();
1176 
1177 	return work_done;
1178 }
1179 
1180 static void enable_ch_napi(struct dpaa2_eth_priv *priv)
1181 {
1182 	struct dpaa2_eth_channel *ch;
1183 	int i;
1184 
1185 	for (i = 0; i < priv->num_channels; i++) {
1186 		ch = priv->channel[i];
1187 		napi_enable(&ch->napi);
1188 	}
1189 }
1190 
1191 static void disable_ch_napi(struct dpaa2_eth_priv *priv)
1192 {
1193 	struct dpaa2_eth_channel *ch;
1194 	int i;
1195 
1196 	for (i = 0; i < priv->num_channels; i++) {
1197 		ch = priv->channel[i];
1198 		napi_disable(&ch->napi);
1199 	}
1200 }
1201 
1202 static int link_state_update(struct dpaa2_eth_priv *priv)
1203 {
1204 	struct dpni_link_state state = {0};
1205 	int err;
1206 
1207 	err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
1208 	if (unlikely(err)) {
1209 		netdev_err(priv->net_dev,
1210 			   "dpni_get_link_state() failed\n");
1211 		return err;
1212 	}
1213 
1214 	/* Chech link state; speed / duplex changes are not treated yet */
1215 	if (priv->link_state.up == state.up)
1216 		return 0;
1217 
1218 	priv->link_state = state;
1219 	if (state.up) {
1220 		netif_carrier_on(priv->net_dev);
1221 		netif_tx_start_all_queues(priv->net_dev);
1222 	} else {
1223 		netif_tx_stop_all_queues(priv->net_dev);
1224 		netif_carrier_off(priv->net_dev);
1225 	}
1226 
1227 	netdev_info(priv->net_dev, "Link Event: state %s\n",
1228 		    state.up ? "up" : "down");
1229 
1230 	return 0;
1231 }
1232 
1233 static int dpaa2_eth_open(struct net_device *net_dev)
1234 {
1235 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1236 	int err;
1237 
1238 	err = seed_pool(priv, priv->bpid);
1239 	if (err) {
1240 		/* Not much to do; the buffer pool, though not filled up,
1241 		 * may still contain some buffers which would enable us
1242 		 * to limp on.
1243 		 */
1244 		netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
1245 			   priv->dpbp_dev->obj_desc.id, priv->bpid);
1246 	}
1247 
1248 	/* We'll only start the txqs when the link is actually ready; make sure
1249 	 * we don't race against the link up notification, which may come
1250 	 * immediately after dpni_enable();
1251 	 */
1252 	netif_tx_stop_all_queues(net_dev);
1253 	enable_ch_napi(priv);
1254 	/* Also, explicitly set carrier off, otherwise netif_carrier_ok() will
1255 	 * return true and cause 'ip link show' to report the LOWER_UP flag,
1256 	 * even though the link notification wasn't even received.
1257 	 */
1258 	netif_carrier_off(net_dev);
1259 
1260 	err = dpni_enable(priv->mc_io, 0, priv->mc_token);
1261 	if (err < 0) {
1262 		netdev_err(net_dev, "dpni_enable() failed\n");
1263 		goto enable_err;
1264 	}
1265 
1266 	/* If the DPMAC object has already processed the link up interrupt,
1267 	 * we have to learn the link state ourselves.
1268 	 */
1269 	err = link_state_update(priv);
1270 	if (err < 0) {
1271 		netdev_err(net_dev, "Can't update link state\n");
1272 		goto link_state_err;
1273 	}
1274 
1275 	return 0;
1276 
1277 link_state_err:
1278 enable_err:
1279 	disable_ch_napi(priv);
1280 	drain_pool(priv);
1281 	return err;
1282 }
1283 
1284 /* Total number of in-flight frames on ingress queues */
1285 static u32 ingress_fq_count(struct dpaa2_eth_priv *priv)
1286 {
1287 	struct dpaa2_eth_fq *fq;
1288 	u32 fcnt = 0, bcnt = 0, total = 0;
1289 	int i, err;
1290 
1291 	for (i = 0; i < priv->num_fqs; i++) {
1292 		fq = &priv->fq[i];
1293 		err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
1294 		if (err) {
1295 			netdev_warn(priv->net_dev, "query_fq_count failed");
1296 			break;
1297 		}
1298 		total += fcnt;
1299 	}
1300 
1301 	return total;
1302 }
1303 
1304 static void wait_for_fq_empty(struct dpaa2_eth_priv *priv)
1305 {
1306 	int retries = 10;
1307 	u32 pending;
1308 
1309 	do {
1310 		pending = ingress_fq_count(priv);
1311 		if (pending)
1312 			msleep(100);
1313 	} while (pending && --retries);
1314 }
1315 
1316 static int dpaa2_eth_stop(struct net_device *net_dev)
1317 {
1318 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1319 	int dpni_enabled = 0;
1320 	int retries = 10;
1321 
1322 	netif_tx_stop_all_queues(net_dev);
1323 	netif_carrier_off(net_dev);
1324 
1325 	/* On dpni_disable(), the MC firmware will:
1326 	 * - stop MAC Rx and wait for all Rx frames to be enqueued to software
1327 	 * - cut off WRIOP dequeues from egress FQs and wait until transmission
1328 	 * of all in flight Tx frames is finished (and corresponding Tx conf
1329 	 * frames are enqueued back to software)
1330 	 *
1331 	 * Before calling dpni_disable(), we wait for all Tx frames to arrive
1332 	 * on WRIOP. After it finishes, wait until all remaining frames on Rx
1333 	 * and Tx conf queues are consumed on NAPI poll.
1334 	 */
1335 	msleep(500);
1336 
1337 	do {
1338 		dpni_disable(priv->mc_io, 0, priv->mc_token);
1339 		dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
1340 		if (dpni_enabled)
1341 			/* Allow the hardware some slack */
1342 			msleep(100);
1343 	} while (dpni_enabled && --retries);
1344 	if (!retries) {
1345 		netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
1346 		/* Must go on and disable NAPI nonetheless, so we don't crash at
1347 		 * the next "ifconfig up"
1348 		 */
1349 	}
1350 
1351 	wait_for_fq_empty(priv);
1352 	disable_ch_napi(priv);
1353 
1354 	/* Empty the buffer pool */
1355 	drain_pool(priv);
1356 
1357 	return 0;
1358 }
1359 
1360 static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
1361 {
1362 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1363 	struct device *dev = net_dev->dev.parent;
1364 	int err;
1365 
1366 	err = eth_mac_addr(net_dev, addr);
1367 	if (err < 0) {
1368 		dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
1369 		return err;
1370 	}
1371 
1372 	err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
1373 					net_dev->dev_addr);
1374 	if (err) {
1375 		dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
1376 		return err;
1377 	}
1378 
1379 	return 0;
1380 }
1381 
1382 /** Fill in counters maintained by the GPP driver. These may be different from
1383  * the hardware counters obtained by ethtool.
1384  */
1385 static void dpaa2_eth_get_stats(struct net_device *net_dev,
1386 				struct rtnl_link_stats64 *stats)
1387 {
1388 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1389 	struct rtnl_link_stats64 *percpu_stats;
1390 	u64 *cpustats;
1391 	u64 *netstats = (u64 *)stats;
1392 	int i, j;
1393 	int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
1394 
1395 	for_each_possible_cpu(i) {
1396 		percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
1397 		cpustats = (u64 *)percpu_stats;
1398 		for (j = 0; j < num; j++)
1399 			netstats[j] += cpustats[j];
1400 	}
1401 }
1402 
1403 /* Copy mac unicast addresses from @net_dev to @priv.
1404  * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1405  */
1406 static void add_uc_hw_addr(const struct net_device *net_dev,
1407 			   struct dpaa2_eth_priv *priv)
1408 {
1409 	struct netdev_hw_addr *ha;
1410 	int err;
1411 
1412 	netdev_for_each_uc_addr(ha, net_dev) {
1413 		err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1414 					ha->addr);
1415 		if (err)
1416 			netdev_warn(priv->net_dev,
1417 				    "Could not add ucast MAC %pM to the filtering table (err %d)\n",
1418 				    ha->addr, err);
1419 	}
1420 }
1421 
1422 /* Copy mac multicast addresses from @net_dev to @priv
1423  * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1424  */
1425 static void add_mc_hw_addr(const struct net_device *net_dev,
1426 			   struct dpaa2_eth_priv *priv)
1427 {
1428 	struct netdev_hw_addr *ha;
1429 	int err;
1430 
1431 	netdev_for_each_mc_addr(ha, net_dev) {
1432 		err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1433 					ha->addr);
1434 		if (err)
1435 			netdev_warn(priv->net_dev,
1436 				    "Could not add mcast MAC %pM to the filtering table (err %d)\n",
1437 				    ha->addr, err);
1438 	}
1439 }
1440 
1441 static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
1442 {
1443 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1444 	int uc_count = netdev_uc_count(net_dev);
1445 	int mc_count = netdev_mc_count(net_dev);
1446 	u8 max_mac = priv->dpni_attrs.mac_filter_entries;
1447 	u32 options = priv->dpni_attrs.options;
1448 	u16 mc_token = priv->mc_token;
1449 	struct fsl_mc_io *mc_io = priv->mc_io;
1450 	int err;
1451 
1452 	/* Basic sanity checks; these probably indicate a misconfiguration */
1453 	if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
1454 		netdev_info(net_dev,
1455 			    "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
1456 			    max_mac);
1457 
1458 	/* Force promiscuous if the uc or mc counts exceed our capabilities. */
1459 	if (uc_count > max_mac) {
1460 		netdev_info(net_dev,
1461 			    "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
1462 			    uc_count, max_mac);
1463 		goto force_promisc;
1464 	}
1465 	if (mc_count + uc_count > max_mac) {
1466 		netdev_info(net_dev,
1467 			    "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
1468 			    uc_count + mc_count, max_mac);
1469 		goto force_mc_promisc;
1470 	}
1471 
1472 	/* Adjust promisc settings due to flag combinations */
1473 	if (net_dev->flags & IFF_PROMISC)
1474 		goto force_promisc;
1475 	if (net_dev->flags & IFF_ALLMULTI) {
1476 		/* First, rebuild unicast filtering table. This should be done
1477 		 * in promisc mode, in order to avoid frame loss while we
1478 		 * progressively add entries to the table.
1479 		 * We don't know whether we had been in promisc already, and
1480 		 * making an MC call to find out is expensive; so set uc promisc
1481 		 * nonetheless.
1482 		 */
1483 		err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1484 		if (err)
1485 			netdev_warn(net_dev, "Can't set uc promisc\n");
1486 
1487 		/* Actual uc table reconstruction. */
1488 		err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
1489 		if (err)
1490 			netdev_warn(net_dev, "Can't clear uc filters\n");
1491 		add_uc_hw_addr(net_dev, priv);
1492 
1493 		/* Finally, clear uc promisc and set mc promisc as requested. */
1494 		err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1495 		if (err)
1496 			netdev_warn(net_dev, "Can't clear uc promisc\n");
1497 		goto force_mc_promisc;
1498 	}
1499 
1500 	/* Neither unicast, nor multicast promisc will be on... eventually.
1501 	 * For now, rebuild mac filtering tables while forcing both of them on.
1502 	 */
1503 	err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1504 	if (err)
1505 		netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
1506 	err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1507 	if (err)
1508 		netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
1509 
1510 	/* Actual mac filtering tables reconstruction */
1511 	err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
1512 	if (err)
1513 		netdev_warn(net_dev, "Can't clear mac filters\n");
1514 	add_mc_hw_addr(net_dev, priv);
1515 	add_uc_hw_addr(net_dev, priv);
1516 
1517 	/* Now we can clear both ucast and mcast promisc, without risking
1518 	 * to drop legitimate frames anymore.
1519 	 */
1520 	err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
1521 	if (err)
1522 		netdev_warn(net_dev, "Can't clear ucast promisc\n");
1523 	err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
1524 	if (err)
1525 		netdev_warn(net_dev, "Can't clear mcast promisc\n");
1526 
1527 	return;
1528 
1529 force_promisc:
1530 	err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
1531 	if (err)
1532 		netdev_warn(net_dev, "Can't set ucast promisc\n");
1533 force_mc_promisc:
1534 	err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
1535 	if (err)
1536 		netdev_warn(net_dev, "Can't set mcast promisc\n");
1537 }
1538 
1539 static int dpaa2_eth_set_features(struct net_device *net_dev,
1540 				  netdev_features_t features)
1541 {
1542 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1543 	netdev_features_t changed = features ^ net_dev->features;
1544 	bool enable;
1545 	int err;
1546 
1547 	if (changed & NETIF_F_RXCSUM) {
1548 		enable = !!(features & NETIF_F_RXCSUM);
1549 		err = set_rx_csum(priv, enable);
1550 		if (err)
1551 			return err;
1552 	}
1553 
1554 	if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
1555 		enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
1556 		err = set_tx_csum(priv, enable);
1557 		if (err)
1558 			return err;
1559 	}
1560 
1561 	return 0;
1562 }
1563 
1564 static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1565 {
1566 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
1567 	struct hwtstamp_config config;
1568 
1569 	if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
1570 		return -EFAULT;
1571 
1572 	switch (config.tx_type) {
1573 	case HWTSTAMP_TX_OFF:
1574 		priv->tx_tstamp = false;
1575 		break;
1576 	case HWTSTAMP_TX_ON:
1577 		priv->tx_tstamp = true;
1578 		break;
1579 	default:
1580 		return -ERANGE;
1581 	}
1582 
1583 	if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
1584 		priv->rx_tstamp = false;
1585 	} else {
1586 		priv->rx_tstamp = true;
1587 		/* TS is set for all frame types, not only those requested */
1588 		config.rx_filter = HWTSTAMP_FILTER_ALL;
1589 	}
1590 
1591 	return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
1592 			-EFAULT : 0;
1593 }
1594 
1595 static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1596 {
1597 	if (cmd == SIOCSHWTSTAMP)
1598 		return dpaa2_eth_ts_ioctl(dev, rq, cmd);
1599 
1600 	return -EINVAL;
1601 }
1602 
1603 static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu)
1604 {
1605 	int mfl, linear_mfl;
1606 
1607 	mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
1608 	linear_mfl = DPAA2_ETH_RX_BUF_SIZE - DPAA2_ETH_RX_HWA_SIZE -
1609 		     dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM;
1610 
1611 	if (mfl > linear_mfl) {
1612 		netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
1613 			    linear_mfl - VLAN_ETH_HLEN);
1614 		return false;
1615 	}
1616 
1617 	return true;
1618 }
1619 
1620 static int set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
1621 {
1622 	int mfl, err;
1623 
1624 	/* We enforce a maximum Rx frame length based on MTU only if we have
1625 	 * an XDP program attached (in order to avoid Rx S/G frames).
1626 	 * Otherwise, we accept all incoming frames as long as they are not
1627 	 * larger than maximum size supported in hardware
1628 	 */
1629 	if (has_xdp)
1630 		mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
1631 	else
1632 		mfl = DPAA2_ETH_MFL;
1633 
1634 	err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl);
1635 	if (err) {
1636 		netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n");
1637 		return err;
1638 	}
1639 
1640 	return 0;
1641 }
1642 
1643 static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu)
1644 {
1645 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
1646 	int err;
1647 
1648 	if (!priv->xdp_prog)
1649 		goto out;
1650 
1651 	if (!xdp_mtu_valid(priv, new_mtu))
1652 		return -EINVAL;
1653 
1654 	err = set_rx_mfl(priv, new_mtu, true);
1655 	if (err)
1656 		return err;
1657 
1658 out:
1659 	dev->mtu = new_mtu;
1660 	return 0;
1661 }
1662 
1663 static int update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp)
1664 {
1665 	struct dpni_buffer_layout buf_layout = {0};
1666 	int err;
1667 
1668 	err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token,
1669 				     DPNI_QUEUE_RX, &buf_layout);
1670 	if (err) {
1671 		netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n");
1672 		return err;
1673 	}
1674 
1675 	/* Reserve extra headroom for XDP header size changes */
1676 	buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) +
1677 				    (has_xdp ? XDP_PACKET_HEADROOM : 0);
1678 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM;
1679 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
1680 				     DPNI_QUEUE_RX, &buf_layout);
1681 	if (err) {
1682 		netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n");
1683 		return err;
1684 	}
1685 
1686 	return 0;
1687 }
1688 
1689 static int setup_xdp(struct net_device *dev, struct bpf_prog *prog)
1690 {
1691 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
1692 	struct dpaa2_eth_channel *ch;
1693 	struct bpf_prog *old;
1694 	bool up, need_update;
1695 	int i, err;
1696 
1697 	if (prog && !xdp_mtu_valid(priv, dev->mtu))
1698 		return -EINVAL;
1699 
1700 	if (prog) {
1701 		prog = bpf_prog_add(prog, priv->num_channels);
1702 		if (IS_ERR(prog))
1703 			return PTR_ERR(prog);
1704 	}
1705 
1706 	up = netif_running(dev);
1707 	need_update = (!!priv->xdp_prog != !!prog);
1708 
1709 	if (up)
1710 		dpaa2_eth_stop(dev);
1711 
1712 	/* While in xdp mode, enforce a maximum Rx frame size based on MTU.
1713 	 * Also, when switching between xdp/non-xdp modes we need to reconfigure
1714 	 * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop,
1715 	 * so we are sure no old format buffers will be used from now on.
1716 	 */
1717 	if (need_update) {
1718 		err = set_rx_mfl(priv, dev->mtu, !!prog);
1719 		if (err)
1720 			goto out_err;
1721 		err = update_rx_buffer_headroom(priv, !!prog);
1722 		if (err)
1723 			goto out_err;
1724 	}
1725 
1726 	old = xchg(&priv->xdp_prog, prog);
1727 	if (old)
1728 		bpf_prog_put(old);
1729 
1730 	for (i = 0; i < priv->num_channels; i++) {
1731 		ch = priv->channel[i];
1732 		old = xchg(&ch->xdp.prog, prog);
1733 		if (old)
1734 			bpf_prog_put(old);
1735 	}
1736 
1737 	if (up) {
1738 		err = dpaa2_eth_open(dev);
1739 		if (err)
1740 			return err;
1741 	}
1742 
1743 	return 0;
1744 
1745 out_err:
1746 	if (prog)
1747 		bpf_prog_sub(prog, priv->num_channels);
1748 	if (up)
1749 		dpaa2_eth_open(dev);
1750 
1751 	return err;
1752 }
1753 
1754 static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
1755 {
1756 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
1757 
1758 	switch (xdp->command) {
1759 	case XDP_SETUP_PROG:
1760 		return setup_xdp(dev, xdp->prog);
1761 	case XDP_QUERY_PROG:
1762 		xdp->prog_id = priv->xdp_prog ? priv->xdp_prog->aux->id : 0;
1763 		break;
1764 	default:
1765 		return -EINVAL;
1766 	}
1767 
1768 	return 0;
1769 }
1770 
1771 static int dpaa2_eth_xdp_xmit_frame(struct net_device *net_dev,
1772 				    struct xdp_frame *xdpf)
1773 {
1774 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1775 	struct device *dev = net_dev->dev.parent;
1776 	struct rtnl_link_stats64 *percpu_stats;
1777 	struct dpaa2_eth_drv_stats *percpu_extras;
1778 	unsigned int needed_headroom;
1779 	struct dpaa2_eth_swa *swa;
1780 	struct dpaa2_eth_fq *fq;
1781 	struct dpaa2_fd fd;
1782 	void *buffer_start, *aligned_start;
1783 	dma_addr_t addr;
1784 	int err, i;
1785 
1786 	/* We require a minimum headroom to be able to transmit the frame.
1787 	 * Otherwise return an error and let the original net_device handle it
1788 	 */
1789 	needed_headroom = dpaa2_eth_needed_headroom(priv, NULL);
1790 	if (xdpf->headroom < needed_headroom)
1791 		return -EINVAL;
1792 
1793 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
1794 	percpu_extras = this_cpu_ptr(priv->percpu_extras);
1795 
1796 	/* Setup the FD fields */
1797 	memset(&fd, 0, sizeof(fd));
1798 
1799 	/* Align FD address, if possible */
1800 	buffer_start = xdpf->data - needed_headroom;
1801 	aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
1802 				  DPAA2_ETH_TX_BUF_ALIGN);
1803 	if (aligned_start >= xdpf->data - xdpf->headroom)
1804 		buffer_start = aligned_start;
1805 
1806 	swa = (struct dpaa2_eth_swa *)buffer_start;
1807 	/* fill in necessary fields here */
1808 	swa->type = DPAA2_ETH_SWA_XDP;
1809 	swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start;
1810 	swa->xdp.xdpf = xdpf;
1811 
1812 	addr = dma_map_single(dev, buffer_start,
1813 			      swa->xdp.dma_size,
1814 			      DMA_BIDIRECTIONAL);
1815 	if (unlikely(dma_mapping_error(dev, addr))) {
1816 		percpu_stats->tx_dropped++;
1817 		return -ENOMEM;
1818 	}
1819 
1820 	dpaa2_fd_set_addr(&fd, addr);
1821 	dpaa2_fd_set_offset(&fd, xdpf->data - buffer_start);
1822 	dpaa2_fd_set_len(&fd, xdpf->len);
1823 	dpaa2_fd_set_format(&fd, dpaa2_fd_single);
1824 	dpaa2_fd_set_ctrl(&fd, FD_CTRL_PTA);
1825 
1826 	fq = &priv->fq[smp_processor_id()];
1827 	for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
1828 		err = priv->enqueue(priv, fq, &fd, 0);
1829 		if (err != -EBUSY)
1830 			break;
1831 	}
1832 	percpu_extras->tx_portal_busy += i;
1833 	if (unlikely(err < 0)) {
1834 		percpu_stats->tx_errors++;
1835 		/* let the Rx device handle the cleanup */
1836 		return err;
1837 	}
1838 
1839 	percpu_stats->tx_packets++;
1840 	percpu_stats->tx_bytes += dpaa2_fd_get_len(&fd);
1841 
1842 	return 0;
1843 }
1844 
1845 static int dpaa2_eth_xdp_xmit(struct net_device *net_dev, int n,
1846 			      struct xdp_frame **frames, u32 flags)
1847 {
1848 	int drops = 0;
1849 	int i, err;
1850 
1851 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
1852 		return -EINVAL;
1853 
1854 	if (!netif_running(net_dev))
1855 		return -ENETDOWN;
1856 
1857 	for (i = 0; i < n; i++) {
1858 		struct xdp_frame *xdpf = frames[i];
1859 
1860 		err = dpaa2_eth_xdp_xmit_frame(net_dev, xdpf);
1861 		if (err) {
1862 			xdp_return_frame_rx_napi(xdpf);
1863 			drops++;
1864 		}
1865 	}
1866 
1867 	return n - drops;
1868 }
1869 
1870 static const struct net_device_ops dpaa2_eth_ops = {
1871 	.ndo_open = dpaa2_eth_open,
1872 	.ndo_start_xmit = dpaa2_eth_tx,
1873 	.ndo_stop = dpaa2_eth_stop,
1874 	.ndo_set_mac_address = dpaa2_eth_set_addr,
1875 	.ndo_get_stats64 = dpaa2_eth_get_stats,
1876 	.ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
1877 	.ndo_set_features = dpaa2_eth_set_features,
1878 	.ndo_do_ioctl = dpaa2_eth_ioctl,
1879 	.ndo_change_mtu = dpaa2_eth_change_mtu,
1880 	.ndo_bpf = dpaa2_eth_xdp,
1881 	.ndo_xdp_xmit = dpaa2_eth_xdp_xmit,
1882 };
1883 
1884 static void cdan_cb(struct dpaa2_io_notification_ctx *ctx)
1885 {
1886 	struct dpaa2_eth_channel *ch;
1887 
1888 	ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
1889 
1890 	/* Update NAPI statistics */
1891 	ch->stats.cdan++;
1892 
1893 	napi_schedule_irqoff(&ch->napi);
1894 }
1895 
1896 /* Allocate and configure a DPCON object */
1897 static struct fsl_mc_device *setup_dpcon(struct dpaa2_eth_priv *priv)
1898 {
1899 	struct fsl_mc_device *dpcon;
1900 	struct device *dev = priv->net_dev->dev.parent;
1901 	struct dpcon_attr attrs;
1902 	int err;
1903 
1904 	err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
1905 				     FSL_MC_POOL_DPCON, &dpcon);
1906 	if (err) {
1907 		if (err == -ENXIO)
1908 			err = -EPROBE_DEFER;
1909 		else
1910 			dev_info(dev, "Not enough DPCONs, will go on as-is\n");
1911 		return ERR_PTR(err);
1912 	}
1913 
1914 	err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
1915 	if (err) {
1916 		dev_err(dev, "dpcon_open() failed\n");
1917 		goto free;
1918 	}
1919 
1920 	err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
1921 	if (err) {
1922 		dev_err(dev, "dpcon_reset() failed\n");
1923 		goto close;
1924 	}
1925 
1926 	err = dpcon_get_attributes(priv->mc_io, 0, dpcon->mc_handle, &attrs);
1927 	if (err) {
1928 		dev_err(dev, "dpcon_get_attributes() failed\n");
1929 		goto close;
1930 	}
1931 
1932 	err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
1933 	if (err) {
1934 		dev_err(dev, "dpcon_enable() failed\n");
1935 		goto close;
1936 	}
1937 
1938 	return dpcon;
1939 
1940 close:
1941 	dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
1942 free:
1943 	fsl_mc_object_free(dpcon);
1944 
1945 	return NULL;
1946 }
1947 
1948 static void free_dpcon(struct dpaa2_eth_priv *priv,
1949 		       struct fsl_mc_device *dpcon)
1950 {
1951 	dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
1952 	dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
1953 	fsl_mc_object_free(dpcon);
1954 }
1955 
1956 static struct dpaa2_eth_channel *
1957 alloc_channel(struct dpaa2_eth_priv *priv)
1958 {
1959 	struct dpaa2_eth_channel *channel;
1960 	struct dpcon_attr attr;
1961 	struct device *dev = priv->net_dev->dev.parent;
1962 	int err;
1963 
1964 	channel = kzalloc(sizeof(*channel), GFP_KERNEL);
1965 	if (!channel)
1966 		return NULL;
1967 
1968 	channel->dpcon = setup_dpcon(priv);
1969 	if (IS_ERR_OR_NULL(channel->dpcon)) {
1970 		err = PTR_ERR(channel->dpcon);
1971 		goto err_setup;
1972 	}
1973 
1974 	err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
1975 				   &attr);
1976 	if (err) {
1977 		dev_err(dev, "dpcon_get_attributes() failed\n");
1978 		goto err_get_attr;
1979 	}
1980 
1981 	channel->dpcon_id = attr.id;
1982 	channel->ch_id = attr.qbman_ch_id;
1983 	channel->priv = priv;
1984 
1985 	return channel;
1986 
1987 err_get_attr:
1988 	free_dpcon(priv, channel->dpcon);
1989 err_setup:
1990 	kfree(channel);
1991 	return ERR_PTR(err);
1992 }
1993 
1994 static void free_channel(struct dpaa2_eth_priv *priv,
1995 			 struct dpaa2_eth_channel *channel)
1996 {
1997 	free_dpcon(priv, channel->dpcon);
1998 	kfree(channel);
1999 }
2000 
2001 /* DPIO setup: allocate and configure QBMan channels, setup core affinity
2002  * and register data availability notifications
2003  */
2004 static int setup_dpio(struct dpaa2_eth_priv *priv)
2005 {
2006 	struct dpaa2_io_notification_ctx *nctx;
2007 	struct dpaa2_eth_channel *channel;
2008 	struct dpcon_notification_cfg dpcon_notif_cfg;
2009 	struct device *dev = priv->net_dev->dev.parent;
2010 	int i, err;
2011 
2012 	/* We want the ability to spread ingress traffic (RX, TX conf) to as
2013 	 * many cores as possible, so we need one channel for each core
2014 	 * (unless there's fewer queues than cores, in which case the extra
2015 	 * channels would be wasted).
2016 	 * Allocate one channel per core and register it to the core's
2017 	 * affine DPIO. If not enough channels are available for all cores
2018 	 * or if some cores don't have an affine DPIO, there will be no
2019 	 * ingress frame processing on those cores.
2020 	 */
2021 	cpumask_clear(&priv->dpio_cpumask);
2022 	for_each_online_cpu(i) {
2023 		/* Try to allocate a channel */
2024 		channel = alloc_channel(priv);
2025 		if (IS_ERR_OR_NULL(channel)) {
2026 			err = PTR_ERR(channel);
2027 			if (err != -EPROBE_DEFER)
2028 				dev_info(dev,
2029 					 "No affine channel for cpu %d and above\n", i);
2030 			goto err_alloc_ch;
2031 		}
2032 
2033 		priv->channel[priv->num_channels] = channel;
2034 
2035 		nctx = &channel->nctx;
2036 		nctx->is_cdan = 1;
2037 		nctx->cb = cdan_cb;
2038 		nctx->id = channel->ch_id;
2039 		nctx->desired_cpu = i;
2040 
2041 		/* Register the new context */
2042 		channel->dpio = dpaa2_io_service_select(i);
2043 		err = dpaa2_io_service_register(channel->dpio, nctx, dev);
2044 		if (err) {
2045 			dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
2046 			/* If no affine DPIO for this core, there's probably
2047 			 * none available for next cores either. Signal we want
2048 			 * to retry later, in case the DPIO devices weren't
2049 			 * probed yet.
2050 			 */
2051 			err = -EPROBE_DEFER;
2052 			goto err_service_reg;
2053 		}
2054 
2055 		/* Register DPCON notification with MC */
2056 		dpcon_notif_cfg.dpio_id = nctx->dpio_id;
2057 		dpcon_notif_cfg.priority = 0;
2058 		dpcon_notif_cfg.user_ctx = nctx->qman64;
2059 		err = dpcon_set_notification(priv->mc_io, 0,
2060 					     channel->dpcon->mc_handle,
2061 					     &dpcon_notif_cfg);
2062 		if (err) {
2063 			dev_err(dev, "dpcon_set_notification failed()\n");
2064 			goto err_set_cdan;
2065 		}
2066 
2067 		/* If we managed to allocate a channel and also found an affine
2068 		 * DPIO for this core, add it to the final mask
2069 		 */
2070 		cpumask_set_cpu(i, &priv->dpio_cpumask);
2071 		priv->num_channels++;
2072 
2073 		/* Stop if we already have enough channels to accommodate all
2074 		 * RX and TX conf queues
2075 		 */
2076 		if (priv->num_channels == priv->dpni_attrs.num_queues)
2077 			break;
2078 	}
2079 
2080 	return 0;
2081 
2082 err_set_cdan:
2083 	dpaa2_io_service_deregister(channel->dpio, nctx, dev);
2084 err_service_reg:
2085 	free_channel(priv, channel);
2086 err_alloc_ch:
2087 	if (err == -EPROBE_DEFER)
2088 		return err;
2089 
2090 	if (cpumask_empty(&priv->dpio_cpumask)) {
2091 		dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
2092 		return -ENODEV;
2093 	}
2094 
2095 	dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
2096 		 cpumask_pr_args(&priv->dpio_cpumask));
2097 
2098 	return 0;
2099 }
2100 
2101 static void free_dpio(struct dpaa2_eth_priv *priv)
2102 {
2103 	struct device *dev = priv->net_dev->dev.parent;
2104 	struct dpaa2_eth_channel *ch;
2105 	int i;
2106 
2107 	/* deregister CDAN notifications and free channels */
2108 	for (i = 0; i < priv->num_channels; i++) {
2109 		ch = priv->channel[i];
2110 		dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev);
2111 		free_channel(priv, ch);
2112 	}
2113 }
2114 
2115 static struct dpaa2_eth_channel *get_affine_channel(struct dpaa2_eth_priv *priv,
2116 						    int cpu)
2117 {
2118 	struct device *dev = priv->net_dev->dev.parent;
2119 	int i;
2120 
2121 	for (i = 0; i < priv->num_channels; i++)
2122 		if (priv->channel[i]->nctx.desired_cpu == cpu)
2123 			return priv->channel[i];
2124 
2125 	/* We should never get here. Issue a warning and return
2126 	 * the first channel, because it's still better than nothing
2127 	 */
2128 	dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
2129 
2130 	return priv->channel[0];
2131 }
2132 
2133 static void set_fq_affinity(struct dpaa2_eth_priv *priv)
2134 {
2135 	struct device *dev = priv->net_dev->dev.parent;
2136 	struct cpumask xps_mask;
2137 	struct dpaa2_eth_fq *fq;
2138 	int rx_cpu, txc_cpu;
2139 	int i, err;
2140 
2141 	/* For each FQ, pick one channel/CPU to deliver frames to.
2142 	 * This may well change at runtime, either through irqbalance or
2143 	 * through direct user intervention.
2144 	 */
2145 	rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
2146 
2147 	for (i = 0; i < priv->num_fqs; i++) {
2148 		fq = &priv->fq[i];
2149 		switch (fq->type) {
2150 		case DPAA2_RX_FQ:
2151 			fq->target_cpu = rx_cpu;
2152 			rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
2153 			if (rx_cpu >= nr_cpu_ids)
2154 				rx_cpu = cpumask_first(&priv->dpio_cpumask);
2155 			break;
2156 		case DPAA2_TX_CONF_FQ:
2157 			fq->target_cpu = txc_cpu;
2158 
2159 			/* Tell the stack to affine to txc_cpu the Tx queue
2160 			 * associated with the confirmation one
2161 			 */
2162 			cpumask_clear(&xps_mask);
2163 			cpumask_set_cpu(txc_cpu, &xps_mask);
2164 			err = netif_set_xps_queue(priv->net_dev, &xps_mask,
2165 						  fq->flowid);
2166 			if (err)
2167 				dev_err(dev, "Error setting XPS queue\n");
2168 
2169 			txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
2170 			if (txc_cpu >= nr_cpu_ids)
2171 				txc_cpu = cpumask_first(&priv->dpio_cpumask);
2172 			break;
2173 		default:
2174 			dev_err(dev, "Unknown FQ type: %d\n", fq->type);
2175 		}
2176 		fq->channel = get_affine_channel(priv, fq->target_cpu);
2177 	}
2178 }
2179 
2180 static void setup_fqs(struct dpaa2_eth_priv *priv)
2181 {
2182 	int i;
2183 
2184 	/* We have one TxConf FQ per Tx flow.
2185 	 * The number of Tx and Rx queues is the same.
2186 	 * Tx queues come first in the fq array.
2187 	 */
2188 	for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2189 		priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
2190 		priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
2191 		priv->fq[priv->num_fqs++].flowid = (u16)i;
2192 	}
2193 
2194 	for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2195 		priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
2196 		priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
2197 		priv->fq[priv->num_fqs++].flowid = (u16)i;
2198 	}
2199 
2200 	/* For each FQ, decide on which core to process incoming frames */
2201 	set_fq_affinity(priv);
2202 }
2203 
2204 /* Allocate and configure one buffer pool for each interface */
2205 static int setup_dpbp(struct dpaa2_eth_priv *priv)
2206 {
2207 	int err;
2208 	struct fsl_mc_device *dpbp_dev;
2209 	struct device *dev = priv->net_dev->dev.parent;
2210 	struct dpbp_attr dpbp_attrs;
2211 
2212 	err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
2213 				     &dpbp_dev);
2214 	if (err) {
2215 		if (err == -ENXIO)
2216 			err = -EPROBE_DEFER;
2217 		else
2218 			dev_err(dev, "DPBP device allocation failed\n");
2219 		return err;
2220 	}
2221 
2222 	priv->dpbp_dev = dpbp_dev;
2223 
2224 	err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id,
2225 			&dpbp_dev->mc_handle);
2226 	if (err) {
2227 		dev_err(dev, "dpbp_open() failed\n");
2228 		goto err_open;
2229 	}
2230 
2231 	err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
2232 	if (err) {
2233 		dev_err(dev, "dpbp_reset() failed\n");
2234 		goto err_reset;
2235 	}
2236 
2237 	err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
2238 	if (err) {
2239 		dev_err(dev, "dpbp_enable() failed\n");
2240 		goto err_enable;
2241 	}
2242 
2243 	err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
2244 				  &dpbp_attrs);
2245 	if (err) {
2246 		dev_err(dev, "dpbp_get_attributes() failed\n");
2247 		goto err_get_attr;
2248 	}
2249 	priv->bpid = dpbp_attrs.bpid;
2250 
2251 	return 0;
2252 
2253 err_get_attr:
2254 	dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
2255 err_enable:
2256 err_reset:
2257 	dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
2258 err_open:
2259 	fsl_mc_object_free(dpbp_dev);
2260 
2261 	return err;
2262 }
2263 
2264 static void free_dpbp(struct dpaa2_eth_priv *priv)
2265 {
2266 	drain_pool(priv);
2267 	dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2268 	dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2269 	fsl_mc_object_free(priv->dpbp_dev);
2270 }
2271 
2272 static int set_buffer_layout(struct dpaa2_eth_priv *priv)
2273 {
2274 	struct device *dev = priv->net_dev->dev.parent;
2275 	struct dpni_buffer_layout buf_layout = {0};
2276 	u16 rx_buf_align;
2277 	int err;
2278 
2279 	/* We need to check for WRIOP version 1.0.0, but depending on the MC
2280 	 * version, this number is not always provided correctly on rev1.
2281 	 * We need to check for both alternatives in this situation.
2282 	 */
2283 	if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
2284 	    priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
2285 		rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
2286 	else
2287 		rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
2288 
2289 	/* tx buffer */
2290 	buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
2291 	buf_layout.pass_timestamp = true;
2292 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
2293 			     DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
2294 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2295 				     DPNI_QUEUE_TX, &buf_layout);
2296 	if (err) {
2297 		dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
2298 		return err;
2299 	}
2300 
2301 	/* tx-confirm buffer */
2302 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
2303 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2304 				     DPNI_QUEUE_TX_CONFIRM, &buf_layout);
2305 	if (err) {
2306 		dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
2307 		return err;
2308 	}
2309 
2310 	/* Now that we've set our tx buffer layout, retrieve the minimum
2311 	 * required tx data offset.
2312 	 */
2313 	err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
2314 				      &priv->tx_data_offset);
2315 	if (err) {
2316 		dev_err(dev, "dpni_get_tx_data_offset() failed\n");
2317 		return err;
2318 	}
2319 
2320 	if ((priv->tx_data_offset % 64) != 0)
2321 		dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
2322 			 priv->tx_data_offset);
2323 
2324 	/* rx buffer */
2325 	buf_layout.pass_frame_status = true;
2326 	buf_layout.pass_parser_result = true;
2327 	buf_layout.data_align = rx_buf_align;
2328 	buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
2329 	buf_layout.private_data_size = 0;
2330 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
2331 			     DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
2332 			     DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
2333 			     DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
2334 			     DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
2335 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2336 				     DPNI_QUEUE_RX, &buf_layout);
2337 	if (err) {
2338 		dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
2339 		return err;
2340 	}
2341 
2342 	return 0;
2343 }
2344 
2345 #define DPNI_ENQUEUE_FQID_VER_MAJOR	7
2346 #define DPNI_ENQUEUE_FQID_VER_MINOR	9
2347 
2348 static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv,
2349 				       struct dpaa2_eth_fq *fq,
2350 				       struct dpaa2_fd *fd, u8 prio)
2351 {
2352 	return dpaa2_io_service_enqueue_qd(fq->channel->dpio,
2353 					   priv->tx_qdid, prio,
2354 					   fq->tx_qdbin, fd);
2355 }
2356 
2357 static inline int dpaa2_eth_enqueue_fq(struct dpaa2_eth_priv *priv,
2358 				       struct dpaa2_eth_fq *fq,
2359 				       struct dpaa2_fd *fd,
2360 				       u8 prio __always_unused)
2361 {
2362 	return dpaa2_io_service_enqueue_fq(fq->channel->dpio,
2363 					   fq->tx_fqid, fd);
2364 }
2365 
2366 static void set_enqueue_mode(struct dpaa2_eth_priv *priv)
2367 {
2368 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
2369 				   DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
2370 		priv->enqueue = dpaa2_eth_enqueue_qd;
2371 	else
2372 		priv->enqueue = dpaa2_eth_enqueue_fq;
2373 }
2374 
2375 /* Configure the DPNI object this interface is associated with */
2376 static int setup_dpni(struct fsl_mc_device *ls_dev)
2377 {
2378 	struct device *dev = &ls_dev->dev;
2379 	struct dpaa2_eth_priv *priv;
2380 	struct net_device *net_dev;
2381 	int err;
2382 
2383 	net_dev = dev_get_drvdata(dev);
2384 	priv = netdev_priv(net_dev);
2385 
2386 	/* get a handle for the DPNI object */
2387 	err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
2388 	if (err) {
2389 		dev_err(dev, "dpni_open() failed\n");
2390 		return err;
2391 	}
2392 
2393 	/* Check if we can work with this DPNI object */
2394 	err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
2395 				   &priv->dpni_ver_minor);
2396 	if (err) {
2397 		dev_err(dev, "dpni_get_api_version() failed\n");
2398 		goto close;
2399 	}
2400 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
2401 		dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
2402 			priv->dpni_ver_major, priv->dpni_ver_minor,
2403 			DPNI_VER_MAJOR, DPNI_VER_MINOR);
2404 		err = -ENOTSUPP;
2405 		goto close;
2406 	}
2407 
2408 	ls_dev->mc_io = priv->mc_io;
2409 	ls_dev->mc_handle = priv->mc_token;
2410 
2411 	err = dpni_reset(priv->mc_io, 0, priv->mc_token);
2412 	if (err) {
2413 		dev_err(dev, "dpni_reset() failed\n");
2414 		goto close;
2415 	}
2416 
2417 	err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
2418 				  &priv->dpni_attrs);
2419 	if (err) {
2420 		dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
2421 		goto close;
2422 	}
2423 
2424 	err = set_buffer_layout(priv);
2425 	if (err)
2426 		goto close;
2427 
2428 	set_enqueue_mode(priv);
2429 
2430 	priv->cls_rules = devm_kzalloc(dev, sizeof(struct dpaa2_eth_cls_rule) *
2431 				       dpaa2_eth_fs_count(priv), GFP_KERNEL);
2432 	if (!priv->cls_rules)
2433 		goto close;
2434 
2435 	return 0;
2436 
2437 close:
2438 	dpni_close(priv->mc_io, 0, priv->mc_token);
2439 
2440 	return err;
2441 }
2442 
2443 static void free_dpni(struct dpaa2_eth_priv *priv)
2444 {
2445 	int err;
2446 
2447 	err = dpni_reset(priv->mc_io, 0, priv->mc_token);
2448 	if (err)
2449 		netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
2450 			    err);
2451 
2452 	dpni_close(priv->mc_io, 0, priv->mc_token);
2453 }
2454 
2455 static int setup_rx_flow(struct dpaa2_eth_priv *priv,
2456 			 struct dpaa2_eth_fq *fq)
2457 {
2458 	struct device *dev = priv->net_dev->dev.parent;
2459 	struct dpni_queue queue;
2460 	struct dpni_queue_id qid;
2461 	struct dpni_taildrop td;
2462 	int err;
2463 
2464 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2465 			     DPNI_QUEUE_RX, 0, fq->flowid, &queue, &qid);
2466 	if (err) {
2467 		dev_err(dev, "dpni_get_queue(RX) failed\n");
2468 		return err;
2469 	}
2470 
2471 	fq->fqid = qid.fqid;
2472 
2473 	queue.destination.id = fq->channel->dpcon_id;
2474 	queue.destination.type = DPNI_DEST_DPCON;
2475 	queue.destination.priority = 1;
2476 	queue.user_context = (u64)(uintptr_t)fq;
2477 	queue.flc.stash_control = 1;
2478 	queue.flc.value &= 0xFFFFFFFFFFFFFFC0;
2479 	/* 01 01 00 - data, annotation, flow context */
2480 	queue.flc.value |= 0x14;
2481 	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
2482 			     DPNI_QUEUE_RX, 0, fq->flowid,
2483 			     DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST |
2484 			     DPNI_QUEUE_OPT_FLC,
2485 			     &queue);
2486 	if (err) {
2487 		dev_err(dev, "dpni_set_queue(RX) failed\n");
2488 		return err;
2489 	}
2490 
2491 	td.enable = 1;
2492 	td.threshold = DPAA2_ETH_TAILDROP_THRESH;
2493 	err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token, DPNI_CP_QUEUE,
2494 				DPNI_QUEUE_RX, 0, fq->flowid, &td);
2495 	if (err) {
2496 		dev_err(dev, "dpni_set_threshold() failed\n");
2497 		return err;
2498 	}
2499 
2500 	/* xdp_rxq setup */
2501 	err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev,
2502 			       fq->flowid);
2503 	if (err) {
2504 		dev_err(dev, "xdp_rxq_info_reg failed\n");
2505 		return err;
2506 	}
2507 
2508 	err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq,
2509 					 MEM_TYPE_PAGE_ORDER0, NULL);
2510 	if (err) {
2511 		dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n");
2512 		return err;
2513 	}
2514 
2515 	return 0;
2516 }
2517 
2518 static int setup_tx_flow(struct dpaa2_eth_priv *priv,
2519 			 struct dpaa2_eth_fq *fq)
2520 {
2521 	struct device *dev = priv->net_dev->dev.parent;
2522 	struct dpni_queue queue;
2523 	struct dpni_queue_id qid;
2524 	int err;
2525 
2526 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2527 			     DPNI_QUEUE_TX, 0, fq->flowid, &queue, &qid);
2528 	if (err) {
2529 		dev_err(dev, "dpni_get_queue(TX) failed\n");
2530 		return err;
2531 	}
2532 
2533 	fq->tx_qdbin = qid.qdbin;
2534 	fq->tx_fqid = qid.fqid;
2535 
2536 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2537 			     DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
2538 			     &queue, &qid);
2539 	if (err) {
2540 		dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
2541 		return err;
2542 	}
2543 
2544 	fq->fqid = qid.fqid;
2545 
2546 	queue.destination.id = fq->channel->dpcon_id;
2547 	queue.destination.type = DPNI_DEST_DPCON;
2548 	queue.destination.priority = 0;
2549 	queue.user_context = (u64)(uintptr_t)fq;
2550 	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
2551 			     DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
2552 			     DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
2553 			     &queue);
2554 	if (err) {
2555 		dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
2556 		return err;
2557 	}
2558 
2559 	return 0;
2560 }
2561 
2562 /* Supported header fields for Rx hash distribution key */
2563 static const struct dpaa2_eth_dist_fields dist_fields[] = {
2564 	{
2565 		/* L2 header */
2566 		.rxnfc_field = RXH_L2DA,
2567 		.cls_prot = NET_PROT_ETH,
2568 		.cls_field = NH_FLD_ETH_DA,
2569 		.size = 6,
2570 	}, {
2571 		.cls_prot = NET_PROT_ETH,
2572 		.cls_field = NH_FLD_ETH_SA,
2573 		.size = 6,
2574 	}, {
2575 		/* This is the last ethertype field parsed:
2576 		 * depending on frame format, it can be the MAC ethertype
2577 		 * or the VLAN etype.
2578 		 */
2579 		.cls_prot = NET_PROT_ETH,
2580 		.cls_field = NH_FLD_ETH_TYPE,
2581 		.size = 2,
2582 	}, {
2583 		/* VLAN header */
2584 		.rxnfc_field = RXH_VLAN,
2585 		.cls_prot = NET_PROT_VLAN,
2586 		.cls_field = NH_FLD_VLAN_TCI,
2587 		.size = 2,
2588 	}, {
2589 		/* IP header */
2590 		.rxnfc_field = RXH_IP_SRC,
2591 		.cls_prot = NET_PROT_IP,
2592 		.cls_field = NH_FLD_IP_SRC,
2593 		.size = 4,
2594 	}, {
2595 		.rxnfc_field = RXH_IP_DST,
2596 		.cls_prot = NET_PROT_IP,
2597 		.cls_field = NH_FLD_IP_DST,
2598 		.size = 4,
2599 	}, {
2600 		.rxnfc_field = RXH_L3_PROTO,
2601 		.cls_prot = NET_PROT_IP,
2602 		.cls_field = NH_FLD_IP_PROTO,
2603 		.size = 1,
2604 	}, {
2605 		/* Using UDP ports, this is functionally equivalent to raw
2606 		 * byte pairs from L4 header.
2607 		 */
2608 		.rxnfc_field = RXH_L4_B_0_1,
2609 		.cls_prot = NET_PROT_UDP,
2610 		.cls_field = NH_FLD_UDP_PORT_SRC,
2611 		.size = 2,
2612 	}, {
2613 		.rxnfc_field = RXH_L4_B_2_3,
2614 		.cls_prot = NET_PROT_UDP,
2615 		.cls_field = NH_FLD_UDP_PORT_DST,
2616 		.size = 2,
2617 	},
2618 };
2619 
2620 /* Configure the Rx hash key using the legacy API */
2621 static int config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2622 {
2623 	struct device *dev = priv->net_dev->dev.parent;
2624 	struct dpni_rx_tc_dist_cfg dist_cfg;
2625 	int err;
2626 
2627 	memset(&dist_cfg, 0, sizeof(dist_cfg));
2628 
2629 	dist_cfg.key_cfg_iova = key;
2630 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2631 	dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
2632 
2633 	err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token, 0, &dist_cfg);
2634 	if (err)
2635 		dev_err(dev, "dpni_set_rx_tc_dist failed\n");
2636 
2637 	return err;
2638 }
2639 
2640 /* Configure the Rx hash key using the new API */
2641 static int config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2642 {
2643 	struct device *dev = priv->net_dev->dev.parent;
2644 	struct dpni_rx_dist_cfg dist_cfg;
2645 	int err;
2646 
2647 	memset(&dist_cfg, 0, sizeof(dist_cfg));
2648 
2649 	dist_cfg.key_cfg_iova = key;
2650 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2651 	dist_cfg.enable = 1;
2652 
2653 	err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token, &dist_cfg);
2654 	if (err)
2655 		dev_err(dev, "dpni_set_rx_hash_dist failed\n");
2656 
2657 	return err;
2658 }
2659 
2660 /* Configure the Rx flow classification key */
2661 static int config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2662 {
2663 	struct device *dev = priv->net_dev->dev.parent;
2664 	struct dpni_rx_dist_cfg dist_cfg;
2665 	int err;
2666 
2667 	memset(&dist_cfg, 0, sizeof(dist_cfg));
2668 
2669 	dist_cfg.key_cfg_iova = key;
2670 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2671 	dist_cfg.enable = 1;
2672 
2673 	err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token, &dist_cfg);
2674 	if (err)
2675 		dev_err(dev, "dpni_set_rx_fs_dist failed\n");
2676 
2677 	return err;
2678 }
2679 
2680 /* Size of the Rx flow classification key */
2681 int dpaa2_eth_cls_key_size(void)
2682 {
2683 	int i, size = 0;
2684 
2685 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
2686 		size += dist_fields[i].size;
2687 
2688 	return size;
2689 }
2690 
2691 /* Offset of header field in Rx classification key */
2692 int dpaa2_eth_cls_fld_off(int prot, int field)
2693 {
2694 	int i, off = 0;
2695 
2696 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
2697 		if (dist_fields[i].cls_prot == prot &&
2698 		    dist_fields[i].cls_field == field)
2699 			return off;
2700 		off += dist_fields[i].size;
2701 	}
2702 
2703 	WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
2704 	return 0;
2705 }
2706 
2707 /* Set Rx distribution (hash or flow classification) key
2708  * flags is a combination of RXH_ bits
2709  */
2710 static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
2711 				  enum dpaa2_eth_rx_dist type, u64 flags)
2712 {
2713 	struct device *dev = net_dev->dev.parent;
2714 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2715 	struct dpkg_profile_cfg cls_cfg;
2716 	u32 rx_hash_fields = 0;
2717 	dma_addr_t key_iova;
2718 	u8 *dma_mem;
2719 	int i;
2720 	int err = 0;
2721 
2722 	memset(&cls_cfg, 0, sizeof(cls_cfg));
2723 
2724 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
2725 		struct dpkg_extract *key =
2726 			&cls_cfg.extracts[cls_cfg.num_extracts];
2727 
2728 		/* For Rx hashing key we set only the selected fields.
2729 		 * For Rx flow classification key we set all supported fields
2730 		 */
2731 		if (type == DPAA2_ETH_RX_DIST_HASH) {
2732 			if (!(flags & dist_fields[i].rxnfc_field))
2733 				continue;
2734 			rx_hash_fields |= dist_fields[i].rxnfc_field;
2735 		}
2736 
2737 		if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
2738 			dev_err(dev, "error adding key extraction rule, too many rules?\n");
2739 			return -E2BIG;
2740 		}
2741 
2742 		key->type = DPKG_EXTRACT_FROM_HDR;
2743 		key->extract.from_hdr.prot = dist_fields[i].cls_prot;
2744 		key->extract.from_hdr.type = DPKG_FULL_FIELD;
2745 		key->extract.from_hdr.field = dist_fields[i].cls_field;
2746 		cls_cfg.num_extracts++;
2747 	}
2748 
2749 	dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
2750 	if (!dma_mem)
2751 		return -ENOMEM;
2752 
2753 	err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
2754 	if (err) {
2755 		dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
2756 		goto free_key;
2757 	}
2758 
2759 	/* Prepare for setting the rx dist */
2760 	key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
2761 				  DMA_TO_DEVICE);
2762 	if (dma_mapping_error(dev, key_iova)) {
2763 		dev_err(dev, "DMA mapping failed\n");
2764 		err = -ENOMEM;
2765 		goto free_key;
2766 	}
2767 
2768 	if (type == DPAA2_ETH_RX_DIST_HASH) {
2769 		if (dpaa2_eth_has_legacy_dist(priv))
2770 			err = config_legacy_hash_key(priv, key_iova);
2771 		else
2772 			err = config_hash_key(priv, key_iova);
2773 	} else {
2774 		err = config_cls_key(priv, key_iova);
2775 	}
2776 
2777 	dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
2778 			 DMA_TO_DEVICE);
2779 	if (!err && type == DPAA2_ETH_RX_DIST_HASH)
2780 		priv->rx_hash_fields = rx_hash_fields;
2781 
2782 free_key:
2783 	kfree(dma_mem);
2784 	return err;
2785 }
2786 
2787 int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
2788 {
2789 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2790 
2791 	if (!dpaa2_eth_hash_enabled(priv))
2792 		return -EOPNOTSUPP;
2793 
2794 	return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, flags);
2795 }
2796 
2797 static int dpaa2_eth_set_cls(struct dpaa2_eth_priv *priv)
2798 {
2799 	struct device *dev = priv->net_dev->dev.parent;
2800 
2801 	/* Check if we actually support Rx flow classification */
2802 	if (dpaa2_eth_has_legacy_dist(priv)) {
2803 		dev_dbg(dev, "Rx cls not supported by current MC version\n");
2804 		return -EOPNOTSUPP;
2805 	}
2806 
2807 	if (priv->dpni_attrs.options & DPNI_OPT_NO_FS ||
2808 	    !(priv->dpni_attrs.options & DPNI_OPT_HAS_KEY_MASKING)) {
2809 		dev_dbg(dev, "Rx cls disabled in DPNI options\n");
2810 		return -EOPNOTSUPP;
2811 	}
2812 
2813 	if (!dpaa2_eth_hash_enabled(priv)) {
2814 		dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
2815 		return -EOPNOTSUPP;
2816 	}
2817 
2818 	priv->rx_cls_enabled = 1;
2819 
2820 	return dpaa2_eth_set_dist_key(priv->net_dev, DPAA2_ETH_RX_DIST_CLS, 0);
2821 }
2822 
2823 /* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
2824  * frame queues and channels
2825  */
2826 static int bind_dpni(struct dpaa2_eth_priv *priv)
2827 {
2828 	struct net_device *net_dev = priv->net_dev;
2829 	struct device *dev = net_dev->dev.parent;
2830 	struct dpni_pools_cfg pools_params;
2831 	struct dpni_error_cfg err_cfg;
2832 	int err = 0;
2833 	int i;
2834 
2835 	pools_params.num_dpbp = 1;
2836 	pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id;
2837 	pools_params.pools[0].backup_pool = 0;
2838 	pools_params.pools[0].buffer_size = DPAA2_ETH_RX_BUF_SIZE;
2839 	err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
2840 	if (err) {
2841 		dev_err(dev, "dpni_set_pools() failed\n");
2842 		return err;
2843 	}
2844 
2845 	/* have the interface implicitly distribute traffic based on
2846 	 * the default hash key
2847 	 */
2848 	err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
2849 	if (err && err != -EOPNOTSUPP)
2850 		dev_err(dev, "Failed to configure hashing\n");
2851 
2852 	/* Configure the flow classification key; it includes all
2853 	 * supported header fields and cannot be modified at runtime
2854 	 */
2855 	err = dpaa2_eth_set_cls(priv);
2856 	if (err && err != -EOPNOTSUPP)
2857 		dev_err(dev, "Failed to configure Rx classification key\n");
2858 
2859 	/* Configure handling of error frames */
2860 	err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
2861 	err_cfg.set_frame_annotation = 1;
2862 	err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
2863 	err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
2864 				       &err_cfg);
2865 	if (err) {
2866 		dev_err(dev, "dpni_set_errors_behavior failed\n");
2867 		return err;
2868 	}
2869 
2870 	/* Configure Rx and Tx conf queues to generate CDANs */
2871 	for (i = 0; i < priv->num_fqs; i++) {
2872 		switch (priv->fq[i].type) {
2873 		case DPAA2_RX_FQ:
2874 			err = setup_rx_flow(priv, &priv->fq[i]);
2875 			break;
2876 		case DPAA2_TX_CONF_FQ:
2877 			err = setup_tx_flow(priv, &priv->fq[i]);
2878 			break;
2879 		default:
2880 			dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
2881 			return -EINVAL;
2882 		}
2883 		if (err)
2884 			return err;
2885 	}
2886 
2887 	err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
2888 			    DPNI_QUEUE_TX, &priv->tx_qdid);
2889 	if (err) {
2890 		dev_err(dev, "dpni_get_qdid() failed\n");
2891 		return err;
2892 	}
2893 
2894 	return 0;
2895 }
2896 
2897 /* Allocate rings for storing incoming frame descriptors */
2898 static int alloc_rings(struct dpaa2_eth_priv *priv)
2899 {
2900 	struct net_device *net_dev = priv->net_dev;
2901 	struct device *dev = net_dev->dev.parent;
2902 	int i;
2903 
2904 	for (i = 0; i < priv->num_channels; i++) {
2905 		priv->channel[i]->store =
2906 			dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
2907 		if (!priv->channel[i]->store) {
2908 			netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
2909 			goto err_ring;
2910 		}
2911 	}
2912 
2913 	return 0;
2914 
2915 err_ring:
2916 	for (i = 0; i < priv->num_channels; i++) {
2917 		if (!priv->channel[i]->store)
2918 			break;
2919 		dpaa2_io_store_destroy(priv->channel[i]->store);
2920 	}
2921 
2922 	return -ENOMEM;
2923 }
2924 
2925 static void free_rings(struct dpaa2_eth_priv *priv)
2926 {
2927 	int i;
2928 
2929 	for (i = 0; i < priv->num_channels; i++)
2930 		dpaa2_io_store_destroy(priv->channel[i]->store);
2931 }
2932 
2933 static int set_mac_addr(struct dpaa2_eth_priv *priv)
2934 {
2935 	struct net_device *net_dev = priv->net_dev;
2936 	struct device *dev = net_dev->dev.parent;
2937 	u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
2938 	int err;
2939 
2940 	/* Get firmware address, if any */
2941 	err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
2942 	if (err) {
2943 		dev_err(dev, "dpni_get_port_mac_addr() failed\n");
2944 		return err;
2945 	}
2946 
2947 	/* Get DPNI attributes address, if any */
2948 	err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
2949 					dpni_mac_addr);
2950 	if (err) {
2951 		dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
2952 		return err;
2953 	}
2954 
2955 	/* First check if firmware has any address configured by bootloader */
2956 	if (!is_zero_ether_addr(mac_addr)) {
2957 		/* If the DPMAC addr != DPNI addr, update it */
2958 		if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
2959 			err = dpni_set_primary_mac_addr(priv->mc_io, 0,
2960 							priv->mc_token,
2961 							mac_addr);
2962 			if (err) {
2963 				dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
2964 				return err;
2965 			}
2966 		}
2967 		memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len);
2968 	} else if (is_zero_ether_addr(dpni_mac_addr)) {
2969 		/* No MAC address configured, fill in net_dev->dev_addr
2970 		 * with a random one
2971 		 */
2972 		eth_hw_addr_random(net_dev);
2973 		dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
2974 
2975 		err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
2976 						net_dev->dev_addr);
2977 		if (err) {
2978 			dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
2979 			return err;
2980 		}
2981 
2982 		/* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
2983 		 * practical purposes, this will be our "permanent" mac address,
2984 		 * at least until the next reboot. This move will also permit
2985 		 * register_netdevice() to properly fill up net_dev->perm_addr.
2986 		 */
2987 		net_dev->addr_assign_type = NET_ADDR_PERM;
2988 	} else {
2989 		/* NET_ADDR_PERM is default, all we have to do is
2990 		 * fill in the device addr.
2991 		 */
2992 		memcpy(net_dev->dev_addr, dpni_mac_addr, net_dev->addr_len);
2993 	}
2994 
2995 	return 0;
2996 }
2997 
2998 static int netdev_init(struct net_device *net_dev)
2999 {
3000 	struct device *dev = net_dev->dev.parent;
3001 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
3002 	u32 options = priv->dpni_attrs.options;
3003 	u64 supported = 0, not_supported = 0;
3004 	u8 bcast_addr[ETH_ALEN];
3005 	u8 num_queues;
3006 	int err;
3007 
3008 	net_dev->netdev_ops = &dpaa2_eth_ops;
3009 	net_dev->ethtool_ops = &dpaa2_ethtool_ops;
3010 
3011 	err = set_mac_addr(priv);
3012 	if (err)
3013 		return err;
3014 
3015 	/* Explicitly add the broadcast address to the MAC filtering table */
3016 	eth_broadcast_addr(bcast_addr);
3017 	err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
3018 	if (err) {
3019 		dev_err(dev, "dpni_add_mac_addr() failed\n");
3020 		return err;
3021 	}
3022 
3023 	/* Set MTU upper limit; lower limit is 68B (default value) */
3024 	net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
3025 	err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
3026 					DPAA2_ETH_MFL);
3027 	if (err) {
3028 		dev_err(dev, "dpni_set_max_frame_length() failed\n");
3029 		return err;
3030 	}
3031 
3032 	/* Set actual number of queues in the net device */
3033 	num_queues = dpaa2_eth_queue_count(priv);
3034 	err = netif_set_real_num_tx_queues(net_dev, num_queues);
3035 	if (err) {
3036 		dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
3037 		return err;
3038 	}
3039 	err = netif_set_real_num_rx_queues(net_dev, num_queues);
3040 	if (err) {
3041 		dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
3042 		return err;
3043 	}
3044 
3045 	/* Capabilities listing */
3046 	supported |= IFF_LIVE_ADDR_CHANGE;
3047 
3048 	if (options & DPNI_OPT_NO_MAC_FILTER)
3049 		not_supported |= IFF_UNICAST_FLT;
3050 	else
3051 		supported |= IFF_UNICAST_FLT;
3052 
3053 	net_dev->priv_flags |= supported;
3054 	net_dev->priv_flags &= ~not_supported;
3055 
3056 	/* Features */
3057 	net_dev->features = NETIF_F_RXCSUM |
3058 			    NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
3059 			    NETIF_F_SG | NETIF_F_HIGHDMA |
3060 			    NETIF_F_LLTX;
3061 	net_dev->hw_features = net_dev->features;
3062 
3063 	return 0;
3064 }
3065 
3066 static int poll_link_state(void *arg)
3067 {
3068 	struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
3069 	int err;
3070 
3071 	while (!kthread_should_stop()) {
3072 		err = link_state_update(priv);
3073 		if (unlikely(err))
3074 			return err;
3075 
3076 		msleep(DPAA2_ETH_LINK_STATE_REFRESH);
3077 	}
3078 
3079 	return 0;
3080 }
3081 
3082 static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
3083 {
3084 	u32 status = ~0;
3085 	struct device *dev = (struct device *)arg;
3086 	struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
3087 	struct net_device *net_dev = dev_get_drvdata(dev);
3088 	int err;
3089 
3090 	err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
3091 				  DPNI_IRQ_INDEX, &status);
3092 	if (unlikely(err)) {
3093 		netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
3094 		return IRQ_HANDLED;
3095 	}
3096 
3097 	if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
3098 		link_state_update(netdev_priv(net_dev));
3099 
3100 	return IRQ_HANDLED;
3101 }
3102 
3103 static int setup_irqs(struct fsl_mc_device *ls_dev)
3104 {
3105 	int err = 0;
3106 	struct fsl_mc_device_irq *irq;
3107 
3108 	err = fsl_mc_allocate_irqs(ls_dev);
3109 	if (err) {
3110 		dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
3111 		return err;
3112 	}
3113 
3114 	irq = ls_dev->irqs[0];
3115 	err = devm_request_threaded_irq(&ls_dev->dev, irq->msi_desc->irq,
3116 					NULL, dpni_irq0_handler_thread,
3117 					IRQF_NO_SUSPEND | IRQF_ONESHOT,
3118 					dev_name(&ls_dev->dev), &ls_dev->dev);
3119 	if (err < 0) {
3120 		dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
3121 		goto free_mc_irq;
3122 	}
3123 
3124 	err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
3125 				DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED);
3126 	if (err < 0) {
3127 		dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
3128 		goto free_irq;
3129 	}
3130 
3131 	err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
3132 				  DPNI_IRQ_INDEX, 1);
3133 	if (err < 0) {
3134 		dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
3135 		goto free_irq;
3136 	}
3137 
3138 	return 0;
3139 
3140 free_irq:
3141 	devm_free_irq(&ls_dev->dev, irq->msi_desc->irq, &ls_dev->dev);
3142 free_mc_irq:
3143 	fsl_mc_free_irqs(ls_dev);
3144 
3145 	return err;
3146 }
3147 
3148 static void add_ch_napi(struct dpaa2_eth_priv *priv)
3149 {
3150 	int i;
3151 	struct dpaa2_eth_channel *ch;
3152 
3153 	for (i = 0; i < priv->num_channels; i++) {
3154 		ch = priv->channel[i];
3155 		/* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
3156 		netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll,
3157 			       NAPI_POLL_WEIGHT);
3158 	}
3159 }
3160 
3161 static void del_ch_napi(struct dpaa2_eth_priv *priv)
3162 {
3163 	int i;
3164 	struct dpaa2_eth_channel *ch;
3165 
3166 	for (i = 0; i < priv->num_channels; i++) {
3167 		ch = priv->channel[i];
3168 		netif_napi_del(&ch->napi);
3169 	}
3170 }
3171 
3172 static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
3173 {
3174 	struct device *dev;
3175 	struct net_device *net_dev = NULL;
3176 	struct dpaa2_eth_priv *priv = NULL;
3177 	int err = 0;
3178 
3179 	dev = &dpni_dev->dev;
3180 
3181 	/* Net device */
3182 	net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_TX_QUEUES);
3183 	if (!net_dev) {
3184 		dev_err(dev, "alloc_etherdev_mq() failed\n");
3185 		return -ENOMEM;
3186 	}
3187 
3188 	SET_NETDEV_DEV(net_dev, dev);
3189 	dev_set_drvdata(dev, net_dev);
3190 
3191 	priv = netdev_priv(net_dev);
3192 	priv->net_dev = net_dev;
3193 
3194 	priv->iommu_domain = iommu_get_domain_for_dev(dev);
3195 
3196 	/* Obtain a MC portal */
3197 	err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
3198 				     &priv->mc_io);
3199 	if (err) {
3200 		if (err == -ENXIO)
3201 			err = -EPROBE_DEFER;
3202 		else
3203 			dev_err(dev, "MC portal allocation failed\n");
3204 		goto err_portal_alloc;
3205 	}
3206 
3207 	/* MC objects initialization and configuration */
3208 	err = setup_dpni(dpni_dev);
3209 	if (err)
3210 		goto err_dpni_setup;
3211 
3212 	err = setup_dpio(priv);
3213 	if (err)
3214 		goto err_dpio_setup;
3215 
3216 	setup_fqs(priv);
3217 
3218 	err = setup_dpbp(priv);
3219 	if (err)
3220 		goto err_dpbp_setup;
3221 
3222 	err = bind_dpni(priv);
3223 	if (err)
3224 		goto err_bind;
3225 
3226 	/* Add a NAPI context for each channel */
3227 	add_ch_napi(priv);
3228 
3229 	/* Percpu statistics */
3230 	priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
3231 	if (!priv->percpu_stats) {
3232 		dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
3233 		err = -ENOMEM;
3234 		goto err_alloc_percpu_stats;
3235 	}
3236 	priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
3237 	if (!priv->percpu_extras) {
3238 		dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
3239 		err = -ENOMEM;
3240 		goto err_alloc_percpu_extras;
3241 	}
3242 
3243 	err = netdev_init(net_dev);
3244 	if (err)
3245 		goto err_netdev_init;
3246 
3247 	/* Configure checksum offload based on current interface flags */
3248 	err = set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
3249 	if (err)
3250 		goto err_csum;
3251 
3252 	err = set_tx_csum(priv, !!(net_dev->features &
3253 				   (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
3254 	if (err)
3255 		goto err_csum;
3256 
3257 	err = alloc_rings(priv);
3258 	if (err)
3259 		goto err_alloc_rings;
3260 
3261 	err = setup_irqs(dpni_dev);
3262 	if (err) {
3263 		netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
3264 		priv->poll_thread = kthread_run(poll_link_state, priv,
3265 						"%s_poll_link", net_dev->name);
3266 		if (IS_ERR(priv->poll_thread)) {
3267 			dev_err(dev, "Error starting polling thread\n");
3268 			goto err_poll_thread;
3269 		}
3270 		priv->do_link_poll = true;
3271 	}
3272 
3273 	err = register_netdev(net_dev);
3274 	if (err < 0) {
3275 		dev_err(dev, "register_netdev() failed\n");
3276 		goto err_netdev_reg;
3277 	}
3278 
3279 #ifdef CONFIG_DEBUG_FS
3280 	dpaa2_dbg_add(priv);
3281 #endif
3282 
3283 	dev_info(dev, "Probed interface %s\n", net_dev->name);
3284 	return 0;
3285 
3286 err_netdev_reg:
3287 	if (priv->do_link_poll)
3288 		kthread_stop(priv->poll_thread);
3289 	else
3290 		fsl_mc_free_irqs(dpni_dev);
3291 err_poll_thread:
3292 	free_rings(priv);
3293 err_alloc_rings:
3294 err_csum:
3295 err_netdev_init:
3296 	free_percpu(priv->percpu_extras);
3297 err_alloc_percpu_extras:
3298 	free_percpu(priv->percpu_stats);
3299 err_alloc_percpu_stats:
3300 	del_ch_napi(priv);
3301 err_bind:
3302 	free_dpbp(priv);
3303 err_dpbp_setup:
3304 	free_dpio(priv);
3305 err_dpio_setup:
3306 	free_dpni(priv);
3307 err_dpni_setup:
3308 	fsl_mc_portal_free(priv->mc_io);
3309 err_portal_alloc:
3310 	dev_set_drvdata(dev, NULL);
3311 	free_netdev(net_dev);
3312 
3313 	return err;
3314 }
3315 
3316 static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
3317 {
3318 	struct device *dev;
3319 	struct net_device *net_dev;
3320 	struct dpaa2_eth_priv *priv;
3321 
3322 	dev = &ls_dev->dev;
3323 	net_dev = dev_get_drvdata(dev);
3324 	priv = netdev_priv(net_dev);
3325 
3326 #ifdef CONFIG_DEBUG_FS
3327 	dpaa2_dbg_remove(priv);
3328 #endif
3329 	unregister_netdev(net_dev);
3330 
3331 	if (priv->do_link_poll)
3332 		kthread_stop(priv->poll_thread);
3333 	else
3334 		fsl_mc_free_irqs(ls_dev);
3335 
3336 	free_rings(priv);
3337 	free_percpu(priv->percpu_stats);
3338 	free_percpu(priv->percpu_extras);
3339 
3340 	del_ch_napi(priv);
3341 	free_dpbp(priv);
3342 	free_dpio(priv);
3343 	free_dpni(priv);
3344 
3345 	fsl_mc_portal_free(priv->mc_io);
3346 
3347 	free_netdev(net_dev);
3348 
3349 	dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
3350 
3351 	return 0;
3352 }
3353 
3354 static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
3355 	{
3356 		.vendor = FSL_MC_VENDOR_FREESCALE,
3357 		.obj_type = "dpni",
3358 	},
3359 	{ .vendor = 0x0 }
3360 };
3361 MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
3362 
3363 static struct fsl_mc_driver dpaa2_eth_driver = {
3364 	.driver = {
3365 		.name = KBUILD_MODNAME,
3366 		.owner = THIS_MODULE,
3367 	},
3368 	.probe = dpaa2_eth_probe,
3369 	.remove = dpaa2_eth_remove,
3370 	.match_id_table = dpaa2_eth_match_id_table
3371 };
3372 
3373 static int __init dpaa2_eth_driver_init(void)
3374 {
3375 	int err;
3376 
3377 	dpaa2_eth_dbg_init();
3378 	err = fsl_mc_driver_register(&dpaa2_eth_driver);
3379 	if (err) {
3380 		dpaa2_eth_dbg_exit();
3381 		return err;
3382 	}
3383 
3384 	return 0;
3385 }
3386 
3387 static void __exit dpaa2_eth_driver_exit(void)
3388 {
3389 	dpaa2_eth_dbg_exit();
3390 	fsl_mc_driver_unregister(&dpaa2_eth_driver);
3391 }
3392 
3393 module_init(dpaa2_eth_driver_init);
3394 module_exit(dpaa2_eth_driver_exit);
3395