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