xref: /freebsd/sys/dev/qat/qat_hw/qat_c4xxx/adf_c4xxx_misc_error_stats.c (revision 8aa51e6d7de0a828020de64560d1385e15955a1c)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright(c) 2007-2025 Intel Corporation */
3 #include "adf_c4xxx_hw_data.h"
4 #include "adf_c4xxx_misc_error_stats.h"
5 #include "adf_common_drv.h"
6 #include "adf_cfg_common.h"
7 #include <sys/sbuf.h>
8 #include <sys/sysctl.h>
9 #include <sys/priv.h>
10 
11 #define MISC_ERROR_DBG_FILE "misc_error_stats"
12 #define LINE                                                                   \
13 	"+-----------------------------------------------------------------+\n"
14 #define BANNER                                                                 \
15 	"|          Miscellaneous Error Statistics for Qat Device          |\n"
16 
17 static void *misc_counter;
18 
19 struct adf_dev_miscellaneous_stats {
20 	u64 misc_counter;
21 };
22 
qat_misc_error_show(SYSCTL_HANDLER_ARGS)23 static int qat_misc_error_show(SYSCTL_HANDLER_ARGS)
24 {
25 	struct sbuf sb;
26 
27 	if (priv_check(curthread, PRIV_DRIVER) != 0)
28 		return EPERM;
29 
30 	sbuf_new_for_sysctl(&sb, NULL, 256, req);
31 	sbuf_printf(&sb, "\n");
32 	sbuf_printf(&sb, LINE);
33 	sbuf_printf(&sb,
34 		    "| Miscellaneous Error:   %40llu |\n",
35 		    (unsigned long long)((struct adf_dev_miscellaneous_stats *)
36 					     misc_counter)
37 			->misc_counter);
38 
39 	sbuf_finish(&sb);
40 	SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
41 	sbuf_delete(&sb);
42 
43 	return 0;
44 }
45 
46 /**
47  * adf_misc_error_add_c4xxx() - Create debugfs entry for
48  * acceleration device Freq counters.
49  * @accel_dev:  Pointer to acceleration device.
50  *
51  * Return: 0 on success, error code otherwise.
52  */
53 int
adf_misc_error_add_c4xxx(struct adf_accel_dev * accel_dev)54 adf_misc_error_add_c4xxx(struct adf_accel_dev *accel_dev)
55 {
56 	struct sysctl_ctx_list *qat_sysctl_ctx = NULL;
57 	struct sysctl_oid *qat_sysctl_tree = NULL;
58 	struct sysctl_oid *misc_er_file = NULL;
59 
60 	qat_sysctl_ctx =
61 	    device_get_sysctl_ctx(accel_dev->accel_pci_dev.pci_dev);
62 	qat_sysctl_tree =
63 	    device_get_sysctl_tree(accel_dev->accel_pci_dev.pci_dev);
64 
65 	misc_er_file = SYSCTL_ADD_PROC(qat_sysctl_ctx,
66 				       SYSCTL_CHILDREN(qat_sysctl_tree),
67 				       OID_AUTO,
68 				       MISC_ERROR_DBG_FILE,
69 				       CTLTYPE_STRING | CTLFLAG_RD,
70 				       accel_dev,
71 				       0,
72 				       qat_misc_error_show,
73 				       "A",
74 				       "QAT Miscellaneous Error Statistics");
75 	accel_dev->misc_error_dbgfile = misc_er_file;
76 	if (!accel_dev->misc_error_dbgfile) {
77 		device_printf(
78 		    GET_DEV(accel_dev),
79 		    "Failed to create qat miscellaneous error debugfs entry.\n");
80 		return ENOENT;
81 	}
82 
83 	misc_counter = kmalloc(PAGE_SIZE, GFP_KERNEL);
84 	if (!misc_counter)
85 		return ENOMEM;
86 
87 	memset(misc_counter, 0, PAGE_SIZE);
88 
89 	return 0;
90 }
91 
92 /**
93  * adf_misc_error_remove_c4xxx() - Remove debugfs entry for
94  * acceleration device misc error counter.
95  * @accel_dev:  Pointer to acceleration device.
96  *
97  * Return: void
98  */
99 void
adf_misc_error_remove_c4xxx(struct adf_accel_dev * accel_dev)100 adf_misc_error_remove_c4xxx(struct adf_accel_dev *accel_dev)
101 {
102 	if (accel_dev->misc_error_dbgfile) {
103 		remove_oid(accel_dev, accel_dev->misc_error_dbgfile);
104 		accel_dev->misc_error_dbgfile = NULL;
105 	}
106 
107 	kfree(misc_counter);
108 	misc_counter = NULL;
109 }
110