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>
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 {
28 static struct fc_trace_flag_type *fc_trc_flag;
31 * fnic_debugfs_init - Initialize debugfs for fnic debug logging
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
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));
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;
61 * fnic_debugfs_terminate - Tear down debugfs infrastructure
64 * When Debugfs is configured this routine removes debugfs file system
65 * elements that are specific to fnic.
67 void fnic_debugfs_terminate(void)
69 debugfs_remove(fnic_stats_debugfs_root);
70 fnic_stats_debugfs_root = NULL;
72 debugfs_remove(fnic_trace_debugfs_root);
73 fnic_trace_debugfs_root = NULL;
79 * fnic_trace_ctrl_read -
80 * Read trace_enable ,fc_trace_enable
81 * or fc_trace_clear debugfs file
82 * @filp: The file pointer to read from.
83 * @ubuf: The buffer to copy the data to.
84 * @cnt: The number of bytes to read.
85 * @ppos: The position in the file to start reading from.
88 * This routine reads value of variable fnic_tracing_enabled or
89 * fnic_fc_tracing_enabled or fnic_fc_trace_cleared
90 * and stores into local @buf.
91 * It will start reading file at @ppos and
92 * copy up to @cnt of data to @ubuf from @buf.
95 * This function returns the amount of data that was read.
97 static ssize_t fnic_trace_ctrl_read(struct file *filp,
99 size_t cnt, loff_t *ppos)
105 trace_type = (u8 *)filp->private_data;
106 if (*trace_type == fc_trc_flag->fnic_trace)
107 len = sprintf(buf, "%d\n", fnic_tracing_enabled);
108 else if (*trace_type == fc_trc_flag->fc_trace)
109 len = sprintf(buf, "%d\n", fnic_fc_tracing_enabled);
110 else if (*trace_type == fc_trc_flag->fc_clear)
111 len = sprintf(buf, "%d\n", fnic_fc_trace_cleared);
113 pr_err("fnic: Cannot read to any debugfs file\n");
115 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
119 * fnic_trace_ctrl_write -
120 * Write to trace_enable, fc_trace_enable or
121 * fc_trace_clear debugfs file
122 * @filp: The file pointer to write from.
123 * @ubuf: The buffer to copy the data from.
124 * @cnt: The number of bytes to write.
125 * @ppos: The position in the file to start writing to.
128 * This routine writes data from user buffer @ubuf to buffer @buf and
129 * sets fc_trace_enable ,tracing_enable or fnic_fc_trace_cleared
130 * value as per user input.
133 * This function returns the amount of data that was written.
135 static ssize_t fnic_trace_ctrl_write(struct file *filp,
136 const char __user *ubuf,
137 size_t cnt, loff_t *ppos)
143 trace_type = (u8 *)filp->private_data;
145 if (cnt >= sizeof(buf))
148 if (copy_from_user(&buf, ubuf, cnt))
153 ret = kstrtoul(buf, 10, &val);
157 if (*trace_type == fc_trc_flag->fnic_trace)
158 fnic_tracing_enabled = val;
159 else if (*trace_type == fc_trc_flag->fc_trace)
160 fnic_fc_tracing_enabled = val;
161 else if (*trace_type == fc_trc_flag->fc_clear)
162 fnic_fc_trace_cleared = val;
164 pr_err("fnic: cannot write to any debugfs file\n");
171 static const struct file_operations fnic_trace_ctrl_fops = {
172 .owner = THIS_MODULE,
174 .read = fnic_trace_ctrl_read,
175 .write = fnic_trace_ctrl_write,
179 * fnic_trace_debugfs_open - Open the fnic trace log
180 * @inode: The inode pointer
181 * @file: The file pointer to attach the log output
184 * This routine is the entry point for the debugfs open file operation.
185 * It allocates the necessary buffer for the log, fills the buffer from
186 * the in-memory log and then returns a pointer to that log in
187 * the private_data field in @file.
190 * This function returns zero if successful. On error it will return
191 * a negative error value.
193 static int fnic_trace_debugfs_open(struct inode *inode,
196 fnic_dbgfs_t *fnic_dbg_prt;
198 rdata_ptr = (u8 *)inode->i_private;
199 fnic_dbg_prt = kzalloc(sizeof(fnic_dbgfs_t), GFP_KERNEL);
203 if (*rdata_ptr == fc_trc_flag->fnic_trace) {
204 fnic_dbg_prt->buffer = vmalloc(array3_size(3, trace_max_pages,
206 if (!fnic_dbg_prt->buffer) {
210 memset((void *)fnic_dbg_prt->buffer, 0,
211 3 * (trace_max_pages * PAGE_SIZE));
212 fnic_dbg_prt->buffer_len = fnic_get_trace_data(fnic_dbg_prt);
214 fnic_dbg_prt->buffer =
215 vmalloc(array3_size(3, fnic_fc_trace_max_pages,
217 if (!fnic_dbg_prt->buffer) {
221 memset((void *)fnic_dbg_prt->buffer, 0,
222 3 * (fnic_fc_trace_max_pages * PAGE_SIZE));
223 fnic_dbg_prt->buffer_len =
224 fnic_fc_trace_get_data(fnic_dbg_prt, *rdata_ptr);
226 file->private_data = fnic_dbg_prt;
232 * fnic_trace_debugfs_lseek - Seek through a debugfs file
233 * @file: The file pointer to seek through.
234 * @offset: The offset to seek to or the amount to seek by.
235 * @howto: Indicates how to seek.
238 * This routine is the entry point for the debugfs lseek file operation.
239 * The @howto parameter indicates whether @offset is the offset to directly
240 * seek to, or if it is a value to seek forward or reverse by. This function
241 * figures out what the new offset of the debugfs file will be and assigns
242 * that value to the f_pos field of @file.
245 * This function returns the new offset if successful and returns a negative
246 * error if unable to process the seek.
248 static loff_t fnic_trace_debugfs_lseek(struct file *file,
252 fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
253 return fixed_size_llseek(file, offset, howto,
254 fnic_dbg_prt->buffer_len);
258 * fnic_trace_debugfs_read - Read a debugfs file
259 * @file: The file pointer to read from.
260 * @ubuf: The buffer to copy the data to.
261 * @nbytes: The number of bytes to read.
262 * @pos: The position in the file to start reading from.
265 * This routine reads data from the buffer indicated in the private_data
266 * field of @file. It will start reading at @pos and copy up to @nbytes of
270 * This function returns the amount of data that was read (this could be
271 * less than @nbytes if the end of the file was reached).
273 static ssize_t fnic_trace_debugfs_read(struct file *file,
278 fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
280 rc = simple_read_from_buffer(ubuf, nbytes, pos,
281 fnic_dbg_prt->buffer,
282 fnic_dbg_prt->buffer_len);
287 * fnic_trace_debugfs_release - Release the buffer used to store
289 * @inode: The inode pointer
290 * @file: The file pointer that contains the buffer to release
293 * This routine frees the buffer that was allocated when the debugfs
297 * This function returns zero.
299 static int fnic_trace_debugfs_release(struct inode *inode,
302 fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
304 vfree(fnic_dbg_prt->buffer);
309 static const struct file_operations fnic_trace_debugfs_fops = {
310 .owner = THIS_MODULE,
311 .open = fnic_trace_debugfs_open,
312 .llseek = fnic_trace_debugfs_lseek,
313 .read = fnic_trace_debugfs_read,
314 .release = fnic_trace_debugfs_release,
318 * fnic_trace_debugfs_init - Initialize debugfs for fnic trace logging
321 * When Debugfs is configured this routine sets up the fnic debugfs
322 * file system. If not already created, this routine will create the
323 * create file trace to log fnic trace buffer output into debugfs and
324 * it will also create file trace_enable to control enable/disable of
325 * trace logging into trace buffer.
327 void fnic_trace_debugfs_init(void)
329 fnic_trace_enable = debugfs_create_file("tracing_enable",
330 S_IFREG|S_IRUGO|S_IWUSR,
331 fnic_trace_debugfs_root,
332 &(fc_trc_flag->fnic_trace),
333 &fnic_trace_ctrl_fops);
335 fnic_trace_debugfs_file = debugfs_create_file("trace",
336 S_IFREG|S_IRUGO|S_IWUSR,
337 fnic_trace_debugfs_root,
338 &(fc_trc_flag->fnic_trace),
339 &fnic_trace_debugfs_fops);
343 * fnic_trace_debugfs_terminate - Tear down debugfs infrastructure
346 * When Debugfs is configured this routine removes debugfs file system
347 * elements that are specific to fnic trace logging.
349 void fnic_trace_debugfs_terminate(void)
351 debugfs_remove(fnic_trace_debugfs_file);
352 fnic_trace_debugfs_file = NULL;
354 debugfs_remove(fnic_trace_enable);
355 fnic_trace_enable = NULL;
359 * fnic_fc_trace_debugfs_init -
360 * Initialize debugfs for fnic control frame trace logging
363 * When Debugfs is configured this routine sets up the fnic_fc debugfs
364 * file system. If not already created, this routine will create the
365 * create file trace to log fnic fc trace buffer output into debugfs and
366 * it will also create file fc_trace_enable to control enable/disable of
367 * trace logging into trace buffer.
370 void fnic_fc_trace_debugfs_init(void)
372 fnic_fc_trace_enable = debugfs_create_file("fc_trace_enable",
373 S_IFREG|S_IRUGO|S_IWUSR,
374 fnic_trace_debugfs_root,
375 &(fc_trc_flag->fc_trace),
376 &fnic_trace_ctrl_fops);
378 fnic_fc_trace_clear = debugfs_create_file("fc_trace_clear",
379 S_IFREG|S_IRUGO|S_IWUSR,
380 fnic_trace_debugfs_root,
381 &(fc_trc_flag->fc_clear),
382 &fnic_trace_ctrl_fops);
384 fnic_fc_rdata_trace_debugfs_file =
385 debugfs_create_file("fc_trace_rdata",
386 S_IFREG|S_IRUGO|S_IWUSR,
387 fnic_trace_debugfs_root,
388 &(fc_trc_flag->fc_normal_file),
389 &fnic_trace_debugfs_fops);
391 fnic_fc_trace_debugfs_file =
392 debugfs_create_file("fc_trace",
393 S_IFREG|S_IRUGO|S_IWUSR,
394 fnic_trace_debugfs_root,
395 &(fc_trc_flag->fc_row_file),
396 &fnic_trace_debugfs_fops);
400 * fnic_fc_trace_debugfs_terminate - Tear down debugfs infrastructure
403 * When Debugfs is configured this routine removes debugfs file system
404 * elements that are specific to fnic_fc trace logging.
407 void fnic_fc_trace_debugfs_terminate(void)
409 debugfs_remove(fnic_fc_trace_debugfs_file);
410 fnic_fc_trace_debugfs_file = NULL;
412 debugfs_remove(fnic_fc_rdata_trace_debugfs_file);
413 fnic_fc_rdata_trace_debugfs_file = NULL;
415 debugfs_remove(fnic_fc_trace_enable);
416 fnic_fc_trace_enable = NULL;
418 debugfs_remove(fnic_fc_trace_clear);
419 fnic_fc_trace_clear = NULL;
423 * fnic_reset_stats_open - Open the reset_stats file
424 * @inode: The inode pointer.
425 * @file: The file pointer to attach the stats reset flag.
428 * This routine opens a debugsfs file reset_stats and stores i_private data
429 * to debug structure to retrieve later for while performing other
433 * This function returns zero if successful.
435 static int fnic_reset_stats_open(struct inode *inode, struct file *file)
437 struct stats_debug_info *debug;
439 debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
443 debug->i_private = inode->i_private;
445 file->private_data = debug;
451 * fnic_reset_stats_read - Read a reset_stats debugfs file
452 * @filp: The file pointer to read from.
453 * @ubuf: The buffer to copy the data to.
454 * @cnt: The number of bytes to read.
455 * @ppos: The position in the file to start reading from.
458 * This routine reads value of variable reset_stats
459 * and stores into local @buf. It will start reading file at @ppos and
460 * copy up to @cnt of data to @ubuf from @buf.
463 * This function returns the amount of data that was read.
465 static ssize_t fnic_reset_stats_read(struct file *file,
467 size_t cnt, loff_t *ppos)
469 struct stats_debug_info *debug = file->private_data;
470 struct fnic *fnic = (struct fnic *)debug->i_private;
474 len = sprintf(buf, "%u\n", fnic->reset_stats);
476 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
480 * fnic_reset_stats_write - Write to reset_stats debugfs file
481 * @filp: The file pointer to write from.
482 * @ubuf: The buffer to copy the data from.
483 * @cnt: The number of bytes to write.
484 * @ppos: The position in the file to start writing to.
487 * This routine writes data from user buffer @ubuf to buffer @buf and
488 * resets cumulative stats of fnic.
491 * This function returns the amount of data that was written.
493 static ssize_t fnic_reset_stats_write(struct file *file,
494 const char __user *ubuf,
495 size_t cnt, loff_t *ppos)
497 struct stats_debug_info *debug = file->private_data;
498 struct fnic *fnic = (struct fnic *)debug->i_private;
499 struct fnic_stats *stats = &fnic->fnic_stats;
500 u64 *io_stats_p = (u64 *)&stats->io_stats;
501 u64 *fw_stats_p = (u64 *)&stats->fw_stats;
506 if (cnt >= sizeof(buf))
509 if (copy_from_user(&buf, ubuf, cnt))
514 ret = kstrtoul(buf, 10, &val);
518 fnic->reset_stats = val;
520 if (fnic->reset_stats) {
521 /* Skip variable is used to avoid descrepancies to Num IOs
522 * and IO Completions stats. Skip incrementing No IO Compls
523 * for pending active IOs after reset stats
525 atomic64_set(&fnic->io_cmpl_skip,
526 atomic64_read(&stats->io_stats.active_ios));
527 memset(&stats->abts_stats, 0, sizeof(struct abort_stats));
528 memset(&stats->term_stats, 0,
529 sizeof(struct terminate_stats));
530 memset(&stats->reset_stats, 0, sizeof(struct reset_stats));
531 memset(&stats->misc_stats, 0, sizeof(struct misc_stats));
532 memset(&stats->vlan_stats, 0, sizeof(struct vlan_stats));
533 memset(io_stats_p+1, 0,
534 sizeof(struct io_path_stats) - sizeof(u64));
535 memset(fw_stats_p+1, 0,
536 sizeof(struct fw_stats) - sizeof(u64));
537 ktime_get_real_ts64(&stats->stats_timestamps.last_reset_time);
545 * fnic_reset_stats_release - Release the buffer used to store
547 * @inode: The inode pointer
548 * @file: The file pointer that contains the buffer to release
551 * This routine frees the buffer that was allocated when the debugfs
555 * This function returns zero.
557 static int fnic_reset_stats_release(struct inode *inode,
560 struct stats_debug_info *debug = file->private_data;
566 * fnic_stats_debugfs_open - Open the stats file for specific host
567 * and get fnic stats.
568 * @inode: The inode pointer.
569 * @file: The file pointer to attach the specific host statistics.
572 * This routine opens a debugsfs file stats of specific host and print
576 * This function returns zero if successful.
578 static int fnic_stats_debugfs_open(struct inode *inode,
581 struct fnic *fnic = inode->i_private;
582 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
583 struct stats_debug_info *debug;
584 int buf_size = 2 * PAGE_SIZE;
586 debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
590 debug->debug_buffer = vmalloc(buf_size);
591 if (!debug->debug_buffer) {
596 debug->buf_size = buf_size;
597 memset((void *)debug->debug_buffer, 0, buf_size);
598 debug->buffer_len = fnic_get_stats_data(debug, fnic_stats);
600 file->private_data = debug;
606 * fnic_stats_debugfs_read - Read a debugfs file
607 * @file: The file pointer to read from.
608 * @ubuf: The buffer to copy the data to.
609 * @nbytes: The number of bytes to read.
610 * @pos: The position in the file to start reading from.
613 * This routine reads data from the buffer indicated in the private_data
614 * field of @file. It will start reading at @pos and copy up to @nbytes of
618 * This function returns the amount of data that was read (this could be
619 * less than @nbytes if the end of the file was reached).
621 static ssize_t fnic_stats_debugfs_read(struct file *file,
626 struct stats_debug_info *debug = file->private_data;
628 rc = simple_read_from_buffer(ubuf, nbytes, pos,
635 * fnic_stats_stats_release - Release the buffer used to store
637 * @inode: The inode pointer
638 * @file: The file pointer that contains the buffer to release
641 * This routine frees the buffer that was allocated when the debugfs
645 * This function returns zero.
647 static int fnic_stats_debugfs_release(struct inode *inode,
650 struct stats_debug_info *debug = file->private_data;
651 vfree(debug->debug_buffer);
656 static const struct file_operations fnic_stats_debugfs_fops = {
657 .owner = THIS_MODULE,
658 .open = fnic_stats_debugfs_open,
659 .read = fnic_stats_debugfs_read,
660 .release = fnic_stats_debugfs_release,
663 static const struct file_operations fnic_reset_debugfs_fops = {
664 .owner = THIS_MODULE,
665 .open = fnic_reset_stats_open,
666 .read = fnic_reset_stats_read,
667 .write = fnic_reset_stats_write,
668 .release = fnic_reset_stats_release,
672 * fnic_stats_init - Initialize stats struct and create stats file per fnic
675 * When Debugfs is configured this routine sets up the stats file per fnic
676 * It will create file stats and reset_stats under statistics/host# directory
677 * to log per fnic stats.
679 void fnic_stats_debugfs_init(struct fnic *fnic)
683 snprintf(name, sizeof(name), "host%d", fnic->lport->host->host_no);
685 fnic->fnic_stats_debugfs_host = debugfs_create_dir(name,
686 fnic_stats_debugfs_root);
688 fnic->fnic_stats_debugfs_file = debugfs_create_file("stats",
689 S_IFREG|S_IRUGO|S_IWUSR,
690 fnic->fnic_stats_debugfs_host,
692 &fnic_stats_debugfs_fops);
694 fnic->fnic_reset_debugfs_file = debugfs_create_file("reset_stats",
695 S_IFREG|S_IRUGO|S_IWUSR,
696 fnic->fnic_stats_debugfs_host,
698 &fnic_reset_debugfs_fops);
702 * fnic_stats_debugfs_remove - Tear down debugfs infrastructure of stats
705 * When Debugfs is configured this routine removes debugfs file system
706 * elements that are specific to fnic stats.
708 void fnic_stats_debugfs_remove(struct fnic *fnic)
713 debugfs_remove(fnic->fnic_stats_debugfs_file);
714 fnic->fnic_stats_debugfs_file = NULL;
716 debugfs_remove(fnic->fnic_reset_debugfs_file);
717 fnic->fnic_reset_debugfs_file = NULL;
719 debugfs_remove(fnic->fnic_stats_debugfs_host);
720 fnic->fnic_stats_debugfs_host = NULL;