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 * Copyright(c) 2015 Intel Corporation.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
22 * Copyright(c) 2015 Intel Corporation.
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
28 * - Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * - Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in
32 * the documentation and/or other materials provided with the
34 * - Neither the name of Intel Corporation nor the names of its
35 * contributors may be used to endorse or promote products derived
36 * from this software without specific prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
41 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
42 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
45 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
46 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
48 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 #include <linux/debugfs.h>
52 #include <linux/seq_file.h>
53 #include <linux/kernel.h>
54 #include <linux/export.h>
62 static struct dentry *hfi1_dbg_root;
64 #define private2dd(file) (file_inode(file)->i_private)
65 #define private2ppd(file) (file_inode(file)->i_private)
67 #define DEBUGFS_SEQ_FILE_OPS(name) \
68 static const struct seq_operations _##name##_seq_ops = { \
69 .start = _##name##_seq_start, \
70 .next = _##name##_seq_next, \
71 .stop = _##name##_seq_stop, \
72 .show = _##name##_seq_show \
74 #define DEBUGFS_SEQ_FILE_OPEN(name) \
75 static int _##name##_open(struct inode *inode, struct file *s) \
77 struct seq_file *seq; \
79 ret = seq_open(s, &_##name##_seq_ops); \
82 seq = s->private_data; \
83 seq->private = inode->i_private; \
87 #define DEBUGFS_FILE_OPS(name) \
88 static const struct file_operations _##name##_file_ops = { \
89 .owner = THIS_MODULE, \
90 .open = _##name##_open, \
92 .llseek = seq_lseek, \
93 .release = seq_release \
96 #define DEBUGFS_FILE_CREATE(name, parent, data, ops, mode) \
99 ent = debugfs_create_file(name, mode, parent, \
102 pr_warn("create of %s failed\n", name); \
106 #define DEBUGFS_SEQ_FILE_CREATE(name, parent, data) \
107 DEBUGFS_FILE_CREATE(#name, parent, data, &_##name##_file_ops, S_IRUGO)
109 static void *_opcode_stats_seq_start(struct seq_file *s, loff_t *pos)
112 struct hfi1_opcode_stats_perctx *opstats;
115 if (*pos >= ARRAY_SIZE(opstats->stats))
120 static void *_opcode_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
122 struct hfi1_opcode_stats_perctx *opstats;
125 if (*pos >= ARRAY_SIZE(opstats->stats))
131 static void _opcode_stats_seq_stop(struct seq_file *s, void *v)
137 static int _opcode_stats_seq_show(struct seq_file *s, void *v)
141 u64 n_packets = 0, n_bytes = 0;
142 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
143 struct hfi1_devdata *dd = dd_from_dev(ibd);
145 for (j = 0; j < dd->first_user_ctxt; j++) {
148 n_packets += dd->rcd[j]->opstats->stats[i].n_packets;
149 n_bytes += dd->rcd[j]->opstats->stats[i].n_bytes;
151 if (!n_packets && !n_bytes)
153 seq_printf(s, "%02llx %llu/%llu\n", i,
154 (unsigned long long) n_packets,
155 (unsigned long long) n_bytes);
160 DEBUGFS_SEQ_FILE_OPS(opcode_stats);
161 DEBUGFS_SEQ_FILE_OPEN(opcode_stats)
162 DEBUGFS_FILE_OPS(opcode_stats);
164 static void *_ctx_stats_seq_start(struct seq_file *s, loff_t *pos)
166 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
167 struct hfi1_devdata *dd = dd_from_dev(ibd);
170 return SEQ_START_TOKEN;
171 if (*pos >= dd->first_user_ctxt)
176 static void *_ctx_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
178 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
179 struct hfi1_devdata *dd = dd_from_dev(ibd);
181 if (v == SEQ_START_TOKEN)
185 if (*pos >= dd->first_user_ctxt)
190 static void _ctx_stats_seq_stop(struct seq_file *s, void *v)
192 /* nothing allocated */
195 static int _ctx_stats_seq_show(struct seq_file *s, void *v)
200 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
201 struct hfi1_devdata *dd = dd_from_dev(ibd);
203 if (v == SEQ_START_TOKEN) {
204 seq_puts(s, "Ctx:npkts\n");
214 for (j = 0; j < ARRAY_SIZE(dd->rcd[i]->opstats->stats); j++)
215 n_packets += dd->rcd[i]->opstats->stats[j].n_packets;
220 seq_printf(s, " %llu:%llu\n", i, n_packets);
224 DEBUGFS_SEQ_FILE_OPS(ctx_stats);
225 DEBUGFS_SEQ_FILE_OPEN(ctx_stats)
226 DEBUGFS_FILE_OPS(ctx_stats);
228 static void *_qp_stats_seq_start(struct seq_file *s, loff_t *pos)
231 struct qp_iter *iter;
235 iter = qp_iter_init(s->private);
240 if (qp_iter_next(iter)) {
249 static void *_qp_stats_seq_next(struct seq_file *s, void *iter_ptr,
252 struct qp_iter *iter = iter_ptr;
256 if (qp_iter_next(iter)) {
264 static void _qp_stats_seq_stop(struct seq_file *s, void *iter_ptr)
270 static int _qp_stats_seq_show(struct seq_file *s, void *iter_ptr)
272 struct qp_iter *iter = iter_ptr;
277 qp_iter_print(s, iter);
282 DEBUGFS_SEQ_FILE_OPS(qp_stats);
283 DEBUGFS_SEQ_FILE_OPEN(qp_stats)
284 DEBUGFS_FILE_OPS(qp_stats);
286 static void *_sdes_seq_start(struct seq_file *s, loff_t *pos)
289 struct hfi1_ibdev *ibd;
290 struct hfi1_devdata *dd;
293 ibd = (struct hfi1_ibdev *)s->private;
294 dd = dd_from_dev(ibd);
295 if (!dd->per_sdma || *pos >= dd->num_sdma)
300 static void *_sdes_seq_next(struct seq_file *s, void *v, loff_t *pos)
302 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
303 struct hfi1_devdata *dd = dd_from_dev(ibd);
306 if (!dd->per_sdma || *pos >= dd->num_sdma)
312 static void _sdes_seq_stop(struct seq_file *s, void *v)
318 static int _sdes_seq_show(struct seq_file *s, void *v)
320 struct hfi1_ibdev *ibd = (struct hfi1_ibdev *)s->private;
321 struct hfi1_devdata *dd = dd_from_dev(ibd);
325 sdma_seqfile_dump_sde(s, &dd->per_sdma[i]);
329 DEBUGFS_SEQ_FILE_OPS(sdes);
330 DEBUGFS_SEQ_FILE_OPEN(sdes)
331 DEBUGFS_FILE_OPS(sdes);
333 /* read the per-device counters */
334 static ssize_t dev_counters_read(struct file *file, char __user *buf,
335 size_t count, loff_t *ppos)
339 struct hfi1_devdata *dd;
343 dd = private2dd(file);
344 avail = hfi1_read_cntrs(dd, *ppos, NULL, &counters);
345 rval = simple_read_from_buffer(buf, count, ppos, counters, avail);
350 /* read the per-device counters */
351 static ssize_t dev_names_read(struct file *file, char __user *buf,
352 size_t count, loff_t *ppos)
356 struct hfi1_devdata *dd;
360 dd = private2dd(file);
361 avail = hfi1_read_cntrs(dd, *ppos, &names, NULL);
362 rval = simple_read_from_buffer(buf, count, ppos, names, avail);
367 struct counter_info {
369 const struct file_operations ops;
373 * Could use file_inode(file)->i_ino to figure out which file,
374 * instead of separate routine for each, but for now, this works...
377 /* read the per-port names (same for each port) */
378 static ssize_t portnames_read(struct file *file, char __user *buf,
379 size_t count, loff_t *ppos)
383 struct hfi1_devdata *dd;
387 dd = private2dd(file);
388 /* port number n/a here since names are constant */
389 avail = hfi1_read_portcntrs(dd, *ppos, 0, &names, NULL);
390 rval = simple_read_from_buffer(buf, count, ppos, names, avail);
395 /* read the per-port counters */
396 static ssize_t portcntrs_debugfs_read(struct file *file, char __user *buf,
397 size_t count, loff_t *ppos)
401 struct hfi1_devdata *dd;
402 struct hfi1_pportdata *ppd;
406 ppd = private2ppd(file);
408 avail = hfi1_read_portcntrs(dd, *ppos, ppd->port - 1, NULL, &counters);
409 rval = simple_read_from_buffer(buf, count, ppos, counters, avail);
415 * read the per-port QSFP data for ppd
417 static ssize_t qsfp_debugfs_dump(struct file *file, char __user *buf,
418 size_t count, loff_t *ppos)
420 struct hfi1_pportdata *ppd;
425 ppd = private2ppd(file);
426 tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
432 ret = qsfp_dump(ppd, tmp, PAGE_SIZE);
434 ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
440 /* Do an i2c write operation on the chain for the given HFI. */
441 static ssize_t __i2c_debugfs_write(struct file *file, const char __user *buf,
442 size_t count, loff_t *ppos, u32 target)
444 struct hfi1_pportdata *ppd;
452 ppd = private2ppd(file);
454 buff = kmalloc(count, GFP_KERNEL);
460 ret = copy_from_user(buff, buf, count);
466 i2c_addr = (*ppos >> 16) & 0xff;
467 offset = *ppos & 0xffff;
469 total_written = i2c_write(ppd, target, i2c_addr, offset, buff, count);
470 if (total_written < 0) {
475 *ppos += total_written;
486 /* Do an i2c write operation on chain for HFI 0. */
487 static ssize_t i2c1_debugfs_write(struct file *file, const char __user *buf,
488 size_t count, loff_t *ppos)
490 return __i2c_debugfs_write(file, buf, count, ppos, 0);
493 /* Do an i2c write operation on chain for HFI 1. */
494 static ssize_t i2c2_debugfs_write(struct file *file, const char __user *buf,
495 size_t count, loff_t *ppos)
497 return __i2c_debugfs_write(file, buf, count, ppos, 1);
500 /* Do an i2c read operation on the chain for the given HFI. */
501 static ssize_t __i2c_debugfs_read(struct file *file, char __user *buf,
502 size_t count, loff_t *ppos, u32 target)
504 struct hfi1_pportdata *ppd;
512 ppd = private2ppd(file);
514 buff = kmalloc(count, GFP_KERNEL);
520 i2c_addr = (*ppos >> 16) & 0xff;
521 offset = *ppos & 0xffff;
523 total_read = i2c_read(ppd, target, i2c_addr, offset, buff, count);
524 if (total_read < 0) {
531 ret = copy_to_user(buf, buff, total_read);
546 /* Do an i2c read operation on chain for HFI 0. */
547 static ssize_t i2c1_debugfs_read(struct file *file, char __user *buf,
548 size_t count, loff_t *ppos)
550 return __i2c_debugfs_read(file, buf, count, ppos, 0);
553 /* Do an i2c read operation on chain for HFI 1. */
554 static ssize_t i2c2_debugfs_read(struct file *file, char __user *buf,
555 size_t count, loff_t *ppos)
557 return __i2c_debugfs_read(file, buf, count, ppos, 1);
560 /* Do a QSFP write operation on the i2c chain for the given HFI. */
561 static ssize_t __qsfp_debugfs_write(struct file *file, const char __user *buf,
562 size_t count, loff_t *ppos, u32 target)
564 struct hfi1_pportdata *ppd;
570 if (*ppos + count > QSFP_PAGESIZE * 4) { /* base page + page00-page03 */
575 ppd = private2ppd(file);
577 buff = kmalloc(count, GFP_KERNEL);
583 ret = copy_from_user(buff, buf, count);
589 total_written = qsfp_write(ppd, target, *ppos, buff, count);
590 if (total_written < 0) {
595 *ppos += total_written;
606 /* Do a QSFP write operation on i2c chain for HFI 0. */
607 static ssize_t qsfp1_debugfs_write(struct file *file, const char __user *buf,
608 size_t count, loff_t *ppos)
610 return __qsfp_debugfs_write(file, buf, count, ppos, 0);
613 /* Do a QSFP write operation on i2c chain for HFI 1. */
614 static ssize_t qsfp2_debugfs_write(struct file *file, const char __user *buf,
615 size_t count, loff_t *ppos)
617 return __qsfp_debugfs_write(file, buf, count, ppos, 1);
620 /* Do a QSFP read operation on the i2c chain for the given HFI. */
621 static ssize_t __qsfp_debugfs_read(struct file *file, char __user *buf,
622 size_t count, loff_t *ppos, u32 target)
624 struct hfi1_pportdata *ppd;
630 if (*ppos + count > QSFP_PAGESIZE * 4) { /* base page + page00-page03 */
635 ppd = private2ppd(file);
637 buff = kmalloc(count, GFP_KERNEL);
643 total_read = qsfp_read(ppd, target, *ppos, buff, count);
644 if (total_read < 0) {
651 ret = copy_to_user(buf, buff, total_read);
666 /* Do a QSFP read operation on i2c chain for HFI 0. */
667 static ssize_t qsfp1_debugfs_read(struct file *file, char __user *buf,
668 size_t count, loff_t *ppos)
670 return __qsfp_debugfs_read(file, buf, count, ppos, 0);
673 /* Do a QSFP read operation on i2c chain for HFI 1. */
674 static ssize_t qsfp2_debugfs_read(struct file *file, char __user *buf,
675 size_t count, loff_t *ppos)
677 return __qsfp_debugfs_read(file, buf, count, ppos, 1);
680 #define DEBUGFS_OPS(nm, readroutine, writeroutine) \
684 .read = readroutine, \
685 .write = writeroutine, \
686 .llseek = generic_file_llseek, \
690 static const struct counter_info cntr_ops[] = {
691 DEBUGFS_OPS("counter_names", dev_names_read, NULL),
692 DEBUGFS_OPS("counters", dev_counters_read, NULL),
693 DEBUGFS_OPS("portcounter_names", portnames_read, NULL),
696 static const struct counter_info port_cntr_ops[] = {
697 DEBUGFS_OPS("port%dcounters", portcntrs_debugfs_read, NULL),
698 DEBUGFS_OPS("i2c1", i2c1_debugfs_read, i2c1_debugfs_write),
699 DEBUGFS_OPS("i2c2", i2c2_debugfs_read, i2c2_debugfs_write),
700 DEBUGFS_OPS("qsfp_dump%d", qsfp_debugfs_dump, NULL),
701 DEBUGFS_OPS("qsfp1", qsfp1_debugfs_read, qsfp1_debugfs_write),
702 DEBUGFS_OPS("qsfp2", qsfp2_debugfs_read, qsfp2_debugfs_write),
705 void hfi1_dbg_ibdev_init(struct hfi1_ibdev *ibd)
707 char name[sizeof("port0counters") + 1];
709 struct hfi1_devdata *dd = dd_from_dev(ibd);
710 struct hfi1_pportdata *ppd;
716 snprintf(name, sizeof(name), "%s_%d", class_name(), unit);
717 snprintf(link, sizeof(link), "%d", unit);
718 ibd->hfi1_ibdev_dbg = debugfs_create_dir(name, hfi1_dbg_root);
719 if (!ibd->hfi1_ibdev_dbg) {
720 pr_warn("create of %s failed\n", name);
723 ibd->hfi1_ibdev_link =
724 debugfs_create_symlink(link, hfi1_dbg_root, name);
725 if (!ibd->hfi1_ibdev_link) {
726 pr_warn("create of %s symlink failed\n", name);
729 DEBUGFS_SEQ_FILE_CREATE(opcode_stats, ibd->hfi1_ibdev_dbg, ibd);
730 DEBUGFS_SEQ_FILE_CREATE(ctx_stats, ibd->hfi1_ibdev_dbg, ibd);
731 DEBUGFS_SEQ_FILE_CREATE(qp_stats, ibd->hfi1_ibdev_dbg, ibd);
732 DEBUGFS_SEQ_FILE_CREATE(sdes, ibd->hfi1_ibdev_dbg, ibd);
733 /* dev counter files */
734 for (i = 0; i < ARRAY_SIZE(cntr_ops); i++)
735 DEBUGFS_FILE_CREATE(cntr_ops[i].name,
738 &cntr_ops[i].ops, S_IRUGO);
740 for (ppd = dd->pport, j = 0; j < dd->num_pports; j++, ppd++)
741 for (i = 0; i < ARRAY_SIZE(port_cntr_ops); i++) {
744 port_cntr_ops[i].name,
746 DEBUGFS_FILE_CREATE(name,
749 &port_cntr_ops[i].ops,
750 port_cntr_ops[i].ops.write == NULL ?
751 S_IRUGO : S_IRUGO|S_IWUSR);
755 void hfi1_dbg_ibdev_exit(struct hfi1_ibdev *ibd)
759 debugfs_remove(ibd->hfi1_ibdev_link);
760 debugfs_remove_recursive(ibd->hfi1_ibdev_dbg);
762 ibd->hfi1_ibdev_dbg = NULL;
767 * driver stats field names, one line per stat, single string. Used by
768 * programs like hfistats to print the stats in a way which works for
769 * different versions of drivers, without changing program source.
770 * if hfi1_ib_stats changes, this needs to change. Names need to be
771 * 12 chars or less (w/o newline), for proper display by hfistats utility.
773 static const char * const hfi1_statnames[] = {
774 /* must be element 0*/
787 static void *_driver_stats_names_seq_start(struct seq_file *s, loff_t *pos)
791 if (*pos >= ARRAY_SIZE(hfi1_statnames))
796 static void *_driver_stats_names_seq_next(
802 if (*pos >= ARRAY_SIZE(hfi1_statnames))
807 static void _driver_stats_names_seq_stop(struct seq_file *s, void *v)
813 static int _driver_stats_names_seq_show(struct seq_file *s, void *v)
817 seq_printf(s, "%s\n", hfi1_statnames[*spos]);
821 DEBUGFS_SEQ_FILE_OPS(driver_stats_names);
822 DEBUGFS_SEQ_FILE_OPEN(driver_stats_names)
823 DEBUGFS_FILE_OPS(driver_stats_names);
825 static void *_driver_stats_seq_start(struct seq_file *s, loff_t *pos)
829 if (*pos >= ARRAY_SIZE(hfi1_statnames))
834 static void *_driver_stats_seq_next(struct seq_file *s, void *v, loff_t *pos)
837 if (*pos >= ARRAY_SIZE(hfi1_statnames))
842 static void _driver_stats_seq_stop(struct seq_file *s, void *v)
848 static u64 hfi1_sps_ints(void)
851 struct hfi1_devdata *dd;
854 spin_lock_irqsave(&hfi1_devs_lock, flags);
855 list_for_each_entry(dd, &hfi1_dev_list, list) {
856 sps_ints += get_all_cpu_total(dd->int_counter);
858 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
862 static int _driver_stats_seq_show(struct seq_file *s, void *v)
866 u64 *stats = (u64 *)&hfi1_stats;
867 size_t sz = seq_get_buf(s, &buffer);
869 if (sz < sizeof(u64))
871 /* special case for interrupts */
873 *(u64 *)buffer = hfi1_sps_ints();
875 *(u64 *)buffer = stats[*spos];
876 seq_commit(s, sizeof(u64));
880 DEBUGFS_SEQ_FILE_OPS(driver_stats);
881 DEBUGFS_SEQ_FILE_OPEN(driver_stats)
882 DEBUGFS_FILE_OPS(driver_stats);
884 void hfi1_dbg_init(void)
886 hfi1_dbg_root = debugfs_create_dir(DRIVER_NAME, NULL);
888 pr_warn("init of debugfs failed\n");
889 DEBUGFS_SEQ_FILE_CREATE(driver_stats_names, hfi1_dbg_root, NULL);
890 DEBUGFS_SEQ_FILE_CREATE(driver_stats, hfi1_dbg_root, NULL);
893 void hfi1_dbg_exit(void)
895 debugfs_remove_recursive(hfi1_dbg_root);
896 hfi1_dbg_root = NULL;