2 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
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"
53 static char *qp_fields
[] = {
56 [QP_XPORT
] = "transport",
58 [QP_N_RECV
] = "num_recv",
59 [QP_RECV_SZ
] = "rcv_wqe_sz",
60 [QP_N_SEND
] = "num_send",
61 [QP_LOG_PG_SZ
] = "log2_page_sz",
62 [QP_RQPN
] = "remote_qpn",
71 static char *eq_fields
[] = {
72 [EQ_NUM_EQES
] = "num_eqes",
74 [EQ_LOG_PG_SZ
] = "log_page_size",
83 static char *cq_fields
[] = {
85 [CQ_NUM_CQES
] = "num_cqes",
86 [CQ_LOG_PG_SZ
] = "log_page_size",
89 struct dentry
*mlx5_debugfs_root
;
90 EXPORT_SYMBOL(mlx5_debugfs_root
);
92 void mlx5_register_debugfs(void)
94 mlx5_debugfs_root
= debugfs_create_dir("mlx5", NULL
);
95 if (IS_ERR_OR_NULL(mlx5_debugfs_root
))
96 mlx5_debugfs_root
= NULL
;
99 void mlx5_unregister_debugfs(void)
101 debugfs_remove(mlx5_debugfs_root
);
104 int mlx5_qp_debugfs_init(struct mlx5_core_dev
*dev
)
106 if (!mlx5_debugfs_root
)
109 atomic_set(&dev
->num_qps
, 0);
111 dev
->priv
.qp_debugfs
= debugfs_create_dir("QPs", dev
->priv
.dbg_root
);
112 if (!dev
->priv
.qp_debugfs
)
118 void mlx5_qp_debugfs_cleanup(struct mlx5_core_dev
*dev
)
120 if (!mlx5_debugfs_root
)
123 debugfs_remove_recursive(dev
->priv
.qp_debugfs
);
126 int mlx5_eq_debugfs_init(struct mlx5_core_dev
*dev
)
128 if (!mlx5_debugfs_root
)
131 dev
->priv
.eq_debugfs
= debugfs_create_dir("EQs", dev
->priv
.dbg_root
);
132 if (!dev
->priv
.eq_debugfs
)
138 void mlx5_eq_debugfs_cleanup(struct mlx5_core_dev
*dev
)
140 if (!mlx5_debugfs_root
)
143 debugfs_remove_recursive(dev
->priv
.eq_debugfs
);
146 static ssize_t
average_read(struct file
*filp
, char __user
*buf
, size_t count
,
149 struct mlx5_cmd_stats
*stats
;
154 stats
= filp
->private_data
;
155 spin_lock_irq(&stats
->lock
);
157 field
= div64_u64(stats
->sum
, stats
->n
);
158 spin_unlock_irq(&stats
->lock
);
159 ret
= snprintf(tbuf
, sizeof(tbuf
), "%llu\n", field
);
160 return simple_read_from_buffer(buf
, count
, pos
, tbuf
, ret
);
163 static ssize_t
average_write(struct file
*filp
, const char __user
*buf
,
164 size_t count
, loff_t
*pos
)
166 struct mlx5_cmd_stats
*stats
;
168 stats
= filp
->private_data
;
169 spin_lock_irq(&stats
->lock
);
172 spin_unlock_irq(&stats
->lock
);
179 static const struct file_operations stats_fops
= {
180 .owner
= THIS_MODULE
,
182 .read
= average_read
,
183 .write
= average_write
,
186 int mlx5_cmdif_debugfs_init(struct mlx5_core_dev
*dev
)
188 struct mlx5_cmd_stats
*stats
;
194 if (!mlx5_debugfs_root
)
197 cmd
= &dev
->priv
.cmdif_debugfs
;
198 *cmd
= debugfs_create_dir("commands", dev
->priv
.dbg_root
);
202 for (i
= 0; i
< ARRAY_SIZE(dev
->cmd
.stats
); i
++) {
203 stats
= &dev
->cmd
.stats
[i
];
204 namep
= mlx5_command_str(i
);
205 if (strcmp(namep
, "unknown command opcode")) {
206 stats
->root
= debugfs_create_dir(namep
, *cmd
);
208 mlx5_core_warn(dev
, "failed adding command %d\n",
214 stats
->avg
= debugfs_create_file("average", 0400,
218 mlx5_core_warn(dev
, "failed creating debugfs file\n");
223 stats
->count
= debugfs_create_u64("n", 0400,
227 mlx5_core_warn(dev
, "failed creating debugfs file\n");
236 debugfs_remove_recursive(dev
->priv
.cmdif_debugfs
);
240 void mlx5_cmdif_debugfs_cleanup(struct mlx5_core_dev
*dev
)
242 if (!mlx5_debugfs_root
)
245 debugfs_remove_recursive(dev
->priv
.cmdif_debugfs
);
248 int mlx5_cq_debugfs_init(struct mlx5_core_dev
*dev
)
250 if (!mlx5_debugfs_root
)
253 dev
->priv
.cq_debugfs
= debugfs_create_dir("CQs", dev
->priv
.dbg_root
);
254 if (!dev
->priv
.cq_debugfs
)
260 void mlx5_cq_debugfs_cleanup(struct mlx5_core_dev
*dev
)
262 if (!mlx5_debugfs_root
)
265 debugfs_remove_recursive(dev
->priv
.cq_debugfs
);
268 static u64
qp_read_field(struct mlx5_core_dev
*dev
, struct mlx5_core_qp
*qp
,
269 int index
, int *is_str
)
271 int outlen
= MLX5_ST_SZ_BYTES(query_qp_out
);
272 struct mlx5_qp_context
*ctx
;
278 out
= kzalloc(outlen
, GFP_KERNEL
);
282 err
= mlx5_core_qp_query(dev
, qp
, out
, outlen
);
284 mlx5_core_warn(dev
, "failed to query qp err=%d\n", err
);
290 /* FIXME: use MLX5_GET rather than mlx5_qp_context manual struct */
291 ctx
= (struct mlx5_qp_context
*)MLX5_ADDR_OF(query_qp_out
, out
, qpc
);
298 param
= (unsigned long)mlx5_qp_state_str(be32_to_cpu(ctx
->flags
) >> 28);
302 param
= (unsigned long)mlx5_qp_type_str((be32_to_cpu(ctx
->flags
) >> 16) & 0xff);
306 switch (ctx
->mtu_msgmax
>> 5) {
327 param
= 1 << ((ctx
->rq_size_stride
>> 3) & 0xf);
330 param
= 1 << ((ctx
->rq_size_stride
& 7) + 4);
333 no_sq
= be16_to_cpu(ctx
->sq_crq_size
) >> 15;
335 param
= 1 << (be16_to_cpu(ctx
->sq_crq_size
) >> 11);
340 param
= (be32_to_cpu(ctx
->log_pg_sz_remote_qpn
) >> 24) & 0x1f;
344 param
= be32_to_cpu(ctx
->log_pg_sz_remote_qpn
) & 0xffffff;
353 static int mlx5_core_eq_query(struct mlx5_core_dev
*dev
, struct mlx5_eq
*eq
,
354 u32
*out
, int outlen
)
356 u32 in
[MLX5_ST_SZ_DW(query_eq_in
)] = {};
358 MLX5_SET(query_eq_in
, in
, opcode
, MLX5_CMD_OP_QUERY_EQ
);
359 MLX5_SET(query_eq_in
, in
, eq_number
, eq
->eqn
);
360 return mlx5_cmd_exec(dev
, in
, sizeof(in
), out
, outlen
);
363 static u64
eq_read_field(struct mlx5_core_dev
*dev
, struct mlx5_eq
*eq
,
366 int outlen
= MLX5_ST_SZ_BYTES(query_eq_out
);
372 out
= kzalloc(outlen
, GFP_KERNEL
);
376 err
= mlx5_core_eq_query(dev
, eq
, out
, outlen
);
378 mlx5_core_warn(dev
, "failed to query eq\n");
381 ctx
= MLX5_ADDR_OF(query_eq_out
, out
, eq_context_entry
);
385 param
= 1 << MLX5_GET(eqc
, ctx
, log_eq_size
);
388 param
= MLX5_GET(eqc
, ctx
, intr
);
391 param
= MLX5_GET(eqc
, ctx
, log_page_size
) + 12;
400 static u64
cq_read_field(struct mlx5_core_dev
*dev
, struct mlx5_core_cq
*cq
,
403 int outlen
= MLX5_ST_SZ_BYTES(query_cq_out
);
409 out
= kvzalloc(outlen
, GFP_KERNEL
);
413 err
= mlx5_core_query_cq(dev
, cq
, out
, outlen
);
415 mlx5_core_warn(dev
, "failed to query cq\n");
418 ctx
= MLX5_ADDR_OF(query_cq_out
, out
, cq_context
);
425 param
= 1 << MLX5_GET(cqc
, ctx
, log_cq_size
);
428 param
= MLX5_GET(cqc
, ctx
, log_page_size
);
437 static ssize_t
dbg_read(struct file
*filp
, char __user
*buf
, size_t count
,
440 struct mlx5_field_desc
*desc
;
441 struct mlx5_rsc_debug
*d
;
447 desc
= filp
->private_data
;
448 d
= (void *)(desc
- desc
->i
) - sizeof(*d
);
450 case MLX5_DBG_RSC_QP
:
451 field
= qp_read_field(d
->dev
, d
->object
, desc
->i
, &is_str
);
454 case MLX5_DBG_RSC_EQ
:
455 field
= eq_read_field(d
->dev
, d
->object
, desc
->i
);
458 case MLX5_DBG_RSC_CQ
:
459 field
= cq_read_field(d
->dev
, d
->object
, desc
->i
);
463 mlx5_core_warn(d
->dev
, "invalid resource type %d\n", d
->type
);
468 ret
= snprintf(tbuf
, sizeof(tbuf
), "%s\n", (const char *)(unsigned long)field
);
470 ret
= snprintf(tbuf
, sizeof(tbuf
), "0x%llx\n", field
);
472 return simple_read_from_buffer(buf
, count
, pos
, tbuf
, ret
);
475 static const struct file_operations fops
= {
476 .owner
= THIS_MODULE
,
481 static int add_res_tree(struct mlx5_core_dev
*dev
, enum dbg_rsc_type type
,
482 struct dentry
*root
, struct mlx5_rsc_debug
**dbg
,
483 int rsn
, char **field
, int nfile
, void *data
)
485 struct mlx5_rsc_debug
*d
;
490 d
= kzalloc(struct_size(d
, fields
, nfile
), GFP_KERNEL
);
497 sprintf(resn
, "0x%x", rsn
);
498 d
->root
= debugfs_create_dir(resn
, root
);
504 for (i
= 0; i
< nfile
; i
++) {
506 d
->fields
[i
].dent
= debugfs_create_file(field
[i
], 0400,
507 d
->root
, &d
->fields
[i
],
509 if (!d
->fields
[i
].dent
) {
518 debugfs_remove_recursive(d
->root
);
525 static void rem_res_tree(struct mlx5_rsc_debug
*d
)
527 debugfs_remove_recursive(d
->root
);
531 int mlx5_debug_qp_add(struct mlx5_core_dev
*dev
, struct mlx5_core_qp
*qp
)
535 if (!mlx5_debugfs_root
)
538 err
= add_res_tree(dev
, MLX5_DBG_RSC_QP
, dev
->priv
.qp_debugfs
,
539 &qp
->dbg
, qp
->qpn
, qp_fields
,
540 ARRAY_SIZE(qp_fields
), qp
);
547 void mlx5_debug_qp_remove(struct mlx5_core_dev
*dev
, struct mlx5_core_qp
*qp
)
549 if (!mlx5_debugfs_root
)
553 rem_res_tree(qp
->dbg
);
556 int mlx5_debug_eq_add(struct mlx5_core_dev
*dev
, struct mlx5_eq
*eq
)
560 if (!mlx5_debugfs_root
)
563 err
= add_res_tree(dev
, MLX5_DBG_RSC_EQ
, dev
->priv
.eq_debugfs
,
564 &eq
->dbg
, eq
->eqn
, eq_fields
,
565 ARRAY_SIZE(eq_fields
), eq
);
572 void mlx5_debug_eq_remove(struct mlx5_core_dev
*dev
, struct mlx5_eq
*eq
)
574 if (!mlx5_debugfs_root
)
578 rem_res_tree(eq
->dbg
);
581 int mlx5_debug_cq_add(struct mlx5_core_dev
*dev
, struct mlx5_core_cq
*cq
)
585 if (!mlx5_debugfs_root
)
588 err
= add_res_tree(dev
, MLX5_DBG_RSC_CQ
, dev
->priv
.cq_debugfs
,
589 &cq
->dbg
, cq
->cqn
, cq_fields
,
590 ARRAY_SIZE(cq_fields
), cq
);
597 void mlx5_debug_cq_remove(struct mlx5_core_dev
*dev
, struct mlx5_core_cq
*cq
)
599 if (!mlx5_debugfs_root
)
603 rem_res_tree(cq
->dbg
);