Whamcloud - gitweb
* lctl set_route <nid> <up/down> enables or disables particular portals
[fs/lustre-release.git] / lustre / portals / include / linux / kp30.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4 #ifndef _KP30_INCLUDED
5 #define _KP30_INCLUDED
6
7 #define PORTAL_DEBUG
8
9 #ifndef offsetof
10 # define offsetof(typ,memb)     ((int)((char *)&(((typ *)0)->memb)))
11 #endif
12
13 #define LOWEST_BIT_SET(x)       ((x) & ~((x) - 1))
14
15 /*
16  *  Debugging
17  */
18 extern unsigned int portal_subsystem_debug;
19 extern unsigned int portal_stack;
20 extern unsigned int portal_debug;
21 extern unsigned int portal_printk;
22 /* Debugging subsystems (32 bits, non-overlapping) */
23 #define S_UNDEFINED    (1 << 0)
24 #define S_MDC          (1 << 1)
25 #define S_MDS          (1 << 2)
26 #define S_OSC          (1 << 3)
27 #define S_OST          (1 << 4)
28 #define S_CLASS        (1 << 5)
29 #define S_LOG          (1 << 6)
30 #define S_LLITE        (1 << 7)
31 #define S_RPC          (1 << 8)
32 #define S_MGMT         (1 << 9)
33 #define S_PORTALS     (1 << 10)
34 #define S_SOCKNAL     (1 << 11)
35 #define S_QSWNAL      (1 << 12)
36 #define S_PINGER      (1 << 13)
37 #define S_FILTER      (1 << 14)
38 #define S_PTLBD       (1 << 15)
39 #define S_ECHO        (1 << 16)
40 #define S_LDLM        (1 << 17)
41 #define S_LOV         (1 << 18)
42 #define S_GMNAL       (1 << 19)
43 #define S_PTLROUTER   (1 << 20)
44 #define S_COBD        (1 << 21)
45
46 /* If you change these values, please keep portals/utils/debug.c
47  * up to date! */
48
49 /* Debugging masks (32 bits, non-overlapping) */
50 #define D_TRACE     (1 << 0) /* ENTRY/EXIT markers */
51 #define D_INODE     (1 << 1)
52 #define D_SUPER     (1 << 2)
53 #define D_EXT2      (1 << 3) /* anything from ext2_debug */
54 #define D_MALLOC    (1 << 4) /* print malloc, free information */
55 #define D_CACHE     (1 << 5) /* cache-related items */
56 #define D_INFO      (1 << 6) /* general information */
57 #define D_IOCTL     (1 << 7) /* ioctl related information */
58 #define D_BLOCKS    (1 << 8) /* ext2 block allocation */
59 #define D_NET       (1 << 9) /* network communications */
60 #define D_WARNING   (1 << 10)
61 #define D_BUFFS     (1 << 11)
62 #define D_OTHER     (1 << 12)
63 #define D_DENTRY    (1 << 13)
64 #define D_PORTALS   (1 << 14) /* ENTRY/EXIT markers */
65 #define D_PAGE      (1 << 15) /* bulk page handling */
66 #define D_DLMTRACE  (1 << 16)
67 #define D_ERROR     (1 << 17) /* CERROR(...) == CDEBUG (D_ERROR, ...) */
68 #define D_EMERG     (1 << 18) /* CEMERG(...) == CDEBUG (D_EMERG, ...) */
69 #define D_HA        (1 << 19) /* recovery and failover */
70 #define D_RPCTRACE  (1 << 20) /* for distributed debugging */
71 #define D_VFSTRACE  (1 << 21)
72
73 #ifdef __KERNEL__
74 # include <linux/sched.h> /* THREAD_SIZE */
75 #else
76 # define THREAD_SIZE 8192
77 #endif
78
79 #ifdef __KERNEL__
80 # ifdef  __ia64__
81 #  define CDEBUG_STACK (THREAD_SIZE -                                      \
82                         ((unsigned long)__builtin_dwarf_cfa() &            \
83                          (THREAD_SIZE - 1)))
84 # else
85 #  define CDEBUG_STACK (THREAD_SIZE -                                      \
86                         ((unsigned long)__builtin_frame_address(0) &       \
87                          (THREAD_SIZE - 1)))
88 # endif
89
90 #define CHECK_STACK(stack)                                                    \
91         do {                                                                  \
92                 if ((stack) > 3*THREAD_SIZE/4 && (stack) > portal_stack) {    \
93                         portals_debug_msg(DEBUG_SUBSYSTEM, D_ERROR,           \
94                                           __FILE__, __FUNCTION__, __LINE__,   \
95                                           (stack),                            \
96                                           "maximum lustre stack %u\n",        \
97                                           portal_stack = (stack));            \
98                       /*panic("LBUG");*/                                      \
99                 }                                                             \
100         } while (0)
101 #else /* __KERNEL__ */
102 #define CHECK_STACK(stack) do { } while(0)
103 #define CDEBUG_STACK (0L)
104 #endif /* __KERNEL__ */
105
106 #if 1
107 #define CDEBUG(mask, format, a...)                                            \
108 do {                                                                          \
109         CHECK_STACK(CDEBUG_STACK);                                            \
110         if (!(mask) || ((mask) & (D_ERROR | D_EMERG)) ||                      \
111             (portal_debug & (mask) &&                                         \
112              portal_subsystem_debug & DEBUG_SUBSYSTEM))                       \
113                 portals_debug_msg(DEBUG_SUBSYSTEM, mask,                      \
114                                   __FILE__, __FUNCTION__, __LINE__,           \
115                                   CDEBUG_STACK, format, ## a);                \
116 } while (0)
117
118 #define CWARN(format, a...) CDEBUG(D_WARNING, format, ## a)
119 #define CERROR(format, a...) CDEBUG(D_ERROR, format, ## a)
120 #define CEMERG(format, a...) CDEBUG(D_EMERG, format, ## a)
121
122 #define GOTO(label, rc)                                                 \
123 do {                                                                    \
124         long GOTO__ret = (long)(rc);                                    \
125         CDEBUG(D_TRACE,"Process leaving via %s (rc=%lu : %ld : %lx)\n", \
126                #label, (unsigned long)GOTO__ret, (signed long)GOTO__ret,\
127                (signed long)GOTO__ret);                                 \
128         goto label;                                                     \
129 } while (0)
130
131 #define RETURN(rc)                                                      \
132 do {                                                                    \
133         typeof(rc) RETURN__ret = (rc);                                  \
134         CDEBUG(D_TRACE, "Process leaving (rc=%lu : %ld : %lx)\n",       \
135                (long)RETURN__ret, (long)RETURN__ret, (long)RETURN__ret);\
136         return RETURN__ret;                                             \
137 } while (0)
138
139 #define ENTRY                                                           \
140 do {                                                                    \
141         CDEBUG(D_TRACE, "Process entered\n");                           \
142 } while (0)
143
144 #define EXIT                                                            \
145 do {                                                                    \
146         CDEBUG(D_TRACE, "Process leaving\n");                           \
147 } while(0)
148 #else
149 #define CDEBUG(mask, format, a...)      do { } while (0)
150 #define CWARN(format, a...)             do { } while (0)
151 #define CERROR(format, a...)            printk("<3>" format, ## a)
152 #define CEMERG(format, a...)            printk("<0>" format, ## a)
153 #define GOTO(label, rc)                 do { (void)(rc); goto label; } while (0)
154 #define RETURN(rc)                      return (rc)
155 #define ENTRY                           do { } while (0)
156 #define EXIT                            do { } while (0)
157 #endif
158
159 #ifdef __KERNEL__
160 # include <linux/vmalloc.h>
161 # include <linux/time.h>
162 # include <linux/slab.h>
163 # include <linux/interrupt.h>
164 # include <linux/highmem.h>
165 # include <linux/module.h>
166 # include <linux/version.h>
167 # include <portals/lib-nal.h>
168 # include <linux/smp_lock.h>
169 # include <asm/atomic.h>
170
171 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
172 #define schedule_work schedule_task
173 #define prepare_work(wq,cb,cbdata)                                            \
174 do {                                                                          \
175         INIT_TQUEUE((wq), 0, 0);                                              \
176         PREPARE_TQUEUE((wq), (cb), (cbdata));                                 \
177 } while (0)
178
179 #define ll_invalidate_inode_pages invalidate_inode_pages
180 #define PageUptodate Page_Uptodate
181 #define our_recalc_sigpending(current) recalc_sigpending(current)
182 #define num_online_cpus() smp_num_cpus
183 static inline void our_cond_resched(void)
184 {
185         if (current->need_resched)
186                schedule ();
187 }
188
189 #else
190
191 #define prepare_work(wq,cb,cbdata)                                            \
192 do {                                                                          \
193         INIT_WORK((wq), (void *)(cb), (void *)(cbdata));                      \
194 } while (0)
195 #define ll_invalidate_inode_pages(inode) invalidate_inode_pages((inode)->i_mapping)
196 #define wait_on_page wait_on_page_locked
197 #define our_recalc_sigpending(current) recalc_sigpending()
198 #define strtok(a,b) strpbrk(a, b)
199 static inline void our_cond_resched(void)
200 {
201         cond_resched();
202 }
203 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) */
204
205 #ifdef PORTAL_DEBUG
206 extern void kportal_assertion_failed(char *expr, char *file, const char *func,
207                                      const int line);
208 #define LASSERT(e) ((e) ? 0 : kportal_assertion_failed( #e , __FILE__,  \
209                                                         __FUNCTION__, __LINE__))
210 #else
211 #define LASSERT(e)
212 #endif
213
214 #ifdef __arch_um__
215 #define LBUG_WITH_LOC(file, func, line)                                 \
216 do {                                                                    \
217         CEMERG("LBUG - trying to dump log to /tmp/lustre-log\n");       \
218         portals_debug_dumplog();                                        \
219         portals_run_lbug_upcall(file, func, line);                      \
220         panic("LBUG");                                                  \
221 } while (0)
222 #else
223 #define LBUG_WITH_LOC(file, func, line)                                 \
224 do {                                                                    \
225         CEMERG("LBUG\n");                                               \
226         portals_debug_dumplog();                                        \
227         portals_run_lbug_upcall(file, func, line);                      \
228         set_task_state(current, TASK_UNINTERRUPTIBLE);                  \
229         schedule();                                                     \
230 } while (0)
231 #endif /* __arch_um__ */
232
233 #define LBUG() LBUG_WITH_LOC(__FILE__, __FUNCTION__, __LINE__)
234
235 /*
236  * Memory
237  */
238 #ifdef PORTAL_DEBUG
239 extern atomic_t portal_kmemory;
240
241 # define portal_kmem_inc(ptr, size)                                           \
242 do {                                                                          \
243         atomic_add(size, &portal_kmemory);                                    \
244 } while (0)
245
246 # define portal_kmem_dec(ptr, size) do {                                      \
247         atomic_sub(size, &portal_kmemory);                                    \
248 } while (0)
249
250 #else
251 # define portal_kmem_inc(ptr, size) do {} while (0)
252 # define portal_kmem_dec(ptr, size) do {} while (0)
253 #endif /* PORTAL_DEBUG */
254
255 #define PORTAL_VMALLOC_SIZE        16384
256
257 #define PORTAL_ALLOC(ptr, size)                                           \
258 do {                                                                      \
259         LASSERT (!in_interrupt());                                        \
260         if ((size) > PORTAL_VMALLOC_SIZE)                                 \
261                 (ptr) = vmalloc(size);                                    \
262         else                                                              \
263                 (ptr) = kmalloc((size), GFP_NOFS);                        \
264         if ((ptr) == NULL)                                                \
265                 CERROR("PORTALS: out of memory at %s:%d (tried to alloc '"\
266                        #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size));\
267         else {                                                            \
268                 portal_kmem_inc((ptr), (size));                           \
269                 memset((ptr), 0, (size));                                 \
270         }                                                                 \
271         CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p (tot %d).\n",    \
272                (int)(size), (ptr), atomic_read (&portal_kmemory));        \
273 } while (0)
274
275 #define PORTAL_FREE(ptr, size)                                          \
276 do {                                                                    \
277         int s = (size);                                                 \
278         if ((ptr) == NULL) {                                            \
279                 CERROR("PORTALS: free NULL '" #ptr "' (%d bytes) at "   \
280                        "%s:%d\n", s, __FILE__, __LINE__);               \
281                 break;                                                  \
282         }                                                               \
283         if (s > PORTAL_VMALLOC_SIZE)                                    \
284                 vfree(ptr);                                             \
285         else                                                            \
286                 kfree(ptr);                                             \
287         portal_kmem_dec((ptr), s);                                      \
288         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n",     \
289                s, (ptr), atomic_read(&portal_kmemory));                 \
290 } while (0)
291
292 #define PORTAL_SLAB_ALLOC(ptr, slab, size)                                \
293 do {                                                                      \
294         LASSERT(!in_interrupt());                                         \
295         (ptr) = kmem_cache_alloc((slab), SLAB_KERNEL);                    \
296         if ((ptr) == NULL) {                                              \
297                 CERROR("PORTALS: out of memory at %s:%d (tried to alloc"  \
298                        " '" #ptr "' from slab '" #slab "')\n", __FILE__,  \
299                        __LINE__);                                         \
300         } else {                                                          \
301                 portal_kmem_inc((ptr), (size));                           \
302                 memset((ptr), 0, (size));                                 \
303         }                                                                 \
304         CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %ld at %p (tot %d).\n",   \
305                (int)(size), (ptr), atomic_read(&portal_kmemory));         \
306 } while (0)
307
308 #define PORTAL_SLAB_FREE(ptr, slab, size)                               \
309 do {                                                                    \
310         int s = (size);                                                 \
311         if ((ptr) == NULL) {                                            \
312                 CERROR("PORTALS: free NULL '" #ptr "' (%d bytes) at "   \
313                        "%s:%d\n", s, __FILE__, __LINE__);               \
314                 break;                                                  \
315         }                                                               \
316         memset((ptr), 0x5a, s);                                         \
317         kmem_cache_free((slab), ptr);                                   \
318         portal_kmem_dec((ptr), s);                                      \
319         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n",     \
320                s, (ptr), atomic_read (&portal_kmemory));                \
321 } while (0)
322
323 /* ------------------------------------------------------------------- */
324
325 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
326
327 #define PORTAL_SYMBOL_REGISTER(x) inter_module_register(#x, THIS_MODULE, &x)
328 #define PORTAL_SYMBOL_UNREGISTER(x) inter_module_unregister(#x)
329
330 #define PORTAL_SYMBOL_GET(x) ((typeof(&x))inter_module_get(#x))
331 #define PORTAL_SYMBOL_PUT(x) inter_module_put(#x)
332
333 #define PORTAL_MODULE_USE       MOD_INC_USE_COUNT
334 #define PORTAL_MODULE_UNUSE     MOD_DEC_USE_COUNT
335 #else
336
337 #define PORTAL_SYMBOL_REGISTER(x)
338 #define PORTAL_SYMBOL_UNREGISTER(x)
339
340 #define PORTAL_SYMBOL_GET(x) symbol_get(x)
341 #define PORTAL_SYMBOL_PUT(x) symbol_put(x)
342
343 #define PORTAL_MODULE_USE       try_module_get(THIS_MODULE)
344 #define PORTAL_MODULE_UNUSE     module_put(THIS_MODULE)
345
346 #endif
347
348 /******************************************************************************/
349 /* Kernel Portals Router interface */
350
351 typedef void (*kpr_fwd_callback_t)(void *arg, int error); // completion callback
352
353 /* space for routing targets to stash "stuff" in a forwarded packet */
354 typedef union {
355         long long        _alignment;
356         void            *_space[16];            /* scale with CPU arch */
357 } kprfd_scratch_t;
358
359 /* Kernel Portals Routing Forwarded message Descriptor */
360 typedef struct {
361         struct list_head     kprfd_list;        /* stash in queues (routing target can use) */
362         ptl_nid_t            kprfd_target_nid;  /* final destination NID */
363         ptl_nid_t            kprfd_gateway_nid; /* gateway NID */
364         int                  kprfd_nob;         /* # message bytes (including header) */
365         int                  kprfd_niov;        /* # message frags (including header) */
366         struct iovec        *kprfd_iov;         /* message fragments */
367         void                *kprfd_router_arg;  // originating NAL's router arg
368         kpr_fwd_callback_t   kprfd_callback;    /* completion callback */
369         void                *kprfd_callback_arg; /* completion callback arg */
370         kprfd_scratch_t      kprfd_scratch;    // scratchpad for routing targets
371 } kpr_fwd_desc_t;
372
373 typedef void  (*kpr_fwd_t)(void *arg, kpr_fwd_desc_t *fwd);
374 typedef void  (*kpr_notify_t)(void *arg, ptl_nid_t peer, int alive);
375
376 /* NAL's routing interface (Kernel Portals Routing Nal Interface) */
377 typedef const struct {
378         int             kprni_nalid;    /* NAL's id */
379         void           *kprni_arg;      /* Arg to pass when calling into NAL */
380         kpr_fwd_t       kprni_fwd;      /* NAL's forwarding entrypoint */
381         kpr_notify_t    kprni_notify;   /* NAL's notification entrypoint */
382 } kpr_nal_interface_t;
383
384 /* Router's routing interface (Kernel Portals Routing Router Interface) */
385 typedef const struct {
386         /* register the calling NAL with the router and get back the handle for
387          * subsequent calls */
388         int     (*kprri_register) (kpr_nal_interface_t *nal_interface,
389                                    void **router_arg);
390
391         /* ask the router to find a gateway that forwards to 'nid' and is a
392          * peer of the calling NAL; assume caller will send 'nob' bytes of
393          * payload there */
394         int     (*kprri_lookup) (void *router_arg, ptl_nid_t nid, int nob,
395                                  ptl_nid_t *gateway_nid);
396
397         /* hand a packet over to the router for forwarding */
398         kpr_fwd_t kprri_fwd_start;
399
400         /* hand a packet back to the router for completion */
401         void    (*kprri_fwd_done) (void *router_arg, kpr_fwd_desc_t *fwd,
402                                    int error);
403
404         /* notify the router about peer state */
405         void    (*kprri_notify) (void *router_arg, ptl_nid_t peer,
406                                  int alive, time_t when);
407
408         /* the calling NAL is shutting down */
409         void    (*kprri_shutdown) (void *router_arg);
410
411         /* deregister the calling NAL with the router */
412         void    (*kprri_deregister) (void *router_arg);
413
414 } kpr_router_interface_t;
415
416 /* Convenient struct for NAL to stash router interface/args */
417 typedef struct {
418         kpr_router_interface_t  *kpr_interface;
419         void                    *kpr_arg;
420 } kpr_router_t;
421
422 /* Router's control interface (Kernel Portals Routing Control Interface) */
423 typedef const struct {
424         int     (*kprci_add_route)(int gateway_nal, ptl_nid_t gateway_nid,
425                                    ptl_nid_t lo_nid, ptl_nid_t hi_nid);
426         int     (*kprci_del_route)(int gateway_nal, ptl_nid_t gateway_nid,
427                                    ptl_nid_t lo_nid, ptl_nid_t hi_nid);
428         int     (*kprci_get_route)(int index, int *gateway_nal,
429                                    ptl_nid_t *gateway,
430                                    ptl_nid_t *lo_nid, ptl_nid_t *hi_nid,
431                                    int *alive);
432         int     (*kprci_notify)(int gateway_nal, ptl_nid_t gateway_nid, 
433                                 int alive, time_t when);
434 } kpr_control_interface_t;
435
436 extern kpr_control_interface_t  kpr_control_interface;
437 extern kpr_router_interface_t   kpr_router_interface;
438
439 static inline int
440 kpr_register (kpr_router_t *router, kpr_nal_interface_t *nalif)
441 {
442         int    rc;
443
444         router->kpr_interface = PORTAL_SYMBOL_GET (kpr_router_interface);
445         if (router->kpr_interface == NULL)
446                 return (-ENOENT);
447
448         rc = (router->kpr_interface)->kprri_register (nalif, &router->kpr_arg);
449         if (rc != 0)
450                 router->kpr_interface = NULL;
451
452         PORTAL_SYMBOL_PUT (kpr_router_interface);
453         return (rc);
454 }
455
456 static inline int
457 kpr_routing (kpr_router_t *router)
458 {
459         return (router->kpr_interface != NULL);
460 }
461
462 static inline int
463 kpr_lookup (kpr_router_t *router, ptl_nid_t nid, int nob, ptl_nid_t *gateway_nid)
464 {
465         if (!kpr_routing (router))
466                 return (-ENETUNREACH);
467
468         return (router->kpr_interface->kprri_lookup(router->kpr_arg, nid, nob,
469                                                     gateway_nid));
470 }
471
472 static inline void
473 kpr_fwd_init (kpr_fwd_desc_t *fwd, ptl_nid_t nid,
474               int nob, int niov, struct iovec *iov,
475               kpr_fwd_callback_t callback, void *callback_arg)
476 {
477         fwd->kprfd_target_nid   = nid;
478         fwd->kprfd_gateway_nid  = nid;
479         fwd->kprfd_nob          = nob;
480         fwd->kprfd_niov         = niov;
481         fwd->kprfd_iov          = iov;
482         fwd->kprfd_callback     = callback;
483         fwd->kprfd_callback_arg = callback_arg;
484 }
485
486 static inline void
487 kpr_fwd_start (kpr_router_t *router, kpr_fwd_desc_t *fwd)
488 {
489         if (!kpr_routing (router))
490                 fwd->kprfd_callback (fwd->kprfd_callback_arg, -ENETUNREACH);
491         else
492                 router->kpr_interface->kprri_fwd_start (router->kpr_arg, fwd);
493 }
494
495 static inline void
496 kpr_fwd_done (kpr_router_t *router, kpr_fwd_desc_t *fwd, int error)
497 {
498         LASSERT (kpr_routing (router));
499         router->kpr_interface->kprri_fwd_done (router->kpr_arg, fwd, error);
500 }
501
502 static inline void
503 kpr_notify (kpr_router_t *router, 
504             ptl_nid_t peer, int alive, time_t when)
505 {
506         if (!kpr_routing (router))
507                 return;
508         
509         router->kpr_interface->kprri_notify(router->kpr_arg, peer, alive, when);
510 }
511
512 static inline void
513 kpr_shutdown (kpr_router_t *router)
514 {
515         if (kpr_routing (router))
516                 router->kpr_interface->kprri_shutdown (router->kpr_arg);
517 }
518
519 static inline void
520 kpr_deregister (kpr_router_t *router)
521 {
522         if (!kpr_routing (router))
523                 return;
524         router->kpr_interface->kprri_deregister (router->kpr_arg);
525         router->kpr_interface = NULL;
526 }
527
528 /******************************************************************************/
529
530 #ifdef PORTALS_PROFILING
531 #define prof_enum(FOO) PROF__##FOO
532 enum {
533         prof_enum(our_recvmsg),
534         prof_enum(our_sendmsg),
535         prof_enum(socknal_recv),
536         prof_enum(lib_parse),
537         prof_enum(conn_list_walk),
538         prof_enum(memcpy),
539         prof_enum(lib_finalize),
540         prof_enum(pingcli_time),
541         prof_enum(gmnal_send),
542         prof_enum(gmnal_recv),
543         MAX_PROFS
544 };
545
546 struct prof_ent {
547         char *str;
548         /* hrmph.  wrap-tastic. */
549         u32       starts;
550         u32       finishes;
551         cycles_t  total_cycles;
552         cycles_t  start;
553         cycles_t  end;
554 };
555
556 extern struct prof_ent prof_ents[MAX_PROFS];
557
558 #define PROF_START(FOO)                                         \
559         do {                                                    \
560                 struct prof_ent *pe = &prof_ents[PROF__##FOO];  \
561                 pe->starts++;                                   \
562                 pe->start = get_cycles();                       \
563         } while (0)
564
565 #define PROF_FINISH(FOO)                                        \
566         do {                                                    \
567                 struct prof_ent *pe = &prof_ents[PROF__##FOO];  \
568                 pe->finishes++;                                 \
569                 pe->end = get_cycles();                         \
570                 pe->total_cycles += (pe->end - pe->start);      \
571         } while (0)
572 #else /* !PORTALS_PROFILING */
573 #define PROF_START(FOO) do {} while(0)
574 #define PROF_FINISH(FOO) do {} while(0)
575 #endif /* PORTALS_PROFILING */
576
577 /* debug.c */
578 void portals_run_upcall(char **argv);
579 void portals_run_lbug_upcall(char * file, const char *fn, const int line);
580 void portals_debug_dumplog(void);
581 int portals_debug_init(unsigned long bufsize);
582 int portals_debug_cleanup(void);
583 int portals_debug_clear_buffer(void);
584 int portals_debug_mark_buffer(char *text);
585 int portals_debug_set_daemon(unsigned int cmd, unsigned int length,
586                              char *file, unsigned int size);
587 __s32 portals_debug_copy_to_user(char *buf, unsigned long len);
588 #if (__GNUC__)
589 /* Use the special GNU C __attribute__ hack to have the compiler check the
590  * printf style argument string against the actual argument count and
591  * types.
592  */
593 #ifdef printf
594 # warning printf has been defined as a macro...
595 # undef printf
596 #endif
597 void portals_debug_msg(int subsys, int mask, char *file, const char *fn,
598                        const int line, unsigned long stack,
599                        const char *format, ...)
600         __attribute__ ((format (printf, 7, 8)));
601 #else
602 void portals_debug_msg(int subsys, int mask, char *file, const char *fn,
603                        const int line, unsigned long stack,
604                        const char *format, ...);
605 #endif /* __GNUC__ */
606 void portals_debug_set_level(unsigned int debug_level);
607
608 # define fprintf(a, format, b...) CDEBUG(D_OTHER, format , ## b)
609 # define printf(format, b...) CDEBUG(D_OTHER, format , ## b)
610 # define time(a) CURRENT_TIME
611
612 extern void kportal_daemonize (char *name);
613 extern void kportal_blockallsigs (void);
614
615 #else  /* !__KERNEL__ */
616 # include <stdio.h>
617 # include <stdlib.h>
618 #ifndef __CYGWIN__
619 # include <stdint.h>
620 #endif
621 # include <unistd.h>
622 # include <time.h>
623 # include <asm/types.h>
624 # ifndef DEBUG_SUBSYSTEM
625 #  define DEBUG_SUBSYSTEM S_UNDEFINED
626 # endif
627 # ifdef PORTAL_DEBUG
628 #  undef NDEBUG
629 #  include <assert.h>
630 #  define LASSERT(e)     assert(e)
631 # else
632 #  define LASSERT(e)
633 # endif
634 # define printk(format, args...) printf (format, ## args)
635 # define PORTAL_ALLOC(ptr, size) do { (ptr) = malloc(size); } while (0);
636 # define PORTAL_FREE(a, b) do { free(a); } while (0);
637 # define portals_debug_msg(subsys, mask, file, fn, line, stack, format, a...) \
638     printf("%02x:%06x (@%lu %s:%s,l. %d %d %lu): " format,                    \
639            (subsys), (mask), (long)time(0), file, fn, line,                   \
640            getpid() , stack, ## a);
641 #endif
642
643 #ifndef CURRENT_TIME
644 # define CURRENT_TIME time(0)
645 #endif
646
647 /******************************************************************************/
648 /* Light-weight trace 
649  * Support for temporary event tracing with minimal Heisenberg effect. */
650 #define LWT_SUPPORT  1
651
652 typedef struct {
653         cycles_t    lwte_when;
654         char       *lwte_where;
655         void       *lwte_task;
656         long        lwte_p1;
657         long        lwte_p2;
658         long        lwte_p3;
659         long        lwte_p4;
660 } lwt_event_t;
661
662 #if LWT_SUPPORT
663 #ifdef __KERNEL__
664 #define LWT_EVENTS_PER_PAGE (PAGE_SIZE / sizeof (lwt_event_t))
665
666 typedef struct _lwt_page {
667         struct list_head     lwtp_list;
668         struct page         *lwtp_page;
669         lwt_event_t         *lwtp_events;
670 } lwt_page_t;
671
672 typedef struct {
673         int                lwtc_current_index;
674         lwt_page_t        *lwtc_current_page;
675 } lwt_cpu_t;
676
677 extern int       lwt_enabled;
678 extern lwt_cpu_t lwt_cpus[];
679
680 extern int  lwt_init (void);
681 extern void lwt_fini (void);
682 extern int  lwt_lookup_string (int *size, char *knlptr,
683                                char *usrptr, int usrsize);
684 extern int  lwt_control (int enable, int clear);
685 extern int  lwt_snapshot (int *ncpu, int *total_size,
686                           void *user_ptr, int user_size);
687
688 /* Note that we _don't_ define LWT_EVENT at all if LWT_SUPPORT isn't set.
689  * This stuff is meant for finding specific problems; it never stays in
690  * production code... */
691
692 #define LWTSTR(n)       #n
693 #define LWTWHERE(f,l)   f ":" LWTSTR(l)
694
695 #define LWT_EVENT(p1, p2, p3, p4)                                       \
696 do {                                                                    \
697         unsigned long    flags;                                         \
698         lwt_cpu_t       *cpu;                                           \
699         lwt_page_t      *p;                                             \
700         lwt_event_t     *e;                                             \
701                                                                         \
702         local_irq_save (flags);                                         \
703                                                                         \
704         if (lwt_enabled) {                                              \
705                 cpu = &lwt_cpus[smp_processor_id()];                    \
706                 p = cpu->lwtc_current_page;                             \
707                 e = &p->lwtp_events[cpu->lwtc_current_index++];         \
708                                                                         \
709                 if (cpu->lwtc_current_index >= LWT_EVENTS_PER_PAGE) {   \
710                         cpu->lwtc_current_page =                        \
711                                 list_entry (p->lwtp_list.next,          \
712                                             lwt_page_t, lwtp_list);     \
713                         cpu->lwtc_current_index = 0;                    \
714                 }                                                       \
715                                                                         \
716                 e->lwte_when  = get_cycles();                           \
717                 e->lwte_where = LWTWHERE(__FILE__,__LINE__);            \
718                 e->lwte_task  = current;                                \
719                 e->lwte_p1    = (long)(p1);                             \
720                 e->lwte_p2    = (long)(p2);                             \
721                 e->lwte_p3    = (long)(p3);                             \
722                 e->lwte_p4    = (long)(p4);                             \
723         }                                                               \
724                                                                         \
725         local_irq_restore (flags);                                      \
726 } while (0)
727 #else  /* __KERNEL__ */
728 #define LWT_EVENT(p1,p2,p3,p4)     /* no userland implementation yet */
729 #endif /* __KERNEL__ */
730 #endif /* LWT_SUPPORT */
731
732
733 #include <linux/portals_lib.h>
734
735 /*
736  * USER LEVEL STUFF BELOW
737  */
738
739 #define PORTAL_IOCTL_VERSION 0x00010007
740 #define PING_SYNC       0
741 #define PING_ASYNC      1
742
743 struct portal_ioctl_data {
744         __u32 ioc_len;
745         __u32 ioc_version;
746         __u64 ioc_nid;
747         __u64 ioc_nid2;
748         __u64 ioc_nid3;
749         __u32 ioc_count;
750         __u32 ioc_nal;
751         __u32 ioc_nal_cmd;
752         __u32 ioc_fd;
753         __u32 ioc_id;
754
755         __u32 ioc_flags;
756         __u32 ioc_size;
757
758         __u32 ioc_wait;
759         __u32 ioc_timeout;
760         __u32 ioc_misc;
761
762         __u32 ioc_inllen1;
763         char *ioc_inlbuf1;
764         __u32 ioc_inllen2;
765         char *ioc_inlbuf2;
766
767         __u32 ioc_plen1; /* buffers in userspace */
768         char *ioc_pbuf1;
769         __u32 ioc_plen2; /* buffers in userspace */
770         char *ioc_pbuf2;
771
772         char ioc_bulk[0];
773 };
774
775 struct portal_ioctl_hdr {
776         __u32 ioc_len;
777         __u32 ioc_version;
778 };
779
780 struct portals_debug_ioctl_data
781 {
782         struct portal_ioctl_hdr hdr;
783         unsigned int subs;
784         unsigned int debug;
785 };
786
787 #define PORTAL_IOC_INIT(data)                           \
788 do {                                                    \
789         memset(&data, 0, sizeof(data));                 \
790         data.ioc_version = PORTAL_IOCTL_VERSION;        \
791         data.ioc_len = sizeof(data);                    \
792 } while (0)
793
794 /* FIXME check conflict with lustre_lib.h */
795 #define PTL_IOC_DEBUG_MASK             _IOWR('f', 250, long)
796
797 static inline int portal_ioctl_packlen(struct portal_ioctl_data *data)
798 {
799         int len = sizeof(*data);
800         len += size_round(data->ioc_inllen1);
801         len += size_round(data->ioc_inllen2);
802         return len;
803 }
804
805 static inline int portal_ioctl_is_invalid(struct portal_ioctl_data *data)
806 {
807         if (data->ioc_len > (1<<30)) {
808                 CERROR ("PORTALS ioctl: ioc_len larger than 1<<30\n");
809                 return 1;
810         }
811         if (data->ioc_inllen1 > (1<<30)) {
812                 CERROR ("PORTALS ioctl: ioc_inllen1 larger than 1<<30\n");
813                 return 1;
814         }
815         if (data->ioc_inllen2 > (1<<30)) {
816                 CERROR ("PORTALS ioctl: ioc_inllen2 larger than 1<<30\n");
817                 return 1;
818         }
819         if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
820                 CERROR ("PORTALS ioctl: inlbuf1 pointer but 0 length\n");
821                 return 1;
822         }
823         if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
824                 CERROR ("PORTALS ioctl: inlbuf2 pointer but 0 length\n");
825                 return 1;
826         }
827         if (data->ioc_pbuf1 && !data->ioc_plen1) {
828                 CERROR ("PORTALS ioctl: pbuf1 pointer but 0 length\n");
829                 return 1;
830         }
831         if (data->ioc_pbuf2 && !data->ioc_plen2) {
832                 CERROR ("PORTALS ioctl: pbuf2 pointer but 0 length\n");
833                 return 1;
834         }
835         if (data->ioc_plen1 && !data->ioc_pbuf1) {
836                 CERROR ("PORTALS ioctl: plen1 nonzero but no pbuf1 pointer\n");
837                 return 1;
838         }
839         if (data->ioc_plen2 && !data->ioc_pbuf2) {
840                 CERROR ("PORTALS ioctl: plen2 nonzero but no pbuf2 pointer\n");
841                 return 1;
842         }
843         if (portal_ioctl_packlen(data) != data->ioc_len ) {
844                 CERROR ("PORTALS ioctl: packlen != ioc_len\n");
845                 return 1;
846         }
847         if (data->ioc_inllen1 &&
848             data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') {
849                 CERROR ("PORTALS ioctl: inlbuf1 not 0 terminated\n");
850                 return 1;
851         }
852         if (data->ioc_inllen2 &&
853             data->ioc_bulk[size_round(data->ioc_inllen1) +
854                            data->ioc_inllen2 - 1] != '\0') {
855                 CERROR ("PORTALS ioctl: inlbuf2 not 0 terminated\n");
856                 return 1;
857         }
858         return 0;
859 }
860
861 #ifndef __KERNEL__
862 static inline int portal_ioctl_pack(struct portal_ioctl_data *data, char **pbuf,
863                                     int max)
864 {
865         char *ptr;
866         struct portal_ioctl_data *overlay;
867         data->ioc_len = portal_ioctl_packlen(data);
868         data->ioc_version = PORTAL_IOCTL_VERSION;
869
870         if (*pbuf && portal_ioctl_packlen(data) > max)
871                 return 1;
872         if (*pbuf == NULL) {
873                 *pbuf = malloc(data->ioc_len);
874         }
875         if (!*pbuf)
876                 return 1;
877         overlay = (struct portal_ioctl_data *)*pbuf;
878         memcpy(*pbuf, data, sizeof(*data));
879
880         ptr = overlay->ioc_bulk;
881         if (data->ioc_inlbuf1)
882                 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
883         if (data->ioc_inlbuf2)
884                 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
885         if (portal_ioctl_is_invalid(overlay))
886                 return 1;
887
888         return 0;
889 }
890 #else
891 #include <asm/uaccess.h>
892
893 /* buffer MUST be at least the size of portal_ioctl_hdr */
894 static inline int portal_ioctl_getdata(char *buf, char *end, void *arg)
895 {
896         struct portal_ioctl_hdr *hdr;
897         struct portal_ioctl_data *data;
898         int err;
899         ENTRY;
900
901         hdr = (struct portal_ioctl_hdr *)buf;
902         data = (struct portal_ioctl_data *)buf;
903
904         err = copy_from_user(buf, (void *)arg, sizeof(*hdr));
905         if ( err ) {
906                 EXIT;
907                 return err;
908         }
909
910         if (hdr->ioc_version != PORTAL_IOCTL_VERSION) {
911                 CERROR ("PORTALS: version mismatch kernel vs application\n");
912                 return -EINVAL;
913         }
914
915         if (hdr->ioc_len + buf >= end) {
916                 CERROR ("PORTALS: user buffer exceeds kernel buffer\n");
917                 return -EINVAL;
918         }
919
920
921         if (hdr->ioc_len < sizeof(struct portal_ioctl_data)) {
922                 CERROR ("PORTALS: user buffer too small for ioctl\n");
923                 return -EINVAL;
924         }
925
926         err = copy_from_user(buf, (void *)arg, hdr->ioc_len);
927         if ( err ) {
928                 EXIT;
929                 return err;
930         }
931
932         if (portal_ioctl_is_invalid(data)) {
933                 CERROR ("PORTALS: ioctl not correctly formatted\n");
934                 return -EINVAL;
935         }
936
937         if (data->ioc_inllen1) {
938                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
939         }
940
941         if (data->ioc_inllen2) {
942                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
943                         size_round(data->ioc_inllen1);
944         }
945
946         EXIT;
947         return 0;
948 }
949 #endif
950
951 /* ioctls for manipulating snapshots 30- */
952 #define IOC_PORTAL_TYPE                   'e'
953 #define IOC_PORTAL_MIN_NR                 30
954
955 #define IOC_PORTAL_PING                    _IOWR('e', 30, long)
956 #define IOC_PORTAL_GET_DEBUG               _IOWR('e', 31, long)
957 #define IOC_PORTAL_CLEAR_DEBUG             _IOWR('e', 32, long)
958 #define IOC_PORTAL_MARK_DEBUG              _IOWR('e', 33, long)
959 #define IOC_PORTAL_PANIC                   _IOWR('e', 34, long)
960 #define IOC_PORTAL_ADD_ROUTE               _IOWR('e', 35, long)
961 #define IOC_PORTAL_DEL_ROUTE               _IOWR('e', 36, long)
962 #define IOC_PORTAL_GET_ROUTE               _IOWR('e', 37, long)
963 #define IOC_PORTAL_NAL_CMD                 _IOWR('e', 38, long)
964 #define IOC_PORTAL_GET_NID                 _IOWR('e', 39, long)
965 #define IOC_PORTAL_FAIL_NID                _IOWR('e', 40, long)
966 #define IOC_PORTAL_SET_DAEMON              _IOWR('e', 41, long)
967 #define IOC_PORTAL_NOTIFY_ROUTER           _IOWR('e', 42, long)
968 #define IOC_PORTAL_LWT_CONTROL             _IOWR('e', 43, long)
969 #define IOC_PORTAL_LWT_SNAPSHOT            _IOWR('e', 44, long)
970 #define IOC_PORTAL_LWT_LOOKUP_STRING       _IOWR('e', 45, long)
971 #define IOC_PORTAL_MAX_NR                             45
972
973 enum {
974         QSWNAL  =  1,
975         SOCKNAL,
976         GMNAL,
977         TOENAL,
978         TCPNAL,
979         SCIMACNAL,
980         NAL_ENUM_END_MARKER
981 };
982
983 #ifdef __KERNEL__
984 extern ptl_handle_ni_t  kqswnal_ni;
985 extern ptl_handle_ni_t  ksocknal_ni;
986 extern ptl_handle_ni_t  ktoenal_ni;
987 extern ptl_handle_ni_t  kgmnal_ni;
988 extern ptl_handle_ni_t  kscimacnal_ni;
989 #endif
990
991 #define NAL_MAX_NR (NAL_ENUM_END_MARKER - 1)
992
993 #define NAL_CMD_REGISTER_PEER_FD     100
994 #define NAL_CMD_CLOSE_CONNECTION     101
995 #define NAL_CMD_REGISTER_MYNID       102
996 #define NAL_CMD_PUSH_CONNECTION      103
997 #define NAL_CMD_GET_CONN             104
998 #define NAL_CMD_DEL_AUTOCONN         105
999 #define NAL_CMD_ADD_AUTOCONN         106
1000 #define NAL_CMD_GET_AUTOCONN         107
1001 #define NAL_CMD_GET_TXDESC           108
1002
1003 enum {
1004         DEBUG_DAEMON_START       =  1,
1005         DEBUG_DAEMON_STOP        =  2,
1006         DEBUG_DAEMON_PAUSE       =  3,
1007         DEBUG_DAEMON_CONTINUE    =  4,
1008 };
1009
1010 /* XXX remove to lustre ASAP */
1011 struct lustre_peer {
1012         ptl_nid_t       peer_nid;
1013         ptl_handle_ni_t peer_ni;
1014 };
1015
1016 /* module.c */
1017 typedef int (*nal_cmd_handler_t)(struct portal_ioctl_data *, void * private);
1018 int kportal_nal_register(int nal, nal_cmd_handler_t handler, void * private);
1019 int kportal_nal_unregister(int nal);
1020
1021 ptl_handle_ni_t *kportal_get_ni (int nal);
1022 void kportal_put_ni (int nal);
1023
1024 #ifdef __CYGWIN__
1025 # ifndef BITS_PER_LONG
1026 #  if (~0UL) == 0xffffffffUL
1027 #   define BITS_PER_LONG 32
1028 #  else
1029 #   define BITS_PER_LONG 64
1030 #  endif
1031 # endif
1032 #endif
1033
1034 #if (BITS_PER_LONG == 32 || __WORDSIZE == 32)
1035 # define LPU64 "%Lu"
1036 # define LPD64 "%Ld"
1037 # define LPX64 "%#Lx"
1038 # define LPSZ  "%u"
1039 # define LPSSZ "%d"
1040 #endif
1041 #if (BITS_PER_LONG == 64 || __WORDSIZE == 64)
1042 # define LPU64 "%lu"
1043 # define LPD64 "%ld"
1044 # define LPX64 "%#lx"
1045 # define LPSZ  "%lu"
1046 # define LPSSZ "%ld"
1047 #endif
1048 #ifndef LPU64
1049 # error "No word size defined"
1050 #endif
1051
1052 #endif