Whamcloud - gitweb
- merge 2 weeks of b1_4 fixes onto HEAD
[fs/lustre-release.git] / lustre / portals / include / portals / lib-p30.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * lib-p30.h
5  *
6  * Top level include for library side routines
7  */
8
9 #ifndef _LIB_P30_H_
10 #define _LIB_P30_H_
11
12 #include "build_check.h"
13
14 #ifdef __KERNEL__
15 # include <asm/page.h>
16 # include <linux/string.h>
17 #else
18 # include <portals/list.h>
19 # include <string.h>
20 # include <pthread.h>
21 #endif
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>
27
28 static inline int ptl_is_wire_handle_none (ptl_handle_wire_t *wh)
29 {
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);
32 }
33
34 #ifdef __KERNEL__
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)
39 #else
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)
44 #endif
45
46
47 #ifdef PTL_USE_LIB_FREELIST
48
49 #define MAX_MES         2048
50 #define MAX_MDS         2048
51 #define MAX_MSGS        2048    /* Outstanding messages */
52 #define MAX_EQS         512
53
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);
56
57 static inline void *
58 lib_freelist_alloc (lib_freelist_t *fl)
59 {
60         /* ALWAYS called with liblock held */
61         lib_freeobj_t *o;
62
63         if (list_empty (&fl->fl_list))
64                 return (NULL);
65         
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);
69 }
70
71 static inline void
72 lib_freelist_free (lib_freelist_t *fl, void *obj)
73 {
74         /* ALWAYS called with liblock held */
75         lib_freeobj_t *o = list_entry (obj, lib_freeobj_t, fo_contents);
76         
77         list_add (&o->fo_list, &fl->fl_list);
78 }
79
80
81 static inline lib_eq_t *
82 lib_eq_alloc (lib_nal_t *nal)
83 {
84         /* NEVER called with liblock held */
85         unsigned long  flags;
86         lib_eq_t      *eq;
87         
88         LIB_LOCK (nal, flags);
89         eq = (lib_eq_t *)lib_freelist_alloc (&nal->libnal_ni.ni_free_eqs);
90         LIB_UNLOCK (nal, flags);
91
92         return (eq);
93 }
94
95 static inline void
96 lib_eq_free (lib_nal_t *nal, lib_eq_t *eq)
97 {
98         /* ALWAYS called with liblock held */
99         lib_freelist_free (&nal->libnal_ni.ni_free_eqs, eq);
100 }
101
102 static inline lib_md_t *
103 lib_md_alloc (lib_nal_t *nal, ptl_md_t *umd)
104 {
105         /* NEVER called with liblock held */
106         unsigned long  flags;
107         lib_md_t      *md;
108         
109         LIB_LOCK (nal, flags);
110         md = (lib_md_t *)lib_freelist_alloc (&nal->libnal_ni.ni_free_mds);
111         LIB_UNLOCK (nal, flags);
112
113         return (md);
114 }
115
116 static inline void
117 lib_md_free (lib_nal_t *nal, lib_md_t *md)
118 {
119         /* ALWAYS called with liblock held */
120         lib_freelist_free (&nal->libnal_ni.ni_free_mds, md);
121 }
122
123 static inline lib_me_t *
124 lib_me_alloc (lib_nal_t *nal)
125 {
126         /* NEVER called with liblock held */
127         unsigned long  flags;
128         lib_me_t      *me;
129         
130         LIB_LOCK (nal, flags);
131         me = (lib_me_t *)lib_freelist_alloc (&nal->libnal_ni.ni_free_mes);
132         LIB_UNLOCK (nal, flags);
133         
134         return (me);
135 }
136
137 static inline void
138 lib_me_free (lib_nal_t *nal, lib_me_t *me)
139 {
140         /* ALWAYS called with liblock held */
141         lib_freelist_free (&nal->libnal_ni.ni_free_mes, me);
142 }
143
144 static inline lib_msg_t *
145 lib_msg_alloc (lib_nal_t *nal)
146 {
147         /* NEVER called with liblock held */
148         unsigned long  flags;
149         lib_msg_t     *msg;
150         
151         LIB_LOCK (nal, flags);
152         msg = (lib_msg_t *)lib_freelist_alloc (&nal->libnal_ni.ni_free_msgs);
153         LIB_UNLOCK (nal, flags);
154
155         if (msg != NULL) {
156                 /* NULL pointers, clear flags etc */
157                 memset (msg, 0, sizeof (*msg));
158                 msg->ack_wmd = PTL_WIRE_HANDLE_NONE;
159         }
160         return(msg);
161 }
162
163 static inline void
164 lib_msg_free (lib_nal_t *nal, lib_msg_t *msg)
165 {
166         /* ALWAYS called with liblock held */
167         lib_freelist_free (&nal->libnal_ni.ni_free_msgs, msg);
168 }
169
170 #else
171
172 static inline lib_eq_t *
173 lib_eq_alloc (lib_nal_t *nal)
174 {
175         /* NEVER called with liblock held */
176         lib_eq_t *eq;
177
178         PORTAL_ALLOC(eq, sizeof(*eq));
179         return (eq);
180 }
181
182 static inline void
183 lib_eq_free (lib_nal_t *nal, lib_eq_t *eq)
184 {
185         /* ALWAYS called with liblock held */
186         PORTAL_FREE(eq, sizeof(*eq));
187 }
188
189 static inline lib_md_t *
190 lib_md_alloc (lib_nal_t *nal, ptl_md_t *umd)
191 {
192         /* NEVER called with liblock held */
193         lib_md_t *md;
194         int       size;
195         int       niov;
196
197         if ((umd->options & PTL_MD_KIOV) != 0) {
198                 niov = umd->length;
199                 size = offsetof(lib_md_t, md_iov.kiov[niov]);
200         } else {
201                 niov = ((umd->options & PTL_MD_IOVEC) != 0) ?
202                        umd->length : 1;
203                 size = offsetof(lib_md_t, md_iov.iov[niov]);
204         }
205
206         PORTAL_ALLOC(md, size);
207
208         if (md != NULL) {
209                 /* Set here in case of early free */
210                 md->options = umd->options;
211                 md->md_niov = niov;
212         }
213         
214         return (md);
215 }
216
217 static inline void 
218 lib_md_free (lib_nal_t *nal, lib_md_t *md)
219 {
220         /* ALWAYS called with liblock held */
221         int       size;
222
223         if ((md->options & PTL_MD_KIOV) != 0)
224                 size = offsetof(lib_md_t, md_iov.kiov[md->md_niov]);
225         else
226                 size = offsetof(lib_md_t, md_iov.iov[md->md_niov]);
227
228         PORTAL_FREE(md, size);
229 }
230
231 static inline lib_me_t *
232 lib_me_alloc (lib_nal_t *nal)
233 {
234         /* NEVER called with liblock held */
235         lib_me_t *me;
236
237         PORTAL_ALLOC(me, sizeof(*me));
238         return (me);
239 }
240
241 static inline void 
242 lib_me_free(lib_nal_t *nal, lib_me_t *me)
243 {
244         /* ALWAYS called with liblock held */
245         PORTAL_FREE(me, sizeof(*me));
246 }
247
248 static inline lib_msg_t *
249 lib_msg_alloc(lib_nal_t *nal)
250 {
251         /* NEVER called with liblock held; may be in interrupt... */
252         lib_msg_t *msg;
253
254         if (in_interrupt())
255                 PORTAL_ALLOC_ATOMIC(msg, sizeof(*msg));
256         else
257                 PORTAL_ALLOC(msg, sizeof(*msg));
258
259         if (msg != NULL) {
260                 /* NULL pointers, clear flags etc */
261                 memset (msg, 0, sizeof (*msg));
262                 msg->ack_wmd = PTL_WIRE_HANDLE_NONE;
263         }
264         return (msg);
265 }
266
267 static inline void 
268 lib_msg_free(lib_nal_t *nal, lib_msg_t *msg)
269 {
270         /* ALWAYS called with liblock held */
271         PORTAL_FREE(msg, sizeof(*msg));
272 }
273 #endif
274
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);
278
279 static inline void
280 ptl_eq2handle (ptl_handle_eq_t *handle, lib_nal_t *nal, lib_eq_t *eq)
281 {
282         handle->nal_idx = nal->libnal_ni.ni_api->nal_handle.nal_idx;
283         handle->cookie = eq->eq_lh.lh_cookie;
284 }
285
286 static inline lib_eq_t *
287 ptl_handle2eq (ptl_handle_eq_t *handle, lib_nal_t *nal)
288 {
289         /* ALWAYS called with liblock held */
290         lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie, 
291                                               PTL_COOKIE_TYPE_EQ);
292         if (lh == NULL)
293                 return (NULL);
294
295         return (lh_entry (lh, lib_eq_t, eq_lh));
296 }
297
298 static inline void
299 ptl_md2handle (ptl_handle_md_t *handle, lib_nal_t *nal, lib_md_t *md)
300 {
301         handle->nal_idx = nal->libnal_ni.ni_api->nal_handle.nal_idx;
302         handle->cookie = md->md_lh.lh_cookie;
303 }
304
305 static inline lib_md_t *
306 ptl_handle2md (ptl_handle_md_t *handle, lib_nal_t *nal)
307 {
308         /* ALWAYS called with liblock held */
309         lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
310                                               PTL_COOKIE_TYPE_MD);
311         if (lh == NULL)
312                 return (NULL);
313
314         return (lh_entry (lh, lib_md_t, md_lh));
315 }
316
317 static inline lib_md_t *
318 ptl_wire_handle2md (ptl_handle_wire_t *wh, lib_nal_t *nal)
319 {
320         /* ALWAYS called with liblock held */
321         lib_handle_t *lh;
322         
323         if (wh->wh_interface_cookie != nal->libnal_ni.ni_interface_cookie)
324                 return (NULL);
325         
326         lh = lib_lookup_cookie (nal, wh->wh_object_cookie,
327                                 PTL_COOKIE_TYPE_MD);
328         if (lh == NULL)
329                 return (NULL);
330
331         return (lh_entry (lh, lib_md_t, md_lh));
332 }
333
334 static inline void
335 ptl_me2handle (ptl_handle_me_t *handle, lib_nal_t *nal, lib_me_t *me)
336 {
337         handle->nal_idx = nal->libnal_ni.ni_api->nal_handle.nal_idx;
338         handle->cookie = me->me_lh.lh_cookie;
339 }
340
341 static inline lib_me_t *
342 ptl_handle2me (ptl_handle_me_t *handle, lib_nal_t *nal)
343 {
344         /* ALWAYS called with liblock held */
345         lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
346                                               PTL_COOKIE_TYPE_ME);
347         if (lh == NULL)
348                 return (NULL);
349
350         return (lh_entry (lh, lib_me_t, me_lh));
351 }
352
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);
358
359 /*
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
367  * received.
368  */
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, 
375                                         lib_msg_t *get_msg);
376 extern void print_hdr (lib_nal_t * nal, ptl_hdr_t * hdr);
377
378
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);
387
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);
396
397 extern void lib_assert_wire_constants (void);
398
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);
404
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);
409
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);
417
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);
434
435 extern int lib_api_get_id(nal_t *nal, ptl_process_id_t *pid);
436
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);
448
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);
459
460 #endif