1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * Top level include for library side routines
13 # include <asm/page.h>
14 # include <linux/string.h>
16 # include <portals/list.h>
19 #include <portals/types.h>
20 #include <linux/kp30.h>
21 #include <portals/p30.h>
22 #include <portals/errno.h>
23 #include <portals/lib-types.h>
24 #include <portals/lib-nal.h>
25 #include <portals/lib-dispatch.h>
27 static inline int ptl_is_wire_handle_none (ptl_handle_wire_t *wh)
29 return (wh->wh_interface_cookie == PTL_WIRE_HANDLE_NONE.wh_interface_cookie &&
30 wh->wh_object_cookie == PTL_WIRE_HANDLE_NONE.wh_object_cookie);
34 #define state_lock(nal,flagsp) \
36 CDEBUG(D_PORTALS, "taking state lock\n"); \
37 nal->cb_cli(nal, flagsp); \
40 #define state_unlock(nal,flagsp) \
42 CDEBUG(D_PORTALS, "releasing state lock\n"); \
43 nal->cb_sti(nal, flagsp); \
46 /* not needed in user space until we thread there */
47 #define state_lock(nal,flagsp) \
49 CDEBUG(D_PORTALS, "taking state lock\n"); \
50 CDEBUG(D_PORTALS, "%p:%p\n", nal, flagsp); \
53 #define state_unlock(nal,flagsp) \
55 CDEBUG(D_PORTALS, "releasing state lock\n"); \
56 CDEBUG(D_PORTALS, "%p:%p\n", nal, flagsp); \
58 #endif /* __KERNEL__ */
60 #ifndef PTL_USE_SLAB_CACHE
64 #define MAX_MSGS 2048 /* Outstanding messages */
67 extern int lib_freelist_init (nal_cb_t *nal, lib_freelist_t *fl, int nobj, int objsize);
68 extern void lib_freelist_fini (nal_cb_t *nal, lib_freelist_t *fl);
71 lib_freelist_alloc (lib_freelist_t *fl)
73 /* ALWAYS called with statelock held */
76 if (list_empty (&fl->fl_list))
79 o = list_entry (fl->fl_list.next, lib_freeobj_t, fo_list);
80 list_del (&o->fo_list);
81 return ((void *)&o->fo_contents);
85 lib_freelist_free (lib_freelist_t *fl, void *obj)
87 /* ALWAYS called with statelock held */
88 lib_freeobj_t *o = list_entry (obj, lib_freeobj_t, fo_contents);
90 list_add (&o->fo_list, &fl->fl_list);
94 static inline lib_eq_t *
95 lib_eq_alloc (nal_cb_t *nal)
97 /* NEVER called with statelock held */
101 state_lock (nal, &flags);
102 eq = (lib_eq_t *)lib_freelist_alloc (&nal->ni.ni_free_eqs);
103 state_unlock (nal, &flags);
109 lib_eq_free (nal_cb_t *nal, lib_eq_t *eq)
111 /* ALWAYS called with statelock held */
112 lib_freelist_free (&nal->ni.ni_free_eqs, eq);
115 static inline lib_md_t *
116 lib_md_alloc (nal_cb_t *nal)
118 /* NEVER called with statelock held */
122 state_lock (nal, &flags);
123 md = (lib_md_t *)lib_freelist_alloc (&nal->ni.ni_free_mds);
124 state_unlock (nal, &flags);
130 lib_md_free (nal_cb_t *nal, lib_md_t *md)
132 /* ALWAYS called with statelock held */
133 lib_freelist_free (&nal->ni.ni_free_mds, md);
136 static inline lib_me_t *
137 lib_me_alloc (nal_cb_t *nal)
139 /* NEVER called with statelock held */
143 state_lock (nal, &flags);
144 me = (lib_me_t *)lib_freelist_alloc (&nal->ni.ni_free_mes);
145 state_unlock (nal, &flags);
151 lib_me_free (nal_cb_t *nal, lib_me_t *me)
153 /* ALWAYS called with statelock held */
154 lib_freelist_free (&nal->ni.ni_free_mes, me);
157 static inline lib_msg_t *
158 lib_msg_alloc (nal_cb_t *nal)
160 /* ALWAYS called with statelock held */
161 return ((lib_msg_t *)lib_freelist_alloc (&nal->ni.ni_free_msgs));
165 lib_msg_free (nal_cb_t *nal, lib_msg_t *msg)
167 /* ALWAYS called with statelock held */
168 lib_freelist_free (&nal->ni.ni_free_msgs, msg);
173 extern kmem_cache_t *ptl_md_slab;
174 extern kmem_cache_t *ptl_msg_slab;
175 extern kmem_cache_t *ptl_me_slab;
176 extern kmem_cache_t *ptl_eq_slab;
177 extern atomic_t md_in_use_count;
178 extern atomic_t msg_in_use_count;
179 extern atomic_t me_in_use_count;
180 extern atomic_t eq_in_use_count;
182 static inline lib_eq_t *
183 lib_eq_alloc (nal_cb_t *nal)
185 /* NEVER called with statelock held */
186 lib_eq_t *eq = kmem_cache_alloc(ptl_eq_slab, GFP_NOFS);
191 atomic_inc (&eq_in_use_count);
196 lib_eq_free (nal_cb_t *nal, lib_eq_t *eq)
198 /* ALWAYS called with statelock held */
199 atomic_dec (&eq_in_use_count);
200 kmem_cache_free(ptl_eq_slab, eq);
203 static inline lib_md_t *
204 lib_md_alloc (nal_cb_t *nal)
206 /* NEVER called with statelock held */
207 lib_md_t *md = kmem_cache_alloc(ptl_md_slab, GFP_NOFS);
212 atomic_inc (&md_in_use_count);
217 lib_md_free (nal_cb_t *nal, lib_md_t *md)
219 /* ALWAYS called with statelock held */
220 atomic_dec (&md_in_use_count);
221 kmem_cache_free(ptl_md_slab, md);
224 static inline lib_me_t *
225 lib_me_alloc (nal_cb_t *nal)
227 /* NEVER called with statelock held */
228 lib_me_t *me = kmem_cache_alloc(ptl_me_slab, GFP_NOFS);
233 atomic_inc (&me_in_use_count);
238 lib_me_free(nal_cb_t *nal, lib_me_t *me)
240 /* ALWAYS called with statelock held */
241 atomic_dec (&me_in_use_count);
242 kmem_cache_free(ptl_me_slab, me);
245 static inline lib_msg_t *
246 lib_msg_alloc(nal_cb_t *nal)
248 /* ALWAYS called with statelock held */
249 lib_msg_t *msg = kmem_cache_alloc(ptl_msg_slab, GFP_ATOMIC);
254 atomic_inc (&msg_in_use_count);
259 lib_msg_free(nal_cb_t *nal, lib_msg_t *msg)
261 /* ALWAYS called with statelock held */
262 atomic_dec (&msg_in_use_count);
263 kmem_cache_free(ptl_msg_slab, msg);
267 extern lib_handle_t *lib_lookup_cookie (nal_cb_t *nal, __u64 cookie, int type);
268 extern void lib_initialise_handle (nal_cb_t *nal, lib_handle_t *lh, int type);
269 extern void lib_invalidate_handle (nal_cb_t *nal, lib_handle_t *lh);
272 ptl_eq2handle (ptl_handle_eq_t *handle, lib_eq_t *eq)
274 handle->cookie = eq->eq_lh.lh_cookie;
277 static inline lib_eq_t *
278 ptl_handle2eq (ptl_handle_eq_t *handle, nal_cb_t *nal)
280 /* ALWAYS called with statelock held */
281 lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
286 return (lh_entry (lh, lib_eq_t, eq_lh));
290 ptl_md2handle (ptl_handle_md_t *handle, lib_md_t *md)
292 handle->cookie = md->md_lh.lh_cookie;
295 static inline lib_md_t *
296 ptl_handle2md (ptl_handle_md_t *handle, nal_cb_t *nal)
298 /* ALWAYS called with statelock held */
299 lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
304 return (lh_entry (lh, lib_md_t, md_lh));
307 static inline lib_md_t *
308 ptl_wire_handle2md (ptl_handle_wire_t *wh, nal_cb_t *nal)
310 /* ALWAYS called with statelock held */
313 if (wh->wh_interface_cookie != nal->ni.ni_interface_cookie)
316 lh = lib_lookup_cookie (nal, wh->wh_object_cookie,
321 return (lh_entry (lh, lib_md_t, md_lh));
325 ptl_me2handle (ptl_handle_me_t *handle, lib_me_t *me)
327 handle->cookie = me->me_lh.lh_cookie;
330 static inline lib_me_t *
331 ptl_handle2me (ptl_handle_me_t *handle, nal_cb_t *nal)
333 /* ALWAYS called with statelock held */
334 lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
339 return (lh_entry (lh, lib_me_t, me_lh));
342 extern int lib_init(nal_cb_t * cb, ptl_nid_t nid, ptl_pid_t pid, int gsize,
343 ptl_pt_index_t tbl_size, ptl_ac_index_t ac_size);
344 extern int lib_fini(nal_cb_t * cb);
345 extern void lib_dispatch(nal_cb_t * cb, void *private, int index,
346 void *arg_block, void *ret_block);
347 extern char *dispatch_name(int index);
350 * When the NAL detects an incoming message, it should call
351 * lib_parse() decode it. The NAL callbacks will be handed
352 * the private cookie as a way for the NAL to maintain state
353 * about which transaction is being processed. An extra parameter,
354 * lib_cookie will contain the necessary information for
355 * finalizing the message.
357 * After it has finished the handling the message, it should
358 * call lib_finalize() with the lib_cookie parameter.
359 * Call backs will be made to write events, send acks or
362 extern int lib_parse(nal_cb_t * nal, ptl_hdr_t * hdr, void *private);
363 extern int lib_finalize(nal_cb_t * nal, void *private, lib_msg_t * msg);
364 extern void print_hdr(nal_cb_t * nal, ptl_hdr_t * hdr);
366 extern ptl_size_t lib_iov_nob (int niov, struct iovec *iov);
367 extern void lib_copy_iov2buf (char *dest, int niov, struct iovec *iov, ptl_size_t len);
368 extern void lib_copy_buf2iov (int niov, struct iovec *iov, char *dest, ptl_size_t len);
370 extern ptl_size_t lib_kiov_nob (int niov, ptl_kiov_t *iov);
371 extern void lib_copy_kiov2buf (char *dest, int niov, ptl_kiov_t *iov, ptl_size_t len);
372 extern void lib_copy_buf2kiov (int niov, ptl_kiov_t *iov, char *src, ptl_size_t len);
373 extern void lib_assert_wire_constants (void);
375 extern void lib_recv (nal_cb_t *nal, void *private, lib_msg_t *msg, lib_md_t *md,
376 ptl_size_t offset, ptl_size_t mlen, ptl_size_t rlen);
377 extern int lib_send (nal_cb_t *nal, void *private, lib_msg_t *msg,
378 ptl_hdr_t *hdr, int type, ptl_nid_t nid, ptl_pid_t pid,
379 lib_md_t *md, ptl_size_t offset, ptl_size_t len);
381 extern void lib_md_deconstruct(nal_cb_t * nal, lib_md_t * md_in,
383 extern void lib_md_unlink(nal_cb_t * nal, lib_md_t * md_in);
384 extern void lib_me_unlink(nal_cb_t * nal, lib_me_t * me_in);