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