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