Whamcloud - gitweb
b=16186,i=liangzhen,i=maxim:
[fs/lustre-release.git] / lnet / klnds / o2iblnd / o2iblnd.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  2008 Sun Microsystems, Inc. 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/o2iblnd/o2iblnd.h
37  *
38  * Author: Eric Barton <eric@bartonsoftware.com>
39  */
40
41 #ifndef EXPORT_SYMTAB
42 # define EXPORT_SYMTAB
43 #endif
44 #ifndef AUTOCONF_INCLUDED
45 #include <linux/config.h>
46 #endif
47 #include <linux/module.h>
48 #include <linux/kernel.h>
49 #include <linux/mm.h>
50 #include <linux/string.h>
51 #include <linux/stat.h>
52 #include <linux/errno.h>
53 #include <linux/smp_lock.h>
54 #include <linux/unistd.h>
55 #include <linux/uio.h>
56
57 #include <asm/system.h>
58 #include <asm/uaccess.h>
59 #include <asm/io.h>
60
61 #include <linux/init.h>
62 #include <linux/fs.h>
63 #include <linux/file.h>
64 #include <linux/stat.h>
65 #include <linux/list.h>
66 #include <linux/kmod.h>
67 #include <linux/sysctl.h>
68 #include <linux/random.h>
69 #include <linux/pci.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 #if !HAVE_GFP_T
82 typedef int gfp_t;
83 #endif
84
85 #include <rdma/rdma_cm.h>
86 #include <rdma/ib_cm.h>
87 #include <rdma/ib_verbs.h>
88 #include <rdma/ib_fmr_pool.h>
89
90 /* tunables fixed at compile time */
91 #ifdef CONFIG_SMP
92 # define IBLND_N_SCHED      num_online_cpus()   /* # schedulers */
93 #else
94 # define IBLND_N_SCHED      1                   /* # schedulers */
95 #endif
96
97 #define IBLND_PEER_HASH_SIZE         101        /* # peer lists */
98 #define IBLND_RESCHED                100        /* # scheduler loops before reschedule */
99 #define IBLND_MSG_QUEUE_SIZE         8          /* # messages/RDMAs in-flight */
100 #define IBLND_CREDIT_HIGHWATER       7          /* when eagerly to return credits */
101 #define IBLND_MSG_SIZE              (4<<10)     /* max size of queued messages (inc hdr) */
102
103 #define IBLND_MAP_ON_DEMAND  0
104 #if IBLND_MAP_ON_DEMAND
105 # define IBLND_MAX_RDMA_FRAGS        1
106 #else
107 # define IBLND_MAX_RDMA_FRAGS        LNET_MAX_IOV
108 #endif
109
110 /************************/
111 /* derived constants... */
112
113 /* TX messages (shared by all connections) */
114 #define IBLND_TX_MSGS()       (*kiblnd_tunables.kib_ntx)
115 #define IBLND_TX_MSG_BYTES()  (IBLND_TX_MSGS() * IBLND_MSG_SIZE)
116 #define IBLND_TX_MSG_PAGES()  ((IBLND_TX_MSG_BYTES() + PAGE_SIZE - 1)/PAGE_SIZE)
117
118 /* RX messages (per connection) */
119 #define IBLND_RX_MSGS         (IBLND_MSG_QUEUE_SIZE * 2)
120 #define IBLND_RX_MSG_BYTES    (IBLND_RX_MSGS * IBLND_MSG_SIZE)
121 #define IBLND_RX_MSG_PAGES    ((IBLND_RX_MSG_BYTES + PAGE_SIZE - 1)/PAGE_SIZE)
122
123 /* WRs and CQEs (per connection) */
124 #define IBLND_RECV_WRS        IBLND_RX_MSGS
125 #define IBLND_SEND_WRS        ((*kiblnd_tunables.kib_concurrent_sends) * \
126                                (1 + IBLND_MAX_RDMA_FRAGS))
127 #define IBLND_CQ_ENTRIES()    (IBLND_RECV_WRS + IBLND_SEND_WRS)
128
129 typedef struct
130 {
131         unsigned int     *kib_service;          /* IB service number */
132         int              *kib_min_reconnect_interval; /* first failed connection retry... */
133         int              *kib_max_reconnect_interval; /* ...exponentially increasing to this */
134         int              *kib_cksum;            /* checksum kib_msg_t? */
135         int              *kib_timeout;          /* comms timeout (seconds) */
136         int              *kib_keepalive;        /* keepalive timeout (seconds) */
137         int              *kib_ntx;              /* # tx descs */
138         int              *kib_credits;          /* # concurrent sends */
139         int              *kib_peercredits;      /* # concurrent sends to 1 peer */
140         int              *kib_peertimeout;      /* seconds to consider peer dead */
141         char            **kib_default_ipif;     /* default IPoIB interface */
142         int              *kib_retry_count;
143         int              *kib_rnr_retry_count;
144         int              *kib_concurrent_sends; /* send work queue sizing */
145         int              *kib_ib_mtu;           /* IB MTU */
146 #if IBLND_MAP_ON_DEMAND
147         int              *kib_fmr_pool_size;    /* # FMRs in pool */
148         int              *kib_fmr_flush_trigger; /* When to trigger FMR flush */
149         int              *kib_fmr_cache;        /* enable FMR pool cache? */
150 #endif
151 #if defined(CONFIG_SYSCTL) && !CFS_SYSFS_MODULE_PARM
152         cfs_sysctl_table_header_t *kib_sysctl;  /* sysctl interface */
153 #endif
154 } kib_tunables_t;
155
156 typedef struct
157 {
158         int               ibp_npages;           /* # pages */
159         struct page      *ibp_pages[0];
160 } kib_pages_t;
161
162 typedef struct 
163 {
164         struct list_head     ibd_list;          /* chain on kib_devs */
165         __u32                ibd_ifip;          /* IPoIB interface IP */
166         char                 ibd_ifname[32];    /* IPoIB interface name */
167         int                  ibd_nnets;         /* # nets extant */
168
169         struct rdma_cm_id   *ibd_cmid;          /* IB listener (bound to 1 device) */
170         struct ib_pd        *ibd_pd;            /* PD for the device */
171         struct ib_mr        *ibd_mr;            /* MR for non RDMA I/O */
172 } kib_dev_t;
173
174 typedef struct
175 {
176         __u64                ibn_incarnation;   /* my epoch */
177         int                  ibn_init;          /* initialisation state */
178         int                  ibn_shutdown;      /* shutting down? */
179
180         atomic_t             ibn_npeers;        /* # peers extant */
181         atomic_t             ibn_nconns;        /* # connections extant */
182
183         struct kib_tx       *ibn_tx_descs;      /* all the tx descriptors */
184         kib_pages_t         *ibn_tx_pages;      /* premapped tx msg pages */
185         struct list_head     ibn_idle_txs;      /* idle tx descriptors */
186         spinlock_t           ibn_tx_lock;       /* serialise */
187
188 #if IBLND_MAP_ON_DEMAND
189         struct ib_fmr_pool  *ibn_fmrpool;       /* FMR pool for RDMA I/O */
190 #endif
191
192         kib_dev_t           *ibn_dev;           /* underlying IB device */
193 } kib_net_t;
194
195 typedef struct
196 {
197         int                  kib_init;          /* initialisation state */
198         int                  kib_shutdown;      /* shut down? */
199         struct list_head     kib_devs;          /* IB devices extant */
200         atomic_t             kib_nthreads;      /* # live threads */
201         rwlock_t             kib_global_lock;   /* stabilize net/dev/peer/conn ops */
202
203         struct list_head    *kib_peers;         /* hash table of all my known peers */
204         int                  kib_peer_hash_size; /* size of kib_peers */
205
206         void                *kib_connd;         /* the connd task (serialisation assertions) */
207         struct list_head     kib_connd_conns;   /* connections to setup/teardown */
208         struct list_head     kib_connd_zombies; /* connections with zero refcount */
209         wait_queue_head_t    kib_connd_waitq;   /* connection daemon sleeps here */
210         spinlock_t           kib_connd_lock;    /* serialise */
211
212         wait_queue_head_t    kib_sched_waitq;   /* schedulers sleep here */
213         struct list_head     kib_sched_conns;   /* conns to check for rx completions */
214         spinlock_t           kib_sched_lock;    /* serialise */
215
216         __u64                kib_next_tx_cookie; /* RDMA completion cookie */
217         struct ib_qp_attr    kib_error_qpa;      /* QP->ERROR */
218 } kib_data_t;
219
220 #define IBLND_INIT_NOTHING         0
221 #define IBLND_INIT_DATA            1
222 #define IBLND_INIT_ALL             2
223
224 /************************************************************************
225  * IB Wire message format.
226  * These are sent in sender's byte order (i.e. receiver flips).
227  */
228
229 typedef struct kib_connparams
230 {
231         __u16             ibcp_queue_depth;
232         __u16             ibcp_max_frags;
233         __u32             ibcp_max_msg_size;
234 } WIRE_ATTR kib_connparams_t;
235
236 typedef struct
237 {
238         lnet_hdr_t        ibim_hdr;             /* portals header */
239         char              ibim_payload[0];      /* piggy-backed payload */
240 } WIRE_ATTR kib_immediate_msg_t;
241
242 #if IBLND_MAP_ON_DEMAND
243 typedef struct
244 {
245         __u64             rd_addr;              /* IO VMA address */
246         __u32             rd_nob;               /* # of bytes */
247         __u32             rd_key;               /* remote key */
248 } WIRE_ATTR kib_rdma_desc_t;
249 #else
250 typedef struct
251 {
252         __u32             rf_nob;               /* # bytes this frag */
253         __u64             rf_addr;              /* CAVEAT EMPTOR: misaligned!! */
254 } WIRE_ATTR kib_rdma_frag_t;
255
256 typedef struct
257 {
258         __u32             rd_key;               /* local/remote key */
259         __u32             rd_nfrags;            /* # fragments */
260         kib_rdma_frag_t   rd_frags[0];          /* buffer frags */
261 } WIRE_ATTR kib_rdma_desc_t;
262 #endif
263         
264 typedef struct
265 {
266         lnet_hdr_t        ibprm_hdr;            /* portals header */
267         __u64             ibprm_cookie;         /* opaque completion cookie */
268 } WIRE_ATTR kib_putreq_msg_t;
269
270 typedef struct
271 {
272         __u64             ibpam_src_cookie;     /* reflected completion cookie */
273         __u64             ibpam_dst_cookie;     /* opaque completion cookie */
274         kib_rdma_desc_t   ibpam_rd;             /* sender's sink buffer */
275 } WIRE_ATTR kib_putack_msg_t;
276
277 typedef struct
278 {
279         lnet_hdr_t        ibgm_hdr;             /* portals header */
280         __u64             ibgm_cookie;          /* opaque completion cookie */
281         kib_rdma_desc_t   ibgm_rd;              /* rdma descriptor */
282 } WIRE_ATTR kib_get_msg_t;
283
284 typedef struct
285 {
286         __u64             ibcm_cookie;          /* opaque completion cookie */
287         __s32             ibcm_status;          /* < 0 failure: >= 0 length */
288 } WIRE_ATTR kib_completion_msg_t;
289
290 typedef struct
291 {
292         /* First 2 fields fixed FOR ALL TIME */
293         __u32             ibm_magic;            /* I'm an openibnal message */
294         __u16             ibm_version;          /* this is my version number */
295
296         __u8              ibm_type;             /* msg type */
297         __u8              ibm_credits;          /* returned credits */
298         __u32             ibm_nob;              /* # bytes in whole message */
299         __u32             ibm_cksum;            /* checksum (0 == no checksum) */
300         __u64             ibm_srcnid;           /* sender's NID */
301         __u64             ibm_srcstamp;         /* sender's incarnation */
302         __u64             ibm_dstnid;           /* destination's NID */
303         __u64             ibm_dststamp;         /* destination's incarnation */
304
305         union {
306                 kib_connparams_t      connparams;
307                 kib_immediate_msg_t   immediate;
308                 kib_putreq_msg_t      putreq;
309                 kib_putack_msg_t      putack;
310                 kib_get_msg_t         get;
311                 kib_completion_msg_t  completion;
312         } WIRE_ATTR ibm_u;
313 } WIRE_ATTR kib_msg_t;
314
315 #define IBLND_MSG_MAGIC LNET_PROTO_IB_MAGIC     /* unique magic */
316
317 #define IBLND_MSG_VERSION           0x11
318
319 #define IBLND_MSG_CONNREQ           0xc0        /* connection request */
320 #define IBLND_MSG_CONNACK           0xc1        /* connection acknowledge */
321 #define IBLND_MSG_NOOP              0xd0        /* nothing (just credits) */
322 #define IBLND_MSG_IMMEDIATE         0xd1        /* immediate */
323 #define IBLND_MSG_PUT_REQ           0xd2        /* putreq (src->sink) */
324 #define IBLND_MSG_PUT_NAK           0xd3        /* completion (sink->src) */
325 #define IBLND_MSG_PUT_ACK           0xd4        /* putack (sink->src) */
326 #define IBLND_MSG_PUT_DONE          0xd5        /* completion (src->sink) */
327 #define IBLND_MSG_GET_REQ           0xd6        /* getreq (sink->src) */
328 #define IBLND_MSG_GET_DONE          0xd7        /* completion (src->sink: all OK) */
329
330 typedef struct {
331         __u32            ibr_magic;             /* sender's magic */
332         __u16            ibr_version;           /* sender's version */
333         __u8             ibr_why;               /* reject reason */
334 } WIRE_ATTR kib_rej_t;
335
336
337 /* connection rejection reasons */
338 #define IBLND_REJECT_CONN_RACE       1          /* You lost connection race */
339 #define IBLND_REJECT_NO_RESOURCES    2          /* Out of memory/conns etc */
340 #define IBLND_REJECT_FATAL           3          /* Anything else */
341
342 /***********************************************************************/
343
344 typedef struct kib_rx                           /* receive message */
345 {
346         struct list_head          rx_list;      /* queue for attention */
347         struct kib_conn          *rx_conn;      /* owning conn */
348         int                       rx_nob;       /* # bytes received (-1 while posted) */
349         enum ib_wc_status         rx_status;    /* completion status */
350         kib_msg_t                *rx_msg;       /* message buffer (host vaddr) */
351         __u64                     rx_msgaddr;   /* message buffer (I/O addr) */
352         DECLARE_PCI_UNMAP_ADDR   (rx_msgunmap); /* for dma_unmap_single() */
353         struct ib_recv_wr         rx_wrq;       /* receive work item... */
354         struct ib_sge             rx_sge;       /* ...and its memory */
355 } kib_rx_t;
356
357 #define IBLND_POSTRX_DONT_POST    0             /* don't post */
358 #define IBLND_POSTRX_NO_CREDIT    1             /* post: no credits */
359 #define IBLND_POSTRX_PEER_CREDIT  2             /* post: give peer back 1 credit */
360 #define IBLND_POSTRX_RSRVD_CREDIT 3             /* post: give myself back 1 reserved credit */
361
362 typedef struct kib_tx                           /* transmit message */
363 {
364         struct list_head          tx_list;      /* queue on idle_txs ibc_tx_queue etc. */
365         struct kib_conn          *tx_conn;      /* owning conn */
366         int                       tx_sending;   /* # tx callbacks outstanding */
367         int                       tx_queued;    /* queued for sending */
368         int                       tx_waiting;   /* waiting for peer */
369         int                       tx_status;    /* LNET completion status */
370         unsigned long             tx_deadline;  /* completion deadline */
371         __u64                     tx_cookie;    /* completion cookie */
372         lnet_msg_t               *tx_lntmsg[2]; /* lnet msgs to finalize on completion */
373         kib_msg_t                *tx_msg;       /* message buffer (host vaddr) */
374         __u64                     tx_msgaddr;   /* message buffer (I/O addr) */
375         DECLARE_PCI_UNMAP_ADDR   (tx_msgunmap); /* for dma_unmap_single() */
376         int                       tx_nwrq;      /* # send work items */
377 #if IBLND_MAP_ON_DEMAND
378         struct ib_send_wr         tx_wrq[2];    /* send work items... */
379         struct ib_sge             tx_sge[2];    /* ...and their memory */
380         kib_rdma_desc_t           tx_rd[1];     /* rdma descriptor */
381         __u64                    *tx_pages;     /* rdma phys page addrs */
382         struct ib_pool_fmr       *tx_fmr;       /* rdma mapping (mapped if != NULL) */
383 #else
384         struct ib_send_wr        *tx_wrq;       /* send work items... */
385         struct ib_sge            *tx_sge;       /* ...and their memory */
386         kib_rdma_desc_t          *tx_rd;        /* rdma descriptor */
387         int                       tx_nfrags;    /* # entries in... */
388         struct scatterlist       *tx_frags;     /* dma_map_sg descriptor */
389         int                       tx_dmadir;    /* dma direction */
390 #endif        
391 } kib_tx_t;
392
393 typedef struct kib_connvars
394 {
395         /* connection-in-progress variables */
396         kib_msg_t                 cv_msg;
397 } kib_connvars_t;
398
399 typedef struct kib_conn
400 {
401         struct kib_peer    *ibc_peer;           /* owning peer */
402         struct list_head    ibc_list;           /* stash on peer's conn list */
403         struct list_head    ibc_sched_list;     /* schedule for attention */
404         __u64               ibc_incarnation;    /* which instance of the peer */
405         atomic_t            ibc_refcount;       /* # users */
406         int                 ibc_state;          /* what's happening */
407         int                 ibc_nsends_posted;  /* # uncompleted sends */
408         int                 ibc_credits;        /* # credits I have */
409         int                 ibc_outstanding_credits; /* # credits to return */
410         int                 ibc_reserved_credits;/* # ACK/DONE msg credits */
411         int                 ibc_comms_error;    /* set on comms error */
412         int                 ibc_nrx:8;          /* receive buffers owned */
413         int                 ibc_scheduled:1;    /* scheduled for attention */
414         int                 ibc_ready:1;        /* CQ callback fired */
415         unsigned long       ibc_last_send;      /* time of last send */
416         struct list_head    ibc_early_rxs;      /* rxs completed before ESTABLISHED */
417         struct list_head    ibc_tx_noops;       /* IBLND_MSG_NOOPs */
418         struct list_head    ibc_tx_queue;       /* sends that need a credit */
419         struct list_head    ibc_tx_queue_nocred;/* sends that don't need a credit */
420         struct list_head    ibc_tx_queue_rsrvd; /* sends that need to reserve an ACK/DONE msg */
421         struct list_head    ibc_active_txs;     /* active tx awaiting completion */
422         spinlock_t          ibc_lock;           /* serialise */
423         kib_rx_t           *ibc_rxs;            /* the rx descs */
424         kib_pages_t        *ibc_rx_pages;       /* premapped rx msg pages */
425
426         struct rdma_cm_id  *ibc_cmid;           /* CM id */
427         struct ib_cq       *ibc_cq;             /* completion queue */
428
429         kib_connvars_t     *ibc_connvars;       /* in-progress connection state */
430 } kib_conn_t;
431
432 #define IBLND_CONN_INIT               0         /* being intialised */
433 #define IBLND_CONN_ACTIVE_CONNECT     1         /* active sending req */
434 #define IBLND_CONN_PASSIVE_WAIT       2         /* passive waiting for rtu */
435 #define IBLND_CONN_ESTABLISHED        3         /* connection established */
436 #define IBLND_CONN_CLOSING            4         /* being closed */
437 #define IBLND_CONN_DISCONNECTED       5         /* disconnected */
438
439 typedef struct kib_peer
440 {
441         struct list_head    ibp_list;           /* stash on global peer list */
442         lnet_nid_t          ibp_nid;            /* who's on the other end(s) */
443         lnet_ni_t          *ibp_ni;             /* LNet interface */
444         atomic_t            ibp_refcount;       /* # users */
445         struct list_head    ibp_conns;          /* all active connections */
446         struct list_head    ibp_tx_queue;       /* msgs waiting for a conn */
447         int                 ibp_connecting;     /* current active connection attempts */
448         int                 ibp_accepting;      /* current passive connection attempts */
449         int                 ibp_error;          /* errno on closing this peer */
450         cfs_time_t          ibp_last_alive;     /* when (in jiffies) I was last alive */
451 } kib_peer_t;
452
453
454 extern kib_data_t      kiblnd_data;
455 extern kib_tunables_t  kiblnd_tunables;
456
457 #define kiblnd_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 kiblnd_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(&kiblnd_data.kib_connd_lock, flags);        \
474                 list_add_tail(&(conn)->ibc_list,                              \
475                               &kiblnd_data.kib_connd_zombies);                \
476                 wake_up(&kiblnd_data.kib_connd_waitq);                        \
477                 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);   \
478         }                                                                     \
479 } while (0)
480
481 #define kiblnd_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 kiblnd_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                 kiblnd_destroy_peer(peer);                      \
498 } while (0)
499
500 static inline struct list_head *
501 kiblnd_nid2peerlist (lnet_nid_t nid)
502 {
503         unsigned int hash = ((unsigned int)nid) % kiblnd_data.kib_peer_hash_size;
504
505         return (&kiblnd_data.kib_peers [hash]);
506 }
507
508 static inline int
509 kiblnd_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 kib_conn_t *
516 kiblnd_get_conn_locked (kib_peer_t *peer)
517 {
518         LASSERT (!list_empty(&peer->ibp_conns));
519
520         /* just return the first connection */
521         return list_entry(peer->ibp_conns.next, kib_conn_t, ibc_list);
522 }
523
524 static inline int
525 kiblnd_send_keepalive(kib_conn_t *conn)
526 {
527         return (*kiblnd_tunables.kib_keepalive > 0) &&
528                 time_after(jiffies, conn->ibc_last_send +
529                            *kiblnd_tunables.kib_keepalive*HZ);
530 }
531
532 static inline int
533 kiblnd_send_noop(kib_conn_t *conn)
534 {
535         LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
536
537         if (conn->ibc_outstanding_credits < IBLND_CREDIT_HIGHWATER &&
538             !kiblnd_send_keepalive(conn))
539                 return 0; /* No need to send NOOP */
540
541         if (!list_empty(&conn->ibc_tx_noops) ||       /* NOOP already queued */
542             !list_empty(&conn->ibc_tx_queue_nocred) || /* can be piggybacked */
543             conn->ibc_credits == 0)                    /* no credit */
544                 return 0;
545
546         if (conn->ibc_credits == 1 &&      /* last credit reserved for */
547             conn->ibc_outstanding_credits == 0) /* giving back credits */
548                 return 0;
549
550         /* No tx to piggyback NOOP onto or no credit to send a tx */
551         return (list_empty(&conn->ibc_tx_queue) || conn->ibc_credits == 1);
552 }
553
554 static inline void
555 kiblnd_abort_receives(kib_conn_t *conn)
556 {
557         ib_modify_qp(conn->ibc_cmid->qp,
558                      &kiblnd_data.kib_error_qpa, IB_QP_STATE);
559 }
560
561 /* CAVEAT EMPTOR: We rely on descriptor alignment to allow us to use the
562  * lowest bits of the work request id to stash the work item type. */
563
564 #define IBLND_WID_TX    0
565 #define IBLND_WID_RDMA  1
566 #define IBLND_WID_RX    2
567 #define IBLND_WID_MASK  3UL
568
569 static inline __u64
570 kiblnd_ptr2wreqid (void *ptr, int type)
571 {
572         unsigned long lptr = (unsigned long)ptr;
573
574         LASSERT ((lptr & IBLND_WID_MASK) == 0);
575         LASSERT ((type & ~IBLND_WID_MASK) == 0);
576         return (__u64)(lptr | type);
577 }
578
579 static inline void *
580 kiblnd_wreqid2ptr (__u64 wreqid)
581 {
582         return (void *)(((unsigned long)wreqid) & ~IBLND_WID_MASK);
583 }
584
585 static inline int
586 kiblnd_wreqid2type (__u64 wreqid)
587 {
588         return (wreqid & IBLND_WID_MASK);
589 }
590
591 static inline void
592 kiblnd_set_conn_state (kib_conn_t *conn, int state)
593 {
594         conn->ibc_state = state;
595         mb();
596 }
597
598 #if IBLND_MAP_ON_DEMAND
599 static inline int
600 kiblnd_rd_size (kib_rdma_desc_t *rd)
601 {
602         return rd->rd_nob;
603 }
604 #else
605 static inline int
606 kiblnd_rd_size (kib_rdma_desc_t *rd)
607 {
608         int   i;
609         int   size;
610         
611         for (i = size = 0; i < rd->rd_nfrags; i++)
612                 size += rd->rd_frags[i].rf_nob;
613         
614         return size;
615 }
616 #endif
617
618 #ifdef HAVE_OFED_IB_DMA_MAP
619
620 static inline __u64 kiblnd_dma_map_single(struct ib_device *dev,
621                                           void *msg, size_t size,
622                                           enum dma_data_direction direction)
623 {
624         return ib_dma_map_single(dev, msg, size, direction);
625 }
626
627 static inline void kiblnd_dma_unmap_single(struct ib_device *dev,
628                                            __u64 addr, size_t size,
629                                           enum dma_data_direction direction)
630 {
631         ib_dma_unmap_single(dev, addr, size, direction);
632 }
633
634 #define KIBLND_UNMAP_ADDR_SET(p, m, a)  do {} while (0)
635 #define KIBLND_UNMAP_ADDR(p, m, a)      (a)
636
637 static inline int kiblnd_dma_map_sg(struct ib_device *dev,
638                                     struct scatterlist *sg, int nents,
639                                     enum dma_data_direction direction)
640 {
641         return ib_dma_map_sg(dev, sg, nents, direction);
642 }
643
644 static inline void kiblnd_dma_unmap_sg(struct ib_device *dev,
645                                        struct scatterlist *sg, int nents,
646                                        enum dma_data_direction direction)
647 {
648         ib_dma_unmap_sg(dev, sg, nents, direction);
649 }
650
651 static inline __u64 kiblnd_sg_dma_address(struct ib_device *dev,
652                                           struct scatterlist *sg)
653 {
654         return ib_sg_dma_address(dev, sg);
655 }
656
657 static inline unsigned int kiblnd_sg_dma_len(struct ib_device *dev,
658                                              struct scatterlist *sg)
659 {
660         return ib_sg_dma_len(dev, sg);
661 }
662
663 /* XXX We use KIBLND_CONN_PARAM(e) as writable buffer, it's not strictly
664  * right because OFED1.2 defines it as const, to use it we have to add
665  * (void *) cast to overcome "const" */
666
667 #define KIBLND_CONN_PARAM(e)            ((e)->param.conn.private_data)
668 #define KIBLND_CONN_PARAM_LEN(e)        ((e)->param.conn.private_data_len)
669
670 #else
671
672 static inline dma_addr_t kiblnd_dma_map_single(struct ib_device *dev,
673                                                void *msg, size_t size,
674                                                enum dma_data_direction direction)
675 {
676         return dma_map_single(dev->dma_device, msg, size, direction);
677 }
678
679 static inline void kiblnd_dma_unmap_single(struct ib_device *dev,
680                                            dma_addr_t addr, size_t size,
681                                            enum dma_data_direction direction)
682 {
683         dma_unmap_single(dev->dma_device, addr, size, direction);
684 }
685
686 #define KIBLND_UNMAP_ADDR_SET(p, m, a)  pci_unmap_addr_set(p, m, a)
687 #define KIBLND_UNMAP_ADDR(p, m, a)      pci_unmap_addr(p, m)
688
689 static inline int kiblnd_dma_map_sg(struct ib_device *dev,
690                                     struct scatterlist *sg, int nents,
691                                     enum dma_data_direction direction)
692 {
693         return dma_map_sg(dev->dma_device, sg, nents, direction);
694 }
695
696 static inline void kiblnd_dma_unmap_sg(struct ib_device *dev,
697                                        struct scatterlist *sg, int nents,
698                                        enum dma_data_direction direction)
699 {
700         return dma_unmap_sg(dev->dma_device, sg, nents, direction);
701 }
702
703
704 static inline dma_addr_t kiblnd_sg_dma_address(struct ib_device *dev,
705                                                struct scatterlist *sg)
706 {
707         return sg_dma_address(sg);
708 }
709
710
711 static inline unsigned int kiblnd_sg_dma_len(struct ib_device *dev,
712                                              struct scatterlist *sg)
713 {
714         return sg_dma_len(sg);
715 }
716
717 #define KIBLND_CONN_PARAM(e)            ((e)->private_data)
718 #define KIBLND_CONN_PARAM_LEN(e)        ((e)->private_data_len)
719
720 #endif
721
722 int  kiblnd_startup (lnet_ni_t *ni);
723 void kiblnd_shutdown (lnet_ni_t *ni);
724 int  kiblnd_ctl (lnet_ni_t *ni, unsigned int cmd, void *arg);
725 void kiblnd_query (struct lnet_ni *ni, lnet_nid_t nid, time_t *when);
726
727 int  kiblnd_tunables_init(void);
728 void kiblnd_tunables_fini(void);
729
730 int  kiblnd_connd (void *arg);
731 int  kiblnd_scheduler(void *arg);
732 int  kiblnd_thread_start (int (*fn)(void *arg), void *arg);
733
734 int  kiblnd_alloc_pages (kib_pages_t **pp, int npages);
735 void kiblnd_free_pages (kib_pages_t *p);
736
737 int  kiblnd_cm_callback(struct rdma_cm_id *cmid,
738                         struct rdma_cm_event *event);
739
740 int  kiblnd_create_peer (lnet_ni_t *ni, kib_peer_t **peerp, lnet_nid_t nid);
741 void kiblnd_destroy_peer (kib_peer_t *peer);
742 void kiblnd_destroy_dev (kib_dev_t *dev);
743 void kiblnd_unlink_peer_locked (kib_peer_t *peer);
744 void kiblnd_peer_alive (kib_peer_t *peer);
745 kib_peer_t *kiblnd_find_peer_locked (lnet_nid_t nid);
746 void kiblnd_peer_connect_failed (kib_peer_t *peer, int active, int error);
747 int  kiblnd_close_stale_conns_locked (kib_peer_t *peer, __u64 incarnation);
748
749 void kiblnd_connreq_done(kib_conn_t *conn, int status);
750 kib_conn_t *kiblnd_create_conn (kib_peer_t *peer, struct rdma_cm_id *cmid,
751                                 int state);
752 void kiblnd_destroy_conn (kib_conn_t *conn);
753 void kiblnd_close_conn (kib_conn_t *conn, int error);
754 void kiblnd_close_conn_locked (kib_conn_t *conn, int error);
755
756 int  kiblnd_init_rdma (lnet_ni_t *ni, kib_tx_t *tx, int type,
757                        int nob, kib_rdma_desc_t *dstrd, __u64 dstcookie);
758
759 void kiblnd_launch_tx (lnet_ni_t *ni, kib_tx_t *tx, lnet_nid_t nid);
760 void kiblnd_queue_tx_locked (kib_tx_t *tx, kib_conn_t *conn);
761 void kiblnd_queue_tx (kib_tx_t *tx, kib_conn_t *conn);
762 void kiblnd_init_tx_msg (lnet_ni_t *ni, kib_tx_t *tx, int type, int body_nob);
763 void kiblnd_txlist_done (lnet_ni_t *ni, struct list_head *txlist, int status);
764 void kiblnd_check_sends (kib_conn_t *conn);
765
766 void kiblnd_qp_event(struct ib_event *event, void *arg);
767 void kiblnd_cq_event(struct ib_event *event, void *arg);
768 void kiblnd_cq_completion(struct ib_cq *cq, void *arg);
769
770 void kiblnd_init_msg (kib_msg_t *msg, int type, int body_nob);
771 void kiblnd_pack_msg (lnet_ni_t *ni, kib_msg_t *msg,
772                       int credits, lnet_nid_t dstnid, __u64 dststamp);
773 int  kiblnd_unpack_msg(kib_msg_t *msg, int nob);
774 int  kiblnd_post_rx (kib_rx_t *rx, int credit);
775
776 int  kiblnd_send(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg);
777 int  kiblnd_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
778                  unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
779                  unsigned int offset, unsigned int mlen, unsigned int rlen);
780