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