summaryrefslogtreecommitdiff
path: root/net/core/devmem.h
blob: e7ba77050b8f272ff3047a5ca12b30d4ec34a8a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Device memory TCP support
 *
 * Authors:	Mina Almasry <almasrymina@google.com>
 *		Willem de Bruijn <willemb@google.com>
 *		Kaiyuan Zhang <kaiyuanz@google.com>
 *
 */
#ifndef _NET_DEVMEM_H
#define _NET_DEVMEM_H

#include <net/netmem.h>
#include <net/netdev_netlink.h>

struct netlink_ext_ack;

struct net_devmem_dmabuf_binding {
	struct dma_buf *dmabuf;
	struct dma_buf_attachment *attachment;
	struct sg_table *sgt;
	struct net_device *dev;
	struct gen_pool *chunk_pool;
	/* Protect dev */
	struct mutex lock;

	/* The user holds a ref (via the netlink API) for as long as they want
	 * the binding to remain alive. Each page pool using this binding holds
	 * a ref to keep the binding alive. The page_pool does not release the
	 * ref until all the net_iovs allocated from this binding are released
	 * back to the page_pool.
	 *
	 * The binding undos itself and unmaps the underlying dmabuf once all
	 * those refs are dropped and the binding is no longer desired or in
	 * use.
	 *
	 * net_devmem_get_net_iov() on dmabuf net_iovs will increment this
	 * reference, making sure that the binding remains alive until all the
	 * net_iovs are no longer used. net_iovs allocated from this binding
	 * that are stuck in the TX path for any reason (such as awaiting
	 * retransmits) hold a reference to the binding until the skb holding
	 * them is freed.
	 */
	refcount_t ref;

	/* The list of bindings currently active. Used for netlink to notify us
	 * of the user dropping the bind.
	 */
	struct list_head list;

	/* rxq's this binding is active on. */
	struct xarray bound_rxqs;

	/* ID of this binding. Globally unique to all bindings currently
	 * active.
	 */
	u32 id;

	/* Array of net_iov pointers for this binding, sorted by virtual
	 * address. This array is convenient to map the virtual addresses to
	 * net_iovs in the TX path.
	 */
	struct net_iov **tx_vec;

	struct work_struct unbind_w;
};

#if defined(CONFIG_NET_DEVMEM)
/* Owner of the dma-buf chunks inserted into the gen pool. Each scatterlist
 * entry from the dmabuf is inserted into the genpool as a chunk, and needs
 * this owner struct to keep track of some metadata necessary to create
 * allocations from this chunk.
 */
struct dmabuf_genpool_chunk_owner {
	struct net_iov_area area;
	struct net_devmem_dmabuf_binding *binding;

	/* dma_addr of the start of the chunk.  */
	dma_addr_t base_dma_addr;
};

void __net_devmem_dmabuf_binding_free(struct work_struct *wq);
struct net_devmem_dmabuf_binding *
net_devmem_bind_dmabuf(struct net_device *dev,
		       enum dma_data_direction direction,
		       unsigned int dmabuf_fd, struct netdev_nl_sock *priv,
		       struct netlink_ext_ack *extack);
struct net_devmem_dmabuf_binding *net_devmem_lookup_dmabuf(u32 id);
void net_devmem_unbind_dmabuf(struct net_devmem_dmabuf_binding *binding);
int net_devmem_bind_dmabuf_to_queue(struct net_device *dev, u32 rxq_idx,
				    struct net_devmem_dmabuf_binding *binding,
				    struct netlink_ext_ack *extack);
void net_devmem_bind_tx_release(struct sock *sk);

static inline struct dmabuf_genpool_chunk_owner *
net_devmem_iov_to_chunk_owner(const struct net_iov *niov)
{
	struct net_iov_area *owner = net_iov_owner(niov);

	return container_of(owner, struct dmabuf_genpool_chunk_owner, area);
}

static inline struct net_devmem_dmabuf_binding *
net_devmem_iov_binding(const struct net_iov *niov)
{
	return net_devmem_iov_to_chunk_owner(niov)->binding;
}

static inline u32 net_devmem_iov_binding_id(const struct net_iov *niov)
{
	return net_devmem_iov_binding(niov)->id;
}

static inline unsigned long net_iov_virtual_addr(const struct net_iov *niov)
{
	struct net_iov_area *owner = net_iov_owner(niov);

	return owner->base_virtual +
	       ((unsigned long)net_iov_idx(niov) << PAGE_SHIFT);
}

static inline bool
net_devmem_dmabuf_binding_get(struct net_devmem_dmabuf_binding *binding)
{
	return refcount_inc_not_zero(&binding->ref);
}

static inline void
net_devmem_dmabuf_binding_put(struct net_devmem_dmabuf_binding *binding)
{
	if (!refcount_dec_and_test(&binding->ref))
		return;

	INIT_WORK(&binding->unbind_w, __net_devmem_dmabuf_binding_free);
	schedule_work(&binding->unbind_w);
}

void net_devmem_get_net_iov(struct net_iov *niov);
void net_devmem_put_net_iov(struct net_iov *niov);

struct net_iov *
net_devmem_alloc_dmabuf(struct net_devmem_dmabuf_binding *binding);
void net_devmem_free_dmabuf(struct net_iov *ppiov);

bool net_is_devmem_iov(struct net_iov *niov);
struct net_devmem_dmabuf_binding *
net_devmem_get_binding(struct sock *sk, unsigned int dmabuf_id);
struct net_iov *
net_devmem_get_niov_at(struct net_devmem_dmabuf_binding *binding, size_t addr,
		       size_t *off, size_t *size);

#else
struct net_devmem_dmabuf_binding;

static inline void
net_devmem_dmabuf_binding_put(struct net_devmem_dmabuf_binding *binding)
{
}

static inline void net_devmem_get_net_iov(struct net_iov *niov)
{
}

static inline void net_devmem_put_net_iov(struct net_iov *niov)
{
}

static inline void __net_devmem_dmabuf_binding_free(struct work_struct *wq)
{
}

static inline struct net_devmem_dmabuf_binding *
net_devmem_bind_dmabuf(struct net_device *dev, unsigned int dmabuf_fd,
		       enum dma_data_direction direction,
		       struct netdev_nl_sock *priv,
		       struct netlink_ext_ack *extack)
{
	return ERR_PTR(-EOPNOTSUPP);
}

static inline struct net_devmem_dmabuf_binding *net_devmem_lookup_dmabuf(u32 id)
{
	return NULL;
}

static inline void
net_devmem_unbind_dmabuf(struct net_devmem_dmabuf_binding *binding)
{
}

static inline int
net_devmem_bind_dmabuf_to_queue(struct net_device *dev, u32 rxq_idx,
				struct net_devmem_dmabuf_binding *binding,
				struct netlink_ext_ack *extack)

{
	return -EOPNOTSUPP;
}

static inline struct net_iov *
net_devmem_alloc_dmabuf(struct net_devmem_dmabuf_binding *binding)
{
	return NULL;
}

static inline void net_devmem_free_dmabuf(struct net_iov *ppiov)
{
}

static inline unsigned long net_iov_virtual_addr(const struct net_iov *niov)
{
	return 0;
}

static inline u32 net_devmem_iov_binding_id(const struct net_iov *niov)
{
	return 0;
}

static inline bool net_is_devmem_iov(struct net_iov *niov)
{
	return false;
}

static inline struct net_devmem_dmabuf_binding *
net_devmem_get_binding(struct sock *sk, unsigned int dmabuf_id)
{
	return ERR_PTR(-EOPNOTSUPP);
}

static inline struct net_iov *
net_devmem_get_niov_at(struct net_devmem_dmabuf_binding *binding, size_t addr,
		       size_t *off, size_t *size)
{
	return NULL;
}

static inline struct net_devmem_dmabuf_binding *
net_devmem_iov_binding(const struct net_iov *niov)
{
	return NULL;
}
#endif

#endif /* _NET_DEVMEM_H */