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