1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright 2012 Cisco Systems, Inc. All rights reserved.
4 #include <linux/module.h>
5 #include <linux/errno.h>
6 #include <linux/debugfs.h>
7 #include <linux/vmalloc.h>
10 static struct dentry
*fnic_trace_debugfs_root
;
11 static struct dentry
*fnic_trace_debugfs_file
;
12 static struct dentry
*fnic_trace_enable
;
13 static struct dentry
*fnic_stats_debugfs_root
;
15 static struct dentry
*fnic_fc_trace_debugfs_file
;
16 static struct dentry
*fnic_fc_rdata_trace_debugfs_file
;
17 static struct dentry
*fnic_fc_trace_enable
;
18 static struct dentry
*fnic_fc_trace_clear
;
20 struct fc_trace_flag_type
{
28 static struct fc_trace_flag_type
*fc_trc_flag
;
31 * fnic_debugfs_init - Initialize debugfs for fnic debug logging
34 * When Debugfs is configured this routine sets up the fnic debugfs
35 * file system. If not already created, this routine will create the
36 * fnic directory and statistics directory for trace buffer and
39 int fnic_debugfs_init(void)
41 fnic_trace_debugfs_root
= debugfs_create_dir("fnic", NULL
);
43 fnic_stats_debugfs_root
= debugfs_create_dir("statistics",
44 fnic_trace_debugfs_root
);
46 /* Allocate memory to structure */
47 fc_trc_flag
= vmalloc(sizeof(struct fc_trace_flag_type
));
50 fc_trc_flag
->fc_row_file
= 0;
51 fc_trc_flag
->fc_normal_file
= 1;
52 fc_trc_flag
->fnic_trace
= 2;
53 fc_trc_flag
->fc_trace
= 3;
54 fc_trc_flag
->fc_clear
= 4;
62 * fnic_debugfs_terminate - Tear down debugfs infrastructure
65 * When Debugfs is configured this routine removes debugfs file system
66 * elements that are specific to fnic.
68 void fnic_debugfs_terminate(void)
70 debugfs_remove(fnic_stats_debugfs_root
);
71 fnic_stats_debugfs_root
= NULL
;
73 debugfs_remove(fnic_trace_debugfs_root
);
74 fnic_trace_debugfs_root
= NULL
;
80 * fnic_trace_ctrl_read -
81 * Read trace_enable ,fc_trace_enable
82 * or fc_trace_clear debugfs file
83 * @filp: The file pointer to read from.
84 * @ubuf: The buffer to copy the data to.
85 * @cnt: The number of bytes to read.
86 * @ppos: The position in the file to start reading from.
89 * This routine reads value of variable fnic_tracing_enabled or
90 * fnic_fc_tracing_enabled or fnic_fc_trace_cleared
91 * and stores into local @buf.
92 * It will start reading file at @ppos and
93 * copy up to @cnt of data to @ubuf from @buf.
96 * This function returns the amount of data that was read.
98 static ssize_t
fnic_trace_ctrl_read(struct file
*filp
,
100 size_t cnt
, loff_t
*ppos
)
106 trace_type
= (u8
*)filp
->private_data
;
107 if (*trace_type
== fc_trc_flag
->fnic_trace
)
108 len
= sprintf(buf
, "%d\n", fnic_tracing_enabled
);
109 else if (*trace_type
== fc_trc_flag
->fc_trace
)
110 len
= sprintf(buf
, "%d\n", fnic_fc_tracing_enabled
);
111 else if (*trace_type
== fc_trc_flag
->fc_clear
)
112 len
= sprintf(buf
, "%d\n", fnic_fc_trace_cleared
);
114 pr_err("fnic: Cannot read to any debugfs file\n");
116 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, len
);
120 * fnic_trace_ctrl_write -
121 * Write to trace_enable, fc_trace_enable or
122 * fc_trace_clear debugfs file
123 * @filp: The file pointer to write from.
124 * @ubuf: The buffer to copy the data from.
125 * @cnt: The number of bytes to write.
126 * @ppos: The position in the file to start writing to.
129 * This routine writes data from user buffer @ubuf to buffer @buf and
130 * sets fc_trace_enable ,tracing_enable or fnic_fc_trace_cleared
131 * value as per user input.
134 * This function returns the amount of data that was written.
136 static ssize_t
fnic_trace_ctrl_write(struct file
*filp
,
137 const char __user
*ubuf
,
138 size_t cnt
, loff_t
*ppos
)
144 trace_type
= (u8
*)filp
->private_data
;
146 if (cnt
>= sizeof(buf
))
149 if (copy_from_user(&buf
, ubuf
, cnt
))
154 ret
= kstrtoul(buf
, 10, &val
);
158 if (*trace_type
== fc_trc_flag
->fnic_trace
)
159 fnic_tracing_enabled
= val
;
160 else if (*trace_type
== fc_trc_flag
->fc_trace
)
161 fnic_fc_tracing_enabled
= val
;
162 else if (*trace_type
== fc_trc_flag
->fc_clear
)
163 fnic_fc_trace_cleared
= val
;
165 pr_err("fnic: cannot write to any debugfs file\n");
172 static const struct file_operations fnic_trace_ctrl_fops
= {
173 .owner
= THIS_MODULE
,
175 .read
= fnic_trace_ctrl_read
,
176 .write
= fnic_trace_ctrl_write
,
180 * fnic_trace_debugfs_open - Open the fnic trace log
181 * @inode: The inode pointer
182 * @file: The file pointer to attach the log output
185 * This routine is the entry point for the debugfs open file operation.
186 * It allocates the necessary buffer for the log, fills the buffer from
187 * the in-memory log and then returns a pointer to that log in
188 * the private_data field in @file.
191 * This function returns zero if successful. On error it will return
192 * a negative error value.
194 static int fnic_trace_debugfs_open(struct inode
*inode
,
197 fnic_dbgfs_t
*fnic_dbg_prt
;
199 rdata_ptr
= (u8
*)inode
->i_private
;
200 fnic_dbg_prt
= kzalloc(sizeof(fnic_dbgfs_t
), GFP_KERNEL
);
204 if (*rdata_ptr
== fc_trc_flag
->fnic_trace
) {
205 fnic_dbg_prt
->buffer
= vzalloc(array3_size(3, trace_max_pages
,
207 if (!fnic_dbg_prt
->buffer
) {
211 fnic_dbg_prt
->buffer_len
= fnic_get_trace_data(fnic_dbg_prt
);
213 fnic_dbg_prt
->buffer
=
214 vzalloc(array3_size(3, fnic_fc_trace_max_pages
,
216 if (!fnic_dbg_prt
->buffer
) {
220 fnic_dbg_prt
->buffer_len
=
221 fnic_fc_trace_get_data(fnic_dbg_prt
, *rdata_ptr
);
223 file
->private_data
= fnic_dbg_prt
;
229 * fnic_trace_debugfs_lseek - Seek through a debugfs file
230 * @file: The file pointer to seek through.
231 * @offset: The offset to seek to or the amount to seek by.
232 * @howto: Indicates how to seek.
235 * This routine is the entry point for the debugfs lseek file operation.
236 * The @howto parameter indicates whether @offset is the offset to directly
237 * seek to, or if it is a value to seek forward or reverse by. This function
238 * figures out what the new offset of the debugfs file will be and assigns
239 * that value to the f_pos field of @file.
242 * This function returns the new offset if successful and returns a negative
243 * error if unable to process the seek.
245 static loff_t
fnic_trace_debugfs_lseek(struct file
*file
,
249 fnic_dbgfs_t
*fnic_dbg_prt
= file
->private_data
;
250 return fixed_size_llseek(file
, offset
, howto
,
251 fnic_dbg_prt
->buffer_len
);
255 * fnic_trace_debugfs_read - Read a debugfs file
256 * @file: The file pointer to read from.
257 * @ubuf: The buffer to copy the data to.
258 * @nbytes: The number of bytes to read.
259 * @pos: The position in the file to start reading from.
262 * This routine reads data from the buffer indicated in the private_data
263 * field of @file. It will start reading at @pos and copy up to @nbytes of
267 * This function returns the amount of data that was read (this could be
268 * less than @nbytes if the end of the file was reached).
270 static ssize_t
fnic_trace_debugfs_read(struct file
*file
,
275 fnic_dbgfs_t
*fnic_dbg_prt
= file
->private_data
;
277 rc
= simple_read_from_buffer(ubuf
, nbytes
, pos
,
278 fnic_dbg_prt
->buffer
,
279 fnic_dbg_prt
->buffer_len
);
284 * fnic_trace_debugfs_release - Release the buffer used to store
286 * @inode: The inode pointer
287 * @file: The file pointer that contains the buffer to release
290 * This routine frees the buffer that was allocated when the debugfs
294 * This function returns zero.
296 static int fnic_trace_debugfs_release(struct inode
*inode
,
299 fnic_dbgfs_t
*fnic_dbg_prt
= file
->private_data
;
301 vfree(fnic_dbg_prt
->buffer
);
306 static const struct file_operations fnic_trace_debugfs_fops
= {
307 .owner
= THIS_MODULE
,
308 .open
= fnic_trace_debugfs_open
,
309 .llseek
= fnic_trace_debugfs_lseek
,
310 .read
= fnic_trace_debugfs_read
,
311 .release
= fnic_trace_debugfs_release
,
315 * fnic_trace_debugfs_init - Initialize debugfs for fnic trace logging
318 * When Debugfs is configured this routine sets up the fnic debugfs
319 * file system. If not already created, this routine will create the
320 * create file trace to log fnic trace buffer output into debugfs and
321 * it will also create file trace_enable to control enable/disable of
322 * trace logging into trace buffer.
324 void fnic_trace_debugfs_init(void)
326 fnic_trace_enable
= debugfs_create_file("tracing_enable",
327 S_IFREG
|S_IRUGO
|S_IWUSR
,
328 fnic_trace_debugfs_root
,
329 &(fc_trc_flag
->fnic_trace
),
330 &fnic_trace_ctrl_fops
);
332 fnic_trace_debugfs_file
= debugfs_create_file("trace",
333 S_IFREG
|S_IRUGO
|S_IWUSR
,
334 fnic_trace_debugfs_root
,
335 &(fc_trc_flag
->fnic_trace
),
336 &fnic_trace_debugfs_fops
);
340 * fnic_trace_debugfs_terminate - Tear down debugfs infrastructure
343 * When Debugfs is configured this routine removes debugfs file system
344 * elements that are specific to fnic trace logging.
346 void fnic_trace_debugfs_terminate(void)
348 debugfs_remove(fnic_trace_debugfs_file
);
349 fnic_trace_debugfs_file
= NULL
;
351 debugfs_remove(fnic_trace_enable
);
352 fnic_trace_enable
= NULL
;
356 * fnic_fc_trace_debugfs_init -
357 * Initialize debugfs for fnic control frame trace logging
360 * When Debugfs is configured this routine sets up the fnic_fc debugfs
361 * file system. If not already created, this routine will create the
362 * create file trace to log fnic fc trace buffer output into debugfs and
363 * it will also create file fc_trace_enable to control enable/disable of
364 * trace logging into trace buffer.
367 void fnic_fc_trace_debugfs_init(void)
369 fnic_fc_trace_enable
= debugfs_create_file("fc_trace_enable",
370 S_IFREG
|S_IRUGO
|S_IWUSR
,
371 fnic_trace_debugfs_root
,
372 &(fc_trc_flag
->fc_trace
),
373 &fnic_trace_ctrl_fops
);
375 fnic_fc_trace_clear
= debugfs_create_file("fc_trace_clear",
376 S_IFREG
|S_IRUGO
|S_IWUSR
,
377 fnic_trace_debugfs_root
,
378 &(fc_trc_flag
->fc_clear
),
379 &fnic_trace_ctrl_fops
);
381 fnic_fc_rdata_trace_debugfs_file
=
382 debugfs_create_file("fc_trace_rdata",
383 S_IFREG
|S_IRUGO
|S_IWUSR
,
384 fnic_trace_debugfs_root
,
385 &(fc_trc_flag
->fc_normal_file
),
386 &fnic_trace_debugfs_fops
);
388 fnic_fc_trace_debugfs_file
=
389 debugfs_create_file("fc_trace",
390 S_IFREG
|S_IRUGO
|S_IWUSR
,
391 fnic_trace_debugfs_root
,
392 &(fc_trc_flag
->fc_row_file
),
393 &fnic_trace_debugfs_fops
);
397 * fnic_fc_trace_debugfs_terminate - Tear down debugfs infrastructure
400 * When Debugfs is configured this routine removes debugfs file system
401 * elements that are specific to fnic_fc trace logging.
404 void fnic_fc_trace_debugfs_terminate(void)
406 debugfs_remove(fnic_fc_trace_debugfs_file
);
407 fnic_fc_trace_debugfs_file
= NULL
;
409 debugfs_remove(fnic_fc_rdata_trace_debugfs_file
);
410 fnic_fc_rdata_trace_debugfs_file
= NULL
;
412 debugfs_remove(fnic_fc_trace_enable
);
413 fnic_fc_trace_enable
= NULL
;
415 debugfs_remove(fnic_fc_trace_clear
);
416 fnic_fc_trace_clear
= NULL
;
420 * fnic_reset_stats_open - Open the reset_stats file
421 * @inode: The inode pointer.
422 * @file: The file pointer to attach the stats reset flag.
425 * This routine opens a debugsfs file reset_stats and stores i_private data
426 * to debug structure to retrieve later for while performing other
430 * This function returns zero if successful.
432 static int fnic_reset_stats_open(struct inode
*inode
, struct file
*file
)
434 struct stats_debug_info
*debug
;
436 debug
= kzalloc(sizeof(struct stats_debug_info
), GFP_KERNEL
);
440 debug
->i_private
= inode
->i_private
;
442 file
->private_data
= debug
;
448 * fnic_reset_stats_read - Read a reset_stats debugfs file
449 * @filp: The file pointer to read from.
450 * @ubuf: The buffer to copy the data to.
451 * @cnt: The number of bytes to read.
452 * @ppos: The position in the file to start reading from.
455 * This routine reads value of variable reset_stats
456 * and stores into local @buf. It will start reading file at @ppos and
457 * copy up to @cnt of data to @ubuf from @buf.
460 * This function returns the amount of data that was read.
462 static ssize_t
fnic_reset_stats_read(struct file
*file
,
464 size_t cnt
, loff_t
*ppos
)
466 struct stats_debug_info
*debug
= file
->private_data
;
467 struct fnic
*fnic
= (struct fnic
*)debug
->i_private
;
471 len
= sprintf(buf
, "%u\n", fnic
->reset_stats
);
473 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, len
);
477 * fnic_reset_stats_write - Write to reset_stats debugfs file
478 * @filp: The file pointer to write from.
479 * @ubuf: The buffer to copy the data from.
480 * @cnt: The number of bytes to write.
481 * @ppos: The position in the file to start writing to.
484 * This routine writes data from user buffer @ubuf to buffer @buf and
485 * resets cumulative stats of fnic.
488 * This function returns the amount of data that was written.
490 static ssize_t
fnic_reset_stats_write(struct file
*file
,
491 const char __user
*ubuf
,
492 size_t cnt
, loff_t
*ppos
)
494 struct stats_debug_info
*debug
= file
->private_data
;
495 struct fnic
*fnic
= (struct fnic
*)debug
->i_private
;
496 struct fnic_stats
*stats
= &fnic
->fnic_stats
;
497 u64
*io_stats_p
= (u64
*)&stats
->io_stats
;
498 u64
*fw_stats_p
= (u64
*)&stats
->fw_stats
;
503 if (cnt
>= sizeof(buf
))
506 if (copy_from_user(&buf
, ubuf
, cnt
))
511 ret
= kstrtoul(buf
, 10, &val
);
515 fnic
->reset_stats
= val
;
517 if (fnic
->reset_stats
) {
518 /* Skip variable is used to avoid descrepancies to Num IOs
519 * and IO Completions stats. Skip incrementing No IO Compls
520 * for pending active IOs after reset stats
522 atomic64_set(&fnic
->io_cmpl_skip
,
523 atomic64_read(&stats
->io_stats
.active_ios
));
524 memset(&stats
->abts_stats
, 0, sizeof(struct abort_stats
));
525 memset(&stats
->term_stats
, 0,
526 sizeof(struct terminate_stats
));
527 memset(&stats
->reset_stats
, 0, sizeof(struct reset_stats
));
528 memset(&stats
->misc_stats
, 0, sizeof(struct misc_stats
));
529 memset(&stats
->vlan_stats
, 0, sizeof(struct vlan_stats
));
530 memset(io_stats_p
+1, 0,
531 sizeof(struct io_path_stats
) - sizeof(u64
));
532 memset(fw_stats_p
+1, 0,
533 sizeof(struct fw_stats
) - sizeof(u64
));
534 ktime_get_real_ts64(&stats
->stats_timestamps
.last_reset_time
);
542 * fnic_reset_stats_release - Release the buffer used to store
544 * @inode: The inode pointer
545 * @file: The file pointer that contains the buffer to release
548 * This routine frees the buffer that was allocated when the debugfs
552 * This function returns zero.
554 static int fnic_reset_stats_release(struct inode
*inode
,
557 struct stats_debug_info
*debug
= file
->private_data
;
563 * fnic_stats_debugfs_open - Open the stats file for specific host
564 * and get fnic stats.
565 * @inode: The inode pointer.
566 * @file: The file pointer to attach the specific host statistics.
569 * This routine opens a debugsfs file stats of specific host and print
573 * This function returns zero if successful.
575 static int fnic_stats_debugfs_open(struct inode
*inode
,
578 struct fnic
*fnic
= inode
->i_private
;
579 struct fnic_stats
*fnic_stats
= &fnic
->fnic_stats
;
580 struct stats_debug_info
*debug
;
581 int buf_size
= 2 * PAGE_SIZE
;
583 debug
= kzalloc(sizeof(struct stats_debug_info
), GFP_KERNEL
);
587 debug
->debug_buffer
= vmalloc(buf_size
);
588 if (!debug
->debug_buffer
) {
593 debug
->buf_size
= buf_size
;
594 memset((void *)debug
->debug_buffer
, 0, buf_size
);
595 debug
->buffer_len
= fnic_get_stats_data(debug
, fnic_stats
);
597 file
->private_data
= debug
;
603 * fnic_stats_debugfs_read - Read a debugfs file
604 * @file: The file pointer to read from.
605 * @ubuf: The buffer to copy the data to.
606 * @nbytes: The number of bytes to read.
607 * @pos: The position in the file to start reading from.
610 * This routine reads data from the buffer indicated in the private_data
611 * field of @file. It will start reading at @pos and copy up to @nbytes of
615 * This function returns the amount of data that was read (this could be
616 * less than @nbytes if the end of the file was reached).
618 static ssize_t
fnic_stats_debugfs_read(struct file
*file
,
623 struct stats_debug_info
*debug
= file
->private_data
;
625 rc
= simple_read_from_buffer(ubuf
, nbytes
, pos
,
632 * fnic_stats_stats_release - Release the buffer used to store
634 * @inode: The inode pointer
635 * @file: The file pointer that contains the buffer to release
638 * This routine frees the buffer that was allocated when the debugfs
642 * This function returns zero.
644 static int fnic_stats_debugfs_release(struct inode
*inode
,
647 struct stats_debug_info
*debug
= file
->private_data
;
648 vfree(debug
->debug_buffer
);
653 static const struct file_operations fnic_stats_debugfs_fops
= {
654 .owner
= THIS_MODULE
,
655 .open
= fnic_stats_debugfs_open
,
656 .read
= fnic_stats_debugfs_read
,
657 .release
= fnic_stats_debugfs_release
,
660 static const struct file_operations fnic_reset_debugfs_fops
= {
661 .owner
= THIS_MODULE
,
662 .open
= fnic_reset_stats_open
,
663 .read
= fnic_reset_stats_read
,
664 .write
= fnic_reset_stats_write
,
665 .release
= fnic_reset_stats_release
,
669 * fnic_stats_init - Initialize stats struct and create stats file per fnic
672 * When Debugfs is configured this routine sets up the stats file per fnic
673 * It will create file stats and reset_stats under statistics/host# directory
674 * to log per fnic stats.
676 void fnic_stats_debugfs_init(struct fnic
*fnic
)
680 snprintf(name
, sizeof(name
), "host%d", fnic
->lport
->host
->host_no
);
682 fnic
->fnic_stats_debugfs_host
= debugfs_create_dir(name
,
683 fnic_stats_debugfs_root
);
685 fnic
->fnic_stats_debugfs_file
= debugfs_create_file("stats",
686 S_IFREG
|S_IRUGO
|S_IWUSR
,
687 fnic
->fnic_stats_debugfs_host
,
689 &fnic_stats_debugfs_fops
);
691 fnic
->fnic_reset_debugfs_file
= debugfs_create_file("reset_stats",
692 S_IFREG
|S_IRUGO
|S_IWUSR
,
693 fnic
->fnic_stats_debugfs_host
,
695 &fnic_reset_debugfs_fops
);
699 * fnic_stats_debugfs_remove - Tear down debugfs infrastructure of stats
702 * When Debugfs is configured this routine removes debugfs file system
703 * elements that are specific to fnic stats.
705 void fnic_stats_debugfs_remove(struct fnic
*fnic
)
710 debugfs_remove(fnic
->fnic_stats_debugfs_file
);
711 fnic
->fnic_stats_debugfs_file
= NULL
;
713 debugfs_remove(fnic
->fnic_reset_debugfs_file
);
714 fnic
->fnic_reset_debugfs_file
= NULL
;
716 debugfs_remove(fnic
->fnic_stats_debugfs_host
);
717 fnic
->fnic_stats_debugfs_host
= NULL
;