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>
23 static struct dentry
*fnic_trace_debugfs_root
;
24 static struct dentry
*fnic_trace_debugfs_file
;
25 static struct dentry
*fnic_trace_enable
;
26 static struct dentry
*fnic_stats_debugfs_root
;
29 * fnic_debugfs_init - Initialize debugfs for fnic debug logging
32 * When Debugfs is configured this routine sets up the fnic debugfs
33 * file system. If not already created, this routine will create the
34 * fnic directory and statistics directory for trace buffer and
37 int fnic_debugfs_init(void)
40 fnic_trace_debugfs_root
= debugfs_create_dir("fnic", NULL
);
41 if (!fnic_trace_debugfs_root
) {
42 printk(KERN_DEBUG
"Cannot create debugfs root\n");
46 if (!fnic_trace_debugfs_root
) {
48 "fnic root directory doesn't exist in debugfs\n");
52 fnic_stats_debugfs_root
= debugfs_create_dir("statistics",
53 fnic_trace_debugfs_root
);
54 if (!fnic_stats_debugfs_root
) {
55 printk(KERN_DEBUG
"Cannot create Statistics directory\n");
64 * fnic_debugfs_terminate - Tear down debugfs infrastructure
67 * When Debugfs is configured this routine removes debugfs file system
68 * elements that are specific to fnic.
70 void fnic_debugfs_terminate(void)
72 debugfs_remove(fnic_stats_debugfs_root
);
73 fnic_stats_debugfs_root
= NULL
;
75 debugfs_remove(fnic_trace_debugfs_root
);
76 fnic_trace_debugfs_root
= NULL
;
80 * fnic_trace_ctrl_open - Open the trace_enable file
81 * @inode: The inode pointer.
82 * @file: The file pointer to attach the trace enable/disable flag.
85 * This routine opens a debugsfs file trace_enable.
88 * This function returns zero if successful.
90 static int fnic_trace_ctrl_open(struct inode
*inode
, struct file
*filp
)
92 filp
->private_data
= inode
->i_private
;
97 * fnic_trace_ctrl_read - Read a trace_enable 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
105 * and stores into local @buf. It will start reading file at @ppos and
106 * copy up to @cnt of data to @ubuf from @buf.
109 * This function returns the amount of data that was read.
111 static ssize_t
fnic_trace_ctrl_read(struct file
*filp
,
113 size_t cnt
, loff_t
*ppos
)
117 len
= sprintf(buf
, "%u\n", fnic_tracing_enabled
);
119 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, len
);
123 * fnic_trace_ctrl_write - Write to trace_enable debugfs file
124 * @filp: The file pointer to write from.
125 * @ubuf: The buffer to copy the data from.
126 * @cnt: The number of bytes to write.
127 * @ppos: The position in the file to start writing to.
130 * This routine writes data from user buffer @ubuf to buffer @buf and
131 * sets fnic_tracing_enabled 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 if (cnt
>= sizeof(buf
))
147 if (copy_from_user(&buf
, ubuf
, cnt
))
152 ret
= kstrtoul(buf
, 10, &val
);
156 fnic_tracing_enabled
= val
;
163 * fnic_trace_debugfs_open - Open the fnic trace log
164 * @inode: The inode pointer
165 * @file: The file pointer to attach the log output
168 * This routine is the entry point for the debugfs open file operation.
169 * It allocates the necessary buffer for the log, fills the buffer from
170 * the in-memory log and then returns a pointer to that log in
171 * the private_data field in @file.
174 * This function returns zero if successful. On error it will return
175 * a negative error value.
177 static int fnic_trace_debugfs_open(struct inode
*inode
,
180 fnic_dbgfs_t
*fnic_dbg_prt
;
181 fnic_dbg_prt
= kzalloc(sizeof(fnic_dbgfs_t
), GFP_KERNEL
);
185 fnic_dbg_prt
->buffer
= vmalloc((3*(trace_max_pages
* PAGE_SIZE
)));
186 if (!fnic_dbg_prt
->buffer
) {
190 memset((void *)fnic_dbg_prt
->buffer
, 0,
191 (3*(trace_max_pages
* PAGE_SIZE
)));
192 fnic_dbg_prt
->buffer_len
= fnic_get_trace_data(fnic_dbg_prt
);
193 file
->private_data
= fnic_dbg_prt
;
198 * fnic_trace_debugfs_lseek - Seek through a debugfs file
199 * @file: The file pointer to seek through.
200 * @offset: The offset to seek to or the amount to seek by.
201 * @howto: Indicates how to seek.
204 * This routine is the entry point for the debugfs lseek file operation.
205 * The @howto parameter indicates whether @offset is the offset to directly
206 * seek to, or if it is a value to seek forward or reverse by. This function
207 * figures out what the new offset of the debugfs file will be and assigns
208 * that value to the f_pos field of @file.
211 * This function returns the new offset if successful and returns a negative
212 * error if unable to process the seek.
214 static loff_t
fnic_trace_debugfs_lseek(struct file
*file
,
218 fnic_dbgfs_t
*fnic_dbg_prt
= file
->private_data
;
219 return fixed_size_llseek(file
, offset
, howto
,
220 fnic_dbg_prt
->buffer_len
);
224 * fnic_trace_debugfs_read - Read a debugfs file
225 * @file: The file pointer to read from.
226 * @ubuf: The buffer to copy the data to.
227 * @nbytes: The number of bytes to read.
228 * @pos: The position in the file to start reading from.
231 * This routine reads data from the buffer indicated in the private_data
232 * field of @file. It will start reading at @pos and copy up to @nbytes of
236 * This function returns the amount of data that was read (this could be
237 * less than @nbytes if the end of the file was reached).
239 static ssize_t
fnic_trace_debugfs_read(struct file
*file
,
244 fnic_dbgfs_t
*fnic_dbg_prt
= file
->private_data
;
246 rc
= simple_read_from_buffer(ubuf
, nbytes
, pos
,
247 fnic_dbg_prt
->buffer
,
248 fnic_dbg_prt
->buffer_len
);
253 * fnic_trace_debugfs_release - Release the buffer used to store
255 * @inode: The inode pointer
256 * @file: The file pointer that contains the buffer to release
259 * This routine frees the buffer that was allocated when the debugfs
263 * This function returns zero.
265 static int fnic_trace_debugfs_release(struct inode
*inode
,
268 fnic_dbgfs_t
*fnic_dbg_prt
= file
->private_data
;
270 vfree(fnic_dbg_prt
->buffer
);
275 static const struct file_operations fnic_trace_ctrl_fops
= {
276 .owner
= THIS_MODULE
,
277 .open
= fnic_trace_ctrl_open
,
278 .read
= fnic_trace_ctrl_read
,
279 .write
= fnic_trace_ctrl_write
,
282 static const struct file_operations fnic_trace_debugfs_fops
= {
283 .owner
= THIS_MODULE
,
284 .open
= fnic_trace_debugfs_open
,
285 .llseek
= fnic_trace_debugfs_lseek
,
286 .read
= fnic_trace_debugfs_read
,
287 .release
= fnic_trace_debugfs_release
,
291 * fnic_trace_debugfs_init - Initialize debugfs for fnic trace logging
294 * When Debugfs is configured this routine sets up the fnic debugfs
295 * file system. If not already created, this routine will create the
296 * create file trace to log fnic trace buffer output into debugfs and
297 * it will also create file trace_enable to control enable/disable of
298 * trace logging into trace buffer.
300 int fnic_trace_debugfs_init(void)
303 if (!fnic_trace_debugfs_root
) {
305 "FNIC Debugfs root directory doesn't exist\n");
308 fnic_trace_enable
= debugfs_create_file("tracing_enable",
309 S_IFREG
|S_IRUGO
|S_IWUSR
,
310 fnic_trace_debugfs_root
,
311 NULL
, &fnic_trace_ctrl_fops
);
313 if (!fnic_trace_enable
) {
315 "Cannot create trace_enable file under debugfs\n");
319 fnic_trace_debugfs_file
= debugfs_create_file("trace",
320 S_IFREG
|S_IRUGO
|S_IWUSR
,
321 fnic_trace_debugfs_root
,
323 &fnic_trace_debugfs_fops
);
325 if (!fnic_trace_debugfs_file
) {
327 "Cannot create trace file under debugfs\n");
335 * fnic_trace_debugfs_terminate - Tear down debugfs infrastructure
338 * When Debugfs is configured this routine removes debugfs file system
339 * elements that are specific to fnic trace logging.
341 void fnic_trace_debugfs_terminate(void)
343 if (fnic_trace_debugfs_file
) {
344 debugfs_remove(fnic_trace_debugfs_file
);
345 fnic_trace_debugfs_file
= NULL
;
347 if (fnic_trace_enable
) {
348 debugfs_remove(fnic_trace_enable
);
349 fnic_trace_enable
= NULL
;
354 * fnic_reset_stats_open - Open the reset_stats file
355 * @inode: The inode pointer.
356 * @file: The file pointer to attach the stats reset flag.
359 * This routine opens a debugsfs file reset_stats and stores i_private data
360 * to debug structure to retrieve later for while performing other
364 * This function returns zero if successful.
366 static int fnic_reset_stats_open(struct inode
*inode
, struct file
*file
)
368 struct stats_debug_info
*debug
;
370 debug
= kzalloc(sizeof(struct stats_debug_info
), GFP_KERNEL
);
374 debug
->i_private
= inode
->i_private
;
376 file
->private_data
= debug
;
382 * fnic_reset_stats_read - Read a reset_stats debugfs file
383 * @filp: The file pointer to read from.
384 * @ubuf: The buffer to copy the data to.
385 * @cnt: The number of bytes to read.
386 * @ppos: The position in the file to start reading from.
389 * This routine reads value of variable reset_stats
390 * and stores into local @buf. It will start reading file at @ppos and
391 * copy up to @cnt of data to @ubuf from @buf.
394 * This function returns the amount of data that was read.
396 static ssize_t
fnic_reset_stats_read(struct file
*file
,
398 size_t cnt
, loff_t
*ppos
)
400 struct stats_debug_info
*debug
= file
->private_data
;
401 struct fnic
*fnic
= (struct fnic
*)debug
->i_private
;
405 len
= sprintf(buf
, "%u\n", fnic
->reset_stats
);
407 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, len
);
411 * fnic_reset_stats_write - Write to reset_stats debugfs file
412 * @filp: The file pointer to write from.
413 * @ubuf: The buffer to copy the data from.
414 * @cnt: The number of bytes to write.
415 * @ppos: The position in the file to start writing to.
418 * This routine writes data from user buffer @ubuf to buffer @buf and
419 * resets cumulative stats of fnic.
422 * This function returns the amount of data that was written.
424 static ssize_t
fnic_reset_stats_write(struct file
*file
,
425 const char __user
*ubuf
,
426 size_t cnt
, loff_t
*ppos
)
428 struct stats_debug_info
*debug
= file
->private_data
;
429 struct fnic
*fnic
= (struct fnic
*)debug
->i_private
;
430 struct fnic_stats
*stats
= &fnic
->fnic_stats
;
431 u64
*io_stats_p
= (u64
*)&stats
->io_stats
;
432 u64
*fw_stats_p
= (u64
*)&stats
->fw_stats
;
437 if (cnt
>= sizeof(buf
))
440 if (copy_from_user(&buf
, ubuf
, cnt
))
445 ret
= kstrtoul(buf
, 10, &val
);
449 fnic
->reset_stats
= val
;
451 if (fnic
->reset_stats
) {
452 /* Skip variable is used to avoid descrepancies to Num IOs
453 * and IO Completions stats. Skip incrementing No IO Compls
454 * for pending active IOs after reset stats
456 atomic64_set(&fnic
->io_cmpl_skip
,
457 atomic64_read(&stats
->io_stats
.active_ios
));
458 memset(&stats
->abts_stats
, 0, sizeof(struct abort_stats
));
459 memset(&stats
->term_stats
, 0,
460 sizeof(struct terminate_stats
));
461 memset(&stats
->reset_stats
, 0, sizeof(struct reset_stats
));
462 memset(&stats
->misc_stats
, 0, sizeof(struct misc_stats
));
463 memset(&stats
->vlan_stats
, 0, sizeof(struct vlan_stats
));
464 memset(io_stats_p
+1, 0,
465 sizeof(struct io_path_stats
) - sizeof(u64
));
466 memset(fw_stats_p
+1, 0,
467 sizeof(struct fw_stats
) - sizeof(u64
));
475 * fnic_reset_stats_release - Release the buffer used to store
477 * @inode: The inode pointer
478 * @file: The file pointer that contains the buffer to release
481 * This routine frees the buffer that was allocated when the debugfs
485 * This function returns zero.
487 static int fnic_reset_stats_release(struct inode
*inode
,
490 struct stats_debug_info
*debug
= file
->private_data
;
496 * fnic_stats_debugfs_open - Open the stats file for specific host
497 * and get fnic stats.
498 * @inode: The inode pointer.
499 * @file: The file pointer to attach the specific host statistics.
502 * This routine opens a debugsfs file stats of specific host and print
506 * This function returns zero if successful.
508 static int fnic_stats_debugfs_open(struct inode
*inode
,
511 struct fnic
*fnic
= inode
->i_private
;
512 struct fnic_stats
*fnic_stats
= &fnic
->fnic_stats
;
513 struct stats_debug_info
*debug
;
514 int buf_size
= 2 * PAGE_SIZE
;
516 debug
= kzalloc(sizeof(struct stats_debug_info
), GFP_KERNEL
);
520 debug
->debug_buffer
= vmalloc(buf_size
);
521 if (!debug
->debug_buffer
) {
526 debug
->buf_size
= buf_size
;
527 memset((void *)debug
->debug_buffer
, 0, buf_size
);
528 debug
->buffer_len
= fnic_get_stats_data(debug
, fnic_stats
);
530 file
->private_data
= debug
;
536 * fnic_stats_debugfs_read - Read a debugfs file
537 * @file: The file pointer to read from.
538 * @ubuf: The buffer to copy the data to.
539 * @nbytes: The number of bytes to read.
540 * @pos: The position in the file to start reading from.
543 * This routine reads data from the buffer indicated in the private_data
544 * field of @file. It will start reading at @pos and copy up to @nbytes of
548 * This function returns the amount of data that was read (this could be
549 * less than @nbytes if the end of the file was reached).
551 static ssize_t
fnic_stats_debugfs_read(struct file
*file
,
556 struct stats_debug_info
*debug
= file
->private_data
;
558 rc
= simple_read_from_buffer(ubuf
, nbytes
, pos
,
565 * fnic_stats_stats_release - Release the buffer used to store
567 * @inode: The inode pointer
568 * @file: The file pointer that contains the buffer to release
571 * This routine frees the buffer that was allocated when the debugfs
575 * This function returns zero.
577 static int fnic_stats_debugfs_release(struct inode
*inode
,
580 struct stats_debug_info
*debug
= file
->private_data
;
581 vfree(debug
->debug_buffer
);
586 static const struct file_operations fnic_stats_debugfs_fops
= {
587 .owner
= THIS_MODULE
,
588 .open
= fnic_stats_debugfs_open
,
589 .read
= fnic_stats_debugfs_read
,
590 .release
= fnic_stats_debugfs_release
,
593 static const struct file_operations fnic_reset_debugfs_fops
= {
594 .owner
= THIS_MODULE
,
595 .open
= fnic_reset_stats_open
,
596 .read
= fnic_reset_stats_read
,
597 .write
= fnic_reset_stats_write
,
598 .release
= fnic_reset_stats_release
,
602 * fnic_stats_init - Initialize stats struct and create stats file per fnic
605 * When Debugfs is configured this routine sets up the stats file per fnic
606 * It will create file stats and reset_stats under statistics/host# directory
607 * to log per fnic stats.
609 int fnic_stats_debugfs_init(struct fnic
*fnic
)
614 snprintf(name
, sizeof(name
), "host%d", fnic
->lport
->host
->host_no
);
616 if (!fnic_stats_debugfs_root
) {
617 printk(KERN_DEBUG
"fnic_stats root doesn't exist\n");
620 fnic
->fnic_stats_debugfs_host
= debugfs_create_dir(name
,
621 fnic_stats_debugfs_root
);
622 if (!fnic
->fnic_stats_debugfs_host
) {
623 printk(KERN_DEBUG
"Cannot create host directory\n");
627 fnic
->fnic_stats_debugfs_file
= debugfs_create_file("stats",
628 S_IFREG
|S_IRUGO
|S_IWUSR
,
629 fnic
->fnic_stats_debugfs_host
,
631 &fnic_stats_debugfs_fops
);
632 if (!fnic
->fnic_stats_debugfs_file
) {
633 printk(KERN_DEBUG
"Cannot create host stats file\n");
637 fnic
->fnic_reset_debugfs_file
= debugfs_create_file("reset_stats",
638 S_IFREG
|S_IRUGO
|S_IWUSR
,
639 fnic
->fnic_stats_debugfs_host
,
641 &fnic_reset_debugfs_fops
);
642 if (!fnic
->fnic_reset_debugfs_file
) {
643 printk(KERN_DEBUG
"Cannot create host stats file\n");
651 * fnic_stats_debugfs_remove - Tear down debugfs infrastructure of stats
654 * When Debugfs is configured this routine removes debugfs file system
655 * elements that are specific to fnic stats.
657 void fnic_stats_debugfs_remove(struct fnic
*fnic
)
662 debugfs_remove(fnic
->fnic_stats_debugfs_file
);
663 fnic
->fnic_stats_debugfs_file
= NULL
;
665 debugfs_remove(fnic
->fnic_reset_debugfs_file
);
666 fnic
->fnic_reset_debugfs_file
= NULL
;
668 debugfs_remove(fnic
->fnic_stats_debugfs_host
);
669 fnic
->fnic_stats_debugfs_host
= NULL
;