devmem.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Device memory TCP support
  4. *
  5. * Authors: Mina Almasry <almasrymina@google.com>
  6. * Willem de Bruijn <willemb@google.com>
  7. * Kaiyuan Zhang <kaiyuanz@google.com>
  8. *
  9. */
  10. #ifndef _NET_DEVMEM_H
  11. #define _NET_DEVMEM_H
  12. #include <net/netmem.h>
  13. #include <net/netdev_netlink.h>
  14. struct netlink_ext_ack;
  15. struct net_devmem_dmabuf_binding {
  16. struct dma_buf *dmabuf;
  17. struct dma_buf_attachment *attachment;
  18. struct sg_table *sgt;
  19. struct net_device *dev;
  20. struct gen_pool *chunk_pool;
  21. /* Protect dev */
  22. struct mutex lock;
  23. /* The user holds a ref (via the netlink API) for as long as they want
  24. * the binding to remain alive. Each page pool using this binding holds
  25. * a ref to keep the binding alive. The page_pool does not release the
  26. * ref until all the net_iovs allocated from this binding are released
  27. * back to the page_pool.
  28. *
  29. * The binding undos itself and unmaps the underlying dmabuf once all
  30. * those refs are dropped and the binding is no longer desired or in
  31. * use.
  32. *
  33. * net_devmem_get_net_iov() on dmabuf net_iovs will increment this
  34. * reference, making sure that the binding remains alive until all the
  35. * net_iovs are no longer used. net_iovs allocated from this binding
  36. * that are stuck in the TX path for any reason (such as awaiting
  37. * retransmits) hold a reference to the binding until the skb holding
  38. * them is freed.
  39. */
  40. struct percpu_ref ref;
  41. /* The list of bindings currently active. Used for netlink to notify us
  42. * of the user dropping the bind.
  43. */
  44. struct list_head list;
  45. /* rxq's this binding is active on. */
  46. struct xarray bound_rxqs;
  47. /* ID of this binding. Globally unique to all bindings currently
  48. * active.
  49. */
  50. u32 id;
  51. /* DMA direction, FROM_DEVICE for Rx binding, TO_DEVICE for Tx. */
  52. enum dma_data_direction direction;
  53. /* Array of net_iov pointers for this binding, sorted by virtual
  54. * address. This array is convenient to map the virtual addresses to
  55. * net_iovs in the TX path.
  56. */
  57. struct net_iov **tx_vec;
  58. struct work_struct unbind_w;
  59. };
  60. #if defined(CONFIG_NET_DEVMEM)
  61. /* Owner of the dma-buf chunks inserted into the gen pool. Each scatterlist
  62. * entry from the dmabuf is inserted into the genpool as a chunk, and needs
  63. * this owner struct to keep track of some metadata necessary to create
  64. * allocations from this chunk.
  65. */
  66. struct dmabuf_genpool_chunk_owner {
  67. struct net_iov_area area;
  68. struct net_devmem_dmabuf_binding *binding;
  69. /* dma_addr of the start of the chunk. */
  70. dma_addr_t base_dma_addr;
  71. };
  72. void __net_devmem_dmabuf_binding_free(struct work_struct *wq);
  73. struct net_devmem_dmabuf_binding *
  74. net_devmem_bind_dmabuf(struct net_device *dev,
  75. struct device *dma_dev,
  76. enum dma_data_direction direction,
  77. unsigned int dmabuf_fd, struct netdev_nl_sock *priv,
  78. struct netlink_ext_ack *extack);
  79. struct net_devmem_dmabuf_binding *net_devmem_lookup_dmabuf(u32 id);
  80. void net_devmem_unbind_dmabuf(struct net_devmem_dmabuf_binding *binding);
  81. int net_devmem_bind_dmabuf_to_queue(struct net_device *dev, u32 rxq_idx,
  82. struct net_devmem_dmabuf_binding *binding,
  83. struct netlink_ext_ack *extack);
  84. static inline struct dmabuf_genpool_chunk_owner *
  85. net_devmem_iov_to_chunk_owner(const struct net_iov *niov)
  86. {
  87. struct net_iov_area *owner = net_iov_owner(niov);
  88. return container_of(owner, struct dmabuf_genpool_chunk_owner, area);
  89. }
  90. static inline struct net_devmem_dmabuf_binding *
  91. net_devmem_iov_binding(const struct net_iov *niov)
  92. {
  93. return net_devmem_iov_to_chunk_owner(niov)->binding;
  94. }
  95. static inline u32 net_devmem_iov_binding_id(const struct net_iov *niov)
  96. {
  97. return net_devmem_iov_binding(niov)->id;
  98. }
  99. static inline unsigned long net_iov_virtual_addr(const struct net_iov *niov)
  100. {
  101. struct net_iov_area *owner = net_iov_owner(niov);
  102. return owner->base_virtual +
  103. ((unsigned long)net_iov_idx(niov) << PAGE_SHIFT);
  104. }
  105. static inline bool
  106. net_devmem_dmabuf_binding_get(struct net_devmem_dmabuf_binding *binding)
  107. {
  108. return percpu_ref_tryget(&binding->ref);
  109. }
  110. static inline void
  111. net_devmem_dmabuf_binding_put(struct net_devmem_dmabuf_binding *binding)
  112. {
  113. percpu_ref_put(&binding->ref);
  114. }
  115. void net_devmem_get_net_iov(struct net_iov *niov);
  116. void net_devmem_put_net_iov(struct net_iov *niov);
  117. struct net_iov *
  118. net_devmem_alloc_dmabuf(struct net_devmem_dmabuf_binding *binding);
  119. void net_devmem_free_dmabuf(struct net_iov *ppiov);
  120. struct net_devmem_dmabuf_binding *
  121. net_devmem_get_binding(struct sock *sk, unsigned int dmabuf_id);
  122. struct net_iov *
  123. net_devmem_get_niov_at(struct net_devmem_dmabuf_binding *binding, size_t addr,
  124. size_t *off, size_t *size);
  125. #else
  126. struct net_devmem_dmabuf_binding;
  127. static inline void
  128. net_devmem_dmabuf_binding_put(struct net_devmem_dmabuf_binding *binding)
  129. {
  130. }
  131. static inline void net_devmem_get_net_iov(struct net_iov *niov)
  132. {
  133. }
  134. static inline void net_devmem_put_net_iov(struct net_iov *niov)
  135. {
  136. }
  137. static inline struct net_devmem_dmabuf_binding *
  138. net_devmem_bind_dmabuf(struct net_device *dev,
  139. struct device *dma_dev,
  140. enum dma_data_direction direction,
  141. unsigned int dmabuf_fd,
  142. struct netdev_nl_sock *priv,
  143. struct netlink_ext_ack *extack)
  144. {
  145. return ERR_PTR(-EOPNOTSUPP);
  146. }
  147. static inline struct net_devmem_dmabuf_binding *net_devmem_lookup_dmabuf(u32 id)
  148. {
  149. return NULL;
  150. }
  151. static inline void
  152. net_devmem_unbind_dmabuf(struct net_devmem_dmabuf_binding *binding)
  153. {
  154. }
  155. static inline int
  156. net_devmem_bind_dmabuf_to_queue(struct net_device *dev, u32 rxq_idx,
  157. struct net_devmem_dmabuf_binding *binding,
  158. struct netlink_ext_ack *extack)
  159. {
  160. return -EOPNOTSUPP;
  161. }
  162. static inline struct net_iov *
  163. net_devmem_alloc_dmabuf(struct net_devmem_dmabuf_binding *binding)
  164. {
  165. return NULL;
  166. }
  167. static inline void net_devmem_free_dmabuf(struct net_iov *ppiov)
  168. {
  169. }
  170. static inline unsigned long net_iov_virtual_addr(const struct net_iov *niov)
  171. {
  172. return 0;
  173. }
  174. static inline u32 net_devmem_iov_binding_id(const struct net_iov *niov)
  175. {
  176. return 0;
  177. }
  178. static inline struct net_devmem_dmabuf_binding *
  179. net_devmem_get_binding(struct sock *sk, unsigned int dmabuf_id)
  180. {
  181. return ERR_PTR(-EOPNOTSUPP);
  182. }
  183. static inline struct net_iov *
  184. net_devmem_get_niov_at(struct net_devmem_dmabuf_binding *binding, size_t addr,
  185. size_t *off, size_t *size)
  186. {
  187. return NULL;
  188. }
  189. static inline struct net_devmem_dmabuf_binding *
  190. net_devmem_iov_binding(const struct net_iov *niov)
  191. {
  192. return NULL;
  193. }
  194. #endif
  195. #endif /* _NET_DEVMEM_H */