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