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)
56 fnic_trace_debugfs_root
= debugfs_create_dir("fnic", NULL
);
58 fnic_stats_debugfs_root
= debugfs_create_dir("statistics",
59 fnic_trace_debugfs_root
);
61 /* Allocate memory to structure */
62 fc_trc_flag
= (struct fc_trace_flag_type
*)
63 vmalloc(sizeof(struct fc_trace_flag_type
));
66 fc_trc_flag
->fc_row_file
= 0;
67 fc_trc_flag
->fc_normal_file
= 1;
68 fc_trc_flag
->fnic_trace
= 2;
69 fc_trc_flag
->fc_trace
= 3;
70 fc_trc_flag
->fc_clear
= 4;
78 * fnic_debugfs_terminate - Tear down debugfs infrastructure
81 * When Debugfs is configured this routine removes debugfs file system
82 * elements that are specific to fnic.
84 void fnic_debugfs_terminate(void)
86 debugfs_remove(fnic_stats_debugfs_root
);
87 fnic_stats_debugfs_root
= NULL
;
89 debugfs_remove(fnic_trace_debugfs_root
);
90 fnic_trace_debugfs_root
= NULL
;
97 * fnic_trace_ctrl_read -
98 * Read trace_enable ,fc_trace_enable
99 * or fc_trace_clear debugfs file
100 * @filp: The file pointer to read from.
101 * @ubuf: The buffer to copy the data to.
102 * @cnt: The number of bytes to read.
103 * @ppos: The position in the file to start reading from.
106 * This routine reads value of variable fnic_tracing_enabled or
107 * fnic_fc_tracing_enabled or fnic_fc_trace_cleared
108 * and stores into local @buf.
109 * It will start reading file at @ppos and
110 * copy up to @cnt of data to @ubuf from @buf.
113 * This function returns the amount of data that was read.
115 static ssize_t
fnic_trace_ctrl_read(struct file
*filp
,
117 size_t cnt
, loff_t
*ppos
)
123 trace_type
= (u8
*)filp
->private_data
;
124 if (*trace_type
== fc_trc_flag
->fnic_trace
)
125 len
= sprintf(buf
, "%u\n", fnic_tracing_enabled
);
126 else if (*trace_type
== fc_trc_flag
->fc_trace
)
127 len
= sprintf(buf
, "%u\n", fnic_fc_tracing_enabled
);
128 else if (*trace_type
== fc_trc_flag
->fc_clear
)
129 len
= sprintf(buf
, "%u\n", fnic_fc_trace_cleared
);
131 pr_err("fnic: Cannot read to any debugfs file\n");
133 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, len
);
137 * fnic_trace_ctrl_write -
138 * Write to trace_enable, fc_trace_enable or
139 * fc_trace_clear debugfs file
140 * @filp: The file pointer to write from.
141 * @ubuf: The buffer to copy the data from.
142 * @cnt: The number of bytes to write.
143 * @ppos: The position in the file to start writing to.
146 * This routine writes data from user buffer @ubuf to buffer @buf and
147 * sets fc_trace_enable ,tracing_enable or fnic_fc_trace_cleared
148 * value as per user input.
151 * This function returns the amount of data that was written.
153 static ssize_t
fnic_trace_ctrl_write(struct file
*filp
,
154 const char __user
*ubuf
,
155 size_t cnt
, loff_t
*ppos
)
161 trace_type
= (u8
*)filp
->private_data
;
163 if (cnt
>= sizeof(buf
))
166 if (copy_from_user(&buf
, ubuf
, cnt
))
171 ret
= kstrtoul(buf
, 10, &val
);
175 if (*trace_type
== fc_trc_flag
->fnic_trace
)
176 fnic_tracing_enabled
= val
;
177 else if (*trace_type
== fc_trc_flag
->fc_trace
)
178 fnic_fc_tracing_enabled
= val
;
179 else if (*trace_type
== fc_trc_flag
->fc_clear
)
180 fnic_fc_trace_cleared
= val
;
182 pr_err("fnic: cannot write to any debugfs file\n");
189 static const struct file_operations fnic_trace_ctrl_fops
= {
190 .owner
= THIS_MODULE
,
192 .read
= fnic_trace_ctrl_read
,
193 .write
= fnic_trace_ctrl_write
,
197 * fnic_trace_debugfs_open - Open the fnic trace log
198 * @inode: The inode pointer
199 * @file: The file pointer to attach the log output
202 * This routine is the entry point for the debugfs open file operation.
203 * It allocates the necessary buffer for the log, fills the buffer from
204 * the in-memory log and then returns a pointer to that log in
205 * the private_data field in @file.
208 * This function returns zero if successful. On error it will return
209 * a negative error value.
211 static int fnic_trace_debugfs_open(struct inode
*inode
,
214 fnic_dbgfs_t
*fnic_dbg_prt
;
216 rdata_ptr
= (u8
*)inode
->i_private
;
217 fnic_dbg_prt
= kzalloc(sizeof(fnic_dbgfs_t
), GFP_KERNEL
);
221 if (*rdata_ptr
== fc_trc_flag
->fnic_trace
) {
222 fnic_dbg_prt
->buffer
= vmalloc(array3_size(3, trace_max_pages
,
224 if (!fnic_dbg_prt
->buffer
) {
228 memset((void *)fnic_dbg_prt
->buffer
, 0,
229 3 * (trace_max_pages
* PAGE_SIZE
));
230 fnic_dbg_prt
->buffer_len
= fnic_get_trace_data(fnic_dbg_prt
);
232 fnic_dbg_prt
->buffer
=
233 vmalloc(array3_size(3, fnic_fc_trace_max_pages
,
235 if (!fnic_dbg_prt
->buffer
) {
239 memset((void *)fnic_dbg_prt
->buffer
, 0,
240 3 * (fnic_fc_trace_max_pages
* PAGE_SIZE
));
241 fnic_dbg_prt
->buffer_len
=
242 fnic_fc_trace_get_data(fnic_dbg_prt
, *rdata_ptr
);
244 file
->private_data
= fnic_dbg_prt
;
250 * fnic_trace_debugfs_lseek - Seek through a debugfs file
251 * @file: The file pointer to seek through.
252 * @offset: The offset to seek to or the amount to seek by.
253 * @howto: Indicates how to seek.
256 * This routine is the entry point for the debugfs lseek file operation.
257 * The @howto parameter indicates whether @offset is the offset to directly
258 * seek to, or if it is a value to seek forward or reverse by. This function
259 * figures out what the new offset of the debugfs file will be and assigns
260 * that value to the f_pos field of @file.
263 * This function returns the new offset if successful and returns a negative
264 * error if unable to process the seek.
266 static loff_t
fnic_trace_debugfs_lseek(struct file
*file
,
270 fnic_dbgfs_t
*fnic_dbg_prt
= file
->private_data
;
271 return fixed_size_llseek(file
, offset
, howto
,
272 fnic_dbg_prt
->buffer_len
);
276 * fnic_trace_debugfs_read - Read a debugfs file
277 * @file: The file pointer to read from.
278 * @ubuf: The buffer to copy the data to.
279 * @nbytes: The number of bytes to read.
280 * @pos: The position in the file to start reading from.
283 * This routine reads data from the buffer indicated in the private_data
284 * field of @file. It will start reading at @pos and copy up to @nbytes of
288 * This function returns the amount of data that was read (this could be
289 * less than @nbytes if the end of the file was reached).
291 static ssize_t
fnic_trace_debugfs_read(struct file
*file
,
296 fnic_dbgfs_t
*fnic_dbg_prt
= file
->private_data
;
298 rc
= simple_read_from_buffer(ubuf
, nbytes
, pos
,
299 fnic_dbg_prt
->buffer
,
300 fnic_dbg_prt
->buffer_len
);
305 * fnic_trace_debugfs_release - Release the buffer used to store
307 * @inode: The inode pointer
308 * @file: The file pointer that contains the buffer to release
311 * This routine frees the buffer that was allocated when the debugfs
315 * This function returns zero.
317 static int fnic_trace_debugfs_release(struct inode
*inode
,
320 fnic_dbgfs_t
*fnic_dbg_prt
= file
->private_data
;
322 vfree(fnic_dbg_prt
->buffer
);
327 static const struct file_operations fnic_trace_debugfs_fops
= {
328 .owner
= THIS_MODULE
,
329 .open
= fnic_trace_debugfs_open
,
330 .llseek
= fnic_trace_debugfs_lseek
,
331 .read
= fnic_trace_debugfs_read
,
332 .release
= fnic_trace_debugfs_release
,
336 * fnic_trace_debugfs_init - Initialize debugfs for fnic trace logging
339 * When Debugfs is configured this routine sets up the fnic debugfs
340 * file system. If not already created, this routine will create the
341 * create file trace to log fnic trace buffer output into debugfs and
342 * it will also create file trace_enable to control enable/disable of
343 * trace logging into trace buffer.
345 void fnic_trace_debugfs_init(void)
347 fnic_trace_enable
= debugfs_create_file("tracing_enable",
348 S_IFREG
|S_IRUGO
|S_IWUSR
,
349 fnic_trace_debugfs_root
,
350 &(fc_trc_flag
->fnic_trace
),
351 &fnic_trace_ctrl_fops
);
353 fnic_trace_debugfs_file
= debugfs_create_file("trace",
354 S_IFREG
|S_IRUGO
|S_IWUSR
,
355 fnic_trace_debugfs_root
,
356 &(fc_trc_flag
->fnic_trace
),
357 &fnic_trace_debugfs_fops
);
361 * fnic_trace_debugfs_terminate - Tear down debugfs infrastructure
364 * When Debugfs is configured this routine removes debugfs file system
365 * elements that are specific to fnic trace logging.
367 void fnic_trace_debugfs_terminate(void)
369 debugfs_remove(fnic_trace_debugfs_file
);
370 fnic_trace_debugfs_file
= NULL
;
372 debugfs_remove(fnic_trace_enable
);
373 fnic_trace_enable
= NULL
;
377 * fnic_fc_trace_debugfs_init -
378 * Initialize debugfs for fnic control frame trace logging
381 * When Debugfs is configured this routine sets up the fnic_fc debugfs
382 * file system. If not already created, this routine will create the
383 * create file trace to log fnic fc trace buffer output into debugfs and
384 * it will also create file fc_trace_enable to control enable/disable of
385 * trace logging into trace buffer.
388 void fnic_fc_trace_debugfs_init(void)
390 fnic_fc_trace_enable
= debugfs_create_file("fc_trace_enable",
391 S_IFREG
|S_IRUGO
|S_IWUSR
,
392 fnic_trace_debugfs_root
,
393 &(fc_trc_flag
->fc_trace
),
394 &fnic_trace_ctrl_fops
);
396 fnic_fc_trace_clear
= debugfs_create_file("fc_trace_clear",
397 S_IFREG
|S_IRUGO
|S_IWUSR
,
398 fnic_trace_debugfs_root
,
399 &(fc_trc_flag
->fc_clear
),
400 &fnic_trace_ctrl_fops
);
402 fnic_fc_rdata_trace_debugfs_file
=
403 debugfs_create_file("fc_trace_rdata",
404 S_IFREG
|S_IRUGO
|S_IWUSR
,
405 fnic_trace_debugfs_root
,
406 &(fc_trc_flag
->fc_normal_file
),
407 &fnic_trace_debugfs_fops
);
409 fnic_fc_trace_debugfs_file
=
410 debugfs_create_file("fc_trace",
411 S_IFREG
|S_IRUGO
|S_IWUSR
,
412 fnic_trace_debugfs_root
,
413 &(fc_trc_flag
->fc_row_file
),
414 &fnic_trace_debugfs_fops
);
418 * fnic_fc_trace_debugfs_terminate - Tear down debugfs infrastructure
421 * When Debugfs is configured this routine removes debugfs file system
422 * elements that are specific to fnic_fc trace logging.
425 void fnic_fc_trace_debugfs_terminate(void)
427 debugfs_remove(fnic_fc_trace_debugfs_file
);
428 fnic_fc_trace_debugfs_file
= NULL
;
430 debugfs_remove(fnic_fc_rdata_trace_debugfs_file
);
431 fnic_fc_rdata_trace_debugfs_file
= NULL
;
433 debugfs_remove(fnic_fc_trace_enable
);
434 fnic_fc_trace_enable
= NULL
;
436 debugfs_remove(fnic_fc_trace_clear
);
437 fnic_fc_trace_clear
= NULL
;
441 * fnic_reset_stats_open - Open the reset_stats file
442 * @inode: The inode pointer.
443 * @file: The file pointer to attach the stats reset flag.
446 * This routine opens a debugsfs file reset_stats and stores i_private data
447 * to debug structure to retrieve later for while performing other
451 * This function returns zero if successful.
453 static int fnic_reset_stats_open(struct inode
*inode
, struct file
*file
)
455 struct stats_debug_info
*debug
;
457 debug
= kzalloc(sizeof(struct stats_debug_info
), GFP_KERNEL
);
461 debug
->i_private
= inode
->i_private
;
463 file
->private_data
= debug
;
469 * fnic_reset_stats_read - Read a reset_stats debugfs file
470 * @filp: The file pointer to read from.
471 * @ubuf: The buffer to copy the data to.
472 * @cnt: The number of bytes to read.
473 * @ppos: The position in the file to start reading from.
476 * This routine reads value of variable reset_stats
477 * and stores into local @buf. It will start reading file at @ppos and
478 * copy up to @cnt of data to @ubuf from @buf.
481 * This function returns the amount of data that was read.
483 static ssize_t
fnic_reset_stats_read(struct file
*file
,
485 size_t cnt
, loff_t
*ppos
)
487 struct stats_debug_info
*debug
= file
->private_data
;
488 struct fnic
*fnic
= (struct fnic
*)debug
->i_private
;
492 len
= sprintf(buf
, "%u\n", fnic
->reset_stats
);
494 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, len
);
498 * fnic_reset_stats_write - Write to reset_stats debugfs file
499 * @filp: The file pointer to write from.
500 * @ubuf: The buffer to copy the data from.
501 * @cnt: The number of bytes to write.
502 * @ppos: The position in the file to start writing to.
505 * This routine writes data from user buffer @ubuf to buffer @buf and
506 * resets cumulative stats of fnic.
509 * This function returns the amount of data that was written.
511 static ssize_t
fnic_reset_stats_write(struct file
*file
,
512 const char __user
*ubuf
,
513 size_t cnt
, loff_t
*ppos
)
515 struct stats_debug_info
*debug
= file
->private_data
;
516 struct fnic
*fnic
= (struct fnic
*)debug
->i_private
;
517 struct fnic_stats
*stats
= &fnic
->fnic_stats
;
518 u64
*io_stats_p
= (u64
*)&stats
->io_stats
;
519 u64
*fw_stats_p
= (u64
*)&stats
->fw_stats
;
524 if (cnt
>= sizeof(buf
))
527 if (copy_from_user(&buf
, ubuf
, cnt
))
532 ret
= kstrtoul(buf
, 10, &val
);
536 fnic
->reset_stats
= val
;
538 if (fnic
->reset_stats
) {
539 /* Skip variable is used to avoid descrepancies to Num IOs
540 * and IO Completions stats. Skip incrementing No IO Compls
541 * for pending active IOs after reset stats
543 atomic64_set(&fnic
->io_cmpl_skip
,
544 atomic64_read(&stats
->io_stats
.active_ios
));
545 memset(&stats
->abts_stats
, 0, sizeof(struct abort_stats
));
546 memset(&stats
->term_stats
, 0,
547 sizeof(struct terminate_stats
));
548 memset(&stats
->reset_stats
, 0, sizeof(struct reset_stats
));
549 memset(&stats
->misc_stats
, 0, sizeof(struct misc_stats
));
550 memset(&stats
->vlan_stats
, 0, sizeof(struct vlan_stats
));
551 memset(io_stats_p
+1, 0,
552 sizeof(struct io_path_stats
) - sizeof(u64
));
553 memset(fw_stats_p
+1, 0,
554 sizeof(struct fw_stats
) - sizeof(u64
));
555 ktime_get_real_ts64(&stats
->stats_timestamps
.last_reset_time
);
563 * fnic_reset_stats_release - Release the buffer used to store
565 * @inode: The inode pointer
566 * @file: The file pointer that contains the buffer to release
569 * This routine frees the buffer that was allocated when the debugfs
573 * This function returns zero.
575 static int fnic_reset_stats_release(struct inode
*inode
,
578 struct stats_debug_info
*debug
= file
->private_data
;
584 * fnic_stats_debugfs_open - Open the stats file for specific host
585 * and get fnic stats.
586 * @inode: The inode pointer.
587 * @file: The file pointer to attach the specific host statistics.
590 * This routine opens a debugsfs file stats of specific host and print
594 * This function returns zero if successful.
596 static int fnic_stats_debugfs_open(struct inode
*inode
,
599 struct fnic
*fnic
= inode
->i_private
;
600 struct fnic_stats
*fnic_stats
= &fnic
->fnic_stats
;
601 struct stats_debug_info
*debug
;
602 int buf_size
= 2 * PAGE_SIZE
;
604 debug
= kzalloc(sizeof(struct stats_debug_info
), GFP_KERNEL
);
608 debug
->debug_buffer
= vmalloc(buf_size
);
609 if (!debug
->debug_buffer
) {
614 debug
->buf_size
= buf_size
;
615 memset((void *)debug
->debug_buffer
, 0, buf_size
);
616 debug
->buffer_len
= fnic_get_stats_data(debug
, fnic_stats
);
618 file
->private_data
= debug
;
624 * fnic_stats_debugfs_read - Read a debugfs file
625 * @file: The file pointer to read from.
626 * @ubuf: The buffer to copy the data to.
627 * @nbytes: The number of bytes to read.
628 * @pos: The position in the file to start reading from.
631 * This routine reads data from the buffer indicated in the private_data
632 * field of @file. It will start reading at @pos and copy up to @nbytes of
636 * This function returns the amount of data that was read (this could be
637 * less than @nbytes if the end of the file was reached).
639 static ssize_t
fnic_stats_debugfs_read(struct file
*file
,
644 struct stats_debug_info
*debug
= file
->private_data
;
646 rc
= simple_read_from_buffer(ubuf
, nbytes
, pos
,
653 * fnic_stats_stats_release - Release the buffer used to store
655 * @inode: The inode pointer
656 * @file: The file pointer that contains the buffer to release
659 * This routine frees the buffer that was allocated when the debugfs
663 * This function returns zero.
665 static int fnic_stats_debugfs_release(struct inode
*inode
,
668 struct stats_debug_info
*debug
= file
->private_data
;
669 vfree(debug
->debug_buffer
);
674 static const struct file_operations fnic_stats_debugfs_fops
= {
675 .owner
= THIS_MODULE
,
676 .open
= fnic_stats_debugfs_open
,
677 .read
= fnic_stats_debugfs_read
,
678 .release
= fnic_stats_debugfs_release
,
681 static const struct file_operations fnic_reset_debugfs_fops
= {
682 .owner
= THIS_MODULE
,
683 .open
= fnic_reset_stats_open
,
684 .read
= fnic_reset_stats_read
,
685 .write
= fnic_reset_stats_write
,
686 .release
= fnic_reset_stats_release
,
690 * fnic_stats_init - Initialize stats struct and create stats file per fnic
693 * When Debugfs is configured this routine sets up the stats file per fnic
694 * It will create file stats and reset_stats under statistics/host# directory
695 * to log per fnic stats.
697 void fnic_stats_debugfs_init(struct fnic
*fnic
)
701 snprintf(name
, sizeof(name
), "host%d", fnic
->lport
->host
->host_no
);
703 fnic
->fnic_stats_debugfs_host
= debugfs_create_dir(name
,
704 fnic_stats_debugfs_root
);
706 fnic
->fnic_stats_debugfs_file
= debugfs_create_file("stats",
707 S_IFREG
|S_IRUGO
|S_IWUSR
,
708 fnic
->fnic_stats_debugfs_host
,
710 &fnic_stats_debugfs_fops
);
712 fnic
->fnic_reset_debugfs_file
= debugfs_create_file("reset_stats",
713 S_IFREG
|S_IRUGO
|S_IWUSR
,
714 fnic
->fnic_stats_debugfs_host
,
716 &fnic_reset_debugfs_fops
);
720 * fnic_stats_debugfs_remove - Tear down debugfs infrastructure of stats
723 * When Debugfs is configured this routine removes debugfs file system
724 * elements that are specific to fnic stats.
726 void fnic_stats_debugfs_remove(struct fnic
*fnic
)
731 debugfs_remove(fnic
->fnic_stats_debugfs_file
);
732 fnic
->fnic_stats_debugfs_file
= NULL
;
734 debugfs_remove(fnic
->fnic_reset_debugfs_file
);
735 fnic
->fnic_reset_debugfs_file
= NULL
;
737 debugfs_remove(fnic
->fnic_stats_debugfs_host
);
738 fnic
->fnic_stats_debugfs_host
= NULL
;