xref: /freebsd/sys/compat/linuxkpi/common/include/linux/kernel.h (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
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 #ifndef	_LINUXKPI_LINUX_KERNEL_H_
31 #define	_LINUXKPI_LINUX_KERNEL_H_
32 
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 #include <sys/time.h>
42 
43 #include <linux/bitops.h>
44 #include <linux/build_bug.h>
45 #include <linux/compiler.h>
46 #include <linux/container_of.h>
47 #include <linux/stringify.h>
48 #include <linux/errno.h>
49 #include <linux/sched.h>
50 #include <linux/types.h>
51 #include <linux/typecheck.h>
52 #include <linux/jiffies.h>
53 #include <linux/log2.h>
54 #include <linux/kconfig.h>
55 
56 #include <asm/byteorder.h>
57 #include <asm/cpufeature.h>
58 #include <asm/processor.h>
59 #include <asm/uaccess.h>
60 
61 #include <linux/stdarg.h>
62 
63 #define KERN_CONT       ""
64 #define	KERN_EMERG	"<0>"
65 #define	KERN_ALERT	"<1>"
66 #define	KERN_CRIT	"<2>"
67 #define	KERN_ERR	"<3>"
68 #define	KERN_WARNING	"<4>"
69 #define	KERN_NOTICE	"<5>"
70 #define	KERN_INFO	"<6>"
71 #define	KERN_DEBUG	"<7>"
72 
73 #define	U8_MAX		((u8)~0U)
74 #define	S8_MAX		((s8)(U8_MAX >> 1))
75 #define	S8_MIN		((s8)(-S8_MAX - 1))
76 #define	U16_MAX		((u16)~0U)
77 #define	S16_MAX		((s16)(U16_MAX >> 1))
78 #define	S16_MIN		((s16)(-S16_MAX - 1))
79 #define	U32_MAX		((u32)~0U)
80 #define	S32_MAX		((s32)(U32_MAX >> 1))
81 #define	S32_MIN		((s32)(-S32_MAX - 1))
82 #define	U64_MAX		((u64)~0ULL)
83 #define	S64_MAX		((s64)(U64_MAX >> 1))
84 #define	S64_MIN		((s64)(-S64_MAX - 1))
85 
86 #define	S8_C(x)  x
87 #define	U8_C(x)  x ## U
88 #define	S16_C(x) x
89 #define	U16_C(x) x ## U
90 #define	S32_C(x) x
91 #define	U32_C(x) x ## U
92 #define	S64_C(x) x ## LL
93 #define	U64_C(x) x ## ULL
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 extern int linuxkpi_warn_dump_stack;
104 #define	WARN_ON(cond) ({					\
105 	bool __ret = (cond);					\
106 	if (__ret) {						\
107 		printf("WARNING %s failed at %s:%d\n",		\
108 		    __stringify(cond), __FILE__, __LINE__);	\
109 		if (linuxkpi_warn_dump_stack)				\
110 			linux_dump_stack();				\
111 	}								\
112 	unlikely(__ret);						\
113 })
114 
115 #define	WARN_ON_SMP(cond)	WARN_ON(cond)
116 
117 #define	WARN_ON_ONCE(cond) ({					\
118 	static bool __warn_on_once;				\
119 	bool __ret = (cond);					\
120 	if (__ret && !__warn_on_once) {				\
121 		__warn_on_once = 1;				\
122 		printf("WARNING %s failed at %s:%d\n",		\
123 		    __stringify(cond), __FILE__, __LINE__);	\
124 		if (linuxkpi_warn_dump_stack)				\
125 			linux_dump_stack();				\
126 	}								\
127 	unlikely(__ret);						\
128 })
129 
130 #define	oops_in_progress	SCHEDULER_STOPPED()
131 
132 #undef	ALIGN
133 #define	ALIGN(x, y)		roundup2((x), (y))
134 #define	ALIGN_DOWN(x, y)	rounddown2(x, y)
135 #undef PTR_ALIGN
136 #define	PTR_ALIGN(p, a)		((__typeof(p))ALIGN((uintptr_t)(p), (a)))
137 #define	IS_ALIGNED(x, a)	(((x) & ((__typeof(x))(a) - 1)) == 0)
138 #define	DIV_ROUND_UP(x, n)	howmany(x, n)
139 #define	__KERNEL_DIV_ROUND_UP(x, n)	howmany(x, n)
140 #define	DIV_ROUND_UP_ULL(x, n)	DIV_ROUND_UP((unsigned long long)(x), (n))
141 #define	DIV_ROUND_DOWN_ULL(x, n) (((unsigned long long)(x) / (n)) * (n))
142 #define	FIELD_SIZEOF(t, f)	sizeof(((t *)0)->f)
143 
144 #define	printk(...)		printf(__VA_ARGS__)
145 #define	vprintk(f, a)		vprintf(f, a)
146 
147 #define	asm			__asm
148 
149 extern void linux_dump_stack(void);
150 #define	dump_stack()		linux_dump_stack()
151 
152 struct va_format {
153 	const char *fmt;
154 	va_list *va;
155 };
156 
157 static inline int
158 vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
159 {
160 	ssize_t ssize = size;
161 	int i;
162 
163 	i = vsnprintf(buf, size, fmt, args);
164 
165 	return ((i >= ssize) ? (ssize - 1) : i);
166 }
167 
168 static inline int
169 scnprintf(char *buf, size_t size, const char *fmt, ...)
170 {
171 	va_list args;
172 	int i;
173 
174 	va_start(args, fmt);
175 	i = vscnprintf(buf, size, fmt, args);
176 	va_end(args);
177 
178 	return (i);
179 }
180 
181 /*
182  * The "pr_debug()" and "pr_devel()" macros should produce zero code
183  * unless DEBUG is defined:
184  */
185 #ifdef DEBUG
186 extern int linuxkpi_debug;
187 #define pr_debug(fmt, ...)					\
188 	do {							\
189 		if (linuxkpi_debug)				\
190 			log(LOG_DEBUG, fmt, ##__VA_ARGS__);	\
191 	} while (0)
192 #define pr_devel(fmt, ...) \
193 	log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__)
194 #else
195 #define pr_debug(fmt, ...) \
196 	({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; })
197 #define pr_devel(fmt, ...) \
198 	({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; })
199 #endif
200 
201 #ifndef pr_fmt
202 #define pr_fmt(fmt) fmt
203 #endif
204 
205 /*
206  * Print a one-time message (analogous to WARN_ONCE() et al):
207  */
208 #define printk_once(...) do {			\
209 	static bool __print_once;		\
210 						\
211 	if (!__print_once) {			\
212 		__print_once = true;		\
213 		printk(__VA_ARGS__);		\
214 	}					\
215 } while (0)
216 
217 /*
218  * Log a one-time message (analogous to WARN_ONCE() et al):
219  */
220 #define log_once(level,...) do {		\
221 	static bool __log_once;			\
222 						\
223 	if (unlikely(!__log_once)) {		\
224 		__log_once = true;		\
225 		log(level, __VA_ARGS__);	\
226 	}					\
227 } while (0)
228 
229 #define pr_emerg(fmt, ...) \
230 	log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__)
231 #define pr_alert(fmt, ...) \
232 	log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__)
233 #define pr_crit(fmt, ...) \
234 	log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__)
235 #define pr_err(fmt, ...) \
236 	log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
237 #define pr_err_once(fmt, ...) \
238 	log_once(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
239 #define pr_warning(fmt, ...) \
240 	log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
241 #define pr_warn(...) \
242 	pr_warning(__VA_ARGS__)
243 #define pr_warn_once(fmt, ...) \
244 	log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
245 #define pr_notice(fmt, ...) \
246 	log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__)
247 #define pr_info(fmt, ...) \
248 	log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
249 #define pr_info_once(fmt, ...) \
250 	log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
251 #define pr_cont(fmt, ...) \
252 	printk(KERN_CONT fmt, ##__VA_ARGS__)
253 #define	pr_warn_ratelimited(...) do {		\
254 	static linux_ratelimit_t __ratelimited;	\
255 	if (linux_ratelimited(&__ratelimited))	\
256 		pr_warning(__VA_ARGS__);	\
257 } while (0)
258 
259 #ifndef WARN
260 #define	WARN(condition, ...) ({			\
261 	bool __ret_warn_on = (condition);	\
262 	if (unlikely(__ret_warn_on))		\
263 		pr_warning(__VA_ARGS__);	\
264 	unlikely(__ret_warn_on);		\
265 })
266 #endif
267 
268 #ifndef WARN_ONCE
269 #define	WARN_ONCE(condition, ...) ({		\
270 	bool __ret_warn_on = (condition);	\
271 	if (unlikely(__ret_warn_on))		\
272 		pr_warn_once(__VA_ARGS__);	\
273 	unlikely(__ret_warn_on);		\
274 })
275 #endif
276 
277 #define	ARRAY_SIZE(x)	(sizeof(x) / sizeof((x)[0]))
278 
279 #define	u64_to_user_ptr(val)	((void *)(uintptr_t)(val))
280 
281 #define _RET_IP_		__builtin_return_address(0)
282 
283 static inline unsigned long long
284 simple_strtoull(const char *cp, char **endp, unsigned int base)
285 {
286 	return (strtouq(cp, endp, base));
287 }
288 
289 static inline long long
290 simple_strtoll(const char *cp, char **endp, unsigned int base)
291 {
292 	return (strtoq(cp, endp, base));
293 }
294 
295 static inline unsigned long
296 simple_strtoul(const char *cp, char **endp, unsigned int base)
297 {
298 	return (strtoul(cp, endp, base));
299 }
300 
301 static inline long
302 simple_strtol(const char *cp, char **endp, unsigned int base)
303 {
304 	return (strtol(cp, endp, base));
305 }
306 
307 static inline int
308 kstrtoul(const char *cp, unsigned int base, unsigned long *res)
309 {
310 	char *end;
311 
312 	*res = strtoul(cp, &end, base);
313 
314 	/* skip newline character, if any */
315 	if (*end == '\n')
316 		end++;
317 	if (*cp == 0 || *end != 0)
318 		return (-EINVAL);
319 	return (0);
320 }
321 
322 static inline int
323 kstrtol(const char *cp, unsigned int base, long *res)
324 {
325 	char *end;
326 
327 	*res = 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 	return (0);
335 }
336 
337 static inline int
338 kstrtoint(const char *cp, unsigned int base, int *res)
339 {
340 	char *end;
341 	long temp;
342 
343 	*res = temp = strtol(cp, &end, base);
344 
345 	/* skip newline character, if any */
346 	if (*end == '\n')
347 		end++;
348 	if (*cp == 0 || *end != 0)
349 		return (-EINVAL);
350 	if (temp != (int)temp)
351 		return (-ERANGE);
352 	return (0);
353 }
354 
355 static inline int
356 kstrtouint(const char *cp, unsigned int base, unsigned int *res)
357 {
358 	char *end;
359 	unsigned long temp;
360 
361 	*res = temp = strtoul(cp, &end, base);
362 
363 	/* skip newline character, if any */
364 	if (*end == '\n')
365 		end++;
366 	if (*cp == 0 || *end != 0)
367 		return (-EINVAL);
368 	if (temp != (unsigned int)temp)
369 		return (-ERANGE);
370 	return (0);
371 }
372 
373 static inline int
374 kstrtou8(const char *cp, unsigned int base, u8 *res)
375 {
376 	char *end;
377 	unsigned long temp;
378 
379 	*res = temp = strtoul(cp, &end, base);
380 
381 	/* skip newline character, if any */
382 	if (*end == '\n')
383 		end++;
384 	if (*cp == 0 || *end != 0)
385 		return (-EINVAL);
386 	if (temp != (u8)temp)
387 		return (-ERANGE);
388 	return (0);
389 }
390 
391 static inline int
392 kstrtou16(const char *cp, unsigned int base, u16 *res)
393 {
394 	char *end;
395 	unsigned long temp;
396 
397 	*res = temp = strtoul(cp, &end, base);
398 
399 	/* skip newline character, if any */
400 	if (*end == '\n')
401 		end++;
402 	if (*cp == 0 || *end != 0)
403 		return (-EINVAL);
404 	if (temp != (u16)temp)
405 		return (-ERANGE);
406 	return (0);
407 }
408 
409 static inline int
410 kstrtou32(const char *cp, unsigned int base, u32 *res)
411 {
412 
413 	return (kstrtouint(cp, base, res));
414 }
415 
416 static inline int
417 kstrtou64(const char *cp, unsigned int base, u64 *res)
418 {
419        char *end;
420 
421        *res = strtouq(cp, &end, base);
422 
423        /* skip newline character, if any */
424        if (*end == '\n')
425                end++;
426        if (*cp == 0 || *end != 0)
427                return (-EINVAL);
428        return (0);
429 }
430 
431 static inline int
432 kstrtoull(const char *cp, unsigned int base, unsigned long long *res)
433 {
434 	return (kstrtou64(cp, base, (u64 *)res));
435 }
436 
437 static inline int
438 kstrtobool(const char *s, bool *res)
439 {
440 	int len;
441 
442 	if (s == NULL || (len = strlen(s)) == 0 || res == NULL)
443 		return (-EINVAL);
444 
445 	/* skip newline character, if any */
446 	if (s[len - 1] == '\n')
447 		len--;
448 
449 	if (len == 1 && strchr("yY1", s[0]) != NULL)
450 		*res = true;
451 	else if (len == 1 && strchr("nN0", s[0]) != NULL)
452 		*res = false;
453 	else if (strncasecmp("on", s, len) == 0)
454 		*res = true;
455 	else if (strncasecmp("off", s, len) == 0)
456 		*res = false;
457 	else
458 		return (-EINVAL);
459 
460 	return (0);
461 }
462 
463 static inline int
464 kstrtobool_from_user(const char __user *s, size_t count, bool *res)
465 {
466 	char buf[8] = {};
467 
468 	if (count > (sizeof(buf) - 1))
469 		count = (sizeof(buf) - 1);
470 
471 	if (copy_from_user(buf, s, count))
472 		return (-EFAULT);
473 
474 	return (kstrtobool(buf, res));
475 }
476 
477 static inline int
478 kstrtoint_from_user(const char __user *s, size_t count, unsigned int base,
479     int *p)
480 {
481 	char buf[36] = {};
482 
483 	if (count > (sizeof(buf) - 1))
484 		count = (sizeof(buf) - 1);
485 
486 	if (copy_from_user(buf, s, count))
487 		return (-EFAULT);
488 
489 	return (kstrtoint(buf, base, p));
490 }
491 
492 static inline int
493 kstrtouint_from_user(const char __user *s, size_t count, unsigned int base,
494     unsigned int *p)
495 {
496 	char buf[36] = {};
497 
498 	if (count > (sizeof(buf) - 1))
499 		count = (sizeof(buf) - 1);
500 
501 	if (copy_from_user(buf, s, count))
502 		return (-EFAULT);
503 
504 	return (kstrtouint(buf, base, p));
505 }
506 
507 static inline int
508 kstrtou32_from_user(const char __user *s, size_t count, unsigned int base,
509     unsigned int *p)
510 {
511 
512 	return (kstrtouint_from_user(s, count, base, p));
513 }
514 
515 static inline int
516 kstrtou8_from_user(const char __user *s, size_t count, unsigned int base,
517     u8 *p)
518 {
519 	char buf[8] = {};
520 
521 	if (count > (sizeof(buf) - 1))
522 		count = (sizeof(buf) - 1);
523 
524 	if (copy_from_user(buf, s, count))
525 		return (-EFAULT);
526 
527 	return (kstrtou8(buf, base, p));
528 }
529 
530 #define min(x, y)	((x) < (y) ? (x) : (y))
531 #define max(x, y)	((x) > (y) ? (x) : (y))
532 
533 #define min3(a, b, c)	min(a, min(b,c))
534 #define max3(a, b, c)	max(a, max(b,c))
535 
536 #define	min_t(type, x, y) ({			\
537 	type __min1 = (x);			\
538 	type __min2 = (y);			\
539 	__min1 < __min2 ? __min1 : __min2; })
540 
541 #define	max_t(type, x, y) ({			\
542 	type __max1 = (x);			\
543 	type __max2 = (y);			\
544 	__max1 > __max2 ? __max1 : __max2; })
545 
546 #define offsetofend(t, m)	\
547         (offsetof(t, m) + sizeof((((t *)0)->m)))
548 
549 #define clamp_t(type, _x, min, max)	min_t(type, max_t(type, _x, min), max)
550 #define clamp(x, lo, hi)		min( max(x,lo), hi)
551 #define	clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
552 
553 /*
554  * This looks more complex than it should be. But we need to
555  * get the type for the ~ right in round_down (it needs to be
556  * as wide as the result!), and we want to evaluate the macro
557  * arguments just once each.
558  */
559 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
560 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
561 #define round_down(x, y) ((x) & ~__round_mask(x, y))
562 
563 #define	smp_processor_id()	PCPU_GET(cpuid)
564 #define	num_possible_cpus()	mp_ncpus
565 #define	num_online_cpus()	mp_ncpus
566 
567 #if defined(__i386__) || defined(__amd64__)
568 extern bool linux_cpu_has_clflush;
569 #define	cpu_has_clflush		linux_cpu_has_clflush
570 #endif
571 
572 /* Swap values of a and b */
573 #define swap(a, b) do {			\
574 	typeof(a) _swap_tmp = a;	\
575 	a = b;				\
576 	b = _swap_tmp;			\
577 } while (0)
578 
579 #define	DIV_ROUND_CLOSEST(x, divisor)	(((x) + ((divisor) / 2)) / (divisor))
580 
581 #define	DIV_ROUND_CLOSEST_ULL(x, divisor) ({		\
582 	__typeof(divisor) __d = (divisor);		\
583 	unsigned long long __ret = (x) + (__d) / 2;	\
584 	__ret /= __d;					\
585 	__ret;						\
586 })
587 
588 static inline uintmax_t
589 mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
590 {
591 	uintmax_t q = (x / divisor);
592 	uintmax_t r = (x % divisor);
593 
594 	return ((q * multiplier) + ((r * multiplier) / divisor));
595 }
596 
597 typedef struct linux_ratelimit {
598 	struct timeval lasttime;
599 	int counter;
600 } linux_ratelimit_t;
601 
602 static inline bool
603 linux_ratelimited(linux_ratelimit_t *rl)
604 {
605 	return (ppsratecheck(&rl->lasttime, &rl->counter, 1));
606 }
607 
608 #define	struct_size(ptr, field, num) ({ \
609 	const size_t __size = offsetof(__typeof(*(ptr)), field); \
610 	const size_t __max = (SIZE_MAX - __size) / sizeof((ptr)->field[0]); \
611 	((num) > __max) ? SIZE_MAX : (__size + sizeof((ptr)->field[0]) * (num)); \
612 })
613 
614 #define	__is_constexpr(x) \
615 	__builtin_constant_p(x)
616 
617 /*
618  * The is_signed() macro below returns true if the passed data type is
619  * signed. Else false is returned.
620  */
621 #define	is_signed(datatype) (((datatype)-1 / (datatype)2) == (datatype)0)
622 
623 /*
624  * The type_max() macro below returns the maxium positive value the
625  * passed data type can hold.
626  */
627 #define	type_max(datatype) ( \
628   (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MAX : UINT64_MAX) : \
629   (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MAX : UINT32_MAX) : \
630   (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MAX : UINT16_MAX) : \
631 			    (is_signed(datatype) ? INT8_MAX : UINT8_MAX) \
632 )
633 
634 /*
635  * The type_min() macro below returns the minimum value the passed
636  * data type can hold. For unsigned types the minimum value is always
637  * zero. For signed types it may vary.
638  */
639 #define	type_min(datatype) ( \
640   (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MIN : 0) : \
641   (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MIN : 0) : \
642   (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MIN : 0) : \
643 			    (is_signed(datatype) ? INT8_MIN : 0) \
644 )
645 
646 #define	TAINT_WARN	0
647 #define	test_taint(x)	(0)
648 #define	add_taint(x,y)	do {	\
649 	} while (0)
650 
651 static inline int
652 _h2b(const char c)
653 {
654 
655 	if (c >= '0' && c <= '9')
656 		return (c - '0');
657 	if (c >= 'a' && c <= 'f')
658 		return (10 + c - 'a');
659 	if (c >= 'A' && c <= 'F')
660 		return (10 + c - 'A');
661 	return (-EINVAL);
662 }
663 
664 static inline int
665 hex2bin(uint8_t *bindst, const char *hexsrc, size_t binlen)
666 {
667 	int hi4, lo4;
668 
669 	while (binlen > 0) {
670 		hi4 = _h2b(*hexsrc++);
671 		lo4 = _h2b(*hexsrc++);
672 		if (hi4 < 0 || lo4 < 0)
673 			return (-EINVAL);
674 
675 		*bindst++ = (hi4 << 4) | lo4;
676 		binlen--;
677 	}
678 
679 	return (0);
680 }
681 
682 static inline bool
683 mac_pton(const char *macin, uint8_t *macout)
684 {
685 	const char *s, *d;
686 	uint8_t mac[6], hx, lx;;
687 	int i;
688 
689 	if (strlen(macin) < (3 * 6 - 1))
690 		return (false);
691 
692 	i = 0;
693 	s = macin;
694 	do {
695 		/* Should we also support '-'-delimiters? */
696 		d = strchrnul(s, ':');
697 		hx = lx = 0;
698 		while (s < d) {
699 			/* Fail on abc:123:xxx:... */
700 			if ((d - s) > 2)
701 				return (false);
702 			/* We do support non-well-formed strings: 3:45:6:... */
703 			if ((d - s) > 1) {
704 				hx = _h2b(*s);
705 				if (hx < 0)
706 					return (false);
707 				s++;
708 			}
709 			lx = _h2b(*s);
710 			if (lx < 0)
711 				return (false);
712 			s++;
713 		}
714 		mac[i] = (hx << 4) | lx;
715 		i++;
716 		if (i >= 6)
717 			return (false);
718 	} while (d != NULL && *d != '\0');
719 
720 	memcpy(macout, mac, 6);
721 	return (true);
722 }
723 
724 #define	DECLARE_FLEX_ARRAY(_t, _n)					\
725     struct { struct { } __dummy_ ## _n; _t _n[0]; }
726 
727 #endif	/* _LINUXKPI_LINUX_KERNEL_H_ */
728