Whamcloud - gitweb
3582b944e7076594ba5d7aaf8869789689b11d28
[fs/lustre-release.git] / lnet / include / lnet / 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 #ifdef PTL_USE_DESC_LISTS
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 atomic_t      md_in_use_count;
159 extern atomic_t      msg_in_use_count;
160 extern atomic_t      me_in_use_count;
161 extern atomic_t      eq_in_use_count;
162
163 static inline lib_eq_t *
164 lib_eq_alloc (nal_cb_t *nal)
165 {
166         /* NEVER called with statelock held */
167         lib_eq_t *eq;
168         PORTAL_ALLOC(eq, sizeof(*eq));
169
170         if (eq == NULL)
171                 return (NULL);
172
173         atomic_inc (&eq_in_use_count);
174         return (eq);
175 }
176
177 static inline void
178 lib_eq_free (nal_cb_t *nal, lib_eq_t *eq)
179 {
180         /* ALWAYS called with statelock held */
181         atomic_dec (&eq_in_use_count);
182         PORTAL_FREE(eq, sizeof(*eq));
183 }
184
185 static inline lib_md_t *
186 lib_md_alloc (nal_cb_t *nal)
187 {
188         /* NEVER called with statelock held */
189         lib_md_t *md;
190         PORTAL_ALLOC(md, sizeof(*md));
191
192         if (md == NULL)
193                 return (NULL);
194
195         atomic_inc (&md_in_use_count);
196         return (md);
197 }
198
199 static inline void 
200 lib_md_free (nal_cb_t *nal, lib_md_t *md)
201 {
202         /* ALWAYS called with statelock held */
203         atomic_dec (&md_in_use_count);
204         PORTAL_FREE(md, sizeof(*md));
205 }
206
207 static inline lib_me_t *
208 lib_me_alloc (nal_cb_t *nal)
209 {
210         /* NEVER called with statelock held */
211         lib_me_t *me;
212         PORTAL_ALLOC(me, sizeof(*me));
213
214         if (me == NULL)
215                 return (NULL);
216
217         atomic_inc (&me_in_use_count);
218         return (me);
219 }
220
221 static inline void 
222 lib_me_free(nal_cb_t *nal, lib_me_t *me)
223 {
224         /* ALWAYS called with statelock held */
225         atomic_dec (&me_in_use_count);
226         PORTAL_FREE(me, sizeof(*me));
227 }
228
229 static inline lib_msg_t *
230 lib_msg_alloc(nal_cb_t *nal)
231 {
232         /* ALWAYS called with statelock held */
233         lib_msg_t *msg;
234         PORTAL_ALLOC_ATOMIC(msg, sizeof(*msg));
235
236         if (msg == NULL)
237                 return (NULL);
238         
239         atomic_inc (&msg_in_use_count);
240         return (msg);
241 }
242
243 static inline void 
244 lib_msg_free(nal_cb_t *nal, lib_msg_t *msg)
245 {
246         /* ALWAYS called with statelock held */
247         atomic_dec (&msg_in_use_count);
248         PORTAL_FREE(msg, sizeof(*msg));
249 }
250 #endif
251
252 extern lib_handle_t *lib_lookup_cookie (nal_cb_t *nal, __u64 cookie, int type);
253 extern void lib_initialise_handle (nal_cb_t *nal, lib_handle_t *lh, int type);
254 extern void lib_invalidate_handle (nal_cb_t *nal, lib_handle_t *lh);
255
256 static inline void
257 ptl_eq2handle (ptl_handle_eq_t *handle, lib_eq_t *eq)
258 {
259         handle->cookie = eq->eq_lh.lh_cookie;
260 }
261
262 static inline lib_eq_t *
263 ptl_handle2eq (ptl_handle_eq_t *handle, nal_cb_t *nal)
264 {
265         /* ALWAYS called with statelock held */
266         lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie, 
267                                               PTL_COOKIE_TYPE_EQ);
268         if (lh == NULL)
269                 return (NULL);
270
271         return (lh_entry (lh, lib_eq_t, eq_lh));
272 }
273
274 static inline void
275 ptl_md2handle (ptl_handle_md_t *handle, lib_md_t *md)
276 {
277         handle->cookie = md->md_lh.lh_cookie;
278 }
279
280 static inline lib_md_t *
281 ptl_handle2md (ptl_handle_md_t *handle, nal_cb_t *nal)
282 {
283         /* ALWAYS called with statelock held */
284         lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
285                                               PTL_COOKIE_TYPE_MD);
286         if (lh == NULL)
287                 return (NULL);
288
289         return (lh_entry (lh, lib_md_t, md_lh));
290 }
291
292 static inline lib_md_t *
293 ptl_wire_handle2md (ptl_handle_wire_t *wh, nal_cb_t *nal)
294 {
295         /* ALWAYS called with statelock held */
296         lib_handle_t *lh;
297         
298         if (wh->wh_interface_cookie != nal->ni.ni_interface_cookie)
299                 return (NULL);
300         
301         lh = lib_lookup_cookie (nal, wh->wh_object_cookie,
302                                 PTL_COOKIE_TYPE_MD);
303         if (lh == NULL)
304                 return (NULL);
305
306         return (lh_entry (lh, lib_md_t, md_lh));
307 }
308
309 static inline void
310 ptl_me2handle (ptl_handle_me_t *handle, lib_me_t *me)
311 {
312         handle->cookie = me->me_lh.lh_cookie;
313 }
314
315 static inline lib_me_t *
316 ptl_handle2me (ptl_handle_me_t *handle, nal_cb_t *nal)
317 {
318         /* ALWAYS called with statelock held */
319         lib_handle_t *lh = lib_lookup_cookie (nal, handle->cookie,
320                                               PTL_COOKIE_TYPE_ME);
321         if (lh == NULL)
322                 return (NULL);
323
324         return (lh_entry (lh, lib_me_t, me_lh));
325 }
326
327 extern int lib_init(nal_cb_t * cb, ptl_nid_t nid, ptl_pid_t pid, int gsize,
328                     ptl_pt_index_t tbl_size, ptl_ac_index_t ac_size);
329 extern int lib_fini(nal_cb_t * cb);
330 extern void lib_dispatch(nal_cb_t * cb, void *private, int index,
331                          void *arg_block, void *ret_block);
332 extern char *dispatch_name(int index);
333
334 /*
335  * When the NAL detects an incoming message, it should call
336  * lib_parse() decode it.  The NAL callbacks will be handed
337  * the private cookie as a way for the NAL to maintain state
338  * about which transaction is being processed.  An extra parameter,
339  * lib_cookie will contain the necessary information for
340  * finalizing the message.
341  *
342  * After it has finished the handling the message, it should
343  * call lib_finalize() with the lib_cookie parameter.
344  * Call backs will be made to write events, send acks or
345  * replies and so on.
346  */
347 extern int lib_parse(nal_cb_t * nal, ptl_hdr_t * hdr, void *private);
348 extern int lib_finalize(nal_cb_t * nal, void *private, lib_msg_t * msg);
349 extern lib_msg_t *lib_fake_reply_msg (nal_cb_t *nal, ptl_nid_t peer_nid, 
350                                       lib_md_t *getmd);
351 extern void print_hdr(nal_cb_t * nal, ptl_hdr_t * hdr);
352
353 extern ptl_size_t lib_iov_nob (int niov, struct iovec *iov);
354 extern void lib_copy_iov2buf (char *dest, int niov, struct iovec *iov, ptl_size_t len);
355 extern void lib_copy_buf2iov (int niov, struct iovec *iov, char *dest, ptl_size_t len);
356
357 extern ptl_size_t lib_kiov_nob (int niov, ptl_kiov_t *iov);
358 extern void lib_copy_kiov2buf (char *dest, int niov, ptl_kiov_t *iov, ptl_size_t len);
359 extern void lib_copy_buf2kiov (int niov, ptl_kiov_t *iov, char *src, ptl_size_t len);
360 extern void lib_assert_wire_constants (void);
361
362 extern void lib_recv (nal_cb_t *nal, void *private, lib_msg_t *msg, lib_md_t *md,
363                       ptl_size_t offset, ptl_size_t mlen, ptl_size_t rlen);
364 extern int lib_send (nal_cb_t *nal, void *private, lib_msg_t *msg,
365                      ptl_hdr_t *hdr, int type, ptl_nid_t nid, ptl_pid_t pid,
366                      lib_md_t *md, ptl_size_t offset, ptl_size_t len);
367
368 extern void lib_md_deconstruct(nal_cb_t * nal, lib_md_t * md_in,
369                                ptl_md_t * md_out);
370 extern void lib_md_unlink(nal_cb_t * nal, lib_md_t * md_in);
371 extern void lib_me_unlink(nal_cb_t * nal, lib_me_t * me_in);
372 #endif