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