xref: /openbmc/linux/drivers/scsi/fnic/fnic_debugfs.c (revision 1dbaa379a419343dab5f99f253d8225b5ab4f939)
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 
5867125b02SHiral Patel 	fnic_stats_debugfs_root = debugfs_create_dir("statistics",
5967125b02SHiral Patel 						fnic_trace_debugfs_root);
6067125b02SHiral Patel 
61abb14148SHiral Shah 	/* Allocate memory to structure */
62abb14148SHiral Shah 	fc_trc_flag = (struct fc_trace_flag_type *)
63abb14148SHiral Shah 		vmalloc(sizeof(struct fc_trace_flag_type));
64abb14148SHiral Shah 
65abb14148SHiral Shah 	if (fc_trc_flag) {
66abb14148SHiral Shah 		fc_trc_flag->fc_row_file = 0;
67abb14148SHiral Shah 		fc_trc_flag->fc_normal_file = 1;
68abb14148SHiral Shah 		fc_trc_flag->fnic_trace = 2;
69abb14148SHiral Shah 		fc_trc_flag->fc_trace = 3;
70abb14148SHiral Shah 		fc_trc_flag->fc_clear = 4;
71abb14148SHiral Shah 	}
72abb14148SHiral Shah 
7367125b02SHiral Patel 	rc = 0;
7467125b02SHiral Patel 	return rc;
7567125b02SHiral Patel }
7667125b02SHiral Patel 
7767125b02SHiral Patel /*
7867125b02SHiral Patel  * fnic_debugfs_terminate - Tear down debugfs infrastructure
7967125b02SHiral Patel  *
8067125b02SHiral Patel  * Description:
8167125b02SHiral Patel  * When Debugfs is configured this routine removes debugfs file system
8267125b02SHiral Patel  * elements that are specific to fnic.
8367125b02SHiral Patel  */
8467125b02SHiral Patel void fnic_debugfs_terminate(void)
8567125b02SHiral Patel {
8667125b02SHiral Patel 	debugfs_remove(fnic_stats_debugfs_root);
8767125b02SHiral Patel 	fnic_stats_debugfs_root = NULL;
8867125b02SHiral Patel 
8967125b02SHiral Patel 	debugfs_remove(fnic_trace_debugfs_root);
9067125b02SHiral Patel 	fnic_trace_debugfs_root = NULL;
91abb14148SHiral Shah 
92abb14148SHiral Shah 	if (fc_trc_flag)
93abb14148SHiral Shah 		vfree(fc_trc_flag);
9467125b02SHiral Patel }
954d7007b4SHiral Patel 
964d7007b4SHiral Patel /*
97abb14148SHiral Shah  * fnic_trace_ctrl_read -
98abb14148SHiral Shah  *          Read  trace_enable ,fc_trace_enable
99abb14148SHiral Shah  *              or fc_trace_clear debugfs file
1004d7007b4SHiral Patel  * @filp: The file pointer to read from.
1014d7007b4SHiral Patel  * @ubuf: The buffer to copy the data to.
1024d7007b4SHiral Patel  * @cnt: The number of bytes to read.
1034d7007b4SHiral Patel  * @ppos: The position in the file to start reading from.
1044d7007b4SHiral Patel  *
1054d7007b4SHiral Patel  * Description:
106abb14148SHiral Shah  * This routine reads value of variable fnic_tracing_enabled or
107abb14148SHiral Shah  * fnic_fc_tracing_enabled or fnic_fc_trace_cleared
108abb14148SHiral Shah  * and stores into local @buf.
109abb14148SHiral Shah  * It will start reading file at @ppos and
1104d7007b4SHiral Patel  * copy up to @cnt of data to @ubuf from @buf.
1114d7007b4SHiral Patel  *
1124d7007b4SHiral Patel  * Returns:
1134d7007b4SHiral Patel  * This function returns the amount of data that was read.
1144d7007b4SHiral Patel  */
1154d7007b4SHiral Patel static ssize_t fnic_trace_ctrl_read(struct file *filp,
1164d7007b4SHiral Patel 				  char __user *ubuf,
1174d7007b4SHiral Patel 				  size_t cnt, loff_t *ppos)
1184d7007b4SHiral Patel {
1194d7007b4SHiral Patel 	char buf[64];
1204d7007b4SHiral Patel 	int len;
121abb14148SHiral Shah 	u8 *trace_type;
122abb14148SHiral Shah 	len = 0;
123abb14148SHiral Shah 	trace_type = (u8 *)filp->private_data;
124abb14148SHiral Shah 	if (*trace_type == fc_trc_flag->fnic_trace)
1254d7007b4SHiral Patel 		len = sprintf(buf, "%u\n", fnic_tracing_enabled);
126abb14148SHiral Shah 	else if (*trace_type == fc_trc_flag->fc_trace)
127abb14148SHiral Shah 		len = sprintf(buf, "%u\n", fnic_fc_tracing_enabled);
128abb14148SHiral Shah 	else if (*trace_type == fc_trc_flag->fc_clear)
129abb14148SHiral Shah 		len = sprintf(buf, "%u\n", fnic_fc_trace_cleared);
130abb14148SHiral Shah 	else
131abb14148SHiral Shah 		pr_err("fnic: Cannot read to any debugfs file\n");
1324d7007b4SHiral Patel 
1334d7007b4SHiral Patel 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
1344d7007b4SHiral Patel }
1354d7007b4SHiral Patel 
1364d7007b4SHiral Patel /*
137abb14148SHiral Shah  * fnic_trace_ctrl_write -
138abb14148SHiral Shah  * Write to trace_enable, fc_trace_enable or
139abb14148SHiral Shah  *         fc_trace_clear debugfs file
1404d7007b4SHiral Patel  * @filp: The file pointer to write from.
1414d7007b4SHiral Patel  * @ubuf: The buffer to copy the data from.
1424d7007b4SHiral Patel  * @cnt: The number of bytes to write.
1434d7007b4SHiral Patel  * @ppos: The position in the file to start writing to.
1444d7007b4SHiral Patel  *
1454d7007b4SHiral Patel  * Description:
1464d7007b4SHiral Patel  * This routine writes data from user buffer @ubuf to buffer @buf and
147abb14148SHiral Shah  * sets fc_trace_enable ,tracing_enable or fnic_fc_trace_cleared
148abb14148SHiral Shah  * value as per user input.
1494d7007b4SHiral Patel  *
1504d7007b4SHiral Patel  * Returns:
1514d7007b4SHiral Patel  * This function returns the amount of data that was written.
1524d7007b4SHiral Patel  */
1534d7007b4SHiral Patel static ssize_t fnic_trace_ctrl_write(struct file *filp,
1544d7007b4SHiral Patel 				  const char __user *ubuf,
1554d7007b4SHiral Patel 				  size_t cnt, loff_t *ppos)
1564d7007b4SHiral Patel {
1574d7007b4SHiral Patel 	char buf[64];
1584d7007b4SHiral Patel 	unsigned long val;
1594d7007b4SHiral Patel 	int ret;
160abb14148SHiral Shah 	u8 *trace_type;
161abb14148SHiral Shah 	trace_type = (u8 *)filp->private_data;
1624d7007b4SHiral Patel 
1634d7007b4SHiral Patel 	if (cnt >= sizeof(buf))
1644d7007b4SHiral Patel 		return -EINVAL;
1654d7007b4SHiral Patel 
1664d7007b4SHiral Patel 	if (copy_from_user(&buf, ubuf, cnt))
1674d7007b4SHiral Patel 		return -EFAULT;
1684d7007b4SHiral Patel 
1694d7007b4SHiral Patel 	buf[cnt] = 0;
1704d7007b4SHiral Patel 
1714d7007b4SHiral Patel 	ret = kstrtoul(buf, 10, &val);
1724d7007b4SHiral Patel 	if (ret < 0)
1734d7007b4SHiral Patel 		return ret;
1744d7007b4SHiral Patel 
175abb14148SHiral Shah 	if (*trace_type == fc_trc_flag->fnic_trace)
1764d7007b4SHiral Patel 		fnic_tracing_enabled = val;
177abb14148SHiral Shah 	else if (*trace_type == fc_trc_flag->fc_trace)
178abb14148SHiral Shah 		fnic_fc_tracing_enabled = val;
179abb14148SHiral Shah 	else if (*trace_type == fc_trc_flag->fc_clear)
180abb14148SHiral Shah 		fnic_fc_trace_cleared = val;
181abb14148SHiral Shah 	else
1821a84db56SMasanari Iida 		pr_err("fnic: cannot write to any debugfs file\n");
183abb14148SHiral Shah 
1844d7007b4SHiral Patel 	(*ppos)++;
1854d7007b4SHiral Patel 
1864d7007b4SHiral Patel 	return cnt;
1874d7007b4SHiral Patel }
1884d7007b4SHiral Patel 
189abb14148SHiral Shah static const struct file_operations fnic_trace_ctrl_fops = {
190abb14148SHiral Shah 	.owner = THIS_MODULE,
191d9462140SVasyl Gomonovych 	.open = simple_open,
192abb14148SHiral Shah 	.read = fnic_trace_ctrl_read,
193abb14148SHiral Shah 	.write = fnic_trace_ctrl_write,
194abb14148SHiral Shah };
195abb14148SHiral Shah 
1964d7007b4SHiral Patel /*
1974d7007b4SHiral Patel  * fnic_trace_debugfs_open - Open the fnic trace log
1984d7007b4SHiral Patel  * @inode: The inode pointer
1994d7007b4SHiral Patel  * @file: The file pointer to attach the log output
2004d7007b4SHiral Patel  *
2014d7007b4SHiral Patel  * Description:
2024d7007b4SHiral Patel  * This routine is the entry point for the debugfs open file operation.
2034d7007b4SHiral Patel  * It allocates the necessary buffer for the log, fills the buffer from
2044d7007b4SHiral Patel  * the in-memory log and then returns a pointer to that log in
2054d7007b4SHiral Patel  * the private_data field in @file.
2064d7007b4SHiral Patel  *
2074d7007b4SHiral Patel  * Returns:
2084d7007b4SHiral Patel  * This function returns zero if successful. On error it will return
2094d7007b4SHiral Patel  * a negative error value.
2104d7007b4SHiral Patel  */
2114d7007b4SHiral Patel static int fnic_trace_debugfs_open(struct inode *inode,
2124d7007b4SHiral Patel 				  struct file *file)
2134d7007b4SHiral Patel {
2144d7007b4SHiral Patel 	fnic_dbgfs_t *fnic_dbg_prt;
215abb14148SHiral Shah 	u8 *rdata_ptr;
216abb14148SHiral Shah 	rdata_ptr = (u8 *)inode->i_private;
2174d7007b4SHiral Patel 	fnic_dbg_prt = kzalloc(sizeof(fnic_dbgfs_t), GFP_KERNEL);
2184d7007b4SHiral Patel 	if (!fnic_dbg_prt)
2194d7007b4SHiral Patel 		return -ENOMEM;
2204d7007b4SHiral Patel 
221abb14148SHiral Shah 	if (*rdata_ptr == fc_trc_flag->fnic_trace) {
22242bc47b3SKees Cook 		fnic_dbg_prt->buffer = vmalloc(array3_size(3, trace_max_pages,
22342bc47b3SKees Cook 							   PAGE_SIZE));
2244d7007b4SHiral Patel 		if (!fnic_dbg_prt->buffer) {
2254d7007b4SHiral Patel 			kfree(fnic_dbg_prt);
2264d7007b4SHiral Patel 			return -ENOMEM;
2274d7007b4SHiral Patel 		}
2284d7007b4SHiral Patel 		memset((void *)fnic_dbg_prt->buffer, 0,
229abb14148SHiral Shah 		3 * (trace_max_pages * PAGE_SIZE));
2304d7007b4SHiral Patel 		fnic_dbg_prt->buffer_len = fnic_get_trace_data(fnic_dbg_prt);
231abb14148SHiral Shah 	} else {
232abb14148SHiral Shah 		fnic_dbg_prt->buffer =
23342bc47b3SKees Cook 			vmalloc(array3_size(3, fnic_fc_trace_max_pages,
23442bc47b3SKees Cook 					    PAGE_SIZE));
235abb14148SHiral Shah 		if (!fnic_dbg_prt->buffer) {
236abb14148SHiral Shah 			kfree(fnic_dbg_prt);
237abb14148SHiral Shah 			return -ENOMEM;
238abb14148SHiral Shah 		}
239abb14148SHiral Shah 		memset((void *)fnic_dbg_prt->buffer, 0,
240abb14148SHiral Shah 			3 * (fnic_fc_trace_max_pages * PAGE_SIZE));
241abb14148SHiral Shah 		fnic_dbg_prt->buffer_len =
242abb14148SHiral Shah 			fnic_fc_trace_get_data(fnic_dbg_prt, *rdata_ptr);
243abb14148SHiral Shah 	}
2444d7007b4SHiral Patel 	file->private_data = fnic_dbg_prt;
245abb14148SHiral Shah 
2464d7007b4SHiral Patel 	return 0;
2474d7007b4SHiral Patel }
2484d7007b4SHiral Patel 
2494d7007b4SHiral Patel /*
2504d7007b4SHiral Patel  * fnic_trace_debugfs_lseek - Seek through a debugfs file
2514d7007b4SHiral Patel  * @file: The file pointer to seek through.
2524d7007b4SHiral Patel  * @offset: The offset to seek to or the amount to seek by.
2534d7007b4SHiral Patel  * @howto: Indicates how to seek.
2544d7007b4SHiral Patel  *
2554d7007b4SHiral Patel  * Description:
2564d7007b4SHiral Patel  * This routine is the entry point for the debugfs lseek file operation.
2574d7007b4SHiral Patel  * The @howto parameter indicates whether @offset is the offset to directly
2584d7007b4SHiral Patel  * seek to, or if it is a value to seek forward or reverse by. This function
2594d7007b4SHiral Patel  * figures out what the new offset of the debugfs file will be and assigns
2604d7007b4SHiral Patel  * that value to the f_pos field of @file.
2614d7007b4SHiral Patel  *
2624d7007b4SHiral Patel  * Returns:
2634d7007b4SHiral Patel  * This function returns the new offset if successful and returns a negative
2644d7007b4SHiral Patel  * error if unable to process the seek.
2654d7007b4SHiral Patel  */
2664d7007b4SHiral Patel static loff_t fnic_trace_debugfs_lseek(struct file *file,
2674d7007b4SHiral Patel 					loff_t offset,
2684d7007b4SHiral Patel 					int howto)
2694d7007b4SHiral Patel {
2704d7007b4SHiral Patel 	fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
271eb5881d3SAl Viro 	return fixed_size_llseek(file, offset, howto,
272eb5881d3SAl Viro 				fnic_dbg_prt->buffer_len);
2734d7007b4SHiral Patel }
2744d7007b4SHiral Patel 
2754d7007b4SHiral Patel /*
2764d7007b4SHiral Patel  * fnic_trace_debugfs_read - Read a debugfs file
2774d7007b4SHiral Patel  * @file: The file pointer to read from.
2784d7007b4SHiral Patel  * @ubuf: The buffer to copy the data to.
2794d7007b4SHiral Patel  * @nbytes: The number of bytes to read.
2804d7007b4SHiral Patel  * @pos: The position in the file to start reading from.
2814d7007b4SHiral Patel  *
2824d7007b4SHiral Patel  * Description:
2834d7007b4SHiral Patel  * This routine reads data from the buffer indicated in the private_data
2844d7007b4SHiral Patel  * field of @file. It will start reading at @pos and copy up to @nbytes of
2854d7007b4SHiral Patel  * data to @ubuf.
2864d7007b4SHiral Patel  *
2874d7007b4SHiral Patel  * Returns:
2884d7007b4SHiral Patel  * This function returns the amount of data that was read (this could be
2894d7007b4SHiral Patel  * less than @nbytes if the end of the file was reached).
2904d7007b4SHiral Patel  */
2914d7007b4SHiral Patel static ssize_t fnic_trace_debugfs_read(struct file *file,
2924d7007b4SHiral Patel 					char __user *ubuf,
2934d7007b4SHiral Patel 					size_t nbytes,
2944d7007b4SHiral Patel 					loff_t *pos)
2954d7007b4SHiral Patel {
2964d7007b4SHiral Patel 	fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
2974d7007b4SHiral Patel 	int rc = 0;
2984d7007b4SHiral Patel 	rc = simple_read_from_buffer(ubuf, nbytes, pos,
2994d7007b4SHiral Patel 				  fnic_dbg_prt->buffer,
3004d7007b4SHiral Patel 				  fnic_dbg_prt->buffer_len);
3014d7007b4SHiral Patel 	return rc;
3024d7007b4SHiral Patel }
3034d7007b4SHiral Patel 
3044d7007b4SHiral Patel /*
3054d7007b4SHiral Patel  * fnic_trace_debugfs_release - Release the buffer used to store
3064d7007b4SHiral Patel  * debugfs file data
3074d7007b4SHiral Patel  * @inode: The inode pointer
3084d7007b4SHiral Patel  * @file: The file pointer that contains the buffer to release
3094d7007b4SHiral Patel  *
3104d7007b4SHiral Patel  * Description:
3114d7007b4SHiral Patel  * This routine frees the buffer that was allocated when the debugfs
3124d7007b4SHiral Patel  * file was opened.
3134d7007b4SHiral Patel  *
3144d7007b4SHiral Patel  * Returns:
3154d7007b4SHiral Patel  * This function returns zero.
3164d7007b4SHiral Patel  */
3174d7007b4SHiral Patel static int fnic_trace_debugfs_release(struct inode *inode,
3184d7007b4SHiral Patel 					  struct file *file)
3194d7007b4SHiral Patel {
3204d7007b4SHiral Patel 	fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
3214d7007b4SHiral Patel 
3224d7007b4SHiral Patel 	vfree(fnic_dbg_prt->buffer);
3234d7007b4SHiral Patel 	kfree(fnic_dbg_prt);
3244d7007b4SHiral Patel 	return 0;
3254d7007b4SHiral Patel }
3264d7007b4SHiral Patel 
3274d7007b4SHiral Patel static const struct file_operations fnic_trace_debugfs_fops = {
3284d7007b4SHiral Patel 	.owner = THIS_MODULE,
3294d7007b4SHiral Patel 	.open = fnic_trace_debugfs_open,
3304d7007b4SHiral Patel 	.llseek = fnic_trace_debugfs_lseek,
3314d7007b4SHiral Patel 	.read = fnic_trace_debugfs_read,
3324d7007b4SHiral Patel 	.release = fnic_trace_debugfs_release,
3334d7007b4SHiral Patel };
3344d7007b4SHiral Patel 
3354d7007b4SHiral Patel /*
3364d7007b4SHiral Patel  * fnic_trace_debugfs_init - Initialize debugfs for fnic trace logging
3374d7007b4SHiral Patel  *
3384d7007b4SHiral Patel  * Description:
3394d7007b4SHiral Patel  * When Debugfs is configured this routine sets up the fnic debugfs
3404d7007b4SHiral Patel  * file system. If not already created, this routine will create the
34167125b02SHiral Patel  * create file trace to log fnic trace buffer output into debugfs and
34267125b02SHiral Patel  * it will also create file trace_enable to control enable/disable of
34367125b02SHiral Patel  * trace logging into trace buffer.
3444d7007b4SHiral Patel  */
345*1dbaa379SGreg Kroah-Hartman void fnic_trace_debugfs_init(void)
3464d7007b4SHiral Patel {
3474d7007b4SHiral Patel 	fnic_trace_enable = debugfs_create_file("tracing_enable",
3484d7007b4SHiral Patel 					S_IFREG|S_IRUGO|S_IWUSR,
3494d7007b4SHiral Patel 					fnic_trace_debugfs_root,
350abb14148SHiral Shah 					&(fc_trc_flag->fnic_trace),
351abb14148SHiral Shah 					&fnic_trace_ctrl_fops);
3524d7007b4SHiral Patel 
3534d7007b4SHiral Patel 	fnic_trace_debugfs_file = debugfs_create_file("trace",
3544d7007b4SHiral Patel 					S_IFREG|S_IRUGO|S_IWUSR,
3554d7007b4SHiral Patel 					fnic_trace_debugfs_root,
356abb14148SHiral Shah 					&(fc_trc_flag->fnic_trace),
3574d7007b4SHiral Patel 					&fnic_trace_debugfs_fops);
3584d7007b4SHiral Patel }
3594d7007b4SHiral Patel 
3604d7007b4SHiral Patel /*
3614d7007b4SHiral Patel  * fnic_trace_debugfs_terminate - Tear down debugfs infrastructure
3624d7007b4SHiral Patel  *
3634d7007b4SHiral Patel  * Description:
3644d7007b4SHiral Patel  * When Debugfs is configured this routine removes debugfs file system
3654d7007b4SHiral Patel  * elements that are specific to fnic trace logging.
3664d7007b4SHiral Patel  */
3674d7007b4SHiral Patel void fnic_trace_debugfs_terminate(void)
3684d7007b4SHiral Patel {
3694d7007b4SHiral Patel 	debugfs_remove(fnic_trace_debugfs_file);
3704d7007b4SHiral Patel 	fnic_trace_debugfs_file = NULL;
371abb14148SHiral Shah 
3724d7007b4SHiral Patel 	debugfs_remove(fnic_trace_enable);
3734d7007b4SHiral Patel 	fnic_trace_enable = NULL;
3744d7007b4SHiral Patel }
375abb14148SHiral Shah 
376abb14148SHiral Shah /*
377abb14148SHiral Shah  * fnic_fc_trace_debugfs_init -
378abb14148SHiral Shah  * Initialize debugfs for fnic control frame trace logging
379abb14148SHiral Shah  *
380abb14148SHiral Shah  * Description:
381abb14148SHiral Shah  * When Debugfs is configured this routine sets up the fnic_fc debugfs
382abb14148SHiral Shah  * file system. If not already created, this routine will create the
383abb14148SHiral Shah  * create file trace to log fnic fc trace buffer output into debugfs and
384abb14148SHiral Shah  * it will also create file fc_trace_enable to control enable/disable of
385abb14148SHiral Shah  * trace logging into trace buffer.
386abb14148SHiral Shah  */
387abb14148SHiral Shah 
388*1dbaa379SGreg Kroah-Hartman void fnic_fc_trace_debugfs_init(void)
389abb14148SHiral Shah {
390abb14148SHiral Shah 	fnic_fc_trace_enable = debugfs_create_file("fc_trace_enable",
391abb14148SHiral Shah 					S_IFREG|S_IRUGO|S_IWUSR,
392abb14148SHiral Shah 					fnic_trace_debugfs_root,
393abb14148SHiral Shah 					&(fc_trc_flag->fc_trace),
394abb14148SHiral Shah 					&fnic_trace_ctrl_fops);
395abb14148SHiral Shah 
396abb14148SHiral Shah 	fnic_fc_trace_clear = debugfs_create_file("fc_trace_clear",
397abb14148SHiral Shah 					S_IFREG|S_IRUGO|S_IWUSR,
398abb14148SHiral Shah 					fnic_trace_debugfs_root,
399abb14148SHiral Shah 					&(fc_trc_flag->fc_clear),
400abb14148SHiral Shah 					&fnic_trace_ctrl_fops);
401abb14148SHiral Shah 
402abb14148SHiral Shah 	fnic_fc_rdata_trace_debugfs_file =
403abb14148SHiral Shah 		debugfs_create_file("fc_trace_rdata",
404abb14148SHiral Shah 				    S_IFREG|S_IRUGO|S_IWUSR,
405abb14148SHiral Shah 				    fnic_trace_debugfs_root,
406abb14148SHiral Shah 				    &(fc_trc_flag->fc_normal_file),
407abb14148SHiral Shah 				    &fnic_trace_debugfs_fops);
408abb14148SHiral Shah 
409abb14148SHiral Shah 	fnic_fc_trace_debugfs_file =
410abb14148SHiral Shah 		debugfs_create_file("fc_trace",
411abb14148SHiral Shah 				    S_IFREG|S_IRUGO|S_IWUSR,
412abb14148SHiral Shah 				    fnic_trace_debugfs_root,
413abb14148SHiral Shah 				    &(fc_trc_flag->fc_row_file),
414abb14148SHiral Shah 				    &fnic_trace_debugfs_fops);
415abb14148SHiral Shah }
416abb14148SHiral Shah 
417abb14148SHiral Shah /*
418abb14148SHiral Shah  * fnic_fc_trace_debugfs_terminate - Tear down debugfs infrastructure
419abb14148SHiral Shah  *
420abb14148SHiral Shah  * Description:
421abb14148SHiral Shah  * When Debugfs is configured this routine removes debugfs file system
422abb14148SHiral Shah  * elements that are specific to fnic_fc trace logging.
423abb14148SHiral Shah  */
424abb14148SHiral Shah 
425abb14148SHiral Shah void fnic_fc_trace_debugfs_terminate(void)
426abb14148SHiral Shah {
427abb14148SHiral Shah 	debugfs_remove(fnic_fc_trace_debugfs_file);
428abb14148SHiral Shah 	fnic_fc_trace_debugfs_file = NULL;
429abb14148SHiral Shah 
430abb14148SHiral Shah 	debugfs_remove(fnic_fc_rdata_trace_debugfs_file);
431abb14148SHiral Shah 	fnic_fc_rdata_trace_debugfs_file = NULL;
432abb14148SHiral Shah 
433abb14148SHiral Shah 	debugfs_remove(fnic_fc_trace_enable);
434abb14148SHiral Shah 	fnic_fc_trace_enable = NULL;
435abb14148SHiral Shah 
436abb14148SHiral Shah 	debugfs_remove(fnic_fc_trace_clear);
437abb14148SHiral Shah 	fnic_fc_trace_clear = NULL;
4384d7007b4SHiral Patel }
43967125b02SHiral Patel 
44067125b02SHiral Patel /*
44167125b02SHiral Patel  * fnic_reset_stats_open - Open the reset_stats file
44267125b02SHiral Patel  * @inode: The inode pointer.
44367125b02SHiral Patel  * @file: The file pointer to attach the stats reset flag.
44467125b02SHiral Patel  *
44567125b02SHiral Patel  * Description:
44667125b02SHiral Patel  * This routine opens a debugsfs file reset_stats and stores i_private data
44767125b02SHiral Patel  * to debug structure to retrieve later for while performing other
44867125b02SHiral Patel  * file oprations.
44967125b02SHiral Patel  *
45067125b02SHiral Patel  * Returns:
45167125b02SHiral Patel  * This function returns zero if successful.
45267125b02SHiral Patel  */
45367125b02SHiral Patel static int fnic_reset_stats_open(struct inode *inode, struct file *file)
45467125b02SHiral Patel {
45567125b02SHiral Patel 	struct stats_debug_info *debug;
45667125b02SHiral Patel 
45767125b02SHiral Patel 	debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
45867125b02SHiral Patel 	if (!debug)
45967125b02SHiral Patel 		return -ENOMEM;
46067125b02SHiral Patel 
46167125b02SHiral Patel 	debug->i_private = inode->i_private;
46267125b02SHiral Patel 
46367125b02SHiral Patel 	file->private_data = debug;
46467125b02SHiral Patel 
46567125b02SHiral Patel 	return 0;
46667125b02SHiral Patel }
46767125b02SHiral Patel 
46867125b02SHiral Patel /*
46967125b02SHiral Patel  * fnic_reset_stats_read - Read a reset_stats debugfs file
47067125b02SHiral Patel  * @filp: The file pointer to read from.
47167125b02SHiral Patel  * @ubuf: The buffer to copy the data to.
47267125b02SHiral Patel  * @cnt: The number of bytes to read.
47367125b02SHiral Patel  * @ppos: The position in the file to start reading from.
47467125b02SHiral Patel  *
47567125b02SHiral Patel  * Description:
47667125b02SHiral Patel  * This routine reads value of variable reset_stats
47767125b02SHiral Patel  * and stores into local @buf. It will start reading file at @ppos and
47867125b02SHiral Patel  * copy up to @cnt of data to @ubuf from @buf.
47967125b02SHiral Patel  *
48067125b02SHiral Patel  * Returns:
48167125b02SHiral Patel  * This function returns the amount of data that was read.
48267125b02SHiral Patel  */
48367125b02SHiral Patel static ssize_t fnic_reset_stats_read(struct file *file,
48467125b02SHiral Patel 					char __user *ubuf,
48567125b02SHiral Patel 					size_t cnt, loff_t *ppos)
48667125b02SHiral Patel {
48767125b02SHiral Patel 	struct stats_debug_info *debug = file->private_data;
48867125b02SHiral Patel 	struct fnic *fnic = (struct fnic *)debug->i_private;
48967125b02SHiral Patel 	char buf[64];
49067125b02SHiral Patel 	int len;
49167125b02SHiral Patel 
49267125b02SHiral Patel 	len = sprintf(buf, "%u\n", fnic->reset_stats);
49367125b02SHiral Patel 
49467125b02SHiral Patel 	return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
49567125b02SHiral Patel }
49667125b02SHiral Patel 
49767125b02SHiral Patel /*
49867125b02SHiral Patel  * fnic_reset_stats_write - Write to reset_stats debugfs file
49967125b02SHiral Patel  * @filp: The file pointer to write from.
50067125b02SHiral Patel  * @ubuf: The buffer to copy the data from.
50167125b02SHiral Patel  * @cnt: The number of bytes to write.
50267125b02SHiral Patel  * @ppos: The position in the file to start writing to.
50367125b02SHiral Patel  *
50467125b02SHiral Patel  * Description:
50567125b02SHiral Patel  * This routine writes data from user buffer @ubuf to buffer @buf and
50667125b02SHiral Patel  * resets cumulative stats of fnic.
50767125b02SHiral Patel  *
50867125b02SHiral Patel  * Returns:
50967125b02SHiral Patel  * This function returns the amount of data that was written.
51067125b02SHiral Patel  */
51167125b02SHiral Patel static ssize_t fnic_reset_stats_write(struct file *file,
51267125b02SHiral Patel 					const char __user *ubuf,
51367125b02SHiral Patel 					size_t cnt, loff_t *ppos)
51467125b02SHiral Patel {
51567125b02SHiral Patel 	struct stats_debug_info *debug = file->private_data;
51667125b02SHiral Patel 	struct fnic *fnic = (struct fnic *)debug->i_private;
51767125b02SHiral Patel 	struct fnic_stats *stats = &fnic->fnic_stats;
51867125b02SHiral Patel 	u64 *io_stats_p = (u64 *)&stats->io_stats;
51967125b02SHiral Patel 	u64 *fw_stats_p = (u64 *)&stats->fw_stats;
52067125b02SHiral Patel 	char buf[64];
52167125b02SHiral Patel 	unsigned long val;
52267125b02SHiral Patel 	int ret;
52367125b02SHiral Patel 
52467125b02SHiral Patel 	if (cnt >= sizeof(buf))
52567125b02SHiral Patel 		return -EINVAL;
52667125b02SHiral Patel 
52767125b02SHiral Patel 	if (copy_from_user(&buf, ubuf, cnt))
52867125b02SHiral Patel 		return -EFAULT;
52967125b02SHiral Patel 
53067125b02SHiral Patel 	buf[cnt] = 0;
53167125b02SHiral Patel 
53267125b02SHiral Patel 	ret = kstrtoul(buf, 10, &val);
53367125b02SHiral Patel 	if (ret < 0)
53467125b02SHiral Patel 		return ret;
53567125b02SHiral Patel 
53667125b02SHiral Patel 	fnic->reset_stats = val;
53767125b02SHiral Patel 
53867125b02SHiral Patel 	if (fnic->reset_stats) {
53967125b02SHiral Patel 		/* Skip variable is used to avoid descrepancies to Num IOs
54067125b02SHiral Patel 		 * and IO Completions stats. Skip incrementing No IO Compls
54167125b02SHiral Patel 		 * for pending active IOs after reset stats
54267125b02SHiral Patel 		 */
54367125b02SHiral Patel 		atomic64_set(&fnic->io_cmpl_skip,
54467125b02SHiral Patel 			atomic64_read(&stats->io_stats.active_ios));
54567125b02SHiral Patel 		memset(&stats->abts_stats, 0, sizeof(struct abort_stats));
54667125b02SHiral Patel 		memset(&stats->term_stats, 0,
54767125b02SHiral Patel 			sizeof(struct terminate_stats));
54867125b02SHiral Patel 		memset(&stats->reset_stats, 0, sizeof(struct reset_stats));
54967125b02SHiral Patel 		memset(&stats->misc_stats, 0, sizeof(struct misc_stats));
55067125b02SHiral Patel 		memset(&stats->vlan_stats, 0, sizeof(struct vlan_stats));
55167125b02SHiral Patel 		memset(io_stats_p+1, 0,
55267125b02SHiral Patel 			sizeof(struct io_path_stats) - sizeof(u64));
55367125b02SHiral Patel 		memset(fw_stats_p+1, 0,
55467125b02SHiral Patel 			sizeof(struct fw_stats) - sizeof(u64));
55522807aa8SArnd Bergmann 		ktime_get_real_ts64(&stats->stats_timestamps.last_reset_time);
55667125b02SHiral Patel 	}
55767125b02SHiral Patel 
55867125b02SHiral Patel 	(*ppos)++;
55967125b02SHiral Patel 	return cnt;
56067125b02SHiral Patel }
56167125b02SHiral Patel 
56267125b02SHiral Patel /*
56367125b02SHiral Patel  * fnic_reset_stats_release - Release the buffer used to store
56467125b02SHiral Patel  * debugfs file data
56567125b02SHiral Patel  * @inode: The inode pointer
56667125b02SHiral Patel  * @file: The file pointer that contains the buffer to release
56767125b02SHiral Patel  *
56867125b02SHiral Patel  * Description:
56967125b02SHiral Patel  * This routine frees the buffer that was allocated when the debugfs
57067125b02SHiral Patel  * file was opened.
57167125b02SHiral Patel  *
57267125b02SHiral Patel  * Returns:
57367125b02SHiral Patel  * This function returns zero.
57467125b02SHiral Patel  */
57567125b02SHiral Patel static int fnic_reset_stats_release(struct inode *inode,
57667125b02SHiral Patel 					struct file *file)
57767125b02SHiral Patel {
57867125b02SHiral Patel 	struct stats_debug_info *debug = file->private_data;
57967125b02SHiral Patel 	kfree(debug);
58067125b02SHiral Patel 	return 0;
58167125b02SHiral Patel }
58267125b02SHiral Patel 
58367125b02SHiral Patel /*
58467125b02SHiral Patel  * fnic_stats_debugfs_open - Open the stats file for specific host
58567125b02SHiral Patel  * and get fnic stats.
58667125b02SHiral Patel  * @inode: The inode pointer.
58767125b02SHiral Patel  * @file: The file pointer to attach the specific host statistics.
58867125b02SHiral Patel  *
58967125b02SHiral Patel  * Description:
59067125b02SHiral Patel  * This routine opens a debugsfs file stats of specific host and print
59167125b02SHiral Patel  * fnic stats.
59267125b02SHiral Patel  *
59367125b02SHiral Patel  * Returns:
59467125b02SHiral Patel  * This function returns zero if successful.
59567125b02SHiral Patel  */
59667125b02SHiral Patel static int fnic_stats_debugfs_open(struct inode *inode,
59767125b02SHiral Patel 					struct file *file)
59867125b02SHiral Patel {
59967125b02SHiral Patel 	struct fnic *fnic = inode->i_private;
60067125b02SHiral Patel 	struct fnic_stats *fnic_stats = &fnic->fnic_stats;
60167125b02SHiral Patel 	struct stats_debug_info *debug;
60267125b02SHiral Patel 	int buf_size = 2 * PAGE_SIZE;
60367125b02SHiral Patel 
60467125b02SHiral Patel 	debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
60567125b02SHiral Patel 	if (!debug)
60667125b02SHiral Patel 		return -ENOMEM;
60767125b02SHiral Patel 
60867125b02SHiral Patel 	debug->debug_buffer = vmalloc(buf_size);
60967125b02SHiral Patel 	if (!debug->debug_buffer) {
61067125b02SHiral Patel 		kfree(debug);
61167125b02SHiral Patel 		return -ENOMEM;
61267125b02SHiral Patel 	}
61367125b02SHiral Patel 
61467125b02SHiral Patel 	debug->buf_size = buf_size;
61567125b02SHiral Patel 	memset((void *)debug->debug_buffer, 0, buf_size);
61667125b02SHiral Patel 	debug->buffer_len = fnic_get_stats_data(debug, fnic_stats);
61767125b02SHiral Patel 
61867125b02SHiral Patel 	file->private_data = debug;
61967125b02SHiral Patel 
62067125b02SHiral Patel 	return 0;
62167125b02SHiral Patel }
62267125b02SHiral Patel 
62367125b02SHiral Patel /*
62467125b02SHiral Patel  * fnic_stats_debugfs_read - Read a debugfs file
62567125b02SHiral Patel  * @file: The file pointer to read from.
62667125b02SHiral Patel  * @ubuf: The buffer to copy the data to.
62767125b02SHiral Patel  * @nbytes: The number of bytes to read.
62867125b02SHiral Patel  * @pos: The position in the file to start reading from.
62967125b02SHiral Patel  *
63067125b02SHiral Patel  * Description:
63167125b02SHiral Patel  * This routine reads data from the buffer indicated in the private_data
63267125b02SHiral Patel  * field of @file. It will start reading at @pos and copy up to @nbytes of
63367125b02SHiral Patel  * data to @ubuf.
63467125b02SHiral Patel  *
63567125b02SHiral Patel  * Returns:
63667125b02SHiral Patel  * This function returns the amount of data that was read (this could be
63767125b02SHiral Patel  * less than @nbytes if the end of the file was reached).
63867125b02SHiral Patel  */
63967125b02SHiral Patel static ssize_t fnic_stats_debugfs_read(struct file *file,
64067125b02SHiral Patel 					char __user *ubuf,
64167125b02SHiral Patel 					size_t nbytes,
64267125b02SHiral Patel 					loff_t *pos)
64367125b02SHiral Patel {
64467125b02SHiral Patel 	struct stats_debug_info *debug = file->private_data;
64567125b02SHiral Patel 	int rc = 0;
64667125b02SHiral Patel 	rc = simple_read_from_buffer(ubuf, nbytes, pos,
64767125b02SHiral Patel 					debug->debug_buffer,
64867125b02SHiral Patel 					debug->buffer_len);
64967125b02SHiral Patel 	return rc;
65067125b02SHiral Patel }
65167125b02SHiral Patel 
65267125b02SHiral Patel /*
65367125b02SHiral Patel  * fnic_stats_stats_release - Release the buffer used to store
65467125b02SHiral Patel  * debugfs file data
65567125b02SHiral Patel  * @inode: The inode pointer
65667125b02SHiral Patel  * @file: The file pointer that contains the buffer to release
65767125b02SHiral Patel  *
65867125b02SHiral Patel  * Description:
65967125b02SHiral Patel  * This routine frees the buffer that was allocated when the debugfs
66067125b02SHiral Patel  * file was opened.
66167125b02SHiral Patel  *
66267125b02SHiral Patel  * Returns:
66367125b02SHiral Patel  * This function returns zero.
66467125b02SHiral Patel  */
66567125b02SHiral Patel static int fnic_stats_debugfs_release(struct inode *inode,
66667125b02SHiral Patel 					struct file *file)
66767125b02SHiral Patel {
66867125b02SHiral Patel 	struct stats_debug_info *debug = file->private_data;
66967125b02SHiral Patel 	vfree(debug->debug_buffer);
67067125b02SHiral Patel 	kfree(debug);
67167125b02SHiral Patel 	return 0;
67267125b02SHiral Patel }
67367125b02SHiral Patel 
67467125b02SHiral Patel static const struct file_operations fnic_stats_debugfs_fops = {
67567125b02SHiral Patel 	.owner = THIS_MODULE,
67667125b02SHiral Patel 	.open = fnic_stats_debugfs_open,
67767125b02SHiral Patel 	.read = fnic_stats_debugfs_read,
67867125b02SHiral Patel 	.release = fnic_stats_debugfs_release,
67967125b02SHiral Patel };
68067125b02SHiral Patel 
68167125b02SHiral Patel static const struct file_operations fnic_reset_debugfs_fops = {
68267125b02SHiral Patel 	.owner = THIS_MODULE,
68367125b02SHiral Patel 	.open = fnic_reset_stats_open,
68467125b02SHiral Patel 	.read = fnic_reset_stats_read,
68567125b02SHiral Patel 	.write = fnic_reset_stats_write,
68667125b02SHiral Patel 	.release = fnic_reset_stats_release,
68767125b02SHiral Patel };
68867125b02SHiral Patel 
68967125b02SHiral Patel /*
69067125b02SHiral Patel  * fnic_stats_init - Initialize stats struct and create stats file per fnic
69167125b02SHiral Patel  *
69267125b02SHiral Patel  * Description:
69367125b02SHiral Patel  * When Debugfs is configured this routine sets up the stats file per fnic
69467125b02SHiral Patel  * It will create file stats and reset_stats under statistics/host# directory
69567125b02SHiral Patel  * to log per fnic stats.
69667125b02SHiral Patel  */
697*1dbaa379SGreg Kroah-Hartman void fnic_stats_debugfs_init(struct fnic *fnic)
69867125b02SHiral Patel {
69967125b02SHiral Patel 	char name[16];
70067125b02SHiral Patel 
70167125b02SHiral Patel 	snprintf(name, sizeof(name), "host%d", fnic->lport->host->host_no);
70267125b02SHiral Patel 
70367125b02SHiral Patel 	fnic->fnic_stats_debugfs_host = debugfs_create_dir(name,
70467125b02SHiral Patel 						fnic_stats_debugfs_root);
70567125b02SHiral Patel 
70667125b02SHiral Patel 	fnic->fnic_stats_debugfs_file = debugfs_create_file("stats",
70767125b02SHiral Patel 						S_IFREG|S_IRUGO|S_IWUSR,
70867125b02SHiral Patel 						fnic->fnic_stats_debugfs_host,
70967125b02SHiral Patel 						fnic,
71067125b02SHiral Patel 						&fnic_stats_debugfs_fops);
71167125b02SHiral Patel 
71267125b02SHiral Patel 	fnic->fnic_reset_debugfs_file = debugfs_create_file("reset_stats",
71367125b02SHiral Patel 						S_IFREG|S_IRUGO|S_IWUSR,
71467125b02SHiral Patel 						fnic->fnic_stats_debugfs_host,
71567125b02SHiral Patel 						fnic,
71667125b02SHiral Patel 						&fnic_reset_debugfs_fops);
71767125b02SHiral Patel }
71867125b02SHiral Patel 
71967125b02SHiral Patel /*
72067125b02SHiral Patel  * fnic_stats_debugfs_remove - Tear down debugfs infrastructure of stats
72167125b02SHiral Patel  *
72267125b02SHiral Patel  * Description:
72367125b02SHiral Patel  * When Debugfs is configured this routine removes debugfs file system
72467125b02SHiral Patel  * elements that are specific to fnic stats.
72567125b02SHiral Patel  */
72667125b02SHiral Patel void fnic_stats_debugfs_remove(struct fnic *fnic)
72767125b02SHiral Patel {
72867125b02SHiral Patel 	if (!fnic)
72967125b02SHiral Patel 		return;
73067125b02SHiral Patel 
73167125b02SHiral Patel 	debugfs_remove(fnic->fnic_stats_debugfs_file);
73267125b02SHiral Patel 	fnic->fnic_stats_debugfs_file = NULL;
73367125b02SHiral Patel 
73467125b02SHiral Patel 	debugfs_remove(fnic->fnic_reset_debugfs_file);
73567125b02SHiral Patel 	fnic->fnic_reset_debugfs_file = NULL;
73667125b02SHiral Patel 
73767125b02SHiral Patel 	debugfs_remove(fnic->fnic_stats_debugfs_host);
73867125b02SHiral Patel 	fnic->fnic_stats_debugfs_host = NULL;
7394d7007b4SHiral Patel }
740