kernel.h revision 277139
1139823Simp/*-
21541Srgrimes * Copyright (c) 2010 Isilon Systems, Inc.
3169465Srwatson * Copyright (c) 2010 iX Systems, Inc.
4169465Srwatson * Copyright (c) 2010 Panasas, Inc.
51541Srgrimes * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
61541Srgrimes * All rights reserved.
71541Srgrimes *
81541Srgrimes * Redistribution and use in source and binary forms, with or without
91541Srgrimes * modification, are permitted provided that the following conditions
101541Srgrimes * are met:
111541Srgrimes * 1. Redistributions of source code must retain the above copyright
121541Srgrimes *    notice unmodified, this list of conditions, and the following
131541Srgrimes *    disclaimer.
141541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151541Srgrimes *    notice, this list of conditions and the following disclaimer in the
161541Srgrimes *    documentation and/or other materials provided with the distribution.
171541Srgrimes *
181541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
191541Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
201541Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
211541Srgrimes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
221541Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
231541Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
241541Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
251541Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
261541Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
271541Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
281541Srgrimes */
291541Srgrimes#ifndef	_LINUX_KERNEL_H_
301541Srgrimes#define	_LINUX_KERNEL_H_
3150477Speter
321541Srgrimes#include <sys/systm.h>
331541Srgrimes#include <sys/param.h>
342169Spaul#include <sys/libkern.h>
35169465Srwatson#include <sys/stat.h>
362169Spaul#include <sys/smp.h>
371541Srgrimes#include <sys/stddef.h>
381541Srgrimes#include <sys/syslog.h>
39169465Srwatson
401541Srgrimes#include <linux/bitops.h>
411541Srgrimes#include <linux/compiler.h>
421541Srgrimes#include <linux/errno.h>
431541Srgrimes#include <linux/kthread.h>
441541Srgrimes#include <linux/types.h>
451541Srgrimes#include <linux/jiffies.h>
461541Srgrimes#include <linux/wait.h>
471541Srgrimes#include <linux/log2.h>
48137971Srwatson#include <asm/byteorder.h>
491541Srgrimes
501541Srgrimes#define KERN_CONT       ""
511541Srgrimes#define	KERN_EMERG	"<0>"
521541Srgrimes#define	KERN_ALERT	"<1>"
531541Srgrimes#define	KERN_CRIT	"<2>"
541541Srgrimes#define	KERN_ERR	"<3>"
551541Srgrimes#define	KERN_WARNING	"<4>"
561541Srgrimes#define	KERN_NOTICE	"<5>"
571541Srgrimes#define	KERN_INFO	"<6>"
581541Srgrimes#define	KERN_DEBUG	"<7>"
591541Srgrimes
6052904Sshin#define BUG()			panic("BUG")
6152904Sshin#define BUG_ON(condition)	do { if (condition) BUG(); } while(0)
6252904Sshin#define	WARN_ON			BUG_ON
6352904Sshin
6452904Sshin#undef	ALIGN
6552904Sshin#define	ALIGN(x, y)		roundup2((x), (y))
6652904Sshin#define	DIV_ROUND_UP		howmany
6752904Sshin
6852904Sshin#define	printk(X...)		printf(X)
6952904Sshin
7052904Sshin/*
7152904Sshin * The "pr_debug()" and "pr_devel()" macros should produce zero code
7252904Sshin * unless DEBUG is defined:
7352904Sshin */
741541Srgrimes#ifdef DEBUG
7552904Sshin#define pr_debug(fmt, ...) \
761541Srgrimes        log(LOG_DEBUG, fmt, ##__VA_ARGS__)
771541Srgrimes#define pr_devel(fmt, ...) \
781541Srgrimes	log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__)
791541Srgrimes#else
80169465Srwatson#define pr_debug(fmt, ...) \
81169465Srwatson        ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; })
82169465Srwatson#define pr_devel(fmt, ...) \
83169465Srwatson	({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; })
841541Srgrimes#endif
8512296Sphk
8645439Sjulian#define udelay(t)       	DELAY(t)
8745439Sjulian
8845439Sjulian#ifndef pr_fmt
8945439Sjulian#define pr_fmt(fmt) fmt
9045439Sjulian#endif
9145439Sjulian
9245439Sjulian/*
9352952Sjlemon * Print a one-time message (analogous to WARN_ONCE() et al):
9445439Sjulian */
9545439Sjulian#define printk_once(...) do {			\
9645439Sjulian	static bool __print_once;		\
9745439Sjulian						\
981541Srgrimes	if (!__print_once) {			\
991541Srgrimes		__print_once = true;		\
1001541Srgrimes		printk(__VA_ARGS__);		\
1011541Srgrimes	}					\
1021541Srgrimes} while (0)
1031541Srgrimes
1041541Srgrimes/*
105171656Sdes * Log a one-time message (analogous to WARN_ONCE() et al):
1061541Srgrimes */
1071541Srgrimes#define log_once(level,...) do {		\
1081541Srgrimes	static bool __log_once;			\
1091541Srgrimes						\
1101541Srgrimes	if (!__log_once) {			\
1112169Spaul		__log_once = true;		\
1122169Spaul		log(level, __VA_ARGS__);	\
113	}					\
114} while (0)
115
116#define pr_emerg(fmt, ...) \
117	log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__)
118#define pr_alert(fmt, ...) \
119	log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__)
120#define pr_crit(fmt, ...) \
121	log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__)
122#define pr_err(fmt, ...) \
123	log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
124#define pr_warning(fmt, ...) \
125	log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
126#define pr_warn pr_warning
127#define pr_notice(fmt, ...) \
128	log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__)
129#define pr_info(fmt, ...) \
130	log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
131#define pr_info_once(fmt, ...) \
132	log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
133#define pr_cont(fmt, ...) \
134	printk(KERN_CONT fmt, ##__VA_ARGS__)
135
136#ifndef WARN
137#define WARN(condition, format...) ({                                   \
138        int __ret_warn_on = !!(condition);                              \
139        if (unlikely(__ret_warn_on))                                    \
140                pr_warning(format);                                     \
141        unlikely(__ret_warn_on);                                        \
142})
143#endif
144
145#define container_of(ptr, type, member)				\
146({								\
147	__typeof(((type *)0)->member) *_p = (ptr);		\
148	(type *)((char *)_p - offsetof(type, member));		\
149})
150
151#define	ARRAY_SIZE(x)	(sizeof(x) / sizeof((x)[0]))
152
153#define	simple_strtoul	strtoul
154#define	simple_strtol	strtol
155#define kstrtol(a,b,c) ({*(c) = strtol(a,0,b);})
156
157#define min(x, y)	((x) < (y) ? (x) : (y))
158#define max(x, y)	((x) > (y) ? (x) : (y))
159#define min_t(type, _x, _y)	((type)(_x) < (type)(_y) ? (type)(_x) : (type)(_y))
160#define max_t(type, _x, _y)	((type)(_x) > (type)(_y) ? (type)(_x) : (type)(_y))
161
162/*
163 * This looks more complex than it should be. But we need to
164 * get the type for the ~ right in round_down (it needs to be
165 * as wide as the result!), and we want to evaluate the macro
166 * arguments just once each.
167 */
168#define __round_mask(x, y) ((__typeof__(x))((y)-1))
169#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
170#define round_down(x, y) ((x) & ~__round_mask(x, y))
171
172#define	num_possible_cpus()	mp_ncpus
173
174typedef struct pm_message {
175        int event;
176} pm_message_t;
177
178#endif	/* _LINUX_KERNEL_H_ */
179