xref: /openbmc/linux/drivers/scsi/fnic/fnic_debugfs.c (revision 43caa03fec79d062c5f7a959a823770d72717b24)
14d7007b4SHiral Patel /*
24d7007b4SHiral Patel  * Copyright 2012 Cisco Systems, Inc.  All rights reserved.
34d7007b4SHiral Patel  *
44d7007b4SHiral Patel  * This program is free software; you may redistribute it and/or modify
54d7007b4SHiral Patel  * it under the terms of the GNU General Public License as published by
64d7007b4SHiral Patel  * the Free Software Foundation; version 2 of the License.
74d7007b4SHiral Patel  *
84d7007b4SHiral Patel  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
94d7007b4SHiral Patel  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
104d7007b4SHiral Patel  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
114d7007b4SHiral Patel  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
124d7007b4SHiral Patel  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
134d7007b4SHiral Patel  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
144d7007b4SHiral Patel  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
154d7007b4SHiral Patel  * SOFTWARE.
164d7007b4SHiral Patel  */
174d7007b4SHiral Patel 
184d7007b4SHiral Patel #include <linux/module.h>
194d7007b4SHiral Patel #include <linux/errno.h>
204d7007b4SHiral Patel #include <linux/debugfs.h>
21d6472302SStephen Rothwell #include <linux/vmalloc.h>
224d7007b4SHiral Patel #include "fnic.h"
234d7007b4SHiral Patel 
244d7007b4SHiral Patel static struct dentry *fnic_trace_debugfs_root;
254d7007b4SHiral Patel static struct dentry *fnic_trace_debugfs_file;
264d7007b4SHiral Patel static struct dentry *fnic_trace_enable;
2767125b02SHiral Patel static struct dentry *fnic_stats_debugfs_root;
2867125b02SHiral Patel 
29abb14148SHiral Shah static struct dentry *fnic_fc_trace_debugfs_file;
30abb14148SHiral Shah static struct dentry *fnic_fc_rdata_trace_debugfs_file;
31abb14148SHiral Shah static struct dentry *fnic_fc_trace_enable;
32abb14148SHiral Shah static struct dentry *fnic_fc_trace_clear;
33abb14148SHiral Shah 
34abb14148SHiral Shah struct fc_trace_flag_type {
35abb14148SHiral Shah 	u8 fc_row_file;
36abb14148SHiral Shah 	u8 fc_normal_file;
37abb14148SHiral Shah 	u8 fnic_trace;
38abb14148SHiral Shah 	u8 fc_trace;
39abb14148SHiral Shah 	u8 fc_clear;
40abb14148SHiral Shah };
41abb14148SHiral Shah 
42abb14148SHiral Shah static struct fc_trace_flag_type *fc_trc_flag;
43abb14148SHiral Shah 
4467125b02SHiral Patel /*
4567125b02SHiral Patel  * fnic_debugfs_init - Initialize debugfs for fnic debug logging
4667125b02SHiral Patel  *
4767125b02SHiral Patel  * Description:
4867125b02SHiral Patel  * When Debugfs is configured this routine sets up the fnic debugfs
4967125b02SHiral Patel  * file system. If not already created, this routine will create the
5067125b02SHiral Patel  * fnic directory and statistics directory for trace buffer and
5167125b02SHiral Patel  * stats logging.
5267125b02SHiral Patel  */
5367125b02SHiral Patel int fnic_debugfs_init(void)
5467125b02SHiral Patel {
5567125b02SHiral Patel 	int rc = -1;
5667125b02SHiral Patel 	fnic_trace_debugfs_root = debugfs_create_dir("fnic", NULL);
5767125b02SHiral Patel 	if (!fnic_trace_debugfs_root) {
5867125b02SHiral Patel 		printk(KERN_DEBUG "Cannot create debugfs root\n");
5967125b02SHiral Patel 		return rc;
6067125b02SHiral Patel 	}
6167125b02SHiral Patel 
6267125b02SHiral Patel 	if (!fnic_trace_debugfs_root) {
6367125b02SHiral Patel 		printk(KERN_DEBUG
6467125b02SHiral Patel 			"fnic root directory doesn't exist in debugfs\n");
6567125b02SHiral Patel 		return rc;
6667125b02SHiral Patel 	}
6767125b02SHiral Patel 
6867125b02SHiral Patel 	fnic_stats_debugfs_root = debugfs_create_dir("statistics",
6967125b02SHiral Patel 						fnic_trace_debugfs_root);
7067125b02SHiral Patel 	if (!fnic_stats_debugfs_root) {
7167125b02SHiral Patel 		printk(KERN_DEBUG "Cannot create Statistics directory\n");
7267125b02SHiral Patel 		return rc;
7367125b02SHiral Patel 	}
7467125b02SHiral Patel 
75abb14148SHiral Shah 	/* Allocate memory to structure */
76abb14148SHiral Shah 	fc_trc_flag = (struct fc_trace_flag_type *)
77abb14148SHiral Shah 		vmalloc(sizeof(struct fc_trace_flag_type));
78abb14148SHiral Shah 
79abb14148SHiral Shah 	if (fc_trc_flag) {
80abb14148SHiral Shah 		fc_trc_flag->fc_row_file = 0;
81abb14148SHiral Shah 		fc_trc_flag->fc_normal_file = 1;
82abb14148SHiral Shah 		fc_trc_flag->fnic_trace = 2;
83abb14148SHiral Shah 		fc_trc_flag->fc_trace = 3;
84abb14148SHiral Shah 		fc_trc_flag->fc_clear = 4;
85abb14148SHiral Shah 	}
86abb14148SHiral Shah 
8767125b02SHiral Patel 	rc = 0;
8867125b02SHiral Patel 	return rc;
8967125b02SHiral Patel }
9067125b02SHiral Patel 
9167125b02SHiral Patel /*
9267125b02SHiral Patel  * fnic_debugfs_terminate - Tear down debugfs infrastructure
9367125b02SHiral Patel  *
9467125b02SHiral Patel  * Description:
9567125b02SHiral Patel  * When Debugfs is configured this routine removes debugfs file system
9667125b02SHiral Patel  * elements that are specific to fnic.
9767125b02SHiral Patel  */
9867125b02SHiral Patel void fnic_debugfs_terminate(void)
9967125b02SHiral Patel {
10067125b02SHiral Patel 	debugfs_remove(fnic_stats_debugfs_root);
10167125b02SHiral Patel 	fnic_stats_debugfs_root = NULL;
10267125b02SHiral Patel 
10367125b02SHiral Patel 	debugfs_remove(fnic_trace_debugfs_root);
10467125b02SHiral Patel 	fnic_trace_debugfs_root = NULL;
105abb14148SHiral Shah 
106abb14148SHiral Shah 	if (fc_trc_flag)
107abb14148SHiral Shah 		vfree(fc_trc_flag);
10867125b02SHiral Patel }
1094d7007b4SHiral Patel 
1104d7007b4SHiral Patel /*
111abb14148SHiral Shah  * fnic_trace_ctrl_open - Open the trace_enable file for fnic_trace
112abb14148SHiral Shah  *               Or Open fc_trace_enable file for fc_trace
1134d7007b4SHiral Patel  * @inode: The inode pointer.
1144d7007b4SHiral Patel  * @file: The file pointer to attach the trace enable/disable flag.
1154d7007b4SHiral Patel  *
1164d7007b4SHiral Patel  * Description:
117abb14148SHiral Shah  * This routine opens a debugsfs file trace_enable or fc_trace_enable.
1184d7007b4SHiral Patel  *
1194d7007b4SHiral Patel  * Returns:
1204d7007b4SHiral Patel  * This function returns zero if successful.
1214d7007b4SHiral Patel  */
1224d7007b4SHiral Patel static int fnic_trace_ctrl_open(struct inode *inode, struct file *filp)
1234d7007b4SHiral Patel {
1244d7007b4SHiral Patel 	filp->private_data = inode->i_private;
1254d7007b4SHiral Patel 	return 0;
1264d7007b4SHiral Patel }
1274d7007b4SHiral Patel 
1284d7007b4SHiral Patel /*
129abb14148SHiral Shah  * fnic_trace_ctrl_read -
130abb14148SHiral Shah  *          Read  trace_enable ,fc_trace_enable
131abb14148SHiral Shah  *              or fc_trace_clear debugfs file
1324d7007b4SHiral Patel  * @filp: The file pointer to read from.
1334d7007b4SHiral Patel  * @ubuf: The buffer to copy the data to.
1344d7007b4SHiral Patel  * @cnt: The number of bytes to read.
1354d7007b4SHiral Patel  * @ppos: The position in the file to start reading from.
1364d7007b4SHiral Patel  *
1374d7007b4SHiral Patel  * Description:
138abb14148SHiral Shah  * This routine reads value of variable fnic_tracing_enabled or
139abb14148SHiral Shah  * fnic_fc_tracing_enabled or fnic_fc_trace_cleared
140abb14148SHiral Shah  * and stores into local @buf.
141abb14148SHiral Shah  * It will start reading file at @ppos and
1424d7007b4SHiral Patel  * copy up to @cnt of data to @ubuf from @buf.
1434d7007b4SHiral Patel  *
1444d7007b4SHiral Patel  * Returns:
1454d7007b4SHiral Patel  * This function returns the amount of data that was read.
1464d7007b4SHiral Patel  */
1474d7007b4SHiral Patel static ssize_t fnic_trace_ctrl_read(struct file *filp,
1484d7007b4SHiral Patel 				  char __user *ubuf,
1494d7007b4SHiral Patel 				  size_t cnt, loff_t *ppos)
1504d7007b4SHiral Patel {
1514d7007b4SHiral Patel 	char buf[64];
1524d7007b4SHiral Patel 	int len;
153abb14148SHiral Shah 	u8 *trace_type;
154abb14148SHiral Shah 	len = 0;
155abb14148SHiral Shah 	trace_type = (u8 *)filp->private_data;
156abb14148SHiral Shah 	if (*trace_type == fc_trc_flag->fnic_trace)
1574d7007b4SHiral Patel 		len = sprintf(buf, "%u\n", fnic_tracing_enabled);
158abb14148SHiral Shah 	else if (*trace_type == fc_trc_flag->fc_trace)
159abb14148SHiral Shah 		len = sprintf(buf, "%u\n", fnic_fc_tracing_enabled);
160abb14148SHiral Shah 	else if (*trace_type == fc_trc_flag->fc_clear)
161abb14148SHiral Shah 		len = sprintf(buf, "%u\n", fnic_fc_trace_cleared);
162abb14148SHiral Shah 	else
163abb14148SHiral Shah 		pr_err("fnic: Cannot read to any debugfs file\n");
1644d7007b4SHiral Patel 
1654d7007b4SHiral Patel 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1664d7007b4SHiral Patel }
1674d7007b4SHiral Patel 
1684d7007b4SHiral Patel /*
169abb14148SHiral Shah  * fnic_trace_ctrl_write -
170abb14148SHiral Shah  * Write to trace_enable, fc_trace_enable or
171abb14148SHiral Shah  *         fc_trace_clear debugfs file
1724d7007b4SHiral Patel  * @filp: The file pointer to write from.
1734d7007b4SHiral Patel  * @ubuf: The buffer to copy the data from.
1744d7007b4SHiral Patel  * @cnt: The number of bytes to write.
1754d7007b4SHiral Patel  * @ppos: The position in the file to start writing to.
1764d7007b4SHiral Patel  *
1774d7007b4SHiral Patel  * Description:
1784d7007b4SHiral Patel  * This routine writes data from user buffer @ubuf to buffer @buf and
179abb14148SHiral Shah  * sets fc_trace_enable ,tracing_enable or fnic_fc_trace_cleared
180abb14148SHiral Shah  * value as per user input.
1814d7007b4SHiral Patel  *
1824d7007b4SHiral Patel  * Returns:
1834d7007b4SHiral Patel  * This function returns the amount of data that was written.
1844d7007b4SHiral Patel  */
1854d7007b4SHiral Patel static ssize_t fnic_trace_ctrl_write(struct file *filp,
1864d7007b4SHiral Patel 				  const char __user *ubuf,
1874d7007b4SHiral Patel 				  size_t cnt, loff_t *ppos)
1884d7007b4SHiral Patel {
1894d7007b4SHiral Patel 	char buf[64];
1904d7007b4SHiral Patel 	unsigned long val;
1914d7007b4SHiral Patel 	int ret;
192abb14148SHiral Shah 	u8 *trace_type;
193abb14148SHiral Shah 	trace_type = (u8 *)filp->private_data;
1944d7007b4SHiral Patel 
1954d7007b4SHiral Patel 	if (cnt >= sizeof(buf))
1964d7007b4SHiral Patel 		return -EINVAL;
1974d7007b4SHiral Patel 
1984d7007b4SHiral Patel 	if (copy_from_user(&buf, ubuf, cnt))
1994d7007b4SHiral Patel 		return -EFAULT;
2004d7007b4SHiral Patel 
2014d7007b4SHiral Patel 	buf[cnt] = 0;
2024d7007b4SHiral Patel 
2034d7007b4SHiral Patel 	ret = kstrtoul(buf, 10, &val);
2044d7007b4SHiral Patel 	if (ret < 0)
2054d7007b4SHiral Patel 		return ret;
2064d7007b4SHiral Patel 
207abb14148SHiral Shah 	if (*trace_type == fc_trc_flag->fnic_trace)
2084d7007b4SHiral Patel 		fnic_tracing_enabled = val;
209abb14148SHiral Shah 	else if (*trace_type == fc_trc_flag->fc_trace)
210abb14148SHiral Shah 		fnic_fc_tracing_enabled = val;
211abb14148SHiral Shah 	else if (*trace_type == fc_trc_flag->fc_clear)
212abb14148SHiral Shah 		fnic_fc_trace_cleared = val;
213abb14148SHiral Shah 	else
2141a84db56SMasanari Iida 		pr_err("fnic: cannot write to any debugfs file\n");
215abb14148SHiral Shah 
2164d7007b4SHiral Patel 	(*ppos)++;
2174d7007b4SHiral Patel 
2184d7007b4SHiral Patel 	return cnt;
2194d7007b4SHiral Patel }
2204d7007b4SHiral Patel 
221abb14148SHiral Shah static const struct file_operations fnic_trace_ctrl_fops = {
222abb14148SHiral Shah 	.owner = THIS_MODULE,
223abb14148SHiral Shah 	.open = fnic_trace_ctrl_open,
224abb14148SHiral Shah 	.read = fnic_trace_ctrl_read,
225abb14148SHiral Shah 	.write = fnic_trace_ctrl_write,
226abb14148SHiral Shah };
227abb14148SHiral Shah 
2284d7007b4SHiral Patel /*
2294d7007b4SHiral Patel  * fnic_trace_debugfs_open - Open the fnic trace log
2304d7007b4SHiral Patel  * @inode: The inode pointer
2314d7007b4SHiral Patel  * @file: The file pointer to attach the log output
2324d7007b4SHiral Patel  *
2334d7007b4SHiral Patel  * Description:
2344d7007b4SHiral Patel  * This routine is the entry point for the debugfs open file operation.
2354d7007b4SHiral Patel  * It allocates the necessary buffer for the log, fills the buffer from
2364d7007b4SHiral Patel  * the in-memory log and then returns a pointer to that log in
2374d7007b4SHiral Patel  * the private_data field in @file.
2384d7007b4SHiral Patel  *
2394d7007b4SHiral Patel  * Returns:
2404d7007b4SHiral Patel  * This function returns zero if successful. On error it will return
2414d7007b4SHiral Patel  * a negative error value.
2424d7007b4SHiral Patel  */
2434d7007b4SHiral Patel static int fnic_trace_debugfs_open(struct inode *inode,
2444d7007b4SHiral Patel 				  struct file *file)
2454d7007b4SHiral Patel {
2464d7007b4SHiral Patel 	fnic_dbgfs_t *fnic_dbg_prt;
247abb14148SHiral Shah 	u8 *rdata_ptr;
248abb14148SHiral Shah 	rdata_ptr = (u8 *)inode->i_private;
2494d7007b4SHiral Patel 	fnic_dbg_prt = kzalloc(sizeof(fnic_dbgfs_t), GFP_KERNEL);
2504d7007b4SHiral Patel 	if (!fnic_dbg_prt)
2514d7007b4SHiral Patel 		return -ENOMEM;
2524d7007b4SHiral Patel 
253abb14148SHiral Shah 	if (*rdata_ptr == fc_trc_flag->fnic_trace) {
254abb14148SHiral Shah 		fnic_dbg_prt->buffer = vmalloc(3 *
255abb14148SHiral Shah 					(trace_max_pages * PAGE_SIZE));
2564d7007b4SHiral Patel 		if (!fnic_dbg_prt->buffer) {
2574d7007b4SHiral Patel 			kfree(fnic_dbg_prt);
2584d7007b4SHiral Patel 			return -ENOMEM;
2594d7007b4SHiral Patel 		}
2604d7007b4SHiral Patel 		memset((void *)fnic_dbg_prt->buffer, 0,
261abb14148SHiral Shah 		3 * (trace_max_pages * PAGE_SIZE));
2624d7007b4SHiral Patel 		fnic_dbg_prt->buffer_len = fnic_get_trace_data(fnic_dbg_prt);
263abb14148SHiral Shah 	} else {
264abb14148SHiral Shah 		fnic_dbg_prt->buffer =
265abb14148SHiral Shah 			vmalloc(3 * (fnic_fc_trace_max_pages * PAGE_SIZE));
266abb14148SHiral Shah 		if (!fnic_dbg_prt->buffer) {
267abb14148SHiral Shah 			kfree(fnic_dbg_prt);
268abb14148SHiral Shah 			return -ENOMEM;
269abb14148SHiral Shah 		}
270abb14148SHiral Shah 		memset((void *)fnic_dbg_prt->buffer, 0,
271abb14148SHiral Shah 			3 * (fnic_fc_trace_max_pages * PAGE_SIZE));
272abb14148SHiral Shah 		fnic_dbg_prt->buffer_len =
273abb14148SHiral Shah 			fnic_fc_trace_get_data(fnic_dbg_prt, *rdata_ptr);
274abb14148SHiral Shah 	}
2754d7007b4SHiral Patel 	file->private_data = fnic_dbg_prt;
276abb14148SHiral Shah 
2774d7007b4SHiral Patel 	return 0;
2784d7007b4SHiral Patel }
2794d7007b4SHiral Patel 
2804d7007b4SHiral Patel /*
2814d7007b4SHiral Patel  * fnic_trace_debugfs_lseek - Seek through a debugfs file
2824d7007b4SHiral Patel  * @file: The file pointer to seek through.
2834d7007b4SHiral Patel  * @offset: The offset to seek to or the amount to seek by.
2844d7007b4SHiral Patel  * @howto: Indicates how to seek.
2854d7007b4SHiral Patel  *
2864d7007b4SHiral Patel  * Description:
2874d7007b4SHiral Patel  * This routine is the entry point for the debugfs lseek file operation.
2884d7007b4SHiral Patel  * The @howto parameter indicates whether @offset is the offset to directly
2894d7007b4SHiral Patel  * seek to, or if it is a value to seek forward or reverse by. This function
2904d7007b4SHiral Patel  * figures out what the new offset of the debugfs file will be and assigns
2914d7007b4SHiral Patel  * that value to the f_pos field of @file.
2924d7007b4SHiral Patel  *
2934d7007b4SHiral Patel  * Returns:
2944d7007b4SHiral Patel  * This function returns the new offset if successful and returns a negative
2954d7007b4SHiral Patel  * error if unable to process the seek.
2964d7007b4SHiral Patel  */
2974d7007b4SHiral Patel static loff_t fnic_trace_debugfs_lseek(struct file *file,
2984d7007b4SHiral Patel 					loff_t offset,
2994d7007b4SHiral Patel 					int howto)
3004d7007b4SHiral Patel {
3014d7007b4SHiral Patel 	fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
302eb5881d3SAl Viro 	return fixed_size_llseek(file, offset, howto,
303eb5881d3SAl Viro 				fnic_dbg_prt->buffer_len);
3044d7007b4SHiral Patel }
3054d7007b4SHiral Patel 
3064d7007b4SHiral Patel /*
3074d7007b4SHiral Patel  * fnic_trace_debugfs_read - Read a debugfs file
3084d7007b4SHiral Patel  * @file: The file pointer to read from.
3094d7007b4SHiral Patel  * @ubuf: The buffer to copy the data to.
3104d7007b4SHiral Patel  * @nbytes: The number of bytes to read.
3114d7007b4SHiral Patel  * @pos: The position in the file to start reading from.
3124d7007b4SHiral Patel  *
3134d7007b4SHiral Patel  * Description:
3144d7007b4SHiral Patel  * This routine reads data from the buffer indicated in the private_data
3154d7007b4SHiral Patel  * field of @file. It will start reading at @pos and copy up to @nbytes of
3164d7007b4SHiral Patel  * data to @ubuf.
3174d7007b4SHiral Patel  *
3184d7007b4SHiral Patel  * Returns:
3194d7007b4SHiral Patel  * This function returns the amount of data that was read (this could be
3204d7007b4SHiral Patel  * less than @nbytes if the end of the file was reached).
3214d7007b4SHiral Patel  */
3224d7007b4SHiral Patel static ssize_t fnic_trace_debugfs_read(struct file *file,
3234d7007b4SHiral Patel 					char __user *ubuf,
3244d7007b4SHiral Patel 					size_t nbytes,
3254d7007b4SHiral Patel 					loff_t *pos)
3264d7007b4SHiral Patel {
3274d7007b4SHiral Patel 	fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
3284d7007b4SHiral Patel 	int rc = 0;
3294d7007b4SHiral Patel 	rc = simple_read_from_buffer(ubuf, nbytes, pos,
3304d7007b4SHiral Patel 				  fnic_dbg_prt->buffer,
3314d7007b4SHiral Patel 				  fnic_dbg_prt->buffer_len);
3324d7007b4SHiral Patel 	return rc;
3334d7007b4SHiral Patel }
3344d7007b4SHiral Patel 
3354d7007b4SHiral Patel /*
3364d7007b4SHiral Patel  * fnic_trace_debugfs_release - Release the buffer used to store
3374d7007b4SHiral Patel  * debugfs file data
3384d7007b4SHiral Patel  * @inode: The inode pointer
3394d7007b4SHiral Patel  * @file: The file pointer that contains the buffer to release
3404d7007b4SHiral Patel  *
3414d7007b4SHiral Patel  * Description:
3424d7007b4SHiral Patel  * This routine frees the buffer that was allocated when the debugfs
3434d7007b4SHiral Patel  * file was opened.
3444d7007b4SHiral Patel  *
3454d7007b4SHiral Patel  * Returns:
3464d7007b4SHiral Patel  * This function returns zero.
3474d7007b4SHiral Patel  */
3484d7007b4SHiral Patel static int fnic_trace_debugfs_release(struct inode *inode,
3494d7007b4SHiral Patel 					  struct file *file)
3504d7007b4SHiral Patel {
3514d7007b4SHiral Patel 	fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
3524d7007b4SHiral Patel 
3534d7007b4SHiral Patel 	vfree(fnic_dbg_prt->buffer);
3544d7007b4SHiral Patel 	kfree(fnic_dbg_prt);
3554d7007b4SHiral Patel 	return 0;
3564d7007b4SHiral Patel }
3574d7007b4SHiral Patel 
3584d7007b4SHiral Patel static const struct file_operations fnic_trace_debugfs_fops = {
3594d7007b4SHiral Patel 	.owner = THIS_MODULE,
3604d7007b4SHiral Patel 	.open = fnic_trace_debugfs_open,
3614d7007b4SHiral Patel 	.llseek = fnic_trace_debugfs_lseek,
3624d7007b4SHiral Patel 	.read = fnic_trace_debugfs_read,
3634d7007b4SHiral Patel 	.release = fnic_trace_debugfs_release,
3644d7007b4SHiral Patel };
3654d7007b4SHiral Patel 
3664d7007b4SHiral Patel /*
3674d7007b4SHiral Patel  * fnic_trace_debugfs_init - Initialize debugfs for fnic trace logging
3684d7007b4SHiral Patel  *
3694d7007b4SHiral Patel  * Description:
3704d7007b4SHiral Patel  * When Debugfs is configured this routine sets up the fnic debugfs
3714d7007b4SHiral Patel  * file system. If not already created, this routine will create the
37267125b02SHiral Patel  * create file trace to log fnic trace buffer output into debugfs and
37367125b02SHiral Patel  * it will also create file trace_enable to control enable/disable of
37467125b02SHiral Patel  * trace logging into trace buffer.
3754d7007b4SHiral Patel  */
3764d7007b4SHiral Patel int fnic_trace_debugfs_init(void)
3774d7007b4SHiral Patel {
3784d7007b4SHiral Patel 	int rc = -1;
3794d7007b4SHiral Patel 	if (!fnic_trace_debugfs_root) {
38067125b02SHiral Patel 		printk(KERN_DEBUG
38167125b02SHiral Patel 			"FNIC Debugfs root directory doesn't exist\n");
3824d7007b4SHiral Patel 		return rc;
3834d7007b4SHiral Patel 	}
3844d7007b4SHiral Patel 	fnic_trace_enable = debugfs_create_file("tracing_enable",
3854d7007b4SHiral Patel 					S_IFREG|S_IRUGO|S_IWUSR,
3864d7007b4SHiral Patel 					fnic_trace_debugfs_root,
387abb14148SHiral Shah 					&(fc_trc_flag->fnic_trace),
388abb14148SHiral Shah 					&fnic_trace_ctrl_fops);
3894d7007b4SHiral Patel 
3904d7007b4SHiral Patel 	if (!fnic_trace_enable) {
39167125b02SHiral Patel 		printk(KERN_DEBUG
39267125b02SHiral Patel 			"Cannot create trace_enable file under debugfs\n");
3934d7007b4SHiral Patel 		return rc;
3944d7007b4SHiral Patel 	}
3954d7007b4SHiral Patel 
3964d7007b4SHiral Patel 	fnic_trace_debugfs_file = debugfs_create_file("trace",
3974d7007b4SHiral Patel 					S_IFREG|S_IRUGO|S_IWUSR,
3984d7007b4SHiral Patel 					fnic_trace_debugfs_root,
399abb14148SHiral Shah 					&(fc_trc_flag->fnic_trace),
4004d7007b4SHiral Patel 					&fnic_trace_debugfs_fops);
4014d7007b4SHiral Patel 
4024d7007b4SHiral Patel 	if (!fnic_trace_debugfs_file) {
40367125b02SHiral Patel 		printk(KERN_DEBUG
40467125b02SHiral Patel 			"Cannot create trace file under debugfs\n");
4054d7007b4SHiral Patel 		return rc;
4064d7007b4SHiral Patel 	}
4074d7007b4SHiral Patel 	rc = 0;
4084d7007b4SHiral Patel 	return rc;
4094d7007b4SHiral Patel }
4104d7007b4SHiral Patel 
4114d7007b4SHiral Patel /*
4124d7007b4SHiral Patel  * fnic_trace_debugfs_terminate - Tear down debugfs infrastructure
4134d7007b4SHiral Patel  *
4144d7007b4SHiral Patel  * Description:
4154d7007b4SHiral Patel  * When Debugfs is configured this routine removes debugfs file system
4164d7007b4SHiral Patel  * elements that are specific to fnic trace logging.
4174d7007b4SHiral Patel  */
4184d7007b4SHiral Patel void fnic_trace_debugfs_terminate(void)
4194d7007b4SHiral Patel {
4204d7007b4SHiral Patel 	debugfs_remove(fnic_trace_debugfs_file);
4214d7007b4SHiral Patel 	fnic_trace_debugfs_file = NULL;
422abb14148SHiral Shah 
4234d7007b4SHiral Patel 	debugfs_remove(fnic_trace_enable);
4244d7007b4SHiral Patel 	fnic_trace_enable = NULL;
4254d7007b4SHiral Patel }
426abb14148SHiral Shah 
427abb14148SHiral Shah /*
428abb14148SHiral Shah  * fnic_fc_trace_debugfs_init -
429abb14148SHiral Shah  * Initialize debugfs for fnic control frame trace logging
430abb14148SHiral Shah  *
431abb14148SHiral Shah  * Description:
432abb14148SHiral Shah  * When Debugfs is configured this routine sets up the fnic_fc debugfs
433abb14148SHiral Shah  * file system. If not already created, this routine will create the
434abb14148SHiral Shah  * create file trace to log fnic fc trace buffer output into debugfs and
435abb14148SHiral Shah  * it will also create file fc_trace_enable to control enable/disable of
436abb14148SHiral Shah  * trace logging into trace buffer.
437abb14148SHiral Shah  */
438abb14148SHiral Shah 
439abb14148SHiral Shah int fnic_fc_trace_debugfs_init(void)
440abb14148SHiral Shah {
441abb14148SHiral Shah 	int rc = -1;
442abb14148SHiral Shah 
443abb14148SHiral Shah 	if (!fnic_trace_debugfs_root) {
444abb14148SHiral Shah 		pr_err("fnic:Debugfs root directory doesn't exist\n");
445abb14148SHiral Shah 		return rc;
446abb14148SHiral Shah 	}
447abb14148SHiral Shah 
448abb14148SHiral Shah 	fnic_fc_trace_enable = debugfs_create_file("fc_trace_enable",
449abb14148SHiral Shah 					S_IFREG|S_IRUGO|S_IWUSR,
450abb14148SHiral Shah 					fnic_trace_debugfs_root,
451abb14148SHiral Shah 					&(fc_trc_flag->fc_trace),
452abb14148SHiral Shah 					&fnic_trace_ctrl_fops);
453abb14148SHiral Shah 
454abb14148SHiral Shah 	if (!fnic_fc_trace_enable) {
455abb14148SHiral Shah 		pr_err("fnic: Failed create fc_trace_enable file\n");
456abb14148SHiral Shah 		return rc;
457abb14148SHiral Shah 	}
458abb14148SHiral Shah 
459abb14148SHiral Shah 	fnic_fc_trace_clear = debugfs_create_file("fc_trace_clear",
460abb14148SHiral Shah 					S_IFREG|S_IRUGO|S_IWUSR,
461abb14148SHiral Shah 					fnic_trace_debugfs_root,
462abb14148SHiral Shah 					&(fc_trc_flag->fc_clear),
463abb14148SHiral Shah 					&fnic_trace_ctrl_fops);
464abb14148SHiral Shah 
465abb14148SHiral Shah 	if (!fnic_fc_trace_clear) {
466abb14148SHiral Shah 		pr_err("fnic: Failed to create fc_trace_enable file\n");
467abb14148SHiral Shah 		return rc;
468abb14148SHiral Shah 	}
469abb14148SHiral Shah 
470abb14148SHiral Shah 	fnic_fc_rdata_trace_debugfs_file =
471abb14148SHiral Shah 		debugfs_create_file("fc_trace_rdata",
472abb14148SHiral Shah 				    S_IFREG|S_IRUGO|S_IWUSR,
473abb14148SHiral Shah 				    fnic_trace_debugfs_root,
474abb14148SHiral Shah 				    &(fc_trc_flag->fc_normal_file),
475abb14148SHiral Shah 				    &fnic_trace_debugfs_fops);
476abb14148SHiral Shah 
477abb14148SHiral Shah 	if (!fnic_fc_rdata_trace_debugfs_file) {
478abb14148SHiral Shah 		pr_err("fnic: Failed create fc_rdata_trace file\n");
479abb14148SHiral Shah 		return rc;
480abb14148SHiral Shah 	}
481abb14148SHiral Shah 
482abb14148SHiral Shah 	fnic_fc_trace_debugfs_file =
483abb14148SHiral Shah 		debugfs_create_file("fc_trace",
484abb14148SHiral Shah 				    S_IFREG|S_IRUGO|S_IWUSR,
485abb14148SHiral Shah 				    fnic_trace_debugfs_root,
486abb14148SHiral Shah 				    &(fc_trc_flag->fc_row_file),
487abb14148SHiral Shah 				    &fnic_trace_debugfs_fops);
488abb14148SHiral Shah 
489abb14148SHiral Shah 	if (!fnic_fc_trace_debugfs_file) {
490abb14148SHiral Shah 		pr_err("fnic: Failed to create fc_trace file\n");
491abb14148SHiral Shah 		return rc;
492abb14148SHiral Shah 	}
493abb14148SHiral Shah 	rc = 0;
494abb14148SHiral Shah 	return rc;
495abb14148SHiral Shah }
496abb14148SHiral Shah 
497abb14148SHiral Shah /*
498abb14148SHiral Shah  * fnic_fc_trace_debugfs_terminate - Tear down debugfs infrastructure
499abb14148SHiral Shah  *
500abb14148SHiral Shah  * Description:
501abb14148SHiral Shah  * When Debugfs is configured this routine removes debugfs file system
502abb14148SHiral Shah  * elements that are specific to fnic_fc trace logging.
503abb14148SHiral Shah  */
504abb14148SHiral Shah 
505abb14148SHiral Shah void fnic_fc_trace_debugfs_terminate(void)
506abb14148SHiral Shah {
507abb14148SHiral Shah 	debugfs_remove(fnic_fc_trace_debugfs_file);
508abb14148SHiral Shah 	fnic_fc_trace_debugfs_file = NULL;
509abb14148SHiral Shah 
510abb14148SHiral Shah 	debugfs_remove(fnic_fc_rdata_trace_debugfs_file);
511abb14148SHiral Shah 	fnic_fc_rdata_trace_debugfs_file = NULL;
512abb14148SHiral Shah 
513abb14148SHiral Shah 	debugfs_remove(fnic_fc_trace_enable);
514abb14148SHiral Shah 	fnic_fc_trace_enable = NULL;
515abb14148SHiral Shah 
516abb14148SHiral Shah 	debugfs_remove(fnic_fc_trace_clear);
517abb14148SHiral Shah 	fnic_fc_trace_clear = NULL;
5184d7007b4SHiral Patel }
51967125b02SHiral Patel 
52067125b02SHiral Patel /*
52167125b02SHiral Patel  * fnic_reset_stats_open - Open the reset_stats file
52267125b02SHiral Patel  * @inode: The inode pointer.
52367125b02SHiral Patel  * @file: The file pointer to attach the stats reset flag.
52467125b02SHiral Patel  *
52567125b02SHiral Patel  * Description:
52667125b02SHiral Patel  * This routine opens a debugsfs file reset_stats and stores i_private data
52767125b02SHiral Patel  * to debug structure to retrieve later for while performing other
52867125b02SHiral Patel  * file oprations.
52967125b02SHiral Patel  *
53067125b02SHiral Patel  * Returns:
53167125b02SHiral Patel  * This function returns zero if successful.
53267125b02SHiral Patel  */
53367125b02SHiral Patel static int fnic_reset_stats_open(struct inode *inode, struct file *file)
53467125b02SHiral Patel {
53567125b02SHiral Patel 	struct stats_debug_info *debug;
53667125b02SHiral Patel 
53767125b02SHiral Patel 	debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
53867125b02SHiral Patel 	if (!debug)
53967125b02SHiral Patel 		return -ENOMEM;
54067125b02SHiral Patel 
54167125b02SHiral Patel 	debug->i_private = inode->i_private;
54267125b02SHiral Patel 
54367125b02SHiral Patel 	file->private_data = debug;
54467125b02SHiral Patel 
54567125b02SHiral Patel 	return 0;
54667125b02SHiral Patel }
54767125b02SHiral Patel 
54867125b02SHiral Patel /*
54967125b02SHiral Patel  * fnic_reset_stats_read - Read a reset_stats debugfs file
55067125b02SHiral Patel  * @filp: The file pointer to read from.
55167125b02SHiral Patel  * @ubuf: The buffer to copy the data to.
55267125b02SHiral Patel  * @cnt: The number of bytes to read.
55367125b02SHiral Patel  * @ppos: The position in the file to start reading from.
55467125b02SHiral Patel  *
55567125b02SHiral Patel  * Description:
55667125b02SHiral Patel  * This routine reads value of variable reset_stats
55767125b02SHiral Patel  * and stores into local @buf. It will start reading file at @ppos and
55867125b02SHiral Patel  * copy up to @cnt of data to @ubuf from @buf.
55967125b02SHiral Patel  *
56067125b02SHiral Patel  * Returns:
56167125b02SHiral Patel  * This function returns the amount of data that was read.
56267125b02SHiral Patel  */
56367125b02SHiral Patel static ssize_t fnic_reset_stats_read(struct file *file,
56467125b02SHiral Patel 					char __user *ubuf,
56567125b02SHiral Patel 					size_t cnt, loff_t *ppos)
56667125b02SHiral Patel {
56767125b02SHiral Patel 	struct stats_debug_info *debug = file->private_data;
56867125b02SHiral Patel 	struct fnic *fnic = (struct fnic *)debug->i_private;
56967125b02SHiral Patel 	char buf[64];
57067125b02SHiral Patel 	int len;
57167125b02SHiral Patel 
57267125b02SHiral Patel 	len = sprintf(buf, "%u\n", fnic->reset_stats);
57367125b02SHiral Patel 
57467125b02SHiral Patel 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
57567125b02SHiral Patel }
57667125b02SHiral Patel 
57767125b02SHiral Patel /*
57867125b02SHiral Patel  * fnic_reset_stats_write - Write to reset_stats debugfs file
57967125b02SHiral Patel  * @filp: The file pointer to write from.
58067125b02SHiral Patel  * @ubuf: The buffer to copy the data from.
58167125b02SHiral Patel  * @cnt: The number of bytes to write.
58267125b02SHiral Patel  * @ppos: The position in the file to start writing to.
58367125b02SHiral Patel  *
58467125b02SHiral Patel  * Description:
58567125b02SHiral Patel  * This routine writes data from user buffer @ubuf to buffer @buf and
58667125b02SHiral Patel  * resets cumulative stats of fnic.
58767125b02SHiral Patel  *
58867125b02SHiral Patel  * Returns:
58967125b02SHiral Patel  * This function returns the amount of data that was written.
59067125b02SHiral Patel  */
59167125b02SHiral Patel static ssize_t fnic_reset_stats_write(struct file *file,
59267125b02SHiral Patel 					const char __user *ubuf,
59367125b02SHiral Patel 					size_t cnt, loff_t *ppos)
59467125b02SHiral Patel {
59567125b02SHiral Patel 	struct stats_debug_info *debug = file->private_data;
59667125b02SHiral Patel 	struct fnic *fnic = (struct fnic *)debug->i_private;
59767125b02SHiral Patel 	struct fnic_stats *stats = &fnic->fnic_stats;
59867125b02SHiral Patel 	u64 *io_stats_p = (u64 *)&stats->io_stats;
59967125b02SHiral Patel 	u64 *fw_stats_p = (u64 *)&stats->fw_stats;
60067125b02SHiral Patel 	char buf[64];
60167125b02SHiral Patel 	unsigned long val;
60267125b02SHiral Patel 	int ret;
60367125b02SHiral Patel 
60467125b02SHiral Patel 	if (cnt >= sizeof(buf))
60567125b02SHiral Patel 		return -EINVAL;
60667125b02SHiral Patel 
60767125b02SHiral Patel 	if (copy_from_user(&buf, ubuf, cnt))
60867125b02SHiral Patel 		return -EFAULT;
60967125b02SHiral Patel 
61067125b02SHiral Patel 	buf[cnt] = 0;
61167125b02SHiral Patel 
61267125b02SHiral Patel 	ret = kstrtoul(buf, 10, &val);
61367125b02SHiral Patel 	if (ret < 0)
61467125b02SHiral Patel 		return ret;
61567125b02SHiral Patel 
61667125b02SHiral Patel 	fnic->reset_stats = val;
61767125b02SHiral Patel 
61867125b02SHiral Patel 	if (fnic->reset_stats) {
61967125b02SHiral Patel 		/* Skip variable is used to avoid descrepancies to Num IOs
62067125b02SHiral Patel 		 * and IO Completions stats. Skip incrementing No IO Compls
62167125b02SHiral Patel 		 * for pending active IOs after reset stats
62267125b02SHiral Patel 		 */
62367125b02SHiral Patel 		atomic64_set(&fnic->io_cmpl_skip,
62467125b02SHiral Patel 			atomic64_read(&stats->io_stats.active_ios));
62567125b02SHiral Patel 		memset(&stats->abts_stats, 0, sizeof(struct abort_stats));
62667125b02SHiral Patel 		memset(&stats->term_stats, 0,
62767125b02SHiral Patel 			sizeof(struct terminate_stats));
62867125b02SHiral Patel 		memset(&stats->reset_stats, 0, sizeof(struct reset_stats));
62967125b02SHiral Patel 		memset(&stats->misc_stats, 0, sizeof(struct misc_stats));
63067125b02SHiral Patel 		memset(&stats->vlan_stats, 0, sizeof(struct vlan_stats));
63167125b02SHiral Patel 		memset(io_stats_p+1, 0,
63267125b02SHiral Patel 			sizeof(struct io_path_stats) - sizeof(u64));
63367125b02SHiral Patel 		memset(fw_stats_p+1, 0,
63467125b02SHiral Patel 			sizeof(struct fw_stats) - sizeof(u64));
635*43caa03fSSatish Kharat 		getnstimeofday(&stats->stats_timestamps.last_reset_time);
63667125b02SHiral Patel 	}
63767125b02SHiral Patel 
63867125b02SHiral Patel 	(*ppos)++;
63967125b02SHiral Patel 	return cnt;
64067125b02SHiral Patel }
64167125b02SHiral Patel 
64267125b02SHiral Patel /*
64367125b02SHiral Patel  * fnic_reset_stats_release - Release the buffer used to store
64467125b02SHiral Patel  * debugfs file data
64567125b02SHiral Patel  * @inode: The inode pointer
64667125b02SHiral Patel  * @file: The file pointer that contains the buffer to release
64767125b02SHiral Patel  *
64867125b02SHiral Patel  * Description:
64967125b02SHiral Patel  * This routine frees the buffer that was allocated when the debugfs
65067125b02SHiral Patel  * file was opened.
65167125b02SHiral Patel  *
65267125b02SHiral Patel  * Returns:
65367125b02SHiral Patel  * This function returns zero.
65467125b02SHiral Patel  */
65567125b02SHiral Patel static int fnic_reset_stats_release(struct inode *inode,
65667125b02SHiral Patel 					struct file *file)
65767125b02SHiral Patel {
65867125b02SHiral Patel 	struct stats_debug_info *debug = file->private_data;
65967125b02SHiral Patel 	kfree(debug);
66067125b02SHiral Patel 	return 0;
66167125b02SHiral Patel }
66267125b02SHiral Patel 
66367125b02SHiral Patel /*
66467125b02SHiral Patel  * fnic_stats_debugfs_open - Open the stats file for specific host
66567125b02SHiral Patel  * and get fnic stats.
66667125b02SHiral Patel  * @inode: The inode pointer.
66767125b02SHiral Patel  * @file: The file pointer to attach the specific host statistics.
66867125b02SHiral Patel  *
66967125b02SHiral Patel  * Description:
67067125b02SHiral Patel  * This routine opens a debugsfs file stats of specific host and print
67167125b02SHiral Patel  * fnic stats.
67267125b02SHiral Patel  *
67367125b02SHiral Patel  * Returns:
67467125b02SHiral Patel  * This function returns zero if successful.
67567125b02SHiral Patel  */
67667125b02SHiral Patel static int fnic_stats_debugfs_open(struct inode *inode,
67767125b02SHiral Patel 					struct file *file)
67867125b02SHiral Patel {
67967125b02SHiral Patel 	struct fnic *fnic = inode->i_private;
68067125b02SHiral Patel 	struct fnic_stats *fnic_stats = &fnic->fnic_stats;
68167125b02SHiral Patel 	struct stats_debug_info *debug;
68267125b02SHiral Patel 	int buf_size = 2 * PAGE_SIZE;
68367125b02SHiral Patel 
68467125b02SHiral Patel 	debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
68567125b02SHiral Patel 	if (!debug)
68667125b02SHiral Patel 		return -ENOMEM;
68767125b02SHiral Patel 
68867125b02SHiral Patel 	debug->debug_buffer = vmalloc(buf_size);
68967125b02SHiral Patel 	if (!debug->debug_buffer) {
69067125b02SHiral Patel 		kfree(debug);
69167125b02SHiral Patel 		return -ENOMEM;
69267125b02SHiral Patel 	}
69367125b02SHiral Patel 
69467125b02SHiral Patel 	debug->buf_size = buf_size;
69567125b02SHiral Patel 	memset((void *)debug->debug_buffer, 0, buf_size);
69667125b02SHiral Patel 	debug->buffer_len = fnic_get_stats_data(debug, fnic_stats);
69767125b02SHiral Patel 
69867125b02SHiral Patel 	file->private_data = debug;
69967125b02SHiral Patel 
70067125b02SHiral Patel 	return 0;
70167125b02SHiral Patel }
70267125b02SHiral Patel 
70367125b02SHiral Patel /*
70467125b02SHiral Patel  * fnic_stats_debugfs_read - Read a debugfs file
70567125b02SHiral Patel  * @file: The file pointer to read from.
70667125b02SHiral Patel  * @ubuf: The buffer to copy the data to.
70767125b02SHiral Patel  * @nbytes: The number of bytes to read.
70867125b02SHiral Patel  * @pos: The position in the file to start reading from.
70967125b02SHiral Patel  *
71067125b02SHiral Patel  * Description:
71167125b02SHiral Patel  * This routine reads data from the buffer indicated in the private_data
71267125b02SHiral Patel  * field of @file. It will start reading at @pos and copy up to @nbytes of
71367125b02SHiral Patel  * data to @ubuf.
71467125b02SHiral Patel  *
71567125b02SHiral Patel  * Returns:
71667125b02SHiral Patel  * This function returns the amount of data that was read (this could be
71767125b02SHiral Patel  * less than @nbytes if the end of the file was reached).
71867125b02SHiral Patel  */
71967125b02SHiral Patel static ssize_t fnic_stats_debugfs_read(struct file *file,
72067125b02SHiral Patel 					char __user *ubuf,
72167125b02SHiral Patel 					size_t nbytes,
72267125b02SHiral Patel 					loff_t *pos)
72367125b02SHiral Patel {
72467125b02SHiral Patel 	struct stats_debug_info *debug = file->private_data;
72567125b02SHiral Patel 	int rc = 0;
72667125b02SHiral Patel 	rc = simple_read_from_buffer(ubuf, nbytes, pos,
72767125b02SHiral Patel 					debug->debug_buffer,
72867125b02SHiral Patel 					debug->buffer_len);
72967125b02SHiral Patel 	return rc;
73067125b02SHiral Patel }
73167125b02SHiral Patel 
73267125b02SHiral Patel /*
73367125b02SHiral Patel  * fnic_stats_stats_release - Release the buffer used to store
73467125b02SHiral Patel  * debugfs file data
73567125b02SHiral Patel  * @inode: The inode pointer
73667125b02SHiral Patel  * @file: The file pointer that contains the buffer to release
73767125b02SHiral Patel  *
73867125b02SHiral Patel  * Description:
73967125b02SHiral Patel  * This routine frees the buffer that was allocated when the debugfs
74067125b02SHiral Patel  * file was opened.
74167125b02SHiral Patel  *
74267125b02SHiral Patel  * Returns:
74367125b02SHiral Patel  * This function returns zero.
74467125b02SHiral Patel  */
74567125b02SHiral Patel static int fnic_stats_debugfs_release(struct inode *inode,
74667125b02SHiral Patel 					struct file *file)
74767125b02SHiral Patel {
74867125b02SHiral Patel 	struct stats_debug_info *debug = file->private_data;
74967125b02SHiral Patel 	vfree(debug->debug_buffer);
75067125b02SHiral Patel 	kfree(debug);
75167125b02SHiral Patel 	return 0;
75267125b02SHiral Patel }
75367125b02SHiral Patel 
75467125b02SHiral Patel static const struct file_operations fnic_stats_debugfs_fops = {
75567125b02SHiral Patel 	.owner = THIS_MODULE,
75667125b02SHiral Patel 	.open = fnic_stats_debugfs_open,
75767125b02SHiral Patel 	.read = fnic_stats_debugfs_read,
75867125b02SHiral Patel 	.release = fnic_stats_debugfs_release,
75967125b02SHiral Patel };
76067125b02SHiral Patel 
76167125b02SHiral Patel static const struct file_operations fnic_reset_debugfs_fops = {
76267125b02SHiral Patel 	.owner = THIS_MODULE,
76367125b02SHiral Patel 	.open = fnic_reset_stats_open,
76467125b02SHiral Patel 	.read = fnic_reset_stats_read,
76567125b02SHiral Patel 	.write = fnic_reset_stats_write,
76667125b02SHiral Patel 	.release = fnic_reset_stats_release,
76767125b02SHiral Patel };
76867125b02SHiral Patel 
76967125b02SHiral Patel /*
77067125b02SHiral Patel  * fnic_stats_init - Initialize stats struct and create stats file per fnic
77167125b02SHiral Patel  *
77267125b02SHiral Patel  * Description:
77367125b02SHiral Patel  * When Debugfs is configured this routine sets up the stats file per fnic
77467125b02SHiral Patel  * It will create file stats and reset_stats under statistics/host# directory
77567125b02SHiral Patel  * to log per fnic stats.
77667125b02SHiral Patel  */
77767125b02SHiral Patel int fnic_stats_debugfs_init(struct fnic *fnic)
77867125b02SHiral Patel {
77967125b02SHiral Patel 	int rc = -1;
78067125b02SHiral Patel 	char name[16];
78167125b02SHiral Patel 
78267125b02SHiral Patel 	snprintf(name, sizeof(name), "host%d", fnic->lport->host->host_no);
78367125b02SHiral Patel 
78467125b02SHiral Patel 	if (!fnic_stats_debugfs_root) {
78567125b02SHiral Patel 		printk(KERN_DEBUG "fnic_stats root doesn't exist\n");
78667125b02SHiral Patel 		return rc;
78767125b02SHiral Patel 	}
78867125b02SHiral Patel 	fnic->fnic_stats_debugfs_host = debugfs_create_dir(name,
78967125b02SHiral Patel 						fnic_stats_debugfs_root);
79067125b02SHiral Patel 	if (!fnic->fnic_stats_debugfs_host) {
79167125b02SHiral Patel 		printk(KERN_DEBUG "Cannot create host directory\n");
79267125b02SHiral Patel 		return rc;
79367125b02SHiral Patel 	}
79467125b02SHiral Patel 
79567125b02SHiral Patel 	fnic->fnic_stats_debugfs_file = debugfs_create_file("stats",
79667125b02SHiral Patel 						S_IFREG|S_IRUGO|S_IWUSR,
79767125b02SHiral Patel 						fnic->fnic_stats_debugfs_host,
79867125b02SHiral Patel 						fnic,
79967125b02SHiral Patel 						&fnic_stats_debugfs_fops);
80067125b02SHiral Patel 	if (!fnic->fnic_stats_debugfs_file) {
80167125b02SHiral Patel 		printk(KERN_DEBUG "Cannot create host stats file\n");
80267125b02SHiral Patel 		return rc;
80367125b02SHiral Patel 	}
80467125b02SHiral Patel 
80567125b02SHiral Patel 	fnic->fnic_reset_debugfs_file = debugfs_create_file("reset_stats",
80667125b02SHiral Patel 						S_IFREG|S_IRUGO|S_IWUSR,
80767125b02SHiral Patel 						fnic->fnic_stats_debugfs_host,
80867125b02SHiral Patel 						fnic,
80967125b02SHiral Patel 						&fnic_reset_debugfs_fops);
81067125b02SHiral Patel 	if (!fnic->fnic_reset_debugfs_file) {
81167125b02SHiral Patel 		printk(KERN_DEBUG "Cannot create host stats file\n");
81267125b02SHiral Patel 		return rc;
81367125b02SHiral Patel 	}
81467125b02SHiral Patel 	rc = 0;
81567125b02SHiral Patel 	return rc;
81667125b02SHiral Patel }
81767125b02SHiral Patel 
81867125b02SHiral Patel /*
81967125b02SHiral Patel  * fnic_stats_debugfs_remove - Tear down debugfs infrastructure of stats
82067125b02SHiral Patel  *
82167125b02SHiral Patel  * Description:
82267125b02SHiral Patel  * When Debugfs is configured this routine removes debugfs file system
82367125b02SHiral Patel  * elements that are specific to fnic stats.
82467125b02SHiral Patel  */
82567125b02SHiral Patel void fnic_stats_debugfs_remove(struct fnic *fnic)
82667125b02SHiral Patel {
82767125b02SHiral Patel 	if (!fnic)
82867125b02SHiral Patel 		return;
82967125b02SHiral Patel 
83067125b02SHiral Patel 	debugfs_remove(fnic->fnic_stats_debugfs_file);
83167125b02SHiral Patel 	fnic->fnic_stats_debugfs_file = NULL;
83267125b02SHiral Patel 
83367125b02SHiral Patel 	debugfs_remove(fnic->fnic_reset_debugfs_file);
83467125b02SHiral Patel 	fnic->fnic_reset_debugfs_file = NULL;
83567125b02SHiral Patel 
83667125b02SHiral Patel 	debugfs_remove(fnic->fnic_stats_debugfs_host);
83767125b02SHiral Patel 	fnic->fnic_stats_debugfs_host = NULL;
8384d7007b4SHiral Patel }
839