Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / scsi / fnic / fnic_debugfs.c
blob2619a2d4f5f143bf742d78fb9594f0c468559c3b
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright 2012 Cisco Systems, Inc. All rights reserved.
4 #include <linux/module.h>
5 #include <linux/errno.h>
6 #include <linux/debugfs.h>
7 #include <linux/vmalloc.h>
8 #include "fnic.h"
10 static struct dentry *fnic_trace_debugfs_root;
11 static struct dentry *fnic_trace_debugfs_file;
12 static struct dentry *fnic_trace_enable;
13 static struct dentry *fnic_stats_debugfs_root;
15 static struct dentry *fnic_fc_trace_debugfs_file;
16 static struct dentry *fnic_fc_rdata_trace_debugfs_file;
17 static struct dentry *fnic_fc_trace_enable;
18 static struct dentry *fnic_fc_trace_clear;
20 struct fc_trace_flag_type {
21 u8 fc_row_file;
22 u8 fc_normal_file;
23 u8 fnic_trace;
24 u8 fc_trace;
25 u8 fc_clear;
28 static struct fc_trace_flag_type *fc_trc_flag;
31 * fnic_debugfs_init - Initialize debugfs for fnic debug logging
33 * Description:
34 * When Debugfs is configured this routine sets up the fnic debugfs
35 * file system. If not already created, this routine will create the
36 * fnic directory and statistics directory for trace buffer and
37 * stats logging.
39 int fnic_debugfs_init(void)
41 fnic_trace_debugfs_root = debugfs_create_dir("fnic", NULL);
43 fnic_stats_debugfs_root = debugfs_create_dir("statistics",
44 fnic_trace_debugfs_root);
46 /* Allocate memory to structure */
47 fc_trc_flag = vmalloc(sizeof(struct fc_trace_flag_type));
49 if (fc_trc_flag) {
50 fc_trc_flag->fc_row_file = 0;
51 fc_trc_flag->fc_normal_file = 1;
52 fc_trc_flag->fnic_trace = 2;
53 fc_trc_flag->fc_trace = 3;
54 fc_trc_flag->fc_clear = 4;
55 return 0;
58 return -ENOMEM;
62 * fnic_debugfs_terminate - Tear down debugfs infrastructure
64 * Description:
65 * When Debugfs is configured this routine removes debugfs file system
66 * elements that are specific to fnic.
68 void fnic_debugfs_terminate(void)
70 debugfs_remove(fnic_stats_debugfs_root);
71 fnic_stats_debugfs_root = NULL;
73 debugfs_remove(fnic_trace_debugfs_root);
74 fnic_trace_debugfs_root = NULL;
76 vfree(fc_trc_flag);
80 * fnic_trace_ctrl_read -
81 * Read trace_enable ,fc_trace_enable
82 * or fc_trace_clear debugfs file
83 * @filp: The file pointer to read from.
84 * @ubuf: The buffer to copy the data to.
85 * @cnt: The number of bytes to read.
86 * @ppos: The position in the file to start reading from.
88 * Description:
89 * This routine reads value of variable fnic_tracing_enabled or
90 * fnic_fc_tracing_enabled or fnic_fc_trace_cleared
91 * and stores into local @buf.
92 * It will start reading file at @ppos and
93 * copy up to @cnt of data to @ubuf from @buf.
95 * Returns:
96 * This function returns the amount of data that was read.
98 static ssize_t fnic_trace_ctrl_read(struct file *filp,
99 char __user *ubuf,
100 size_t cnt, loff_t *ppos)
102 char buf[64];
103 int len;
104 u8 *trace_type;
105 len = 0;
106 trace_type = (u8 *)filp->private_data;
107 if (*trace_type == fc_trc_flag->fnic_trace)
108 len = sprintf(buf, "%d\n", fnic_tracing_enabled);
109 else if (*trace_type == fc_trc_flag->fc_trace)
110 len = sprintf(buf, "%d\n", fnic_fc_tracing_enabled);
111 else if (*trace_type == fc_trc_flag->fc_clear)
112 len = sprintf(buf, "%d\n", fnic_fc_trace_cleared);
113 else
114 pr_err("fnic: Cannot read to any debugfs file\n");
116 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
120 * fnic_trace_ctrl_write -
121 * Write to trace_enable, fc_trace_enable or
122 * fc_trace_clear debugfs file
123 * @filp: The file pointer to write from.
124 * @ubuf: The buffer to copy the data from.
125 * @cnt: The number of bytes to write.
126 * @ppos: The position in the file to start writing to.
128 * Description:
129 * This routine writes data from user buffer @ubuf to buffer @buf and
130 * sets fc_trace_enable ,tracing_enable or fnic_fc_trace_cleared
131 * value as per user input.
133 * Returns:
134 * This function returns the amount of data that was written.
136 static ssize_t fnic_trace_ctrl_write(struct file *filp,
137 const char __user *ubuf,
138 size_t cnt, loff_t *ppos)
140 char buf[64];
141 unsigned long val;
142 int ret;
143 u8 *trace_type;
144 trace_type = (u8 *)filp->private_data;
146 if (cnt >= sizeof(buf))
147 return -EINVAL;
149 if (copy_from_user(&buf, ubuf, cnt))
150 return -EFAULT;
152 buf[cnt] = 0;
154 ret = kstrtoul(buf, 10, &val);
155 if (ret < 0)
156 return ret;
158 if (*trace_type == fc_trc_flag->fnic_trace)
159 fnic_tracing_enabled = val;
160 else if (*trace_type == fc_trc_flag->fc_trace)
161 fnic_fc_tracing_enabled = val;
162 else if (*trace_type == fc_trc_flag->fc_clear)
163 fnic_fc_trace_cleared = val;
164 else
165 pr_err("fnic: cannot write to any debugfs file\n");
167 (*ppos)++;
169 return cnt;
172 static const struct file_operations fnic_trace_ctrl_fops = {
173 .owner = THIS_MODULE,
174 .open = simple_open,
175 .read = fnic_trace_ctrl_read,
176 .write = fnic_trace_ctrl_write,
180 * fnic_trace_debugfs_open - Open the fnic trace log
181 * @inode: The inode pointer
182 * @file: The file pointer to attach the log output
184 * Description:
185 * This routine is the entry point for the debugfs open file operation.
186 * It allocates the necessary buffer for the log, fills the buffer from
187 * the in-memory log and then returns a pointer to that log in
188 * the private_data field in @file.
190 * Returns:
191 * This function returns zero if successful. On error it will return
192 * a negative error value.
194 static int fnic_trace_debugfs_open(struct inode *inode,
195 struct file *file)
197 fnic_dbgfs_t *fnic_dbg_prt;
198 u8 *rdata_ptr;
199 rdata_ptr = (u8 *)inode->i_private;
200 fnic_dbg_prt = kzalloc(sizeof(fnic_dbgfs_t), GFP_KERNEL);
201 if (!fnic_dbg_prt)
202 return -ENOMEM;
204 if (*rdata_ptr == fc_trc_flag->fnic_trace) {
205 fnic_dbg_prt->buffer = vzalloc(array3_size(3, trace_max_pages,
206 PAGE_SIZE));
207 if (!fnic_dbg_prt->buffer) {
208 kfree(fnic_dbg_prt);
209 return -ENOMEM;
211 fnic_dbg_prt->buffer_len = fnic_get_trace_data(fnic_dbg_prt);
212 } else {
213 fnic_dbg_prt->buffer =
214 vzalloc(array3_size(3, fnic_fc_trace_max_pages,
215 PAGE_SIZE));
216 if (!fnic_dbg_prt->buffer) {
217 kfree(fnic_dbg_prt);
218 return -ENOMEM;
220 fnic_dbg_prt->buffer_len =
221 fnic_fc_trace_get_data(fnic_dbg_prt, *rdata_ptr);
223 file->private_data = fnic_dbg_prt;
225 return 0;
229 * fnic_trace_debugfs_lseek - Seek through a debugfs file
230 * @file: The file pointer to seek through.
231 * @offset: The offset to seek to or the amount to seek by.
232 * @howto: Indicates how to seek.
234 * Description:
235 * This routine is the entry point for the debugfs lseek file operation.
236 * The @howto parameter indicates whether @offset is the offset to directly
237 * seek to, or if it is a value to seek forward or reverse by. This function
238 * figures out what the new offset of the debugfs file will be and assigns
239 * that value to the f_pos field of @file.
241 * Returns:
242 * This function returns the new offset if successful and returns a negative
243 * error if unable to process the seek.
245 static loff_t fnic_trace_debugfs_lseek(struct file *file,
246 loff_t offset,
247 int howto)
249 fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
250 return fixed_size_llseek(file, offset, howto,
251 fnic_dbg_prt->buffer_len);
255 * fnic_trace_debugfs_read - Read a debugfs file
256 * @file: The file pointer to read from.
257 * @ubuf: The buffer to copy the data to.
258 * @nbytes: The number of bytes to read.
259 * @pos: The position in the file to start reading from.
261 * Description:
262 * This routine reads data from the buffer indicated in the private_data
263 * field of @file. It will start reading at @pos and copy up to @nbytes of
264 * data to @ubuf.
266 * Returns:
267 * This function returns the amount of data that was read (this could be
268 * less than @nbytes if the end of the file was reached).
270 static ssize_t fnic_trace_debugfs_read(struct file *file,
271 char __user *ubuf,
272 size_t nbytes,
273 loff_t *pos)
275 fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
276 int rc = 0;
277 rc = simple_read_from_buffer(ubuf, nbytes, pos,
278 fnic_dbg_prt->buffer,
279 fnic_dbg_prt->buffer_len);
280 return rc;
284 * fnic_trace_debugfs_release - Release the buffer used to store
285 * debugfs file data
286 * @inode: The inode pointer
287 * @file: The file pointer that contains the buffer to release
289 * Description:
290 * This routine frees the buffer that was allocated when the debugfs
291 * file was opened.
293 * Returns:
294 * This function returns zero.
296 static int fnic_trace_debugfs_release(struct inode *inode,
297 struct file *file)
299 fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
301 vfree(fnic_dbg_prt->buffer);
302 kfree(fnic_dbg_prt);
303 return 0;
306 static const struct file_operations fnic_trace_debugfs_fops = {
307 .owner = THIS_MODULE,
308 .open = fnic_trace_debugfs_open,
309 .llseek = fnic_trace_debugfs_lseek,
310 .read = fnic_trace_debugfs_read,
311 .release = fnic_trace_debugfs_release,
315 * fnic_trace_debugfs_init - Initialize debugfs for fnic trace logging
317 * Description:
318 * When Debugfs is configured this routine sets up the fnic debugfs
319 * file system. If not already created, this routine will create the
320 * create file trace to log fnic trace buffer output into debugfs and
321 * it will also create file trace_enable to control enable/disable of
322 * trace logging into trace buffer.
324 void fnic_trace_debugfs_init(void)
326 fnic_trace_enable = debugfs_create_file("tracing_enable",
327 S_IFREG|S_IRUGO|S_IWUSR,
328 fnic_trace_debugfs_root,
329 &(fc_trc_flag->fnic_trace),
330 &fnic_trace_ctrl_fops);
332 fnic_trace_debugfs_file = debugfs_create_file("trace",
333 S_IFREG|S_IRUGO|S_IWUSR,
334 fnic_trace_debugfs_root,
335 &(fc_trc_flag->fnic_trace),
336 &fnic_trace_debugfs_fops);
340 * fnic_trace_debugfs_terminate - Tear down debugfs infrastructure
342 * Description:
343 * When Debugfs is configured this routine removes debugfs file system
344 * elements that are specific to fnic trace logging.
346 void fnic_trace_debugfs_terminate(void)
348 debugfs_remove(fnic_trace_debugfs_file);
349 fnic_trace_debugfs_file = NULL;
351 debugfs_remove(fnic_trace_enable);
352 fnic_trace_enable = NULL;
356 * fnic_fc_trace_debugfs_init -
357 * Initialize debugfs for fnic control frame trace logging
359 * Description:
360 * When Debugfs is configured this routine sets up the fnic_fc debugfs
361 * file system. If not already created, this routine will create the
362 * create file trace to log fnic fc trace buffer output into debugfs and
363 * it will also create file fc_trace_enable to control enable/disable of
364 * trace logging into trace buffer.
367 void fnic_fc_trace_debugfs_init(void)
369 fnic_fc_trace_enable = debugfs_create_file("fc_trace_enable",
370 S_IFREG|S_IRUGO|S_IWUSR,
371 fnic_trace_debugfs_root,
372 &(fc_trc_flag->fc_trace),
373 &fnic_trace_ctrl_fops);
375 fnic_fc_trace_clear = debugfs_create_file("fc_trace_clear",
376 S_IFREG|S_IRUGO|S_IWUSR,
377 fnic_trace_debugfs_root,
378 &(fc_trc_flag->fc_clear),
379 &fnic_trace_ctrl_fops);
381 fnic_fc_rdata_trace_debugfs_file =
382 debugfs_create_file("fc_trace_rdata",
383 S_IFREG|S_IRUGO|S_IWUSR,
384 fnic_trace_debugfs_root,
385 &(fc_trc_flag->fc_normal_file),
386 &fnic_trace_debugfs_fops);
388 fnic_fc_trace_debugfs_file =
389 debugfs_create_file("fc_trace",
390 S_IFREG|S_IRUGO|S_IWUSR,
391 fnic_trace_debugfs_root,
392 &(fc_trc_flag->fc_row_file),
393 &fnic_trace_debugfs_fops);
397 * fnic_fc_trace_debugfs_terminate - Tear down debugfs infrastructure
399 * Description:
400 * When Debugfs is configured this routine removes debugfs file system
401 * elements that are specific to fnic_fc trace logging.
404 void fnic_fc_trace_debugfs_terminate(void)
406 debugfs_remove(fnic_fc_trace_debugfs_file);
407 fnic_fc_trace_debugfs_file = NULL;
409 debugfs_remove(fnic_fc_rdata_trace_debugfs_file);
410 fnic_fc_rdata_trace_debugfs_file = NULL;
412 debugfs_remove(fnic_fc_trace_enable);
413 fnic_fc_trace_enable = NULL;
415 debugfs_remove(fnic_fc_trace_clear);
416 fnic_fc_trace_clear = NULL;
420 * fnic_reset_stats_open - Open the reset_stats file
421 * @inode: The inode pointer.
422 * @file: The file pointer to attach the stats reset flag.
424 * Description:
425 * This routine opens a debugsfs file reset_stats and stores i_private data
426 * to debug structure to retrieve later for while performing other
427 * file oprations.
429 * Returns:
430 * This function returns zero if successful.
432 static int fnic_reset_stats_open(struct inode *inode, struct file *file)
434 struct stats_debug_info *debug;
436 debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
437 if (!debug)
438 return -ENOMEM;
440 debug->i_private = inode->i_private;
442 file->private_data = debug;
444 return 0;
448 * fnic_reset_stats_read - Read a reset_stats debugfs file
449 * @filp: The file pointer to read from.
450 * @ubuf: The buffer to copy the data to.
451 * @cnt: The number of bytes to read.
452 * @ppos: The position in the file to start reading from.
454 * Description:
455 * This routine reads value of variable reset_stats
456 * and stores into local @buf. It will start reading file at @ppos and
457 * copy up to @cnt of data to @ubuf from @buf.
459 * Returns:
460 * This function returns the amount of data that was read.
462 static ssize_t fnic_reset_stats_read(struct file *file,
463 char __user *ubuf,
464 size_t cnt, loff_t *ppos)
466 struct stats_debug_info *debug = file->private_data;
467 struct fnic *fnic = (struct fnic *)debug->i_private;
468 char buf[64];
469 int len;
471 len = sprintf(buf, "%u\n", fnic->reset_stats);
473 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
477 * fnic_reset_stats_write - Write to reset_stats debugfs file
478 * @filp: The file pointer to write from.
479 * @ubuf: The buffer to copy the data from.
480 * @cnt: The number of bytes to write.
481 * @ppos: The position in the file to start writing to.
483 * Description:
484 * This routine writes data from user buffer @ubuf to buffer @buf and
485 * resets cumulative stats of fnic.
487 * Returns:
488 * This function returns the amount of data that was written.
490 static ssize_t fnic_reset_stats_write(struct file *file,
491 const char __user *ubuf,
492 size_t cnt, loff_t *ppos)
494 struct stats_debug_info *debug = file->private_data;
495 struct fnic *fnic = (struct fnic *)debug->i_private;
496 struct fnic_stats *stats = &fnic->fnic_stats;
497 u64 *io_stats_p = (u64 *)&stats->io_stats;
498 u64 *fw_stats_p = (u64 *)&stats->fw_stats;
499 char buf[64];
500 unsigned long val;
501 int ret;
503 if (cnt >= sizeof(buf))
504 return -EINVAL;
506 if (copy_from_user(&buf, ubuf, cnt))
507 return -EFAULT;
509 buf[cnt] = 0;
511 ret = kstrtoul(buf, 10, &val);
512 if (ret < 0)
513 return ret;
515 fnic->reset_stats = val;
517 if (fnic->reset_stats) {
518 /* Skip variable is used to avoid descrepancies to Num IOs
519 * and IO Completions stats. Skip incrementing No IO Compls
520 * for pending active IOs after reset stats
522 atomic64_set(&fnic->io_cmpl_skip,
523 atomic64_read(&stats->io_stats.active_ios));
524 memset(&stats->abts_stats, 0, sizeof(struct abort_stats));
525 memset(&stats->term_stats, 0,
526 sizeof(struct terminate_stats));
527 memset(&stats->reset_stats, 0, sizeof(struct reset_stats));
528 memset(&stats->misc_stats, 0, sizeof(struct misc_stats));
529 memset(&stats->vlan_stats, 0, sizeof(struct vlan_stats));
530 memset(io_stats_p+1, 0,
531 sizeof(struct io_path_stats) - sizeof(u64));
532 memset(fw_stats_p+1, 0,
533 sizeof(struct fw_stats) - sizeof(u64));
534 ktime_get_real_ts64(&stats->stats_timestamps.last_reset_time);
537 (*ppos)++;
538 return cnt;
542 * fnic_reset_stats_release - Release the buffer used to store
543 * debugfs file data
544 * @inode: The inode pointer
545 * @file: The file pointer that contains the buffer to release
547 * Description:
548 * This routine frees the buffer that was allocated when the debugfs
549 * file was opened.
551 * Returns:
552 * This function returns zero.
554 static int fnic_reset_stats_release(struct inode *inode,
555 struct file *file)
557 struct stats_debug_info *debug = file->private_data;
558 kfree(debug);
559 return 0;
563 * fnic_stats_debugfs_open - Open the stats file for specific host
564 * and get fnic stats.
565 * @inode: The inode pointer.
566 * @file: The file pointer to attach the specific host statistics.
568 * Description:
569 * This routine opens a debugsfs file stats of specific host and print
570 * fnic stats.
572 * Returns:
573 * This function returns zero if successful.
575 static int fnic_stats_debugfs_open(struct inode *inode,
576 struct file *file)
578 struct fnic *fnic = inode->i_private;
579 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
580 struct stats_debug_info *debug;
581 int buf_size = 2 * PAGE_SIZE;
583 debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
584 if (!debug)
585 return -ENOMEM;
587 debug->debug_buffer = vmalloc(buf_size);
588 if (!debug->debug_buffer) {
589 kfree(debug);
590 return -ENOMEM;
593 debug->buf_size = buf_size;
594 memset((void *)debug->debug_buffer, 0, buf_size);
595 debug->buffer_len = fnic_get_stats_data(debug, fnic_stats);
597 file->private_data = debug;
599 return 0;
603 * fnic_stats_debugfs_read - Read a debugfs file
604 * @file: The file pointer to read from.
605 * @ubuf: The buffer to copy the data to.
606 * @nbytes: The number of bytes to read.
607 * @pos: The position in the file to start reading from.
609 * Description:
610 * This routine reads data from the buffer indicated in the private_data
611 * field of @file. It will start reading at @pos and copy up to @nbytes of
612 * data to @ubuf.
614 * Returns:
615 * This function returns the amount of data that was read (this could be
616 * less than @nbytes if the end of the file was reached).
618 static ssize_t fnic_stats_debugfs_read(struct file *file,
619 char __user *ubuf,
620 size_t nbytes,
621 loff_t *pos)
623 struct stats_debug_info *debug = file->private_data;
624 int rc = 0;
625 rc = simple_read_from_buffer(ubuf, nbytes, pos,
626 debug->debug_buffer,
627 debug->buffer_len);
628 return rc;
632 * fnic_stats_stats_release - Release the buffer used to store
633 * debugfs file data
634 * @inode: The inode pointer
635 * @file: The file pointer that contains the buffer to release
637 * Description:
638 * This routine frees the buffer that was allocated when the debugfs
639 * file was opened.
641 * Returns:
642 * This function returns zero.
644 static int fnic_stats_debugfs_release(struct inode *inode,
645 struct file *file)
647 struct stats_debug_info *debug = file->private_data;
648 vfree(debug->debug_buffer);
649 kfree(debug);
650 return 0;
653 static const struct file_operations fnic_stats_debugfs_fops = {
654 .owner = THIS_MODULE,
655 .open = fnic_stats_debugfs_open,
656 .read = fnic_stats_debugfs_read,
657 .release = fnic_stats_debugfs_release,
660 static const struct file_operations fnic_reset_debugfs_fops = {
661 .owner = THIS_MODULE,
662 .open = fnic_reset_stats_open,
663 .read = fnic_reset_stats_read,
664 .write = fnic_reset_stats_write,
665 .release = fnic_reset_stats_release,
669 * fnic_stats_init - Initialize stats struct and create stats file per fnic
671 * Description:
672 * When Debugfs is configured this routine sets up the stats file per fnic
673 * It will create file stats and reset_stats under statistics/host# directory
674 * to log per fnic stats.
676 void fnic_stats_debugfs_init(struct fnic *fnic)
678 char name[16];
680 snprintf(name, sizeof(name), "host%d", fnic->lport->host->host_no);
682 fnic->fnic_stats_debugfs_host = debugfs_create_dir(name,
683 fnic_stats_debugfs_root);
685 fnic->fnic_stats_debugfs_file = debugfs_create_file("stats",
686 S_IFREG|S_IRUGO|S_IWUSR,
687 fnic->fnic_stats_debugfs_host,
688 fnic,
689 &fnic_stats_debugfs_fops);
691 fnic->fnic_reset_debugfs_file = debugfs_create_file("reset_stats",
692 S_IFREG|S_IRUGO|S_IWUSR,
693 fnic->fnic_stats_debugfs_host,
694 fnic,
695 &fnic_reset_debugfs_fops);
699 * fnic_stats_debugfs_remove - Tear down debugfs infrastructure of stats
701 * Description:
702 * When Debugfs is configured this routine removes debugfs file system
703 * elements that are specific to fnic stats.
705 void fnic_stats_debugfs_remove(struct fnic *fnic)
707 if (!fnic)
708 return;
710 debugfs_remove(fnic->fnic_stats_debugfs_file);
711 fnic->fnic_stats_debugfs_file = NULL;
713 debugfs_remove(fnic->fnic_reset_debugfs_file);
714 fnic->fnic_reset_debugfs_file = NULL;
716 debugfs_remove(fnic->fnic_stats_debugfs_host);
717 fnic->fnic_stats_debugfs_host = NULL;