Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / net / ethernet / cisco / enic / vnic_vic.c
blob66b57783533897e6399ec12505441004646b8ebc
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright 2010 Cisco Systems, Inc. All rights reserved.
4 #include <linux/kernel.h>
5 #include <linux/errno.h>
6 #include <linux/types.h>
7 #include <linux/slab.h>
9 #include "vnic_vic.h"
11 struct vic_provinfo *vic_provinfo_alloc(gfp_t flags, const u8 *oui,
12 const u8 type)
14 struct vic_provinfo *vp;
16 if (!oui)
17 return NULL;
19 vp = kzalloc(VIC_PROVINFO_MAX_DATA, flags);
20 if (!vp)
21 return NULL;
23 memcpy(vp->oui, oui, sizeof(vp->oui));
24 vp->type = type;
25 vp->length = htonl(sizeof(vp->num_tlvs));
27 return vp;
30 void vic_provinfo_free(struct vic_provinfo *vp)
32 kfree(vp);
35 int vic_provinfo_add_tlv(struct vic_provinfo *vp, u16 type, u16 length,
36 const void *value)
38 struct vic_provinfo_tlv *tlv;
40 if (!vp || !value)
41 return -EINVAL;
43 if (ntohl(vp->length) + offsetof(struct vic_provinfo_tlv, value) +
44 length > VIC_PROVINFO_MAX_TLV_DATA)
45 return -ENOMEM;
47 tlv = (struct vic_provinfo_tlv *)((u8 *)vp->tlv +
48 ntohl(vp->length) - sizeof(vp->num_tlvs));
50 tlv->type = htons(type);
51 tlv->length = htons(length);
52 unsafe_memcpy(tlv->value, value, length,
53 /* Flexible array of flexible arrays */);
55 vp->num_tlvs = htonl(ntohl(vp->num_tlvs) + 1);
56 vp->length = htonl(ntohl(vp->length) +
57 offsetof(struct vic_provinfo_tlv, value) + length);
59 return 0;
62 size_t vic_provinfo_size(struct vic_provinfo *vp)
64 return vp ? ntohl(vp->length) + sizeof(*vp) - sizeof(vp->num_tlvs) : 0;