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