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);
33 #define state_lock(nal,flagsp) \
35 CDEBUG(D_PORTALS, "taking state lock\n"); \
36 nal->cb_cli(nal, flagsp); \
39 #define state_unlock(nal,flagsp) \
41 CDEBUG(D_PORTALS, "releasing state lock\n"); \
42 nal->cb_sti(nal, flagsp); \
45 #ifndef PTL_USE_SLAB_CACHE
49 #define MAX_MSGS 2048 /* Outstanding messages */
52 extern int lib_freelist_init (nal_cb_t *nal, lib_freelist_t *fl, int nobj, int objsize);
53 extern void lib_freelist_fini (nal_cb_t *nal, lib_freelist_t *fl);
56 lib_freelist_alloc (lib_freelist_t *fl)
58 /* ALWAYS called with statelock held */
61 if (list_empty (&fl->fl_list))
64 o = list_entry (fl->fl_list.next, lib_freeobj_t, fo_list);
65 list_del (&o->fo_list);
66 return ((void *)&o->fo_contents);
70 lib_freelist_free (lib_freelist_t *fl, void *obj)
72 /* ALWAYS called with statelock held */
73 lib_freeobj_t *o = list_entry (obj, lib_freeobj_t, fo_contents);
75 list_add (&o->fo_list, &fl->fl_list);
79 static inline lib_eq_t *
80 lib_eq_alloc (nal_cb_t *nal)
82 /* NEVER called with statelock held */
86 state_lock (nal, &flags);
87 eq = (lib_eq_t *)lib_freelist_alloc (&nal->ni.ni_free_eqs);
88 state_unlock (nal, &flags);
94 lib_eq_free (nal_cb_t *nal, lib_eq_t *eq)
96 /* ALWAYS called with statelock held */
97 lib_freelist_free (&nal->ni.ni_free_eqs, eq);
100 static inline lib_md_t *
101 lib_md_alloc (nal_cb_t *nal)
103 /* NEVER called with statelock held */
107 state_lock (nal, &flags);
108 md = (lib_md_t *)lib_freelist_alloc (&nal->ni.ni_free_mds);
109 state_unlock (nal, &flags);
115 lib_md_free (nal_cb_t *nal, lib_md_t *md)
117 /* ALWAYS called with statelock held */
118 lib_freelist_free (&nal->ni.ni_free_mds, md);
121 static inline lib_me_t *
122 lib_me_alloc (nal_cb_t *nal)
124 /* NEVER called with statelock held */
128 state_lock (nal, &flags);
129 me = (lib_me_t *)lib_freelist_alloc (&nal->ni.ni_free_mes);
130 state_unlock (nal, &flags);
136 lib_me_free (nal_cb_t *nal, lib_me_t *me)
138 /* ALWAYS called with statelock held */
139 lib_freelist_free (&nal->ni.ni_free_mes, me);
142 static inline lib_msg_t *
143 lib_msg_alloc (nal_cb_t *nal)
145 /* ALWAYS called with statelock held */
146 return ((lib_msg_t *)lib_freelist_alloc (&nal->ni.ni_free_msgs));
150 lib_msg_free (nal_cb_t *nal, lib_msg_t *msg)
152 /* ALWAYS called with statelock held */
153 lib_freelist_free (&nal->ni.ni_free_msgs, msg);
158 extern kmem_cache_t *ptl_md_slab;
159 extern kmem_cache_t *ptl_msg_slab;
160 extern kmem_cache_t *ptl_me_slab;
161 extern kmem_cache_t *ptl_eq_slab;
162 extern atomic_t md_in_use_count;
163 extern atomic_t msg_in_use_count;
164 extern atomic_t me_in_use_count;
165 extern atomic_t eq_in_use_count;
167 static inline lib_eq_t *
168 lib_eq_alloc (nal_cb_t *nal)
170 /* NEVER called with statelock held */
172 PORTAL_SLAB_ALLOC(eq, ptl_eq_slab, sizeof(*eq));
177 atomic_inc (&eq_in_use_count);
182 lib_eq_free (nal_cb_t *nal, lib_eq_t *eq)
184 /* ALWAYS called with statelock held */
185 atomic_dec (&eq_in_use_count);
186 PORTAL_SLAB_FREE(eq, ptl_eq_slab, sizeof(*eq));
189 static inline lib_md_t *
190 lib_md_alloc (nal_cb_t *nal)
192 /* NEVER called with statelock held */
194 PORTAL_SLAB_ALLOC(md, ptl_md_slab, sizeof(*md));
199 atomic_inc (&md_in_use_count);
204 lib_md_free (nal_cb_t *nal, lib_md_t *md)
206 /* ALWAYS called with statelock held */
207 atomic_dec (&md_in_use_count);
208 PORTAL_SLAB_FREE(md, ptl_md_slab, sizeof(*md));
211 static inline lib_me_t *
212 lib_me_alloc (nal_cb_t *nal)
214 /* NEVER called with statelock held */
216 PORTAL_SLAB_ALLOC(me, ptl_me_slab, sizeof(*me));
221 atomic_inc (&me_in_use_count);
226 lib_me_free(nal_cb_t *nal, lib_me_t *me)
228 /* ALWAYS called with statelock held */
229 atomic_dec (&me_in_use_count);
230 PORTAL_SLAB_FREE(me, ptl_me_slab, sizeof(*me));
233 static inline lib_msg_t *
234 lib_msg_alloc(nal_cb_t *nal)
236 /* ALWAYS called with statelock held */
238 PORTAL_SLAB_ALLOC(msg, ptl_msg_slab, sizeof(*msg));
243 atomic_inc (&msg_in_use_count);
248 lib_msg_free(nal_cb_t *nal, lib_msg_t *msg)
250 /* ALWAYS called with statelock held */
251 atomic_dec (&msg_in_use_count);
252 PORTAL_SLAB_FREE(msg, ptl_msg_slab, sizeof(*msg));
256 extern lib_handle_t *lib_lookup_cookie (nal_cb_t *nal, __u64 cookie, int type);
257 extern void lib_initialise_handle (nal_cb_t *nal, lib_handle_t *lh, int type);
258 extern void lib_invalidate_handle (nal_cb_t *nal, lib_handle_t *lh);
261 ptl_eq2handle (ptl_handle_eq_t *handle, lib_eq_t *eq)
263 handle->cookie = eq->eq_lh.lh_cookie;
266 static inline lib_eq_t *
267 ptl_handle2eq (ptl_handle_eq_t *handle, nal_cb_t *nal)
269 /* ALWAYS called with statelock held */
270 lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
275 return (lh_entry (lh, lib_eq_t, eq_lh));
279 ptl_md2handle (ptl_handle_md_t *handle, lib_md_t *md)
281 handle->cookie = md->md_lh.lh_cookie;
284 static inline lib_md_t *
285 ptl_handle2md (ptl_handle_md_t *handle, nal_cb_t *nal)
287 /* ALWAYS called with statelock held */
288 lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
293 return (lh_entry (lh, lib_md_t, md_lh));
296 static inline lib_md_t *
297 ptl_wire_handle2md (ptl_handle_wire_t *wh, nal_cb_t *nal)
299 /* ALWAYS called with statelock held */
302 if (wh->wh_interface_cookie != nal->ni.ni_interface_cookie)
305 lh = lib_lookup_cookie (nal, wh->wh_object_cookie,
310 return (lh_entry (lh, lib_md_t, md_lh));
314 ptl_me2handle (ptl_handle_me_t *handle, lib_me_t *me)
316 handle->cookie = me->me_lh.lh_cookie;
319 static inline lib_me_t *
320 ptl_handle2me (ptl_handle_me_t *handle, nal_cb_t *nal)
322 /* ALWAYS called with statelock held */
323 lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
328 return (lh_entry (lh, lib_me_t, me_lh));
331 extern int lib_init(nal_cb_t * cb, ptl_nid_t nid, ptl_pid_t pid, int gsize,
332 ptl_pt_index_t tbl_size, ptl_ac_index_t ac_size);
333 extern int lib_fini(nal_cb_t * cb);
334 extern void lib_dispatch(nal_cb_t * cb, void *private, int index,
335 void *arg_block, void *ret_block);
336 extern char *dispatch_name(int index);
339 * When the NAL detects an incoming message, it should call
340 * lib_parse() decode it. The NAL callbacks will be handed
341 * the private cookie as a way for the NAL to maintain state
342 * about which transaction is being processed. An extra parameter,
343 * lib_cookie will contain the necessary information for
344 * finalizing the message.
346 * After it has finished the handling the message, it should
347 * call lib_finalize() with the lib_cookie parameter.
348 * Call backs will be made to write events, send acks or
351 extern int lib_parse(nal_cb_t * nal, ptl_hdr_t * hdr, void *private);
352 extern int lib_finalize(nal_cb_t * nal, void *private, lib_msg_t * msg);
353 extern lib_msg_t *lib_fake_reply_msg (nal_cb_t *nal, ptl_nid_t peer_nid,
355 extern void print_hdr(nal_cb_t * nal, ptl_hdr_t * hdr);
357 extern ptl_size_t lib_iov_nob (int niov, struct iovec *iov);
358 extern void lib_copy_iov2buf (char *dest, int niov, struct iovec *iov, ptl_size_t len);
359 extern void lib_copy_buf2iov (int niov, struct iovec *iov, char *dest, ptl_size_t len);
361 extern ptl_size_t lib_kiov_nob (int niov, ptl_kiov_t *iov);
362 extern void lib_copy_kiov2buf (char *dest, int niov, ptl_kiov_t *iov, ptl_size_t len);
363 extern void lib_copy_buf2kiov (int niov, ptl_kiov_t *iov, char *src, ptl_size_t len);
364 extern void lib_assert_wire_constants (void);
366 extern void lib_recv (nal_cb_t *nal, void *private, lib_msg_t *msg, lib_md_t *md,
367 ptl_size_t offset, ptl_size_t mlen, ptl_size_t rlen);
368 extern int lib_send (nal_cb_t *nal, void *private, lib_msg_t *msg,
369 ptl_hdr_t *hdr, int type, ptl_nid_t nid, ptl_pid_t pid,
370 lib_md_t *md, ptl_size_t offset, ptl_size_t len);
372 extern void lib_md_deconstruct(nal_cb_t * nal, lib_md_t * md_in,
374 extern void lib_md_unlink(nal_cb_t * nal, lib_md_t * md_in);
375 extern void lib_me_unlink(nal_cb_t * nal, lib_me_t * me_in);