IB: split struct ib_send_wr
[firefly-linux-kernel-4.4.55.git] / net / rds / ib.h
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_FMR_SIZE                    256
13 #define RDS_FMR_POOL_SIZE               8192
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
21 #define RDS_IB_DEFAULT_RETRY_COUNT      2
22
23 #define RDS_IB_SUPPORTED_PROTOCOLS      0x00000003      /* minor versions supported */
24
25 #define RDS_IB_RECYCLE_BATCH_COUNT      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 struct rds_ib_device;
97
98 struct rds_ib_connection {
99
100         struct list_head        ib_node;
101         struct rds_ib_device    *rds_ibdev;
102         struct rds_connection   *conn;
103
104         /* alphabet soup, IBTA style */
105         struct rdma_cm_id       *i_cm_id;
106         struct ib_pd            *i_pd;
107         struct ib_cq            *i_send_cq;
108         struct ib_cq            *i_recv_cq;
109
110         /* tx */
111         struct rds_ib_work_ring i_send_ring;
112         struct rm_data_op       *i_data_op;
113         struct rds_header       *i_send_hdrs;
114         u64                     i_send_hdrs_dma;
115         struct rds_ib_send_work *i_sends;
116         atomic_t                i_signaled_sends;
117
118         /* rx */
119         struct tasklet_struct   i_recv_tasklet;
120         struct mutex            i_recv_mutex;
121         struct rds_ib_work_ring i_recv_ring;
122         struct rds_ib_incoming  *i_ibinc;
123         u32                     i_recv_data_rem;
124         struct rds_header       *i_recv_hdrs;
125         u64                     i_recv_hdrs_dma;
126         struct rds_ib_recv_work *i_recvs;
127         u64                     i_ack_recv;     /* last ACK received */
128         struct rds_ib_refill_cache i_cache_incs;
129         struct rds_ib_refill_cache i_cache_frags;
130
131         /* sending acks */
132         unsigned long           i_ack_flags;
133 #ifdef KERNEL_HAS_ATOMIC64
134         atomic64_t              i_ack_next;     /* next ACK to send */
135 #else
136         spinlock_t              i_ack_lock;     /* protect i_ack_next */
137         u64                     i_ack_next;     /* next ACK to send */
138 #endif
139         struct rds_header       *i_ack;
140         struct ib_send_wr       i_ack_wr;
141         struct ib_sge           i_ack_sge;
142         u64                     i_ack_dma;
143         unsigned long           i_ack_queued;
144
145         /* Flow control related information
146          *
147          * Our algorithm uses a pair variables that we need to access
148          * atomically - one for the send credits, and one posted
149          * recv credits we need to transfer to remote.
150          * Rather than protect them using a slow spinlock, we put both into
151          * a single atomic_t and update it using cmpxchg
152          */
153         atomic_t                i_credits;
154
155         /* Protocol version specific information */
156         unsigned int            i_flowctl:1;    /* enable/disable flow ctl */
157
158         /* Batched completions */
159         unsigned int            i_unsignaled_wrs;
160 };
161
162 /* This assumes that atomic_t is at least 32 bits */
163 #define IB_GET_SEND_CREDITS(v)  ((v) & 0xffff)
164 #define IB_GET_POST_CREDITS(v)  ((v) >> 16)
165 #define IB_SET_SEND_CREDITS(v)  ((v) & 0xffff)
166 #define IB_SET_POST_CREDITS(v)  ((v) << 16)
167
168 struct rds_ib_ipaddr {
169         struct list_head        list;
170         __be32                  ipaddr;
171 };
172
173 struct rds_ib_device {
174         struct list_head        list;
175         struct list_head        ipaddr_list;
176         struct list_head        conn_list;
177         struct ib_device        *dev;
178         struct ib_pd            *pd;
179         struct rds_ib_mr_pool   *mr_pool;
180         unsigned int            fmr_max_remaps;
181         unsigned int            max_fmrs;
182         int                     max_sge;
183         unsigned int            max_wrs;
184         unsigned int            max_initiator_depth;
185         unsigned int            max_responder_resources;
186         spinlock_t              spinlock;       /* protect the above */
187         atomic_t                refcount;
188         struct work_struct      free_work;
189 };
190
191 #define ibdev_to_node(ibdev) dev_to_node(ibdev->dma_device)
192 #define rdsibdev_to_node(rdsibdev) ibdev_to_node(rdsibdev->dev)
193
194 /* bits for i_ack_flags */
195 #define IB_ACK_IN_FLIGHT        0
196 #define IB_ACK_REQUESTED        1
197
198 /* Magic WR_ID for ACKs */
199 #define RDS_IB_ACK_WR_ID        (~(u64) 0)
200
201 struct rds_ib_statistics {
202         uint64_t        s_ib_connect_raced;
203         uint64_t        s_ib_listen_closed_stale;
204         uint64_t        s_ib_tx_cq_call;
205         uint64_t        s_ib_tx_cq_event;
206         uint64_t        s_ib_tx_ring_full;
207         uint64_t        s_ib_tx_throttle;
208         uint64_t        s_ib_tx_sg_mapping_failure;
209         uint64_t        s_ib_tx_stalled;
210         uint64_t        s_ib_tx_credit_updates;
211         uint64_t        s_ib_rx_cq_call;
212         uint64_t        s_ib_rx_cq_event;
213         uint64_t        s_ib_rx_ring_empty;
214         uint64_t        s_ib_rx_refill_from_cq;
215         uint64_t        s_ib_rx_refill_from_thread;
216         uint64_t        s_ib_rx_alloc_limit;
217         uint64_t        s_ib_rx_credit_updates;
218         uint64_t        s_ib_ack_sent;
219         uint64_t        s_ib_ack_send_failure;
220         uint64_t        s_ib_ack_send_delayed;
221         uint64_t        s_ib_ack_send_piggybacked;
222         uint64_t        s_ib_ack_received;
223         uint64_t        s_ib_rdma_mr_alloc;
224         uint64_t        s_ib_rdma_mr_free;
225         uint64_t        s_ib_rdma_mr_used;
226         uint64_t        s_ib_rdma_mr_pool_flush;
227         uint64_t        s_ib_rdma_mr_pool_wait;
228         uint64_t        s_ib_rdma_mr_pool_depleted;
229         uint64_t        s_ib_atomic_cswp;
230         uint64_t        s_ib_atomic_fadd;
231 };
232
233 extern struct workqueue_struct *rds_ib_wq;
234
235 /*
236  * Fake ib_dma_sync_sg_for_{cpu,device} as long as ib_verbs.h
237  * doesn't define it.
238  */
239 static inline void rds_ib_dma_sync_sg_for_cpu(struct ib_device *dev,
240                                               struct scatterlist *sglist,
241                                               unsigned int sg_dma_len,
242                                               int direction)
243 {
244         struct scatterlist *sg;
245         unsigned int i;
246
247         for_each_sg(sglist, sg, sg_dma_len, i) {
248                 ib_dma_sync_single_for_cpu(dev,
249                                 ib_sg_dma_address(dev, sg),
250                                 ib_sg_dma_len(dev, sg),
251                                 direction);
252         }
253 }
254 #define ib_dma_sync_sg_for_cpu  rds_ib_dma_sync_sg_for_cpu
255
256 static inline void rds_ib_dma_sync_sg_for_device(struct ib_device *dev,
257                                                  struct scatterlist *sglist,
258                                                  unsigned int sg_dma_len,
259                                                  int direction)
260 {
261         struct scatterlist *sg;
262         unsigned int i;
263
264         for_each_sg(sglist, sg, sg_dma_len, i) {
265                 ib_dma_sync_single_for_device(dev,
266                                 ib_sg_dma_address(dev, sg),
267                                 ib_sg_dma_len(dev, sg),
268                                 direction);
269         }
270 }
271 #define ib_dma_sync_sg_for_device       rds_ib_dma_sync_sg_for_device
272
273
274 /* ib.c */
275 extern struct rds_transport rds_ib_transport;
276 struct rds_ib_device *rds_ib_get_client_data(struct ib_device *device);
277 void rds_ib_dev_put(struct rds_ib_device *rds_ibdev);
278 extern struct ib_client rds_ib_client;
279
280 extern unsigned int fmr_message_size;
281 extern unsigned int rds_ib_retry_count;
282
283 extern spinlock_t ib_nodev_conns_lock;
284 extern struct list_head ib_nodev_conns;
285
286 /* ib_cm.c */
287 int rds_ib_conn_alloc(struct rds_connection *conn, gfp_t gfp);
288 void rds_ib_conn_free(void *arg);
289 int rds_ib_conn_connect(struct rds_connection *conn);
290 void rds_ib_conn_shutdown(struct rds_connection *conn);
291 void rds_ib_state_change(struct sock *sk);
292 int rds_ib_listen_init(void);
293 void rds_ib_listen_stop(void);
294 void __rds_ib_conn_error(struct rds_connection *conn, const char *, ...);
295 int rds_ib_cm_handle_connect(struct rdma_cm_id *cm_id,
296                              struct rdma_cm_event *event);
297 int rds_ib_cm_initiate_connect(struct rdma_cm_id *cm_id);
298 void rds_ib_cm_connect_complete(struct rds_connection *conn,
299                                 struct rdma_cm_event *event);
300
301
302 #define rds_ib_conn_error(conn, fmt...) \
303         __rds_ib_conn_error(conn, KERN_WARNING "RDS/IB: " fmt)
304
305 /* ib_rdma.c */
306 int rds_ib_update_ipaddr(struct rds_ib_device *rds_ibdev, __be32 ipaddr);
307 void rds_ib_add_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn);
308 void rds_ib_remove_conn(struct rds_ib_device *rds_ibdev, struct rds_connection *conn);
309 void rds_ib_destroy_nodev_conns(void);
310 struct rds_ib_mr_pool *rds_ib_create_mr_pool(struct rds_ib_device *);
311 void rds_ib_get_mr_info(struct rds_ib_device *rds_ibdev, struct rds_info_rdma_connection *iinfo);
312 void rds_ib_destroy_mr_pool(struct rds_ib_mr_pool *);
313 void *rds_ib_get_mr(struct scatterlist *sg, unsigned long nents,
314                     struct rds_sock *rs, u32 *key_ret);
315 void rds_ib_sync_mr(void *trans_private, int dir);
316 void rds_ib_free_mr(void *trans_private, int invalidate);
317 void rds_ib_flush_mrs(void);
318 int rds_ib_fmr_init(void);
319 void rds_ib_fmr_exit(void);
320
321 /* ib_recv.c */
322 int rds_ib_recv_init(void);
323 void rds_ib_recv_exit(void);
324 int rds_ib_recv(struct rds_connection *conn);
325 int rds_ib_recv_alloc_caches(struct rds_ib_connection *ic);
326 void rds_ib_recv_free_caches(struct rds_ib_connection *ic);
327 void rds_ib_recv_refill(struct rds_connection *conn, int prefill, gfp_t gfp);
328 void rds_ib_inc_free(struct rds_incoming *inc);
329 int rds_ib_inc_copy_to_user(struct rds_incoming *inc, struct iov_iter *to);
330 void rds_ib_recv_cq_comp_handler(struct ib_cq *cq, void *context);
331 void rds_ib_recv_tasklet_fn(unsigned long data);
332 void rds_ib_recv_init_ring(struct rds_ib_connection *ic);
333 void rds_ib_recv_clear_ring(struct rds_ib_connection *ic);
334 void rds_ib_recv_init_ack(struct rds_ib_connection *ic);
335 void rds_ib_attempt_ack(struct rds_ib_connection *ic);
336 void rds_ib_ack_send_complete(struct rds_ib_connection *ic);
337 u64 rds_ib_piggyb_ack(struct rds_ib_connection *ic);
338
339 /* ib_ring.c */
340 void rds_ib_ring_init(struct rds_ib_work_ring *ring, u32 nr);
341 void rds_ib_ring_resize(struct rds_ib_work_ring *ring, u32 nr);
342 u32 rds_ib_ring_alloc(struct rds_ib_work_ring *ring, u32 val, u32 *pos);
343 void rds_ib_ring_free(struct rds_ib_work_ring *ring, u32 val);
344 void rds_ib_ring_unalloc(struct rds_ib_work_ring *ring, u32 val);
345 int rds_ib_ring_empty(struct rds_ib_work_ring *ring);
346 int rds_ib_ring_low(struct rds_ib_work_ring *ring);
347 u32 rds_ib_ring_oldest(struct rds_ib_work_ring *ring);
348 u32 rds_ib_ring_completed(struct rds_ib_work_ring *ring, u32 wr_id, u32 oldest);
349 extern wait_queue_head_t rds_ib_ring_empty_wait;
350
351 /* ib_send.c */
352 void rds_ib_xmit_complete(struct rds_connection *conn);
353 int rds_ib_xmit(struct rds_connection *conn, struct rds_message *rm,
354                 unsigned int hdr_off, unsigned int sg, unsigned int off);
355 void rds_ib_send_cq_comp_handler(struct ib_cq *cq, void *context);
356 void rds_ib_send_init_ring(struct rds_ib_connection *ic);
357 void rds_ib_send_clear_ring(struct rds_ib_connection *ic);
358 int rds_ib_xmit_rdma(struct rds_connection *conn, struct rm_rdma_op *op);
359 void rds_ib_send_add_credits(struct rds_connection *conn, unsigned int credits);
360 void rds_ib_advertise_credits(struct rds_connection *conn, unsigned int posted);
361 int rds_ib_send_grab_credits(struct rds_ib_connection *ic, u32 wanted,
362                              u32 *adv_credits, int need_posted, int max_posted);
363 int rds_ib_xmit_atomic(struct rds_connection *conn, struct rm_atomic_op *op);
364
365 /* ib_stats.c */
366 DECLARE_PER_CPU(struct rds_ib_statistics, rds_ib_stats);
367 #define rds_ib_stats_inc(member) rds_stats_inc_which(rds_ib_stats, member)
368 unsigned int rds_ib_stats_info_copy(struct rds_info_iterator *iter,
369                                     unsigned int avail);
370
371 /* ib_sysctl.c */
372 int rds_ib_sysctl_init(void);
373 void rds_ib_sysctl_exit(void);
374 extern unsigned long rds_ib_sysctl_max_send_wr;
375 extern unsigned long rds_ib_sysctl_max_recv_wr;
376 extern unsigned long rds_ib_sysctl_max_unsig_wrs;
377 extern unsigned long rds_ib_sysctl_max_unsig_bytes;
378 extern unsigned long rds_ib_sysctl_max_recv_allocation;
379 extern unsigned int rds_ib_sysctl_flow_control;
380
381 #endif