xref: /linux/drivers/net/ethernet/freescale/dpaa/dpaa_eth.c (revision f339bd3b51dac675fbbc08b861d2371ae3df0c0b)
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 	struct rtnl_link_stats64 *percpu_stats;
2276 	struct dpaa_percpu_priv *percpu_priv;
2277 	struct netdev_queue *txq;
2278 	struct dpaa_priv *priv;
2279 	struct qm_fd fd;
2280 	bool nonlinear;
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 	/* Packet data is always read as 32-bit words, so zero out any part of
2291 	 * the skb which might be sent if we have to pad the packet
2292 	 */
2293 	if (__skb_put_padto(skb, ETH_ZLEN, false))
2294 		goto enomem;
2295 
2296 	nonlinear = skb_is_nonlinear(skb);
2297 	if (!nonlinear) {
2298 		/* We're going to store the skb backpointer at the beginning
2299 		 * of the data buffer, so we need a privately owned skb
2300 		 *
2301 		 * We've made sure skb is not shared in dev->priv_flags,
2302 		 * we need to verify the skb head is not cloned
2303 		 */
2304 		if (skb_cow_head(skb, priv->tx_headroom))
2305 			goto enomem;
2306 
2307 		WARN_ON(skb_is_nonlinear(skb));
2308 	}
2309 
2310 	/* MAX_SKB_FRAGS is equal or larger than our dpaa_SGT_MAX_ENTRIES;
2311 	 * make sure we don't feed FMan with more fragments than it supports.
2312 	 */
2313 	if (unlikely(nonlinear &&
2314 		     (skb_shinfo(skb)->nr_frags >= DPAA_SGT_MAX_ENTRIES))) {
2315 		/* If the egress skb contains more fragments than we support
2316 		 * we have no choice but to linearize it ourselves.
2317 		 */
2318 		if (__skb_linearize(skb))
2319 			goto enomem;
2320 
2321 		nonlinear = skb_is_nonlinear(skb);
2322 	}
2323 
2324 #ifdef CONFIG_DPAA_ERRATUM_A050385
2325 	if (unlikely(fman_has_errata_a050385())) {
2326 		if (dpaa_a050385_wa_skb(net_dev, &skb))
2327 			goto enomem;
2328 		nonlinear = skb_is_nonlinear(skb);
2329 	}
2330 #endif
2331 
2332 	if (nonlinear) {
2333 		/* Just create a S/G fd based on the skb */
2334 		err = skb_to_sg_fd(priv, skb, &fd);
2335 		percpu_priv->tx_frag_skbuffs++;
2336 	} else {
2337 		/* Create a contig FD from this skb */
2338 		err = skb_to_contig_fd(priv, skb, &fd, &offset);
2339 	}
2340 	if (unlikely(err < 0))
2341 		goto skb_to_fd_failed;
2342 
2343 	txq = netdev_get_tx_queue(net_dev, queue_mapping);
2344 
2345 	/* LLTX requires to do our own update of trans_start */
2346 	txq_trans_cond_update(txq);
2347 
2348 	if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
2349 		fd.cmd |= cpu_to_be32(FM_FD_CMD_UPD);
2350 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2351 	}
2352 
2353 	if (likely(dpaa_xmit(priv, percpu_stats, queue_mapping, &fd) == 0))
2354 		return NETDEV_TX_OK;
2355 
2356 	dpaa_cleanup_tx_fd(priv, &fd, false);
2357 skb_to_fd_failed:
2358 enomem:
2359 	percpu_stats->tx_errors++;
2360 	dev_kfree_skb(skb);
2361 	return NETDEV_TX_OK;
2362 }
2363 
2364 static void dpaa_rx_error(struct net_device *net_dev,
2365 			  const struct dpaa_priv *priv,
2366 			  struct dpaa_percpu_priv *percpu_priv,
2367 			  const struct qm_fd *fd,
2368 			  u32 fqid)
2369 {
2370 	if (net_ratelimit())
2371 		netif_err(priv, hw, net_dev, "Err FD status = 0x%08x\n",
2372 			  be32_to_cpu(fd->status) & FM_FD_STAT_RX_ERRORS);
2373 
2374 	percpu_priv->stats.rx_errors++;
2375 
2376 	if (be32_to_cpu(fd->status) & FM_FD_ERR_DMA)
2377 		percpu_priv->rx_errors.dme++;
2378 	if (be32_to_cpu(fd->status) & FM_FD_ERR_PHYSICAL)
2379 		percpu_priv->rx_errors.fpe++;
2380 	if (be32_to_cpu(fd->status) & FM_FD_ERR_SIZE)
2381 		percpu_priv->rx_errors.fse++;
2382 	if (be32_to_cpu(fd->status) & FM_FD_ERR_PRS_HDR_ERR)
2383 		percpu_priv->rx_errors.phe++;
2384 
2385 	dpaa_fd_release(net_dev, fd);
2386 }
2387 
2388 static void dpaa_tx_error(struct net_device *net_dev,
2389 			  const struct dpaa_priv *priv,
2390 			  struct dpaa_percpu_priv *percpu_priv,
2391 			  const struct qm_fd *fd,
2392 			  u32 fqid)
2393 {
2394 	struct sk_buff *skb;
2395 
2396 	if (net_ratelimit())
2397 		netif_warn(priv, hw, net_dev, "FD status = 0x%08x\n",
2398 			   be32_to_cpu(fd->status) & FM_FD_STAT_TX_ERRORS);
2399 
2400 	percpu_priv->stats.tx_errors++;
2401 
2402 	skb = dpaa_cleanup_tx_fd(priv, fd, false);
2403 	dev_kfree_skb(skb);
2404 }
2405 
2406 static int dpaa_eth_poll(struct napi_struct *napi, int budget)
2407 {
2408 	struct dpaa_napi_portal *np =
2409 			container_of(napi, struct dpaa_napi_portal, napi);
2410 	int cleaned;
2411 
2412 	np->xdp_act = 0;
2413 
2414 	cleaned = qman_p_poll_dqrr(np->p, budget);
2415 
2416 	if (np->xdp_act & XDP_REDIRECT)
2417 		xdp_do_flush();
2418 
2419 	if (cleaned < budget) {
2420 		napi_complete_done(napi, cleaned);
2421 		qman_p_irqsource_add(np->p, QM_PIRQ_DQRI);
2422 	} else if (np->down) {
2423 		qman_p_irqsource_add(np->p, QM_PIRQ_DQRI);
2424 	}
2425 
2426 	return cleaned;
2427 }
2428 
2429 static void dpaa_tx_conf(struct net_device *net_dev,
2430 			 const struct dpaa_priv *priv,
2431 			 struct dpaa_percpu_priv *percpu_priv,
2432 			 const struct qm_fd *fd,
2433 			 u32 fqid)
2434 {
2435 	struct sk_buff	*skb;
2436 
2437 	if (unlikely(be32_to_cpu(fd->status) & FM_FD_STAT_TX_ERRORS)) {
2438 		if (net_ratelimit())
2439 			netif_warn(priv, hw, net_dev, "FD status = 0x%08x\n",
2440 				   be32_to_cpu(fd->status) &
2441 				   FM_FD_STAT_TX_ERRORS);
2442 
2443 		percpu_priv->stats.tx_errors++;
2444 	}
2445 
2446 	percpu_priv->tx_confirm++;
2447 
2448 	skb = dpaa_cleanup_tx_fd(priv, fd, true);
2449 
2450 	consume_skb(skb);
2451 }
2452 
2453 static inline int dpaa_eth_napi_schedule(struct dpaa_percpu_priv *percpu_priv,
2454 					 struct qman_portal *portal, bool sched_napi)
2455 {
2456 	if (sched_napi) {
2457 		/* Disable QMan IRQ and invoke NAPI */
2458 		qman_p_irqsource_remove(portal, QM_PIRQ_DQRI);
2459 
2460 		percpu_priv->np.p = portal;
2461 		napi_schedule(&percpu_priv->np.napi);
2462 		percpu_priv->in_interrupt++;
2463 		return 1;
2464 	}
2465 	return 0;
2466 }
2467 
2468 static enum qman_cb_dqrr_result rx_error_dqrr(struct qman_portal *portal,
2469 					      struct qman_fq *fq,
2470 					      const struct qm_dqrr_entry *dq,
2471 					      bool sched_napi)
2472 {
2473 	struct dpaa_fq *dpaa_fq = container_of(fq, struct dpaa_fq, fq_base);
2474 	struct dpaa_percpu_priv *percpu_priv;
2475 	struct net_device *net_dev;
2476 	struct dpaa_bp *dpaa_bp;
2477 	struct dpaa_priv *priv;
2478 
2479 	net_dev = dpaa_fq->net_dev;
2480 	priv = netdev_priv(net_dev);
2481 	dpaa_bp = dpaa_bpid2pool(dq->fd.bpid);
2482 	if (!dpaa_bp)
2483 		return qman_cb_dqrr_consume;
2484 
2485 	percpu_priv = this_cpu_ptr(priv->percpu_priv);
2486 
2487 	if (dpaa_eth_napi_schedule(percpu_priv, portal, sched_napi))
2488 		return qman_cb_dqrr_stop;
2489 
2490 	dpaa_eth_refill_bpools(priv);
2491 	dpaa_rx_error(net_dev, priv, percpu_priv, &dq->fd, fq->fqid);
2492 
2493 	return qman_cb_dqrr_consume;
2494 }
2495 
2496 static int dpaa_xdp_xmit_frame(struct net_device *net_dev,
2497 			       struct xdp_frame *xdpf)
2498 {
2499 	struct dpaa_priv *priv = netdev_priv(net_dev);
2500 	struct rtnl_link_stats64 *percpu_stats;
2501 	struct dpaa_percpu_priv *percpu_priv;
2502 	struct dpaa_eth_swbp *swbp;
2503 	struct netdev_queue *txq;
2504 	void *buff_start;
2505 	struct qm_fd fd;
2506 	dma_addr_t addr;
2507 	int err;
2508 
2509 	percpu_priv = this_cpu_ptr(priv->percpu_priv);
2510 	percpu_stats = &percpu_priv->stats;
2511 
2512 #ifdef CONFIG_DPAA_ERRATUM_A050385
2513 	if (unlikely(fman_has_errata_a050385())) {
2514 		if (dpaa_a050385_wa_xdpf(priv, &xdpf)) {
2515 			err = -ENOMEM;
2516 			goto out_error;
2517 		}
2518 	}
2519 #endif
2520 
2521 	if (xdpf->headroom < DPAA_TX_PRIV_DATA_SIZE) {
2522 		err = -EINVAL;
2523 		goto out_error;
2524 	}
2525 
2526 	buff_start = xdpf->data - xdpf->headroom;
2527 
2528 	/* Leave empty the skb backpointer at the start of the buffer.
2529 	 * Save the XDP frame for easy cleanup on confirmation.
2530 	 */
2531 	swbp = (struct dpaa_eth_swbp *)buff_start;
2532 	swbp->skb = NULL;
2533 	swbp->xdpf = xdpf;
2534 
2535 	qm_fd_clear_fd(&fd);
2536 	fd.bpid = FSL_DPAA_BPID_INV;
2537 	fd.cmd |= cpu_to_be32(FM_FD_CMD_FCO);
2538 	qm_fd_set_contig(&fd, xdpf->headroom, xdpf->len);
2539 
2540 	addr = dma_map_single(priv->tx_dma_dev, buff_start,
2541 			      xdpf->headroom + xdpf->len,
2542 			      DMA_TO_DEVICE);
2543 	if (unlikely(dma_mapping_error(priv->tx_dma_dev, addr))) {
2544 		err = -EINVAL;
2545 		goto out_error;
2546 	}
2547 
2548 	qm_fd_addr_set64(&fd, addr);
2549 
2550 	/* Bump the trans_start */
2551 	txq = netdev_get_tx_queue(net_dev, smp_processor_id());
2552 	txq_trans_cond_update(txq);
2553 
2554 	err = dpaa_xmit(priv, percpu_stats, smp_processor_id(), &fd);
2555 	if (err) {
2556 		dma_unmap_single(priv->tx_dma_dev, addr,
2557 				 qm_fd_get_offset(&fd) + qm_fd_get_length(&fd),
2558 				 DMA_TO_DEVICE);
2559 		goto out_error;
2560 	}
2561 
2562 	return 0;
2563 
2564 out_error:
2565 	percpu_stats->tx_errors++;
2566 	return err;
2567 }
2568 
2569 static u32 dpaa_run_xdp(struct dpaa_priv *priv, struct qm_fd *fd, void *vaddr,
2570 			struct dpaa_fq *dpaa_fq, unsigned int *xdp_meta_len)
2571 {
2572 	ssize_t fd_off = qm_fd_get_offset(fd);
2573 	struct bpf_prog *xdp_prog;
2574 	struct xdp_frame *xdpf;
2575 	struct xdp_buff xdp;
2576 	u32 xdp_act;
2577 	int err;
2578 
2579 	xdp_prog = READ_ONCE(priv->xdp_prog);
2580 	if (!xdp_prog)
2581 		return XDP_PASS;
2582 
2583 	xdp_init_buff(&xdp, DPAA_BP_RAW_SIZE - DPAA_TX_PRIV_DATA_SIZE,
2584 		      &dpaa_fq->xdp_rxq);
2585 	xdp_prepare_buff(&xdp, vaddr + fd_off - XDP_PACKET_HEADROOM,
2586 			 XDP_PACKET_HEADROOM, qm_fd_get_length(fd), true);
2587 
2588 	/* We reserve a fixed headroom of 256 bytes under the erratum and we
2589 	 * offer it all to XDP programs to use. If no room is left for the
2590 	 * xdpf backpointer on TX, we will need to copy the data.
2591 	 * Disable metadata support since data realignments might be required
2592 	 * and the information can be lost.
2593 	 */
2594 #ifdef CONFIG_DPAA_ERRATUM_A050385
2595 	if (unlikely(fman_has_errata_a050385())) {
2596 		xdp_set_data_meta_invalid(&xdp);
2597 		xdp.data_hard_start = vaddr;
2598 		xdp.frame_sz = DPAA_BP_RAW_SIZE;
2599 	}
2600 #endif
2601 
2602 	xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
2603 
2604 	/* Update the length and the offset of the FD */
2605 	qm_fd_set_contig(fd, xdp.data - vaddr, xdp.data_end - xdp.data);
2606 
2607 	switch (xdp_act) {
2608 	case XDP_PASS:
2609 #ifdef CONFIG_DPAA_ERRATUM_A050385
2610 		*xdp_meta_len = xdp_data_meta_unsupported(&xdp) ? 0 :
2611 				xdp.data - xdp.data_meta;
2612 #else
2613 		*xdp_meta_len = xdp.data - xdp.data_meta;
2614 #endif
2615 		break;
2616 	case XDP_TX:
2617 		/* We can access the full headroom when sending the frame
2618 		 * back out
2619 		 */
2620 		xdp.data_hard_start = vaddr;
2621 		xdp.frame_sz = DPAA_BP_RAW_SIZE;
2622 		xdpf = xdp_convert_buff_to_frame(&xdp);
2623 		if (unlikely(!xdpf)) {
2624 			free_pages((unsigned long)vaddr, 0);
2625 			break;
2626 		}
2627 
2628 		if (dpaa_xdp_xmit_frame(priv->net_dev, xdpf))
2629 			xdp_return_frame_rx_napi(xdpf);
2630 
2631 		break;
2632 	case XDP_REDIRECT:
2633 		/* Allow redirect to use the full headroom */
2634 		xdp.data_hard_start = vaddr;
2635 		xdp.frame_sz = DPAA_BP_RAW_SIZE;
2636 
2637 		err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog);
2638 		if (err) {
2639 			trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
2640 			free_pages((unsigned long)vaddr, 0);
2641 		}
2642 		break;
2643 	default:
2644 		bpf_warn_invalid_xdp_action(priv->net_dev, xdp_prog, xdp_act);
2645 		fallthrough;
2646 	case XDP_ABORTED:
2647 		trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
2648 		fallthrough;
2649 	case XDP_DROP:
2650 		/* Free the buffer */
2651 		free_pages((unsigned long)vaddr, 0);
2652 		break;
2653 	}
2654 
2655 	return xdp_act;
2656 }
2657 
2658 static enum qman_cb_dqrr_result rx_default_dqrr(struct qman_portal *portal,
2659 						struct qman_fq *fq,
2660 						const struct qm_dqrr_entry *dq,
2661 						bool sched_napi)
2662 {
2663 	bool ts_valid = false, hash_valid = false;
2664 	struct skb_shared_hwtstamps *shhwtstamps;
2665 	unsigned int skb_len, xdp_meta_len = 0;
2666 	struct rtnl_link_stats64 *percpu_stats;
2667 	struct dpaa_percpu_priv *percpu_priv;
2668 	const struct qm_fd *fd = &dq->fd;
2669 	dma_addr_t addr = qm_fd_addr(fd);
2670 	struct dpaa_napi_portal *np;
2671 	enum qm_fd_format fd_format;
2672 	struct net_device *net_dev;
2673 	u32 fd_status, hash_offset;
2674 	struct qm_sg_entry *sgt;
2675 	struct dpaa_bp *dpaa_bp;
2676 	struct dpaa_fq *dpaa_fq;
2677 	struct dpaa_priv *priv;
2678 	struct sk_buff *skb;
2679 	int *count_ptr;
2680 	u32 xdp_act;
2681 	void *vaddr;
2682 	u32 hash;
2683 	u64 ns;
2684 
2685 	dpaa_fq = container_of(fq, struct dpaa_fq, fq_base);
2686 	fd_status = be32_to_cpu(fd->status);
2687 	fd_format = qm_fd_get_format(fd);
2688 	net_dev = dpaa_fq->net_dev;
2689 	priv = netdev_priv(net_dev);
2690 	dpaa_bp = dpaa_bpid2pool(dq->fd.bpid);
2691 	if (!dpaa_bp)
2692 		return qman_cb_dqrr_consume;
2693 
2694 	/* Trace the Rx fd */
2695 	trace_dpaa_rx_fd(net_dev, fq, &dq->fd);
2696 
2697 	percpu_priv = this_cpu_ptr(priv->percpu_priv);
2698 	percpu_stats = &percpu_priv->stats;
2699 	np = &percpu_priv->np;
2700 
2701 	if (unlikely(dpaa_eth_napi_schedule(percpu_priv, portal, sched_napi)))
2702 		return qman_cb_dqrr_stop;
2703 
2704 	/* Make sure we didn't run out of buffers */
2705 	if (unlikely(dpaa_eth_refill_bpools(priv))) {
2706 		/* Unable to refill the buffer pool due to insufficient
2707 		 * system memory. Just release the frame back into the pool,
2708 		 * otherwise we'll soon end up with an empty buffer pool.
2709 		 */
2710 		dpaa_fd_release(net_dev, &dq->fd);
2711 		return qman_cb_dqrr_consume;
2712 	}
2713 
2714 	if (unlikely(fd_status & FM_FD_STAT_RX_ERRORS) != 0) {
2715 		if (net_ratelimit())
2716 			netif_warn(priv, hw, net_dev, "FD status = 0x%08x\n",
2717 				   fd_status & FM_FD_STAT_RX_ERRORS);
2718 
2719 		percpu_stats->rx_errors++;
2720 		dpaa_fd_release(net_dev, fd);
2721 		return qman_cb_dqrr_consume;
2722 	}
2723 
2724 	dma_unmap_page(dpaa_bp->priv->rx_dma_dev, addr, DPAA_BP_RAW_SIZE,
2725 		       DMA_FROM_DEVICE);
2726 
2727 	/* prefetch the first 64 bytes of the frame or the SGT start */
2728 	vaddr = phys_to_virt(addr);
2729 	prefetch(vaddr + qm_fd_get_offset(fd));
2730 
2731 	/* The only FD types that we may receive are contig and S/G */
2732 	WARN_ON((fd_format != qm_fd_contig) && (fd_format != qm_fd_sg));
2733 
2734 	/* Account for either the contig buffer or the SGT buffer (depending on
2735 	 * which case we were in) having been removed from the pool.
2736 	 */
2737 	count_ptr = this_cpu_ptr(dpaa_bp->percpu_count);
2738 	(*count_ptr)--;
2739 
2740 	/* Extract the timestamp stored in the headroom before running XDP */
2741 	if (priv->rx_tstamp) {
2742 		if (!fman_port_get_tstamp(priv->mac_dev->port[RX], vaddr, &ns))
2743 			ts_valid = true;
2744 		else
2745 			WARN_ONCE(1, "fman_port_get_tstamp failed!\n");
2746 	}
2747 
2748 	/* Extract the hash stored in the headroom before running XDP */
2749 	if (net_dev->features & NETIF_F_RXHASH && priv->keygen_in_use &&
2750 	    !fman_port_get_hash_result_offset(priv->mac_dev->port[RX],
2751 					      &hash_offset)) {
2752 		hash = be32_to_cpu(*(u32 *)(vaddr + hash_offset));
2753 		hash_valid = true;
2754 	}
2755 
2756 	if (likely(fd_format == qm_fd_contig)) {
2757 		xdp_act = dpaa_run_xdp(priv, (struct qm_fd *)fd, vaddr,
2758 				       dpaa_fq, &xdp_meta_len);
2759 		np->xdp_act |= xdp_act;
2760 		if (xdp_act != XDP_PASS) {
2761 			percpu_stats->rx_packets++;
2762 			percpu_stats->rx_bytes += qm_fd_get_length(fd);
2763 			return qman_cb_dqrr_consume;
2764 		}
2765 		skb = contig_fd_to_skb(priv, fd);
2766 	} else {
2767 		/* XDP doesn't support S/G frames. Return the fragments to the
2768 		 * buffer pool and release the SGT.
2769 		 */
2770 		if (READ_ONCE(priv->xdp_prog)) {
2771 			WARN_ONCE(1, "S/G frames not supported under XDP\n");
2772 			sgt = vaddr + qm_fd_get_offset(fd);
2773 			dpaa_release_sgt_members(sgt);
2774 			free_pages((unsigned long)vaddr, 0);
2775 			return qman_cb_dqrr_consume;
2776 		}
2777 		skb = sg_fd_to_skb(priv, fd);
2778 	}
2779 	if (!skb)
2780 		return qman_cb_dqrr_consume;
2781 
2782 	if (xdp_meta_len)
2783 		skb_metadata_set(skb, xdp_meta_len);
2784 
2785 	/* Set the previously extracted timestamp */
2786 	if (ts_valid) {
2787 		shhwtstamps = skb_hwtstamps(skb);
2788 		memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2789 		shhwtstamps->hwtstamp = ns_to_ktime(ns);
2790 	}
2791 
2792 	skb->protocol = eth_type_trans(skb, net_dev);
2793 
2794 	/* Set the previously extracted hash */
2795 	if (hash_valid) {
2796 		enum pkt_hash_types type;
2797 
2798 		/* if L4 exists, it was used in the hash generation */
2799 		type = be32_to_cpu(fd->status) & FM_FD_STAT_L4CV ?
2800 			PKT_HASH_TYPE_L4 : PKT_HASH_TYPE_L3;
2801 		skb_set_hash(skb, hash, type);
2802 	}
2803 
2804 	skb_len = skb->len;
2805 
2806 	if (unlikely(netif_receive_skb(skb) == NET_RX_DROP)) {
2807 		percpu_stats->rx_dropped++;
2808 		return qman_cb_dqrr_consume;
2809 	}
2810 
2811 	percpu_stats->rx_packets++;
2812 	percpu_stats->rx_bytes += skb_len;
2813 
2814 	return qman_cb_dqrr_consume;
2815 }
2816 
2817 static enum qman_cb_dqrr_result conf_error_dqrr(struct qman_portal *portal,
2818 						struct qman_fq *fq,
2819 						const struct qm_dqrr_entry *dq,
2820 						bool sched_napi)
2821 {
2822 	struct dpaa_percpu_priv *percpu_priv;
2823 	struct net_device *net_dev;
2824 	struct dpaa_priv *priv;
2825 
2826 	net_dev = ((struct dpaa_fq *)fq)->net_dev;
2827 	priv = netdev_priv(net_dev);
2828 
2829 	percpu_priv = this_cpu_ptr(priv->percpu_priv);
2830 
2831 	if (dpaa_eth_napi_schedule(percpu_priv, portal, sched_napi))
2832 		return qman_cb_dqrr_stop;
2833 
2834 	dpaa_tx_error(net_dev, priv, percpu_priv, &dq->fd, fq->fqid);
2835 
2836 	return qman_cb_dqrr_consume;
2837 }
2838 
2839 static enum qman_cb_dqrr_result conf_dflt_dqrr(struct qman_portal *portal,
2840 					       struct qman_fq *fq,
2841 					       const struct qm_dqrr_entry *dq,
2842 					       bool sched_napi)
2843 {
2844 	struct dpaa_percpu_priv *percpu_priv;
2845 	struct net_device *net_dev;
2846 	struct dpaa_priv *priv;
2847 
2848 	net_dev = ((struct dpaa_fq *)fq)->net_dev;
2849 	priv = netdev_priv(net_dev);
2850 
2851 	/* Trace the fd */
2852 	trace_dpaa_tx_conf_fd(net_dev, fq, &dq->fd);
2853 
2854 	percpu_priv = this_cpu_ptr(priv->percpu_priv);
2855 
2856 	if (dpaa_eth_napi_schedule(percpu_priv, portal, sched_napi))
2857 		return qman_cb_dqrr_stop;
2858 
2859 	dpaa_tx_conf(net_dev, priv, percpu_priv, &dq->fd, fq->fqid);
2860 
2861 	return qman_cb_dqrr_consume;
2862 }
2863 
2864 static void egress_ern(struct qman_portal *portal,
2865 		       struct qman_fq *fq,
2866 		       const union qm_mr_entry *msg)
2867 {
2868 	const struct qm_fd *fd = &msg->ern.fd;
2869 	struct dpaa_percpu_priv *percpu_priv;
2870 	const struct dpaa_priv *priv;
2871 	struct net_device *net_dev;
2872 	struct sk_buff *skb;
2873 
2874 	net_dev = ((struct dpaa_fq *)fq)->net_dev;
2875 	priv = netdev_priv(net_dev);
2876 	percpu_priv = this_cpu_ptr(priv->percpu_priv);
2877 
2878 	percpu_priv->stats.tx_dropped++;
2879 	percpu_priv->stats.tx_fifo_errors++;
2880 	count_ern(percpu_priv, msg);
2881 
2882 	skb = dpaa_cleanup_tx_fd(priv, fd, false);
2883 	dev_kfree_skb_any(skb);
2884 }
2885 
2886 static const struct dpaa_fq_cbs dpaa_fq_cbs = {
2887 	.rx_defq = { .cb = { .dqrr = rx_default_dqrr } },
2888 	.tx_defq = { .cb = { .dqrr = conf_dflt_dqrr } },
2889 	.rx_errq = { .cb = { .dqrr = rx_error_dqrr } },
2890 	.tx_errq = { .cb = { .dqrr = conf_error_dqrr } },
2891 	.egress_ern = { .cb = { .ern = egress_ern } }
2892 };
2893 
2894 static void dpaa_eth_napi_enable(struct dpaa_priv *priv)
2895 {
2896 	struct dpaa_percpu_priv *percpu_priv;
2897 	int i;
2898 
2899 	for_each_online_cpu(i) {
2900 		percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
2901 
2902 		percpu_priv->np.down = false;
2903 		napi_enable(&percpu_priv->np.napi);
2904 	}
2905 }
2906 
2907 static void dpaa_eth_napi_disable(struct dpaa_priv *priv)
2908 {
2909 	struct dpaa_percpu_priv *percpu_priv;
2910 	int i;
2911 
2912 	for_each_online_cpu(i) {
2913 		percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
2914 
2915 		percpu_priv->np.down = true;
2916 		napi_disable(&percpu_priv->np.napi);
2917 	}
2918 }
2919 
2920 static int dpaa_open(struct net_device *net_dev)
2921 {
2922 	struct mac_device *mac_dev;
2923 	struct dpaa_priv *priv;
2924 	int err, i;
2925 
2926 	priv = netdev_priv(net_dev);
2927 	mac_dev = priv->mac_dev;
2928 	dpaa_eth_napi_enable(priv);
2929 
2930 	err = phylink_of_phy_connect(mac_dev->phylink,
2931 				     mac_dev->dev->of_node, 0);
2932 	if (err)
2933 		goto phy_init_failed;
2934 
2935 	for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++) {
2936 		err = fman_port_enable(mac_dev->port[i]);
2937 		if (err)
2938 			goto mac_start_failed;
2939 	}
2940 
2941 	err = priv->mac_dev->enable(mac_dev->fman_mac);
2942 	if (err < 0) {
2943 		netif_err(priv, ifup, net_dev, "mac_dev->enable() = %d\n", err);
2944 		goto mac_start_failed;
2945 	}
2946 	phylink_start(mac_dev->phylink);
2947 
2948 	netif_tx_start_all_queues(net_dev);
2949 
2950 	return 0;
2951 
2952 mac_start_failed:
2953 	for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++)
2954 		fman_port_disable(mac_dev->port[i]);
2955 	phylink_disconnect_phy(mac_dev->phylink);
2956 
2957 phy_init_failed:
2958 	dpaa_eth_napi_disable(priv);
2959 
2960 	return err;
2961 }
2962 
2963 static int dpaa_eth_stop(struct net_device *net_dev)
2964 {
2965 	struct dpaa_priv *priv;
2966 	int err;
2967 
2968 	err = dpaa_stop(net_dev);
2969 
2970 	priv = netdev_priv(net_dev);
2971 	dpaa_eth_napi_disable(priv);
2972 
2973 	return err;
2974 }
2975 
2976 static bool xdp_validate_mtu(struct dpaa_priv *priv, int mtu)
2977 {
2978 	int max_contig_data = priv->dpaa_bp->size - priv->rx_headroom;
2979 
2980 	/* We do not support S/G fragments when XDP is enabled.
2981 	 * Limit the MTU in relation to the buffer size.
2982 	 */
2983 	if (mtu + VLAN_ETH_HLEN + ETH_FCS_LEN > max_contig_data) {
2984 		dev_warn(priv->net_dev->dev.parent,
2985 			 "The maximum MTU for XDP is %d\n",
2986 			 max_contig_data - VLAN_ETH_HLEN - ETH_FCS_LEN);
2987 		return false;
2988 	}
2989 
2990 	return true;
2991 }
2992 
2993 static int dpaa_change_mtu(struct net_device *net_dev, int new_mtu)
2994 {
2995 	struct dpaa_priv *priv = netdev_priv(net_dev);
2996 
2997 	if (priv->xdp_prog && !xdp_validate_mtu(priv, new_mtu))
2998 		return -EINVAL;
2999 
3000 	WRITE_ONCE(net_dev->mtu, new_mtu);
3001 	return 0;
3002 }
3003 
3004 static int dpaa_setup_xdp(struct net_device *net_dev, struct netdev_bpf *bpf)
3005 {
3006 	struct dpaa_priv *priv = netdev_priv(net_dev);
3007 	struct bpf_prog *old_prog;
3008 	int err;
3009 	bool up;
3010 
3011 	/* S/G fragments are not supported in XDP-mode */
3012 	if (bpf->prog && !xdp_validate_mtu(priv, net_dev->mtu)) {
3013 		NL_SET_ERR_MSG_MOD(bpf->extack, "MTU too large for XDP");
3014 		return -EINVAL;
3015 	}
3016 
3017 	up = netif_running(net_dev);
3018 
3019 	if (up)
3020 		dpaa_eth_stop(net_dev);
3021 
3022 	old_prog = xchg(&priv->xdp_prog, bpf->prog);
3023 	if (old_prog)
3024 		bpf_prog_put(old_prog);
3025 
3026 	if (up) {
3027 		err = dpaa_open(net_dev);
3028 		if (err) {
3029 			NL_SET_ERR_MSG_MOD(bpf->extack, "dpaa_open() failed");
3030 			return err;
3031 		}
3032 	}
3033 
3034 	return 0;
3035 }
3036 
3037 static int dpaa_xdp(struct net_device *net_dev, struct netdev_bpf *xdp)
3038 {
3039 	switch (xdp->command) {
3040 	case XDP_SETUP_PROG:
3041 		return dpaa_setup_xdp(net_dev, xdp);
3042 	default:
3043 		return -EINVAL;
3044 	}
3045 }
3046 
3047 static int dpaa_xdp_xmit(struct net_device *net_dev, int n,
3048 			 struct xdp_frame **frames, u32 flags)
3049 {
3050 	struct xdp_frame *xdpf;
3051 	int i, nxmit = 0;
3052 
3053 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
3054 		return -EINVAL;
3055 
3056 	if (!netif_running(net_dev))
3057 		return -ENETDOWN;
3058 
3059 	for (i = 0; i < n; i++) {
3060 		xdpf = frames[i];
3061 		if (dpaa_xdp_xmit_frame(net_dev, xdpf))
3062 			break;
3063 		nxmit++;
3064 	}
3065 
3066 	return nxmit;
3067 }
3068 
3069 static int dpaa_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3070 {
3071 	struct dpaa_priv *priv = netdev_priv(dev);
3072 	struct hwtstamp_config config;
3073 
3074 	if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
3075 		return -EFAULT;
3076 
3077 	switch (config.tx_type) {
3078 	case HWTSTAMP_TX_OFF:
3079 		/* Couldn't disable rx/tx timestamping separately.
3080 		 * Do nothing here.
3081 		 */
3082 		priv->tx_tstamp = false;
3083 		break;
3084 	case HWTSTAMP_TX_ON:
3085 		priv->mac_dev->set_tstamp(priv->mac_dev->fman_mac, true);
3086 		priv->tx_tstamp = true;
3087 		break;
3088 	default:
3089 		return -ERANGE;
3090 	}
3091 
3092 	if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
3093 		/* Couldn't disable rx/tx timestamping separately.
3094 		 * Do nothing here.
3095 		 */
3096 		priv->rx_tstamp = false;
3097 	} else {
3098 		priv->mac_dev->set_tstamp(priv->mac_dev->fman_mac, true);
3099 		priv->rx_tstamp = true;
3100 		/* TS is set for all frame types, not only those requested */
3101 		config.rx_filter = HWTSTAMP_FILTER_ALL;
3102 	}
3103 
3104 	return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
3105 			-EFAULT : 0;
3106 }
3107 
3108 static int dpaa_ioctl(struct net_device *net_dev, struct ifreq *rq, int cmd)
3109 {
3110 	int ret = -EINVAL;
3111 	struct dpaa_priv *priv = netdev_priv(net_dev);
3112 
3113 	if (cmd == SIOCGMIIREG) {
3114 		if (net_dev->phydev)
3115 			return phylink_mii_ioctl(priv->mac_dev->phylink, rq,
3116 						 cmd);
3117 	}
3118 
3119 	if (cmd == SIOCSHWTSTAMP)
3120 		return dpaa_ts_ioctl(net_dev, rq, cmd);
3121 
3122 	return ret;
3123 }
3124 
3125 static const struct net_device_ops dpaa_ops = {
3126 	.ndo_open = dpaa_open,
3127 	.ndo_start_xmit = dpaa_start_xmit,
3128 	.ndo_stop = dpaa_eth_stop,
3129 	.ndo_tx_timeout = dpaa_tx_timeout,
3130 	.ndo_get_stats64 = dpaa_get_stats64,
3131 	.ndo_change_carrier = fixed_phy_change_carrier,
3132 	.ndo_set_mac_address = dpaa_set_mac_address,
3133 	.ndo_validate_addr = eth_validate_addr,
3134 	.ndo_set_rx_mode = dpaa_set_rx_mode,
3135 	.ndo_eth_ioctl = dpaa_ioctl,
3136 	.ndo_setup_tc = dpaa_setup_tc,
3137 	.ndo_change_mtu = dpaa_change_mtu,
3138 	.ndo_bpf = dpaa_xdp,
3139 	.ndo_xdp_xmit = dpaa_xdp_xmit,
3140 };
3141 
3142 static int dpaa_napi_add(struct net_device *net_dev)
3143 {
3144 	struct dpaa_priv *priv = netdev_priv(net_dev);
3145 	struct dpaa_percpu_priv *percpu_priv;
3146 	int cpu;
3147 
3148 	for_each_possible_cpu(cpu) {
3149 		percpu_priv = per_cpu_ptr(priv->percpu_priv, cpu);
3150 
3151 		netif_napi_add(net_dev, &percpu_priv->np.napi, dpaa_eth_poll);
3152 	}
3153 
3154 	return 0;
3155 }
3156 
3157 static void dpaa_napi_del(struct net_device *net_dev)
3158 {
3159 	struct dpaa_priv *priv = netdev_priv(net_dev);
3160 	struct dpaa_percpu_priv *percpu_priv;
3161 	int cpu;
3162 
3163 	for_each_possible_cpu(cpu) {
3164 		percpu_priv = per_cpu_ptr(priv->percpu_priv, cpu);
3165 
3166 		netif_napi_del(&percpu_priv->np.napi);
3167 	}
3168 }
3169 
3170 static inline void dpaa_bp_free_pf(const struct dpaa_bp *bp,
3171 				   struct bm_buffer *bmb)
3172 {
3173 	dma_addr_t addr = bm_buf_addr(bmb);
3174 
3175 	dma_unmap_page(bp->priv->rx_dma_dev, addr, DPAA_BP_RAW_SIZE,
3176 		       DMA_FROM_DEVICE);
3177 
3178 	skb_free_frag(phys_to_virt(addr));
3179 }
3180 
3181 /* Alloc the dpaa_bp struct and configure default values */
3182 static struct dpaa_bp *dpaa_bp_alloc(struct device *dev)
3183 {
3184 	struct dpaa_bp *dpaa_bp;
3185 
3186 	dpaa_bp = devm_kzalloc(dev, sizeof(*dpaa_bp), GFP_KERNEL);
3187 	if (!dpaa_bp)
3188 		return ERR_PTR(-ENOMEM);
3189 
3190 	dpaa_bp->bpid = FSL_DPAA_BPID_INV;
3191 	dpaa_bp->percpu_count = devm_alloc_percpu(dev, *dpaa_bp->percpu_count);
3192 	if (!dpaa_bp->percpu_count)
3193 		return ERR_PTR(-ENOMEM);
3194 
3195 	dpaa_bp->config_count = FSL_DPAA_ETH_MAX_BUF_COUNT;
3196 
3197 	dpaa_bp->seed_cb = dpaa_bp_seed;
3198 	dpaa_bp->free_buf_cb = dpaa_bp_free_pf;
3199 
3200 	return dpaa_bp;
3201 }
3202 
3203 /* Place all ingress FQs (Rx Default, Rx Error) in a dedicated CGR.
3204  * We won't be sending congestion notifications to FMan; for now, we just use
3205  * this CGR to generate enqueue rejections to FMan in order to drop the frames
3206  * before they reach our ingress queues and eat up memory.
3207  */
3208 static int dpaa_ingress_cgr_init(struct dpaa_priv *priv)
3209 {
3210 	struct qm_mcc_initcgr initcgr;
3211 	u32 cs_th;
3212 	int err;
3213 
3214 	err = qman_alloc_cgrid(&priv->ingress_cgr.cgrid);
3215 	if (err < 0) {
3216 		if (netif_msg_drv(priv))
3217 			pr_err("Error %d allocating CGR ID\n", err);
3218 		goto out_error;
3219 	}
3220 
3221 	/* Enable CS TD, but disable Congestion State Change Notifications. */
3222 	memset(&initcgr, 0, sizeof(initcgr));
3223 	initcgr.we_mask = cpu_to_be16(QM_CGR_WE_CS_THRES);
3224 	initcgr.cgr.cscn_en = QM_CGR_EN;
3225 	cs_th = DPAA_INGRESS_CS_THRESHOLD;
3226 	qm_cgr_cs_thres_set64(&initcgr.cgr.cs_thres, cs_th, 1);
3227 
3228 	initcgr.we_mask |= cpu_to_be16(QM_CGR_WE_CSTD_EN);
3229 	initcgr.cgr.cstd_en = QM_CGR_EN;
3230 
3231 	/* This CGR will be associated with the SWP affined to the current CPU.
3232 	 * However, we'll place all our ingress FQs in it.
3233 	 */
3234 	err = qman_create_cgr(&priv->ingress_cgr, QMAN_CGR_FLAG_USE_INIT,
3235 			      &initcgr);
3236 	if (err < 0) {
3237 		if (netif_msg_drv(priv))
3238 			pr_err("Error %d creating ingress CGR with ID %d\n",
3239 			       err, priv->ingress_cgr.cgrid);
3240 		qman_release_cgrid(priv->ingress_cgr.cgrid);
3241 		goto out_error;
3242 	}
3243 	if (netif_msg_drv(priv))
3244 		pr_debug("Created ingress CGR %d for netdev with hwaddr %pM\n",
3245 			 priv->ingress_cgr.cgrid, priv->mac_dev->addr);
3246 
3247 	priv->use_ingress_cgr = true;
3248 
3249 out_error:
3250 	return err;
3251 }
3252 
3253 static u16 dpaa_get_headroom(struct dpaa_buffer_layout *bl,
3254 			     enum port_type port)
3255 {
3256 	u16 headroom;
3257 
3258 	/* The frame headroom must accommodate:
3259 	 * - the driver private data area
3260 	 * - parse results, hash results, timestamp if selected
3261 	 * If either hash results or time stamp are selected, both will
3262 	 * be copied to/from the frame headroom, as TS is located between PR and
3263 	 * HR in the IC and IC copy size has a granularity of 16bytes
3264 	 * (see description of FMBM_RICP and FMBM_TICP registers in DPAARM)
3265 	 *
3266 	 * Also make sure the headroom is a multiple of data_align bytes
3267 	 */
3268 	headroom = (u16)(bl[port].priv_data_size + DPAA_HWA_SIZE);
3269 
3270 	if (port == RX) {
3271 #ifdef CONFIG_DPAA_ERRATUM_A050385
3272 		if (unlikely(fman_has_errata_a050385()))
3273 			headroom = XDP_PACKET_HEADROOM;
3274 #endif
3275 
3276 		return ALIGN(headroom, DPAA_FD_RX_DATA_ALIGNMENT);
3277 	} else {
3278 		return ALIGN(headroom, DPAA_FD_DATA_ALIGNMENT);
3279 	}
3280 }
3281 
3282 static int dpaa_eth_probe(struct platform_device *pdev)
3283 {
3284 	struct net_device *net_dev = NULL;
3285 	struct dpaa_bp *dpaa_bp = NULL;
3286 	struct dpaa_fq *dpaa_fq, *tmp;
3287 	struct dpaa_priv *priv = NULL;
3288 	struct fm_port_fqs port_fqs;
3289 	struct mac_device *mac_dev;
3290 	int err = 0, channel;
3291 	struct device *dev;
3292 
3293 	dev = &pdev->dev;
3294 
3295 	err = bman_is_probed();
3296 	if (!err)
3297 		return -EPROBE_DEFER;
3298 	if (err < 0) {
3299 		dev_err(dev, "failing probe due to bman probe error\n");
3300 		return -ENODEV;
3301 	}
3302 	err = qman_is_probed();
3303 	if (!err)
3304 		return -EPROBE_DEFER;
3305 	if (err < 0) {
3306 		dev_err(dev, "failing probe due to qman probe error\n");
3307 		return -ENODEV;
3308 	}
3309 	err = bman_portals_probed();
3310 	if (!err)
3311 		return -EPROBE_DEFER;
3312 	if (err < 0) {
3313 		dev_err(dev,
3314 			"failing probe due to bman portals probe error\n");
3315 		return -ENODEV;
3316 	}
3317 	err = qman_portals_probed();
3318 	if (!err)
3319 		return -EPROBE_DEFER;
3320 	if (err < 0) {
3321 		dev_err(dev,
3322 			"failing probe due to qman portals probe error\n");
3323 		return -ENODEV;
3324 	}
3325 
3326 	/* Allocate this early, so we can store relevant information in
3327 	 * the private area
3328 	 */
3329 	net_dev = alloc_etherdev_mq(sizeof(*priv), dpaa_max_num_txqs());
3330 	if (!net_dev) {
3331 		dev_err(dev, "alloc_etherdev_mq() failed\n");
3332 		return -ENOMEM;
3333 	}
3334 
3335 	/* Do this here, so we can be verbose early */
3336 	SET_NETDEV_DEV(net_dev, dev->parent);
3337 	dev_set_drvdata(dev, net_dev);
3338 
3339 	priv = netdev_priv(net_dev);
3340 	priv->net_dev = net_dev;
3341 
3342 	priv->msg_enable = netif_msg_init(debug, DPAA_MSG_DEFAULT);
3343 
3344 	priv->egress_fqs = devm_kcalloc(dev, dpaa_max_num_txqs(),
3345 					sizeof(*priv->egress_fqs),
3346 					GFP_KERNEL);
3347 	if (!priv->egress_fqs) {
3348 		err = -ENOMEM;
3349 		goto free_netdev;
3350 	}
3351 
3352 	priv->conf_fqs = devm_kcalloc(dev, dpaa_max_num_txqs(),
3353 				      sizeof(*priv->conf_fqs),
3354 				      GFP_KERNEL);
3355 	if (!priv->conf_fqs) {
3356 		err = -ENOMEM;
3357 		goto free_netdev;
3358 	}
3359 
3360 	mac_dev = dpaa_mac_dev_get(pdev);
3361 	if (IS_ERR(mac_dev)) {
3362 		netdev_err(net_dev, "dpaa_mac_dev_get() failed\n");
3363 		err = PTR_ERR(mac_dev);
3364 		goto free_netdev;
3365 	}
3366 
3367 	/* Devices used for DMA mapping */
3368 	priv->rx_dma_dev = fman_port_get_device(mac_dev->port[RX]);
3369 	priv->tx_dma_dev = fman_port_get_device(mac_dev->port[TX]);
3370 	err = dma_coerce_mask_and_coherent(priv->rx_dma_dev, DMA_BIT_MASK(40));
3371 	if (!err)
3372 		err = dma_coerce_mask_and_coherent(priv->tx_dma_dev,
3373 						   DMA_BIT_MASK(40));
3374 	if (err) {
3375 		netdev_err(net_dev, "dma_coerce_mask_and_coherent() failed\n");
3376 		goto free_netdev;
3377 	}
3378 
3379 	/* If fsl_fm_max_frm is set to a higher value than the all-common 1500,
3380 	 * we choose conservatively and let the user explicitly set a higher
3381 	 * MTU via ifconfig. Otherwise, the user may end up with different MTUs
3382 	 * in the same LAN.
3383 	 * If on the other hand fsl_fm_max_frm has been chosen below 1500,
3384 	 * start with the maximum allowed.
3385 	 */
3386 	net_dev->mtu = min(dpaa_get_max_mtu(), ETH_DATA_LEN);
3387 
3388 	netdev_dbg(net_dev, "Setting initial MTU on net device: %d\n",
3389 		   net_dev->mtu);
3390 
3391 	priv->buf_layout[RX].priv_data_size = DPAA_RX_PRIV_DATA_SIZE; /* Rx */
3392 	priv->buf_layout[TX].priv_data_size = DPAA_TX_PRIV_DATA_SIZE; /* Tx */
3393 
3394 	/* bp init */
3395 	dpaa_bp = dpaa_bp_alloc(dev);
3396 	if (IS_ERR(dpaa_bp)) {
3397 		err = PTR_ERR(dpaa_bp);
3398 		goto free_dpaa_bps;
3399 	}
3400 	/* the raw size of the buffers used for reception */
3401 	dpaa_bp->raw_size = DPAA_BP_RAW_SIZE;
3402 	/* avoid runtime computations by keeping the usable size here */
3403 	dpaa_bp->size = dpaa_bp_size(dpaa_bp->raw_size);
3404 	dpaa_bp->priv = priv;
3405 
3406 	err = dpaa_bp_alloc_pool(dpaa_bp);
3407 	if (err < 0)
3408 		goto free_dpaa_bps;
3409 	priv->dpaa_bp = dpaa_bp;
3410 
3411 	INIT_LIST_HEAD(&priv->dpaa_fq_list);
3412 
3413 	memset(&port_fqs, 0, sizeof(port_fqs));
3414 
3415 	err = dpaa_alloc_all_fqs(dev, &priv->dpaa_fq_list, &port_fqs);
3416 	if (err < 0) {
3417 		dev_err(dev, "dpaa_alloc_all_fqs() failed\n");
3418 		goto free_dpaa_bps;
3419 	}
3420 
3421 	priv->mac_dev = mac_dev;
3422 
3423 	channel = dpaa_get_channel();
3424 	if (channel < 0) {
3425 		dev_err(dev, "dpaa_get_channel() failed\n");
3426 		err = channel;
3427 		goto free_dpaa_bps;
3428 	}
3429 
3430 	priv->channel = (u16)channel;
3431 
3432 	/* Walk the CPUs with affine portals
3433 	 * and add this pool channel to each's dequeue mask.
3434 	 */
3435 	dpaa_eth_add_channel(priv->channel, &pdev->dev);
3436 
3437 	err = dpaa_fq_setup(priv, &dpaa_fq_cbs, priv->mac_dev->port[TX]);
3438 	if (err)
3439 		goto free_dpaa_bps;
3440 
3441 	/* Create a congestion group for this netdev, with
3442 	 * dynamically-allocated CGR ID.
3443 	 * Must be executed after probing the MAC, but before
3444 	 * assigning the egress FQs to the CGRs.
3445 	 */
3446 	err = dpaa_eth_cgr_init(priv);
3447 	if (err < 0) {
3448 		dev_err(dev, "Error initializing CGR\n");
3449 		goto free_dpaa_bps;
3450 	}
3451 
3452 	err = dpaa_ingress_cgr_init(priv);
3453 	if (err < 0) {
3454 		dev_err(dev, "Error initializing ingress CGR\n");
3455 		goto delete_egress_cgr;
3456 	}
3457 
3458 	/* Add the FQs to the interface, and make them active */
3459 	list_for_each_entry_safe(dpaa_fq, tmp, &priv->dpaa_fq_list, list) {
3460 		err = dpaa_fq_init(dpaa_fq, false);
3461 		if (err < 0)
3462 			goto free_dpaa_fqs;
3463 	}
3464 
3465 	priv->tx_headroom = dpaa_get_headroom(priv->buf_layout, TX);
3466 	priv->rx_headroom = dpaa_get_headroom(priv->buf_layout, RX);
3467 
3468 	/* All real interfaces need their ports initialized */
3469 	err = dpaa_eth_init_ports(mac_dev, dpaa_bp, &port_fqs,
3470 				  &priv->buf_layout[0], dev);
3471 	if (err)
3472 		goto free_dpaa_fqs;
3473 
3474 	/* Rx traffic distribution based on keygen hashing defaults to on */
3475 	priv->keygen_in_use = true;
3476 
3477 	priv->percpu_priv = devm_alloc_percpu(dev, *priv->percpu_priv);
3478 	if (!priv->percpu_priv) {
3479 		dev_err(dev, "devm_alloc_percpu() failed\n");
3480 		err = -ENOMEM;
3481 		goto free_dpaa_fqs;
3482 	}
3483 
3484 	priv->num_tc = 1;
3485 	netif_set_real_num_tx_queues(net_dev,
3486 				     priv->num_tc * dpaa_num_txqs_per_tc());
3487 
3488 	/* Initialize NAPI */
3489 	err = dpaa_napi_add(net_dev);
3490 	if (err < 0)
3491 		goto delete_dpaa_napi;
3492 
3493 	err = dpaa_netdev_init(net_dev, &dpaa_ops, tx_timeout);
3494 	if (err < 0)
3495 		goto delete_dpaa_napi;
3496 
3497 	dpaa_eth_sysfs_init(&net_dev->dev);
3498 
3499 	netif_info(priv, probe, net_dev, "Probed interface %s\n",
3500 		   net_dev->name);
3501 
3502 	return 0;
3503 
3504 delete_dpaa_napi:
3505 	dpaa_napi_del(net_dev);
3506 free_dpaa_fqs:
3507 	dpaa_fq_free(dev, &priv->dpaa_fq_list);
3508 	qman_delete_cgr_safe(&priv->ingress_cgr);
3509 	qman_release_cgrid(priv->ingress_cgr.cgrid);
3510 delete_egress_cgr:
3511 	qman_delete_cgr_safe(&priv->cgr_data.cgr);
3512 	qman_release_cgrid(priv->cgr_data.cgr.cgrid);
3513 free_dpaa_bps:
3514 	dpaa_bps_free(priv);
3515 free_netdev:
3516 	dev_set_drvdata(dev, NULL);
3517 	free_netdev(net_dev);
3518 
3519 	return err;
3520 }
3521 
3522 static void dpaa_remove(struct platform_device *pdev)
3523 {
3524 	struct net_device *net_dev;
3525 	struct dpaa_priv *priv;
3526 	struct device *dev;
3527 	int err;
3528 
3529 	dev = &pdev->dev;
3530 	net_dev = dev_get_drvdata(dev);
3531 
3532 	priv = netdev_priv(net_dev);
3533 
3534 	dpaa_eth_sysfs_remove(dev);
3535 
3536 	dev_set_drvdata(dev, NULL);
3537 	unregister_netdev(net_dev);
3538 	phylink_destroy(priv->mac_dev->phylink);
3539 
3540 	err = dpaa_fq_free(dev, &priv->dpaa_fq_list);
3541 	if (err)
3542 		dev_err(dev, "Failed to free FQs on remove (%pE)\n",
3543 			ERR_PTR(err));
3544 
3545 	qman_delete_cgr_safe(&priv->ingress_cgr);
3546 	qman_release_cgrid(priv->ingress_cgr.cgrid);
3547 	qman_delete_cgr_safe(&priv->cgr_data.cgr);
3548 	qman_release_cgrid(priv->cgr_data.cgr.cgrid);
3549 
3550 	dpaa_napi_del(net_dev);
3551 
3552 	dpaa_bps_free(priv);
3553 
3554 	free_netdev(net_dev);
3555 }
3556 
3557 static const struct platform_device_id dpaa_devtype[] = {
3558 	{
3559 		.name = "dpaa-ethernet",
3560 		.driver_data = 0,
3561 	}, {
3562 	}
3563 };
3564 MODULE_DEVICE_TABLE(platform, dpaa_devtype);
3565 
3566 static struct platform_driver dpaa_driver = {
3567 	.driver = {
3568 		.name = KBUILD_MODNAME,
3569 	},
3570 	.id_table = dpaa_devtype,
3571 	.probe = dpaa_eth_probe,
3572 	.remove_new = dpaa_remove
3573 };
3574 
3575 static int __init dpaa_load(void)
3576 {
3577 	int err;
3578 
3579 	pr_debug("FSL DPAA Ethernet driver\n");
3580 
3581 	/* initialize dpaa_eth mirror values */
3582 	dpaa_rx_extra_headroom = fman_get_rx_extra_headroom();
3583 	dpaa_max_frm = fman_get_max_frm();
3584 
3585 	err = platform_driver_register(&dpaa_driver);
3586 	if (err < 0)
3587 		pr_err("Error, platform_driver_register() = %d\n", err);
3588 
3589 	return err;
3590 }
3591 module_init(dpaa_load);
3592 
3593 static void __exit dpaa_unload(void)
3594 {
3595 	platform_driver_unregister(&dpaa_driver);
3596 
3597 	/* Only one channel is used and needs to be released after all
3598 	 * interfaces are removed
3599 	 */
3600 	dpaa_release_channel();
3601 }
3602 module_exit(dpaa_unload);
3603 
3604 MODULE_LICENSE("Dual BSD/GPL");
3605 MODULE_DESCRIPTION("FSL DPAA Ethernet driver");
3606