1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * drivers/net/ethernet/rocker/rocker_tlv.c - Rocker switch device driver
4  * Copyright (c) 2014-2016 Jiri Pirko <jiri@mellanox.com>
5  * Copyright (c) 2014 Scott Feldman <sfeldma@gmail.com>
6  */
7 
8 #include <linux/types.h>
9 #include <linux/string.h>
10 #include <linux/errno.h>
11 
12 #include "rocker_hw.h"
13 #include "rocker_tlv.h"
14 
15 void rocker_tlv_parse(const struct rocker_tlv **tb, int maxtype,
16 		      const char *buf, int buf_len)
17 {
18 	const struct rocker_tlv *tlv;
19 	const struct rocker_tlv *head = (const struct rocker_tlv *) buf;
20 	int rem;
21 
22 	memset(tb, 0, sizeof(struct rocker_tlv *) * (maxtype + 1));
23 
24 	rocker_tlv_for_each(tlv, head, buf_len, rem) {
25 		u32 type = rocker_tlv_type(tlv);
26 
27 		if (type > 0 && type <= maxtype)
28 			tb[type] = tlv;
29 	}
30 }
31 
32 int rocker_tlv_put(struct rocker_desc_info *desc_info,
33 		   int attrtype, int attrlen, const void *data)
34 {
35 	int tail_room = desc_info->data_size - desc_info->tlv_size;
36 	int total_size = rocker_tlv_total_size(attrlen);
37 	struct rocker_tlv *tlv;
38 
39 	if (unlikely(tail_room < total_size))
40 		return -EMSGSIZE;
41 
42 	tlv = rocker_tlv_start(desc_info);
43 	desc_info->tlv_size += total_size;
44 	tlv->type = attrtype;
45 	tlv->len = rocker_tlv_attr_size(attrlen);
46 	memcpy(rocker_tlv_data(tlv), data, attrlen);
47 	memset((char *) tlv + tlv->len, 0, rocker_tlv_padlen(attrlen));
48 	return 0;
49 }
50