Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
0002 /* QLogic qede NIC Driver
0003  * Copyright (c) 2015-2017  QLogic Corporation
0004  * Copyright (c) 2019-2020 Marvell International Ltd.
0005  */
0006 
0007 #include <linux/netdevice.h>
0008 #include <linux/etherdevice.h>
0009 #include <linux/skbuff.h>
0010 #include <linux/bpf_trace.h>
0011 #include <net/udp_tunnel.h>
0012 #include <linux/ip.h>
0013 #include <net/gro.h>
0014 #include <net/ipv6.h>
0015 #include <net/tcp.h>
0016 #include <linux/if_ether.h>
0017 #include <linux/if_vlan.h>
0018 #include <net/ip6_checksum.h>
0019 #include "qede_ptp.h"
0020 
0021 #include <linux/qed/qed_if.h>
0022 #include "qede.h"
0023 /*********************************
0024  * Content also used by slowpath *
0025  *********************************/
0026 
0027 int qede_alloc_rx_buffer(struct qede_rx_queue *rxq, bool allow_lazy)
0028 {
0029     struct sw_rx_data *sw_rx_data;
0030     struct eth_rx_bd *rx_bd;
0031     dma_addr_t mapping;
0032     struct page *data;
0033 
0034     /* In case lazy-allocation is allowed, postpone allocation until the
0035      * end of the NAPI run. We'd still need to make sure the Rx ring has
0036      * sufficient buffers to guarantee an additional Rx interrupt.
0037      */
0038     if (allow_lazy && likely(rxq->filled_buffers > 12)) {
0039         rxq->filled_buffers--;
0040         return 0;
0041     }
0042 
0043     data = alloc_pages(GFP_ATOMIC, 0);
0044     if (unlikely(!data))
0045         return -ENOMEM;
0046 
0047     /* Map the entire page as it would be used
0048      * for multiple RX buffer segment size mapping.
0049      */
0050     mapping = dma_map_page(rxq->dev, data, 0,
0051                    PAGE_SIZE, rxq->data_direction);
0052     if (unlikely(dma_mapping_error(rxq->dev, mapping))) {
0053         __free_page(data);
0054         return -ENOMEM;
0055     }
0056 
0057     sw_rx_data = &rxq->sw_rx_ring[rxq->sw_rx_prod & NUM_RX_BDS_MAX];
0058     sw_rx_data->page_offset = 0;
0059     sw_rx_data->data = data;
0060     sw_rx_data->mapping = mapping;
0061 
0062     /* Advance PROD and get BD pointer */
0063     rx_bd = (struct eth_rx_bd *)qed_chain_produce(&rxq->rx_bd_ring);
0064     WARN_ON(!rx_bd);
0065     rx_bd->addr.hi = cpu_to_le32(upper_32_bits(mapping));
0066     rx_bd->addr.lo = cpu_to_le32(lower_32_bits(mapping) +
0067                      rxq->rx_headroom);
0068 
0069     rxq->sw_rx_prod++;
0070     rxq->filled_buffers++;
0071 
0072     return 0;
0073 }
0074 
0075 /* Unmap the data and free skb */
0076 int qede_free_tx_pkt(struct qede_dev *edev, struct qede_tx_queue *txq, int *len)
0077 {
0078     u16 idx = txq->sw_tx_cons;
0079     struct sk_buff *skb = txq->sw_tx_ring.skbs[idx].skb;
0080     struct eth_tx_1st_bd *first_bd;
0081     struct eth_tx_bd *tx_data_bd;
0082     int bds_consumed = 0;
0083     int nbds;
0084     bool data_split = txq->sw_tx_ring.skbs[idx].flags & QEDE_TSO_SPLIT_BD;
0085     int i, split_bd_len = 0;
0086 
0087     if (unlikely(!skb)) {
0088         DP_ERR(edev,
0089                "skb is null for txq idx=%d txq->sw_tx_cons=%d txq->sw_tx_prod=%d\n",
0090                idx, txq->sw_tx_cons, txq->sw_tx_prod);
0091         return -1;
0092     }
0093 
0094     *len = skb->len;
0095 
0096     first_bd = (struct eth_tx_1st_bd *)qed_chain_consume(&txq->tx_pbl);
0097 
0098     bds_consumed++;
0099 
0100     nbds = first_bd->data.nbds;
0101 
0102     if (data_split) {
0103         struct eth_tx_bd *split = (struct eth_tx_bd *)
0104             qed_chain_consume(&txq->tx_pbl);
0105         split_bd_len = BD_UNMAP_LEN(split);
0106         bds_consumed++;
0107     }
0108     dma_unmap_single(&edev->pdev->dev, BD_UNMAP_ADDR(first_bd),
0109              BD_UNMAP_LEN(first_bd) + split_bd_len, DMA_TO_DEVICE);
0110 
0111     /* Unmap the data of the skb frags */
0112     for (i = 0; i < skb_shinfo(skb)->nr_frags; i++, bds_consumed++) {
0113         tx_data_bd = (struct eth_tx_bd *)
0114             qed_chain_consume(&txq->tx_pbl);
0115         dma_unmap_page(&edev->pdev->dev, BD_UNMAP_ADDR(tx_data_bd),
0116                    BD_UNMAP_LEN(tx_data_bd), DMA_TO_DEVICE);
0117     }
0118 
0119     while (bds_consumed++ < nbds)
0120         qed_chain_consume(&txq->tx_pbl);
0121 
0122     /* Free skb */
0123     dev_kfree_skb_any(skb);
0124     txq->sw_tx_ring.skbs[idx].skb = NULL;
0125     txq->sw_tx_ring.skbs[idx].flags = 0;
0126 
0127     return 0;
0128 }
0129 
0130 /* Unmap the data and free skb when mapping failed during start_xmit */
0131 static void qede_free_failed_tx_pkt(struct qede_tx_queue *txq,
0132                     struct eth_tx_1st_bd *first_bd,
0133                     int nbd, bool data_split)
0134 {
0135     u16 idx = txq->sw_tx_prod;
0136     struct sk_buff *skb = txq->sw_tx_ring.skbs[idx].skb;
0137     struct eth_tx_bd *tx_data_bd;
0138     int i, split_bd_len = 0;
0139 
0140     /* Return prod to its position before this skb was handled */
0141     qed_chain_set_prod(&txq->tx_pbl,
0142                le16_to_cpu(txq->tx_db.data.bd_prod), first_bd);
0143 
0144     first_bd = (struct eth_tx_1st_bd *)qed_chain_produce(&txq->tx_pbl);
0145 
0146     if (data_split) {
0147         struct eth_tx_bd *split = (struct eth_tx_bd *)
0148                       qed_chain_produce(&txq->tx_pbl);
0149         split_bd_len = BD_UNMAP_LEN(split);
0150         nbd--;
0151     }
0152 
0153     dma_unmap_single(txq->dev, BD_UNMAP_ADDR(first_bd),
0154              BD_UNMAP_LEN(first_bd) + split_bd_len, DMA_TO_DEVICE);
0155 
0156     /* Unmap the data of the skb frags */
0157     for (i = 0; i < nbd; i++) {
0158         tx_data_bd = (struct eth_tx_bd *)
0159             qed_chain_produce(&txq->tx_pbl);
0160         if (tx_data_bd->nbytes)
0161             dma_unmap_page(txq->dev,
0162                        BD_UNMAP_ADDR(tx_data_bd),
0163                        BD_UNMAP_LEN(tx_data_bd), DMA_TO_DEVICE);
0164     }
0165 
0166     /* Return again prod to its position before this skb was handled */
0167     qed_chain_set_prod(&txq->tx_pbl,
0168                le16_to_cpu(txq->tx_db.data.bd_prod), first_bd);
0169 
0170     /* Free skb */
0171     dev_kfree_skb_any(skb);
0172     txq->sw_tx_ring.skbs[idx].skb = NULL;
0173     txq->sw_tx_ring.skbs[idx].flags = 0;
0174 }
0175 
0176 static u32 qede_xmit_type(struct sk_buff *skb, int *ipv6_ext)
0177 {
0178     u32 rc = XMIT_L4_CSUM;
0179     __be16 l3_proto;
0180 
0181     if (skb->ip_summed != CHECKSUM_PARTIAL)
0182         return XMIT_PLAIN;
0183 
0184     l3_proto = vlan_get_protocol(skb);
0185     if (l3_proto == htons(ETH_P_IPV6) &&
0186         (ipv6_hdr(skb)->nexthdr == NEXTHDR_IPV6))
0187         *ipv6_ext = 1;
0188 
0189     if (skb->encapsulation) {
0190         rc |= XMIT_ENC;
0191         if (skb_is_gso(skb)) {
0192             unsigned short gso_type = skb_shinfo(skb)->gso_type;
0193 
0194             if ((gso_type & SKB_GSO_UDP_TUNNEL_CSUM) ||
0195                 (gso_type & SKB_GSO_GRE_CSUM))
0196                 rc |= XMIT_ENC_GSO_L4_CSUM;
0197 
0198             rc |= XMIT_LSO;
0199             return rc;
0200         }
0201     }
0202 
0203     if (skb_is_gso(skb))
0204         rc |= XMIT_LSO;
0205 
0206     return rc;
0207 }
0208 
0209 static void qede_set_params_for_ipv6_ext(struct sk_buff *skb,
0210                      struct eth_tx_2nd_bd *second_bd,
0211                      struct eth_tx_3rd_bd *third_bd)
0212 {
0213     u8 l4_proto;
0214     u16 bd2_bits1 = 0, bd2_bits2 = 0;
0215 
0216     bd2_bits1 |= (1 << ETH_TX_DATA_2ND_BD_IPV6_EXT_SHIFT);
0217 
0218     bd2_bits2 |= ((((u8 *)skb_transport_header(skb) - skb->data) >> 1) &
0219              ETH_TX_DATA_2ND_BD_L4_HDR_START_OFFSET_W_MASK)
0220             << ETH_TX_DATA_2ND_BD_L4_HDR_START_OFFSET_W_SHIFT;
0221 
0222     bd2_bits1 |= (ETH_L4_PSEUDO_CSUM_CORRECT_LENGTH <<
0223               ETH_TX_DATA_2ND_BD_L4_PSEUDO_CSUM_MODE_SHIFT);
0224 
0225     if (vlan_get_protocol(skb) == htons(ETH_P_IPV6))
0226         l4_proto = ipv6_hdr(skb)->nexthdr;
0227     else
0228         l4_proto = ip_hdr(skb)->protocol;
0229 
0230     if (l4_proto == IPPROTO_UDP)
0231         bd2_bits1 |= 1 << ETH_TX_DATA_2ND_BD_L4_UDP_SHIFT;
0232 
0233     if (third_bd)
0234         third_bd->data.bitfields |=
0235             cpu_to_le16(((tcp_hdrlen(skb) / 4) &
0236                 ETH_TX_DATA_3RD_BD_TCP_HDR_LEN_DW_MASK) <<
0237                 ETH_TX_DATA_3RD_BD_TCP_HDR_LEN_DW_SHIFT);
0238 
0239     second_bd->data.bitfields1 = cpu_to_le16(bd2_bits1);
0240     second_bd->data.bitfields2 = cpu_to_le16(bd2_bits2);
0241 }
0242 
0243 static int map_frag_to_bd(struct qede_tx_queue *txq,
0244               skb_frag_t *frag, struct eth_tx_bd *bd)
0245 {
0246     dma_addr_t mapping;
0247 
0248     /* Map skb non-linear frag data for DMA */
0249     mapping = skb_frag_dma_map(txq->dev, frag, 0,
0250                    skb_frag_size(frag), DMA_TO_DEVICE);
0251     if (unlikely(dma_mapping_error(txq->dev, mapping)))
0252         return -ENOMEM;
0253 
0254     /* Setup the data pointer of the frag data */
0255     BD_SET_UNMAP_ADDR_LEN(bd, mapping, skb_frag_size(frag));
0256 
0257     return 0;
0258 }
0259 
0260 static u16 qede_get_skb_hlen(struct sk_buff *skb, bool is_encap_pkt)
0261 {
0262     if (is_encap_pkt)
0263         return skb_inner_tcp_all_headers(skb);
0264 
0265     return skb_tcp_all_headers(skb);
0266 }
0267 
0268 /* +2 for 1st BD for headers and 2nd BD for headlen (if required) */
0269 #if ((MAX_SKB_FRAGS + 2) > ETH_TX_MAX_BDS_PER_NON_LSO_PACKET)
0270 static bool qede_pkt_req_lin(struct sk_buff *skb, u8 xmit_type)
0271 {
0272     int allowed_frags = ETH_TX_MAX_BDS_PER_NON_LSO_PACKET - 1;
0273 
0274     if (xmit_type & XMIT_LSO) {
0275         int hlen;
0276 
0277         hlen = qede_get_skb_hlen(skb, xmit_type & XMIT_ENC);
0278 
0279         /* linear payload would require its own BD */
0280         if (skb_headlen(skb) > hlen)
0281             allowed_frags--;
0282     }
0283 
0284     return (skb_shinfo(skb)->nr_frags > allowed_frags);
0285 }
0286 #endif
0287 
0288 static inline void qede_update_tx_producer(struct qede_tx_queue *txq)
0289 {
0290     /* wmb makes sure that the BDs data is updated before updating the
0291      * producer, otherwise FW may read old data from the BDs.
0292      */
0293     wmb();
0294     barrier();
0295     writel(txq->tx_db.raw, txq->doorbell_addr);
0296 
0297     /* Fence required to flush the write combined buffer, since another
0298      * CPU may write to the same doorbell address and data may be lost
0299      * due to relaxed order nature of write combined bar.
0300      */
0301     wmb();
0302 }
0303 
0304 static int qede_xdp_xmit(struct qede_tx_queue *txq, dma_addr_t dma, u16 pad,
0305              u16 len, struct page *page, struct xdp_frame *xdpf)
0306 {
0307     struct eth_tx_1st_bd *bd;
0308     struct sw_tx_xdp *xdp;
0309     u16 val;
0310 
0311     if (unlikely(qed_chain_get_elem_used(&txq->tx_pbl) >=
0312              txq->num_tx_buffers)) {
0313         txq->stopped_cnt++;
0314         return -ENOMEM;
0315     }
0316 
0317     bd = qed_chain_produce(&txq->tx_pbl);
0318     bd->data.nbds = 1;
0319     bd->data.bd_flags.bitfields = BIT(ETH_TX_1ST_BD_FLAGS_START_BD_SHIFT);
0320 
0321     val = (len & ETH_TX_DATA_1ST_BD_PKT_LEN_MASK) <<
0322            ETH_TX_DATA_1ST_BD_PKT_LEN_SHIFT;
0323 
0324     bd->data.bitfields = cpu_to_le16(val);
0325 
0326     /* We can safely ignore the offset, as it's 0 for XDP */
0327     BD_SET_UNMAP_ADDR_LEN(bd, dma + pad, len);
0328 
0329     xdp = txq->sw_tx_ring.xdp + txq->sw_tx_prod;
0330     xdp->mapping = dma;
0331     xdp->page = page;
0332     xdp->xdpf = xdpf;
0333 
0334     txq->sw_tx_prod = (txq->sw_tx_prod + 1) % txq->num_tx_buffers;
0335 
0336     return 0;
0337 }
0338 
0339 int qede_xdp_transmit(struct net_device *dev, int n_frames,
0340               struct xdp_frame **frames, u32 flags)
0341 {
0342     struct qede_dev *edev = netdev_priv(dev);
0343     struct device *dmadev = &edev->pdev->dev;
0344     struct qede_tx_queue *xdp_tx;
0345     struct xdp_frame *xdpf;
0346     dma_addr_t mapping;
0347     int i, nxmit = 0;
0348     u16 xdp_prod;
0349 
0350     if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
0351         return -EINVAL;
0352 
0353     if (unlikely(!netif_running(dev)))
0354         return -ENETDOWN;
0355 
0356     i = smp_processor_id() % edev->total_xdp_queues;
0357     xdp_tx = edev->fp_array[i].xdp_tx;
0358 
0359     spin_lock(&xdp_tx->xdp_tx_lock);
0360 
0361     for (i = 0; i < n_frames; i++) {
0362         xdpf = frames[i];
0363 
0364         mapping = dma_map_single(dmadev, xdpf->data, xdpf->len,
0365                      DMA_TO_DEVICE);
0366         if (unlikely(dma_mapping_error(dmadev, mapping)))
0367             break;
0368 
0369         if (unlikely(qede_xdp_xmit(xdp_tx, mapping, 0, xdpf->len,
0370                        NULL, xdpf)))
0371             break;
0372         nxmit++;
0373     }
0374 
0375     if (flags & XDP_XMIT_FLUSH) {
0376         xdp_prod = qed_chain_get_prod_idx(&xdp_tx->tx_pbl);
0377 
0378         xdp_tx->tx_db.data.bd_prod = cpu_to_le16(xdp_prod);
0379         qede_update_tx_producer(xdp_tx);
0380     }
0381 
0382     spin_unlock(&xdp_tx->xdp_tx_lock);
0383 
0384     return nxmit;
0385 }
0386 
0387 int qede_txq_has_work(struct qede_tx_queue *txq)
0388 {
0389     u16 hw_bd_cons;
0390 
0391     /* Tell compiler that consumer and producer can change */
0392     barrier();
0393     hw_bd_cons = le16_to_cpu(*txq->hw_cons_ptr);
0394     if (qed_chain_get_cons_idx(&txq->tx_pbl) == hw_bd_cons + 1)
0395         return 0;
0396 
0397     return hw_bd_cons != qed_chain_get_cons_idx(&txq->tx_pbl);
0398 }
0399 
0400 static void qede_xdp_tx_int(struct qede_dev *edev, struct qede_tx_queue *txq)
0401 {
0402     struct sw_tx_xdp *xdp_info, *xdp_arr = txq->sw_tx_ring.xdp;
0403     struct device *dev = &edev->pdev->dev;
0404     struct xdp_frame *xdpf;
0405     u16 hw_bd_cons;
0406 
0407     hw_bd_cons = le16_to_cpu(*txq->hw_cons_ptr);
0408     barrier();
0409 
0410     while (hw_bd_cons != qed_chain_get_cons_idx(&txq->tx_pbl)) {
0411         xdp_info = xdp_arr + txq->sw_tx_cons;
0412         xdpf = xdp_info->xdpf;
0413 
0414         if (xdpf) {
0415             dma_unmap_single(dev, xdp_info->mapping, xdpf->len,
0416                      DMA_TO_DEVICE);
0417             xdp_return_frame(xdpf);
0418 
0419             xdp_info->xdpf = NULL;
0420         } else {
0421             dma_unmap_page(dev, xdp_info->mapping, PAGE_SIZE,
0422                        DMA_BIDIRECTIONAL);
0423             __free_page(xdp_info->page);
0424         }
0425 
0426         qed_chain_consume(&txq->tx_pbl);
0427         txq->sw_tx_cons = (txq->sw_tx_cons + 1) % txq->num_tx_buffers;
0428         txq->xmit_pkts++;
0429     }
0430 }
0431 
0432 static int qede_tx_int(struct qede_dev *edev, struct qede_tx_queue *txq)
0433 {
0434     unsigned int pkts_compl = 0, bytes_compl = 0;
0435     struct netdev_queue *netdev_txq;
0436     u16 hw_bd_cons;
0437     int rc;
0438 
0439     netdev_txq = netdev_get_tx_queue(edev->ndev, txq->ndev_txq_id);
0440 
0441     hw_bd_cons = le16_to_cpu(*txq->hw_cons_ptr);
0442     barrier();
0443 
0444     while (hw_bd_cons != qed_chain_get_cons_idx(&txq->tx_pbl)) {
0445         int len = 0;
0446 
0447         rc = qede_free_tx_pkt(edev, txq, &len);
0448         if (rc) {
0449             DP_NOTICE(edev, "hw_bd_cons = %d, chain_cons=%d\n",
0450                   hw_bd_cons,
0451                   qed_chain_get_cons_idx(&txq->tx_pbl));
0452             break;
0453         }
0454 
0455         bytes_compl += len;
0456         pkts_compl++;
0457         txq->sw_tx_cons = (txq->sw_tx_cons + 1) % txq->num_tx_buffers;
0458         txq->xmit_pkts++;
0459     }
0460 
0461     netdev_tx_completed_queue(netdev_txq, pkts_compl, bytes_compl);
0462 
0463     /* Need to make the tx_bd_cons update visible to start_xmit()
0464      * before checking for netif_tx_queue_stopped().  Without the
0465      * memory barrier, there is a small possibility that
0466      * start_xmit() will miss it and cause the queue to be stopped
0467      * forever.
0468      * On the other hand we need an rmb() here to ensure the proper
0469      * ordering of bit testing in the following
0470      * netif_tx_queue_stopped(txq) call.
0471      */
0472     smp_mb();
0473 
0474     if (unlikely(netif_tx_queue_stopped(netdev_txq))) {
0475         /* Taking tx_lock is needed to prevent reenabling the queue
0476          * while it's empty. This could have happen if rx_action() gets
0477          * suspended in qede_tx_int() after the condition before
0478          * netif_tx_wake_queue(), while tx_action (qede_start_xmit()):
0479          *
0480          * stops the queue->sees fresh tx_bd_cons->releases the queue->
0481          * sends some packets consuming the whole queue again->
0482          * stops the queue
0483          */
0484 
0485         __netif_tx_lock(netdev_txq, smp_processor_id());
0486 
0487         if ((netif_tx_queue_stopped(netdev_txq)) &&
0488             (edev->state == QEDE_STATE_OPEN) &&
0489             (qed_chain_get_elem_left(&txq->tx_pbl)
0490               >= (MAX_SKB_FRAGS + 1))) {
0491             netif_tx_wake_queue(netdev_txq);
0492             DP_VERBOSE(edev, NETIF_MSG_TX_DONE,
0493                    "Wake queue was called\n");
0494         }
0495 
0496         __netif_tx_unlock(netdev_txq);
0497     }
0498 
0499     return 0;
0500 }
0501 
0502 bool qede_has_rx_work(struct qede_rx_queue *rxq)
0503 {
0504     u16 hw_comp_cons, sw_comp_cons;
0505 
0506     /* Tell compiler that status block fields can change */
0507     barrier();
0508 
0509     hw_comp_cons = le16_to_cpu(*rxq->hw_cons_ptr);
0510     sw_comp_cons = qed_chain_get_cons_idx(&rxq->rx_comp_ring);
0511 
0512     return hw_comp_cons != sw_comp_cons;
0513 }
0514 
0515 static inline void qede_rx_bd_ring_consume(struct qede_rx_queue *rxq)
0516 {
0517     qed_chain_consume(&rxq->rx_bd_ring);
0518     rxq->sw_rx_cons++;
0519 }
0520 
0521 /* This function reuses the buffer(from an offset) from
0522  * consumer index to producer index in the bd ring
0523  */
0524 static inline void qede_reuse_page(struct qede_rx_queue *rxq,
0525                    struct sw_rx_data *curr_cons)
0526 {
0527     struct eth_rx_bd *rx_bd_prod = qed_chain_produce(&rxq->rx_bd_ring);
0528     struct sw_rx_data *curr_prod;
0529     dma_addr_t new_mapping;
0530 
0531     curr_prod = &rxq->sw_rx_ring[rxq->sw_rx_prod & NUM_RX_BDS_MAX];
0532     *curr_prod = *curr_cons;
0533 
0534     new_mapping = curr_prod->mapping + curr_prod->page_offset;
0535 
0536     rx_bd_prod->addr.hi = cpu_to_le32(upper_32_bits(new_mapping));
0537     rx_bd_prod->addr.lo = cpu_to_le32(lower_32_bits(new_mapping) +
0538                       rxq->rx_headroom);
0539 
0540     rxq->sw_rx_prod++;
0541     curr_cons->data = NULL;
0542 }
0543 
0544 /* In case of allocation failures reuse buffers
0545  * from consumer index to produce buffers for firmware
0546  */
0547 void qede_recycle_rx_bd_ring(struct qede_rx_queue *rxq, u8 count)
0548 {
0549     struct sw_rx_data *curr_cons;
0550 
0551     for (; count > 0; count--) {
0552         curr_cons = &rxq->sw_rx_ring[rxq->sw_rx_cons & NUM_RX_BDS_MAX];
0553         qede_reuse_page(rxq, curr_cons);
0554         qede_rx_bd_ring_consume(rxq);
0555     }
0556 }
0557 
0558 static inline int qede_realloc_rx_buffer(struct qede_rx_queue *rxq,
0559                      struct sw_rx_data *curr_cons)
0560 {
0561     /* Move to the next segment in the page */
0562     curr_cons->page_offset += rxq->rx_buf_seg_size;
0563 
0564     if (curr_cons->page_offset == PAGE_SIZE) {
0565         if (unlikely(qede_alloc_rx_buffer(rxq, true))) {
0566             /* Since we failed to allocate new buffer
0567              * current buffer can be used again.
0568              */
0569             curr_cons->page_offset -= rxq->rx_buf_seg_size;
0570 
0571             return -ENOMEM;
0572         }
0573 
0574         dma_unmap_page(rxq->dev, curr_cons->mapping,
0575                    PAGE_SIZE, rxq->data_direction);
0576     } else {
0577         /* Increment refcount of the page as we don't want
0578          * network stack to take the ownership of the page
0579          * which can be recycled multiple times by the driver.
0580          */
0581         page_ref_inc(curr_cons->data);
0582         qede_reuse_page(rxq, curr_cons);
0583     }
0584 
0585     return 0;
0586 }
0587 
0588 void qede_update_rx_prod(struct qede_dev *edev, struct qede_rx_queue *rxq)
0589 {
0590     u16 bd_prod = qed_chain_get_prod_idx(&rxq->rx_bd_ring);
0591     u16 cqe_prod = qed_chain_get_prod_idx(&rxq->rx_comp_ring);
0592     struct eth_rx_prod_data rx_prods = {0};
0593 
0594     /* Update producers */
0595     rx_prods.bd_prod = cpu_to_le16(bd_prod);
0596     rx_prods.cqe_prod = cpu_to_le16(cqe_prod);
0597 
0598     /* Make sure that the BD and SGE data is updated before updating the
0599      * producers since FW might read the BD/SGE right after the producer
0600      * is updated.
0601      */
0602     wmb();
0603 
0604     internal_ram_wr(rxq->hw_rxq_prod_addr, sizeof(rx_prods),
0605             (u32 *)&rx_prods);
0606 }
0607 
0608 static void qede_get_rxhash(struct sk_buff *skb, u8 bitfields, __le32 rss_hash)
0609 {
0610     enum pkt_hash_types hash_type = PKT_HASH_TYPE_NONE;
0611     enum rss_hash_type htype;
0612     u32 hash = 0;
0613 
0614     htype = GET_FIELD(bitfields, ETH_FAST_PATH_RX_REG_CQE_RSS_HASH_TYPE);
0615     if (htype) {
0616         hash_type = ((htype == RSS_HASH_TYPE_IPV4) ||
0617                  (htype == RSS_HASH_TYPE_IPV6)) ?
0618                 PKT_HASH_TYPE_L3 : PKT_HASH_TYPE_L4;
0619         hash = le32_to_cpu(rss_hash);
0620     }
0621     skb_set_hash(skb, hash, hash_type);
0622 }
0623 
0624 static void qede_set_skb_csum(struct sk_buff *skb, u8 csum_flag)
0625 {
0626     skb_checksum_none_assert(skb);
0627 
0628     if (csum_flag & QEDE_CSUM_UNNECESSARY)
0629         skb->ip_summed = CHECKSUM_UNNECESSARY;
0630 
0631     if (csum_flag & QEDE_TUNN_CSUM_UNNECESSARY) {
0632         skb->csum_level = 1;
0633         skb->encapsulation = 1;
0634     }
0635 }
0636 
0637 static inline void qede_skb_receive(struct qede_dev *edev,
0638                     struct qede_fastpath *fp,
0639                     struct qede_rx_queue *rxq,
0640                     struct sk_buff *skb, u16 vlan_tag)
0641 {
0642     if (vlan_tag)
0643         __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
0644 
0645     napi_gro_receive(&fp->napi, skb);
0646 }
0647 
0648 static void qede_set_gro_params(struct qede_dev *edev,
0649                 struct sk_buff *skb,
0650                 struct eth_fast_path_rx_tpa_start_cqe *cqe)
0651 {
0652     u16 parsing_flags = le16_to_cpu(cqe->pars_flags.flags);
0653 
0654     if (((parsing_flags >> PARSING_AND_ERR_FLAGS_L3TYPE_SHIFT) &
0655         PARSING_AND_ERR_FLAGS_L3TYPE_MASK) == 2)
0656         skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
0657     else
0658         skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
0659 
0660     skb_shinfo(skb)->gso_size = __le16_to_cpu(cqe->len_on_first_bd) -
0661                     cqe->header_len;
0662 }
0663 
0664 static int qede_fill_frag_skb(struct qede_dev *edev,
0665                   struct qede_rx_queue *rxq,
0666                   u8 tpa_agg_index, u16 len_on_bd)
0667 {
0668     struct sw_rx_data *current_bd = &rxq->sw_rx_ring[rxq->sw_rx_cons &
0669                              NUM_RX_BDS_MAX];
0670     struct qede_agg_info *tpa_info = &rxq->tpa_info[tpa_agg_index];
0671     struct sk_buff *skb = tpa_info->skb;
0672 
0673     if (unlikely(tpa_info->state != QEDE_AGG_STATE_START))
0674         goto out;
0675 
0676     /* Add one frag and update the appropriate fields in the skb */
0677     skb_fill_page_desc(skb, tpa_info->frag_id++,
0678                current_bd->data,
0679                current_bd->page_offset + rxq->rx_headroom,
0680                len_on_bd);
0681 
0682     if (unlikely(qede_realloc_rx_buffer(rxq, current_bd))) {
0683         /* Incr page ref count to reuse on allocation failure
0684          * so that it doesn't get freed while freeing SKB.
0685          */
0686         page_ref_inc(current_bd->data);
0687         goto out;
0688     }
0689 
0690     qede_rx_bd_ring_consume(rxq);
0691 
0692     skb->data_len += len_on_bd;
0693     skb->truesize += rxq->rx_buf_seg_size;
0694     skb->len += len_on_bd;
0695 
0696     return 0;
0697 
0698 out:
0699     tpa_info->state = QEDE_AGG_STATE_ERROR;
0700     qede_recycle_rx_bd_ring(rxq, 1);
0701 
0702     return -ENOMEM;
0703 }
0704 
0705 static bool qede_tunn_exist(u16 flag)
0706 {
0707     return !!(flag & (PARSING_AND_ERR_FLAGS_TUNNELEXIST_MASK <<
0708               PARSING_AND_ERR_FLAGS_TUNNELEXIST_SHIFT));
0709 }
0710 
0711 static u8 qede_check_tunn_csum(u16 flag)
0712 {
0713     u16 csum_flag = 0;
0714     u8 tcsum = 0;
0715 
0716     if (flag & (PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMWASCALCULATED_MASK <<
0717             PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMWASCALCULATED_SHIFT))
0718         csum_flag |= PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMERROR_MASK <<
0719                  PARSING_AND_ERR_FLAGS_TUNNELL4CHKSMERROR_SHIFT;
0720 
0721     if (flag & (PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_MASK <<
0722             PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_SHIFT)) {
0723         csum_flag |= PARSING_AND_ERR_FLAGS_L4CHKSMERROR_MASK <<
0724                  PARSING_AND_ERR_FLAGS_L4CHKSMERROR_SHIFT;
0725         tcsum = QEDE_TUNN_CSUM_UNNECESSARY;
0726     }
0727 
0728     csum_flag |= PARSING_AND_ERR_FLAGS_TUNNELIPHDRERROR_MASK <<
0729              PARSING_AND_ERR_FLAGS_TUNNELIPHDRERROR_SHIFT |
0730              PARSING_AND_ERR_FLAGS_IPHDRERROR_MASK <<
0731              PARSING_AND_ERR_FLAGS_IPHDRERROR_SHIFT;
0732 
0733     if (csum_flag & flag)
0734         return QEDE_CSUM_ERROR;
0735 
0736     return QEDE_CSUM_UNNECESSARY | tcsum;
0737 }
0738 
0739 static inline struct sk_buff *
0740 qede_build_skb(struct qede_rx_queue *rxq,
0741            struct sw_rx_data *bd, u16 len, u16 pad)
0742 {
0743     struct sk_buff *skb;
0744     void *buf;
0745 
0746     buf = page_address(bd->data) + bd->page_offset;
0747     skb = build_skb(buf, rxq->rx_buf_seg_size);
0748 
0749     if (unlikely(!skb))
0750         return NULL;
0751 
0752     skb_reserve(skb, pad);
0753     skb_put(skb, len);
0754 
0755     return skb;
0756 }
0757 
0758 static struct sk_buff *
0759 qede_tpa_rx_build_skb(struct qede_dev *edev,
0760               struct qede_rx_queue *rxq,
0761               struct sw_rx_data *bd, u16 len, u16 pad,
0762               bool alloc_skb)
0763 {
0764     struct sk_buff *skb;
0765 
0766     skb = qede_build_skb(rxq, bd, len, pad);
0767     bd->page_offset += rxq->rx_buf_seg_size;
0768 
0769     if (bd->page_offset == PAGE_SIZE) {
0770         if (unlikely(qede_alloc_rx_buffer(rxq, true))) {
0771             DP_NOTICE(edev,
0772                   "Failed to allocate RX buffer for tpa start\n");
0773             bd->page_offset -= rxq->rx_buf_seg_size;
0774             page_ref_inc(bd->data);
0775             dev_kfree_skb_any(skb);
0776             return NULL;
0777         }
0778     } else {
0779         page_ref_inc(bd->data);
0780         qede_reuse_page(rxq, bd);
0781     }
0782 
0783     /* We've consumed the first BD and prepared an SKB */
0784     qede_rx_bd_ring_consume(rxq);
0785 
0786     return skb;
0787 }
0788 
0789 static struct sk_buff *
0790 qede_rx_build_skb(struct qede_dev *edev,
0791           struct qede_rx_queue *rxq,
0792           struct sw_rx_data *bd, u16 len, u16 pad)
0793 {
0794     struct sk_buff *skb = NULL;
0795 
0796     /* For smaller frames still need to allocate skb, memcpy
0797      * data and benefit in reusing the page segment instead of
0798      * un-mapping it.
0799      */
0800     if ((len + pad <= edev->rx_copybreak)) {
0801         unsigned int offset = bd->page_offset + pad;
0802 
0803         skb = netdev_alloc_skb(edev->ndev, QEDE_RX_HDR_SIZE);
0804         if (unlikely(!skb))
0805             return NULL;
0806 
0807         skb_reserve(skb, pad);
0808         skb_put_data(skb, page_address(bd->data) + offset, len);
0809         qede_reuse_page(rxq, bd);
0810         goto out;
0811     }
0812 
0813     skb = qede_build_skb(rxq, bd, len, pad);
0814 
0815     if (unlikely(qede_realloc_rx_buffer(rxq, bd))) {
0816         /* Incr page ref count to reuse on allocation failure so
0817          * that it doesn't get freed while freeing SKB [as its
0818          * already mapped there].
0819          */
0820         page_ref_inc(bd->data);
0821         dev_kfree_skb_any(skb);
0822         return NULL;
0823     }
0824 out:
0825     /* We've consumed the first BD and prepared an SKB */
0826     qede_rx_bd_ring_consume(rxq);
0827 
0828     return skb;
0829 }
0830 
0831 static void qede_tpa_start(struct qede_dev *edev,
0832                struct qede_rx_queue *rxq,
0833                struct eth_fast_path_rx_tpa_start_cqe *cqe)
0834 {
0835     struct qede_agg_info *tpa_info = &rxq->tpa_info[cqe->tpa_agg_index];
0836     struct sw_rx_data *sw_rx_data_cons;
0837     u16 pad;
0838 
0839     sw_rx_data_cons = &rxq->sw_rx_ring[rxq->sw_rx_cons & NUM_RX_BDS_MAX];
0840     pad = cqe->placement_offset + rxq->rx_headroom;
0841 
0842     tpa_info->skb = qede_tpa_rx_build_skb(edev, rxq, sw_rx_data_cons,
0843                           le16_to_cpu(cqe->len_on_first_bd),
0844                           pad, false);
0845     tpa_info->buffer.page_offset = sw_rx_data_cons->page_offset;
0846     tpa_info->buffer.mapping = sw_rx_data_cons->mapping;
0847 
0848     if (unlikely(!tpa_info->skb)) {
0849         DP_NOTICE(edev, "Failed to allocate SKB for gro\n");
0850 
0851         /* Consume from ring but do not produce since
0852          * this might be used by FW still, it will be re-used
0853          * at TPA end.
0854          */
0855         tpa_info->tpa_start_fail = true;
0856         qede_rx_bd_ring_consume(rxq);
0857         tpa_info->state = QEDE_AGG_STATE_ERROR;
0858         goto cons_buf;
0859     }
0860 
0861     tpa_info->frag_id = 0;
0862     tpa_info->state = QEDE_AGG_STATE_START;
0863 
0864     if ((le16_to_cpu(cqe->pars_flags.flags) >>
0865          PARSING_AND_ERR_FLAGS_TAG8021QEXIST_SHIFT) &
0866         PARSING_AND_ERR_FLAGS_TAG8021QEXIST_MASK)
0867         tpa_info->vlan_tag = le16_to_cpu(cqe->vlan_tag);
0868     else
0869         tpa_info->vlan_tag = 0;
0870 
0871     qede_get_rxhash(tpa_info->skb, cqe->bitfields, cqe->rss_hash);
0872 
0873     /* This is needed in order to enable forwarding support */
0874     qede_set_gro_params(edev, tpa_info->skb, cqe);
0875 
0876 cons_buf: /* We still need to handle bd_len_list to consume buffers */
0877     if (likely(cqe->bw_ext_bd_len_list[0]))
0878         qede_fill_frag_skb(edev, rxq, cqe->tpa_agg_index,
0879                    le16_to_cpu(cqe->bw_ext_bd_len_list[0]));
0880 
0881     if (unlikely(cqe->bw_ext_bd_len_list[1])) {
0882         DP_ERR(edev,
0883                "Unlikely - got a TPA aggregation with more than one bw_ext_bd_len_list entry in the TPA start\n");
0884         tpa_info->state = QEDE_AGG_STATE_ERROR;
0885     }
0886 }
0887 
0888 #ifdef CONFIG_INET
0889 static void qede_gro_ip_csum(struct sk_buff *skb)
0890 {
0891     const struct iphdr *iph = ip_hdr(skb);
0892     struct tcphdr *th;
0893 
0894     skb_set_transport_header(skb, sizeof(struct iphdr));
0895     th = tcp_hdr(skb);
0896 
0897     th->check = ~tcp_v4_check(skb->len - skb_transport_offset(skb),
0898                   iph->saddr, iph->daddr, 0);
0899 
0900     tcp_gro_complete(skb);
0901 }
0902 
0903 static void qede_gro_ipv6_csum(struct sk_buff *skb)
0904 {
0905     struct ipv6hdr *iph = ipv6_hdr(skb);
0906     struct tcphdr *th;
0907 
0908     skb_set_transport_header(skb, sizeof(struct ipv6hdr));
0909     th = tcp_hdr(skb);
0910 
0911     th->check = ~tcp_v6_check(skb->len - skb_transport_offset(skb),
0912                   &iph->saddr, &iph->daddr, 0);
0913     tcp_gro_complete(skb);
0914 }
0915 #endif
0916 
0917 static void qede_gro_receive(struct qede_dev *edev,
0918                  struct qede_fastpath *fp,
0919                  struct sk_buff *skb,
0920                  u16 vlan_tag)
0921 {
0922     /* FW can send a single MTU sized packet from gro flow
0923      * due to aggregation timeout/last segment etc. which
0924      * is not expected to be a gro packet. If a skb has zero
0925      * frags then simply push it in the stack as non gso skb.
0926      */
0927     if (unlikely(!skb->data_len)) {
0928         skb_shinfo(skb)->gso_type = 0;
0929         skb_shinfo(skb)->gso_size = 0;
0930         goto send_skb;
0931     }
0932 
0933 #ifdef CONFIG_INET
0934     if (skb_shinfo(skb)->gso_size) {
0935         skb_reset_network_header(skb);
0936 
0937         switch (skb->protocol) {
0938         case htons(ETH_P_IP):
0939             qede_gro_ip_csum(skb);
0940             break;
0941         case htons(ETH_P_IPV6):
0942             qede_gro_ipv6_csum(skb);
0943             break;
0944         default:
0945             DP_ERR(edev,
0946                    "Error: FW GRO supports only IPv4/IPv6, not 0x%04x\n",
0947                    ntohs(skb->protocol));
0948         }
0949     }
0950 #endif
0951 
0952 send_skb:
0953     skb_record_rx_queue(skb, fp->rxq->rxq_id);
0954     qede_skb_receive(edev, fp, fp->rxq, skb, vlan_tag);
0955 }
0956 
0957 static inline void qede_tpa_cont(struct qede_dev *edev,
0958                  struct qede_rx_queue *rxq,
0959                  struct eth_fast_path_rx_tpa_cont_cqe *cqe)
0960 {
0961     int i;
0962 
0963     for (i = 0; cqe->len_list[i]; i++)
0964         qede_fill_frag_skb(edev, rxq, cqe->tpa_agg_index,
0965                    le16_to_cpu(cqe->len_list[i]));
0966 
0967     if (unlikely(i > 1))
0968         DP_ERR(edev,
0969                "Strange - TPA cont with more than a single len_list entry\n");
0970 }
0971 
0972 static int qede_tpa_end(struct qede_dev *edev,
0973             struct qede_fastpath *fp,
0974             struct eth_fast_path_rx_tpa_end_cqe *cqe)
0975 {
0976     struct qede_rx_queue *rxq = fp->rxq;
0977     struct qede_agg_info *tpa_info;
0978     struct sk_buff *skb;
0979     int i;
0980 
0981     tpa_info = &rxq->tpa_info[cqe->tpa_agg_index];
0982     skb = tpa_info->skb;
0983 
0984     if (tpa_info->buffer.page_offset == PAGE_SIZE)
0985         dma_unmap_page(rxq->dev, tpa_info->buffer.mapping,
0986                    PAGE_SIZE, rxq->data_direction);
0987 
0988     for (i = 0; cqe->len_list[i]; i++)
0989         qede_fill_frag_skb(edev, rxq, cqe->tpa_agg_index,
0990                    le16_to_cpu(cqe->len_list[i]));
0991     if (unlikely(i > 1))
0992         DP_ERR(edev,
0993                "Strange - TPA emd with more than a single len_list entry\n");
0994 
0995     if (unlikely(tpa_info->state != QEDE_AGG_STATE_START))
0996         goto err;
0997 
0998     /* Sanity */
0999     if (unlikely(cqe->num_of_bds != tpa_info->frag_id + 1))
1000         DP_ERR(edev,
1001                "Strange - TPA had %02x BDs, but SKB has only %d frags\n",
1002                cqe->num_of_bds, tpa_info->frag_id);
1003     if (unlikely(skb->len != le16_to_cpu(cqe->total_packet_len)))
1004         DP_ERR(edev,
1005                "Strange - total packet len [cqe] is %4x but SKB has len %04x\n",
1006                le16_to_cpu(cqe->total_packet_len), skb->len);
1007 
1008     /* Finalize the SKB */
1009     skb->protocol = eth_type_trans(skb, edev->ndev);
1010     skb->ip_summed = CHECKSUM_UNNECESSARY;
1011 
1012     /* tcp_gro_complete() will copy NAPI_GRO_CB(skb)->count
1013      * to skb_shinfo(skb)->gso_segs
1014      */
1015     NAPI_GRO_CB(skb)->count = le16_to_cpu(cqe->num_of_coalesced_segs);
1016 
1017     qede_gro_receive(edev, fp, skb, tpa_info->vlan_tag);
1018 
1019     tpa_info->state = QEDE_AGG_STATE_NONE;
1020 
1021     return 1;
1022 err:
1023     tpa_info->state = QEDE_AGG_STATE_NONE;
1024 
1025     if (tpa_info->tpa_start_fail) {
1026         qede_reuse_page(rxq, &tpa_info->buffer);
1027         tpa_info->tpa_start_fail = false;
1028     }
1029 
1030     dev_kfree_skb_any(tpa_info->skb);
1031     tpa_info->skb = NULL;
1032     return 0;
1033 }
1034 
1035 static u8 qede_check_notunn_csum(u16 flag)
1036 {
1037     u16 csum_flag = 0;
1038     u8 csum = 0;
1039 
1040     if (flag & (PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_MASK <<
1041             PARSING_AND_ERR_FLAGS_L4CHKSMWASCALCULATED_SHIFT)) {
1042         csum_flag |= PARSING_AND_ERR_FLAGS_L4CHKSMERROR_MASK <<
1043                  PARSING_AND_ERR_FLAGS_L4CHKSMERROR_SHIFT;
1044         csum = QEDE_CSUM_UNNECESSARY;
1045     }
1046 
1047     csum_flag |= PARSING_AND_ERR_FLAGS_IPHDRERROR_MASK <<
1048              PARSING_AND_ERR_FLAGS_IPHDRERROR_SHIFT;
1049 
1050     if (csum_flag & flag)
1051         return QEDE_CSUM_ERROR;
1052 
1053     return csum;
1054 }
1055 
1056 static u8 qede_check_csum(u16 flag)
1057 {
1058     if (!qede_tunn_exist(flag))
1059         return qede_check_notunn_csum(flag);
1060     else
1061         return qede_check_tunn_csum(flag);
1062 }
1063 
1064 static bool qede_pkt_is_ip_fragmented(struct eth_fast_path_rx_reg_cqe *cqe,
1065                       u16 flag)
1066 {
1067     u8 tun_pars_flg = cqe->tunnel_pars_flags.flags;
1068 
1069     if ((tun_pars_flg & (ETH_TUNNEL_PARSING_FLAGS_IPV4_FRAGMENT_MASK <<
1070                  ETH_TUNNEL_PARSING_FLAGS_IPV4_FRAGMENT_SHIFT)) ||
1071         (flag & (PARSING_AND_ERR_FLAGS_IPV4FRAG_MASK <<
1072              PARSING_AND_ERR_FLAGS_IPV4FRAG_SHIFT)))
1073         return true;
1074 
1075     return false;
1076 }
1077 
1078 /* Return true iff packet is to be passed to stack */
1079 static bool qede_rx_xdp(struct qede_dev *edev,
1080             struct qede_fastpath *fp,
1081             struct qede_rx_queue *rxq,
1082             struct bpf_prog *prog,
1083             struct sw_rx_data *bd,
1084             struct eth_fast_path_rx_reg_cqe *cqe,
1085             u16 *data_offset, u16 *len)
1086 {
1087     struct xdp_buff xdp;
1088     enum xdp_action act;
1089 
1090     xdp_init_buff(&xdp, rxq->rx_buf_seg_size, &rxq->xdp_rxq);
1091     xdp_prepare_buff(&xdp, page_address(bd->data), *data_offset,
1092              *len, false);
1093 
1094     act = bpf_prog_run_xdp(prog, &xdp);
1095 
1096     /* Recalculate, as XDP might have changed the headers */
1097     *data_offset = xdp.data - xdp.data_hard_start;
1098     *len = xdp.data_end - xdp.data;
1099 
1100     if (act == XDP_PASS)
1101         return true;
1102 
1103     /* Count number of packets not to be passed to stack */
1104     rxq->xdp_no_pass++;
1105 
1106     switch (act) {
1107     case XDP_TX:
1108         /* We need the replacement buffer before transmit. */
1109         if (unlikely(qede_alloc_rx_buffer(rxq, true))) {
1110             qede_recycle_rx_bd_ring(rxq, 1);
1111 
1112             trace_xdp_exception(edev->ndev, prog, act);
1113             break;
1114         }
1115 
1116         /* Now if there's a transmission problem, we'd still have to
1117          * throw current buffer, as replacement was already allocated.
1118          */
1119         if (unlikely(qede_xdp_xmit(fp->xdp_tx, bd->mapping,
1120                        *data_offset, *len, bd->data,
1121                        NULL))) {
1122             dma_unmap_page(rxq->dev, bd->mapping, PAGE_SIZE,
1123                        rxq->data_direction);
1124             __free_page(bd->data);
1125 
1126             trace_xdp_exception(edev->ndev, prog, act);
1127         } else {
1128             dma_sync_single_for_device(rxq->dev,
1129                            bd->mapping + *data_offset,
1130                            *len, rxq->data_direction);
1131             fp->xdp_xmit |= QEDE_XDP_TX;
1132         }
1133 
1134         /* Regardless, we've consumed an Rx BD */
1135         qede_rx_bd_ring_consume(rxq);
1136         break;
1137     case XDP_REDIRECT:
1138         /* We need the replacement buffer before transmit. */
1139         if (unlikely(qede_alloc_rx_buffer(rxq, true))) {
1140             qede_recycle_rx_bd_ring(rxq, 1);
1141 
1142             trace_xdp_exception(edev->ndev, prog, act);
1143             break;
1144         }
1145 
1146         dma_unmap_page(rxq->dev, bd->mapping, PAGE_SIZE,
1147                    rxq->data_direction);
1148 
1149         if (unlikely(xdp_do_redirect(edev->ndev, &xdp, prog)))
1150             DP_NOTICE(edev, "Failed to redirect the packet\n");
1151         else
1152             fp->xdp_xmit |= QEDE_XDP_REDIRECT;
1153 
1154         qede_rx_bd_ring_consume(rxq);
1155         break;
1156     default:
1157         bpf_warn_invalid_xdp_action(edev->ndev, prog, act);
1158         fallthrough;
1159     case XDP_ABORTED:
1160         trace_xdp_exception(edev->ndev, prog, act);
1161         fallthrough;
1162     case XDP_DROP:
1163         qede_recycle_rx_bd_ring(rxq, cqe->bd_num);
1164     }
1165 
1166     return false;
1167 }
1168 
1169 static int qede_rx_build_jumbo(struct qede_dev *edev,
1170                    struct qede_rx_queue *rxq,
1171                    struct sk_buff *skb,
1172                    struct eth_fast_path_rx_reg_cqe *cqe,
1173                    u16 first_bd_len)
1174 {
1175     u16 pkt_len = le16_to_cpu(cqe->pkt_len);
1176     struct sw_rx_data *bd;
1177     u16 bd_cons_idx;
1178     u8 num_frags;
1179 
1180     pkt_len -= first_bd_len;
1181 
1182     /* We've already used one BD for the SKB. Now take care of the rest */
1183     for (num_frags = cqe->bd_num - 1; num_frags > 0; num_frags--) {
1184         u16 cur_size = pkt_len > rxq->rx_buf_size ? rxq->rx_buf_size :
1185             pkt_len;
1186 
1187         if (unlikely(!cur_size)) {
1188             DP_ERR(edev,
1189                    "Still got %d BDs for mapping jumbo, but length became 0\n",
1190                    num_frags);
1191             goto out;
1192         }
1193 
1194         /* We need a replacement buffer for each BD */
1195         if (unlikely(qede_alloc_rx_buffer(rxq, true)))
1196             goto out;
1197 
1198         /* Now that we've allocated the replacement buffer,
1199          * we can safely consume the next BD and map it to the SKB.
1200          */
1201         bd_cons_idx = rxq->sw_rx_cons & NUM_RX_BDS_MAX;
1202         bd = &rxq->sw_rx_ring[bd_cons_idx];
1203         qede_rx_bd_ring_consume(rxq);
1204 
1205         dma_unmap_page(rxq->dev, bd->mapping,
1206                    PAGE_SIZE, DMA_FROM_DEVICE);
1207 
1208         skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, bd->data,
1209                 rxq->rx_headroom, cur_size, PAGE_SIZE);
1210 
1211         pkt_len -= cur_size;
1212     }
1213 
1214     if (unlikely(pkt_len))
1215         DP_ERR(edev,
1216                "Mapped all BDs of jumbo, but still have %d bytes\n",
1217                pkt_len);
1218 
1219 out:
1220     return num_frags;
1221 }
1222 
1223 static int qede_rx_process_tpa_cqe(struct qede_dev *edev,
1224                    struct qede_fastpath *fp,
1225                    struct qede_rx_queue *rxq,
1226                    union eth_rx_cqe *cqe,
1227                    enum eth_rx_cqe_type type)
1228 {
1229     switch (type) {
1230     case ETH_RX_CQE_TYPE_TPA_START:
1231         qede_tpa_start(edev, rxq, &cqe->fast_path_tpa_start);
1232         return 0;
1233     case ETH_RX_CQE_TYPE_TPA_CONT:
1234         qede_tpa_cont(edev, rxq, &cqe->fast_path_tpa_cont);
1235         return 0;
1236     case ETH_RX_CQE_TYPE_TPA_END:
1237         return qede_tpa_end(edev, fp, &cqe->fast_path_tpa_end);
1238     default:
1239         return 0;
1240     }
1241 }
1242 
1243 static int qede_rx_process_cqe(struct qede_dev *edev,
1244                    struct qede_fastpath *fp,
1245                    struct qede_rx_queue *rxq)
1246 {
1247     struct bpf_prog *xdp_prog = READ_ONCE(rxq->xdp_prog);
1248     struct eth_fast_path_rx_reg_cqe *fp_cqe;
1249     u16 len, pad, bd_cons_idx, parse_flag;
1250     enum eth_rx_cqe_type cqe_type;
1251     union eth_rx_cqe *cqe;
1252     struct sw_rx_data *bd;
1253     struct sk_buff *skb;
1254     __le16 flags;
1255     u8 csum_flag;
1256 
1257     /* Get the CQE from the completion ring */
1258     cqe = (union eth_rx_cqe *)qed_chain_consume(&rxq->rx_comp_ring);
1259     cqe_type = cqe->fast_path_regular.type;
1260 
1261     /* Process an unlikely slowpath event */
1262     if (unlikely(cqe_type == ETH_RX_CQE_TYPE_SLOW_PATH)) {
1263         struct eth_slow_path_rx_cqe *sp_cqe;
1264 
1265         sp_cqe = (struct eth_slow_path_rx_cqe *)cqe;
1266         edev->ops->eth_cqe_completion(edev->cdev, fp->id, sp_cqe);
1267         return 0;
1268     }
1269 
1270     /* Handle TPA cqes */
1271     if (cqe_type != ETH_RX_CQE_TYPE_REGULAR)
1272         return qede_rx_process_tpa_cqe(edev, fp, rxq, cqe, cqe_type);
1273 
1274     /* Get the data from the SW ring; Consume it only after it's evident
1275      * we wouldn't recycle it.
1276      */
1277     bd_cons_idx = rxq->sw_rx_cons & NUM_RX_BDS_MAX;
1278     bd = &rxq->sw_rx_ring[bd_cons_idx];
1279 
1280     fp_cqe = &cqe->fast_path_regular;
1281     len = le16_to_cpu(fp_cqe->len_on_first_bd);
1282     pad = fp_cqe->placement_offset + rxq->rx_headroom;
1283 
1284     /* Run eBPF program if one is attached */
1285     if (xdp_prog)
1286         if (!qede_rx_xdp(edev, fp, rxq, xdp_prog, bd, fp_cqe,
1287                  &pad, &len))
1288             return 0;
1289 
1290     /* If this is an error packet then drop it */
1291     flags = cqe->fast_path_regular.pars_flags.flags;
1292     parse_flag = le16_to_cpu(flags);
1293 
1294     csum_flag = qede_check_csum(parse_flag);
1295     if (unlikely(csum_flag == QEDE_CSUM_ERROR)) {
1296         if (qede_pkt_is_ip_fragmented(fp_cqe, parse_flag))
1297             rxq->rx_ip_frags++;
1298         else
1299             rxq->rx_hw_errors++;
1300     }
1301 
1302     /* Basic validation passed; Need to prepare an SKB. This would also
1303      * guarantee to finally consume the first BD upon success.
1304      */
1305     skb = qede_rx_build_skb(edev, rxq, bd, len, pad);
1306     if (!skb) {
1307         rxq->rx_alloc_errors++;
1308         qede_recycle_rx_bd_ring(rxq, fp_cqe->bd_num);
1309         return 0;
1310     }
1311 
1312     /* In case of Jumbo packet, several PAGE_SIZEd buffers will be pointed
1313      * by a single cqe.
1314      */
1315     if (fp_cqe->bd_num > 1) {
1316         u16 unmapped_frags = qede_rx_build_jumbo(edev, rxq, skb,
1317                              fp_cqe, len);
1318 
1319         if (unlikely(unmapped_frags > 0)) {
1320             qede_recycle_rx_bd_ring(rxq, unmapped_frags);
1321             dev_kfree_skb_any(skb);
1322             return 0;
1323         }
1324     }
1325 
1326     /* The SKB contains all the data. Now prepare meta-magic */
1327     skb->protocol = eth_type_trans(skb, edev->ndev);
1328     qede_get_rxhash(skb, fp_cqe->bitfields, fp_cqe->rss_hash);
1329     qede_set_skb_csum(skb, csum_flag);
1330     skb_record_rx_queue(skb, rxq->rxq_id);
1331     qede_ptp_record_rx_ts(edev, cqe, skb);
1332 
1333     /* SKB is prepared - pass it to stack */
1334     qede_skb_receive(edev, fp, rxq, skb, le16_to_cpu(fp_cqe->vlan_tag));
1335 
1336     return 1;
1337 }
1338 
1339 static int qede_rx_int(struct qede_fastpath *fp, int budget)
1340 {
1341     struct qede_rx_queue *rxq = fp->rxq;
1342     struct qede_dev *edev = fp->edev;
1343     int work_done = 0, rcv_pkts = 0;
1344     u16 hw_comp_cons, sw_comp_cons;
1345 
1346     hw_comp_cons = le16_to_cpu(*rxq->hw_cons_ptr);
1347     sw_comp_cons = qed_chain_get_cons_idx(&rxq->rx_comp_ring);
1348 
1349     /* Memory barrier to prevent the CPU from doing speculative reads of CQE
1350      * / BD in the while-loop before reading hw_comp_cons. If the CQE is
1351      * read before it is written by FW, then FW writes CQE and SB, and then
1352      * the CPU reads the hw_comp_cons, it will use an old CQE.
1353      */
1354     rmb();
1355 
1356     /* Loop to complete all indicated BDs */
1357     while ((sw_comp_cons != hw_comp_cons) && (work_done < budget)) {
1358         rcv_pkts += qede_rx_process_cqe(edev, fp, rxq);
1359         qed_chain_recycle_consumed(&rxq->rx_comp_ring);
1360         sw_comp_cons = qed_chain_get_cons_idx(&rxq->rx_comp_ring);
1361         work_done++;
1362     }
1363 
1364     rxq->rcv_pkts += rcv_pkts;
1365 
1366     /* Allocate replacement buffers */
1367     while (rxq->num_rx_buffers - rxq->filled_buffers)
1368         if (qede_alloc_rx_buffer(rxq, false))
1369             break;
1370 
1371     /* Update producers */
1372     qede_update_rx_prod(edev, rxq);
1373 
1374     return work_done;
1375 }
1376 
1377 static bool qede_poll_is_more_work(struct qede_fastpath *fp)
1378 {
1379     qed_sb_update_sb_idx(fp->sb_info);
1380 
1381     /* *_has_*_work() reads the status block, thus we need to ensure that
1382      * status block indices have been actually read (qed_sb_update_sb_idx)
1383      * prior to this check (*_has_*_work) so that we won't write the
1384      * "newer" value of the status block to HW (if there was a DMA right
1385      * after qede_has_rx_work and if there is no rmb, the memory reading
1386      * (qed_sb_update_sb_idx) may be postponed to right before *_ack_sb).
1387      * In this case there will never be another interrupt until there is
1388      * another update of the status block, while there is still unhandled
1389      * work.
1390      */
1391     rmb();
1392 
1393     if (likely(fp->type & QEDE_FASTPATH_RX))
1394         if (qede_has_rx_work(fp->rxq))
1395             return true;
1396 
1397     if (fp->type & QEDE_FASTPATH_XDP)
1398         if (qede_txq_has_work(fp->xdp_tx))
1399             return true;
1400 
1401     if (likely(fp->type & QEDE_FASTPATH_TX)) {
1402         int cos;
1403 
1404         for_each_cos_in_txq(fp->edev, cos) {
1405             if (qede_txq_has_work(&fp->txq[cos]))
1406                 return true;
1407         }
1408     }
1409 
1410     return false;
1411 }
1412 
1413 /*********************
1414  * NDO & API related *
1415  *********************/
1416 int qede_poll(struct napi_struct *napi, int budget)
1417 {
1418     struct qede_fastpath *fp = container_of(napi, struct qede_fastpath,
1419                         napi);
1420     struct qede_dev *edev = fp->edev;
1421     int rx_work_done = 0;
1422     u16 xdp_prod;
1423 
1424     fp->xdp_xmit = 0;
1425 
1426     if (likely(fp->type & QEDE_FASTPATH_TX)) {
1427         int cos;
1428 
1429         for_each_cos_in_txq(fp->edev, cos) {
1430             if (qede_txq_has_work(&fp->txq[cos]))
1431                 qede_tx_int(edev, &fp->txq[cos]);
1432         }
1433     }
1434 
1435     if ((fp->type & QEDE_FASTPATH_XDP) && qede_txq_has_work(fp->xdp_tx))
1436         qede_xdp_tx_int(edev, fp->xdp_tx);
1437 
1438     rx_work_done = (likely(fp->type & QEDE_FASTPATH_RX) &&
1439             qede_has_rx_work(fp->rxq)) ?
1440             qede_rx_int(fp, budget) : 0;
1441     /* Handle case where we are called by netpoll with a budget of 0 */
1442     if (rx_work_done < budget || !budget) {
1443         if (!qede_poll_is_more_work(fp)) {
1444             napi_complete_done(napi, rx_work_done);
1445 
1446             /* Update and reenable interrupts */
1447             qed_sb_ack(fp->sb_info, IGU_INT_ENABLE, 1);
1448         } else {
1449             rx_work_done = budget;
1450         }
1451     }
1452 
1453     if (fp->xdp_xmit & QEDE_XDP_TX) {
1454         xdp_prod = qed_chain_get_prod_idx(&fp->xdp_tx->tx_pbl);
1455 
1456         fp->xdp_tx->tx_db.data.bd_prod = cpu_to_le16(xdp_prod);
1457         qede_update_tx_producer(fp->xdp_tx);
1458     }
1459 
1460     if (fp->xdp_xmit & QEDE_XDP_REDIRECT)
1461         xdp_do_flush_map();
1462 
1463     return rx_work_done;
1464 }
1465 
1466 irqreturn_t qede_msix_fp_int(int irq, void *fp_cookie)
1467 {
1468     struct qede_fastpath *fp = fp_cookie;
1469 
1470     qed_sb_ack(fp->sb_info, IGU_INT_DISABLE, 0 /*do not update*/);
1471 
1472     napi_schedule_irqoff(&fp->napi);
1473     return IRQ_HANDLED;
1474 }
1475 
1476 /* Main transmit function */
1477 netdev_tx_t qede_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1478 {
1479     struct qede_dev *edev = netdev_priv(ndev);
1480     struct netdev_queue *netdev_txq;
1481     struct qede_tx_queue *txq;
1482     struct eth_tx_1st_bd *first_bd;
1483     struct eth_tx_2nd_bd *second_bd = NULL;
1484     struct eth_tx_3rd_bd *third_bd = NULL;
1485     struct eth_tx_bd *tx_data_bd = NULL;
1486     u16 txq_index, val = 0;
1487     u8 nbd = 0;
1488     dma_addr_t mapping;
1489     int rc, frag_idx = 0, ipv6_ext = 0;
1490     u8 xmit_type;
1491     u16 idx;
1492     u16 hlen;
1493     bool data_split = false;
1494 
1495     /* Get tx-queue context and netdev index */
1496     txq_index = skb_get_queue_mapping(skb);
1497     WARN_ON(txq_index >= QEDE_TSS_COUNT(edev) * edev->dev_info.num_tc);
1498     txq = QEDE_NDEV_TXQ_ID_TO_TXQ(edev, txq_index);
1499     netdev_txq = netdev_get_tx_queue(ndev, txq_index);
1500 
1501     WARN_ON(qed_chain_get_elem_left(&txq->tx_pbl) < (MAX_SKB_FRAGS + 1));
1502 
1503     xmit_type = qede_xmit_type(skb, &ipv6_ext);
1504 
1505 #if ((MAX_SKB_FRAGS + 2) > ETH_TX_MAX_BDS_PER_NON_LSO_PACKET)
1506     if (qede_pkt_req_lin(skb, xmit_type)) {
1507         if (skb_linearize(skb)) {
1508             txq->tx_mem_alloc_err++;
1509 
1510             dev_kfree_skb_any(skb);
1511             return NETDEV_TX_OK;
1512         }
1513     }
1514 #endif
1515 
1516     /* Fill the entry in the SW ring and the BDs in the FW ring */
1517     idx = txq->sw_tx_prod;
1518     txq->sw_tx_ring.skbs[idx].skb = skb;
1519     first_bd = (struct eth_tx_1st_bd *)
1520            qed_chain_produce(&txq->tx_pbl);
1521     memset(first_bd, 0, sizeof(*first_bd));
1522     first_bd->data.bd_flags.bitfields =
1523         1 << ETH_TX_1ST_BD_FLAGS_START_BD_SHIFT;
1524 
1525     if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
1526         qede_ptp_tx_ts(edev, skb);
1527 
1528     /* Map skb linear data for DMA and set in the first BD */
1529     mapping = dma_map_single(txq->dev, skb->data,
1530                  skb_headlen(skb), DMA_TO_DEVICE);
1531     if (unlikely(dma_mapping_error(txq->dev, mapping))) {
1532         DP_NOTICE(edev, "SKB mapping failed\n");
1533         qede_free_failed_tx_pkt(txq, first_bd, 0, false);
1534         qede_update_tx_producer(txq);
1535         return NETDEV_TX_OK;
1536     }
1537     nbd++;
1538     BD_SET_UNMAP_ADDR_LEN(first_bd, mapping, skb_headlen(skb));
1539 
1540     /* In case there is IPv6 with extension headers or LSO we need 2nd and
1541      * 3rd BDs.
1542      */
1543     if (unlikely((xmit_type & XMIT_LSO) | ipv6_ext)) {
1544         second_bd = (struct eth_tx_2nd_bd *)
1545             qed_chain_produce(&txq->tx_pbl);
1546         memset(second_bd, 0, sizeof(*second_bd));
1547 
1548         nbd++;
1549         third_bd = (struct eth_tx_3rd_bd *)
1550             qed_chain_produce(&txq->tx_pbl);
1551         memset(third_bd, 0, sizeof(*third_bd));
1552 
1553         nbd++;
1554         /* We need to fill in additional data in second_bd... */
1555         tx_data_bd = (struct eth_tx_bd *)second_bd;
1556     }
1557 
1558     if (skb_vlan_tag_present(skb)) {
1559         first_bd->data.vlan = cpu_to_le16(skb_vlan_tag_get(skb));
1560         first_bd->data.bd_flags.bitfields |=
1561             1 << ETH_TX_1ST_BD_FLAGS_VLAN_INSERTION_SHIFT;
1562     }
1563 
1564     /* Fill the parsing flags & params according to the requested offload */
1565     if (xmit_type & XMIT_L4_CSUM) {
1566         /* We don't re-calculate IP checksum as it is already done by
1567          * the upper stack
1568          */
1569         first_bd->data.bd_flags.bitfields |=
1570             1 << ETH_TX_1ST_BD_FLAGS_L4_CSUM_SHIFT;
1571 
1572         if (xmit_type & XMIT_ENC) {
1573             first_bd->data.bd_flags.bitfields |=
1574                 1 << ETH_TX_1ST_BD_FLAGS_IP_CSUM_SHIFT;
1575 
1576             val |= (1 << ETH_TX_DATA_1ST_BD_TUNN_FLAG_SHIFT);
1577         }
1578 
1579         /* Legacy FW had flipped behavior in regard to this bit -
1580          * I.e., needed to set to prevent FW from touching encapsulated
1581          * packets when it didn't need to.
1582          */
1583         if (unlikely(txq->is_legacy))
1584             val ^= (1 << ETH_TX_DATA_1ST_BD_TUNN_FLAG_SHIFT);
1585 
1586         /* If the packet is IPv6 with extension header, indicate that
1587          * to FW and pass few params, since the device cracker doesn't
1588          * support parsing IPv6 with extension header/s.
1589          */
1590         if (unlikely(ipv6_ext))
1591             qede_set_params_for_ipv6_ext(skb, second_bd, third_bd);
1592     }
1593 
1594     if (xmit_type & XMIT_LSO) {
1595         first_bd->data.bd_flags.bitfields |=
1596             (1 << ETH_TX_1ST_BD_FLAGS_LSO_SHIFT);
1597         third_bd->data.lso_mss =
1598             cpu_to_le16(skb_shinfo(skb)->gso_size);
1599 
1600         if (unlikely(xmit_type & XMIT_ENC)) {
1601             first_bd->data.bd_flags.bitfields |=
1602                 1 << ETH_TX_1ST_BD_FLAGS_TUNN_IP_CSUM_SHIFT;
1603 
1604             if (xmit_type & XMIT_ENC_GSO_L4_CSUM) {
1605                 u8 tmp = ETH_TX_1ST_BD_FLAGS_TUNN_L4_CSUM_SHIFT;
1606 
1607                 first_bd->data.bd_flags.bitfields |= 1 << tmp;
1608             }
1609             hlen = qede_get_skb_hlen(skb, true);
1610         } else {
1611             first_bd->data.bd_flags.bitfields |=
1612                 1 << ETH_TX_1ST_BD_FLAGS_IP_CSUM_SHIFT;
1613             hlen = qede_get_skb_hlen(skb, false);
1614         }
1615 
1616         /* @@@TBD - if will not be removed need to check */
1617         third_bd->data.bitfields |=
1618             cpu_to_le16(1 << ETH_TX_DATA_3RD_BD_HDR_NBD_SHIFT);
1619 
1620         /* Make life easier for FW guys who can't deal with header and
1621          * data on same BD. If we need to split, use the second bd...
1622          */
1623         if (unlikely(skb_headlen(skb) > hlen)) {
1624             DP_VERBOSE(edev, NETIF_MSG_TX_QUEUED,
1625                    "TSO split header size is %d (%x:%x)\n",
1626                    first_bd->nbytes, first_bd->addr.hi,
1627                    first_bd->addr.lo);
1628 
1629             mapping = HILO_U64(le32_to_cpu(first_bd->addr.hi),
1630                        le32_to_cpu(first_bd->addr.lo)) +
1631                        hlen;
1632 
1633             BD_SET_UNMAP_ADDR_LEN(tx_data_bd, mapping,
1634                           le16_to_cpu(first_bd->nbytes) -
1635                           hlen);
1636 
1637             /* this marks the BD as one that has no
1638              * individual mapping
1639              */
1640             txq->sw_tx_ring.skbs[idx].flags |= QEDE_TSO_SPLIT_BD;
1641 
1642             first_bd->nbytes = cpu_to_le16(hlen);
1643 
1644             tx_data_bd = (struct eth_tx_bd *)third_bd;
1645             data_split = true;
1646         }
1647     } else {
1648         if (unlikely(skb->len > ETH_TX_MAX_NON_LSO_PKT_LEN)) {
1649             DP_ERR(edev, "Unexpected non LSO skb length = 0x%x\n", skb->len);
1650             qede_free_failed_tx_pkt(txq, first_bd, 0, false);
1651             qede_update_tx_producer(txq);
1652             return NETDEV_TX_OK;
1653         }
1654 
1655         val |= ((skb->len & ETH_TX_DATA_1ST_BD_PKT_LEN_MASK) <<
1656              ETH_TX_DATA_1ST_BD_PKT_LEN_SHIFT);
1657     }
1658 
1659     first_bd->data.bitfields = cpu_to_le16(val);
1660 
1661     /* Handle fragmented skb */
1662     /* special handle for frags inside 2nd and 3rd bds.. */
1663     while (tx_data_bd && frag_idx < skb_shinfo(skb)->nr_frags) {
1664         rc = map_frag_to_bd(txq,
1665                     &skb_shinfo(skb)->frags[frag_idx],
1666                     tx_data_bd);
1667         if (rc) {
1668             qede_free_failed_tx_pkt(txq, first_bd, nbd, data_split);
1669             qede_update_tx_producer(txq);
1670             return NETDEV_TX_OK;
1671         }
1672 
1673         if (tx_data_bd == (struct eth_tx_bd *)second_bd)
1674             tx_data_bd = (struct eth_tx_bd *)third_bd;
1675         else
1676             tx_data_bd = NULL;
1677 
1678         frag_idx++;
1679     }
1680 
1681     /* map last frags into 4th, 5th .... */
1682     for (; frag_idx < skb_shinfo(skb)->nr_frags; frag_idx++, nbd++) {
1683         tx_data_bd = (struct eth_tx_bd *)
1684                  qed_chain_produce(&txq->tx_pbl);
1685 
1686         memset(tx_data_bd, 0, sizeof(*tx_data_bd));
1687 
1688         rc = map_frag_to_bd(txq,
1689                     &skb_shinfo(skb)->frags[frag_idx],
1690                     tx_data_bd);
1691         if (rc) {
1692             qede_free_failed_tx_pkt(txq, first_bd, nbd, data_split);
1693             qede_update_tx_producer(txq);
1694             return NETDEV_TX_OK;
1695         }
1696     }
1697 
1698     /* update the first BD with the actual num BDs */
1699     first_bd->data.nbds = nbd;
1700 
1701     netdev_tx_sent_queue(netdev_txq, skb->len);
1702 
1703     skb_tx_timestamp(skb);
1704 
1705     /* Advance packet producer only before sending the packet since mapping
1706      * of pages may fail.
1707      */
1708     txq->sw_tx_prod = (txq->sw_tx_prod + 1) % txq->num_tx_buffers;
1709 
1710     /* 'next page' entries are counted in the producer value */
1711     txq->tx_db.data.bd_prod =
1712         cpu_to_le16(qed_chain_get_prod_idx(&txq->tx_pbl));
1713 
1714     if (!netdev_xmit_more() || netif_xmit_stopped(netdev_txq))
1715         qede_update_tx_producer(txq);
1716 
1717     if (unlikely(qed_chain_get_elem_left(&txq->tx_pbl)
1718               < (MAX_SKB_FRAGS + 1))) {
1719         if (netdev_xmit_more())
1720             qede_update_tx_producer(txq);
1721 
1722         netif_tx_stop_queue(netdev_txq);
1723         txq->stopped_cnt++;
1724         DP_VERBOSE(edev, NETIF_MSG_TX_QUEUED,
1725                "Stop queue was called\n");
1726         /* paired memory barrier is in qede_tx_int(), we have to keep
1727          * ordering of set_bit() in netif_tx_stop_queue() and read of
1728          * fp->bd_tx_cons
1729          */
1730         smp_mb();
1731 
1732         if ((qed_chain_get_elem_left(&txq->tx_pbl) >=
1733              (MAX_SKB_FRAGS + 1)) &&
1734             (edev->state == QEDE_STATE_OPEN)) {
1735             netif_tx_wake_queue(netdev_txq);
1736             DP_VERBOSE(edev, NETIF_MSG_TX_QUEUED,
1737                    "Wake queue was called\n");
1738         }
1739     }
1740 
1741     return NETDEV_TX_OK;
1742 }
1743 
1744 u16 qede_select_queue(struct net_device *dev, struct sk_buff *skb,
1745               struct net_device *sb_dev)
1746 {
1747     struct qede_dev *edev = netdev_priv(dev);
1748     int total_txq;
1749 
1750     total_txq = QEDE_TSS_COUNT(edev) * edev->dev_info.num_tc;
1751 
1752     return QEDE_TSS_COUNT(edev) ?
1753         netdev_pick_tx(dev, skb, NULL) % total_txq :  0;
1754 }
1755 
1756 /* 8B udp header + 8B base tunnel header + 32B option length */
1757 #define QEDE_MAX_TUN_HDR_LEN 48
1758 
1759 netdev_features_t qede_features_check(struct sk_buff *skb,
1760                       struct net_device *dev,
1761                       netdev_features_t features)
1762 {
1763     if (skb->encapsulation) {
1764         u8 l4_proto = 0;
1765 
1766         switch (vlan_get_protocol(skb)) {
1767         case htons(ETH_P_IP):
1768             l4_proto = ip_hdr(skb)->protocol;
1769             break;
1770         case htons(ETH_P_IPV6):
1771             l4_proto = ipv6_hdr(skb)->nexthdr;
1772             break;
1773         default:
1774             return features;
1775         }
1776 
1777         /* Disable offloads for geneve tunnels, as HW can't parse
1778          * the geneve header which has option length greater than 32b
1779          * and disable offloads for the ports which are not offloaded.
1780          */
1781         if (l4_proto == IPPROTO_UDP) {
1782             struct qede_dev *edev = netdev_priv(dev);
1783             u16 hdrlen, vxln_port, gnv_port;
1784 
1785             hdrlen = QEDE_MAX_TUN_HDR_LEN;
1786             vxln_port = edev->vxlan_dst_port;
1787             gnv_port = edev->geneve_dst_port;
1788 
1789             if ((skb_inner_mac_header(skb) -
1790                  skb_transport_header(skb)) > hdrlen ||
1791                  (ntohs(udp_hdr(skb)->dest) != vxln_port &&
1792                   ntohs(udp_hdr(skb)->dest) != gnv_port))
1793                 return features & ~(NETIF_F_CSUM_MASK |
1794                             NETIF_F_GSO_MASK);
1795         } else if (l4_proto == IPPROTO_IPIP) {
1796             /* IPIP tunnels are unknown to the device or at least unsupported natively,
1797              * offloads for them can't be done trivially, so disable them for such skb.
1798              */
1799             return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
1800         }
1801     }
1802 
1803     return features;
1804 }