xref: /linux/drivers/scsi/snic/snic_debugfs.c (revision 4d38b88fd17e9989429e65420bf3c33ca53b2085)
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright 2014 Cisco Systems, Inc.  All rights reserved.
3 
4 #include <linux/module.h>
5 #include <linux/errno.h>
6 #include <linux/debugfs.h>
7 
8 #include "snic.h"
9 
10 /*
11  * snic_debugfs_init - Initialize debugfs for snic debug logging
12  *
13  * Description:
14  * When Debugfs is configured this routine sets up fnic debugfs
15  * filesystem. If not already created. this routine will crate the
16  * fnic directory and statistics directory for trace buffer and
17  * stats logging
18  */
snic_debugfs_init(void)19 void snic_debugfs_init(void)
20 {
21 	snic_glob->trc_root = debugfs_create_dir("snic", NULL);
22 
23 	snic_glob->stats_root = debugfs_create_dir("statistics",
24 						   snic_glob->trc_root);
25 }
26 
27 /*
28  * snic_debugfs_term - Tear down debugfs intrastructure
29  *
30  * Description:
31  * When Debufs is configured this routine removes debugfs file system
32  * elements that are specific to snic
33  */
34 void
snic_debugfs_term(void)35 snic_debugfs_term(void)
36 {
37 	debugfs_remove(snic_glob->stats_root);
38 	snic_glob->stats_root = NULL;
39 
40 	debugfs_remove(snic_glob->trc_root);
41 	snic_glob->trc_root = NULL;
42 }
43 
44 /*
45  * snic_reset_stats_open - Open the reset_stats file
46  */
47 static int
snic_reset_stats_open(struct inode * inode,struct file * filp)48 snic_reset_stats_open(struct inode *inode, struct file *filp)
49 {
50 	SNIC_BUG_ON(!inode->i_private);
51 	filp->private_data = inode->i_private;
52 
53 	return 0;
54 }
55 
56 /*
57  * snic_reset_stats_read - Read a reset_stats debugfs file
58  * @filp: The file pointer to read from.
59  * @ubuf: The buffer tocopy the data to.
60  * @cnt: The number of bytes to read.
61  * @ppos: The position in the file to start reading frm.
62  *
63  * Description:
64  * This routine reads value of variable reset_stats
65  * and stores into local @buf. It will start reading file @ppos and
66  * copy up to @cnt of data to @ubuf from @buf.
67  *
68  * Returns:
69  * This function returns the amount of data that was read.
70  */
71 static ssize_t
snic_reset_stats_read(struct file * filp,char __user * ubuf,size_t cnt,loff_t * ppos)72 snic_reset_stats_read(struct file *filp,
73 		      char __user *ubuf,
74 		      size_t cnt,
75 		      loff_t *ppos)
76 {
77 	struct snic *snic = (struct snic *) filp->private_data;
78 	char buf[64];
79 	int len;
80 
81 	len = sprintf(buf, "%u\n", snic->reset_stats);
82 
83 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
84 }
85 
86 /*
87  * snic_reset_stats_write - Write to reset_stats debugfs file
88  * @filp: The file pointer to write from
89  * @ubuf: The buffer to copy the data from.
90  * @cnt: The number of bytes to write.
91  * @ppos: The position in the file to start writing to.
92  *
93  * Description:
94  * This routine writes data from user buffer @ubuf to buffer @buf and
95  * resets cumulative stats of snic.
96  *
97  * Returns:
98  * This function returns the amount of data that was written.
99  */
100 static ssize_t
snic_reset_stats_write(struct file * filp,const char __user * ubuf,size_t cnt,loff_t * ppos)101 snic_reset_stats_write(struct file *filp,
102 		       const char __user *ubuf,
103 		       size_t cnt,
104 		       loff_t *ppos)
105 {
106 	struct snic *snic = (struct snic *) filp->private_data;
107 	struct snic_stats *stats = &snic->s_stats;
108 	u64 *io_stats_p = (u64 *) &stats->io;
109 	u64 *fw_stats_p = (u64 *) &stats->fw;
110 	char buf[64];
111 	unsigned long val;
112 	int ret;
113 
114 	if (cnt >= sizeof(buf))
115 		return -EINVAL;
116 
117 	if (copy_from_user(&buf, ubuf, cnt))
118 		return -EFAULT;
119 
120 	buf[cnt] = '\0';
121 
122 	ret = kstrtoul(buf, 10, &val);
123 	if (ret < 0)
124 		return ret;
125 
126 	snic->reset_stats = val;
127 
128 	if (snic->reset_stats) {
129 		/* Skip variable is used to avoid descrepancies to Num IOs
130 		 * and IO Completions stats. Skip incrementing No IO Compls
131 		 * for pending active IOs after reset_stats
132 		 */
133 		atomic64_set(&snic->io_cmpl_skip,
134 			     atomic64_read(&stats->io.active));
135 		memset(&stats->abts, 0, sizeof(struct snic_abort_stats));
136 		memset(&stats->reset, 0, sizeof(struct snic_reset_stats));
137 		memset(&stats->misc, 0, sizeof(struct snic_misc_stats));
138 		memset(io_stats_p+1,
139 			0,
140 			sizeof(struct snic_io_stats) - sizeof(u64));
141 		memset(fw_stats_p+1,
142 			0,
143 			sizeof(struct snic_fw_stats) - sizeof(u64));
144 	}
145 
146 	(*ppos)++;
147 
148 	SNIC_HOST_INFO(snic->shost, "Reset Op: Driver statistics.\n");
149 
150 	return cnt;
151 }
152 
153 static int
snic_reset_stats_release(struct inode * inode,struct file * filp)154 snic_reset_stats_release(struct inode *inode, struct file *filp)
155 {
156 	filp->private_data = NULL;
157 
158 	return 0;
159 }
160 
161 /*
162  * snic_stats_show - Formats and prints per host specific driver stats.
163  */
164 static int
snic_stats_show(struct seq_file * sfp,void * data)165 snic_stats_show(struct seq_file *sfp, void *data)
166 {
167 	struct snic *snic = (struct snic *) sfp->private;
168 	struct snic_stats *stats = &snic->s_stats;
169 	struct timespec64 last_isr_tms, last_ack_tms;
170 	u64 maxio_tm;
171 	int i;
172 
173 	/* Dump IO Stats */
174 	seq_printf(sfp,
175 		   "------------------------------------------\n"
176 		   "\t\t IO Statistics\n"
177 		   "------------------------------------------\n");
178 
179 	maxio_tm = (u64) atomic64_read(&stats->io.max_time);
180 	seq_printf(sfp,
181 		   "Active IOs                  : %lld\n"
182 		   "Max Active IOs              : %lld\n"
183 		   "Total IOs                   : %lld\n"
184 		   "IOs Completed               : %lld\n"
185 		   "IOs Failed                  : %lld\n"
186 		   "IOs Not Found               : %lld\n"
187 		   "Memory Alloc Failures       : %lld\n"
188 		   "REQs Null                   : %lld\n"
189 		   "SCSI Cmd Pointers Null      : %lld\n"
190 		   "Max SGL for any IO          : %lld\n"
191 		   "Max IO Size                 : %lld Sectors\n"
192 		   "Max Queuing Time            : %lld\n"
193 		   "Max Completion Time         : %lld\n"
194 		   "Max IO Process Time(FW)     : %lld (%u msec)\n",
195 		   (u64) atomic64_read(&stats->io.active),
196 		   (u64) atomic64_read(&stats->io.max_active),
197 		   (u64) atomic64_read(&stats->io.num_ios),
198 		   (u64) atomic64_read(&stats->io.compl),
199 		   (u64) atomic64_read(&stats->io.fail),
200 		   (u64) atomic64_read(&stats->io.io_not_found),
201 		   (u64) atomic64_read(&stats->io.alloc_fail),
202 		   (u64) atomic64_read(&stats->io.req_null),
203 		   (u64) atomic64_read(&stats->io.sc_null),
204 		   (u64) atomic64_read(&stats->io.max_sgl),
205 		   (u64) atomic64_read(&stats->io.max_io_sz),
206 		   (u64) atomic64_read(&stats->io.max_qtime),
207 		   (u64) atomic64_read(&stats->io.max_cmpl_time),
208 		   maxio_tm,
209 		   jiffies_to_msecs(maxio_tm));
210 
211 	seq_puts(sfp, "\nSGL Counters\n");
212 
213 	for (i = 0; i < SNIC_MAX_SG_DESC_CNT; i++) {
214 		seq_printf(sfp,
215 			   "%10lld ",
216 			   (u64) atomic64_read(&stats->io.sgl_cnt[i]));
217 
218 		if ((i + 1) % 8 == 0)
219 			seq_puts(sfp, "\n");
220 	}
221 
222 	/* Dump Abort Stats */
223 	seq_printf(sfp,
224 		   "\n-------------------------------------------\n"
225 		   "\t\t Abort Statistics\n"
226 		   "---------------------------------------------\n");
227 
228 	seq_printf(sfp,
229 		   "Aborts                      : %lld\n"
230 		   "Aborts Fail                 : %lld\n"
231 		   "Aborts Driver Timeout       : %lld\n"
232 		   "Abort FW Timeout            : %lld\n"
233 		   "Abort IO NOT Found          : %lld\n"
234 		   "Abort Queuing Failed        : %lld\n",
235 		   (u64) atomic64_read(&stats->abts.num),
236 		   (u64) atomic64_read(&stats->abts.fail),
237 		   (u64) atomic64_read(&stats->abts.drv_tmo),
238 		   (u64) atomic64_read(&stats->abts.fw_tmo),
239 		   (u64) atomic64_read(&stats->abts.io_not_found),
240 		   (u64) atomic64_read(&stats->abts.q_fail));
241 
242 	/* Dump Reset Stats */
243 	seq_printf(sfp,
244 		   "\n-------------------------------------------\n"
245 		   "\t\t Reset Statistics\n"
246 		   "---------------------------------------------\n");
247 
248 	seq_printf(sfp,
249 		   "HBA Resets                  : %lld\n"
250 		   "HBA Reset Cmpls             : %lld\n"
251 		   "HBA Reset Fail              : %lld\n",
252 		   (u64) atomic64_read(&stats->reset.hba_resets),
253 		   (u64) atomic64_read(&stats->reset.hba_reset_cmpl),
254 		   (u64) atomic64_read(&stats->reset.hba_reset_fail));
255 
256 	/* Dump Firmware Stats */
257 	seq_printf(sfp,
258 		   "\n-------------------------------------------\n"
259 		   "\t\t Firmware Statistics\n"
260 		   "---------------------------------------------\n");
261 
262 	seq_printf(sfp,
263 		"Active FW Requests             : %lld\n"
264 		"Max FW Requests                : %lld\n"
265 		"FW Out Of Resource Errs        : %lld\n"
266 		"FW IO Errors                   : %lld\n"
267 		"FW SCSI Errors                 : %lld\n",
268 		(u64) atomic64_read(&stats->fw.actv_reqs),
269 		(u64) atomic64_read(&stats->fw.max_actv_reqs),
270 		(u64) atomic64_read(&stats->fw.out_of_res),
271 		(u64) atomic64_read(&stats->fw.io_errs),
272 		(u64) atomic64_read(&stats->fw.scsi_errs));
273 
274 
275 	/* Dump Miscellenous Stats */
276 	seq_printf(sfp,
277 		   "\n---------------------------------------------\n"
278 		   "\t\t Other Statistics\n"
279 		   "\n---------------------------------------------\n");
280 
281 	jiffies_to_timespec64(stats->misc.last_isr_time, &last_isr_tms);
282 	jiffies_to_timespec64(stats->misc.last_ack_time, &last_ack_tms);
283 
284 	seq_printf(sfp,
285 		   "Last ISR Time               : %llu (%ptSp)\n"
286 		   "Last Ack Time               : %llu (%ptSp)\n"
287 		   "Ack ISRs                    : %llu\n"
288 		   "IO Cmpl ISRs                : %llu\n"
289 		   "Err Notify ISRs             : %llu\n"
290 		   "Max CQ Entries              : %lld\n"
291 		   "Data Count Mismatch         : %lld\n"
292 		   "IOs w/ Timeout Status       : %lld\n"
293 		   "IOs w/ Aborted Status       : %lld\n"
294 		   "IOs w/ SGL Invalid Stat     : %lld\n"
295 		   "WQ Desc Alloc Fail          : %lld\n"
296 		   "Queue Full                  : %lld\n"
297 		   "Queue Ramp Up               : %lld\n"
298 		   "Queue Ramp Down             : %lld\n"
299 		   "Queue Last Queue Depth      : %lld\n"
300 		   "Target Not Ready            : %lld\n",
301 		   (u64) stats->misc.last_isr_time, &last_isr_tms,
302 		   (u64) stats->misc.last_ack_time, &last_ack_tms,
303 		   (u64) atomic64_read(&stats->misc.ack_isr_cnt),
304 		   (u64) atomic64_read(&stats->misc.cmpl_isr_cnt),
305 		   (u64) atomic64_read(&stats->misc.errnotify_isr_cnt),
306 		   (u64) atomic64_read(&stats->misc.max_cq_ents),
307 		   (u64) atomic64_read(&stats->misc.data_cnt_mismat),
308 		   (u64) atomic64_read(&stats->misc.io_tmo),
309 		   (u64) atomic64_read(&stats->misc.io_aborted),
310 		   (u64) atomic64_read(&stats->misc.sgl_inval),
311 		   (u64) atomic64_read(&stats->misc.wq_alloc_fail),
312 		   (u64) atomic64_read(&stats->misc.qfull),
313 		   (u64) atomic64_read(&stats->misc.qsz_rampup),
314 		   (u64) atomic64_read(&stats->misc.qsz_rampdown),
315 		   (u64) atomic64_read(&stats->misc.last_qsz),
316 		   (u64) atomic64_read(&stats->misc.tgt_not_rdy));
317 
318 	return 0;
319 }
320 
321 DEFINE_SHOW_ATTRIBUTE(snic_stats);
322 
323 static const struct file_operations snic_reset_stats_fops = {
324 	.owner = THIS_MODULE,
325 	.open = snic_reset_stats_open,
326 	.read = snic_reset_stats_read,
327 	.write = snic_reset_stats_write,
328 	.release = snic_reset_stats_release,
329 };
330 
331 /*
332  * snic_stats_init - Initialize stats struct and create stats file
333  * per snic
334  *
335  * Description:
336  * When debugfs is cofigured this routine sets up the stats file per snic
337  * It will create file stats and reset_stats under statistics/host# directory
338  * to log per snic stats
339  */
snic_stats_debugfs_init(struct snic * snic)340 void snic_stats_debugfs_init(struct snic *snic)
341 {
342 	char name[16];
343 
344 	snprintf(name, sizeof(name), "host%d", snic->shost->host_no);
345 
346 	snic->stats_host = debugfs_create_dir(name, snic_glob->stats_root);
347 
348 	snic->stats_file = debugfs_create_file("stats", S_IFREG|S_IRUGO,
349 					       snic->stats_host, snic,
350 					       &snic_stats_fops);
351 
352 	snic->reset_stats_file = debugfs_create_file("reset_stats",
353 						     S_IFREG|S_IRUGO|S_IWUSR,
354 						     snic->stats_host, snic,
355 						     &snic_reset_stats_fops);
356 }
357 
358 /*
359  * snic_stats_debugfs_remove - Tear down debugfs infrastructure of stats
360  *
361  * Description:
362  * When Debufs is configured this routine removes debugfs file system
363  * elements that are specific to to snic stats
364  */
365 void
snic_stats_debugfs_remove(struct snic * snic)366 snic_stats_debugfs_remove(struct snic *snic)
367 {
368 	debugfs_remove(snic->stats_file);
369 	snic->stats_file = NULL;
370 
371 	debugfs_remove(snic->reset_stats_file);
372 	snic->reset_stats_file = NULL;
373 
374 	debugfs_remove(snic->stats_host);
375 	snic->stats_host = NULL;
376 }
377 
378 /* Trace Facility related API */
379 static void *
snic_trc_seq_start(struct seq_file * sfp,loff_t * pos)380 snic_trc_seq_start(struct seq_file *sfp, loff_t *pos)
381 {
382 	return &snic_glob->trc;
383 }
384 
385 static void *
snic_trc_seq_next(struct seq_file * sfp,void * data,loff_t * pos)386 snic_trc_seq_next(struct seq_file *sfp, void *data, loff_t *pos)
387 {
388 	return NULL;
389 }
390 
391 static void
snic_trc_seq_stop(struct seq_file * sfp,void * data)392 snic_trc_seq_stop(struct seq_file *sfp, void *data)
393 {
394 }
395 
396 #define SNIC_TRC_PBLEN	256
397 static int
snic_trc_seq_show(struct seq_file * sfp,void * data)398 snic_trc_seq_show(struct seq_file *sfp, void *data)
399 {
400 	char buf[SNIC_TRC_PBLEN];
401 
402 	if (snic_get_trc_data(buf, SNIC_TRC_PBLEN) > 0)
403 		seq_printf(sfp, "%s\n", buf);
404 
405 	return 0;
406 }
407 
408 static const struct seq_operations snic_trc_sops = {
409 	.start	= snic_trc_seq_start,
410 	.next	= snic_trc_seq_next,
411 	.stop	= snic_trc_seq_stop,
412 	.show	= snic_trc_seq_show,
413 };
414 
415 DEFINE_SEQ_ATTRIBUTE(snic_trc);
416 
417 #define TRC_ENABLE_FILE	"tracing_enable"
418 #define TRC_FILE	"trace"
419 /*
420  * snic_trc_debugfs_init : creates trace/tracing_enable files for trace
421  * under debugfs
422  */
snic_trc_debugfs_init(void)423 void snic_trc_debugfs_init(void)
424 {
425 	debugfs_create_bool(TRC_ENABLE_FILE, S_IFREG | S_IRUGO | S_IWUSR,
426 			    snic_glob->trc_root, &snic_glob->trc.enable);
427 
428 	debugfs_create_file(TRC_FILE, S_IFREG | S_IRUGO | S_IWUSR,
429 			    snic_glob->trc_root, NULL, &snic_trc_fops);
430 }
431 
432 /*
433  * snic_trc_debugfs_term : cleans up the files created for trace under debugfs
434  */
435 void
snic_trc_debugfs_term(void)436 snic_trc_debugfs_term(void)
437 {
438 	debugfs_lookup_and_remove(TRC_FILE, snic_glob->trc_root);
439 	debugfs_lookup_and_remove(TRC_ENABLE_FILE, snic_glob->trc_root);
440 }
441