Whamcloud - gitweb
6596d33ae982eefb602f4d4aeefaa830a661cc6e
[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    (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 ll_invalidate_inode_pages(inode) invalidate_inode_pages(inode)
188 #define ll_truncate_complete_page(page) truncate_complete_page(page)
189 #define PageUptodate Page_Uptodate
190 #define our_recalc_sigpending(current) recalc_sigpending(current)
191 #define num_online_cpus() smp_num_cpus
192 static inline void our_cond_resched(void)
193 {
194         if (current->need_resched)
195                schedule ();
196 }
197 #define work_struct_t       struct tq_struct
198
199 #else
200
201 #define prepare_work(wq,cb,cbdata)                                            \
202 do {                                                                          \
203         INIT_WORK((wq), (void *)(cb), (void *)(cbdata));                      \
204 } while (0)
205 #define ll_invalidate_inode_pages(inode) \
206         invalidate_inode_pages((inode)->i_mapping)
207 #define ll_truncate_complete_page(page) \
208         truncate_complete_page((page)->mapping, page)
209 #define wait_on_page wait_on_page_locked
210 #define our_recalc_sigpending(current) recalc_sigpending()
211 #define strtok(a,b) strpbrk(a, b)
212 static inline void our_cond_resched(void)
213 {
214         cond_resched();
215 }
216 #define work_struct_t      struct work_struct
217
218 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) */
219
220 #ifdef PORTAL_DEBUG
221 extern void kportal_assertion_failed(char *expr, char *file, const char *func,
222                                      const int line);
223 #define LASSERT(e) ((e) ? 0 : kportal_assertion_failed( #e , __FILE__,  \
224                                                         __FUNCTION__, __LINE__))
225 /* it would be great to dump_stack() here, but some kernels
226  * export it as show_stack() and I can't be bothered to
227  * proprely engage in that dance right now */
228 #define LASSERTF(cond, fmt...)                                                \
229         do {                                                                  \
230                 if (unlikely(!(cond))) {                                      \
231                         portals_debug_msg(0, D_EMERG,  __FILE__, __FUNCTION__,\
232                                           __LINE__,  CDEBUG_STACK,            \
233                                           "ASSERTION(" #cond ") failed:" fmt);\
234                         LBUG();                                               \
235                 }                                                             \
236         } while (0)
237
238 #else
239 #define LASSERT(e)
240 #define LASSERTF(cond, fmt...) 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         int                  kprfd_nob;         /* # message bytes (including header) */
372         int                  kprfd_niov;        /* # message frags (including header) */
373         struct iovec        *kprfd_iov;         /* message fragments */
374         void                *kprfd_router_arg;  // originating NAL's router arg
375         kpr_fwd_callback_t   kprfd_callback;    /* completion callback */
376         void                *kprfd_callback_arg; /* completion callback arg */
377         kprfd_scratch_t      kprfd_scratch;    // scratchpad for routing targets
378 } kpr_fwd_desc_t;
379
380 typedef void  (*kpr_fwd_t)(void *arg, kpr_fwd_desc_t *fwd);
381 typedef void  (*kpr_notify_t)(void *arg, ptl_nid_t peer, int alive);
382
383 /* NAL's routing interface (Kernel Portals Routing Nal Interface) */
384 typedef const struct {
385         int             kprni_nalid;    /* NAL's id */
386         void           *kprni_arg;      /* Arg to pass when calling into NAL */
387         kpr_fwd_t       kprni_fwd;      /* NAL's forwarding entrypoint */
388         kpr_notify_t    kprni_notify;   /* NAL's notification entrypoint */
389 } kpr_nal_interface_t;
390
391 /* Router's routing interface (Kernel Portals Routing Router Interface) */
392 typedef const struct {
393         /* register the calling NAL with the router and get back the handle for
394          * subsequent calls */
395         int     (*kprri_register) (kpr_nal_interface_t *nal_interface,
396                                    void **router_arg);
397
398         /* ask the router to find a gateway that forwards to 'nid' and is a
399          * peer of the calling NAL; assume caller will send 'nob' bytes of
400          * payload there */
401         int     (*kprri_lookup) (void *router_arg, ptl_nid_t nid, int nob,
402                                  ptl_nid_t *gateway_nid);
403
404         /* hand a packet over to the router for forwarding */
405         kpr_fwd_t kprri_fwd_start;
406
407         /* hand a packet back to the router for completion */
408         void    (*kprri_fwd_done) (void *router_arg, kpr_fwd_desc_t *fwd,
409                                    int error);
410
411         /* notify the router about peer state */
412         void    (*kprri_notify) (void *router_arg, ptl_nid_t peer,
413                                  int alive, time_t when);
414
415         /* the calling NAL is shutting down */
416         void    (*kprri_shutdown) (void *router_arg);
417
418         /* deregister the calling NAL with the router */
419         void    (*kprri_deregister) (void *router_arg);
420
421 } kpr_router_interface_t;
422
423 /* Convenient struct for NAL to stash router interface/args */
424 typedef struct {
425         kpr_router_interface_t  *kpr_interface;
426         void                    *kpr_arg;
427 } kpr_router_t;
428
429 /* Router's control interface (Kernel Portals Routing Control Interface) */
430 typedef const struct {
431         int     (*kprci_add_route)(int gateway_nal, ptl_nid_t gateway_nid,
432                                    ptl_nid_t lo_nid, ptl_nid_t hi_nid);
433         int     (*kprci_del_route)(int gateway_nal, ptl_nid_t gateway_nid,
434                                    ptl_nid_t lo_nid, ptl_nid_t hi_nid);
435         int     (*kprci_get_route)(int index, int *gateway_nal,
436                                    ptl_nid_t *gateway,
437                                    ptl_nid_t *lo_nid, ptl_nid_t *hi_nid,
438                                    int *alive);
439         int     (*kprci_notify)(int gateway_nal, ptl_nid_t gateway_nid,
440                                 int alive, time_t when);
441 } kpr_control_interface_t;
442
443 extern kpr_control_interface_t  kpr_control_interface;
444 extern kpr_router_interface_t   kpr_router_interface;
445
446 static inline int
447 kpr_register (kpr_router_t *router, kpr_nal_interface_t *nalif)
448 {
449         int    rc;
450
451         router->kpr_interface = PORTAL_SYMBOL_GET (kpr_router_interface);
452         if (router->kpr_interface == NULL)
453                 return (-ENOENT);
454
455         rc = (router->kpr_interface)->kprri_register (nalif, &router->kpr_arg);
456         if (rc != 0)
457                 router->kpr_interface = NULL;
458
459         PORTAL_SYMBOL_PUT (kpr_router_interface);
460         return (rc);
461 }
462
463 static inline int
464 kpr_routing (kpr_router_t *router)
465 {
466         return (router->kpr_interface != NULL);
467 }
468
469 static inline int
470 kpr_lookup (kpr_router_t *router, ptl_nid_t nid, int nob, ptl_nid_t *gateway_nid)
471 {
472         if (!kpr_routing (router))
473                 return (-ENETUNREACH);
474
475         return (router->kpr_interface->kprri_lookup(router->kpr_arg, nid, nob,
476                                                     gateway_nid));
477 }
478
479 static inline void
480 kpr_fwd_init (kpr_fwd_desc_t *fwd, ptl_nid_t nid,
481               int nob, int niov, struct iovec *iov,
482               kpr_fwd_callback_t callback, void *callback_arg)
483 {
484         fwd->kprfd_target_nid   = nid;
485         fwd->kprfd_gateway_nid  = nid;
486         fwd->kprfd_nob          = nob;
487         fwd->kprfd_niov         = niov;
488         fwd->kprfd_iov          = iov;
489         fwd->kprfd_callback     = callback;
490         fwd->kprfd_callback_arg = callback_arg;
491 }
492
493 static inline void
494 kpr_fwd_start (kpr_router_t *router, kpr_fwd_desc_t *fwd)
495 {
496         if (!kpr_routing (router))
497                 fwd->kprfd_callback (fwd->kprfd_callback_arg, -ENETUNREACH);
498         else
499                 router->kpr_interface->kprri_fwd_start (router->kpr_arg, fwd);
500 }
501
502 static inline void
503 kpr_fwd_done (kpr_router_t *router, kpr_fwd_desc_t *fwd, int error)
504 {
505         LASSERT (kpr_routing (router));
506         router->kpr_interface->kprri_fwd_done (router->kpr_arg, fwd, error);
507 }
508
509 static inline void
510 kpr_notify (kpr_router_t *router,
511             ptl_nid_t peer, int alive, time_t when)
512 {
513         if (!kpr_routing (router))
514                 return;
515
516         router->kpr_interface->kprri_notify(router->kpr_arg, peer, alive, when);
517 }
518
519 static inline void
520 kpr_shutdown (kpr_router_t *router)
521 {
522         if (kpr_routing (router))
523                 router->kpr_interface->kprri_shutdown (router->kpr_arg);
524 }
525
526 static inline void
527 kpr_deregister (kpr_router_t *router)
528 {
529         if (!kpr_routing (router))
530                 return;
531         router->kpr_interface->kprri_deregister (router->kpr_arg);
532         router->kpr_interface = NULL;
533 }
534
535 /******************************************************************************/
536
537 #ifdef PORTALS_PROFILING
538 #define prof_enum(FOO) PROF__##FOO
539 enum {
540         prof_enum(our_recvmsg),
541         prof_enum(our_sendmsg),
542         prof_enum(socknal_recv),
543         prof_enum(lib_parse),
544         prof_enum(conn_list_walk),
545         prof_enum(memcpy),
546         prof_enum(lib_finalize),
547         prof_enum(pingcli_time),
548         prof_enum(gmnal_send),
549         prof_enum(gmnal_recv),
550         MAX_PROFS
551 };
552
553 struct prof_ent {
554         char *str;
555         /* hrmph.  wrap-tastic. */
556         u32       starts;
557         u32       finishes;
558         cycles_t  total_cycles;
559         cycles_t  start;
560         cycles_t  end;
561 };
562
563 extern struct prof_ent prof_ents[MAX_PROFS];
564
565 #define PROF_START(FOO)                                         \
566         do {                                                    \
567                 struct prof_ent *pe = &prof_ents[PROF__##FOO];  \
568                 pe->starts++;                                   \
569                 pe->start = get_cycles();                       \
570         } while (0)
571
572 #define PROF_FINISH(FOO)                                        \
573         do {                                                    \
574                 struct prof_ent *pe = &prof_ents[PROF__##FOO];  \
575                 pe->finishes++;                                 \
576                 pe->end = get_cycles();                         \
577                 pe->total_cycles += (pe->end - pe->start);      \
578         } while (0)
579 #else /* !PORTALS_PROFILING */
580 #define PROF_START(FOO) do {} while(0)
581 #define PROF_FINISH(FOO) do {} while(0)
582 #endif /* PORTALS_PROFILING */
583
584 /* debug.c */
585 extern spinlock_t stack_backtrace_lock;
586
587 char *portals_debug_dumpstack(void);
588 void portals_run_upcall(char **argv);
589 void portals_run_lbug_upcall(char * file, const char *fn, const int line);
590 void portals_debug_dumplog(void);
591 int portals_debug_init(unsigned long bufsize);
592 int portals_debug_cleanup(void);
593 int portals_debug_clear_buffer(void);
594 int portals_debug_mark_buffer(char *text);
595 int portals_debug_set_daemon(unsigned int cmd, unsigned int length,
596                              char *file, unsigned int size);
597 __s32 portals_debug_copy_to_user(char *buf, unsigned long len);
598 #if (__GNUC__)
599 /* Use the special GNU C __attribute__ hack to have the compiler check the
600  * printf style argument string against the actual argument count and
601  * types.
602  */
603 #ifdef printf
604 # warning printf has been defined as a macro...
605 # undef printf
606 #endif
607 void portals_debug_msg(int subsys, int mask, char *file, const char *fn,
608                        const int line, unsigned long stack,
609                        char *format, ...)
610         __attribute__ ((format (printf, 7, 8)));
611 #else
612 void portals_debug_msg(int subsys, int mask, char *file, const char *fn,
613                        const int line, unsigned long stack,
614                        const char *format, ...);
615 #endif /* __GNUC__ */
616 void portals_debug_set_level(unsigned int debug_level);
617
618 # define fprintf(a, format, b...) CDEBUG(D_OTHER, format , ## b)
619 # define printf(format, b...) CDEBUG(D_OTHER, format , ## b)
620 # define time(a) CURRENT_TIME
621
622 extern void kportal_daemonize (char *name);
623 extern void kportal_blockallsigs (void);
624
625 #else  /* !__KERNEL__ */
626 # include <stdio.h>
627 # include <stdlib.h>
628 #ifndef __CYGWIN__
629 # include <stdint.h>
630 #else
631 # include <cygwin-ioctl.h>
632 #endif
633 # include <unistd.h>
634 # include <time.h>
635 # include <asm/types.h>
636 # ifndef DEBUG_SUBSYSTEM
637 #  define DEBUG_SUBSYSTEM S_UNDEFINED
638 # endif
639 # ifdef PORTAL_DEBUG
640 #  undef NDEBUG
641 #  include <assert.h>
642 #  define LASSERT(e)     assert(e)
643 #  define LASSERTF(cond, args...)     assert(cond)
644 # else
645 #  define LASSERT(e)
646 #  define LASSERTF(cond, args...) do { } while (0)
647 # endif
648 # define printk(format, args...) printf (format, ## args)
649 # define PORTAL_ALLOC(ptr, size) do { (ptr) = malloc(size); } while (0);
650 # define PORTAL_FREE(a, b) do { free(a); } while (0);
651 # define portals_debug_msg(subsys, mask, file, fn, line, stack, format, a...) \
652     printf("%02x:%06x (@%lu %s:%s,l. %d %d %lu): " format,                    \
653            (subsys), (mask), (long)time(0), file, fn, line,                   \
654            getpid() , stack, ## a);
655 #endif
656
657 /* support decl needed both by kernel and liblustre */
658 char *portals_nid2str(int nal, ptl_nid_t nid, char *str);
659
660 #ifndef CURRENT_TIME
661 # define CURRENT_TIME time(0)
662 #endif
663
664 /******************************************************************************/
665 /* Light-weight trace
666  * Support for temporary event tracing with minimal Heisenberg effect. */
667 #define LWT_SUPPORT  0
668
669 #define LWT_MEMORY   (64<<20)
670 #define LWT_MAX_CPUS 4
671
672 typedef struct {
673         cycles_t    lwte_when;
674         char       *lwte_where;
675         void       *lwte_task;
676         long        lwte_p1;
677         long        lwte_p2;
678         long        lwte_p3;
679         long        lwte_p4;
680 #if BITS_PER_LONG > 32
681         long        lwte_pad;
682 #endif
683 } lwt_event_t;
684
685 #if LWT_SUPPORT
686 #ifdef __KERNEL__
687 #define LWT_EVENTS_PER_PAGE (PAGE_SIZE / sizeof (lwt_event_t))
688
689 typedef struct _lwt_page {
690         struct list_head     lwtp_list;
691         struct page         *lwtp_page;
692         lwt_event_t         *lwtp_events;
693 } lwt_page_t;
694
695 typedef struct {
696         int                lwtc_current_index;
697         lwt_page_t        *lwtc_current_page;
698 } lwt_cpu_t;
699
700 extern int       lwt_enabled;
701 extern lwt_cpu_t lwt_cpus[];
702
703 extern int  lwt_init (void);
704 extern void lwt_fini (void);
705 extern int  lwt_lookup_string (int *size, char *knlptr,
706                                char *usrptr, int usrsize);
707 extern int  lwt_control (int enable, int clear);
708 extern int  lwt_snapshot (cycles_t *now, int *ncpu, int *total_size,
709                           void *user_ptr, int user_size);
710
711 /* Note that we _don't_ define LWT_EVENT at all if LWT_SUPPORT isn't set.
712  * This stuff is meant for finding specific problems; it never stays in
713  * production code... */
714
715 #define LWTSTR(n)       #n
716 #define LWTWHERE(f,l)   f ":" LWTSTR(l)
717
718 #define LWT_EVENT(p1, p2, p3, p4)                                       \
719 do {                                                                    \
720         unsigned long    flags;                                         \
721         lwt_cpu_t       *cpu;                                           \
722         lwt_page_t      *p;                                             \
723         lwt_event_t     *e;                                             \
724                                                                         \
725         local_irq_save (flags);                                         \
726                                                                         \
727         if (lwt_enabled) {                                              \
728                 cpu = &lwt_cpus[smp_processor_id()];                    \
729                 p = cpu->lwtc_current_page;                             \
730                 e = &p->lwtp_events[cpu->lwtc_current_index++];         \
731                                                                         \
732                 if (cpu->lwtc_current_index >= LWT_EVENTS_PER_PAGE) {   \
733                         cpu->lwtc_current_page =                        \
734                                 list_entry (p->lwtp_list.next,          \
735                                             lwt_page_t, lwtp_list);     \
736                         cpu->lwtc_current_index = 0;                    \
737                 }                                                       \
738                                                                         \
739                 e->lwte_when  = get_cycles();                           \
740                 e->lwte_where = LWTWHERE(__FILE__,__LINE__);            \
741                 e->lwte_task  = current;                                \
742                 e->lwte_p1    = (long)(p1);                             \
743                 e->lwte_p2    = (long)(p2);                             \
744                 e->lwte_p3    = (long)(p3);                             \
745                 e->lwte_p4    = (long)(p4);                             \
746         }                                                               \
747                                                                         \
748         local_irq_restore (flags);                                      \
749 } while (0)
750 #else  /* __KERNEL__ */
751 #define LWT_EVENT(p1,p2,p3,p4)     /* no userland implementation yet */
752 #endif /* __KERNEL__ */
753 #endif /* LWT_SUPPORT */
754
755 struct portals_device_userstate
756 {
757         int          pdu_memhog_pages;
758         struct page *pdu_memhog_root_page;
759 };
760
761 #include <linux/portals_lib.h>
762
763 /*
764  * USER LEVEL STUFF BELOW
765  */
766
767 #define PORTALS_CFG_VERSION 0x00010001;
768
769 struct portals_cfg {
770         __u32 pcfg_version;
771         __u32 pcfg_command;
772
773         __u32 pcfg_nal;
774         __u32 pcfg_flags;
775
776         __u32 pcfg_gw_nal;
777         __u64 pcfg_nid;
778         __u64 pcfg_nid2;
779         __u64 pcfg_nid3;
780         __u32 pcfg_id;
781         __u32 pcfg_misc;
782         __u32 pcfg_fd;
783         __u32 pcfg_count;
784         __u32 pcfg_size;
785         __u32 pcfg_wait;
786
787         __u32 pcfg_plen1; /* buffers in userspace */
788         char *pcfg_pbuf1;
789         __u32 pcfg_plen2; /* buffers in userspace */
790         char *pcfg_pbuf2;
791 };
792
793 #define PCFG_INIT(pcfg, cmd)                            \
794 do {                                                    \
795         memset(&pcfg, 0, sizeof(pcfg));                 \
796         pcfg.pcfg_version = PORTALS_CFG_VERSION;        \
797         pcfg.pcfg_command = (cmd);                      \
798                                                         \
799 } while (0)
800
801 #define PORTAL_IOCTL_VERSION 0x00010007
802 #define PING_SYNC       0
803 #define PING_ASYNC      1
804
805 struct portal_ioctl_data {
806         __u32 ioc_len;
807         __u32 ioc_version;
808         __u64 ioc_nid;
809         __u64 ioc_nid2;
810         __u64 ioc_nid3;
811         __u32 ioc_count;
812         __u32 ioc_nal;
813         __u32 ioc_nal_cmd;
814         __u32 ioc_fd;
815         __u32 ioc_id;
816
817         __u32 ioc_flags;
818         __u32 ioc_size;
819
820         __u32 ioc_wait;
821         __u32 ioc_timeout;
822         __u32 ioc_misc;
823
824         __u32 ioc_inllen1;
825         char *ioc_inlbuf1;
826         __u32 ioc_inllen2;
827         char *ioc_inlbuf2;
828
829         __u32 ioc_plen1; /* buffers in userspace */
830         char *ioc_pbuf1;
831         __u32 ioc_plen2; /* buffers in userspace */
832         char *ioc_pbuf2;
833
834         char ioc_bulk[0];
835 };
836
837 struct portal_ioctl_hdr {
838         __u32 ioc_len;
839         __u32 ioc_version;
840 };
841
842 struct portals_debug_ioctl_data
843 {
844         struct portal_ioctl_hdr hdr;
845         unsigned int subs;
846         unsigned int debug;
847 };
848
849 #define PORTAL_IOC_INIT(data)                           \
850 do {                                                    \
851         memset(&data, 0, sizeof(data));                 \
852         data.ioc_version = PORTAL_IOCTL_VERSION;        \
853         data.ioc_len = sizeof(data);                    \
854 } while (0)
855
856 /* FIXME check conflict with lustre_lib.h */
857 #define PTL_IOC_DEBUG_MASK             _IOWR('f', 250, long)
858
859 static inline int portal_ioctl_packlen(struct portal_ioctl_data *data)
860 {
861         int len = sizeof(*data);
862         len += size_round(data->ioc_inllen1);
863         len += size_round(data->ioc_inllen2);
864         return len;
865 }
866
867 static inline int portal_ioctl_is_invalid(struct portal_ioctl_data *data)
868 {
869         if (data->ioc_len > (1<<30)) {
870                 CERROR ("PORTALS ioctl: ioc_len larger than 1<<30\n");
871                 return 1;
872         }
873         if (data->ioc_inllen1 > (1<<30)) {
874                 CERROR ("PORTALS ioctl: ioc_inllen1 larger than 1<<30\n");
875                 return 1;
876         }
877         if (data->ioc_inllen2 > (1<<30)) {
878                 CERROR ("PORTALS ioctl: ioc_inllen2 larger than 1<<30\n");
879                 return 1;
880         }
881         if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
882                 CERROR ("PORTALS ioctl: inlbuf1 pointer but 0 length\n");
883                 return 1;
884         }
885         if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
886                 CERROR ("PORTALS ioctl: inlbuf2 pointer but 0 length\n");
887                 return 1;
888         }
889         if (data->ioc_pbuf1 && !data->ioc_plen1) {
890                 CERROR ("PORTALS ioctl: pbuf1 pointer but 0 length\n");
891                 return 1;
892         }
893         if (data->ioc_pbuf2 && !data->ioc_plen2) {
894                 CERROR ("PORTALS ioctl: pbuf2 pointer but 0 length\n");
895                 return 1;
896         }
897         if (data->ioc_plen1 && !data->ioc_pbuf1) {
898                 CERROR ("PORTALS ioctl: plen1 nonzero but no pbuf1 pointer\n");
899                 return 1;
900         }
901         if (data->ioc_plen2 && !data->ioc_pbuf2) {
902                 CERROR ("PORTALS ioctl: plen2 nonzero but no pbuf2 pointer\n");
903                 return 1;
904         }
905         if (portal_ioctl_packlen(data) != data->ioc_len ) {
906                 CERROR ("PORTALS ioctl: packlen != ioc_len\n");
907                 return 1;
908         }
909         if (data->ioc_inllen1 &&
910             data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') {
911                 CERROR ("PORTALS ioctl: inlbuf1 not 0 terminated\n");
912                 return 1;
913         }
914         if (data->ioc_inllen2 &&
915             data->ioc_bulk[size_round(data->ioc_inllen1) +
916                            data->ioc_inllen2 - 1] != '\0') {
917                 CERROR ("PORTALS ioctl: inlbuf2 not 0 terminated\n");
918                 return 1;
919         }
920         return 0;
921 }
922
923 #ifndef __KERNEL__
924 static inline int portal_ioctl_pack(struct portal_ioctl_data *data, char **pbuf,
925                                     int max)
926 {
927         char *ptr;
928         struct portal_ioctl_data *overlay;
929         data->ioc_len = portal_ioctl_packlen(data);
930         data->ioc_version = PORTAL_IOCTL_VERSION;
931
932         if (*pbuf && portal_ioctl_packlen(data) > max)
933                 return 1;
934         if (*pbuf == NULL) {
935                 *pbuf = malloc(data->ioc_len);
936         }
937         if (!*pbuf)
938                 return 1;
939         overlay = (struct portal_ioctl_data *)*pbuf;
940         memcpy(*pbuf, data, sizeof(*data));
941
942         ptr = overlay->ioc_bulk;
943         if (data->ioc_inlbuf1)
944                 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
945         if (data->ioc_inlbuf2)
946                 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
947         if (portal_ioctl_is_invalid(overlay))
948                 return 1;
949
950         return 0;
951 }
952 #else
953 #include <asm/uaccess.h>
954
955 /* buffer MUST be at least the size of portal_ioctl_hdr */
956 static inline int portal_ioctl_getdata(char *buf, char *end, void *arg)
957 {
958         struct portal_ioctl_hdr *hdr;
959         struct portal_ioctl_data *data;
960         int err;
961         ENTRY;
962
963         hdr = (struct portal_ioctl_hdr *)buf;
964         data = (struct portal_ioctl_data *)buf;
965
966         err = copy_from_user(buf, (void *)arg, sizeof(*hdr));
967         if ( err ) {
968                 EXIT;
969                 return err;
970         }
971
972         if (hdr->ioc_version != PORTAL_IOCTL_VERSION) {
973                 CERROR ("PORTALS: version mismatch kernel vs application\n");
974                 return -EINVAL;
975         }
976
977         if (hdr->ioc_len + buf >= end) {
978                 CERROR ("PORTALS: user buffer exceeds kernel buffer\n");
979                 return -EINVAL;
980         }
981
982
983         if (hdr->ioc_len < sizeof(struct portal_ioctl_data)) {
984                 CERROR ("PORTALS: user buffer too small for ioctl\n");
985                 return -EINVAL;
986         }
987
988         err = copy_from_user(buf, (void *)arg, hdr->ioc_len);
989         if ( err ) {
990                 EXIT;
991                 return err;
992         }
993
994         if (portal_ioctl_is_invalid(data)) {
995                 CERROR ("PORTALS: ioctl not correctly formatted\n");
996                 return -EINVAL;
997         }
998
999         if (data->ioc_inllen1) {
1000                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
1001         }
1002
1003         if (data->ioc_inllen2) {
1004                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
1005                         size_round(data->ioc_inllen1);
1006         }
1007
1008         EXIT;
1009         return 0;
1010 }
1011 #endif
1012
1013 /* ioctls for manipulating snapshots 30- */
1014 #define IOC_PORTAL_TYPE                   'e'
1015 #define IOC_PORTAL_MIN_NR                 30
1016
1017 #define IOC_PORTAL_PING                    _IOWR('e', 30, long)
1018 #define IOC_PORTAL_GET_DEBUG               _IOWR('e', 31, long)
1019 #define IOC_PORTAL_CLEAR_DEBUG             _IOWR('e', 32, long)
1020 #define IOC_PORTAL_MARK_DEBUG              _IOWR('e', 33, long)
1021 #define IOC_PORTAL_PANIC                   _IOWR('e', 34, long)
1022 #define IOC_PORTAL_NAL_CMD                 _IOWR('e', 35, long)
1023 #define IOC_PORTAL_GET_NID                 _IOWR('e', 36, long)
1024 #define IOC_PORTAL_FAIL_NID                _IOWR('e', 37, long)
1025 #define IOC_PORTAL_SET_DAEMON              _IOWR('e', 38, long)
1026 #define IOC_PORTAL_LWT_CONTROL             _IOWR('e', 39, long)
1027 #define IOC_PORTAL_LWT_SNAPSHOT            _IOWR('e', 40, long)
1028 #define IOC_PORTAL_LWT_LOOKUP_STRING       _IOWR('e', 41, long)
1029 #define IOC_PORTAL_MEMHOG                  _IOWR('e', 42, long)
1030 #define IOC_PORTAL_MAX_NR                             42
1031
1032 enum {
1033         QSWNAL    = 1,
1034         SOCKNAL   = 2,
1035         GMNAL     = 3,
1036         /*          4 unused */
1037         TCPNAL    = 5,
1038         SCIMACNAL = 6,
1039         ROUTER    = 7,
1040         IBNAL     = 8,
1041         NAL_ENUM_END_MARKER
1042 };
1043
1044 #ifdef __KERNEL__
1045 extern ptl_handle_ni_t  kqswnal_ni;
1046 extern ptl_handle_ni_t  ksocknal_ni;
1047 extern ptl_handle_ni_t  kgmnal_ni;
1048 extern ptl_handle_ni_t  kibnal_ni;
1049 extern ptl_handle_ni_t  kscimacnal_ni;
1050 #endif
1051
1052 #define PTL_NALFMT_SIZE         16
1053
1054 #define NAL_MAX_NR (NAL_ENUM_END_MARKER - 1)
1055
1056 #define NAL_CMD_REGISTER_PEER_FD     100
1057 #define NAL_CMD_CLOSE_CONNECTION     101
1058 #define NAL_CMD_REGISTER_MYNID       102
1059 #define NAL_CMD_PUSH_CONNECTION      103
1060 #define NAL_CMD_GET_CONN             104
1061 #define NAL_CMD_DEL_AUTOCONN         105
1062 #define NAL_CMD_ADD_AUTOCONN         106
1063 #define NAL_CMD_GET_AUTOCONN         107
1064 #define NAL_CMD_GET_TXDESC           108
1065 #define NAL_CMD_ADD_ROUTE            109
1066 #define NAL_CMD_DEL_ROUTE            110
1067 #define NAL_CMD_GET_ROUTE            111
1068 #define NAL_CMD_NOTIFY_ROUTER        112
1069
1070 enum {
1071         DEBUG_DAEMON_START       =  1,
1072         DEBUG_DAEMON_STOP        =  2,
1073         DEBUG_DAEMON_PAUSE       =  3,
1074         DEBUG_DAEMON_CONTINUE    =  4,
1075 };
1076
1077 /* XXX remove to lustre ASAP */
1078 struct lustre_peer {
1079         ptl_nid_t       peer_nid;
1080         ptl_handle_ni_t peer_ni;
1081 };
1082
1083
1084 /* module.c */
1085 typedef int (*nal_cmd_handler_t)(struct portals_cfg *, void * private);
1086 int kportal_nal_register(int nal, nal_cmd_handler_t handler, void * private);
1087 int kportal_nal_unregister(int nal);
1088
1089 enum cfg_record_type {
1090         PORTALS_CFG_TYPE = 1,
1091         LUSTRE_CFG_TYPE = 123,
1092 };
1093
1094 typedef int (*cfg_record_cb_t)(enum cfg_record_type, int len, void *data);
1095 int kportal_nal_cmd(struct portals_cfg *);
1096
1097 ptl_handle_ni_t *kportal_get_ni (int nal);
1098 void kportal_put_ni (int nal);
1099
1100 #ifdef __CYGWIN__
1101 # ifndef BITS_PER_LONG
1102 #  if (~0UL) == 0xffffffffUL
1103 #   define BITS_PER_LONG 32
1104 #  else
1105 #   define BITS_PER_LONG 64
1106 #  endif
1107 # endif
1108 #endif
1109
1110 #if defined(__x86_64__)
1111 # define LPU64 "%Lu"
1112 # define LPD64 "%Ld"
1113 # define LPX64 "%#Lx"
1114 # define LPSZ  "%lu"
1115 # define LPSSZ "%ld"
1116 #elif (BITS_PER_LONG == 32 || __WORDSIZE == 32)
1117 # define LPU64 "%Lu"
1118 # define LPD64 "%Ld"
1119 # define LPX64 "%#Lx"
1120 # define LPSZ  "%u"
1121 # define LPSSZ "%d"
1122 #elif (BITS_PER_LONG == 64 || __WORDSIZE == 64)
1123 # define LPU64 "%lu"
1124 # define LPD64 "%ld"
1125 # define LPX64 "%#lx"
1126 # define LPSZ  "%lu"
1127 # define LPSSZ "%ld"
1128 #endif
1129 #ifndef LPU64
1130 # error "No word size defined"
1131 #endif
1132
1133 #endif