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