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