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