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 = vmalloc(sizeof(struct fc_trace_flag_type));
64 fc_trc_flag->fc_row_file = 0;
65 fc_trc_flag->fc_normal_file = 1;
66 fc_trc_flag->fnic_trace = 2;
67 fc_trc_flag->fc_trace = 3;
68 fc_trc_flag->fc_clear = 4;
75 * fnic_debugfs_terminate - Tear down debugfs infrastructure
78 * When Debugfs is configured this routine removes debugfs file system
79 * elements that are specific to fnic.
81 void fnic_debugfs_terminate(void)
83 debugfs_remove(fnic_stats_debugfs_root);
84 fnic_stats_debugfs_root = NULL;
86 debugfs_remove(fnic_trace_debugfs_root);
87 fnic_trace_debugfs_root = NULL;
94 * fnic_trace_ctrl_read -
95 * Read trace_enable ,fc_trace_enable
96 * or fc_trace_clear debugfs file
97 * @filp: The file pointer to read from.
98 * @ubuf: The buffer to copy the data to.
99 * @cnt: The number of bytes to read.
100 * @ppos: The position in the file to start reading from.
103 * This routine reads value of variable fnic_tracing_enabled or
104 * fnic_fc_tracing_enabled or fnic_fc_trace_cleared
105 * and stores into local @buf.
106 * It will start reading file at @ppos and
107 * copy up to @cnt of data to @ubuf from @buf.
110 * This function returns the amount of data that was read.
112 static ssize_t fnic_trace_ctrl_read(struct file *filp,
114 size_t cnt, loff_t *ppos)
120 trace_type = (u8 *)filp->private_data;
121 if (*trace_type == fc_trc_flag->fnic_trace)
122 len = sprintf(buf, "%d\n", fnic_tracing_enabled);
123 else if (*trace_type == fc_trc_flag->fc_trace)
124 len = sprintf(buf, "%d\n", fnic_fc_tracing_enabled);
125 else if (*trace_type == fc_trc_flag->fc_clear)
126 len = sprintf(buf, "%d\n", fnic_fc_trace_cleared);
128 pr_err("fnic: Cannot read to any debugfs file\n");
130 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
134 * fnic_trace_ctrl_write -
135 * Write to trace_enable, fc_trace_enable or
136 * fc_trace_clear debugfs file
137 * @filp: The file pointer to write from.
138 * @ubuf: The buffer to copy the data from.
139 * @cnt: The number of bytes to write.
140 * @ppos: The position in the file to start writing to.
143 * This routine writes data from user buffer @ubuf to buffer @buf and
144 * sets fc_trace_enable ,tracing_enable or fnic_fc_trace_cleared
145 * value as per user input.
148 * This function returns the amount of data that was written.
150 static ssize_t fnic_trace_ctrl_write(struct file *filp,
151 const char __user *ubuf,
152 size_t cnt, loff_t *ppos)
158 trace_type = (u8 *)filp->private_data;
160 if (cnt >= sizeof(buf))
163 if (copy_from_user(&buf, ubuf, cnt))
168 ret = kstrtoul(buf, 10, &val);
172 if (*trace_type == fc_trc_flag->fnic_trace)
173 fnic_tracing_enabled = val;
174 else if (*trace_type == fc_trc_flag->fc_trace)
175 fnic_fc_tracing_enabled = val;
176 else if (*trace_type == fc_trc_flag->fc_clear)
177 fnic_fc_trace_cleared = val;
179 pr_err("fnic: cannot write to any debugfs file\n");
186 static const struct file_operations fnic_trace_ctrl_fops = {
187 .owner = THIS_MODULE,
189 .read = fnic_trace_ctrl_read,
190 .write = fnic_trace_ctrl_write,
194 * fnic_trace_debugfs_open - Open the fnic trace log
195 * @inode: The inode pointer
196 * @file: The file pointer to attach the log output
199 * This routine is the entry point for the debugfs open file operation.
200 * It allocates the necessary buffer for the log, fills the buffer from
201 * the in-memory log and then returns a pointer to that log in
202 * the private_data field in @file.
205 * This function returns zero if successful. On error it will return
206 * a negative error value.
208 static int fnic_trace_debugfs_open(struct inode *inode,
211 fnic_dbgfs_t *fnic_dbg_prt;
213 rdata_ptr = (u8 *)inode->i_private;
214 fnic_dbg_prt = kzalloc(sizeof(fnic_dbgfs_t), GFP_KERNEL);
218 if (*rdata_ptr == fc_trc_flag->fnic_trace) {
219 fnic_dbg_prt->buffer = vmalloc(array3_size(3, trace_max_pages,
221 if (!fnic_dbg_prt->buffer) {
225 memset((void *)fnic_dbg_prt->buffer, 0,
226 3 * (trace_max_pages * PAGE_SIZE));
227 fnic_dbg_prt->buffer_len = fnic_get_trace_data(fnic_dbg_prt);
229 fnic_dbg_prt->buffer =
230 vmalloc(array3_size(3, fnic_fc_trace_max_pages,
232 if (!fnic_dbg_prt->buffer) {
236 memset((void *)fnic_dbg_prt->buffer, 0,
237 3 * (fnic_fc_trace_max_pages * PAGE_SIZE));
238 fnic_dbg_prt->buffer_len =
239 fnic_fc_trace_get_data(fnic_dbg_prt, *rdata_ptr);
241 file->private_data = fnic_dbg_prt;
247 * fnic_trace_debugfs_lseek - Seek through a debugfs file
248 * @file: The file pointer to seek through.
249 * @offset: The offset to seek to or the amount to seek by.
250 * @howto: Indicates how to seek.
253 * This routine is the entry point for the debugfs lseek file operation.
254 * The @howto parameter indicates whether @offset is the offset to directly
255 * seek to, or if it is a value to seek forward or reverse by. This function
256 * figures out what the new offset of the debugfs file will be and assigns
257 * that value to the f_pos field of @file.
260 * This function returns the new offset if successful and returns a negative
261 * error if unable to process the seek.
263 static loff_t fnic_trace_debugfs_lseek(struct file *file,
267 fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
268 return fixed_size_llseek(file, offset, howto,
269 fnic_dbg_prt->buffer_len);
273 * fnic_trace_debugfs_read - Read a debugfs file
274 * @file: The file pointer to read from.
275 * @ubuf: The buffer to copy the data to.
276 * @nbytes: The number of bytes to read.
277 * @pos: The position in the file to start reading from.
280 * This routine reads data from the buffer indicated in the private_data
281 * field of @file. It will start reading at @pos and copy up to @nbytes of
285 * This function returns the amount of data that was read (this could be
286 * less than @nbytes if the end of the file was reached).
288 static ssize_t fnic_trace_debugfs_read(struct file *file,
293 fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
295 rc = simple_read_from_buffer(ubuf, nbytes, pos,
296 fnic_dbg_prt->buffer,
297 fnic_dbg_prt->buffer_len);
302 * fnic_trace_debugfs_release - Release the buffer used to store
304 * @inode: The inode pointer
305 * @file: The file pointer that contains the buffer to release
308 * This routine frees the buffer that was allocated when the debugfs
312 * This function returns zero.
314 static int fnic_trace_debugfs_release(struct inode *inode,
317 fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
319 vfree(fnic_dbg_prt->buffer);
324 static const struct file_operations fnic_trace_debugfs_fops = {
325 .owner = THIS_MODULE,
326 .open = fnic_trace_debugfs_open,
327 .llseek = fnic_trace_debugfs_lseek,
328 .read = fnic_trace_debugfs_read,
329 .release = fnic_trace_debugfs_release,
333 * fnic_trace_debugfs_init - Initialize debugfs for fnic trace logging
336 * When Debugfs is configured this routine sets up the fnic debugfs
337 * file system. If not already created, this routine will create the
338 * create file trace to log fnic trace buffer output into debugfs and
339 * it will also create file trace_enable to control enable/disable of
340 * trace logging into trace buffer.
342 void fnic_trace_debugfs_init(void)
344 fnic_trace_enable = debugfs_create_file("tracing_enable",
345 S_IFREG|S_IRUGO|S_IWUSR,
346 fnic_trace_debugfs_root,
347 &(fc_trc_flag->fnic_trace),
348 &fnic_trace_ctrl_fops);
350 fnic_trace_debugfs_file = debugfs_create_file("trace",
351 S_IFREG|S_IRUGO|S_IWUSR,
352 fnic_trace_debugfs_root,
353 &(fc_trc_flag->fnic_trace),
354 &fnic_trace_debugfs_fops);
358 * fnic_trace_debugfs_terminate - Tear down debugfs infrastructure
361 * When Debugfs is configured this routine removes debugfs file system
362 * elements that are specific to fnic trace logging.
364 void fnic_trace_debugfs_terminate(void)
366 debugfs_remove(fnic_trace_debugfs_file);
367 fnic_trace_debugfs_file = NULL;
369 debugfs_remove(fnic_trace_enable);
370 fnic_trace_enable = NULL;
374 * fnic_fc_trace_debugfs_init -
375 * Initialize debugfs for fnic control frame trace logging
378 * When Debugfs is configured this routine sets up the fnic_fc debugfs
379 * file system. If not already created, this routine will create the
380 * create file trace to log fnic fc trace buffer output into debugfs and
381 * it will also create file fc_trace_enable to control enable/disable of
382 * trace logging into trace buffer.
385 void fnic_fc_trace_debugfs_init(void)
387 fnic_fc_trace_enable = debugfs_create_file("fc_trace_enable",
388 S_IFREG|S_IRUGO|S_IWUSR,
389 fnic_trace_debugfs_root,
390 &(fc_trc_flag->fc_trace),
391 &fnic_trace_ctrl_fops);
393 fnic_fc_trace_clear = debugfs_create_file("fc_trace_clear",
394 S_IFREG|S_IRUGO|S_IWUSR,
395 fnic_trace_debugfs_root,
396 &(fc_trc_flag->fc_clear),
397 &fnic_trace_ctrl_fops);
399 fnic_fc_rdata_trace_debugfs_file =
400 debugfs_create_file("fc_trace_rdata",
401 S_IFREG|S_IRUGO|S_IWUSR,
402 fnic_trace_debugfs_root,
403 &(fc_trc_flag->fc_normal_file),
404 &fnic_trace_debugfs_fops);
406 fnic_fc_trace_debugfs_file =
407 debugfs_create_file("fc_trace",
408 S_IFREG|S_IRUGO|S_IWUSR,
409 fnic_trace_debugfs_root,
410 &(fc_trc_flag->fc_row_file),
411 &fnic_trace_debugfs_fops);
415 * fnic_fc_trace_debugfs_terminate - Tear down debugfs infrastructure
418 * When Debugfs is configured this routine removes debugfs file system
419 * elements that are specific to fnic_fc trace logging.
422 void fnic_fc_trace_debugfs_terminate(void)
424 debugfs_remove(fnic_fc_trace_debugfs_file);
425 fnic_fc_trace_debugfs_file = NULL;
427 debugfs_remove(fnic_fc_rdata_trace_debugfs_file);
428 fnic_fc_rdata_trace_debugfs_file = NULL;
430 debugfs_remove(fnic_fc_trace_enable);
431 fnic_fc_trace_enable = NULL;
433 debugfs_remove(fnic_fc_trace_clear);
434 fnic_fc_trace_clear = NULL;
438 * fnic_reset_stats_open - Open the reset_stats file
439 * @inode: The inode pointer.
440 * @file: The file pointer to attach the stats reset flag.
443 * This routine opens a debugsfs file reset_stats and stores i_private data
444 * to debug structure to retrieve later for while performing other
448 * This function returns zero if successful.
450 static int fnic_reset_stats_open(struct inode *inode, struct file *file)
452 struct stats_debug_info *debug;
454 debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
458 debug->i_private = inode->i_private;
460 file->private_data = debug;
466 * fnic_reset_stats_read - Read a reset_stats debugfs file
467 * @filp: The file pointer to read from.
468 * @ubuf: The buffer to copy the data to.
469 * @cnt: The number of bytes to read.
470 * @ppos: The position in the file to start reading from.
473 * This routine reads value of variable reset_stats
474 * and stores into local @buf. It will start reading file at @ppos and
475 * copy up to @cnt of data to @ubuf from @buf.
478 * This function returns the amount of data that was read.
480 static ssize_t fnic_reset_stats_read(struct file *file,
482 size_t cnt, loff_t *ppos)
484 struct stats_debug_info *debug = file->private_data;
485 struct fnic *fnic = (struct fnic *)debug->i_private;
489 len = sprintf(buf, "%u\n", fnic->reset_stats);
491 return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
495 * fnic_reset_stats_write - Write to reset_stats debugfs file
496 * @filp: The file pointer to write from.
497 * @ubuf: The buffer to copy the data from.
498 * @cnt: The number of bytes to write.
499 * @ppos: The position in the file to start writing to.
502 * This routine writes data from user buffer @ubuf to buffer @buf and
503 * resets cumulative stats of fnic.
506 * This function returns the amount of data that was written.
508 static ssize_t fnic_reset_stats_write(struct file *file,
509 const char __user *ubuf,
510 size_t cnt, loff_t *ppos)
512 struct stats_debug_info *debug = file->private_data;
513 struct fnic *fnic = (struct fnic *)debug->i_private;
514 struct fnic_stats *stats = &fnic->fnic_stats;
515 u64 *io_stats_p = (u64 *)&stats->io_stats;
516 u64 *fw_stats_p = (u64 *)&stats->fw_stats;
521 if (cnt >= sizeof(buf))
524 if (copy_from_user(&buf, ubuf, cnt))
529 ret = kstrtoul(buf, 10, &val);
533 fnic->reset_stats = val;
535 if (fnic->reset_stats) {
536 /* Skip variable is used to avoid descrepancies to Num IOs
537 * and IO Completions stats. Skip incrementing No IO Compls
538 * for pending active IOs after reset stats
540 atomic64_set(&fnic->io_cmpl_skip,
541 atomic64_read(&stats->io_stats.active_ios));
542 memset(&stats->abts_stats, 0, sizeof(struct abort_stats));
543 memset(&stats->term_stats, 0,
544 sizeof(struct terminate_stats));
545 memset(&stats->reset_stats, 0, sizeof(struct reset_stats));
546 memset(&stats->misc_stats, 0, sizeof(struct misc_stats));
547 memset(&stats->vlan_stats, 0, sizeof(struct vlan_stats));
548 memset(io_stats_p+1, 0,
549 sizeof(struct io_path_stats) - sizeof(u64));
550 memset(fw_stats_p+1, 0,
551 sizeof(struct fw_stats) - sizeof(u64));
552 ktime_get_real_ts64(&stats->stats_timestamps.last_reset_time);
560 * fnic_reset_stats_release - Release the buffer used to store
562 * @inode: The inode pointer
563 * @file: The file pointer that contains the buffer to release
566 * This routine frees the buffer that was allocated when the debugfs
570 * This function returns zero.
572 static int fnic_reset_stats_release(struct inode *inode,
575 struct stats_debug_info *debug = file->private_data;
581 * fnic_stats_debugfs_open - Open the stats file for specific host
582 * and get fnic stats.
583 * @inode: The inode pointer.
584 * @file: The file pointer to attach the specific host statistics.
587 * This routine opens a debugsfs file stats of specific host and print
591 * This function returns zero if successful.
593 static int fnic_stats_debugfs_open(struct inode *inode,
596 struct fnic *fnic = inode->i_private;
597 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
598 struct stats_debug_info *debug;
599 int buf_size = 2 * PAGE_SIZE;
601 debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
605 debug->debug_buffer = vmalloc(buf_size);
606 if (!debug->debug_buffer) {
611 debug->buf_size = buf_size;
612 memset((void *)debug->debug_buffer, 0, buf_size);
613 debug->buffer_len = fnic_get_stats_data(debug, fnic_stats);
615 file->private_data = debug;
621 * fnic_stats_debugfs_read - Read a debugfs file
622 * @file: The file pointer to read from.
623 * @ubuf: The buffer to copy the data to.
624 * @nbytes: The number of bytes to read.
625 * @pos: The position in the file to start reading from.
628 * This routine reads data from the buffer indicated in the private_data
629 * field of @file. It will start reading at @pos and copy up to @nbytes of
633 * This function returns the amount of data that was read (this could be
634 * less than @nbytes if the end of the file was reached).
636 static ssize_t fnic_stats_debugfs_read(struct file *file,
641 struct stats_debug_info *debug = file->private_data;
643 rc = simple_read_from_buffer(ubuf, nbytes, pos,
650 * fnic_stats_stats_release - Release the buffer used to store
652 * @inode: The inode pointer
653 * @file: The file pointer that contains the buffer to release
656 * This routine frees the buffer that was allocated when the debugfs
660 * This function returns zero.
662 static int fnic_stats_debugfs_release(struct inode *inode,
665 struct stats_debug_info *debug = file->private_data;
666 vfree(debug->debug_buffer);
671 static const struct file_operations fnic_stats_debugfs_fops = {
672 .owner = THIS_MODULE,
673 .open = fnic_stats_debugfs_open,
674 .read = fnic_stats_debugfs_read,
675 .release = fnic_stats_debugfs_release,
678 static const struct file_operations fnic_reset_debugfs_fops = {
679 .owner = THIS_MODULE,
680 .open = fnic_reset_stats_open,
681 .read = fnic_reset_stats_read,
682 .write = fnic_reset_stats_write,
683 .release = fnic_reset_stats_release,
687 * fnic_stats_init - Initialize stats struct and create stats file per fnic
690 * When Debugfs is configured this routine sets up the stats file per fnic
691 * It will create file stats and reset_stats under statistics/host# directory
692 * to log per fnic stats.
694 void fnic_stats_debugfs_init(struct fnic *fnic)
698 snprintf(name, sizeof(name), "host%d", fnic->lport->host->host_no);
700 fnic->fnic_stats_debugfs_host = debugfs_create_dir(name,
701 fnic_stats_debugfs_root);
703 fnic->fnic_stats_debugfs_file = debugfs_create_file("stats",
704 S_IFREG|S_IRUGO|S_IWUSR,
705 fnic->fnic_stats_debugfs_host,
707 &fnic_stats_debugfs_fops);
709 fnic->fnic_reset_debugfs_file = debugfs_create_file("reset_stats",
710 S_IFREG|S_IRUGO|S_IWUSR,
711 fnic->fnic_stats_debugfs_host,
713 &fnic_reset_debugfs_fops);
717 * fnic_stats_debugfs_remove - Tear down debugfs infrastructure of stats
720 * When Debugfs is configured this routine removes debugfs file system
721 * elements that are specific to fnic stats.
723 void fnic_stats_debugfs_remove(struct fnic *fnic)
728 debugfs_remove(fnic->fnic_stats_debugfs_file);
729 fnic->fnic_stats_debugfs_file = NULL;
731 debugfs_remove(fnic->fnic_reset_debugfs_file);
732 fnic->fnic_reset_debugfs_file = NULL;
734 debugfs_remove(fnic->fnic_stats_debugfs_host);
735 fnic->fnic_stats_debugfs_host = NULL;