Whamcloud - gitweb
Landing b_hd_newconfig on HEAD
[fs/lustre-release.git] / lnet / klnds / iiblnd / iiblnd.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
28 #include <linux/config.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/mm.h>
32 #include <linux/string.h>
33 #include <linux/stat.h>
34 #include <linux/errno.h>
35 #include <linux/smp_lock.h>
36 #include <linux/unistd.h>
37 #include <linux/uio.h>
38
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
41 #include <asm/io.h>
42
43 #include <linux/init.h>
44 #include <linux/fs.h>
45 #include <linux/file.h>
46 #include <linux/stat.h>
47 #include <linux/list.h>
48 #include <linux/kmod.h>
49 #include <linux/sysctl.h>
50
51 #define DEBUG_SUBSYSTEM S_LND
52
53 #include <libcfs/kp30.h>
54 #include <lnet/lnet.h>
55 #include <lnet/lib-lnet.h>
56
57 #include <linux/iba/ibt.h>
58
59 #define GCC_VERSION (__GNUC__ * 10000 \
60                 + __GNUC_MINOR__ * 100 \
61                 + __GNUC_PATCHLEVEL__)
62
63 /* Test for GCC > 3.2.2 */
64 #if GCC_VERSION <= 30202
65 /* GCC 3.2.2, and presumably several versions before it, will
66  * miscompile this driver. See
67  * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9853. */
68 #error Invalid GCC version. Must use GCC >= 3.2.3
69 #endif
70
71 #if CONFIG_SMP
72 # define IBNAL_N_SCHED      num_online_cpus()   /* # schedulers */
73 #else
74 # define IBNAL_N_SCHED      1                   /* # schedulers */
75 #endif
76
77 #define IBNAL_FMR                    0          /* map on demand v. use whole mem mapping */
78
79 /* tunables fixed at compile time */
80 #define IBNAL_PEER_HASH_SIZE         101        /* # peer lists */
81 #define IBNAL_RESCHED                100        /* # scheduler loops before reschedule */
82 #define IBNAL_MSG_QUEUE_SIZE         8          /* # messages/RDMAs in-flight */
83 #define IBNAL_CREDIT_HIGHWATER       7          /* when to eagerly return credits */
84 #define IBNAL_MSG_SIZE              (4<<10)     /* max size of queued messages (inc hdr) */
85 #define IBNAL_RDMA_BASE              0x0eeb0000
86 #define IBNAL_STARTING_PSN           1
87
88 /* QP tunables */
89 /* 7 indicates infinite retry attempts, Infinicon recommended 5 */
90 #define IBNAL_RETRY                  5          /* # times to retry */
91 #define IBNAL_RNR_RETRY              5          /*  */
92 #define IBNAL_CM_RETRY               5          /* # times to retry connection */
93 #define IBNAL_FLOW_CONTROL           1
94 #define IBNAL_ACK_TIMEOUT            20         /* supposedly 4 secs */
95 #define IBNAL_EE_FLOW                1
96 #define IBNAL_LOCAL_SUB              1
97 #define IBNAL_FAILOVER_ACCEPTED      0
98
99 /************************/
100 /* derived constants... */
101
102 /* TX messages (shared by all connections) */
103 #define IBNAL_TX_MSGS()       (*kibnal_tunables.kib_ntx)
104 #define IBNAL_TX_MSG_BYTES()  (IBNAL_TX_MSGS() * IBNAL_MSG_SIZE)
105 #define IBNAL_TX_MSG_PAGES()  ((IBNAL_TX_MSG_BYTES() + PAGE_SIZE - 1)/PAGE_SIZE)
106
107 #if IBNAL_USE_FMR
108 # define IBNAL_MAX_RDMA_FRAGS 1
109 # define IBNAL_CONCURRENT_SENDS IBNAL_RX_MSGS
110 #else
111 # define IBNAL_MAX_RDMA_FRAGS LNET_MAX_IOV
112 # define IBNAL_CONCURRENT_SENDS IBNAL_MSG_QUEUE_SIZE
113 #endif
114
115 /* RX messages (per connection) */
116 #define IBNAL_RX_MSGS         (IBNAL_MSG_QUEUE_SIZE * 2)
117 #define IBNAL_RX_MSG_BYTES    (IBNAL_RX_MSGS * IBNAL_MSG_SIZE)
118 #define IBNAL_RX_MSG_PAGES    ((IBNAL_RX_MSG_BYTES + PAGE_SIZE - 1)/PAGE_SIZE)
119
120 #define IBNAL_CQ_ENTRIES()  (IBNAL_TX_MSGS() * (1 + IBNAL_MAX_RDMA_FRAGS) +             \
121                              (IBNAL_RX_MSGS * *kibnal_tunables.kib_concurrent_peers))
122
123 typedef struct
124 {
125         char            **kib_hca_basename;     /* HCA base name */
126         char            **kib_ipif_basename;    /* IPoIB interface base name */
127         char            **kib_service_name;     /* global service name */
128         unsigned int     *kib_service_number;   /* global service number */
129         int              *kib_min_reconnect_interval; /* min connect retry seconds... */
130         int              *kib_max_reconnect_interval; /* max connect retry seconds */
131         int              *kib_concurrent_peers; /* max # peers */
132         int              *kib_cksum;            /* checksum kib_msg_t? */
133         int              *kib_timeout;          /* comms timeout (seconds) */
134         int              *kib_keepalive;        /* keepalive timeout (seconds) */
135         int              *kib_ntx;              /* # tx descs */
136         int              *kib_credits;          /* # concurrent sends */
137         int              *kib_peercredits;      /* # concurrent sends to 1 peer */
138         int              *kib_sd_retries;       /* # concurrent sends to 1 peer */
139         int              *kib_concurrent_sends; /* send work queue sizing */
140 #if CONFIG_SYSCTL && !CFS_SYSFS_MODULE_PARM
141         struct ctl_table_header *kib_sysctl;    /* sysctl interface */
142 #endif
143 } kib_tunables_t;
144
145 /* NB The Infinicon stack has specific typedefs for some things
146  * (e.g. IB_{L,R}_KEY), that just map back to __u32 etc */
147 typedef struct
148 {
149         int               ibp_npages;           /* # pages */
150         struct page      *ibp_pages[0];
151 } kib_pages_t;
152
153 typedef struct
154 {
155         IB_HANDLE         md_handle;
156         __u32             md_lkey;
157         __u32             md_rkey;
158         __u64             md_addr;
159 } kib_md_t;
160
161 typedef struct
162 {
163         int               kib_init;             /* initialisation state */
164         __u64             kib_incarnation;      /* which one am I */
165         int               kib_shutdown;         /* shut down? */
166         atomic_t          kib_nthreads;         /* # live threads */
167         lnet_ni_t        *kib_ni;               /* _the_ iib instance */
168
169         __u64             kib_port_guid;        /* my GUID (lo 64 of GID)*/
170         __u16             kib_port_pkey;        /* my pkey, whatever that is */
171         struct semaphore  kib_listener_signal;  /* signal completion */
172         IB_HANDLE         kib_listener_cep;     /* connection end point */
173
174         rwlock_t          kib_global_lock;      /* stabilize peer/conn ops */
175         int               kib_ready;            /* CQ callback fired */
176         int               kib_checking_cq;      /* a scheduler is checking the CQ */
177
178         struct list_head *kib_peers;            /* hash table of all my known peers */
179         int               kib_peer_hash_size;   /* size of kib_peers */
180         atomic_t          kib_npeers;           /* # peers extant */
181         atomic_t          kib_nconns;           /* # connections extant */
182
183         struct list_head  kib_connd_zombies;    /* connections to free */
184         struct list_head  kib_connd_conns;      /* connections to progress */
185         struct list_head  kib_connd_peers;      /* peers waiting for a connection */
186         wait_queue_head_t kib_connd_waitq;      /* connection daemon sleep here */
187         spinlock_t        kib_connd_lock;       /* serialise */
188
189         wait_queue_head_t kib_sched_waitq;      /* schedulers sleep here */
190         spinlock_t        kib_sched_lock;       /* serialise */
191
192         struct kib_tx    *kib_tx_descs;         /* all the tx descriptors */
193         kib_pages_t      *kib_tx_pages;         /* premapped tx msg pages */
194
195         struct list_head  kib_idle_txs;         /* idle tx descriptors */
196         __u64             kib_next_tx_cookie;   /* RDMA completion cookie */
197         spinlock_t        kib_tx_lock;          /* serialise */
198
199         IB_HANDLE         kib_hca;              /* The HCA */
200         int               kib_port;             /* port on the device */
201         IB_HANDLE         kib_pd;               /* protection domain */
202         IB_HANDLE         kib_sd;               /* SD handle */
203         IB_HANDLE         kib_cq;               /* completion queue */
204         kib_md_t          kib_whole_mem;        /* whole-mem registration */
205
206         int               kib_hca_idx;          /* my HCA number */
207         uint64            kib_hca_guids[8];     /* all the HCA guids */
208         IB_CA_ATTRIBUTES  kib_hca_attrs;        /* where to get HCA attrs */
209
210         COMMAND_CONTROL_PARAMETERS kib_sdretry; /* control SD query retries */
211 } kib_data_t;
212
213 #define IBNAL_INIT_NOTHING         0
214 #define IBNAL_INIT_DATA            1
215 #define IBNAL_INIT_LIB             2
216 #define IBNAL_INIT_HCA             3
217 #define IBNAL_INIT_PORTATTRS       4
218 #define IBNAL_INIT_SD              5
219 #define IBNAL_INIT_PD              6
220 #define IBNAL_INIT_MD              7
221 #define IBNAL_INIT_TXD             8
222 #define IBNAL_INIT_CQ              9
223 #define IBNAL_INIT_ALL             10
224
225 /************************************************************************
226  * Wire message structs.
227  * These are sent in sender's byte order (i.e. receiver flips).
228  * CAVEAT EMPTOR: other structs communicated between nodes (e.g. MAD
229  * private data and SM service info), is LE on the wire.
230  */
231
232 typedef struct kib_connparams
233 {
234         __u32             ibcp_queue_depth;
235         __u32             ibcp_max_msg_size;
236         __u32             ibcp_max_frags;
237 } WIRE_ATTR kib_connparams_t;
238
239 typedef struct
240 {
241         lnet_hdr_t        ibim_hdr;             /* portals header */
242         char              ibim_payload[0];      /* piggy-backed payload */
243 } WIRE_ATTR kib_immediate_msg_t;
244
245 #if IBNAL_USE_FMR
246 typedef struct
247 {
248         __u64             rd_addr;              /* IO VMA address */
249         __u32             rd_nob;               /* # of bytes */
250         __u32             rd_key;               /* remote key */
251 } WIRE_ATTR kib_rdma_desc_t;
252 #else
253 typedef struct
254 {
255         __u32             rf_nob;               /* # of bytes */
256         __u64             rf_addr;              /* remote io vaddr */
257 } WIRE_ATTR kib_rdma_frag_t;
258
259 typedef struct
260 {
261         __u32             rd_key;               /* local/remote key */
262         __u32             rd_nfrag;             /* # fragments */
263         kib_rdma_frag_t   rd_frags[0];          /* buffer frags */
264 } WIRE_ATTR kib_rdma_desc_t;
265 #endif
266
267 typedef struct
268 {
269         lnet_hdr_t        ibprm_hdr;            /* LNET header */
270         __u64             ibprm_cookie;         /* opaque completion cookie */
271 } WIRE_ATTR kib_putreq_msg_t;
272
273 typedef struct
274 {
275         __u64             ibpam_src_cookie;     /* reflected completion cookie */
276         __u64             ibpam_dst_cookie;     /* opaque completion cookie */
277         kib_rdma_desc_t   ibpam_rd;             /* sender's sink buffer */
278 } WIRE_ATTR kib_putack_msg_t;
279
280 typedef struct
281 {
282         lnet_hdr_t        ibgm_hdr;             /* LNET header */
283         __u64             ibgm_cookie;          /* opaque completion cookie */
284         kib_rdma_desc_t   ibgm_rd;              /* sender's sink buffer */
285 } WIRE_ATTR kib_get_msg_t;
286
287 typedef struct
288 {
289         __u64             ibcm_cookie;          /* opaque completion cookie */
290         __u32             ibcm_status;          /* completion status */
291 } WIRE_ATTR kib_completion_msg_t;
292
293 typedef struct
294 {
295         /* First 2 fields fixed FOR ALL TIME */
296         __u32             ibm_magic;            /* I'm an openibnal message */
297         __u16             ibm_version;          /* this is my version number */
298
299         __u8              ibm_type;             /* msg type */
300         __u8              ibm_credits;          /* returned credits */
301         __u32             ibm_nob;              /* # bytes in whole message */
302         __u32             ibm_cksum;            /* checksum (0 == no checksum) */
303         __u64             ibm_srcnid;           /* sender's NID */
304         __u64             ibm_srcstamp;         /* sender's incarnation */
305         __u64             ibm_dstnid;           /* destination's NID */
306         __u64             ibm_dststamp;         /* destination's incarnation */
307         __u64             ibm_seq;              /* sequence number */
308
309         union {
310                 kib_connparams_t      connparams;
311                 kib_immediate_msg_t   immediate;
312                 kib_putreq_msg_t      putreq;
313                 kib_putack_msg_t      putack;
314                 kib_get_msg_t         get;
315                 kib_completion_msg_t  completion;
316         } WIRE_ATTR ibm_u;
317 } WIRE_ATTR kib_msg_t;
318
319 #define IBNAL_MSG_MAGIC LNET_PROTO_IIB_MAGIC    /* unique magic */
320 #define IBNAL_MSG_VERSION              2        /* current protocol version */
321 #define IBNAL_MSG_VERSION_RDMAREPLYNOTRSRVD 1   /* previous version */
322
323 #define IBNAL_MSG_CONNREQ           0xc0        /* connection request */
324 #define IBNAL_MSG_CONNACK           0xc1        /* connection acknowledge */
325 #define IBNAL_MSG_NOOP              0xd0        /* nothing (just credits) */
326 #define IBNAL_MSG_IMMEDIATE         0xd1        /* immediate */
327 #define IBNAL_MSG_PUT_REQ           0xd2        /* putreq (src->sink) */
328 #define IBNAL_MSG_PUT_NAK           0xd3        /* completion (sink->src) */
329 #define IBNAL_MSG_PUT_ACK           0xd4        /* putack (sink->src) */
330 #define IBNAL_MSG_PUT_DONE          0xd5        /* completion (src->sink) */
331 #define IBNAL_MSG_GET_REQ           0xd6        /* getreq (sink->src) */
332 #define IBNAL_MSG_GET_DONE          0xd7        /* completion (src->sink: all OK) */
333
334 /* connection rejection reasons */
335 #define IBNAL_REJECT_CONN_RACE       0          /* You lost connection race */
336 #define IBNAL_REJECT_NO_RESOURCES    1          /* Out of memory/conns etc */
337 #define IBNAL_REJECT_FATAL           2          /* Anything else */
338
339 /***********************************************************************/
340
341 typedef struct kib_rx                           /* receive message */
342 {
343         struct list_head          rx_list;      /* queue for attention */
344         struct kib_conn          *rx_conn;      /* owning conn */
345         int                       rx_nob;       /* # bytes received (-1 while posted) */
346         __u64                     rx_hca_msg;   /* pre-mapped buffer (hca vaddr) */
347         kib_msg_t                *rx_msg;       /* pre-mapped buffer (host vaddr) */
348         IB_WORK_REQ2              rx_wrq;
349         IB_LOCAL_DATASEGMENT      rx_gl;        /* and its memory */
350 } kib_rx_t;
351
352 typedef struct kib_tx                           /* transmit message */
353 {
354         struct list_head          tx_list;      /* queue on idle_txs ibc_tx_queue etc. */
355         struct kib_conn          *tx_conn;      /* owning conn */
356         int                       tx_mapped;    /* mapped for RDMA? */
357         int                       tx_sending;   /* # tx callbacks outstanding */
358         int                       tx_queued;    /* queued for sending */
359         int                       tx_waiting;   /* waiting for peer */
360         int                       tx_status;    /* completion status */
361         unsigned long             tx_deadline;  /* completion deadline */
362         __u64                     tx_cookie;    /* completion cookie */
363         lnet_msg_t               *tx_lntmsg[2]; /* lnet msgs to finalize on completion */
364         kib_msg_t                *tx_msg;       /* pre-mapped buffer (host vaddr) */
365         __u64                     tx_hca_msg;   /* pre-mapped buffer (HCA vaddr) */
366         int                       tx_nwrq;      /* # send work items */
367 #if IBNAL_USE_FMR
368         IB_WORK_REQ2              tx_wrq[2];    /* send work items... */
369         IB_LOCAL_DATASEGMENT      tx_gl[2];     /* ...and their memory */
370         kib_rdma_desc_t           tx_rd[1];     /* rdma descriptor */
371         kib_md_t                  tx_md;        /* mapping */
372         __u64                    *tx_pages;     /* page phys addrs */
373 #else
374         IB_WORK_REQ2             *tx_wrq;       /* send work items... */
375         IB_LOCAL_DATASEGMENT     *tx_gl;        /* ...and their memory */
376         kib_rdma_desc_t          *tx_rd;        /* rdma descriptor (src buffers) */
377 #endif
378 } kib_tx_t;
379
380 typedef struct
381 {
382         /* scratchpad during connection establishment */
383         IB_QP_ATTRIBUTES_QUERY cv_qpattrs;
384         QUERY                  cv_query;
385         IB_SERVICE_RECORD      cv_svcrec;
386         IB_PATH_RECORD         cv_path;
387         CM_CONN_INFO           cv_cmci;
388 } kib_connvars_t;
389
390 typedef struct kib_conn
391 {
392         struct kib_peer    *ibc_peer;           /* owning peer */
393         struct list_head    ibc_list;           /* stash on peer's conn list */
394         __u64               ibc_incarnation;    /* which instance of the peer */
395         __u64               ibc_txseq;          /* tx sequence number */
396         __u64               ibc_rxseq;          /* rx sequence number */
397         __u32               ibc_version;        /* peer protocol version */
398         atomic_t            ibc_refcount;       /* # users */
399         int                 ibc_state;          /* what's happening */
400         int                 ibc_nsends_posted;  /* # uncompleted sends */
401         int                 ibc_credits;        /* # credits I have */
402         int                 ibc_outstanding_credits; /* # credits to return */
403         int                 ibc_reserved_credits; /* # credits for ACK/DONE msgs */
404         unsigned long       ibc_last_send;      /* time of last send */
405         struct list_head    ibc_early_rxs;      /* rxs completed before ESTABLISHED */
406         struct list_head    ibc_tx_queue_nocred; /* sends that don't need a cred */
407         struct list_head    ibc_tx_queue_rsrvd; /* sends that need a reserved cred */
408         struct list_head    ibc_tx_queue;       /* send queue */
409         struct list_head    ibc_active_txs;     /* active tx awaiting completion */
410         spinlock_t          ibc_lock;           /* serialise */
411         kib_rx_t           *ibc_rxs;            /* the rx descs */
412         kib_pages_t        *ibc_rx_pages;       /* premapped rx msg pages */
413         IB_HANDLE           ibc_qp;             /* queue pair */
414         IB_HANDLE           ibc_cep;            /* CM endpoint */
415         kib_connvars_t     *ibc_cvars;          /* connection scratchpad */
416 } kib_conn_t;
417
418 #define IBNAL_CONN_INIT_NOTHING      0          /* initial state */
419 #define IBNAL_CONN_INIT_QP           1          /* ibc_qp set up */
420 #define IBNAL_CONN_CONNECTING        2          /* started to connect */
421 #define IBNAL_CONN_ESTABLISHED       3          /* connection established */
422 #define IBNAL_CONN_DISCONNECTING     4          /* to send disconnect req */
423 #define IBNAL_CONN_DISCONNECTED      5          /* no more QP or CM traffic */
424
425 /* types of connection */
426 #define IBNAL_CONN_ACTIVE            0          /* active connect */
427 #define IBNAL_CONN_PASSIVE           1          /* passive connect */
428 #define IBNAL_CONN_WAITING           2          /* waiting for connect */
429
430 typedef struct kib_peer
431 {
432         struct list_head    ibp_list;           /* stash on global peer list */
433         struct list_head    ibp_connd_list;     /* schedule on kib_connd_peers */
434         lnet_nid_t          ibp_nid;            /* who's on the other end(s) */
435         atomic_t            ibp_refcount;       /* # users */
436         int                 ibp_persistence;    /* "known" peer refs */
437         int                 ibp_version;        /* protocol version */
438         struct list_head    ibp_conns;          /* all active connections */
439         struct list_head    ibp_tx_queue;       /* msgs waiting for a conn */
440         int                 ibp_connecting;     /* active connects in progress */
441         int                 ibp_accepting;      /* passive connects in progress */
442         int                 ibp_passivewait;    /* waiting for peer to connect */
443         unsigned long       ibp_passivewait_deadline; /* when passive wait must complete */
444         unsigned long       ibp_reconnect_time; /* when reconnect may be attempted */
445         unsigned long       ibp_reconnect_interval; /* exponential backoff */
446         int                 ibp_error;          /* errno on closing this peer */
447         cfs_time_t          ibp_last_alive;     /* when (in jiffies) I was last alive */
448 } kib_peer_t;
449
450
451 extern kib_data_t      kibnal_data;
452 extern kib_tunables_t  kibnal_tunables;
453
454 /******************************************************************************/
455
456 /* these are purposely avoiding using local vars so they don't increase
457  * stack consumption. */
458
459 #define kibnal_conn_addref(conn)                                \
460 do {                                                            \
461         CDEBUG(D_NET, "conn[%p] (%d)++\n",                      \
462                (conn), atomic_read(&(conn)->ibc_refcount));     \
463         LASSERT(atomic_read(&(conn)->ibc_refcount) > 0);        \
464         atomic_inc(&(conn)->ibc_refcount);                      \
465 } while (0)
466
467 #define kibnal_conn_decref(conn)                                              \
468 do {                                                                          \
469         unsigned long   flags;                                                \
470                                                                               \
471         CDEBUG(D_NET, "conn[%p] (%d)--\n",                                    \
472                (conn), atomic_read(&(conn)->ibc_refcount));                   \
473         LASSERT(atomic_read(&(conn)->ibc_refcount) > 0);                      \
474         if (atomic_dec_and_test(&(conn)->ibc_refcount)) {                     \
475                 spin_lock_irqsave(&kibnal_data.kib_connd_lock, flags);        \
476                 list_add_tail(&(conn)->ibc_list,                              \
477                               &kibnal_data.kib_connd_zombies);                \
478                 wake_up(&kibnal_data.kib_connd_waitq);                        \
479                 spin_unlock_irqrestore(&kibnal_data.kib_connd_lock, flags);   \
480         }                                                                     \
481 } while (0)
482
483 #define kibnal_peer_addref(peer)                                \
484 do {                                                            \
485         CDEBUG(D_NET, "peer[%p] -> %s (%d)++\n",                \
486                (peer), libcfs_nid2str((peer)->ibp_nid),         \
487                atomic_read (&(peer)->ibp_refcount));            \
488         LASSERT(atomic_read(&(peer)->ibp_refcount) > 0);        \
489         atomic_inc(&(peer)->ibp_refcount);                      \
490 } while (0)
491
492 #define kibnal_peer_decref(peer)                                \
493 do {                                                            \
494         CDEBUG(D_NET, "peer[%p] -> %s (%d)--\n",                \
495                (peer), libcfs_nid2str((peer)->ibp_nid),         \
496                atomic_read (&(peer)->ibp_refcount));            \
497         LASSERT(atomic_read(&(peer)->ibp_refcount) > 0);        \
498         if (atomic_dec_and_test(&(peer)->ibp_refcount))         \
499                 kibnal_destroy_peer(peer);                      \
500 } while (0)
501
502 /******************************************************************************/
503
504 static inline struct list_head *
505 kibnal_nid2peerlist (lnet_nid_t nid)
506 {
507         unsigned int hash = ((unsigned int)nid) % kibnal_data.kib_peer_hash_size;
508
509         return (&kibnal_data.kib_peers [hash]);
510 }
511
512 static inline int
513 kibnal_peer_active(kib_peer_t *peer)
514 {
515         /* Am I in the peer hash table? */
516         return (!list_empty(&peer->ibp_list));
517 }
518
519 static inline int
520 kibnal_peer_connecting(kib_peer_t *peer)
521 {
522         /* Am I expecting a connection to materialise? */
523         return (peer->ibp_connecting != 0 ||
524                 peer->ibp_accepting != 0 ||
525                 peer->ibp_passivewait);
526 }
527
528 static inline void
529 kibnal_queue_tx_locked (kib_tx_t *tx, kib_conn_t *conn)
530 {
531         struct list_head  *q;
532         
533         LASSERT (tx->tx_nwrq > 0);              /* work items set up */
534         LASSERT (!tx->tx_queued);               /* not queued for sending already */
535
536         tx->tx_queued = 1;
537         tx->tx_deadline = jiffies + (*kibnal_tunables.kib_timeout * HZ);
538
539         if (tx->tx_conn == NULL) {
540                 kibnal_conn_addref(conn);
541                 tx->tx_conn = conn;
542                 LASSERT (tx->tx_msg->ibm_type != IBNAL_MSG_PUT_DONE);
543         } else {
544                 LASSERT (tx->tx_conn == conn);
545                 LASSERT (tx->tx_msg->ibm_type == IBNAL_MSG_PUT_DONE);
546         }
547
548         if (conn->ibc_version == IBNAL_MSG_VERSION_RDMAREPLYNOTRSRVD) {
549                 /* All messages have simple credit control */
550                 q = &conn->ibc_tx_queue;
551         } else {
552                 LASSERT (conn->ibc_version == IBNAL_MSG_VERSION);
553                 
554                 switch (tx->tx_msg->ibm_type) {
555                 case IBNAL_MSG_PUT_REQ:
556                 case IBNAL_MSG_GET_REQ:
557                         /* RDMA request: reserve a buffer for the RDMA reply
558                          * before sending */
559                         q = &conn->ibc_tx_queue_rsrvd;
560                         break;
561
562                 case IBNAL_MSG_PUT_NAK:
563                 case IBNAL_MSG_PUT_ACK:
564                 case IBNAL_MSG_PUT_DONE:
565                 case IBNAL_MSG_GET_DONE:
566                         /* RDMA reply/completion: no credits; peer has reserved
567                          * a reply buffer */
568                         q = &conn->ibc_tx_queue_nocred;
569                         break;
570                 
571                 case IBNAL_MSG_NOOP:
572                 case IBNAL_MSG_IMMEDIATE:
573                         /* Otherwise: consume a credit before sending */
574                         q = &conn->ibc_tx_queue;
575                         break;
576                 
577                 default:
578                         LBUG();
579                         q = NULL;
580                 }
581         }
582         
583         list_add_tail(&tx->tx_list, q);
584 }
585
586 static inline int
587 kibnal_send_keepalive(kib_conn_t *conn) 
588 {
589         return (*kibnal_tunables.kib_keepalive > 0) &&
590                 time_after(jiffies, conn->ibc_last_send +
591                            *kibnal_tunables.kib_keepalive*HZ);
592 }
593
594 #define KIBNAL_SERVICE_KEY_MASK  (IB_SERVICE_RECORD_COMP_SERVICENAME |          \
595                                   IB_SERVICE_RECORD_COMP_SERVICEDATA8_1 |       \
596                                   IB_SERVICE_RECORD_COMP_SERVICEDATA8_2 |       \
597                                   IB_SERVICE_RECORD_COMP_SERVICEDATA8_3 |       \
598                                   IB_SERVICE_RECORD_COMP_SERVICEDATA8_4 |       \
599                                   IB_SERVICE_RECORD_COMP_SERVICEDATA8_5 |       \
600                                   IB_SERVICE_RECORD_COMP_SERVICEDATA8_6 |       \
601                                   IB_SERVICE_RECORD_COMP_SERVICEDATA8_7 |       \
602                                   IB_SERVICE_RECORD_COMP_SERVICEDATA8_8)
603
604 static inline __u64*
605 kibnal_service_nid_field(IB_SERVICE_RECORD *srv)
606 {
607         /* must be consistent with KIBNAL_SERVICE_KEY_MASK */
608         return (__u64 *)srv->ServiceData8;
609 }
610
611 static inline void
612 kibnal_set_service_keys(IB_SERVICE_RECORD *srv, lnet_nid_t nid)
613 {
614         char *svc_name = *kibnal_tunables.kib_service_name;
615
616         LASSERT (strlen(svc_name) < sizeof(srv->ServiceName));
617         memset (srv->ServiceName, 0, sizeof(srv->ServiceName));
618         strcpy (srv->ServiceName, svc_name);
619
620         *kibnal_service_nid_field(srv) = cpu_to_le64(nid);
621 }
622
623 /* CAVEAT EMPTOR: We rely on tx/rx descriptor alignment to allow us to use the
624  * lowest 2 bits of the work request id to stash the work item type (the op
625  * field is not valid when the wc completes in error). */
626
627 #define IBNAL_WID_TX    0
628 #define IBNAL_WID_RX    1
629 #define IBNAL_WID_RDMA  2
630 #define IBNAL_WID_MASK  3UL
631
632 static inline __u64
633 kibnal_ptr2wreqid (void *ptr, int type)
634 {
635         unsigned long lptr = (unsigned long)ptr;
636
637         LASSERT ((lptr & IBNAL_WID_MASK) == 0);
638         LASSERT ((type & ~IBNAL_WID_MASK) == 0);
639         return (__u64)(lptr | type);
640 }
641
642 static inline void *
643 kibnal_wreqid2ptr (__u64 wreqid)
644 {
645         return (void *)(((unsigned long)wreqid) & ~IBNAL_WID_MASK);
646 }
647
648 static inline int
649 kibnal_wreqid2type (__u64 wreqid)
650 {
651         return (wreqid & IBNAL_WID_MASK);
652 }
653
654 static inline void
655 kibnal_set_conn_state (kib_conn_t *conn, int state)
656 {
657         CDEBUG(D_NET,"%p state %d\n", conn, state);
658         conn->ibc_state = state;
659         mb();
660 }
661
662 #if IBNAL_USE_FMR
663
664 static inline int
665 kibnal_rd_size (kib_rdma_desc_t *rd) 
666 {
667         return rd->rd_nob;
668 }
669
670 #else
671 static inline int
672 kibnal_rd_size (kib_rdma_desc_t *rd)
673 {
674         int   i;
675         int   size;
676         
677         for (i = size = 0; i < rd->rd_nfrag; i++)
678                 size += rd->rd_frags[i].rf_nob;
679         
680         return size;
681 }
682 #endif
683
684 int  kibnal_startup (lnet_ni_t *ni);
685 void kibnal_shutdown (lnet_ni_t *ni);
686 int  kibnal_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg);
687 int  kibnal_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg);
688 int  kibnal_eager_recv (lnet_ni_t *ni, void *private, 
689                         lnet_msg_t *lntmsg, void **new_private);
690 int  kibnal_recv (lnet_ni_t *ni, void *private, lnet_msg_t *msg,
691                   int delayed, unsigned int niov,
692                   struct iovec *iov, lnet_kiov_t *kiov,
693                   unsigned int offset, unsigned int mlen, unsigned int rlen);
694 void kibnal_init_msg(kib_msg_t *msg, int type, int body_nob);
695 void kibnal_pack_msg(kib_msg_t *msg, __u32 version, int credits, 
696                      lnet_nid_t dstnid, __u64 dststamp, __u64 seq);
697 void kibnal_pack_connmsg(kib_msg_t *msg, __u32 version, int nob, int type,
698                          lnet_nid_t dstnid, __u64 dststamp);
699 int  kibnal_unpack_msg(kib_msg_t *msg, __u32 expected_version, int nob);
700 IB_HANDLE kibnal_create_cep(lnet_nid_t nid);
701 int  kibnal_create_peer (kib_peer_t **peerp, lnet_nid_t nid);
702 void kibnal_destroy_peer (kib_peer_t *peer);
703 kib_peer_t *kibnal_find_peer_locked (lnet_nid_t nid);
704 int  kibnal_del_peer (lnet_nid_t nid);
705 void kibnal_peer_alive (kib_peer_t *peer);
706 void kibnal_unlink_peer_locked (kib_peer_t *peer);
707 int  kibnal_add_persistent_peer (lnet_nid_t nid);
708 int  kibnal_close_stale_conns_locked (kib_peer_t *peer,
709                                       __u64 incarnation);
710 int  kibnal_conn_rts(kib_conn_t *conn,
711                      __u32 qpn, __u8 resp_res, __u8 init_depth, __u32 psn);
712 kib_conn_t *kibnal_create_conn (lnet_nid_t nid, int proto_version);
713 void kibnal_destroy_conn (kib_conn_t *conn);
714 void kibnal_listen_callback(IB_HANDLE cep, CM_CONN_INFO *info, void *arg);
715 int  kibnal_alloc_pages (kib_pages_t **pp, int npages);
716 void kibnal_free_pages (kib_pages_t *p);
717 void kibnal_queue_tx (kib_tx_t *tx, kib_conn_t *conn);
718 void kibnal_txlist_done (struct list_head *txlist, int status);
719 int  kibnal_post_receives (kib_conn_t *conn);
720 int  kibnal_init_rdma (kib_tx_t *tx, int type, int nob,
721                        kib_rdma_desc_t *dstrd, __u64 dstcookie);
722 void kibnal_check_sends (kib_conn_t *conn);
723 void kibnal_close_conn_locked (kib_conn_t *conn, int error);
724 int  kibnal_thread_start (int (*fn)(void *arg), void *arg);
725 int  kibnal_scheduler(void *arg);
726 int  kibnal_connd (void *arg);
727 void kibnal_init_tx_msg (kib_tx_t *tx, int type, int body_nob);
728 void kibnal_close_conn (kib_conn_t *conn, int why);
729 void kibnal_start_active_rdma (int type, int status,
730                                kib_rx_t *rx, lnet_msg_t *lntmsg,
731                                unsigned int niov,
732                                struct iovec *iov, lnet_kiov_t *kiov,
733                                unsigned int offset, unsigned int nob);
734 void kibnal_hca_async_callback (void *hca_arg, IB_EVENT_RECORD *ev);
735 void kibnal_hca_callback (void *hca_arg, void *cq_arg);
736 int  kibnal_tunables_init (void);
737 void kibnal_tunables_fini (void);