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