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);
176 struct hfi1_ctxtdata *rcd;
178 for (j = 0; j < dd->first_dyn_alloc_ctxt; j++) {
179 rcd = hfi1_rcd_get_by_index(dd, j);
181 n_packets += rcd->opstats->stats[i].n_packets;
182 n_bytes += rcd->opstats->stats[i].n_bytes;
186 if (!n_packets && !n_bytes)
188 seq_printf(s, "%02llx %llu/%llu\n", i,
189 (unsigned long long)n_packets,
190 (unsigned long long)n_bytes);
195 DEBUGFS_SEQ_FILE_OPS(opcode_stats);
196 DEBUGFS_SEQ_FILE_OPEN(opcode_stats)
197 DEBUGFS_FILE_OPS(opcode_stats);
199 static void *_ctx_stats_seq_start(struct seq_file *s, loff_t *pos)
201 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
202 struct hfi1_devdata *dd = dd_from_dev(ibd);
205 return SEQ_START_TOKEN;
206 if (*pos >= dd->first_dyn_alloc_ctxt)
211 static void *_ctx_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
213 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
214 struct hfi1_devdata *dd = dd_from_dev(ibd);
216 if (v == SEQ_START_TOKEN)
220 if (*pos >= dd->first_dyn_alloc_ctxt)
225 static void _ctx_stats_seq_stop(struct seq_file *s, void *v)
227 /* nothing allocated */
230 static int _ctx_stats_seq_show(struct seq_file *s, void *v)
235 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
236 struct hfi1_devdata *dd = dd_from_dev(ibd);
237 struct hfi1_ctxtdata *rcd;
239 if (v == SEQ_START_TOKEN) {
240 seq_puts(s, "Ctx:npkts\n");
247 rcd = hfi1_rcd_get_by_index(dd, i);
251 for (j = 0; j < ARRAY_SIZE(rcd->opstats->stats); j++)
252 n_packets += rcd->opstats->stats[j].n_packets;
259 seq_printf(s, " %llu:%llu\n", i, n_packets);
263 DEBUGFS_SEQ_FILE_OPS(ctx_stats);
264 DEBUGFS_SEQ_FILE_OPEN(ctx_stats)
265 DEBUGFS_FILE_OPS(ctx_stats);
267 static void *_qp_stats_seq_start(struct seq_file *s, loff_t *pos)
270 struct qp_iter *iter;
273 iter = qp_iter_init(s->private);
275 /* stop calls rcu_read_unlock */
282 if (qp_iter_next(iter)) {
291 static void *_qp_stats_seq_next(struct seq_file *s, void *iter_ptr,
295 struct qp_iter *iter = iter_ptr;
299 if (qp_iter_next(iter)) {
307 static void _qp_stats_seq_stop(struct seq_file *s, void *iter_ptr)
313 static int _qp_stats_seq_show(struct seq_file *s, void *iter_ptr)
315 struct qp_iter *iter = iter_ptr;
320 qp_iter_print(s, iter);
325 DEBUGFS_SEQ_FILE_OPS(qp_stats);
326 DEBUGFS_SEQ_FILE_OPEN(qp_stats)
327 DEBUGFS_FILE_OPS(qp_stats);
329 static void *_sdes_seq_start(struct seq_file *s, loff_t *pos)
331 struct hfi1_ibdev *ibd;
332 struct hfi1_devdata *dd;
334 ibd = (struct hfi1_ibdev *)s->private;
335 dd = dd_from_dev(ibd);
336 if (!dd->per_sdma || *pos >= dd->num_sdma)
341 static void *_sdes_seq_next(struct seq_file *s, void *v, loff_t *pos)
343 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
344 struct hfi1_devdata *dd = dd_from_dev(ibd);
347 if (!dd->per_sdma || *pos >= dd->num_sdma)
352 static void _sdes_seq_stop(struct seq_file *s, void *v)
356 static int _sdes_seq_show(struct seq_file *s, void *v)
358 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
359 struct hfi1_devdata *dd = dd_from_dev(ibd);
363 sdma_seqfile_dump_sde(s, &dd->per_sdma[i]);
367 DEBUGFS_SEQ_FILE_OPS(sdes);
368 DEBUGFS_SEQ_FILE_OPEN(sdes)
369 DEBUGFS_FILE_OPS(sdes);
371 static void *_rcds_seq_start(struct seq_file *s, loff_t *pos)
373 struct hfi1_ibdev *ibd;
374 struct hfi1_devdata *dd;
376 ibd = (struct hfi1_ibdev *)s->private;
377 dd = dd_from_dev(ibd);
378 if (!dd->rcd || *pos >= dd->n_krcv_queues)
383 static void *_rcds_seq_next(struct seq_file *s, void *v, loff_t *pos)
385 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
386 struct hfi1_devdata *dd = dd_from_dev(ibd);
389 if (!dd->rcd || *pos >= dd->n_krcv_queues)
394 static void _rcds_seq_stop(struct seq_file *s, void *v)
398 static int _rcds_seq_show(struct seq_file *s, void *v)
400 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
401 struct hfi1_devdata *dd = dd_from_dev(ibd);
402 struct hfi1_ctxtdata *rcd;
406 rcd = hfi1_rcd_get_by_index(dd, i);
408 seqfile_dump_rcd(s, rcd);
413 DEBUGFS_SEQ_FILE_OPS(rcds);
414 DEBUGFS_SEQ_FILE_OPEN(rcds)
415 DEBUGFS_FILE_OPS(rcds);
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)
423 struct hfi1_devdata *dd;
426 dd = private2dd(file);
427 avail = hfi1_read_cntrs(dd, NULL, &counters);
428 rval = simple_read_from_buffer(buf, count, ppos, counters, avail);
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)
438 struct hfi1_devdata *dd;
441 dd = private2dd(file);
442 avail = hfi1_read_cntrs(dd, &names, NULL);
443 rval = simple_read_from_buffer(buf, count, ppos, names, avail);
447 struct counter_info {
449 const struct file_operations ops;
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...
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)
463 struct hfi1_devdata *dd;
466 dd = private2dd(file);
467 avail = hfi1_read_portcntrs(dd->pport, &names, NULL);
468 rval = simple_read_from_buffer(buf, count, ppos, names, avail);
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)
478 struct hfi1_pportdata *ppd;
481 ppd = private2ppd(file);
482 avail = hfi1_read_portcntrs(ppd, NULL, &counters);
483 rval = simple_read_from_buffer(buf, count, ppos, counters, avail);
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)
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",
497 this_hfi == hfi ? "this" : "other");
501 static ssize_t asic_flags_read(struct file *file, char __user *buf,
502 size_t count, loff_t *ppos)
504 struct hfi1_pportdata *ppd;
505 struct hfi1_devdata *dd;
513 ppd = private2ppd(file);
517 tmp = kmalloc(size, GFP_KERNEL);
521 scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
522 used += scnprintf(tmp + used, size - used,
523 "Resource flags: 0x%016llx\n", scratch0);
525 /* check permanent flag */
526 if (scratch0 & CR_THERM_INIT) {
527 used += scnprintf(tmp + used, size - used,
528 " 0x%08x - thermal monitoring initialized\n",
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,
536 check_dyn_flag(scratch0, tmp, size, &used, dd->hfi1_id, i,
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");
543 used += scnprintf(tmp + used, size - used, "Write bits to clear\n");
545 ret = simple_read_from_buffer(buf, count, ppos, tmp, used);
550 static ssize_t asic_flags_write(struct file *file, const char __user *buf,
551 size_t count, loff_t *ppos)
553 struct hfi1_pportdata *ppd;
554 struct hfi1_devdata *dd;
557 unsigned long long value;
561 ppd = private2ppd(file);
564 /* zero terminate and read the expected integer */
565 buff = memdup_user_nul(buf, count);
567 return PTR_ERR(buff);
569 ret = kstrtoull(buff, 0, &value);
574 /* obtain exclusive access */
575 mutex_lock(&dd->asic_data->asic_resource_mutex);
576 acquire_hw_mutex(dd);
578 scratch0 = read_csr(dd, ASIC_CFG_SCRATCH);
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);
584 release_hw_mutex(dd);
585 mutex_unlock(&dd->asic_data->asic_resource_mutex);
587 /* return the number of bytes written */
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)
599 struct hfi1_pportdata *ppd = private2ppd(file);
604 /* the checks below expect the position to be positive */
608 tmp = kzalloc(DC8051_DATA_MEM_SIZE, GFP_KERNEL);
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
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));
629 rval = simple_read_from_buffer(buf, count, ppos, tmp,
630 DC8051_DATA_MEM_SIZE);
636 static ssize_t debugfs_lcb_read(struct file *file, char __user *buf,
637 size_t count, loff_t *ppos)
639 struct hfi1_pportdata *ppd = private2ppd(file);
640 struct hfi1_devdata *dd = ppd->dd;
641 unsigned long total, csr_off;
646 /* only read 8 byte quantities */
647 if ((count % 8) != 0)
649 /* offset must be 8-byte aligned */
650 if ((*ppos % 8) != 0)
652 /* do nothing if out of range or zero count */
653 if (*ppos >= (LCB_END - LCB_START) || !count)
655 /* reduce count if needed */
656 if (*ppos + count > LCB_END - LCB_START)
657 count = (LCB_END - LCB_START) - *ppos;
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))
663 if (put_user(data, (unsigned long __user *)(buf + total)))
670 static ssize_t debugfs_lcb_write(struct file *file, const char __user *buf,
671 size_t count, loff_t *ppos)
673 struct hfi1_pportdata *ppd = private2ppd(file);
674 struct hfi1_devdata *dd = ppd->dd;
675 unsigned long total, csr_off, data;
679 /* only write 8 byte quantities */
680 if ((count % 8) != 0)
682 /* offset must be 8-byte aligned */
683 if ((*ppos % 8) != 0)
685 /* do nothing if out of range or zero count */
686 if (*ppos >= (LCB_END - LCB_START) || !count)
688 /* reduce count if needed */
689 if (*ppos + count > LCB_END - LCB_START)
690 count = (LCB_END - LCB_START) - *ppos;
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)))
696 if (write_lcb_csr(dd, csr_off, data))
704 * read the per-port QSFP data for ppd
706 static ssize_t qsfp_debugfs_dump(struct file *file, char __user *buf,
707 size_t count, loff_t *ppos)
709 struct hfi1_pportdata *ppd;
713 ppd = private2ppd(file);
714 tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
718 ret = qsfp_dump(ppd, tmp, PAGE_SIZE);
720 ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
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)
729 struct hfi1_pportdata *ppd;
736 ppd = private2ppd(file);
738 /* byte offset format: [offsetSize][i2cAddr][offsetHigh][offsetLow] */
739 i2c_addr = (*ppos >> 16) & 0xffff;
740 offset = *ppos & 0xffff;
742 /* explicitly reject invalid address 0 to catch cp and cat */
746 buff = memdup_user(buf, count);
748 return PTR_ERR(buff);
750 total_written = i2c_write(ppd, target, i2c_addr, offset, buff, count);
751 if (total_written < 0) {
756 *ppos += total_written;
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)
769 return __i2c_debugfs_write(file, buf, count, ppos, 0);
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)
776 return __i2c_debugfs_write(file, buf, count, ppos, 1);
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)
783 struct hfi1_pportdata *ppd;
790 ppd = private2ppd(file);
792 /* byte offset format: [offsetSize][i2cAddr][offsetHigh][offsetLow] */
793 i2c_addr = (*ppos >> 16) & 0xffff;
794 offset = *ppos & 0xffff;
796 /* explicitly reject invalid address 0 to catch cp and cat */
800 buff = kmalloc(count, GFP_KERNEL);
804 total_read = i2c_read(ppd, target, i2c_addr, offset, buff, count);
805 if (total_read < 0) {
812 ret = copy_to_user(buf, buff, total_read);
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)
829 return __i2c_debugfs_read(file, buf, count, ppos, 0);
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)
836 return __i2c_debugfs_read(file, buf, count, ppos, 1);
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)
843 struct hfi1_pportdata *ppd;
848 if (*ppos + count > QSFP_PAGESIZE * 4) /* base page + page00-page03 */
851 ppd = private2ppd(file);
853 buff = memdup_user(buf, count);
855 return PTR_ERR(buff);
857 total_written = qsfp_write(ppd, target, *ppos, buff, count);
858 if (total_written < 0) {
863 *ppos += total_written;
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)
876 return __qsfp_debugfs_write(file, buf, count, ppos, 0);
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)
883 return __qsfp_debugfs_write(file, buf, count, ppos, 1);
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)
890 struct hfi1_pportdata *ppd;
895 if (*ppos + count > QSFP_PAGESIZE * 4) { /* base page + page00-page03 */
900 ppd = private2ppd(file);
902 buff = kmalloc(count, GFP_KERNEL);
908 total_read = qsfp_read(ppd, target, *ppos, buff, count);
909 if (total_read < 0) {
916 ret = copy_to_user(buf, buff, total_read);
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)
934 return __qsfp_debugfs_read(file, buf, count, ppos, 0);
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)
941 return __qsfp_debugfs_read(file, buf, count, ppos, 1);
944 static int __i2c_debugfs_open(struct inode *in, struct file *fp, u32 target)
946 struct hfi1_pportdata *ppd;
949 if (!try_module_get(THIS_MODULE))
952 ppd = private2ppd(fp);
954 ret = acquire_chip_resource(ppd->dd, i2c_target(target), 0);
955 if (ret) /* failed - release the module */
956 module_put(THIS_MODULE);
961 static int i2c1_debugfs_open(struct inode *in, struct file *fp)
963 return __i2c_debugfs_open(in, fp, 0);
966 static int i2c2_debugfs_open(struct inode *in, struct file *fp)
968 return __i2c_debugfs_open(in, fp, 1);
971 static int __i2c_debugfs_release(struct inode *in, struct file *fp, u32 target)
973 struct hfi1_pportdata *ppd;
975 ppd = private2ppd(fp);
977 release_chip_resource(ppd->dd, i2c_target(target));
978 module_put(THIS_MODULE);
983 static int i2c1_debugfs_release(struct inode *in, struct file *fp)
985 return __i2c_debugfs_release(in, fp, 0);
988 static int i2c2_debugfs_release(struct inode *in, struct file *fp)
990 return __i2c_debugfs_release(in, fp, 1);
993 static int __qsfp_debugfs_open(struct inode *in, struct file *fp, u32 target)
995 struct hfi1_pportdata *ppd;
998 if (!try_module_get(THIS_MODULE))
1001 ppd = private2ppd(fp);
1003 ret = acquire_chip_resource(ppd->dd, i2c_target(target), 0);
1004 if (ret) /* failed - release the module */
1005 module_put(THIS_MODULE);
1010 static int qsfp1_debugfs_open(struct inode *in, struct file *fp)
1012 return __qsfp_debugfs_open(in, fp, 0);
1015 static int qsfp2_debugfs_open(struct inode *in, struct file *fp)
1017 return __qsfp_debugfs_open(in, fp, 1);
1020 static int __qsfp_debugfs_release(struct inode *in, struct file *fp, u32 target)
1022 struct hfi1_pportdata *ppd;
1024 ppd = private2ppd(fp);
1026 release_chip_resource(ppd->dd, i2c_target(target));
1027 module_put(THIS_MODULE);
1032 static int qsfp1_debugfs_release(struct inode *in, struct file *fp)
1034 return __qsfp_debugfs_release(in, fp, 0);
1037 static int qsfp2_debugfs_release(struct inode *in, struct file *fp)
1039 return __qsfp_debugfs_release(in, fp, 1);
1042 #define DEBUGFS_OPS(nm, readroutine, writeroutine) \
1046 .read = readroutine, \
1047 .write = writeroutine, \
1048 .llseek = generic_file_llseek, \
1052 #define DEBUGFS_XOPS(nm, readf, writef, openf, releasef) \
1058 .llseek = generic_file_llseek, \
1060 .release = releasef \
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),
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),
1086 static void *_sdma_cpu_list_seq_start(struct seq_file *s, loff_t *pos)
1088 if (*pos >= num_online_cpus())
1094 static void *_sdma_cpu_list_seq_next(struct seq_file *s, void *v, loff_t *pos)
1097 if (*pos >= num_online_cpus())
1103 static void _sdma_cpu_list_seq_stop(struct seq_file *s, void *v)
1105 /* nothing allocated */
1108 static int _sdma_cpu_list_seq_show(struct seq_file *s, void *v)
1110 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
1111 struct hfi1_devdata *dd = dd_from_dev(ibd);
1115 sdma_seqfile_dump_cpu_list(s, dd, (unsigned long)i);
1119 DEBUGFS_SEQ_FILE_OPS(sdma_cpu_list);
1120 DEBUGFS_SEQ_FILE_OPEN(sdma_cpu_list)
1121 DEBUGFS_FILE_OPS(sdma_cpu_list);
1123 #ifdef CONFIG_FAULT_INJECTION
1124 static void *_fault_stats_seq_start(struct seq_file *s, loff_t *pos)
1126 struct hfi1_opcode_stats_perctx *opstats;
1128 if (*pos >= ARRAY_SIZE(opstats->stats))
1133 static void *_fault_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
1135 struct hfi1_opcode_stats_perctx *opstats;
1138 if (*pos >= ARRAY_SIZE(opstats->stats))
1143 static void _fault_stats_seq_stop(struct seq_file *s, void *v)
1147 static int _fault_stats_seq_show(struct seq_file *s, void *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;
1156 for (j = 0; j < dd->first_dyn_alloc_ctxt; j++) {
1157 rcd = hfi1_rcd_get_by_index(dd, j);
1159 n_packets += rcd->opstats->stats[i].n_packets;
1160 n_bytes += rcd->opstats->stats[i].n_bytes;
1164 if (!n_packets && !n_bytes)
1166 if (!ibd->fault_opcode->n_rxfaults[i] &&
1167 !ibd->fault_opcode->n_txfaults[i])
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]);
1177 DEBUGFS_SEQ_FILE_OPS(fault_stats);
1178 DEBUGFS_SEQ_FILE_OPEN(fault_stats);
1179 DEBUGFS_FILE_OPS(fault_stats);
1181 static void fault_exit_opcode_debugfs(struct hfi1_ibdev *ibd)
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);
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))
1219 if (!debugfs_create_x8("opcode", 0600, ibd->fault_opcode->dir,
1220 &ibd->fault_opcode->opcode))
1222 if (!debugfs_create_x8("mask", 0600, ibd->fault_opcode->dir,
1223 &ibd->fault_opcode->mask))
1228 fault_exit_opcode_debugfs(ibd);
1232 static void fault_exit_packet_debugfs(struct hfi1_ibdev *ibd)
1234 debugfs_remove_recursive(ibd->fault_packet->dir);
1235 kfree(ibd->fault_packet);
1236 ibd->fault_packet = NULL;
1239 static int fault_init_packet_debugfs(struct hfi1_ibdev *ibd)
1241 struct dentry *parent = ibd->hfi1_ibdev_dbg;
1243 ibd->fault_packet = kzalloc(sizeof(*ibd->fault_packet), GFP_KERNEL);
1244 if (!ibd->fault_packet)
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;
1254 ibd->fault_packet->dir =
1255 fault_create_debugfs_attr("fault_packet",
1257 &ibd->fault_opcode->attr);
1258 if (IS_ERR(ibd->fault_packet->dir)) {
1259 kfree(ibd->fault_packet);
1263 if (!debugfs_create_bool("fault_by_packet", 0600,
1264 ibd->fault_packet->dir,
1265 &ibd->fault_packet->fault_by_packet))
1267 if (!debugfs_create_u64("fault_stats", 0400,
1268 ibd->fault_packet->dir,
1269 &ibd->fault_packet->n_faults))
1274 fault_exit_packet_debugfs(ibd);
1278 static void fault_exit_debugfs(struct hfi1_ibdev *ibd)
1280 fault_exit_opcode_debugfs(ibd);
1281 fault_exit_packet_debugfs(ibd);
1284 static int fault_init_debugfs(struct hfi1_ibdev *ibd)
1288 ret = fault_init_opcode_debugfs(ibd);
1292 ret = fault_init_packet_debugfs(ibd);
1294 fault_exit_opcode_debugfs(ibd);
1299 bool hfi1_dbg_fault_suppress_err(struct hfi1_ibdev *ibd)
1301 return ibd->fault_suppress_err;
1304 bool hfi1_dbg_fault_opcode(struct rvt_qp *qp, u32 opcode, bool rx)
1307 struct hfi1_ibdev *ibd = to_idev(qp->ibqp.device);
1309 if (!ibd->fault_opcode || !ibd->fault_opcode->fault_by_opcode)
1311 if (ibd->fault_opcode->opcode != (opcode & ibd->fault_opcode->mask))
1313 ret = should_fail(&ibd->fault_opcode->attr, 1);
1315 trace_hfi1_fault_opcode(qp, opcode);
1317 ibd->fault_opcode->n_rxfaults[opcode]++;
1319 ibd->fault_opcode->n_txfaults[opcode]++;
1324 bool hfi1_dbg_fault_packet(struct hfi1_packet *packet)
1326 struct rvt_dev_info *rdi = &packet->rcd->ppd->dd->verbs_dev.rdi;
1327 struct hfi1_ibdev *ibd = dev_from_rdi(rdi);
1330 if (!ibd->fault_packet || !ibd->fault_packet->fault_by_packet)
1333 ret = should_fail(&ibd->fault_packet->attr, 1);
1335 ++ibd->fault_packet->n_faults;
1336 trace_hfi1_fault_packet(packet);
1342 void hfi1_dbg_ibdev_init(struct hfi1_ibdev *ibd)
1344 char name[sizeof("port0counters") + 1];
1346 struct hfi1_devdata *dd = dd_from_dev(ibd);
1347 struct hfi1_pportdata *ppd;
1348 int unit = dd->unit;
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);
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);
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,
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++) {
1383 port_cntr_ops[i].name,
1385 DEBUGFS_FILE_CREATE(name,
1386 ibd->hfi1_ibdev_dbg,
1388 &port_cntr_ops[i].ops,
1389 !port_cntr_ops[i].ops.write ?
1390 S_IRUGO : S_IRUGO | S_IWUSR);
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);
1401 void hfi1_dbg_ibdev_exit(struct hfi1_ibdev *ibd)
1405 #ifdef CONFIG_FAULT_INJECTION
1406 fault_exit_debugfs(ibd);
1408 debugfs_remove(ibd->hfi1_ibdev_link);
1409 debugfs_remove_recursive(ibd->hfi1_ibdev_dbg);
1411 ibd->hfi1_ibdev_dbg = NULL;
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.
1421 static const char * const hfi1_statnames[] = {
1422 /* must be element 0*/
1435 static void *_driver_stats_names_seq_start(struct seq_file *s, loff_t *pos)
1437 if (*pos >= ARRAY_SIZE(hfi1_statnames))
1442 static void *_driver_stats_names_seq_next(
1448 if (*pos >= ARRAY_SIZE(hfi1_statnames))
1453 static void _driver_stats_names_seq_stop(struct seq_file *s, void *v)
1457 static int _driver_stats_names_seq_show(struct seq_file *s, void *v)
1461 seq_printf(s, "%s\n", hfi1_statnames[*spos]);
1465 DEBUGFS_SEQ_FILE_OPS(driver_stats_names);
1466 DEBUGFS_SEQ_FILE_OPEN(driver_stats_names)
1467 DEBUGFS_FILE_OPS(driver_stats_names);
1469 static void *_driver_stats_seq_start(struct seq_file *s, loff_t *pos)
1471 if (*pos >= ARRAY_SIZE(hfi1_statnames))
1476 static void *_driver_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
1479 if (*pos >= ARRAY_SIZE(hfi1_statnames))
1484 static void _driver_stats_seq_stop(struct seq_file *s, void *v)
1488 static u64 hfi1_sps_ints(void)
1490 unsigned long flags;
1491 struct hfi1_devdata *dd;
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);
1498 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
1502 static int _driver_stats_seq_show(struct seq_file *s, void *v)
1506 u64 *stats = (u64 *)&hfi1_stats;
1507 size_t sz = seq_get_buf(s, &buffer);
1509 if (sz < sizeof(u64))
1511 /* special case for interrupts */
1513 *(u64 *)buffer = hfi1_sps_ints();
1515 *(u64 *)buffer = stats[*spos];
1516 seq_commit(s, sizeof(u64));
1520 DEBUGFS_SEQ_FILE_OPS(driver_stats);
1521 DEBUGFS_SEQ_FILE_OPEN(driver_stats)
1522 DEBUGFS_FILE_OPS(driver_stats);
1524 void hfi1_dbg_init(void)
1526 hfi1_dbg_root = debugfs_create_dir(DRIVER_NAME, NULL);
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);
1533 void hfi1_dbg_exit(void)
1535 debugfs_remove_recursive(hfi1_dbg_root);
1536 hfi1_dbg_root = NULL;