Whamcloud - gitweb
aceaa2c0ea34e9da21d778ca961306203d7d9d4a
[fs/lustre-release.git] / lnet / include / libcfs / linux / kp30.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4 #ifndef __LIBCFS_LINUX_KP30_H__
5 #define __LIBCFS_LINUX_KP30_H__
6
7 #ifndef __LIBCFS_KP30_H__
8 #error Do not #include this file directly. #include <libcfs/kp30.h> instead
9 #endif
10
11 #ifdef __KERNEL__
12 #ifndef AUTOCONF_INCLUDED
13 # include <linux/config.h>
14 #endif
15 # include <linux/kernel.h>
16 # include <linux/mm.h>
17 # include <linux/string.h>
18 # include <linux/stat.h>
19 # include <linux/init.h>
20 # include <linux/errno.h>
21 # include <linux/unistd.h>
22 # include <asm/system.h>
23 # include <linux/kmod.h>
24 # include <linux/notifier.h>
25 # include <linux/fs.h>
26 # include <linux/miscdevice.h>
27 # include <linux/vmalloc.h>
28 # include <linux/time.h>
29 # include <linux/slab.h>
30 # include <linux/interrupt.h>
31 # include <linux/highmem.h>
32 # include <linux/module.h>
33 # include <linux/version.h>
34 # include <lnet/lnet.h>
35 # include <linux/smp_lock.h>
36 # include <asm/atomic.h>
37 # include <asm/uaccess.h>
38 # include <linux/rwsem.h>
39 # include <linux/proc_fs.h>
40 # include <linux/file.h>
41 # include <linux/smp.h>
42 # include <linux/ctype.h>
43 # include <linux/compiler.h>
44 # ifdef HAVE_MM_INLINE
45 #  include <linux/mm_inline.h>
46 # endif
47 # if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
48 #  include <linux/kallsyms.h>
49 #  include <linux/moduleparam.h>
50 # endif
51
52 #include <libcfs/linux/portals_compat25.h>
53
54 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
55 #define schedule_work schedule_task
56 #define prepare_work(wq,cb,cbdata)                                            \
57 do {                                                                          \
58         INIT_TQUEUE((wq), 0, 0);                                              \
59         PREPARE_TQUEUE((wq), (cb), (cbdata));                                 \
60 } while (0)
61
62 #define PageUptodate Page_Uptodate
63 #define our_recalc_sigpending(current) recalc_sigpending(current)
64 #define num_online_cpus() smp_num_cpus
65 static inline void our_cond_resched(void)
66 {
67         if (current->need_resched)
68                schedule ();
69 }
70 #define work_struct_t                   struct tq_struct
71 #define cfs_get_work_data(type,field,data)   (data)
72 #else
73
74 #ifdef HAVE_3ARGS_INIT_WORK
75
76 #define prepare_work(wq,cb,cbdata)                                            \
77 do {                                                                          \
78         INIT_WORK((wq), (void *)(cb), (void *)(cbdata));                      \
79 } while (0)
80
81 #define cfs_get_work_data(type,field,data)   (data)
82
83 #else
84
85 #define prepare_work(wq,cb,cbdata)                                            \
86 do {                                                                          \
87         INIT_WORK((wq), (void *)(cb));                                        \
88 } while (0)
89
90 #define cfs_get_work_data(type,field,data) container_of(data,type,field)
91
92 #endif
93
94 #define wait_on_page wait_on_page_locked
95 #define our_recalc_sigpending(current) recalc_sigpending()
96 #define strtok(a,b) strpbrk(a, b)
97 static inline void our_cond_resched(void)
98 {
99         cond_resched();
100 }
101 #define work_struct_t      struct work_struct
102
103 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) */
104
105 #ifdef CONFIG_SMP
106 #define LASSERT_SPIN_LOCKED(lock) LASSERT(spin_is_locked(lock))
107 #else
108 #define LASSERT_SPIN_LOCKED(lock) do {} while(0)
109 #endif
110 #define LASSERT_SEM_LOCKED(sem) LASSERT(down_trylock(sem) != 0)
111
112 #define LIBCFS_PANIC(msg)            panic(msg)
113
114 /* ------------------------------------------------------------------- */
115
116 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
117
118 #define PORTAL_SYMBOL_REGISTER(x) inter_module_register(#x, THIS_MODULE, &x)
119 #define PORTAL_SYMBOL_UNREGISTER(x) inter_module_unregister(#x)
120
121 #define PORTAL_SYMBOL_GET(x) ((typeof(&x))inter_module_get(#x))
122 #define PORTAL_SYMBOL_PUT(x) inter_module_put(#x)
123
124 #define PORTAL_MODULE_USE       MOD_INC_USE_COUNT
125 #define PORTAL_MODULE_UNUSE     MOD_DEC_USE_COUNT
126 #else
127
128 #define PORTAL_SYMBOL_REGISTER(x)
129 #define PORTAL_SYMBOL_UNREGISTER(x)
130
131 #define PORTAL_SYMBOL_GET(x) symbol_get(x)
132 #define PORTAL_SYMBOL_PUT(x) symbol_put(x)
133
134 #define PORTAL_MODULE_USE       try_module_get(THIS_MODULE)
135 #define PORTAL_MODULE_UNUSE     module_put(THIS_MODULE)
136
137 #endif
138
139 /******************************************************************************/
140 /* Module parameter support */
141 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
142 # define CFS_MODULE_PARM(name, t, type, perm, desc) \
143         MODULE_PARM(name, t);\
144         MODULE_PARM_DESC(name, desc)
145
146 #else
147 # define CFS_MODULE_PARM(name, t, type, perm, desc) \
148         module_param(name, type, perm);\
149         MODULE_PARM_DESC(name, desc)
150 #endif
151
152 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,9))
153 # define CFS_SYSFS_MODULE_PARM  0 /* no sysfs module parameters */
154 #else
155 # define CFS_SYSFS_MODULE_PARM  1 /* module parameters accessible via sysfs */
156 #endif
157 /******************************************************************************/
158
159 #if (__GNUC__)
160 /* Use the special GNU C __attribute__ hack to have the compiler check the
161  * printf style argument string against the actual argument count and
162  * types.
163  */
164 #ifdef printf
165 # warning printf has been defined as a macro...
166 # undef printf
167 #endif
168
169 #endif /* __GNUC__ */
170
171 # define fprintf(a, format, b...) CDEBUG(D_OTHER, format , ## b)
172 # define printf(format, b...) CDEBUG(D_OTHER, format , ## b)
173 # define time(a) CURRENT_TIME
174
175 #ifndef num_possible_cpus
176 #define num_possible_cpus() NR_CPUS
177 #endif
178 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
179 #define i_size_read(a) ((a)->i_size)
180 #endif
181
182 #else  /* !__KERNEL__ */
183 # include <stdio.h>
184 # include <stdlib.h>
185 #if defined(__CYGWIN__)
186 # include <cygwin-ioctl.h>
187 #else
188 # include <stdint.h>
189 #endif
190 # include <unistd.h>
191 # include <time.h>
192 # include <limits.h>
193 # include <errno.h>
194 # include <sys/ioctl.h>                         /* for _IOWR */
195 #ifndef _IOWR
196 #include "ioctl.h"
197 #endif
198
199 # define CFS_MODULE_PARM(name, t, type, perm, desc)
200 #define PORTAL_SYMBOL_GET(x) inter_module_get(#x)
201 #define PORTAL_SYMBOL_PUT(x) inter_module_put(#x)
202
203 #endif /* End of !__KERNEL__ */
204
205 /******************************************************************************/
206 /* Light-weight trace
207  * Support for temporary event tracing with minimal Heisenberg effect. */
208 #define LWT_SUPPORT  0
209
210 #define LWT_MEMORY   (16<<20)
211
212 #ifndef KLWT_SUPPORT
213 # if defined(__KERNEL__)
214 #  if !defined(BITS_PER_LONG)
215 #   error "BITS_PER_LONG not defined"
216 #  endif
217 # elif !defined(__WORDSIZE)
218 #  error "__WORDSIZE not defined"
219 # else
220 #  define BITS_PER_LONG __WORDSIZE
221 # endif
222
223 /* kernel hasn't defined this? */
224 typedef struct {
225         long long   lwte_when;
226         char       *lwte_where;
227         void       *lwte_task;
228         long        lwte_p1;
229         long        lwte_p2;
230         long        lwte_p3;
231         long        lwte_p4;
232 # if BITS_PER_LONG > 32
233         long        lwte_pad;
234 # endif
235 } lwt_event_t;
236 #endif /* !KLWT_SUPPORT */
237
238 #if LWT_SUPPORT
239 # ifdef __KERNEL__
240 #  if !KLWT_SUPPORT
241
242 typedef struct _lwt_page {
243         struct list_head     lwtp_list;
244         struct page         *lwtp_page;
245         lwt_event_t         *lwtp_events;
246 } lwt_page_t;
247
248 typedef struct {
249         int                lwtc_current_index;
250         lwt_page_t        *lwtc_current_page;
251 } lwt_cpu_t;
252
253 extern int       lwt_enabled;
254 extern lwt_cpu_t lwt_cpus[];
255
256 /* Note that we _don't_ define LWT_EVENT at all if LWT_SUPPORT isn't set.
257  * This stuff is meant for finding specific problems; it never stays in
258  * production code... */
259
260 #define LWTSTR(n)       #n
261 #define LWTWHERE(f,l)   f ":" LWTSTR(l)
262 #define LWT_EVENTS_PER_PAGE (CFS_PAGE_SIZE / sizeof (lwt_event_t))
263
264 #define LWT_EVENT(p1, p2, p3, p4)                                       \
265 do {                                                                    \
266         unsigned long    flags;                                         \
267         lwt_cpu_t       *cpu;                                           \
268         lwt_page_t      *p;                                             \
269         lwt_event_t     *e;                                             \
270                                                                         \
271         if (lwt_enabled) {                                              \
272                 local_irq_save (flags);                                 \
273                                                                         \
274                 cpu = &lwt_cpus[smp_processor_id()];                    \
275                 p = cpu->lwtc_current_page;                             \
276                 e = &p->lwtp_events[cpu->lwtc_current_index++];         \
277                                                                         \
278                 if (cpu->lwtc_current_index >= LWT_EVENTS_PER_PAGE) {   \
279                         cpu->lwtc_current_page =                        \
280                                 list_entry (p->lwtp_list.next,          \
281                                             lwt_page_t, lwtp_list);     \
282                         cpu->lwtc_current_index = 0;                    \
283                 }                                                       \
284                                                                         \
285                 e->lwte_when  = get_cycles();                           \
286                 e->lwte_where = LWTWHERE(__FILE__,__LINE__);            \
287                 e->lwte_task  = current;                                \
288                 e->lwte_p1    = (long)(p1);                             \
289                 e->lwte_p2    = (long)(p2);                             \
290                 e->lwte_p3    = (long)(p3);                             \
291                 e->lwte_p4    = (long)(p4);                             \
292                                                                         \
293                 local_irq_restore (flags);                              \
294         }                                                               \
295 } while (0)
296
297 #endif /* !KLWT_SUPPORT */
298
299 extern int  lwt_init (void);
300 extern void lwt_fini (void);
301 extern int  lwt_lookup_string (int *size, char *knlptr,
302                                char *usrptr, int usrsize);
303 extern int  lwt_control (int enable, int clear);
304 extern int  lwt_snapshot (cycles_t *now, int *ncpu, int *total_size,
305                           void *user_ptr, int user_size);
306 # else  /* __KERNEL__ */
307 #  define LWT_EVENT(p1,p2,p3,p4)     /* no userland implementation yet */
308 # endif /* __KERNEL__ */
309 #endif /* LWT_SUPPORT */
310
311 /* ------------------------------------------------------------------ */
312
313 #define IOCTL_LIBCFS_TYPE long
314
315 #ifdef __CYGWIN__
316 # ifndef BITS_PER_LONG
317 #  if (~0UL) == 0xffffffffUL
318 #   define BITS_PER_LONG 32
319 #  else
320 #   define BITS_PER_LONG 64
321 #  endif
322 # endif
323 #endif
324
325 #if BITS_PER_LONG > 32
326 # define LI_POISON ((int)0x5a5a5a5a5a5a5a5a)
327 # define LL_POISON ((long)0x5a5a5a5a5a5a5a5a)
328 # define LP_POISON ((void *)(long)0x5a5a5a5a5a5a5a5a)
329 #else
330 # define LI_POISON ((int)0x5a5a5a5a)
331 # define LL_POISON ((long)0x5a5a5a5a)
332 # define LP_POISON ((void *)(long)0x5a5a5a5a)
333 #endif
334
335 /* this is a bit chunky */
336
337 #if defined(__KERNEL__)
338  #define _LWORDSIZE BITS_PER_LONG
339 #else
340  #define _LWORDSIZE __WORDSIZE
341 #endif
342
343 #if (defined(__x86_64__) && (defined(__KERNEL__) || defined(CRAY_XT3)))
344 /* x86_64 defines __u64 as "long" in userspace, but "long long" in the kernel */
345 # define LPU64 "%Lu"
346 # define LPD64 "%Ld"
347 # define LPX64 "%#Lx"
348 # define LPF64 "L"
349 # define LPSZ  "%lu"
350 # define LPSSZ "%ld"
351 #elif (_LWORDSIZE == 32)
352 # define LPU64 "%Lu"
353 # define LPD64 "%Ld"
354 # define LPX64 "%#Lx"
355 # define LPF64 "L"
356 # define LPSZ  "%u"
357 # define LPSSZ "%d"
358 #elif (_LWORDSIZE == 64)
359 # define LPU64 "%lu"
360 # define LPD64 "%ld"
361 # define LPX64 "%#lx"
362 # define LPF64 "l"
363 # define LPSZ  "%lu"
364 # define LPSSZ "%ld"
365 #endif
366 #ifndef LPU64
367 # error "No word size defined"
368 #endif
369
370 #undef _LWORDSIZE
371
372 #endif