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