1 /*
2  * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <linux/module.h>
34 #include <linux/debugfs.h>
35 #include <linux/mlx5/qp.h>
36 #include <linux/mlx5/cq.h>
37 #include <linux/mlx5/driver.h>
38 #include "mlx5_core.h"
39 
40 enum {
41 	QP_PID,
42 	QP_STATE,
43 	QP_XPORT,
44 	QP_MTU,
45 	QP_N_RECV,
46 	QP_RECV_SZ,
47 	QP_N_SEND,
48 	QP_LOG_PG_SZ,
49 	QP_RQPN,
50 };
51 
52 static char *qp_fields[] = {
53 	[QP_PID]	= "pid",
54 	[QP_STATE]	= "state",
55 	[QP_XPORT]	= "transport",
56 	[QP_MTU]	= "mtu",
57 	[QP_N_RECV]	= "num_recv",
58 	[QP_RECV_SZ]	= "rcv_wqe_sz",
59 	[QP_N_SEND]	= "num_send",
60 	[QP_LOG_PG_SZ]	= "log2_page_sz",
61 	[QP_RQPN]	= "remote_qpn",
62 };
63 
64 enum {
65 	EQ_NUM_EQES,
66 	EQ_INTR,
67 	EQ_LOG_PG_SZ,
68 };
69 
70 static char *eq_fields[] = {
71 	[EQ_NUM_EQES]	= "num_eqes",
72 	[EQ_INTR]	= "intr",
73 	[EQ_LOG_PG_SZ]	= "log_page_size",
74 };
75 
76 enum {
77 	CQ_PID,
78 	CQ_NUM_CQES,
79 	CQ_LOG_PG_SZ,
80 };
81 
82 static char *cq_fields[] = {
83 	[CQ_PID]	= "pid",
84 	[CQ_NUM_CQES]	= "num_cqes",
85 	[CQ_LOG_PG_SZ]	= "log_page_size",
86 };
87 
88 struct dentry *mlx5_debugfs_root;
89 EXPORT_SYMBOL(mlx5_debugfs_root);
90 
91 void mlx5_register_debugfs(void)
92 {
93 	mlx5_debugfs_root = debugfs_create_dir("mlx5", NULL);
94 	if (IS_ERR_OR_NULL(mlx5_debugfs_root))
95 		mlx5_debugfs_root = NULL;
96 }
97 
98 void mlx5_unregister_debugfs(void)
99 {
100 	debugfs_remove(mlx5_debugfs_root);
101 }
102 
103 int mlx5_qp_debugfs_init(struct mlx5_core_dev *dev)
104 {
105 	if (!mlx5_debugfs_root)
106 		return 0;
107 
108 	atomic_set(&dev->num_qps, 0);
109 
110 	dev->priv.qp_debugfs = debugfs_create_dir("QPs",  dev->priv.dbg_root);
111 	if (!dev->priv.qp_debugfs)
112 		return -ENOMEM;
113 
114 	return 0;
115 }
116 
117 void mlx5_qp_debugfs_cleanup(struct mlx5_core_dev *dev)
118 {
119 	if (!mlx5_debugfs_root)
120 		return;
121 
122 	debugfs_remove_recursive(dev->priv.qp_debugfs);
123 }
124 
125 int mlx5_eq_debugfs_init(struct mlx5_core_dev *dev)
126 {
127 	if (!mlx5_debugfs_root)
128 		return 0;
129 
130 	dev->priv.eq_debugfs = debugfs_create_dir("EQs",  dev->priv.dbg_root);
131 	if (!dev->priv.eq_debugfs)
132 		return -ENOMEM;
133 
134 	return 0;
135 }
136 
137 void mlx5_eq_debugfs_cleanup(struct mlx5_core_dev *dev)
138 {
139 	if (!mlx5_debugfs_root)
140 		return;
141 
142 	debugfs_remove_recursive(dev->priv.eq_debugfs);
143 }
144 
145 static ssize_t average_read(struct file *filp, char __user *buf, size_t count,
146 			    loff_t *pos)
147 {
148 	struct mlx5_cmd_stats *stats;
149 	u64 field = 0;
150 	int ret;
151 	char tbuf[22];
152 
153 	if (*pos)
154 		return 0;
155 
156 	stats = filp->private_data;
157 	spin_lock_irq(&stats->lock);
158 	if (stats->n)
159 		field = div64_u64(stats->sum, stats->n);
160 	spin_unlock_irq(&stats->lock);
161 	ret = snprintf(tbuf, sizeof(tbuf), "%llu\n", field);
162 	if (ret > 0) {
163 		if (copy_to_user(buf, tbuf, ret))
164 			return -EFAULT;
165 	}
166 
167 	*pos += ret;
168 	return ret;
169 }
170 
171 
172 static ssize_t average_write(struct file *filp, const char __user *buf,
173 			     size_t count, loff_t *pos)
174 {
175 	struct mlx5_cmd_stats *stats;
176 
177 	stats = filp->private_data;
178 	spin_lock_irq(&stats->lock);
179 	stats->sum = 0;
180 	stats->n = 0;
181 	spin_unlock_irq(&stats->lock);
182 
183 	*pos += count;
184 
185 	return count;
186 }
187 
188 static const struct file_operations stats_fops = {
189 	.owner	= THIS_MODULE,
190 	.open	= simple_open,
191 	.read	= average_read,
192 	.write	= average_write,
193 };
194 
195 int mlx5_cmdif_debugfs_init(struct mlx5_core_dev *dev)
196 {
197 	struct mlx5_cmd_stats *stats;
198 	struct dentry **cmd;
199 	const char *namep;
200 	int err;
201 	int i;
202 
203 	if (!mlx5_debugfs_root)
204 		return 0;
205 
206 	cmd = &dev->priv.cmdif_debugfs;
207 	*cmd = debugfs_create_dir("commands", dev->priv.dbg_root);
208 	if (!*cmd)
209 		return -ENOMEM;
210 
211 	for (i = 0; i < ARRAY_SIZE(dev->cmd.stats); i++) {
212 		stats = &dev->cmd.stats[i];
213 		namep = mlx5_command_str(i);
214 		if (strcmp(namep, "unknown command opcode")) {
215 			stats->root = debugfs_create_dir(namep, *cmd);
216 			if (!stats->root) {
217 				mlx5_core_warn(dev, "failed adding command %d\n",
218 					       i);
219 				err = -ENOMEM;
220 				goto out;
221 			}
222 
223 			stats->avg = debugfs_create_file("average", 0400,
224 							 stats->root, stats,
225 							 &stats_fops);
226 			if (!stats->avg) {
227 				mlx5_core_warn(dev, "failed creating debugfs file\n");
228 				err = -ENOMEM;
229 				goto out;
230 			}
231 
232 			stats->count = debugfs_create_u64("n", 0400,
233 							  stats->root,
234 							  &stats->n);
235 			if (!stats->count) {
236 				mlx5_core_warn(dev, "failed creating debugfs file\n");
237 				err = -ENOMEM;
238 				goto out;
239 			}
240 		}
241 	}
242 
243 	return 0;
244 out:
245 	debugfs_remove_recursive(dev->priv.cmdif_debugfs);
246 	return err;
247 }
248 
249 void mlx5_cmdif_debugfs_cleanup(struct mlx5_core_dev *dev)
250 {
251 	if (!mlx5_debugfs_root)
252 		return;
253 
254 	debugfs_remove_recursive(dev->priv.cmdif_debugfs);
255 }
256 
257 int mlx5_cq_debugfs_init(struct mlx5_core_dev *dev)
258 {
259 	if (!mlx5_debugfs_root)
260 		return 0;
261 
262 	dev->priv.cq_debugfs = debugfs_create_dir("CQs",  dev->priv.dbg_root);
263 	if (!dev->priv.cq_debugfs)
264 		return -ENOMEM;
265 
266 	return 0;
267 }
268 
269 void mlx5_cq_debugfs_cleanup(struct mlx5_core_dev *dev)
270 {
271 	if (!mlx5_debugfs_root)
272 		return;
273 
274 	debugfs_remove_recursive(dev->priv.cq_debugfs);
275 }
276 
277 static u64 qp_read_field(struct mlx5_core_dev *dev, struct mlx5_core_qp *qp,
278 			 int index, int *is_str)
279 {
280 	struct mlx5_query_qp_mbox_out *out;
281 	struct mlx5_qp_context *ctx;
282 	u64 param = 0;
283 	int err;
284 	int no_sq;
285 
286 	out = kzalloc(sizeof(*out), GFP_KERNEL);
287 	if (!out)
288 		return param;
289 
290 	err = mlx5_core_qp_query(dev, qp, out, sizeof(*out));
291 	if (err) {
292 		mlx5_core_warn(dev, "failed to query qp\n");
293 		goto out;
294 	}
295 
296 	*is_str = 0;
297 	ctx = &out->ctx;
298 	switch (index) {
299 	case QP_PID:
300 		param = qp->pid;
301 		break;
302 	case QP_STATE:
303 		param = (unsigned long)mlx5_qp_state_str(be32_to_cpu(ctx->flags) >> 28);
304 		*is_str = 1;
305 		break;
306 	case QP_XPORT:
307 		param = (unsigned long)mlx5_qp_type_str((be32_to_cpu(ctx->flags) >> 16) & 0xff);
308 		*is_str = 1;
309 		break;
310 	case QP_MTU:
311 		switch (ctx->mtu_msgmax >> 5) {
312 		case IB_MTU_256:
313 			param = 256;
314 			break;
315 		case IB_MTU_512:
316 			param = 512;
317 			break;
318 		case IB_MTU_1024:
319 			param = 1024;
320 			break;
321 		case IB_MTU_2048:
322 			param = 2048;
323 			break;
324 		case IB_MTU_4096:
325 			param = 4096;
326 			break;
327 		default:
328 			param = 0;
329 		}
330 		break;
331 	case QP_N_RECV:
332 		param = 1 << ((ctx->rq_size_stride >> 3) & 0xf);
333 		break;
334 	case QP_RECV_SZ:
335 		param = 1 << ((ctx->rq_size_stride & 7) + 4);
336 		break;
337 	case QP_N_SEND:
338 		no_sq = be16_to_cpu(ctx->sq_crq_size) >> 15;
339 		if (!no_sq)
340 			param = 1 << (be16_to_cpu(ctx->sq_crq_size) >> 11);
341 		else
342 			param = 0;
343 		break;
344 	case QP_LOG_PG_SZ:
345 		param = (be32_to_cpu(ctx->log_pg_sz_remote_qpn) >> 24) & 0x1f;
346 		param += 12;
347 		break;
348 	case QP_RQPN:
349 		param = be32_to_cpu(ctx->log_pg_sz_remote_qpn) & 0xffffff;
350 		break;
351 	}
352 
353 out:
354 	kfree(out);
355 	return param;
356 }
357 
358 static u64 eq_read_field(struct mlx5_core_dev *dev, struct mlx5_eq *eq,
359 			 int index)
360 {
361 	struct mlx5_query_eq_mbox_out *out;
362 	struct mlx5_eq_context *ctx;
363 	u64 param = 0;
364 	int err;
365 
366 	out = kzalloc(sizeof(*out), GFP_KERNEL);
367 	if (!out)
368 		return param;
369 
370 	ctx = &out->ctx;
371 
372 	err = mlx5_core_eq_query(dev, eq, out, sizeof(*out));
373 	if (err) {
374 		mlx5_core_warn(dev, "failed to query eq\n");
375 		goto out;
376 	}
377 
378 	switch (index) {
379 	case EQ_NUM_EQES:
380 		param = 1 << ((be32_to_cpu(ctx->log_sz_usr_page) >> 24) & 0x1f);
381 		break;
382 	case EQ_INTR:
383 		param = ctx->intr;
384 		break;
385 	case EQ_LOG_PG_SZ:
386 		param = (ctx->log_page_size & 0x1f) + 12;
387 		break;
388 	}
389 
390 out:
391 	kfree(out);
392 	return param;
393 }
394 
395 static u64 cq_read_field(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
396 			 int index)
397 {
398 	struct mlx5_query_cq_mbox_out *out;
399 	struct mlx5_cq_context *ctx;
400 	u64 param = 0;
401 	int err;
402 
403 	out = kzalloc(sizeof(*out), GFP_KERNEL);
404 	if (!out)
405 		return param;
406 
407 	ctx = &out->ctx;
408 
409 	err = mlx5_core_query_cq(dev, cq, out);
410 	if (err) {
411 		mlx5_core_warn(dev, "failed to query cq\n");
412 		goto out;
413 	}
414 
415 	switch (index) {
416 	case CQ_PID:
417 		param = cq->pid;
418 		break;
419 	case CQ_NUM_CQES:
420 		param = 1 << ((be32_to_cpu(ctx->log_sz_usr_page) >> 24) & 0x1f);
421 		break;
422 	case CQ_LOG_PG_SZ:
423 		param = (ctx->log_pg_sz & 0x1f) + 12;
424 		break;
425 	}
426 
427 out:
428 	kfree(out);
429 	return param;
430 }
431 
432 static ssize_t dbg_read(struct file *filp, char __user *buf, size_t count,
433 			loff_t *pos)
434 {
435 	struct mlx5_field_desc *desc;
436 	struct mlx5_rsc_debug *d;
437 	char tbuf[18];
438 	int is_str = 0;
439 	u64 field;
440 	int ret;
441 
442 	if (*pos)
443 		return 0;
444 
445 	desc = filp->private_data;
446 	d = (void *)(desc - desc->i) - sizeof(*d);
447 	switch (d->type) {
448 	case MLX5_DBG_RSC_QP:
449 		field = qp_read_field(d->dev, d->object, desc->i, &is_str);
450 		break;
451 
452 	case MLX5_DBG_RSC_EQ:
453 		field = eq_read_field(d->dev, d->object, desc->i);
454 		break;
455 
456 	case MLX5_DBG_RSC_CQ:
457 		field = cq_read_field(d->dev, d->object, desc->i);
458 		break;
459 
460 	default:
461 		mlx5_core_warn(d->dev, "invalid resource type %d\n", d->type);
462 		return -EINVAL;
463 	}
464 
465 
466 	if (is_str)
467 		ret = snprintf(tbuf, sizeof(tbuf), "%s\n", (const char *)(unsigned long)field);
468 	else
469 		ret = snprintf(tbuf, sizeof(tbuf), "0x%llx\n", field);
470 
471 	if (ret > 0) {
472 		if (copy_to_user(buf, tbuf, ret))
473 			return -EFAULT;
474 	}
475 
476 	*pos += ret;
477 	return ret;
478 }
479 
480 static const struct file_operations fops = {
481 	.owner	= THIS_MODULE,
482 	.open	= simple_open,
483 	.read	= dbg_read,
484 };
485 
486 static int add_res_tree(struct mlx5_core_dev *dev, enum dbg_rsc_type type,
487 			struct dentry *root, struct mlx5_rsc_debug **dbg,
488 			int rsn, char **field, int nfile, void *data)
489 {
490 	struct mlx5_rsc_debug *d;
491 	char resn[32];
492 	int err;
493 	int i;
494 
495 	d = kzalloc(sizeof(*d) + nfile * sizeof(d->fields[0]), GFP_KERNEL);
496 	if (!d)
497 		return -ENOMEM;
498 
499 	d->dev = dev;
500 	d->object = data;
501 	d->type = type;
502 	sprintf(resn, "0x%x", rsn);
503 	d->root = debugfs_create_dir(resn,  root);
504 	if (!d->root) {
505 		err = -ENOMEM;
506 		goto out_free;
507 	}
508 
509 	for (i = 0; i < nfile; i++) {
510 		d->fields[i].i = i;
511 		d->fields[i].dent = debugfs_create_file(field[i], 0400,
512 							d->root, &d->fields[i],
513 							&fops);
514 		if (!d->fields[i].dent) {
515 			err = -ENOMEM;
516 			goto out_rem;
517 		}
518 	}
519 	*dbg = d;
520 
521 	return 0;
522 out_rem:
523 	debugfs_remove_recursive(d->root);
524 
525 out_free:
526 	kfree(d);
527 	return err;
528 }
529 
530 static void rem_res_tree(struct mlx5_rsc_debug *d)
531 {
532 	debugfs_remove_recursive(d->root);
533 	kfree(d);
534 }
535 
536 int mlx5_debug_qp_add(struct mlx5_core_dev *dev, struct mlx5_core_qp *qp)
537 {
538 	int err;
539 
540 	if (!mlx5_debugfs_root)
541 		return 0;
542 
543 	err = add_res_tree(dev, MLX5_DBG_RSC_QP, dev->priv.qp_debugfs,
544 			   &qp->dbg, qp->qpn, qp_fields,
545 			   ARRAY_SIZE(qp_fields), qp);
546 	if (err)
547 		qp->dbg = NULL;
548 
549 	return err;
550 }
551 
552 void mlx5_debug_qp_remove(struct mlx5_core_dev *dev, struct mlx5_core_qp *qp)
553 {
554 	if (!mlx5_debugfs_root)
555 		return;
556 
557 	if (qp->dbg)
558 		rem_res_tree(qp->dbg);
559 }
560 
561 
562 int mlx5_debug_eq_add(struct mlx5_core_dev *dev, struct mlx5_eq *eq)
563 {
564 	int err;
565 
566 	if (!mlx5_debugfs_root)
567 		return 0;
568 
569 	err = add_res_tree(dev, MLX5_DBG_RSC_EQ, dev->priv.eq_debugfs,
570 			   &eq->dbg, eq->eqn, eq_fields,
571 			   ARRAY_SIZE(eq_fields), eq);
572 	if (err)
573 		eq->dbg = NULL;
574 
575 	return err;
576 }
577 
578 void mlx5_debug_eq_remove(struct mlx5_core_dev *dev, struct mlx5_eq *eq)
579 {
580 	if (!mlx5_debugfs_root)
581 		return;
582 
583 	if (eq->dbg)
584 		rem_res_tree(eq->dbg);
585 }
586 
587 int mlx5_debug_cq_add(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq)
588 {
589 	int err;
590 
591 	if (!mlx5_debugfs_root)
592 		return 0;
593 
594 	err = add_res_tree(dev, MLX5_DBG_RSC_CQ, dev->priv.cq_debugfs,
595 			   &cq->dbg, cq->cqn, cq_fields,
596 			   ARRAY_SIZE(cq_fields), cq);
597 	if (err)
598 		cq->dbg = NULL;
599 
600 	return err;
601 }
602 
603 void mlx5_debug_cq_remove(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq)
604 {
605 	if (!mlx5_debugfs_root)
606 		return;
607 
608 	if (cq->dbg)
609 		rem_res_tree(cq->dbg);
610 }
611