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 /* Total number of in-flight frames on ingress queues */
1247 static u32 ingress_fq_count(struct dpaa2_eth_priv *priv)
1248 {
1249 	struct dpaa2_eth_fq *fq;
1250 	u32 fcnt = 0, bcnt = 0, total = 0;
1251 	int i, err;
1252 
1253 	for (i = 0; i < priv->num_fqs; i++) {
1254 		fq = &priv->fq[i];
1255 		err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
1256 		if (err) {
1257 			netdev_warn(priv->net_dev, "query_fq_count failed");
1258 			break;
1259 		}
1260 		total += fcnt;
1261 	}
1262 
1263 	return total;
1264 }
1265 
1266 static void wait_for_fq_empty(struct dpaa2_eth_priv *priv)
1267 {
1268 	int retries = 10;
1269 	u32 pending;
1270 
1271 	do {
1272 		pending = ingress_fq_count(priv);
1273 		if (pending)
1274 			msleep(100);
1275 	} while (pending && --retries);
1276 }
1277 
1278 static int dpaa2_eth_stop(struct net_device *net_dev)
1279 {
1280 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1281 	int dpni_enabled = 0;
1282 	int retries = 10;
1283 
1284 	netif_tx_stop_all_queues(net_dev);
1285 	netif_carrier_off(net_dev);
1286 
1287 	/* On dpni_disable(), the MC firmware will:
1288 	 * - stop MAC Rx and wait for all Rx frames to be enqueued to software
1289 	 * - cut off WRIOP dequeues from egress FQs and wait until transmission
1290 	 * of all in flight Tx frames is finished (and corresponding Tx conf
1291 	 * frames are enqueued back to software)
1292 	 *
1293 	 * Before calling dpni_disable(), we wait for all Tx frames to arrive
1294 	 * on WRIOP. After it finishes, wait until all remaining frames on Rx
1295 	 * and Tx conf queues are consumed on NAPI poll.
1296 	 */
1297 	msleep(500);
1298 
1299 	do {
1300 		dpni_disable(priv->mc_io, 0, priv->mc_token);
1301 		dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
1302 		if (dpni_enabled)
1303 			/* Allow the hardware some slack */
1304 			msleep(100);
1305 	} while (dpni_enabled && --retries);
1306 	if (!retries) {
1307 		netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
1308 		/* Must go on and disable NAPI nonetheless, so we don't crash at
1309 		 * the next "ifconfig up"
1310 		 */
1311 	}
1312 
1313 	wait_for_fq_empty(priv);
1314 	disable_ch_napi(priv);
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);
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);
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 	int i;
1966 	struct dpaa2_eth_channel *ch;
1967 
1968 	/* deregister CDAN notifications and free channels */
1969 	for (i = 0; i < priv->num_channels; i++) {
1970 		ch = priv->channel[i];
1971 		dpaa2_io_service_deregister(ch->dpio, &ch->nctx);
1972 		free_channel(priv, ch);
1973 	}
1974 }
1975 
1976 static struct dpaa2_eth_channel *get_affine_channel(struct dpaa2_eth_priv *priv,
1977 						    int cpu)
1978 {
1979 	struct device *dev = priv->net_dev->dev.parent;
1980 	int i;
1981 
1982 	for (i = 0; i < priv->num_channels; i++)
1983 		if (priv->channel[i]->nctx.desired_cpu == cpu)
1984 			return priv->channel[i];
1985 
1986 	/* We should never get here. Issue a warning and return
1987 	 * the first channel, because it's still better than nothing
1988 	 */
1989 	dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
1990 
1991 	return priv->channel[0];
1992 }
1993 
1994 static void set_fq_affinity(struct dpaa2_eth_priv *priv)
1995 {
1996 	struct device *dev = priv->net_dev->dev.parent;
1997 	struct cpumask xps_mask;
1998 	struct dpaa2_eth_fq *fq;
1999 	int rx_cpu, txc_cpu;
2000 	int i, err;
2001 
2002 	/* For each FQ, pick one channel/CPU to deliver frames to.
2003 	 * This may well change at runtime, either through irqbalance or
2004 	 * through direct user intervention.
2005 	 */
2006 	rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
2007 
2008 	for (i = 0; i < priv->num_fqs; i++) {
2009 		fq = &priv->fq[i];
2010 		switch (fq->type) {
2011 		case DPAA2_RX_FQ:
2012 			fq->target_cpu = rx_cpu;
2013 			rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
2014 			if (rx_cpu >= nr_cpu_ids)
2015 				rx_cpu = cpumask_first(&priv->dpio_cpumask);
2016 			break;
2017 		case DPAA2_TX_CONF_FQ:
2018 			fq->target_cpu = txc_cpu;
2019 
2020 			/* Tell the stack to affine to txc_cpu the Tx queue
2021 			 * associated with the confirmation one
2022 			 */
2023 			cpumask_clear(&xps_mask);
2024 			cpumask_set_cpu(txc_cpu, &xps_mask);
2025 			err = netif_set_xps_queue(priv->net_dev, &xps_mask,
2026 						  fq->flowid);
2027 			if (err)
2028 				dev_err(dev, "Error setting XPS queue\n");
2029 
2030 			txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
2031 			if (txc_cpu >= nr_cpu_ids)
2032 				txc_cpu = cpumask_first(&priv->dpio_cpumask);
2033 			break;
2034 		default:
2035 			dev_err(dev, "Unknown FQ type: %d\n", fq->type);
2036 		}
2037 		fq->channel = get_affine_channel(priv, fq->target_cpu);
2038 	}
2039 }
2040 
2041 static void setup_fqs(struct dpaa2_eth_priv *priv)
2042 {
2043 	int i;
2044 
2045 	/* We have one TxConf FQ per Tx flow.
2046 	 * The number of Tx and Rx queues is the same.
2047 	 * Tx queues come first in the fq array.
2048 	 */
2049 	for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2050 		priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
2051 		priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
2052 		priv->fq[priv->num_fqs++].flowid = (u16)i;
2053 	}
2054 
2055 	for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2056 		priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
2057 		priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
2058 		priv->fq[priv->num_fqs++].flowid = (u16)i;
2059 	}
2060 
2061 	/* For each FQ, decide on which core to process incoming frames */
2062 	set_fq_affinity(priv);
2063 }
2064 
2065 /* Allocate and configure one buffer pool for each interface */
2066 static int setup_dpbp(struct dpaa2_eth_priv *priv)
2067 {
2068 	int err;
2069 	struct fsl_mc_device *dpbp_dev;
2070 	struct device *dev = priv->net_dev->dev.parent;
2071 	struct dpbp_attr dpbp_attrs;
2072 
2073 	err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
2074 				     &dpbp_dev);
2075 	if (err) {
2076 		if (err == -ENXIO)
2077 			err = -EPROBE_DEFER;
2078 		else
2079 			dev_err(dev, "DPBP device allocation failed\n");
2080 		return err;
2081 	}
2082 
2083 	priv->dpbp_dev = dpbp_dev;
2084 
2085 	err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id,
2086 			&dpbp_dev->mc_handle);
2087 	if (err) {
2088 		dev_err(dev, "dpbp_open() failed\n");
2089 		goto err_open;
2090 	}
2091 
2092 	err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
2093 	if (err) {
2094 		dev_err(dev, "dpbp_reset() failed\n");
2095 		goto err_reset;
2096 	}
2097 
2098 	err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
2099 	if (err) {
2100 		dev_err(dev, "dpbp_enable() failed\n");
2101 		goto err_enable;
2102 	}
2103 
2104 	err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
2105 				  &dpbp_attrs);
2106 	if (err) {
2107 		dev_err(dev, "dpbp_get_attributes() failed\n");
2108 		goto err_get_attr;
2109 	}
2110 	priv->bpid = dpbp_attrs.bpid;
2111 
2112 	return 0;
2113 
2114 err_get_attr:
2115 	dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
2116 err_enable:
2117 err_reset:
2118 	dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
2119 err_open:
2120 	fsl_mc_object_free(dpbp_dev);
2121 
2122 	return err;
2123 }
2124 
2125 static void free_dpbp(struct dpaa2_eth_priv *priv)
2126 {
2127 	drain_pool(priv);
2128 	dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2129 	dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2130 	fsl_mc_object_free(priv->dpbp_dev);
2131 }
2132 
2133 static int set_buffer_layout(struct dpaa2_eth_priv *priv)
2134 {
2135 	struct device *dev = priv->net_dev->dev.parent;
2136 	struct dpni_buffer_layout buf_layout = {0};
2137 	int err;
2138 
2139 	/* We need to check for WRIOP version 1.0.0, but depending on the MC
2140 	 * version, this number is not always provided correctly on rev1.
2141 	 * We need to check for both alternatives in this situation.
2142 	 */
2143 	if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
2144 	    priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
2145 		priv->rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
2146 	else
2147 		priv->rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
2148 
2149 	/* tx buffer */
2150 	buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
2151 	buf_layout.pass_timestamp = true;
2152 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
2153 			     DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
2154 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2155 				     DPNI_QUEUE_TX, &buf_layout);
2156 	if (err) {
2157 		dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
2158 		return err;
2159 	}
2160 
2161 	/* tx-confirm buffer */
2162 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
2163 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2164 				     DPNI_QUEUE_TX_CONFIRM, &buf_layout);
2165 	if (err) {
2166 		dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
2167 		return err;
2168 	}
2169 
2170 	/* Now that we've set our tx buffer layout, retrieve the minimum
2171 	 * required tx data offset.
2172 	 */
2173 	err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
2174 				      &priv->tx_data_offset);
2175 	if (err) {
2176 		dev_err(dev, "dpni_get_tx_data_offset() failed\n");
2177 		return err;
2178 	}
2179 
2180 	if ((priv->tx_data_offset % 64) != 0)
2181 		dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
2182 			 priv->tx_data_offset);
2183 
2184 	/* rx buffer */
2185 	buf_layout.pass_frame_status = true;
2186 	buf_layout.pass_parser_result = true;
2187 	buf_layout.data_align = priv->rx_buf_align;
2188 	buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
2189 	buf_layout.private_data_size = 0;
2190 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
2191 			     DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
2192 			     DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
2193 			     DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
2194 			     DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
2195 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2196 				     DPNI_QUEUE_RX, &buf_layout);
2197 	if (err) {
2198 		dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
2199 		return err;
2200 	}
2201 
2202 	return 0;
2203 }
2204 
2205 /* Configure the DPNI object this interface is associated with */
2206 static int setup_dpni(struct fsl_mc_device *ls_dev)
2207 {
2208 	struct device *dev = &ls_dev->dev;
2209 	struct dpaa2_eth_priv *priv;
2210 	struct net_device *net_dev;
2211 	int err;
2212 
2213 	net_dev = dev_get_drvdata(dev);
2214 	priv = netdev_priv(net_dev);
2215 
2216 	/* get a handle for the DPNI object */
2217 	err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
2218 	if (err) {
2219 		dev_err(dev, "dpni_open() failed\n");
2220 		return err;
2221 	}
2222 
2223 	/* Check if we can work with this DPNI object */
2224 	err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
2225 				   &priv->dpni_ver_minor);
2226 	if (err) {
2227 		dev_err(dev, "dpni_get_api_version() failed\n");
2228 		goto close;
2229 	}
2230 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
2231 		dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
2232 			priv->dpni_ver_major, priv->dpni_ver_minor,
2233 			DPNI_VER_MAJOR, DPNI_VER_MINOR);
2234 		err = -ENOTSUPP;
2235 		goto close;
2236 	}
2237 
2238 	ls_dev->mc_io = priv->mc_io;
2239 	ls_dev->mc_handle = priv->mc_token;
2240 
2241 	err = dpni_reset(priv->mc_io, 0, priv->mc_token);
2242 	if (err) {
2243 		dev_err(dev, "dpni_reset() failed\n");
2244 		goto close;
2245 	}
2246 
2247 	err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
2248 				  &priv->dpni_attrs);
2249 	if (err) {
2250 		dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
2251 		goto close;
2252 	}
2253 
2254 	err = set_buffer_layout(priv);
2255 	if (err)
2256 		goto close;
2257 
2258 	priv->cls_rules = devm_kzalloc(dev, sizeof(struct dpaa2_eth_cls_rule) *
2259 				       dpaa2_eth_fs_count(priv), GFP_KERNEL);
2260 	if (!priv->cls_rules)
2261 		goto close;
2262 
2263 	return 0;
2264 
2265 close:
2266 	dpni_close(priv->mc_io, 0, priv->mc_token);
2267 
2268 	return err;
2269 }
2270 
2271 static void free_dpni(struct dpaa2_eth_priv *priv)
2272 {
2273 	int err;
2274 
2275 	err = dpni_reset(priv->mc_io, 0, priv->mc_token);
2276 	if (err)
2277 		netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
2278 			    err);
2279 
2280 	dpni_close(priv->mc_io, 0, priv->mc_token);
2281 }
2282 
2283 static int setup_rx_flow(struct dpaa2_eth_priv *priv,
2284 			 struct dpaa2_eth_fq *fq)
2285 {
2286 	struct device *dev = priv->net_dev->dev.parent;
2287 	struct dpni_queue queue;
2288 	struct dpni_queue_id qid;
2289 	struct dpni_taildrop td;
2290 	int err;
2291 
2292 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2293 			     DPNI_QUEUE_RX, 0, fq->flowid, &queue, &qid);
2294 	if (err) {
2295 		dev_err(dev, "dpni_get_queue(RX) failed\n");
2296 		return err;
2297 	}
2298 
2299 	fq->fqid = qid.fqid;
2300 
2301 	queue.destination.id = fq->channel->dpcon_id;
2302 	queue.destination.type = DPNI_DEST_DPCON;
2303 	queue.destination.priority = 1;
2304 	queue.user_context = (u64)(uintptr_t)fq;
2305 	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
2306 			     DPNI_QUEUE_RX, 0, fq->flowid,
2307 			     DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
2308 			     &queue);
2309 	if (err) {
2310 		dev_err(dev, "dpni_set_queue(RX) failed\n");
2311 		return err;
2312 	}
2313 
2314 	td.enable = 1;
2315 	td.threshold = DPAA2_ETH_TAILDROP_THRESH;
2316 	err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token, DPNI_CP_QUEUE,
2317 				DPNI_QUEUE_RX, 0, fq->flowid, &td);
2318 	if (err) {
2319 		dev_err(dev, "dpni_set_threshold() failed\n");
2320 		return err;
2321 	}
2322 
2323 	return 0;
2324 }
2325 
2326 static int setup_tx_flow(struct dpaa2_eth_priv *priv,
2327 			 struct dpaa2_eth_fq *fq)
2328 {
2329 	struct device *dev = priv->net_dev->dev.parent;
2330 	struct dpni_queue queue;
2331 	struct dpni_queue_id qid;
2332 	int err;
2333 
2334 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2335 			     DPNI_QUEUE_TX, 0, fq->flowid, &queue, &qid);
2336 	if (err) {
2337 		dev_err(dev, "dpni_get_queue(TX) failed\n");
2338 		return err;
2339 	}
2340 
2341 	fq->tx_qdbin = qid.qdbin;
2342 
2343 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
2344 			     DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
2345 			     &queue, &qid);
2346 	if (err) {
2347 		dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
2348 		return err;
2349 	}
2350 
2351 	fq->fqid = qid.fqid;
2352 
2353 	queue.destination.id = fq->channel->dpcon_id;
2354 	queue.destination.type = DPNI_DEST_DPCON;
2355 	queue.destination.priority = 0;
2356 	queue.user_context = (u64)(uintptr_t)fq;
2357 	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
2358 			     DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
2359 			     DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
2360 			     &queue);
2361 	if (err) {
2362 		dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
2363 		return err;
2364 	}
2365 
2366 	return 0;
2367 }
2368 
2369 /* Supported header fields for Rx hash distribution key */
2370 static const struct dpaa2_eth_dist_fields dist_fields[] = {
2371 	{
2372 		/* L2 header */
2373 		.rxnfc_field = RXH_L2DA,
2374 		.cls_prot = NET_PROT_ETH,
2375 		.cls_field = NH_FLD_ETH_DA,
2376 		.size = 6,
2377 	}, {
2378 		.cls_prot = NET_PROT_ETH,
2379 		.cls_field = NH_FLD_ETH_SA,
2380 		.size = 6,
2381 	}, {
2382 		/* This is the last ethertype field parsed:
2383 		 * depending on frame format, it can be the MAC ethertype
2384 		 * or the VLAN etype.
2385 		 */
2386 		.cls_prot = NET_PROT_ETH,
2387 		.cls_field = NH_FLD_ETH_TYPE,
2388 		.size = 2,
2389 	}, {
2390 		/* VLAN header */
2391 		.rxnfc_field = RXH_VLAN,
2392 		.cls_prot = NET_PROT_VLAN,
2393 		.cls_field = NH_FLD_VLAN_TCI,
2394 		.size = 2,
2395 	}, {
2396 		/* IP header */
2397 		.rxnfc_field = RXH_IP_SRC,
2398 		.cls_prot = NET_PROT_IP,
2399 		.cls_field = NH_FLD_IP_SRC,
2400 		.size = 4,
2401 	}, {
2402 		.rxnfc_field = RXH_IP_DST,
2403 		.cls_prot = NET_PROT_IP,
2404 		.cls_field = NH_FLD_IP_DST,
2405 		.size = 4,
2406 	}, {
2407 		.rxnfc_field = RXH_L3_PROTO,
2408 		.cls_prot = NET_PROT_IP,
2409 		.cls_field = NH_FLD_IP_PROTO,
2410 		.size = 1,
2411 	}, {
2412 		/* Using UDP ports, this is functionally equivalent to raw
2413 		 * byte pairs from L4 header.
2414 		 */
2415 		.rxnfc_field = RXH_L4_B_0_1,
2416 		.cls_prot = NET_PROT_UDP,
2417 		.cls_field = NH_FLD_UDP_PORT_SRC,
2418 		.size = 2,
2419 	}, {
2420 		.rxnfc_field = RXH_L4_B_2_3,
2421 		.cls_prot = NET_PROT_UDP,
2422 		.cls_field = NH_FLD_UDP_PORT_DST,
2423 		.size = 2,
2424 	},
2425 };
2426 
2427 /* Configure the Rx hash key using the legacy API */
2428 static int config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2429 {
2430 	struct device *dev = priv->net_dev->dev.parent;
2431 	struct dpni_rx_tc_dist_cfg dist_cfg;
2432 	int err;
2433 
2434 	memset(&dist_cfg, 0, sizeof(dist_cfg));
2435 
2436 	dist_cfg.key_cfg_iova = key;
2437 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2438 	dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
2439 
2440 	err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token, 0, &dist_cfg);
2441 	if (err)
2442 		dev_err(dev, "dpni_set_rx_tc_dist failed\n");
2443 
2444 	return err;
2445 }
2446 
2447 /* Configure the Rx hash key using the new API */
2448 static int config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2449 {
2450 	struct device *dev = priv->net_dev->dev.parent;
2451 	struct dpni_rx_dist_cfg dist_cfg;
2452 	int err;
2453 
2454 	memset(&dist_cfg, 0, sizeof(dist_cfg));
2455 
2456 	dist_cfg.key_cfg_iova = key;
2457 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2458 	dist_cfg.enable = 1;
2459 
2460 	err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token, &dist_cfg);
2461 	if (err)
2462 		dev_err(dev, "dpni_set_rx_hash_dist failed\n");
2463 
2464 	return err;
2465 }
2466 
2467 /* Configure the Rx flow classification key */
2468 static int config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
2469 {
2470 	struct device *dev = priv->net_dev->dev.parent;
2471 	struct dpni_rx_dist_cfg dist_cfg;
2472 	int err;
2473 
2474 	memset(&dist_cfg, 0, sizeof(dist_cfg));
2475 
2476 	dist_cfg.key_cfg_iova = key;
2477 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
2478 	dist_cfg.enable = 1;
2479 
2480 	err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token, &dist_cfg);
2481 	if (err)
2482 		dev_err(dev, "dpni_set_rx_fs_dist failed\n");
2483 
2484 	return err;
2485 }
2486 
2487 /* Size of the Rx flow classification key */
2488 int dpaa2_eth_cls_key_size(void)
2489 {
2490 	int i, size = 0;
2491 
2492 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
2493 		size += dist_fields[i].size;
2494 
2495 	return size;
2496 }
2497 
2498 /* Offset of header field in Rx classification key */
2499 int dpaa2_eth_cls_fld_off(int prot, int field)
2500 {
2501 	int i, off = 0;
2502 
2503 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
2504 		if (dist_fields[i].cls_prot == prot &&
2505 		    dist_fields[i].cls_field == field)
2506 			return off;
2507 		off += dist_fields[i].size;
2508 	}
2509 
2510 	WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
2511 	return 0;
2512 }
2513 
2514 /* Set Rx distribution (hash or flow classification) key
2515  * flags is a combination of RXH_ bits
2516  */
2517 static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
2518 				  enum dpaa2_eth_rx_dist type, u64 flags)
2519 {
2520 	struct device *dev = net_dev->dev.parent;
2521 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2522 	struct dpkg_profile_cfg cls_cfg;
2523 	u32 rx_hash_fields = 0;
2524 	dma_addr_t key_iova;
2525 	u8 *dma_mem;
2526 	int i;
2527 	int err = 0;
2528 
2529 	memset(&cls_cfg, 0, sizeof(cls_cfg));
2530 
2531 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
2532 		struct dpkg_extract *key =
2533 			&cls_cfg.extracts[cls_cfg.num_extracts];
2534 
2535 		/* For Rx hashing key we set only the selected fields.
2536 		 * For Rx flow classification key we set all supported fields
2537 		 */
2538 		if (type == DPAA2_ETH_RX_DIST_HASH) {
2539 			if (!(flags & dist_fields[i].rxnfc_field))
2540 				continue;
2541 			rx_hash_fields |= dist_fields[i].rxnfc_field;
2542 		}
2543 
2544 		if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
2545 			dev_err(dev, "error adding key extraction rule, too many rules?\n");
2546 			return -E2BIG;
2547 		}
2548 
2549 		key->type = DPKG_EXTRACT_FROM_HDR;
2550 		key->extract.from_hdr.prot = dist_fields[i].cls_prot;
2551 		key->extract.from_hdr.type = DPKG_FULL_FIELD;
2552 		key->extract.from_hdr.field = dist_fields[i].cls_field;
2553 		cls_cfg.num_extracts++;
2554 	}
2555 
2556 	dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
2557 	if (!dma_mem)
2558 		return -ENOMEM;
2559 
2560 	err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
2561 	if (err) {
2562 		dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
2563 		goto free_key;
2564 	}
2565 
2566 	/* Prepare for setting the rx dist */
2567 	key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
2568 				  DMA_TO_DEVICE);
2569 	if (dma_mapping_error(dev, key_iova)) {
2570 		dev_err(dev, "DMA mapping failed\n");
2571 		err = -ENOMEM;
2572 		goto free_key;
2573 	}
2574 
2575 	if (type == DPAA2_ETH_RX_DIST_HASH) {
2576 		if (dpaa2_eth_has_legacy_dist(priv))
2577 			err = config_legacy_hash_key(priv, key_iova);
2578 		else
2579 			err = config_hash_key(priv, key_iova);
2580 	} else {
2581 		err = config_cls_key(priv, key_iova);
2582 	}
2583 
2584 	dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
2585 			 DMA_TO_DEVICE);
2586 	if (!err && type == DPAA2_ETH_RX_DIST_HASH)
2587 		priv->rx_hash_fields = rx_hash_fields;
2588 
2589 free_key:
2590 	kfree(dma_mem);
2591 	return err;
2592 }
2593 
2594 int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
2595 {
2596 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2597 
2598 	if (!dpaa2_eth_hash_enabled(priv))
2599 		return -EOPNOTSUPP;
2600 
2601 	return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, flags);
2602 }
2603 
2604 static int dpaa2_eth_set_cls(struct dpaa2_eth_priv *priv)
2605 {
2606 	struct device *dev = priv->net_dev->dev.parent;
2607 
2608 	/* Check if we actually support Rx flow classification */
2609 	if (dpaa2_eth_has_legacy_dist(priv)) {
2610 		dev_dbg(dev, "Rx cls not supported by current MC version\n");
2611 		return -EOPNOTSUPP;
2612 	}
2613 
2614 	if (priv->dpni_attrs.options & DPNI_OPT_NO_FS ||
2615 	    !(priv->dpni_attrs.options & DPNI_OPT_HAS_KEY_MASKING)) {
2616 		dev_dbg(dev, "Rx cls disabled in DPNI options\n");
2617 		return -EOPNOTSUPP;
2618 	}
2619 
2620 	if (!dpaa2_eth_hash_enabled(priv)) {
2621 		dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
2622 		return -EOPNOTSUPP;
2623 	}
2624 
2625 	priv->rx_cls_enabled = 1;
2626 
2627 	return dpaa2_eth_set_dist_key(priv->net_dev, DPAA2_ETH_RX_DIST_CLS, 0);
2628 }
2629 
2630 /* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
2631  * frame queues and channels
2632  */
2633 static int bind_dpni(struct dpaa2_eth_priv *priv)
2634 {
2635 	struct net_device *net_dev = priv->net_dev;
2636 	struct device *dev = net_dev->dev.parent;
2637 	struct dpni_pools_cfg pools_params;
2638 	struct dpni_error_cfg err_cfg;
2639 	int err = 0;
2640 	int i;
2641 
2642 	pools_params.num_dpbp = 1;
2643 	pools_params.pools[0].dpbp_id = priv->dpbp_dev->obj_desc.id;
2644 	pools_params.pools[0].backup_pool = 0;
2645 	pools_params.pools[0].buffer_size = DPAA2_ETH_RX_BUF_SIZE;
2646 	err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
2647 	if (err) {
2648 		dev_err(dev, "dpni_set_pools() failed\n");
2649 		return err;
2650 	}
2651 
2652 	/* have the interface implicitly distribute traffic based on
2653 	 * the default hash key
2654 	 */
2655 	err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
2656 	if (err && err != -EOPNOTSUPP)
2657 		dev_err(dev, "Failed to configure hashing\n");
2658 
2659 	/* Configure the flow classification key; it includes all
2660 	 * supported header fields and cannot be modified at runtime
2661 	 */
2662 	err = dpaa2_eth_set_cls(priv);
2663 	if (err && err != -EOPNOTSUPP)
2664 		dev_err(dev, "Failed to configure Rx classification key\n");
2665 
2666 	/* Configure handling of error frames */
2667 	err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
2668 	err_cfg.set_frame_annotation = 1;
2669 	err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
2670 	err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
2671 				       &err_cfg);
2672 	if (err) {
2673 		dev_err(dev, "dpni_set_errors_behavior failed\n");
2674 		return err;
2675 	}
2676 
2677 	/* Configure Rx and Tx conf queues to generate CDANs */
2678 	for (i = 0; i < priv->num_fqs; i++) {
2679 		switch (priv->fq[i].type) {
2680 		case DPAA2_RX_FQ:
2681 			err = setup_rx_flow(priv, &priv->fq[i]);
2682 			break;
2683 		case DPAA2_TX_CONF_FQ:
2684 			err = setup_tx_flow(priv, &priv->fq[i]);
2685 			break;
2686 		default:
2687 			dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
2688 			return -EINVAL;
2689 		}
2690 		if (err)
2691 			return err;
2692 	}
2693 
2694 	err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
2695 			    DPNI_QUEUE_TX, &priv->tx_qdid);
2696 	if (err) {
2697 		dev_err(dev, "dpni_get_qdid() failed\n");
2698 		return err;
2699 	}
2700 
2701 	return 0;
2702 }
2703 
2704 /* Allocate rings for storing incoming frame descriptors */
2705 static int alloc_rings(struct dpaa2_eth_priv *priv)
2706 {
2707 	struct net_device *net_dev = priv->net_dev;
2708 	struct device *dev = net_dev->dev.parent;
2709 	int i;
2710 
2711 	for (i = 0; i < priv->num_channels; i++) {
2712 		priv->channel[i]->store =
2713 			dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
2714 		if (!priv->channel[i]->store) {
2715 			netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
2716 			goto err_ring;
2717 		}
2718 	}
2719 
2720 	return 0;
2721 
2722 err_ring:
2723 	for (i = 0; i < priv->num_channels; i++) {
2724 		if (!priv->channel[i]->store)
2725 			break;
2726 		dpaa2_io_store_destroy(priv->channel[i]->store);
2727 	}
2728 
2729 	return -ENOMEM;
2730 }
2731 
2732 static void free_rings(struct dpaa2_eth_priv *priv)
2733 {
2734 	int i;
2735 
2736 	for (i = 0; i < priv->num_channels; i++)
2737 		dpaa2_io_store_destroy(priv->channel[i]->store);
2738 }
2739 
2740 static int set_mac_addr(struct dpaa2_eth_priv *priv)
2741 {
2742 	struct net_device *net_dev = priv->net_dev;
2743 	struct device *dev = net_dev->dev.parent;
2744 	u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
2745 	int err;
2746 
2747 	/* Get firmware address, if any */
2748 	err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
2749 	if (err) {
2750 		dev_err(dev, "dpni_get_port_mac_addr() failed\n");
2751 		return err;
2752 	}
2753 
2754 	/* Get DPNI attributes address, if any */
2755 	err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
2756 					dpni_mac_addr);
2757 	if (err) {
2758 		dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
2759 		return err;
2760 	}
2761 
2762 	/* First check if firmware has any address configured by bootloader */
2763 	if (!is_zero_ether_addr(mac_addr)) {
2764 		/* If the DPMAC addr != DPNI addr, update it */
2765 		if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
2766 			err = dpni_set_primary_mac_addr(priv->mc_io, 0,
2767 							priv->mc_token,
2768 							mac_addr);
2769 			if (err) {
2770 				dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
2771 				return err;
2772 			}
2773 		}
2774 		memcpy(net_dev->dev_addr, mac_addr, net_dev->addr_len);
2775 	} else if (is_zero_ether_addr(dpni_mac_addr)) {
2776 		/* No MAC address configured, fill in net_dev->dev_addr
2777 		 * with a random one
2778 		 */
2779 		eth_hw_addr_random(net_dev);
2780 		dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
2781 
2782 		err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
2783 						net_dev->dev_addr);
2784 		if (err) {
2785 			dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
2786 			return err;
2787 		}
2788 
2789 		/* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
2790 		 * practical purposes, this will be our "permanent" mac address,
2791 		 * at least until the next reboot. This move will also permit
2792 		 * register_netdevice() to properly fill up net_dev->perm_addr.
2793 		 */
2794 		net_dev->addr_assign_type = NET_ADDR_PERM;
2795 	} else {
2796 		/* NET_ADDR_PERM is default, all we have to do is
2797 		 * fill in the device addr.
2798 		 */
2799 		memcpy(net_dev->dev_addr, dpni_mac_addr, net_dev->addr_len);
2800 	}
2801 
2802 	return 0;
2803 }
2804 
2805 static int netdev_init(struct net_device *net_dev)
2806 {
2807 	struct device *dev = net_dev->dev.parent;
2808 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2809 	u32 options = priv->dpni_attrs.options;
2810 	u64 supported = 0, not_supported = 0;
2811 	u8 bcast_addr[ETH_ALEN];
2812 	u8 num_queues;
2813 	int err;
2814 
2815 	net_dev->netdev_ops = &dpaa2_eth_ops;
2816 	net_dev->ethtool_ops = &dpaa2_ethtool_ops;
2817 
2818 	err = set_mac_addr(priv);
2819 	if (err)
2820 		return err;
2821 
2822 	/* Explicitly add the broadcast address to the MAC filtering table */
2823 	eth_broadcast_addr(bcast_addr);
2824 	err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
2825 	if (err) {
2826 		dev_err(dev, "dpni_add_mac_addr() failed\n");
2827 		return err;
2828 	}
2829 
2830 	/* Set MTU upper limit; lower limit is 68B (default value) */
2831 	net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
2832 	err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
2833 					DPAA2_ETH_MFL);
2834 	if (err) {
2835 		dev_err(dev, "dpni_set_max_frame_length() failed\n");
2836 		return err;
2837 	}
2838 
2839 	/* Set actual number of queues in the net device */
2840 	num_queues = dpaa2_eth_queue_count(priv);
2841 	err = netif_set_real_num_tx_queues(net_dev, num_queues);
2842 	if (err) {
2843 		dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
2844 		return err;
2845 	}
2846 	err = netif_set_real_num_rx_queues(net_dev, num_queues);
2847 	if (err) {
2848 		dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
2849 		return err;
2850 	}
2851 
2852 	/* Capabilities listing */
2853 	supported |= IFF_LIVE_ADDR_CHANGE;
2854 
2855 	if (options & DPNI_OPT_NO_MAC_FILTER)
2856 		not_supported |= IFF_UNICAST_FLT;
2857 	else
2858 		supported |= IFF_UNICAST_FLT;
2859 
2860 	net_dev->priv_flags |= supported;
2861 	net_dev->priv_flags &= ~not_supported;
2862 
2863 	/* Features */
2864 	net_dev->features = NETIF_F_RXCSUM |
2865 			    NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2866 			    NETIF_F_SG | NETIF_F_HIGHDMA |
2867 			    NETIF_F_LLTX;
2868 	net_dev->hw_features = net_dev->features;
2869 
2870 	return 0;
2871 }
2872 
2873 static int poll_link_state(void *arg)
2874 {
2875 	struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
2876 	int err;
2877 
2878 	while (!kthread_should_stop()) {
2879 		err = link_state_update(priv);
2880 		if (unlikely(err))
2881 			return err;
2882 
2883 		msleep(DPAA2_ETH_LINK_STATE_REFRESH);
2884 	}
2885 
2886 	return 0;
2887 }
2888 
2889 static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
2890 {
2891 	u32 status = ~0;
2892 	struct device *dev = (struct device *)arg;
2893 	struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
2894 	struct net_device *net_dev = dev_get_drvdata(dev);
2895 	int err;
2896 
2897 	err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
2898 				  DPNI_IRQ_INDEX, &status);
2899 	if (unlikely(err)) {
2900 		netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
2901 		return IRQ_HANDLED;
2902 	}
2903 
2904 	if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
2905 		link_state_update(netdev_priv(net_dev));
2906 
2907 	return IRQ_HANDLED;
2908 }
2909 
2910 static int setup_irqs(struct fsl_mc_device *ls_dev)
2911 {
2912 	int err = 0;
2913 	struct fsl_mc_device_irq *irq;
2914 
2915 	err = fsl_mc_allocate_irqs(ls_dev);
2916 	if (err) {
2917 		dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
2918 		return err;
2919 	}
2920 
2921 	irq = ls_dev->irqs[0];
2922 	err = devm_request_threaded_irq(&ls_dev->dev, irq->msi_desc->irq,
2923 					NULL, dpni_irq0_handler_thread,
2924 					IRQF_NO_SUSPEND | IRQF_ONESHOT,
2925 					dev_name(&ls_dev->dev), &ls_dev->dev);
2926 	if (err < 0) {
2927 		dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
2928 		goto free_mc_irq;
2929 	}
2930 
2931 	err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
2932 				DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED);
2933 	if (err < 0) {
2934 		dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
2935 		goto free_irq;
2936 	}
2937 
2938 	err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
2939 				  DPNI_IRQ_INDEX, 1);
2940 	if (err < 0) {
2941 		dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
2942 		goto free_irq;
2943 	}
2944 
2945 	return 0;
2946 
2947 free_irq:
2948 	devm_free_irq(&ls_dev->dev, irq->msi_desc->irq, &ls_dev->dev);
2949 free_mc_irq:
2950 	fsl_mc_free_irqs(ls_dev);
2951 
2952 	return err;
2953 }
2954 
2955 static void add_ch_napi(struct dpaa2_eth_priv *priv)
2956 {
2957 	int i;
2958 	struct dpaa2_eth_channel *ch;
2959 
2960 	for (i = 0; i < priv->num_channels; i++) {
2961 		ch = priv->channel[i];
2962 		/* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
2963 		netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll,
2964 			       NAPI_POLL_WEIGHT);
2965 	}
2966 }
2967 
2968 static void del_ch_napi(struct dpaa2_eth_priv *priv)
2969 {
2970 	int i;
2971 	struct dpaa2_eth_channel *ch;
2972 
2973 	for (i = 0; i < priv->num_channels; i++) {
2974 		ch = priv->channel[i];
2975 		netif_napi_del(&ch->napi);
2976 	}
2977 }
2978 
2979 static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
2980 {
2981 	struct device *dev;
2982 	struct net_device *net_dev = NULL;
2983 	struct dpaa2_eth_priv *priv = NULL;
2984 	int err = 0;
2985 
2986 	dev = &dpni_dev->dev;
2987 
2988 	/* Net device */
2989 	net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_TX_QUEUES);
2990 	if (!net_dev) {
2991 		dev_err(dev, "alloc_etherdev_mq() failed\n");
2992 		return -ENOMEM;
2993 	}
2994 
2995 	SET_NETDEV_DEV(net_dev, dev);
2996 	dev_set_drvdata(dev, net_dev);
2997 
2998 	priv = netdev_priv(net_dev);
2999 	priv->net_dev = net_dev;
3000 
3001 	priv->iommu_domain = iommu_get_domain_for_dev(dev);
3002 
3003 	/* Obtain a MC portal */
3004 	err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
3005 				     &priv->mc_io);
3006 	if (err) {
3007 		if (err == -ENXIO)
3008 			err = -EPROBE_DEFER;
3009 		else
3010 			dev_err(dev, "MC portal allocation failed\n");
3011 		goto err_portal_alloc;
3012 	}
3013 
3014 	/* MC objects initialization and configuration */
3015 	err = setup_dpni(dpni_dev);
3016 	if (err)
3017 		goto err_dpni_setup;
3018 
3019 	err = setup_dpio(priv);
3020 	if (err)
3021 		goto err_dpio_setup;
3022 
3023 	setup_fqs(priv);
3024 
3025 	err = setup_dpbp(priv);
3026 	if (err)
3027 		goto err_dpbp_setup;
3028 
3029 	err = bind_dpni(priv);
3030 	if (err)
3031 		goto err_bind;
3032 
3033 	/* Add a NAPI context for each channel */
3034 	add_ch_napi(priv);
3035 
3036 	/* Percpu statistics */
3037 	priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
3038 	if (!priv->percpu_stats) {
3039 		dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
3040 		err = -ENOMEM;
3041 		goto err_alloc_percpu_stats;
3042 	}
3043 	priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
3044 	if (!priv->percpu_extras) {
3045 		dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
3046 		err = -ENOMEM;
3047 		goto err_alloc_percpu_extras;
3048 	}
3049 
3050 	err = netdev_init(net_dev);
3051 	if (err)
3052 		goto err_netdev_init;
3053 
3054 	/* Configure checksum offload based on current interface flags */
3055 	err = set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
3056 	if (err)
3057 		goto err_csum;
3058 
3059 	err = set_tx_csum(priv, !!(net_dev->features &
3060 				   (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
3061 	if (err)
3062 		goto err_csum;
3063 
3064 	err = alloc_rings(priv);
3065 	if (err)
3066 		goto err_alloc_rings;
3067 
3068 	err = setup_irqs(dpni_dev);
3069 	if (err) {
3070 		netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
3071 		priv->poll_thread = kthread_run(poll_link_state, priv,
3072 						"%s_poll_link", net_dev->name);
3073 		if (IS_ERR(priv->poll_thread)) {
3074 			dev_err(dev, "Error starting polling thread\n");
3075 			goto err_poll_thread;
3076 		}
3077 		priv->do_link_poll = true;
3078 	}
3079 
3080 	err = register_netdev(net_dev);
3081 	if (err < 0) {
3082 		dev_err(dev, "register_netdev() failed\n");
3083 		goto err_netdev_reg;
3084 	}
3085 
3086 	dev_info(dev, "Probed interface %s\n", net_dev->name);
3087 	return 0;
3088 
3089 err_netdev_reg:
3090 	if (priv->do_link_poll)
3091 		kthread_stop(priv->poll_thread);
3092 	else
3093 		fsl_mc_free_irqs(dpni_dev);
3094 err_poll_thread:
3095 	free_rings(priv);
3096 err_alloc_rings:
3097 err_csum:
3098 err_netdev_init:
3099 	free_percpu(priv->percpu_extras);
3100 err_alloc_percpu_extras:
3101 	free_percpu(priv->percpu_stats);
3102 err_alloc_percpu_stats:
3103 	del_ch_napi(priv);
3104 err_bind:
3105 	free_dpbp(priv);
3106 err_dpbp_setup:
3107 	free_dpio(priv);
3108 err_dpio_setup:
3109 	free_dpni(priv);
3110 err_dpni_setup:
3111 	fsl_mc_portal_free(priv->mc_io);
3112 err_portal_alloc:
3113 	dev_set_drvdata(dev, NULL);
3114 	free_netdev(net_dev);
3115 
3116 	return err;
3117 }
3118 
3119 static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
3120 {
3121 	struct device *dev;
3122 	struct net_device *net_dev;
3123 	struct dpaa2_eth_priv *priv;
3124 
3125 	dev = &ls_dev->dev;
3126 	net_dev = dev_get_drvdata(dev);
3127 	priv = netdev_priv(net_dev);
3128 
3129 	unregister_netdev(net_dev);
3130 
3131 	if (priv->do_link_poll)
3132 		kthread_stop(priv->poll_thread);
3133 	else
3134 		fsl_mc_free_irqs(ls_dev);
3135 
3136 	free_rings(priv);
3137 	free_percpu(priv->percpu_stats);
3138 	free_percpu(priv->percpu_extras);
3139 
3140 	del_ch_napi(priv);
3141 	free_dpbp(priv);
3142 	free_dpio(priv);
3143 	free_dpni(priv);
3144 
3145 	fsl_mc_portal_free(priv->mc_io);
3146 
3147 	free_netdev(net_dev);
3148 
3149 	dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
3150 
3151 	return 0;
3152 }
3153 
3154 static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
3155 	{
3156 		.vendor = FSL_MC_VENDOR_FREESCALE,
3157 		.obj_type = "dpni",
3158 	},
3159 	{ .vendor = 0x0 }
3160 };
3161 MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
3162 
3163 static struct fsl_mc_driver dpaa2_eth_driver = {
3164 	.driver = {
3165 		.name = KBUILD_MODNAME,
3166 		.owner = THIS_MODULE,
3167 	},
3168 	.probe = dpaa2_eth_probe,
3169 	.remove = dpaa2_eth_remove,
3170 	.match_id_table = dpaa2_eth_match_id_table
3171 };
3172 
3173 module_fsl_mc_driver(dpaa2_eth_driver);
3174