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;
76 * fnic_debugfs_terminate - Tear down debugfs infrastructure
79 * When Debugfs is configured this routine removes debugfs file system
80 * elements that are specific to fnic.
82 void fnic_debugfs_terminate(void)
84 debugfs_remove(fnic_stats_debugfs_root
);
85 fnic_stats_debugfs_root
= NULL
;
87 debugfs_remove(fnic_trace_debugfs_root
);
88 fnic_trace_debugfs_root
= NULL
;
95 * fnic_trace_ctrl_read -
96 * Read trace_enable ,fc_trace_enable
97 * or fc_trace_clear debugfs file
98 * @filp: The file pointer to read from.
99 * @ubuf: The buffer to copy the data to.
100 * @cnt: The number of bytes to read.
101 * @ppos: The position in the file to start reading from.
104 * This routine reads value of variable fnic_tracing_enabled or
105 * fnic_fc_tracing_enabled or fnic_fc_trace_cleared
106 * and stores into local @buf.
107 * It will start reading file at @ppos and
108 * copy up to @cnt of data to @ubuf from @buf.
111 * This function returns the amount of data that was read.
113 static ssize_t
fnic_trace_ctrl_read(struct file
*filp
,
115 size_t cnt
, loff_t
*ppos
)
121 trace_type
= (u8
*)filp
->private_data
;
122 if (*trace_type
== fc_trc_flag
->fnic_trace
)
123 len
= sprintf(buf
, "%d\n", fnic_tracing_enabled
);
124 else if (*trace_type
== fc_trc_flag
->fc_trace
)
125 len
= sprintf(buf
, "%d\n", fnic_fc_tracing_enabled
);
126 else if (*trace_type
== fc_trc_flag
->fc_clear
)
127 len
= sprintf(buf
, "%d\n", fnic_fc_trace_cleared
);
129 pr_err("fnic: Cannot read to any debugfs file\n");
131 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, len
);
135 * fnic_trace_ctrl_write -
136 * Write to trace_enable, fc_trace_enable or
137 * fc_trace_clear debugfs file
138 * @filp: The file pointer to write from.
139 * @ubuf: The buffer to copy the data from.
140 * @cnt: The number of bytes to write.
141 * @ppos: The position in the file to start writing to.
144 * This routine writes data from user buffer @ubuf to buffer @buf and
145 * sets fc_trace_enable ,tracing_enable or fnic_fc_trace_cleared
146 * value as per user input.
149 * This function returns the amount of data that was written.
151 static ssize_t
fnic_trace_ctrl_write(struct file
*filp
,
152 const char __user
*ubuf
,
153 size_t cnt
, loff_t
*ppos
)
159 trace_type
= (u8
*)filp
->private_data
;
161 if (cnt
>= sizeof(buf
))
164 if (copy_from_user(&buf
, ubuf
, cnt
))
169 ret
= kstrtoul(buf
, 10, &val
);
173 if (*trace_type
== fc_trc_flag
->fnic_trace
)
174 fnic_tracing_enabled
= val
;
175 else if (*trace_type
== fc_trc_flag
->fc_trace
)
176 fnic_fc_tracing_enabled
= val
;
177 else if (*trace_type
== fc_trc_flag
->fc_clear
)
178 fnic_fc_trace_cleared
= val
;
180 pr_err("fnic: cannot write to any debugfs file\n");
187 static const struct file_operations fnic_trace_ctrl_fops
= {
188 .owner
= THIS_MODULE
,
190 .read
= fnic_trace_ctrl_read
,
191 .write
= fnic_trace_ctrl_write
,
195 * fnic_trace_debugfs_open - Open the fnic trace log
196 * @inode: The inode pointer
197 * @file: The file pointer to attach the log output
200 * This routine is the entry point for the debugfs open file operation.
201 * It allocates the necessary buffer for the log, fills the buffer from
202 * the in-memory log and then returns a pointer to that log in
203 * the private_data field in @file.
206 * This function returns zero if successful. On error it will return
207 * a negative error value.
209 static int fnic_trace_debugfs_open(struct inode
*inode
,
212 fnic_dbgfs_t
*fnic_dbg_prt
;
214 rdata_ptr
= (u8
*)inode
->i_private
;
215 fnic_dbg_prt
= kzalloc(sizeof(fnic_dbgfs_t
), GFP_KERNEL
);
219 if (*rdata_ptr
== fc_trc_flag
->fnic_trace
) {
220 fnic_dbg_prt
->buffer
= vmalloc(array3_size(3, trace_max_pages
,
222 if (!fnic_dbg_prt
->buffer
) {
226 memset((void *)fnic_dbg_prt
->buffer
, 0,
227 3 * (trace_max_pages
* PAGE_SIZE
));
228 fnic_dbg_prt
->buffer_len
= fnic_get_trace_data(fnic_dbg_prt
);
230 fnic_dbg_prt
->buffer
=
231 vmalloc(array3_size(3, fnic_fc_trace_max_pages
,
233 if (!fnic_dbg_prt
->buffer
) {
237 memset((void *)fnic_dbg_prt
->buffer
, 0,
238 3 * (fnic_fc_trace_max_pages
* PAGE_SIZE
));
239 fnic_dbg_prt
->buffer_len
=
240 fnic_fc_trace_get_data(fnic_dbg_prt
, *rdata_ptr
);
242 file
->private_data
= fnic_dbg_prt
;
248 * fnic_trace_debugfs_lseek - Seek through a debugfs file
249 * @file: The file pointer to seek through.
250 * @offset: The offset to seek to or the amount to seek by.
251 * @howto: Indicates how to seek.
254 * This routine is the entry point for the debugfs lseek file operation.
255 * The @howto parameter indicates whether @offset is the offset to directly
256 * seek to, or if it is a value to seek forward or reverse by. This function
257 * figures out what the new offset of the debugfs file will be and assigns
258 * that value to the f_pos field of @file.
261 * This function returns the new offset if successful and returns a negative
262 * error if unable to process the seek.
264 static loff_t
fnic_trace_debugfs_lseek(struct file
*file
,
268 fnic_dbgfs_t
*fnic_dbg_prt
= file
->private_data
;
269 return fixed_size_llseek(file
, offset
, howto
,
270 fnic_dbg_prt
->buffer_len
);
274 * fnic_trace_debugfs_read - Read a debugfs file
275 * @file: The file pointer to read from.
276 * @ubuf: The buffer to copy the data to.
277 * @nbytes: The number of bytes to read.
278 * @pos: The position in the file to start reading from.
281 * This routine reads data from the buffer indicated in the private_data
282 * field of @file. It will start reading at @pos and copy up to @nbytes of
286 * This function returns the amount of data that was read (this could be
287 * less than @nbytes if the end of the file was reached).
289 static ssize_t
fnic_trace_debugfs_read(struct file
*file
,
294 fnic_dbgfs_t
*fnic_dbg_prt
= file
->private_data
;
296 rc
= simple_read_from_buffer(ubuf
, nbytes
, pos
,
297 fnic_dbg_prt
->buffer
,
298 fnic_dbg_prt
->buffer_len
);
303 * fnic_trace_debugfs_release - Release the buffer used to store
305 * @inode: The inode pointer
306 * @file: The file pointer that contains the buffer to release
309 * This routine frees the buffer that was allocated when the debugfs
313 * This function returns zero.
315 static int fnic_trace_debugfs_release(struct inode
*inode
,
318 fnic_dbgfs_t
*fnic_dbg_prt
= file
->private_data
;
320 vfree(fnic_dbg_prt
->buffer
);
325 static const struct file_operations fnic_trace_debugfs_fops
= {
326 .owner
= THIS_MODULE
,
327 .open
= fnic_trace_debugfs_open
,
328 .llseek
= fnic_trace_debugfs_lseek
,
329 .read
= fnic_trace_debugfs_read
,
330 .release
= fnic_trace_debugfs_release
,
334 * fnic_trace_debugfs_init - Initialize debugfs for fnic trace logging
337 * When Debugfs is configured this routine sets up the fnic debugfs
338 * file system. If not already created, this routine will create the
339 * create file trace to log fnic trace buffer output into debugfs and
340 * it will also create file trace_enable to control enable/disable of
341 * trace logging into trace buffer.
343 void fnic_trace_debugfs_init(void)
345 fnic_trace_enable
= debugfs_create_file("tracing_enable",
346 S_IFREG
|S_IRUGO
|S_IWUSR
,
347 fnic_trace_debugfs_root
,
348 &(fc_trc_flag
->fnic_trace
),
349 &fnic_trace_ctrl_fops
);
351 fnic_trace_debugfs_file
= debugfs_create_file("trace",
352 S_IFREG
|S_IRUGO
|S_IWUSR
,
353 fnic_trace_debugfs_root
,
354 &(fc_trc_flag
->fnic_trace
),
355 &fnic_trace_debugfs_fops
);
359 * fnic_trace_debugfs_terminate - Tear down debugfs infrastructure
362 * When Debugfs is configured this routine removes debugfs file system
363 * elements that are specific to fnic trace logging.
365 void fnic_trace_debugfs_terminate(void)
367 debugfs_remove(fnic_trace_debugfs_file
);
368 fnic_trace_debugfs_file
= NULL
;
370 debugfs_remove(fnic_trace_enable
);
371 fnic_trace_enable
= NULL
;
375 * fnic_fc_trace_debugfs_init -
376 * Initialize debugfs for fnic control frame trace logging
379 * When Debugfs is configured this routine sets up the fnic_fc debugfs
380 * file system. If not already created, this routine will create the
381 * create file trace to log fnic fc trace buffer output into debugfs and
382 * it will also create file fc_trace_enable to control enable/disable of
383 * trace logging into trace buffer.
386 void fnic_fc_trace_debugfs_init(void)
388 fnic_fc_trace_enable
= debugfs_create_file("fc_trace_enable",
389 S_IFREG
|S_IRUGO
|S_IWUSR
,
390 fnic_trace_debugfs_root
,
391 &(fc_trc_flag
->fc_trace
),
392 &fnic_trace_ctrl_fops
);
394 fnic_fc_trace_clear
= debugfs_create_file("fc_trace_clear",
395 S_IFREG
|S_IRUGO
|S_IWUSR
,
396 fnic_trace_debugfs_root
,
397 &(fc_trc_flag
->fc_clear
),
398 &fnic_trace_ctrl_fops
);
400 fnic_fc_rdata_trace_debugfs_file
=
401 debugfs_create_file("fc_trace_rdata",
402 S_IFREG
|S_IRUGO
|S_IWUSR
,
403 fnic_trace_debugfs_root
,
404 &(fc_trc_flag
->fc_normal_file
),
405 &fnic_trace_debugfs_fops
);
407 fnic_fc_trace_debugfs_file
=
408 debugfs_create_file("fc_trace",
409 S_IFREG
|S_IRUGO
|S_IWUSR
,
410 fnic_trace_debugfs_root
,
411 &(fc_trc_flag
->fc_row_file
),
412 &fnic_trace_debugfs_fops
);
416 * fnic_fc_trace_debugfs_terminate - Tear down debugfs infrastructure
419 * When Debugfs is configured this routine removes debugfs file system
420 * elements that are specific to fnic_fc trace logging.
423 void fnic_fc_trace_debugfs_terminate(void)
425 debugfs_remove(fnic_fc_trace_debugfs_file
);
426 fnic_fc_trace_debugfs_file
= NULL
;
428 debugfs_remove(fnic_fc_rdata_trace_debugfs_file
);
429 fnic_fc_rdata_trace_debugfs_file
= NULL
;
431 debugfs_remove(fnic_fc_trace_enable
);
432 fnic_fc_trace_enable
= NULL
;
434 debugfs_remove(fnic_fc_trace_clear
);
435 fnic_fc_trace_clear
= NULL
;
439 * fnic_reset_stats_open - Open the reset_stats file
440 * @inode: The inode pointer.
441 * @file: The file pointer to attach the stats reset flag.
444 * This routine opens a debugsfs file reset_stats and stores i_private data
445 * to debug structure to retrieve later for while performing other
449 * This function returns zero if successful.
451 static int fnic_reset_stats_open(struct inode
*inode
, struct file
*file
)
453 struct stats_debug_info
*debug
;
455 debug
= kzalloc(sizeof(struct stats_debug_info
), GFP_KERNEL
);
459 debug
->i_private
= inode
->i_private
;
461 file
->private_data
= debug
;
467 * fnic_reset_stats_read - Read a reset_stats debugfs file
468 * @filp: The file pointer to read from.
469 * @ubuf: The buffer to copy the data to.
470 * @cnt: The number of bytes to read.
471 * @ppos: The position in the file to start reading from.
474 * This routine reads value of variable reset_stats
475 * and stores into local @buf. It will start reading file at @ppos and
476 * copy up to @cnt of data to @ubuf from @buf.
479 * This function returns the amount of data that was read.
481 static ssize_t
fnic_reset_stats_read(struct file
*file
,
483 size_t cnt
, loff_t
*ppos
)
485 struct stats_debug_info
*debug
= file
->private_data
;
486 struct fnic
*fnic
= (struct fnic
*)debug
->i_private
;
490 len
= sprintf(buf
, "%u\n", fnic
->reset_stats
);
492 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, len
);
496 * fnic_reset_stats_write - Write to reset_stats debugfs file
497 * @filp: The file pointer to write from.
498 * @ubuf: The buffer to copy the data from.
499 * @cnt: The number of bytes to write.
500 * @ppos: The position in the file to start writing to.
503 * This routine writes data from user buffer @ubuf to buffer @buf and
504 * resets cumulative stats of fnic.
507 * This function returns the amount of data that was written.
509 static ssize_t
fnic_reset_stats_write(struct file
*file
,
510 const char __user
*ubuf
,
511 size_t cnt
, loff_t
*ppos
)
513 struct stats_debug_info
*debug
= file
->private_data
;
514 struct fnic
*fnic
= (struct fnic
*)debug
->i_private
;
515 struct fnic_stats
*stats
= &fnic
->fnic_stats
;
516 u64
*io_stats_p
= (u64
*)&stats
->io_stats
;
517 u64
*fw_stats_p
= (u64
*)&stats
->fw_stats
;
522 if (cnt
>= sizeof(buf
))
525 if (copy_from_user(&buf
, ubuf
, cnt
))
530 ret
= kstrtoul(buf
, 10, &val
);
534 fnic
->reset_stats
= val
;
536 if (fnic
->reset_stats
) {
537 /* Skip variable is used to avoid descrepancies to Num IOs
538 * and IO Completions stats. Skip incrementing No IO Compls
539 * for pending active IOs after reset stats
541 atomic64_set(&fnic
->io_cmpl_skip
,
542 atomic64_read(&stats
->io_stats
.active_ios
));
543 memset(&stats
->abts_stats
, 0, sizeof(struct abort_stats
));
544 memset(&stats
->term_stats
, 0,
545 sizeof(struct terminate_stats
));
546 memset(&stats
->reset_stats
, 0, sizeof(struct reset_stats
));
547 memset(&stats
->misc_stats
, 0, sizeof(struct misc_stats
));
548 memset(&stats
->vlan_stats
, 0, sizeof(struct vlan_stats
));
549 memset(io_stats_p
+1, 0,
550 sizeof(struct io_path_stats
) - sizeof(u64
));
551 memset(fw_stats_p
+1, 0,
552 sizeof(struct fw_stats
) - sizeof(u64
));
553 ktime_get_real_ts64(&stats
->stats_timestamps
.last_reset_time
);
561 * fnic_reset_stats_release - Release the buffer used to store
563 * @inode: The inode pointer
564 * @file: The file pointer that contains the buffer to release
567 * This routine frees the buffer that was allocated when the debugfs
571 * This function returns zero.
573 static int fnic_reset_stats_release(struct inode
*inode
,
576 struct stats_debug_info
*debug
= file
->private_data
;
582 * fnic_stats_debugfs_open - Open the stats file for specific host
583 * and get fnic stats.
584 * @inode: The inode pointer.
585 * @file: The file pointer to attach the specific host statistics.
588 * This routine opens a debugsfs file stats of specific host and print
592 * This function returns zero if successful.
594 static int fnic_stats_debugfs_open(struct inode
*inode
,
597 struct fnic
*fnic
= inode
->i_private
;
598 struct fnic_stats
*fnic_stats
= &fnic
->fnic_stats
;
599 struct stats_debug_info
*debug
;
600 int buf_size
= 2 * PAGE_SIZE
;
602 debug
= kzalloc(sizeof(struct stats_debug_info
), GFP_KERNEL
);
606 debug
->debug_buffer
= vmalloc(buf_size
);
607 if (!debug
->debug_buffer
) {
612 debug
->buf_size
= buf_size
;
613 memset((void *)debug
->debug_buffer
, 0, buf_size
);
614 debug
->buffer_len
= fnic_get_stats_data(debug
, fnic_stats
);
616 file
->private_data
= debug
;
622 * fnic_stats_debugfs_read - Read a debugfs file
623 * @file: The file pointer to read from.
624 * @ubuf: The buffer to copy the data to.
625 * @nbytes: The number of bytes to read.
626 * @pos: The position in the file to start reading from.
629 * This routine reads data from the buffer indicated in the private_data
630 * field of @file. It will start reading at @pos and copy up to @nbytes of
634 * This function returns the amount of data that was read (this could be
635 * less than @nbytes if the end of the file was reached).
637 static ssize_t
fnic_stats_debugfs_read(struct file
*file
,
642 struct stats_debug_info
*debug
= file
->private_data
;
644 rc
= simple_read_from_buffer(ubuf
, nbytes
, pos
,
651 * fnic_stats_stats_release - Release the buffer used to store
653 * @inode: The inode pointer
654 * @file: The file pointer that contains the buffer to release
657 * This routine frees the buffer that was allocated when the debugfs
661 * This function returns zero.
663 static int fnic_stats_debugfs_release(struct inode
*inode
,
666 struct stats_debug_info
*debug
= file
->private_data
;
667 vfree(debug
->debug_buffer
);
672 static const struct file_operations fnic_stats_debugfs_fops
= {
673 .owner
= THIS_MODULE
,
674 .open
= fnic_stats_debugfs_open
,
675 .read
= fnic_stats_debugfs_read
,
676 .release
= fnic_stats_debugfs_release
,
679 static const struct file_operations fnic_reset_debugfs_fops
= {
680 .owner
= THIS_MODULE
,
681 .open
= fnic_reset_stats_open
,
682 .read
= fnic_reset_stats_read
,
683 .write
= fnic_reset_stats_write
,
684 .release
= fnic_reset_stats_release
,
688 * fnic_stats_init - Initialize stats struct and create stats file per fnic
691 * When Debugfs is configured this routine sets up the stats file per fnic
692 * It will create file stats and reset_stats under statistics/host# directory
693 * to log per fnic stats.
695 void fnic_stats_debugfs_init(struct fnic
*fnic
)
699 snprintf(name
, sizeof(name
), "host%d", fnic
->lport
->host
->host_no
);
701 fnic
->fnic_stats_debugfs_host
= debugfs_create_dir(name
,
702 fnic_stats_debugfs_root
);
704 fnic
->fnic_stats_debugfs_file
= debugfs_create_file("stats",
705 S_IFREG
|S_IRUGO
|S_IWUSR
,
706 fnic
->fnic_stats_debugfs_host
,
708 &fnic_stats_debugfs_fops
);
710 fnic
->fnic_reset_debugfs_file
= debugfs_create_file("reset_stats",
711 S_IFREG
|S_IRUGO
|S_IWUSR
,
712 fnic
->fnic_stats_debugfs_host
,
714 &fnic_reset_debugfs_fops
);
718 * fnic_stats_debugfs_remove - Tear down debugfs infrastructure of stats
721 * When Debugfs is configured this routine removes debugfs file system
722 * elements that are specific to fnic stats.
724 void fnic_stats_debugfs_remove(struct fnic
*fnic
)
729 debugfs_remove(fnic
->fnic_stats_debugfs_file
);
730 fnic
->fnic_stats_debugfs_file
= NULL
;
732 debugfs_remove(fnic
->fnic_reset_debugfs_file
);
733 fnic
->fnic_reset_debugfs_file
= NULL
;
735 debugfs_remove(fnic
->fnic_stats_debugfs_host
);
736 fnic
->fnic_stats_debugfs_host
= NULL
;