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
12 #include "build_check.h"
15 # include <asm/page.h>
16 # include <linux/string.h>
18 # include <portals/list.h>
21 #include <portals/types.h>
22 #include <linux/kp30.h>
23 #include <portals/p30.h>
24 #include <portals/lib-types.h>
25 #include <portals/lib-nal.h>
26 #include <portals/lib-dispatch.h>
28 static inline int ptl_is_wire_handle_none (ptl_handle_wire_t *wh)
30 return (wh->wh_interface_cookie == PTL_WIRE_HANDLE_NONE.wh_interface_cookie &&
31 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 #ifdef PTL_USE_LIB_FREELIST
50 #define MAX_MSGS 2048 /* Outstanding messages */
53 extern int lib_freelist_init (nal_cb_t *nal, lib_freelist_t *fl, int nobj, int objsize);
54 extern void lib_freelist_fini (nal_cb_t *nal, lib_freelist_t *fl);
57 lib_freelist_alloc (lib_freelist_t *fl)
59 /* ALWAYS called with statelock held */
62 if (list_empty (&fl->fl_list))
65 o = list_entry (fl->fl_list.next, lib_freeobj_t, fo_list);
66 list_del (&o->fo_list);
67 return ((void *)&o->fo_contents);
71 lib_freelist_free (lib_freelist_t *fl, void *obj)
73 /* ALWAYS called with statelock held */
74 lib_freeobj_t *o = list_entry (obj, lib_freeobj_t, fo_contents);
76 list_add (&o->fo_list, &fl->fl_list);
80 static inline lib_eq_t *
81 lib_eq_alloc (nal_cb_t *nal)
83 /* NEVER called with statelock held */
87 state_lock (nal, &flags);
88 eq = (lib_eq_t *)lib_freelist_alloc (&nal->ni.ni_free_eqs);
89 state_unlock (nal, &flags);
95 lib_eq_free (nal_cb_t *nal, lib_eq_t *eq)
97 /* ALWAYS called with statelock held */
98 lib_freelist_free (&nal->ni.ni_free_eqs, eq);
101 static inline lib_md_t *
102 lib_md_alloc (nal_cb_t *nal, ptl_md_t *umd)
104 /* NEVER called with statelock held */
108 state_lock (nal, &flags);
109 md = (lib_md_t *)lib_freelist_alloc (&nal->ni.ni_free_mds);
110 state_unlock (nal, &flags);
116 lib_md_free (nal_cb_t *nal, lib_md_t *md)
118 /* ALWAYS called with statelock held */
119 lib_freelist_free (&nal->ni.ni_free_mds, md);
122 static inline lib_me_t *
123 lib_me_alloc (nal_cb_t *nal)
125 /* NEVER called with statelock held */
129 state_lock (nal, &flags);
130 me = (lib_me_t *)lib_freelist_alloc (&nal->ni.ni_free_mes);
131 state_unlock (nal, &flags);
137 lib_me_free (nal_cb_t *nal, lib_me_t *me)
139 /* ALWAYS called with statelock held */
140 lib_freelist_free (&nal->ni.ni_free_mes, me);
143 static inline lib_msg_t *
144 lib_msg_alloc (nal_cb_t *nal)
146 /* NEVER called with statelock held */
150 state_lock (nal, &flags);
151 msg = (lib_msg_t *)lib_freelist_alloc (&nal->ni.ni_free_msgs);
152 state_unlock (nal, &flags);
155 /* NULL pointers, clear flags etc */
156 memset (msg, 0, sizeof (*msg));
157 msg->ack_wmd = PTL_WIRE_HANDLE_NONE;
163 lib_msg_free (nal_cb_t *nal, lib_msg_t *msg)
165 /* ALWAYS called with statelock held */
166 lib_freelist_free (&nal->ni.ni_free_msgs, msg);
171 static inline lib_eq_t *
172 lib_eq_alloc (nal_cb_t *nal)
174 /* NEVER called with statelock held */
177 PORTAL_ALLOC(eq, sizeof(*eq));
182 lib_eq_free (nal_cb_t *nal, lib_eq_t *eq)
184 /* ALWAYS called with statelock held */
185 PORTAL_FREE(eq, sizeof(*eq));
188 static inline lib_md_t *
189 lib_md_alloc (nal_cb_t *nal, ptl_md_t *umd)
191 /* NEVER called with statelock held */
196 if ((umd->options & PTL_MD_KIOV) != 0) {
198 size = offsetof(lib_md_t, md_iov.kiov[niov]);
200 niov = ((umd->options & PTL_MD_IOVEC) != 0) ?
202 size = offsetof(lib_md_t, md_iov.iov[niov]);
205 PORTAL_ALLOC(md, size);
208 /* Set here in case of early free */
209 md->options = umd->options;
217 lib_md_free (nal_cb_t *nal, lib_md_t *md)
219 /* ALWAYS called with statelock held */
222 if ((md->options & PTL_MD_KIOV) != 0)
223 size = offsetof(lib_md_t, md_iov.kiov[md->md_niov]);
225 size = offsetof(lib_md_t, md_iov.iov[md->md_niov]);
227 PORTAL_FREE(md, size);
230 static inline lib_me_t *
231 lib_me_alloc (nal_cb_t *nal)
233 /* NEVER called with statelock held */
236 PORTAL_ALLOC(me, sizeof(*me));
241 lib_me_free(nal_cb_t *nal, lib_me_t *me)
243 /* ALWAYS called with statelock held */
244 PORTAL_FREE(me, sizeof(*me));
247 static inline lib_msg_t *
248 lib_msg_alloc(nal_cb_t *nal)
250 /* NEVER called with statelock held; may be in interrupt... */
254 PORTAL_ALLOC_ATOMIC(msg, sizeof(*msg));
256 PORTAL_ALLOC(msg, sizeof(*msg));
259 /* NULL pointers, clear flags etc */
260 memset (msg, 0, sizeof (*msg));
261 msg->ack_wmd = PTL_WIRE_HANDLE_NONE;
267 lib_msg_free(nal_cb_t *nal, lib_msg_t *msg)
269 /* ALWAYS called with statelock held */
270 PORTAL_FREE(msg, sizeof(*msg));
274 extern lib_handle_t *lib_lookup_cookie (nal_cb_t *nal, __u64 cookie, int type);
275 extern void lib_initialise_handle (nal_cb_t *nal, lib_handle_t *lh, int type);
276 extern void lib_invalidate_handle (nal_cb_t *nal, lib_handle_t *lh);
279 ptl_eq2handle (ptl_handle_eq_t *handle, lib_eq_t *eq)
281 handle->cookie = eq->eq_lh.lh_cookie;
284 static inline lib_eq_t *
285 ptl_handle2eq (ptl_handle_eq_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_eq_t, eq_lh));
297 ptl_md2handle (ptl_handle_md_t *handle, lib_md_t *md)
299 handle->cookie = md->md_lh.lh_cookie;
302 static inline lib_md_t *
303 ptl_handle2md (ptl_handle_md_t *handle, nal_cb_t *nal)
305 /* ALWAYS called with statelock held */
306 lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
311 return (lh_entry (lh, lib_md_t, md_lh));
314 static inline lib_md_t *
315 ptl_wire_handle2md (ptl_handle_wire_t *wh, nal_cb_t *nal)
317 /* ALWAYS called with statelock held */
320 if (wh->wh_interface_cookie != nal->ni.ni_interface_cookie)
323 lh = lib_lookup_cookie (nal, wh->wh_object_cookie,
328 return (lh_entry (lh, lib_md_t, md_lh));
332 ptl_me2handle (ptl_handle_me_t *handle, lib_me_t *me)
334 handle->cookie = me->me_lh.lh_cookie;
337 static inline lib_me_t *
338 ptl_handle2me (ptl_handle_me_t *handle, nal_cb_t *nal)
340 /* ALWAYS called with statelock held */
341 lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
346 return (lh_entry (lh, lib_me_t, me_lh));
349 extern int lib_init(nal_cb_t * cb, ptl_nid_t nid, ptl_pid_t pid, int gsize,
350 ptl_pt_index_t tbl_size, ptl_ac_index_t ac_size);
351 extern int lib_fini(nal_cb_t * cb);
352 extern void lib_dispatch(nal_cb_t * cb, void *private, int index,
353 void *arg_block, void *ret_block);
354 extern char *dispatch_name(int index);
357 * When the NAL detects an incoming message, it should call
358 * lib_parse() decode it. The NAL callbacks will be handed
359 * the private cookie as a way for the NAL to maintain state
360 * about which transaction is being processed. An extra parameter,
361 * lib_cookie will contain the necessary information for
362 * finalizing the message.
364 * After it has finished the handling the message, it should
365 * call lib_finalize() with the lib_cookie parameter.
366 * Call backs will be made to write events, send acks or
369 extern void lib_enq_event_locked (nal_cb_t *nal, void *private,
370 lib_eq_t *eq, ptl_event_t *ev);
371 extern void lib_finalize (nal_cb_t *nal, void *private, lib_msg_t *msg,
372 ptl_ni_fail_t ni_fail_type);
373 extern void lib_parse (nal_cb_t *nal, ptl_hdr_t *hdr, void *private);
374 extern lib_msg_t *lib_create_reply_msg (nal_cb_t *nal, ptl_nid_t peer_nid,
376 extern void print_hdr (nal_cb_t * nal, ptl_hdr_t * hdr);
379 extern ptl_size_t lib_iov_nob (int niov, struct iovec *iov);
380 extern void lib_copy_iov2buf (char *dest, int niov, struct iovec *iov,
381 ptl_size_t offset, ptl_size_t len);
382 extern void lib_copy_buf2iov (int niov, struct iovec *iov, ptl_size_t offset,
383 char *src, ptl_size_t len);
384 extern int lib_extract_iov (int dst_niov, struct iovec *dst,
385 int src_niov, struct iovec *src,
386 ptl_size_t offset, ptl_size_t len);
388 extern ptl_size_t lib_kiov_nob (int niov, ptl_kiov_t *iov);
389 extern void lib_copy_kiov2buf (char *dest, int niov, ptl_kiov_t *kiov,
390 ptl_size_t offset, ptl_size_t len);
391 extern void lib_copy_buf2kiov (int niov, ptl_kiov_t *kiov, ptl_size_t offset,
392 char *src, ptl_size_t len);
393 extern int lib_extract_kiov (int dst_niov, ptl_kiov_t *dst,
394 int src_niov, ptl_kiov_t *src,
395 ptl_size_t offset, ptl_size_t len);
397 extern void lib_assert_wire_constants (void);
399 extern ptl_err_t lib_recv (nal_cb_t *nal, void *private, lib_msg_t *msg, lib_md_t *md,
400 ptl_size_t offset, ptl_size_t mlen, ptl_size_t rlen);
401 extern ptl_err_t lib_send (nal_cb_t *nal, void *private, lib_msg_t *msg,
402 ptl_hdr_t *hdr, int type, ptl_nid_t nid, ptl_pid_t pid,
403 lib_md_t *md, ptl_size_t offset, ptl_size_t len);
405 extern void lib_md_deconstruct(nal_cb_t * nal, lib_md_t * md_in,
407 extern void lib_md_unlink(nal_cb_t * nal, lib_md_t * md_in);
408 extern void lib_me_unlink(nal_cb_t * nal, lib_me_t * me_in);