Whamcloud - gitweb
- merge 0.7rc1 from b_devel to HEAD (20030612 merge point)
[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 #ifdef __KERNEL__
13 # include <asm/page.h>
14 # include <linux/string.h>
15 #else
16 # include <portals/list.h>
17 # include <string.h>
18 #endif
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>
26
27 static inline int ptl_is_wire_handle_none (ptl_handle_wire_t *wh)
28 {
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);
31 }
32
33 #ifdef __KERNEL__
34 #define state_lock(nal,flagsp)                          \
35 do {                                                    \
36         CDEBUG(D_PORTALS, "taking state lock\n");       \
37         nal->cb_cli(nal, flagsp);                       \
38 } while (0)
39
40 #define state_unlock(nal,flagsp)                        \
41 {                                                       \
42         CDEBUG(D_PORTALS, "releasing state lock\n");    \
43         nal->cb_sti(nal, flagsp);                       \
44 }
45 #else
46 /* not needed in user space until we thread there */
47 #define state_lock(nal,flagsp)                          \
48 do {                                                    \
49         CDEBUG(D_PORTALS, "taking state lock\n");       \
50         CDEBUG(D_PORTALS, "%p:%p\n", nal, flagsp);      \
51 } while (0)
52
53 #define state_unlock(nal,flagsp)                        \
54 {                                                       \
55         CDEBUG(D_PORTALS, "releasing state lock\n");    \
56         CDEBUG(D_PORTALS, "%p:%p\n", nal, flagsp);      \
57 }
58 #endif /* __KERNEL__ */
59
60 #ifndef PTL_USE_SLAB_CACHE
61
62 #define MAX_MES         2048
63 #define MAX_MDS         2048
64 #define MAX_MSGS        2048    /* Outstanding messages */
65 #define MAX_EQS         512
66
67 extern int lib_freelist_init (nal_cb_t *nal, lib_freelist_t *fl, int nobj, int objsize);
68 extern void lib_freelist_fini (nal_cb_t *nal, lib_freelist_t *fl);
69
70 static inline void *
71 lib_freelist_alloc (lib_freelist_t *fl)
72 {
73         /* ALWAYS called with statelock held */
74         lib_freeobj_t *o;
75
76         if (list_empty (&fl->fl_list))
77                 return (NULL);
78         
79         o = list_entry (fl->fl_list.next, lib_freeobj_t, fo_list);
80         list_del (&o->fo_list);
81         return ((void *)&o->fo_contents);
82 }
83
84 static inline void
85 lib_freelist_free (lib_freelist_t *fl, void *obj)
86 {
87         /* ALWAYS called with statelock held */
88         lib_freeobj_t *o = list_entry (obj, lib_freeobj_t, fo_contents);
89         
90         list_add (&o->fo_list, &fl->fl_list);
91 }
92
93
94 static inline lib_eq_t *
95 lib_eq_alloc (nal_cb_t *nal)
96 {
97         /* NEVER called with statelock held */
98         unsigned long  flags;
99         lib_eq_t      *eq;
100         
101         state_lock (nal, &flags);
102         eq = (lib_eq_t *)lib_freelist_alloc (&nal->ni.ni_free_eqs);
103         state_unlock (nal, &flags);
104
105         return (eq);
106 }
107
108 static inline void
109 lib_eq_free (nal_cb_t *nal, lib_eq_t *eq)
110 {
111         /* ALWAYS called with statelock held */
112         lib_freelist_free (&nal->ni.ni_free_eqs, eq);
113 }
114
115 static inline lib_md_t *
116 lib_md_alloc (nal_cb_t *nal)
117 {
118         /* NEVER called with statelock held */
119         unsigned long  flags;
120         lib_md_t      *md;
121         
122         state_lock (nal, &flags);
123         md = (lib_md_t *)lib_freelist_alloc (&nal->ni.ni_free_mds);
124         state_unlock (nal, &flags);
125
126         return (md);
127 }
128
129 static inline void
130 lib_md_free (nal_cb_t *nal, lib_md_t *md)
131 {
132         /* ALWAYS called with statelock held */
133         lib_freelist_free (&nal->ni.ni_free_mds, md);
134 }
135
136 static inline lib_me_t *
137 lib_me_alloc (nal_cb_t *nal)
138 {
139         /* NEVER called with statelock held */
140         unsigned long  flags;
141         lib_me_t      *me;
142         
143         state_lock (nal, &flags);
144         me = (lib_me_t *)lib_freelist_alloc (&nal->ni.ni_free_mes);
145         state_unlock (nal, &flags);
146         
147         return (me);
148 }
149
150 static inline void
151 lib_me_free (nal_cb_t *nal, lib_me_t *me)
152 {
153         /* ALWAYS called with statelock held */
154         lib_freelist_free (&nal->ni.ni_free_mes, me);
155 }
156
157 static inline lib_msg_t *
158 lib_msg_alloc (nal_cb_t *nal)
159 {
160         /* ALWAYS called with statelock held */
161         return ((lib_msg_t *)lib_freelist_alloc (&nal->ni.ni_free_msgs));
162 }
163
164 static inline void
165 lib_msg_free (nal_cb_t *nal, lib_msg_t *msg)
166 {
167         /* ALWAYS called with statelock held */
168         lib_freelist_free (&nal->ni.ni_free_msgs, msg);
169 }
170
171 #else
172
173 extern kmem_cache_t *ptl_md_slab;
174 extern kmem_cache_t *ptl_msg_slab;
175 extern kmem_cache_t *ptl_me_slab;
176 extern kmem_cache_t *ptl_eq_slab;
177 extern atomic_t      md_in_use_count;
178 extern atomic_t      msg_in_use_count;
179 extern atomic_t      me_in_use_count;
180 extern atomic_t      eq_in_use_count;
181
182 static inline lib_eq_t *
183 lib_eq_alloc (nal_cb_t *nal)
184 {
185         /* NEVER called with statelock held */
186         lib_eq_t *eq = kmem_cache_alloc(ptl_eq_slab, GFP_NOFS);
187
188         if (eq == NULL)
189                 return (NULL);
190
191         atomic_inc (&eq_in_use_count);
192         return (eq);
193 }
194
195 static inline void
196 lib_eq_free (nal_cb_t *nal, lib_eq_t *eq)
197 {
198         /* ALWAYS called with statelock held */
199         atomic_dec (&eq_in_use_count);
200         kmem_cache_free(ptl_eq_slab, eq);
201 }
202
203 static inline lib_md_t *
204 lib_md_alloc (nal_cb_t *nal)
205 {
206         /* NEVER called with statelock held */
207         lib_md_t *md = kmem_cache_alloc(ptl_md_slab, GFP_NOFS);
208
209         if (md == NULL)
210                 return (NULL);
211
212         atomic_inc (&md_in_use_count);
213         return (md);
214 }
215
216 static inline void 
217 lib_md_free (nal_cb_t *nal, lib_md_t *md)
218 {
219         /* ALWAYS called with statelock held */
220         atomic_dec (&md_in_use_count);
221         kmem_cache_free(ptl_md_slab, md); 
222 }
223
224 static inline lib_me_t *
225 lib_me_alloc (nal_cb_t *nal)
226 {
227         /* NEVER called with statelock held */
228         lib_me_t *me = kmem_cache_alloc(ptl_me_slab, GFP_NOFS);
229
230         if (me == NULL)
231                 return (NULL);
232
233         atomic_inc (&me_in_use_count);
234         return (me);
235 }
236
237 static inline void 
238 lib_me_free(nal_cb_t *nal, lib_me_t *me)
239 {
240         /* ALWAYS called with statelock held */
241         atomic_dec (&me_in_use_count);
242         kmem_cache_free(ptl_me_slab, me);
243 }
244
245 static inline lib_msg_t *
246 lib_msg_alloc(nal_cb_t *nal)
247 {
248         /* ALWAYS called with statelock held */
249         lib_msg_t *msg = kmem_cache_alloc(ptl_msg_slab, GFP_ATOMIC);
250
251         if (msg == NULL)
252                 return (NULL);
253         
254         atomic_inc (&msg_in_use_count);
255         return (msg);
256 }
257
258 static inline void 
259 lib_msg_free(nal_cb_t *nal, lib_msg_t *msg)
260 {
261         /* ALWAYS called with statelock held */
262         atomic_dec (&msg_in_use_count);
263         kmem_cache_free(ptl_msg_slab, msg); 
264 }
265 #endif
266
267 extern lib_handle_t *lib_lookup_cookie (nal_cb_t *nal, __u64 cookie, int type);
268 extern void lib_initialise_handle (nal_cb_t *nal, lib_handle_t *lh, int type);
269 extern void lib_invalidate_handle (nal_cb_t *nal, lib_handle_t *lh);
270
271 static inline void
272 ptl_eq2handle (ptl_handle_eq_t *handle, lib_eq_t *eq)
273 {
274         handle->cookie = eq->eq_lh.lh_cookie;
275 }
276
277 static inline lib_eq_t *
278 ptl_handle2eq (ptl_handle_eq_t *handle, nal_cb_t *nal)
279 {
280         /* ALWAYS called with statelock held */
281         lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie, 
282                                               PTL_COOKIE_TYPE_EQ);
283         if (lh == NULL)
284                 return (NULL);
285
286         return (lh_entry (lh, lib_eq_t, eq_lh));
287 }
288
289 static inline void
290 ptl_md2handle (ptl_handle_md_t *handle, lib_md_t *md)
291 {
292         handle->cookie = md->md_lh.lh_cookie;
293 }
294
295 static inline lib_md_t *
296 ptl_handle2md (ptl_handle_md_t *handle, nal_cb_t *nal)
297 {
298         /* ALWAYS called with statelock held */
299         lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
300                                               PTL_COOKIE_TYPE_MD);
301         if (lh == NULL)
302                 return (NULL);
303
304         return (lh_entry (lh, lib_md_t, md_lh));
305 }
306
307 static inline lib_md_t *
308 ptl_wire_handle2md (ptl_handle_wire_t *wh, nal_cb_t *nal)
309 {
310         /* ALWAYS called with statelock held */
311         lib_handle_t *lh;
312         
313         if (wh->wh_interface_cookie != nal->ni.ni_interface_cookie)
314                 return (NULL);
315         
316         lh = lib_lookup_cookie (nal, wh->wh_object_cookie,
317                                 PTL_COOKIE_TYPE_MD);
318         if (lh == NULL)
319                 return (NULL);
320
321         return (lh_entry (lh, lib_md_t, md_lh));
322 }
323
324 static inline void
325 ptl_me2handle (ptl_handle_me_t *handle, lib_me_t *me)
326 {
327         handle->cookie = me->me_lh.lh_cookie;
328 }
329
330 static inline lib_me_t *
331 ptl_handle2me (ptl_handle_me_t *handle, nal_cb_t *nal)
332 {
333         /* ALWAYS called with statelock held */
334         lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
335                                               PTL_COOKIE_TYPE_ME);
336         if (lh == NULL)
337                 return (NULL);
338
339         return (lh_entry (lh, lib_me_t, me_lh));
340 }
341
342 extern int lib_init(nal_cb_t * cb, ptl_nid_t nid, ptl_pid_t pid, int gsize,
343                     ptl_pt_index_t tbl_size, ptl_ac_index_t ac_size);
344 extern int lib_fini(nal_cb_t * cb);
345 extern void lib_dispatch(nal_cb_t * cb, void *private, int index,
346                          void *arg_block, void *ret_block);
347 extern char *dispatch_name(int index);
348
349 /*
350  * When the NAL detects an incoming message, it should call
351  * lib_parse() decode it.  The NAL callbacks will be handed
352  * the private cookie as a way for the NAL to maintain state
353  * about which transaction is being processed.  An extra parameter,
354  * lib_cookie will contain the necessary information for
355  * finalizing the message.
356  *
357  * After it has finished the handling the message, it should
358  * call lib_finalize() with the lib_cookie parameter.
359  * Call backs will be made to write events, send acks or
360  * replies and so on.
361  */
362 extern int lib_parse(nal_cb_t * nal, ptl_hdr_t * hdr, void *private);
363 extern int lib_finalize(nal_cb_t * nal, void *private, lib_msg_t * msg);
364 extern void print_hdr(nal_cb_t * nal, ptl_hdr_t * hdr);
365
366 extern ptl_size_t lib_iov_nob (int niov, struct iovec *iov);
367 extern void lib_copy_iov2buf (char *dest, int niov, struct iovec *iov, ptl_size_t len);
368 extern void lib_copy_buf2iov (int niov, struct iovec *iov, char *dest, ptl_size_t len);
369
370 extern ptl_size_t lib_kiov_nob (int niov, ptl_kiov_t *iov);
371 extern void lib_copy_kiov2buf (char *dest, int niov, ptl_kiov_t *iov, ptl_size_t len);
372 extern void lib_copy_buf2kiov (int niov, ptl_kiov_t *iov, char *src, ptl_size_t len);
373 extern void lib_assert_wire_constants (void);
374
375 extern void lib_recv (nal_cb_t *nal, void *private, lib_msg_t *msg, lib_md_t *md,
376                       ptl_size_t offset, ptl_size_t mlen, ptl_size_t rlen);
377 extern int lib_send (nal_cb_t *nal, void *private, lib_msg_t *msg,
378                      ptl_hdr_t *hdr, int type, ptl_nid_t nid, ptl_pid_t pid,
379                      lib_md_t *md, ptl_size_t offset, ptl_size_t len);
380
381 extern void lib_md_deconstruct(nal_cb_t * nal, lib_md_t * md_in,
382                                ptl_md_t * md_out);
383 extern void lib_md_unlink(nal_cb_t * nal, lib_md_t * md_in);
384 extern void lib_me_unlink(nal_cb_t * nal, lib_me_t * me_in);
385 #endif