Whamcloud - gitweb
Revert "b20288 fix a deadlock in kiblnd_check_conns i=isaac i=maxim"
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lnet/klnds/viblnd/viblnd.h
37  *
38  * Author: Eric Barton <eric@bartonsoftware.com>
39  * Author: Frank Zago <fzago@systemfabricworks.com>
40  */
41
42 #ifndef EXPORT_SYMTAB
43 # define EXPORT_SYMTAB
44 #endif
45 #ifndef AUTOCONF_INCLUDED
46 #include <linux/config.h>
47 #endif
48 #include <linux/module.h>
49 #include <linux/kernel.h>
50 #include <linux/mm.h>
51 #include <linux/string.h>
52 #include <linux/stat.h>
53 #include <linux/errno.h>
54 #include <linux/smp_lock.h>
55 #include <linux/unistd.h>
56 #include <linux/uio.h>
57
58 #include <asm/system.h>
59 #include <asm/uaccess.h>
60 #include <asm/io.h>
61
62 #include <linux/init.h>
63 #include <linux/fs.h>
64 #include <linux/file.h>
65 #include <linux/stat.h>
66 #include <linux/list.h>
67 #include <linux/kmod.h>
68 #include <linux/sysctl.h>
69 #include <linux/random.h>
70
71 #include <net/sock.h>
72 #include <linux/in.h>
73
74 #define DEBUG_SUBSYSTEM S_LND
75
76 #include <libcfs/libcfs.h>
77 #include <lnet/lnet.h>
78 #include <lnet/lib-lnet.h>
79 #include <lnet/lnet-sysctl.h>
80
81 /* CPU_{L,B}E #defines needed by Voltaire headers */
82 #include <asm/byteorder.h>
83 #ifdef __BIG_ENDIAN__
84 #define CPU_BE 1
85 #define CPU_LE 0
86 #endif
87 #ifdef __LITTLE_ENDIAN__
88 #define CPU_BE 0
89 #define CPU_LE 1
90 #endif
91
92 #include <vverbs.h>
93 #include <ib-cm.h>
94 #include <ibat.h>
95
96 /* GCC 3.2.2, miscompiles this driver.  
97  * See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9853. */
98 #define GCC_VERSION ((__GNUC__*100 + __GNUC_MINOR__)*100 + __GNUC_PATCHLEVEL__)
99 #if (GCC_VERSION >= 30000) && (GCC_VERSION < 30203)
100 # error Invalid GCC version. Must use GCC < 3.0.0 || GCC >= 3.2.3
101 #endif
102
103 #ifdef CONFIG_SMP
104 # define IBNAL_N_SCHED      num_online_cpus()   /* # schedulers */
105 #else
106 # define IBNAL_N_SCHED      1                   /* # schedulers */
107 #endif
108
109 #define IBNAL_USE_FMR  1
110
111 /* tunables fixed at compile time */
112 #define IBNAL_PEER_HASH_SIZE         101        /* # peer lists */
113 #define IBNAL_RESCHED                100        /* # scheduler loops before reschedule */
114 #define IBNAL_MSG_QUEUE_SIZE         8          /* # messages/RDMAs in-flight */
115 #define IBNAL_CREDIT_HIGHWATER       7          /* when eagerly to return credits */
116 #define IBNAL_MSG_SIZE              (4<<10)     /* max size of queued messages (inc hdr) */
117
118 /* constants derived from sdp-connection.c */
119 #define IBNAL_QKEY               0
120 #define IBNAL_PKEY               0xffff
121 #define IBNAL_PKEY_IDX           0
122 #define IBNAL_SGID_IDX           0
123 #define IBNAL_SERVICE_LEVEL      0
124 #define IBNAL_STATIC_RATE        0
125 #define IBNAL_EE_FLOW_CNT        1
126 #define IBNAL_LOCAL_SUB          1
127 #define IBNAL_TRAFFIC_CLASS      0
128 #define IBNAL_SOURCE_PATH_BIT    0
129 #define IBNAL_OUS_DST_RD         1
130 #define IBNAL_IB_MTU             vv_mtu_1024
131
132 /* constants derived from sdp-hca-params.h */
133 #define PATH_RATE_2_5GB           2
134 #define MLX_IPD_1x                1
135 #define MLX_IPD_4x                0
136 #define IBNAL_R_2_STATIC_RATE(r)  ((r) == PATH_RATE_2_5GB ? MLX_IPD_1x : MLX_IPD_4x)
137
138 /* other low-level IB constants */
139 #define IBNAL_PKT_LIFETIME        5
140 #define IBNAL_ARB_INITIATOR_DEPTH 0
141 #define IBNAL_ARB_RESP_RES        0
142 #define IBNAL_FAILOVER_ACCEPTED   0
143
144 /************************/
145 /* derived constants... */
146
147 /* TX messages (shared by all connections) */
148 #define IBNAL_TX_MSGS()       (*kibnal_tunables.kib_ntx)
149 #define IBNAL_TX_MSG_BYTES()  (IBNAL_TX_MSGS() * IBNAL_MSG_SIZE)
150 #define IBNAL_TX_MSG_PAGES()  ((IBNAL_TX_MSG_BYTES() + PAGE_SIZE - 1)/PAGE_SIZE)
151
152 #if IBNAL_USE_FMR
153 # define IBNAL_MAX_RDMA_FRAGS 1
154 # define IBNAL_CONCURRENT_SENDS IBNAL_RX_MSGS
155 #else
156 # define IBNAL_MAX_RDMA_FRAGS LNET_MAX_IOV
157 # define IBNAL_CONCURRENT_SENDS IBNAL_MSG_QUEUE_SIZE
158 #endif
159
160 /* RX messages (per connection) */
161 #define IBNAL_RX_MSGS         (IBNAL_MSG_QUEUE_SIZE*2)
162 #define IBNAL_RX_MSG_BYTES    (IBNAL_RX_MSGS * IBNAL_MSG_SIZE)
163 #define IBNAL_RX_MSG_PAGES    ((IBNAL_RX_MSG_BYTES + PAGE_SIZE - 1)/PAGE_SIZE)
164
165 #define IBNAL_CQ_ENTRIES()    (IBNAL_TX_MSGS() * (1 + IBNAL_MAX_RDMA_FRAGS) +           \
166                                IBNAL_RX_MSGS * *kibnal_tunables.kib_concurrent_peers)
167
168 typedef struct
169 {
170         unsigned int     *kib_service_number;   /* IB service number */
171         int              *kib_min_reconnect_interval; /* first failed connection retry... */
172         int              *kib_max_reconnect_interval; /* ...exponentially increasing to this */
173         int              *kib_concurrent_peers; /* max # nodes all talking to me */
174         int              *kib_cksum;            /* checksum kib_msg_t? */
175         int              *kib_timeout;          /* comms timeout (seconds) */
176         int              *kib_ntx;              /* # tx descs */
177         int              *kib_credits;          /* # concurrent sends */
178         int              *kib_peercredits;      /* # concurrent sends to 1 peer */
179         int              *kib_arp_retries;      /* # times to retry ARP */
180         char            **kib_hca_basename;     /* HCA base name */
181         char            **kib_ipif_basename;    /* IPoIB interface base name */
182         int              *kib_local_ack_timeout; /* IB RC QP ack timeout... */
183         int              *kib_retry_cnt;        /* ...and retry */
184         int              *kib_rnr_cnt;          /* RNR retries... */
185         int              *kib_rnr_nak_timer;    /* ...and interval */
186         int              *kib_keepalive;        /* keepalive interval */
187         int              *kib_concurrent_sends; /* send work queue sizing */
188 #if IBNAL_USE_FMR
189         int              *kib_fmr_remaps;       /* # FMR maps before unmap required */
190 #endif
191 #if defined(CONFIG_SYSCTL) && !CFS_SYSFS_MODULE_PARM
192         cfs_sysctl_table_header_t *kib_sysctl;  /* sysctl interface */
193 #endif
194 } kib_tunables_t;
195
196 typedef struct
197 {
198         int               ibp_npages;           /* # pages */
199         struct page      *ibp_pages[0];
200 } kib_pages_t;
201
202 #if IBNAL_USE_FMR
203 typedef struct
204 {
205         vv_fmr_h_t        md_fmrhandle;         /* FMR handle */
206         int               md_fmrcount;          /* # mappings left */
207         int               md_active;            /* mapping in use? */
208         __u32             md_lkey;              /* local key */
209         __u32             md_rkey;              /* remote key */
210         __u64             md_addr;              /* IO VM address */
211 } kib_md_t;
212 #endif
213
214 typedef struct
215 {
216         int               kib_init;             /* initialisation state */
217         __u64             kib_incarnation;      /* which one am I */
218         int               kib_shutdown;         /* shut down? */
219         atomic_t          kib_nthreads;         /* # live threads */
220         lnet_ni_t        *kib_ni;               /* _the_ nal instance */
221
222         vv_gid_t          kib_port_gid;         /* device/port GID */
223         vv_p_key_t        kib_port_pkey;        /* device/port pkey */
224         
225         cm_cep_handle_t   kib_listen_handle;    /* IB listen handle */
226
227         rwlock_t          kib_global_lock;      /* stabilize peer/conn ops */
228         int               kib_ready;            /* CQ callback fired */
229         int               kib_checking_cq;      /* a scheduler is checking the CQ */
230         
231         struct list_head *kib_peers;            /* hash table of all my known peers */
232         int               kib_peer_hash_size;   /* size of kib_peers */
233         atomic_t          kib_npeers;           /* # peers extant */
234         atomic_t          kib_nconns;           /* # connections extant */
235
236         void             *kib_connd;            /* the connd task (serialisation assertions) */
237         struct list_head  kib_connd_peers;      /* peers wanting to get connected */
238         struct list_head  kib_connd_pcreqs;     /* passive connection requests */
239         struct list_head  kib_connd_conns;      /* connections to setup/teardown */
240         struct list_head  kib_connd_zombies;    /* connections with zero refcount */
241         wait_queue_head_t kib_connd_waitq;      /* connection daemon sleeps here */
242         spinlock_t        kib_connd_lock;       /* serialise */
243
244         wait_queue_head_t kib_sched_waitq;      /* schedulers sleep here */
245         spinlock_t        kib_sched_lock;       /* serialise */
246
247         struct kib_tx    *kib_tx_descs;         /* all the tx descriptors */
248         kib_pages_t      *kib_tx_pages;         /* premapped tx msg pages */
249
250         struct list_head  kib_idle_txs;         /* idle tx descriptors */
251         __u64             kib_next_tx_cookie;   /* RDMA completion cookie */
252         spinlock_t        kib_tx_lock;          /* serialise */
253
254         vv_hca_h_t        kib_hca;              /* The HCA */
255         vv_hca_attrib_t   kib_hca_attrs;        /* its properties */
256         int               kib_port;             /* port on the device */
257         vv_port_attrib_t  kib_port_attr;        /* its properties */
258
259         vv_pd_h_t         kib_pd;               /* protection domain */
260         vv_cq_h_t         kib_cq;               /* completion queue */
261
262 } kib_data_t;
263
264 #define IBNAL_INIT_NOTHING         0
265 #define IBNAL_INIT_DATA            1
266 #define IBNAL_INIT_LIB             2
267 #define IBNAL_INIT_HCA             3
268 #define IBNAL_INIT_ASYNC           4
269 #define IBNAL_INIT_PD              5
270 #define IBNAL_INIT_TXD             6
271 #define IBNAL_INIT_CQ              7
272 #define IBNAL_INIT_ALL             8
273
274 #include "viblnd_wire.h"
275
276 /***********************************************************************/
277
278 typedef struct kib_rx                           /* receive message */
279 {
280         struct list_head          rx_list;      /* queue for attention */
281         struct kib_conn          *rx_conn;      /* owning conn */
282         int                       rx_nob;       /* # bytes received (-1 while posted) */
283         vv_l_key_t                rx_lkey;      /* local key */
284         kib_msg_t                *rx_msg;       /* pre-mapped buffer (host vaddr) */
285         vv_wr_t                   rx_wrq;       /* receive work item */
286         vv_scatgat_t              rx_gl;        /* and its memory */
287 } kib_rx_t;
288
289 typedef struct kib_tx                           /* transmit message */
290 {
291         struct list_head          tx_list;      /* queue on idle_txs ibc_tx_queue etc. */
292         struct kib_conn          *tx_conn;      /* owning conn */
293         int                       tx_sending;   /* # tx callbacks outstanding */
294         int                       tx_queued;    /* queued for sending */
295         int                       tx_waiting;   /* waiting for peer */
296         int                       tx_status;    /* completion status */
297         unsigned long             tx_deadline;  /* completion deadline */
298         __u64                     tx_cookie;    /* completion cookie */
299         lnet_msg_t               *tx_lntmsg[2]; /* lnet msgs to finalize on completion */
300         vv_l_key_t                tx_lkey;      /* local key for message buffer */
301         kib_msg_t                *tx_msg;       /* message buffer (host vaddr) */
302         int                       tx_nwrq;      /* # send work items */
303 #if IBNAL_USE_FMR
304         vv_wr_t                   tx_wrq[2];    /* send work items... */
305         vv_scatgat_t              tx_gl[2];     /* ...and their memory */
306         kib_rdma_desc_t           tx_rd[1];     /* rdma descriptor */
307         kib_md_t                  tx_md;        /* FMR mapping descriptor */
308         __u64                    *tx_pages;     /* page phys addrs */
309 #else
310         vv_wr_t                  *tx_wrq;       /* send work items... */
311         vv_scatgat_t             *tx_gl;        /* ...and their memory */
312         kib_rdma_desc_t          *tx_rd;        /* rdma descriptor (src buffers) */
313 #endif
314 } kib_tx_t;
315
316 /* Passive connection request (listener callback) queued for handling by connd */
317 typedef struct kib_pcreq
318 {
319         struct list_head  pcr_list;             /* queue for handling by connd */
320         cm_cep_handle_t   pcr_cep;              /* listening handle */
321         cm_request_data_t pcr_cmreq;            /* request data */
322 } kib_pcreq_t;
323
324 typedef struct kib_connvars
325 {
326         /* connection-in-progress variables */
327         __u32               cv_port;
328         __u32               cv_pkey_index;
329         __u32               cv_rnr_count;
330         __u32               cv_sgid_index;
331         __u32               cv_remote_qpn;
332         __u32               cv_local_qpn;
333         __u32               cv_rxpsn;
334         __u32               cv_txpsn;
335         ib_path_record_v2_t cv_path;
336         ibat_arp_data_t     cv_arp;
337         ibat_stat_t         cv_arprc;
338         cm_conn_data_t      cv_conndata;
339 } kib_connvars_t;
340
341 typedef struct kib_conn
342 {
343         struct kib_peer    *ibc_peer;           /* owning peer */
344         struct list_head    ibc_list;           /* stash on peer's conn list */
345         __u64               ibc_incarnation;    /* which instance of the peer */
346         __u64               ibc_txseq;          /* tx sequence number */
347         __u64               ibc_rxseq;          /* rx sequence number */
348         __u32               ibc_version;        /* peer protocol version */
349         atomic_t            ibc_refcount;       /* # users */
350         int                 ibc_state;          /* what's happening */
351         int                 ibc_nsends_posted;  /* # uncompleted sends */
352         int                 ibc_credits;        /* # credits I have */
353         int                 ibc_outstanding_credits; /* # credits to return */
354         int                 ibc_reserved_credits; /* # credits for ACK/DONE msgs */
355         int                 ibc_disconnect;     /* some disconnect callback fired */
356         int                 ibc_comms_error;    /* set on comms error */
357         unsigned long       ibc_last_send;      /* time of last send */
358         struct list_head    ibc_early_rxs;      /* rxs completed before ESTABLISHED */
359         struct list_head    ibc_tx_queue_nocred; /* sends that don't need a cred */
360         struct list_head    ibc_tx_queue_rsrvd; /* sends that need a reserved cred */
361         struct list_head    ibc_tx_queue;       /* send queue */
362         struct list_head    ibc_active_txs;     /* active tx awaiting completion */
363         spinlock_t          ibc_lock;           /* serialise */
364         kib_rx_t           *ibc_rxs;            /* the rx descs */
365         kib_pages_t        *ibc_rx_pages;       /* premapped rx msg pages */
366         vv_qp_h_t           ibc_qp;             /* queue pair */
367         cm_cep_handle_t     ibc_cep;            /* connection endpoint */
368         kib_connvars_t     *ibc_connvars;       /* in-progress connection state */
369 } kib_conn_t;
370
371 #define IBNAL_CONN_INIT_NOTHING       0         /* incomplete init */
372 #define IBNAL_CONN_INIT_QP            1         /* QP allocated */
373 #define IBNAL_CONN_INIT               2         /* completed init */
374 #define IBNAL_CONN_ACTIVE_ARP         3         /* active arping */
375 #define IBNAL_CONN_ACTIVE_CONNECT     4         /* active sending req */
376 #define IBNAL_CONN_ACTIVE_CHECK_REPLY 5         /* active checking reply */
377 #define IBNAL_CONN_ACTIVE_RTU         6         /* active sending rtu */
378 #define IBNAL_CONN_PASSIVE_WAIT       7         /* passive waiting for rtu */
379 #define IBNAL_CONN_ESTABLISHED        8         /* connection established */
380 #define IBNAL_CONN_DISCONNECT1        9         /* disconnect phase 1 */
381 #define IBNAL_CONN_DISCONNECT2        10        /* disconnect phase 2 */
382 #define IBNAL_CONN_DISCONNECTED       11        /* disconnect complete */
383
384 typedef struct kib_peer
385 {
386         struct list_head    ibp_list;           /* stash on global peer list */
387         struct list_head    ibp_connd_list;     /* schedule on kib_connd_peers */
388         lnet_nid_t          ibp_nid;            /* who's on the other end(s) */
389         __u32               ibp_ip;             /* IP to query for peer conn params */
390         int                 ibp_port;           /* port to qery for peer conn params */
391         __u64               ibp_incarnation;    /* peer's incarnation */
392         atomic_t            ibp_refcount;       /* # users */
393         int                 ibp_persistence;    /* "known" peer refs */
394         struct list_head    ibp_conns;          /* all active connections */
395         struct list_head    ibp_tx_queue;       /* msgs waiting for a conn */
396         int                 ibp_connecting;     /* current active connection attempts */
397         int                 ibp_accepting;      /* current passive connection attempts */
398         int                 ibp_arp_count;      /* # arp attempts */
399         unsigned long       ibp_reconnect_time; /* when reconnect may be attempted */
400         unsigned long       ibp_reconnect_interval; /* exponential backoff */
401         int                 ibp_error;          /* errno on closing this peer */
402         cfs_time_t          ibp_last_alive;     /* when (in jiffies) I was last alive */
403 } kib_peer_t;
404
405
406 extern kib_data_t      kibnal_data;
407 extern kib_tunables_t  kibnal_tunables;
408
409 int kibnal_startup (lnet_ni_t *ni);
410 void kibnal_shutdown (lnet_ni_t *ni);
411 int kibnal_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg);
412 int kibnal_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg);
413 extern int kibnal_eager_recv (lnet_ni_t *ni, void *private,
414                               lnet_msg_t *lntmsg, void **new_private);
415 int kibnal_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, 
416                 int delayed, unsigned int niov, 
417                 struct iovec *iov, lnet_kiov_t *kiov,
418                 unsigned int offset, unsigned int mlen, unsigned int rlen);
419 extern void kibnal_init_msg(kib_msg_t *msg, int type, int body_nob);
420 extern void kibnal_pack_msg(kib_msg_t *msg, __u32 version, int credits,
421                             lnet_nid_t dstnid, __u64 dststamp, __u64 seq);
422 extern int  kibnal_unpack_msg(kib_msg_t *msg, __u32 expected_version, int nob);
423 extern int  kibnal_create_peer(kib_peer_t **peerp, lnet_nid_t nid);
424 extern void kibnal_destroy_peer(kib_peer_t *peer);
425 extern int  kibnal_add_persistent_peer (lnet_nid_t nid, __u32 ip);
426 extern int  kibnal_del_peer(lnet_nid_t nid);
427 extern kib_peer_t *kibnal_find_peer_locked(lnet_nid_t nid);
428 extern void kibnal_unlink_peer_locked(kib_peer_t *peer);
429 extern void kibnal_peer_alive(kib_peer_t *peer);
430 extern int  kibnal_close_stale_conns_locked(kib_peer_t *peer,
431                                             __u64 incarnation);
432 extern kib_conn_t *kibnal_create_conn(cm_cep_handle_t cep);
433 extern void kibnal_listen_callback(cm_cep_handle_t cep, cm_conn_data_t *info, void *arg);
434
435 extern int  kibnal_alloc_pages(kib_pages_t **pp, int npages, int access);
436 extern void kibnal_free_pages(kib_pages_t *p);
437
438 extern void kibnal_check_sends(kib_conn_t *conn);
439 extern void kibnal_close_conn_locked(kib_conn_t *conn, int error);
440 extern void kibnal_destroy_conn(kib_conn_t *conn);
441 extern int  kibnal_thread_start(int (*fn)(void *arg), void *arg);
442 extern int  kibnal_scheduler(void *arg);
443 extern int  kibnal_connd(void *arg);
444 extern void kibnal_init_tx_msg(kib_tx_t *tx, int type, int body_nob);
445 extern void kibnal_close_conn(kib_conn_t *conn, int why);
446 extern int  kibnal_set_qp_state(kib_conn_t *conn, vv_qp_state_t new_state);
447 extern void kibnal_async_callback(vv_event_record_t ev);
448 extern void kibnal_cq_callback(unsigned long context);
449 extern void kibnal_passive_connreq(kib_pcreq_t *pcr, int reject);
450 extern void kibnal_txlist_done (struct list_head *txlist, int status);
451 extern void kibnal_queue_tx(kib_tx_t *tx, kib_conn_t *conn);
452 extern int  kibnal_init_rdma(kib_tx_t *tx, int type, int nob,
453                              kib_rdma_desc_t *dstrd, __u64 dstcookie);
454 extern int  kibnal_tunables_init(void);
455 extern void kibnal_tunables_fini(void);
456
457 #define kibnal_conn_addref(conn)                                \
458 do {                                                            \
459         CDEBUG(D_NET, "conn[%p] (%d)++\n",                      \
460                (conn), atomic_read(&(conn)->ibc_refcount));     \
461         LASSERT(atomic_read(&(conn)->ibc_refcount) > 0);        \
462         atomic_inc(&(conn)->ibc_refcount);                      \
463 } while (0)
464
465 #define kibnal_conn_decref(conn)                                              \
466 do {                                                                          \
467         unsigned long   flags;                                                \
468                                                                               \
469         CDEBUG(D_NET, "conn[%p] (%d)--\n",                                    \
470                (conn), atomic_read(&(conn)->ibc_refcount));                   \
471         LASSERT(atomic_read(&(conn)->ibc_refcount) > 0);                      \
472         if (atomic_dec_and_test(&(conn)->ibc_refcount)) {                     \
473                 spin_lock_irqsave(&kibnal_data.kib_connd_lock, flags);        \
474                 list_add_tail(&(conn)->ibc_list,                              \
475                               &kibnal_data.kib_connd_zombies);                \
476                 wake_up(&kibnal_data.kib_connd_waitq);                        \
477                 spin_unlock_irqrestore(&kibnal_data.kib_connd_lock, flags);   \
478         }                                                                     \
479 } while (0)
480
481 #define kibnal_peer_addref(peer)                                \
482 do {                                                            \
483         CDEBUG(D_NET, "peer[%p] -> %s (%d)++\n",                \
484                (peer), libcfs_nid2str((peer)->ibp_nid),         \
485                atomic_read (&(peer)->ibp_refcount));            \
486         LASSERT(atomic_read(&(peer)->ibp_refcount) > 0);        \
487         atomic_inc(&(peer)->ibp_refcount);                      \
488 } while (0)
489
490 #define kibnal_peer_decref(peer)                                \
491 do {                                                            \
492         CDEBUG(D_NET, "peer[%p] -> %s (%d)--\n",                \
493                (peer), libcfs_nid2str((peer)->ibp_nid),         \
494                atomic_read (&(peer)->ibp_refcount));            \
495         LASSERT(atomic_read(&(peer)->ibp_refcount) > 0);        \
496         if (atomic_dec_and_test(&(peer)->ibp_refcount))         \
497                 kibnal_destroy_peer(peer);                      \
498 } while (0)
499
500 static inline struct list_head *
501 kibnal_nid2peerlist (lnet_nid_t nid)
502 {
503         unsigned int hash = ((unsigned int)nid) % kibnal_data.kib_peer_hash_size;
504
505         return (&kibnal_data.kib_peers [hash]);
506 }
507
508 static inline int
509 kibnal_peer_active (kib_peer_t *peer)
510 {
511         /* Am I in the peer hash table? */
512         return (!list_empty(&peer->ibp_list));
513 }
514
515 static inline void
516 kibnal_queue_tx_locked (kib_tx_t *tx, kib_conn_t *conn)
517 {
518         struct list_head  *q;
519         
520         LASSERT (tx->tx_nwrq > 0);              /* work items set up */
521         LASSERT (!tx->tx_queued);               /* not queued for sending already */
522
523         tx->tx_queued = 1;
524         tx->tx_deadline = jiffies + (*kibnal_tunables.kib_timeout * HZ);
525
526         if (tx->tx_conn == NULL) {
527                 kibnal_conn_addref(conn);
528                 tx->tx_conn = conn;
529                 LASSERT (tx->tx_msg->ibm_type != IBNAL_MSG_PUT_DONE);
530         } else {
531                 LASSERT (tx->tx_conn == conn);
532                 LASSERT (tx->tx_msg->ibm_type == IBNAL_MSG_PUT_DONE);
533         }
534
535         if (conn->ibc_version == IBNAL_MSG_VERSION_RDMAREPLYNOTRSRVD) {
536                 /* All messages have simple credit control */
537                 q = &conn->ibc_tx_queue;
538         } else {
539                 LASSERT (conn->ibc_version == IBNAL_MSG_VERSION);
540                 
541                 switch (tx->tx_msg->ibm_type) {
542                 case IBNAL_MSG_PUT_REQ:
543                 case IBNAL_MSG_GET_REQ:
544                         /* RDMA request: reserve a buffer for the RDMA reply
545                          * before sending */
546                         q = &conn->ibc_tx_queue_rsrvd;
547                         break;
548
549                 case IBNAL_MSG_PUT_NAK:
550                 case IBNAL_MSG_PUT_ACK:
551                 case IBNAL_MSG_PUT_DONE:
552                 case IBNAL_MSG_GET_DONE:
553                         /* RDMA reply/completion: no credits; peer has reserved
554                          * a reply buffer */
555                         q = &conn->ibc_tx_queue_nocred;
556                         break;
557                 
558                 case IBNAL_MSG_NOOP:
559                 case IBNAL_MSG_IMMEDIATE:
560                         /* Otherwise: consume a credit before sending */
561                         q = &conn->ibc_tx_queue;
562                         break;
563                 
564                 default:
565                         LBUG();
566                         q = NULL;
567                 }
568         }
569         
570         list_add_tail(&tx->tx_list, q);
571 }
572
573 static inline int
574 kibnal_send_keepalive(kib_conn_t *conn) 
575 {
576         return (*kibnal_tunables.kib_keepalive > 0) &&
577                 time_after(jiffies, conn->ibc_last_send +
578                            *kibnal_tunables.kib_keepalive*HZ);
579 }
580
581 #ifndef IBNAL_VOIDSTAR_SGADDR
582 # define IBNAL_VOIDSTAR_SGADDR 0
583 #endif
584
585 #if IBNAL_VOIDSTAR_SGADDR
586 # if defined(CONFIG_HIGHMEM)
587 #  if defined(CONFIG_X86) && defined(CONFIG_HIGHMEM4G)
588    /* truncation to void* doesn't matter if 0 <= physmem < 4G
589     * so allow x86 with 32 bit phys addrs */
590 #  elif defined(CONFIG_IA64)
591    /* OK anyway on 64-bit arch */
592 #  else
593 #   error "Can't support HIGHMEM when vv_scatgat_t::v_address is void *"
594 #  endif
595 # endif
596 # define KIBNAL_ADDR2SG(a)       ((void *)((unsigned long)(a)))
597 # define KIBNAL_SG2ADDR(a)       ((__u64)((unsigned long)(a)))
598 static inline __u64 kibnal_addr2net (__u64 addr)
599 {
600         void        *netaddr;
601         vv_return_t  vvrc = vv_va2advertise_addr(kibnal_data.kib_hca, 
602                                                  KIBNAL_ADDR2SG(addr),
603                                                  &netaddr);
604         LASSERT (vvrc == vv_return_ok);
605         return KIBNAL_SG2ADDR(netaddr);
606 }
607 #else
608 # define KIBNAL_ADDR2SG(a)       a
609 # define KIBNAL_SG2ADDR(a)       a
610 static inline __u64 kibnal_addr2net (__u64 addr)
611 {
612         __u64        netaddr;
613         vv_return_t  vvrc = vv_va2advertise_addr(kibnal_data.kib_hca, 
614                                                  addr,
615                                                  &netaddr);
616         LASSERT (vvrc == vv_return_ok);
617         return netaddr;
618 }
619 #endif
620
621 /* CAVEAT EMPTOR: We rely on tx/rx descriptor alignment to allow us to use the
622  * lowest 2 bits of the work request id to stash the work item type (the op
623  * field is not valid when the wc completes in error). */
624
625 #define IBNAL_WID_TX    0
626 #define IBNAL_WID_RX    1
627 #define IBNAL_WID_RDMA  2
628 #define IBNAL_WID_MASK  3UL
629
630 static inline vv_wr_id_t
631 kibnal_ptr2wreqid (void *ptr, int type)
632 {
633         unsigned long lptr = (unsigned long)ptr;
634
635         LASSERT ((lptr & IBNAL_WID_MASK) == 0);
636         LASSERT ((type & ~IBNAL_WID_MASK) == 0);
637         return (vv_wr_id_t)(lptr | type);
638 }
639
640 static inline void *
641 kibnal_wreqid2ptr (vv_wr_id_t wreqid)
642 {
643         return (void *)(((unsigned long)wreqid) & ~IBNAL_WID_MASK);
644 }
645
646 static inline int
647 kibnal_wreqid2type (vv_wr_id_t wreqid)
648 {
649         return (wreqid & IBNAL_WID_MASK);
650 }
651
652 static inline void
653 kibnal_set_conn_state (kib_conn_t *conn, int state)
654 {
655         conn->ibc_state = state;
656         mb();
657 }
658
659 #if IBNAL_USE_FMR
660
661 static inline int
662 kibnal_rd_size (kib_rdma_desc_t *rd) 
663 {
664         return rd->rd_nob;
665 }
666
667 #else
668 static inline __u64
669 kibnal_rf_addr (kib_rdma_frag_t *rf)
670 {
671         return  (((__u64)rf->rf_addr_hi)<<32) | ((__u64)rf->rf_addr_lo);
672 }
673
674 static inline void
675 kibnal_rf_set (kib_rdma_frag_t *rf, __u64 addr, int nob)
676 {
677         rf->rf_addr_lo = addr & 0xffffffff;
678         rf->rf_addr_hi = (addr >> 32) & 0xffffffff;
679         rf->rf_nob = nob;
680 }
681
682 static inline int
683 kibnal_rd_size (kib_rdma_desc_t *rd)
684 {
685         int   i;
686         int   size;
687         
688         for (i = size = 0; i < rd->rd_nfrag; i++)
689                 size += rd->rd_frags[i].rf_nob;
690         
691         return size;
692 }
693 #endif