xref: /openbmc/linux/drivers/scsi/fnic/fnic_debugfs.c (revision ba61bb17)
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 	int rc = -1;
56 	fnic_trace_debugfs_root = debugfs_create_dir("fnic", NULL);
57 	if (!fnic_trace_debugfs_root) {
58 		printk(KERN_DEBUG "Cannot create debugfs root\n");
59 		return rc;
60 	}
61 
62 	if (!fnic_trace_debugfs_root) {
63 		printk(KERN_DEBUG
64 			"fnic root directory doesn't exist in debugfs\n");
65 		return rc;
66 	}
67 
68 	fnic_stats_debugfs_root = debugfs_create_dir("statistics",
69 						fnic_trace_debugfs_root);
70 	if (!fnic_stats_debugfs_root) {
71 		printk(KERN_DEBUG "Cannot create Statistics directory\n");
72 		return rc;
73 	}
74 
75 	/* Allocate memory to structure */
76 	fc_trc_flag = (struct fc_trace_flag_type *)
77 		vmalloc(sizeof(struct fc_trace_flag_type));
78 
79 	if (fc_trc_flag) {
80 		fc_trc_flag->fc_row_file = 0;
81 		fc_trc_flag->fc_normal_file = 1;
82 		fc_trc_flag->fnic_trace = 2;
83 		fc_trc_flag->fc_trace = 3;
84 		fc_trc_flag->fc_clear = 4;
85 	}
86 
87 	rc = 0;
88 	return rc;
89 }
90 
91 /*
92  * fnic_debugfs_terminate - Tear down debugfs infrastructure
93  *
94  * Description:
95  * When Debugfs is configured this routine removes debugfs file system
96  * elements that are specific to fnic.
97  */
98 void fnic_debugfs_terminate(void)
99 {
100 	debugfs_remove(fnic_stats_debugfs_root);
101 	fnic_stats_debugfs_root = NULL;
102 
103 	debugfs_remove(fnic_trace_debugfs_root);
104 	fnic_trace_debugfs_root = NULL;
105 
106 	if (fc_trc_flag)
107 		vfree(fc_trc_flag);
108 }
109 
110 /*
111  * fnic_trace_ctrl_read -
112  *          Read  trace_enable ,fc_trace_enable
113  *              or fc_trace_clear debugfs file
114  * @filp: The file pointer to read from.
115  * @ubuf: The buffer to copy the data to.
116  * @cnt: The number of bytes to read.
117  * @ppos: The position in the file to start reading from.
118  *
119  * Description:
120  * This routine reads value of variable fnic_tracing_enabled or
121  * fnic_fc_tracing_enabled or fnic_fc_trace_cleared
122  * and stores into local @buf.
123  * It will start reading file at @ppos and
124  * copy up to @cnt of data to @ubuf from @buf.
125  *
126  * Returns:
127  * This function returns the amount of data that was read.
128  */
129 static ssize_t fnic_trace_ctrl_read(struct file *filp,
130 				  char __user *ubuf,
131 				  size_t cnt, loff_t *ppos)
132 {
133 	char buf[64];
134 	int len;
135 	u8 *trace_type;
136 	len = 0;
137 	trace_type = (u8 *)filp->private_data;
138 	if (*trace_type == fc_trc_flag->fnic_trace)
139 		len = sprintf(buf, "%u\n", fnic_tracing_enabled);
140 	else if (*trace_type == fc_trc_flag->fc_trace)
141 		len = sprintf(buf, "%u\n", fnic_fc_tracing_enabled);
142 	else if (*trace_type == fc_trc_flag->fc_clear)
143 		len = sprintf(buf, "%u\n", fnic_fc_trace_cleared);
144 	else
145 		pr_err("fnic: Cannot read to any debugfs file\n");
146 
147 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
148 }
149 
150 /*
151  * fnic_trace_ctrl_write -
152  * Write to trace_enable, fc_trace_enable or
153  *         fc_trace_clear debugfs file
154  * @filp: The file pointer to write from.
155  * @ubuf: The buffer to copy the data from.
156  * @cnt: The number of bytes to write.
157  * @ppos: The position in the file to start writing to.
158  *
159  * Description:
160  * This routine writes data from user buffer @ubuf to buffer @buf and
161  * sets fc_trace_enable ,tracing_enable or fnic_fc_trace_cleared
162  * value as per user input.
163  *
164  * Returns:
165  * This function returns the amount of data that was written.
166  */
167 static ssize_t fnic_trace_ctrl_write(struct file *filp,
168 				  const char __user *ubuf,
169 				  size_t cnt, loff_t *ppos)
170 {
171 	char buf[64];
172 	unsigned long val;
173 	int ret;
174 	u8 *trace_type;
175 	trace_type = (u8 *)filp->private_data;
176 
177 	if (cnt >= sizeof(buf))
178 		return -EINVAL;
179 
180 	if (copy_from_user(&buf, ubuf, cnt))
181 		return -EFAULT;
182 
183 	buf[cnt] = 0;
184 
185 	ret = kstrtoul(buf, 10, &val);
186 	if (ret < 0)
187 		return ret;
188 
189 	if (*trace_type == fc_trc_flag->fnic_trace)
190 		fnic_tracing_enabled = val;
191 	else if (*trace_type == fc_trc_flag->fc_trace)
192 		fnic_fc_tracing_enabled = val;
193 	else if (*trace_type == fc_trc_flag->fc_clear)
194 		fnic_fc_trace_cleared = val;
195 	else
196 		pr_err("fnic: cannot write to any debugfs file\n");
197 
198 	(*ppos)++;
199 
200 	return cnt;
201 }
202 
203 static const struct file_operations fnic_trace_ctrl_fops = {
204 	.owner = THIS_MODULE,
205 	.open = simple_open,
206 	.read = fnic_trace_ctrl_read,
207 	.write = fnic_trace_ctrl_write,
208 };
209 
210 /*
211  * fnic_trace_debugfs_open - Open the fnic trace log
212  * @inode: The inode pointer
213  * @file: The file pointer to attach the log output
214  *
215  * Description:
216  * This routine is the entry point for the debugfs open file operation.
217  * It allocates the necessary buffer for the log, fills the buffer from
218  * the in-memory log and then returns a pointer to that log in
219  * the private_data field in @file.
220  *
221  * Returns:
222  * This function returns zero if successful. On error it will return
223  * a negative error value.
224  */
225 static int fnic_trace_debugfs_open(struct inode *inode,
226 				  struct file *file)
227 {
228 	fnic_dbgfs_t *fnic_dbg_prt;
229 	u8 *rdata_ptr;
230 	rdata_ptr = (u8 *)inode->i_private;
231 	fnic_dbg_prt = kzalloc(sizeof(fnic_dbgfs_t), GFP_KERNEL);
232 	if (!fnic_dbg_prt)
233 		return -ENOMEM;
234 
235 	if (*rdata_ptr == fc_trc_flag->fnic_trace) {
236 		fnic_dbg_prt->buffer = vmalloc(array3_size(3, trace_max_pages,
237 							   PAGE_SIZE));
238 		if (!fnic_dbg_prt->buffer) {
239 			kfree(fnic_dbg_prt);
240 			return -ENOMEM;
241 		}
242 		memset((void *)fnic_dbg_prt->buffer, 0,
243 		3 * (trace_max_pages * PAGE_SIZE));
244 		fnic_dbg_prt->buffer_len = fnic_get_trace_data(fnic_dbg_prt);
245 	} else {
246 		fnic_dbg_prt->buffer =
247 			vmalloc(array3_size(3, fnic_fc_trace_max_pages,
248 					    PAGE_SIZE));
249 		if (!fnic_dbg_prt->buffer) {
250 			kfree(fnic_dbg_prt);
251 			return -ENOMEM;
252 		}
253 		memset((void *)fnic_dbg_prt->buffer, 0,
254 			3 * (fnic_fc_trace_max_pages * PAGE_SIZE));
255 		fnic_dbg_prt->buffer_len =
256 			fnic_fc_trace_get_data(fnic_dbg_prt, *rdata_ptr);
257 	}
258 	file->private_data = fnic_dbg_prt;
259 
260 	return 0;
261 }
262 
263 /*
264  * fnic_trace_debugfs_lseek - Seek through a debugfs file
265  * @file: The file pointer to seek through.
266  * @offset: The offset to seek to or the amount to seek by.
267  * @howto: Indicates how to seek.
268  *
269  * Description:
270  * This routine is the entry point for the debugfs lseek file operation.
271  * The @howto parameter indicates whether @offset is the offset to directly
272  * seek to, or if it is a value to seek forward or reverse by. This function
273  * figures out what the new offset of the debugfs file will be and assigns
274  * that value to the f_pos field of @file.
275  *
276  * Returns:
277  * This function returns the new offset if successful and returns a negative
278  * error if unable to process the seek.
279  */
280 static loff_t fnic_trace_debugfs_lseek(struct file *file,
281 					loff_t offset,
282 					int howto)
283 {
284 	fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
285 	return fixed_size_llseek(file, offset, howto,
286 				fnic_dbg_prt->buffer_len);
287 }
288 
289 /*
290  * fnic_trace_debugfs_read - Read a debugfs file
291  * @file: The file pointer to read from.
292  * @ubuf: The buffer to copy the data to.
293  * @nbytes: The number of bytes to read.
294  * @pos: The position in the file to start reading from.
295  *
296  * Description:
297  * This routine reads data from the buffer indicated in the private_data
298  * field of @file. It will start reading at @pos and copy up to @nbytes of
299  * data to @ubuf.
300  *
301  * Returns:
302  * This function returns the amount of data that was read (this could be
303  * less than @nbytes if the end of the file was reached).
304  */
305 static ssize_t fnic_trace_debugfs_read(struct file *file,
306 					char __user *ubuf,
307 					size_t nbytes,
308 					loff_t *pos)
309 {
310 	fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
311 	int rc = 0;
312 	rc = simple_read_from_buffer(ubuf, nbytes, pos,
313 				  fnic_dbg_prt->buffer,
314 				  fnic_dbg_prt->buffer_len);
315 	return rc;
316 }
317 
318 /*
319  * fnic_trace_debugfs_release - Release the buffer used to store
320  * debugfs file data
321  * @inode: The inode pointer
322  * @file: The file pointer that contains the buffer to release
323  *
324  * Description:
325  * This routine frees the buffer that was allocated when the debugfs
326  * file was opened.
327  *
328  * Returns:
329  * This function returns zero.
330  */
331 static int fnic_trace_debugfs_release(struct inode *inode,
332 					  struct file *file)
333 {
334 	fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
335 
336 	vfree(fnic_dbg_prt->buffer);
337 	kfree(fnic_dbg_prt);
338 	return 0;
339 }
340 
341 static const struct file_operations fnic_trace_debugfs_fops = {
342 	.owner = THIS_MODULE,
343 	.open = fnic_trace_debugfs_open,
344 	.llseek = fnic_trace_debugfs_lseek,
345 	.read = fnic_trace_debugfs_read,
346 	.release = fnic_trace_debugfs_release,
347 };
348 
349 /*
350  * fnic_trace_debugfs_init - Initialize debugfs for fnic trace logging
351  *
352  * Description:
353  * When Debugfs is configured this routine sets up the fnic debugfs
354  * file system. If not already created, this routine will create the
355  * create file trace to log fnic trace buffer output into debugfs and
356  * it will also create file trace_enable to control enable/disable of
357  * trace logging into trace buffer.
358  */
359 int fnic_trace_debugfs_init(void)
360 {
361 	int rc = -1;
362 	if (!fnic_trace_debugfs_root) {
363 		printk(KERN_DEBUG
364 			"FNIC Debugfs root directory doesn't exist\n");
365 		return rc;
366 	}
367 	fnic_trace_enable = debugfs_create_file("tracing_enable",
368 					S_IFREG|S_IRUGO|S_IWUSR,
369 					fnic_trace_debugfs_root,
370 					&(fc_trc_flag->fnic_trace),
371 					&fnic_trace_ctrl_fops);
372 
373 	if (!fnic_trace_enable) {
374 		printk(KERN_DEBUG
375 			"Cannot create trace_enable file under debugfs\n");
376 		return rc;
377 	}
378 
379 	fnic_trace_debugfs_file = debugfs_create_file("trace",
380 					S_IFREG|S_IRUGO|S_IWUSR,
381 					fnic_trace_debugfs_root,
382 					&(fc_trc_flag->fnic_trace),
383 					&fnic_trace_debugfs_fops);
384 
385 	if (!fnic_trace_debugfs_file) {
386 		printk(KERN_DEBUG
387 			"Cannot create trace file under debugfs\n");
388 		return rc;
389 	}
390 	rc = 0;
391 	return rc;
392 }
393 
394 /*
395  * fnic_trace_debugfs_terminate - Tear down debugfs infrastructure
396  *
397  * Description:
398  * When Debugfs is configured this routine removes debugfs file system
399  * elements that are specific to fnic trace logging.
400  */
401 void fnic_trace_debugfs_terminate(void)
402 {
403 	debugfs_remove(fnic_trace_debugfs_file);
404 	fnic_trace_debugfs_file = NULL;
405 
406 	debugfs_remove(fnic_trace_enable);
407 	fnic_trace_enable = NULL;
408 }
409 
410 /*
411  * fnic_fc_trace_debugfs_init -
412  * Initialize debugfs for fnic control frame trace logging
413  *
414  * Description:
415  * When Debugfs is configured this routine sets up the fnic_fc debugfs
416  * file system. If not already created, this routine will create the
417  * create file trace to log fnic fc trace buffer output into debugfs and
418  * it will also create file fc_trace_enable to control enable/disable of
419  * trace logging into trace buffer.
420  */
421 
422 int fnic_fc_trace_debugfs_init(void)
423 {
424 	int rc = -1;
425 
426 	if (!fnic_trace_debugfs_root) {
427 		pr_err("fnic:Debugfs root directory doesn't exist\n");
428 		return rc;
429 	}
430 
431 	fnic_fc_trace_enable = debugfs_create_file("fc_trace_enable",
432 					S_IFREG|S_IRUGO|S_IWUSR,
433 					fnic_trace_debugfs_root,
434 					&(fc_trc_flag->fc_trace),
435 					&fnic_trace_ctrl_fops);
436 
437 	if (!fnic_fc_trace_enable) {
438 		pr_err("fnic: Failed create fc_trace_enable file\n");
439 		return rc;
440 	}
441 
442 	fnic_fc_trace_clear = debugfs_create_file("fc_trace_clear",
443 					S_IFREG|S_IRUGO|S_IWUSR,
444 					fnic_trace_debugfs_root,
445 					&(fc_trc_flag->fc_clear),
446 					&fnic_trace_ctrl_fops);
447 
448 	if (!fnic_fc_trace_clear) {
449 		pr_err("fnic: Failed to create fc_trace_enable file\n");
450 		return rc;
451 	}
452 
453 	fnic_fc_rdata_trace_debugfs_file =
454 		debugfs_create_file("fc_trace_rdata",
455 				    S_IFREG|S_IRUGO|S_IWUSR,
456 				    fnic_trace_debugfs_root,
457 				    &(fc_trc_flag->fc_normal_file),
458 				    &fnic_trace_debugfs_fops);
459 
460 	if (!fnic_fc_rdata_trace_debugfs_file) {
461 		pr_err("fnic: Failed create fc_rdata_trace file\n");
462 		return rc;
463 	}
464 
465 	fnic_fc_trace_debugfs_file =
466 		debugfs_create_file("fc_trace",
467 				    S_IFREG|S_IRUGO|S_IWUSR,
468 				    fnic_trace_debugfs_root,
469 				    &(fc_trc_flag->fc_row_file),
470 				    &fnic_trace_debugfs_fops);
471 
472 	if (!fnic_fc_trace_debugfs_file) {
473 		pr_err("fnic: Failed to create fc_trace file\n");
474 		return rc;
475 	}
476 	rc = 0;
477 	return rc;
478 }
479 
480 /*
481  * fnic_fc_trace_debugfs_terminate - Tear down debugfs infrastructure
482  *
483  * Description:
484  * When Debugfs is configured this routine removes debugfs file system
485  * elements that are specific to fnic_fc trace logging.
486  */
487 
488 void fnic_fc_trace_debugfs_terminate(void)
489 {
490 	debugfs_remove(fnic_fc_trace_debugfs_file);
491 	fnic_fc_trace_debugfs_file = NULL;
492 
493 	debugfs_remove(fnic_fc_rdata_trace_debugfs_file);
494 	fnic_fc_rdata_trace_debugfs_file = NULL;
495 
496 	debugfs_remove(fnic_fc_trace_enable);
497 	fnic_fc_trace_enable = NULL;
498 
499 	debugfs_remove(fnic_fc_trace_clear);
500 	fnic_fc_trace_clear = NULL;
501 }
502 
503 /*
504  * fnic_reset_stats_open - Open the reset_stats file
505  * @inode: The inode pointer.
506  * @file: The file pointer to attach the stats reset flag.
507  *
508  * Description:
509  * This routine opens a debugsfs file reset_stats and stores i_private data
510  * to debug structure to retrieve later for while performing other
511  * file oprations.
512  *
513  * Returns:
514  * This function returns zero if successful.
515  */
516 static int fnic_reset_stats_open(struct inode *inode, struct file *file)
517 {
518 	struct stats_debug_info *debug;
519 
520 	debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
521 	if (!debug)
522 		return -ENOMEM;
523 
524 	debug->i_private = inode->i_private;
525 
526 	file->private_data = debug;
527 
528 	return 0;
529 }
530 
531 /*
532  * fnic_reset_stats_read - Read a reset_stats debugfs file
533  * @filp: The file pointer to read from.
534  * @ubuf: The buffer to copy the data to.
535  * @cnt: The number of bytes to read.
536  * @ppos: The position in the file to start reading from.
537  *
538  * Description:
539  * This routine reads value of variable reset_stats
540  * and stores into local @buf. It will start reading file at @ppos and
541  * copy up to @cnt of data to @ubuf from @buf.
542  *
543  * Returns:
544  * This function returns the amount of data that was read.
545  */
546 static ssize_t fnic_reset_stats_read(struct file *file,
547 					char __user *ubuf,
548 					size_t cnt, loff_t *ppos)
549 {
550 	struct stats_debug_info *debug = file->private_data;
551 	struct fnic *fnic = (struct fnic *)debug->i_private;
552 	char buf[64];
553 	int len;
554 
555 	len = sprintf(buf, "%u\n", fnic->reset_stats);
556 
557 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
558 }
559 
560 /*
561  * fnic_reset_stats_write - Write to reset_stats debugfs file
562  * @filp: The file pointer to write from.
563  * @ubuf: The buffer to copy the data from.
564  * @cnt: The number of bytes to write.
565  * @ppos: The position in the file to start writing to.
566  *
567  * Description:
568  * This routine writes data from user buffer @ubuf to buffer @buf and
569  * resets cumulative stats of fnic.
570  *
571  * Returns:
572  * This function returns the amount of data that was written.
573  */
574 static ssize_t fnic_reset_stats_write(struct file *file,
575 					const char __user *ubuf,
576 					size_t cnt, loff_t *ppos)
577 {
578 	struct stats_debug_info *debug = file->private_data;
579 	struct fnic *fnic = (struct fnic *)debug->i_private;
580 	struct fnic_stats *stats = &fnic->fnic_stats;
581 	u64 *io_stats_p = (u64 *)&stats->io_stats;
582 	u64 *fw_stats_p = (u64 *)&stats->fw_stats;
583 	char buf[64];
584 	unsigned long val;
585 	int ret;
586 
587 	if (cnt >= sizeof(buf))
588 		return -EINVAL;
589 
590 	if (copy_from_user(&buf, ubuf, cnt))
591 		return -EFAULT;
592 
593 	buf[cnt] = 0;
594 
595 	ret = kstrtoul(buf, 10, &val);
596 	if (ret < 0)
597 		return ret;
598 
599 	fnic->reset_stats = val;
600 
601 	if (fnic->reset_stats) {
602 		/* Skip variable is used to avoid descrepancies to Num IOs
603 		 * and IO Completions stats. Skip incrementing No IO Compls
604 		 * for pending active IOs after reset stats
605 		 */
606 		atomic64_set(&fnic->io_cmpl_skip,
607 			atomic64_read(&stats->io_stats.active_ios));
608 		memset(&stats->abts_stats, 0, sizeof(struct abort_stats));
609 		memset(&stats->term_stats, 0,
610 			sizeof(struct terminate_stats));
611 		memset(&stats->reset_stats, 0, sizeof(struct reset_stats));
612 		memset(&stats->misc_stats, 0, sizeof(struct misc_stats));
613 		memset(&stats->vlan_stats, 0, sizeof(struct vlan_stats));
614 		memset(io_stats_p+1, 0,
615 			sizeof(struct io_path_stats) - sizeof(u64));
616 		memset(fw_stats_p+1, 0,
617 			sizeof(struct fw_stats) - sizeof(u64));
618 		ktime_get_real_ts64(&stats->stats_timestamps.last_reset_time);
619 	}
620 
621 	(*ppos)++;
622 	return cnt;
623 }
624 
625 /*
626  * fnic_reset_stats_release - Release the buffer used to store
627  * debugfs file data
628  * @inode: The inode pointer
629  * @file: The file pointer that contains the buffer to release
630  *
631  * Description:
632  * This routine frees the buffer that was allocated when the debugfs
633  * file was opened.
634  *
635  * Returns:
636  * This function returns zero.
637  */
638 static int fnic_reset_stats_release(struct inode *inode,
639 					struct file *file)
640 {
641 	struct stats_debug_info *debug = file->private_data;
642 	kfree(debug);
643 	return 0;
644 }
645 
646 /*
647  * fnic_stats_debugfs_open - Open the stats file for specific host
648  * and get fnic stats.
649  * @inode: The inode pointer.
650  * @file: The file pointer to attach the specific host statistics.
651  *
652  * Description:
653  * This routine opens a debugsfs file stats of specific host and print
654  * fnic stats.
655  *
656  * Returns:
657  * This function returns zero if successful.
658  */
659 static int fnic_stats_debugfs_open(struct inode *inode,
660 					struct file *file)
661 {
662 	struct fnic *fnic = inode->i_private;
663 	struct fnic_stats *fnic_stats = &fnic->fnic_stats;
664 	struct stats_debug_info *debug;
665 	int buf_size = 2 * PAGE_SIZE;
666 
667 	debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
668 	if (!debug)
669 		return -ENOMEM;
670 
671 	debug->debug_buffer = vmalloc(buf_size);
672 	if (!debug->debug_buffer) {
673 		kfree(debug);
674 		return -ENOMEM;
675 	}
676 
677 	debug->buf_size = buf_size;
678 	memset((void *)debug->debug_buffer, 0, buf_size);
679 	debug->buffer_len = fnic_get_stats_data(debug, fnic_stats);
680 
681 	file->private_data = debug;
682 
683 	return 0;
684 }
685 
686 /*
687  * fnic_stats_debugfs_read - Read a debugfs file
688  * @file: The file pointer to read from.
689  * @ubuf: The buffer to copy the data to.
690  * @nbytes: The number of bytes to read.
691  * @pos: The position in the file to start reading from.
692  *
693  * Description:
694  * This routine reads data from the buffer indicated in the private_data
695  * field of @file. It will start reading at @pos and copy up to @nbytes of
696  * data to @ubuf.
697  *
698  * Returns:
699  * This function returns the amount of data that was read (this could be
700  * less than @nbytes if the end of the file was reached).
701  */
702 static ssize_t fnic_stats_debugfs_read(struct file *file,
703 					char __user *ubuf,
704 					size_t nbytes,
705 					loff_t *pos)
706 {
707 	struct stats_debug_info *debug = file->private_data;
708 	int rc = 0;
709 	rc = simple_read_from_buffer(ubuf, nbytes, pos,
710 					debug->debug_buffer,
711 					debug->buffer_len);
712 	return rc;
713 }
714 
715 /*
716  * fnic_stats_stats_release - Release the buffer used to store
717  * debugfs file data
718  * @inode: The inode pointer
719  * @file: The file pointer that contains the buffer to release
720  *
721  * Description:
722  * This routine frees the buffer that was allocated when the debugfs
723  * file was opened.
724  *
725  * Returns:
726  * This function returns zero.
727  */
728 static int fnic_stats_debugfs_release(struct inode *inode,
729 					struct file *file)
730 {
731 	struct stats_debug_info *debug = file->private_data;
732 	vfree(debug->debug_buffer);
733 	kfree(debug);
734 	return 0;
735 }
736 
737 static const struct file_operations fnic_stats_debugfs_fops = {
738 	.owner = THIS_MODULE,
739 	.open = fnic_stats_debugfs_open,
740 	.read = fnic_stats_debugfs_read,
741 	.release = fnic_stats_debugfs_release,
742 };
743 
744 static const struct file_operations fnic_reset_debugfs_fops = {
745 	.owner = THIS_MODULE,
746 	.open = fnic_reset_stats_open,
747 	.read = fnic_reset_stats_read,
748 	.write = fnic_reset_stats_write,
749 	.release = fnic_reset_stats_release,
750 };
751 
752 /*
753  * fnic_stats_init - Initialize stats struct and create stats file per fnic
754  *
755  * Description:
756  * When Debugfs is configured this routine sets up the stats file per fnic
757  * It will create file stats and reset_stats under statistics/host# directory
758  * to log per fnic stats.
759  */
760 int fnic_stats_debugfs_init(struct fnic *fnic)
761 {
762 	int rc = -1;
763 	char name[16];
764 
765 	snprintf(name, sizeof(name), "host%d", fnic->lport->host->host_no);
766 
767 	if (!fnic_stats_debugfs_root) {
768 		printk(KERN_DEBUG "fnic_stats root doesn't exist\n");
769 		return rc;
770 	}
771 	fnic->fnic_stats_debugfs_host = debugfs_create_dir(name,
772 						fnic_stats_debugfs_root);
773 	if (!fnic->fnic_stats_debugfs_host) {
774 		printk(KERN_DEBUG "Cannot create host directory\n");
775 		return rc;
776 	}
777 
778 	fnic->fnic_stats_debugfs_file = debugfs_create_file("stats",
779 						S_IFREG|S_IRUGO|S_IWUSR,
780 						fnic->fnic_stats_debugfs_host,
781 						fnic,
782 						&fnic_stats_debugfs_fops);
783 	if (!fnic->fnic_stats_debugfs_file) {
784 		printk(KERN_DEBUG "Cannot create host stats file\n");
785 		return rc;
786 	}
787 
788 	fnic->fnic_reset_debugfs_file = debugfs_create_file("reset_stats",
789 						S_IFREG|S_IRUGO|S_IWUSR,
790 						fnic->fnic_stats_debugfs_host,
791 						fnic,
792 						&fnic_reset_debugfs_fops);
793 	if (!fnic->fnic_reset_debugfs_file) {
794 		printk(KERN_DEBUG "Cannot create host stats file\n");
795 		return rc;
796 	}
797 	rc = 0;
798 	return rc;
799 }
800 
801 /*
802  * fnic_stats_debugfs_remove - Tear down debugfs infrastructure of stats
803  *
804  * Description:
805  * When Debugfs is configured this routine removes debugfs file system
806  * elements that are specific to fnic stats.
807  */
808 void fnic_stats_debugfs_remove(struct fnic *fnic)
809 {
810 	if (!fnic)
811 		return;
812 
813 	debugfs_remove(fnic->fnic_stats_debugfs_file);
814 	fnic->fnic_stats_debugfs_file = NULL;
815 
816 	debugfs_remove(fnic->fnic_reset_debugfs_file);
817 	fnic->fnic_reset_debugfs_file = NULL;
818 
819 	debugfs_remove(fnic->fnic_stats_debugfs_host);
820 	fnic->fnic_stats_debugfs_host = NULL;
821 }
822