xref: /openbmc/linux/net/rds/ib.h (revision 3805e6a1)
1 #ifndef _RDS_IB_H
2 #define _RDS_IB_H
3 
4 #include <rdma/ib_verbs.h>
5 #include <rdma/rdma_cm.h>
6 #include <linux/interrupt.h>
7 #include <linux/pci.h>
8 #include <linux/slab.h>
9 #include "rds.h"
10 #include "rdma_transport.h"
11 
12 #define RDS_IB_MAX_SGE			8
13 #define RDS_IB_RECV_SGE 		2
14 
15 #define RDS_IB_DEFAULT_RECV_WR		1024
16 #define RDS_IB_DEFAULT_SEND_WR		256
17 #define RDS_IB_DEFAULT_FR_WR		512
18 
19 #define RDS_IB_DEFAULT_RETRY_COUNT	2
20 
21 #define RDS_IB_SUPPORTED_PROTOCOLS	0x00000003	/* minor versions supported */
22 
23 #define RDS_IB_RECYCLE_BATCH_COUNT	32
24 
25 #define RDS_IB_WC_MAX			32
26 
27 extern struct rw_semaphore rds_ib_devices_lock;
28 extern struct list_head rds_ib_devices;
29 
30 /*
31  * IB posts RDS_FRAG_SIZE fragments of pages to the receive queues to
32  * try and minimize the amount of memory tied up both the device and
33  * socket receive queues.
34  */
35 struct rds_page_frag {
36 	struct list_head	f_item;
37 	struct list_head	f_cache_entry;
38 	struct scatterlist	f_sg;
39 };
40 
41 struct rds_ib_incoming {
42 	struct list_head	ii_frags;
43 	struct list_head	ii_cache_entry;
44 	struct rds_incoming	ii_inc;
45 };
46 
47 struct rds_ib_cache_head {
48 	struct list_head *first;
49 	unsigned long count;
50 };
51 
52 struct rds_ib_refill_cache {
53 	struct rds_ib_cache_head __percpu *percpu;
54 	struct list_head	 *xfer;
55 	struct list_head	 *ready;
56 };
57 
58 struct rds_ib_connect_private {
59 	/* Add new fields at the end, and don't permute existing fields. */
60 	__be32			dp_saddr;
61 	__be32			dp_daddr;
62 	u8			dp_protocol_major;
63 	u8			dp_protocol_minor;
64 	__be16			dp_protocol_minor_mask; /* bitmask */
65 	__be32			dp_reserved1;
66 	__be64			dp_ack_seq;
67 	__be32			dp_credit;		/* non-zero enables flow ctl */
68 };
69 
70 struct rds_ib_send_work {
71 	void			*s_op;
72 	union {
73 		struct ib_send_wr	s_wr;
74 		struct ib_rdma_wr	s_rdma_wr;
75 		struct ib_atomic_wr	s_atomic_wr;
76 	};
77 	struct ib_sge		s_sge[RDS_IB_MAX_SGE];
78 	unsigned long		s_queued;
79 };
80 
81 struct rds_ib_recv_work {
82 	struct rds_ib_incoming 	*r_ibinc;
83 	struct rds_page_frag	*r_frag;
84 	struct ib_recv_wr	r_wr;
85 	struct ib_sge		r_sge[2];
86 };
87 
88 struct rds_ib_work_ring {
89 	u32		w_nr;
90 	u32		w_alloc_ptr;
91 	u32		w_alloc_ctr;
92 	u32		w_free_ptr;
93 	atomic_t	w_free_ctr;
94 };
95 
96 /* Rings are posted with all the allocations they'll need to queue the
97  * incoming message to the receiving socket so this can't fail.
98  * All fragments start with a header, so we can make sure we're not receiving
99  * garbage, and we can tell a small 8 byte fragment from an ACK frame.
100  */
101 struct rds_ib_ack_state {
102 	u64		ack_next;
103 	u64		ack_recv;
104 	unsigned int	ack_required:1;
105 	unsigned int	ack_next_valid:1;
106 	unsigned int	ack_recv_valid:1;
107 };
108 
109 
110 struct rds_ib_device;
111 
112 struct rds_ib_connection {
113 
114 	struct list_head	ib_node;
115 	struct rds_ib_device	*rds_ibdev;
116 	struct rds_connection	*conn;
117 
118 	/* alphabet soup, IBTA style */
119 	struct rdma_cm_id	*i_cm_id;
120 	struct ib_pd		*i_pd;
121 	struct ib_cq		*i_send_cq;
122 	struct ib_cq		*i_recv_cq;
123 	struct ib_wc		i_send_wc[RDS_IB_WC_MAX];
124 	struct ib_wc		i_recv_wc[RDS_IB_WC_MAX];
125 
126 	/* To control the number of wrs from fastreg */
127 	atomic_t		i_fastreg_wrs;
128 
129 	/* interrupt handling */
130 	struct tasklet_struct	i_send_tasklet;
131 	struct tasklet_struct	i_recv_tasklet;
132 
133 	/* tx */
134 	struct rds_ib_work_ring	i_send_ring;
135 	struct rm_data_op	*i_data_op;
136 	struct rds_header	*i_send_hdrs;
137 	u64			i_send_hdrs_dma;
138 	struct rds_ib_send_work *i_sends;
139 	atomic_t		i_signaled_sends;
140 
141 	/* rx */
142 	struct mutex		i_recv_mutex;
143 	struct rds_ib_work_ring	i_recv_ring;
144 	struct rds_ib_incoming	*i_ibinc;
145 	u32			i_recv_data_rem;
146 	struct rds_header	*i_recv_hdrs;
147 	u64			i_recv_hdrs_dma;
148 	struct rds_ib_recv_work *i_recvs;
149 	u64			i_ack_recv;	/* last ACK received */
150 	struct rds_ib_refill_cache i_cache_incs;
151 	struct rds_ib_refill_cache i_cache_frags;
152 
153 	/* sending acks */
154 	unsigned long		i_ack_flags;
155 #ifdef KERNEL_HAS_ATOMIC64
156 	atomic64_t		i_ack_next;	/* next ACK to send */
157 #else
158 	spinlock_t		i_ack_lock;	/* protect i_ack_next */
159 	u64			i_ack_next;	/* next ACK to send */
160 #endif
161 	struct rds_header	*i_ack;
162 	struct ib_send_wr	i_ack_wr;
163 	struct ib_sge		i_ack_sge;
164 	u64			i_ack_dma;
165 	unsigned long		i_ack_queued;
166 
167 	/* Flow control related information
168 	 *
169 	 * Our algorithm uses a pair variables that we need to access
170 	 * atomically - one for the send credits, and one posted
171 	 * recv credits we need to transfer to remote.
172 	 * Rather than protect them using a slow spinlock, we put both into
173 	 * a single atomic_t and update it using cmpxchg
174 	 */
175 	atomic_t		i_credits;
176 
177 	/* Protocol version specific information */
178 	unsigned int		i_flowctl:1;	/* enable/disable flow ctl */
179 
180 	/* Batched completions */
181 	unsigned int		i_unsignaled_wrs;
182 };
183 
184 /* This assumes that atomic_t is at least 32 bits */
185 #define IB_GET_SEND_CREDITS(v)	((v) & 0xffff)
186 #define IB_GET_POST_CREDITS(v)	((v) >> 16)
187 #define IB_SET_SEND_CREDITS(v)	((v) & 0xffff)
188 #define IB_SET_POST_CREDITS(v)	((v) << 16)
189 
190 struct rds_ib_ipaddr {
191 	struct list_head	list;
192 	__be32			ipaddr;
193 	struct rcu_head		rcu;
194 };
195 
196 enum {
197 	RDS_IB_MR_8K_POOL,
198 	RDS_IB_MR_1M_POOL,
199 };
200 
201 struct rds_ib_device {
202 	struct list_head	list;
203 	struct list_head	ipaddr_list;
204 	struct list_head	conn_list;
205 	struct ib_device	*dev;
206 	struct ib_pd		*pd;
207 	bool                    has_fmr;
208 	bool                    has_fr;
209 	bool                    use_fastreg;
210 
211 	unsigned int		max_mrs;
212 	struct rds_ib_mr_pool	*mr_1m_pool;
213 	struct rds_ib_mr_pool   *mr_8k_pool;
214 	unsigned int		fmr_max_remaps;
215 	unsigned int		max_8k_mrs;
216 	unsigned int		max_1m_mrs;
217 	int			max_sge;
218 	unsigned int		max_wrs;
219 	unsigned int		max_initiator_depth;
220 	unsigned int		max_responder_resources;
221 	spinlock_t		spinlock;	/* protect the above */
222 	atomic_t		refcount;
223 	struct work_struct	free_work;
224 };
225 
226 #define ibdev_to_node(ibdev) dev_to_node(ibdev->dma_device)
227 #define rdsibdev_to_node(rdsibdev) ibdev_to_node(rdsibdev->dev)
228 
229 /* bits for i_ack_flags */
230 #define IB_ACK_IN_FLIGHT	0
231 #define IB_ACK_REQUESTED	1
232 
233 /* Magic WR_ID for ACKs */
234 #define RDS_IB_ACK_WR_ID	(~(u64) 0)
235 
236 struct rds_ib_statistics {
237 	uint64_t	s_ib_connect_raced;
238 	uint64_t	s_ib_listen_closed_stale;
239 	uint64_t	s_ib_evt_handler_call;
240 	uint64_t	s_ib_tasklet_call;
241 	uint64_t	s_ib_tx_cq_event;
242 	uint64_t	s_ib_tx_ring_full;
243 	uint64_t	s_ib_tx_throttle;
244 	uint64_t	s_ib_tx_sg_mapping_failure;
245 	uint64_t	s_ib_tx_stalled;
246 	uint64_t	s_ib_tx_credit_updates;
247 	uint64_t	s_ib_rx_cq_event;
248 	uint64_t	s_ib_rx_ring_empty;
249 	uint64_t	s_ib_rx_refill_from_cq;
250 	uint64_t	s_ib_rx_refill_from_thread;
251 	uint64_t	s_ib_rx_alloc_limit;
252 	uint64_t	s_ib_rx_credit_updates;
253 	uint64_t	s_ib_ack_sent;
254 	uint64_t	s_ib_ack_send_failure;
255 	uint64_t	s_ib_ack_send_delayed;
256 	uint64_t	s_ib_ack_send_piggybacked;
257 	uint64_t	s_ib_ack_received;
258 	uint64_t	s_ib_rdma_mr_8k_alloc;
259 	uint64_t	s_ib_rdma_mr_8k_free;
260 	uint64_t	s_ib_rdma_mr_8k_used;
261 	uint64_t	s_ib_rdma_mr_8k_pool_flush;
262 	uint64_t	s_ib_rdma_mr_8k_pool_wait;
263 	uint64_t	s_ib_rdma_mr_8k_pool_depleted;
264 	uint64_t	s_ib_rdma_mr_1m_alloc;
265 	uint64_t	s_ib_rdma_mr_1m_free;
266 	uint64_t	s_ib_rdma_mr_1m_used;
267 	uint64_t	s_ib_rdma_mr_1m_pool_flush;
268 	uint64_t	s_ib_rdma_mr_1m_pool_wait;
269 	uint64_t	s_ib_rdma_mr_1m_pool_depleted;
270 	uint64_t	s_ib_rdma_mr_8k_reused;
271 	uint64_t	s_ib_rdma_mr_1m_reused;
272 	uint64_t	s_ib_atomic_cswp;
273 	uint64_t	s_ib_atomic_fadd;
274 };
275 
276 extern struct workqueue_struct *rds_ib_wq;
277 
278 /*
279  * Fake ib_dma_sync_sg_for_{cpu,device} as long as ib_verbs.h
280  * doesn't define it.
281  */
282 static inline void rds_ib_dma_sync_sg_for_cpu(struct ib_device *dev,
283 					      struct scatterlist *sglist,
284 					      unsigned int sg_dma_len,
285 					      int direction)
286 {
287 	struct scatterlist *sg;
288 	unsigned int i;
289 
290 	for_each_sg(sglist, sg, sg_dma_len, i) {
291 		ib_dma_sync_single_for_cpu(dev,
292 				ib_sg_dma_address(dev, sg),
293 				ib_sg_dma_len(dev, sg),
294 				direction);
295 	}
296 }
297 #define ib_dma_sync_sg_for_cpu	rds_ib_dma_sync_sg_for_cpu
298 
299 static inline void rds_ib_dma_sync_sg_for_device(struct ib_device *dev,
300 						 struct scatterlist *sglist,
301 						 unsigned int sg_dma_len,
302 						 int direction)
303 {
304 	struct scatterlist *sg;
305 	unsigned int i;
306 
307 	for_each_sg(sglist, sg, sg_dma_len, i) {
308 		ib_dma_sync_single_for_device(dev,
309 				ib_sg_dma_address(dev, sg),
310 				ib_sg_dma_len(dev, sg),
311 				direction);
312 	}
313 }
314 #define ib_dma_sync_sg_for_device	rds_ib_dma_sync_sg_for_device
315 
316 
317 /* ib.c */
318 extern struct rds_transport rds_ib_transport;
319 struct rds_ib_device *rds_ib_get_client_data(struct ib_device *device);
320 void rds_ib_dev_put(struct rds_ib_device *rds_ibdev);
321 extern struct ib_client rds_ib_client;
322 
323 extern unsigned int rds_ib_retry_count;
324 
325 extern spinlock_t ib_nodev_conns_lock;
326 extern struct list_head ib_nodev_conns;
327 
328 /* ib_cm.c */
329 int rds_ib_conn_alloc(struct rds_connection *conn, gfp_t gfp);
330 void rds_ib_conn_free(void *arg);
331 int rds_ib_conn_connect(struct rds_connection *conn);
332 void rds_ib_conn_shutdown(struct rds_connection *conn);
333 void rds_ib_state_change(struct sock *sk);
334 int rds_ib_listen_init(void);
335 void rds_ib_listen_stop(void);
336 void __rds_ib_conn_error(struct rds_connection *conn, const char *, ...);
337 int rds_ib_cm_handle_connect(struct rdma_cm_id *cm_id,
338 			     struct rdma_cm_event *event);
339 int rds_ib_cm_initiate_connect(struct rdma_cm_id *cm_id);
340 void rds_ib_cm_connect_complete(struct rds_connection *conn,
341 				struct rdma_cm_event *event);
342 
343 
344 #define rds_ib_conn_error(conn, fmt...) \
345 	__rds_ib_conn_error(conn, KERN_WARNING "RDS/IB: " fmt)
346 
347 /* ib_rdma.c */
348 int rds_ib_update_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr);
349 void rds_ib_add_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn);
350 void rds_ib_remove_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn);
351 void rds_ib_destroy_nodev_conns(void);
352 void rds_ib_mr_cqe_handler(struct rds_ib_connection *ic, struct ib_wc *wc);
353 
354 /* ib_recv.c */
355 int rds_ib_recv_init(void);
356 void rds_ib_recv_exit(void);
357 int rds_ib_recv(struct rds_connection *conn);
358 int rds_ib_recv_alloc_caches(struct rds_ib_connection *ic);
359 void rds_ib_recv_free_caches(struct rds_ib_connection *ic);
360 void rds_ib_recv_refill(struct rds_connection *conn, int prefill, gfp_t gfp);
361 void rds_ib_inc_free(struct rds_incoming *inc);
362 int rds_ib_inc_copy_to_user(struct rds_incoming *inc, struct iov_iter *to);
363 void rds_ib_recv_cqe_handler(struct rds_ib_connection *ic, struct ib_wc *wc,
364 			     struct rds_ib_ack_state *state);
365 void rds_ib_recv_tasklet_fn(unsigned long data);
366 void rds_ib_recv_init_ring(struct rds_ib_connection *ic);
367 void rds_ib_recv_clear_ring(struct rds_ib_connection *ic);
368 void rds_ib_recv_init_ack(struct rds_ib_connection *ic);
369 void rds_ib_attempt_ack(struct rds_ib_connection *ic);
370 void rds_ib_ack_send_complete(struct rds_ib_connection *ic);
371 u64 rds_ib_piggyb_ack(struct rds_ib_connection *ic);
372 void rds_ib_set_ack(struct rds_ib_connection *ic, u64 seq, int ack_required);
373 
374 /* ib_ring.c */
375 void rds_ib_ring_init(struct rds_ib_work_ring *ring, u32 nr);
376 void rds_ib_ring_resize(struct rds_ib_work_ring *ring, u32 nr);
377 u32 rds_ib_ring_alloc(struct rds_ib_work_ring *ring, u32 val, u32 *pos);
378 void rds_ib_ring_free(struct rds_ib_work_ring *ring, u32 val);
379 void rds_ib_ring_unalloc(struct rds_ib_work_ring *ring, u32 val);
380 int rds_ib_ring_empty(struct rds_ib_work_ring *ring);
381 int rds_ib_ring_low(struct rds_ib_work_ring *ring);
382 u32 rds_ib_ring_oldest(struct rds_ib_work_ring *ring);
383 u32 rds_ib_ring_completed(struct rds_ib_work_ring *ring, u32 wr_id, u32 oldest);
384 extern wait_queue_head_t rds_ib_ring_empty_wait;
385 
386 /* ib_send.c */
387 void rds_ib_xmit_complete(struct rds_connection *conn);
388 int rds_ib_xmit(struct rds_connection *conn, struct rds_message *rm,
389 		unsigned int hdr_off, unsigned int sg, unsigned int off);
390 void rds_ib_send_cqe_handler(struct rds_ib_connection *ic, struct ib_wc *wc);
391 void rds_ib_send_init_ring(struct rds_ib_connection *ic);
392 void rds_ib_send_clear_ring(struct rds_ib_connection *ic);
393 int rds_ib_xmit_rdma(struct rds_connection *conn, struct rm_rdma_op *op);
394 void rds_ib_send_add_credits(struct rds_connection *conn, unsigned int credits);
395 void rds_ib_advertise_credits(struct rds_connection *conn, unsigned int posted);
396 int rds_ib_send_grab_credits(struct rds_ib_connection *ic, u32 wanted,
397 			     u32 *adv_credits, int need_posted, int max_posted);
398 int rds_ib_xmit_atomic(struct rds_connection *conn, struct rm_atomic_op *op);
399 
400 /* ib_stats.c */
401 DECLARE_PER_CPU(struct rds_ib_statistics, rds_ib_stats);
402 #define rds_ib_stats_inc(member) rds_stats_inc_which(rds_ib_stats, member)
403 unsigned int rds_ib_stats_info_copy(struct rds_info_iterator *iter,
404 				    unsigned int avail);
405 
406 /* ib_sysctl.c */
407 int rds_ib_sysctl_init(void);
408 void rds_ib_sysctl_exit(void);
409 extern unsigned long rds_ib_sysctl_max_send_wr;
410 extern unsigned long rds_ib_sysctl_max_recv_wr;
411 extern unsigned long rds_ib_sysctl_max_unsig_wrs;
412 extern unsigned long rds_ib_sysctl_max_unsig_bytes;
413 extern unsigned long rds_ib_sysctl_max_recv_allocation;
414 extern unsigned int rds_ib_sysctl_flow_control;
415 
416 #endif
417