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