xref: /freebsd/sys/dev/mlxfw/mlxfw_mfa2_tlv_multi.c (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
13b2324c3SHans Petter Selasky /*-
23b2324c3SHans Petter Selasky  * SPDX-License-Identifier: BSD-3-Clause
33b2324c3SHans Petter Selasky  *
43b2324c3SHans Petter Selasky  * Copyright (c) 2017-2019 Mellanox Technologies.
53b2324c3SHans Petter Selasky  * All rights reserved.
63b2324c3SHans Petter Selasky  *
73b2324c3SHans Petter Selasky  * Redistribution and use in source and binary forms, with or without
83b2324c3SHans Petter Selasky  * modification, are permitted provided that the following conditions
93b2324c3SHans Petter Selasky  * are met:
103b2324c3SHans Petter Selasky  * 1. Redistributions of source code must retain the above copyright
113b2324c3SHans Petter Selasky  *    notice, this list of conditions and the following disclaimer.
123b2324c3SHans Petter Selasky  * 2. Redistributions in binary form must reproduce the above copyright
133b2324c3SHans Petter Selasky  *    notice, this list of conditions and the following disclaimer in the
143b2324c3SHans Petter Selasky  *    documentation and/or other materials provided with the distribution.
153b2324c3SHans Petter Selasky  * 3. Neither the name of Mellanox nor the names of contributors
163b2324c3SHans Petter Selasky  *    may be used to endorse or promote products derived from this software
173b2324c3SHans Petter Selasky  *    without specific prior written permission.
183b2324c3SHans Petter Selasky  *
193b2324c3SHans Petter Selasky  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
203b2324c3SHans Petter Selasky  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
213b2324c3SHans Petter Selasky  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
223b2324c3SHans Petter Selasky  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
233b2324c3SHans Petter Selasky  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
243b2324c3SHans Petter Selasky  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
253b2324c3SHans Petter Selasky  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
263b2324c3SHans Petter Selasky  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
273b2324c3SHans Petter Selasky  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
283b2324c3SHans Petter Selasky  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
293b2324c3SHans Petter Selasky  * SUCH DAMAGE.
303b2324c3SHans Petter Selasky  */
313b2324c3SHans Petter Selasky 
323b2324c3SHans Petter Selasky #define pr_fmt(fmt) "MFA2: " fmt
333b2324c3SHans Petter Selasky 
343b2324c3SHans Petter Selasky #include "mlxfw_mfa2_tlv_multi.h"
353b2324c3SHans Petter Selasky 
363b2324c3SHans Petter Selasky #define MLXFW_MFA2_TLV_TOTAL_SIZE(tlv) \
373b2324c3SHans Petter Selasky 	NLA_ALIGN(sizeof(*(tlv)) + be16_to_cpu((tlv)->len))
383b2324c3SHans Petter Selasky 
393b2324c3SHans Petter Selasky const struct mlxfw_mfa2_tlv *
mlxfw_mfa2_tlv_multi_child(const struct mlxfw_mfa2_file * mfa2_file,const struct mlxfw_mfa2_tlv_multi * multi)403b2324c3SHans Petter Selasky mlxfw_mfa2_tlv_multi_child(const struct mlxfw_mfa2_file *mfa2_file,
413b2324c3SHans Petter Selasky 			   const struct mlxfw_mfa2_tlv_multi *multi)
423b2324c3SHans Petter Selasky {
433b2324c3SHans Petter Selasky 	size_t multi_len;
443b2324c3SHans Petter Selasky 
453b2324c3SHans Petter Selasky 	multi_len = NLA_ALIGN(sizeof(struct mlxfw_mfa2_tlv_multi));
463b2324c3SHans Petter Selasky 	return mlxfw_mfa2_tlv_get(mfa2_file, (const u8 *) multi + multi_len);
473b2324c3SHans Petter Selasky }
483b2324c3SHans Petter Selasky 
493b2324c3SHans Petter Selasky const struct mlxfw_mfa2_tlv *
mlxfw_mfa2_tlv_next(const struct mlxfw_mfa2_file * mfa2_file,const struct mlxfw_mfa2_tlv * tlv)503b2324c3SHans Petter Selasky mlxfw_mfa2_tlv_next(const struct mlxfw_mfa2_file *mfa2_file,
513b2324c3SHans Petter Selasky 		    const struct mlxfw_mfa2_tlv *tlv)
523b2324c3SHans Petter Selasky {
533b2324c3SHans Petter Selasky 	const struct mlxfw_mfa2_tlv_multi *multi;
543b2324c3SHans Petter Selasky 	u16 tlv_len;
553b2324c3SHans Petter Selasky 	const u8 *next;
563b2324c3SHans Petter Selasky 
573b2324c3SHans Petter Selasky 	tlv_len = MLXFW_MFA2_TLV_TOTAL_SIZE(tlv);
583b2324c3SHans Petter Selasky 
593b2324c3SHans Petter Selasky 	if (tlv->type == MLXFW_MFA2_TLV_MULTI_PART) {
603b2324c3SHans Petter Selasky 		multi = mlxfw_mfa2_tlv_multi_get(mfa2_file, tlv);
61*ecaeac80SEric van Gyzen 		if (!multi)
62*ecaeac80SEric van Gyzen 			return NULL;
633b2324c3SHans Petter Selasky 		tlv_len = NLA_ALIGN(tlv_len + be16_to_cpu(multi->total_len));
643b2324c3SHans Petter Selasky 	}
653b2324c3SHans Petter Selasky 
663b2324c3SHans Petter Selasky 	next = (const u8 *) tlv + tlv_len;
673b2324c3SHans Petter Selasky 	return mlxfw_mfa2_tlv_get(mfa2_file, next);
683b2324c3SHans Petter Selasky }
693b2324c3SHans Petter Selasky 
703b2324c3SHans Petter Selasky const struct mlxfw_mfa2_tlv *
mlxfw_mfa2_tlv_advance(const struct mlxfw_mfa2_file * mfa2_file,const struct mlxfw_mfa2_tlv * from_tlv,u16 count)713b2324c3SHans Petter Selasky mlxfw_mfa2_tlv_advance(const struct mlxfw_mfa2_file *mfa2_file,
723b2324c3SHans Petter Selasky 		       const struct mlxfw_mfa2_tlv *from_tlv, u16 count)
733b2324c3SHans Petter Selasky {
743b2324c3SHans Petter Selasky 	const struct mlxfw_mfa2_tlv *tlv;
753b2324c3SHans Petter Selasky 	u16 idx;
763b2324c3SHans Petter Selasky 
773b2324c3SHans Petter Selasky 	mlxfw_mfa2_tlv_foreach(mfa2_file, tlv, idx, from_tlv, count)
783b2324c3SHans Petter Selasky 		if (!tlv)
793b2324c3SHans Petter Selasky 			return NULL;
803b2324c3SHans Petter Selasky 	return tlv;
813b2324c3SHans Petter Selasky }
823b2324c3SHans Petter Selasky 
833b2324c3SHans Petter Selasky const struct mlxfw_mfa2_tlv *
mlxfw_mfa2_tlv_multi_child_find(const struct mlxfw_mfa2_file * mfa2_file,const struct mlxfw_mfa2_tlv_multi * multi,enum mlxfw_mfa2_tlv_type type,u16 index)843b2324c3SHans Petter Selasky mlxfw_mfa2_tlv_multi_child_find(const struct mlxfw_mfa2_file *mfa2_file,
853b2324c3SHans Petter Selasky 				const struct mlxfw_mfa2_tlv_multi *multi,
863b2324c3SHans Petter Selasky 				enum mlxfw_mfa2_tlv_type type, u16 index)
873b2324c3SHans Petter Selasky {
883b2324c3SHans Petter Selasky 	const struct mlxfw_mfa2_tlv *tlv;
893b2324c3SHans Petter Selasky 	u16 skip = 0;
903b2324c3SHans Petter Selasky 	u16 idx;
913b2324c3SHans Petter Selasky 
923b2324c3SHans Petter Selasky 	mlxfw_mfa2_tlv_multi_foreach(mfa2_file, tlv, idx, multi) {
933b2324c3SHans Petter Selasky 		if (!tlv) {
943b2324c3SHans Petter Selasky 			pr_err("TLV parsing error\n");
953b2324c3SHans Petter Selasky 			return NULL;
963b2324c3SHans Petter Selasky 		}
973b2324c3SHans Petter Selasky 		if (tlv->type == type)
983b2324c3SHans Petter Selasky 			if (skip++ == index)
993b2324c3SHans Petter Selasky 				return tlv;
1003b2324c3SHans Petter Selasky 	}
1013b2324c3SHans Petter Selasky 	return NULL;
1023b2324c3SHans Petter Selasky }
1033b2324c3SHans Petter Selasky 
mlxfw_mfa2_tlv_multi_child_count(const struct mlxfw_mfa2_file * mfa2_file,const struct mlxfw_mfa2_tlv_multi * multi,enum mlxfw_mfa2_tlv_type type,u16 * p_count)1043b2324c3SHans Petter Selasky int mlxfw_mfa2_tlv_multi_child_count(const struct mlxfw_mfa2_file *mfa2_file,
1053b2324c3SHans Petter Selasky 				     const struct mlxfw_mfa2_tlv_multi *multi,
1063b2324c3SHans Petter Selasky 				     enum mlxfw_mfa2_tlv_type type,
1073b2324c3SHans Petter Selasky 				     u16 *p_count)
1083b2324c3SHans Petter Selasky {
1093b2324c3SHans Petter Selasky 	const struct mlxfw_mfa2_tlv *tlv;
1103b2324c3SHans Petter Selasky 	u16 count = 0;
1113b2324c3SHans Petter Selasky 	u16 idx;
1123b2324c3SHans Petter Selasky 
1133b2324c3SHans Petter Selasky 	mlxfw_mfa2_tlv_multi_foreach(mfa2_file, tlv, idx, multi) {
1143b2324c3SHans Petter Selasky 		if (!tlv) {
1153b2324c3SHans Petter Selasky 			pr_err("TLV parsing error\n");
1163b2324c3SHans Petter Selasky 			return -EINVAL;
1173b2324c3SHans Petter Selasky 		}
1183b2324c3SHans Petter Selasky 
1193b2324c3SHans Petter Selasky 		if (tlv->type == type)
1203b2324c3SHans Petter Selasky 			count++;
1213b2324c3SHans Petter Selasky 	}
1223b2324c3SHans Petter Selasky 	*p_count = count;
1233b2324c3SHans Petter Selasky 	return 0;
1243b2324c3SHans Petter Selasky }
125