xref: /linux/drivers/scsi/fnic/fnic_debugfs.c (revision 4c461ee2b2a5a7c327fe092b41de1fcc002adc01)
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright 2012 Cisco Systems, Inc.  All rights reserved.
3 
4 #include <linux/module.h>
5 #include <linux/errno.h>
6 #include <linux/debugfs.h>
7 #include <linux/vmalloc.h>
8 #include "fnic.h"
9 
10 extern int fnic_get_debug_info(struct stats_debug_info *debug_buffer,
11 							   struct fnic *fnic);
12 
13 static int fnic_nvmef_debugfs_open(struct inode *inode,
14 			struct file *file);
15 static ssize_t fnic_nvmef_debugfs_read(struct file *file,
16 			char __user *ubuf,
17 			size_t nbytes, loff_t *pos);
18 static int fnic_nvmef_debugfs_release(struct inode *inode,
19 			struct file *file);
20 
21 static struct dentry *fnic_trace_debugfs_root;
22 static struct dentry *fnic_trace_debugfs_file;
23 static struct dentry *fnic_trace_enable;
24 static struct dentry *fnic_stats_debugfs_root;
25 static struct dentry *fnic_nvmef_debugfs_root;
26 
27 static struct dentry *fnic_fc_trace_debugfs_file;
28 static struct dentry *fnic_fc_rdata_trace_debugfs_file;
29 static struct dentry *fnic_fc_trace_enable;
30 static struct dentry *fnic_fc_trace_clear;
31 
32 struct fc_trace_flag_type {
33 	u8 fc_row_file;
34 	u8 fc_normal_file;
35 	u8 fnic_trace;
36 	u8 fc_trace;
37 	u8 fc_clear;
38 };
39 
40 static struct fc_trace_flag_type *fc_trc_flag;
41 
42 /*
43  * fnic_debugfs_init - Initialize debugfs for fnic debug logging
44  *
45  * Description:
46  * When Debugfs is configured this routine sets up the fnic debugfs
47  * file system. If not already created, this routine will create the
48  * fnic directory and statistics directory for trace buffer and
49  * stats logging.
50  */
51 int fnic_debugfs_init(void)
52 {
53 	fnic_trace_debugfs_root = debugfs_create_dir("fnic", NULL);
54 
55 	fnic_stats_debugfs_root = debugfs_create_dir("statistics",
56 						fnic_trace_debugfs_root);
57 
58 	fnic_nvmef_debugfs_root = debugfs_create_dir("nvme_info",
59 						     fnic_trace_debugfs_root);
60 
61 	/* Allocate memory to structure */
62 	fc_trc_flag = vmalloc(sizeof(struct fc_trace_flag_type));
63 
64 	if (fc_trc_flag) {
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;
70 		return 0;
71 	}
72 
73 	return -ENOMEM;
74 }
75 
76 /*
77  * fnic_debugfs_terminate - Tear down debugfs infrastructure
78  *
79  * Description:
80  * When Debugfs is configured this routine removes debugfs file system
81  * elements that are specific to fnic.
82  */
83 void fnic_debugfs_terminate(void)
84 {
85 	debugfs_remove(fnic_nvmef_debugfs_root);
86 	fnic_nvmef_debugfs_root = NULL;
87 
88 	debugfs_remove(fnic_stats_debugfs_root);
89 	fnic_stats_debugfs_root = NULL;
90 
91 	debugfs_remove(fnic_trace_debugfs_root);
92 	fnic_trace_debugfs_root = NULL;
93 
94 	vfree(fc_trc_flag);
95 }
96 
97 /*
98  * fnic_trace_ctrl_read -
99  *          Read  trace_enable ,fc_trace_enable
100  *              or fc_trace_clear debugfs file
101  * @filp: The file pointer to read from.
102  * @ubuf: The buffer to copy the data to.
103  * @cnt: The number of bytes to read.
104  * @ppos: The position in the file to start reading from.
105  *
106  * Description:
107  * This routine reads value of variable fnic_tracing_enabled or
108  * fnic_fc_tracing_enabled or fnic_fc_trace_cleared
109  * and stores into local @buf.
110  * It will start reading file at @ppos and
111  * copy up to @cnt of data to @ubuf from @buf.
112  *
113  * Returns:
114  * This function returns the amount of data that was read.
115  */
116 static ssize_t fnic_trace_ctrl_read(struct file *filp,
117 				  char __user *ubuf,
118 				  size_t cnt, loff_t *ppos)
119 {
120 	char buf[64];
121 	int len;
122 	u8 *trace_type;
123 	len = 0;
124 	trace_type = (u8 *)filp->private_data;
125 	if (*trace_type == fc_trc_flag->fnic_trace)
126 		len = sprintf(buf, "%d\n", fnic_tracing_enabled);
127 	else if (*trace_type == fc_trc_flag->fc_trace)
128 		len = sprintf(buf, "%d\n", fnic_fc_tracing_enabled);
129 	else if (*trace_type == fc_trc_flag->fc_clear)
130 		len = sprintf(buf, "%d\n", fnic_fc_trace_cleared);
131 	else
132 		pr_err("fnic: Cannot read to any debugfs file\n");
133 
134 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
135 }
136 
137 /*
138  * fnic_trace_ctrl_write -
139  * Write to trace_enable, fc_trace_enable or
140  *         fc_trace_clear debugfs file
141  * @filp: The file pointer to write from.
142  * @ubuf: The buffer to copy the data from.
143  * @cnt: The number of bytes to write.
144  * @ppos: The position in the file to start writing to.
145  *
146  * Description:
147  * This routine writes data from user buffer @ubuf to buffer @buf and
148  * sets fc_trace_enable ,tracing_enable or fnic_fc_trace_cleared
149  * value as per user input.
150  *
151  * Returns:
152  * This function returns the amount of data that was written.
153  */
154 static ssize_t fnic_trace_ctrl_write(struct file *filp,
155 				  const char __user *ubuf,
156 				  size_t cnt, loff_t *ppos)
157 {
158 	char buf[64];
159 	unsigned long val;
160 	int ret;
161 	u8 *trace_type;
162 	trace_type = (u8 *)filp->private_data;
163 
164 	if (cnt >= sizeof(buf))
165 		return -EINVAL;
166 
167 	if (copy_from_user(&buf, ubuf, cnt))
168 		return -EFAULT;
169 
170 	buf[cnt] = 0;
171 
172 	ret = kstrtoul(buf, 10, &val);
173 	if (ret < 0)
174 		return ret;
175 
176 	if (*trace_type == fc_trc_flag->fnic_trace)
177 		fnic_tracing_enabled = val;
178 	else if (*trace_type == fc_trc_flag->fc_trace)
179 		fnic_fc_tracing_enabled = val;
180 	else if (*trace_type == fc_trc_flag->fc_clear)
181 		fnic_fc_trace_cleared = val;
182 	else
183 		pr_err("fnic: cannot write to any debugfs file\n");
184 
185 	(*ppos)++;
186 
187 	return cnt;
188 }
189 
190 static const struct file_operations fnic_trace_ctrl_fops = {
191 	.owner = THIS_MODULE,
192 	.open = simple_open,
193 	.read = fnic_trace_ctrl_read,
194 	.write = fnic_trace_ctrl_write,
195 };
196 
197 /*
198  * fnic_trace_debugfs_open - Open the fnic trace log
199  * @inode: The inode pointer
200  * @file: The file pointer to attach the log output
201  *
202  * Description:
203  * This routine is the entry point for the debugfs open file operation.
204  * It allocates the necessary buffer for the log, fills the buffer from
205  * the in-memory log and then returns a pointer to that log in
206  * the private_data field in @file.
207  *
208  * Returns:
209  * This function returns zero if successful. On error it will return
210  * a negative error value.
211  */
212 static int fnic_trace_debugfs_open(struct inode *inode,
213 				  struct file *file)
214 {
215 	fnic_dbgfs_t *fnic_dbg_prt;
216 	u8 *rdata_ptr;
217 	rdata_ptr = (u8 *)inode->i_private;
218 	fnic_dbg_prt = kzalloc_obj(fnic_dbgfs_t);
219 	if (!fnic_dbg_prt)
220 		return -ENOMEM;
221 
222 	if (*rdata_ptr == fc_trc_flag->fnic_trace) {
223 		fnic_dbg_prt->buffer = vzalloc(array3_size(3, trace_max_pages,
224 							   PAGE_SIZE));
225 		if (!fnic_dbg_prt->buffer) {
226 			kfree(fnic_dbg_prt);
227 			return -ENOMEM;
228 		}
229 		fnic_dbg_prt->buffer_len = fnic_get_trace_data(fnic_dbg_prt);
230 	} else {
231 		fnic_dbg_prt->buffer =
232 			vzalloc(array3_size(3, fnic_fc_trace_max_pages,
233 					    PAGE_SIZE));
234 		if (!fnic_dbg_prt->buffer) {
235 			kfree(fnic_dbg_prt);
236 			return -ENOMEM;
237 		}
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_obj(struct stats_debug_info);
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_obj(struct stats_debug_info);
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 	debug->buffer_len += fnic_get_debug_info(debug, fnic);
615 
616 	file->private_data = debug;
617 
618 	return 0;
619 }
620 
621 /*
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.
627  *
628  * Description:
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
631  * data to @ubuf.
632  *
633  * Returns:
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).
636  */
637 static ssize_t fnic_stats_debugfs_read(struct file *file,
638 					char __user *ubuf,
639 					size_t nbytes,
640 					loff_t *pos)
641 {
642 	struct stats_debug_info *debug = file->private_data;
643 	int rc = 0;
644 	rc = simple_read_from_buffer(ubuf, nbytes, pos,
645 					debug->debug_buffer,
646 					debug->buffer_len);
647 	return rc;
648 }
649 
650 /*
651  * fnic_stats_stats_release - Release the buffer used to store
652  * debugfs file data
653  * @inode: The inode pointer
654  * @file: The file pointer that contains the buffer to release
655  *
656  * Description:
657  * This routine frees the buffer that was allocated when the debugfs
658  * file was opened.
659  *
660  * Returns:
661  * This function returns zero.
662  */
663 static int fnic_stats_debugfs_release(struct inode *inode,
664 					struct file *file)
665 {
666 	struct stats_debug_info *debug = file->private_data;
667 	vfree(debug->debug_buffer);
668 	kfree(debug);
669 	return 0;
670 }
671 
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,
677 };
678 
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,
685 };
686 
687 static const struct file_operations fnic_nvmef_debugfs_fops = {
688 	.owner = THIS_MODULE,
689 	.open = fnic_nvmef_debugfs_open,
690 	.read = fnic_nvmef_debugfs_read,
691 	.release = fnic_nvmef_debugfs_release,
692 };
693 
694 /*
695  * fnic_stats_init - Initialize stats struct and create stats file per fnic
696  *
697  * Description:
698  * When Debugfs is configured this routine sets up the stats file per fnic
699  * It will create file stats and reset_stats under statistics/host# directory
700  * to log per fnic stats.
701  */
702 int fnic_stats_debugfs_init(struct fnic *fnic)
703 {
704 	char name[16];
705 
706 	if (IS_FNIC_FCP_INITIATOR(fnic))
707 		snprintf(name, sizeof(name), "host%d", fnic->host->host_no);
708 	else if (IS_FNIC_NVME_INITIATOR(fnic))
709 		snprintf(name, sizeof(name), "nvfnic%d", fnic->fnic_num);
710 
711 	fnic->fnic_stats_debugfs_host = debugfs_create_dir(name,
712 						fnic_stats_debugfs_root);
713 	fnic->fnic_stats_debugfs_file = debugfs_create_file("stats",
714 						S_IFREG|S_IRUGO|S_IWUSR,
715 						fnic->fnic_stats_debugfs_host,
716 						fnic,
717 						&fnic_stats_debugfs_fops);
718 	fnic->fnic_reset_debugfs_file = debugfs_create_file("reset_stats",
719 						S_IFREG|S_IRUGO|S_IWUSR,
720 						fnic->fnic_stats_debugfs_host,
721 						fnic,
722 						&fnic_reset_debugfs_fops);
723 	return 0;
724 }
725 
726 /*
727  * fnic_stats_debugfs_remove - Tear down debugfs infrastructure of stats
728  *
729  * Description:
730  * When Debugfs is configured this routine removes debugfs file system
731  * elements that are specific to fnic stats.
732  */
733 void fnic_stats_debugfs_remove(struct fnic *fnic)
734 {
735 	if (!fnic)
736 		return;
737 
738 	debugfs_remove(fnic->fnic_stats_debugfs_file);
739 	fnic->fnic_stats_debugfs_file = NULL;
740 
741 	debugfs_remove(fnic->fnic_reset_debugfs_file);
742 	fnic->fnic_reset_debugfs_file = NULL;
743 
744 	debugfs_remove(fnic->fnic_stats_debugfs_host);
745 	fnic->fnic_stats_debugfs_host = NULL;
746 }
747 
748 void fnic_nvmef_debugfs_init(struct fnic *fnic)
749 {
750 	char name[16];
751 
752 	snprintf(name, sizeof(name), "host%d", fnic->fnic_num);
753 
754 	fnic->fnic_nvmef_debugfs_host = debugfs_create_dir(name,
755 							   fnic_nvmef_debugfs_root);
756 	fnic->fnic_nvmef_debugfs_file = debugfs_create_file("nvmef_info",
757 							    S_IFREG | 0444,
758 							    fnic->fnic_nvmef_debugfs_host,
759 							    fnic,
760 							    &fnic_nvmef_debugfs_fops);
761 }
762 
763 static int fnic_nvmef_debugfs_open(struct inode *inode, struct file *file)
764 {
765 
766 	struct fnic *fnic = inode->i_private;
767 	struct fnic_nvmef_info *info;
768 	int buf_size = 2 * PAGE_SIZE;
769 
770 	info = kzalloc_obj(struct fnic_nvmef_info, GFP_KERNEL);
771 	if (!info)
772 		return -ENOMEM;
773 
774 	info->info_buffer = vmalloc(buf_size);
775 	if (!info->info_buffer) {
776 		kfree(info);
777 		return -ENOMEM;
778 	}
779 
780 	info->buf_size = buf_size;
781 	memset((void *)info->info_buffer, 0, buf_size);
782 	info->buffer_len = nvfnic_get_nvmef_info(fnic, info);
783 
784 	file->private_data = info;
785 
786 	return 0;
787 }
788 
789 static ssize_t fnic_nvmef_debugfs_read(struct file *file,
790 				       char __user *ubuf,
791 				       size_t nbytes, loff_t *pos)
792 {
793 	struct fnic_nvmef_info *info = file->private_data;
794 
795 	return simple_read_from_buffer(ubuf, nbytes, pos,
796 				     info->info_buffer, info->buffer_len);
797 }
798 
799 static int fnic_nvmef_debugfs_release(struct inode *inode, struct file *file)
800 {
801 	struct fnic_nvmef_info *info = file->private_data;
802 
803 	vfree(info->info_buffer);
804 	kfree(info);
805 	return 0;
806 }
807 
808 void fnic_nvmef_debugfs_remove(struct fnic *fnic)
809 {
810 	if (!fnic)
811 		return;
812 
813 	debugfs_remove(fnic->fnic_nvmef_debugfs_file);
814 	fnic->fnic_nvmef_debugfs_file = NULL;
815 
816 	debugfs_remove(fnic->fnic_nvmef_debugfs_host);
817 	fnic->fnic_nvmef_debugfs_host = NULL;
818 }
819