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