1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    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  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #ifndef	_LINUX_KERNEL_H_
30 #define	_LINUX_KERNEL_H_
31 
32 #include <sys/cdefs.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/param.h>
36 #include <sys/libkern.h>
37 #include <sys/stat.h>
38 #include <sys/smp.h>
39 #include <sys/stddef.h>
40 #include <sys/syslog.h>
41 
42 #include <linux/bitops.h>
43 #include <linux/compiler.h>
44 #include <linux/errno.h>
45 #include <linux/kthread.h>
46 #include <linux/types.h>
47 #include <linux/jiffies.h>
48 #include <linux/wait.h>
49 #include <linux/log2.h>
50 #include <asm/byteorder.h>
51 
52 #define KERN_CONT       ""
53 #define	KERN_EMERG	"<0>"
54 #define	KERN_ALERT	"<1>"
55 #define	KERN_CRIT	"<2>"
56 #define	KERN_ERR	"<3>"
57 #define	KERN_WARNING	"<4>"
58 #define	KERN_NOTICE	"<5>"
59 #define	KERN_INFO	"<6>"
60 #define	KERN_DEBUG	"<7>"
61 
62 #define	BUILD_BUG_ON(x)		CTASSERT(!(x))
63 
64 #define BUG()			panic("BUG")
65 #define BUG_ON(condition)	do { if (condition) BUG(); } while(0)
66 #define	WARN_ON(cond) ({					\
67       bool __ret = (cond);					\
68       if (__ret) {						\
69 		printf("WARNING %s failed at %s:%d\n",		\
70 		    __stringify(cond), __FILE__, __LINE__);	\
71       }								\
72       unlikely(__ret);						\
73 })
74 
75 #undef	ALIGN
76 #define	ALIGN(x, y)		roundup2((x), (y))
77 #undef PTR_ALIGN
78 #define	PTR_ALIGN(p, a)		((__typeof(p))ALIGN((uintptr_t)(p), (a)))
79 #define	DIV_ROUND_UP		howmany
80 #define	FIELD_SIZEOF(t, f)	sizeof(((t *)0)->f)
81 
82 #define	printk(X...)		printf(X)
83 
84 /*
85  * The "pr_debug()" and "pr_devel()" macros should produce zero code
86  * unless DEBUG is defined:
87  */
88 #ifdef DEBUG
89 #define pr_debug(fmt, ...) \
90         log(LOG_DEBUG, fmt, ##__VA_ARGS__)
91 #define pr_devel(fmt, ...) \
92 	log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__)
93 #else
94 #define pr_debug(fmt, ...) \
95         ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; })
96 #define pr_devel(fmt, ...) \
97 	({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; })
98 #endif
99 
100 #define udelay(t)       	DELAY(t)
101 #define usleep_range(min,max)	DELAY(min)
102 
103 #ifndef pr_fmt
104 #define pr_fmt(fmt) fmt
105 #endif
106 
107 /*
108  * Print a one-time message (analogous to WARN_ONCE() et al):
109  */
110 #define printk_once(...) do {			\
111 	static bool __print_once;		\
112 						\
113 	if (!__print_once) {			\
114 		__print_once = true;		\
115 		printk(__VA_ARGS__);		\
116 	}					\
117 } while (0)
118 
119 /*
120  * Log a one-time message (analogous to WARN_ONCE() et al):
121  */
122 #define log_once(level,...) do {		\
123 	static bool __log_once;			\
124 						\
125 	if (!__log_once) {			\
126 		__log_once = true;		\
127 		log(level, __VA_ARGS__);	\
128 	}					\
129 } while (0)
130 
131 #define pr_emerg(fmt, ...) \
132 	log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__)
133 #define pr_alert(fmt, ...) \
134 	log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__)
135 #define pr_crit(fmt, ...) \
136 	log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__)
137 #define pr_err(fmt, ...) \
138 	log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
139 #define pr_warning(fmt, ...) \
140 	log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
141 #define pr_warn pr_warning
142 #define pr_notice(fmt, ...) \
143 	log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__)
144 #define pr_info(fmt, ...) \
145 	log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
146 #define pr_info_once(fmt, ...) \
147 	log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
148 #define pr_cont(fmt, ...) \
149 	printk(KERN_CONT fmt, ##__VA_ARGS__)
150 
151 #ifndef WARN
152 #define WARN(condition, format...) ({                                   \
153         int __ret_warn_on = !!(condition);                              \
154         if (unlikely(__ret_warn_on))                                    \
155                 pr_warning(format);                                     \
156         unlikely(__ret_warn_on);                                        \
157 })
158 #endif
159 
160 #define container_of(ptr, type, member)				\
161 ({								\
162 	__typeof(((type *)0)->member) *_p = (ptr);		\
163 	(type *)((char *)_p - offsetof(type, member));		\
164 })
165 
166 #define	ARRAY_SIZE(x)	(sizeof(x) / sizeof((x)[0]))
167 
168 #define	simple_strtoul	strtoul
169 #define	simple_strtol	strtol
170 #define kstrtol(a,b,c) ({*(c) = strtol(a,0,b);})
171 
172 #define min(x, y)	((x) < (y) ? (x) : (y))
173 #define max(x, y)	((x) > (y) ? (x) : (y))
174 #define min_t(type, _x, _y)	((type)(_x) < (type)(_y) ? (type)(_x) : (type)(_y))
175 #define max_t(type, _x, _y)	((type)(_x) > (type)(_y) ? (type)(_x) : (type)(_y))
176 
177 /*
178  * This looks more complex than it should be. But we need to
179  * get the type for the ~ right in round_down (it needs to be
180  * as wide as the result!), and we want to evaluate the macro
181  * arguments just once each.
182  */
183 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
184 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
185 #define round_down(x, y) ((x) & ~__round_mask(x, y))
186 
187 #define	num_possible_cpus()	mp_ncpus
188 #define	num_online_cpus()	mp_ncpus
189 
190 typedef struct pm_message {
191         int event;
192 } pm_message_t;
193 
194 #endif	/* _LINUX_KERNEL_H_ */
195