Whamcloud - gitweb
Land b_release_1_4_3 onto HEAD (20050619_0305)
[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_catastrophe = 1;                                        \
97         portals_debug_dumplog();                                        \
98         portals_run_lbug_upcall(file, func, line);                      \
99         panic("LBUG");                                                  \
100 } while (0)
101 #else
102 #define LBUG_WITH_LOC(file, func, line)                                 \
103 do {                                                                    \
104         CEMERG("LBUG\n");                                               \
105         portals_catastrophe = 1;                                        \
106         portals_debug_dumpstack(NULL);                                  \
107         portals_debug_dumplog();                                        \
108         portals_run_lbug_upcall(file, func, line);                      \
109         set_task_state(current, TASK_UNINTERRUPTIBLE);                  \
110         schedule();                                                     \
111 } while (0)
112 #endif /* __arch_um__ */
113
114 /* ------------------------------------------------------------------- */
115
116 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
117
118 #define PORTAL_SYMBOL_REGISTER(x) inter_module_register(#x, THIS_MODULE, &x)
119 #define PORTAL_SYMBOL_UNREGISTER(x) inter_module_unregister(#x)
120
121 #define PORTAL_SYMBOL_GET(x) ((typeof(&x))inter_module_get(#x))
122 #define PORTAL_SYMBOL_PUT(x) inter_module_put(#x)
123
124 #define PORTAL_MODULE_USE       MOD_INC_USE_COUNT
125 #define PORTAL_MODULE_UNUSE     MOD_DEC_USE_COUNT
126 #else
127
128 #define PORTAL_SYMBOL_REGISTER(x)
129 #define PORTAL_SYMBOL_UNREGISTER(x)
130
131 #define PORTAL_SYMBOL_GET(x) symbol_get(x)
132 #define PORTAL_SYMBOL_PUT(x) symbol_put(x)
133
134 #define PORTAL_MODULE_USE       try_module_get(THIS_MODULE)
135 #define PORTAL_MODULE_UNUSE     module_put(THIS_MODULE)
136
137 #endif
138
139 /******************************************************************************/
140
141 #if (__GNUC__)
142 /* Use the special GNU C __attribute__ hack to have the compiler check the
143  * printf style argument string against the actual argument count and
144  * types.
145  */
146 #ifdef printf
147 # warning printf has been defined as a macro...
148 # undef printf
149 #endif
150
151 #endif /* __GNUC__ */
152
153 # define fprintf(a, format, b...) CDEBUG(D_OTHER, format , ## b)
154 # define printf(format, b...) CDEBUG(D_OTHER, format , ## b)
155 # define time(a) CURRENT_TIME
156
157 #else  /* !__KERNEL__ */
158 # include <stdio.h>
159 # include <stdlib.h>
160 #ifndef __CYGWIN__
161 # include <stdint.h>
162 #else
163 # include <cygwin-ioctl.h>
164 #endif
165 # include <unistd.h>
166 # include <time.h>
167 # include <limits.h>
168
169 #endif /* End of !__KERNEL__ */
170
171 /******************************************************************************/
172 /* Light-weight trace
173  * Support for temporary event tracing with minimal Heisenberg effect. */
174 #define LWT_SUPPORT  0
175
176 #define LWT_MEMORY   (16<<20)
177
178 #if !KLWT_SUPPORT
179 # if defined(__KERNEL__)
180 #  if !defined(BITS_PER_LONG)
181 #   error "BITS_PER_LONG not defined"
182 #  endif
183 # elif !defined(__WORDSIZE)
184 #  error "__WORDSIZE not defined"
185 # else
186 #  define BITS_PER_LONG __WORDSIZE
187 # endif
188
189 /* kernel hasn't defined this? */
190 typedef struct {
191         long long   lwte_when;
192         char       *lwte_where;
193         void       *lwte_task;
194         long        lwte_p1;
195         long        lwte_p2;
196         long        lwte_p3;
197         long        lwte_p4;
198 # if BITS_PER_LONG > 32
199         long        lwte_pad;
200 # endif
201 } lwt_event_t;
202 #endif /* !KLWT_SUPPORT */
203
204 #if LWT_SUPPORT
205 # ifdef __KERNEL__
206 #  if !KLWT_SUPPORT
207
208 typedef struct _lwt_page {
209         struct list_head     lwtp_list;
210         struct page         *lwtp_page;
211         lwt_event_t         *lwtp_events;
212 } lwt_page_t;
213
214 typedef struct {
215         int                lwtc_current_index;
216         lwt_page_t        *lwtc_current_page;
217 } lwt_cpu_t;
218
219 extern int       lwt_enabled;
220 extern lwt_cpu_t lwt_cpus[];
221
222 /* Note that we _don't_ define LWT_EVENT at all if LWT_SUPPORT isn't set.
223  * This stuff is meant for finding specific problems; it never stays in
224  * production code... */
225
226 #define LWTSTR(n)       #n
227 #define LWTWHERE(f,l)   f ":" LWTSTR(l)
228 #define LWT_EVENTS_PER_PAGE (PAGE_SIZE / sizeof (lwt_event_t))
229
230 #define LWT_EVENT(p1, p2, p3, p4)                                       \
231 do {                                                                    \
232         unsigned long    flags;                                         \
233         lwt_cpu_t       *cpu;                                           \
234         lwt_page_t      *p;                                             \
235         lwt_event_t     *e;                                             \
236                                                                         \
237         if (lwt_enabled) {                                              \
238                 local_irq_save (flags);                                 \
239                                                                         \
240                 cpu = &lwt_cpus[smp_processor_id()];                    \
241                 p = cpu->lwtc_current_page;                             \
242                 e = &p->lwtp_events[cpu->lwtc_current_index++];         \
243                                                                         \
244                 if (cpu->lwtc_current_index >= LWT_EVENTS_PER_PAGE) {   \
245                         cpu->lwtc_current_page =                        \
246                                 list_entry (p->lwtp_list.next,          \
247                                             lwt_page_t, lwtp_list);     \
248                         cpu->lwtc_current_index = 0;                    \
249                 }                                                       \
250                                                                         \
251                 e->lwte_when  = get_cycles();                           \
252                 e->lwte_where = LWTWHERE(__FILE__,__LINE__);            \
253                 e->lwte_task  = current;                                \
254                 e->lwte_p1    = (long)(p1);                             \
255                 e->lwte_p2    = (long)(p2);                             \
256                 e->lwte_p3    = (long)(p3);                             \
257                 e->lwte_p4    = (long)(p4);                             \
258                                                                         \
259                 local_irq_restore (flags);                              \
260         }                                                               \
261 } while (0)
262
263 #endif /* !KLWT_SUPPORT */
264
265 extern int  lwt_init (void);
266 extern void lwt_fini (void);
267 extern int  lwt_lookup_string (int *size, char *knlptr,
268                                char *usrptr, int usrsize);
269 extern int  lwt_control (int enable, int clear);
270 extern int  lwt_snapshot (cycles_t *now, int *ncpu, int *total_size,
271                           void *user_ptr, int user_size);
272 # else  /* __KERNEL__ */
273 #  define LWT_EVENT(p1,p2,p3,p4)     /* no userland implementation yet */
274 # endif /* __KERNEL__ */
275 #endif /* LWT_SUPPORT */
276
277 /* ------------------------------------------------------------------ */
278
279 #define IOCTL_PORTAL_TYPE long
280
281 #ifdef __CYGWIN__
282 # ifndef BITS_PER_LONG
283 #  if (~0UL) == 0xffffffffUL
284 #   define BITS_PER_LONG 32
285 #  else
286 #   define BITS_PER_LONG 64
287 #  endif
288 # endif
289 #endif
290
291 #if BITS_PER_LONG > 32
292 # define LI_POISON ((int)0x5a5a5a5a5a5a5a5a)
293 # define LL_POISON ((long)0x5a5a5a5a5a5a5a5a)
294 # define LP_POISON ((void *)(long)0x5a5a5a5a5a5a5a5a)
295 #else
296 # define LI_POISON ((int)0x5a5a5a5a)
297 # define LL_POISON ((long)0x5a5a5a5a)
298 # define LP_POISON ((void *)(long)0x5a5a5a5a)
299 #endif
300
301 #if defined(__x86_64__) && defined(__KERNEL__)
302 /* x86_64 defines __u64 as "long" in userspace, but "long long" in the kernel */
303 # define LPU64 "%Lu"
304 # define LPD64 "%Ld"
305 # define LPX64 "%#Lx"
306 # define LPSZ  "%lu"
307 # define LPSSZ "%ld"
308 #elif (BITS_PER_LONG == 32 || __WORDSIZE == 32)
309 # define LPU64 "%Lu"
310 # define LPD64 "%Ld"
311 # define LPX64 "%#Lx"
312 # define LPSZ  "%u"
313 # define LPSSZ "%d"
314 #elif (BITS_PER_LONG == 64 || __WORDSIZE == 64)
315 # define LPU64 "%lu"
316 # define LPD64 "%ld"
317 # define LPX64 "%#lx"
318 # define LPSZ  "%lu"
319 # define LPSSZ "%ld"
320 #endif
321 #ifndef LPU64
322 # error "No word size defined"
323 #endif
324
325 #endif