kernel.h revision 331799
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-2016 Mellanox Technologies, Ltd.
6 * Copyright (c) 2014-2015 Fran��ois Tigeot
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
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice unmodified, this list of conditions, and the following
14 *    disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/kernel.h 331799 2018-03-30 18:51:36Z hselasky $
31 */
32#ifndef	_LINUX_KERNEL_H_
33#define	_LINUX_KERNEL_H_
34
35#include <sys/cdefs.h>
36#include <sys/types.h>
37#include <sys/systm.h>
38#include <sys/param.h>
39#include <sys/libkern.h>
40#include <sys/stat.h>
41#include <sys/smp.h>
42#include <sys/stddef.h>
43#include <sys/syslog.h>
44#include <sys/time.h>
45
46#include <linux/bitops.h>
47#include <linux/compiler.h>
48#include <linux/errno.h>
49#include <linux/sched.h>
50#include <linux/types.h>
51#include <linux/jiffies.h>
52#include <linux/log2.h>
53#include <asm/byteorder.h>
54
55#include <machine/stdarg.h>
56
57#define KERN_CONT       ""
58#define	KERN_EMERG	"<0>"
59#define	KERN_ALERT	"<1>"
60#define	KERN_CRIT	"<2>"
61#define	KERN_ERR	"<3>"
62#define	KERN_WARNING	"<4>"
63#define	KERN_NOTICE	"<5>"
64#define	KERN_INFO	"<6>"
65#define	KERN_DEBUG	"<7>"
66
67#define	U8_MAX		((u8)~0U)
68#define	S8_MAX		((s8)(U8_MAX >> 1))
69#define	S8_MIN		((s8)(-S8_MAX - 1))
70#define	U16_MAX		((u16)~0U)
71#define	S16_MAX		((s16)(U16_MAX >> 1))
72#define	S16_MIN		((s16)(-S16_MAX - 1))
73#define	U32_MAX		((u32)~0U)
74#define	S32_MAX		((s32)(U32_MAX >> 1))
75#define	S32_MIN		((s32)(-S32_MAX - 1))
76#define	U64_MAX		((u64)~0ULL)
77#define	S64_MAX		((s64)(U64_MAX >> 1))
78#define	S64_MIN		((s64)(-S64_MAX - 1))
79
80#define	S8_C(x)  x
81#define	U8_C(x)  x ## U
82#define	S16_C(x) x
83#define	U16_C(x) x ## U
84#define	S32_C(x) x
85#define	U32_C(x) x ## U
86#define	S64_C(x) x ## LL
87#define	U64_C(x) x ## ULL
88
89#define	BUILD_BUG()			do { CTASSERT(0); } while (0)
90#define	BUILD_BUG_ON(x)			CTASSERT(!(x))
91#define	BUILD_BUG_ON_MSG(x, msg)	BUILD_BUG_ON(x)
92#define	BUILD_BUG_ON_NOT_POWER_OF_2(x)	BUILD_BUG_ON(!powerof2(x))
93#define	BUILD_BUG_ON_INVALID(expr)	while (0) { (void)(expr); }
94
95#define	BUG()			panic("BUG at %s:%d", __FILE__, __LINE__)
96#define	BUG_ON(cond)		do {				\
97	if (cond) {						\
98		panic("BUG ON %s failed at %s:%d",		\
99		    __stringify(cond), __FILE__, __LINE__);	\
100	}							\
101} while (0)
102
103#define	WARN_ON(cond) ({					\
104      bool __ret = (cond);					\
105      if (__ret) {						\
106		printf("WARNING %s failed at %s:%d\n",		\
107		    __stringify(cond), __FILE__, __LINE__);	\
108      }								\
109      unlikely(__ret);						\
110})
111
112#define	WARN_ON_SMP(cond)	WARN_ON(cond)
113
114#define	WARN_ON_ONCE(cond) ({					\
115      static bool __warn_on_once;				\
116      bool __ret = (cond);					\
117      if (__ret && !__warn_on_once) {				\
118		__warn_on_once = 1;				\
119		printf("WARNING %s failed at %s:%d\n",		\
120		    __stringify(cond), __FILE__, __LINE__);	\
121      }								\
122      unlikely(__ret);						\
123})
124
125#define	oops_in_progress	SCHEDULER_STOPPED()
126
127#undef	ALIGN
128#define	ALIGN(x, y)		roundup2((x), (y))
129#undef PTR_ALIGN
130#define	PTR_ALIGN(p, a)		((__typeof(p))ALIGN((uintptr_t)(p), (a)))
131#define	DIV_ROUND_UP(x, n)	howmany(x, n)
132#define	DIV_ROUND_UP_ULL(x, n)	DIV_ROUND_UP((unsigned long long)(x), (n))
133#define	FIELD_SIZEOF(t, f)	sizeof(((t *)0)->f)
134
135#define	printk(...)		printf(__VA_ARGS__)
136#define	vprintk(f, a)		vprintf(f, a)
137
138struct va_format {
139	const char *fmt;
140	va_list *va;
141};
142
143static inline int
144vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
145{
146	ssize_t ssize = size;
147	int i;
148
149	i = vsnprintf(buf, size, fmt, args);
150
151	return ((i >= ssize) ? (ssize - 1) : i);
152}
153
154static inline int
155scnprintf(char *buf, size_t size, const char *fmt, ...)
156{
157	va_list args;
158	int i;
159
160	va_start(args, fmt);
161	i = vscnprintf(buf, size, fmt, args);
162	va_end(args);
163
164	return (i);
165}
166
167/*
168 * The "pr_debug()" and "pr_devel()" macros should produce zero code
169 * unless DEBUG is defined:
170 */
171#ifdef DEBUG
172#define pr_debug(fmt, ...) \
173	log(LOG_DEBUG, fmt, ##__VA_ARGS__)
174#define pr_devel(fmt, ...) \
175	log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__)
176#else
177#define pr_debug(fmt, ...) \
178	({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; })
179#define pr_devel(fmt, ...) \
180	({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; })
181#endif
182
183#ifndef pr_fmt
184#define pr_fmt(fmt) fmt
185#endif
186
187/*
188 * Print a one-time message (analogous to WARN_ONCE() et al):
189 */
190#define printk_once(...) do {			\
191	static bool __print_once;		\
192						\
193	if (!__print_once) {			\
194		__print_once = true;		\
195		printk(__VA_ARGS__);		\
196	}					\
197} while (0)
198
199/*
200 * Log a one-time message (analogous to WARN_ONCE() et al):
201 */
202#define log_once(level,...) do {		\
203	static bool __log_once;			\
204						\
205	if (unlikely(!__log_once)) {		\
206		__log_once = true;		\
207		log(level, __VA_ARGS__);	\
208	}					\
209} while (0)
210
211#define pr_emerg(fmt, ...) \
212	log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__)
213#define pr_alert(fmt, ...) \
214	log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__)
215#define pr_crit(fmt, ...) \
216	log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__)
217#define pr_err(fmt, ...) \
218	log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
219#define pr_warning(fmt, ...) \
220	log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
221#define pr_warn(...) \
222	pr_warning(__VA_ARGS__)
223#define pr_warn_once(fmt, ...) \
224	log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
225#define pr_notice(fmt, ...) \
226	log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__)
227#define pr_info(fmt, ...) \
228	log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
229#define pr_info_once(fmt, ...) \
230	log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
231#define pr_cont(fmt, ...) \
232	printk(KERN_CONT fmt, ##__VA_ARGS__)
233#define	pr_warn_ratelimited(...) do {		\
234	static linux_ratelimit_t __ratelimited;	\
235	if (linux_ratelimited(&__ratelimited))	\
236		pr_warning(__VA_ARGS__);	\
237} while (0)
238
239#ifndef WARN
240#define	WARN(condition, ...) ({			\
241	bool __ret_warn_on = (condition);	\
242	if (unlikely(__ret_warn_on))		\
243		pr_warning(__VA_ARGS__);	\
244	unlikely(__ret_warn_on);		\
245})
246#endif
247
248#ifndef WARN_ONCE
249#define	WARN_ONCE(condition, ...) ({		\
250	bool __ret_warn_on = (condition);	\
251	if (unlikely(__ret_warn_on))		\
252		pr_warn_once(__VA_ARGS__);	\
253	unlikely(__ret_warn_on);		\
254})
255#endif
256
257#define container_of(ptr, type, member)				\
258({								\
259	const __typeof(((type *)0)->member) *__p = (ptr);	\
260	(type *)((uintptr_t)__p - offsetof(type, member));	\
261})
262
263#define	ARRAY_SIZE(x)	(sizeof(x) / sizeof((x)[0]))
264
265#define	u64_to_user_ptr(val)	((void *)(uintptr_t)(val))
266
267static inline unsigned long long
268simple_strtoull(const char *cp, char **endp, unsigned int base)
269{
270	return (strtouq(cp, endp, base));
271}
272
273static inline long long
274simple_strtoll(const char *cp, char **endp, unsigned int base)
275{
276	return (strtoq(cp, endp, base));
277}
278
279static inline unsigned long
280simple_strtoul(const char *cp, char **endp, unsigned int base)
281{
282	return (strtoul(cp, endp, base));
283}
284
285static inline long
286simple_strtol(const char *cp, char **endp, unsigned int base)
287{
288	return (strtol(cp, endp, base));
289}
290
291static inline int
292kstrtoul(const char *cp, unsigned int base, unsigned long *res)
293{
294	char *end;
295
296	*res = strtoul(cp, &end, base);
297
298	/* skip newline character, if any */
299	if (*end == '\n')
300		end++;
301	if (*cp == 0 || *end != 0)
302		return (-EINVAL);
303	return (0);
304}
305
306static inline int
307kstrtol(const char *cp, unsigned int base, long *res)
308{
309	char *end;
310
311	*res = strtol(cp, &end, base);
312
313	/* skip newline character, if any */
314	if (*end == '\n')
315		end++;
316	if (*cp == 0 || *end != 0)
317		return (-EINVAL);
318	return (0);
319}
320
321static inline int
322kstrtoint(const char *cp, unsigned int base, int *res)
323{
324	char *end;
325	long temp;
326
327	*res = temp = strtol(cp, &end, base);
328
329	/* skip newline character, if any */
330	if (*end == '\n')
331		end++;
332	if (*cp == 0 || *end != 0)
333		return (-EINVAL);
334	if (temp != (int)temp)
335		return (-ERANGE);
336	return (0);
337}
338
339static inline int
340kstrtouint(const char *cp, unsigned int base, unsigned int *res)
341{
342	char *end;
343	unsigned long temp;
344
345	*res = temp = strtoul(cp, &end, base);
346
347	/* skip newline character, if any */
348	if (*end == '\n')
349		end++;
350	if (*cp == 0 || *end != 0)
351		return (-EINVAL);
352	if (temp != (unsigned int)temp)
353		return (-ERANGE);
354	return (0);
355}
356
357static inline int
358kstrtou32(const char *cp, unsigned int base, u32 *res)
359{
360	char *end;
361	unsigned long temp;
362
363	*res = temp = strtoul(cp, &end, base);
364
365	/* skip newline character, if any */
366	if (*end == '\n')
367		end++;
368	if (*cp == 0 || *end != 0)
369		return (-EINVAL);
370	if (temp != (u32)temp)
371		return (-ERANGE);
372	return (0);
373}
374
375#define min(x, y)	((x) < (y) ? (x) : (y))
376#define max(x, y)	((x) > (y) ? (x) : (y))
377
378#define min3(a, b, c)	min(a, min(b,c))
379#define max3(a, b, c)	max(a, max(b,c))
380
381#define	min_t(type, x, y) ({			\
382	type __min1 = (x);			\
383	type __min2 = (y);			\
384	__min1 < __min2 ? __min1 : __min2; })
385
386#define	max_t(type, x, y) ({			\
387	type __max1 = (x);			\
388	type __max2 = (y);			\
389	__max1 > __max2 ? __max1 : __max2; })
390
391#define clamp_t(type, _x, min, max)	min_t(type, max_t(type, _x, min), max)
392#define clamp(x, lo, hi)		min( max(x,lo), hi)
393#define	clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
394
395/*
396 * This looks more complex than it should be. But we need to
397 * get the type for the ~ right in round_down (it needs to be
398 * as wide as the result!), and we want to evaluate the macro
399 * arguments just once each.
400 */
401#define __round_mask(x, y) ((__typeof__(x))((y)-1))
402#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
403#define round_down(x, y) ((x) & ~__round_mask(x, y))
404
405#define	smp_processor_id()	PCPU_GET(cpuid)
406#define	num_possible_cpus()	mp_ncpus
407#define	num_online_cpus()	mp_ncpus
408
409#if defined(__i386__) || defined(__amd64__)
410extern bool linux_cpu_has_clflush;
411#define	cpu_has_clflush		linux_cpu_has_clflush
412#endif
413
414typedef struct pm_message {
415	int event;
416} pm_message_t;
417
418/* Swap values of a and b */
419#define swap(a, b) do {			\
420	typeof(a) _swap_tmp = a;	\
421	a = b;				\
422	b = _swap_tmp;			\
423} while (0)
424
425#define	DIV_ROUND_CLOSEST(x, divisor)	(((x) + ((divisor) / 2)) / (divisor))
426
427#define	DIV_ROUND_CLOSEST_ULL(x, divisor) ({		\
428	__typeof(divisor) __d = (divisor);		\
429	unsigned long long __ret = (x) + (__d) / 2;	\
430	__ret /= __d;					\
431	__ret;						\
432})
433
434static inline uintmax_t
435mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
436{
437	uintmax_t q = (x / divisor);
438	uintmax_t r = (x % divisor);
439
440	return ((q * multiplier) + ((r * multiplier) / divisor));
441}
442
443static inline int64_t
444abs64(int64_t x)
445{
446	return (x < 0 ? -x : x);
447}
448
449typedef struct linux_ratelimit {
450	struct timeval lasttime;
451	int counter;
452} linux_ratelimit_t;
453
454static inline bool
455linux_ratelimited(linux_ratelimit_t *rl)
456{
457	return (ppsratecheck(&rl->lasttime, &rl->counter, 1));
458}
459
460#endif	/* _LINUX_KERNEL_H_ */
461