2 * Copyright(c) 2015-2017 Intel Corporation.
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.
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.
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.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
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
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.
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.
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>
64 static struct dentry *hfi1_dbg_root;
66 /* wrappers to enforce srcu in seq file */
67 static ssize_t hfi1_seq_read(
73 struct dentry *d = file->f_path.dentry;
77 r = debugfs_use_file_start(d, &srcu_idx);
79 r = seq_read(file, buf, size, ppos);
80 debugfs_use_file_finish(srcu_idx);
84 static loff_t hfi1_seq_lseek(
89 struct dentry *d = file->f_path.dentry;
93 r = debugfs_use_file_start(d, &srcu_idx);
95 r = seq_lseek(file, offset, whence);
96 debugfs_use_file_finish(srcu_idx);
100 #define private2dd(file) (file_inode(file)->i_private)
101 #define private2ppd(file) (file_inode(file)->i_private)
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 \
111 #define DEBUGFS_SEQ_FILE_OPEN(name) \
112 static int _##name##_open(struct inode *inode, struct file *s) \
114 struct seq_file *seq; \
116 ret = seq_open(s, &_##name##_seq_ops); \
119 seq = s->private_data; \
120 seq->private = inode->i_private; \
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 \
133 #define DEBUGFS_FILE_CREATE(name, parent, data, ops, mode) \
135 struct dentry *ent; \
136 ent = debugfs_create_file(name, mode, parent, \
139 pr_warn("create of %s failed\n", name); \
142 #define DEBUGFS_SEQ_FILE_CREATE(name, parent, data) \
143 DEBUGFS_FILE_CREATE(#name, parent, data, &_##name##_file_ops, S_IRUGO)
145 static void *_opcode_stats_seq_start(struct seq_file *s, loff_t *pos)
147 struct hfi1_opcode_stats_perctx *opstats;
149 if (*pos >= ARRAY_SIZE(opstats->stats))
154 static void *_opcode_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
156 struct hfi1_opcode_stats_perctx *opstats;
159 if (*pos >= ARRAY_SIZE(opstats->stats))
164 static void _opcode_stats_seq_stop(struct seq_file *s, void *v)
168 static int _opcode_stats_seq_show(struct seq_file *s, void *v)
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;
177 for (j = 0; j < dd->first_dyn_alloc_ctxt; j++) {
178 rcd = hfi1_rcd_get_by_index(dd, j);
180 n_packets += rcd->opstats->stats[i].n_packets;
181 n_bytes += rcd->opstats->stats[i].n_bytes;
185 if (!n_packets && !n_bytes)
187 seq_printf(s, "%02llx %llu/%llu\n", i,
188 (unsigned long long)n_packets,
189 (unsigned long long)n_bytes);
194 DEBUGFS_SEQ_FILE_OPS(opcode_stats);
195 DEBUGFS_SEQ_FILE_OPEN(opcode_stats)
196 DEBUGFS_FILE_OPS(opcode_stats);
198 static void *_ctx_stats_seq_start(struct seq_file *s, loff_t *pos)
200 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
201 struct hfi1_devdata *dd = dd_from_dev(ibd);
204 return SEQ_START_TOKEN;
205 if (*pos >= dd->first_dyn_alloc_ctxt)
210 static void *_ctx_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
212 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
213 struct hfi1_devdata *dd = dd_from_dev(ibd);
215 if (v == SEQ_START_TOKEN)
219 if (*pos >= dd->first_dyn_alloc_ctxt)
224 static void _ctx_stats_seq_stop(struct seq_file *s, void *v)
226 /* nothing allocated */
229 static int _ctx_stats_seq_show(struct seq_file *s, void *v)
234 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
235 struct hfi1_devdata *dd = dd_from_dev(ibd);
236 struct hfi1_ctxtdata *rcd;
238 if (v == SEQ_START_TOKEN) {
239 seq_puts(s, "Ctx:npkts\n");
246 rcd = hfi1_rcd_get_by_index(dd, i);
250 for (j = 0; j < ARRAY_SIZE(rcd->opstats->stats); j++)
251 n_packets += rcd->opstats->stats[j].n_packets;
258 seq_printf(s, " %llu:%llu\n", i, n_packets);
262 DEBUGFS_SEQ_FILE_OPS(ctx_stats);
263 DEBUGFS_SEQ_FILE_OPEN(ctx_stats)
264 DEBUGFS_FILE_OPS(ctx_stats);
266 static void *_qp_stats_seq_start(struct seq_file *s, loff_t *pos)
269 struct rvt_qp_iter *iter;
272 iter = rvt_qp_iter_init(s->private, 0, NULL);
274 /* stop calls rcu_read_unlock */
281 if (rvt_qp_iter_next(iter)) {
290 static void *_qp_stats_seq_next(struct seq_file *s, void *iter_ptr,
294 struct rvt_qp_iter *iter = iter_ptr;
298 if (rvt_qp_iter_next(iter)) {
306 static void _qp_stats_seq_stop(struct seq_file *s, void *iter_ptr)
312 static int _qp_stats_seq_show(struct seq_file *s, void *iter_ptr)
314 struct rvt_qp_iter *iter = iter_ptr;
319 qp_iter_print(s, iter);
324 DEBUGFS_SEQ_FILE_OPS(qp_stats);
325 DEBUGFS_SEQ_FILE_OPEN(qp_stats)
326 DEBUGFS_FILE_OPS(qp_stats);
328 static void *_sdes_seq_start(struct seq_file *s, loff_t *pos)
330 struct hfi1_ibdev *ibd;
331 struct hfi1_devdata *dd;
333 ibd = (struct hfi1_ibdev *)s->private;
334 dd = dd_from_dev(ibd);
335 if (!dd->per_sdma || *pos >= dd->num_sdma)
340 static void *_sdes_seq_next(struct seq_file *s, void *v, loff_t *pos)
342 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
343 struct hfi1_devdata *dd = dd_from_dev(ibd);
346 if (!dd->per_sdma || *pos >= dd->num_sdma)
351 static void _sdes_seq_stop(struct seq_file *s, void *v)
355 static int _sdes_seq_show(struct seq_file *s, void *v)
357 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
358 struct hfi1_devdata *dd = dd_from_dev(ibd);
362 sdma_seqfile_dump_sde(s, &dd->per_sdma[i]);
366 DEBUGFS_SEQ_FILE_OPS(sdes);
367 DEBUGFS_SEQ_FILE_OPEN(sdes)
368 DEBUGFS_FILE_OPS(sdes);
370 static void *_rcds_seq_start(struct seq_file *s, loff_t *pos)
372 struct hfi1_ibdev *ibd;
373 struct hfi1_devdata *dd;
375 ibd = (struct hfi1_ibdev *)s->private;
376 dd = dd_from_dev(ibd);
377 if (!dd->rcd || *pos >= dd->n_krcv_queues)
382 static void *_rcds_seq_next(struct seq_file *s, void *v, loff_t *pos)
384 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
385 struct hfi1_devdata *dd = dd_from_dev(ibd);
388 if (!dd->rcd || *pos >= dd->n_krcv_queues)
393 static void _rcds_seq_stop(struct seq_file *s, void *v)
397 static int _rcds_seq_show(struct seq_file *s, void *v)
399 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
400 struct hfi1_devdata *dd = dd_from_dev(ibd);
401 struct hfi1_ctxtdata *rcd;
405 rcd = hfi1_rcd_get_by_index(dd, i);
407 seqfile_dump_rcd(s, rcd);
412 DEBUGFS_SEQ_FILE_OPS(rcds);
413 DEBUGFS_SEQ_FILE_OPEN(rcds)
414 DEBUGFS_FILE_OPS(rcds);
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)
422 struct hfi1_devdata *dd;
425 dd = private2dd(file);
426 avail = hfi1_read_cntrs(dd, NULL, &counters);
427 rval = simple_read_from_buffer(buf, count, ppos, counters, avail);
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)
437 struct hfi1_devdata *dd;
440 dd = private2dd(file);
441 avail = hfi1_read_cntrs(dd, &names, NULL);
442 rval = simple_read_from_buffer(buf, count, ppos, names, avail);
446 struct counter_info {
448 const struct file_operations ops;
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...
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)
462 struct hfi1_devdata *dd;
465 dd = private2dd(file);
466 avail = hfi1_read_portcntrs(dd->pport, &names, NULL);
467 rval = simple_read_from_buffer(buf, count, ppos, names, avail);
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)
477 struct hfi1_pportdata *ppd;
480 ppd = private2ppd(file);
481 avail = hfi1_read_portcntrs(ppd, NULL, &counters);
482 rval = simple_read_from_buffer(buf, count, ppos, counters, avail);
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)
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",
496 this_hfi == hfi ? "this" : "other");
500 static ssize_t asic_flags_read(struct file *file, char __user *buf,
501 size_t count, loff_t *ppos)
503 struct hfi1_pportdata *ppd;
504 struct hfi1_devdata *dd;
512 ppd = private2ppd(file);
516 tmp = kmalloc(size, GFP_KERNEL);
520 scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
521 used += scnprintf(tmp + used, size - used,
522 "Resource flags: 0x%016llx\n", scratch0);
524 /* check permanent flag */
525 if (scratch0 & CR_THERM_INIT) {
526 used += scnprintf(tmp + used, size - used,
527 " 0x%08x - thermal monitoring initialized\n",
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,
535 check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
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");
542 used += scnprintf(tmp + used, size - used, "Write bits to clear\n");
544 ret = simple_read_from_buffer(buf, count, ppos, tmp, used);
549 static ssize_t asic_flags_write(struct file *file, const char __user *buf,
550 size_t count, loff_t *ppos)
552 struct hfi1_pportdata *ppd;
553 struct hfi1_devdata *dd;
556 unsigned long long value;
560 ppd = private2ppd(file);
563 /* zero terminate and read the expected integer */
564 buff = memdup_user_nul(buf, count);
566 return PTR_ERR(buff);
568 ret = kstrtoull(buff, 0, &value);
573 /* obtain exclusive access */
574 mutex_lock(&dd->asic_data->asic_resource_mutex);
575 acquire_hw_mutex(dd);
577 scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
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);
583 release_hw_mutex(dd);
584 mutex_unlock(&dd->asic_data->asic_resource_mutex);
586 /* return the number of bytes written */
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)
598 struct hfi1_pportdata *ppd = private2ppd(file);
603 /* the checks below expect the position to be positive */
607 tmp = kzalloc(DC8051_DATA_MEM_SIZE, GFP_KERNEL);
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
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));
628 rval = simple_read_from_buffer(buf, count, ppos, tmp,
629 DC8051_DATA_MEM_SIZE);
635 static ssize_t debugfs_lcb_read(struct file *file, char __user *buf,
636 size_t count, loff_t *ppos)
638 struct hfi1_pportdata *ppd = private2ppd(file);
639 struct hfi1_devdata *dd = ppd->dd;
640 unsigned long total, csr_off;
645 /* only read 8 byte quantities */
646 if ((count % 8) != 0)
648 /* offset must be 8-byte aligned */
649 if ((*ppos % 8) != 0)
651 /* do nothing if out of range or zero count */
652 if (*ppos >= (LCB_END - LCB_START) || !count)
654 /* reduce count if needed */
655 if (*ppos + count > LCB_END - LCB_START)
656 count = (LCB_END - LCB_START) - *ppos;
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))
662 if (put_user(data, (unsigned long __user *)(buf + total)))
669 static ssize_t debugfs_lcb_write(struct file *file, const char __user *buf,
670 size_t count, loff_t *ppos)
672 struct hfi1_pportdata *ppd = private2ppd(file);
673 struct hfi1_devdata *dd = ppd->dd;
674 unsigned long total, csr_off, data;
678 /* only write 8 byte quantities */
679 if ((count % 8) != 0)
681 /* offset must be 8-byte aligned */
682 if ((*ppos % 8) != 0)
684 /* do nothing if out of range or zero count */
685 if (*ppos >= (LCB_END - LCB_START) || !count)
687 /* reduce count if needed */
688 if (*ppos + count > LCB_END - LCB_START)
689 count = (LCB_END - LCB_START) - *ppos;
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)))
695 if (write_lcb_csr(dd, csr_off, data))
703 * read the per-port QSFP data for ppd
705 static ssize_t qsfp_debugfs_dump(struct file *file, char __user *buf,
706 size_t count, loff_t *ppos)
708 struct hfi1_pportdata *ppd;
712 ppd = private2ppd(file);
713 tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
717 ret = qsfp_dump(ppd, tmp, PAGE_SIZE);
719 ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
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)
728 struct hfi1_pportdata *ppd;
735 ppd = private2ppd(file);
737 /* byte offset format: [offsetSize][i2cAddr][offsetHigh][offsetLow] */
738 i2c_addr = (*ppos >> 16) & 0xffff;
739 offset = *ppos & 0xffff;
741 /* explicitly reject invalid address 0 to catch cp and cat */
745 buff = memdup_user(buf, count);
747 return PTR_ERR(buff);
749 total_written = i2c_write(ppd, target, i2c_addr, offset, buff, count);
750 if (total_written < 0) {
755 *ppos += total_written;
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)
768 return __i2c_debugfs_write(file, buf, count, ppos, 0);
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)
775 return __i2c_debugfs_write(file, buf, count, ppos, 1);
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)
782 struct hfi1_pportdata *ppd;
789 ppd = private2ppd(file);
791 /* byte offset format: [offsetSize][i2cAddr][offsetHigh][offsetLow] */
792 i2c_addr = (*ppos >> 16) & 0xffff;
793 offset = *ppos & 0xffff;
795 /* explicitly reject invalid address 0 to catch cp and cat */
799 buff = kmalloc(count, GFP_KERNEL);
803 total_read = i2c_read(ppd, target, i2c_addr, offset, buff, count);
804 if (total_read < 0) {
811 ret = copy_to_user(buf, buff, total_read);
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)
828 return __i2c_debugfs_read(file, buf, count, ppos, 0);
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)
835 return __i2c_debugfs_read(file, buf, count, ppos, 1);
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)
842 struct hfi1_pportdata *ppd;
847 if (*ppos + count > QSFP_PAGESIZE * 4) /* base page + page00-page03 */
850 ppd = private2ppd(file);
852 buff = memdup_user(buf, count);
854 return PTR_ERR(buff);
856 total_written = qsfp_write(ppd, target, *ppos, buff, count);
857 if (total_written < 0) {
862 *ppos += total_written;
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)
875 return __qsfp_debugfs_write(file, buf, count, ppos, 0);
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)
882 return __qsfp_debugfs_write(file, buf, count, ppos, 1);
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)
889 struct hfi1_pportdata *ppd;
894 if (*ppos + count > QSFP_PAGESIZE * 4) { /* base page + page00-page03 */
899 ppd = private2ppd(file);
901 buff = kmalloc(count, GFP_KERNEL);
907 total_read = qsfp_read(ppd, target, *ppos, buff, count);
908 if (total_read < 0) {
915 ret = copy_to_user(buf, buff, total_read);
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)
933 return __qsfp_debugfs_read(file, buf, count, ppos, 0);
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)
940 return __qsfp_debugfs_read(file, buf, count, ppos, 1);
943 static int __i2c_debugfs_open(struct inode *in, struct file *fp, u32 target)
945 struct hfi1_pportdata *ppd;
948 if (!try_module_get(THIS_MODULE))
951 ppd = private2ppd(fp);
953 ret = acquire_chip_resource(ppd->dd, i2c_target(target), 0);
954 if (ret) /* failed - release the module */
955 module_put(THIS_MODULE);
960 static int i2c1_debugfs_open(struct inode *in, struct file *fp)
962 return __i2c_debugfs_open(in, fp, 0);
965 static int i2c2_debugfs_open(struct inode *in, struct file *fp)
967 return __i2c_debugfs_open(in, fp, 1);
970 static int __i2c_debugfs_release(struct inode *in, struct file *fp, u32 target)
972 struct hfi1_pportdata *ppd;
974 ppd = private2ppd(fp);
976 release_chip_resource(ppd->dd, i2c_target(target));
977 module_put(THIS_MODULE);
982 static int i2c1_debugfs_release(struct inode *in, struct file *fp)
984 return __i2c_debugfs_release(in, fp, 0);
987 static int i2c2_debugfs_release(struct inode *in, struct file *fp)
989 return __i2c_debugfs_release(in, fp, 1);
992 static int __qsfp_debugfs_open(struct inode *in, struct file *fp, u32 target)
994 struct hfi1_pportdata *ppd;
997 if (!try_module_get(THIS_MODULE))
1000 ppd = private2ppd(fp);
1002 ret = acquire_chip_resource(ppd->dd, i2c_target(target), 0);
1003 if (ret) /* failed - release the module */
1004 module_put(THIS_MODULE);
1009 static int qsfp1_debugfs_open(struct inode *in, struct file *fp)
1011 return __qsfp_debugfs_open(in, fp, 0);
1014 static int qsfp2_debugfs_open(struct inode *in, struct file *fp)
1016 return __qsfp_debugfs_open(in, fp, 1);
1019 static int __qsfp_debugfs_release(struct inode *in, struct file *fp, u32 target)
1021 struct hfi1_pportdata *ppd;
1023 ppd = private2ppd(fp);
1025 release_chip_resource(ppd->dd, i2c_target(target));
1026 module_put(THIS_MODULE);
1031 static int qsfp1_debugfs_release(struct inode *in, struct file *fp)
1033 return __qsfp_debugfs_release(in, fp, 0);
1036 static int qsfp2_debugfs_release(struct inode *in, struct file *fp)
1038 return __qsfp_debugfs_release(in, fp, 1);
1041 #define DEBUGFS_OPS(nm, readroutine, writeroutine) \
1045 .read = readroutine, \
1046 .write = writeroutine, \
1047 .llseek = generic_file_llseek, \
1051 #define DEBUGFS_XOPS(nm, readf, writef, openf, releasef) \
1057 .llseek = generic_file_llseek, \
1059 .release = releasef \
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),
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),
1085 static void *_sdma_cpu_list_seq_start(struct seq_file *s, loff_t *pos)
1087 if (*pos >= num_online_cpus())
1093 static void *_sdma_cpu_list_seq_next(struct seq_file *s, void *v, loff_t *pos)
1096 if (*pos >= num_online_cpus())
1102 static void _sdma_cpu_list_seq_stop(struct seq_file *s, void *v)
1104 /* nothing allocated */
1107 static int _sdma_cpu_list_seq_show(struct seq_file *s, void *v)
1109 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
1110 struct hfi1_devdata *dd = dd_from_dev(ibd);
1114 sdma_seqfile_dump_cpu_list(s, dd, (unsigned long)i);
1118 DEBUGFS_SEQ_FILE_OPS(sdma_cpu_list);
1119 DEBUGFS_SEQ_FILE_OPEN(sdma_cpu_list)
1120 DEBUGFS_FILE_OPS(sdma_cpu_list);
1122 #ifdef CONFIG_FAULT_INJECTION
1123 static void *_fault_stats_seq_start(struct seq_file *s, loff_t *pos)
1125 struct hfi1_opcode_stats_perctx *opstats;
1127 if (*pos >= ARRAY_SIZE(opstats->stats))
1132 static void *_fault_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
1134 struct hfi1_opcode_stats_perctx *opstats;
1137 if (*pos >= ARRAY_SIZE(opstats->stats))
1142 static void _fault_stats_seq_stop(struct seq_file *s, void *v)
1146 static int _fault_stats_seq_show(struct seq_file *s, void *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;
1155 for (j = 0; j < dd->first_dyn_alloc_ctxt; j++) {
1156 rcd = hfi1_rcd_get_by_index(dd, j);
1158 n_packets += rcd->opstats->stats[i].n_packets;
1159 n_bytes += rcd->opstats->stats[i].n_bytes;
1163 if (!n_packets && !n_bytes)
1165 if (!ibd->fault_opcode->n_rxfaults[i] &&
1166 !ibd->fault_opcode->n_txfaults[i])
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]);
1176 DEBUGFS_SEQ_FILE_OPS(fault_stats);
1177 DEBUGFS_SEQ_FILE_OPEN(fault_stats);
1178 DEBUGFS_FILE_OPS(fault_stats);
1180 static void fault_exit_opcode_debugfs(struct hfi1_ibdev *ibd)
1182 if (ibd->fault_opcode)
1183 debugfs_remove_recursive(ibd->fault_opcode->dir);
1184 kfree(ibd->fault_opcode);
1185 ibd->fault_opcode = NULL;
1188 static int fault_init_opcode_debugfs(struct hfi1_ibdev *ibd)
1190 struct dentry *parent = ibd->hfi1_ibdev_dbg;
1192 ibd->fault_opcode = kzalloc(sizeof(*ibd->fault_opcode), GFP_KERNEL);
1193 if (!ibd->fault_opcode)
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;
1205 ibd->fault_opcode->dir =
1206 fault_create_debugfs_attr("fault_opcode",
1208 &ibd->fault_opcode->attr);
1209 if (IS_ERR(ibd->fault_opcode->dir)) {
1210 kfree(ibd->fault_opcode);
1211 ibd->fault_opcode = NULL;
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))
1220 if (!debugfs_create_x8("opcode", 0600, ibd->fault_opcode->dir,
1221 &ibd->fault_opcode->opcode))
1223 if (!debugfs_create_x8("mask", 0600, ibd->fault_opcode->dir,
1224 &ibd->fault_opcode->mask))
1229 fault_exit_opcode_debugfs(ibd);
1233 static void fault_exit_packet_debugfs(struct hfi1_ibdev *ibd)
1235 if (ibd->fault_packet)
1236 debugfs_remove_recursive(ibd->fault_packet->dir);
1237 kfree(ibd->fault_packet);
1238 ibd->fault_packet = NULL;
1241 static int fault_init_packet_debugfs(struct hfi1_ibdev *ibd)
1243 struct dentry *parent = ibd->hfi1_ibdev_dbg;
1245 ibd->fault_packet = kzalloc(sizeof(*ibd->fault_packet), GFP_KERNEL);
1246 if (!ibd->fault_packet)
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;
1256 ibd->fault_packet->dir =
1257 fault_create_debugfs_attr("fault_packet",
1259 &ibd->fault_opcode->attr);
1260 if (IS_ERR(ibd->fault_packet->dir)) {
1261 kfree(ibd->fault_packet);
1262 ibd->fault_packet = NULL;
1266 if (!debugfs_create_bool("fault_by_packet", 0600,
1267 ibd->fault_packet->dir,
1268 &ibd->fault_packet->fault_by_packet))
1270 if (!debugfs_create_u64("fault_stats", 0400,
1271 ibd->fault_packet->dir,
1272 &ibd->fault_packet->n_faults))
1277 fault_exit_packet_debugfs(ibd);
1281 static void fault_exit_debugfs(struct hfi1_ibdev *ibd)
1283 fault_exit_opcode_debugfs(ibd);
1284 fault_exit_packet_debugfs(ibd);
1287 static int fault_init_debugfs(struct hfi1_ibdev *ibd)
1291 ret = fault_init_opcode_debugfs(ibd);
1295 ret = fault_init_packet_debugfs(ibd);
1297 fault_exit_opcode_debugfs(ibd);
1302 bool hfi1_dbg_fault_suppress_err(struct hfi1_ibdev *ibd)
1304 return ibd->fault_suppress_err;
1307 bool hfi1_dbg_fault_opcode(struct rvt_qp *qp, u32 opcode, bool rx)
1310 struct hfi1_ibdev *ibd = to_idev(qp->ibqp.device);
1312 if (!ibd->fault_opcode || !ibd->fault_opcode->fault_by_opcode)
1314 if (ibd->fault_opcode->opcode != (opcode & ibd->fault_opcode->mask))
1316 ret = should_fail(&ibd->fault_opcode->attr, 1);
1318 trace_hfi1_fault_opcode(qp, opcode);
1320 ibd->fault_opcode->n_rxfaults[opcode]++;
1322 ibd->fault_opcode->n_txfaults[opcode]++;
1327 bool hfi1_dbg_fault_packet(struct hfi1_packet *packet)
1329 struct rvt_dev_info *rdi = &packet->rcd->ppd->dd->verbs_dev.rdi;
1330 struct hfi1_ibdev *ibd = dev_from_rdi(rdi);
1333 if (!ibd->fault_packet || !ibd->fault_packet->fault_by_packet)
1336 ret = should_fail(&ibd->fault_packet->attr, 1);
1338 ++ibd->fault_packet->n_faults;
1339 trace_hfi1_fault_packet(packet);
1345 void hfi1_dbg_ibdev_init(struct hfi1_ibdev *ibd)
1347 char name[sizeof("port0counters") + 1];
1349 struct hfi1_devdata *dd = dd_from_dev(ibd);
1350 struct hfi1_pportdata *ppd;
1351 int unit = dd->unit;
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);
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);
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,
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++) {
1386 port_cntr_ops[i].name,
1388 DEBUGFS_FILE_CREATE(name,
1389 ibd->hfi1_ibdev_dbg,
1391 &port_cntr_ops[i].ops,
1392 !port_cntr_ops[i].ops.write ?
1393 S_IRUGO : S_IRUGO | S_IWUSR);
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);
1404 void hfi1_dbg_ibdev_exit(struct hfi1_ibdev *ibd)
1408 #ifdef CONFIG_FAULT_INJECTION
1409 fault_exit_debugfs(ibd);
1411 debugfs_remove(ibd->hfi1_ibdev_link);
1412 debugfs_remove_recursive(ibd->hfi1_ibdev_dbg);
1414 ibd->hfi1_ibdev_dbg = NULL;
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.
1424 static const char * const hfi1_statnames[] = {
1425 /* must be element 0*/
1438 static void *_driver_stats_names_seq_start(struct seq_file *s, loff_t *pos)
1440 if (*pos >= ARRAY_SIZE(hfi1_statnames))
1445 static void *_driver_stats_names_seq_next(
1451 if (*pos >= ARRAY_SIZE(hfi1_statnames))
1456 static void _driver_stats_names_seq_stop(struct seq_file *s, void *v)
1460 static int _driver_stats_names_seq_show(struct seq_file *s, void *v)
1464 seq_printf(s, "%s\n", hfi1_statnames[*spos]);
1468 DEBUGFS_SEQ_FILE_OPS(driver_stats_names);
1469 DEBUGFS_SEQ_FILE_OPEN(driver_stats_names)
1470 DEBUGFS_FILE_OPS(driver_stats_names);
1472 static void *_driver_stats_seq_start(struct seq_file *s, loff_t *pos)
1474 if (*pos >= ARRAY_SIZE(hfi1_statnames))
1479 static void *_driver_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
1482 if (*pos >= ARRAY_SIZE(hfi1_statnames))
1487 static void _driver_stats_seq_stop(struct seq_file *s, void *v)
1491 static u64 hfi1_sps_ints(void)
1493 unsigned long flags;
1494 struct hfi1_devdata *dd;
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);
1501 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
1505 static int _driver_stats_seq_show(struct seq_file *s, void *v)
1509 u64 *stats = (u64 *)&hfi1_stats;
1510 size_t sz = seq_get_buf(s, &buffer);
1512 if (sz < sizeof(u64))
1514 /* special case for interrupts */
1516 *(u64 *)buffer = hfi1_sps_ints();
1518 *(u64 *)buffer = stats[*spos];
1519 seq_commit(s, sizeof(u64));
1523 DEBUGFS_SEQ_FILE_OPS(driver_stats);
1524 DEBUGFS_SEQ_FILE_OPEN(driver_stats)
1525 DEBUGFS_FILE_OPS(driver_stats);
1527 void hfi1_dbg_init(void)
1529 hfi1_dbg_root = debugfs_create_dir(DRIVER_NAME, NULL);
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);
1536 void hfi1_dbg_exit(void)
1538 debugfs_remove_recursive(hfi1_dbg_root);
1539 hfi1_dbg_root = NULL;