summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/mlxcx/mlxcx_endint.h
blob: 4ad69173c00b03abbabd8407ade5e3c099e2f62e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2020, The University of Queensland
 */

#ifndef _MLXCX_ENDINT_H
#define	_MLXCX_ENDINT_H

#include <sys/types.h>
#include <sys/byteorder.h>

/*
 * The inlines and structs in this file are used by mlxcx to ensure endian
 * safety when dealing with memory-mapped structures from the device, and
 * also simpler use of 24-bit integers (which Mellanox loves).
 *
 * By declaring all of these values in the memory-mapped structures as structs
 * (e.g. uint32be_t) rather than bare integers (uint32_t) we ensure that the
 * compiler will not allow them to be silently converted to integers and used
 * without doing the necessary byte-swapping work.
 *
 * The uintXbe_t structs are designed to be used inside a #pragma pack(1)
 * context only and we don't try to fix up their alignment.
 *
 * Also present in here are a number of bitsX_t types which can be used to
 * gain a little bit of type safety when dealing with endian-swapped bitfields.
 */

#pragma pack(1)
typedef struct { uint16_t be_val; } uint16be_t;
typedef struct { uint8_t be_val[3]; } uint24be_t;
typedef struct { uint32_t be_val; } uint32be_t;
typedef struct { uint64_t be_val; } uint64be_t;
#pragma pack()

static inline uint16_t
from_be16(uint16be_t v)
{
	return (BE_16(v.be_val));
}

static inline uint32_t
from_be24(uint24be_t v)
{
	return (((uint32_t)v.be_val[0] << 16) |
	    ((uint32_t)v.be_val[1] << 8) |
	    ((uint32_t)v.be_val[2]));
}

static inline uint32_t
from_be32(uint32be_t v)
{
	return (BE_32(v.be_val));
}

static inline uint64_t
from_be64(uint64be_t v)
{
	return (BE_64(v.be_val));
}

static inline uint16be_t
to_be16(uint16_t v)
{
	/* CSTYLED */
	return ((uint16be_t){ .be_val = BE_16(v) });
}

static inline uint24be_t
to_be24(uint32_t v)
{
	/* CSTYLED */
	return ((uint24be_t){ .be_val = {
	    (v & 0xFF0000) >> 16,
	    (v & 0x00FF00) >> 8,
	    (v & 0x0000FF)
	}});
}

static inline uint32be_t
to_be32(uint32_t v)
{
	/* CSTYLED */
	return ((uint32be_t){ .be_val = BE_32(v) });
}

static inline uint64be_t
to_be64(uint64_t v)
{
	/* CSTYLED */
	return ((uint64be_t){ .be_val = BE_64(v) });
}

#pragma pack(1)
typedef struct { uint8_t bit_val; } bits8_t;
typedef struct { uint16_t bit_val; } bits16_t;
typedef struct { uint32_t bit_val; } bits32_t;
typedef struct { uint24be_t bit_val; } bits24_t;
typedef struct { uint64_t bit_val; } bits64_t;
typedef struct { uint64_t bit_shift; uint64_t bit_mask; } bitdef_t;
#pragma pack()

static inline uint8_t
get_bits8(bits8_t v, bitdef_t d)
{
	return ((v.bit_val & d.bit_mask) >> d.bit_shift);
}
static inline void
set_bits8(bits8_t *v, bitdef_t d, uint8_t val)
{
	v->bit_val &= ~d.bit_mask;
	v->bit_val |= (val << d.bit_shift) & d.bit_mask;
}
static inline uint8_t
get_bit8(bits8_t v, uint8_t mask)
{
	return ((v.bit_val & mask) != 0);
}
static inline void
set_bit8(bits8_t *v, uint8_t mask)
{
	v->bit_val |= mask;
}
static inline void
clear_bit8(bits8_t *v, uint8_t mask)
{
	v->bit_val &= ~mask;
}
static inline bits8_t
new_bits8(void)
{
	/* CSTYLED */
	return ((bits8_t){ .bit_val = 0 });
}
static inline uint8_t
from_bits8(bits8_t v)
{
	return (v.bit_val);
}

static inline uint16_t
get_bits16(bits16_t v, bitdef_t d)
{
	return ((BE_16(v.bit_val) & d.bit_mask) >> d.bit_shift);
}
static inline void
set_bits16(bits16_t *v, bitdef_t d, uint16_t val)
{
	v->bit_val &= BE_16(~d.bit_mask);
	v->bit_val |= BE_16((val << d.bit_shift) & d.bit_mask);
}
static inline uint16_t
get_bit16(bits16_t v, uint16_t mask)
{
	return ((BE_16(v.bit_val) & mask) != 0);
}
static inline void
set_bit16(bits16_t *v, uint16_t mask)
{
	v->bit_val |= BE_16(mask);
}
static inline void
clear_bit16(bits16_t *v, uint16_t mask)
{
	v->bit_val &= BE_16(~mask);
}
static inline bits16_t
new_bits16(void)
{
	/* CSTYLED */
	return ((bits16_t){ .bit_val = 0 });
}
static inline uint16_t
from_bits16(bits16_t v)
{
	return (BE_16(v.bit_val));
}

static inline uint32_t
get_bits32(bits32_t v, bitdef_t d)
{
	return ((BE_32(v.bit_val) & d.bit_mask) >> d.bit_shift);
}
static inline void
set_bits32(bits32_t *v, bitdef_t d, uint32_t val)
{
	v->bit_val &= BE_32(~d.bit_mask);
	v->bit_val |= BE_32((val << d.bit_shift) & d.bit_mask);
}
static inline uint32_t
get_bit32(bits32_t v, uint32_t mask)
{
	return ((BE_32(v.bit_val) & mask) != 0);
}
static inline void
set_bit32(bits32_t *v, uint32_t mask)
{
	v->bit_val |= BE_32(mask);
}
static inline void
clear_bit32(bits32_t *v, uint32_t mask)
{
	v->bit_val &= BE_32(~mask);
}
static inline bits32_t
new_bits32(void)
{
	/* CSTYLED */
	return ((bits32_t){ .bit_val = 0 });
}
static inline uint32_t
from_bits32(bits32_t v)
{
	return (BE_32(v.bit_val));
}

static inline uint32_t
get_bits24(bits24_t v, bitdef_t d)
{
	return ((from_be24(v.bit_val) & d.bit_mask) >> d.bit_shift);
}
static inline void
set_bits24(bits24_t *v, bitdef_t d, uint32_t val)
{
	uint32_t vv = from_be24(v->bit_val);
	vv &= ~d.bit_mask;
	vv |= (val << d.bit_shift) & d.bit_mask;
	v->bit_val = to_be24(vv);
}
static inline uint32_t
get_bit24(bits24_t v, uint32_t mask)
{
	return ((from_be24(v.bit_val) & mask) != 0);
}
static inline void
set_bit24(bits24_t *v, uint32_t mask)
{
	v->bit_val = to_be24(from_be24(v->bit_val) | mask);
}
static inline void
clear_bit24(bits24_t *v, uint32_t mask)
{
	v->bit_val = to_be24(from_be24(v->bit_val) & ~mask);
}
static inline bits24_t
new_bits24(void)
{
	/* CSTYLED */
	return ((bits24_t){ .bit_val = to_be24(0) });
}
static inline uint32_t
from_bits24(bits24_t v)
{
	return (from_be24(v.bit_val));
}

static inline uint64_t
get_bits64(bits64_t v, bitdef_t d)
{
	return ((BE_64(v.bit_val) & d.bit_mask) >> d.bit_shift);
}
static inline void
set_bits64(bits64_t *v, bitdef_t d, uint64_t val)
{
	v->bit_val &= BE_64(~d.bit_mask);
	v->bit_val |= BE_64((val << d.bit_shift) & d.bit_mask);
}
static inline uint64_t
get_bit64(bits64_t v, uint64_t mask)
{
	return ((BE_64(v.bit_val) & mask) != 0);
}
static inline void
set_bit64(bits64_t *v, uint64_t mask)
{
	v->bit_val |= BE_64(mask);
}
static inline void
clear_bit64(bits64_t *v, uint64_t mask)
{
	v->bit_val &= BE_64(~mask);
}
static inline bits64_t
new_bits64(void)
{
	/* CSTYLED */
	return ((bits64_t){ .bit_val = 0 });
}
static inline uint64_t
from_bits64(bits64_t v)
{
	return (BE_64(v.bit_val));
}

#endif /* _MLXCX_ENDINT_H */