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