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