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