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