xref: /OK3568_Linux_fs/kernel/net/ethtool/bitset.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun #include <linux/ethtool_netlink.h>
4*4882a593Smuzhiyun #include <linux/bitmap.h>
5*4882a593Smuzhiyun #include "netlink.h"
6*4882a593Smuzhiyun #include "bitset.h"
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun /* Some bitmaps are internally represented as an array of unsigned long, some
9*4882a593Smuzhiyun  * as an array of u32 (some even as single u32 for now). To avoid the need of
10*4882a593Smuzhiyun  * wrappers on caller side, we provide two set of functions: those with "32"
11*4882a593Smuzhiyun  * suffix in their names expect u32 based bitmaps, those without it expect
12*4882a593Smuzhiyun  * unsigned long bitmaps.
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun 
ethnl_lower_bits(unsigned int n)15*4882a593Smuzhiyun static u32 ethnl_lower_bits(unsigned int n)
16*4882a593Smuzhiyun {
17*4882a593Smuzhiyun 	return ~(u32)0 >> (32 - n % 32);
18*4882a593Smuzhiyun }
19*4882a593Smuzhiyun 
ethnl_upper_bits(unsigned int n)20*4882a593Smuzhiyun static u32 ethnl_upper_bits(unsigned int n)
21*4882a593Smuzhiyun {
22*4882a593Smuzhiyun 	return ~(u32)0 << (n % 32);
23*4882a593Smuzhiyun }
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun /**
26*4882a593Smuzhiyun  * ethnl_bitmap32_clear() - Clear u32 based bitmap
27*4882a593Smuzhiyun  * @dst:   bitmap to clear
28*4882a593Smuzhiyun  * @start: beginning of the interval
29*4882a593Smuzhiyun  * @end:   end of the interval
30*4882a593Smuzhiyun  * @mod:   set if bitmap was modified
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  * Clear @nbits bits of a bitmap with indices @start <= i < @end
33*4882a593Smuzhiyun  */
ethnl_bitmap32_clear(u32 * dst,unsigned int start,unsigned int end,bool * mod)34*4882a593Smuzhiyun static void ethnl_bitmap32_clear(u32 *dst, unsigned int start, unsigned int end,
35*4882a593Smuzhiyun 				 bool *mod)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun 	unsigned int start_word = start / 32;
38*4882a593Smuzhiyun 	unsigned int end_word = end / 32;
39*4882a593Smuzhiyun 	unsigned int i;
40*4882a593Smuzhiyun 	u32 mask;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	if (end <= start)
43*4882a593Smuzhiyun 		return;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	if (start % 32) {
46*4882a593Smuzhiyun 		mask = ethnl_upper_bits(start);
47*4882a593Smuzhiyun 		if (end_word == start_word) {
48*4882a593Smuzhiyun 			mask &= ethnl_lower_bits(end);
49*4882a593Smuzhiyun 			if (dst[start_word] & mask) {
50*4882a593Smuzhiyun 				dst[start_word] &= ~mask;
51*4882a593Smuzhiyun 				*mod = true;
52*4882a593Smuzhiyun 			}
53*4882a593Smuzhiyun 			return;
54*4882a593Smuzhiyun 		}
55*4882a593Smuzhiyun 		if (dst[start_word] & mask) {
56*4882a593Smuzhiyun 			dst[start_word] &= ~mask;
57*4882a593Smuzhiyun 			*mod = true;
58*4882a593Smuzhiyun 		}
59*4882a593Smuzhiyun 		start_word++;
60*4882a593Smuzhiyun 	}
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	for (i = start_word; i < end_word; i++) {
63*4882a593Smuzhiyun 		if (dst[i]) {
64*4882a593Smuzhiyun 			dst[i] = 0;
65*4882a593Smuzhiyun 			*mod = true;
66*4882a593Smuzhiyun 		}
67*4882a593Smuzhiyun 	}
68*4882a593Smuzhiyun 	if (end % 32) {
69*4882a593Smuzhiyun 		mask = ethnl_lower_bits(end);
70*4882a593Smuzhiyun 		if (dst[end_word] & mask) {
71*4882a593Smuzhiyun 			dst[end_word] &= ~mask;
72*4882a593Smuzhiyun 			*mod = true;
73*4882a593Smuzhiyun 		}
74*4882a593Smuzhiyun 	}
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun /**
78*4882a593Smuzhiyun  * ethnl_bitmap32_not_zero() - Check if any bit is set in an interval
79*4882a593Smuzhiyun  * @map:   bitmap to test
80*4882a593Smuzhiyun  * @start: beginning of the interval
81*4882a593Smuzhiyun  * @end:   end of the interval
82*4882a593Smuzhiyun  *
83*4882a593Smuzhiyun  * Return: true if there is non-zero bit with  index @start <= i < @end,
84*4882a593Smuzhiyun  *         false if the whole interval is zero
85*4882a593Smuzhiyun  */
ethnl_bitmap32_not_zero(const u32 * map,unsigned int start,unsigned int end)86*4882a593Smuzhiyun static bool ethnl_bitmap32_not_zero(const u32 *map, unsigned int start,
87*4882a593Smuzhiyun 				    unsigned int end)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun 	unsigned int start_word = start / 32;
90*4882a593Smuzhiyun 	unsigned int end_word = end / 32;
91*4882a593Smuzhiyun 	u32 mask;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	if (end <= start)
94*4882a593Smuzhiyun 		return true;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	if (start % 32) {
97*4882a593Smuzhiyun 		mask = ethnl_upper_bits(start);
98*4882a593Smuzhiyun 		if (end_word == start_word) {
99*4882a593Smuzhiyun 			mask &= ethnl_lower_bits(end);
100*4882a593Smuzhiyun 			return map[start_word] & mask;
101*4882a593Smuzhiyun 		}
102*4882a593Smuzhiyun 		if (map[start_word] & mask)
103*4882a593Smuzhiyun 			return true;
104*4882a593Smuzhiyun 		start_word++;
105*4882a593Smuzhiyun 	}
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	if (!memchr_inv(map + start_word, '\0',
108*4882a593Smuzhiyun 			(end_word - start_word) * sizeof(u32)))
109*4882a593Smuzhiyun 		return true;
110*4882a593Smuzhiyun 	if (end % 32 == 0)
111*4882a593Smuzhiyun 		return true;
112*4882a593Smuzhiyun 	return map[end_word] & ethnl_lower_bits(end);
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun /**
116*4882a593Smuzhiyun  * ethnl_bitmap32_update() - Modify u32 based bitmap according to value/mask
117*4882a593Smuzhiyun  *			     pair
118*4882a593Smuzhiyun  * @dst:   bitmap to update
119*4882a593Smuzhiyun  * @nbits: bit size of the bitmap
120*4882a593Smuzhiyun  * @value: values to set
121*4882a593Smuzhiyun  * @mask:  mask of bits to set
122*4882a593Smuzhiyun  * @mod:   set to true if bitmap is modified, preserve if not
123*4882a593Smuzhiyun  *
124*4882a593Smuzhiyun  * Set bits in @dst bitmap which are set in @mask to values from @value, leave
125*4882a593Smuzhiyun  * the rest untouched. If destination bitmap was modified, set @mod to true,
126*4882a593Smuzhiyun  * leave as it is if not.
127*4882a593Smuzhiyun  */
ethnl_bitmap32_update(u32 * dst,unsigned int nbits,const u32 * value,const u32 * mask,bool * mod)128*4882a593Smuzhiyun static void ethnl_bitmap32_update(u32 *dst, unsigned int nbits,
129*4882a593Smuzhiyun 				  const u32 *value, const u32 *mask, bool *mod)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun 	while (nbits > 0) {
132*4882a593Smuzhiyun 		u32 real_mask = mask ? *mask : ~(u32)0;
133*4882a593Smuzhiyun 		u32 new_value;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 		if (nbits < 32)
136*4882a593Smuzhiyun 			real_mask &= ethnl_lower_bits(nbits);
137*4882a593Smuzhiyun 		new_value = (*dst & ~real_mask) | (*value & real_mask);
138*4882a593Smuzhiyun 		if (new_value != *dst) {
139*4882a593Smuzhiyun 			*dst = new_value;
140*4882a593Smuzhiyun 			*mod = true;
141*4882a593Smuzhiyun 		}
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 		if (nbits <= 32)
144*4882a593Smuzhiyun 			break;
145*4882a593Smuzhiyun 		dst++;
146*4882a593Smuzhiyun 		nbits -= 32;
147*4882a593Smuzhiyun 		value++;
148*4882a593Smuzhiyun 		if (mask)
149*4882a593Smuzhiyun 			mask++;
150*4882a593Smuzhiyun 	}
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun 
ethnl_bitmap32_test_bit(const u32 * map,unsigned int index)153*4882a593Smuzhiyun static bool ethnl_bitmap32_test_bit(const u32 *map, unsigned int index)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun 	return map[index / 32] & (1U << (index % 32));
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun /**
159*4882a593Smuzhiyun  * ethnl_bitset32_size() - Calculate size of bitset nested attribute
160*4882a593Smuzhiyun  * @val:     value bitmap (u32 based)
161*4882a593Smuzhiyun  * @mask:    mask bitmap (u32 based, optional)
162*4882a593Smuzhiyun  * @nbits:   bit length of the bitset
163*4882a593Smuzhiyun  * @names:   array of bit names (optional)
164*4882a593Smuzhiyun  * @compact: assume compact format for output
165*4882a593Smuzhiyun  *
166*4882a593Smuzhiyun  * Estimate length of netlink attribute composed by a later call to
167*4882a593Smuzhiyun  * ethnl_put_bitset32() call with the same arguments.
168*4882a593Smuzhiyun  *
169*4882a593Smuzhiyun  * Return: negative error code or attribute length estimate
170*4882a593Smuzhiyun  */
ethnl_bitset32_size(const u32 * val,const u32 * mask,unsigned int nbits,ethnl_string_array_t names,bool compact)171*4882a593Smuzhiyun int ethnl_bitset32_size(const u32 *val, const u32 *mask, unsigned int nbits,
172*4882a593Smuzhiyun 			ethnl_string_array_t names, bool compact)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun 	unsigned int len = 0;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	/* list flag */
177*4882a593Smuzhiyun 	if (!mask)
178*4882a593Smuzhiyun 		len += nla_total_size(sizeof(u32));
179*4882a593Smuzhiyun 	/* size */
180*4882a593Smuzhiyun 	len += nla_total_size(sizeof(u32));
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	if (compact) {
183*4882a593Smuzhiyun 		unsigned int nwords = DIV_ROUND_UP(nbits, 32);
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 		/* value, mask */
186*4882a593Smuzhiyun 		len += (mask ? 2 : 1) * nla_total_size(nwords * sizeof(u32));
187*4882a593Smuzhiyun 	} else {
188*4882a593Smuzhiyun 		unsigned int bits_len = 0;
189*4882a593Smuzhiyun 		unsigned int bit_len, i;
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 		for (i = 0; i < nbits; i++) {
192*4882a593Smuzhiyun 			const char *name = names ? names[i] : NULL;
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 			if (!ethnl_bitmap32_test_bit(mask ?: val, i))
195*4882a593Smuzhiyun 				continue;
196*4882a593Smuzhiyun 			/* index */
197*4882a593Smuzhiyun 			bit_len = nla_total_size(sizeof(u32));
198*4882a593Smuzhiyun 			/* name */
199*4882a593Smuzhiyun 			if (name)
200*4882a593Smuzhiyun 				bit_len += ethnl_strz_size(name);
201*4882a593Smuzhiyun 			/* value */
202*4882a593Smuzhiyun 			if (mask && ethnl_bitmap32_test_bit(val, i))
203*4882a593Smuzhiyun 				bit_len += nla_total_size(0);
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 			/* bit nest */
206*4882a593Smuzhiyun 			bits_len += nla_total_size(bit_len);
207*4882a593Smuzhiyun 		}
208*4882a593Smuzhiyun 		/* bits nest */
209*4882a593Smuzhiyun 		len += nla_total_size(bits_len);
210*4882a593Smuzhiyun 	}
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	/* outermost nest */
213*4882a593Smuzhiyun 	return nla_total_size(len);
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun /**
217*4882a593Smuzhiyun  * ethnl_put_bitset32() - Put a bitset nest into a message
218*4882a593Smuzhiyun  * @skb:      skb with the message
219*4882a593Smuzhiyun  * @attrtype: attribute type for the bitset nest
220*4882a593Smuzhiyun  * @val:      value bitmap (u32 based)
221*4882a593Smuzhiyun  * @mask:     mask bitmap (u32 based, optional)
222*4882a593Smuzhiyun  * @nbits:    bit length of the bitset
223*4882a593Smuzhiyun  * @names:    array of bit names (optional)
224*4882a593Smuzhiyun  * @compact:  use compact format for the output
225*4882a593Smuzhiyun  *
226*4882a593Smuzhiyun  * Compose a nested attribute representing a bitset. If @mask is null, simple
227*4882a593Smuzhiyun  * bitmap (bit list) is created, if @mask is provided, represent a value/mask
228*4882a593Smuzhiyun  * pair. Bit names are only used in verbose mode and when provided by calller.
229*4882a593Smuzhiyun  *
230*4882a593Smuzhiyun  * Return: 0 on success, negative error value on error
231*4882a593Smuzhiyun  */
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)232*4882a593Smuzhiyun int ethnl_put_bitset32(struct sk_buff *skb, int attrtype, const u32 *val,
233*4882a593Smuzhiyun 		       const u32 *mask, unsigned int nbits,
234*4882a593Smuzhiyun 		       ethnl_string_array_t names, bool compact)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun 	struct nlattr *nest;
237*4882a593Smuzhiyun 	struct nlattr *attr;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	nest = nla_nest_start(skb, attrtype);
240*4882a593Smuzhiyun 	if (!nest)
241*4882a593Smuzhiyun 		return -EMSGSIZE;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	if (!mask && nla_put_flag(skb, ETHTOOL_A_BITSET_NOMASK))
244*4882a593Smuzhiyun 		goto nla_put_failure;
245*4882a593Smuzhiyun 	if (nla_put_u32(skb, ETHTOOL_A_BITSET_SIZE, nbits))
246*4882a593Smuzhiyun 		goto nla_put_failure;
247*4882a593Smuzhiyun 	if (compact) {
248*4882a593Smuzhiyun 		unsigned int nwords = DIV_ROUND_UP(nbits, 32);
249*4882a593Smuzhiyun 		unsigned int nbytes = nwords * sizeof(u32);
250*4882a593Smuzhiyun 		u32 *dst;
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 		attr = nla_reserve(skb, ETHTOOL_A_BITSET_VALUE, nbytes);
253*4882a593Smuzhiyun 		if (!attr)
254*4882a593Smuzhiyun 			goto nla_put_failure;
255*4882a593Smuzhiyun 		dst = nla_data(attr);
256*4882a593Smuzhiyun 		memcpy(dst, val, nbytes);
257*4882a593Smuzhiyun 		if (nbits % 32)
258*4882a593Smuzhiyun 			dst[nwords - 1] &= ethnl_lower_bits(nbits);
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 		if (mask) {
261*4882a593Smuzhiyun 			attr = nla_reserve(skb, ETHTOOL_A_BITSET_MASK, nbytes);
262*4882a593Smuzhiyun 			if (!attr)
263*4882a593Smuzhiyun 				goto nla_put_failure;
264*4882a593Smuzhiyun 			dst = nla_data(attr);
265*4882a593Smuzhiyun 			memcpy(dst, mask, nbytes);
266*4882a593Smuzhiyun 			if (nbits % 32)
267*4882a593Smuzhiyun 				dst[nwords - 1] &= ethnl_lower_bits(nbits);
268*4882a593Smuzhiyun 		}
269*4882a593Smuzhiyun 	} else {
270*4882a593Smuzhiyun 		struct nlattr *bits;
271*4882a593Smuzhiyun 		unsigned int i;
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 		bits = nla_nest_start(skb, ETHTOOL_A_BITSET_BITS);
274*4882a593Smuzhiyun 		if (!bits)
275*4882a593Smuzhiyun 			goto nla_put_failure;
276*4882a593Smuzhiyun 		for (i = 0; i < nbits; i++) {
277*4882a593Smuzhiyun 			const char *name = names ? names[i] : NULL;
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 			if (!ethnl_bitmap32_test_bit(mask ?: val, i))
280*4882a593Smuzhiyun 				continue;
281*4882a593Smuzhiyun 			attr = nla_nest_start(skb, ETHTOOL_A_BITSET_BITS_BIT);
282*4882a593Smuzhiyun 			if (!attr)
283*4882a593Smuzhiyun 				goto nla_put_failure;
284*4882a593Smuzhiyun 			if (nla_put_u32(skb, ETHTOOL_A_BITSET_BIT_INDEX, i))
285*4882a593Smuzhiyun 				goto nla_put_failure;
286*4882a593Smuzhiyun 			if (name &&
287*4882a593Smuzhiyun 			    ethnl_put_strz(skb, ETHTOOL_A_BITSET_BIT_NAME, name))
288*4882a593Smuzhiyun 				goto nla_put_failure;
289*4882a593Smuzhiyun 			if (mask && ethnl_bitmap32_test_bit(val, i) &&
290*4882a593Smuzhiyun 			    nla_put_flag(skb, ETHTOOL_A_BITSET_BIT_VALUE))
291*4882a593Smuzhiyun 				goto nla_put_failure;
292*4882a593Smuzhiyun 			nla_nest_end(skb, attr);
293*4882a593Smuzhiyun 		}
294*4882a593Smuzhiyun 		nla_nest_end(skb, bits);
295*4882a593Smuzhiyun 	}
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	nla_nest_end(skb, nest);
298*4882a593Smuzhiyun 	return 0;
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun nla_put_failure:
301*4882a593Smuzhiyun 	nla_nest_cancel(skb, nest);
302*4882a593Smuzhiyun 	return -EMSGSIZE;
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun static const struct nla_policy bitset_policy[] = {
306*4882a593Smuzhiyun 	[ETHTOOL_A_BITSET_NOMASK]	= { .type = NLA_FLAG },
307*4882a593Smuzhiyun 	[ETHTOOL_A_BITSET_SIZE]		= NLA_POLICY_MAX(NLA_U32,
308*4882a593Smuzhiyun 							 ETHNL_MAX_BITSET_SIZE),
309*4882a593Smuzhiyun 	[ETHTOOL_A_BITSET_BITS]		= { .type = NLA_NESTED },
310*4882a593Smuzhiyun 	[ETHTOOL_A_BITSET_VALUE]	= { .type = NLA_BINARY },
311*4882a593Smuzhiyun 	[ETHTOOL_A_BITSET_MASK]		= { .type = NLA_BINARY },
312*4882a593Smuzhiyun };
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun static const struct nla_policy bit_policy[] = {
315*4882a593Smuzhiyun 	[ETHTOOL_A_BITSET_BIT_INDEX]	= { .type = NLA_U32 },
316*4882a593Smuzhiyun 	[ETHTOOL_A_BITSET_BIT_NAME]	= { .type = NLA_NUL_STRING },
317*4882a593Smuzhiyun 	[ETHTOOL_A_BITSET_BIT_VALUE]	= { .type = NLA_FLAG },
318*4882a593Smuzhiyun };
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun /**
321*4882a593Smuzhiyun  * ethnl_bitset_is_compact() - check if bitset attribute represents a compact
322*4882a593Smuzhiyun  *			       bitset
323*4882a593Smuzhiyun  * @bitset:  nested attribute representing a bitset
324*4882a593Smuzhiyun  * @compact: pointer for return value
325*4882a593Smuzhiyun  *
326*4882a593Smuzhiyun  * Return: 0 on success, negative error code on failure
327*4882a593Smuzhiyun  */
ethnl_bitset_is_compact(const struct nlattr * bitset,bool * compact)328*4882a593Smuzhiyun int ethnl_bitset_is_compact(const struct nlattr *bitset, bool *compact)
329*4882a593Smuzhiyun {
330*4882a593Smuzhiyun 	struct nlattr *tb[ARRAY_SIZE(bitset_policy)];
331*4882a593Smuzhiyun 	int ret;
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	ret = nla_parse_nested(tb, ARRAY_SIZE(bitset_policy) - 1, bitset,
334*4882a593Smuzhiyun 			       bitset_policy, NULL);
335*4882a593Smuzhiyun 	if (ret < 0)
336*4882a593Smuzhiyun 		return ret;
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	if (tb[ETHTOOL_A_BITSET_BITS]) {
339*4882a593Smuzhiyun 		if (tb[ETHTOOL_A_BITSET_VALUE] || tb[ETHTOOL_A_BITSET_MASK])
340*4882a593Smuzhiyun 			return -EINVAL;
341*4882a593Smuzhiyun 		*compact = false;
342*4882a593Smuzhiyun 		return 0;
343*4882a593Smuzhiyun 	}
344*4882a593Smuzhiyun 	if (!tb[ETHTOOL_A_BITSET_SIZE] || !tb[ETHTOOL_A_BITSET_VALUE])
345*4882a593Smuzhiyun 		return -EINVAL;
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	*compact = true;
348*4882a593Smuzhiyun 	return 0;
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun /**
352*4882a593Smuzhiyun  * ethnl_name_to_idx() - look up string index for a name
353*4882a593Smuzhiyun  * @names:   array of ETH_GSTRING_LEN sized strings
354*4882a593Smuzhiyun  * @n_names: number of strings in the array
355*4882a593Smuzhiyun  * @name:    name to look up
356*4882a593Smuzhiyun  *
357*4882a593Smuzhiyun  * Return: index of the string if found, -ENOENT if not found
358*4882a593Smuzhiyun  */
ethnl_name_to_idx(ethnl_string_array_t names,unsigned int n_names,const char * name)359*4882a593Smuzhiyun static int ethnl_name_to_idx(ethnl_string_array_t names, unsigned int n_names,
360*4882a593Smuzhiyun 			     const char *name)
361*4882a593Smuzhiyun {
362*4882a593Smuzhiyun 	unsigned int i;
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	if (!names)
365*4882a593Smuzhiyun 		return -ENOENT;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	for (i = 0; i < n_names; i++) {
368*4882a593Smuzhiyun 		/* names[i] may not be null terminated */
369*4882a593Smuzhiyun 		if (!strncmp(names[i], name, ETH_GSTRING_LEN) &&
370*4882a593Smuzhiyun 		    strlen(name) <= ETH_GSTRING_LEN)
371*4882a593Smuzhiyun 			return i;
372*4882a593Smuzhiyun 	}
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	return -ENOENT;
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun 
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)377*4882a593Smuzhiyun static int ethnl_parse_bit(unsigned int *index, bool *val, unsigned int nbits,
378*4882a593Smuzhiyun 			   const struct nlattr *bit_attr, bool no_mask,
379*4882a593Smuzhiyun 			   ethnl_string_array_t names,
380*4882a593Smuzhiyun 			   struct netlink_ext_ack *extack)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun 	struct nlattr *tb[ARRAY_SIZE(bit_policy)];
383*4882a593Smuzhiyun 	int ret, idx;
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	ret = nla_parse_nested(tb, ARRAY_SIZE(bit_policy) - 1, bit_attr,
386*4882a593Smuzhiyun 			       bit_policy, extack);
387*4882a593Smuzhiyun 	if (ret < 0)
388*4882a593Smuzhiyun 		return ret;
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	if (tb[ETHTOOL_A_BITSET_BIT_INDEX]) {
391*4882a593Smuzhiyun 		const char *name;
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 		idx = nla_get_u32(tb[ETHTOOL_A_BITSET_BIT_INDEX]);
394*4882a593Smuzhiyun 		if (idx >= nbits) {
395*4882a593Smuzhiyun 			NL_SET_ERR_MSG_ATTR(extack,
396*4882a593Smuzhiyun 					    tb[ETHTOOL_A_BITSET_BIT_INDEX],
397*4882a593Smuzhiyun 					    "bit index too high");
398*4882a593Smuzhiyun 			return -EOPNOTSUPP;
399*4882a593Smuzhiyun 		}
400*4882a593Smuzhiyun 		name = names ? names[idx] : NULL;
401*4882a593Smuzhiyun 		if (tb[ETHTOOL_A_BITSET_BIT_NAME] && name &&
402*4882a593Smuzhiyun 		    strncmp(nla_data(tb[ETHTOOL_A_BITSET_BIT_NAME]), name,
403*4882a593Smuzhiyun 			    nla_len(tb[ETHTOOL_A_BITSET_BIT_NAME]))) {
404*4882a593Smuzhiyun 			NL_SET_ERR_MSG_ATTR(extack, bit_attr,
405*4882a593Smuzhiyun 					    "bit index and name mismatch");
406*4882a593Smuzhiyun 			return -EINVAL;
407*4882a593Smuzhiyun 		}
408*4882a593Smuzhiyun 	} else if (tb[ETHTOOL_A_BITSET_BIT_NAME]) {
409*4882a593Smuzhiyun 		idx = ethnl_name_to_idx(names, nbits,
410*4882a593Smuzhiyun 					nla_data(tb[ETHTOOL_A_BITSET_BIT_NAME]));
411*4882a593Smuzhiyun 		if (idx < 0) {
412*4882a593Smuzhiyun 			NL_SET_ERR_MSG_ATTR(extack,
413*4882a593Smuzhiyun 					    tb[ETHTOOL_A_BITSET_BIT_NAME],
414*4882a593Smuzhiyun 					    "bit name not found");
415*4882a593Smuzhiyun 			return -EOPNOTSUPP;
416*4882a593Smuzhiyun 		}
417*4882a593Smuzhiyun 	} else {
418*4882a593Smuzhiyun 		NL_SET_ERR_MSG_ATTR(extack, bit_attr,
419*4882a593Smuzhiyun 				    "neither bit index nor name specified");
420*4882a593Smuzhiyun 		return -EINVAL;
421*4882a593Smuzhiyun 	}
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	*index = idx;
424*4882a593Smuzhiyun 	*val = no_mask || tb[ETHTOOL_A_BITSET_BIT_VALUE];
425*4882a593Smuzhiyun 	return 0;
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 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)429*4882a593Smuzhiyun ethnl_update_bitset32_verbose(u32 *bitmap, unsigned int nbits,
430*4882a593Smuzhiyun 			      const struct nlattr *attr, struct nlattr **tb,
431*4882a593Smuzhiyun 			      ethnl_string_array_t names,
432*4882a593Smuzhiyun 			      struct netlink_ext_ack *extack, bool *mod)
433*4882a593Smuzhiyun {
434*4882a593Smuzhiyun 	struct nlattr *bit_attr;
435*4882a593Smuzhiyun 	bool no_mask;
436*4882a593Smuzhiyun 	int rem;
437*4882a593Smuzhiyun 	int ret;
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	if (tb[ETHTOOL_A_BITSET_VALUE]) {
440*4882a593Smuzhiyun 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_VALUE],
441*4882a593Smuzhiyun 				    "value only allowed in compact bitset");
442*4882a593Smuzhiyun 		return -EINVAL;
443*4882a593Smuzhiyun 	}
444*4882a593Smuzhiyun 	if (tb[ETHTOOL_A_BITSET_MASK]) {
445*4882a593Smuzhiyun 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK],
446*4882a593Smuzhiyun 				    "mask only allowed in compact bitset");
447*4882a593Smuzhiyun 		return -EINVAL;
448*4882a593Smuzhiyun 	}
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	no_mask = tb[ETHTOOL_A_BITSET_NOMASK];
451*4882a593Smuzhiyun 	if (no_mask)
452*4882a593Smuzhiyun 		ethnl_bitmap32_clear(bitmap, 0, nbits, mod);
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	nla_for_each_nested(bit_attr, tb[ETHTOOL_A_BITSET_BITS], rem) {
455*4882a593Smuzhiyun 		bool old_val, new_val;
456*4882a593Smuzhiyun 		unsigned int idx;
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 		if (nla_type(bit_attr) != ETHTOOL_A_BITSET_BITS_BIT) {
459*4882a593Smuzhiyun 			NL_SET_ERR_MSG_ATTR(extack, bit_attr,
460*4882a593Smuzhiyun 					    "only ETHTOOL_A_BITSET_BITS_BIT allowed in ETHTOOL_A_BITSET_BITS");
461*4882a593Smuzhiyun 			return -EINVAL;
462*4882a593Smuzhiyun 		}
463*4882a593Smuzhiyun 		ret = ethnl_parse_bit(&idx, &new_val, nbits, bit_attr, no_mask,
464*4882a593Smuzhiyun 				      names, extack);
465*4882a593Smuzhiyun 		if (ret < 0)
466*4882a593Smuzhiyun 			return ret;
467*4882a593Smuzhiyun 		old_val = bitmap[idx / 32] & ((u32)1 << (idx % 32));
468*4882a593Smuzhiyun 		if (new_val != old_val) {
469*4882a593Smuzhiyun 			if (new_val)
470*4882a593Smuzhiyun 				bitmap[idx / 32] |= ((u32)1 << (idx % 32));
471*4882a593Smuzhiyun 			else
472*4882a593Smuzhiyun 				bitmap[idx / 32] &= ~((u32)1 << (idx % 32));
473*4882a593Smuzhiyun 			*mod = true;
474*4882a593Smuzhiyun 		}
475*4882a593Smuzhiyun 	}
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	return 0;
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun 
ethnl_compact_sanity_checks(unsigned int nbits,const struct nlattr * nest,struct nlattr ** tb,struct netlink_ext_ack * extack)480*4882a593Smuzhiyun static int ethnl_compact_sanity_checks(unsigned int nbits,
481*4882a593Smuzhiyun 				       const struct nlattr *nest,
482*4882a593Smuzhiyun 				       struct nlattr **tb,
483*4882a593Smuzhiyun 				       struct netlink_ext_ack *extack)
484*4882a593Smuzhiyun {
485*4882a593Smuzhiyun 	bool no_mask = tb[ETHTOOL_A_BITSET_NOMASK];
486*4882a593Smuzhiyun 	unsigned int attr_nbits, attr_nwords;
487*4882a593Smuzhiyun 	const struct nlattr *test_attr;
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	if (no_mask && tb[ETHTOOL_A_BITSET_MASK]) {
490*4882a593Smuzhiyun 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK],
491*4882a593Smuzhiyun 				    "mask not allowed in list bitset");
492*4882a593Smuzhiyun 		return -EINVAL;
493*4882a593Smuzhiyun 	}
494*4882a593Smuzhiyun 	if (!tb[ETHTOOL_A_BITSET_SIZE]) {
495*4882a593Smuzhiyun 		NL_SET_ERR_MSG_ATTR(extack, nest,
496*4882a593Smuzhiyun 				    "missing size in compact bitset");
497*4882a593Smuzhiyun 		return -EINVAL;
498*4882a593Smuzhiyun 	}
499*4882a593Smuzhiyun 	if (!tb[ETHTOOL_A_BITSET_VALUE]) {
500*4882a593Smuzhiyun 		NL_SET_ERR_MSG_ATTR(extack, nest,
501*4882a593Smuzhiyun 				    "missing value in compact bitset");
502*4882a593Smuzhiyun 		return -EINVAL;
503*4882a593Smuzhiyun 	}
504*4882a593Smuzhiyun 	if (!no_mask && !tb[ETHTOOL_A_BITSET_MASK]) {
505*4882a593Smuzhiyun 		NL_SET_ERR_MSG_ATTR(extack, nest,
506*4882a593Smuzhiyun 				    "missing mask in compact nonlist bitset");
507*4882a593Smuzhiyun 		return -EINVAL;
508*4882a593Smuzhiyun 	}
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	attr_nbits = nla_get_u32(tb[ETHTOOL_A_BITSET_SIZE]);
511*4882a593Smuzhiyun 	attr_nwords = DIV_ROUND_UP(attr_nbits, 32);
512*4882a593Smuzhiyun 	if (nla_len(tb[ETHTOOL_A_BITSET_VALUE]) != attr_nwords * sizeof(u32)) {
513*4882a593Smuzhiyun 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_VALUE],
514*4882a593Smuzhiyun 				    "bitset value length does not match size");
515*4882a593Smuzhiyun 		return -EINVAL;
516*4882a593Smuzhiyun 	}
517*4882a593Smuzhiyun 	if (tb[ETHTOOL_A_BITSET_MASK] &&
518*4882a593Smuzhiyun 	    nla_len(tb[ETHTOOL_A_BITSET_MASK]) != attr_nwords * sizeof(u32)) {
519*4882a593Smuzhiyun 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK],
520*4882a593Smuzhiyun 				    "bitset mask length does not match size");
521*4882a593Smuzhiyun 		return -EINVAL;
522*4882a593Smuzhiyun 	}
523*4882a593Smuzhiyun 	if (attr_nbits <= nbits)
524*4882a593Smuzhiyun 		return 0;
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun 	test_attr = no_mask ? tb[ETHTOOL_A_BITSET_VALUE] :
527*4882a593Smuzhiyun 			      tb[ETHTOOL_A_BITSET_MASK];
528*4882a593Smuzhiyun 	if (ethnl_bitmap32_not_zero(nla_data(test_attr), nbits, attr_nbits)) {
529*4882a593Smuzhiyun 		NL_SET_ERR_MSG_ATTR(extack, test_attr,
530*4882a593Smuzhiyun 				    "cannot modify bits past kernel bitset size");
531*4882a593Smuzhiyun 		return -EINVAL;
532*4882a593Smuzhiyun 	}
533*4882a593Smuzhiyun 	return 0;
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun /**
537*4882a593Smuzhiyun  * ethnl_update_bitset32() - Apply a bitset nest to a u32 based bitmap
538*4882a593Smuzhiyun  * @bitmap:  bitmap to update
539*4882a593Smuzhiyun  * @nbits:   size of the updated bitmap in bits
540*4882a593Smuzhiyun  * @attr:    nest attribute to parse and apply
541*4882a593Smuzhiyun  * @names:   array of bit names; may be null for compact format
542*4882a593Smuzhiyun  * @extack:  extack for error reporting
543*4882a593Smuzhiyun  * @mod:     set this to true if bitmap is modified, leave as it is if not
544*4882a593Smuzhiyun  *
545*4882a593Smuzhiyun  * Apply bitset netsted attribute to a bitmap. If the attribute represents
546*4882a593Smuzhiyun  * a bit list, @bitmap is set to its contents; otherwise, bits in mask are
547*4882a593Smuzhiyun  * set to values from value. Bitmaps in the attribute may be longer than
548*4882a593Smuzhiyun  * @nbits but the message must not request modifying any bits past @nbits.
549*4882a593Smuzhiyun  *
550*4882a593Smuzhiyun  * Return: negative error code on failure, 0 on success
551*4882a593Smuzhiyun  */
ethnl_update_bitset32(u32 * bitmap,unsigned int nbits,const struct nlattr * attr,ethnl_string_array_t names,struct netlink_ext_ack * extack,bool * mod)552*4882a593Smuzhiyun int ethnl_update_bitset32(u32 *bitmap, unsigned int nbits,
553*4882a593Smuzhiyun 			  const struct nlattr *attr, ethnl_string_array_t names,
554*4882a593Smuzhiyun 			  struct netlink_ext_ack *extack, bool *mod)
555*4882a593Smuzhiyun {
556*4882a593Smuzhiyun 	struct nlattr *tb[ARRAY_SIZE(bitset_policy)];
557*4882a593Smuzhiyun 	unsigned int change_bits;
558*4882a593Smuzhiyun 	bool no_mask;
559*4882a593Smuzhiyun 	int ret;
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun 	if (!attr)
562*4882a593Smuzhiyun 		return 0;
563*4882a593Smuzhiyun 	ret = nla_parse_nested(tb, ARRAY_SIZE(bitset_policy) - 1, attr,
564*4882a593Smuzhiyun 			       bitset_policy, extack);
565*4882a593Smuzhiyun 	if (ret < 0)
566*4882a593Smuzhiyun 		return ret;
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 	if (tb[ETHTOOL_A_BITSET_BITS])
569*4882a593Smuzhiyun 		return ethnl_update_bitset32_verbose(bitmap, nbits, attr, tb,
570*4882a593Smuzhiyun 						     names, extack, mod);
571*4882a593Smuzhiyun 	ret = ethnl_compact_sanity_checks(nbits, attr, tb, extack);
572*4882a593Smuzhiyun 	if (ret < 0)
573*4882a593Smuzhiyun 		return ret;
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun 	no_mask = tb[ETHTOOL_A_BITSET_NOMASK];
576*4882a593Smuzhiyun 	change_bits = min_t(unsigned int,
577*4882a593Smuzhiyun 			    nla_get_u32(tb[ETHTOOL_A_BITSET_SIZE]), nbits);
578*4882a593Smuzhiyun 	ethnl_bitmap32_update(bitmap, change_bits,
579*4882a593Smuzhiyun 			      nla_data(tb[ETHTOOL_A_BITSET_VALUE]),
580*4882a593Smuzhiyun 			      no_mask ? NULL :
581*4882a593Smuzhiyun 					nla_data(tb[ETHTOOL_A_BITSET_MASK]),
582*4882a593Smuzhiyun 			      mod);
583*4882a593Smuzhiyun 	if (no_mask && change_bits < nbits)
584*4882a593Smuzhiyun 		ethnl_bitmap32_clear(bitmap, change_bits, nbits, mod);
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun 	return 0;
587*4882a593Smuzhiyun }
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun /**
590*4882a593Smuzhiyun  * ethnl_parse_bitset() - Compute effective value and mask from bitset nest
591*4882a593Smuzhiyun  * @val:     unsigned long based bitmap to put value into
592*4882a593Smuzhiyun  * @mask:    unsigned long based bitmap to put mask into
593*4882a593Smuzhiyun  * @nbits:   size of @val and @mask bitmaps
594*4882a593Smuzhiyun  * @attr:    nest attribute to parse and apply
595*4882a593Smuzhiyun  * @names:   array of bit names; may be null for compact format
596*4882a593Smuzhiyun  * @extack:  extack for error reporting
597*4882a593Smuzhiyun  *
598*4882a593Smuzhiyun  * Provide @nbits size long bitmaps for value and mask so that
599*4882a593Smuzhiyun  * x = (val & mask) | (x & ~mask) would modify any @nbits sized bitmap x
600*4882a593Smuzhiyun  * the same way ethnl_update_bitset() with the same bitset attribute would.
601*4882a593Smuzhiyun  *
602*4882a593Smuzhiyun  * Return:   negative error code on failure, 0 on success
603*4882a593Smuzhiyun  */
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)604*4882a593Smuzhiyun int ethnl_parse_bitset(unsigned long *val, unsigned long *mask,
605*4882a593Smuzhiyun 		       unsigned int nbits, const struct nlattr *attr,
606*4882a593Smuzhiyun 		       ethnl_string_array_t names,
607*4882a593Smuzhiyun 		       struct netlink_ext_ack *extack)
608*4882a593Smuzhiyun {
609*4882a593Smuzhiyun 	struct nlattr *tb[ARRAY_SIZE(bitset_policy)];
610*4882a593Smuzhiyun 	const struct nlattr *bit_attr;
611*4882a593Smuzhiyun 	bool no_mask;
612*4882a593Smuzhiyun 	int rem;
613*4882a593Smuzhiyun 	int ret;
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	if (!attr)
616*4882a593Smuzhiyun 		return 0;
617*4882a593Smuzhiyun 	ret = nla_parse_nested(tb, ARRAY_SIZE(bitset_policy) - 1, attr,
618*4882a593Smuzhiyun 			       bitset_policy, extack);
619*4882a593Smuzhiyun 	if (ret < 0)
620*4882a593Smuzhiyun 		return ret;
621*4882a593Smuzhiyun 	no_mask = tb[ETHTOOL_A_BITSET_NOMASK];
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 	if (!tb[ETHTOOL_A_BITSET_BITS]) {
624*4882a593Smuzhiyun 		unsigned int change_bits;
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun 		ret = ethnl_compact_sanity_checks(nbits, attr, tb, extack);
627*4882a593Smuzhiyun 		if (ret < 0)
628*4882a593Smuzhiyun 			return ret;
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun 		change_bits = nla_get_u32(tb[ETHTOOL_A_BITSET_SIZE]);
631*4882a593Smuzhiyun 		if (change_bits > nbits)
632*4882a593Smuzhiyun 			change_bits = nbits;
633*4882a593Smuzhiyun 		bitmap_from_arr32(val, nla_data(tb[ETHTOOL_A_BITSET_VALUE]),
634*4882a593Smuzhiyun 				  change_bits);
635*4882a593Smuzhiyun 		if (change_bits < nbits)
636*4882a593Smuzhiyun 			bitmap_clear(val, change_bits, nbits - change_bits);
637*4882a593Smuzhiyun 		if (no_mask) {
638*4882a593Smuzhiyun 			bitmap_fill(mask, nbits);
639*4882a593Smuzhiyun 		} else {
640*4882a593Smuzhiyun 			bitmap_from_arr32(mask,
641*4882a593Smuzhiyun 					  nla_data(tb[ETHTOOL_A_BITSET_MASK]),
642*4882a593Smuzhiyun 					  change_bits);
643*4882a593Smuzhiyun 			if (change_bits < nbits)
644*4882a593Smuzhiyun 				bitmap_clear(mask, change_bits,
645*4882a593Smuzhiyun 					     nbits - change_bits);
646*4882a593Smuzhiyun 		}
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 		return 0;
649*4882a593Smuzhiyun 	}
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	if (tb[ETHTOOL_A_BITSET_VALUE]) {
652*4882a593Smuzhiyun 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_VALUE],
653*4882a593Smuzhiyun 				    "value only allowed in compact bitset");
654*4882a593Smuzhiyun 		return -EINVAL;
655*4882a593Smuzhiyun 	}
656*4882a593Smuzhiyun 	if (tb[ETHTOOL_A_BITSET_MASK]) {
657*4882a593Smuzhiyun 		NL_SET_ERR_MSG_ATTR(extack, tb[ETHTOOL_A_BITSET_MASK],
658*4882a593Smuzhiyun 				    "mask only allowed in compact bitset");
659*4882a593Smuzhiyun 		return -EINVAL;
660*4882a593Smuzhiyun 	}
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 	bitmap_zero(val, nbits);
663*4882a593Smuzhiyun 	if (no_mask)
664*4882a593Smuzhiyun 		bitmap_fill(mask, nbits);
665*4882a593Smuzhiyun 	else
666*4882a593Smuzhiyun 		bitmap_zero(mask, nbits);
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	nla_for_each_nested(bit_attr, tb[ETHTOOL_A_BITSET_BITS], rem) {
669*4882a593Smuzhiyun 		unsigned int idx;
670*4882a593Smuzhiyun 		bool bit_val;
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 		ret = ethnl_parse_bit(&idx, &bit_val, nbits, bit_attr, no_mask,
673*4882a593Smuzhiyun 				      names, extack);
674*4882a593Smuzhiyun 		if (ret < 0)
675*4882a593Smuzhiyun 			return ret;
676*4882a593Smuzhiyun 		if (bit_val)
677*4882a593Smuzhiyun 			__set_bit(idx, val);
678*4882a593Smuzhiyun 		if (!no_mask)
679*4882a593Smuzhiyun 			__set_bit(idx, mask);
680*4882a593Smuzhiyun 	}
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 	return 0;
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun #if BITS_PER_LONG == 64 && defined(__BIG_ENDIAN)
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun /* 64-bit big endian architectures are the only case when u32 based bitmaps
688*4882a593Smuzhiyun  * and unsigned long based bitmaps have different memory layout so that we
689*4882a593Smuzhiyun  * cannot simply cast the latter to the former and need actual wrappers
690*4882a593Smuzhiyun  * converting the latter to the former.
691*4882a593Smuzhiyun  *
692*4882a593Smuzhiyun  * To reduce the number of slab allocations, the wrappers use fixed size local
693*4882a593Smuzhiyun  * variables for bitmaps up to ETHNL_SMALL_BITMAP_BITS bits which is the
694*4882a593Smuzhiyun  * majority of bitmaps used by ethtool.
695*4882a593Smuzhiyun  */
696*4882a593Smuzhiyun #define ETHNL_SMALL_BITMAP_BITS 128
697*4882a593Smuzhiyun #define ETHNL_SMALL_BITMAP_WORDS DIV_ROUND_UP(ETHNL_SMALL_BITMAP_BITS, 32)
698*4882a593Smuzhiyun 
ethnl_bitset_size(const unsigned long * val,const unsigned long * mask,unsigned int nbits,ethnl_string_array_t names,bool compact)699*4882a593Smuzhiyun int ethnl_bitset_size(const unsigned long *val, const unsigned long *mask,
700*4882a593Smuzhiyun 		      unsigned int nbits, ethnl_string_array_t names,
701*4882a593Smuzhiyun 		      bool compact)
702*4882a593Smuzhiyun {
703*4882a593Smuzhiyun 	u32 small_mask32[ETHNL_SMALL_BITMAP_WORDS];
704*4882a593Smuzhiyun 	u32 small_val32[ETHNL_SMALL_BITMAP_WORDS];
705*4882a593Smuzhiyun 	u32 *mask32;
706*4882a593Smuzhiyun 	u32 *val32;
707*4882a593Smuzhiyun 	int ret;
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun 	if (nbits > ETHNL_SMALL_BITMAP_BITS) {
710*4882a593Smuzhiyun 		unsigned int nwords = DIV_ROUND_UP(nbits, 32);
711*4882a593Smuzhiyun 
712*4882a593Smuzhiyun 		val32 = kmalloc_array(2 * nwords, sizeof(u32), GFP_KERNEL);
713*4882a593Smuzhiyun 		if (!val32)
714*4882a593Smuzhiyun 			return -ENOMEM;
715*4882a593Smuzhiyun 		mask32 = val32 + nwords;
716*4882a593Smuzhiyun 	} else {
717*4882a593Smuzhiyun 		val32 = small_val32;
718*4882a593Smuzhiyun 		mask32 = small_mask32;
719*4882a593Smuzhiyun 	}
720*4882a593Smuzhiyun 
721*4882a593Smuzhiyun 	bitmap_to_arr32(val32, val, nbits);
722*4882a593Smuzhiyun 	if (mask)
723*4882a593Smuzhiyun 		bitmap_to_arr32(mask32, mask, nbits);
724*4882a593Smuzhiyun 	else
725*4882a593Smuzhiyun 		mask32 = NULL;
726*4882a593Smuzhiyun 	ret = ethnl_bitset32_size(val32, mask32, nbits, names, compact);
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 	if (nbits > ETHNL_SMALL_BITMAP_BITS)
729*4882a593Smuzhiyun 		kfree(val32);
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun 	return ret;
732*4882a593Smuzhiyun }
733*4882a593Smuzhiyun 
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)734*4882a593Smuzhiyun int ethnl_put_bitset(struct sk_buff *skb, int attrtype,
735*4882a593Smuzhiyun 		     const unsigned long *val, const unsigned long *mask,
736*4882a593Smuzhiyun 		     unsigned int nbits, ethnl_string_array_t names,
737*4882a593Smuzhiyun 		     bool compact)
738*4882a593Smuzhiyun {
739*4882a593Smuzhiyun 	u32 small_mask32[ETHNL_SMALL_BITMAP_WORDS];
740*4882a593Smuzhiyun 	u32 small_val32[ETHNL_SMALL_BITMAP_WORDS];
741*4882a593Smuzhiyun 	u32 *mask32;
742*4882a593Smuzhiyun 	u32 *val32;
743*4882a593Smuzhiyun 	int ret;
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 	if (nbits > ETHNL_SMALL_BITMAP_BITS) {
746*4882a593Smuzhiyun 		unsigned int nwords = DIV_ROUND_UP(nbits, 32);
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 		val32 = kmalloc_array(2 * nwords, sizeof(u32), GFP_KERNEL);
749*4882a593Smuzhiyun 		if (!val32)
750*4882a593Smuzhiyun 			return -ENOMEM;
751*4882a593Smuzhiyun 		mask32 = val32 + nwords;
752*4882a593Smuzhiyun 	} else {
753*4882a593Smuzhiyun 		val32 = small_val32;
754*4882a593Smuzhiyun 		mask32 = small_mask32;
755*4882a593Smuzhiyun 	}
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun 	bitmap_to_arr32(val32, val, nbits);
758*4882a593Smuzhiyun 	if (mask)
759*4882a593Smuzhiyun 		bitmap_to_arr32(mask32, mask, nbits);
760*4882a593Smuzhiyun 	else
761*4882a593Smuzhiyun 		mask32 = NULL;
762*4882a593Smuzhiyun 	ret = ethnl_put_bitset32(skb, attrtype, val32, mask32, nbits, names,
763*4882a593Smuzhiyun 				 compact);
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	if (nbits > ETHNL_SMALL_BITMAP_BITS)
766*4882a593Smuzhiyun 		kfree(val32);
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	return ret;
769*4882a593Smuzhiyun }
770*4882a593Smuzhiyun 
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)771*4882a593Smuzhiyun int ethnl_update_bitset(unsigned long *bitmap, unsigned int nbits,
772*4882a593Smuzhiyun 			const struct nlattr *attr, ethnl_string_array_t names,
773*4882a593Smuzhiyun 			struct netlink_ext_ack *extack, bool *mod)
774*4882a593Smuzhiyun {
775*4882a593Smuzhiyun 	u32 small_bitmap32[ETHNL_SMALL_BITMAP_WORDS];
776*4882a593Smuzhiyun 	u32 *bitmap32 = small_bitmap32;
777*4882a593Smuzhiyun 	bool u32_mod = false;
778*4882a593Smuzhiyun 	int ret;
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 	if (nbits > ETHNL_SMALL_BITMAP_BITS) {
781*4882a593Smuzhiyun 		unsigned int dst_words = DIV_ROUND_UP(nbits, 32);
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 		bitmap32 = kmalloc_array(dst_words, sizeof(u32), GFP_KERNEL);
784*4882a593Smuzhiyun 		if (!bitmap32)
785*4882a593Smuzhiyun 			return -ENOMEM;
786*4882a593Smuzhiyun 	}
787*4882a593Smuzhiyun 
788*4882a593Smuzhiyun 	bitmap_to_arr32(bitmap32, bitmap, nbits);
789*4882a593Smuzhiyun 	ret = ethnl_update_bitset32(bitmap32, nbits, attr, names, extack,
790*4882a593Smuzhiyun 				    &u32_mod);
791*4882a593Smuzhiyun 	if (u32_mod) {
792*4882a593Smuzhiyun 		bitmap_from_arr32(bitmap, bitmap32, nbits);
793*4882a593Smuzhiyun 		*mod = true;
794*4882a593Smuzhiyun 	}
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun 	if (nbits > ETHNL_SMALL_BITMAP_BITS)
797*4882a593Smuzhiyun 		kfree(bitmap32);
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun 	return ret;
800*4882a593Smuzhiyun }
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun #else
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun /* On little endian 64-bit and all 32-bit architectures, an unsigned long
805*4882a593Smuzhiyun  * based bitmap can be interpreted as u32 based one using a simple cast.
806*4882a593Smuzhiyun  */
807*4882a593Smuzhiyun 
ethnl_bitset_size(const unsigned long * val,const unsigned long * mask,unsigned int nbits,ethnl_string_array_t names,bool compact)808*4882a593Smuzhiyun int ethnl_bitset_size(const unsigned long *val, const unsigned long *mask,
809*4882a593Smuzhiyun 		      unsigned int nbits, ethnl_string_array_t names,
810*4882a593Smuzhiyun 		      bool compact)
811*4882a593Smuzhiyun {
812*4882a593Smuzhiyun 	return ethnl_bitset32_size((const u32 *)val, (const u32 *)mask, nbits,
813*4882a593Smuzhiyun 				   names, compact);
814*4882a593Smuzhiyun }
815*4882a593Smuzhiyun 
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)816*4882a593Smuzhiyun int ethnl_put_bitset(struct sk_buff *skb, int attrtype,
817*4882a593Smuzhiyun 		     const unsigned long *val, const unsigned long *mask,
818*4882a593Smuzhiyun 		     unsigned int nbits, ethnl_string_array_t names,
819*4882a593Smuzhiyun 		     bool compact)
820*4882a593Smuzhiyun {
821*4882a593Smuzhiyun 	return ethnl_put_bitset32(skb, attrtype, (const u32 *)val,
822*4882a593Smuzhiyun 				  (const u32 *)mask, nbits, names, compact);
823*4882a593Smuzhiyun }
824*4882a593Smuzhiyun 
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)825*4882a593Smuzhiyun int ethnl_update_bitset(unsigned long *bitmap, unsigned int nbits,
826*4882a593Smuzhiyun 			const struct nlattr *attr, ethnl_string_array_t names,
827*4882a593Smuzhiyun 			struct netlink_ext_ack *extack, bool *mod)
828*4882a593Smuzhiyun {
829*4882a593Smuzhiyun 	return ethnl_update_bitset32((u32 *)bitmap, nbits, attr, names, extack,
830*4882a593Smuzhiyun 				     mod);
831*4882a593Smuzhiyun }
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun #endif /* BITS_PER_LONG == 64 && defined(__BIG_ENDIAN) */
834