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