xref: /linux/net/core/devmem.h (revision 0e59523b440c90ed1899476f32ad4e949282f43e)
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 
13 #include <net/netmem.h>
14 #include <net/netdev_netlink.h>
15 
16 struct netlink_ext_ack;
17 
18 struct net_devmem_dmabuf_binding {
19 	struct net_iov_area area;
20 	struct dma_buf *dmabuf;
21 	struct dma_buf_attachment *attachment;
22 	struct sg_table *sgt;
23 	/* Physical NIC that does the actual DMA for this binding. */
24 	struct net_device *dev;
25 	/* Opaque cookie identifying the virtual device (e.g. netkit) the user
26 	 * called bind-tx on. Used only for pointer comparison. Never
27 	 * dereferenced.
28 	 */
29 	void *vdev;
30 	/* Protect dev */
31 	struct mutex lock;
32 
33 	/* The user holds a ref (via the netlink API) for as long as they want
34 	 * the binding to remain alive. Each page pool using this binding holds
35 	 * a ref to keep the binding alive. The page_pool does not release the
36 	 * ref until all the net_iovs allocated from this binding are released
37 	 * back to the page_pool.
38 	 *
39 	 * The binding undos itself and unmaps the underlying dmabuf once all
40 	 * those refs are dropped and the binding is no longer desired or in
41 	 * use.
42 	 *
43 	 * net_devmem_get_net_iov() on dmabuf net_iovs will increment this
44 	 * reference, making sure that the binding remains alive until all the
45 	 * net_iovs are no longer used. net_iovs allocated from this binding
46 	 * that are stuck in the TX path for any reason (such as awaiting
47 	 * retransmits) hold a reference to the binding until the skb holding
48 	 * them is freed.
49 	 */
50 	struct percpu_ref ref;
51 
52 	/* The list of bindings currently active. Used for netlink to notify us
53 	 * of the user dropping the bind.
54 	 */
55 	struct list_head list;
56 
57 	/* rxq's this binding is active on. */
58 	struct xarray bound_rxqs;
59 
60 	spinlock_t freelist_lock ____cacheline_aligned_in_smp;
61 	u32 free_count;
62 	u32 *freelist;
63 
64 	/* ID of this binding. Globally unique to all bindings currently
65 	 * active.
66 	 */
67 	u32 id;
68 
69 	/* DMA direction, FROM_DEVICE for Rx binding, TO_DEVICE for Tx. */
70 	enum dma_data_direction direction;
71 
72 	/* Array of net_iov pointers for this binding, sorted by virtual
73 	 * address. This array is convenient to map the virtual addresses to
74 	 * net_iovs in the TX path.
75 	 */
76 	struct net_iov **tx_vec;
77 
78 	unsigned int niov_shift;
79 
80 	struct work_struct unbind_w;
81 };
82 
83 #if defined(CONFIG_NET_DEVMEM)
84 void __net_devmem_dmabuf_binding_free(struct work_struct *wq);
85 struct net_devmem_dmabuf_binding *
86 net_devmem_bind_dmabuf(struct net_device *dev, void *vdev,
87 		       struct device *dma_dev,
88 		       enum dma_data_direction direction,
89 		       unsigned int dmabuf_fd, unsigned int niov_shift,
90 		       struct netdev_nl_sock *priv,
91 		       struct netlink_ext_ack *extack);
92 struct net_devmem_dmabuf_binding *net_devmem_lookup_dmabuf(u32 id);
93 void net_devmem_unbind_dmabuf(struct net_devmem_dmabuf_binding *binding);
94 int net_devmem_bind_dmabuf_to_queue(struct net_device *dev, u32 rxq_idx,
95 				    struct net_devmem_dmabuf_binding *binding,
96 				    struct netlink_ext_ack *extack);
97 
98 static inline struct net_devmem_dmabuf_binding *
99 net_devmem_iov_binding(const struct net_iov *niov)
100 {
101 	return container_of(net_iov_owner(niov),
102 			    struct net_devmem_dmabuf_binding, area);
103 }
104 
105 static inline u32 net_devmem_iov_binding_id(const struct net_iov *niov)
106 {
107 	return net_devmem_iov_binding(niov)->id;
108 }
109 
110 static inline unsigned long net_iov_virtual_addr(const struct net_iov *niov)
111 {
112 	struct net_devmem_dmabuf_binding *binding =
113 		net_devmem_iov_binding(niov);
114 
115 	return (unsigned long)net_iov_idx(niov) << binding->niov_shift;
116 }
117 
118 static inline bool
119 net_devmem_dmabuf_binding_get(struct net_devmem_dmabuf_binding *binding)
120 {
121 	return percpu_ref_tryget(&binding->ref);
122 }
123 
124 static inline void
125 net_devmem_dmabuf_binding_put(struct net_devmem_dmabuf_binding *binding)
126 {
127 	percpu_ref_put(&binding->ref);
128 }
129 
130 void net_devmem_get_net_iov(struct net_iov *niov);
131 void net_devmem_put_net_iov(struct net_iov *niov);
132 
133 void net_devmem_free_dmabuf(struct net_iov *ppiov);
134 
135 
136 struct net_devmem_dmabuf_binding *
137 net_devmem_get_binding(struct sock *sk, unsigned int dmabuf_id);
138 struct net_iov *
139 net_devmem_get_niov_at(struct net_devmem_dmabuf_binding *binding, size_t addr,
140 		       size_t *off, size_t *size);
141 
142 #else
143 struct net_devmem_dmabuf_binding;
144 
145 static inline void
146 net_devmem_dmabuf_binding_put(struct net_devmem_dmabuf_binding *binding)
147 {
148 }
149 
150 static inline void net_devmem_get_net_iov(struct net_iov *niov)
151 {
152 }
153 
154 static inline void net_devmem_put_net_iov(struct net_iov *niov)
155 {
156 }
157 
158 static inline struct net_devmem_dmabuf_binding *
159 net_devmem_bind_dmabuf(struct net_device *dev, void *vdev,
160 		       struct device *dma_dev,
161 		       enum dma_data_direction direction,
162 		       unsigned int dmabuf_fd,
163 		       unsigned int niov_shift,
164 		       struct netdev_nl_sock *priv,
165 		       struct netlink_ext_ack *extack)
166 {
167 	return ERR_PTR(-EOPNOTSUPP);
168 }
169 
170 static inline struct net_devmem_dmabuf_binding *net_devmem_lookup_dmabuf(u32 id)
171 {
172 	return NULL;
173 }
174 
175 static inline void
176 net_devmem_unbind_dmabuf(struct net_devmem_dmabuf_binding *binding)
177 {
178 }
179 
180 static inline int
181 net_devmem_bind_dmabuf_to_queue(struct net_device *dev, u32 rxq_idx,
182 				struct net_devmem_dmabuf_binding *binding,
183 				struct netlink_ext_ack *extack)
184 
185 {
186 	return -EOPNOTSUPP;
187 }
188 
189 static inline void net_devmem_free_dmabuf(struct net_iov *ppiov)
190 {
191 }
192 
193 static inline unsigned long net_iov_virtual_addr(const struct net_iov *niov)
194 {
195 	return 0;
196 }
197 
198 static inline u32 net_devmem_iov_binding_id(const struct net_iov *niov)
199 {
200 	return 0;
201 }
202 
203 static inline struct net_devmem_dmabuf_binding *
204 net_devmem_get_binding(struct sock *sk, unsigned int dmabuf_id)
205 {
206 	return ERR_PTR(-EOPNOTSUPP);
207 }
208 
209 static inline struct net_iov *
210 net_devmem_get_niov_at(struct net_devmem_dmabuf_binding *binding, size_t addr,
211 		       size_t *off, size_t *size)
212 {
213 	return NULL;
214 }
215 
216 static inline struct net_devmem_dmabuf_binding *
217 net_devmem_iov_binding(const struct net_iov *niov)
218 {
219 	return NULL;
220 }
221 #endif
222 
223 #endif /* _NET_DEVMEM_H */
224