Whamcloud - gitweb
c2a15f4087a140fb9ce6d1497d6548938f33d848
[fs/lustre-release.git] / lnet / 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
6
7 #define PORTAL_DEBUG
8
9 /* I think this beast is just trying to get cycles_t and get_cycles().
10  * this should be in its own header. */
11 #ifdef __linux__
12 # include <asm/types.h>
13 # if defined(__powerpc__) && !defined(__KERNEL__)
14 #  define __KERNEL__
15 #  include <asm/timex.h>
16 #  undef __KERNEL__
17 # else
18 #  if defined(__KERNEL__)
19 #   include <asm/timex.h>
20 #  else
21 #   include <sys/time.h>
22 #   define cycles_t unsigned long
23 static inline cycles_t get_cycles(void) 
24 {
25         struct timeval tv;
26         gettimeofday(&tv, NULL);
27         return (tv.tv_sec * 100000) + tv.tv_usec;
28 }
29 #  endif
30 # endif
31 #else
32 # include <sys/types.h>
33 typedef u_int32_t __u32;
34 typedef u_int64_t __u64;
35 #endif
36
37 #ifdef __KERNEL__
38 # include <linux/time.h>
39 #else
40 # include <sys/time.h>
41 # define do_gettimeofday(tv) gettimeofday(tv, NULL);
42 #endif
43
44 #ifndef offsetof
45 # define offsetof(typ,memb)     ((unsigned long)((char *)&(((typ *)0)->memb)))
46 #endif
47
48 #define LOWEST_BIT_SET(x)       ((x) & ~((x) - 1))
49
50 /*
51  *  Debugging
52  */
53 extern unsigned int portal_subsystem_debug;
54 extern unsigned int portal_stack;
55 extern unsigned int portal_debug;
56 extern unsigned int portal_printk;
57 extern unsigned int portal_cerror;
58 /* Debugging subsystems (32 bits, non-overlapping) */
59 #define S_UNDEFINED   0x00000001
60 #define S_MDC         0x00000002
61 #define S_MDS         0x00000004
62 #define S_OSC         0x00000008
63 #define S_OST         0x00000010
64 #define S_CLASS       0x00000020
65 #define S_LOG         0x00000040
66 #define S_LLITE       0x00000080
67 #define S_RPC         0x00000100
68 #define S_MGMT        0x00000200
69 #define S_PORTALS     0x00000400
70 #define S_SOCKNAL     0x00000800
71 #define S_QSWNAL      0x00001000
72 #define S_PINGER      0x00002000
73 #define S_FILTER      0x00004000
74 #define S_PTLBD       0x00008000
75 #define S_ECHO        0x00010000
76 #define S_LDLM        0x00020000
77 #define S_LOV         0x00040000
78 #define S_GMNAL       0x00080000
79 #define S_PTLROUTER   0x00100000
80 #define S_COBD        0x00200000
81 #define S_IBNAL       0x00400000
82 #define S_LMV         0x00800000
83 #define S_SM          0x01000000
84 #define S_CMOBD       0x02000000
85 /* If you change these values, please keep portals/utils/debug.c
86  * up to date! */
87
88 /* Debugging masks (32 bits, non-overlapping) */
89 #define D_TRACE       0x00000001 /* ENTRY/EXIT markers */
90 #define D_INODE       0x00000002
91 #define D_SUPER       0x00000004
92 #define D_EXT2        0x00000008 /* anything from ext2_debug */
93 #define D_MALLOC      0x00000010 /* print malloc, free information */
94 #define D_CACHE       0x00000020 /* cache-related items */
95 #define D_INFO        0x00000040 /* general information */
96 #define D_IOCTL       0x00000080 /* ioctl related information */
97 #define D_BLOCKS      0x00000100 /* ext2 block allocation */
98 #define D_NET         0x00000200 /* network communications */
99 #define D_WARNING     0x00000400 /* CWARN(...) == CDEBUG (D_WARNING, ...) */
100 #define D_BUFFS       0x00000800
101 #define D_OTHER       0x00001000
102 #define D_DENTRY      0x00002000
103 #define D_PORTALS     0x00004000 /* ENTRY/EXIT markers */
104 #define D_PAGE        0x00008000 /* bulk page handling */
105 #define D_DLMTRACE    0x00010000
106 #define D_ERROR       0x00020000 /* CERROR(...) == CDEBUG (D_ERROR, ...) */
107 #define D_EMERG       0x00040000 /* CEMERG(...) == CDEBUG (D_EMERG, ...) */
108 #define D_HA          0x00080000 /* recovery and failover */
109 #define D_RPCTRACE    0x00100000 /* for distributed debugging */
110 #define D_VFSTRACE    0x00200000
111 #define D_READA       0x00400000 /* read-ahead */
112
113 #ifdef __KERNEL__
114 # include <linux/sched.h> /* THREAD_SIZE */
115 #else
116 # ifndef THREAD_SIZE /* x86_64 has THREAD_SIZE in userspace */
117 #  define THREAD_SIZE 8192
118 # endif
119 #endif
120
121 #define LUSTRE_TRACE_SIZE (THREAD_SIZE >> 5)
122
123 #ifdef __KERNEL__
124 # ifdef  __ia64__
125 #  define CDEBUG_STACK (THREAD_SIZE -                                      \
126                         ((unsigned long)__builtin_dwarf_cfa() &            \
127                          (THREAD_SIZE - 1)))
128 # else
129 #  define CDEBUG_STACK (THREAD_SIZE -                                      \
130                         ((unsigned long)__builtin_frame_address(0) &       \
131                          (THREAD_SIZE - 1)))
132 # endif
133
134 #define CHECK_STACK(stack)                                                    \
135         do {                                                                  \
136                 if ((stack) > 3*THREAD_SIZE/4 && (stack) > portal_stack) {    \
137                         portals_debug_msg(DEBUG_SUBSYSTEM, D_WARNING,         \
138                                           __FILE__, __FUNCTION__, __LINE__,   \
139                                           (stack),"maximum lustre stack %u\n",\
140                                           portal_stack = (stack));            \
141                       /*panic("LBUG");*/                                      \
142                 }                                                             \
143         } while (0)
144 #else /* __KERNEL__ */
145 #define CHECK_STACK(stack) do { } while(0)
146 #define CDEBUG_STACK (0L)
147 #endif /* __KERNEL__ */
148
149 #if 1
150 #define CDEBUG(mask, format, a...)                                            \
151 do {                                                                          \
152         if (portal_cerror == 0)                                               \
153                 break;                                                        \
154         CHECK_STACK(CDEBUG_STACK);                                            \
155         if (((mask) & (D_ERROR | D_EMERG | D_WARNING)) ||                     \
156             (portal_debug & (mask) &&                                         \
157              portal_subsystem_debug & DEBUG_SUBSYSTEM))                       \
158                 portals_debug_msg(DEBUG_SUBSYSTEM, mask,                      \
159                                   __FILE__, __FUNCTION__, __LINE__,           \
160                                   CDEBUG_STACK, format, ## a);                \
161 } while (0)
162
163 #define CWARN(format, a...) CDEBUG(D_WARNING, format, ## a)
164 #define CERROR(format, a...) CDEBUG(D_ERROR, format, ## a)
165 #define CEMERG(format, a...) CDEBUG(D_EMERG, format, ## a)
166
167 #define GOTO(label, rc)                                                 \
168 do {                                                                    \
169         long GOTO__ret = (long)(rc);                                    \
170         CDEBUG(D_TRACE,"Process leaving via %s (rc=%lu : %ld : %lx)\n", \
171                #label, (unsigned long)GOTO__ret, (signed long)GOTO__ret,\
172                (signed long)GOTO__ret);                                 \
173         goto label;                                                     \
174 } while (0)
175
176 #define RETURN(rc)                                                      \
177 do {                                                                    \
178         typeof(rc) RETURN__ret = (rc);                                  \
179         CDEBUG(D_TRACE, "Process leaving (rc=%lu : %ld : %lx)\n",       \
180                (long)RETURN__ret, (long)RETURN__ret, (long)RETURN__ret);\
181         return RETURN__ret;                                             \
182 } while (0)
183
184 #define ENTRY                                                           \
185 do {                                                                    \
186         CDEBUG(D_TRACE, "Process entered\n");                           \
187 } while (0)
188
189 #define EXIT                                                            \
190 do {                                                                    \
191         CDEBUG(D_TRACE, "Process leaving\n");                           \
192 } while(0)
193 #else
194 #define CDEBUG(mask, format, a...)      do { } while (0)
195 #define CWARN(format, a...)             do { } while (0)
196 #define CERROR(format, a...)            printk("<3>" format, ## a)
197 #define CEMERG(format, a...)            printk("<0>" format, ## a)
198 #define GOTO(label, rc)                 do { (void)(rc); goto label; } while (0)
199 #define RETURN(rc)                      return (rc)
200 #define ENTRY                           do { } while (0)
201 #define EXIT                            do { } while (0)
202 #endif
203
204 #define PORTALS_CFG_VERSION 0x00010001;
205
206 struct portals_cfg {
207         __u32 pcfg_version;
208         __u32 pcfg_command;
209
210         __u32 pcfg_nal;
211         __u32 pcfg_flags;
212
213         __u32 pcfg_gw_nal;
214         __u64 pcfg_nid;
215         __u64 pcfg_nid2;
216         __u64 pcfg_nid3;
217         __u32 pcfg_id;
218         __u32 pcfg_misc;
219         __u32 pcfg_fd;
220         __u32 pcfg_count;
221         __u32 pcfg_size;
222         __u32 pcfg_wait;
223
224         __u32 pcfg_plen1; /* buffers in userspace */
225         char *pcfg_pbuf1;
226         __u32 pcfg_plen2; /* buffers in userspace */
227         char *pcfg_pbuf2;
228 };
229
230 #define PCFG_INIT(pcfg, cmd)                            \
231 do {                                                    \
232         memset(&pcfg, 0, sizeof(pcfg));                 \
233         pcfg.pcfg_version = PORTALS_CFG_VERSION;        \
234         pcfg.pcfg_command = (cmd);                      \
235                                                         \
236 } while (0)
237
238 typedef int (nal_cmd_handler_fn)(struct portals_cfg *, void *);
239 int libcfs_nal_cmd_register(int nal, nal_cmd_handler_fn *handler, void *arg);
240 int libcfs_nal_cmd(struct portals_cfg *pcfg);
241 void libcfs_nal_cmd_unregister(int nal);
242
243 struct portal_ioctl_data {
244         __u32 ioc_len;
245         __u32 ioc_version;
246         __u64 ioc_nid;
247         __u64 ioc_nid2;
248         __u64 ioc_nid3;
249         __u32 ioc_count;
250         __u32 ioc_nal;
251         __u32 ioc_nal_cmd;
252         __u32 ioc_fd;
253         __u32 ioc_id;
254
255         __u32 ioc_flags;
256         __u32 ioc_size;
257
258         __u32 ioc_wait;
259         __u32 ioc_timeout;
260         __u32 ioc_misc;
261
262         __u32 ioc_inllen1;
263         char *ioc_inlbuf1;
264         __u32 ioc_inllen2;
265         char *ioc_inlbuf2;
266
267         __u32 ioc_plen1; /* buffers in userspace */
268         char *ioc_pbuf1;
269         __u32 ioc_plen2; /* buffers in userspace */
270         char *ioc_pbuf2;
271
272         char ioc_bulk[0];
273 };
274
275
276 #ifdef __KERNEL__
277
278 #include <linux/list.h>
279
280 struct libcfs_ioctl_handler {
281         struct list_head item;
282         int (*handle_ioctl)(struct portal_ioctl_data *data,
283                             unsigned int cmd, unsigned long args);
284 };
285
286 #define DECLARE_IOCTL_HANDLER(ident, func)              \
287         struct libcfs_ioctl_handler ident = {           \
288                 .item = LIST_HEAD_INIT(ident.item),     \
289                 .handle_ioctl = func                    \
290         }
291
292 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand);
293 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand);
294
295 #endif
296
297 #define _LIBCFS_H
298
299 #endif /* _LIBCFS_H */