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