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