xref: /openbmc/linux/drivers/scsi/fnic/fnic_debugfs.c (revision f16fe2d3)
1 /*
2  * Copyright 2012 Cisco Systems, Inc.  All rights reserved.
3  *
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.
7  *
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
15  * SOFTWARE.
16  */
17 
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/debugfs.h>
21 #include <linux/vmalloc.h>
22 #include "fnic.h"
23 
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;
28 
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;
33 
34 struct fc_trace_flag_type {
35 	u8 fc_row_file;
36 	u8 fc_normal_file;
37 	u8 fnic_trace;
38 	u8 fc_trace;
39 	u8 fc_clear;
40 };
41 
42 static struct fc_trace_flag_type *fc_trc_flag;
43 
44 /*
45  * fnic_debugfs_init - Initialize debugfs for fnic debug logging
46  *
47  * Description:
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
51  * stats logging.
52  */
53 int fnic_debugfs_init(void)
54 {
55 	fnic_trace_debugfs_root = debugfs_create_dir("fnic", NULL);
56 
57 	fnic_stats_debugfs_root = debugfs_create_dir("statistics",
58 						fnic_trace_debugfs_root);
59 
60 	/* Allocate memory to structure */
61 	fc_trc_flag = vmalloc(sizeof(struct fc_trace_flag_type));
62 
63 	if (fc_trc_flag) {
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;
69 	}
70 
71 	return 0;
72 }
73 
74 /*
75  * fnic_debugfs_terminate - Tear down debugfs infrastructure
76  *
77  * Description:
78  * When Debugfs is configured this routine removes debugfs file system
79  * elements that are specific to fnic.
80  */
81 void fnic_debugfs_terminate(void)
82 {
83 	debugfs_remove(fnic_stats_debugfs_root);
84 	fnic_stats_debugfs_root = NULL;
85 
86 	debugfs_remove(fnic_trace_debugfs_root);
87 	fnic_trace_debugfs_root = NULL;
88 
89 	if (fc_trc_flag)
90 		vfree(fc_trc_flag);
91 }
92 
93 /*
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.
101  *
102  * Description:
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.
108  *
109  * Returns:
110  * This function returns the amount of data that was read.
111  */
112 static ssize_t fnic_trace_ctrl_read(struct file *filp,
113 				  char __user *ubuf,
114 				  size_t cnt, loff_t *ppos)
115 {
116 	char buf[64];
117 	int len;
118 	u8 *trace_type;
119 	len = 0;
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);
127 	else
128 		pr_err("fnic: Cannot read to any debugfs file\n");
129 
130 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
131 }
132 
133 /*
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.
141  *
142  * Description:
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.
146  *
147  * Returns:
148  * This function returns the amount of data that was written.
149  */
150 static ssize_t fnic_trace_ctrl_write(struct file *filp,
151 				  const char __user *ubuf,
152 				  size_t cnt, loff_t *ppos)
153 {
154 	char buf[64];
155 	unsigned long val;
156 	int ret;
157 	u8 *trace_type;
158 	trace_type = (u8 *)filp->private_data;
159 
160 	if (cnt >= sizeof(buf))
161 		return -EINVAL;
162 
163 	if (copy_from_user(&buf, ubuf, cnt))
164 		return -EFAULT;
165 
166 	buf[cnt] = 0;
167 
168 	ret = kstrtoul(buf, 10, &val);
169 	if (ret < 0)
170 		return ret;
171 
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;
178 	else
179 		pr_err("fnic: cannot write to any debugfs file\n");
180 
181 	(*ppos)++;
182 
183 	return cnt;
184 }
185 
186 static const struct file_operations fnic_trace_ctrl_fops = {
187 	.owner = THIS_MODULE,
188 	.open = simple_open,
189 	.read = fnic_trace_ctrl_read,
190 	.write = fnic_trace_ctrl_write,
191 };
192 
193 /*
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
197  *
198  * Description:
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.
203  *
204  * Returns:
205  * This function returns zero if successful. On error it will return
206  * a negative error value.
207  */
208 static int fnic_trace_debugfs_open(struct inode *inode,
209 				  struct file *file)
210 {
211 	fnic_dbgfs_t *fnic_dbg_prt;
212 	u8 *rdata_ptr;
213 	rdata_ptr = (u8 *)inode->i_private;
214 	fnic_dbg_prt = kzalloc(sizeof(fnic_dbgfs_t), GFP_KERNEL);
215 	if (!fnic_dbg_prt)
216 		return -ENOMEM;
217 
218 	if (*rdata_ptr == fc_trc_flag->fnic_trace) {
219 		fnic_dbg_prt->buffer = vmalloc(array3_size(3, trace_max_pages,
220 							   PAGE_SIZE));
221 		if (!fnic_dbg_prt->buffer) {
222 			kfree(fnic_dbg_prt);
223 			return -ENOMEM;
224 		}
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);
228 	} else {
229 		fnic_dbg_prt->buffer =
230 			vmalloc(array3_size(3, fnic_fc_trace_max_pages,
231 					    PAGE_SIZE));
232 		if (!fnic_dbg_prt->buffer) {
233 			kfree(fnic_dbg_prt);
234 			return -ENOMEM;
235 		}
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);
240 	}
241 	file->private_data = fnic_dbg_prt;
242 
243 	return 0;
244 }
245 
246 /*
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.
251  *
252  * Description:
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.
258  *
259  * Returns:
260  * This function returns the new offset if successful and returns a negative
261  * error if unable to process the seek.
262  */
263 static loff_t fnic_trace_debugfs_lseek(struct file *file,
264 					loff_t offset,
265 					int howto)
266 {
267 	fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
268 	return fixed_size_llseek(file, offset, howto,
269 				fnic_dbg_prt->buffer_len);
270 }
271 
272 /*
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.
278  *
279  * Description:
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
282  * data to @ubuf.
283  *
284  * Returns:
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).
287  */
288 static ssize_t fnic_trace_debugfs_read(struct file *file,
289 					char __user *ubuf,
290 					size_t nbytes,
291 					loff_t *pos)
292 {
293 	fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
294 	int rc = 0;
295 	rc = simple_read_from_buffer(ubuf, nbytes, pos,
296 				  fnic_dbg_prt->buffer,
297 				  fnic_dbg_prt->buffer_len);
298 	return rc;
299 }
300 
301 /*
302  * fnic_trace_debugfs_release - Release the buffer used to store
303  * debugfs file data
304  * @inode: The inode pointer
305  * @file: The file pointer that contains the buffer to release
306  *
307  * Description:
308  * This routine frees the buffer that was allocated when the debugfs
309  * file was opened.
310  *
311  * Returns:
312  * This function returns zero.
313  */
314 static int fnic_trace_debugfs_release(struct inode *inode,
315 					  struct file *file)
316 {
317 	fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
318 
319 	vfree(fnic_dbg_prt->buffer);
320 	kfree(fnic_dbg_prt);
321 	return 0;
322 }
323 
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,
330 };
331 
332 /*
333  * fnic_trace_debugfs_init - Initialize debugfs for fnic trace logging
334  *
335  * Description:
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.
341  */
342 void fnic_trace_debugfs_init(void)
343 {
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);
349 
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);
355 }
356 
357 /*
358  * fnic_trace_debugfs_terminate - Tear down debugfs infrastructure
359  *
360  * Description:
361  * When Debugfs is configured this routine removes debugfs file system
362  * elements that are specific to fnic trace logging.
363  */
364 void fnic_trace_debugfs_terminate(void)
365 {
366 	debugfs_remove(fnic_trace_debugfs_file);
367 	fnic_trace_debugfs_file = NULL;
368 
369 	debugfs_remove(fnic_trace_enable);
370 	fnic_trace_enable = NULL;
371 }
372 
373 /*
374  * fnic_fc_trace_debugfs_init -
375  * Initialize debugfs for fnic control frame trace logging
376  *
377  * Description:
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.
383  */
384 
385 void fnic_fc_trace_debugfs_init(void)
386 {
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);
392 
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);
398 
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);
405 
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);
412 }
413 
414 /*
415  * fnic_fc_trace_debugfs_terminate - Tear down debugfs infrastructure
416  *
417  * Description:
418  * When Debugfs is configured this routine removes debugfs file system
419  * elements that are specific to fnic_fc trace logging.
420  */
421 
422 void fnic_fc_trace_debugfs_terminate(void)
423 {
424 	debugfs_remove(fnic_fc_trace_debugfs_file);
425 	fnic_fc_trace_debugfs_file = NULL;
426 
427 	debugfs_remove(fnic_fc_rdata_trace_debugfs_file);
428 	fnic_fc_rdata_trace_debugfs_file = NULL;
429 
430 	debugfs_remove(fnic_fc_trace_enable);
431 	fnic_fc_trace_enable = NULL;
432 
433 	debugfs_remove(fnic_fc_trace_clear);
434 	fnic_fc_trace_clear = NULL;
435 }
436 
437 /*
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.
441  *
442  * Description:
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
445  * file oprations.
446  *
447  * Returns:
448  * This function returns zero if successful.
449  */
450 static int fnic_reset_stats_open(struct inode *inode, struct file *file)
451 {
452 	struct stats_debug_info *debug;
453 
454 	debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
455 	if (!debug)
456 		return -ENOMEM;
457 
458 	debug->i_private = inode->i_private;
459 
460 	file->private_data = debug;
461 
462 	return 0;
463 }
464 
465 /*
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.
471  *
472  * Description:
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.
476  *
477  * Returns:
478  * This function returns the amount of data that was read.
479  */
480 static ssize_t fnic_reset_stats_read(struct file *file,
481 					char __user *ubuf,
482 					size_t cnt, loff_t *ppos)
483 {
484 	struct stats_debug_info *debug = file->private_data;
485 	struct fnic *fnic = (struct fnic *)debug->i_private;
486 	char buf[64];
487 	int len;
488 
489 	len = sprintf(buf, "%u\n", fnic->reset_stats);
490 
491 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
492 }
493 
494 /*
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.
500  *
501  * Description:
502  * This routine writes data from user buffer @ubuf to buffer @buf and
503  * resets cumulative stats of fnic.
504  *
505  * Returns:
506  * This function returns the amount of data that was written.
507  */
508 static ssize_t fnic_reset_stats_write(struct file *file,
509 					const char __user *ubuf,
510 					size_t cnt, loff_t *ppos)
511 {
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;
517 	char buf[64];
518 	unsigned long val;
519 	int ret;
520 
521 	if (cnt >= sizeof(buf))
522 		return -EINVAL;
523 
524 	if (copy_from_user(&buf, ubuf, cnt))
525 		return -EFAULT;
526 
527 	buf[cnt] = 0;
528 
529 	ret = kstrtoul(buf, 10, &val);
530 	if (ret < 0)
531 		return ret;
532 
533 	fnic->reset_stats = val;
534 
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
539 		 */
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);
553 	}
554 
555 	(*ppos)++;
556 	return cnt;
557 }
558 
559 /*
560  * fnic_reset_stats_release - Release the buffer used to store
561  * debugfs file data
562  * @inode: The inode pointer
563  * @file: The file pointer that contains the buffer to release
564  *
565  * Description:
566  * This routine frees the buffer that was allocated when the debugfs
567  * file was opened.
568  *
569  * Returns:
570  * This function returns zero.
571  */
572 static int fnic_reset_stats_release(struct inode *inode,
573 					struct file *file)
574 {
575 	struct stats_debug_info *debug = file->private_data;
576 	kfree(debug);
577 	return 0;
578 }
579 
580 /*
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.
585  *
586  * Description:
587  * This routine opens a debugsfs file stats of specific host and print
588  * fnic stats.
589  *
590  * Returns:
591  * This function returns zero if successful.
592  */
593 static int fnic_stats_debugfs_open(struct inode *inode,
594 					struct file *file)
595 {
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;
600 
601 	debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
602 	if (!debug)
603 		return -ENOMEM;
604 
605 	debug->debug_buffer = vmalloc(buf_size);
606 	if (!debug->debug_buffer) {
607 		kfree(debug);
608 		return -ENOMEM;
609 	}
610 
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);
614 
615 	file->private_data = debug;
616 
617 	return 0;
618 }
619 
620 /*
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.
626  *
627  * Description:
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
630  * data to @ubuf.
631  *
632  * Returns:
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).
635  */
636 static ssize_t fnic_stats_debugfs_read(struct file *file,
637 					char __user *ubuf,
638 					size_t nbytes,
639 					loff_t *pos)
640 {
641 	struct stats_debug_info *debug = file->private_data;
642 	int rc = 0;
643 	rc = simple_read_from_buffer(ubuf, nbytes, pos,
644 					debug->debug_buffer,
645 					debug->buffer_len);
646 	return rc;
647 }
648 
649 /*
650  * fnic_stats_stats_release - Release the buffer used to store
651  * debugfs file data
652  * @inode: The inode pointer
653  * @file: The file pointer that contains the buffer to release
654  *
655  * Description:
656  * This routine frees the buffer that was allocated when the debugfs
657  * file was opened.
658  *
659  * Returns:
660  * This function returns zero.
661  */
662 static int fnic_stats_debugfs_release(struct inode *inode,
663 					struct file *file)
664 {
665 	struct stats_debug_info *debug = file->private_data;
666 	vfree(debug->debug_buffer);
667 	kfree(debug);
668 	return 0;
669 }
670 
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,
676 };
677 
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,
684 };
685 
686 /*
687  * fnic_stats_init - Initialize stats struct and create stats file per fnic
688  *
689  * Description:
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.
693  */
694 void fnic_stats_debugfs_init(struct fnic *fnic)
695 {
696 	char name[16];
697 
698 	snprintf(name, sizeof(name), "host%d", fnic->lport->host->host_no);
699 
700 	fnic->fnic_stats_debugfs_host = debugfs_create_dir(name,
701 						fnic_stats_debugfs_root);
702 
703 	fnic->fnic_stats_debugfs_file = debugfs_create_file("stats",
704 						S_IFREG|S_IRUGO|S_IWUSR,
705 						fnic->fnic_stats_debugfs_host,
706 						fnic,
707 						&fnic_stats_debugfs_fops);
708 
709 	fnic->fnic_reset_debugfs_file = debugfs_create_file("reset_stats",
710 						S_IFREG|S_IRUGO|S_IWUSR,
711 						fnic->fnic_stats_debugfs_host,
712 						fnic,
713 						&fnic_reset_debugfs_fops);
714 }
715 
716 /*
717  * fnic_stats_debugfs_remove - Tear down debugfs infrastructure of stats
718  *
719  * Description:
720  * When Debugfs is configured this routine removes debugfs file system
721  * elements that are specific to fnic stats.
722  */
723 void fnic_stats_debugfs_remove(struct fnic *fnic)
724 {
725 	if (!fnic)
726 		return;
727 
728 	debugfs_remove(fnic->fnic_stats_debugfs_file);
729 	fnic->fnic_stats_debugfs_file = NULL;
730 
731 	debugfs_remove(fnic->fnic_reset_debugfs_file);
732 	fnic->fnic_reset_debugfs_file = NULL;
733 
734 	debugfs_remove(fnic->fnic_stats_debugfs_host);
735 	fnic->fnic_stats_debugfs_host = NULL;
736 }
737