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