]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/scsi/fnic/fnic_debugfs.c
6d3e1cb4fea61f5eebb8ac976d6da79b24908f9a
[linux.git] / drivers / scsi / fnic / fnic_debugfs.c
1 /*
2  * Copyright 2012 Cisco Systems, Inc.  All rights reserved.
3  *
4  * This program is free software; you may redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
9  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
10  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
11  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
12  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
13  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
14  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
15  * SOFTWARE.
16  */
17
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/debugfs.h>
21 #include <linux/vmalloc.h>
22 #include "fnic.h"
23
24 static struct dentry *fnic_trace_debugfs_root;
25 static struct dentry *fnic_trace_debugfs_file;
26 static struct dentry *fnic_trace_enable;
27 static struct dentry *fnic_stats_debugfs_root;
28
29 static struct dentry *fnic_fc_trace_debugfs_file;
30 static struct dentry *fnic_fc_rdata_trace_debugfs_file;
31 static struct dentry *fnic_fc_trace_enable;
32 static struct dentry *fnic_fc_trace_clear;
33
34 struct fc_trace_flag_type {
35         u8 fc_row_file;
36         u8 fc_normal_file;
37         u8 fnic_trace;
38         u8 fc_trace;
39         u8 fc_clear;
40 };
41
42 static struct fc_trace_flag_type *fc_trc_flag;
43
44 /*
45  * fnic_debugfs_init - Initialize debugfs for fnic debug logging
46  *
47  * Description:
48  * When Debugfs is configured this routine sets up the fnic debugfs
49  * file system. If not already created, this routine will create the
50  * fnic directory and statistics directory for trace buffer and
51  * stats logging.
52  */
53 int fnic_debugfs_init(void)
54 {
55         int rc = -1;
56         fnic_trace_debugfs_root = debugfs_create_dir("fnic", NULL);
57         if (!fnic_trace_debugfs_root) {
58                 printk(KERN_DEBUG "Cannot create debugfs root\n");
59                 return rc;
60         }
61
62         if (!fnic_trace_debugfs_root) {
63                 printk(KERN_DEBUG
64                         "fnic root directory doesn't exist in debugfs\n");
65                 return rc;
66         }
67
68         fnic_stats_debugfs_root = debugfs_create_dir("statistics",
69                                                 fnic_trace_debugfs_root);
70         if (!fnic_stats_debugfs_root) {
71                 printk(KERN_DEBUG "Cannot create Statistics directory\n");
72                 return rc;
73         }
74
75         /* Allocate memory to structure */
76         fc_trc_flag = (struct fc_trace_flag_type *)
77                 vmalloc(sizeof(struct fc_trace_flag_type));
78
79         if (fc_trc_flag) {
80                 fc_trc_flag->fc_row_file = 0;
81                 fc_trc_flag->fc_normal_file = 1;
82                 fc_trc_flag->fnic_trace = 2;
83                 fc_trc_flag->fc_trace = 3;
84                 fc_trc_flag->fc_clear = 4;
85         }
86
87         rc = 0;
88         return rc;
89 }
90
91 /*
92  * fnic_debugfs_terminate - Tear down debugfs infrastructure
93  *
94  * Description:
95  * When Debugfs is configured this routine removes debugfs file system
96  * elements that are specific to fnic.
97  */
98 void fnic_debugfs_terminate(void)
99 {
100         debugfs_remove(fnic_stats_debugfs_root);
101         fnic_stats_debugfs_root = NULL;
102
103         debugfs_remove(fnic_trace_debugfs_root);
104         fnic_trace_debugfs_root = NULL;
105
106         if (fc_trc_flag)
107                 vfree(fc_trc_flag);
108 }
109
110 /*
111  * fnic_trace_ctrl_read -
112  *          Read  trace_enable ,fc_trace_enable
113  *              or fc_trace_clear debugfs file
114  * @filp: The file pointer to read from.
115  * @ubuf: The buffer to copy the data to.
116  * @cnt: The number of bytes to read.
117  * @ppos: The position in the file to start reading from.
118  *
119  * Description:
120  * This routine reads value of variable fnic_tracing_enabled or
121  * fnic_fc_tracing_enabled or fnic_fc_trace_cleared
122  * and stores into local @buf.
123  * It will start reading file at @ppos and
124  * copy up to @cnt of data to @ubuf from @buf.
125  *
126  * Returns:
127  * This function returns the amount of data that was read.
128  */
129 static ssize_t fnic_trace_ctrl_read(struct file *filp,
130                                   char __user *ubuf,
131                                   size_t cnt, loff_t *ppos)
132 {
133         char buf[64];
134         int len;
135         u8 *trace_type;
136         len = 0;
137         trace_type = (u8 *)filp->private_data;
138         if (*trace_type == fc_trc_flag->fnic_trace)
139                 len = sprintf(buf, "%u\n", fnic_tracing_enabled);
140         else if (*trace_type == fc_trc_flag->fc_trace)
141                 len = sprintf(buf, "%u\n", fnic_fc_tracing_enabled);
142         else if (*trace_type == fc_trc_flag->fc_clear)
143                 len = sprintf(buf, "%u\n", fnic_fc_trace_cleared);
144         else
145                 pr_err("fnic: Cannot read to any debugfs file\n");
146
147         return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
148 }
149
150 /*
151  * fnic_trace_ctrl_write -
152  * Write to trace_enable, fc_trace_enable or
153  *         fc_trace_clear debugfs file
154  * @filp: The file pointer to write from.
155  * @ubuf: The buffer to copy the data from.
156  * @cnt: The number of bytes to write.
157  * @ppos: The position in the file to start writing to.
158  *
159  * Description:
160  * This routine writes data from user buffer @ubuf to buffer @buf and
161  * sets fc_trace_enable ,tracing_enable or fnic_fc_trace_cleared
162  * value as per user input.
163  *
164  * Returns:
165  * This function returns the amount of data that was written.
166  */
167 static ssize_t fnic_trace_ctrl_write(struct file *filp,
168                                   const char __user *ubuf,
169                                   size_t cnt, loff_t *ppos)
170 {
171         char buf[64];
172         unsigned long val;
173         int ret;
174         u8 *trace_type;
175         trace_type = (u8 *)filp->private_data;
176
177         if (cnt >= sizeof(buf))
178                 return -EINVAL;
179
180         if (copy_from_user(&buf, ubuf, cnt))
181                 return -EFAULT;
182
183         buf[cnt] = 0;
184
185         ret = kstrtoul(buf, 10, &val);
186         if (ret < 0)
187                 return ret;
188
189         if (*trace_type == fc_trc_flag->fnic_trace)
190                 fnic_tracing_enabled = val;
191         else if (*trace_type == fc_trc_flag->fc_trace)
192                 fnic_fc_tracing_enabled = val;
193         else if (*trace_type == fc_trc_flag->fc_clear)
194                 fnic_fc_trace_cleared = val;
195         else
196                 pr_err("fnic: cannot write to any debugfs file\n");
197
198         (*ppos)++;
199
200         return cnt;
201 }
202
203 static const struct file_operations fnic_trace_ctrl_fops = {
204         .owner = THIS_MODULE,
205         .open = simple_open,
206         .read = fnic_trace_ctrl_read,
207         .write = fnic_trace_ctrl_write,
208 };
209
210 /*
211  * fnic_trace_debugfs_open - Open the fnic trace log
212  * @inode: The inode pointer
213  * @file: The file pointer to attach the log output
214  *
215  * Description:
216  * This routine is the entry point for the debugfs open file operation.
217  * It allocates the necessary buffer for the log, fills the buffer from
218  * the in-memory log and then returns a pointer to that log in
219  * the private_data field in @file.
220  *
221  * Returns:
222  * This function returns zero if successful. On error it will return
223  * a negative error value.
224  */
225 static int fnic_trace_debugfs_open(struct inode *inode,
226                                   struct file *file)
227 {
228         fnic_dbgfs_t *fnic_dbg_prt;
229         u8 *rdata_ptr;
230         rdata_ptr = (u8 *)inode->i_private;
231         fnic_dbg_prt = kzalloc(sizeof(fnic_dbgfs_t), GFP_KERNEL);
232         if (!fnic_dbg_prt)
233                 return -ENOMEM;
234
235         if (*rdata_ptr == fc_trc_flag->fnic_trace) {
236                 fnic_dbg_prt->buffer = vmalloc(3 *
237                                         (trace_max_pages * PAGE_SIZE));
238                 if (!fnic_dbg_prt->buffer) {
239                         kfree(fnic_dbg_prt);
240                         return -ENOMEM;
241                 }
242                 memset((void *)fnic_dbg_prt->buffer, 0,
243                 3 * (trace_max_pages * PAGE_SIZE));
244                 fnic_dbg_prt->buffer_len = fnic_get_trace_data(fnic_dbg_prt);
245         } else {
246                 fnic_dbg_prt->buffer =
247                         vmalloc(3 * (fnic_fc_trace_max_pages * PAGE_SIZE));
248                 if (!fnic_dbg_prt->buffer) {
249                         kfree(fnic_dbg_prt);
250                         return -ENOMEM;
251                 }
252                 memset((void *)fnic_dbg_prt->buffer, 0,
253                         3 * (fnic_fc_trace_max_pages * PAGE_SIZE));
254                 fnic_dbg_prt->buffer_len =
255                         fnic_fc_trace_get_data(fnic_dbg_prt, *rdata_ptr);
256         }
257         file->private_data = fnic_dbg_prt;
258
259         return 0;
260 }
261
262 /*
263  * fnic_trace_debugfs_lseek - Seek through a debugfs file
264  * @file: The file pointer to seek through.
265  * @offset: The offset to seek to or the amount to seek by.
266  * @howto: Indicates how to seek.
267  *
268  * Description:
269  * This routine is the entry point for the debugfs lseek file operation.
270  * The @howto parameter indicates whether @offset is the offset to directly
271  * seek to, or if it is a value to seek forward or reverse by. This function
272  * figures out what the new offset of the debugfs file will be and assigns
273  * that value to the f_pos field of @file.
274  *
275  * Returns:
276  * This function returns the new offset if successful and returns a negative
277  * error if unable to process the seek.
278  */
279 static loff_t fnic_trace_debugfs_lseek(struct file *file,
280                                         loff_t offset,
281                                         int howto)
282 {
283         fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
284         return fixed_size_llseek(file, offset, howto,
285                                 fnic_dbg_prt->buffer_len);
286 }
287
288 /*
289  * fnic_trace_debugfs_read - Read a debugfs file
290  * @file: The file pointer to read from.
291  * @ubuf: The buffer to copy the data to.
292  * @nbytes: The number of bytes to read.
293  * @pos: The position in the file to start reading from.
294  *
295  * Description:
296  * This routine reads data from the buffer indicated in the private_data
297  * field of @file. It will start reading at @pos and copy up to @nbytes of
298  * data to @ubuf.
299  *
300  * Returns:
301  * This function returns the amount of data that was read (this could be
302  * less than @nbytes if the end of the file was reached).
303  */
304 static ssize_t fnic_trace_debugfs_read(struct file *file,
305                                         char __user *ubuf,
306                                         size_t nbytes,
307                                         loff_t *pos)
308 {
309         fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
310         int rc = 0;
311         rc = simple_read_from_buffer(ubuf, nbytes, pos,
312                                   fnic_dbg_prt->buffer,
313                                   fnic_dbg_prt->buffer_len);
314         return rc;
315 }
316
317 /*
318  * fnic_trace_debugfs_release - Release the buffer used to store
319  * debugfs file data
320  * @inode: The inode pointer
321  * @file: The file pointer that contains the buffer to release
322  *
323  * Description:
324  * This routine frees the buffer that was allocated when the debugfs
325  * file was opened.
326  *
327  * Returns:
328  * This function returns zero.
329  */
330 static int fnic_trace_debugfs_release(struct inode *inode,
331                                           struct file *file)
332 {
333         fnic_dbgfs_t *fnic_dbg_prt = file->private_data;
334
335         vfree(fnic_dbg_prt->buffer);
336         kfree(fnic_dbg_prt);
337         return 0;
338 }
339
340 static const struct file_operations fnic_trace_debugfs_fops = {
341         .owner = THIS_MODULE,
342         .open = fnic_trace_debugfs_open,
343         .llseek = fnic_trace_debugfs_lseek,
344         .read = fnic_trace_debugfs_read,
345         .release = fnic_trace_debugfs_release,
346 };
347
348 /*
349  * fnic_trace_debugfs_init - Initialize debugfs for fnic trace logging
350  *
351  * Description:
352  * When Debugfs is configured this routine sets up the fnic debugfs
353  * file system. If not already created, this routine will create the
354  * create file trace to log fnic trace buffer output into debugfs and
355  * it will also create file trace_enable to control enable/disable of
356  * trace logging into trace buffer.
357  */
358 int fnic_trace_debugfs_init(void)
359 {
360         int rc = -1;
361         if (!fnic_trace_debugfs_root) {
362                 printk(KERN_DEBUG
363                         "FNIC Debugfs root directory doesn't exist\n");
364                 return rc;
365         }
366         fnic_trace_enable = debugfs_create_file("tracing_enable",
367                                         S_IFREG|S_IRUGO|S_IWUSR,
368                                         fnic_trace_debugfs_root,
369                                         &(fc_trc_flag->fnic_trace),
370                                         &fnic_trace_ctrl_fops);
371
372         if (!fnic_trace_enable) {
373                 printk(KERN_DEBUG
374                         "Cannot create trace_enable file under debugfs\n");
375                 return rc;
376         }
377
378         fnic_trace_debugfs_file = debugfs_create_file("trace",
379                                         S_IFREG|S_IRUGO|S_IWUSR,
380                                         fnic_trace_debugfs_root,
381                                         &(fc_trc_flag->fnic_trace),
382                                         &fnic_trace_debugfs_fops);
383
384         if (!fnic_trace_debugfs_file) {
385                 printk(KERN_DEBUG
386                         "Cannot create trace file under debugfs\n");
387                 return rc;
388         }
389         rc = 0;
390         return rc;
391 }
392
393 /*
394  * fnic_trace_debugfs_terminate - Tear down debugfs infrastructure
395  *
396  * Description:
397  * When Debugfs is configured this routine removes debugfs file system
398  * elements that are specific to fnic trace logging.
399  */
400 void fnic_trace_debugfs_terminate(void)
401 {
402         debugfs_remove(fnic_trace_debugfs_file);
403         fnic_trace_debugfs_file = NULL;
404
405         debugfs_remove(fnic_trace_enable);
406         fnic_trace_enable = NULL;
407 }
408
409 /*
410  * fnic_fc_trace_debugfs_init -
411  * Initialize debugfs for fnic control frame trace logging
412  *
413  * Description:
414  * When Debugfs is configured this routine sets up the fnic_fc debugfs
415  * file system. If not already created, this routine will create the
416  * create file trace to log fnic fc trace buffer output into debugfs and
417  * it will also create file fc_trace_enable to control enable/disable of
418  * trace logging into trace buffer.
419  */
420
421 int fnic_fc_trace_debugfs_init(void)
422 {
423         int rc = -1;
424
425         if (!fnic_trace_debugfs_root) {
426                 pr_err("fnic:Debugfs root directory doesn't exist\n");
427                 return rc;
428         }
429
430         fnic_fc_trace_enable = debugfs_create_file("fc_trace_enable",
431                                         S_IFREG|S_IRUGO|S_IWUSR,
432                                         fnic_trace_debugfs_root,
433                                         &(fc_trc_flag->fc_trace),
434                                         &fnic_trace_ctrl_fops);
435
436         if (!fnic_fc_trace_enable) {
437                 pr_err("fnic: Failed create fc_trace_enable file\n");
438                 return rc;
439         }
440
441         fnic_fc_trace_clear = debugfs_create_file("fc_trace_clear",
442                                         S_IFREG|S_IRUGO|S_IWUSR,
443                                         fnic_trace_debugfs_root,
444                                         &(fc_trc_flag->fc_clear),
445                                         &fnic_trace_ctrl_fops);
446
447         if (!fnic_fc_trace_clear) {
448                 pr_err("fnic: Failed to create fc_trace_enable file\n");
449                 return rc;
450         }
451
452         fnic_fc_rdata_trace_debugfs_file =
453                 debugfs_create_file("fc_trace_rdata",
454                                     S_IFREG|S_IRUGO|S_IWUSR,
455                                     fnic_trace_debugfs_root,
456                                     &(fc_trc_flag->fc_normal_file),
457                                     &fnic_trace_debugfs_fops);
458
459         if (!fnic_fc_rdata_trace_debugfs_file) {
460                 pr_err("fnic: Failed create fc_rdata_trace file\n");
461                 return rc;
462         }
463
464         fnic_fc_trace_debugfs_file =
465                 debugfs_create_file("fc_trace",
466                                     S_IFREG|S_IRUGO|S_IWUSR,
467                                     fnic_trace_debugfs_root,
468                                     &(fc_trc_flag->fc_row_file),
469                                     &fnic_trace_debugfs_fops);
470
471         if (!fnic_fc_trace_debugfs_file) {
472                 pr_err("fnic: Failed to create fc_trace file\n");
473                 return rc;
474         }
475         rc = 0;
476         return rc;
477 }
478
479 /*
480  * fnic_fc_trace_debugfs_terminate - Tear down debugfs infrastructure
481  *
482  * Description:
483  * When Debugfs is configured this routine removes debugfs file system
484  * elements that are specific to fnic_fc trace logging.
485  */
486
487 void fnic_fc_trace_debugfs_terminate(void)
488 {
489         debugfs_remove(fnic_fc_trace_debugfs_file);
490         fnic_fc_trace_debugfs_file = NULL;
491
492         debugfs_remove(fnic_fc_rdata_trace_debugfs_file);
493         fnic_fc_rdata_trace_debugfs_file = NULL;
494
495         debugfs_remove(fnic_fc_trace_enable);
496         fnic_fc_trace_enable = NULL;
497
498         debugfs_remove(fnic_fc_trace_clear);
499         fnic_fc_trace_clear = NULL;
500 }
501
502 /*
503  * fnic_reset_stats_open - Open the reset_stats file
504  * @inode: The inode pointer.
505  * @file: The file pointer to attach the stats reset flag.
506  *
507  * Description:
508  * This routine opens a debugsfs file reset_stats and stores i_private data
509  * to debug structure to retrieve later for while performing other
510  * file oprations.
511  *
512  * Returns:
513  * This function returns zero if successful.
514  */
515 static int fnic_reset_stats_open(struct inode *inode, struct file *file)
516 {
517         struct stats_debug_info *debug;
518
519         debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
520         if (!debug)
521                 return -ENOMEM;
522
523         debug->i_private = inode->i_private;
524
525         file->private_data = debug;
526
527         return 0;
528 }
529
530 /*
531  * fnic_reset_stats_read - Read a reset_stats debugfs file
532  * @filp: The file pointer to read from.
533  * @ubuf: The buffer to copy the data to.
534  * @cnt: The number of bytes to read.
535  * @ppos: The position in the file to start reading from.
536  *
537  * Description:
538  * This routine reads value of variable reset_stats
539  * and stores into local @buf. It will start reading file at @ppos and
540  * copy up to @cnt of data to @ubuf from @buf.
541  *
542  * Returns:
543  * This function returns the amount of data that was read.
544  */
545 static ssize_t fnic_reset_stats_read(struct file *file,
546                                         char __user *ubuf,
547                                         size_t cnt, loff_t *ppos)
548 {
549         struct stats_debug_info *debug = file->private_data;
550         struct fnic *fnic = (struct fnic *)debug->i_private;
551         char buf[64];
552         int len;
553
554         len = sprintf(buf, "%u\n", fnic->reset_stats);
555
556         return simple_read_from_buffer(ubuf, cnt, ppos, buf, len);
557 }
558
559 /*
560  * fnic_reset_stats_write - Write to reset_stats debugfs file
561  * @filp: The file pointer to write from.
562  * @ubuf: The buffer to copy the data from.
563  * @cnt: The number of bytes to write.
564  * @ppos: The position in the file to start writing to.
565  *
566  * Description:
567  * This routine writes data from user buffer @ubuf to buffer @buf and
568  * resets cumulative stats of fnic.
569  *
570  * Returns:
571  * This function returns the amount of data that was written.
572  */
573 static ssize_t fnic_reset_stats_write(struct file *file,
574                                         const char __user *ubuf,
575                                         size_t cnt, loff_t *ppos)
576 {
577         struct stats_debug_info *debug = file->private_data;
578         struct fnic *fnic = (struct fnic *)debug->i_private;
579         struct fnic_stats *stats = &fnic->fnic_stats;
580         u64 *io_stats_p = (u64 *)&stats->io_stats;
581         u64 *fw_stats_p = (u64 *)&stats->fw_stats;
582         char buf[64];
583         unsigned long val;
584         int ret;
585
586         if (cnt >= sizeof(buf))
587                 return -EINVAL;
588
589         if (copy_from_user(&buf, ubuf, cnt))
590                 return -EFAULT;
591
592         buf[cnt] = 0;
593
594         ret = kstrtoul(buf, 10, &val);
595         if (ret < 0)
596                 return ret;
597
598         fnic->reset_stats = val;
599
600         if (fnic->reset_stats) {
601                 /* Skip variable is used to avoid descrepancies to Num IOs
602                  * and IO Completions stats. Skip incrementing No IO Compls
603                  * for pending active IOs after reset stats
604                  */
605                 atomic64_set(&fnic->io_cmpl_skip,
606                         atomic64_read(&stats->io_stats.active_ios));
607                 memset(&stats->abts_stats, 0, sizeof(struct abort_stats));
608                 memset(&stats->term_stats, 0,
609                         sizeof(struct terminate_stats));
610                 memset(&stats->reset_stats, 0, sizeof(struct reset_stats));
611                 memset(&stats->misc_stats, 0, sizeof(struct misc_stats));
612                 memset(&stats->vlan_stats, 0, sizeof(struct vlan_stats));
613                 memset(io_stats_p+1, 0,
614                         sizeof(struct io_path_stats) - sizeof(u64));
615                 memset(fw_stats_p+1, 0,
616                         sizeof(struct fw_stats) - sizeof(u64));
617                 ktime_get_real_ts64(&stats->stats_timestamps.last_reset_time);
618         }
619
620         (*ppos)++;
621         return cnt;
622 }
623
624 /*
625  * fnic_reset_stats_release - Release the buffer used to store
626  * debugfs file data
627  * @inode: The inode pointer
628  * @file: The file pointer that contains the buffer to release
629  *
630  * Description:
631  * This routine frees the buffer that was allocated when the debugfs
632  * file was opened.
633  *
634  * Returns:
635  * This function returns zero.
636  */
637 static int fnic_reset_stats_release(struct inode *inode,
638                                         struct file *file)
639 {
640         struct stats_debug_info *debug = file->private_data;
641         kfree(debug);
642         return 0;
643 }
644
645 /*
646  * fnic_stats_debugfs_open - Open the stats file for specific host
647  * and get fnic stats.
648  * @inode: The inode pointer.
649  * @file: The file pointer to attach the specific host statistics.
650  *
651  * Description:
652  * This routine opens a debugsfs file stats of specific host and print
653  * fnic stats.
654  *
655  * Returns:
656  * This function returns zero if successful.
657  */
658 static int fnic_stats_debugfs_open(struct inode *inode,
659                                         struct file *file)
660 {
661         struct fnic *fnic = inode->i_private;
662         struct fnic_stats *fnic_stats = &fnic->fnic_stats;
663         struct stats_debug_info *debug;
664         int buf_size = 2 * PAGE_SIZE;
665
666         debug = kzalloc(sizeof(struct stats_debug_info), GFP_KERNEL);
667         if (!debug)
668                 return -ENOMEM;
669
670         debug->debug_buffer = vmalloc(buf_size);
671         if (!debug->debug_buffer) {
672                 kfree(debug);
673                 return -ENOMEM;
674         }
675
676         debug->buf_size = buf_size;
677         memset((void *)debug->debug_buffer, 0, buf_size);
678         debug->buffer_len = fnic_get_stats_data(debug, fnic_stats);
679
680         file->private_data = debug;
681
682         return 0;
683 }
684
685 /*
686  * fnic_stats_debugfs_read - Read a debugfs file
687  * @file: The file pointer to read from.
688  * @ubuf: The buffer to copy the data to.
689  * @nbytes: The number of bytes to read.
690  * @pos: The position in the file to start reading from.
691  *
692  * Description:
693  * This routine reads data from the buffer indicated in the private_data
694  * field of @file. It will start reading at @pos and copy up to @nbytes of
695  * data to @ubuf.
696  *
697  * Returns:
698  * This function returns the amount of data that was read (this could be
699  * less than @nbytes if the end of the file was reached).
700  */
701 static ssize_t fnic_stats_debugfs_read(struct file *file,
702                                         char __user *ubuf,
703                                         size_t nbytes,
704                                         loff_t *pos)
705 {
706         struct stats_debug_info *debug = file->private_data;
707         int rc = 0;
708         rc = simple_read_from_buffer(ubuf, nbytes, pos,
709                                         debug->debug_buffer,
710                                         debug->buffer_len);
711         return rc;
712 }
713
714 /*
715  * fnic_stats_stats_release - Release the buffer used to store
716  * debugfs file data
717  * @inode: The inode pointer
718  * @file: The file pointer that contains the buffer to release
719  *
720  * Description:
721  * This routine frees the buffer that was allocated when the debugfs
722  * file was opened.
723  *
724  * Returns:
725  * This function returns zero.
726  */
727 static int fnic_stats_debugfs_release(struct inode *inode,
728                                         struct file *file)
729 {
730         struct stats_debug_info *debug = file->private_data;
731         vfree(debug->debug_buffer);
732         kfree(debug);
733         return 0;
734 }
735
736 static const struct file_operations fnic_stats_debugfs_fops = {
737         .owner = THIS_MODULE,
738         .open = fnic_stats_debugfs_open,
739         .read = fnic_stats_debugfs_read,
740         .release = fnic_stats_debugfs_release,
741 };
742
743 static const struct file_operations fnic_reset_debugfs_fops = {
744         .owner = THIS_MODULE,
745         .open = fnic_reset_stats_open,
746         .read = fnic_reset_stats_read,
747         .write = fnic_reset_stats_write,
748         .release = fnic_reset_stats_release,
749 };
750
751 /*
752  * fnic_stats_init - Initialize stats struct and create stats file per fnic
753  *
754  * Description:
755  * When Debugfs is configured this routine sets up the stats file per fnic
756  * It will create file stats and reset_stats under statistics/host# directory
757  * to log per fnic stats.
758  */
759 int fnic_stats_debugfs_init(struct fnic *fnic)
760 {
761         int rc = -1;
762         char name[16];
763
764         snprintf(name, sizeof(name), "host%d", fnic->lport->host->host_no);
765
766         if (!fnic_stats_debugfs_root) {
767                 printk(KERN_DEBUG "fnic_stats root doesn't exist\n");
768                 return rc;
769         }
770         fnic->fnic_stats_debugfs_host = debugfs_create_dir(name,
771                                                 fnic_stats_debugfs_root);
772         if (!fnic->fnic_stats_debugfs_host) {
773                 printk(KERN_DEBUG "Cannot create host directory\n");
774                 return rc;
775         }
776
777         fnic->fnic_stats_debugfs_file = debugfs_create_file("stats",
778                                                 S_IFREG|S_IRUGO|S_IWUSR,
779                                                 fnic->fnic_stats_debugfs_host,
780                                                 fnic,
781                                                 &fnic_stats_debugfs_fops);
782         if (!fnic->fnic_stats_debugfs_file) {
783                 printk(KERN_DEBUG "Cannot create host stats file\n");
784                 return rc;
785         }
786
787         fnic->fnic_reset_debugfs_file = debugfs_create_file("reset_stats",
788                                                 S_IFREG|S_IRUGO|S_IWUSR,
789                                                 fnic->fnic_stats_debugfs_host,
790                                                 fnic,
791                                                 &fnic_reset_debugfs_fops);
792         if (!fnic->fnic_reset_debugfs_file) {
793                 printk(KERN_DEBUG "Cannot create host stats file\n");
794                 return rc;
795         }
796         rc = 0;
797         return rc;
798 }
799
800 /*
801  * fnic_stats_debugfs_remove - Tear down debugfs infrastructure of stats
802  *
803  * Description:
804  * When Debugfs is configured this routine removes debugfs file system
805  * elements that are specific to fnic stats.
806  */
807 void fnic_stats_debugfs_remove(struct fnic *fnic)
808 {
809         if (!fnic)
810                 return;
811
812         debugfs_remove(fnic->fnic_stats_debugfs_file);
813         fnic->fnic_stats_debugfs_file = NULL;
814
815         debugfs_remove(fnic->fnic_reset_debugfs_file);
816         fnic->fnic_reset_debugfs_file = NULL;
817
818         debugfs_remove(fnic->fnic_stats_debugfs_host);
819         fnic->fnic_stats_debugfs_host = NULL;
820 }