GNU Linux-libre 4.19.295-gnu1
[releases.git] / drivers / scsi / qedf / qedf_debugfs.c
1 /*
2  *  QLogic FCoE Offload Driver
3  *  Copyright (c) 2016-2018 QLogic Corporation
4  *
5  *  This software is available under the terms of the GNU General Public License
6  *  (GPL) Version 2, available from the file COPYING in the main directory of
7  *  this source tree.
8  */
9 #ifdef CONFIG_DEBUG_FS
10
11 #include <linux/uaccess.h>
12 #include <linux/debugfs.h>
13 #include <linux/module.h>
14 #include <linux/vmalloc.h>
15
16 #include "qedf.h"
17 #include "qedf_dbg.h"
18
19 static struct dentry *qedf_dbg_root;
20
21 /**
22  * qedf_dbg_host_init - setup the debugfs file for the pf
23  * @pf: the pf that is starting up
24  **/
25 void
26 qedf_dbg_host_init(struct qedf_dbg_ctx *qedf,
27                     const struct qedf_debugfs_ops *dops,
28                     const struct file_operations *fops)
29 {
30         char host_dirname[32];
31         struct dentry *file_dentry = NULL;
32
33         QEDF_INFO(qedf, QEDF_LOG_DEBUGFS, "Creating debugfs host node\n");
34         /* create pf dir */
35         sprintf(host_dirname, "host%u", qedf->host_no);
36         qedf->bdf_dentry = debugfs_create_dir(host_dirname, qedf_dbg_root);
37         if (!qedf->bdf_dentry)
38                 return;
39
40         /* create debugfs files */
41         while (dops) {
42                 if (!(dops->name))
43                         break;
44
45                 file_dentry = debugfs_create_file(dops->name, 0600,
46                                                   qedf->bdf_dentry, qedf,
47                                                   fops);
48                 if (!file_dentry) {
49                         QEDF_INFO(qedf, QEDF_LOG_DEBUGFS,
50                                    "Debugfs entry %s creation failed\n",
51                                    dops->name);
52                         debugfs_remove_recursive(qedf->bdf_dentry);
53                         return;
54                 }
55                 dops++;
56                 fops++;
57         }
58 }
59
60 /**
61  * qedf_dbg_host_exit - clear out the pf's debugfs entries
62  * @pf: the pf that is stopping
63  **/
64 void
65 qedf_dbg_host_exit(struct qedf_dbg_ctx *qedf)
66 {
67         QEDF_INFO(qedf, QEDF_LOG_DEBUGFS, "Destroying debugfs host "
68                    "entry\n");
69         /* remove debugfs  entries of this PF */
70         debugfs_remove_recursive(qedf->bdf_dentry);
71         qedf->bdf_dentry = NULL;
72 }
73
74 /**
75  * qedf_dbg_init - start up debugfs for the driver
76  **/
77 void
78 qedf_dbg_init(char *drv_name)
79 {
80         QEDF_INFO(NULL, QEDF_LOG_DEBUGFS, "Creating debugfs root node\n");
81
82         /* create qed dir in root of debugfs. NULL means debugfs root */
83         qedf_dbg_root = debugfs_create_dir(drv_name, NULL);
84         if (!qedf_dbg_root)
85                 QEDF_INFO(NULL, QEDF_LOG_DEBUGFS, "Init of debugfs "
86                            "failed\n");
87 }
88
89 /**
90  * qedf_dbg_exit - clean out the driver's debugfs entries
91  **/
92 void
93 qedf_dbg_exit(void)
94 {
95         QEDF_INFO(NULL, QEDF_LOG_DEBUGFS, "Destroying debugfs root "
96                    "entry\n");
97
98         /* remove qed dir in root of debugfs */
99         debugfs_remove_recursive(qedf_dbg_root);
100         qedf_dbg_root = NULL;
101 }
102
103 const struct qedf_debugfs_ops qedf_debugfs_ops[] = {
104         { "fp_int", NULL },
105         { "io_trace", NULL },
106         { "debug", NULL },
107         { "stop_io_on_error", NULL},
108         { "driver_stats", NULL},
109         { "clear_stats", NULL},
110         { "offload_stats", NULL},
111         /* This must be last */
112         { NULL, NULL }
113 };
114
115 DECLARE_PER_CPU(struct qedf_percpu_iothread_s, qedf_percpu_iothreads);
116
117 static ssize_t
118 qedf_dbg_fp_int_cmd_read(struct file *filp, char __user *buffer, size_t count,
119                          loff_t *ppos)
120 {
121         ssize_t ret;
122         size_t cnt = 0;
123         char *cbuf;
124         int id;
125         struct qedf_fastpath *fp = NULL;
126         struct qedf_dbg_ctx *qedf_dbg =
127                                 (struct qedf_dbg_ctx *)filp->private_data;
128         struct qedf_ctx *qedf = container_of(qedf_dbg,
129             struct qedf_ctx, dbg_ctx);
130
131         QEDF_INFO(qedf_dbg, QEDF_LOG_DEBUGFS, "entered\n");
132
133         cbuf = vmalloc(QEDF_DEBUGFS_LOG_LEN);
134         if (!cbuf)
135                 return 0;
136
137         cnt += scnprintf(cbuf + cnt, QEDF_DEBUGFS_LOG_LEN - cnt, "\nFastpath I/O completions\n\n");
138
139         for (id = 0; id < qedf->num_queues; id++) {
140                 fp = &(qedf->fp_array[id]);
141                 if (fp->sb_id == QEDF_SB_ID_NULL)
142                         continue;
143                 cnt += scnprintf(cbuf + cnt, QEDF_DEBUGFS_LOG_LEN - cnt,
144                                  "#%d: %lu\n", id, fp->completions);
145         }
146
147         ret = simple_read_from_buffer(buffer, count, ppos, cbuf, cnt);
148
149         vfree(cbuf);
150
151         return ret;
152 }
153
154 static ssize_t
155 qedf_dbg_fp_int_cmd_write(struct file *filp, const char __user *buffer,
156                           size_t count, loff_t *ppos)
157 {
158         if (!count || *ppos)
159                 return 0;
160
161         return count;
162 }
163
164 static ssize_t
165 qedf_dbg_debug_cmd_read(struct file *filp, char __user *buffer, size_t count,
166                         loff_t *ppos)
167 {
168         int cnt;
169         struct qedf_dbg_ctx *qedf =
170                                 (struct qedf_dbg_ctx *)filp->private_data;
171
172         QEDF_INFO(qedf, QEDF_LOG_DEBUGFS, "entered\n");
173         cnt = sprintf(buffer, "debug mask = 0x%x\n", qedf_debug);
174
175         cnt = min_t(int, count, cnt - *ppos);
176         *ppos += cnt;
177         return cnt;
178 }
179
180 static ssize_t
181 qedf_dbg_debug_cmd_write(struct file *filp, const char __user *buffer,
182                          size_t count, loff_t *ppos)
183 {
184         uint32_t val;
185         void *kern_buf;
186         int rval;
187         struct qedf_dbg_ctx *qedf =
188             (struct qedf_dbg_ctx *)filp->private_data;
189
190         if (!count || *ppos)
191                 return 0;
192
193         kern_buf = memdup_user(buffer, count);
194         if (IS_ERR(kern_buf))
195                 return PTR_ERR(kern_buf);
196
197         rval = kstrtouint(kern_buf, 10, &val);
198         kfree(kern_buf);
199         if (rval)
200                 return rval;
201
202         if (val == 1)
203                 qedf_debug = QEDF_DEFAULT_LOG_MASK;
204         else
205                 qedf_debug = val;
206
207         QEDF_INFO(qedf, QEDF_LOG_DEBUGFS, "Setting debug=0x%x.\n", val);
208         return count;
209 }
210
211 static ssize_t
212 qedf_dbg_stop_io_on_error_cmd_read(struct file *filp, char __user *buffer,
213                                    size_t count, loff_t *ppos)
214 {
215         int cnt;
216         char cbuf[7];
217         struct qedf_dbg_ctx *qedf_dbg =
218                                 (struct qedf_dbg_ctx *)filp->private_data;
219         struct qedf_ctx *qedf = container_of(qedf_dbg,
220             struct qedf_ctx, dbg_ctx);
221
222         QEDF_INFO(qedf_dbg, QEDF_LOG_DEBUGFS, "entered\n");
223         cnt = scnprintf(cbuf, sizeof(cbuf), "%s\n",
224             qedf->stop_io_on_error ? "true" : "false");
225
226         return simple_read_from_buffer(buffer, count, ppos, cbuf, cnt);
227 }
228
229 static ssize_t
230 qedf_dbg_stop_io_on_error_cmd_write(struct file *filp,
231                                     const char __user *buffer, size_t count,
232                                     loff_t *ppos)
233 {
234         void *kern_buf;
235         struct qedf_dbg_ctx *qedf_dbg =
236                                 (struct qedf_dbg_ctx *)filp->private_data;
237         struct qedf_ctx *qedf = container_of(qedf_dbg, struct qedf_ctx,
238             dbg_ctx);
239
240         QEDF_INFO(qedf_dbg, QEDF_LOG_DEBUGFS, "entered\n");
241
242         if (!count || *ppos)
243                 return 0;
244
245         kern_buf = memdup_user(buffer, 6);
246         if (IS_ERR(kern_buf))
247                 return PTR_ERR(kern_buf);
248
249         if (strncmp(kern_buf, "false", 5) == 0)
250                 qedf->stop_io_on_error = false;
251         else if (strncmp(kern_buf, "true", 4) == 0)
252                 qedf->stop_io_on_error = true;
253         else if (strncmp(kern_buf, "now", 3) == 0)
254                 /* Trigger from user to stop all I/O on this host */
255                 set_bit(QEDF_DBG_STOP_IO, &qedf->flags);
256
257         kfree(kern_buf);
258         return count;
259 }
260
261 static int
262 qedf_io_trace_show(struct seq_file *s, void *unused)
263 {
264         int i, idx = 0;
265         struct qedf_ctx *qedf = s->private;
266         struct qedf_dbg_ctx *qedf_dbg = &qedf->dbg_ctx;
267         struct qedf_io_log *io_log;
268         unsigned long flags;
269
270         if (!qedf_io_tracing) {
271                 seq_puts(s, "I/O tracing not enabled.\n");
272                 goto out;
273         }
274
275         QEDF_INFO(qedf_dbg, QEDF_LOG_DEBUGFS, "entered\n");
276
277         spin_lock_irqsave(&qedf->io_trace_lock, flags);
278         idx = qedf->io_trace_idx;
279         for (i = 0; i < QEDF_IO_TRACE_SIZE; i++) {
280                 io_log = &qedf->io_trace_buf[idx];
281                 seq_printf(s, "%d:", io_log->direction);
282                 seq_printf(s, "0x%x:", io_log->task_id);
283                 seq_printf(s, "0x%06x:", io_log->port_id);
284                 seq_printf(s, "%d:", io_log->lun);
285                 seq_printf(s, "0x%02x:", io_log->op);
286                 seq_printf(s, "0x%02x%02x%02x%02x:", io_log->lba[0],
287                     io_log->lba[1], io_log->lba[2], io_log->lba[3]);
288                 seq_printf(s, "%d:", io_log->bufflen);
289                 seq_printf(s, "%d:", io_log->sg_count);
290                 seq_printf(s, "0x%08x:", io_log->result);
291                 seq_printf(s, "%lu:", io_log->jiffies);
292                 seq_printf(s, "%d:", io_log->refcount);
293                 seq_printf(s, "%d:", io_log->req_cpu);
294                 seq_printf(s, "%d:", io_log->int_cpu);
295                 seq_printf(s, "%d:", io_log->rsp_cpu);
296                 seq_printf(s, "%d\n", io_log->sge_type);
297
298                 idx++;
299                 if (idx == QEDF_IO_TRACE_SIZE)
300                         idx = 0;
301         }
302         spin_unlock_irqrestore(&qedf->io_trace_lock, flags);
303
304 out:
305         return 0;
306 }
307
308 static int
309 qedf_dbg_io_trace_open(struct inode *inode, struct file *file)
310 {
311         struct qedf_dbg_ctx *qedf_dbg = inode->i_private;
312         struct qedf_ctx *qedf = container_of(qedf_dbg,
313             struct qedf_ctx, dbg_ctx);
314
315         return single_open(file, qedf_io_trace_show, qedf);
316 }
317
318 static int
319 qedf_driver_stats_show(struct seq_file *s, void *unused)
320 {
321         struct qedf_ctx *qedf = s->private;
322         struct qedf_rport *fcport;
323         struct fc_rport_priv *rdata;
324
325         seq_printf(s, "cmg_mgr free io_reqs: %d\n",
326             atomic_read(&qedf->cmd_mgr->free_list_cnt));
327         seq_printf(s, "slow SGEs: %d\n", qedf->slow_sge_ios);
328         seq_printf(s, "single SGEs: %d\n", qedf->single_sge_ios);
329         seq_printf(s, "fast SGEs: %d\n\n", qedf->fast_sge_ios);
330
331         seq_puts(s, "Offloaded ports:\n\n");
332
333         rcu_read_lock();
334         list_for_each_entry_rcu(fcport, &qedf->fcports, peers) {
335                 rdata = fcport->rdata;
336                 if (rdata == NULL)
337                         continue;
338                 seq_printf(s, "%06x: free_sqes: %d, num_active_ios: %d\n",
339                     rdata->ids.port_id, atomic_read(&fcport->free_sqes),
340                     atomic_read(&fcport->num_active_ios));
341         }
342         rcu_read_unlock();
343
344         return 0;
345 }
346
347 static int
348 qedf_dbg_driver_stats_open(struct inode *inode, struct file *file)
349 {
350         struct qedf_dbg_ctx *qedf_dbg = inode->i_private;
351         struct qedf_ctx *qedf = container_of(qedf_dbg,
352             struct qedf_ctx, dbg_ctx);
353
354         return single_open(file, qedf_driver_stats_show, qedf);
355 }
356
357 static ssize_t
358 qedf_dbg_clear_stats_cmd_read(struct file *filp, char __user *buffer,
359                                    size_t count, loff_t *ppos)
360 {
361         int cnt = 0;
362
363         /* Essentially a read stub */
364         cnt = min_t(int, count, cnt - *ppos);
365         *ppos += cnt;
366         return cnt;
367 }
368
369 static ssize_t
370 qedf_dbg_clear_stats_cmd_write(struct file *filp,
371                                     const char __user *buffer, size_t count,
372                                     loff_t *ppos)
373 {
374         struct qedf_dbg_ctx *qedf_dbg =
375                                 (struct qedf_dbg_ctx *)filp->private_data;
376         struct qedf_ctx *qedf = container_of(qedf_dbg, struct qedf_ctx,
377             dbg_ctx);
378
379         QEDF_INFO(qedf_dbg, QEDF_LOG_DEBUGFS, "Clearing stat counters.\n");
380
381         if (!count || *ppos)
382                 return 0;
383
384         /* Clear stat counters exposed by 'stats' node */
385         qedf->slow_sge_ios = 0;
386         qedf->single_sge_ios = 0;
387         qedf->fast_sge_ios = 0;
388
389         return count;
390 }
391
392 static int
393 qedf_offload_stats_show(struct seq_file *s, void *unused)
394 {
395         struct qedf_ctx *qedf = s->private;
396         struct qed_fcoe_stats *fw_fcoe_stats;
397
398         fw_fcoe_stats = kmalloc(sizeof(struct qed_fcoe_stats), GFP_KERNEL);
399         if (!fw_fcoe_stats) {
400                 QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate memory for "
401                     "fw_fcoe_stats.\n");
402                 goto out;
403         }
404
405         /* Query firmware for offload stats */
406         qed_ops->get_stats(qedf->cdev, fw_fcoe_stats);
407
408         seq_printf(s, "fcoe_rx_byte_cnt=%llu\n"
409             "fcoe_rx_data_pkt_cnt=%llu\n"
410             "fcoe_rx_xfer_pkt_cnt=%llu\n"
411             "fcoe_rx_other_pkt_cnt=%llu\n"
412             "fcoe_silent_drop_pkt_cmdq_full_cnt=%u\n"
413             "fcoe_silent_drop_pkt_crc_error_cnt=%u\n"
414             "fcoe_silent_drop_pkt_task_invalid_cnt=%u\n"
415             "fcoe_silent_drop_total_pkt_cnt=%u\n"
416             "fcoe_silent_drop_pkt_rq_full_cnt=%u\n"
417             "fcoe_tx_byte_cnt=%llu\n"
418             "fcoe_tx_data_pkt_cnt=%llu\n"
419             "fcoe_tx_xfer_pkt_cnt=%llu\n"
420             "fcoe_tx_other_pkt_cnt=%llu\n",
421             fw_fcoe_stats->fcoe_rx_byte_cnt,
422             fw_fcoe_stats->fcoe_rx_data_pkt_cnt,
423             fw_fcoe_stats->fcoe_rx_xfer_pkt_cnt,
424             fw_fcoe_stats->fcoe_rx_other_pkt_cnt,
425             fw_fcoe_stats->fcoe_silent_drop_pkt_cmdq_full_cnt,
426             fw_fcoe_stats->fcoe_silent_drop_pkt_crc_error_cnt,
427             fw_fcoe_stats->fcoe_silent_drop_pkt_task_invalid_cnt,
428             fw_fcoe_stats->fcoe_silent_drop_total_pkt_cnt,
429             fw_fcoe_stats->fcoe_silent_drop_pkt_rq_full_cnt,
430             fw_fcoe_stats->fcoe_tx_byte_cnt,
431             fw_fcoe_stats->fcoe_tx_data_pkt_cnt,
432             fw_fcoe_stats->fcoe_tx_xfer_pkt_cnt,
433             fw_fcoe_stats->fcoe_tx_other_pkt_cnt);
434
435         kfree(fw_fcoe_stats);
436 out:
437         return 0;
438 }
439
440 static int
441 qedf_dbg_offload_stats_open(struct inode *inode, struct file *file)
442 {
443         struct qedf_dbg_ctx *qedf_dbg = inode->i_private;
444         struct qedf_ctx *qedf = container_of(qedf_dbg,
445             struct qedf_ctx, dbg_ctx);
446
447         return single_open(file, qedf_offload_stats_show, qedf);
448 }
449
450 const struct file_operations qedf_dbg_fops[] = {
451         qedf_dbg_fileops(qedf, fp_int),
452         qedf_dbg_fileops_seq(qedf, io_trace),
453         qedf_dbg_fileops(qedf, debug),
454         qedf_dbg_fileops(qedf, stop_io_on_error),
455         qedf_dbg_fileops_seq(qedf, driver_stats),
456         qedf_dbg_fileops(qedf, clear_stats),
457         qedf_dbg_fileops_seq(qedf, offload_stats),
458         /* This must be last */
459         { },
460 };
461
462 #else /* CONFIG_DEBUG_FS */
463 void qedf_dbg_host_init(struct qedf_dbg_ctx *);
464 void qedf_dbg_host_exit(struct qedf_dbg_ctx *);
465 void qedf_dbg_init(char *);
466 void qedf_dbg_exit(void);
467 #endif /* CONFIG_DEBUG_FS */