2 * Copyright 2012 Cisco Systems, Inc. All rights reserved.
4 * This program is free software; you may redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
9 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
10 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
11 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
12 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
13 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
14 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/debugfs.h>
23 static struct dentry *fnic_trace_debugfs_root;
24 static struct dentry *fnic_trace_debugfs_file;
25 static struct dentry *fnic_trace_enable;
26 static struct dentry *fnic_stats_debugfs_root;
28 static struct dentry *fnic_fc_trace_debugfs_file;
29 static struct dentry *fnic_fc_rdata_trace_debugfs_file;
30 static struct dentry *fnic_fc_trace_enable;
31 static struct dentry *fnic_fc_trace_clear;
33 struct fc_trace_flag_type {
41 static struct fc_trace_flag_type *fc_trc_flag;
44 * fnic_debugfs_init - Initialize debugfs for fnic debug logging
47 * When Debugfs is configured this routine sets up the fnic debugfs
48 * file system. If not already created, this routine will create the
49 * fnic directory and statistics directory for trace buffer and
52 int fnic_debugfs_init(void)
55 fnic_trace_debugfs_root = debugfs_create_dir("fnic", NULL);
56 if (!fnic_trace_debugfs_root) {
57 printk(KERN_DEBUG "Cannot create debugfs root\n");
61 if (!fnic_trace_debugfs_root) {
63 "fnic root directory doesn't exist in debugfs\n");
67 fnic_stats_debugfs_root = debugfs_create_dir("statistics",
68 fnic_trace_debugfs_root);
69 if (!fnic_stats_debugfs_root) {
70 printk(KERN_DEBUG "Cannot create Statistics directory\n");
74 /* Allocate memory to structure */
75 fc_trc_flag = (struct fc_trace_flag_type *)
76 vmalloc(sizeof(struct fc_trace_flag_type));
79 fc_trc_flag->fc_row_file = 0;
80 fc_trc_flag->fc_normal_file = 1;
81 fc_trc_flag->fnic_trace = 2;
82 fc_trc_flag->fc_trace = 3;
83 fc_trc_flag->fc_clear = 4;
91 * fnic_debugfs_terminate - Tear down debugfs infrastructure
94 * When Debugfs is configured this routine removes debugfs file system
95 * elements that are specific to fnic.
97 void fnic_debugfs_terminate(void)
99 debugfs_remove(fnic_stats_debugfs_root);
100 fnic_stats_debugfs_root = NULL;
102 debugfs_remove(fnic_trace_debugfs_root);
103 fnic_trace_debugfs_root = NULL;
110 * fnic_trace_ctrl_open - Open the trace_enable file for fnic_trace
111 * Or Open fc_trace_enable file for fc_trace
112 * @inode: The inode pointer.
113 * @file: The file pointer to attach the trace enable/disable flag.
116 * This routine opens a debugsfs file trace_enable or fc_trace_enable.
119 * This function returns zero if successful.
121 static int fnic_trace_ctrl_open(struct inode *inode, struct file *filp)
123 filp->private_data = inode->i_private;
128 * fnic_trace_ctrl_read -
129 * Read trace_enable ,fc_trace_enable
130 * or fc_trace_clear debugfs file
131 * @filp: The file pointer to read from.
132 * @ubuf: The buffer to copy the data to.
133 * @cnt: The number of bytes to read.
134 * @ppos: The position in the file to start reading from.
137 * This routine reads value of variable fnic_tracing_enabled or
138 * fnic_fc_tracing_enabled or fnic_fc_trace_cleared
139 * and stores into local @buf.
140 * It will start reading file at @ppos and
141 * copy up to @cnt of data to @ubuf from @buf.
144 * This function returns the amount of data that was read.
146 static ssize_t fnic_trace_ctrl_read(struct file *filp,
148 size_t cnt, loff_t *ppos)
154 trace_type = (u8 *)filp->private_data;
155 if (*trace_type == fc_trc_flag->fnic_trace)
156 len = sprintf(buf, "%u\n", fnic_tracing_enabled);
157 else if (*trace_type == fc_trc_flag->fc_trace)
158 len = sprintf(buf, "%u\n", fnic_fc_tracing_enabled);
159 else if (*trace_type == fc_trc_flag->fc_clear)
160 len = sprintf(buf, "%u\n", fnic_fc_trace_cleared);
162 pr_err("fnic: Cannot read to any debugfs file\n");
164 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
168 * fnic_trace_ctrl_write -
169 * Write to trace_enable, fc_trace_enable or
170 * fc_trace_clear debugfs file
171 * @filp: The file pointer to write from.
172 * @ubuf: The buffer to copy the data from.
173 * @cnt: The number of bytes to write.
174 * @ppos: The position in the file to start writing to.
177 * This routine writes data from user buffer @ubuf to buffer @buf and
178 * sets fc_trace_enable ,tracing_enable or fnic_fc_trace_cleared
179 * value as per user input.
182 * This function returns the amount of data that was written.
184 static ssize_t fnic_trace_ctrl_write(struct file *filp,
185 const char __user *ubuf,
186 size_t cnt, loff_t *ppos)
192 trace_type = (u8 *)filp->private_data;
194 if (cnt >= sizeof(buf))
197 if (copy_from_user(&buf, ubuf, cnt))
202 ret = kstrtoul(buf, 10, &val);
206 if (*trace_type == fc_trc_flag->fnic_trace)
207 fnic_tracing_enabled = val;
208 else if (*trace_type == fc_trc_flag->fc_trace)
209 fnic_fc_tracing_enabled = val;
210 else if (*trace_type == fc_trc_flag->fc_clear)
211 fnic_fc_trace_cleared = val;
213 pr_err("fnic: cannot write to any debugfs file\n");
220 static const struct file_operations fnic_trace_ctrl_fops = {
221 .owner = THIS_MODULE,
222 .open = fnic_trace_ctrl_open,
223 .read = fnic_trace_ctrl_read,
224 .write = fnic_trace_ctrl_write,
228 * fnic_trace_debugfs_open - Open the fnic trace log
229 * @inode: The inode pointer
230 * @file: The file pointer to attach the log output
233 * This routine is the entry point for the debugfs open file operation.
234 * It allocates the necessary buffer for the log, fills the buffer from
235 * the in-memory log and then returns a pointer to that log in
236 * the private_data field in @file.
239 * This function returns zero if successful. On error it will return
240 * a negative error value.
242 static int fnic_trace_debugfs_open(struct inode *inode,
245 fnic_dbgfs_t *fnic_dbg_prt;
247 rdata_ptr = (u8 *)inode->i_private;
248 fnic_dbg_prt = kzalloc(sizeof(fnic_dbgfs_t), GFP_KERNEL);
252 if (*rdata_ptr == fc_trc_flag->fnic_trace) {
253 fnic_dbg_prt->buffer = vmalloc(3 *
254 (trace_max_pages * PAGE_SIZE));
255 if (!fnic_dbg_prt->buffer) {
259 memset((void *)fnic_dbg_prt->buffer, 0,
260 3 * (trace_max_pages * PAGE_SIZE));
261 fnic_dbg_prt->buffer_len = fnic_get_trace_data(fnic_dbg_prt);
263 fnic_dbg_prt->buffer =
264 vmalloc(3 * (fnic_fc_trace_max_pages * PAGE_SIZE));
265 if (!fnic_dbg_prt->buffer) {
269 memset((void *)fnic_dbg_prt->buffer, 0,
270 3 * (fnic_fc_trace_max_pages * PAGE_SIZE));
271 fnic_dbg_prt->buffer_len =
272 fnic_fc_trace_get_data(fnic_dbg_prt, *rdata_ptr);
274 file->private_data = fnic_dbg_prt;
280 * fnic_trace_debugfs_lseek - Seek through a debugfs file
281 * @file: The file pointer to seek through.
282 * @offset: The offset to seek to or the amount to seek by.
283 * @howto: Indicates how to seek.
286 * This routine is the entry point for the debugfs lseek file operation.
287 * The @howto parameter indicates whether @offset is the offset to directly
288 * seek to, or if it is a value to seek forward or reverse by. This function
289 * figures out what the new offset of the debugfs file will be and assigns
290 * that value to the f_pos field of @file.
293 * This function returns the new offset if successful and returns a negative
294 * error if unable to process the seek.
296 static loff_t fnic_trace_debugfs_lseek(struct file *file,
300 fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
301 return fixed_size_llseek(file, offset, howto,
302 fnic_dbg_prt->buffer_len);
306 * fnic_trace_debugfs_read - Read a debugfs file
307 * @file: The file pointer to read from.
308 * @ubuf: The buffer to copy the data to.
309 * @nbytes: The number of bytes to read.
310 * @pos: The position in the file to start reading from.
313 * This routine reads data from the buffer indicated in the private_data
314 * field of @file. It will start reading at @pos and copy up to @nbytes of
318 * This function returns the amount of data that was read (this could be
319 * less than @nbytes if the end of the file was reached).
321 static ssize_t fnic_trace_debugfs_read(struct file *file,
326 fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
328 rc = simple_read_from_buffer(ubuf, nbytes, pos,
329 fnic_dbg_prt->buffer,
330 fnic_dbg_prt->buffer_len);
335 * fnic_trace_debugfs_release - Release the buffer used to store
337 * @inode: The inode pointer
338 * @file: The file pointer that contains the buffer to release
341 * This routine frees the buffer that was allocated when the debugfs
345 * This function returns zero.
347 static int fnic_trace_debugfs_release(struct inode *inode,
350 fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
352 vfree(fnic_dbg_prt->buffer);
357 static const struct file_operations fnic_trace_debugfs_fops = {
358 .owner = THIS_MODULE,
359 .open = fnic_trace_debugfs_open,
360 .llseek = fnic_trace_debugfs_lseek,
361 .read = fnic_trace_debugfs_read,
362 .release = fnic_trace_debugfs_release,
366 * fnic_trace_debugfs_init - Initialize debugfs for fnic trace logging
369 * When Debugfs is configured this routine sets up the fnic debugfs
370 * file system. If not already created, this routine will create the
371 * create file trace to log fnic trace buffer output into debugfs and
372 * it will also create file trace_enable to control enable/disable of
373 * trace logging into trace buffer.
375 int fnic_trace_debugfs_init(void)
378 if (!fnic_trace_debugfs_root) {
380 "FNIC Debugfs root directory doesn't exist\n");
383 fnic_trace_enable = debugfs_create_file("tracing_enable",
384 S_IFREG|S_IRUGO|S_IWUSR,
385 fnic_trace_debugfs_root,
386 &(fc_trc_flag->fnic_trace),
387 &fnic_trace_ctrl_fops);
389 if (!fnic_trace_enable) {
391 "Cannot create trace_enable file under debugfs\n");
395 fnic_trace_debugfs_file = debugfs_create_file("trace",
396 S_IFREG|S_IRUGO|S_IWUSR,
397 fnic_trace_debugfs_root,
398 &(fc_trc_flag->fnic_trace),
399 &fnic_trace_debugfs_fops);
401 if (!fnic_trace_debugfs_file) {
403 "Cannot create trace file under debugfs\n");
411 * fnic_trace_debugfs_terminate - Tear down debugfs infrastructure
414 * When Debugfs is configured this routine removes debugfs file system
415 * elements that are specific to fnic trace logging.
417 void fnic_trace_debugfs_terminate(void)
419 debugfs_remove(fnic_trace_debugfs_file);
420 fnic_trace_debugfs_file = NULL;
422 debugfs_remove(fnic_trace_enable);
423 fnic_trace_enable = NULL;
427 * fnic_fc_trace_debugfs_init -
428 * Initialize debugfs for fnic control frame trace logging
431 * When Debugfs is configured this routine sets up the fnic_fc debugfs
432 * file system. If not already created, this routine will create the
433 * create file trace to log fnic fc trace buffer output into debugfs and
434 * it will also create file fc_trace_enable to control enable/disable of
435 * trace logging into trace buffer.
438 int fnic_fc_trace_debugfs_init(void)
442 if (!fnic_trace_debugfs_root) {
443 pr_err("fnic:Debugfs root directory doesn't exist\n");
447 fnic_fc_trace_enable = debugfs_create_file("fc_trace_enable",
448 S_IFREG|S_IRUGO|S_IWUSR,
449 fnic_trace_debugfs_root,
450 &(fc_trc_flag->fc_trace),
451 &fnic_trace_ctrl_fops);
453 if (!fnic_fc_trace_enable) {
454 pr_err("fnic: Failed create fc_trace_enable file\n");
458 fnic_fc_trace_clear = debugfs_create_file("fc_trace_clear",
459 S_IFREG|S_IRUGO|S_IWUSR,
460 fnic_trace_debugfs_root,
461 &(fc_trc_flag->fc_clear),
462 &fnic_trace_ctrl_fops);
464 if (!fnic_fc_trace_clear) {
465 pr_err("fnic: Failed to create fc_trace_enable file\n");
469 fnic_fc_rdata_trace_debugfs_file =
470 debugfs_create_file("fc_trace_rdata",
471 S_IFREG|S_IRUGO|S_IWUSR,
472 fnic_trace_debugfs_root,
473 &(fc_trc_flag->fc_normal_file),
474 &fnic_trace_debugfs_fops);
476 if (!fnic_fc_rdata_trace_debugfs_file) {
477 pr_err("fnic: Failed create fc_rdata_trace file\n");
481 fnic_fc_trace_debugfs_file =
482 debugfs_create_file("fc_trace",
483 S_IFREG|S_IRUGO|S_IWUSR,
484 fnic_trace_debugfs_root,
485 &(fc_trc_flag->fc_row_file),
486 &fnic_trace_debugfs_fops);
488 if (!fnic_fc_trace_debugfs_file) {
489 pr_err("fnic: Failed to create fc_trace file\n");
497 * fnic_fc_trace_debugfs_terminate - Tear down debugfs infrastructure
500 * When Debugfs is configured this routine removes debugfs file system
501 * elements that are specific to fnic_fc trace logging.
504 void fnic_fc_trace_debugfs_terminate(void)
506 debugfs_remove(fnic_fc_trace_debugfs_file);
507 fnic_fc_trace_debugfs_file = NULL;
509 debugfs_remove(fnic_fc_rdata_trace_debugfs_file);
510 fnic_fc_rdata_trace_debugfs_file = NULL;
512 debugfs_remove(fnic_fc_trace_enable);
513 fnic_fc_trace_enable = NULL;
515 debugfs_remove(fnic_fc_trace_clear);
516 fnic_fc_trace_clear = NULL;
520 * fnic_reset_stats_open - Open the reset_stats file
521 * @inode: The inode pointer.
522 * @file: The file pointer to attach the stats reset flag.
525 * This routine opens a debugsfs file reset_stats and stores i_private data
526 * to debug structure to retrieve later for while performing other
530 * This function returns zero if successful.
532 static int fnic_reset_stats_open(struct inode *inode, struct file *file)
534 struct stats_debug_info *debug;
536 debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
540 debug->i_private = inode->i_private;
542 file->private_data = debug;
548 * fnic_reset_stats_read - Read a reset_stats debugfs file
549 * @filp: The file pointer to read from.
550 * @ubuf: The buffer to copy the data to.
551 * @cnt: The number of bytes to read.
552 * @ppos: The position in the file to start reading from.
555 * This routine reads value of variable reset_stats
556 * and stores into local @buf. It will start reading file at @ppos and
557 * copy up to @cnt of data to @ubuf from @buf.
560 * This function returns the amount of data that was read.
562 static ssize_t fnic_reset_stats_read(struct file *file,
564 size_t cnt, loff_t *ppos)
566 struct stats_debug_info *debug = file->private_data;
567 struct fnic *fnic = (struct fnic *)debug->i_private;
571 len = sprintf(buf, "%u\n", fnic->reset_stats);
573 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
577 * fnic_reset_stats_write - Write to reset_stats debugfs file
578 * @filp: The file pointer to write from.
579 * @ubuf: The buffer to copy the data from.
580 * @cnt: The number of bytes to write.
581 * @ppos: The position in the file to start writing to.
584 * This routine writes data from user buffer @ubuf to buffer @buf and
585 * resets cumulative stats of fnic.
588 * This function returns the amount of data that was written.
590 static ssize_t fnic_reset_stats_write(struct file *file,
591 const char __user *ubuf,
592 size_t cnt, loff_t *ppos)
594 struct stats_debug_info *debug = file->private_data;
595 struct fnic *fnic = (struct fnic *)debug->i_private;
596 struct fnic_stats *stats = &fnic->fnic_stats;
597 u64 *io_stats_p = (u64 *)&stats->io_stats;
598 u64 *fw_stats_p = (u64 *)&stats->fw_stats;
603 if (cnt >= sizeof(buf))
606 if (copy_from_user(&buf, ubuf, cnt))
611 ret = kstrtoul(buf, 10, &val);
615 fnic->reset_stats = val;
617 if (fnic->reset_stats) {
618 /* Skip variable is used to avoid descrepancies to Num IOs
619 * and IO Completions stats. Skip incrementing No IO Compls
620 * for pending active IOs after reset stats
622 atomic64_set(&fnic->io_cmpl_skip,
623 atomic64_read(&stats->io_stats.active_ios));
624 memset(&stats->abts_stats, 0, sizeof(struct abort_stats));
625 memset(&stats->term_stats, 0,
626 sizeof(struct terminate_stats));
627 memset(&stats->reset_stats, 0, sizeof(struct reset_stats));
628 memset(&stats->misc_stats, 0, sizeof(struct misc_stats));
629 memset(&stats->vlan_stats, 0, sizeof(struct vlan_stats));
630 memset(io_stats_p+1, 0,
631 sizeof(struct io_path_stats) - sizeof(u64));
632 memset(fw_stats_p+1, 0,
633 sizeof(struct fw_stats) - sizeof(u64));
641 * fnic_reset_stats_release - Release the buffer used to store
643 * @inode: The inode pointer
644 * @file: The file pointer that contains the buffer to release
647 * This routine frees the buffer that was allocated when the debugfs
651 * This function returns zero.
653 static int fnic_reset_stats_release(struct inode *inode,
656 struct stats_debug_info *debug = file->private_data;
662 * fnic_stats_debugfs_open - Open the stats file for specific host
663 * and get fnic stats.
664 * @inode: The inode pointer.
665 * @file: The file pointer to attach the specific host statistics.
668 * This routine opens a debugsfs file stats of specific host and print
672 * This function returns zero if successful.
674 static int fnic_stats_debugfs_open(struct inode *inode,
677 struct fnic *fnic = inode->i_private;
678 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
679 struct stats_debug_info *debug;
680 int buf_size = 2 * PAGE_SIZE;
682 debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
686 debug->debug_buffer = vmalloc(buf_size);
687 if (!debug->debug_buffer) {
692 debug->buf_size = buf_size;
693 memset((void *)debug->debug_buffer, 0, buf_size);
694 debug->buffer_len = fnic_get_stats_data(debug, fnic_stats);
696 file->private_data = debug;
702 * fnic_stats_debugfs_read - Read a debugfs file
703 * @file: The file pointer to read from.
704 * @ubuf: The buffer to copy the data to.
705 * @nbytes: The number of bytes to read.
706 * @pos: The position in the file to start reading from.
709 * This routine reads data from the buffer indicated in the private_data
710 * field of @file. It will start reading at @pos and copy up to @nbytes of
714 * This function returns the amount of data that was read (this could be
715 * less than @nbytes if the end of the file was reached).
717 static ssize_t fnic_stats_debugfs_read(struct file *file,
722 struct stats_debug_info *debug = file->private_data;
724 rc = simple_read_from_buffer(ubuf, nbytes, pos,
731 * fnic_stats_stats_release - Release the buffer used to store
733 * @inode: The inode pointer
734 * @file: The file pointer that contains the buffer to release
737 * This routine frees the buffer that was allocated when the debugfs
741 * This function returns zero.
743 static int fnic_stats_debugfs_release(struct inode *inode,
746 struct stats_debug_info *debug = file->private_data;
747 vfree(debug->debug_buffer);
752 static const struct file_operations fnic_stats_debugfs_fops = {
753 .owner = THIS_MODULE,
754 .open = fnic_stats_debugfs_open,
755 .read = fnic_stats_debugfs_read,
756 .release = fnic_stats_debugfs_release,
759 static const struct file_operations fnic_reset_debugfs_fops = {
760 .owner = THIS_MODULE,
761 .open = fnic_reset_stats_open,
762 .read = fnic_reset_stats_read,
763 .write = fnic_reset_stats_write,
764 .release = fnic_reset_stats_release,
768 * fnic_stats_init - Initialize stats struct and create stats file per fnic
771 * When Debugfs is configured this routine sets up the stats file per fnic
772 * It will create file stats and reset_stats under statistics/host# directory
773 * to log per fnic stats.
775 int fnic_stats_debugfs_init(struct fnic *fnic)
780 snprintf(name, sizeof(name), "host%d", fnic->lport->host->host_no);
782 if (!fnic_stats_debugfs_root) {
783 printk(KERN_DEBUG "fnic_stats root doesn't exist\n");
786 fnic->fnic_stats_debugfs_host = debugfs_create_dir(name,
787 fnic_stats_debugfs_root);
788 if (!fnic->fnic_stats_debugfs_host) {
789 printk(KERN_DEBUG "Cannot create host directory\n");
793 fnic->fnic_stats_debugfs_file = debugfs_create_file("stats",
794 S_IFREG|S_IRUGO|S_IWUSR,
795 fnic->fnic_stats_debugfs_host,
797 &fnic_stats_debugfs_fops);
798 if (!fnic->fnic_stats_debugfs_file) {
799 printk(KERN_DEBUG "Cannot create host stats file\n");
803 fnic->fnic_reset_debugfs_file = debugfs_create_file("reset_stats",
804 S_IFREG|S_IRUGO|S_IWUSR,
805 fnic->fnic_stats_debugfs_host,
807 &fnic_reset_debugfs_fops);
808 if (!fnic->fnic_reset_debugfs_file) {
809 printk(KERN_DEBUG "Cannot create host stats file\n");
817 * fnic_stats_debugfs_remove - Tear down debugfs infrastructure of stats
820 * When Debugfs is configured this routine removes debugfs file system
821 * elements that are specific to fnic stats.
823 void fnic_stats_debugfs_remove(struct fnic *fnic)
828 debugfs_remove(fnic->fnic_stats_debugfs_file);
829 fnic->fnic_stats_debugfs_file = NULL;
831 debugfs_remove(fnic->fnic_reset_debugfs_file);
832 fnic->fnic_reset_debugfs_file = NULL;
834 debugfs_remove(fnic->fnic_stats_debugfs_host);
835 fnic->fnic_stats_debugfs_host = NULL;