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