2 * Copyright (c) 2013, Mellanox Technologies inc. 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"
52 static char *qp_fields
[] = {
55 [QP_XPORT
] = "transport",
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",
70 static char *eq_fields
[] = {
71 [EQ_NUM_EQES
] = "num_eqes",
73 [EQ_LOG_PG_SZ
] = "log_page_size",
82 static char *cq_fields
[] = {
84 [CQ_NUM_CQES
] = "num_cqes",
85 [CQ_LOG_PG_SZ
] = "log_page_size",
88 struct dentry
*mlx5_debugfs_root
;
89 EXPORT_SYMBOL(mlx5_debugfs_root
);
91 void mlx5_register_debugfs(void)
93 mlx5_debugfs_root
= debugfs_create_dir("mlx5", NULL
);
94 if (IS_ERR_OR_NULL(mlx5_debugfs_root
))
95 mlx5_debugfs_root
= NULL
;
98 void mlx5_unregister_debugfs(void)
100 debugfs_remove(mlx5_debugfs_root
);
103 int mlx5_qp_debugfs_init(struct mlx5_core_dev
*dev
)
105 if (!mlx5_debugfs_root
)
108 atomic_set(&dev
->num_qps
, 0);
110 dev
->priv
.qp_debugfs
= debugfs_create_dir("QPs", dev
->priv
.dbg_root
);
111 if (!dev
->priv
.qp_debugfs
)
117 void mlx5_qp_debugfs_cleanup(struct mlx5_core_dev
*dev
)
119 if (!mlx5_debugfs_root
)
122 debugfs_remove_recursive(dev
->priv
.qp_debugfs
);
125 int mlx5_eq_debugfs_init(struct mlx5_core_dev
*dev
)
127 if (!mlx5_debugfs_root
)
130 dev
->priv
.eq_debugfs
= debugfs_create_dir("EQs", dev
->priv
.dbg_root
);
131 if (!dev
->priv
.eq_debugfs
)
137 void mlx5_eq_debugfs_cleanup(struct mlx5_core_dev
*dev
)
139 if (!mlx5_debugfs_root
)
142 debugfs_remove_recursive(dev
->priv
.eq_debugfs
);
145 static ssize_t
average_read(struct file
*filp
, char __user
*buf
, size_t count
,
148 struct mlx5_cmd_stats
*stats
;
156 stats
= filp
->private_data
;
157 spin_lock(&stats
->lock
);
159 field
= stats
->sum
/ stats
->n
;
160 spin_unlock(&stats
->lock
);
161 ret
= snprintf(tbuf
, sizeof(tbuf
), "%llu\n", field
);
163 if (copy_to_user(buf
, tbuf
, ret
))
172 static ssize_t
average_write(struct file
*filp
, const char __user
*buf
,
173 size_t count
, loff_t
*pos
)
175 struct mlx5_cmd_stats
*stats
;
177 stats
= filp
->private_data
;
178 spin_lock(&stats
->lock
);
181 spin_unlock(&stats
->lock
);
188 static const struct file_operations stats_fops
= {
189 .owner
= THIS_MODULE
,
191 .read
= average_read
,
192 .write
= average_write
,
195 int mlx5_cmdif_debugfs_init(struct mlx5_core_dev
*dev
)
197 struct mlx5_cmd_stats
*stats
;
203 if (!mlx5_debugfs_root
)
206 cmd
= &dev
->priv
.cmdif_debugfs
;
207 *cmd
= debugfs_create_dir("commands", dev
->priv
.dbg_root
);
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
);
217 mlx5_core_warn(dev
, "failed adding command %d\n",
223 stats
->avg
= debugfs_create_file("average", 0400,
227 mlx5_core_warn(dev
, "failed creating debugfs file\n");
232 stats
->count
= debugfs_create_u64("n", 0400,
236 mlx5_core_warn(dev
, "failed creating debugfs file\n");
245 debugfs_remove_recursive(dev
->priv
.cmdif_debugfs
);
249 void mlx5_cmdif_debugfs_cleanup(struct mlx5_core_dev
*dev
)
251 if (!mlx5_debugfs_root
)
254 debugfs_remove_recursive(dev
->priv
.cmdif_debugfs
);
257 int mlx5_cq_debugfs_init(struct mlx5_core_dev
*dev
)
259 if (!mlx5_debugfs_root
)
262 dev
->priv
.cq_debugfs
= debugfs_create_dir("CQs", dev
->priv
.dbg_root
);
263 if (!dev
->priv
.cq_debugfs
)
269 void mlx5_cq_debugfs_cleanup(struct mlx5_core_dev
*dev
)
271 if (!mlx5_debugfs_root
)
274 debugfs_remove_recursive(dev
->priv
.cq_debugfs
);
277 static u64
qp_read_field(struct mlx5_core_dev
*dev
, struct mlx5_core_qp
*qp
,
280 struct mlx5_query_qp_mbox_out
*out
;
281 struct mlx5_qp_context
*ctx
;
286 out
= kzalloc(sizeof(*out
), GFP_KERNEL
);
290 err
= mlx5_core_qp_query(dev
, qp
, out
, sizeof(*out
));
292 mlx5_core_warn(dev
, "failed to query qp\n");
302 param
= be32_to_cpu(ctx
->flags
) >> 28;
305 param
= (be32_to_cpu(ctx
->flags
) >> 16) & 0xff;
308 param
= ctx
->mtu_msgmax
>> 5;
311 param
= 1 << ((ctx
->rq_size_stride
>> 3) & 0xf);
314 param
= 1 << ((ctx
->rq_size_stride
& 7) + 4);
317 no_sq
= be16_to_cpu(ctx
->sq_crq_size
) >> 15;
319 param
= 1 << (be16_to_cpu(ctx
->sq_crq_size
) >> 11);
324 param
= (be32_to_cpu(ctx
->log_pg_sz_remote_qpn
) >> 24) & 0x1f;
328 param
= be32_to_cpu(ctx
->log_pg_sz_remote_qpn
) & 0xffffff;
337 static u64
eq_read_field(struct mlx5_core_dev
*dev
, struct mlx5_eq
*eq
,
340 struct mlx5_query_eq_mbox_out
*out
;
341 struct mlx5_eq_context
*ctx
;
345 out
= kzalloc(sizeof(*out
), GFP_KERNEL
);
351 err
= mlx5_core_eq_query(dev
, eq
, out
, sizeof(*out
));
353 mlx5_core_warn(dev
, "failed to query eq\n");
359 param
= 1 << ((be32_to_cpu(ctx
->log_sz_usr_page
) >> 24) & 0x1f);
365 param
= (ctx
->log_page_size
& 0x1f) + 12;
374 static u64
cq_read_field(struct mlx5_core_dev
*dev
, struct mlx5_core_cq
*cq
,
377 struct mlx5_query_cq_mbox_out
*out
;
378 struct mlx5_cq_context
*ctx
;
382 out
= kzalloc(sizeof(*out
), GFP_KERNEL
);
388 err
= mlx5_core_query_cq(dev
, cq
, out
);
390 mlx5_core_warn(dev
, "failed to query cq\n");
399 param
= 1 << ((be32_to_cpu(ctx
->log_sz_usr_page
) >> 24) & 0x1f);
402 param
= (ctx
->log_pg_sz
& 0x1f) + 12;
411 static ssize_t
dbg_read(struct file
*filp
, char __user
*buf
, size_t count
,
414 struct mlx5_field_desc
*desc
;
415 struct mlx5_rsc_debug
*d
;
423 desc
= filp
->private_data
;
424 d
= (void *)(desc
- desc
->i
) - sizeof(*d
);
426 case MLX5_DBG_RSC_QP
:
427 field
= qp_read_field(d
->dev
, d
->object
, desc
->i
);
430 case MLX5_DBG_RSC_EQ
:
431 field
= eq_read_field(d
->dev
, d
->object
, desc
->i
);
434 case MLX5_DBG_RSC_CQ
:
435 field
= cq_read_field(d
->dev
, d
->object
, desc
->i
);
439 mlx5_core_warn(d
->dev
, "invalid resource type %d\n", d
->type
);
443 ret
= snprintf(tbuf
, sizeof(tbuf
), "0x%llx\n", field
);
445 if (copy_to_user(buf
, tbuf
, ret
))
453 static const struct file_operations fops
= {
454 .owner
= THIS_MODULE
,
459 static int add_res_tree(struct mlx5_core_dev
*dev
, enum dbg_rsc_type type
,
460 struct dentry
*root
, struct mlx5_rsc_debug
**dbg
,
461 int rsn
, char **field
, int nfile
, void *data
)
463 struct mlx5_rsc_debug
*d
;
468 d
= kzalloc(sizeof(*d
) + nfile
* sizeof(d
->fields
[0]), GFP_KERNEL
);
475 sprintf(resn
, "0x%x", rsn
);
476 d
->root
= debugfs_create_dir(resn
, root
);
482 for (i
= 0; i
< nfile
; i
++) {
484 d
->fields
[i
].dent
= debugfs_create_file(field
[i
], 0400,
485 d
->root
, &d
->fields
[i
],
487 if (!d
->fields
[i
].dent
) {
496 debugfs_remove_recursive(d
->root
);
503 static void rem_res_tree(struct mlx5_rsc_debug
*d
)
505 debugfs_remove_recursive(d
->root
);
509 int mlx5_debug_qp_add(struct mlx5_core_dev
*dev
, struct mlx5_core_qp
*qp
)
513 if (!mlx5_debugfs_root
)
516 err
= add_res_tree(dev
, MLX5_DBG_RSC_QP
, dev
->priv
.qp_debugfs
,
517 &qp
->dbg
, qp
->qpn
, qp_fields
,
518 ARRAY_SIZE(qp_fields
), qp
);
525 void mlx5_debug_qp_remove(struct mlx5_core_dev
*dev
, struct mlx5_core_qp
*qp
)
527 if (!mlx5_debugfs_root
)
531 rem_res_tree(qp
->dbg
);
535 int mlx5_debug_eq_add(struct mlx5_core_dev
*dev
, struct mlx5_eq
*eq
)
539 if (!mlx5_debugfs_root
)
542 err
= add_res_tree(dev
, MLX5_DBG_RSC_EQ
, dev
->priv
.eq_debugfs
,
543 &eq
->dbg
, eq
->eqn
, eq_fields
,
544 ARRAY_SIZE(eq_fields
), eq
);
551 void mlx5_debug_eq_remove(struct mlx5_core_dev
*dev
, struct mlx5_eq
*eq
)
553 if (!mlx5_debugfs_root
)
557 rem_res_tree(eq
->dbg
);
560 int mlx5_debug_cq_add(struct mlx5_core_dev
*dev
, struct mlx5_core_cq
*cq
)
564 if (!mlx5_debugfs_root
)
567 err
= add_res_tree(dev
, MLX5_DBG_RSC_CQ
, dev
->priv
.cq_debugfs
,
568 &cq
->dbg
, cq
->cqn
, cq_fields
,
569 ARRAY_SIZE(cq_fields
), cq
);
576 void mlx5_debug_cq_remove(struct mlx5_core_dev
*dev
, struct mlx5_core_cq
*cq
)
578 if (!mlx5_debugfs_root
)
582 rem_res_tree(cq
->dbg
);