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