xref: /openbmc/linux/drivers/scsi/qedf/qedf_debugfs.c (revision be709d48)
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 static int
297 qedf_driver_stats_show(struct seq_file *s, void *unused)
298 {
299 	struct qedf_ctx *qedf = s->private;
300 	struct qedf_rport *fcport;
301 	struct fc_rport_priv *rdata;
302 
303 	seq_printf(s, "cmg_mgr free io_reqs: %d\n",
304 	    atomic_read(&qedf->cmd_mgr->free_list_cnt));
305 	seq_printf(s, "slow SGEs: %d\n", qedf->slow_sge_ios);
306 	seq_printf(s, "single SGEs: %d\n", qedf->single_sge_ios);
307 	seq_printf(s, "fast SGEs: %d\n\n", qedf->fast_sge_ios);
308 
309 	seq_puts(s, "Offloaded ports:\n\n");
310 
311 	rcu_read_lock();
312 	list_for_each_entry_rcu(fcport, &qedf->fcports, peers) {
313 		rdata = fcport->rdata;
314 		if (rdata == NULL)
315 			continue;
316 		seq_printf(s, "%06x: free_sqes: %d, num_active_ios: %d\n",
317 		    rdata->ids.port_id, atomic_read(&fcport->free_sqes),
318 		    atomic_read(&fcport->num_active_ios));
319 	}
320 	rcu_read_unlock();
321 
322 	return 0;
323 }
324 
325 static int
326 qedf_dbg_driver_stats_open(struct inode *inode, struct file *file)
327 {
328 	struct qedf_dbg_ctx *qedf_dbg = inode->i_private;
329 	struct qedf_ctx *qedf = container_of(qedf_dbg,
330 	    struct qedf_ctx, dbg_ctx);
331 
332 	return single_open(file, qedf_driver_stats_show, qedf);
333 }
334 
335 static ssize_t
336 qedf_dbg_clear_stats_cmd_read(struct file *filp, char __user *buffer,
337 				   size_t count, loff_t *ppos)
338 {
339 	int cnt = 0;
340 
341 	/* Essentially a read stub */
342 	cnt = min_t(int, count, cnt - *ppos);
343 	*ppos += cnt;
344 	return cnt;
345 }
346 
347 static ssize_t
348 qedf_dbg_clear_stats_cmd_write(struct file *filp,
349 				    const char __user *buffer, size_t count,
350 				    loff_t *ppos)
351 {
352 	struct qedf_dbg_ctx *qedf_dbg =
353 				(struct qedf_dbg_ctx *)filp->private_data;
354 	struct qedf_ctx *qedf = container_of(qedf_dbg, struct qedf_ctx,
355 	    dbg_ctx);
356 
357 	QEDF_INFO(qedf_dbg, QEDF_LOG_DEBUGFS, "Clearing stat counters.\n");
358 
359 	if (!count || *ppos)
360 		return 0;
361 
362 	/* Clear stat counters exposed by 'stats' node */
363 	qedf->slow_sge_ios = 0;
364 	qedf->single_sge_ios = 0;
365 	qedf->fast_sge_ios = 0;
366 
367 	return count;
368 }
369 
370 static int
371 qedf_offload_stats_show(struct seq_file *s, void *unused)
372 {
373 	struct qedf_ctx *qedf = s->private;
374 	struct qed_fcoe_stats *fw_fcoe_stats;
375 
376 	fw_fcoe_stats = kmalloc(sizeof(struct qed_fcoe_stats), GFP_KERNEL);
377 	if (!fw_fcoe_stats) {
378 		QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate memory for "
379 		    "fw_fcoe_stats.\n");
380 		goto out;
381 	}
382 
383 	/* Query firmware for offload stats */
384 	qed_ops->get_stats(qedf->cdev, fw_fcoe_stats);
385 
386 	seq_printf(s, "fcoe_rx_byte_cnt=%llu\n"
387 	    "fcoe_rx_data_pkt_cnt=%llu\n"
388 	    "fcoe_rx_xfer_pkt_cnt=%llu\n"
389 	    "fcoe_rx_other_pkt_cnt=%llu\n"
390 	    "fcoe_silent_drop_pkt_cmdq_full_cnt=%u\n"
391 	    "fcoe_silent_drop_pkt_crc_error_cnt=%u\n"
392 	    "fcoe_silent_drop_pkt_task_invalid_cnt=%u\n"
393 	    "fcoe_silent_drop_total_pkt_cnt=%u\n"
394 	    "fcoe_silent_drop_pkt_rq_full_cnt=%u\n"
395 	    "fcoe_tx_byte_cnt=%llu\n"
396 	    "fcoe_tx_data_pkt_cnt=%llu\n"
397 	    "fcoe_tx_xfer_pkt_cnt=%llu\n"
398 	    "fcoe_tx_other_pkt_cnt=%llu\n",
399 	    fw_fcoe_stats->fcoe_rx_byte_cnt,
400 	    fw_fcoe_stats->fcoe_rx_data_pkt_cnt,
401 	    fw_fcoe_stats->fcoe_rx_xfer_pkt_cnt,
402 	    fw_fcoe_stats->fcoe_rx_other_pkt_cnt,
403 	    fw_fcoe_stats->fcoe_silent_drop_pkt_cmdq_full_cnt,
404 	    fw_fcoe_stats->fcoe_silent_drop_pkt_crc_error_cnt,
405 	    fw_fcoe_stats->fcoe_silent_drop_pkt_task_invalid_cnt,
406 	    fw_fcoe_stats->fcoe_silent_drop_total_pkt_cnt,
407 	    fw_fcoe_stats->fcoe_silent_drop_pkt_rq_full_cnt,
408 	    fw_fcoe_stats->fcoe_tx_byte_cnt,
409 	    fw_fcoe_stats->fcoe_tx_data_pkt_cnt,
410 	    fw_fcoe_stats->fcoe_tx_xfer_pkt_cnt,
411 	    fw_fcoe_stats->fcoe_tx_other_pkt_cnt);
412 
413 	kfree(fw_fcoe_stats);
414 out:
415 	return 0;
416 }
417 
418 static int
419 qedf_dbg_offload_stats_open(struct inode *inode, struct file *file)
420 {
421 	struct qedf_dbg_ctx *qedf_dbg = inode->i_private;
422 	struct qedf_ctx *qedf = container_of(qedf_dbg,
423 	    struct qedf_ctx, dbg_ctx);
424 
425 	return single_open(file, qedf_offload_stats_show, qedf);
426 }
427 
428 const struct file_operations qedf_dbg_fops[] = {
429 	qedf_dbg_fileops(qedf, fp_int),
430 	qedf_dbg_fileops_seq(qedf, io_trace),
431 	qedf_dbg_fileops(qedf, debug),
432 	qedf_dbg_fileops(qedf, stop_io_on_error),
433 	qedf_dbg_fileops_seq(qedf, driver_stats),
434 	qedf_dbg_fileops(qedf, clear_stats),
435 	qedf_dbg_fileops_seq(qedf, offload_stats),
436 	/* This must be last */
437 	{ },
438 };
439 
440 #else /* CONFIG_DEBUG_FS */
441 void qedf_dbg_host_init(struct qedf_dbg_ctx *);
442 void qedf_dbg_host_exit(struct qedf_dbg_ctx *);
443 void qedf_dbg_init(char *);
444 void qedf_dbg_exit(void);
445 #endif /* CONFIG_DEBUG_FS */
446