xref: /openbmc/linux/net/ethtool/bitset.c (revision 52451502)
110b518d4SMichal Kubecek // SPDX-License-Identifier: GPL-2.0-only
210b518d4SMichal Kubecek 
310b518d4SMichal Kubecek #include <linux/ethtool_netlink.h>
410b518d4SMichal Kubecek #include <linux/bitmap.h>
510b518d4SMichal Kubecek #include "netlink.h"
610b518d4SMichal Kubecek #include "bitset.h"
710b518d4SMichal Kubecek 
810b518d4SMichal Kubecek /* Some bitmaps are internally represented as an array of unsigned long, some
910b518d4SMichal Kubecek  * as an array of u32 (some even as single u32 for now). To avoid the need of
1010b518d4SMichal Kubecek  * wrappers on caller side, we provide two set of functions: those with "32"
1110b518d4SMichal Kubecek  * suffix in their names expect u32 based bitmaps, those without it expect
1210b518d4SMichal Kubecek  * unsigned long bitmaps.
1310b518d4SMichal Kubecek  */
1410b518d4SMichal Kubecek 
ethnl_lower_bits(unsigned int n)1510b518d4SMichal Kubecek static u32 ethnl_lower_bits(unsigned int n)
1610b518d4SMichal Kubecek {
1710b518d4SMichal Kubecek 	return ~(u32)0 >> (32 - n % 32);
1810b518d4SMichal Kubecek }
1910b518d4SMichal Kubecek 
ethnl_upper_bits(unsigned int n)2010b518d4SMichal Kubecek static u32 ethnl_upper_bits(unsigned int n)
2110b518d4SMichal Kubecek {
2210b518d4SMichal Kubecek 	return ~(u32)0 << (n % 32);
2310b518d4SMichal Kubecek }
2410b518d4SMichal Kubecek 
2510b518d4SMichal Kubecek /**
2610b518d4SMichal Kubecek  * ethnl_bitmap32_clear() - Clear u32 based bitmap
2710b518d4SMichal Kubecek  * @dst:   bitmap to clear
2810b518d4SMichal Kubecek  * @start: beginning of the interval
2910b518d4SMichal Kubecek  * @end:   end of the interval
3010b518d4SMichal Kubecek  * @mod:   set if bitmap was modified
3110b518d4SMichal Kubecek  *
3210b518d4SMichal Kubecek  * Clear @nbits bits of a bitmap with indices @start <= i < @end
3310b518d4SMichal Kubecek  */
ethnl_bitmap32_clear(u32 * dst,unsigned int start,unsigned int end,bool * mod)3410b518d4SMichal Kubecek static void ethnl_bitmap32_clear(u32 *dst, unsigned int start, unsigned int end,
3510b518d4SMichal Kubecek 				 bool *mod)
3610b518d4SMichal Kubecek {
3710b518d4SMichal Kubecek 	unsigned int start_word = start / 32;
3810b518d4SMichal Kubecek 	unsigned int end_word = end / 32;
3910b518d4SMichal Kubecek 	unsigned int i;
4010b518d4SMichal Kubecek 	u32 mask;
4110b518d4SMichal Kubecek 
4210b518d4SMichal Kubecek 	if (end <= start)
4310b518d4SMichal Kubecek 		return;
4410b518d4SMichal Kubecek 
4510b518d4SMichal Kubecek 	if (start % 32) {
4610b518d4SMichal Kubecek 		mask = ethnl_upper_bits(start);
4710b518d4SMichal Kubecek 		if (end_word == start_word) {
4810b518d4SMichal Kubecek 			mask &= ethnl_lower_bits(end);
4910b518d4SMichal Kubecek 			if (dst[start_word] & mask) {
5010b518d4SMichal Kubecek 				dst[start_word] &= ~mask;
5110b518d4SMichal Kubecek 				*mod = true;
5210b518d4SMichal Kubecek 			}
5310b518d4SMichal Kubecek 			return;
5410b518d4SMichal Kubecek 		}
5510b518d4SMichal Kubecek 		if (dst[start_word] & mask) {
5610b518d4SMichal Kubecek 			dst[start_word] &= ~mask;
5710b518d4SMichal Kubecek 			*mod = true;
5810b518d4SMichal Kubecek 		}
5910b518d4SMichal Kubecek 		start_word++;
6010b518d4SMichal Kubecek 	}
6110b518d4SMichal Kubecek 
6210b518d4SMichal Kubecek 	for (i = start_word; i < end_word; i++) {
6310b518d4SMichal Kubecek 		if (dst[i]) {
6410b518d4SMichal Kubecek 			dst[i] = 0;
6510b518d4SMichal Kubecek 			*mod = true;
6610b518d4SMichal Kubecek 		}
6710b518d4SMichal Kubecek 	}
6810b518d4SMichal Kubecek 	if (end % 32) {
6910b518d4SMichal Kubecek 		mask = ethnl_lower_bits(end);
7010b518d4SMichal Kubecek 		if (dst[end_word] & mask) {
7110b518d4SMichal Kubecek 			dst[end_word] &= ~mask;
7210b518d4SMichal Kubecek 			*mod = true;
7310b518d4SMichal Kubecek 		}
7410b518d4SMichal Kubecek 	}
7510b518d4SMichal Kubecek }
7610b518d4SMichal Kubecek 
7710b518d4SMichal Kubecek /**
7810b518d4SMichal Kubecek  * ethnl_bitmap32_not_zero() - Check if any bit is set in an interval
7910b518d4SMichal Kubecek  * @map:   bitmap to test
8010b518d4SMichal Kubecek  * @start: beginning of the interval
8110b518d4SMichal Kubecek  * @end:   end of the interval
8210b518d4SMichal Kubecek  *
8310b518d4SMichal Kubecek  * Return: true if there is non-zero bit with  index @start <= i < @end,
8410b518d4SMichal Kubecek  *         false if the whole interval is zero
8510b518d4SMichal Kubecek  */
ethnl_bitmap32_not_zero(const u32 * map,unsigned int start,unsigned int end)8610b518d4SMichal Kubecek static bool ethnl_bitmap32_not_zero(const u32 *map, unsigned int start,
8710b518d4SMichal Kubecek 				    unsigned int end)
8810b518d4SMichal Kubecek {
8910b518d4SMichal Kubecek 	unsigned int start_word = start / 32;
9010b518d4SMichal Kubecek 	unsigned int end_word = end / 32;
9110b518d4SMichal Kubecek 	u32 mask;
9210b518d4SMichal Kubecek 
9310b518d4SMichal Kubecek 	if (end <= start)
9410b518d4SMichal Kubecek 		return true;
9510b518d4SMichal Kubecek 
9610b518d4SMichal Kubecek 	if (start % 32) {
9710b518d4SMichal Kubecek 		mask = ethnl_upper_bits(start);
9810b518d4SMichal Kubecek 		if (end_word == start_word) {
9910b518d4SMichal Kubecek 			mask &= ethnl_lower_bits(end);
10010b518d4SMichal Kubecek 			return map[start_word] & mask;
10110b518d4SMichal Kubecek 		}
10210b518d4SMichal Kubecek 		if (map[start_word] & mask)
10310b518d4SMichal Kubecek 			return true;
10410b518d4SMichal Kubecek 		start_word++;
10510b518d4SMichal Kubecek 	}
10610b518d4SMichal Kubecek 
10710b518d4SMichal Kubecek 	if (!memchr_inv(map + start_word, '\0',
10810b518d4SMichal Kubecek 			(end_word - start_word) * sizeof(u32)))
10910b518d4SMichal Kubecek 		return true;
11010b518d4SMichal Kubecek 	if (end % 32 == 0)
11110b518d4SMichal Kubecek 		return true;
11210b518d4SMichal Kubecek 	return map[end_word] & ethnl_lower_bits(end);
11310b518d4SMichal Kubecek }
11410b518d4SMichal Kubecek 
11510b518d4SMichal Kubecek /**
11610b518d4SMichal Kubecek  * ethnl_bitmap32_update() - Modify u32 based bitmap according to value/mask
11710b518d4SMichal Kubecek  *			     pair
11810b518d4SMichal Kubecek  * @dst:   bitmap to update
11910b518d4SMichal Kubecek  * @nbits: bit size of the bitmap
12010b518d4SMichal Kubecek  * @value: values to set
12110b518d4SMichal Kubecek  * @mask:  mask of bits to set
12210b518d4SMichal Kubecek  * @mod:   set to true if bitmap is modified, preserve if not
12310b518d4SMichal Kubecek  *
12410b518d4SMichal Kubecek  * Set bits in @dst bitmap which are set in @mask to values from @value, leave
12510b518d4SMichal Kubecek  * the rest untouched. If destination bitmap was modified, set @mod to true,
12610b518d4SMichal Kubecek  * leave as it is if not.
12710b518d4SMichal Kubecek  */
ethnl_bitmap32_update(u32 * dst,unsigned int nbits,const u32 * value,const u32 * mask,bool * mod)12810b518d4SMichal Kubecek static void ethnl_bitmap32_update(u32 *dst, unsigned int nbits,
12910b518d4SMichal Kubecek 				  const u32 *value, const u32 *mask, bool *mod)
13010b518d4SMichal Kubecek {
13110b518d4SMichal Kubecek 	while (nbits > 0) {
13210b518d4SMichal Kubecek 		u32 real_mask = mask ? *mask : ~(u32)0;
13310b518d4SMichal Kubecek 		u32 new_value;
13410b518d4SMichal Kubecek 
13510b518d4SMichal Kubecek 		if (nbits < 32)
13610b518d4SMichal Kubecek 			real_mask &= ethnl_lower_bits(nbits);
13710b518d4SMichal Kubecek 		new_value = (*dst & ~real_mask) | (*value & real_mask);
13810b518d4SMichal Kubecek 		if (new_value != *dst) {
13910b518d4SMichal Kubecek 			*dst = new_value;
14010b518d4SMichal Kubecek 			*mod = true;
14110b518d4SMichal Kubecek 		}
14210b518d4SMichal Kubecek 
14310b518d4SMichal Kubecek 		if (nbits <= 32)
14410b518d4SMichal Kubecek 			break;
14510b518d4SMichal Kubecek 		dst++;
14610b518d4SMichal Kubecek 		nbits -= 32;
14710b518d4SMichal Kubecek 		value++;
14810b518d4SMichal Kubecek 		if (mask)
14910b518d4SMichal Kubecek 			mask++;
15010b518d4SMichal Kubecek 	}
15110b518d4SMichal Kubecek }
15210b518d4SMichal Kubecek 
ethnl_bitmap32_test_bit(const u32 * map,unsigned int index)15310b518d4SMichal Kubecek static bool ethnl_bitmap32_test_bit(const u32 *map, unsigned int index)
15410b518d4SMichal Kubecek {
15510b518d4SMichal Kubecek 	return map[index / 32] & (1U << (index % 32));
15610b518d4SMichal Kubecek }
15710b518d4SMichal Kubecek 
15810b518d4SMichal Kubecek /**
15910b518d4SMichal Kubecek  * ethnl_bitset32_size() - Calculate size of bitset nested attribute
16010b518d4SMichal Kubecek  * @val:     value bitmap (u32 based)
16110b518d4SMichal Kubecek  * @mask:    mask bitmap (u32 based, optional)
16210b518d4SMichal Kubecek  * @nbits:   bit length of the bitset
16310b518d4SMichal Kubecek  * @names:   array of bit names (optional)
16410b518d4SMichal Kubecek  * @compact: assume compact format for output
16510b518d4SMichal Kubecek  *
16610b518d4SMichal Kubecek  * Estimate length of netlink attribute composed by a later call to
16710b518d4SMichal Kubecek  * ethnl_put_bitset32() call with the same arguments.
16810b518d4SMichal Kubecek  *
16910b518d4SMichal Kubecek  * Return: negative error code or attribute length estimate
17010b518d4SMichal Kubecek  */
ethnl_bitset32_size(const u32 * val,const u32 * mask,unsigned int nbits,ethnl_string_array_t names,bool compact)17110b518d4SMichal Kubecek int ethnl_bitset32_size(const u32 *val, const u32 *mask, unsigned int nbits,
17210b518d4SMichal Kubecek 			ethnl_string_array_t names, bool compact)
17310b518d4SMichal Kubecek {
17410b518d4SMichal Kubecek 	unsigned int len = 0;
17510b518d4SMichal Kubecek 
17610b518d4SMichal Kubecek 	/* list flag */
17710b518d4SMichal Kubecek 	if (!mask)
17810b518d4SMichal Kubecek 		len += nla_total_size(sizeof(u32));
17910b518d4SMichal Kubecek 	/* size */
18010b518d4SMichal Kubecek 	len += nla_total_size(sizeof(u32));
18110b518d4SMichal Kubecek 
18210b518d4SMichal Kubecek 	if (compact) {
18310b518d4SMichal Kubecek 		unsigned int nwords = DIV_ROUND_UP(nbits, 32);
18410b518d4SMichal Kubecek 
18510b518d4SMichal Kubecek 		/* value, mask */
18610b518d4SMichal Kubecek 		len += (mask ? 2 : 1) * nla_total_size(nwords * sizeof(u32));
18710b518d4SMichal Kubecek 	} else {
18810b518d4SMichal Kubecek 		unsigned int bits_len = 0;
18910b518d4SMichal Kubecek 		unsigned int bit_len, i;
19010b518d4SMichal Kubecek 
19110b518d4SMichal Kubecek 		for (i = 0; i < nbits; i++) {
19210b518d4SMichal Kubecek 			const char *name = names ? names[i] : NULL;
19310b518d4SMichal Kubecek 
19410b518d4SMichal Kubecek 			if (!ethnl_bitmap32_test_bit(mask ?: val, i))
19510b518d4SMichal Kubecek 				continue;
19610b518d4SMichal Kubecek 			/* index */
19710b518d4SMichal Kubecek 			bit_len = nla_total_size(sizeof(u32));
19810b518d4SMichal Kubecek 			/* name */
19910b518d4SMichal Kubecek 			if (name)
20010b518d4SMichal Kubecek 				bit_len += ethnl_strz_size(name);
20110b518d4SMichal Kubecek 			/* value */
20210b518d4SMichal Kubecek 			if (mask && ethnl_bitmap32_test_bit(val, i))
20310b518d4SMichal Kubecek 				bit_len += nla_total_size(0);
20410b518d4SMichal Kubecek 
20510b518d4SMichal Kubecek 			/* bit nest */
20610b518d4SMichal Kubecek 			bits_len += nla_total_size(bit_len);
20710b518d4SMichal Kubecek 		}
20810b518d4SMichal Kubecek 		/* bits nest */
20910b518d4SMichal Kubecek 		len += nla_total_size(bits_len);
21010b518d4SMichal Kubecek 	}
21110b518d4SMichal Kubecek 
21210b518d4SMichal Kubecek 	/* outermost nest */
21310b518d4SMichal Kubecek 	return nla_total_size(len);
21410b518d4SMichal Kubecek }
21510b518d4SMichal Kubecek 
21610b518d4SMichal Kubecek /**
21710b518d4SMichal Kubecek  * ethnl_put_bitset32() - Put a bitset nest into a message
21810b518d4SMichal Kubecek  * @skb:      skb with the message
21910b518d4SMichal Kubecek  * @attrtype: attribute type for the bitset nest
22010b518d4SMichal Kubecek  * @val:      value bitmap (u32 based)
22110b518d4SMichal Kubecek  * @mask:     mask bitmap (u32 based, optional)
22210b518d4SMichal Kubecek  * @nbits:    bit length of the bitset
22310b518d4SMichal Kubecek  * @names:    array of bit names (optional)
22410b518d4SMichal Kubecek  * @compact:  use compact format for the output
22510b518d4SMichal Kubecek  *
22610b518d4SMichal Kubecek  * Compose a nested attribute representing a bitset. If @mask is null, simple
22710b518d4SMichal Kubecek  * bitmap (bit list) is created, if @mask is provided, represent a value/mask
22810b518d4SMichal Kubecek  * pair. Bit names are only used in verbose mode and when provided by calller.
22910b518d4SMichal Kubecek  *
23010b518d4SMichal Kubecek  * Return: 0 on success, negative error value on error
23110b518d4SMichal Kubecek  */
ethnl_put_bitset32(struct sk_buff * skb,int attrtype,const u32 * val,const u32 * mask,unsigned int nbits,ethnl_string_array_t names,bool compact)23210b518d4SMichal Kubecek int ethnl_put_bitset32(struct sk_buff *skb, int attrtype, const u32 *val,
23310b518d4SMichal Kubecek 		       const u32 *mask, unsigned int nbits,
23410b518d4SMichal Kubecek 		       ethnl_string_array_t names, bool compact)
23510b518d4SMichal Kubecek {
23610b518d4SMichal Kubecek 	struct nlattr *nest;
23710b518d4SMichal Kubecek 	struct nlattr *attr;
23810b518d4SMichal Kubecek 
23910b518d4SMichal Kubecek 	nest = nla_nest_start(skb, attrtype);
24010b518d4SMichal Kubecek 	if (!nest)
24110b518d4SMichal Kubecek 		return -EMSGSIZE;
24210b518d4SMichal Kubecek 
24310b518d4SMichal Kubecek 	if (!mask && nla_put_flag(skb, ETHTOOL_A_BITSET_NOMASK))
24410b518d4SMichal Kubecek 		goto nla_put_failure;
24510b518d4SMichal Kubecek 	if (nla_put_u32(skb, ETHTOOL_A_BITSET_SIZE, nbits))
24610b518d4SMichal Kubecek 		goto nla_put_failure;
24710b518d4SMichal Kubecek 	if (compact) {
24810b518d4SMichal Kubecek 		unsigned int nwords = DIV_ROUND_UP(nbits, 32);
24910b518d4SMichal Kubecek 		unsigned int nbytes = nwords * sizeof(u32);
25010b518d4SMichal Kubecek 		u32 *dst;
25110b518d4SMichal Kubecek 
25210b518d4SMichal Kubecek 		attr = nla_reserve(skb, ETHTOOL_A_BITSET_VALUE, nbytes);
25310b518d4SMichal Kubecek 		if (!attr)
25410b518d4SMichal Kubecek 			goto nla_put_failure;
25510b518d4SMichal Kubecek 		dst = nla_data(attr);
25610b518d4SMichal Kubecek 		memcpy(dst, val, nbytes);
25710b518d4SMichal Kubecek 		if (nbits % 32)
25810b518d4SMichal Kubecek 			dst[nwords - 1] &= ethnl_lower_bits(nbits);
25910b518d4SMichal Kubecek 
26010b518d4SMichal Kubecek 		if (mask) {
26110b518d4SMichal Kubecek 			attr = nla_reserve(skb, ETHTOOL_A_BITSET_MASK, nbytes);
26210b518d4SMichal Kubecek 			if (!attr)
26310b518d4SMichal Kubecek 				goto nla_put_failure;
26410b518d4SMichal Kubecek 			dst = nla_data(attr);
26510b518d4SMichal Kubecek 			memcpy(dst, mask, nbytes);
26610b518d4SMichal Kubecek 			if (nbits % 32)
26710b518d4SMichal Kubecek 				dst[nwords - 1] &= ethnl_lower_bits(nbits);
26810b518d4SMichal Kubecek 		}
26910b518d4SMichal Kubecek 	} else {
27010b518d4SMichal Kubecek 		struct nlattr *bits;
27110b518d4SMichal Kubecek 		unsigned int i;
27210b518d4SMichal Kubecek 
27310b518d4SMichal Kubecek 		bits = nla_nest_start(skb, ETHTOOL_A_BITSET_BITS);
27410b518d4SMichal Kubecek 		if (!bits)
27510b518d4SMichal Kubecek 			goto nla_put_failure;
27610b518d4SMichal Kubecek 		for (i = 0; i < nbits; i++) {
27710b518d4SMichal Kubecek 			const char *name = names ? names[i] : NULL;
27810b518d4SMichal Kubecek 
27910b518d4SMichal Kubecek 			if (!ethnl_bitmap32_test_bit(mask ?: val, i))
28010b518d4SMichal Kubecek 				continue;
28110b518d4SMichal Kubecek 			attr = nla_nest_start(skb, ETHTOOL_A_BITSET_BITS_BIT);
28210b518d4SMichal Kubecek 			if (!attr)
28310b518d4SMichal Kubecek 				goto nla_put_failure;
28410b518d4SMichal Kubecek 			if (nla_put_u32(skb, ETHTOOL_A_BITSET_BIT_INDEX, i))
28510b518d4SMichal Kubecek 				goto nla_put_failure;
28610b518d4SMichal Kubecek 			if (name &&
28710b518d4SMichal Kubecek 			    ethnl_put_strz(skb, ETHTOOL_A_BITSET_BIT_NAME, name))
28810b518d4SMichal Kubecek 				goto nla_put_failure;
28910b518d4SMichal Kubecek 			if (mask && ethnl_bitmap32_test_bit(val, i) &&
29010b518d4SMichal Kubecek 			    nla_put_flag(skb, ETHTOOL_A_BITSET_BIT_VALUE))
29110b518d4SMichal Kubecek 				goto nla_put_failure;
29210b518d4SMichal Kubecek 			nla_nest_end(skb, attr);
29310b518d4SMichal Kubecek 		}
29410b518d4SMichal Kubecek 		nla_nest_end(skb, bits);
29510b518d4SMichal Kubecek 	}
29610b518d4SMichal Kubecek 
29710b518d4SMichal Kubecek 	nla_nest_end(skb, nest);
29810b518d4SMichal Kubecek 	return 0;
29910b518d4SMichal Kubecek 
30010b518d4SMichal Kubecek nla_put_failure:
30110b518d4SMichal Kubecek 	nla_nest_cancel(skb, nest);
30210b518d4SMichal Kubecek 	return -EMSGSIZE;
30310b518d4SMichal Kubecek }
30410b518d4SMichal Kubecek 
305ff419afaSJakub Kicinski static const struct nla_policy bitset_policy[] = {
30610b518d4SMichal Kubecek 	[ETHTOOL_A_BITSET_NOMASK]	= { .type = NLA_FLAG },
307e34f1753SMichal Kubecek 	[ETHTOOL_A_BITSET_SIZE]		= NLA_POLICY_MAX(NLA_U32,
308e34f1753SMichal Kubecek 							 ETHNL_MAX_BITSET_SIZE),
30910b518d4SMichal Kubecek 	[ETHTOOL_A_BITSET_BITS]		= { .type = NLA_NESTED },
31010b518d4SMichal Kubecek 	[ETHTOOL_A_BITSET_VALUE]	= { .type = NLA_BINARY },
31110b518d4SMichal Kubecek 	[ETHTOOL_A_BITSET_MASK]		= { .type = NLA_BINARY },
31210b518d4SMichal Kubecek };
31310b518d4SMichal Kubecek 
314ff419afaSJakub Kicinski static const struct nla_policy bit_policy[] = {
31510b518d4SMichal Kubecek 	[ETHTOOL_A_BITSET_BIT_INDEX]	= { .type = NLA_U32 },
31610b518d4SMichal Kubecek 	[ETHTOOL_A_BITSET_BIT_NAME]	= { .type = NLA_NUL_STRING },
31710b518d4SMichal Kubecek 	[ETHTOOL_A_BITSET_BIT_VALUE]	= { .type = NLA_FLAG },
31810b518d4SMichal Kubecek };
31910b518d4SMichal Kubecek 
32010b518d4SMichal Kubecek /**
32110b518d4SMichal Kubecek  * ethnl_bitset_is_compact() - check if bitset attribute represents a compact
32210b518d4SMichal Kubecek  *			       bitset
32310b518d4SMichal Kubecek  * @bitset:  nested attribute representing a bitset
32410b518d4SMichal Kubecek  * @compact: pointer for return value
32510b518d4SMichal Kubecek  *
32610b518d4SMichal Kubecek  * Return: 0 on success, negative error code on failure
32710b518d4SMichal Kubecek  */
ethnl_bitset_is_compact(const struct nlattr * bitset,bool * compact)32810b518d4SMichal Kubecek int ethnl_bitset_is_compact(const struct nlattr *bitset, bool *compact)
32910b518d4SMichal Kubecek {
330ff419afaSJakub Kicinski 	struct nlattr *tb[ARRAY_SIZE(bitset_policy)];
33110b518d4SMichal Kubecek 	int ret;
33210b518d4SMichal Kubecek 
333ff419afaSJakub Kicinski 	ret = nla_parse_nested(tb, ARRAY_SIZE(bitset_policy) - 1, bitset,
33410b518d4SMichal Kubecek 			       bitset_policy, NULL);
33510b518d4SMichal Kubecek 	if (ret < 0)
33610b518d4SMichal Kubecek 		return ret;
33710b518d4SMichal Kubecek 
33810b518d4SMichal Kubecek 	if (tb[ETHTOOL_A_BITSET_BITS]) {
33910b518d4SMichal Kubecek 		if (tb[ETHTOOL_A_BITSET_VALUE] || tb[ETHTOOL_A_BITSET_MASK])
34010b518d4SMichal Kubecek 			return -EINVAL;
34110b518d4SMichal Kubecek 		*compact = false;
34210b518d4SMichal Kubecek 		return 0;
34310b518d4SMichal Kubecek 	}
34410b518d4SMichal Kubecek 	if (!tb[ETHTOOL_A_BITSET_SIZE] || !tb[ETHTOOL_A_BITSET_VALUE])
34510b518d4SMichal Kubecek 		return -EINVAL;
34610b518d4SMichal Kubecek 
34710b518d4SMichal Kubecek 	*compact = true;
34810b518d4SMichal Kubecek 	return 0;
34910b518d4SMichal Kubecek }
35010b518d4SMichal Kubecek 
35110b518d4SMichal Kubecek /**
35210b518d4SMichal Kubecek  * ethnl_name_to_idx() - look up string index for a name
35310b518d4SMichal Kubecek  * @names:   array of ETH_GSTRING_LEN sized strings
35410b518d4SMichal Kubecek  * @n_names: number of strings in the array
35510b518d4SMichal Kubecek  * @name:    name to look up
35610b518d4SMichal Kubecek  *
35710b518d4SMichal Kubecek  * Return: index of the string if found, -ENOENT if not found
35810b518d4SMichal Kubecek  */
ethnl_name_to_idx(ethnl_string_array_t names,unsigned int n_names,const char * name)35910b518d4SMichal Kubecek static int ethnl_name_to_idx(ethnl_string_array_t names, unsigned int n_names,
36010b518d4SMichal Kubecek 			     const char *name)
36110b518d4SMichal Kubecek {
36210b518d4SMichal Kubecek 	unsigned int i;
36310b518d4SMichal Kubecek 
36410b518d4SMichal Kubecek 	if (!names)
36510b518d4SMichal Kubecek 		return -ENOENT;
36610b518d4SMichal Kubecek 
36710b518d4SMichal Kubecek 	for (i = 0; i < n_names; i++) {
36810b518d4SMichal Kubecek 		/* names[i] may not be null terminated */
36910b518d4SMichal Kubecek 		if (!strncmp(names[i], name, ETH_GSTRING_LEN) &&
37010b518d4SMichal Kubecek 		    strlen(name) <= ETH_GSTRING_LEN)
37110b518d4SMichal Kubecek 			return i;
37210b518d4SMichal Kubecek 	}
37310b518d4SMichal Kubecek 
37410b518d4SMichal Kubecek 	return -ENOENT;
37510b518d4SMichal Kubecek }
37610b518d4SMichal Kubecek 
ethnl_parse_bit(unsigned int * index,bool * val,unsigned int nbits,const struct nlattr * bit_attr,bool no_mask,ethnl_string_array_t names,struct netlink_ext_ack * extack)37710b518d4SMichal Kubecek static int ethnl_parse_bit(unsigned int *index, bool *val, unsigned int nbits,
37810b518d4SMichal Kubecek 			   const struct nlattr *bit_attr, bool no_mask,
37910b518d4SMichal Kubecek 			   ethnl_string_array_t names,
38010b518d4SMichal Kubecek 			   struct netlink_ext_ack *extack)
38110b518d4SMichal Kubecek {
382ff419afaSJakub Kicinski 	struct nlattr *tb[ARRAY_SIZE(bit_policy)];
38310b518d4SMichal Kubecek 	int ret, idx;
38410b518d4SMichal Kubecek 
385ff419afaSJakub Kicinski 	ret = nla_parse_nested(tb, ARRAY_SIZE(bit_policy) - 1, bit_attr,
38610b518d4SMichal Kubecek 			       bit_policy, extack);
38710b518d4SMichal Kubecek 	if (ret < 0)
38810b518d4SMichal Kubecek 		return ret;
38910b518d4SMichal Kubecek 
39010b518d4SMichal Kubecek 	if (tb[ETHTOOL_A_BITSET_BIT_INDEX]) {
39110b518d4SMichal Kubecek 		const char *name;
39210b518d4SMichal Kubecek 
39310b518d4SMichal Kubecek 		idx = nla_get_u32(tb[ETHTOOL_A_BITSET_BIT_INDEX]);
39410b518d4SMichal Kubecek 		if (idx >= nbits) {
39510b518d4SMichal Kubecek 			NL_SET_ERR_MSG_ATTR(extack,
39610b518d4SMichal Kubecek 					    tb[ETHTOOL_A_BITSET_BIT_INDEX],
39710b518d4SMichal Kubecek 					    "bit index too high");
39810b518d4SMichal Kubecek 			return -EOPNOTSUPP;
39910b518d4SMichal Kubecek 		}
40010b518d4SMichal Kubecek 		name = names ? names[idx] : NULL;
40110b518d4SMichal Kubecek 		if (tb[ETHTOOL_A_BITSET_BIT_NAME] && name &&
40210b518d4SMichal Kubecek 		    strncmp(nla_data(tb[ETHTOOL_A_BITSET_BIT_NAME]), name,
40310b518d4SMichal Kubecek 			    nla_len(tb[ETHTOOL_A_BITSET_BIT_NAME]))) {
40410b518d4SMichal Kubecek 			NL_SET_ERR_MSG_ATTR(extack, bit_attr,
40510b518d4SMichal Kubecek 					    "bit index and name mismatch");
40610b518d4SMichal Kubecek 			return -EINVAL;
40710b518d4SMichal Kubecek 		}
40810b518d4SMichal Kubecek 	} else if (tb[ETHTOOL_A_BITSET_BIT_NAME]) {
40910b518d4SMichal Kubecek 		idx = ethnl_name_to_idx(names, nbits,
41010b518d4SMichal Kubecek 					nla_data(tb[ETHTOOL_A_BITSET_BIT_NAME]));
41110b518d4SMichal Kubecek 		if (idx < 0) {
41210b518d4SMichal Kubecek 			NL_SET_ERR_MSG_ATTR(extack,
41310b518d4SMichal Kubecek 					    tb[ETHTOOL_A_BITSET_BIT_NAME],
41410b518d4SMichal Kubecek 					    "bit name not found");
41510b518d4SMichal Kubecek 			return -EOPNOTSUPP;
41610b518d4SMichal Kubecek 		}
41710b518d4SMichal Kubecek 	} else {
41810b518d4SMichal Kubecek 		NL_SET_ERR_MSG_ATTR(extack, bit_attr,
41910b518d4SMichal Kubecek 				    "neither bit index nor name specified");
42010b518d4SMichal Kubecek 		return -EINVAL;
42110b518d4SMichal Kubecek 	}
42210b518d4SMichal Kubecek 
42310b518d4SMichal Kubecek 	*index = idx;
42410b518d4SMichal Kubecek 	*val = no_mask || tb[ETHTOOL_A_BITSET_BIT_VALUE];
42510b518d4SMichal Kubecek 	return 0;
42610b518d4SMichal Kubecek }
42710b518d4SMichal Kubecek 
42810b518d4SMichal Kubecek static int
ethnl_update_bitset32_verbose(u32 * bitmap,unsigned int nbits,const struct nlattr * attr,struct nlattr ** tb,ethnl_string_array_t names,struct netlink_ext_ack * extack,bool * mod)42910b518d4SMichal Kubecek ethnl_update_bitset32_verbose(u32 *bitmap, unsigned int nbits,
43010b518d4SMichal Kubecek 			      const struct nlattr *attr, struct nlattr **tb,
43110b518d4SMichal Kubecek 			      ethnl_string_array_t names,
43210b518d4SMichal Kubecek 			      struct netlink_ext_ack *extack, bool *mod)
43310b518d4SMichal Kubecek {
43410b518d4SMichal Kubecek 	struct nlattr *bit_attr;
43510b518d4SMichal Kubecek 	bool no_mask;
43610b518d4SMichal Kubecek 	int rem;
43710b518d4SMichal Kubecek 	int ret;
43810b518d4SMichal Kubecek 
43910b518d4SMichal Kubecek 	if (tb[ETHTOOL_A_BITSET_VALUE]) {
44010b518d4SMichal Kubecek 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_VALUE],
44110b518d4SMichal Kubecek 				    "value only allowed in compact bitset");
44210b518d4SMichal Kubecek 		return -EINVAL;
44310b518d4SMichal Kubecek 	}
44410b518d4SMichal Kubecek 	if (tb[ETHTOOL_A_BITSET_MASK]) {
44510b518d4SMichal Kubecek 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK],
44610b518d4SMichal Kubecek 				    "mask only allowed in compact bitset");
44710b518d4SMichal Kubecek 		return -EINVAL;
44810b518d4SMichal Kubecek 	}
44966991703SMichal Kubecek 
45010b518d4SMichal Kubecek 	no_mask = tb[ETHTOOL_A_BITSET_NOMASK];
451*52451502SKory Maincent 	if (no_mask)
452*52451502SKory Maincent 		ethnl_bitmap32_clear(bitmap, 0, nbits, mod);
45310b518d4SMichal Kubecek 
45410b518d4SMichal Kubecek 	nla_for_each_nested(bit_attr, tb[ETHTOOL_A_BITSET_BITS], rem) {
45510b518d4SMichal Kubecek 		bool old_val, new_val;
45610b518d4SMichal Kubecek 		unsigned int idx;
45710b518d4SMichal Kubecek 
45810b518d4SMichal Kubecek 		if (nla_type(bit_attr) != ETHTOOL_A_BITSET_BITS_BIT) {
45910b518d4SMichal Kubecek 			NL_SET_ERR_MSG_ATTR(extack, bit_attr,
46010b518d4SMichal Kubecek 					    "only ETHTOOL_A_BITSET_BITS_BIT allowed in ETHTOOL_A_BITSET_BITS");
461*52451502SKory Maincent 			return -EINVAL;
46210b518d4SMichal Kubecek 		}
46310b518d4SMichal Kubecek 		ret = ethnl_parse_bit(&idx, &new_val, nbits, bit_attr, no_mask,
46410b518d4SMichal Kubecek 				      names, extack);
46510b518d4SMichal Kubecek 		if (ret < 0)
466*52451502SKory Maincent 			return ret;
467*52451502SKory Maincent 		old_val = bitmap[idx / 32] & ((u32)1 << (idx % 32));
46810b518d4SMichal Kubecek 		if (new_val != old_val) {
46910b518d4SMichal Kubecek 			if (new_val)
47010b518d4SMichal Kubecek 				bitmap[idx / 32] |= ((u32)1 << (idx % 32));
47110b518d4SMichal Kubecek 			else
47210b518d4SMichal Kubecek 				bitmap[idx / 32] &= ~((u32)1 << (idx % 32));
47310b518d4SMichal Kubecek 			*mod = true;
47410b518d4SMichal Kubecek 		}
47510b518d4SMichal Kubecek 	}
47610b518d4SMichal Kubecek 
477*52451502SKory Maincent 	return 0;
47810b518d4SMichal Kubecek }
47910b518d4SMichal Kubecek 
ethnl_compact_sanity_checks(unsigned int nbits,const struct nlattr * nest,struct nlattr ** tb,struct netlink_ext_ack * extack)48010b518d4SMichal Kubecek static int ethnl_compact_sanity_checks(unsigned int nbits,
48110b518d4SMichal Kubecek 				       const struct nlattr *nest,
48210b518d4SMichal Kubecek 				       struct nlattr **tb,
48310b518d4SMichal Kubecek 				       struct netlink_ext_ack *extack)
48410b518d4SMichal Kubecek {
48510b518d4SMichal Kubecek 	bool no_mask = tb[ETHTOOL_A_BITSET_NOMASK];
48610b518d4SMichal Kubecek 	unsigned int attr_nbits, attr_nwords;
48710b518d4SMichal Kubecek 	const struct nlattr *test_attr;
48810b518d4SMichal Kubecek 
48910b518d4SMichal Kubecek 	if (no_mask && tb[ETHTOOL_A_BITSET_MASK]) {
49010b518d4SMichal Kubecek 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK],
49110b518d4SMichal Kubecek 				    "mask not allowed in list bitset");
49210b518d4SMichal Kubecek 		return -EINVAL;
49310b518d4SMichal Kubecek 	}
49410b518d4SMichal Kubecek 	if (!tb[ETHTOOL_A_BITSET_SIZE]) {
49510b518d4SMichal Kubecek 		NL_SET_ERR_MSG_ATTR(extack, nest,
49610b518d4SMichal Kubecek 				    "missing size in compact bitset");
49710b518d4SMichal Kubecek 		return -EINVAL;
49810b518d4SMichal Kubecek 	}
49910b518d4SMichal Kubecek 	if (!tb[ETHTOOL_A_BITSET_VALUE]) {
50010b518d4SMichal Kubecek 		NL_SET_ERR_MSG_ATTR(extack, nest,
50110b518d4SMichal Kubecek 				    "missing value in compact bitset");
50210b518d4SMichal Kubecek 		return -EINVAL;
50310b518d4SMichal Kubecek 	}
50410b518d4SMichal Kubecek 	if (!no_mask && !tb[ETHTOOL_A_BITSET_MASK]) {
50510b518d4SMichal Kubecek 		NL_SET_ERR_MSG_ATTR(extack, nest,
50610b518d4SMichal Kubecek 				    "missing mask in compact nonlist bitset");
50710b518d4SMichal Kubecek 		return -EINVAL;
50810b518d4SMichal Kubecek 	}
50910b518d4SMichal Kubecek 
51010b518d4SMichal Kubecek 	attr_nbits = nla_get_u32(tb[ETHTOOL_A_BITSET_SIZE]);
51110b518d4SMichal Kubecek 	attr_nwords = DIV_ROUND_UP(attr_nbits, 32);
51210b518d4SMichal Kubecek 	if (nla_len(tb[ETHTOOL_A_BITSET_VALUE]) != attr_nwords * sizeof(u32)) {
51310b518d4SMichal Kubecek 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_VALUE],
51410b518d4SMichal Kubecek 				    "bitset value length does not match size");
51510b518d4SMichal Kubecek 		return -EINVAL;
51610b518d4SMichal Kubecek 	}
51710b518d4SMichal Kubecek 	if (tb[ETHTOOL_A_BITSET_MASK] &&
51810b518d4SMichal Kubecek 	    nla_len(tb[ETHTOOL_A_BITSET_MASK]) != attr_nwords * sizeof(u32)) {
51910b518d4SMichal Kubecek 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK],
52010b518d4SMichal Kubecek 				    "bitset mask length does not match size");
52110b518d4SMichal Kubecek 		return -EINVAL;
52210b518d4SMichal Kubecek 	}
52310b518d4SMichal Kubecek 	if (attr_nbits <= nbits)
52410b518d4SMichal Kubecek 		return 0;
52510b518d4SMichal Kubecek 
52610b518d4SMichal Kubecek 	test_attr = no_mask ? tb[ETHTOOL_A_BITSET_VALUE] :
52710b518d4SMichal Kubecek 			      tb[ETHTOOL_A_BITSET_MASK];
52810b518d4SMichal Kubecek 	if (ethnl_bitmap32_not_zero(nla_data(test_attr), nbits, attr_nbits)) {
52910b518d4SMichal Kubecek 		NL_SET_ERR_MSG_ATTR(extack, test_attr,
53010b518d4SMichal Kubecek 				    "cannot modify bits past kernel bitset size");
53110b518d4SMichal Kubecek 		return -EINVAL;
53210b518d4SMichal Kubecek 	}
53310b518d4SMichal Kubecek 	return 0;
53410b518d4SMichal Kubecek }
53510b518d4SMichal Kubecek 
53610b518d4SMichal Kubecek /**
53710b518d4SMichal Kubecek  * ethnl_update_bitset32() - Apply a bitset nest to a u32 based bitmap
53810b518d4SMichal Kubecek  * @bitmap:  bitmap to update
53910b518d4SMichal Kubecek  * @nbits:   size of the updated bitmap in bits
54010b518d4SMichal Kubecek  * @attr:    nest attribute to parse and apply
54110b518d4SMichal Kubecek  * @names:   array of bit names; may be null for compact format
54210b518d4SMichal Kubecek  * @extack:  extack for error reporting
54310b518d4SMichal Kubecek  * @mod:     set this to true if bitmap is modified, leave as it is if not
54410b518d4SMichal Kubecek  *
54510b518d4SMichal Kubecek  * Apply bitset netsted attribute to a bitmap. If the attribute represents
54610b518d4SMichal Kubecek  * a bit list, @bitmap is set to its contents; otherwise, bits in mask are
54710b518d4SMichal Kubecek  * set to values from value. Bitmaps in the attribute may be longer than
54810b518d4SMichal Kubecek  * @nbits but the message must not request modifying any bits past @nbits.
54910b518d4SMichal Kubecek  *
55010b518d4SMichal Kubecek  * Return: negative error code on failure, 0 on success
55110b518d4SMichal Kubecek  */
ethnl_update_bitset32(u32 * bitmap,unsigned int nbits,const struct nlattr * attr,ethnl_string_array_t names,struct netlink_ext_ack * extack,bool * mod)55210b518d4SMichal Kubecek int ethnl_update_bitset32(u32 *bitmap, unsigned int nbits,
55310b518d4SMichal Kubecek 			  const struct nlattr *attr, ethnl_string_array_t names,
55410b518d4SMichal Kubecek 			  struct netlink_ext_ack *extack, bool *mod)
55510b518d4SMichal Kubecek {
556ff419afaSJakub Kicinski 	struct nlattr *tb[ARRAY_SIZE(bitset_policy)];
55710b518d4SMichal Kubecek 	unsigned int change_bits;
55810b518d4SMichal Kubecek 	bool no_mask;
55910b518d4SMichal Kubecek 	int ret;
56010b518d4SMichal Kubecek 
56110b518d4SMichal Kubecek 	if (!attr)
56210b518d4SMichal Kubecek 		return 0;
563ff419afaSJakub Kicinski 	ret = nla_parse_nested(tb, ARRAY_SIZE(bitset_policy) - 1, attr,
564ff419afaSJakub Kicinski 			       bitset_policy, extack);
56510b518d4SMichal Kubecek 	if (ret < 0)
56610b518d4SMichal Kubecek 		return ret;
56710b518d4SMichal Kubecek 
56810b518d4SMichal Kubecek 	if (tb[ETHTOOL_A_BITSET_BITS])
56910b518d4SMichal Kubecek 		return ethnl_update_bitset32_verbose(bitmap, nbits, attr, tb,
57010b518d4SMichal Kubecek 						     names, extack, mod);
57110b518d4SMichal Kubecek 	ret = ethnl_compact_sanity_checks(nbits, attr, tb, extack);
57210b518d4SMichal Kubecek 	if (ret < 0)
57310b518d4SMichal Kubecek 		return ret;
57410b518d4SMichal Kubecek 
57510b518d4SMichal Kubecek 	no_mask = tb[ETHTOOL_A_BITSET_NOMASK];
57610b518d4SMichal Kubecek 	change_bits = min_t(unsigned int,
57710b518d4SMichal Kubecek 			    nla_get_u32(tb[ETHTOOL_A_BITSET_SIZE]), nbits);
57810b518d4SMichal Kubecek 	ethnl_bitmap32_update(bitmap, change_bits,
57910b518d4SMichal Kubecek 			      nla_data(tb[ETHTOOL_A_BITSET_VALUE]),
58010b518d4SMichal Kubecek 			      no_mask ? NULL :
58110b518d4SMichal Kubecek 					nla_data(tb[ETHTOOL_A_BITSET_MASK]),
58210b518d4SMichal Kubecek 			      mod);
58310b518d4SMichal Kubecek 	if (no_mask && change_bits < nbits)
58410b518d4SMichal Kubecek 		ethnl_bitmap32_clear(bitmap, change_bits, nbits, mod);
58510b518d4SMichal Kubecek 
58610b518d4SMichal Kubecek 	return 0;
58710b518d4SMichal Kubecek }
58810b518d4SMichal Kubecek 
58988db6d1eSMichal Kubecek /**
59088db6d1eSMichal Kubecek  * ethnl_parse_bitset() - Compute effective value and mask from bitset nest
59188db6d1eSMichal Kubecek  * @val:     unsigned long based bitmap to put value into
59288db6d1eSMichal Kubecek  * @mask:    unsigned long based bitmap to put mask into
59388db6d1eSMichal Kubecek  * @nbits:   size of @val and @mask bitmaps
59488db6d1eSMichal Kubecek  * @attr:    nest attribute to parse and apply
59588db6d1eSMichal Kubecek  * @names:   array of bit names; may be null for compact format
59688db6d1eSMichal Kubecek  * @extack:  extack for error reporting
59788db6d1eSMichal Kubecek  *
59888db6d1eSMichal Kubecek  * Provide @nbits size long bitmaps for value and mask so that
59988db6d1eSMichal Kubecek  * x = (val & mask) | (x & ~mask) would modify any @nbits sized bitmap x
60088db6d1eSMichal Kubecek  * the same way ethnl_update_bitset() with the same bitset attribute would.
60188db6d1eSMichal Kubecek  *
60288db6d1eSMichal Kubecek  * Return:   negative error code on failure, 0 on success
60388db6d1eSMichal Kubecek  */
ethnl_parse_bitset(unsigned long * val,unsigned long * mask,unsigned int nbits,const struct nlattr * attr,ethnl_string_array_t names,struct netlink_ext_ack * extack)60488db6d1eSMichal Kubecek int ethnl_parse_bitset(unsigned long *val, unsigned long *mask,
60588db6d1eSMichal Kubecek 		       unsigned int nbits, const struct nlattr *attr,
60688db6d1eSMichal Kubecek 		       ethnl_string_array_t names,
60788db6d1eSMichal Kubecek 		       struct netlink_ext_ack *extack)
60888db6d1eSMichal Kubecek {
609ff419afaSJakub Kicinski 	struct nlattr *tb[ARRAY_SIZE(bitset_policy)];
61088db6d1eSMichal Kubecek 	const struct nlattr *bit_attr;
61188db6d1eSMichal Kubecek 	bool no_mask;
61288db6d1eSMichal Kubecek 	int rem;
61388db6d1eSMichal Kubecek 	int ret;
61488db6d1eSMichal Kubecek 
61588db6d1eSMichal Kubecek 	if (!attr)
61688db6d1eSMichal Kubecek 		return 0;
617ff419afaSJakub Kicinski 	ret = nla_parse_nested(tb, ARRAY_SIZE(bitset_policy) - 1, attr,
618ff419afaSJakub Kicinski 			       bitset_policy, extack);
61988db6d1eSMichal Kubecek 	if (ret < 0)
62088db6d1eSMichal Kubecek 		return ret;
62188db6d1eSMichal Kubecek 	no_mask = tb[ETHTOOL_A_BITSET_NOMASK];
62288db6d1eSMichal Kubecek 
62388db6d1eSMichal Kubecek 	if (!tb[ETHTOOL_A_BITSET_BITS]) {
62488db6d1eSMichal Kubecek 		unsigned int change_bits;
62588db6d1eSMichal Kubecek 
62688db6d1eSMichal Kubecek 		ret = ethnl_compact_sanity_checks(nbits, attr, tb, extack);
62788db6d1eSMichal Kubecek 		if (ret < 0)
62888db6d1eSMichal Kubecek 			return ret;
62988db6d1eSMichal Kubecek 
63088db6d1eSMichal Kubecek 		change_bits = nla_get_u32(tb[ETHTOOL_A_BITSET_SIZE]);
631a770bf51SMichal Kubecek 		if (change_bits > nbits)
632a770bf51SMichal Kubecek 			change_bits = nbits;
63388db6d1eSMichal Kubecek 		bitmap_from_arr32(val, nla_data(tb[ETHTOOL_A_BITSET_VALUE]),
63488db6d1eSMichal Kubecek 				  change_bits);
63588db6d1eSMichal Kubecek 		if (change_bits < nbits)
63688db6d1eSMichal Kubecek 			bitmap_clear(val, change_bits, nbits - change_bits);
63788db6d1eSMichal Kubecek 		if (no_mask) {
63888db6d1eSMichal Kubecek 			bitmap_fill(mask, nbits);
63988db6d1eSMichal Kubecek 		} else {
64088db6d1eSMichal Kubecek 			bitmap_from_arr32(mask,
64188db6d1eSMichal Kubecek 					  nla_data(tb[ETHTOOL_A_BITSET_MASK]),
64288db6d1eSMichal Kubecek 					  change_bits);
64388db6d1eSMichal Kubecek 			if (change_bits < nbits)
64488db6d1eSMichal Kubecek 				bitmap_clear(mask, change_bits,
64588db6d1eSMichal Kubecek 					     nbits - change_bits);
64688db6d1eSMichal Kubecek 		}
64788db6d1eSMichal Kubecek 
64888db6d1eSMichal Kubecek 		return 0;
64988db6d1eSMichal Kubecek 	}
65088db6d1eSMichal Kubecek 
65188db6d1eSMichal Kubecek 	if (tb[ETHTOOL_A_BITSET_VALUE]) {
65288db6d1eSMichal Kubecek 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_VALUE],
65388db6d1eSMichal Kubecek 				    "value only allowed in compact bitset");
65488db6d1eSMichal Kubecek 		return -EINVAL;
65588db6d1eSMichal Kubecek 	}
65688db6d1eSMichal Kubecek 	if (tb[ETHTOOL_A_BITSET_MASK]) {
65788db6d1eSMichal Kubecek 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK],
65888db6d1eSMichal Kubecek 				    "mask only allowed in compact bitset");
65988db6d1eSMichal Kubecek 		return -EINVAL;
66088db6d1eSMichal Kubecek 	}
66188db6d1eSMichal Kubecek 
66288db6d1eSMichal Kubecek 	bitmap_zero(val, nbits);
66388db6d1eSMichal Kubecek 	if (no_mask)
66488db6d1eSMichal Kubecek 		bitmap_fill(mask, nbits);
66588db6d1eSMichal Kubecek 	else
66688db6d1eSMichal Kubecek 		bitmap_zero(mask, nbits);
66788db6d1eSMichal Kubecek 
66888db6d1eSMichal Kubecek 	nla_for_each_nested(bit_attr, tb[ETHTOOL_A_BITSET_BITS], rem) {
66988db6d1eSMichal Kubecek 		unsigned int idx;
67088db6d1eSMichal Kubecek 		bool bit_val;
67188db6d1eSMichal Kubecek 
67288db6d1eSMichal Kubecek 		ret = ethnl_parse_bit(&idx, &bit_val, nbits, bit_attr, no_mask,
67388db6d1eSMichal Kubecek 				      names, extack);
67488db6d1eSMichal Kubecek 		if (ret < 0)
67588db6d1eSMichal Kubecek 			return ret;
67688db6d1eSMichal Kubecek 		if (bit_val)
67788db6d1eSMichal Kubecek 			__set_bit(idx, val);
67888db6d1eSMichal Kubecek 		if (!no_mask)
67988db6d1eSMichal Kubecek 			__set_bit(idx, mask);
68088db6d1eSMichal Kubecek 	}
68188db6d1eSMichal Kubecek 
68288db6d1eSMichal Kubecek 	return 0;
68388db6d1eSMichal Kubecek }
68488db6d1eSMichal Kubecek 
68510b518d4SMichal Kubecek #if BITS_PER_LONG == 64 && defined(__BIG_ENDIAN)
68610b518d4SMichal Kubecek 
68710b518d4SMichal Kubecek /* 64-bit big endian architectures are the only case when u32 based bitmaps
68810b518d4SMichal Kubecek  * and unsigned long based bitmaps have different memory layout so that we
68910b518d4SMichal Kubecek  * cannot simply cast the latter to the former and need actual wrappers
69010b518d4SMichal Kubecek  * converting the latter to the former.
69110b518d4SMichal Kubecek  *
69210b518d4SMichal Kubecek  * To reduce the number of slab allocations, the wrappers use fixed size local
69310b518d4SMichal Kubecek  * variables for bitmaps up to ETHNL_SMALL_BITMAP_BITS bits which is the
69410b518d4SMichal Kubecek  * majority of bitmaps used by ethtool.
69510b518d4SMichal Kubecek  */
69610b518d4SMichal Kubecek #define ETHNL_SMALL_BITMAP_BITS 128
69710b518d4SMichal Kubecek #define ETHNL_SMALL_BITMAP_WORDS DIV_ROUND_UP(ETHNL_SMALL_BITMAP_BITS, 32)
69810b518d4SMichal Kubecek 
ethnl_bitset_size(const unsigned long * val,const unsigned long * mask,unsigned int nbits,ethnl_string_array_t names,bool compact)69910b518d4SMichal Kubecek int ethnl_bitset_size(const unsigned long *val, const unsigned long *mask,
70010b518d4SMichal Kubecek 		      unsigned int nbits, ethnl_string_array_t names,
70110b518d4SMichal Kubecek 		      bool compact)
70210b518d4SMichal Kubecek {
70310b518d4SMichal Kubecek 	u32 small_mask32[ETHNL_SMALL_BITMAP_WORDS];
70410b518d4SMichal Kubecek 	u32 small_val32[ETHNL_SMALL_BITMAP_WORDS];
70510b518d4SMichal Kubecek 	u32 *mask32;
70610b518d4SMichal Kubecek 	u32 *val32;
70710b518d4SMichal Kubecek 	int ret;
70810b518d4SMichal Kubecek 
70910b518d4SMichal Kubecek 	if (nbits > ETHNL_SMALL_BITMAP_BITS) {
71010b518d4SMichal Kubecek 		unsigned int nwords = DIV_ROUND_UP(nbits, 32);
71110b518d4SMichal Kubecek 
71210b518d4SMichal Kubecek 		val32 = kmalloc_array(2 * nwords, sizeof(u32), GFP_KERNEL);
71310b518d4SMichal Kubecek 		if (!val32)
71410b518d4SMichal Kubecek 			return -ENOMEM;
71510b518d4SMichal Kubecek 		mask32 = val32 + nwords;
71610b518d4SMichal Kubecek 	} else {
71710b518d4SMichal Kubecek 		val32 = small_val32;
71810b518d4SMichal Kubecek 		mask32 = small_mask32;
71910b518d4SMichal Kubecek 	}
72010b518d4SMichal Kubecek 
72110b518d4SMichal Kubecek 	bitmap_to_arr32(val32, val, nbits);
72210b518d4SMichal Kubecek 	if (mask)
72310b518d4SMichal Kubecek 		bitmap_to_arr32(mask32, mask, nbits);
72410b518d4SMichal Kubecek 	else
72510b518d4SMichal Kubecek 		mask32 = NULL;
72610b518d4SMichal Kubecek 	ret = ethnl_bitset32_size(val32, mask32, nbits, names, compact);
72710b518d4SMichal Kubecek 
72810b518d4SMichal Kubecek 	if (nbits > ETHNL_SMALL_BITMAP_BITS)
72910b518d4SMichal Kubecek 		kfree(val32);
73010b518d4SMichal Kubecek 
73110b518d4SMichal Kubecek 	return ret;
73210b518d4SMichal Kubecek }
73310b518d4SMichal Kubecek 
ethnl_put_bitset(struct sk_buff * skb,int attrtype,const unsigned long * val,const unsigned long * mask,unsigned int nbits,ethnl_string_array_t names,bool compact)73410b518d4SMichal Kubecek int ethnl_put_bitset(struct sk_buff *skb, int attrtype,
73510b518d4SMichal Kubecek 		     const unsigned long *val, const unsigned long *mask,
73610b518d4SMichal Kubecek 		     unsigned int nbits, ethnl_string_array_t names,
73710b518d4SMichal Kubecek 		     bool compact)
73810b518d4SMichal Kubecek {
73910b518d4SMichal Kubecek 	u32 small_mask32[ETHNL_SMALL_BITMAP_WORDS];
74010b518d4SMichal Kubecek 	u32 small_val32[ETHNL_SMALL_BITMAP_WORDS];
74110b518d4SMichal Kubecek 	u32 *mask32;
74210b518d4SMichal Kubecek 	u32 *val32;
74310b518d4SMichal Kubecek 	int ret;
74410b518d4SMichal Kubecek 
74510b518d4SMichal Kubecek 	if (nbits > ETHNL_SMALL_BITMAP_BITS) {
74610b518d4SMichal Kubecek 		unsigned int nwords = DIV_ROUND_UP(nbits, 32);
74710b518d4SMichal Kubecek 
74810b518d4SMichal Kubecek 		val32 = kmalloc_array(2 * nwords, sizeof(u32), GFP_KERNEL);
74910b518d4SMichal Kubecek 		if (!val32)
75010b518d4SMichal Kubecek 			return -ENOMEM;
75110b518d4SMichal Kubecek 		mask32 = val32 + nwords;
75210b518d4SMichal Kubecek 	} else {
75310b518d4SMichal Kubecek 		val32 = small_val32;
75410b518d4SMichal Kubecek 		mask32 = small_mask32;
75510b518d4SMichal Kubecek 	}
75610b518d4SMichal Kubecek 
75710b518d4SMichal Kubecek 	bitmap_to_arr32(val32, val, nbits);
75810b518d4SMichal Kubecek 	if (mask)
75910b518d4SMichal Kubecek 		bitmap_to_arr32(mask32, mask, nbits);
76010b518d4SMichal Kubecek 	else
76110b518d4SMichal Kubecek 		mask32 = NULL;
76210b518d4SMichal Kubecek 	ret = ethnl_put_bitset32(skb, attrtype, val32, mask32, nbits, names,
76310b518d4SMichal Kubecek 				 compact);
76410b518d4SMichal Kubecek 
76510b518d4SMichal Kubecek 	if (nbits > ETHNL_SMALL_BITMAP_BITS)
76610b518d4SMichal Kubecek 		kfree(val32);
76710b518d4SMichal Kubecek 
76810b518d4SMichal Kubecek 	return ret;
76910b518d4SMichal Kubecek }
77010b518d4SMichal Kubecek 
ethnl_update_bitset(unsigned long * bitmap,unsigned int nbits,const struct nlattr * attr,ethnl_string_array_t names,struct netlink_ext_ack * extack,bool * mod)77110b518d4SMichal Kubecek int ethnl_update_bitset(unsigned long *bitmap, unsigned int nbits,
77210b518d4SMichal Kubecek 			const struct nlattr *attr, ethnl_string_array_t names,
77310b518d4SMichal Kubecek 			struct netlink_ext_ack *extack, bool *mod)
77410b518d4SMichal Kubecek {
77510b518d4SMichal Kubecek 	u32 small_bitmap32[ETHNL_SMALL_BITMAP_WORDS];
77610b518d4SMichal Kubecek 	u32 *bitmap32 = small_bitmap32;
77710b518d4SMichal Kubecek 	bool u32_mod = false;
77810b518d4SMichal Kubecek 	int ret;
77910b518d4SMichal Kubecek 
78010b518d4SMichal Kubecek 	if (nbits > ETHNL_SMALL_BITMAP_BITS) {
78110b518d4SMichal Kubecek 		unsigned int dst_words = DIV_ROUND_UP(nbits, 32);
78210b518d4SMichal Kubecek 
78310b518d4SMichal Kubecek 		bitmap32 = kmalloc_array(dst_words, sizeof(u32), GFP_KERNEL);
78410b518d4SMichal Kubecek 		if (!bitmap32)
78510b518d4SMichal Kubecek 			return -ENOMEM;
78610b518d4SMichal Kubecek 	}
78710b518d4SMichal Kubecek 
78810b518d4SMichal Kubecek 	bitmap_to_arr32(bitmap32, bitmap, nbits);
78910b518d4SMichal Kubecek 	ret = ethnl_update_bitset32(bitmap32, nbits, attr, names, extack,
79010b518d4SMichal Kubecek 				    &u32_mod);
79110b518d4SMichal Kubecek 	if (u32_mod) {
79210b518d4SMichal Kubecek 		bitmap_from_arr32(bitmap, bitmap32, nbits);
79310b518d4SMichal Kubecek 		*mod = true;
79410b518d4SMichal Kubecek 	}
79510b518d4SMichal Kubecek 
79610b518d4SMichal Kubecek 	if (nbits > ETHNL_SMALL_BITMAP_BITS)
79710b518d4SMichal Kubecek 		kfree(bitmap32);
79810b518d4SMichal Kubecek 
79910b518d4SMichal Kubecek 	return ret;
80010b518d4SMichal Kubecek }
80110b518d4SMichal Kubecek 
80210b518d4SMichal Kubecek #else
80310b518d4SMichal Kubecek 
80410b518d4SMichal Kubecek /* On little endian 64-bit and all 32-bit architectures, an unsigned long
80510b518d4SMichal Kubecek  * based bitmap can be interpreted as u32 based one using a simple cast.
80610b518d4SMichal Kubecek  */
80710b518d4SMichal Kubecek 
ethnl_bitset_size(const unsigned long * val,const unsigned long * mask,unsigned int nbits,ethnl_string_array_t names,bool compact)80810b518d4SMichal Kubecek int ethnl_bitset_size(const unsigned long *val, const unsigned long *mask,
80910b518d4SMichal Kubecek 		      unsigned int nbits, ethnl_string_array_t names,
81010b518d4SMichal Kubecek 		      bool compact)
81110b518d4SMichal Kubecek {
81210b518d4SMichal Kubecek 	return ethnl_bitset32_size((const u32 *)val, (const u32 *)mask, nbits,
81310b518d4SMichal Kubecek 				   names, compact);
81410b518d4SMichal Kubecek }
81510b518d4SMichal Kubecek 
ethnl_put_bitset(struct sk_buff * skb,int attrtype,const unsigned long * val,const unsigned long * mask,unsigned int nbits,ethnl_string_array_t names,bool compact)81610b518d4SMichal Kubecek int ethnl_put_bitset(struct sk_buff *skb, int attrtype,
81710b518d4SMichal Kubecek 		     const unsigned long *val, const unsigned long *mask,
81810b518d4SMichal Kubecek 		     unsigned int nbits, ethnl_string_array_t names,
81910b518d4SMichal Kubecek 		     bool compact)
82010b518d4SMichal Kubecek {
82110b518d4SMichal Kubecek 	return ethnl_put_bitset32(skb, attrtype, (const u32 *)val,
82210b518d4SMichal Kubecek 				  (const u32 *)mask, nbits, names, compact);
82310b518d4SMichal Kubecek }
82410b518d4SMichal Kubecek 
ethnl_update_bitset(unsigned long * bitmap,unsigned int nbits,const struct nlattr * attr,ethnl_string_array_t names,struct netlink_ext_ack * extack,bool * mod)82510b518d4SMichal Kubecek int ethnl_update_bitset(unsigned long *bitmap, unsigned int nbits,
82610b518d4SMichal Kubecek 			const struct nlattr *attr, ethnl_string_array_t names,
82710b518d4SMichal Kubecek 			struct netlink_ext_ack *extack, bool *mod)
82810b518d4SMichal Kubecek {
82910b518d4SMichal Kubecek 	return ethnl_update_bitset32((u32 *)bitmap, nbits, attr, names, extack,
83010b518d4SMichal Kubecek 				     mod);
83110b518d4SMichal Kubecek }
83210b518d4SMichal Kubecek 
83310b518d4SMichal Kubecek #endif /* BITS_PER_LONG == 64 && defined(__BIG_ENDIAN) */
834