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