xref: /linux/drivers/crypto/intel/iaa/iaa_crypto_stats.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2021 Intel Corporation. All rights rsvd. */
3 
4 #include <linux/module.h>
5 #include <linux/kernel.h>
6 #include <linux/highmem.h>
7 #include <linux/mm.h>
8 #include <linux/slab.h>
9 #include <linux/delay.h>
10 #include <linux/smp.h>
11 #include <uapi/linux/idxd.h>
12 #include <linux/idxd.h>
13 #include <linux/dmaengine.h>
14 #include "../../dma/idxd/idxd.h"
15 #include <linux/debugfs.h>
16 #include <crypto/internal/acompress.h>
17 #include "iaa_crypto.h"
18 #include "iaa_crypto_stats.h"
19 
20 static atomic64_t total_comp_calls;
21 static atomic64_t total_decomp_calls;
22 static atomic64_t total_sw_comp_calls;
23 static atomic64_t total_sw_decomp_calls;
24 static atomic64_t total_comp_bytes_out;
25 static atomic64_t total_decomp_bytes_in;
26 static atomic64_t total_completion_einval_errors;
27 static atomic64_t total_completion_timeout_errors;
28 static atomic64_t total_completion_comp_buf_overflow_errors;
29 
30 static struct dentry *iaa_crypto_debugfs_root;
31 
32 void update_total_comp_calls(void)
33 {
34 	atomic64_inc(&total_comp_calls);
35 }
36 
37 void update_total_comp_bytes_out(int n)
38 {
39 	atomic64_add(n, &total_comp_bytes_out);
40 }
41 
42 void update_total_decomp_calls(void)
43 {
44 	atomic64_inc(&total_decomp_calls);
45 }
46 
47 void update_total_sw_comp_calls(void)
48 {
49 	atomic64_inc(&total_sw_comp_calls);
50 }
51 
52 void update_total_sw_decomp_calls(void)
53 {
54 	atomic64_inc(&total_sw_decomp_calls);
55 }
56 
57 void update_total_decomp_bytes_in(int n)
58 {
59 	atomic64_add(n, &total_decomp_bytes_in);
60 }
61 
62 void update_completion_einval_errs(void)
63 {
64 	atomic64_inc(&total_completion_einval_errors);
65 }
66 
67 void update_completion_timeout_errs(void)
68 {
69 	atomic64_inc(&total_completion_timeout_errors);
70 }
71 
72 void update_completion_comp_buf_overflow_errs(void)
73 {
74 	atomic64_inc(&total_completion_comp_buf_overflow_errors);
75 }
76 
77 void update_wq_comp_calls(struct idxd_wq *idxd_wq)
78 {
79 	struct iaa_wq *wq = idxd_wq_get_private(idxd_wq);
80 
81 	atomic64_inc(&wq->comp_calls);
82 	atomic64_inc(&wq->iaa_device->comp_calls);
83 }
84 
85 void update_wq_comp_bytes(struct idxd_wq *idxd_wq, int n)
86 {
87 	struct iaa_wq *wq = idxd_wq_get_private(idxd_wq);
88 
89 	atomic64_add(n, &wq->comp_bytes);
90 	atomic64_add(n, &wq->iaa_device->comp_bytes);
91 }
92 
93 void update_wq_decomp_calls(struct idxd_wq *idxd_wq)
94 {
95 	struct iaa_wq *wq = idxd_wq_get_private(idxd_wq);
96 
97 	atomic64_inc(&wq->decomp_calls);
98 	atomic64_inc(&wq->iaa_device->decomp_calls);
99 }
100 
101 void update_wq_decomp_bytes(struct idxd_wq *idxd_wq, int n)
102 {
103 	struct iaa_wq *wq = idxd_wq_get_private(idxd_wq);
104 
105 	atomic64_add(n, &wq->decomp_bytes);
106 	atomic64_add(n, &wq->iaa_device->decomp_bytes);
107 }
108 
109 static void reset_iaa_crypto_stats(void)
110 {
111 	atomic64_set(&total_comp_calls, 0);
112 	atomic64_set(&total_decomp_calls, 0);
113 	atomic64_set(&total_sw_comp_calls, 0);
114 	atomic64_set(&total_sw_decomp_calls, 0);
115 	atomic64_set(&total_comp_bytes_out, 0);
116 	atomic64_set(&total_decomp_bytes_in, 0);
117 	atomic64_set(&total_completion_einval_errors, 0);
118 	atomic64_set(&total_completion_timeout_errors, 0);
119 	atomic64_set(&total_completion_comp_buf_overflow_errors, 0);
120 }
121 
122 static void reset_wq_stats(struct iaa_wq *wq)
123 {
124 	atomic64_set(&wq->comp_calls, 0);
125 	atomic64_set(&wq->comp_bytes, 0);
126 	atomic64_set(&wq->decomp_calls, 0);
127 	atomic64_set(&wq->decomp_bytes, 0);
128 }
129 
130 static void reset_device_stats(struct iaa_device *iaa_device)
131 {
132 	struct iaa_wq *iaa_wq;
133 
134 	atomic64_set(&iaa_device->comp_calls, 0);
135 	atomic64_set(&iaa_device->comp_bytes, 0);
136 	atomic64_set(&iaa_device->decomp_calls, 0);
137 	atomic64_set(&iaa_device->decomp_bytes, 0);
138 
139 	list_for_each_entry(iaa_wq, &iaa_device->wqs, list)
140 		reset_wq_stats(iaa_wq);
141 }
142 
143 static void wq_show(struct seq_file *m, struct iaa_wq *iaa_wq)
144 {
145 	seq_printf(m, "    name: %s\n", iaa_wq->wq->name);
146 	seq_printf(m, "    comp_calls: %llu\n",
147 		   atomic64_read(&iaa_wq->comp_calls));
148 	seq_printf(m, "    comp_bytes: %llu\n",
149 		   atomic64_read(&iaa_wq->comp_bytes));
150 	seq_printf(m, "    decomp_calls: %llu\n",
151 		   atomic64_read(&iaa_wq->decomp_calls));
152 	seq_printf(m, "    decomp_bytes: %llu\n\n",
153 		   atomic64_read(&iaa_wq->decomp_bytes));
154 }
155 
156 static void device_stats_show(struct seq_file *m, struct iaa_device *iaa_device)
157 {
158 	struct iaa_wq *iaa_wq;
159 
160 	seq_puts(m, "iaa device:\n");
161 	seq_printf(m, "  id: %d\n", iaa_device->idxd->id);
162 	seq_printf(m, "  n_wqs: %d\n", iaa_device->n_wq);
163 	seq_printf(m, "  comp_calls: %llu\n",
164 		   atomic64_read(&iaa_device->comp_calls));
165 	seq_printf(m, "  comp_bytes: %llu\n",
166 		   atomic64_read(&iaa_device->comp_bytes));
167 	seq_printf(m, "  decomp_calls: %llu\n",
168 		   atomic64_read(&iaa_device->decomp_calls));
169 	seq_printf(m, "  decomp_bytes: %llu\n",
170 		   atomic64_read(&iaa_device->decomp_bytes));
171 	seq_puts(m, "  wqs:\n");
172 
173 	list_for_each_entry(iaa_wq, &iaa_device->wqs, list)
174 		wq_show(m, iaa_wq);
175 }
176 
177 static int global_stats_show(struct seq_file *m, void *v)
178 {
179 	seq_puts(m, "global stats:\n");
180 	seq_printf(m, "  total_comp_calls: %llu\n",
181 		   atomic64_read(&total_comp_calls));
182 	seq_printf(m, "  total_decomp_calls: %llu\n",
183 		   atomic64_read(&total_decomp_calls));
184 	seq_printf(m, "  total_sw_comp_calls: %llu\n",
185 		   atomic64_read(&total_sw_comp_calls));
186 	seq_printf(m, "  total_sw_decomp_calls: %llu\n",
187 		   atomic64_read(&total_sw_decomp_calls));
188 	seq_printf(m, "  total_comp_bytes_out: %llu\n",
189 		   atomic64_read(&total_comp_bytes_out));
190 	seq_printf(m, "  total_decomp_bytes_in: %llu\n",
191 		   atomic64_read(&total_decomp_bytes_in));
192 	seq_printf(m, "  total_completion_einval_errors: %llu\n",
193 		   atomic64_read(&total_completion_einval_errors));
194 	seq_printf(m, "  total_completion_timeout_errors: %llu\n",
195 		   atomic64_read(&total_completion_timeout_errors));
196 	seq_printf(m, "  total_completion_comp_buf_overflow_errors: %llu\n\n",
197 		   atomic64_read(&total_completion_comp_buf_overflow_errors));
198 
199 	return 0;
200 }
201 
202 static int wq_stats_show(struct seq_file *m, void *v)
203 {
204 	struct iaa_device *iaa_device;
205 
206 	mutex_lock(&iaa_devices_lock);
207 
208 	list_for_each_entry(iaa_device, &iaa_devices, list)
209 		device_stats_show(m, iaa_device);
210 
211 	mutex_unlock(&iaa_devices_lock);
212 
213 	return 0;
214 }
215 
216 static int iaa_crypto_stats_reset(void *data, u64 value)
217 {
218 	struct iaa_device *iaa_device;
219 
220 	reset_iaa_crypto_stats();
221 
222 	mutex_lock(&iaa_devices_lock);
223 
224 	list_for_each_entry(iaa_device, &iaa_devices, list)
225 		reset_device_stats(iaa_device);
226 
227 	mutex_unlock(&iaa_devices_lock);
228 
229 	return 0;
230 }
231 
232 static int wq_stats_open(struct inode *inode, struct file *file)
233 {
234 	return single_open(file, wq_stats_show, file);
235 }
236 
237 static const struct file_operations wq_stats_fops = {
238 	.open = wq_stats_open,
239 	.read = seq_read,
240 	.llseek = seq_lseek,
241 	.release = single_release,
242 };
243 
244 static int global_stats_open(struct inode *inode, struct file *file)
245 {
246 	return single_open(file, global_stats_show, file);
247 }
248 
249 static const struct file_operations global_stats_fops = {
250 	.open = global_stats_open,
251 	.read = seq_read,
252 	.llseek = seq_lseek,
253 	.release = single_release,
254 };
255 
256 DEFINE_DEBUGFS_ATTRIBUTE(wq_stats_reset_fops, NULL, iaa_crypto_stats_reset, "%llu\n");
257 
258 int __init iaa_crypto_debugfs_init(void)
259 {
260 	if (!debugfs_initialized())
261 		return -ENODEV;
262 
263 	iaa_crypto_debugfs_root = debugfs_create_dir("iaa_crypto", NULL);
264 
265 	debugfs_create_file("global_stats", 0644, iaa_crypto_debugfs_root, NULL,
266 			    &global_stats_fops);
267 	debugfs_create_file("wq_stats", 0644, iaa_crypto_debugfs_root, NULL,
268 			    &wq_stats_fops);
269 	debugfs_create_file("stats_reset", 0644, iaa_crypto_debugfs_root, NULL,
270 			    &wq_stats_reset_fops);
271 
272 	return 0;
273 }
274 
275 void __exit iaa_crypto_debugfs_cleanup(void)
276 {
277 	debugfs_remove_recursive(iaa_crypto_debugfs_root);
278 }
279 
280 MODULE_LICENSE("GPL");
281