/* Copyright(c) 2018 Intel Corporation. */
 
 #include <linux/bpf_trace.h>
+#include <linux/stringify.h>
 #include <net/xdp_sock_drv.h>
 #include <net/xdp.h>
 
        return failure ? budget : (int)total_rx_packets;
 }
 
+static void i40e_xmit_pkt(struct i40e_ring *xdp_ring, struct xdp_desc *desc,
+                         unsigned int *total_bytes)
+{
+       struct i40e_tx_desc *tx_desc;
+       dma_addr_t dma;
+
+       dma = xsk_buff_raw_get_dma(xdp_ring->xsk_pool, desc->addr);
+       xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_pool, dma, desc->len);
+
+       tx_desc = I40E_TX_DESC(xdp_ring, xdp_ring->next_to_use++);
+       tx_desc->buffer_addr = cpu_to_le64(dma);
+       tx_desc->cmd_type_offset_bsz = build_ctob(I40E_TX_DESC_CMD_ICRC | I40E_TX_DESC_CMD_EOP,
+                                                 0, desc->len, 0);
+
+       *total_bytes += desc->len;
+}
+
+static void i40e_xmit_pkt_batch(struct i40e_ring *xdp_ring, struct xdp_desc *desc,
+                               unsigned int *total_bytes)
+{
+       u16 ntu = xdp_ring->next_to_use;
+       struct i40e_tx_desc *tx_desc;
+       dma_addr_t dma;
+       u32 i;
+
+       loop_unrolled_for(i = 0; i < PKTS_PER_BATCH; i++) {
+               dma = xsk_buff_raw_get_dma(xdp_ring->xsk_pool, desc[i].addr);
+               xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_pool, dma, desc[i].len);
+
+               tx_desc = I40E_TX_DESC(xdp_ring, ntu++);
+               tx_desc->buffer_addr = cpu_to_le64(dma);
+               tx_desc->cmd_type_offset_bsz = build_ctob(I40E_TX_DESC_CMD_ICRC |
+                                                         I40E_TX_DESC_CMD_EOP,
+                                                         0, desc[i].len, 0);
+
+               *total_bytes += desc[i].len;
+       }
+
+       xdp_ring->next_to_use = ntu;
+}
+
+static void i40e_fill_tx_hw_ring(struct i40e_ring *xdp_ring, struct xdp_desc *descs, u32 nb_pkts,
+                                unsigned int *total_bytes)
+{
+       u32 batched, leftover, i;
+
+       batched = nb_pkts & ~(PKTS_PER_BATCH - 1);
+       leftover = nb_pkts & (PKTS_PER_BATCH - 1);
+       for (i = 0; i < batched; i += PKTS_PER_BATCH)
+               i40e_xmit_pkt_batch(xdp_ring, &descs[i], total_bytes);
+       for (i = batched; i < batched + leftover; i++)
+               i40e_xmit_pkt(xdp_ring, &descs[i], total_bytes);
+}
+
+static void i40e_set_rs_bit(struct i40e_ring *xdp_ring)
+{
+       u16 ntu = xdp_ring->next_to_use ? xdp_ring->next_to_use - 1 : xdp_ring->count - 1;
+       struct i40e_tx_desc *tx_desc;
+
+       tx_desc = I40E_TX_DESC(xdp_ring, ntu);
+       tx_desc->cmd_type_offset_bsz |= (I40E_TX_DESC_CMD_RS << I40E_TXD_QW1_CMD_SHIFT);
+}
+
 /**
  * i40e_xmit_zc - Performs zero-copy Tx AF_XDP
  * @xdp_ring: XDP Tx ring
  **/
 static bool i40e_xmit_zc(struct i40e_ring *xdp_ring, unsigned int budget)
 {
-       unsigned int sent_frames = 0, total_bytes = 0;
-       struct i40e_tx_desc *tx_desc = NULL;
-       struct xdp_desc desc;
-       dma_addr_t dma;
-
-       while (budget-- > 0) {
-               if (!xsk_tx_peek_desc(xdp_ring->xsk_pool, &desc))
-                       break;
-
-               dma = xsk_buff_raw_get_dma(xdp_ring->xsk_pool, desc.addr);
-               xsk_buff_raw_dma_sync_for_device(xdp_ring->xsk_pool, dma,
-                                                desc.len);
-
-               tx_desc = I40E_TX_DESC(xdp_ring, xdp_ring->next_to_use);
-               tx_desc->buffer_addr = cpu_to_le64(dma);
-               tx_desc->cmd_type_offset_bsz =
-                       build_ctob(I40E_TX_DESC_CMD_ICRC
-                                  | I40E_TX_DESC_CMD_EOP,
-                                  0, desc.len, 0);
-
-               sent_frames++;
-               total_bytes += desc.len;
-
-               xdp_ring->next_to_use++;
-               if (xdp_ring->next_to_use == xdp_ring->count)
-                       xdp_ring->next_to_use = 0;
+       struct xdp_desc *descs = xdp_ring->xsk_descs;
+       u32 nb_pkts, nb_processed = 0;
+       unsigned int total_bytes = 0;
+
+       nb_pkts = xsk_tx_peek_release_desc_batch(xdp_ring->xsk_pool, descs, budget);
+       if (!nb_pkts)
+               return false;
+
+       if (xdp_ring->next_to_use + nb_pkts >= xdp_ring->count) {
+               nb_processed = xdp_ring->count - xdp_ring->next_to_use;
+               i40e_fill_tx_hw_ring(xdp_ring, descs, nb_processed, &total_bytes);
+               xdp_ring->next_to_use = 0;
        }
 
-       if (tx_desc) {
-               /* Request an interrupt for the last frame and bump tail ptr. */
-               tx_desc->cmd_type_offset_bsz |= (I40E_TX_DESC_CMD_RS <<
-                                                I40E_TXD_QW1_CMD_SHIFT);
-               i40e_xdp_ring_update_tail(xdp_ring);
+       i40e_fill_tx_hw_ring(xdp_ring, &descs[nb_processed], nb_pkts - nb_processed,
+                            &total_bytes);
 
-               xsk_tx_release(xdp_ring->xsk_pool);
-               i40e_update_tx_stats(xdp_ring, sent_frames, total_bytes);
-       }
+       /* Request an interrupt for the last frame and bump tail ptr. */
+       i40e_set_rs_bit(xdp_ring);
+       i40e_xdp_ring_update_tail(xdp_ring);
+
+       i40e_update_tx_stats(xdp_ring, nb_pkts, total_bytes);
 
-       return !!budget;
+       return true;
 }
 
 /**