18d59ecb2SHans Petter Selasky /*-
28d59ecb2SHans Petter Selasky  * Copyright (c) 2010 Isilon Systems, Inc.
38d59ecb2SHans Petter Selasky  * Copyright (c) 2010 iX Systems, Inc.
48d59ecb2SHans Petter Selasky  * Copyright (c) 2010 Panasas, Inc.
5fe68f570SHans Petter Selasky  * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
68d59ecb2SHans Petter Selasky  * Copyright (c) 2014-2015 François Tigeot
78d59ecb2SHans Petter Selasky  * All rights reserved.
88d59ecb2SHans Petter Selasky  *
98d59ecb2SHans Petter Selasky  * Redistribution and use in source and binary forms, with or without
108d59ecb2SHans Petter Selasky  * modification, are permitted provided that the following conditions
118d59ecb2SHans Petter Selasky  * are met:
128d59ecb2SHans Petter Selasky  * 1. Redistributions of source code must retain the above copyright
138d59ecb2SHans Petter Selasky  *    notice unmodified, this list of conditions, and the following
148d59ecb2SHans Petter Selasky  *    disclaimer.
158d59ecb2SHans Petter Selasky  * 2. Redistributions in binary form must reproduce the above copyright
168d59ecb2SHans Petter Selasky  *    notice, this list of conditions and the following disclaimer in the
178d59ecb2SHans Petter Selasky  *    documentation and/or other materials provided with the distribution.
188d59ecb2SHans Petter Selasky  *
198d59ecb2SHans Petter Selasky  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
208d59ecb2SHans Petter Selasky  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
218d59ecb2SHans Petter Selasky  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
228d59ecb2SHans Petter Selasky  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
238d59ecb2SHans Petter Selasky  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
248d59ecb2SHans Petter Selasky  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
258d59ecb2SHans Petter Selasky  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
268d59ecb2SHans Petter Selasky  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
278d59ecb2SHans Petter Selasky  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
288d59ecb2SHans Petter Selasky  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
298d59ecb2SHans Petter Selasky  */
30307f78f3SVladimir Kondratyev #ifndef	_LINUXKPI_LINUX_KERNEL_H_
31307f78f3SVladimir Kondratyev #define	_LINUXKPI_LINUX_KERNEL_H_
328d59ecb2SHans Petter Selasky 
338d59ecb2SHans Petter Selasky #include <sys/types.h>
348d59ecb2SHans Petter Selasky #include <sys/systm.h>
358d59ecb2SHans Petter Selasky #include <sys/param.h>
368d59ecb2SHans Petter Selasky #include <sys/libkern.h>
378d59ecb2SHans Petter Selasky #include <sys/stat.h>
388d59ecb2SHans Petter Selasky #include <sys/smp.h>
398d59ecb2SHans Petter Selasky #include <sys/stddef.h>
408d59ecb2SHans Petter Selasky #include <sys/syslog.h>
418f7eee5aSHans Petter Selasky #include <sys/time.h>
428d59ecb2SHans Petter Selasky 
438d59ecb2SHans Petter Selasky #include <linux/bitops.h>
4431ef2909SVladimir Kondratyev #include <linux/build_bug.h>
458d59ecb2SHans Petter Selasky #include <linux/compiler.h>
46fa862482SVladimir Kondratyev #include <linux/container_of.h>
478cfd1dd8SVladimir Kondratyev #include <linux/limits.h>
4843b4c006SBjoern A. Zeeb #include <linux/stringify.h>
498d59ecb2SHans Petter Selasky #include <linux/errno.h>
501e3db1deSHans Petter Selasky #include <linux/sched.h>
518d59ecb2SHans Petter Selasky #include <linux/types.h>
524b2cb13eSEmmanuel Vadot #include <linux/typecheck.h>
538d59ecb2SHans Petter Selasky #include <linux/jiffies.h>
548d59ecb2SHans Petter Selasky #include <linux/log2.h>
554b0552d5SJean-Sébastien Pédron #include <linux/kconfig.h>
5635555d47SHans Petter Selasky 
578d59ecb2SHans Petter Selasky #include <asm/byteorder.h>
584b2cb13eSEmmanuel Vadot #include <asm/cpufeature.h>
594b2cb13eSEmmanuel Vadot #include <asm/processor.h>
6035555d47SHans Petter Selasky #include <asm/uaccess.h>
618d59ecb2SHans Petter Selasky 
62d91cf060SJean-Sébastien Pédron #include <linux/stdarg.h>
63aef2a67bSHans Petter Selasky 
648d59ecb2SHans Petter Selasky #define KERN_CONT       ""
658d59ecb2SHans Petter Selasky #define	KERN_EMERG	"<0>"
668d59ecb2SHans Petter Selasky #define	KERN_ALERT	"<1>"
678d59ecb2SHans Petter Selasky #define	KERN_CRIT	"<2>"
688d59ecb2SHans Petter Selasky #define	KERN_ERR	"<3>"
698d59ecb2SHans Petter Selasky #define	KERN_WARNING	"<4>"
708d59ecb2SHans Petter Selasky #define	KERN_NOTICE	"<5>"
718d59ecb2SHans Petter Selasky #define	KERN_INFO	"<6>"
728d59ecb2SHans Petter Selasky #define	KERN_DEBUG	"<7>"
738d59ecb2SHans Petter Selasky 
74abb14a54SHans Petter Selasky #define	S8_C(x)  x
75abb14a54SHans Petter Selasky #define	U8_C(x)  x ## U
76abb14a54SHans Petter Selasky #define	S16_C(x) x
77abb14a54SHans Petter Selasky #define	U16_C(x) x ## U
78abb14a54SHans Petter Selasky #define	S32_C(x) x
79abb14a54SHans Petter Selasky #define	U32_C(x) x ## U
80abb14a54SHans Petter Selasky #define	S64_C(x) x ## LL
81abb14a54SHans Petter Selasky #define	U64_C(x) x ## ULL
82abb14a54SHans Petter Selasky 
83abb14a54SHans Petter Selasky #define	BUG()			panic("BUG at %s:%d", __FILE__, __LINE__)
84abb14a54SHans Petter Selasky #define	BUG_ON(cond)		do {				\
85abb14a54SHans Petter Selasky 	if (cond) {						\
86abb14a54SHans Petter Selasky 		panic("BUG ON %s failed at %s:%d",		\
87abb14a54SHans Petter Selasky 		    __stringify(cond), __FILE__, __LINE__);	\
88abb14a54SHans Petter Selasky 	}							\
89abb14a54SHans Petter Selasky } while (0)
90abb14a54SHans Petter Selasky 
912390a144SHans Petter Selasky extern int linuxkpi_warn_dump_stack;
92abb14a54SHans Petter Selasky #define	WARN_ON(cond) ({					\
93abb14a54SHans Petter Selasky 	bool __ret = (cond);					\
94abb14a54SHans Petter Selasky 	if (__ret) {						\
95abb14a54SHans Petter Selasky 		printf("WARNING %s failed at %s:%d\n",		\
96abb14a54SHans Petter Selasky 		    __stringify(cond), __FILE__, __LINE__);	\
972390a144SHans Petter Selasky 		if (linuxkpi_warn_dump_stack)				\
98a164a32bSAndriy Gapon 			linux_dump_stack();				\
99abb14a54SHans Petter Selasky 	}								\
100abb14a54SHans Petter Selasky 	unlikely(__ret);						\
101abb14a54SHans Petter Selasky })
102abb14a54SHans Petter Selasky 
103abb14a54SHans Petter Selasky #define	WARN_ON_SMP(cond)	WARN_ON(cond)
104abb14a54SHans Petter Selasky 
105abb14a54SHans Petter Selasky #define	WARN_ON_ONCE(cond) ({					\
106abb14a54SHans Petter Selasky 	static bool __warn_on_once;				\
107abb14a54SHans Petter Selasky 	bool __ret = (cond);					\
108abb14a54SHans Petter Selasky 	if (__ret && !__warn_on_once) {				\
109abb14a54SHans Petter Selasky 		__warn_on_once = 1;				\
110abb14a54SHans Petter Selasky 		printf("WARNING %s failed at %s:%d\n",		\
111abb14a54SHans Petter Selasky 		    __stringify(cond), __FILE__, __LINE__);	\
1122390a144SHans Petter Selasky 		if (linuxkpi_warn_dump_stack)				\
113a164a32bSAndriy Gapon 			linux_dump_stack();				\
114abb14a54SHans Petter Selasky 	}								\
115abb14a54SHans Petter Selasky 	unlikely(__ret);						\
116abb14a54SHans Petter Selasky })
1178d59ecb2SHans Petter Selasky 
118cb564d24SMark Johnston #define	oops_in_progress	SCHEDULER_STOPPED()
119cb564d24SMark Johnston 
1208d59ecb2SHans Petter Selasky #undef	ALIGN
1218d59ecb2SHans Petter Selasky #define	ALIGN(x, y)		roundup2((x), (y))
12297f2e93aSBjoern A. Zeeb #define	ALIGN_DOWN(x, y)	rounddown2(x, y)
1238d59ecb2SHans Petter Selasky #undef PTR_ALIGN
1248d59ecb2SHans Petter Selasky #define	PTR_ALIGN(p, a)		((__typeof(p))ALIGN((uintptr_t)(p), (a)))
125c4e0746eSJohannes Lundberg #define	IS_ALIGNED(x, a)	(((x) & ((__typeof(x))(a) - 1)) == 0)
1268d59ecb2SHans Petter Selasky #define	DIV_ROUND_UP(x, n)	howmany(x, n)
127bf05cd05SHans Petter Selasky #define	__KERNEL_DIV_ROUND_UP(x, n)	howmany(x, n)
1288d59ecb2SHans Petter Selasky #define	DIV_ROUND_UP_ULL(x, n)	DIV_ROUND_UP((unsigned long long)(x), (n))
129c4e0746eSJohannes Lundberg #define	DIV_ROUND_DOWN_ULL(x, n) (((unsigned long long)(x) / (n)) * (n))
1308d59ecb2SHans Petter Selasky #define	FIELD_SIZEOF(t, f)	sizeof(((t *)0)->f)
1318d59ecb2SHans Petter Selasky 
132aef2a67bSHans Petter Selasky #define	printk(...)		printf(__VA_ARGS__)
133aef2a67bSHans Petter Selasky #define	vprintk(f, a)		vprintf(f, a)
134aef2a67bSHans Petter Selasky 
135e0eeeca8SVladimir Kondratyev #define PTR_IF(x, p)		((x) ? (p) : NULL)
136e0eeeca8SVladimir Kondratyev 
1370df8bab6SHans Petter Selasky #define	asm			__asm
1380df8bab6SHans Petter Selasky 
139e35079dbSHans Petter Selasky extern void linux_dump_stack(void);
140e35079dbSHans Petter Selasky #define	dump_stack()		linux_dump_stack()
141e35079dbSHans Petter Selasky 
142aef2a67bSHans Petter Selasky struct va_format {
143aef2a67bSHans Petter Selasky 	const char *fmt;
144aef2a67bSHans Petter Selasky 	va_list *va;
145aef2a67bSHans Petter Selasky };
146aef2a67bSHans Petter Selasky 
147aef2a67bSHans Petter Selasky static inline int
vscnprintf(char * buf,size_t size,const char * fmt,va_list args)148aef2a67bSHans Petter Selasky vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
149aef2a67bSHans Petter Selasky {
150aef2a67bSHans Petter Selasky 	ssize_t ssize = size;
151aef2a67bSHans Petter Selasky 	int i;
152aef2a67bSHans Petter Selasky 
153aef2a67bSHans Petter Selasky 	i = vsnprintf(buf, size, fmt, args);
154aef2a67bSHans Petter Selasky 
155aef2a67bSHans Petter Selasky 	return ((i >= ssize) ? (ssize - 1) : i);
156aef2a67bSHans Petter Selasky }
157aef2a67bSHans Petter Selasky 
158aef2a67bSHans Petter Selasky static inline int
scnprintf(char * buf,size_t size,const char * fmt,...)159aef2a67bSHans Petter Selasky scnprintf(char *buf, size_t size, const char *fmt, ...)
160aef2a67bSHans Petter Selasky {
161aef2a67bSHans Petter Selasky 	va_list args;
162aef2a67bSHans Petter Selasky 	int i;
163aef2a67bSHans Petter Selasky 
164aef2a67bSHans Petter Selasky 	va_start(args, fmt);
165aef2a67bSHans Petter Selasky 	i = vscnprintf(buf, size, fmt, args);
166aef2a67bSHans Petter Selasky 	va_end(args);
167aef2a67bSHans Petter Selasky 
168aef2a67bSHans Petter Selasky 	return (i);
169aef2a67bSHans Petter Selasky }
1708d59ecb2SHans Petter Selasky 
1718d59ecb2SHans Petter Selasky /*
1728d59ecb2SHans Petter Selasky  * The "pr_debug()" and "pr_devel()" macros should produce zero code
1738d59ecb2SHans Petter Selasky  * unless DEBUG is defined:
1748d59ecb2SHans Petter Selasky  */
1758d59ecb2SHans Petter Selasky #ifdef DEBUG
1764580f5eaSHans Petter Selasky extern int linuxkpi_debug;
1778d59ecb2SHans Petter Selasky #define pr_debug(fmt, ...)					\
1784580f5eaSHans Petter Selasky 	do {							\
1794580f5eaSHans Petter Selasky 		if (linuxkpi_debug)				\
1804580f5eaSHans Petter Selasky 			log(LOG_DEBUG, fmt, ##__VA_ARGS__);	\
1814580f5eaSHans Petter Selasky 	} while (0)
1828d59ecb2SHans Petter Selasky #define pr_devel(fmt, ...) \
1838d59ecb2SHans Petter Selasky 	log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__)
1848d59ecb2SHans Petter Selasky #else
1858d59ecb2SHans Petter Selasky #define pr_debug(fmt, ...) \
1868d59ecb2SHans Petter Selasky 	({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; })
1878d59ecb2SHans Petter Selasky #define pr_devel(fmt, ...) \
1888d59ecb2SHans Petter Selasky 	({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; })
1898d59ecb2SHans Petter Selasky #endif
1908d59ecb2SHans Petter Selasky 
1918d59ecb2SHans Petter Selasky #ifndef pr_fmt
1928d59ecb2SHans Petter Selasky #define pr_fmt(fmt) fmt
1938d59ecb2SHans Petter Selasky #endif
1948d59ecb2SHans Petter Selasky 
1958d59ecb2SHans Petter Selasky /*
1968d59ecb2SHans Petter Selasky  * Print a one-time message (analogous to WARN_ONCE() et al):
1978d59ecb2SHans Petter Selasky  */
1988d59ecb2SHans Petter Selasky #define printk_once(...) do {			\
1998d59ecb2SHans Petter Selasky 	static bool __print_once;		\
2008d59ecb2SHans Petter Selasky 						\
2018d59ecb2SHans Petter Selasky 	if (!__print_once) {			\
2028d59ecb2SHans Petter Selasky 		__print_once = true;		\
2038d59ecb2SHans Petter Selasky 		printk(__VA_ARGS__);		\
2048d59ecb2SHans Petter Selasky 	}					\
2058d59ecb2SHans Petter Selasky } while (0)
2068d59ecb2SHans Petter Selasky 
2078d59ecb2SHans Petter Selasky /*
2088d59ecb2SHans Petter Selasky  * Log a one-time message (analogous to WARN_ONCE() et al):
2098d59ecb2SHans Petter Selasky  */
2108d59ecb2SHans Petter Selasky #define log_once(level,...) do {		\
2118d59ecb2SHans Petter Selasky 	static bool __log_once;			\
2128d59ecb2SHans Petter Selasky 						\
213abb14a54SHans Petter Selasky 	if (unlikely(!__log_once)) {		\
2148d59ecb2SHans Petter Selasky 		__log_once = true;		\
2158d59ecb2SHans Petter Selasky 		log(level, __VA_ARGS__);	\
2168d59ecb2SHans Petter Selasky 	}					\
2178d59ecb2SHans Petter Selasky } while (0)
2188d59ecb2SHans Petter Selasky 
2198d59ecb2SHans Petter Selasky #define pr_emerg(fmt, ...) \
2208d59ecb2SHans Petter Selasky 	log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__)
2218d59ecb2SHans Petter Selasky #define pr_alert(fmt, ...) \
2228d59ecb2SHans Petter Selasky 	log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__)
2238d59ecb2SHans Petter Selasky #define pr_crit(fmt, ...) \
2248d59ecb2SHans Petter Selasky 	log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__)
2258d59ecb2SHans Petter Selasky #define pr_err(fmt, ...) \
2268d59ecb2SHans Petter Selasky 	log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
22705c2d94aSGreg V #define pr_err_once(fmt, ...) \
22805c2d94aSGreg V 	log_once(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
2298d59ecb2SHans Petter Selasky #define pr_warning(fmt, ...) \
2308d59ecb2SHans Petter Selasky 	log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
231abb14a54SHans Petter Selasky #define pr_warn(...) \
232abb14a54SHans Petter Selasky 	pr_warning(__VA_ARGS__)
233abb14a54SHans Petter Selasky #define pr_warn_once(fmt, ...) \
234abb14a54SHans Petter Selasky 	log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
2358d59ecb2SHans Petter Selasky #define pr_notice(fmt, ...) \
2368d59ecb2SHans Petter Selasky 	log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__)
2378d59ecb2SHans Petter Selasky #define pr_info(fmt, ...) \
2388d59ecb2SHans Petter Selasky 	log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
2398d59ecb2SHans Petter Selasky #define pr_info_once(fmt, ...) \
2408d59ecb2SHans Petter Selasky 	log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
2418d59ecb2SHans Petter Selasky #define pr_cont(fmt, ...) \
2428d59ecb2SHans Petter Selasky 	printk(KERN_CONT fmt, ##__VA_ARGS__)
243e9db3df2SHans Petter Selasky #define	pr_warn_ratelimited(...) do {		\
2448f7eee5aSHans Petter Selasky 	static linux_ratelimit_t __ratelimited;	\
245e9db3df2SHans Petter Selasky 	if (linux_ratelimited(&__ratelimited))	\
246e9db3df2SHans Petter Selasky 		pr_warning(__VA_ARGS__);	\
247e9db3df2SHans Petter Selasky } while (0)
2488d59ecb2SHans Petter Selasky 
2498d59ecb2SHans Petter Selasky #ifndef WARN
250abb14a54SHans Petter Selasky #define	WARN(condition, ...) ({			\
251abb14a54SHans Petter Selasky 	bool __ret_warn_on = (condition);	\
2528d59ecb2SHans Petter Selasky 	if (unlikely(__ret_warn_on))		\
253abb14a54SHans Petter Selasky 		pr_warning(__VA_ARGS__);	\
254abb14a54SHans Petter Selasky 	unlikely(__ret_warn_on);		\
255abb14a54SHans Petter Selasky })
256abb14a54SHans Petter Selasky #endif
257abb14a54SHans Petter Selasky 
258abb14a54SHans Petter Selasky #ifndef WARN_ONCE
259abb14a54SHans Petter Selasky #define	WARN_ONCE(condition, ...) ({		\
260abb14a54SHans Petter Selasky 	bool __ret_warn_on = (condition);	\
261abb14a54SHans Petter Selasky 	if (unlikely(__ret_warn_on))		\
262abb14a54SHans Petter Selasky 		pr_warn_once(__VA_ARGS__);	\
2638d59ecb2SHans Petter Selasky 	unlikely(__ret_warn_on);		\
2648d59ecb2SHans Petter Selasky })
2658d59ecb2SHans Petter Selasky #endif
2668d59ecb2SHans Petter Selasky 
2678d59ecb2SHans Petter Selasky #define	ARRAY_SIZE(x)	(sizeof(x) / sizeof((x)[0]))
2688d59ecb2SHans Petter Selasky 
269ee7c3198SMark Johnston #define	u64_to_user_ptr(val)	((void *)(uintptr_t)(val))
270ee7c3198SMark Johnston 
271fee0d486SNeel Chauhan #define _RET_IP_		__builtin_return_address(0)
272fee0d486SNeel Chauhan 
273d2f312e0SHans Petter Selasky static inline unsigned long long
simple_strtoull(const char * cp,char ** endp,unsigned int base)274d2f312e0SHans Petter Selasky simple_strtoull(const char *cp, char **endp, unsigned int base)
275d2f312e0SHans Petter Selasky {
276d2f312e0SHans Petter Selasky 	return (strtouq(cp, endp, base));
277d2f312e0SHans Petter Selasky }
278d2f312e0SHans Petter Selasky 
279d2f312e0SHans Petter Selasky static inline long long
simple_strtoll(const char * cp,char ** endp,unsigned int base)280d2f312e0SHans Petter Selasky simple_strtoll(const char *cp, char **endp, unsigned int base)
281d2f312e0SHans Petter Selasky {
282d2f312e0SHans Petter Selasky 	return (strtoq(cp, endp, base));
283d2f312e0SHans Petter Selasky }
284d2f312e0SHans Petter Selasky 
285d2f312e0SHans Petter Selasky static inline unsigned long
simple_strtoul(const char * cp,char ** endp,unsigned int base)286d2f312e0SHans Petter Selasky simple_strtoul(const char *cp, char **endp, unsigned int base)
287d2f312e0SHans Petter Selasky {
288d2f312e0SHans Petter Selasky 	return (strtoul(cp, endp, base));
289d2f312e0SHans Petter Selasky }
290d2f312e0SHans Petter Selasky 
291d2f312e0SHans Petter Selasky static inline long
simple_strtol(const char * cp,char ** endp,unsigned int base)292d2f312e0SHans Petter Selasky simple_strtol(const char *cp, char **endp, unsigned int base)
293d2f312e0SHans Petter Selasky {
294d2f312e0SHans Petter Selasky 	return (strtol(cp, endp, base));
295d2f312e0SHans Petter Selasky }
296d2f312e0SHans Petter Selasky 
297d2f312e0SHans Petter Selasky static inline int
kstrtoul(const char * cp,unsigned int base,unsigned long * res)298d2f312e0SHans Petter Selasky kstrtoul(const char *cp, unsigned int base, unsigned long *res)
299d2f312e0SHans Petter Selasky {
300d2f312e0SHans Petter Selasky 	char *end;
301d2f312e0SHans Petter Selasky 
302d2f312e0SHans Petter Selasky 	*res = strtoul(cp, &end, base);
303d2f312e0SHans Petter Selasky 
304cbfc3c73SHans Petter Selasky 	/* skip newline character, if any */
305cbfc3c73SHans Petter Selasky 	if (*end == '\n')
306cbfc3c73SHans Petter Selasky 		end++;
307d2f312e0SHans Petter Selasky 	if (*cp == 0 || *end != 0)
308d2f312e0SHans Petter Selasky 		return (-EINVAL);
309d2f312e0SHans Petter Selasky 	return (0);
310d2f312e0SHans Petter Selasky }
311d2f312e0SHans Petter Selasky 
312d2f312e0SHans Petter Selasky static inline int
kstrtol(const char * cp,unsigned int base,long * res)313d2f312e0SHans Petter Selasky kstrtol(const char *cp, unsigned int base, long *res)
314d2f312e0SHans Petter Selasky {
315d2f312e0SHans Petter Selasky 	char *end;
316d2f312e0SHans Petter Selasky 
317d2f312e0SHans Petter Selasky 	*res = strtol(cp, &end, base);
318d2f312e0SHans Petter Selasky 
319cbfc3c73SHans Petter Selasky 	/* skip newline character, if any */
320cbfc3c73SHans Petter Selasky 	if (*end == '\n')
321cbfc3c73SHans Petter Selasky 		end++;
322d2f312e0SHans Petter Selasky 	if (*cp == 0 || *end != 0)
323d2f312e0SHans Petter Selasky 		return (-EINVAL);
324d2f312e0SHans Petter Selasky 	return (0);
325d2f312e0SHans Petter Selasky }
326d2f312e0SHans Petter Selasky 
327d2f312e0SHans Petter Selasky static inline int
kstrtoint(const char * cp,unsigned int base,int * res)328d2f312e0SHans Petter Selasky kstrtoint(const char *cp, unsigned int base, int *res)
329d2f312e0SHans Petter Selasky {
330d2f312e0SHans Petter Selasky 	char *end;
331d2f312e0SHans Petter Selasky 	long temp;
332d2f312e0SHans Petter Selasky 
333d2f312e0SHans Petter Selasky 	*res = temp = strtol(cp, &end, base);
334d2f312e0SHans Petter Selasky 
335cbfc3c73SHans Petter Selasky 	/* skip newline character, if any */
336cbfc3c73SHans Petter Selasky 	if (*end == '\n')
337cbfc3c73SHans Petter Selasky 		end++;
338d2f312e0SHans Petter Selasky 	if (*cp == 0 || *end != 0)
339d2f312e0SHans Petter Selasky 		return (-EINVAL);
340d2f312e0SHans Petter Selasky 	if (temp != (int)temp)
341d2f312e0SHans Petter Selasky 		return (-ERANGE);
342d2f312e0SHans Petter Selasky 	return (0);
343d2f312e0SHans Petter Selasky }
344d2f312e0SHans Petter Selasky 
345d2f312e0SHans Petter Selasky static inline int
kstrtouint(const char * cp,unsigned int base,unsigned int * res)346d2f312e0SHans Petter Selasky kstrtouint(const char *cp, unsigned int base, unsigned int *res)
347d2f312e0SHans Petter Selasky {
348d2f312e0SHans Petter Selasky 	char *end;
349d2f312e0SHans Petter Selasky 	unsigned long temp;
350d2f312e0SHans Petter Selasky 
351d2f312e0SHans Petter Selasky 	*res = temp = strtoul(cp, &end, base);
352d2f312e0SHans Petter Selasky 
353cbfc3c73SHans Petter Selasky 	/* skip newline character, if any */
354cbfc3c73SHans Petter Selasky 	if (*end == '\n')
355cbfc3c73SHans Petter Selasky 		end++;
356d2f312e0SHans Petter Selasky 	if (*cp == 0 || *end != 0)
357d2f312e0SHans Petter Selasky 		return (-EINVAL);
358d2f312e0SHans Petter Selasky 	if (temp != (unsigned int)temp)
359d2f312e0SHans Petter Selasky 		return (-ERANGE);
360d2f312e0SHans Petter Selasky 	return (0);
361d2f312e0SHans Petter Selasky }
362d2f312e0SHans Petter Selasky 
363d2f312e0SHans Petter Selasky static inline int
kstrtou8(const char * cp,unsigned int base,u8 * res)364b382b785SBjoern A. Zeeb kstrtou8(const char *cp, unsigned int base, u8 *res)
365b382b785SBjoern A. Zeeb {
366b382b785SBjoern A. Zeeb 	char *end;
367b382b785SBjoern A. Zeeb 	unsigned long temp;
368b382b785SBjoern A. Zeeb 
369b382b785SBjoern A. Zeeb 	*res = temp = strtoul(cp, &end, base);
370b382b785SBjoern A. Zeeb 
371b382b785SBjoern A. Zeeb 	/* skip newline character, if any */
372b382b785SBjoern A. Zeeb 	if (*end == '\n')
373b382b785SBjoern A. Zeeb 		end++;
374b382b785SBjoern A. Zeeb 	if (*cp == 0 || *end != 0)
375b382b785SBjoern A. Zeeb 		return (-EINVAL);
376b382b785SBjoern A. Zeeb 	if (temp != (u8)temp)
377b382b785SBjoern A. Zeeb 		return (-ERANGE);
378b382b785SBjoern A. Zeeb 	return (0);
379b382b785SBjoern A. Zeeb }
380b382b785SBjoern A. Zeeb 
381b382b785SBjoern A. Zeeb static inline int
kstrtou16(const char * cp,unsigned int base,u16 * res)3828e52f22bSEmmanuel Vadot kstrtou16(const char *cp, unsigned int base, u16 *res)
3838e52f22bSEmmanuel Vadot {
3848e52f22bSEmmanuel Vadot 	char *end;
3858e52f22bSEmmanuel Vadot 	unsigned long temp;
3868e52f22bSEmmanuel Vadot 
3878e52f22bSEmmanuel Vadot 	*res = temp = strtoul(cp, &end, base);
3888e52f22bSEmmanuel Vadot 
3898e52f22bSEmmanuel Vadot 	/* skip newline character, if any */
3908e52f22bSEmmanuel Vadot 	if (*end == '\n')
3918e52f22bSEmmanuel Vadot 		end++;
3928e52f22bSEmmanuel Vadot 	if (*cp == 0 || *end != 0)
3938e52f22bSEmmanuel Vadot 		return (-EINVAL);
3948e52f22bSEmmanuel Vadot 	if (temp != (u16)temp)
3958e52f22bSEmmanuel Vadot 		return (-ERANGE);
3968e52f22bSEmmanuel Vadot 	return (0);
3978e52f22bSEmmanuel Vadot }
3988e52f22bSEmmanuel Vadot 
3998e52f22bSEmmanuel Vadot static inline int
kstrtou32(const char * cp,unsigned int base,u32 * res)400d2f312e0SHans Petter Selasky kstrtou32(const char *cp, unsigned int base, u32 *res)
401d2f312e0SHans Petter Selasky {
402d2f312e0SHans Petter Selasky 
403f82cf28bSBjoern A. Zeeb 	return (kstrtouint(cp, base, res));
404d2f312e0SHans Petter Selasky }
4058d59ecb2SHans Petter Selasky 
40635555d47SHans Petter Selasky static inline int
kstrtou64(const char * cp,unsigned int base,u64 * res)40761f7fe6bSHans Petter Selasky kstrtou64(const char *cp, unsigned int base, u64 *res)
40861f7fe6bSHans Petter Selasky {
40961f7fe6bSHans Petter Selasky        char *end;
41061f7fe6bSHans Petter Selasky 
41161f7fe6bSHans Petter Selasky        *res = strtouq(cp, &end, base);
41261f7fe6bSHans Petter Selasky 
41361f7fe6bSHans Petter Selasky        /* skip newline character, if any */
41461f7fe6bSHans Petter Selasky        if (*end == '\n')
41561f7fe6bSHans Petter Selasky                end++;
41661f7fe6bSHans Petter Selasky        if (*cp == 0 || *end != 0)
41761f7fe6bSHans Petter Selasky                return (-EINVAL);
41861f7fe6bSHans Petter Selasky        return (0);
41961f7fe6bSHans Petter Selasky }
42061f7fe6bSHans Petter Selasky 
42161f7fe6bSHans Petter Selasky static inline int
kstrtoull(const char * cp,unsigned int base,unsigned long long * res)422c427456fSVladimir Kondratyev kstrtoull(const char *cp, unsigned int base, unsigned long long *res)
423c427456fSVladimir Kondratyev {
424c427456fSVladimir Kondratyev 	return (kstrtou64(cp, base, (u64 *)res));
425c427456fSVladimir Kondratyev }
426c427456fSVladimir Kondratyev 
427c427456fSVladimir Kondratyev static inline int
kstrtobool(const char * s,bool * res)42835555d47SHans Petter Selasky kstrtobool(const char *s, bool *res)
42935555d47SHans Petter Selasky {
43035555d47SHans Petter Selasky 	int len;
43135555d47SHans Petter Selasky 
43235555d47SHans Petter Selasky 	if (s == NULL || (len = strlen(s)) == 0 || res == NULL)
43335555d47SHans Petter Selasky 		return (-EINVAL);
43435555d47SHans Petter Selasky 
43535555d47SHans Petter Selasky 	/* skip newline character, if any */
43635555d47SHans Petter Selasky 	if (s[len - 1] == '\n')
43735555d47SHans Petter Selasky 		len--;
43835555d47SHans Petter Selasky 
43935555d47SHans Petter Selasky 	if (len == 1 && strchr("yY1", s[0]) != NULL)
44035555d47SHans Petter Selasky 		*res = true;
44135555d47SHans Petter Selasky 	else if (len == 1 && strchr("nN0", s[0]) != NULL)
44235555d47SHans Petter Selasky 		*res = false;
44335555d47SHans Petter Selasky 	else if (strncasecmp("on", s, len) == 0)
44435555d47SHans Petter Selasky 		*res = true;
44535555d47SHans Petter Selasky 	else if (strncasecmp("off", s, len) == 0)
44635555d47SHans Petter Selasky 		*res = false;
44735555d47SHans Petter Selasky 	else
44835555d47SHans Petter Selasky 		return (-EINVAL);
44935555d47SHans Petter Selasky 
45035555d47SHans Petter Selasky 	return (0);
45135555d47SHans Petter Selasky }
45235555d47SHans Petter Selasky 
45335555d47SHans Petter Selasky static inline int
kstrtobool_from_user(const char __user * s,size_t count,bool * res)45435555d47SHans Petter Selasky kstrtobool_from_user(const char __user *s, size_t count, bool *res)
45535555d47SHans Petter Selasky {
45635555d47SHans Petter Selasky 	char buf[8] = {};
45735555d47SHans Petter Selasky 
45835555d47SHans Petter Selasky 	if (count > (sizeof(buf) - 1))
45935555d47SHans Petter Selasky 		count = (sizeof(buf) - 1);
46035555d47SHans Petter Selasky 
46135555d47SHans Petter Selasky 	if (copy_from_user(buf, s, count))
46235555d47SHans Petter Selasky 		return (-EFAULT);
46335555d47SHans Petter Selasky 
46435555d47SHans Petter Selasky 	return (kstrtobool(buf, res));
46535555d47SHans Petter Selasky }
46635555d47SHans Petter Selasky 
467b382b785SBjoern A. Zeeb static inline int
kstrtoint_from_user(const char __user * s,size_t count,unsigned int base,int * p)468c840d5ceSBjoern A. Zeeb kstrtoint_from_user(const char __user *s, size_t count, unsigned int base,
469c840d5ceSBjoern A. Zeeb     int *p)
470c840d5ceSBjoern A. Zeeb {
471c840d5ceSBjoern A. Zeeb 	char buf[36] = {};
472c840d5ceSBjoern A. Zeeb 
473c840d5ceSBjoern A. Zeeb 	if (count > (sizeof(buf) - 1))
474c840d5ceSBjoern A. Zeeb 		count = (sizeof(buf) - 1);
475c840d5ceSBjoern A. Zeeb 
476c840d5ceSBjoern A. Zeeb 	if (copy_from_user(buf, s, count))
477c840d5ceSBjoern A. Zeeb 		return (-EFAULT);
478c840d5ceSBjoern A. Zeeb 
479c840d5ceSBjoern A. Zeeb 	return (kstrtoint(buf, base, p));
480c840d5ceSBjoern A. Zeeb }
481c840d5ceSBjoern A. Zeeb 
482c840d5ceSBjoern A. Zeeb static inline int
kstrtouint_from_user(const char __user * s,size_t count,unsigned int base,unsigned int * p)4838e587a5fSEmmanuel Vadot kstrtouint_from_user(const char __user *s, size_t count, unsigned int base,
484f82cf28bSBjoern A. Zeeb     unsigned int *p)
4858e587a5fSEmmanuel Vadot {
4868e587a5fSEmmanuel Vadot 	char buf[36] = {};
4878e587a5fSEmmanuel Vadot 
4888e587a5fSEmmanuel Vadot 	if (count > (sizeof(buf) - 1))
4898e587a5fSEmmanuel Vadot 		count = (sizeof(buf) - 1);
4908e587a5fSEmmanuel Vadot 
4918e587a5fSEmmanuel Vadot 	if (copy_from_user(buf, s, count))
4928e587a5fSEmmanuel Vadot 		return (-EFAULT);
4938e587a5fSEmmanuel Vadot 
4948e587a5fSEmmanuel Vadot 	return (kstrtouint(buf, base, p));
4958e587a5fSEmmanuel Vadot }
4968e587a5fSEmmanuel Vadot 
4978e587a5fSEmmanuel Vadot static inline int
kstrtou32_from_user(const char __user * s,size_t count,unsigned int base,unsigned int * p)498f82cf28bSBjoern A. Zeeb kstrtou32_from_user(const char __user *s, size_t count, unsigned int base,
499f82cf28bSBjoern A. Zeeb     unsigned int *p)
500f82cf28bSBjoern A. Zeeb {
501f82cf28bSBjoern A. Zeeb 
502f82cf28bSBjoern A. Zeeb 	return (kstrtouint_from_user(s, count, base, p));
503f82cf28bSBjoern A. Zeeb }
504f82cf28bSBjoern A. Zeeb 
505f82cf28bSBjoern A. Zeeb static inline int
kstrtou8_from_user(const char __user * s,size_t count,unsigned int base,u8 * p)506b382b785SBjoern A. Zeeb kstrtou8_from_user(const char __user *s, size_t count, unsigned int base,
507b382b785SBjoern A. Zeeb     u8 *p)
508b382b785SBjoern A. Zeeb {
509b382b785SBjoern A. Zeeb 	char buf[8] = {};
510b382b785SBjoern A. Zeeb 
511b382b785SBjoern A. Zeeb 	if (count > (sizeof(buf) - 1))
512b382b785SBjoern A. Zeeb 		count = (sizeof(buf) - 1);
513b382b785SBjoern A. Zeeb 
514b382b785SBjoern A. Zeeb 	if (copy_from_user(buf, s, count))
515b382b785SBjoern A. Zeeb 		return (-EFAULT);
516b382b785SBjoern A. Zeeb 
517b382b785SBjoern A. Zeeb 	return (kstrtou8(buf, base, p));
518b382b785SBjoern A. Zeeb }
519b382b785SBjoern A. Zeeb 
5208d59ecb2SHans Petter Selasky #define min(x, y)	((x) < (y) ? (x) : (y))
5218d59ecb2SHans Petter Selasky #define max(x, y)	((x) > (y) ? (x) : (y))
5228d59ecb2SHans Petter Selasky 
5238d59ecb2SHans Petter Selasky #define min3(a, b, c)	min(a, min(b,c))
5248d59ecb2SHans Petter Selasky #define max3(a, b, c)	max(a, max(b,c))
5258d59ecb2SHans Petter Selasky 
526abb14a54SHans Petter Selasky #define	min_t(type, x, y) ({			\
527abb14a54SHans Petter Selasky 	type __min1 = (x);			\
528abb14a54SHans Petter Selasky 	type __min2 = (y);			\
529abb14a54SHans Petter Selasky 	__min1 < __min2 ? __min1 : __min2; })
530abb14a54SHans Petter Selasky 
531abb14a54SHans Petter Selasky #define	max_t(type, x, y) ({			\
532abb14a54SHans Petter Selasky 	type __max1 = (x);			\
533abb14a54SHans Petter Selasky 	type __max2 = (y);			\
534abb14a54SHans Petter Selasky 	__max1 > __max2 ? __max1 : __max2; })
5358d59ecb2SHans Petter Selasky 
536355711eaSEmmanuel Vadot #define offsetofend(t, m)	\
537355711eaSEmmanuel Vadot         (offsetof(t, m) + sizeof((((t *)0)->m)))
538355711eaSEmmanuel Vadot 
5398d59ecb2SHans Petter Selasky #define clamp_t(type, _x, min, max)	min_t(type, max_t(type, _x, min), max)
5408d59ecb2SHans Petter Selasky #define clamp(x, lo, hi)		min( max(x,lo), hi)
541abb14a54SHans Petter Selasky #define	clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
5428d59ecb2SHans Petter Selasky 
5438d59ecb2SHans Petter Selasky /*
5448d59ecb2SHans Petter Selasky  * This looks more complex than it should be. But we need to
5458d59ecb2SHans Petter Selasky  * get the type for the ~ right in round_down (it needs to be
5468d59ecb2SHans Petter Selasky  * as wide as the result!), and we want to evaluate the macro
5478d59ecb2SHans Petter Selasky  * arguments just once each.
5488d59ecb2SHans Petter Selasky  */
5498d59ecb2SHans Petter Selasky #define __round_mask(x, y) ((__typeof__(x))((y)-1))
5508d59ecb2SHans Petter Selasky #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
5518d59ecb2SHans Petter Selasky #define round_down(x, y) ((x) & ~__round_mask(x, y))
5528d59ecb2SHans Petter Selasky 
553fe68f570SHans Petter Selasky #define	smp_processor_id()	PCPU_GET(cpuid)
5548d59ecb2SHans Petter Selasky #define	num_possible_cpus()	mp_ncpus
5558d59ecb2SHans Petter Selasky #define	num_online_cpus()	mp_ncpus
55603219fbaSHans Petter Selasky 
55703219fbaSHans Petter Selasky #if defined(__i386__) || defined(__amd64__)
55803219fbaSHans Petter Selasky extern bool linux_cpu_has_clflush;
55903219fbaSHans Petter Selasky #define	cpu_has_clflush		linux_cpu_has_clflush
56003219fbaSHans Petter Selasky #endif
5618d59ecb2SHans Petter Selasky 
5628d59ecb2SHans Petter Selasky /* Swap values of a and b */
5638d59ecb2SHans Petter Selasky #define swap(a, b) do {			\
5648d59ecb2SHans Petter Selasky 	typeof(a) _swap_tmp = a;	\
5658d59ecb2SHans Petter Selasky 	a = b;				\
5668d59ecb2SHans Petter Selasky 	b = _swap_tmp;			\
5678d59ecb2SHans Petter Selasky } while (0)
5688d59ecb2SHans Petter Selasky 
5698d59ecb2SHans Petter Selasky #define	DIV_ROUND_CLOSEST(x, divisor)	(((x) + ((divisor) / 2)) / (divisor))
5708d59ecb2SHans Petter Selasky 
571abb14a54SHans Petter Selasky #define	DIV_ROUND_CLOSEST_ULL(x, divisor) ({		\
572abb14a54SHans Petter Selasky 	__typeof(divisor) __d = (divisor);		\
573abb14a54SHans Petter Selasky 	unsigned long long __ret = (x) + (__d) / 2;	\
574abb14a54SHans Petter Selasky 	__ret /= __d;					\
575abb14a54SHans Petter Selasky 	__ret;						\
576abb14a54SHans Petter Selasky })
577abb14a54SHans Petter Selasky 
5788d59ecb2SHans Petter Selasky static inline uintmax_t
mult_frac(uintmax_t x,uintmax_t multiplier,uintmax_t divisor)5798d59ecb2SHans Petter Selasky mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
5808d59ecb2SHans Petter Selasky {
5818d59ecb2SHans Petter Selasky 	uintmax_t q = (x / divisor);
5828d59ecb2SHans Petter Selasky 	uintmax_t r = (x % divisor);
5838d59ecb2SHans Petter Selasky 
5848d59ecb2SHans Petter Selasky 	return ((q * multiplier) + ((r * multiplier) / divisor));
5858d59ecb2SHans Petter Selasky }
5868d59ecb2SHans Petter Selasky 
5878f7eee5aSHans Petter Selasky typedef struct linux_ratelimit {
5888f7eee5aSHans Petter Selasky 	struct timeval lasttime;
5898f7eee5aSHans Petter Selasky 	int counter;
5908f7eee5aSHans Petter Selasky } linux_ratelimit_t;
5918f7eee5aSHans Petter Selasky 
5928f7eee5aSHans Petter Selasky static inline bool
linux_ratelimited(linux_ratelimit_t * rl)5938f7eee5aSHans Petter Selasky linux_ratelimited(linux_ratelimit_t *rl)
5948f7eee5aSHans Petter Selasky {
5958f7eee5aSHans Petter Selasky 	return (ppsratecheck(&rl->lasttime, &rl->counter, 1));
5968f7eee5aSHans Petter Selasky }
597e9db3df2SHans Petter Selasky 
598d053391cSHans Petter Selasky #define	__is_constexpr(x) \
599d053391cSHans Petter Selasky 	__builtin_constant_p(x)
600d053391cSHans Petter Selasky 
601d326a6c7SHans Petter Selasky /*
602d326a6c7SHans Petter Selasky  * The is_signed() macro below returns true if the passed data type is
603d326a6c7SHans Petter Selasky  * signed. Else false is returned.
604d326a6c7SHans Petter Selasky  */
605d326a6c7SHans Petter Selasky #define	is_signed(datatype) (((datatype)-1 / (datatype)2) == (datatype)0)
606d0eed838SHans Petter Selasky 
6070e4e9e8fSEmmanuel Vadot #define	TAINT_WARN	0
6080e4e9e8fSEmmanuel Vadot #define	test_taint(x)	(0)
609bf27839aSEmmanuel Vadot #define	add_taint(x,y)	do {	\
610bf27839aSEmmanuel Vadot 	} while (0)
6110e4e9e8fSEmmanuel Vadot 
612deb9bfbdSBjoern A. Zeeb static inline int
_h2b(const char c)613deb9bfbdSBjoern A. Zeeb _h2b(const char c)
614deb9bfbdSBjoern A. Zeeb {
615deb9bfbdSBjoern A. Zeeb 
616deb9bfbdSBjoern A. Zeeb 	if (c >= '0' && c <= '9')
617deb9bfbdSBjoern A. Zeeb 		return (c - '0');
618deb9bfbdSBjoern A. Zeeb 	if (c >= 'a' && c <= 'f')
619deb9bfbdSBjoern A. Zeeb 		return (10 + c - 'a');
620deb9bfbdSBjoern A. Zeeb 	if (c >= 'A' && c <= 'F')
621deb9bfbdSBjoern A. Zeeb 		return (10 + c - 'A');
622deb9bfbdSBjoern A. Zeeb 	return (-EINVAL);
623deb9bfbdSBjoern A. Zeeb }
624deb9bfbdSBjoern A. Zeeb 
625deb9bfbdSBjoern A. Zeeb static inline int
hex2bin(uint8_t * bindst,const char * hexsrc,size_t binlen)626deb9bfbdSBjoern A. Zeeb hex2bin(uint8_t *bindst, const char *hexsrc, size_t binlen)
627deb9bfbdSBjoern A. Zeeb {
628deb9bfbdSBjoern A. Zeeb 	int hi4, lo4;
629deb9bfbdSBjoern A. Zeeb 
630deb9bfbdSBjoern A. Zeeb 	while (binlen > 0) {
631deb9bfbdSBjoern A. Zeeb 		hi4 = _h2b(*hexsrc++);
632deb9bfbdSBjoern A. Zeeb 		lo4 = _h2b(*hexsrc++);
633deb9bfbdSBjoern A. Zeeb 		if (hi4 < 0 || lo4 < 0)
634deb9bfbdSBjoern A. Zeeb 			return (-EINVAL);
635deb9bfbdSBjoern A. Zeeb 
636deb9bfbdSBjoern A. Zeeb 		*bindst++ = (hi4 << 4) | lo4;
637deb9bfbdSBjoern A. Zeeb 		binlen--;
638deb9bfbdSBjoern A. Zeeb 	}
639deb9bfbdSBjoern A. Zeeb 
640deb9bfbdSBjoern A. Zeeb 	return (0);
641deb9bfbdSBjoern A. Zeeb }
642deb9bfbdSBjoern A. Zeeb 
643f82cf28bSBjoern A. Zeeb static inline bool
mac_pton(const char * macin,uint8_t * macout)644f82cf28bSBjoern A. Zeeb mac_pton(const char *macin, uint8_t *macout)
645f82cf28bSBjoern A. Zeeb {
646f82cf28bSBjoern A. Zeeb 	const char *s, *d;
647f82cf28bSBjoern A. Zeeb 	uint8_t mac[6], hx, lx;;
648f82cf28bSBjoern A. Zeeb 	int i;
649f82cf28bSBjoern A. Zeeb 
650f82cf28bSBjoern A. Zeeb 	if (strlen(macin) < (3 * 6 - 1))
651f82cf28bSBjoern A. Zeeb 		return (false);
652f82cf28bSBjoern A. Zeeb 
653f82cf28bSBjoern A. Zeeb 	i = 0;
654f82cf28bSBjoern A. Zeeb 	s = macin;
655f82cf28bSBjoern A. Zeeb 	do {
656f82cf28bSBjoern A. Zeeb 		/* Should we also support '-'-delimiters? */
657f82cf28bSBjoern A. Zeeb 		d = strchrnul(s, ':');
658f82cf28bSBjoern A. Zeeb 		hx = lx = 0;
659f82cf28bSBjoern A. Zeeb 		while (s < d) {
660f82cf28bSBjoern A. Zeeb 			/* Fail on abc:123:xxx:... */
661f82cf28bSBjoern A. Zeeb 			if ((d - s) > 2)
662f82cf28bSBjoern A. Zeeb 				return (false);
663f82cf28bSBjoern A. Zeeb 			/* We do support non-well-formed strings: 3:45:6:... */
664f82cf28bSBjoern A. Zeeb 			if ((d - s) > 1) {
665f82cf28bSBjoern A. Zeeb 				hx = _h2b(*s);
666f82cf28bSBjoern A. Zeeb 				if (hx < 0)
667f82cf28bSBjoern A. Zeeb 					return (false);
668f82cf28bSBjoern A. Zeeb 				s++;
669f82cf28bSBjoern A. Zeeb 			}
670f82cf28bSBjoern A. Zeeb 			lx = _h2b(*s);
671f82cf28bSBjoern A. Zeeb 			if (lx < 0)
672f82cf28bSBjoern A. Zeeb 				return (false);
673f82cf28bSBjoern A. Zeeb 			s++;
674f82cf28bSBjoern A. Zeeb 		}
675f82cf28bSBjoern A. Zeeb 		mac[i] = (hx << 4) | lx;
676f82cf28bSBjoern A. Zeeb 		i++;
677f82cf28bSBjoern A. Zeeb 		if (i >= 6)
678f82cf28bSBjoern A. Zeeb 			return (false);
679f82cf28bSBjoern A. Zeeb 	} while (d != NULL && *d != '\0');
680f82cf28bSBjoern A. Zeeb 
681f82cf28bSBjoern A. Zeeb 	memcpy(macout, mac, 6);
682f82cf28bSBjoern A. Zeeb 	return (true);
683f82cf28bSBjoern A. Zeeb }
684f82cf28bSBjoern A. Zeeb 
685c840d5ceSBjoern A. Zeeb #define	DECLARE_FLEX_ARRAY(_t, _n)					\
686fab601c5SBjoern A. Zeeb     struct { struct { } __dummy_ ## _n; _t _n[0]; }
687c840d5ceSBjoern A. Zeeb 
688307f78f3SVladimir Kondratyev #endif	/* _LINUXKPI_LINUX_KERNEL_H_ */
689