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