Whamcloud - gitweb
c19a903d40e77dce73716f0e41a82fe002f474c1
[fs/lustre-release.git] / lnet / klnds / viblnd / viblnd.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  *   Author: Frank Zago <fzago@systemfabricworks.com>
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  */
24
25 #ifndef EXPORT_SYMTAB
26 # define EXPORT_SYMTAB
27 #endif
28
29 #include <linux/config.h>
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 #include <linux/random.h>
52
53 #include <net/sock.h>
54 #include <linux/in.h>
55
56 #define DEBUG_SUBSYSTEM S_NAL
57
58 #include <libcfs/kp30.h>
59 #include <portals/p30.h>
60 #include <portals/lib-p30.h>
61 #include <portals/nal.h>
62
63 /* CPU_{L,B}E #defines needed by Voltaire headers */
64 #include <asm/byteorder.h>
65 #ifdef __BIG_ENDIAN__
66 #define CPU_BE 1
67 #define CPU_LE 0
68 #endif
69 #ifdef __LITTLE_ENDIAN__
70 #define CPU_BE 0
71 #define CPU_LE 1
72 #endif
73
74 #include <vverbs.h>
75 #include <ib-cm.h>
76 #include <ibat.h>
77
78 /* GCC 3.2.2, miscompiles this driver.  
79  * See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9853. */
80 #define GCC_VERSION ((__GNUC__*100 + __GNUC_MINOR__)*100 + __GNUC_PATCHLEVEL__)
81 #if GCC_VERSION < 30203
82 #error Invalid GCC version. Must use GCC >= 3.2.3
83 #endif
84
85 #if CONFIG_SMP
86 # define IBNAL_N_SCHED      num_online_cpus()   /* # schedulers */
87 #else
88 # define IBNAL_N_SCHED      1                   /* # schedulers */
89 #endif
90
91 /* sdp-connection.c */
92 #define IBNAL_QKEY               0
93 #define IBNAL_PKEY               0xffff
94 #define IBNAL_PKEY_IDX           0
95 #define IBNAL_SGID_IDX           0
96 #define IBNAL_SERVICE_LEVEL      0
97 #define IBNAL_STATIC_RATE        0
98 #define IBNAL_RETRY_CNT          7
99 #define IBNAL_RNR_CNT            7 
100 #define IBNAL_EE_FLOW_CNT        1
101 #define IBNAL_LOCAL_SUB          1
102 #define IBNAL_TRAFFIC_CLASS      0
103 #define IBNAL_SOURCE_PATH_BIT    0
104 #define IBNAL_OUS_DST_RD         32
105 #define IBNAL_IB_MTU             vv_mtu_1024
106
107 /* sdp-hca-params.h */
108 #define PATH_RATE_2_5GB           2
109 #define MLX_IPD_1x                1
110 #define MLX_IPD_4x                0
111 #define IBNAL_R_2_STATIC_RATE(r)  ((r) == PATH_RATE_2_5GB ? MLX_IPD_1x : MLX_IPD_4x)
112
113 /* other low-level IB constants */
114 #define IBNAL_LOCAL_ACK_TIMEOUT   0x12
115 #define IBNAL_PKT_LIFETIME        5
116 #define IBNAL_ARB_INITIATOR_DEPTH 0
117 #define IBNAL_ARB_RESP_RES        0
118 #define IBNAL_FAILOVER_ACCEPTED   0
119 #define IBNAL_SERVICE_NUMBER      0x11b9a2      /* Fixed service number */
120
121 #define IBNAL_MIN_RECONNECT_INTERVAL HZ         /* first failed connection retry... */
122 #define IBNAL_MAX_RECONNECT_INTERVAL (60*HZ)    /* ...exponentially increasing to this */
123
124 #define IBNAL_MSG_SIZE           (4<<10)        /* max size of queued messages (inc hdr) */
125
126 #define IBNAL_MSG_QUEUE_SIZE      8             /* # messages/RDMAs in-flight */
127 #define IBNAL_CREDIT_HIGHWATER    7             /* when to eagerly return credits */
128
129 #define IBNAL_NTX                 64            /* # tx descs */
130 #define IBNAL_NTX_NBLK            128           /* # reserved tx descs */
131 /* reduced from 256 to ensure we register < 255 pages per region.  
132  * this can change if we register all memory. */
133
134 #define IBNAL_PEER_HASH_SIZE      101           /* # peer lists */
135
136 #define IBNAL_RESCHED             100           /* # scheduler loops before reschedule */
137
138 #define IBNAL_CONCURRENT_PEERS    1000          /* # nodes all talking at once to me */
139
140 #define IBNAL_RDMA_BASE  0x0eeb0000
141 #define IBNAL_CKSUM      0
142 #define IBNAL_WHOLE_MEM  1
143 #if !IBNAL_WHOLE_MEM
144 # error "incompatible with voltaire adaptor-tavor (REGISTER_RAM_IN_ONE_PHY_MR)"
145 #endif
146
147 /* default vals for runtime tunables */
148 #define IBNAL_IO_TIMEOUT          50            /* default comms timeout (seconds) */
149
150 /************************/
151 /* derived constants... */
152
153 /* TX messages (shared by all connections) */
154 #define IBNAL_TX_MSGS       (IBNAL_NTX + IBNAL_NTX_NBLK)
155 #define IBNAL_TX_MSG_BYTES  (IBNAL_TX_MSGS * IBNAL_MSG_SIZE)
156 #define IBNAL_TX_MSG_PAGES  ((IBNAL_TX_MSG_BYTES + PAGE_SIZE - 1)/PAGE_SIZE)
157
158 #if IBNAL_WHOLE_MEM
159 # define IBNAL_MAX_RDMA_FRAGS PTL_MD_MAX_IOV
160 #else
161 # define IBNAL_MAX_RDMA_FRAGS 1
162 #endif
163
164 /* RX messages (per connection) */
165 #define IBNAL_RX_MSGS       IBNAL_MSG_QUEUE_SIZE
166 #define IBNAL_RX_MSG_BYTES  (IBNAL_RX_MSGS * IBNAL_MSG_SIZE)
167 #define IBNAL_RX_MSG_PAGES  ((IBNAL_RX_MSG_BYTES + PAGE_SIZE - 1)/PAGE_SIZE)
168
169 #define IBNAL_CQ_ENTRIES  (IBNAL_TX_MSGS * (1 + IBNAL_MAX_RDMA_FRAGS) + \
170                            IBNAL_RX_MSGS * IBNAL_CONCURRENT_PEERS)
171
172 typedef struct
173 {
174         int               kib_io_timeout;       /* comms timeout (seconds) */
175         struct ctl_table_header *kib_sysctl;    /* sysctl interface */
176 } kib_tunables_t;
177
178 typedef struct
179 {
180         int               ibp_npages;           /* # pages */
181         int               ibp_mapped;           /* mapped? */
182         __u64             ibp_vaddr;            /* mapped region vaddr */
183         __u32             ibp_lkey;             /* mapped region lkey */
184         __u32             ibp_rkey;             /* mapped region rkey */
185         vv_mem_reg_h_t    ibp_handle;           /* mapped region handle */
186         struct page      *ibp_pages[0];
187 } kib_pages_t;
188
189 typedef struct
190 {
191         vv_mem_reg_h_t    md_handle;
192         __u32             md_lkey;
193         __u32             md_rkey;
194         __u64             md_addr;
195 } kib_md_t;
196
197 typedef struct
198 {
199         int               kib_init;             /* initialisation state */
200         __u64             kib_incarnation;      /* which one am I */
201         int               kib_shutdown;         /* shut down? */
202         atomic_t          kib_nthreads;         /* # live threads */
203
204         __u64             kib_svc_id;           /* service number I listen on */
205         vv_gid_t          kib_port_gid;         /* device/port GID */
206         vv_p_key_t        kib_port_pkey;        /* device/port pkey */
207         
208         struct semaphore  kib_nid_mutex;        /* serialise NID ops */
209         cm_cep_handle_t   kib_listen_handle;    /* IB listen handle */
210
211         rwlock_t          kib_global_lock;      /* stabilize peer/conn ops */
212         spinlock_t        kib_vverbs_lock;      /* serialize vverbs calls */
213         int               kib_ready;            /* CQ callback fired */
214         int               kib_checking_cq;      /* a scheduler is checking the CQ */
215         
216         struct list_head *kib_peers;            /* hash table of all my known peers */
217         int               kib_peer_hash_size;   /* size of kib_peers */
218         atomic_t          kib_npeers;           /* # peers extant */
219         atomic_t          kib_nconns;           /* # connections extant */
220
221         void             *kib_connd;            /* the connd task (serialisation assertions) */
222         struct list_head  kib_connd_peers;      /* peers wanting to get connected */
223         struct list_head  kib_connd_pcreqs;     /* passive connection requests */
224         struct list_head  kib_connd_conns;      /* connections to setup/teardown */
225         struct list_head  kib_connd_zombies;    /* connections with zero refcount */
226         wait_queue_head_t kib_connd_waitq;      /* connection daemon sleeps here */
227         spinlock_t        kib_connd_lock;       /* serialise */
228
229         wait_queue_head_t kib_sched_waitq;      /* schedulers sleep here */
230         struct list_head  kib_sched_txq;        /* tx requiring attention */
231         struct list_head  kib_sched_rxq;        /* rx requiring attention */
232         spinlock_t        kib_sched_lock;       /* serialise */
233
234         struct kib_tx    *kib_tx_descs;         /* all the tx descriptors */
235         kib_pages_t      *kib_tx_pages;         /* premapped tx msg pages */
236
237         struct list_head  kib_idle_txs;         /* idle tx descriptors */
238         struct list_head  kib_idle_nblk_txs;    /* idle reserved tx descriptors */
239         wait_queue_head_t kib_idle_tx_waitq;    /* block here for tx descriptor */
240         __u64             kib_next_tx_cookie;   /* RDMA completion cookie */
241         spinlock_t        kib_tx_lock;          /* serialise */
242
243         vv_hca_h_t        kib_hca;              /* The HCA */
244         vv_hca_attrib_t   kib_hca_attrs;        /* its properties */
245         int               kib_port;             /* port on the device */
246         vv_port_attrib_t  kib_port_attr;        /* its properties */
247
248         vv_pd_h_t         kib_pd;               /* protection domain */
249         vv_cq_h_t         kib_cq;               /* completion queue */
250
251 } kib_data_t;
252
253 #define IBNAL_INIT_NOTHING         0
254 #define IBNAL_INIT_DATA            1
255 #define IBNAL_INIT_LIB             2
256 #define IBNAL_INIT_HCA             3
257 #define IBNAL_INIT_ASYNC           4
258 #define IBNAL_INIT_PD              5
259 #define IBNAL_INIT_TXD             6
260 #define IBNAL_INIT_CQ              7
261 #define IBNAL_INIT_ALL             8
262
263 /************************************************************************
264  * IB Wire message format.
265  * These are sent in sender's byte order (i.e. receiver flips).
266  */
267
268 typedef struct kib_connparams
269 {
270         __u32             ibcp_queue_depth;
271         __u32             ibcp_max_msg_size;
272         __u32             ibcp_max_frags;
273 } kib_connparams_t __attribute__((packed));
274
275 typedef struct
276 {
277         ptl_hdr_t         ibim_hdr;             /* portals header */
278         char              ibim_payload[0];      /* piggy-backed payload */
279 } kib_immediate_msg_t __attribute__((packed));
280
281 /* YEUCH! the __u64 address is split into 2 __u32 fields to ensure proper
282  * packing.  Otherwise we can't fit enough frags into an IBNAL message (<=
283  * smallest page size on any arch). */
284 typedef struct
285 {
286         __u32             rf_nob;               /* # of bytes */
287         __u32             rf_addr_lo;           /* lo 4 bytes of vaddr */
288         __u32             rf_addr_hi;           /* hi 4 bytes of vaddr */
289 } kib_rdma_frag_t __attribute__((packed));
290
291 typedef struct
292 {
293         __u32             rd_key;               /* local/remote key */
294         __u32             rd_nfrag;             /* # fragments */
295         kib_rdma_frag_t   rd_frags[0];          /* buffer frags */
296 } kib_rdma_desc_t __attribute__((packed));
297
298 /* CAVEAT EMPTOR!  We don't actually put ibprm_rd on the wire; it's just there
299  * to remember the source buffers while we wait for the PUT_ACK */
300
301 typedef struct
302 {
303         ptl_hdr_t         ibprm_hdr;            /* portals header */
304         __u64             ibprm_cookie;         /* opaque completion cookie */
305         kib_rdma_frag_t   ibprm_rd;             /* source buffer */
306 } kib_putreq_msg_t __attribute__((packed));
307
308 typedef struct
309 {
310         __u64             ibpam_src_cookie;     /* reflected completion cookie */
311         __u64             ibpam_dst_cookie;     /* opaque completion cookie */
312         kib_rdma_desc_t   ibpam_rd;             /* sender's sink buffer */
313 } kib_putack_msg_t __attribute__((packed));
314
315 typedef struct
316 {
317         ptl_hdr_t         ibgm_hdr;             /* portals header */
318         __u64             ibgm_cookie;          /* opaque completion cookie */
319         kib_rdma_desc_t   ibgm_rd;              /* rdma descriptor */
320 } kib_get_msg_t __attribute__((packed));
321
322 typedef struct
323 {
324         __u64             ibcm_cookie;          /* opaque completion cookie */
325         __s32             ibcm_status;          /* < 0 failure: >= 0 length */
326 } kib_completion_msg_t __attribute__((packed));
327
328 typedef struct
329 {
330         /* First 2 fields fixed FOR ALL TIME */
331         __u32             ibm_magic;            /* I'm an openibnal message */
332         __u16             ibm_version;          /* this is my version number */
333
334         __u8              ibm_type;             /* msg type */
335         __u8              ibm_credits;          /* returned credits */
336         __u32             ibm_nob;              /* # bytes in whole message */
337         __u32             ibm_cksum;            /* checksum (0 == no checksum) */
338         __u64             ibm_srcnid;           /* sender's NID */
339         __u64             ibm_srcstamp;         /* sender's incarnation */
340         __u64             ibm_dstnid;           /* destination's NID */
341         __u64             ibm_dststamp;         /* destination's incarnation */
342         __u64             ibm_seq;              /* sequence number */
343
344         union {
345                 kib_connparams_t      connparams;
346                 kib_immediate_msg_t   immediate;
347                 kib_putreq_msg_t      putreq;
348                 kib_putack_msg_t      putack;
349                 kib_get_msg_t         get;
350                 kib_completion_msg_t  completion;
351         } ibm_u __attribute__((packed));
352 } kib_msg_t __attribute__((packed));
353
354 #define IBNAL_MSG_MAGIC       0x0be91b91        /* unique magic */
355 #define IBNAL_MSG_VERSION              4        /* current protocol version */
356
357 #define IBNAL_MSG_CONNREQ           0xc0        /* connection request */
358 #define IBNAL_MSG_CONNACK           0xc1        /* connection acknowledge */
359 #define IBNAL_MSG_NOOP              0xd0        /* nothing (just credits) */
360 #define IBNAL_MSG_IMMEDIATE         0xd1        /* immediate */
361 #define IBNAL_MSG_PUT_REQ           0xd2        /* putreq (src->sink) */
362 #define IBNAL_MSG_PUT_NAK           0xd3        /* completion (sink->src) */
363 #define IBNAL_MSG_PUT_ACK           0xd4        /* putack (sink->src) */
364 #define IBNAL_MSG_PUT_DONE          0xd5        /* completion (src->sink) */
365 #define IBNAL_MSG_GET_REQ           0xd6        /* getreq (sink->src) */
366 #define IBNAL_MSG_GET_DONE          0xd7        /* completion (src->sink: all OK) */
367
368 /***********************************************************************/
369
370 typedef struct kib_rx                           /* receive message */
371 {
372         struct list_head          rx_list;      /* queue for attention */
373         struct kib_conn          *rx_conn;      /* owning conn */
374         int                       rx_responded; /* responded to peer? */
375         int                       rx_posted;    /* posted? */
376 #if IBNAL_WHOLE_MEM
377         vv_l_key_t                rx_lkey;      /* local key */
378 #else        
379         __u64                     rx_vaddr;     /* pre-mapped buffer (hca vaddr) */
380 #endif
381         kib_msg_t                *rx_msg;       /* pre-mapped buffer (host vaddr) */
382         vv_wr_t                   rx_wrq;       /* receive work item */
383         vv_scatgat_t              rx_gl;        /* and its memory */
384 } kib_rx_t;
385
386 #if IBNAL_WHOLE_MEM
387 # define KIBNAL_RX_VADDR(rx) ((__u64)((unsigned long)((rx)->rx_msg)))
388 # define KIBNAL_RX_LKEY(rx)  ((rx)->rx_lkey)
389 #else
390 # define KIBNAL_RX_VADDR(rx) ((rx)->rx_vaddr)
391 # define KIBNAL_RX_LKEY(rx)  ((rx)->rx_conn->ibc_rx_pages->ibp_lkey)
392 #endif
393
394 typedef struct kib_tx                           /* transmit message */
395 {
396         struct list_head          tx_list;      /* queue on idle_txs ibc_tx_queue etc. */
397         int                       tx_isnblk;    /* I'm reserved for non-blocking sends */
398         struct kib_conn          *tx_conn;      /* owning conn */
399         int                       tx_mapped;    /* mapped for RDMA? */
400         int                       tx_sending;   /* # tx callbacks outstanding */
401         int                       tx_queued;    /* queued for sending */
402         int                       tx_waiting;   /* waiting for peer */
403         int                       tx_status;    /* completion status */
404         unsigned long             tx_deadline;  /* completion deadline */
405         __u64                     tx_cookie;    /* completion cookie */
406         lib_msg_t                *tx_libmsg[2]; /* lib msgs to finalize on completion */
407 #if IBNAL_WHOLE_MEM
408         vv_l_key_t                tx_lkey;      /* local key for message buffer */
409 #else
410         kib_md_t                  tx_md;        /* RDMA mapping (active/passive) */
411         __u64                     tx_vaddr;     /* pre-mapped buffer (hca vaddr) */
412 #endif
413         kib_msg_t                *tx_msg;       /* message buffer (host vaddr) */
414         int                       tx_nwrq;      /* # send work items */
415         vv_wr_t                  *tx_wrq;       /* send work items... */
416         vv_scatgat_t             *tx_gl;        /* ...and their memory */
417         kib_rdma_desc_t          *tx_rd;        /* rdma descriptor (src buffers) */
418 } kib_tx_t;
419
420 #if IBNAL_WHOLE_MEM
421 # define KIBNAL_TX_VADDR(tx) ((__u64)((unsigned long)((tx)->tx_msg)))
422 # define KIBNAL_TX_LKEY(tx)  ((tx)->tx_lkey)
423 #else
424 # define KIBNAL_TX_VADDR(tx) ((tx)->tx_vaddr)
425 # define KIBNAL_TX_LKEY(tx)  (kibnal_data.kib_tx_pages->ibp_lkey)
426 #endif
427
428 #define KIB_TX_UNMAPPED       0
429 #define KIB_TX_MAPPED         1
430
431 /* Passive connection request (listener callback) queued for handling by connd */
432 typedef struct kib_pcreq
433 {
434         struct list_head  pcr_list;             /* queue for handling by connd */
435         cm_cep_handle_t   pcr_cep;              /* listening handle */
436         cm_request_data_t pcr_cmreq;            /* request data */
437 } kib_pcreq_t;
438
439 typedef struct kib_connvars
440 {
441         /* connection-in-progress variables */
442         __u32               cv_port;
443         __u32               cv_pkey_index;
444         __u32               cv_rnr_count;
445         __u32               cv_sgid_index;
446         __u32               cv_remote_qpn;
447         __u32               cv_local_qpn;
448         __u32               cv_rxpsn;
449         __u32               cv_txpsn;
450         ib_path_record_v2_t cv_path;
451         ibat_arp_data_t     cv_arp;
452         ibat_stat_t         cv_arprc;
453         cm_conn_data_t      cv_conndata;
454 } kib_connvars_t;
455
456 typedef struct kib_conn
457 {
458         struct kib_peer    *ibc_peer;           /* owning peer */
459         struct list_head    ibc_list;           /* stash on peer's conn list */
460         __u64               ibc_incarnation;    /* which instance of the peer */
461         __u64               ibc_txseq;          /* tx sequence number */
462         __u64               ibc_rxseq;          /* rx sequence number */
463         atomic_t            ibc_refcount;       /* # users */
464         int                 ibc_state;          /* what's happening */
465         atomic_t            ibc_nob;            /* # bytes buffered */
466         int                 ibc_nsends_posted;  /* # uncompleted sends */
467         int                 ibc_credits;        /* # credits I have */
468         int                 ibc_outstanding_credits; /* # credits to return */
469         int                 ibc_disconnect;     /* some disconnect callback fired */
470         int                 ibc_comms_error;    /* set on comms error */
471         struct list_head    ibc_early_rxs;      /* rxs completed before ESTABLISHED */
472         struct list_head    ibc_tx_queue;       /* send queue */
473         struct list_head    ibc_active_txs;     /* active tx awaiting completion */
474         spinlock_t          ibc_lock;           /* serialise */
475         kib_rx_t           *ibc_rxs;            /* the rx descs */
476         kib_pages_t        *ibc_rx_pages;       /* premapped rx msg pages */
477         vv_qp_h_t           ibc_qp;             /* queue pair */
478         cm_cep_handle_t     ibc_cep;            /* connection endpoint */
479         kib_connvars_t     *ibc_connvars;       /* in-progress connection state */
480 } kib_conn_t;
481
482 #define IBNAL_CONN_INIT_NOTHING       0         /* incomplete init */
483 #define IBNAL_CONN_INIT               1         /* completed init */
484 #define IBNAL_CONN_ACTIVE_ARP         2         /* active arping */
485 #define IBNAL_CONN_ACTIVE_CONNECT     3         /* active sending req */
486 #define IBNAL_CONN_ACTIVE_CHECK_REPLY 4         /* active checking reply */
487 #define IBNAL_CONN_ACTIVE_RTU         5         /* active sending rtu */
488 #define IBNAL_CONN_PASSIVE_WAIT       6         /* passive waiting for rtu */
489 #define IBNAL_CONN_ESTABLISHED        7         /* connection established */
490 #define IBNAL_CONN_DISCONNECT1        8         /* disconnect phase 1 */
491 #define IBNAL_CONN_DISCONNECT2        9         /* disconnect phase 2 */
492 #define IBNAL_CONN_DISCONNECTED       10        /* disconnect complete */
493
494 typedef struct kib_peer
495 {
496         struct list_head    ibp_list;           /* stash on global peer list */
497         struct list_head    ibp_connd_list;     /* schedule on kib_connd_peers */
498         ptl_nid_t           ibp_nid;            /* who's on the other end(s) */
499         __u32               ibp_ip;             /* IP to query for peer conn params */
500         int                 ibp_port;           /* port to qery for peer conn params */
501         __u64               ibp_incarnation;    /* peer's incarnation */
502         atomic_t            ibp_refcount;       /* # users */
503         int                 ibp_persistence;    /* "known" peer refs */
504         struct list_head    ibp_conns;          /* all active connections */
505         struct list_head    ibp_tx_queue;       /* msgs waiting for a conn */
506         int                 ibp_connecting;     /* connecting+accepting */
507         unsigned long       ibp_reconnect_time; /* when reconnect may be attempted */
508         unsigned long       ibp_reconnect_interval; /* exponential backoff */
509 } kib_peer_t;
510
511
512 extern lib_nal_t       kibnal_lib;
513 extern kib_data_t      kibnal_data;
514 extern kib_tunables_t  kibnal_tunables;
515
516 extern void kibnal_init_msg(kib_msg_t *msg, int type, int body_nob);
517 extern void kibnal_pack_msg(kib_msg_t *msg, int credits, ptl_nid_t dstnid,
518                             __u64 dststamp, __u64 seq);
519 extern int kibnal_unpack_msg(kib_msg_t *msg, int nob);
520 extern kib_peer_t *kibnal_create_peer(ptl_nid_t nid);
521 extern void kibnal_destroy_peer(kib_peer_t *peer);
522 extern int kibnal_del_peer(ptl_nid_t nid, int single_share);
523 extern kib_peer_t *kibnal_find_peer_locked(ptl_nid_t nid);
524 extern void kibnal_unlink_peer_locked(kib_peer_t *peer);
525 extern int  kibnal_close_stale_conns_locked(kib_peer_t *peer,
526                                             __u64 incarnation);
527 extern kib_conn_t *kibnal_create_conn(cm_cep_handle_t cep);
528 extern void kibnal_listen_callback(cm_cep_handle_t cep, cm_conn_data_t *info, void *arg);
529
530 extern int kibnal_alloc_pages(kib_pages_t **pp, int npages, int access);
531 extern void kibnal_free_pages(kib_pages_t *p);
532
533 extern void kibnal_check_sends(kib_conn_t *conn);
534 extern void kibnal_close_conn_locked(kib_conn_t *conn, int error);
535 extern void kibnal_destroy_conn(kib_conn_t *conn);
536 extern int  kibnal_thread_start(int (*fn)(void *arg), void *arg);
537 extern int  kibnal_scheduler(void *arg);
538 extern int  kibnal_connd(void *arg);
539 extern void kibnal_init_tx_msg(kib_tx_t *tx, int type, int body_nob);
540 extern void kibnal_close_conn(kib_conn_t *conn, int why);
541 extern int  kibnal_set_qp_state(kib_conn_t *conn, vv_qp_state_t new_state);
542 extern void kibnal_async_callback(vv_event_record_t ev);
543 extern void kibnal_cq_callback(unsigned long context);
544 extern void kibnal_passive_connreq(kib_pcreq_t *pcr, int reject);
545 extern void kibnal_pause(int ticks);
546 extern void kibnal_queue_tx(kib_tx_t *tx, kib_conn_t *conn);
547 extern int  kibnal_init_rdma(kib_tx_t *tx, int type, int nob,
548                              kib_rdma_desc_t *dstrd, __u64 dstcookie);
549
550 static inline int
551 wrq_signals_completion (vv_wr_t *wrq)
552 {
553         return wrq->completion_notification != 0;
554 }
555
556 #define kibnal_conn_addref(conn)                                \
557 do {                                                            \
558         CDEBUG(D_NET, "conn[%p] (%d)++\n",                      \
559                (conn), atomic_read(&(conn)->ibc_refcount));     \
560         LASSERT(atomic_read(&(conn)->ibc_refcount) > 0);        \
561         atomic_inc(&(conn)->ibc_refcount);                      \
562 } while (0)
563
564 #define kibnal_conn_decref(conn)                                              \
565 do {                                                                          \
566         unsigned long   flags;                                                \
567                                                                               \
568         CDEBUG(D_NET, "conn[%p] (%d)--\n",                                    \
569                (conn), atomic_read(&(conn)->ibc_refcount));                   \
570         LASSERT(atomic_read(&(conn)->ibc_refcount) > 0);                      \
571         if (atomic_dec_and_test(&(conn)->ibc_refcount)) {                     \
572                 spin_lock_irqsave(&kibnal_data.kib_connd_lock, flags);        \
573                 list_add_tail(&(conn)->ibc_list,                              \
574                               &kibnal_data.kib_connd_zombies);                \
575                 wake_up(&kibnal_data.kib_connd_waitq);                        \
576                 spin_unlock_irqrestore(&kibnal_data.kib_connd_lock, flags);   \
577         }                                                                     \
578 } while (0)
579
580 #define kibnal_peer_addref(peer)                                \
581 do {                                                            \
582         CDEBUG(D_NET, "peer[%p] -> "LPX64" (%d)++\n",           \
583                (peer), (peer)->ibp_nid,                         \
584                atomic_read (&(peer)->ibp_refcount));            \
585         LASSERT(atomic_read(&(peer)->ibp_refcount) > 0);        \
586         atomic_inc(&(peer)->ibp_refcount);                      \
587 } while (0)
588
589 #define kibnal_peer_decref(peer)                                \
590 do {                                                            \
591         CDEBUG(D_NET, "peer[%p] -> "LPX64" (%d)--\n",           \
592                (peer), (peer)->ibp_nid,                         \
593                atomic_read (&(peer)->ibp_refcount));            \
594         LASSERT(atomic_read(&(peer)->ibp_refcount) > 0);        \
595         if (atomic_dec_and_test(&(peer)->ibp_refcount))         \
596                 kibnal_destroy_peer(peer);                      \
597 } while (0)
598
599 static inline struct list_head *
600 kibnal_nid2peerlist (ptl_nid_t nid)
601 {
602         unsigned int hash = ((unsigned int)nid) % kibnal_data.kib_peer_hash_size;
603
604         return (&kibnal_data.kib_peers [hash]);
605 }
606
607 static inline int
608 kibnal_peer_active (kib_peer_t *peer)
609 {
610         /* Am I in the peer hash table? */
611         return (!list_empty(&peer->ibp_list));
612 }
613
614 static inline void
615 kibnal_queue_tx_locked (kib_tx_t *tx, kib_conn_t *conn)
616 {
617         /* CAVEAT EMPTOR: tx takes caller's ref on conn */
618
619         LASSERT (tx->tx_nwrq > 0);              /* work items set up */
620         LASSERT (!tx->tx_queued);               /* not queued for sending already */
621
622         if (tx->tx_conn == NULL) {
623                 kibnal_conn_addref(conn);
624                 tx->tx_conn = conn;
625         } else {
626                 LASSERT (tx->tx_conn == conn);
627                 LASSERT (tx->tx_msg->ibm_type == IBNAL_MSG_PUT_DONE);
628         }
629         tx->tx_queued = 1;
630         tx->tx_deadline = jiffies + kibnal_tunables.kib_io_timeout * HZ;
631         list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
632 }
633
634 /* CAVEAT EMPTOR: We rely on tx/rx descriptor alignment to allow us to use the
635  * lowest 2 bits of the work request id to stash the work item type (the op
636  * field is not valid when the wc completes in error). */
637
638 #define IBNAL_WID_TX    0
639 #define IBNAL_WID_RX    1
640 #define IBNAL_WID_RDMA  2
641 #define IBNAL_WID_MASK  3UL
642
643 static inline vv_wr_id_t
644 kibnal_ptr2wreqid (void *ptr, int type)
645 {
646         unsigned long lptr = (unsigned long)ptr;
647
648         LASSERT ((lptr & IBNAL_WID_MASK) == 0);
649         LASSERT ((type & ~IBNAL_WID_MASK) == 0);
650         return (vv_wr_id_t)(lptr | type);
651 }
652
653 static inline void *
654 kibnal_wreqid2ptr (vv_wr_id_t wreqid)
655 {
656         return (void *)(((unsigned long)wreqid) & ~IBNAL_WID_MASK);
657 }
658
659 static inline int
660 kibnal_wreqid2type (vv_wr_id_t wreqid)
661 {
662         return (wreqid & IBNAL_WID_MASK);
663 }
664
665 static inline void
666 kibnal_set_conn_state (kib_conn_t *conn, int state)
667 {
668         conn->ibc_state = state;
669         mb();
670 }
671
672 static inline __u64
673 kibnal_rf_addr (kib_rdma_frag_t *rf)
674 {
675         return  (((__u64)rf->rf_addr_hi)<<32) | ((__u64)rf->rf_addr_lo);
676 }
677
678 static inline void
679 kibnal_rf_set (kib_rdma_frag_t *rf, __u64 addr, int nob)
680 {
681         rf->rf_addr_lo = addr & 0xffffffff;
682         rf->rf_addr_hi = (addr >> 32) & 0xffffffff;
683         rf->rf_nob = nob;
684 }
685
686 static inline int
687 kibnal_rd_size (kib_rdma_desc_t *rd)
688 {
689         int   i;
690         int   size;
691         
692         for (i = size = 0; i < rd->rd_nfrag; i++)
693                 size += rd->rd_frags[i].rf_nob;
694         
695         return size;
696 }