tx.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /* Copyright (C) 2024-2025 Intel Corporation */
  3. #ifndef __LIBETH_TX_H
  4. #define __LIBETH_TX_H
  5. #include <linux/skbuff.h>
  6. #include <net/libeth/types.h>
  7. /* Tx buffer completion */
  8. /**
  9. * enum libeth_sqe_type - type of &libeth_sqe to act on Tx completion
  10. * @LIBETH_SQE_EMPTY: unused/empty OR XDP_TX/XSk frame, no action required
  11. * @LIBETH_SQE_CTX: context descriptor with empty SQE, no action required
  12. * @LIBETH_SQE_SLAB: kmalloc-allocated buffer, unmap and kfree()
  13. * @LIBETH_SQE_FRAG: mapped skb frag, only unmap DMA
  14. * @LIBETH_SQE_SKB: &sk_buff, unmap and napi_consume_skb(), update stats
  15. * @__LIBETH_SQE_XDP_START: separator between skb and XDP types
  16. * @LIBETH_SQE_XDP_TX: &skb_shared_info, libeth_xdp_return_buff_bulk(), stats
  17. * @LIBETH_SQE_XDP_XMIT: &xdp_frame, unmap and xdp_return_frame_bulk(), stats
  18. * @LIBETH_SQE_XDP_XMIT_FRAG: &xdp_frame frag, only unmap DMA
  19. * @LIBETH_SQE_XSK_TX: &libeth_xdp_buff on XSk queue, xsk_buff_free(), stats
  20. * @LIBETH_SQE_XSK_TX_FRAG: &libeth_xdp_buff frag on XSk queue, xsk_buff_free()
  21. */
  22. enum libeth_sqe_type {
  23. LIBETH_SQE_EMPTY = 0U,
  24. LIBETH_SQE_CTX,
  25. LIBETH_SQE_SLAB,
  26. LIBETH_SQE_FRAG,
  27. LIBETH_SQE_SKB,
  28. __LIBETH_SQE_XDP_START,
  29. LIBETH_SQE_XDP_TX = __LIBETH_SQE_XDP_START,
  30. LIBETH_SQE_XDP_XMIT,
  31. LIBETH_SQE_XDP_XMIT_FRAG,
  32. LIBETH_SQE_XSK_TX,
  33. LIBETH_SQE_XSK_TX_FRAG,
  34. };
  35. /**
  36. * struct libeth_sqe - represents a Send Queue Element / Tx buffer
  37. * @type: type of the buffer, see the enum above
  38. * @rs_idx: index of the last buffer from the batch this one was sent in
  39. * @raw: slab buffer to free via kfree()
  40. * @skb: &sk_buff to consume
  41. * @sinfo: skb shared info of an XDP_TX frame
  42. * @xdpf: XDP frame from ::ndo_xdp_xmit()
  43. * @xsk: XSk Rx frame from XDP_TX action
  44. * @dma: DMA address to unmap
  45. * @len: length of the mapped region to unmap
  46. * @nr_frags: number of frags in the frame this buffer belongs to
  47. * @packets: number of physical packets sent for this frame
  48. * @bytes: number of physical bytes sent for this frame
  49. * @priv: driver-private scratchpad
  50. */
  51. struct libeth_sqe {
  52. enum libeth_sqe_type type:32;
  53. u32 rs_idx;
  54. union {
  55. void *raw;
  56. struct sk_buff *skb;
  57. struct skb_shared_info *sinfo;
  58. struct xdp_frame *xdpf;
  59. struct libeth_xdp_buff *xsk;
  60. };
  61. DEFINE_DMA_UNMAP_ADDR(dma);
  62. DEFINE_DMA_UNMAP_LEN(len);
  63. u32 nr_frags;
  64. u32 packets;
  65. u32 bytes;
  66. unsigned long priv;
  67. } __aligned_largest;
  68. /**
  69. * LIBETH_SQE_CHECK_PRIV - check the driver's private SQE data
  70. * @p: type or name of the object the driver wants to fit into &libeth_sqe
  71. *
  72. * Make sure the driver's private data fits into libeth_sqe::priv. To be used
  73. * right after its declaration.
  74. */
  75. #define LIBETH_SQE_CHECK_PRIV(p) \
  76. static_assert(sizeof(p) <= sizeof_field(struct libeth_sqe, priv))
  77. /**
  78. * struct libeth_cq_pp - completion queue poll params
  79. * @dev: &device to perform DMA unmapping
  80. * @bq: XDP frame bulk to combine return operations
  81. * @ss: onstack NAPI stats to fill
  82. * @xss: onstack XDPSQ NAPI stats to fill
  83. * @xdp_tx: number of XDP-not-XSk frames processed
  84. * @napi: whether it's called from the NAPI context
  85. *
  86. * libeth uses this structure to access objects needed for performing full
  87. * Tx complete operation without passing lots of arguments and change the
  88. * prototypes each time a new one is added.
  89. */
  90. struct libeth_cq_pp {
  91. struct device *dev;
  92. struct xdp_frame_bulk *bq;
  93. union {
  94. struct libeth_sq_napi_stats *ss;
  95. struct libeth_xdpsq_napi_stats *xss;
  96. };
  97. u32 xdp_tx;
  98. bool napi;
  99. };
  100. /**
  101. * libeth_tx_complete - perform Tx completion for one SQE
  102. * @sqe: SQE to complete
  103. * @cp: poll params
  104. *
  105. * Do Tx complete for all the types of buffers, incl. freeing, unmapping,
  106. * updating the stats etc.
  107. */
  108. static inline void libeth_tx_complete(struct libeth_sqe *sqe,
  109. const struct libeth_cq_pp *cp)
  110. {
  111. switch (sqe->type) {
  112. case LIBETH_SQE_EMPTY:
  113. return;
  114. case LIBETH_SQE_SKB:
  115. case LIBETH_SQE_FRAG:
  116. case LIBETH_SQE_SLAB:
  117. dma_unmap_page(cp->dev, dma_unmap_addr(sqe, dma),
  118. dma_unmap_len(sqe, len), DMA_TO_DEVICE);
  119. break;
  120. default:
  121. break;
  122. }
  123. switch (sqe->type) {
  124. case LIBETH_SQE_SKB:
  125. cp->ss->packets += sqe->packets;
  126. cp->ss->bytes += sqe->bytes;
  127. napi_consume_skb(sqe->skb, cp->napi);
  128. break;
  129. case LIBETH_SQE_SLAB:
  130. kfree(sqe->raw);
  131. break;
  132. default:
  133. break;
  134. }
  135. sqe->type = LIBETH_SQE_EMPTY;
  136. }
  137. void libeth_tx_complete_any(struct libeth_sqe *sqe, struct libeth_cq_pp *cp);
  138. #endif /* __LIBETH_TX_H */