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