1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /* Copyright 2014-2016 Freescale Semiconductor Inc.
3  * Copyright 2016-2022 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/fsl/mc.h>
15 #include <linux/bpf.h>
16 #include <linux/bpf_trace.h>
17 #include <linux/fsl/ptp_qoriq.h>
18 #include <linux/ptp_classify.h>
19 #include <net/pkt_cls.h>
20 #include <net/sock.h>
21 #include <net/tso.h>
22 
23 #include "dpaa2-eth.h"
24 
25 /* CREATE_TRACE_POINTS only needs to be defined once. Other dpa files
26  * using trace events only need to #include <trace/events/sched.h>
27  */
28 #define CREATE_TRACE_POINTS
29 #include "dpaa2-eth-trace.h"
30 
31 MODULE_LICENSE("Dual BSD/GPL");
32 MODULE_AUTHOR("Freescale Semiconductor, Inc");
33 MODULE_DESCRIPTION("Freescale DPAA2 Ethernet Driver");
34 
35 struct ptp_qoriq *dpaa2_ptp;
36 EXPORT_SYMBOL(dpaa2_ptp);
37 
38 static void dpaa2_eth_detect_features(struct dpaa2_eth_priv *priv)
39 {
40 	priv->features = 0;
41 
42 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_PTP_ONESTEP_VER_MAJOR,
43 				   DPNI_PTP_ONESTEP_VER_MINOR) >= 0)
44 		priv->features |= DPAA2_ETH_FEATURE_ONESTEP_CFG_DIRECT;
45 }
46 
47 static void dpaa2_update_ptp_onestep_indirect(struct dpaa2_eth_priv *priv,
48 					      u32 offset, u8 udp)
49 {
50 	struct dpni_single_step_cfg cfg;
51 
52 	cfg.en = 1;
53 	cfg.ch_update = udp;
54 	cfg.offset = offset;
55 	cfg.peer_delay = 0;
56 
57 	if (dpni_set_single_step_cfg(priv->mc_io, 0, priv->mc_token, &cfg))
58 		WARN_ONCE(1, "Failed to set single step register");
59 }
60 
61 static void dpaa2_update_ptp_onestep_direct(struct dpaa2_eth_priv *priv,
62 					    u32 offset, u8 udp)
63 {
64 	u32 val = 0;
65 
66 	val = DPAA2_PTP_SINGLE_STEP_ENABLE |
67 	       DPAA2_PTP_SINGLE_CORRECTION_OFF(offset);
68 
69 	if (udp)
70 		val |= DPAA2_PTP_SINGLE_STEP_CH;
71 
72 	if (priv->onestep_reg_base)
73 		writel(val, priv->onestep_reg_base);
74 }
75 
76 static void dpaa2_ptp_onestep_reg_update_method(struct dpaa2_eth_priv *priv)
77 {
78 	struct device *dev = priv->net_dev->dev.parent;
79 	struct dpni_single_step_cfg ptp_cfg;
80 
81 	priv->dpaa2_set_onestep_params_cb = dpaa2_update_ptp_onestep_indirect;
82 
83 	if (!(priv->features & DPAA2_ETH_FEATURE_ONESTEP_CFG_DIRECT))
84 		return;
85 
86 	if (dpni_get_single_step_cfg(priv->mc_io, 0,
87 				     priv->mc_token, &ptp_cfg)) {
88 		dev_err(dev, "dpni_get_single_step_cfg cannot retrieve onestep reg, falling back to indirect update\n");
89 		return;
90 	}
91 
92 	if (!ptp_cfg.ptp_onestep_reg_base) {
93 		dev_err(dev, "1588 onestep reg not available, falling back to indirect update\n");
94 		return;
95 	}
96 
97 	priv->onestep_reg_base = ioremap(ptp_cfg.ptp_onestep_reg_base,
98 					 sizeof(u32));
99 	if (!priv->onestep_reg_base) {
100 		dev_err(dev, "1588 onestep reg cannot be mapped, falling back to indirect update\n");
101 		return;
102 	}
103 
104 	priv->dpaa2_set_onestep_params_cb = dpaa2_update_ptp_onestep_direct;
105 }
106 
107 static void *dpaa2_iova_to_virt(struct iommu_domain *domain,
108 				dma_addr_t iova_addr)
109 {
110 	phys_addr_t phys_addr;
111 
112 	phys_addr = domain ? iommu_iova_to_phys(domain, iova_addr) : iova_addr;
113 
114 	return phys_to_virt(phys_addr);
115 }
116 
117 static void dpaa2_eth_validate_rx_csum(struct dpaa2_eth_priv *priv,
118 				       u32 fd_status,
119 				       struct sk_buff *skb)
120 {
121 	skb_checksum_none_assert(skb);
122 
123 	/* HW checksum validation is disabled, nothing to do here */
124 	if (!(priv->net_dev->features & NETIF_F_RXCSUM))
125 		return;
126 
127 	/* Read checksum validation bits */
128 	if (!((fd_status & DPAA2_FAS_L3CV) &&
129 	      (fd_status & DPAA2_FAS_L4CV)))
130 		return;
131 
132 	/* Inform the stack there's no need to compute L3/L4 csum anymore */
133 	skb->ip_summed = CHECKSUM_UNNECESSARY;
134 }
135 
136 /* Free a received FD.
137  * Not to be used for Tx conf FDs or on any other paths.
138  */
139 static void dpaa2_eth_free_rx_fd(struct dpaa2_eth_priv *priv,
140 				 const struct dpaa2_fd *fd,
141 				 void *vaddr)
142 {
143 	struct device *dev = priv->net_dev->dev.parent;
144 	dma_addr_t addr = dpaa2_fd_get_addr(fd);
145 	u8 fd_format = dpaa2_fd_get_format(fd);
146 	struct dpaa2_sg_entry *sgt;
147 	void *sg_vaddr;
148 	int i;
149 
150 	/* If single buffer frame, just free the data buffer */
151 	if (fd_format == dpaa2_fd_single)
152 		goto free_buf;
153 	else if (fd_format != dpaa2_fd_sg)
154 		/* We don't support any other format */
155 		return;
156 
157 	/* For S/G frames, we first need to free all SG entries
158 	 * except the first one, which was taken care of already
159 	 */
160 	sgt = vaddr + dpaa2_fd_get_offset(fd);
161 	for (i = 1; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
162 		addr = dpaa2_sg_get_addr(&sgt[i]);
163 		sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
164 		dma_unmap_page(dev, addr, priv->rx_buf_size,
165 			       DMA_BIDIRECTIONAL);
166 
167 		free_pages((unsigned long)sg_vaddr, 0);
168 		if (dpaa2_sg_is_final(&sgt[i]))
169 			break;
170 	}
171 
172 free_buf:
173 	free_pages((unsigned long)vaddr, 0);
174 }
175 
176 /* Build a linear skb based on a single-buffer frame descriptor */
177 static struct sk_buff *dpaa2_eth_build_linear_skb(struct dpaa2_eth_channel *ch,
178 						  const struct dpaa2_fd *fd,
179 						  void *fd_vaddr)
180 {
181 	struct sk_buff *skb = NULL;
182 	u16 fd_offset = dpaa2_fd_get_offset(fd);
183 	u32 fd_length = dpaa2_fd_get_len(fd);
184 
185 	ch->buf_count--;
186 
187 	skb = build_skb(fd_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
188 	if (unlikely(!skb))
189 		return NULL;
190 
191 	skb_reserve(skb, fd_offset);
192 	skb_put(skb, fd_length);
193 
194 	return skb;
195 }
196 
197 /* Build a non linear (fragmented) skb based on a S/G table */
198 static struct sk_buff *dpaa2_eth_build_frag_skb(struct dpaa2_eth_priv *priv,
199 						struct dpaa2_eth_channel *ch,
200 						struct dpaa2_sg_entry *sgt)
201 {
202 	struct sk_buff *skb = NULL;
203 	struct device *dev = priv->net_dev->dev.parent;
204 	void *sg_vaddr;
205 	dma_addr_t sg_addr;
206 	u16 sg_offset;
207 	u32 sg_length;
208 	struct page *page, *head_page;
209 	int page_offset;
210 	int i;
211 
212 	for (i = 0; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
213 		struct dpaa2_sg_entry *sge = &sgt[i];
214 
215 		/* NOTE: We only support SG entries in dpaa2_sg_single format,
216 		 * but this is the only format we may receive from HW anyway
217 		 */
218 
219 		/* Get the address and length from the S/G entry */
220 		sg_addr = dpaa2_sg_get_addr(sge);
221 		sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, sg_addr);
222 		dma_unmap_page(dev, sg_addr, priv->rx_buf_size,
223 			       DMA_BIDIRECTIONAL);
224 
225 		sg_length = dpaa2_sg_get_len(sge);
226 
227 		if (i == 0) {
228 			/* We build the skb around the first data buffer */
229 			skb = build_skb(sg_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
230 			if (unlikely(!skb)) {
231 				/* Free the first SG entry now, since we already
232 				 * unmapped it and obtained the virtual address
233 				 */
234 				free_pages((unsigned long)sg_vaddr, 0);
235 
236 				/* We still need to subtract the buffers used
237 				 * by this FD from our software counter
238 				 */
239 				while (!dpaa2_sg_is_final(&sgt[i]) &&
240 				       i < DPAA2_ETH_MAX_SG_ENTRIES)
241 					i++;
242 				break;
243 			}
244 
245 			sg_offset = dpaa2_sg_get_offset(sge);
246 			skb_reserve(skb, sg_offset);
247 			skb_put(skb, sg_length);
248 		} else {
249 			/* Rest of the data buffers are stored as skb frags */
250 			page = virt_to_page(sg_vaddr);
251 			head_page = virt_to_head_page(sg_vaddr);
252 
253 			/* Offset in page (which may be compound).
254 			 * Data in subsequent SG entries is stored from the
255 			 * beginning of the buffer, so we don't need to add the
256 			 * sg_offset.
257 			 */
258 			page_offset = ((unsigned long)sg_vaddr &
259 				(PAGE_SIZE - 1)) +
260 				(page_address(page) - page_address(head_page));
261 
262 			skb_add_rx_frag(skb, i - 1, head_page, page_offset,
263 					sg_length, priv->rx_buf_size);
264 		}
265 
266 		if (dpaa2_sg_is_final(sge))
267 			break;
268 	}
269 
270 	WARN_ONCE(i == DPAA2_ETH_MAX_SG_ENTRIES, "Final bit not set in SGT");
271 
272 	/* Count all data buffers + SG table buffer */
273 	ch->buf_count -= i + 2;
274 
275 	return skb;
276 }
277 
278 /* Free buffers acquired from the buffer pool or which were meant to
279  * be released in the pool
280  */
281 static void dpaa2_eth_free_bufs(struct dpaa2_eth_priv *priv, u64 *buf_array,
282 				int count)
283 {
284 	struct device *dev = priv->net_dev->dev.parent;
285 	void *vaddr;
286 	int i;
287 
288 	for (i = 0; i < count; i++) {
289 		vaddr = dpaa2_iova_to_virt(priv->iommu_domain, buf_array[i]);
290 		dma_unmap_page(dev, buf_array[i], priv->rx_buf_size,
291 			       DMA_BIDIRECTIONAL);
292 		free_pages((unsigned long)vaddr, 0);
293 	}
294 }
295 
296 static void dpaa2_eth_recycle_buf(struct dpaa2_eth_priv *priv,
297 				  struct dpaa2_eth_channel *ch,
298 				  dma_addr_t addr)
299 {
300 	int retries = 0;
301 	int err;
302 
303 	ch->recycled_bufs[ch->recycled_bufs_cnt++] = addr;
304 	if (ch->recycled_bufs_cnt < DPAA2_ETH_BUFS_PER_CMD)
305 		return;
306 
307 	while ((err = dpaa2_io_service_release(ch->dpio, ch->bp->bpid,
308 					       ch->recycled_bufs,
309 					       ch->recycled_bufs_cnt)) == -EBUSY) {
310 		if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
311 			break;
312 		cpu_relax();
313 	}
314 
315 	if (err) {
316 		dpaa2_eth_free_bufs(priv, ch->recycled_bufs, ch->recycled_bufs_cnt);
317 		ch->buf_count -= ch->recycled_bufs_cnt;
318 	}
319 
320 	ch->recycled_bufs_cnt = 0;
321 }
322 
323 static int dpaa2_eth_xdp_flush(struct dpaa2_eth_priv *priv,
324 			       struct dpaa2_eth_fq *fq,
325 			       struct dpaa2_eth_xdp_fds *xdp_fds)
326 {
327 	int total_enqueued = 0, retries = 0, enqueued;
328 	struct dpaa2_eth_drv_stats *percpu_extras;
329 	int num_fds, err, max_retries;
330 	struct dpaa2_fd *fds;
331 
332 	percpu_extras = this_cpu_ptr(priv->percpu_extras);
333 
334 	/* try to enqueue all the FDs until the max number of retries is hit */
335 	fds = xdp_fds->fds;
336 	num_fds = xdp_fds->num;
337 	max_retries = num_fds * DPAA2_ETH_ENQUEUE_RETRIES;
338 	while (total_enqueued < num_fds && retries < max_retries) {
339 		err = priv->enqueue(priv, fq, &fds[total_enqueued],
340 				    0, num_fds - total_enqueued, &enqueued);
341 		if (err == -EBUSY) {
342 			percpu_extras->tx_portal_busy += ++retries;
343 			continue;
344 		}
345 		total_enqueued += enqueued;
346 	}
347 	xdp_fds->num = 0;
348 
349 	return total_enqueued;
350 }
351 
352 static void dpaa2_eth_xdp_tx_flush(struct dpaa2_eth_priv *priv,
353 				   struct dpaa2_eth_channel *ch,
354 				   struct dpaa2_eth_fq *fq)
355 {
356 	struct rtnl_link_stats64 *percpu_stats;
357 	struct dpaa2_fd *fds;
358 	int enqueued, i;
359 
360 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
361 
362 	// enqueue the array of XDP_TX frames
363 	enqueued = dpaa2_eth_xdp_flush(priv, fq, &fq->xdp_tx_fds);
364 
365 	/* update statistics */
366 	percpu_stats->tx_packets += enqueued;
367 	fds = fq->xdp_tx_fds.fds;
368 	for (i = 0; i < enqueued; i++) {
369 		percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
370 		ch->stats.xdp_tx++;
371 	}
372 	for (i = enqueued; i < fq->xdp_tx_fds.num; i++) {
373 		dpaa2_eth_recycle_buf(priv, ch, dpaa2_fd_get_addr(&fds[i]));
374 		percpu_stats->tx_errors++;
375 		ch->stats.xdp_tx_err++;
376 	}
377 	fq->xdp_tx_fds.num = 0;
378 }
379 
380 static void dpaa2_eth_xdp_enqueue(struct dpaa2_eth_priv *priv,
381 				  struct dpaa2_eth_channel *ch,
382 				  struct dpaa2_fd *fd,
383 				  void *buf_start, u16 queue_id)
384 {
385 	struct dpaa2_faead *faead;
386 	struct dpaa2_fd *dest_fd;
387 	struct dpaa2_eth_fq *fq;
388 	u32 ctrl, frc;
389 
390 	/* Mark the egress frame hardware annotation area as valid */
391 	frc = dpaa2_fd_get_frc(fd);
392 	dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
393 	dpaa2_fd_set_ctrl(fd, DPAA2_FD_CTRL_ASAL);
394 
395 	/* Instruct hardware to release the FD buffer directly into
396 	 * the buffer pool once transmission is completed, instead of
397 	 * sending a Tx confirmation frame to us
398 	 */
399 	ctrl = DPAA2_FAEAD_A4V | DPAA2_FAEAD_A2V | DPAA2_FAEAD_EBDDV;
400 	faead = dpaa2_get_faead(buf_start, false);
401 	faead->ctrl = cpu_to_le32(ctrl);
402 	faead->conf_fqid = 0;
403 
404 	fq = &priv->fq[queue_id];
405 	dest_fd = &fq->xdp_tx_fds.fds[fq->xdp_tx_fds.num++];
406 	memcpy(dest_fd, fd, sizeof(*dest_fd));
407 
408 	if (fq->xdp_tx_fds.num < DEV_MAP_BULK_SIZE)
409 		return;
410 
411 	dpaa2_eth_xdp_tx_flush(priv, ch, fq);
412 }
413 
414 static u32 dpaa2_eth_run_xdp(struct dpaa2_eth_priv *priv,
415 			     struct dpaa2_eth_channel *ch,
416 			     struct dpaa2_eth_fq *rx_fq,
417 			     struct dpaa2_fd *fd, void *vaddr)
418 {
419 	dma_addr_t addr = dpaa2_fd_get_addr(fd);
420 	struct bpf_prog *xdp_prog;
421 	struct xdp_buff xdp;
422 	u32 xdp_act = XDP_PASS;
423 	int err, offset;
424 
425 	xdp_prog = READ_ONCE(ch->xdp.prog);
426 	if (!xdp_prog)
427 		goto out;
428 
429 	offset = dpaa2_fd_get_offset(fd) - XDP_PACKET_HEADROOM;
430 	xdp_init_buff(&xdp, DPAA2_ETH_RX_BUF_RAW_SIZE - offset, &ch->xdp_rxq);
431 	xdp_prepare_buff(&xdp, vaddr + offset, XDP_PACKET_HEADROOM,
432 			 dpaa2_fd_get_len(fd), false);
433 
434 	xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
435 
436 	/* xdp.data pointer may have changed */
437 	dpaa2_fd_set_offset(fd, xdp.data - vaddr);
438 	dpaa2_fd_set_len(fd, xdp.data_end - xdp.data);
439 
440 	switch (xdp_act) {
441 	case XDP_PASS:
442 		break;
443 	case XDP_TX:
444 		dpaa2_eth_xdp_enqueue(priv, ch, fd, vaddr, rx_fq->flowid);
445 		break;
446 	default:
447 		bpf_warn_invalid_xdp_action(priv->net_dev, xdp_prog, xdp_act);
448 		fallthrough;
449 	case XDP_ABORTED:
450 		trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
451 		fallthrough;
452 	case XDP_DROP:
453 		dpaa2_eth_recycle_buf(priv, ch, addr);
454 		ch->stats.xdp_drop++;
455 		break;
456 	case XDP_REDIRECT:
457 		dma_unmap_page(priv->net_dev->dev.parent, addr,
458 			       priv->rx_buf_size, DMA_BIDIRECTIONAL);
459 		ch->buf_count--;
460 
461 		/* Allow redirect use of full headroom */
462 		xdp.data_hard_start = vaddr;
463 		xdp.frame_sz = DPAA2_ETH_RX_BUF_RAW_SIZE;
464 
465 		err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog);
466 		if (unlikely(err)) {
467 			addr = dma_map_page(priv->net_dev->dev.parent,
468 					    virt_to_page(vaddr), 0,
469 					    priv->rx_buf_size, DMA_BIDIRECTIONAL);
470 			if (unlikely(dma_mapping_error(priv->net_dev->dev.parent, addr))) {
471 				free_pages((unsigned long)vaddr, 0);
472 			} else {
473 				ch->buf_count++;
474 				dpaa2_eth_recycle_buf(priv, ch, addr);
475 			}
476 			ch->stats.xdp_drop++;
477 		} else {
478 			ch->stats.xdp_redirect++;
479 		}
480 		break;
481 	}
482 
483 	ch->xdp.res |= xdp_act;
484 out:
485 	return xdp_act;
486 }
487 
488 struct sk_buff *dpaa2_eth_alloc_skb(struct dpaa2_eth_priv *priv,
489 				    struct dpaa2_eth_channel *ch,
490 				    const struct dpaa2_fd *fd, u32 fd_length,
491 				    void *fd_vaddr)
492 {
493 	u16 fd_offset = dpaa2_fd_get_offset(fd);
494 	struct sk_buff *skb = NULL;
495 	unsigned int skb_len;
496 
497 	skb_len = fd_length + dpaa2_eth_needed_headroom(NULL);
498 
499 	skb = napi_alloc_skb(&ch->napi, skb_len);
500 	if (!skb)
501 		return NULL;
502 
503 	skb_reserve(skb, dpaa2_eth_needed_headroom(NULL));
504 	skb_put(skb, fd_length);
505 
506 	memcpy(skb->data, fd_vaddr + fd_offset, fd_length);
507 
508 	dpaa2_eth_recycle_buf(priv, ch, dpaa2_fd_get_addr(fd));
509 
510 	return skb;
511 }
512 
513 static struct sk_buff *dpaa2_eth_copybreak(struct dpaa2_eth_channel *ch,
514 					   const struct dpaa2_fd *fd,
515 					   void *fd_vaddr)
516 {
517 	struct dpaa2_eth_priv *priv = ch->priv;
518 	u32 fd_length = dpaa2_fd_get_len(fd);
519 
520 	if (fd_length > priv->rx_copybreak)
521 		return NULL;
522 
523 	return dpaa2_eth_alloc_skb(priv, ch, fd, fd_length, fd_vaddr);
524 }
525 
526 /* Main Rx frame processing routine */
527 static void dpaa2_eth_rx(struct dpaa2_eth_priv *priv,
528 			 struct dpaa2_eth_channel *ch,
529 			 const struct dpaa2_fd *fd,
530 			 struct dpaa2_eth_fq *fq)
531 {
532 	dma_addr_t addr = dpaa2_fd_get_addr(fd);
533 	u8 fd_format = dpaa2_fd_get_format(fd);
534 	void *vaddr;
535 	struct sk_buff *skb;
536 	struct rtnl_link_stats64 *percpu_stats;
537 	struct dpaa2_eth_drv_stats *percpu_extras;
538 	struct device *dev = priv->net_dev->dev.parent;
539 	struct dpaa2_fas *fas;
540 	void *buf_data;
541 	u32 status = 0;
542 	u32 xdp_act;
543 
544 	/* Tracing point */
545 	trace_dpaa2_rx_fd(priv->net_dev, fd);
546 
547 	vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
548 	dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
549 				DMA_BIDIRECTIONAL);
550 
551 	fas = dpaa2_get_fas(vaddr, false);
552 	prefetch(fas);
553 	buf_data = vaddr + dpaa2_fd_get_offset(fd);
554 	prefetch(buf_data);
555 
556 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
557 	percpu_extras = this_cpu_ptr(priv->percpu_extras);
558 
559 	if (fd_format == dpaa2_fd_single) {
560 		xdp_act = dpaa2_eth_run_xdp(priv, ch, fq, (struct dpaa2_fd *)fd, vaddr);
561 		if (xdp_act != XDP_PASS) {
562 			percpu_stats->rx_packets++;
563 			percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
564 			return;
565 		}
566 
567 		skb = dpaa2_eth_copybreak(ch, fd, vaddr);
568 		if (!skb) {
569 			dma_unmap_page(dev, addr, priv->rx_buf_size,
570 				       DMA_BIDIRECTIONAL);
571 			skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr);
572 		}
573 	} else if (fd_format == dpaa2_fd_sg) {
574 		WARN_ON(priv->xdp_prog);
575 
576 		dma_unmap_page(dev, addr, priv->rx_buf_size,
577 			       DMA_BIDIRECTIONAL);
578 		skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data);
579 		free_pages((unsigned long)vaddr, 0);
580 		percpu_extras->rx_sg_frames++;
581 		percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd);
582 	} else {
583 		/* We don't support any other format */
584 		goto err_frame_format;
585 	}
586 
587 	if (unlikely(!skb))
588 		goto err_build_skb;
589 
590 	prefetch(skb->data);
591 
592 	/* Get the timestamp value */
593 	if (priv->rx_tstamp) {
594 		struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
595 		__le64 *ts = dpaa2_get_ts(vaddr, false);
596 		u64 ns;
597 
598 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
599 
600 		ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
601 		shhwtstamps->hwtstamp = ns_to_ktime(ns);
602 	}
603 
604 	/* Check if we need to validate the L4 csum */
605 	if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) {
606 		status = le32_to_cpu(fas->status);
607 		dpaa2_eth_validate_rx_csum(priv, status, skb);
608 	}
609 
610 	skb->protocol = eth_type_trans(skb, priv->net_dev);
611 	skb_record_rx_queue(skb, fq->flowid);
612 
613 	percpu_stats->rx_packets++;
614 	percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
615 	ch->stats.bytes_per_cdan += dpaa2_fd_get_len(fd);
616 
617 	list_add_tail(&skb->list, ch->rx_list);
618 
619 	return;
620 
621 err_build_skb:
622 	dpaa2_eth_free_rx_fd(priv, fd, vaddr);
623 err_frame_format:
624 	percpu_stats->rx_dropped++;
625 }
626 
627 /* Processing of Rx frames received on the error FQ
628  * We check and print the error bits and then free the frame
629  */
630 static void dpaa2_eth_rx_err(struct dpaa2_eth_priv *priv,
631 			     struct dpaa2_eth_channel *ch,
632 			     const struct dpaa2_fd *fd,
633 			     struct dpaa2_eth_fq *fq __always_unused)
634 {
635 	struct device *dev = priv->net_dev->dev.parent;
636 	dma_addr_t addr = dpaa2_fd_get_addr(fd);
637 	u8 fd_format = dpaa2_fd_get_format(fd);
638 	struct rtnl_link_stats64 *percpu_stats;
639 	struct dpaa2_eth_trap_item *trap_item;
640 	struct dpaa2_fapr *fapr;
641 	struct sk_buff *skb;
642 	void *buf_data;
643 	void *vaddr;
644 
645 	vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
646 	dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
647 				DMA_BIDIRECTIONAL);
648 
649 	buf_data = vaddr + dpaa2_fd_get_offset(fd);
650 
651 	if (fd_format == dpaa2_fd_single) {
652 		dma_unmap_page(dev, addr, priv->rx_buf_size,
653 			       DMA_BIDIRECTIONAL);
654 		skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr);
655 	} else if (fd_format == dpaa2_fd_sg) {
656 		dma_unmap_page(dev, addr, priv->rx_buf_size,
657 			       DMA_BIDIRECTIONAL);
658 		skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data);
659 		free_pages((unsigned long)vaddr, 0);
660 	} else {
661 		/* We don't support any other format */
662 		dpaa2_eth_free_rx_fd(priv, fd, vaddr);
663 		goto err_frame_format;
664 	}
665 
666 	fapr = dpaa2_get_fapr(vaddr, false);
667 	trap_item = dpaa2_eth_dl_get_trap(priv, fapr);
668 	if (trap_item)
669 		devlink_trap_report(priv->devlink, skb, trap_item->trap_ctx,
670 				    &priv->devlink_port, NULL);
671 	consume_skb(skb);
672 
673 err_frame_format:
674 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
675 	percpu_stats->rx_errors++;
676 	ch->buf_count--;
677 }
678 
679 /* Consume all frames pull-dequeued into the store. This is the simplest way to
680  * make sure we don't accidentally issue another volatile dequeue which would
681  * overwrite (leak) frames already in the store.
682  *
683  * Observance of NAPI budget is not our concern, leaving that to the caller.
684  */
685 static int dpaa2_eth_consume_frames(struct dpaa2_eth_channel *ch,
686 				    struct dpaa2_eth_fq **src)
687 {
688 	struct dpaa2_eth_priv *priv = ch->priv;
689 	struct dpaa2_eth_fq *fq = NULL;
690 	struct dpaa2_dq *dq;
691 	const struct dpaa2_fd *fd;
692 	int cleaned = 0, retries = 0;
693 	int is_last;
694 
695 	do {
696 		dq = dpaa2_io_store_next(ch->store, &is_last);
697 		if (unlikely(!dq)) {
698 			/* If we're here, we *must* have placed a
699 			 * volatile dequeue comnmand, so keep reading through
700 			 * the store until we get some sort of valid response
701 			 * token (either a valid frame or an "empty dequeue")
702 			 */
703 			if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES) {
704 				netdev_err_once(priv->net_dev,
705 						"Unable to read a valid dequeue response\n");
706 				return -ETIMEDOUT;
707 			}
708 			continue;
709 		}
710 
711 		fd = dpaa2_dq_fd(dq);
712 		fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq);
713 
714 		fq->consume(priv, ch, fd, fq);
715 		cleaned++;
716 		retries = 0;
717 	} while (!is_last);
718 
719 	if (!cleaned)
720 		return 0;
721 
722 	fq->stats.frames += cleaned;
723 	ch->stats.frames += cleaned;
724 	ch->stats.frames_per_cdan += cleaned;
725 
726 	/* A dequeue operation only pulls frames from a single queue
727 	 * into the store. Return the frame queue as an out param.
728 	 */
729 	if (src)
730 		*src = fq;
731 
732 	return cleaned;
733 }
734 
735 static int dpaa2_eth_ptp_parse(struct sk_buff *skb,
736 			       u8 *msgtype, u8 *twostep, u8 *udp,
737 			       u16 *correction_offset,
738 			       u16 *origintimestamp_offset)
739 {
740 	unsigned int ptp_class;
741 	struct ptp_header *hdr;
742 	unsigned int type;
743 	u8 *base;
744 
745 	ptp_class = ptp_classify_raw(skb);
746 	if (ptp_class == PTP_CLASS_NONE)
747 		return -EINVAL;
748 
749 	hdr = ptp_parse_header(skb, ptp_class);
750 	if (!hdr)
751 		return -EINVAL;
752 
753 	*msgtype = ptp_get_msgtype(hdr, ptp_class);
754 	*twostep = hdr->flag_field[0] & 0x2;
755 
756 	type = ptp_class & PTP_CLASS_PMASK;
757 	if (type == PTP_CLASS_IPV4 ||
758 	    type == PTP_CLASS_IPV6)
759 		*udp = 1;
760 	else
761 		*udp = 0;
762 
763 	base = skb_mac_header(skb);
764 	*correction_offset = (u8 *)&hdr->correction - base;
765 	*origintimestamp_offset = (u8 *)hdr + sizeof(struct ptp_header) - base;
766 
767 	return 0;
768 }
769 
770 /* Configure the egress frame annotation for timestamp update */
771 static void dpaa2_eth_enable_tx_tstamp(struct dpaa2_eth_priv *priv,
772 				       struct dpaa2_fd *fd,
773 				       void *buf_start,
774 				       struct sk_buff *skb)
775 {
776 	struct ptp_tstamp origin_timestamp;
777 	u8 msgtype, twostep, udp;
778 	struct dpaa2_faead *faead;
779 	struct dpaa2_fas *fas;
780 	struct timespec64 ts;
781 	u16 offset1, offset2;
782 	u32 ctrl, frc;
783 	__le64 *ns;
784 	u8 *data;
785 
786 	/* Mark the egress frame annotation area as valid */
787 	frc = dpaa2_fd_get_frc(fd);
788 	dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
789 
790 	/* Set hardware annotation size */
791 	ctrl = dpaa2_fd_get_ctrl(fd);
792 	dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL);
793 
794 	/* enable UPD (update prepanded data) bit in FAEAD field of
795 	 * hardware frame annotation area
796 	 */
797 	ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD;
798 	faead = dpaa2_get_faead(buf_start, true);
799 	faead->ctrl = cpu_to_le32(ctrl);
800 
801 	if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
802 		if (dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
803 					&offset1, &offset2) ||
804 		    msgtype != PTP_MSGTYPE_SYNC || twostep) {
805 			WARN_ONCE(1, "Bad packet for one-step timestamping\n");
806 			return;
807 		}
808 
809 		/* Mark the frame annotation status as valid */
810 		frc = dpaa2_fd_get_frc(fd);
811 		dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FASV);
812 
813 		/* Mark the PTP flag for one step timestamping */
814 		fas = dpaa2_get_fas(buf_start, true);
815 		fas->status = cpu_to_le32(DPAA2_FAS_PTP);
816 
817 		dpaa2_ptp->caps.gettime64(&dpaa2_ptp->caps, &ts);
818 		ns = dpaa2_get_ts(buf_start, true);
819 		*ns = cpu_to_le64(timespec64_to_ns(&ts) /
820 				  DPAA2_PTP_CLK_PERIOD_NS);
821 
822 		/* Update current time to PTP message originTimestamp field */
823 		ns_to_ptp_tstamp(&origin_timestamp, le64_to_cpup(ns));
824 		data = skb_mac_header(skb);
825 		*(__be16 *)(data + offset2) = htons(origin_timestamp.sec_msb);
826 		*(__be32 *)(data + offset2 + 2) =
827 			htonl(origin_timestamp.sec_lsb);
828 		*(__be32 *)(data + offset2 + 6) = htonl(origin_timestamp.nsec);
829 
830 		if (priv->ptp_correction_off == offset1)
831 			return;
832 
833 		priv->dpaa2_set_onestep_params_cb(priv, offset1, udp);
834 		priv->ptp_correction_off = offset1;
835 
836 	}
837 }
838 
839 static void *dpaa2_eth_sgt_get(struct dpaa2_eth_priv *priv)
840 {
841 	struct dpaa2_eth_sgt_cache *sgt_cache;
842 	void *sgt_buf = NULL;
843 	int sgt_buf_size;
844 
845 	sgt_cache = this_cpu_ptr(priv->sgt_cache);
846 	sgt_buf_size = priv->tx_data_offset +
847 		DPAA2_ETH_SG_ENTRIES_MAX * sizeof(struct dpaa2_sg_entry);
848 
849 	if (sgt_cache->count == 0)
850 		sgt_buf = napi_alloc_frag_align(sgt_buf_size, DPAA2_ETH_TX_BUF_ALIGN);
851 	else
852 		sgt_buf = sgt_cache->buf[--sgt_cache->count];
853 	if (!sgt_buf)
854 		return NULL;
855 
856 	memset(sgt_buf, 0, sgt_buf_size);
857 
858 	return sgt_buf;
859 }
860 
861 static void dpaa2_eth_sgt_recycle(struct dpaa2_eth_priv *priv, void *sgt_buf)
862 {
863 	struct dpaa2_eth_sgt_cache *sgt_cache;
864 
865 	sgt_cache = this_cpu_ptr(priv->sgt_cache);
866 	if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE)
867 		skb_free_frag(sgt_buf);
868 	else
869 		sgt_cache->buf[sgt_cache->count++] = sgt_buf;
870 }
871 
872 /* Create a frame descriptor based on a fragmented skb */
873 static int dpaa2_eth_build_sg_fd(struct dpaa2_eth_priv *priv,
874 				 struct sk_buff *skb,
875 				 struct dpaa2_fd *fd,
876 				 void **swa_addr)
877 {
878 	struct device *dev = priv->net_dev->dev.parent;
879 	void *sgt_buf = NULL;
880 	dma_addr_t addr;
881 	int nr_frags = skb_shinfo(skb)->nr_frags;
882 	struct dpaa2_sg_entry *sgt;
883 	int i, err;
884 	int sgt_buf_size;
885 	struct scatterlist *scl, *crt_scl;
886 	int num_sg;
887 	int num_dma_bufs;
888 	struct dpaa2_eth_swa *swa;
889 
890 	/* Create and map scatterlist.
891 	 * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have
892 	 * to go beyond nr_frags+1.
893 	 * Note: We don't support chained scatterlists
894 	 */
895 	if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1))
896 		return -EINVAL;
897 
898 	scl = kmalloc_array(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC);
899 	if (unlikely(!scl))
900 		return -ENOMEM;
901 
902 	sg_init_table(scl, nr_frags + 1);
903 	num_sg = skb_to_sgvec(skb, scl, 0, skb->len);
904 	if (unlikely(num_sg < 0)) {
905 		err = -ENOMEM;
906 		goto dma_map_sg_failed;
907 	}
908 	num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
909 	if (unlikely(!num_dma_bufs)) {
910 		err = -ENOMEM;
911 		goto dma_map_sg_failed;
912 	}
913 
914 	/* Prepare the HW SGT structure */
915 	sgt_buf_size = priv->tx_data_offset +
916 		       sizeof(struct dpaa2_sg_entry) *  num_dma_bufs;
917 	sgt_buf = dpaa2_eth_sgt_get(priv);
918 	if (unlikely(!sgt_buf)) {
919 		err = -ENOMEM;
920 		goto sgt_buf_alloc_failed;
921 	}
922 
923 	sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
924 
925 	/* Fill in the HW SGT structure.
926 	 *
927 	 * sgt_buf is zeroed out, so the following fields are implicit
928 	 * in all sgt entries:
929 	 *   - offset is 0
930 	 *   - format is 'dpaa2_sg_single'
931 	 */
932 	for_each_sg(scl, crt_scl, num_dma_bufs, i) {
933 		dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl));
934 		dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl));
935 	}
936 	dpaa2_sg_set_final(&sgt[i - 1], true);
937 
938 	/* Store the skb backpointer in the SGT buffer.
939 	 * Fit the scatterlist and the number of buffers alongside the
940 	 * skb backpointer in the software annotation area. We'll need
941 	 * all of them on Tx Conf.
942 	 */
943 	*swa_addr = (void *)sgt_buf;
944 	swa = (struct dpaa2_eth_swa *)sgt_buf;
945 	swa->type = DPAA2_ETH_SWA_SG;
946 	swa->sg.skb = skb;
947 	swa->sg.scl = scl;
948 	swa->sg.num_sg = num_sg;
949 	swa->sg.sgt_size = sgt_buf_size;
950 
951 	/* Separately map the SGT buffer */
952 	addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
953 	if (unlikely(dma_mapping_error(dev, addr))) {
954 		err = -ENOMEM;
955 		goto dma_map_single_failed;
956 	}
957 	memset(fd, 0, sizeof(struct dpaa2_fd));
958 	dpaa2_fd_set_offset(fd, priv->tx_data_offset);
959 	dpaa2_fd_set_format(fd, dpaa2_fd_sg);
960 	dpaa2_fd_set_addr(fd, addr);
961 	dpaa2_fd_set_len(fd, skb->len);
962 	dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
963 
964 	return 0;
965 
966 dma_map_single_failed:
967 	dpaa2_eth_sgt_recycle(priv, sgt_buf);
968 sgt_buf_alloc_failed:
969 	dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
970 dma_map_sg_failed:
971 	kfree(scl);
972 	return err;
973 }
974 
975 /* Create a SG frame descriptor based on a linear skb.
976  *
977  * This function is used on the Tx path when the skb headroom is not large
978  * enough for the HW requirements, thus instead of realloc-ing the skb we
979  * create a SG frame descriptor with only one entry.
980  */
981 static int dpaa2_eth_build_sg_fd_single_buf(struct dpaa2_eth_priv *priv,
982 					    struct sk_buff *skb,
983 					    struct dpaa2_fd *fd,
984 					    void **swa_addr)
985 {
986 	struct device *dev = priv->net_dev->dev.parent;
987 	struct dpaa2_sg_entry *sgt;
988 	struct dpaa2_eth_swa *swa;
989 	dma_addr_t addr, sgt_addr;
990 	void *sgt_buf = NULL;
991 	int sgt_buf_size;
992 	int err;
993 
994 	/* Prepare the HW SGT structure */
995 	sgt_buf_size = priv->tx_data_offset + sizeof(struct dpaa2_sg_entry);
996 	sgt_buf = dpaa2_eth_sgt_get(priv);
997 	if (unlikely(!sgt_buf))
998 		return -ENOMEM;
999 	sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
1000 
1001 	addr = dma_map_single(dev, skb->data, skb->len, DMA_BIDIRECTIONAL);
1002 	if (unlikely(dma_mapping_error(dev, addr))) {
1003 		err = -ENOMEM;
1004 		goto data_map_failed;
1005 	}
1006 
1007 	/* Fill in the HW SGT structure */
1008 	dpaa2_sg_set_addr(sgt, addr);
1009 	dpaa2_sg_set_len(sgt, skb->len);
1010 	dpaa2_sg_set_final(sgt, true);
1011 
1012 	/* Store the skb backpointer in the SGT buffer */
1013 	*swa_addr = (void *)sgt_buf;
1014 	swa = (struct dpaa2_eth_swa *)sgt_buf;
1015 	swa->type = DPAA2_ETH_SWA_SINGLE;
1016 	swa->single.skb = skb;
1017 	swa->single.sgt_size = sgt_buf_size;
1018 
1019 	/* Separately map the SGT buffer */
1020 	sgt_addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
1021 	if (unlikely(dma_mapping_error(dev, sgt_addr))) {
1022 		err = -ENOMEM;
1023 		goto sgt_map_failed;
1024 	}
1025 
1026 	memset(fd, 0, sizeof(struct dpaa2_fd));
1027 	dpaa2_fd_set_offset(fd, priv->tx_data_offset);
1028 	dpaa2_fd_set_format(fd, dpaa2_fd_sg);
1029 	dpaa2_fd_set_addr(fd, sgt_addr);
1030 	dpaa2_fd_set_len(fd, skb->len);
1031 	dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
1032 
1033 	return 0;
1034 
1035 sgt_map_failed:
1036 	dma_unmap_single(dev, addr, skb->len, DMA_BIDIRECTIONAL);
1037 data_map_failed:
1038 	dpaa2_eth_sgt_recycle(priv, sgt_buf);
1039 
1040 	return err;
1041 }
1042 
1043 /* Create a frame descriptor based on a linear skb */
1044 static int dpaa2_eth_build_single_fd(struct dpaa2_eth_priv *priv,
1045 				     struct sk_buff *skb,
1046 				     struct dpaa2_fd *fd,
1047 				     void **swa_addr)
1048 {
1049 	struct device *dev = priv->net_dev->dev.parent;
1050 	u8 *buffer_start, *aligned_start;
1051 	struct dpaa2_eth_swa *swa;
1052 	dma_addr_t addr;
1053 
1054 	buffer_start = skb->data - dpaa2_eth_needed_headroom(skb);
1055 
1056 	/* If there's enough room to align the FD address, do it.
1057 	 * It will help hardware optimize accesses.
1058 	 */
1059 	aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
1060 				  DPAA2_ETH_TX_BUF_ALIGN);
1061 	if (aligned_start >= skb->head)
1062 		buffer_start = aligned_start;
1063 
1064 	/* Store a backpointer to the skb at the beginning of the buffer
1065 	 * (in the private data area) such that we can release it
1066 	 * on Tx confirm
1067 	 */
1068 	*swa_addr = (void *)buffer_start;
1069 	swa = (struct dpaa2_eth_swa *)buffer_start;
1070 	swa->type = DPAA2_ETH_SWA_SINGLE;
1071 	swa->single.skb = skb;
1072 
1073 	addr = dma_map_single(dev, buffer_start,
1074 			      skb_tail_pointer(skb) - buffer_start,
1075 			      DMA_BIDIRECTIONAL);
1076 	if (unlikely(dma_mapping_error(dev, addr)))
1077 		return -ENOMEM;
1078 
1079 	memset(fd, 0, sizeof(struct dpaa2_fd));
1080 	dpaa2_fd_set_addr(fd, addr);
1081 	dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start));
1082 	dpaa2_fd_set_len(fd, skb->len);
1083 	dpaa2_fd_set_format(fd, dpaa2_fd_single);
1084 	dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
1085 
1086 	return 0;
1087 }
1088 
1089 /* FD freeing routine on the Tx path
1090  *
1091  * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb
1092  * back-pointed to is also freed.
1093  * This can be called either from dpaa2_eth_tx_conf() or on the error path of
1094  * dpaa2_eth_tx().
1095  */
1096 static void dpaa2_eth_free_tx_fd(struct dpaa2_eth_priv *priv,
1097 				 struct dpaa2_eth_fq *fq,
1098 				 const struct dpaa2_fd *fd, bool in_napi)
1099 {
1100 	struct device *dev = priv->net_dev->dev.parent;
1101 	dma_addr_t fd_addr, sg_addr;
1102 	struct sk_buff *skb = NULL;
1103 	unsigned char *buffer_start;
1104 	struct dpaa2_eth_swa *swa;
1105 	u8 fd_format = dpaa2_fd_get_format(fd);
1106 	u32 fd_len = dpaa2_fd_get_len(fd);
1107 	struct dpaa2_sg_entry *sgt;
1108 	int should_free_skb = 1;
1109 	void *tso_hdr;
1110 	int i;
1111 
1112 	fd_addr = dpaa2_fd_get_addr(fd);
1113 	buffer_start = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr);
1114 	swa = (struct dpaa2_eth_swa *)buffer_start;
1115 
1116 	if (fd_format == dpaa2_fd_single) {
1117 		if (swa->type == DPAA2_ETH_SWA_SINGLE) {
1118 			skb = swa->single.skb;
1119 			/* Accessing the skb buffer is safe before dma unmap,
1120 			 * because we didn't map the actual skb shell.
1121 			 */
1122 			dma_unmap_single(dev, fd_addr,
1123 					 skb_tail_pointer(skb) - buffer_start,
1124 					 DMA_BIDIRECTIONAL);
1125 		} else {
1126 			WARN_ONCE(swa->type != DPAA2_ETH_SWA_XDP, "Wrong SWA type");
1127 			dma_unmap_single(dev, fd_addr, swa->xdp.dma_size,
1128 					 DMA_BIDIRECTIONAL);
1129 		}
1130 	} else if (fd_format == dpaa2_fd_sg) {
1131 		if (swa->type == DPAA2_ETH_SWA_SG) {
1132 			skb = swa->sg.skb;
1133 
1134 			/* Unmap the scatterlist */
1135 			dma_unmap_sg(dev, swa->sg.scl, swa->sg.num_sg,
1136 				     DMA_BIDIRECTIONAL);
1137 			kfree(swa->sg.scl);
1138 
1139 			/* Unmap the SGT buffer */
1140 			dma_unmap_single(dev, fd_addr, swa->sg.sgt_size,
1141 					 DMA_BIDIRECTIONAL);
1142 		} else if (swa->type == DPAA2_ETH_SWA_SW_TSO) {
1143 			skb = swa->tso.skb;
1144 
1145 			sgt = (struct dpaa2_sg_entry *)(buffer_start +
1146 							priv->tx_data_offset);
1147 
1148 			/* Unmap the SGT buffer */
1149 			dma_unmap_single(dev, fd_addr, swa->tso.sgt_size,
1150 					 DMA_BIDIRECTIONAL);
1151 
1152 			/* Unmap and free the header */
1153 			tso_hdr = dpaa2_iova_to_virt(priv->iommu_domain, dpaa2_sg_get_addr(sgt));
1154 			dma_unmap_single(dev, dpaa2_sg_get_addr(sgt), TSO_HEADER_SIZE,
1155 					 DMA_TO_DEVICE);
1156 			kfree(tso_hdr);
1157 
1158 			/* Unmap the other SG entries for the data */
1159 			for (i = 1; i < swa->tso.num_sg; i++)
1160 				dma_unmap_single(dev, dpaa2_sg_get_addr(&sgt[i]),
1161 						 dpaa2_sg_get_len(&sgt[i]), DMA_TO_DEVICE);
1162 
1163 			if (!swa->tso.is_last_fd)
1164 				should_free_skb = 0;
1165 		} else {
1166 			skb = swa->single.skb;
1167 
1168 			/* Unmap the SGT Buffer */
1169 			dma_unmap_single(dev, fd_addr, swa->single.sgt_size,
1170 					 DMA_BIDIRECTIONAL);
1171 
1172 			sgt = (struct dpaa2_sg_entry *)(buffer_start +
1173 							priv->tx_data_offset);
1174 			sg_addr = dpaa2_sg_get_addr(sgt);
1175 			dma_unmap_single(dev, sg_addr, skb->len, DMA_BIDIRECTIONAL);
1176 		}
1177 	} else {
1178 		netdev_dbg(priv->net_dev, "Invalid FD format\n");
1179 		return;
1180 	}
1181 
1182 	if (swa->type != DPAA2_ETH_SWA_XDP && in_napi) {
1183 		fq->dq_frames++;
1184 		fq->dq_bytes += fd_len;
1185 	}
1186 
1187 	if (swa->type == DPAA2_ETH_SWA_XDP) {
1188 		xdp_return_frame(swa->xdp.xdpf);
1189 		return;
1190 	}
1191 
1192 	/* Get the timestamp value */
1193 	if (swa->type != DPAA2_ETH_SWA_SW_TSO) {
1194 		if (skb->cb[0] == TX_TSTAMP) {
1195 			struct skb_shared_hwtstamps shhwtstamps;
1196 			__le64 *ts = dpaa2_get_ts(buffer_start, true);
1197 			u64 ns;
1198 
1199 			memset(&shhwtstamps, 0, sizeof(shhwtstamps));
1200 
1201 			ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
1202 			shhwtstamps.hwtstamp = ns_to_ktime(ns);
1203 			skb_tstamp_tx(skb, &shhwtstamps);
1204 		} else if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
1205 			mutex_unlock(&priv->onestep_tstamp_lock);
1206 		}
1207 	}
1208 
1209 	/* Free SGT buffer allocated on tx */
1210 	if (fd_format != dpaa2_fd_single)
1211 		dpaa2_eth_sgt_recycle(priv, buffer_start);
1212 
1213 	/* Move on with skb release. If we are just confirming multiple FDs
1214 	 * from the same TSO skb then only the last one will need to free the
1215 	 * skb.
1216 	 */
1217 	if (should_free_skb)
1218 		napi_consume_skb(skb, in_napi);
1219 }
1220 
1221 static int dpaa2_eth_build_gso_fd(struct dpaa2_eth_priv *priv,
1222 				  struct sk_buff *skb, struct dpaa2_fd *fd,
1223 				  int *num_fds, u32 *total_fds_len)
1224 {
1225 	struct device *dev = priv->net_dev->dev.parent;
1226 	int hdr_len, total_len, data_left, fd_len;
1227 	int num_sge, err, i, sgt_buf_size;
1228 	struct dpaa2_fd *fd_start = fd;
1229 	struct dpaa2_sg_entry *sgt;
1230 	struct dpaa2_eth_swa *swa;
1231 	dma_addr_t sgt_addr, addr;
1232 	dma_addr_t tso_hdr_dma;
1233 	unsigned int index = 0;
1234 	struct tso_t tso;
1235 	char *tso_hdr;
1236 	void *sgt_buf;
1237 
1238 	/* Initialize the TSO handler, and prepare the first payload */
1239 	hdr_len = tso_start(skb, &tso);
1240 	*total_fds_len = 0;
1241 
1242 	total_len = skb->len - hdr_len;
1243 	while (total_len > 0) {
1244 		/* Prepare the HW SGT structure for this frame */
1245 		sgt_buf = dpaa2_eth_sgt_get(priv);
1246 		if (unlikely(!sgt_buf)) {
1247 			netdev_err(priv->net_dev, "dpaa2_eth_sgt_get() failed\n");
1248 			err = -ENOMEM;
1249 			goto err_sgt_get;
1250 		}
1251 		sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
1252 
1253 		/* Determine the data length of this frame */
1254 		data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
1255 		total_len -= data_left;
1256 		fd_len = data_left + hdr_len;
1257 
1258 		/* Prepare packet headers: MAC + IP + TCP */
1259 		tso_hdr = kmalloc(TSO_HEADER_SIZE, GFP_ATOMIC);
1260 		if (!tso_hdr) {
1261 			err =  -ENOMEM;
1262 			goto err_alloc_tso_hdr;
1263 		}
1264 
1265 		tso_build_hdr(skb, tso_hdr, &tso, data_left, total_len == 0);
1266 		tso_hdr_dma = dma_map_single(dev, tso_hdr, TSO_HEADER_SIZE, DMA_TO_DEVICE);
1267 		if (dma_mapping_error(dev, tso_hdr_dma)) {
1268 			netdev_err(priv->net_dev, "dma_map_single(tso_hdr) failed\n");
1269 			err = -ENOMEM;
1270 			goto err_map_tso_hdr;
1271 		}
1272 
1273 		/* Setup the SG entry for the header */
1274 		dpaa2_sg_set_addr(sgt, tso_hdr_dma);
1275 		dpaa2_sg_set_len(sgt, hdr_len);
1276 		dpaa2_sg_set_final(sgt, data_left <= 0);
1277 
1278 		/* Compose the SG entries for each fragment of data */
1279 		num_sge = 1;
1280 		while (data_left > 0) {
1281 			int size;
1282 
1283 			/* Move to the next SG entry */
1284 			sgt++;
1285 			size = min_t(int, tso.size, data_left);
1286 
1287 			addr = dma_map_single(dev, tso.data, size, DMA_TO_DEVICE);
1288 			if (dma_mapping_error(dev, addr)) {
1289 				netdev_err(priv->net_dev, "dma_map_single(tso.data) failed\n");
1290 				err = -ENOMEM;
1291 				goto err_map_data;
1292 			}
1293 			dpaa2_sg_set_addr(sgt, addr);
1294 			dpaa2_sg_set_len(sgt, size);
1295 			dpaa2_sg_set_final(sgt, size == data_left);
1296 
1297 			num_sge++;
1298 
1299 			/* Build the data for the __next__ fragment */
1300 			data_left -= size;
1301 			tso_build_data(skb, &tso, size);
1302 		}
1303 
1304 		/* Store the skb backpointer in the SGT buffer */
1305 		sgt_buf_size = priv->tx_data_offset + num_sge * sizeof(struct dpaa2_sg_entry);
1306 		swa = (struct dpaa2_eth_swa *)sgt_buf;
1307 		swa->type = DPAA2_ETH_SWA_SW_TSO;
1308 		swa->tso.skb = skb;
1309 		swa->tso.num_sg = num_sge;
1310 		swa->tso.sgt_size = sgt_buf_size;
1311 		swa->tso.is_last_fd = total_len == 0 ? 1 : 0;
1312 
1313 		/* Separately map the SGT buffer */
1314 		sgt_addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
1315 		if (unlikely(dma_mapping_error(dev, sgt_addr))) {
1316 			netdev_err(priv->net_dev, "dma_map_single(sgt_buf) failed\n");
1317 			err = -ENOMEM;
1318 			goto err_map_sgt;
1319 		}
1320 
1321 		/* Setup the frame descriptor */
1322 		memset(fd, 0, sizeof(struct dpaa2_fd));
1323 		dpaa2_fd_set_offset(fd, priv->tx_data_offset);
1324 		dpaa2_fd_set_format(fd, dpaa2_fd_sg);
1325 		dpaa2_fd_set_addr(fd, sgt_addr);
1326 		dpaa2_fd_set_len(fd, fd_len);
1327 		dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
1328 
1329 		*total_fds_len += fd_len;
1330 		/* Advance to the next frame descriptor */
1331 		fd++;
1332 		index++;
1333 	}
1334 
1335 	*num_fds = index;
1336 
1337 	return 0;
1338 
1339 err_map_sgt:
1340 err_map_data:
1341 	/* Unmap all the data S/G entries for the current FD */
1342 	sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
1343 	for (i = 1; i < num_sge; i++)
1344 		dma_unmap_single(dev, dpaa2_sg_get_addr(&sgt[i]),
1345 				 dpaa2_sg_get_len(&sgt[i]), DMA_TO_DEVICE);
1346 
1347 	/* Unmap the header entry */
1348 	dma_unmap_single(dev, tso_hdr_dma, TSO_HEADER_SIZE, DMA_TO_DEVICE);
1349 err_map_tso_hdr:
1350 	kfree(tso_hdr);
1351 err_alloc_tso_hdr:
1352 	dpaa2_eth_sgt_recycle(priv, sgt_buf);
1353 err_sgt_get:
1354 	/* Free all the other FDs that were already fully created */
1355 	for (i = 0; i < index; i++)
1356 		dpaa2_eth_free_tx_fd(priv, NULL, &fd_start[i], false);
1357 
1358 	return err;
1359 }
1360 
1361 static netdev_tx_t __dpaa2_eth_tx(struct sk_buff *skb,
1362 				  struct net_device *net_dev)
1363 {
1364 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1365 	int total_enqueued = 0, retries = 0, enqueued;
1366 	struct dpaa2_eth_drv_stats *percpu_extras;
1367 	struct rtnl_link_stats64 *percpu_stats;
1368 	unsigned int needed_headroom;
1369 	int num_fds = 1, max_retries;
1370 	struct dpaa2_eth_fq *fq;
1371 	struct netdev_queue *nq;
1372 	struct dpaa2_fd *fd;
1373 	u16 queue_mapping;
1374 	void *swa = NULL;
1375 	u8 prio = 0;
1376 	int err, i;
1377 	u32 fd_len;
1378 
1379 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
1380 	percpu_extras = this_cpu_ptr(priv->percpu_extras);
1381 	fd = (this_cpu_ptr(priv->fd))->array;
1382 
1383 	needed_headroom = dpaa2_eth_needed_headroom(skb);
1384 
1385 	/* We'll be holding a back-reference to the skb until Tx Confirmation;
1386 	 * we don't want that overwritten by a concurrent Tx with a cloned skb.
1387 	 */
1388 	skb = skb_unshare(skb, GFP_ATOMIC);
1389 	if (unlikely(!skb)) {
1390 		/* skb_unshare() has already freed the skb */
1391 		percpu_stats->tx_dropped++;
1392 		return NETDEV_TX_OK;
1393 	}
1394 
1395 	/* Setup the FD fields */
1396 
1397 	if (skb_is_gso(skb)) {
1398 		err = dpaa2_eth_build_gso_fd(priv, skb, fd, &num_fds, &fd_len);
1399 		percpu_extras->tx_sg_frames += num_fds;
1400 		percpu_extras->tx_sg_bytes += fd_len;
1401 		percpu_extras->tx_tso_frames += num_fds;
1402 		percpu_extras->tx_tso_bytes += fd_len;
1403 	} else if (skb_is_nonlinear(skb)) {
1404 		err = dpaa2_eth_build_sg_fd(priv, skb, fd, &swa);
1405 		percpu_extras->tx_sg_frames++;
1406 		percpu_extras->tx_sg_bytes += skb->len;
1407 		fd_len = dpaa2_fd_get_len(fd);
1408 	} else if (skb_headroom(skb) < needed_headroom) {
1409 		err = dpaa2_eth_build_sg_fd_single_buf(priv, skb, fd, &swa);
1410 		percpu_extras->tx_sg_frames++;
1411 		percpu_extras->tx_sg_bytes += skb->len;
1412 		percpu_extras->tx_converted_sg_frames++;
1413 		percpu_extras->tx_converted_sg_bytes += skb->len;
1414 		fd_len = dpaa2_fd_get_len(fd);
1415 	} else {
1416 		err = dpaa2_eth_build_single_fd(priv, skb, fd, &swa);
1417 		fd_len = dpaa2_fd_get_len(fd);
1418 	}
1419 
1420 	if (unlikely(err)) {
1421 		percpu_stats->tx_dropped++;
1422 		goto err_build_fd;
1423 	}
1424 
1425 	if (swa && skb->cb[0])
1426 		dpaa2_eth_enable_tx_tstamp(priv, fd, swa, skb);
1427 
1428 	/* Tracing point */
1429 	for (i = 0; i < num_fds; i++)
1430 		trace_dpaa2_tx_fd(net_dev, &fd[i]);
1431 
1432 	/* TxConf FQ selection relies on queue id from the stack.
1433 	 * In case of a forwarded frame from another DPNI interface, we choose
1434 	 * a queue affined to the same core that processed the Rx frame
1435 	 */
1436 	queue_mapping = skb_get_queue_mapping(skb);
1437 
1438 	if (net_dev->num_tc) {
1439 		prio = netdev_txq_to_tc(net_dev, queue_mapping);
1440 		/* Hardware interprets priority level 0 as being the highest,
1441 		 * so we need to do a reverse mapping to the netdev tc index
1442 		 */
1443 		prio = net_dev->num_tc - prio - 1;
1444 		/* We have only one FQ array entry for all Tx hardware queues
1445 		 * with the same flow id (but different priority levels)
1446 		 */
1447 		queue_mapping %= dpaa2_eth_queue_count(priv);
1448 	}
1449 	fq = &priv->fq[queue_mapping];
1450 	nq = netdev_get_tx_queue(net_dev, queue_mapping);
1451 	netdev_tx_sent_queue(nq, fd_len);
1452 
1453 	/* Everything that happens after this enqueues might race with
1454 	 * the Tx confirmation callback for this frame
1455 	 */
1456 	max_retries = num_fds * DPAA2_ETH_ENQUEUE_RETRIES;
1457 	while (total_enqueued < num_fds && retries < max_retries) {
1458 		err = priv->enqueue(priv, fq, &fd[total_enqueued],
1459 				    prio, num_fds - total_enqueued, &enqueued);
1460 		if (err == -EBUSY) {
1461 			retries++;
1462 			continue;
1463 		}
1464 
1465 		total_enqueued += enqueued;
1466 	}
1467 	percpu_extras->tx_portal_busy += retries;
1468 
1469 	if (unlikely(err < 0)) {
1470 		percpu_stats->tx_errors++;
1471 		/* Clean up everything, including freeing the skb */
1472 		dpaa2_eth_free_tx_fd(priv, fq, fd, false);
1473 		netdev_tx_completed_queue(nq, 1, fd_len);
1474 	} else {
1475 		percpu_stats->tx_packets += total_enqueued;
1476 		percpu_stats->tx_bytes += fd_len;
1477 	}
1478 
1479 	return NETDEV_TX_OK;
1480 
1481 err_build_fd:
1482 	dev_kfree_skb(skb);
1483 
1484 	return NETDEV_TX_OK;
1485 }
1486 
1487 static void dpaa2_eth_tx_onestep_tstamp(struct work_struct *work)
1488 {
1489 	struct dpaa2_eth_priv *priv = container_of(work, struct dpaa2_eth_priv,
1490 						   tx_onestep_tstamp);
1491 	struct sk_buff *skb;
1492 
1493 	while (true) {
1494 		skb = skb_dequeue(&priv->tx_skbs);
1495 		if (!skb)
1496 			return;
1497 
1498 		/* Lock just before TX one-step timestamping packet,
1499 		 * and release the lock in dpaa2_eth_free_tx_fd when
1500 		 * confirm the packet has been sent on hardware, or
1501 		 * when clean up during transmit failure.
1502 		 */
1503 		mutex_lock(&priv->onestep_tstamp_lock);
1504 		__dpaa2_eth_tx(skb, priv->net_dev);
1505 	}
1506 }
1507 
1508 static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev)
1509 {
1510 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1511 	u8 msgtype, twostep, udp;
1512 	u16 offset1, offset2;
1513 
1514 	/* Utilize skb->cb[0] for timestamping request per skb */
1515 	skb->cb[0] = 0;
1516 
1517 	if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && dpaa2_ptp) {
1518 		if (priv->tx_tstamp_type == HWTSTAMP_TX_ON)
1519 			skb->cb[0] = TX_TSTAMP;
1520 		else if (priv->tx_tstamp_type == HWTSTAMP_TX_ONESTEP_SYNC)
1521 			skb->cb[0] = TX_TSTAMP_ONESTEP_SYNC;
1522 	}
1523 
1524 	/* TX for one-step timestamping PTP Sync packet */
1525 	if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
1526 		if (!dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
1527 					 &offset1, &offset2))
1528 			if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0) {
1529 				skb_queue_tail(&priv->tx_skbs, skb);
1530 				queue_work(priv->dpaa2_ptp_wq,
1531 					   &priv->tx_onestep_tstamp);
1532 				return NETDEV_TX_OK;
1533 			}
1534 		/* Use two-step timestamping if not one-step timestamping
1535 		 * PTP Sync packet
1536 		 */
1537 		skb->cb[0] = TX_TSTAMP;
1538 	}
1539 
1540 	/* TX for other packets */
1541 	return __dpaa2_eth_tx(skb, net_dev);
1542 }
1543 
1544 /* Tx confirmation frame processing routine */
1545 static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
1546 			      struct dpaa2_eth_channel *ch,
1547 			      const struct dpaa2_fd *fd,
1548 			      struct dpaa2_eth_fq *fq)
1549 {
1550 	struct rtnl_link_stats64 *percpu_stats;
1551 	struct dpaa2_eth_drv_stats *percpu_extras;
1552 	u32 fd_len = dpaa2_fd_get_len(fd);
1553 	u32 fd_errors;
1554 
1555 	/* Tracing point */
1556 	trace_dpaa2_tx_conf_fd(priv->net_dev, fd);
1557 
1558 	percpu_extras = this_cpu_ptr(priv->percpu_extras);
1559 	percpu_extras->tx_conf_frames++;
1560 	percpu_extras->tx_conf_bytes += fd_len;
1561 	ch->stats.bytes_per_cdan += fd_len;
1562 
1563 	/* Check frame errors in the FD field */
1564 	fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK;
1565 	dpaa2_eth_free_tx_fd(priv, fq, fd, true);
1566 
1567 	if (likely(!fd_errors))
1568 		return;
1569 
1570 	if (net_ratelimit())
1571 		netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n",
1572 			   fd_errors);
1573 
1574 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
1575 	/* Tx-conf logically pertains to the egress path. */
1576 	percpu_stats->tx_errors++;
1577 }
1578 
1579 static int dpaa2_eth_set_rx_vlan_filtering(struct dpaa2_eth_priv *priv,
1580 					   bool enable)
1581 {
1582 	int err;
1583 
1584 	err = dpni_enable_vlan_filter(priv->mc_io, 0, priv->mc_token, enable);
1585 
1586 	if (err) {
1587 		netdev_err(priv->net_dev,
1588 			   "dpni_enable_vlan_filter failed\n");
1589 		return err;
1590 	}
1591 
1592 	return 0;
1593 }
1594 
1595 static int dpaa2_eth_set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
1596 {
1597 	int err;
1598 
1599 	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1600 			       DPNI_OFF_RX_L3_CSUM, enable);
1601 	if (err) {
1602 		netdev_err(priv->net_dev,
1603 			   "dpni_set_offload(RX_L3_CSUM) failed\n");
1604 		return err;
1605 	}
1606 
1607 	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1608 			       DPNI_OFF_RX_L4_CSUM, enable);
1609 	if (err) {
1610 		netdev_err(priv->net_dev,
1611 			   "dpni_set_offload(RX_L4_CSUM) failed\n");
1612 		return err;
1613 	}
1614 
1615 	return 0;
1616 }
1617 
1618 static int dpaa2_eth_set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
1619 {
1620 	int err;
1621 
1622 	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1623 			       DPNI_OFF_TX_L3_CSUM, enable);
1624 	if (err) {
1625 		netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
1626 		return err;
1627 	}
1628 
1629 	err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1630 			       DPNI_OFF_TX_L4_CSUM, enable);
1631 	if (err) {
1632 		netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
1633 		return err;
1634 	}
1635 
1636 	return 0;
1637 }
1638 
1639 /* Perform a single release command to add buffers
1640  * to the specified buffer pool
1641  */
1642 static int dpaa2_eth_add_bufs(struct dpaa2_eth_priv *priv,
1643 			      struct dpaa2_eth_channel *ch)
1644 {
1645 	struct device *dev = priv->net_dev->dev.parent;
1646 	u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
1647 	struct page *page;
1648 	dma_addr_t addr;
1649 	int retries = 0;
1650 	int i, err;
1651 
1652 	for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
1653 		/* Allocate buffer visible to WRIOP + skb shared info +
1654 		 * alignment padding
1655 		 */
1656 		/* allocate one page for each Rx buffer. WRIOP sees
1657 		 * the entire page except for a tailroom reserved for
1658 		 * skb shared info
1659 		 */
1660 		page = dev_alloc_pages(0);
1661 		if (!page)
1662 			goto err_alloc;
1663 
1664 		addr = dma_map_page(dev, page, 0, priv->rx_buf_size,
1665 				    DMA_BIDIRECTIONAL);
1666 		if (unlikely(dma_mapping_error(dev, addr)))
1667 			goto err_map;
1668 
1669 		buf_array[i] = addr;
1670 
1671 		/* tracing point */
1672 		trace_dpaa2_eth_buf_seed(priv->net_dev, page_address(page),
1673 					 DPAA2_ETH_RX_BUF_RAW_SIZE,
1674 					 addr, priv->rx_buf_size,
1675 					 ch->bp->bpid);
1676 	}
1677 
1678 release_bufs:
1679 	/* In case the portal is busy, retry until successful */
1680 	while ((err = dpaa2_io_service_release(ch->dpio, ch->bp->bpid,
1681 					       buf_array, i)) == -EBUSY) {
1682 		if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
1683 			break;
1684 		cpu_relax();
1685 	}
1686 
1687 	/* If release command failed, clean up and bail out;
1688 	 * not much else we can do about it
1689 	 */
1690 	if (err) {
1691 		dpaa2_eth_free_bufs(priv, buf_array, i);
1692 		return 0;
1693 	}
1694 
1695 	return i;
1696 
1697 err_map:
1698 	__free_pages(page, 0);
1699 err_alloc:
1700 	/* If we managed to allocate at least some buffers,
1701 	 * release them to hardware
1702 	 */
1703 	if (i)
1704 		goto release_bufs;
1705 
1706 	return 0;
1707 }
1708 
1709 static int dpaa2_eth_seed_pool(struct dpaa2_eth_priv *priv,
1710 			       struct dpaa2_eth_channel *ch)
1711 {
1712 	int i;
1713 	int new_count;
1714 
1715 	for (i = 0; i < DPAA2_ETH_NUM_BUFS; i += DPAA2_ETH_BUFS_PER_CMD) {
1716 		new_count = dpaa2_eth_add_bufs(priv, ch);
1717 		ch->buf_count += new_count;
1718 
1719 		if (new_count < DPAA2_ETH_BUFS_PER_CMD)
1720 			return -ENOMEM;
1721 	}
1722 
1723 	return 0;
1724 }
1725 
1726 static void dpaa2_eth_seed_pools(struct dpaa2_eth_priv *priv)
1727 {
1728 	struct net_device *net_dev = priv->net_dev;
1729 	struct dpaa2_eth_channel *channel;
1730 	int i, err = 0;
1731 
1732 	for (i = 0; i < priv->num_channels; i++) {
1733 		channel = priv->channel[i];
1734 
1735 		err = dpaa2_eth_seed_pool(priv, channel);
1736 
1737 		/* Not much to do; the buffer pool, though not filled up,
1738 		 * may still contain some buffers which would enable us
1739 		 * to limp on.
1740 		 */
1741 		if (err)
1742 			netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
1743 				   channel->bp->dev->obj_desc.id,
1744 				   channel->bp->bpid);
1745 	}
1746 }
1747 
1748 /*
1749  * Drain the specified number of buffers from one of the DPNI's private buffer
1750  * pools.
1751  * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
1752  */
1753 static void dpaa2_eth_drain_bufs(struct dpaa2_eth_priv *priv, int bpid,
1754 				 int count)
1755 {
1756 	u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
1757 	int retries = 0;
1758 	int ret;
1759 
1760 	do {
1761 		ret = dpaa2_io_service_acquire(NULL, bpid, buf_array, count);
1762 		if (ret < 0) {
1763 			if (ret == -EBUSY &&
1764 			    retries++ < DPAA2_ETH_SWP_BUSY_RETRIES)
1765 				continue;
1766 			netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
1767 			return;
1768 		}
1769 		dpaa2_eth_free_bufs(priv, buf_array, ret);
1770 		retries = 0;
1771 	} while (ret);
1772 }
1773 
1774 static void dpaa2_eth_drain_pool(struct dpaa2_eth_priv *priv, int bpid)
1775 {
1776 	int i;
1777 
1778 	/* Drain the buffer pool */
1779 	dpaa2_eth_drain_bufs(priv, bpid, DPAA2_ETH_BUFS_PER_CMD);
1780 	dpaa2_eth_drain_bufs(priv, bpid, 1);
1781 
1782 	/* Setup to zero the buffer count of all channels which were
1783 	 * using this buffer pool.
1784 	 */
1785 	for (i = 0; i < priv->num_channels; i++)
1786 		if (priv->channel[i]->bp->bpid == bpid)
1787 			priv->channel[i]->buf_count = 0;
1788 }
1789 
1790 static void dpaa2_eth_drain_pools(struct dpaa2_eth_priv *priv)
1791 {
1792 	int i;
1793 
1794 	for (i = 0; i < priv->num_bps; i++)
1795 		dpaa2_eth_drain_pool(priv, priv->bp[i]->bpid);
1796 }
1797 
1798 /* Function is called from softirq context only, so we don't need to guard
1799  * the access to percpu count
1800  */
1801 static int dpaa2_eth_refill_pool(struct dpaa2_eth_priv *priv,
1802 				 struct dpaa2_eth_channel *ch)
1803 {
1804 	int new_count;
1805 
1806 	if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
1807 		return 0;
1808 
1809 	do {
1810 		new_count = dpaa2_eth_add_bufs(priv, ch);
1811 		if (unlikely(!new_count)) {
1812 			/* Out of memory; abort for now, we'll try later on */
1813 			break;
1814 		}
1815 		ch->buf_count += new_count;
1816 	} while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
1817 
1818 	if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
1819 		return -ENOMEM;
1820 
1821 	return 0;
1822 }
1823 
1824 static void dpaa2_eth_sgt_cache_drain(struct dpaa2_eth_priv *priv)
1825 {
1826 	struct dpaa2_eth_sgt_cache *sgt_cache;
1827 	u16 count;
1828 	int k, i;
1829 
1830 	for_each_possible_cpu(k) {
1831 		sgt_cache = per_cpu_ptr(priv->sgt_cache, k);
1832 		count = sgt_cache->count;
1833 
1834 		for (i = 0; i < count; i++)
1835 			skb_free_frag(sgt_cache->buf[i]);
1836 		sgt_cache->count = 0;
1837 	}
1838 }
1839 
1840 static int dpaa2_eth_pull_channel(struct dpaa2_eth_channel *ch)
1841 {
1842 	int err;
1843 	int dequeues = -1;
1844 
1845 	/* Retry while portal is busy */
1846 	do {
1847 		err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
1848 						    ch->store);
1849 		dequeues++;
1850 		cpu_relax();
1851 	} while (err == -EBUSY && dequeues < DPAA2_ETH_SWP_BUSY_RETRIES);
1852 
1853 	ch->stats.dequeue_portal_busy += dequeues;
1854 	if (unlikely(err))
1855 		ch->stats.pull_err++;
1856 
1857 	return err;
1858 }
1859 
1860 /* NAPI poll routine
1861  *
1862  * Frames are dequeued from the QMan channel associated with this NAPI context.
1863  * Rx, Tx confirmation and (if configured) Rx error frames all count
1864  * towards the NAPI budget.
1865  */
1866 static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
1867 {
1868 	struct dpaa2_eth_channel *ch;
1869 	struct dpaa2_eth_priv *priv;
1870 	int rx_cleaned = 0, txconf_cleaned = 0;
1871 	struct dpaa2_eth_fq *fq, *txc_fq = NULL;
1872 	struct netdev_queue *nq;
1873 	int store_cleaned, work_done;
1874 	struct list_head rx_list;
1875 	int retries = 0;
1876 	u16 flowid;
1877 	int err;
1878 
1879 	ch = container_of(napi, struct dpaa2_eth_channel, napi);
1880 	ch->xdp.res = 0;
1881 	priv = ch->priv;
1882 
1883 	INIT_LIST_HEAD(&rx_list);
1884 	ch->rx_list = &rx_list;
1885 
1886 	do {
1887 		err = dpaa2_eth_pull_channel(ch);
1888 		if (unlikely(err))
1889 			break;
1890 
1891 		/* Refill pool if appropriate */
1892 		dpaa2_eth_refill_pool(priv, ch);
1893 
1894 		store_cleaned = dpaa2_eth_consume_frames(ch, &fq);
1895 		if (store_cleaned <= 0)
1896 			break;
1897 		if (fq->type == DPAA2_RX_FQ) {
1898 			rx_cleaned += store_cleaned;
1899 			flowid = fq->flowid;
1900 		} else {
1901 			txconf_cleaned += store_cleaned;
1902 			/* We have a single Tx conf FQ on this channel */
1903 			txc_fq = fq;
1904 		}
1905 
1906 		/* If we either consumed the whole NAPI budget with Rx frames
1907 		 * or we reached the Tx confirmations threshold, we're done.
1908 		 */
1909 		if (rx_cleaned >= budget ||
1910 		    txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
1911 			work_done = budget;
1912 			goto out;
1913 		}
1914 	} while (store_cleaned);
1915 
1916 	/* Update NET DIM with the values for this CDAN */
1917 	dpaa2_io_update_net_dim(ch->dpio, ch->stats.frames_per_cdan,
1918 				ch->stats.bytes_per_cdan);
1919 	ch->stats.frames_per_cdan = 0;
1920 	ch->stats.bytes_per_cdan = 0;
1921 
1922 	/* We didn't consume the entire budget, so finish napi and
1923 	 * re-enable data availability notifications
1924 	 */
1925 	napi_complete_done(napi, rx_cleaned);
1926 	do {
1927 		err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
1928 		cpu_relax();
1929 	} while (err == -EBUSY && retries++ < DPAA2_ETH_SWP_BUSY_RETRIES);
1930 	WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
1931 		  ch->nctx.desired_cpu);
1932 
1933 	work_done = max(rx_cleaned, 1);
1934 
1935 out:
1936 	netif_receive_skb_list(ch->rx_list);
1937 
1938 	if (txc_fq && txc_fq->dq_frames) {
1939 		nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid);
1940 		netdev_tx_completed_queue(nq, txc_fq->dq_frames,
1941 					  txc_fq->dq_bytes);
1942 		txc_fq->dq_frames = 0;
1943 		txc_fq->dq_bytes = 0;
1944 	}
1945 
1946 	if (ch->xdp.res & XDP_REDIRECT)
1947 		xdp_do_flush_map();
1948 	else if (rx_cleaned && ch->xdp.res & XDP_TX)
1949 		dpaa2_eth_xdp_tx_flush(priv, ch, &priv->fq[flowid]);
1950 
1951 	return work_done;
1952 }
1953 
1954 static void dpaa2_eth_enable_ch_napi(struct dpaa2_eth_priv *priv)
1955 {
1956 	struct dpaa2_eth_channel *ch;
1957 	int i;
1958 
1959 	for (i = 0; i < priv->num_channels; i++) {
1960 		ch = priv->channel[i];
1961 		napi_enable(&ch->napi);
1962 	}
1963 }
1964 
1965 static void dpaa2_eth_disable_ch_napi(struct dpaa2_eth_priv *priv)
1966 {
1967 	struct dpaa2_eth_channel *ch;
1968 	int i;
1969 
1970 	for (i = 0; i < priv->num_channels; i++) {
1971 		ch = priv->channel[i];
1972 		napi_disable(&ch->napi);
1973 	}
1974 }
1975 
1976 void dpaa2_eth_set_rx_taildrop(struct dpaa2_eth_priv *priv,
1977 			       bool tx_pause, bool pfc)
1978 {
1979 	struct dpni_taildrop td = {0};
1980 	struct dpaa2_eth_fq *fq;
1981 	int i, err;
1982 
1983 	/* FQ taildrop: threshold is in bytes, per frame queue. Enabled if
1984 	 * flow control is disabled (as it might interfere with either the
1985 	 * buffer pool depletion trigger for pause frames or with the group
1986 	 * congestion trigger for PFC frames)
1987 	 */
1988 	td.enable = !tx_pause;
1989 	if (priv->rx_fqtd_enabled == td.enable)
1990 		goto set_cgtd;
1991 
1992 	td.threshold = DPAA2_ETH_FQ_TAILDROP_THRESH;
1993 	td.units = DPNI_CONGESTION_UNIT_BYTES;
1994 
1995 	for (i = 0; i < priv->num_fqs; i++) {
1996 		fq = &priv->fq[i];
1997 		if (fq->type != DPAA2_RX_FQ)
1998 			continue;
1999 		err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
2000 					DPNI_CP_QUEUE, DPNI_QUEUE_RX,
2001 					fq->tc, fq->flowid, &td);
2002 		if (err) {
2003 			netdev_err(priv->net_dev,
2004 				   "dpni_set_taildrop(FQ) failed\n");
2005 			return;
2006 		}
2007 	}
2008 
2009 	priv->rx_fqtd_enabled = td.enable;
2010 
2011 set_cgtd:
2012 	/* Congestion group taildrop: threshold is in frames, per group
2013 	 * of FQs belonging to the same traffic class
2014 	 * Enabled if general Tx pause disabled or if PFCs are enabled
2015 	 * (congestion group threhsold for PFC generation is lower than the
2016 	 * CG taildrop threshold, so it won't interfere with it; we also
2017 	 * want frames in non-PFC enabled traffic classes to be kept in check)
2018 	 */
2019 	td.enable = !tx_pause || pfc;
2020 	if (priv->rx_cgtd_enabled == td.enable)
2021 		return;
2022 
2023 	td.threshold = DPAA2_ETH_CG_TAILDROP_THRESH(priv);
2024 	td.units = DPNI_CONGESTION_UNIT_FRAMES;
2025 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
2026 		err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
2027 					DPNI_CP_GROUP, DPNI_QUEUE_RX,
2028 					i, 0, &td);
2029 		if (err) {
2030 			netdev_err(priv->net_dev,
2031 				   "dpni_set_taildrop(CG) failed\n");
2032 			return;
2033 		}
2034 	}
2035 
2036 	priv->rx_cgtd_enabled = td.enable;
2037 }
2038 
2039 static int dpaa2_eth_link_state_update(struct dpaa2_eth_priv *priv)
2040 {
2041 	struct dpni_link_state state = {0};
2042 	bool tx_pause;
2043 	int err;
2044 
2045 	err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
2046 	if (unlikely(err)) {
2047 		netdev_err(priv->net_dev,
2048 			   "dpni_get_link_state() failed\n");
2049 		return err;
2050 	}
2051 
2052 	/* If Tx pause frame settings have changed, we need to update
2053 	 * Rx FQ taildrop configuration as well. We configure taildrop
2054 	 * only when pause frame generation is disabled.
2055 	 */
2056 	tx_pause = dpaa2_eth_tx_pause_enabled(state.options);
2057 	dpaa2_eth_set_rx_taildrop(priv, tx_pause, priv->pfc_enabled);
2058 
2059 	/* When we manage the MAC/PHY using phylink there is no need
2060 	 * to manually update the netif_carrier.
2061 	 */
2062 	if (dpaa2_eth_is_type_phy(priv))
2063 		goto out;
2064 
2065 	/* Chech link state; speed / duplex changes are not treated yet */
2066 	if (priv->link_state.up == state.up)
2067 		goto out;
2068 
2069 	if (state.up) {
2070 		netif_carrier_on(priv->net_dev);
2071 		netif_tx_start_all_queues(priv->net_dev);
2072 	} else {
2073 		netif_tx_stop_all_queues(priv->net_dev);
2074 		netif_carrier_off(priv->net_dev);
2075 	}
2076 
2077 	netdev_info(priv->net_dev, "Link Event: state %s\n",
2078 		    state.up ? "up" : "down");
2079 
2080 out:
2081 	priv->link_state = state;
2082 
2083 	return 0;
2084 }
2085 
2086 static int dpaa2_eth_open(struct net_device *net_dev)
2087 {
2088 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2089 	int err;
2090 
2091 	dpaa2_eth_seed_pools(priv);
2092 
2093 	if (!dpaa2_eth_is_type_phy(priv)) {
2094 		/* We'll only start the txqs when the link is actually ready;
2095 		 * make sure we don't race against the link up notification,
2096 		 * which may come immediately after dpni_enable();
2097 		 */
2098 		netif_tx_stop_all_queues(net_dev);
2099 
2100 		/* Also, explicitly set carrier off, otherwise
2101 		 * netif_carrier_ok() will return true and cause 'ip link show'
2102 		 * to report the LOWER_UP flag, even though the link
2103 		 * notification wasn't even received.
2104 		 */
2105 		netif_carrier_off(net_dev);
2106 	}
2107 	dpaa2_eth_enable_ch_napi(priv);
2108 
2109 	err = dpni_enable(priv->mc_io, 0, priv->mc_token);
2110 	if (err < 0) {
2111 		netdev_err(net_dev, "dpni_enable() failed\n");
2112 		goto enable_err;
2113 	}
2114 
2115 	if (dpaa2_eth_is_type_phy(priv)) {
2116 		dpaa2_mac_start(priv->mac);
2117 		phylink_start(priv->mac->phylink);
2118 	}
2119 
2120 	return 0;
2121 
2122 enable_err:
2123 	dpaa2_eth_disable_ch_napi(priv);
2124 	dpaa2_eth_drain_pools(priv);
2125 	return err;
2126 }
2127 
2128 /* Total number of in-flight frames on ingress queues */
2129 static u32 dpaa2_eth_ingress_fq_count(struct dpaa2_eth_priv *priv)
2130 {
2131 	struct dpaa2_eth_fq *fq;
2132 	u32 fcnt = 0, bcnt = 0, total = 0;
2133 	int i, err;
2134 
2135 	for (i = 0; i < priv->num_fqs; i++) {
2136 		fq = &priv->fq[i];
2137 		err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
2138 		if (err) {
2139 			netdev_warn(priv->net_dev, "query_fq_count failed");
2140 			break;
2141 		}
2142 		total += fcnt;
2143 	}
2144 
2145 	return total;
2146 }
2147 
2148 static void dpaa2_eth_wait_for_ingress_fq_empty(struct dpaa2_eth_priv *priv)
2149 {
2150 	int retries = 10;
2151 	u32 pending;
2152 
2153 	do {
2154 		pending = dpaa2_eth_ingress_fq_count(priv);
2155 		if (pending)
2156 			msleep(100);
2157 	} while (pending && --retries);
2158 }
2159 
2160 #define DPNI_TX_PENDING_VER_MAJOR	7
2161 #define DPNI_TX_PENDING_VER_MINOR	13
2162 static void dpaa2_eth_wait_for_egress_fq_empty(struct dpaa2_eth_priv *priv)
2163 {
2164 	union dpni_statistics stats;
2165 	int retries = 10;
2166 	int err;
2167 
2168 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_TX_PENDING_VER_MAJOR,
2169 				   DPNI_TX_PENDING_VER_MINOR) < 0)
2170 		goto out;
2171 
2172 	do {
2173 		err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token, 6,
2174 					  &stats);
2175 		if (err)
2176 			goto out;
2177 		if (stats.page_6.tx_pending_frames == 0)
2178 			return;
2179 	} while (--retries);
2180 
2181 out:
2182 	msleep(500);
2183 }
2184 
2185 static int dpaa2_eth_stop(struct net_device *net_dev)
2186 {
2187 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2188 	int dpni_enabled = 0;
2189 	int retries = 10;
2190 
2191 	if (dpaa2_eth_is_type_phy(priv)) {
2192 		phylink_stop(priv->mac->phylink);
2193 		dpaa2_mac_stop(priv->mac);
2194 	} else {
2195 		netif_tx_stop_all_queues(net_dev);
2196 		netif_carrier_off(net_dev);
2197 	}
2198 
2199 	/* On dpni_disable(), the MC firmware will:
2200 	 * - stop MAC Rx and wait for all Rx frames to be enqueued to software
2201 	 * - cut off WRIOP dequeues from egress FQs and wait until transmission
2202 	 * of all in flight Tx frames is finished (and corresponding Tx conf
2203 	 * frames are enqueued back to software)
2204 	 *
2205 	 * Before calling dpni_disable(), we wait for all Tx frames to arrive
2206 	 * on WRIOP. After it finishes, wait until all remaining frames on Rx
2207 	 * and Tx conf queues are consumed on NAPI poll.
2208 	 */
2209 	dpaa2_eth_wait_for_egress_fq_empty(priv);
2210 
2211 	do {
2212 		dpni_disable(priv->mc_io, 0, priv->mc_token);
2213 		dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
2214 		if (dpni_enabled)
2215 			/* Allow the hardware some slack */
2216 			msleep(100);
2217 	} while (dpni_enabled && --retries);
2218 	if (!retries) {
2219 		netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
2220 		/* Must go on and disable NAPI nonetheless, so we don't crash at
2221 		 * the next "ifconfig up"
2222 		 */
2223 	}
2224 
2225 	dpaa2_eth_wait_for_ingress_fq_empty(priv);
2226 	dpaa2_eth_disable_ch_napi(priv);
2227 
2228 	/* Empty the buffer pool */
2229 	dpaa2_eth_drain_pools(priv);
2230 
2231 	/* Empty the Scatter-Gather Buffer cache */
2232 	dpaa2_eth_sgt_cache_drain(priv);
2233 
2234 	return 0;
2235 }
2236 
2237 static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
2238 {
2239 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2240 	struct device *dev = net_dev->dev.parent;
2241 	int err;
2242 
2243 	err = eth_mac_addr(net_dev, addr);
2244 	if (err < 0) {
2245 		dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
2246 		return err;
2247 	}
2248 
2249 	err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
2250 					net_dev->dev_addr);
2251 	if (err) {
2252 		dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
2253 		return err;
2254 	}
2255 
2256 	return 0;
2257 }
2258 
2259 /** Fill in counters maintained by the GPP driver. These may be different from
2260  * the hardware counters obtained by ethtool.
2261  */
2262 static void dpaa2_eth_get_stats(struct net_device *net_dev,
2263 				struct rtnl_link_stats64 *stats)
2264 {
2265 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2266 	struct rtnl_link_stats64 *percpu_stats;
2267 	u64 *cpustats;
2268 	u64 *netstats = (u64 *)stats;
2269 	int i, j;
2270 	int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
2271 
2272 	for_each_possible_cpu(i) {
2273 		percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
2274 		cpustats = (u64 *)percpu_stats;
2275 		for (j = 0; j < num; j++)
2276 			netstats[j] += cpustats[j];
2277 	}
2278 }
2279 
2280 /* Copy mac unicast addresses from @net_dev to @priv.
2281  * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
2282  */
2283 static void dpaa2_eth_add_uc_hw_addr(const struct net_device *net_dev,
2284 				     struct dpaa2_eth_priv *priv)
2285 {
2286 	struct netdev_hw_addr *ha;
2287 	int err;
2288 
2289 	netdev_for_each_uc_addr(ha, net_dev) {
2290 		err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
2291 					ha->addr);
2292 		if (err)
2293 			netdev_warn(priv->net_dev,
2294 				    "Could not add ucast MAC %pM to the filtering table (err %d)\n",
2295 				    ha->addr, err);
2296 	}
2297 }
2298 
2299 /* Copy mac multicast addresses from @net_dev to @priv
2300  * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
2301  */
2302 static void dpaa2_eth_add_mc_hw_addr(const struct net_device *net_dev,
2303 				     struct dpaa2_eth_priv *priv)
2304 {
2305 	struct netdev_hw_addr *ha;
2306 	int err;
2307 
2308 	netdev_for_each_mc_addr(ha, net_dev) {
2309 		err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
2310 					ha->addr);
2311 		if (err)
2312 			netdev_warn(priv->net_dev,
2313 				    "Could not add mcast MAC %pM to the filtering table (err %d)\n",
2314 				    ha->addr, err);
2315 	}
2316 }
2317 
2318 static int dpaa2_eth_rx_add_vid(struct net_device *net_dev,
2319 				__be16 vlan_proto, u16 vid)
2320 {
2321 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2322 	int err;
2323 
2324 	err = dpni_add_vlan_id(priv->mc_io, 0, priv->mc_token,
2325 			       vid, 0, 0, 0);
2326 
2327 	if (err) {
2328 		netdev_warn(priv->net_dev,
2329 			    "Could not add the vlan id %u\n",
2330 			    vid);
2331 		return err;
2332 	}
2333 
2334 	return 0;
2335 }
2336 
2337 static int dpaa2_eth_rx_kill_vid(struct net_device *net_dev,
2338 				 __be16 vlan_proto, u16 vid)
2339 {
2340 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2341 	int err;
2342 
2343 	err = dpni_remove_vlan_id(priv->mc_io, 0, priv->mc_token, vid);
2344 
2345 	if (err) {
2346 		netdev_warn(priv->net_dev,
2347 			    "Could not remove the vlan id %u\n",
2348 			    vid);
2349 		return err;
2350 	}
2351 
2352 	return 0;
2353 }
2354 
2355 static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
2356 {
2357 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2358 	int uc_count = netdev_uc_count(net_dev);
2359 	int mc_count = netdev_mc_count(net_dev);
2360 	u8 max_mac = priv->dpni_attrs.mac_filter_entries;
2361 	u32 options = priv->dpni_attrs.options;
2362 	u16 mc_token = priv->mc_token;
2363 	struct fsl_mc_io *mc_io = priv->mc_io;
2364 	int err;
2365 
2366 	/* Basic sanity checks; these probably indicate a misconfiguration */
2367 	if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
2368 		netdev_info(net_dev,
2369 			    "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
2370 			    max_mac);
2371 
2372 	/* Force promiscuous if the uc or mc counts exceed our capabilities. */
2373 	if (uc_count > max_mac) {
2374 		netdev_info(net_dev,
2375 			    "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
2376 			    uc_count, max_mac);
2377 		goto force_promisc;
2378 	}
2379 	if (mc_count + uc_count > max_mac) {
2380 		netdev_info(net_dev,
2381 			    "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
2382 			    uc_count + mc_count, max_mac);
2383 		goto force_mc_promisc;
2384 	}
2385 
2386 	/* Adjust promisc settings due to flag combinations */
2387 	if (net_dev->flags & IFF_PROMISC)
2388 		goto force_promisc;
2389 	if (net_dev->flags & IFF_ALLMULTI) {
2390 		/* First, rebuild unicast filtering table. This should be done
2391 		 * in promisc mode, in order to avoid frame loss while we
2392 		 * progressively add entries to the table.
2393 		 * We don't know whether we had been in promisc already, and
2394 		 * making an MC call to find out is expensive; so set uc promisc
2395 		 * nonetheless.
2396 		 */
2397 		err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2398 		if (err)
2399 			netdev_warn(net_dev, "Can't set uc promisc\n");
2400 
2401 		/* Actual uc table reconstruction. */
2402 		err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
2403 		if (err)
2404 			netdev_warn(net_dev, "Can't clear uc filters\n");
2405 		dpaa2_eth_add_uc_hw_addr(net_dev, priv);
2406 
2407 		/* Finally, clear uc promisc and set mc promisc as requested. */
2408 		err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
2409 		if (err)
2410 			netdev_warn(net_dev, "Can't clear uc promisc\n");
2411 		goto force_mc_promisc;
2412 	}
2413 
2414 	/* Neither unicast, nor multicast promisc will be on... eventually.
2415 	 * For now, rebuild mac filtering tables while forcing both of them on.
2416 	 */
2417 	err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2418 	if (err)
2419 		netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
2420 	err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
2421 	if (err)
2422 		netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
2423 
2424 	/* Actual mac filtering tables reconstruction */
2425 	err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
2426 	if (err)
2427 		netdev_warn(net_dev, "Can't clear mac filters\n");
2428 	dpaa2_eth_add_mc_hw_addr(net_dev, priv);
2429 	dpaa2_eth_add_uc_hw_addr(net_dev, priv);
2430 
2431 	/* Now we can clear both ucast and mcast promisc, without risking
2432 	 * to drop legitimate frames anymore.
2433 	 */
2434 	err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
2435 	if (err)
2436 		netdev_warn(net_dev, "Can't clear ucast promisc\n");
2437 	err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
2438 	if (err)
2439 		netdev_warn(net_dev, "Can't clear mcast promisc\n");
2440 
2441 	return;
2442 
2443 force_promisc:
2444 	err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2445 	if (err)
2446 		netdev_warn(net_dev, "Can't set ucast promisc\n");
2447 force_mc_promisc:
2448 	err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
2449 	if (err)
2450 		netdev_warn(net_dev, "Can't set mcast promisc\n");
2451 }
2452 
2453 static int dpaa2_eth_set_features(struct net_device *net_dev,
2454 				  netdev_features_t features)
2455 {
2456 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2457 	netdev_features_t changed = features ^ net_dev->features;
2458 	bool enable;
2459 	int err;
2460 
2461 	if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
2462 		enable = !!(features & NETIF_F_HW_VLAN_CTAG_FILTER);
2463 		err = dpaa2_eth_set_rx_vlan_filtering(priv, enable);
2464 		if (err)
2465 			return err;
2466 	}
2467 
2468 	if (changed & NETIF_F_RXCSUM) {
2469 		enable = !!(features & NETIF_F_RXCSUM);
2470 		err = dpaa2_eth_set_rx_csum(priv, enable);
2471 		if (err)
2472 			return err;
2473 	}
2474 
2475 	if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
2476 		enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
2477 		err = dpaa2_eth_set_tx_csum(priv, enable);
2478 		if (err)
2479 			return err;
2480 	}
2481 
2482 	return 0;
2483 }
2484 
2485 static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2486 {
2487 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
2488 	struct hwtstamp_config config;
2489 
2490 	if (!dpaa2_ptp)
2491 		return -EINVAL;
2492 
2493 	if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
2494 		return -EFAULT;
2495 
2496 	switch (config.tx_type) {
2497 	case HWTSTAMP_TX_OFF:
2498 	case HWTSTAMP_TX_ON:
2499 	case HWTSTAMP_TX_ONESTEP_SYNC:
2500 		priv->tx_tstamp_type = config.tx_type;
2501 		break;
2502 	default:
2503 		return -ERANGE;
2504 	}
2505 
2506 	if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
2507 		priv->rx_tstamp = false;
2508 	} else {
2509 		priv->rx_tstamp = true;
2510 		/* TS is set for all frame types, not only those requested */
2511 		config.rx_filter = HWTSTAMP_FILTER_ALL;
2512 	}
2513 
2514 	if (priv->tx_tstamp_type == HWTSTAMP_TX_ONESTEP_SYNC)
2515 		dpaa2_ptp_onestep_reg_update_method(priv);
2516 
2517 	return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
2518 			-EFAULT : 0;
2519 }
2520 
2521 static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2522 {
2523 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
2524 
2525 	if (cmd == SIOCSHWTSTAMP)
2526 		return dpaa2_eth_ts_ioctl(dev, rq, cmd);
2527 
2528 	if (dpaa2_eth_is_type_phy(priv))
2529 		return phylink_mii_ioctl(priv->mac->phylink, rq, cmd);
2530 
2531 	return -EOPNOTSUPP;
2532 }
2533 
2534 static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu)
2535 {
2536 	int mfl, linear_mfl;
2537 
2538 	mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
2539 	linear_mfl = priv->rx_buf_size - DPAA2_ETH_RX_HWA_SIZE -
2540 		     dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM;
2541 
2542 	if (mfl > linear_mfl) {
2543 		netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
2544 			    linear_mfl - VLAN_ETH_HLEN);
2545 		return false;
2546 	}
2547 
2548 	return true;
2549 }
2550 
2551 static int dpaa2_eth_set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
2552 {
2553 	int mfl, err;
2554 
2555 	/* We enforce a maximum Rx frame length based on MTU only if we have
2556 	 * an XDP program attached (in order to avoid Rx S/G frames).
2557 	 * Otherwise, we accept all incoming frames as long as they are not
2558 	 * larger than maximum size supported in hardware
2559 	 */
2560 	if (has_xdp)
2561 		mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
2562 	else
2563 		mfl = DPAA2_ETH_MFL;
2564 
2565 	err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl);
2566 	if (err) {
2567 		netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n");
2568 		return err;
2569 	}
2570 
2571 	return 0;
2572 }
2573 
2574 static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu)
2575 {
2576 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
2577 	int err;
2578 
2579 	if (!priv->xdp_prog)
2580 		goto out;
2581 
2582 	if (!xdp_mtu_valid(priv, new_mtu))
2583 		return -EINVAL;
2584 
2585 	err = dpaa2_eth_set_rx_mfl(priv, new_mtu, true);
2586 	if (err)
2587 		return err;
2588 
2589 out:
2590 	dev->mtu = new_mtu;
2591 	return 0;
2592 }
2593 
2594 static int dpaa2_eth_update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp)
2595 {
2596 	struct dpni_buffer_layout buf_layout = {0};
2597 	int err;
2598 
2599 	err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token,
2600 				     DPNI_QUEUE_RX, &buf_layout);
2601 	if (err) {
2602 		netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n");
2603 		return err;
2604 	}
2605 
2606 	/* Reserve extra headroom for XDP header size changes */
2607 	buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) +
2608 				    (has_xdp ? XDP_PACKET_HEADROOM : 0);
2609 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM;
2610 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2611 				     DPNI_QUEUE_RX, &buf_layout);
2612 	if (err) {
2613 		netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n");
2614 		return err;
2615 	}
2616 
2617 	return 0;
2618 }
2619 
2620 static int dpaa2_eth_setup_xdp(struct net_device *dev, struct bpf_prog *prog)
2621 {
2622 	struct dpaa2_eth_priv *priv = netdev_priv(dev);
2623 	struct dpaa2_eth_channel *ch;
2624 	struct bpf_prog *old;
2625 	bool up, need_update;
2626 	int i, err;
2627 
2628 	if (prog && !xdp_mtu_valid(priv, dev->mtu))
2629 		return -EINVAL;
2630 
2631 	if (prog)
2632 		bpf_prog_add(prog, priv->num_channels);
2633 
2634 	up = netif_running(dev);
2635 	need_update = (!!priv->xdp_prog != !!prog);
2636 
2637 	if (up)
2638 		dev_close(dev);
2639 
2640 	/* While in xdp mode, enforce a maximum Rx frame size based on MTU.
2641 	 * Also, when switching between xdp/non-xdp modes we need to reconfigure
2642 	 * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop,
2643 	 * so we are sure no old format buffers will be used from now on.
2644 	 */
2645 	if (need_update) {
2646 		err = dpaa2_eth_set_rx_mfl(priv, dev->mtu, !!prog);
2647 		if (err)
2648 			goto out_err;
2649 		err = dpaa2_eth_update_rx_buffer_headroom(priv, !!prog);
2650 		if (err)
2651 			goto out_err;
2652 	}
2653 
2654 	old = xchg(&priv->xdp_prog, prog);
2655 	if (old)
2656 		bpf_prog_put(old);
2657 
2658 	for (i = 0; i < priv->num_channels; i++) {
2659 		ch = priv->channel[i];
2660 		old = xchg(&ch->xdp.prog, prog);
2661 		if (old)
2662 			bpf_prog_put(old);
2663 	}
2664 
2665 	if (up) {
2666 		err = dev_open(dev, NULL);
2667 		if (err)
2668 			return err;
2669 	}
2670 
2671 	return 0;
2672 
2673 out_err:
2674 	if (prog)
2675 		bpf_prog_sub(prog, priv->num_channels);
2676 	if (up)
2677 		dev_open(dev, NULL);
2678 
2679 	return err;
2680 }
2681 
2682 static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2683 {
2684 	switch (xdp->command) {
2685 	case XDP_SETUP_PROG:
2686 		return dpaa2_eth_setup_xdp(dev, xdp->prog);
2687 	default:
2688 		return -EINVAL;
2689 	}
2690 
2691 	return 0;
2692 }
2693 
2694 static int dpaa2_eth_xdp_create_fd(struct net_device *net_dev,
2695 				   struct xdp_frame *xdpf,
2696 				   struct dpaa2_fd *fd)
2697 {
2698 	struct device *dev = net_dev->dev.parent;
2699 	unsigned int needed_headroom;
2700 	struct dpaa2_eth_swa *swa;
2701 	void *buffer_start, *aligned_start;
2702 	dma_addr_t addr;
2703 
2704 	/* We require a minimum headroom to be able to transmit the frame.
2705 	 * Otherwise return an error and let the original net_device handle it
2706 	 */
2707 	needed_headroom = dpaa2_eth_needed_headroom(NULL);
2708 	if (xdpf->headroom < needed_headroom)
2709 		return -EINVAL;
2710 
2711 	/* Setup the FD fields */
2712 	memset(fd, 0, sizeof(*fd));
2713 
2714 	/* Align FD address, if possible */
2715 	buffer_start = xdpf->data - needed_headroom;
2716 	aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
2717 				  DPAA2_ETH_TX_BUF_ALIGN);
2718 	if (aligned_start >= xdpf->data - xdpf->headroom)
2719 		buffer_start = aligned_start;
2720 
2721 	swa = (struct dpaa2_eth_swa *)buffer_start;
2722 	/* fill in necessary fields here */
2723 	swa->type = DPAA2_ETH_SWA_XDP;
2724 	swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start;
2725 	swa->xdp.xdpf = xdpf;
2726 
2727 	addr = dma_map_single(dev, buffer_start,
2728 			      swa->xdp.dma_size,
2729 			      DMA_BIDIRECTIONAL);
2730 	if (unlikely(dma_mapping_error(dev, addr)))
2731 		return -ENOMEM;
2732 
2733 	dpaa2_fd_set_addr(fd, addr);
2734 	dpaa2_fd_set_offset(fd, xdpf->data - buffer_start);
2735 	dpaa2_fd_set_len(fd, xdpf->len);
2736 	dpaa2_fd_set_format(fd, dpaa2_fd_single);
2737 	dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
2738 
2739 	return 0;
2740 }
2741 
2742 static int dpaa2_eth_xdp_xmit(struct net_device *net_dev, int n,
2743 			      struct xdp_frame **frames, u32 flags)
2744 {
2745 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2746 	struct dpaa2_eth_xdp_fds *xdp_redirect_fds;
2747 	struct rtnl_link_stats64 *percpu_stats;
2748 	struct dpaa2_eth_fq *fq;
2749 	struct dpaa2_fd *fds;
2750 	int enqueued, i, err;
2751 
2752 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2753 		return -EINVAL;
2754 
2755 	if (!netif_running(net_dev))
2756 		return -ENETDOWN;
2757 
2758 	fq = &priv->fq[smp_processor_id()];
2759 	xdp_redirect_fds = &fq->xdp_redirect_fds;
2760 	fds = xdp_redirect_fds->fds;
2761 
2762 	percpu_stats = this_cpu_ptr(priv->percpu_stats);
2763 
2764 	/* create a FD for each xdp_frame in the list received */
2765 	for (i = 0; i < n; i++) {
2766 		err = dpaa2_eth_xdp_create_fd(net_dev, frames[i], &fds[i]);
2767 		if (err)
2768 			break;
2769 	}
2770 	xdp_redirect_fds->num = i;
2771 
2772 	/* enqueue all the frame descriptors */
2773 	enqueued = dpaa2_eth_xdp_flush(priv, fq, xdp_redirect_fds);
2774 
2775 	/* update statistics */
2776 	percpu_stats->tx_packets += enqueued;
2777 	for (i = 0; i < enqueued; i++)
2778 		percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
2779 
2780 	return enqueued;
2781 }
2782 
2783 static int update_xps(struct dpaa2_eth_priv *priv)
2784 {
2785 	struct net_device *net_dev = priv->net_dev;
2786 	struct cpumask xps_mask;
2787 	struct dpaa2_eth_fq *fq;
2788 	int i, num_queues, netdev_queues;
2789 	int err = 0;
2790 
2791 	num_queues = dpaa2_eth_queue_count(priv);
2792 	netdev_queues = (net_dev->num_tc ? : 1) * num_queues;
2793 
2794 	/* The first <num_queues> entries in priv->fq array are Tx/Tx conf
2795 	 * queues, so only process those
2796 	 */
2797 	for (i = 0; i < netdev_queues; i++) {
2798 		fq = &priv->fq[i % num_queues];
2799 
2800 		cpumask_clear(&xps_mask);
2801 		cpumask_set_cpu(fq->target_cpu, &xps_mask);
2802 
2803 		err = netif_set_xps_queue(net_dev, &xps_mask, i);
2804 		if (err) {
2805 			netdev_warn_once(net_dev, "Error setting XPS queue\n");
2806 			break;
2807 		}
2808 	}
2809 
2810 	return err;
2811 }
2812 
2813 static int dpaa2_eth_setup_mqprio(struct net_device *net_dev,
2814 				  struct tc_mqprio_qopt *mqprio)
2815 {
2816 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2817 	u8 num_tc, num_queues;
2818 	int i;
2819 
2820 	mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
2821 	num_queues = dpaa2_eth_queue_count(priv);
2822 	num_tc = mqprio->num_tc;
2823 
2824 	if (num_tc == net_dev->num_tc)
2825 		return 0;
2826 
2827 	if (num_tc  > dpaa2_eth_tc_count(priv)) {
2828 		netdev_err(net_dev, "Max %d traffic classes supported\n",
2829 			   dpaa2_eth_tc_count(priv));
2830 		return -EOPNOTSUPP;
2831 	}
2832 
2833 	if (!num_tc) {
2834 		netdev_reset_tc(net_dev);
2835 		netif_set_real_num_tx_queues(net_dev, num_queues);
2836 		goto out;
2837 	}
2838 
2839 	netdev_set_num_tc(net_dev, num_tc);
2840 	netif_set_real_num_tx_queues(net_dev, num_tc * num_queues);
2841 
2842 	for (i = 0; i < num_tc; i++)
2843 		netdev_set_tc_queue(net_dev, i, num_queues, i * num_queues);
2844 
2845 out:
2846 	update_xps(priv);
2847 
2848 	return 0;
2849 }
2850 
2851 #define bps_to_mbits(rate) (div_u64((rate), 1000000) * 8)
2852 
2853 static int dpaa2_eth_setup_tbf(struct net_device *net_dev, struct tc_tbf_qopt_offload *p)
2854 {
2855 	struct tc_tbf_qopt_offload_replace_params *cfg = &p->replace_params;
2856 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2857 	struct dpni_tx_shaping_cfg tx_cr_shaper = { 0 };
2858 	struct dpni_tx_shaping_cfg tx_er_shaper = { 0 };
2859 	int err;
2860 
2861 	if (p->command == TC_TBF_STATS)
2862 		return -EOPNOTSUPP;
2863 
2864 	/* Only per port Tx shaping */
2865 	if (p->parent != TC_H_ROOT)
2866 		return -EOPNOTSUPP;
2867 
2868 	if (p->command == TC_TBF_REPLACE) {
2869 		if (cfg->max_size > DPAA2_ETH_MAX_BURST_SIZE) {
2870 			netdev_err(net_dev, "burst size cannot be greater than %d\n",
2871 				   DPAA2_ETH_MAX_BURST_SIZE);
2872 			return -EINVAL;
2873 		}
2874 
2875 		tx_cr_shaper.max_burst_size = cfg->max_size;
2876 		/* The TBF interface is in bytes/s, whereas DPAA2 expects the
2877 		 * rate in Mbits/s
2878 		 */
2879 		tx_cr_shaper.rate_limit = bps_to_mbits(cfg->rate.rate_bytes_ps);
2880 	}
2881 
2882 	err = dpni_set_tx_shaping(priv->mc_io, 0, priv->mc_token, &tx_cr_shaper,
2883 				  &tx_er_shaper, 0);
2884 	if (err) {
2885 		netdev_err(net_dev, "dpni_set_tx_shaping() = %d\n", err);
2886 		return err;
2887 	}
2888 
2889 	return 0;
2890 }
2891 
2892 static int dpaa2_eth_setup_tc(struct net_device *net_dev,
2893 			      enum tc_setup_type type, void *type_data)
2894 {
2895 	switch (type) {
2896 	case TC_SETUP_QDISC_MQPRIO:
2897 		return dpaa2_eth_setup_mqprio(net_dev, type_data);
2898 	case TC_SETUP_QDISC_TBF:
2899 		return dpaa2_eth_setup_tbf(net_dev, type_data);
2900 	default:
2901 		return -EOPNOTSUPP;
2902 	}
2903 }
2904 
2905 static const struct net_device_ops dpaa2_eth_ops = {
2906 	.ndo_open = dpaa2_eth_open,
2907 	.ndo_start_xmit = dpaa2_eth_tx,
2908 	.ndo_stop = dpaa2_eth_stop,
2909 	.ndo_set_mac_address = dpaa2_eth_set_addr,
2910 	.ndo_get_stats64 = dpaa2_eth_get_stats,
2911 	.ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
2912 	.ndo_set_features = dpaa2_eth_set_features,
2913 	.ndo_eth_ioctl = dpaa2_eth_ioctl,
2914 	.ndo_change_mtu = dpaa2_eth_change_mtu,
2915 	.ndo_bpf = dpaa2_eth_xdp,
2916 	.ndo_xdp_xmit = dpaa2_eth_xdp_xmit,
2917 	.ndo_setup_tc = dpaa2_eth_setup_tc,
2918 	.ndo_vlan_rx_add_vid = dpaa2_eth_rx_add_vid,
2919 	.ndo_vlan_rx_kill_vid = dpaa2_eth_rx_kill_vid
2920 };
2921 
2922 static void dpaa2_eth_cdan_cb(struct dpaa2_io_notification_ctx *ctx)
2923 {
2924 	struct dpaa2_eth_channel *ch;
2925 
2926 	ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
2927 
2928 	/* Update NAPI statistics */
2929 	ch->stats.cdan++;
2930 
2931 	napi_schedule(&ch->napi);
2932 }
2933 
2934 /* Allocate and configure a DPCON object */
2935 static struct fsl_mc_device *dpaa2_eth_setup_dpcon(struct dpaa2_eth_priv *priv)
2936 {
2937 	struct fsl_mc_device *dpcon;
2938 	struct device *dev = priv->net_dev->dev.parent;
2939 	int err;
2940 
2941 	err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
2942 				     FSL_MC_POOL_DPCON, &dpcon);
2943 	if (err) {
2944 		if (err == -ENXIO)
2945 			err = -EPROBE_DEFER;
2946 		else
2947 			dev_info(dev, "Not enough DPCONs, will go on as-is\n");
2948 		return ERR_PTR(err);
2949 	}
2950 
2951 	err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
2952 	if (err) {
2953 		dev_err(dev, "dpcon_open() failed\n");
2954 		goto free;
2955 	}
2956 
2957 	err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
2958 	if (err) {
2959 		dev_err(dev, "dpcon_reset() failed\n");
2960 		goto close;
2961 	}
2962 
2963 	err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
2964 	if (err) {
2965 		dev_err(dev, "dpcon_enable() failed\n");
2966 		goto close;
2967 	}
2968 
2969 	return dpcon;
2970 
2971 close:
2972 	dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
2973 free:
2974 	fsl_mc_object_free(dpcon);
2975 
2976 	return ERR_PTR(err);
2977 }
2978 
2979 static void dpaa2_eth_free_dpcon(struct dpaa2_eth_priv *priv,
2980 				 struct fsl_mc_device *dpcon)
2981 {
2982 	dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
2983 	dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
2984 	fsl_mc_object_free(dpcon);
2985 }
2986 
2987 static struct dpaa2_eth_channel *dpaa2_eth_alloc_channel(struct dpaa2_eth_priv *priv)
2988 {
2989 	struct dpaa2_eth_channel *channel;
2990 	struct dpcon_attr attr;
2991 	struct device *dev = priv->net_dev->dev.parent;
2992 	int err;
2993 
2994 	channel = kzalloc(sizeof(*channel), GFP_KERNEL);
2995 	if (!channel)
2996 		return NULL;
2997 
2998 	channel->dpcon = dpaa2_eth_setup_dpcon(priv);
2999 	if (IS_ERR(channel->dpcon)) {
3000 		err = PTR_ERR(channel->dpcon);
3001 		goto err_setup;
3002 	}
3003 
3004 	err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
3005 				   &attr);
3006 	if (err) {
3007 		dev_err(dev, "dpcon_get_attributes() failed\n");
3008 		goto err_get_attr;
3009 	}
3010 
3011 	channel->dpcon_id = attr.id;
3012 	channel->ch_id = attr.qbman_ch_id;
3013 	channel->priv = priv;
3014 
3015 	return channel;
3016 
3017 err_get_attr:
3018 	dpaa2_eth_free_dpcon(priv, channel->dpcon);
3019 err_setup:
3020 	kfree(channel);
3021 	return ERR_PTR(err);
3022 }
3023 
3024 static void dpaa2_eth_free_channel(struct dpaa2_eth_priv *priv,
3025 				   struct dpaa2_eth_channel *channel)
3026 {
3027 	dpaa2_eth_free_dpcon(priv, channel->dpcon);
3028 	kfree(channel);
3029 }
3030 
3031 /* DPIO setup: allocate and configure QBMan channels, setup core affinity
3032  * and register data availability notifications
3033  */
3034 static int dpaa2_eth_setup_dpio(struct dpaa2_eth_priv *priv)
3035 {
3036 	struct dpaa2_io_notification_ctx *nctx;
3037 	struct dpaa2_eth_channel *channel;
3038 	struct dpcon_notification_cfg dpcon_notif_cfg;
3039 	struct device *dev = priv->net_dev->dev.parent;
3040 	int i, err;
3041 
3042 	/* We want the ability to spread ingress traffic (RX, TX conf) to as
3043 	 * many cores as possible, so we need one channel for each core
3044 	 * (unless there's fewer queues than cores, in which case the extra
3045 	 * channels would be wasted).
3046 	 * Allocate one channel per core and register it to the core's
3047 	 * affine DPIO. If not enough channels are available for all cores
3048 	 * or if some cores don't have an affine DPIO, there will be no
3049 	 * ingress frame processing on those cores.
3050 	 */
3051 	cpumask_clear(&priv->dpio_cpumask);
3052 	for_each_online_cpu(i) {
3053 		/* Try to allocate a channel */
3054 		channel = dpaa2_eth_alloc_channel(priv);
3055 		if (IS_ERR_OR_NULL(channel)) {
3056 			err = PTR_ERR_OR_ZERO(channel);
3057 			if (err != -EPROBE_DEFER)
3058 				dev_info(dev,
3059 					 "No affine channel for cpu %d and above\n", i);
3060 			goto err_alloc_ch;
3061 		}
3062 
3063 		priv->channel[priv->num_channels] = channel;
3064 
3065 		nctx = &channel->nctx;
3066 		nctx->is_cdan = 1;
3067 		nctx->cb = dpaa2_eth_cdan_cb;
3068 		nctx->id = channel->ch_id;
3069 		nctx->desired_cpu = i;
3070 
3071 		/* Register the new context */
3072 		channel->dpio = dpaa2_io_service_select(i);
3073 		err = dpaa2_io_service_register(channel->dpio, nctx, dev);
3074 		if (err) {
3075 			dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
3076 			/* If no affine DPIO for this core, there's probably
3077 			 * none available for next cores either. Signal we want
3078 			 * to retry later, in case the DPIO devices weren't
3079 			 * probed yet.
3080 			 */
3081 			err = -EPROBE_DEFER;
3082 			goto err_service_reg;
3083 		}
3084 
3085 		/* Register DPCON notification with MC */
3086 		dpcon_notif_cfg.dpio_id = nctx->dpio_id;
3087 		dpcon_notif_cfg.priority = 0;
3088 		dpcon_notif_cfg.user_ctx = nctx->qman64;
3089 		err = dpcon_set_notification(priv->mc_io, 0,
3090 					     channel->dpcon->mc_handle,
3091 					     &dpcon_notif_cfg);
3092 		if (err) {
3093 			dev_err(dev, "dpcon_set_notification failed()\n");
3094 			goto err_set_cdan;
3095 		}
3096 
3097 		/* If we managed to allocate a channel and also found an affine
3098 		 * DPIO for this core, add it to the final mask
3099 		 */
3100 		cpumask_set_cpu(i, &priv->dpio_cpumask);
3101 		priv->num_channels++;
3102 
3103 		/* Stop if we already have enough channels to accommodate all
3104 		 * RX and TX conf queues
3105 		 */
3106 		if (priv->num_channels == priv->dpni_attrs.num_queues)
3107 			break;
3108 	}
3109 
3110 	return 0;
3111 
3112 err_set_cdan:
3113 	dpaa2_io_service_deregister(channel->dpio, nctx, dev);
3114 err_service_reg:
3115 	dpaa2_eth_free_channel(priv, channel);
3116 err_alloc_ch:
3117 	if (err == -EPROBE_DEFER) {
3118 		for (i = 0; i < priv->num_channels; i++) {
3119 			channel = priv->channel[i];
3120 			nctx = &channel->nctx;
3121 			dpaa2_io_service_deregister(channel->dpio, nctx, dev);
3122 			dpaa2_eth_free_channel(priv, channel);
3123 		}
3124 		priv->num_channels = 0;
3125 		return err;
3126 	}
3127 
3128 	if (cpumask_empty(&priv->dpio_cpumask)) {
3129 		dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
3130 		return -ENODEV;
3131 	}
3132 
3133 	dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
3134 		 cpumask_pr_args(&priv->dpio_cpumask));
3135 
3136 	return 0;
3137 }
3138 
3139 static void dpaa2_eth_free_dpio(struct dpaa2_eth_priv *priv)
3140 {
3141 	struct device *dev = priv->net_dev->dev.parent;
3142 	struct dpaa2_eth_channel *ch;
3143 	int i;
3144 
3145 	/* deregister CDAN notifications and free channels */
3146 	for (i = 0; i < priv->num_channels; i++) {
3147 		ch = priv->channel[i];
3148 		dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev);
3149 		dpaa2_eth_free_channel(priv, ch);
3150 	}
3151 }
3152 
3153 static struct dpaa2_eth_channel *dpaa2_eth_get_affine_channel(struct dpaa2_eth_priv *priv,
3154 							      int cpu)
3155 {
3156 	struct device *dev = priv->net_dev->dev.parent;
3157 	int i;
3158 
3159 	for (i = 0; i < priv->num_channels; i++)
3160 		if (priv->channel[i]->nctx.desired_cpu == cpu)
3161 			return priv->channel[i];
3162 
3163 	/* We should never get here. Issue a warning and return
3164 	 * the first channel, because it's still better than nothing
3165 	 */
3166 	dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
3167 
3168 	return priv->channel[0];
3169 }
3170 
3171 static void dpaa2_eth_set_fq_affinity(struct dpaa2_eth_priv *priv)
3172 {
3173 	struct device *dev = priv->net_dev->dev.parent;
3174 	struct dpaa2_eth_fq *fq;
3175 	int rx_cpu, txc_cpu;
3176 	int i;
3177 
3178 	/* For each FQ, pick one channel/CPU to deliver frames to.
3179 	 * This may well change at runtime, either through irqbalance or
3180 	 * through direct user intervention.
3181 	 */
3182 	rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
3183 
3184 	for (i = 0; i < priv->num_fqs; i++) {
3185 		fq = &priv->fq[i];
3186 		switch (fq->type) {
3187 		case DPAA2_RX_FQ:
3188 		case DPAA2_RX_ERR_FQ:
3189 			fq->target_cpu = rx_cpu;
3190 			rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
3191 			if (rx_cpu >= nr_cpu_ids)
3192 				rx_cpu = cpumask_first(&priv->dpio_cpumask);
3193 			break;
3194 		case DPAA2_TX_CONF_FQ:
3195 			fq->target_cpu = txc_cpu;
3196 			txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
3197 			if (txc_cpu >= nr_cpu_ids)
3198 				txc_cpu = cpumask_first(&priv->dpio_cpumask);
3199 			break;
3200 		default:
3201 			dev_err(dev, "Unknown FQ type: %d\n", fq->type);
3202 		}
3203 		fq->channel = dpaa2_eth_get_affine_channel(priv, fq->target_cpu);
3204 	}
3205 
3206 	update_xps(priv);
3207 }
3208 
3209 static void dpaa2_eth_setup_fqs(struct dpaa2_eth_priv *priv)
3210 {
3211 	int i, j;
3212 
3213 	/* We have one TxConf FQ per Tx flow.
3214 	 * The number of Tx and Rx queues is the same.
3215 	 * Tx queues come first in the fq array.
3216 	 */
3217 	for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
3218 		priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
3219 		priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
3220 		priv->fq[priv->num_fqs++].flowid = (u16)i;
3221 	}
3222 
3223 	for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
3224 		for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
3225 			priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
3226 			priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
3227 			priv->fq[priv->num_fqs].tc = (u8)j;
3228 			priv->fq[priv->num_fqs++].flowid = (u16)i;
3229 		}
3230 	}
3231 
3232 	/* We have exactly one Rx error queue per DPNI */
3233 	priv->fq[priv->num_fqs].type = DPAA2_RX_ERR_FQ;
3234 	priv->fq[priv->num_fqs++].consume = dpaa2_eth_rx_err;
3235 
3236 	/* For each FQ, decide on which core to process incoming frames */
3237 	dpaa2_eth_set_fq_affinity(priv);
3238 }
3239 
3240 /* Allocate and configure a buffer pool */
3241 struct dpaa2_eth_bp *dpaa2_eth_allocate_dpbp(struct dpaa2_eth_priv *priv)
3242 {
3243 	struct device *dev = priv->net_dev->dev.parent;
3244 	struct fsl_mc_device *dpbp_dev;
3245 	struct dpbp_attr dpbp_attrs;
3246 	struct dpaa2_eth_bp *bp;
3247 	int err;
3248 
3249 	err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
3250 				     &dpbp_dev);
3251 	if (err) {
3252 		if (err == -ENXIO)
3253 			err = -EPROBE_DEFER;
3254 		else
3255 			dev_err(dev, "DPBP device allocation failed\n");
3256 		return ERR_PTR(err);
3257 	}
3258 
3259 	bp = kzalloc(sizeof(*bp), GFP_KERNEL);
3260 	if (!bp) {
3261 		err = -ENOMEM;
3262 		goto err_alloc;
3263 	}
3264 
3265 	err = dpbp_open(priv->mc_io, 0, dpbp_dev->obj_desc.id,
3266 			&dpbp_dev->mc_handle);
3267 	if (err) {
3268 		dev_err(dev, "dpbp_open() failed\n");
3269 		goto err_open;
3270 	}
3271 
3272 	err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
3273 	if (err) {
3274 		dev_err(dev, "dpbp_reset() failed\n");
3275 		goto err_reset;
3276 	}
3277 
3278 	err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
3279 	if (err) {
3280 		dev_err(dev, "dpbp_enable() failed\n");
3281 		goto err_enable;
3282 	}
3283 
3284 	err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
3285 				  &dpbp_attrs);
3286 	if (err) {
3287 		dev_err(dev, "dpbp_get_attributes() failed\n");
3288 		goto err_get_attr;
3289 	}
3290 
3291 	bp->dev = dpbp_dev;
3292 	bp->bpid = dpbp_attrs.bpid;
3293 
3294 	return bp;
3295 
3296 err_get_attr:
3297 	dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
3298 err_enable:
3299 err_reset:
3300 	dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
3301 err_open:
3302 	kfree(bp);
3303 err_alloc:
3304 	fsl_mc_object_free(dpbp_dev);
3305 
3306 	return ERR_PTR(err);
3307 }
3308 
3309 static int dpaa2_eth_setup_default_dpbp(struct dpaa2_eth_priv *priv)
3310 {
3311 	struct dpaa2_eth_bp *bp;
3312 	int i;
3313 
3314 	bp = dpaa2_eth_allocate_dpbp(priv);
3315 	if (IS_ERR(bp))
3316 		return PTR_ERR(bp);
3317 
3318 	priv->bp[DPAA2_ETH_DEFAULT_BP_IDX] = bp;
3319 	priv->num_bps++;
3320 
3321 	for (i = 0; i < priv->num_channels; i++)
3322 		priv->channel[i]->bp = bp;
3323 
3324 	return 0;
3325 }
3326 
3327 void dpaa2_eth_free_dpbp(struct dpaa2_eth_priv *priv, struct dpaa2_eth_bp *bp)
3328 {
3329 	int idx_bp;
3330 
3331 	/* Find the index at which this BP is stored */
3332 	for (idx_bp = 0; idx_bp < priv->num_bps; idx_bp++)
3333 		if (priv->bp[idx_bp] == bp)
3334 			break;
3335 
3336 	/* Drain the pool and disable the associated MC object */
3337 	dpaa2_eth_drain_pool(priv, bp->bpid);
3338 	dpbp_disable(priv->mc_io, 0, bp->dev->mc_handle);
3339 	dpbp_close(priv->mc_io, 0, bp->dev->mc_handle);
3340 	fsl_mc_object_free(bp->dev);
3341 	kfree(bp);
3342 
3343 	/* Move the last in use DPBP over in this position */
3344 	priv->bp[idx_bp] = priv->bp[priv->num_bps - 1];
3345 	priv->num_bps--;
3346 }
3347 
3348 static void dpaa2_eth_free_dpbps(struct dpaa2_eth_priv *priv)
3349 {
3350 	int i;
3351 
3352 	for (i = 0; i < priv->num_bps; i++)
3353 		dpaa2_eth_free_dpbp(priv, priv->bp[i]);
3354 }
3355 
3356 static int dpaa2_eth_set_buffer_layout(struct dpaa2_eth_priv *priv)
3357 {
3358 	struct device *dev = priv->net_dev->dev.parent;
3359 	struct dpni_buffer_layout buf_layout = {0};
3360 	u16 rx_buf_align;
3361 	int err;
3362 
3363 	/* We need to check for WRIOP version 1.0.0, but depending on the MC
3364 	 * version, this number is not always provided correctly on rev1.
3365 	 * We need to check for both alternatives in this situation.
3366 	 */
3367 	if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
3368 	    priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
3369 		rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
3370 	else
3371 		rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
3372 
3373 	/* We need to ensure that the buffer size seen by WRIOP is a multiple
3374 	 * of 64 or 256 bytes depending on the WRIOP version.
3375 	 */
3376 	priv->rx_buf_size = ALIGN_DOWN(DPAA2_ETH_RX_BUF_SIZE, rx_buf_align);
3377 
3378 	/* tx buffer */
3379 	buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
3380 	buf_layout.pass_timestamp = true;
3381 	buf_layout.pass_frame_status = true;
3382 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
3383 			     DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
3384 			     DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
3385 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3386 				     DPNI_QUEUE_TX, &buf_layout);
3387 	if (err) {
3388 		dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
3389 		return err;
3390 	}
3391 
3392 	/* tx-confirm buffer */
3393 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
3394 			     DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
3395 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3396 				     DPNI_QUEUE_TX_CONFIRM, &buf_layout);
3397 	if (err) {
3398 		dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
3399 		return err;
3400 	}
3401 
3402 	/* Now that we've set our tx buffer layout, retrieve the minimum
3403 	 * required tx data offset.
3404 	 */
3405 	err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
3406 				      &priv->tx_data_offset);
3407 	if (err) {
3408 		dev_err(dev, "dpni_get_tx_data_offset() failed\n");
3409 		return err;
3410 	}
3411 
3412 	if ((priv->tx_data_offset % 64) != 0)
3413 		dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
3414 			 priv->tx_data_offset);
3415 
3416 	/* rx buffer */
3417 	buf_layout.pass_frame_status = true;
3418 	buf_layout.pass_parser_result = true;
3419 	buf_layout.data_align = rx_buf_align;
3420 	buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
3421 	buf_layout.private_data_size = 0;
3422 	buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
3423 			     DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
3424 			     DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
3425 			     DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
3426 			     DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
3427 	err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3428 				     DPNI_QUEUE_RX, &buf_layout);
3429 	if (err) {
3430 		dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
3431 		return err;
3432 	}
3433 
3434 	return 0;
3435 }
3436 
3437 #define DPNI_ENQUEUE_FQID_VER_MAJOR	7
3438 #define DPNI_ENQUEUE_FQID_VER_MINOR	9
3439 
3440 static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv,
3441 				       struct dpaa2_eth_fq *fq,
3442 				       struct dpaa2_fd *fd, u8 prio,
3443 				       u32 num_frames __always_unused,
3444 				       int *frames_enqueued)
3445 {
3446 	int err;
3447 
3448 	err = dpaa2_io_service_enqueue_qd(fq->channel->dpio,
3449 					  priv->tx_qdid, prio,
3450 					  fq->tx_qdbin, fd);
3451 	if (!err && frames_enqueued)
3452 		*frames_enqueued = 1;
3453 	return err;
3454 }
3455 
3456 static inline int dpaa2_eth_enqueue_fq_multiple(struct dpaa2_eth_priv *priv,
3457 						struct dpaa2_eth_fq *fq,
3458 						struct dpaa2_fd *fd,
3459 						u8 prio, u32 num_frames,
3460 						int *frames_enqueued)
3461 {
3462 	int err;
3463 
3464 	err = dpaa2_io_service_enqueue_multiple_fq(fq->channel->dpio,
3465 						   fq->tx_fqid[prio],
3466 						   fd, num_frames);
3467 
3468 	if (err == 0)
3469 		return -EBUSY;
3470 
3471 	if (frames_enqueued)
3472 		*frames_enqueued = err;
3473 	return 0;
3474 }
3475 
3476 static void dpaa2_eth_set_enqueue_mode(struct dpaa2_eth_priv *priv)
3477 {
3478 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3479 				   DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3480 		priv->enqueue = dpaa2_eth_enqueue_qd;
3481 	else
3482 		priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
3483 }
3484 
3485 static int dpaa2_eth_set_pause(struct dpaa2_eth_priv *priv)
3486 {
3487 	struct device *dev = priv->net_dev->dev.parent;
3488 	struct dpni_link_cfg link_cfg = {0};
3489 	int err;
3490 
3491 	/* Get the default link options so we don't override other flags */
3492 	err = dpni_get_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
3493 	if (err) {
3494 		dev_err(dev, "dpni_get_link_cfg() failed\n");
3495 		return err;
3496 	}
3497 
3498 	/* By default, enable both Rx and Tx pause frames */
3499 	link_cfg.options |= DPNI_LINK_OPT_PAUSE;
3500 	link_cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
3501 	err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
3502 	if (err) {
3503 		dev_err(dev, "dpni_set_link_cfg() failed\n");
3504 		return err;
3505 	}
3506 
3507 	priv->link_state.options = link_cfg.options;
3508 
3509 	return 0;
3510 }
3511 
3512 static void dpaa2_eth_update_tx_fqids(struct dpaa2_eth_priv *priv)
3513 {
3514 	struct dpni_queue_id qid = {0};
3515 	struct dpaa2_eth_fq *fq;
3516 	struct dpni_queue queue;
3517 	int i, j, err;
3518 
3519 	/* We only use Tx FQIDs for FQID-based enqueue, so check
3520 	 * if DPNI version supports it before updating FQIDs
3521 	 */
3522 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3523 				   DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3524 		return;
3525 
3526 	for (i = 0; i < priv->num_fqs; i++) {
3527 		fq = &priv->fq[i];
3528 		if (fq->type != DPAA2_TX_CONF_FQ)
3529 			continue;
3530 		for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
3531 			err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3532 					     DPNI_QUEUE_TX, j, fq->flowid,
3533 					     &queue, &qid);
3534 			if (err)
3535 				goto out_err;
3536 
3537 			fq->tx_fqid[j] = qid.fqid;
3538 			if (fq->tx_fqid[j] == 0)
3539 				goto out_err;
3540 		}
3541 	}
3542 
3543 	priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
3544 
3545 	return;
3546 
3547 out_err:
3548 	netdev_info(priv->net_dev,
3549 		    "Error reading Tx FQID, fallback to QDID-based enqueue\n");
3550 	priv->enqueue = dpaa2_eth_enqueue_qd;
3551 }
3552 
3553 /* Configure ingress classification based on VLAN PCP */
3554 static int dpaa2_eth_set_vlan_qos(struct dpaa2_eth_priv *priv)
3555 {
3556 	struct device *dev = priv->net_dev->dev.parent;
3557 	struct dpkg_profile_cfg kg_cfg = {0};
3558 	struct dpni_qos_tbl_cfg qos_cfg = {0};
3559 	struct dpni_rule_cfg key_params;
3560 	void *dma_mem, *key, *mask;
3561 	u8 key_size = 2;	/* VLAN TCI field */
3562 	int i, pcp, err;
3563 
3564 	/* VLAN-based classification only makes sense if we have multiple
3565 	 * traffic classes.
3566 	 * Also, we need to extract just the 3-bit PCP field from the VLAN
3567 	 * header and we can only do that by using a mask
3568 	 */
3569 	if (dpaa2_eth_tc_count(priv) == 1 || !dpaa2_eth_fs_mask_enabled(priv)) {
3570 		dev_dbg(dev, "VLAN-based QoS classification not supported\n");
3571 		return -EOPNOTSUPP;
3572 	}
3573 
3574 	dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
3575 	if (!dma_mem)
3576 		return -ENOMEM;
3577 
3578 	kg_cfg.num_extracts = 1;
3579 	kg_cfg.extracts[0].type = DPKG_EXTRACT_FROM_HDR;
3580 	kg_cfg.extracts[0].extract.from_hdr.prot = NET_PROT_VLAN;
3581 	kg_cfg.extracts[0].extract.from_hdr.type = DPKG_FULL_FIELD;
3582 	kg_cfg.extracts[0].extract.from_hdr.field = NH_FLD_VLAN_TCI;
3583 
3584 	err = dpni_prepare_key_cfg(&kg_cfg, dma_mem);
3585 	if (err) {
3586 		dev_err(dev, "dpni_prepare_key_cfg failed\n");
3587 		goto out_free_tbl;
3588 	}
3589 
3590 	/* set QoS table */
3591 	qos_cfg.default_tc = 0;
3592 	qos_cfg.discard_on_miss = 0;
3593 	qos_cfg.key_cfg_iova = dma_map_single(dev, dma_mem,
3594 					      DPAA2_CLASSIFIER_DMA_SIZE,
3595 					      DMA_TO_DEVICE);
3596 	if (dma_mapping_error(dev, qos_cfg.key_cfg_iova)) {
3597 		dev_err(dev, "QoS table DMA mapping failed\n");
3598 		err = -ENOMEM;
3599 		goto out_free_tbl;
3600 	}
3601 
3602 	err = dpni_set_qos_table(priv->mc_io, 0, priv->mc_token, &qos_cfg);
3603 	if (err) {
3604 		dev_err(dev, "dpni_set_qos_table failed\n");
3605 		goto out_unmap_tbl;
3606 	}
3607 
3608 	/* Add QoS table entries */
3609 	key = kzalloc(key_size * 2, GFP_KERNEL);
3610 	if (!key) {
3611 		err = -ENOMEM;
3612 		goto out_unmap_tbl;
3613 	}
3614 	mask = key + key_size;
3615 	*(__be16 *)mask = cpu_to_be16(VLAN_PRIO_MASK);
3616 
3617 	key_params.key_iova = dma_map_single(dev, key, key_size * 2,
3618 					     DMA_TO_DEVICE);
3619 	if (dma_mapping_error(dev, key_params.key_iova)) {
3620 		dev_err(dev, "Qos table entry DMA mapping failed\n");
3621 		err = -ENOMEM;
3622 		goto out_free_key;
3623 	}
3624 
3625 	key_params.mask_iova = key_params.key_iova + key_size;
3626 	key_params.key_size = key_size;
3627 
3628 	/* We add rules for PCP-based distribution starting with highest
3629 	 * priority (VLAN PCP = 7). If this DPNI doesn't have enough traffic
3630 	 * classes to accommodate all priority levels, the lowest ones end up
3631 	 * on TC 0 which was configured as default
3632 	 */
3633 	for (i = dpaa2_eth_tc_count(priv) - 1, pcp = 7; i >= 0; i--, pcp--) {
3634 		*(__be16 *)key = cpu_to_be16(pcp << VLAN_PRIO_SHIFT);
3635 		dma_sync_single_for_device(dev, key_params.key_iova,
3636 					   key_size * 2, DMA_TO_DEVICE);
3637 
3638 		err = dpni_add_qos_entry(priv->mc_io, 0, priv->mc_token,
3639 					 &key_params, i, i);
3640 		if (err) {
3641 			dev_err(dev, "dpni_add_qos_entry failed\n");
3642 			dpni_clear_qos_table(priv->mc_io, 0, priv->mc_token);
3643 			goto out_unmap_key;
3644 		}
3645 	}
3646 
3647 	priv->vlan_cls_enabled = true;
3648 
3649 	/* Table and key memory is not persistent, clean everything up after
3650 	 * configuration is finished
3651 	 */
3652 out_unmap_key:
3653 	dma_unmap_single(dev, key_params.key_iova, key_size * 2, DMA_TO_DEVICE);
3654 out_free_key:
3655 	kfree(key);
3656 out_unmap_tbl:
3657 	dma_unmap_single(dev, qos_cfg.key_cfg_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3658 			 DMA_TO_DEVICE);
3659 out_free_tbl:
3660 	kfree(dma_mem);
3661 
3662 	return err;
3663 }
3664 
3665 /* Configure the DPNI object this interface is associated with */
3666 static int dpaa2_eth_setup_dpni(struct fsl_mc_device *ls_dev)
3667 {
3668 	struct device *dev = &ls_dev->dev;
3669 	struct dpaa2_eth_priv *priv;
3670 	struct net_device *net_dev;
3671 	int err;
3672 
3673 	net_dev = dev_get_drvdata(dev);
3674 	priv = netdev_priv(net_dev);
3675 
3676 	/* get a handle for the DPNI object */
3677 	err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
3678 	if (err) {
3679 		dev_err(dev, "dpni_open() failed\n");
3680 		return err;
3681 	}
3682 
3683 	/* Check if we can work with this DPNI object */
3684 	err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
3685 				   &priv->dpni_ver_minor);
3686 	if (err) {
3687 		dev_err(dev, "dpni_get_api_version() failed\n");
3688 		goto close;
3689 	}
3690 	if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
3691 		dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
3692 			priv->dpni_ver_major, priv->dpni_ver_minor,
3693 			DPNI_VER_MAJOR, DPNI_VER_MINOR);
3694 		err = -ENOTSUPP;
3695 		goto close;
3696 	}
3697 
3698 	ls_dev->mc_io = priv->mc_io;
3699 	ls_dev->mc_handle = priv->mc_token;
3700 
3701 	err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3702 	if (err) {
3703 		dev_err(dev, "dpni_reset() failed\n");
3704 		goto close;
3705 	}
3706 
3707 	err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
3708 				  &priv->dpni_attrs);
3709 	if (err) {
3710 		dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
3711 		goto close;
3712 	}
3713 
3714 	err = dpaa2_eth_set_buffer_layout(priv);
3715 	if (err)
3716 		goto close;
3717 
3718 	dpaa2_eth_set_enqueue_mode(priv);
3719 
3720 	/* Enable pause frame support */
3721 	if (dpaa2_eth_has_pause_support(priv)) {
3722 		err = dpaa2_eth_set_pause(priv);
3723 		if (err)
3724 			goto close;
3725 	}
3726 
3727 	err = dpaa2_eth_set_vlan_qos(priv);
3728 	if (err && err != -EOPNOTSUPP)
3729 		goto close;
3730 
3731 	priv->cls_rules = devm_kcalloc(dev, dpaa2_eth_fs_count(priv),
3732 				       sizeof(struct dpaa2_eth_cls_rule),
3733 				       GFP_KERNEL);
3734 	if (!priv->cls_rules) {
3735 		err = -ENOMEM;
3736 		goto close;
3737 	}
3738 
3739 	return 0;
3740 
3741 close:
3742 	dpni_close(priv->mc_io, 0, priv->mc_token);
3743 
3744 	return err;
3745 }
3746 
3747 static void dpaa2_eth_free_dpni(struct dpaa2_eth_priv *priv)
3748 {
3749 	int err;
3750 
3751 	err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3752 	if (err)
3753 		netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
3754 			    err);
3755 
3756 	dpni_close(priv->mc_io, 0, priv->mc_token);
3757 }
3758 
3759 static int dpaa2_eth_setup_rx_flow(struct dpaa2_eth_priv *priv,
3760 				   struct dpaa2_eth_fq *fq)
3761 {
3762 	struct device *dev = priv->net_dev->dev.parent;
3763 	struct dpni_queue queue;
3764 	struct dpni_queue_id qid;
3765 	int err;
3766 
3767 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3768 			     DPNI_QUEUE_RX, fq->tc, fq->flowid, &queue, &qid);
3769 	if (err) {
3770 		dev_err(dev, "dpni_get_queue(RX) failed\n");
3771 		return err;
3772 	}
3773 
3774 	fq->fqid = qid.fqid;
3775 
3776 	queue.destination.id = fq->channel->dpcon_id;
3777 	queue.destination.type = DPNI_DEST_DPCON;
3778 	queue.destination.priority = 1;
3779 	queue.user_context = (u64)(uintptr_t)fq;
3780 	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3781 			     DPNI_QUEUE_RX, fq->tc, fq->flowid,
3782 			     DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3783 			     &queue);
3784 	if (err) {
3785 		dev_err(dev, "dpni_set_queue(RX) failed\n");
3786 		return err;
3787 	}
3788 
3789 	/* xdp_rxq setup */
3790 	/* only once for each channel */
3791 	if (fq->tc > 0)
3792 		return 0;
3793 
3794 	err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev,
3795 			       fq->flowid, 0);
3796 	if (err) {
3797 		dev_err(dev, "xdp_rxq_info_reg failed\n");
3798 		return err;
3799 	}
3800 
3801 	err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq,
3802 					 MEM_TYPE_PAGE_ORDER0, NULL);
3803 	if (err) {
3804 		dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n");
3805 		return err;
3806 	}
3807 
3808 	return 0;
3809 }
3810 
3811 static int dpaa2_eth_setup_tx_flow(struct dpaa2_eth_priv *priv,
3812 				   struct dpaa2_eth_fq *fq)
3813 {
3814 	struct device *dev = priv->net_dev->dev.parent;
3815 	struct dpni_queue queue;
3816 	struct dpni_queue_id qid;
3817 	int i, err;
3818 
3819 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3820 		err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3821 				     DPNI_QUEUE_TX, i, fq->flowid,
3822 				     &queue, &qid);
3823 		if (err) {
3824 			dev_err(dev, "dpni_get_queue(TX) failed\n");
3825 			return err;
3826 		}
3827 		fq->tx_fqid[i] = qid.fqid;
3828 	}
3829 
3830 	/* All Tx queues belonging to the same flowid have the same qdbin */
3831 	fq->tx_qdbin = qid.qdbin;
3832 
3833 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3834 			     DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3835 			     &queue, &qid);
3836 	if (err) {
3837 		dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
3838 		return err;
3839 	}
3840 
3841 	fq->fqid = qid.fqid;
3842 
3843 	queue.destination.id = fq->channel->dpcon_id;
3844 	queue.destination.type = DPNI_DEST_DPCON;
3845 	queue.destination.priority = 0;
3846 	queue.user_context = (u64)(uintptr_t)fq;
3847 	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3848 			     DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3849 			     DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3850 			     &queue);
3851 	if (err) {
3852 		dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
3853 		return err;
3854 	}
3855 
3856 	return 0;
3857 }
3858 
3859 static int setup_rx_err_flow(struct dpaa2_eth_priv *priv,
3860 			     struct dpaa2_eth_fq *fq)
3861 {
3862 	struct device *dev = priv->net_dev->dev.parent;
3863 	struct dpni_queue q = { { 0 } };
3864 	struct dpni_queue_id qid;
3865 	u8 q_opt = DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST;
3866 	int err;
3867 
3868 	err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3869 			     DPNI_QUEUE_RX_ERR, 0, 0, &q, &qid);
3870 	if (err) {
3871 		dev_err(dev, "dpni_get_queue() failed (%d)\n", err);
3872 		return err;
3873 	}
3874 
3875 	fq->fqid = qid.fqid;
3876 
3877 	q.destination.id = fq->channel->dpcon_id;
3878 	q.destination.type = DPNI_DEST_DPCON;
3879 	q.destination.priority = 1;
3880 	q.user_context = (u64)(uintptr_t)fq;
3881 	err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3882 			     DPNI_QUEUE_RX_ERR, 0, 0, q_opt, &q);
3883 	if (err) {
3884 		dev_err(dev, "dpni_set_queue() failed (%d)\n", err);
3885 		return err;
3886 	}
3887 
3888 	return 0;
3889 }
3890 
3891 /* Supported header fields for Rx hash distribution key */
3892 static const struct dpaa2_eth_dist_fields dist_fields[] = {
3893 	{
3894 		/* L2 header */
3895 		.rxnfc_field = RXH_L2DA,
3896 		.cls_prot = NET_PROT_ETH,
3897 		.cls_field = NH_FLD_ETH_DA,
3898 		.id = DPAA2_ETH_DIST_ETHDST,
3899 		.size = 6,
3900 	}, {
3901 		.cls_prot = NET_PROT_ETH,
3902 		.cls_field = NH_FLD_ETH_SA,
3903 		.id = DPAA2_ETH_DIST_ETHSRC,
3904 		.size = 6,
3905 	}, {
3906 		/* This is the last ethertype field parsed:
3907 		 * depending on frame format, it can be the MAC ethertype
3908 		 * or the VLAN etype.
3909 		 */
3910 		.cls_prot = NET_PROT_ETH,
3911 		.cls_field = NH_FLD_ETH_TYPE,
3912 		.id = DPAA2_ETH_DIST_ETHTYPE,
3913 		.size = 2,
3914 	}, {
3915 		/* VLAN header */
3916 		.rxnfc_field = RXH_VLAN,
3917 		.cls_prot = NET_PROT_VLAN,
3918 		.cls_field = NH_FLD_VLAN_TCI,
3919 		.id = DPAA2_ETH_DIST_VLAN,
3920 		.size = 2,
3921 	}, {
3922 		/* IP header */
3923 		.rxnfc_field = RXH_IP_SRC,
3924 		.cls_prot = NET_PROT_IP,
3925 		.cls_field = NH_FLD_IP_SRC,
3926 		.id = DPAA2_ETH_DIST_IPSRC,
3927 		.size = 4,
3928 	}, {
3929 		.rxnfc_field = RXH_IP_DST,
3930 		.cls_prot = NET_PROT_IP,
3931 		.cls_field = NH_FLD_IP_DST,
3932 		.id = DPAA2_ETH_DIST_IPDST,
3933 		.size = 4,
3934 	}, {
3935 		.rxnfc_field = RXH_L3_PROTO,
3936 		.cls_prot = NET_PROT_IP,
3937 		.cls_field = NH_FLD_IP_PROTO,
3938 		.id = DPAA2_ETH_DIST_IPPROTO,
3939 		.size = 1,
3940 	}, {
3941 		/* Using UDP ports, this is functionally equivalent to raw
3942 		 * byte pairs from L4 header.
3943 		 */
3944 		.rxnfc_field = RXH_L4_B_0_1,
3945 		.cls_prot = NET_PROT_UDP,
3946 		.cls_field = NH_FLD_UDP_PORT_SRC,
3947 		.id = DPAA2_ETH_DIST_L4SRC,
3948 		.size = 2,
3949 	}, {
3950 		.rxnfc_field = RXH_L4_B_2_3,
3951 		.cls_prot = NET_PROT_UDP,
3952 		.cls_field = NH_FLD_UDP_PORT_DST,
3953 		.id = DPAA2_ETH_DIST_L4DST,
3954 		.size = 2,
3955 	},
3956 };
3957 
3958 /* Configure the Rx hash key using the legacy API */
3959 static int dpaa2_eth_config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
3960 {
3961 	struct device *dev = priv->net_dev->dev.parent;
3962 	struct dpni_rx_tc_dist_cfg dist_cfg;
3963 	int i, err = 0;
3964 
3965 	memset(&dist_cfg, 0, sizeof(dist_cfg));
3966 
3967 	dist_cfg.key_cfg_iova = key;
3968 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3969 	dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
3970 
3971 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3972 		err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token,
3973 					  i, &dist_cfg);
3974 		if (err) {
3975 			dev_err(dev, "dpni_set_rx_tc_dist failed\n");
3976 			break;
3977 		}
3978 	}
3979 
3980 	return err;
3981 }
3982 
3983 /* Configure the Rx hash key using the new API */
3984 static int dpaa2_eth_config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
3985 {
3986 	struct device *dev = priv->net_dev->dev.parent;
3987 	struct dpni_rx_dist_cfg dist_cfg;
3988 	int i, err = 0;
3989 
3990 	memset(&dist_cfg, 0, sizeof(dist_cfg));
3991 
3992 	dist_cfg.key_cfg_iova = key;
3993 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3994 	dist_cfg.enable = 1;
3995 
3996 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3997 		dist_cfg.tc = i;
3998 		err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token,
3999 					    &dist_cfg);
4000 		if (err) {
4001 			dev_err(dev, "dpni_set_rx_hash_dist failed\n");
4002 			break;
4003 		}
4004 
4005 		/* If the flow steering / hashing key is shared between all
4006 		 * traffic classes, install it just once
4007 		 */
4008 		if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
4009 			break;
4010 	}
4011 
4012 	return err;
4013 }
4014 
4015 /* Configure the Rx flow classification key */
4016 static int dpaa2_eth_config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
4017 {
4018 	struct device *dev = priv->net_dev->dev.parent;
4019 	struct dpni_rx_dist_cfg dist_cfg;
4020 	int i, err = 0;
4021 
4022 	memset(&dist_cfg, 0, sizeof(dist_cfg));
4023 
4024 	dist_cfg.key_cfg_iova = key;
4025 	dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
4026 	dist_cfg.enable = 1;
4027 
4028 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
4029 		dist_cfg.tc = i;
4030 		err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token,
4031 					  &dist_cfg);
4032 		if (err) {
4033 			dev_err(dev, "dpni_set_rx_fs_dist failed\n");
4034 			break;
4035 		}
4036 
4037 		/* If the flow steering / hashing key is shared between all
4038 		 * traffic classes, install it just once
4039 		 */
4040 		if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
4041 			break;
4042 	}
4043 
4044 	return err;
4045 }
4046 
4047 /* Size of the Rx flow classification key */
4048 int dpaa2_eth_cls_key_size(u64 fields)
4049 {
4050 	int i, size = 0;
4051 
4052 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
4053 		if (!(fields & dist_fields[i].id))
4054 			continue;
4055 		size += dist_fields[i].size;
4056 	}
4057 
4058 	return size;
4059 }
4060 
4061 /* Offset of header field in Rx classification key */
4062 int dpaa2_eth_cls_fld_off(int prot, int field)
4063 {
4064 	int i, off = 0;
4065 
4066 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
4067 		if (dist_fields[i].cls_prot == prot &&
4068 		    dist_fields[i].cls_field == field)
4069 			return off;
4070 		off += dist_fields[i].size;
4071 	}
4072 
4073 	WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
4074 	return 0;
4075 }
4076 
4077 /* Prune unused fields from the classification rule.
4078  * Used when masking is not supported
4079  */
4080 void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields)
4081 {
4082 	int off = 0, new_off = 0;
4083 	int i, size;
4084 
4085 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
4086 		size = dist_fields[i].size;
4087 		if (dist_fields[i].id & fields) {
4088 			memcpy(key_mem + new_off, key_mem + off, size);
4089 			new_off += size;
4090 		}
4091 		off += size;
4092 	}
4093 }
4094 
4095 /* Set Rx distribution (hash or flow classification) key
4096  * flags is a combination of RXH_ bits
4097  */
4098 static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
4099 				  enum dpaa2_eth_rx_dist type, u64 flags)
4100 {
4101 	struct device *dev = net_dev->dev.parent;
4102 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4103 	struct dpkg_profile_cfg cls_cfg;
4104 	u32 rx_hash_fields = 0;
4105 	dma_addr_t key_iova;
4106 	u8 *dma_mem;
4107 	int i;
4108 	int err = 0;
4109 
4110 	memset(&cls_cfg, 0, sizeof(cls_cfg));
4111 
4112 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
4113 		struct dpkg_extract *key =
4114 			&cls_cfg.extracts[cls_cfg.num_extracts];
4115 
4116 		/* For both Rx hashing and classification keys
4117 		 * we set only the selected fields.
4118 		 */
4119 		if (!(flags & dist_fields[i].id))
4120 			continue;
4121 		if (type == DPAA2_ETH_RX_DIST_HASH)
4122 			rx_hash_fields |= dist_fields[i].rxnfc_field;
4123 
4124 		if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
4125 			dev_err(dev, "error adding key extraction rule, too many rules?\n");
4126 			return -E2BIG;
4127 		}
4128 
4129 		key->type = DPKG_EXTRACT_FROM_HDR;
4130 		key->extract.from_hdr.prot = dist_fields[i].cls_prot;
4131 		key->extract.from_hdr.type = DPKG_FULL_FIELD;
4132 		key->extract.from_hdr.field = dist_fields[i].cls_field;
4133 		cls_cfg.num_extracts++;
4134 	}
4135 
4136 	dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
4137 	if (!dma_mem)
4138 		return -ENOMEM;
4139 
4140 	err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
4141 	if (err) {
4142 		dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
4143 		goto free_key;
4144 	}
4145 
4146 	/* Prepare for setting the rx dist */
4147 	key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
4148 				  DMA_TO_DEVICE);
4149 	if (dma_mapping_error(dev, key_iova)) {
4150 		dev_err(dev, "DMA mapping failed\n");
4151 		err = -ENOMEM;
4152 		goto free_key;
4153 	}
4154 
4155 	if (type == DPAA2_ETH_RX_DIST_HASH) {
4156 		if (dpaa2_eth_has_legacy_dist(priv))
4157 			err = dpaa2_eth_config_legacy_hash_key(priv, key_iova);
4158 		else
4159 			err = dpaa2_eth_config_hash_key(priv, key_iova);
4160 	} else {
4161 		err = dpaa2_eth_config_cls_key(priv, key_iova);
4162 	}
4163 
4164 	dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
4165 			 DMA_TO_DEVICE);
4166 	if (!err && type == DPAA2_ETH_RX_DIST_HASH)
4167 		priv->rx_hash_fields = rx_hash_fields;
4168 
4169 free_key:
4170 	kfree(dma_mem);
4171 	return err;
4172 }
4173 
4174 int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
4175 {
4176 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4177 	u64 key = 0;
4178 	int i;
4179 
4180 	if (!dpaa2_eth_hash_enabled(priv))
4181 		return -EOPNOTSUPP;
4182 
4183 	for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
4184 		if (dist_fields[i].rxnfc_field & flags)
4185 			key |= dist_fields[i].id;
4186 
4187 	return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, key);
4188 }
4189 
4190 int dpaa2_eth_set_cls(struct net_device *net_dev, u64 flags)
4191 {
4192 	return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_CLS, flags);
4193 }
4194 
4195 static int dpaa2_eth_set_default_cls(struct dpaa2_eth_priv *priv)
4196 {
4197 	struct device *dev = priv->net_dev->dev.parent;
4198 	int err;
4199 
4200 	/* Check if we actually support Rx flow classification */
4201 	if (dpaa2_eth_has_legacy_dist(priv)) {
4202 		dev_dbg(dev, "Rx cls not supported by current MC version\n");
4203 		return -EOPNOTSUPP;
4204 	}
4205 
4206 	if (!dpaa2_eth_fs_enabled(priv)) {
4207 		dev_dbg(dev, "Rx cls disabled in DPNI options\n");
4208 		return -EOPNOTSUPP;
4209 	}
4210 
4211 	if (!dpaa2_eth_hash_enabled(priv)) {
4212 		dev_dbg(dev, "Rx cls disabled for single queue DPNIs\n");
4213 		return -EOPNOTSUPP;
4214 	}
4215 
4216 	/* If there is no support for masking in the classification table,
4217 	 * we don't set a default key, as it will depend on the rules
4218 	 * added by the user at runtime.
4219 	 */
4220 	if (!dpaa2_eth_fs_mask_enabled(priv))
4221 		goto out;
4222 
4223 	err = dpaa2_eth_set_cls(priv->net_dev, DPAA2_ETH_DIST_ALL);
4224 	if (err)
4225 		return err;
4226 
4227 out:
4228 	priv->rx_cls_enabled = 1;
4229 
4230 	return 0;
4231 }
4232 
4233 /* Bind the DPNI to its needed objects and resources: buffer pool, DPIOs,
4234  * frame queues and channels
4235  */
4236 static int dpaa2_eth_bind_dpni(struct dpaa2_eth_priv *priv)
4237 {
4238 	struct dpaa2_eth_bp *bp = priv->bp[DPAA2_ETH_DEFAULT_BP_IDX];
4239 	struct net_device *net_dev = priv->net_dev;
4240 	struct device *dev = net_dev->dev.parent;
4241 	struct dpni_pools_cfg pools_params;
4242 	struct dpni_error_cfg err_cfg;
4243 	int err = 0;
4244 	int i;
4245 
4246 	pools_params.num_dpbp = 1;
4247 	pools_params.pools[0].dpbp_id = bp->dev->obj_desc.id;
4248 	pools_params.pools[0].backup_pool = 0;
4249 	pools_params.pools[0].buffer_size = priv->rx_buf_size;
4250 	err = dpni_set_pools(priv->mc_io, 0, priv->mc_token, &pools_params);
4251 	if (err) {
4252 		dev_err(dev, "dpni_set_pools() failed\n");
4253 		return err;
4254 	}
4255 
4256 	/* have the interface implicitly distribute traffic based on
4257 	 * the default hash key
4258 	 */
4259 	err = dpaa2_eth_set_hash(net_dev, DPAA2_RXH_DEFAULT);
4260 	if (err && err != -EOPNOTSUPP)
4261 		dev_err(dev, "Failed to configure hashing\n");
4262 
4263 	/* Configure the flow classification key; it includes all
4264 	 * supported header fields and cannot be modified at runtime
4265 	 */
4266 	err = dpaa2_eth_set_default_cls(priv);
4267 	if (err && err != -EOPNOTSUPP)
4268 		dev_err(dev, "Failed to configure Rx classification key\n");
4269 
4270 	/* Configure handling of error frames */
4271 	err_cfg.errors = DPAA2_FAS_RX_ERR_MASK;
4272 	err_cfg.set_frame_annotation = 1;
4273 	err_cfg.error_action = DPNI_ERROR_ACTION_DISCARD;
4274 	err = dpni_set_errors_behavior(priv->mc_io, 0, priv->mc_token,
4275 				       &err_cfg);
4276 	if (err) {
4277 		dev_err(dev, "dpni_set_errors_behavior failed\n");
4278 		return err;
4279 	}
4280 
4281 	/* Configure Rx and Tx conf queues to generate CDANs */
4282 	for (i = 0; i < priv->num_fqs; i++) {
4283 		switch (priv->fq[i].type) {
4284 		case DPAA2_RX_FQ:
4285 			err = dpaa2_eth_setup_rx_flow(priv, &priv->fq[i]);
4286 			break;
4287 		case DPAA2_TX_CONF_FQ:
4288 			err = dpaa2_eth_setup_tx_flow(priv, &priv->fq[i]);
4289 			break;
4290 		case DPAA2_RX_ERR_FQ:
4291 			err = setup_rx_err_flow(priv, &priv->fq[i]);
4292 			break;
4293 		default:
4294 			dev_err(dev, "Invalid FQ type %d\n", priv->fq[i].type);
4295 			return -EINVAL;
4296 		}
4297 		if (err)
4298 			return err;
4299 	}
4300 
4301 	err = dpni_get_qdid(priv->mc_io, 0, priv->mc_token,
4302 			    DPNI_QUEUE_TX, &priv->tx_qdid);
4303 	if (err) {
4304 		dev_err(dev, "dpni_get_qdid() failed\n");
4305 		return err;
4306 	}
4307 
4308 	return 0;
4309 }
4310 
4311 /* Allocate rings for storing incoming frame descriptors */
4312 static int dpaa2_eth_alloc_rings(struct dpaa2_eth_priv *priv)
4313 {
4314 	struct net_device *net_dev = priv->net_dev;
4315 	struct device *dev = net_dev->dev.parent;
4316 	int i;
4317 
4318 	for (i = 0; i < priv->num_channels; i++) {
4319 		priv->channel[i]->store =
4320 			dpaa2_io_store_create(DPAA2_ETH_STORE_SIZE, dev);
4321 		if (!priv->channel[i]->store) {
4322 			netdev_err(net_dev, "dpaa2_io_store_create() failed\n");
4323 			goto err_ring;
4324 		}
4325 	}
4326 
4327 	return 0;
4328 
4329 err_ring:
4330 	for (i = 0; i < priv->num_channels; i++) {
4331 		if (!priv->channel[i]->store)
4332 			break;
4333 		dpaa2_io_store_destroy(priv->channel[i]->store);
4334 	}
4335 
4336 	return -ENOMEM;
4337 }
4338 
4339 static void dpaa2_eth_free_rings(struct dpaa2_eth_priv *priv)
4340 {
4341 	int i;
4342 
4343 	for (i = 0; i < priv->num_channels; i++)
4344 		dpaa2_io_store_destroy(priv->channel[i]->store);
4345 }
4346 
4347 static int dpaa2_eth_set_mac_addr(struct dpaa2_eth_priv *priv)
4348 {
4349 	struct net_device *net_dev = priv->net_dev;
4350 	struct device *dev = net_dev->dev.parent;
4351 	u8 mac_addr[ETH_ALEN], dpni_mac_addr[ETH_ALEN];
4352 	int err;
4353 
4354 	/* Get firmware address, if any */
4355 	err = dpni_get_port_mac_addr(priv->mc_io, 0, priv->mc_token, mac_addr);
4356 	if (err) {
4357 		dev_err(dev, "dpni_get_port_mac_addr() failed\n");
4358 		return err;
4359 	}
4360 
4361 	/* Get DPNI attributes address, if any */
4362 	err = dpni_get_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
4363 					dpni_mac_addr);
4364 	if (err) {
4365 		dev_err(dev, "dpni_get_primary_mac_addr() failed\n");
4366 		return err;
4367 	}
4368 
4369 	/* First check if firmware has any address configured by bootloader */
4370 	if (!is_zero_ether_addr(mac_addr)) {
4371 		/* If the DPMAC addr != DPNI addr, update it */
4372 		if (!ether_addr_equal(mac_addr, dpni_mac_addr)) {
4373 			err = dpni_set_primary_mac_addr(priv->mc_io, 0,
4374 							priv->mc_token,
4375 							mac_addr);
4376 			if (err) {
4377 				dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
4378 				return err;
4379 			}
4380 		}
4381 		eth_hw_addr_set(net_dev, mac_addr);
4382 	} else if (is_zero_ether_addr(dpni_mac_addr)) {
4383 		/* No MAC address configured, fill in net_dev->dev_addr
4384 		 * with a random one
4385 		 */
4386 		eth_hw_addr_random(net_dev);
4387 		dev_dbg_once(dev, "device(s) have all-zero hwaddr, replaced with random\n");
4388 
4389 		err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
4390 						net_dev->dev_addr);
4391 		if (err) {
4392 			dev_err(dev, "dpni_set_primary_mac_addr() failed\n");
4393 			return err;
4394 		}
4395 
4396 		/* Override NET_ADDR_RANDOM set by eth_hw_addr_random(); for all
4397 		 * practical purposes, this will be our "permanent" mac address,
4398 		 * at least until the next reboot. This move will also permit
4399 		 * register_netdevice() to properly fill up net_dev->perm_addr.
4400 		 */
4401 		net_dev->addr_assign_type = NET_ADDR_PERM;
4402 	} else {
4403 		/* NET_ADDR_PERM is default, all we have to do is
4404 		 * fill in the device addr.
4405 		 */
4406 		eth_hw_addr_set(net_dev, dpni_mac_addr);
4407 	}
4408 
4409 	return 0;
4410 }
4411 
4412 static int dpaa2_eth_netdev_init(struct net_device *net_dev)
4413 {
4414 	struct device *dev = net_dev->dev.parent;
4415 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4416 	u32 options = priv->dpni_attrs.options;
4417 	u64 supported = 0, not_supported = 0;
4418 	u8 bcast_addr[ETH_ALEN];
4419 	u8 num_queues;
4420 	int err;
4421 
4422 	net_dev->netdev_ops = &dpaa2_eth_ops;
4423 	net_dev->ethtool_ops = &dpaa2_ethtool_ops;
4424 
4425 	err = dpaa2_eth_set_mac_addr(priv);
4426 	if (err)
4427 		return err;
4428 
4429 	/* Explicitly add the broadcast address to the MAC filtering table */
4430 	eth_broadcast_addr(bcast_addr);
4431 	err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token, bcast_addr);
4432 	if (err) {
4433 		dev_err(dev, "dpni_add_mac_addr() failed\n");
4434 		return err;
4435 	}
4436 
4437 	/* Set MTU upper limit; lower limit is 68B (default value) */
4438 	net_dev->max_mtu = DPAA2_ETH_MAX_MTU;
4439 	err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token,
4440 					DPAA2_ETH_MFL);
4441 	if (err) {
4442 		dev_err(dev, "dpni_set_max_frame_length() failed\n");
4443 		return err;
4444 	}
4445 
4446 	/* Set actual number of queues in the net device */
4447 	num_queues = dpaa2_eth_queue_count(priv);
4448 	err = netif_set_real_num_tx_queues(net_dev, num_queues);
4449 	if (err) {
4450 		dev_err(dev, "netif_set_real_num_tx_queues() failed\n");
4451 		return err;
4452 	}
4453 	err = netif_set_real_num_rx_queues(net_dev, num_queues);
4454 	if (err) {
4455 		dev_err(dev, "netif_set_real_num_rx_queues() failed\n");
4456 		return err;
4457 	}
4458 
4459 	dpaa2_eth_detect_features(priv);
4460 
4461 	/* Capabilities listing */
4462 	supported |= IFF_LIVE_ADDR_CHANGE;
4463 
4464 	if (options & DPNI_OPT_NO_MAC_FILTER)
4465 		not_supported |= IFF_UNICAST_FLT;
4466 	else
4467 		supported |= IFF_UNICAST_FLT;
4468 
4469 	net_dev->priv_flags |= supported;
4470 	net_dev->priv_flags &= ~not_supported;
4471 
4472 	/* Features */
4473 	net_dev->features = NETIF_F_RXCSUM |
4474 			    NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
4475 			    NETIF_F_SG | NETIF_F_HIGHDMA |
4476 			    NETIF_F_LLTX | NETIF_F_HW_TC | NETIF_F_TSO;
4477 	net_dev->gso_max_segs = DPAA2_ETH_ENQUEUE_MAX_FDS;
4478 	net_dev->hw_features = net_dev->features;
4479 
4480 	if (priv->dpni_attrs.vlan_filter_entries)
4481 		net_dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
4482 
4483 	return 0;
4484 }
4485 
4486 static int dpaa2_eth_poll_link_state(void *arg)
4487 {
4488 	struct dpaa2_eth_priv *priv = (struct dpaa2_eth_priv *)arg;
4489 	int err;
4490 
4491 	while (!kthread_should_stop()) {
4492 		err = dpaa2_eth_link_state_update(priv);
4493 		if (unlikely(err))
4494 			return err;
4495 
4496 		msleep(DPAA2_ETH_LINK_STATE_REFRESH);
4497 	}
4498 
4499 	return 0;
4500 }
4501 
4502 static int dpaa2_eth_connect_mac(struct dpaa2_eth_priv *priv)
4503 {
4504 	struct fsl_mc_device *dpni_dev, *dpmac_dev;
4505 	struct dpaa2_mac *mac;
4506 	int err;
4507 
4508 	dpni_dev = to_fsl_mc_device(priv->net_dev->dev.parent);
4509 	dpmac_dev = fsl_mc_get_endpoint(dpni_dev, 0);
4510 
4511 	if (PTR_ERR(dpmac_dev) == -EPROBE_DEFER)
4512 		return PTR_ERR(dpmac_dev);
4513 
4514 	if (IS_ERR(dpmac_dev) || dpmac_dev->dev.type != &fsl_mc_bus_dpmac_type)
4515 		return 0;
4516 
4517 	mac = kzalloc(sizeof(struct dpaa2_mac), GFP_KERNEL);
4518 	if (!mac)
4519 		return -ENOMEM;
4520 
4521 	mac->mc_dev = dpmac_dev;
4522 	mac->mc_io = priv->mc_io;
4523 	mac->net_dev = priv->net_dev;
4524 
4525 	err = dpaa2_mac_open(mac);
4526 	if (err)
4527 		goto err_free_mac;
4528 	priv->mac = mac;
4529 
4530 	if (dpaa2_eth_is_type_phy(priv)) {
4531 		err = dpaa2_mac_connect(mac);
4532 		if (err && err != -EPROBE_DEFER)
4533 			netdev_err(priv->net_dev, "Error connecting to the MAC endpoint: %pe",
4534 				   ERR_PTR(err));
4535 		if (err)
4536 			goto err_close_mac;
4537 	}
4538 
4539 	return 0;
4540 
4541 err_close_mac:
4542 	dpaa2_mac_close(mac);
4543 	priv->mac = NULL;
4544 err_free_mac:
4545 	kfree(mac);
4546 	return err;
4547 }
4548 
4549 static void dpaa2_eth_disconnect_mac(struct dpaa2_eth_priv *priv)
4550 {
4551 	if (dpaa2_eth_is_type_phy(priv))
4552 		dpaa2_mac_disconnect(priv->mac);
4553 
4554 	if (!dpaa2_eth_has_mac(priv))
4555 		return;
4556 
4557 	dpaa2_mac_close(priv->mac);
4558 	kfree(priv->mac);
4559 	priv->mac = NULL;
4560 }
4561 
4562 static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)
4563 {
4564 	u32 status = ~0;
4565 	struct device *dev = (struct device *)arg;
4566 	struct fsl_mc_device *dpni_dev = to_fsl_mc_device(dev);
4567 	struct net_device *net_dev = dev_get_drvdata(dev);
4568 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
4569 	int err;
4570 
4571 	err = dpni_get_irq_status(dpni_dev->mc_io, 0, dpni_dev->mc_handle,
4572 				  DPNI_IRQ_INDEX, &status);
4573 	if (unlikely(err)) {
4574 		netdev_err(net_dev, "Can't get irq status (err %d)\n", err);
4575 		return IRQ_HANDLED;
4576 	}
4577 
4578 	if (status & DPNI_IRQ_EVENT_LINK_CHANGED)
4579 		dpaa2_eth_link_state_update(netdev_priv(net_dev));
4580 
4581 	if (status & DPNI_IRQ_EVENT_ENDPOINT_CHANGED) {
4582 		dpaa2_eth_set_mac_addr(netdev_priv(net_dev));
4583 		dpaa2_eth_update_tx_fqids(priv);
4584 
4585 		rtnl_lock();
4586 		if (dpaa2_eth_has_mac(priv))
4587 			dpaa2_eth_disconnect_mac(priv);
4588 		else
4589 			dpaa2_eth_connect_mac(priv);
4590 		rtnl_unlock();
4591 	}
4592 
4593 	return IRQ_HANDLED;
4594 }
4595 
4596 static int dpaa2_eth_setup_irqs(struct fsl_mc_device *ls_dev)
4597 {
4598 	int err = 0;
4599 	struct fsl_mc_device_irq *irq;
4600 
4601 	err = fsl_mc_allocate_irqs(ls_dev);
4602 	if (err) {
4603 		dev_err(&ls_dev->dev, "MC irqs allocation failed\n");
4604 		return err;
4605 	}
4606 
4607 	irq = ls_dev->irqs[0];
4608 	err = devm_request_threaded_irq(&ls_dev->dev, irq->virq,
4609 					NULL, dpni_irq0_handler_thread,
4610 					IRQF_NO_SUSPEND | IRQF_ONESHOT,
4611 					dev_name(&ls_dev->dev), &ls_dev->dev);
4612 	if (err < 0) {
4613 		dev_err(&ls_dev->dev, "devm_request_threaded_irq(): %d\n", err);
4614 		goto free_mc_irq;
4615 	}
4616 
4617 	err = dpni_set_irq_mask(ls_dev->mc_io, 0, ls_dev->mc_handle,
4618 				DPNI_IRQ_INDEX, DPNI_IRQ_EVENT_LINK_CHANGED |
4619 				DPNI_IRQ_EVENT_ENDPOINT_CHANGED);
4620 	if (err < 0) {
4621 		dev_err(&ls_dev->dev, "dpni_set_irq_mask(): %d\n", err);
4622 		goto free_irq;
4623 	}
4624 
4625 	err = dpni_set_irq_enable(ls_dev->mc_io, 0, ls_dev->mc_handle,
4626 				  DPNI_IRQ_INDEX, 1);
4627 	if (err < 0) {
4628 		dev_err(&ls_dev->dev, "dpni_set_irq_enable(): %d\n", err);
4629 		goto free_irq;
4630 	}
4631 
4632 	return 0;
4633 
4634 free_irq:
4635 	devm_free_irq(&ls_dev->dev, irq->virq, &ls_dev->dev);
4636 free_mc_irq:
4637 	fsl_mc_free_irqs(ls_dev);
4638 
4639 	return err;
4640 }
4641 
4642 static void dpaa2_eth_add_ch_napi(struct dpaa2_eth_priv *priv)
4643 {
4644 	int i;
4645 	struct dpaa2_eth_channel *ch;
4646 
4647 	for (i = 0; i < priv->num_channels; i++) {
4648 		ch = priv->channel[i];
4649 		/* NAPI weight *MUST* be a multiple of DPAA2_ETH_STORE_SIZE */
4650 		netif_napi_add(priv->net_dev, &ch->napi, dpaa2_eth_poll);
4651 	}
4652 }
4653 
4654 static void dpaa2_eth_del_ch_napi(struct dpaa2_eth_priv *priv)
4655 {
4656 	int i;
4657 	struct dpaa2_eth_channel *ch;
4658 
4659 	for (i = 0; i < priv->num_channels; i++) {
4660 		ch = priv->channel[i];
4661 		netif_napi_del(&ch->napi);
4662 	}
4663 }
4664 
4665 static int dpaa2_eth_probe(struct fsl_mc_device *dpni_dev)
4666 {
4667 	struct device *dev;
4668 	struct net_device *net_dev = NULL;
4669 	struct dpaa2_eth_priv *priv = NULL;
4670 	int err = 0;
4671 
4672 	dev = &dpni_dev->dev;
4673 
4674 	/* Net device */
4675 	net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA2_ETH_MAX_NETDEV_QUEUES);
4676 	if (!net_dev) {
4677 		dev_err(dev, "alloc_etherdev_mq() failed\n");
4678 		return -ENOMEM;
4679 	}
4680 
4681 	SET_NETDEV_DEV(net_dev, dev);
4682 	dev_set_drvdata(dev, net_dev);
4683 
4684 	priv = netdev_priv(net_dev);
4685 	priv->net_dev = net_dev;
4686 
4687 	priv->iommu_domain = iommu_get_domain_for_dev(dev);
4688 
4689 	priv->tx_tstamp_type = HWTSTAMP_TX_OFF;
4690 	priv->rx_tstamp = false;
4691 
4692 	priv->dpaa2_ptp_wq = alloc_workqueue("dpaa2_ptp_wq", 0, 0);
4693 	if (!priv->dpaa2_ptp_wq) {
4694 		err = -ENOMEM;
4695 		goto err_wq_alloc;
4696 	}
4697 
4698 	INIT_WORK(&priv->tx_onestep_tstamp, dpaa2_eth_tx_onestep_tstamp);
4699 	mutex_init(&priv->onestep_tstamp_lock);
4700 	skb_queue_head_init(&priv->tx_skbs);
4701 
4702 	priv->rx_copybreak = DPAA2_ETH_DEFAULT_COPYBREAK;
4703 
4704 	/* Obtain a MC portal */
4705 	err = fsl_mc_portal_allocate(dpni_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
4706 				     &priv->mc_io);
4707 	if (err) {
4708 		if (err == -ENXIO)
4709 			err = -EPROBE_DEFER;
4710 		else
4711 			dev_err(dev, "MC portal allocation failed\n");
4712 		goto err_portal_alloc;
4713 	}
4714 
4715 	/* MC objects initialization and configuration */
4716 	err = dpaa2_eth_setup_dpni(dpni_dev);
4717 	if (err)
4718 		goto err_dpni_setup;
4719 
4720 	err = dpaa2_eth_setup_dpio(priv);
4721 	if (err)
4722 		goto err_dpio_setup;
4723 
4724 	dpaa2_eth_setup_fqs(priv);
4725 
4726 	err = dpaa2_eth_setup_default_dpbp(priv);
4727 	if (err)
4728 		goto err_dpbp_setup;
4729 
4730 	err = dpaa2_eth_bind_dpni(priv);
4731 	if (err)
4732 		goto err_bind;
4733 
4734 	/* Add a NAPI context for each channel */
4735 	dpaa2_eth_add_ch_napi(priv);
4736 
4737 	/* Percpu statistics */
4738 	priv->percpu_stats = alloc_percpu(*priv->percpu_stats);
4739 	if (!priv->percpu_stats) {
4740 		dev_err(dev, "alloc_percpu(percpu_stats) failed\n");
4741 		err = -ENOMEM;
4742 		goto err_alloc_percpu_stats;
4743 	}
4744 	priv->percpu_extras = alloc_percpu(*priv->percpu_extras);
4745 	if (!priv->percpu_extras) {
4746 		dev_err(dev, "alloc_percpu(percpu_extras) failed\n");
4747 		err = -ENOMEM;
4748 		goto err_alloc_percpu_extras;
4749 	}
4750 
4751 	priv->sgt_cache = alloc_percpu(*priv->sgt_cache);
4752 	if (!priv->sgt_cache) {
4753 		dev_err(dev, "alloc_percpu(sgt_cache) failed\n");
4754 		err = -ENOMEM;
4755 		goto err_alloc_sgt_cache;
4756 	}
4757 
4758 	priv->fd = alloc_percpu(*priv->fd);
4759 	if (!priv->fd) {
4760 		dev_err(dev, "alloc_percpu(fds) failed\n");
4761 		err = -ENOMEM;
4762 		goto err_alloc_fds;
4763 	}
4764 
4765 	err = dpaa2_eth_netdev_init(net_dev);
4766 	if (err)
4767 		goto err_netdev_init;
4768 
4769 	/* Configure checksum offload based on current interface flags */
4770 	err = dpaa2_eth_set_rx_csum(priv, !!(net_dev->features & NETIF_F_RXCSUM));
4771 	if (err)
4772 		goto err_csum;
4773 
4774 	err = dpaa2_eth_set_tx_csum(priv,
4775 				    !!(net_dev->features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)));
4776 	if (err)
4777 		goto err_csum;
4778 
4779 	err = dpaa2_eth_alloc_rings(priv);
4780 	if (err)
4781 		goto err_alloc_rings;
4782 
4783 #ifdef CONFIG_FSL_DPAA2_ETH_DCB
4784 	if (dpaa2_eth_has_pause_support(priv) && priv->vlan_cls_enabled) {
4785 		priv->dcbx_mode = DCB_CAP_DCBX_HOST | DCB_CAP_DCBX_VER_IEEE;
4786 		net_dev->dcbnl_ops = &dpaa2_eth_dcbnl_ops;
4787 	} else {
4788 		dev_dbg(dev, "PFC not supported\n");
4789 	}
4790 #endif
4791 
4792 	err = dpaa2_eth_setup_irqs(dpni_dev);
4793 	if (err) {
4794 		netdev_warn(net_dev, "Failed to set link interrupt, fall back to polling\n");
4795 		priv->poll_thread = kthread_run(dpaa2_eth_poll_link_state, priv,
4796 						"%s_poll_link", net_dev->name);
4797 		if (IS_ERR(priv->poll_thread)) {
4798 			dev_err(dev, "Error starting polling thread\n");
4799 			goto err_poll_thread;
4800 		}
4801 		priv->do_link_poll = true;
4802 	}
4803 
4804 	err = dpaa2_eth_connect_mac(priv);
4805 	if (err)
4806 		goto err_connect_mac;
4807 
4808 	err = dpaa2_eth_dl_alloc(priv);
4809 	if (err)
4810 		goto err_dl_register;
4811 
4812 	err = dpaa2_eth_dl_traps_register(priv);
4813 	if (err)
4814 		goto err_dl_trap_register;
4815 
4816 	err = dpaa2_eth_dl_port_add(priv);
4817 	if (err)
4818 		goto err_dl_port_add;
4819 
4820 	err = register_netdev(net_dev);
4821 	if (err < 0) {
4822 		dev_err(dev, "register_netdev() failed\n");
4823 		goto err_netdev_reg;
4824 	}
4825 
4826 #ifdef CONFIG_DEBUG_FS
4827 	dpaa2_dbg_add(priv);
4828 #endif
4829 
4830 	dpaa2_eth_dl_register(priv);
4831 	dev_info(dev, "Probed interface %s\n", net_dev->name);
4832 	return 0;
4833 
4834 err_netdev_reg:
4835 	dpaa2_eth_dl_port_del(priv);
4836 err_dl_port_add:
4837 	dpaa2_eth_dl_traps_unregister(priv);
4838 err_dl_trap_register:
4839 	dpaa2_eth_dl_free(priv);
4840 err_dl_register:
4841 	dpaa2_eth_disconnect_mac(priv);
4842 err_connect_mac:
4843 	if (priv->do_link_poll)
4844 		kthread_stop(priv->poll_thread);
4845 	else
4846 		fsl_mc_free_irqs(dpni_dev);
4847 err_poll_thread:
4848 	dpaa2_eth_free_rings(priv);
4849 err_alloc_rings:
4850 err_csum:
4851 err_netdev_init:
4852 	free_percpu(priv->fd);
4853 err_alloc_fds:
4854 	free_percpu(priv->sgt_cache);
4855 err_alloc_sgt_cache:
4856 	free_percpu(priv->percpu_extras);
4857 err_alloc_percpu_extras:
4858 	free_percpu(priv->percpu_stats);
4859 err_alloc_percpu_stats:
4860 	dpaa2_eth_del_ch_napi(priv);
4861 err_bind:
4862 	dpaa2_eth_free_dpbps(priv);
4863 err_dpbp_setup:
4864 	dpaa2_eth_free_dpio(priv);
4865 err_dpio_setup:
4866 	dpaa2_eth_free_dpni(priv);
4867 err_dpni_setup:
4868 	fsl_mc_portal_free(priv->mc_io);
4869 err_portal_alloc:
4870 	destroy_workqueue(priv->dpaa2_ptp_wq);
4871 err_wq_alloc:
4872 	dev_set_drvdata(dev, NULL);
4873 	free_netdev(net_dev);
4874 
4875 	return err;
4876 }
4877 
4878 static int dpaa2_eth_remove(struct fsl_mc_device *ls_dev)
4879 {
4880 	struct device *dev;
4881 	struct net_device *net_dev;
4882 	struct dpaa2_eth_priv *priv;
4883 
4884 	dev = &ls_dev->dev;
4885 	net_dev = dev_get_drvdata(dev);
4886 	priv = netdev_priv(net_dev);
4887 
4888 	dpaa2_eth_dl_unregister(priv);
4889 
4890 #ifdef CONFIG_DEBUG_FS
4891 	dpaa2_dbg_remove(priv);
4892 #endif
4893 
4894 	unregister_netdev(net_dev);
4895 	rtnl_lock();
4896 	dpaa2_eth_disconnect_mac(priv);
4897 	rtnl_unlock();
4898 
4899 	dpaa2_eth_dl_port_del(priv);
4900 	dpaa2_eth_dl_traps_unregister(priv);
4901 	dpaa2_eth_dl_free(priv);
4902 
4903 	if (priv->do_link_poll)
4904 		kthread_stop(priv->poll_thread);
4905 	else
4906 		fsl_mc_free_irqs(ls_dev);
4907 
4908 	dpaa2_eth_free_rings(priv);
4909 	free_percpu(priv->fd);
4910 	free_percpu(priv->sgt_cache);
4911 	free_percpu(priv->percpu_stats);
4912 	free_percpu(priv->percpu_extras);
4913 
4914 	dpaa2_eth_del_ch_napi(priv);
4915 	dpaa2_eth_free_dpbps(priv);
4916 	dpaa2_eth_free_dpio(priv);
4917 	dpaa2_eth_free_dpni(priv);
4918 	if (priv->onestep_reg_base)
4919 		iounmap(priv->onestep_reg_base);
4920 
4921 	fsl_mc_portal_free(priv->mc_io);
4922 
4923 	destroy_workqueue(priv->dpaa2_ptp_wq);
4924 
4925 	dev_dbg(net_dev->dev.parent, "Removed interface %s\n", net_dev->name);
4926 
4927 	free_netdev(net_dev);
4928 
4929 	return 0;
4930 }
4931 
4932 static const struct fsl_mc_device_id dpaa2_eth_match_id_table[] = {
4933 	{
4934 		.vendor = FSL_MC_VENDOR_FREESCALE,
4935 		.obj_type = "dpni",
4936 	},
4937 	{ .vendor = 0x0 }
4938 };
4939 MODULE_DEVICE_TABLE(fslmc, dpaa2_eth_match_id_table);
4940 
4941 static struct fsl_mc_driver dpaa2_eth_driver = {
4942 	.driver = {
4943 		.name = KBUILD_MODNAME,
4944 		.owner = THIS_MODULE,
4945 	},
4946 	.probe = dpaa2_eth_probe,
4947 	.remove = dpaa2_eth_remove,
4948 	.match_id_table = dpaa2_eth_match_id_table
4949 };
4950 
4951 static int __init dpaa2_eth_driver_init(void)
4952 {
4953 	int err;
4954 
4955 	dpaa2_eth_dbg_init();
4956 	err = fsl_mc_driver_register(&dpaa2_eth_driver);
4957 	if (err) {
4958 		dpaa2_eth_dbg_exit();
4959 		return err;
4960 	}
4961 
4962 	return 0;
4963 }
4964 
4965 static void __exit dpaa2_eth_driver_exit(void)
4966 {
4967 	dpaa2_eth_dbg_exit();
4968 	fsl_mc_driver_unregister(&dpaa2_eth_driver);
4969 }
4970 
4971 module_init(dpaa2_eth_driver_init);
4972 module_exit(dpaa2_eth_driver_exit);
4973