]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob - drivers/net/ethernet/mellanox/mlx5/core/debugfs.c
Merge tag 'drm/tegra/for-5.1-rc5' of git://anongit.freedesktop.org/tegra/linux into...
[thirdparty/kernel/stable.git] / drivers / net / ethernet / mellanox / mlx5 / core / debugfs.c
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 #include "lib/eq.h"
40
41 enum {
42 QP_PID,
43 QP_STATE,
44 QP_XPORT,
45 QP_MTU,
46 QP_N_RECV,
47 QP_RECV_SZ,
48 QP_N_SEND,
49 QP_LOG_PG_SZ,
50 QP_RQPN,
51 };
52
53 static char *qp_fields[] = {
54 [QP_PID] = "pid",
55 [QP_STATE] = "state",
56 [QP_XPORT] = "transport",
57 [QP_MTU] = "mtu",
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",
63 };
64
65 enum {
66 EQ_NUM_EQES,
67 EQ_INTR,
68 EQ_LOG_PG_SZ,
69 };
70
71 static char *eq_fields[] = {
72 [EQ_NUM_EQES] = "num_eqes",
73 [EQ_INTR] = "intr",
74 [EQ_LOG_PG_SZ] = "log_page_size",
75 };
76
77 enum {
78 CQ_PID,
79 CQ_NUM_CQES,
80 CQ_LOG_PG_SZ,
81 };
82
83 static char *cq_fields[] = {
84 [CQ_PID] = "pid",
85 [CQ_NUM_CQES] = "num_cqes",
86 [CQ_LOG_PG_SZ] = "log_page_size",
87 };
88
89 struct dentry *mlx5_debugfs_root;
90 EXPORT_SYMBOL(mlx5_debugfs_root);
91
92 void mlx5_register_debugfs(void)
93 {
94 mlx5_debugfs_root = debugfs_create_dir("mlx5", NULL);
95 if (IS_ERR_OR_NULL(mlx5_debugfs_root))
96 mlx5_debugfs_root = NULL;
97 }
98
99 void mlx5_unregister_debugfs(void)
100 {
101 debugfs_remove(mlx5_debugfs_root);
102 }
103
104 int mlx5_qp_debugfs_init(struct mlx5_core_dev *dev)
105 {
106 if (!mlx5_debugfs_root)
107 return 0;
108
109 atomic_set(&dev->num_qps, 0);
110
111 dev->priv.qp_debugfs = debugfs_create_dir("QPs", dev->priv.dbg_root);
112 if (!dev->priv.qp_debugfs)
113 return -ENOMEM;
114
115 return 0;
116 }
117
118 void mlx5_qp_debugfs_cleanup(struct mlx5_core_dev *dev)
119 {
120 if (!mlx5_debugfs_root)
121 return;
122
123 debugfs_remove_recursive(dev->priv.qp_debugfs);
124 }
125
126 int mlx5_eq_debugfs_init(struct mlx5_core_dev *dev)
127 {
128 if (!mlx5_debugfs_root)
129 return 0;
130
131 dev->priv.eq_debugfs = debugfs_create_dir("EQs", dev->priv.dbg_root);
132 if (!dev->priv.eq_debugfs)
133 return -ENOMEM;
134
135 return 0;
136 }
137
138 void mlx5_eq_debugfs_cleanup(struct mlx5_core_dev *dev)
139 {
140 if (!mlx5_debugfs_root)
141 return;
142
143 debugfs_remove_recursive(dev->priv.eq_debugfs);
144 }
145
146 static ssize_t average_read(struct file *filp, char __user *buf, size_t count,
147 loff_t *pos)
148 {
149 struct mlx5_cmd_stats *stats;
150 u64 field = 0;
151 int ret;
152 char tbuf[22];
153
154 stats = filp->private_data;
155 spin_lock_irq(&stats->lock);
156 if (stats->n)
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);
161 }
162
163 static ssize_t average_write(struct file *filp, const char __user *buf,
164 size_t count, loff_t *pos)
165 {
166 struct mlx5_cmd_stats *stats;
167
168 stats = filp->private_data;
169 spin_lock_irq(&stats->lock);
170 stats->sum = 0;
171 stats->n = 0;
172 spin_unlock_irq(&stats->lock);
173
174 *pos += count;
175
176 return count;
177 }
178
179 static const struct file_operations stats_fops = {
180 .owner = THIS_MODULE,
181 .open = simple_open,
182 .read = average_read,
183 .write = average_write,
184 };
185
186 int mlx5_cmdif_debugfs_init(struct mlx5_core_dev *dev)
187 {
188 struct mlx5_cmd_stats *stats;
189 struct dentry **cmd;
190 const char *namep;
191 int err;
192 int i;
193
194 if (!mlx5_debugfs_root)
195 return 0;
196
197 cmd = &dev->priv.cmdif_debugfs;
198 *cmd = debugfs_create_dir("commands", dev->priv.dbg_root);
199 if (!*cmd)
200 return -ENOMEM;
201
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);
207 if (!stats->root) {
208 mlx5_core_warn(dev, "failed adding command %d\n",
209 i);
210 err = -ENOMEM;
211 goto out;
212 }
213
214 stats->avg = debugfs_create_file("average", 0400,
215 stats->root, stats,
216 &stats_fops);
217 if (!stats->avg) {
218 mlx5_core_warn(dev, "failed creating debugfs file\n");
219 err = -ENOMEM;
220 goto out;
221 }
222
223 stats->count = debugfs_create_u64("n", 0400,
224 stats->root,
225 &stats->n);
226 if (!stats->count) {
227 mlx5_core_warn(dev, "failed creating debugfs file\n");
228 err = -ENOMEM;
229 goto out;
230 }
231 }
232 }
233
234 return 0;
235 out:
236 debugfs_remove_recursive(dev->priv.cmdif_debugfs);
237 return err;
238 }
239
240 void mlx5_cmdif_debugfs_cleanup(struct mlx5_core_dev *dev)
241 {
242 if (!mlx5_debugfs_root)
243 return;
244
245 debugfs_remove_recursive(dev->priv.cmdif_debugfs);
246 }
247
248 int mlx5_cq_debugfs_init(struct mlx5_core_dev *dev)
249 {
250 if (!mlx5_debugfs_root)
251 return 0;
252
253 dev->priv.cq_debugfs = debugfs_create_dir("CQs", dev->priv.dbg_root);
254 if (!dev->priv.cq_debugfs)
255 return -ENOMEM;
256
257 return 0;
258 }
259
260 void mlx5_cq_debugfs_cleanup(struct mlx5_core_dev *dev)
261 {
262 if (!mlx5_debugfs_root)
263 return;
264
265 debugfs_remove_recursive(dev->priv.cq_debugfs);
266 }
267
268 static u64 qp_read_field(struct mlx5_core_dev *dev, struct mlx5_core_qp *qp,
269 int index, int *is_str)
270 {
271 int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
272 struct mlx5_qp_context *ctx;
273 u64 param = 0;
274 u32 *out;
275 int err;
276 int no_sq;
277
278 out = kzalloc(outlen, GFP_KERNEL);
279 if (!out)
280 return param;
281
282 err = mlx5_core_qp_query(dev, qp, out, outlen);
283 if (err) {
284 mlx5_core_warn(dev, "failed to query qp err=%d\n", err);
285 goto out;
286 }
287
288 *is_str = 0;
289
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);
292
293 switch (index) {
294 case QP_PID:
295 param = qp->pid;
296 break;
297 case QP_STATE:
298 param = (unsigned long)mlx5_qp_state_str(be32_to_cpu(ctx->flags) >> 28);
299 *is_str = 1;
300 break;
301 case QP_XPORT:
302 param = (unsigned long)mlx5_qp_type_str((be32_to_cpu(ctx->flags) >> 16) & 0xff);
303 *is_str = 1;
304 break;
305 case QP_MTU:
306 switch (ctx->mtu_msgmax >> 5) {
307 case IB_MTU_256:
308 param = 256;
309 break;
310 case IB_MTU_512:
311 param = 512;
312 break;
313 case IB_MTU_1024:
314 param = 1024;
315 break;
316 case IB_MTU_2048:
317 param = 2048;
318 break;
319 case IB_MTU_4096:
320 param = 4096;
321 break;
322 default:
323 param = 0;
324 }
325 break;
326 case QP_N_RECV:
327 param = 1 << ((ctx->rq_size_stride >> 3) & 0xf);
328 break;
329 case QP_RECV_SZ:
330 param = 1 << ((ctx->rq_size_stride & 7) + 4);
331 break;
332 case QP_N_SEND:
333 no_sq = be16_to_cpu(ctx->sq_crq_size) >> 15;
334 if (!no_sq)
335 param = 1 << (be16_to_cpu(ctx->sq_crq_size) >> 11);
336 else
337 param = 0;
338 break;
339 case QP_LOG_PG_SZ:
340 param = (be32_to_cpu(ctx->log_pg_sz_remote_qpn) >> 24) & 0x1f;
341 param += 12;
342 break;
343 case QP_RQPN:
344 param = be32_to_cpu(ctx->log_pg_sz_remote_qpn) & 0xffffff;
345 break;
346 }
347
348 out:
349 kfree(out);
350 return param;
351 }
352
353 static int mlx5_core_eq_query(struct mlx5_core_dev *dev, struct mlx5_eq *eq,
354 u32 *out, int outlen)
355 {
356 u32 in[MLX5_ST_SZ_DW(query_eq_in)] = {};
357
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);
361 }
362
363 static u64 eq_read_field(struct mlx5_core_dev *dev, struct mlx5_eq *eq,
364 int index)
365 {
366 int outlen = MLX5_ST_SZ_BYTES(query_eq_out);
367 u64 param = 0;
368 void *ctx;
369 u32 *out;
370 int err;
371
372 out = kzalloc(outlen, GFP_KERNEL);
373 if (!out)
374 return param;
375
376 err = mlx5_core_eq_query(dev, eq, out, outlen);
377 if (err) {
378 mlx5_core_warn(dev, "failed to query eq\n");
379 goto out;
380 }
381 ctx = MLX5_ADDR_OF(query_eq_out, out, eq_context_entry);
382
383 switch (index) {
384 case EQ_NUM_EQES:
385 param = 1 << MLX5_GET(eqc, ctx, log_eq_size);
386 break;
387 case EQ_INTR:
388 param = MLX5_GET(eqc, ctx, intr);
389 break;
390 case EQ_LOG_PG_SZ:
391 param = MLX5_GET(eqc, ctx, log_page_size) + 12;
392 break;
393 }
394
395 out:
396 kfree(out);
397 return param;
398 }
399
400 static u64 cq_read_field(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
401 int index)
402 {
403 int outlen = MLX5_ST_SZ_BYTES(query_cq_out);
404 u64 param = 0;
405 void *ctx;
406 u32 *out;
407 int err;
408
409 out = kvzalloc(outlen, GFP_KERNEL);
410 if (!out)
411 return param;
412
413 err = mlx5_core_query_cq(dev, cq, out, outlen);
414 if (err) {
415 mlx5_core_warn(dev, "failed to query cq\n");
416 goto out;
417 }
418 ctx = MLX5_ADDR_OF(query_cq_out, out, cq_context);
419
420 switch (index) {
421 case CQ_PID:
422 param = cq->pid;
423 break;
424 case CQ_NUM_CQES:
425 param = 1 << MLX5_GET(cqc, ctx, log_cq_size);
426 break;
427 case CQ_LOG_PG_SZ:
428 param = MLX5_GET(cqc, ctx, log_page_size);
429 break;
430 }
431
432 out:
433 kvfree(out);
434 return param;
435 }
436
437 static ssize_t dbg_read(struct file *filp, char __user *buf, size_t count,
438 loff_t *pos)
439 {
440 struct mlx5_field_desc *desc;
441 struct mlx5_rsc_debug *d;
442 char tbuf[18];
443 int is_str = 0;
444 u64 field;
445 int ret;
446
447 desc = filp->private_data;
448 d = (void *)(desc - desc->i) - sizeof(*d);
449 switch (d->type) {
450 case MLX5_DBG_RSC_QP:
451 field = qp_read_field(d->dev, d->object, desc->i, &is_str);
452 break;
453
454 case MLX5_DBG_RSC_EQ:
455 field = eq_read_field(d->dev, d->object, desc->i);
456 break;
457
458 case MLX5_DBG_RSC_CQ:
459 field = cq_read_field(d->dev, d->object, desc->i);
460 break;
461
462 default:
463 mlx5_core_warn(d->dev, "invalid resource type %d\n", d->type);
464 return -EINVAL;
465 }
466
467 if (is_str)
468 ret = snprintf(tbuf, sizeof(tbuf), "%s\n", (const char *)(unsigned long)field);
469 else
470 ret = snprintf(tbuf, sizeof(tbuf), "0x%llx\n", field);
471
472 return simple_read_from_buffer(buf, count, pos, tbuf, ret);
473 }
474
475 static const struct file_operations fops = {
476 .owner = THIS_MODULE,
477 .open = simple_open,
478 .read = dbg_read,
479 };
480
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)
484 {
485 struct mlx5_rsc_debug *d;
486 char resn[32];
487 int err;
488 int i;
489
490 d = kzalloc(struct_size(d, fields, nfile), GFP_KERNEL);
491 if (!d)
492 return -ENOMEM;
493
494 d->dev = dev;
495 d->object = data;
496 d->type = type;
497 sprintf(resn, "0x%x", rsn);
498 d->root = debugfs_create_dir(resn, root);
499 if (!d->root) {
500 err = -ENOMEM;
501 goto out_free;
502 }
503
504 for (i = 0; i < nfile; i++) {
505 d->fields[i].i = i;
506 d->fields[i].dent = debugfs_create_file(field[i], 0400,
507 d->root, &d->fields[i],
508 &fops);
509 if (!d->fields[i].dent) {
510 err = -ENOMEM;
511 goto out_rem;
512 }
513 }
514 *dbg = d;
515
516 return 0;
517 out_rem:
518 debugfs_remove_recursive(d->root);
519
520 out_free:
521 kfree(d);
522 return err;
523 }
524
525 static void rem_res_tree(struct mlx5_rsc_debug *d)
526 {
527 debugfs_remove_recursive(d->root);
528 kfree(d);
529 }
530
531 int mlx5_debug_qp_add(struct mlx5_core_dev *dev, struct mlx5_core_qp *qp)
532 {
533 int err;
534
535 if (!mlx5_debugfs_root)
536 return 0;
537
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);
541 if (err)
542 qp->dbg = NULL;
543
544 return err;
545 }
546
547 void mlx5_debug_qp_remove(struct mlx5_core_dev *dev, struct mlx5_core_qp *qp)
548 {
549 if (!mlx5_debugfs_root)
550 return;
551
552 if (qp->dbg)
553 rem_res_tree(qp->dbg);
554 }
555
556 int mlx5_debug_eq_add(struct mlx5_core_dev *dev, struct mlx5_eq *eq)
557 {
558 int err;
559
560 if (!mlx5_debugfs_root)
561 return 0;
562
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);
566 if (err)
567 eq->dbg = NULL;
568
569 return err;
570 }
571
572 void mlx5_debug_eq_remove(struct mlx5_core_dev *dev, struct mlx5_eq *eq)
573 {
574 if (!mlx5_debugfs_root)
575 return;
576
577 if (eq->dbg)
578 rem_res_tree(eq->dbg);
579 }
580
581 int mlx5_debug_cq_add(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq)
582 {
583 int err;
584
585 if (!mlx5_debugfs_root)
586 return 0;
587
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);
591 if (err)
592 cq->dbg = NULL;
593
594 return err;
595 }
596
597 void mlx5_debug_cq_remove(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq)
598 {
599 if (!mlx5_debugfs_root)
600 return;
601
602 if (cq->dbg)
603 rem_res_tree(cq->dbg);
604 }