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