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