Linux-libre 4.14.82-gnu
[librecmc/linux-libre.git] / drivers / infiniband / hw / hfi1 / debugfs.c
1 /*
2  * Copyright(c) 2015-2017 Intel Corporation.
3  *
4  * This file is provided under a dual BSD/GPLv2 license.  When using or
5  * redistributing this file, you may do so under either license.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of version 2 of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * BSD LICENSE
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  *
24  *  - Redistributions of source code must retain the above copyright
25  *    notice, this list of conditions and the following disclaimer.
26  *  - Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in
28  *    the documentation and/or other materials provided with the
29  *    distribution.
30  *  - Neither the name of Intel Corporation nor the names of its
31  *    contributors may be used to endorse or promote products derived
32  *    from this software without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  */
47 #include <linux/debugfs.h>
48 #include <linux/seq_file.h>
49 #include <linux/kernel.h>
50 #include <linux/export.h>
51 #include <linux/module.h>
52 #include <linux/string.h>
53 #include <linux/types.h>
54 #include <linux/ratelimit.h>
55 #include <linux/fault-inject.h>
56
57 #include "hfi.h"
58 #include "trace.h"
59 #include "debugfs.h"
60 #include "device.h"
61 #include "qp.h"
62 #include "sdma.h"
63
64 static struct dentry *hfi1_dbg_root;
65
66 /* wrappers to enforce srcu in seq file */
67 static ssize_t hfi1_seq_read(
68         struct file *file,
69         char __user *buf,
70         size_t size,
71         loff_t *ppos)
72 {
73         struct dentry *d = file->f_path.dentry;
74         int srcu_idx;
75         ssize_t r;
76
77         r = debugfs_use_file_start(d, &srcu_idx);
78         if (likely(!r))
79                 r = seq_read(file, buf, size, ppos);
80         debugfs_use_file_finish(srcu_idx);
81         return r;
82 }
83
84 static loff_t hfi1_seq_lseek(
85         struct file *file,
86         loff_t offset,
87         int whence)
88 {
89         struct dentry *d = file->f_path.dentry;
90         int srcu_idx;
91         loff_t r;
92
93         r = debugfs_use_file_start(d, &srcu_idx);
94         if (likely(!r))
95                 r = seq_lseek(file, offset, whence);
96         debugfs_use_file_finish(srcu_idx);
97         return r;
98 }
99
100 #define private2dd(file) (file_inode(file)->i_private)
101 #define private2ppd(file) (file_inode(file)->i_private)
102
103 #define DEBUGFS_SEQ_FILE_OPS(name) \
104 static const struct seq_operations _##name##_seq_ops = { \
105         .start = _##name##_seq_start, \
106         .next  = _##name##_seq_next, \
107         .stop  = _##name##_seq_stop, \
108         .show  = _##name##_seq_show \
109 }
110
111 #define DEBUGFS_SEQ_FILE_OPEN(name) \
112 static int _##name##_open(struct inode *inode, struct file *s) \
113 { \
114         struct seq_file *seq; \
115         int ret; \
116         ret =  seq_open(s, &_##name##_seq_ops); \
117         if (ret) \
118                 return ret; \
119         seq = s->private_data; \
120         seq->private = inode->i_private; \
121         return 0; \
122 }
123
124 #define DEBUGFS_FILE_OPS(name) \
125 static const struct file_operations _##name##_file_ops = { \
126         .owner   = THIS_MODULE, \
127         .open    = _##name##_open, \
128         .read    = hfi1_seq_read, \
129         .llseek  = hfi1_seq_lseek, \
130         .release = seq_release \
131 }
132
133 #define DEBUGFS_FILE_CREATE(name, parent, data, ops, mode)      \
134 do { \
135         struct dentry *ent; \
136         ent = debugfs_create_file(name, mode, parent, \
137                 data, ops); \
138         if (!ent) \
139                 pr_warn("create of %s failed\n", name); \
140 } while (0)
141
142 #define DEBUGFS_SEQ_FILE_CREATE(name, parent, data) \
143         DEBUGFS_FILE_CREATE(#name, parent, data, &_##name##_file_ops, S_IRUGO)
144
145 static void *_opcode_stats_seq_start(struct seq_file *s, loff_t *pos)
146 {
147         struct hfi1_opcode_stats_perctx *opstats;
148
149         if (*pos >= ARRAY_SIZE(opstats->stats))
150                 return NULL;
151         return pos;
152 }
153
154 static void *_opcode_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
155 {
156         struct hfi1_opcode_stats_perctx *opstats;
157
158         ++*pos;
159         if (*pos >= ARRAY_SIZE(opstats->stats))
160                 return NULL;
161         return pos;
162 }
163
164 static void _opcode_stats_seq_stop(struct seq_file *s, void *v)
165 {
166 }
167
168 static int _opcode_stats_seq_show(struct seq_file *s, void *v)
169 {
170         loff_t *spos = v;
171         loff_t i = *spos, j;
172         u64 n_packets = 0, n_bytes = 0;
173         struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
174         struct hfi1_devdata *dd = dd_from_dev(ibd);
175         struct hfi1_ctxtdata *rcd;
176
177         for (j = 0; j < dd->first_dyn_alloc_ctxt; j++) {
178                 rcd = hfi1_rcd_get_by_index(dd, j);
179                 if (rcd) {
180                         n_packets += rcd->opstats->stats[i].n_packets;
181                         n_bytes += rcd->opstats->stats[i].n_bytes;
182                 }
183                 hfi1_rcd_put(rcd);
184         }
185         if (!n_packets && !n_bytes)
186                 return SEQ_SKIP;
187         seq_printf(s, "%02llx %llu/%llu\n", i,
188                    (unsigned long long)n_packets,
189                    (unsigned long long)n_bytes);
190
191         return 0;
192 }
193
194 DEBUGFS_SEQ_FILE_OPS(opcode_stats);
195 DEBUGFS_SEQ_FILE_OPEN(opcode_stats)
196 DEBUGFS_FILE_OPS(opcode_stats);
197
198 static void *_ctx_stats_seq_start(struct seq_file *s, loff_t *pos)
199 {
200         struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
201         struct hfi1_devdata *dd = dd_from_dev(ibd);
202
203         if (!*pos)
204                 return SEQ_START_TOKEN;
205         if (*pos >= dd->first_dyn_alloc_ctxt)
206                 return NULL;
207         return pos;
208 }
209
210 static void *_ctx_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
211 {
212         struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
213         struct hfi1_devdata *dd = dd_from_dev(ibd);
214
215         if (v == SEQ_START_TOKEN)
216                 return pos;
217
218         ++*pos;
219         if (*pos >= dd->first_dyn_alloc_ctxt)
220                 return NULL;
221         return pos;
222 }
223
224 static void _ctx_stats_seq_stop(struct seq_file *s, void *v)
225 {
226         /* nothing allocated */
227 }
228
229 static int _ctx_stats_seq_show(struct seq_file *s, void *v)
230 {
231         loff_t *spos;
232         loff_t i, j;
233         u64 n_packets = 0;
234         struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
235         struct hfi1_devdata *dd = dd_from_dev(ibd);
236         struct hfi1_ctxtdata *rcd;
237
238         if (v == SEQ_START_TOKEN) {
239                 seq_puts(s, "Ctx:npkts\n");
240                 return 0;
241         }
242
243         spos = v;
244         i = *spos;
245
246         rcd = hfi1_rcd_get_by_index(dd, i);
247         if (!rcd)
248                 return SEQ_SKIP;
249
250         for (j = 0; j < ARRAY_SIZE(rcd->opstats->stats); j++)
251                 n_packets += rcd->opstats->stats[j].n_packets;
252
253         hfi1_rcd_put(rcd);
254
255         if (!n_packets)
256                 return SEQ_SKIP;
257
258         seq_printf(s, "  %llu:%llu\n", i, n_packets);
259         return 0;
260 }
261
262 DEBUGFS_SEQ_FILE_OPS(ctx_stats);
263 DEBUGFS_SEQ_FILE_OPEN(ctx_stats)
264 DEBUGFS_FILE_OPS(ctx_stats);
265
266 static void *_qp_stats_seq_start(struct seq_file *s, loff_t *pos)
267         __acquires(RCU)
268 {
269         struct rvt_qp_iter *iter;
270         loff_t n = *pos;
271
272         iter = rvt_qp_iter_init(s->private, 0, NULL);
273
274         /* stop calls rcu_read_unlock */
275         rcu_read_lock();
276
277         if (!iter)
278                 return NULL;
279
280         do {
281                 if (rvt_qp_iter_next(iter)) {
282                         kfree(iter);
283                         return NULL;
284                 }
285         } while (n--);
286
287         return iter;
288 }
289
290 static void *_qp_stats_seq_next(struct seq_file *s, void *iter_ptr,
291                                 loff_t *pos)
292         __must_hold(RCU)
293 {
294         struct rvt_qp_iter *iter = iter_ptr;
295
296         (*pos)++;
297
298         if (rvt_qp_iter_next(iter)) {
299                 kfree(iter);
300                 return NULL;
301         }
302
303         return iter;
304 }
305
306 static void _qp_stats_seq_stop(struct seq_file *s, void *iter_ptr)
307         __releases(RCU)
308 {
309         rcu_read_unlock();
310 }
311
312 static int _qp_stats_seq_show(struct seq_file *s, void *iter_ptr)
313 {
314         struct rvt_qp_iter *iter = iter_ptr;
315
316         if (!iter)
317                 return 0;
318
319         qp_iter_print(s, iter);
320
321         return 0;
322 }
323
324 DEBUGFS_SEQ_FILE_OPS(qp_stats);
325 DEBUGFS_SEQ_FILE_OPEN(qp_stats)
326 DEBUGFS_FILE_OPS(qp_stats);
327
328 static void *_sdes_seq_start(struct seq_file *s, loff_t *pos)
329 {
330         struct hfi1_ibdev *ibd;
331         struct hfi1_devdata *dd;
332
333         ibd = (struct hfi1_ibdev *)s->private;
334         dd = dd_from_dev(ibd);
335         if (!dd->per_sdma || *pos >= dd->num_sdma)
336                 return NULL;
337         return pos;
338 }
339
340 static void *_sdes_seq_next(struct seq_file *s, void *v, loff_t *pos)
341 {
342         struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
343         struct hfi1_devdata *dd = dd_from_dev(ibd);
344
345         ++*pos;
346         if (!dd->per_sdma || *pos >= dd->num_sdma)
347                 return NULL;
348         return pos;
349 }
350
351 static void _sdes_seq_stop(struct seq_file *s, void *v)
352 {
353 }
354
355 static int _sdes_seq_show(struct seq_file *s, void *v)
356 {
357         struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
358         struct hfi1_devdata *dd = dd_from_dev(ibd);
359         loff_t *spos = v;
360         loff_t i = *spos;
361
362         sdma_seqfile_dump_sde(s, &dd->per_sdma[i]);
363         return 0;
364 }
365
366 DEBUGFS_SEQ_FILE_OPS(sdes);
367 DEBUGFS_SEQ_FILE_OPEN(sdes)
368 DEBUGFS_FILE_OPS(sdes);
369
370 static void *_rcds_seq_start(struct seq_file *s, loff_t *pos)
371 {
372         struct hfi1_ibdev *ibd;
373         struct hfi1_devdata *dd;
374
375         ibd = (struct hfi1_ibdev *)s->private;
376         dd = dd_from_dev(ibd);
377         if (!dd->rcd || *pos >= dd->n_krcv_queues)
378                 return NULL;
379         return pos;
380 }
381
382 static void *_rcds_seq_next(struct seq_file *s, void *v, loff_t *pos)
383 {
384         struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
385         struct hfi1_devdata *dd = dd_from_dev(ibd);
386
387         ++*pos;
388         if (!dd->rcd || *pos >= dd->n_krcv_queues)
389                 return NULL;
390         return pos;
391 }
392
393 static void _rcds_seq_stop(struct seq_file *s, void *v)
394 {
395 }
396
397 static int _rcds_seq_show(struct seq_file *s, void *v)
398 {
399         struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
400         struct hfi1_devdata *dd = dd_from_dev(ibd);
401         struct hfi1_ctxtdata *rcd;
402         loff_t *spos = v;
403         loff_t i = *spos;
404
405         rcd = hfi1_rcd_get_by_index(dd, i);
406         if (rcd)
407                 seqfile_dump_rcd(s, rcd);
408         hfi1_rcd_put(rcd);
409         return 0;
410 }
411
412 DEBUGFS_SEQ_FILE_OPS(rcds);
413 DEBUGFS_SEQ_FILE_OPEN(rcds)
414 DEBUGFS_FILE_OPS(rcds);
415
416 /* read the per-device counters */
417 static ssize_t dev_counters_read(struct file *file, char __user *buf,
418                                  size_t count, loff_t *ppos)
419 {
420         u64 *counters;
421         size_t avail;
422         struct hfi1_devdata *dd;
423         ssize_t rval;
424
425         dd = private2dd(file);
426         avail = hfi1_read_cntrs(dd, NULL, &counters);
427         rval =  simple_read_from_buffer(buf, count, ppos, counters, avail);
428         return rval;
429 }
430
431 /* read the per-device counters */
432 static ssize_t dev_names_read(struct file *file, char __user *buf,
433                               size_t count, loff_t *ppos)
434 {
435         char *names;
436         size_t avail;
437         struct hfi1_devdata *dd;
438         ssize_t rval;
439
440         dd = private2dd(file);
441         avail = hfi1_read_cntrs(dd, &names, NULL);
442         rval =  simple_read_from_buffer(buf, count, ppos, names, avail);
443         return rval;
444 }
445
446 struct counter_info {
447         char *name;
448         const struct file_operations ops;
449 };
450
451 /*
452  * Could use file_inode(file)->i_ino to figure out which file,
453  * instead of separate routine for each, but for now, this works...
454  */
455
456 /* read the per-port names (same for each port) */
457 static ssize_t portnames_read(struct file *file, char __user *buf,
458                               size_t count, loff_t *ppos)
459 {
460         char *names;
461         size_t avail;
462         struct hfi1_devdata *dd;
463         ssize_t rval;
464
465         dd = private2dd(file);
466         avail = hfi1_read_portcntrs(dd->pport, &names, NULL);
467         rval = simple_read_from_buffer(buf, count, ppos, names, avail);
468         return rval;
469 }
470
471 /* read the per-port counters */
472 static ssize_t portcntrs_debugfs_read(struct file *file, char __user *buf,
473                                       size_t count, loff_t *ppos)
474 {
475         u64 *counters;
476         size_t avail;
477         struct hfi1_pportdata *ppd;
478         ssize_t rval;
479
480         ppd = private2ppd(file);
481         avail = hfi1_read_portcntrs(ppd, NULL, &counters);
482         rval = simple_read_from_buffer(buf, count, ppos, counters, avail);
483         return rval;
484 }
485
486 static void check_dyn_flag(u64 scratch0, char *p, int size, int *used,
487                            int this_hfi, int hfi, u32 flag, const char *what)
488 {
489         u32 mask;
490
491         mask = flag << (hfi ? CR_DYN_SHIFT : 0);
492         if (scratch0 & mask) {
493                 *used += scnprintf(p + *used, size - *used,
494                                    "  0x%08x - HFI%d %s in use, %s device\n",
495                                    mask, hfi, what,
496                                    this_hfi == hfi ? "this" : "other");
497         }
498 }
499
500 static ssize_t asic_flags_read(struct file *file, char __user *buf,
501                                size_t count, loff_t *ppos)
502 {
503         struct hfi1_pportdata *ppd;
504         struct hfi1_devdata *dd;
505         u64 scratch0;
506         char *tmp;
507         int ret = 0;
508         int size;
509         int used;
510         int i;
511
512         ppd = private2ppd(file);
513         dd = ppd->dd;
514         size = PAGE_SIZE;
515         used = 0;
516         tmp = kmalloc(size, GFP_KERNEL);
517         if (!tmp)
518                 return -ENOMEM;
519
520         scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
521         used += scnprintf(tmp + used, size - used,
522                           "Resource flags: 0x%016llx\n", scratch0);
523
524         /* check permanent flag */
525         if (scratch0 & CR_THERM_INIT) {
526                 used += scnprintf(tmp + used, size - used,
527                                   "  0x%08x - thermal monitoring initialized\n",
528                                   (u32)CR_THERM_INIT);
529         }
530
531         /* check each dynamic flag on each HFI */
532         for (i = 0; i < 2; i++) {
533                 check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
534                                CR_SBUS, "SBus");
535                 check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
536                                CR_EPROM, "EPROM");
537                 check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
538                                CR_I2C1, "i2c chain 1");
539                 check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
540                                CR_I2C2, "i2c chain 2");
541         }
542         used += scnprintf(tmp + used, size - used, "Write bits to clear\n");
543
544         ret = simple_read_from_buffer(buf, count, ppos, tmp, used);
545         kfree(tmp);
546         return ret;
547 }
548
549 static ssize_t asic_flags_write(struct file *file, const char __user *buf,
550                                 size_t count, loff_t *ppos)
551 {
552         struct hfi1_pportdata *ppd;
553         struct hfi1_devdata *dd;
554         char *buff;
555         int ret;
556         unsigned long long value;
557         u64 scratch0;
558         u64 clear;
559
560         ppd = private2ppd(file);
561         dd = ppd->dd;
562
563         /* zero terminate and read the expected integer */
564         buff = memdup_user_nul(buf, count);
565         if (IS_ERR(buff))
566                 return PTR_ERR(buff);
567
568         ret = kstrtoull(buff, 0, &value);
569         if (ret)
570                 goto do_free;
571         clear = value;
572
573         /* obtain exclusive access */
574         mutex_lock(&dd->asic_data->asic_resource_mutex);
575         acquire_hw_mutex(dd);
576
577         scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
578         scratch0 &= ~clear;
579         write_csr(dd, ASIC_CFG_SCRATCH, scratch0);
580         /* force write to be visible to other HFI on another OS */
581         (void)read_csr(dd, ASIC_CFG_SCRATCH);
582
583         release_hw_mutex(dd);
584         mutex_unlock(&dd->asic_data->asic_resource_mutex);
585
586         /* return the number of bytes written */
587         ret = count;
588
589  do_free:
590         kfree(buff);
591         return ret;
592 }
593
594 /* read the dc8051 memory */
595 static ssize_t dc8051_memory_read(struct file *file, char __user *buf,
596                                   size_t count, loff_t *ppos)
597 {
598         struct hfi1_pportdata *ppd = private2ppd(file);
599         ssize_t rval;
600         void *tmp;
601         loff_t start, end;
602
603         /* the checks below expect the position to be positive */
604         if (*ppos < 0)
605                 return -EINVAL;
606
607         tmp = kzalloc(DC8051_DATA_MEM_SIZE, GFP_KERNEL);
608         if (!tmp)
609                 return -ENOMEM;
610
611         /*
612          * Fill in the requested portion of the temporary buffer from the
613          * 8051 memory.  The 8051 memory read is done in terms of 8 bytes.
614          * Adjust start and end to fit.  Skip reading anything if out of
615          * range.
616          */
617         start = *ppos & ~0x7;   /* round down */
618         if (start < DC8051_DATA_MEM_SIZE) {
619                 end = (*ppos + count + 7) & ~0x7; /* round up */
620                 if (end > DC8051_DATA_MEM_SIZE)
621                         end = DC8051_DATA_MEM_SIZE;
622                 rval = read_8051_data(ppd->dd, start, end - start,
623                                       (u64 *)(tmp + start));
624                 if (rval)
625                         goto done;
626         }
627
628         rval = simple_read_from_buffer(buf, count, ppos, tmp,
629                                        DC8051_DATA_MEM_SIZE);
630 done:
631         kfree(tmp);
632         return rval;
633 }
634
635 static ssize_t debugfs_lcb_read(struct file *file, char __user *buf,
636                                 size_t count, loff_t *ppos)
637 {
638         struct hfi1_pportdata *ppd = private2ppd(file);
639         struct hfi1_devdata *dd = ppd->dd;
640         unsigned long total, csr_off;
641         u64 data;
642
643         if (*ppos < 0)
644                 return -EINVAL;
645         /* only read 8 byte quantities */
646         if ((count % 8) != 0)
647                 return -EINVAL;
648         /* offset must be 8-byte aligned */
649         if ((*ppos % 8) != 0)
650                 return -EINVAL;
651         /* do nothing if out of range or zero count */
652         if (*ppos >= (LCB_END - LCB_START) || !count)
653                 return 0;
654         /* reduce count if needed */
655         if (*ppos + count > LCB_END - LCB_START)
656                 count = (LCB_END - LCB_START) - *ppos;
657
658         csr_off = LCB_START + *ppos;
659         for (total = 0; total < count; total += 8, csr_off += 8) {
660                 if (read_lcb_csr(dd, csr_off, (u64 *)&data))
661                         break; /* failed */
662                 if (put_user(data, (unsigned long __user *)(buf + total)))
663                         break;
664         }
665         *ppos += total;
666         return total;
667 }
668
669 static ssize_t debugfs_lcb_write(struct file *file, const char __user *buf,
670                                  size_t count, loff_t *ppos)
671 {
672         struct hfi1_pportdata *ppd = private2ppd(file);
673         struct hfi1_devdata *dd = ppd->dd;
674         unsigned long total, csr_off, data;
675
676         if (*ppos < 0)
677                 return -EINVAL;
678         /* only write 8 byte quantities */
679         if ((count % 8) != 0)
680                 return -EINVAL;
681         /* offset must be 8-byte aligned */
682         if ((*ppos % 8) != 0)
683                 return -EINVAL;
684         /* do nothing if out of range or zero count */
685         if (*ppos >= (LCB_END - LCB_START) || !count)
686                 return 0;
687         /* reduce count if needed */
688         if (*ppos + count > LCB_END - LCB_START)
689                 count = (LCB_END - LCB_START) - *ppos;
690
691         csr_off = LCB_START + *ppos;
692         for (total = 0; total < count; total += 8, csr_off += 8) {
693                 if (get_user(data, (unsigned long __user *)(buf + total)))
694                         break;
695                 if (write_lcb_csr(dd, csr_off, data))
696                         break; /* failed */
697         }
698         *ppos += total;
699         return total;
700 }
701
702 /*
703  * read the per-port QSFP data for ppd
704  */
705 static ssize_t qsfp_debugfs_dump(struct file *file, char __user *buf,
706                                  size_t count, loff_t *ppos)
707 {
708         struct hfi1_pportdata *ppd;
709         char *tmp;
710         int ret;
711
712         ppd = private2ppd(file);
713         tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
714         if (!tmp)
715                 return -ENOMEM;
716
717         ret = qsfp_dump(ppd, tmp, PAGE_SIZE);
718         if (ret > 0)
719                 ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
720         kfree(tmp);
721         return ret;
722 }
723
724 /* Do an i2c write operation on the chain for the given HFI. */
725 static ssize_t __i2c_debugfs_write(struct file *file, const char __user *buf,
726                                    size_t count, loff_t *ppos, u32 target)
727 {
728         struct hfi1_pportdata *ppd;
729         char *buff;
730         int ret;
731         int i2c_addr;
732         int offset;
733         int total_written;
734
735         ppd = private2ppd(file);
736
737         /* byte offset format: [offsetSize][i2cAddr][offsetHigh][offsetLow] */
738         i2c_addr = (*ppos >> 16) & 0xffff;
739         offset = *ppos & 0xffff;
740
741         /* explicitly reject invalid address 0 to catch cp and cat */
742         if (i2c_addr == 0)
743                 return -EINVAL;
744
745         buff = memdup_user(buf, count);
746         if (IS_ERR(buff))
747                 return PTR_ERR(buff);
748
749         total_written = i2c_write(ppd, target, i2c_addr, offset, buff, count);
750         if (total_written < 0) {
751                 ret = total_written;
752                 goto _free;
753         }
754
755         *ppos += total_written;
756
757         ret = total_written;
758
759  _free:
760         kfree(buff);
761         return ret;
762 }
763
764 /* Do an i2c write operation on chain for HFI 0. */
765 static ssize_t i2c1_debugfs_write(struct file *file, const char __user *buf,
766                                   size_t count, loff_t *ppos)
767 {
768         return __i2c_debugfs_write(file, buf, count, ppos, 0);
769 }
770
771 /* Do an i2c write operation on chain for HFI 1. */
772 static ssize_t i2c2_debugfs_write(struct file *file, const char __user *buf,
773                                   size_t count, loff_t *ppos)
774 {
775         return __i2c_debugfs_write(file, buf, count, ppos, 1);
776 }
777
778 /* Do an i2c read operation on the chain for the given HFI. */
779 static ssize_t __i2c_debugfs_read(struct file *file, char __user *buf,
780                                   size_t count, loff_t *ppos, u32 target)
781 {
782         struct hfi1_pportdata *ppd;
783         char *buff;
784         int ret;
785         int i2c_addr;
786         int offset;
787         int total_read;
788
789         ppd = private2ppd(file);
790
791         /* byte offset format: [offsetSize][i2cAddr][offsetHigh][offsetLow] */
792         i2c_addr = (*ppos >> 16) & 0xffff;
793         offset = *ppos & 0xffff;
794
795         /* explicitly reject invalid address 0 to catch cp and cat */
796         if (i2c_addr == 0)
797                 return -EINVAL;
798
799         buff = kmalloc(count, GFP_KERNEL);
800         if (!buff)
801                 return -ENOMEM;
802
803         total_read = i2c_read(ppd, target, i2c_addr, offset, buff, count);
804         if (total_read < 0) {
805                 ret = total_read;
806                 goto _free;
807         }
808
809         *ppos += total_read;
810
811         ret = copy_to_user(buf, buff, total_read);
812         if (ret > 0) {
813                 ret = -EFAULT;
814                 goto _free;
815         }
816
817         ret = total_read;
818
819  _free:
820         kfree(buff);
821         return ret;
822 }
823
824 /* Do an i2c read operation on chain for HFI 0. */
825 static ssize_t i2c1_debugfs_read(struct file *file, char __user *buf,
826                                  size_t count, loff_t *ppos)
827 {
828         return __i2c_debugfs_read(file, buf, count, ppos, 0);
829 }
830
831 /* Do an i2c read operation on chain for HFI 1. */
832 static ssize_t i2c2_debugfs_read(struct file *file, char __user *buf,
833                                  size_t count, loff_t *ppos)
834 {
835         return __i2c_debugfs_read(file, buf, count, ppos, 1);
836 }
837
838 /* Do a QSFP write operation on the i2c chain for the given HFI. */
839 static ssize_t __qsfp_debugfs_write(struct file *file, const char __user *buf,
840                                     size_t count, loff_t *ppos, u32 target)
841 {
842         struct hfi1_pportdata *ppd;
843         char *buff;
844         int ret;
845         int total_written;
846
847         if (*ppos + count > QSFP_PAGESIZE * 4) /* base page + page00-page03 */
848                 return -EINVAL;
849
850         ppd = private2ppd(file);
851
852         buff = memdup_user(buf, count);
853         if (IS_ERR(buff))
854                 return PTR_ERR(buff);
855
856         total_written = qsfp_write(ppd, target, *ppos, buff, count);
857         if (total_written < 0) {
858                 ret = total_written;
859                 goto _free;
860         }
861
862         *ppos += total_written;
863
864         ret = total_written;
865
866  _free:
867         kfree(buff);
868         return ret;
869 }
870
871 /* Do a QSFP write operation on i2c chain for HFI 0. */
872 static ssize_t qsfp1_debugfs_write(struct file *file, const char __user *buf,
873                                    size_t count, loff_t *ppos)
874 {
875         return __qsfp_debugfs_write(file, buf, count, ppos, 0);
876 }
877
878 /* Do a QSFP write operation on i2c chain for HFI 1. */
879 static ssize_t qsfp2_debugfs_write(struct file *file, const char __user *buf,
880                                    size_t count, loff_t *ppos)
881 {
882         return __qsfp_debugfs_write(file, buf, count, ppos, 1);
883 }
884
885 /* Do a QSFP read operation on the i2c chain for the given HFI. */
886 static ssize_t __qsfp_debugfs_read(struct file *file, char __user *buf,
887                                    size_t count, loff_t *ppos, u32 target)
888 {
889         struct hfi1_pportdata *ppd;
890         char *buff;
891         int ret;
892         int total_read;
893
894         if (*ppos + count > QSFP_PAGESIZE * 4) { /* base page + page00-page03 */
895                 ret = -EINVAL;
896                 goto _return;
897         }
898
899         ppd = private2ppd(file);
900
901         buff = kmalloc(count, GFP_KERNEL);
902         if (!buff) {
903                 ret = -ENOMEM;
904                 goto _return;
905         }
906
907         total_read = qsfp_read(ppd, target, *ppos, buff, count);
908         if (total_read < 0) {
909                 ret = total_read;
910                 goto _free;
911         }
912
913         *ppos += total_read;
914
915         ret = copy_to_user(buf, buff, total_read);
916         if (ret > 0) {
917                 ret = -EFAULT;
918                 goto _free;
919         }
920
921         ret = total_read;
922
923  _free:
924         kfree(buff);
925  _return:
926         return ret;
927 }
928
929 /* Do a QSFP read operation on i2c chain for HFI 0. */
930 static ssize_t qsfp1_debugfs_read(struct file *file, char __user *buf,
931                                   size_t count, loff_t *ppos)
932 {
933         return __qsfp_debugfs_read(file, buf, count, ppos, 0);
934 }
935
936 /* Do a QSFP read operation on i2c chain for HFI 1. */
937 static ssize_t qsfp2_debugfs_read(struct file *file, char __user *buf,
938                                   size_t count, loff_t *ppos)
939 {
940         return __qsfp_debugfs_read(file, buf, count, ppos, 1);
941 }
942
943 static int __i2c_debugfs_open(struct inode *in, struct file *fp, u32 target)
944 {
945         struct hfi1_pportdata *ppd;
946         int ret;
947
948         if (!try_module_get(THIS_MODULE))
949                 return -ENODEV;
950
951         ppd = private2ppd(fp);
952
953         ret = acquire_chip_resource(ppd->dd, i2c_target(target), 0);
954         if (ret) /* failed - release the module */
955                 module_put(THIS_MODULE);
956
957         return ret;
958 }
959
960 static int i2c1_debugfs_open(struct inode *in, struct file *fp)
961 {
962         return __i2c_debugfs_open(in, fp, 0);
963 }
964
965 static int i2c2_debugfs_open(struct inode *in, struct file *fp)
966 {
967         return __i2c_debugfs_open(in, fp, 1);
968 }
969
970 static int __i2c_debugfs_release(struct inode *in, struct file *fp, u32 target)
971 {
972         struct hfi1_pportdata *ppd;
973
974         ppd = private2ppd(fp);
975
976         release_chip_resource(ppd->dd, i2c_target(target));
977         module_put(THIS_MODULE);
978
979         return 0;
980 }
981
982 static int i2c1_debugfs_release(struct inode *in, struct file *fp)
983 {
984         return __i2c_debugfs_release(in, fp, 0);
985 }
986
987 static int i2c2_debugfs_release(struct inode *in, struct file *fp)
988 {
989         return __i2c_debugfs_release(in, fp, 1);
990 }
991
992 static int __qsfp_debugfs_open(struct inode *in, struct file *fp, u32 target)
993 {
994         struct hfi1_pportdata *ppd;
995         int ret;
996
997         if (!try_module_get(THIS_MODULE))
998                 return -ENODEV;
999
1000         ppd = private2ppd(fp);
1001
1002         ret = acquire_chip_resource(ppd->dd, i2c_target(target), 0);
1003         if (ret) /* failed - release the module */
1004                 module_put(THIS_MODULE);
1005
1006         return ret;
1007 }
1008
1009 static int qsfp1_debugfs_open(struct inode *in, struct file *fp)
1010 {
1011         return __qsfp_debugfs_open(in, fp, 0);
1012 }
1013
1014 static int qsfp2_debugfs_open(struct inode *in, struct file *fp)
1015 {
1016         return __qsfp_debugfs_open(in, fp, 1);
1017 }
1018
1019 static int __qsfp_debugfs_release(struct inode *in, struct file *fp, u32 target)
1020 {
1021         struct hfi1_pportdata *ppd;
1022
1023         ppd = private2ppd(fp);
1024
1025         release_chip_resource(ppd->dd, i2c_target(target));
1026         module_put(THIS_MODULE);
1027
1028         return 0;
1029 }
1030
1031 static int qsfp1_debugfs_release(struct inode *in, struct file *fp)
1032 {
1033         return __qsfp_debugfs_release(in, fp, 0);
1034 }
1035
1036 static int qsfp2_debugfs_release(struct inode *in, struct file *fp)
1037 {
1038         return __qsfp_debugfs_release(in, fp, 1);
1039 }
1040
1041 #define DEBUGFS_OPS(nm, readroutine, writeroutine)      \
1042 { \
1043         .name = nm, \
1044         .ops = { \
1045                 .read = readroutine, \
1046                 .write = writeroutine, \
1047                 .llseek = generic_file_llseek, \
1048         }, \
1049 }
1050
1051 #define DEBUGFS_XOPS(nm, readf, writef, openf, releasef) \
1052 { \
1053         .name = nm, \
1054         .ops = { \
1055                 .read = readf, \
1056                 .write = writef, \
1057                 .llseek = generic_file_llseek, \
1058                 .open = openf, \
1059                 .release = releasef \
1060         }, \
1061 }
1062
1063 static const struct counter_info cntr_ops[] = {
1064         DEBUGFS_OPS("counter_names", dev_names_read, NULL),
1065         DEBUGFS_OPS("counters", dev_counters_read, NULL),
1066         DEBUGFS_OPS("portcounter_names", portnames_read, NULL),
1067 };
1068
1069 static const struct counter_info port_cntr_ops[] = {
1070         DEBUGFS_OPS("port%dcounters", portcntrs_debugfs_read, NULL),
1071         DEBUGFS_XOPS("i2c1", i2c1_debugfs_read, i2c1_debugfs_write,
1072                      i2c1_debugfs_open, i2c1_debugfs_release),
1073         DEBUGFS_XOPS("i2c2", i2c2_debugfs_read, i2c2_debugfs_write,
1074                      i2c2_debugfs_open, i2c2_debugfs_release),
1075         DEBUGFS_OPS("qsfp_dump%d", qsfp_debugfs_dump, NULL),
1076         DEBUGFS_XOPS("qsfp1", qsfp1_debugfs_read, qsfp1_debugfs_write,
1077                      qsfp1_debugfs_open, qsfp1_debugfs_release),
1078         DEBUGFS_XOPS("qsfp2", qsfp2_debugfs_read, qsfp2_debugfs_write,
1079                      qsfp2_debugfs_open, qsfp2_debugfs_release),
1080         DEBUGFS_OPS("asic_flags", asic_flags_read, asic_flags_write),
1081         DEBUGFS_OPS("dc8051_memory", dc8051_memory_read, NULL),
1082         DEBUGFS_OPS("lcb", debugfs_lcb_read, debugfs_lcb_write),
1083 };
1084
1085 static void *_sdma_cpu_list_seq_start(struct seq_file *s, loff_t *pos)
1086 {
1087         if (*pos >= num_online_cpus())
1088                 return NULL;
1089
1090         return pos;
1091 }
1092
1093 static void *_sdma_cpu_list_seq_next(struct seq_file *s, void *v, loff_t *pos)
1094 {
1095         ++*pos;
1096         if (*pos >= num_online_cpus())
1097                 return NULL;
1098
1099         return pos;
1100 }
1101
1102 static void _sdma_cpu_list_seq_stop(struct seq_file *s, void *v)
1103 {
1104         /* nothing allocated */
1105 }
1106
1107 static int _sdma_cpu_list_seq_show(struct seq_file *s, void *v)
1108 {
1109         struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
1110         struct hfi1_devdata *dd = dd_from_dev(ibd);
1111         loff_t *spos = v;
1112         loff_t i = *spos;
1113
1114         sdma_seqfile_dump_cpu_list(s, dd, (unsigned long)i);
1115         return 0;
1116 }
1117
1118 DEBUGFS_SEQ_FILE_OPS(sdma_cpu_list);
1119 DEBUGFS_SEQ_FILE_OPEN(sdma_cpu_list)
1120 DEBUGFS_FILE_OPS(sdma_cpu_list);
1121
1122 #ifdef CONFIG_FAULT_INJECTION
1123 static void *_fault_stats_seq_start(struct seq_file *s, loff_t *pos)
1124 {
1125         struct hfi1_opcode_stats_perctx *opstats;
1126
1127         if (*pos >= ARRAY_SIZE(opstats->stats))
1128                 return NULL;
1129         return pos;
1130 }
1131
1132 static void *_fault_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
1133 {
1134         struct hfi1_opcode_stats_perctx *opstats;
1135
1136         ++*pos;
1137         if (*pos >= ARRAY_SIZE(opstats->stats))
1138                 return NULL;
1139         return pos;
1140 }
1141
1142 static void _fault_stats_seq_stop(struct seq_file *s, void *v)
1143 {
1144 }
1145
1146 static int _fault_stats_seq_show(struct seq_file *s, void *v)
1147 {
1148         loff_t *spos = v;
1149         loff_t i = *spos, j;
1150         u64 n_packets = 0, n_bytes = 0;
1151         struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
1152         struct hfi1_devdata *dd = dd_from_dev(ibd);
1153         struct hfi1_ctxtdata *rcd;
1154
1155         for (j = 0; j < dd->first_dyn_alloc_ctxt; j++) {
1156                 rcd = hfi1_rcd_get_by_index(dd, j);
1157                 if (rcd) {
1158                         n_packets += rcd->opstats->stats[i].n_packets;
1159                         n_bytes += rcd->opstats->stats[i].n_bytes;
1160                 }
1161                 hfi1_rcd_put(rcd);
1162         }
1163         if (!n_packets && !n_bytes)
1164                 return SEQ_SKIP;
1165         if (!ibd->fault_opcode->n_rxfaults[i] &&
1166             !ibd->fault_opcode->n_txfaults[i])
1167                 return SEQ_SKIP;
1168         seq_printf(s, "%02llx %llu/%llu (faults rx:%llu faults: tx:%llu)\n", i,
1169                    (unsigned long long)n_packets,
1170                    (unsigned long long)n_bytes,
1171                    (unsigned long long)ibd->fault_opcode->n_rxfaults[i],
1172                    (unsigned long long)ibd->fault_opcode->n_txfaults[i]);
1173         return 0;
1174 }
1175
1176 DEBUGFS_SEQ_FILE_OPS(fault_stats);
1177 DEBUGFS_SEQ_FILE_OPEN(fault_stats);
1178 DEBUGFS_FILE_OPS(fault_stats);
1179
1180 static void fault_exit_opcode_debugfs(struct hfi1_ibdev *ibd)
1181 {
1182         if (ibd->fault_opcode)
1183                 debugfs_remove_recursive(ibd->fault_opcode->dir);
1184         kfree(ibd->fault_opcode);
1185         ibd->fault_opcode = NULL;
1186 }
1187
1188 static int fault_init_opcode_debugfs(struct hfi1_ibdev *ibd)
1189 {
1190         struct dentry *parent = ibd->hfi1_ibdev_dbg;
1191
1192         ibd->fault_opcode = kzalloc(sizeof(*ibd->fault_opcode), GFP_KERNEL);
1193         if (!ibd->fault_opcode)
1194                 return -ENOMEM;
1195
1196         ibd->fault_opcode->attr.interval = 1;
1197         ibd->fault_opcode->attr.require_end = ULONG_MAX;
1198         ibd->fault_opcode->attr.stacktrace_depth = 32;
1199         ibd->fault_opcode->attr.dname = NULL;
1200         ibd->fault_opcode->attr.verbose = 0;
1201         ibd->fault_opcode->fault_by_opcode = false;
1202         ibd->fault_opcode->opcode = 0;
1203         ibd->fault_opcode->mask = 0xff;
1204
1205         ibd->fault_opcode->dir =
1206                 fault_create_debugfs_attr("fault_opcode",
1207                                           parent,
1208                                           &ibd->fault_opcode->attr);
1209         if (IS_ERR(ibd->fault_opcode->dir)) {
1210                 kfree(ibd->fault_opcode);
1211                 ibd->fault_opcode = NULL;
1212                 return -ENOENT;
1213         }
1214
1215         DEBUGFS_SEQ_FILE_CREATE(fault_stats, ibd->fault_opcode->dir, ibd);
1216         if (!debugfs_create_bool("fault_by_opcode", 0600,
1217                                  ibd->fault_opcode->dir,
1218                                  &ibd->fault_opcode->fault_by_opcode))
1219                 goto fail;
1220         if (!debugfs_create_x8("opcode", 0600, ibd->fault_opcode->dir,
1221                                &ibd->fault_opcode->opcode))
1222                 goto fail;
1223         if (!debugfs_create_x8("mask", 0600, ibd->fault_opcode->dir,
1224                                &ibd->fault_opcode->mask))
1225                 goto fail;
1226
1227         return 0;
1228 fail:
1229         fault_exit_opcode_debugfs(ibd);
1230         return -ENOMEM;
1231 }
1232
1233 static void fault_exit_packet_debugfs(struct hfi1_ibdev *ibd)
1234 {
1235         if (ibd->fault_packet)
1236                 debugfs_remove_recursive(ibd->fault_packet->dir);
1237         kfree(ibd->fault_packet);
1238         ibd->fault_packet = NULL;
1239 }
1240
1241 static int fault_init_packet_debugfs(struct hfi1_ibdev *ibd)
1242 {
1243         struct dentry *parent = ibd->hfi1_ibdev_dbg;
1244
1245         ibd->fault_packet = kzalloc(sizeof(*ibd->fault_packet), GFP_KERNEL);
1246         if (!ibd->fault_packet)
1247                 return -ENOMEM;
1248
1249         ibd->fault_packet->attr.interval = 1;
1250         ibd->fault_packet->attr.require_end = ULONG_MAX;
1251         ibd->fault_packet->attr.stacktrace_depth = 32;
1252         ibd->fault_packet->attr.dname = NULL;
1253         ibd->fault_packet->attr.verbose = 0;
1254         ibd->fault_packet->fault_by_packet = false;
1255
1256         ibd->fault_packet->dir =
1257                 fault_create_debugfs_attr("fault_packet",
1258                                           parent,
1259                                           &ibd->fault_opcode->attr);
1260         if (IS_ERR(ibd->fault_packet->dir)) {
1261                 kfree(ibd->fault_packet);
1262                 ibd->fault_packet = NULL;
1263                 return -ENOENT;
1264         }
1265
1266         if (!debugfs_create_bool("fault_by_packet", 0600,
1267                                  ibd->fault_packet->dir,
1268                                  &ibd->fault_packet->fault_by_packet))
1269                 goto fail;
1270         if (!debugfs_create_u64("fault_stats", 0400,
1271                                 ibd->fault_packet->dir,
1272                                 &ibd->fault_packet->n_faults))
1273                 goto fail;
1274
1275         return 0;
1276 fail:
1277         fault_exit_packet_debugfs(ibd);
1278         return -ENOMEM;
1279 }
1280
1281 static void fault_exit_debugfs(struct hfi1_ibdev *ibd)
1282 {
1283         fault_exit_opcode_debugfs(ibd);
1284         fault_exit_packet_debugfs(ibd);
1285 }
1286
1287 static int fault_init_debugfs(struct hfi1_ibdev *ibd)
1288 {
1289         int ret = 0;
1290
1291         ret = fault_init_opcode_debugfs(ibd);
1292         if (ret)
1293                 return ret;
1294
1295         ret = fault_init_packet_debugfs(ibd);
1296         if (ret)
1297                 fault_exit_opcode_debugfs(ibd);
1298
1299         return ret;
1300 }
1301
1302 bool hfi1_dbg_fault_suppress_err(struct hfi1_ibdev *ibd)
1303 {
1304         return ibd->fault_suppress_err;
1305 }
1306
1307 bool hfi1_dbg_fault_opcode(struct rvt_qp *qp, u32 opcode, bool rx)
1308 {
1309         bool ret = false;
1310         struct hfi1_ibdev *ibd = to_idev(qp->ibqp.device);
1311
1312         if (!ibd->fault_opcode || !ibd->fault_opcode->fault_by_opcode)
1313                 return false;
1314         if (ibd->fault_opcode->opcode != (opcode & ibd->fault_opcode->mask))
1315                 return false;
1316         ret = should_fail(&ibd->fault_opcode->attr, 1);
1317         if (ret) {
1318                 trace_hfi1_fault_opcode(qp, opcode);
1319                 if (rx)
1320                         ibd->fault_opcode->n_rxfaults[opcode]++;
1321                 else
1322                         ibd->fault_opcode->n_txfaults[opcode]++;
1323         }
1324         return ret;
1325 }
1326
1327 bool hfi1_dbg_fault_packet(struct hfi1_packet *packet)
1328 {
1329         struct rvt_dev_info *rdi = &packet->rcd->ppd->dd->verbs_dev.rdi;
1330         struct hfi1_ibdev *ibd = dev_from_rdi(rdi);
1331         bool ret = false;
1332
1333         if (!ibd->fault_packet || !ibd->fault_packet->fault_by_packet)
1334                 return false;
1335
1336         ret = should_fail(&ibd->fault_packet->attr, 1);
1337         if (ret) {
1338                 ++ibd->fault_packet->n_faults;
1339                 trace_hfi1_fault_packet(packet);
1340         }
1341         return ret;
1342 }
1343 #endif
1344
1345 void hfi1_dbg_ibdev_init(struct hfi1_ibdev *ibd)
1346 {
1347         char name[sizeof("port0counters") + 1];
1348         char link[10];
1349         struct hfi1_devdata *dd = dd_from_dev(ibd);
1350         struct hfi1_pportdata *ppd;
1351         int unit = dd->unit;
1352         int i, j;
1353
1354         if (!hfi1_dbg_root)
1355                 return;
1356         snprintf(name, sizeof(name), "%s_%d", class_name(), unit);
1357         snprintf(link, sizeof(link), "%d", unit);
1358         ibd->hfi1_ibdev_dbg = debugfs_create_dir(name, hfi1_dbg_root);
1359         if (!ibd->hfi1_ibdev_dbg) {
1360                 pr_warn("create of %s failed\n", name);
1361                 return;
1362         }
1363         ibd->hfi1_ibdev_link =
1364                 debugfs_create_symlink(link, hfi1_dbg_root, name);
1365         if (!ibd->hfi1_ibdev_link) {
1366                 pr_warn("create of %s symlink failed\n", name);
1367                 return;
1368         }
1369         DEBUGFS_SEQ_FILE_CREATE(opcode_stats, ibd->hfi1_ibdev_dbg, ibd);
1370         DEBUGFS_SEQ_FILE_CREATE(ctx_stats, ibd->hfi1_ibdev_dbg, ibd);
1371         DEBUGFS_SEQ_FILE_CREATE(qp_stats, ibd->hfi1_ibdev_dbg, ibd);
1372         DEBUGFS_SEQ_FILE_CREATE(sdes, ibd->hfi1_ibdev_dbg, ibd);
1373         DEBUGFS_SEQ_FILE_CREATE(rcds, ibd->hfi1_ibdev_dbg, ibd);
1374         DEBUGFS_SEQ_FILE_CREATE(sdma_cpu_list, ibd->hfi1_ibdev_dbg, ibd);
1375         /* dev counter files */
1376         for (i = 0; i < ARRAY_SIZE(cntr_ops); i++)
1377                 DEBUGFS_FILE_CREATE(cntr_ops[i].name,
1378                                     ibd->hfi1_ibdev_dbg,
1379                                     dd,
1380                                     &cntr_ops[i].ops, S_IRUGO);
1381         /* per port files */
1382         for (ppd = dd->pport, j = 0; j < dd->num_pports; j++, ppd++)
1383                 for (i = 0; i < ARRAY_SIZE(port_cntr_ops); i++) {
1384                         snprintf(name,
1385                                  sizeof(name),
1386                                  port_cntr_ops[i].name,
1387                                  j + 1);
1388                         DEBUGFS_FILE_CREATE(name,
1389                                             ibd->hfi1_ibdev_dbg,
1390                                             ppd,
1391                                             &port_cntr_ops[i].ops,
1392                                             !port_cntr_ops[i].ops.write ?
1393                                             S_IRUGO : S_IRUGO | S_IWUSR);
1394                 }
1395
1396 #ifdef CONFIG_FAULT_INJECTION
1397         debugfs_create_bool("fault_suppress_err", 0600,
1398                             ibd->hfi1_ibdev_dbg,
1399                             &ibd->fault_suppress_err);
1400         fault_init_debugfs(ibd);
1401 #endif
1402 }
1403
1404 void hfi1_dbg_ibdev_exit(struct hfi1_ibdev *ibd)
1405 {
1406         if (!hfi1_dbg_root)
1407                 goto out;
1408 #ifdef CONFIG_FAULT_INJECTION
1409         fault_exit_debugfs(ibd);
1410 #endif
1411         debugfs_remove(ibd->hfi1_ibdev_link);
1412         debugfs_remove_recursive(ibd->hfi1_ibdev_dbg);
1413 out:
1414         ibd->hfi1_ibdev_dbg = NULL;
1415 }
1416
1417 /*
1418  * driver stats field names, one line per stat, single string.  Used by
1419  * programs like hfistats to print the stats in a way which works for
1420  * different versions of drivers, without changing program source.
1421  * if hfi1_ib_stats changes, this needs to change.  Names need to be
1422  * 12 chars or less (w/o newline), for proper display by hfistats utility.
1423  */
1424 static const char * const hfi1_statnames[] = {
1425         /* must be element 0*/
1426         "KernIntr",
1427         "ErrorIntr",
1428         "Tx_Errs",
1429         "Rcv_Errs",
1430         "H/W_Errs",
1431         "NoPIOBufs",
1432         "CtxtsOpen",
1433         "RcvLen_Errs",
1434         "EgrBufFull",
1435         "EgrHdrFull"
1436 };
1437
1438 static void *_driver_stats_names_seq_start(struct seq_file *s, loff_t *pos)
1439 {
1440         if (*pos >= ARRAY_SIZE(hfi1_statnames))
1441                 return NULL;
1442         return pos;
1443 }
1444
1445 static void *_driver_stats_names_seq_next(
1446         struct seq_file *s,
1447         void *v,
1448         loff_t *pos)
1449 {
1450         ++*pos;
1451         if (*pos >= ARRAY_SIZE(hfi1_statnames))
1452                 return NULL;
1453         return pos;
1454 }
1455
1456 static void _driver_stats_names_seq_stop(struct seq_file *s, void *v)
1457 {
1458 }
1459
1460 static int _driver_stats_names_seq_show(struct seq_file *s, void *v)
1461 {
1462         loff_t *spos = v;
1463
1464         seq_printf(s, "%s\n", hfi1_statnames[*spos]);
1465         return 0;
1466 }
1467
1468 DEBUGFS_SEQ_FILE_OPS(driver_stats_names);
1469 DEBUGFS_SEQ_FILE_OPEN(driver_stats_names)
1470 DEBUGFS_FILE_OPS(driver_stats_names);
1471
1472 static void *_driver_stats_seq_start(struct seq_file *s, loff_t *pos)
1473 {
1474         if (*pos >= ARRAY_SIZE(hfi1_statnames))
1475                 return NULL;
1476         return pos;
1477 }
1478
1479 static void *_driver_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
1480 {
1481         ++*pos;
1482         if (*pos >= ARRAY_SIZE(hfi1_statnames))
1483                 return NULL;
1484         return pos;
1485 }
1486
1487 static void _driver_stats_seq_stop(struct seq_file *s, void *v)
1488 {
1489 }
1490
1491 static u64 hfi1_sps_ints(void)
1492 {
1493         unsigned long flags;
1494         struct hfi1_devdata *dd;
1495         u64 sps_ints = 0;
1496
1497         spin_lock_irqsave(&hfi1_devs_lock, flags);
1498         list_for_each_entry(dd, &hfi1_dev_list, list) {
1499                 sps_ints += get_all_cpu_total(dd->int_counter);
1500         }
1501         spin_unlock_irqrestore(&hfi1_devs_lock, flags);
1502         return sps_ints;
1503 }
1504
1505 static int _driver_stats_seq_show(struct seq_file *s, void *v)
1506 {
1507         loff_t *spos = v;
1508         char *buffer;
1509         u64 *stats = (u64 *)&hfi1_stats;
1510         size_t sz = seq_get_buf(s, &buffer);
1511
1512         if (sz < sizeof(u64))
1513                 return SEQ_SKIP;
1514         /* special case for interrupts */
1515         if (*spos == 0)
1516                 *(u64 *)buffer = hfi1_sps_ints();
1517         else
1518                 *(u64 *)buffer = stats[*spos];
1519         seq_commit(s,  sizeof(u64));
1520         return 0;
1521 }
1522
1523 DEBUGFS_SEQ_FILE_OPS(driver_stats);
1524 DEBUGFS_SEQ_FILE_OPEN(driver_stats)
1525 DEBUGFS_FILE_OPS(driver_stats);
1526
1527 void hfi1_dbg_init(void)
1528 {
1529         hfi1_dbg_root  = debugfs_create_dir(DRIVER_NAME, NULL);
1530         if (!hfi1_dbg_root)
1531                 pr_warn("init of debugfs failed\n");
1532         DEBUGFS_SEQ_FILE_CREATE(driver_stats_names, hfi1_dbg_root, NULL);
1533         DEBUGFS_SEQ_FILE_CREATE(driver_stats, hfi1_dbg_root, NULL);
1534 }
1535
1536 void hfi1_dbg_exit(void)
1537 {
1538         debugfs_remove_recursive(hfi1_dbg_root);
1539         hfi1_dbg_root = NULL;
1540 }