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