1219820Sjeff/*
2219820Sjeff * Copyright (c) 2004-2007 Voltaire Inc.  All rights reserved.
3219820Sjeff *
4219820Sjeff * This software is available to you under a choice of one of two
5219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
6219820Sjeff * General Public License (GPL) Version 2, available from the file
7219820Sjeff * COPYING in the main directory of this source tree, or the
8219820Sjeff * OpenIB.org BSD license below:
9219820Sjeff *
10219820Sjeff *     Redistribution and use in source and binary forms, with or
11219820Sjeff *     without modification, are permitted provided that the following
12219820Sjeff *     conditions are met:
13219820Sjeff *
14219820Sjeff *      - Redistributions of source code must retain the above
15219820Sjeff *        copyright notice, this list of conditions and the following
16219820Sjeff *        disclaimer.
17219820Sjeff *
18219820Sjeff *      - Redistributions in binary form must reproduce the above
19219820Sjeff *        copyright notice, this list of conditions and the following
20219820Sjeff *        disclaimer in the documentation and/or other materials
21219820Sjeff *        provided with the distribution.
22219820Sjeff *
23219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30219820Sjeff * SOFTWARE.
31219820Sjeff *
32219820Sjeff */
33219820Sjeff#ifndef __COMMON_H__
34219820Sjeff#define __COMMON_H__
35219820Sjeff
36219820Sjeff#include <stdio.h>
37219820Sjeff#include <sys/select.h>
38219820Sjeff#include <sys/types.h>
39219820Sjeff#include <dirent.h>
40219820Sjeff#include <stdint.h>
41219820Sjeff#include <infiniband/byteswap.h>
42219820Sjeff
43219820Sjeff#ifdef __cplusplus
44219820Sjeff#  define BEGIN_C_DECLS extern "C" {
45219820Sjeff#  define END_C_DECLS   }
46219820Sjeff#else /* !__cplusplus */
47219820Sjeff#  define BEGIN_C_DECLS
48219820Sjeff#  define END_C_DECLS
49219820Sjeff#endif /* __cplusplus */
50219820Sjeff
51219820SjeffBEGIN_C_DECLS
52219820Sjeff
53219820Sjeff#if __BYTE_ORDER == __LITTLE_ENDIAN
54219820Sjeff#ifndef ntohll
55219820Sjeffstatic inline uint64_t ntohll(uint64_t x) {
56219820Sjeff	return bswap_64(x);
57219820Sjeff}
58219820Sjeff#endif
59219820Sjeff#ifndef htonll
60219820Sjeffstatic inline uint64_t htonll(uint64_t x) {
61219820Sjeff	return bswap_64(x);
62219820Sjeff}
63219820Sjeff#endif
64219820Sjeff#elif __BYTE_ORDER == __BIG_ENDIAN
65219820Sjeff#ifndef ntohll
66219820Sjeffstatic inline uint64_t ntohll(uint64_t x) {
67219820Sjeff	return x;
68219820Sjeff}
69219820Sjeff#endif
70219820Sjeff#ifndef htonll
71219820Sjeffstatic inline uint64_t htonll(uint64_t x) {
72219820Sjeff	return x;
73219820Sjeff}
74219820Sjeff#endif
75219820Sjeff#endif	/* __BYTE_ORDER == __BIG_ENDIAN */
76219820Sjeff
77219820Sjeff/*****************************
78219820Sjeff * COMMON MACHINE INDEPENDENT
79219820Sjeff */
80219820Sjeff
81219820Sjeff/* Misc. macros: */
82219820Sjeff/** align value \a l to \a size (ceil) */
83219820Sjeff#ifndef ALIGN
84219820Sjeff#define ALIGN(l, size) (((l) + ((size) - 1)) / (size) * (size))
85219820Sjeff
86219820Sjeff/** align value \a l to \a sizeof 32 bit int (ceil) */
87219820Sjeff#define ALIGN32(l) (ALIGN((l), sizeof(uint32)))
88219820Sjeff#endif
89219820Sjeff
90219820Sjeff/** printf style debugging MACRO, conmmon header includes name of function */
91219820Sjeff#define IBWARN(fmt, args...)	ibwarn(__FUNCTION__, fmt, ## args)
92219820Sjeff
93219820Sjeff/** printf style debugging MACRO, conmmon header includes name of function */
94219820Sjeff#define LOG(fmt, args...)	logmsg(__FUNCTION__, fmt, ## args)
95219820Sjeff
96219820Sjeff/** printf style abort MACRO, common header includes name of function */
97219820Sjeff#define IBPANIC(fmt, args...)	ibpanic(__FUNCTION__, fmt, ## args)
98219820Sjeff
99219820Sjeff/** abort program if expression \a x is \b false */
100219820Sjeff#define SANITY(x)		if (common.sanity && !(x))\
101219820Sjeff					ibpanic(__FUNCTION__,\
102219820Sjeff					"sanity check <%s> failed: line %d",\
103219820Sjeff					(x), __LINE__)
104219820Sjeff
105219820Sjeff/** avoid unused compilation warning  */
106219820Sjeff#ifndef USED
107219820Sjeff#define USED(x)	while(0) {void *v = &(x); printf("%p", v);}
108219820Sjeff#endif
109219820Sjeff
110219820Sjeff/** define index macro for string array generated by enumstr.awk */
111219820Sjeff#define ENUM_STR_DEF(enumname, last, val) 	(((unsigned)(val) < last) ? enumname ## _str[val] : "???")
112219820Sjeff#define ENUM_STR_ARRAY(name)		char * name ## _str[]
113219820Sjeff
114219820Sjeff#ifdef __GNUC__
115219820Sjeff#define IBCOMMON_STRICT_FORMAT __attribute__((format(printf, 2, 3)))
116219820Sjeff#else
117219820Sjeff#define IBCOMMON_STRICT_FORMAT
118219820Sjeff#endif
119219820Sjeff
120219820Sjeff/* util.c: debugging and tracing */
121219820Sjeffvoid	ibwarn(const char * const fn, char *msg, ...) IBCOMMON_STRICT_FORMAT;
122219820Sjeffvoid	ibpanic(const char * const fn, char *msg, ...) IBCOMMON_STRICT_FORMAT;
123219820Sjeffvoid	logmsg(const char *const fn, char *msg, ...) IBCOMMON_STRICT_FORMAT;
124219820Sjeff
125219820Sjeffvoid	xdump(FILE *file, char *msg, void *p, int size);
126219820Sjeff
127219820Sjeff/* sysfs.c: /sys utilities */
128219820Sjeffint	sys_read_string(char *dir_name, char *file_name, char *str, int max_len);
129219820Sjeffint	sys_read_guid(char *dir_name, char *file_name, uint64_t *net_guid);
130219820Sjeffint	sys_read_gid(char *dir_name, char *file_name, uint8_t *gid);
131219820Sjeffint	sys_read_uint64(char *dir_name, char *file_name, uint64_t *u);
132219820Sjeffint	sys_read_uint(char *dir_name, char *file_name, unsigned *u);
133219820Sjeffint	sys_scandir(const char *dirname, struct dirent ***namelist,
134219820Sjeff	    int (*select)(const struct dirent *),
135219820Sjeff	    int (*compar)(const struct dirent **, const struct dirent **));
136219820Sjeff
137219820Sjeff/* stack.c */
138219820Sjeffvoid	stack_dump(void);
139219820Sjeffvoid	enable_stack_dump(int loop);
140219820Sjeff
141219820Sjeff/* time.c */
142219820Sjeffuint64_t getcurrenttime(void);
143219820Sjeff
144219820Sjeff/* hash.c */
145219820Sjeffuint32_t fhash(uint8_t *k, int length, uint32_t initval);
146219820Sjeff
147219820SjeffEND_C_DECLS
148219820Sjeff
149219820Sjeff#endif /* __COMMON_H__ */
150