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