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