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)
282 handle->nal_idx = nal->libnal_ni.ni_api->nal_handle.nal_idx;
283 handle->cookie = eq->eq_lh.lh_cookie;
286 static inline lib_eq_t *
287 ptl_handle2eq (ptl_handle_eq_t *handle, lib_nal_t *nal)
289 /* ALWAYS called with liblock held */
290 lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
295 return (lh_entry (lh, lib_eq_t, eq_lh));
299 ptl_md2handle (ptl_handle_md_t *handle, lib_nal_t *nal, lib_md_t *md)
301 handle->nal_idx = nal->libnal_ni.ni_api->nal_handle.nal_idx;
302 handle->cookie = md->md_lh.lh_cookie;
305 static inline lib_md_t *
306 ptl_handle2md (ptl_handle_md_t *handle, lib_nal_t *nal)
308 /* ALWAYS called with liblock held */
309 lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
314 return (lh_entry (lh, lib_md_t, md_lh));
317 static inline lib_md_t *
318 ptl_wire_handle2md (ptl_handle_wire_t *wh, lib_nal_t *nal)
320 /* ALWAYS called with liblock held */
323 if (wh->wh_interface_cookie != nal->libnal_ni.ni_interface_cookie)
326 lh = lib_lookup_cookie (nal, wh->wh_object_cookie,
331 return (lh_entry (lh, lib_md_t, md_lh));
335 ptl_me2handle (ptl_handle_me_t *handle, lib_nal_t *nal, lib_me_t *me)
337 handle->nal_idx = nal->libnal_ni.ni_api->nal_handle.nal_idx;
338 handle->cookie = me->me_lh.lh_cookie;
341 static inline lib_me_t *
342 ptl_handle2me (ptl_handle_me_t *handle, lib_nal_t *nal)
344 /* ALWAYS called with liblock held */
345 lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
350 return (lh_entry (lh, lib_me_t, me_lh));
353 extern int lib_init(lib_nal_t *libnal, nal_t *apinal,
354 ptl_process_id_t pid,
355 ptl_ni_limits_t *desired_limits,
356 ptl_ni_limits_t *actual_limits);
357 extern int lib_fini(lib_nal_t *libnal);
360 * When the NAL detects an incoming message header, it should call
361 * lib_parse() decode it. If the message header is garbage, lib_parse()
362 * returns immediately with failure, otherwise the NAL callbacks will be
363 * called to receive the message body. They are handed the private cookie
364 * as a way for the NAL to maintain state about which transaction is being
365 * processed. An extra parameter, lib_msg contains the lib-level message
366 * state for passing to lib_finalize() when the message body has been
369 extern void lib_enq_event_locked (lib_nal_t *nal, void *private,
370 lib_eq_t *eq, ptl_event_t *ev);
371 extern void lib_finalize (lib_nal_t *nal, void *private, lib_msg_t *msg,
372 ptl_ni_fail_t ni_fail_type);
373 extern ptl_err_t lib_parse (lib_nal_t *nal, ptl_hdr_t *hdr, void *private);
374 extern lib_msg_t *lib_create_reply_msg (lib_nal_t *nal, ptl_nid_t peer_nid,
376 extern void print_hdr (lib_nal_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 (lib_nal_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 (lib_nal_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 int lib_api_ni_status (nal_t *nal, ptl_sr_index_t sr_idx,
406 ptl_sr_value_t *status);
407 extern int lib_api_ni_dist (nal_t *nal, ptl_process_id_t *pid,
408 unsigned long *dist);
410 extern int lib_api_eq_alloc (nal_t *nal, ptl_size_t count,
411 ptl_eq_handler_t callback,
412 ptl_handle_eq_t *handle);
413 extern int lib_api_eq_free(nal_t *nal, ptl_handle_eq_t *eqh);
414 extern int lib_api_eq_poll (nal_t *nal,
415 ptl_handle_eq_t *eventqs, int neq, int timeout_ms,
416 ptl_event_t *event, int *which);
418 extern int lib_api_me_attach(nal_t *nal,
419 ptl_pt_index_t portal,
420 ptl_process_id_t match_id,
421 ptl_match_bits_t match_bits,
422 ptl_match_bits_t ignore_bits,
423 ptl_unlink_t unlink, ptl_ins_pos_t pos,
424 ptl_handle_me_t *handle);
425 extern int lib_api_me_insert(nal_t *nal,
426 ptl_handle_me_t *current_meh,
427 ptl_process_id_t match_id,
428 ptl_match_bits_t match_bits,
429 ptl_match_bits_t ignore_bits,
430 ptl_unlink_t unlink, ptl_ins_pos_t pos,
431 ptl_handle_me_t *handle);
432 extern int lib_api_me_unlink (nal_t *nal, ptl_handle_me_t *meh);
433 extern void lib_me_unlink(lib_nal_t *nal, lib_me_t *me);
435 extern int lib_api_get_id(nal_t *nal, ptl_process_id_t *pid);
437 extern void lib_md_unlink(lib_nal_t *nal, lib_md_t *md);
438 extern void lib_md_deconstruct(lib_nal_t *nal, lib_md_t *lmd, ptl_md_t *umd);
439 extern int lib_api_md_attach(nal_t *nal, ptl_handle_me_t *meh,
440 ptl_md_t *umd, ptl_unlink_t unlink,
441 ptl_handle_md_t *handle);
442 extern int lib_api_md_bind(nal_t *nal, ptl_md_t *umd, ptl_unlink_t unlink,
443 ptl_handle_md_t *handle);
444 extern int lib_api_md_unlink (nal_t *nal, ptl_handle_md_t *mdh);
445 extern int lib_api_md_update (nal_t *nal, ptl_handle_md_t *mdh,
446 ptl_md_t *oldumd, ptl_md_t *newumd,
447 ptl_handle_eq_t *testqh);
449 extern int lib_api_get(nal_t *apinal, ptl_handle_md_t *mdh,
450 ptl_process_id_t *id,
451 ptl_pt_index_t portal, ptl_ac_index_t ac,
452 ptl_match_bits_t match_bits, ptl_size_t offset);
453 extern int lib_api_put(nal_t *apinal, ptl_handle_md_t *mdh,
454 ptl_ack_req_t ack, ptl_process_id_t *id,
455 ptl_pt_index_t portal, ptl_ac_index_t ac,
456 ptl_match_bits_t match_bits,
457 ptl_size_t offset, ptl_hdr_data_t hdr_data);
458 extern int lib_api_fail_nid(nal_t *apinal, ptl_nid_t nid, unsigned int threshold);