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