source: rtems-libbsd/linux/drivers/net/ethernet/freescale/dpaa/dpaa_eth.c

6-freebsd-12
Last change on this file was 647dd08, checked in by Sebastian Huber <sebastian.huber@…>, on 04/11/19 at 07:33:39

Update due to API changes

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