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