LCOV - code coverage report
Current view: top level - drivers/net/enic - enic_main.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 855 0.0 %
Date: 2026-11-01 19:11:22 Functions: 0 52 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 470 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright 2008-2017 Cisco Systems, Inc.  All rights reserved.
       3                 :            :  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include <stdio.h>
       7                 :            : 
       8                 :            : #include <sys/stat.h>
       9                 :            : #include <sys/mman.h>
      10                 :            : #include <fcntl.h>
      11                 :            : 
      12                 :            : #include <rte_pci.h>
      13                 :            : #include <bus_pci_driver.h>
      14                 :            : #include <rte_memzone.h>
      15                 :            : #include <rte_malloc.h>
      16                 :            : #include <rte_mbuf.h>
      17                 :            : #include <rte_string_fns.h>
      18                 :            : #include <ethdev_driver.h>
      19                 :            : #include <rte_geneve.h>
      20                 :            : 
      21                 :            : #include "enic_compat.h"
      22                 :            : #include "enic.h"
      23                 :            : #include "enic_sriov.h"
      24                 :            : #include "wq_enet_desc.h"
      25                 :            : #include "rq_enet_desc.h"
      26                 :            : #include "cq_enet_desc.h"
      27                 :            : #include "vnic_enet.h"
      28                 :            : #include "vnic_dev.h"
      29                 :            : #include "vnic_wq.h"
      30                 :            : #include "vnic_rq.h"
      31                 :            : #include "vnic_cq.h"
      32                 :            : #include "vnic_intr.h"
      33                 :            : #include "vnic_nic.h"
      34                 :            : 
      35                 :            : void
      36                 :          0 : enic_rxmbuf_queue_release(__rte_unused struct enic *enic, struct vnic_rq *rq)
      37                 :            : {
      38                 :            :         uint16_t i;
      39                 :            : 
      40   [ #  #  #  # ]:          0 :         if (!rq || !rq->mbuf_ring) {
      41                 :          0 :                 dev_debug(enic, "Pointer to rq or mbuf_ring is NULL");
      42                 :          0 :                 return;
      43                 :            :         }
      44                 :            : 
      45         [ #  # ]:          0 :         for (i = 0; i < rq->ring.desc_count; i++) {
      46         [ #  # ]:          0 :                 if (rq->mbuf_ring[i]) {
      47                 :            :                         rte_pktmbuf_free_seg(rq->mbuf_ring[i]);
      48                 :          0 :                         rq->mbuf_ring[i] = NULL;
      49                 :            :                 }
      50                 :            :         }
      51                 :            : }
      52                 :            : 
      53                 :          0 : void enic_free_wq_buf(struct rte_mbuf **buf)
      54                 :            : {
      55                 :          0 :         struct rte_mbuf *mbuf = *buf;
      56                 :            : 
      57                 :            :         rte_pktmbuf_free_seg(mbuf);
      58                 :          0 :         *buf = NULL;
      59                 :          0 : }
      60                 :            : 
      61                 :          0 : static void enic_log_q_error(struct enic *enic)
      62                 :            : {
      63                 :            :         unsigned int i;
      64                 :            :         uint32_t error_status;
      65                 :            : 
      66         [ #  # ]:          0 :         for (i = 0; i < enic->wq_count; i++) {
      67                 :          0 :                 error_status = vnic_wq_error_status(&enic->wq[i]);
      68         [ #  # ]:          0 :                 if (error_status)
      69                 :          0 :                         dev_err(enic, "WQ[%d] error_status %d\n", i,
      70                 :            :                                 error_status);
      71                 :            :         }
      72                 :            : 
      73         [ #  # ]:          0 :         for (i = 0; i < enic_vnic_rq_count(enic); i++) {
      74         [ #  # ]:          0 :                 if (!enic->rq[i].in_use)
      75                 :          0 :                         continue;
      76                 :          0 :                 error_status = vnic_rq_error_status(&enic->rq[i]);
      77         [ #  # ]:          0 :                 if (error_status)
      78                 :          0 :                         dev_err(enic, "RQ[%d] error_status %d\n", i,
      79                 :            :                                 error_status);
      80                 :            :         }
      81                 :          0 : }
      82                 :            : 
      83                 :            : static void enic_clear_soft_stats(struct enic *enic)
      84                 :            : {
      85         [ #  # ]:          0 :         struct enic_soft_stats *soft_stats = &enic->soft_stats;
      86                 :            : 
      87                 :            :         memset(soft_stats, 0, sizeof(*soft_stats));
      88                 :            : }
      89                 :            : 
      90                 :          0 : int enic_dev_stats_clear(struct enic *enic)
      91                 :            : {
      92                 :            :         int ret;
      93                 :            : 
      94                 :          0 :         ret = vnic_dev_stats_clear(enic->vdev);
      95         [ #  # ]:          0 :         if (ret != 0) {
      96                 :          0 :                 dev_err(enic, "Error in clearing stats\n");
      97                 :          0 :                 return ret;
      98                 :            :         }
      99                 :            :         enic_clear_soft_stats(enic);
     100                 :            : 
     101                 :          0 :         return 0;
     102                 :            : }
     103                 :            : 
     104                 :          0 : int enic_dev_stats_get(struct enic *enic, struct rte_eth_stats *r_stats,
     105                 :            :                         struct eth_queue_stats *qstats __rte_unused)
     106                 :            : {
     107                 :            :         struct vnic_stats *stats;
     108                 :            :         struct enic_soft_stats *soft_stats = &enic->soft_stats;
     109                 :            :         int64_t rx_truncated;
     110                 :            :         uint64_t rx_packet_errors;
     111                 :          0 :         int ret = vnic_dev_stats_dump(enic->vdev, &stats);
     112                 :            : 
     113         [ #  # ]:          0 :         if (ret) {
     114                 :          0 :                 dev_err(enic, "Error in getting stats\n");
     115                 :          0 :                 return ret;
     116                 :            :         }
     117                 :            : 
     118                 :            :         /* The number of truncated packets can only be calculated by
     119                 :            :          * subtracting a hardware counter from error packets received by
     120                 :            :          * the driver. Note: this causes transient inaccuracies in the
     121                 :            :          * ipackets count. Also, the length of truncated packets are
     122                 :            :          * counted in ibytes even though truncated packets are dropped
     123                 :            :          * which can make ibytes be slightly higher than it should be.
     124                 :            :          */
     125                 :          0 :         rx_packet_errors = soft_stats->rx_packet_errors;
     126                 :          0 :         rx_truncated = rx_packet_errors - stats->rx.rx_errors;
     127                 :            : 
     128                 :          0 :         r_stats->ipackets = stats->rx.rx_frames_ok - rx_truncated;
     129                 :          0 :         r_stats->opackets = stats->tx.tx_frames_ok;
     130                 :            : 
     131                 :          0 :         r_stats->ibytes = stats->rx.rx_bytes_ok;
     132                 :          0 :         r_stats->obytes = stats->tx.tx_bytes_ok;
     133                 :            : 
     134                 :          0 :         r_stats->ierrors = stats->rx.rx_errors + stats->rx.rx_drop;
     135                 :          0 :         r_stats->oerrors = stats->tx.tx_errors + soft_stats->tx_oversized;
     136                 :            : 
     137                 :          0 :         r_stats->imissed = stats->rx.rx_no_bufs + rx_truncated;
     138                 :            : 
     139                 :          0 :         r_stats->rx_nombuf = soft_stats->rx_nombuf;
     140                 :          0 :         return 0;
     141                 :            : }
     142                 :            : 
     143                 :          0 : int enic_del_mac_address(struct enic *enic, int mac_index)
     144                 :            : {
     145                 :          0 :         struct rte_eth_dev *eth_dev = enic->rte_dev;
     146                 :          0 :         uint8_t *mac_addr = eth_dev->data->mac_addrs[mac_index].addr_bytes;
     147                 :            : 
     148                 :          0 :         return enic_dev_del_addr(enic, mac_addr);
     149                 :            : }
     150                 :            : 
     151                 :          0 : int enic_set_mac_address(struct enic *enic, uint8_t *mac_addr)
     152                 :            : {
     153                 :            :         int err;
     154                 :            : 
     155                 :          0 :         err = enic_dev_add_addr(enic, mac_addr);
     156         [ #  # ]:          0 :         if (err)
     157                 :          0 :                 dev_err(enic, "add mac addr failed\n");
     158                 :          0 :         return err;
     159                 :            : }
     160                 :            : 
     161                 :          0 : void enic_free_rq_buf(struct rte_mbuf **mbuf)
     162                 :            : {
     163         [ #  # ]:          0 :         if (*mbuf == NULL)
     164                 :            :                 return;
     165                 :            : 
     166                 :          0 :         rte_pktmbuf_free(*mbuf);
     167                 :          0 :         *mbuf = NULL;
     168                 :            : }
     169                 :            : 
     170                 :          0 : void enic_init_vnic_resources(struct enic *enic)
     171                 :            : {
     172                 :            :         unsigned int error_interrupt_enable = 1;
     173                 :            :         unsigned int error_interrupt_offset = 0;
     174                 :            :         unsigned int rxq_interrupt_enable = 0;
     175                 :            :         unsigned int rxq_interrupt_offset = ENICPMD_RXQ_INTR_OFFSET;
     176                 :            :         unsigned int index = 0;
     177                 :            :         unsigned int cq_idx;
     178                 :            :         struct vnic_rq *data_rq;
     179                 :            : 
     180         [ #  # ]:          0 :         if (enic->rte_dev->data->dev_conf.intr_conf.rxq)
     181                 :            :                 rxq_interrupt_enable = 1;
     182                 :            : 
     183         [ #  # ]:          0 :         for (index = 0; index < enic->rq_count; index++) {
     184                 :            :                 cq_idx = enic_cq_rq(enic, enic_rte_rq_idx_to_sop_idx(index));
     185                 :            : 
     186                 :          0 :                 vnic_rq_init(&enic->rq[enic_rte_rq_idx_to_sop_idx(index)],
     187                 :            :                         cq_idx,
     188                 :            :                         error_interrupt_enable,
     189                 :            :                         error_interrupt_offset);
     190                 :            : 
     191         [ #  # ]:          0 :                 data_rq = &enic->rq[enic_rte_rq_idx_to_data_idx(index, enic)];
     192         [ #  # ]:          0 :                 if (data_rq->in_use)
     193                 :          0 :                         vnic_rq_init(data_rq,
     194                 :            :                                      cq_idx,
     195                 :            :                                      error_interrupt_enable,
     196                 :            :                                      error_interrupt_offset);
     197                 :          0 :                 vnic_cq_init(&enic->cq[cq_idx],
     198                 :            :                         0 /* flow_control_enable */,
     199                 :            :                         1 /* color_enable */,
     200                 :            :                         0 /* cq_head */,
     201                 :            :                         0 /* cq_tail */,
     202                 :            :                         1 /* cq_tail_color */,
     203                 :            :                         rxq_interrupt_enable,
     204                 :            :                         1 /* cq_entry_enable */,
     205                 :            :                         0 /* cq_message_enable */,
     206                 :            :                         rxq_interrupt_offset,
     207                 :            :                         0 /* cq_message_addr */);
     208         [ #  # ]:          0 :                 if (rxq_interrupt_enable)
     209                 :          0 :                         rxq_interrupt_offset++;
     210                 :            :         }
     211                 :            : 
     212         [ #  # ]:          0 :         for (index = 0; index < enic->wq_count; index++) {
     213                 :          0 :                 vnic_wq_init(&enic->wq[index],
     214                 :            :                         enic_cq_wq(enic, index),
     215                 :            :                         error_interrupt_enable,
     216                 :            :                         error_interrupt_offset);
     217                 :            :                 /* Compute unsupported ol flags for enic_prep_pkts() */
     218                 :          0 :                 enic->wq[index].tx_offload_notsup_mask =
     219                 :          0 :                         RTE_MBUF_F_TX_OFFLOAD_MASK ^ enic->tx_offload_mask;
     220                 :            : 
     221                 :            :                 cq_idx = enic_cq_wq(enic, index);
     222                 :          0 :                 vnic_cq_init(&enic->cq[cq_idx],
     223                 :            :                         0 /* flow_control_enable */,
     224                 :            :                         1 /* color_enable */,
     225                 :            :                         0 /* cq_head */,
     226                 :            :                         0 /* cq_tail */,
     227                 :            :                         1 /* cq_tail_color */,
     228                 :            :                         0 /* interrupt_enable */,
     229                 :            :                         0 /* cq_entry_enable */,
     230                 :            :                         1 /* cq_message_enable */,
     231                 :            :                         0 /* interrupt offset */,
     232                 :          0 :                         (uint64_t)enic->wq[index].cqmsg_rz->iova);
     233                 :            :         }
     234                 :            : 
     235         [ #  # ]:          0 :         for (index = 0; index < enic->intr_count; index++) {
     236                 :          0 :                 vnic_intr_init(&enic->intr[index],
     237                 :            :                                enic->config.intr_timer_usec,
     238                 :          0 :                                enic->config.intr_timer_type,
     239                 :            :                                /*mask_on_assertion*/1);
     240                 :            :         }
     241                 :          0 : }
     242                 :            : 
     243                 :            : 
     244                 :            : int
     245                 :          0 : enic_alloc_rx_queue_mbufs(struct enic *enic, struct vnic_rq *rq)
     246                 :            : {
     247                 :            :         struct rte_mbuf *mb;
     248                 :          0 :         struct rq_enet_desc *rqd = rq->ring.descs;
     249                 :            :         unsigned i;
     250                 :            :         dma_addr_t dma_addr;
     251                 :            :         uint32_t max_rx_pktlen;
     252                 :            :         uint16_t rq_buf_len;
     253                 :            : 
     254         [ #  # ]:          0 :         if (!rq->in_use)
     255                 :            :                 return 0;
     256                 :            : 
     257                 :          0 :         dev_debug(enic, "queue %u, allocating %u rx queue mbufs\n", rq->index,
     258                 :            :                   rq->ring.desc_count);
     259                 :            : 
     260                 :            :         /*
     261                 :            :          * If *not* using scatter and the mbuf size is greater than the
     262                 :            :          * requested max packet size (mtu + eth overhead), then reduce the
     263                 :            :          * posted buffer size to max packet size. HW still receives packets
     264                 :            :          * larger than max packet size, but they will be truncated, which we
     265                 :            :          * drop in the rx handler. Not ideal, but better than returning
     266                 :            :          * large packets when the user is not expecting them.
     267                 :            :          */
     268         [ #  # ]:          0 :         max_rx_pktlen = enic_mtu_to_max_rx_pktlen(enic->rte_dev->data->mtu);
     269         [ #  # ]:          0 :         rq_buf_len = rte_pktmbuf_data_room_size(rq->mp) - RTE_PKTMBUF_HEADROOM;
     270   [ #  #  #  # ]:          0 :         if (max_rx_pktlen < rq_buf_len && !rq->data_queue_enable)
     271                 :          0 :                 rq_buf_len = max_rx_pktlen;
     272         [ #  # ]:          0 :         for (i = 0; i < rq->ring.desc_count; i++, rqd++) {
     273                 :          0 :                 mb = rte_mbuf_raw_alloc(rq->mp);
     274         [ #  # ]:          0 :                 if (mb == NULL) {
     275                 :          0 :                         dev_err(enic, "RX mbuf alloc failed queue_id=%u\n",
     276                 :            :                         (unsigned)rq->index);
     277                 :          0 :                         return -ENOMEM;
     278                 :            :                 }
     279                 :            : 
     280                 :          0 :                 mb->data_off = RTE_PKTMBUF_HEADROOM;
     281                 :          0 :                 dma_addr = (dma_addr_t)(mb->buf_iova
     282                 :            :                            + RTE_PKTMBUF_HEADROOM);
     283                 :          0 :                 rq_enet_desc_enc(rqd, dma_addr,
     284                 :          0 :                                 (rq->is_sop ? RQ_ENET_TYPE_ONLY_SOP
     285                 :            :                                 : RQ_ENET_TYPE_NOT_SOP),
     286                 :            :                                 rq_buf_len);
     287                 :          0 :                 rq->mbuf_ring[i] = mb;
     288                 :            :         }
     289                 :            :         /*
     290                 :            :          * Do not post the buffers to the NIC until we enable the RQ via
     291                 :            :          * enic_start_rq().
     292                 :            :          */
     293                 :          0 :         rq->need_initial_post = true;
     294                 :            :         /* Initialize fetch index while RQ is disabled */
     295                 :          0 :         iowrite32(0, &rq->ctrl->fetch_index);
     296                 :          0 :         return 0;
     297                 :            : }
     298                 :            : 
     299                 :            : /*
     300                 :            :  * Post the Rx buffers for the first time. enic_alloc_rx_queue_mbufs() has
     301                 :            :  * allocated the buffers and filled the RQ descriptor ring. Just need to push
     302                 :            :  * the post index to the NIC.
     303                 :            :  */
     304                 :            : static void
     305                 :          0 : enic_initial_post_rx(struct enic *enic, struct vnic_rq *rq)
     306                 :            : {
     307   [ #  #  #  # ]:          0 :         if (!rq->in_use || !rq->need_initial_post)
     308                 :            :                 return;
     309                 :            : 
     310                 :            :         /* make sure all prior writes are complete before doing the PIO write */
     311                 :            :         rte_rmb();
     312                 :            : 
     313                 :            :         /* Post all but the last buffer to VIC. */
     314                 :          0 :         rq->posted_index = rq->ring.desc_count - 1;
     315                 :            : 
     316                 :          0 :         rq->rx_nb_hold = 0;
     317                 :            : 
     318                 :          0 :         dev_debug(enic, "port=%u, qidx=%u, Write %u posted idx, %u sw held\n",
     319                 :            :                 enic->port_id, rq->index, rq->posted_index, rq->rx_nb_hold);
     320                 :          0 :         iowrite32(rq->posted_index, &rq->ctrl->posted_index);
     321                 :            :         rte_rmb();
     322                 :          0 :         rq->need_initial_post = false;
     323                 :            : }
     324                 :            : 
     325                 :            : void *
     326                 :          0 : enic_alloc_consistent(void *priv, size_t size,
     327                 :            :         dma_addr_t *dma_handle, uint8_t *name)
     328                 :            : {
     329                 :            :         void *vaddr;
     330                 :            :         const struct rte_memzone *rz;
     331                 :          0 :         *dma_handle = 0;
     332                 :            :         struct enic *enic = (struct enic *)priv;
     333                 :            :         struct enic_memzone_entry *mze;
     334                 :            : 
     335                 :          0 :         rz = rte_memzone_reserve_aligned((const char *)name, size,
     336                 :            :                         SOCKET_ID_ANY, RTE_MEMZONE_IOVA_CONTIG, ENIC_PAGE_SIZE);
     337         [ #  # ]:          0 :         if (!rz) {
     338                 :          0 :                 pr_err("%s : Failed to allocate memory requested for %s\n",
     339                 :            :                         __func__, name);
     340                 :          0 :                 return NULL;
     341                 :            :         }
     342                 :            : 
     343                 :          0 :         vaddr = rz->addr;
     344                 :          0 :         *dma_handle = (dma_addr_t)rz->iova;
     345                 :            : 
     346                 :          0 :         mze = rte_malloc("enic memzone entry",
     347                 :            :                          sizeof(struct enic_memzone_entry), 0);
     348                 :            : 
     349         [ #  # ]:          0 :         if (!mze) {
     350                 :          0 :                 pr_err("%s : Failed to allocate memory for memzone list\n",
     351                 :            :                        __func__);
     352                 :          0 :                 rte_memzone_free(rz);
     353                 :          0 :                 return NULL;
     354                 :            :         }
     355                 :            : 
     356                 :          0 :         mze->rz = rz;
     357                 :            : 
     358                 :          0 :         rte_spinlock_lock(&enic->memzone_list_lock);
     359         [ #  # ]:          0 :         LIST_INSERT_HEAD(&enic->memzone_list, mze, entries);
     360                 :            :         rte_spinlock_unlock(&enic->memzone_list_lock);
     361                 :            : 
     362                 :          0 :         return vaddr;
     363                 :            : }
     364                 :            : 
     365                 :            : void
     366                 :          0 : enic_free_consistent(void *priv,
     367                 :            :                      __rte_unused size_t size,
     368                 :            :                      void *vaddr,
     369                 :            :                      dma_addr_t dma_handle)
     370                 :            : {
     371                 :            :         struct enic_memzone_entry *mze;
     372                 :            :         struct enic *enic = (struct enic *)priv;
     373                 :            : 
     374                 :          0 :         rte_spinlock_lock(&enic->memzone_list_lock);
     375         [ #  # ]:          0 :         LIST_FOREACH(mze, &enic->memzone_list, entries) {
     376         [ #  # ]:          0 :                 if (mze->rz->addr == vaddr &&
     377         [ #  # ]:          0 :                     mze->rz->iova == dma_handle)
     378                 :            :                         break;
     379                 :            :         }
     380         [ #  # ]:          0 :         if (mze == NULL) {
     381                 :            :                 rte_spinlock_unlock(&enic->memzone_list_lock);
     382                 :          0 :                 dev_warning(enic,
     383                 :            :                             "Tried to free memory, but couldn't find it in the memzone list\n");
     384                 :          0 :                 return;
     385                 :            :         }
     386         [ #  # ]:          0 :         LIST_REMOVE(mze, entries);
     387                 :            :         rte_spinlock_unlock(&enic->memzone_list_lock);
     388                 :          0 :         rte_memzone_free(mze->rz);
     389                 :          0 :         rte_free(mze);
     390                 :            : }
     391                 :            : 
     392                 :          0 : int enic_link_update(struct rte_eth_dev *eth_dev)
     393                 :            : {
     394                 :            :         struct enic *enic = pmd_priv(eth_dev);
     395                 :            :         struct rte_eth_link link;
     396                 :            : 
     397                 :            :         memset(&link, 0, sizeof(link));
     398                 :          0 :         link.link_status = enic_get_link_status(enic);
     399                 :          0 :         link.link_duplex = RTE_ETH_LINK_FULL_DUPLEX;
     400         [ #  # ]:          0 :         link.link_speed = vnic_dev_port_speed(enic->vdev);
     401                 :            : 
     402                 :          0 :         return rte_eth_linkstatus_set(eth_dev, &link);
     403                 :            : }
     404                 :            : 
     405                 :            : static void
     406                 :          0 : enic_intr_handler(void *arg)
     407                 :            : {
     408                 :            :         struct rte_eth_dev *dev = (struct rte_eth_dev *)arg;
     409                 :            :         struct enic *enic = pmd_priv(dev);
     410                 :            : 
     411                 :          0 :         ENICPMD_FUNC_TRACE();
     412                 :            : 
     413                 :          0 :         vnic_intr_return_all_credits(&enic->intr[ENICPMD_LSC_INTR_OFFSET]);
     414                 :            : 
     415         [ #  # ]:          0 :         if (enic_is_vf(enic)) {
     416                 :            :                 /*
     417                 :            :                  * When using the admin channel, VF receives link
     418                 :            :                  * status changes from PF. enic_poll_vf_admin_chan()
     419                 :            :                  * calls RTE_ETH_EVENT_INTR_LSC.
     420                 :            :                  */
     421                 :          0 :                 enic_poll_vf_admin_chan(enic);
     422                 :          0 :                 return;
     423                 :            :         }
     424                 :            : 
     425                 :          0 :         enic_link_update(dev);
     426                 :          0 :         rte_eth_dev_callback_process(dev, RTE_ETH_EVENT_INTR_LSC, NULL);
     427                 :          0 :         enic_log_q_error(enic);
     428                 :            :         /* Re-enable irq in case of INTx */
     429                 :          0 :         rte_intr_ack(enic->pdev->intr_handle);
     430                 :            : }
     431                 :            : 
     432                 :          0 : static int enic_rxq_intr_init(struct enic *enic)
     433                 :            : {
     434                 :            :         struct rte_intr_handle *intr_handle;
     435                 :            :         uint32_t rxq_intr_count, i;
     436                 :            :         int err;
     437                 :            : 
     438                 :          0 :         intr_handle = enic->rte_dev->intr_handle;
     439         [ #  # ]:          0 :         if (!enic->rte_dev->data->dev_conf.intr_conf.rxq)
     440                 :            :                 return 0;
     441                 :            :         /*
     442                 :            :          * Rx queue interrupts only work when we have MSI-X interrupts,
     443                 :            :          * one per queue. Sharing one interrupt is technically
     444                 :            :          * possible with VIC, but it is not worth the complications it brings.
     445                 :            :          */
     446         [ #  # ]:          0 :         if (!rte_intr_cap_multiple(intr_handle)) {
     447                 :          0 :                 dev_err(enic, "Rx queue interrupts require MSI-X interrupts"
     448                 :            :                         " (vfio-pci driver)\n");
     449                 :          0 :                 return -ENOTSUP;
     450                 :            :         }
     451                 :          0 :         rxq_intr_count = enic->intr_count - ENICPMD_RXQ_INTR_OFFSET;
     452                 :          0 :         err = rte_intr_efd_enable(intr_handle, rxq_intr_count);
     453         [ #  # ]:          0 :         if (err) {
     454                 :          0 :                 dev_err(enic, "Failed to enable event fds for Rx queue"
     455                 :            :                         " interrupts\n");
     456                 :          0 :                 return err;
     457                 :            :         }
     458                 :            : 
     459         [ #  # ]:          0 :         if (rte_intr_vec_list_alloc(intr_handle, "enic_intr_vec",
     460                 :            :                                            rxq_intr_count)) {
     461                 :          0 :                 dev_err(enic, "Failed to allocate intr_vec\n");
     462                 :          0 :                 return -ENOMEM;
     463                 :            :         }
     464         [ #  # ]:          0 :         for (i = 0; i < rxq_intr_count; i++)
     465         [ #  # ]:          0 :                 if (rte_intr_vec_list_index_set(intr_handle, i,
     466                 :          0 :                                                    i + ENICPMD_RXQ_INTR_OFFSET))
     467                 :          0 :                         return -rte_errno;
     468                 :            :         return 0;
     469                 :            : }
     470                 :            : 
     471                 :            : static void enic_rxq_intr_deinit(struct enic *enic)
     472                 :            : {
     473                 :            :         struct rte_intr_handle *intr_handle;
     474                 :            : 
     475                 :          0 :         intr_handle = enic->rte_dev->intr_handle;
     476                 :          0 :         rte_intr_efd_disable(intr_handle);
     477                 :            : 
     478                 :          0 :         rte_intr_vec_list_free(intr_handle);
     479                 :            : }
     480                 :            : 
     481                 :            : static void enic_prep_wq_for_simple_tx(struct enic *enic, uint16_t queue_idx)
     482                 :            : {
     483                 :            :         struct wq_enet_desc *desc;
     484                 :            :         struct vnic_wq *wq;
     485                 :            :         unsigned int i;
     486                 :            : 
     487                 :            :         /*
     488                 :            :          * Fill WQ descriptor fields that never change. Every descriptor is
     489                 :            :          * one packet, so set EOP. Also set CQ_ENTRY every ENIC_WQ_CQ_THRESH
     490                 :            :          * descriptors (i.e. request one completion update every 32 packets).
     491                 :            :          */
     492                 :          0 :         wq = &enic->wq[queue_idx];
     493                 :          0 :         desc = (struct wq_enet_desc *)wq->ring.descs;
     494         [ #  # ]:          0 :         for (i = 0; i < wq->ring.desc_count; i++, desc++) {
     495                 :          0 :                 desc->header_length_flags = 1 << WQ_ENET_FLAGS_EOP_SHIFT;
     496         [ #  # ]:          0 :                 if (i % ENIC_WQ_CQ_THRESH == ENIC_WQ_CQ_THRESH - 1)
     497                 :          0 :                         desc->header_length_flags |=
     498                 :            :                                 (1 << WQ_ENET_FLAGS_CQ_ENTRY_SHIFT);
     499                 :            :         }
     500                 :            : }
     501                 :            : 
     502                 :            : #ifndef ENIC_RXTX_VEC
     503                 :            : bool
     504                 :            : enic_use_vector_rx_handler(__rte_unused struct rte_eth_dev *eth_dev)
     505                 :            : {
     506                 :            :         return false;
     507                 :            : }
     508                 :            : #endif /* ENIC_RXTX_VEC */
     509                 :            : 
     510         [ #  # ]:          0 : void enic_pick_rx_handler(struct rte_eth_dev *eth_dev)
     511                 :            : {
     512                 :            :         struct enic *enic = pmd_priv(eth_dev);
     513                 :            : 
     514         [ #  # ]:          0 :         if (enic->cq64) {
     515                 :          0 :                 ENICPMD_LOG(DEBUG, " use the normal Rx handler for 64B CQ entry");
     516                 :          0 :                 eth_dev->rx_pkt_burst = &enic_recv_pkts_64;
     517                 :          0 :                 return;
     518                 :            :         }
     519                 :            :         /*
     520                 :            :          * Preference order:
     521                 :            :          * 1. The vectorized handler if possible and requested.
     522                 :            :          * 2. The non-scatter, simplified handler if scatter Rx is not used.
     523                 :            :          * 3. The default handler as a fallback.
     524                 :            :          */
     525         [ #  # ]:          0 :         if (enic_use_vector_rx_handler(eth_dev))
     526                 :            :                 return;
     527   [ #  #  #  # ]:          0 :         if (enic->rq_count > 0 && enic->rq[0].data_queue_enable == 0) {
     528                 :          0 :                 ENICPMD_LOG(DEBUG, " use the non-scatter Rx handler");
     529                 :          0 :                 eth_dev->rx_pkt_burst = &enic_noscatter_recv_pkts;
     530                 :            :         } else {
     531                 :          0 :                 ENICPMD_LOG(DEBUG, " use the normal Rx handler");
     532                 :          0 :                 eth_dev->rx_pkt_burst = &enic_recv_pkts;
     533                 :            :         }
     534                 :            : }
     535                 :            : 
     536                 :            : /* Secondary process uses this to set the Tx handler */
     537         [ #  # ]:          0 : void enic_pick_tx_handler(struct rte_eth_dev *eth_dev)
     538                 :            : {
     539                 :            :         struct enic *enic = pmd_priv(eth_dev);
     540                 :            : 
     541         [ #  # ]:          0 :         if (enic->use_simple_tx_handler) {
     542                 :          0 :                 ENICPMD_LOG(DEBUG, " use the simple tx handler");
     543                 :          0 :                 eth_dev->tx_pkt_burst = &enic_simple_xmit_pkts;
     544                 :            :         } else {
     545                 :          0 :                 ENICPMD_LOG(DEBUG, " use the default tx handler");
     546                 :          0 :                 eth_dev->tx_pkt_burst = &enic_xmit_pkts;
     547                 :            :         }
     548                 :          0 : }
     549                 :            : 
     550                 :          0 : int enic_enable(struct enic *enic)
     551                 :            : {
     552                 :            :         unsigned int index;
     553                 :            :         int err;
     554                 :          0 :         struct rte_eth_dev *eth_dev = enic->rte_dev;
     555                 :            :         uint64_t simple_tx_offloads;
     556                 :            :         uintptr_t p;
     557                 :            : 
     558         [ #  # ]:          0 :         if (enic->enable_avx2_rx) {
     559                 :          0 :                 struct rte_mbuf mb_def = { .buf_addr = 0 };
     560                 :            : 
     561                 :            :                 /*
     562                 :            :                  * mbuf_initializer contains const-after-init fields of
     563                 :            :                  * receive mbufs (i.e. 64 bits of fields from rearm_data).
     564                 :            :                  * It is currently used by the vectorized handler.
     565                 :            :                  */
     566                 :          0 :                 mb_def.nb_segs = 1;
     567                 :          0 :                 mb_def.data_off = RTE_PKTMBUF_HEADROOM;
     568                 :          0 :                 mb_def.port = enic->port_id;
     569                 :            :                 rte_mbuf_refcnt_set(&mb_def, 1);
     570                 :          0 :                 rte_compiler_barrier();
     571                 :            :                 p = (uintptr_t)&mb_def.rearm_data;
     572                 :          0 :                 enic->mbuf_initializer = *(uint64_t *)p;
     573                 :            :         }
     574                 :            : 
     575                 :          0 :         eth_dev->data->dev_link.link_speed = vnic_dev_port_speed(enic->vdev);
     576                 :          0 :         eth_dev->data->dev_link.link_duplex = RTE_ETH_LINK_FULL_DUPLEX;
     577                 :            : 
     578                 :            :         /* vnic notification of link status has already been turned on in
     579                 :            :          * enic_dev_init() which is called during probe time.  Here we are
     580                 :            :          * just turning on interrupt vector 0 if needed.
     581                 :            :          */
     582         [ #  # ]:          0 :         if (eth_dev->data->dev_conf.intr_conf.lsc)
     583                 :          0 :                 vnic_dev_notify_set(enic->vdev, 0);
     584                 :            : 
     585                 :          0 :         err = enic_rxq_intr_init(enic);
     586         [ #  # ]:          0 :         if (err)
     587                 :            :                 return err;
     588                 :            : 
     589                 :            :         /* Initialize flowman if not already initialized during probe */
     590   [ #  #  #  # ]:          0 :         if (enic->fm == NULL && enic_fm_init(enic))
     591                 :          0 :                 dev_warning(enic, "Init of flowman failed.\n");
     592                 :            : 
     593         [ #  # ]:          0 :         for (index = 0; index < enic->rq_count; index++) {
     594                 :          0 :                 err = enic_alloc_rx_queue_mbufs(enic,
     595                 :          0 :                         &enic->rq[enic_rte_rq_idx_to_sop_idx(index)]);
     596         [ #  # ]:          0 :                 if (err) {
     597                 :          0 :                         dev_err(enic, "Failed to alloc sop RX queue mbufs\n");
     598                 :          0 :                         return err;
     599                 :            :                 }
     600                 :          0 :                 err = enic_alloc_rx_queue_mbufs(enic,
     601                 :          0 :                         &enic->rq[enic_rte_rq_idx_to_data_idx(index, enic)]);
     602         [ #  # ]:          0 :                 if (err) {
     603                 :            :                         /* release the allocated mbufs for the sop rq*/
     604                 :          0 :                         enic_rxmbuf_queue_release(enic,
     605                 :          0 :                                 &enic->rq[enic_rte_rq_idx_to_sop_idx(index)]);
     606                 :            : 
     607                 :          0 :                         dev_err(enic, "Failed to alloc data RX queue mbufs\n");
     608                 :          0 :                         return err;
     609                 :            :                 }
     610                 :            :         }
     611                 :            : 
     612                 :            :         /*
     613                 :            :          * Use the simple TX handler if possible. Only checksum offloads
     614                 :            :          * and vlan insertion are supported.
     615                 :            :          */
     616                 :          0 :         simple_tx_offloads = enic->tx_offload_capa &
     617                 :            :                 (RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM |
     618                 :            :                  RTE_ETH_TX_OFFLOAD_VLAN_INSERT |
     619                 :            :                  RTE_ETH_TX_OFFLOAD_IPV4_CKSUM |
     620                 :            :                  RTE_ETH_TX_OFFLOAD_UDP_CKSUM |
     621                 :            :                  RTE_ETH_TX_OFFLOAD_TCP_CKSUM);
     622                 :          0 :         if ((eth_dev->data->dev_conf.txmode.offloads &
     623         [ #  # ]:          0 :              ~simple_tx_offloads) == 0) {
     624                 :          0 :                 ENICPMD_LOG(DEBUG, " use the simple tx handler");
     625                 :          0 :                 eth_dev->tx_pkt_burst = &enic_simple_xmit_pkts;
     626         [ #  # ]:          0 :                 for (index = 0; index < enic->wq_count; index++)
     627                 :          0 :                         enic_prep_wq_for_simple_tx(enic, index);
     628                 :          0 :                 enic->use_simple_tx_handler = 1;
     629                 :            :         } else {
     630                 :          0 :                 ENICPMD_LOG(DEBUG, " use the default tx handler");
     631                 :          0 :                 eth_dev->tx_pkt_burst = &enic_xmit_pkts;
     632                 :            :         }
     633                 :            : 
     634                 :          0 :         enic_pick_rx_handler(eth_dev);
     635                 :            : 
     636         [ #  # ]:          0 :         for (index = 0; index < enic->wq_count; index++)
     637                 :          0 :                 enic_start_wq(enic, index);
     638         [ #  # ]:          0 :         for (index = 0; index < enic->rq_count; index++)
     639                 :          0 :                 enic_start_rq(enic, index);
     640                 :            : 
     641                 :          0 :         enic_dev_add_addr(enic, enic->mac_addr);
     642                 :            : 
     643                 :          0 :         vnic_dev_enable_wait(enic->vdev);
     644                 :            : 
     645                 :            :         /* Register and enable error interrupt */
     646                 :          0 :         rte_intr_callback_register(enic->pdev->intr_handle,
     647                 :          0 :                 enic_intr_handler, (void *)enic->rte_dev);
     648                 :          0 :         rte_intr_enable(enic->pdev->intr_handle);
     649                 :            :         /* Unmask LSC interrupt */
     650                 :          0 :         vnic_intr_unmask(&enic->intr[ENICPMD_LSC_INTR_OFFSET]);
     651                 :            : 
     652                 :          0 :         return 0;
     653                 :            : }
     654                 :            : 
     655                 :          0 : int enic_alloc_intr_resources(struct enic *enic)
     656                 :            : {
     657                 :            :         int err;
     658                 :            :         unsigned int i;
     659                 :            : 
     660                 :          0 :         dev_info(enic, "vNIC resources used:  "\
     661                 :            :                 "wq %d rq %d cq %d intr %d\n",
     662                 :            :                 enic->wq_count, enic_vnic_rq_count(enic),
     663                 :            :                 enic->cq_count, enic->intr_count);
     664                 :            : 
     665         [ #  # ]:          0 :         if (enic_is_vf(enic)) {
     666                 :          0 :                 dev_info(enic, "vNIC admin channel resources used: wq %d rq %d cq %d\n",
     667                 :            :                          enic->conf_admin_wq_count, enic->conf_admin_rq_count,
     668                 :            :                          enic->conf_admin_cq_count);
     669                 :            :         }
     670                 :            : 
     671         [ #  # ]:          0 :         for (i = 0; i < enic->intr_count; i++) {
     672                 :          0 :                 err = vnic_intr_alloc(enic->vdev, &enic->intr[i], i);
     673         [ #  # ]:          0 :                 if (err) {
     674                 :          0 :                         enic_free_vnic_resources(enic);
     675                 :          0 :                         return err;
     676                 :            :                 }
     677                 :            :         }
     678                 :            : 
     679                 :            :         return 0;
     680                 :            : }
     681                 :            : 
     682                 :          0 : void enic_free_rq(void *rxq)
     683                 :            : {
     684                 :            :         struct vnic_rq *rq_sop, *rq_data;
     685                 :            :         struct enic *enic;
     686                 :            : 
     687         [ #  # ]:          0 :         if (rxq == NULL)
     688                 :            :                 return;
     689                 :            : 
     690                 :            :         rq_sop = (struct vnic_rq *)rxq;
     691                 :          0 :         enic = vnic_dev_priv(rq_sop->vdev);
     692                 :          0 :         rq_data = &enic->rq[rq_sop->data_queue_idx];
     693                 :            : 
     694         [ #  # ]:          0 :         if (rq_sop->free_mbufs) {
     695                 :            :                 struct rte_mbuf **mb;
     696                 :            :                 int i;
     697                 :            : 
     698                 :            :                 mb = rq_sop->free_mbufs;
     699                 :          0 :                 for (i = ENIC_RX_BURST_MAX - rq_sop->num_free_mbufs;
     700         [ #  # ]:          0 :                      i < ENIC_RX_BURST_MAX; i++)
     701                 :          0 :                         rte_pktmbuf_free(mb[i]);
     702                 :          0 :                 rte_free(rq_sop->free_mbufs);
     703                 :          0 :                 rq_sop->free_mbufs = NULL;
     704                 :          0 :                 rq_sop->num_free_mbufs = 0;
     705                 :            :         }
     706                 :            : 
     707                 :          0 :         enic_rxmbuf_queue_release(enic, rq_sop);
     708         [ #  # ]:          0 :         if (rq_data->in_use)
     709                 :          0 :                 enic_rxmbuf_queue_release(enic, rq_data);
     710                 :            : 
     711                 :          0 :         rte_free(rq_sop->mbuf_ring);
     712         [ #  # ]:          0 :         if (rq_data->in_use)
     713                 :          0 :                 rte_free(rq_data->mbuf_ring);
     714                 :            : 
     715                 :          0 :         rq_sop->mbuf_ring = NULL;
     716                 :          0 :         rq_data->mbuf_ring = NULL;
     717                 :            : 
     718                 :          0 :         vnic_rq_free(rq_sop);
     719         [ #  # ]:          0 :         if (rq_data->in_use)
     720                 :          0 :                 vnic_rq_free(rq_data);
     721                 :            : 
     722                 :          0 :         vnic_cq_free(&enic->cq[enic_sop_rq_idx_to_cq_idx(rq_sop->index)]);
     723                 :            : 
     724                 :          0 :         rq_sop->in_use = 0;
     725                 :          0 :         rq_data->in_use = 0;
     726                 :            : }
     727                 :            : 
     728                 :          0 : void enic_start_wq(struct enic *enic, uint16_t queue_idx)
     729                 :            : {
     730                 :          0 :         struct rte_eth_dev_data *data = enic->dev_data;
     731                 :          0 :         vnic_wq_enable(&enic->wq[queue_idx]);
     732                 :          0 :         data->tx_queue_state[queue_idx] = RTE_ETH_QUEUE_STATE_STARTED;
     733                 :          0 : }
     734                 :            : 
     735                 :          0 : int enic_stop_wq(struct enic *enic, uint16_t queue_idx)
     736                 :            : {
     737                 :          0 :         struct rte_eth_dev_data *data = enic->dev_data;
     738                 :            :         int ret;
     739                 :            : 
     740                 :          0 :         ret = vnic_wq_disable(&enic->wq[queue_idx]);
     741         [ #  # ]:          0 :         if (ret)
     742                 :            :                 return ret;
     743                 :            : 
     744                 :          0 :         data->tx_queue_state[queue_idx] = RTE_ETH_QUEUE_STATE_STOPPED;
     745                 :          0 :         return 0;
     746                 :            : }
     747                 :            : 
     748                 :          0 : void enic_start_rq(struct enic *enic, uint16_t queue_idx)
     749                 :            : {
     750                 :          0 :         struct rte_eth_dev_data *data = enic->dev_data;
     751                 :            :         struct vnic_rq *rq_sop;
     752                 :            :         struct vnic_rq *rq_data;
     753                 :          0 :         rq_sop = &enic->rq[enic_rte_rq_idx_to_sop_idx(queue_idx)];
     754                 :          0 :         rq_data = &enic->rq[rq_sop->data_queue_idx];
     755                 :            : 
     756         [ #  # ]:          0 :         if (rq_data->in_use) {
     757                 :          0 :                 vnic_rq_enable(rq_data);
     758                 :          0 :                 enic_initial_post_rx(enic, rq_data);
     759                 :            :         }
     760                 :            :         rte_mb();
     761                 :          0 :         vnic_rq_enable(rq_sop);
     762                 :          0 :         enic_initial_post_rx(enic, rq_sop);
     763                 :          0 :         data->rx_queue_state[queue_idx] = RTE_ETH_QUEUE_STATE_STARTED;
     764                 :          0 : }
     765                 :            : 
     766                 :          0 : int enic_stop_rq(struct enic *enic, uint16_t queue_idx)
     767                 :            : {
     768                 :          0 :         struct rte_eth_dev_data *data = enic->dev_data;
     769                 :            :         int ret1 = 0, ret2 = 0;
     770                 :            :         struct vnic_rq *rq_sop;
     771                 :            :         struct vnic_rq *rq_data;
     772                 :          0 :         rq_sop = &enic->rq[enic_rte_rq_idx_to_sop_idx(queue_idx)];
     773                 :          0 :         rq_data = &enic->rq[rq_sop->data_queue_idx];
     774                 :            : 
     775                 :          0 :         ret2 = vnic_rq_disable(rq_sop);
     776                 :            :         rte_mb();
     777         [ #  # ]:          0 :         if (rq_data->in_use)
     778                 :          0 :                 ret1 = vnic_rq_disable(rq_data);
     779                 :            : 
     780         [ #  # ]:          0 :         if (ret2)
     781                 :            :                 return ret2;
     782         [ #  # ]:          0 :         else if (ret1)
     783                 :            :                 return ret1;
     784                 :            : 
     785                 :          0 :         data->rx_queue_state[queue_idx] = RTE_ETH_QUEUE_STATE_STOPPED;
     786                 :          0 :         return 0;
     787                 :            : }
     788                 :            : 
     789                 :          0 : int enic_alloc_rq(struct enic *enic, uint16_t queue_idx,
     790                 :            :         unsigned int socket_id, struct rte_mempool *mp,
     791                 :            :         uint16_t nb_desc, uint16_t free_thresh)
     792                 :            : {
     793                 :            :         struct enic_vf_representor *vf;
     794                 :            :         int rc;
     795                 :            :         uint16_t sop_queue_idx;
     796                 :            :         uint16_t data_queue_idx;
     797                 :            :         uint16_t cq_idx;
     798                 :            :         struct vnic_rq *rq_sop;
     799                 :            :         struct vnic_rq *rq_data;
     800                 :            :         unsigned int mbuf_size, mbufs_per_pkt;
     801                 :            :         unsigned int nb_sop_desc, nb_data_desc;
     802                 :            :         uint16_t min_sop, max_sop, min_data, max_data;
     803                 :            :         uint32_t max_rx_pktlen;
     804                 :            : 
     805                 :            :         /*
     806                 :            :          * Representor uses a reserved PF queue. Translate representor
     807                 :            :          * queue number to PF queue number.
     808                 :            :          */
     809         [ #  # ]:          0 :         if (rte_eth_dev_is_repr(enic->rte_dev)) {
     810                 :            :                 RTE_ASSERT(queue_idx == 0);
     811                 :            :                 vf = VF_ENIC_TO_VF_REP(enic);
     812                 :          0 :                 sop_queue_idx = vf->pf_rq_sop_idx;
     813                 :          0 :                 data_queue_idx = vf->pf_rq_data_idx;
     814                 :          0 :                 enic = vf->pf;
     815                 :            :                 queue_idx = sop_queue_idx;
     816                 :            :         } else {
     817                 :          0 :                 sop_queue_idx = enic_rte_rq_idx_to_sop_idx(queue_idx);
     818                 :          0 :                 data_queue_idx = enic_rte_rq_idx_to_data_idx(queue_idx, enic);
     819                 :            :         }
     820                 :          0 :         cq_idx = enic_cq_rq(enic, sop_queue_idx);
     821                 :          0 :         rq_sop = &enic->rq[sop_queue_idx];
     822                 :          0 :         rq_data = &enic->rq[data_queue_idx];
     823                 :          0 :         rq_sop->is_sop = 1;
     824                 :          0 :         rq_sop->data_queue_idx = data_queue_idx;
     825                 :          0 :         rq_data->is_sop = 0;
     826                 :          0 :         rq_data->data_queue_idx = 0;
     827                 :          0 :         rq_sop->socket_id = socket_id;
     828                 :          0 :         rq_sop->mp = mp;
     829                 :          0 :         rq_data->socket_id = socket_id;
     830                 :          0 :         rq_data->mp = mp;
     831                 :          0 :         rq_sop->in_use = 1;
     832                 :          0 :         rq_sop->rx_free_thresh = free_thresh;
     833                 :          0 :         rq_data->rx_free_thresh = free_thresh;
     834                 :          0 :         dev_debug(enic, "Set queue_id:%u free thresh:%u\n", queue_idx,
     835                 :            :                   free_thresh);
     836                 :            : 
     837                 :          0 :         mbuf_size = (uint16_t)(rte_pktmbuf_data_room_size(mp) -
     838                 :            :                                RTE_PKTMBUF_HEADROOM);
     839                 :            :         /* max_rx_pktlen includes the ethernet header and CRC. */
     840         [ #  # ]:          0 :         max_rx_pktlen = enic_mtu_to_max_rx_pktlen(enic->rte_dev->data->mtu);
     841                 :            : 
     842         [ #  # ]:          0 :         if (enic->rte_dev->data->dev_conf.rxmode.offloads &
     843                 :            :             RTE_ETH_RX_OFFLOAD_SCATTER) {
     844                 :          0 :                 dev_info(enic, "Rq %u Scatter rx mode enabled\n", queue_idx);
     845                 :            :                 /* ceil((max pkt len)/mbuf_size) */
     846                 :          0 :                 mbufs_per_pkt = (max_rx_pktlen + mbuf_size - 1) / mbuf_size;
     847                 :            :         } else {
     848                 :          0 :                 dev_info(enic, "Scatter rx mode disabled\n");
     849                 :            :                 mbufs_per_pkt = 1;
     850         [ #  # ]:          0 :                 if (max_rx_pktlen > mbuf_size) {
     851                 :          0 :                         dev_warning(enic, "The maximum Rx packet size (%u) is"
     852                 :            :                                     " larger than the mbuf size (%u), and"
     853                 :            :                                     " scatter is disabled. Larger packets will"
     854                 :            :                                     " be truncated.\n",
     855                 :            :                                     max_rx_pktlen, mbuf_size);
     856                 :            :                 }
     857                 :            :         }
     858                 :            : 
     859         [ #  # ]:          0 :         if (mbufs_per_pkt > 1) {
     860                 :          0 :                 dev_info(enic, "Rq %u Scatter rx mode in use\n", queue_idx);
     861                 :          0 :                 rq_sop->data_queue_enable = 1;
     862                 :          0 :                 rq_data->in_use = 1;
     863                 :            :                 /*
     864                 :            :                  * HW does not directly support MTU. HW always
     865                 :            :                  * receives packet sizes up to the "max" MTU.
     866                 :            :                  * If not using scatter, we can achieve the effect of dropping
     867                 :            :                  * larger packets by reducing the size of posted buffers.
     868                 :            :                  * See enic_alloc_rx_queue_mbufs().
     869                 :            :                  */
     870         [ #  # ]:          0 :                 if (enic->rte_dev->data->mtu < enic->max_mtu) {
     871                 :          0 :                         dev_warning(enic,
     872                 :            :                                 "mtu is ignored when scatter rx mode is in use.\n");
     873                 :            :                 }
     874                 :            :         } else {
     875                 :          0 :                 dev_info(enic, "Rq %u Scatter rx mode not being used\n",
     876                 :            :                          queue_idx);
     877                 :          0 :                 rq_sop->data_queue_enable = 0;
     878                 :          0 :                 rq_data->in_use = 0;
     879                 :            :         }
     880                 :            : 
     881                 :            :         /* number of descriptors have to be a multiple of 32 */
     882                 :          0 :         nb_sop_desc = (nb_desc / mbufs_per_pkt) & ENIC_ALIGN_DESCS_MASK;
     883                 :          0 :         nb_data_desc = (nb_desc - nb_sop_desc) & ENIC_ALIGN_DESCS_MASK;
     884                 :            : 
     885                 :          0 :         rq_sop->max_mbufs_per_pkt = mbufs_per_pkt;
     886                 :          0 :         rq_data->max_mbufs_per_pkt = mbufs_per_pkt;
     887                 :            : 
     888         [ #  # ]:          0 :         if (mbufs_per_pkt > 1) {
     889                 :            :                 min_sop = ENIC_RX_BURST_MAX;
     890                 :          0 :                 max_sop = ((enic->config.rq_desc_count /
     891                 :          0 :                             (mbufs_per_pkt - 1)) & ENIC_ALIGN_DESCS_MASK);
     892                 :          0 :                 min_data = min_sop * (mbufs_per_pkt - 1);
     893                 :          0 :                 max_data = enic->config.rq_desc_count;
     894                 :            :         } else {
     895                 :            :                 min_sop = ENIC_RX_BURST_MAX;
     896                 :          0 :                 max_sop = enic->config.rq_desc_count;
     897                 :            :                 min_data = 0;
     898                 :            :                 max_data = 0;
     899                 :            :         }
     900                 :            : 
     901         [ #  # ]:          0 :         if (nb_desc < (min_sop + min_data)) {
     902                 :          0 :                 dev_warning(enic,
     903                 :            :                             "Number of rx descs too low, adjusting to minimum\n");
     904                 :            :                 nb_sop_desc = min_sop;
     905                 :          0 :                 nb_data_desc = min_data;
     906         [ #  # ]:          0 :         } else if (nb_desc > (max_sop + max_data)) {
     907                 :          0 :                 dev_warning(enic,
     908                 :            :                             "Number of rx_descs too high, adjusting to maximum\n");
     909                 :          0 :                 nb_sop_desc = max_sop;
     910                 :          0 :                 nb_data_desc = max_data;
     911                 :            :         }
     912         [ #  # ]:          0 :         if (mbufs_per_pkt > 1) {
     913                 :          0 :                 dev_info(enic, "For max packet size %u and mbuf size %u valid"
     914                 :            :                          " rx descriptor range is %u to %u\n",
     915                 :            :                          max_rx_pktlen, mbuf_size, min_sop + min_data,
     916                 :            :                          max_sop + max_data);
     917                 :            :         }
     918                 :          0 :         dev_info(enic, "Using %d rx descriptors (sop %d, data %d)\n",
     919                 :            :                  nb_sop_desc + nb_data_desc, nb_sop_desc, nb_data_desc);
     920                 :            : 
     921                 :            :         /* Allocate sop queue resources */
     922                 :          0 :         rc = vnic_rq_alloc(enic->vdev, rq_sop, sop_queue_idx,
     923                 :            :                 nb_sop_desc, sizeof(struct rq_enet_desc));
     924         [ #  # ]:          0 :         if (rc) {
     925                 :          0 :                 dev_err(enic, "error in allocation of sop rq\n");
     926                 :          0 :                 goto err_exit;
     927                 :            :         }
     928                 :          0 :         nb_sop_desc = rq_sop->ring.desc_count;
     929                 :            : 
     930         [ #  # ]:          0 :         if (rq_data->in_use) {
     931                 :            :                 /* Allocate data queue resources */
     932                 :          0 :                 rc = vnic_rq_alloc(enic->vdev, rq_data, data_queue_idx,
     933                 :            :                                    nb_data_desc,
     934                 :            :                                    sizeof(struct rq_enet_desc));
     935         [ #  # ]:          0 :                 if (rc) {
     936                 :          0 :                         dev_err(enic, "error in allocation of data rq\n");
     937                 :          0 :                         goto err_free_rq_sop;
     938                 :            :                 }
     939                 :          0 :                 nb_data_desc = rq_data->ring.desc_count;
     940                 :            :         }
     941                 :            :         /* Enable 64B CQ entry if requested */
     942   [ #  #  #  # ]:          0 :         if (enic->cq64 && vnic_dev_set_cq_entry_size(enic->vdev,
     943                 :            :                                 sop_queue_idx, VNIC_RQ_CQ_ENTRY_SIZE_64)) {
     944                 :          0 :                 dev_err(enic, "failed to enable 64B CQ entry on sop rq\n");
     945                 :          0 :                 goto err_free_rq_data;
     946                 :            :         }
     947   [ #  #  #  #  :          0 :         if (rq_data->in_use && enic->cq64 &&
                   #  # ]
     948                 :          0 :             vnic_dev_set_cq_entry_size(enic->vdev, data_queue_idx,
     949                 :            :                 VNIC_RQ_CQ_ENTRY_SIZE_64)) {
     950                 :          0 :                 dev_err(enic, "failed to enable 64B CQ entry on data rq\n");
     951                 :          0 :                 goto err_free_rq_data;
     952                 :            :         }
     953                 :            : 
     954                 :          0 :         rc = vnic_cq_alloc(enic->vdev, &enic->cq[cq_idx], cq_idx,
     955                 :            :                            socket_id, nb_sop_desc + nb_data_desc,
     956         [ #  # ]:          0 :                            enic->cq64 ?      sizeof(struct cq_enet_rq_desc_64) :
     957                 :            :                            sizeof(struct cq_enet_rq_desc));
     958         [ #  # ]:          0 :         if (rc) {
     959                 :          0 :                 dev_err(enic, "error in allocation of cq for rq\n");
     960                 :          0 :                 goto err_free_rq_data;
     961                 :            :         }
     962                 :            : 
     963                 :            :         /* Allocate the mbuf rings */
     964                 :          0 :         rq_sop->mbuf_ring = (struct rte_mbuf **)
     965                 :          0 :                 rte_zmalloc_socket("rq->mbuf_ring",
     966                 :            :                                    sizeof(struct rte_mbuf *) * nb_sop_desc,
     967                 :          0 :                                    RTE_CACHE_LINE_SIZE, rq_sop->socket_id);
     968         [ #  # ]:          0 :         if (rq_sop->mbuf_ring == NULL)
     969                 :          0 :                 goto err_free_cq;
     970                 :            : 
     971         [ #  # ]:          0 :         if (rq_data->in_use) {
     972                 :          0 :                 rq_data->mbuf_ring = (struct rte_mbuf **)
     973                 :          0 :                         rte_zmalloc_socket("rq->mbuf_ring",
     974                 :            :                                 sizeof(struct rte_mbuf *) * nb_data_desc,
     975                 :          0 :                                 RTE_CACHE_LINE_SIZE, rq_sop->socket_id);
     976         [ #  # ]:          0 :                 if (rq_data->mbuf_ring == NULL)
     977                 :          0 :                         goto err_free_sop_mbuf;
     978                 :            :         }
     979                 :            : 
     980                 :          0 :         rq_sop->free_mbufs = (struct rte_mbuf **)
     981                 :          0 :                 rte_zmalloc_socket("rq->free_mbufs",
     982                 :            :                                    sizeof(struct rte_mbuf *) *
     983                 :            :                                    ENIC_RX_BURST_MAX,
     984                 :          0 :                                    RTE_CACHE_LINE_SIZE, rq_sop->socket_id);
     985         [ #  # ]:          0 :         if (rq_sop->free_mbufs == NULL)
     986                 :          0 :                 goto err_free_data_mbuf;
     987                 :          0 :         rq_sop->num_free_mbufs = 0;
     988                 :            : 
     989                 :          0 :         rq_sop->tot_nb_desc = nb_desc; /* squirl away for MTU update function */
     990                 :            : 
     991                 :          0 :         return 0;
     992                 :            : 
     993                 :            : err_free_data_mbuf:
     994                 :          0 :         rte_free(rq_data->mbuf_ring);
     995                 :          0 : err_free_sop_mbuf:
     996                 :          0 :         rte_free(rq_sop->mbuf_ring);
     997                 :          0 : err_free_cq:
     998                 :            :         /* cleanup on error */
     999                 :          0 :         vnic_cq_free(&enic->cq[cq_idx]);
    1000                 :          0 : err_free_rq_data:
    1001         [ #  # ]:          0 :         if (rq_data->in_use)
    1002                 :          0 :                 vnic_rq_free(rq_data);
    1003                 :          0 : err_free_rq_sop:
    1004                 :          0 :         vnic_rq_free(rq_sop);
    1005                 :            : err_exit:
    1006                 :            :         return -ENOMEM;
    1007                 :            : }
    1008                 :            : 
    1009                 :          0 : void enic_free_wq(void *txq)
    1010                 :            : {
    1011                 :            :         struct vnic_wq *wq;
    1012                 :            :         struct enic *enic;
    1013                 :            : 
    1014         [ #  # ]:          0 :         if (txq == NULL)
    1015                 :            :                 return;
    1016                 :            : 
    1017                 :            :         wq = (struct vnic_wq *)txq;
    1018                 :          0 :         enic = vnic_dev_priv(wq->vdev);
    1019                 :          0 :         rte_memzone_free(wq->cqmsg_rz);
    1020                 :          0 :         vnic_wq_free(wq);
    1021                 :          0 :         vnic_cq_free(&enic->cq[enic->rq_count + wq->index]);
    1022                 :            : }
    1023                 :            : 
    1024                 :          0 : int enic_alloc_wq(struct enic *enic, uint16_t queue_idx,
    1025                 :            :         unsigned int socket_id, uint16_t nb_desc)
    1026                 :            : {
    1027                 :            :         struct enic_vf_representor *vf;
    1028                 :            :         int err;
    1029                 :            :         struct vnic_wq *wq;
    1030                 :            :         unsigned int cq_index;
    1031                 :            :         char name[RTE_MEMZONE_NAMESIZE];
    1032                 :            :         static int instance;
    1033                 :            : 
    1034                 :            :         /*
    1035                 :            :          * Representor uses a reserved PF queue. Translate representor
    1036                 :            :          * queue number to PF queue number.
    1037                 :            :          */
    1038         [ #  # ]:          0 :         if (rte_eth_dev_is_repr(enic->rte_dev)) {
    1039                 :            :                 RTE_ASSERT(queue_idx == 0);
    1040                 :            :                 vf = VF_ENIC_TO_VF_REP(enic);
    1041                 :          0 :                 queue_idx = vf->pf_wq_idx;
    1042                 :          0 :                 cq_index = vf->pf_wq_cq_idx;
    1043                 :          0 :                 enic = vf->pf;
    1044                 :            :         } else {
    1045                 :          0 :                 cq_index = enic_cq_wq(enic, queue_idx);
    1046                 :            :         }
    1047                 :          0 :         wq = &enic->wq[queue_idx];
    1048                 :          0 :         wq->socket_id = socket_id;
    1049                 :            :         /*
    1050                 :            :          * rte_eth_tx_queue_setup() checks min, max, and alignment. So just
    1051                 :            :          * print an info message for diagnostics.
    1052                 :            :          */
    1053                 :          0 :         dev_info(enic, "TX Queues - effective number of descs:%d\n", nb_desc);
    1054                 :            : 
    1055                 :            :         /* Allocate queue resources */
    1056                 :          0 :         err = vnic_wq_alloc(enic->vdev, &enic->wq[queue_idx], queue_idx,
    1057                 :            :                 nb_desc,
    1058                 :            :                 sizeof(struct wq_enet_desc));
    1059         [ #  # ]:          0 :         if (err) {
    1060                 :          0 :                 dev_err(enic, "error in allocation of wq\n");
    1061                 :          0 :                 return err;
    1062                 :            :         }
    1063                 :            : 
    1064                 :          0 :         err = vnic_cq_alloc(enic->vdev, &enic->cq[cq_index], cq_index,
    1065                 :            :                 socket_id, nb_desc,
    1066                 :            :                 sizeof(struct cq_enet_wq_desc));
    1067         [ #  # ]:          0 :         if (err) {
    1068                 :          0 :                 vnic_wq_free(wq);
    1069                 :          0 :                 dev_err(enic, "error in allocation of cq for wq\n");
    1070                 :            :         }
    1071                 :            : 
    1072                 :            :         /* setup up CQ message */
    1073                 :          0 :         snprintf((char *)name, sizeof(name),
    1074                 :          0 :                  "vnic_cqmsg-%s-%d-%d", enic->bdf_name, queue_idx,
    1075                 :            :                 instance++);
    1076                 :            : 
    1077                 :          0 :         wq->cqmsg_rz = rte_memzone_reserve_aligned((const char *)name,
    1078                 :            :                         sizeof(uint32_t), SOCKET_ID_ANY,
    1079                 :            :                         RTE_MEMZONE_IOVA_CONTIG, ENIC_PAGE_SIZE);
    1080         [ #  # ]:          0 :         if (!wq->cqmsg_rz)
    1081                 :          0 :                 return -ENOMEM;
    1082                 :            : 
    1083                 :            :         return err;
    1084                 :            : }
    1085                 :            : 
    1086                 :          0 : int enic_disable(struct enic *enic)
    1087                 :            : {
    1088                 :            :         unsigned int i;
    1089                 :            :         int err;
    1090                 :            : 
    1091         [ #  # ]:          0 :         for (i = 0; i < enic->intr_count; i++) {
    1092                 :          0 :                 vnic_intr_mask(&enic->intr[i]);
    1093                 :          0 :                 (void)vnic_intr_masked(&enic->intr[i]); /* flush write */
    1094                 :            :         }
    1095                 :            :         enic_rxq_intr_deinit(enic);
    1096                 :          0 :         rte_intr_disable(enic->pdev->intr_handle);
    1097                 :          0 :         rte_intr_callback_unregister(enic->pdev->intr_handle,
    1098                 :            :                                      enic_intr_handler,
    1099                 :          0 :                                      (void *)enic->rte_dev);
    1100                 :            : 
    1101                 :          0 :         vnic_dev_disable(enic->vdev);
    1102                 :            : 
    1103                 :          0 :         enic_fm_destroy(enic);
    1104                 :            : 
    1105                 :          0 :         enic_dev_del_addr(enic, enic->mac_addr);
    1106                 :            : 
    1107         [ #  # ]:          0 :         for (i = 0; i < enic->wq_count; i++) {
    1108                 :          0 :                 err = vnic_wq_disable(&enic->wq[i]);
    1109         [ #  # ]:          0 :                 if (err)
    1110                 :          0 :                         return err;
    1111                 :            :         }
    1112         [ #  # ]:          0 :         for (i = 0; i < enic_vnic_rq_count(enic); i++) {
    1113         [ #  # ]:          0 :                 if (enic->rq[i].in_use) {
    1114                 :          0 :                         err = vnic_rq_disable(&enic->rq[i]);
    1115         [ #  # ]:          0 :                         if (err)
    1116                 :          0 :                                 return err;
    1117                 :            :                 }
    1118                 :            :         }
    1119                 :            : 
    1120                 :            :         /* If we were using interrupts, set the interrupt vector to -1
    1121                 :            :          * to disable interrupts.  We are not disabling link notifications,
    1122                 :            :          * though, as we want the polling of link status to continue working.
    1123                 :            :          */
    1124         [ #  # ]:          0 :         if (enic->rte_dev->data->dev_conf.intr_conf.lsc)
    1125                 :          0 :                 vnic_dev_notify_set(enic->vdev, -1);
    1126                 :            : 
    1127                 :          0 :         vnic_dev_set_reset_flag(enic->vdev, 1);
    1128                 :            : 
    1129         [ #  # ]:          0 :         for (i = 0; i < enic->wq_count; i++)
    1130                 :          0 :                 vnic_wq_clean(&enic->wq[i], enic_free_wq_buf);
    1131                 :            : 
    1132         [ #  # ]:          0 :         for (i = 0; i < enic_vnic_rq_count(enic); i++)
    1133         [ #  # ]:          0 :                 if (enic->rq[i].in_use)
    1134                 :          0 :                         vnic_rq_clean(&enic->rq[i], enic_free_rq_buf);
    1135         [ #  # ]:          0 :         for (i = 0; i < enic->cq_count; i++)
    1136                 :          0 :                 vnic_cq_clean(&enic->cq[i]);
    1137         [ #  # ]:          0 :         for (i = 0; i < enic->intr_count; i++)
    1138                 :          0 :                 vnic_intr_clean(&enic->intr[i]);
    1139                 :            : 
    1140         [ #  # ]:          0 :         if (enic_is_vf(enic))
    1141                 :          0 :                 enic_disable_vf_admin_chan(enic, true);
    1142                 :            :         return 0;
    1143                 :            : }
    1144                 :            : 
    1145                 :          0 : static int enic_dev_wait(struct vnic_dev *vdev,
    1146                 :            :         int (*start)(struct vnic_dev *, int),
    1147                 :            :         int (*finished)(struct vnic_dev *, int *),
    1148                 :            :         int arg)
    1149                 :            : {
    1150                 :            :         int done;
    1151                 :            :         int err;
    1152                 :            :         int i;
    1153                 :            : 
    1154                 :          0 :         err = start(vdev, arg);
    1155         [ #  # ]:          0 :         if (err)
    1156                 :            :                 return err;
    1157                 :            : 
    1158                 :            :         /* Wait for func to complete...2 seconds max */
    1159         [ #  # ]:          0 :         for (i = 0; i < 2000; i++) {
    1160                 :          0 :                 err = finished(vdev, &done);
    1161         [ #  # ]:          0 :                 if (err)
    1162                 :          0 :                         return err;
    1163         [ #  # ]:          0 :                 if (done)
    1164                 :            :                         return 0;
    1165                 :          0 :                 usleep(1000);
    1166                 :            :         }
    1167                 :            :         return -ETIMEDOUT;
    1168                 :            : }
    1169                 :            : 
    1170                 :          0 : static int enic_dev_open(struct enic *enic)
    1171                 :            : {
    1172                 :            :         int err;
    1173                 :            :         int flags = CMD_OPENF_IG_DESCCACHE;
    1174                 :            : 
    1175                 :          0 :         err = enic_dev_wait(enic->vdev, vnic_dev_open,
    1176                 :            :                 vnic_dev_open_done, flags);
    1177         [ #  # ]:          0 :         if (err)
    1178                 :          0 :                 dev_err(enic_get_dev(enic),
    1179                 :            :                         "vNIC device open failed, err %d\n", err);
    1180                 :            : 
    1181                 :          0 :         return err;
    1182                 :            : }
    1183                 :            : 
    1184                 :          0 : static int enic_set_rsskey(struct enic *enic, uint8_t *user_key)
    1185                 :            : {
    1186                 :            :         dma_addr_t rss_key_buf_pa;
    1187                 :            :         union vnic_rss_key *rss_key_buf_va = NULL;
    1188                 :            :         int err, i;
    1189                 :            :         uint8_t name[RTE_MEMZONE_NAMESIZE];
    1190                 :            : 
    1191                 :            :         RTE_ASSERT(user_key != NULL);
    1192                 :          0 :         snprintf((char *)name, sizeof(name), "rss_key-%s", enic->bdf_name);
    1193                 :          0 :         rss_key_buf_va = enic_alloc_consistent(enic, sizeof(union vnic_rss_key),
    1194                 :            :                 &rss_key_buf_pa, name);
    1195         [ #  # ]:          0 :         if (!rss_key_buf_va)
    1196                 :            :                 return -ENOMEM;
    1197                 :            : 
    1198         [ #  # ]:          0 :         for (i = 0; i < ENIC_RSS_HASH_KEY_SIZE; i++)
    1199                 :          0 :                 rss_key_buf_va->key[i / 10].b[i % 10] = user_key[i];
    1200                 :            : 
    1201                 :          0 :         err = enic_set_rss_key(enic,
    1202                 :            :                 rss_key_buf_pa,
    1203                 :            :                 sizeof(union vnic_rss_key));
    1204                 :            : 
    1205                 :            :         /* Save for later queries */
    1206         [ #  # ]:          0 :         if (!err) {
    1207                 :            :                 rte_memcpy(&enic->rss_key, rss_key_buf_va,
    1208                 :            :                            sizeof(union vnic_rss_key));
    1209                 :            :         }
    1210                 :          0 :         enic_free_consistent(enic, sizeof(union vnic_rss_key),
    1211                 :            :                 rss_key_buf_va, rss_key_buf_pa);
    1212                 :            : 
    1213                 :          0 :         return err;
    1214                 :            : }
    1215                 :            : 
    1216                 :          0 : int enic_set_rss_reta(struct enic *enic, union vnic_rss_cpu *rss_cpu)
    1217                 :            : {
    1218                 :            :         dma_addr_t rss_cpu_buf_pa;
    1219                 :            :         union vnic_rss_cpu *rss_cpu_buf_va = NULL;
    1220                 :            :         int err;
    1221                 :            :         uint8_t name[RTE_MEMZONE_NAMESIZE];
    1222                 :            : 
    1223                 :          0 :         snprintf((char *)name, sizeof(name), "rss_cpu-%s", enic->bdf_name);
    1224                 :          0 :         rss_cpu_buf_va = enic_alloc_consistent(enic, sizeof(union vnic_rss_cpu),
    1225                 :            :                 &rss_cpu_buf_pa, name);
    1226         [ #  # ]:          0 :         if (!rss_cpu_buf_va)
    1227                 :            :                 return -ENOMEM;
    1228                 :            : 
    1229                 :          0 :         *rss_cpu_buf_va = *rss_cpu;
    1230                 :            : 
    1231                 :          0 :         err = enic_set_rss_cpu(enic,
    1232                 :            :                 rss_cpu_buf_pa,
    1233                 :            :                 sizeof(union vnic_rss_cpu));
    1234                 :            : 
    1235                 :          0 :         enic_free_consistent(enic, sizeof(union vnic_rss_cpu),
    1236                 :            :                 rss_cpu_buf_va, rss_cpu_buf_pa);
    1237                 :            : 
    1238                 :            :         /* Save for later queries */
    1239         [ #  # ]:          0 :         if (!err)
    1240         [ #  # ]:          0 :                 rte_memcpy(&enic->rss_cpu, rss_cpu, sizeof(union vnic_rss_cpu));
    1241                 :            :         return err;
    1242                 :            : }
    1243                 :            : 
    1244                 :            : static int enic_set_niccfg(struct enic *enic, uint8_t rss_default_cpu,
    1245                 :            :         uint8_t rss_hash_type, uint8_t rss_hash_bits, uint8_t rss_base_cpu,
    1246                 :            :         uint8_t rss_enable)
    1247                 :            : {
    1248                 :            :         const uint8_t tso_ipid_split_en = 0;
    1249                 :            :         int err;
    1250                 :            : 
    1251                 :          0 :         err = enic_set_nic_cfg(enic,
    1252                 :            :                 rss_default_cpu, rss_hash_type,
    1253                 :            :                 rss_hash_bits, rss_base_cpu,
    1254                 :            :                 rss_enable, tso_ipid_split_en,
    1255                 :          0 :                 enic->ig_vlan_strip_en);
    1256                 :            : 
    1257                 :            :         return err;
    1258                 :            : }
    1259                 :            : 
    1260                 :            : /* Initialize RSS with defaults, called from dev_configure */
    1261                 :          0 : int enic_init_rss_nic_cfg(struct enic *enic)
    1262                 :            : {
    1263                 :            :         static uint8_t default_rss_key[] = {
    1264                 :            :                 85, 67, 83, 97, 119, 101, 115, 111, 109, 101,
    1265                 :            :                 80, 65, 76, 79, 117, 110, 105, 113, 117, 101,
    1266                 :            :                 76, 73, 78, 85, 88, 114, 111, 99, 107, 115,
    1267                 :            :                 69, 78, 73, 67, 105, 115, 99, 111, 111, 108,
    1268                 :            :         };
    1269                 :            :         struct rte_eth_rss_conf rss_conf;
    1270                 :            :         union vnic_rss_cpu rss_cpu;
    1271                 :            :         int ret, i;
    1272                 :            : 
    1273                 :          0 :         rss_conf = enic->rte_dev->data->dev_conf.rx_adv_conf.rss_conf;
    1274                 :            :         /*
    1275                 :            :          * If setting key for the first time, and the user gives us none, then
    1276                 :            :          * push the default key to NIC.
    1277                 :            :          */
    1278         [ #  # ]:          0 :         if (rss_conf.rss_key == NULL) {
    1279                 :          0 :                 rss_conf.rss_key = default_rss_key;
    1280                 :          0 :                 rss_conf.rss_key_len = ENIC_RSS_HASH_KEY_SIZE;
    1281                 :            :         }
    1282                 :          0 :         ret = enic_set_rss_conf(enic, &rss_conf);
    1283         [ #  # ]:          0 :         if (ret) {
    1284                 :          0 :                 dev_err(enic, "Failed to configure RSS\n");
    1285                 :          0 :                 return ret;
    1286                 :            :         }
    1287         [ #  # ]:          0 :         if (enic->rss_enable) {
    1288                 :            :                 /* If enabling RSS, use the default reta */
    1289         [ #  # ]:          0 :                 for (i = 0; i < ENIC_RSS_RETA_SIZE; i++) {
    1290                 :          0 :                         rss_cpu.cpu[i / 4].b[i % 4] =
    1291                 :          0 :                                 enic_rte_rq_idx_to_sop_idx(i % enic->rq_count);
    1292                 :            :                 }
    1293                 :          0 :                 ret = enic_set_rss_reta(enic, &rss_cpu);
    1294         [ #  # ]:          0 :                 if (ret)
    1295                 :          0 :                         dev_err(enic, "Failed to set RSS indirection table\n");
    1296                 :            :         }
    1297                 :            :         return ret;
    1298                 :            : }
    1299                 :            : 
    1300                 :          0 : int enic_setup_finish(struct enic *enic)
    1301                 :            : {
    1302                 :            :         int err;
    1303                 :            : 
    1304                 :          0 :         ENICPMD_FUNC_TRACE();
    1305                 :            :         enic_clear_soft_stats(enic);
    1306                 :            : 
    1307                 :            :         /*
    1308                 :            :          * Enable admin channel so we can perform certain devcmds
    1309                 :            :          * via admin channel. For example, vnic_dev_packet_filter()
    1310                 :            :          */
    1311         [ #  # ]:          0 :         if (enic_is_vf(enic)) {
    1312                 :          0 :                 err = enic_enable_vf_admin_chan(enic);
    1313         [ #  # ]:          0 :                 if (err)
    1314                 :            :                         return err;
    1315                 :            :         }
    1316                 :            : 
    1317                 :            :         /* switchdev: enable promisc mode on PF */
    1318         [ #  # ]:          0 :         if (enic->switchdev_mode) {
    1319         [ #  # ]:          0 :                 RTE_VERIFY(!enic_is_vf(enic));
    1320                 :          0 :                 vnic_dev_packet_filter(enic->vdev,
    1321                 :            :                                        0 /* directed  */,
    1322                 :            :                                        0 /* multicast */,
    1323                 :            :                                        0 /* broadcast */,
    1324                 :            :                                        1 /* promisc   */,
    1325                 :            :                                        0 /* allmulti  */);
    1326                 :          0 :                 enic->promisc = 1;
    1327                 :          0 :                 enic->allmulti = 0;
    1328                 :          0 :                 return 0;
    1329                 :            :         }
    1330                 :            :         /* Default conf */
    1331                 :          0 :         err = enic_dev_packet_filter(enic,
    1332                 :            :                 1 /* directed  */,
    1333                 :            :                 1 /* multicast */,
    1334                 :            :                 1 /* broadcast */,
    1335                 :            :                 0 /* promisc   */,
    1336                 :            :                 1 /* allmulti  */);
    1337                 :            : 
    1338                 :          0 :         enic->promisc = 0;
    1339                 :          0 :         enic->allmulti = 1;
    1340                 :            : 
    1341                 :          0 :         return err;
    1342                 :            : }
    1343                 :            : 
    1344                 :          0 : static int enic_rss_conf_valid(struct enic *enic,
    1345                 :            :                                struct rte_eth_rss_conf *rss_conf)
    1346                 :            : {
    1347                 :            :         /* RSS is disabled per VIC settings. Ignore rss_conf. */
    1348         [ #  # ]:          0 :         if (enic->flow_type_rss_offloads == 0)
    1349                 :            :                 return 0;
    1350         [ #  # ]:          0 :         if (rss_conf->rss_key != NULL &&
    1351         [ #  # ]:          0 :             rss_conf->rss_key_len != ENIC_RSS_HASH_KEY_SIZE) {
    1352                 :          0 :                 dev_err(enic, "Given rss_key is %d bytes, it must be %d\n",
    1353                 :            :                         rss_conf->rss_key_len, ENIC_RSS_HASH_KEY_SIZE);
    1354                 :          0 :                 return -EINVAL;
    1355                 :            :         }
    1356         [ #  # ]:          0 :         if (rss_conf->rss_hf != 0 &&
    1357         [ #  # ]:          0 :             (rss_conf->rss_hf & enic->flow_type_rss_offloads) == 0) {
    1358                 :          0 :                 dev_err(enic, "Given rss_hf contains none of the supported"
    1359                 :            :                         " types\n");
    1360                 :          0 :                 return -EINVAL;
    1361                 :            :         }
    1362                 :            :         return 0;
    1363                 :            : }
    1364                 :            : 
    1365                 :            : /* Set hash type and key according to rss_conf */
    1366                 :          0 : int enic_set_rss_conf(struct enic *enic, struct rte_eth_rss_conf *rss_conf)
    1367                 :            : {
    1368                 :            :         struct rte_eth_dev *eth_dev;
    1369                 :            :         uint64_t rss_hf;
    1370                 :            :         uint8_t rss_hash_type;
    1371                 :            :         uint8_t rss_enable;
    1372                 :            :         int ret;
    1373                 :            : 
    1374                 :            :         RTE_ASSERT(rss_conf != NULL);
    1375                 :          0 :         ret = enic_rss_conf_valid(enic, rss_conf);
    1376         [ #  # ]:          0 :         if (ret) {
    1377                 :          0 :                 dev_err(enic, "RSS configuration (rss_conf) is invalid\n");
    1378                 :          0 :                 return ret;
    1379                 :            :         }
    1380                 :            : 
    1381                 :          0 :         eth_dev = enic->rte_dev;
    1382                 :            :         rss_hash_type = 0;
    1383                 :          0 :         rss_hf = rss_conf->rss_hf & enic->flow_type_rss_offloads;
    1384         [ #  # ]:          0 :         if (enic->rq_count > 1 &&
    1385   [ #  #  #  # ]:          0 :             (eth_dev->data->dev_conf.rxmode.mq_mode & RTE_ETH_MQ_RX_RSS_FLAG) &&
    1386                 :            :             rss_hf != 0) {
    1387                 :            :                 rss_enable = 1;
    1388         [ #  # ]:          0 :                 if (rss_hf & (RTE_ETH_RSS_IPV4 | RTE_ETH_RSS_FRAG_IPV4 |
    1389                 :            :                               RTE_ETH_RSS_NONFRAG_IPV4_OTHER))
    1390                 :            :                         rss_hash_type |= NIC_CFG_RSS_HASH_TYPE_IPV4;
    1391         [ #  # ]:          0 :                 if (rss_hf & RTE_ETH_RSS_NONFRAG_IPV4_TCP)
    1392                 :          0 :                         rss_hash_type |= NIC_CFG_RSS_HASH_TYPE_TCP_IPV4;
    1393         [ #  # ]:          0 :                 if (rss_hf & RTE_ETH_RSS_NONFRAG_IPV4_UDP) {
    1394                 :          0 :                         rss_hash_type |= NIC_CFG_RSS_HASH_TYPE_UDP_IPV4;
    1395         [ #  # ]:          0 :                         if (enic->udp_rss_weak) {
    1396                 :            :                                 /*
    1397                 :            :                                  * 'TCP' is not a typo. The "weak" version of
    1398                 :            :                                  * UDP RSS requires both the TCP and UDP bits
    1399                 :            :                                  * be set. It does enable TCP RSS as well.
    1400                 :            :                                  */
    1401                 :          0 :                                 rss_hash_type |= NIC_CFG_RSS_HASH_TYPE_TCP_IPV4;
    1402                 :            :                         }
    1403                 :            :                 }
    1404         [ #  # ]:          0 :                 if (rss_hf & (RTE_ETH_RSS_IPV6 | RTE_ETH_RSS_IPV6_EX |
    1405                 :            :                               RTE_ETH_RSS_FRAG_IPV6 | RTE_ETH_RSS_NONFRAG_IPV6_OTHER))
    1406                 :          0 :                         rss_hash_type |= NIC_CFG_RSS_HASH_TYPE_IPV6;
    1407         [ #  # ]:          0 :                 if (rss_hf & (RTE_ETH_RSS_NONFRAG_IPV6_TCP | RTE_ETH_RSS_IPV6_TCP_EX))
    1408                 :          0 :                         rss_hash_type |= NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
    1409         [ #  # ]:          0 :                 if (rss_hf & (RTE_ETH_RSS_NONFRAG_IPV6_UDP | RTE_ETH_RSS_IPV6_UDP_EX)) {
    1410                 :          0 :                         rss_hash_type |= NIC_CFG_RSS_HASH_TYPE_UDP_IPV6;
    1411         [ #  # ]:          0 :                         if (enic->udp_rss_weak)
    1412                 :          0 :                                 rss_hash_type |= NIC_CFG_RSS_HASH_TYPE_TCP_IPV6;
    1413                 :            :                 }
    1414                 :            :         } else {
    1415                 :            :                 rss_enable = 0;
    1416                 :            :                 rss_hf = 0;
    1417                 :            :         }
    1418                 :            : 
    1419                 :            :         /* Set the hash key if provided */
    1420   [ #  #  #  # ]:          0 :         if (rss_enable && rss_conf->rss_key) {
    1421                 :          0 :                 ret = enic_set_rsskey(enic, rss_conf->rss_key);
    1422         [ #  # ]:          0 :                 if (ret) {
    1423                 :          0 :                         dev_err(enic, "Failed to set RSS key\n");
    1424                 :          0 :                         return ret;
    1425                 :            :                 }
    1426                 :            :         }
    1427                 :            : 
    1428                 :          0 :         ret = enic_set_niccfg(enic, ENIC_RSS_DEFAULT_CPU, rss_hash_type,
    1429                 :            :                               ENIC_RSS_HASH_BITS, ENIC_RSS_BASE_CPU,
    1430                 :            :                               rss_enable);
    1431         [ #  # ]:          0 :         if (!ret) {
    1432                 :          0 :                 enic->rss_hf = rss_hf;
    1433                 :          0 :                 enic->rss_hash_type = rss_hash_type;
    1434                 :          0 :                 enic->rss_enable = rss_enable;
    1435                 :            :         } else {
    1436                 :          0 :                 dev_err(enic, "Failed to update RSS configurations."
    1437                 :            :                         " hash=0x%x\n", rss_hash_type);
    1438                 :            :         }
    1439                 :            :         return ret;
    1440                 :            : }
    1441                 :            : 
    1442                 :          0 : int enic_set_vlan_strip(struct enic *enic)
    1443                 :            : {
    1444                 :            :         /*
    1445                 :            :          * Unfortunately, VLAN strip on/off and RSS on/off are configured
    1446                 :            :          * together. So, re-do niccfg, preserving the current RSS settings.
    1447                 :            :          */
    1448                 :          0 :         return enic_set_niccfg(enic, ENIC_RSS_DEFAULT_CPU, enic->rss_hash_type,
    1449                 :            :                                ENIC_RSS_HASH_BITS, ENIC_RSS_BASE_CPU,
    1450                 :          0 :                                enic->rss_enable);
    1451                 :            : }
    1452                 :            : 
    1453                 :          0 : int enic_add_packet_filter(struct enic *enic)
    1454                 :            : {
    1455                 :          0 :         ENICPMD_FUNC_TRACE();
    1456                 :            :         /* switchdev ignores packet filters */
    1457         [ #  # ]:          0 :         if (enic->switchdev_mode) {
    1458                 :          0 :                 ENICPMD_LOG(DEBUG, " switchdev: ignore packet filter");
    1459                 :          0 :                 return 0;
    1460                 :            :         }
    1461                 :            :         /* Args -> directed, multicast, broadcast, promisc, allmulti */
    1462                 :          0 :         return enic_dev_packet_filter(enic, 1, 1, 1,
    1463                 :            :                 enic->promisc, enic->allmulti);
    1464                 :            : }
    1465                 :            : 
    1466                 :          0 : int enic_get_link_status(struct enic *enic)
    1467                 :            : {
    1468                 :          0 :         return vnic_dev_link_status(enic->vdev);
    1469                 :            : }
    1470                 :            : 
    1471                 :          0 : static void enic_dev_deinit(struct enic *enic)
    1472                 :            : {
    1473                 :            :         /* stop link status checking */
    1474                 :          0 :         vnic_dev_notify_unset(enic->vdev);
    1475                 :            : 
    1476                 :            :         /* mac_addrs is freed by rte_eth_dev_release_port() */
    1477                 :          0 :         rte_free(enic->cq);
    1478                 :          0 :         rte_free(enic->intr);
    1479                 :          0 :         rte_free(enic->rq);
    1480                 :          0 :         rte_free(enic->wq);
    1481                 :          0 : }
    1482                 :            : 
    1483                 :            : 
    1484                 :          0 : int enic_set_vnic_res(struct enic *enic)
    1485                 :            : {
    1486                 :          0 :         struct rte_eth_dev *eth_dev = enic->rte_dev;
    1487                 :            :         int rc = 0;
    1488                 :            :         unsigned int required_rq, required_wq, required_cq, required_intr;
    1489                 :            : 
    1490                 :            :         /* Always use two vNIC RQs per eth_dev RQ, regardless of Rx scatter. */
    1491                 :          0 :         required_rq = eth_dev->data->nb_rx_queues * 2;
    1492                 :          0 :         required_wq = eth_dev->data->nb_tx_queues;
    1493                 :          0 :         required_cq = eth_dev->data->nb_rx_queues + eth_dev->data->nb_tx_queues;
    1494                 :            :         required_intr = 1; /* 1 for LSC even if intr_conf.lsc is 0 */
    1495         [ #  # ]:          0 :         if (eth_dev->data->dev_conf.intr_conf.rxq) {
    1496                 :          0 :                 required_intr += eth_dev->data->nb_rx_queues;
    1497                 :            :         }
    1498                 :            :         /* FW adds 2 interrupts for admin chan. Use 1 for RQ */
    1499         [ #  # ]:          0 :         if (enic_is_vf(enic))
    1500                 :          0 :                 required_intr += 1;
    1501                 :          0 :         ENICPMD_LOG(DEBUG, "Required queues for PF: rq %u wq %u cq %u",
    1502                 :            :                     required_rq, required_wq, required_cq);
    1503         [ #  # ]:          0 :         if (enic->vf_required_rq) {
    1504                 :            :                 /* Queues needed for VF representors */
    1505                 :          0 :                 required_rq += enic->vf_required_rq;
    1506                 :          0 :                 required_wq += enic->vf_required_wq;
    1507                 :          0 :                 required_cq += enic->vf_required_cq;
    1508                 :          0 :                 ENICPMD_LOG(DEBUG, "Required queues for VF representors: rq %u wq %u cq %u",
    1509                 :            :                             enic->vf_required_rq, enic->vf_required_wq,
    1510                 :            :                             enic->vf_required_cq);
    1511                 :            :         }
    1512                 :            : 
    1513         [ #  # ]:          0 :         if (enic->conf_rq_count < required_rq) {
    1514                 :          0 :                 dev_err(dev, "Not enough Receive queues. Requested:%u which uses %d RQs on VIC, Configured:%u\n",
    1515                 :            :                         eth_dev->data->nb_rx_queues,
    1516                 :            :                         required_rq, enic->conf_rq_count);
    1517                 :            :                 rc = -EINVAL;
    1518                 :            :         }
    1519         [ #  # ]:          0 :         if (enic->conf_wq_count < required_wq) {
    1520                 :          0 :                 dev_err(dev, "Not enough Transmit queues. Requested:%u, Configured:%u\n",
    1521                 :            :                         eth_dev->data->nb_tx_queues, enic->conf_wq_count);
    1522                 :            :                 rc = -EINVAL;
    1523                 :            :         }
    1524                 :            : 
    1525         [ #  # ]:          0 :         if (enic->conf_cq_count < required_cq) {
    1526                 :          0 :                 dev_err(dev, "Not enough Completion queues. Required:%u, Configured:%u\n",
    1527                 :            :                         required_cq, enic->conf_cq_count);
    1528                 :            :                 rc = -EINVAL;
    1529                 :            :         }
    1530         [ #  # ]:          0 :         if (enic->conf_intr_count < required_intr) {
    1531                 :          0 :                 dev_err(dev, "Not enough Interrupts to support Rx queue"
    1532                 :            :                         " interrupts. Required:%u, Configured:%u\n",
    1533                 :            :                         required_intr, enic->conf_intr_count);
    1534                 :            :                 rc = -EINVAL;
    1535                 :            :         }
    1536                 :            : 
    1537         [ #  # ]:          0 :         if (rc == 0) {
    1538                 :          0 :                 enic->rq_count = eth_dev->data->nb_rx_queues;
    1539                 :          0 :                 enic->wq_count = eth_dev->data->nb_tx_queues;
    1540                 :          0 :                 enic->cq_count = enic->rq_count + enic->wq_count;
    1541                 :          0 :                 enic->intr_count = required_intr;
    1542                 :            :         }
    1543                 :            : 
    1544                 :          0 :         return rc;
    1545                 :            : }
    1546                 :            : 
    1547                 :            : /* Initialize the completion queue for an RQ */
    1548                 :            : static int
    1549                 :          0 : enic_reinit_rq(struct enic *enic, unsigned int rq_idx)
    1550                 :            : {
    1551                 :            :         struct vnic_rq *sop_rq, *data_rq;
    1552                 :            :         unsigned int cq_idx;
    1553                 :            :         int rc = 0;
    1554                 :            : 
    1555                 :          0 :         sop_rq = &enic->rq[enic_rte_rq_idx_to_sop_idx(rq_idx)];
    1556                 :          0 :         data_rq = &enic->rq[enic_rte_rq_idx_to_data_idx(rq_idx, enic)];
    1557                 :            :         cq_idx = enic_cq_rq(enic, rq_idx);
    1558                 :            : 
    1559                 :          0 :         vnic_cq_clean(&enic->cq[cq_idx]);
    1560                 :          0 :         vnic_cq_init(&enic->cq[cq_idx],
    1561                 :            :                      0 /* flow_control_enable */,
    1562                 :            :                      1 /* color_enable */,
    1563                 :            :                      0 /* cq_head */,
    1564                 :            :                      0 /* cq_tail */,
    1565                 :            :                      1 /* cq_tail_color */,
    1566                 :            :                      0 /* interrupt_enable */,
    1567                 :            :                      1 /* cq_entry_enable */,
    1568                 :            :                      0 /* cq_message_enable */,
    1569                 :            :                      0 /* interrupt offset */,
    1570                 :            :                      0 /* cq_message_addr */);
    1571                 :            : 
    1572                 :            : 
    1573                 :          0 :         vnic_rq_init_start(sop_rq, enic_cq_rq(enic,
    1574                 :            :                            enic_rte_rq_idx_to_sop_idx(rq_idx)), 0,
    1575                 :          0 :                            sop_rq->ring.desc_count - 1, 1, 0);
    1576         [ #  # ]:          0 :         if (data_rq->in_use) {
    1577                 :          0 :                 vnic_rq_init_start(data_rq,
    1578                 :            :                                    enic_cq_rq(enic,
    1579                 :            :                                    enic_rte_rq_idx_to_data_idx(rq_idx, enic)),
    1580                 :          0 :                                    0, data_rq->ring.desc_count - 1, 1, 0);
    1581                 :            :         }
    1582                 :            : 
    1583                 :          0 :         rc = enic_alloc_rx_queue_mbufs(enic, sop_rq);
    1584         [ #  # ]:          0 :         if (rc)
    1585                 :            :                 return rc;
    1586                 :            : 
    1587         [ #  # ]:          0 :         if (data_rq->in_use) {
    1588                 :          0 :                 rc = enic_alloc_rx_queue_mbufs(enic, data_rq);
    1589         [ #  # ]:          0 :                 if (rc) {
    1590                 :          0 :                         enic_rxmbuf_queue_release(enic, sop_rq);
    1591                 :          0 :                         return rc;
    1592                 :            :                 }
    1593                 :            :         }
    1594                 :            : 
    1595                 :            :         return 0;
    1596                 :            : }
    1597                 :            : 
    1598                 :            : /* The Cisco NIC can send and receive packets up to a max packet size
    1599                 :            :  * determined by the NIC type and firmware. There is also an MTU
    1600                 :            :  * configured into the NIC via the CIMC/UCSM management interface
    1601                 :            :  * which can be overridden by this function (up to the max packet size).
    1602                 :            :  * Depending on the network setup, doing so may cause packet drops
    1603                 :            :  * and unexpected behavior.
    1604                 :            :  */
    1605                 :          0 : int enic_set_mtu(struct enic *enic, uint16_t new_mtu)
    1606                 :            : {
    1607                 :            :         unsigned int rq_idx;
    1608                 :            :         struct vnic_rq *rq;
    1609                 :            :         int rc = 0;
    1610                 :            :         uint16_t old_mtu;       /* previous setting */
    1611                 :            :         uint16_t config_mtu;    /* Value configured into NIC via CIMC/UCSM */
    1612                 :          0 :         struct rte_eth_dev *eth_dev = enic->rte_dev;
    1613                 :            : 
    1614                 :          0 :         old_mtu = eth_dev->data->mtu;
    1615                 :          0 :         config_mtu = enic->config.mtu;
    1616                 :            : 
    1617         [ #  # ]:          0 :         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
    1618                 :            :                 return -E_RTE_SECONDARY;
    1619                 :            : 
    1620         [ #  # ]:          0 :         if (new_mtu > enic->max_mtu) {
    1621                 :          0 :                 dev_err(enic,
    1622                 :            :                         "MTU not updated: requested (%u) greater than max (%u)\n",
    1623                 :            :                         new_mtu, enic->max_mtu);
    1624                 :          0 :                 return -EINVAL;
    1625                 :            :         }
    1626         [ #  # ]:          0 :         if (new_mtu < ENIC_MIN_MTU) {
    1627                 :          0 :                 dev_info(enic,
    1628                 :            :                         "MTU not updated: requested (%u) less than min (%u)\n",
    1629                 :            :                         new_mtu, ENIC_MIN_MTU);
    1630                 :          0 :                 return -EINVAL;
    1631                 :            :         }
    1632         [ #  # ]:          0 :         if (new_mtu > config_mtu)
    1633                 :          0 :                 dev_warning(enic,
    1634                 :            :                         "MTU (%u) is greater than value configured in NIC (%u)\n",
    1635                 :            :                         new_mtu, config_mtu);
    1636                 :            : 
    1637                 :            :         /*
    1638                 :            :          * If the device has not started (enic_enable), nothing to do.
    1639                 :            :          * Later, enic_enable() will set up RQs reflecting the new maximum
    1640                 :            :          * packet length.
    1641                 :            :          */
    1642         [ #  # ]:          0 :         if (!eth_dev->data->dev_started)
    1643                 :            :                 return rc;
    1644                 :            : 
    1645                 :            :         /*
    1646                 :            :          * The device has started, re-do RQs on the fly. In the process, we
    1647                 :            :          * pick up the new maximum packet length.
    1648                 :            :          *
    1649                 :            :          * Some applications rely on the ability to change MTU without stopping
    1650                 :            :          * the device. So keep this behavior for now.
    1651                 :            :          */
    1652                 :          0 :         rte_spinlock_lock(&enic->mtu_lock);
    1653                 :            : 
    1654                 :            :         /* Stop traffic on all RQs */
    1655         [ #  # ]:          0 :         for (rq_idx = 0; rq_idx < enic->rq_count * 2; rq_idx++) {
    1656                 :          0 :                 rq = &enic->rq[rq_idx];
    1657   [ #  #  #  # ]:          0 :                 if (rq->is_sop && rq->in_use) {
    1658                 :          0 :                         rc = enic_stop_rq(enic,
    1659                 :            :                                           enic_sop_rq_idx_to_rte_idx(rq_idx));
    1660         [ #  # ]:          0 :                         if (rc) {
    1661                 :          0 :                                 dev_err(enic, "Failed to stop Rq %u\n", rq_idx);
    1662                 :          0 :                                 goto set_mtu_done;
    1663                 :            :                         }
    1664                 :            :                 }
    1665                 :            :         }
    1666                 :            : 
    1667                 :            :         /* replace Rx function with a no-op to avoid getting stale pkts */
    1668                 :          0 :         eth_dev->rx_pkt_burst = rte_eth_pkt_burst_dummy;
    1669                 :          0 :         rte_eth_fp_ops[enic->port_id].rx_pkt_burst = eth_dev->rx_pkt_burst;
    1670                 :            :         rte_mb();
    1671                 :            : 
    1672                 :            :         /* Allow time for threads to exit the real Rx function. */
    1673                 :          0 :         usleep(100000);
    1674                 :            : 
    1675                 :            :         /* now it is safe to reconfigure the RQs */
    1676                 :            : 
    1677                 :            : 
    1678                 :            :         /* free and reallocate RQs with the new MTU */
    1679         [ #  # ]:          0 :         for (rq_idx = 0; rq_idx < enic->rq_count; rq_idx++) {
    1680                 :          0 :                 rq = &enic->rq[enic_rte_rq_idx_to_sop_idx(rq_idx)];
    1681         [ #  # ]:          0 :                 if (!rq->in_use)
    1682                 :          0 :                         continue;
    1683                 :            : 
    1684                 :          0 :                 enic_free_rq(rq);
    1685                 :          0 :                 rc = enic_alloc_rq(enic, rq_idx, rq->socket_id, rq->mp,
    1686                 :          0 :                                    rq->tot_nb_desc, rq->rx_free_thresh);
    1687         [ #  # ]:          0 :                 if (rc) {
    1688                 :          0 :                         dev_err(enic,
    1689                 :            :                                 "Fatal MTU alloc error- No traffic will pass\n");
    1690                 :          0 :                         goto set_mtu_done;
    1691                 :            :                 }
    1692                 :            : 
    1693                 :          0 :                 rc = enic_reinit_rq(enic, rq_idx);
    1694         [ #  # ]:          0 :                 if (rc) {
    1695                 :          0 :                         dev_err(enic,
    1696                 :            :                                 "Fatal MTU RQ reinit- No traffic will pass\n");
    1697                 :          0 :                         goto set_mtu_done;
    1698                 :            :                 }
    1699                 :            :         }
    1700                 :            : 
    1701                 :            :         /* put back the real receive function */
    1702                 :            :         rte_mb();
    1703                 :          0 :         enic_pick_rx_handler(eth_dev);
    1704                 :          0 :         rte_eth_fp_ops[enic->port_id].rx_pkt_burst = eth_dev->rx_pkt_burst;
    1705                 :            :         rte_mb();
    1706                 :            : 
    1707                 :            :         /* restart Rx traffic */
    1708         [ #  # ]:          0 :         for (rq_idx = 0; rq_idx < enic->rq_count; rq_idx++) {
    1709                 :          0 :                 rq = &enic->rq[enic_rte_rq_idx_to_sop_idx(rq_idx)];
    1710   [ #  #  #  # ]:          0 :                 if (rq->is_sop && rq->in_use)
    1711                 :          0 :                         enic_start_rq(enic, rq_idx);
    1712                 :            :         }
    1713                 :            : 
    1714                 :          0 : set_mtu_done:
    1715                 :          0 :         dev_info(enic, "MTU changed from %u to %u\n",  old_mtu, new_mtu);
    1716                 :            :         rte_spinlock_unlock(&enic->mtu_lock);
    1717                 :          0 :         return rc;
    1718                 :            : }
    1719                 :            : 
    1720                 :            : static void
    1721                 :          0 : enic_disable_overlay_offload(struct enic *enic)
    1722                 :            : {
    1723                 :            :         /*
    1724                 :            :          * Disabling fails if the feature is provisioned but
    1725                 :            :          * not enabled. So ignore result and do not log error.
    1726                 :            :          */
    1727         [ #  # ]:          0 :         if (enic->vxlan) {
    1728                 :          0 :                 vnic_dev_overlay_offload_ctrl(enic->vdev,
    1729                 :            :                         OVERLAY_FEATURE_VXLAN, OVERLAY_OFFLOAD_DISABLE);
    1730                 :            :         }
    1731         [ #  # ]:          0 :         if (enic->geneve) {
    1732                 :          0 :                 vnic_dev_overlay_offload_ctrl(enic->vdev,
    1733                 :            :                         OVERLAY_FEATURE_GENEVE, OVERLAY_OFFLOAD_DISABLE);
    1734                 :            :         }
    1735                 :          0 : }
    1736                 :            : 
    1737                 :            : static int
    1738                 :          0 : enic_enable_overlay_offload(struct enic *enic)
    1739                 :            : {
    1740   [ #  #  #  # ]:          0 :         if (enic->vxlan && vnic_dev_overlay_offload_ctrl(enic->vdev,
    1741                 :            :                         OVERLAY_FEATURE_VXLAN, OVERLAY_OFFLOAD_ENABLE) != 0) {
    1742                 :          0 :                 dev_err(NULL, "failed to enable VXLAN offload\n");
    1743                 :          0 :                 return -EINVAL;
    1744                 :            :         }
    1745   [ #  #  #  # ]:          0 :         if (enic->geneve && vnic_dev_overlay_offload_ctrl(enic->vdev,
    1746                 :            :                         OVERLAY_FEATURE_GENEVE, OVERLAY_OFFLOAD_ENABLE) != 0) {
    1747                 :          0 :                 dev_err(NULL, "failed to enable Geneve offload\n");
    1748                 :          0 :                 return -EINVAL;
    1749                 :            :         }
    1750                 :          0 :         enic->tx_offload_capa |=
    1751                 :          0 :                 RTE_ETH_TX_OFFLOAD_OUTER_IPV4_CKSUM |
    1752         [ #  # ]:          0 :                 (enic->geneve ? RTE_ETH_TX_OFFLOAD_GENEVE_TNL_TSO : 0) |
    1753         [ #  # ]:          0 :                 (enic->vxlan ? RTE_ETH_TX_OFFLOAD_VXLAN_TNL_TSO : 0);
    1754                 :          0 :         enic->tx_offload_mask |=
    1755                 :            :                 RTE_MBUF_F_TX_OUTER_IPV6 |
    1756                 :            :                 RTE_MBUF_F_TX_OUTER_IPV4 |
    1757                 :            :                 RTE_MBUF_F_TX_OUTER_IP_CKSUM |
    1758                 :            :                 RTE_MBUF_F_TX_TUNNEL_MASK;
    1759                 :          0 :         enic->overlay_offload = true;
    1760                 :            : 
    1761   [ #  #  #  # ]:          0 :         if (enic->vxlan && enic->geneve)
    1762                 :          0 :                 dev_info(NULL, "Overlay offload is enabled (VxLAN, Geneve)\n");
    1763         [ #  # ]:          0 :         else if (enic->vxlan)
    1764                 :          0 :                 dev_info(NULL, "Overlay offload is enabled (VxLAN)\n");
    1765                 :            :         else
    1766                 :          0 :                 dev_info(NULL, "Overlay offload is enabled (Geneve)\n");
    1767                 :            : 
    1768                 :            :         return 0;
    1769                 :            : }
    1770                 :            : 
    1771                 :            : static int
    1772                 :          0 : enic_reset_overlay_port(struct enic *enic)
    1773                 :            : {
    1774         [ #  # ]:          0 :         if (enic->vxlan) {
    1775                 :          0 :                 enic->vxlan_port = RTE_VXLAN_DEFAULT_PORT;
    1776                 :            :                 /*
    1777                 :            :                  * Reset the vxlan port to the default, as the NIC firmware
    1778                 :            :                  * does not reset it automatically and keeps the old setting.
    1779                 :            :                  */
    1780         [ #  # ]:          0 :                 if (vnic_dev_overlay_offload_cfg(enic->vdev,
    1781                 :            :                                                  OVERLAY_CFG_VXLAN_PORT_UPDATE,
    1782                 :            :                                                  RTE_VXLAN_DEFAULT_PORT)) {
    1783                 :          0 :                         dev_err(enic, "failed to update vxlan port\n");
    1784                 :          0 :                         return -EINVAL;
    1785                 :            :                 }
    1786                 :            :         }
    1787         [ #  # ]:          0 :         if (enic->geneve) {
    1788                 :          0 :                 enic->geneve_port = RTE_GENEVE_DEFAULT_PORT;
    1789         [ #  # ]:          0 :                 if (vnic_dev_overlay_offload_cfg(enic->vdev,
    1790                 :            :                                                  OVERLAY_CFG_GENEVE_PORT_UPDATE,
    1791                 :            :                                                  RTE_GENEVE_DEFAULT_PORT)) {
    1792                 :          0 :                         dev_err(enic, "failed to update vxlan port\n");
    1793                 :          0 :                         return -EINVAL;
    1794                 :            :                 }
    1795                 :            :         }
    1796                 :            :         return 0;
    1797                 :            : }
    1798                 :            : 
    1799                 :          0 : static int enic_dev_init(struct enic *enic)
    1800                 :            : {
    1801                 :            :         int err;
    1802                 :          0 :         struct rte_eth_dev *eth_dev = enic->rte_dev;
    1803                 :            : 
    1804                 :          0 :         vnic_dev_intr_coal_timer_info_default(enic->vdev);
    1805                 :            : 
    1806                 :            :         /* Get vNIC configuration
    1807                 :            :         */
    1808                 :          0 :         err = enic_get_vnic_config(enic);
    1809         [ #  # ]:          0 :         if (err) {
    1810                 :          0 :                 dev_err(dev, "Get vNIC configuration failed, aborting\n");
    1811                 :          0 :                 return err;
    1812                 :            :         }
    1813                 :            : 
    1814                 :            :         /* Get available resource counts */
    1815                 :          0 :         enic_get_res_counts(enic);
    1816         [ #  # ]:          0 :         if (enic->conf_rq_count == 1) {
    1817                 :          0 :                 dev_err(enic, "Running with only 1 RQ configured in the vNIC is not supported.\n");
    1818                 :          0 :                 dev_err(enic, "Please configure 2 RQs in the vNIC for each Rx queue used by DPDK.\n");
    1819                 :          0 :                 dev_err(enic, "See the ENIC PMD guide for more information.\n");
    1820                 :          0 :                 return -EINVAL;
    1821                 :            :         }
    1822                 :            :         /* Queue counts may be zeros. rte_zmalloc returns NULL in that case. */
    1823                 :          0 :         enic->cq = rte_zmalloc("enic_vnic_cq", sizeof(struct vnic_cq) *
    1824                 :          0 :                                enic->conf_cq_count, 8);
    1825                 :          0 :         enic->intr = rte_zmalloc("enic_vnic_intr", sizeof(struct vnic_intr) *
    1826                 :          0 :                                  enic->conf_intr_count, 8);
    1827                 :          0 :         enic->rq = rte_zmalloc("enic_vnic_rq", sizeof(struct vnic_rq) *
    1828                 :          0 :                                enic->conf_rq_count, 8);
    1829                 :          0 :         enic->wq = rte_zmalloc("enic_vnic_wq", sizeof(struct vnic_wq) *
    1830                 :          0 :                                enic->conf_wq_count, 8);
    1831   [ #  #  #  # ]:          0 :         if (enic->conf_cq_count > 0 && enic->cq == NULL) {
    1832                 :          0 :                 dev_err(enic, "failed to allocate vnic_cq, aborting.\n");
    1833                 :          0 :                 return -1;
    1834                 :            :         }
    1835   [ #  #  #  # ]:          0 :         if (enic->conf_intr_count > 0 && enic->intr == NULL) {
    1836                 :          0 :                 dev_err(enic, "failed to allocate vnic_intr, aborting.\n");
    1837                 :          0 :                 return -1;
    1838                 :            :         }
    1839   [ #  #  #  # ]:          0 :         if (enic->conf_rq_count > 0 && enic->rq == NULL) {
    1840                 :          0 :                 dev_err(enic, "failed to allocate vnic_rq, aborting.\n");
    1841                 :          0 :                 return -1;
    1842                 :            :         }
    1843   [ #  #  #  # ]:          0 :         if (enic->conf_wq_count > 0 && enic->wq == NULL) {
    1844                 :          0 :                 dev_err(enic, "failed to allocate vnic_wq, aborting.\n");
    1845                 :          0 :                 return -1;
    1846                 :            :         }
    1847                 :            : 
    1848                 :          0 :         eth_dev->data->mac_addrs = rte_zmalloc("enic_mac_addr",
    1849                 :            :                                         sizeof(struct rte_ether_addr) *
    1850                 :            :                                         ENIC_UNICAST_PERFECT_FILTERS, 0);
    1851         [ #  # ]:          0 :         if (!eth_dev->data->mac_addrs) {
    1852                 :          0 :                 dev_err(enic, "mac addr storage alloc failed, aborting.\n");
    1853                 :          0 :                 return -1;
    1854                 :            :         }
    1855                 :            : 
    1856                 :            :         /*
    1857                 :            :          * If PF has not assigned any MAC address for VF, generate a random one.
    1858                 :            :          */
    1859         [ #  # ]:          0 :         if (enic_is_vf(enic)) {
    1860                 :            :                 struct rte_ether_addr ea;
    1861                 :            : 
    1862                 :            :                 memcpy(ea.addr_bytes, enic->mac_addr, RTE_ETHER_ADDR_LEN);
    1863                 :            :                 if (!rte_is_valid_assigned_ether_addr(&ea)) {
    1864                 :          0 :                         rte_eth_random_addr(ea.addr_bytes);
    1865                 :          0 :                         ENICPMD_LOG(INFO, "assigned random MAC address " RTE_ETHER_ADDR_PRT_FMT,
    1866                 :            :                                     RTE_ETHER_ADDR_BYTES(&ea));
    1867                 :          0 :                         memcpy(enic->mac_addr, ea.addr_bytes, RTE_ETHER_ADDR_LEN);
    1868                 :            :                 }
    1869                 :            :         }
    1870                 :            : 
    1871                 :          0 :         rte_ether_addr_copy((struct rte_ether_addr *)enic->mac_addr,
    1872                 :          0 :                         eth_dev->data->mac_addrs);
    1873                 :            : 
    1874                 :          0 :         vnic_dev_set_reset_flag(enic->vdev, 0);
    1875                 :            : 
    1876                 :          0 :         LIST_INIT(&enic->flows);
    1877                 :            : 
    1878                 :            :         /* set up link status checking */
    1879                 :          0 :         err = vnic_dev_notify_set(enic->vdev, -1); /* No Intr for notify */
    1880         [ #  # ]:          0 :         if (err) {
    1881                 :          0 :                 dev_err(enic, "failed to enable notify buffer\n");
    1882                 :          0 :                 return err;
    1883                 :            :         }
    1884                 :            : 
    1885                 :          0 :         enic->overlay_offload = false;
    1886                 :            :         /*
    1887                 :            :          * First, explicitly disable overlay offload as the setting is
    1888                 :            :          * sticky, and resetting vNIC may not disable it.
    1889                 :            :          */
    1890                 :          0 :         enic_disable_overlay_offload(enic);
    1891                 :            :         /* Then, enable overlay offload according to vNIC flags */
    1892   [ #  #  #  # ]:          0 :         if (!enic->disable_overlay && (enic->vxlan || enic->geneve)) {
    1893                 :          0 :                 err = enic_enable_overlay_offload(enic);
    1894         [ #  # ]:          0 :                 if (err) {
    1895                 :          0 :                         dev_info(NULL, "failed to enable overlay offload\n");
    1896                 :          0 :                         return err;
    1897                 :            :                 }
    1898                 :            :         }
    1899                 :            :         /*
    1900                 :            :          * Reset the vxlan/geneve port if HW parsing is available. It
    1901                 :            :          * is always enabled regardless of overlay offload
    1902                 :            :          * enable/disable.
    1903                 :            :          */
    1904                 :          0 :         err = enic_reset_overlay_port(enic);
    1905         [ #  # ]:          0 :         if (err)
    1906                 :            :                 return err;
    1907                 :            : 
    1908         [ #  # ]:          0 :         if (enic_fm_init(enic))
    1909                 :          0 :                 dev_warning(enic, "Init of flowman failed.\n");
    1910                 :            :         return 0;
    1911                 :            : }
    1912                 :            : 
    1913                 :          0 : static void lock_devcmd(void *priv)
    1914                 :            : {
    1915                 :            :         struct enic *enic = priv;
    1916                 :            : 
    1917                 :          0 :         rte_spinlock_lock(&enic->devcmd_lock);
    1918                 :          0 : }
    1919                 :            : 
    1920                 :          0 : static void unlock_devcmd(void *priv)
    1921                 :            : {
    1922                 :            :         struct enic *enic = priv;
    1923                 :            : 
    1924                 :          0 :         rte_spinlock_unlock(&enic->devcmd_lock);
    1925                 :          0 : }
    1926                 :            : 
    1927                 :          0 : int enic_probe(struct enic *enic)
    1928                 :            : {
    1929                 :          0 :         struct rte_pci_device *pdev = enic->pdev;
    1930                 :            :         int err = -1;
    1931                 :            : 
    1932                 :          0 :         dev_debug(enic, "Initializing ENIC PMD\n");
    1933                 :            : 
    1934                 :            :         /* if this is a secondary process the hardware is already initialized */
    1935         [ #  # ]:          0 :         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
    1936                 :            :                 return 0;
    1937                 :            : 
    1938                 :          0 :         enic->bar0.vaddr = (void *)pdev->mem_resource[0].addr;
    1939                 :          0 :         enic->bar0.len = pdev->mem_resource[0].len;
    1940                 :            : 
    1941                 :            :         /* Register vNIC device */
    1942                 :          0 :         enic->vdev = vnic_dev_register(NULL, enic, enic->pdev, &enic->bar0, 1);
    1943         [ #  # ]:          0 :         if (!enic->vdev) {
    1944                 :          0 :                 dev_err(enic, "vNIC registration failed, aborting\n");
    1945                 :          0 :                 goto err_out;
    1946                 :            :         }
    1947                 :            : 
    1948                 :          0 :         LIST_INIT(&enic->memzone_list);
    1949                 :            :         rte_spinlock_init(&enic->memzone_list_lock);
    1950                 :            : 
    1951                 :          0 :         vnic_register_cbacks(enic->vdev,
    1952                 :            :                 enic_alloc_consistent,
    1953                 :            :                 enic_free_consistent);
    1954                 :            : 
    1955                 :            :         /*
    1956                 :            :          * Allocate the consistent memory for stats upfront so both primary and
    1957                 :            :          * secondary processes can dump stats.
    1958                 :            :          */
    1959                 :          0 :         err = vnic_dev_alloc_stats_mem(enic->vdev);
    1960         [ #  # ]:          0 :         if (err) {
    1961                 :          0 :                 dev_err(enic, "Failed to allocate cmd memory, aborting\n");
    1962                 :          0 :                 goto err_out_unregister;
    1963                 :            :         }
    1964                 :            :         /* Issue device open to get device in known state */
    1965                 :          0 :         err = enic_dev_open(enic);
    1966         [ #  # ]:          0 :         if (err) {
    1967                 :          0 :                 dev_err(enic, "vNIC dev open failed, aborting\n");
    1968                 :          0 :                 goto err_out_unregister;
    1969                 :            :         }
    1970                 :            : 
    1971                 :            :         /* Set ingress vlan rewrite mode before vnic initialization */
    1972                 :          0 :         dev_debug(enic, "Set ig_vlan_rewrite_mode=%u\n",
    1973                 :            :                   enic->ig_vlan_rewrite_mode);
    1974                 :          0 :         err = vnic_dev_set_ig_vlan_rewrite_mode(enic->vdev,
    1975                 :          0 :                 enic->ig_vlan_rewrite_mode);
    1976         [ #  # ]:          0 :         if (err) {
    1977                 :          0 :                 dev_err(enic,
    1978                 :            :                         "Failed to set ingress vlan rewrite mode, aborting.\n");
    1979                 :          0 :                 goto err_out_dev_close;
    1980                 :            :         }
    1981                 :            : 
    1982                 :            :         /* Issue device init to initialize the vnic-to-switch link.
    1983                 :            :          * We'll start with carrier off and wait for link UP
    1984                 :            :          * notification later to turn on carrier.  We don't need
    1985                 :            :          * to wait here for the vnic-to-switch link initialization
    1986                 :            :          * to complete; link UP notification is the indication that
    1987                 :            :          * the process is complete.
    1988                 :            :          */
    1989                 :            : 
    1990                 :          0 :         err = vnic_dev_init(enic->vdev, 0);
    1991         [ #  # ]:          0 :         if (err) {
    1992                 :          0 :                 dev_err(enic, "vNIC dev init failed, aborting\n");
    1993                 :          0 :                 goto err_out_dev_close;
    1994                 :            :         }
    1995                 :            : 
    1996                 :          0 :         err = enic_dev_init(enic);
    1997         [ #  # ]:          0 :         if (err) {
    1998                 :          0 :                 dev_err(enic, "Device initialization failed, aborting\n");
    1999                 :          0 :                 goto err_out_dev_close;
    2000                 :            :         }
    2001                 :            : 
    2002                 :            :         /* Use a PF spinlock to serialize devcmd from PF and VF representors */
    2003         [ #  # ]:          0 :         if (enic->switchdev_mode) {
    2004                 :            :                 rte_spinlock_init(&enic->devcmd_lock);
    2005                 :          0 :                 vnic_register_lock(enic->vdev, lock_devcmd, unlock_devcmd);
    2006                 :            :         }
    2007                 :            :         return 0;
    2008                 :            : 
    2009                 :          0 : err_out_dev_close:
    2010                 :          0 :         vnic_dev_close(enic->vdev);
    2011                 :          0 : err_out_unregister:
    2012                 :          0 :         vnic_dev_unregister(enic->vdev);
    2013                 :            : err_out:
    2014                 :            :         return err;
    2015                 :            : }
    2016                 :            : 
    2017                 :          0 : void enic_remove(struct enic *enic)
    2018                 :            : {
    2019                 :          0 :         enic_dev_deinit(enic);
    2020                 :          0 :         vnic_dev_close(enic->vdev);
    2021                 :          0 :         vnic_dev_unregister(enic->vdev);
    2022                 :          0 : }

Generated by: LCOV version 1.14