Whamcloud - gitweb
i=maxim,b=18460,b=20171:
[fs/lustre-release.git] / lnet / klnds / ptllnd / ptllnd.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/ptllnd/ptllnd.h
37  *
38  * Author: PJ Kirner <pjkirner@clusterfs.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
70 #include <net/sock.h>
71 #include <linux/in.h>
72
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 #include <portals/p30.h>
81 #ifdef CRAY_XT3
82 #include <portals/ptltrace.h>
83 #endif
84 #include <lnet/ptllnd.h>        /* Depends on portals/p30.h */
85
86 /*
87  * Define this to enable console debug logging
88  * and simulation
89  */
90 //#define PJK_DEBUGGING
91
92 #ifdef CONFIG_SMP
93 # define PTLLND_N_SCHED         num_online_cpus()   /* # schedulers */
94 #else
95 # define PTLLND_N_SCHED         1                   /* # schedulers */
96 #endif
97
98 #define PTLLND_CREDIT_HIGHWATER ((*kptllnd_tunables.kptl_peertxcredits)-1)
99   /* when eagerly to return credits */
100
101 typedef struct
102 {
103         int             *kptl_ntx;              /* # tx descs to pre-allocate */
104         int             *kptl_max_nodes;        /* max # nodes all talking to me */
105         int             *kptl_max_procs_per_node; /* max # processes per node */
106         int             *kptl_checksum;         /* checksum kptl_msg_t? */
107         int             *kptl_timeout;          /* comms timeout (seconds) */
108         int             *kptl_portal;           /* portal number */
109         int             *kptl_pid;              /* portals PID (self + kernel peers) */
110         int             *kptl_rxb_npages;       /* number of pages for rx buffer */
111         int             *kptl_rxb_nspare;       /* number of spare rx buffers */
112         int             *kptl_credits;          /* number of credits */
113         int             *kptl_peertxcredits;    /* number of peer tx credits */
114         int             *kptl_peerrtrcredits;   /* number of peer router credits */
115         int             *kptl_max_msg_size;     /* max immd message size*/
116         int             *kptl_peer_hash_table_size; /* # slots in peer hash table */
117         int             *kptl_reschedule_loops; /* scheduler yield loops */
118         int             *kptl_ack_puts;         /* make portals ack PUTs */
119 #ifdef CRAY_XT3
120         int             *kptl_ptltrace_on_timeout; /* dump pltrace on timeout? */
121         int             *kptl_ptltrace_on_fail;    /* dump pltrace on PTL_NAL_FAILED? */
122         char           **kptl_ptltrace_basename;  /* ptltrace dump file basename */
123 #endif
124 #ifdef PJK_DEBUGGING
125         int             *kptl_simulation_bitmap;/* simulation bitmap */
126 #endif
127
128 #if defined(CONFIG_SYSCTL) && !CFS_SYSFS_MODULE_PARM
129         cfs_sysctl_table_header_t *kptl_sysctl; /* sysctl interface */
130 #endif
131 } kptl_tunables_t;
132
133 #include "lnet/ptllnd_wire.h"
134
135 /***********************************************************************/
136
137 typedef struct kptl_data kptl_data_t;
138 typedef struct kptl_net kptl_net_t;
139 typedef struct kptl_rx_buffer kptl_rx_buffer_t;
140 typedef struct kptl_peer kptl_peer_t;
141
142 typedef struct {
143         char      eva_type;
144 } kptl_eventarg_t;
145
146 #define PTLLND_EVENTARG_TYPE_MSG    0x1
147 #define PTLLND_EVENTARG_TYPE_RDMA   0x2
148 #define PTLLND_EVENTARG_TYPE_BUF    0x3
149
150 typedef struct kptl_rx                          /* receive message */
151 {
152         struct list_head        rx_list;        /* queue for attention */
153         kptl_rx_buffer_t       *rx_rxb;         /* the rx buffer pointer */
154         kptl_msg_t             *rx_msg;         /* received message */
155         int                     rx_nob;         /* received message size */
156         unsigned long           rx_treceived;   /* time received */
157         ptl_process_id_t        rx_initiator;   /* sender's address */
158 #ifdef CRAY_XT3
159         ptl_uid_t               rx_uid;         /* sender's uid */
160 #endif
161         kptl_peer_t            *rx_peer;        /* pointer to peer */
162         char                    rx_space[0];    /* copy of incoming request */
163 } kptl_rx_t;
164
165 #define PTLLND_POSTRX_DONT_POST    0            /* don't post */
166 #define PTLLND_POSTRX_NO_CREDIT    1            /* post: no credits */
167 #define PTLLND_POSTRX_PEER_CREDIT  2            /* post: give peer back 1 credit */
168
169 typedef struct kptl_rx_buffer_pool
170 {
171         spinlock_t              rxbp_lock;
172         struct list_head        rxbp_list;      /* all allocated buffers */
173         int                     rxbp_count;     /* # allocated buffers */
174         int                     rxbp_reserved;  /* # requests to buffer */
175         int                     rxbp_shutdown;  /* shutdown flag */
176 } kptl_rx_buffer_pool_t;
177
178 struct kptl_rx_buffer
179 {
180         kptl_rx_buffer_pool_t  *rxb_pool;
181         struct list_head        rxb_list;       /* for the rxb_pool list */
182         struct list_head        rxb_repost_list;/* for the kptl_sched_rxbq list */
183         int                     rxb_posted:1;   /* on the net */
184         int                     rxb_idle:1;     /* all done */
185         kptl_eventarg_t         rxb_eventarg;   /* event->md.user_ptr */
186         int                     rxb_refcount;   /* reference count */
187         ptl_handle_md_t         rxb_mdh;        /* the portals memory descriptor (MD) handle */
188         char                   *rxb_buffer;     /* the buffer */
189
190 };
191
192 enum kptl_tx_type
193 {
194         TX_TYPE_RESERVED                = 0,
195         TX_TYPE_SMALL_MESSAGE           = 1,
196         TX_TYPE_PUT_REQUEST             = 2,
197         TX_TYPE_GET_REQUEST             = 3,
198         TX_TYPE_PUT_RESPONSE            = 4,
199         TX_TYPE_GET_RESPONSE            = 5,
200 };
201
202 typedef union {
203 #ifdef _USING_LUSTRE_PORTALS_
204         struct iovec iov[PTL_MD_MAX_IOV];
205         lnet_kiov_t kiov[PTL_MD_MAX_IOV];
206 #else
207         ptl_md_iovec_t iov[PTL_MD_MAX_IOV];
208 #endif
209 } kptl_fragvec_t;
210
211 typedef struct kptl_tx                           /* transmit message */
212 {
213         struct list_head        tx_list;      /* queue on idle_txs etc */
214         atomic_t                tx_refcount;  /* reference count*/
215         enum kptl_tx_type       tx_type;      /* small msg/{put,get}{req,resp} */
216         int                     tx_active:1;  /* queued on the peer */
217         int                     tx_idle:1;    /* on the free list */
218         int                     tx_acked:1;   /* portals ACK wanted (for debug only) */
219         kptl_eventarg_t         tx_msg_eventarg; /* event->md.user_ptr */
220         kptl_eventarg_t         tx_rdma_eventarg; /* event->md.user_ptr */
221         int                     tx_status;    /* the status of this tx descriptor */
222         ptl_handle_md_t         tx_rdma_mdh;  /* RDMA buffer */
223         ptl_handle_md_t         tx_msg_mdh;   /* the portals MD handle for the initial message */
224         lnet_msg_t             *tx_lnet_msg;  /* LNET message to finalize */
225         lnet_msg_t             *tx_lnet_replymsg; /* LNET reply message to finalize */
226         kptl_msg_t             *tx_msg;       /* the message data */
227         kptl_peer_t            *tx_peer;      /* the peer this is waiting on */
228         unsigned long           tx_deadline;  /* deadline */
229         unsigned long           tx_tposted;   /* time posted */
230         ptl_md_t                tx_rdma_md;   /* rdma descriptor */
231         kptl_fragvec_t         *tx_frags;     /* buffer fragments */
232 } kptl_tx_t;
233
234 enum kptllnd_peer_state
235 {
236         PEER_STATE_UNINITIALIZED        = 0,
237         PEER_STATE_ALLOCATED            = 1,
238         PEER_STATE_WAITING_HELLO        = 2,
239         PEER_STATE_ACTIVE               = 3,
240         PEER_STATE_CLOSING              = 4,
241         PEER_STATE_ZOMBIE               = 5,
242 };
243
244 struct kptl_peer
245 {
246         struct list_head        peer_list;
247         atomic_t                peer_refcount;          /* The current references */
248         enum kptllnd_peer_state peer_state;
249         spinlock_t              peer_lock;              /* serialize */
250         struct list_head        peer_noops;             /* PTLLND_MSG_TYPE_NOOP txs */
251         struct list_head        peer_sendq;             /* txs waiting for mh handles */
252         struct list_head        peer_activeq;           /* txs awaiting completion */
253         lnet_process_id_t       peer_id;                /* Peer's LNET id */
254         ptl_process_id_t        peer_ptlid;             /* Peer's portals id */
255         __u64                   peer_incarnation;       /* peer's incarnation */
256         __u64                   peer_myincarnation;     /* my incarnation at HELLO */
257         int                     peer_sent_hello;        /* have I sent HELLO? */
258         int                     peer_credits;           /* number of send credits */
259         int                     peer_outstanding_credits;/* number of peer credits to return */
260         int                     peer_sent_credits;      /* #msg buffers posted for peer */
261         int                     peer_max_msg_size;      /* peer's rx buffer size */
262         int                     peer_error;             /* errno on closing this peer */
263         int                     peer_retry_noop;        /* need to retry returning credits */
264         int                     peer_check_stamp;       /* watchdog check stamp */
265         cfs_time_t              peer_last_alive;        /* when (in jiffies) I was last alive */
266         __u64                   peer_next_matchbits;    /* Next value to register RDMA from peer */
267         __u64                   peer_last_matchbits_seen; /* last matchbits used to RDMA to peer */
268 };
269
270 struct kptl_data
271 {
272         int                     kptl_init;             /* initialisation state */
273         volatile int            kptl_shutdown;         /* shut down? */
274         atomic_t                kptl_nthreads;         /* # live threads */
275         ptl_handle_ni_t         kptl_nih;              /* network inteface handle */
276         ptl_process_id_t        kptl_portals_id;       /* Portals ID of interface */
277         __u64                   kptl_incarnation;      /* which one am I */
278         ptl_handle_eq_t         kptl_eqh;              /* Event Queue (EQ) */
279
280         rwlock_t                kptl_net_rw_lock;      /* serialise... */
281         struct list_head        kptl_nets;             /* kptl_net instances */
282
283         spinlock_t              kptl_sched_lock;       /* serialise... */
284         wait_queue_head_t       kptl_sched_waitq;      /* schedulers sleep here */
285         struct list_head        kptl_sched_txq;        /* tx requiring attention */
286         struct list_head        kptl_sched_rxq;        /* rx requiring attention */
287         struct list_head        kptl_sched_rxbq;       /* rxb requiring reposting */
288
289         wait_queue_head_t       kptl_watchdog_waitq;   /* watchdog sleeps here */
290         atomic_t                kptl_needs_ptltrace;   /* watchdog thread to dump ptltrace */
291
292         kptl_rx_buffer_pool_t   kptl_rx_buffer_pool;   /* rx buffer pool */
293         cfs_mem_cache_t*        kptl_rx_cache;         /* rx descripter cache */
294
295         atomic_t                kptl_ntx;              /* # tx descs allocated */
296         spinlock_t              kptl_tx_lock;          /* serialise idle tx list*/
297         struct list_head        kptl_idle_txs;         /* idle tx descriptors */
298
299         rwlock_t                kptl_peer_rw_lock;     /* lock for peer table */
300         struct list_head       *kptl_peers;            /* hash table of all my known peers */
301         struct list_head        kptl_closing_peers;    /* peers being closed */
302         struct list_head        kptl_zombie_peers;     /* peers waiting for refs to drain */
303         int                     kptl_peer_hash_size;   /* size of kptl_peers */
304         int                     kptl_npeers;           /* # peers extant */
305         int                     kptl_n_active_peers;   /* # active peers */
306         int                     kptl_expected_peers;   /* # peers I can buffer HELLOs from */
307
308         kptl_msg_t             *kptl_nak_msg;          /* common NAK message */
309         spinlock_t              kptl_ptlid2str_lock;   /* serialise str ops */
310 };
311
312 struct kptl_net
313 {
314         struct list_head  net_list;      /* chain on kptl_data:: kptl_nets */
315         lnet_ni_t        *net_ni;
316         atomic_t          net_refcount;  /* # current references */
317         int               net_shutdown;  /* lnd_shutdown called */
318 };
319
320 enum 
321 {
322         PTLLND_INIT_NOTHING = 0,
323         PTLLND_INIT_DATA,
324         PTLLND_INIT_ALL,
325 };
326
327 extern kptl_tunables_t  kptllnd_tunables;
328 extern kptl_data_t      kptllnd_data;
329
330 static inline lnet_nid_t 
331 kptllnd_ptl2lnetnid(lnet_nid_t ni_nid, ptl_nid_t ptl_nid)
332 {
333 #ifdef _USING_LUSTRE_PORTALS_
334         return LNET_MKNID(LNET_NIDNET(ni_nid), LNET_NIDADDR(ptl_nid));
335 #else
336         return LNET_MKNID(LNET_NIDNET(ni_nid), ptl_nid);
337 #endif
338 }
339
340 static inline ptl_nid_t 
341 kptllnd_lnet2ptlnid(lnet_nid_t lnet_nid)
342 {
343 #ifdef _USING_LUSTRE_PORTALS_
344         return LNET_MKNID(LNET_NIDNET(kptllnd_data.kptl_portals_id.nid),
345                           LNET_NIDADDR(lnet_nid));
346 #else
347         return LNET_NIDADDR(lnet_nid);
348 #endif
349 }
350
351 static inline void
352 kptllnd_schedule_ptltrace_dump (void)
353 {
354 #ifdef CRAY_XT3
355         if (*kptllnd_tunables.kptl_ptltrace_on_fail) {
356                 atomic_inc(&kptllnd_data.kptl_needs_ptltrace);
357                 wake_up(&kptllnd_data.kptl_watchdog_waitq);
358         }
359 #endif
360 }
361
362 int  kptllnd_startup(lnet_ni_t *ni);
363 void kptllnd_shutdown(lnet_ni_t *ni);
364 int  kptllnd_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg);
365 void kptllnd_query (struct lnet_ni *ni, lnet_nid_t nid, cfs_time_t *when);
366 int  kptllnd_send(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg);
367 int  kptllnd_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg,
368                   int delayed, unsigned int niov, 
369                   struct iovec *iov, lnet_kiov_t *kiov,
370                   unsigned int offset, unsigned int mlen, unsigned int rlen);
371 int  kptllnd_eager_recv(struct lnet_ni *ni, void *private, 
372                         lnet_msg_t *msg, void **new_privatep);
373 void kptllnd_eq_callback(ptl_event_t *evp);
374 int  kptllnd_scheduler(void *arg);
375 int  kptllnd_watchdog(void *arg);
376 int  kptllnd_thread_start(int (*fn)(void *arg), void *arg);
377 int  kptllnd_tunables_init(void);
378 void kptllnd_tunables_fini(void);
379
380 const char *kptllnd_evtype2str(int evtype);
381 const char *kptllnd_msgtype2str(int msgtype);
382 const char *kptllnd_errtype2str(int errtype);
383
384 static inline void *
385 kptllnd_eventarg2obj (kptl_eventarg_t *eva)
386 {
387         switch (eva->eva_type) {
388         default:
389                 LBUG();
390         case PTLLND_EVENTARG_TYPE_BUF:
391                 return list_entry(eva, kptl_rx_buffer_t, rxb_eventarg);
392         case PTLLND_EVENTARG_TYPE_RDMA:
393                 return list_entry(eva, kptl_tx_t, tx_rdma_eventarg);
394         case PTLLND_EVENTARG_TYPE_MSG:
395                 return list_entry(eva, kptl_tx_t, tx_msg_eventarg);
396         }
397 }
398
399 /*
400  * RX BUFFER SUPPORT FUNCTIONS
401  */
402 void kptllnd_rx_buffer_pool_init(kptl_rx_buffer_pool_t *rxbp);
403 void kptllnd_rx_buffer_pool_fini(kptl_rx_buffer_pool_t *rxbp);
404 int  kptllnd_rx_buffer_pool_reserve(kptl_rx_buffer_pool_t *rxbp, int count);
405 void kptllnd_rx_buffer_pool_unreserve(kptl_rx_buffer_pool_t *rxbp, int count);
406 void kptllnd_rx_buffer_callback(ptl_event_t *ev);
407 void kptllnd_rx_buffer_post(kptl_rx_buffer_t *rxb);
408
409 static inline int
410 kptllnd_rx_buffer_size(void)
411 {
412         return PAGE_SIZE * (*kptllnd_tunables.kptl_rxb_npages);
413 }
414
415 static inline void
416 kptllnd_rx_buffer_addref(kptl_rx_buffer_t *rxb)
417 {
418         unsigned long flags;
419         
420         spin_lock_irqsave(&rxb->rxb_pool->rxbp_lock, flags);
421         rxb->rxb_refcount++;
422         spin_unlock_irqrestore(&rxb->rxb_pool->rxbp_lock, flags);
423 }
424
425 static inline void
426 kptllnd_rx_buffer_decref_locked(kptl_rx_buffer_t *rxb)
427 {
428         if (--(rxb->rxb_refcount) == 0) {
429                 spin_lock(&kptllnd_data.kptl_sched_lock);
430         
431                 list_add_tail(&rxb->rxb_repost_list,
432                               &kptllnd_data.kptl_sched_rxbq);
433                 wake_up(&kptllnd_data.kptl_sched_waitq);
434
435                 spin_unlock(&kptllnd_data.kptl_sched_lock);
436         }
437 }
438
439 static inline void
440 kptllnd_rx_buffer_decref(kptl_rx_buffer_t *rxb)
441 {
442         unsigned long flags;
443         int           count;
444         
445         spin_lock_irqsave(&rxb->rxb_pool->rxbp_lock, flags);
446         count = --(rxb->rxb_refcount);
447         spin_unlock_irqrestore(&rxb->rxb_pool->rxbp_lock, flags);
448
449         if (count == 0)
450                 kptllnd_rx_buffer_post(rxb);
451 }
452
453 /*
454  * RX SUPPORT FUNCTIONS
455  */
456 void kptllnd_rx_parse(kptl_rx_t *rx);
457 void kptllnd_rx_done(kptl_rx_t *rx, int post_credit);
458
459 /*
460  * PEER SUPPORT FUNCTIONS
461  */
462 int kptllnd_get_peer_info(int index,
463                           lnet_process_id_t *id, 
464                           int *state, int *sent_hello,
465                           int *refcount, __u64 *incarnation,
466                           __u64 *next_matchbits, __u64 *last_matchbits_seen,
467                           int *nsendq, int *nactiveq,
468                           int *credits, int *outstanding_credits);
469 void kptllnd_peer_destroy(kptl_peer_t *peer);
470 int  kptllnd_peer_del(lnet_process_id_t id);
471 void kptllnd_peer_close_locked(kptl_peer_t *peer, int why);
472 void kptllnd_peer_close(kptl_peer_t *peer, int why);
473 void kptllnd_handle_closing_peers(void);
474 int  kptllnd_peer_connect(kptl_tx_t *tx, lnet_nid_t nid);
475 void kptllnd_peer_check_sends(kptl_peer_t *peer);
476 void kptllnd_peer_check_bucket(int idx, int stamp);
477 void kptllnd_tx_launch(kptl_peer_t *peer, kptl_tx_t *tx, int nfrag);
478 int  kptllnd_find_target(kptl_net_t *net, lnet_process_id_t target,
479                          kptl_peer_t **peerp);
480 kptl_peer_t *kptllnd_peer_handle_hello(kptl_net_t *net,
481                                        ptl_process_id_t initiator,
482                                        kptl_msg_t *msg);
483 kptl_peer_t *kptllnd_id2peer_locked(lnet_process_id_t id);
484 void kptllnd_peer_alive(kptl_peer_t *peer);
485
486 static inline void
487 kptllnd_peer_addref (kptl_peer_t *peer)
488 {
489         atomic_inc(&peer->peer_refcount);
490 }
491
492 static inline void
493 kptllnd_peer_decref (kptl_peer_t *peer)
494 {
495         if (atomic_dec_and_test(&peer->peer_refcount))
496                 kptllnd_peer_destroy(peer);
497 }
498
499 static inline void
500 kptllnd_net_addref (kptl_net_t *net)
501 {
502         LASSERT (atomic_read(&net->net_refcount) > 0);
503         atomic_inc(&net->net_refcount);
504 }
505
506 static inline void
507 kptllnd_net_decref (kptl_net_t *net)
508 {
509         LASSERT (atomic_read(&net->net_refcount) > 0);
510         atomic_dec(&net->net_refcount);
511 }
512
513 static inline void
514 kptllnd_set_tx_peer(kptl_tx_t *tx, kptl_peer_t *peer) 
515 {
516         LASSERT (tx->tx_peer == NULL);
517         
518         kptllnd_peer_addref(peer);
519         tx->tx_peer = peer;
520 }
521
522 static inline struct list_head *
523 kptllnd_nid2peerlist(lnet_nid_t nid)
524 {
525         /* Only one copy of peer state for all logical peers, so the net part
526          * of NIDs is ignored; e.g. A@ptl0 and A@ptl2 share peer state */
527         unsigned int hash = ((unsigned int)LNET_NIDADDR(nid)) %
528                             kptllnd_data.kptl_peer_hash_size;
529
530         return &kptllnd_data.kptl_peers[hash];
531 }
532
533 static inline kptl_peer_t *
534 kptllnd_id2peer(lnet_process_id_t id)
535 {
536         kptl_peer_t   *peer;
537         unsigned long  flags;
538
539         read_lock_irqsave(&kptllnd_data.kptl_peer_rw_lock, flags);
540         peer = kptllnd_id2peer_locked(id);
541         read_unlock_irqrestore(&kptllnd_data.kptl_peer_rw_lock, flags);
542
543         return peer;
544 }
545
546 static inline int
547 kptllnd_reserve_buffers(int n)
548 {
549         return kptllnd_rx_buffer_pool_reserve(&kptllnd_data.kptl_rx_buffer_pool,
550                                               n);
551 }
552
553 static inline int
554 kptllnd_peer_reserve_buffers(void)
555 {
556         return kptllnd_reserve_buffers(*kptllnd_tunables.kptl_peertxcredits);
557 }
558
559 static inline void
560 kptllnd_peer_unreserve_buffers(void)
561 {
562         kptllnd_rx_buffer_pool_unreserve(&kptllnd_data.kptl_rx_buffer_pool,
563                                          *kptllnd_tunables.kptl_peertxcredits);
564 }
565
566 /*
567  * TX SUPPORT FUNCTIONS
568  */
569 int  kptllnd_setup_tx_descs(void);
570 void kptllnd_cleanup_tx_descs(void);
571 void kptllnd_tx_fini(kptl_tx_t *tx);
572 void kptllnd_cancel_txlist(struct list_head *peerq, struct list_head *txs);
573 void kptllnd_restart_txs(kptl_net_t *net, lnet_process_id_t id, struct list_head *restarts);
574 kptl_tx_t *kptllnd_get_idle_tx(enum kptl_tx_type purpose);
575 void kptllnd_tx_callback(ptl_event_t *ev);
576 const char *kptllnd_tx_typestr(int type);
577
578 static inline void
579 kptllnd_tx_addref(kptl_tx_t *tx)
580 {
581         atomic_inc(&tx->tx_refcount);
582 }
583
584 static inline void 
585 kptllnd_tx_decref(kptl_tx_t *tx)
586 {
587         LASSERT (!in_interrupt());        /* Thread context only */
588
589         if (atomic_dec_and_test(&tx->tx_refcount))
590                 kptllnd_tx_fini(tx);
591 }
592
593 /*
594  * MESSAGE SUPPORT FUNCTIONS
595  */
596 void kptllnd_init_msg(kptl_msg_t *msg, int type,
597                       lnet_process_id_t target, int body_nob);
598 void kptllnd_msg_pack(kptl_msg_t *msg, kptl_peer_t *peer);
599 int  kptllnd_msg_unpack(kptl_msg_t *msg, int nob);
600
601 /*
602  * MISC SUPPORT FUNCTIONS
603  */
604 void kptllnd_init_rdma_md(kptl_tx_t *tx, unsigned int niov,
605                           struct iovec *iov, lnet_kiov_t *kiov,
606                           unsigned int offset, unsigned int nob);
607 char *kptllnd_ptlid2str(ptl_process_id_t id);
608
609 void kptllnd_init_ptltrace(void);
610 void kptllnd_dump_ptltrace(void);
611
612 #ifdef PJK_DEBUGGING
613 #define SIMULATION_FAIL_TX_PUT_ALLOC   0       /* 0x00000001 */
614 #define SIMULATION_FAIL_TX_GET_ALLOC   1       /* 0x00000002 */
615 #define SIMULATION_FAIL_TX             2       /* 0x00000004 */
616 #define SIMULATION_FAIL_RX_ALLOC       3       /* 0x00000008 */
617
618 #define IS_SIMULATION_ENABLED(x) \
619         (((*kptllnd_tunables.kptl_simulation_bitmap) & 1<< SIMULATION_##x) != 0)
620 #else
621 #define IS_SIMULATION_ENABLED(x)       0
622 #endif