Whamcloud - gitweb
Branch b1_6
[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 #ifdef CRAY_XT3
189 # include <ioctl.h>
190 #elif defined(__CYGWIN__)
191 # include <cygwin-ioctl.h>
192 #else
193 # include <stdint.h>
194 #endif
195 # include <unistd.h>
196 # include <time.h>
197 # include <limits.h>
198 # include <errno.h>
199 # include <sys/ioctl.h>                         /* for _IOWR */
200
201 # define CFS_MODULE_PARM(name, t, type, perm, desc)
202 #define PORTAL_SYMBOL_GET(x) inter_module_get(#x)
203 #define PORTAL_SYMBOL_PUT(x) inter_module_put(#x)
204
205 #endif /* End of !__KERNEL__ */
206
207 /******************************************************************************/
208 /* Light-weight trace
209  * Support for temporary event tracing with minimal Heisenberg effect. */
210 #define LWT_SUPPORT  0
211
212 #define LWT_MEMORY   (16<<20)
213
214 #ifndef KLWT_SUPPORT
215 # if defined(__KERNEL__)
216 #  if !defined(BITS_PER_LONG)
217 #   error "BITS_PER_LONG not defined"
218 #  endif
219 # elif !defined(__WORDSIZE)
220 #  error "__WORDSIZE not defined"
221 # else
222 #  define BITS_PER_LONG __WORDSIZE
223 # endif
224
225 /* kernel hasn't defined this? */
226 typedef struct {
227         long long   lwte_when;
228         char       *lwte_where;
229         void       *lwte_task;
230         long        lwte_p1;
231         long        lwte_p2;
232         long        lwte_p3;
233         long        lwte_p4;
234 # if BITS_PER_LONG > 32
235         long        lwte_pad;
236 # endif
237 } lwt_event_t;
238 #endif /* !KLWT_SUPPORT */
239
240 #if LWT_SUPPORT
241 # ifdef __KERNEL__
242 #  if !KLWT_SUPPORT
243
244 typedef struct _lwt_page {
245         struct list_head     lwtp_list;
246         struct page         *lwtp_page;
247         lwt_event_t         *lwtp_events;
248 } lwt_page_t;
249
250 typedef struct {
251         int                lwtc_current_index;
252         lwt_page_t        *lwtc_current_page;
253 } lwt_cpu_t;
254
255 extern int       lwt_enabled;
256 extern lwt_cpu_t lwt_cpus[];
257
258 /* Note that we _don't_ define LWT_EVENT at all if LWT_SUPPORT isn't set.
259  * This stuff is meant for finding specific problems; it never stays in
260  * production code... */
261
262 #define LWTSTR(n)       #n
263 #define LWTWHERE(f,l)   f ":" LWTSTR(l)
264 #define LWT_EVENTS_PER_PAGE (CFS_PAGE_SIZE / sizeof (lwt_event_t))
265
266 #define LWT_EVENT(p1, p2, p3, p4)                                       \
267 do {                                                                    \
268         unsigned long    flags;                                         \
269         lwt_cpu_t       *cpu;                                           \
270         lwt_page_t      *p;                                             \
271         lwt_event_t     *e;                                             \
272                                                                         \
273         if (lwt_enabled) {                                              \
274                 local_irq_save (flags);                                 \
275                                                                         \
276                 cpu = &lwt_cpus[smp_processor_id()];                    \
277                 p = cpu->lwtc_current_page;                             \
278                 e = &p->lwtp_events[cpu->lwtc_current_index++];         \
279                                                                         \
280                 if (cpu->lwtc_current_index >= LWT_EVENTS_PER_PAGE) {   \
281                         cpu->lwtc_current_page =                        \
282                                 list_entry (p->lwtp_list.next,          \
283                                             lwt_page_t, lwtp_list);     \
284                         cpu->lwtc_current_index = 0;                    \
285                 }                                                       \
286                                                                         \
287                 e->lwte_when  = get_cycles();                           \
288                 e->lwte_where = LWTWHERE(__FILE__,__LINE__);            \
289                 e->lwte_task  = current;                                \
290                 e->lwte_p1    = (long)(p1);                             \
291                 e->lwte_p2    = (long)(p2);                             \
292                 e->lwte_p3    = (long)(p3);                             \
293                 e->lwte_p4    = (long)(p4);                             \
294                                                                         \
295                 local_irq_restore (flags);                              \
296         }                                                               \
297 } while (0)
298
299 #endif /* !KLWT_SUPPORT */
300
301 extern int  lwt_init (void);
302 extern void lwt_fini (void);
303 extern int  lwt_lookup_string (int *size, char *knlptr,
304                                char *usrptr, int usrsize);
305 extern int  lwt_control (int enable, int clear);
306 extern int  lwt_snapshot (cycles_t *now, int *ncpu, int *total_size,
307                           void *user_ptr, int user_size);
308 # else  /* __KERNEL__ */
309 #  define LWT_EVENT(p1,p2,p3,p4)     /* no userland implementation yet */
310 # endif /* __KERNEL__ */
311 #endif /* LWT_SUPPORT */
312
313 /* ------------------------------------------------------------------ */
314
315 #define IOCTL_LIBCFS_TYPE long
316
317 #ifdef __CYGWIN__
318 # ifndef BITS_PER_LONG
319 #  if (~0UL) == 0xffffffffUL
320 #   define BITS_PER_LONG 32
321 #  else
322 #   define BITS_PER_LONG 64
323 #  endif
324 # endif
325 #endif
326
327 #if BITS_PER_LONG > 32
328 # define LI_POISON ((int)0x5a5a5a5a5a5a5a5a)
329 # define LL_POISON ((long)0x5a5a5a5a5a5a5a5a)
330 # define LP_POISON ((void *)(long)0x5a5a5a5a5a5a5a5a)
331 #else
332 # define LI_POISON ((int)0x5a5a5a5a)
333 # define LL_POISON ((long)0x5a5a5a5a)
334 # define LP_POISON ((void *)(long)0x5a5a5a5a)
335 #endif
336
337 /* this is a bit chunky */
338
339 #if defined(__KERNEL__)
340  #define _LWORDSIZE BITS_PER_LONG
341 #else
342  #define _LWORDSIZE __WORDSIZE
343 #endif
344
345 #if (defined(__x86_64__) && (defined(__KERNEL__) || defined(CRAY_XT3)))
346 /* x86_64 defines __u64 as "long" in userspace, but "long long" in the kernel */
347 # define LPU64 "%Lu"
348 # define LPD64 "%Ld"
349 # define LPX64 "%#Lx"
350 # define LPF64 "L"
351 # define LPSZ  "%lu"
352 # define LPSSZ "%ld"
353 #elif (_LWORDSIZE == 32)
354 # define LPU64 "%Lu"
355 # define LPD64 "%Ld"
356 # define LPX64 "%#Lx"
357 # define LPF64 "L"
358 # define LPSZ  "%u"
359 # define LPSSZ "%d"
360 #elif (_LWORDSIZE == 64)
361 # define LPU64 "%lu"
362 # define LPD64 "%ld"
363 # define LPX64 "%#lx"
364 # define LPF64 "l"
365 # define LPSZ  "%lu"
366 # define LPSSZ "%ld"
367 #endif
368 #ifndef LPU64
369 # error "No word size defined"
370 #endif
371
372 #undef _LWORDSIZE
373
374 #endif