OSDN Git Service

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