1 // SPDX-License-Identifier: GPL-2.0+
2 /* Microchip VCAP API debug file system support
3  *
4  * Copyright (c) 2022 Microchip Technology Inc. and its subsidiaries.
5  *
6  */
7 
8 #include "vcap_api_private.h"
9 #include "vcap_api_debugfs.h"
10 
11 struct vcap_admin_debugfs_info {
12 	struct vcap_control *vctrl;
13 	struct vcap_admin *admin;
14 };
15 
16 struct vcap_port_debugfs_info {
17 	struct vcap_control *vctrl;
18 	struct net_device *ndev;
19 };
20 
21 static bool vcap_bitarray_zero(int width, u8 *value)
22 {
23 	int bytes = DIV_ROUND_UP(width, BITS_PER_BYTE);
24 	u8 total = 0, bmask = 0xff;
25 	int rwidth = width;
26 	int idx;
27 
28 	for (idx = 0; idx < bytes; ++idx, rwidth -= BITS_PER_BYTE) {
29 		if (rwidth && rwidth < BITS_PER_BYTE)
30 			bmask = (1 << rwidth) - 1;
31 		total += value[idx] & bmask;
32 	}
33 	return total == 0;
34 }
35 
36 static bool vcap_get_bit(u32 *stream, struct vcap_stream_iter *itr)
37 {
38 	u32 mask = BIT(itr->reg_bitpos);
39 	u32 *p = &stream[itr->reg_idx];
40 
41 	return !!(*p & mask);
42 }
43 
44 static void vcap_decode_field(u32 *stream, struct vcap_stream_iter *itr,
45 			      int width, u8 *value)
46 {
47 	int idx;
48 
49 	/* Loop over the field value bits and get the field bits and
50 	 * set them in the output value byte array
51 	 */
52 	for (idx = 0; idx < width; idx++) {
53 		u8 bidx = idx & 0x7;
54 
55 		/* Decode one field value bit */
56 		if (vcap_get_bit(stream, itr))
57 			*value |= 1 << bidx;
58 		vcap_iter_next(itr);
59 		if (bidx == 7)
60 			value++;
61 	}
62 }
63 
64 /* Verify that the typegroup bits have the correct values */
65 static int vcap_verify_typegroups(u32 *stream, int sw_width,
66 				  const struct vcap_typegroup *tgt, bool mask,
67 				  int sw_max)
68 {
69 	struct vcap_stream_iter iter;
70 	int sw_cnt, idx;
71 
72 	vcap_iter_set(&iter, sw_width, tgt, 0);
73 	sw_cnt = 0;
74 	while (iter.tg->width) {
75 		u32 value = 0;
76 		u32 tg_value = iter.tg->value;
77 
78 		if (mask)
79 			tg_value = (1 << iter.tg->width) - 1;
80 		/* Set position to current typegroup bit */
81 		iter.offset = iter.tg->offset;
82 		vcap_iter_update(&iter);
83 		for (idx = 0; idx < iter.tg->width; idx++) {
84 			/* Decode one typegroup bit */
85 			if (vcap_get_bit(stream, &iter))
86 				value |= 1 << idx;
87 			iter.offset++;
88 			vcap_iter_update(&iter);
89 		}
90 		if (value != tg_value)
91 			return -EINVAL;
92 		iter.tg++; /* next typegroup */
93 		sw_cnt++;
94 		/* Stop checking more typegroups */
95 		if (sw_max && sw_cnt >= sw_max)
96 			break;
97 	}
98 	return 0;
99 }
100 
101 /* Find the subword width of the key typegroup that matches the stream data */
102 static int vcap_find_keystream_typegroup_sw(struct vcap_control *vctrl,
103 					    enum vcap_type vt, u32 *stream,
104 					    bool mask, int sw_max)
105 {
106 	const struct vcap_typegroup **tgt;
107 	int sw_idx, res;
108 
109 	tgt = vctrl->vcaps[vt].keyfield_set_typegroups;
110 	/* Try the longest subword match first */
111 	for (sw_idx = vctrl->vcaps[vt].sw_count; sw_idx >= 0; sw_idx--) {
112 		if (!tgt[sw_idx])
113 			continue;
114 
115 		res = vcap_verify_typegroups(stream, vctrl->vcaps[vt].sw_width,
116 					     tgt[sw_idx], mask, sw_max);
117 		if (res == 0)
118 			return sw_idx;
119 	}
120 	return -EINVAL;
121 }
122 
123 /* Find the subword width of the action typegroup that matches the stream data
124  */
125 static int vcap_find_actionstream_typegroup_sw(struct vcap_control *vctrl,
126 					       enum vcap_type vt, u32 *stream,
127 					       int sw_max)
128 {
129 	const struct vcap_typegroup **tgt;
130 	int sw_idx, res;
131 
132 	tgt = vctrl->vcaps[vt].actionfield_set_typegroups;
133 	/* Try the longest subword match first */
134 	for (sw_idx = vctrl->vcaps[vt].sw_count; sw_idx >= 0; sw_idx--) {
135 		if (!tgt[sw_idx])
136 			continue;
137 		res = vcap_verify_typegroups(stream, vctrl->vcaps[vt].act_width,
138 					     tgt[sw_idx], false, sw_max);
139 		if (res == 0)
140 			return sw_idx;
141 	}
142 	return -EINVAL;
143 }
144 
145 /* Verify that the type id in the stream matches the type id of the keyset */
146 static bool vcap_verify_keystream_keyset(struct vcap_control *vctrl,
147 					 enum vcap_type vt,
148 					 u32 *keystream,
149 					 u32 *mskstream,
150 					 enum vcap_keyfield_set keyset)
151 {
152 	const struct vcap_info *vcap = &vctrl->vcaps[vt];
153 	const struct vcap_field *typefld;
154 	const struct vcap_typegroup *tgt;
155 	const struct vcap_field *fields;
156 	struct vcap_stream_iter iter;
157 	const struct vcap_set *info;
158 	u32 value = 0;
159 	u32 mask = 0;
160 
161 	if (vcap_keyfield_count(vctrl, vt, keyset) == 0)
162 		return false;
163 
164 	info = vcap_keyfieldset(vctrl, vt, keyset);
165 	/* Check that the keyset is valid */
166 	if (!info)
167 		return false;
168 
169 	/* a type_id of value -1 means that there is no type field */
170 	if (info->type_id == (u8)-1)
171 		return true;
172 
173 	/* Get a valid typegroup for the specific keyset */
174 	tgt = vcap_keyfield_typegroup(vctrl, vt, keyset);
175 	if (!tgt)
176 		return false;
177 
178 	fields = vcap_keyfields(vctrl, vt, keyset);
179 	if (!fields)
180 		return false;
181 
182 	typefld = &fields[VCAP_KF_TYPE];
183 	vcap_iter_init(&iter, vcap->sw_width, tgt, typefld->offset);
184 	vcap_decode_field(mskstream, &iter, typefld->width, (u8 *)&mask);
185 	/* no type info if there are no mask bits */
186 	if (vcap_bitarray_zero(typefld->width, (u8 *)&mask))
187 		return false;
188 
189 	/* Get the value of the type field in the stream and compare to the
190 	 * one define in the vcap keyset
191 	 */
192 	vcap_iter_init(&iter, vcap->sw_width, tgt, typefld->offset);
193 	vcap_decode_field(keystream, &iter, typefld->width, (u8 *)&value);
194 
195 	return (value & mask) == (info->type_id & mask);
196 }
197 
198 /* Verify that the typegroup information, subword count, keyset and type id
199  * are in sync and correct, return the list of matching keysets
200  */
201 static int
202 vcap_find_keystream_keysets(struct vcap_control *vctrl,
203 			    enum vcap_type vt,
204 			    u32 *keystream,
205 			    u32 *mskstream,
206 			    bool mask, int sw_max,
207 			    struct vcap_keyset_list *kslist)
208 {
209 	const struct vcap_set *keyfield_set;
210 	int sw_count, idx;
211 
212 	sw_count = vcap_find_keystream_typegroup_sw(vctrl, vt, keystream, mask,
213 						    sw_max);
214 	if (sw_count < 0)
215 		return sw_count;
216 
217 	keyfield_set = vctrl->vcaps[vt].keyfield_set;
218 	for (idx = 0; idx < vctrl->vcaps[vt].keyfield_set_size; ++idx) {
219 		if (keyfield_set[idx].sw_per_item != sw_count)
220 			continue;
221 
222 		if (vcap_verify_keystream_keyset(vctrl, vt, keystream,
223 						 mskstream, idx))
224 			vcap_keyset_list_add(kslist, idx);
225 	}
226 	if (kslist->cnt > 0)
227 		return 0;
228 	return -EINVAL;
229 }
230 
231 /* Read key data from a VCAP address and discover if there is a rule keyset
232  * here
233  */
234 static bool
235 vcap_verify_actionstream_actionset(struct vcap_control *vctrl,
236 				   enum vcap_type vt,
237 				   u32 *actionstream,
238 				   enum vcap_actionfield_set actionset)
239 {
240 	const struct vcap_typegroup *tgt;
241 	const struct vcap_field *fields;
242 	const struct vcap_set *info;
243 
244 	if (vcap_actionfield_count(vctrl, vt, actionset) == 0)
245 		return false;
246 
247 	info = vcap_actionfieldset(vctrl, vt, actionset);
248 	/* Check that the actionset is valid */
249 	if (!info)
250 		return false;
251 
252 	/* a type_id of value -1 means that there is no type field */
253 	if (info->type_id == (u8)-1)
254 		return true;
255 
256 	/* Get a valid typegroup for the specific actionset */
257 	tgt = vcap_actionfield_typegroup(vctrl, vt, actionset);
258 	if (!tgt)
259 		return false;
260 
261 	fields = vcap_actionfields(vctrl, vt, actionset);
262 	if (!fields)
263 		return false;
264 
265 	/* Later this will be expanded with a check of the type id */
266 	return true;
267 }
268 
269 /* Verify that the typegroup information, subword count, actionset and type id
270  * are in sync and correct, return the actionset
271  */
272 static enum vcap_actionfield_set
273 vcap_find_actionstream_actionset(struct vcap_control *vctrl,
274 				 enum vcap_type vt,
275 				 u32 *stream,
276 				 int sw_max)
277 {
278 	const struct vcap_set *actionfield_set;
279 	int sw_count, idx;
280 	bool res;
281 
282 	sw_count = vcap_find_actionstream_typegroup_sw(vctrl, vt, stream,
283 						       sw_max);
284 	if (sw_count < 0)
285 		return sw_count;
286 
287 	actionfield_set = vctrl->vcaps[vt].actionfield_set;
288 	for (idx = 0; idx < vctrl->vcaps[vt].actionfield_set_size; ++idx) {
289 		if (actionfield_set[idx].sw_per_item != sw_count)
290 			continue;
291 
292 		res = vcap_verify_actionstream_actionset(vctrl, vt,
293 							 stream, idx);
294 		if (res)
295 			return idx;
296 	}
297 	return -EINVAL;
298 }
299 
300 /* Read key data from a VCAP address and discover if there are any rule keysets
301  * here
302  */
303 static int vcap_addr_keysets(struct vcap_control *vctrl,
304 			     struct net_device *ndev,
305 			     struct vcap_admin *admin,
306 			     int addr,
307 			     struct vcap_keyset_list *kslist)
308 {
309 	enum vcap_type vt = admin->vtype;
310 	int keyset_sw_regs, idx;
311 	u32 key = 0, mask = 0;
312 
313 	/* Read the cache at the specified address */
314 	keyset_sw_regs = DIV_ROUND_UP(vctrl->vcaps[vt].sw_width, 32);
315 	vctrl->ops->update(ndev, admin, VCAP_CMD_READ, VCAP_SEL_ALL, addr);
316 	vctrl->ops->cache_read(ndev, admin, VCAP_SEL_ENTRY, 0,
317 			       keyset_sw_regs);
318 	/* Skip uninitialized key/mask entries */
319 	for (idx = 0; idx < keyset_sw_regs; ++idx) {
320 		key |= ~admin->cache.keystream[idx];
321 		mask |= admin->cache.maskstream[idx];
322 	}
323 	if (key == 0 && mask == 0)
324 		return -EINVAL;
325 	/* Decode and locate the keysets */
326 	return vcap_find_keystream_keysets(vctrl, vt, admin->cache.keystream,
327 					   admin->cache.maskstream, false, 0,
328 					   kslist);
329 }
330 
331 static int vcap_read_rule(struct vcap_rule_internal *ri)
332 {
333 	struct vcap_admin *admin = ri->admin;
334 	int sw_idx, ent_idx = 0, act_idx = 0;
335 	u32 addr = ri->addr;
336 
337 	if (!ri->size || !ri->keyset_sw_regs || !ri->actionset_sw_regs) {
338 		pr_err("%s:%d: rule is empty\n", __func__, __LINE__);
339 		return -EINVAL;
340 	}
341 	vcap_erase_cache(ri);
342 	/* Use the values in the streams to read the VCAP cache */
343 	for (sw_idx = 0; sw_idx < ri->size; sw_idx++, addr++) {
344 		ri->vctrl->ops->update(ri->ndev, admin, VCAP_CMD_READ,
345 				       VCAP_SEL_ALL, addr);
346 		ri->vctrl->ops->cache_read(ri->ndev, admin,
347 					   VCAP_SEL_ENTRY, ent_idx,
348 					   ri->keyset_sw_regs);
349 		ri->vctrl->ops->cache_read(ri->ndev, admin,
350 					   VCAP_SEL_ACTION, act_idx,
351 					   ri->actionset_sw_regs);
352 		if (sw_idx == 0)
353 			ri->vctrl->ops->cache_read(ri->ndev, admin,
354 						   VCAP_SEL_COUNTER,
355 						   ri->counter_id, 0);
356 		ent_idx += ri->keyset_sw_regs;
357 		act_idx += ri->actionset_sw_regs;
358 	}
359 	return 0;
360 }
361 
362 /* Dump the keyfields value and mask values */
363 static void vcap_debugfs_show_rule_keyfield(struct vcap_control *vctrl,
364 					    struct vcap_output_print *out,
365 					    enum vcap_key_field key,
366 					    const struct vcap_field *keyfield,
367 					    u8 *value, u8 *mask)
368 {
369 	bool hex = false;
370 	int idx, bytes;
371 
372 	out->prf(out->dst, "    %s: W%d: ", vcap_keyfield_name(vctrl, key),
373 		 keyfield[key].width);
374 
375 	switch (keyfield[key].type) {
376 	case VCAP_FIELD_BIT:
377 		out->prf(out->dst, "%d/%d", value[0], mask[0]);
378 		break;
379 	case VCAP_FIELD_U32:
380 		if (key == VCAP_KF_L3_IP4_SIP || key == VCAP_KF_L3_IP4_DIP) {
381 			out->prf(out->dst, "%pI4h/%pI4h", value, mask);
382 		} else if (key == VCAP_KF_ETYPE ||
383 			   key == VCAP_KF_IF_IGR_PORT_MASK) {
384 			hex = true;
385 		} else {
386 			u32 fmsk = (1 << keyfield[key].width) - 1;
387 			u32 val = *(u32 *)value;
388 			u32 msk = *(u32 *)mask;
389 
390 			out->prf(out->dst, "%u/%u", val & fmsk, msk & fmsk);
391 		}
392 		break;
393 	case VCAP_FIELD_U48:
394 		if (key == VCAP_KF_L2_SMAC || key == VCAP_KF_L2_DMAC)
395 			out->prf(out->dst, "%pMR/%pMR", value, mask);
396 		else
397 			hex = true;
398 		break;
399 	case VCAP_FIELD_U56:
400 	case VCAP_FIELD_U64:
401 	case VCAP_FIELD_U72:
402 	case VCAP_FIELD_U112:
403 		hex = true;
404 		break;
405 	case VCAP_FIELD_U128:
406 		if (key == VCAP_KF_L3_IP6_SIP || key == VCAP_KF_L3_IP6_DIP) {
407 			u8 nvalue[16], nmask[16];
408 
409 			vcap_netbytes_copy(nvalue, value, sizeof(nvalue));
410 			vcap_netbytes_copy(nmask, mask, sizeof(nmask));
411 			out->prf(out->dst, "%pI6/%pI6", nvalue, nmask);
412 		} else {
413 			hex = true;
414 		}
415 		break;
416 	}
417 	if (hex) {
418 		bytes = DIV_ROUND_UP(keyfield[key].width, BITS_PER_BYTE);
419 		out->prf(out->dst, "0x");
420 		for (idx = 0; idx < bytes; ++idx)
421 			out->prf(out->dst, "%02x", value[bytes - idx - 1]);
422 		out->prf(out->dst, "/0x");
423 		for (idx = 0; idx < bytes; ++idx)
424 			out->prf(out->dst, "%02x", mask[bytes - idx - 1]);
425 	}
426 	out->prf(out->dst, "\n");
427 }
428 
429 static void
430 vcap_debugfs_show_rule_actionfield(struct vcap_control *vctrl,
431 				   struct vcap_output_print *out,
432 				   enum vcap_action_field action,
433 				   const struct vcap_field *actionfield,
434 				   u8 *value)
435 {
436 	bool hex = false;
437 	int idx, bytes;
438 	u32 fmsk, val;
439 
440 	out->prf(out->dst, "    %s: W%d: ",
441 		 vcap_actionfield_name(vctrl, action),
442 		 actionfield[action].width);
443 
444 	switch (actionfield[action].type) {
445 	case VCAP_FIELD_BIT:
446 		out->prf(out->dst, "%d", value[0]);
447 		break;
448 	case VCAP_FIELD_U32:
449 		fmsk = (1 << actionfield[action].width) - 1;
450 		val = *(u32 *)value;
451 		out->prf(out->dst, "%u", val & fmsk);
452 		break;
453 	case VCAP_FIELD_U48:
454 	case VCAP_FIELD_U56:
455 	case VCAP_FIELD_U64:
456 	case VCAP_FIELD_U72:
457 	case VCAP_FIELD_U112:
458 	case VCAP_FIELD_U128:
459 		hex = true;
460 		break;
461 	}
462 	if (hex) {
463 		bytes = DIV_ROUND_UP(actionfield[action].width, BITS_PER_BYTE);
464 		out->prf(out->dst, "0x");
465 		for (idx = 0; idx < bytes; ++idx)
466 			out->prf(out->dst, "%02x", value[bytes - idx - 1]);
467 	}
468 	out->prf(out->dst, "\n");
469 }
470 
471 static int vcap_debugfs_show_rule_keyset(struct vcap_rule_internal *ri,
472 					 struct vcap_output_print *out)
473 {
474 	struct vcap_control *vctrl = ri->vctrl;
475 	struct vcap_stream_iter kiter, miter;
476 	struct vcap_admin *admin = ri->admin;
477 	enum vcap_keyfield_set keysets[10];
478 	const struct vcap_field *keyfield;
479 	enum vcap_type vt = admin->vtype;
480 	const struct vcap_typegroup *tgt;
481 	struct vcap_keyset_list matches;
482 	enum vcap_keyfield_set keyset;
483 	int idx, res, keyfield_count;
484 	u32 *maskstream;
485 	u32 *keystream;
486 	u8 value[16];
487 	u8 mask[16];
488 
489 	keystream = admin->cache.keystream;
490 	maskstream = admin->cache.maskstream;
491 	matches.keysets = keysets;
492 	matches.cnt = 0;
493 	matches.max = ARRAY_SIZE(keysets);
494 	res = vcap_find_keystream_keysets(vctrl, vt, keystream, maskstream,
495 					  false, 0, &matches);
496 	if (res < 0) {
497 		pr_err("%s:%d: could not find valid keysets: %d\n",
498 		       __func__, __LINE__, res);
499 		return -EINVAL;
500 	}
501 	keyset = matches.keysets[0];
502 	out->prf(out->dst, "  keysets:");
503 	for (idx = 0; idx < matches.cnt; ++idx)
504 		out->prf(out->dst, " %s",
505 			 vcap_keyset_name(vctrl, matches.keysets[idx]));
506 	out->prf(out->dst, "\n");
507 	out->prf(out->dst, "  keyset_sw: %d\n", ri->keyset_sw);
508 	out->prf(out->dst, "  keyset_sw_regs: %d\n", ri->keyset_sw_regs);
509 	keyfield_count = vcap_keyfield_count(vctrl, vt, keyset);
510 	keyfield = vcap_keyfields(vctrl, vt, keyset);
511 	tgt = vcap_keyfield_typegroup(vctrl, vt, keyset);
512 	/* Start decoding the streams */
513 	for (idx = 0; idx < keyfield_count; ++idx) {
514 		if (keyfield[idx].width <= 0)
515 			continue;
516 		/* First get the mask */
517 		memset(mask, 0, DIV_ROUND_UP(keyfield[idx].width, 8));
518 		vcap_iter_init(&miter, vctrl->vcaps[vt].sw_width, tgt,
519 			       keyfield[idx].offset);
520 		vcap_decode_field(maskstream, &miter, keyfield[idx].width,
521 				  mask);
522 		/* Skip if no mask bits are set */
523 		if (vcap_bitarray_zero(keyfield[idx].width, mask))
524 			continue;
525 		/* Get the key */
526 		memset(value, 0, DIV_ROUND_UP(keyfield[idx].width, 8));
527 		vcap_iter_init(&kiter, vctrl->vcaps[vt].sw_width, tgt,
528 			       keyfield[idx].offset);
529 		vcap_decode_field(keystream, &kiter, keyfield[idx].width,
530 				  value);
531 		vcap_debugfs_show_rule_keyfield(vctrl, out, idx, keyfield,
532 						value, mask);
533 	}
534 	return 0;
535 }
536 
537 static int vcap_debugfs_show_rule_actionset(struct vcap_rule_internal *ri,
538 					    struct vcap_output_print *out)
539 {
540 	struct vcap_control *vctrl = ri->vctrl;
541 	struct vcap_admin *admin = ri->admin;
542 	const struct vcap_field *actionfield;
543 	enum vcap_actionfield_set actionset;
544 	enum vcap_type vt = admin->vtype;
545 	const struct vcap_typegroup *tgt;
546 	struct vcap_stream_iter iter;
547 	int idx, res, actfield_count;
548 	u32 *actstream;
549 	u8 value[16];
550 	bool no_bits;
551 
552 	actstream = admin->cache.actionstream;
553 	res = vcap_find_actionstream_actionset(vctrl, vt, actstream, 0);
554 	if (res < 0) {
555 		pr_err("%s:%d: could not find valid actionset: %d\n",
556 		       __func__, __LINE__, res);
557 		return -EINVAL;
558 	}
559 	actionset = res;
560 	out->prf(out->dst, "  actionset: %s\n",
561 		 vcap_actionset_name(vctrl, ri->data.actionset));
562 	out->prf(out->dst, "  actionset_sw: %d\n", ri->actionset_sw);
563 	out->prf(out->dst, "  actionset_sw_regs: %d\n", ri->actionset_sw_regs);
564 	actfield_count = vcap_actionfield_count(vctrl, vt, actionset);
565 	actionfield = vcap_actionfields(vctrl, vt, actionset);
566 	tgt = vcap_actionfield_typegroup(vctrl, vt, actionset);
567 	/* Start decoding the stream */
568 	for (idx = 0; idx < actfield_count; ++idx) {
569 		if (actionfield[idx].width <= 0)
570 			continue;
571 		/* Get the action */
572 		memset(value, 0, DIV_ROUND_UP(actionfield[idx].width, 8));
573 		vcap_iter_init(&iter, vctrl->vcaps[vt].act_width, tgt,
574 			       actionfield[idx].offset);
575 		vcap_decode_field(actstream, &iter, actionfield[idx].width,
576 				  value);
577 		/* Skip if no bits are set */
578 		no_bits = vcap_bitarray_zero(actionfield[idx].width, value);
579 		if (no_bits)
580 			continue;
581 		/* Later the action id will also be checked */
582 		vcap_debugfs_show_rule_actionfield(vctrl, out, idx, actionfield,
583 						   value);
584 	}
585 	return 0;
586 }
587 
588 static void vcap_show_admin_rule(struct vcap_control *vctrl,
589 				 struct vcap_admin *admin,
590 				 struct vcap_output_print *out,
591 				 struct vcap_rule_internal *ri)
592 {
593 	ri->counter.value = admin->cache.counter;
594 	ri->counter.sticky = admin->cache.sticky;
595 	out->prf(out->dst,
596 		 "rule: %u, addr: [%d,%d], X%d, ctr[%d]: %d, hit: %d\n",
597 		 ri->data.id, ri->addr, ri->addr + ri->size - 1, ri->size,
598 		 ri->counter_id, ri->counter.value, ri->counter.sticky);
599 	out->prf(out->dst, "  chain_id: %d\n", ri->data.vcap_chain_id);
600 	out->prf(out->dst, "  user: %d\n", ri->data.user);
601 	out->prf(out->dst, "  priority: %d\n", ri->data.priority);
602 	vcap_debugfs_show_rule_keyset(ri, out);
603 	vcap_debugfs_show_rule_actionset(ri, out);
604 }
605 
606 static void vcap_show_admin_info(struct vcap_control *vctrl,
607 				 struct vcap_admin *admin,
608 				 struct vcap_output_print *out)
609 {
610 	const struct vcap_info *vcap = &vctrl->vcaps[admin->vtype];
611 
612 	out->prf(out->dst, "name: %s\n", vcap->name);
613 	out->prf(out->dst, "rows: %d\n", vcap->rows);
614 	out->prf(out->dst, "sw_count: %d\n", vcap->sw_count);
615 	out->prf(out->dst, "sw_width: %d\n", vcap->sw_width);
616 	out->prf(out->dst, "sticky_width: %d\n", vcap->sticky_width);
617 	out->prf(out->dst, "act_width: %d\n", vcap->act_width);
618 	out->prf(out->dst, "default_cnt: %d\n", vcap->default_cnt);
619 	out->prf(out->dst, "require_cnt_dis: %d\n", vcap->require_cnt_dis);
620 	out->prf(out->dst, "version: %d\n", vcap->version);
621 	out->prf(out->dst, "vtype: %d\n", admin->vtype);
622 	out->prf(out->dst, "vinst: %d\n", admin->vinst);
623 	out->prf(out->dst, "first_cid: %d\n", admin->first_cid);
624 	out->prf(out->dst, "last_cid: %d\n", admin->last_cid);
625 	out->prf(out->dst, "lookups: %d\n", admin->lookups);
626 	out->prf(out->dst, "first_valid_addr: %d\n", admin->first_valid_addr);
627 	out->prf(out->dst, "last_valid_addr: %d\n", admin->last_valid_addr);
628 	out->prf(out->dst, "last_used_addr: %d\n", admin->last_used_addr);
629 }
630 
631 static int vcap_show_admin(struct vcap_control *vctrl,
632 			   struct vcap_admin *admin,
633 			   struct vcap_output_print *out)
634 {
635 	struct vcap_rule_internal *elem, *ri;
636 	int ret = 0;
637 
638 	vcap_show_admin_info(vctrl, admin, out);
639 	mutex_lock(&admin->lock);
640 	list_for_each_entry(elem, &admin->rules, list) {
641 		ri = vcap_dup_rule(elem);
642 		if (IS_ERR(ri))
643 			goto free_rule;
644 		/* Read data from VCAP */
645 		ret = vcap_read_rule(ri);
646 		if (ret)
647 			goto free_rule;
648 		out->prf(out->dst, "\n");
649 		vcap_show_admin_rule(vctrl, admin, out, ri);
650 free_rule:
651 		vcap_free_rule((struct vcap_rule *)ri);
652 	}
653 	mutex_unlock(&admin->lock);
654 	return ret;
655 }
656 
657 static int vcap_show_admin_raw(struct vcap_control *vctrl,
658 			       struct vcap_admin *admin,
659 			       struct vcap_output_print *out)
660 {
661 	enum vcap_keyfield_set keysets[10];
662 	enum vcap_type vt = admin->vtype;
663 	struct vcap_keyset_list kslist;
664 	struct vcap_rule_internal *ri;
665 	const struct vcap_set *info;
666 	int addr, idx;
667 	int ret;
668 
669 	if (list_empty(&admin->rules))
670 		return 0;
671 
672 	ret = vcap_api_check(vctrl);
673 	if (ret)
674 		return ret;
675 
676 	ri = list_first_entry(&admin->rules, struct vcap_rule_internal, list);
677 
678 	/* Go from higher to lower addresses searching for a keyset */
679 	kslist.keysets = keysets;
680 	kslist.max = ARRAY_SIZE(keysets);
681 	for (addr = admin->last_valid_addr; addr >= admin->first_valid_addr;
682 	     --addr) {
683 		kslist.cnt = 0;
684 		ret = vcap_addr_keysets(vctrl, ri->ndev, admin, addr, &kslist);
685 		if (ret < 0)
686 			continue;
687 		info = vcap_keyfieldset(vctrl, vt, kslist.keysets[0]);
688 		if (!info)
689 			continue;
690 		if (addr % info->sw_per_item) {
691 			pr_info("addr: %d X%d error rule, keyset: %s\n",
692 				addr,
693 				info->sw_per_item,
694 				vcap_keyset_name(vctrl, kslist.keysets[0]));
695 		} else {
696 			out->prf(out->dst, "  addr: %d, X%d rule, keysets:",
697 				 addr,
698 				 info->sw_per_item);
699 			for (idx = 0; idx < kslist.cnt; ++idx)
700 				out->prf(out->dst, " %s",
701 					 vcap_keyset_name(vctrl,
702 							  kslist.keysets[idx]));
703 			out->prf(out->dst, "\n");
704 		}
705 	}
706 	return 0;
707 }
708 
709 /* Show the port configuration and status */
710 static int vcap_port_debugfs_show(struct seq_file *m, void *unused)
711 {
712 	struct vcap_port_debugfs_info *info = m->private;
713 	struct vcap_admin *admin;
714 	struct vcap_output_print out = {
715 		.prf = (void *)seq_printf,
716 		.dst = m,
717 	};
718 
719 	list_for_each_entry(admin, &info->vctrl->list, list) {
720 		if (admin->vinst)
721 			continue;
722 		info->vctrl->ops->port_info(info->ndev, admin, &out);
723 	}
724 	return 0;
725 }
726 DEFINE_SHOW_ATTRIBUTE(vcap_port_debugfs);
727 
728 void vcap_port_debugfs(struct device *dev, struct dentry *parent,
729 		       struct vcap_control *vctrl,
730 		       struct net_device *ndev)
731 {
732 	struct vcap_port_debugfs_info *info;
733 
734 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
735 	if (!info)
736 		return;
737 
738 	info->vctrl = vctrl;
739 	info->ndev = ndev;
740 	debugfs_create_file(netdev_name(ndev), 0444, parent, info,
741 			    &vcap_port_debugfs_fops);
742 }
743 EXPORT_SYMBOL_GPL(vcap_port_debugfs);
744 
745 /* Show the full VCAP instance data (rules with all fields) */
746 static int vcap_debugfs_show(struct seq_file *m, void *unused)
747 {
748 	struct vcap_admin_debugfs_info *info = m->private;
749 	struct vcap_output_print out = {
750 		.prf = (void *)seq_printf,
751 		.dst = m,
752 	};
753 
754 	return vcap_show_admin(info->vctrl, info->admin, &out);
755 }
756 DEFINE_SHOW_ATTRIBUTE(vcap_debugfs);
757 
758 /* Show the raw VCAP instance data (rules with address info) */
759 static int vcap_raw_debugfs_show(struct seq_file *m, void *unused)
760 {
761 	struct vcap_admin_debugfs_info *info = m->private;
762 	struct vcap_output_print out = {
763 		.prf = (void *)seq_printf,
764 		.dst = m,
765 	};
766 
767 	return vcap_show_admin_raw(info->vctrl, info->admin, &out);
768 }
769 DEFINE_SHOW_ATTRIBUTE(vcap_raw_debugfs);
770 
771 struct dentry *vcap_debugfs(struct device *dev, struct dentry *parent,
772 			    struct vcap_control *vctrl)
773 {
774 	struct vcap_admin_debugfs_info *info;
775 	struct vcap_admin *admin;
776 	struct dentry *dir;
777 	char name[50];
778 
779 	dir = debugfs_create_dir("vcaps", parent);
780 	if (PTR_ERR_OR_ZERO(dir))
781 		return NULL;
782 	list_for_each_entry(admin, &vctrl->list, list) {
783 		sprintf(name, "raw_%s_%d", vctrl->vcaps[admin->vtype].name,
784 			admin->vinst);
785 		info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
786 		if (!info)
787 			return NULL;
788 		info->vctrl = vctrl;
789 		info->admin = admin;
790 		debugfs_create_file(name, 0444, dir, info,
791 				    &vcap_raw_debugfs_fops);
792 		sprintf(name, "%s_%d", vctrl->vcaps[admin->vtype].name,
793 			admin->vinst);
794 		debugfs_create_file(name, 0444, dir, info, &vcap_debugfs_fops);
795 	}
796 	return dir;
797 }
798 EXPORT_SYMBOL_GPL(vcap_debugfs);
799 
800 #ifdef CONFIG_VCAP_KUNIT_TEST
801 #include "vcap_api_debugfs_kunit.c"
802 #endif
803