162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2020, Mellanox Technologies inc.  All rights reserved.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci#ifndef _IBA_DEFS_H_
662306a36Sopenharmony_ci#define _IBA_DEFS_H_
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/kernel.h>
962306a36Sopenharmony_ci#include <linux/bitfield.h>
1062306a36Sopenharmony_ci#include <asm/unaligned.h>
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_cistatic inline u32 _iba_get8(const u8 *ptr)
1362306a36Sopenharmony_ci{
1462306a36Sopenharmony_ci	return *ptr;
1562306a36Sopenharmony_ci}
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_cistatic inline void _iba_set8(u8 *ptr, u32 mask, u32 prep_value)
1862306a36Sopenharmony_ci{
1962306a36Sopenharmony_ci	*ptr = (*ptr & ~mask) | prep_value;
2062306a36Sopenharmony_ci}
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_cistatic inline u16 _iba_get16(const __be16 *ptr)
2362306a36Sopenharmony_ci{
2462306a36Sopenharmony_ci	return be16_to_cpu(*ptr);
2562306a36Sopenharmony_ci}
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_cistatic inline void _iba_set16(__be16 *ptr, u16 mask, u16 prep_value)
2862306a36Sopenharmony_ci{
2962306a36Sopenharmony_ci	*ptr = cpu_to_be16((be16_to_cpu(*ptr) & ~mask) | prep_value);
3062306a36Sopenharmony_ci}
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_cistatic inline u32 _iba_get32(const __be32 *ptr)
3362306a36Sopenharmony_ci{
3462306a36Sopenharmony_ci	return be32_to_cpu(*ptr);
3562306a36Sopenharmony_ci}
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cistatic inline void _iba_set32(__be32 *ptr, u32 mask, u32 prep_value)
3862306a36Sopenharmony_ci{
3962306a36Sopenharmony_ci	*ptr = cpu_to_be32((be32_to_cpu(*ptr) & ~mask) | prep_value);
4062306a36Sopenharmony_ci}
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_cistatic inline u64 _iba_get64(const __be64 *ptr)
4362306a36Sopenharmony_ci{
4462306a36Sopenharmony_ci	/*
4562306a36Sopenharmony_ci	 * The mads are constructed so that 32 bit and smaller are naturally
4662306a36Sopenharmony_ci	 * aligned, everything larger has a max alignment of 4 bytes.
4762306a36Sopenharmony_ci	 */
4862306a36Sopenharmony_ci	return be64_to_cpu(get_unaligned(ptr));
4962306a36Sopenharmony_ci}
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_cistatic inline void _iba_set64(__be64 *ptr, u64 mask, u64 prep_value)
5262306a36Sopenharmony_ci{
5362306a36Sopenharmony_ci	put_unaligned(cpu_to_be64((_iba_get64(ptr) & ~mask) | prep_value), ptr);
5462306a36Sopenharmony_ci}
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci#define _IBA_SET(field_struct, field_offset, field_mask, num_bits, ptr, value) \
5762306a36Sopenharmony_ci	({                                                                     \
5862306a36Sopenharmony_ci		field_struct *_ptr = ptr;                                      \
5962306a36Sopenharmony_ci		_iba_set##num_bits((void *)_ptr + (field_offset), field_mask,  \
6062306a36Sopenharmony_ci				   FIELD_PREP(field_mask, value));             \
6162306a36Sopenharmony_ci	})
6262306a36Sopenharmony_ci#define IBA_SET(field, ptr, value) _IBA_SET(field, ptr, value)
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci#define _IBA_GET_MEM_PTR(field_struct, field_offset, type, num_bits, ptr)      \
6562306a36Sopenharmony_ci	({                                                                     \
6662306a36Sopenharmony_ci		field_struct *_ptr = ptr;                                      \
6762306a36Sopenharmony_ci		(type *)((void *)_ptr + (field_offset));                       \
6862306a36Sopenharmony_ci	})
6962306a36Sopenharmony_ci#define IBA_GET_MEM_PTR(field, ptr) _IBA_GET_MEM_PTR(field, ptr)
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci/* FIXME: A set should always set the entire field, meaning we should zero the trailing bytes */
7262306a36Sopenharmony_ci#define _IBA_SET_MEM(field_struct, field_offset, type, num_bits, ptr, in,      \
7362306a36Sopenharmony_ci		     bytes)                                                    \
7462306a36Sopenharmony_ci	({                                                                     \
7562306a36Sopenharmony_ci		const type *_in_ptr = in;                                      \
7662306a36Sopenharmony_ci		WARN_ON(bytes * 8 > num_bits);                                 \
7762306a36Sopenharmony_ci		if (in && bytes)                                               \
7862306a36Sopenharmony_ci			memcpy(_IBA_GET_MEM_PTR(field_struct, field_offset,    \
7962306a36Sopenharmony_ci						type, num_bits, ptr),          \
8062306a36Sopenharmony_ci			       _in_ptr, bytes);                                \
8162306a36Sopenharmony_ci	})
8262306a36Sopenharmony_ci#define IBA_SET_MEM(field, ptr, in, bytes) _IBA_SET_MEM(field, ptr, in, bytes)
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci#define _IBA_GET(field_struct, field_offset, field_mask, num_bits, ptr)        \
8562306a36Sopenharmony_ci	({                                                                     \
8662306a36Sopenharmony_ci		const field_struct *_ptr = ptr;                                \
8762306a36Sopenharmony_ci		(u##num_bits) FIELD_GET(                                       \
8862306a36Sopenharmony_ci			field_mask, _iba_get##num_bits((const void *)_ptr +    \
8962306a36Sopenharmony_ci						       (field_offset)));       \
9062306a36Sopenharmony_ci	})
9162306a36Sopenharmony_ci#define IBA_GET(field, ptr) _IBA_GET(field, ptr)
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci#define _IBA_GET_MEM(field_struct, field_offset, type, num_bits, ptr, out,     \
9462306a36Sopenharmony_ci		     bytes)                                                    \
9562306a36Sopenharmony_ci	({                                                                     \
9662306a36Sopenharmony_ci		type *_out_ptr = out;                                          \
9762306a36Sopenharmony_ci		WARN_ON(bytes * 8 > num_bits);                                 \
9862306a36Sopenharmony_ci		if (out && bytes)                                              \
9962306a36Sopenharmony_ci			memcpy(_out_ptr,                                       \
10062306a36Sopenharmony_ci			       _IBA_GET_MEM_PTR(field_struct, field_offset,    \
10162306a36Sopenharmony_ci						type, num_bits, ptr),          \
10262306a36Sopenharmony_ci			       bytes);                                         \
10362306a36Sopenharmony_ci	})
10462306a36Sopenharmony_ci#define IBA_GET_MEM(field, ptr, out, bytes) _IBA_GET_MEM(field, ptr, out, bytes)
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci/*
10762306a36Sopenharmony_ci * The generated list becomes the parameters to the macros, the order is:
10862306a36Sopenharmony_ci *  - struct this applies to
10962306a36Sopenharmony_ci *  - starting offset of the max
11062306a36Sopenharmony_ci *  - GENMASK or GENMASK_ULL in CPU order
11162306a36Sopenharmony_ci *  - The width of data the mask operations should work on, in bits
11262306a36Sopenharmony_ci */
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci/*
11562306a36Sopenharmony_ci * Extraction using a tabular description like table 106. bit_offset is from
11662306a36Sopenharmony_ci * the Byte[Bit] notation.
11762306a36Sopenharmony_ci */
11862306a36Sopenharmony_ci#define IBA_FIELD_BLOC(field_struct, byte_offset, bit_offset, num_bits)        \
11962306a36Sopenharmony_ci	field_struct, byte_offset,                                             \
12062306a36Sopenharmony_ci		GENMASK(7 - (bit_offset), 7 - (bit_offset) - (num_bits - 1)),  \
12162306a36Sopenharmony_ci		8
12262306a36Sopenharmony_ci#define IBA_FIELD8_LOC(field_struct, byte_offset, num_bits)                    \
12362306a36Sopenharmony_ci	IBA_FIELD_BLOC(field_struct, byte_offset, 0, num_bits)
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci#define IBA_FIELD16_LOC(field_struct, byte_offset, num_bits)                   \
12662306a36Sopenharmony_ci	field_struct, (byte_offset)&0xFFFE,                                    \
12762306a36Sopenharmony_ci		GENMASK(15 - (((byte_offset) % 2) * 8),                        \
12862306a36Sopenharmony_ci			15 - (((byte_offset) % 2) * 8) - (num_bits - 1)),      \
12962306a36Sopenharmony_ci		16
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci#define IBA_FIELD32_LOC(field_struct, byte_offset, num_bits)                   \
13262306a36Sopenharmony_ci	field_struct, (byte_offset)&0xFFFC,                                    \
13362306a36Sopenharmony_ci		GENMASK(31 - (((byte_offset) % 4) * 8),                        \
13462306a36Sopenharmony_ci			31 - (((byte_offset) % 4) * 8) - (num_bits - 1)),      \
13562306a36Sopenharmony_ci		32
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci#define IBA_FIELD64_LOC(field_struct, byte_offset)                             \
13862306a36Sopenharmony_ci	field_struct, byte_offset, GENMASK_ULL(63, 0), 64
13962306a36Sopenharmony_ci/*
14062306a36Sopenharmony_ci * In IBTA spec, everything that is more than 64bits is multiple
14162306a36Sopenharmony_ci * of bytes without leftover bits.
14262306a36Sopenharmony_ci */
14362306a36Sopenharmony_ci#define IBA_FIELD_MLOC(field_struct, byte_offset, num_bits, type)              \
14462306a36Sopenharmony_ci	field_struct, byte_offset, type, num_bits
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci#endif /* _IBA_DEFS_H_ */
147