1 /* SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 */
2 /* Copyright (c) 2017-2019 Mellanox Technologies. All rights reserved */
4 #ifndef _MLXFW_MFA2_TLV_H
5 #define _MLXFW_MFA2_TLV_H
7 #include <linux/kernel.h>
8 #include "mlxfw_mfa2_file.h"
10 struct mlxfw_mfa2_tlv {
17 static inline const struct mlxfw_mfa2_tlv *
18 mlxfw_mfa2_tlv_get(const struct mlxfw_mfa2_file *mfa2_file, const void *ptr)
20 if (!mlxfw_mfa2_valid_ptr(mfa2_file, ptr) ||
21 !mlxfw_mfa2_valid_ptr(mfa2_file, ptr + sizeof(struct mlxfw_mfa2_tlv)))
26 static inline const void *
27 mlxfw_mfa2_tlv_payload_get(const struct mlxfw_mfa2_file *mfa2_file,
28 const struct mlxfw_mfa2_tlv *tlv, u8 payload_type,
29 size_t payload_size, bool varsize)
33 tlv_top = (void *) tlv + be16_to_cpu(tlv->len) - 1;
34 if (!mlxfw_mfa2_valid_ptr(mfa2_file, tlv) ||
35 !mlxfw_mfa2_valid_ptr(mfa2_file, tlv_top))
37 if (tlv->type != payload_type)
39 if (varsize && (be16_to_cpu(tlv->len) < payload_size))
41 if (!varsize && (be16_to_cpu(tlv->len) != payload_size))
47 #define MLXFW_MFA2_TLV(name, payload_type, tlv_type) \
48 static inline const payload_type * \
49 mlxfw_mfa2_tlv_ ## name ## _get(const struct mlxfw_mfa2_file *mfa2_file, \
50 const struct mlxfw_mfa2_tlv *tlv) \
52 return mlxfw_mfa2_tlv_payload_get(mfa2_file, tlv, \
53 tlv_type, sizeof(payload_type), \
57 #define MLXFW_MFA2_TLV_VARSIZE(name, payload_type, tlv_type) \
58 static inline const payload_type * \
59 mlxfw_mfa2_tlv_ ## name ## _get(const struct mlxfw_mfa2_file *mfa2_file, \
60 const struct mlxfw_mfa2_tlv *tlv) \
62 return mlxfw_mfa2_tlv_payload_get(mfa2_file, tlv, \
63 tlv_type, sizeof(payload_type), \