mlx5: Add driver for Mellanox Connect-IB adapters
[cascardo/linux.git] / drivers / net / ethernet / mellanox / mlx5 / core / debugfs.c
1 /*
2  * Copyright (c) 2013, Mellanox Technologies inc.  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         int err;
152         char tbuf[22];
153
154         if (*pos)
155                 return 0;
156
157         stats = filp->private_data;
158         spin_lock(&stats->lock);
159         if (stats->n)
160                 field = stats->sum / stats->n;
161         spin_unlock(&stats->lock);
162         ret = snprintf(tbuf, sizeof(tbuf), "%llu\n", field);
163         if (ret > 0) {
164                 err = copy_to_user(buf, tbuf, ret);
165                 if (err)
166                         return err;
167         }
168
169         *pos += ret;
170         return ret;
171 }
172
173
174 static ssize_t average_write(struct file *filp, const char __user *buf,
175                              size_t count, loff_t *pos)
176 {
177         struct mlx5_cmd_stats *stats;
178
179         stats = filp->private_data;
180         spin_lock(&stats->lock);
181         stats->sum = 0;
182         stats->n = 0;
183         spin_unlock(&stats->lock);
184
185         *pos += count;
186
187         return count;
188 }
189
190 static const struct file_operations stats_fops = {
191         .owner  = THIS_MODULE,
192         .open   = simple_open,
193         .read   = average_read,
194         .write  = average_write,
195 };
196
197 int mlx5_cmdif_debugfs_init(struct mlx5_core_dev *dev)
198 {
199         struct mlx5_cmd_stats *stats;
200         struct dentry **cmd;
201         const char *namep;
202         int err;
203         int i;
204
205         if (!mlx5_debugfs_root)
206                 return 0;
207
208         cmd = &dev->priv.cmdif_debugfs;
209         *cmd = debugfs_create_dir("commands", dev->priv.dbg_root);
210         if (!*cmd)
211                 return -ENOMEM;
212
213         for (i = 0; i < ARRAY_SIZE(dev->cmd.stats); i++) {
214                 stats = &dev->cmd.stats[i];
215                 namep = mlx5_command_str(i);
216                 if (strcmp(namep, "unknown command opcode")) {
217                         stats->root = debugfs_create_dir(namep, *cmd);
218                         if (!stats->root) {
219                                 mlx5_core_warn(dev, "failed adding command %d\n",
220                                                i);
221                                 err = -ENOMEM;
222                                 goto out;
223                         }
224
225                         stats->avg = debugfs_create_file("average", 0400,
226                                                          stats->root, stats,
227                                                          &stats_fops);
228                         if (!stats->avg) {
229                                 mlx5_core_warn(dev, "failed creating debugfs file\n");
230                                 err = -ENOMEM;
231                                 goto out;
232                         }
233
234                         stats->count = debugfs_create_u64("n", 0400,
235                                                           stats->root,
236                                                           &stats->n);
237                         if (!stats->count) {
238                                 mlx5_core_warn(dev, "failed creating debugfs file\n");
239                                 err = -ENOMEM;
240                                 goto out;
241                         }
242                 }
243         }
244
245         return 0;
246 out:
247         debugfs_remove_recursive(dev->priv.cmdif_debugfs);
248         return err;
249 }
250
251 void mlx5_cmdif_debugfs_cleanup(struct mlx5_core_dev *dev)
252 {
253         if (!mlx5_debugfs_root)
254                 return;
255
256         debugfs_remove_recursive(dev->priv.cmdif_debugfs);
257 }
258
259 int mlx5_cq_debugfs_init(struct mlx5_core_dev *dev)
260 {
261         if (!mlx5_debugfs_root)
262                 return 0;
263
264         dev->priv.cq_debugfs = debugfs_create_dir("CQs",  dev->priv.dbg_root);
265         if (!dev->priv.cq_debugfs)
266                 return -ENOMEM;
267
268         return 0;
269 }
270
271 void mlx5_cq_debugfs_cleanup(struct mlx5_core_dev *dev)
272 {
273         if (!mlx5_debugfs_root)
274                 return;
275
276         debugfs_remove_recursive(dev->priv.cq_debugfs);
277 }
278
279 static u64 qp_read_field(struct mlx5_core_dev *dev, struct mlx5_core_qp *qp,
280                          int index)
281 {
282         struct mlx5_query_qp_mbox_out *out;
283         struct mlx5_qp_context *ctx;
284         u64 param = 0;
285         int err;
286         int no_sq;
287
288         out = kzalloc(sizeof(*out), GFP_KERNEL);
289         if (!out)
290                 return param;
291
292         err = mlx5_core_qp_query(dev, qp, out, sizeof(*out));
293         if (err) {
294                 mlx5_core_warn(dev, "failed to query qp\n");
295                 goto out;
296         }
297
298         ctx = &out->ctx;
299         switch (index) {
300         case QP_PID:
301                 param = qp->pid;
302                 break;
303         case QP_STATE:
304                 param = be32_to_cpu(ctx->flags) >> 28;
305                 break;
306         case QP_XPORT:
307                 param = (be32_to_cpu(ctx->flags) >> 16) & 0xff;
308                 break;
309         case QP_MTU:
310                 param = ctx->mtu_msgmax >> 5;
311                 break;
312         case QP_N_RECV:
313                 param = 1 << ((ctx->rq_size_stride >> 3) & 0xf);
314                 break;
315         case QP_RECV_SZ:
316                 param = 1 << ((ctx->rq_size_stride & 7) + 4);
317                 break;
318         case QP_N_SEND:
319                 no_sq = be16_to_cpu(ctx->sq_crq_size) >> 15;
320                 if (!no_sq)
321                         param = 1 << (be16_to_cpu(ctx->sq_crq_size) >> 11);
322                 else
323                         param = 0;
324                 break;
325         case QP_LOG_PG_SZ:
326                 param = ((cpu_to_be32(ctx->log_pg_sz_remote_qpn) >> 24) & 0x1f);
327                 param += 12;
328                 break;
329         case QP_RQPN:
330                 param = cpu_to_be32(ctx->log_pg_sz_remote_qpn) & 0xffffff;
331                 break;
332         }
333
334 out:
335         kfree(out);
336         return param;
337 }
338
339 static u64 eq_read_field(struct mlx5_core_dev *dev, struct mlx5_eq *eq,
340                          int index)
341 {
342         struct mlx5_query_eq_mbox_out *out;
343         struct mlx5_eq_context *ctx;
344         u64 param = 0;
345         int err;
346
347         out = kzalloc(sizeof(*out), GFP_KERNEL);
348         if (!out)
349                 return param;
350
351         ctx = &out->ctx;
352
353         err = mlx5_core_eq_query(dev, eq, out, sizeof(*out));
354         if (err) {
355                 mlx5_core_warn(dev, "failed to query eq\n");
356                 goto out;
357         }
358
359         switch (index) {
360         case EQ_NUM_EQES:
361                 param = 1 << ((be32_to_cpu(ctx->log_sz_usr_page) >> 24) & 0x1f);
362                 break;
363         case EQ_INTR:
364                 param = ctx->intr;
365                 break;
366         case EQ_LOG_PG_SZ:
367                 param = (ctx->log_page_size & 0x1f) + 12;
368                 break;
369         }
370
371 out:
372         kfree(out);
373         return param;
374 }
375
376 static u64 cq_read_field(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
377                          int index)
378 {
379         struct mlx5_query_cq_mbox_out *out;
380         struct mlx5_cq_context *ctx;
381         u64 param = 0;
382         int err;
383
384         out = kzalloc(sizeof(*out), GFP_KERNEL);
385         if (!out)
386                 return param;
387
388         ctx = &out->ctx;
389
390         err = mlx5_core_query_cq(dev, cq, out);
391         if (err) {
392                 mlx5_core_warn(dev, "failed to query cq\n");
393                 goto out;
394         }
395
396         switch (index) {
397         case CQ_PID:
398                 param = cq->pid;
399                 break;
400         case CQ_NUM_CQES:
401                 param = 1 << ((be32_to_cpu(ctx->log_sz_usr_page) >> 24) & 0x1f);
402                 break;
403         case CQ_LOG_PG_SZ:
404                 param = (ctx->log_pg_sz & 0x1f) + 12;
405                 break;
406         }
407
408 out:
409         kfree(out);
410         return param;
411 }
412
413 static ssize_t dbg_read(struct file *filp, char __user *buf, size_t count,
414                         loff_t *pos)
415 {
416         struct mlx5_field_desc *desc;
417         struct mlx5_rsc_debug *d;
418         char tbuf[18];
419         u64 field;
420         int ret;
421         int err;
422
423         if (*pos)
424                 return 0;
425
426         desc = filp->private_data;
427         d = (void *)(desc - desc->i) - sizeof(*d);
428         switch (d->type) {
429         case MLX5_DBG_RSC_QP:
430                 field = qp_read_field(d->dev, d->object, desc->i);
431                 break;
432
433         case MLX5_DBG_RSC_EQ:
434                 field = eq_read_field(d->dev, d->object, desc->i);
435                 break;
436
437         case MLX5_DBG_RSC_CQ:
438                 field = cq_read_field(d->dev, d->object, desc->i);
439                 break;
440
441         default:
442                 mlx5_core_warn(d->dev, "invalid resource type %d\n", d->type);
443                 return -EINVAL;
444         }
445
446         ret = snprintf(tbuf, sizeof(tbuf), "0x%llx\n", field);
447         if (ret > 0) {
448                 err = copy_to_user(buf, tbuf, ret);
449                 if (err)
450                         return err;
451         }
452
453         *pos += ret;
454         return ret;
455 }
456
457 static const struct file_operations fops = {
458         .owner  = THIS_MODULE,
459         .open   = simple_open,
460         .read   = dbg_read,
461 };
462
463 static int add_res_tree(struct mlx5_core_dev *dev, enum dbg_rsc_type type,
464                         struct dentry *root, struct mlx5_rsc_debug **dbg,
465                         int rsn, char **field, int nfile, void *data)
466 {
467         struct mlx5_rsc_debug *d;
468         char resn[32];
469         int err;
470         int i;
471
472         d = kzalloc(sizeof(*d) + nfile * sizeof(d->fields[0]), GFP_KERNEL);
473         if (!d)
474                 return -ENOMEM;
475
476         d->dev = dev;
477         d->object = data;
478         d->type = type;
479         sprintf(resn, "0x%x", rsn);
480         d->root = debugfs_create_dir(resn,  root);
481         if (!d->root) {
482                 err = -ENOMEM;
483                 goto out_free;
484         }
485
486         for (i = 0; i < nfile; i++) {
487                 d->fields[i].i = i;
488                 d->fields[i].dent = debugfs_create_file(field[i], 0400,
489                                                         d->root, &d->fields[i],
490                                                         &fops);
491                 if (!d->fields[i].dent) {
492                         err = -ENOMEM;
493                         goto out_rem;
494                 }
495         }
496         *dbg = d;
497
498         return 0;
499 out_rem:
500         debugfs_remove_recursive(d->root);
501
502 out_free:
503         kfree(d);
504         return err;
505 }
506
507 static void rem_res_tree(struct mlx5_rsc_debug *d)
508 {
509         debugfs_remove_recursive(d->root);
510         kfree(d);
511 }
512
513 int mlx5_debug_qp_add(struct mlx5_core_dev *dev, struct mlx5_core_qp *qp)
514 {
515         int err;
516
517         if (!mlx5_debugfs_root)
518                 return 0;
519
520         err = add_res_tree(dev, MLX5_DBG_RSC_QP, dev->priv.qp_debugfs,
521                            &qp->dbg, qp->qpn, qp_fields,
522                            ARRAY_SIZE(qp_fields), qp);
523         if (err)
524                 qp->dbg = NULL;
525
526         return err;
527 }
528
529 void mlx5_debug_qp_remove(struct mlx5_core_dev *dev, struct mlx5_core_qp *qp)
530 {
531         if (!mlx5_debugfs_root)
532                 return;
533
534         if (qp->dbg)
535                 rem_res_tree(qp->dbg);
536 }
537
538
539 int mlx5_debug_eq_add(struct mlx5_core_dev *dev, struct mlx5_eq *eq)
540 {
541         int err;
542
543         if (!mlx5_debugfs_root)
544                 return 0;
545
546         err = add_res_tree(dev, MLX5_DBG_RSC_EQ, dev->priv.eq_debugfs,
547                            &eq->dbg, eq->eqn, eq_fields,
548                            ARRAY_SIZE(eq_fields), eq);
549         if (err)
550                 eq->dbg = NULL;
551
552         return err;
553 }
554
555 void mlx5_debug_eq_remove(struct mlx5_core_dev *dev, struct mlx5_eq *eq)
556 {
557         if (!mlx5_debugfs_root)
558                 return;
559
560         if (eq->dbg)
561                 rem_res_tree(eq->dbg);
562 }
563
564 int mlx5_debug_cq_add(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq)
565 {
566         int err;
567
568         if (!mlx5_debugfs_root)
569                 return 0;
570
571         err = add_res_tree(dev, MLX5_DBG_RSC_CQ, dev->priv.cq_debugfs,
572                            &cq->dbg, cq->cqn, cq_fields,
573                            ARRAY_SIZE(cq_fields), cq);
574         if (err)
575                 cq->dbg = NULL;
576
577         return err;
578 }
579
580 void mlx5_debug_cq_remove(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq)
581 {
582         if (!mlx5_debugfs_root)
583                 return;
584
585         if (cq->dbg)
586                 rem_res_tree(cq->dbg);
587 }