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 	queue.flc.stash_control = 1;
2307 	queue.flc.value &= 0xFFFFFFFFFFFFFFC0;
2308 	/* 01 01 00 - data, annotation, flow context */
2309 	queue.flc.value |= 0x14;
2310 	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
2311 			     DPNI_QUEUE_RX, 0, fq->flowid,
2312 			     DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST |
2313 			     DPNI_QUEUE_OPT_FLC,
2314 			     &queue);
2315 	if (err) {
2316 		dev_err(dev, "dpni_set_queue(RX) failed\n");
2317 		return err;
2318 	}
2319 
2320 	td.enable = 1;
2321 	td.threshold = DPAA2_ETH_TAILDROP_THRESH;
2322 	err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token, DPNI_CP_QUEUE,
2323 				DPNI_QUEUE_RX, 0, fq->flowid, &td);
2324 	if (err) {
2325 		dev_err(dev, "dpni_set_threshold() failed\n");
2326 		return err;
2327 	}
2328 
2329 	return 0;
2330 }
2331 
2332 static int setup_tx_flow(struct dpaa2_eth_priv *priv,
2333 			 struct dpaa2_eth_fq *fq)
2334 {
2335 	struct device *dev = priv->net_dev->dev.parent;
2336 	struct dpni_queue queue;
2337 	struct dpni_queue_id qid;
2338 	int err;
2339 
2340 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2341 			     DPNI_QUEUE_TX, 0, fq->flowid, &queue, &qid);
2342 	if (err) {
2343 		dev_err(dev, "dpni_get_queue(TX) failed\n");
2344 		return err;
2345 	}
2346 
2347 	fq->tx_qdbin = qid.qdbin;
2348 
2349 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2350 			     DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
2351 			     &queue, &qid);
2352 	if (err) {
2353 		dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
2354 		return err;
2355 	}
2356 
2357 	fq->fqid = qid.fqid;
2358 
2359 	queue.destination.id = fq->channel->dpcon_id;
2360 	queue.destination.type = DPNI_DEST_DPCON;
2361 	queue.destination.priority = 0;
2362 	queue.user_context = (u64)(uintptr_t)fq;
2363 	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
2364 			     DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
2365 			     DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
2366 			     &queue);
2367 	if (err) {
2368 		dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
2369 		return err;
2370 	}
2371 
2372 	return 0;
2373 }
2374 
2375 /* Supported header fields for Rx hash distribution key */
2376 static const struct dpaa2_eth_dist_fields dist_fields[] = {
2377 	{
2378 		/* L2 header */
2379 		.rxnfc_field = RXH_L2DA,
2380 		.cls_prot = NET_PROT_ETH,
2381 		.cls_field = NH_FLD_ETH_DA,
2382 		.size = 6,
2383 	}, {
2384 		.cls_prot = NET_PROT_ETH,
2385 		.cls_field = NH_FLD_ETH_SA,
2386 		.size = 6,
2387 	}, {
2388 		/* This is the last ethertype field parsed:
2389 		 * depending on frame format, it can be the MAC ethertype
2390 		 * or the VLAN etype.
2391 		 */
2392 		.cls_prot = NET_PROT_ETH,
2393 		.cls_field = NH_FLD_ETH_TYPE,
2394 		.size = 2,
2395 	}, {
2396 		/* VLAN header */
2397 		.rxnfc_field = RXH_VLAN,
2398 		.cls_prot = NET_PROT_VLAN,
2399 		.cls_field = NH_FLD_VLAN_TCI,
2400 		.size = 2,
2401 	}, {
2402 		/* IP header */
2403 		.rxnfc_field = RXH_IP_SRC,
2404 		.cls_prot = NET_PROT_IP,
2405 		.cls_field = NH_FLD_IP_SRC,
2406 		.size = 4,
2407 	}, {
2408 		.rxnfc_field = RXH_IP_DST,
2409 		.cls_prot = NET_PROT_IP,
2410 		.cls_field = NH_FLD_IP_DST,
2411 		.size = 4,
2412 	}, {
2413 		.rxnfc_field = RXH_L3_PROTO,
2414 		.cls_prot = NET_PROT_IP,
2415 		.cls_field = NH_FLD_IP_PROTO,
2416 		.size = 1,
2417 	}, {
2418 		/* Using UDP ports, this is functionally equivalent to raw
2419 		 * byte pairs from L4 header.
2420 		 */
2421 		.rxnfc_field = RXH_L4_B_0_1,
2422 		.cls_prot = NET_PROT_UDP,
2423 		.cls_field = NH_FLD_UDP_PORT_SRC,
2424 		.size = 2,
2425 	}, {
2426 		.rxnfc_field = RXH_L4_B_2_3,
2427 		.cls_prot = NET_PROT_UDP,
2428 		.cls_field = NH_FLD_UDP_PORT_DST,
2429 		.size = 2,
2430 	},
2431 };
2432 
2433 /* Configure the Rx hash key using the legacy API */
2434 static int config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2435 {
2436 	struct device *dev = priv->net_dev->dev.parent;
2437 	struct dpni_rx_tc_dist_cfg dist_cfg;
2438 	int err;
2439 
2440 	memset(&dist_cfg, 0, sizeof(dist_cfg));
2441 
2442 	dist_cfg.key_cfg_iova = key;
2443 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2444 	dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
2445 
2446 	err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token, 0, &dist_cfg);
2447 	if (err)
2448 		dev_err(dev, "dpni_set_rx_tc_dist failed\n");
2449 
2450 	return err;
2451 }
2452 
2453 /* Configure the Rx hash key using the new API */
2454 static int config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2455 {
2456 	struct device *dev = priv->net_dev->dev.parent;
2457 	struct dpni_rx_dist_cfg dist_cfg;
2458 	int err;
2459 
2460 	memset(&dist_cfg, 0, sizeof(dist_cfg));
2461 
2462 	dist_cfg.key_cfg_iova = key;
2463 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2464 	dist_cfg.enable = 1;
2465 
2466 	err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token, &dist_cfg);
2467 	if (err)
2468 		dev_err(dev, "dpni_set_rx_hash_dist failed\n");
2469 
2470 	return err;
2471 }
2472 
2473 /* Configure the Rx flow classification key */
2474 static int config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2475 {
2476 	struct device *dev = priv->net_dev->dev.parent;
2477 	struct dpni_rx_dist_cfg dist_cfg;
2478 	int err;
2479 
2480 	memset(&dist_cfg, 0, sizeof(dist_cfg));
2481 
2482 	dist_cfg.key_cfg_iova = key;
2483 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2484 	dist_cfg.enable = 1;
2485 
2486 	err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token, &dist_cfg);
2487 	if (err)
2488 		dev_err(dev, "dpni_set_rx_fs_dist failed\n");
2489 
2490 	return err;
2491 }
2492 
2493 /* Size of the Rx flow classification key */
2494 int dpaa2_eth_cls_key_size(void)
2495 {
2496 	int i, size = 0;
2497 
2498 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
2499 		size += dist_fields[i].size;
2500 
2501 	return size;
2502 }
2503 
2504 /* Offset of header field in Rx classification key */
2505 int dpaa2_eth_cls_fld_off(int prot, int field)
2506 {
2507 	int i, off = 0;
2508 
2509 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
2510 		if (dist_fields[i].cls_prot == prot &&
2511 		    dist_fields[i].cls_field == field)
2512 			return off;
2513 		off += dist_fields[i].size;
2514 	}
2515 
2516 	WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
2517 	return 0;
2518 }
2519 
2520 /* Set Rx distribution (hash or flow classification) key
2521  * flags is a combination of RXH_ bits
2522  */
2523 static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
2524 				  enum dpaa2_eth_rx_dist type, u64 flags)
2525 {
2526 	struct device *dev = net_dev->dev.parent;
2527 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2528 	struct dpkg_profile_cfg cls_cfg;
2529 	u32 rx_hash_fields = 0;
2530 	dma_addr_t key_iova;
2531 	u8 *dma_mem;
2532 	int i;
2533 	int err = 0;
2534 
2535 	memset(&cls_cfg, 0, sizeof(cls_cfg));
2536 
2537 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
2538 		struct dpkg_extract *key =
2539 			&cls_cfg.extracts[cls_cfg.num_extracts];
2540 
2541 		/* For Rx hashing key we set only the selected fields.
2542 		 * For Rx flow classification key we set all supported fields
2543 		 */
2544 		if (type == DPAA2_ETH_RX_DIST_HASH) {
2545 			if (!(flags & dist_fields[i].rxnfc_field))
2546 				continue;
2547 			rx_hash_fields |= dist_fields[i].rxnfc_field;
2548 		}
2549 
2550 		if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
2551 			dev_err(dev, "error adding key extraction rule, too many rules?\n");
2552 			return -E2BIG;
2553 		}
2554 
2555 		key->type = DPKG_EXTRACT_FROM_HDR;
2556 		key->extract.from_hdr.prot = dist_fields[i].cls_prot;
2557 		key->extract.from_hdr.type = DPKG_FULL_FIELD;
2558 		key->extract.from_hdr.field = dist_fields[i].cls_field;
2559 		cls_cfg.num_extracts++;
2560 	}
2561 
2562 	dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
2563 	if (!dma_mem)
2564 		return -ENOMEM;
2565 
2566 	err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
2567 	if (err) {
2568 		dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
2569 		goto free_key;
2570 	}
2571 
2572 	/* Prepare for setting the rx dist */
2573 	key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
2574 				  DMA_TO_DEVICE);
2575 	if (dma_mapping_error(dev, key_iova)) {
2576 		dev_err(dev, "DMA mapping failed\n");
2577 		err = -ENOMEM;
2578 		goto free_key;
2579 	}
2580 
2581 	if (type == DPAA2_ETH_RX_DIST_HASH) {
2582 		if (dpaa2_eth_has_legacy_dist(priv))
2583 			err = config_legacy_hash_key(priv, key_iova);
2584 		else
2585 			err = config_hash_key(priv, key_iova);
2586 	} else {
2587 		err = config_cls_key(priv, key_iova);
2588 	}
2589 
2590 	dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
2591 			 DMA_TO_DEVICE);
2592 	if (!err && type == DPAA2_ETH_RX_DIST_HASH)
2593 		priv->rx_hash_fields = rx_hash_fields;
2594 
2595 free_key:
2596 	kfree(dma_mem);
2597 	return err;
2598 }
2599 
2600 int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
2601 {
2602 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2603 
2604 	if (!dpaa2_eth_hash_enabled(priv))
2605 		return -EOPNOTSUPP;
2606 
2607 	return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, flags);
2608 }
2609 
2610 static int dpaa2_eth_set_cls(struct dpaa2_eth_priv *priv)
2611 {
2612 	struct device *dev = priv->net_dev->dev.parent;
2613 
2614 	/* Check if we actually support Rx flow classification */
2615 	if (dpaa2_eth_has_legacy_dist(priv)) {
2616 		dev_dbg(dev, "Rx cls not supported by current MC version\n");
2617 		return -EOPNOTSUPP;
2618 	}
2619 
2620 	if (priv->dpni_attrs.options & DPNI_OPT_NO_FS ||
2621 	    !(priv->dpni_attrs.options & DPNI_OPT_HAS_KEY_MASKING)) {
2622 		dev_dbg(dev, "Rx cls disabled in DPNI options\n");
2623 		return -EOPNOTSUPP;
2624 	}
2625 
2626 	if (!dpaa2_eth_hash_enabled(priv)) {
2627 		dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
2628 		return -EOPNOTSUPP;
2629 	}
2630 
2631 	priv->rx_cls_enabled = 1;
2632 
2633 	return dpaa2_eth_set_dist_key(priv->net_dev, DPAA2_ETH_RX_DIST_CLS, 0);
2634 }
2635 
2636 /* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
2637  * frame queues and channels
2638  */
2639 static int bind_dpni(struct dpaa2_eth_priv *priv)
2640 {
2641 	struct net_device *net_dev = priv->net_dev;
2642 	struct device *dev = net_dev->dev.parent;
2643 	struct dpni_pools_cfg pools_params;
2644 	struct dpni_error_cfg err_cfg;
2645 	int err = 0;
2646 	int i;
2647 
2648 	pools_params.num_dpbp = 1;
2649 	pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id;
2650 	pools_params.pools[0].backup_pool = 0;
2651 	pools_params.pools[0].buffer_size = DPAA2_ETH_RX_BUF_SIZE;
2652 	err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
2653 	if (err) {
2654 		dev_err(dev, "dpni_set_pools() failed\n");
2655 		return err;
2656 	}
2657 
2658 	/* have the interface implicitly distribute traffic based on
2659 	 * the default hash key
2660 	 */
2661 	err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
2662 	if (err && err != -EOPNOTSUPP)
2663 		dev_err(dev, "Failed to configure hashing\n");
2664 
2665 	/* Configure the flow classification key; it includes all
2666 	 * supported header fields and cannot be modified at runtime
2667 	 */
2668 	err = dpaa2_eth_set_cls(priv);
2669 	if (err && err != -EOPNOTSUPP)
2670 		dev_err(dev, "Failed to configure Rx classification key\n");
2671 
2672 	/* Configure handling of error frames */
2673 	err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
2674 	err_cfg.set_frame_annotation = 1;
2675 	err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
2676 	err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
2677 				       &err_cfg);
2678 	if (err) {
2679 		dev_err(dev, "dpni_set_errors_behavior failed\n");
2680 		return err;
2681 	}
2682 
2683 	/* Configure Rx and Tx conf queues to generate CDANs */
2684 	for (i = 0; i < priv->num_fqs; i++) {
2685 		switch (priv->fq[i].type) {
2686 		case DPAA2_RX_FQ:
2687 			err = setup_rx_flow(priv, &priv->fq[i]);
2688 			break;
2689 		case DPAA2_TX_CONF_FQ:
2690 			err = setup_tx_flow(priv, &priv->fq[i]);
2691 			break;
2692 		default:
2693 			dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
2694 			return -EINVAL;
2695 		}
2696 		if (err)
2697 			return err;
2698 	}
2699 
2700 	err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
2701 			    DPNI_QUEUE_TX, &priv->tx_qdid);
2702 	if (err) {
2703 		dev_err(dev, "dpni_get_qdid() failed\n");
2704 		return err;
2705 	}
2706 
2707 	return 0;
2708 }
2709 
2710 /* Allocate rings for storing incoming frame descriptors */
2711 static int alloc_rings(struct dpaa2_eth_priv *priv)
2712 {
2713 	struct net_device *net_dev = priv->net_dev;
2714 	struct device *dev = net_dev->dev.parent;
2715 	int i;
2716 
2717 	for (i = 0; i < priv->num_channels; i++) {
2718 		priv->channel[i]->store =
2719 			dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
2720 		if (!priv->channel[i]->store) {
2721 			netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
2722 			goto err_ring;
2723 		}
2724 	}
2725 
2726 	return 0;
2727 
2728 err_ring:
2729 	for (i = 0; i < priv->num_channels; i++) {
2730 		if (!priv->channel[i]->store)
2731 			break;
2732 		dpaa2_io_store_destroy(priv->channel[i]->store);
2733 	}
2734 
2735 	return -ENOMEM;
2736 }
2737 
2738 static void free_rings(struct dpaa2_eth_priv *priv)
2739 {
2740 	int i;
2741 
2742 	for (i = 0; i < priv->num_channels; i++)
2743 		dpaa2_io_store_destroy(priv->channel[i]->store);
2744 }
2745 
2746 static int set_mac_addr(struct dpaa2_eth_priv *priv)
2747 {
2748 	struct net_device *net_dev = priv->net_dev;
2749 	struct device *dev = net_dev->dev.parent;
2750 	u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
2751 	int err;
2752 
2753 	/* Get firmware address, if any */
2754 	err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
2755 	if (err) {
2756 		dev_err(dev, "dpni_get_port_mac_addr() failed\n");
2757 		return err;
2758 	}
2759 
2760 	/* Get DPNI attributes address, if any */
2761 	err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
2762 					dpni_mac_addr);
2763 	if (err) {
2764 		dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
2765 		return err;
2766 	}
2767 
2768 	/* First check if firmware has any address configured by bootloader */
2769 	if (!is_zero_ether_addr(mac_addr)) {
2770 		/* If the DPMAC addr != DPNI addr, update it */
2771 		if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
2772 			err = dpni_set_primary_mac_addr(priv->mc_io, 0,
2773 							priv->mc_token,
2774 							mac_addr);
2775 			if (err) {
2776 				dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
2777 				return err;
2778 			}
2779 		}
2780 		memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len);
2781 	} else if (is_zero_ether_addr(dpni_mac_addr)) {
2782 		/* No MAC address configured, fill in net_dev->dev_addr
2783 		 * with a random one
2784 		 */
2785 		eth_hw_addr_random(net_dev);
2786 		dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
2787 
2788 		err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
2789 						net_dev->dev_addr);
2790 		if (err) {
2791 			dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
2792 			return err;
2793 		}
2794 
2795 		/* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
2796 		 * practical purposes, this will be our "permanent" mac address,
2797 		 * at least until the next reboot. This move will also permit
2798 		 * register_netdevice() to properly fill up net_dev->perm_addr.
2799 		 */
2800 		net_dev->addr_assign_type = NET_ADDR_PERM;
2801 	} else {
2802 		/* NET_ADDR_PERM is default, all we have to do is
2803 		 * fill in the device addr.
2804 		 */
2805 		memcpy(net_dev->dev_addr, dpni_mac_addr, net_dev->addr_len);
2806 	}
2807 
2808 	return 0;
2809 }
2810 
2811 static int netdev_init(struct net_device *net_dev)
2812 {
2813 	struct device *dev = net_dev->dev.parent;
2814 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2815 	u32 options = priv->dpni_attrs.options;
2816 	u64 supported = 0, not_supported = 0;
2817 	u8 bcast_addr[ETH_ALEN];
2818 	u8 num_queues;
2819 	int err;
2820 
2821 	net_dev->netdev_ops = &dpaa2_eth_ops;
2822 	net_dev->ethtool_ops = &dpaa2_ethtool_ops;
2823 
2824 	err = set_mac_addr(priv);
2825 	if (err)
2826 		return err;
2827 
2828 	/* Explicitly add the broadcast address to the MAC filtering table */
2829 	eth_broadcast_addr(bcast_addr);
2830 	err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
2831 	if (err) {
2832 		dev_err(dev, "dpni_add_mac_addr() failed\n");
2833 		return err;
2834 	}
2835 
2836 	/* Set MTU upper limit; lower limit is 68B (default value) */
2837 	net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
2838 	err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
2839 					DPAA2_ETH_MFL);
2840 	if (err) {
2841 		dev_err(dev, "dpni_set_max_frame_length() failed\n");
2842 		return err;
2843 	}
2844 
2845 	/* Set actual number of queues in the net device */
2846 	num_queues = dpaa2_eth_queue_count(priv);
2847 	err = netif_set_real_num_tx_queues(net_dev, num_queues);
2848 	if (err) {
2849 		dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
2850 		return err;
2851 	}
2852 	err = netif_set_real_num_rx_queues(net_dev, num_queues);
2853 	if (err) {
2854 		dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
2855 		return err;
2856 	}
2857 
2858 	/* Capabilities listing */
2859 	supported |= IFF_LIVE_ADDR_CHANGE;
2860 
2861 	if (options & DPNI_OPT_NO_MAC_FILTER)
2862 		not_supported |= IFF_UNICAST_FLT;
2863 	else
2864 		supported |= IFF_UNICAST_FLT;
2865 
2866 	net_dev->priv_flags |= supported;
2867 	net_dev->priv_flags &= ~not_supported;
2868 
2869 	/* Features */
2870 	net_dev->features = NETIF_F_RXCSUM |
2871 			    NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2872 			    NETIF_F_SG | NETIF_F_HIGHDMA |
2873 			    NETIF_F_LLTX;
2874 	net_dev->hw_features = net_dev->features;
2875 
2876 	return 0;
2877 }
2878 
2879 static int poll_link_state(void *arg)
2880 {
2881 	struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
2882 	int err;
2883 
2884 	while (!kthread_should_stop()) {
2885 		err = link_state_update(priv);
2886 		if (unlikely(err))
2887 			return err;
2888 
2889 		msleep(DPAA2_ETH_LINK_STATE_REFRESH);
2890 	}
2891 
2892 	return 0;
2893 }
2894 
2895 static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
2896 {
2897 	u32 status = ~0;
2898 	struct device *dev = (struct device *)arg;
2899 	struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
2900 	struct net_device *net_dev = dev_get_drvdata(dev);
2901 	int err;
2902 
2903 	err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
2904 				  DPNI_IRQ_INDEX, &status);
2905 	if (unlikely(err)) {
2906 		netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
2907 		return IRQ_HANDLED;
2908 	}
2909 
2910 	if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
2911 		link_state_update(netdev_priv(net_dev));
2912 
2913 	return IRQ_HANDLED;
2914 }
2915 
2916 static int setup_irqs(struct fsl_mc_device *ls_dev)
2917 {
2918 	int err = 0;
2919 	struct fsl_mc_device_irq *irq;
2920 
2921 	err = fsl_mc_allocate_irqs(ls_dev);
2922 	if (err) {
2923 		dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
2924 		return err;
2925 	}
2926 
2927 	irq = ls_dev->irqs[0];
2928 	err = devm_request_threaded_irq(&ls_dev->dev, irq->msi_desc->irq,
2929 					NULL, dpni_irq0_handler_thread,
2930 					IRQF_NO_SUSPEND | IRQF_ONESHOT,
2931 					dev_name(&ls_dev->dev), &ls_dev->dev);
2932 	if (err < 0) {
2933 		dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
2934 		goto free_mc_irq;
2935 	}
2936 
2937 	err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
2938 				DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED);
2939 	if (err < 0) {
2940 		dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
2941 		goto free_irq;
2942 	}
2943 
2944 	err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
2945 				  DPNI_IRQ_INDEX, 1);
2946 	if (err < 0) {
2947 		dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
2948 		goto free_irq;
2949 	}
2950 
2951 	return 0;
2952 
2953 free_irq:
2954 	devm_free_irq(&ls_dev->dev, irq->msi_desc->irq, &ls_dev->dev);
2955 free_mc_irq:
2956 	fsl_mc_free_irqs(ls_dev);
2957 
2958 	return err;
2959 }
2960 
2961 static void add_ch_napi(struct dpaa2_eth_priv *priv)
2962 {
2963 	int i;
2964 	struct dpaa2_eth_channel *ch;
2965 
2966 	for (i = 0; i < priv->num_channels; i++) {
2967 		ch = priv->channel[i];
2968 		/* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
2969 		netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll,
2970 			       NAPI_POLL_WEIGHT);
2971 	}
2972 }
2973 
2974 static void del_ch_napi(struct dpaa2_eth_priv *priv)
2975 {
2976 	int i;
2977 	struct dpaa2_eth_channel *ch;
2978 
2979 	for (i = 0; i < priv->num_channels; i++) {
2980 		ch = priv->channel[i];
2981 		netif_napi_del(&ch->napi);
2982 	}
2983 }
2984 
2985 static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
2986 {
2987 	struct device *dev;
2988 	struct net_device *net_dev = NULL;
2989 	struct dpaa2_eth_priv *priv = NULL;
2990 	int err = 0;
2991 
2992 	dev = &dpni_dev->dev;
2993 
2994 	/* Net device */
2995 	net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_TX_QUEUES);
2996 	if (!net_dev) {
2997 		dev_err(dev, "alloc_etherdev_mq() failed\n");
2998 		return -ENOMEM;
2999 	}
3000 
3001 	SET_NETDEV_DEV(net_dev, dev);
3002 	dev_set_drvdata(dev, net_dev);
3003 
3004 	priv = netdev_priv(net_dev);
3005 	priv->net_dev = net_dev;
3006 
3007 	priv->iommu_domain = iommu_get_domain_for_dev(dev);
3008 
3009 	/* Obtain a MC portal */
3010 	err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
3011 				     &priv->mc_io);
3012 	if (err) {
3013 		if (err == -ENXIO)
3014 			err = -EPROBE_DEFER;
3015 		else
3016 			dev_err(dev, "MC portal allocation failed\n");
3017 		goto err_portal_alloc;
3018 	}
3019 
3020 	/* MC objects initialization and configuration */
3021 	err = setup_dpni(dpni_dev);
3022 	if (err)
3023 		goto err_dpni_setup;
3024 
3025 	err = setup_dpio(priv);
3026 	if (err)
3027 		goto err_dpio_setup;
3028 
3029 	setup_fqs(priv);
3030 
3031 	err = setup_dpbp(priv);
3032 	if (err)
3033 		goto err_dpbp_setup;
3034 
3035 	err = bind_dpni(priv);
3036 	if (err)
3037 		goto err_bind;
3038 
3039 	/* Add a NAPI context for each channel */
3040 	add_ch_napi(priv);
3041 
3042 	/* Percpu statistics */
3043 	priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
3044 	if (!priv->percpu_stats) {
3045 		dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
3046 		err = -ENOMEM;
3047 		goto err_alloc_percpu_stats;
3048 	}
3049 	priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
3050 	if (!priv->percpu_extras) {
3051 		dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
3052 		err = -ENOMEM;
3053 		goto err_alloc_percpu_extras;
3054 	}
3055 
3056 	err = netdev_init(net_dev);
3057 	if (err)
3058 		goto err_netdev_init;
3059 
3060 	/* Configure checksum offload based on current interface flags */
3061 	err = set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
3062 	if (err)
3063 		goto err_csum;
3064 
3065 	err = set_tx_csum(priv, !!(net_dev->features &
3066 				   (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
3067 	if (err)
3068 		goto err_csum;
3069 
3070 	err = alloc_rings(priv);
3071 	if (err)
3072 		goto err_alloc_rings;
3073 
3074 	err = setup_irqs(dpni_dev);
3075 	if (err) {
3076 		netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
3077 		priv->poll_thread = kthread_run(poll_link_state, priv,
3078 						"%s_poll_link", net_dev->name);
3079 		if (IS_ERR(priv->poll_thread)) {
3080 			dev_err(dev, "Error starting polling thread\n");
3081 			goto err_poll_thread;
3082 		}
3083 		priv->do_link_poll = true;
3084 	}
3085 
3086 	err = register_netdev(net_dev);
3087 	if (err < 0) {
3088 		dev_err(dev, "register_netdev() failed\n");
3089 		goto err_netdev_reg;
3090 	}
3091 
3092 	dev_info(dev, "Probed interface %s\n", net_dev->name);
3093 	return 0;
3094 
3095 err_netdev_reg:
3096 	if (priv->do_link_poll)
3097 		kthread_stop(priv->poll_thread);
3098 	else
3099 		fsl_mc_free_irqs(dpni_dev);
3100 err_poll_thread:
3101 	free_rings(priv);
3102 err_alloc_rings:
3103 err_csum:
3104 err_netdev_init:
3105 	free_percpu(priv->percpu_extras);
3106 err_alloc_percpu_extras:
3107 	free_percpu(priv->percpu_stats);
3108 err_alloc_percpu_stats:
3109 	del_ch_napi(priv);
3110 err_bind:
3111 	free_dpbp(priv);
3112 err_dpbp_setup:
3113 	free_dpio(priv);
3114 err_dpio_setup:
3115 	free_dpni(priv);
3116 err_dpni_setup:
3117 	fsl_mc_portal_free(priv->mc_io);
3118 err_portal_alloc:
3119 	dev_set_drvdata(dev, NULL);
3120 	free_netdev(net_dev);
3121 
3122 	return err;
3123 }
3124 
3125 static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
3126 {
3127 	struct device *dev;
3128 	struct net_device *net_dev;
3129 	struct dpaa2_eth_priv *priv;
3130 
3131 	dev = &ls_dev->dev;
3132 	net_dev = dev_get_drvdata(dev);
3133 	priv = netdev_priv(net_dev);
3134 
3135 	unregister_netdev(net_dev);
3136 
3137 	if (priv->do_link_poll)
3138 		kthread_stop(priv->poll_thread);
3139 	else
3140 		fsl_mc_free_irqs(ls_dev);
3141 
3142 	free_rings(priv);
3143 	free_percpu(priv->percpu_stats);
3144 	free_percpu(priv->percpu_extras);
3145 
3146 	del_ch_napi(priv);
3147 	free_dpbp(priv);
3148 	free_dpio(priv);
3149 	free_dpni(priv);
3150 
3151 	fsl_mc_portal_free(priv->mc_io);
3152 
3153 	free_netdev(net_dev);
3154 
3155 	dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
3156 
3157 	return 0;
3158 }
3159 
3160 static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
3161 	{
3162 		.vendor = FSL_MC_VENDOR_FREESCALE,
3163 		.obj_type = "dpni",
3164 	},
3165 	{ .vendor = 0x0 }
3166 };
3167 MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
3168 
3169 static struct fsl_mc_driver dpaa2_eth_driver = {
3170 	.driver = {
3171 		.name = KBUILD_MODNAME,
3172 		.owner = THIS_MODULE,
3173 	},
3174 	.probe = dpaa2_eth_probe,
3175 	.remove = dpaa2_eth_remove,
3176 	.match_id_table = dpaa2_eth_match_id_table
3177 };
3178 
3179 module_fsl_mc_driver(dpaa2_eth_driver);
3180