net/mlx5: Replace hand written QP context struct with automatic getters
[linux-2.6-microblaze.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 }
96
97 void mlx5_unregister_debugfs(void)
98 {
99         debugfs_remove(mlx5_debugfs_root);
100 }
101
102 void mlx5_qp_debugfs_init(struct mlx5_core_dev *dev)
103 {
104         atomic_set(&dev->num_qps, 0);
105
106         dev->priv.qp_debugfs = debugfs_create_dir("QPs",  dev->priv.dbg_root);
107 }
108
109 void mlx5_qp_debugfs_cleanup(struct mlx5_core_dev *dev)
110 {
111         debugfs_remove_recursive(dev->priv.qp_debugfs);
112 }
113
114 void mlx5_eq_debugfs_init(struct mlx5_core_dev *dev)
115 {
116         dev->priv.eq_debugfs = debugfs_create_dir("EQs",  dev->priv.dbg_root);
117 }
118
119 void mlx5_eq_debugfs_cleanup(struct mlx5_core_dev *dev)
120 {
121         debugfs_remove_recursive(dev->priv.eq_debugfs);
122 }
123
124 static ssize_t average_read(struct file *filp, char __user *buf, size_t count,
125                             loff_t *pos)
126 {
127         struct mlx5_cmd_stats *stats;
128         u64 field = 0;
129         int ret;
130         char tbuf[22];
131
132         stats = filp->private_data;
133         spin_lock_irq(&stats->lock);
134         if (stats->n)
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);
139 }
140
141 static ssize_t average_write(struct file *filp, const char __user *buf,
142                              size_t count, loff_t *pos)
143 {
144         struct mlx5_cmd_stats *stats;
145
146         stats = filp->private_data;
147         spin_lock_irq(&stats->lock);
148         stats->sum = 0;
149         stats->n = 0;
150         spin_unlock_irq(&stats->lock);
151
152         *pos += count;
153
154         return count;
155 }
156
157 static const struct file_operations stats_fops = {
158         .owner  = THIS_MODULE,
159         .open   = simple_open,
160         .read   = average_read,
161         .write  = average_write,
162 };
163
164 void mlx5_cmdif_debugfs_init(struct mlx5_core_dev *dev)
165 {
166         struct mlx5_cmd_stats *stats;
167         struct dentry **cmd;
168         const char *namep;
169         int i;
170
171         cmd = &dev->priv.cmdif_debugfs;
172         *cmd = debugfs_create_dir("commands", dev->priv.dbg_root);
173
174         for (i = 0; i < ARRAY_SIZE(dev->cmd.stats); 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);
179
180                         debugfs_create_file("average", 0400, stats->root, stats,
181                                             &stats_fops);
182                         debugfs_create_u64("n", 0400, stats->root, &stats->n);
183                 }
184         }
185 }
186
187 void mlx5_cmdif_debugfs_cleanup(struct mlx5_core_dev *dev)
188 {
189         debugfs_remove_recursive(dev->priv.cmdif_debugfs);
190 }
191
192 void mlx5_cq_debugfs_init(struct mlx5_core_dev *dev)
193 {
194         dev->priv.cq_debugfs = debugfs_create_dir("CQs",  dev->priv.dbg_root);
195 }
196
197 void mlx5_cq_debugfs_cleanup(struct mlx5_core_dev *dev)
198 {
199         debugfs_remove_recursive(dev->priv.cq_debugfs);
200 }
201
202 static u64 qp_read_field(struct mlx5_core_dev *dev, struct mlx5_core_qp *qp,
203                          int index, int *is_str)
204 {
205         u32 out[MLX5_ST_SZ_BYTES(query_qp_out)] = {};
206         u32 in[MLX5_ST_SZ_DW(query_qp_in)] = {};
207         u64 param = 0;
208         int state;
209         u32 *qpc;
210         int err;
211
212         MLX5_SET(query_qp_in, in, opcode, MLX5_CMD_OP_QUERY_QP);
213         MLX5_SET(query_qp_in, in, qpn, qp->qpn);
214         err = mlx5_cmd_exec_inout(dev, query_qp, in, out);
215         if (err)
216                 return 0;
217
218         *is_str = 0;
219
220         qpc = MLX5_ADDR_OF(query_qp_out, out, qpc);
221         switch (index) {
222         case QP_PID:
223                 param = qp->pid;
224                 break;
225         case QP_STATE:
226                 state = MLX5_GET(qpc, qpc, state);
227                 param = (unsigned long)mlx5_qp_state_str(state);
228                 *is_str = 1;
229                 break;
230         case QP_XPORT:
231                 param = (unsigned long)mlx5_qp_type_str(MLX5_GET(qpc, qpc, st));
232                 *is_str = 1;
233                 break;
234         case QP_MTU:
235                 switch (MLX5_GET(qpc, qpc, mtu)) {
236                 case IB_MTU_256:
237                         param = 256;
238                         break;
239                 case IB_MTU_512:
240                         param = 512;
241                         break;
242                 case IB_MTU_1024:
243                         param = 1024;
244                         break;
245                 case IB_MTU_2048:
246                         param = 2048;
247                         break;
248                 case IB_MTU_4096:
249                         param = 4096;
250                         break;
251                 default:
252                         param = 0;
253                 }
254                 break;
255         case QP_N_RECV:
256                 param = 1 << MLX5_GET(qpc, qpc, log_rq_size);
257                 break;
258         case QP_RECV_SZ:
259                 param = 1 << (MLX5_GET(qpc, qpc, log_rq_stride) + 4);
260                 break;
261         case QP_N_SEND:
262                 if (!MLX5_GET(qpc, qpc, no_sq))
263                         param = 1 << MLX5_GET(qpc, qpc, log_sq_size);
264                 break;
265         case QP_LOG_PG_SZ:
266                 param = MLX5_GET(qpc, qpc, log_page_size) + 12;
267                 break;
268         case QP_RQPN:
269                 param = MLX5_GET(qpc, qpc, remote_qpn);
270                 break;
271         }
272
273         return param;
274 }
275
276 static int mlx5_core_eq_query(struct mlx5_core_dev *dev, struct mlx5_eq *eq,
277                               u32 *out, int outlen)
278 {
279         u32 in[MLX5_ST_SZ_DW(query_eq_in)] = {};
280
281         MLX5_SET(query_eq_in, in, opcode, MLX5_CMD_OP_QUERY_EQ);
282         MLX5_SET(query_eq_in, in, eq_number, eq->eqn);
283         return mlx5_cmd_exec(dev, in, sizeof(in), out, outlen);
284 }
285
286 static u64 eq_read_field(struct mlx5_core_dev *dev, struct mlx5_eq *eq,
287                          int index)
288 {
289         int outlen = MLX5_ST_SZ_BYTES(query_eq_out);
290         u64 param = 0;
291         void *ctx;
292         u32 *out;
293         int err;
294
295         out = kzalloc(outlen, GFP_KERNEL);
296         if (!out)
297                 return param;
298
299         err = mlx5_core_eq_query(dev, eq, out, outlen);
300         if (err) {
301                 mlx5_core_warn(dev, "failed to query eq\n");
302                 goto out;
303         }
304         ctx = MLX5_ADDR_OF(query_eq_out, out, eq_context_entry);
305
306         switch (index) {
307         case EQ_NUM_EQES:
308                 param = 1 << MLX5_GET(eqc, ctx, log_eq_size);
309                 break;
310         case EQ_INTR:
311                 param = MLX5_GET(eqc, ctx, intr);
312                 break;
313         case EQ_LOG_PG_SZ:
314                 param = MLX5_GET(eqc, ctx, log_page_size) + 12;
315                 break;
316         }
317
318 out:
319         kfree(out);
320         return param;
321 }
322
323 static u64 cq_read_field(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
324                          int index)
325 {
326         int outlen = MLX5_ST_SZ_BYTES(query_cq_out);
327         u64 param = 0;
328         void *ctx;
329         u32 *out;
330         int err;
331
332         out = kvzalloc(outlen, GFP_KERNEL);
333         if (!out)
334                 return param;
335
336         err = mlx5_core_query_cq(dev, cq, out, outlen);
337         if (err) {
338                 mlx5_core_warn(dev, "failed to query cq\n");
339                 goto out;
340         }
341         ctx = MLX5_ADDR_OF(query_cq_out, out, cq_context);
342
343         switch (index) {
344         case CQ_PID:
345                 param = cq->pid;
346                 break;
347         case CQ_NUM_CQES:
348                 param = 1 << MLX5_GET(cqc, ctx, log_cq_size);
349                 break;
350         case CQ_LOG_PG_SZ:
351                 param = MLX5_GET(cqc, ctx, log_page_size);
352                 break;
353         }
354
355 out:
356         kvfree(out);
357         return param;
358 }
359
360 static ssize_t dbg_read(struct file *filp, char __user *buf, size_t count,
361                         loff_t *pos)
362 {
363         struct mlx5_field_desc *desc;
364         struct mlx5_rsc_debug *d;
365         char tbuf[18];
366         int is_str = 0;
367         u64 field;
368         int ret;
369
370         desc = filp->private_data;
371         d = (void *)(desc - desc->i) - sizeof(*d);
372         switch (d->type) {
373         case MLX5_DBG_RSC_QP:
374                 field = qp_read_field(d->dev, d->object, desc->i, &is_str);
375                 break;
376
377         case MLX5_DBG_RSC_EQ:
378                 field = eq_read_field(d->dev, d->object, desc->i);
379                 break;
380
381         case MLX5_DBG_RSC_CQ:
382                 field = cq_read_field(d->dev, d->object, desc->i);
383                 break;
384
385         default:
386                 mlx5_core_warn(d->dev, "invalid resource type %d\n", d->type);
387                 return -EINVAL;
388         }
389
390         if (is_str)
391                 ret = snprintf(tbuf, sizeof(tbuf), "%s\n", (const char *)(unsigned long)field);
392         else
393                 ret = snprintf(tbuf, sizeof(tbuf), "0x%llx\n", field);
394
395         return simple_read_from_buffer(buf, count, pos, tbuf, ret);
396 }
397
398 static const struct file_operations fops = {
399         .owner  = THIS_MODULE,
400         .open   = simple_open,
401         .read   = dbg_read,
402 };
403
404 static int add_res_tree(struct mlx5_core_dev *dev, enum dbg_rsc_type type,
405                         struct dentry *root, struct mlx5_rsc_debug **dbg,
406                         int rsn, char **field, int nfile, void *data)
407 {
408         struct mlx5_rsc_debug *d;
409         char resn[32];
410         int i;
411
412         d = kzalloc(struct_size(d, fields, nfile), GFP_KERNEL);
413         if (!d)
414                 return -ENOMEM;
415
416         d->dev = dev;
417         d->object = data;
418         d->type = type;
419         sprintf(resn, "0x%x", rsn);
420         d->root = debugfs_create_dir(resn,  root);
421
422         for (i = 0; i < nfile; i++) {
423                 d->fields[i].i = i;
424                 debugfs_create_file(field[i], 0400, d->root, &d->fields[i],
425                                     &fops);
426         }
427         *dbg = d;
428
429         return 0;
430 }
431
432 static void rem_res_tree(struct mlx5_rsc_debug *d)
433 {
434         debugfs_remove_recursive(d->root);
435         kfree(d);
436 }
437
438 int mlx5_debug_qp_add(struct mlx5_core_dev *dev, struct mlx5_core_qp *qp)
439 {
440         int err;
441
442         if (!mlx5_debugfs_root)
443                 return 0;
444
445         err = add_res_tree(dev, MLX5_DBG_RSC_QP, dev->priv.qp_debugfs,
446                            &qp->dbg, qp->qpn, qp_fields,
447                            ARRAY_SIZE(qp_fields), qp);
448         if (err)
449                 qp->dbg = NULL;
450
451         return err;
452 }
453
454 void mlx5_debug_qp_remove(struct mlx5_core_dev *dev, struct mlx5_core_qp *qp)
455 {
456         if (!mlx5_debugfs_root)
457                 return;
458
459         if (qp->dbg)
460                 rem_res_tree(qp->dbg);
461 }
462
463 int mlx5_debug_eq_add(struct mlx5_core_dev *dev, struct mlx5_eq *eq)
464 {
465         int err;
466
467         if (!mlx5_debugfs_root)
468                 return 0;
469
470         err = add_res_tree(dev, MLX5_DBG_RSC_EQ, dev->priv.eq_debugfs,
471                            &eq->dbg, eq->eqn, eq_fields,
472                            ARRAY_SIZE(eq_fields), eq);
473         if (err)
474                 eq->dbg = NULL;
475
476         return err;
477 }
478
479 void mlx5_debug_eq_remove(struct mlx5_core_dev *dev, struct mlx5_eq *eq)
480 {
481         if (!mlx5_debugfs_root)
482                 return;
483
484         if (eq->dbg)
485                 rem_res_tree(eq->dbg);
486 }
487
488 int mlx5_debug_cq_add(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq)
489 {
490         int err;
491
492         if (!mlx5_debugfs_root)
493                 return 0;
494
495         err = add_res_tree(dev, MLX5_DBG_RSC_CQ, dev->priv.cq_debugfs,
496                            &cq->dbg, cq->cqn, cq_fields,
497                            ARRAY_SIZE(cq_fields), cq);
498         if (err)
499                 cq->dbg = NULL;
500
501         return err;
502 }
503
504 void mlx5_debug_cq_remove(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq)
505 {
506         if (!mlx5_debugfs_root)
507                 return;
508
509         if (cq->dbg)
510                 rem_res_tree(cq->dbg);
511 }