+
+static __rte_always_inline uint64_t
+octeontx_pktmbuf_detach(struct rte_mbuf *m, struct rte_mbuf **m_tofree)
+{
+ struct rte_mempool *mp = m->pool;
+ uint32_t mbuf_size, buf_len;
+ struct rte_mbuf *md;
+ uint16_t priv_size;
+ uint16_t refcount;
+
+ /* Update refcount of direct mbuf */
+ md = rte_mbuf_from_indirect(m);
+ /* The real data will be in the direct buffer, inform callers this */
+ *m_tofree = md;
+ refcount = rte_mbuf_refcnt_update(md, -1);
+
+ priv_size = rte_pktmbuf_priv_size(mp);
+ mbuf_size = (uint32_t)(sizeof(struct rte_mbuf) + priv_size);
+ buf_len = rte_pktmbuf_data_room_size(mp);
+
+ m->priv_size = priv_size;
+ m->buf_addr = (char *)m + mbuf_size;
+ m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
+ m->buf_len = (uint16_t)buf_len;
+ rte_pktmbuf_reset_headroom(m);
+ m->data_len = 0;
+ m->ol_flags = 0;
+ m->next = NULL;
+ m->nb_segs = 1;
+
+ /* Now indirect mbuf is safe to free */
+ rte_pktmbuf_free(m);
+
+ if (refcount == 0) {
+ rte_mbuf_refcnt_set(md, 1);
+ md->data_len = 0;
+ md->ol_flags = 0;
+ md->next = NULL;
+ md->nb_segs = 1;
+ return 0;
+ } else {
+ return 1;
+ }
+}
+
+static __rte_always_inline uint64_t
+octeontx_prefree_seg(struct rte_mbuf *m, struct rte_mbuf **m_tofree)
+{
+ if (likely(rte_mbuf_refcnt_read(m) == 1)) {
+ if (!RTE_MBUF_DIRECT(m))
+ return octeontx_pktmbuf_detach(m, m_tofree);
+
+ m->next = NULL;
+ m->nb_segs = 1;
+ return 0;
+ } else if (rte_mbuf_refcnt_update(m, -1) == 0) {
+ if (!RTE_MBUF_DIRECT(m))
+ return octeontx_pktmbuf_detach(m, m_tofree);
+
+ rte_mbuf_refcnt_set(m, 1);
+ m->next = NULL;
+ m->nb_segs = 1;
+ return 0;
+ }
+
+ /* Mbuf is having refcount more than 1 so need not to be freed */
+ return 1;
+}
+
+static __rte_always_inline void
+octeontx_tx_checksum_offload(uint64_t *cmd_buf, const uint16_t flags,
+ struct rte_mbuf *m)
+{
+ struct octeontx_send_hdr_s *send_hdr =
+ (struct octeontx_send_hdr_s *)cmd_buf;
+ uint64_t ol_flags = m->ol_flags;
+
+ /* PKO Checksum L4 Algorithm Enumeration
+ * 0x0 - No checksum
+ * 0x1 - UDP L4 checksum
+ * 0x2 - TCP L4 checksum
+ * 0x3 - SCTP L4 checksum
+ */
+ const uint8_t csum = (!(((ol_flags ^ RTE_MBUF_F_TX_UDP_CKSUM) >> 52) & 0x3) +
+ (!(((ol_flags ^ RTE_MBUF_F_TX_TCP_CKSUM) >> 52) & 0x3) * 2) +
+ (!(((ol_flags ^ RTE_MBUF_F_TX_SCTP_CKSUM) >> 52) & 0x3) * 3));
+
+ const uint8_t is_tunnel_parsed = (!!(ol_flags & RTE_MBUF_F_TX_TUNNEL_GTP) ||
+ !!(ol_flags & RTE_MBUF_F_TX_TUNNEL_VXLAN_GPE) ||
+ !!(ol_flags & RTE_MBUF_F_TX_TUNNEL_VXLAN) ||
+ !!(ol_flags & RTE_MBUF_F_TX_TUNNEL_GRE) ||
+ !!(ol_flags & RTE_MBUF_F_TX_TUNNEL_GENEVE) ||
+ !!(ol_flags & RTE_MBUF_F_TX_TUNNEL_IP) ||
+ !!(ol_flags & RTE_MBUF_F_TX_TUNNEL_IPIP));
+
+ const uint8_t csum_outer = (!!(ol_flags & RTE_MBUF_F_TX_OUTER_UDP_CKSUM) ||
+ !!(ol_flags & RTE_MBUF_F_TX_TUNNEL_UDP));
+ const uint8_t outer_l2_len = m->outer_l2_len;
+ const uint8_t l2_len = m->l2_len;
+
+ if ((flags & OCCTX_TX_OFFLOAD_OL3_OL4_CSUM_F) &&
+ (flags & OCCTX_TX_OFFLOAD_L3_L4_CSUM_F)) {
+ if (is_tunnel_parsed) {
+ /* Outer L3 */
+ send_hdr->w0.l3ptr = outer_l2_len;
+ send_hdr->w0.l4ptr = outer_l2_len + m->outer_l3_len;
+ /* Set clk3 for PKO to calculate IPV4 header checksum */
+ send_hdr->w0.ckl3 = !!(ol_flags & RTE_MBUF_F_TX_OUTER_IPV4);
+
+ /* Outer L4 */
+ send_hdr->w0.ckl4 = csum_outer;
+
+ /* Inner L3 */
+ send_hdr->w1.leptr = send_hdr->w0.l4ptr + l2_len;
+ send_hdr->w1.lfptr = send_hdr->w1.leptr + m->l3_len;
+ /* Set clke for PKO to calculate inner IPV4 header
+ * checksum.
+ */
+ send_hdr->w0.ckle = !!(ol_flags & RTE_MBUF_F_TX_IPV4);
+
+ /* Inner L4 */
+ send_hdr->w0.cklf = csum;
+ } else {
+ /* Inner L3 */
+ send_hdr->w0.l3ptr = l2_len;
+ send_hdr->w0.l4ptr = l2_len + m->l3_len;
+ /* Set clk3 for PKO to calculate IPV4 header checksum */
+ send_hdr->w0.ckl3 = !!(ol_flags & RTE_MBUF_F_TX_IPV4);
+
+ /* Inner L4 */
+ send_hdr->w0.ckl4 = csum;
+ }
+ } else if (flags & OCCTX_TX_OFFLOAD_OL3_OL4_CSUM_F) {
+ /* Outer L3 */
+ send_hdr->w0.l3ptr = outer_l2_len;
+ send_hdr->w0.l4ptr = outer_l2_len + m->outer_l3_len;
+ /* Set clk3 for PKO to calculate IPV4 header checksum */
+ send_hdr->w0.ckl3 = !!(ol_flags & RTE_MBUF_F_TX_OUTER_IPV4);
+
+ /* Outer L4 */
+ send_hdr->w0.ckl4 = csum_outer;
+ } else if (flags & OCCTX_TX_OFFLOAD_L3_L4_CSUM_F) {
+ /* Inner L3 */
+ send_hdr->w0.l3ptr = l2_len;
+ send_hdr->w0.l4ptr = l2_len + m->l3_len;
+ /* Set clk3 for PKO to calculate IPV4 header checksum */
+ send_hdr->w0.ckl3 = !!(ol_flags & RTE_MBUF_F_TX_IPV4);
+
+ /* Inner L4 */
+ send_hdr->w0.ckl4 = csum;
+ }
+}
+
+static __rte_always_inline uint16_t
+__octeontx_xmit_prepare(struct rte_mbuf *tx_pkt, uint64_t *cmd_buf,
+ const uint16_t flag)
+{
+ uint16_t gaura_id, nb_desc = 0;
+ struct rte_mbuf *m_tofree;
+ rte_iova_t iova;
+ uint16_t data_len;
+
+ m_tofree = tx_pkt;
+
+ data_len = tx_pkt->data_len;
+ iova = rte_mbuf_data_iova(tx_pkt);
+
+ /* Setup PKO_SEND_HDR_S */
+ cmd_buf[nb_desc++] = tx_pkt->data_len & 0xffff;
+ cmd_buf[nb_desc++] = 0x0;
+
+ /* Enable tx checksum offload */
+ if ((flag & OCCTX_TX_OFFLOAD_OL3_OL4_CSUM_F) ||
+ (flag & OCCTX_TX_OFFLOAD_L3_L4_CSUM_F))
+ octeontx_tx_checksum_offload(cmd_buf, flag, tx_pkt);
+
+ /* SEND_HDR[DF] bit controls if buffer is to be freed or
+ * not, as SG_DESC[I] and SEND_HDR[II] are clear.
+ */
+ if (flag & OCCTX_TX_OFFLOAD_MBUF_NOFF_F)
+ cmd_buf[0] |= (octeontx_prefree_seg(tx_pkt, &m_tofree) <<
+ 58);
+
+ /* Mark mempool object as "put" since it is freed by PKO */
+ if (!(cmd_buf[0] & (1ULL << 58)))
+ RTE_MEMPOOL_CHECK_COOKIES(m_tofree->pool, (void **)&m_tofree,
+ 1, 0);
+ /* Get the gaura Id */
+ gaura_id =
+ octeontx_fpa_bufpool_gaura((uintptr_t)m_tofree->pool->pool_id);
+
+ /* Setup PKO_SEND_BUFLINK_S */
+ cmd_buf[nb_desc++] = PKO_SEND_BUFLINK_SUBDC |
+ PKO_SEND_BUFLINK_LDTYPE(0x1ull) |
+ PKO_SEND_BUFLINK_GAUAR((long)gaura_id) |
+ data_len;
+ cmd_buf[nb_desc++] = iova;
+
+ return nb_desc;
+}
+
+static __rte_always_inline uint16_t
+__octeontx_xmit_mseg_prepare(struct rte_mbuf *tx_pkt, uint64_t *cmd_buf,
+ const uint16_t flag)
+{
+ uint16_t nb_segs, nb_desc = 0;
+ uint16_t gaura_id;
+ struct rte_mbuf *m_next = NULL, *m_tofree;
+ rte_iova_t iova;
+ uint16_t data_len;
+
+ nb_segs = tx_pkt->nb_segs;
+ /* Setup PKO_SEND_HDR_S */
+ cmd_buf[nb_desc++] = tx_pkt->pkt_len & 0xffff;
+ cmd_buf[nb_desc++] = 0x0;
+
+ /* Enable tx checksum offload */
+ if ((flag & OCCTX_TX_OFFLOAD_OL3_OL4_CSUM_F) ||
+ (flag & OCCTX_TX_OFFLOAD_L3_L4_CSUM_F))
+ octeontx_tx_checksum_offload(cmd_buf, flag, tx_pkt);
+
+ do {
+ m_next = tx_pkt->next;
+ /* Get TX parameters up front, octeontx_prefree_seg might change
+ * them
+ */
+ m_tofree = tx_pkt;
+ data_len = tx_pkt->data_len;
+ iova = rte_mbuf_data_iova(tx_pkt);
+
+ /* Setup PKO_SEND_GATHER_S */
+ cmd_buf[nb_desc] = 0;
+
+ /* SG_DESC[I] bit controls if buffer is to be freed or
+ * not, as SEND_HDR[DF] and SEND_HDR[II] are clear.
+ */
+ if (flag & OCCTX_TX_OFFLOAD_MBUF_NOFF_F) {
+ cmd_buf[nb_desc] |=
+ (octeontx_prefree_seg(tx_pkt, &m_tofree) << 57);
+ }
+
+ /* To handle case where mbufs belong to diff pools, like
+ * fragmentation
+ */
+ gaura_id = octeontx_fpa_bufpool_gaura((uintptr_t)
+ m_tofree->pool->pool_id);
+
+ /* Setup PKO_SEND_GATHER_S */
+ cmd_buf[nb_desc] |= PKO_SEND_GATHER_SUBDC |
+ PKO_SEND_GATHER_LDTYPE(0x1ull) |
+ PKO_SEND_GATHER_GAUAR((long)gaura_id) |
+ data_len;
+
+ /* Mark mempool object as "put" since it is freed by
+ * PKO.
+ */
+ if (!(cmd_buf[nb_desc] & (1ULL << 57))) {
+ tx_pkt->next = NULL;
+ RTE_MEMPOOL_CHECK_COOKIES(m_tofree->pool,
+ (void **)&m_tofree, 1, 0);
+ }
+ nb_desc++;
+
+ cmd_buf[nb_desc++] = iova;
+
+ nb_segs--;
+ tx_pkt = m_next;
+ } while (nb_segs);
+
+ return nb_desc;
+}
+
+static __rte_always_inline uint16_t
+__octeontx_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
+ uint16_t nb_pkts, uint64_t *cmd_buf,
+ const uint16_t flags)
+{
+ struct octeontx_txq *txq = tx_queue;
+ octeontx_dq_t *dq = &txq->dq;
+ uint16_t count = 0, nb_desc;
+ rte_io_wmb();
+
+ while (count < nb_pkts) {
+ if (unlikely(*((volatile int64_t *)dq->fc_status_va) < 0))
+ break;
+
+ if (flags & OCCTX_TX_MULTI_SEG_F) {
+ nb_desc = __octeontx_xmit_mseg_prepare(tx_pkts[count],
+ cmd_buf, flags);
+ } else {
+ nb_desc = __octeontx_xmit_prepare(tx_pkts[count],
+ cmd_buf, flags);
+ }
+
+ octeontx_reg_lmtst(dq->lmtline_va, dq->ioreg_va, cmd_buf,
+ nb_desc);
+
+ count++;
+ }
+ return count;
+}