Whamcloud - gitweb
8f8d6fe3073a78c03e5f4525738903ac8263baaf
[fs/lustre-release.git] / lustre / portals / include / linux / libcfs.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4 #ifndef _LIBCFS_H
5 #define _LIBCFS_H
6
7 #ifdef HAVE_ASM_TYPES_H
8 #include <asm/types.h>
9 #else
10 #include "types.h"
11 #endif
12
13 #ifdef __KERNEL__
14 # include <linux/time.h>
15 # include <asm/timex.h>
16 #else
17 # include <sys/time.h>
18 # define do_gettimeofday(tv) gettimeofday(tv, NULL);
19 typedef unsigned long long cycles_t;
20 #endif
21
22 #define PORTAL_DEBUG
23
24 #ifndef offsetof
25 # define offsetof(typ,memb)     ((unsigned long)((char *)&(((typ *)0)->memb)))
26 #endif
27
28 #define LOWEST_BIT_SET(x)       ((x) & ~((x) - 1))
29
30 #ifndef __KERNEL__
31 /* Userpace byte flipping */
32 # include <endian.h>
33 # include <byteswap.h>
34 # define __swab16(x) bswap_16(x)
35 # define __swab32(x) bswap_32(x)
36 # define __swab64(x) bswap_64(x)
37 # define __swab16s(x) do {*(x) = bswap_16(*(x));} while (0)
38 # define __swab32s(x) do {*(x) = bswap_32(*(x));} while (0)
39 # define __swab64s(x) do {*(x) = bswap_64(*(x));} while (0)
40 # if __BYTE_ORDER == __LITTLE_ENDIAN
41 #  define le16_to_cpu(x) (x)
42 #  define cpu_to_le16(x) (x)
43 #  define le32_to_cpu(x) (x)
44 #  define cpu_to_le32(x) (x)
45 #  define le64_to_cpu(x) (x)
46 #  define cpu_to_le64(x) (x)
47 # else
48 #  if __BYTE_ORDER == __BIG_ENDIAN
49 #   define le16_to_cpu(x) bswap_16(x)
50 #   define cpu_to_le16(x) bswap_16(x)
51 #   define le32_to_cpu(x) bswap_32(x)
52 #   define cpu_to_le32(x) bswap_32(x)
53 #   define le64_to_cpu(x) bswap_64(x)
54 #   define cpu_to_le64(x) bswap_64(x)
55 #  else
56 #   error "Unknown byte order"
57 #  endif /* __BIG_ENDIAN */
58 # endif /* __LITTLE_ENDIAN */
59 #endif /* ! __KERNEL__ */
60
61 /*
62  *  Debugging
63  */
64 extern unsigned int portal_subsystem_debug;
65 extern unsigned int portal_stack;
66 extern unsigned int portal_debug;
67 extern unsigned int portal_printk;
68
69 struct ptldebug_header {
70         __u32 ph_len;
71         __u32 ph_flags;
72         __u32 ph_subsys;
73         __u32 ph_mask;
74         __u32 ph_cpu_id;
75         __u32 ph_sec;
76         __u64 ph_usec;
77         __u32 ph_stack;
78         __u32 ph_pid;
79         __u32 ph_extern_pid;
80         __u32 ph_line_num;
81 } __attribute__((packed));
82
83 #define PH_FLAG_FIRST_RECORD 1
84
85 /* Debugging subsystems (32 bits, non-overlapping) */
86 #define S_UNDEFINED   0x00000001
87 #define S_MDC         0x00000002
88 #define S_MDS         0x00000004
89 #define S_OSC         0x00000008
90 #define S_OST         0x00000010
91 #define S_CLASS       0x00000020
92 #define S_LOG         0x00000040
93 #define S_LLITE       0x00000080
94 #define S_RPC         0x00000100
95 #define S_MGMT        0x00000200
96 #define S_PORTALS     0x00000400
97 #define S_SOCKNAL     0x00000800
98 #define S_QSWNAL      0x00001000
99 #define S_PINGER      0x00002000
100 #define S_FILTER      0x00004000
101 #define S_PTLBD       0x00008000
102 #define S_ECHO        0x00010000
103 #define S_LDLM        0x00020000
104 #define S_LOV         0x00040000
105 #define S_GMNAL       0x00080000
106 #define S_PTLROUTER   0x00100000
107 #define S_COBD        0x00200000
108 #define S_IBNAL       0x00400000 /* All IB NALs */
109 #define S_SM          0x00800000
110 #define S_ASOBD       0x01000000
111 #define S_CONFOBD     0x02000000
112 #define S_LMV         0x04000000
113 #define S_CMOBD       0x08000000
114 #define S_LONAL       0x10000000
115 /* If you change these values, please keep these files up to date...
116  *    portals/utils/debug.c
117  *    utils/lconf
118  */
119
120 /* Debugging masks (32 bits, non-overlapping) */
121 #define D_TRACE       0x00000001 /* ENTRY/EXIT markers */
122 #define D_INODE       0x00000002
123 #define D_SUPER       0x00000004
124 #define D_EXT2        0x00000008 /* anything from ext2_debug */
125 #define D_MALLOC      0x00000010 /* print malloc, free information */
126 #define D_CACHE       0x00000020 /* cache-related items */
127 #define D_INFO        0x00000040 /* general information */
128 #define D_IOCTL       0x00000080 /* ioctl related information */
129 #define D_BLOCKS      0x00000100 /* ext2 block allocation */
130 #define D_NET         0x00000200 /* network communications */
131 #define D_WARNING     0x00000400 /* CWARN(...) == CDEBUG (D_WARNING, ...) */
132 #define D_BUFFS       0x00000800
133 #define D_OTHER       0x00001000
134 #define D_DENTRY      0x00002000
135 #define D_PORTALS     0x00004000 /* ENTRY/EXIT markers */
136 #define D_PAGE        0x00008000 /* bulk page handling */
137 #define D_DLMTRACE    0x00010000
138 #define D_ERROR       0x00020000 /* CERROR(...) == CDEBUG (D_ERROR, ...) */
139 #define D_EMERG       0x00040000 /* CEMERG(...) == CDEBUG (D_EMERG, ...) */
140 #define D_HA          0x00080000 /* recovery and failover */
141 #define D_RPCTRACE    0x00100000 /* for distributed debugging */
142 #define D_VFSTRACE    0x00200000
143 #define D_READA       0x00400000 /* read-ahead */
144 #define D_MMAP        0x00800000
145 #define D_CONFIG      0x01000000
146 /* If you change these values, please keep these files up to date...
147  *    portals/utils/debug.c
148  *    utils/lconf
149  */
150
151 #ifdef __KERNEL__
152 # include <linux/sched.h> /* THREAD_SIZE */
153 #else
154 # ifndef THREAD_SIZE /* x86_64 has THREAD_SIZE in userspace */
155 #  define THREAD_SIZE 8192
156 # endif
157 #endif
158
159 #define LUSTRE_TRACE_SIZE (THREAD_SIZE >> 5)
160
161 #ifdef __KERNEL__
162 # ifdef  __ia64__
163 #  define CDEBUG_STACK (THREAD_SIZE -                                      \
164                         ((unsigned long)__builtin_dwarf_cfa() &            \
165                          (THREAD_SIZE - 1)))
166 # else
167 #  define CDEBUG_STACK (THREAD_SIZE -                                      \
168                         ((unsigned long)__builtin_frame_address(0) &       \
169                          (THREAD_SIZE - 1)))
170 # endif /* __ia64__ */
171
172 #define CHECK_STACK(stack)                                                    \
173         do {                                                                  \
174                 if ((stack) > 3*THREAD_SIZE/4 && (stack) > portal_stack) {    \
175                         portals_debug_msg(DEBUG_SUBSYSTEM, D_WARNING,         \
176                                           __FILE__, __FUNCTION__, __LINE__,   \
177                                           (stack),"maximum lustre stack %u\n",\
178                                           portal_stack = (stack));            \
179                       /*panic("LBUG");*/                                      \
180                 }                                                             \
181         } while (0)
182 #else /* !__KERNEL__ */
183 #define CHECK_STACK(stack) do { } while(0)
184 #define CDEBUG_STACK (0L)
185 #endif /* __KERNEL__ */
186
187 #if 1
188 #define CDEBUG(mask, format, a...)                                            \
189 do {                                                                          \
190         CHECK_STACK(CDEBUG_STACK);                                            \
191         if (((mask) & (D_ERROR | D_EMERG | D_WARNING)) ||                     \
192             (portal_debug & (mask) &&                                         \
193              portal_subsystem_debug & DEBUG_SUBSYSTEM))                       \
194                 portals_debug_msg(DEBUG_SUBSYSTEM, mask,                      \
195                                   __FILE__, __FUNCTION__, __LINE__,           \
196                                   CDEBUG_STACK, format, ## a);                \
197 } while (0)
198
199 #define CDEBUG_MAX_LIMIT 600
200 #define CDEBUG_LIMIT(cdebug_mask, cdebug_format, a...)                        \
201 do {                                                                          \
202         static unsigned long cdebug_next = 0;                                 \
203         static int cdebug_count = 0, cdebug_delay = 1;                        \
204                                                                               \
205         CHECK_STACK(CDEBUG_STACK);                                            \
206         if (time_after(jiffies, cdebug_next)) {                               \
207                 portals_debug_msg(DEBUG_SUBSYSTEM, cdebug_mask, __FILE__,     \
208                                   __FUNCTION__, __LINE__, CDEBUG_STACK,       \
209                                   cdebug_format, ## a);                       \
210                 if (cdebug_count) {                                           \
211                         portals_debug_msg(DEBUG_SUBSYSTEM, cdebug_mask,       \
212                                           __FILE__, __FUNCTION__, __LINE__,   \
213                                           0, "skipped %d similar messages\n", \
214                                           cdebug_count);                      \
215                         cdebug_count = 0;                                     \
216                 }                                                             \
217                 if (time_after(jiffies, cdebug_next+(CDEBUG_MAX_LIMIT+10)*HZ))\
218                         cdebug_delay = cdebug_delay > 8 ? cdebug_delay/8 : 1; \
219                 else                                                          \
220                         cdebug_delay = cdebug_delay*2 >= CDEBUG_MAX_LIMIT*HZ ?\
221                                         CDEBUG_MAX_LIMIT*HZ : cdebug_delay*2; \
222                 cdebug_next = jiffies + cdebug_delay;                         \
223         } else {                                                              \
224                 portals_debug_msg(DEBUG_SUBSYSTEM,                            \
225                                   portal_debug & ~(D_EMERG|D_ERROR|D_WARNING),\
226                                   __FILE__, __FUNCTION__, __LINE__,           \
227                                   CDEBUG_STACK, cdebug_format, ## a);         \
228                 cdebug_count++;                                               \
229         }                                                                     \
230 } while (0)
231
232 #define CWARN(format, a...) CDEBUG_LIMIT(D_WARNING, format, ## a)
233 #define CERROR(format, a...) CDEBUG_LIMIT(D_ERROR, format, ## a)
234 #define CEMERG(format, a...) CDEBUG(D_EMERG, format, ## a)
235
236 #define GOTO(label, rc)                                                 \
237 do {                                                                    \
238         long GOTO__ret = (long)(rc);                                    \
239         CDEBUG(D_TRACE,"Process leaving via %s (rc=%lu : %ld : %lx)\n", \
240                #label, (unsigned long)GOTO__ret, (signed long)GOTO__ret,\
241                (signed long)GOTO__ret);                                 \
242         goto label;                                                     \
243 } while (0)
244
245 #define RETURN(rc)                                                      \
246 do {                                                                    \
247         typeof(rc) RETURN__ret = (rc);                                  \
248         CDEBUG(D_TRACE, "Process leaving (rc=%lu : %ld : %lx)\n",       \
249                (long)RETURN__ret, (long)RETURN__ret, (long)RETURN__ret);\
250         return RETURN__ret;                                             \
251 } while (0)
252
253 #define ENTRY                                                           \
254 do {                                                                    \
255         CDEBUG(D_TRACE, "Process entered\n");                           \
256 } while (0)
257
258 #define EXIT                                                            \
259 do {                                                                    \
260         CDEBUG(D_TRACE, "Process leaving\n");                           \
261 } while(0)
262 #else
263 #define CDEBUG(mask, format, a...)      do { } while (0)
264 #define CWARN(format, a...)             printk(KERN_WARNING format, ## a)
265 #define CERROR(format, a...)            printk(KERN_ERR format, ## a)
266 #define CEMERG(format, a...)            printk(KERN_EMERG format, ## a)
267 #define GOTO(label, rc)                 do { (void)(rc); goto label; } while (0)
268 #define RETURN(rc)                      return (rc)
269 #define ENTRY                           do { } while (0)
270 #define EXIT                            do { } while (0)
271 #endif
272
273 /* initial pid  */
274 # if CRAY_PORTALS
275 /* 
276  * 1) ptl_pid_t in cray portals is only 16 bits, not 32 bits, therefore this
277  *    is too big.
278  *
279  * 2) the implementation of ernal in cray portals further restricts the pid
280  *    space that may be used to 0 <= pid <= 255 (an 8 bit value).  Returns
281  *    an error at nal init time for any pid outside this range.  Other nals
282  *    in cray portals don't have this restriction.
283  * */
284 #define LUSTRE_PTL_PID          9
285 # else
286 #define LUSTRE_PTL_PID          12345
287 # endif
288
289 #define LUSTRE_SRV_PTL_PID      LUSTRE_PTL_PID    
290
291 #define PORTALS_CFG_VERSION 0x00010001;
292
293 struct portals_cfg {
294         __u32 pcfg_version;
295         __u32 pcfg_command;
296
297         __u32 pcfg_nal;
298         __u32 pcfg_flags;
299
300         __u32 pcfg_gw_nal;
301         __u64 pcfg_nid;
302         __u64 pcfg_nid2;
303         __u64 pcfg_nid3;
304         __u32 pcfg_id;
305         __u32 pcfg_misc;
306         __u32 pcfg_fd;
307         __u32 pcfg_count;
308         __u32 pcfg_size;
309         __u32 pcfg_wait;
310
311         __u32 pcfg_plen1; /* buffers in userspace */
312         char *pcfg_pbuf1;
313         __u32 pcfg_plen2; /* buffers in userspace */
314         char *pcfg_pbuf2;
315 };
316
317 #define PCFG_INIT(pcfg, cmd)                            \
318 do {                                                    \
319         memset(&pcfg, 0, sizeof(pcfg));                 \
320         pcfg.pcfg_version = PORTALS_CFG_VERSION;        \
321         pcfg.pcfg_command = (cmd);                      \
322                                                         \
323 } while (0)
324
325 typedef int (nal_cmd_handler_fn)(struct portals_cfg *, void *);
326 int libcfs_nal_cmd_register(int nal, nal_cmd_handler_fn *handler, void *arg);
327 int libcfs_nal_cmd(struct portals_cfg *pcfg);
328 void libcfs_nal_cmd_unregister(int nal);
329
330 struct portal_ioctl_data {
331         __u32 ioc_len;
332         __u32 ioc_version;
333         __u64 ioc_nid;
334         __u64 ioc_nid2;
335         __u64 ioc_nid3;
336         __u32 ioc_count;
337         __u32 ioc_nal;
338         __u32 ioc_nal_cmd;
339         __u32 ioc_fd;
340         __u32 ioc_id;
341
342         __u32 ioc_flags;
343         __u32 ioc_size;
344
345         __u32 ioc_wait;
346         __u32 ioc_timeout;
347         __u32 ioc_misc;
348
349         __u32 ioc_inllen1;
350         char *ioc_inlbuf1;
351         __u32 ioc_inllen2;
352         char *ioc_inlbuf2;
353
354         __u32 ioc_plen1; /* buffers in userspace */
355         char *ioc_pbuf1;
356         __u32 ioc_plen2; /* buffers in userspace */
357         char *ioc_pbuf2;
358
359         char ioc_bulk[0];
360 };
361
362
363 #ifdef __KERNEL__
364
365 #include <linux/list.h>
366
367 struct libcfs_ioctl_handler {
368         struct list_head item;
369         int (*handle_ioctl)(struct portal_ioctl_data *data,
370                             unsigned int cmd, unsigned long args);
371 };
372
373 #define DECLARE_IOCTL_HANDLER(ident, func)              \
374         struct libcfs_ioctl_handler ident = {           \
375                 .item = LIST_HEAD_INIT(ident.item),     \
376                 .handle_ioctl = func                    \
377         }
378
379 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand);
380 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand);
381
382 #endif
383
384 #ifdef __KERNEL__
385 /* libcfs watchdogs */
386 struct lc_watchdog;
387
388 /* Just use the default handler (dumplog)  */
389 #define LC_WATCHDOG_DEFAULT_CB NULL
390
391 /* Add a watchdog which fires after "time" milliseconds of delay.  You have to
392  * touch it once to enable it. */
393 struct lc_watchdog *lc_watchdog_add(int time, 
394                                     void (*cb)(struct lc_watchdog *,
395                                                struct task_struct *,
396                                                void *),
397                                     void *data);
398
399 /* Enables a watchdog and resets its timer. */
400 void lc_watchdog_touch(struct lc_watchdog *lcw);
401
402 /* Disable a watchdog; touch it to restart it. */
403 void lc_watchdog_disable(struct lc_watchdog *lcw);
404
405 /* Clean up the watchdog */
406 void lc_watchdog_delete(struct lc_watchdog *lcw);
407
408 /* Dump a debug log */
409 void lc_watchdog_dumplog(struct lc_watchdog *lcw,
410                          struct task_struct *tsk,
411                          void *data);
412 #endif /* !__KERNEL__ */
413
414 #endif /* _LIBCFS_H */