Whamcloud - gitweb
b=bug4165
[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
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_SM          0x00800000
83 #define S_ASOBD       0x01000000
84 #define S_LMV         0x02000000
85 #define S_CMOBD       0x04000000
86
87 /* If you change these values, please keep portals/utils/debug.c
88  * up to date! */
89
90 /* Debugging masks (32 bits, non-overlapping) */
91 #define D_TRACE       0x00000001 /* ENTRY/EXIT markers */
92 #define D_INODE       0x00000002
93 #define D_SUPER       0x00000004
94 #define D_EXT2        0x00000008 /* anything from ext2_debug */
95 #define D_MALLOC      0x00000010 /* print malloc, free information */
96 #define D_CACHE       0x00000020 /* cache-related items */
97 #define D_INFO        0x00000040 /* general information */
98 #define D_IOCTL       0x00000080 /* ioctl related information */
99 #define D_BLOCKS      0x00000100 /* ext2 block allocation */
100 #define D_NET         0x00000200 /* network communications */
101 #define D_WARNING     0x00000400 /* CWARN(...) == CDEBUG (D_WARNING, ...) */
102 #define D_BUFFS       0x00000800
103 #define D_OTHER       0x00001000
104 #define D_DENTRY      0x00002000
105 #define D_PORTALS     0x00004000 /* ENTRY/EXIT markers */
106 #define D_PAGE        0x00008000 /* bulk page handling */
107 #define D_DLMTRACE    0x00010000
108 #define D_ERROR       0x00020000 /* CERROR(...) == CDEBUG (D_ERROR, ...) */
109 #define D_EMERG       0x00040000 /* CEMERG(...) == CDEBUG (D_EMERG, ...) */
110 #define D_HA          0x00080000 /* recovery and failover */
111 #define D_RPCTRACE    0x00100000 /* for distributed debugging */
112 #define D_VFSTRACE    0x00200000
113 #define D_READA       0x00400000 /* read-ahead */
114 #define D_CONFIG      0x00800000
115
116 #ifdef __KERNEL__
117 # include <linux/sched.h> /* THREAD_SIZE */
118 #else
119 # ifndef THREAD_SIZE /* x86_64 has THREAD_SIZE in userspace */
120 #  define THREAD_SIZE 8192
121 # endif
122 #endif
123
124 #define LUSTRE_TRACE_SIZE (THREAD_SIZE >> 5)
125
126 //#ifdef __KERNEL__
127 #if 0
128 # ifdef  __ia64__
129 #  define CDEBUG_STACK (THREAD_SIZE -                                      \
130                         ((unsigned long)__builtin_dwarf_cfa() &            \
131                          (THREAD_SIZE - 1)))
132 # else
133 #  define CDEBUG_STACK (THREAD_SIZE -                                      \
134                         ((unsigned long)__builtin_frame_address(0) &       \
135                          (THREAD_SIZE - 1)))
136 # endif
137
138 #define CHECK_STACK(stack)                                                    \
139         do {                                                                  \
140                 if ((stack) > 3*THREAD_SIZE/4 && (stack) > portal_stack) {    \
141                         portals_debug_msg(DEBUG_SUBSYSTEM, D_WARNING,         \
142                                           __FILE__, __FUNCTION__, __LINE__,   \
143                                           (stack),"maximum lustre stack %u\n",\
144                                           portal_stack = (stack));            \
145                       /*panic("LBUG");*/                                      \
146                 }                                                             \
147         } while (0)
148 #else /* __KERNEL__ */
149 #define CHECK_STACK(stack) do { } while(0)
150 #define CDEBUG_STACK (0L)
151 #endif /* __KERNEL__ */
152
153 #if 1
154 #define CDEBUG(mask, format, a...)                                            \
155 do {                                                                          \
156         if (likely(portal_debug == 0))                                        \
157                 break;                                                        \
158         if (((mask) & (D_ERROR | D_EMERG | D_WARNING)) ||                     \
159             (portal_debug & (mask) &&                                         \
160              portal_subsystem_debug & DEBUG_SUBSYSTEM))                       \
161                 portals_debug_msg(DEBUG_SUBSYSTEM, mask,                      \
162                                   __FILE__, __FUNCTION__, __LINE__,           \
163                                   CDEBUG_STACK, format, ## a);                \
164 } while (0)
165
166 #define CWARN(format, a...) \
167 do {                                                                          \
168                 portals_debug_msg(DEBUG_SUBSYSTEM, D_WARNING,                 \
169                                   __FILE__, __FUNCTION__, __LINE__,           \
170                                   CDEBUG_STACK, format, ## a);                \
171 } while (0)
172
173 #define CERROR(format, a...)  \
174 do {                                                                          \
175                 portals_debug_msg(DEBUG_SUBSYSTEM, D_ERROR,                 \
176                                   __FILE__, __FUNCTION__, __LINE__,           \
177                                   CDEBUG_STACK, format, ## a);                \
178 } while (0)
179
180 #define CEMERG(format, a...) \
181 do {                                                                          \
182                 portals_debug_msg(DEBUG_SUBSYSTEM, D_EMERG,                 \
183                                   __FILE__, __FUNCTION__, __LINE__,           \
184                                   CDEBUG_STACK, format, ## a);                \
185 } while (0)
186
187 #define GOTO(label, rc)                                                 \
188 do {                                                                    \
189         long GOTO__ret = (long)(rc);                                    \
190         CDEBUG(D_TRACE,"Process leaving via %s (rc=%lu : %ld : %lx)\n", \
191                #label, (unsigned long)GOTO__ret, (signed long)GOTO__ret,\
192                (signed long)GOTO__ret);                                 \
193         goto label;                                                     \
194 } while (0)
195
196 #define RETURN(rc)                                                      \
197 do {                                                                    \
198         typeof(rc) RETURN__ret = (rc);                                  \
199         CDEBUG(D_TRACE, "Process leaving (rc=%lu : %ld : %lx)\n",       \
200                (long)RETURN__ret, (long)RETURN__ret, (long)RETURN__ret);\
201         return RETURN__ret;                                             \
202 } while (0)
203
204 #define ENTRY                                                           \
205 do {                                                                    \
206         CDEBUG(D_TRACE, "Process entered\n");                           \
207 } while (0)
208
209 #define EXIT                                                            \
210 do {                                                                    \
211         CDEBUG(D_TRACE, "Process leaving\n");                           \
212 } while(0)
213 #else
214 #define CDEBUG(mask, format, a...)      do { } while (0)
215 #define CWARN(format, a...)             printk("<4>" format, ## a)
216 #define CERROR(format, a...)            printk("<3>" format, ## a)
217 #define CEMERG(format, a...)            printk("<0>" format, ## a)
218 #define GOTO(label, rc)                 do { (void)(rc); goto label; } while (0)
219 #define RETURN(rc)                      return (rc)
220 #define ENTRY                           do { } while (0)
221 #define EXIT                            do { } while (0)
222 #endif
223
224 /* initial pid  */
225 # if CRAY_PORTALS
226 /* 
227  *
228  * 1) ptl_pid_t in cray portals is only 16 bits, not 32 bits, therefore this is too
229  * big.
230  *
231  * 2) the implementation of ernal in cray portals further restricts the pid space
232  * that may be used to 0 <= pid <= 255 (an 8 bit value).  Returns an error at nal
233  * init time for any pid outside this range.  Other nals in cray portals don't have
234  * this restriction.
235  * */
236 #define LUSTRE_PTL_PID          9
237 # else
238 #define LUSTRE_PTL_PID          12345
239 # endif
240
241 #define LUSTRE_SRV_PTL_PID      LUSTRE_PTL_PID    
242
243 #define PORTALS_CFG_VERSION 0x00010001;
244
245 struct portals_cfg {
246         __u32 pcfg_version;
247         __u32 pcfg_command;
248
249         __u32 pcfg_nal;
250         __u32 pcfg_flags;
251
252         __u32 pcfg_gw_nal;
253         __u64 pcfg_nid;
254         __u64 pcfg_nid2;
255         __u64 pcfg_nid3;
256         __u32 pcfg_id;
257         __u32 pcfg_misc;
258         __u32 pcfg_fd;
259         __u32 pcfg_count;
260         __u32 pcfg_size;
261         __u32 pcfg_wait;
262
263         __u32 pcfg_plen1; /* buffers in userspace */
264         char *pcfg_pbuf1;
265         __u32 pcfg_plen2; /* buffers in userspace */
266         char *pcfg_pbuf2;
267 };
268
269 #define PCFG_INIT(pcfg, cmd)                            \
270 do {                                                    \
271         memset(&pcfg, 0, sizeof(pcfg));                 \
272         pcfg.pcfg_version = PORTALS_CFG_VERSION;        \
273         pcfg.pcfg_command = (cmd);                      \
274                                                         \
275 } while (0)
276
277 typedef int (nal_cmd_handler_fn)(struct portals_cfg *, void *);
278 int libcfs_nal_cmd_register(int nal, nal_cmd_handler_fn *handler, void *arg);
279 int libcfs_nal_cmd(struct portals_cfg *pcfg);
280 void libcfs_nal_cmd_unregister(int nal);
281
282 struct portal_ioctl_data {
283         __u32 ioc_len;
284         __u32 ioc_version;
285         __u64 ioc_nid;
286         __u64 ioc_nid2;
287         __u64 ioc_nid3;
288         __u32 ioc_count;
289         __u32 ioc_nal;
290         __u32 ioc_nal_cmd;
291         __u32 ioc_fd;
292         __u32 ioc_id;
293
294         __u32 ioc_flags;
295         __u32 ioc_size;
296
297         __u32 ioc_wait;
298         __u32 ioc_timeout;
299         __u32 ioc_misc;
300
301         __u32 ioc_inllen1;
302         char *ioc_inlbuf1;
303         __u32 ioc_inllen2;
304         char *ioc_inlbuf2;
305
306         __u32 ioc_plen1; /* buffers in userspace */
307         char *ioc_pbuf1;
308         __u32 ioc_plen2; /* buffers in userspace */
309         char *ioc_pbuf2;
310
311         char ioc_bulk[0];
312 };
313
314
315 #ifdef __KERNEL__
316
317 #include <linux/list.h>
318
319 struct libcfs_ioctl_handler {
320         struct list_head item;
321         int (*handle_ioctl)(struct portal_ioctl_data *data,
322                             unsigned int cmd, unsigned long args);
323 };
324
325 #define DECLARE_IOCTL_HANDLER(ident, func)              \
326         struct libcfs_ioctl_handler ident = {           \
327                 .item = LIST_HEAD_INIT(ident.item),     \
328                 .handle_ioctl = func                    \
329         }
330
331 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand);
332 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand);
333
334 #endif
335
336 #define _LIBCFS_H
337
338 #endif /* _LIBCFS_H */