1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later
2 /*
3  * Copyright 2008 - 2016 Freescale Semiconductor Inc.
4  * Copyright 2020 NXP
5  */
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/of_platform.h>
12 #include <linux/of_mdio.h>
13 #include <linux/of_net.h>
14 #include <linux/io.h>
15 #include <linux/if_arp.h>
16 #include <linux/if_vlan.h>
17 #include <linux/icmp.h>
18 #include <linux/ip.h>
19 #include <linux/ipv6.h>
20 #include <linux/udp.h>
21 #include <linux/tcp.h>
22 #include <linux/net.h>
23 #include <linux/skbuff.h>
24 #include <linux/etherdevice.h>
25 #include <linux/if_ether.h>
26 #include <linux/highmem.h>
27 #include <linux/percpu.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/sort.h>
30 #include <linux/phy_fixed.h>
31 #include <linux/bpf.h>
32 #include <linux/bpf_trace.h>
33 #include <soc/fsl/bman.h>
34 #include <soc/fsl/qman.h>
35 #include "fman.h"
36 #include "fman_port.h"
37 #include "mac.h"
38 #include "dpaa_eth.h"
39 
40 /* CREATE_TRACE_POINTS only needs to be defined once. Other dpaa files
41  * using trace events only need to #include <trace/events/sched.h>
42  */
43 #define CREATE_TRACE_POINTS
44 #include "dpaa_eth_trace.h"
45 
46 static int debug = -1;
47 module_param(debug, int, 0444);
48 MODULE_PARM_DESC(debug, "Module/Driver verbosity level (0=none,...,16=all)");
49 
50 static u16 tx_timeout = 1000;
51 module_param(tx_timeout, ushort, 0444);
52 MODULE_PARM_DESC(tx_timeout, "The Tx timeout in ms");
53 
54 #define FM_FD_STAT_RX_ERRORS						\
55 	(FM_FD_ERR_DMA | FM_FD_ERR_PHYSICAL	| \
56 	 FM_FD_ERR_SIZE | FM_FD_ERR_CLS_DISCARD | \
57 	 FM_FD_ERR_EXTRACTION | FM_FD_ERR_NO_SCHEME	| \
58 	 FM_FD_ERR_PRS_TIMEOUT | FM_FD_ERR_PRS_ILL_INSTRUCT | \
59 	 FM_FD_ERR_PRS_HDR_ERR)
60 
61 #define FM_FD_STAT_TX_ERRORS \
62 	(FM_FD_ERR_UNSUPPORTED_FORMAT | \
63 	 FM_FD_ERR_LENGTH | FM_FD_ERR_DMA)
64 
65 #define DPAA_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
66 			  NETIF_MSG_LINK | NETIF_MSG_IFUP | \
67 			  NETIF_MSG_IFDOWN | NETIF_MSG_HW)
68 
69 #define DPAA_INGRESS_CS_THRESHOLD 0x10000000
70 /* Ingress congestion threshold on FMan ports
71  * The size in bytes of the ingress tail-drop threshold on FMan ports.
72  * Traffic piling up above this value will be rejected by QMan and discarded
73  * by FMan.
74  */
75 
76 /* Size in bytes of the FQ taildrop threshold */
77 #define DPAA_FQ_TD 0x200000
78 
79 #define DPAA_CS_THRESHOLD_1G 0x06000000
80 /* Egress congestion threshold on 1G ports, range 0x1000 .. 0x10000000
81  * The size in bytes of the egress Congestion State notification threshold on
82  * 1G ports. The 1G dTSECs can quite easily be flooded by cores doing Tx in a
83  * tight loop (e.g. by sending UDP datagrams at "while(1) speed"),
84  * and the larger the frame size, the more acute the problem.
85  * So we have to find a balance between these factors:
86  * - avoiding the device staying congested for a prolonged time (risking
87  *   the netdev watchdog to fire - see also the tx_timeout module param);
88  * - affecting performance of protocols such as TCP, which otherwise
89  *   behave well under the congestion notification mechanism;
90  * - preventing the Tx cores from tightly-looping (as if the congestion
91  *   threshold was too low to be effective);
92  * - running out of memory if the CS threshold is set too high.
93  */
94 
95 #define DPAA_CS_THRESHOLD_10G 0x10000000
96 /* The size in bytes of the egress Congestion State notification threshold on
97  * 10G ports, range 0x1000 .. 0x10000000
98  */
99 
100 /* Largest value that the FQD's OAL field can hold */
101 #define FSL_QMAN_MAX_OAL	127
102 
103 /* Default alignment for start of data in an Rx FD */
104 #ifdef CONFIG_DPAA_ERRATUM_A050385
105 /* aligning data start to 64 avoids DMA transaction splits, unless the buffer
106  * is crossing a 4k page boundary
107  */
108 #define DPAA_FD_DATA_ALIGNMENT  (fman_has_errata_a050385() ? 64 : 16)
109 /* aligning to 256 avoids DMA transaction splits caused by 4k page boundary
110  * crossings; also, all SG fragments except the last must have a size multiple
111  * of 256 to avoid DMA transaction splits
112  */
113 #define DPAA_A050385_ALIGN 256
114 #define DPAA_FD_RX_DATA_ALIGNMENT (fman_has_errata_a050385() ? \
115 				   DPAA_A050385_ALIGN : 16)
116 #else
117 #define DPAA_FD_DATA_ALIGNMENT  16
118 #define DPAA_FD_RX_DATA_ALIGNMENT DPAA_FD_DATA_ALIGNMENT
119 #endif
120 
121 /* The DPAA requires 256 bytes reserved and mapped for the SGT */
122 #define DPAA_SGT_SIZE 256
123 
124 /* Values for the L3R field of the FM Parse Results
125  */
126 /* L3 Type field: First IP Present IPv4 */
127 #define FM_L3_PARSE_RESULT_IPV4	0x8000
128 /* L3 Type field: First IP Present IPv6 */
129 #define FM_L3_PARSE_RESULT_IPV6	0x4000
130 /* Values for the L4R field of the FM Parse Results */
131 /* L4 Type field: UDP */
132 #define FM_L4_PARSE_RESULT_UDP	0x40
133 /* L4 Type field: TCP */
134 #define FM_L4_PARSE_RESULT_TCP	0x20
135 
136 /* FD status field indicating whether the FM Parser has attempted to validate
137  * the L4 csum of the frame.
138  * Note that having this bit set doesn't necessarily imply that the checksum
139  * is valid. One would have to check the parse results to find that out.
140  */
141 #define FM_FD_STAT_L4CV         0x00000004
142 
143 #define DPAA_SGT_MAX_ENTRIES 16 /* maximum number of entries in SG Table */
144 #define DPAA_BUFF_RELEASE_MAX 8 /* maximum number of buffers released at once */
145 
146 #define FSL_DPAA_BPID_INV		0xff
147 #define FSL_DPAA_ETH_MAX_BUF_COUNT	128
148 #define FSL_DPAA_ETH_REFILL_THRESHOLD	80
149 
150 #define DPAA_TX_PRIV_DATA_SIZE	16
151 #define DPAA_PARSE_RESULTS_SIZE sizeof(struct fman_prs_result)
152 #define DPAA_TIME_STAMP_SIZE 8
153 #define DPAA_HASH_RESULTS_SIZE 8
154 #define DPAA_HWA_SIZE (DPAA_PARSE_RESULTS_SIZE + DPAA_TIME_STAMP_SIZE \
155 		       + DPAA_HASH_RESULTS_SIZE)
156 #define DPAA_RX_PRIV_DATA_DEFAULT_SIZE (DPAA_TX_PRIV_DATA_SIZE + \
157 					XDP_PACKET_HEADROOM - DPAA_HWA_SIZE)
158 #ifdef CONFIG_DPAA_ERRATUM_A050385
159 #define DPAA_RX_PRIV_DATA_A050385_SIZE (DPAA_A050385_ALIGN - DPAA_HWA_SIZE)
160 #define DPAA_RX_PRIV_DATA_SIZE (fman_has_errata_a050385() ? \
161 				DPAA_RX_PRIV_DATA_A050385_SIZE : \
162 				DPAA_RX_PRIV_DATA_DEFAULT_SIZE)
163 #else
164 #define DPAA_RX_PRIV_DATA_SIZE DPAA_RX_PRIV_DATA_DEFAULT_SIZE
165 #endif
166 
167 #define DPAA_ETH_PCD_RXQ_NUM	128
168 
169 #define DPAA_ENQUEUE_RETRIES	100000
170 
171 enum port_type {RX, TX};
172 
173 struct fm_port_fqs {
174 	struct dpaa_fq *tx_defq;
175 	struct dpaa_fq *tx_errq;
176 	struct dpaa_fq *rx_defq;
177 	struct dpaa_fq *rx_errq;
178 	struct dpaa_fq *rx_pcdq;
179 };
180 
181 /* All the dpa bps in use at any moment */
182 static struct dpaa_bp *dpaa_bp_array[BM_MAX_NUM_OF_POOLS];
183 
184 #define DPAA_BP_RAW_SIZE 4096
185 
186 #ifdef CONFIG_DPAA_ERRATUM_A050385
187 #define dpaa_bp_size(raw_size) (SKB_WITH_OVERHEAD(raw_size) & \
188 				~(DPAA_A050385_ALIGN - 1))
189 #else
190 #define dpaa_bp_size(raw_size) SKB_WITH_OVERHEAD(raw_size)
191 #endif
192 
193 static int dpaa_max_frm;
194 
195 static int dpaa_rx_extra_headroom;
196 
197 #define dpaa_get_max_mtu()	\
198 	(dpaa_max_frm - (VLAN_ETH_HLEN + ETH_FCS_LEN))
199 
200 static int dpaa_netdev_init(struct net_device *net_dev,
201 			    const struct net_device_ops *dpaa_ops,
202 			    u16 tx_timeout)
203 {
204 	struct dpaa_priv *priv = netdev_priv(net_dev);
205 	struct device *dev = net_dev->dev.parent;
206 	struct dpaa_percpu_priv *percpu_priv;
207 	const u8 *mac_addr;
208 	int i, err;
209 
210 	/* Although we access another CPU's private data here
211 	 * we do it at initialization so it is safe
212 	 */
213 	for_each_possible_cpu(i) {
214 		percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
215 		percpu_priv->net_dev = net_dev;
216 	}
217 
218 	net_dev->netdev_ops = dpaa_ops;
219 	mac_addr = priv->mac_dev->addr;
220 
221 	net_dev->mem_start = priv->mac_dev->res->start;
222 	net_dev->mem_end = priv->mac_dev->res->end;
223 
224 	net_dev->min_mtu = ETH_MIN_MTU;
225 	net_dev->max_mtu = dpaa_get_max_mtu();
226 
227 	net_dev->hw_features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
228 				 NETIF_F_LLTX | NETIF_F_RXHASH);
229 
230 	net_dev->hw_features |= NETIF_F_SG | NETIF_F_HIGHDMA;
231 	/* The kernels enables GSO automatically, if we declare NETIF_F_SG.
232 	 * For conformity, we'll still declare GSO explicitly.
233 	 */
234 	net_dev->features |= NETIF_F_GSO;
235 	net_dev->features |= NETIF_F_RXCSUM;
236 
237 	net_dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
238 	/* we do not want shared skbs on TX */
239 	net_dev->priv_flags &= ~IFF_TX_SKB_SHARING;
240 
241 	net_dev->features |= net_dev->hw_features;
242 	net_dev->vlan_features = net_dev->features;
243 
244 	if (is_valid_ether_addr(mac_addr)) {
245 		memcpy(net_dev->perm_addr, mac_addr, net_dev->addr_len);
246 		eth_hw_addr_set(net_dev, mac_addr);
247 	} else {
248 		eth_hw_addr_random(net_dev);
249 		err = priv->mac_dev->change_addr(priv->mac_dev->fman_mac,
250 			(const enet_addr_t *)net_dev->dev_addr);
251 		if (err) {
252 			dev_err(dev, "Failed to set random MAC address\n");
253 			return -EINVAL;
254 		}
255 		dev_info(dev, "Using random MAC address: %pM\n",
256 			 net_dev->dev_addr);
257 	}
258 
259 	net_dev->ethtool_ops = &dpaa_ethtool_ops;
260 
261 	net_dev->needed_headroom = priv->tx_headroom;
262 	net_dev->watchdog_timeo = msecs_to_jiffies(tx_timeout);
263 
264 	/* start without the RUNNING flag, phylib controls it later */
265 	netif_carrier_off(net_dev);
266 
267 	err = register_netdev(net_dev);
268 	if (err < 0) {
269 		dev_err(dev, "register_netdev() = %d\n", err);
270 		return err;
271 	}
272 
273 	return 0;
274 }
275 
276 static int dpaa_stop(struct net_device *net_dev)
277 {
278 	struct mac_device *mac_dev;
279 	struct dpaa_priv *priv;
280 	int i, err, error;
281 
282 	priv = netdev_priv(net_dev);
283 	mac_dev = priv->mac_dev;
284 
285 	netif_tx_stop_all_queues(net_dev);
286 	/* Allow the Fman (Tx) port to process in-flight frames before we
287 	 * try switching it off.
288 	 */
289 	msleep(200);
290 
291 	if (mac_dev->phy_dev)
292 		phy_stop(mac_dev->phy_dev);
293 	err = mac_dev->disable(mac_dev->fman_mac);
294 	if (err < 0)
295 		netif_err(priv, ifdown, net_dev, "mac_dev->disable() = %d\n",
296 			  err);
297 
298 	for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++) {
299 		error = fman_port_disable(mac_dev->port[i]);
300 		if (error)
301 			err = error;
302 	}
303 
304 	if (net_dev->phydev)
305 		phy_disconnect(net_dev->phydev);
306 	net_dev->phydev = NULL;
307 
308 	msleep(200);
309 
310 	return err;
311 }
312 
313 static void dpaa_tx_timeout(struct net_device *net_dev, unsigned int txqueue)
314 {
315 	struct dpaa_percpu_priv *percpu_priv;
316 	const struct dpaa_priv	*priv;
317 
318 	priv = netdev_priv(net_dev);
319 	percpu_priv = this_cpu_ptr(priv->percpu_priv);
320 
321 	netif_crit(priv, timer, net_dev, "Transmit timeout latency: %u ms\n",
322 		   jiffies_to_msecs(jiffies - dev_trans_start(net_dev)));
323 
324 	percpu_priv->stats.tx_errors++;
325 }
326 
327 /* Calculates the statistics for the given device by adding the statistics
328  * collected by each CPU.
329  */
330 static void dpaa_get_stats64(struct net_device *net_dev,
331 			     struct rtnl_link_stats64 *s)
332 {
333 	int numstats = sizeof(struct rtnl_link_stats64) / sizeof(u64);
334 	struct dpaa_priv *priv = netdev_priv(net_dev);
335 	struct dpaa_percpu_priv *percpu_priv;
336 	u64 *netstats = (u64 *)s;
337 	u64 *cpustats;
338 	int i, j;
339 
340 	for_each_possible_cpu(i) {
341 		percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
342 
343 		cpustats = (u64 *)&percpu_priv->stats;
344 
345 		/* add stats from all CPUs */
346 		for (j = 0; j < numstats; j++)
347 			netstats[j] += cpustats[j];
348 	}
349 }
350 
351 static int dpaa_setup_tc(struct net_device *net_dev, enum tc_setup_type type,
352 			 void *type_data)
353 {
354 	struct dpaa_priv *priv = netdev_priv(net_dev);
355 	struct tc_mqprio_qopt *mqprio = type_data;
356 	u8 num_tc;
357 	int i;
358 
359 	if (type != TC_SETUP_QDISC_MQPRIO)
360 		return -EOPNOTSUPP;
361 
362 	mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
363 	num_tc = mqprio->num_tc;
364 
365 	if (num_tc == priv->num_tc)
366 		return 0;
367 
368 	if (!num_tc) {
369 		netdev_reset_tc(net_dev);
370 		goto out;
371 	}
372 
373 	if (num_tc > DPAA_TC_NUM) {
374 		netdev_err(net_dev, "Too many traffic classes: max %d supported.\n",
375 			   DPAA_TC_NUM);
376 		return -EINVAL;
377 	}
378 
379 	netdev_set_num_tc(net_dev, num_tc);
380 
381 	for (i = 0; i < num_tc; i++)
382 		netdev_set_tc_queue(net_dev, i, DPAA_TC_TXQ_NUM,
383 				    i * DPAA_TC_TXQ_NUM);
384 
385 out:
386 	priv->num_tc = num_tc ? : 1;
387 	netif_set_real_num_tx_queues(net_dev, priv->num_tc * DPAA_TC_TXQ_NUM);
388 	return 0;
389 }
390 
391 static struct mac_device *dpaa_mac_dev_get(struct platform_device *pdev)
392 {
393 	struct dpaa_eth_data *eth_data;
394 	struct device *dpaa_dev;
395 	struct mac_device *mac_dev;
396 
397 	dpaa_dev = &pdev->dev;
398 	eth_data = dpaa_dev->platform_data;
399 	if (!eth_data) {
400 		dev_err(dpaa_dev, "eth_data missing\n");
401 		return ERR_PTR(-ENODEV);
402 	}
403 	mac_dev = eth_data->mac_dev;
404 	if (!mac_dev) {
405 		dev_err(dpaa_dev, "mac_dev missing\n");
406 		return ERR_PTR(-EINVAL);
407 	}
408 
409 	return mac_dev;
410 }
411 
412 static int dpaa_set_mac_address(struct net_device *net_dev, void *addr)
413 {
414 	const struct dpaa_priv *priv;
415 	struct mac_device *mac_dev;
416 	struct sockaddr old_addr;
417 	int err;
418 
419 	priv = netdev_priv(net_dev);
420 
421 	memcpy(old_addr.sa_data, net_dev->dev_addr,  ETH_ALEN);
422 
423 	err = eth_mac_addr(net_dev, addr);
424 	if (err < 0) {
425 		netif_err(priv, drv, net_dev, "eth_mac_addr() = %d\n", err);
426 		return err;
427 	}
428 
429 	mac_dev = priv->mac_dev;
430 
431 	err = mac_dev->change_addr(mac_dev->fman_mac,
432 				   (const enet_addr_t *)net_dev->dev_addr);
433 	if (err < 0) {
434 		netif_err(priv, drv, net_dev, "mac_dev->change_addr() = %d\n",
435 			  err);
436 		/* reverting to previous address */
437 		eth_mac_addr(net_dev, &old_addr);
438 
439 		return err;
440 	}
441 
442 	return 0;
443 }
444 
445 static void dpaa_set_rx_mode(struct net_device *net_dev)
446 {
447 	const struct dpaa_priv	*priv;
448 	int err;
449 
450 	priv = netdev_priv(net_dev);
451 
452 	if (!!(net_dev->flags & IFF_PROMISC) != priv->mac_dev->promisc) {
453 		priv->mac_dev->promisc = !priv->mac_dev->promisc;
454 		err = priv->mac_dev->set_promisc(priv->mac_dev->fman_mac,
455 						 priv->mac_dev->promisc);
456 		if (err < 0)
457 			netif_err(priv, drv, net_dev,
458 				  "mac_dev->set_promisc() = %d\n",
459 				  err);
460 	}
461 
462 	if (!!(net_dev->flags & IFF_ALLMULTI) != priv->mac_dev->allmulti) {
463 		priv->mac_dev->allmulti = !priv->mac_dev->allmulti;
464 		err = priv->mac_dev->set_allmulti(priv->mac_dev->fman_mac,
465 						  priv->mac_dev->allmulti);
466 		if (err < 0)
467 			netif_err(priv, drv, net_dev,
468 				  "mac_dev->set_allmulti() = %d\n",
469 				  err);
470 	}
471 
472 	err = priv->mac_dev->set_multi(net_dev, priv->mac_dev);
473 	if (err < 0)
474 		netif_err(priv, drv, net_dev, "mac_dev->set_multi() = %d\n",
475 			  err);
476 }
477 
478 static struct dpaa_bp *dpaa_bpid2pool(int bpid)
479 {
480 	if (WARN_ON(bpid < 0 || bpid >= BM_MAX_NUM_OF_POOLS))
481 		return NULL;
482 
483 	return dpaa_bp_array[bpid];
484 }
485 
486 /* checks if this bpool is already allocated */
487 static bool dpaa_bpid2pool_use(int bpid)
488 {
489 	if (dpaa_bpid2pool(bpid)) {
490 		refcount_inc(&dpaa_bp_array[bpid]->refs);
491 		return true;
492 	}
493 
494 	return false;
495 }
496 
497 /* called only once per bpid by dpaa_bp_alloc_pool() */
498 static void dpaa_bpid2pool_map(int bpid, struct dpaa_bp *dpaa_bp)
499 {
500 	dpaa_bp_array[bpid] = dpaa_bp;
501 	refcount_set(&dpaa_bp->refs, 1);
502 }
503 
504 static int dpaa_bp_alloc_pool(struct dpaa_bp *dpaa_bp)
505 {
506 	int err;
507 
508 	if (dpaa_bp->size == 0 || dpaa_bp->config_count == 0) {
509 		pr_err("%s: Buffer pool is not properly initialized! Missing size or initial number of buffers\n",
510 		       __func__);
511 		return -EINVAL;
512 	}
513 
514 	/* If the pool is already specified, we only create one per bpid */
515 	if (dpaa_bp->bpid != FSL_DPAA_BPID_INV &&
516 	    dpaa_bpid2pool_use(dpaa_bp->bpid))
517 		return 0;
518 
519 	if (dpaa_bp->bpid == FSL_DPAA_BPID_INV) {
520 		dpaa_bp->pool = bman_new_pool();
521 		if (!dpaa_bp->pool) {
522 			pr_err("%s: bman_new_pool() failed\n",
523 			       __func__);
524 			return -ENODEV;
525 		}
526 
527 		dpaa_bp->bpid = (u8)bman_get_bpid(dpaa_bp->pool);
528 	}
529 
530 	if (dpaa_bp->seed_cb) {
531 		err = dpaa_bp->seed_cb(dpaa_bp);
532 		if (err)
533 			goto pool_seed_failed;
534 	}
535 
536 	dpaa_bpid2pool_map(dpaa_bp->bpid, dpaa_bp);
537 
538 	return 0;
539 
540 pool_seed_failed:
541 	pr_err("%s: pool seeding failed\n", __func__);
542 	bman_free_pool(dpaa_bp->pool);
543 
544 	return err;
545 }
546 
547 /* remove and free all the buffers from the given buffer pool */
548 static void dpaa_bp_drain(struct dpaa_bp *bp)
549 {
550 	u8 num = 8;
551 	int ret;
552 
553 	do {
554 		struct bm_buffer bmb[8];
555 		int i;
556 
557 		ret = bman_acquire(bp->pool, bmb, num);
558 		if (ret < 0) {
559 			if (num == 8) {
560 				/* we have less than 8 buffers left;
561 				 * drain them one by one
562 				 */
563 				num = 1;
564 				ret = 1;
565 				continue;
566 			} else {
567 				/* Pool is fully drained */
568 				break;
569 			}
570 		}
571 
572 		if (bp->free_buf_cb)
573 			for (i = 0; i < num; i++)
574 				bp->free_buf_cb(bp, &bmb[i]);
575 	} while (ret > 0);
576 }
577 
578 static void dpaa_bp_free(struct dpaa_bp *dpaa_bp)
579 {
580 	struct dpaa_bp *bp = dpaa_bpid2pool(dpaa_bp->bpid);
581 
582 	/* the mapping between bpid and dpaa_bp is done very late in the
583 	 * allocation procedure; if something failed before the mapping, the bp
584 	 * was not configured, therefore we don't need the below instructions
585 	 */
586 	if (!bp)
587 		return;
588 
589 	if (!refcount_dec_and_test(&bp->refs))
590 		return;
591 
592 	if (bp->free_buf_cb)
593 		dpaa_bp_drain(bp);
594 
595 	dpaa_bp_array[bp->bpid] = NULL;
596 	bman_free_pool(bp->pool);
597 }
598 
599 static void dpaa_bps_free(struct dpaa_priv *priv)
600 {
601 	dpaa_bp_free(priv->dpaa_bp);
602 }
603 
604 /* Use multiple WQs for FQ assignment:
605  *	- Tx Confirmation queues go to WQ1.
606  *	- Rx Error and Tx Error queues go to WQ5 (giving them a better chance
607  *	  to be scheduled, in case there are many more FQs in WQ6).
608  *	- Rx Default goes to WQ6.
609  *	- Tx queues go to different WQs depending on their priority. Equal
610  *	  chunks of NR_CPUS queues go to WQ6 (lowest priority), WQ2, WQ1 and
611  *	  WQ0 (highest priority).
612  * This ensures that Tx-confirmed buffers are timely released. In particular,
613  * it avoids congestion on the Tx Confirm FQs, which can pile up PFDRs if they
614  * are greatly outnumbered by other FQs in the system, while
615  * dequeue scheduling is round-robin.
616  */
617 static inline void dpaa_assign_wq(struct dpaa_fq *fq, int idx)
618 {
619 	switch (fq->fq_type) {
620 	case FQ_TYPE_TX_CONFIRM:
621 	case FQ_TYPE_TX_CONF_MQ:
622 		fq->wq = 1;
623 		break;
624 	case FQ_TYPE_RX_ERROR:
625 	case FQ_TYPE_TX_ERROR:
626 		fq->wq = 5;
627 		break;
628 	case FQ_TYPE_RX_DEFAULT:
629 	case FQ_TYPE_RX_PCD:
630 		fq->wq = 6;
631 		break;
632 	case FQ_TYPE_TX:
633 		switch (idx / DPAA_TC_TXQ_NUM) {
634 		case 0:
635 			/* Low priority (best effort) */
636 			fq->wq = 6;
637 			break;
638 		case 1:
639 			/* Medium priority */
640 			fq->wq = 2;
641 			break;
642 		case 2:
643 			/* High priority */
644 			fq->wq = 1;
645 			break;
646 		case 3:
647 			/* Very high priority */
648 			fq->wq = 0;
649 			break;
650 		default:
651 			WARN(1, "Too many TX FQs: more than %d!\n",
652 			     DPAA_ETH_TXQ_NUM);
653 		}
654 		break;
655 	default:
656 		WARN(1, "Invalid FQ type %d for FQID %d!\n",
657 		     fq->fq_type, fq->fqid);
658 	}
659 }
660 
661 static struct dpaa_fq *dpaa_fq_alloc(struct device *dev,
662 				     u32 start, u32 count,
663 				     struct list_head *list,
664 				     enum dpaa_fq_type fq_type)
665 {
666 	struct dpaa_fq *dpaa_fq;
667 	int i;
668 
669 	dpaa_fq = devm_kcalloc(dev, count, sizeof(*dpaa_fq),
670 			       GFP_KERNEL);
671 	if (!dpaa_fq)
672 		return NULL;
673 
674 	for (i = 0; i < count; i++) {
675 		dpaa_fq[i].fq_type = fq_type;
676 		dpaa_fq[i].fqid = start ? start + i : 0;
677 		list_add_tail(&dpaa_fq[i].list, list);
678 	}
679 
680 	for (i = 0; i < count; i++)
681 		dpaa_assign_wq(dpaa_fq + i, i);
682 
683 	return dpaa_fq;
684 }
685 
686 static int dpaa_alloc_all_fqs(struct device *dev, struct list_head *list,
687 			      struct fm_port_fqs *port_fqs)
688 {
689 	struct dpaa_fq *dpaa_fq;
690 	u32 fq_base, fq_base_aligned, i;
691 
692 	dpaa_fq = dpaa_fq_alloc(dev, 0, 1, list, FQ_TYPE_RX_ERROR);
693 	if (!dpaa_fq)
694 		goto fq_alloc_failed;
695 
696 	port_fqs->rx_errq = &dpaa_fq[0];
697 
698 	dpaa_fq = dpaa_fq_alloc(dev, 0, 1, list, FQ_TYPE_RX_DEFAULT);
699 	if (!dpaa_fq)
700 		goto fq_alloc_failed;
701 
702 	port_fqs->rx_defq = &dpaa_fq[0];
703 
704 	/* the PCD FQIDs range needs to be aligned for correct operation */
705 	if (qman_alloc_fqid_range(&fq_base, 2 * DPAA_ETH_PCD_RXQ_NUM))
706 		goto fq_alloc_failed;
707 
708 	fq_base_aligned = ALIGN(fq_base, DPAA_ETH_PCD_RXQ_NUM);
709 
710 	for (i = fq_base; i < fq_base_aligned; i++)
711 		qman_release_fqid(i);
712 
713 	for (i = fq_base_aligned + DPAA_ETH_PCD_RXQ_NUM;
714 	     i < (fq_base + 2 * DPAA_ETH_PCD_RXQ_NUM); i++)
715 		qman_release_fqid(i);
716 
717 	dpaa_fq = dpaa_fq_alloc(dev, fq_base_aligned, DPAA_ETH_PCD_RXQ_NUM,
718 				list, FQ_TYPE_RX_PCD);
719 	if (!dpaa_fq)
720 		goto fq_alloc_failed;
721 
722 	port_fqs->rx_pcdq = &dpaa_fq[0];
723 
724 	if (!dpaa_fq_alloc(dev, 0, DPAA_ETH_TXQ_NUM, list, FQ_TYPE_TX_CONF_MQ))
725 		goto fq_alloc_failed;
726 
727 	dpaa_fq = dpaa_fq_alloc(dev, 0, 1, list, FQ_TYPE_TX_ERROR);
728 	if (!dpaa_fq)
729 		goto fq_alloc_failed;
730 
731 	port_fqs->tx_errq = &dpaa_fq[0];
732 
733 	dpaa_fq = dpaa_fq_alloc(dev, 0, 1, list, FQ_TYPE_TX_CONFIRM);
734 	if (!dpaa_fq)
735 		goto fq_alloc_failed;
736 
737 	port_fqs->tx_defq = &dpaa_fq[0];
738 
739 	if (!dpaa_fq_alloc(dev, 0, DPAA_ETH_TXQ_NUM, list, FQ_TYPE_TX))
740 		goto fq_alloc_failed;
741 
742 	return 0;
743 
744 fq_alloc_failed:
745 	dev_err(dev, "dpaa_fq_alloc() failed\n");
746 	return -ENOMEM;
747 }
748 
749 static u32 rx_pool_channel;
750 static DEFINE_SPINLOCK(rx_pool_channel_init);
751 
752 static int dpaa_get_channel(void)
753 {
754 	spin_lock(&rx_pool_channel_init);
755 	if (!rx_pool_channel) {
756 		u32 pool;
757 		int ret;
758 
759 		ret = qman_alloc_pool(&pool);
760 
761 		if (!ret)
762 			rx_pool_channel = pool;
763 	}
764 	spin_unlock(&rx_pool_channel_init);
765 	if (!rx_pool_channel)
766 		return -ENOMEM;
767 	return rx_pool_channel;
768 }
769 
770 static void dpaa_release_channel(void)
771 {
772 	qman_release_pool(rx_pool_channel);
773 }
774 
775 static void dpaa_eth_add_channel(u16 channel, struct device *dev)
776 {
777 	u32 pool = QM_SDQCR_CHANNELS_POOL_CONV(channel);
778 	const cpumask_t *cpus = qman_affine_cpus();
779 	struct qman_portal *portal;
780 	int cpu;
781 
782 	for_each_cpu_and(cpu, cpus, cpu_online_mask) {
783 		portal = qman_get_affine_portal(cpu);
784 		qman_p_static_dequeue_add(portal, pool);
785 		qman_start_using_portal(portal, dev);
786 	}
787 }
788 
789 /* Congestion group state change notification callback.
790  * Stops the device's egress queues while they are congested and
791  * wakes them upon exiting congested state.
792  * Also updates some CGR-related stats.
793  */
794 static void dpaa_eth_cgscn(struct qman_portal *qm, struct qman_cgr *cgr,
795 			   int congested)
796 {
797 	struct dpaa_priv *priv = (struct dpaa_priv *)container_of(cgr,
798 		struct dpaa_priv, cgr_data.cgr);
799 
800 	if (congested) {
801 		priv->cgr_data.congestion_start_jiffies = jiffies;
802 		netif_tx_stop_all_queues(priv->net_dev);
803 		priv->cgr_data.cgr_congested_count++;
804 	} else {
805 		priv->cgr_data.congested_jiffies +=
806 			(jiffies - priv->cgr_data.congestion_start_jiffies);
807 		netif_tx_wake_all_queues(priv->net_dev);
808 	}
809 }
810 
811 static int dpaa_eth_cgr_init(struct dpaa_priv *priv)
812 {
813 	struct qm_mcc_initcgr initcgr;
814 	u32 cs_th;
815 	int err;
816 
817 	err = qman_alloc_cgrid(&priv->cgr_data.cgr.cgrid);
818 	if (err < 0) {
819 		if (netif_msg_drv(priv))
820 			pr_err("%s: Error %d allocating CGR ID\n",
821 			       __func__, err);
822 		goto out_error;
823 	}
824 	priv->cgr_data.cgr.cb = dpaa_eth_cgscn;
825 
826 	/* Enable Congestion State Change Notifications and CS taildrop */
827 	memset(&initcgr, 0, sizeof(initcgr));
828 	initcgr.we_mask = cpu_to_be16(QM_CGR_WE_CSCN_EN | QM_CGR_WE_CS_THRES);
829 	initcgr.cgr.cscn_en = QM_CGR_EN;
830 
831 	/* Set different thresholds based on the MAC speed.
832 	 * This may turn suboptimal if the MAC is reconfigured at a speed
833 	 * lower than its max, e.g. if a dTSEC later negotiates a 100Mbps link.
834 	 * In such cases, we ought to reconfigure the threshold, too.
835 	 */
836 	if (priv->mac_dev->if_support & SUPPORTED_10000baseT_Full)
837 		cs_th = DPAA_CS_THRESHOLD_10G;
838 	else
839 		cs_th = DPAA_CS_THRESHOLD_1G;
840 	qm_cgr_cs_thres_set64(&initcgr.cgr.cs_thres, cs_th, 1);
841 
842 	initcgr.we_mask |= cpu_to_be16(QM_CGR_WE_CSTD_EN);
843 	initcgr.cgr.cstd_en = QM_CGR_EN;
844 
845 	err = qman_create_cgr(&priv->cgr_data.cgr, QMAN_CGR_FLAG_USE_INIT,
846 			      &initcgr);
847 	if (err < 0) {
848 		if (netif_msg_drv(priv))
849 			pr_err("%s: Error %d creating CGR with ID %d\n",
850 			       __func__, err, priv->cgr_data.cgr.cgrid);
851 		qman_release_cgrid(priv->cgr_data.cgr.cgrid);
852 		goto out_error;
853 	}
854 	if (netif_msg_drv(priv))
855 		pr_debug("Created CGR %d for netdev with hwaddr %pM on QMan channel %d\n",
856 			 priv->cgr_data.cgr.cgrid, priv->mac_dev->addr,
857 			 priv->cgr_data.cgr.chan);
858 
859 out_error:
860 	return err;
861 }
862 
863 static inline void dpaa_setup_ingress(const struct dpaa_priv *priv,
864 				      struct dpaa_fq *fq,
865 				      const struct qman_fq *template)
866 {
867 	fq->fq_base = *template;
868 	fq->net_dev = priv->net_dev;
869 
870 	fq->flags = QMAN_FQ_FLAG_NO_ENQUEUE;
871 	fq->channel = priv->channel;
872 }
873 
874 static inline void dpaa_setup_egress(const struct dpaa_priv *priv,
875 				     struct dpaa_fq *fq,
876 				     struct fman_port *port,
877 				     const struct qman_fq *template)
878 {
879 	fq->fq_base = *template;
880 	fq->net_dev = priv->net_dev;
881 
882 	if (port) {
883 		fq->flags = QMAN_FQ_FLAG_TO_DCPORTAL;
884 		fq->channel = (u16)fman_port_get_qman_channel_id(port);
885 	} else {
886 		fq->flags = QMAN_FQ_FLAG_NO_MODIFY;
887 	}
888 }
889 
890 static void dpaa_fq_setup(struct dpaa_priv *priv,
891 			  const struct dpaa_fq_cbs *fq_cbs,
892 			  struct fman_port *tx_port)
893 {
894 	int egress_cnt = 0, conf_cnt = 0, num_portals = 0, portal_cnt = 0, cpu;
895 	const cpumask_t *affine_cpus = qman_affine_cpus();
896 	u16 channels[NR_CPUS];
897 	struct dpaa_fq *fq;
898 
899 	for_each_cpu_and(cpu, affine_cpus, cpu_online_mask)
900 		channels[num_portals++] = qman_affine_channel(cpu);
901 
902 	if (num_portals == 0)
903 		dev_err(priv->net_dev->dev.parent,
904 			"No Qman software (affine) channels found\n");
905 
906 	/* Initialize each FQ in the list */
907 	list_for_each_entry(fq, &priv->dpaa_fq_list, list) {
908 		switch (fq->fq_type) {
909 		case FQ_TYPE_RX_DEFAULT:
910 			dpaa_setup_ingress(priv, fq, &fq_cbs->rx_defq);
911 			break;
912 		case FQ_TYPE_RX_ERROR:
913 			dpaa_setup_ingress(priv, fq, &fq_cbs->rx_errq);
914 			break;
915 		case FQ_TYPE_RX_PCD:
916 			if (!num_portals)
917 				continue;
918 			dpaa_setup_ingress(priv, fq, &fq_cbs->rx_defq);
919 			fq->channel = channels[portal_cnt++ % num_portals];
920 			break;
921 		case FQ_TYPE_TX:
922 			dpaa_setup_egress(priv, fq, tx_port,
923 					  &fq_cbs->egress_ern);
924 			/* If we have more Tx queues than the number of cores,
925 			 * just ignore the extra ones.
926 			 */
927 			if (egress_cnt < DPAA_ETH_TXQ_NUM)
928 				priv->egress_fqs[egress_cnt++] = &fq->fq_base;
929 			break;
930 		case FQ_TYPE_TX_CONF_MQ:
931 			priv->conf_fqs[conf_cnt++] = &fq->fq_base;
932 			fallthrough;
933 		case FQ_TYPE_TX_CONFIRM:
934 			dpaa_setup_ingress(priv, fq, &fq_cbs->tx_defq);
935 			break;
936 		case FQ_TYPE_TX_ERROR:
937 			dpaa_setup_ingress(priv, fq, &fq_cbs->tx_errq);
938 			break;
939 		default:
940 			dev_warn(priv->net_dev->dev.parent,
941 				 "Unknown FQ type detected!\n");
942 			break;
943 		}
944 	}
945 
946 	 /* Make sure all CPUs receive a corresponding Tx queue. */
947 	while (egress_cnt < DPAA_ETH_TXQ_NUM) {
948 		list_for_each_entry(fq, &priv->dpaa_fq_list, list) {
949 			if (fq->fq_type != FQ_TYPE_TX)
950 				continue;
951 			priv->egress_fqs[egress_cnt++] = &fq->fq_base;
952 			if (egress_cnt == DPAA_ETH_TXQ_NUM)
953 				break;
954 		}
955 	}
956 }
957 
958 static inline int dpaa_tx_fq_to_id(const struct dpaa_priv *priv,
959 				   struct qman_fq *tx_fq)
960 {
961 	int i;
962 
963 	for (i = 0; i < DPAA_ETH_TXQ_NUM; i++)
964 		if (priv->egress_fqs[i] == tx_fq)
965 			return i;
966 
967 	return -EINVAL;
968 }
969 
970 static int dpaa_fq_init(struct dpaa_fq *dpaa_fq, bool td_enable)
971 {
972 	const struct dpaa_priv	*priv;
973 	struct qman_fq *confq = NULL;
974 	struct qm_mcc_initfq initfq;
975 	struct device *dev;
976 	struct qman_fq *fq;
977 	int queue_id;
978 	int err;
979 
980 	priv = netdev_priv(dpaa_fq->net_dev);
981 	dev = dpaa_fq->net_dev->dev.parent;
982 
983 	if (dpaa_fq->fqid == 0)
984 		dpaa_fq->flags |= QMAN_FQ_FLAG_DYNAMIC_FQID;
985 
986 	dpaa_fq->init = !(dpaa_fq->flags & QMAN_FQ_FLAG_NO_MODIFY);
987 
988 	err = qman_create_fq(dpaa_fq->fqid, dpaa_fq->flags, &dpaa_fq->fq_base);
989 	if (err) {
990 		dev_err(dev, "qman_create_fq() failed\n");
991 		return err;
992 	}
993 	fq = &dpaa_fq->fq_base;
994 
995 	if (dpaa_fq->init) {
996 		memset(&initfq, 0, sizeof(initfq));
997 
998 		initfq.we_mask = cpu_to_be16(QM_INITFQ_WE_FQCTRL);
999 		/* Note: we may get to keep an empty FQ in cache */
1000 		initfq.fqd.fq_ctrl = cpu_to_be16(QM_FQCTRL_PREFERINCACHE);
1001 
1002 		/* Try to reduce the number of portal interrupts for
1003 		 * Tx Confirmation FQs.
1004 		 */
1005 		if (dpaa_fq->fq_type == FQ_TYPE_TX_CONFIRM)
1006 			initfq.fqd.fq_ctrl |= cpu_to_be16(QM_FQCTRL_AVOIDBLOCK);
1007 
1008 		/* FQ placement */
1009 		initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_DESTWQ);
1010 
1011 		qm_fqd_set_destwq(&initfq.fqd, dpaa_fq->channel, dpaa_fq->wq);
1012 
1013 		/* Put all egress queues in a congestion group of their own.
1014 		 * Sensu stricto, the Tx confirmation queues are Rx FQs,
1015 		 * rather than Tx - but they nonetheless account for the
1016 		 * memory footprint on behalf of egress traffic. We therefore
1017 		 * place them in the netdev's CGR, along with the Tx FQs.
1018 		 */
1019 		if (dpaa_fq->fq_type == FQ_TYPE_TX ||
1020 		    dpaa_fq->fq_type == FQ_TYPE_TX_CONFIRM ||
1021 		    dpaa_fq->fq_type == FQ_TYPE_TX_CONF_MQ) {
1022 			initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_CGID);
1023 			initfq.fqd.fq_ctrl |= cpu_to_be16(QM_FQCTRL_CGE);
1024 			initfq.fqd.cgid = (u8)priv->cgr_data.cgr.cgrid;
1025 			/* Set a fixed overhead accounting, in an attempt to
1026 			 * reduce the impact of fixed-size skb shells and the
1027 			 * driver's needed headroom on system memory. This is
1028 			 * especially the case when the egress traffic is
1029 			 * composed of small datagrams.
1030 			 * Unfortunately, QMan's OAL value is capped to an
1031 			 * insufficient value, but even that is better than
1032 			 * no overhead accounting at all.
1033 			 */
1034 			initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_OAC);
1035 			qm_fqd_set_oac(&initfq.fqd, QM_OAC_CG);
1036 			qm_fqd_set_oal(&initfq.fqd,
1037 				       min(sizeof(struct sk_buff) +
1038 				       priv->tx_headroom,
1039 				       (size_t)FSL_QMAN_MAX_OAL));
1040 		}
1041 
1042 		if (td_enable) {
1043 			initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_TDTHRESH);
1044 			qm_fqd_set_taildrop(&initfq.fqd, DPAA_FQ_TD, 1);
1045 			initfq.fqd.fq_ctrl = cpu_to_be16(QM_FQCTRL_TDE);
1046 		}
1047 
1048 		if (dpaa_fq->fq_type == FQ_TYPE_TX) {
1049 			queue_id = dpaa_tx_fq_to_id(priv, &dpaa_fq->fq_base);
1050 			if (queue_id >= 0)
1051 				confq = priv->conf_fqs[queue_id];
1052 			if (confq) {
1053 				initfq.we_mask |=
1054 					cpu_to_be16(QM_INITFQ_WE_CONTEXTA);
1055 			/* ContextA: OVOM=1(use contextA2 bits instead of ICAD)
1056 			 *	     A2V=1 (contextA A2 field is valid)
1057 			 *	     A0V=1 (contextA A0 field is valid)
1058 			 *	     B0V=1 (contextB field is valid)
1059 			 * ContextA A2: EBD=1 (deallocate buffers inside FMan)
1060 			 * ContextB B0(ASPID): 0 (absolute Virtual Storage ID)
1061 			 */
1062 				qm_fqd_context_a_set64(&initfq.fqd,
1063 						       0x1e00000080000000ULL);
1064 			}
1065 		}
1066 
1067 		/* Put all the ingress queues in our "ingress CGR". */
1068 		if (priv->use_ingress_cgr &&
1069 		    (dpaa_fq->fq_type == FQ_TYPE_RX_DEFAULT ||
1070 		     dpaa_fq->fq_type == FQ_TYPE_RX_ERROR ||
1071 		     dpaa_fq->fq_type == FQ_TYPE_RX_PCD)) {
1072 			initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_CGID);
1073 			initfq.fqd.fq_ctrl |= cpu_to_be16(QM_FQCTRL_CGE);
1074 			initfq.fqd.cgid = (u8)priv->ingress_cgr.cgrid;
1075 			/* Set a fixed overhead accounting, just like for the
1076 			 * egress CGR.
1077 			 */
1078 			initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_OAC);
1079 			qm_fqd_set_oac(&initfq.fqd, QM_OAC_CG);
1080 			qm_fqd_set_oal(&initfq.fqd,
1081 				       min(sizeof(struct sk_buff) +
1082 				       priv->tx_headroom,
1083 				       (size_t)FSL_QMAN_MAX_OAL));
1084 		}
1085 
1086 		/* Initialization common to all ingress queues */
1087 		if (dpaa_fq->flags & QMAN_FQ_FLAG_NO_ENQUEUE) {
1088 			initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_CONTEXTA);
1089 			initfq.fqd.fq_ctrl |= cpu_to_be16(QM_FQCTRL_HOLDACTIVE |
1090 						QM_FQCTRL_CTXASTASHING);
1091 			initfq.fqd.context_a.stashing.exclusive =
1092 				QM_STASHING_EXCL_DATA | QM_STASHING_EXCL_CTX |
1093 				QM_STASHING_EXCL_ANNOTATION;
1094 			qm_fqd_set_stashing(&initfq.fqd, 1, 2,
1095 					    DIV_ROUND_UP(sizeof(struct qman_fq),
1096 							 64));
1097 		}
1098 
1099 		err = qman_init_fq(fq, QMAN_INITFQ_FLAG_SCHED, &initfq);
1100 		if (err < 0) {
1101 			dev_err(dev, "qman_init_fq(%u) = %d\n",
1102 				qman_fq_fqid(fq), err);
1103 			qman_destroy_fq(fq);
1104 			return err;
1105 		}
1106 	}
1107 
1108 	dpaa_fq->fqid = qman_fq_fqid(fq);
1109 
1110 	if (dpaa_fq->fq_type == FQ_TYPE_RX_DEFAULT ||
1111 	    dpaa_fq->fq_type == FQ_TYPE_RX_PCD) {
1112 		err = xdp_rxq_info_reg(&dpaa_fq->xdp_rxq, dpaa_fq->net_dev,
1113 				       dpaa_fq->fqid, 0);
1114 		if (err) {
1115 			dev_err(dev, "xdp_rxq_info_reg() = %d\n", err);
1116 			return err;
1117 		}
1118 
1119 		err = xdp_rxq_info_reg_mem_model(&dpaa_fq->xdp_rxq,
1120 						 MEM_TYPE_PAGE_ORDER0, NULL);
1121 		if (err) {
1122 			dev_err(dev, "xdp_rxq_info_reg_mem_model() = %d\n",
1123 				err);
1124 			xdp_rxq_info_unreg(&dpaa_fq->xdp_rxq);
1125 			return err;
1126 		}
1127 	}
1128 
1129 	return 0;
1130 }
1131 
1132 static int dpaa_fq_free_entry(struct device *dev, struct qman_fq *fq)
1133 {
1134 	const struct dpaa_priv  *priv;
1135 	struct dpaa_fq *dpaa_fq;
1136 	int err, error;
1137 
1138 	err = 0;
1139 
1140 	dpaa_fq = container_of(fq, struct dpaa_fq, fq_base);
1141 	priv = netdev_priv(dpaa_fq->net_dev);
1142 
1143 	if (dpaa_fq->init) {
1144 		err = qman_retire_fq(fq, NULL);
1145 		if (err < 0 && netif_msg_drv(priv))
1146 			dev_err(dev, "qman_retire_fq(%u) = %d\n",
1147 				qman_fq_fqid(fq), err);
1148 
1149 		error = qman_oos_fq(fq);
1150 		if (error < 0 && netif_msg_drv(priv)) {
1151 			dev_err(dev, "qman_oos_fq(%u) = %d\n",
1152 				qman_fq_fqid(fq), error);
1153 			if (err >= 0)
1154 				err = error;
1155 		}
1156 	}
1157 
1158 	if ((dpaa_fq->fq_type == FQ_TYPE_RX_DEFAULT ||
1159 	     dpaa_fq->fq_type == FQ_TYPE_RX_PCD) &&
1160 	    xdp_rxq_info_is_reg(&dpaa_fq->xdp_rxq))
1161 		xdp_rxq_info_unreg(&dpaa_fq->xdp_rxq);
1162 
1163 	qman_destroy_fq(fq);
1164 	list_del(&dpaa_fq->list);
1165 
1166 	return err;
1167 }
1168 
1169 static int dpaa_fq_free(struct device *dev, struct list_head *list)
1170 {
1171 	struct dpaa_fq *dpaa_fq, *tmp;
1172 	int err, error;
1173 
1174 	err = 0;
1175 	list_for_each_entry_safe(dpaa_fq, tmp, list, list) {
1176 		error = dpaa_fq_free_entry(dev, (struct qman_fq *)dpaa_fq);
1177 		if (error < 0 && err >= 0)
1178 			err = error;
1179 	}
1180 
1181 	return err;
1182 }
1183 
1184 static int dpaa_eth_init_tx_port(struct fman_port *port, struct dpaa_fq *errq,
1185 				 struct dpaa_fq *defq,
1186 				 struct dpaa_buffer_layout *buf_layout)
1187 {
1188 	struct fman_buffer_prefix_content buf_prefix_content;
1189 	struct fman_port_params params;
1190 	int err;
1191 
1192 	memset(&params, 0, sizeof(params));
1193 	memset(&buf_prefix_content, 0, sizeof(buf_prefix_content));
1194 
1195 	buf_prefix_content.priv_data_size = buf_layout->priv_data_size;
1196 	buf_prefix_content.pass_prs_result = true;
1197 	buf_prefix_content.pass_hash_result = true;
1198 	buf_prefix_content.pass_time_stamp = true;
1199 	buf_prefix_content.data_align = DPAA_FD_DATA_ALIGNMENT;
1200 
1201 	params.specific_params.non_rx_params.err_fqid = errq->fqid;
1202 	params.specific_params.non_rx_params.dflt_fqid = defq->fqid;
1203 
1204 	err = fman_port_config(port, &params);
1205 	if (err) {
1206 		pr_err("%s: fman_port_config failed\n", __func__);
1207 		return err;
1208 	}
1209 
1210 	err = fman_port_cfg_buf_prefix_content(port, &buf_prefix_content);
1211 	if (err) {
1212 		pr_err("%s: fman_port_cfg_buf_prefix_content failed\n",
1213 		       __func__);
1214 		return err;
1215 	}
1216 
1217 	err = fman_port_init(port);
1218 	if (err)
1219 		pr_err("%s: fm_port_init failed\n", __func__);
1220 
1221 	return err;
1222 }
1223 
1224 static int dpaa_eth_init_rx_port(struct fman_port *port, struct dpaa_bp *bp,
1225 				 struct dpaa_fq *errq,
1226 				 struct dpaa_fq *defq, struct dpaa_fq *pcdq,
1227 				 struct dpaa_buffer_layout *buf_layout)
1228 {
1229 	struct fman_buffer_prefix_content buf_prefix_content;
1230 	struct fman_port_rx_params *rx_p;
1231 	struct fman_port_params params;
1232 	int err;
1233 
1234 	memset(&params, 0, sizeof(params));
1235 	memset(&buf_prefix_content, 0, sizeof(buf_prefix_content));
1236 
1237 	buf_prefix_content.priv_data_size = buf_layout->priv_data_size;
1238 	buf_prefix_content.pass_prs_result = true;
1239 	buf_prefix_content.pass_hash_result = true;
1240 	buf_prefix_content.pass_time_stamp = true;
1241 	buf_prefix_content.data_align = DPAA_FD_RX_DATA_ALIGNMENT;
1242 
1243 	rx_p = &params.specific_params.rx_params;
1244 	rx_p->err_fqid = errq->fqid;
1245 	rx_p->dflt_fqid = defq->fqid;
1246 	if (pcdq) {
1247 		rx_p->pcd_base_fqid = pcdq->fqid;
1248 		rx_p->pcd_fqs_count = DPAA_ETH_PCD_RXQ_NUM;
1249 	}
1250 
1251 	rx_p->ext_buf_pools.num_of_pools_used = 1;
1252 	rx_p->ext_buf_pools.ext_buf_pool[0].id =  bp->bpid;
1253 	rx_p->ext_buf_pools.ext_buf_pool[0].size = (u16)bp->size;
1254 
1255 	err = fman_port_config(port, &params);
1256 	if (err) {
1257 		pr_err("%s: fman_port_config failed\n", __func__);
1258 		return err;
1259 	}
1260 
1261 	err = fman_port_cfg_buf_prefix_content(port, &buf_prefix_content);
1262 	if (err) {
1263 		pr_err("%s: fman_port_cfg_buf_prefix_content failed\n",
1264 		       __func__);
1265 		return err;
1266 	}
1267 
1268 	err = fman_port_init(port);
1269 	if (err)
1270 		pr_err("%s: fm_port_init failed\n", __func__);
1271 
1272 	return err;
1273 }
1274 
1275 static int dpaa_eth_init_ports(struct mac_device *mac_dev,
1276 			       struct dpaa_bp *bp,
1277 			       struct fm_port_fqs *port_fqs,
1278 			       struct dpaa_buffer_layout *buf_layout,
1279 			       struct device *dev)
1280 {
1281 	struct fman_port *rxport = mac_dev->port[RX];
1282 	struct fman_port *txport = mac_dev->port[TX];
1283 	int err;
1284 
1285 	err = dpaa_eth_init_tx_port(txport, port_fqs->tx_errq,
1286 				    port_fqs->tx_defq, &buf_layout[TX]);
1287 	if (err)
1288 		return err;
1289 
1290 	err = dpaa_eth_init_rx_port(rxport, bp, port_fqs->rx_errq,
1291 				    port_fqs->rx_defq, port_fqs->rx_pcdq,
1292 				    &buf_layout[RX]);
1293 
1294 	return err;
1295 }
1296 
1297 static int dpaa_bman_release(const struct dpaa_bp *dpaa_bp,
1298 			     struct bm_buffer *bmb, int cnt)
1299 {
1300 	int err;
1301 
1302 	err = bman_release(dpaa_bp->pool, bmb, cnt);
1303 	/* Should never occur, address anyway to avoid leaking the buffers */
1304 	if (WARN_ON(err) && dpaa_bp->free_buf_cb)
1305 		while (cnt-- > 0)
1306 			dpaa_bp->free_buf_cb(dpaa_bp, &bmb[cnt]);
1307 
1308 	return cnt;
1309 }
1310 
1311 static void dpaa_release_sgt_members(struct qm_sg_entry *sgt)
1312 {
1313 	struct bm_buffer bmb[DPAA_BUFF_RELEASE_MAX];
1314 	struct dpaa_bp *dpaa_bp;
1315 	int i = 0, j;
1316 
1317 	memset(bmb, 0, sizeof(bmb));
1318 
1319 	do {
1320 		dpaa_bp = dpaa_bpid2pool(sgt[i].bpid);
1321 		if (!dpaa_bp)
1322 			return;
1323 
1324 		j = 0;
1325 		do {
1326 			WARN_ON(qm_sg_entry_is_ext(&sgt[i]));
1327 
1328 			bm_buffer_set64(&bmb[j], qm_sg_entry_get64(&sgt[i]));
1329 
1330 			j++; i++;
1331 		} while (j < ARRAY_SIZE(bmb) &&
1332 				!qm_sg_entry_is_final(&sgt[i - 1]) &&
1333 				sgt[i - 1].bpid == sgt[i].bpid);
1334 
1335 		dpaa_bman_release(dpaa_bp, bmb, j);
1336 	} while (!qm_sg_entry_is_final(&sgt[i - 1]));
1337 }
1338 
1339 static void dpaa_fd_release(const struct net_device *net_dev,
1340 			    const struct qm_fd *fd)
1341 {
1342 	struct qm_sg_entry *sgt;
1343 	struct dpaa_bp *dpaa_bp;
1344 	struct bm_buffer bmb;
1345 	dma_addr_t addr;
1346 	void *vaddr;
1347 
1348 	bmb.data = 0;
1349 	bm_buffer_set64(&bmb, qm_fd_addr(fd));
1350 
1351 	dpaa_bp = dpaa_bpid2pool(fd->bpid);
1352 	if (!dpaa_bp)
1353 		return;
1354 
1355 	if (qm_fd_get_format(fd) == qm_fd_sg) {
1356 		vaddr = phys_to_virt(qm_fd_addr(fd));
1357 		sgt = vaddr + qm_fd_get_offset(fd);
1358 
1359 		dma_unmap_page(dpaa_bp->priv->rx_dma_dev, qm_fd_addr(fd),
1360 			       DPAA_BP_RAW_SIZE, DMA_FROM_DEVICE);
1361 
1362 		dpaa_release_sgt_members(sgt);
1363 
1364 		addr = dma_map_page(dpaa_bp->priv->rx_dma_dev,
1365 				    virt_to_page(vaddr), 0, DPAA_BP_RAW_SIZE,
1366 				    DMA_FROM_DEVICE);
1367 		if (dma_mapping_error(dpaa_bp->priv->rx_dma_dev, addr)) {
1368 			netdev_err(net_dev, "DMA mapping failed\n");
1369 			return;
1370 		}
1371 		bm_buffer_set64(&bmb, addr);
1372 	}
1373 
1374 	dpaa_bman_release(dpaa_bp, &bmb, 1);
1375 }
1376 
1377 static void count_ern(struct dpaa_percpu_priv *percpu_priv,
1378 		      const union qm_mr_entry *msg)
1379 {
1380 	switch (msg->ern.rc & QM_MR_RC_MASK) {
1381 	case QM_MR_RC_CGR_TAILDROP:
1382 		percpu_priv->ern_cnt.cg_tdrop++;
1383 		break;
1384 	case QM_MR_RC_WRED:
1385 		percpu_priv->ern_cnt.wred++;
1386 		break;
1387 	case QM_MR_RC_ERROR:
1388 		percpu_priv->ern_cnt.err_cond++;
1389 		break;
1390 	case QM_MR_RC_ORPWINDOW_EARLY:
1391 		percpu_priv->ern_cnt.early_window++;
1392 		break;
1393 	case QM_MR_RC_ORPWINDOW_LATE:
1394 		percpu_priv->ern_cnt.late_window++;
1395 		break;
1396 	case QM_MR_RC_FQ_TAILDROP:
1397 		percpu_priv->ern_cnt.fq_tdrop++;
1398 		break;
1399 	case QM_MR_RC_ORPWINDOW_RETIRED:
1400 		percpu_priv->ern_cnt.fq_retired++;
1401 		break;
1402 	case QM_MR_RC_ORP_ZERO:
1403 		percpu_priv->ern_cnt.orp_zero++;
1404 		break;
1405 	}
1406 }
1407 
1408 /* Turn on HW checksum computation for this outgoing frame.
1409  * If the current protocol is not something we support in this regard
1410  * (or if the stack has already computed the SW checksum), we do nothing.
1411  *
1412  * Returns 0 if all goes well (or HW csum doesn't apply), and a negative value
1413  * otherwise.
1414  *
1415  * Note that this function may modify the fd->cmd field and the skb data buffer
1416  * (the Parse Results area).
1417  */
1418 static int dpaa_enable_tx_csum(struct dpaa_priv *priv,
1419 			       struct sk_buff *skb,
1420 			       struct qm_fd *fd,
1421 			       void *parse_results)
1422 {
1423 	struct fman_prs_result *parse_result;
1424 	u16 ethertype = ntohs(skb->protocol);
1425 	struct ipv6hdr *ipv6h = NULL;
1426 	struct iphdr *iph;
1427 	int retval = 0;
1428 	u8 l4_proto;
1429 
1430 	if (skb->ip_summed != CHECKSUM_PARTIAL)
1431 		return 0;
1432 
1433 	/* Note: L3 csum seems to be already computed in sw, but we can't choose
1434 	 * L4 alone from the FM configuration anyway.
1435 	 */
1436 
1437 	/* Fill in some fields of the Parse Results array, so the FMan
1438 	 * can find them as if they came from the FMan Parser.
1439 	 */
1440 	parse_result = (struct fman_prs_result *)parse_results;
1441 
1442 	/* If we're dealing with VLAN, get the real Ethernet type */
1443 	if (ethertype == ETH_P_8021Q) {
1444 		/* We can't always assume the MAC header is set correctly
1445 		 * by the stack, so reset to beginning of skb->data
1446 		 */
1447 		skb_reset_mac_header(skb);
1448 		ethertype = ntohs(vlan_eth_hdr(skb)->h_vlan_encapsulated_proto);
1449 	}
1450 
1451 	/* Fill in the relevant L3 parse result fields
1452 	 * and read the L4 protocol type
1453 	 */
1454 	switch (ethertype) {
1455 	case ETH_P_IP:
1456 		parse_result->l3r = cpu_to_be16(FM_L3_PARSE_RESULT_IPV4);
1457 		iph = ip_hdr(skb);
1458 		WARN_ON(!iph);
1459 		l4_proto = iph->protocol;
1460 		break;
1461 	case ETH_P_IPV6:
1462 		parse_result->l3r = cpu_to_be16(FM_L3_PARSE_RESULT_IPV6);
1463 		ipv6h = ipv6_hdr(skb);
1464 		WARN_ON(!ipv6h);
1465 		l4_proto = ipv6h->nexthdr;
1466 		break;
1467 	default:
1468 		/* We shouldn't even be here */
1469 		if (net_ratelimit())
1470 			netif_alert(priv, tx_err, priv->net_dev,
1471 				    "Can't compute HW csum for L3 proto 0x%x\n",
1472 				    ntohs(skb->protocol));
1473 		retval = -EIO;
1474 		goto return_error;
1475 	}
1476 
1477 	/* Fill in the relevant L4 parse result fields */
1478 	switch (l4_proto) {
1479 	case IPPROTO_UDP:
1480 		parse_result->l4r = FM_L4_PARSE_RESULT_UDP;
1481 		break;
1482 	case IPPROTO_TCP:
1483 		parse_result->l4r = FM_L4_PARSE_RESULT_TCP;
1484 		break;
1485 	default:
1486 		if (net_ratelimit())
1487 			netif_alert(priv, tx_err, priv->net_dev,
1488 				    "Can't compute HW csum for L4 proto 0x%x\n",
1489 				    l4_proto);
1490 		retval = -EIO;
1491 		goto return_error;
1492 	}
1493 
1494 	/* At index 0 is IPOffset_1 as defined in the Parse Results */
1495 	parse_result->ip_off[0] = (u8)skb_network_offset(skb);
1496 	parse_result->l4_off = (u8)skb_transport_offset(skb);
1497 
1498 	/* Enable L3 (and L4, if TCP or UDP) HW checksum. */
1499 	fd->cmd |= cpu_to_be32(FM_FD_CMD_RPD | FM_FD_CMD_DTC);
1500 
1501 	/* On P1023 and similar platforms fd->cmd interpretation could
1502 	 * be disabled by setting CONTEXT_A bit ICMD; currently this bit
1503 	 * is not set so we do not need to check; in the future, if/when
1504 	 * using context_a we need to check this bit
1505 	 */
1506 
1507 return_error:
1508 	return retval;
1509 }
1510 
1511 static int dpaa_bp_add_8_bufs(const struct dpaa_bp *dpaa_bp)
1512 {
1513 	struct net_device *net_dev = dpaa_bp->priv->net_dev;
1514 	struct bm_buffer bmb[8];
1515 	dma_addr_t addr;
1516 	struct page *p;
1517 	u8 i;
1518 
1519 	for (i = 0; i < 8; i++) {
1520 		p = dev_alloc_pages(0);
1521 		if (unlikely(!p)) {
1522 			netdev_err(net_dev, "dev_alloc_pages() failed\n");
1523 			goto release_previous_buffs;
1524 		}
1525 
1526 		addr = dma_map_page(dpaa_bp->priv->rx_dma_dev, p, 0,
1527 				    DPAA_BP_RAW_SIZE, DMA_FROM_DEVICE);
1528 		if (unlikely(dma_mapping_error(dpaa_bp->priv->rx_dma_dev,
1529 					       addr))) {
1530 			netdev_err(net_dev, "DMA map failed\n");
1531 			goto release_previous_buffs;
1532 		}
1533 
1534 		bmb[i].data = 0;
1535 		bm_buffer_set64(&bmb[i], addr);
1536 	}
1537 
1538 release_bufs:
1539 	return dpaa_bman_release(dpaa_bp, bmb, i);
1540 
1541 release_previous_buffs:
1542 	WARN_ONCE(1, "dpaa_eth: failed to add buffers on Rx\n");
1543 
1544 	bm_buffer_set64(&bmb[i], 0);
1545 	/* Avoid releasing a completely null buffer; bman_release() requires
1546 	 * at least one buffer.
1547 	 */
1548 	if (likely(i))
1549 		goto release_bufs;
1550 
1551 	return 0;
1552 }
1553 
1554 static int dpaa_bp_seed(struct dpaa_bp *dpaa_bp)
1555 {
1556 	int i;
1557 
1558 	/* Give each CPU an allotment of "config_count" buffers */
1559 	for_each_possible_cpu(i) {
1560 		int *count_ptr = per_cpu_ptr(dpaa_bp->percpu_count, i);
1561 		int j;
1562 
1563 		/* Although we access another CPU's counters here
1564 		 * we do it at boot time so it is safe
1565 		 */
1566 		for (j = 0; j < dpaa_bp->config_count; j += 8)
1567 			*count_ptr += dpaa_bp_add_8_bufs(dpaa_bp);
1568 	}
1569 	return 0;
1570 }
1571 
1572 /* Add buffers/(pages) for Rx processing whenever bpool count falls below
1573  * REFILL_THRESHOLD.
1574  */
1575 static int dpaa_eth_refill_bpool(struct dpaa_bp *dpaa_bp, int *countptr)
1576 {
1577 	int count = *countptr;
1578 	int new_bufs;
1579 
1580 	if (unlikely(count < FSL_DPAA_ETH_REFILL_THRESHOLD)) {
1581 		do {
1582 			new_bufs = dpaa_bp_add_8_bufs(dpaa_bp);
1583 			if (unlikely(!new_bufs)) {
1584 				/* Avoid looping forever if we've temporarily
1585 				 * run out of memory. We'll try again at the
1586 				 * next NAPI cycle.
1587 				 */
1588 				break;
1589 			}
1590 			count += new_bufs;
1591 		} while (count < FSL_DPAA_ETH_MAX_BUF_COUNT);
1592 
1593 		*countptr = count;
1594 		if (unlikely(count < FSL_DPAA_ETH_MAX_BUF_COUNT))
1595 			return -ENOMEM;
1596 	}
1597 
1598 	return 0;
1599 }
1600 
1601 static int dpaa_eth_refill_bpools(struct dpaa_priv *priv)
1602 {
1603 	struct dpaa_bp *dpaa_bp;
1604 	int *countptr;
1605 
1606 	dpaa_bp = priv->dpaa_bp;
1607 	if (!dpaa_bp)
1608 		return -EINVAL;
1609 	countptr = this_cpu_ptr(dpaa_bp->percpu_count);
1610 
1611 	return dpaa_eth_refill_bpool(dpaa_bp, countptr);
1612 }
1613 
1614 /* Cleanup function for outgoing frame descriptors that were built on Tx path,
1615  * either contiguous frames or scatter/gather ones.
1616  * Skb freeing is not handled here.
1617  *
1618  * This function may be called on error paths in the Tx function, so guard
1619  * against cases when not all fd relevant fields were filled in. To avoid
1620  * reading the invalid transmission timestamp for the error paths set ts to
1621  * false.
1622  *
1623  * Return the skb backpointer, since for S/G frames the buffer containing it
1624  * gets freed here.
1625  *
1626  * No skb backpointer is set when transmitting XDP frames. Cleanup the buffer
1627  * and return NULL in this case.
1628  */
1629 static struct sk_buff *dpaa_cleanup_tx_fd(const struct dpaa_priv *priv,
1630 					  const struct qm_fd *fd, bool ts)
1631 {
1632 	const enum dma_data_direction dma_dir = DMA_TO_DEVICE;
1633 	struct device *dev = priv->net_dev->dev.parent;
1634 	struct skb_shared_hwtstamps shhwtstamps;
1635 	dma_addr_t addr = qm_fd_addr(fd);
1636 	void *vaddr = phys_to_virt(addr);
1637 	const struct qm_sg_entry *sgt;
1638 	struct dpaa_eth_swbp *swbp;
1639 	struct sk_buff *skb;
1640 	u64 ns;
1641 	int i;
1642 
1643 	if (unlikely(qm_fd_get_format(fd) == qm_fd_sg)) {
1644 		dma_unmap_page(priv->tx_dma_dev, addr,
1645 			       qm_fd_get_offset(fd) + DPAA_SGT_SIZE,
1646 			       dma_dir);
1647 
1648 		/* The sgt buffer has been allocated with netdev_alloc_frag(),
1649 		 * it's from lowmem.
1650 		 */
1651 		sgt = vaddr + qm_fd_get_offset(fd);
1652 
1653 		/* sgt[0] is from lowmem, was dma_map_single()-ed */
1654 		dma_unmap_single(priv->tx_dma_dev, qm_sg_addr(&sgt[0]),
1655 				 qm_sg_entry_get_len(&sgt[0]), dma_dir);
1656 
1657 		/* remaining pages were mapped with skb_frag_dma_map() */
1658 		for (i = 1; (i < DPAA_SGT_MAX_ENTRIES) &&
1659 		     !qm_sg_entry_is_final(&sgt[i - 1]); i++) {
1660 			WARN_ON(qm_sg_entry_is_ext(&sgt[i]));
1661 
1662 			dma_unmap_page(priv->tx_dma_dev, qm_sg_addr(&sgt[i]),
1663 				       qm_sg_entry_get_len(&sgt[i]), dma_dir);
1664 		}
1665 	} else {
1666 		dma_unmap_single(priv->tx_dma_dev, addr,
1667 				 qm_fd_get_offset(fd) + qm_fd_get_length(fd),
1668 				 dma_dir);
1669 	}
1670 
1671 	swbp = (struct dpaa_eth_swbp *)vaddr;
1672 	skb = swbp->skb;
1673 
1674 	/* No skb backpointer is set when running XDP. An xdp_frame
1675 	 * backpointer is saved instead.
1676 	 */
1677 	if (!skb) {
1678 		xdp_return_frame(swbp->xdpf);
1679 		return NULL;
1680 	}
1681 
1682 	/* DMA unmapping is required before accessing the HW provided info */
1683 	if (ts && priv->tx_tstamp &&
1684 	    skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
1685 		memset(&shhwtstamps, 0, sizeof(shhwtstamps));
1686 
1687 		if (!fman_port_get_tstamp(priv->mac_dev->port[TX], vaddr,
1688 					  &ns)) {
1689 			shhwtstamps.hwtstamp = ns_to_ktime(ns);
1690 			skb_tstamp_tx(skb, &shhwtstamps);
1691 		} else {
1692 			dev_warn(dev, "fman_port_get_tstamp failed!\n");
1693 		}
1694 	}
1695 
1696 	if (qm_fd_get_format(fd) == qm_fd_sg)
1697 		/* Free the page that we allocated on Tx for the SGT */
1698 		free_pages((unsigned long)vaddr, 0);
1699 
1700 	return skb;
1701 }
1702 
1703 static u8 rx_csum_offload(const struct dpaa_priv *priv, const struct qm_fd *fd)
1704 {
1705 	/* The parser has run and performed L4 checksum validation.
1706 	 * We know there were no parser errors (and implicitly no
1707 	 * L4 csum error), otherwise we wouldn't be here.
1708 	 */
1709 	if ((priv->net_dev->features & NETIF_F_RXCSUM) &&
1710 	    (be32_to_cpu(fd->status) & FM_FD_STAT_L4CV))
1711 		return CHECKSUM_UNNECESSARY;
1712 
1713 	/* We're here because either the parser didn't run or the L4 checksum
1714 	 * was not verified. This may include the case of a UDP frame with
1715 	 * checksum zero or an L4 proto other than TCP/UDP
1716 	 */
1717 	return CHECKSUM_NONE;
1718 }
1719 
1720 #define PTR_IS_ALIGNED(x, a) (IS_ALIGNED((unsigned long)(x), (a)))
1721 
1722 /* Build a linear skb around the received buffer.
1723  * We are guaranteed there is enough room at the end of the data buffer to
1724  * accommodate the shared info area of the skb.
1725  */
1726 static struct sk_buff *contig_fd_to_skb(const struct dpaa_priv *priv,
1727 					const struct qm_fd *fd)
1728 {
1729 	ssize_t fd_off = qm_fd_get_offset(fd);
1730 	dma_addr_t addr = qm_fd_addr(fd);
1731 	struct dpaa_bp *dpaa_bp;
1732 	struct sk_buff *skb;
1733 	void *vaddr;
1734 
1735 	vaddr = phys_to_virt(addr);
1736 	WARN_ON(!IS_ALIGNED((unsigned long)vaddr, SMP_CACHE_BYTES));
1737 
1738 	dpaa_bp = dpaa_bpid2pool(fd->bpid);
1739 	if (!dpaa_bp)
1740 		goto free_buffer;
1741 
1742 	skb = build_skb(vaddr, dpaa_bp->size +
1743 			SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
1744 	if (WARN_ONCE(!skb, "Build skb failure on Rx\n"))
1745 		goto free_buffer;
1746 	skb_reserve(skb, fd_off);
1747 	skb_put(skb, qm_fd_get_length(fd));
1748 
1749 	skb->ip_summed = rx_csum_offload(priv, fd);
1750 
1751 	return skb;
1752 
1753 free_buffer:
1754 	free_pages((unsigned long)vaddr, 0);
1755 	return NULL;
1756 }
1757 
1758 /* Build an skb with the data of the first S/G entry in the linear portion and
1759  * the rest of the frame as skb fragments.
1760  *
1761  * The page fragment holding the S/G Table is recycled here.
1762  */
1763 static struct sk_buff *sg_fd_to_skb(const struct dpaa_priv *priv,
1764 				    const struct qm_fd *fd)
1765 {
1766 	ssize_t fd_off = qm_fd_get_offset(fd);
1767 	dma_addr_t addr = qm_fd_addr(fd);
1768 	const struct qm_sg_entry *sgt;
1769 	struct page *page, *head_page;
1770 	struct dpaa_bp *dpaa_bp;
1771 	void *vaddr, *sg_vaddr;
1772 	int frag_off, frag_len;
1773 	struct sk_buff *skb;
1774 	dma_addr_t sg_addr;
1775 	int page_offset;
1776 	unsigned int sz;
1777 	int *count_ptr;
1778 	int i, j;
1779 
1780 	vaddr = phys_to_virt(addr);
1781 	WARN_ON(!IS_ALIGNED((unsigned long)vaddr, SMP_CACHE_BYTES));
1782 
1783 	/* Iterate through the SGT entries and add data buffers to the skb */
1784 	sgt = vaddr + fd_off;
1785 	skb = NULL;
1786 	for (i = 0; i < DPAA_SGT_MAX_ENTRIES; i++) {
1787 		/* Extension bit is not supported */
1788 		WARN_ON(qm_sg_entry_is_ext(&sgt[i]));
1789 
1790 		sg_addr = qm_sg_addr(&sgt[i]);
1791 		sg_vaddr = phys_to_virt(sg_addr);
1792 		WARN_ON(!PTR_IS_ALIGNED(sg_vaddr, SMP_CACHE_BYTES));
1793 
1794 		dma_unmap_page(priv->rx_dma_dev, sg_addr,
1795 			       DPAA_BP_RAW_SIZE, DMA_FROM_DEVICE);
1796 
1797 		/* We may use multiple Rx pools */
1798 		dpaa_bp = dpaa_bpid2pool(sgt[i].bpid);
1799 		if (!dpaa_bp)
1800 			goto free_buffers;
1801 
1802 		if (!skb) {
1803 			sz = dpaa_bp->size +
1804 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1805 			skb = build_skb(sg_vaddr, sz);
1806 			if (WARN_ON(!skb))
1807 				goto free_buffers;
1808 
1809 			skb->ip_summed = rx_csum_offload(priv, fd);
1810 
1811 			/* Make sure forwarded skbs will have enough space
1812 			 * on Tx, if extra headers are added.
1813 			 */
1814 			WARN_ON(fd_off != priv->rx_headroom);
1815 			skb_reserve(skb, fd_off);
1816 			skb_put(skb, qm_sg_entry_get_len(&sgt[i]));
1817 		} else {
1818 			/* Not the first S/G entry; all data from buffer will
1819 			 * be added in an skb fragment; fragment index is offset
1820 			 * by one since first S/G entry was incorporated in the
1821 			 * linear part of the skb.
1822 			 *
1823 			 * Caution: 'page' may be a tail page.
1824 			 */
1825 			page = virt_to_page(sg_vaddr);
1826 			head_page = virt_to_head_page(sg_vaddr);
1827 
1828 			/* Compute offset in (possibly tail) page */
1829 			page_offset = ((unsigned long)sg_vaddr &
1830 					(PAGE_SIZE - 1)) +
1831 				(page_address(page) - page_address(head_page));
1832 			/* page_offset only refers to the beginning of sgt[i];
1833 			 * but the buffer itself may have an internal offset.
1834 			 */
1835 			frag_off = qm_sg_entry_get_off(&sgt[i]) + page_offset;
1836 			frag_len = qm_sg_entry_get_len(&sgt[i]);
1837 			/* skb_add_rx_frag() does no checking on the page; if
1838 			 * we pass it a tail page, we'll end up with
1839 			 * bad page accounting and eventually with segafults.
1840 			 */
1841 			skb_add_rx_frag(skb, i - 1, head_page, frag_off,
1842 					frag_len, dpaa_bp->size);
1843 		}
1844 
1845 		/* Update the pool count for the current {cpu x bpool} */
1846 		count_ptr = this_cpu_ptr(dpaa_bp->percpu_count);
1847 		(*count_ptr)--;
1848 
1849 		if (qm_sg_entry_is_final(&sgt[i]))
1850 			break;
1851 	}
1852 	WARN_ONCE(i == DPAA_SGT_MAX_ENTRIES, "No final bit on SGT\n");
1853 
1854 	/* free the SG table buffer */
1855 	free_pages((unsigned long)vaddr, 0);
1856 
1857 	return skb;
1858 
1859 free_buffers:
1860 	/* free all the SG entries */
1861 	for (j = 0; j < DPAA_SGT_MAX_ENTRIES ; j++) {
1862 		sg_addr = qm_sg_addr(&sgt[j]);
1863 		sg_vaddr = phys_to_virt(sg_addr);
1864 		/* all pages 0..i were unmaped */
1865 		if (j > i)
1866 			dma_unmap_page(priv->rx_dma_dev, qm_sg_addr(&sgt[j]),
1867 				       DPAA_BP_RAW_SIZE, DMA_FROM_DEVICE);
1868 		free_pages((unsigned long)sg_vaddr, 0);
1869 		/* counters 0..i-1 were decremented */
1870 		if (j >= i) {
1871 			dpaa_bp = dpaa_bpid2pool(sgt[j].bpid);
1872 			if (dpaa_bp) {
1873 				count_ptr = this_cpu_ptr(dpaa_bp->percpu_count);
1874 				(*count_ptr)--;
1875 			}
1876 		}
1877 
1878 		if (qm_sg_entry_is_final(&sgt[j]))
1879 			break;
1880 	}
1881 	/* free the SGT fragment */
1882 	free_pages((unsigned long)vaddr, 0);
1883 
1884 	return NULL;
1885 }
1886 
1887 static int skb_to_contig_fd(struct dpaa_priv *priv,
1888 			    struct sk_buff *skb, struct qm_fd *fd,
1889 			    int *offset)
1890 {
1891 	struct net_device *net_dev = priv->net_dev;
1892 	enum dma_data_direction dma_dir;
1893 	struct dpaa_eth_swbp *swbp;
1894 	unsigned char *buff_start;
1895 	dma_addr_t addr;
1896 	int err;
1897 
1898 	/* We are guaranteed to have at least tx_headroom bytes
1899 	 * available, so just use that for offset.
1900 	 */
1901 	fd->bpid = FSL_DPAA_BPID_INV;
1902 	buff_start = skb->data - priv->tx_headroom;
1903 	dma_dir = DMA_TO_DEVICE;
1904 
1905 	swbp = (struct dpaa_eth_swbp *)buff_start;
1906 	swbp->skb = skb;
1907 
1908 	/* Enable L3/L4 hardware checksum computation.
1909 	 *
1910 	 * We must do this before dma_map_single(DMA_TO_DEVICE), because we may
1911 	 * need to write into the skb.
1912 	 */
1913 	err = dpaa_enable_tx_csum(priv, skb, fd,
1914 				  buff_start + DPAA_TX_PRIV_DATA_SIZE);
1915 	if (unlikely(err < 0)) {
1916 		if (net_ratelimit())
1917 			netif_err(priv, tx_err, net_dev, "HW csum error: %d\n",
1918 				  err);
1919 		return err;
1920 	}
1921 
1922 	/* Fill in the rest of the FD fields */
1923 	qm_fd_set_contig(fd, priv->tx_headroom, skb->len);
1924 	fd->cmd |= cpu_to_be32(FM_FD_CMD_FCO);
1925 
1926 	/* Map the entire buffer size that may be seen by FMan, but no more */
1927 	addr = dma_map_single(priv->tx_dma_dev, buff_start,
1928 			      priv->tx_headroom + skb->len, dma_dir);
1929 	if (unlikely(dma_mapping_error(priv->tx_dma_dev, addr))) {
1930 		if (net_ratelimit())
1931 			netif_err(priv, tx_err, net_dev, "dma_map_single() failed\n");
1932 		return -EINVAL;
1933 	}
1934 	qm_fd_addr_set64(fd, addr);
1935 
1936 	return 0;
1937 }
1938 
1939 static int skb_to_sg_fd(struct dpaa_priv *priv,
1940 			struct sk_buff *skb, struct qm_fd *fd)
1941 {
1942 	const enum dma_data_direction dma_dir = DMA_TO_DEVICE;
1943 	const int nr_frags = skb_shinfo(skb)->nr_frags;
1944 	struct net_device *net_dev = priv->net_dev;
1945 	struct dpaa_eth_swbp *swbp;
1946 	struct qm_sg_entry *sgt;
1947 	void *buff_start;
1948 	skb_frag_t *frag;
1949 	dma_addr_t addr;
1950 	size_t frag_len;
1951 	struct page *p;
1952 	int i, j, err;
1953 
1954 	/* get a page to store the SGTable */
1955 	p = dev_alloc_pages(0);
1956 	if (unlikely(!p)) {
1957 		netdev_err(net_dev, "dev_alloc_pages() failed\n");
1958 		return -ENOMEM;
1959 	}
1960 	buff_start = page_address(p);
1961 
1962 	/* Enable L3/L4 hardware checksum computation.
1963 	 *
1964 	 * We must do this before dma_map_single(DMA_TO_DEVICE), because we may
1965 	 * need to write into the skb.
1966 	 */
1967 	err = dpaa_enable_tx_csum(priv, skb, fd,
1968 				  buff_start + DPAA_TX_PRIV_DATA_SIZE);
1969 	if (unlikely(err < 0)) {
1970 		if (net_ratelimit())
1971 			netif_err(priv, tx_err, net_dev, "HW csum error: %d\n",
1972 				  err);
1973 		goto csum_failed;
1974 	}
1975 
1976 	/* SGT[0] is used by the linear part */
1977 	sgt = (struct qm_sg_entry *)(buff_start + priv->tx_headroom);
1978 	frag_len = skb_headlen(skb);
1979 	qm_sg_entry_set_len(&sgt[0], frag_len);
1980 	sgt[0].bpid = FSL_DPAA_BPID_INV;
1981 	sgt[0].offset = 0;
1982 	addr = dma_map_single(priv->tx_dma_dev, skb->data,
1983 			      skb_headlen(skb), dma_dir);
1984 	if (unlikely(dma_mapping_error(priv->tx_dma_dev, addr))) {
1985 		netdev_err(priv->net_dev, "DMA mapping failed\n");
1986 		err = -EINVAL;
1987 		goto sg0_map_failed;
1988 	}
1989 	qm_sg_entry_set64(&sgt[0], addr);
1990 
1991 	/* populate the rest of SGT entries */
1992 	for (i = 0; i < nr_frags; i++) {
1993 		frag = &skb_shinfo(skb)->frags[i];
1994 		frag_len = skb_frag_size(frag);
1995 		WARN_ON(!skb_frag_page(frag));
1996 		addr = skb_frag_dma_map(priv->tx_dma_dev, frag, 0,
1997 					frag_len, dma_dir);
1998 		if (unlikely(dma_mapping_error(priv->tx_dma_dev, addr))) {
1999 			netdev_err(priv->net_dev, "DMA mapping failed\n");
2000 			err = -EINVAL;
2001 			goto sg_map_failed;
2002 		}
2003 
2004 		qm_sg_entry_set_len(&sgt[i + 1], frag_len);
2005 		sgt[i + 1].bpid = FSL_DPAA_BPID_INV;
2006 		sgt[i + 1].offset = 0;
2007 
2008 		/* keep the offset in the address */
2009 		qm_sg_entry_set64(&sgt[i + 1], addr);
2010 	}
2011 
2012 	/* Set the final bit in the last used entry of the SGT */
2013 	qm_sg_entry_set_f(&sgt[nr_frags], frag_len);
2014 
2015 	/* set fd offset to priv->tx_headroom */
2016 	qm_fd_set_sg(fd, priv->tx_headroom, skb->len);
2017 
2018 	/* DMA map the SGT page */
2019 	swbp = (struct dpaa_eth_swbp *)buff_start;
2020 	swbp->skb = skb;
2021 
2022 	addr = dma_map_page(priv->tx_dma_dev, p, 0,
2023 			    priv->tx_headroom + DPAA_SGT_SIZE, dma_dir);
2024 	if (unlikely(dma_mapping_error(priv->tx_dma_dev, addr))) {
2025 		netdev_err(priv->net_dev, "DMA mapping failed\n");
2026 		err = -EINVAL;
2027 		goto sgt_map_failed;
2028 	}
2029 
2030 	fd->bpid = FSL_DPAA_BPID_INV;
2031 	fd->cmd |= cpu_to_be32(FM_FD_CMD_FCO);
2032 	qm_fd_addr_set64(fd, addr);
2033 
2034 	return 0;
2035 
2036 sgt_map_failed:
2037 sg_map_failed:
2038 	for (j = 0; j < i; j++)
2039 		dma_unmap_page(priv->tx_dma_dev, qm_sg_addr(&sgt[j]),
2040 			       qm_sg_entry_get_len(&sgt[j]), dma_dir);
2041 sg0_map_failed:
2042 csum_failed:
2043 	free_pages((unsigned long)buff_start, 0);
2044 
2045 	return err;
2046 }
2047 
2048 static inline int dpaa_xmit(struct dpaa_priv *priv,
2049 			    struct rtnl_link_stats64 *percpu_stats,
2050 			    int queue,
2051 			    struct qm_fd *fd)
2052 {
2053 	struct qman_fq *egress_fq;
2054 	int err, i;
2055 
2056 	egress_fq = priv->egress_fqs[queue];
2057 	if (fd->bpid == FSL_DPAA_BPID_INV)
2058 		fd->cmd |= cpu_to_be32(qman_fq_fqid(priv->conf_fqs[queue]));
2059 
2060 	/* Trace this Tx fd */
2061 	trace_dpaa_tx_fd(priv->net_dev, egress_fq, fd);
2062 
2063 	for (i = 0; i < DPAA_ENQUEUE_RETRIES; i++) {
2064 		err = qman_enqueue(egress_fq, fd);
2065 		if (err != -EBUSY)
2066 			break;
2067 	}
2068 
2069 	if (unlikely(err < 0)) {
2070 		percpu_stats->tx_fifo_errors++;
2071 		return err;
2072 	}
2073 
2074 	percpu_stats->tx_packets++;
2075 	percpu_stats->tx_bytes += qm_fd_get_length(fd);
2076 
2077 	return 0;
2078 }
2079 
2080 #ifdef CONFIG_DPAA_ERRATUM_A050385
2081 static int dpaa_a050385_wa_skb(struct net_device *net_dev, struct sk_buff **s)
2082 {
2083 	struct dpaa_priv *priv = netdev_priv(net_dev);
2084 	struct sk_buff *new_skb, *skb = *s;
2085 	unsigned char *start, i;
2086 
2087 	/* check linear buffer alignment */
2088 	if (!PTR_IS_ALIGNED(skb->data, DPAA_A050385_ALIGN))
2089 		goto workaround;
2090 
2091 	/* linear buffers just need to have an aligned start */
2092 	if (!skb_is_nonlinear(skb))
2093 		return 0;
2094 
2095 	/* linear data size for nonlinear skbs needs to be aligned */
2096 	if (!IS_ALIGNED(skb_headlen(skb), DPAA_A050385_ALIGN))
2097 		goto workaround;
2098 
2099 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2100 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2101 
2102 		/* all fragments need to have aligned start addresses */
2103 		if (!IS_ALIGNED(skb_frag_off(frag), DPAA_A050385_ALIGN))
2104 			goto workaround;
2105 
2106 		/* all but last fragment need to have aligned sizes */
2107 		if (!IS_ALIGNED(skb_frag_size(frag), DPAA_A050385_ALIGN) &&
2108 		    (i < skb_shinfo(skb)->nr_frags - 1))
2109 			goto workaround;
2110 	}
2111 
2112 	return 0;
2113 
2114 workaround:
2115 	/* copy all the skb content into a new linear buffer */
2116 	new_skb = netdev_alloc_skb(net_dev, skb->len + DPAA_A050385_ALIGN - 1 +
2117 						priv->tx_headroom);
2118 	if (!new_skb)
2119 		return -ENOMEM;
2120 
2121 	/* NET_SKB_PAD bytes already reserved, adding up to tx_headroom */
2122 	skb_reserve(new_skb, priv->tx_headroom - NET_SKB_PAD);
2123 
2124 	/* Workaround for DPAA_A050385 requires data start to be aligned */
2125 	start = PTR_ALIGN(new_skb->data, DPAA_A050385_ALIGN);
2126 	if (start - new_skb->data)
2127 		skb_reserve(new_skb, start - new_skb->data);
2128 
2129 	skb_put(new_skb, skb->len);
2130 	skb_copy_bits(skb, 0, new_skb->data, skb->len);
2131 	skb_copy_header(new_skb, skb);
2132 	new_skb->dev = skb->dev;
2133 
2134 	/* Copy relevant timestamp info from the old skb to the new */
2135 	if (priv->tx_tstamp) {
2136 		skb_shinfo(new_skb)->tx_flags = skb_shinfo(skb)->tx_flags;
2137 		skb_shinfo(new_skb)->hwtstamps = skb_shinfo(skb)->hwtstamps;
2138 		skb_shinfo(new_skb)->tskey = skb_shinfo(skb)->tskey;
2139 		if (skb->sk)
2140 			skb_set_owner_w(new_skb, skb->sk);
2141 	}
2142 
2143 	/* We move the headroom when we align it so we have to reset the
2144 	 * network and transport header offsets relative to the new data
2145 	 * pointer. The checksum offload relies on these offsets.
2146 	 */
2147 	skb_set_network_header(new_skb, skb_network_offset(skb));
2148 	skb_set_transport_header(new_skb, skb_transport_offset(skb));
2149 
2150 	dev_kfree_skb(skb);
2151 	*s = new_skb;
2152 
2153 	return 0;
2154 }
2155 
2156 static int dpaa_a050385_wa_xdpf(struct dpaa_priv *priv,
2157 				struct xdp_frame **init_xdpf)
2158 {
2159 	struct xdp_frame *new_xdpf, *xdpf = *init_xdpf;
2160 	void *new_buff, *aligned_data;
2161 	struct page *p;
2162 	u32 data_shift;
2163 	int headroom;
2164 
2165 	/* Check the data alignment and make sure the headroom is large
2166 	 * enough to store the xdpf backpointer. Use an aligned headroom
2167 	 * value.
2168 	 *
2169 	 * Due to alignment constraints, we give XDP access to the full 256
2170 	 * byte frame headroom. If the XDP program uses all of it, copy the
2171 	 * data to a new buffer and make room for storing the backpointer.
2172 	 */
2173 	if (PTR_IS_ALIGNED(xdpf->data, DPAA_FD_DATA_ALIGNMENT) &&
2174 	    xdpf->headroom >= priv->tx_headroom) {
2175 		xdpf->headroom = priv->tx_headroom;
2176 		return 0;
2177 	}
2178 
2179 	/* Try to move the data inside the buffer just enough to align it and
2180 	 * store the xdpf backpointer. If the available headroom isn't large
2181 	 * enough, resort to allocating a new buffer and copying the data.
2182 	 */
2183 	aligned_data = PTR_ALIGN_DOWN(xdpf->data, DPAA_FD_DATA_ALIGNMENT);
2184 	data_shift = xdpf->data - aligned_data;
2185 
2186 	/* The XDP frame's headroom needs to be large enough to accommodate
2187 	 * shifting the data as well as storing the xdpf backpointer.
2188 	 */
2189 	if (xdpf->headroom  >= data_shift + priv->tx_headroom) {
2190 		memmove(aligned_data, xdpf->data, xdpf->len);
2191 		xdpf->data = aligned_data;
2192 		xdpf->headroom = priv->tx_headroom;
2193 		return 0;
2194 	}
2195 
2196 	/* The new xdp_frame is stored in the new buffer. Reserve enough space
2197 	 * in the headroom for storing it along with the driver's private
2198 	 * info. The headroom needs to be aligned to DPAA_FD_DATA_ALIGNMENT to
2199 	 * guarantee the data's alignment in the buffer.
2200 	 */
2201 	headroom = ALIGN(sizeof(*new_xdpf) + priv->tx_headroom,
2202 			 DPAA_FD_DATA_ALIGNMENT);
2203 
2204 	/* Assure the extended headroom and data don't overflow the buffer,
2205 	 * while maintaining the mandatory tailroom.
2206 	 */
2207 	if (headroom + xdpf->len > DPAA_BP_RAW_SIZE -
2208 			SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
2209 		return -ENOMEM;
2210 
2211 	p = dev_alloc_pages(0);
2212 	if (unlikely(!p))
2213 		return -ENOMEM;
2214 
2215 	/* Copy the data to the new buffer at a properly aligned offset */
2216 	new_buff = page_address(p);
2217 	memcpy(new_buff + headroom, xdpf->data, xdpf->len);
2218 
2219 	/* Create an XDP frame around the new buffer in a similar fashion
2220 	 * to xdp_convert_buff_to_frame.
2221 	 */
2222 	new_xdpf = new_buff;
2223 	new_xdpf->data = new_buff + headroom;
2224 	new_xdpf->len = xdpf->len;
2225 	new_xdpf->headroom = priv->tx_headroom;
2226 	new_xdpf->frame_sz = DPAA_BP_RAW_SIZE;
2227 	new_xdpf->mem.type = MEM_TYPE_PAGE_ORDER0;
2228 
2229 	/* Release the initial buffer */
2230 	xdp_return_frame_rx_napi(xdpf);
2231 
2232 	*init_xdpf = new_xdpf;
2233 	return 0;
2234 }
2235 #endif
2236 
2237 static netdev_tx_t
2238 dpaa_start_xmit(struct sk_buff *skb, struct net_device *net_dev)
2239 {
2240 	const int queue_mapping = skb_get_queue_mapping(skb);
2241 	bool nonlinear = skb_is_nonlinear(skb);
2242 	struct rtnl_link_stats64 *percpu_stats;
2243 	struct dpaa_percpu_priv *percpu_priv;
2244 	struct netdev_queue *txq;
2245 	struct dpaa_priv *priv;
2246 	struct qm_fd fd;
2247 	int offset = 0;
2248 	int err = 0;
2249 
2250 	priv = netdev_priv(net_dev);
2251 	percpu_priv = this_cpu_ptr(priv->percpu_priv);
2252 	percpu_stats = &percpu_priv->stats;
2253 
2254 	qm_fd_clear_fd(&fd);
2255 
2256 	if (!nonlinear) {
2257 		/* We're going to store the skb backpointer at the beginning
2258 		 * of the data buffer, so we need a privately owned skb
2259 		 *
2260 		 * We've made sure skb is not shared in dev->priv_flags,
2261 		 * we need to verify the skb head is not cloned
2262 		 */
2263 		if (skb_cow_head(skb, priv->tx_headroom))
2264 			goto enomem;
2265 
2266 		WARN_ON(skb_is_nonlinear(skb));
2267 	}
2268 
2269 	/* MAX_SKB_FRAGS is equal or larger than our dpaa_SGT_MAX_ENTRIES;
2270 	 * make sure we don't feed FMan with more fragments than it supports.
2271 	 */
2272 	if (unlikely(nonlinear &&
2273 		     (skb_shinfo(skb)->nr_frags >= DPAA_SGT_MAX_ENTRIES))) {
2274 		/* If the egress skb contains more fragments than we support
2275 		 * we have no choice but to linearize it ourselves.
2276 		 */
2277 		if (__skb_linearize(skb))
2278 			goto enomem;
2279 
2280 		nonlinear = skb_is_nonlinear(skb);
2281 	}
2282 
2283 #ifdef CONFIG_DPAA_ERRATUM_A050385
2284 	if (unlikely(fman_has_errata_a050385())) {
2285 		if (dpaa_a050385_wa_skb(net_dev, &skb))
2286 			goto enomem;
2287 		nonlinear = skb_is_nonlinear(skb);
2288 	}
2289 #endif
2290 
2291 	if (nonlinear) {
2292 		/* Just create a S/G fd based on the skb */
2293 		err = skb_to_sg_fd(priv, skb, &fd);
2294 		percpu_priv->tx_frag_skbuffs++;
2295 	} else {
2296 		/* Create a contig FD from this skb */
2297 		err = skb_to_contig_fd(priv, skb, &fd, &offset);
2298 	}
2299 	if (unlikely(err < 0))
2300 		goto skb_to_fd_failed;
2301 
2302 	txq = netdev_get_tx_queue(net_dev, queue_mapping);
2303 
2304 	/* LLTX requires to do our own update of trans_start */
2305 	txq_trans_cond_update(txq);
2306 
2307 	if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
2308 		fd.cmd |= cpu_to_be32(FM_FD_CMD_UPD);
2309 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2310 	}
2311 
2312 	if (likely(dpaa_xmit(priv, percpu_stats, queue_mapping, &fd) == 0))
2313 		return NETDEV_TX_OK;
2314 
2315 	dpaa_cleanup_tx_fd(priv, &fd, false);
2316 skb_to_fd_failed:
2317 enomem:
2318 	percpu_stats->tx_errors++;
2319 	dev_kfree_skb(skb);
2320 	return NETDEV_TX_OK;
2321 }
2322 
2323 static void dpaa_rx_error(struct net_device *net_dev,
2324 			  const struct dpaa_priv *priv,
2325 			  struct dpaa_percpu_priv *percpu_priv,
2326 			  const struct qm_fd *fd,
2327 			  u32 fqid)
2328 {
2329 	if (net_ratelimit())
2330 		netif_err(priv, hw, net_dev, "Err FD status = 0x%08x\n",
2331 			  be32_to_cpu(fd->status) & FM_FD_STAT_RX_ERRORS);
2332 
2333 	percpu_priv->stats.rx_errors++;
2334 
2335 	if (be32_to_cpu(fd->status) & FM_FD_ERR_DMA)
2336 		percpu_priv->rx_errors.dme++;
2337 	if (be32_to_cpu(fd->status) & FM_FD_ERR_PHYSICAL)
2338 		percpu_priv->rx_errors.fpe++;
2339 	if (be32_to_cpu(fd->status) & FM_FD_ERR_SIZE)
2340 		percpu_priv->rx_errors.fse++;
2341 	if (be32_to_cpu(fd->status) & FM_FD_ERR_PRS_HDR_ERR)
2342 		percpu_priv->rx_errors.phe++;
2343 
2344 	dpaa_fd_release(net_dev, fd);
2345 }
2346 
2347 static void dpaa_tx_error(struct net_device *net_dev,
2348 			  const struct dpaa_priv *priv,
2349 			  struct dpaa_percpu_priv *percpu_priv,
2350 			  const struct qm_fd *fd,
2351 			  u32 fqid)
2352 {
2353 	struct sk_buff *skb;
2354 
2355 	if (net_ratelimit())
2356 		netif_warn(priv, hw, net_dev, "FD status = 0x%08x\n",
2357 			   be32_to_cpu(fd->status) & FM_FD_STAT_TX_ERRORS);
2358 
2359 	percpu_priv->stats.tx_errors++;
2360 
2361 	skb = dpaa_cleanup_tx_fd(priv, fd, false);
2362 	dev_kfree_skb(skb);
2363 }
2364 
2365 static int dpaa_eth_poll(struct napi_struct *napi, int budget)
2366 {
2367 	struct dpaa_napi_portal *np =
2368 			container_of(napi, struct dpaa_napi_portal, napi);
2369 	int cleaned;
2370 
2371 	np->xdp_act = 0;
2372 
2373 	cleaned = qman_p_poll_dqrr(np->p, budget);
2374 
2375 	if (cleaned < budget) {
2376 		napi_complete_done(napi, cleaned);
2377 		qman_p_irqsource_add(np->p, QM_PIRQ_DQRI);
2378 	} else if (np->down) {
2379 		qman_p_irqsource_add(np->p, QM_PIRQ_DQRI);
2380 	}
2381 
2382 	if (np->xdp_act & XDP_REDIRECT)
2383 		xdp_do_flush();
2384 
2385 	return cleaned;
2386 }
2387 
2388 static void dpaa_tx_conf(struct net_device *net_dev,
2389 			 const struct dpaa_priv *priv,
2390 			 struct dpaa_percpu_priv *percpu_priv,
2391 			 const struct qm_fd *fd,
2392 			 u32 fqid)
2393 {
2394 	struct sk_buff	*skb;
2395 
2396 	if (unlikely(be32_to_cpu(fd->status) & FM_FD_STAT_TX_ERRORS)) {
2397 		if (net_ratelimit())
2398 			netif_warn(priv, hw, net_dev, "FD status = 0x%08x\n",
2399 				   be32_to_cpu(fd->status) &
2400 				   FM_FD_STAT_TX_ERRORS);
2401 
2402 		percpu_priv->stats.tx_errors++;
2403 	}
2404 
2405 	percpu_priv->tx_confirm++;
2406 
2407 	skb = dpaa_cleanup_tx_fd(priv, fd, true);
2408 
2409 	consume_skb(skb);
2410 }
2411 
2412 static inline int dpaa_eth_napi_schedule(struct dpaa_percpu_priv *percpu_priv,
2413 					 struct qman_portal *portal, bool sched_napi)
2414 {
2415 	if (sched_napi) {
2416 		/* Disable QMan IRQ and invoke NAPI */
2417 		qman_p_irqsource_remove(portal, QM_PIRQ_DQRI);
2418 
2419 		percpu_priv->np.p = portal;
2420 		napi_schedule(&percpu_priv->np.napi);
2421 		percpu_priv->in_interrupt++;
2422 		return 1;
2423 	}
2424 	return 0;
2425 }
2426 
2427 static enum qman_cb_dqrr_result rx_error_dqrr(struct qman_portal *portal,
2428 					      struct qman_fq *fq,
2429 					      const struct qm_dqrr_entry *dq,
2430 					      bool sched_napi)
2431 {
2432 	struct dpaa_fq *dpaa_fq = container_of(fq, struct dpaa_fq, fq_base);
2433 	struct dpaa_percpu_priv *percpu_priv;
2434 	struct net_device *net_dev;
2435 	struct dpaa_bp *dpaa_bp;
2436 	struct dpaa_priv *priv;
2437 
2438 	net_dev = dpaa_fq->net_dev;
2439 	priv = netdev_priv(net_dev);
2440 	dpaa_bp = dpaa_bpid2pool(dq->fd.bpid);
2441 	if (!dpaa_bp)
2442 		return qman_cb_dqrr_consume;
2443 
2444 	percpu_priv = this_cpu_ptr(priv->percpu_priv);
2445 
2446 	if (dpaa_eth_napi_schedule(percpu_priv, portal, sched_napi))
2447 		return qman_cb_dqrr_stop;
2448 
2449 	dpaa_eth_refill_bpools(priv);
2450 	dpaa_rx_error(net_dev, priv, percpu_priv, &dq->fd, fq->fqid);
2451 
2452 	return qman_cb_dqrr_consume;
2453 }
2454 
2455 static int dpaa_xdp_xmit_frame(struct net_device *net_dev,
2456 			       struct xdp_frame *xdpf)
2457 {
2458 	struct dpaa_priv *priv = netdev_priv(net_dev);
2459 	struct rtnl_link_stats64 *percpu_stats;
2460 	struct dpaa_percpu_priv *percpu_priv;
2461 	struct dpaa_eth_swbp *swbp;
2462 	struct netdev_queue *txq;
2463 	void *buff_start;
2464 	struct qm_fd fd;
2465 	dma_addr_t addr;
2466 	int err;
2467 
2468 	percpu_priv = this_cpu_ptr(priv->percpu_priv);
2469 	percpu_stats = &percpu_priv->stats;
2470 
2471 #ifdef CONFIG_DPAA_ERRATUM_A050385
2472 	if (unlikely(fman_has_errata_a050385())) {
2473 		if (dpaa_a050385_wa_xdpf(priv, &xdpf)) {
2474 			err = -ENOMEM;
2475 			goto out_error;
2476 		}
2477 	}
2478 #endif
2479 
2480 	if (xdpf->headroom < DPAA_TX_PRIV_DATA_SIZE) {
2481 		err = -EINVAL;
2482 		goto out_error;
2483 	}
2484 
2485 	buff_start = xdpf->data - xdpf->headroom;
2486 
2487 	/* Leave empty the skb backpointer at the start of the buffer.
2488 	 * Save the XDP frame for easy cleanup on confirmation.
2489 	 */
2490 	swbp = (struct dpaa_eth_swbp *)buff_start;
2491 	swbp->skb = NULL;
2492 	swbp->xdpf = xdpf;
2493 
2494 	qm_fd_clear_fd(&fd);
2495 	fd.bpid = FSL_DPAA_BPID_INV;
2496 	fd.cmd |= cpu_to_be32(FM_FD_CMD_FCO);
2497 	qm_fd_set_contig(&fd, xdpf->headroom, xdpf->len);
2498 
2499 	addr = dma_map_single(priv->tx_dma_dev, buff_start,
2500 			      xdpf->headroom + xdpf->len,
2501 			      DMA_TO_DEVICE);
2502 	if (unlikely(dma_mapping_error(priv->tx_dma_dev, addr))) {
2503 		err = -EINVAL;
2504 		goto out_error;
2505 	}
2506 
2507 	qm_fd_addr_set64(&fd, addr);
2508 
2509 	/* Bump the trans_start */
2510 	txq = netdev_get_tx_queue(net_dev, smp_processor_id());
2511 	txq_trans_cond_update(txq);
2512 
2513 	err = dpaa_xmit(priv, percpu_stats, smp_processor_id(), &fd);
2514 	if (err) {
2515 		dma_unmap_single(priv->tx_dma_dev, addr,
2516 				 qm_fd_get_offset(&fd) + qm_fd_get_length(&fd),
2517 				 DMA_TO_DEVICE);
2518 		goto out_error;
2519 	}
2520 
2521 	return 0;
2522 
2523 out_error:
2524 	percpu_stats->tx_errors++;
2525 	return err;
2526 }
2527 
2528 static u32 dpaa_run_xdp(struct dpaa_priv *priv, struct qm_fd *fd, void *vaddr,
2529 			struct dpaa_fq *dpaa_fq, unsigned int *xdp_meta_len)
2530 {
2531 	ssize_t fd_off = qm_fd_get_offset(fd);
2532 	struct bpf_prog *xdp_prog;
2533 	struct xdp_frame *xdpf;
2534 	struct xdp_buff xdp;
2535 	u32 xdp_act;
2536 	int err;
2537 
2538 	xdp_prog = READ_ONCE(priv->xdp_prog);
2539 	if (!xdp_prog)
2540 		return XDP_PASS;
2541 
2542 	xdp_init_buff(&xdp, DPAA_BP_RAW_SIZE - DPAA_TX_PRIV_DATA_SIZE,
2543 		      &dpaa_fq->xdp_rxq);
2544 	xdp_prepare_buff(&xdp, vaddr + fd_off - XDP_PACKET_HEADROOM,
2545 			 XDP_PACKET_HEADROOM, qm_fd_get_length(fd), true);
2546 
2547 	/* We reserve a fixed headroom of 256 bytes under the erratum and we
2548 	 * offer it all to XDP programs to use. If no room is left for the
2549 	 * xdpf backpointer on TX, we will need to copy the data.
2550 	 * Disable metadata support since data realignments might be required
2551 	 * and the information can be lost.
2552 	 */
2553 #ifdef CONFIG_DPAA_ERRATUM_A050385
2554 	if (unlikely(fman_has_errata_a050385())) {
2555 		xdp_set_data_meta_invalid(&xdp);
2556 		xdp.data_hard_start = vaddr;
2557 		xdp.frame_sz = DPAA_BP_RAW_SIZE;
2558 	}
2559 #endif
2560 
2561 	xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
2562 
2563 	/* Update the length and the offset of the FD */
2564 	qm_fd_set_contig(fd, xdp.data - vaddr, xdp.data_end - xdp.data);
2565 
2566 	switch (xdp_act) {
2567 	case XDP_PASS:
2568 #ifdef CONFIG_DPAA_ERRATUM_A050385
2569 		*xdp_meta_len = xdp_data_meta_unsupported(&xdp) ? 0 :
2570 				xdp.data - xdp.data_meta;
2571 #else
2572 		*xdp_meta_len = xdp.data - xdp.data_meta;
2573 #endif
2574 		break;
2575 	case XDP_TX:
2576 		/* We can access the full headroom when sending the frame
2577 		 * back out
2578 		 */
2579 		xdp.data_hard_start = vaddr;
2580 		xdp.frame_sz = DPAA_BP_RAW_SIZE;
2581 		xdpf = xdp_convert_buff_to_frame(&xdp);
2582 		if (unlikely(!xdpf)) {
2583 			free_pages((unsigned long)vaddr, 0);
2584 			break;
2585 		}
2586 
2587 		if (dpaa_xdp_xmit_frame(priv->net_dev, xdpf))
2588 			xdp_return_frame_rx_napi(xdpf);
2589 
2590 		break;
2591 	case XDP_REDIRECT:
2592 		/* Allow redirect to use the full headroom */
2593 		xdp.data_hard_start = vaddr;
2594 		xdp.frame_sz = DPAA_BP_RAW_SIZE;
2595 
2596 		err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog);
2597 		if (err) {
2598 			trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
2599 			free_pages((unsigned long)vaddr, 0);
2600 		}
2601 		break;
2602 	default:
2603 		bpf_warn_invalid_xdp_action(priv->net_dev, xdp_prog, xdp_act);
2604 		fallthrough;
2605 	case XDP_ABORTED:
2606 		trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
2607 		fallthrough;
2608 	case XDP_DROP:
2609 		/* Free the buffer */
2610 		free_pages((unsigned long)vaddr, 0);
2611 		break;
2612 	}
2613 
2614 	return xdp_act;
2615 }
2616 
2617 static enum qman_cb_dqrr_result rx_default_dqrr(struct qman_portal *portal,
2618 						struct qman_fq *fq,
2619 						const struct qm_dqrr_entry *dq,
2620 						bool sched_napi)
2621 {
2622 	bool ts_valid = false, hash_valid = false;
2623 	struct skb_shared_hwtstamps *shhwtstamps;
2624 	unsigned int skb_len, xdp_meta_len = 0;
2625 	struct rtnl_link_stats64 *percpu_stats;
2626 	struct dpaa_percpu_priv *percpu_priv;
2627 	const struct qm_fd *fd = &dq->fd;
2628 	dma_addr_t addr = qm_fd_addr(fd);
2629 	struct dpaa_napi_portal *np;
2630 	enum qm_fd_format fd_format;
2631 	struct net_device *net_dev;
2632 	u32 fd_status, hash_offset;
2633 	struct qm_sg_entry *sgt;
2634 	struct dpaa_bp *dpaa_bp;
2635 	struct dpaa_fq *dpaa_fq;
2636 	struct dpaa_priv *priv;
2637 	struct sk_buff *skb;
2638 	int *count_ptr;
2639 	u32 xdp_act;
2640 	void *vaddr;
2641 	u32 hash;
2642 	u64 ns;
2643 
2644 	dpaa_fq = container_of(fq, struct dpaa_fq, fq_base);
2645 	fd_status = be32_to_cpu(fd->status);
2646 	fd_format = qm_fd_get_format(fd);
2647 	net_dev = dpaa_fq->net_dev;
2648 	priv = netdev_priv(net_dev);
2649 	dpaa_bp = dpaa_bpid2pool(dq->fd.bpid);
2650 	if (!dpaa_bp)
2651 		return qman_cb_dqrr_consume;
2652 
2653 	/* Trace the Rx fd */
2654 	trace_dpaa_rx_fd(net_dev, fq, &dq->fd);
2655 
2656 	percpu_priv = this_cpu_ptr(priv->percpu_priv);
2657 	percpu_stats = &percpu_priv->stats;
2658 	np = &percpu_priv->np;
2659 
2660 	if (unlikely(dpaa_eth_napi_schedule(percpu_priv, portal, sched_napi)))
2661 		return qman_cb_dqrr_stop;
2662 
2663 	/* Make sure we didn't run out of buffers */
2664 	if (unlikely(dpaa_eth_refill_bpools(priv))) {
2665 		/* Unable to refill the buffer pool due to insufficient
2666 		 * system memory. Just release the frame back into the pool,
2667 		 * otherwise we'll soon end up with an empty buffer pool.
2668 		 */
2669 		dpaa_fd_release(net_dev, &dq->fd);
2670 		return qman_cb_dqrr_consume;
2671 	}
2672 
2673 	if (unlikely(fd_status & FM_FD_STAT_RX_ERRORS) != 0) {
2674 		if (net_ratelimit())
2675 			netif_warn(priv, hw, net_dev, "FD status = 0x%08x\n",
2676 				   fd_status & FM_FD_STAT_RX_ERRORS);
2677 
2678 		percpu_stats->rx_errors++;
2679 		dpaa_fd_release(net_dev, fd);
2680 		return qman_cb_dqrr_consume;
2681 	}
2682 
2683 	dma_unmap_page(dpaa_bp->priv->rx_dma_dev, addr, DPAA_BP_RAW_SIZE,
2684 		       DMA_FROM_DEVICE);
2685 
2686 	/* prefetch the first 64 bytes of the frame or the SGT start */
2687 	vaddr = phys_to_virt(addr);
2688 	prefetch(vaddr + qm_fd_get_offset(fd));
2689 
2690 	/* The only FD types that we may receive are contig and S/G */
2691 	WARN_ON((fd_format != qm_fd_contig) && (fd_format != qm_fd_sg));
2692 
2693 	/* Account for either the contig buffer or the SGT buffer (depending on
2694 	 * which case we were in) having been removed from the pool.
2695 	 */
2696 	count_ptr = this_cpu_ptr(dpaa_bp->percpu_count);
2697 	(*count_ptr)--;
2698 
2699 	/* Extract the timestamp stored in the headroom before running XDP */
2700 	if (priv->rx_tstamp) {
2701 		if (!fman_port_get_tstamp(priv->mac_dev->port[RX], vaddr, &ns))
2702 			ts_valid = true;
2703 		else
2704 			WARN_ONCE(1, "fman_port_get_tstamp failed!\n");
2705 	}
2706 
2707 	/* Extract the hash stored in the headroom before running XDP */
2708 	if (net_dev->features & NETIF_F_RXHASH && priv->keygen_in_use &&
2709 	    !fman_port_get_hash_result_offset(priv->mac_dev->port[RX],
2710 					      &hash_offset)) {
2711 		hash = be32_to_cpu(*(u32 *)(vaddr + hash_offset));
2712 		hash_valid = true;
2713 	}
2714 
2715 	if (likely(fd_format == qm_fd_contig)) {
2716 		xdp_act = dpaa_run_xdp(priv, (struct qm_fd *)fd, vaddr,
2717 				       dpaa_fq, &xdp_meta_len);
2718 		np->xdp_act |= xdp_act;
2719 		if (xdp_act != XDP_PASS) {
2720 			percpu_stats->rx_packets++;
2721 			percpu_stats->rx_bytes += qm_fd_get_length(fd);
2722 			return qman_cb_dqrr_consume;
2723 		}
2724 		skb = contig_fd_to_skb(priv, fd);
2725 	} else {
2726 		/* XDP doesn't support S/G frames. Return the fragments to the
2727 		 * buffer pool and release the SGT.
2728 		 */
2729 		if (READ_ONCE(priv->xdp_prog)) {
2730 			WARN_ONCE(1, "S/G frames not supported under XDP\n");
2731 			sgt = vaddr + qm_fd_get_offset(fd);
2732 			dpaa_release_sgt_members(sgt);
2733 			free_pages((unsigned long)vaddr, 0);
2734 			return qman_cb_dqrr_consume;
2735 		}
2736 		skb = sg_fd_to_skb(priv, fd);
2737 	}
2738 	if (!skb)
2739 		return qman_cb_dqrr_consume;
2740 
2741 	if (xdp_meta_len)
2742 		skb_metadata_set(skb, xdp_meta_len);
2743 
2744 	/* Set the previously extracted timestamp */
2745 	if (ts_valid) {
2746 		shhwtstamps = skb_hwtstamps(skb);
2747 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2748 		shhwtstamps->hwtstamp = ns_to_ktime(ns);
2749 	}
2750 
2751 	skb->protocol = eth_type_trans(skb, net_dev);
2752 
2753 	/* Set the previously extracted hash */
2754 	if (hash_valid) {
2755 		enum pkt_hash_types type;
2756 
2757 		/* if L4 exists, it was used in the hash generation */
2758 		type = be32_to_cpu(fd->status) & FM_FD_STAT_L4CV ?
2759 			PKT_HASH_TYPE_L4 : PKT_HASH_TYPE_L3;
2760 		skb_set_hash(skb, hash, type);
2761 	}
2762 
2763 	skb_len = skb->len;
2764 
2765 	if (unlikely(netif_receive_skb(skb) == NET_RX_DROP)) {
2766 		percpu_stats->rx_dropped++;
2767 		return qman_cb_dqrr_consume;
2768 	}
2769 
2770 	percpu_stats->rx_packets++;
2771 	percpu_stats->rx_bytes += skb_len;
2772 
2773 	return qman_cb_dqrr_consume;
2774 }
2775 
2776 static enum qman_cb_dqrr_result conf_error_dqrr(struct qman_portal *portal,
2777 						struct qman_fq *fq,
2778 						const struct qm_dqrr_entry *dq,
2779 						bool sched_napi)
2780 {
2781 	struct dpaa_percpu_priv *percpu_priv;
2782 	struct net_device *net_dev;
2783 	struct dpaa_priv *priv;
2784 
2785 	net_dev = ((struct dpaa_fq *)fq)->net_dev;
2786 	priv = netdev_priv(net_dev);
2787 
2788 	percpu_priv = this_cpu_ptr(priv->percpu_priv);
2789 
2790 	if (dpaa_eth_napi_schedule(percpu_priv, portal, sched_napi))
2791 		return qman_cb_dqrr_stop;
2792 
2793 	dpaa_tx_error(net_dev, priv, percpu_priv, &dq->fd, fq->fqid);
2794 
2795 	return qman_cb_dqrr_consume;
2796 }
2797 
2798 static enum qman_cb_dqrr_result conf_dflt_dqrr(struct qman_portal *portal,
2799 					       struct qman_fq *fq,
2800 					       const struct qm_dqrr_entry *dq,
2801 					       bool sched_napi)
2802 {
2803 	struct dpaa_percpu_priv *percpu_priv;
2804 	struct net_device *net_dev;
2805 	struct dpaa_priv *priv;
2806 
2807 	net_dev = ((struct dpaa_fq *)fq)->net_dev;
2808 	priv = netdev_priv(net_dev);
2809 
2810 	/* Trace the fd */
2811 	trace_dpaa_tx_conf_fd(net_dev, fq, &dq->fd);
2812 
2813 	percpu_priv = this_cpu_ptr(priv->percpu_priv);
2814 
2815 	if (dpaa_eth_napi_schedule(percpu_priv, portal, sched_napi))
2816 		return qman_cb_dqrr_stop;
2817 
2818 	dpaa_tx_conf(net_dev, priv, percpu_priv, &dq->fd, fq->fqid);
2819 
2820 	return qman_cb_dqrr_consume;
2821 }
2822 
2823 static void egress_ern(struct qman_portal *portal,
2824 		       struct qman_fq *fq,
2825 		       const union qm_mr_entry *msg)
2826 {
2827 	const struct qm_fd *fd = &msg->ern.fd;
2828 	struct dpaa_percpu_priv *percpu_priv;
2829 	const struct dpaa_priv *priv;
2830 	struct net_device *net_dev;
2831 	struct sk_buff *skb;
2832 
2833 	net_dev = ((struct dpaa_fq *)fq)->net_dev;
2834 	priv = netdev_priv(net_dev);
2835 	percpu_priv = this_cpu_ptr(priv->percpu_priv);
2836 
2837 	percpu_priv->stats.tx_dropped++;
2838 	percpu_priv->stats.tx_fifo_errors++;
2839 	count_ern(percpu_priv, msg);
2840 
2841 	skb = dpaa_cleanup_tx_fd(priv, fd, false);
2842 	dev_kfree_skb_any(skb);
2843 }
2844 
2845 static const struct dpaa_fq_cbs dpaa_fq_cbs = {
2846 	.rx_defq = { .cb = { .dqrr = rx_default_dqrr } },
2847 	.tx_defq = { .cb = { .dqrr = conf_dflt_dqrr } },
2848 	.rx_errq = { .cb = { .dqrr = rx_error_dqrr } },
2849 	.tx_errq = { .cb = { .dqrr = conf_error_dqrr } },
2850 	.egress_ern = { .cb = { .ern = egress_ern } }
2851 };
2852 
2853 static void dpaa_eth_napi_enable(struct dpaa_priv *priv)
2854 {
2855 	struct dpaa_percpu_priv *percpu_priv;
2856 	int i;
2857 
2858 	for_each_online_cpu(i) {
2859 		percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
2860 
2861 		percpu_priv->np.down = false;
2862 		napi_enable(&percpu_priv->np.napi);
2863 	}
2864 }
2865 
2866 static void dpaa_eth_napi_disable(struct dpaa_priv *priv)
2867 {
2868 	struct dpaa_percpu_priv *percpu_priv;
2869 	int i;
2870 
2871 	for_each_online_cpu(i) {
2872 		percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
2873 
2874 		percpu_priv->np.down = true;
2875 		napi_disable(&percpu_priv->np.napi);
2876 	}
2877 }
2878 
2879 static void dpaa_adjust_link(struct net_device *net_dev)
2880 {
2881 	struct mac_device *mac_dev;
2882 	struct dpaa_priv *priv;
2883 
2884 	priv = netdev_priv(net_dev);
2885 	mac_dev = priv->mac_dev;
2886 	mac_dev->adjust_link(mac_dev);
2887 }
2888 
2889 /* The Aquantia PHYs are capable of performing rate adaptation */
2890 #define PHY_VEND_AQUANTIA	0x03a1b400
2891 #define PHY_VEND_AQUANTIA2	0x31c31c00
2892 
2893 static int dpaa_phy_init(struct net_device *net_dev)
2894 {
2895 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
2896 	struct mac_device *mac_dev;
2897 	struct phy_device *phy_dev;
2898 	struct dpaa_priv *priv;
2899 	u32 phy_vendor;
2900 
2901 	priv = netdev_priv(net_dev);
2902 	mac_dev = priv->mac_dev;
2903 
2904 	phy_dev = of_phy_connect(net_dev, mac_dev->phy_node,
2905 				 &dpaa_adjust_link, 0,
2906 				 mac_dev->phy_if);
2907 	if (!phy_dev) {
2908 		netif_err(priv, ifup, net_dev, "init_phy() failed\n");
2909 		return -ENODEV;
2910 	}
2911 
2912 	phy_vendor = phy_dev->drv->phy_id & GENMASK(31, 10);
2913 	/* Unless the PHY is capable of rate adaptation */
2914 	if (mac_dev->phy_if != PHY_INTERFACE_MODE_XGMII ||
2915 	    (phy_vendor != PHY_VEND_AQUANTIA &&
2916 	     phy_vendor != PHY_VEND_AQUANTIA2)) {
2917 		/* remove any features not supported by the controller */
2918 		ethtool_convert_legacy_u32_to_link_mode(mask,
2919 							mac_dev->if_support);
2920 		linkmode_and(phy_dev->supported, phy_dev->supported, mask);
2921 	}
2922 
2923 	phy_support_asym_pause(phy_dev);
2924 
2925 	mac_dev->phy_dev = phy_dev;
2926 	net_dev->phydev = phy_dev;
2927 
2928 	return 0;
2929 }
2930 
2931 static int dpaa_open(struct net_device *net_dev)
2932 {
2933 	struct mac_device *mac_dev;
2934 	struct dpaa_priv *priv;
2935 	int err, i;
2936 
2937 	priv = netdev_priv(net_dev);
2938 	mac_dev = priv->mac_dev;
2939 	dpaa_eth_napi_enable(priv);
2940 
2941 	err = dpaa_phy_init(net_dev);
2942 	if (err)
2943 		goto phy_init_failed;
2944 
2945 	for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++) {
2946 		err = fman_port_enable(mac_dev->port[i]);
2947 		if (err)
2948 			goto mac_start_failed;
2949 	}
2950 
2951 	err = priv->mac_dev->enable(mac_dev->fman_mac);
2952 	if (err < 0) {
2953 		netif_err(priv, ifup, net_dev, "mac_dev->enable() = %d\n", err);
2954 		goto mac_start_failed;
2955 	}
2956 	phy_start(priv->mac_dev->phy_dev);
2957 
2958 	netif_tx_start_all_queues(net_dev);
2959 
2960 	return 0;
2961 
2962 mac_start_failed:
2963 	for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++)
2964 		fman_port_disable(mac_dev->port[i]);
2965 
2966 phy_init_failed:
2967 	dpaa_eth_napi_disable(priv);
2968 
2969 	return err;
2970 }
2971 
2972 static int dpaa_eth_stop(struct net_device *net_dev)
2973 {
2974 	struct dpaa_priv *priv;
2975 	int err;
2976 
2977 	err = dpaa_stop(net_dev);
2978 
2979 	priv = netdev_priv(net_dev);
2980 	dpaa_eth_napi_disable(priv);
2981 
2982 	return err;
2983 }
2984 
2985 static bool xdp_validate_mtu(struct dpaa_priv *priv, int mtu)
2986 {
2987 	int max_contig_data = priv->dpaa_bp->size - priv->rx_headroom;
2988 
2989 	/* We do not support S/G fragments when XDP is enabled.
2990 	 * Limit the MTU in relation to the buffer size.
2991 	 */
2992 	if (mtu + VLAN_ETH_HLEN + ETH_FCS_LEN > max_contig_data) {
2993 		dev_warn(priv->net_dev->dev.parent,
2994 			 "The maximum MTU for XDP is %d\n",
2995 			 max_contig_data - VLAN_ETH_HLEN - ETH_FCS_LEN);
2996 		return false;
2997 	}
2998 
2999 	return true;
3000 }
3001 
3002 static int dpaa_change_mtu(struct net_device *net_dev, int new_mtu)
3003 {
3004 	struct dpaa_priv *priv = netdev_priv(net_dev);
3005 
3006 	if (priv->xdp_prog && !xdp_validate_mtu(priv, new_mtu))
3007 		return -EINVAL;
3008 
3009 	net_dev->mtu = new_mtu;
3010 	return 0;
3011 }
3012 
3013 static int dpaa_setup_xdp(struct net_device *net_dev, struct netdev_bpf *bpf)
3014 {
3015 	struct dpaa_priv *priv = netdev_priv(net_dev);
3016 	struct bpf_prog *old_prog;
3017 	int err;
3018 	bool up;
3019 
3020 	/* S/G fragments are not supported in XDP-mode */
3021 	if (bpf->prog && !xdp_validate_mtu(priv, net_dev->mtu)) {
3022 		NL_SET_ERR_MSG_MOD(bpf->extack, "MTU too large for XDP");
3023 		return -EINVAL;
3024 	}
3025 
3026 	up = netif_running(net_dev);
3027 
3028 	if (up)
3029 		dpaa_eth_stop(net_dev);
3030 
3031 	old_prog = xchg(&priv->xdp_prog, bpf->prog);
3032 	if (old_prog)
3033 		bpf_prog_put(old_prog);
3034 
3035 	if (up) {
3036 		err = dpaa_open(net_dev);
3037 		if (err) {
3038 			NL_SET_ERR_MSG_MOD(bpf->extack, "dpaa_open() failed");
3039 			return err;
3040 		}
3041 	}
3042 
3043 	return 0;
3044 }
3045 
3046 static int dpaa_xdp(struct net_device *net_dev, struct netdev_bpf *xdp)
3047 {
3048 	switch (xdp->command) {
3049 	case XDP_SETUP_PROG:
3050 		return dpaa_setup_xdp(net_dev, xdp);
3051 	default:
3052 		return -EINVAL;
3053 	}
3054 }
3055 
3056 static int dpaa_xdp_xmit(struct net_device *net_dev, int n,
3057 			 struct xdp_frame **frames, u32 flags)
3058 {
3059 	struct xdp_frame *xdpf;
3060 	int i, nxmit = 0;
3061 
3062 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
3063 		return -EINVAL;
3064 
3065 	if (!netif_running(net_dev))
3066 		return -ENETDOWN;
3067 
3068 	for (i = 0; i < n; i++) {
3069 		xdpf = frames[i];
3070 		if (dpaa_xdp_xmit_frame(net_dev, xdpf))
3071 			break;
3072 		nxmit++;
3073 	}
3074 
3075 	return nxmit;
3076 }
3077 
3078 static int dpaa_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3079 {
3080 	struct dpaa_priv *priv = netdev_priv(dev);
3081 	struct hwtstamp_config config;
3082 
3083 	if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
3084 		return -EFAULT;
3085 
3086 	switch (config.tx_type) {
3087 	case HWTSTAMP_TX_OFF:
3088 		/* Couldn't disable rx/tx timestamping separately.
3089 		 * Do nothing here.
3090 		 */
3091 		priv->tx_tstamp = false;
3092 		break;
3093 	case HWTSTAMP_TX_ON:
3094 		priv->mac_dev->set_tstamp(priv->mac_dev->fman_mac, true);
3095 		priv->tx_tstamp = true;
3096 		break;
3097 	default:
3098 		return -ERANGE;
3099 	}
3100 
3101 	if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
3102 		/* Couldn't disable rx/tx timestamping separately.
3103 		 * Do nothing here.
3104 		 */
3105 		priv->rx_tstamp = false;
3106 	} else {
3107 		priv->mac_dev->set_tstamp(priv->mac_dev->fman_mac, true);
3108 		priv->rx_tstamp = true;
3109 		/* TS is set for all frame types, not only those requested */
3110 		config.rx_filter = HWTSTAMP_FILTER_ALL;
3111 	}
3112 
3113 	return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
3114 			-EFAULT : 0;
3115 }
3116 
3117 static int dpaa_ioctl(struct net_device *net_dev, struct ifreq *rq, int cmd)
3118 {
3119 	int ret = -EINVAL;
3120 
3121 	if (cmd == SIOCGMIIREG) {
3122 		if (net_dev->phydev)
3123 			return phy_mii_ioctl(net_dev->phydev, rq, cmd);
3124 	}
3125 
3126 	if (cmd == SIOCSHWTSTAMP)
3127 		return dpaa_ts_ioctl(net_dev, rq, cmd);
3128 
3129 	return ret;
3130 }
3131 
3132 static const struct net_device_ops dpaa_ops = {
3133 	.ndo_open = dpaa_open,
3134 	.ndo_start_xmit = dpaa_start_xmit,
3135 	.ndo_stop = dpaa_eth_stop,
3136 	.ndo_tx_timeout = dpaa_tx_timeout,
3137 	.ndo_get_stats64 = dpaa_get_stats64,
3138 	.ndo_change_carrier = fixed_phy_change_carrier,
3139 	.ndo_set_mac_address = dpaa_set_mac_address,
3140 	.ndo_validate_addr = eth_validate_addr,
3141 	.ndo_set_rx_mode = dpaa_set_rx_mode,
3142 	.ndo_eth_ioctl = dpaa_ioctl,
3143 	.ndo_setup_tc = dpaa_setup_tc,
3144 	.ndo_change_mtu = dpaa_change_mtu,
3145 	.ndo_bpf = dpaa_xdp,
3146 	.ndo_xdp_xmit = dpaa_xdp_xmit,
3147 };
3148 
3149 static int dpaa_napi_add(struct net_device *net_dev)
3150 {
3151 	struct dpaa_priv *priv = netdev_priv(net_dev);
3152 	struct dpaa_percpu_priv *percpu_priv;
3153 	int cpu;
3154 
3155 	for_each_possible_cpu(cpu) {
3156 		percpu_priv = per_cpu_ptr(priv->percpu_priv, cpu);
3157 
3158 		netif_napi_add(net_dev, &percpu_priv->np.napi,
3159 			       dpaa_eth_poll, NAPI_POLL_WEIGHT);
3160 	}
3161 
3162 	return 0;
3163 }
3164 
3165 static void dpaa_napi_del(struct net_device *net_dev)
3166 {
3167 	struct dpaa_priv *priv = netdev_priv(net_dev);
3168 	struct dpaa_percpu_priv *percpu_priv;
3169 	int cpu;
3170 
3171 	for_each_possible_cpu(cpu) {
3172 		percpu_priv = per_cpu_ptr(priv->percpu_priv, cpu);
3173 
3174 		netif_napi_del(&percpu_priv->np.napi);
3175 	}
3176 }
3177 
3178 static inline void dpaa_bp_free_pf(const struct dpaa_bp *bp,
3179 				   struct bm_buffer *bmb)
3180 {
3181 	dma_addr_t addr = bm_buf_addr(bmb);
3182 
3183 	dma_unmap_page(bp->priv->rx_dma_dev, addr, DPAA_BP_RAW_SIZE,
3184 		       DMA_FROM_DEVICE);
3185 
3186 	skb_free_frag(phys_to_virt(addr));
3187 }
3188 
3189 /* Alloc the dpaa_bp struct and configure default values */
3190 static struct dpaa_bp *dpaa_bp_alloc(struct device *dev)
3191 {
3192 	struct dpaa_bp *dpaa_bp;
3193 
3194 	dpaa_bp = devm_kzalloc(dev, sizeof(*dpaa_bp), GFP_KERNEL);
3195 	if (!dpaa_bp)
3196 		return ERR_PTR(-ENOMEM);
3197 
3198 	dpaa_bp->bpid = FSL_DPAA_BPID_INV;
3199 	dpaa_bp->percpu_count = devm_alloc_percpu(dev, *dpaa_bp->percpu_count);
3200 	if (!dpaa_bp->percpu_count)
3201 		return ERR_PTR(-ENOMEM);
3202 
3203 	dpaa_bp->config_count = FSL_DPAA_ETH_MAX_BUF_COUNT;
3204 
3205 	dpaa_bp->seed_cb = dpaa_bp_seed;
3206 	dpaa_bp->free_buf_cb = dpaa_bp_free_pf;
3207 
3208 	return dpaa_bp;
3209 }
3210 
3211 /* Place all ingress FQs (Rx Default, Rx Error) in a dedicated CGR.
3212  * We won't be sending congestion notifications to FMan; for now, we just use
3213  * this CGR to generate enqueue rejections to FMan in order to drop the frames
3214  * before they reach our ingress queues and eat up memory.
3215  */
3216 static int dpaa_ingress_cgr_init(struct dpaa_priv *priv)
3217 {
3218 	struct qm_mcc_initcgr initcgr;
3219 	u32 cs_th;
3220 	int err;
3221 
3222 	err = qman_alloc_cgrid(&priv->ingress_cgr.cgrid);
3223 	if (err < 0) {
3224 		if (netif_msg_drv(priv))
3225 			pr_err("Error %d allocating CGR ID\n", err);
3226 		goto out_error;
3227 	}
3228 
3229 	/* Enable CS TD, but disable Congestion State Change Notifications. */
3230 	memset(&initcgr, 0, sizeof(initcgr));
3231 	initcgr.we_mask = cpu_to_be16(QM_CGR_WE_CS_THRES);
3232 	initcgr.cgr.cscn_en = QM_CGR_EN;
3233 	cs_th = DPAA_INGRESS_CS_THRESHOLD;
3234 	qm_cgr_cs_thres_set64(&initcgr.cgr.cs_thres, cs_th, 1);
3235 
3236 	initcgr.we_mask |= cpu_to_be16(QM_CGR_WE_CSTD_EN);
3237 	initcgr.cgr.cstd_en = QM_CGR_EN;
3238 
3239 	/* This CGR will be associated with the SWP affined to the current CPU.
3240 	 * However, we'll place all our ingress FQs in it.
3241 	 */
3242 	err = qman_create_cgr(&priv->ingress_cgr, QMAN_CGR_FLAG_USE_INIT,
3243 			      &initcgr);
3244 	if (err < 0) {
3245 		if (netif_msg_drv(priv))
3246 			pr_err("Error %d creating ingress CGR with ID %d\n",
3247 			       err, priv->ingress_cgr.cgrid);
3248 		qman_release_cgrid(priv->ingress_cgr.cgrid);
3249 		goto out_error;
3250 	}
3251 	if (netif_msg_drv(priv))
3252 		pr_debug("Created ingress CGR %d for netdev with hwaddr %pM\n",
3253 			 priv->ingress_cgr.cgrid, priv->mac_dev->addr);
3254 
3255 	priv->use_ingress_cgr = true;
3256 
3257 out_error:
3258 	return err;
3259 }
3260 
3261 static u16 dpaa_get_headroom(struct dpaa_buffer_layout *bl,
3262 			     enum port_type port)
3263 {
3264 	u16 headroom;
3265 
3266 	/* The frame headroom must accommodate:
3267 	 * - the driver private data area
3268 	 * - parse results, hash results, timestamp if selected
3269 	 * If either hash results or time stamp are selected, both will
3270 	 * be copied to/from the frame headroom, as TS is located between PR and
3271 	 * HR in the IC and IC copy size has a granularity of 16bytes
3272 	 * (see description of FMBM_RICP and FMBM_TICP registers in DPAARM)
3273 	 *
3274 	 * Also make sure the headroom is a multiple of data_align bytes
3275 	 */
3276 	headroom = (u16)(bl[port].priv_data_size + DPAA_HWA_SIZE);
3277 
3278 	if (port == RX) {
3279 #ifdef CONFIG_DPAA_ERRATUM_A050385
3280 		if (unlikely(fman_has_errata_a050385()))
3281 			headroom = XDP_PACKET_HEADROOM;
3282 #endif
3283 
3284 		return ALIGN(headroom, DPAA_FD_RX_DATA_ALIGNMENT);
3285 	} else {
3286 		return ALIGN(headroom, DPAA_FD_DATA_ALIGNMENT);
3287 	}
3288 }
3289 
3290 static int dpaa_eth_probe(struct platform_device *pdev)
3291 {
3292 	struct net_device *net_dev = NULL;
3293 	struct dpaa_bp *dpaa_bp = NULL;
3294 	struct dpaa_fq *dpaa_fq, *tmp;
3295 	struct dpaa_priv *priv = NULL;
3296 	struct fm_port_fqs port_fqs;
3297 	struct mac_device *mac_dev;
3298 	int err = 0, channel;
3299 	struct device *dev;
3300 
3301 	dev = &pdev->dev;
3302 
3303 	err = bman_is_probed();
3304 	if (!err)
3305 		return -EPROBE_DEFER;
3306 	if (err < 0) {
3307 		dev_err(dev, "failing probe due to bman probe error\n");
3308 		return -ENODEV;
3309 	}
3310 	err = qman_is_probed();
3311 	if (!err)
3312 		return -EPROBE_DEFER;
3313 	if (err < 0) {
3314 		dev_err(dev, "failing probe due to qman probe error\n");
3315 		return -ENODEV;
3316 	}
3317 	err = bman_portals_probed();
3318 	if (!err)
3319 		return -EPROBE_DEFER;
3320 	if (err < 0) {
3321 		dev_err(dev,
3322 			"failing probe due to bman portals probe error\n");
3323 		return -ENODEV;
3324 	}
3325 	err = qman_portals_probed();
3326 	if (!err)
3327 		return -EPROBE_DEFER;
3328 	if (err < 0) {
3329 		dev_err(dev,
3330 			"failing probe due to qman portals probe error\n");
3331 		return -ENODEV;
3332 	}
3333 
3334 	/* Allocate this early, so we can store relevant information in
3335 	 * the private area
3336 	 */
3337 	net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA_ETH_TXQ_NUM);
3338 	if (!net_dev) {
3339 		dev_err(dev, "alloc_etherdev_mq() failed\n");
3340 		return -ENOMEM;
3341 	}
3342 
3343 	/* Do this here, so we can be verbose early */
3344 	SET_NETDEV_DEV(net_dev, dev->parent);
3345 	dev_set_drvdata(dev, net_dev);
3346 
3347 	priv = netdev_priv(net_dev);
3348 	priv->net_dev = net_dev;
3349 
3350 	priv->msg_enable = netif_msg_init(debug, DPAA_MSG_DEFAULT);
3351 
3352 	mac_dev = dpaa_mac_dev_get(pdev);
3353 	if (IS_ERR(mac_dev)) {
3354 		netdev_err(net_dev, "dpaa_mac_dev_get() failed\n");
3355 		err = PTR_ERR(mac_dev);
3356 		goto free_netdev;
3357 	}
3358 
3359 	/* Devices used for DMA mapping */
3360 	priv->rx_dma_dev = fman_port_get_device(mac_dev->port[RX]);
3361 	priv->tx_dma_dev = fman_port_get_device(mac_dev->port[TX]);
3362 	err = dma_coerce_mask_and_coherent(priv->rx_dma_dev, DMA_BIT_MASK(40));
3363 	if (!err)
3364 		err = dma_coerce_mask_and_coherent(priv->tx_dma_dev,
3365 						   DMA_BIT_MASK(40));
3366 	if (err) {
3367 		netdev_err(net_dev, "dma_coerce_mask_and_coherent() failed\n");
3368 		goto free_netdev;
3369 	}
3370 
3371 	/* If fsl_fm_max_frm is set to a higher value than the all-common 1500,
3372 	 * we choose conservatively and let the user explicitly set a higher
3373 	 * MTU via ifconfig. Otherwise, the user may end up with different MTUs
3374 	 * in the same LAN.
3375 	 * If on the other hand fsl_fm_max_frm has been chosen below 1500,
3376 	 * start with the maximum allowed.
3377 	 */
3378 	net_dev->mtu = min(dpaa_get_max_mtu(), ETH_DATA_LEN);
3379 
3380 	netdev_dbg(net_dev, "Setting initial MTU on net device: %d\n",
3381 		   net_dev->mtu);
3382 
3383 	priv->buf_layout[RX].priv_data_size = DPAA_RX_PRIV_DATA_SIZE; /* Rx */
3384 	priv->buf_layout[TX].priv_data_size = DPAA_TX_PRIV_DATA_SIZE; /* Tx */
3385 
3386 	/* bp init */
3387 	dpaa_bp = dpaa_bp_alloc(dev);
3388 	if (IS_ERR(dpaa_bp)) {
3389 		err = PTR_ERR(dpaa_bp);
3390 		goto free_dpaa_bps;
3391 	}
3392 	/* the raw size of the buffers used for reception */
3393 	dpaa_bp->raw_size = DPAA_BP_RAW_SIZE;
3394 	/* avoid runtime computations by keeping the usable size here */
3395 	dpaa_bp->size = dpaa_bp_size(dpaa_bp->raw_size);
3396 	dpaa_bp->priv = priv;
3397 
3398 	err = dpaa_bp_alloc_pool(dpaa_bp);
3399 	if (err < 0)
3400 		goto free_dpaa_bps;
3401 	priv->dpaa_bp = dpaa_bp;
3402 
3403 	INIT_LIST_HEAD(&priv->dpaa_fq_list);
3404 
3405 	memset(&port_fqs, 0, sizeof(port_fqs));
3406 
3407 	err = dpaa_alloc_all_fqs(dev, &priv->dpaa_fq_list, &port_fqs);
3408 	if (err < 0) {
3409 		dev_err(dev, "dpaa_alloc_all_fqs() failed\n");
3410 		goto free_dpaa_bps;
3411 	}
3412 
3413 	priv->mac_dev = mac_dev;
3414 
3415 	channel = dpaa_get_channel();
3416 	if (channel < 0) {
3417 		dev_err(dev, "dpaa_get_channel() failed\n");
3418 		err = channel;
3419 		goto free_dpaa_bps;
3420 	}
3421 
3422 	priv->channel = (u16)channel;
3423 
3424 	/* Walk the CPUs with affine portals
3425 	 * and add this pool channel to each's dequeue mask.
3426 	 */
3427 	dpaa_eth_add_channel(priv->channel, &pdev->dev);
3428 
3429 	dpaa_fq_setup(priv, &dpaa_fq_cbs, priv->mac_dev->port[TX]);
3430 
3431 	/* Create a congestion group for this netdev, with
3432 	 * dynamically-allocated CGR ID.
3433 	 * Must be executed after probing the MAC, but before
3434 	 * assigning the egress FQs to the CGRs.
3435 	 */
3436 	err = dpaa_eth_cgr_init(priv);
3437 	if (err < 0) {
3438 		dev_err(dev, "Error initializing CGR\n");
3439 		goto free_dpaa_bps;
3440 	}
3441 
3442 	err = dpaa_ingress_cgr_init(priv);
3443 	if (err < 0) {
3444 		dev_err(dev, "Error initializing ingress CGR\n");
3445 		goto delete_egress_cgr;
3446 	}
3447 
3448 	/* Add the FQs to the interface, and make them active */
3449 	list_for_each_entry_safe(dpaa_fq, tmp, &priv->dpaa_fq_list, list) {
3450 		err = dpaa_fq_init(dpaa_fq, false);
3451 		if (err < 0)
3452 			goto free_dpaa_fqs;
3453 	}
3454 
3455 	priv->tx_headroom = dpaa_get_headroom(priv->buf_layout, TX);
3456 	priv->rx_headroom = dpaa_get_headroom(priv->buf_layout, RX);
3457 
3458 	/* All real interfaces need their ports initialized */
3459 	err = dpaa_eth_init_ports(mac_dev, dpaa_bp, &port_fqs,
3460 				  &priv->buf_layout[0], dev);
3461 	if (err)
3462 		goto free_dpaa_fqs;
3463 
3464 	/* Rx traffic distribution based on keygen hashing defaults to on */
3465 	priv->keygen_in_use = true;
3466 
3467 	priv->percpu_priv = devm_alloc_percpu(dev, *priv->percpu_priv);
3468 	if (!priv->percpu_priv) {
3469 		dev_err(dev, "devm_alloc_percpu() failed\n");
3470 		err = -ENOMEM;
3471 		goto free_dpaa_fqs;
3472 	}
3473 
3474 	priv->num_tc = 1;
3475 	netif_set_real_num_tx_queues(net_dev, priv->num_tc * DPAA_TC_TXQ_NUM);
3476 
3477 	/* Initialize NAPI */
3478 	err = dpaa_napi_add(net_dev);
3479 	if (err < 0)
3480 		goto delete_dpaa_napi;
3481 
3482 	err = dpaa_netdev_init(net_dev, &dpaa_ops, tx_timeout);
3483 	if (err < 0)
3484 		goto delete_dpaa_napi;
3485 
3486 	dpaa_eth_sysfs_init(&net_dev->dev);
3487 
3488 	netif_info(priv, probe, net_dev, "Probed interface %s\n",
3489 		   net_dev->name);
3490 
3491 	return 0;
3492 
3493 delete_dpaa_napi:
3494 	dpaa_napi_del(net_dev);
3495 free_dpaa_fqs:
3496 	dpaa_fq_free(dev, &priv->dpaa_fq_list);
3497 	qman_delete_cgr_safe(&priv->ingress_cgr);
3498 	qman_release_cgrid(priv->ingress_cgr.cgrid);
3499 delete_egress_cgr:
3500 	qman_delete_cgr_safe(&priv->cgr_data.cgr);
3501 	qman_release_cgrid(priv->cgr_data.cgr.cgrid);
3502 free_dpaa_bps:
3503 	dpaa_bps_free(priv);
3504 free_netdev:
3505 	dev_set_drvdata(dev, NULL);
3506 	free_netdev(net_dev);
3507 
3508 	return err;
3509 }
3510 
3511 static int dpaa_remove(struct platform_device *pdev)
3512 {
3513 	struct net_device *net_dev;
3514 	struct dpaa_priv *priv;
3515 	struct device *dev;
3516 	int err;
3517 
3518 	dev = &pdev->dev;
3519 	net_dev = dev_get_drvdata(dev);
3520 
3521 	priv = netdev_priv(net_dev);
3522 
3523 	dpaa_eth_sysfs_remove(dev);
3524 
3525 	dev_set_drvdata(dev, NULL);
3526 	unregister_netdev(net_dev);
3527 
3528 	err = dpaa_fq_free(dev, &priv->dpaa_fq_list);
3529 
3530 	qman_delete_cgr_safe(&priv->ingress_cgr);
3531 	qman_release_cgrid(priv->ingress_cgr.cgrid);
3532 	qman_delete_cgr_safe(&priv->cgr_data.cgr);
3533 	qman_release_cgrid(priv->cgr_data.cgr.cgrid);
3534 
3535 	dpaa_napi_del(net_dev);
3536 
3537 	dpaa_bps_free(priv);
3538 
3539 	free_netdev(net_dev);
3540 
3541 	return err;
3542 }
3543 
3544 static const struct platform_device_id dpaa_devtype[] = {
3545 	{
3546 		.name = "dpaa-ethernet",
3547 		.driver_data = 0,
3548 	}, {
3549 	}
3550 };
3551 MODULE_DEVICE_TABLE(platform, dpaa_devtype);
3552 
3553 static struct platform_driver dpaa_driver = {
3554 	.driver = {
3555 		.name = KBUILD_MODNAME,
3556 	},
3557 	.id_table = dpaa_devtype,
3558 	.probe = dpaa_eth_probe,
3559 	.remove = dpaa_remove
3560 };
3561 
3562 static int __init dpaa_load(void)
3563 {
3564 	int err;
3565 
3566 	pr_debug("FSL DPAA Ethernet driver\n");
3567 
3568 	/* initialize dpaa_eth mirror values */
3569 	dpaa_rx_extra_headroom = fman_get_rx_extra_headroom();
3570 	dpaa_max_frm = fman_get_max_frm();
3571 
3572 	err = platform_driver_register(&dpaa_driver);
3573 	if (err < 0)
3574 		pr_err("Error, platform_driver_register() = %d\n", err);
3575 
3576 	return err;
3577 }
3578 module_init(dpaa_load);
3579 
3580 static void __exit dpaa_unload(void)
3581 {
3582 	platform_driver_unregister(&dpaa_driver);
3583 
3584 	/* Only one channel is used and needs to be released after all
3585 	 * interfaces are removed
3586 	 */
3587 	dpaa_release_channel();
3588 }
3589 module_exit(dpaa_unload);
3590 
3591 MODULE_LICENSE("Dual BSD/GPL");
3592 MODULE_DESCRIPTION("FSL DPAA Ethernet driver");
3593