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

Generated by: LCOV version 1.14