Whamcloud - gitweb
8afba88f771a1bba967b5787a924049ac9641244
[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  * Copyright (C) 2006 Cluster File Systems, Inc.
5  *   Author: Eric Barton <eric@bartonsoftware.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #ifndef EXPORT_SYMTAB
25 # define EXPORT_SYMTAB
26 #endif
27
28 #include <linux/config.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/mm.h>
32 #include <linux/string.h>
33 #include <linux/stat.h>
34 #include <linux/errno.h>
35 #include <linux/smp_lock.h>
36 #include <linux/unistd.h>
37 #include <linux/uio.h>
38
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
41 #include <asm/io.h>
42
43 #include <linux/init.h>
44 #include <linux/fs.h>
45 #include <linux/file.h>
46 #include <linux/stat.h>
47 #include <linux/list.h>
48 #include <linux/kmod.h>
49 #include <linux/sysctl.h>
50 #include <linux/random.h>
51
52 #include <net/sock.h>
53 #include <linux/in.h>
54
55 #define DEBUG_SUBSYSTEM S_LND
56
57 #include <libcfs/kp30.h>
58 #include <lnet/lnet.h>
59 #include <lnet/lib-lnet.h>
60
61 #if !HAVE_GFP_T
62 typedef int gfp_t;
63 #endif
64
65 #include <rdma/rdma_cm.h>
66 #include <rdma/ib_cm.h>
67 #include <rdma/ib_verbs.h>
68 #include <rdma/ib_fmr_pool.h>
69
70 /* tunables fixed at compile time */
71 #ifdef CONFIG_SMP
72 # define IBLND_N_SCHED      num_online_cpus()   /* # schedulers */
73 #else
74 # define IBLND_N_SCHED      1                   /* # schedulers */
75 #endif
76
77 #define IBLND_PEER_HASH_SIZE         101        /* # peer lists */
78 #define IBLND_RESCHED                100        /* # scheduler loops before reschedule */
79 #define IBLND_MSG_QUEUE_SIZE         8          /* # messages/RDMAs in-flight */
80 #define IBLND_CREDIT_HIGHWATER       7          /* when eagerly to return credits */
81 #define IBLND_MSG_SIZE              (4<<10)     /* max size of queued messages (inc hdr) */
82
83 #define IBLND_MAP_ON_DEMAND  0
84 #if IBLND_MAP_ON_DEMAND
85 # define IBLND_MAX_RDMA_FRAGS        1
86 #else
87 # define IBLND_MAX_RDMA_FRAGS        LNET_MAX_IOV
88 #endif
89
90 /************************/
91 /* derived constants... */
92
93 /* TX messages (shared by all connections) */
94 #define IBLND_TX_MSGS()       (*kiblnd_tunables.kib_ntx)
95 #define IBLND_TX_MSG_BYTES()  (IBLND_TX_MSGS() * IBLND_MSG_SIZE)
96 #define IBLND_TX_MSG_PAGES()  ((IBLND_TX_MSG_BYTES() + PAGE_SIZE - 1)/PAGE_SIZE)
97
98 /* RX messages (per connection) */
99 #define IBLND_RX_MSGS         (IBLND_MSG_QUEUE_SIZE*2)
100 #define IBLND_RX_MSG_BYTES    (IBLND_RX_MSGS * IBLND_MSG_SIZE)
101 #define IBLND_RX_MSG_PAGES    ((IBLND_RX_MSG_BYTES + PAGE_SIZE - 1)/PAGE_SIZE)
102
103 #define IBLND_CQ_ENTRIES()    (IBLND_RX_MSGS +                                  \
104                                (*kiblnd_tunables.kib_concurrent_sends) *        \
105                                (1 + IBLND_MAX_RDMA_FRAGS))
106
107 typedef struct
108 {
109         unsigned int     *kib_service;          /* IB service number */
110         int              *kib_min_reconnect_interval; /* first failed connection retry... */
111         int              *kib_max_reconnect_interval; /* ...exponentially increasing to this */
112         int              *kib_cksum;            /* checksum kib_msg_t? */
113         int              *kib_timeout;          /* comms timeout (seconds) */
114         int              *kib_keepalive;        /* keepalive timeout (seconds) */
115         int              *kib_ntx;              /* # tx descs */
116         int              *kib_credits;          /* # concurrent sends */
117         int              *kib_peercredits;      /* # concurrent sends to 1 peer */
118         char            **kib_default_ipif;     /* default IPoIB interface */
119         int              *kib_retry_count;
120         int              *kib_rnr_retry_count;
121         int              *kib_concurrent_sends; /* send work queue sizing */
122         int              *kib_ib_mtu;           /* IB MTU */
123 #if IBLND_MAP_ON_DEMAND
124         int              *kib_fmr_pool_size;    /* # FMRs in pool */
125         int              *kib_fmr_flush_trigger; /* When to trigger FMR flush */
126         int              *kib_fmr_cache;        /* enable FMR pool cache? */
127 #endif
128 #if CONFIG_SYSCTL && !CFS_SYSFS_MODULE_PARM
129         struct ctl_table_header *kib_sysctl;    /* sysctl interface */
130 #endif
131 } kib_tunables_t;
132
133 typedef struct
134 {
135         int               ibp_npages;           /* # pages */
136         struct page      *ibp_pages[0];
137 } kib_pages_t;
138
139 typedef struct 
140 {
141         struct list_head     ibd_list;          /* chain on kib_devs */
142         __u32                ibd_ifip;          /* IPoIB interface IP */
143         char                 ibd_ifname[32];    /* IPoIB interface name */
144         int                  ibd_nnets;         /* # nets extant */
145
146         struct rdma_cm_id   *ibd_cmid;          /* IB listener (bound to 1 device) */
147         struct ib_pd        *ibd_pd;            /* PD for the device */
148         struct ib_mr        *ibd_mr;            /* MR for non RDMA I/O */
149 } kib_dev_t;
150
151 typedef struct
152 {
153         __u64                ibn_incarnation;   /* my epoch */
154         int                  ibn_init;          /* initialisation state */
155         int                  ibn_shutdown;      /* shutting down? */
156
157         atomic_t             ibn_npeers;        /* # peers extant */
158         atomic_t             ibn_nconns;        /* # connections extant */
159
160         struct kib_tx       *ibn_tx_descs;      /* all the tx descriptors */
161         kib_pages_t         *ibn_tx_pages;      /* premapped tx msg pages */
162         struct list_head     ibn_idle_txs;      /* idle tx descriptors */
163         spinlock_t           ibn_tx_lock;       /* serialise */
164
165 #if IBLND_MAP_ON_DEMAND
166         struct ib_fmr_pool  *ibn_fmrpool;       /* FMR pool for RDMA I/O */
167 #endif
168
169         kib_dev_t           *ibn_dev;           /* underlying IB device */
170 } kib_net_t;
171
172 typedef struct
173 {
174         int                  kib_init;          /* initialisation state */
175         int                  kib_shutdown;      /* shut down? */
176         struct list_head     kib_devs;          /* IB devices extant */
177         atomic_t             kib_nthreads;      /* # live threads */
178         rwlock_t             kib_global_lock;   /* stabilize net/dev/peer/conn ops */
179
180         struct list_head    *kib_peers;         /* hash table of all my known peers */
181         int                  kib_peer_hash_size; /* size of kib_peers */
182
183         void                *kib_connd;         /* the connd task (serialisation assertions) */
184         struct list_head     kib_connd_conns;   /* connections to setup/teardown */
185         struct list_head     kib_connd_zombies; /* connections with zero refcount */
186         wait_queue_head_t    kib_connd_waitq;   /* connection daemon sleeps here */
187         spinlock_t           kib_connd_lock;    /* serialise */
188
189         wait_queue_head_t    kib_sched_waitq;   /* schedulers sleep here */
190         struct list_head     kib_sched_conns;   /* conns to check for rx completions */
191         spinlock_t           kib_sched_lock;    /* serialise */
192
193         __u64                kib_next_tx_cookie; /* RDMA completion cookie */
194         struct ib_qp_attr    kib_error_qpa;      /* QP->ERROR */
195 } kib_data_t;
196
197 #define IBLND_INIT_NOTHING         0
198 #define IBLND_INIT_DATA            1
199 #define IBLND_INIT_ALL             2
200
201 /************************************************************************
202  * IB Wire message format.
203  * These are sent in sender's byte order (i.e. receiver flips).
204  */
205
206 typedef struct kib_connparams
207 {
208         __u16             ibcp_queue_depth;
209         __u16             ibcp_max_frags;
210         __u32             ibcp_max_msg_size;
211 } WIRE_ATTR kib_connparams_t;
212
213 typedef struct
214 {
215         lnet_hdr_t        ibim_hdr;             /* portals header */
216         char              ibim_payload[0];      /* piggy-backed payload */
217 } WIRE_ATTR kib_immediate_msg_t;
218
219 #if IBLND_MAP_ON_DEMAND
220 typedef struct
221 {
222         __u64             rd_addr;              /* IO VMA address */
223         __u32             rd_nob;               /* # of bytes */
224         __u32             rd_key;               /* remote key */
225 } WIRE_ATTR kib_rdma_desc_t;
226 #else
227 typedef struct
228 {
229         __u32             rf_nob;               /* # bytes this frag */
230         __u64             rf_addr;              /* CAVEAT EMPTOR: misaligned!! */
231 } WIRE_ATTR kib_rdma_frag_t;
232
233 typedef struct
234 {
235         __u32             rd_key;               /* local/remote key */
236         __u32             rd_nfrags;            /* # fragments */
237         kib_rdma_frag_t   rd_frags[0];          /* buffer frags */
238 } WIRE_ATTR kib_rdma_desc_t;
239 #endif
240         
241 typedef struct
242 {
243         lnet_hdr_t        ibprm_hdr;            /* portals header */
244         __u64             ibprm_cookie;         /* opaque completion cookie */
245 } WIRE_ATTR kib_putreq_msg_t;
246
247 typedef struct
248 {
249         __u64             ibpam_src_cookie;     /* reflected completion cookie */
250         __u64             ibpam_dst_cookie;     /* opaque completion cookie */
251         kib_rdma_desc_t   ibpam_rd;             /* sender's sink buffer */
252 } WIRE_ATTR kib_putack_msg_t;
253
254 typedef struct
255 {
256         lnet_hdr_t        ibgm_hdr;             /* portals header */
257         __u64             ibgm_cookie;          /* opaque completion cookie */
258         kib_rdma_desc_t   ibgm_rd;              /* rdma descriptor */
259 } WIRE_ATTR kib_get_msg_t;
260
261 typedef struct
262 {
263         __u64             ibcm_cookie;          /* opaque completion cookie */
264         __s32             ibcm_status;          /* < 0 failure: >= 0 length */
265 } WIRE_ATTR kib_completion_msg_t;
266
267 typedef struct
268 {
269         /* First 2 fields fixed FOR ALL TIME */
270         __u32             ibm_magic;            /* I'm an openibnal message */
271         __u16             ibm_version;          /* this is my version number */
272
273         __u8              ibm_type;             /* msg type */
274         __u8              ibm_credits;          /* returned credits */
275         __u32             ibm_nob;              /* # bytes in whole message */
276         __u32             ibm_cksum;            /* checksum (0 == no checksum) */
277         __u64             ibm_srcnid;           /* sender's NID */
278         __u64             ibm_srcstamp;         /* sender's incarnation */
279         __u64             ibm_dstnid;           /* destination's NID */
280         __u64             ibm_dststamp;         /* destination's incarnation */
281
282         union {
283                 kib_connparams_t      connparams;
284                 kib_immediate_msg_t   immediate;
285                 kib_putreq_msg_t      putreq;
286                 kib_putack_msg_t      putack;
287                 kib_get_msg_t         get;
288                 kib_completion_msg_t  completion;
289         } WIRE_ATTR ibm_u;
290 } WIRE_ATTR kib_msg_t;
291
292 #define IBLND_MSG_MAGIC LNET_PROTO_IB_MAGIC     /* unique magic */
293
294 #define IBLND_MSG_VERSION           0x11
295
296 #define IBLND_MSG_CONNREQ           0xc0        /* connection request */
297 #define IBLND_MSG_CONNACK           0xc1        /* connection acknowledge */
298 #define IBLND_MSG_NOOP              0xd0        /* nothing (just credits) */
299 #define IBLND_MSG_IMMEDIATE         0xd1        /* immediate */
300 #define IBLND_MSG_PUT_REQ           0xd2        /* putreq (src->sink) */
301 #define IBLND_MSG_PUT_NAK           0xd3        /* completion (sink->src) */
302 #define IBLND_MSG_PUT_ACK           0xd4        /* putack (sink->src) */
303 #define IBLND_MSG_PUT_DONE          0xd5        /* completion (src->sink) */
304 #define IBLND_MSG_GET_REQ           0xd6        /* getreq (sink->src) */
305 #define IBLND_MSG_GET_DONE          0xd7        /* completion (src->sink: all OK) */
306
307 typedef struct {
308         __u32            ibr_magic;             /* sender's magic */
309         __u16            ibr_version;           /* sender's version */
310         __u8             ibr_why;               /* reject reason */
311 } WIRE_ATTR kib_rej_t;
312
313
314 /* connection rejection reasons */
315 #define IBLND_REJECT_CONN_RACE       1          /* You lost connection race */
316 #define IBLND_REJECT_NO_RESOURCES    2          /* Out of memory/conns etc */
317 #define IBLND_REJECT_FATAL           3          /* Anything else */
318
319 /***********************************************************************/
320
321 typedef struct kib_rx                           /* receive message */
322 {
323         struct list_head          rx_list;      /* queue for attention */
324         struct kib_conn          *rx_conn;      /* owning conn */
325         int                       rx_nob;       /* # bytes received (-1 while posted) */
326         enum ib_wc_status         rx_status;    /* completion status */
327         kib_msg_t                *rx_msg;       /* message buffer (host vaddr) */
328         __u64                     rx_msgaddr;   /* message buffer (I/O addr) */
329         DECLARE_PCI_UNMAP_ADDR   (rx_msgunmap); /* for dma_unmap_single() */
330         struct ib_recv_wr         rx_wrq;       /* receive work item... */
331         struct ib_sge             rx_sge;       /* ...and its memory */
332 } kib_rx_t;
333
334 #define IBLND_POSTRX_DONT_POST    0             /* don't post */
335 #define IBLND_POSTRX_NO_CREDIT    1             /* post: no credits */
336 #define IBLND_POSTRX_PEER_CREDIT  2             /* post: give peer back 1 credit */
337 #define IBLND_POSTRX_RSRVD_CREDIT 3             /* post: give myself back 1 reserved credit */
338
339 typedef struct kib_tx                           /* transmit message */
340 {
341         struct list_head          tx_list;      /* queue on idle_txs ibc_tx_queue etc. */
342         struct kib_conn          *tx_conn;      /* owning conn */
343         int                       tx_sending;   /* # tx callbacks outstanding */
344         int                       tx_queued;    /* queued for sending */
345         int                       tx_waiting;   /* waiting for peer */
346         int                       tx_status;    /* LNET completion status */
347         unsigned long             tx_deadline;  /* completion deadline */
348         __u64                     tx_cookie;    /* completion cookie */
349         lnet_msg_t               *tx_lntmsg[2]; /* lnet msgs to finalize on completion */
350         kib_msg_t                *tx_msg;       /* message buffer (host vaddr) */
351         __u64                     tx_msgaddr;   /* message buffer (I/O addr) */
352         DECLARE_PCI_UNMAP_ADDR   (tx_msgunmap); /* for dma_unmap_single() */
353         int                       tx_nwrq;      /* # send work items */
354 #if IBLND_MAP_ON_DEMAND
355         struct ib_send_wr         tx_wrq[2];    /* send work items... */
356         struct ib_sge             tx_sge[2];    /* ...and their memory */
357         kib_rdma_desc_t           tx_rd[1];     /* rdma descriptor */
358         __u64                    *tx_pages;     /* rdma phys page addrs */
359         struct ib_pool_fmr       *tx_fmr;       /* rdma mapping (mapped if != NULL) */
360 #else
361         struct ib_send_wr        *tx_wrq;       /* send work items... */
362         struct ib_sge            *tx_sge;       /* ...and their memory */
363         kib_rdma_desc_t          *tx_rd;        /* rdma descriptor */
364         int                       tx_nfrags;    /* # entries in... */
365         struct scatterlist       *tx_frags;     /* dma_map_sg descriptor */
366         int                       tx_dmadir;    /* dma direction */
367 #endif        
368 } kib_tx_t;
369
370 typedef struct kib_connvars
371 {
372         /* connection-in-progress variables */
373         kib_msg_t                 cv_msg;
374 } kib_connvars_t;
375
376 typedef struct kib_conn
377 {
378         struct kib_peer    *ibc_peer;           /* owning peer */
379         struct list_head    ibc_list;           /* stash on peer's conn list */
380         struct list_head    ibc_sched_list;     /* schedule for attention */
381         __u64               ibc_incarnation;    /* which instance of the peer */
382         atomic_t            ibc_refcount;       /* # users */
383         int                 ibc_state;          /* what's happening */
384         int                 ibc_nsends_posted;  /* # uncompleted sends */
385         int                 ibc_credits;        /* # credits I have */
386         int                 ibc_outstanding_credits; /* # credits to return */
387         int                 ibc_reserved_credits;/* # ACK/DONE msg credits */
388         int                 ibc_comms_error;    /* set on comms error */
389         int                 ibc_nrx:8;          /* receive buffers owned */
390         int                 ibc_scheduled:1;    /* scheduled for attention */
391         int                 ibc_ready:1;        /* CQ callback fired */
392         unsigned long       ibc_last_send;      /* time of last send */
393         struct list_head    ibc_early_rxs;      /* rxs completed before ESTABLISHED */
394         struct list_head    ibc_tx_queue;       /* sends that need a credit */
395         struct list_head    ibc_tx_queue_nocred;/* sends that don't need a credit */
396         struct list_head    ibc_tx_queue_rsrvd; /* sends that need to reserve an ACK/DONE msg */
397         struct list_head    ibc_active_txs;     /* active tx awaiting completion */
398         spinlock_t          ibc_lock;           /* serialise */
399         kib_rx_t           *ibc_rxs;            /* the rx descs */
400         kib_pages_t        *ibc_rx_pages;       /* premapped rx msg pages */
401
402         struct rdma_cm_id  *ibc_cmid;           /* CM id */
403         struct ib_cq       *ibc_cq;             /* completion queue */
404
405         kib_connvars_t     *ibc_connvars;       /* in-progress connection state */
406 } kib_conn_t;
407
408 #define IBLND_CONN_INIT               0         /* being intialised */
409 #define IBLND_CONN_ACTIVE_CONNECT     1         /* active sending req */
410 #define IBLND_CONN_PASSIVE_WAIT       2         /* passive waiting for rtu */
411 #define IBLND_CONN_ESTABLISHED        3         /* connection established */
412 #define IBLND_CONN_CLOSING            4         /* being closed */
413 #define IBLND_CONN_DISCONNECTED       5         /* disconnected */
414
415 typedef struct kib_peer
416 {
417         struct list_head    ibp_list;           /* stash on global peer list */
418         lnet_nid_t          ibp_nid;            /* who's on the other end(s) */
419         lnet_ni_t          *ibp_ni;             /* LNet interface */
420         atomic_t            ibp_refcount;       /* # users */
421         struct list_head    ibp_conns;          /* all active connections */
422         struct list_head    ibp_tx_queue;       /* msgs waiting for a conn */
423         int                 ibp_connecting;     /* current active connection attempts */
424         int                 ibp_accepting;      /* current passive connection attempts */
425         int                 ibp_error;          /* errno on closing this peer */
426         cfs_time_t          ibp_last_alive;     /* when (in jiffies) I was last alive */
427 } kib_peer_t;
428
429
430 extern kib_data_t      kiblnd_data;
431 extern kib_tunables_t  kiblnd_tunables;
432
433 #define kiblnd_conn_addref(conn)                                \
434 do {                                                            \
435         CDEBUG(D_NET, "conn[%p] (%d)++\n",                      \
436                (conn), atomic_read(&(conn)->ibc_refcount));     \
437         LASSERT(atomic_read(&(conn)->ibc_refcount) > 0);        \
438         atomic_inc(&(conn)->ibc_refcount);                      \
439 } while (0)
440
441 #define kiblnd_conn_decref(conn)                                              \
442 do {                                                                          \
443         unsigned long   flags;                                                \
444                                                                               \
445         CDEBUG(D_NET, "conn[%p] (%d)--\n",                                    \
446                (conn), atomic_read(&(conn)->ibc_refcount));                   \
447         LASSERT(atomic_read(&(conn)->ibc_refcount) > 0);                      \
448         if (atomic_dec_and_test(&(conn)->ibc_refcount)) {                     \
449                 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);        \
450                 list_add_tail(&(conn)->ibc_list,                              \
451                               &kiblnd_data.kib_connd_zombies);                \
452                 wake_up(&kiblnd_data.kib_connd_waitq);                        \
453                 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);   \
454         }                                                                     \
455 } while (0)
456
457 #define kiblnd_peer_addref(peer)                                \
458 do {                                                            \
459         CDEBUG(D_NET, "peer[%p] -> %s (%d)++\n",                \
460                (peer), libcfs_nid2str((peer)->ibp_nid),         \
461                atomic_read (&(peer)->ibp_refcount));            \
462         LASSERT(atomic_read(&(peer)->ibp_refcount) > 0);        \
463         atomic_inc(&(peer)->ibp_refcount);                      \
464 } while (0)
465
466 #define kiblnd_peer_decref(peer)                                \
467 do {                                                            \
468         CDEBUG(D_NET, "peer[%p] -> %s (%d)--\n",                \
469                (peer), libcfs_nid2str((peer)->ibp_nid),         \
470                atomic_read (&(peer)->ibp_refcount));            \
471         LASSERT(atomic_read(&(peer)->ibp_refcount) > 0);        \
472         if (atomic_dec_and_test(&(peer)->ibp_refcount))         \
473                 kiblnd_destroy_peer(peer);                      \
474 } while (0)
475
476 static inline struct list_head *
477 kiblnd_nid2peerlist (lnet_nid_t nid)
478 {
479         unsigned int hash = ((unsigned int)nid) % kiblnd_data.kib_peer_hash_size;
480
481         return (&kiblnd_data.kib_peers [hash]);
482 }
483
484 static inline int
485 kiblnd_peer_active (kib_peer_t *peer)
486 {
487         /* Am I in the peer hash table? */
488         return (!list_empty(&peer->ibp_list));
489 }
490
491 static inline kib_conn_t *
492 kiblnd_get_conn_locked (kib_peer_t *peer)
493 {
494         LASSERT (!list_empty(&peer->ibp_conns));
495         
496         /* just return the first connection */
497         return list_entry(peer->ibp_conns.next, kib_conn_t, ibc_list);
498 }
499
500 static inline int
501 kiblnd_send_keepalive(kib_conn_t *conn) 
502 {
503         return (*kiblnd_tunables.kib_keepalive > 0) &&
504                 time_after(jiffies, conn->ibc_last_send +
505                            *kiblnd_tunables.kib_keepalive*HZ);
506 }
507
508 static inline void
509 kiblnd_abort_receives(kib_conn_t *conn)
510 {
511         ib_modify_qp(conn->ibc_cmid->qp,
512                      &kiblnd_data.kib_error_qpa, IB_QP_STATE);
513 }
514
515 /* CAVEAT EMPTOR: We rely on descriptor alignment to allow us to use the
516  * lowest bits of the work request id to stash the work item type. */
517
518 #define IBLND_WID_TX    0
519 #define IBLND_WID_RDMA  1
520 #define IBLND_WID_RX    2
521 #define IBLND_WID_MASK  3UL
522
523 static inline __u64
524 kiblnd_ptr2wreqid (void *ptr, int type)
525 {
526         unsigned long lptr = (unsigned long)ptr;
527
528         LASSERT ((lptr & IBLND_WID_MASK) == 0);
529         LASSERT ((type & ~IBLND_WID_MASK) == 0);
530         return (__u64)(lptr | type);
531 }
532
533 static inline void *
534 kiblnd_wreqid2ptr (__u64 wreqid)
535 {
536         return (void *)(((unsigned long)wreqid) & ~IBLND_WID_MASK);
537 }
538
539 static inline int
540 kiblnd_wreqid2type (__u64 wreqid)
541 {
542         return (wreqid & IBLND_WID_MASK);
543 }
544
545 static inline void
546 kiblnd_set_conn_state (kib_conn_t *conn, int state)
547 {
548         conn->ibc_state = state;
549         mb();
550 }
551
552 #if IBLND_MAP_ON_DEMAND
553 static inline int
554 kiblnd_rd_size (kib_rdma_desc_t *rd)
555 {
556         return rd->rd_nob;
557 }
558 #else
559 static inline int
560 kiblnd_rd_size (kib_rdma_desc_t *rd)
561 {
562         int   i;
563         int   size;
564         
565         for (i = size = 0; i < rd->rd_nfrags; i++)
566                 size += rd->rd_frags[i].rf_nob;
567         
568         return size;
569 }
570 #endif
571
572 int  kiblnd_startup (lnet_ni_t *ni);
573 void kiblnd_shutdown (lnet_ni_t *ni);
574 int  kiblnd_ctl (lnet_ni_t *ni, unsigned int cmd, void *arg);
575
576 int  kiblnd_tunables_init(void);
577 void kiblnd_tunables_fini(void);
578
579 int  kiblnd_connd (void *arg);
580 int  kiblnd_scheduler(void *arg);
581 int  kiblnd_thread_start (int (*fn)(void *arg), void *arg);
582
583 int  kiblnd_alloc_pages (kib_pages_t **pp, int npages);
584 void kiblnd_free_pages (kib_pages_t *p);
585
586 int  kiblnd_cm_callback(struct rdma_cm_id *cmid,
587                         struct rdma_cm_event *event);
588
589 int  kiblnd_create_peer (lnet_ni_t *ni, kib_peer_t **peerp, lnet_nid_t nid);
590 void kiblnd_destroy_peer (kib_peer_t *peer);
591 void kiblnd_destroy_dev (kib_dev_t *dev);
592 void kiblnd_unlink_peer_locked (kib_peer_t *peer);
593 void kiblnd_peer_alive (kib_peer_t *peer);
594 kib_peer_t *kiblnd_find_peer_locked (lnet_nid_t nid);
595 void kiblnd_peer_connect_failed (kib_peer_t *peer, int active, int error);
596 int  kiblnd_close_stale_conns_locked (kib_peer_t *peer, __u64 incarnation);
597
598 void kiblnd_connreq_done(kib_conn_t *conn, int status);
599 kib_conn_t *kiblnd_create_conn (kib_peer_t *peer, struct rdma_cm_id *cmid,
600                                 int state);
601 void kiblnd_destroy_conn (kib_conn_t *conn);
602 void kiblnd_close_conn (kib_conn_t *conn, int error);
603 void kiblnd_close_conn_locked (kib_conn_t *conn, int error);
604
605 int  kiblnd_init_rdma (lnet_ni_t *ni, kib_tx_t *tx, int type,
606                        int nob, kib_rdma_desc_t *dstrd, __u64 dstcookie);
607
608 void kiblnd_queue_tx_locked (kib_tx_t *tx, kib_conn_t *conn);
609 void kiblnd_queue_tx (kib_tx_t *tx, kib_conn_t *conn);
610 void kiblnd_init_tx_msg (lnet_ni_t *ni, kib_tx_t *tx, int type, int body_nob);
611 void kiblnd_txlist_done (lnet_ni_t *ni, struct list_head *txlist, int status);
612 void kiblnd_check_sends (kib_conn_t *conn);
613
614 void kiblnd_qp_event(struct ib_event *event, void *arg);
615 void kiblnd_cq_event(struct ib_event *event, void *arg);
616 void kiblnd_cq_completion(struct ib_cq *cq, void *arg);
617
618 void kiblnd_init_msg (kib_msg_t *msg, int type, int body_nob);
619 void kiblnd_pack_msg (lnet_ni_t *ni, kib_msg_t *msg,
620                       int credits, lnet_nid_t dstnid, __u64 dststamp);
621 int  kiblnd_unpack_msg(kib_msg_t *msg, int nob);
622 int  kiblnd_post_rx (kib_rx_t *rx, int credit);
623
624 int  kiblnd_send(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg);
625 int  kiblnd_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
626                  unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
627                  unsigned int offset, unsigned int mlen, unsigned int rlen);
628
629
630