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 #ifdef PTL_USE_DESC_LISTS
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 atomic_t md_in_use_count;
159 extern atomic_t msg_in_use_count;
160 extern atomic_t me_in_use_count;
161 extern atomic_t eq_in_use_count;
163 static inline lib_eq_t *
164 lib_eq_alloc (nal_cb_t *nal)
166 /* NEVER called with statelock held */
168 PORTAL_ALLOC(eq, sizeof(*eq));
173 atomic_inc (&eq_in_use_count);
178 lib_eq_free (nal_cb_t *nal, lib_eq_t *eq)
180 /* ALWAYS called with statelock held */
181 atomic_dec (&eq_in_use_count);
182 PORTAL_FREE(eq, sizeof(*eq));
185 static inline lib_md_t *
186 lib_md_alloc (nal_cb_t *nal)
188 /* NEVER called with statelock held */
190 PORTAL_ALLOC(md, sizeof(*md));
195 atomic_inc (&md_in_use_count);
200 lib_md_free (nal_cb_t *nal, lib_md_t *md)
202 /* ALWAYS called with statelock held */
203 atomic_dec (&md_in_use_count);
204 PORTAL_FREE(md, sizeof(*md));
207 static inline lib_me_t *
208 lib_me_alloc (nal_cb_t *nal)
210 /* NEVER called with statelock held */
212 PORTAL_ALLOC(me, sizeof(*me));
217 atomic_inc (&me_in_use_count);
222 lib_me_free(nal_cb_t *nal, lib_me_t *me)
224 /* ALWAYS called with statelock held */
225 atomic_dec (&me_in_use_count);
226 PORTAL_FREE(me, sizeof(*me));
229 static inline lib_msg_t *
230 lib_msg_alloc(nal_cb_t *nal)
232 /* ALWAYS called with statelock held */
234 PORTAL_ALLOC_ATOMIC(msg, sizeof(*msg));
239 atomic_inc (&msg_in_use_count);
244 lib_msg_free(nal_cb_t *nal, lib_msg_t *msg)
246 /* ALWAYS called with statelock held */
247 atomic_dec (&msg_in_use_count);
248 PORTAL_FREE(msg, sizeof(*msg));
252 extern lib_handle_t *lib_lookup_cookie (nal_cb_t *nal, __u64 cookie, int type);
253 extern void lib_initialise_handle (nal_cb_t *nal, lib_handle_t *lh, int type);
254 extern void lib_invalidate_handle (nal_cb_t *nal, lib_handle_t *lh);
257 ptl_eq2handle (ptl_handle_eq_t *handle, lib_eq_t *eq)
259 handle->cookie = eq->eq_lh.lh_cookie;
262 static inline lib_eq_t *
263 ptl_handle2eq (ptl_handle_eq_t *handle, nal_cb_t *nal)
265 /* ALWAYS called with statelock held */
266 lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
271 return (lh_entry (lh, lib_eq_t, eq_lh));
275 ptl_md2handle (ptl_handle_md_t *handle, lib_md_t *md)
277 handle->cookie = md->md_lh.lh_cookie;
280 static inline lib_md_t *
281 ptl_handle2md (ptl_handle_md_t *handle, nal_cb_t *nal)
283 /* ALWAYS called with statelock held */
284 lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
289 return (lh_entry (lh, lib_md_t, md_lh));
292 static inline lib_md_t *
293 ptl_wire_handle2md (ptl_handle_wire_t *wh, nal_cb_t *nal)
295 /* ALWAYS called with statelock held */
298 if (wh->wh_interface_cookie != nal->ni.ni_interface_cookie)
301 lh = lib_lookup_cookie (nal, wh->wh_object_cookie,
306 return (lh_entry (lh, lib_md_t, md_lh));
310 ptl_me2handle (ptl_handle_me_t *handle, lib_me_t *me)
312 handle->cookie = me->me_lh.lh_cookie;
315 static inline lib_me_t *
316 ptl_handle2me (ptl_handle_me_t *handle, nal_cb_t *nal)
318 /* ALWAYS called with statelock held */
319 lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
324 return (lh_entry (lh, lib_me_t, me_lh));
327 extern int lib_init(nal_cb_t * cb, ptl_nid_t nid, ptl_pid_t pid, int gsize,
328 ptl_pt_index_t tbl_size, ptl_ac_index_t ac_size);
329 extern int lib_fini(nal_cb_t * cb);
330 extern void lib_dispatch(nal_cb_t * cb, void *private, int index,
331 void *arg_block, void *ret_block);
332 extern char *dispatch_name(int index);
335 * When the NAL detects an incoming message, it should call
336 * lib_parse() decode it. The NAL callbacks will be handed
337 * the private cookie as a way for the NAL to maintain state
338 * about which transaction is being processed. An extra parameter,
339 * lib_cookie will contain the necessary information for
340 * finalizing the message.
342 * After it has finished the handling the message, it should
343 * call lib_finalize() with the lib_cookie parameter.
344 * Call backs will be made to write events, send acks or
347 extern int lib_parse(nal_cb_t * nal, ptl_hdr_t * hdr, void *private);
348 extern int lib_finalize(nal_cb_t * nal, void *private, lib_msg_t * msg);
349 extern lib_msg_t *lib_fake_reply_msg (nal_cb_t *nal, ptl_nid_t peer_nid,
351 extern void print_hdr(nal_cb_t * nal, ptl_hdr_t * hdr);
353 extern ptl_size_t lib_iov_nob (int niov, struct iovec *iov);
354 extern void lib_copy_iov2buf (char *dest, int niov, struct iovec *iov, ptl_size_t len);
355 extern void lib_copy_buf2iov (int niov, struct iovec *iov, char *dest, ptl_size_t len);
357 extern ptl_size_t lib_kiov_nob (int niov, ptl_kiov_t *iov);
358 extern void lib_copy_kiov2buf (char *dest, int niov, ptl_kiov_t *iov, ptl_size_t len);
359 extern void lib_copy_buf2kiov (int niov, ptl_kiov_t *iov, char *src, ptl_size_t len);
360 extern void lib_assert_wire_constants (void);
362 extern void lib_recv (nal_cb_t *nal, void *private, lib_msg_t *msg, lib_md_t *md,
363 ptl_size_t offset, ptl_size_t mlen, ptl_size_t rlen);
364 extern int lib_send (nal_cb_t *nal, void *private, lib_msg_t *msg,
365 ptl_hdr_t *hdr, int type, ptl_nid_t nid, ptl_pid_t pid,
366 lib_md_t *md, ptl_size_t offset, ptl_size_t len);
368 extern void lib_md_deconstruct(nal_cb_t * nal, lib_md_t * md_in,
370 extern void lib_md_unlink(nal_cb_t * nal, lib_md_t * md_in);
371 extern void lib_me_unlink(nal_cb_t * nal, lib_me_t * me_in);