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