xref: /openbmc/linux/drivers/scsi/fnic/fnic_debugfs.c (revision 14ce2c261d6cce89023a1df770d83df859e89f7e)
1e6550b3eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e6550b3eSThomas Gleixner // Copyright 2012 Cisco Systems, Inc.  All rights reserved.
34d7007b4SHiral Patel 
44d7007b4SHiral Patel #include <linux/module.h>
54d7007b4SHiral Patel #include <linux/errno.h>
64d7007b4SHiral Patel #include <linux/debugfs.h>
7d6472302SStephen Rothwell #include <linux/vmalloc.h>
84d7007b4SHiral Patel #include "fnic.h"
94d7007b4SHiral Patel 
104d7007b4SHiral Patel static struct dentry *fnic_trace_debugfs_root;
114d7007b4SHiral Patel static struct dentry *fnic_trace_debugfs_file;
124d7007b4SHiral Patel static struct dentry *fnic_trace_enable;
1367125b02SHiral Patel static struct dentry *fnic_stats_debugfs_root;
1467125b02SHiral Patel 
15abb14148SHiral Shah static struct dentry *fnic_fc_trace_debugfs_file;
16abb14148SHiral Shah static struct dentry *fnic_fc_rdata_trace_debugfs_file;
17abb14148SHiral Shah static struct dentry *fnic_fc_trace_enable;
18abb14148SHiral Shah static struct dentry *fnic_fc_trace_clear;
19abb14148SHiral Shah 
20abb14148SHiral Shah struct fc_trace_flag_type {
21abb14148SHiral Shah 	u8 fc_row_file;
22abb14148SHiral Shah 	u8 fc_normal_file;
23abb14148SHiral Shah 	u8 fnic_trace;
24abb14148SHiral Shah 	u8 fc_trace;
25abb14148SHiral Shah 	u8 fc_clear;
26abb14148SHiral Shah };
27abb14148SHiral Shah 
28abb14148SHiral Shah static struct fc_trace_flag_type *fc_trc_flag;
29abb14148SHiral Shah 
3067125b02SHiral Patel /*
3167125b02SHiral Patel  * fnic_debugfs_init - Initialize debugfs for fnic debug logging
3267125b02SHiral Patel  *
3367125b02SHiral Patel  * Description:
3467125b02SHiral Patel  * When Debugfs is configured this routine sets up the fnic debugfs
3567125b02SHiral Patel  * file system. If not already created, this routine will create the
3667125b02SHiral Patel  * fnic directory and statistics directory for trace buffer and
3767125b02SHiral Patel  * stats logging.
3867125b02SHiral Patel  */
3967125b02SHiral Patel int fnic_debugfs_init(void)
4067125b02SHiral Patel {
4167125b02SHiral Patel 	fnic_trace_debugfs_root = debugfs_create_dir("fnic", NULL);
4267125b02SHiral Patel 
4367125b02SHiral Patel 	fnic_stats_debugfs_root = debugfs_create_dir("statistics",
4467125b02SHiral Patel 						fnic_trace_debugfs_root);
4567125b02SHiral Patel 
46abb14148SHiral Shah 	/* Allocate memory to structure */
473ba9f38eSWang Qing 	fc_trc_flag = vmalloc(sizeof(struct fc_trace_flag_type));
48abb14148SHiral Shah 
49abb14148SHiral Shah 	if (fc_trc_flag) {
50abb14148SHiral Shah 		fc_trc_flag->fc_row_file = 0;
51abb14148SHiral Shah 		fc_trc_flag->fc_normal_file = 1;
52abb14148SHiral Shah 		fc_trc_flag->fnic_trace = 2;
53abb14148SHiral Shah 		fc_trc_flag->fc_trace = 3;
54abb14148SHiral Shah 		fc_trc_flag->fc_clear = 4;
55abb14148SHiral Shah 	}
56abb14148SHiral Shah 
579730ddfbSColin Ian King 	return 0;
5867125b02SHiral Patel }
5967125b02SHiral Patel 
6067125b02SHiral Patel /*
6167125b02SHiral Patel  * fnic_debugfs_terminate - Tear down debugfs infrastructure
6267125b02SHiral Patel  *
6367125b02SHiral Patel  * Description:
6467125b02SHiral Patel  * When Debugfs is configured this routine removes debugfs file system
6567125b02SHiral Patel  * elements that are specific to fnic.
6667125b02SHiral Patel  */
6767125b02SHiral Patel void fnic_debugfs_terminate(void)
6867125b02SHiral Patel {
6967125b02SHiral Patel 	debugfs_remove(fnic_stats_debugfs_root);
7067125b02SHiral Patel 	fnic_stats_debugfs_root = NULL;
7167125b02SHiral Patel 
7267125b02SHiral Patel 	debugfs_remove(fnic_trace_debugfs_root);
7367125b02SHiral Patel 	fnic_trace_debugfs_root = NULL;
74abb14148SHiral Shah 
75abb14148SHiral Shah 	vfree(fc_trc_flag);
7667125b02SHiral Patel }
774d7007b4SHiral Patel 
784d7007b4SHiral Patel /*
79abb14148SHiral Shah  * fnic_trace_ctrl_read -
80abb14148SHiral Shah  *          Read  trace_enable ,fc_trace_enable
81abb14148SHiral Shah  *              or fc_trace_clear debugfs file
824d7007b4SHiral Patel  * @filp: The file pointer to read from.
834d7007b4SHiral Patel  * @ubuf: The buffer to copy the data to.
844d7007b4SHiral Patel  * @cnt: The number of bytes to read.
854d7007b4SHiral Patel  * @ppos: The position in the file to start reading from.
864d7007b4SHiral Patel  *
874d7007b4SHiral Patel  * Description:
88abb14148SHiral Shah  * This routine reads value of variable fnic_tracing_enabled or
89abb14148SHiral Shah  * fnic_fc_tracing_enabled or fnic_fc_trace_cleared
90abb14148SHiral Shah  * and stores into local @buf.
91abb14148SHiral Shah  * It will start reading file at @ppos and
924d7007b4SHiral Patel  * copy up to @cnt of data to @ubuf from @buf.
934d7007b4SHiral Patel  *
944d7007b4SHiral Patel  * Returns:
954d7007b4SHiral Patel  * This function returns the amount of data that was read.
964d7007b4SHiral Patel  */
974d7007b4SHiral Patel static ssize_t fnic_trace_ctrl_read(struct file *filp,
984d7007b4SHiral Patel 				  char __user *ubuf,
994d7007b4SHiral Patel 				  size_t cnt, loff_t *ppos)
1004d7007b4SHiral Patel {
1014d7007b4SHiral Patel 	char buf[64];
1024d7007b4SHiral Patel 	int len;
103abb14148SHiral Shah 	u8 *trace_type;
104abb14148SHiral Shah 	len = 0;
105abb14148SHiral Shah 	trace_type = (u8 *)filp->private_data;
106abb14148SHiral Shah 	if (*trace_type == fc_trc_flag->fnic_trace)
1071dfbed19SYe Bin 		len = sprintf(buf, "%d\n", fnic_tracing_enabled);
108abb14148SHiral Shah 	else if (*trace_type == fc_trc_flag->fc_trace)
1091dfbed19SYe Bin 		len = sprintf(buf, "%d\n", fnic_fc_tracing_enabled);
110abb14148SHiral Shah 	else if (*trace_type == fc_trc_flag->fc_clear)
1111dfbed19SYe Bin 		len = sprintf(buf, "%d\n", fnic_fc_trace_cleared);
112abb14148SHiral Shah 	else
113abb14148SHiral Shah 		pr_err("fnic: Cannot read to any debugfs file\n");
1144d7007b4SHiral Patel 
1154d7007b4SHiral Patel 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1164d7007b4SHiral Patel }
1174d7007b4SHiral Patel 
1184d7007b4SHiral Patel /*
119abb14148SHiral Shah  * fnic_trace_ctrl_write -
120abb14148SHiral Shah  * Write to trace_enable, fc_trace_enable or
121abb14148SHiral Shah  *         fc_trace_clear debugfs file
1224d7007b4SHiral Patel  * @filp: The file pointer to write from.
1234d7007b4SHiral Patel  * @ubuf: The buffer to copy the data from.
1244d7007b4SHiral Patel  * @cnt: The number of bytes to write.
1254d7007b4SHiral Patel  * @ppos: The position in the file to start writing to.
1264d7007b4SHiral Patel  *
1274d7007b4SHiral Patel  * Description:
1284d7007b4SHiral Patel  * This routine writes data from user buffer @ubuf to buffer @buf and
129abb14148SHiral Shah  * sets fc_trace_enable ,tracing_enable or fnic_fc_trace_cleared
130abb14148SHiral Shah  * value as per user input.
1314d7007b4SHiral Patel  *
1324d7007b4SHiral Patel  * Returns:
1334d7007b4SHiral Patel  * This function returns the amount of data that was written.
1344d7007b4SHiral Patel  */
1354d7007b4SHiral Patel static ssize_t fnic_trace_ctrl_write(struct file *filp,
1364d7007b4SHiral Patel 				  const char __user *ubuf,
1374d7007b4SHiral Patel 				  size_t cnt, loff_t *ppos)
1384d7007b4SHiral Patel {
1394d7007b4SHiral Patel 	char buf[64];
1404d7007b4SHiral Patel 	unsigned long val;
1414d7007b4SHiral Patel 	int ret;
142abb14148SHiral Shah 	u8 *trace_type;
143abb14148SHiral Shah 	trace_type = (u8 *)filp->private_data;
1444d7007b4SHiral Patel 
1454d7007b4SHiral Patel 	if (cnt >= sizeof(buf))
1464d7007b4SHiral Patel 		return -EINVAL;
1474d7007b4SHiral Patel 
1484d7007b4SHiral Patel 	if (copy_from_user(&buf, ubuf, cnt))
1494d7007b4SHiral Patel 		return -EFAULT;
1504d7007b4SHiral Patel 
1514d7007b4SHiral Patel 	buf[cnt] = 0;
1524d7007b4SHiral Patel 
1534d7007b4SHiral Patel 	ret = kstrtoul(buf, 10, &val);
1544d7007b4SHiral Patel 	if (ret < 0)
1554d7007b4SHiral Patel 		return ret;
1564d7007b4SHiral Patel 
157abb14148SHiral Shah 	if (*trace_type == fc_trc_flag->fnic_trace)
1584d7007b4SHiral Patel 		fnic_tracing_enabled = val;
159abb14148SHiral Shah 	else if (*trace_type == fc_trc_flag->fc_trace)
160abb14148SHiral Shah 		fnic_fc_tracing_enabled = val;
161abb14148SHiral Shah 	else if (*trace_type == fc_trc_flag->fc_clear)
162abb14148SHiral Shah 		fnic_fc_trace_cleared = val;
163abb14148SHiral Shah 	else
1641a84db56SMasanari Iida 		pr_err("fnic: cannot write to any debugfs file\n");
165abb14148SHiral Shah 
1664d7007b4SHiral Patel 	(*ppos)++;
1674d7007b4SHiral Patel 
1684d7007b4SHiral Patel 	return cnt;
1694d7007b4SHiral Patel }
1704d7007b4SHiral Patel 
171abb14148SHiral Shah static const struct file_operations fnic_trace_ctrl_fops = {
172abb14148SHiral Shah 	.owner = THIS_MODULE,
173d9462140SVasyl Gomonovych 	.open = simple_open,
174abb14148SHiral Shah 	.read = fnic_trace_ctrl_read,
175abb14148SHiral Shah 	.write = fnic_trace_ctrl_write,
176abb14148SHiral Shah };
177abb14148SHiral Shah 
1784d7007b4SHiral Patel /*
1794d7007b4SHiral Patel  * fnic_trace_debugfs_open - Open the fnic trace log
1804d7007b4SHiral Patel  * @inode: The inode pointer
1814d7007b4SHiral Patel  * @file: The file pointer to attach the log output
1824d7007b4SHiral Patel  *
1834d7007b4SHiral Patel  * Description:
1844d7007b4SHiral Patel  * This routine is the entry point for the debugfs open file operation.
1854d7007b4SHiral Patel  * It allocates the necessary buffer for the log, fills the buffer from
1864d7007b4SHiral Patel  * the in-memory log and then returns a pointer to that log in
1874d7007b4SHiral Patel  * the private_data field in @file.
1884d7007b4SHiral Patel  *
1894d7007b4SHiral Patel  * Returns:
1904d7007b4SHiral Patel  * This function returns zero if successful. On error it will return
1914d7007b4SHiral Patel  * a negative error value.
1924d7007b4SHiral Patel  */
1934d7007b4SHiral Patel static int fnic_trace_debugfs_open(struct inode *inode,
1944d7007b4SHiral Patel 				  struct file *file)
1954d7007b4SHiral Patel {
1964d7007b4SHiral Patel 	fnic_dbgfs_t *fnic_dbg_prt;
197abb14148SHiral Shah 	u8 *rdata_ptr;
198abb14148SHiral Shah 	rdata_ptr = (u8 *)inode->i_private;
1994d7007b4SHiral Patel 	fnic_dbg_prt = kzalloc(sizeof(fnic_dbgfs_t), GFP_KERNEL);
2004d7007b4SHiral Patel 	if (!fnic_dbg_prt)
2014d7007b4SHiral Patel 		return -ENOMEM;
2024d7007b4SHiral Patel 
203abb14148SHiral Shah 	if (*rdata_ptr == fc_trc_flag->fnic_trace) {
204*14ce2c26SChristophe JAILLET 		fnic_dbg_prt->buffer = vzalloc(array3_size(3, trace_max_pages,
20542bc47b3SKees Cook 							   PAGE_SIZE));
2064d7007b4SHiral Patel 		if (!fnic_dbg_prt->buffer) {
2074d7007b4SHiral Patel 			kfree(fnic_dbg_prt);
2084d7007b4SHiral Patel 			return -ENOMEM;
2094d7007b4SHiral Patel 		}
2104d7007b4SHiral Patel 		fnic_dbg_prt->buffer_len = fnic_get_trace_data(fnic_dbg_prt);
211abb14148SHiral Shah 	} else {
212abb14148SHiral Shah 		fnic_dbg_prt->buffer =
213*14ce2c26SChristophe JAILLET 			vzalloc(array3_size(3, fnic_fc_trace_max_pages,
21442bc47b3SKees Cook 					    PAGE_SIZE));
215abb14148SHiral Shah 		if (!fnic_dbg_prt->buffer) {
216abb14148SHiral Shah 			kfree(fnic_dbg_prt);
217abb14148SHiral Shah 			return -ENOMEM;
218abb14148SHiral Shah 		}
219abb14148SHiral Shah 		fnic_dbg_prt->buffer_len =
220abb14148SHiral Shah 			fnic_fc_trace_get_data(fnic_dbg_prt, *rdata_ptr);
221abb14148SHiral Shah 	}
2224d7007b4SHiral Patel 	file->private_data = fnic_dbg_prt;
223abb14148SHiral Shah 
2244d7007b4SHiral Patel 	return 0;
2254d7007b4SHiral Patel }
2264d7007b4SHiral Patel 
2274d7007b4SHiral Patel /*
2284d7007b4SHiral Patel  * fnic_trace_debugfs_lseek - Seek through a debugfs file
2294d7007b4SHiral Patel  * @file: The file pointer to seek through.
2304d7007b4SHiral Patel  * @offset: The offset to seek to or the amount to seek by.
2314d7007b4SHiral Patel  * @howto: Indicates how to seek.
2324d7007b4SHiral Patel  *
2334d7007b4SHiral Patel  * Description:
2344d7007b4SHiral Patel  * This routine is the entry point for the debugfs lseek file operation.
2354d7007b4SHiral Patel  * The @howto parameter indicates whether @offset is the offset to directly
2364d7007b4SHiral Patel  * seek to, or if it is a value to seek forward or reverse by. This function
2374d7007b4SHiral Patel  * figures out what the new offset of the debugfs file will be and assigns
2384d7007b4SHiral Patel  * that value to the f_pos field of @file.
2394d7007b4SHiral Patel  *
2404d7007b4SHiral Patel  * Returns:
2414d7007b4SHiral Patel  * This function returns the new offset if successful and returns a negative
2424d7007b4SHiral Patel  * error if unable to process the seek.
2434d7007b4SHiral Patel  */
2444d7007b4SHiral Patel static loff_t fnic_trace_debugfs_lseek(struct file *file,
2454d7007b4SHiral Patel 					loff_t offset,
2464d7007b4SHiral Patel 					int howto)
2474d7007b4SHiral Patel {
2484d7007b4SHiral Patel 	fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
249eb5881d3SAl Viro 	return fixed_size_llseek(file, offset, howto,
250eb5881d3SAl Viro 				fnic_dbg_prt->buffer_len);
2514d7007b4SHiral Patel }
2524d7007b4SHiral Patel 
2534d7007b4SHiral Patel /*
2544d7007b4SHiral Patel  * fnic_trace_debugfs_read - Read a debugfs file
2554d7007b4SHiral Patel  * @file: The file pointer to read from.
2564d7007b4SHiral Patel  * @ubuf: The buffer to copy the data to.
2574d7007b4SHiral Patel  * @nbytes: The number of bytes to read.
2584d7007b4SHiral Patel  * @pos: The position in the file to start reading from.
2594d7007b4SHiral Patel  *
2604d7007b4SHiral Patel  * Description:
2614d7007b4SHiral Patel  * This routine reads data from the buffer indicated in the private_data
2624d7007b4SHiral Patel  * field of @file. It will start reading at @pos and copy up to @nbytes of
2634d7007b4SHiral Patel  * data to @ubuf.
2644d7007b4SHiral Patel  *
2654d7007b4SHiral Patel  * Returns:
2664d7007b4SHiral Patel  * This function returns the amount of data that was read (this could be
2674d7007b4SHiral Patel  * less than @nbytes if the end of the file was reached).
2684d7007b4SHiral Patel  */
2694d7007b4SHiral Patel static ssize_t fnic_trace_debugfs_read(struct file *file,
2704d7007b4SHiral Patel 					char __user *ubuf,
2714d7007b4SHiral Patel 					size_t nbytes,
2724d7007b4SHiral Patel 					loff_t *pos)
2734d7007b4SHiral Patel {
2744d7007b4SHiral Patel 	fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
2754d7007b4SHiral Patel 	int rc = 0;
2764d7007b4SHiral Patel 	rc = simple_read_from_buffer(ubuf, nbytes, pos,
2774d7007b4SHiral Patel 				  fnic_dbg_prt->buffer,
2784d7007b4SHiral Patel 				  fnic_dbg_prt->buffer_len);
2794d7007b4SHiral Patel 	return rc;
2804d7007b4SHiral Patel }
2814d7007b4SHiral Patel 
2824d7007b4SHiral Patel /*
2834d7007b4SHiral Patel  * fnic_trace_debugfs_release - Release the buffer used to store
2844d7007b4SHiral Patel  * debugfs file data
2854d7007b4SHiral Patel  * @inode: The inode pointer
2864d7007b4SHiral Patel  * @file: The file pointer that contains the buffer to release
2874d7007b4SHiral Patel  *
2884d7007b4SHiral Patel  * Description:
2894d7007b4SHiral Patel  * This routine frees the buffer that was allocated when the debugfs
2904d7007b4SHiral Patel  * file was opened.
2914d7007b4SHiral Patel  *
2924d7007b4SHiral Patel  * Returns:
2934d7007b4SHiral Patel  * This function returns zero.
2944d7007b4SHiral Patel  */
2954d7007b4SHiral Patel static int fnic_trace_debugfs_release(struct inode *inode,
2964d7007b4SHiral Patel 					  struct file *file)
2974d7007b4SHiral Patel {
2984d7007b4SHiral Patel 	fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
2994d7007b4SHiral Patel 
3004d7007b4SHiral Patel 	vfree(fnic_dbg_prt->buffer);
3014d7007b4SHiral Patel 	kfree(fnic_dbg_prt);
3024d7007b4SHiral Patel 	return 0;
3034d7007b4SHiral Patel }
3044d7007b4SHiral Patel 
3054d7007b4SHiral Patel static const struct file_operations fnic_trace_debugfs_fops = {
3064d7007b4SHiral Patel 	.owner = THIS_MODULE,
3074d7007b4SHiral Patel 	.open = fnic_trace_debugfs_open,
3084d7007b4SHiral Patel 	.llseek = fnic_trace_debugfs_lseek,
3094d7007b4SHiral Patel 	.read = fnic_trace_debugfs_read,
3104d7007b4SHiral Patel 	.release = fnic_trace_debugfs_release,
3114d7007b4SHiral Patel };
3124d7007b4SHiral Patel 
3134d7007b4SHiral Patel /*
3144d7007b4SHiral Patel  * fnic_trace_debugfs_init - Initialize debugfs for fnic trace logging
3154d7007b4SHiral Patel  *
3164d7007b4SHiral Patel  * Description:
3174d7007b4SHiral Patel  * When Debugfs is configured this routine sets up the fnic debugfs
3184d7007b4SHiral Patel  * file system. If not already created, this routine will create the
31967125b02SHiral Patel  * create file trace to log fnic trace buffer output into debugfs and
32067125b02SHiral Patel  * it will also create file trace_enable to control enable/disable of
32167125b02SHiral Patel  * trace logging into trace buffer.
3224d7007b4SHiral Patel  */
3231dbaa379SGreg Kroah-Hartman void fnic_trace_debugfs_init(void)
3244d7007b4SHiral Patel {
3254d7007b4SHiral Patel 	fnic_trace_enable = debugfs_create_file("tracing_enable",
3264d7007b4SHiral Patel 					S_IFREG|S_IRUGO|S_IWUSR,
3274d7007b4SHiral Patel 					fnic_trace_debugfs_root,
328abb14148SHiral Shah 					&(fc_trc_flag->fnic_trace),
329abb14148SHiral Shah 					&fnic_trace_ctrl_fops);
3304d7007b4SHiral Patel 
3314d7007b4SHiral Patel 	fnic_trace_debugfs_file = debugfs_create_file("trace",
3324d7007b4SHiral Patel 					S_IFREG|S_IRUGO|S_IWUSR,
3334d7007b4SHiral Patel 					fnic_trace_debugfs_root,
334abb14148SHiral Shah 					&(fc_trc_flag->fnic_trace),
3354d7007b4SHiral Patel 					&fnic_trace_debugfs_fops);
3364d7007b4SHiral Patel }
3374d7007b4SHiral Patel 
3384d7007b4SHiral Patel /*
3394d7007b4SHiral Patel  * fnic_trace_debugfs_terminate - Tear down debugfs infrastructure
3404d7007b4SHiral Patel  *
3414d7007b4SHiral Patel  * Description:
3424d7007b4SHiral Patel  * When Debugfs is configured this routine removes debugfs file system
3434d7007b4SHiral Patel  * elements that are specific to fnic trace logging.
3444d7007b4SHiral Patel  */
3454d7007b4SHiral Patel void fnic_trace_debugfs_terminate(void)
3464d7007b4SHiral Patel {
3474d7007b4SHiral Patel 	debugfs_remove(fnic_trace_debugfs_file);
3484d7007b4SHiral Patel 	fnic_trace_debugfs_file = NULL;
349abb14148SHiral Shah 
3504d7007b4SHiral Patel 	debugfs_remove(fnic_trace_enable);
3514d7007b4SHiral Patel 	fnic_trace_enable = NULL;
3524d7007b4SHiral Patel }
353abb14148SHiral Shah 
354abb14148SHiral Shah /*
355abb14148SHiral Shah  * fnic_fc_trace_debugfs_init -
356abb14148SHiral Shah  * Initialize debugfs for fnic control frame trace logging
357abb14148SHiral Shah  *
358abb14148SHiral Shah  * Description:
359abb14148SHiral Shah  * When Debugfs is configured this routine sets up the fnic_fc debugfs
360abb14148SHiral Shah  * file system. If not already created, this routine will create the
361abb14148SHiral Shah  * create file trace to log fnic fc trace buffer output into debugfs and
362abb14148SHiral Shah  * it will also create file fc_trace_enable to control enable/disable of
363abb14148SHiral Shah  * trace logging into trace buffer.
364abb14148SHiral Shah  */
365abb14148SHiral Shah 
3661dbaa379SGreg Kroah-Hartman void fnic_fc_trace_debugfs_init(void)
367abb14148SHiral Shah {
368abb14148SHiral Shah 	fnic_fc_trace_enable = debugfs_create_file("fc_trace_enable",
369abb14148SHiral Shah 					S_IFREG|S_IRUGO|S_IWUSR,
370abb14148SHiral Shah 					fnic_trace_debugfs_root,
371abb14148SHiral Shah 					&(fc_trc_flag->fc_trace),
372abb14148SHiral Shah 					&fnic_trace_ctrl_fops);
373abb14148SHiral Shah 
374abb14148SHiral Shah 	fnic_fc_trace_clear = debugfs_create_file("fc_trace_clear",
375abb14148SHiral Shah 					S_IFREG|S_IRUGO|S_IWUSR,
376abb14148SHiral Shah 					fnic_trace_debugfs_root,
377abb14148SHiral Shah 					&(fc_trc_flag->fc_clear),
378abb14148SHiral Shah 					&fnic_trace_ctrl_fops);
379abb14148SHiral Shah 
380abb14148SHiral Shah 	fnic_fc_rdata_trace_debugfs_file =
381abb14148SHiral Shah 		debugfs_create_file("fc_trace_rdata",
382abb14148SHiral Shah 				    S_IFREG|S_IRUGO|S_IWUSR,
383abb14148SHiral Shah 				    fnic_trace_debugfs_root,
384abb14148SHiral Shah 				    &(fc_trc_flag->fc_normal_file),
385abb14148SHiral Shah 				    &fnic_trace_debugfs_fops);
386abb14148SHiral Shah 
387abb14148SHiral Shah 	fnic_fc_trace_debugfs_file =
388abb14148SHiral Shah 		debugfs_create_file("fc_trace",
389abb14148SHiral Shah 				    S_IFREG|S_IRUGO|S_IWUSR,
390abb14148SHiral Shah 				    fnic_trace_debugfs_root,
391abb14148SHiral Shah 				    &(fc_trc_flag->fc_row_file),
392abb14148SHiral Shah 				    &fnic_trace_debugfs_fops);
393abb14148SHiral Shah }
394abb14148SHiral Shah 
395abb14148SHiral Shah /*
396abb14148SHiral Shah  * fnic_fc_trace_debugfs_terminate - Tear down debugfs infrastructure
397abb14148SHiral Shah  *
398abb14148SHiral Shah  * Description:
399abb14148SHiral Shah  * When Debugfs is configured this routine removes debugfs file system
400abb14148SHiral Shah  * elements that are specific to fnic_fc trace logging.
401abb14148SHiral Shah  */
402abb14148SHiral Shah 
403abb14148SHiral Shah void fnic_fc_trace_debugfs_terminate(void)
404abb14148SHiral Shah {
405abb14148SHiral Shah 	debugfs_remove(fnic_fc_trace_debugfs_file);
406abb14148SHiral Shah 	fnic_fc_trace_debugfs_file = NULL;
407abb14148SHiral Shah 
408abb14148SHiral Shah 	debugfs_remove(fnic_fc_rdata_trace_debugfs_file);
409abb14148SHiral Shah 	fnic_fc_rdata_trace_debugfs_file = NULL;
410abb14148SHiral Shah 
411abb14148SHiral Shah 	debugfs_remove(fnic_fc_trace_enable);
412abb14148SHiral Shah 	fnic_fc_trace_enable = NULL;
413abb14148SHiral Shah 
414abb14148SHiral Shah 	debugfs_remove(fnic_fc_trace_clear);
415abb14148SHiral Shah 	fnic_fc_trace_clear = NULL;
4164d7007b4SHiral Patel }
41767125b02SHiral Patel 
41867125b02SHiral Patel /*
41967125b02SHiral Patel  * fnic_reset_stats_open - Open the reset_stats file
42067125b02SHiral Patel  * @inode: The inode pointer.
42167125b02SHiral Patel  * @file: The file pointer to attach the stats reset flag.
42267125b02SHiral Patel  *
42367125b02SHiral Patel  * Description:
42467125b02SHiral Patel  * This routine opens a debugsfs file reset_stats and stores i_private data
42567125b02SHiral Patel  * to debug structure to retrieve later for while performing other
42667125b02SHiral Patel  * file oprations.
42767125b02SHiral Patel  *
42867125b02SHiral Patel  * Returns:
42967125b02SHiral Patel  * This function returns zero if successful.
43067125b02SHiral Patel  */
43167125b02SHiral Patel static int fnic_reset_stats_open(struct inode *inode, struct file *file)
43267125b02SHiral Patel {
43367125b02SHiral Patel 	struct stats_debug_info *debug;
43467125b02SHiral Patel 
43567125b02SHiral Patel 	debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
43667125b02SHiral Patel 	if (!debug)
43767125b02SHiral Patel 		return -ENOMEM;
43867125b02SHiral Patel 
43967125b02SHiral Patel 	debug->i_private = inode->i_private;
44067125b02SHiral Patel 
44167125b02SHiral Patel 	file->private_data = debug;
44267125b02SHiral Patel 
44367125b02SHiral Patel 	return 0;
44467125b02SHiral Patel }
44567125b02SHiral Patel 
44667125b02SHiral Patel /*
44767125b02SHiral Patel  * fnic_reset_stats_read - Read a reset_stats debugfs file
44867125b02SHiral Patel  * @filp: The file pointer to read from.
44967125b02SHiral Patel  * @ubuf: The buffer to copy the data to.
45067125b02SHiral Patel  * @cnt: The number of bytes to read.
45167125b02SHiral Patel  * @ppos: The position in the file to start reading from.
45267125b02SHiral Patel  *
45367125b02SHiral Patel  * Description:
45467125b02SHiral Patel  * This routine reads value of variable reset_stats
45567125b02SHiral Patel  * and stores into local @buf. It will start reading file at @ppos and
45667125b02SHiral Patel  * copy up to @cnt of data to @ubuf from @buf.
45767125b02SHiral Patel  *
45867125b02SHiral Patel  * Returns:
45967125b02SHiral Patel  * This function returns the amount of data that was read.
46067125b02SHiral Patel  */
46167125b02SHiral Patel static ssize_t fnic_reset_stats_read(struct file *file,
46267125b02SHiral Patel 					char __user *ubuf,
46367125b02SHiral Patel 					size_t cnt, loff_t *ppos)
46467125b02SHiral Patel {
46567125b02SHiral Patel 	struct stats_debug_info *debug = file->private_data;
46667125b02SHiral Patel 	struct fnic *fnic = (struct fnic *)debug->i_private;
46767125b02SHiral Patel 	char buf[64];
46867125b02SHiral Patel 	int len;
46967125b02SHiral Patel 
47067125b02SHiral Patel 	len = sprintf(buf, "%u\n", fnic->reset_stats);
47167125b02SHiral Patel 
47267125b02SHiral Patel 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
47367125b02SHiral Patel }
47467125b02SHiral Patel 
47567125b02SHiral Patel /*
47667125b02SHiral Patel  * fnic_reset_stats_write - Write to reset_stats debugfs file
47767125b02SHiral Patel  * @filp: The file pointer to write from.
47867125b02SHiral Patel  * @ubuf: The buffer to copy the data from.
47967125b02SHiral Patel  * @cnt: The number of bytes to write.
48067125b02SHiral Patel  * @ppos: The position in the file to start writing to.
48167125b02SHiral Patel  *
48267125b02SHiral Patel  * Description:
48367125b02SHiral Patel  * This routine writes data from user buffer @ubuf to buffer @buf and
48467125b02SHiral Patel  * resets cumulative stats of fnic.
48567125b02SHiral Patel  *
48667125b02SHiral Patel  * Returns:
48767125b02SHiral Patel  * This function returns the amount of data that was written.
48867125b02SHiral Patel  */
48967125b02SHiral Patel static ssize_t fnic_reset_stats_write(struct file *file,
49067125b02SHiral Patel 					const char __user *ubuf,
49167125b02SHiral Patel 					size_t cnt, loff_t *ppos)
49267125b02SHiral Patel {
49367125b02SHiral Patel 	struct stats_debug_info *debug = file->private_data;
49467125b02SHiral Patel 	struct fnic *fnic = (struct fnic *)debug->i_private;
49567125b02SHiral Patel 	struct fnic_stats *stats = &fnic->fnic_stats;
49667125b02SHiral Patel 	u64 *io_stats_p = (u64 *)&stats->io_stats;
49767125b02SHiral Patel 	u64 *fw_stats_p = (u64 *)&stats->fw_stats;
49867125b02SHiral Patel 	char buf[64];
49967125b02SHiral Patel 	unsigned long val;
50067125b02SHiral Patel 	int ret;
50167125b02SHiral Patel 
50267125b02SHiral Patel 	if (cnt >= sizeof(buf))
50367125b02SHiral Patel 		return -EINVAL;
50467125b02SHiral Patel 
50567125b02SHiral Patel 	if (copy_from_user(&buf, ubuf, cnt))
50667125b02SHiral Patel 		return -EFAULT;
50767125b02SHiral Patel 
50867125b02SHiral Patel 	buf[cnt] = 0;
50967125b02SHiral Patel 
51067125b02SHiral Patel 	ret = kstrtoul(buf, 10, &val);
51167125b02SHiral Patel 	if (ret < 0)
51267125b02SHiral Patel 		return ret;
51367125b02SHiral Patel 
51467125b02SHiral Patel 	fnic->reset_stats = val;
51567125b02SHiral Patel 
51667125b02SHiral Patel 	if (fnic->reset_stats) {
51767125b02SHiral Patel 		/* Skip variable is used to avoid descrepancies to Num IOs
51867125b02SHiral Patel 		 * and IO Completions stats. Skip incrementing No IO Compls
51967125b02SHiral Patel 		 * for pending active IOs after reset stats
52067125b02SHiral Patel 		 */
52167125b02SHiral Patel 		atomic64_set(&fnic->io_cmpl_skip,
52267125b02SHiral Patel 			atomic64_read(&stats->io_stats.active_ios));
52367125b02SHiral Patel 		memset(&stats->abts_stats, 0, sizeof(struct abort_stats));
52467125b02SHiral Patel 		memset(&stats->term_stats, 0,
52567125b02SHiral Patel 			sizeof(struct terminate_stats));
52667125b02SHiral Patel 		memset(&stats->reset_stats, 0, sizeof(struct reset_stats));
52767125b02SHiral Patel 		memset(&stats->misc_stats, 0, sizeof(struct misc_stats));
52867125b02SHiral Patel 		memset(&stats->vlan_stats, 0, sizeof(struct vlan_stats));
52967125b02SHiral Patel 		memset(io_stats_p+1, 0,
53067125b02SHiral Patel 			sizeof(struct io_path_stats) - sizeof(u64));
53167125b02SHiral Patel 		memset(fw_stats_p+1, 0,
53267125b02SHiral Patel 			sizeof(struct fw_stats) - sizeof(u64));
53322807aa8SArnd Bergmann 		ktime_get_real_ts64(&stats->stats_timestamps.last_reset_time);
53467125b02SHiral Patel 	}
53567125b02SHiral Patel 
53667125b02SHiral Patel 	(*ppos)++;
53767125b02SHiral Patel 	return cnt;
53867125b02SHiral Patel }
53967125b02SHiral Patel 
54067125b02SHiral Patel /*
54167125b02SHiral Patel  * fnic_reset_stats_release - Release the buffer used to store
54267125b02SHiral Patel  * debugfs file data
54367125b02SHiral Patel  * @inode: The inode pointer
54467125b02SHiral Patel  * @file: The file pointer that contains the buffer to release
54567125b02SHiral Patel  *
54667125b02SHiral Patel  * Description:
54767125b02SHiral Patel  * This routine frees the buffer that was allocated when the debugfs
54867125b02SHiral Patel  * file was opened.
54967125b02SHiral Patel  *
55067125b02SHiral Patel  * Returns:
55167125b02SHiral Patel  * This function returns zero.
55267125b02SHiral Patel  */
55367125b02SHiral Patel static int fnic_reset_stats_release(struct inode *inode,
55467125b02SHiral Patel 					struct file *file)
55567125b02SHiral Patel {
55667125b02SHiral Patel 	struct stats_debug_info *debug = file->private_data;
55767125b02SHiral Patel 	kfree(debug);
55867125b02SHiral Patel 	return 0;
55967125b02SHiral Patel }
56067125b02SHiral Patel 
56167125b02SHiral Patel /*
56267125b02SHiral Patel  * fnic_stats_debugfs_open - Open the stats file for specific host
56367125b02SHiral Patel  * and get fnic stats.
56467125b02SHiral Patel  * @inode: The inode pointer.
56567125b02SHiral Patel  * @file: The file pointer to attach the specific host statistics.
56667125b02SHiral Patel  *
56767125b02SHiral Patel  * Description:
56867125b02SHiral Patel  * This routine opens a debugsfs file stats of specific host and print
56967125b02SHiral Patel  * fnic stats.
57067125b02SHiral Patel  *
57167125b02SHiral Patel  * Returns:
57267125b02SHiral Patel  * This function returns zero if successful.
57367125b02SHiral Patel  */
57467125b02SHiral Patel static int fnic_stats_debugfs_open(struct inode *inode,
57567125b02SHiral Patel 					struct file *file)
57667125b02SHiral Patel {
57767125b02SHiral Patel 	struct fnic *fnic = inode->i_private;
57867125b02SHiral Patel 	struct fnic_stats *fnic_stats = &fnic->fnic_stats;
57967125b02SHiral Patel 	struct stats_debug_info *debug;
58067125b02SHiral Patel 	int buf_size = 2 * PAGE_SIZE;
58167125b02SHiral Patel 
58267125b02SHiral Patel 	debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
58367125b02SHiral Patel 	if (!debug)
58467125b02SHiral Patel 		return -ENOMEM;
58567125b02SHiral Patel 
58667125b02SHiral Patel 	debug->debug_buffer = vmalloc(buf_size);
58767125b02SHiral Patel 	if (!debug->debug_buffer) {
58867125b02SHiral Patel 		kfree(debug);
58967125b02SHiral Patel 		return -ENOMEM;
59067125b02SHiral Patel 	}
59167125b02SHiral Patel 
59267125b02SHiral Patel 	debug->buf_size = buf_size;
59367125b02SHiral Patel 	memset((void *)debug->debug_buffer, 0, buf_size);
59467125b02SHiral Patel 	debug->buffer_len = fnic_get_stats_data(debug, fnic_stats);
59567125b02SHiral Patel 
59667125b02SHiral Patel 	file->private_data = debug;
59767125b02SHiral Patel 
59867125b02SHiral Patel 	return 0;
59967125b02SHiral Patel }
60067125b02SHiral Patel 
60167125b02SHiral Patel /*
60267125b02SHiral Patel  * fnic_stats_debugfs_read - Read a debugfs file
60367125b02SHiral Patel  * @file: The file pointer to read from.
60467125b02SHiral Patel  * @ubuf: The buffer to copy the data to.
60567125b02SHiral Patel  * @nbytes: The number of bytes to read.
60667125b02SHiral Patel  * @pos: The position in the file to start reading from.
60767125b02SHiral Patel  *
60867125b02SHiral Patel  * Description:
60967125b02SHiral Patel  * This routine reads data from the buffer indicated in the private_data
61067125b02SHiral Patel  * field of @file. It will start reading at @pos and copy up to @nbytes of
61167125b02SHiral Patel  * data to @ubuf.
61267125b02SHiral Patel  *
61367125b02SHiral Patel  * Returns:
61467125b02SHiral Patel  * This function returns the amount of data that was read (this could be
61567125b02SHiral Patel  * less than @nbytes if the end of the file was reached).
61667125b02SHiral Patel  */
61767125b02SHiral Patel static ssize_t fnic_stats_debugfs_read(struct file *file,
61867125b02SHiral Patel 					char __user *ubuf,
61967125b02SHiral Patel 					size_t nbytes,
62067125b02SHiral Patel 					loff_t *pos)
62167125b02SHiral Patel {
62267125b02SHiral Patel 	struct stats_debug_info *debug = file->private_data;
62367125b02SHiral Patel 	int rc = 0;
62467125b02SHiral Patel 	rc = simple_read_from_buffer(ubuf, nbytes, pos,
62567125b02SHiral Patel 					debug->debug_buffer,
62667125b02SHiral Patel 					debug->buffer_len);
62767125b02SHiral Patel 	return rc;
62867125b02SHiral Patel }
62967125b02SHiral Patel 
63067125b02SHiral Patel /*
63167125b02SHiral Patel  * fnic_stats_stats_release - Release the buffer used to store
63267125b02SHiral Patel  * debugfs file data
63367125b02SHiral Patel  * @inode: The inode pointer
63467125b02SHiral Patel  * @file: The file pointer that contains the buffer to release
63567125b02SHiral Patel  *
63667125b02SHiral Patel  * Description:
63767125b02SHiral Patel  * This routine frees the buffer that was allocated when the debugfs
63867125b02SHiral Patel  * file was opened.
63967125b02SHiral Patel  *
64067125b02SHiral Patel  * Returns:
64167125b02SHiral Patel  * This function returns zero.
64267125b02SHiral Patel  */
64367125b02SHiral Patel static int fnic_stats_debugfs_release(struct inode *inode,
64467125b02SHiral Patel 					struct file *file)
64567125b02SHiral Patel {
64667125b02SHiral Patel 	struct stats_debug_info *debug = file->private_data;
64767125b02SHiral Patel 	vfree(debug->debug_buffer);
64867125b02SHiral Patel 	kfree(debug);
64967125b02SHiral Patel 	return 0;
65067125b02SHiral Patel }
65167125b02SHiral Patel 
65267125b02SHiral Patel static const struct file_operations fnic_stats_debugfs_fops = {
65367125b02SHiral Patel 	.owner = THIS_MODULE,
65467125b02SHiral Patel 	.open = fnic_stats_debugfs_open,
65567125b02SHiral Patel 	.read = fnic_stats_debugfs_read,
65667125b02SHiral Patel 	.release = fnic_stats_debugfs_release,
65767125b02SHiral Patel };
65867125b02SHiral Patel 
65967125b02SHiral Patel static const struct file_operations fnic_reset_debugfs_fops = {
66067125b02SHiral Patel 	.owner = THIS_MODULE,
66167125b02SHiral Patel 	.open = fnic_reset_stats_open,
66267125b02SHiral Patel 	.read = fnic_reset_stats_read,
66367125b02SHiral Patel 	.write = fnic_reset_stats_write,
66467125b02SHiral Patel 	.release = fnic_reset_stats_release,
66567125b02SHiral Patel };
66667125b02SHiral Patel 
66767125b02SHiral Patel /*
66867125b02SHiral Patel  * fnic_stats_init - Initialize stats struct and create stats file per fnic
66967125b02SHiral Patel  *
67067125b02SHiral Patel  * Description:
67167125b02SHiral Patel  * When Debugfs is configured this routine sets up the stats file per fnic
67267125b02SHiral Patel  * It will create file stats and reset_stats under statistics/host# directory
67367125b02SHiral Patel  * to log per fnic stats.
67467125b02SHiral Patel  */
6751dbaa379SGreg Kroah-Hartman void fnic_stats_debugfs_init(struct fnic *fnic)
67667125b02SHiral Patel {
67767125b02SHiral Patel 	char name[16];
67867125b02SHiral Patel 
67967125b02SHiral Patel 	snprintf(name, sizeof(name), "host%d", fnic->lport->host->host_no);
68067125b02SHiral Patel 
68167125b02SHiral Patel 	fnic->fnic_stats_debugfs_host = debugfs_create_dir(name,
68267125b02SHiral Patel 						fnic_stats_debugfs_root);
68367125b02SHiral Patel 
68467125b02SHiral Patel 	fnic->fnic_stats_debugfs_file = debugfs_create_file("stats",
68567125b02SHiral Patel 						S_IFREG|S_IRUGO|S_IWUSR,
68667125b02SHiral Patel 						fnic->fnic_stats_debugfs_host,
68767125b02SHiral Patel 						fnic,
68867125b02SHiral Patel 						&fnic_stats_debugfs_fops);
68967125b02SHiral Patel 
69067125b02SHiral Patel 	fnic->fnic_reset_debugfs_file = debugfs_create_file("reset_stats",
69167125b02SHiral Patel 						S_IFREG|S_IRUGO|S_IWUSR,
69267125b02SHiral Patel 						fnic->fnic_stats_debugfs_host,
69367125b02SHiral Patel 						fnic,
69467125b02SHiral Patel 						&fnic_reset_debugfs_fops);
69567125b02SHiral Patel }
69667125b02SHiral Patel 
69767125b02SHiral Patel /*
69867125b02SHiral Patel  * fnic_stats_debugfs_remove - Tear down debugfs infrastructure of stats
69967125b02SHiral Patel  *
70067125b02SHiral Patel  * Description:
70167125b02SHiral Patel  * When Debugfs is configured this routine removes debugfs file system
70267125b02SHiral Patel  * elements that are specific to fnic stats.
70367125b02SHiral Patel  */
70467125b02SHiral Patel void fnic_stats_debugfs_remove(struct fnic *fnic)
70567125b02SHiral Patel {
70667125b02SHiral Patel 	if (!fnic)
70767125b02SHiral Patel 		return;
70867125b02SHiral Patel 
70967125b02SHiral Patel 	debugfs_remove(fnic->fnic_stats_debugfs_file);
71067125b02SHiral Patel 	fnic->fnic_stats_debugfs_file = NULL;
71167125b02SHiral Patel 
71267125b02SHiral Patel 	debugfs_remove(fnic->fnic_reset_debugfs_file);
71367125b02SHiral Patel 	fnic->fnic_reset_debugfs_file = NULL;
71467125b02SHiral Patel 
71567125b02SHiral Patel 	debugfs_remove(fnic->fnic_stats_debugfs_host);
71667125b02SHiral Patel 	fnic->fnic_stats_debugfs_host = NULL;
7174d7007b4SHiral Patel }
718