kernel.h revision 329980
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 329980 2018-02-25 10:51:39Z 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_ON(x)			CTASSERT(!(x))
90#define	BUILD_BUG_ON_MSG(x, msg)	BUILD_BUG_ON(x)
91#define	BUILD_BUG_ON_NOT_POWER_OF_2(x)	BUILD_BUG_ON(!powerof2(x))
92#define	BUILD_BUG_ON_INVALID(expr)	while (0) { (void)(expr); }
93
94#define	BUG()			panic("BUG at %s:%d", __FILE__, __LINE__)
95#define	BUG_ON(cond)		do {				\
96	if (cond) {						\
97		panic("BUG ON %s failed at %s:%d",		\
98		    __stringify(cond), __FILE__, __LINE__);	\
99	}							\
100} while (0)
101
102#define	WARN_ON(cond) ({					\
103      bool __ret = (cond);					\
104      if (__ret) {						\
105		printf("WARNING %s failed at %s:%d\n",		\
106		    __stringify(cond), __FILE__, __LINE__);	\
107      }								\
108      unlikely(__ret);						\
109})
110
111#define	WARN_ON_SMP(cond)	WARN_ON(cond)
112
113#define	WARN_ON_ONCE(cond) ({					\
114      static bool __warn_on_once;				\
115      bool __ret = (cond);					\
116      if (__ret && !__warn_on_once) {				\
117		__warn_on_once = 1;				\
118		printf("WARNING %s failed at %s:%d\n",		\
119		    __stringify(cond), __FILE__, __LINE__);	\
120      }								\
121      unlikely(__ret);						\
122})
123
124#define	oops_in_progress	SCHEDULER_STOPPED()
125
126#undef	ALIGN
127#define	ALIGN(x, y)		roundup2((x), (y))
128#undef PTR_ALIGN
129#define	PTR_ALIGN(p, a)		((__typeof(p))ALIGN((uintptr_t)(p), (a)))
130#define	DIV_ROUND_UP(x, n)	howmany(x, n)
131#define	DIV_ROUND_UP_ULL(x, n)	DIV_ROUND_UP((unsigned long long)(x), (n))
132#define	FIELD_SIZEOF(t, f)	sizeof(((t *)0)->f)
133
134#define	printk(...)		printf(__VA_ARGS__)
135#define	vprintk(f, a)		vprintf(f, a)
136
137struct va_format {
138	const char *fmt;
139	va_list *va;
140};
141
142static inline int
143vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
144{
145	ssize_t ssize = size;
146	int i;
147
148	i = vsnprintf(buf, size, fmt, args);
149
150	return ((i >= ssize) ? (ssize - 1) : i);
151}
152
153static inline int
154scnprintf(char *buf, size_t size, const char *fmt, ...)
155{
156	va_list args;
157	int i;
158
159	va_start(args, fmt);
160	i = vscnprintf(buf, size, fmt, args);
161	va_end(args);
162
163	return (i);
164}
165
166/*
167 * The "pr_debug()" and "pr_devel()" macros should produce zero code
168 * unless DEBUG is defined:
169 */
170#ifdef DEBUG
171#define pr_debug(fmt, ...) \
172        log(LOG_DEBUG, fmt, ##__VA_ARGS__)
173#define pr_devel(fmt, ...) \
174	log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__)
175#else
176#define pr_debug(fmt, ...) \
177        ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; })
178#define pr_devel(fmt, ...) \
179	({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; })
180#endif
181
182#ifndef pr_fmt
183#define pr_fmt(fmt) fmt
184#endif
185
186/*
187 * Print a one-time message (analogous to WARN_ONCE() et al):
188 */
189#define printk_once(...) do {			\
190	static bool __print_once;		\
191						\
192	if (!__print_once) {			\
193		__print_once = true;		\
194		printk(__VA_ARGS__);		\
195	}					\
196} while (0)
197
198/*
199 * Log a one-time message (analogous to WARN_ONCE() et al):
200 */
201#define log_once(level,...) do {		\
202	static bool __log_once;			\
203						\
204	if (unlikely(!__log_once)) {		\
205		__log_once = true;		\
206		log(level, __VA_ARGS__);	\
207	}					\
208} while (0)
209
210#define pr_emerg(fmt, ...) \
211	log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__)
212#define pr_alert(fmt, ...) \
213	log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__)
214#define pr_crit(fmt, ...) \
215	log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__)
216#define pr_err(fmt, ...) \
217	log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
218#define pr_warning(fmt, ...) \
219	log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
220#define pr_warn(...) \
221	pr_warning(__VA_ARGS__)
222#define pr_warn_once(fmt, ...) \
223	log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
224#define pr_notice(fmt, ...) \
225	log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__)
226#define pr_info(fmt, ...) \
227	log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
228#define pr_info_once(fmt, ...) \
229	log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
230#define pr_cont(fmt, ...) \
231	printk(KERN_CONT fmt, ##__VA_ARGS__)
232#define	pr_warn_ratelimited(...) do {		\
233	static linux_ratelimit_t __ratelimited;	\
234	if (linux_ratelimited(&__ratelimited))	\
235		pr_warning(__VA_ARGS__);	\
236} while (0)
237
238#ifndef WARN
239#define	WARN(condition, ...) ({			\
240        bool __ret_warn_on = (condition);	\
241        if (unlikely(__ret_warn_on))		\
242                pr_warning(__VA_ARGS__);	\
243        unlikely(__ret_warn_on);		\
244})
245#endif
246
247#ifndef WARN_ONCE
248#define	WARN_ONCE(condition, ...) ({		\
249        bool __ret_warn_on = (condition);	\
250        if (unlikely(__ret_warn_on))		\
251                pr_warn_once(__VA_ARGS__);	\
252        unlikely(__ret_warn_on);		\
253})
254#endif
255
256#define container_of(ptr, type, member)				\
257({								\
258	const __typeof(((type *)0)->member) *__p = (ptr);	\
259	(type *)((uintptr_t)__p - offsetof(type, member));	\
260})
261
262#define	ARRAY_SIZE(x)	(sizeof(x) / sizeof((x)[0]))
263
264#define	u64_to_user_ptr(val)	((void *)(uintptr_t)(val))
265
266static inline unsigned long long
267simple_strtoull(const char *cp, char **endp, unsigned int base)
268{
269	return (strtouq(cp, endp, base));
270}
271
272static inline long long
273simple_strtoll(const char *cp, char **endp, unsigned int base)
274{
275	return (strtoq(cp, endp, base));
276}
277
278static inline unsigned long
279simple_strtoul(const char *cp, char **endp, unsigned int base)
280{
281	return (strtoul(cp, endp, base));
282}
283
284static inline long
285simple_strtol(const char *cp, char **endp, unsigned int base)
286{
287	return (strtol(cp, endp, base));
288}
289
290static inline int
291kstrtoul(const char *cp, unsigned int base, unsigned long *res)
292{
293	char *end;
294
295	*res = strtoul(cp, &end, base);
296
297	if (*cp == 0 || *end != 0)
298		return (-EINVAL);
299	return (0);
300}
301
302static inline int
303kstrtol(const char *cp, unsigned int base, long *res)
304{
305	char *end;
306
307	*res = strtol(cp, &end, base);
308
309	if (*cp == 0 || *end != 0)
310		return (-EINVAL);
311	return (0);
312}
313
314static inline int
315kstrtoint(const char *cp, unsigned int base, int *res)
316{
317	char *end;
318	long temp;
319
320	*res = temp = strtol(cp, &end, base);
321
322	if (*cp == 0 || *end != 0)
323		return (-EINVAL);
324	if (temp != (int)temp)
325		return (-ERANGE);
326	return (0);
327}
328
329static inline int
330kstrtouint(const char *cp, unsigned int base, unsigned int *res)
331{
332	char *end;
333	unsigned long temp;
334
335	*res = temp = strtoul(cp, &end, base);
336
337	if (*cp == 0 || *end != 0)
338		return (-EINVAL);
339	if (temp != (unsigned int)temp)
340		return (-ERANGE);
341	return (0);
342}
343
344static inline int
345kstrtou32(const char *cp, unsigned int base, u32 *res)
346{
347	char *end;
348	unsigned long temp;
349
350	*res = temp = strtoul(cp, &end, base);
351
352	if (*cp == 0 || *end != 0)
353		return (-EINVAL);
354	if (temp != (u32)temp)
355		return (-ERANGE);
356	return (0);
357}
358
359#define min(x, y)	((x) < (y) ? (x) : (y))
360#define max(x, y)	((x) > (y) ? (x) : (y))
361
362#define min3(a, b, c)	min(a, min(b,c))
363#define max3(a, b, c)	max(a, max(b,c))
364
365#define	min_t(type, x, y) ({			\
366	type __min1 = (x);			\
367	type __min2 = (y);			\
368	__min1 < __min2 ? __min1 : __min2; })
369
370#define	max_t(type, x, y) ({			\
371	type __max1 = (x);			\
372	type __max2 = (y);			\
373	__max1 > __max2 ? __max1 : __max2; })
374
375#define clamp_t(type, _x, min, max)	min_t(type, max_t(type, _x, min), max)
376#define clamp(x, lo, hi)		min( max(x,lo), hi)
377#define	clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
378
379/*
380 * This looks more complex than it should be. But we need to
381 * get the type for the ~ right in round_down (it needs to be
382 * as wide as the result!), and we want to evaluate the macro
383 * arguments just once each.
384 */
385#define __round_mask(x, y) ((__typeof__(x))((y)-1))
386#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
387#define round_down(x, y) ((x) & ~__round_mask(x, y))
388
389#define	smp_processor_id()	PCPU_GET(cpuid)
390#define	num_possible_cpus()	mp_ncpus
391#define	num_online_cpus()	mp_ncpus
392
393#if defined(__i386__) || defined(__amd64__)
394extern bool linux_cpu_has_clflush;
395#define	cpu_has_clflush		linux_cpu_has_clflush
396#endif
397
398typedef struct pm_message {
399        int event;
400} pm_message_t;
401
402/* Swap values of a and b */
403#define swap(a, b) do {			\
404	typeof(a) _swap_tmp = a;	\
405	a = b;				\
406	b = _swap_tmp;			\
407} while (0)
408
409#define	DIV_ROUND_CLOSEST(x, divisor)	(((x) + ((divisor) / 2)) / (divisor))
410
411#define	DIV_ROUND_CLOSEST_ULL(x, divisor) ({		\
412	__typeof(divisor) __d = (divisor);		\
413	unsigned long long __ret = (x) + (__d) / 2;	\
414	__ret /= __d;					\
415	__ret;						\
416})
417
418static inline uintmax_t
419mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
420{
421	uintmax_t q = (x / divisor);
422	uintmax_t r = (x % divisor);
423
424	return ((q * multiplier) + ((r * multiplier) / divisor));
425}
426
427static inline int64_t
428abs64(int64_t x)
429{
430	return (x < 0 ? -x : x);
431}
432
433typedef struct linux_ratelimit {
434	struct timeval lasttime;
435	int counter;
436} linux_ratelimit_t;
437
438static inline bool
439linux_ratelimited(linux_ratelimit_t *rl)
440{
441	return (ppsratecheck(&rl->lasttime, &rl->counter, 1));
442}
443
444#endif	/* _LINUX_KERNEL_H_ */
445