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