xref: /openbmc/linux/drivers/net/ethernet/mellanox/mlxsw/core_acl_flex_keys.c (revision 278002edb19bce2c628fafb0af936e77000f3a5b)
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2017-2018 Mellanox Technologies. All rights reserved */
3 
4 #include <linux/kernel.h>
5 #include <linux/slab.h>
6 #include <linux/list.h>
7 #include <linux/errno.h>
8 #include <linux/refcount.h>
9 
10 #include "item.h"
11 #include "core_acl_flex_keys.h"
12 
13 /* For the purpose of the driver, define an internal storage scratchpad
14  * that will be used to store key/mask values. For each defined element type
15  * define an internal storage geometry.
16  *
17  * When adding new elements, MLXSW_AFK_ELEMENT_STORAGE_SIZE must be increased
18  * accordingly.
19  */
20 static const struct mlxsw_afk_element_info mlxsw_afk_element_infos[] = {
21 	MLXSW_AFK_ELEMENT_INFO_U32(SRC_SYS_PORT, 0x00, 16, 16),
22 	MLXSW_AFK_ELEMENT_INFO_BUF(DMAC_32_47, 0x04, 2),
23 	MLXSW_AFK_ELEMENT_INFO_BUF(DMAC_0_31, 0x06, 4),
24 	MLXSW_AFK_ELEMENT_INFO_BUF(SMAC_32_47, 0x0A, 2),
25 	MLXSW_AFK_ELEMENT_INFO_BUF(SMAC_0_31, 0x0C, 4),
26 	MLXSW_AFK_ELEMENT_INFO_U32(ETHERTYPE, 0x00, 0, 16),
27 	MLXSW_AFK_ELEMENT_INFO_U32(IP_PROTO, 0x10, 0, 8),
28 	MLXSW_AFK_ELEMENT_INFO_U32(VID, 0x10, 8, 12),
29 	MLXSW_AFK_ELEMENT_INFO_U32(PCP, 0x10, 20, 3),
30 	MLXSW_AFK_ELEMENT_INFO_U32(TCP_FLAGS, 0x10, 23, 9),
31 	MLXSW_AFK_ELEMENT_INFO_U32(DST_L4_PORT, 0x14, 0, 16),
32 	MLXSW_AFK_ELEMENT_INFO_U32(SRC_L4_PORT, 0x14, 16, 16),
33 	MLXSW_AFK_ELEMENT_INFO_U32(IP_TTL_, 0x18, 0, 8),
34 	MLXSW_AFK_ELEMENT_INFO_U32(IP_ECN, 0x18, 9, 2),
35 	MLXSW_AFK_ELEMENT_INFO_U32(IP_DSCP, 0x18, 11, 6),
36 	MLXSW_AFK_ELEMENT_INFO_U32(VIRT_ROUTER, 0x18, 17, 12),
37 	MLXSW_AFK_ELEMENT_INFO_BUF(SRC_IP_96_127, 0x20, 4),
38 	MLXSW_AFK_ELEMENT_INFO_BUF(SRC_IP_64_95, 0x24, 4),
39 	MLXSW_AFK_ELEMENT_INFO_BUF(SRC_IP_32_63, 0x28, 4),
40 	MLXSW_AFK_ELEMENT_INFO_BUF(SRC_IP_0_31, 0x2C, 4),
41 	MLXSW_AFK_ELEMENT_INFO_BUF(DST_IP_96_127, 0x30, 4),
42 	MLXSW_AFK_ELEMENT_INFO_BUF(DST_IP_64_95, 0x34, 4),
43 	MLXSW_AFK_ELEMENT_INFO_BUF(DST_IP_32_63, 0x38, 4),
44 	MLXSW_AFK_ELEMENT_INFO_BUF(DST_IP_0_31, 0x3C, 4),
45 	MLXSW_AFK_ELEMENT_INFO_U32(FDB_MISS, 0x40, 0, 1),
46 	MLXSW_AFK_ELEMENT_INFO_U32(L4_PORT_RANGE, 0x40, 1, 16),
47 	MLXSW_AFK_ELEMENT_INFO_U32(VIRT_ROUTER_0_3, 0x40, 17, 4),
48 	MLXSW_AFK_ELEMENT_INFO_U32(VIRT_ROUTER_4_7, 0x40, 21, 4),
49 	MLXSW_AFK_ELEMENT_INFO_U32(VIRT_ROUTER_MSB, 0x40, 25, 4),
50 };
51 
52 struct mlxsw_afk {
53 	struct list_head key_info_list;
54 	unsigned int max_blocks;
55 	const struct mlxsw_afk_ops *ops;
56 	const struct mlxsw_afk_block *blocks;
57 	unsigned int blocks_count;
58 };
59 
mlxsw_afk_blocks_check(struct mlxsw_afk * mlxsw_afk)60 static bool mlxsw_afk_blocks_check(struct mlxsw_afk *mlxsw_afk)
61 {
62 	int i;
63 	int j;
64 
65 	for (i = 0; i < mlxsw_afk->blocks_count; i++) {
66 		const struct mlxsw_afk_block *block = &mlxsw_afk->blocks[i];
67 
68 		for (j = 0; j < block->instances_count; j++) {
69 			const struct mlxsw_afk_element_info *elinfo;
70 			const struct mlxsw_afk_element_inst *elinst;
71 
72 			elinst = &block->instances[j];
73 			elinfo = &mlxsw_afk_element_infos[elinst->element];
74 			if (elinst->type != elinfo->type ||
75 			    (!elinst->avoid_size_check &&
76 			     elinst->item.size.bits !=
77 			     elinfo->item.size.bits))
78 				return false;
79 		}
80 	}
81 	return true;
82 }
83 
mlxsw_afk_create(unsigned int max_blocks,const struct mlxsw_afk_ops * ops)84 struct mlxsw_afk *mlxsw_afk_create(unsigned int max_blocks,
85 				   const struct mlxsw_afk_ops *ops)
86 {
87 	struct mlxsw_afk *mlxsw_afk;
88 
89 	mlxsw_afk = kzalloc(sizeof(*mlxsw_afk), GFP_KERNEL);
90 	if (!mlxsw_afk)
91 		return NULL;
92 	INIT_LIST_HEAD(&mlxsw_afk->key_info_list);
93 	mlxsw_afk->max_blocks = max_blocks;
94 	mlxsw_afk->ops = ops;
95 	mlxsw_afk->blocks = ops->blocks;
96 	mlxsw_afk->blocks_count = ops->blocks_count;
97 	WARN_ON(!mlxsw_afk_blocks_check(mlxsw_afk));
98 	return mlxsw_afk;
99 }
100 EXPORT_SYMBOL(mlxsw_afk_create);
101 
mlxsw_afk_destroy(struct mlxsw_afk * mlxsw_afk)102 void mlxsw_afk_destroy(struct mlxsw_afk *mlxsw_afk)
103 {
104 	WARN_ON(!list_empty(&mlxsw_afk->key_info_list));
105 	kfree(mlxsw_afk);
106 }
107 EXPORT_SYMBOL(mlxsw_afk_destroy);
108 
109 struct mlxsw_afk_key_info {
110 	struct list_head list;
111 	refcount_t ref_count;
112 	unsigned int blocks_count;
113 	int element_to_block[MLXSW_AFK_ELEMENT_MAX]; /* index is element, value
114 						      * is index inside "blocks"
115 						      */
116 	struct mlxsw_afk_element_usage elusage;
117 	const struct mlxsw_afk_block *blocks[];
118 };
119 
120 static bool
mlxsw_afk_key_info_elements_eq(struct mlxsw_afk_key_info * key_info,struct mlxsw_afk_element_usage * elusage)121 mlxsw_afk_key_info_elements_eq(struct mlxsw_afk_key_info *key_info,
122 			       struct mlxsw_afk_element_usage *elusage)
123 {
124 	return memcmp(&key_info->elusage, elusage, sizeof(*elusage)) == 0;
125 }
126 
127 static struct mlxsw_afk_key_info *
mlxsw_afk_key_info_find(struct mlxsw_afk * mlxsw_afk,struct mlxsw_afk_element_usage * elusage)128 mlxsw_afk_key_info_find(struct mlxsw_afk *mlxsw_afk,
129 			struct mlxsw_afk_element_usage *elusage)
130 {
131 	struct mlxsw_afk_key_info *key_info;
132 
133 	list_for_each_entry(key_info, &mlxsw_afk->key_info_list, list) {
134 		if (mlxsw_afk_key_info_elements_eq(key_info, elusage))
135 			return key_info;
136 	}
137 	return NULL;
138 }
139 
140 struct mlxsw_afk_picker {
141 	DECLARE_BITMAP(element, MLXSW_AFK_ELEMENT_MAX);
142 	unsigned int total;
143 };
144 
mlxsw_afk_picker_count_hits(struct mlxsw_afk * mlxsw_afk,struct mlxsw_afk_picker * picker,enum mlxsw_afk_element element)145 static void mlxsw_afk_picker_count_hits(struct mlxsw_afk *mlxsw_afk,
146 					struct mlxsw_afk_picker *picker,
147 					enum mlxsw_afk_element element)
148 {
149 	int i;
150 	int j;
151 
152 	for (i = 0; i < mlxsw_afk->blocks_count; i++) {
153 		const struct mlxsw_afk_block *block = &mlxsw_afk->blocks[i];
154 
155 		for (j = 0; j < block->instances_count; j++) {
156 			const struct mlxsw_afk_element_inst *elinst;
157 
158 			elinst = &block->instances[j];
159 			if (elinst->element == element) {
160 				__set_bit(element, picker[i].element);
161 				picker[i].total++;
162 			}
163 		}
164 	}
165 }
166 
mlxsw_afk_picker_subtract_hits(struct mlxsw_afk * mlxsw_afk,struct mlxsw_afk_picker * picker,int block_index)167 static void mlxsw_afk_picker_subtract_hits(struct mlxsw_afk *mlxsw_afk,
168 					   struct mlxsw_afk_picker *picker,
169 					   int block_index)
170 {
171 	DECLARE_BITMAP(hits_element, MLXSW_AFK_ELEMENT_MAX);
172 	int i;
173 	int j;
174 
175 	memcpy(&hits_element, &picker[block_index].element,
176 	       sizeof(hits_element));
177 
178 	for (i = 0; i < mlxsw_afk->blocks_count; i++) {
179 		for_each_set_bit(j, hits_element, MLXSW_AFK_ELEMENT_MAX) {
180 			if (__test_and_clear_bit(j, picker[i].element))
181 				picker[i].total--;
182 		}
183 	}
184 }
185 
mlxsw_afk_picker_most_hits_get(struct mlxsw_afk * mlxsw_afk,struct mlxsw_afk_picker * picker)186 static int mlxsw_afk_picker_most_hits_get(struct mlxsw_afk *mlxsw_afk,
187 					  struct mlxsw_afk_picker *picker)
188 {
189 	int most_index = -EINVAL; /* Should never happen to return this */
190 	int most_hits = 0;
191 	int i;
192 
193 	for (i = 0; i < mlxsw_afk->blocks_count; i++) {
194 		if (picker[i].total > most_hits) {
195 			most_hits = picker[i].total;
196 			most_index = i;
197 		}
198 	}
199 	return most_index;
200 }
201 
mlxsw_afk_picker_key_info_add(struct mlxsw_afk * mlxsw_afk,struct mlxsw_afk_picker * picker,int block_index,struct mlxsw_afk_key_info * key_info)202 static int mlxsw_afk_picker_key_info_add(struct mlxsw_afk *mlxsw_afk,
203 					 struct mlxsw_afk_picker *picker,
204 					 int block_index,
205 					 struct mlxsw_afk_key_info *key_info)
206 {
207 	enum mlxsw_afk_element element;
208 
209 	if (key_info->blocks_count == mlxsw_afk->max_blocks)
210 		return -EINVAL;
211 
212 	for_each_set_bit(element, picker[block_index].element,
213 			 MLXSW_AFK_ELEMENT_MAX) {
214 		key_info->element_to_block[element] = key_info->blocks_count;
215 		mlxsw_afk_element_usage_add(&key_info->elusage, element);
216 	}
217 
218 	key_info->blocks[key_info->blocks_count] =
219 					&mlxsw_afk->blocks[block_index];
220 	key_info->blocks_count++;
221 	return 0;
222 }
223 
mlxsw_afk_picker(struct mlxsw_afk * mlxsw_afk,struct mlxsw_afk_key_info * key_info,struct mlxsw_afk_element_usage * elusage)224 static int mlxsw_afk_picker(struct mlxsw_afk *mlxsw_afk,
225 			    struct mlxsw_afk_key_info *key_info,
226 			    struct mlxsw_afk_element_usage *elusage)
227 {
228 	struct mlxsw_afk_picker *picker;
229 	enum mlxsw_afk_element element;
230 	int err;
231 
232 	picker = kcalloc(mlxsw_afk->blocks_count, sizeof(*picker), GFP_KERNEL);
233 	if (!picker)
234 		return -ENOMEM;
235 
236 	/* Since the same elements could be present in multiple blocks,
237 	 * we must find out optimal block list in order to make the
238 	 * block count as low as possible.
239 	 *
240 	 * First, we count hits. We go over all available blocks and count
241 	 * how many of requested elements are covered by each.
242 	 *
243 	 * Then in loop, we find block with most hits and add it to
244 	 * output key_info. Then we have to subtract this block hits so
245 	 * the next iteration will find most suitable block for
246 	 * the rest of requested elements.
247 	 */
248 
249 	mlxsw_afk_element_usage_for_each(element, elusage)
250 		mlxsw_afk_picker_count_hits(mlxsw_afk, picker, element);
251 
252 	do {
253 		int block_index;
254 
255 		block_index = mlxsw_afk_picker_most_hits_get(mlxsw_afk, picker);
256 		if (block_index < 0) {
257 			err = block_index;
258 			goto out;
259 		}
260 		err = mlxsw_afk_picker_key_info_add(mlxsw_afk, picker,
261 						    block_index, key_info);
262 		if (err)
263 			goto out;
264 		mlxsw_afk_picker_subtract_hits(mlxsw_afk, picker, block_index);
265 	} while (!mlxsw_afk_key_info_elements_eq(key_info, elusage));
266 
267 	err = 0;
268 out:
269 	kfree(picker);
270 	return err;
271 }
272 
273 static struct mlxsw_afk_key_info *
mlxsw_afk_key_info_create(struct mlxsw_afk * mlxsw_afk,struct mlxsw_afk_element_usage * elusage)274 mlxsw_afk_key_info_create(struct mlxsw_afk *mlxsw_afk,
275 			  struct mlxsw_afk_element_usage *elusage)
276 {
277 	struct mlxsw_afk_key_info *key_info;
278 	int err;
279 
280 	key_info = kzalloc(struct_size(key_info, blocks, mlxsw_afk->max_blocks),
281 			   GFP_KERNEL);
282 	if (!key_info)
283 		return ERR_PTR(-ENOMEM);
284 	err = mlxsw_afk_picker(mlxsw_afk, key_info, elusage);
285 	if (err)
286 		goto err_picker;
287 	list_add(&key_info->list, &mlxsw_afk->key_info_list);
288 	refcount_set(&key_info->ref_count, 1);
289 	return key_info;
290 
291 err_picker:
292 	kfree(key_info);
293 	return ERR_PTR(err);
294 }
295 
mlxsw_afk_key_info_destroy(struct mlxsw_afk_key_info * key_info)296 static void mlxsw_afk_key_info_destroy(struct mlxsw_afk_key_info *key_info)
297 {
298 	list_del(&key_info->list);
299 	kfree(key_info);
300 }
301 
302 struct mlxsw_afk_key_info *
mlxsw_afk_key_info_get(struct mlxsw_afk * mlxsw_afk,struct mlxsw_afk_element_usage * elusage)303 mlxsw_afk_key_info_get(struct mlxsw_afk *mlxsw_afk,
304 		       struct mlxsw_afk_element_usage *elusage)
305 {
306 	struct mlxsw_afk_key_info *key_info;
307 
308 	key_info = mlxsw_afk_key_info_find(mlxsw_afk, elusage);
309 	if (key_info) {
310 		refcount_inc(&key_info->ref_count);
311 		return key_info;
312 	}
313 	return mlxsw_afk_key_info_create(mlxsw_afk, elusage);
314 }
315 EXPORT_SYMBOL(mlxsw_afk_key_info_get);
316 
mlxsw_afk_key_info_put(struct mlxsw_afk_key_info * key_info)317 void mlxsw_afk_key_info_put(struct mlxsw_afk_key_info *key_info)
318 {
319 	if (!refcount_dec_and_test(&key_info->ref_count))
320 		return;
321 	mlxsw_afk_key_info_destroy(key_info);
322 }
323 EXPORT_SYMBOL(mlxsw_afk_key_info_put);
324 
mlxsw_afk_key_info_subset(struct mlxsw_afk_key_info * key_info,struct mlxsw_afk_element_usage * elusage)325 bool mlxsw_afk_key_info_subset(struct mlxsw_afk_key_info *key_info,
326 			       struct mlxsw_afk_element_usage *elusage)
327 {
328 	return mlxsw_afk_element_usage_subset(elusage, &key_info->elusage);
329 }
330 EXPORT_SYMBOL(mlxsw_afk_key_info_subset);
331 
332 static const struct mlxsw_afk_element_inst *
mlxsw_afk_block_elinst_get(const struct mlxsw_afk_block * block,enum mlxsw_afk_element element)333 mlxsw_afk_block_elinst_get(const struct mlxsw_afk_block *block,
334 			   enum mlxsw_afk_element element)
335 {
336 	int i;
337 
338 	for (i = 0; i < block->instances_count; i++) {
339 		const struct mlxsw_afk_element_inst *elinst;
340 
341 		elinst = &block->instances[i];
342 		if (elinst->element == element)
343 			return elinst;
344 	}
345 	return NULL;
346 }
347 
348 static const struct mlxsw_afk_element_inst *
mlxsw_afk_key_info_elinst_get(struct mlxsw_afk_key_info * key_info,enum mlxsw_afk_element element,int * p_block_index)349 mlxsw_afk_key_info_elinst_get(struct mlxsw_afk_key_info *key_info,
350 			      enum mlxsw_afk_element element,
351 			      int *p_block_index)
352 {
353 	const struct mlxsw_afk_element_inst *elinst;
354 	const struct mlxsw_afk_block *block;
355 	int block_index;
356 
357 	if (WARN_ON(!test_bit(element, key_info->elusage.usage)))
358 		return NULL;
359 	block_index = key_info->element_to_block[element];
360 	block = key_info->blocks[block_index];
361 
362 	elinst = mlxsw_afk_block_elinst_get(block, element);
363 	if (WARN_ON(!elinst))
364 		return NULL;
365 
366 	*p_block_index = block_index;
367 	return elinst;
368 }
369 
370 u16
mlxsw_afk_key_info_block_encoding_get(const struct mlxsw_afk_key_info * key_info,int block_index)371 mlxsw_afk_key_info_block_encoding_get(const struct mlxsw_afk_key_info *key_info,
372 				      int block_index)
373 {
374 	return key_info->blocks[block_index]->encoding;
375 }
376 EXPORT_SYMBOL(mlxsw_afk_key_info_block_encoding_get);
377 
378 unsigned int
mlxsw_afk_key_info_blocks_count_get(const struct mlxsw_afk_key_info * key_info)379 mlxsw_afk_key_info_blocks_count_get(const struct mlxsw_afk_key_info *key_info)
380 {
381 	return key_info->blocks_count;
382 }
383 EXPORT_SYMBOL(mlxsw_afk_key_info_blocks_count_get);
384 
mlxsw_afk_values_add_u32(struct mlxsw_afk_element_values * values,enum mlxsw_afk_element element,u32 key_value,u32 mask_value)385 void mlxsw_afk_values_add_u32(struct mlxsw_afk_element_values *values,
386 			      enum mlxsw_afk_element element,
387 			      u32 key_value, u32 mask_value)
388 {
389 	const struct mlxsw_afk_element_info *elinfo =
390 				&mlxsw_afk_element_infos[element];
391 	const struct mlxsw_item *storage_item = &elinfo->item;
392 
393 	if (!mask_value)
394 		return;
395 	if (WARN_ON(elinfo->type != MLXSW_AFK_ELEMENT_TYPE_U32))
396 		return;
397 	__mlxsw_item_set32(values->storage.key, storage_item, 0, key_value);
398 	__mlxsw_item_set32(values->storage.mask, storage_item, 0, mask_value);
399 	mlxsw_afk_element_usage_add(&values->elusage, element);
400 }
401 EXPORT_SYMBOL(mlxsw_afk_values_add_u32);
402 
mlxsw_afk_values_add_buf(struct mlxsw_afk_element_values * values,enum mlxsw_afk_element element,const char * key_value,const char * mask_value,unsigned int len)403 void mlxsw_afk_values_add_buf(struct mlxsw_afk_element_values *values,
404 			      enum mlxsw_afk_element element,
405 			      const char *key_value, const char *mask_value,
406 			      unsigned int len)
407 {
408 	const struct mlxsw_afk_element_info *elinfo =
409 				&mlxsw_afk_element_infos[element];
410 	const struct mlxsw_item *storage_item = &elinfo->item;
411 
412 	if (!memchr_inv(mask_value, 0, len)) /* If mask is zero */
413 		return;
414 	if (WARN_ON(elinfo->type != MLXSW_AFK_ELEMENT_TYPE_BUF) ||
415 	    WARN_ON(elinfo->item.size.bytes != len))
416 		return;
417 	__mlxsw_item_memcpy_to(values->storage.key, key_value,
418 			       storage_item, 0);
419 	__mlxsw_item_memcpy_to(values->storage.mask, mask_value,
420 			       storage_item, 0);
421 	mlxsw_afk_element_usage_add(&values->elusage, element);
422 }
423 EXPORT_SYMBOL(mlxsw_afk_values_add_buf);
424 
mlxsw_sp_afk_encode_u32(const struct mlxsw_item * storage_item,const struct mlxsw_item * output_item,char * storage,char * output,int diff)425 static void mlxsw_sp_afk_encode_u32(const struct mlxsw_item *storage_item,
426 				    const struct mlxsw_item *output_item,
427 				    char *storage, char *output, int diff)
428 {
429 	u32 value;
430 
431 	value = __mlxsw_item_get32(storage, storage_item, 0);
432 	__mlxsw_item_set32(output, output_item, 0, value + diff);
433 }
434 
mlxsw_sp_afk_encode_buf(const struct mlxsw_item * storage_item,const struct mlxsw_item * output_item,char * storage,char * output)435 static void mlxsw_sp_afk_encode_buf(const struct mlxsw_item *storage_item,
436 				    const struct mlxsw_item *output_item,
437 				    char *storage, char *output)
438 {
439 	char *storage_data = __mlxsw_item_data(storage, storage_item, 0);
440 	char *output_data = __mlxsw_item_data(output, output_item, 0);
441 	size_t len = output_item->size.bytes;
442 
443 	memcpy(output_data, storage_data, len);
444 }
445 
446 static void
mlxsw_sp_afk_encode_one(const struct mlxsw_afk_element_inst * elinst,char * output,char * storage,int u32_diff)447 mlxsw_sp_afk_encode_one(const struct mlxsw_afk_element_inst *elinst,
448 			char *output, char *storage, int u32_diff)
449 {
450 	const struct mlxsw_item *output_item = &elinst->item;
451 	const struct mlxsw_afk_element_info *elinfo;
452 	const struct mlxsw_item *storage_item;
453 
454 	elinfo = &mlxsw_afk_element_infos[elinst->element];
455 	storage_item = &elinfo->item;
456 	if (elinst->type == MLXSW_AFK_ELEMENT_TYPE_U32)
457 		mlxsw_sp_afk_encode_u32(storage_item, output_item,
458 					storage, output, u32_diff);
459 	else if (elinst->type == MLXSW_AFK_ELEMENT_TYPE_BUF)
460 		mlxsw_sp_afk_encode_buf(storage_item, output_item,
461 					storage, output);
462 }
463 
464 #define MLXSW_SP_AFK_KEY_BLOCK_MAX_SIZE 16
465 
mlxsw_afk_encode(struct mlxsw_afk * mlxsw_afk,struct mlxsw_afk_key_info * key_info,struct mlxsw_afk_element_values * values,char * key,char * mask)466 void mlxsw_afk_encode(struct mlxsw_afk *mlxsw_afk,
467 		      struct mlxsw_afk_key_info *key_info,
468 		      struct mlxsw_afk_element_values *values,
469 		      char *key, char *mask)
470 {
471 	unsigned int blocks_count =
472 			mlxsw_afk_key_info_blocks_count_get(key_info);
473 	char block_mask[MLXSW_SP_AFK_KEY_BLOCK_MAX_SIZE];
474 	char block_key[MLXSW_SP_AFK_KEY_BLOCK_MAX_SIZE];
475 	const struct mlxsw_afk_element_inst *elinst;
476 	enum mlxsw_afk_element element;
477 	int block_index, i;
478 
479 	for (i = 0; i < blocks_count; i++) {
480 		memset(block_key, 0, MLXSW_SP_AFK_KEY_BLOCK_MAX_SIZE);
481 		memset(block_mask, 0, MLXSW_SP_AFK_KEY_BLOCK_MAX_SIZE);
482 
483 		mlxsw_afk_element_usage_for_each(element, &values->elusage) {
484 			elinst = mlxsw_afk_key_info_elinst_get(key_info,
485 							       element,
486 							       &block_index);
487 			if (!elinst || block_index != i)
488 				continue;
489 
490 			mlxsw_sp_afk_encode_one(elinst, block_key,
491 						values->storage.key,
492 						elinst->u32_key_diff);
493 			mlxsw_sp_afk_encode_one(elinst, block_mask,
494 						values->storage.mask, 0);
495 		}
496 
497 		mlxsw_afk->ops->encode_block(key, i, block_key);
498 		mlxsw_afk->ops->encode_block(mask, i, block_mask);
499 	}
500 }
501 EXPORT_SYMBOL(mlxsw_afk_encode);
502 
mlxsw_afk_clear(struct mlxsw_afk * mlxsw_afk,char * key,int block_start,int block_end)503 void mlxsw_afk_clear(struct mlxsw_afk *mlxsw_afk, char *key,
504 		     int block_start, int block_end)
505 {
506 	int i;
507 
508 	for (i = block_start; i <= block_end; i++)
509 		mlxsw_afk->ops->clear_block(key, i);
510 }
511 EXPORT_SYMBOL(mlxsw_afk_clear);
512