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