Whamcloud - gitweb
LU-17024 build: Make o2ib defines consistent
[fs/lustre-release.git] / lnet / klnds / o2iblnd / o2iblnd.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lnet/klnds/o2iblnd/o2iblnd.h
32  *
33  * Author: Eric Barton <eric@bartonsoftware.com>
34  */
35
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38
39 #if defined(MLNX_OFED_BUILD) && !defined(HAVE_OFED_IB_DMA_MAP_SG_SANE)
40 #undef CONFIG_INFINIBAND_VIRT_DMA
41 #endif
42
43 #if defined(NEED_LOCKDEP_IS_HELD_DISCARD_CONST) \
44  && defined(CONFIG_LOCKDEP) \
45  && defined(lockdep_is_held)
46 #undef lockdep_is_held
47         #define lockdep_is_held(lock) \
48                 lock_is_held((struct lockdep_map *)&(lock)->dep_map)
49 #endif
50
51 #ifdef HAVE_OFED_COMPAT_RDMA
52 #include <linux/compat-2.6.h>
53
54 #ifdef LINUX_3_17_COMPAT_H
55 #undef NEED_KTIME_GET_REAL_NS
56 #endif
57
58 #define HAVE_NLA_PUT_U64_64BIT 1
59 #define HAVE_NLA_PARSE_6_PARAMS 1
60 #define HAVE_NETLINK_EXTACK 1
61
62 /* MOFED has its own bitmap_alloc backport */
63 #define HAVE_BITMAP_ALLOC 1
64
65 #endif
66
67 #include <linux/kthread.h>
68 #include <linux/mm.h>
69 #include <linux/string.h>
70 #include <linux/stat.h>
71 #include <linux/errno.h>
72 #include <linux/unistd.h>
73 #include <linux/uio.h>
74
75 #include <asm/uaccess.h>
76 #include <asm/io.h>
77
78 #include <linux/init.h>
79 #include <linux/fs.h>
80 #include <linux/file.h>
81 #include <linux/stat.h>
82 #include <linux/list.h>
83 #include <linux/kmod.h>
84 #include <linux/sysctl.h>
85 #include <linux/pci.h>
86
87 #include <net/sock.h>
88 #include <linux/in.h>
89
90 #include <rdma/rdma_cm.h>
91 #include <rdma/ib_cm.h>
92 #include <rdma/ib_verbs.h>
93 #ifdef HAVE_OFED_FMR_POOL_API
94 #include <rdma/ib_fmr_pool.h>
95 #endif
96
97 #define DEBUG_SUBSYSTEM S_LND
98
99 #include <lnet/lib-lnet.h>
100 #include <lnet/lnet_rdma.h>
101 #include "o2iblnd-idl.h"
102
103 enum kiblnd_ni_lnd_tunables_attr {
104         LNET_NET_O2IBLND_TUNABLES_ATTR_UNSPEC = 0,
105
106         LNET_NET_O2IBLND_TUNABLES_ATTR_HIW_PEER_CREDITS,
107         LNET_NET_O2IBLND_TUNABLES_ATTR_CONCURRENT_SENDS,
108         LNET_NET_O2IBLND_TUNABLES_ATTR_MAP_ON_DEMAND,
109         LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_POOL_SIZE,
110         LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_FLUSH_TRIGGER,
111         LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_CACHE,
112         LNET_NET_O2IBLND_TUNABLES_ATTR_NTX,
113         LNET_NET_O2IBLND_TUNABLES_ATTR_CONNS_PER_PEER,
114         LNET_NET_O2IBLND_TUNABLES_ATTR_LND_TIMEOUT,
115         __LNET_NET_O2IBLND_TUNABLES_ATTR_MAX_PLUS_ONE,
116 };
117
118 #define LNET_NET_O2IBLND_TUNABLES_ATTR_MAX (__LNET_NET_O2IBLND_TUNABLES_ATTR_MAX_PLUS_ONE - 1)
119
120 #define IBLND_PEER_HASH_BITS            7       /* log2 of # peer_ni lists */
121 #define IBLND_N_SCHED                   2
122 #define IBLND_N_SCHED_HIGH              4
123
124 struct kib_tunables {
125         int              *kib_dev_failover;     /* HCA failover */
126         unsigned int     *kib_service;          /* IB service number */
127         int              *kib_cksum;            /* checksum struct kib_msg? */
128         int              *kib_timeout;          /* comms timeout (seconds) */
129         int              *kib_keepalive;        /* keepalive timeout (seconds) */
130         char            **kib_default_ipif;     /* default IPoIB interface */
131         int              *kib_retry_count;
132         int              *kib_rnr_retry_count;
133         int              *kib_ib_mtu;           /* IB MTU */
134         int              *kib_require_priv_port;/* accept only privileged ports */
135         int              *kib_use_priv_port;    /* use privileged port for active connect */
136         /* # threads on each CPT */
137         int              *kib_nscheds;
138         int              *kib_wrq_sge;          /* # sg elements per wrq */
139         int              *kib_use_fastreg_gaps; /* enable discontiguous fastreg fragment support */
140 };
141
142 extern struct kib_tunables  kiblnd_tunables;
143
144 #define IBLND_MSG_QUEUE_SIZE_V1      8          /* V1 only : # messages/RDMAs in-flight */
145 #define IBLND_CREDIT_HIGHWATER_V1    7          /* V1 only : when eagerly to return credits */
146
147 #define IBLND_CREDITS_DEFAULT        8          /* default # of peer_ni credits */
148 #define IBLND_CREDITS_MAX          ((typeof(((struct kib_msg *) 0)->ibm_credits)) - 1)  /* Max # of peer_ni credits */
149
150 #ifdef HAVE_OFED_RDMA_CREATE_ID_5ARG
151 # define kiblnd_rdma_create_id(ns, cb, dev, ps, qpt) \
152          rdma_create_id((ns) ? (ns) : &init_net, cb, dev, ps, qpt)
153 #else
154 # ifdef HAVE_OFED_RDMA_CREATE_ID_4ARG
155 #  define kiblnd_rdma_create_id(ns, cb, dev, ps, qpt) \
156           rdma_create_id(cb, dev, ps, qpt)
157 # else
158 #  define kiblnd_rdma_create_id(ns, cb, dev, ps, qpt) \
159           rdma_create_id(cb, dev, ps)
160 # endif
161 #endif
162
163 /* 2 OOB shall suffice for 1 keepalive and 1 returning credits */
164 #define IBLND_OOB_CAPABLE(v)       ((v) != IBLND_MSG_VERSION_1)
165 #define IBLND_OOB_MSGS(v)           (IBLND_OOB_CAPABLE(v) ? 2 : 0)
166
167 /* max size of queued messages (inc hdr) */
168 #define IBLND_MSG_SIZE              (4<<10)
169 /* max # of fragments supported. + 1 for unaligned case */
170 #define IBLND_MAX_RDMA_FRAGS        (LNET_MAX_IOV + 1)
171
172 /************************/
173 /* derived constants... */
174 /* Pools (shared by connections on each CPT) */
175 /* These pools can grow at runtime, so don't need give a very large value */
176 #define IBLND_TX_POOL                   256
177 #define IBLND_FMR_POOL                  256
178 #define IBLND_FMR_POOL_FLUSH            192
179
180 /* RX messages (per connection) */
181 #define IBLND_RX_MSGS(c)        \
182         ((c->ibc_queue_depth) * 2 + IBLND_OOB_MSGS(c->ibc_version))
183 #define IBLND_RX_MSG_BYTES(c)       (IBLND_RX_MSGS(c) * IBLND_MSG_SIZE)
184 #define IBLND_RX_MSG_PAGES(c)   \
185         ((IBLND_RX_MSG_BYTES(c) + PAGE_SIZE - 1) / PAGE_SIZE)
186
187 /* WRs and CQEs (per connection) */
188 #define IBLND_RECV_WRS(c)            IBLND_RX_MSGS(c)
189
190 /* 2 = LNet msg + Transfer chain */
191 #define IBLND_CQ_ENTRIES(c) (IBLND_RECV_WRS(c) + kiblnd_send_wrs(c))
192
193 struct kib_hca_dev;
194
195 /* o2iblnd can run over aliased interface */
196 #ifdef IFALIASZ
197 #define KIB_IFNAME_SIZE              IFALIASZ
198 #else
199 #define KIB_IFNAME_SIZE              256
200 #endif
201
202 enum kib_dev_caps {
203         IBLND_DEV_CAPS_FASTREG_ENABLED          = BIT(0),
204         IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT     = BIT(1),
205 #ifdef HAVE_OFED_FMR_POOL_API
206         IBLND_DEV_CAPS_FMR_ENABLED              = BIT(2),
207 #endif
208 };
209
210 #define IS_FAST_REG_DEV(dev) \
211         ((dev)->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_ENABLED)
212
213
214 struct kib_dev {
215         struct list_head        ibd_list;       /* chain on kib_devs */
216         struct list_head        ibd_fail_list;  /* chain on kib_failed_devs */
217         __u32                   ibd_ifip;       /* IPoIB interface IP */
218         /** IPoIB interface name */
219         char                    ibd_ifname[KIB_IFNAME_SIZE];
220         int                     ibd_nnets;      /* # nets extant */
221
222         time64_t                ibd_next_failover;
223         /* # failover failures */
224         int                     ibd_failed_failover;
225         /* failover in progress */
226         unsigned int            ibd_failover;
227         /* IPoIB interface is a bonding master */
228         unsigned int            ibd_can_failover;
229         struct list_head        ibd_nets;
230         struct kib_hca_dev      *ibd_hdev;
231         enum kib_dev_caps       ibd_dev_caps;
232 };
233
234 struct kib_hca_dev {
235         struct rdma_cm_id   *ibh_cmid;          /* listener cmid */
236         struct ib_device    *ibh_ibdev;         /* IB device */
237         int                  ibh_page_shift;    /* page shift of current HCA */
238         int                  ibh_page_size;     /* page size of current HCA */
239         __u64                ibh_page_mask;     /* page mask of current HCA */
240         __u64                ibh_mr_size;       /* size of MR */
241         int                  ibh_max_qp_wr;     /* maximum work requests size */
242 #ifdef HAVE_OFED_IB_GET_DMA_MR
243         struct ib_mr        *ibh_mrs;           /* global MR */
244 #endif
245         struct ib_pd        *ibh_pd;            /* PD */
246         u8                   ibh_port;          /* port number */
247         struct ib_event_handler
248                              ibh_event_handler; /* IB event handler */
249         int                  ibh_state;         /* device status */
250 #define IBLND_DEV_PORT_DOWN     0
251 #define IBLND_DEV_PORT_ACTIVE   1
252 #define IBLND_DEV_FATAL         2
253         struct kib_dev           *ibh_dev;           /* owner */
254         atomic_t             ibh_ref;           /* refcount */
255 };
256
257 /** # of seconds to keep pool alive */
258 #define IBLND_POOL_DEADLINE     300
259 /** # of seconds to retry if allocation failed */
260 #define IBLND_POOL_RETRY        1
261
262 struct kib_pages {
263         int                     ibp_npages;             /* # pages */
264         struct page            *ibp_pages[0];           /* page array */
265 };
266
267 struct kib_pool;
268 struct kib_poolset;
269
270 typedef int  (*kib_ps_pool_create_t)(struct kib_poolset *ps,
271                                      int inc, struct kib_pool **pp_po);
272 typedef void (*kib_ps_pool_destroy_t)(struct kib_pool *po);
273 typedef void (*kib_ps_node_init_t)(struct kib_pool *po, struct list_head *node);
274 typedef void (*kib_ps_node_fini_t)(struct kib_pool *po, struct list_head *node);
275
276 struct kib_net;
277
278 #define IBLND_POOL_NAME_LEN     32
279
280 struct kib_poolset {
281         /* serialize */
282         spinlock_t              ps_lock;
283         /* network it belongs to */
284         struct kib_net          *ps_net;
285         /* pool set name */
286         char                    ps_name[IBLND_POOL_NAME_LEN];
287         /* list of pools */
288         struct list_head        ps_pool_list;
289         /* failed pool list */
290         struct list_head        ps_failed_pool_list;
291         /* time stamp for retry if failed to allocate */
292         time64_t                ps_next_retry;
293         /* is allocating new pool */
294         int                     ps_increasing;
295         /* new pool size */
296         int                     ps_pool_size;
297         /* CPT id */
298         int                     ps_cpt;
299
300         /* create a new pool */
301         kib_ps_pool_create_t    ps_pool_create;
302         /* destroy a pool */
303         kib_ps_pool_destroy_t   ps_pool_destroy;
304         /* initialize new allocated node */
305         kib_ps_node_init_t      ps_node_init;
306         /* finalize node */
307         kib_ps_node_fini_t      ps_node_fini;
308 };
309
310 struct kib_pool {
311         /* chain on pool list */
312         struct list_head        po_list;
313         /* pre-allocated node */
314         struct list_head        po_free_list;
315         /* pool_set of this pool */
316         struct kib_poolset     *po_owner;
317         /* deadline of this pool */
318         time64_t                po_deadline;
319         /* # of elements in use */
320         int                     po_allocated;
321         /* pool is created on failed HCA */
322         int                     po_failed;
323         /* # of pre-allocated elements */
324         int                     po_size;
325 };
326
327 struct kib_tx_poolset {
328         struct kib_poolset      tps_poolset;            /* pool-set */
329         __u64                   tps_next_tx_cookie;     /* cookie of TX */
330 };
331
332 struct kib_tx_pool {
333         struct kib_pool         tpo_pool;               /* pool */
334         struct kib_hca_dev     *tpo_hdev;               /* device for this pool */
335         struct kib_tx          *tpo_tx_descs;           /* all the tx descriptors */
336         struct kib_pages       *tpo_tx_pages;           /* premapped tx msg pages */
337 };
338
339 struct kib_fmr_poolset {
340         spinlock_t              fps_lock;               /* serialize */
341         struct kib_net         *fps_net;                /* IB network */
342         struct list_head        fps_pool_list;          /* FMR pool list */
343         struct list_head        fps_failed_pool_list;   /* FMR pool list */
344         __u64                   fps_version;            /* validity stamp */
345         int                     fps_cpt;                /* CPT id */
346         int                     fps_pool_size;
347         int                     fps_flush_trigger;
348         int                     fps_cache;
349         /* is allocating new pool */
350         int                     fps_increasing;
351         /* time stamp for retry if failed to allocate */
352         time64_t                fps_next_retry;
353 };
354
355 #ifndef HAVE_OFED_IB_RDMA_WR
356 struct ib_rdma_wr {
357         struct ib_send_wr wr;
358 };
359 #endif
360
361 struct kib_fast_reg_descriptor { /* For fast registration */
362         struct list_head                 frd_list;
363         struct ib_rdma_wr                frd_inv_wr;
364 #ifdef HAVE_OFED_IB_MAP_MR_SG
365         struct ib_reg_wr                 frd_fastreg_wr;
366 #else
367         struct ib_rdma_wr                frd_fastreg_wr;
368         struct ib_fast_reg_page_list    *frd_frpl;
369 #endif
370         struct ib_mr                    *frd_mr;
371         bool                             frd_valid;
372         bool                             frd_posted;
373 };
374
375 struct kib_fmr_pool {
376         struct list_head        fpo_list;       /* chain on pool list */
377         struct kib_hca_dev     *fpo_hdev;       /* device for this pool */
378         struct kib_fmr_poolset      *fpo_owner; /* owner of this pool */
379 #ifdef HAVE_OFED_FMR_POOL_API
380         union {
381                 struct {
382                         struct ib_fmr_pool *fpo_fmr_pool; /* IB FMR pool */
383                 } fmr;
384 #endif
385                 struct { /* For fast registration */
386                         struct list_head  fpo_pool_list;
387                         int               fpo_pool_size;
388                 } fast_reg;
389 #ifdef HAVE_OFED_FMR_POOL_API
390         };
391         bool                    fpo_is_fmr; /* True if FMR pools allocated */
392 #endif
393         time64_t                fpo_deadline;   /* deadline of this pool */
394         int                     fpo_failed;     /* fmr pool is failed */
395         int                     fpo_map_count;  /* # of mapped FMR */
396 };
397
398 struct kib_fmr {
399         struct kib_fmr_pool             *fmr_pool;      /* pool of FMR */
400 #ifdef HAVE_OFED_FMR_POOL_API
401         struct ib_pool_fmr              *fmr_pfmr;      /* IB pool fmr */
402 #endif /* HAVE_OFED_FMR_POOL_API */
403         struct kib_fast_reg_descriptor  *fmr_frd;
404         u32                              fmr_key;
405 };
406
407 #ifdef HAVE_OFED_FMR_POOL_API
408
409 #ifdef HAVE_ORACLE_OFED_EXTENSIONS
410 #define kib_fmr_pool_map(pool, pgs, n, iov) \
411         ib_fmr_pool_map_phys((pool), (pgs), (n), (iov), NULL)
412 #else
413 #define kib_fmr_pool_map(pool, pgs, n, iov) \
414         ib_fmr_pool_map_phys((pool), (pgs), (n), (iov))
415 #endif
416
417 #endif /* HAVE_OFED_FMR_POOL_API */
418
419 struct kib_net {
420         /* chain on struct kib_dev::ibd_nets */
421         struct list_head        ibn_list;
422         __u64                   ibn_incarnation;/* my epoch */
423         int                     ibn_init;       /* initialisation state */
424         int                     ibn_shutdown;   /* shutting down? */
425
426         atomic_t                ibn_npeers;     /* # peers extant */
427         atomic_t                ibn_nconns;     /* # connections extant */
428
429         struct kib_tx_poolset   **ibn_tx_ps;    /* tx pool-set */
430         struct kib_fmr_poolset  **ibn_fmr_ps;   /* fmr pool-set */
431
432         struct kib_dev          *ibn_dev;       /* underlying IB device */
433         struct lnet_ni          *ibn_ni;        /* LNet interface */
434 };
435
436 #define KIB_THREAD_SHIFT                16
437 #define KIB_THREAD_ID(cpt, tid)         ((cpt) << KIB_THREAD_SHIFT | (tid))
438 #define KIB_THREAD_CPT(id)              ((id) >> KIB_THREAD_SHIFT)
439 #define KIB_THREAD_TID(id)              ((id) & ((1UL << KIB_THREAD_SHIFT) - 1))
440
441 struct kib_sched_info {
442         /* serialise */
443         spinlock_t              ibs_lock;
444         /* schedulers sleep here */
445         wait_queue_head_t       ibs_waitq;
446         /* conns to check for rx completions */
447         struct list_head        ibs_conns;
448         /* number of scheduler threads */
449         int                     ibs_nthreads;
450         /* max allowed scheduler threads */
451         int                     ibs_nthreads_max;
452         int                     ibs_cpt;        /* CPT id */
453 };
454
455 struct kib_data {
456         int                     kib_init;       /* initialisation state */
457         int                     kib_shutdown;   /* shut down? */
458         struct list_head        kib_devs;       /* IB devices extant */
459         /* list head of failed devices */
460         struct list_head        kib_failed_devs;
461         /* schedulers sleep here */
462         wait_queue_head_t       kib_failover_waitq;
463         atomic_t                kib_nthreads;   /* # live threads */
464         /* stabilize net/dev/peer_ni/conn ops */
465         rwlock_t                kib_global_lock;
466         /* hash table of all my known peers */
467         DECLARE_HASHTABLE(kib_peers, IBLND_PEER_HASH_BITS);
468         /* the connd task (serialisation assertions) */
469         void                    *kib_connd;
470         /* connections to setup/teardown */
471         struct list_head        kib_connd_conns;
472         /* connections with zero refcount */
473         struct list_head        kib_connd_zombies;
474         /* connections to reconnect */
475         struct list_head        kib_reconn_list;
476         /* peers wait for reconnection */
477         struct list_head        kib_reconn_wait;
478         /* connections wait for completion */
479         struct list_head        kib_connd_waits;
480         /*
481          * The second that peers are pulled out from \a kib_reconn_wait
482          * for reconnection.
483          */
484         time64_t                kib_reconn_sec;
485         /* connection daemon sleeps here */
486         wait_queue_head_t       kib_connd_waitq;
487         spinlock_t              kib_connd_lock; /* serialise */
488         struct ib_qp_attr       kib_error_qpa;  /* QP->ERROR */
489         /* percpt data for schedulers */
490         struct kib_sched_info   **kib_scheds;
491 };
492
493 #define IBLND_INIT_NOTHING         0
494 #define IBLND_INIT_DATA            1
495 #define IBLND_INIT_ALL             2
496
497 struct kib_rx {                                 /* receive message */
498         /* queue for attention */
499         struct list_head        rx_list;
500         /* owning conn */
501         struct kib_conn        *rx_conn;
502         /* # bytes received (-1 while posted) */
503         int                     rx_nob;
504         /* message buffer (host vaddr) */
505         struct kib_msg         *rx_msg;
506         /* message buffer (I/O addr) */
507         __u64                   rx_msgaddr;
508         /* for dma_unmap_single() */
509         DEFINE_DMA_UNMAP_ADDR(rx_msgunmap);
510         /* receive work item... */
511         struct ib_recv_wr       rx_wrq;
512         /* ...and its memory */
513         struct ib_sge           rx_sge;
514 };
515
516 #define IBLND_POSTRX_DONT_POST    0             /* don't post */
517 #define IBLND_POSTRX_NO_CREDIT    1             /* post: no credits */
518 #define IBLND_POSTRX_PEER_CREDIT  2             /* post: give peer_ni back 1 credit */
519 #define IBLND_POSTRX_RSRVD_CREDIT 3             /* post: give myself back 1 reserved credit */
520
521 struct kib_tx {                                 /* transmit message */
522         /* queue on idle_txs ibc_tx_queue etc. */
523         struct list_head        tx_list;
524         /* pool I'm from */
525         struct kib_tx_pool      *tx_pool;
526         /* owning conn */
527         struct kib_conn         *tx_conn;
528         /* # tx callbacks outstanding */
529         short                   tx_sending;
530         /* queued for sending */
531         unsigned long           tx_queued:1,
532         /* waiting for peer_ni */
533                                 tx_waiting:1,
534         /* force RDMA */
535                                 tx_gpu:1;
536         /* LNET completion status */
537         int                     tx_status;
538         /* health status of the transmit */
539         enum lnet_msg_hstatus   tx_hstatus;
540         /* completion deadline */
541         ktime_t                 tx_deadline;
542         /* completion cookie */
543         __u64                   tx_cookie;
544         /* lnet msgs to finalize on completion */
545         struct lnet_msg         *tx_lntmsg[2];
546         /* message buffer (host vaddr) */
547         struct kib_msg          *tx_msg;
548         /* message buffer (I/O addr) */
549         __u64                   tx_msgaddr;
550         /* for dma_unmap_single() */
551         DEFINE_DMA_UNMAP_ADDR(tx_msgunmap);
552         /* # send work items */
553         int                     tx_nwrq;
554         /* # used scatter/gather elements */
555         int                     tx_nsge;
556         /* send work items... */
557         struct ib_rdma_wr       *tx_wrq;
558         /* ...and their memory */
559         struct ib_sge           *tx_sge;
560         /* rdma descriptor */
561         struct kib_rdma_desc    *tx_rd;
562         /* # entries in... */
563         int                     tx_nfrags;
564         /* dma_map_sg descriptor */
565         struct scatterlist      *tx_frags;
566         /* rdma phys page addrs */
567         __u64                   *tx_pages;
568         /* gaps in fragments */
569         bool                    tx_gaps;
570         /* FMR */
571         struct kib_fmr          tx_fmr;
572                                 /* dma direction */
573         int                     tx_dmadir;
574 };
575
576 struct kib_connvars {
577         /* connection-in-progress variables */
578         struct kib_msg          cv_msg;
579 };
580
581 struct kib_conn {
582         /* scheduler information */
583         struct kib_sched_info   *ibc_sched;
584         /* owning peer_ni */
585         struct kib_peer_ni      *ibc_peer;
586         /* HCA bound on */
587         struct kib_hca_dev      *ibc_hdev;
588         /* stash on peer_ni's conn list */
589         struct list_head        ibc_list;
590         /* schedule for attention */
591         struct list_head        ibc_sched_list;
592         /* version of connection */
593         __u16                   ibc_version;
594         /* reconnect later */
595         __u16                   ibc_reconnect:1;
596         /* which instance of the peer */
597         __u64                   ibc_incarnation;
598         /* # users */
599         atomic_t                ibc_refcount;
600         /* what's happening */
601         int                     ibc_state;
602         /* # uncompleted sends */
603         int                     ibc_nsends_posted;
604         /* # uncompleted NOOPs */
605         int                     ibc_noops_posted;
606         /* # credits I have */
607         int                     ibc_credits;
608         /* # credits to return */
609         int                     ibc_outstanding_credits;
610         /* # ACK/DONE msg credits */
611         int                     ibc_reserved_credits;
612         /* set on comms error */
613         int                     ibc_comms_error;
614         /* connections queue depth */
615         __u16                   ibc_queue_depth;
616         /* connections max frags */
617         __u16                   ibc_max_frags;
618         /* count of timeout txs waiting on cq */
619         __u16                   ibc_waits;
620         /* receive buffers owned */
621         unsigned int            ibc_nrx:16;
622         /* scheduled for attention */
623         unsigned int            ibc_scheduled:1;
624         /* CQ callback fired */
625         unsigned int            ibc_ready:1;
626         /* time of last send */
627         ktime_t                 ibc_last_send;
628         /** link chain for kiblnd_check_conns only */
629         struct list_head        ibc_connd_list;
630         /** rxs completed before ESTABLISHED */
631         struct list_head        ibc_early_rxs;
632         /** IBLND_MSG_NOOPs for IBLND_MSG_VERSION_1 */
633         struct list_head        ibc_tx_noops;
634         /* sends that need a credit */
635         struct list_head        ibc_tx_queue;
636         /* sends that don't need a credit */
637         struct list_head        ibc_tx_queue_nocred;
638         /* sends that need to reserve an ACK/DONE msg */
639         struct list_head        ibc_tx_queue_rsrvd;
640         /* active tx awaiting completion */
641         struct list_head        ibc_active_txs;
642         /* zombie tx awaiting done */
643         struct list_head        ibc_zombie_txs;
644         /* serialise */
645         spinlock_t              ibc_lock;
646         /* the rx descs */
647         struct kib_rx           *ibc_rxs;
648         /* premapped rx msg pages */
649         struct kib_pages        *ibc_rx_pages;
650
651         /* CM id */
652         struct rdma_cm_id       *ibc_cmid;
653         /* completion queue */
654         struct ib_cq            *ibc_cq;
655
656         /* in-progress connection state */
657         struct kib_connvars     *ibc_connvars;
658 };
659
660 #define IBLND_CONN_INIT               0         /* being initialised */
661 #define IBLND_CONN_ACTIVE_CONNECT     1         /* active sending req */
662 #define IBLND_CONN_PASSIVE_WAIT       2         /* passive waiting for rtu */
663 #define IBLND_CONN_ESTABLISHED        3         /* connection established */
664 #define IBLND_CONN_CLOSING            4         /* being closed */
665 #define IBLND_CONN_DISCONNECTED       5         /* disconnected */
666
667 struct kib_peer_ni {
668         /* on peer_ni hash chain */
669         struct hlist_node       ibp_list;
670         /* who's on the other end(s) */
671         lnet_nid_t              ibp_nid;
672         /* LNet interface */
673         struct lnet_ni          *ibp_ni;
674         /* all active connections */
675         struct list_head        ibp_conns;
676         /* next connection to send on for round robin */
677         struct kib_conn         *ibp_next_conn;
678         /* msgs waiting for a conn */
679         struct list_head        ibp_tx_queue;
680         /* incarnation of peer_ni */
681         __u64                   ibp_incarnation;
682         /* when (in seconds) I was last alive */
683         time64_t                ibp_last_alive;
684         /* # users */
685         struct kref             ibp_kref;
686         /* version of peer_ni */
687         __u16                   ibp_version;
688         /* current passive connection attempts */
689         unsigned short          ibp_accepting;
690         /* current active connection attempts */
691         unsigned short          ibp_connecting;
692         /* reconnect this peer_ni later */
693         unsigned char           ibp_reconnecting;
694         /* counter of how many times we triggered a conn race */
695         unsigned char           ibp_races;
696         /* # consecutive reconnection attempts to this peer */
697         unsigned int            ibp_reconnected;
698         /* errno on closing this peer_ni */
699         int                     ibp_error;
700         /* max map_on_demand */
701         __u16                   ibp_max_frags;
702         /* max_peer_credits */
703         __u16                   ibp_queue_depth;
704         /* reduced value which allows conn to be created if max fails */
705         __u16                   ibp_queue_depth_mod;
706         /* Number of connections allocated. */
707         atomic_t                ibp_nconns;
708 };
709
710 #ifndef HAVE_OFED_IB_INC_RKEY
711 /**
712  * ib_inc_rkey - increments the key portion of the given rkey. Can be used
713  * for calculating a new rkey for type 2 memory windows.
714  * @rkey - the rkey to increment.
715  */
716 static inline u32 ib_inc_rkey(u32 rkey)
717 {
718         const u32 mask = 0x000000ff;
719         return ((rkey + 1) & mask) | (rkey & ~mask);
720 }
721 #endif
722
723 extern struct kib_data kiblnd_data;
724
725 extern void kiblnd_hdev_destroy(struct kib_hca_dev *hdev);
726
727 int kiblnd_msg_queue_size(int version, struct lnet_ni *ni);
728
729 static inline int kiblnd_timeout(void)
730 {
731         return *kiblnd_tunables.kib_timeout ? *kiblnd_tunables.kib_timeout :
732                 lnet_get_lnd_timeout();
733 }
734
735 static inline int
736 kiblnd_concurrent_sends(int version, struct lnet_ni *ni)
737 {
738         struct lnet_ioctl_config_o2iblnd_tunables *tunables;
739         int concurrent_sends;
740
741         tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
742         concurrent_sends = tunables->lnd_concurrent_sends;
743
744         if (version == IBLND_MSG_VERSION_1) {
745                 if (concurrent_sends > IBLND_MSG_QUEUE_SIZE_V1 * 2)
746                         return IBLND_MSG_QUEUE_SIZE_V1 * 2;
747
748                 if (concurrent_sends < IBLND_MSG_QUEUE_SIZE_V1 / 2)
749                         return IBLND_MSG_QUEUE_SIZE_V1 / 2;
750         }
751
752         return concurrent_sends;
753 }
754
755 static inline void
756 kiblnd_hdev_addref_locked(struct kib_hca_dev *hdev)
757 {
758         LASSERT(atomic_read(&hdev->ibh_ref) > 0);
759         atomic_inc(&hdev->ibh_ref);
760 }
761
762 static inline void
763 kiblnd_hdev_decref(struct kib_hca_dev *hdev)
764 {
765         LASSERT(atomic_read(&hdev->ibh_ref) > 0);
766         if (atomic_dec_and_test(&hdev->ibh_ref))
767                 kiblnd_hdev_destroy(hdev);
768 }
769
770 static inline int
771 kiblnd_dev_can_failover(struct kib_dev *dev)
772 {
773         if (!list_empty(&dev->ibd_fail_list)) /* already scheduled */
774                 return 0;
775
776         if (*kiblnd_tunables.kib_dev_failover == 0) /* disabled */
777                 return 0;
778
779         if (*kiblnd_tunables.kib_dev_failover > 1) /* force failover */
780                 return 1;
781
782         return dev->ibd_can_failover;
783 }
784
785 static inline void kiblnd_conn_addref(struct kib_conn *conn)
786 {
787 #ifdef O2IBLND_CONN_REFCOUNT_DEBUG
788         CDEBUG(D_NET, "conn[%p] (%d)++\n",
789                (conn), atomic_read(&(conn)->ibc_refcount));
790 #endif
791         atomic_inc(&(conn)->ibc_refcount);
792 }
793
794 static inline void kiblnd_conn_decref(struct kib_conn *conn)
795 {
796         unsigned long flags;
797 #ifdef O2IBLND_CONN_REFCOUNT_DEBUG
798         CDEBUG(D_NET, "conn[%p] (%d)--\n",
799                (conn), atomic_read(&(conn)->ibc_refcount));
800 #endif
801         LASSERT(atomic_read(&(conn)->ibc_refcount) > 0);
802         if (atomic_dec_and_test(&(conn)->ibc_refcount)) {
803                 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
804                 list_add_tail(&(conn)->ibc_list,
805                               &kiblnd_data.kib_connd_zombies);
806                 wake_up(&kiblnd_data.kib_connd_waitq);
807                 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
808         }
809 }
810
811 void kiblnd_destroy_peer(struct kref *kref);
812
813 static inline void kiblnd_peer_addref(struct kib_peer_ni *peer_ni)
814 {
815         CDEBUG(D_NET, "peer_ni[%p] -> %s (%d)++\n",
816                peer_ni, libcfs_nid2str(peer_ni->ibp_nid),
817                kref_read(&peer_ni->ibp_kref));
818         kref_get(&(peer_ni)->ibp_kref);
819 }
820
821 static inline void kiblnd_peer_decref(struct kib_peer_ni *peer_ni)
822 {
823         CDEBUG(D_NET, "peer_ni[%p] -> %s (%d)--\n",
824                peer_ni, libcfs_nid2str(peer_ni->ibp_nid),
825                kref_read(&peer_ni->ibp_kref));
826         kref_put(&peer_ni->ibp_kref, kiblnd_destroy_peer);
827 }
828
829 static inline bool
830 kiblnd_peer_connecting(struct kib_peer_ni *peer_ni)
831 {
832         return peer_ni->ibp_connecting != 0 ||
833                peer_ni->ibp_reconnecting != 0 ||
834                peer_ni->ibp_accepting != 0;
835 }
836
837 static inline bool
838 kiblnd_peer_idle(struct kib_peer_ni *peer_ni)
839 {
840         return !kiblnd_peer_connecting(peer_ni) && list_empty(&peer_ni->ibp_conns);
841 }
842
843 static inline int
844 kiblnd_peer_active(struct kib_peer_ni *peer_ni)
845 {
846         /* Am I in the peer_ni hash table? */
847         return !hlist_unhashed(&peer_ni->ibp_list);
848 }
849
850 static inline struct kib_conn *
851 kiblnd_get_conn_locked(struct kib_peer_ni *peer_ni)
852 {
853         struct list_head *next;
854
855         LASSERT(!list_empty(&peer_ni->ibp_conns));
856
857         /* Advance to next connection, be sure to skip the head node */
858         if (!peer_ni->ibp_next_conn ||
859             peer_ni->ibp_next_conn->ibc_list.next == &peer_ni->ibp_conns)
860                 next = peer_ni->ibp_conns.next;
861         else
862                 next = peer_ni->ibp_next_conn->ibc_list.next;
863         peer_ni->ibp_next_conn = list_entry(next, struct kib_conn, ibc_list);
864
865         return peer_ni->ibp_next_conn;
866 }
867
868 static inline int
869 kiblnd_send_keepalive(struct kib_conn *conn)
870 {
871         s64 keepalive_ns = *kiblnd_tunables.kib_keepalive * NSEC_PER_SEC;
872
873         return (*kiblnd_tunables.kib_keepalive > 0) &&
874                 ktime_after(ktime_get(),
875                             ktime_add_ns(conn->ibc_last_send, keepalive_ns));
876 }
877
878 /* when to return credits eagerly */
879 static inline int
880 kiblnd_credits_highwater(struct lnet_ioctl_config_o2iblnd_tunables *t,
881                          struct lnet_ioctl_config_lnd_cmn_tunables *nt,
882                          struct kib_conn *conn)
883 {
884         int credits_hiw = IBLND_CREDIT_HIGHWATER_V1;
885
886         if ((conn->ibc_version) == IBLND_MSG_VERSION_1)
887                 return credits_hiw;
888
889         /* if queue depth is negotiated down, calculate hiw proportionally */
890         credits_hiw = (conn->ibc_queue_depth * t->lnd_peercredits_hiw) /
891                        nt->lct_peer_tx_credits;
892
893         return credits_hiw;
894 }
895
896 static inline int
897 kiblnd_need_noop(struct kib_conn *conn)
898 {
899         struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
900         struct lnet_ioctl_config_o2iblnd_tunables *tunables;
901         struct lnet_ioctl_config_lnd_cmn_tunables *net_tunables;
902
903         LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
904         tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
905         net_tunables = &ni->ni_net->net_tunables;
906
907         if (conn->ibc_outstanding_credits <
908             kiblnd_credits_highwater(tunables, net_tunables, conn) &&
909             !kiblnd_send_keepalive(conn))
910                 return 0; /* No need to send NOOP */
911
912         if (IBLND_OOB_CAPABLE(conn->ibc_version)) {
913                 if (!list_empty(&conn->ibc_tx_queue_nocred))
914                         return 0; /* NOOP can be piggybacked */
915
916                 /* No tx to piggyback NOOP onto or no credit to send a tx */
917                 return (list_empty(&conn->ibc_tx_queue) ||
918                         conn->ibc_credits == 0);
919         }
920
921         if (!list_empty(&conn->ibc_tx_noops) || /* NOOP already queued */
922             !list_empty(&conn->ibc_tx_queue_nocred) || /* piggyback NOOP */
923             conn->ibc_credits == 0)                    /* no credit */
924                 return 0;
925
926         if (conn->ibc_credits == 1 &&      /* last credit reserved for */
927             conn->ibc_outstanding_credits == 0) /* giving back credits */
928                 return 0;
929
930         /* No tx to piggyback NOOP onto or no credit to send a tx */
931         return (list_empty(&conn->ibc_tx_queue) || conn->ibc_credits == 1);
932 }
933
934 static inline void
935 kiblnd_abort_receives(struct kib_conn *conn)
936 {
937         ib_modify_qp(conn->ibc_cmid->qp,
938                      &kiblnd_data.kib_error_qpa, IB_QP_STATE);
939 }
940
941 static inline const char *
942 kiblnd_queue2str(struct kib_conn *conn, struct list_head *q)
943 {
944         if (q == &conn->ibc_tx_queue)
945                 return "tx_queue";
946
947         if (q == &conn->ibc_tx_queue_rsrvd)
948                 return "tx_queue_rsrvd";
949
950         if (q == &conn->ibc_tx_queue_nocred)
951                 return "tx_queue_nocred";
952
953         if (q == &conn->ibc_active_txs)
954                 return "active_txs";
955
956         LBUG();
957         return NULL;
958 }
959
960 /* CAVEAT EMPTOR: We rely on descriptor alignment to allow us to use the
961  * lowest bits of the work request id to stash the work item type. */
962
963 #define IBLND_WID_INVAL 0
964 #define IBLND_WID_TX    1
965 #define IBLND_WID_RX    2
966 #define IBLND_WID_RDMA  3
967 #define IBLND_WID_MR    4
968 #define IBLND_WID_MASK  7UL
969
970 static inline __u64
971 kiblnd_ptr2wreqid (void *ptr, int type)
972 {
973         unsigned long lptr = (unsigned long)ptr;
974
975         LASSERT ((lptr & IBLND_WID_MASK) == 0);
976         LASSERT ((type & ~IBLND_WID_MASK) == 0);
977         return (__u64)(lptr | type);
978 }
979
980 static inline void *
981 kiblnd_wreqid2ptr (__u64 wreqid)
982 {
983         return (void *)(((unsigned long)wreqid) & ~IBLND_WID_MASK);
984 }
985
986 static inline int
987 kiblnd_wreqid2type (__u64 wreqid)
988 {
989         return (wreqid & IBLND_WID_MASK);
990 }
991
992 static inline void
993 kiblnd_set_conn_state(struct kib_conn *conn, int state)
994 {
995         conn->ibc_state = state;
996         smp_mb();
997 }
998
999 static inline void
1000 kiblnd_init_msg(struct kib_msg *msg, int type, int body_nob)
1001 {
1002         msg->ibm_type = type;
1003         msg->ibm_nob = offsetof(struct kib_msg, ibm_u) + body_nob;
1004 }
1005
1006 static inline int
1007 kiblnd_rd_size(struct kib_rdma_desc *rd)
1008 {
1009         int   i;
1010         int   size;
1011
1012         for (i = size = 0; i < rd->rd_nfrags; i++)
1013                 size += rd->rd_frags[i].rf_nob;
1014
1015         return size;
1016 }
1017
1018 static inline __u64
1019 kiblnd_rd_frag_addr(struct kib_rdma_desc *rd, int index)
1020 {
1021         return rd->rd_frags[index].rf_addr;
1022 }
1023
1024 static inline int
1025 kiblnd_rd_frag_size(struct kib_rdma_desc *rd, int index)
1026 {
1027         return rd->rd_frags[index].rf_nob;
1028 }
1029
1030 static inline __u32
1031 kiblnd_rd_frag_key(struct kib_rdma_desc *rd, int index)
1032 {
1033         return rd->rd_key;
1034 }
1035
1036 static inline int
1037 kiblnd_rd_consume_frag(struct kib_rdma_desc *rd, int index, __u32 nob)
1038 {
1039         if (nob < rd->rd_frags[index].rf_nob) {
1040                 rd->rd_frags[index].rf_addr += nob;
1041                 rd->rd_frags[index].rf_nob  -= nob;
1042         } else {
1043                 index ++;
1044         }
1045
1046         return index;
1047 }
1048
1049 static inline int
1050 kiblnd_rd_msg_size(struct kib_rdma_desc *rd, int msgtype, int n)
1051 {
1052         LASSERT (msgtype == IBLND_MSG_GET_REQ ||
1053                  msgtype == IBLND_MSG_PUT_ACK);
1054
1055         return msgtype == IBLND_MSG_GET_REQ ?
1056                offsetof(struct kib_get_msg, ibgm_rd.rd_frags[n]) :
1057                offsetof(struct kib_putack_msg, ibpam_rd.rd_frags[n]);
1058 }
1059
1060 static inline __u64
1061 kiblnd_dma_mapping_error(struct ib_device *dev, u64 dma_addr)
1062 {
1063         return ib_dma_mapping_error(dev, dma_addr);
1064 }
1065
1066 static inline __u64 kiblnd_dma_map_single(struct ib_device *dev,
1067                                           void *msg, size_t size,
1068                                           enum dma_data_direction direction)
1069 {
1070         return ib_dma_map_single(dev, msg, size, direction);
1071 }
1072
1073 static inline void kiblnd_dma_unmap_single(struct ib_device *dev,
1074                                            __u64 addr, size_t size,
1075                                           enum dma_data_direction direction)
1076 {
1077         ib_dma_unmap_single(dev, addr, size, direction);
1078 }
1079
1080 #define KIBLND_UNMAP_ADDR_SET(p, m, a)  do {} while (0)
1081 #define KIBLND_UNMAP_ADDR(p, m, a)      (a)
1082
1083 static inline
1084 int kiblnd_dma_map_sg(struct kib_hca_dev *hdev, struct kib_tx *tx)
1085 {
1086         struct scatterlist *sg = tx->tx_frags;
1087         int nents = tx->tx_nfrags;
1088         enum dma_data_direction direction = tx->tx_dmadir;
1089
1090         if (tx->tx_gpu)
1091                 return lnet_rdma_map_sg_attrs(hdev->ibh_ibdev->dma_device,
1092                                               sg, nents, direction);
1093
1094         return ib_dma_map_sg(hdev->ibh_ibdev, sg, nents, direction);
1095 }
1096
1097 static inline
1098 void kiblnd_dma_unmap_sg(struct kib_hca_dev *hdev, struct kib_tx *tx)
1099 {
1100         struct scatterlist *sg = tx->tx_frags;
1101         int nents = tx->tx_nfrags;
1102         enum dma_data_direction direction = tx->tx_dmadir;
1103
1104         if (tx->tx_gpu)
1105                 lnet_rdma_unmap_sg(hdev->ibh_ibdev->dma_device,
1106                                           sg, nents, direction);
1107         else
1108                 ib_dma_unmap_sg(hdev->ibh_ibdev, sg, nents, direction);
1109 }
1110
1111 #ifndef HAVE_OFED_IB_SG_DMA_ADDRESS
1112 #include <linux/scatterlist.h>
1113 #define ib_sg_dma_address(dev, sg)      sg_dma_address(sg)
1114 #define ib_sg_dma_len(dev, sg)          sg_dma_len(sg)
1115 #endif
1116
1117 static inline __u64 kiblnd_sg_dma_address(struct ib_device *dev,
1118                                           struct scatterlist *sg)
1119 {
1120         return ib_sg_dma_address(dev, sg);
1121 }
1122
1123 static inline unsigned int kiblnd_sg_dma_len(struct ib_device *dev,
1124                                              struct scatterlist *sg)
1125 {
1126         return ib_sg_dma_len(dev, sg);
1127 }
1128
1129 #ifndef HAVE_OFED_RDMA_CONNECT_LOCKED
1130 #define rdma_connect_locked(cmid, cpp)  rdma_connect(cmid, cpp)
1131 #endif
1132
1133 /* XXX We use KIBLND_CONN_PARAM(e) as writable buffer, it's not strictly
1134  * right because OFED1.2 defines it as const, to use it we have to add
1135  * (void *) cast to overcome "const" */
1136
1137 #define KIBLND_CONN_PARAM(e)            ((e)->param.conn.private_data)
1138 #define KIBLND_CONN_PARAM_LEN(e)        ((e)->param.conn.private_data_len)
1139
1140 void kiblnd_abort_txs(struct kib_conn *conn, struct list_head *txs);
1141 void kiblnd_map_rx_descs(struct kib_conn *conn);
1142 void kiblnd_unmap_rx_descs(struct kib_conn *conn);
1143 void kiblnd_pool_free_node(struct kib_pool *pool, struct list_head *node);
1144 struct list_head *kiblnd_pool_alloc_node(struct kib_poolset *ps);
1145
1146 int kiblnd_fmr_pool_map(struct kib_fmr_poolset *fps, struct kib_tx *tx,
1147                         struct kib_rdma_desc *rd, u32 nob, u64 iov,
1148                         struct kib_fmr *fmr);
1149 void kiblnd_fmr_pool_unmap(struct kib_fmr *fmr, int status);
1150
1151 int  kiblnd_tunables_setup(struct lnet_ni *ni);
1152 int  kiblnd_tunables_init(void);
1153
1154 int  kiblnd_connd (void *arg);
1155 int  kiblnd_scheduler(void *arg);
1156 #define kiblnd_thread_start(fn, data, namefmt, arg...)                  \
1157         ({                                                              \
1158                 struct task_struct *__task = kthread_run(fn, data,      \
1159                                                          namefmt, ##arg); \
1160                 if (!IS_ERR(__task))                                    \
1161                         atomic_inc(&kiblnd_data.kib_nthreads);          \
1162                 PTR_ERR_OR_ZERO(__task);                                \
1163         })
1164
1165 int  kiblnd_failover_thread (void *arg);
1166
1167 int kiblnd_alloc_pages(struct kib_pages **pp, int cpt, int npages);
1168
1169 int  kiblnd_cm_callback(struct rdma_cm_id *cmid,
1170                         struct rdma_cm_event *event);
1171 int  kiblnd_translate_mtu(int value);
1172
1173 int  kiblnd_dev_failover(struct kib_dev *dev, struct net *ns);
1174 int kiblnd_create_peer(struct lnet_ni *ni, struct kib_peer_ni **peerp,
1175                        lnet_nid_t nid);
1176 bool kiblnd_reconnect_peer(struct kib_peer_ni *peer);
1177 void kiblnd_destroy_dev(struct kib_dev *dev);
1178 void kiblnd_unlink_peer_locked(struct kib_peer_ni *peer_ni);
1179 struct kib_peer_ni *kiblnd_find_peer_locked(struct lnet_ni *ni, lnet_nid_t nid);
1180 int  kiblnd_close_stale_conns_locked(struct kib_peer_ni *peer_ni,
1181                                      int version, u64 incarnation);
1182 int  kiblnd_close_peer_conns_locked(struct kib_peer_ni *peer_ni, int why);
1183
1184 struct kib_conn *kiblnd_create_conn(struct kib_peer_ni *peer_ni,
1185                                     struct rdma_cm_id *cmid,
1186                                     int state, int version);
1187 void kiblnd_destroy_conn(struct kib_conn *conn);
1188 void kiblnd_close_conn(struct kib_conn *conn, int error);
1189 void kiblnd_close_conn_locked(struct kib_conn *conn, int error);
1190
1191 void kiblnd_launch_tx(struct lnet_ni *ni, struct kib_tx *tx, lnet_nid_t nid);
1192 void kiblnd_txlist_done(struct list_head *txlist, int status,
1193                         enum lnet_msg_hstatus hstatus);
1194
1195 void kiblnd_qp_event(struct ib_event *event, void *arg);
1196 void kiblnd_cq_event(struct ib_event *event, void *arg);
1197 void kiblnd_cq_completion(struct ib_cq *cq, void *arg);
1198
1199 void kiblnd_pack_msg(struct lnet_ni *ni, struct kib_msg *msg, int version,
1200                      int credits, lnet_nid_t dstnid, __u64 dststamp);
1201 int kiblnd_unpack_msg(struct kib_msg *msg, int nob);
1202 int kiblnd_post_rx(struct kib_rx *rx, int credit);
1203
1204 int kiblnd_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg);
1205 int kiblnd_recv(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg,
1206                 int delayed, unsigned int niov,
1207                 struct bio_vec *kiov, unsigned int offset, unsigned int mlen,
1208                 unsigned int rlen);
1209 unsigned int kiblnd_get_dev_prio(struct lnet_ni *ni, unsigned int dev_idx);
1210
1211 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 11, 0)
1212 #undef netdev_notifier_info_to_dev
1213 #define netdev_notifier_info_to_dev(ndev) ndev
1214 #endif
1215
1216 #define kiblnd_dump_conn_dbg(conn)                      \
1217 ({                                                      \
1218         if (conn && conn->ibc_cmid)                     \
1219                 CDEBUG(D_NET, "conn %p state %d nposted %d/%d c/o/r %d/%d/%d ce %d : cm_id %p qp_num 0x%x device_name %s\n",    \
1220                         conn,                           \
1221                         conn->ibc_state,                \
1222                         conn->ibc_noops_posted,         \
1223                         conn->ibc_nsends_posted,        \
1224                         conn->ibc_credits,              \
1225                         conn->ibc_outstanding_credits,  \
1226                         conn->ibc_reserved_credits,     \
1227                         conn->ibc_comms_error,          \
1228                         conn->ibc_cmid,                 \
1229                         conn->ibc_cmid->qp ? conn->ibc_cmid->qp->qp_num : 0,    \
1230                         conn->ibc_cmid->qp ? (conn->ibc_cmid->qp->device ? dev_name(&conn->ibc_cmid->qp->device->dev) : "NULL") : "NULL");      \
1231         else if (conn)                                  \
1232                 CDEBUG(D_NET, "conn %p state %d nposted %d/%d c/o/r %d/%d/%d ce %d : cm_id NULL\n",     \
1233                         conn,                           \
1234                         conn->ibc_state,                \
1235                         conn->ibc_noops_posted,         \
1236                         conn->ibc_nsends_posted,        \
1237                         conn->ibc_credits,              \
1238                         conn->ibc_outstanding_credits,  \
1239                         conn->ibc_reserved_credits,     \
1240                         conn->ibc_comms_error           \
1241                         );                              \
1242 })