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