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>
22 #include <portals/types.h>
23 #include <linux/kp30.h>
24 #include <portals/p30.h>
25 #include <portals/nal.h>
26 #include <portals/lib-types.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);
35 #define LIB_LOCK(nal,flags) \
36 spin_lock_irqsave(&(nal)->libnal_ni.ni_lock, flags)
37 #define LIB_UNLOCK(nal,flags) \
38 spin_unlock_irqrestore(&(nal)->libnal_ni.ni_lock, flags)
40 #define LIB_LOCK(nal,flags) \
41 (pthread_mutex_lock(&(nal)->libnal_ni.ni_mutex), (flags) = 0)
42 #define LIB_UNLOCK(nal,flags) \
43 pthread_mutex_unlock(&(nal)->libnal_ni.ni_mutex)
47 #ifdef PTL_USE_LIB_FREELIST
51 #define MAX_MSGS 2048 /* Outstanding messages */
54 extern int lib_freelist_init (lib_nal_t *nal, lib_freelist_t *fl, int nobj, int objsize);
55 extern void lib_freelist_fini (lib_nal_t *nal, lib_freelist_t *fl);
58 lib_freelist_alloc (lib_freelist_t *fl)
60 /* ALWAYS called with liblock held */
63 if (list_empty (&fl->fl_list))
66 o = list_entry (fl->fl_list.next, lib_freeobj_t, fo_list);
67 list_del (&o->fo_list);
68 return ((void *)&o->fo_contents);
72 lib_freelist_free (lib_freelist_t *fl, void *obj)
74 /* ALWAYS called with liblock held */
75 lib_freeobj_t *o = list_entry (obj, lib_freeobj_t, fo_contents);
77 list_add (&o->fo_list, &fl->fl_list);
81 static inline lib_eq_t *
82 lib_eq_alloc (lib_nal_t *nal)
84 /* NEVER called with liblock held */
88 LIB_LOCK (nal, flags);
89 eq = (lib_eq_t *)lib_freelist_alloc (&nal->libnal_ni.ni_free_eqs);
90 LIB_UNLOCK (nal, flags);
96 lib_eq_free (lib_nal_t *nal, lib_eq_t *eq)
98 /* ALWAYS called with liblock held */
99 lib_freelist_free (&nal->libnal_ni.ni_free_eqs, eq);
102 static inline lib_md_t *
103 lib_md_alloc (lib_nal_t *nal, ptl_md_t *umd)
105 /* NEVER called with liblock held */
109 LIB_LOCK (nal, flags);
110 md = (lib_md_t *)lib_freelist_alloc (&nal->libnal_ni.ni_free_mds);
111 LIB_UNLOCK (nal, flags);
117 lib_md_free (lib_nal_t *nal, lib_md_t *md)
119 /* ALWAYS called with liblock held */
120 lib_freelist_free (&nal->libnal_ni.ni_free_mds, md);
123 static inline lib_me_t *
124 lib_me_alloc (lib_nal_t *nal)
126 /* NEVER called with liblock held */
130 LIB_LOCK (nal, flags);
131 me = (lib_me_t *)lib_freelist_alloc (&nal->libnal_ni.ni_free_mes);
132 LIB_UNLOCK (nal, flags);
138 lib_me_free (lib_nal_t *nal, lib_me_t *me)
140 /* ALWAYS called with liblock held */
141 lib_freelist_free (&nal->libnal_ni.ni_free_mes, me);
144 static inline lib_msg_t *
145 lib_msg_alloc (lib_nal_t *nal)
147 /* NEVER called with liblock held */
151 LIB_LOCK (nal, flags);
152 msg = (lib_msg_t *)lib_freelist_alloc (&nal->libnal_ni.ni_free_msgs);
153 LIB_UNLOCK (nal, flags);
156 /* NULL pointers, clear flags etc */
157 memset (msg, 0, sizeof (*msg));
158 msg->ack_wmd = PTL_WIRE_HANDLE_NONE;
164 lib_msg_free (lib_nal_t *nal, lib_msg_t *msg)
166 /* ALWAYS called with liblock held */
167 lib_freelist_free (&nal->libnal_ni.ni_free_msgs, msg);
172 static inline lib_eq_t *
173 lib_eq_alloc (lib_nal_t *nal)
175 /* NEVER called with liblock held */
178 PORTAL_ALLOC(eq, sizeof(*eq));
183 lib_eq_free (lib_nal_t *nal, lib_eq_t *eq)
185 /* ALWAYS called with liblock held */
186 PORTAL_FREE(eq, sizeof(*eq));
189 static inline lib_md_t *
190 lib_md_alloc (lib_nal_t *nal, ptl_md_t *umd)
192 /* NEVER called with liblock held */
197 if ((umd->options & PTL_MD_KIOV) != 0) {
199 size = offsetof(lib_md_t, md_iov.kiov[niov]);
201 niov = ((umd->options & PTL_MD_IOVEC) != 0) ?
203 size = offsetof(lib_md_t, md_iov.iov[niov]);
206 PORTAL_ALLOC(md, size);
209 /* Set here in case of early free */
210 md->options = umd->options;
218 lib_md_free (lib_nal_t *nal, lib_md_t *md)
220 /* ALWAYS called with liblock held */
223 if ((md->options & PTL_MD_KIOV) != 0)
224 size = offsetof(lib_md_t, md_iov.kiov[md->md_niov]);
226 size = offsetof(lib_md_t, md_iov.iov[md->md_niov]);
228 PORTAL_FREE(md, size);
231 static inline lib_me_t *
232 lib_me_alloc (lib_nal_t *nal)
234 /* NEVER called with liblock held */
237 PORTAL_ALLOC(me, sizeof(*me));
242 lib_me_free(lib_nal_t *nal, lib_me_t *me)
244 /* ALWAYS called with liblock held */
245 PORTAL_FREE(me, sizeof(*me));
248 static inline lib_msg_t *
249 lib_msg_alloc(lib_nal_t *nal)
251 /* NEVER called with liblock held; may be in interrupt... */
255 PORTAL_ALLOC_ATOMIC(msg, sizeof(*msg));
257 PORTAL_ALLOC(msg, sizeof(*msg));
260 /* NULL pointers, clear flags etc */
261 memset (msg, 0, sizeof (*msg));
262 msg->ack_wmd = PTL_WIRE_HANDLE_NONE;
268 lib_msg_free(lib_nal_t *nal, lib_msg_t *msg)
270 /* ALWAYS called with liblock held */
271 PORTAL_FREE(msg, sizeof(*msg));
275 extern lib_handle_t *lib_lookup_cookie (lib_nal_t *nal, __u64 cookie, int type);
276 extern void lib_initialise_handle (lib_nal_t *nal, lib_handle_t *lh, int type);
277 extern void lib_invalidate_handle (lib_nal_t *nal, lib_handle_t *lh);
280 ptl_eq2handle (ptl_handle_eq_t *handle, lib_nal_t *nal, lib_eq_t *eq)
283 *handle = PTL_EQ_NONE;
287 handle->nal_idx = nal->libnal_ni.ni_api->nal_handle.nal_idx;
288 handle->cookie = eq->eq_lh.lh_cookie;
291 static inline lib_eq_t *
292 ptl_handle2eq (ptl_handle_eq_t *handle, lib_nal_t *nal)
294 /* ALWAYS called with liblock held */
295 lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
300 return (lh_entry (lh, lib_eq_t, eq_lh));
304 ptl_md2handle (ptl_handle_md_t *handle, lib_nal_t *nal, lib_md_t *md)
306 handle->nal_idx = nal->libnal_ni.ni_api->nal_handle.nal_idx;
307 handle->cookie = md->md_lh.lh_cookie;
310 static inline lib_md_t *
311 ptl_handle2md (ptl_handle_md_t *handle, lib_nal_t *nal)
313 /* ALWAYS called with liblock held */
314 lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
319 return (lh_entry (lh, lib_md_t, md_lh));
322 static inline lib_md_t *
323 ptl_wire_handle2md (ptl_handle_wire_t *wh, lib_nal_t *nal)
325 /* ALWAYS called with liblock held */
328 if (wh->wh_interface_cookie != nal->libnal_ni.ni_interface_cookie)
331 lh = lib_lookup_cookie (nal, wh->wh_object_cookie,
336 return (lh_entry (lh, lib_md_t, md_lh));
340 ptl_me2handle (ptl_handle_me_t *handle, lib_nal_t *nal, lib_me_t *me)
342 handle->nal_idx = nal->libnal_ni.ni_api->nal_handle.nal_idx;
343 handle->cookie = me->me_lh.lh_cookie;
346 static inline lib_me_t *
347 ptl_handle2me (ptl_handle_me_t *handle, lib_nal_t *nal)
349 /* ALWAYS called with liblock held */
350 lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
355 return (lh_entry (lh, lib_me_t, me_lh));
358 extern int lib_init(lib_nal_t *libnal, nal_t *apinal,
359 ptl_process_id_t pid,
360 ptl_ni_limits_t *desired_limits,
361 ptl_ni_limits_t *actual_limits);
362 extern int lib_fini(lib_nal_t *libnal);
365 * When the NAL detects an incoming message header, it should call
366 * lib_parse() decode it. If the message header is garbage, lib_parse()
367 * returns immediately with failure, otherwise the NAL callbacks will be
368 * called to receive the message body. They are handed the private cookie
369 * as a way for the NAL to maintain state about which transaction is being
370 * processed. An extra parameter, lib_msg contains the lib-level message
371 * state for passing to lib_finalize() when the message body has been
374 extern void lib_enq_event_locked (lib_nal_t *nal, void *private,
375 lib_eq_t *eq, ptl_event_t *ev);
376 extern void lib_finalize (lib_nal_t *nal, void *private, lib_msg_t *msg,
377 ptl_ni_fail_t ni_fail_type);
378 extern ptl_err_t lib_parse (lib_nal_t *nal, ptl_hdr_t *hdr, void *private);
379 extern lib_msg_t *lib_create_reply_msg (lib_nal_t *nal, ptl_nid_t peer_nid,
381 extern void print_hdr (lib_nal_t * nal, ptl_hdr_t * hdr);
384 extern ptl_size_t lib_iov_nob (int niov, struct iovec *iov);
385 extern void lib_copy_iov2buf (char *dest, int niov, struct iovec *iov,
386 ptl_size_t offset, ptl_size_t len);
387 extern void lib_copy_buf2iov (int niov, struct iovec *iov, ptl_size_t offset,
388 char *src, ptl_size_t len);
389 extern int lib_extract_iov (int dst_niov, struct iovec *dst,
390 int src_niov, struct iovec *src,
391 ptl_size_t offset, ptl_size_t len);
393 extern ptl_size_t lib_kiov_nob (int niov, ptl_kiov_t *iov);
394 extern void lib_copy_kiov2buf (char *dest, int niov, ptl_kiov_t *kiov,
395 ptl_size_t offset, ptl_size_t len);
396 extern void lib_copy_buf2kiov (int niov, ptl_kiov_t *kiov, ptl_size_t offset,
397 char *src, ptl_size_t len);
398 extern int lib_extract_kiov (int dst_niov, ptl_kiov_t *dst,
399 int src_niov, ptl_kiov_t *src,
400 ptl_size_t offset, ptl_size_t len);
402 extern void lib_assert_wire_constants (void);
404 extern ptl_err_t lib_recv (lib_nal_t *nal, void *private, lib_msg_t *msg, lib_md_t *md,
405 ptl_size_t offset, ptl_size_t mlen, ptl_size_t rlen);
406 extern ptl_err_t lib_send (lib_nal_t *nal, void *private, lib_msg_t *msg,
407 ptl_hdr_t *hdr, int type, ptl_nid_t nid, ptl_pid_t pid,
408 lib_md_t *md, ptl_size_t offset, ptl_size_t len);
410 extern int lib_api_ni_status (nal_t *nal, ptl_sr_index_t sr_idx,
411 ptl_sr_value_t *status);
412 extern int lib_api_ni_dist (nal_t *nal, ptl_process_id_t *pid,
413 unsigned long *dist);
415 extern int lib_api_eq_alloc (nal_t *nal, ptl_size_t count,
416 ptl_eq_handler_t callback,
417 ptl_handle_eq_t *handle);
418 extern int lib_api_eq_free(nal_t *nal, ptl_handle_eq_t *eqh);
419 extern int lib_api_eq_poll (nal_t *nal,
420 ptl_handle_eq_t *eventqs, int neq, int timeout_ms,
421 ptl_event_t *event, int *which);
423 extern int lib_api_me_attach(nal_t *nal,
424 ptl_pt_index_t portal,
425 ptl_process_id_t match_id,
426 ptl_match_bits_t match_bits,
427 ptl_match_bits_t ignore_bits,
428 ptl_unlink_t unlink, ptl_ins_pos_t pos,
429 ptl_handle_me_t *handle);
430 extern int lib_api_me_insert(nal_t *nal,
431 ptl_handle_me_t *current_meh,
432 ptl_process_id_t match_id,
433 ptl_match_bits_t match_bits,
434 ptl_match_bits_t ignore_bits,
435 ptl_unlink_t unlink, ptl_ins_pos_t pos,
436 ptl_handle_me_t *handle);
437 extern int lib_api_me_unlink (nal_t *nal, ptl_handle_me_t *meh);
438 extern void lib_me_unlink(lib_nal_t *nal, lib_me_t *me);
440 extern int lib_api_get_id(nal_t *nal, ptl_process_id_t *pid);
442 extern void lib_md_unlink(lib_nal_t *nal, lib_md_t *md);
443 extern void lib_md_deconstruct(lib_nal_t *nal, lib_md_t *lmd, ptl_md_t *umd);
444 extern int lib_api_md_attach(nal_t *nal, ptl_handle_me_t *meh,
445 ptl_md_t *umd, ptl_unlink_t unlink,
446 ptl_handle_md_t *handle);
447 extern int lib_api_md_bind(nal_t *nal, ptl_md_t *umd, ptl_unlink_t unlink,
448 ptl_handle_md_t *handle);
449 extern int lib_api_md_unlink (nal_t *nal, ptl_handle_md_t *mdh);
450 extern int lib_api_md_update (nal_t *nal, ptl_handle_md_t *mdh,
451 ptl_md_t *oldumd, ptl_md_t *newumd,
452 ptl_handle_eq_t *testqh);
454 extern int lib_api_get(nal_t *apinal, ptl_handle_md_t *mdh,
455 ptl_process_id_t *id,
456 ptl_pt_index_t portal, ptl_ac_index_t ac,
457 ptl_match_bits_t match_bits, ptl_size_t offset);
458 extern int lib_api_put(nal_t *apinal, ptl_handle_md_t *mdh,
459 ptl_ack_req_t ack, ptl_process_id_t *id,
460 ptl_pt_index_t portal, ptl_ac_index_t ac,
461 ptl_match_bits_t match_bits,
462 ptl_size_t offset, ptl_hdr_data_t hdr_data);
463 extern int lib_api_fail_nid(nal_t *apinal, ptl_nid_t nid, unsigned int threshold);