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
);
97 void mlx5_unregister_debugfs(void)
99 debugfs_remove(mlx5_debugfs_root
);
102 void mlx5_qp_debugfs_init(struct mlx5_core_dev
*dev
)
104 dev
->priv
.qp_debugfs
= debugfs_create_dir("QPs", dev
->priv
.dbg_root
);
106 EXPORT_SYMBOL(mlx5_qp_debugfs_init
);
108 void mlx5_qp_debugfs_cleanup(struct mlx5_core_dev
*dev
)
110 debugfs_remove_recursive(dev
->priv
.qp_debugfs
);
112 EXPORT_SYMBOL(mlx5_qp_debugfs_cleanup
);
114 void mlx5_eq_debugfs_init(struct mlx5_core_dev
*dev
)
116 dev
->priv
.eq_debugfs
= debugfs_create_dir("EQs", dev
->priv
.dbg_root
);
119 void mlx5_eq_debugfs_cleanup(struct mlx5_core_dev
*dev
)
121 debugfs_remove_recursive(dev
->priv
.eq_debugfs
);
124 static ssize_t
average_read(struct file
*filp
, char __user
*buf
, size_t count
,
127 struct mlx5_cmd_stats
*stats
;
132 stats
= filp
->private_data
;
133 spin_lock_irq(&stats
->lock
);
135 field
= div64_u64(stats
->sum
, stats
->n
);
136 spin_unlock_irq(&stats
->lock
);
137 ret
= snprintf(tbuf
, sizeof(tbuf
), "%llu\n", field
);
138 return simple_read_from_buffer(buf
, count
, pos
, tbuf
, ret
);
141 static ssize_t
average_write(struct file
*filp
, const char __user
*buf
,
142 size_t count
, loff_t
*pos
)
144 struct mlx5_cmd_stats
*stats
;
146 stats
= filp
->private_data
;
147 spin_lock_irq(&stats
->lock
);
150 spin_unlock_irq(&stats
->lock
);
157 static const struct file_operations stats_fops
= {
158 .owner
= THIS_MODULE
,
160 .read
= average_read
,
161 .write
= average_write
,
164 void mlx5_cmdif_debugfs_init(struct mlx5_core_dev
*dev
)
166 struct mlx5_cmd_stats
*stats
;
171 cmd
= &dev
->priv
.cmdif_debugfs
;
172 *cmd
= debugfs_create_dir("commands", dev
->priv
.dbg_root
);
174 for (i
= 0; i
< MLX5_CMD_OP_MAX
; i
++) {
175 stats
= &dev
->cmd
.stats
[i
];
176 namep
= mlx5_command_str(i
);
177 if (strcmp(namep
, "unknown command opcode")) {
178 stats
->root
= debugfs_create_dir(namep
, *cmd
);
180 debugfs_create_file("average", 0400, stats
->root
, stats
,
182 debugfs_create_u64("n", 0400, stats
->root
, &stats
->n
);
187 void mlx5_cmdif_debugfs_cleanup(struct mlx5_core_dev
*dev
)
189 debugfs_remove_recursive(dev
->priv
.cmdif_debugfs
);
192 void mlx5_cq_debugfs_init(struct mlx5_core_dev
*dev
)
194 dev
->priv
.cq_debugfs
= debugfs_create_dir("CQs", dev
->priv
.dbg_root
);
197 void mlx5_cq_debugfs_cleanup(struct mlx5_core_dev
*dev
)
199 debugfs_remove_recursive(dev
->priv
.cq_debugfs
);
202 static u64
qp_read_field(struct mlx5_core_dev
*dev
, struct mlx5_core_qp
*qp
,
203 int index
, int *is_str
)
205 int outlen
= MLX5_ST_SZ_BYTES(query_qp_out
);
206 u32 in
[MLX5_ST_SZ_DW(query_qp_in
)] = {};
213 out
= kzalloc(outlen
, GFP_KERNEL
);
217 MLX5_SET(query_qp_in
, in
, opcode
, MLX5_CMD_OP_QUERY_QP
);
218 MLX5_SET(query_qp_in
, in
, qpn
, qp
->qpn
);
219 err
= mlx5_cmd_exec_inout(dev
, query_qp
, in
, out
);
225 qpc
= MLX5_ADDR_OF(query_qp_out
, out
, qpc
);
231 state
= MLX5_GET(qpc
, qpc
, state
);
232 param
= (unsigned long)mlx5_qp_state_str(state
);
236 param
= (unsigned long)mlx5_qp_type_str(MLX5_GET(qpc
, qpc
, st
));
240 switch (MLX5_GET(qpc
, qpc
, mtu
)) {
261 param
= 1 << MLX5_GET(qpc
, qpc
, log_rq_size
);
264 param
= 1 << (MLX5_GET(qpc
, qpc
, log_rq_stride
) + 4);
267 if (!MLX5_GET(qpc
, qpc
, no_sq
))
268 param
= 1 << MLX5_GET(qpc
, qpc
, log_sq_size
);
271 param
= MLX5_GET(qpc
, qpc
, log_page_size
) + 12;
274 param
= MLX5_GET(qpc
, qpc
, remote_qpn
);
282 static u64
eq_read_field(struct mlx5_core_dev
*dev
, struct mlx5_eq
*eq
,
285 int outlen
= MLX5_ST_SZ_BYTES(query_eq_out
);
286 u32 in
[MLX5_ST_SZ_DW(query_eq_in
)] = {};
292 out
= kzalloc(outlen
, GFP_KERNEL
);
296 MLX5_SET(query_eq_in
, in
, opcode
, MLX5_CMD_OP_QUERY_EQ
);
297 MLX5_SET(query_eq_in
, in
, eq_number
, eq
->eqn
);
298 err
= mlx5_cmd_exec_inout(dev
, query_eq
, in
, out
);
300 mlx5_core_warn(dev
, "failed to query eq\n");
303 ctx
= MLX5_ADDR_OF(query_eq_out
, out
, eq_context_entry
);
307 param
= 1 << MLX5_GET(eqc
, ctx
, log_eq_size
);
310 param
= MLX5_GET(eqc
, ctx
, intr
);
313 param
= MLX5_GET(eqc
, ctx
, log_page_size
) + 12;
322 static u64
cq_read_field(struct mlx5_core_dev
*dev
, struct mlx5_core_cq
*cq
,
325 int outlen
= MLX5_ST_SZ_BYTES(query_cq_out
);
331 out
= kvzalloc(outlen
, GFP_KERNEL
);
335 err
= mlx5_core_query_cq(dev
, cq
, out
);
337 mlx5_core_warn(dev
, "failed to query cq\n");
340 ctx
= MLX5_ADDR_OF(query_cq_out
, out
, cq_context
);
347 param
= 1 << MLX5_GET(cqc
, ctx
, log_cq_size
);
350 param
= MLX5_GET(cqc
, ctx
, log_page_size
);
359 static ssize_t
dbg_read(struct file
*filp
, char __user
*buf
, size_t count
,
362 struct mlx5_field_desc
*desc
;
363 struct mlx5_rsc_debug
*d
;
369 desc
= filp
->private_data
;
370 d
= (void *)(desc
- desc
->i
) - sizeof(*d
);
372 case MLX5_DBG_RSC_QP
:
373 field
= qp_read_field(d
->dev
, d
->object
, desc
->i
, &is_str
);
376 case MLX5_DBG_RSC_EQ
:
377 field
= eq_read_field(d
->dev
, d
->object
, desc
->i
);
380 case MLX5_DBG_RSC_CQ
:
381 field
= cq_read_field(d
->dev
, d
->object
, desc
->i
);
385 mlx5_core_warn(d
->dev
, "invalid resource type %d\n", d
->type
);
390 ret
= snprintf(tbuf
, sizeof(tbuf
), "%s\n", (const char *)(unsigned long)field
);
392 ret
= snprintf(tbuf
, sizeof(tbuf
), "0x%llx\n", field
);
394 return simple_read_from_buffer(buf
, count
, pos
, tbuf
, ret
);
397 static const struct file_operations fops
= {
398 .owner
= THIS_MODULE
,
403 static int add_res_tree(struct mlx5_core_dev
*dev
, enum dbg_rsc_type type
,
404 struct dentry
*root
, struct mlx5_rsc_debug
**dbg
,
405 int rsn
, char **field
, int nfile
, void *data
)
407 struct mlx5_rsc_debug
*d
;
411 d
= kzalloc(struct_size(d
, fields
, nfile
), GFP_KERNEL
);
418 sprintf(resn
, "0x%x", rsn
);
419 d
->root
= debugfs_create_dir(resn
, root
);
421 for (i
= 0; i
< nfile
; i
++) {
423 debugfs_create_file(field
[i
], 0400, d
->root
, &d
->fields
[i
],
431 static void rem_res_tree(struct mlx5_rsc_debug
*d
)
433 debugfs_remove_recursive(d
->root
);
437 int mlx5_debug_qp_add(struct mlx5_core_dev
*dev
, struct mlx5_core_qp
*qp
)
441 if (!mlx5_debugfs_root
)
444 err
= add_res_tree(dev
, MLX5_DBG_RSC_QP
, dev
->priv
.qp_debugfs
,
445 &qp
->dbg
, qp
->qpn
, qp_fields
,
446 ARRAY_SIZE(qp_fields
), qp
);
452 EXPORT_SYMBOL(mlx5_debug_qp_add
);
454 void mlx5_debug_qp_remove(struct mlx5_core_dev
*dev
, struct mlx5_core_qp
*qp
)
456 if (!mlx5_debugfs_root
)
460 rem_res_tree(qp
->dbg
);
462 EXPORT_SYMBOL(mlx5_debug_qp_remove
);
464 int mlx5_debug_eq_add(struct mlx5_core_dev
*dev
, struct mlx5_eq
*eq
)
468 if (!mlx5_debugfs_root
)
471 err
= add_res_tree(dev
, MLX5_DBG_RSC_EQ
, dev
->priv
.eq_debugfs
,
472 &eq
->dbg
, eq
->eqn
, eq_fields
,
473 ARRAY_SIZE(eq_fields
), eq
);
480 void mlx5_debug_eq_remove(struct mlx5_core_dev
*dev
, struct mlx5_eq
*eq
)
482 if (!mlx5_debugfs_root
)
486 rem_res_tree(eq
->dbg
);
489 int mlx5_debug_cq_add(struct mlx5_core_dev
*dev
, struct mlx5_core_cq
*cq
)
493 if (!mlx5_debugfs_root
)
496 err
= add_res_tree(dev
, MLX5_DBG_RSC_CQ
, dev
->priv
.cq_debugfs
,
497 &cq
->dbg
, cq
->cqn
, cq_fields
,
498 ARRAY_SIZE(cq_fields
), cq
);
505 void mlx5_debug_cq_remove(struct mlx5_core_dev
*dev
, struct mlx5_core_cq
*cq
)
507 if (!mlx5_debugfs_root
)
511 rem_res_tree(cq
->dbg
);