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.
5677a229cSHans Petter Selasky  * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
68d59ecb2SHans Petter Selasky  * All rights reserved.
78d59ecb2SHans Petter Selasky  *
88d59ecb2SHans Petter Selasky  * Redistribution and use in source and binary forms, with or without
98d59ecb2SHans Petter Selasky  * modification, are permitted provided that the following conditions
108d59ecb2SHans Petter Selasky  * are met:
118d59ecb2SHans Petter Selasky  * 1. Redistributions of source code must retain the above copyright
128d59ecb2SHans Petter Selasky  *    notice unmodified, this list of conditions, and the following
138d59ecb2SHans Petter Selasky  *    disclaimer.
148d59ecb2SHans Petter Selasky  * 2. Redistributions in binary form must reproduce the above copyright
158d59ecb2SHans Petter Selasky  *    notice, this list of conditions and the following disclaimer in the
168d59ecb2SHans Petter Selasky  *    documentation and/or other materials provided with the distribution.
178d59ecb2SHans Petter Selasky  *
188d59ecb2SHans Petter Selasky  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
198d59ecb2SHans Petter Selasky  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
208d59ecb2SHans Petter Selasky  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
218d59ecb2SHans Petter Selasky  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
228d59ecb2SHans Petter Selasky  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
238d59ecb2SHans Petter Selasky  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
248d59ecb2SHans Petter Selasky  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
258d59ecb2SHans Petter Selasky  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
268d59ecb2SHans Petter Selasky  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
278d59ecb2SHans Petter Selasky  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
288d59ecb2SHans Petter Selasky  *
298d59ecb2SHans Petter Selasky  * $FreeBSD$
308d59ecb2SHans Petter Selasky  */
31307f78f3SVladimir Kondratyev #ifndef	_LINUXKPI_LINUX_ERR_H_
32307f78f3SVladimir Kondratyev #define	_LINUXKPI_LINUX_ERR_H_
338d59ecb2SHans Petter Selasky 
3447c0672bSHans Petter Selasky #include <sys/types.h>
3547c0672bSHans Petter Selasky 
36677a229cSHans Petter Selasky #include <linux/compiler.h>
37677a229cSHans Petter Selasky 
388d59ecb2SHans Petter Selasky #define MAX_ERRNO	4095
398d59ecb2SHans Petter Selasky 
40de66c9a1SJohn Baldwin #define IS_ERR_VALUE(x) unlikely((x) >= (uintptr_t)-MAX_ERRNO)
418d59ecb2SHans Petter Selasky 
428d59ecb2SHans Petter Selasky static inline void *
438d59ecb2SHans Petter Selasky ERR_PTR(long error)
448d59ecb2SHans Petter Selasky {
45de66c9a1SJohn Baldwin 	return (void *)(intptr_t)error;
468d59ecb2SHans Petter Selasky }
478d59ecb2SHans Petter Selasky 
488d59ecb2SHans Petter Selasky static inline long
498d59ecb2SHans Petter Selasky PTR_ERR(const void *ptr)
508d59ecb2SHans Petter Selasky {
51de66c9a1SJohn Baldwin 	return (intptr_t)ptr;
528d59ecb2SHans Petter Selasky }
538d59ecb2SHans Petter Selasky 
54de66c9a1SJohn Baldwin static inline bool
558d59ecb2SHans Petter Selasky IS_ERR(const void *ptr)
568d59ecb2SHans Petter Selasky {
57de66c9a1SJohn Baldwin 	return IS_ERR_VALUE((uintptr_t)ptr);
588d59ecb2SHans Petter Selasky }
598d59ecb2SHans Petter Selasky 
60de66c9a1SJohn Baldwin static inline bool
618d59ecb2SHans Petter Selasky IS_ERR_OR_NULL(const void *ptr)
628d59ecb2SHans Petter Selasky {
63de66c9a1SJohn Baldwin 	return !ptr || IS_ERR_VALUE((uintptr_t)ptr);
648d59ecb2SHans Petter Selasky }
658d59ecb2SHans Petter Selasky 
668d59ecb2SHans Petter Selasky static inline void *
6747c0672bSHans Petter Selasky ERR_CAST(const void *ptr)
688d59ecb2SHans Petter Selasky {
6947c0672bSHans Petter Selasky 	return __DECONST(void *, ptr);
708d59ecb2SHans Petter Selasky }
718d59ecb2SHans Petter Selasky 
728d59ecb2SHans Petter Selasky static inline int
738d59ecb2SHans Petter Selasky PTR_ERR_OR_ZERO(const void *ptr)
748d59ecb2SHans Petter Selasky {
758d59ecb2SHans Petter Selasky 	if (IS_ERR(ptr))
768d59ecb2SHans Petter Selasky 		return PTR_ERR(ptr);
778d59ecb2SHans Petter Selasky 	else
788d59ecb2SHans Petter Selasky 		return 0;
798d59ecb2SHans Petter Selasky }
808d59ecb2SHans Petter Selasky 
818d59ecb2SHans Petter Selasky #define PTR_RET(p) PTR_ERR_OR_ZERO(p)
828d59ecb2SHans Petter Selasky 
83307f78f3SVladimir Kondratyev #endif	/* _LINUXKPI_LINUX_ERR_H_ */
84