linux/drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c
<<
>>
Prefs
   1// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
   2/* Copyright 2014-2016 Freescale Semiconductor Inc.
   3 * Copyright 2016-2020 NXP
   4 */
   5#include <linux/init.h>
   6#include <linux/module.h>
   7#include <linux/platform_device.h>
   8#include <linux/etherdevice.h>
   9#include <linux/of_net.h>
  10#include <linux/interrupt.h>
  11#include <linux/msi.h>
  12#include <linux/kthread.h>
  13#include <linux/iommu.h>
  14#include <linux/fsl/mc.h>
  15#include <linux/bpf.h>
  16#include <linux/bpf_trace.h>
  17#include <linux/fsl/ptp_qoriq.h>
  18#include <linux/ptp_classify.h>
  19#include <net/pkt_cls.h>
  20#include <net/sock.h>
  21
  22#include "dpaa2-eth.h"
  23
  24/* CREATE_TRACE_POINTS only needs to be defined once. Other dpa files
  25 * using trace events only need to #include <trace/events/sched.h>
  26 */
  27#define CREATE_TRACE_POINTS
  28#include "dpaa2-eth-trace.h"
  29
  30MODULE_LICENSE("Dual BSD/GPL");
  31MODULE_AUTHOR("Freescale Semiconductor, Inc");
  32MODULE_DESCRIPTION("Freescale DPAA2 Ethernet Driver");
  33
  34struct ptp_qoriq *dpaa2_ptp;
  35EXPORT_SYMBOL(dpaa2_ptp);
  36
  37static void *dpaa2_iova_to_virt(struct iommu_domain *domain,
  38                                dma_addr_t iova_addr)
  39{
  40        phys_addr_t phys_addr;
  41
  42        phys_addr = domain ? iommu_iova_to_phys(domain, iova_addr) : iova_addr;
  43
  44        return phys_to_virt(phys_addr);
  45}
  46
  47static void dpaa2_eth_validate_rx_csum(struct dpaa2_eth_priv *priv,
  48                                       u32 fd_status,
  49                                       struct sk_buff *skb)
  50{
  51        skb_checksum_none_assert(skb);
  52
  53        /* HW checksum validation is disabled, nothing to do here */
  54        if (!(priv->net_dev->features & NETIF_F_RXCSUM))
  55                return;
  56
  57        /* Read checksum validation bits */
  58        if (!((fd_status & DPAA2_FAS_L3CV) &&
  59              (fd_status & DPAA2_FAS_L4CV)))
  60                return;
  61
  62        /* Inform the stack there's no need to compute L3/L4 csum anymore */
  63        skb->ip_summed = CHECKSUM_UNNECESSARY;
  64}
  65
  66/* Free a received FD.
  67 * Not to be used for Tx conf FDs or on any other paths.
  68 */
  69static void dpaa2_eth_free_rx_fd(struct dpaa2_eth_priv *priv,
  70                                 const struct dpaa2_fd *fd,
  71                                 void *vaddr)
  72{
  73        struct device *dev = priv->net_dev->dev.parent;
  74        dma_addr_t addr = dpaa2_fd_get_addr(fd);
  75        u8 fd_format = dpaa2_fd_get_format(fd);
  76        struct dpaa2_sg_entry *sgt;
  77        void *sg_vaddr;
  78        int i;
  79
  80        /* If single buffer frame, just free the data buffer */
  81        if (fd_format == dpaa2_fd_single)
  82                goto free_buf;
  83        else if (fd_format != dpaa2_fd_sg)
  84                /* We don't support any other format */
  85                return;
  86
  87        /* For S/G frames, we first need to free all SG entries
  88         * except the first one, which was taken care of already
  89         */
  90        sgt = vaddr + dpaa2_fd_get_offset(fd);
  91        for (i = 1; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
  92                addr = dpaa2_sg_get_addr(&sgt[i]);
  93                sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
  94                dma_unmap_page(dev, addr, priv->rx_buf_size,
  95                               DMA_BIDIRECTIONAL);
  96
  97                free_pages((unsigned long)sg_vaddr, 0);
  98                if (dpaa2_sg_is_final(&sgt[i]))
  99                        break;
 100        }
 101
 102free_buf:
 103        free_pages((unsigned long)vaddr, 0);
 104}
 105
 106/* Build a linear skb based on a single-buffer frame descriptor */
 107static struct sk_buff *dpaa2_eth_build_linear_skb(struct dpaa2_eth_channel *ch,
 108                                                  const struct dpaa2_fd *fd,
 109                                                  void *fd_vaddr)
 110{
 111        struct sk_buff *skb = NULL;
 112        u16 fd_offset = dpaa2_fd_get_offset(fd);
 113        u32 fd_length = dpaa2_fd_get_len(fd);
 114
 115        ch->buf_count--;
 116
 117        skb = build_skb(fd_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
 118        if (unlikely(!skb))
 119                return NULL;
 120
 121        skb_reserve(skb, fd_offset);
 122        skb_put(skb, fd_length);
 123
 124        return skb;
 125}
 126
 127/* Build a non linear (fragmented) skb based on a S/G table */
 128static struct sk_buff *dpaa2_eth_build_frag_skb(struct dpaa2_eth_priv *priv,
 129                                                struct dpaa2_eth_channel *ch,
 130                                                struct dpaa2_sg_entry *sgt)
 131{
 132        struct sk_buff *skb = NULL;
 133        struct device *dev = priv->net_dev->dev.parent;
 134        void *sg_vaddr;
 135        dma_addr_t sg_addr;
 136        u16 sg_offset;
 137        u32 sg_length;
 138        struct page *page, *head_page;
 139        int page_offset;
 140        int i;
 141
 142        for (i = 0; i < DPAA2_ETH_MAX_SG_ENTRIES; i++) {
 143                struct dpaa2_sg_entry *sge = &sgt[i];
 144
 145                /* NOTE: We only support SG entries in dpaa2_sg_single format,
 146                 * but this is the only format we may receive from HW anyway
 147                 */
 148
 149                /* Get the address and length from the S/G entry */
 150                sg_addr = dpaa2_sg_get_addr(sge);
 151                sg_vaddr = dpaa2_iova_to_virt(priv->iommu_domain, sg_addr);
 152                dma_unmap_page(dev, sg_addr, priv->rx_buf_size,
 153                               DMA_BIDIRECTIONAL);
 154
 155                sg_length = dpaa2_sg_get_len(sge);
 156
 157                if (i == 0) {
 158                        /* We build the skb around the first data buffer */
 159                        skb = build_skb(sg_vaddr, DPAA2_ETH_RX_BUF_RAW_SIZE);
 160                        if (unlikely(!skb)) {
 161                                /* Free the first SG entry now, since we already
 162                                 * unmapped it and obtained the virtual address
 163                                 */
 164                                free_pages((unsigned long)sg_vaddr, 0);
 165
 166                                /* We still need to subtract the buffers used
 167                                 * by this FD from our software counter
 168                                 */
 169                                while (!dpaa2_sg_is_final(&sgt[i]) &&
 170                                       i < DPAA2_ETH_MAX_SG_ENTRIES)
 171                                        i++;
 172                                break;
 173                        }
 174
 175                        sg_offset = dpaa2_sg_get_offset(sge);
 176                        skb_reserve(skb, sg_offset);
 177                        skb_put(skb, sg_length);
 178                } else {
 179                        /* Rest of the data buffers are stored as skb frags */
 180                        page = virt_to_page(sg_vaddr);
 181                        head_page = virt_to_head_page(sg_vaddr);
 182
 183                        /* Offset in page (which may be compound).
 184                         * Data in subsequent SG entries is stored from the
 185                         * beginning of the buffer, so we don't need to add the
 186                         * sg_offset.
 187                         */
 188                        page_offset = ((unsigned long)sg_vaddr &
 189                                (PAGE_SIZE - 1)) +
 190                                (page_address(page) - page_address(head_page));
 191
 192                        skb_add_rx_frag(skb, i - 1, head_page, page_offset,
 193                                        sg_length, priv->rx_buf_size);
 194                }
 195
 196                if (dpaa2_sg_is_final(sge))
 197                        break;
 198        }
 199
 200        WARN_ONCE(i == DPAA2_ETH_MAX_SG_ENTRIES, "Final bit not set in SGT");
 201
 202        /* Count all data buffers + SG table buffer */
 203        ch->buf_count -= i + 2;
 204
 205        return skb;
 206}
 207
 208/* Free buffers acquired from the buffer pool or which were meant to
 209 * be released in the pool
 210 */
 211static void dpaa2_eth_free_bufs(struct dpaa2_eth_priv *priv, u64 *buf_array,
 212                                int count)
 213{
 214        struct device *dev = priv->net_dev->dev.parent;
 215        void *vaddr;
 216        int i;
 217
 218        for (i = 0; i < count; i++) {
 219                vaddr = dpaa2_iova_to_virt(priv->iommu_domain, buf_array[i]);
 220                dma_unmap_page(dev, buf_array[i], priv->rx_buf_size,
 221                               DMA_BIDIRECTIONAL);
 222                free_pages((unsigned long)vaddr, 0);
 223        }
 224}
 225
 226static void dpaa2_eth_recycle_buf(struct dpaa2_eth_priv *priv,
 227                                  struct dpaa2_eth_channel *ch,
 228                                  dma_addr_t addr)
 229{
 230        int retries = 0;
 231        int err;
 232
 233        ch->recycled_bufs[ch->recycled_bufs_cnt++] = addr;
 234        if (ch->recycled_bufs_cnt < DPAA2_ETH_BUFS_PER_CMD)
 235                return;
 236
 237        while ((err = dpaa2_io_service_release(ch->dpio, priv->bpid,
 238                                               ch->recycled_bufs,
 239                                               ch->recycled_bufs_cnt)) == -EBUSY) {
 240                if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
 241                        break;
 242                cpu_relax();
 243        }
 244
 245        if (err) {
 246                dpaa2_eth_free_bufs(priv, ch->recycled_bufs, ch->recycled_bufs_cnt);
 247                ch->buf_count -= ch->recycled_bufs_cnt;
 248        }
 249
 250        ch->recycled_bufs_cnt = 0;
 251}
 252
 253static int dpaa2_eth_xdp_flush(struct dpaa2_eth_priv *priv,
 254                               struct dpaa2_eth_fq *fq,
 255                               struct dpaa2_eth_xdp_fds *xdp_fds)
 256{
 257        int total_enqueued = 0, retries = 0, enqueued;
 258        struct dpaa2_eth_drv_stats *percpu_extras;
 259        int num_fds, err, max_retries;
 260        struct dpaa2_fd *fds;
 261
 262        percpu_extras = this_cpu_ptr(priv->percpu_extras);
 263
 264        /* try to enqueue all the FDs until the max number of retries is hit */
 265        fds = xdp_fds->fds;
 266        num_fds = xdp_fds->num;
 267        max_retries = num_fds * DPAA2_ETH_ENQUEUE_RETRIES;
 268        while (total_enqueued < num_fds && retries < max_retries) {
 269                err = priv->enqueue(priv, fq, &fds[total_enqueued],
 270                                    0, num_fds - total_enqueued, &enqueued);
 271                if (err == -EBUSY) {
 272                        percpu_extras->tx_portal_busy += ++retries;
 273                        continue;
 274                }
 275                total_enqueued += enqueued;
 276        }
 277        xdp_fds->num = 0;
 278
 279        return total_enqueued;
 280}
 281
 282static void dpaa2_eth_xdp_tx_flush(struct dpaa2_eth_priv *priv,
 283                                   struct dpaa2_eth_channel *ch,
 284                                   struct dpaa2_eth_fq *fq)
 285{
 286        struct rtnl_link_stats64 *percpu_stats;
 287        struct dpaa2_fd *fds;
 288        int enqueued, i;
 289
 290        percpu_stats = this_cpu_ptr(priv->percpu_stats);
 291
 292        // enqueue the array of XDP_TX frames
 293        enqueued = dpaa2_eth_xdp_flush(priv, fq, &fq->xdp_tx_fds);
 294
 295        /* update statistics */
 296        percpu_stats->tx_packets += enqueued;
 297        fds = fq->xdp_tx_fds.fds;
 298        for (i = 0; i < enqueued; i++) {
 299                percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
 300                ch->stats.xdp_tx++;
 301        }
 302        for (i = enqueued; i < fq->xdp_tx_fds.num; i++) {
 303                dpaa2_eth_recycle_buf(priv, ch, dpaa2_fd_get_addr(&fds[i]));
 304                percpu_stats->tx_errors++;
 305                ch->stats.xdp_tx_err++;
 306        }
 307        fq->xdp_tx_fds.num = 0;
 308}
 309
 310static void dpaa2_eth_xdp_enqueue(struct dpaa2_eth_priv *priv,
 311                                  struct dpaa2_eth_channel *ch,
 312                                  struct dpaa2_fd *fd,
 313                                  void *buf_start, u16 queue_id)
 314{
 315        struct dpaa2_faead *faead;
 316        struct dpaa2_fd *dest_fd;
 317        struct dpaa2_eth_fq *fq;
 318        u32 ctrl, frc;
 319
 320        /* Mark the egress frame hardware annotation area as valid */
 321        frc = dpaa2_fd_get_frc(fd);
 322        dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
 323        dpaa2_fd_set_ctrl(fd, DPAA2_FD_CTRL_ASAL);
 324
 325        /* Instruct hardware to release the FD buffer directly into
 326         * the buffer pool once transmission is completed, instead of
 327         * sending a Tx confirmation frame to us
 328         */
 329        ctrl = DPAA2_FAEAD_A4V | DPAA2_FAEAD_A2V | DPAA2_FAEAD_EBDDV;
 330        faead = dpaa2_get_faead(buf_start, false);
 331        faead->ctrl = cpu_to_le32(ctrl);
 332        faead->conf_fqid = 0;
 333
 334        fq = &priv->fq[queue_id];
 335        dest_fd = &fq->xdp_tx_fds.fds[fq->xdp_tx_fds.num++];
 336        memcpy(dest_fd, fd, sizeof(*dest_fd));
 337
 338        if (fq->xdp_tx_fds.num < DEV_MAP_BULK_SIZE)
 339                return;
 340
 341        dpaa2_eth_xdp_tx_flush(priv, ch, fq);
 342}
 343
 344static u32 dpaa2_eth_run_xdp(struct dpaa2_eth_priv *priv,
 345                             struct dpaa2_eth_channel *ch,
 346                             struct dpaa2_eth_fq *rx_fq,
 347                             struct dpaa2_fd *fd, void *vaddr)
 348{
 349        dma_addr_t addr = dpaa2_fd_get_addr(fd);
 350        struct bpf_prog *xdp_prog;
 351        struct xdp_buff xdp;
 352        u32 xdp_act = XDP_PASS;
 353        int err, offset;
 354
 355        rcu_read_lock();
 356
 357        xdp_prog = READ_ONCE(ch->xdp.prog);
 358        if (!xdp_prog)
 359                goto out;
 360
 361        offset = dpaa2_fd_get_offset(fd) - XDP_PACKET_HEADROOM;
 362        xdp_init_buff(&xdp, DPAA2_ETH_RX_BUF_RAW_SIZE - offset, &ch->xdp_rxq);
 363        xdp_prepare_buff(&xdp, vaddr + offset, XDP_PACKET_HEADROOM,
 364                         dpaa2_fd_get_len(fd), false);
 365
 366        xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
 367
 368        /* xdp.data pointer may have changed */
 369        dpaa2_fd_set_offset(fd, xdp.data - vaddr);
 370        dpaa2_fd_set_len(fd, xdp.data_end - xdp.data);
 371
 372        switch (xdp_act) {
 373        case XDP_PASS:
 374                break;
 375        case XDP_TX:
 376                dpaa2_eth_xdp_enqueue(priv, ch, fd, vaddr, rx_fq->flowid);
 377                break;
 378        default:
 379                bpf_warn_invalid_xdp_action(xdp_act);
 380                fallthrough;
 381        case XDP_ABORTED:
 382                trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
 383                fallthrough;
 384        case XDP_DROP:
 385                dpaa2_eth_recycle_buf(priv, ch, addr);
 386                ch->stats.xdp_drop++;
 387                break;
 388        case XDP_REDIRECT:
 389                dma_unmap_page(priv->net_dev->dev.parent, addr,
 390                               priv->rx_buf_size, DMA_BIDIRECTIONAL);
 391                ch->buf_count--;
 392
 393                /* Allow redirect use of full headroom */
 394                xdp.data_hard_start = vaddr;
 395                xdp.frame_sz = DPAA2_ETH_RX_BUF_RAW_SIZE;
 396
 397                err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog);
 398                if (unlikely(err)) {
 399                        addr = dma_map_page(priv->net_dev->dev.parent,
 400                                            virt_to_page(vaddr), 0,
 401                                            priv->rx_buf_size, DMA_BIDIRECTIONAL);
 402                        if (unlikely(dma_mapping_error(priv->net_dev->dev.parent, addr))) {
 403                                free_pages((unsigned long)vaddr, 0);
 404                        } else {
 405                                ch->buf_count++;
 406                                dpaa2_eth_recycle_buf(priv, ch, addr);
 407                        }
 408                        ch->stats.xdp_drop++;
 409                } else {
 410                        ch->stats.xdp_redirect++;
 411                }
 412                break;
 413        }
 414
 415        ch->xdp.res |= xdp_act;
 416out:
 417        rcu_read_unlock();
 418        return xdp_act;
 419}
 420
 421static struct sk_buff *dpaa2_eth_copybreak(struct dpaa2_eth_channel *ch,
 422                                           const struct dpaa2_fd *fd,
 423                                           void *fd_vaddr)
 424{
 425        u16 fd_offset = dpaa2_fd_get_offset(fd);
 426        struct dpaa2_eth_priv *priv = ch->priv;
 427        u32 fd_length = dpaa2_fd_get_len(fd);
 428        struct sk_buff *skb = NULL;
 429        unsigned int skb_len;
 430
 431        if (fd_length > priv->rx_copybreak)
 432                return NULL;
 433
 434        skb_len = fd_length + dpaa2_eth_needed_headroom(NULL);
 435
 436        skb = napi_alloc_skb(&ch->napi, skb_len);
 437        if (!skb)
 438                return NULL;
 439
 440        skb_reserve(skb, dpaa2_eth_needed_headroom(NULL));
 441        skb_put(skb, fd_length);
 442
 443        memcpy(skb->data, fd_vaddr + fd_offset, fd_length);
 444
 445        dpaa2_eth_recycle_buf(priv, ch, dpaa2_fd_get_addr(fd));
 446
 447        return skb;
 448}
 449
 450/* Main Rx frame processing routine */
 451static void dpaa2_eth_rx(struct dpaa2_eth_priv *priv,
 452                         struct dpaa2_eth_channel *ch,
 453                         const struct dpaa2_fd *fd,
 454                         struct dpaa2_eth_fq *fq)
 455{
 456        dma_addr_t addr = dpaa2_fd_get_addr(fd);
 457        u8 fd_format = dpaa2_fd_get_format(fd);
 458        void *vaddr;
 459        struct sk_buff *skb;
 460        struct rtnl_link_stats64 *percpu_stats;
 461        struct dpaa2_eth_drv_stats *percpu_extras;
 462        struct device *dev = priv->net_dev->dev.parent;
 463        struct dpaa2_fas *fas;
 464        void *buf_data;
 465        u32 status = 0;
 466        u32 xdp_act;
 467
 468        /* Tracing point */
 469        trace_dpaa2_rx_fd(priv->net_dev, fd);
 470
 471        vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
 472        dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
 473                                DMA_BIDIRECTIONAL);
 474
 475        fas = dpaa2_get_fas(vaddr, false);
 476        prefetch(fas);
 477        buf_data = vaddr + dpaa2_fd_get_offset(fd);
 478        prefetch(buf_data);
 479
 480        percpu_stats = this_cpu_ptr(priv->percpu_stats);
 481        percpu_extras = this_cpu_ptr(priv->percpu_extras);
 482
 483        if (fd_format == dpaa2_fd_single) {
 484                xdp_act = dpaa2_eth_run_xdp(priv, ch, fq, (struct dpaa2_fd *)fd, vaddr);
 485                if (xdp_act != XDP_PASS) {
 486                        percpu_stats->rx_packets++;
 487                        percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
 488                        return;
 489                }
 490
 491                skb = dpaa2_eth_copybreak(ch, fd, vaddr);
 492                if (!skb) {
 493                        dma_unmap_page(dev, addr, priv->rx_buf_size,
 494                                       DMA_BIDIRECTIONAL);
 495                        skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr);
 496                }
 497        } else if (fd_format == dpaa2_fd_sg) {
 498                WARN_ON(priv->xdp_prog);
 499
 500                dma_unmap_page(dev, addr, priv->rx_buf_size,
 501                               DMA_BIDIRECTIONAL);
 502                skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data);
 503                free_pages((unsigned long)vaddr, 0);
 504                percpu_extras->rx_sg_frames++;
 505                percpu_extras->rx_sg_bytes += dpaa2_fd_get_len(fd);
 506        } else {
 507                /* We don't support any other format */
 508                goto err_frame_format;
 509        }
 510
 511        if (unlikely(!skb))
 512                goto err_build_skb;
 513
 514        prefetch(skb->data);
 515
 516        /* Get the timestamp value */
 517        if (priv->rx_tstamp) {
 518                struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
 519                __le64 *ts = dpaa2_get_ts(vaddr, false);
 520                u64 ns;
 521
 522                memset(shhwtstamps, 0, sizeof(*shhwtstamps));
 523
 524                ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
 525                shhwtstamps->hwtstamp = ns_to_ktime(ns);
 526        }
 527
 528        /* Check if we need to validate the L4 csum */
 529        if (likely(dpaa2_fd_get_frc(fd) & DPAA2_FD_FRC_FASV)) {
 530                status = le32_to_cpu(fas->status);
 531                dpaa2_eth_validate_rx_csum(priv, status, skb);
 532        }
 533
 534        skb->protocol = eth_type_trans(skb, priv->net_dev);
 535        skb_record_rx_queue(skb, fq->flowid);
 536
 537        percpu_stats->rx_packets++;
 538        percpu_stats->rx_bytes += dpaa2_fd_get_len(fd);
 539
 540        list_add_tail(&skb->list, ch->rx_list);
 541
 542        return;
 543
 544err_build_skb:
 545        dpaa2_eth_free_rx_fd(priv, fd, vaddr);
 546err_frame_format:
 547        percpu_stats->rx_dropped++;
 548}
 549
 550/* Processing of Rx frames received on the error FQ
 551 * We check and print the error bits and then free the frame
 552 */
 553static void dpaa2_eth_rx_err(struct dpaa2_eth_priv *priv,
 554                             struct dpaa2_eth_channel *ch,
 555                             const struct dpaa2_fd *fd,
 556                             struct dpaa2_eth_fq *fq __always_unused)
 557{
 558        struct device *dev = priv->net_dev->dev.parent;
 559        dma_addr_t addr = dpaa2_fd_get_addr(fd);
 560        u8 fd_format = dpaa2_fd_get_format(fd);
 561        struct rtnl_link_stats64 *percpu_stats;
 562        struct dpaa2_eth_trap_item *trap_item;
 563        struct dpaa2_fapr *fapr;
 564        struct sk_buff *skb;
 565        void *buf_data;
 566        void *vaddr;
 567
 568        vaddr = dpaa2_iova_to_virt(priv->iommu_domain, addr);
 569        dma_sync_single_for_cpu(dev, addr, priv->rx_buf_size,
 570                                DMA_BIDIRECTIONAL);
 571
 572        buf_data = vaddr + dpaa2_fd_get_offset(fd);
 573
 574        if (fd_format == dpaa2_fd_single) {
 575                dma_unmap_page(dev, addr, priv->rx_buf_size,
 576                               DMA_BIDIRECTIONAL);
 577                skb = dpaa2_eth_build_linear_skb(ch, fd, vaddr);
 578        } else if (fd_format == dpaa2_fd_sg) {
 579                dma_unmap_page(dev, addr, priv->rx_buf_size,
 580                               DMA_BIDIRECTIONAL);
 581                skb = dpaa2_eth_build_frag_skb(priv, ch, buf_data);
 582                free_pages((unsigned long)vaddr, 0);
 583        } else {
 584                /* We don't support any other format */
 585                dpaa2_eth_free_rx_fd(priv, fd, vaddr);
 586                goto err_frame_format;
 587        }
 588
 589        fapr = dpaa2_get_fapr(vaddr, false);
 590        trap_item = dpaa2_eth_dl_get_trap(priv, fapr);
 591        if (trap_item)
 592                devlink_trap_report(priv->devlink, skb, trap_item->trap_ctx,
 593                                    &priv->devlink_port, NULL);
 594        consume_skb(skb);
 595
 596err_frame_format:
 597        percpu_stats = this_cpu_ptr(priv->percpu_stats);
 598        percpu_stats->rx_errors++;
 599        ch->buf_count--;
 600}
 601
 602/* Consume all frames pull-dequeued into the store. This is the simplest way to
 603 * make sure we don't accidentally issue another volatile dequeue which would
 604 * overwrite (leak) frames already in the store.
 605 *
 606 * Observance of NAPI budget is not our concern, leaving that to the caller.
 607 */
 608static int dpaa2_eth_consume_frames(struct dpaa2_eth_channel *ch,
 609                                    struct dpaa2_eth_fq **src)
 610{
 611        struct dpaa2_eth_priv *priv = ch->priv;
 612        struct dpaa2_eth_fq *fq = NULL;
 613        struct dpaa2_dq *dq;
 614        const struct dpaa2_fd *fd;
 615        int cleaned = 0, retries = 0;
 616        int is_last;
 617
 618        do {
 619                dq = dpaa2_io_store_next(ch->store, &is_last);
 620                if (unlikely(!dq)) {
 621                        /* If we're here, we *must* have placed a
 622                         * volatile dequeue comnmand, so keep reading through
 623                         * the store until we get some sort of valid response
 624                         * token (either a valid frame or an "empty dequeue")
 625                         */
 626                        if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES) {
 627                                netdev_err_once(priv->net_dev,
 628                                                "Unable to read a valid dequeue response\n");
 629                                return -ETIMEDOUT;
 630                        }
 631                        continue;
 632                }
 633
 634                fd = dpaa2_dq_fd(dq);
 635                fq = (struct dpaa2_eth_fq *)(uintptr_t)dpaa2_dq_fqd_ctx(dq);
 636
 637                fq->consume(priv, ch, fd, fq);
 638                cleaned++;
 639                retries = 0;
 640        } while (!is_last);
 641
 642        if (!cleaned)
 643                return 0;
 644
 645        fq->stats.frames += cleaned;
 646        ch->stats.frames += cleaned;
 647
 648        /* A dequeue operation only pulls frames from a single queue
 649         * into the store. Return the frame queue as an out param.
 650         */
 651        if (src)
 652                *src = fq;
 653
 654        return cleaned;
 655}
 656
 657static int dpaa2_eth_ptp_parse(struct sk_buff *skb,
 658                               u8 *msgtype, u8 *twostep, u8 *udp,
 659                               u16 *correction_offset,
 660                               u16 *origintimestamp_offset)
 661{
 662        unsigned int ptp_class;
 663        struct ptp_header *hdr;
 664        unsigned int type;
 665        u8 *base;
 666
 667        ptp_class = ptp_classify_raw(skb);
 668        if (ptp_class == PTP_CLASS_NONE)
 669                return -EINVAL;
 670
 671        hdr = ptp_parse_header(skb, ptp_class);
 672        if (!hdr)
 673                return -EINVAL;
 674
 675        *msgtype = ptp_get_msgtype(hdr, ptp_class);
 676        *twostep = hdr->flag_field[0] & 0x2;
 677
 678        type = ptp_class & PTP_CLASS_PMASK;
 679        if (type == PTP_CLASS_IPV4 ||
 680            type == PTP_CLASS_IPV6)
 681                *udp = 1;
 682        else
 683                *udp = 0;
 684
 685        base = skb_mac_header(skb);
 686        *correction_offset = (u8 *)&hdr->correction - base;
 687        *origintimestamp_offset = (u8 *)hdr + sizeof(struct ptp_header) - base;
 688
 689        return 0;
 690}
 691
 692/* Configure the egress frame annotation for timestamp update */
 693static void dpaa2_eth_enable_tx_tstamp(struct dpaa2_eth_priv *priv,
 694                                       struct dpaa2_fd *fd,
 695                                       void *buf_start,
 696                                       struct sk_buff *skb)
 697{
 698        struct ptp_tstamp origin_timestamp;
 699        struct dpni_single_step_cfg cfg;
 700        u8 msgtype, twostep, udp;
 701        struct dpaa2_faead *faead;
 702        struct dpaa2_fas *fas;
 703        struct timespec64 ts;
 704        u16 offset1, offset2;
 705        u32 ctrl, frc;
 706        __le64 *ns;
 707        u8 *data;
 708
 709        /* Mark the egress frame annotation area as valid */
 710        frc = dpaa2_fd_get_frc(fd);
 711        dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FAEADV);
 712
 713        /* Set hardware annotation size */
 714        ctrl = dpaa2_fd_get_ctrl(fd);
 715        dpaa2_fd_set_ctrl(fd, ctrl | DPAA2_FD_CTRL_ASAL);
 716
 717        /* enable UPD (update prepanded data) bit in FAEAD field of
 718         * hardware frame annotation area
 719         */
 720        ctrl = DPAA2_FAEAD_A2V | DPAA2_FAEAD_UPDV | DPAA2_FAEAD_UPD;
 721        faead = dpaa2_get_faead(buf_start, true);
 722        faead->ctrl = cpu_to_le32(ctrl);
 723
 724        if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
 725                if (dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
 726                                        &offset1, &offset2) ||
 727                    msgtype != PTP_MSGTYPE_SYNC || twostep) {
 728                        WARN_ONCE(1, "Bad packet for one-step timestamping\n");
 729                        return;
 730                }
 731
 732                /* Mark the frame annotation status as valid */
 733                frc = dpaa2_fd_get_frc(fd);
 734                dpaa2_fd_set_frc(fd, frc | DPAA2_FD_FRC_FASV);
 735
 736                /* Mark the PTP flag for one step timestamping */
 737                fas = dpaa2_get_fas(buf_start, true);
 738                fas->status = cpu_to_le32(DPAA2_FAS_PTP);
 739
 740                dpaa2_ptp->caps.gettime64(&dpaa2_ptp->caps, &ts);
 741                ns = dpaa2_get_ts(buf_start, true);
 742                *ns = cpu_to_le64(timespec64_to_ns(&ts) /
 743                                  DPAA2_PTP_CLK_PERIOD_NS);
 744
 745                /* Update current time to PTP message originTimestamp field */
 746                ns_to_ptp_tstamp(&origin_timestamp, le64_to_cpup(ns));
 747                data = skb_mac_header(skb);
 748                *(__be16 *)(data + offset2) = htons(origin_timestamp.sec_msb);
 749                *(__be32 *)(data + offset2 + 2) =
 750                        htonl(origin_timestamp.sec_lsb);
 751                *(__be32 *)(data + offset2 + 6) = htonl(origin_timestamp.nsec);
 752
 753                cfg.en = 1;
 754                cfg.ch_update = udp;
 755                cfg.offset = offset1;
 756                cfg.peer_delay = 0;
 757
 758                if (dpni_set_single_step_cfg(priv->mc_io, 0, priv->mc_token,
 759                                             &cfg))
 760                        WARN_ONCE(1, "Failed to set single step register");
 761        }
 762}
 763
 764/* Create a frame descriptor based on a fragmented skb */
 765static int dpaa2_eth_build_sg_fd(struct dpaa2_eth_priv *priv,
 766                                 struct sk_buff *skb,
 767                                 struct dpaa2_fd *fd,
 768                                 void **swa_addr)
 769{
 770        struct device *dev = priv->net_dev->dev.parent;
 771        void *sgt_buf = NULL;
 772        dma_addr_t addr;
 773        int nr_frags = skb_shinfo(skb)->nr_frags;
 774        struct dpaa2_sg_entry *sgt;
 775        int i, err;
 776        int sgt_buf_size;
 777        struct scatterlist *scl, *crt_scl;
 778        int num_sg;
 779        int num_dma_bufs;
 780        struct dpaa2_eth_swa *swa;
 781
 782        /* Create and map scatterlist.
 783         * We don't advertise NETIF_F_FRAGLIST, so skb_to_sgvec() will not have
 784         * to go beyond nr_frags+1.
 785         * Note: We don't support chained scatterlists
 786         */
 787        if (unlikely(PAGE_SIZE / sizeof(struct scatterlist) < nr_frags + 1))
 788                return -EINVAL;
 789
 790        scl = kmalloc_array(nr_frags + 1, sizeof(struct scatterlist), GFP_ATOMIC);
 791        if (unlikely(!scl))
 792                return -ENOMEM;
 793
 794        sg_init_table(scl, nr_frags + 1);
 795        num_sg = skb_to_sgvec(skb, scl, 0, skb->len);
 796        if (unlikely(num_sg < 0)) {
 797                err = -ENOMEM;
 798                goto dma_map_sg_failed;
 799        }
 800        num_dma_bufs = dma_map_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
 801        if (unlikely(!num_dma_bufs)) {
 802                err = -ENOMEM;
 803                goto dma_map_sg_failed;
 804        }
 805
 806        /* Prepare the HW SGT structure */
 807        sgt_buf_size = priv->tx_data_offset +
 808                       sizeof(struct dpaa2_sg_entry) *  num_dma_bufs;
 809        sgt_buf = napi_alloc_frag_align(sgt_buf_size, DPAA2_ETH_TX_BUF_ALIGN);
 810        if (unlikely(!sgt_buf)) {
 811                err = -ENOMEM;
 812                goto sgt_buf_alloc_failed;
 813        }
 814        memset(sgt_buf, 0, sgt_buf_size);
 815
 816        sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
 817
 818        /* Fill in the HW SGT structure.
 819         *
 820         * sgt_buf is zeroed out, so the following fields are implicit
 821         * in all sgt entries:
 822         *   - offset is 0
 823         *   - format is 'dpaa2_sg_single'
 824         */
 825        for_each_sg(scl, crt_scl, num_dma_bufs, i) {
 826                dpaa2_sg_set_addr(&sgt[i], sg_dma_address(crt_scl));
 827                dpaa2_sg_set_len(&sgt[i], sg_dma_len(crt_scl));
 828        }
 829        dpaa2_sg_set_final(&sgt[i - 1], true);
 830
 831        /* Store the skb backpointer in the SGT buffer.
 832         * Fit the scatterlist and the number of buffers alongside the
 833         * skb backpointer in the software annotation area. We'll need
 834         * all of them on Tx Conf.
 835         */
 836        *swa_addr = (void *)sgt_buf;
 837        swa = (struct dpaa2_eth_swa *)sgt_buf;
 838        swa->type = DPAA2_ETH_SWA_SG;
 839        swa->sg.skb = skb;
 840        swa->sg.scl = scl;
 841        swa->sg.num_sg = num_sg;
 842        swa->sg.sgt_size = sgt_buf_size;
 843
 844        /* Separately map the SGT buffer */
 845        addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
 846        if (unlikely(dma_mapping_error(dev, addr))) {
 847                err = -ENOMEM;
 848                goto dma_map_single_failed;
 849        }
 850        dpaa2_fd_set_offset(fd, priv->tx_data_offset);
 851        dpaa2_fd_set_format(fd, dpaa2_fd_sg);
 852        dpaa2_fd_set_addr(fd, addr);
 853        dpaa2_fd_set_len(fd, skb->len);
 854        dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
 855
 856        return 0;
 857
 858dma_map_single_failed:
 859        skb_free_frag(sgt_buf);
 860sgt_buf_alloc_failed:
 861        dma_unmap_sg(dev, scl, num_sg, DMA_BIDIRECTIONAL);
 862dma_map_sg_failed:
 863        kfree(scl);
 864        return err;
 865}
 866
 867/* Create a SG frame descriptor based on a linear skb.
 868 *
 869 * This function is used on the Tx path when the skb headroom is not large
 870 * enough for the HW requirements, thus instead of realloc-ing the skb we
 871 * create a SG frame descriptor with only one entry.
 872 */
 873static int dpaa2_eth_build_sg_fd_single_buf(struct dpaa2_eth_priv *priv,
 874                                            struct sk_buff *skb,
 875                                            struct dpaa2_fd *fd,
 876                                            void **swa_addr)
 877{
 878        struct device *dev = priv->net_dev->dev.parent;
 879        struct dpaa2_eth_sgt_cache *sgt_cache;
 880        struct dpaa2_sg_entry *sgt;
 881        struct dpaa2_eth_swa *swa;
 882        dma_addr_t addr, sgt_addr;
 883        void *sgt_buf = NULL;
 884        int sgt_buf_size;
 885        int err;
 886
 887        /* Prepare the HW SGT structure */
 888        sgt_cache = this_cpu_ptr(priv->sgt_cache);
 889        sgt_buf_size = priv->tx_data_offset + sizeof(struct dpaa2_sg_entry);
 890
 891        if (sgt_cache->count == 0)
 892                sgt_buf = kzalloc(sgt_buf_size + DPAA2_ETH_TX_BUF_ALIGN,
 893                                  GFP_ATOMIC);
 894        else
 895                sgt_buf = sgt_cache->buf[--sgt_cache->count];
 896        if (unlikely(!sgt_buf))
 897                return -ENOMEM;
 898
 899        sgt_buf = PTR_ALIGN(sgt_buf, DPAA2_ETH_TX_BUF_ALIGN);
 900        sgt = (struct dpaa2_sg_entry *)(sgt_buf + priv->tx_data_offset);
 901
 902        addr = dma_map_single(dev, skb->data, skb->len, DMA_BIDIRECTIONAL);
 903        if (unlikely(dma_mapping_error(dev, addr))) {
 904                err = -ENOMEM;
 905                goto data_map_failed;
 906        }
 907
 908        /* Fill in the HW SGT structure */
 909        dpaa2_sg_set_addr(sgt, addr);
 910        dpaa2_sg_set_len(sgt, skb->len);
 911        dpaa2_sg_set_final(sgt, true);
 912
 913        /* Store the skb backpointer in the SGT buffer */
 914        *swa_addr = (void *)sgt_buf;
 915        swa = (struct dpaa2_eth_swa *)sgt_buf;
 916        swa->type = DPAA2_ETH_SWA_SINGLE;
 917        swa->single.skb = skb;
 918        swa->single.sgt_size = sgt_buf_size;
 919
 920        /* Separately map the SGT buffer */
 921        sgt_addr = dma_map_single(dev, sgt_buf, sgt_buf_size, DMA_BIDIRECTIONAL);
 922        if (unlikely(dma_mapping_error(dev, sgt_addr))) {
 923                err = -ENOMEM;
 924                goto sgt_map_failed;
 925        }
 926
 927        dpaa2_fd_set_offset(fd, priv->tx_data_offset);
 928        dpaa2_fd_set_format(fd, dpaa2_fd_sg);
 929        dpaa2_fd_set_addr(fd, sgt_addr);
 930        dpaa2_fd_set_len(fd, skb->len);
 931        dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
 932
 933        return 0;
 934
 935sgt_map_failed:
 936        dma_unmap_single(dev, addr, skb->len, DMA_BIDIRECTIONAL);
 937data_map_failed:
 938        if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE)
 939                kfree(sgt_buf);
 940        else
 941                sgt_cache->buf[sgt_cache->count++] = sgt_buf;
 942
 943        return err;
 944}
 945
 946/* Create a frame descriptor based on a linear skb */
 947static int dpaa2_eth_build_single_fd(struct dpaa2_eth_priv *priv,
 948                                     struct sk_buff *skb,
 949                                     struct dpaa2_fd *fd,
 950                                     void **swa_addr)
 951{
 952        struct device *dev = priv->net_dev->dev.parent;
 953        u8 *buffer_start, *aligned_start;
 954        struct dpaa2_eth_swa *swa;
 955        dma_addr_t addr;
 956
 957        buffer_start = skb->data - dpaa2_eth_needed_headroom(skb);
 958
 959        /* If there's enough room to align the FD address, do it.
 960         * It will help hardware optimize accesses.
 961         */
 962        aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
 963                                  DPAA2_ETH_TX_BUF_ALIGN);
 964        if (aligned_start >= skb->head)
 965                buffer_start = aligned_start;
 966
 967        /* Store a backpointer to the skb at the beginning of the buffer
 968         * (in the private data area) such that we can release it
 969         * on Tx confirm
 970         */
 971        *swa_addr = (void *)buffer_start;
 972        swa = (struct dpaa2_eth_swa *)buffer_start;
 973        swa->type = DPAA2_ETH_SWA_SINGLE;
 974        swa->single.skb = skb;
 975
 976        addr = dma_map_single(dev, buffer_start,
 977                              skb_tail_pointer(skb) - buffer_start,
 978                              DMA_BIDIRECTIONAL);
 979        if (unlikely(dma_mapping_error(dev, addr)))
 980                return -ENOMEM;
 981
 982        dpaa2_fd_set_addr(fd, addr);
 983        dpaa2_fd_set_offset(fd, (u16)(skb->data - buffer_start));
 984        dpaa2_fd_set_len(fd, skb->len);
 985        dpaa2_fd_set_format(fd, dpaa2_fd_single);
 986        dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
 987
 988        return 0;
 989}
 990
 991/* FD freeing routine on the Tx path
 992 *
 993 * DMA-unmap and free FD and possibly SGT buffer allocated on Tx. The skb
 994 * back-pointed to is also freed.
 995 * This can be called either from dpaa2_eth_tx_conf() or on the error path of
 996 * dpaa2_eth_tx().
 997 */
 998static void dpaa2_eth_free_tx_fd(struct dpaa2_eth_priv *priv,
 999                                 struct dpaa2_eth_fq *fq,
1000                                 const struct dpaa2_fd *fd, bool in_napi)
1001{
1002        struct device *dev = priv->net_dev->dev.parent;
1003        dma_addr_t fd_addr, sg_addr;
1004        struct sk_buff *skb = NULL;
1005        unsigned char *buffer_start;
1006        struct dpaa2_eth_swa *swa;
1007        u8 fd_format = dpaa2_fd_get_format(fd);
1008        u32 fd_len = dpaa2_fd_get_len(fd);
1009
1010        struct dpaa2_eth_sgt_cache *sgt_cache;
1011        struct dpaa2_sg_entry *sgt;
1012
1013        fd_addr = dpaa2_fd_get_addr(fd);
1014        buffer_start = dpaa2_iova_to_virt(priv->iommu_domain, fd_addr);
1015        swa = (struct dpaa2_eth_swa *)buffer_start;
1016
1017        if (fd_format == dpaa2_fd_single) {
1018                if (swa->type == DPAA2_ETH_SWA_SINGLE) {
1019                        skb = swa->single.skb;
1020                        /* Accessing the skb buffer is safe before dma unmap,
1021                         * because we didn't map the actual skb shell.
1022                         */
1023                        dma_unmap_single(dev, fd_addr,
1024                                         skb_tail_pointer(skb) - buffer_start,
1025                                         DMA_BIDIRECTIONAL);
1026                } else {
1027                        WARN_ONCE(swa->type != DPAA2_ETH_SWA_XDP, "Wrong SWA type");
1028                        dma_unmap_single(dev, fd_addr, swa->xdp.dma_size,
1029                                         DMA_BIDIRECTIONAL);
1030                }
1031        } else if (fd_format == dpaa2_fd_sg) {
1032                if (swa->type == DPAA2_ETH_SWA_SG) {
1033                        skb = swa->sg.skb;
1034
1035                        /* Unmap the scatterlist */
1036                        dma_unmap_sg(dev, swa->sg.scl, swa->sg.num_sg,
1037                                     DMA_BIDIRECTIONAL);
1038                        kfree(swa->sg.scl);
1039
1040                        /* Unmap the SGT buffer */
1041                        dma_unmap_single(dev, fd_addr, swa->sg.sgt_size,
1042                                         DMA_BIDIRECTIONAL);
1043                } else {
1044                        skb = swa->single.skb;
1045
1046                        /* Unmap the SGT Buffer */
1047                        dma_unmap_single(dev, fd_addr, swa->single.sgt_size,
1048                                         DMA_BIDIRECTIONAL);
1049
1050                        sgt = (struct dpaa2_sg_entry *)(buffer_start +
1051                                                        priv->tx_data_offset);
1052                        sg_addr = dpaa2_sg_get_addr(sgt);
1053                        dma_unmap_single(dev, sg_addr, skb->len, DMA_BIDIRECTIONAL);
1054                }
1055        } else {
1056                netdev_dbg(priv->net_dev, "Invalid FD format\n");
1057                return;
1058        }
1059
1060        if (swa->type != DPAA2_ETH_SWA_XDP && in_napi) {
1061                fq->dq_frames++;
1062                fq->dq_bytes += fd_len;
1063        }
1064
1065        if (swa->type == DPAA2_ETH_SWA_XDP) {
1066                xdp_return_frame(swa->xdp.xdpf);
1067                return;
1068        }
1069
1070        /* Get the timestamp value */
1071        if (skb->cb[0] == TX_TSTAMP) {
1072                struct skb_shared_hwtstamps shhwtstamps;
1073                __le64 *ts = dpaa2_get_ts(buffer_start, true);
1074                u64 ns;
1075
1076                memset(&shhwtstamps, 0, sizeof(shhwtstamps));
1077
1078                ns = DPAA2_PTP_CLK_PERIOD_NS * le64_to_cpup(ts);
1079                shhwtstamps.hwtstamp = ns_to_ktime(ns);
1080                skb_tstamp_tx(skb, &shhwtstamps);
1081        } else if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
1082                mutex_unlock(&priv->onestep_tstamp_lock);
1083        }
1084
1085        /* Free SGT buffer allocated on tx */
1086        if (fd_format != dpaa2_fd_single) {
1087                sgt_cache = this_cpu_ptr(priv->sgt_cache);
1088                if (swa->type == DPAA2_ETH_SWA_SG) {
1089                        skb_free_frag(buffer_start);
1090                } else {
1091                        if (sgt_cache->count >= DPAA2_ETH_SGT_CACHE_SIZE)
1092                                kfree(buffer_start);
1093                        else
1094                                sgt_cache->buf[sgt_cache->count++] = buffer_start;
1095                }
1096        }
1097
1098        /* Move on with skb release */
1099        napi_consume_skb(skb, in_napi);
1100}
1101
1102static netdev_tx_t __dpaa2_eth_tx(struct sk_buff *skb,
1103                                  struct net_device *net_dev)
1104{
1105        struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1106        struct dpaa2_fd fd;
1107        struct rtnl_link_stats64 *percpu_stats;
1108        struct dpaa2_eth_drv_stats *percpu_extras;
1109        struct dpaa2_eth_fq *fq;
1110        struct netdev_queue *nq;
1111        u16 queue_mapping;
1112        unsigned int needed_headroom;
1113        u32 fd_len;
1114        u8 prio = 0;
1115        int err, i;
1116        void *swa;
1117
1118        percpu_stats = this_cpu_ptr(priv->percpu_stats);
1119        percpu_extras = this_cpu_ptr(priv->percpu_extras);
1120
1121        needed_headroom = dpaa2_eth_needed_headroom(skb);
1122
1123        /* We'll be holding a back-reference to the skb until Tx Confirmation;
1124         * we don't want that overwritten by a concurrent Tx with a cloned skb.
1125         */
1126        skb = skb_unshare(skb, GFP_ATOMIC);
1127        if (unlikely(!skb)) {
1128                /* skb_unshare() has already freed the skb */
1129                percpu_stats->tx_dropped++;
1130                return NETDEV_TX_OK;
1131        }
1132
1133        /* Setup the FD fields */
1134        memset(&fd, 0, sizeof(fd));
1135
1136        if (skb_is_nonlinear(skb)) {
1137                err = dpaa2_eth_build_sg_fd(priv, skb, &fd, &swa);
1138                percpu_extras->tx_sg_frames++;
1139                percpu_extras->tx_sg_bytes += skb->len;
1140        } else if (skb_headroom(skb) < needed_headroom) {
1141                err = dpaa2_eth_build_sg_fd_single_buf(priv, skb, &fd, &swa);
1142                percpu_extras->tx_sg_frames++;
1143                percpu_extras->tx_sg_bytes += skb->len;
1144                percpu_extras->tx_converted_sg_frames++;
1145                percpu_extras->tx_converted_sg_bytes += skb->len;
1146        } else {
1147                err = dpaa2_eth_build_single_fd(priv, skb, &fd, &swa);
1148        }
1149
1150        if (unlikely(err)) {
1151                percpu_stats->tx_dropped++;
1152                goto err_build_fd;
1153        }
1154
1155        if (skb->cb[0])
1156                dpaa2_eth_enable_tx_tstamp(priv, &fd, swa, skb);
1157
1158        /* Tracing point */
1159        trace_dpaa2_tx_fd(net_dev, &fd);
1160
1161        /* TxConf FQ selection relies on queue id from the stack.
1162         * In case of a forwarded frame from another DPNI interface, we choose
1163         * a queue affined to the same core that processed the Rx frame
1164         */
1165        queue_mapping = skb_get_queue_mapping(skb);
1166
1167        if (net_dev->num_tc) {
1168                prio = netdev_txq_to_tc(net_dev, queue_mapping);
1169                /* Hardware interprets priority level 0 as being the highest,
1170                 * so we need to do a reverse mapping to the netdev tc index
1171                 */
1172                prio = net_dev->num_tc - prio - 1;
1173                /* We have only one FQ array entry for all Tx hardware queues
1174                 * with the same flow id (but different priority levels)
1175                 */
1176                queue_mapping %= dpaa2_eth_queue_count(priv);
1177        }
1178        fq = &priv->fq[queue_mapping];
1179
1180        fd_len = dpaa2_fd_get_len(&fd);
1181        nq = netdev_get_tx_queue(net_dev, queue_mapping);
1182        netdev_tx_sent_queue(nq, fd_len);
1183
1184        /* Everything that happens after this enqueues might race with
1185         * the Tx confirmation callback for this frame
1186         */
1187        for (i = 0; i < DPAA2_ETH_ENQUEUE_RETRIES; i++) {
1188                err = priv->enqueue(priv, fq, &fd, prio, 1, NULL);
1189                if (err != -EBUSY)
1190                        break;
1191        }
1192        percpu_extras->tx_portal_busy += i;
1193        if (unlikely(err < 0)) {
1194                percpu_stats->tx_errors++;
1195                /* Clean up everything, including freeing the skb */
1196                dpaa2_eth_free_tx_fd(priv, fq, &fd, false);
1197                netdev_tx_completed_queue(nq, 1, fd_len);
1198        } else {
1199                percpu_stats->tx_packets++;
1200                percpu_stats->tx_bytes += fd_len;
1201        }
1202
1203        return NETDEV_TX_OK;
1204
1205err_build_fd:
1206        dev_kfree_skb(skb);
1207
1208        return NETDEV_TX_OK;
1209}
1210
1211static void dpaa2_eth_tx_onestep_tstamp(struct work_struct *work)
1212{
1213        struct dpaa2_eth_priv *priv = container_of(work, struct dpaa2_eth_priv,
1214                                                   tx_onestep_tstamp);
1215        struct sk_buff *skb;
1216
1217        while (true) {
1218                skb = skb_dequeue(&priv->tx_skbs);
1219                if (!skb)
1220                        return;
1221
1222                /* Lock just before TX one-step timestamping packet,
1223                 * and release the lock in dpaa2_eth_free_tx_fd when
1224                 * confirm the packet has been sent on hardware, or
1225                 * when clean up during transmit failure.
1226                 */
1227                mutex_lock(&priv->onestep_tstamp_lock);
1228                __dpaa2_eth_tx(skb, priv->net_dev);
1229        }
1230}
1231
1232static netdev_tx_t dpaa2_eth_tx(struct sk_buff *skb, struct net_device *net_dev)
1233{
1234        struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1235        u8 msgtype, twostep, udp;
1236        u16 offset1, offset2;
1237
1238        /* Utilize skb->cb[0] for timestamping request per skb */
1239        skb->cb[0] = 0;
1240
1241        if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && dpaa2_ptp) {
1242                if (priv->tx_tstamp_type == HWTSTAMP_TX_ON)
1243                        skb->cb[0] = TX_TSTAMP;
1244                else if (priv->tx_tstamp_type == HWTSTAMP_TX_ONESTEP_SYNC)
1245                        skb->cb[0] = TX_TSTAMP_ONESTEP_SYNC;
1246        }
1247
1248        /* TX for one-step timestamping PTP Sync packet */
1249        if (skb->cb[0] == TX_TSTAMP_ONESTEP_SYNC) {
1250                if (!dpaa2_eth_ptp_parse(skb, &msgtype, &twostep, &udp,
1251                                         &offset1, &offset2))
1252                        if (msgtype == PTP_MSGTYPE_SYNC && twostep == 0) {
1253                                skb_queue_tail(&priv->tx_skbs, skb);
1254                                queue_work(priv->dpaa2_ptp_wq,
1255                                           &priv->tx_onestep_tstamp);
1256                                return NETDEV_TX_OK;
1257                        }
1258                /* Use two-step timestamping if not one-step timestamping
1259                 * PTP Sync packet
1260                 */
1261                skb->cb[0] = TX_TSTAMP;
1262        }
1263
1264        /* TX for other packets */
1265        return __dpaa2_eth_tx(skb, net_dev);
1266}
1267
1268/* Tx confirmation frame processing routine */
1269static void dpaa2_eth_tx_conf(struct dpaa2_eth_priv *priv,
1270                              struct dpaa2_eth_channel *ch __always_unused,
1271                              const struct dpaa2_fd *fd,
1272                              struct dpaa2_eth_fq *fq)
1273{
1274        struct rtnl_link_stats64 *percpu_stats;
1275        struct dpaa2_eth_drv_stats *percpu_extras;
1276        u32 fd_len = dpaa2_fd_get_len(fd);
1277        u32 fd_errors;
1278
1279        /* Tracing point */
1280        trace_dpaa2_tx_conf_fd(priv->net_dev, fd);
1281
1282        percpu_extras = this_cpu_ptr(priv->percpu_extras);
1283        percpu_extras->tx_conf_frames++;
1284        percpu_extras->tx_conf_bytes += fd_len;
1285
1286        /* Check frame errors in the FD field */
1287        fd_errors = dpaa2_fd_get_ctrl(fd) & DPAA2_FD_TX_ERR_MASK;
1288        dpaa2_eth_free_tx_fd(priv, fq, fd, true);
1289
1290        if (likely(!fd_errors))
1291                return;
1292
1293        if (net_ratelimit())
1294                netdev_dbg(priv->net_dev, "TX frame FD error: 0x%08x\n",
1295                           fd_errors);
1296
1297        percpu_stats = this_cpu_ptr(priv->percpu_stats);
1298        /* Tx-conf logically pertains to the egress path. */
1299        percpu_stats->tx_errors++;
1300}
1301
1302static int dpaa2_eth_set_rx_vlan_filtering(struct dpaa2_eth_priv *priv,
1303                                           bool enable)
1304{
1305        int err;
1306
1307        err = dpni_enable_vlan_filter(priv->mc_io, 0, priv->mc_token, enable);
1308
1309        if (err) {
1310                netdev_err(priv->net_dev,
1311                           "dpni_enable_vlan_filter failed\n");
1312                return err;
1313        }
1314
1315        return 0;
1316}
1317
1318static int dpaa2_eth_set_rx_csum(struct dpaa2_eth_priv *priv, bool enable)
1319{
1320        int err;
1321
1322        err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1323                               DPNI_OFF_RX_L3_CSUM, enable);
1324        if (err) {
1325                netdev_err(priv->net_dev,
1326                           "dpni_set_offload(RX_L3_CSUM) failed\n");
1327                return err;
1328        }
1329
1330        err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1331                               DPNI_OFF_RX_L4_CSUM, enable);
1332        if (err) {
1333                netdev_err(priv->net_dev,
1334                           "dpni_set_offload(RX_L4_CSUM) failed\n");
1335                return err;
1336        }
1337
1338        return 0;
1339}
1340
1341static int dpaa2_eth_set_tx_csum(struct dpaa2_eth_priv *priv, bool enable)
1342{
1343        int err;
1344
1345        err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1346                               DPNI_OFF_TX_L3_CSUM, enable);
1347        if (err) {
1348                netdev_err(priv->net_dev, "dpni_set_offload(TX_L3_CSUM) failed\n");
1349                return err;
1350        }
1351
1352        err = dpni_set_offload(priv->mc_io, 0, priv->mc_token,
1353                               DPNI_OFF_TX_L4_CSUM, enable);
1354        if (err) {
1355                netdev_err(priv->net_dev, "dpni_set_offload(TX_L4_CSUM) failed\n");
1356                return err;
1357        }
1358
1359        return 0;
1360}
1361
1362/* Perform a single release command to add buffers
1363 * to the specified buffer pool
1364 */
1365static int dpaa2_eth_add_bufs(struct dpaa2_eth_priv *priv,
1366                              struct dpaa2_eth_channel *ch, u16 bpid)
1367{
1368        struct device *dev = priv->net_dev->dev.parent;
1369        u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
1370        struct page *page;
1371        dma_addr_t addr;
1372        int retries = 0;
1373        int i, err;
1374
1375        for (i = 0; i < DPAA2_ETH_BUFS_PER_CMD; i++) {
1376                /* Allocate buffer visible to WRIOP + skb shared info +
1377                 * alignment padding
1378                 */
1379                /* allocate one page for each Rx buffer. WRIOP sees
1380                 * the entire page except for a tailroom reserved for
1381                 * skb shared info
1382                 */
1383                page = dev_alloc_pages(0);
1384                if (!page)
1385                        goto err_alloc;
1386
1387                addr = dma_map_page(dev, page, 0, priv->rx_buf_size,
1388                                    DMA_BIDIRECTIONAL);
1389                if (unlikely(dma_mapping_error(dev, addr)))
1390                        goto err_map;
1391
1392                buf_array[i] = addr;
1393
1394                /* tracing point */
1395                trace_dpaa2_eth_buf_seed(priv->net_dev,
1396                                         page, DPAA2_ETH_RX_BUF_RAW_SIZE,
1397                                         addr, priv->rx_buf_size,
1398                                         bpid);
1399        }
1400
1401release_bufs:
1402        /* In case the portal is busy, retry until successful */
1403        while ((err = dpaa2_io_service_release(ch->dpio, bpid,
1404                                               buf_array, i)) == -EBUSY) {
1405                if (retries++ >= DPAA2_ETH_SWP_BUSY_RETRIES)
1406                        break;
1407                cpu_relax();
1408        }
1409
1410        /* If release command failed, clean up and bail out;
1411         * not much else we can do about it
1412         */
1413        if (err) {
1414                dpaa2_eth_free_bufs(priv, buf_array, i);
1415                return 0;
1416        }
1417
1418        return i;
1419
1420err_map:
1421        __free_pages(page, 0);
1422err_alloc:
1423        /* If we managed to allocate at least some buffers,
1424         * release them to hardware
1425         */
1426        if (i)
1427                goto release_bufs;
1428
1429        return 0;
1430}
1431
1432static int dpaa2_eth_seed_pool(struct dpaa2_eth_priv *priv, u16 bpid)
1433{
1434        int i, j;
1435        int new_count;
1436
1437        for (j = 0; j < priv->num_channels; j++) {
1438                for (i = 0; i < DPAA2_ETH_NUM_BUFS;
1439                     i += DPAA2_ETH_BUFS_PER_CMD) {
1440                        new_count = dpaa2_eth_add_bufs(priv, priv->channel[j], bpid);
1441                        priv->channel[j]->buf_count += new_count;
1442
1443                        if (new_count < DPAA2_ETH_BUFS_PER_CMD) {
1444                                return -ENOMEM;
1445                        }
1446                }
1447        }
1448
1449        return 0;
1450}
1451
1452/*
1453 * Drain the specified number of buffers from the DPNI's private buffer pool.
1454 * @count must not exceeed DPAA2_ETH_BUFS_PER_CMD
1455 */
1456static void dpaa2_eth_drain_bufs(struct dpaa2_eth_priv *priv, int count)
1457{
1458        u64 buf_array[DPAA2_ETH_BUFS_PER_CMD];
1459        int retries = 0;
1460        int ret;
1461
1462        do {
1463                ret = dpaa2_io_service_acquire(NULL, priv->bpid,
1464                                               buf_array, count);
1465                if (ret < 0) {
1466                        if (ret == -EBUSY &&
1467                            retries++ < DPAA2_ETH_SWP_BUSY_RETRIES)
1468                                continue;
1469                        netdev_err(priv->net_dev, "dpaa2_io_service_acquire() failed\n");
1470                        return;
1471                }
1472                dpaa2_eth_free_bufs(priv, buf_array, ret);
1473                retries = 0;
1474        } while (ret);
1475}
1476
1477static void dpaa2_eth_drain_pool(struct dpaa2_eth_priv *priv)
1478{
1479        int i;
1480
1481        dpaa2_eth_drain_bufs(priv, DPAA2_ETH_BUFS_PER_CMD);
1482        dpaa2_eth_drain_bufs(priv, 1);
1483
1484        for (i = 0; i < priv->num_channels; i++)
1485                priv->channel[i]->buf_count = 0;
1486}
1487
1488/* Function is called from softirq context only, so we don't need to guard
1489 * the access to percpu count
1490 */
1491static int dpaa2_eth_refill_pool(struct dpaa2_eth_priv *priv,
1492                                 struct dpaa2_eth_channel *ch,
1493                                 u16 bpid)
1494{
1495        int new_count;
1496
1497        if (likely(ch->buf_count >= DPAA2_ETH_REFILL_THRESH))
1498                return 0;
1499
1500        do {
1501                new_count = dpaa2_eth_add_bufs(priv, ch, bpid);
1502                if (unlikely(!new_count)) {
1503                        /* Out of memory; abort for now, we'll try later on */
1504                        break;
1505                }
1506                ch->buf_count += new_count;
1507        } while (ch->buf_count < DPAA2_ETH_NUM_BUFS);
1508
1509        if (unlikely(ch->buf_count < DPAA2_ETH_NUM_BUFS))
1510                return -ENOMEM;
1511
1512        return 0;
1513}
1514
1515static void dpaa2_eth_sgt_cache_drain(struct dpaa2_eth_priv *priv)
1516{
1517        struct dpaa2_eth_sgt_cache *sgt_cache;
1518        u16 count;
1519        int k, i;
1520
1521        for_each_possible_cpu(k) {
1522                sgt_cache = per_cpu_ptr(priv->sgt_cache, k);
1523                count = sgt_cache->count;
1524
1525                for (i = 0; i < count; i++)
1526                        kfree(sgt_cache->buf[i]);
1527                sgt_cache->count = 0;
1528        }
1529}
1530
1531static int dpaa2_eth_pull_channel(struct dpaa2_eth_channel *ch)
1532{
1533        int err;
1534        int dequeues = -1;
1535
1536        /* Retry while portal is busy */
1537        do {
1538                err = dpaa2_io_service_pull_channel(ch->dpio, ch->ch_id,
1539                                                    ch->store);
1540                dequeues++;
1541                cpu_relax();
1542        } while (err == -EBUSY && dequeues < DPAA2_ETH_SWP_BUSY_RETRIES);
1543
1544        ch->stats.dequeue_portal_busy += dequeues;
1545        if (unlikely(err))
1546                ch->stats.pull_err++;
1547
1548        return err;
1549}
1550
1551/* NAPI poll routine
1552 *
1553 * Frames are dequeued from the QMan channel associated with this NAPI context.
1554 * Rx, Tx confirmation and (if configured) Rx error frames all count
1555 * towards the NAPI budget.
1556 */
1557static int dpaa2_eth_poll(struct napi_struct *napi, int budget)
1558{
1559        struct dpaa2_eth_channel *ch;
1560        struct dpaa2_eth_priv *priv;
1561        int rx_cleaned = 0, txconf_cleaned = 0;
1562        struct dpaa2_eth_fq *fq, *txc_fq = NULL;
1563        struct netdev_queue *nq;
1564        int store_cleaned, work_done;
1565        struct list_head rx_list;
1566        int retries = 0;
1567        u16 flowid;
1568        int err;
1569
1570        ch = container_of(napi, struct dpaa2_eth_channel, napi);
1571        ch->xdp.res = 0;
1572        priv = ch->priv;
1573
1574        INIT_LIST_HEAD(&rx_list);
1575        ch->rx_list = &rx_list;
1576
1577        do {
1578                err = dpaa2_eth_pull_channel(ch);
1579                if (unlikely(err))
1580                        break;
1581
1582                /* Refill pool if appropriate */
1583                dpaa2_eth_refill_pool(priv, ch, priv->bpid);
1584
1585                store_cleaned = dpaa2_eth_consume_frames(ch, &fq);
1586                if (store_cleaned <= 0)
1587                        break;
1588                if (fq->type == DPAA2_RX_FQ) {
1589                        rx_cleaned += store_cleaned;
1590                        flowid = fq->flowid;
1591                } else {
1592                        txconf_cleaned += store_cleaned;
1593                        /* We have a single Tx conf FQ on this channel */
1594                        txc_fq = fq;
1595                }
1596
1597                /* If we either consumed the whole NAPI budget with Rx frames
1598                 * or we reached the Tx confirmations threshold, we're done.
1599                 */
1600                if (rx_cleaned >= budget ||
1601                    txconf_cleaned >= DPAA2_ETH_TXCONF_PER_NAPI) {
1602                        work_done = budget;
1603                        goto out;
1604                }
1605        } while (store_cleaned);
1606
1607        /* We didn't consume the entire budget, so finish napi and
1608         * re-enable data availability notifications
1609         */
1610        napi_complete_done(napi, rx_cleaned);
1611        do {
1612                err = dpaa2_io_service_rearm(ch->dpio, &ch->nctx);
1613                cpu_relax();
1614        } while (err == -EBUSY && retries++ < DPAA2_ETH_SWP_BUSY_RETRIES);
1615        WARN_ONCE(err, "CDAN notifications rearm failed on core %d",
1616                  ch->nctx.desired_cpu);
1617
1618        work_done = max(rx_cleaned, 1);
1619
1620out:
1621        netif_receive_skb_list(ch->rx_list);
1622
1623        if (txc_fq && txc_fq->dq_frames) {
1624                nq = netdev_get_tx_queue(priv->net_dev, txc_fq->flowid);
1625                netdev_tx_completed_queue(nq, txc_fq->dq_frames,
1626                                          txc_fq->dq_bytes);
1627                txc_fq->dq_frames = 0;
1628                txc_fq->dq_bytes = 0;
1629        }
1630
1631        if (ch->xdp.res & XDP_REDIRECT)
1632                xdp_do_flush_map();
1633        else if (rx_cleaned && ch->xdp.res & XDP_TX)
1634                dpaa2_eth_xdp_tx_flush(priv, ch, &priv->fq[flowid]);
1635
1636        return work_done;
1637}
1638
1639static void dpaa2_eth_enable_ch_napi(struct dpaa2_eth_priv *priv)
1640{
1641        struct dpaa2_eth_channel *ch;
1642        int i;
1643
1644        for (i = 0; i < priv->num_channels; i++) {
1645                ch = priv->channel[i];
1646                napi_enable(&ch->napi);
1647        }
1648}
1649
1650static void dpaa2_eth_disable_ch_napi(struct dpaa2_eth_priv *priv)
1651{
1652        struct dpaa2_eth_channel *ch;
1653        int i;
1654
1655        for (i = 0; i < priv->num_channels; i++) {
1656                ch = priv->channel[i];
1657                napi_disable(&ch->napi);
1658        }
1659}
1660
1661void dpaa2_eth_set_rx_taildrop(struct dpaa2_eth_priv *priv,
1662                               bool tx_pause, bool pfc)
1663{
1664        struct dpni_taildrop td = {0};
1665        struct dpaa2_eth_fq *fq;
1666        int i, err;
1667
1668        /* FQ taildrop: threshold is in bytes, per frame queue. Enabled if
1669         * flow control is disabled (as it might interfere with either the
1670         * buffer pool depletion trigger for pause frames or with the group
1671         * congestion trigger for PFC frames)
1672         */
1673        td.enable = !tx_pause;
1674        if (priv->rx_fqtd_enabled == td.enable)
1675                goto set_cgtd;
1676
1677        td.threshold = DPAA2_ETH_FQ_TAILDROP_THRESH;
1678        td.units = DPNI_CONGESTION_UNIT_BYTES;
1679
1680        for (i = 0; i < priv->num_fqs; i++) {
1681                fq = &priv->fq[i];
1682                if (fq->type != DPAA2_RX_FQ)
1683                        continue;
1684                err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
1685                                        DPNI_CP_QUEUE, DPNI_QUEUE_RX,
1686                                        fq->tc, fq->flowid, &td);
1687                if (err) {
1688                        netdev_err(priv->net_dev,
1689                                   "dpni_set_taildrop(FQ) failed\n");
1690                        return;
1691                }
1692        }
1693
1694        priv->rx_fqtd_enabled = td.enable;
1695
1696set_cgtd:
1697        /* Congestion group taildrop: threshold is in frames, per group
1698         * of FQs belonging to the same traffic class
1699         * Enabled if general Tx pause disabled or if PFCs are enabled
1700         * (congestion group threhsold for PFC generation is lower than the
1701         * CG taildrop threshold, so it won't interfere with it; we also
1702         * want frames in non-PFC enabled traffic classes to be kept in check)
1703         */
1704        td.enable = !tx_pause || pfc;
1705        if (priv->rx_cgtd_enabled == td.enable)
1706                return;
1707
1708        td.threshold = DPAA2_ETH_CG_TAILDROP_THRESH(priv);
1709        td.units = DPNI_CONGESTION_UNIT_FRAMES;
1710        for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
1711                err = dpni_set_taildrop(priv->mc_io, 0, priv->mc_token,
1712                                        DPNI_CP_GROUP, DPNI_QUEUE_RX,
1713                                        i, 0, &td);
1714                if (err) {
1715                        netdev_err(priv->net_dev,
1716                                   "dpni_set_taildrop(CG) failed\n");
1717                        return;
1718                }
1719        }
1720
1721        priv->rx_cgtd_enabled = td.enable;
1722}
1723
1724static int dpaa2_eth_link_state_update(struct dpaa2_eth_priv *priv)
1725{
1726        struct dpni_link_state state = {0};
1727        bool tx_pause;
1728        int err;
1729
1730        err = dpni_get_link_state(priv->mc_io, 0, priv->mc_token, &state);
1731        if (unlikely(err)) {
1732                netdev_err(priv->net_dev,
1733                           "dpni_get_link_state() failed\n");
1734                return err;
1735        }
1736
1737        /* If Tx pause frame settings have changed, we need to update
1738         * Rx FQ taildrop configuration as well. We configure taildrop
1739         * only when pause frame generation is disabled.
1740         */
1741        tx_pause = dpaa2_eth_tx_pause_enabled(state.options);
1742        dpaa2_eth_set_rx_taildrop(priv, tx_pause, priv->pfc_enabled);
1743
1744        /* When we manage the MAC/PHY using phylink there is no need
1745         * to manually update the netif_carrier.
1746         */
1747        if (dpaa2_eth_is_type_phy(priv))
1748                goto out;
1749
1750        /* Chech link state; speed / duplex changes are not treated yet */
1751        if (priv->link_state.up == state.up)
1752                goto out;
1753
1754        if (state.up) {
1755                netif_carrier_on(priv->net_dev);
1756                netif_tx_start_all_queues(priv->net_dev);
1757        } else {
1758                netif_tx_stop_all_queues(priv->net_dev);
1759                netif_carrier_off(priv->net_dev);
1760        }
1761
1762        netdev_info(priv->net_dev, "Link Event: state %s\n",
1763                    state.up ? "up" : "down");
1764
1765out:
1766        priv->link_state = state;
1767
1768        return 0;
1769}
1770
1771static int dpaa2_eth_open(struct net_device *net_dev)
1772{
1773        struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1774        int err;
1775
1776        err = dpaa2_eth_seed_pool(priv, priv->bpid);
1777        if (err) {
1778                /* Not much to do; the buffer pool, though not filled up,
1779                 * may still contain some buffers which would enable us
1780                 * to limp on.
1781                 */
1782                netdev_err(net_dev, "Buffer seeding failed for DPBP %d (bpid=%d)\n",
1783                           priv->dpbp_dev->obj_desc.id, priv->bpid);
1784        }
1785
1786        if (!dpaa2_eth_is_type_phy(priv)) {
1787                /* We'll only start the txqs when the link is actually ready;
1788                 * make sure we don't race against the link up notification,
1789                 * which may come immediately after dpni_enable();
1790                 */
1791                netif_tx_stop_all_queues(net_dev);
1792
1793                /* Also, explicitly set carrier off, otherwise
1794                 * netif_carrier_ok() will return true and cause 'ip link show'
1795                 * to report the LOWER_UP flag, even though the link
1796                 * notification wasn't even received.
1797                 */
1798                netif_carrier_off(net_dev);
1799        }
1800        dpaa2_eth_enable_ch_napi(priv);
1801
1802        err = dpni_enable(priv->mc_io, 0, priv->mc_token);
1803        if (err < 0) {
1804                netdev_err(net_dev, "dpni_enable() failed\n");
1805                goto enable_err;
1806        }
1807
1808        if (dpaa2_eth_is_type_phy(priv))
1809                phylink_start(priv->mac->phylink);
1810
1811        return 0;
1812
1813enable_err:
1814        dpaa2_eth_disable_ch_napi(priv);
1815        dpaa2_eth_drain_pool(priv);
1816        return err;
1817}
1818
1819/* Total number of in-flight frames on ingress queues */
1820static u32 dpaa2_eth_ingress_fq_count(struct dpaa2_eth_priv *priv)
1821{
1822        struct dpaa2_eth_fq *fq;
1823        u32 fcnt = 0, bcnt = 0, total = 0;
1824        int i, err;
1825
1826        for (i = 0; i < priv->num_fqs; i++) {
1827                fq = &priv->fq[i];
1828                err = dpaa2_io_query_fq_count(NULL, fq->fqid, &fcnt, &bcnt);
1829                if (err) {
1830                        netdev_warn(priv->net_dev, "query_fq_count failed");
1831                        break;
1832                }
1833                total += fcnt;
1834        }
1835
1836        return total;
1837}
1838
1839static void dpaa2_eth_wait_for_ingress_fq_empty(struct dpaa2_eth_priv *priv)
1840{
1841        int retries = 10;
1842        u32 pending;
1843
1844        do {
1845                pending = dpaa2_eth_ingress_fq_count(priv);
1846                if (pending)
1847                        msleep(100);
1848        } while (pending && --retries);
1849}
1850
1851#define DPNI_TX_PENDING_VER_MAJOR       7
1852#define DPNI_TX_PENDING_VER_MINOR       13
1853static void dpaa2_eth_wait_for_egress_fq_empty(struct dpaa2_eth_priv *priv)
1854{
1855        union dpni_statistics stats;
1856        int retries = 10;
1857        int err;
1858
1859        if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_TX_PENDING_VER_MAJOR,
1860                                   DPNI_TX_PENDING_VER_MINOR) < 0)
1861                goto out;
1862
1863        do {
1864                err = dpni_get_statistics(priv->mc_io, 0, priv->mc_token, 6,
1865                                          &stats);
1866                if (err)
1867                        goto out;
1868                if (stats.page_6.tx_pending_frames == 0)
1869                        return;
1870        } while (--retries);
1871
1872out:
1873        msleep(500);
1874}
1875
1876static int dpaa2_eth_stop(struct net_device *net_dev)
1877{
1878        struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1879        int dpni_enabled = 0;
1880        int retries = 10;
1881
1882        if (dpaa2_eth_is_type_phy(priv)) {
1883                phylink_stop(priv->mac->phylink);
1884        } else {
1885                netif_tx_stop_all_queues(net_dev);
1886                netif_carrier_off(net_dev);
1887        }
1888
1889        /* On dpni_disable(), the MC firmware will:
1890         * - stop MAC Rx and wait for all Rx frames to be enqueued to software
1891         * - cut off WRIOP dequeues from egress FQs and wait until transmission
1892         * of all in flight Tx frames is finished (and corresponding Tx conf
1893         * frames are enqueued back to software)
1894         *
1895         * Before calling dpni_disable(), we wait for all Tx frames to arrive
1896         * on WRIOP. After it finishes, wait until all remaining frames on Rx
1897         * and Tx conf queues are consumed on NAPI poll.
1898         */
1899        dpaa2_eth_wait_for_egress_fq_empty(priv);
1900
1901        do {
1902                dpni_disable(priv->mc_io, 0, priv->mc_token);
1903                dpni_is_enabled(priv->mc_io, 0, priv->mc_token, &dpni_enabled);
1904                if (dpni_enabled)
1905                        /* Allow the hardware some slack */
1906                        msleep(100);
1907        } while (dpni_enabled && --retries);
1908        if (!retries) {
1909                netdev_warn(net_dev, "Retry count exceeded disabling DPNI\n");
1910                /* Must go on and disable NAPI nonetheless, so we don't crash at
1911                 * the next "ifconfig up"
1912                 */
1913        }
1914
1915        dpaa2_eth_wait_for_ingress_fq_empty(priv);
1916        dpaa2_eth_disable_ch_napi(priv);
1917
1918        /* Empty the buffer pool */
1919        dpaa2_eth_drain_pool(priv);
1920
1921        /* Empty the Scatter-Gather Buffer cache */
1922        dpaa2_eth_sgt_cache_drain(priv);
1923
1924        return 0;
1925}
1926
1927static int dpaa2_eth_set_addr(struct net_device *net_dev, void *addr)
1928{
1929        struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1930        struct device *dev = net_dev->dev.parent;
1931        int err;
1932
1933        err = eth_mac_addr(net_dev, addr);
1934        if (err < 0) {
1935                dev_err(dev, "eth_mac_addr() failed (%d)\n", err);
1936                return err;
1937        }
1938
1939        err = dpni_set_primary_mac_addr(priv->mc_io, 0, priv->mc_token,
1940                                        net_dev->dev_addr);
1941        if (err) {
1942                dev_err(dev, "dpni_set_primary_mac_addr() failed (%d)\n", err);
1943                return err;
1944        }
1945
1946        return 0;
1947}
1948
1949/** Fill in counters maintained by the GPP driver. These may be different from
1950 * the hardware counters obtained by ethtool.
1951 */
1952static void dpaa2_eth_get_stats(struct net_device *net_dev,
1953                                struct rtnl_link_stats64 *stats)
1954{
1955        struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
1956        struct rtnl_link_stats64 *percpu_stats;
1957        u64 *cpustats;
1958        u64 *netstats = (u64 *)stats;
1959        int i, j;
1960        int num = sizeof(struct rtnl_link_stats64) / sizeof(u64);
1961
1962        for_each_possible_cpu(i) {
1963                percpu_stats = per_cpu_ptr(priv->percpu_stats, i);
1964                cpustats = (u64 *)percpu_stats;
1965                for (j = 0; j < num; j++)
1966                        netstats[j] += cpustats[j];
1967        }
1968}
1969
1970/* Copy mac unicast addresses from @net_dev to @priv.
1971 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1972 */
1973static void dpaa2_eth_add_uc_hw_addr(const struct net_device *net_dev,
1974                                     struct dpaa2_eth_priv *priv)
1975{
1976        struct netdev_hw_addr *ha;
1977        int err;
1978
1979        netdev_for_each_uc_addr(ha, net_dev) {
1980                err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
1981                                        ha->addr);
1982                if (err)
1983                        netdev_warn(priv->net_dev,
1984                                    "Could not add ucast MAC %pM to the filtering table (err %d)\n",
1985                                    ha->addr, err);
1986        }
1987}
1988
1989/* Copy mac multicast addresses from @net_dev to @priv
1990 * Its sole purpose is to make dpaa2_eth_set_rx_mode() more readable.
1991 */
1992static void dpaa2_eth_add_mc_hw_addr(const struct net_device *net_dev,
1993                                     struct dpaa2_eth_priv *priv)
1994{
1995        struct netdev_hw_addr *ha;
1996        int err;
1997
1998        netdev_for_each_mc_addr(ha, net_dev) {
1999                err = dpni_add_mac_addr(priv->mc_io, 0, priv->mc_token,
2000                                        ha->addr);
2001                if (err)
2002                        netdev_warn(priv->net_dev,
2003                                    "Could not add mcast MAC %pM to the filtering table (err %d)\n",
2004                                    ha->addr, err);
2005        }
2006}
2007
2008static int dpaa2_eth_rx_add_vid(struct net_device *net_dev,
2009                                __be16 vlan_proto, u16 vid)
2010{
2011        struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2012        int err;
2013
2014        err = dpni_add_vlan_id(priv->mc_io, 0, priv->mc_token,
2015                               vid, 0, 0, 0);
2016
2017        if (err) {
2018                netdev_warn(priv->net_dev,
2019                            "Could not add the vlan id %u\n",
2020                            vid);
2021                return err;
2022        }
2023
2024        return 0;
2025}
2026
2027static int dpaa2_eth_rx_kill_vid(struct net_device *net_dev,
2028                                 __be16 vlan_proto, u16 vid)
2029{
2030        struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2031        int err;
2032
2033        err = dpni_remove_vlan_id(priv->mc_io, 0, priv->mc_token, vid);
2034
2035        if (err) {
2036                netdev_warn(priv->net_dev,
2037                            "Could not remove the vlan id %u\n",
2038                            vid);
2039                return err;
2040        }
2041
2042        return 0;
2043}
2044
2045static void dpaa2_eth_set_rx_mode(struct net_device *net_dev)
2046{
2047        struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2048        int uc_count = netdev_uc_count(net_dev);
2049        int mc_count = netdev_mc_count(net_dev);
2050        u8 max_mac = priv->dpni_attrs.mac_filter_entries;
2051        u32 options = priv->dpni_attrs.options;
2052        u16 mc_token = priv->mc_token;
2053        struct fsl_mc_io *mc_io = priv->mc_io;
2054        int err;
2055
2056        /* Basic sanity checks; these probably indicate a misconfiguration */
2057        if (options & DPNI_OPT_NO_MAC_FILTER && max_mac != 0)
2058                netdev_info(net_dev,
2059                            "mac_filter_entries=%d, DPNI_OPT_NO_MAC_FILTER option must be disabled\n",
2060                            max_mac);
2061
2062        /* Force promiscuous if the uc or mc counts exceed our capabilities. */
2063        if (uc_count > max_mac) {
2064                netdev_info(net_dev,
2065                            "Unicast addr count reached %d, max allowed is %d; forcing promisc\n",
2066                            uc_count, max_mac);
2067                goto force_promisc;
2068        }
2069        if (mc_count + uc_count > max_mac) {
2070                netdev_info(net_dev,
2071                            "Unicast + multicast addr count reached %d, max allowed is %d; forcing promisc\n",
2072                            uc_count + mc_count, max_mac);
2073                goto force_mc_promisc;
2074        }
2075
2076        /* Adjust promisc settings due to flag combinations */
2077        if (net_dev->flags & IFF_PROMISC)
2078                goto force_promisc;
2079        if (net_dev->flags & IFF_ALLMULTI) {
2080                /* First, rebuild unicast filtering table. This should be done
2081                 * in promisc mode, in order to avoid frame loss while we
2082                 * progressively add entries to the table.
2083                 * We don't know whether we had been in promisc already, and
2084                 * making an MC call to find out is expensive; so set uc promisc
2085                 * nonetheless.
2086                 */
2087                err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2088                if (err)
2089                        netdev_warn(net_dev, "Can't set uc promisc\n");
2090
2091                /* Actual uc table reconstruction. */
2092                err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 0);
2093                if (err)
2094                        netdev_warn(net_dev, "Can't clear uc filters\n");
2095                dpaa2_eth_add_uc_hw_addr(net_dev, priv);
2096
2097                /* Finally, clear uc promisc and set mc promisc as requested. */
2098                err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
2099                if (err)
2100                        netdev_warn(net_dev, "Can't clear uc promisc\n");
2101                goto force_mc_promisc;
2102        }
2103
2104        /* Neither unicast, nor multicast promisc will be on... eventually.
2105         * For now, rebuild mac filtering tables while forcing both of them on.
2106         */
2107        err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2108        if (err)
2109                netdev_warn(net_dev, "Can't set uc promisc (%d)\n", err);
2110        err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
2111        if (err)
2112                netdev_warn(net_dev, "Can't set mc promisc (%d)\n", err);
2113
2114        /* Actual mac filtering tables reconstruction */
2115        err = dpni_clear_mac_filters(mc_io, 0, mc_token, 1, 1);
2116        if (err)
2117                netdev_warn(net_dev, "Can't clear mac filters\n");
2118        dpaa2_eth_add_mc_hw_addr(net_dev, priv);
2119        dpaa2_eth_add_uc_hw_addr(net_dev, priv);
2120
2121        /* Now we can clear both ucast and mcast promisc, without risking
2122         * to drop legitimate frames anymore.
2123         */
2124        err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 0);
2125        if (err)
2126                netdev_warn(net_dev, "Can't clear ucast promisc\n");
2127        err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 0);
2128        if (err)
2129                netdev_warn(net_dev, "Can't clear mcast promisc\n");
2130
2131        return;
2132
2133force_promisc:
2134        err = dpni_set_unicast_promisc(mc_io, 0, mc_token, 1);
2135        if (err)
2136                netdev_warn(net_dev, "Can't set ucast promisc\n");
2137force_mc_promisc:
2138        err = dpni_set_multicast_promisc(mc_io, 0, mc_token, 1);
2139        if (err)
2140                netdev_warn(net_dev, "Can't set mcast promisc\n");
2141}
2142
2143static int dpaa2_eth_set_features(struct net_device *net_dev,
2144                                  netdev_features_t features)
2145{
2146        struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2147        netdev_features_t changed = features ^ net_dev->features;
2148        bool enable;
2149        int err;
2150
2151        if (changed & NETIF_F_HW_VLAN_CTAG_FILTER) {
2152                enable = !!(features & NETIF_F_HW_VLAN_CTAG_FILTER);
2153                err = dpaa2_eth_set_rx_vlan_filtering(priv, enable);
2154                if (err)
2155                        return err;
2156        }
2157
2158        if (changed & NETIF_F_RXCSUM) {
2159                enable = !!(features & NETIF_F_RXCSUM);
2160                err = dpaa2_eth_set_rx_csum(priv, enable);
2161                if (err)
2162                        return err;
2163        }
2164
2165        if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)) {
2166                enable = !!(features & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
2167                err = dpaa2_eth_set_tx_csum(priv, enable);
2168                if (err)
2169                        return err;
2170        }
2171
2172        return 0;
2173}
2174
2175static int dpaa2_eth_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2176{
2177        struct dpaa2_eth_priv *priv = netdev_priv(dev);
2178        struct hwtstamp_config config;
2179
2180        if (!dpaa2_ptp)
2181                return -EINVAL;
2182
2183        if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
2184                return -EFAULT;
2185
2186        switch (config.tx_type) {
2187        case HWTSTAMP_TX_OFF:
2188        case HWTSTAMP_TX_ON:
2189        case HWTSTAMP_TX_ONESTEP_SYNC:
2190                priv->tx_tstamp_type = config.tx_type;
2191                break;
2192        default:
2193                return -ERANGE;
2194        }
2195
2196        if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
2197                priv->rx_tstamp = false;
2198        } else {
2199                priv->rx_tstamp = true;
2200                /* TS is set for all frame types, not only those requested */
2201                config.rx_filter = HWTSTAMP_FILTER_ALL;
2202        }
2203
2204        return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
2205                        -EFAULT : 0;
2206}
2207
2208static int dpaa2_eth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2209{
2210        struct dpaa2_eth_priv *priv = netdev_priv(dev);
2211
2212        if (cmd == SIOCSHWTSTAMP)
2213                return dpaa2_eth_ts_ioctl(dev, rq, cmd);
2214
2215        if (dpaa2_eth_is_type_phy(priv))
2216                return phylink_mii_ioctl(priv->mac->phylink, rq, cmd);
2217
2218        return -EOPNOTSUPP;
2219}
2220
2221static bool xdp_mtu_valid(struct dpaa2_eth_priv *priv, int mtu)
2222{
2223        int mfl, linear_mfl;
2224
2225        mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
2226        linear_mfl = priv->rx_buf_size - DPAA2_ETH_RX_HWA_SIZE -
2227                     dpaa2_eth_rx_head_room(priv) - XDP_PACKET_HEADROOM;
2228
2229        if (mfl > linear_mfl) {
2230                netdev_warn(priv->net_dev, "Maximum MTU for XDP is %d\n",
2231                            linear_mfl - VLAN_ETH_HLEN);
2232                return false;
2233        }
2234
2235        return true;
2236}
2237
2238static int dpaa2_eth_set_rx_mfl(struct dpaa2_eth_priv *priv, int mtu, bool has_xdp)
2239{
2240        int mfl, err;
2241
2242        /* We enforce a maximum Rx frame length based on MTU only if we have
2243         * an XDP program attached (in order to avoid Rx S/G frames).
2244         * Otherwise, we accept all incoming frames as long as they are not
2245         * larger than maximum size supported in hardware
2246         */
2247        if (has_xdp)
2248                mfl = DPAA2_ETH_L2_MAX_FRM(mtu);
2249        else
2250                mfl = DPAA2_ETH_MFL;
2251
2252        err = dpni_set_max_frame_length(priv->mc_io, 0, priv->mc_token, mfl);
2253        if (err) {
2254                netdev_err(priv->net_dev, "dpni_set_max_frame_length failed\n");
2255                return err;
2256        }
2257
2258        return 0;
2259}
2260
2261static int dpaa2_eth_change_mtu(struct net_device *dev, int new_mtu)
2262{
2263        struct dpaa2_eth_priv *priv = netdev_priv(dev);
2264        int err;
2265
2266        if (!priv->xdp_prog)
2267                goto out;
2268
2269        if (!xdp_mtu_valid(priv, new_mtu))
2270                return -EINVAL;
2271
2272        err = dpaa2_eth_set_rx_mfl(priv, new_mtu, true);
2273        if (err)
2274                return err;
2275
2276out:
2277        dev->mtu = new_mtu;
2278        return 0;
2279}
2280
2281static int dpaa2_eth_update_rx_buffer_headroom(struct dpaa2_eth_priv *priv, bool has_xdp)
2282{
2283        struct dpni_buffer_layout buf_layout = {0};
2284        int err;
2285
2286        err = dpni_get_buffer_layout(priv->mc_io, 0, priv->mc_token,
2287                                     DPNI_QUEUE_RX, &buf_layout);
2288        if (err) {
2289                netdev_err(priv->net_dev, "dpni_get_buffer_layout failed\n");
2290                return err;
2291        }
2292
2293        /* Reserve extra headroom for XDP header size changes */
2294        buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv) +
2295                                    (has_xdp ? XDP_PACKET_HEADROOM : 0);
2296        buf_layout.options = DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM;
2297        err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
2298                                     DPNI_QUEUE_RX, &buf_layout);
2299        if (err) {
2300                netdev_err(priv->net_dev, "dpni_set_buffer_layout failed\n");
2301                return err;
2302        }
2303
2304        return 0;
2305}
2306
2307static int dpaa2_eth_setup_xdp(struct net_device *dev, struct bpf_prog *prog)
2308{
2309        struct dpaa2_eth_priv *priv = netdev_priv(dev);
2310        struct dpaa2_eth_channel *ch;
2311        struct bpf_prog *old;
2312        bool up, need_update;
2313        int i, err;
2314
2315        if (prog && !xdp_mtu_valid(priv, dev->mtu))
2316                return -EINVAL;
2317
2318        if (prog)
2319                bpf_prog_add(prog, priv->num_channels);
2320
2321        up = netif_running(dev);
2322        need_update = (!!priv->xdp_prog != !!prog);
2323
2324        if (up)
2325                dpaa2_eth_stop(dev);
2326
2327        /* While in xdp mode, enforce a maximum Rx frame size based on MTU.
2328         * Also, when switching between xdp/non-xdp modes we need to reconfigure
2329         * our Rx buffer layout. Buffer pool was drained on dpaa2_eth_stop,
2330         * so we are sure no old format buffers will be used from now on.
2331         */
2332        if (need_update) {
2333                err = dpaa2_eth_set_rx_mfl(priv, dev->mtu, !!prog);
2334                if (err)
2335                        goto out_err;
2336                err = dpaa2_eth_update_rx_buffer_headroom(priv, !!prog);
2337                if (err)
2338                        goto out_err;
2339        }
2340
2341        old = xchg(&priv->xdp_prog, prog);
2342        if (old)
2343                bpf_prog_put(old);
2344
2345        for (i = 0; i < priv->num_channels; i++) {
2346                ch = priv->channel[i];
2347                old = xchg(&ch->xdp.prog, prog);
2348                if (old)
2349                        bpf_prog_put(old);
2350        }
2351
2352        if (up) {
2353                err = dpaa2_eth_open(dev);
2354                if (err)
2355                        return err;
2356        }
2357
2358        return 0;
2359
2360out_err:
2361        if (prog)
2362                bpf_prog_sub(prog, priv->num_channels);
2363        if (up)
2364                dpaa2_eth_open(dev);
2365
2366        return err;
2367}
2368
2369static int dpaa2_eth_xdp(struct net_device *dev, struct netdev_bpf *xdp)
2370{
2371        switch (xdp->command) {
2372        case XDP_SETUP_PROG:
2373                return dpaa2_eth_setup_xdp(dev, xdp->prog);
2374        default:
2375                return -EINVAL;
2376        }
2377
2378        return 0;
2379}
2380
2381static int dpaa2_eth_xdp_create_fd(struct net_device *net_dev,
2382                                   struct xdp_frame *xdpf,
2383                                   struct dpaa2_fd *fd)
2384{
2385        struct device *dev = net_dev->dev.parent;
2386        unsigned int needed_headroom;
2387        struct dpaa2_eth_swa *swa;
2388        void *buffer_start, *aligned_start;
2389        dma_addr_t addr;
2390
2391        /* We require a minimum headroom to be able to transmit the frame.
2392         * Otherwise return an error and let the original net_device handle it
2393         */
2394        needed_headroom = dpaa2_eth_needed_headroom(NULL);
2395        if (xdpf->headroom < needed_headroom)
2396                return -EINVAL;
2397
2398        /* Setup the FD fields */
2399        memset(fd, 0, sizeof(*fd));
2400
2401        /* Align FD address, if possible */
2402        buffer_start = xdpf->data - needed_headroom;
2403        aligned_start = PTR_ALIGN(buffer_start - DPAA2_ETH_TX_BUF_ALIGN,
2404                                  DPAA2_ETH_TX_BUF_ALIGN);
2405        if (aligned_start >= xdpf->data - xdpf->headroom)
2406                buffer_start = aligned_start;
2407
2408        swa = (struct dpaa2_eth_swa *)buffer_start;
2409        /* fill in necessary fields here */
2410        swa->type = DPAA2_ETH_SWA_XDP;
2411        swa->xdp.dma_size = xdpf->data + xdpf->len - buffer_start;
2412        swa->xdp.xdpf = xdpf;
2413
2414        addr = dma_map_single(dev, buffer_start,
2415                              swa->xdp.dma_size,
2416                              DMA_BIDIRECTIONAL);
2417        if (unlikely(dma_mapping_error(dev, addr)))
2418                return -ENOMEM;
2419
2420        dpaa2_fd_set_addr(fd, addr);
2421        dpaa2_fd_set_offset(fd, xdpf->data - buffer_start);
2422        dpaa2_fd_set_len(fd, xdpf->len);
2423        dpaa2_fd_set_format(fd, dpaa2_fd_single);
2424        dpaa2_fd_set_ctrl(fd, FD_CTRL_PTA);
2425
2426        return 0;
2427}
2428
2429static int dpaa2_eth_xdp_xmit(struct net_device *net_dev, int n,
2430                              struct xdp_frame **frames, u32 flags)
2431{
2432        struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2433        struct dpaa2_eth_xdp_fds *xdp_redirect_fds;
2434        struct rtnl_link_stats64 *percpu_stats;
2435        struct dpaa2_eth_fq *fq;
2436        struct dpaa2_fd *fds;
2437        int enqueued, i, err;
2438
2439        if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2440                return -EINVAL;
2441
2442        if (!netif_running(net_dev))
2443                return -ENETDOWN;
2444
2445        fq = &priv->fq[smp_processor_id()];
2446        xdp_redirect_fds = &fq->xdp_redirect_fds;
2447        fds = xdp_redirect_fds->fds;
2448
2449        percpu_stats = this_cpu_ptr(priv->percpu_stats);
2450
2451        /* create a FD for each xdp_frame in the list received */
2452        for (i = 0; i < n; i++) {
2453                err = dpaa2_eth_xdp_create_fd(net_dev, frames[i], &fds[i]);
2454                if (err)
2455                        break;
2456        }
2457        xdp_redirect_fds->num = i;
2458
2459        /* enqueue all the frame descriptors */
2460        enqueued = dpaa2_eth_xdp_flush(priv, fq, xdp_redirect_fds);
2461
2462        /* update statistics */
2463        percpu_stats->tx_packets += enqueued;
2464        for (i = 0; i < enqueued; i++)
2465                percpu_stats->tx_bytes += dpaa2_fd_get_len(&fds[i]);
2466
2467        return enqueued;
2468}
2469
2470static int update_xps(struct dpaa2_eth_priv *priv)
2471{
2472        struct net_device *net_dev = priv->net_dev;
2473        struct cpumask xps_mask;
2474        struct dpaa2_eth_fq *fq;
2475        int i, num_queues, netdev_queues;
2476        int err = 0;
2477
2478        num_queues = dpaa2_eth_queue_count(priv);
2479        netdev_queues = (net_dev->num_tc ? : 1) * num_queues;
2480
2481        /* The first <num_queues> entries in priv->fq array are Tx/Tx conf
2482         * queues, so only process those
2483         */
2484        for (i = 0; i < netdev_queues; i++) {
2485                fq = &priv->fq[i % num_queues];
2486
2487                cpumask_clear(&xps_mask);
2488                cpumask_set_cpu(fq->target_cpu, &xps_mask);
2489
2490                err = netif_set_xps_queue(net_dev, &xps_mask, i);
2491                if (err) {
2492                        netdev_warn_once(net_dev, "Error setting XPS queue\n");
2493                        break;
2494                }
2495        }
2496
2497        return err;
2498}
2499
2500static int dpaa2_eth_setup_mqprio(struct net_device *net_dev,
2501                                  struct tc_mqprio_qopt *mqprio)
2502{
2503        struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2504        u8 num_tc, num_queues;
2505        int i;
2506
2507        mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
2508        num_queues = dpaa2_eth_queue_count(priv);
2509        num_tc = mqprio->num_tc;
2510
2511        if (num_tc == net_dev->num_tc)
2512                return 0;
2513
2514        if (num_tc  > dpaa2_eth_tc_count(priv)) {
2515                netdev_err(net_dev, "Max %d traffic classes supported\n",
2516                           dpaa2_eth_tc_count(priv));
2517                return -EOPNOTSUPP;
2518        }
2519
2520        if (!num_tc) {
2521                netdev_reset_tc(net_dev);
2522                netif_set_real_num_tx_queues(net_dev, num_queues);
2523                goto out;
2524        }
2525
2526        netdev_set_num_tc(net_dev, num_tc);
2527        netif_set_real_num_tx_queues(net_dev, num_tc * num_queues);
2528
2529        for (i = 0; i < num_tc; i++)
2530                netdev_set_tc_queue(net_dev, i, num_queues, i * num_queues);
2531
2532out:
2533        update_xps(priv);
2534
2535        return 0;
2536}
2537
2538#define bps_to_mbits(rate) (div_u64((rate), 1000000) * 8)
2539
2540static int dpaa2_eth_setup_tbf(struct net_device *net_dev, struct tc_tbf_qopt_offload *p)
2541{
2542        struct tc_tbf_qopt_offload_replace_params *cfg = &p->replace_params;
2543        struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
2544        struct dpni_tx_shaping_cfg tx_cr_shaper = { 0 };
2545        struct dpni_tx_shaping_cfg tx_er_shaper = { 0 };
2546        int err;
2547
2548        if (p->command == TC_TBF_STATS)
2549                return -EOPNOTSUPP;
2550
2551        /* Only per port Tx shaping */
2552        if (p->parent != TC_H_ROOT)
2553                return -EOPNOTSUPP;
2554
2555        if (p->command == TC_TBF_REPLACE) {
2556                if (cfg->max_size > DPAA2_ETH_MAX_BURST_SIZE) {
2557                        netdev_err(net_dev, "burst size cannot be greater than %d\n",
2558                                   DPAA2_ETH_MAX_BURST_SIZE);
2559                        return -EINVAL;
2560                }
2561
2562                tx_cr_shaper.max_burst_size = cfg->max_size;
2563                /* The TBF interface is in bytes/s, whereas DPAA2 expects the
2564                 * rate in Mbits/s
2565                 */
2566                tx_cr_shaper.rate_limit = bps_to_mbits(cfg->rate.rate_bytes_ps);
2567        }
2568
2569        err = dpni_set_tx_shaping(priv->mc_io, 0, priv->mc_token, &tx_cr_shaper,
2570                                  &tx_er_shaper, 0);
2571        if (err) {
2572                netdev_err(net_dev, "dpni_set_tx_shaping() = %d\n", err);
2573                return err;
2574        }
2575
2576        return 0;
2577}
2578
2579static int dpaa2_eth_setup_tc(struct net_device *net_dev,
2580                              enum tc_setup_type type, void *type_data)
2581{
2582        switch (type) {
2583        case TC_SETUP_QDISC_MQPRIO:
2584                return dpaa2_eth_setup_mqprio(net_dev, type_data);
2585        case TC_SETUP_QDISC_TBF:
2586                return dpaa2_eth_setup_tbf(net_dev, type_data);
2587        default:
2588                return -EOPNOTSUPP;
2589        }
2590}
2591
2592static const struct net_device_ops dpaa2_eth_ops = {
2593        .ndo_open = dpaa2_eth_open,
2594        .ndo_start_xmit = dpaa2_eth_tx,
2595        .ndo_stop = dpaa2_eth_stop,
2596        .ndo_set_mac_address = dpaa2_eth_set_addr,
2597        .ndo_get_stats64 = dpaa2_eth_get_stats,
2598        .ndo_set_rx_mode = dpaa2_eth_set_rx_mode,
2599        .ndo_set_features = dpaa2_eth_set_features,
2600        .ndo_do_ioctl = dpaa2_eth_ioctl,
2601        .ndo_change_mtu = dpaa2_eth_change_mtu,
2602        .ndo_bpf = dpaa2_eth_xdp,
2603        .ndo_xdp_xmit = dpaa2_eth_xdp_xmit,
2604        .ndo_setup_tc = dpaa2_eth_setup_tc,
2605        .ndo_vlan_rx_add_vid = dpaa2_eth_rx_add_vid,
2606        .ndo_vlan_rx_kill_vid = dpaa2_eth_rx_kill_vid
2607};
2608
2609static void dpaa2_eth_cdan_cb(struct dpaa2_io_notification_ctx *ctx)
2610{
2611        struct dpaa2_eth_channel *ch;
2612
2613        ch = container_of(ctx, struct dpaa2_eth_channel, nctx);
2614
2615        /* Update NAPI statistics */
2616        ch->stats.cdan++;
2617
2618        napi_schedule(&ch->napi);
2619}
2620
2621/* Allocate and configure a DPCON object */
2622static struct fsl_mc_device *dpaa2_eth_setup_dpcon(struct dpaa2_eth_priv *priv)
2623{
2624        struct fsl_mc_device *dpcon;
2625        struct device *dev = priv->net_dev->dev.parent;
2626        int err;
2627
2628        err = fsl_mc_object_allocate(to_fsl_mc_device(dev),
2629                                     FSL_MC_POOL_DPCON, &dpcon);
2630        if (err) {
2631                if (err == -ENXIO)
2632                        err = -EPROBE_DEFER;
2633                else
2634                        dev_info(dev, "Not enough DPCONs, will go on as-is\n");
2635                return ERR_PTR(err);
2636        }
2637
2638        err = dpcon_open(priv->mc_io, 0, dpcon->obj_desc.id, &dpcon->mc_handle);
2639        if (err) {
2640                dev_err(dev, "dpcon_open() failed\n");
2641                goto free;
2642        }
2643
2644        err = dpcon_reset(priv->mc_io, 0, dpcon->mc_handle);
2645        if (err) {
2646                dev_err(dev, "dpcon_reset() failed\n");
2647                goto close;
2648        }
2649
2650        err = dpcon_enable(priv->mc_io, 0, dpcon->mc_handle);
2651        if (err) {
2652                dev_err(dev, "dpcon_enable() failed\n");
2653                goto close;
2654        }
2655
2656        return dpcon;
2657
2658close:
2659        dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
2660free:
2661        fsl_mc_object_free(dpcon);
2662
2663        return ERR_PTR(err);
2664}
2665
2666static void dpaa2_eth_free_dpcon(struct dpaa2_eth_priv *priv,
2667                                 struct fsl_mc_device *dpcon)
2668{
2669        dpcon_disable(priv->mc_io, 0, dpcon->mc_handle);
2670        dpcon_close(priv->mc_io, 0, dpcon->mc_handle);
2671        fsl_mc_object_free(dpcon);
2672}
2673
2674static struct dpaa2_eth_channel *dpaa2_eth_alloc_channel(struct dpaa2_eth_priv *priv)
2675{
2676        struct dpaa2_eth_channel *channel;
2677        struct dpcon_attr attr;
2678        struct device *dev = priv->net_dev->dev.parent;
2679        int err;
2680
2681        channel = kzalloc(sizeof(*channel), GFP_KERNEL);
2682        if (!channel)
2683                return NULL;
2684
2685        channel->dpcon = dpaa2_eth_setup_dpcon(priv);
2686        if (IS_ERR(channel->dpcon)) {
2687                err = PTR_ERR(channel->dpcon);
2688                goto err_setup;
2689        }
2690
2691        err = dpcon_get_attributes(priv->mc_io, 0, channel->dpcon->mc_handle,
2692                                   &attr);
2693        if (err) {
2694                dev_err(dev, "dpcon_get_attributes() failed\n");
2695                goto err_get_attr;
2696        }
2697
2698        channel->dpcon_id = attr.id;
2699        channel->ch_id = attr.qbman_ch_id;
2700        channel->priv = priv;
2701
2702        return channel;
2703
2704err_get_attr:
2705        dpaa2_eth_free_dpcon(priv, channel->dpcon);
2706err_setup:
2707        kfree(channel);
2708        return ERR_PTR(err);
2709}
2710
2711static void dpaa2_eth_free_channel(struct dpaa2_eth_priv *priv,
2712                                   struct dpaa2_eth_channel *channel)
2713{
2714        dpaa2_eth_free_dpcon(priv, channel->dpcon);
2715        kfree(channel);
2716}
2717
2718/* DPIO setup: allocate and configure QBMan channels, setup core affinity
2719 * and register data availability notifications
2720 */
2721static int dpaa2_eth_setup_dpio(struct dpaa2_eth_priv *priv)
2722{
2723        struct dpaa2_io_notification_ctx *nctx;
2724        struct dpaa2_eth_channel *channel;
2725        struct dpcon_notification_cfg dpcon_notif_cfg;
2726        struct device *dev = priv->net_dev->dev.parent;
2727        int i, err;
2728
2729        /* We want the ability to spread ingress traffic (RX, TX conf) to as
2730         * many cores as possible, so we need one channel for each core
2731         * (unless there's fewer queues than cores, in which case the extra
2732         * channels would be wasted).
2733         * Allocate one channel per core and register it to the core's
2734         * affine DPIO. If not enough channels are available for all cores
2735         * or if some cores don't have an affine DPIO, there will be no
2736         * ingress frame processing on those cores.
2737         */
2738        cpumask_clear(&priv->dpio_cpumask);
2739        for_each_online_cpu(i) {
2740                /* Try to allocate a channel */
2741                channel = dpaa2_eth_alloc_channel(priv);
2742                if (IS_ERR_OR_NULL(channel)) {
2743                        err = PTR_ERR_OR_ZERO(channel);
2744                        if (err != -EPROBE_DEFER)
2745                                dev_info(dev,
2746                                         "No affine channel for cpu %d and above\n", i);
2747                        goto err_alloc_ch;
2748                }
2749
2750                priv->channel[priv->num_channels] = channel;
2751
2752                nctx = &channel->nctx;
2753                nctx->is_cdan = 1;
2754                nctx->cb = dpaa2_eth_cdan_cb;
2755                nctx->id = channel->ch_id;
2756                nctx->desired_cpu = i;
2757
2758                /* Register the new context */
2759                channel->dpio = dpaa2_io_service_select(i);
2760                err = dpaa2_io_service_register(channel->dpio, nctx, dev);
2761                if (err) {
2762                        dev_dbg(dev, "No affine DPIO for cpu %d\n", i);
2763                        /* If no affine DPIO for this core, there's probably
2764                         * none available for next cores either. Signal we want
2765                         * to retry later, in case the DPIO devices weren't
2766                         * probed yet.
2767                         */
2768                        err = -EPROBE_DEFER;
2769                        goto err_service_reg;
2770                }
2771
2772                /* Register DPCON notification with MC */
2773                dpcon_notif_cfg.dpio_id = nctx->dpio_id;
2774                dpcon_notif_cfg.priority = 0;
2775                dpcon_notif_cfg.user_ctx = nctx->qman64;
2776                err = dpcon_set_notification(priv->mc_io, 0,
2777                                             channel->dpcon->mc_handle,
2778                                             &dpcon_notif_cfg);
2779                if (err) {
2780                        dev_err(dev, "dpcon_set_notification failed()\n");
2781                        goto err_set_cdan;
2782                }
2783
2784                /* If we managed to allocate a channel and also found an affine
2785                 * DPIO for this core, add it to the final mask
2786                 */
2787                cpumask_set_cpu(i, &priv->dpio_cpumask);
2788                priv->num_channels++;
2789
2790                /* Stop if we already have enough channels to accommodate all
2791                 * RX and TX conf queues
2792                 */
2793                if (priv->num_channels == priv->dpni_attrs.num_queues)
2794                        break;
2795        }
2796
2797        return 0;
2798
2799err_set_cdan:
2800        dpaa2_io_service_deregister(channel->dpio, nctx, dev);
2801err_service_reg:
2802        dpaa2_eth_free_channel(priv, channel);
2803err_alloc_ch:
2804        if (err == -EPROBE_DEFER) {
2805                for (i = 0; i < priv->num_channels; i++) {
2806                        channel = priv->channel[i];
2807                        nctx = &channel->nctx;
2808                        dpaa2_io_service_deregister(channel->dpio, nctx, dev);
2809                        dpaa2_eth_free_channel(priv, channel);
2810                }
2811                priv->num_channels = 0;
2812                return err;
2813        }
2814
2815        if (cpumask_empty(&priv->dpio_cpumask)) {
2816                dev_err(dev, "No cpu with an affine DPIO/DPCON\n");
2817                return -ENODEV;
2818        }
2819
2820        dev_info(dev, "Cores %*pbl available for processing ingress traffic\n",
2821                 cpumask_pr_args(&priv->dpio_cpumask));
2822
2823        return 0;
2824}
2825
2826static void dpaa2_eth_free_dpio(struct dpaa2_eth_priv *priv)
2827{
2828        struct device *dev = priv->net_dev->dev.parent;
2829        struct dpaa2_eth_channel *ch;
2830        int i;
2831
2832        /* deregister CDAN notifications and free channels */
2833        for (i = 0; i < priv->num_channels; i++) {
2834                ch = priv->channel[i];
2835                dpaa2_io_service_deregister(ch->dpio, &ch->nctx, dev);
2836                dpaa2_eth_free_channel(priv, ch);
2837        }
2838}
2839
2840static struct dpaa2_eth_channel *dpaa2_eth_get_affine_channel(struct dpaa2_eth_priv *priv,
2841                                                              int cpu)
2842{
2843        struct device *dev = priv->net_dev->dev.parent;
2844        int i;
2845
2846        for (i = 0; i < priv->num_channels; i++)
2847                if (priv->channel[i]->nctx.desired_cpu == cpu)
2848                        return priv->channel[i];
2849
2850        /* We should never get here. Issue a warning and return
2851         * the first channel, because it's still better than nothing
2852         */
2853        dev_warn(dev, "No affine channel found for cpu %d\n", cpu);
2854
2855        return priv->channel[0];
2856}
2857
2858static void dpaa2_eth_set_fq_affinity(struct dpaa2_eth_priv *priv)
2859{
2860        struct device *dev = priv->net_dev->dev.parent;
2861        struct dpaa2_eth_fq *fq;
2862        int rx_cpu, txc_cpu;
2863        int i;
2864
2865        /* For each FQ, pick one channel/CPU to deliver frames to.
2866         * This may well change at runtime, either through irqbalance or
2867         * through direct user intervention.
2868         */
2869        rx_cpu = txc_cpu = cpumask_first(&priv->dpio_cpumask);
2870
2871        for (i = 0; i < priv->num_fqs; i++) {
2872                fq = &priv->fq[i];
2873                switch (fq->type) {
2874                case DPAA2_RX_FQ:
2875                case DPAA2_RX_ERR_FQ:
2876                        fq->target_cpu = rx_cpu;
2877                        rx_cpu = cpumask_next(rx_cpu, &priv->dpio_cpumask);
2878                        if (rx_cpu >= nr_cpu_ids)
2879                                rx_cpu = cpumask_first(&priv->dpio_cpumask);
2880                        break;
2881                case DPAA2_TX_CONF_FQ:
2882                        fq->target_cpu = txc_cpu;
2883                        txc_cpu = cpumask_next(txc_cpu, &priv->dpio_cpumask);
2884                        if (txc_cpu >= nr_cpu_ids)
2885                                txc_cpu = cpumask_first(&priv->dpio_cpumask);
2886                        break;
2887                default:
2888                        dev_err(dev, "Unknown FQ type: %d\n", fq->type);
2889                }
2890                fq->channel = dpaa2_eth_get_affine_channel(priv, fq->target_cpu);
2891        }
2892
2893        update_xps(priv);
2894}
2895
2896static void dpaa2_eth_setup_fqs(struct dpaa2_eth_priv *priv)
2897{
2898        int i, j;
2899
2900        /* We have one TxConf FQ per Tx flow.
2901         * The number of Tx and Rx queues is the same.
2902         * Tx queues come first in the fq array.
2903         */
2904        for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2905                priv->fq[priv->num_fqs].type = DPAA2_TX_CONF_FQ;
2906                priv->fq[priv->num_fqs].consume = dpaa2_eth_tx_conf;
2907                priv->fq[priv->num_fqs++].flowid = (u16)i;
2908        }
2909
2910        for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
2911                for (i = 0; i < dpaa2_eth_queue_count(priv); i++) {
2912                        priv->fq[priv->num_fqs].type = DPAA2_RX_FQ;
2913                        priv->fq[priv->num_fqs].consume = dpaa2_eth_rx;
2914                        priv->fq[priv->num_fqs].tc = (u8)j;
2915                        priv->fq[priv->num_fqs++].flowid = (u16)i;
2916                }
2917        }
2918
2919        /* We have exactly one Rx error queue per DPNI */
2920        priv->fq[priv->num_fqs].type = DPAA2_RX_ERR_FQ;
2921        priv->fq[priv->num_fqs++].consume = dpaa2_eth_rx_err;
2922
2923        /* For each FQ, decide on which core to process incoming frames */
2924        dpaa2_eth_set_fq_affinity(priv);
2925}
2926
2927/* Allocate and configure one buffer pool for each interface */
2928static int dpaa2_eth_setup_dpbp(struct dpaa2_eth_priv *priv)
2929{
2930        int err;
2931        struct fsl_mc_device *dpbp_dev;
2932        struct device *dev = priv->net_dev->dev.parent;
2933        struct dpbp_attr dpbp_attrs;
2934
2935        err = fsl_mc_object_allocate(to_fsl_mc_device(dev), FSL_MC_POOL_DPBP,
2936                                     &dpbp_dev);
2937        if (err) {
2938                if (err == -ENXIO)
2939                        err = -EPROBE_DEFER;
2940                else
2941                        dev_err(dev, "DPBP device allocation failed\n");
2942                return err;
2943        }
2944
2945        priv->dpbp_dev = dpbp_dev;
2946
2947        err = dpbp_open(priv->mc_io, 0, priv->dpbp_dev->obj_desc.id,
2948                        &dpbp_dev->mc_handle);
2949        if (err) {
2950                dev_err(dev, "dpbp_open() failed\n");
2951                goto err_open;
2952        }
2953
2954        err = dpbp_reset(priv->mc_io, 0, dpbp_dev->mc_handle);
2955        if (err) {
2956                dev_err(dev, "dpbp_reset() failed\n");
2957                goto err_reset;
2958        }
2959
2960        err = dpbp_enable(priv->mc_io, 0, dpbp_dev->mc_handle);
2961        if (err) {
2962                dev_err(dev, "dpbp_enable() failed\n");
2963                goto err_enable;
2964        }
2965
2966        err = dpbp_get_attributes(priv->mc_io, 0, dpbp_dev->mc_handle,
2967                                  &dpbp_attrs);
2968        if (err) {
2969                dev_err(dev, "dpbp_get_attributes() failed\n");
2970                goto err_get_attr;
2971        }
2972        priv->bpid = dpbp_attrs.bpid;
2973
2974        return 0;
2975
2976err_get_attr:
2977        dpbp_disable(priv->mc_io, 0, dpbp_dev->mc_handle);
2978err_enable:
2979err_reset:
2980        dpbp_close(priv->mc_io, 0, dpbp_dev->mc_handle);
2981err_open:
2982        fsl_mc_object_free(dpbp_dev);
2983
2984        return err;
2985}
2986
2987static void dpaa2_eth_free_dpbp(struct dpaa2_eth_priv *priv)
2988{
2989        dpaa2_eth_drain_pool(priv);
2990        dpbp_disable(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2991        dpbp_close(priv->mc_io, 0, priv->dpbp_dev->mc_handle);
2992        fsl_mc_object_free(priv->dpbp_dev);
2993}
2994
2995static int dpaa2_eth_set_buffer_layout(struct dpaa2_eth_priv *priv)
2996{
2997        struct device *dev = priv->net_dev->dev.parent;
2998        struct dpni_buffer_layout buf_layout = {0};
2999        u16 rx_buf_align;
3000        int err;
3001
3002        /* We need to check for WRIOP version 1.0.0, but depending on the MC
3003         * version, this number is not always provided correctly on rev1.
3004         * We need to check for both alternatives in this situation.
3005         */
3006        if (priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(0, 0, 0) ||
3007            priv->dpni_attrs.wriop_version == DPAA2_WRIOP_VERSION(1, 0, 0))
3008                rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN_REV1;
3009        else
3010                rx_buf_align = DPAA2_ETH_RX_BUF_ALIGN;
3011
3012        /* We need to ensure that the buffer size seen by WRIOP is a multiple
3013         * of 64 or 256 bytes depending on the WRIOP version.
3014         */
3015        priv->rx_buf_size = ALIGN_DOWN(DPAA2_ETH_RX_BUF_SIZE, rx_buf_align);
3016
3017        /* tx buffer */
3018        buf_layout.private_data_size = DPAA2_ETH_SWA_SIZE;
3019        buf_layout.pass_timestamp = true;
3020        buf_layout.pass_frame_status = true;
3021        buf_layout.options = DPNI_BUF_LAYOUT_OPT_PRIVATE_DATA_SIZE |
3022                             DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
3023                             DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
3024        err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3025                                     DPNI_QUEUE_TX, &buf_layout);
3026        if (err) {
3027                dev_err(dev, "dpni_set_buffer_layout(TX) failed\n");
3028                return err;
3029        }
3030
3031        /* tx-confirm buffer */
3032        buf_layout.options = DPNI_BUF_LAYOUT_OPT_TIMESTAMP |
3033                             DPNI_BUF_LAYOUT_OPT_FRAME_STATUS;
3034        err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3035                                     DPNI_QUEUE_TX_CONFIRM, &buf_layout);
3036        if (err) {
3037                dev_err(dev, "dpni_set_buffer_layout(TX_CONF) failed\n");
3038                return err;
3039        }
3040
3041        /* Now that we've set our tx buffer layout, retrieve the minimum
3042         * required tx data offset.
3043         */
3044        err = dpni_get_tx_data_offset(priv->mc_io, 0, priv->mc_token,
3045                                      &priv->tx_data_offset);
3046        if (err) {
3047                dev_err(dev, "dpni_get_tx_data_offset() failed\n");
3048                return err;
3049        }
3050
3051        if ((priv->tx_data_offset % 64) != 0)
3052                dev_warn(dev, "Tx data offset (%d) not a multiple of 64B\n",
3053                         priv->tx_data_offset);
3054
3055        /* rx buffer */
3056        buf_layout.pass_frame_status = true;
3057        buf_layout.pass_parser_result = true;
3058        buf_layout.data_align = rx_buf_align;
3059        buf_layout.data_head_room = dpaa2_eth_rx_head_room(priv);
3060        buf_layout.private_data_size = 0;
3061        buf_layout.options = DPNI_BUF_LAYOUT_OPT_PARSER_RESULT |
3062                             DPNI_BUF_LAYOUT_OPT_FRAME_STATUS |
3063                             DPNI_BUF_LAYOUT_OPT_DATA_ALIGN |
3064                             DPNI_BUF_LAYOUT_OPT_DATA_HEAD_ROOM |
3065                             DPNI_BUF_LAYOUT_OPT_TIMESTAMP;
3066        err = dpni_set_buffer_layout(priv->mc_io, 0, priv->mc_token,
3067                                     DPNI_QUEUE_RX, &buf_layout);
3068        if (err) {
3069                dev_err(dev, "dpni_set_buffer_layout(RX) failed\n");
3070                return err;
3071        }
3072
3073        return 0;
3074}
3075
3076#define DPNI_ENQUEUE_FQID_VER_MAJOR     7
3077#define DPNI_ENQUEUE_FQID_VER_MINOR     9
3078
3079static inline int dpaa2_eth_enqueue_qd(struct dpaa2_eth_priv *priv,
3080                                       struct dpaa2_eth_fq *fq,
3081                                       struct dpaa2_fd *fd, u8 prio,
3082                                       u32 num_frames __always_unused,
3083                                       int *frames_enqueued)
3084{
3085        int err;
3086
3087        err = dpaa2_io_service_enqueue_qd(fq->channel->dpio,
3088                                          priv->tx_qdid, prio,
3089                                          fq->tx_qdbin, fd);
3090        if (!err && frames_enqueued)
3091                *frames_enqueued = 1;
3092        return err;
3093}
3094
3095static inline int dpaa2_eth_enqueue_fq_multiple(struct dpaa2_eth_priv *priv,
3096                                                struct dpaa2_eth_fq *fq,
3097                                                struct dpaa2_fd *fd,
3098                                                u8 prio, u32 num_frames,
3099                                                int *frames_enqueued)
3100{
3101        int err;
3102
3103        err = dpaa2_io_service_enqueue_multiple_fq(fq->channel->dpio,
3104                                                   fq->tx_fqid[prio],
3105                                                   fd, num_frames);
3106
3107        if (err == 0)
3108                return -EBUSY;
3109
3110        if (frames_enqueued)
3111                *frames_enqueued = err;
3112        return 0;
3113}
3114
3115static void dpaa2_eth_set_enqueue_mode(struct dpaa2_eth_priv *priv)
3116{
3117        if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3118                                   DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3119                priv->enqueue = dpaa2_eth_enqueue_qd;
3120        else
3121                priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
3122}
3123
3124static int dpaa2_eth_set_pause(struct dpaa2_eth_priv *priv)
3125{
3126        struct device *dev = priv->net_dev->dev.parent;
3127        struct dpni_link_cfg link_cfg = {0};
3128        int err;
3129
3130        /* Get the default link options so we don't override other flags */
3131        err = dpni_get_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
3132        if (err) {
3133                dev_err(dev, "dpni_get_link_cfg() failed\n");
3134                return err;
3135        }
3136
3137        /* By default, enable both Rx and Tx pause frames */
3138        link_cfg.options |= DPNI_LINK_OPT_PAUSE;
3139        link_cfg.options &= ~DPNI_LINK_OPT_ASYM_PAUSE;
3140        err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
3141        if (err) {
3142                dev_err(dev, "dpni_set_link_cfg() failed\n");
3143                return err;
3144        }
3145
3146        priv->link_state.options = link_cfg.options;
3147
3148        return 0;
3149}
3150
3151static void dpaa2_eth_update_tx_fqids(struct dpaa2_eth_priv *priv)
3152{
3153        struct dpni_queue_id qid = {0};
3154        struct dpaa2_eth_fq *fq;
3155        struct dpni_queue queue;
3156        int i, j, err;
3157
3158        /* We only use Tx FQIDs for FQID-based enqueue, so check
3159         * if DPNI version supports it before updating FQIDs
3160         */
3161        if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_ENQUEUE_FQID_VER_MAJOR,
3162                                   DPNI_ENQUEUE_FQID_VER_MINOR) < 0)
3163                return;
3164
3165        for (i = 0; i < priv->num_fqs; i++) {
3166                fq = &priv->fq[i];
3167                if (fq->type != DPAA2_TX_CONF_FQ)
3168                        continue;
3169                for (j = 0; j < dpaa2_eth_tc_count(priv); j++) {
3170                        err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3171                                             DPNI_QUEUE_TX, j, fq->flowid,
3172                                             &queue, &qid);
3173                        if (err)
3174                                goto out_err;
3175
3176                        fq->tx_fqid[j] = qid.fqid;
3177                        if (fq->tx_fqid[j] == 0)
3178                                goto out_err;
3179                }
3180        }
3181
3182        priv->enqueue = dpaa2_eth_enqueue_fq_multiple;
3183
3184        return;
3185
3186out_err:
3187        netdev_info(priv->net_dev,
3188                    "Error reading Tx FQID, fallback to QDID-based enqueue\n");
3189        priv->enqueue = dpaa2_eth_enqueue_qd;
3190}
3191
3192/* Configure ingress classification based on VLAN PCP */
3193static int dpaa2_eth_set_vlan_qos(struct dpaa2_eth_priv *priv)
3194{
3195        struct device *dev = priv->net_dev->dev.parent;
3196        struct dpkg_profile_cfg kg_cfg = {0};
3197        struct dpni_qos_tbl_cfg qos_cfg = {0};
3198        struct dpni_rule_cfg key_params;
3199        void *dma_mem, *key, *mask;
3200        u8 key_size = 2;        /* VLAN TCI field */
3201        int i, pcp, err;
3202
3203        /* VLAN-based classification only makes sense if we have multiple
3204         * traffic classes.
3205         * Also, we need to extract just the 3-bit PCP field from the VLAN
3206         * header and we can only do that by using a mask
3207         */
3208        if (dpaa2_eth_tc_count(priv) == 1 || !dpaa2_eth_fs_mask_enabled(priv)) {
3209                dev_dbg(dev, "VLAN-based QoS classification not supported\n");
3210                return -EOPNOTSUPP;
3211        }
3212
3213        dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
3214        if (!dma_mem)
3215                return -ENOMEM;
3216
3217        kg_cfg.num_extracts = 1;
3218        kg_cfg.extracts[0].type = DPKG_EXTRACT_FROM_HDR;
3219        kg_cfg.extracts[0].extract.from_hdr.prot = NET_PROT_VLAN;
3220        kg_cfg.extracts[0].extract.from_hdr.type = DPKG_FULL_FIELD;
3221        kg_cfg.extracts[0].extract.from_hdr.field = NH_FLD_VLAN_TCI;
3222
3223        err = dpni_prepare_key_cfg(&kg_cfg, dma_mem);
3224        if (err) {
3225                dev_err(dev, "dpni_prepare_key_cfg failed\n");
3226                goto out_free_tbl;
3227        }
3228
3229        /* set QoS table */
3230        qos_cfg.default_tc = 0;
3231        qos_cfg.discard_on_miss = 0;
3232        qos_cfg.key_cfg_iova = dma_map_single(dev, dma_mem,
3233                                              DPAA2_CLASSIFIER_DMA_SIZE,
3234                                              DMA_TO_DEVICE);
3235        if (dma_mapping_error(dev, qos_cfg.key_cfg_iova)) {
3236                dev_err(dev, "QoS table DMA mapping failed\n");
3237                err = -ENOMEM;
3238                goto out_free_tbl;
3239        }
3240
3241        err = dpni_set_qos_table(priv->mc_io, 0, priv->mc_token, &qos_cfg);
3242        if (err) {
3243                dev_err(dev, "dpni_set_qos_table failed\n");
3244                goto out_unmap_tbl;
3245        }
3246
3247        /* Add QoS table entries */
3248        key = kzalloc(key_size * 2, GFP_KERNEL);
3249        if (!key) {
3250                err = -ENOMEM;
3251                goto out_unmap_tbl;
3252        }
3253        mask = key + key_size;
3254        *(__be16 *)mask = cpu_to_be16(VLAN_PRIO_MASK);
3255
3256        key_params.key_iova = dma_map_single(dev, key, key_size * 2,
3257                                             DMA_TO_DEVICE);
3258        if (dma_mapping_error(dev, key_params.key_iova)) {
3259                dev_err(dev, "Qos table entry DMA mapping failed\n");
3260                err = -ENOMEM;
3261                goto out_free_key;
3262        }
3263
3264        key_params.mask_iova = key_params.key_iova + key_size;
3265        key_params.key_size = key_size;
3266
3267        /* We add rules for PCP-based distribution starting with highest
3268         * priority (VLAN PCP = 7). If this DPNI doesn't have enough traffic
3269         * classes to accommodate all priority levels, the lowest ones end up
3270         * on TC 0 which was configured as default
3271         */
3272        for (i = dpaa2_eth_tc_count(priv) - 1, pcp = 7; i >= 0; i--, pcp--) {
3273                *(__be16 *)key = cpu_to_be16(pcp << VLAN_PRIO_SHIFT);
3274                dma_sync_single_for_device(dev, key_params.key_iova,
3275                                           key_size * 2, DMA_TO_DEVICE);
3276
3277                err = dpni_add_qos_entry(priv->mc_io, 0, priv->mc_token,
3278                                         &key_params, i, i);
3279                if (err) {
3280                        dev_err(dev, "dpni_add_qos_entry failed\n");
3281                        dpni_clear_qos_table(priv->mc_io, 0, priv->mc_token);
3282                        goto out_unmap_key;
3283                }
3284        }
3285
3286        priv->vlan_cls_enabled = true;
3287
3288        /* Table and key memory is not persistent, clean everything up after
3289         * configuration is finished
3290         */
3291out_unmap_key:
3292        dma_unmap_single(dev, key_params.key_iova, key_size * 2, DMA_TO_DEVICE);
3293out_free_key:
3294        kfree(key);
3295out_unmap_tbl:
3296        dma_unmap_single(dev, qos_cfg.key_cfg_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3297                         DMA_TO_DEVICE);
3298out_free_tbl:
3299        kfree(dma_mem);
3300
3301        return err;
3302}
3303
3304/* Configure the DPNI object this interface is associated with */
3305static int dpaa2_eth_setup_dpni(struct fsl_mc_device *ls_dev)
3306{
3307        struct device *dev = &ls_dev->dev;
3308        struct dpaa2_eth_priv *priv;
3309        struct net_device *net_dev;
3310        int err;
3311
3312        net_dev = dev_get_drvdata(dev);
3313        priv = netdev_priv(net_dev);
3314
3315        /* get a handle for the DPNI object */
3316        err = dpni_open(priv->mc_io, 0, ls_dev->obj_desc.id, &priv->mc_token);
3317        if (err) {
3318                dev_err(dev, "dpni_open() failed\n");
3319                return err;
3320        }
3321
3322        /* Check if we can work with this DPNI object */
3323        err = dpni_get_api_version(priv->mc_io, 0, &priv->dpni_ver_major,
3324                                   &priv->dpni_ver_minor);
3325        if (err) {
3326                dev_err(dev, "dpni_get_api_version() failed\n");
3327                goto close;
3328        }
3329        if (dpaa2_eth_cmp_dpni_ver(priv, DPNI_VER_MAJOR, DPNI_VER_MINOR) < 0) {
3330                dev_err(dev, "DPNI version %u.%u not supported, need >= %u.%u\n",
3331                        priv->dpni_ver_major, priv->dpni_ver_minor,
3332                        DPNI_VER_MAJOR, DPNI_VER_MINOR);
3333                err = -ENOTSUPP;
3334                goto close;
3335        }
3336
3337        ls_dev->mc_io = priv->mc_io;
3338        ls_dev->mc_handle = priv->mc_token;
3339
3340        err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3341        if (err) {
3342                dev_err(dev, "dpni_reset() failed\n");
3343                goto close;
3344        }
3345
3346        err = dpni_get_attributes(priv->mc_io, 0, priv->mc_token,
3347                                  &priv->dpni_attrs);
3348        if (err) {
3349                dev_err(dev, "dpni_get_attributes() failed (err=%d)\n", err);
3350                goto close;
3351        }
3352
3353        err = dpaa2_eth_set_buffer_layout(priv);
3354        if (err)
3355                goto close;
3356
3357        dpaa2_eth_set_enqueue_mode(priv);
3358
3359        /* Enable pause frame support */
3360        if (dpaa2_eth_has_pause_support(priv)) {
3361                err = dpaa2_eth_set_pause(priv);
3362                if (err)
3363                        goto close;
3364        }
3365
3366        err = dpaa2_eth_set_vlan_qos(priv);
3367        if (err && err != -EOPNOTSUPP)
3368                goto close;
3369
3370        priv->cls_rules = devm_kcalloc(dev, dpaa2_eth_fs_count(priv),
3371                                       sizeof(struct dpaa2_eth_cls_rule),
3372                                       GFP_KERNEL);
3373        if (!priv->cls_rules) {
3374                err = -ENOMEM;
3375                goto close;
3376        }
3377
3378        return 0;
3379
3380close:
3381        dpni_close(priv->mc_io, 0, priv->mc_token);
3382
3383        return err;
3384}
3385
3386static void dpaa2_eth_free_dpni(struct dpaa2_eth_priv *priv)
3387{
3388        int err;
3389
3390        err = dpni_reset(priv->mc_io, 0, priv->mc_token);
3391        if (err)
3392                netdev_warn(priv->net_dev, "dpni_reset() failed (err %d)\n",
3393                            err);
3394
3395        dpni_close(priv->mc_io, 0, priv->mc_token);
3396}
3397
3398static int dpaa2_eth_setup_rx_flow(struct dpaa2_eth_priv *priv,
3399                                   struct dpaa2_eth_fq *fq)
3400{
3401        struct device *dev = priv->net_dev->dev.parent;
3402        struct dpni_queue queue;
3403        struct dpni_queue_id qid;
3404        int err;
3405
3406        err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3407                             DPNI_QUEUE_RX, fq->tc, fq->flowid, &queue, &qid);
3408        if (err) {
3409                dev_err(dev, "dpni_get_queue(RX) failed\n");
3410                return err;
3411        }
3412
3413        fq->fqid = qid.fqid;
3414
3415        queue.destination.id = fq->channel->dpcon_id;
3416        queue.destination.type = DPNI_DEST_DPCON;
3417        queue.destination.priority = 1;
3418        queue.user_context = (u64)(uintptr_t)fq;
3419        err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3420                             DPNI_QUEUE_RX, fq->tc, fq->flowid,
3421                             DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3422                             &queue);
3423        if (err) {
3424                dev_err(dev, "dpni_set_queue(RX) failed\n");
3425                return err;
3426        }
3427
3428        /* xdp_rxq setup */
3429        /* only once for each channel */
3430        if (fq->tc > 0)
3431                return 0;
3432
3433        err = xdp_rxq_info_reg(&fq->channel->xdp_rxq, priv->net_dev,
3434                               fq->flowid, 0);
3435        if (err) {
3436                dev_err(dev, "xdp_rxq_info_reg failed\n");
3437                return err;
3438        }
3439
3440        err = xdp_rxq_info_reg_mem_model(&fq->channel->xdp_rxq,
3441                                         MEM_TYPE_PAGE_ORDER0, NULL);
3442        if (err) {
3443                dev_err(dev, "xdp_rxq_info_reg_mem_model failed\n");
3444                return err;
3445        }
3446
3447        return 0;
3448}
3449
3450static int dpaa2_eth_setup_tx_flow(struct dpaa2_eth_priv *priv,
3451                                   struct dpaa2_eth_fq *fq)
3452{
3453        struct device *dev = priv->net_dev->dev.parent;
3454        struct dpni_queue queue;
3455        struct dpni_queue_id qid;
3456        int i, err;
3457
3458        for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3459                err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3460                                     DPNI_QUEUE_TX, i, fq->flowid,
3461                                     &queue, &qid);
3462                if (err) {
3463                        dev_err(dev, "dpni_get_queue(TX) failed\n");
3464                        return err;
3465                }
3466                fq->tx_fqid[i] = qid.fqid;
3467        }
3468
3469        /* All Tx queues belonging to the same flowid have the same qdbin */
3470        fq->tx_qdbin = qid.qdbin;
3471
3472        err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3473                             DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3474                             &queue, &qid);
3475        if (err) {
3476                dev_err(dev, "dpni_get_queue(TX_CONF) failed\n");
3477                return err;
3478        }
3479
3480        fq->fqid = qid.fqid;
3481
3482        queue.destination.id = fq->channel->dpcon_id;
3483        queue.destination.type = DPNI_DEST_DPCON;
3484        queue.destination.priority = 0;
3485        queue.user_context = (u64)(uintptr_t)fq;
3486        err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3487                             DPNI_QUEUE_TX_CONFIRM, 0, fq->flowid,
3488                             DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST,
3489                             &queue);
3490        if (err) {
3491                dev_err(dev, "dpni_set_queue(TX_CONF) failed\n");
3492                return err;
3493        }
3494
3495        return 0;
3496}
3497
3498static int setup_rx_err_flow(struct dpaa2_eth_priv *priv,
3499                             struct dpaa2_eth_fq *fq)
3500{
3501        struct device *dev = priv->net_dev->dev.parent;
3502        struct dpni_queue q = { { 0 } };
3503        struct dpni_queue_id qid;
3504        u8 q_opt = DPNI_QUEUE_OPT_USER_CTX | DPNI_QUEUE_OPT_DEST;
3505        int err;
3506
3507        err = dpni_get_queue(priv->mc_io, 0, priv->mc_token,
3508                             DPNI_QUEUE_RX_ERR, 0, 0, &q, &qid);
3509        if (err) {
3510                dev_err(dev, "dpni_get_queue() failed (%d)\n", err);
3511                return err;
3512        }
3513
3514        fq->fqid = qid.fqid;
3515
3516        q.destination.id = fq->channel->dpcon_id;
3517        q.destination.type = DPNI_DEST_DPCON;
3518        q.destination.priority = 1;
3519        q.user_context = (u64)(uintptr_t)fq;
3520        err = dpni_set_queue(priv->mc_io, 0, priv->mc_token,
3521                             DPNI_QUEUE_RX_ERR, 0, 0, q_opt, &q);
3522        if (err) {
3523                dev_err(dev, "dpni_set_queue() failed (%d)\n", err);
3524                return err;
3525        }
3526
3527        return 0;
3528}
3529
3530/* Supported header fields for Rx hash distribution key */
3531static const struct dpaa2_eth_dist_fields dist_fields[] = {
3532        {
3533                /* L2 header */
3534                .rxnfc_field = RXH_L2DA,
3535                .cls_prot = NET_PROT_ETH,
3536                .cls_field = NH_FLD_ETH_DA,
3537                .id = DPAA2_ETH_DIST_ETHDST,
3538                .size = 6,
3539        }, {
3540                .cls_prot = NET_PROT_ETH,
3541                .cls_field = NH_FLD_ETH_SA,
3542                .id = DPAA2_ETH_DIST_ETHSRC,
3543                .size = 6,
3544        }, {
3545                /* This is the last ethertype field parsed:
3546                 * depending on frame format, it can be the MAC ethertype
3547                 * or the VLAN etype.
3548                 */
3549                .cls_prot = NET_PROT_ETH,
3550                .cls_field = NH_FLD_ETH_TYPE,
3551                .id = DPAA2_ETH_DIST_ETHTYPE,
3552                .size = 2,
3553        }, {
3554                /* VLAN header */
3555                .rxnfc_field = RXH_VLAN,
3556                .cls_prot = NET_PROT_VLAN,
3557                .cls_field = NH_FLD_VLAN_TCI,
3558                .id = DPAA2_ETH_DIST_VLAN,
3559                .size = 2,
3560        }, {
3561                /* IP header */
3562                .rxnfc_field = RXH_IP_SRC,
3563                .cls_prot = NET_PROT_IP,
3564                .cls_field = NH_FLD_IP_SRC,
3565                .id = DPAA2_ETH_DIST_IPSRC,
3566                .size = 4,
3567        }, {
3568                .rxnfc_field = RXH_IP_DST,
3569                .cls_prot = NET_PROT_IP,
3570                .cls_field = NH_FLD_IP_DST,
3571                .id = DPAA2_ETH_DIST_IPDST,
3572                .size = 4,
3573        }, {
3574                .rxnfc_field = RXH_L3_PROTO,
3575                .cls_prot = NET_PROT_IP,
3576                .cls_field = NH_FLD_IP_PROTO,
3577                .id = DPAA2_ETH_DIST_IPPROTO,
3578                .size = 1,
3579        }, {
3580                /* Using UDP ports, this is functionally equivalent to raw
3581                 * byte pairs from L4 header.
3582                 */
3583                .rxnfc_field = RXH_L4_B_0_1,
3584                .cls_prot = NET_PROT_UDP,
3585                .cls_field = NH_FLD_UDP_PORT_SRC,
3586                .id = DPAA2_ETH_DIST_L4SRC,
3587                .size = 2,
3588        }, {
3589                .rxnfc_field = RXH_L4_B_2_3,
3590                .cls_prot = NET_PROT_UDP,
3591                .cls_field = NH_FLD_UDP_PORT_DST,
3592                .id = DPAA2_ETH_DIST_L4DST,
3593                .size = 2,
3594        },
3595};
3596
3597/* Configure the Rx hash key using the legacy API */
3598static int dpaa2_eth_config_legacy_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
3599{
3600        struct device *dev = priv->net_dev->dev.parent;
3601        struct dpni_rx_tc_dist_cfg dist_cfg;
3602        int i, err = 0;
3603
3604        memset(&dist_cfg, 0, sizeof(dist_cfg));
3605
3606        dist_cfg.key_cfg_iova = key;
3607        dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3608        dist_cfg.dist_mode = DPNI_DIST_MODE_HASH;
3609
3610        for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3611                err = dpni_set_rx_tc_dist(priv->mc_io, 0, priv->mc_token,
3612                                          i, &dist_cfg);
3613                if (err) {
3614                        dev_err(dev, "dpni_set_rx_tc_dist failed\n");
3615                        break;
3616                }
3617        }
3618
3619        return err;
3620}
3621
3622/* Configure the Rx hash key using the new API */
3623static int dpaa2_eth_config_hash_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
3624{
3625        struct device *dev = priv->net_dev->dev.parent;
3626        struct dpni_rx_dist_cfg dist_cfg;
3627        int i, err = 0;
3628
3629        memset(&dist_cfg, 0, sizeof(dist_cfg));
3630
3631        dist_cfg.key_cfg_iova = key;
3632        dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3633        dist_cfg.enable = 1;
3634
3635        for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3636                dist_cfg.tc = i;
3637                err = dpni_set_rx_hash_dist(priv->mc_io, 0, priv->mc_token,
3638                                            &dist_cfg);
3639                if (err) {
3640                        dev_err(dev, "dpni_set_rx_hash_dist failed\n");
3641                        break;
3642                }
3643
3644                /* If the flow steering / hashing key is shared between all
3645                 * traffic classes, install it just once
3646                 */
3647                if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
3648                        break;
3649        }
3650
3651        return err;
3652}
3653
3654/* Configure the Rx flow classification key */
3655static int dpaa2_eth_config_cls_key(struct dpaa2_eth_priv *priv, dma_addr_t key)
3656{
3657        struct device *dev = priv->net_dev->dev.parent;
3658        struct dpni_rx_dist_cfg dist_cfg;
3659        int i, err = 0;
3660
3661        memset(&dist_cfg, 0, sizeof(dist_cfg));
3662
3663        dist_cfg.key_cfg_iova = key;
3664        dist_cfg.dist_size = dpaa2_eth_queue_count(priv);
3665        dist_cfg.enable = 1;
3666
3667        for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
3668                dist_cfg.tc = i;
3669                err = dpni_set_rx_fs_dist(priv->mc_io, 0, priv->mc_token,
3670                                          &dist_cfg);
3671                if (err) {
3672                        dev_err(dev, "dpni_set_rx_fs_dist failed\n");
3673                        break;
3674                }
3675
3676                /* If the flow steering / hashing key is shared between all
3677                 * traffic classes, install it just once
3678                 */
3679                if (priv->dpni_attrs.options & DPNI_OPT_SHARED_FS)
3680                        break;
3681        }
3682
3683        return err;
3684}
3685
3686/* Size of the Rx flow classification key */
3687int dpaa2_eth_cls_key_size(u64 fields)
3688{
3689        int i, size = 0;
3690
3691        for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3692                if (!(fields & dist_fields[i].id))
3693                        continue;
3694                size += dist_fields[i].size;
3695        }
3696
3697        return size;
3698}
3699
3700/* Offset of header field in Rx classification key */
3701int dpaa2_eth_cls_fld_off(int prot, int field)
3702{
3703        int i, off = 0;
3704
3705        for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3706                if (dist_fields[i].cls_prot == prot &&
3707                    dist_fields[i].cls_field == field)
3708                        return off;
3709                off += dist_fields[i].size;
3710        }
3711
3712        WARN_ONCE(1, "Unsupported header field used for Rx flow cls\n");
3713        return 0;
3714}
3715
3716/* Prune unused fields from the classification rule.
3717 * Used when masking is not supported
3718 */
3719void dpaa2_eth_cls_trim_rule(void *key_mem, u64 fields)
3720{
3721        int off = 0, new_off = 0;
3722        int i, size;
3723
3724        for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3725                size = dist_fields[i].size;
3726                if (dist_fields[i].id & fields) {
3727                        memcpy(key_mem + new_off, key_mem + off, size);
3728                        new_off += size;
3729                }
3730                off += size;
3731        }
3732}
3733
3734/* Set Rx distribution (hash or flow classification) key
3735 * flags is a combination of RXH_ bits
3736 */
3737static int dpaa2_eth_set_dist_key(struct net_device *net_dev,
3738                                  enum dpaa2_eth_rx_dist type, u64 flags)
3739{
3740        struct device *dev = net_dev->dev.parent;
3741        struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
3742        struct dpkg_profile_cfg cls_cfg;
3743        u32 rx_hash_fields = 0;
3744        dma_addr_t key_iova;
3745        u8 *dma_mem;
3746        int i;
3747        int err = 0;
3748
3749        memset(&cls_cfg, 0, sizeof(cls_cfg));
3750
3751        for (i = 0; i < ARRAY_SIZE(dist_fields); i++) {
3752                struct dpkg_extract *key =
3753                        &cls_cfg.extracts[cls_cfg.num_extracts];
3754
3755                /* For both Rx hashing and classification keys
3756                 * we set only the selected fields.
3757                 */
3758                if (!(flags & dist_fields[i].id))
3759                        continue;
3760                if (type == DPAA2_ETH_RX_DIST_HASH)
3761                        rx_hash_fields |= dist_fields[i].rxnfc_field;
3762
3763                if (cls_cfg.num_extracts >= DPKG_MAX_NUM_OF_EXTRACTS) {
3764                        dev_err(dev, "error adding key extraction rule, too many rules?\n");
3765                        return -E2BIG;
3766                }
3767
3768                key->type = DPKG_EXTRACT_FROM_HDR;
3769                key->extract.from_hdr.prot = dist_fields[i].cls_prot;
3770                key->extract.from_hdr.type = DPKG_FULL_FIELD;
3771                key->extract.from_hdr.field = dist_fields[i].cls_field;
3772                cls_cfg.num_extracts++;
3773        }
3774
3775        dma_mem = kzalloc(DPAA2_CLASSIFIER_DMA_SIZE, GFP_KERNEL);
3776        if (!dma_mem)
3777                return -ENOMEM;
3778
3779        err = dpni_prepare_key_cfg(&cls_cfg, dma_mem);
3780        if (err) {
3781                dev_err(dev, "dpni_prepare_key_cfg error %d\n", err);
3782                goto free_key;
3783        }
3784
3785        /* Prepare for setting the rx dist */
3786        key_iova = dma_map_single(dev, dma_mem, DPAA2_CLASSIFIER_DMA_SIZE,
3787                                  DMA_TO_DEVICE);
3788        if (dma_mapping_error(dev, key_iova)) {
3789                dev_err(dev, "DMA mapping failed\n");
3790                err = -ENOMEM;
3791                goto free_key;
3792        }
3793
3794        if (type == DPAA2_ETH_RX_DIST_HASH) {
3795                if (dpaa2_eth_has_legacy_dist(priv))
3796                        err = dpaa2_eth_config_legacy_hash_key(priv, key_iova);
3797                else
3798                        err = dpaa2_eth_config_hash_key(priv, key_iova);
3799        } else {
3800                err = dpaa2_eth_config_cls_key(priv, key_iova);
3801        }
3802
3803        dma_unmap_single(dev, key_iova, DPAA2_CLASSIFIER_DMA_SIZE,
3804                         DMA_TO_DEVICE);
3805        if (!err && type == DPAA2_ETH_RX_DIST_HASH)
3806                priv->rx_hash_fields = rx_hash_fields;
3807
3808free_key:
3809        kfree(dma_mem);
3810        return err;
3811}
3812
3813int dpaa2_eth_set_hash(struct net_device *net_dev, u64 flags)
3814{
3815        struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
3816        u64 key = 0;
3817        int i;
3818
3819        if (!dpaa2_eth_hash_enabled(priv))
3820                return -EOPNOTSUPP;
3821
3822        for (i = 0; i < ARRAY_SIZE(dist_fields); i++)
3823                if (dist_fields[i].rxnfc_field & flags)
3824                        key |= dist_fields[i].id;
3825
3826        return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_HASH, key);
3827}
3828
3829int dpaa2_eth_set_cls(struct net_device *net_dev, u64 flags)
3830{
3831        return dpaa2_eth_set_dist_key(net_dev, DPAA2_ETH_RX_DIST_CLS, flags);
3832}
3833
3834static int dpaa2_eth_set_default_cls(struct dpaa2_eth_priv *priv)
3835{
3836        struct device *dev = priv->net_dev->dev.parent;
3837        int err;