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