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>
21 #include <linux/vmalloc.h>
24 static struct dentry *fnic_trace_debugfs_root;
25 static struct dentry *fnic_trace_debugfs_file;
26 static struct dentry *fnic_trace_enable;
27 static struct dentry *fnic_stats_debugfs_root;
29 static struct dentry *fnic_fc_trace_debugfs_file;
30 static struct dentry *fnic_fc_rdata_trace_debugfs_file;
31 static struct dentry *fnic_fc_trace_enable;
32 static struct dentry *fnic_fc_trace_clear;
34 struct fc_trace_flag_type {
42 static struct fc_trace_flag_type *fc_trc_flag;
45 * fnic_debugfs_init - Initialize debugfs for fnic debug logging
48 * When Debugfs is configured this routine sets up the fnic debugfs
49 * file system. If not already created, this routine will create the
50 * fnic directory and statistics directory for trace buffer and
53 int fnic_debugfs_init(void)
55 fnic_trace_debugfs_root = debugfs_create_dir("fnic", NULL);
57 fnic_stats_debugfs_root = debugfs_create_dir("statistics",
58 fnic_trace_debugfs_root);
60 /* Allocate memory to structure */
61 fc_trc_flag = (struct fc_trace_flag_type *)
62 vmalloc(sizeof(struct fc_trace_flag_type));
65 fc_trc_flag->fc_row_file = 0;
66 fc_trc_flag->fc_normal_file = 1;
67 fc_trc_flag->fnic_trace = 2;
68 fc_trc_flag->fc_trace = 3;
69 fc_trc_flag->fc_clear = 4;
77 * fnic_debugfs_terminate - Tear down debugfs infrastructure
80 * When Debugfs is configured this routine removes debugfs file system
81 * elements that are specific to fnic.
83 void fnic_debugfs_terminate(void)
85 debugfs_remove(fnic_stats_debugfs_root);
86 fnic_stats_debugfs_root = NULL;
88 debugfs_remove(fnic_trace_debugfs_root);
89 fnic_trace_debugfs_root = NULL;
96 * fnic_trace_ctrl_read -
97 * Read trace_enable ,fc_trace_enable
98 * or fc_trace_clear debugfs file
99 * @filp: The file pointer to read from.
100 * @ubuf: The buffer to copy the data to.
101 * @cnt: The number of bytes to read.
102 * @ppos: The position in the file to start reading from.
105 * This routine reads value of variable fnic_tracing_enabled or
106 * fnic_fc_tracing_enabled or fnic_fc_trace_cleared
107 * and stores into local @buf.
108 * It will start reading file at @ppos and
109 * copy up to @cnt of data to @ubuf from @buf.
112 * This function returns the amount of data that was read.
114 static ssize_t fnic_trace_ctrl_read(struct file *filp,
116 size_t cnt, loff_t *ppos)
122 trace_type = (u8 *)filp->private_data;
123 if (*trace_type == fc_trc_flag->fnic_trace)
124 len = sprintf(buf, "%d\n", fnic_tracing_enabled);
125 else if (*trace_type == fc_trc_flag->fc_trace)
126 len = sprintf(buf, "%d\n", fnic_fc_tracing_enabled);
127 else if (*trace_type == fc_trc_flag->fc_clear)
128 len = sprintf(buf, "%d\n", fnic_fc_trace_cleared);
130 pr_err("fnic: Cannot read to any debugfs file\n");
132 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
136 * fnic_trace_ctrl_write -
137 * Write to trace_enable, fc_trace_enable or
138 * fc_trace_clear debugfs file
139 * @filp: The file pointer to write from.
140 * @ubuf: The buffer to copy the data from.
141 * @cnt: The number of bytes to write.
142 * @ppos: The position in the file to start writing to.
145 * This routine writes data from user buffer @ubuf to buffer @buf and
146 * sets fc_trace_enable ,tracing_enable or fnic_fc_trace_cleared
147 * value as per user input.
150 * This function returns the amount of data that was written.
152 static ssize_t fnic_trace_ctrl_write(struct file *filp,
153 const char __user *ubuf,
154 size_t cnt, loff_t *ppos)
160 trace_type = (u8 *)filp->private_data;
162 if (cnt >= sizeof(buf))
165 if (copy_from_user(&buf, ubuf, cnt))
170 ret = kstrtoul(buf, 10, &val);
174 if (*trace_type == fc_trc_flag->fnic_trace)
175 fnic_tracing_enabled = val;
176 else if (*trace_type == fc_trc_flag->fc_trace)
177 fnic_fc_tracing_enabled = val;
178 else if (*trace_type == fc_trc_flag->fc_clear)
179 fnic_fc_trace_cleared = val;
181 pr_err("fnic: cannot write to any debugfs file\n");
188 static const struct file_operations fnic_trace_ctrl_fops = {
189 .owner = THIS_MODULE,
191 .read = fnic_trace_ctrl_read,
192 .write = fnic_trace_ctrl_write,
196 * fnic_trace_debugfs_open - Open the fnic trace log
197 * @inode: The inode pointer
198 * @file: The file pointer to attach the log output
201 * This routine is the entry point for the debugfs open file operation.
202 * It allocates the necessary buffer for the log, fills the buffer from
203 * the in-memory log and then returns a pointer to that log in
204 * the private_data field in @file.
207 * This function returns zero if successful. On error it will return
208 * a negative error value.
210 static int fnic_trace_debugfs_open(struct inode *inode,
213 fnic_dbgfs_t *fnic_dbg_prt;
215 rdata_ptr = (u8 *)inode->i_private;
216 fnic_dbg_prt = kzalloc(sizeof(fnic_dbgfs_t), GFP_KERNEL);
220 if (*rdata_ptr == fc_trc_flag->fnic_trace) {
221 fnic_dbg_prt->buffer = vmalloc(array3_size(3, trace_max_pages,
223 if (!fnic_dbg_prt->buffer) {
227 memset((void *)fnic_dbg_prt->buffer, 0,
228 3 * (trace_max_pages * PAGE_SIZE));
229 fnic_dbg_prt->buffer_len = fnic_get_trace_data(fnic_dbg_prt);
231 fnic_dbg_prt->buffer =
232 vmalloc(array3_size(3, fnic_fc_trace_max_pages,
234 if (!fnic_dbg_prt->buffer) {
238 memset((void *)fnic_dbg_prt->buffer, 0,
239 3 * (fnic_fc_trace_max_pages * PAGE_SIZE));
240 fnic_dbg_prt->buffer_len =
241 fnic_fc_trace_get_data(fnic_dbg_prt, *rdata_ptr);
243 file->private_data = fnic_dbg_prt;
249 * fnic_trace_debugfs_lseek - Seek through a debugfs file
250 * @file: The file pointer to seek through.
251 * @offset: The offset to seek to or the amount to seek by.
252 * @howto: Indicates how to seek.
255 * This routine is the entry point for the debugfs lseek file operation.
256 * The @howto parameter indicates whether @offset is the offset to directly
257 * seek to, or if it is a value to seek forward or reverse by. This function
258 * figures out what the new offset of the debugfs file will be and assigns
259 * that value to the f_pos field of @file.
262 * This function returns the new offset if successful and returns a negative
263 * error if unable to process the seek.
265 static loff_t fnic_trace_debugfs_lseek(struct file *file,
269 fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
270 return fixed_size_llseek(file, offset, howto,
271 fnic_dbg_prt->buffer_len);
275 * fnic_trace_debugfs_read - Read a debugfs file
276 * @file: The file pointer to read from.
277 * @ubuf: The buffer to copy the data to.
278 * @nbytes: The number of bytes to read.
279 * @pos: The position in the file to start reading from.
282 * This routine reads data from the buffer indicated in the private_data
283 * field of @file. It will start reading at @pos and copy up to @nbytes of
287 * This function returns the amount of data that was read (this could be
288 * less than @nbytes if the end of the file was reached).
290 static ssize_t fnic_trace_debugfs_read(struct file *file,
295 fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
297 rc = simple_read_from_buffer(ubuf, nbytes, pos,
298 fnic_dbg_prt->buffer,
299 fnic_dbg_prt->buffer_len);
304 * fnic_trace_debugfs_release - Release the buffer used to store
306 * @inode: The inode pointer
307 * @file: The file pointer that contains the buffer to release
310 * This routine frees the buffer that was allocated when the debugfs
314 * This function returns zero.
316 static int fnic_trace_debugfs_release(struct inode *inode,
319 fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
321 vfree(fnic_dbg_prt->buffer);
326 static const struct file_operations fnic_trace_debugfs_fops = {
327 .owner = THIS_MODULE,
328 .open = fnic_trace_debugfs_open,
329 .llseek = fnic_trace_debugfs_lseek,
330 .read = fnic_trace_debugfs_read,
331 .release = fnic_trace_debugfs_release,
335 * fnic_trace_debugfs_init - Initialize debugfs for fnic trace logging
338 * When Debugfs is configured this routine sets up the fnic debugfs
339 * file system. If not already created, this routine will create the
340 * create file trace to log fnic trace buffer output into debugfs and
341 * it will also create file trace_enable to control enable/disable of
342 * trace logging into trace buffer.
344 void fnic_trace_debugfs_init(void)
346 fnic_trace_enable = debugfs_create_file("tracing_enable",
347 S_IFREG|S_IRUGO|S_IWUSR,
348 fnic_trace_debugfs_root,
349 &(fc_trc_flag->fnic_trace),
350 &fnic_trace_ctrl_fops);
352 fnic_trace_debugfs_file = debugfs_create_file("trace",
353 S_IFREG|S_IRUGO|S_IWUSR,
354 fnic_trace_debugfs_root,
355 &(fc_trc_flag->fnic_trace),
356 &fnic_trace_debugfs_fops);
360 * fnic_trace_debugfs_terminate - Tear down debugfs infrastructure
363 * When Debugfs is configured this routine removes debugfs file system
364 * elements that are specific to fnic trace logging.
366 void fnic_trace_debugfs_terminate(void)
368 debugfs_remove(fnic_trace_debugfs_file);
369 fnic_trace_debugfs_file = NULL;
371 debugfs_remove(fnic_trace_enable);
372 fnic_trace_enable = NULL;
376 * fnic_fc_trace_debugfs_init -
377 * Initialize debugfs for fnic control frame trace logging
380 * When Debugfs is configured this routine sets up the fnic_fc debugfs
381 * file system. If not already created, this routine will create the
382 * create file trace to log fnic fc trace buffer output into debugfs and
383 * it will also create file fc_trace_enable to control enable/disable of
384 * trace logging into trace buffer.
387 void fnic_fc_trace_debugfs_init(void)
389 fnic_fc_trace_enable = debugfs_create_file("fc_trace_enable",
390 S_IFREG|S_IRUGO|S_IWUSR,
391 fnic_trace_debugfs_root,
392 &(fc_trc_flag->fc_trace),
393 &fnic_trace_ctrl_fops);
395 fnic_fc_trace_clear = debugfs_create_file("fc_trace_clear",
396 S_IFREG|S_IRUGO|S_IWUSR,
397 fnic_trace_debugfs_root,
398 &(fc_trc_flag->fc_clear),
399 &fnic_trace_ctrl_fops);
401 fnic_fc_rdata_trace_debugfs_file =
402 debugfs_create_file("fc_trace_rdata",
403 S_IFREG|S_IRUGO|S_IWUSR,
404 fnic_trace_debugfs_root,
405 &(fc_trc_flag->fc_normal_file),
406 &fnic_trace_debugfs_fops);
408 fnic_fc_trace_debugfs_file =
409 debugfs_create_file("fc_trace",
410 S_IFREG|S_IRUGO|S_IWUSR,
411 fnic_trace_debugfs_root,
412 &(fc_trc_flag->fc_row_file),
413 &fnic_trace_debugfs_fops);
417 * fnic_fc_trace_debugfs_terminate - Tear down debugfs infrastructure
420 * When Debugfs is configured this routine removes debugfs file system
421 * elements that are specific to fnic_fc trace logging.
424 void fnic_fc_trace_debugfs_terminate(void)
426 debugfs_remove(fnic_fc_trace_debugfs_file);
427 fnic_fc_trace_debugfs_file = NULL;
429 debugfs_remove(fnic_fc_rdata_trace_debugfs_file);
430 fnic_fc_rdata_trace_debugfs_file = NULL;
432 debugfs_remove(fnic_fc_trace_enable);
433 fnic_fc_trace_enable = NULL;
435 debugfs_remove(fnic_fc_trace_clear);
436 fnic_fc_trace_clear = NULL;
440 * fnic_reset_stats_open - Open the reset_stats file
441 * @inode: The inode pointer.
442 * @file: The file pointer to attach the stats reset flag.
445 * This routine opens a debugsfs file reset_stats and stores i_private data
446 * to debug structure to retrieve later for while performing other
450 * This function returns zero if successful.
452 static int fnic_reset_stats_open(struct inode *inode, struct file *file)
454 struct stats_debug_info *debug;
456 debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
460 debug->i_private = inode->i_private;
462 file->private_data = debug;
468 * fnic_reset_stats_read - Read a reset_stats debugfs file
469 * @filp: The file pointer to read from.
470 * @ubuf: The buffer to copy the data to.
471 * @cnt: The number of bytes to read.
472 * @ppos: The position in the file to start reading from.
475 * This routine reads value of variable reset_stats
476 * and stores into local @buf. It will start reading file at @ppos and
477 * copy up to @cnt of data to @ubuf from @buf.
480 * This function returns the amount of data that was read.
482 static ssize_t fnic_reset_stats_read(struct file *file,
484 size_t cnt, loff_t *ppos)
486 struct stats_debug_info *debug = file->private_data;
487 struct fnic *fnic = (struct fnic *)debug->i_private;
491 len = sprintf(buf, "%u\n", fnic->reset_stats);
493 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
497 * fnic_reset_stats_write - Write to reset_stats debugfs file
498 * @filp: The file pointer to write from.
499 * @ubuf: The buffer to copy the data from.
500 * @cnt: The number of bytes to write.
501 * @ppos: The position in the file to start writing to.
504 * This routine writes data from user buffer @ubuf to buffer @buf and
505 * resets cumulative stats of fnic.
508 * This function returns the amount of data that was written.
510 static ssize_t fnic_reset_stats_write(struct file *file,
511 const char __user *ubuf,
512 size_t cnt, loff_t *ppos)
514 struct stats_debug_info *debug = file->private_data;
515 struct fnic *fnic = (struct fnic *)debug->i_private;
516 struct fnic_stats *stats = &fnic->fnic_stats;
517 u64 *io_stats_p = (u64 *)&stats->io_stats;
518 u64 *fw_stats_p = (u64 *)&stats->fw_stats;
523 if (cnt >= sizeof(buf))
526 if (copy_from_user(&buf, ubuf, cnt))
531 ret = kstrtoul(buf, 10, &val);
535 fnic->reset_stats = val;
537 if (fnic->reset_stats) {
538 /* Skip variable is used to avoid descrepancies to Num IOs
539 * and IO Completions stats. Skip incrementing No IO Compls
540 * for pending active IOs after reset stats
542 atomic64_set(&fnic->io_cmpl_skip,
543 atomic64_read(&stats->io_stats.active_ios));
544 memset(&stats->abts_stats, 0, sizeof(struct abort_stats));
545 memset(&stats->term_stats, 0,
546 sizeof(struct terminate_stats));
547 memset(&stats->reset_stats, 0, sizeof(struct reset_stats));
548 memset(&stats->misc_stats, 0, sizeof(struct misc_stats));
549 memset(&stats->vlan_stats, 0, sizeof(struct vlan_stats));
550 memset(io_stats_p+1, 0,
551 sizeof(struct io_path_stats) - sizeof(u64));
552 memset(fw_stats_p+1, 0,
553 sizeof(struct fw_stats) - sizeof(u64));
554 ktime_get_real_ts64(&stats->stats_timestamps.last_reset_time);
562 * fnic_reset_stats_release - Release the buffer used to store
564 * @inode: The inode pointer
565 * @file: The file pointer that contains the buffer to release
568 * This routine frees the buffer that was allocated when the debugfs
572 * This function returns zero.
574 static int fnic_reset_stats_release(struct inode *inode,
577 struct stats_debug_info *debug = file->private_data;
583 * fnic_stats_debugfs_open - Open the stats file for specific host
584 * and get fnic stats.
585 * @inode: The inode pointer.
586 * @file: The file pointer to attach the specific host statistics.
589 * This routine opens a debugsfs file stats of specific host and print
593 * This function returns zero if successful.
595 static int fnic_stats_debugfs_open(struct inode *inode,
598 struct fnic *fnic = inode->i_private;
599 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
600 struct stats_debug_info *debug;
601 int buf_size = 2 * PAGE_SIZE;
603 debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
607 debug->debug_buffer = vmalloc(buf_size);
608 if (!debug->debug_buffer) {
613 debug->buf_size = buf_size;
614 memset((void *)debug->debug_buffer, 0, buf_size);
615 debug->buffer_len = fnic_get_stats_data(debug, fnic_stats);
617 file->private_data = debug;
623 * fnic_stats_debugfs_read - Read a debugfs file
624 * @file: The file pointer to read from.
625 * @ubuf: The buffer to copy the data to.
626 * @nbytes: The number of bytes to read.
627 * @pos: The position in the file to start reading from.
630 * This routine reads data from the buffer indicated in the private_data
631 * field of @file. It will start reading at @pos and copy up to @nbytes of
635 * This function returns the amount of data that was read (this could be
636 * less than @nbytes if the end of the file was reached).
638 static ssize_t fnic_stats_debugfs_read(struct file *file,
643 struct stats_debug_info *debug = file->private_data;
645 rc = simple_read_from_buffer(ubuf, nbytes, pos,
652 * fnic_stats_stats_release - Release the buffer used to store
654 * @inode: The inode pointer
655 * @file: The file pointer that contains the buffer to release
658 * This routine frees the buffer that was allocated when the debugfs
662 * This function returns zero.
664 static int fnic_stats_debugfs_release(struct inode *inode,
667 struct stats_debug_info *debug = file->private_data;
668 vfree(debug->debug_buffer);
673 static const struct file_operations fnic_stats_debugfs_fops = {
674 .owner = THIS_MODULE,
675 .open = fnic_stats_debugfs_open,
676 .read = fnic_stats_debugfs_read,
677 .release = fnic_stats_debugfs_release,
680 static const struct file_operations fnic_reset_debugfs_fops = {
681 .owner = THIS_MODULE,
682 .open = fnic_reset_stats_open,
683 .read = fnic_reset_stats_read,
684 .write = fnic_reset_stats_write,
685 .release = fnic_reset_stats_release,
689 * fnic_stats_init - Initialize stats struct and create stats file per fnic
692 * When Debugfs is configured this routine sets up the stats file per fnic
693 * It will create file stats and reset_stats under statistics/host# directory
694 * to log per fnic stats.
696 void fnic_stats_debugfs_init(struct fnic *fnic)
700 snprintf(name, sizeof(name), "host%d", fnic->lport->host->host_no);
702 fnic->fnic_stats_debugfs_host = debugfs_create_dir(name,
703 fnic_stats_debugfs_root);
705 fnic->fnic_stats_debugfs_file = debugfs_create_file("stats",
706 S_IFREG|S_IRUGO|S_IWUSR,
707 fnic->fnic_stats_debugfs_host,
709 &fnic_stats_debugfs_fops);
711 fnic->fnic_reset_debugfs_file = debugfs_create_file("reset_stats",
712 S_IFREG|S_IRUGO|S_IWUSR,
713 fnic->fnic_stats_debugfs_host,
715 &fnic_reset_debugfs_fops);
719 * fnic_stats_debugfs_remove - Tear down debugfs infrastructure of stats
722 * When Debugfs is configured this routine removes debugfs file system
723 * elements that are specific to fnic stats.
725 void fnic_stats_debugfs_remove(struct fnic *fnic)
730 debugfs_remove(fnic->fnic_stats_debugfs_file);
731 fnic->fnic_stats_debugfs_file = NULL;
733 debugfs_remove(fnic->fnic_reset_debugfs_file);
734 fnic->fnic_reset_debugfs_file = NULL;
736 debugfs_remove(fnic->fnic_stats_debugfs_host);
737 fnic->fnic_stats_debugfs_host = NULL;