Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later
0002 /*
0003  * Copyright 2008 - 2016 Freescale Semiconductor Inc.
0004  * Copyright 2020 NXP
0005  */
0006 
0007 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0008 
0009 #include <linux/init.h>
0010 #include <linux/module.h>
0011 #include <linux/of_platform.h>
0012 #include <linux/of_mdio.h>
0013 #include <linux/of_net.h>
0014 #include <linux/io.h>
0015 #include <linux/if_arp.h>
0016 #include <linux/if_vlan.h>
0017 #include <linux/icmp.h>
0018 #include <linux/ip.h>
0019 #include <linux/ipv6.h>
0020 #include <linux/udp.h>
0021 #include <linux/tcp.h>
0022 #include <linux/net.h>
0023 #include <linux/skbuff.h>
0024 #include <linux/etherdevice.h>
0025 #include <linux/if_ether.h>
0026 #include <linux/highmem.h>
0027 #include <linux/percpu.h>
0028 #include <linux/dma-mapping.h>
0029 #include <linux/sort.h>
0030 #include <linux/phy_fixed.h>
0031 #include <linux/bpf.h>
0032 #include <linux/bpf_trace.h>
0033 #include <soc/fsl/bman.h>
0034 #include <soc/fsl/qman.h>
0035 #include "fman.h"
0036 #include "fman_port.h"
0037 #include "mac.h"
0038 #include "dpaa_eth.h"
0039 
0040 /* CREATE_TRACE_POINTS only needs to be defined once. Other dpaa files
0041  * using trace events only need to #include <trace/events/sched.h>
0042  */
0043 #define CREATE_TRACE_POINTS
0044 #include "dpaa_eth_trace.h"
0045 
0046 static int debug = -1;
0047 module_param(debug, int, 0444);
0048 MODULE_PARM_DESC(debug, "Module/Driver verbosity level (0=none,...,16=all)");
0049 
0050 static u16 tx_timeout = 1000;
0051 module_param(tx_timeout, ushort, 0444);
0052 MODULE_PARM_DESC(tx_timeout, "The Tx timeout in ms");
0053 
0054 #define FM_FD_STAT_RX_ERRORS                        \
0055     (FM_FD_ERR_DMA | FM_FD_ERR_PHYSICAL | \
0056      FM_FD_ERR_SIZE | FM_FD_ERR_CLS_DISCARD | \
0057      FM_FD_ERR_EXTRACTION | FM_FD_ERR_NO_SCHEME | \
0058      FM_FD_ERR_PRS_TIMEOUT | FM_FD_ERR_PRS_ILL_INSTRUCT | \
0059      FM_FD_ERR_PRS_HDR_ERR)
0060 
0061 #define FM_FD_STAT_TX_ERRORS \
0062     (FM_FD_ERR_UNSUPPORTED_FORMAT | \
0063      FM_FD_ERR_LENGTH | FM_FD_ERR_DMA)
0064 
0065 #define DPAA_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
0066               NETIF_MSG_LINK | NETIF_MSG_IFUP | \
0067               NETIF_MSG_IFDOWN | NETIF_MSG_HW)
0068 
0069 #define DPAA_INGRESS_CS_THRESHOLD 0x10000000
0070 /* Ingress congestion threshold on FMan ports
0071  * The size in bytes of the ingress tail-drop threshold on FMan ports.
0072  * Traffic piling up above this value will be rejected by QMan and discarded
0073  * by FMan.
0074  */
0075 
0076 /* Size in bytes of the FQ taildrop threshold */
0077 #define DPAA_FQ_TD 0x200000
0078 
0079 #define DPAA_CS_THRESHOLD_1G 0x06000000
0080 /* Egress congestion threshold on 1G ports, range 0x1000 .. 0x10000000
0081  * The size in bytes of the egress Congestion State notification threshold on
0082  * 1G ports. The 1G dTSECs can quite easily be flooded by cores doing Tx in a
0083  * tight loop (e.g. by sending UDP datagrams at "while(1) speed"),
0084  * and the larger the frame size, the more acute the problem.
0085  * So we have to find a balance between these factors:
0086  * - avoiding the device staying congested for a prolonged time (risking
0087  *   the netdev watchdog to fire - see also the tx_timeout module param);
0088  * - affecting performance of protocols such as TCP, which otherwise
0089  *   behave well under the congestion notification mechanism;
0090  * - preventing the Tx cores from tightly-looping (as if the congestion
0091  *   threshold was too low to be effective);
0092  * - running out of memory if the CS threshold is set too high.
0093  */
0094 
0095 #define DPAA_CS_THRESHOLD_10G 0x10000000
0096 /* The size in bytes of the egress Congestion State notification threshold on
0097  * 10G ports, range 0x1000 .. 0x10000000
0098  */
0099 
0100 /* Largest value that the FQD's OAL field can hold */
0101 #define FSL_QMAN_MAX_OAL    127
0102 
0103 /* Default alignment for start of data in an Rx FD */
0104 #ifdef CONFIG_DPAA_ERRATUM_A050385
0105 /* aligning data start to 64 avoids DMA transaction splits, unless the buffer
0106  * is crossing a 4k page boundary
0107  */
0108 #define DPAA_FD_DATA_ALIGNMENT  (fman_has_errata_a050385() ? 64 : 16)
0109 /* aligning to 256 avoids DMA transaction splits caused by 4k page boundary
0110  * crossings; also, all SG fragments except the last must have a size multiple
0111  * of 256 to avoid DMA transaction splits
0112  */
0113 #define DPAA_A050385_ALIGN 256
0114 #define DPAA_FD_RX_DATA_ALIGNMENT (fman_has_errata_a050385() ? \
0115                    DPAA_A050385_ALIGN : 16)
0116 #else
0117 #define DPAA_FD_DATA_ALIGNMENT  16
0118 #define DPAA_FD_RX_DATA_ALIGNMENT DPAA_FD_DATA_ALIGNMENT
0119 #endif
0120 
0121 /* The DPAA requires 256 bytes reserved and mapped for the SGT */
0122 #define DPAA_SGT_SIZE 256
0123 
0124 /* Values for the L3R field of the FM Parse Results
0125  */
0126 /* L3 Type field: First IP Present IPv4 */
0127 #define FM_L3_PARSE_RESULT_IPV4 0x8000
0128 /* L3 Type field: First IP Present IPv6 */
0129 #define FM_L3_PARSE_RESULT_IPV6 0x4000
0130 /* Values for the L4R field of the FM Parse Results */
0131 /* L4 Type field: UDP */
0132 #define FM_L4_PARSE_RESULT_UDP  0x40
0133 /* L4 Type field: TCP */
0134 #define FM_L4_PARSE_RESULT_TCP  0x20
0135 
0136 /* FD status field indicating whether the FM Parser has attempted to validate
0137  * the L4 csum of the frame.
0138  * Note that having this bit set doesn't necessarily imply that the checksum
0139  * is valid. One would have to check the parse results to find that out.
0140  */
0141 #define FM_FD_STAT_L4CV         0x00000004
0142 
0143 #define DPAA_SGT_MAX_ENTRIES 16 /* maximum number of entries in SG Table */
0144 #define DPAA_BUFF_RELEASE_MAX 8 /* maximum number of buffers released at once */
0145 
0146 #define FSL_DPAA_BPID_INV       0xff
0147 #define FSL_DPAA_ETH_MAX_BUF_COUNT  128
0148 #define FSL_DPAA_ETH_REFILL_THRESHOLD   80
0149 
0150 #define DPAA_TX_PRIV_DATA_SIZE  16
0151 #define DPAA_PARSE_RESULTS_SIZE sizeof(struct fman_prs_result)
0152 #define DPAA_TIME_STAMP_SIZE 8
0153 #define DPAA_HASH_RESULTS_SIZE 8
0154 #define DPAA_HWA_SIZE (DPAA_PARSE_RESULTS_SIZE + DPAA_TIME_STAMP_SIZE \
0155                + DPAA_HASH_RESULTS_SIZE)
0156 #define DPAA_RX_PRIV_DATA_DEFAULT_SIZE (DPAA_TX_PRIV_DATA_SIZE + \
0157                     XDP_PACKET_HEADROOM - DPAA_HWA_SIZE)
0158 #ifdef CONFIG_DPAA_ERRATUM_A050385
0159 #define DPAA_RX_PRIV_DATA_A050385_SIZE (DPAA_A050385_ALIGN - DPAA_HWA_SIZE)
0160 #define DPAA_RX_PRIV_DATA_SIZE (fman_has_errata_a050385() ? \
0161                 DPAA_RX_PRIV_DATA_A050385_SIZE : \
0162                 DPAA_RX_PRIV_DATA_DEFAULT_SIZE)
0163 #else
0164 #define DPAA_RX_PRIV_DATA_SIZE DPAA_RX_PRIV_DATA_DEFAULT_SIZE
0165 #endif
0166 
0167 #define DPAA_ETH_PCD_RXQ_NUM    128
0168 
0169 #define DPAA_ENQUEUE_RETRIES    100000
0170 
0171 enum port_type {RX, TX};
0172 
0173 struct fm_port_fqs {
0174     struct dpaa_fq *tx_defq;
0175     struct dpaa_fq *tx_errq;
0176     struct dpaa_fq *rx_defq;
0177     struct dpaa_fq *rx_errq;
0178     struct dpaa_fq *rx_pcdq;
0179 };
0180 
0181 /* All the dpa bps in use at any moment */
0182 static struct dpaa_bp *dpaa_bp_array[BM_MAX_NUM_OF_POOLS];
0183 
0184 #define DPAA_BP_RAW_SIZE 4096
0185 
0186 #ifdef CONFIG_DPAA_ERRATUM_A050385
0187 #define dpaa_bp_size(raw_size) (SKB_WITH_OVERHEAD(raw_size) & \
0188                 ~(DPAA_A050385_ALIGN - 1))
0189 #else
0190 #define dpaa_bp_size(raw_size) SKB_WITH_OVERHEAD(raw_size)
0191 #endif
0192 
0193 static int dpaa_max_frm;
0194 
0195 static int dpaa_rx_extra_headroom;
0196 
0197 #define dpaa_get_max_mtu()  \
0198     (dpaa_max_frm - (VLAN_ETH_HLEN + ETH_FCS_LEN))
0199 
0200 static int dpaa_netdev_init(struct net_device *net_dev,
0201                 const struct net_device_ops *dpaa_ops,
0202                 u16 tx_timeout)
0203 {
0204     struct dpaa_priv *priv = netdev_priv(net_dev);
0205     struct device *dev = net_dev->dev.parent;
0206     struct dpaa_percpu_priv *percpu_priv;
0207     const u8 *mac_addr;
0208     int i, err;
0209 
0210     /* Although we access another CPU's private data here
0211      * we do it at initialization so it is safe
0212      */
0213     for_each_possible_cpu(i) {
0214         percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
0215         percpu_priv->net_dev = net_dev;
0216     }
0217 
0218     net_dev->netdev_ops = dpaa_ops;
0219     mac_addr = priv->mac_dev->addr;
0220 
0221     net_dev->mem_start = priv->mac_dev->res->start;
0222     net_dev->mem_end = priv->mac_dev->res->end;
0223 
0224     net_dev->min_mtu = ETH_MIN_MTU;
0225     net_dev->max_mtu = dpaa_get_max_mtu();
0226 
0227     net_dev->hw_features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
0228                  NETIF_F_LLTX | NETIF_F_RXHASH);
0229 
0230     net_dev->hw_features |= NETIF_F_SG | NETIF_F_HIGHDMA;
0231     /* The kernels enables GSO automatically, if we declare NETIF_F_SG.
0232      * For conformity, we'll still declare GSO explicitly.
0233      */
0234     net_dev->features |= NETIF_F_GSO;
0235     net_dev->features |= NETIF_F_RXCSUM;
0236 
0237     net_dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
0238     /* we do not want shared skbs on TX */
0239     net_dev->priv_flags &= ~IFF_TX_SKB_SHARING;
0240 
0241     net_dev->features |= net_dev->hw_features;
0242     net_dev->vlan_features = net_dev->features;
0243 
0244     if (is_valid_ether_addr(mac_addr)) {
0245         memcpy(net_dev->perm_addr, mac_addr, net_dev->addr_len);
0246         eth_hw_addr_set(net_dev, mac_addr);
0247     } else {
0248         eth_hw_addr_random(net_dev);
0249         err = priv->mac_dev->change_addr(priv->mac_dev->fman_mac,
0250             (const enet_addr_t *)net_dev->dev_addr);
0251         if (err) {
0252             dev_err(dev, "Failed to set random MAC address\n");
0253             return -EINVAL;
0254         }
0255         dev_info(dev, "Using random MAC address: %pM\n",
0256              net_dev->dev_addr);
0257     }
0258 
0259     net_dev->ethtool_ops = &dpaa_ethtool_ops;
0260 
0261     net_dev->needed_headroom = priv->tx_headroom;
0262     net_dev->watchdog_timeo = msecs_to_jiffies(tx_timeout);
0263 
0264     /* start without the RUNNING flag, phylib controls it later */
0265     netif_carrier_off(net_dev);
0266 
0267     err = register_netdev(net_dev);
0268     if (err < 0) {
0269         dev_err(dev, "register_netdev() = %d\n", err);
0270         return err;
0271     }
0272 
0273     return 0;
0274 }
0275 
0276 static int dpaa_stop(struct net_device *net_dev)
0277 {
0278     struct mac_device *mac_dev;
0279     struct dpaa_priv *priv;
0280     int i, err, error;
0281 
0282     priv = netdev_priv(net_dev);
0283     mac_dev = priv->mac_dev;
0284 
0285     netif_tx_stop_all_queues(net_dev);
0286     /* Allow the Fman (Tx) port to process in-flight frames before we
0287      * try switching it off.
0288      */
0289     msleep(200);
0290 
0291     err = mac_dev->stop(mac_dev);
0292     if (err < 0)
0293         netif_err(priv, ifdown, net_dev, "mac_dev->stop() = %d\n",
0294               err);
0295 
0296     for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++) {
0297         error = fman_port_disable(mac_dev->port[i]);
0298         if (error)
0299             err = error;
0300     }
0301 
0302     if (net_dev->phydev)
0303         phy_disconnect(net_dev->phydev);
0304     net_dev->phydev = NULL;
0305 
0306     msleep(200);
0307 
0308     return err;
0309 }
0310 
0311 static void dpaa_tx_timeout(struct net_device *net_dev, unsigned int txqueue)
0312 {
0313     struct dpaa_percpu_priv *percpu_priv;
0314     const struct dpaa_priv  *priv;
0315 
0316     priv = netdev_priv(net_dev);
0317     percpu_priv = this_cpu_ptr(priv->percpu_priv);
0318 
0319     netif_crit(priv, timer, net_dev, "Transmit timeout latency: %u ms\n",
0320            jiffies_to_msecs(jiffies - dev_trans_start(net_dev)));
0321 
0322     percpu_priv->stats.tx_errors++;
0323 }
0324 
0325 /* Calculates the statistics for the given device by adding the statistics
0326  * collected by each CPU.
0327  */
0328 static void dpaa_get_stats64(struct net_device *net_dev,
0329                  struct rtnl_link_stats64 *s)
0330 {
0331     int numstats = sizeof(struct rtnl_link_stats64) / sizeof(u64);
0332     struct dpaa_priv *priv = netdev_priv(net_dev);
0333     struct dpaa_percpu_priv *percpu_priv;
0334     u64 *netstats = (u64 *)s;
0335     u64 *cpustats;
0336     int i, j;
0337 
0338     for_each_possible_cpu(i) {
0339         percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
0340 
0341         cpustats = (u64 *)&percpu_priv->stats;
0342 
0343         /* add stats from all CPUs */
0344         for (j = 0; j < numstats; j++)
0345             netstats[j] += cpustats[j];
0346     }
0347 }
0348 
0349 static int dpaa_setup_tc(struct net_device *net_dev, enum tc_setup_type type,
0350              void *type_data)
0351 {
0352     struct dpaa_priv *priv = netdev_priv(net_dev);
0353     struct tc_mqprio_qopt *mqprio = type_data;
0354     u8 num_tc;
0355     int i;
0356 
0357     if (type != TC_SETUP_QDISC_MQPRIO)
0358         return -EOPNOTSUPP;
0359 
0360     mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
0361     num_tc = mqprio->num_tc;
0362 
0363     if (num_tc == priv->num_tc)
0364         return 0;
0365 
0366     if (!num_tc) {
0367         netdev_reset_tc(net_dev);
0368         goto out;
0369     }
0370 
0371     if (num_tc > DPAA_TC_NUM) {
0372         netdev_err(net_dev, "Too many traffic classes: max %d supported.\n",
0373                DPAA_TC_NUM);
0374         return -EINVAL;
0375     }
0376 
0377     netdev_set_num_tc(net_dev, num_tc);
0378 
0379     for (i = 0; i < num_tc; i++)
0380         netdev_set_tc_queue(net_dev, i, DPAA_TC_TXQ_NUM,
0381                     i * DPAA_TC_TXQ_NUM);
0382 
0383 out:
0384     priv->num_tc = num_tc ? : 1;
0385     netif_set_real_num_tx_queues(net_dev, priv->num_tc * DPAA_TC_TXQ_NUM);
0386     return 0;
0387 }
0388 
0389 static struct mac_device *dpaa_mac_dev_get(struct platform_device *pdev)
0390 {
0391     struct dpaa_eth_data *eth_data;
0392     struct device *dpaa_dev;
0393     struct mac_device *mac_dev;
0394 
0395     dpaa_dev = &pdev->dev;
0396     eth_data = dpaa_dev->platform_data;
0397     if (!eth_data) {
0398         dev_err(dpaa_dev, "eth_data missing\n");
0399         return ERR_PTR(-ENODEV);
0400     }
0401     mac_dev = eth_data->mac_dev;
0402     if (!mac_dev) {
0403         dev_err(dpaa_dev, "mac_dev missing\n");
0404         return ERR_PTR(-EINVAL);
0405     }
0406 
0407     return mac_dev;
0408 }
0409 
0410 static int dpaa_set_mac_address(struct net_device *net_dev, void *addr)
0411 {
0412     const struct dpaa_priv *priv;
0413     struct mac_device *mac_dev;
0414     struct sockaddr old_addr;
0415     int err;
0416 
0417     priv = netdev_priv(net_dev);
0418 
0419     memcpy(old_addr.sa_data, net_dev->dev_addr,  ETH_ALEN);
0420 
0421     err = eth_mac_addr(net_dev, addr);
0422     if (err < 0) {
0423         netif_err(priv, drv, net_dev, "eth_mac_addr() = %d\n", err);
0424         return err;
0425     }
0426 
0427     mac_dev = priv->mac_dev;
0428 
0429     err = mac_dev->change_addr(mac_dev->fman_mac,
0430                    (const enet_addr_t *)net_dev->dev_addr);
0431     if (err < 0) {
0432         netif_err(priv, drv, net_dev, "mac_dev->change_addr() = %d\n",
0433               err);
0434         /* reverting to previous address */
0435         eth_mac_addr(net_dev, &old_addr);
0436 
0437         return err;
0438     }
0439 
0440     return 0;
0441 }
0442 
0443 static void dpaa_set_rx_mode(struct net_device *net_dev)
0444 {
0445     const struct dpaa_priv  *priv;
0446     int err;
0447 
0448     priv = netdev_priv(net_dev);
0449 
0450     if (!!(net_dev->flags & IFF_PROMISC) != priv->mac_dev->promisc) {
0451         priv->mac_dev->promisc = !priv->mac_dev->promisc;
0452         err = priv->mac_dev->set_promisc(priv->mac_dev->fman_mac,
0453                          priv->mac_dev->promisc);
0454         if (err < 0)
0455             netif_err(priv, drv, net_dev,
0456                   "mac_dev->set_promisc() = %d\n",
0457                   err);
0458     }
0459 
0460     if (!!(net_dev->flags & IFF_ALLMULTI) != priv->mac_dev->allmulti) {
0461         priv->mac_dev->allmulti = !priv->mac_dev->allmulti;
0462         err = priv->mac_dev->set_allmulti(priv->mac_dev->fman_mac,
0463                           priv->mac_dev->allmulti);
0464         if (err < 0)
0465             netif_err(priv, drv, net_dev,
0466                   "mac_dev->set_allmulti() = %d\n",
0467                   err);
0468     }
0469 
0470     err = priv->mac_dev->set_multi(net_dev, priv->mac_dev);
0471     if (err < 0)
0472         netif_err(priv, drv, net_dev, "mac_dev->set_multi() = %d\n",
0473               err);
0474 }
0475 
0476 static struct dpaa_bp *dpaa_bpid2pool(int bpid)
0477 {
0478     if (WARN_ON(bpid < 0 || bpid >= BM_MAX_NUM_OF_POOLS))
0479         return NULL;
0480 
0481     return dpaa_bp_array[bpid];
0482 }
0483 
0484 /* checks if this bpool is already allocated */
0485 static bool dpaa_bpid2pool_use(int bpid)
0486 {
0487     if (dpaa_bpid2pool(bpid)) {
0488         refcount_inc(&dpaa_bp_array[bpid]->refs);
0489         return true;
0490     }
0491 
0492     return false;
0493 }
0494 
0495 /* called only once per bpid by dpaa_bp_alloc_pool() */
0496 static void dpaa_bpid2pool_map(int bpid, struct dpaa_bp *dpaa_bp)
0497 {
0498     dpaa_bp_array[bpid] = dpaa_bp;
0499     refcount_set(&dpaa_bp->refs, 1);
0500 }
0501 
0502 static int dpaa_bp_alloc_pool(struct dpaa_bp *dpaa_bp)
0503 {
0504     int err;
0505 
0506     if (dpaa_bp->size == 0 || dpaa_bp->config_count == 0) {
0507         pr_err("%s: Buffer pool is not properly initialized! Missing size or initial number of buffers\n",
0508                __func__);
0509         return -EINVAL;
0510     }
0511 
0512     /* If the pool is already specified, we only create one per bpid */
0513     if (dpaa_bp->bpid != FSL_DPAA_BPID_INV &&
0514         dpaa_bpid2pool_use(dpaa_bp->bpid))
0515         return 0;
0516 
0517     if (dpaa_bp->bpid == FSL_DPAA_BPID_INV) {
0518         dpaa_bp->pool = bman_new_pool();
0519         if (!dpaa_bp->pool) {
0520             pr_err("%s: bman_new_pool() failed\n",
0521                    __func__);
0522             return -ENODEV;
0523         }
0524 
0525         dpaa_bp->bpid = (u8)bman_get_bpid(dpaa_bp->pool);
0526     }
0527 
0528     if (dpaa_bp->seed_cb) {
0529         err = dpaa_bp->seed_cb(dpaa_bp);
0530         if (err)
0531             goto pool_seed_failed;
0532     }
0533 
0534     dpaa_bpid2pool_map(dpaa_bp->bpid, dpaa_bp);
0535 
0536     return 0;
0537 
0538 pool_seed_failed:
0539     pr_err("%s: pool seeding failed\n", __func__);
0540     bman_free_pool(dpaa_bp->pool);
0541 
0542     return err;
0543 }
0544 
0545 /* remove and free all the buffers from the given buffer pool */
0546 static void dpaa_bp_drain(struct dpaa_bp *bp)
0547 {
0548     u8 num = 8;
0549     int ret;
0550 
0551     do {
0552         struct bm_buffer bmb[8];
0553         int i;
0554 
0555         ret = bman_acquire(bp->pool, bmb, num);
0556         if (ret < 0) {
0557             if (num == 8) {
0558                 /* we have less than 8 buffers left;
0559                  * drain them one by one
0560                  */
0561                 num = 1;
0562                 ret = 1;
0563                 continue;
0564             } else {
0565                 /* Pool is fully drained */
0566                 break;
0567             }
0568         }
0569 
0570         if (bp->free_buf_cb)
0571             for (i = 0; i < num; i++)
0572                 bp->free_buf_cb(bp, &bmb[i]);
0573     } while (ret > 0);
0574 }
0575 
0576 static void dpaa_bp_free(struct dpaa_bp *dpaa_bp)
0577 {
0578     struct dpaa_bp *bp = dpaa_bpid2pool(dpaa_bp->bpid);
0579 
0580     /* the mapping between bpid and dpaa_bp is done very late in the
0581      * allocation procedure; if something failed before the mapping, the bp
0582      * was not configured, therefore we don't need the below instructions
0583      */
0584     if (!bp)
0585         return;
0586 
0587     if (!refcount_dec_and_test(&bp->refs))
0588         return;
0589 
0590     if (bp->free_buf_cb)
0591         dpaa_bp_drain(bp);
0592 
0593     dpaa_bp_array[bp->bpid] = NULL;
0594     bman_free_pool(bp->pool);
0595 }
0596 
0597 static void dpaa_bps_free(struct dpaa_priv *priv)
0598 {
0599     dpaa_bp_free(priv->dpaa_bp);
0600 }
0601 
0602 /* Use multiple WQs for FQ assignment:
0603  *  - Tx Confirmation queues go to WQ1.
0604  *  - Rx Error and Tx Error queues go to WQ5 (giving them a better chance
0605  *    to be scheduled, in case there are many more FQs in WQ6).
0606  *  - Rx Default goes to WQ6.
0607  *  - Tx queues go to different WQs depending on their priority. Equal
0608  *    chunks of NR_CPUS queues go to WQ6 (lowest priority), WQ2, WQ1 and
0609  *    WQ0 (highest priority).
0610  * This ensures that Tx-confirmed buffers are timely released. In particular,
0611  * it avoids congestion on the Tx Confirm FQs, which can pile up PFDRs if they
0612  * are greatly outnumbered by other FQs in the system, while
0613  * dequeue scheduling is round-robin.
0614  */
0615 static inline void dpaa_assign_wq(struct dpaa_fq *fq, int idx)
0616 {
0617     switch (fq->fq_type) {
0618     case FQ_TYPE_TX_CONFIRM:
0619     case FQ_TYPE_TX_CONF_MQ:
0620         fq->wq = 1;
0621         break;
0622     case FQ_TYPE_RX_ERROR:
0623     case FQ_TYPE_TX_ERROR:
0624         fq->wq = 5;
0625         break;
0626     case FQ_TYPE_RX_DEFAULT:
0627     case FQ_TYPE_RX_PCD:
0628         fq->wq = 6;
0629         break;
0630     case FQ_TYPE_TX:
0631         switch (idx / DPAA_TC_TXQ_NUM) {
0632         case 0:
0633             /* Low priority (best effort) */
0634             fq->wq = 6;
0635             break;
0636         case 1:
0637             /* Medium priority */
0638             fq->wq = 2;
0639             break;
0640         case 2:
0641             /* High priority */
0642             fq->wq = 1;
0643             break;
0644         case 3:
0645             /* Very high priority */
0646             fq->wq = 0;
0647             break;
0648         default:
0649             WARN(1, "Too many TX FQs: more than %d!\n",
0650                  DPAA_ETH_TXQ_NUM);
0651         }
0652         break;
0653     default:
0654         WARN(1, "Invalid FQ type %d for FQID %d!\n",
0655              fq->fq_type, fq->fqid);
0656     }
0657 }
0658 
0659 static struct dpaa_fq *dpaa_fq_alloc(struct device *dev,
0660                      u32 start, u32 count,
0661                      struct list_head *list,
0662                      enum dpaa_fq_type fq_type)
0663 {
0664     struct dpaa_fq *dpaa_fq;
0665     int i;
0666 
0667     dpaa_fq = devm_kcalloc(dev, count, sizeof(*dpaa_fq),
0668                    GFP_KERNEL);
0669     if (!dpaa_fq)
0670         return NULL;
0671 
0672     for (i = 0; i < count; i++) {
0673         dpaa_fq[i].fq_type = fq_type;
0674         dpaa_fq[i].fqid = start ? start + i : 0;
0675         list_add_tail(&dpaa_fq[i].list, list);
0676     }
0677 
0678     for (i = 0; i < count; i++)
0679         dpaa_assign_wq(dpaa_fq + i, i);
0680 
0681     return dpaa_fq;
0682 }
0683 
0684 static int dpaa_alloc_all_fqs(struct device *dev, struct list_head *list,
0685                   struct fm_port_fqs *port_fqs)
0686 {
0687     struct dpaa_fq *dpaa_fq;
0688     u32 fq_base, fq_base_aligned, i;
0689 
0690     dpaa_fq = dpaa_fq_alloc(dev, 0, 1, list, FQ_TYPE_RX_ERROR);
0691     if (!dpaa_fq)
0692         goto fq_alloc_failed;
0693 
0694     port_fqs->rx_errq = &dpaa_fq[0];
0695 
0696     dpaa_fq = dpaa_fq_alloc(dev, 0, 1, list, FQ_TYPE_RX_DEFAULT);
0697     if (!dpaa_fq)
0698         goto fq_alloc_failed;
0699 
0700     port_fqs->rx_defq = &dpaa_fq[0];
0701 
0702     /* the PCD FQIDs range needs to be aligned for correct operation */
0703     if (qman_alloc_fqid_range(&fq_base, 2 * DPAA_ETH_PCD_RXQ_NUM))
0704         goto fq_alloc_failed;
0705 
0706     fq_base_aligned = ALIGN(fq_base, DPAA_ETH_PCD_RXQ_NUM);
0707 
0708     for (i = fq_base; i < fq_base_aligned; i++)
0709         qman_release_fqid(i);
0710 
0711     for (i = fq_base_aligned + DPAA_ETH_PCD_RXQ_NUM;
0712          i < (fq_base + 2 * DPAA_ETH_PCD_RXQ_NUM); i++)
0713         qman_release_fqid(i);
0714 
0715     dpaa_fq = dpaa_fq_alloc(dev, fq_base_aligned, DPAA_ETH_PCD_RXQ_NUM,
0716                 list, FQ_TYPE_RX_PCD);
0717     if (!dpaa_fq)
0718         goto fq_alloc_failed;
0719 
0720     port_fqs->rx_pcdq = &dpaa_fq[0];
0721 
0722     if (!dpaa_fq_alloc(dev, 0, DPAA_ETH_TXQ_NUM, list, FQ_TYPE_TX_CONF_MQ))
0723         goto fq_alloc_failed;
0724 
0725     dpaa_fq = dpaa_fq_alloc(dev, 0, 1, list, FQ_TYPE_TX_ERROR);
0726     if (!dpaa_fq)
0727         goto fq_alloc_failed;
0728 
0729     port_fqs->tx_errq = &dpaa_fq[0];
0730 
0731     dpaa_fq = dpaa_fq_alloc(dev, 0, 1, list, FQ_TYPE_TX_CONFIRM);
0732     if (!dpaa_fq)
0733         goto fq_alloc_failed;
0734 
0735     port_fqs->tx_defq = &dpaa_fq[0];
0736 
0737     if (!dpaa_fq_alloc(dev, 0, DPAA_ETH_TXQ_NUM, list, FQ_TYPE_TX))
0738         goto fq_alloc_failed;
0739 
0740     return 0;
0741 
0742 fq_alloc_failed:
0743     dev_err(dev, "dpaa_fq_alloc() failed\n");
0744     return -ENOMEM;
0745 }
0746 
0747 static u32 rx_pool_channel;
0748 static DEFINE_SPINLOCK(rx_pool_channel_init);
0749 
0750 static int dpaa_get_channel(void)
0751 {
0752     spin_lock(&rx_pool_channel_init);
0753     if (!rx_pool_channel) {
0754         u32 pool;
0755         int ret;
0756 
0757         ret = qman_alloc_pool(&pool);
0758 
0759         if (!ret)
0760             rx_pool_channel = pool;
0761     }
0762     spin_unlock(&rx_pool_channel_init);
0763     if (!rx_pool_channel)
0764         return -ENOMEM;
0765     return rx_pool_channel;
0766 }
0767 
0768 static void dpaa_release_channel(void)
0769 {
0770     qman_release_pool(rx_pool_channel);
0771 }
0772 
0773 static void dpaa_eth_add_channel(u16 channel, struct device *dev)
0774 {
0775     u32 pool = QM_SDQCR_CHANNELS_POOL_CONV(channel);
0776     const cpumask_t *cpus = qman_affine_cpus();
0777     struct qman_portal *portal;
0778     int cpu;
0779 
0780     for_each_cpu_and(cpu, cpus, cpu_online_mask) {
0781         portal = qman_get_affine_portal(cpu);
0782         qman_p_static_dequeue_add(portal, pool);
0783         qman_start_using_portal(portal, dev);
0784     }
0785 }
0786 
0787 /* Congestion group state change notification callback.
0788  * Stops the device's egress queues while they are congested and
0789  * wakes them upon exiting congested state.
0790  * Also updates some CGR-related stats.
0791  */
0792 static void dpaa_eth_cgscn(struct qman_portal *qm, struct qman_cgr *cgr,
0793                int congested)
0794 {
0795     struct dpaa_priv *priv = (struct dpaa_priv *)container_of(cgr,
0796         struct dpaa_priv, cgr_data.cgr);
0797 
0798     if (congested) {
0799         priv->cgr_data.congestion_start_jiffies = jiffies;
0800         netif_tx_stop_all_queues(priv->net_dev);
0801         priv->cgr_data.cgr_congested_count++;
0802     } else {
0803         priv->cgr_data.congested_jiffies +=
0804             (jiffies - priv->cgr_data.congestion_start_jiffies);
0805         netif_tx_wake_all_queues(priv->net_dev);
0806     }
0807 }
0808 
0809 static int dpaa_eth_cgr_init(struct dpaa_priv *priv)
0810 {
0811     struct qm_mcc_initcgr initcgr;
0812     u32 cs_th;
0813     int err;
0814 
0815     err = qman_alloc_cgrid(&priv->cgr_data.cgr.cgrid);
0816     if (err < 0) {
0817         if (netif_msg_drv(priv))
0818             pr_err("%s: Error %d allocating CGR ID\n",
0819                    __func__, err);
0820         goto out_error;
0821     }
0822     priv->cgr_data.cgr.cb = dpaa_eth_cgscn;
0823 
0824     /* Enable Congestion State Change Notifications and CS taildrop */
0825     memset(&initcgr, 0, sizeof(initcgr));
0826     initcgr.we_mask = cpu_to_be16(QM_CGR_WE_CSCN_EN | QM_CGR_WE_CS_THRES);
0827     initcgr.cgr.cscn_en = QM_CGR_EN;
0828 
0829     /* Set different thresholds based on the MAC speed.
0830      * This may turn suboptimal if the MAC is reconfigured at a speed
0831      * lower than its max, e.g. if a dTSEC later negotiates a 100Mbps link.
0832      * In such cases, we ought to reconfigure the threshold, too.
0833      */
0834     if (priv->mac_dev->if_support & SUPPORTED_10000baseT_Full)
0835         cs_th = DPAA_CS_THRESHOLD_10G;
0836     else
0837         cs_th = DPAA_CS_THRESHOLD_1G;
0838     qm_cgr_cs_thres_set64(&initcgr.cgr.cs_thres, cs_th, 1);
0839 
0840     initcgr.we_mask |= cpu_to_be16(QM_CGR_WE_CSTD_EN);
0841     initcgr.cgr.cstd_en = QM_CGR_EN;
0842 
0843     err = qman_create_cgr(&priv->cgr_data.cgr, QMAN_CGR_FLAG_USE_INIT,
0844                   &initcgr);
0845     if (err < 0) {
0846         if (netif_msg_drv(priv))
0847             pr_err("%s: Error %d creating CGR with ID %d\n",
0848                    __func__, err, priv->cgr_data.cgr.cgrid);
0849         qman_release_cgrid(priv->cgr_data.cgr.cgrid);
0850         goto out_error;
0851     }
0852     if (netif_msg_drv(priv))
0853         pr_debug("Created CGR %d for netdev with hwaddr %pM on QMan channel %d\n",
0854              priv->cgr_data.cgr.cgrid, priv->mac_dev->addr,
0855              priv->cgr_data.cgr.chan);
0856 
0857 out_error:
0858     return err;
0859 }
0860 
0861 static inline void dpaa_setup_ingress(const struct dpaa_priv *priv,
0862                       struct dpaa_fq *fq,
0863                       const struct qman_fq *template)
0864 {
0865     fq->fq_base = *template;
0866     fq->net_dev = priv->net_dev;
0867 
0868     fq->flags = QMAN_FQ_FLAG_NO_ENQUEUE;
0869     fq->channel = priv->channel;
0870 }
0871 
0872 static inline void dpaa_setup_egress(const struct dpaa_priv *priv,
0873                      struct dpaa_fq *fq,
0874                      struct fman_port *port,
0875                      const struct qman_fq *template)
0876 {
0877     fq->fq_base = *template;
0878     fq->net_dev = priv->net_dev;
0879 
0880     if (port) {
0881         fq->flags = QMAN_FQ_FLAG_TO_DCPORTAL;
0882         fq->channel = (u16)fman_port_get_qman_channel_id(port);
0883     } else {
0884         fq->flags = QMAN_FQ_FLAG_NO_MODIFY;
0885     }
0886 }
0887 
0888 static void dpaa_fq_setup(struct dpaa_priv *priv,
0889               const struct dpaa_fq_cbs *fq_cbs,
0890               struct fman_port *tx_port)
0891 {
0892     int egress_cnt = 0, conf_cnt = 0, num_portals = 0, portal_cnt = 0, cpu;
0893     const cpumask_t *affine_cpus = qman_affine_cpus();
0894     u16 channels[NR_CPUS];
0895     struct dpaa_fq *fq;
0896 
0897     for_each_cpu_and(cpu, affine_cpus, cpu_online_mask)
0898         channels[num_portals++] = qman_affine_channel(cpu);
0899 
0900     if (num_portals == 0)
0901         dev_err(priv->net_dev->dev.parent,
0902             "No Qman software (affine) channels found\n");
0903 
0904     /* Initialize each FQ in the list */
0905     list_for_each_entry(fq, &priv->dpaa_fq_list, list) {
0906         switch (fq->fq_type) {
0907         case FQ_TYPE_RX_DEFAULT:
0908             dpaa_setup_ingress(priv, fq, &fq_cbs->rx_defq);
0909             break;
0910         case FQ_TYPE_RX_ERROR:
0911             dpaa_setup_ingress(priv, fq, &fq_cbs->rx_errq);
0912             break;
0913         case FQ_TYPE_RX_PCD:
0914             if (!num_portals)
0915                 continue;
0916             dpaa_setup_ingress(priv, fq, &fq_cbs->rx_defq);
0917             fq->channel = channels[portal_cnt++ % num_portals];
0918             break;
0919         case FQ_TYPE_TX:
0920             dpaa_setup_egress(priv, fq, tx_port,
0921                       &fq_cbs->egress_ern);
0922             /* If we have more Tx queues than the number of cores,
0923              * just ignore the extra ones.
0924              */
0925             if (egress_cnt < DPAA_ETH_TXQ_NUM)
0926                 priv->egress_fqs[egress_cnt++] = &fq->fq_base;
0927             break;
0928         case FQ_TYPE_TX_CONF_MQ:
0929             priv->conf_fqs[conf_cnt++] = &fq->fq_base;
0930             fallthrough;
0931         case FQ_TYPE_TX_CONFIRM:
0932             dpaa_setup_ingress(priv, fq, &fq_cbs->tx_defq);
0933             break;
0934         case FQ_TYPE_TX_ERROR:
0935             dpaa_setup_ingress(priv, fq, &fq_cbs->tx_errq);
0936             break;
0937         default:
0938             dev_warn(priv->net_dev->dev.parent,
0939                  "Unknown FQ type detected!\n");
0940             break;
0941         }
0942     }
0943 
0944      /* Make sure all CPUs receive a corresponding Tx queue. */
0945     while (egress_cnt < DPAA_ETH_TXQ_NUM) {
0946         list_for_each_entry(fq, &priv->dpaa_fq_list, list) {
0947             if (fq->fq_type != FQ_TYPE_TX)
0948                 continue;
0949             priv->egress_fqs[egress_cnt++] = &fq->fq_base;
0950             if (egress_cnt == DPAA_ETH_TXQ_NUM)
0951                 break;
0952         }
0953     }
0954 }
0955 
0956 static inline int dpaa_tx_fq_to_id(const struct dpaa_priv *priv,
0957                    struct qman_fq *tx_fq)
0958 {
0959     int i;
0960 
0961     for (i = 0; i < DPAA_ETH_TXQ_NUM; i++)
0962         if (priv->egress_fqs[i] == tx_fq)
0963             return i;
0964 
0965     return -EINVAL;
0966 }
0967 
0968 static int dpaa_fq_init(struct dpaa_fq *dpaa_fq, bool td_enable)
0969 {
0970     const struct dpaa_priv  *priv;
0971     struct qman_fq *confq = NULL;
0972     struct qm_mcc_initfq initfq;
0973     struct device *dev;
0974     struct qman_fq *fq;
0975     int queue_id;
0976     int err;
0977 
0978     priv = netdev_priv(dpaa_fq->net_dev);
0979     dev = dpaa_fq->net_dev->dev.parent;
0980 
0981     if (dpaa_fq->fqid == 0)
0982         dpaa_fq->flags |= QMAN_FQ_FLAG_DYNAMIC_FQID;
0983 
0984     dpaa_fq->init = !(dpaa_fq->flags & QMAN_FQ_FLAG_NO_MODIFY);
0985 
0986     err = qman_create_fq(dpaa_fq->fqid, dpaa_fq->flags, &dpaa_fq->fq_base);
0987     if (err) {
0988         dev_err(dev, "qman_create_fq() failed\n");
0989         return err;
0990     }
0991     fq = &dpaa_fq->fq_base;
0992 
0993     if (dpaa_fq->init) {
0994         memset(&initfq, 0, sizeof(initfq));
0995 
0996         initfq.we_mask = cpu_to_be16(QM_INITFQ_WE_FQCTRL);
0997         /* Note: we may get to keep an empty FQ in cache */
0998         initfq.fqd.fq_ctrl = cpu_to_be16(QM_FQCTRL_PREFERINCACHE);
0999 
1000         /* Try to reduce the number of portal interrupts for
1001          * Tx Confirmation FQs.
1002          */
1003         if (dpaa_fq->fq_type == FQ_TYPE_TX_CONFIRM)
1004             initfq.fqd.fq_ctrl |= cpu_to_be16(QM_FQCTRL_AVOIDBLOCK);
1005 
1006         /* FQ placement */
1007         initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_DESTWQ);
1008 
1009         qm_fqd_set_destwq(&initfq.fqd, dpaa_fq->channel, dpaa_fq->wq);
1010 
1011         /* Put all egress queues in a congestion group of their own.
1012          * Sensu stricto, the Tx confirmation queues are Rx FQs,
1013          * rather than Tx - but they nonetheless account for the
1014          * memory footprint on behalf of egress traffic. We therefore
1015          * place them in the netdev's CGR, along with the Tx FQs.
1016          */
1017         if (dpaa_fq->fq_type == FQ_TYPE_TX ||
1018             dpaa_fq->fq_type == FQ_TYPE_TX_CONFIRM ||
1019             dpaa_fq->fq_type == FQ_TYPE_TX_CONF_MQ) {
1020             initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_CGID);
1021             initfq.fqd.fq_ctrl |= cpu_to_be16(QM_FQCTRL_CGE);
1022             initfq.fqd.cgid = (u8)priv->cgr_data.cgr.cgrid;
1023             /* Set a fixed overhead accounting, in an attempt to
1024              * reduce the impact of fixed-size skb shells and the
1025              * driver's needed headroom on system memory. This is
1026              * especially the case when the egress traffic is
1027              * composed of small datagrams.
1028              * Unfortunately, QMan's OAL value is capped to an
1029              * insufficient value, but even that is better than
1030              * no overhead accounting at all.
1031              */
1032             initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_OAC);
1033             qm_fqd_set_oac(&initfq.fqd, QM_OAC_CG);
1034             qm_fqd_set_oal(&initfq.fqd,
1035                        min(sizeof(struct sk_buff) +
1036                        priv->tx_headroom,
1037                        (size_t)FSL_QMAN_MAX_OAL));
1038         }
1039 
1040         if (td_enable) {
1041             initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_TDTHRESH);
1042             qm_fqd_set_taildrop(&initfq.fqd, DPAA_FQ_TD, 1);
1043             initfq.fqd.fq_ctrl = cpu_to_be16(QM_FQCTRL_TDE);
1044         }
1045 
1046         if (dpaa_fq->fq_type == FQ_TYPE_TX) {
1047             queue_id = dpaa_tx_fq_to_id(priv, &dpaa_fq->fq_base);
1048             if (queue_id >= 0)
1049                 confq = priv->conf_fqs[queue_id];
1050             if (confq) {
1051                 initfq.we_mask |=
1052                     cpu_to_be16(QM_INITFQ_WE_CONTEXTA);
1053             /* ContextA: OVOM=1(use contextA2 bits instead of ICAD)
1054              *       A2V=1 (contextA A2 field is valid)
1055              *       A0V=1 (contextA A0 field is valid)
1056              *       B0V=1 (contextB field is valid)
1057              * ContextA A2: EBD=1 (deallocate buffers inside FMan)
1058              * ContextB B0(ASPID): 0 (absolute Virtual Storage ID)
1059              */
1060                 qm_fqd_context_a_set64(&initfq.fqd,
1061                                0x1e00000080000000ULL);
1062             }
1063         }
1064 
1065         /* Put all the ingress queues in our "ingress CGR". */
1066         if (priv->use_ingress_cgr &&
1067             (dpaa_fq->fq_type == FQ_TYPE_RX_DEFAULT ||
1068              dpaa_fq->fq_type == FQ_TYPE_RX_ERROR ||
1069              dpaa_fq->fq_type == FQ_TYPE_RX_PCD)) {
1070             initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_CGID);
1071             initfq.fqd.fq_ctrl |= cpu_to_be16(QM_FQCTRL_CGE);
1072             initfq.fqd.cgid = (u8)priv->ingress_cgr.cgrid;
1073             /* Set a fixed overhead accounting, just like for the
1074              * egress CGR.
1075              */
1076             initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_OAC);
1077             qm_fqd_set_oac(&initfq.fqd, QM_OAC_CG);
1078             qm_fqd_set_oal(&initfq.fqd,
1079                        min(sizeof(struct sk_buff) +
1080                        priv->tx_headroom,
1081                        (size_t)FSL_QMAN_MAX_OAL));
1082         }
1083 
1084         /* Initialization common to all ingress queues */
1085         if (dpaa_fq->flags & QMAN_FQ_FLAG_NO_ENQUEUE) {
1086             initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_CONTEXTA);
1087             initfq.fqd.fq_ctrl |= cpu_to_be16(QM_FQCTRL_HOLDACTIVE |
1088                         QM_FQCTRL_CTXASTASHING);
1089             initfq.fqd.context_a.stashing.exclusive =
1090                 QM_STASHING_EXCL_DATA | QM_STASHING_EXCL_CTX |
1091                 QM_STASHING_EXCL_ANNOTATION;
1092             qm_fqd_set_stashing(&initfq.fqd, 1, 2,
1093                         DIV_ROUND_UP(sizeof(struct qman_fq),
1094                              64));
1095         }
1096 
1097         err = qman_init_fq(fq, QMAN_INITFQ_FLAG_SCHED, &initfq);
1098         if (err < 0) {
1099             dev_err(dev, "qman_init_fq(%u) = %d\n",
1100                 qman_fq_fqid(fq), err);
1101             qman_destroy_fq(fq);
1102             return err;
1103         }
1104     }
1105 
1106     dpaa_fq->fqid = qman_fq_fqid(fq);
1107 
1108     if (dpaa_fq->fq_type == FQ_TYPE_RX_DEFAULT ||
1109         dpaa_fq->fq_type == FQ_TYPE_RX_PCD) {
1110         err = xdp_rxq_info_reg(&dpaa_fq->xdp_rxq, dpaa_fq->net_dev,
1111                        dpaa_fq->fqid, 0);
1112         if (err) {
1113             dev_err(dev, "xdp_rxq_info_reg() = %d\n", err);
1114             return err;
1115         }
1116 
1117         err = xdp_rxq_info_reg_mem_model(&dpaa_fq->xdp_rxq,
1118                          MEM_TYPE_PAGE_ORDER0, NULL);
1119         if (err) {
1120             dev_err(dev, "xdp_rxq_info_reg_mem_model() = %d\n",
1121                 err);
1122             xdp_rxq_info_unreg(&dpaa_fq->xdp_rxq);
1123             return err;
1124         }
1125     }
1126 
1127     return 0;
1128 }
1129 
1130 static int dpaa_fq_free_entry(struct device *dev, struct qman_fq *fq)
1131 {
1132     const struct dpaa_priv  *priv;
1133     struct dpaa_fq *dpaa_fq;
1134     int err, error;
1135 
1136     err = 0;
1137 
1138     dpaa_fq = container_of(fq, struct dpaa_fq, fq_base);
1139     priv = netdev_priv(dpaa_fq->net_dev);
1140 
1141     if (dpaa_fq->init) {
1142         err = qman_retire_fq(fq, NULL);
1143         if (err < 0 && netif_msg_drv(priv))
1144             dev_err(dev, "qman_retire_fq(%u) = %d\n",
1145                 qman_fq_fqid(fq), err);
1146 
1147         error = qman_oos_fq(fq);
1148         if (error < 0 && netif_msg_drv(priv)) {
1149             dev_err(dev, "qman_oos_fq(%u) = %d\n",
1150                 qman_fq_fqid(fq), error);
1151             if (err >= 0)
1152                 err = error;
1153         }
1154     }
1155 
1156     if ((dpaa_fq->fq_type == FQ_TYPE_RX_DEFAULT ||
1157          dpaa_fq->fq_type == FQ_TYPE_RX_PCD) &&
1158         xdp_rxq_info_is_reg(&dpaa_fq->xdp_rxq))
1159         xdp_rxq_info_unreg(&dpaa_fq->xdp_rxq);
1160 
1161     qman_destroy_fq(fq);
1162     list_del(&dpaa_fq->list);
1163 
1164     return err;
1165 }
1166 
1167 static int dpaa_fq_free(struct device *dev, struct list_head *list)
1168 {
1169     struct dpaa_fq *dpaa_fq, *tmp;
1170     int err, error;
1171 
1172     err = 0;
1173     list_for_each_entry_safe(dpaa_fq, tmp, list, list) {
1174         error = dpaa_fq_free_entry(dev, (struct qman_fq *)dpaa_fq);
1175         if (error < 0 && err >= 0)
1176             err = error;
1177     }
1178 
1179     return err;
1180 }
1181 
1182 static int dpaa_eth_init_tx_port(struct fman_port *port, struct dpaa_fq *errq,
1183                  struct dpaa_fq *defq,
1184                  struct dpaa_buffer_layout *buf_layout)
1185 {
1186     struct fman_buffer_prefix_content buf_prefix_content;
1187     struct fman_port_params params;
1188     int err;
1189 
1190     memset(&params, 0, sizeof(params));
1191     memset(&buf_prefix_content, 0, sizeof(buf_prefix_content));
1192 
1193     buf_prefix_content.priv_data_size = buf_layout->priv_data_size;
1194     buf_prefix_content.pass_prs_result = true;
1195     buf_prefix_content.pass_hash_result = true;
1196     buf_prefix_content.pass_time_stamp = true;
1197     buf_prefix_content.data_align = DPAA_FD_DATA_ALIGNMENT;
1198 
1199     params.specific_params.non_rx_params.err_fqid = errq->fqid;
1200     params.specific_params.non_rx_params.dflt_fqid = defq->fqid;
1201 
1202     err = fman_port_config(port, &params);
1203     if (err) {
1204         pr_err("%s: fman_port_config failed\n", __func__);
1205         return err;
1206     }
1207 
1208     err = fman_port_cfg_buf_prefix_content(port, &buf_prefix_content);
1209     if (err) {
1210         pr_err("%s: fman_port_cfg_buf_prefix_content failed\n",
1211                __func__);
1212         return err;
1213     }
1214 
1215     err = fman_port_init(port);
1216     if (err)
1217         pr_err("%s: fm_port_init failed\n", __func__);
1218 
1219     return err;
1220 }
1221 
1222 static int dpaa_eth_init_rx_port(struct fman_port *port, struct dpaa_bp *bp,
1223                  struct dpaa_fq *errq,
1224                  struct dpaa_fq *defq, struct dpaa_fq *pcdq,
1225                  struct dpaa_buffer_layout *buf_layout)
1226 {
1227     struct fman_buffer_prefix_content buf_prefix_content;
1228     struct fman_port_rx_params *rx_p;
1229     struct fman_port_params params;
1230     int err;
1231 
1232     memset(&params, 0, sizeof(params));
1233     memset(&buf_prefix_content, 0, sizeof(buf_prefix_content));
1234 
1235     buf_prefix_content.priv_data_size = buf_layout->priv_data_size;
1236     buf_prefix_content.pass_prs_result = true;
1237     buf_prefix_content.pass_hash_result = true;
1238     buf_prefix_content.pass_time_stamp = true;
1239     buf_prefix_content.data_align = DPAA_FD_RX_DATA_ALIGNMENT;
1240 
1241     rx_p = &params.specific_params.rx_params;
1242     rx_p->err_fqid = errq->fqid;
1243     rx_p->dflt_fqid = defq->fqid;
1244     if (pcdq) {
1245         rx_p->pcd_base_fqid = pcdq->fqid;
1246         rx_p->pcd_fqs_count = DPAA_ETH_PCD_RXQ_NUM;
1247     }
1248 
1249     rx_p->ext_buf_pools.num_of_pools_used = 1;
1250     rx_p->ext_buf_pools.ext_buf_pool[0].id =  bp->bpid;
1251     rx_p->ext_buf_pools.ext_buf_pool[0].size = (u16)bp->size;
1252 
1253     err = fman_port_config(port, &params);
1254     if (err) {
1255         pr_err("%s: fman_port_config failed\n", __func__);
1256         return err;
1257     }
1258 
1259     err = fman_port_cfg_buf_prefix_content(port, &buf_prefix_content);
1260     if (err) {
1261         pr_err("%s: fman_port_cfg_buf_prefix_content failed\n",
1262                __func__);
1263         return err;
1264     }
1265 
1266     err = fman_port_init(port);
1267     if (err)
1268         pr_err("%s: fm_port_init failed\n", __func__);
1269 
1270     return err;
1271 }
1272 
1273 static int dpaa_eth_init_ports(struct mac_device *mac_dev,
1274                    struct dpaa_bp *bp,
1275                    struct fm_port_fqs *port_fqs,
1276                    struct dpaa_buffer_layout *buf_layout,
1277                    struct device *dev)
1278 {
1279     struct fman_port *rxport = mac_dev->port[RX];
1280     struct fman_port *txport = mac_dev->port[TX];
1281     int err;
1282 
1283     err = dpaa_eth_init_tx_port(txport, port_fqs->tx_errq,
1284                     port_fqs->tx_defq, &buf_layout[TX]);
1285     if (err)
1286         return err;
1287 
1288     err = dpaa_eth_init_rx_port(rxport, bp, port_fqs->rx_errq,
1289                     port_fqs->rx_defq, port_fqs->rx_pcdq,
1290                     &buf_layout[RX]);
1291 
1292     return err;
1293 }
1294 
1295 static int dpaa_bman_release(const struct dpaa_bp *dpaa_bp,
1296                  struct bm_buffer *bmb, int cnt)
1297 {
1298     int err;
1299 
1300     err = bman_release(dpaa_bp->pool, bmb, cnt);
1301     /* Should never occur, address anyway to avoid leaking the buffers */
1302     if (WARN_ON(err) && dpaa_bp->free_buf_cb)
1303         while (cnt-- > 0)
1304             dpaa_bp->free_buf_cb(dpaa_bp, &bmb[cnt]);
1305 
1306     return cnt;
1307 }
1308 
1309 static void dpaa_release_sgt_members(struct qm_sg_entry *sgt)
1310 {
1311     struct bm_buffer bmb[DPAA_BUFF_RELEASE_MAX];
1312     struct dpaa_bp *dpaa_bp;
1313     int i = 0, j;
1314 
1315     memset(bmb, 0, sizeof(bmb));
1316 
1317     do {
1318         dpaa_bp = dpaa_bpid2pool(sgt[i].bpid);
1319         if (!dpaa_bp)
1320             return;
1321 
1322         j = 0;
1323         do {
1324             WARN_ON(qm_sg_entry_is_ext(&sgt[i]));
1325 
1326             bm_buffer_set64(&bmb[j], qm_sg_entry_get64(&sgt[i]));
1327 
1328             j++; i++;
1329         } while (j < ARRAY_SIZE(bmb) &&
1330                 !qm_sg_entry_is_final(&sgt[i - 1]) &&
1331                 sgt[i - 1].bpid == sgt[i].bpid);
1332 
1333         dpaa_bman_release(dpaa_bp, bmb, j);
1334     } while (!qm_sg_entry_is_final(&sgt[i - 1]));
1335 }
1336 
1337 static void dpaa_fd_release(const struct net_device *net_dev,
1338                 const struct qm_fd *fd)
1339 {
1340     struct qm_sg_entry *sgt;
1341     struct dpaa_bp *dpaa_bp;
1342     struct bm_buffer bmb;
1343     dma_addr_t addr;
1344     void *vaddr;
1345 
1346     bmb.data = 0;
1347     bm_buffer_set64(&bmb, qm_fd_addr(fd));
1348 
1349     dpaa_bp = dpaa_bpid2pool(fd->bpid);
1350     if (!dpaa_bp)
1351         return;
1352 
1353     if (qm_fd_get_format(fd) == qm_fd_sg) {
1354         vaddr = phys_to_virt(qm_fd_addr(fd));
1355         sgt = vaddr + qm_fd_get_offset(fd);
1356 
1357         dma_unmap_page(dpaa_bp->priv->rx_dma_dev, qm_fd_addr(fd),
1358                    DPAA_BP_RAW_SIZE, DMA_FROM_DEVICE);
1359 
1360         dpaa_release_sgt_members(sgt);
1361 
1362         addr = dma_map_page(dpaa_bp->priv->rx_dma_dev,
1363                     virt_to_page(vaddr), 0, DPAA_BP_RAW_SIZE,
1364                     DMA_FROM_DEVICE);
1365         if (dma_mapping_error(dpaa_bp->priv->rx_dma_dev, addr)) {
1366             netdev_err(net_dev, "DMA mapping failed\n");
1367             return;
1368         }
1369         bm_buffer_set64(&bmb, addr);
1370     }
1371 
1372     dpaa_bman_release(dpaa_bp, &bmb, 1);
1373 }
1374 
1375 static void count_ern(struct dpaa_percpu_priv *percpu_priv,
1376               const union qm_mr_entry *msg)
1377 {
1378     switch (msg->ern.rc & QM_MR_RC_MASK) {
1379     case QM_MR_RC_CGR_TAILDROP:
1380         percpu_priv->ern_cnt.cg_tdrop++;
1381         break;
1382     case QM_MR_RC_WRED:
1383         percpu_priv->ern_cnt.wred++;
1384         break;
1385     case QM_MR_RC_ERROR:
1386         percpu_priv->ern_cnt.err_cond++;
1387         break;
1388     case QM_MR_RC_ORPWINDOW_EARLY:
1389         percpu_priv->ern_cnt.early_window++;
1390         break;
1391     case QM_MR_RC_ORPWINDOW_LATE:
1392         percpu_priv->ern_cnt.late_window++;
1393         break;
1394     case QM_MR_RC_FQ_TAILDROP:
1395         percpu_priv->ern_cnt.fq_tdrop++;
1396         break;
1397     case QM_MR_RC_ORPWINDOW_RETIRED:
1398         percpu_priv->ern_cnt.fq_retired++;
1399         break;
1400     case QM_MR_RC_ORP_ZERO:
1401         percpu_priv->ern_cnt.orp_zero++;
1402         break;
1403     }
1404 }
1405 
1406 /* Turn on HW checksum computation for this outgoing frame.
1407  * If the current protocol is not something we support in this regard
1408  * (or if the stack has already computed the SW checksum), we do nothing.
1409  *
1410  * Returns 0 if all goes well (or HW csum doesn't apply), and a negative value
1411  * otherwise.
1412  *
1413  * Note that this function may modify the fd->cmd field and the skb data buffer
1414  * (the Parse Results area).
1415  */
1416 static int dpaa_enable_tx_csum(struct dpaa_priv *priv,
1417                    struct sk_buff *skb,
1418                    struct qm_fd *fd,
1419                    void *parse_results)
1420 {
1421     struct fman_prs_result *parse_result;
1422     u16 ethertype = ntohs(skb->protocol);
1423     struct ipv6hdr *ipv6h = NULL;
1424     struct iphdr *iph;
1425     int retval = 0;
1426     u8 l4_proto;
1427 
1428     if (skb->ip_summed != CHECKSUM_PARTIAL)
1429         return 0;
1430 
1431     /* Note: L3 csum seems to be already computed in sw, but we can't choose
1432      * L4 alone from the FM configuration anyway.
1433      */
1434 
1435     /* Fill in some fields of the Parse Results array, so the FMan
1436      * can find them as if they came from the FMan Parser.
1437      */
1438     parse_result = (struct fman_prs_result *)parse_results;
1439 
1440     /* If we're dealing with VLAN, get the real Ethernet type */
1441     if (ethertype == ETH_P_8021Q) {
1442         /* We can't always assume the MAC header is set correctly
1443          * by the stack, so reset to beginning of skb->data
1444          */
1445         skb_reset_mac_header(skb);
1446         ethertype = ntohs(vlan_eth_hdr(skb)->h_vlan_encapsulated_proto);
1447     }
1448 
1449     /* Fill in the relevant L3 parse result fields
1450      * and read the L4 protocol type
1451      */
1452     switch (ethertype) {
1453     case ETH_P_IP:
1454         parse_result->l3r = cpu_to_be16(FM_L3_PARSE_RESULT_IPV4);
1455         iph = ip_hdr(skb);
1456         WARN_ON(!iph);
1457         l4_proto = iph->protocol;
1458         break;
1459     case ETH_P_IPV6:
1460         parse_result->l3r = cpu_to_be16(FM_L3_PARSE_RESULT_IPV6);
1461         ipv6h = ipv6_hdr(skb);
1462         WARN_ON(!ipv6h);
1463         l4_proto = ipv6h->nexthdr;
1464         break;
1465     default:
1466         /* We shouldn't even be here */
1467         if (net_ratelimit())
1468             netif_alert(priv, tx_err, priv->net_dev,
1469                     "Can't compute HW csum for L3 proto 0x%x\n",
1470                     ntohs(skb->protocol));
1471         retval = -EIO;
1472         goto return_error;
1473     }
1474 
1475     /* Fill in the relevant L4 parse result fields */
1476     switch (l4_proto) {
1477     case IPPROTO_UDP:
1478         parse_result->l4r = FM_L4_PARSE_RESULT_UDP;
1479         break;
1480     case IPPROTO_TCP:
1481         parse_result->l4r = FM_L4_PARSE_RESULT_TCP;
1482         break;
1483     default:
1484         if (net_ratelimit())
1485             netif_alert(priv, tx_err, priv->net_dev,
1486                     "Can't compute HW csum for L4 proto 0x%x\n",
1487                     l4_proto);
1488         retval = -EIO;
1489         goto return_error;
1490     }
1491 
1492     /* At index 0 is IPOffset_1 as defined in the Parse Results */
1493     parse_result->ip_off[0] = (u8)skb_network_offset(skb);
1494     parse_result->l4_off = (u8)skb_transport_offset(skb);
1495 
1496     /* Enable L3 (and L4, if TCP or UDP) HW checksum. */
1497     fd->cmd |= cpu_to_be32(FM_FD_CMD_RPD | FM_FD_CMD_DTC);
1498 
1499     /* On P1023 and similar platforms fd->cmd interpretation could
1500      * be disabled by setting CONTEXT_A bit ICMD; currently this bit
1501      * is not set so we do not need to check; in the future, if/when
1502      * using context_a we need to check this bit
1503      */
1504 
1505 return_error:
1506     return retval;
1507 }
1508 
1509 static int dpaa_bp_add_8_bufs(const struct dpaa_bp *dpaa_bp)
1510 {
1511     struct net_device *net_dev = dpaa_bp->priv->net_dev;
1512     struct bm_buffer bmb[8];
1513     dma_addr_t addr;
1514     struct page *p;
1515     u8 i;
1516 
1517     for (i = 0; i < 8; i++) {
1518         p = dev_alloc_pages(0);
1519         if (unlikely(!p)) {
1520             netdev_err(net_dev, "dev_alloc_pages() failed\n");
1521             goto release_previous_buffs;
1522         }
1523 
1524         addr = dma_map_page(dpaa_bp->priv->rx_dma_dev, p, 0,
1525                     DPAA_BP_RAW_SIZE, DMA_FROM_DEVICE);
1526         if (unlikely(dma_mapping_error(dpaa_bp->priv->rx_dma_dev,
1527                            addr))) {
1528             netdev_err(net_dev, "DMA map failed\n");
1529             goto release_previous_buffs;
1530         }
1531 
1532         bmb[i].data = 0;
1533         bm_buffer_set64(&bmb[i], addr);
1534     }
1535 
1536 release_bufs:
1537     return dpaa_bman_release(dpaa_bp, bmb, i);
1538 
1539 release_previous_buffs:
1540     WARN_ONCE(1, "dpaa_eth: failed to add buffers on Rx\n");
1541 
1542     bm_buffer_set64(&bmb[i], 0);
1543     /* Avoid releasing a completely null buffer; bman_release() requires
1544      * at least one buffer.
1545      */
1546     if (likely(i))
1547         goto release_bufs;
1548 
1549     return 0;
1550 }
1551 
1552 static int dpaa_bp_seed(struct dpaa_bp *dpaa_bp)
1553 {
1554     int i;
1555 
1556     /* Give each CPU an allotment of "config_count" buffers */
1557     for_each_possible_cpu(i) {
1558         int *count_ptr = per_cpu_ptr(dpaa_bp->percpu_count, i);
1559         int j;
1560 
1561         /* Although we access another CPU's counters here
1562          * we do it at boot time so it is safe
1563          */
1564         for (j = 0; j < dpaa_bp->config_count; j += 8)
1565             *count_ptr += dpaa_bp_add_8_bufs(dpaa_bp);
1566     }
1567     return 0;
1568 }
1569 
1570 /* Add buffers/(pages) for Rx processing whenever bpool count falls below
1571  * REFILL_THRESHOLD.
1572  */
1573 static int dpaa_eth_refill_bpool(struct dpaa_bp *dpaa_bp, int *countptr)
1574 {
1575     int count = *countptr;
1576     int new_bufs;
1577 
1578     if (unlikely(count < FSL_DPAA_ETH_REFILL_THRESHOLD)) {
1579         do {
1580             new_bufs = dpaa_bp_add_8_bufs(dpaa_bp);
1581             if (unlikely(!new_bufs)) {
1582                 /* Avoid looping forever if we've temporarily
1583                  * run out of memory. We'll try again at the
1584                  * next NAPI cycle.
1585                  */
1586                 break;
1587             }
1588             count += new_bufs;
1589         } while (count < FSL_DPAA_ETH_MAX_BUF_COUNT);
1590 
1591         *countptr = count;
1592         if (unlikely(count < FSL_DPAA_ETH_MAX_BUF_COUNT))
1593             return -ENOMEM;
1594     }
1595 
1596     return 0;
1597 }
1598 
1599 static int dpaa_eth_refill_bpools(struct dpaa_priv *priv)
1600 {
1601     struct dpaa_bp *dpaa_bp;
1602     int *countptr;
1603 
1604     dpaa_bp = priv->dpaa_bp;
1605     if (!dpaa_bp)
1606         return -EINVAL;
1607     countptr = this_cpu_ptr(dpaa_bp->percpu_count);
1608 
1609     return dpaa_eth_refill_bpool(dpaa_bp, countptr);
1610 }
1611 
1612 /* Cleanup function for outgoing frame descriptors that were built on Tx path,
1613  * either contiguous frames or scatter/gather ones.
1614  * Skb freeing is not handled here.
1615  *
1616  * This function may be called on error paths in the Tx function, so guard
1617  * against cases when not all fd relevant fields were filled in. To avoid
1618  * reading the invalid transmission timestamp for the error paths set ts to
1619  * false.
1620  *
1621  * Return the skb backpointer, since for S/G frames the buffer containing it
1622  * gets freed here.
1623  *
1624  * No skb backpointer is set when transmitting XDP frames. Cleanup the buffer
1625  * and return NULL in this case.
1626  */
1627 static struct sk_buff *dpaa_cleanup_tx_fd(const struct dpaa_priv *priv,
1628                       const struct qm_fd *fd, bool ts)
1629 {
1630     const enum dma_data_direction dma_dir = DMA_TO_DEVICE;
1631     struct device *dev = priv->net_dev->dev.parent;
1632     struct skb_shared_hwtstamps shhwtstamps;
1633     dma_addr_t addr = qm_fd_addr(fd);
1634     void *vaddr = phys_to_virt(addr);
1635     const struct qm_sg_entry *sgt;
1636     struct dpaa_eth_swbp *swbp;
1637     struct sk_buff *skb;
1638     u64 ns;
1639     int i;
1640 
1641     if (unlikely(qm_fd_get_format(fd) == qm_fd_sg)) {
1642         dma_unmap_page(priv->tx_dma_dev, addr,
1643                    qm_fd_get_offset(fd) + DPAA_SGT_SIZE,
1644                    dma_dir);
1645 
1646         /* The sgt buffer has been allocated with netdev_alloc_frag(),
1647          * it's from lowmem.
1648          */
1649         sgt = vaddr + qm_fd_get_offset(fd);
1650 
1651         /* sgt[0] is from lowmem, was dma_map_single()-ed */
1652         dma_unmap_single(priv->tx_dma_dev, qm_sg_addr(&sgt[0]),
1653                  qm_sg_entry_get_len(&sgt[0]), dma_dir);
1654 
1655         /* remaining pages were mapped with skb_frag_dma_map() */
1656         for (i = 1; (i < DPAA_SGT_MAX_ENTRIES) &&
1657              !qm_sg_entry_is_final(&sgt[i - 1]); i++) {
1658             WARN_ON(qm_sg_entry_is_ext(&sgt[i]));
1659 
1660             dma_unmap_page(priv->tx_dma_dev, qm_sg_addr(&sgt[i]),
1661                        qm_sg_entry_get_len(&sgt[i]), dma_dir);
1662         }
1663     } else {
1664         dma_unmap_single(priv->tx_dma_dev, addr,
1665                  qm_fd_get_offset(fd) + qm_fd_get_length(fd),
1666                  dma_dir);
1667     }
1668 
1669     swbp = (struct dpaa_eth_swbp *)vaddr;
1670     skb = swbp->skb;
1671 
1672     /* No skb backpointer is set when running XDP. An xdp_frame
1673      * backpointer is saved instead.
1674      */
1675     if (!skb) {
1676         xdp_return_frame(swbp->xdpf);
1677         return NULL;
1678     }
1679 
1680     /* DMA unmapping is required before accessing the HW provided info */
1681     if (ts && priv->tx_tstamp &&
1682         skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
1683         memset(&shhwtstamps, 0, sizeof(shhwtstamps));
1684 
1685         if (!fman_port_get_tstamp(priv->mac_dev->port[TX], vaddr,
1686                       &ns)) {
1687             shhwtstamps.hwtstamp = ns_to_ktime(ns);
1688             skb_tstamp_tx(skb, &shhwtstamps);
1689         } else {
1690             dev_warn(dev, "fman_port_get_tstamp failed!\n");
1691         }
1692     }
1693 
1694     if (qm_fd_get_format(fd) == qm_fd_sg)
1695         /* Free the page that we allocated on Tx for the SGT */
1696         free_pages((unsigned long)vaddr, 0);
1697 
1698     return skb;
1699 }
1700 
1701 static u8 rx_csum_offload(const struct dpaa_priv *priv, const struct qm_fd *fd)
1702 {
1703     /* The parser has run and performed L4 checksum validation.
1704      * We know there were no parser errors (and implicitly no
1705      * L4 csum error), otherwise we wouldn't be here.
1706      */
1707     if ((priv->net_dev->features & NETIF_F_RXCSUM) &&
1708         (be32_to_cpu(fd->status) & FM_FD_STAT_L4CV))
1709         return CHECKSUM_UNNECESSARY;
1710 
1711     /* We're here because either the parser didn't run or the L4 checksum
1712      * was not verified. This may include the case of a UDP frame with
1713      * checksum zero or an L4 proto other than TCP/UDP
1714      */
1715     return CHECKSUM_NONE;
1716 }
1717 
1718 #define PTR_IS_ALIGNED(x, a) (IS_ALIGNED((unsigned long)(x), (a)))
1719 
1720 /* Build a linear skb around the received buffer.
1721  * We are guaranteed there is enough room at the end of the data buffer to
1722  * accommodate the shared info area of the skb.
1723  */
1724 static struct sk_buff *contig_fd_to_skb(const struct dpaa_priv *priv,
1725                     const struct qm_fd *fd)
1726 {
1727     ssize_t fd_off = qm_fd_get_offset(fd);
1728     dma_addr_t addr = qm_fd_addr(fd);
1729     struct dpaa_bp *dpaa_bp;
1730     struct sk_buff *skb;
1731     void *vaddr;
1732 
1733     vaddr = phys_to_virt(addr);
1734     WARN_ON(!IS_ALIGNED((unsigned long)vaddr, SMP_CACHE_BYTES));
1735 
1736     dpaa_bp = dpaa_bpid2pool(fd->bpid);
1737     if (!dpaa_bp)
1738         goto free_buffer;
1739 
1740     skb = build_skb(vaddr, dpaa_bp->size +
1741             SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
1742     if (WARN_ONCE(!skb, "Build skb failure on Rx\n"))
1743         goto free_buffer;
1744     skb_reserve(skb, fd_off);
1745     skb_put(skb, qm_fd_get_length(fd));
1746 
1747     skb->ip_summed = rx_csum_offload(priv, fd);
1748 
1749     return skb;
1750 
1751 free_buffer:
1752     free_pages((unsigned long)vaddr, 0);
1753     return NULL;
1754 }
1755 
1756 /* Build an skb with the data of the first S/G entry in the linear portion and
1757  * the rest of the frame as skb fragments.
1758  *
1759  * The page fragment holding the S/G Table is recycled here.
1760  */
1761 static struct sk_buff *sg_fd_to_skb(const struct dpaa_priv *priv,
1762                     const struct qm_fd *fd)
1763 {
1764     ssize_t fd_off = qm_fd_get_offset(fd);
1765     dma_addr_t addr = qm_fd_addr(fd);
1766     const struct qm_sg_entry *sgt;
1767     struct page *page, *head_page;
1768     struct dpaa_bp *dpaa_bp;
1769     void *vaddr, *sg_vaddr;
1770     int frag_off, frag_len;
1771     struct sk_buff *skb;
1772     dma_addr_t sg_addr;
1773     int page_offset;
1774     unsigned int sz;
1775     int *count_ptr;
1776     int i, j;
1777 
1778     vaddr = phys_to_virt(addr);
1779     WARN_ON(!IS_ALIGNED((unsigned long)vaddr, SMP_CACHE_BYTES));
1780 
1781     /* Iterate through the SGT entries and add data buffers to the skb */
1782     sgt = vaddr + fd_off;
1783     skb = NULL;
1784     for (i = 0; i < DPAA_SGT_MAX_ENTRIES; i++) {
1785         /* Extension bit is not supported */
1786         WARN_ON(qm_sg_entry_is_ext(&sgt[i]));
1787 
1788         sg_addr = qm_sg_addr(&sgt[i]);
1789         sg_vaddr = phys_to_virt(sg_addr);
1790         WARN_ON(!PTR_IS_ALIGNED(sg_vaddr, SMP_CACHE_BYTES));
1791 
1792         dma_unmap_page(priv->rx_dma_dev, sg_addr,
1793                    DPAA_BP_RAW_SIZE, DMA_FROM_DEVICE);
1794 
1795         /* We may use multiple Rx pools */
1796         dpaa_bp = dpaa_bpid2pool(sgt[i].bpid);
1797         if (!dpaa_bp)
1798             goto free_buffers;
1799 
1800         if (!skb) {
1801             sz = dpaa_bp->size +
1802                 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1803             skb = build_skb(sg_vaddr, sz);
1804             if (WARN_ON(!skb))
1805                 goto free_buffers;
1806 
1807             skb->ip_summed = rx_csum_offload(priv, fd);
1808 
1809             /* Make sure forwarded skbs will have enough space
1810              * on Tx, if extra headers are added.
1811              */
1812             WARN_ON(fd_off != priv->rx_headroom);
1813             skb_reserve(skb, fd_off);
1814             skb_put(skb, qm_sg_entry_get_len(&sgt[i]));
1815         } else {
1816             /* Not the first S/G entry; all data from buffer will
1817              * be added in an skb fragment; fragment index is offset
1818              * by one since first S/G entry was incorporated in the
1819              * linear part of the skb.
1820              *
1821              * Caution: 'page' may be a tail page.
1822              */
1823             page = virt_to_page(sg_vaddr);
1824             head_page = virt_to_head_page(sg_vaddr);
1825 
1826             /* Compute offset in (possibly tail) page */
1827             page_offset = ((unsigned long)sg_vaddr &
1828                     (PAGE_SIZE - 1)) +
1829                 (page_address(page) - page_address(head_page));
1830             /* page_offset only refers to the beginning of sgt[i];
1831              * but the buffer itself may have an internal offset.
1832              */
1833             frag_off = qm_sg_entry_get_off(&sgt[i]) + page_offset;
1834             frag_len = qm_sg_entry_get_len(&sgt[i]);
1835             /* skb_add_rx_frag() does no checking on the page; if
1836              * we pass it a tail page, we'll end up with
1837              * bad page accounting and eventually with segafults.
1838              */
1839             skb_add_rx_frag(skb, i - 1, head_page, frag_off,
1840                     frag_len, dpaa_bp->size);
1841         }
1842 
1843         /* Update the pool count for the current {cpu x bpool} */
1844         count_ptr = this_cpu_ptr(dpaa_bp->percpu_count);
1845         (*count_ptr)--;
1846 
1847         if (qm_sg_entry_is_final(&sgt[i]))
1848             break;
1849     }
1850     WARN_ONCE(i == DPAA_SGT_MAX_ENTRIES, "No final bit on SGT\n");
1851 
1852     /* free the SG table buffer */
1853     free_pages((unsigned long)vaddr, 0);
1854 
1855     return skb;
1856 
1857 free_buffers:
1858     /* free all the SG entries */
1859     for (j = 0; j < DPAA_SGT_MAX_ENTRIES ; j++) {
1860         sg_addr = qm_sg_addr(&sgt[j]);
1861         sg_vaddr = phys_to_virt(sg_addr);
1862         /* all pages 0..i were unmaped */
1863         if (j > i)
1864             dma_unmap_page(priv->rx_dma_dev, qm_sg_addr(&sgt[j]),
1865                        DPAA_BP_RAW_SIZE, DMA_FROM_DEVICE);
1866         free_pages((unsigned long)sg_vaddr, 0);
1867         /* counters 0..i-1 were decremented */
1868         if (j >= i) {
1869             dpaa_bp = dpaa_bpid2pool(sgt[j].bpid);
1870             if (dpaa_bp) {
1871                 count_ptr = this_cpu_ptr(dpaa_bp->percpu_count);
1872                 (*count_ptr)--;
1873             }
1874         }
1875 
1876         if (qm_sg_entry_is_final(&sgt[j]))
1877             break;
1878     }
1879     /* free the SGT fragment */
1880     free_pages((unsigned long)vaddr, 0);
1881 
1882     return NULL;
1883 }
1884 
1885 static int skb_to_contig_fd(struct dpaa_priv *priv,
1886                 struct sk_buff *skb, struct qm_fd *fd,
1887                 int *offset)
1888 {
1889     struct net_device *net_dev = priv->net_dev;
1890     enum dma_data_direction dma_dir;
1891     struct dpaa_eth_swbp *swbp;
1892     unsigned char *buff_start;
1893     dma_addr_t addr;
1894     int err;
1895 
1896     /* We are guaranteed to have at least tx_headroom bytes
1897      * available, so just use that for offset.
1898      */
1899     fd->bpid = FSL_DPAA_BPID_INV;
1900     buff_start = skb->data - priv->tx_headroom;
1901     dma_dir = DMA_TO_DEVICE;
1902 
1903     swbp = (struct dpaa_eth_swbp *)buff_start;
1904     swbp->skb = skb;
1905 
1906     /* Enable L3/L4 hardware checksum computation.
1907      *
1908      * We must do this before dma_map_single(DMA_TO_DEVICE), because we may
1909      * need to write into the skb.
1910      */
1911     err = dpaa_enable_tx_csum(priv, skb, fd,
1912                   buff_start + DPAA_TX_PRIV_DATA_SIZE);
1913     if (unlikely(err < 0)) {
1914         if (net_ratelimit())
1915             netif_err(priv, tx_err, net_dev, "HW csum error: %d\n",
1916                   err);
1917         return err;
1918     }
1919 
1920     /* Fill in the rest of the FD fields */
1921     qm_fd_set_contig(fd, priv->tx_headroom, skb->len);
1922     fd->cmd |= cpu_to_be32(FM_FD_CMD_FCO);
1923 
1924     /* Map the entire buffer size that may be seen by FMan, but no more */
1925     addr = dma_map_single(priv->tx_dma_dev, buff_start,
1926                   priv->tx_headroom + skb->len, dma_dir);
1927     if (unlikely(dma_mapping_error(priv->tx_dma_dev, addr))) {
1928         if (net_ratelimit())
1929             netif_err(priv, tx_err, net_dev, "dma_map_single() failed\n");
1930         return -EINVAL;
1931     }
1932     qm_fd_addr_set64(fd, addr);
1933 
1934     return 0;
1935 }
1936 
1937 static int skb_to_sg_fd(struct dpaa_priv *priv,
1938             struct sk_buff *skb, struct qm_fd *fd)
1939 {
1940     const enum dma_data_direction dma_dir = DMA_TO_DEVICE;
1941     const int nr_frags = skb_shinfo(skb)->nr_frags;
1942     struct net_device *net_dev = priv->net_dev;
1943     struct dpaa_eth_swbp *swbp;
1944     struct qm_sg_entry *sgt;
1945     void *buff_start;
1946     skb_frag_t *frag;
1947     dma_addr_t addr;
1948     size_t frag_len;
1949     struct page *p;
1950     int i, j, err;
1951 
1952     /* get a page to store the SGTable */
1953     p = dev_alloc_pages(0);
1954     if (unlikely(!p)) {
1955         netdev_err(net_dev, "dev_alloc_pages() failed\n");
1956         return -ENOMEM;
1957     }
1958     buff_start = page_address(p);
1959 
1960     /* Enable L3/L4 hardware checksum computation.
1961      *
1962      * We must do this before dma_map_single(DMA_TO_DEVICE), because we may
1963      * need to write into the skb.
1964      */
1965     err = dpaa_enable_tx_csum(priv, skb, fd,
1966                   buff_start + DPAA_TX_PRIV_DATA_SIZE);
1967     if (unlikely(err < 0)) {
1968         if (net_ratelimit())
1969             netif_err(priv, tx_err, net_dev, "HW csum error: %d\n",
1970                   err);
1971         goto csum_failed;
1972     }
1973 
1974     /* SGT[0] is used by the linear part */
1975     sgt = (struct qm_sg_entry *)(buff_start + priv->tx_headroom);
1976     frag_len = skb_headlen(skb);
1977     qm_sg_entry_set_len(&sgt[0], frag_len);
1978     sgt[0].bpid = FSL_DPAA_BPID_INV;
1979     sgt[0].offset = 0;
1980     addr = dma_map_single(priv->tx_dma_dev, skb->data,
1981                   skb_headlen(skb), dma_dir);
1982     if (unlikely(dma_mapping_error(priv->tx_dma_dev, addr))) {
1983         netdev_err(priv->net_dev, "DMA mapping failed\n");
1984         err = -EINVAL;
1985         goto sg0_map_failed;
1986     }
1987     qm_sg_entry_set64(&sgt[0], addr);
1988 
1989     /* populate the rest of SGT entries */
1990     for (i = 0; i < nr_frags; i++) {
1991         frag = &skb_shinfo(skb)->frags[i];
1992         frag_len = skb_frag_size(frag);
1993         WARN_ON(!skb_frag_page(frag));
1994         addr = skb_frag_dma_map(priv->tx_dma_dev, frag, 0,
1995                     frag_len, dma_dir);
1996         if (unlikely(dma_mapping_error(priv->tx_dma_dev, addr))) {
1997             netdev_err(priv->net_dev, "DMA mapping failed\n");
1998             err = -EINVAL;
1999             goto sg_map_failed;
2000         }
2001 
2002         qm_sg_entry_set_len(&sgt[i + 1], frag_len);
2003         sgt[i + 1].bpid = FSL_DPAA_BPID_INV;
2004         sgt[i + 1].offset = 0;
2005 
2006         /* keep the offset in the address */
2007         qm_sg_entry_set64(&sgt[i + 1], addr);
2008     }
2009 
2010     /* Set the final bit in the last used entry of the SGT */
2011     qm_sg_entry_set_f(&sgt[nr_frags], frag_len);
2012 
2013     /* set fd offset to priv->tx_headroom */
2014     qm_fd_set_sg(fd, priv->tx_headroom, skb->len);
2015 
2016     /* DMA map the SGT page */
2017     swbp = (struct dpaa_eth_swbp *)buff_start;
2018     swbp->skb = skb;
2019 
2020     addr = dma_map_page(priv->tx_dma_dev, p, 0,
2021                 priv->tx_headroom + DPAA_SGT_SIZE, dma_dir);
2022     if (unlikely(dma_mapping_error(priv->tx_dma_dev, addr))) {
2023         netdev_err(priv->net_dev, "DMA mapping failed\n");
2024         err = -EINVAL;
2025         goto sgt_map_failed;
2026     }
2027 
2028     fd->bpid = FSL_DPAA_BPID_INV;
2029     fd->cmd |= cpu_to_be32(FM_FD_CMD_FCO);
2030     qm_fd_addr_set64(fd, addr);
2031 
2032     return 0;
2033 
2034 sgt_map_failed:
2035 sg_map_failed:
2036     for (j = 0; j < i; j++)
2037         dma_unmap_page(priv->tx_dma_dev, qm_sg_addr(&sgt[j]),
2038                    qm_sg_entry_get_len(&sgt[j]), dma_dir);
2039 sg0_map_failed:
2040 csum_failed:
2041     free_pages((unsigned long)buff_start, 0);
2042 
2043     return err;
2044 }
2045 
2046 static inline int dpaa_xmit(struct dpaa_priv *priv,
2047                 struct rtnl_link_stats64 *percpu_stats,
2048                 int queue,
2049                 struct qm_fd *fd)
2050 {
2051     struct qman_fq *egress_fq;
2052     int err, i;
2053 
2054     egress_fq = priv->egress_fqs[queue];
2055     if (fd->bpid == FSL_DPAA_BPID_INV)
2056         fd->cmd |= cpu_to_be32(qman_fq_fqid(priv->conf_fqs[queue]));
2057 
2058     /* Trace this Tx fd */
2059     trace_dpaa_tx_fd(priv->net_dev, egress_fq, fd);
2060 
2061     for (i = 0; i < DPAA_ENQUEUE_RETRIES; i++) {
2062         err = qman_enqueue(egress_fq, fd);
2063         if (err != -EBUSY)
2064             break;
2065     }
2066 
2067     if (unlikely(err < 0)) {
2068         percpu_stats->tx_fifo_errors++;
2069         return err;
2070     }
2071 
2072     percpu_stats->tx_packets++;
2073     percpu_stats->tx_bytes += qm_fd_get_length(fd);
2074 
2075     return 0;
2076 }
2077 
2078 #ifdef CONFIG_DPAA_ERRATUM_A050385
2079 static int dpaa_a050385_wa_skb(struct net_device *net_dev, struct sk_buff **s)
2080 {
2081     struct dpaa_priv *priv = netdev_priv(net_dev);
2082     struct sk_buff *new_skb, *skb = *s;
2083     unsigned char *start, i;
2084 
2085     /* check linear buffer alignment */
2086     if (!PTR_IS_ALIGNED(skb->data, DPAA_A050385_ALIGN))
2087         goto workaround;
2088 
2089     /* linear buffers just need to have an aligned start */
2090     if (!skb_is_nonlinear(skb))
2091         return 0;
2092 
2093     /* linear data size for nonlinear skbs needs to be aligned */
2094     if (!IS_ALIGNED(skb_headlen(skb), DPAA_A050385_ALIGN))
2095         goto workaround;
2096 
2097     for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2098         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2099 
2100         /* all fragments need to have aligned start addresses */
2101         if (!IS_ALIGNED(skb_frag_off(frag), DPAA_A050385_ALIGN))
2102             goto workaround;
2103 
2104         /* all but last fragment need to have aligned sizes */
2105         if (!IS_ALIGNED(skb_frag_size(frag), DPAA_A050385_ALIGN) &&
2106             (i < skb_shinfo(skb)->nr_frags - 1))
2107             goto workaround;
2108     }
2109 
2110     return 0;
2111 
2112 workaround:
2113     /* copy all the skb content into a new linear buffer */
2114     new_skb = netdev_alloc_skb(net_dev, skb->len + DPAA_A050385_ALIGN - 1 +
2115                         priv->tx_headroom);
2116     if (!new_skb)
2117         return -ENOMEM;
2118 
2119     /* NET_SKB_PAD bytes already reserved, adding up to tx_headroom */
2120     skb_reserve(new_skb, priv->tx_headroom - NET_SKB_PAD);
2121 
2122     /* Workaround for DPAA_A050385 requires data start to be aligned */
2123     start = PTR_ALIGN(new_skb->data, DPAA_A050385_ALIGN);
2124     if (start - new_skb->data)
2125         skb_reserve(new_skb, start - new_skb->data);
2126 
2127     skb_put(new_skb, skb->len);
2128     skb_copy_bits(skb, 0, new_skb->data, skb->len);
2129     skb_copy_header(new_skb, skb);
2130     new_skb->dev = skb->dev;
2131 
2132     /* Copy relevant timestamp info from the old skb to the new */
2133     if (priv->tx_tstamp) {
2134         skb_shinfo(new_skb)->tx_flags = skb_shinfo(skb)->tx_flags;
2135         skb_shinfo(new_skb)->hwtstamps = skb_shinfo(skb)->hwtstamps;
2136         skb_shinfo(new_skb)->tskey = skb_shinfo(skb)->tskey;
2137         if (skb->sk)
2138             skb_set_owner_w(new_skb, skb->sk);
2139     }
2140 
2141     /* We move the headroom when we align it so we have to reset the
2142      * network and transport header offsets relative to the new data
2143      * pointer. The checksum offload relies on these offsets.
2144      */
2145     skb_set_network_header(new_skb, skb_network_offset(skb));
2146     skb_set_transport_header(new_skb, skb_transport_offset(skb));
2147 
2148     dev_kfree_skb(skb);
2149     *s = new_skb;
2150 
2151     return 0;
2152 }
2153 
2154 static int dpaa_a050385_wa_xdpf(struct dpaa_priv *priv,
2155                 struct xdp_frame **init_xdpf)
2156 {
2157     struct xdp_frame *new_xdpf, *xdpf = *init_xdpf;
2158     void *new_buff, *aligned_data;
2159     struct page *p;
2160     u32 data_shift;
2161     int headroom;
2162 
2163     /* Check the data alignment and make sure the headroom is large
2164      * enough to store the xdpf backpointer. Use an aligned headroom
2165      * value.
2166      *
2167      * Due to alignment constraints, we give XDP access to the full 256
2168      * byte frame headroom. If the XDP program uses all of it, copy the
2169      * data to a new buffer and make room for storing the backpointer.
2170      */
2171     if (PTR_IS_ALIGNED(xdpf->data, DPAA_FD_DATA_ALIGNMENT) &&
2172         xdpf->headroom >= priv->tx_headroom) {
2173         xdpf->headroom = priv->tx_headroom;
2174         return 0;
2175     }
2176 
2177     /* Try to move the data inside the buffer just enough to align it and
2178      * store the xdpf backpointer. If the available headroom isn't large
2179      * enough, resort to allocating a new buffer and copying the data.
2180      */
2181     aligned_data = PTR_ALIGN_DOWN(xdpf->data, DPAA_FD_DATA_ALIGNMENT);
2182     data_shift = xdpf->data - aligned_data;
2183 
2184     /* The XDP frame's headroom needs to be large enough to accommodate
2185      * shifting the data as well as storing the xdpf backpointer.
2186      */
2187     if (xdpf->headroom  >= data_shift + priv->tx_headroom) {
2188         memmove(aligned_data, xdpf->data, xdpf->len);
2189         xdpf->data = aligned_data;
2190         xdpf->headroom = priv->tx_headroom;
2191         return 0;
2192     }
2193 
2194     /* The new xdp_frame is stored in the new buffer. Reserve enough space
2195      * in the headroom for storing it along with the driver's private
2196      * info. The headroom needs to be aligned to DPAA_FD_DATA_ALIGNMENT to
2197      * guarantee the data's alignment in the buffer.
2198      */
2199     headroom = ALIGN(sizeof(*new_xdpf) + priv->tx_headroom,
2200              DPAA_FD_DATA_ALIGNMENT);
2201 
2202     /* Assure the extended headroom and data don't overflow the buffer,
2203      * while maintaining the mandatory tailroom.
2204      */
2205     if (headroom + xdpf->len > DPAA_BP_RAW_SIZE -
2206             SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
2207         return -ENOMEM;
2208 
2209     p = dev_alloc_pages(0);
2210     if (unlikely(!p))
2211         return -ENOMEM;
2212 
2213     /* Copy the data to the new buffer at a properly aligned offset */
2214     new_buff = page_address(p);
2215     memcpy(new_buff + headroom, xdpf->data, xdpf->len);
2216 
2217     /* Create an XDP frame around the new buffer in a similar fashion
2218      * to xdp_convert_buff_to_frame.
2219      */
2220     new_xdpf = new_buff;
2221     new_xdpf->data = new_buff + headroom;
2222     new_xdpf->len = xdpf->len;
2223     new_xdpf->headroom = priv->tx_headroom;
2224     new_xdpf->frame_sz = DPAA_BP_RAW_SIZE;
2225     new_xdpf->mem.type = MEM_TYPE_PAGE_ORDER0;
2226 
2227     /* Release the initial buffer */
2228     xdp_return_frame_rx_napi(xdpf);
2229 
2230     *init_xdpf = new_xdpf;
2231     return 0;
2232 }
2233 #endif
2234 
2235 static netdev_tx_t
2236 dpaa_start_xmit(struct sk_buff *skb, struct net_device *net_dev)
2237 {
2238     const int queue_mapping = skb_get_queue_mapping(skb);
2239     bool nonlinear = skb_is_nonlinear(skb);
2240     struct rtnl_link_stats64 *percpu_stats;
2241     struct dpaa_percpu_priv *percpu_priv;
2242     struct netdev_queue *txq;
2243     struct dpaa_priv *priv;
2244     struct qm_fd fd;
2245     int offset = 0;
2246     int err = 0;
2247 
2248     priv = netdev_priv(net_dev);
2249     percpu_priv = this_cpu_ptr(priv->percpu_priv);
2250     percpu_stats = &percpu_priv->stats;
2251 
2252     qm_fd_clear_fd(&fd);
2253 
2254     if (!nonlinear) {
2255         /* We're going to store the skb backpointer at the beginning
2256          * of the data buffer, so we need a privately owned skb
2257          *
2258          * We've made sure skb is not shared in dev->priv_flags,
2259          * we need to verify the skb head is not cloned
2260          */
2261         if (skb_cow_head(skb, priv->tx_headroom))
2262             goto enomem;
2263 
2264         WARN_ON(skb_is_nonlinear(skb));
2265     }
2266 
2267     /* MAX_SKB_FRAGS is equal or larger than our dpaa_SGT_MAX_ENTRIES;
2268      * make sure we don't feed FMan with more fragments than it supports.
2269      */
2270     if (unlikely(nonlinear &&
2271              (skb_shinfo(skb)->nr_frags >= DPAA_SGT_MAX_ENTRIES))) {
2272         /* If the egress skb contains more fragments than we support
2273          * we have no choice but to linearize it ourselves.
2274          */
2275         if (__skb_linearize(skb))
2276             goto enomem;
2277 
2278         nonlinear = skb_is_nonlinear(skb);
2279     }
2280 
2281 #ifdef CONFIG_DPAA_ERRATUM_A050385
2282     if (unlikely(fman_has_errata_a050385())) {
2283         if (dpaa_a050385_wa_skb(net_dev, &skb))
2284             goto enomem;
2285         nonlinear = skb_is_nonlinear(skb);
2286     }
2287 #endif
2288 
2289     if (nonlinear) {
2290         /* Just create a S/G fd based on the skb */
2291         err = skb_to_sg_fd(priv, skb, &fd);
2292         percpu_priv->tx_frag_skbuffs++;
2293     } else {
2294         /* Create a contig FD from this skb */
2295         err = skb_to_contig_fd(priv, skb, &fd, &offset);
2296     }
2297     if (unlikely(err < 0))
2298         goto skb_to_fd_failed;
2299 
2300     txq = netdev_get_tx_queue(net_dev, queue_mapping);
2301 
2302     /* LLTX requires to do our own update of trans_start */
2303     txq_trans_cond_update(txq);
2304 
2305     if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
2306         fd.cmd |= cpu_to_be32(FM_FD_CMD_UPD);
2307         skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2308     }
2309 
2310     if (likely(dpaa_xmit(priv, percpu_stats, queue_mapping, &fd) == 0))
2311         return NETDEV_TX_OK;
2312 
2313     dpaa_cleanup_tx_fd(priv, &fd, false);
2314 skb_to_fd_failed:
2315 enomem:
2316     percpu_stats->tx_errors++;
2317     dev_kfree_skb(skb);
2318     return NETDEV_TX_OK;
2319 }
2320 
2321 static void dpaa_rx_error(struct net_device *net_dev,
2322               const struct dpaa_priv *priv,
2323               struct dpaa_percpu_priv *percpu_priv,
2324               const struct qm_fd *fd,
2325               u32 fqid)
2326 {
2327     if (net_ratelimit())
2328         netif_err(priv, hw, net_dev, "Err FD status = 0x%08x\n",
2329               be32_to_cpu(fd->status) & FM_FD_STAT_RX_ERRORS);
2330 
2331     percpu_priv->stats.rx_errors++;
2332 
2333     if (be32_to_cpu(fd->status) & FM_FD_ERR_DMA)
2334         percpu_priv->rx_errors.dme++;
2335     if (be32_to_cpu(fd->status) & FM_FD_ERR_PHYSICAL)
2336         percpu_priv->rx_errors.fpe++;
2337     if (be32_to_cpu(fd->status) & FM_FD_ERR_SIZE)
2338         percpu_priv->rx_errors.fse++;
2339     if (be32_to_cpu(fd->status) & FM_FD_ERR_PRS_HDR_ERR)
2340         percpu_priv->rx_errors.phe++;
2341 
2342     dpaa_fd_release(net_dev, fd);
2343 }
2344 
2345 static void dpaa_tx_error(struct net_device *net_dev,
2346               const struct dpaa_priv *priv,
2347               struct dpaa_percpu_priv *percpu_priv,
2348               const struct qm_fd *fd,
2349               u32 fqid)
2350 {
2351     struct sk_buff *skb;
2352 
2353     if (net_ratelimit())
2354         netif_warn(priv, hw, net_dev, "FD status = 0x%08x\n",
2355                be32_to_cpu(fd->status) & FM_FD_STAT_TX_ERRORS);
2356 
2357     percpu_priv->stats.tx_errors++;
2358 
2359     skb = dpaa_cleanup_tx_fd(priv, fd, false);
2360     dev_kfree_skb(skb);
2361 }
2362 
2363 static int dpaa_eth_poll(struct napi_struct *napi, int budget)
2364 {
2365     struct dpaa_napi_portal *np =
2366             container_of(napi, struct dpaa_napi_portal, napi);
2367     int cleaned;
2368 
2369     np->xdp_act = 0;
2370 
2371     cleaned = qman_p_poll_dqrr(np->p, budget);
2372 
2373     if (cleaned < budget) {
2374         napi_complete_done(napi, cleaned);
2375         qman_p_irqsource_add(np->p, QM_PIRQ_DQRI);
2376     } else if (np->down) {
2377         qman_p_irqsource_add(np->p, QM_PIRQ_DQRI);
2378     }
2379 
2380     if (np->xdp_act & XDP_REDIRECT)
2381         xdp_do_flush();
2382 
2383     return cleaned;
2384 }
2385 
2386 static void dpaa_tx_conf(struct net_device *net_dev,
2387              const struct dpaa_priv *priv,
2388              struct dpaa_percpu_priv *percpu_priv,
2389              const struct qm_fd *fd,
2390              u32 fqid)
2391 {
2392     struct sk_buff  *skb;
2393 
2394     if (unlikely(be32_to_cpu(fd->status) & FM_FD_STAT_TX_ERRORS)) {
2395         if (net_ratelimit())
2396             netif_warn(priv, hw, net_dev, "FD status = 0x%08x\n",
2397                    be32_to_cpu(fd->status) &
2398                    FM_FD_STAT_TX_ERRORS);
2399 
2400         percpu_priv->stats.tx_errors++;
2401     }
2402 
2403     percpu_priv->tx_confirm++;
2404 
2405     skb = dpaa_cleanup_tx_fd(priv, fd, true);
2406 
2407     consume_skb(skb);
2408 }
2409 
2410 static inline int dpaa_eth_napi_schedule(struct dpaa_percpu_priv *percpu_priv,
2411                      struct qman_portal *portal, bool sched_napi)
2412 {
2413     if (sched_napi) {
2414         /* Disable QMan IRQ and invoke NAPI */
2415         qman_p_irqsource_remove(portal, QM_PIRQ_DQRI);
2416 
2417         percpu_priv->np.p = portal;
2418         napi_schedule(&percpu_priv->np.napi);
2419         percpu_priv->in_interrupt++;
2420         return 1;
2421     }
2422     return 0;
2423 }
2424 
2425 static enum qman_cb_dqrr_result rx_error_dqrr(struct qman_portal *portal,
2426                           struct qman_fq *fq,
2427                           const struct qm_dqrr_entry *dq,
2428                           bool sched_napi)
2429 {
2430     struct dpaa_fq *dpaa_fq = container_of(fq, struct dpaa_fq, fq_base);
2431     struct dpaa_percpu_priv *percpu_priv;
2432     struct net_device *net_dev;
2433     struct dpaa_bp *dpaa_bp;
2434     struct dpaa_priv *priv;
2435 
2436     net_dev = dpaa_fq->net_dev;
2437     priv = netdev_priv(net_dev);
2438     dpaa_bp = dpaa_bpid2pool(dq->fd.bpid);
2439     if (!dpaa_bp)
2440         return qman_cb_dqrr_consume;
2441 
2442     percpu_priv = this_cpu_ptr(priv->percpu_priv);
2443 
2444     if (dpaa_eth_napi_schedule(percpu_priv, portal, sched_napi))
2445         return qman_cb_dqrr_stop;
2446 
2447     dpaa_eth_refill_bpools(priv);
2448     dpaa_rx_error(net_dev, priv, percpu_priv, &dq->fd, fq->fqid);
2449 
2450     return qman_cb_dqrr_consume;
2451 }
2452 
2453 static int dpaa_xdp_xmit_frame(struct net_device *net_dev,
2454                    struct xdp_frame *xdpf)
2455 {
2456     struct dpaa_priv *priv = netdev_priv(net_dev);
2457     struct rtnl_link_stats64 *percpu_stats;
2458     struct dpaa_percpu_priv *percpu_priv;
2459     struct dpaa_eth_swbp *swbp;
2460     struct netdev_queue *txq;
2461     void *buff_start;
2462     struct qm_fd fd;
2463     dma_addr_t addr;
2464     int err;
2465 
2466     percpu_priv = this_cpu_ptr(priv->percpu_priv);
2467     percpu_stats = &percpu_priv->stats;
2468 
2469 #ifdef CONFIG_DPAA_ERRATUM_A050385
2470     if (unlikely(fman_has_errata_a050385())) {
2471         if (dpaa_a050385_wa_xdpf(priv, &xdpf)) {
2472             err = -ENOMEM;
2473             goto out_error;
2474         }
2475     }
2476 #endif
2477 
2478     if (xdpf->headroom < DPAA_TX_PRIV_DATA_SIZE) {
2479         err = -EINVAL;
2480         goto out_error;
2481     }
2482 
2483     buff_start = xdpf->data - xdpf->headroom;
2484 
2485     /* Leave empty the skb backpointer at the start of the buffer.
2486      * Save the XDP frame for easy cleanup on confirmation.
2487      */
2488     swbp = (struct dpaa_eth_swbp *)buff_start;
2489     swbp->skb = NULL;
2490     swbp->xdpf = xdpf;
2491 
2492     qm_fd_clear_fd(&fd);
2493     fd.bpid = FSL_DPAA_BPID_INV;
2494     fd.cmd |= cpu_to_be32(FM_FD_CMD_FCO);
2495     qm_fd_set_contig(&fd, xdpf->headroom, xdpf->len);
2496 
2497     addr = dma_map_single(priv->tx_dma_dev, buff_start,
2498                   xdpf->headroom + xdpf->len,
2499                   DMA_TO_DEVICE);
2500     if (unlikely(dma_mapping_error(priv->tx_dma_dev, addr))) {
2501         err = -EINVAL;
2502         goto out_error;
2503     }
2504 
2505     qm_fd_addr_set64(&fd, addr);
2506 
2507     /* Bump the trans_start */
2508     txq = netdev_get_tx_queue(net_dev, smp_processor_id());
2509     txq_trans_cond_update(txq);
2510 
2511     err = dpaa_xmit(priv, percpu_stats, smp_processor_id(), &fd);
2512     if (err) {
2513         dma_unmap_single(priv->tx_dma_dev, addr,
2514                  qm_fd_get_offset(&fd) + qm_fd_get_length(&fd),
2515                  DMA_TO_DEVICE);
2516         goto out_error;
2517     }
2518 
2519     return 0;
2520 
2521 out_error:
2522     percpu_stats->tx_errors++;
2523     return err;
2524 }
2525 
2526 static u32 dpaa_run_xdp(struct dpaa_priv *priv, struct qm_fd *fd, void *vaddr,
2527             struct dpaa_fq *dpaa_fq, unsigned int *xdp_meta_len)
2528 {
2529     ssize_t fd_off = qm_fd_get_offset(fd);
2530     struct bpf_prog *xdp_prog;
2531     struct xdp_frame *xdpf;
2532     struct xdp_buff xdp;
2533     u32 xdp_act;
2534     int err;
2535 
2536     xdp_prog = READ_ONCE(priv->xdp_prog);
2537     if (!xdp_prog)
2538         return XDP_PASS;
2539 
2540     xdp_init_buff(&xdp, DPAA_BP_RAW_SIZE - DPAA_TX_PRIV_DATA_SIZE,
2541               &dpaa_fq->xdp_rxq);
2542     xdp_prepare_buff(&xdp, vaddr + fd_off - XDP_PACKET_HEADROOM,
2543              XDP_PACKET_HEADROOM, qm_fd_get_length(fd), true);
2544 
2545     /* We reserve a fixed headroom of 256 bytes under the erratum and we
2546      * offer it all to XDP programs to use. If no room is left for the
2547      * xdpf backpointer on TX, we will need to copy the data.
2548      * Disable metadata support since data realignments might be required
2549      * and the information can be lost.
2550      */
2551 #ifdef CONFIG_DPAA_ERRATUM_A050385
2552     if (unlikely(fman_has_errata_a050385())) {
2553         xdp_set_data_meta_invalid(&xdp);
2554         xdp.data_hard_start = vaddr;
2555         xdp.frame_sz = DPAA_BP_RAW_SIZE;
2556     }
2557 #endif
2558 
2559     xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
2560 
2561     /* Update the length and the offset of the FD */
2562     qm_fd_set_contig(fd, xdp.data - vaddr, xdp.data_end - xdp.data);
2563 
2564     switch (xdp_act) {
2565     case XDP_PASS:
2566 #ifdef CONFIG_DPAA_ERRATUM_A050385
2567         *xdp_meta_len = xdp_data_meta_unsupported(&xdp) ? 0 :
2568                 xdp.data - xdp.data_meta;
2569 #else
2570         *xdp_meta_len = xdp.data - xdp.data_meta;
2571 #endif
2572         break;
2573     case XDP_TX:
2574         /* We can access the full headroom when sending the frame
2575          * back out
2576          */
2577         xdp.data_hard_start = vaddr;
2578         xdp.frame_sz = DPAA_BP_RAW_SIZE;
2579         xdpf = xdp_convert_buff_to_frame(&xdp);
2580         if (unlikely(!xdpf)) {
2581             free_pages((unsigned long)vaddr, 0);
2582             break;
2583         }
2584 
2585         if (dpaa_xdp_xmit_frame(priv->net_dev, xdpf))
2586             xdp_return_frame_rx_napi(xdpf);
2587 
2588         break;
2589     case XDP_REDIRECT:
2590         /* Allow redirect to use the full headroom */
2591         xdp.data_hard_start = vaddr;
2592         xdp.frame_sz = DPAA_BP_RAW_SIZE;
2593 
2594         err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog);
2595         if (err) {
2596             trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
2597             free_pages((unsigned long)vaddr, 0);
2598         }
2599         break;
2600     default:
2601         bpf_warn_invalid_xdp_action(priv->net_dev, xdp_prog, xdp_act);
2602         fallthrough;
2603     case XDP_ABORTED:
2604         trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
2605         fallthrough;
2606     case XDP_DROP:
2607         /* Free the buffer */
2608         free_pages((unsigned long)vaddr, 0);
2609         break;
2610     }
2611 
2612     return xdp_act;
2613 }
2614 
2615 static enum qman_cb_dqrr_result rx_default_dqrr(struct qman_portal *portal,
2616                         struct qman_fq *fq,
2617                         const struct qm_dqrr_entry *dq,
2618                         bool sched_napi)
2619 {
2620     bool ts_valid = false, hash_valid = false;
2621     struct skb_shared_hwtstamps *shhwtstamps;
2622     unsigned int skb_len, xdp_meta_len = 0;
2623     struct rtnl_link_stats64 *percpu_stats;
2624     struct dpaa_percpu_priv *percpu_priv;
2625     const struct qm_fd *fd = &dq->fd;
2626     dma_addr_t addr = qm_fd_addr(fd);
2627     struct dpaa_napi_portal *np;
2628     enum qm_fd_format fd_format;
2629     struct net_device *net_dev;
2630     u32 fd_status, hash_offset;
2631     struct qm_sg_entry *sgt;
2632     struct dpaa_bp *dpaa_bp;
2633     struct dpaa_fq *dpaa_fq;
2634     struct dpaa_priv *priv;
2635     struct sk_buff *skb;
2636     int *count_ptr;
2637     u32 xdp_act;
2638     void *vaddr;
2639     u32 hash;
2640     u64 ns;
2641 
2642     dpaa_fq = container_of(fq, struct dpaa_fq, fq_base);
2643     fd_status = be32_to_cpu(fd->status);
2644     fd_format = qm_fd_get_format(fd);
2645     net_dev = dpaa_fq->net_dev;
2646     priv = netdev_priv(net_dev);
2647     dpaa_bp = dpaa_bpid2pool(dq->fd.bpid);
2648     if (!dpaa_bp)
2649         return qman_cb_dqrr_consume;
2650 
2651     /* Trace the Rx fd */
2652     trace_dpaa_rx_fd(net_dev, fq, &dq->fd);
2653 
2654     percpu_priv = this_cpu_ptr(priv->percpu_priv);
2655     percpu_stats = &percpu_priv->stats;
2656     np = &percpu_priv->np;
2657 
2658     if (unlikely(dpaa_eth_napi_schedule(percpu_priv, portal, sched_napi)))
2659         return qman_cb_dqrr_stop;
2660 
2661     /* Make sure we didn't run out of buffers */
2662     if (unlikely(dpaa_eth_refill_bpools(priv))) {
2663         /* Unable to refill the buffer pool due to insufficient
2664          * system memory. Just release the frame back into the pool,
2665          * otherwise we'll soon end up with an empty buffer pool.
2666          */
2667         dpaa_fd_release(net_dev, &dq->fd);
2668         return qman_cb_dqrr_consume;
2669     }
2670 
2671     if (unlikely(fd_status & FM_FD_STAT_RX_ERRORS) != 0) {
2672         if (net_ratelimit())
2673             netif_warn(priv, hw, net_dev, "FD status = 0x%08x\n",
2674                    fd_status & FM_FD_STAT_RX_ERRORS);
2675 
2676         percpu_stats->rx_errors++;
2677         dpaa_fd_release(net_dev, fd);
2678         return qman_cb_dqrr_consume;
2679     }
2680 
2681     dma_unmap_page(dpaa_bp->priv->rx_dma_dev, addr, DPAA_BP_RAW_SIZE,
2682                DMA_FROM_DEVICE);
2683 
2684     /* prefetch the first 64 bytes of the frame or the SGT start */
2685     vaddr = phys_to_virt(addr);
2686     prefetch(vaddr + qm_fd_get_offset(fd));
2687 
2688     /* The only FD types that we may receive are contig and S/G */
2689     WARN_ON((fd_format != qm_fd_contig) && (fd_format != qm_fd_sg));
2690 
2691     /* Account for either the contig buffer or the SGT buffer (depending on
2692      * which case we were in) having been removed from the pool.
2693      */
2694     count_ptr = this_cpu_ptr(dpaa_bp->percpu_count);
2695     (*count_ptr)--;
2696 
2697     /* Extract the timestamp stored in the headroom before running XDP */
2698     if (priv->rx_tstamp) {
2699         if (!fman_port_get_tstamp(priv->mac_dev->port[RX], vaddr, &ns))
2700             ts_valid = true;
2701         else
2702             WARN_ONCE(1, "fman_port_get_tstamp failed!\n");
2703     }
2704 
2705     /* Extract the hash stored in the headroom before running XDP */
2706     if (net_dev->features & NETIF_F_RXHASH && priv->keygen_in_use &&
2707         !fman_port_get_hash_result_offset(priv->mac_dev->port[RX],
2708                           &hash_offset)) {
2709         hash = be32_to_cpu(*(u32 *)(vaddr + hash_offset));
2710         hash_valid = true;
2711     }
2712 
2713     if (likely(fd_format == qm_fd_contig)) {
2714         xdp_act = dpaa_run_xdp(priv, (struct qm_fd *)fd, vaddr,
2715                        dpaa_fq, &xdp_meta_len);
2716         np->xdp_act |= xdp_act;
2717         if (xdp_act != XDP_PASS) {
2718             percpu_stats->rx_packets++;
2719             percpu_stats->rx_bytes += qm_fd_get_length(fd);
2720             return qman_cb_dqrr_consume;
2721         }
2722         skb = contig_fd_to_skb(priv, fd);
2723     } else {
2724         /* XDP doesn't support S/G frames. Return the fragments to the
2725          * buffer pool and release the SGT.
2726          */
2727         if (READ_ONCE(priv->xdp_prog)) {
2728             WARN_ONCE(1, "S/G frames not supported under XDP\n");
2729             sgt = vaddr + qm_fd_get_offset(fd);
2730             dpaa_release_sgt_members(sgt);
2731             free_pages((unsigned long)vaddr, 0);
2732             return qman_cb_dqrr_consume;
2733         }
2734         skb = sg_fd_to_skb(priv, fd);
2735     }
2736     if (!skb)
2737         return qman_cb_dqrr_consume;
2738 
2739     if (xdp_meta_len)
2740         skb_metadata_set(skb, xdp_meta_len);
2741 
2742     /* Set the previously extracted timestamp */
2743     if (ts_valid) {
2744         shhwtstamps = skb_hwtstamps(skb);
2745         memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2746         shhwtstamps->hwtstamp = ns_to_ktime(ns);
2747     }
2748 
2749     skb->protocol = eth_type_trans(skb, net_dev);
2750 
2751     /* Set the previously extracted hash */
2752     if (hash_valid) {
2753         enum pkt_hash_types type;
2754 
2755         /* if L4 exists, it was used in the hash generation */
2756         type = be32_to_cpu(fd->status) & FM_FD_STAT_L4CV ?
2757             PKT_HASH_TYPE_L4 : PKT_HASH_TYPE_L3;
2758         skb_set_hash(skb, hash, type);
2759     }
2760 
2761     skb_len = skb->len;
2762 
2763     if (unlikely(netif_receive_skb(skb) == NET_RX_DROP)) {
2764         percpu_stats->rx_dropped++;
2765         return qman_cb_dqrr_consume;
2766     }
2767 
2768     percpu_stats->rx_packets++;
2769     percpu_stats->rx_bytes += skb_len;
2770 
2771     return qman_cb_dqrr_consume;
2772 }
2773 
2774 static enum qman_cb_dqrr_result conf_error_dqrr(struct qman_portal *portal,
2775                         struct qman_fq *fq,
2776                         const struct qm_dqrr_entry *dq,
2777                         bool sched_napi)
2778 {
2779     struct dpaa_percpu_priv *percpu_priv;
2780     struct net_device *net_dev;
2781     struct dpaa_priv *priv;
2782 
2783     net_dev = ((struct dpaa_fq *)fq)->net_dev;
2784     priv = netdev_priv(net_dev);
2785 
2786     percpu_priv = this_cpu_ptr(priv->percpu_priv);
2787 
2788     if (dpaa_eth_napi_schedule(percpu_priv, portal, sched_napi))
2789         return qman_cb_dqrr_stop;
2790 
2791     dpaa_tx_error(net_dev, priv, percpu_priv, &dq->fd, fq->fqid);
2792 
2793     return qman_cb_dqrr_consume;
2794 }
2795 
2796 static enum qman_cb_dqrr_result conf_dflt_dqrr(struct qman_portal *portal,
2797                            struct qman_fq *fq,
2798                            const struct qm_dqrr_entry *dq,
2799                            bool sched_napi)
2800 {
2801     struct dpaa_percpu_priv *percpu_priv;
2802     struct net_device *net_dev;
2803     struct dpaa_priv *priv;
2804 
2805     net_dev = ((struct dpaa_fq *)fq)->net_dev;
2806     priv = netdev_priv(net_dev);
2807 
2808     /* Trace the fd */
2809     trace_dpaa_tx_conf_fd(net_dev, fq, &dq->fd);
2810 
2811     percpu_priv = this_cpu_ptr(priv->percpu_priv);
2812 
2813     if (dpaa_eth_napi_schedule(percpu_priv, portal, sched_napi))
2814         return qman_cb_dqrr_stop;
2815 
2816     dpaa_tx_conf(net_dev, priv, percpu_priv, &dq->fd, fq->fqid);
2817 
2818     return qman_cb_dqrr_consume;
2819 }
2820 
2821 static void egress_ern(struct qman_portal *portal,
2822                struct qman_fq *fq,
2823                const union qm_mr_entry *msg)
2824 {
2825     const struct qm_fd *fd = &msg->ern.fd;
2826     struct dpaa_percpu_priv *percpu_priv;
2827     const struct dpaa_priv *priv;
2828     struct net_device *net_dev;
2829     struct sk_buff *skb;
2830 
2831     net_dev = ((struct dpaa_fq *)fq)->net_dev;
2832     priv = netdev_priv(net_dev);
2833     percpu_priv = this_cpu_ptr(priv->percpu_priv);
2834 
2835     percpu_priv->stats.tx_dropped++;
2836     percpu_priv->stats.tx_fifo_errors++;
2837     count_ern(percpu_priv, msg);
2838 
2839     skb = dpaa_cleanup_tx_fd(priv, fd, false);
2840     dev_kfree_skb_any(skb);
2841 }
2842 
2843 static const struct dpaa_fq_cbs dpaa_fq_cbs = {
2844     .rx_defq = { .cb = { .dqrr = rx_default_dqrr } },
2845     .tx_defq = { .cb = { .dqrr = conf_dflt_dqrr } },
2846     .rx_errq = { .cb = { .dqrr = rx_error_dqrr } },
2847     .tx_errq = { .cb = { .dqrr = conf_error_dqrr } },
2848     .egress_ern = { .cb = { .ern = egress_ern } }
2849 };
2850 
2851 static void dpaa_eth_napi_enable(struct dpaa_priv *priv)
2852 {
2853     struct dpaa_percpu_priv *percpu_priv;
2854     int i;
2855 
2856     for_each_online_cpu(i) {
2857         percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
2858 
2859         percpu_priv->np.down = false;
2860         napi_enable(&percpu_priv->np.napi);
2861     }
2862 }
2863 
2864 static void dpaa_eth_napi_disable(struct dpaa_priv *priv)
2865 {
2866     struct dpaa_percpu_priv *percpu_priv;
2867     int i;
2868 
2869     for_each_online_cpu(i) {
2870         percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
2871 
2872         percpu_priv->np.down = true;
2873         napi_disable(&percpu_priv->np.napi);
2874     }
2875 }
2876 
2877 static void dpaa_adjust_link(struct net_device *net_dev)
2878 {
2879     struct mac_device *mac_dev;
2880     struct dpaa_priv *priv;
2881 
2882     priv = netdev_priv(net_dev);
2883     mac_dev = priv->mac_dev;
2884     mac_dev->adjust_link(mac_dev);
2885 }
2886 
2887 /* The Aquantia PHYs are capable of performing rate adaptation */
2888 #define PHY_VEND_AQUANTIA   0x03a1b400
2889 #define PHY_VEND_AQUANTIA2  0x31c31c00
2890 
2891 static int dpaa_phy_init(struct net_device *net_dev)
2892 {
2893     __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
2894     struct mac_device *mac_dev;
2895     struct phy_device *phy_dev;
2896     struct dpaa_priv *priv;
2897     u32 phy_vendor;
2898 
2899     priv = netdev_priv(net_dev);
2900     mac_dev = priv->mac_dev;
2901 
2902     phy_dev = of_phy_connect(net_dev, mac_dev->phy_node,
2903                  &dpaa_adjust_link, 0,
2904                  mac_dev->phy_if);
2905     if (!phy_dev) {
2906         netif_err(priv, ifup, net_dev, "init_phy() failed\n");
2907         return -ENODEV;
2908     }
2909 
2910     phy_vendor = phy_dev->drv->phy_id & GENMASK(31, 10);
2911     /* Unless the PHY is capable of rate adaptation */
2912     if (mac_dev->phy_if != PHY_INTERFACE_MODE_XGMII ||
2913         (phy_vendor != PHY_VEND_AQUANTIA &&
2914          phy_vendor != PHY_VEND_AQUANTIA2)) {
2915         /* remove any features not supported by the controller */
2916         ethtool_convert_legacy_u32_to_link_mode(mask,
2917                             mac_dev->if_support);
2918         linkmode_and(phy_dev->supported, phy_dev->supported, mask);
2919     }
2920 
2921     phy_support_asym_pause(phy_dev);
2922 
2923     mac_dev->phy_dev = phy_dev;
2924     net_dev->phydev = phy_dev;
2925 
2926     return 0;
2927 }
2928 
2929 static int dpaa_open(struct net_device *net_dev)
2930 {
2931     struct mac_device *mac_dev;
2932     struct dpaa_priv *priv;
2933     int err, i;
2934 
2935     priv = netdev_priv(net_dev);
2936     mac_dev = priv->mac_dev;
2937     dpaa_eth_napi_enable(priv);
2938 
2939     err = dpaa_phy_init(net_dev);
2940     if (err)
2941         goto phy_init_failed;
2942 
2943     for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++) {
2944         err = fman_port_enable(mac_dev->port[i]);
2945         if (err)
2946             goto mac_start_failed;
2947     }
2948 
2949     err = priv->mac_dev->start(mac_dev);
2950     if (err < 0) {
2951         netif_err(priv, ifup, net_dev, "mac_dev->start() = %d\n", err);
2952         goto mac_start_failed;
2953     }
2954 
2955     netif_tx_start_all_queues(net_dev);
2956 
2957     return 0;
2958 
2959 mac_start_failed:
2960     for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++)
2961         fman_port_disable(mac_dev->port[i]);
2962 
2963 phy_init_failed:
2964     dpaa_eth_napi_disable(priv);
2965 
2966     return err;
2967 }
2968 
2969 static int dpaa_eth_stop(struct net_device *net_dev)
2970 {
2971     struct dpaa_priv *priv;
2972     int err;
2973 
2974     err = dpaa_stop(net_dev);
2975 
2976     priv = netdev_priv(net_dev);
2977     dpaa_eth_napi_disable(priv);
2978 
2979     return err;
2980 }
2981 
2982 static bool xdp_validate_mtu(struct dpaa_priv *priv, int mtu)
2983 {
2984     int max_contig_data = priv->dpaa_bp->size - priv->rx_headroom;
2985 
2986     /* We do not support S/G fragments when XDP is enabled.
2987      * Limit the MTU in relation to the buffer size.
2988      */
2989     if (mtu + VLAN_ETH_HLEN + ETH_FCS_LEN > max_contig_data) {
2990         dev_warn(priv->net_dev->dev.parent,
2991              "The maximum MTU for XDP is %d\n",
2992              max_contig_data - VLAN_ETH_HLEN - ETH_FCS_LEN);
2993         return false;
2994     }
2995 
2996     return true;
2997 }
2998 
2999 static int dpaa_change_mtu(struct net_device *net_dev, int new_mtu)
3000 {
3001     struct dpaa_priv *priv = netdev_priv(net_dev);
3002 
3003     if (priv->xdp_prog && !xdp_validate_mtu(priv, new_mtu))
3004         return -EINVAL;
3005 
3006     net_dev->mtu = new_mtu;
3007     return 0;
3008 }
3009 
3010 static int dpaa_setup_xdp(struct net_device *net_dev, struct netdev_bpf *bpf)
3011 {
3012     struct dpaa_priv *priv = netdev_priv(net_dev);
3013     struct bpf_prog *old_prog;
3014     int err;
3015     bool up;
3016 
3017     /* S/G fragments are not supported in XDP-mode */
3018     if (bpf->prog && !xdp_validate_mtu(priv, net_dev->mtu)) {
3019         NL_SET_ERR_MSG_MOD(bpf->extack, "MTU too large for XDP");
3020         return -EINVAL;
3021     }
3022 
3023     up = netif_running(net_dev);
3024 
3025     if (up)
3026         dpaa_eth_stop(net_dev);
3027 
3028     old_prog = xchg(&priv->xdp_prog, bpf->prog);
3029     if (old_prog)
3030         bpf_prog_put(old_prog);
3031 
3032     if (up) {
3033         err = dpaa_open(net_dev);
3034         if (err) {
3035             NL_SET_ERR_MSG_MOD(bpf->extack, "dpaa_open() failed");
3036             return err;
3037         }
3038     }
3039 
3040     return 0;
3041 }
3042 
3043 static int dpaa_xdp(struct net_device *net_dev, struct netdev_bpf *xdp)
3044 {
3045     switch (xdp->command) {
3046     case XDP_SETUP_PROG:
3047         return dpaa_setup_xdp(net_dev, xdp);
3048     default:
3049         return -EINVAL;
3050     }
3051 }
3052 
3053 static int dpaa_xdp_xmit(struct net_device *net_dev, int n,
3054              struct xdp_frame **frames, u32 flags)
3055 {
3056     struct xdp_frame *xdpf;
3057     int i, nxmit = 0;
3058 
3059     if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
3060         return -EINVAL;
3061 
3062     if (!netif_running(net_dev))
3063         return -ENETDOWN;
3064 
3065     for (i = 0; i < n; i++) {
3066         xdpf = frames[i];
3067         if (dpaa_xdp_xmit_frame(net_dev, xdpf))
3068             break;
3069         nxmit++;
3070     }
3071 
3072     return nxmit;
3073 }
3074 
3075 static int dpaa_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3076 {
3077     struct dpaa_priv *priv = netdev_priv(dev);
3078     struct hwtstamp_config config;
3079 
3080     if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
3081         return -EFAULT;
3082 
3083     switch (config.tx_type) {
3084     case HWTSTAMP_TX_OFF:
3085         /* Couldn't disable rx/tx timestamping separately.
3086          * Do nothing here.
3087          */
3088         priv->tx_tstamp = false;
3089         break;
3090     case HWTSTAMP_TX_ON:
3091         priv->mac_dev->set_tstamp(priv->mac_dev->fman_mac, true);
3092         priv->tx_tstamp = true;
3093         break;
3094     default:
3095         return -ERANGE;
3096     }
3097 
3098     if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
3099         /* Couldn't disable rx/tx timestamping separately.
3100          * Do nothing here.
3101          */
3102         priv->rx_tstamp = false;
3103     } else {
3104         priv->mac_dev->set_tstamp(priv->mac_dev->fman_mac, true);
3105         priv->rx_tstamp = true;
3106         /* TS is set for all frame types, not only those requested */
3107         config.rx_filter = HWTSTAMP_FILTER_ALL;
3108     }
3109 
3110     return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
3111             -EFAULT : 0;
3112 }
3113 
3114 static int dpaa_ioctl(struct net_device *net_dev, struct ifreq *rq, int cmd)
3115 {
3116     int ret = -EINVAL;
3117 
3118     if (cmd == SIOCGMIIREG) {
3119         if (net_dev->phydev)
3120             return phy_mii_ioctl(net_dev->phydev, rq, cmd);
3121     }
3122 
3123     if (cmd == SIOCSHWTSTAMP)
3124         return dpaa_ts_ioctl(net_dev, rq, cmd);
3125 
3126     return ret;
3127 }
3128 
3129 static const struct net_device_ops dpaa_ops = {
3130     .ndo_open = dpaa_open,
3131     .ndo_start_xmit = dpaa_start_xmit,
3132     .ndo_stop = dpaa_eth_stop,
3133     .ndo_tx_timeout = dpaa_tx_timeout,
3134     .ndo_get_stats64 = dpaa_get_stats64,
3135     .ndo_change_carrier = fixed_phy_change_carrier,
3136     .ndo_set_mac_address = dpaa_set_mac_address,
3137     .ndo_validate_addr = eth_validate_addr,
3138     .ndo_set_rx_mode = dpaa_set_rx_mode,
3139     .ndo_eth_ioctl = dpaa_ioctl,
3140     .ndo_setup_tc = dpaa_setup_tc,
3141     .ndo_change_mtu = dpaa_change_mtu,
3142     .ndo_bpf = dpaa_xdp,
3143     .ndo_xdp_xmit = dpaa_xdp_xmit,
3144 };
3145 
3146 static int dpaa_napi_add(struct net_device *net_dev)
3147 {
3148     struct dpaa_priv *priv = netdev_priv(net_dev);
3149     struct dpaa_percpu_priv *percpu_priv;
3150     int cpu;
3151 
3152     for_each_possible_cpu(cpu) {
3153         percpu_priv = per_cpu_ptr(priv->percpu_priv, cpu);
3154 
3155         netif_napi_add(net_dev, &percpu_priv->np.napi,
3156                    dpaa_eth_poll, NAPI_POLL_WEIGHT);
3157     }
3158 
3159     return 0;
3160 }
3161 
3162 static void dpaa_napi_del(struct net_device *net_dev)
3163 {
3164     struct dpaa_priv *priv = netdev_priv(net_dev);
3165     struct dpaa_percpu_priv *percpu_priv;
3166     int cpu;
3167 
3168     for_each_possible_cpu(cpu) {
3169         percpu_priv = per_cpu_ptr(priv->percpu_priv, cpu);
3170 
3171         netif_napi_del(&percpu_priv->np.napi);
3172     }
3173 }
3174 
3175 static inline void dpaa_bp_free_pf(const struct dpaa_bp *bp,
3176                    struct bm_buffer *bmb)
3177 {
3178     dma_addr_t addr = bm_buf_addr(bmb);
3179 
3180     dma_unmap_page(bp->priv->rx_dma_dev, addr, DPAA_BP_RAW_SIZE,
3181                DMA_FROM_DEVICE);
3182 
3183     skb_free_frag(phys_to_virt(addr));
3184 }
3185 
3186 /* Alloc the dpaa_bp struct and configure default values */
3187 static struct dpaa_bp *dpaa_bp_alloc(struct device *dev)
3188 {
3189     struct dpaa_bp *dpaa_bp;
3190 
3191     dpaa_bp = devm_kzalloc(dev, sizeof(*dpaa_bp), GFP_KERNEL);
3192     if (!dpaa_bp)
3193         return ERR_PTR(-ENOMEM);
3194 
3195     dpaa_bp->bpid = FSL_DPAA_BPID_INV;
3196     dpaa_bp->percpu_count = devm_alloc_percpu(dev, *dpaa_bp->percpu_count);
3197     if (!dpaa_bp->percpu_count)
3198         return ERR_PTR(-ENOMEM);
3199 
3200     dpaa_bp->config_count = FSL_DPAA_ETH_MAX_BUF_COUNT;
3201 
3202     dpaa_bp->seed_cb = dpaa_bp_seed;
3203     dpaa_bp->free_buf_cb = dpaa_bp_free_pf;
3204 
3205     return dpaa_bp;
3206 }
3207 
3208 /* Place all ingress FQs (Rx Default, Rx Error) in a dedicated CGR.
3209  * We won't be sending congestion notifications to FMan; for now, we just use
3210  * this CGR to generate enqueue rejections to FMan in order to drop the frames
3211  * before they reach our ingress queues and eat up memory.
3212  */
3213 static int dpaa_ingress_cgr_init(struct dpaa_priv *priv)
3214 {
3215     struct qm_mcc_initcgr initcgr;
3216     u32 cs_th;
3217     int err;
3218 
3219     err = qman_alloc_cgrid(&priv->ingress_cgr.cgrid);
3220     if (err < 0) {
3221         if (netif_msg_drv(priv))
3222             pr_err("Error %d allocating CGR ID\n", err);
3223         goto out_error;
3224     }
3225 
3226     /* Enable CS TD, but disable Congestion State Change Notifications. */
3227     memset(&initcgr, 0, sizeof(initcgr));
3228     initcgr.we_mask = cpu_to_be16(QM_CGR_WE_CS_THRES);
3229     initcgr.cgr.cscn_en = QM_CGR_EN;
3230     cs_th = DPAA_INGRESS_CS_THRESHOLD;
3231     qm_cgr_cs_thres_set64(&initcgr.cgr.cs_thres, cs_th, 1);
3232 
3233     initcgr.we_mask |= cpu_to_be16(QM_CGR_WE_CSTD_EN);
3234     initcgr.cgr.cstd_en = QM_CGR_EN;
3235 
3236     /* This CGR will be associated with the SWP affined to the current CPU.
3237      * However, we'll place all our ingress FQs in it.
3238      */
3239     err = qman_create_cgr(&priv->ingress_cgr, QMAN_CGR_FLAG_USE_INIT,
3240                   &initcgr);
3241     if (err < 0) {
3242         if (netif_msg_drv(priv))
3243             pr_err("Error %d creating ingress CGR with ID %d\n",
3244                    err, priv->ingress_cgr.cgrid);
3245         qman_release_cgrid(priv->ingress_cgr.cgrid);
3246         goto out_error;
3247     }
3248     if (netif_msg_drv(priv))
3249         pr_debug("Created ingress CGR %d for netdev with hwaddr %pM\n",
3250              priv->ingress_cgr.cgrid, priv->mac_dev->addr);
3251 
3252     priv->use_ingress_cgr = true;
3253 
3254 out_error:
3255     return err;
3256 }
3257 
3258 static u16 dpaa_get_headroom(struct dpaa_buffer_layout *bl,
3259                  enum port_type port)
3260 {
3261     u16 headroom;
3262 
3263     /* The frame headroom must accommodate:
3264      * - the driver private data area
3265      * - parse results, hash results, timestamp if selected
3266      * If either hash results or time stamp are selected, both will
3267      * be copied to/from the frame headroom, as TS is located between PR and
3268      * HR in the IC and IC copy size has a granularity of 16bytes
3269      * (see description of FMBM_RICP and FMBM_TICP registers in DPAARM)
3270      *
3271      * Also make sure the headroom is a multiple of data_align bytes
3272      */
3273     headroom = (u16)(bl[port].priv_data_size + DPAA_HWA_SIZE);
3274 
3275     if (port == RX) {
3276 #ifdef CONFIG_DPAA_ERRATUM_A050385
3277         if (unlikely(fman_has_errata_a050385()))
3278             headroom = XDP_PACKET_HEADROOM;
3279 #endif
3280 
3281         return ALIGN(headroom, DPAA_FD_RX_DATA_ALIGNMENT);
3282     } else {
3283         return ALIGN(headroom, DPAA_FD_DATA_ALIGNMENT);
3284     }
3285 }
3286 
3287 static int dpaa_eth_probe(struct platform_device *pdev)
3288 {
3289     struct net_device *net_dev = NULL;
3290     struct dpaa_bp *dpaa_bp = NULL;
3291     struct dpaa_fq *dpaa_fq, *tmp;
3292     struct dpaa_priv *priv = NULL;
3293     struct fm_port_fqs port_fqs;
3294     struct mac_device *mac_dev;
3295     int err = 0, channel;
3296     struct device *dev;
3297 
3298     dev = &pdev->dev;
3299 
3300     err = bman_is_probed();
3301     if (!err)
3302         return -EPROBE_DEFER;
3303     if (err < 0) {
3304         dev_err(dev, "failing probe due to bman probe error\n");
3305         return -ENODEV;
3306     }
3307     err = qman_is_probed();
3308     if (!err)
3309         return -EPROBE_DEFER;
3310     if (err < 0) {
3311         dev_err(dev, "failing probe due to qman probe error\n");
3312         return -ENODEV;
3313     }
3314     err = bman_portals_probed();
3315     if (!err)
3316         return -EPROBE_DEFER;
3317     if (err < 0) {
3318         dev_err(dev,
3319             "failing probe due to bman portals probe error\n");
3320         return -ENODEV;
3321     }
3322     err = qman_portals_probed();
3323     if (!err)
3324         return -EPROBE_DEFER;
3325     if (err < 0) {
3326         dev_err(dev,
3327             "failing probe due to qman portals probe error\n");
3328         return -ENODEV;
3329     }
3330 
3331     /* Allocate this early, so we can store relevant information in
3332      * the private area
3333      */
3334     net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA_ETH_TXQ_NUM);
3335     if (!net_dev) {
3336         dev_err(dev, "alloc_etherdev_mq() failed\n");
3337         return -ENOMEM;
3338     }
3339 
3340     /* Do this here, so we can be verbose early */
3341     SET_NETDEV_DEV(net_dev, dev->parent);
3342     dev_set_drvdata(dev, net_dev);
3343 
3344     priv = netdev_priv(net_dev);
3345     priv->net_dev = net_dev;
3346 
3347     priv->msg_enable = netif_msg_init(debug, DPAA_MSG_DEFAULT);
3348 
3349     mac_dev = dpaa_mac_dev_get(pdev);
3350     if (IS_ERR(mac_dev)) {
3351         netdev_err(net_dev, "dpaa_mac_dev_get() failed\n");
3352         err = PTR_ERR(mac_dev);
3353         goto free_netdev;
3354     }
3355 
3356     /* Devices used for DMA mapping */
3357     priv->rx_dma_dev = fman_port_get_device(mac_dev->port[RX]);
3358     priv->tx_dma_dev = fman_port_get_device(mac_dev->port[TX]);
3359     err = dma_coerce_mask_and_coherent(priv->rx_dma_dev, DMA_BIT_MASK(40));
3360     if (!err)
3361         err = dma_coerce_mask_and_coherent(priv->tx_dma_dev,
3362                            DMA_BIT_MASK(40));
3363     if (err) {
3364         netdev_err(net_dev, "dma_coerce_mask_and_coherent() failed\n");
3365         goto free_netdev;
3366     }
3367 
3368     /* If fsl_fm_max_frm is set to a higher value than the all-common 1500,
3369      * we choose conservatively and let the user explicitly set a higher
3370      * MTU via ifconfig. Otherwise, the user may end up with different MTUs
3371      * in the same LAN.
3372      * If on the other hand fsl_fm_max_frm has been chosen below 1500,
3373      * start with the maximum allowed.
3374      */
3375     net_dev->mtu = min(dpaa_get_max_mtu(), ETH_DATA_LEN);
3376 
3377     netdev_dbg(net_dev, "Setting initial MTU on net device: %d\n",
3378            net_dev->mtu);
3379 
3380     priv->buf_layout[RX].priv_data_size = DPAA_RX_PRIV_DATA_SIZE; /* Rx */
3381     priv->buf_layout[TX].priv_data_size = DPAA_TX_PRIV_DATA_SIZE; /* Tx */
3382 
3383     /* bp init */
3384     dpaa_bp = dpaa_bp_alloc(dev);
3385     if (IS_ERR(dpaa_bp)) {
3386         err = PTR_ERR(dpaa_bp);
3387         goto free_dpaa_bps;
3388     }
3389     /* the raw size of the buffers used for reception */
3390     dpaa_bp->raw_size = DPAA_BP_RAW_SIZE;
3391     /* avoid runtime computations by keeping the usable size here */
3392     dpaa_bp->size = dpaa_bp_size(dpaa_bp->raw_size);
3393     dpaa_bp->priv = priv;
3394 
3395     err = dpaa_bp_alloc_pool(dpaa_bp);
3396     if (err < 0)
3397         goto free_dpaa_bps;
3398     priv->dpaa_bp = dpaa_bp;
3399 
3400     INIT_LIST_HEAD(&priv->dpaa_fq_list);
3401 
3402     memset(&port_fqs, 0, sizeof(port_fqs));
3403 
3404     err = dpaa_alloc_all_fqs(dev, &priv->dpaa_fq_list, &port_fqs);
3405     if (err < 0) {
3406         dev_err(dev, "dpaa_alloc_all_fqs() failed\n");
3407         goto free_dpaa_bps;
3408     }
3409 
3410     priv->mac_dev = mac_dev;
3411 
3412     channel = dpaa_get_channel();
3413     if (channel < 0) {
3414         dev_err(dev, "dpaa_get_channel() failed\n");
3415         err = channel;
3416         goto free_dpaa_bps;
3417     }
3418 
3419     priv->channel = (u16)channel;
3420 
3421     /* Walk the CPUs with affine portals
3422      * and add this pool channel to each's dequeue mask.
3423      */
3424     dpaa_eth_add_channel(priv->channel, &pdev->dev);
3425 
3426     dpaa_fq_setup(priv, &dpaa_fq_cbs, priv->mac_dev->port[TX]);
3427 
3428     /* Create a congestion group for this netdev, with
3429      * dynamically-allocated CGR ID.
3430      * Must be executed after probing the MAC, but before
3431      * assigning the egress FQs to the CGRs.
3432      */
3433     err = dpaa_eth_cgr_init(priv);
3434     if (err < 0) {
3435         dev_err(dev, "Error initializing CGR\n");
3436         goto free_dpaa_bps;
3437     }
3438 
3439     err = dpaa_ingress_cgr_init(priv);
3440     if (err < 0) {
3441         dev_err(dev, "Error initializing ingress CGR\n");
3442         goto delete_egress_cgr;
3443     }
3444 
3445     /* Add the FQs to the interface, and make them active */
3446     list_for_each_entry_safe(dpaa_fq, tmp, &priv->dpaa_fq_list, list) {
3447         err = dpaa_fq_init(dpaa_fq, false);
3448         if (err < 0)
3449             goto free_dpaa_fqs;
3450     }
3451 
3452     priv->tx_headroom = dpaa_get_headroom(priv->buf_layout, TX);
3453     priv->rx_headroom = dpaa_get_headroom(priv->buf_layout, RX);
3454 
3455     /* All real interfaces need their ports initialized */
3456     err = dpaa_eth_init_ports(mac_dev, dpaa_bp, &port_fqs,
3457                   &priv->buf_layout[0], dev);
3458     if (err)
3459         goto free_dpaa_fqs;
3460 
3461     /* Rx traffic distribution based on keygen hashing defaults to on */
3462     priv->keygen_in_use = true;
3463 
3464     priv->percpu_priv = devm_alloc_percpu(dev, *priv->percpu_priv);
3465     if (!priv->percpu_priv) {
3466         dev_err(dev, "devm_alloc_percpu() failed\n");
3467         err = -ENOMEM;
3468         goto free_dpaa_fqs;
3469     }
3470 
3471     priv->num_tc = 1;
3472     netif_set_real_num_tx_queues(net_dev, priv->num_tc * DPAA_TC_TXQ_NUM);
3473 
3474     /* Initialize NAPI */
3475     err = dpaa_napi_add(net_dev);
3476     if (err < 0)
3477         goto delete_dpaa_napi;
3478 
3479     err = dpaa_netdev_init(net_dev, &dpaa_ops, tx_timeout);
3480     if (err < 0)
3481         goto delete_dpaa_napi;
3482 
3483     dpaa_eth_sysfs_init(&net_dev->dev);
3484 
3485     netif_info(priv, probe, net_dev, "Probed interface %s\n",
3486            net_dev->name);
3487 
3488     return 0;
3489 
3490 delete_dpaa_napi:
3491     dpaa_napi_del(net_dev);
3492 free_dpaa_fqs:
3493     dpaa_fq_free(dev, &priv->dpaa_fq_list);
3494     qman_delete_cgr_safe(&priv->ingress_cgr);
3495     qman_release_cgrid(priv->ingress_cgr.cgrid);
3496 delete_egress_cgr:
3497     qman_delete_cgr_safe(&priv->cgr_data.cgr);
3498     qman_release_cgrid(priv->cgr_data.cgr.cgrid);
3499 free_dpaa_bps:
3500     dpaa_bps_free(priv);
3501 free_netdev:
3502     dev_set_drvdata(dev, NULL);
3503     free_netdev(net_dev);
3504 
3505     return err;
3506 }
3507 
3508 static int dpaa_remove(struct platform_device *pdev)
3509 {
3510     struct net_device *net_dev;
3511     struct dpaa_priv *priv;
3512     struct device *dev;
3513     int err;
3514 
3515     dev = &pdev->dev;
3516     net_dev = dev_get_drvdata(dev);
3517 
3518     priv = netdev_priv(net_dev);
3519 
3520     dpaa_eth_sysfs_remove(dev);
3521 
3522     dev_set_drvdata(dev, NULL);
3523     unregister_netdev(net_dev);
3524 
3525     err = dpaa_fq_free(dev, &priv->dpaa_fq_list);
3526 
3527     qman_delete_cgr_safe(&priv->ingress_cgr);
3528     qman_release_cgrid(priv->ingress_cgr.cgrid);
3529     qman_delete_cgr_safe(&priv->cgr_data.cgr);
3530     qman_release_cgrid(priv->cgr_data.cgr.cgrid);
3531 
3532     dpaa_napi_del(net_dev);
3533 
3534     dpaa_bps_free(priv);
3535 
3536     free_netdev(net_dev);
3537 
3538     return err;
3539 }
3540 
3541 static const struct platform_device_id dpaa_devtype[] = {
3542     {
3543         .name = "dpaa-ethernet",
3544         .driver_data = 0,
3545     }, {
3546     }
3547 };
3548 MODULE_DEVICE_TABLE(platform, dpaa_devtype);
3549 
3550 static struct platform_driver dpaa_driver = {
3551     .driver = {
3552         .name = KBUILD_MODNAME,
3553     },
3554     .id_table = dpaa_devtype,
3555     .probe = dpaa_eth_probe,
3556     .remove = dpaa_remove
3557 };
3558 
3559 static int __init dpaa_load(void)
3560 {
3561     int err;
3562 
3563     pr_debug("FSL DPAA Ethernet driver\n");
3564 
3565     /* initialize dpaa_eth mirror values */
3566     dpaa_rx_extra_headroom = fman_get_rx_extra_headroom();
3567     dpaa_max_frm = fman_get_max_frm();
3568 
3569     err = platform_driver_register(&dpaa_driver);
3570     if (err < 0)
3571         pr_err("Error, platform_driver_register() = %d\n", err);
3572 
3573     return err;
3574 }
3575 module_init(dpaa_load);
3576 
3577 static void __exit dpaa_unload(void)
3578 {
3579     platform_driver_unregister(&dpaa_driver);
3580 
3581     /* Only one channel is used and needs to be released after all
3582      * interfaces are removed
3583      */
3584     dpaa_release_channel();
3585 }
3586 module_exit(dpaa_unload);
3587 
3588 MODULE_LICENSE("Dual BSD/GPL");
3589 MODULE_DESCRIPTION("FSL DPAA Ethernet driver");