3 * Copyright(c) 2015-2017 Intel Corporation.
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.
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.
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.
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
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
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.
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.
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>
65 static struct dentry *hfi1_dbg_root;
67 /* wrappers to enforce srcu in seq file */
68 static ssize_t hfi1_seq_read(
74 struct dentry *d = file->f_path.dentry;
78 r = debugfs_use_file_start(d, &srcu_idx);
80 r = seq_read(file, buf, size, ppos);
81 debugfs_use_file_finish(srcu_idx);
85 static loff_t hfi1_seq_lseek(
90 struct dentry *d = file->f_path.dentry;
94 r = debugfs_use_file_start(d, &srcu_idx);
96 r = seq_lseek(file, offset, whence);
97 debugfs_use_file_finish(srcu_idx);
101 #define private2dd(file) (file_inode(file)->i_private)
102 #define private2ppd(file) (file_inode(file)->i_private)
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 \
112 #define DEBUGFS_SEQ_FILE_OPEN(name) \
113 static int _##name##_open(struct inode *inode, struct file *s) \
115 struct seq_file *seq; \
117 ret = seq_open(s, &_##name##_seq_ops); \
120 seq = s->private_data; \
121 seq->private = inode->i_private; \
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 \
134 #define DEBUGFS_FILE_CREATE(name, parent, data, ops, mode) \
136 struct dentry *ent; \
137 ent = debugfs_create_file(name, mode, parent, \
140 pr_warn("create of %s failed\n", name); \
143 #define DEBUGFS_SEQ_FILE_CREATE(name, parent, data) \
144 DEBUGFS_FILE_CREATE(#name, parent, data, &_##name##_file_ops, S_IRUGO)
146 static void *_opcode_stats_seq_start(struct seq_file *s, loff_t *pos)
148 struct hfi1_opcode_stats_perctx *opstats;
150 if (*pos >= ARRAY_SIZE(opstats->stats))
155 static void *_opcode_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
157 struct hfi1_opcode_stats_perctx *opstats;
160 if (*pos >= ARRAY_SIZE(opstats->stats))
165 static void _opcode_stats_seq_stop(struct seq_file *s, void *v)
169 static int _opcode_stats_seq_show(struct seq_file *s, void *v)
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);
177 for (j = 0; j < dd->first_dyn_alloc_ctxt; j++) {
180 n_packets += dd->rcd[j]->opstats->stats[i].n_packets;
181 n_bytes += dd->rcd[j]->opstats->stats[i].n_bytes;
183 if (!n_packets && !n_bytes)
185 seq_printf(s, "%02llx %llu/%llu\n", i,
186 (unsigned long long)n_packets,
187 (unsigned long long)n_bytes);
192 DEBUGFS_SEQ_FILE_OPS(opcode_stats);
193 DEBUGFS_SEQ_FILE_OPEN(opcode_stats)
194 DEBUGFS_FILE_OPS(opcode_stats);
196 static void *_ctx_stats_seq_start(struct seq_file *s, loff_t *pos)
198 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
199 struct hfi1_devdata *dd = dd_from_dev(ibd);
202 return SEQ_START_TOKEN;
203 if (*pos >= dd->first_dyn_alloc_ctxt)
208 static void *_ctx_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
210 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
211 struct hfi1_devdata *dd = dd_from_dev(ibd);
213 if (v == SEQ_START_TOKEN)
217 if (*pos >= dd->first_dyn_alloc_ctxt)
222 static void _ctx_stats_seq_stop(struct seq_file *s, void *v)
224 /* nothing allocated */
227 static int _ctx_stats_seq_show(struct seq_file *s, void *v)
232 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
233 struct hfi1_devdata *dd = dd_from_dev(ibd);
235 if (v == SEQ_START_TOKEN) {
236 seq_puts(s, "Ctx:npkts\n");
246 for (j = 0; j < ARRAY_SIZE(dd->rcd[i]->opstats->stats); j++)
247 n_packets += dd->rcd[i]->opstats->stats[j].n_packets;
252 seq_printf(s, " %llu:%llu\n", i, n_packets);
256 DEBUGFS_SEQ_FILE_OPS(ctx_stats);
257 DEBUGFS_SEQ_FILE_OPEN(ctx_stats)
258 DEBUGFS_FILE_OPS(ctx_stats);
260 static void *_qp_stats_seq_start(struct seq_file *s, loff_t *pos)
263 struct qp_iter *iter;
266 iter = qp_iter_init(s->private);
268 /* stop calls rcu_read_unlock */
275 if (qp_iter_next(iter)) {
284 static void *_qp_stats_seq_next(struct seq_file *s, void *iter_ptr,
288 struct qp_iter *iter = iter_ptr;
292 if (qp_iter_next(iter)) {
300 static void _qp_stats_seq_stop(struct seq_file *s, void *iter_ptr)
306 static int _qp_stats_seq_show(struct seq_file *s, void *iter_ptr)
308 struct qp_iter *iter = iter_ptr;
313 qp_iter_print(s, iter);
318 DEBUGFS_SEQ_FILE_OPS(qp_stats);
319 DEBUGFS_SEQ_FILE_OPEN(qp_stats)
320 DEBUGFS_FILE_OPS(qp_stats);
322 static void *_sdes_seq_start(struct seq_file *s, loff_t *pos)
324 struct hfi1_ibdev *ibd;
325 struct hfi1_devdata *dd;
327 ibd = (struct hfi1_ibdev *)s->private;
328 dd = dd_from_dev(ibd);
329 if (!dd->per_sdma || *pos >= dd->num_sdma)
334 static void *_sdes_seq_next(struct seq_file *s, void *v, loff_t *pos)
336 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
337 struct hfi1_devdata *dd = dd_from_dev(ibd);
340 if (!dd->per_sdma || *pos >= dd->num_sdma)
345 static void _sdes_seq_stop(struct seq_file *s, void *v)
349 static int _sdes_seq_show(struct seq_file *s, void *v)
351 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
352 struct hfi1_devdata *dd = dd_from_dev(ibd);
356 sdma_seqfile_dump_sde(s, &dd->per_sdma[i]);
360 DEBUGFS_SEQ_FILE_OPS(sdes);
361 DEBUGFS_SEQ_FILE_OPEN(sdes)
362 DEBUGFS_FILE_OPS(sdes);
364 /* read the per-device counters */
365 static ssize_t dev_counters_read(struct file *file, char __user *buf,
366 size_t count, loff_t *ppos)
370 struct hfi1_devdata *dd;
373 dd = private2dd(file);
374 avail = hfi1_read_cntrs(dd, NULL, &counters);
375 rval = simple_read_from_buffer(buf, count, ppos, counters, avail);
379 /* read the per-device counters */
380 static ssize_t dev_names_read(struct file *file, char __user *buf,
381 size_t count, loff_t *ppos)
385 struct hfi1_devdata *dd;
388 dd = private2dd(file);
389 avail = hfi1_read_cntrs(dd, &names, NULL);
390 rval = simple_read_from_buffer(buf, count, ppos, names, avail);
394 struct counter_info {
396 const struct file_operations ops;
400 * Could use file_inode(file)->i_ino to figure out which file,
401 * instead of separate routine for each, but for now, this works...
404 /* read the per-port names (same for each port) */
405 static ssize_t portnames_read(struct file *file, char __user *buf,
406 size_t count, loff_t *ppos)
410 struct hfi1_devdata *dd;
413 dd = private2dd(file);
414 avail = hfi1_read_portcntrs(dd->pport, &names, NULL);
415 rval = simple_read_from_buffer(buf, count, ppos, names, avail);
419 /* read the per-port counters */
420 static ssize_t portcntrs_debugfs_read(struct file *file, char __user *buf,
421 size_t count, loff_t *ppos)
425 struct hfi1_pportdata *ppd;
428 ppd = private2ppd(file);
429 avail = hfi1_read_portcntrs(ppd, NULL, &counters);
430 rval = simple_read_from_buffer(buf, count, ppos, counters, avail);
434 static void check_dyn_flag(u64 scratch0, char *p, int size, int *used,
435 int this_hfi, int hfi, u32 flag, const char *what)
439 mask = flag << (hfi ? CR_DYN_SHIFT : 0);
440 if (scratch0 & mask) {
441 *used += scnprintf(p + *used, size - *used,
442 " 0x%08x - HFI%d %s in use, %s device\n",
444 this_hfi == hfi ? "this" : "other");
448 static ssize_t asic_flags_read(struct file *file, char __user *buf,
449 size_t count, loff_t *ppos)
451 struct hfi1_pportdata *ppd;
452 struct hfi1_devdata *dd;
460 ppd = private2ppd(file);
464 tmp = kmalloc(size, GFP_KERNEL);
468 scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
469 used += scnprintf(tmp + used, size - used,
470 "Resource flags: 0x%016llx\n", scratch0);
472 /* check permanent flag */
473 if (scratch0 & CR_THERM_INIT) {
474 used += scnprintf(tmp + used, size - used,
475 " 0x%08x - thermal monitoring initialized\n",
479 /* check each dynamic flag on each HFI */
480 for (i = 0; i < 2; i++) {
481 check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
483 check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
485 check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
486 CR_I2C1, "i2c chain 1");
487 check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
488 CR_I2C2, "i2c chain 2");
490 used += scnprintf(tmp + used, size - used, "Write bits to clear\n");
492 ret = simple_read_from_buffer(buf, count, ppos, tmp, used);
497 static ssize_t asic_flags_write(struct file *file, const char __user *buf,
498 size_t count, loff_t *ppos)
500 struct hfi1_pportdata *ppd;
501 struct hfi1_devdata *dd;
504 unsigned long long value;
508 ppd = private2ppd(file);
511 /* zero terminate and read the expected integer */
512 buff = memdup_user_nul(buf, count);
514 return PTR_ERR(buff);
516 ret = kstrtoull(buff, 0, &value);
521 /* obtain exclusive access */
522 mutex_lock(&dd->asic_data->asic_resource_mutex);
523 acquire_hw_mutex(dd);
525 scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
527 write_csr(dd, ASIC_CFG_SCRATCH, scratch0);
528 /* force write to be visible to other HFI on another OS */
529 (void)read_csr(dd, ASIC_CFG_SCRATCH);
531 release_hw_mutex(dd);
532 mutex_unlock(&dd->asic_data->asic_resource_mutex);
534 /* return the number of bytes written */
542 /* read the dc8051 memory */
543 static ssize_t dc8051_memory_read(struct file *file, char __user *buf,
544 size_t count, loff_t *ppos)
546 struct hfi1_pportdata *ppd = private2ppd(file);
551 /* the checks below expect the position to be positive */
555 tmp = kzalloc(DC8051_DATA_MEM_SIZE, GFP_KERNEL);
560 * Fill in the requested portion of the temporary buffer from the
561 * 8051 memory. The 8051 memory read is done in terms of 8 bytes.
562 * Adjust start and end to fit. Skip reading anything if out of
565 start = *ppos & ~0x7; /* round down */
566 if (start < DC8051_DATA_MEM_SIZE) {
567 end = (*ppos + count + 7) & ~0x7; /* round up */
568 if (end > DC8051_DATA_MEM_SIZE)
569 end = DC8051_DATA_MEM_SIZE;
570 rval = read_8051_data(ppd->dd, start, end - start,
571 (u64 *)(tmp + start));
576 rval = simple_read_from_buffer(buf, count, ppos, tmp,
577 DC8051_DATA_MEM_SIZE);
583 static ssize_t debugfs_lcb_read(struct file *file, char __user *buf,
584 size_t count, loff_t *ppos)
586 struct hfi1_pportdata *ppd = private2ppd(file);
587 struct hfi1_devdata *dd = ppd->dd;
588 unsigned long total, csr_off;
593 /* only read 8 byte quantities */
594 if ((count % 8) != 0)
596 /* offset must be 8-byte aligned */
597 if ((*ppos % 8) != 0)
599 /* do nothing if out of range or zero count */
600 if (*ppos >= (LCB_END - LCB_START) || !count)
602 /* reduce count if needed */
603 if (*ppos + count > LCB_END - LCB_START)
604 count = (LCB_END - LCB_START) - *ppos;
606 csr_off = LCB_START + *ppos;
607 for (total = 0; total < count; total += 8, csr_off += 8) {
608 if (read_lcb_csr(dd, csr_off, (u64 *)&data))
610 if (put_user(data, (unsigned long __user *)(buf + total)))
617 static ssize_t debugfs_lcb_write(struct file *file, const char __user *buf,
618 size_t count, loff_t *ppos)
620 struct hfi1_pportdata *ppd = private2ppd(file);
621 struct hfi1_devdata *dd = ppd->dd;
622 unsigned long total, csr_off, data;
626 /* only write 8 byte quantities */
627 if ((count % 8) != 0)
629 /* offset must be 8-byte aligned */
630 if ((*ppos % 8) != 0)
632 /* do nothing if out of range or zero count */
633 if (*ppos >= (LCB_END - LCB_START) || !count)
635 /* reduce count if needed */
636 if (*ppos + count > LCB_END - LCB_START)
637 count = (LCB_END - LCB_START) - *ppos;
639 csr_off = LCB_START + *ppos;
640 for (total = 0; total < count; total += 8, csr_off += 8) {
641 if (get_user(data, (unsigned long __user *)(buf + total)))
643 if (write_lcb_csr(dd, csr_off, data))
651 * read the per-port QSFP data for ppd
653 static ssize_t qsfp_debugfs_dump(struct file *file, char __user *buf,
654 size_t count, loff_t *ppos)
656 struct hfi1_pportdata *ppd;
660 ppd = private2ppd(file);
661 tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
665 ret = qsfp_dump(ppd, tmp, PAGE_SIZE);
667 ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
672 /* Do an i2c write operation on the chain for the given HFI. */
673 static ssize_t __i2c_debugfs_write(struct file *file, const char __user *buf,
674 size_t count, loff_t *ppos, u32 target)
676 struct hfi1_pportdata *ppd;
683 ppd = private2ppd(file);
685 /* byte offset format: [offsetSize][i2cAddr][offsetHigh][offsetLow] */
686 i2c_addr = (*ppos >> 16) & 0xffff;
687 offset = *ppos & 0xffff;
689 /* explicitly reject invalid address 0 to catch cp and cat */
693 buff = memdup_user(buf, count);
695 return PTR_ERR(buff);
697 total_written = i2c_write(ppd, target, i2c_addr, offset, buff, count);
698 if (total_written < 0) {
703 *ppos += total_written;
712 /* Do an i2c write operation on chain for HFI 0. */
713 static ssize_t i2c1_debugfs_write(struct file *file, const char __user *buf,
714 size_t count, loff_t *ppos)
716 return __i2c_debugfs_write(file, buf, count, ppos, 0);
719 /* Do an i2c write operation on chain for HFI 1. */
720 static ssize_t i2c2_debugfs_write(struct file *file, const char __user *buf,
721 size_t count, loff_t *ppos)
723 return __i2c_debugfs_write(file, buf, count, ppos, 1);
726 /* Do an i2c read operation on the chain for the given HFI. */
727 static ssize_t __i2c_debugfs_read(struct file *file, char __user *buf,
728 size_t count, loff_t *ppos, u32 target)
730 struct hfi1_pportdata *ppd;
737 ppd = private2ppd(file);
739 /* byte offset format: [offsetSize][i2cAddr][offsetHigh][offsetLow] */
740 i2c_addr = (*ppos >> 16) & 0xffff;
741 offset = *ppos & 0xffff;
743 /* explicitly reject invalid address 0 to catch cp and cat */
747 buff = kmalloc(count, GFP_KERNEL);
751 total_read = i2c_read(ppd, target, i2c_addr, offset, buff, count);
752 if (total_read < 0) {
759 ret = copy_to_user(buf, buff, total_read);
772 /* Do an i2c read operation on chain for HFI 0. */
773 static ssize_t i2c1_debugfs_read(struct file *file, char __user *buf,
774 size_t count, loff_t *ppos)
776 return __i2c_debugfs_read(file, buf, count, ppos, 0);
779 /* Do an i2c read operation on chain for HFI 1. */
780 static ssize_t i2c2_debugfs_read(struct file *file, char __user *buf,
781 size_t count, loff_t *ppos)
783 return __i2c_debugfs_read(file, buf, count, ppos, 1);
786 /* Do a QSFP write operation on the i2c chain for the given HFI. */
787 static ssize_t __qsfp_debugfs_write(struct file *file, const char __user *buf,
788 size_t count, loff_t *ppos, u32 target)
790 struct hfi1_pportdata *ppd;
795 if (*ppos + count > QSFP_PAGESIZE * 4) /* base page + page00-page03 */
798 ppd = private2ppd(file);
800 buff = memdup_user(buf, count);
802 return PTR_ERR(buff);
804 total_written = qsfp_write(ppd, target, *ppos, buff, count);
805 if (total_written < 0) {
810 *ppos += total_written;
819 /* Do a QSFP write operation on i2c chain for HFI 0. */
820 static ssize_t qsfp1_debugfs_write(struct file *file, const char __user *buf,
821 size_t count, loff_t *ppos)
823 return __qsfp_debugfs_write(file, buf, count, ppos, 0);
826 /* Do a QSFP write operation on i2c chain for HFI 1. */
827 static ssize_t qsfp2_debugfs_write(struct file *file, const char __user *buf,
828 size_t count, loff_t *ppos)
830 return __qsfp_debugfs_write(file, buf, count, ppos, 1);
833 /* Do a QSFP read operation on the i2c chain for the given HFI. */
834 static ssize_t __qsfp_debugfs_read(struct file *file, char __user *buf,
835 size_t count, loff_t *ppos, u32 target)
837 struct hfi1_pportdata *ppd;
842 if (*ppos + count > QSFP_PAGESIZE * 4) { /* base page + page00-page03 */
847 ppd = private2ppd(file);
849 buff = kmalloc(count, GFP_KERNEL);
855 total_read = qsfp_read(ppd, target, *ppos, buff, count);
856 if (total_read < 0) {
863 ret = copy_to_user(buf, buff, total_read);
877 /* Do a QSFP read operation on i2c chain for HFI 0. */
878 static ssize_t qsfp1_debugfs_read(struct file *file, char __user *buf,
879 size_t count, loff_t *ppos)
881 return __qsfp_debugfs_read(file, buf, count, ppos, 0);
884 /* Do a QSFP read operation on i2c chain for HFI 1. */
885 static ssize_t qsfp2_debugfs_read(struct file *file, char __user *buf,
886 size_t count, loff_t *ppos)
888 return __qsfp_debugfs_read(file, buf, count, ppos, 1);
891 static int __i2c_debugfs_open(struct inode *in, struct file *fp, u32 target)
893 struct hfi1_pportdata *ppd;
896 if (!try_module_get(THIS_MODULE))
899 ppd = private2ppd(fp);
901 ret = acquire_chip_resource(ppd->dd, i2c_target(target), 0);
902 if (ret) /* failed - release the module */
903 module_put(THIS_MODULE);
908 static int i2c1_debugfs_open(struct inode *in, struct file *fp)
910 return __i2c_debugfs_open(in, fp, 0);
913 static int i2c2_debugfs_open(struct inode *in, struct file *fp)
915 return __i2c_debugfs_open(in, fp, 1);
918 static int __i2c_debugfs_release(struct inode *in, struct file *fp, u32 target)
920 struct hfi1_pportdata *ppd;
922 ppd = private2ppd(fp);
924 release_chip_resource(ppd->dd, i2c_target(target));
925 module_put(THIS_MODULE);
930 static int i2c1_debugfs_release(struct inode *in, struct file *fp)
932 return __i2c_debugfs_release(in, fp, 0);
935 static int i2c2_debugfs_release(struct inode *in, struct file *fp)
937 return __i2c_debugfs_release(in, fp, 1);
940 static int __qsfp_debugfs_open(struct inode *in, struct file *fp, u32 target)
942 struct hfi1_pportdata *ppd;
945 if (!try_module_get(THIS_MODULE))
948 ppd = private2ppd(fp);
950 ret = acquire_chip_resource(ppd->dd, i2c_target(target), 0);
951 if (ret) /* failed - release the module */
952 module_put(THIS_MODULE);
957 static int qsfp1_debugfs_open(struct inode *in, struct file *fp)
959 return __qsfp_debugfs_open(in, fp, 0);
962 static int qsfp2_debugfs_open(struct inode *in, struct file *fp)
964 return __qsfp_debugfs_open(in, fp, 1);
967 static int __qsfp_debugfs_release(struct inode *in, struct file *fp, u32 target)
969 struct hfi1_pportdata *ppd;
971 ppd = private2ppd(fp);
973 release_chip_resource(ppd->dd, i2c_target(target));
974 module_put(THIS_MODULE);
979 static int qsfp1_debugfs_release(struct inode *in, struct file *fp)
981 return __qsfp_debugfs_release(in, fp, 0);
984 static int qsfp2_debugfs_release(struct inode *in, struct file *fp)
986 return __qsfp_debugfs_release(in, fp, 1);
989 #define DEBUGFS_OPS(nm, readroutine, writeroutine) \
993 .read = readroutine, \
994 .write = writeroutine, \
995 .llseek = generic_file_llseek, \
999 #define DEBUGFS_XOPS(nm, readf, writef, openf, releasef) \
1005 .llseek = generic_file_llseek, \
1007 .release = releasef \
1011 static const struct counter_info cntr_ops[] = {
1012 DEBUGFS_OPS("counter_names", dev_names_read, NULL),
1013 DEBUGFS_OPS("counters", dev_counters_read, NULL),
1014 DEBUGFS_OPS("portcounter_names", portnames_read, NULL),
1017 static const struct counter_info port_cntr_ops[] = {
1018 DEBUGFS_OPS("port%dcounters", portcntrs_debugfs_read, NULL),
1019 DEBUGFS_XOPS("i2c1", i2c1_debugfs_read, i2c1_debugfs_write,
1020 i2c1_debugfs_open, i2c1_debugfs_release),
1021 DEBUGFS_XOPS("i2c2", i2c2_debugfs_read, i2c2_debugfs_write,
1022 i2c2_debugfs_open, i2c2_debugfs_release),
1023 DEBUGFS_OPS("qsfp_dump%d", qsfp_debugfs_dump, NULL),
1024 DEBUGFS_XOPS("qsfp1", qsfp1_debugfs_read, qsfp1_debugfs_write,
1025 qsfp1_debugfs_open, qsfp1_debugfs_release),
1026 DEBUGFS_XOPS("qsfp2", qsfp2_debugfs_read, qsfp2_debugfs_write,
1027 qsfp2_debugfs_open, qsfp2_debugfs_release),
1028 DEBUGFS_OPS("asic_flags", asic_flags_read, asic_flags_write),
1029 DEBUGFS_OPS("dc8051_memory", dc8051_memory_read, NULL),
1030 DEBUGFS_OPS("lcb", debugfs_lcb_read, debugfs_lcb_write),
1033 static void *_sdma_cpu_list_seq_start(struct seq_file *s, loff_t *pos)
1035 if (*pos >= num_online_cpus())
1041 static void *_sdma_cpu_list_seq_next(struct seq_file *s, void *v, loff_t *pos)
1044 if (*pos >= num_online_cpus())
1050 static void _sdma_cpu_list_seq_stop(struct seq_file *s, void *v)
1052 /* nothing allocated */
1055 static int _sdma_cpu_list_seq_show(struct seq_file *s, void *v)
1057 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
1058 struct hfi1_devdata *dd = dd_from_dev(ibd);
1062 sdma_seqfile_dump_cpu_list(s, dd, (unsigned long)i);
1066 DEBUGFS_SEQ_FILE_OPS(sdma_cpu_list);
1067 DEBUGFS_SEQ_FILE_OPEN(sdma_cpu_list)
1068 DEBUGFS_FILE_OPS(sdma_cpu_list);
1070 #ifdef CONFIG_FAULT_INJECTION
1071 static void *_fault_stats_seq_start(struct seq_file *s, loff_t *pos)
1073 struct hfi1_opcode_stats_perctx *opstats;
1075 if (*pos >= ARRAY_SIZE(opstats->stats))
1080 static void *_fault_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
1082 struct hfi1_opcode_stats_perctx *opstats;
1085 if (*pos >= ARRAY_SIZE(opstats->stats))
1090 static void _fault_stats_seq_stop(struct seq_file *s, void *v)
1094 static int _fault_stats_seq_show(struct seq_file *s, void *v)
1097 loff_t i = *spos, j;
1098 u64 n_packets = 0, n_bytes = 0;
1099 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
1100 struct hfi1_devdata *dd = dd_from_dev(ibd);
1102 for (j = 0; j < dd->first_dyn_alloc_ctxt; j++) {
1105 n_packets += dd->rcd[j]->opstats->stats[i].n_packets;
1106 n_bytes += dd->rcd[j]->opstats->stats[i].n_bytes;
1108 if (!n_packets && !n_bytes)
1110 if (!ibd->fault_opcode->n_rxfaults[i] &&
1111 !ibd->fault_opcode->n_txfaults[i])
1113 seq_printf(s, "%02llx %llu/%llu (faults rx:%llu faults: tx:%llu)\n", i,
1114 (unsigned long long)n_packets,
1115 (unsigned long long)n_bytes,
1116 (unsigned long long)ibd->fault_opcode->n_rxfaults[i],
1117 (unsigned long long)ibd->fault_opcode->n_txfaults[i]);
1121 DEBUGFS_SEQ_FILE_OPS(fault_stats);
1122 DEBUGFS_SEQ_FILE_OPEN(fault_stats);
1123 DEBUGFS_FILE_OPS(fault_stats);
1125 static void fault_exit_opcode_debugfs(struct hfi1_ibdev *ibd)
1127 debugfs_remove_recursive(ibd->fault_opcode->dir);
1128 kfree(ibd->fault_opcode);
1129 ibd->fault_opcode = NULL;
1132 static int fault_init_opcode_debugfs(struct hfi1_ibdev *ibd)
1134 struct dentry *parent = ibd->hfi1_ibdev_dbg;
1136 ibd->fault_opcode = kzalloc(sizeof(*ibd->fault_opcode), GFP_KERNEL);
1137 if (!ibd->fault_opcode)
1140 ibd->fault_opcode->attr.interval = 1;
1141 ibd->fault_opcode->attr.require_end = ULONG_MAX;
1142 ibd->fault_opcode->attr.stacktrace_depth = 32;
1143 ibd->fault_opcode->attr.dname = NULL;
1144 ibd->fault_opcode->attr.verbose = 0;
1145 ibd->fault_opcode->fault_by_opcode = false;
1146 ibd->fault_opcode->opcode = 0;
1147 ibd->fault_opcode->mask = 0xff;
1149 ibd->fault_opcode->dir =
1150 fault_create_debugfs_attr("fault_opcode",
1152 &ibd->fault_opcode->attr);
1153 if (IS_ERR(ibd->fault_opcode->dir)) {
1154 kfree(ibd->fault_opcode);
1158 DEBUGFS_SEQ_FILE_CREATE(fault_stats, ibd->fault_opcode->dir, ibd);
1159 if (!debugfs_create_bool("fault_by_opcode", 0600,
1160 ibd->fault_opcode->dir,
1161 &ibd->fault_opcode->fault_by_opcode))
1163 if (!debugfs_create_x8("opcode", 0600, ibd->fault_opcode->dir,
1164 &ibd->fault_opcode->opcode))
1166 if (!debugfs_create_x8("mask", 0600, ibd->fault_opcode->dir,
1167 &ibd->fault_opcode->mask))
1172 fault_exit_opcode_debugfs(ibd);
1176 static void fault_exit_packet_debugfs(struct hfi1_ibdev *ibd)
1178 debugfs_remove_recursive(ibd->fault_packet->dir);
1179 kfree(ibd->fault_packet);
1180 ibd->fault_packet = NULL;
1183 static int fault_init_packet_debugfs(struct hfi1_ibdev *ibd)
1185 struct dentry *parent = ibd->hfi1_ibdev_dbg;
1187 ibd->fault_packet = kzalloc(sizeof(*ibd->fault_packet), GFP_KERNEL);
1188 if (!ibd->fault_packet)
1191 ibd->fault_packet->attr.interval = 1;
1192 ibd->fault_packet->attr.require_end = ULONG_MAX;
1193 ibd->fault_packet->attr.stacktrace_depth = 32;
1194 ibd->fault_packet->attr.dname = NULL;
1195 ibd->fault_packet->attr.verbose = 0;
1196 ibd->fault_packet->fault_by_packet = false;
1198 ibd->fault_packet->dir =
1199 fault_create_debugfs_attr("fault_packet",
1201 &ibd->fault_opcode->attr);
1202 if (IS_ERR(ibd->fault_packet->dir)) {
1203 kfree(ibd->fault_packet);
1207 if (!debugfs_create_bool("fault_by_packet", 0600,
1208 ibd->fault_packet->dir,
1209 &ibd->fault_packet->fault_by_packet))
1211 if (!debugfs_create_u64("fault_stats", 0400,
1212 ibd->fault_packet->dir,
1213 &ibd->fault_packet->n_faults))
1218 fault_exit_packet_debugfs(ibd);
1222 static void fault_exit_debugfs(struct hfi1_ibdev *ibd)
1224 fault_exit_opcode_debugfs(ibd);
1225 fault_exit_packet_debugfs(ibd);
1228 static int fault_init_debugfs(struct hfi1_ibdev *ibd)
1232 ret = fault_init_opcode_debugfs(ibd);
1236 ret = fault_init_packet_debugfs(ibd);
1238 fault_exit_opcode_debugfs(ibd);
1243 bool hfi1_dbg_fault_suppress_err(struct hfi1_ibdev *ibd)
1245 return ibd->fault_suppress_err;
1248 bool hfi1_dbg_fault_opcode(struct rvt_qp *qp, u32 opcode, bool rx)
1251 struct hfi1_ibdev *ibd = to_idev(qp->ibqp.device);
1253 if (!ibd->fault_opcode || !ibd->fault_opcode->fault_by_opcode)
1255 if (ibd->fault_opcode->opcode != (opcode & ibd->fault_opcode->mask))
1257 ret = should_fail(&ibd->fault_opcode->attr, 1);
1259 trace_hfi1_fault_opcode(qp, opcode);
1261 ibd->fault_opcode->n_rxfaults[opcode]++;
1263 ibd->fault_opcode->n_txfaults[opcode]++;
1268 bool hfi1_dbg_fault_packet(struct hfi1_packet *packet)
1270 struct rvt_dev_info *rdi = &packet->rcd->ppd->dd->verbs_dev.rdi;
1271 struct hfi1_ibdev *ibd = dev_from_rdi(rdi);
1274 if (!ibd->fault_packet || !ibd->fault_packet->fault_by_packet)
1277 ret = should_fail(&ibd->fault_packet->attr, 1);
1279 ++ibd->fault_packet->n_faults;
1280 trace_hfi1_fault_packet(packet);
1286 void hfi1_dbg_ibdev_init(struct hfi1_ibdev *ibd)
1288 char name[sizeof("port0counters") + 1];
1290 struct hfi1_devdata *dd = dd_from_dev(ibd);
1291 struct hfi1_pportdata *ppd;
1292 int unit = dd->unit;
1297 snprintf(name, sizeof(name), "%s_%d", class_name(), unit);
1298 snprintf(link, sizeof(link), "%d", unit);
1299 ibd->hfi1_ibdev_dbg = debugfs_create_dir(name, hfi1_dbg_root);
1300 if (!ibd->hfi1_ibdev_dbg) {
1301 pr_warn("create of %s failed\n", name);
1304 ibd->hfi1_ibdev_link =
1305 debugfs_create_symlink(link, hfi1_dbg_root, name);
1306 if (!ibd->hfi1_ibdev_link) {
1307 pr_warn("create of %s symlink failed\n", name);
1310 DEBUGFS_SEQ_FILE_CREATE(opcode_stats, ibd->hfi1_ibdev_dbg, ibd);
1311 DEBUGFS_SEQ_FILE_CREATE(ctx_stats, ibd->hfi1_ibdev_dbg, ibd);
1312 DEBUGFS_SEQ_FILE_CREATE(qp_stats, ibd->hfi1_ibdev_dbg, ibd);
1313 DEBUGFS_SEQ_FILE_CREATE(sdes, ibd->hfi1_ibdev_dbg, ibd);
1314 DEBUGFS_SEQ_FILE_CREATE(sdma_cpu_list, ibd->hfi1_ibdev_dbg, ibd);
1315 /* dev counter files */
1316 for (i = 0; i < ARRAY_SIZE(cntr_ops); i++)
1317 DEBUGFS_FILE_CREATE(cntr_ops[i].name,
1318 ibd->hfi1_ibdev_dbg,
1320 &cntr_ops[i].ops, S_IRUGO);
1321 /* per port files */
1322 for (ppd = dd->pport, j = 0; j < dd->num_pports; j++, ppd++)
1323 for (i = 0; i < ARRAY_SIZE(port_cntr_ops); i++) {
1326 port_cntr_ops[i].name,
1328 DEBUGFS_FILE_CREATE(name,
1329 ibd->hfi1_ibdev_dbg,
1331 &port_cntr_ops[i].ops,
1332 !port_cntr_ops[i].ops.write ?
1333 S_IRUGO : S_IRUGO | S_IWUSR);
1336 #ifdef CONFIG_FAULT_INJECTION
1337 debugfs_create_bool("fault_suppress_err", 0600,
1338 ibd->hfi1_ibdev_dbg,
1339 &ibd->fault_suppress_err);
1340 fault_init_debugfs(ibd);
1344 void hfi1_dbg_ibdev_exit(struct hfi1_ibdev *ibd)
1348 #ifdef CONFIG_FAULT_INJECTION
1349 fault_exit_debugfs(ibd);
1351 debugfs_remove(ibd->hfi1_ibdev_link);
1352 debugfs_remove_recursive(ibd->hfi1_ibdev_dbg);
1354 ibd->hfi1_ibdev_dbg = NULL;
1358 * driver stats field names, one line per stat, single string. Used by
1359 * programs like hfistats to print the stats in a way which works for
1360 * different versions of drivers, without changing program source.
1361 * if hfi1_ib_stats changes, this needs to change. Names need to be
1362 * 12 chars or less (w/o newline), for proper display by hfistats utility.
1364 static const char * const hfi1_statnames[] = {
1365 /* must be element 0*/
1378 static void *_driver_stats_names_seq_start(struct seq_file *s, loff_t *pos)
1380 if (*pos >= ARRAY_SIZE(hfi1_statnames))
1385 static void *_driver_stats_names_seq_next(
1391 if (*pos >= ARRAY_SIZE(hfi1_statnames))
1396 static void _driver_stats_names_seq_stop(struct seq_file *s, void *v)
1400 static int _driver_stats_names_seq_show(struct seq_file *s, void *v)
1404 seq_printf(s, "%s\n", hfi1_statnames[*spos]);
1408 DEBUGFS_SEQ_FILE_OPS(driver_stats_names);
1409 DEBUGFS_SEQ_FILE_OPEN(driver_stats_names)
1410 DEBUGFS_FILE_OPS(driver_stats_names);
1412 static void *_driver_stats_seq_start(struct seq_file *s, loff_t *pos)
1414 if (*pos >= ARRAY_SIZE(hfi1_statnames))
1419 static void *_driver_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
1422 if (*pos >= ARRAY_SIZE(hfi1_statnames))
1427 static void _driver_stats_seq_stop(struct seq_file *s, void *v)
1431 static u64 hfi1_sps_ints(void)
1433 unsigned long flags;
1434 struct hfi1_devdata *dd;
1437 spin_lock_irqsave(&hfi1_devs_lock, flags);
1438 list_for_each_entry(dd, &hfi1_dev_list, list) {
1439 sps_ints += get_all_cpu_total(dd->int_counter);
1441 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
1445 static int _driver_stats_seq_show(struct seq_file *s, void *v)
1449 u64 *stats = (u64 *)&hfi1_stats;
1450 size_t sz = seq_get_buf(s, &buffer);
1452 if (sz < sizeof(u64))
1454 /* special case for interrupts */
1456 *(u64 *)buffer = hfi1_sps_ints();
1458 *(u64 *)buffer = stats[*spos];
1459 seq_commit(s, sizeof(u64));
1463 DEBUGFS_SEQ_FILE_OPS(driver_stats);
1464 DEBUGFS_SEQ_FILE_OPEN(driver_stats)
1465 DEBUGFS_FILE_OPS(driver_stats);
1467 void hfi1_dbg_init(void)
1469 hfi1_dbg_root = debugfs_create_dir(DRIVER_NAME, NULL);
1471 pr_warn("init of debugfs failed\n");
1472 DEBUGFS_SEQ_FILE_CREATE(driver_stats_names, hfi1_dbg_root, NULL);
1473 DEBUGFS_SEQ_FILE_CREATE(driver_stats, hfi1_dbg_root, NULL);
1476 void hfi1_dbg_exit(void)
1478 debugfs_remove_recursive(hfi1_dbg_root);
1479 hfi1_dbg_root = NULL;