[TIPC] Cleaned up info/warn/err macros
[pandora-kernel.git] / net / tipc / core.h
1 /*
2  * net/tipc/core.h: Include file for TIPC global declarations
3  * 
4  * Copyright (c) 2003-2005, Ericsson Research Canada
5  * Copyright (c) 2005, Wind River Systems
6  * Copyright (c) 2005-2006, Ericsson AB
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the names of the copyright holders nor the names of its
18  *    contributors may be used to endorse or promote products derived from
19  *    this software without specific prior written permission.
20  *
21  * Alternatively, this software may be distributed under the terms of the
22  * GNU General Public License ("GPL") version 2 as published by the Free
23  * Software Foundation.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37
38 #ifndef _TIPC_CORE_H
39 #define _TIPC_CORE_H
40
41 #include <net/tipc/tipc.h>
42 #include <linux/types.h>
43 #include <linux/kernel.h>
44 #include <linux/errno.h>
45 #include <linux/mm.h>
46 #include <linux/timer.h>
47 #include <linux/string.h>
48 #include <asm/uaccess.h>
49 #include <linux/interrupt.h>
50 #include <asm/atomic.h>
51 #include <asm/hardirq.h>
52 #include <linux/netdevice.h>
53 #include <linux/in.h>   
54 #include <linux/list.h>
55 #include <linux/vmalloc.h>
56
57 /*
58  * TIPC debugging code
59  */
60
61 #define assert(i)  BUG_ON(!(i))
62
63 struct tipc_msg;
64 extern struct print_buf *CONS, *LOG;
65 extern struct print_buf *TEE(struct print_buf *, struct print_buf *);
66 void msg_print(struct print_buf*,struct tipc_msg *,const char*);
67 void tipc_printf(struct print_buf *, const char *fmt, ...);
68 void tipc_dump(struct print_buf*,const char *fmt, ...);
69
70 #ifdef CONFIG_TIPC_DEBUG
71
72 /*
73  * TIPC debug support included:
74  * - system messages are printed to TIPC_OUTPUT print buffer
75  * - debug messages are printed to DBG_OUTPUT print buffer
76  */
77
78 #define err(fmt, arg...)  tipc_printf(TIPC_OUTPUT, KERN_ERR "TIPC: " fmt, ## arg)
79 #define warn(fmt, arg...) tipc_printf(TIPC_OUTPUT, KERN_WARNING "TIPC: " fmt, ## arg)
80 #define info(fmt, arg...) tipc_printf(TIPC_OUTPUT, KERN_NOTICE "TIPC: " fmt, ## arg)
81
82 #define dbg(fmt, arg...)  do {if (DBG_OUTPUT) tipc_printf(DBG_OUTPUT, fmt, ## arg);} while(0)
83 #define msg_dbg(msg, txt) do {if (DBG_OUTPUT) msg_print(DBG_OUTPUT, msg, txt);} while(0)
84 #define dump(fmt, arg...) do {if (DBG_OUTPUT) tipc_dump(DBG_OUTPUT, fmt, ##arg);} while(0)
85
86
87 /*      
88  * By default, TIPC_OUTPUT is defined to be system console and TIPC log buffer,
89  * while DBG_OUTPUT is the null print buffer.  These defaults can be changed
90  * here, or on a per .c file basis, by redefining these symbols.  The following
91  * print buffer options are available:
92  *
93  * NULL                 : Output to null print buffer (i.e. print nowhere)
94  * CONS                 : Output to system console
95  * LOG                  : Output to TIPC log buffer 
96  * &buf                 : Output to user-defined buffer (struct print_buf *)
97  * TEE(&buf_a,&buf_b)   : Output to two print buffers (eg. TEE(CONS,LOG) )
98  */
99
100 #ifndef TIPC_OUTPUT
101 #define TIPC_OUTPUT TEE(CONS,LOG)
102 #endif
103
104 #ifndef DBG_OUTPUT
105 #define DBG_OUTPUT NULL
106 #endif
107
108 #else
109
110 #ifndef DBG_OUTPUT
111 #define DBG_OUTPUT NULL
112 #endif
113
114 /*
115  * TIPC debug support not included:
116  * - system messages are printed to system console
117  * - debug messages are not printed
118  */
119
120 #define err(fmt, arg...)  printk(KERN_ERR "TIPC: " fmt , ## arg)
121 #define info(fmt, arg...) printk(KERN_INFO "TIPC: " fmt , ## arg)
122 #define warn(fmt, arg...) printk(KERN_WARNING "TIPC: " fmt , ## arg)
123
124 #define dbg(fmt, arg...) do {} while (0)
125 #define msg_dbg(msg,txt) do {} while (0)
126 #define dump(fmt,arg...) do {} while (0)
127
128 #endif                    
129
130
131 /* 
132  * TIPC-specific error codes
133  */
134
135 #define ELINKCONG EAGAIN        /* link congestion <=> resource unavailable */
136
137 /*
138  * Global configuration variables
139  */
140
141 extern u32 tipc_own_addr;
142 extern int tipc_max_zones;
143 extern int tipc_max_clusters;
144 extern int tipc_max_nodes;
145 extern int tipc_max_slaves;
146 extern int tipc_max_ports;
147 extern int tipc_max_subscriptions;
148 extern int tipc_max_publications;
149 extern int tipc_net_id;
150 extern int tipc_remote_management;
151
152 /*
153  * Other global variables
154  */
155
156 extern int tipc_mode;
157 extern int tipc_random;
158 extern const char tipc_alphabet[];
159 extern atomic_t tipc_user_count;
160
161
162 /*
163  * Routines available to privileged subsystems
164  */
165
166 extern int  start_core(void);
167 extern void stop_core(void);
168 extern int  start_net(void);
169 extern void stop_net(void);
170
171 static inline int delimit(int val, int min, int max)
172 {
173         if (val > max)
174                 return max;
175         if (val < min)
176                 return min;
177         return val;
178 }
179
180
181 /*
182  * TIPC timer and signal code
183  */
184
185 typedef void (*Handler) (unsigned long);
186
187 u32 k_signal(Handler routine, unsigned long argument);
188
189 /**
190  * k_init_timer - initialize a timer
191  * @timer: pointer to timer structure
192  * @routine: pointer to routine to invoke when timer expires
193  * @argument: value to pass to routine when timer expires
194  * 
195  * Timer must be initialized before use (and terminated when no longer needed).
196  */
197
198 static inline void k_init_timer(struct timer_list *timer, Handler routine, 
199                                 unsigned long argument)
200 {
201         dbg("initializing timer %p\n", timer);
202         init_timer(timer);
203         timer->function = routine;
204         timer->data = argument;
205 }
206
207 /**
208  * k_start_timer - start a timer
209  * @timer: pointer to timer structure
210  * @msec: time to delay (in ms)
211  * 
212  * Schedules a previously initialized timer for later execution.
213  * If timer is already running, the new timeout overrides the previous request.
214  * 
215  * To ensure the timer doesn't expire before the specified delay elapses,
216  * the amount of delay is rounded up when converting to the jiffies
217  * then an additional jiffy is added to account for the fact that 
218  * the starting time may be in the middle of the current jiffy.
219  */
220
221 static inline void k_start_timer(struct timer_list *timer, unsigned long msec)
222 {
223         dbg("starting timer %p for %u\n", timer, msec);
224         mod_timer(timer, jiffies + msecs_to_jiffies(msec) + 1);
225 }
226
227 /**
228  * k_cancel_timer - cancel a timer
229  * @timer: pointer to timer structure
230  * 
231  * Cancels a previously initialized timer.  
232  * Can be called safely even if the timer is already inactive.
233  * 
234  * WARNING: Must not be called when holding locks required by the timer's
235  *          timeout routine, otherwise deadlock can occur on SMP systems!
236  */
237
238 static inline void k_cancel_timer(struct timer_list *timer)
239 {
240         dbg("cancelling timer %p\n", timer);
241         del_timer_sync(timer);
242 }
243
244 /**
245  * k_term_timer - terminate a timer
246  * @timer: pointer to timer structure
247  * 
248  * Prevents further use of a previously initialized timer.
249  * 
250  * WARNING: Caller must ensure timer isn't currently running.
251  * 
252  * (Do not "enhance" this routine to automatically cancel an active timer,
253  * otherwise deadlock can arise when a timeout routine calls k_term_timer.)
254  */
255
256 static inline void k_term_timer(struct timer_list *timer)
257 {
258         dbg("terminating timer %p\n", timer);
259 }
260
261
262 /*
263  * TIPC message buffer code
264  *
265  * TIPC message buffer headroom leaves room for 14 byte Ethernet header, 
266  * while ensuring TIPC header is word aligned for quicker access
267  */
268
269 #define BUF_HEADROOM 16u 
270
271 struct tipc_skb_cb {
272         void *handle;
273 };
274
275 #define TIPC_SKB_CB(__skb) ((struct tipc_skb_cb *)&((__skb)->cb[0]))
276
277
278 static inline struct tipc_msg *buf_msg(struct sk_buff *skb)
279 {
280         return (struct tipc_msg *)skb->data;
281 }
282
283 /**
284  * buf_acquire - creates a TIPC message buffer
285  * @size: message size (including TIPC header)
286  *
287  * Returns a new buffer.  Space is reserved for a data link header.
288  */
289
290 static inline struct sk_buff *buf_acquire(u32 size)
291 {
292         struct sk_buff *skb;
293         unsigned int buf_size = (BUF_HEADROOM + size + 3) & ~3u;
294
295         skb = alloc_skb(buf_size, GFP_ATOMIC);
296         if (skb) {
297                 skb_reserve(skb, BUF_HEADROOM);
298                 skb_put(skb, size);
299                 skb->next = NULL;
300         }
301         return skb;
302 }
303
304 /**
305  * buf_discard - frees a TIPC message buffer
306  * @skb: message buffer
307  *
308  * Frees a new buffer.  If passed NULL, just returns.
309  */
310
311 static inline void buf_discard(struct sk_buff *skb)
312 {
313         if (likely(skb != NULL))
314                 kfree_skb(skb);
315 }
316
317 #endif