Whamcloud - gitweb
4354dd2d16273eb28787e3843b235c36614c80ac
[fs/lustre-release.git] / lnet / klnds / openiblnd / openiblnd.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2004 Cluster File Systems, Inc.
5  *   Author: Eric Barton <eric@bartonsoftware.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #ifndef EXPORT_SYMTAB
25 # define EXPORT_SYMTAB
26 #endif
27 #ifndef AUTOCONF_INCLUDED
28 #include <linux/config.h>
29 #endif
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/mm.h>
33 #include <linux/string.h>
34 #include <linux/stat.h>
35 #include <linux/errno.h>
36 #include <linux/smp_lock.h>
37 #include <linux/unistd.h>
38 #include <linux/uio.h>
39
40 #include <asm/system.h>
41 #include <asm/uaccess.h>
42 #include <asm/io.h>
43
44 #include <linux/init.h>
45 #include <linux/fs.h>
46 #include <linux/file.h>
47 #include <linux/stat.h>
48 #include <linux/list.h>
49 #include <linux/kmod.h>
50 #include <linux/sysctl.h>
51
52 #include <net/sock.h>
53 #include <linux/in.h>
54
55 #define DEBUG_SUBSYSTEM S_LND
56
57 #include <libcfs/kp30.h>
58 #include <lnet/lnet.h>
59 #include <lnet/lib-lnet.h>
60
61 #include <ts_ib_core.h>
62 #include <ts_ib_cm.h>
63 #include <ts_ib_sa_client.h>
64
65 #ifndef USING_TSAPI
66
67 /* OpenIB Gen1 */
68 typedef struct ib_qp       ib_qp_t;
69 typedef struct ib_mr       ib_mr_t;
70 typedef struct ib_fmr      ib_fmr_t;
71 typedef struct ib_pd       ib_pd_t;
72 typedef struct ib_cq       ib_cq_t;
73 typedef struct ib_fmr_pool ib_fmr_pool_t;
74
75 #else
76
77 /* Cisco (topspin) */
78 typedef void                 ib_qp_t;
79 typedef void                 ib_mr_t;
80 typedef void                 ib_fmr_t;
81 typedef void                 ib_pd_t;
82 typedef void                 ib_cq_t;
83 typedef void                 ib_fmr_pool_t;
84
85 #define IB_ACCESS_LOCAL_WRITE              TS_IB_ACCESS_LOCAL_WRITE
86 #define IB_WQ_SIGNAL_SELECTABLE            TS_IB_ACCESS_LOCAL_WRITE
87 #define IB_TRANSPORT_RC                    TS_IB_TRANSPORT_RC
88 #define IB_QP_STATE_INIT                   TS_IB_QP_STATE_INIT
89 #define IB_QP_ATTRIBUTE_STATE              TS_IB_QP_ATTRIBUTE_STATE
90 #define IB_QP_ATTRIBUTE_PORT               TS_IB_QP_ATTRIBUTE_PORT
91 #define IB_QP_ATTRIBUTE_PKEY_INDEX         TS_IB_QP_ATTRIBUTE_PKEY_INDEX
92 #define IB_QP_ATTRIBUTE_RDMA_ATOMIC_ENABLE TS_IB_QP_ATTRIBUTE_RDMA_ATOMIC_ENABLE
93 #define IB_ACCESS_LOCAL_WRITE              TS_IB_ACCESS_LOCAL_WRITE
94 #define IB_ACCESS_REMOTE_WRITE             TS_IB_ACCESS_REMOTE_WRITE
95 #define IB_ACCESS_REMOTE_READ              TS_IB_ACCESS_REMOTE_READ
96 #define IB_CQ_CALLBACK_INTERRU             TS_IB_CQ_CALLBACK_INTERRUPTPT
97 #define IB_CQ_PROVIDER_REARM               TS_IB_CQ_PROVIDER_REARM
98 #define IB_CQ_CALLBACK_INTERRUPT           TS_IB_CQ_CALLBACK_INTERRUPT
99 #define IB_COMPLETION_STATUS_SUCCESS       TS_IB_COMPLETION_STATUS_SUCCESS
100 #define IB_OP_SEND                         TS_IB_OP_SEND
101 #define IB_OP_RDMA_WRITE                   TS_IB_OP_RDMA_WRITE
102 #define IB_OP_RDMA_READ                    TS_IB_OP_RDMA_READ
103
104 #endif
105
106 #ifdef CONFIG_SMP
107 # define IBNAL_N_SCHED      num_online_cpus()   /* # schedulers */
108 #else
109 # define IBNAL_N_SCHED      1                   /* # schedulers */
110 #endif
111
112 #define IBNAL_FMR                    1
113 //#define IBNAL_CALLBACK_CTXT  IB_CQ_CALLBACK_PROCESS
114 #define IBNAL_CALLBACK_CTXT  IB_CQ_CALLBACK_INTERRUPT
115
116
117 /* tunables fixed at compile time */
118 #define IBNAL_PEER_HASH_SIZE         101        /* # peer lists */
119 #define IBNAL_RESCHED                100        /* # scheduler loops before reschedule */
120 #define IBNAL_MSG_QUEUE_SIZE         8          /* # messages/RDMAs in-flight */
121 #define IBNAL_CREDIT_HIGHWATER       6          /* when to eagerly return credits */
122 #define IBNAL_MSG_SIZE              (4<<10)     /* max size of queued messages (inc hdr) */
123 #define IBNAL_RDMA_BASE              0x0eeb0000
124
125 /* QP tunables */
126 #define IBNAL_RETRY                  7          /* # times to retry */
127 #define IBNAL_RNR_RETRY              7          /*  */
128 #define IBNAL_CM_RETRY               7          /* # times to retry connection */
129 #define IBNAL_FLOW_CONTROL           1
130 #define IBNAL_RESPONDER_RESOURCES    8
131
132 /************************/
133 /* derived constants... */
134
135 /* TX messages (shared by all connections) */
136 #define IBNAL_TX_MSGS()       (*kibnal_tunables.kib_ntx)
137 #define IBNAL_TX_MSG_BYTES()  (IBNAL_TX_MSGS() * IBNAL_MSG_SIZE)
138 #define IBNAL_TX_MSG_PAGES()  ((IBNAL_TX_MSG_BYTES() + PAGE_SIZE - 1)/PAGE_SIZE)
139
140 /* RX messages (per connection) */
141 #define IBNAL_RX_MSGS         (IBNAL_MSG_QUEUE_SIZE * 2)
142 #define IBNAL_RX_MSG_BYTES    (IBNAL_RX_MSGS * IBNAL_MSG_SIZE)
143 #define IBNAL_RX_MSG_PAGES    ((IBNAL_RX_MSG_BYTES + PAGE_SIZE - 1)/PAGE_SIZE)
144
145 /* we may have up to 2 completions per transmit +
146    1 completion per receive, per connection */
147 #define IBNAL_CQ_ENTRIES()  ((2*IBNAL_TX_MSGS()) +                                      \
148                              (IBNAL_RX_MSGS * *kibnal_tunables.kib_concurrent_peers))
149
150 typedef struct
151 {
152         char    **kib_ipif_basename;            /* IPoIB interface base name */
153         int      *kib_n_connd;                  /* # connection daemons */
154         int      *kib_min_reconnect_interval;   /* min connect retry seconds... */
155         int      *kib_max_reconnect_interval;   /* max connect retry seconds */
156         int      *kib_concurrent_peers;         /* max # peers */
157         int      *kib_cksum;                    /* checksum kib_msg_t? */
158         int      *kib_timeout;                  /* comms timeout (seconds) */
159         int      *kib_keepalive;                /* keepalive (seconds) */
160         int      *kib_ntx;                      /* # tx descs */
161         int      *kib_credits;                  /* # concurrent sends */
162         int      *kib_peercredits;              /* # concurrent sends to 1 peer */
163
164         cfs_sysctl_table_header_t *kib_sysctl;  /* sysctl interface */
165 } kib_tunables_t;
166
167 typedef struct
168 {
169         int               ibp_npages;           /* # pages */
170         int               ibp_mapped;           /* mapped? */
171         __u64             ibp_vaddr;            /* mapped region vaddr */
172         __u32             ibp_lkey;             /* mapped region lkey */
173         __u32             ibp_rkey;             /* mapped region rkey */
174         ib_mr_t          *ibp_handle;           /* mapped region handle */
175         struct page      *ibp_pages[0];
176 } kib_pages_t;
177
178 typedef struct
179 {
180         int               kib_init;             /* initialisation state */
181         __u64             kib_incarnation;      /* which one am I */
182         int               kib_shutdown;         /* shut down? */
183         atomic_t          kib_nthreads;         /* # live threads */
184         lnet_ni_t        *kib_ni;               /* _the_ openib interface */
185
186         __u64             kib_svc_id;           /* service number I listen on */
187         tTS_IB_GID        kib_svc_gid;          /* device/port GID */
188         __u16             kib_svc_pkey;         /* device/port pkey */
189         
190         void             *kib_listen_handle;    /* IB listen handle */
191         
192         rwlock_t          kib_global_lock;      /* stabilize peer/conn ops */
193
194         struct list_head *kib_peers;            /* hash table of all my known peers */
195         int               kib_peer_hash_size;   /* size of kib_peers */
196         int               kib_nonewpeers;       /* prevent new peers? */
197         atomic_t          kib_npeers;           /* # peers extant */
198         atomic_t          kib_nconns;           /* # connections extant */
199
200         struct list_head  kib_reaper_conns;     /* connections to reap */
201         wait_queue_head_t kib_reaper_waitq;     /* reaper sleeps here */
202         unsigned long     kib_reaper_waketime;  /* when reaper will wake */
203         spinlock_t        kib_reaper_lock;      /* serialise */
204
205         struct list_head  kib_connd_peers;      /* peers waiting for a connection */
206         struct list_head  kib_connd_acceptq;    /* accepted sockets to handle */
207         wait_queue_head_t kib_connd_waitq;      /* connection daemons sleep here */
208         int               kib_connd_connecting; /* # connds connecting */
209         spinlock_t        kib_connd_lock;       /* serialise */
210
211         wait_queue_head_t kib_sched_waitq;      /* schedulers sleep here */
212         struct list_head  kib_sched_txq;        /* tx requiring attention */
213         struct list_head  kib_sched_rxq;        /* rx requiring attention */
214         spinlock_t        kib_sched_lock;       /* serialise */
215
216         struct kib_tx    *kib_tx_descs;         /* all the tx descriptors */
217         kib_pages_t      *kib_tx_pages;         /* premapped tx msg pages */
218
219         struct list_head  kib_idle_txs;         /* idle tx descriptors */
220         __u64             kib_next_tx_cookie;   /* RDMA completion cookie */
221         spinlock_t        kib_tx_lock;          /* serialise */
222
223         int               kib_hca_idx;          /* my HCA number */
224         struct ib_device *kib_device;           /* "the" device */
225         struct ib_device_properties kib_device_props; /* its properties */
226         int               kib_port;             /* port on the device */
227         struct ib_port_properties kib_port_props; /* its properties */
228         ib_pd_t          *kib_pd;               /* protection domain */
229 #if IBNAL_FMR
230         ib_fmr_pool_t    *kib_fmr_pool;         /* fast memory region pool */
231 #endif
232         ib_cq_t          *kib_cq;               /* completion queue */
233
234 } kib_data_t;
235
236 #define IBNAL_INIT_NOTHING         0
237 #define IBNAL_INIT_DATA            1
238 #define IBNAL_INIT_LIB             2
239 #define IBNAL_INIT_PD              3
240 #define IBNAL_INIT_FMR             4
241 #define IBNAL_INIT_TXD             5
242 #define IBNAL_INIT_CQ              6
243 #define IBNAL_INIT_ALL             7
244
245 typedef struct kib_acceptsock                   /* accepted socket queued for connd */
246 {
247         struct list_head     ibas_list;         /* queue for attention */
248         struct socket       *ibas_sock;         /* the accepted socket */
249 } kib_acceptsock_t;
250
251 /************************************************************************
252  * IB Wire message format.
253  * These are sent in sender's byte order (i.e. receiver flips).
254  * They may be sent via TCP/IP (service ID,GID,PKEY query/response),
255  * as private data in the connection request/response, or "normally".
256  */
257
258 typedef struct kib_svcrsp                       /* service response */
259 {
260         __u64             ibsr_svc_id;          /* service's id */
261         __u8              ibsr_svc_gid[16];     /* service's gid */
262         __u16             ibsr_svc_pkey;        /* service's pkey */
263 } WIRE_ATTR kib_svcrsp_t;
264
265 typedef struct kib_connparams
266 {
267         __u32             ibcp_queue_depth;
268 } WIRE_ATTR kib_connparams_t;
269
270 typedef struct
271 {
272         union {
273                 ib_mr_t         *mr;
274                 ib_fmr_t        *fmr;
275         }                 md_handle;
276         __u32             md_lkey;
277         __u32             md_rkey;
278         __u64             md_addr;
279 } kib_md_t;
280
281 typedef struct
282 {
283         __u32             rd_key;               /* remote key */
284         __u32             rd_nob;               /* # of bytes */
285         __u64             rd_addr;              /* remote io vaddr */
286 } WIRE_ATTR kib_rdma_desc_t;
287
288 typedef struct
289 {
290         lnet_hdr_t        ibim_hdr;             /* portals header */
291         char              ibim_payload[0];      /* piggy-backed payload */
292 } WIRE_ATTR kib_immediate_msg_t;
293
294 typedef struct
295 {
296         lnet_hdr_t        ibrm_hdr;             /* portals header */
297         __u64             ibrm_cookie;          /* opaque completion cookie */
298         kib_rdma_desc_t   ibrm_desc;            /* where to suck/blow */
299 } WIRE_ATTR kib_rdma_msg_t;
300
301 typedef struct
302 {
303         __u64             ibcm_cookie;          /* opaque completion cookie */
304         __u32             ibcm_status;          /* completion status */
305 } WIRE_ATTR kib_completion_msg_t;
306
307 typedef struct
308 {
309         /* First 2 fields fixed FOR ALL TIME */
310         __u32             ibm_magic;            /* I'm an openibnal message */
311         __u16             ibm_version;          /* this is my version number */
312
313         __u8              ibm_type;             /* msg type */
314         __u8              ibm_credits;          /* returned credits */
315         __u32             ibm_nob;              /* # bytes in whole message */
316         __u32             ibm_cksum;            /* checksum (0 == no checksum) */
317         __u64             ibm_srcnid;           /* sender's NID */
318         __u64             ibm_srcstamp;         /* sender's incarnation */
319         __u64             ibm_dstnid;           /* destination's NID */
320         __u64             ibm_dststamp;         /* destination's incarnation */
321         union {
322                 kib_svcrsp_t          svcrsp;
323                 kib_connparams_t      connparams;
324                 kib_immediate_msg_t   immediate;
325                 kib_rdma_msg_t        rdma;
326                 kib_completion_msg_t  completion;
327         } WIRE_ATTR       ibm_u;
328 } WIRE_ATTR kib_msg_t;
329
330 #define IBNAL_MSG_MAGIC LNET_PROTO_OPENIB_MAGIC /* unique magic */
331 #define IBNAL_MSG_VERSION_RDMAREPLYNOTRSRVD 2   /* previous protocol version */
332 #define IBNAL_MSG_VERSION              3        /* current protocol version */
333
334 #define IBNAL_MSG_SVCQRY            0xb0        /* service query */
335 #define IBNAL_MSG_SVCRSP            0xb1        /* service response */
336 #define IBNAL_MSG_CONNREQ           0xc0        /* connection request */
337 #define IBNAL_MSG_CONNACK           0xc1        /* connection acknowledge */
338 #define IBNAL_MSG_NOOP              0xd0        /* nothing (just credits) */
339 #define IBNAL_MSG_IMMEDIATE         0xd1        /* portals hdr + payload */
340 #define IBNAL_MSG_PUT_RDMA          0xd2        /* portals PUT hdr + source rdma desc */
341 #define IBNAL_MSG_PUT_DONE          0xd3        /* signal PUT rdma completion */
342 #define IBNAL_MSG_GET_RDMA          0xd4        /* portals GET hdr + sink rdma desc */
343 #define IBNAL_MSG_GET_DONE          0xd5        /* signal GET rdma completion */
344
345 /***********************************************************************/
346
347 typedef struct kib_rx                           /* receive message */
348 {
349         struct list_head          rx_list;      /* queue for attention */
350         struct kib_conn          *rx_conn;      /* owning conn */
351         int                       rx_nob;       /* # bytes received (-1 while posted) */
352         __u64                     rx_vaddr;     /* pre-mapped buffer (hca vaddr) */
353         kib_msg_t                *rx_msg;       /* pre-mapped buffer (host vaddr) */
354         struct ib_receive_param   rx_sp;        /* receive work item */
355         struct ib_gather_scatter  rx_gl;        /* and it's memory */
356 } kib_rx_t;
357
358 typedef struct kib_tx                           /* transmit message */
359 {
360         struct list_head          tx_list;      /* queue on idle_txs ibc_tx_queue etc. */
361         struct kib_conn          *tx_conn;      /* owning conn */
362         int                       tx_mapped;    /* mapped for RDMA? */
363         int                       tx_sending;   /* # tx callbacks outstanding */
364         int                       tx_status;    /* completion status */
365         unsigned long             tx_deadline;  /* completion deadline */
366         int                       tx_passive_rdma; /* peer sucks/blows */
367         int                       tx_passive_rdma_wait; /* waiting for peer to complete */
368         __u64                     tx_passive_rdma_cookie; /* completion cookie */
369         lnet_msg_t               *tx_lntmsg[2]; /* ptl msgs to finalize on completion */
370         kib_md_t                  tx_md;        /* RDMA mapping (active/passive) */
371         __u64                     tx_vaddr;     /* pre-mapped buffer (hca vaddr) */
372         kib_msg_t                *tx_msg;       /* pre-mapped buffer (host vaddr) */
373         int                       tx_nsp;       /* # send work items */
374         struct ib_send_param      tx_sp[2];     /* send work items... */
375         struct ib_gather_scatter  tx_gl[2];     /* ...and their memory */
376 } kib_tx_t;
377
378 #define KIB_TX_UNMAPPED       0
379 #define KIB_TX_MAPPED         1
380 #define KIB_TX_MAPPED_FMR     2
381
382 typedef struct kib_connreq
383 {
384         /* active connection-in-progress state */
385         struct kib_conn           *cr_conn;
386         kib_msg_t                  cr_msg;
387         __u64                      cr_tid;
388         tTS_IB_GID                 cr_gid;
389         kib_svcrsp_t               cr_svcrsp;
390         struct ib_path_record      cr_path;
391         struct ib_cm_active_param  cr_connparam;
392 } kib_connreq_t;
393
394 typedef struct kib_conn
395 {
396         struct kib_peer    *ibc_peer;           /* owning peer */
397         struct list_head    ibc_list;           /* stash on peer's conn list */
398         __u64               ibc_incarnation;    /* which instance of the peer */
399         int                 ibc_version;        /* peer protocol version */
400         atomic_t            ibc_refcount;       /* # users */
401         int                 ibc_state;          /* what's happening */
402         int                 ibc_nsends_posted;  /* # uncompleted sends */
403         int                 ibc_credits;        /* # credits I have */
404         int                 ibc_outstanding_credits; /* # credits to return */
405         int                 ibc_reserved_credits; /* # credits for ACK/DONE msgs */
406         unsigned long       ibc_last_send;      /* time of last send */
407         struct list_head    ibc_tx_queue_nocred; /* sends that don't need a credit */
408         struct list_head    ibc_tx_queue_rsrvd; /* sends that need a reserved cred */
409         struct list_head    ibc_tx_queue;       /* send queue */
410         struct list_head    ibc_active_txs;     /* active tx awaiting completion */
411         spinlock_t          ibc_lock;           /* serialise */
412         kib_rx_t           *ibc_rxs;            /* the rx descs */
413         kib_pages_t        *ibc_rx_pages;       /* premapped rx msg pages */
414         ib_qp_t            *ibc_qp;             /* queue pair */
415         __u32               ibc_qpn;            /* queue pair number */
416         tTS_IB_CM_COMM_ID   ibc_comm_id;        /* connection ID? */
417         kib_connreq_t      *ibc_connreq;        /* connection request state */
418 } kib_conn_t;
419
420 #define IBNAL_CONN_INIT_NOTHING      0          /* initial state */
421 #define IBNAL_CONN_INIT_QP           1          /* ibc_qp set up */
422 #define IBNAL_CONN_CONNECTING        2          /* started to connect */
423 #define IBNAL_CONN_ESTABLISHED       3          /* connection established */
424 #define IBNAL_CONN_DEATHROW          4          /* waiting to be closed */
425 #define IBNAL_CONN_ZOMBIE            5          /* waiting to be freed */
426
427 typedef struct kib_peer
428 {
429         struct list_head    ibp_list;           /* stash on global peer list */
430         struct list_head    ibp_connd_list;     /* schedule on kib_connd_peers */
431         lnet_nid_t          ibp_nid;            /* who's on the other end(s) */
432         __u32               ibp_ip;             /* IP to query for peer conn params */
433         int                 ibp_port;           /* port to qery for peer conn params */
434         __u64               ibp_incarnation;    /* peer's incarnation */
435         atomic_t            ibp_refcount;       /* # users */
436         int                 ibp_persistence;    /* "known" peer refs */
437         struct list_head    ibp_conns;          /* all active connections */
438         struct list_head    ibp_tx_queue;       /* msgs waiting for a conn */
439         int                 ibp_connecting;     /* current active connection attempts */
440         int                 ibp_accepting;      /* current passive connection attempts */
441         unsigned long       ibp_reconnect_time; /* when reconnect may be attempted */
442         unsigned long       ibp_reconnect_interval; /* exponential backoff */
443         int                 ibp_error;          /* errno on closing this peer */
444         cfs_time_t          ibp_last_alive;     /* when (in jiffies) I was last alive */
445 } kib_peer_t;
446
447 extern kib_data_t      kibnal_data;
448 extern kib_tunables_t  kibnal_tunables;
449
450 /******************************************************************************/
451
452 /* these are purposely avoiding using local vars so they don't increase
453  * stack consumption. */
454
455 #define kibnal_conn_addref(conn)                                \
456 do {                                                            \
457         CDEBUG(D_NET, "conn[%p] (%d)++\n",                      \
458                (conn), atomic_read(&(conn)->ibc_refcount));     \
459         LASSERT(atomic_read(&(conn)->ibc_refcount) > 0);        \
460         atomic_inc(&(conn)->ibc_refcount);                      \
461 } while (0)
462
463 #define kibnal_conn_decref(conn)                                              \
464 do {                                                                          \
465         unsigned long   flags;                                                \
466                                                                               \
467         CDEBUG(D_NET, "conn[%p] (%d)--\n",                                    \
468                (conn), atomic_read(&(conn)->ibc_refcount));                   \
469         LASSERT(atomic_read(&(conn)->ibc_refcount) > 0);                      \
470         if (atomic_dec_and_test(&(conn)->ibc_refcount)) {                     \
471                 spin_lock_irqsave(&kibnal_data.kib_reaper_lock, flags);       \
472                 list_add_tail(&(conn)->ibc_list,                              \
473                               &kibnal_data.kib_reaper_conns);                 \
474                 wake_up(&kibnal_data.kib_reaper_waitq);                       \
475                 spin_unlock_irqrestore(&kibnal_data.kib_reaper_lock, flags);  \
476         }                                                                     \
477 } while (0)
478
479 #define kibnal_peer_addref(peer)                                \
480 do {                                                            \
481         CDEBUG(D_NET, "peer[%p] -> %s (%d)++\n",                \
482                (peer), libcfs_nid2str((peer)->ibp_nid),         \
483                atomic_read (&(peer)->ibp_refcount));            \
484         LASSERT(atomic_read(&(peer)->ibp_refcount) > 0);        \
485         atomic_inc(&(peer)->ibp_refcount);                      \
486 } while (0)
487
488 #define kibnal_peer_decref(peer)                                \
489 do {                                                            \
490         CDEBUG(D_NET, "peer[%p] -> %s (%d)--\n",                \
491                (peer), libcfs_nid2str((peer)->ibp_nid),         \
492                atomic_read (&(peer)->ibp_refcount));            \
493         LASSERT(atomic_read(&(peer)->ibp_refcount) > 0);        \
494         if (atomic_dec_and_test(&(peer)->ibp_refcount))         \
495                 kibnal_destroy_peer(peer);                      \
496 } while (0)
497
498 /******************************************************************************/
499
500 static inline struct list_head *
501 kibnal_nid2peerlist (lnet_nid_t nid)
502 {
503         unsigned int hash = ((unsigned int)nid) % kibnal_data.kib_peer_hash_size;
504
505         return (&kibnal_data.kib_peers [hash]);
506 }
507
508 static inline int
509 kibnal_peer_active(kib_peer_t *peer)
510 {
511         /* Am I in the peer hash table? */
512         return (!list_empty(&peer->ibp_list));
513 }
514
515 static inline void
516 kibnal_queue_tx_locked (kib_tx_t *tx, kib_conn_t *conn)
517 {
518         struct list_head      *q;
519
520         LASSERT (tx->tx_nsp > 0);               /* work items set up */
521         LASSERT (tx->tx_conn == NULL);          /* only set here */
522
523         kibnal_conn_addref(conn);
524         tx->tx_conn = conn;
525         tx->tx_deadline = jiffies + *kibnal_tunables.kib_timeout * HZ;
526
527         if (conn->ibc_version == IBNAL_MSG_VERSION_RDMAREPLYNOTRSRVD) {
528                 /* All messages have simple credit control */
529                 q = &conn->ibc_tx_queue;
530         } else {
531                 LASSERT (conn->ibc_version == IBNAL_MSG_VERSION);
532                 
533                 switch (tx->tx_msg->ibm_type) {
534                 case IBNAL_MSG_PUT_RDMA:
535                 case IBNAL_MSG_GET_RDMA:
536                         /* RDMA request: reserve a buffer for the RDMA reply
537                          * before sending */
538                         q = &conn->ibc_tx_queue_rsrvd;
539                         break;
540
541                 case IBNAL_MSG_PUT_DONE:
542                 case IBNAL_MSG_GET_DONE:
543                         /* RDMA completion: no credits; peer has reserved a
544                          * reply buffer */
545                         q = &conn->ibc_tx_queue_nocred;
546                         break;
547                 
548                 case IBNAL_MSG_NOOP:
549                 case IBNAL_MSG_IMMEDIATE:
550                         /* Otherwise: consume a credit before sending */
551                         q = &conn->ibc_tx_queue;
552                         break;
553                 
554                 default:
555                         LBUG();
556                         q = NULL;
557                 }
558         }
559
560         list_add_tail(&tx->tx_list, q);
561 }
562
563 static inline int
564 kibnal_send_keepalive(kib_conn_t *conn) 
565 {
566         return (*kibnal_tunables.kib_keepalive > 0) &&
567                 time_after(jiffies, conn->ibc_last_send +
568                            *kibnal_tunables.kib_keepalive*HZ);
569 }
570
571 /* CAVEAT EMPTOR:
572  * We rely on tx/rx descriptor alignment to allow us to use the lowest bit
573  * of the work request id as a flag to determine if the completion is for a
574  * transmit or a receive.  It seems that that the CQ entry's 'op' field
575  * isn't always set correctly on completions that occur after QP teardown. */
576
577 static inline __u64
578 kibnal_ptr2wreqid (void *ptr, int isrx)
579 {
580         unsigned long lptr = (unsigned long)ptr;
581
582         LASSERT ((lptr & 1) == 0);
583         return (__u64)(lptr | (isrx ? 1 : 0));
584 }
585
586 static inline void *
587 kibnal_wreqid2ptr (__u64 wreqid)
588 {
589         return (void *)(((unsigned long)wreqid) & ~1UL);
590 }
591
592 static inline int
593 kibnal_wreqid_is_rx (__u64 wreqid)
594 {
595         return (wreqid & 1) != 0;
596 }
597
598 #if (IB_NTXRXPARAMS == 3)
599 static inline int
600 kibnal_ib_send(ib_qp_t *qp, struct ib_send_param *p)
601 {
602         return ib_send(qp, p, 1);
603 }
604
605 static inline int
606 kibnal_ib_receive(ib_qp_t *qp, struct ib_receive_param *p)
607 {
608         return ib_receive(qp, p, 1);
609 }
610 #elif (IB_NTXRXPARAMS == 4)
611 static inline int
612 kibnal_ib_send(ib_qp_t *qp, struct ib_send_param *p)
613 {
614         return ib_send(qp, p, 1, NULL);
615 }
616
617 static inline int
618 kibnal_ib_receive(ib_qp_t *qp, struct ib_receive_param *p)
619 {
620         return ib_receive(qp, p, 1, NULL);
621 }
622 #else
623  #error "IB_NTXRXPARAMS not set correctly"
624 #endif
625
626 int kibnal_startup (lnet_ni_t *ni);
627 void kibnal_shutdown (lnet_ni_t *ni);
628 int kibnal_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg);
629 int kibnal_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg);
630 int kibnal_eager_recv (lnet_ni_t *ni, void *private, 
631                        lnet_msg_t *lntmsg, void **new_private);
632 int kibnal_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, 
633                 int delayed, unsigned int niov, 
634                 struct iovec *iov, lnet_kiov_t *kiov,
635                 unsigned int offset, unsigned int mlen, unsigned int rlen);
636 int kibnal_accept(lnet_ni_t *ni, struct socket *sock);
637
638 extern void kibnal_init_msg(kib_msg_t *msg, int type, int body_nob);
639 extern void kibnal_pack_msg(kib_msg_t *msg, int version, int credits, 
640                             lnet_nid_t dstnid, __u64 dststamp);
641 extern int kibnal_unpack_msg(kib_msg_t *msg, int expected_version, int nob);
642 extern void kibnal_handle_svcqry (struct socket *sock);
643 extern int kibnal_make_svcqry (kib_conn_t *conn);
644 extern void kibnal_free_acceptsock (kib_acceptsock_t *as);
645 extern int kibnal_create_peer (kib_peer_t **peerp, lnet_nid_t nid);
646 extern void kibnal_destroy_peer (kib_peer_t *peer);
647 extern int kibnal_add_persistent_peer(lnet_nid_t nid, __u32 ip, int port);
648 extern int kibnal_del_peer (lnet_nid_t nid);
649 extern kib_peer_t *kibnal_find_peer_locked (lnet_nid_t nid);
650 extern void kibnal_unlink_peer_locked (kib_peer_t *peer);
651 extern void kibnal_peer_alive(kib_peer_t *peer);
652 extern int  kibnal_close_stale_conns_locked (kib_peer_t *peer,
653                                               __u64 incarnation);
654 extern kib_conn_t *kibnal_create_conn (void);
655 extern void kibnal_destroy_conn (kib_conn_t *conn);
656 extern int kibnal_alloc_pages (kib_pages_t **pp, int npages, int access);
657 extern void kibnal_free_pages (kib_pages_t *p);
658
659 extern void kibnal_check_sends (kib_conn_t *conn);
660
661 extern tTS_IB_CM_CALLBACK_RETURN
662 kibnal_bad_conn_callback (tTS_IB_CM_EVENT event, tTS_IB_CM_COMM_ID cid,
663                           void *param, void *arg);
664 extern tTS_IB_CM_CALLBACK_RETURN
665 kibnal_conn_callback (tTS_IB_CM_EVENT event, tTS_IB_CM_COMM_ID cid,
666                        void *param, void *arg);
667 extern tTS_IB_CM_CALLBACK_RETURN
668 kibnal_passive_conn_callback (tTS_IB_CM_EVENT event, tTS_IB_CM_COMM_ID cid,
669                                void *param, void *arg);
670
671 extern void kibnal_close_conn_locked (kib_conn_t *conn, int error);
672 extern void kibnal_destroy_conn (kib_conn_t *conn);
673 extern int  kibnal_thread_start (int (*fn)(void *arg), void *arg);
674 extern int  kibnal_scheduler(void *arg);
675 extern int  kibnal_connd (void *arg);
676 extern int  kibnal_reaper (void *arg);
677 extern void kibnal_callback (ib_cq_t *cq, struct ib_cq_entry *e, void *arg);
678 extern void kibnal_txlist_done (struct list_head *txlist, int status);
679 extern void kibnal_init_tx_msg (kib_tx_t *tx, int type, int body_nob);
680 extern int  kibnal_close_conn (kib_conn_t *conn, int why);
681 extern void kibnal_start_active_rdma (int type, int status,
682                                       kib_rx_t *rx, lnet_msg_t *lntmsg,
683                                       unsigned int niov,
684                                       struct iovec *iov, lnet_kiov_t *kiov,
685                                       int offset, int nob);
686
687 extern int  kibnal_tunables_init(void);
688 extern void kibnal_tunables_fini(void);