1 /*
2  * drivers/net/ethernet/rocker/rocker_tlv.c - Rocker switch device driver
3  * Copyright (c) 2014-2016 Jiri Pirko <jiri@mellanox.com>
4  * Copyright (c) 2014 Scott Feldman <sfeldma@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11 
12 #include <linux/types.h>
13 #include <linux/string.h>
14 #include <linux/errno.h>
15 
16 #include "rocker_hw.h"
17 #include "rocker_tlv.h"
18 
19 void rocker_tlv_parse(const struct rocker_tlv **tb, int maxtype,
20 		      const char *buf, int buf_len)
21 {
22 	const struct rocker_tlv *tlv;
23 	const struct rocker_tlv *head = (const struct rocker_tlv *) buf;
24 	int rem;
25 
26 	memset(tb, 0, sizeof(struct rocker_tlv *) * (maxtype + 1));
27 
28 	rocker_tlv_for_each(tlv, head, buf_len, rem) {
29 		u32 type = rocker_tlv_type(tlv);
30 
31 		if (type > 0 && type <= maxtype)
32 			tb[type] = tlv;
33 	}
34 }
35 
36 int rocker_tlv_put(struct rocker_desc_info *desc_info,
37 		   int attrtype, int attrlen, const void *data)
38 {
39 	int tail_room = desc_info->data_size - desc_info->tlv_size;
40 	int total_size = rocker_tlv_total_size(attrlen);
41 	struct rocker_tlv *tlv;
42 
43 	if (unlikely(tail_room < total_size))
44 		return -EMSGSIZE;
45 
46 	tlv = rocker_tlv_start(desc_info);
47 	desc_info->tlv_size += total_size;
48 	tlv->type = attrtype;
49 	tlv->len = rocker_tlv_attr_size(attrlen);
50 	memcpy(rocker_tlv_data(tlv), data, attrlen);
51 	memset((char *) tlv + tlv->len, 0, rocker_tlv_padlen(attrlen));
52 	return 0;
53 }
54