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