Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / staging / lustre / include / linux / lnet / lib-lnet.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
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/include/lnet/lib-lnet.h
37  *
38  * Top level include for library side routines
39  */
40
41 #ifndef __LNET_LIB_LNET_H__
42 #define __LNET_LIB_LNET_H__
43
44 #include "linux/lib-lnet.h"
45 #include "../libcfs/libcfs.h"
46 #include "types.h"
47 #include "lnet.h"
48 #include "lib-types.h"
49
50 extern lnet_t  the_lnet;                        /* THE network */
51
52 #if  defined(LNET_USE_LIB_FREELIST)
53 /* 1 CPT, simplify implementation... */
54 # define LNET_CPT_MAX_BITS      0
55
56 #else /* KERNEL and no freelist */
57
58 # if (BITS_PER_LONG == 32)
59 /* 2 CPTs, allowing more CPTs might make us under memory pressure */
60 #  define LNET_CPT_MAX_BITS     1
61
62 # else /* 64-bit system */
63 /*
64  * 256 CPTs for thousands of CPUs, allowing more CPTs might make us
65  * under risk of consuming all lh_cookie.
66  */
67 #  define LNET_CPT_MAX_BITS     8
68 # endif /* BITS_PER_LONG == 32 */
69 #endif
70
71 /* max allowed CPT number */
72 #define LNET_CPT_MAX        (1 << LNET_CPT_MAX_BITS)
73
74 #define LNET_CPT_NUMBER  (the_lnet.ln_cpt_number)
75 #define LNET_CPT_BITS      (the_lnet.ln_cpt_bits)
76 #define LNET_CPT_MASK      ((1ULL << LNET_CPT_BITS) - 1)
77
78 /** exclusive lock */
79 #define LNET_LOCK_EX        CFS_PERCPT_LOCK_EX
80
81 static inline int lnet_is_wire_handle_none(lnet_handle_wire_t *wh)
82 {
83         return (wh->wh_interface_cookie == LNET_WIRE_HANDLE_COOKIE_NONE &&
84                 wh->wh_object_cookie == LNET_WIRE_HANDLE_COOKIE_NONE);
85 }
86
87 static inline int lnet_md_exhausted(lnet_libmd_t *md)
88 {
89         return (md->md_threshold == 0 ||
90                 ((md->md_options & LNET_MD_MAX_SIZE) != 0 &&
91                  md->md_offset + md->md_max_size > md->md_length));
92 }
93
94 static inline int lnet_md_unlinkable(lnet_libmd_t *md)
95 {
96         /* Should unlink md when its refcount is 0 and either:
97          *  - md has been flagged for deletion (by auto unlink or
98          *    LNetM[DE]Unlink, in the latter case md may not be exhausted).
99          *  - auto unlink is on and md is exhausted.
100          */
101         if (md->md_refcount != 0)
102                 return 0;
103
104         if ((md->md_flags & LNET_MD_FLAG_ZOMBIE) != 0)
105                 return 1;
106
107         return ((md->md_flags & LNET_MD_FLAG_AUTO_UNLINK) != 0 &&
108                 lnet_md_exhausted(md));
109 }
110
111 #define lnet_cpt_table()        (the_lnet.ln_cpt_table)
112 #define lnet_cpt_current()      cfs_cpt_current(the_lnet.ln_cpt_table, 1)
113
114 static inline int
115 lnet_cpt_of_cookie(__u64 cookie)
116 {
117         unsigned int cpt = (cookie >> LNET_COOKIE_TYPE_BITS) & LNET_CPT_MASK;
118
119         /* LNET_CPT_NUMBER doesn't have to be power2, which means we can
120          * get illegal cpt from it's invalid cookie */
121         return cpt < LNET_CPT_NUMBER ? cpt : cpt % LNET_CPT_NUMBER;
122 }
123
124 static inline void
125 lnet_res_lock(int cpt)
126 {
127         cfs_percpt_lock(the_lnet.ln_res_lock, cpt);
128 }
129
130 static inline void
131 lnet_res_unlock(int cpt)
132 {
133         cfs_percpt_unlock(the_lnet.ln_res_lock, cpt);
134 }
135
136 static inline int
137 lnet_res_lock_current(void)
138 {
139         int cpt = lnet_cpt_current();
140
141         lnet_res_lock(cpt);
142         return cpt;
143 }
144
145 static inline void
146 lnet_net_lock(int cpt)
147 {
148         cfs_percpt_lock(the_lnet.ln_net_lock, cpt);
149 }
150
151 static inline void
152 lnet_net_unlock(int cpt)
153 {
154         cfs_percpt_unlock(the_lnet.ln_net_lock, cpt);
155 }
156
157 static inline int
158 lnet_net_lock_current(void)
159 {
160         int cpt = lnet_cpt_current();
161
162         lnet_net_lock(cpt);
163         return cpt;
164 }
165
166 #define LNET_LOCK()             lnet_net_lock(LNET_LOCK_EX)
167 #define LNET_UNLOCK()           lnet_net_unlock(LNET_LOCK_EX)
168
169 #define lnet_ptl_lock(ptl)      spin_lock(&(ptl)->ptl_lock)
170 #define lnet_ptl_unlock(ptl)    spin_unlock(&(ptl)->ptl_lock)
171 #define lnet_eq_wait_lock()     spin_lock(&the_lnet.ln_eq_wait_lock)
172 #define lnet_eq_wait_unlock()   spin_unlock(&the_lnet.ln_eq_wait_lock)
173 #define lnet_ni_lock(ni)        spin_lock(&(ni)->ni_lock)
174 #define lnet_ni_unlock(ni)      spin_unlock(&(ni)->ni_lock)
175 #define LNET_MUTEX_LOCK(m)      mutex_lock(m)
176 #define LNET_MUTEX_UNLOCK(m)    mutex_unlock(m)
177
178 #define MAX_PORTALS     64
179
180 /* these are only used by code with LNET_USE_LIB_FREELIST, but we still
181  * exported them to !LNET_USE_LIB_FREELIST for easy implementation */
182 #define LNET_FL_MAX_MES         2048
183 #define LNET_FL_MAX_MDS         2048
184 #define LNET_FL_MAX_EQS         512
185 #define LNET_FL_MAX_MSGS        2048    /* Outstanding messages */
186
187 #ifdef LNET_USE_LIB_FREELIST
188
189 int lnet_freelist_init(lnet_freelist_t *fl, int n, int size);
190 void lnet_freelist_fini(lnet_freelist_t *fl);
191
192 static inline void *
193 lnet_freelist_alloc(lnet_freelist_t *fl)
194 {
195         /* ALWAYS called with liblock held */
196         lnet_freeobj_t *o;
197
198         if (list_empty(&fl->fl_list))
199                 return NULL;
200
201         o = list_entry(fl->fl_list.next, lnet_freeobj_t, fo_list);
202         list_del(&o->fo_list);
203         return (void *)&o->fo_contents;
204 }
205
206 static inline void
207 lnet_freelist_free(lnet_freelist_t *fl, void *obj)
208 {
209         /* ALWAYS called with liblock held */
210         lnet_freeobj_t *o = list_entry(obj, lnet_freeobj_t, fo_contents);
211
212         list_add(&o->fo_list, &fl->fl_list);
213 }
214
215 static inline lnet_eq_t *
216 lnet_eq_alloc(void)
217 {
218         /* NEVER called with resource lock held */
219         struct lnet_res_container *rec = &the_lnet.ln_eq_container;
220         lnet_eq_t                 *eq;
221
222         LASSERT(LNET_CPT_NUMBER == 1);
223
224         lnet_res_lock(0);
225         eq = (lnet_eq_t *)lnet_freelist_alloc(&rec->rec_freelist);
226         lnet_res_unlock(0);
227
228         return eq;
229 }
230
231 static inline void
232 lnet_eq_free_locked(lnet_eq_t *eq)
233 {
234         /* ALWAYS called with resource lock held */
235         struct lnet_res_container *rec = &the_lnet.ln_eq_container;
236
237         LASSERT(LNET_CPT_NUMBER == 1);
238         lnet_freelist_free(&rec->rec_freelist, eq);
239 }
240
241 static inline void
242 lnet_eq_free(lnet_eq_t *eq)
243 {
244         lnet_res_lock(0);
245         lnet_eq_free_locked(eq);
246         lnet_res_unlock(0);
247 }
248
249 static inline lnet_libmd_t *
250 lnet_md_alloc(lnet_md_t *umd)
251 {
252         /* NEVER called with resource lock held */
253         struct lnet_res_container *rec = the_lnet.ln_md_containers[0];
254         lnet_libmd_t              *md;
255
256         LASSERT(LNET_CPT_NUMBER == 1);
257
258         lnet_res_lock(0);
259         md = (lnet_libmd_t *)lnet_freelist_alloc(&rec->rec_freelist);
260         lnet_res_unlock(0);
261
262         if (md != NULL)
263                 INIT_LIST_HEAD(&md->md_list);
264
265         return md;
266 }
267
268 static inline void
269 lnet_md_free_locked(lnet_libmd_t *md)
270 {
271         /* ALWAYS called with resource lock held */
272         struct lnet_res_container *rec = the_lnet.ln_md_containers[0];
273
274         LASSERT(LNET_CPT_NUMBER == 1);
275         lnet_freelist_free(&rec->rec_freelist, md);
276 }
277
278 static inline void
279 lnet_md_free(lnet_libmd_t *md)
280 {
281         lnet_res_lock(0);
282         lnet_md_free_locked(md);
283         lnet_res_unlock(0);
284 }
285
286 static inline lnet_me_t *
287 lnet_me_alloc(void)
288 {
289         /* NEVER called with resource lock held */
290         struct lnet_res_container *rec = the_lnet.ln_me_containers[0];
291         lnet_me_t                 *me;
292
293         LASSERT(LNET_CPT_NUMBER == 1);
294
295         lnet_res_lock(0);
296         me = (lnet_me_t *)lnet_freelist_alloc(&rec->rec_freelist);
297         lnet_res_unlock(0);
298
299         return me;
300 }
301
302 static inline void
303 lnet_me_free_locked(lnet_me_t *me)
304 {
305         /* ALWAYS called with resource lock held */
306         struct lnet_res_container *rec = the_lnet.ln_me_containers[0];
307
308         LASSERT(LNET_CPT_NUMBER == 1);
309         lnet_freelist_free(&rec->rec_freelist, me);
310 }
311
312 static inline void
313 lnet_me_free(lnet_me_t *me)
314 {
315         lnet_res_lock(0);
316         lnet_me_free_locked(me);
317         lnet_res_unlock(0);
318 }
319
320 static inline lnet_msg_t *
321 lnet_msg_alloc(void)
322 {
323         /* NEVER called with network lock held */
324         struct lnet_msg_container *msc = the_lnet.ln_msg_containers[0];
325         lnet_msg_t                *msg;
326
327         LASSERT(LNET_CPT_NUMBER == 1);
328
329         lnet_net_lock(0);
330         msg = (lnet_msg_t *)lnet_freelist_alloc(&msc->msc_freelist);
331         lnet_net_unlock(0);
332
333         if (msg != NULL) {
334                 /* NULL pointers, clear flags etc */
335                 memset(msg, 0, sizeof(*msg));
336         }
337         return msg;
338 }
339
340 static inline void
341 lnet_msg_free_locked(lnet_msg_t *msg)
342 {
343         /* ALWAYS called with network lock held */
344         struct lnet_msg_container *msc = the_lnet.ln_msg_containers[0];
345
346         LASSERT(LNET_CPT_NUMBER == 1);
347         LASSERT(!msg->msg_onactivelist);
348         lnet_freelist_free(&msc->msc_freelist, msg);
349 }
350
351 static inline void
352 lnet_msg_free(lnet_msg_t *msg)
353 {
354         lnet_net_lock(0);
355         lnet_msg_free_locked(msg);
356         lnet_net_unlock(0);
357 }
358
359 #else /* !LNET_USE_LIB_FREELIST */
360
361 static inline lnet_eq_t *
362 lnet_eq_alloc(void)
363 {
364         /* NEVER called with liblock held */
365         lnet_eq_t *eq;
366
367         LIBCFS_ALLOC(eq, sizeof(*eq));
368         return eq;
369 }
370
371 static inline void
372 lnet_eq_free(lnet_eq_t *eq)
373 {
374         /* ALWAYS called with resource lock held */
375         LIBCFS_FREE(eq, sizeof(*eq));
376 }
377
378 static inline lnet_libmd_t *
379 lnet_md_alloc(lnet_md_t *umd)
380 {
381         /* NEVER called with liblock held */
382         lnet_libmd_t *md;
383         unsigned int  size;
384         unsigned int  niov;
385
386         if ((umd->options & LNET_MD_KIOV) != 0) {
387                 niov = umd->length;
388                 size = offsetof(lnet_libmd_t, md_iov.kiov[niov]);
389         } else {
390                 niov = ((umd->options & LNET_MD_IOVEC) != 0) ?
391                        umd->length : 1;
392                 size = offsetof(lnet_libmd_t, md_iov.iov[niov]);
393         }
394
395         LIBCFS_ALLOC(md, size);
396
397         if (md != NULL) {
398                 /* Set here in case of early free */
399                 md->md_options = umd->options;
400                 md->md_niov = niov;
401                 INIT_LIST_HEAD(&md->md_list);
402         }
403
404         return md;
405 }
406
407 static inline void
408 lnet_md_free(lnet_libmd_t *md)
409 {
410         /* ALWAYS called with resource lock held */
411         unsigned int  size;
412
413         if ((md->md_options & LNET_MD_KIOV) != 0)
414                 size = offsetof(lnet_libmd_t, md_iov.kiov[md->md_niov]);
415         else
416                 size = offsetof(lnet_libmd_t, md_iov.iov[md->md_niov]);
417
418         LIBCFS_FREE(md, size);
419 }
420
421 static inline lnet_me_t *
422 lnet_me_alloc(void)
423 {
424         /* NEVER called with liblock held */
425         lnet_me_t *me;
426
427         LIBCFS_ALLOC(me, sizeof(*me));
428         return me;
429 }
430
431 static inline void
432 lnet_me_free(lnet_me_t *me)
433 {
434         /* ALWAYS called with resource lock held */
435         LIBCFS_FREE(me, sizeof(*me));
436 }
437
438 static inline lnet_msg_t *
439 lnet_msg_alloc(void)
440 {
441         /* NEVER called with liblock held */
442         lnet_msg_t *msg;
443
444         LIBCFS_ALLOC(msg, sizeof(*msg));
445
446         /* no need to zero, LIBCFS_ALLOC does for us */
447         return msg;
448 }
449
450 static inline void
451 lnet_msg_free(lnet_msg_t *msg)
452 {
453         /* ALWAYS called with network lock held */
454         LASSERT(!msg->msg_onactivelist);
455         LIBCFS_FREE(msg, sizeof(*msg));
456 }
457
458 #define lnet_eq_free_locked(eq)         lnet_eq_free(eq)
459 #define lnet_md_free_locked(md)         lnet_md_free(md)
460 #define lnet_me_free_locked(me)         lnet_me_free(me)
461 #define lnet_msg_free_locked(msg)       lnet_msg_free(msg)
462
463 #endif /* LNET_USE_LIB_FREELIST */
464
465 lnet_libhandle_t *lnet_res_lh_lookup(struct lnet_res_container *rec,
466                                      __u64 cookie);
467 void lnet_res_lh_initialize(struct lnet_res_container *rec,
468                             lnet_libhandle_t *lh);
469 static inline void
470 lnet_res_lh_invalidate(lnet_libhandle_t *lh)
471 {
472         /* ALWAYS called with resource lock held */
473         /* NB: cookie is still useful, don't reset it */
474         list_del(&lh->lh_hash_chain);
475 }
476
477 static inline void
478 lnet_eq2handle(lnet_handle_eq_t *handle, lnet_eq_t *eq)
479 {
480         if (eq == NULL) {
481                 LNetInvalidateHandle(handle);
482                 return;
483         }
484
485         handle->cookie = eq->eq_lh.lh_cookie;
486 }
487
488 static inline lnet_eq_t *
489 lnet_handle2eq(lnet_handle_eq_t *handle)
490 {
491         /* ALWAYS called with resource lock held */
492         lnet_libhandle_t *lh;
493
494         lh = lnet_res_lh_lookup(&the_lnet.ln_eq_container, handle->cookie);
495         if (lh == NULL)
496                 return NULL;
497
498         return lh_entry(lh, lnet_eq_t, eq_lh);
499 }
500
501 static inline void
502 lnet_md2handle(lnet_handle_md_t *handle, lnet_libmd_t *md)
503 {
504         handle->cookie = md->md_lh.lh_cookie;
505 }
506
507 static inline lnet_libmd_t *
508 lnet_handle2md(lnet_handle_md_t *handle)
509 {
510         /* ALWAYS called with resource lock held */
511         lnet_libhandle_t *lh;
512         int              cpt;
513
514         cpt = lnet_cpt_of_cookie(handle->cookie);
515         lh = lnet_res_lh_lookup(the_lnet.ln_md_containers[cpt],
516                                 handle->cookie);
517         if (lh == NULL)
518                 return NULL;
519
520         return lh_entry(lh, lnet_libmd_t, md_lh);
521 }
522
523 static inline lnet_libmd_t *
524 lnet_wire_handle2md(lnet_handle_wire_t *wh)
525 {
526         /* ALWAYS called with resource lock held */
527         lnet_libhandle_t *lh;
528         int              cpt;
529
530         if (wh->wh_interface_cookie != the_lnet.ln_interface_cookie)
531                 return NULL;
532
533         cpt = lnet_cpt_of_cookie(wh->wh_object_cookie);
534         lh = lnet_res_lh_lookup(the_lnet.ln_md_containers[cpt],
535                                 wh->wh_object_cookie);
536         if (lh == NULL)
537                 return NULL;
538
539         return lh_entry(lh, lnet_libmd_t, md_lh);
540 }
541
542 static inline void
543 lnet_me2handle(lnet_handle_me_t *handle, lnet_me_t *me)
544 {
545         handle->cookie = me->me_lh.lh_cookie;
546 }
547
548 static inline lnet_me_t *
549 lnet_handle2me(lnet_handle_me_t *handle)
550 {
551         /* ALWAYS called with resource lock held */
552         lnet_libhandle_t *lh;
553         int              cpt;
554
555         cpt = lnet_cpt_of_cookie(handle->cookie);
556         lh = lnet_res_lh_lookup(the_lnet.ln_me_containers[cpt],
557                                 handle->cookie);
558         if (lh == NULL)
559                 return NULL;
560
561         return lh_entry(lh, lnet_me_t, me_lh);
562 }
563
564 static inline void
565 lnet_peer_addref_locked(lnet_peer_t *lp)
566 {
567         LASSERT(lp->lp_refcount > 0);
568         lp->lp_refcount++;
569 }
570
571 void lnet_destroy_peer_locked(lnet_peer_t *lp);
572
573 static inline void
574 lnet_peer_decref_locked(lnet_peer_t *lp)
575 {
576         LASSERT(lp->lp_refcount > 0);
577         lp->lp_refcount--;
578         if (lp->lp_refcount == 0)
579                 lnet_destroy_peer_locked(lp);
580 }
581
582 static inline int
583 lnet_isrouter(lnet_peer_t *lp)
584 {
585         return lp->lp_rtr_refcount != 0;
586 }
587
588 static inline void
589 lnet_ni_addref_locked(lnet_ni_t *ni, int cpt)
590 {
591         LASSERT(cpt >= 0 && cpt < LNET_CPT_NUMBER);
592         LASSERT(*ni->ni_refs[cpt] >= 0);
593
594         (*ni->ni_refs[cpt])++;
595 }
596
597 static inline void
598 lnet_ni_addref(lnet_ni_t *ni)
599 {
600         lnet_net_lock(0);
601         lnet_ni_addref_locked(ni, 0);
602         lnet_net_unlock(0);
603 }
604
605 static inline void
606 lnet_ni_decref_locked(lnet_ni_t *ni, int cpt)
607 {
608         LASSERT(cpt >= 0 && cpt < LNET_CPT_NUMBER);
609         LASSERT(*ni->ni_refs[cpt] > 0);
610
611         (*ni->ni_refs[cpt])--;
612 }
613
614 static inline void
615 lnet_ni_decref(lnet_ni_t *ni)
616 {
617         lnet_net_lock(0);
618         lnet_ni_decref_locked(ni, 0);
619         lnet_net_unlock(0);
620 }
621
622 void lnet_ni_free(lnet_ni_t *ni);
623
624 static inline int
625 lnet_nid2peerhash(lnet_nid_t nid)
626 {
627         return hash_long(nid, LNET_PEER_HASH_BITS);
628 }
629
630 static inline struct list_head *
631 lnet_net2rnethash(__u32 net)
632 {
633         return &the_lnet.ln_remote_nets_hash[(LNET_NETNUM(net) +
634                 LNET_NETTYP(net)) &
635                 ((1U << the_lnet.ln_remote_nets_hbits) - 1)];
636 }
637
638 extern lnd_t the_lolnd;
639 extern int avoid_asym_router_failure;
640
641 int lnet_cpt_of_nid_locked(lnet_nid_t nid);
642 int lnet_cpt_of_nid(lnet_nid_t nid);
643 lnet_ni_t *lnet_nid2ni_locked(lnet_nid_t nid, int cpt);
644 lnet_ni_t *lnet_net2ni_locked(__u32 net, int cpt);
645 lnet_ni_t *lnet_net2ni(__u32 net);
646
647 int lnet_notify(lnet_ni_t *ni, lnet_nid_t peer, int alive, unsigned long when);
648 void lnet_notify_locked(lnet_peer_t *lp, int notifylnd, int alive,
649                         unsigned long when);
650 int lnet_add_route(__u32 net, unsigned int hops, lnet_nid_t gateway_nid,
651                    unsigned int priority);
652 int lnet_check_routes(void);
653 int lnet_del_route(__u32 net, lnet_nid_t gw_nid);
654 void lnet_destroy_routes(void);
655 int lnet_get_route(int idx, __u32 *net, __u32 *hops,
656                    lnet_nid_t *gateway, __u32 *alive, __u32 *priority);
657 void lnet_proc_init(void);
658 void lnet_proc_fini(void);
659 int  lnet_rtrpools_alloc(int im_a_router);
660 void lnet_rtrpools_free(void);
661 lnet_remotenet_t *lnet_find_net_locked(__u32 net);
662
663 int lnet_islocalnid(lnet_nid_t nid);
664 int lnet_islocalnet(__u32 net);
665
666 void lnet_msg_attach_md(lnet_msg_t *msg, lnet_libmd_t *md,
667                         unsigned int offset, unsigned int mlen);
668 void lnet_msg_detach_md(lnet_msg_t *msg, int status);
669 void lnet_build_unlink_event(lnet_libmd_t *md, lnet_event_t *ev);
670 void lnet_build_msg_event(lnet_msg_t *msg, lnet_event_kind_t ev_type);
671 void lnet_msg_commit(lnet_msg_t *msg, int cpt);
672 void lnet_msg_decommit(lnet_msg_t *msg, int cpt, int status);
673
674 void lnet_eq_enqueue_event(lnet_eq_t *eq, lnet_event_t *ev);
675 void lnet_prep_send(lnet_msg_t *msg, int type, lnet_process_id_t target,
676                     unsigned int offset, unsigned int len);
677 int lnet_send(lnet_nid_t nid, lnet_msg_t *msg, lnet_nid_t rtr_nid);
678 void lnet_return_tx_credits_locked(lnet_msg_t *msg);
679 void lnet_return_rx_credits_locked(lnet_msg_t *msg);
680
681 /* portals functions */
682 /* portals attributes */
683 static inline int
684 lnet_ptl_is_lazy(lnet_portal_t *ptl)
685 {
686         return !!(ptl->ptl_options & LNET_PTL_LAZY);
687 }
688
689 static inline int
690 lnet_ptl_is_unique(lnet_portal_t *ptl)
691 {
692         return !!(ptl->ptl_options & LNET_PTL_MATCH_UNIQUE);
693 }
694
695 static inline int
696 lnet_ptl_is_wildcard(lnet_portal_t *ptl)
697 {
698         return !!(ptl->ptl_options & LNET_PTL_MATCH_WILDCARD);
699 }
700
701 static inline void
702 lnet_ptl_setopt(lnet_portal_t *ptl, int opt)
703 {
704         ptl->ptl_options |= opt;
705 }
706
707 static inline void
708 lnet_ptl_unsetopt(lnet_portal_t *ptl, int opt)
709 {
710         ptl->ptl_options &= ~opt;
711 }
712
713 /* match-table functions */
714 struct list_head *lnet_mt_match_head(struct lnet_match_table *mtable,
715                                      lnet_process_id_t id, __u64 mbits);
716 struct lnet_match_table *lnet_mt_of_attach(unsigned int index,
717                                            lnet_process_id_t id, __u64 mbits,
718                                            __u64 ignore_bits,
719                                            lnet_ins_pos_t pos);
720 int lnet_mt_match_md(struct lnet_match_table *mtable,
721                      struct lnet_match_info *info, struct lnet_msg *msg);
722
723 /* portals match/attach functions */
724 void lnet_ptl_attach_md(lnet_me_t *me, lnet_libmd_t *md,
725                         struct list_head *matches, struct list_head *drops);
726 void lnet_ptl_detach_md(lnet_me_t *me, lnet_libmd_t *md);
727 int lnet_ptl_match_md(struct lnet_match_info *info, struct lnet_msg *msg);
728
729 /* initialized and finalize portals */
730 int lnet_portals_create(void);
731 void lnet_portals_destroy(void);
732
733 /* message functions */
734 int lnet_parse(lnet_ni_t *ni, lnet_hdr_t *hdr,
735                lnet_nid_t fromnid, void *private, int rdma_req);
736 void lnet_recv(lnet_ni_t *ni, void *private, lnet_msg_t *msg, int delayed,
737                unsigned int offset, unsigned int mlen, unsigned int rlen);
738 lnet_msg_t *lnet_create_reply_msg(lnet_ni_t *ni, lnet_msg_t *get_msg);
739 void lnet_set_reply_msg_len(lnet_ni_t *ni, lnet_msg_t *msg, unsigned int len);
740 void lnet_finalize(lnet_ni_t *ni, lnet_msg_t *msg, int rc);
741 void lnet_drop_delayed_msg_list(struct list_head *head, char *reason);
742 void lnet_recv_delayed_msg_list(struct list_head *head);
743
744 int lnet_msg_container_setup(struct lnet_msg_container *container, int cpt);
745 void lnet_msg_container_cleanup(struct lnet_msg_container *container);
746 void lnet_msg_containers_destroy(void);
747 int lnet_msg_containers_create(void);
748
749 char *lnet_msgtyp2str(int type);
750 void lnet_print_hdr(lnet_hdr_t *hdr);
751 int lnet_fail_nid(lnet_nid_t nid, unsigned int threshold);
752
753 void lnet_counters_get(lnet_counters_t *counters);
754 void lnet_counters_reset(void);
755
756 unsigned int lnet_iov_nob(unsigned int niov, struct kvec *iov);
757 int lnet_extract_iov(int dst_niov, struct kvec *dst,
758                      int src_niov, struct kvec *src,
759                       unsigned int offset, unsigned int len);
760
761 unsigned int lnet_kiov_nob(unsigned int niov, lnet_kiov_t *iov);
762 int lnet_extract_kiov(int dst_niov, lnet_kiov_t *dst,
763                       int src_niov, lnet_kiov_t *src,
764                       unsigned int offset, unsigned int len);
765
766 void lnet_copy_iov2iov(unsigned int ndiov, struct kvec *diov,
767                        unsigned int doffset,
768                         unsigned int nsiov, struct kvec *siov,
769                         unsigned int soffset, unsigned int nob);
770 void lnet_copy_kiov2iov(unsigned int niov, struct kvec *iov,
771                         unsigned int iovoffset,
772                          unsigned int nkiov, lnet_kiov_t *kiov,
773                          unsigned int kiovoffset, unsigned int nob);
774 void lnet_copy_iov2kiov(unsigned int nkiov, lnet_kiov_t *kiov,
775                         unsigned int kiovoffset,
776                          unsigned int niov, struct kvec *iov,
777                          unsigned int iovoffset, unsigned int nob);
778 void lnet_copy_kiov2kiov(unsigned int ndkiov, lnet_kiov_t *dkiov,
779                          unsigned int doffset,
780                           unsigned int nskiov, lnet_kiov_t *skiov,
781                           unsigned int soffset, unsigned int nob);
782
783 static inline void
784 lnet_copy_iov2flat(int dlen, void *dest, unsigned int doffset,
785                    unsigned int nsiov, struct kvec *siov, unsigned int soffset,
786                    unsigned int nob)
787 {
788         struct kvec diov = {/*.iov_base = */ dest, /*.iov_len = */ dlen};
789
790         lnet_copy_iov2iov(1, &diov, doffset,
791                           nsiov, siov, soffset, nob);
792 }
793
794 static inline void
795 lnet_copy_kiov2flat(int dlen, void *dest, unsigned int doffset,
796                     unsigned int nsiov, lnet_kiov_t *skiov,
797                     unsigned int soffset, unsigned int nob)
798 {
799         struct kvec diov = {/* .iov_base = */ dest, /* .iov_len = */ dlen};
800
801         lnet_copy_kiov2iov(1, &diov, doffset,
802                            nsiov, skiov, soffset, nob);
803 }
804
805 static inline void
806 lnet_copy_flat2iov(unsigned int ndiov, struct kvec *diov, unsigned int doffset,
807                    int slen, void *src, unsigned int soffset, unsigned int nob)
808 {
809         struct kvec siov = {/*.iov_base = */ src, /*.iov_len = */slen};
810
811         lnet_copy_iov2iov(ndiov, diov, doffset,
812                           1, &siov, soffset, nob);
813 }
814
815 static inline void
816 lnet_copy_flat2kiov(unsigned int ndiov, lnet_kiov_t *dkiov,
817                     unsigned int doffset, int slen, void *src,
818                     unsigned int soffset, unsigned int nob)
819 {
820         struct kvec siov = {/* .iov_base = */ src, /* .iov_len = */ slen};
821
822         lnet_copy_iov2kiov(ndiov, dkiov, doffset,
823                            1, &siov, soffset, nob);
824 }
825
826 void lnet_me_unlink(lnet_me_t *me);
827
828 void lnet_md_unlink(lnet_libmd_t *md);
829 void lnet_md_deconstruct(lnet_libmd_t *lmd, lnet_md_t *umd);
830
831 void lnet_register_lnd(lnd_t *lnd);
832 void lnet_unregister_lnd(lnd_t *lnd);
833 int lnet_set_ip_niaddr(lnet_ni_t *ni);
834
835 int lnet_connect(struct socket **sockp, lnet_nid_t peer_nid,
836                  __u32 local_ip, __u32 peer_ip, int peer_port);
837 void lnet_connect_console_error(int rc, lnet_nid_t peer_nid,
838                                 __u32 peer_ip, int port);
839 int lnet_count_acceptor_nis(void);
840 int lnet_acceptor_timeout(void);
841 int lnet_acceptor_port(void);
842
843 int lnet_count_acceptor_nis(void);
844 int lnet_acceptor_port(void);
845
846 int lnet_acceptor_start(void);
847 void lnet_acceptor_stop(void);
848
849 void lnet_get_tunables(void);
850 int lnet_peers_start_down(void);
851 int lnet_peer_buffer_credits(lnet_ni_t *ni);
852
853 int lnet_router_checker_start(void);
854 void lnet_router_checker_stop(void);
855 void lnet_router_ni_update_locked(lnet_peer_t *gw, __u32 net);
856 void lnet_swap_pinginfo(lnet_ping_info_t *info);
857
858 int lnet_ping_target_init(void);
859 void lnet_ping_target_fini(void);
860 int lnet_ping(lnet_process_id_t id, int timeout_ms,
861               lnet_process_id_t *ids, int n_ids);
862
863 int lnet_parse_ip2nets(char **networksp, char *ip2nets);
864 int lnet_parse_routes(char *route_str, int *im_a_router);
865 int lnet_parse_networks(struct list_head *nilist, char *networks);
866
867 int lnet_nid2peer_locked(lnet_peer_t **lpp, lnet_nid_t nid, int cpt);
868 lnet_peer_t *lnet_find_peer_locked(struct lnet_peer_table *ptable,
869                                    lnet_nid_t nid);
870 void lnet_peer_tables_cleanup(void);
871 void lnet_peer_tables_destroy(void);
872 int lnet_peer_tables_create(void);
873 void lnet_debug_peer(lnet_nid_t nid);
874
875 static inline void lnet_peer_set_alive(lnet_peer_t *lp)
876 {
877         lp->lp_last_alive = lp->lp_last_query = get_seconds();
878         if (!lp->lp_alive)
879                 lnet_notify_locked(lp, 0, 1, lp->lp_last_alive);
880 }
881
882
883 #endif